time.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 "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 int nuc900_clockevent_shutdown(struct clock_event_device *evt)
  43. {
  44. unsigned int val = __raw_readl(REG_TCSR0) & ~(0x03 << 27);
  45. __raw_writel(val, REG_TCSR0);
  46. return 0;
  47. }
  48. static int nuc900_clockevent_set_oneshot(struct clock_event_device *evt)
  49. {
  50. unsigned int val = __raw_readl(REG_TCSR0) & ~(0x03 << 27);
  51. val |= (ONESHOT | COUNTEN | INTEN | PRESCALE);
  52. __raw_writel(val, REG_TCSR0);
  53. return 0;
  54. }
  55. static int nuc900_clockevent_set_periodic(struct clock_event_device *evt)
  56. {
  57. unsigned int val = __raw_readl(REG_TCSR0) & ~(0x03 << 27);
  58. __raw_writel(timer0_load, REG_TICR0);
  59. val |= (PERIOD | COUNTEN | INTEN | PRESCALE);
  60. __raw_writel(val, REG_TCSR0);
  61. return 0;
  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. .features = CLOCK_EVT_FEAT_PERIODIC |
  76. CLOCK_EVT_FEAT_ONESHOT,
  77. .set_state_shutdown = nuc900_clockevent_shutdown,
  78. .set_state_periodic = nuc900_clockevent_set_periodic,
  79. .set_state_oneshot = nuc900_clockevent_set_oneshot,
  80. .tick_resume = nuc900_clockevent_shutdown,
  81. .set_next_event = nuc900_clockevent_setnextevent,
  82. .rating = 300,
  83. };
  84. /*IRQ handler for the timer*/
  85. static irqreturn_t nuc900_timer0_interrupt(int irq, void *dev_id)
  86. {
  87. struct clock_event_device *evt = &nuc900_clockevent_device;
  88. __raw_writel(0x01, REG_TISR); /* clear TIF0 */
  89. evt->event_handler(evt);
  90. return IRQ_HANDLED;
  91. }
  92. static struct irqaction nuc900_timer0_irq = {
  93. .name = "nuc900-timer0",
  94. .flags = IRQF_TIMER | IRQF_IRQPOLL,
  95. .handler = nuc900_timer0_interrupt,
  96. };
  97. static void __init nuc900_clockevents_init(void)
  98. {
  99. unsigned int rate;
  100. struct clk *clk = clk_get(NULL, "timer0");
  101. BUG_ON(IS_ERR(clk));
  102. __raw_writel(0x00, REG_TCSR0);
  103. clk_enable(clk);
  104. rate = clk_get_rate(clk) / (PRESCALE + 1);
  105. timer0_load = (rate / TICKS_PER_SEC);
  106. __raw_writel(RESETINT, REG_TISR);
  107. setup_irq(IRQ_TIMER0, &nuc900_timer0_irq);
  108. nuc900_clockevent_device.cpumask = cpumask_of(0);
  109. clockevents_config_and_register(&nuc900_clockevent_device, rate,
  110. 0xf, 0xffffffff);
  111. }
  112. static void __init nuc900_clocksource_init(void)
  113. {
  114. unsigned int val;
  115. unsigned int rate;
  116. struct clk *clk = clk_get(NULL, "timer1");
  117. BUG_ON(IS_ERR(clk));
  118. __raw_writel(0x00, REG_TCSR1);
  119. clk_enable(clk);
  120. rate = clk_get_rate(clk) / (PRESCALE + 1);
  121. __raw_writel(0xffffffff, REG_TICR1);
  122. val = __raw_readl(REG_TCSR1);
  123. val |= (COUNTEN | PERIOD | PRESCALE);
  124. __raw_writel(val, REG_TCSR1);
  125. clocksource_mmio_init(REG_TDR1, "nuc900-timer1", rate, 200,
  126. TDR_SHIFT, clocksource_mmio_readl_down);
  127. }
  128. void __init nuc900_timer_init(void)
  129. {
  130. nuc900_clocksource_init();
  131. nuc900_clockevents_init();
  132. }