[1ec501c] | 1 | /* clock.c |
---|
| 2 | * |
---|
| 3 | * This routine initializes the PIT on the MPC8xx. |
---|
| 4 | * The tick frequency is specified by the bsp. |
---|
| 5 | * |
---|
| 6 | * Author: Jay Monkman (jmonkman@frasca.com) |
---|
| 7 | * Copyright (C) 1998 by Frasca International, Inc. |
---|
| 8 | * |
---|
| 9 | * Derived from c/src/lib/libcpu/ppc/ppc403/clock/clock.c: |
---|
| 10 | * |
---|
| 11 | * Author: Andrew Bray <andy@i-cubed.co.uk> |
---|
| 12 | * |
---|
| 13 | * COPYRIGHT (c) 1995 by i-cubed ltd. |
---|
| 14 | * |
---|
| 15 | * To anyone who acknowledges that this file is provided "AS IS" |
---|
| 16 | * without any express or implied warranty: |
---|
| 17 | * permission to use, copy, modify, and distribute this file |
---|
| 18 | * for any purpose is hereby granted without fee, provided that |
---|
| 19 | * the above copyright notice and this notice appears in all |
---|
| 20 | * copies, and that the name of i-cubed limited not be used in |
---|
| 21 | * advertising or publicity pertaining to distribution of the |
---|
| 22 | * software without specific, written prior permission. |
---|
| 23 | * i-cubed limited makes no representations about the suitability |
---|
| 24 | * of this software for any purpose. |
---|
| 25 | * |
---|
| 26 | * Derived from c/src/lib/libcpu/hppa1_1/clock/clock.c: |
---|
| 27 | * |
---|
| 28 | * COPYRIGHT (c) 1989-1998. |
---|
| 29 | * On-Line Applications Research Corporation (OAR). |
---|
| 30 | * |
---|
| 31 | * The license and distribution terms for this file may be |
---|
| 32 | * found in the file LICENSE in this distribution or at |
---|
[21e1c44] | 33 | * http://www.rtems.com/license/LICENSE. |
---|
[1ec501c] | 34 | * |
---|
| 35 | * $Id$ |
---|
| 36 | */ |
---|
| 37 | |
---|
| 38 | #include <rtems.h> |
---|
| 39 | #include <clockdrv.h> |
---|
| 40 | #include <rtems/libio.h> |
---|
| 41 | |
---|
| 42 | #include <stdlib.h> /* for atexit() */ |
---|
| 43 | #include <mpc8260.h> |
---|
| 44 | #include <bsp/irq.h> |
---|
| 45 | |
---|
| 46 | volatile rtems_unsigned32 Clock_driver_ticks; |
---|
| 47 | extern int BSP_get_clock_irq_level(); |
---|
| 48 | extern int BSP_connect_clock_handler(rtems_isr_entry); |
---|
| 49 | extern int BSP_disconnect_clock_handler(); |
---|
| 50 | |
---|
| 51 | void Clock_exit( void ); |
---|
| 52 | |
---|
| 53 | rtems_unsigned32 decrementer_value; |
---|
| 54 | |
---|
| 55 | volatile int ClockInitialised = 0; |
---|
| 56 | |
---|
| 57 | |
---|
| 58 | /* |
---|
| 59 | * These are set by clock driver during its init |
---|
| 60 | */ |
---|
| 61 | |
---|
| 62 | rtems_device_major_number rtems_clock_major = ~0; |
---|
| 63 | rtems_device_minor_number rtems_clock_minor; |
---|
| 64 | |
---|
| 65 | /* |
---|
| 66 | * ISR Handler |
---|
| 67 | */ |
---|
| 68 | rtems_isr Clock_isr(rtems_vector_number vector) |
---|
| 69 | { |
---|
| 70 | int clicks; |
---|
| 71 | |
---|
| 72 | if( ClockInitialised ) { |
---|
| 73 | PPC_Get_decrementer( clicks ); |
---|
| 74 | do { |
---|
| 75 | clicks += decrementer_value; |
---|
| 76 | PPC_Set_decrementer( clicks ); |
---|
| 77 | |
---|
| 78 | Clock_driver_ticks++; |
---|
| 79 | rtems_clock_tick(); |
---|
| 80 | } while( clicks < 100 ); |
---|
| 81 | } |
---|
| 82 | #if 0 |
---|
| 83 | m8260.piscr |= M8260_PISCR_PS; |
---|
| 84 | Clock_driver_ticks++; |
---|
| 85 | rtems_clock_tick(); |
---|
| 86 | #endif |
---|
| 87 | |
---|
| 88 | |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | void clockOn(void* unused) |
---|
| 92 | { |
---|
| 93 | |
---|
| 94 | decrementer_value = rtems_configuration_get_microseconds_per_tick() * |
---|
| 95 | rtems_cpu_configuration_get_clicks_per_usec() - 1; |
---|
| 96 | |
---|
| 97 | PPC_Set_decrementer( decrementer_value ); |
---|
| 98 | Clock_driver_ticks = 0; |
---|
| 99 | |
---|
| 100 | ClockInitialised = 1; |
---|
| 101 | |
---|
| 102 | |
---|
| 103 | #if 0 |
---|
| 104 | unsigned desiredLevel; |
---|
| 105 | rtems_unsigned32 pit_value; |
---|
| 106 | |
---|
| 107 | pit_value = (rtems_configuration_get_microseconds_per_tick() * |
---|
| 108 | rtems_cpu_configuration_get_clicks_per_usec()) - 1 ; |
---|
| 109 | |
---|
| 110 | if (pit_value > 0xffff) { /* pit is only 16 bits long */ |
---|
| 111 | rtems_fatal_error_occurred(-1); |
---|
| 112 | } |
---|
| 113 | m8260.sccr &= ~(1<<24); |
---|
| 114 | m8260.pitc = pit_value; |
---|
| 115 | |
---|
| 116 | desiredLevel = BSP_get_clock_irq_level(); |
---|
| 117 | /* set PIT irq level, enable PIT, PIT interrupts */ |
---|
| 118 | /* and clear int. status */ |
---|
| 119 | m8260.piscr = /*M8260_PISCR_PIRQ(desiredLevel) |*/ |
---|
| 120 | M8260_PISCR_PTE | M8260_PISCR_PS | M8260_PISCR_PIE; |
---|
| 121 | #endif |
---|
| 122 | } |
---|
| 123 | /* |
---|
| 124 | * Called via atexit() |
---|
| 125 | * Remove the clock interrupt handler by setting handler to NULL |
---|
| 126 | */ |
---|
| 127 | void |
---|
| 128 | clockOff(void* unused) |
---|
| 129 | { |
---|
| 130 | #if 0 |
---|
| 131 | /* disable PIT and PIT interrupts */ |
---|
| 132 | m8260.piscr &= ~(M8260_PISCR_PTE | M8260_PISCR_PIE); |
---|
| 133 | #endif |
---|
| 134 | ClockInitialised = 0; |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | int clockIsOn(void* unused) |
---|
| 138 | { |
---|
| 139 | return ClockInitialised; |
---|
| 140 | #if 0 |
---|
| 141 | if (m8260.piscr & M8260_PISCR_PIE) return 1; |
---|
| 142 | return 0; |
---|
| 143 | #endif |
---|
| 144 | |
---|
| 145 | } |
---|
| 146 | |
---|
| 147 | /* |
---|
| 148 | * Called via atexit() |
---|
| 149 | * Remove the clock interrupt handler by setting handler to NULL |
---|
| 150 | */ |
---|
| 151 | void |
---|
| 152 | Clock_exit(void) |
---|
| 153 | { |
---|
| 154 | (void) BSP_disconnect_clock_handler (); |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | void Install_clock(rtems_isr_entry clock_isr) |
---|
| 158 | { |
---|
| 159 | Clock_driver_ticks = 0; |
---|
| 160 | |
---|
| 161 | |
---|
| 162 | decrementer_value = rtems_configuration_get_microseconds_per_tick() * |
---|
| 163 | rtems_cpu_configuration_get_clicks_per_usec() - 1; |
---|
| 164 | |
---|
| 165 | PPC_Set_decrementer( decrementer_value ); |
---|
| 166 | |
---|
| 167 | BSP_connect_clock_handler (clock_isr); |
---|
| 168 | |
---|
| 169 | |
---|
| 170 | ClockInitialised = 1; |
---|
| 171 | |
---|
| 172 | atexit(Clock_exit); |
---|
| 173 | |
---|
| 174 | } |
---|
| 175 | |
---|
| 176 | void |
---|
| 177 | ReInstall_clock(rtems_isr_entry new_clock_isr) |
---|
| 178 | { |
---|
| 179 | BSP_connect_clock_handler (new_clock_isr); |
---|
| 180 | } |
---|
| 181 | |
---|
| 182 | |
---|
| 183 | rtems_device_driver Clock_initialize( |
---|
| 184 | rtems_device_major_number major, |
---|
| 185 | rtems_device_minor_number minor, |
---|
| 186 | void *pargp |
---|
| 187 | ) |
---|
| 188 | { |
---|
| 189 | Install_clock( Clock_isr ); |
---|
| 190 | |
---|
| 191 | /* |
---|
| 192 | * make major/minor avail to others such as shared memory driver |
---|
| 193 | */ |
---|
| 194 | |
---|
| 195 | rtems_clock_major = major; |
---|
| 196 | rtems_clock_minor = minor; |
---|
| 197 | |
---|
| 198 | return RTEMS_SUCCESSFUL; |
---|
| 199 | } |
---|
| 200 | |
---|
| 201 | rtems_device_driver Clock_control( |
---|
| 202 | rtems_device_major_number major, |
---|
| 203 | rtems_device_minor_number minor, |
---|
| 204 | void *pargp |
---|
| 205 | ) |
---|
| 206 | { |
---|
| 207 | rtems_libio_ioctl_args_t *args = pargp; |
---|
| 208 | |
---|
| 209 | if (args == 0) |
---|
| 210 | goto done; |
---|
| 211 | |
---|
| 212 | /* |
---|
| 213 | * This is hokey, but until we get a defined interface |
---|
| 214 | * to do this, it will just be this simple... |
---|
| 215 | */ |
---|
| 216 | |
---|
| 217 | if (args->command == rtems_build_name('I', 'S', 'R', ' ')) { |
---|
| 218 | Clock_isr(BSP_PERIODIC_TIMER); |
---|
| 219 | } |
---|
| 220 | else if (args->command == rtems_build_name('N', 'E', 'W', ' ')) { |
---|
| 221 | ReInstall_clock(args->buffer); |
---|
| 222 | } |
---|
| 223 | |
---|
| 224 | done: |
---|
| 225 | return RTEMS_SUCCESSFUL; |
---|
| 226 | } |
---|
| 227 | |
---|