[cd67dca] | 1 | /* |
---|
| 2 | * Clock Driver for SH4 simulator (timer interrupt not supported now). |
---|
| 3 | * |
---|
| 4 | * Copyright (C) 2001 OKTET Ltd., St.-Petersburg, Russia |
---|
| 5 | * Author: Victor V. Vengerov <vvv@oktet.ru> |
---|
| 6 | * |
---|
| 7 | * COPYRIGHT (c) 1989-2001. |
---|
| 8 | * On-Line Applications Research Corporation (OAR). |
---|
| 9 | * |
---|
| 10 | * The license and distribution terms for this file may be |
---|
| 11 | * found in the file LICENSE in this distribution or at |
---|
| 12 | * |
---|
[b5bc659] | 13 | * http://www.rtems.com/license/LICENSE. |
---|
[cd67dca] | 14 | * |
---|
| 15 | * $Id$ |
---|
| 16 | */ |
---|
| 17 | |
---|
| 18 | #include <stdlib.h> |
---|
| 19 | #include <bsp.h> |
---|
| 20 | #include <rtems/libio.h> |
---|
| 21 | |
---|
[fa920f1] | 22 | extern uint32_t bsp_clicks_per_second; |
---|
| 23 | |
---|
[cd67dca] | 24 | /* |
---|
| 25 | * Clock_driver_ticks is a monotonically increasing counter of the |
---|
| 26 | * number of clock ticks since the driver was initialized. |
---|
| 27 | */ |
---|
[c939d5e] | 28 | volatile uint32_t Clock_driver_ticks; |
---|
[cd67dca] | 29 | |
---|
| 30 | /* |
---|
| 31 | * These are set by clock driver during its init |
---|
| 32 | */ |
---|
[0fdc099] | 33 | |
---|
[cd67dca] | 34 | rtems_device_major_number rtems_clock_major = ~0; |
---|
| 35 | rtems_device_minor_number rtems_clock_minor; |
---|
| 36 | |
---|
| 37 | rtems_isr (*rtems_clock_hook)(rtems_vector_number) = NULL; |
---|
| 38 | |
---|
| 39 | static void |
---|
[c939d5e] | 40 | set_clock_period(uint32_t period) |
---|
[cd67dca] | 41 | { |
---|
| 42 | asm volatile ("\tmov %0,r0\n" |
---|
[0fdc099] | 43 | "\ttrapa\t#4\n" |
---|
| 44 | : |
---|
[cd67dca] | 45 | : "r" (period) |
---|
| 46 | : "r0" ); |
---|
| 47 | } |
---|
| 48 | |
---|
| 49 | /* Clock_isr -- |
---|
| 50 | * This handles the timer interrupt by clearing the timer's interrupt |
---|
| 51 | * flag and announcing the clock tick to the system. |
---|
| 52 | * |
---|
| 53 | * PARAMETERS: |
---|
| 54 | * vector - timer interrupt vector number |
---|
| 55 | * |
---|
| 56 | * RETURNS: |
---|
| 57 | * none |
---|
| 58 | */ |
---|
| 59 | rtems_isr |
---|
| 60 | Clock_isr (rtems_vector_number vector) |
---|
| 61 | { |
---|
| 62 | /* Announce the clock tick */ |
---|
| 63 | Clock_driver_ticks++; |
---|
| 64 | rtems_clock_tick(); |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | /* Clock_exit -- |
---|
| 68 | * This shuts down the timer if it was enabled and removes it |
---|
| 69 | * from the interrupt mask. |
---|
| 70 | * |
---|
| 71 | * PARAMETERS: |
---|
| 72 | * none |
---|
| 73 | * |
---|
| 74 | * RETURNS: |
---|
| 75 | * none |
---|
| 76 | */ |
---|
| 77 | void |
---|
| 78 | Clock_exit(void) |
---|
| 79 | { |
---|
| 80 | if (BSP_Configuration.ticks_per_timeslice) |
---|
| 81 | { |
---|
| 82 | /* disable all timer1 interrupts */ |
---|
| 83 | set_clock_period(0); |
---|
| 84 | } |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | /* Install_clock -- |
---|
| 88 | * This initialises timer1 with the BSP timeslice config value |
---|
| 89 | * as a reference and sets up the interrupt handler for clock ticks. |
---|
| 90 | * |
---|
| 91 | * PARAMETERS: |
---|
| 92 | * clock_isr - clock interrupt handler routine |
---|
| 93 | * |
---|
| 94 | * RETURNS: |
---|
| 95 | * none. |
---|
| 96 | */ |
---|
| 97 | static void |
---|
| 98 | Install_clock(rtems_isr_entry clock_isr) |
---|
| 99 | { |
---|
[fa920f1] | 100 | uint32_t period; |
---|
[0fdc099] | 101 | |
---|
[fa920f1] | 102 | Clock_driver_ticks = 0; |
---|
| 103 | if (BSP_Configuration.ticks_per_timeslice) { |
---|
| 104 | rtems_isr_entry old_isr; |
---|
| 105 | period = bsp_clicks_per_second / BSP_Configuration.ticks_per_timeslice; |
---|
[0fdc099] | 106 | |
---|
[fa920f1] | 107 | /* Configure timer interrupts */ |
---|
| 108 | set_clock_period(period); |
---|
[0fdc099] | 109 | |
---|
[fa920f1] | 110 | /* Register the interrupt handler */ |
---|
| 111 | rtems_interrupt_catch(clock_isr, CLOCK_VECTOR, &old_isr); |
---|
| 112 | |
---|
| 113 | /* Register the driver exit procedure so we can shutdown */ |
---|
| 114 | atexit(Clock_exit); |
---|
| 115 | } |
---|
[cd67dca] | 116 | } |
---|
| 117 | |
---|
| 118 | /* Clock_initialize -- |
---|
| 119 | * This is called to setup the clock driver. It calls the hardware |
---|
| 120 | * setup function and make the driver major/minor values available |
---|
| 121 | * for other. |
---|
| 122 | * |
---|
| 123 | * PARAMETERS: |
---|
| 124 | * major - clock device major number |
---|
| 125 | * minor - clock device minor number |
---|
| 126 | * pargp - device driver initialization argument (not used) |
---|
| 127 | * |
---|
| 128 | * RETURNS: |
---|
| 129 | * RTEMS status code |
---|
| 130 | */ |
---|
| 131 | rtems_device_driver |
---|
| 132 | Clock_initialize(rtems_device_major_number major, |
---|
| 133 | rtems_device_minor_number minor, |
---|
| 134 | void *pargp) |
---|
| 135 | { |
---|
| 136 | Install_clock (Clock_isr); |
---|
[0fdc099] | 137 | |
---|
[cd67dca] | 138 | /* Make major/minor avail to others such as shared memory driver */ |
---|
| 139 | rtems_clock_major = major; |
---|
| 140 | rtems_clock_minor = minor; |
---|
[0fdc099] | 141 | |
---|
[cd67dca] | 142 | return RTEMS_SUCCESSFUL; |
---|
| 143 | } |
---|
[0fdc099] | 144 | |
---|
[cd67dca] | 145 | /* Clock_control -- |
---|
| 146 | * I/O control (IOCTL) function for Clock driver. At this moment this |
---|
| 147 | * just runs the interrupt handler or re-registers the interrupt handler |
---|
| 148 | * on request. |
---|
| 149 | * |
---|
| 150 | * PARAMETERS: |
---|
| 151 | * major - clock major device number |
---|
| 152 | * minor - clock minor device number |
---|
| 153 | * pargp - pointer to IOCTL arguments |
---|
| 154 | * |
---|
| 155 | * RETURNS: |
---|
| 156 | * RTEMS status code |
---|
| 157 | */ |
---|
| 158 | rtems_device_driver |
---|
| 159 | Clock_control(rtems_device_major_number major, |
---|
| 160 | rtems_device_minor_number minor, |
---|
| 161 | void *pargp) |
---|
| 162 | { |
---|
[c939d5e] | 163 | uint32_t isrlevel; |
---|
[cd67dca] | 164 | rtems_libio_ioctl_args_t *args = pargp; |
---|
| 165 | |
---|
| 166 | if (args) |
---|
| 167 | { |
---|
[a1a84a3] | 168 | rtems_isr_entry old_isr; |
---|
[cd67dca] | 169 | /* |
---|
| 170 | * This is hokey, but until we get a defined interface |
---|
| 171 | * to do this, it will just be this simple... |
---|
| 172 | */ |
---|
[0fdc099] | 173 | if (args->command == rtems_build_name('I', 'S', 'R', ' ')) |
---|
[cd67dca] | 174 | { |
---|
| 175 | Clock_isr(CLOCK_VECTOR); |
---|
| 176 | } |
---|
| 177 | else if (args->command == rtems_build_name('N', 'E', 'W', ' ')) |
---|
| 178 | { |
---|
| 179 | rtems_interrupt_disable( isrlevel ); |
---|
| 180 | rtems_interrupt_catch(Clock_isr, CLOCK_VECTOR, &old_isr); |
---|
| 181 | rtems_interrupt_enable( isrlevel ); |
---|
| 182 | } |
---|
| 183 | } |
---|
| 184 | return RTEMS_SUCCESSFUL; |
---|
| 185 | } |
---|