time.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * linux/arch/arm/mach-w90x900/time.c
  3. *
  4. * Based on linux/arch/arm/plat-s3c24xx/time.c by Ben Dooks
  5. *
  6. * Copyright (c) 2009 Nuvoton technology corporation
  7. * All rights reserved.
  8. *
  9. * Wan ZongShun <mcuos.com@gmail.com>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. */
  17. #include <linux/kernel.h>
  18. #include <linux/sched.h>
  19. #include <linux/init.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/err.h>
  22. #include <linux/clk.h>
  23. #include <linux/io.h>
  24. #include <linux/leds.h>
  25. #include <linux/clocksource.h>
  26. #include <linux/clockchips.h>
  27. #include <asm/mach-types.h>
  28. #include <asm/mach/irq.h>
  29. #include <asm/mach/time.h>
  30. #include <mach/map.h>
  31. #include <mach/regs-timer.h>
  32. #include "nuc9xx.h"
  33. #define RESETINT 0x1f
  34. #define PERIOD (0x01 << 27)
  35. #define ONESHOT (0x00 << 27)
  36. #define COUNTEN (0x01 << 30)
  37. #define INTEN (0x01 << 29)
  38. #define TICKS_PER_SEC 100
  39. #define PRESCALE 0x63 /* Divider = prescale + 1 */
  40. #define TDR_SHIFT 24
  41. static unsigned int timer0_load;
  42. static void nuc900_clockevent_setmode(enum clock_event_mode mode,
  43. struct clock_event_device *clk)
  44. {
  45. unsigned int val;
  46. val = __raw_readl(REG_TCSR0);
  47. val &= ~(0x03 << 27);
  48. switch (mode) {
  49. case CLOCK_EVT_MODE_PERIODIC:
  50. __raw_writel(timer0_load, REG_TICR0);
  51. val |= (PERIOD | COUNTEN | INTEN | PRESCALE);
  52. break;
  53. case CLOCK_EVT_MODE_ONESHOT:
  54. val |= (ONESHOT | COUNTEN | INTEN | PRESCALE);
  55. break;
  56. case CLOCK_EVT_MODE_UNUSED:
  57. case CLOCK_EVT_MODE_SHUTDOWN:
  58. case CLOCK_EVT_MODE_RESUME:
  59. break;
  60. }
  61. __raw_writel(val, REG_TCSR0);
  62. }
  63. static int nuc900_clockevent_setnextevent(unsigned long evt,
  64. struct clock_event_device *clk)
  65. {
  66. unsigned int val;
  67. __raw_writel(evt, REG_TICR0);
  68. val = __raw_readl(REG_TCSR0);
  69. val |= (COUNTEN | INTEN | PRESCALE);
  70. __raw_writel(val, REG_TCSR0);
  71. return 0;
  72. }
  73. static struct clock_event_device nuc900_clockevent_device = {
  74. .name = "nuc900-timer0",
  75. .shift = 32,
  76. .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
  77. .set_mode = nuc900_clockevent_setmode,
  78. .set_next_event = nuc900_clockevent_setnextevent,
  79. .rating = 300,
  80. };
  81. /*IRQ handler for the timer*/
  82. static irqreturn_t nuc900_timer0_interrupt(int irq, void *dev_id)
  83. {
  84. struct clock_event_device *evt = &nuc900_clockevent_device;
  85. __raw_writel(0x01, REG_TISR); /* clear TIF0 */
  86. evt->event_handler(evt);
  87. return IRQ_HANDLED;
  88. }
  89. static struct irqaction nuc900_timer0_irq = {
  90. .name = "nuc900-timer0",
  91. .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
  92. .handler = nuc900_timer0_interrupt,
  93. };
  94. static void __init nuc900_clockevents_init(void)
  95. {
  96. unsigned int rate;
  97. struct clk *clk = clk_get(NULL, "timer0");
  98. BUG_ON(IS_ERR(clk));
  99. __raw_writel(0x00, REG_TCSR0);
  100. clk_enable(clk);
  101. rate = clk_get_rate(clk) / (PRESCALE + 1);
  102. timer0_load = (rate / TICKS_PER_SEC);
  103. __raw_writel(RESETINT, REG_TISR);
  104. setup_irq(IRQ_TIMER0, &nuc900_timer0_irq);
  105. nuc900_clockevent_device.mult = div_sc(rate, NSEC_PER_SEC,
  106. nuc900_clockevent_device.shift);
  107. nuc900_clockevent_device.max_delta_ns = clockevent_delta2ns(0xffffffff,
  108. &nuc900_clockevent_device);
  109. nuc900_clockevent_device.min_delta_ns = clockevent_delta2ns(0xf,
  110. &nuc900_clockevent_device);
  111. nuc900_clockevent_device.cpumask = cpumask_of(0);
  112. clockevents_register_device(&nuc900_clockevent_device);
  113. }
  114. static void __init nuc900_clocksource_init(void)
  115. {
  116. unsigned int val;
  117. unsigned int rate;
  118. struct clk *clk = clk_get(NULL, "timer1");
  119. BUG_ON(IS_ERR(clk));
  120. __raw_writel(0x00, REG_TCSR1);
  121. clk_enable(clk);
  122. rate = clk_get_rate(clk) / (PRESCALE + 1);
  123. __raw_writel(0xffffffff, REG_TICR1);
  124. val = __raw_readl(REG_TCSR1);
  125. val |= (COUNTEN | PERIOD | PRESCALE);
  126. __raw_writel(val, REG_TCSR1);
  127. clocksource_mmio_init(REG_TDR1, "nuc900-timer1", rate, 200,
  128. TDR_SHIFT, clocksource_mmio_readl_down);
  129. }
  130. static void __init nuc900_timer_init(void)
  131. {
  132. nuc900_clocksource_init();
  133. nuc900_clockevents_init();
  134. }
  135. struct sys_timer nuc900_timer = {
  136. .init = nuc900_timer_init,
  137. };