time.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2008-2009 Manuel Lauss <[email protected]>
  4. *
  5. * Previous incarnations were:
  6. * Copyright (C) 2001, 2006, 2008 MontaVista Software, <[email protected]>
  7. * Copied and modified Carsten Langgaard's time.c
  8. *
  9. * Carsten Langgaard, [email protected]
  10. * Copyright (C) 1999,2000 MIPS Technologies, Inc. All rights reserved.
  11. *
  12. * ########################################################################
  13. *
  14. * ########################################################################
  15. *
  16. * Clocksource/event using the 32.768kHz-clocked Counter1 ('RTC' in the
  17. * databooks). Firmware/Board init code must enable the counters in the
  18. * counter control register, otherwise the CP0 counter clocksource/event
  19. * will be installed instead (and use of 'wait' instruction is prohibited).
  20. */
  21. #include <linux/clockchips.h>
  22. #include <linux/clocksource.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/spinlock.h>
  25. #include <asm/idle.h>
  26. #include <asm/processor.h>
  27. #include <asm/time.h>
  28. #include <asm/mach-au1x00/au1000.h>
  29. /* 32kHz clock enabled and detected */
  30. #define CNTR_OK (SYS_CNTRL_E0 | SYS_CNTRL_32S)
  31. static u64 au1x_counter1_read(struct clocksource *cs)
  32. {
  33. return alchemy_rdsys(AU1000_SYS_RTCREAD);
  34. }
  35. static struct clocksource au1x_counter1_clocksource = {
  36. .name = "alchemy-counter1",
  37. .read = au1x_counter1_read,
  38. .mask = CLOCKSOURCE_MASK(32),
  39. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  40. .rating = 1500,
  41. };
  42. static int au1x_rtcmatch2_set_next_event(unsigned long delta,
  43. struct clock_event_device *cd)
  44. {
  45. delta += alchemy_rdsys(AU1000_SYS_RTCREAD);
  46. /* wait for register access */
  47. while (alchemy_rdsys(AU1000_SYS_CNTRCTRL) & SYS_CNTRL_M21)
  48. ;
  49. alchemy_wrsys(delta, AU1000_SYS_RTCMATCH2);
  50. return 0;
  51. }
  52. static irqreturn_t au1x_rtcmatch2_irq(int irq, void *dev_id)
  53. {
  54. struct clock_event_device *cd = dev_id;
  55. cd->event_handler(cd);
  56. return IRQ_HANDLED;
  57. }
  58. static struct clock_event_device au1x_rtcmatch2_clockdev = {
  59. .name = "rtcmatch2",
  60. .features = CLOCK_EVT_FEAT_ONESHOT,
  61. .rating = 1500,
  62. .set_next_event = au1x_rtcmatch2_set_next_event,
  63. .cpumask = cpu_possible_mask,
  64. };
  65. static int __init alchemy_time_init(unsigned int m2int)
  66. {
  67. struct clock_event_device *cd = &au1x_rtcmatch2_clockdev;
  68. unsigned long t;
  69. au1x_rtcmatch2_clockdev.irq = m2int;
  70. /* Check if firmware (YAMON, ...) has enabled 32kHz and clock
  71. * has been detected. If so install the rtcmatch2 clocksource,
  72. * otherwise don't bother. Note that both bits being set is by
  73. * no means a definite guarantee that the counters actually work
  74. * (the 32S bit seems to be stuck set to 1 once a single clock-
  75. * edge is detected, hence the timeouts).
  76. */
  77. if (CNTR_OK != (alchemy_rdsys(AU1000_SYS_CNTRCTRL) & CNTR_OK))
  78. goto cntr_err;
  79. /*
  80. * setup counter 1 (RTC) to tick at full speed
  81. */
  82. t = 0xffffff;
  83. while ((alchemy_rdsys(AU1000_SYS_CNTRCTRL) & SYS_CNTRL_T1S) && --t)
  84. asm volatile ("nop");
  85. if (!t)
  86. goto cntr_err;
  87. alchemy_wrsys(0, AU1000_SYS_RTCTRIM); /* 32.768 kHz */
  88. t = 0xffffff;
  89. while ((alchemy_rdsys(AU1000_SYS_CNTRCTRL) & SYS_CNTRL_C1S) && --t)
  90. asm volatile ("nop");
  91. if (!t)
  92. goto cntr_err;
  93. alchemy_wrsys(0, AU1000_SYS_RTCWRITE);
  94. t = 0xffffff;
  95. while ((alchemy_rdsys(AU1000_SYS_CNTRCTRL) & SYS_CNTRL_C1S) && --t)
  96. asm volatile ("nop");
  97. if (!t)
  98. goto cntr_err;
  99. /* register counter1 clocksource and event device */
  100. clocksource_register_hz(&au1x_counter1_clocksource, 32768);
  101. cd->shift = 32;
  102. cd->mult = div_sc(32768, NSEC_PER_SEC, cd->shift);
  103. cd->max_delta_ns = clockevent_delta2ns(0xffffffff, cd);
  104. cd->max_delta_ticks = 0xffffffff;
  105. cd->min_delta_ns = clockevent_delta2ns(9, cd);
  106. cd->min_delta_ticks = 9; /* ~0.28ms */
  107. clockevents_register_device(cd);
  108. if (request_irq(m2int, au1x_rtcmatch2_irq, IRQF_TIMER, "timer",
  109. &au1x_rtcmatch2_clockdev))
  110. pr_err("Failed to register timer interrupt\n");
  111. printk(KERN_INFO "Alchemy clocksource installed\n");
  112. return 0;
  113. cntr_err:
  114. return -1;
  115. }
  116. static int alchemy_m2inttab[] __initdata = {
  117. AU1000_RTC_MATCH2_INT,
  118. AU1500_RTC_MATCH2_INT,
  119. AU1100_RTC_MATCH2_INT,
  120. AU1550_RTC_MATCH2_INT,
  121. AU1200_RTC_MATCH2_INT,
  122. AU1300_RTC_MATCH2_INT,
  123. };
  124. void __init plat_time_init(void)
  125. {
  126. int t;
  127. t = alchemy_get_cputype();
  128. if (t == ALCHEMY_CPU_UNKNOWN ||
  129. alchemy_time_init(alchemy_m2inttab[t]))
  130. cpu_wait = NULL; /* wait doesn't work with r4k timer */
  131. }