[8430205] | 1 | /* clock.c |
---|
| 2 | * |
---|
| 3 | * This routine initializes the PIT on the MPC5xx. |
---|
| 4 | * The tick frequency is specified by the bsp. |
---|
| 5 | * |
---|
| 6 | * |
---|
| 7 | * MPC5xx port sponsored by Defence Research and Development Canada - Suffield |
---|
| 8 | * Copyright (C) 2004, Real-Time Systems Inc. (querbach@realtime.bc.ca) |
---|
| 9 | * |
---|
| 10 | * Derived from c/src/lib/libcpu/powerpc/mpc8xx/clock/clock.c: |
---|
| 11 | * |
---|
| 12 | * Author: Jay Monkman (jmonkman@frasca.com) |
---|
| 13 | * Copyright (C) 1998 by Frasca International, Inc. |
---|
| 14 | * |
---|
| 15 | * Derived from c/src/lib/libcpu/ppc/ppc403/clock/clock.c: |
---|
| 16 | * |
---|
| 17 | * Author: Andrew Bray <andy@i-cubed.co.uk> |
---|
| 18 | * |
---|
| 19 | * COPYRIGHT (c) 1995 by i-cubed ltd. |
---|
| 20 | * |
---|
| 21 | * To anyone who acknowledges that this file is provided "AS IS" |
---|
| 22 | * without any express or implied warranty: |
---|
| 23 | * permission to use, copy, modify, and distribute this file |
---|
| 24 | * for any purpose is hereby granted without fee, provided that |
---|
| 25 | * the above copyright notice and this notice appears in all |
---|
| 26 | * copies, and that the name of i-cubed limited not be used in |
---|
| 27 | * advertising or publicity pertaining to distribution of the |
---|
| 28 | * software without specific, written prior permission. |
---|
| 29 | * i-cubed limited makes no representations about the suitability |
---|
| 30 | * of this software for any purpose. |
---|
| 31 | * |
---|
| 32 | * Derived from c/src/lib/libcpu/hppa1_1/clock/clock.c: |
---|
| 33 | * |
---|
| 34 | * COPYRIGHT (c) 1989-1998. |
---|
| 35 | * On-Line Applications Research Corporation (OAR). |
---|
| 36 | * |
---|
| 37 | * The license and distribution terms for this file may be |
---|
| 38 | * found in the file LICENSE in this distribution or at |
---|
| 39 | * http://www.rtems.com/license/LICENSE. |
---|
| 40 | * |
---|
| 41 | * $Id$ |
---|
| 42 | */ |
---|
| 43 | |
---|
| 44 | #include <rtems.h> |
---|
| 45 | #include <clockdrv.h> |
---|
| 46 | #include <rtems/libio.h> |
---|
| 47 | #include <libcpu/irq.h> |
---|
| 48 | |
---|
| 49 | #include <stdlib.h> /* for atexit() */ |
---|
| 50 | #include <mpc5xx.h> |
---|
| 51 | |
---|
| 52 | volatile rtems_unsigned32 Clock_driver_ticks; |
---|
| 53 | extern int BSP_connect_clock_handler(rtems_isr_entry); |
---|
| 54 | extern int BSP_disconnect_clock_handler(); |
---|
| 55 | |
---|
| 56 | void Clock_exit( void ); |
---|
| 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 | usiu.piscrk = USIU_UNLOCK_KEY; |
---|
| 71 | usiu.piscr |= USIU_PISCR_PS; /* acknowledge interrupt */ |
---|
| 72 | usiu.piscrk = 0; |
---|
| 73 | |
---|
| 74 | Clock_driver_ticks++; |
---|
| 75 | rtems_clock_tick(); |
---|
| 76 | } |
---|
| 77 | |
---|
| 78 | void clockOn(void* unused) |
---|
| 79 | { |
---|
| 80 | unsigned desiredLevel; |
---|
| 81 | rtems_unsigned32 pit_value; |
---|
| 82 | |
---|
| 83 | /* calculate and set modulus */ |
---|
| 84 | pit_value = (rtems_configuration_get_microseconds_per_tick() * |
---|
| 85 | rtems_cpu_configuration_get_clicks_per_usec()) - 1 ; |
---|
| 86 | |
---|
| 87 | if (pit_value > 0xffff) { /* pit is only 16 bits long */ |
---|
| 88 | rtems_fatal_error_occurred(-1); |
---|
| 89 | } |
---|
| 90 | usiu.sccrk = USIU_UNLOCK_KEY; |
---|
| 91 | usiu.sccr &= ~USIU_SCCR_RTDIV; /* RTC and PIT clock is divided by 4 */ |
---|
| 92 | usiu.sccrk = 0; |
---|
| 93 | |
---|
| 94 | usiu.pitck = USIU_UNLOCK_KEY; |
---|
| 95 | usiu.pitc = pit_value; |
---|
| 96 | usiu.pitck = 0; |
---|
| 97 | |
---|
| 98 | /* set PIT irq level, enable PIT, PIT interrupts */ |
---|
| 99 | /* and clear int. status */ |
---|
| 100 | desiredLevel = CPU_irq_level_from_symbolic_name(CPU_PERIODIC_TIMER); |
---|
| 101 | |
---|
| 102 | usiu.piscrk = USIU_UNLOCK_KEY; |
---|
| 103 | usiu.piscr = USIU_PISCR_PIRQ(desiredLevel) /* set interrupt priority */ |
---|
| 104 | | USIU_PISCR_PS /* acknowledge interrupt */ |
---|
| 105 | | USIU_PISCR_PIE /* enable interrupt */ |
---|
| 106 | | USIU_PISCR_PITF /* freeze during debug */ |
---|
| 107 | | USIU_PISCR_PTE; /* enable timer */ |
---|
| 108 | usiu.piscrk = 0; |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | void |
---|
| 112 | clockOff(void* unused) |
---|
| 113 | { |
---|
| 114 | /* disable PIT and PIT interrupts */ |
---|
| 115 | usiu.piscrk = USIU_UNLOCK_KEY; |
---|
| 116 | usiu.piscr &= ~(USIU_PISCR_PTE | USIU_PISCR_PIE); |
---|
| 117 | usiu.piscrk = 0; |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | int clockIsOn(void* unused) |
---|
| 121 | { |
---|
| 122 | if (usiu.piscr & USIU_PISCR_PIE) |
---|
| 123 | return 1; |
---|
| 124 | return 0; |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | /* |
---|
| 128 | * Called via atexit() |
---|
| 129 | * Remove the clock interrupt handler by setting handler to NULL |
---|
| 130 | */ |
---|
| 131 | void |
---|
| 132 | Clock_exit(void) |
---|
| 133 | { |
---|
| 134 | (void) BSP_disconnect_clock_handler (); |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | void Install_clock(rtems_isr_entry clock_isr) |
---|
| 138 | { |
---|
| 139 | Clock_driver_ticks = 0; |
---|
| 140 | |
---|
| 141 | BSP_connect_clock_handler (clock_isr); |
---|
| 142 | atexit(Clock_exit); |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | void |
---|
| 146 | ReInstall_clock(rtems_isr_entry new_clock_isr) |
---|
| 147 | { |
---|
| 148 | BSP_connect_clock_handler (new_clock_isr); |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | |
---|
| 152 | rtems_device_driver Clock_initialize( |
---|
| 153 | rtems_device_major_number major, |
---|
| 154 | rtems_device_minor_number minor, |
---|
| 155 | void *pargp |
---|
| 156 | ) |
---|
| 157 | { |
---|
| 158 | Install_clock( Clock_isr ); |
---|
| 159 | |
---|
| 160 | /* |
---|
| 161 | * make major/minor avail to others such as shared memory driver |
---|
| 162 | */ |
---|
| 163 | |
---|
| 164 | rtems_clock_major = major; |
---|
| 165 | rtems_clock_minor = minor; |
---|
| 166 | |
---|
| 167 | return RTEMS_SUCCESSFUL; |
---|
| 168 | } |
---|
| 169 | |
---|
| 170 | rtems_device_driver Clock_control( |
---|
| 171 | rtems_device_major_number major, |
---|
| 172 | rtems_device_minor_number minor, |
---|
| 173 | void *pargp |
---|
| 174 | ) |
---|
| 175 | { |
---|
| 176 | rtems_libio_ioctl_args_t *args = pargp; |
---|
| 177 | |
---|
| 178 | if (args == 0) |
---|
| 179 | goto done; |
---|
| 180 | |
---|
| 181 | /* |
---|
| 182 | * This is hokey, but until we get a defined interface |
---|
| 183 | * to do this, it will just be this simple... |
---|
| 184 | */ |
---|
| 185 | |
---|
| 186 | if (args->command == rtems_build_name('I', 'S', 'R', ' ')) { |
---|
| 187 | Clock_isr(0); /* vector number ignored */ |
---|
| 188 | } |
---|
| 189 | else if (args->command == rtems_build_name('N', 'E', 'W', ' ')) { |
---|
| 190 | ReInstall_clock(args->buffer); |
---|
| 191 | } |
---|
| 192 | |
---|
| 193 | done: |
---|
| 194 | return RTEMS_SUCCESSFUL; |
---|
| 195 | } |
---|
| 196 | |
---|