[ee733965] | 1 | /* clock.c |
---|
| 2 | * |
---|
| 3 | * This routine initializes the PIT on the MPC860. |
---|
| 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 | * |
---|
[08311cc3] | 28 | * COPYRIGHT (c) 1989-1999. |
---|
[ee733965] | 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 |
---|
| 33 | * http://www.OARcorp.com/rtems/license.html. |
---|
| 34 | * |
---|
| 35 | * $Id$ |
---|
| 36 | */ |
---|
| 37 | |
---|
[f817b02] | 38 | #include <rtems.h> |
---|
[ee733965] | 39 | #include <clockdrv.h> |
---|
| 40 | #include <rtems/libio.h> |
---|
| 41 | |
---|
| 42 | #include <stdlib.h> /* for atexit() */ |
---|
| 43 | #include <mpc860.h> |
---|
| 44 | |
---|
| 45 | volatile rtems_unsigned32 Clock_driver_ticks; |
---|
| 46 | extern volatile m860_t m860; |
---|
| 47 | |
---|
| 48 | void Clock_exit( void ); |
---|
| 49 | |
---|
| 50 | /* |
---|
| 51 | * These are set by clock driver during its init |
---|
| 52 | */ |
---|
| 53 | |
---|
| 54 | rtems_device_major_number rtems_clock_major = ~0; |
---|
| 55 | rtems_device_minor_number rtems_clock_minor; |
---|
| 56 | |
---|
| 57 | /* |
---|
| 58 | * ISR Handler |
---|
| 59 | */ |
---|
| 60 | rtems_isr Clock_isr(rtems_vector_number vector) |
---|
| 61 | { |
---|
| 62 | m860.piscr |= M860_PISCR_PS; |
---|
| 63 | Clock_driver_ticks++; |
---|
| 64 | rtems_clock_tick(); |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | void Install_clock(rtems_isr_entry clock_isr) |
---|
| 68 | { |
---|
| 69 | rtems_isr_entry previous_isr; |
---|
| 70 | rtems_unsigned32 pit_value; |
---|
| 71 | |
---|
| 72 | Clock_driver_ticks = 0; |
---|
| 73 | |
---|
[f817b02] | 74 | pit_value = rtems_configuration_get_microseconds_per_tick() / |
---|
[458bd34] | 75 | rtems_cpu_configuration_get_clicks_per_usec(); |
---|
[ee733965] | 76 | if (pit_value == 0) { |
---|
| 77 | pit_value = 0xffff; |
---|
| 78 | } else { |
---|
| 79 | pit_value--; |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | if (pit_value > 0xffff) { /* pit is only 16 bits long */ |
---|
| 83 | rtems_fatal_error_occurred(-1); |
---|
| 84 | } |
---|
| 85 | |
---|
[0dd1d44] | 86 | /* |
---|
| 87 | * initialize the interval here |
---|
| 88 | * First tick is set to right amount of time in the future |
---|
| 89 | * Future ticks will be incremented over last value set |
---|
| 90 | * in order to provide consistent clicks in the face of |
---|
| 91 | * interrupt overhead |
---|
| 92 | */ |
---|
| 93 | |
---|
| 94 | rtems_interrupt_catch(clock_isr, PPC_IRQ_LVL0, &previous_isr); |
---|
[ee733965] | 95 | |
---|
[0dd1d44] | 96 | m860.sccr &= ~(1<<24); |
---|
| 97 | m860.pitc = pit_value; |
---|
[ee733965] | 98 | |
---|
[0dd1d44] | 99 | /* set PIT irq level, enable PIT, PIT interrupts */ |
---|
| 100 | /* and clear int. status */ |
---|
| 101 | m860.piscr = M860_PISCR_PIRQ(0) | |
---|
| 102 | M860_PISCR_PTE | M860_PISCR_PS | M860_PISCR_PIE; |
---|
[ee733965] | 103 | |
---|
[0dd1d44] | 104 | m860.simask |= M860_SIMASK_LVM0; |
---|
[ee733965] | 105 | atexit(Clock_exit); |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | void |
---|
| 109 | ReInstall_clock(rtems_isr_entry new_clock_isr) |
---|
| 110 | { |
---|
| 111 | rtems_isr_entry previous_isr; |
---|
| 112 | rtems_unsigned32 isrlevel = 0; |
---|
| 113 | |
---|
| 114 | rtems_interrupt_disable(isrlevel); |
---|
| 115 | |
---|
| 116 | rtems_interrupt_catch(new_clock_isr, PPC_IRQ_LVL0, &previous_isr); |
---|
| 117 | |
---|
| 118 | rtems_interrupt_enable(isrlevel); |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | |
---|
| 122 | /* |
---|
| 123 | * Called via atexit() |
---|
| 124 | * Remove the clock interrupt handler by setting handler to NULL |
---|
| 125 | */ |
---|
| 126 | void |
---|
| 127 | Clock_exit(void) |
---|
| 128 | { |
---|
[0dd1d44] | 129 | /* disable PIT and PIT interrupts */ |
---|
| 130 | m860.piscr &= ~(M860_PISCR_PTE | M860_PISCR_PIE); |
---|
[ee733965] | 131 | |
---|
[0dd1d44] | 132 | (void) set_vector(0, PPC_IRQ_LVL0, 1); |
---|
[ee733965] | 133 | } |
---|
| 134 | |
---|
| 135 | rtems_device_driver Clock_initialize( |
---|
| 136 | rtems_device_major_number major, |
---|
| 137 | rtems_device_minor_number minor, |
---|
| 138 | void *pargp |
---|
| 139 | ) |
---|
| 140 | { |
---|
| 141 | Install_clock( Clock_isr ); |
---|
| 142 | |
---|
| 143 | /* |
---|
| 144 | * make major/minor avail to others such as shared memory driver |
---|
| 145 | */ |
---|
| 146 | |
---|
| 147 | rtems_clock_major = major; |
---|
| 148 | rtems_clock_minor = minor; |
---|
| 149 | |
---|
| 150 | return RTEMS_SUCCESSFUL; |
---|
| 151 | } |
---|
| 152 | |
---|
| 153 | rtems_device_driver Clock_control( |
---|
| 154 | rtems_device_major_number major, |
---|
| 155 | rtems_device_minor_number minor, |
---|
| 156 | void *pargp |
---|
| 157 | ) |
---|
| 158 | { |
---|
| 159 | rtems_libio_ioctl_args_t *args = pargp; |
---|
| 160 | |
---|
| 161 | if (args == 0) |
---|
| 162 | goto done; |
---|
| 163 | |
---|
| 164 | /* |
---|
| 165 | * This is hokey, but until we get a defined interface |
---|
| 166 | * to do this, it will just be this simple... |
---|
| 167 | */ |
---|
| 168 | |
---|
| 169 | if (args->command == rtems_build_name('I', 'S', 'R', ' ')) { |
---|
| 170 | Clock_isr(PPC_IRQ_LVL0); |
---|
| 171 | } |
---|
| 172 | else if (args->command == rtems_build_name('N', 'E', 'W', ' ')) { |
---|
| 173 | ReInstall_clock(args->buffer); |
---|
| 174 | } |
---|
| 175 | |
---|
| 176 | done: |
---|
| 177 | return RTEMS_SUCCESSFUL; |
---|
| 178 | } |
---|
| 179 | |
---|