time.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * Copyright (C) 2008 Manuel Lauss <mano@roarinelk.homelinux.net>
  3. *
  4. * Previous incarnations were:
  5. * Copyright (C) 2001, 2006, 2008 MontaVista Software, <source@mvista.com>
  6. * Copied and modified Carsten Langgaard's time.c
  7. *
  8. * Carsten Langgaard, carstenl@mips.com
  9. * Copyright (C) 1999,2000 MIPS Technologies, Inc. All rights reserved.
  10. *
  11. * ########################################################################
  12. *
  13. * This program is free software; you can distribute it and/or modify it
  14. * under the terms of the GNU General Public License (Version 2) as
  15. * published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope it will be useful, but WITHOUT
  18. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  19. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  20. * for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License along
  23. * with this program; if not, write to the Free Software Foundation, Inc.,
  24. * 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
  25. *
  26. * ########################################################################
  27. *
  28. * Clocksource/event using the 32.768kHz-clocked Counter1 ('RTC' in the
  29. * databooks). Firmware/Board init code must enable the counters in the
  30. * counter control register, otherwise the CP0 counter clocksource/event
  31. * will be installed instead (and use of 'wait' instruction is prohibited).
  32. */
  33. #include <linux/clockchips.h>
  34. #include <linux/clocksource.h>
  35. #include <linux/interrupt.h>
  36. #include <linux/spinlock.h>
  37. #include <asm/time.h>
  38. #include <asm/mach-au1x00/au1000.h>
  39. /* 32kHz clock enabled and detected */
  40. #define CNTR_OK (SYS_CNTRL_E0 | SYS_CNTRL_32S)
  41. extern int allow_au1k_wait; /* default off for CP0 Counter */
  42. static cycle_t au1x_counter1_read(struct clocksource *cs)
  43. {
  44. return au_readl(SYS_RTCREAD);
  45. }
  46. static struct clocksource au1x_counter1_clocksource = {
  47. .name = "alchemy-counter1",
  48. .read = au1x_counter1_read,
  49. .mask = CLOCKSOURCE_MASK(32),
  50. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  51. .rating = 100,
  52. };
  53. static int au1x_rtcmatch2_set_next_event(unsigned long delta,
  54. struct clock_event_device *cd)
  55. {
  56. delta += au_readl(SYS_RTCREAD);
  57. /* wait for register access */
  58. while (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_M21)
  59. ;
  60. au_writel(delta, SYS_RTCMATCH2);
  61. au_sync();
  62. return 0;
  63. }
  64. static void au1x_rtcmatch2_set_mode(enum clock_event_mode mode,
  65. struct clock_event_device *cd)
  66. {
  67. }
  68. static irqreturn_t au1x_rtcmatch2_irq(int irq, void *dev_id)
  69. {
  70. struct clock_event_device *cd = dev_id;
  71. cd->event_handler(cd);
  72. return IRQ_HANDLED;
  73. }
  74. static struct clock_event_device au1x_rtcmatch2_clockdev = {
  75. .name = "rtcmatch2",
  76. .features = CLOCK_EVT_FEAT_ONESHOT,
  77. .rating = 100,
  78. .irq = AU1000_RTC_MATCH2_INT,
  79. .set_next_event = au1x_rtcmatch2_set_next_event,
  80. .set_mode = au1x_rtcmatch2_set_mode,
  81. .cpumask = CPU_MASK_ALL_PTR,
  82. };
  83. static struct irqaction au1x_rtcmatch2_irqaction = {
  84. .handler = au1x_rtcmatch2_irq,
  85. .flags = IRQF_DISABLED | IRQF_TIMER,
  86. .name = "timer",
  87. .dev_id = &au1x_rtcmatch2_clockdev,
  88. };
  89. void __init plat_time_init(void)
  90. {
  91. struct clock_event_device *cd = &au1x_rtcmatch2_clockdev;
  92. unsigned long t;
  93. /* Check if firmware (YAMON, ...) has enabled 32kHz and clock
  94. * has been detected. If so install the rtcmatch2 clocksource,
  95. * otherwise don't bother. Note that both bits being set is by
  96. * no means a definite guarantee that the counters actually work
  97. * (the 32S bit seems to be stuck set to 1 once a single clock-
  98. * edge is detected, hence the timeouts).
  99. */
  100. if (CNTR_OK != (au_readl(SYS_COUNTER_CNTRL) & CNTR_OK))
  101. goto cntr_err;
  102. /*
  103. * setup counter 1 (RTC) to tick at full speed
  104. */
  105. t = 0xffffff;
  106. while ((au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_T1S) && --t)
  107. asm volatile ("nop");
  108. if (!t)
  109. goto cntr_err;
  110. au_writel(0, SYS_RTCTRIM); /* 32.768 kHz */
  111. au_sync();
  112. t = 0xffffff;
  113. while ((au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_C1S) && --t)
  114. asm volatile ("nop");
  115. if (!t)
  116. goto cntr_err;
  117. au_writel(0, SYS_RTCWRITE);
  118. au_sync();
  119. t = 0xffffff;
  120. while ((au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_C1S) && --t)
  121. asm volatile ("nop");
  122. if (!t)
  123. goto cntr_err;
  124. /* register counter1 clocksource and event device */
  125. clocksource_set_clock(&au1x_counter1_clocksource, 32768);
  126. clocksource_register(&au1x_counter1_clocksource);
  127. cd->shift = 32;
  128. cd->mult = div_sc(32768, NSEC_PER_SEC, cd->shift);
  129. cd->max_delta_ns = clockevent_delta2ns(0xffffffff, cd);
  130. cd->min_delta_ns = clockevent_delta2ns(8, cd); /* ~0.25ms */
  131. clockevents_register_device(cd);
  132. setup_irq(AU1000_RTC_MATCH2_INT, &au1x_rtcmatch2_irqaction);
  133. printk(KERN_INFO "Alchemy clocksource installed\n");
  134. /* can now use 'wait' */
  135. allow_au1k_wait = 1;
  136. return;
  137. cntr_err:
  138. /* counters unusable, use C0 counter */
  139. r4k_clockevent_init();
  140. init_r4k_clocksource();
  141. allow_au1k_wait = 0;
  142. }