time.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * Copyright (C) 2008-2009 Manuel Lauss <manuel.lauss@gmail.com>
  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/idle.h>
  38. #include <asm/processor.h>
  39. #include <asm/time.h>
  40. #include <asm/mach-au1x00/au1000.h>
  41. /* 32kHz clock enabled and detected */
  42. #define CNTR_OK (SYS_CNTRL_E0 | SYS_CNTRL_32S)
  43. static cycle_t au1x_counter1_read(struct clocksource *cs)
  44. {
  45. return alchemy_rdsys(AU1000_SYS_RTCREAD);
  46. }
  47. static struct clocksource au1x_counter1_clocksource = {
  48. .name = "alchemy-counter1",
  49. .read = au1x_counter1_read,
  50. .mask = CLOCKSOURCE_MASK(32),
  51. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  52. .rating = 1500,
  53. };
  54. static int au1x_rtcmatch2_set_next_event(unsigned long delta,
  55. struct clock_event_device *cd)
  56. {
  57. delta += alchemy_rdsys(AU1000_SYS_RTCREAD);
  58. /* wait for register access */
  59. while (alchemy_rdsys(AU1000_SYS_CNTRCTRL) & SYS_CNTRL_M21)
  60. ;
  61. alchemy_wrsys(delta, AU1000_SYS_RTCMATCH2);
  62. return 0;
  63. }
  64. static irqreturn_t au1x_rtcmatch2_irq(int irq, void *dev_id)
  65. {
  66. struct clock_event_device *cd = dev_id;
  67. cd->event_handler(cd);
  68. return IRQ_HANDLED;
  69. }
  70. static struct clock_event_device au1x_rtcmatch2_clockdev = {
  71. .name = "rtcmatch2",
  72. .features = CLOCK_EVT_FEAT_ONESHOT,
  73. .rating = 1500,
  74. .set_next_event = au1x_rtcmatch2_set_next_event,
  75. .cpumask = cpu_all_mask,
  76. };
  77. static struct irqaction au1x_rtcmatch2_irqaction = {
  78. .handler = au1x_rtcmatch2_irq,
  79. .flags = IRQF_TIMER,
  80. .name = "timer",
  81. .dev_id = &au1x_rtcmatch2_clockdev,
  82. };
  83. static int __init alchemy_time_init(unsigned int m2int)
  84. {
  85. struct clock_event_device *cd = &au1x_rtcmatch2_clockdev;
  86. unsigned long t;
  87. au1x_rtcmatch2_clockdev.irq = m2int;
  88. /* Check if firmware (YAMON, ...) has enabled 32kHz and clock
  89. * has been detected. If so install the rtcmatch2 clocksource,
  90. * otherwise don't bother. Note that both bits being set is by
  91. * no means a definite guarantee that the counters actually work
  92. * (the 32S bit seems to be stuck set to 1 once a single clock-
  93. * edge is detected, hence the timeouts).
  94. */
  95. if (CNTR_OK != (alchemy_rdsys(AU1000_SYS_CNTRCTRL) & CNTR_OK))
  96. goto cntr_err;
  97. /*
  98. * setup counter 1 (RTC) to tick at full speed
  99. */
  100. t = 0xffffff;
  101. while ((alchemy_rdsys(AU1000_SYS_CNTRCTRL) & SYS_CNTRL_T1S) && --t)
  102. asm volatile ("nop");
  103. if (!t)
  104. goto cntr_err;
  105. alchemy_wrsys(0, AU1000_SYS_RTCTRIM); /* 32.768 kHz */
  106. t = 0xffffff;
  107. while ((alchemy_rdsys(AU1000_SYS_CNTRCTRL) & SYS_CNTRL_C1S) && --t)
  108. asm volatile ("nop");
  109. if (!t)
  110. goto cntr_err;
  111. alchemy_wrsys(0, AU1000_SYS_RTCWRITE);
  112. t = 0xffffff;
  113. while ((alchemy_rdsys(AU1000_SYS_CNTRCTRL) & SYS_CNTRL_C1S) && --t)
  114. asm volatile ("nop");
  115. if (!t)
  116. goto cntr_err;
  117. /* register counter1 clocksource and event device */
  118. clocksource_register_hz(&au1x_counter1_clocksource, 32768);
  119. cd->shift = 32;
  120. cd->mult = div_sc(32768, NSEC_PER_SEC, cd->shift);
  121. cd->max_delta_ns = clockevent_delta2ns(0xffffffff, cd);
  122. cd->min_delta_ns = clockevent_delta2ns(9, cd); /* ~0.28ms */
  123. clockevents_register_device(cd);
  124. setup_irq(m2int, &au1x_rtcmatch2_irqaction);
  125. printk(KERN_INFO "Alchemy clocksource installed\n");
  126. return 0;
  127. cntr_err:
  128. return -1;
  129. }
  130. static int alchemy_m2inttab[] __initdata = {
  131. AU1000_RTC_MATCH2_INT,
  132. AU1500_RTC_MATCH2_INT,
  133. AU1100_RTC_MATCH2_INT,
  134. AU1550_RTC_MATCH2_INT,
  135. AU1200_RTC_MATCH2_INT,
  136. AU1300_RTC_MATCH2_INT,
  137. };
  138. void __init plat_time_init(void)
  139. {
  140. int t;
  141. t = alchemy_get_cputype();
  142. if (t == ALCHEMY_CPU_UNKNOWN ||
  143. alchemy_time_init(alchemy_m2inttab[t]))
  144. cpu_wait = NULL; /* wait doesn't work with r4k timer */
  145. }