[b7a6d23a] | 1 | /* Driver for discovery timers and watchdog */ |
---|
| 2 | |
---|
| 3 | /* |
---|
| 4 | * Acknowledgements: |
---|
| 5 | * Valuable information was obtained from the following drivers |
---|
| 6 | * netbsd: (C) Allegro Networks Inc; Wasabi Systems Inc. |
---|
| 7 | * linux: (C) MontaVista, Software, Inc; Mark A. Greer. |
---|
| 8 | * rtems: (C) Brookhaven National Laboratory; K. Feng |
---|
| 9 | * but this implementation is original work by the author. |
---|
| 10 | */ |
---|
| 11 | |
---|
| 12 | /* |
---|
| 13 | * Authorship |
---|
| 14 | * ---------- |
---|
| 15 | * This software ('beatnik' RTEMS BSP for MVME6100 and MVME5500) was |
---|
| 16 | * created by Till Straumann <strauman@slac.stanford.edu>, 2005-2007, |
---|
[2cb449d] | 17 | * Stanford Linear Accelerator Center, Stanford University. |
---|
[b7a6d23a] | 18 | * |
---|
| 19 | * Acknowledgement of sponsorship |
---|
| 20 | * ------------------------------ |
---|
| 21 | * The 'beatnik' BSP was produced by |
---|
| 22 | * the Stanford Linear Accelerator Center, Stanford University, |
---|
[2cb449d] | 23 | * under Contract DE-AC03-76SFO0515 with the Department of Energy. |
---|
[b7a6d23a] | 24 | * |
---|
| 25 | * Government disclaimer of liability |
---|
| 26 | * ---------------------------------- |
---|
| 27 | * Neither the United States nor the United States Department of Energy, |
---|
| 28 | * nor any of their employees, makes any warranty, express or implied, or |
---|
| 29 | * assumes any legal liability or responsibility for the accuracy, |
---|
| 30 | * completeness, or usefulness of any data, apparatus, product, or process |
---|
| 31 | * disclosed, or represents that its use would not infringe privately owned |
---|
| 32 | * rights. |
---|
| 33 | * |
---|
| 34 | * Stanford disclaimer of liability |
---|
| 35 | * -------------------------------- |
---|
| 36 | * Stanford University makes no representations or warranties, express or |
---|
| 37 | * implied, nor assumes any liability for the use of this software. |
---|
| 38 | * |
---|
| 39 | * Stanford disclaimer of copyright |
---|
| 40 | * -------------------------------- |
---|
| 41 | * Stanford University, owner of the copyright, hereby disclaims its |
---|
| 42 | * copyright and all other rights in this software. Hence, anyone may |
---|
| 43 | * freely use it for any purpose without restriction. |
---|
| 44 | * |
---|
| 45 | * Maintenance of notices |
---|
| 46 | * ---------------------- |
---|
| 47 | * In the interest of clarity regarding the origin and status of this |
---|
| 48 | * SLAC software, this and all the preceding Stanford University notices |
---|
| 49 | * are to remain affixed to any copy or derivative of this software made |
---|
| 50 | * or distributed by the recipient and are to be affixed to any copy of |
---|
| 51 | * software made or distributed by the recipient that contains a copy or |
---|
| 52 | * derivative of this software. |
---|
| 53 | * |
---|
| 54 | * ------------------ SLAC Software Notices, Set 4 OTT.002a, 2004 FEB 03 |
---|
| 55 | */ |
---|
[2cb449d] | 56 | |
---|
[b7a6d23a] | 57 | #include <rtems.h> |
---|
| 58 | #include <bsp/gtreg.h> |
---|
| 59 | #include <libcpu/io.h> |
---|
| 60 | #include <bsp.h> |
---|
| 61 | #include <bsp/irq.h> |
---|
| 62 | #include <rtems/bspIo.h> |
---|
| 63 | |
---|
| 64 | #include <stdint.h> |
---|
| 65 | |
---|
[3964329] | 66 | #include <bsp/gt_timer.h> |
---|
[b7a6d23a] | 67 | |
---|
| 68 | #define DEBUG |
---|
| 69 | |
---|
| 70 | static inline uint32_t gt_rd(uint32_t off) |
---|
| 71 | { |
---|
[2d5c486] | 72 | return in_le32( (volatile uint32_t *)(BSP_MV64x60_BASE+off) ); |
---|
[b7a6d23a] | 73 | } |
---|
| 74 | |
---|
| 75 | static inline void gt_wr(uint32_t off, uint32_t val) |
---|
| 76 | { |
---|
[2d5c486] | 77 | out_le32( (volatile uint32_t *)(BSP_MV64x60_BASE+off), val); |
---|
[b7a6d23a] | 78 | } |
---|
| 79 | |
---|
| 80 | static inline uint32_t gt_timer_bitmod(uint32_t off, uint32_t clr, uint32_t set) |
---|
| 81 | { |
---|
[2cb449d] | 82 | unsigned flags; |
---|
| 83 | uint32_t rval; |
---|
| 84 | |
---|
| 85 | rtems_interrupt_disable(flags); |
---|
| 86 | rval = gt_rd( off ); |
---|
| 87 | gt_wr( off, (rval & ~clr) | set ); |
---|
| 88 | rtems_interrupt_enable(flags); |
---|
| 89 | return rval; |
---|
[b7a6d23a] | 90 | } |
---|
| 91 | |
---|
[2cb449d] | 92 | #define GT_TIMER_MAX 3 |
---|
[e8606d5b] | 93 | #define TIMER_ARGCHECK(t) do { if ((t)>GT_TIMER_MAX) return -1; } while (0) |
---|
[b7a6d23a] | 94 | |
---|
| 95 | static struct { |
---|
[2cb449d] | 96 | void (*isr)(void *); |
---|
| 97 | void *arg; |
---|
[b7a6d23a] | 98 | } gt_timer_isrs[GT_TIMER_MAX+1] = {{0},}; |
---|
| 99 | |
---|
| 100 | uint32_t BSP_timer_read(uint32_t timer) |
---|
| 101 | { |
---|
[2cb449d] | 102 | TIMER_ARGCHECK(timer); |
---|
| 103 | return gt_rd(GT_TIMER_0 + (timer<<2)); |
---|
[b7a6d23a] | 104 | } |
---|
| 105 | |
---|
| 106 | int |
---|
| 107 | BSP_timer_start(uint32_t timer, uint32_t period) |
---|
| 108 | { |
---|
[2cb449d] | 109 | TIMER_ARGCHECK(timer); |
---|
| 110 | gt_wr(GT_TIMER_0 + (timer<<2), period); |
---|
| 111 | return 0; |
---|
[b7a6d23a] | 112 | } |
---|
| 113 | |
---|
| 114 | int |
---|
| 115 | BSP_timer_stop(uint32_t timer) |
---|
| 116 | { |
---|
[2cb449d] | 117 | TIMER_ARGCHECK(timer); |
---|
| 118 | /* disable, clear period, re-enable */ |
---|
| 119 | gt_timer_bitmod(GT_TIMER_0_3_Ctl, GT_TIMER_0_Ctl_Enb << (timer<<3), 0); |
---|
| 120 | gt_wr(GT_TIMER_0 + (timer<<2), 0); |
---|
| 121 | gt_timer_bitmod(GT_TIMER_0_3_Ctl, 0, GT_TIMER_0_Ctl_Enb << (timer<<3)); |
---|
| 122 | return 0; |
---|
[b7a6d23a] | 123 | } |
---|
| 124 | |
---|
| 125 | int |
---|
| 126 | BSP_timer_setup(uint32_t timer, void (*isr)(void *arg), void *arg, int reload) |
---|
| 127 | { |
---|
[2cb449d] | 128 | TIMER_ARGCHECK(timer); |
---|
| 129 | if ( isr && gt_timer_isrs[timer].isr ) |
---|
| 130 | return -1; |
---|
| 131 | |
---|
| 132 | BSP_timer_stop(timer); |
---|
| 133 | /* mask and clear */ |
---|
| 134 | gt_timer_bitmod(GT_TIMER_0_3_Intr_Msk, GT_TIMER_0_Intr<<timer, 0); |
---|
| 135 | gt_timer_bitmod(GT_TIMER_0_3_Intr_Cse, GT_TIMER_0_Intr<<timer, 0); |
---|
| 136 | |
---|
| 137 | /* set reload bit */ |
---|
| 138 | if ( reload ) |
---|
| 139 | gt_timer_bitmod(GT_TIMER_0_3_Ctl, 0, GT_TIMER_0_Ctl_Rld << (timer<<3)); |
---|
| 140 | else |
---|
| 141 | gt_timer_bitmod(GT_TIMER_0_3_Ctl, GT_TIMER_0_Ctl_Rld << (timer<<3), 0); |
---|
| 142 | |
---|
| 143 | asm volatile("":::"memory"); |
---|
| 144 | |
---|
| 145 | if ( isr ) { |
---|
| 146 | gt_timer_isrs[timer].isr = isr; |
---|
| 147 | gt_timer_isrs[timer].arg = arg; |
---|
| 148 | asm volatile("":::"memory"); |
---|
| 149 | gt_timer_bitmod(GT_TIMER_0_3_Intr_Msk, 0, GT_TIMER_0_Intr<<timer); |
---|
| 150 | } else { |
---|
| 151 | gt_timer_isrs[timer].isr = 0; |
---|
| 152 | gt_timer_isrs[timer].arg = 0; |
---|
| 153 | } |
---|
| 154 | return 0; |
---|
[b7a6d23a] | 155 | } |
---|
| 156 | |
---|
| 157 | static void |
---|
| 158 | gt_timer_hdl(rtems_irq_hdl_param arg) |
---|
| 159 | { |
---|
[2cb449d] | 160 | int iarg = (int)arg; |
---|
| 161 | int timer; |
---|
| 162 | uint32_t bit; |
---|
| 163 | |
---|
| 164 | for ( ; iarg; iarg >>= 4 ) { |
---|
| 165 | timer = (iarg & 0xf)-1; |
---|
| 166 | bit = GT_TIMER_0_Intr<<timer; |
---|
| 167 | if ( gt_timer_bitmod(GT_TIMER_0_3_Intr_Cse, bit, 0) & bit ) { |
---|
| 168 | /* cause was set */ |
---|
| 169 | if ( ! gt_timer_isrs[timer].isr ) { |
---|
| 170 | printk("gt_timer: warning; no ISR connected but and IRQ happened (timer # %i)\n", timer); |
---|
| 171 | /* mask */ |
---|
| 172 | gt_timer_bitmod(GT_TIMER_0_3_Intr_Msk, bit, 0); |
---|
| 173 | } else { |
---|
| 174 | gt_timer_isrs[timer].isr(gt_timer_isrs[timer].arg); |
---|
| 175 | } |
---|
| 176 | } |
---|
| 177 | } |
---|
[b7a6d23a] | 178 | } |
---|
| 179 | |
---|
| 180 | int |
---|
| 181 | BSP_timers_initialize(void) |
---|
| 182 | { |
---|
[2cb449d] | 183 | rtems_irq_connect_data xx = {0}; |
---|
| 184 | int i, ainc, arg; |
---|
| 185 | |
---|
| 186 | xx.hdl = gt_timer_hdl; |
---|
| 187 | xx.on = 0; |
---|
| 188 | xx.off = 0; |
---|
| 189 | xx.isOn = 0; |
---|
| 190 | |
---|
| 191 | switch (BSP_getDiscoveryVersion(0)) { |
---|
| 192 | case MV_64360: |
---|
| 193 | i = 3; |
---|
| 194 | ainc = 1; |
---|
| 195 | arg = 4; |
---|
| 196 | break; |
---|
| 197 | default: |
---|
| 198 | i = 1; |
---|
| 199 | ainc = 0x0202; |
---|
| 200 | arg = 0x0403; |
---|
| 201 | break; |
---|
| 202 | } |
---|
| 203 | |
---|
| 204 | for ( ; i>=0; i--, arg-=ainc ) { |
---|
| 205 | xx.name = BSP_IRQ_TIME0_1 + i; |
---|
| 206 | xx.handle = (rtems_irq_hdl_param)arg; |
---|
| 207 | if ( !BSP_install_rtems_irq_handler(&xx) ) |
---|
| 208 | return -1; |
---|
| 209 | } |
---|
| 210 | |
---|
| 211 | return 0; |
---|
[b7a6d23a] | 212 | } |
---|
| 213 | |
---|
[2cb449d] | 214 | #ifdef DEBUG_MODULAR |
---|
| 215 | static int |
---|
[b7a6d23a] | 216 | BSP_timers_uninstall(void) |
---|
| 217 | { |
---|
[2cb449d] | 218 | rtems_irq_connect_data xx = {0}; |
---|
| 219 | int i; |
---|
| 220 | |
---|
| 221 | xx.hdl = gt_timer_hdl; |
---|
| 222 | xx.on = 0; |
---|
| 223 | xx.off = 0; |
---|
| 224 | xx.isOn = 0; |
---|
| 225 | |
---|
| 226 | for ( i=0; i<= GT_TIMER_MAX; i++ ) { |
---|
| 227 | if ( BSP_timer_setup(i, 0, 0, 0) ) |
---|
| 228 | return -1; |
---|
| 229 | } |
---|
| 230 | |
---|
| 231 | switch (BSP_getDiscoveryVersion(0)) { |
---|
| 232 | case MV_64360: |
---|
| 233 | i = 3; |
---|
| 234 | break; |
---|
| 235 | default: |
---|
| 236 | i = 1; |
---|
| 237 | break; |
---|
| 238 | } |
---|
| 239 | |
---|
| 240 | for ( ; i >= 0; i-- ) { |
---|
| 241 | xx.name = BSP_IRQ_TIME0_1 + i; |
---|
| 242 | BSP_get_current_rtems_irq_handler(&xx); |
---|
| 243 | if ( !BSP_remove_rtems_irq_handler(&xx) ) |
---|
| 244 | return -1; |
---|
| 245 | } |
---|
| 246 | |
---|
| 247 | return 0; |
---|
[b7a6d23a] | 248 | } |
---|
[2cb449d] | 249 | #endif |
---|
[b7a6d23a] | 250 | |
---|
| 251 | uint32_t |
---|
| 252 | BSP_timer_clock_get(uint32_t timer) |
---|
| 253 | { |
---|
[2cb449d] | 254 | return BSP_bus_frequency; |
---|
[b7a6d23a] | 255 | } |
---|
| 256 | |
---|
| 257 | int BSP_timer_instances(void) |
---|
| 258 | { |
---|
[2cb449d] | 259 | return GT_TIMER_MAX + 1; |
---|
[b7a6d23a] | 260 | } |
---|
| 261 | |
---|
| 262 | /* On a 64260A we can't read the status (on/off), apparently |
---|
| 263 | * so we maintain it locally and assume the firmware has |
---|
| 264 | * not enabled the dog initially... |
---|
| 265 | */ |
---|
| 266 | static uint32_t wdog_on = 0x00ffffff; |
---|
| 267 | |
---|
| 268 | static uint32_t rd_wdcnf(void) |
---|
| 269 | { |
---|
[2cb449d] | 270 | uint32_t cnf = gt_rd(GT_WDOG_Config); |
---|
| 271 | |
---|
| 272 | /* BSD driver says that on the 64260A we always |
---|
| 273 | * read 0xffffffff so we have to maintain the |
---|
| 274 | * status locally (and hope we get the initial |
---|
| 275 | * value right). |
---|
| 276 | */ |
---|
| 277 | if ( ~0 == cnf ) |
---|
| 278 | cnf = wdog_on; |
---|
| 279 | return cnf; |
---|
[b7a6d23a] | 280 | } |
---|
| 281 | |
---|
| 282 | /* change on/off state assume caller has IRQs disabled */ |
---|
| 283 | static void dog_toggle(uint32_t ctl) |
---|
| 284 | { |
---|
[2cb449d] | 285 | ctl &= ~( GT_WDOG_Config_Ctl1a | GT_WDOG_Config_Ctl1b \ |
---|
| 286 | | GT_WDOG_Config_Ctl2a | GT_WDOG_Config_Ctl2b); |
---|
| 287 | gt_wr(GT_WDOG_Config, ctl | GT_WDOG_Config_Ctl1a); |
---|
| 288 | gt_wr(GT_WDOG_Config, ctl | GT_WDOG_Config_Ctl1b); |
---|
[b7a6d23a] | 289 | } |
---|
| 290 | |
---|
| 291 | static void dog_pet(uint32_t ctl) |
---|
| 292 | { |
---|
[2cb449d] | 293 | ctl &= ~( GT_WDOG_Config_Ctl1a | GT_WDOG_Config_Ctl1b \ |
---|
| 294 | | GT_WDOG_Config_Ctl2a | GT_WDOG_Config_Ctl2b); |
---|
| 295 | gt_wr(GT_WDOG_Config, ctl | GT_WDOG_Config_Ctl2a); |
---|
| 296 | gt_wr(GT_WDOG_Config, ctl | GT_WDOG_Config_Ctl2b); |
---|
[b7a6d23a] | 297 | } |
---|
| 298 | |
---|
| 299 | |
---|
| 300 | /* Enable watchdog and set a timeout (in us) |
---|
| 301 | * a timeout of 0xffffffff selects the old/existing |
---|
| 302 | * timeout. |
---|
| 303 | * |
---|
| 304 | * RETURNS 0 on success |
---|
| 305 | */ |
---|
| 306 | int |
---|
| 307 | BSP_watchdog_enable(uint32_t timeout_us) |
---|
| 308 | { |
---|
[2cb449d] | 309 | unsigned long long x = timeout_us; |
---|
| 310 | unsigned flags; |
---|
| 311 | uint32_t ctl; |
---|
[b7a6d23a] | 312 | |
---|
[2cb449d] | 313 | x *= BSP_bus_frequency; |
---|
| 314 | x /= 256; /* there seems to be a prescaler */ |
---|
[b7a6d23a] | 315 | x /= 1000000; /* us/s */ |
---|
| 316 | |
---|
[2cb449d] | 317 | if ( x > (1<<24)-1 ) |
---|
| 318 | x = (1<<24)-1; |
---|
[b7a6d23a] | 319 | |
---|
[2cb449d] | 320 | if ( 0xffffffff != timeout_us ) |
---|
| 321 | timeout_us = x; |
---|
[b7a6d23a] | 322 | |
---|
[2cb449d] | 323 | rtems_interrupt_disable(flags); |
---|
[b7a6d23a] | 324 | |
---|
[2cb449d] | 325 | ctl = rd_wdcnf(); |
---|
[b7a6d23a] | 326 | |
---|
[2cb449d] | 327 | /* if enabled, disable first */ |
---|
| 328 | if ( GT_WDOG_Config_Enb & ctl ) { |
---|
| 329 | dog_toggle(ctl); |
---|
| 330 | } |
---|
| 331 | if ( 0xffffffff == timeout_us ) { |
---|
| 332 | timeout_us = ctl & ((1<<24)-1); |
---|
| 333 | dog_toggle(ctl); |
---|
| 334 | dog_pet(ctl); |
---|
| 335 | } else { |
---|
| 336 | gt_wr(GT_WDOG_Config, timeout_us | GT_WDOG_Config_Ctl1a); |
---|
| 337 | gt_wr(GT_WDOG_Config, timeout_us | GT_WDOG_Config_Ctl1b); |
---|
| 338 | } |
---|
[b7a6d23a] | 339 | |
---|
[2cb449d] | 340 | wdog_on = GT_WDOG_Config_Enb | timeout_us; |
---|
[b7a6d23a] | 341 | |
---|
[2cb449d] | 342 | rtems_interrupt_enable(flags); |
---|
| 343 | return 0; |
---|
[b7a6d23a] | 344 | } |
---|
| 345 | |
---|
| 346 | /* Disable watchdog |
---|
| 347 | * RETURNS 0 on success |
---|
| 348 | */ |
---|
| 349 | int BSP_watchdog_disable(void) |
---|
| 350 | { |
---|
| 351 | unsigned long flags; |
---|
| 352 | uint32_t ctl; |
---|
| 353 | |
---|
[2cb449d] | 354 | rtems_interrupt_disable(flags); |
---|
[b7a6d23a] | 355 | |
---|
[2cb449d] | 356 | ctl = rd_wdcnf(); |
---|
[b7a6d23a] | 357 | |
---|
[2cb449d] | 358 | if ( (GT_WDOG_Config_Enb & ctl) ) { |
---|
| 359 | dog_toggle(ctl); |
---|
| 360 | wdog_on = ctl & ~(GT_WDOG_Config_Enb); |
---|
| 361 | } |
---|
[b7a6d23a] | 362 | |
---|
[2cb449d] | 363 | rtems_interrupt_enable(flags); |
---|
| 364 | return 0; |
---|
[b7a6d23a] | 365 | } |
---|
| 366 | |
---|
| 367 | /* Check status -- unfortunately there seems to be no way |
---|
| 368 | * to read the running value... |
---|
| 369 | * |
---|
| 370 | * RETURNS nonzero if enabled/running, zero if disabled/stopped |
---|
| 371 | */ |
---|
| 372 | int BSP_watchdog_status(void) |
---|
| 373 | { |
---|
[2cb449d] | 374 | uint32_t ctl = rd_wdcnf(); |
---|
[b7a6d23a] | 375 | |
---|
[2cb449d] | 376 | /* report also the current period */ |
---|
| 377 | return GT_WDOG_Config_Enb & ctl ? ctl : 0; |
---|
[b7a6d23a] | 378 | } |
---|
| 379 | |
---|
| 380 | /* Pet the watchdog (rearm to configured timeout) |
---|
| 381 | * RETURNS: 0 on success, nonzero on failure (watchdog |
---|
| 382 | * currently not running). |
---|
| 383 | */ |
---|
| 384 | int BSP_watchdog_pet(void) |
---|
| 385 | { |
---|
[2cb449d] | 386 | unsigned long flags; |
---|
| 387 | |
---|
| 388 | if ( !wdog_on ) |
---|
| 389 | return -1; |
---|
| 390 | rtems_interrupt_disable(flags); |
---|
| 391 | dog_pet(rd_wdcnf()); |
---|
| 392 | rtems_interrupt_enable(flags); |
---|
| 393 | return 0; |
---|
[b7a6d23a] | 394 | } |
---|
| 395 | |
---|
| 396 | |
---|
| 397 | #ifdef DEBUG_MODULAR |
---|
| 398 | int |
---|
| 399 | _cexpModuleFinalize(void *unused) |
---|
| 400 | { |
---|
[2cb449d] | 401 | BSP_watchdog_disable(); |
---|
| 402 | return BSP_timers_uninstall(); |
---|
[b7a6d23a] | 403 | } |
---|
| 404 | |
---|
| 405 | void |
---|
| 406 | _cexpModuleInitialize(void *unused) |
---|
| 407 | { |
---|
[2cb449d] | 408 | BSP_timers_initialize(); |
---|
[b7a6d23a] | 409 | } |
---|
| 410 | #endif |
---|