mpc83xx_timer.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2018
  4. * Mario Six, Guntermann & Drunck GmbH, mario.six@gdsys.cc
  5. */
  6. #include <common.h>
  7. #include <clk.h>
  8. #include <dm.h>
  9. #include <irq_func.h>
  10. #include <log.h>
  11. #include <status_led.h>
  12. #include <sysinfo.h>
  13. #include <time.h>
  14. #include <timer.h>
  15. #include <watchdog.h>
  16. #include <asm/global_data.h>
  17. #include <asm/ptrace.h>
  18. #include <linux/bitops.h>
  19. DECLARE_GLOBAL_DATA_PTR;
  20. #ifndef CFG_SYS_WATCHDOG_FREQ
  21. #define CFG_SYS_WATCHDOG_FREQ (CONFIG_SYS_HZ / 2)
  22. #endif
  23. /**
  24. * struct mpc83xx_timer_priv - Private data structure for MPC83xx timer driver
  25. * @decrementer_count: Value to which the decrementer register should be re-set
  26. * to when a timer interrupt occurs, thus determines the
  27. * interrupt frequency (value for 1e6/HZ microseconds)
  28. * @timestamp: Counter for the number of timer interrupts that have
  29. * occurred (i.e. can be used to trigger events
  30. * periodically in the timer interrupt)
  31. */
  32. struct mpc83xx_timer_priv {
  33. uint decrementer_count;
  34. ulong timestamp;
  35. };
  36. /*
  37. * Bitmask for enabling the time base in the SPCR (System Priority
  38. * Configuration Register)
  39. */
  40. static const u32 SPCR_TBEN_MASK = BIT(31 - 9);
  41. /**
  42. * get_dec() - Get the value of the decrementer register
  43. *
  44. * Return: The value of the decrementer register
  45. */
  46. static inline unsigned long get_dec(void)
  47. {
  48. unsigned long val;
  49. asm volatile ("mfdec %0" : "=r" (val) : );
  50. return val;
  51. }
  52. /**
  53. * set_dec() - Set the value of the decrementer register
  54. * @val: The value of the decrementer register to be set
  55. */
  56. static inline void set_dec(unsigned long val)
  57. {
  58. if (val)
  59. asm volatile ("mtdec %0"::"r" (val));
  60. }
  61. /**
  62. * mftbu() - Get value of TBU (upper time base) register
  63. *
  64. * Return: Value of the TBU register
  65. */
  66. static inline u32 mftbu(void)
  67. {
  68. u32 rval;
  69. asm volatile("mftbu %0" : "=r" (rval));
  70. return rval;
  71. }
  72. /**
  73. * mftb() - Get value of TBL (lower time base) register
  74. *
  75. * Return: Value of the TBL register
  76. */
  77. static inline u32 mftb(void)
  78. {
  79. u32 rval;
  80. asm volatile("mftb %0" : "=r" (rval));
  81. return rval;
  82. }
  83. /*
  84. * TODO(mario.six@gdsys.cc): This should really be done by timer_init, and the
  85. * interrupt init should go into a interrupt driver.
  86. */
  87. int interrupt_init(void)
  88. {
  89. immap_t *immr = (immap_t *)CONFIG_SYS_IMMR;
  90. struct udevice *csb;
  91. struct udevice *sysinfo;
  92. struct udevice *timer;
  93. struct mpc83xx_timer_priv *timer_priv;
  94. struct clk clock;
  95. int ret;
  96. ret = uclass_first_device_err(UCLASS_TIMER, &timer);
  97. if (ret) {
  98. debug("%s: Could not find timer device (error: %d)",
  99. __func__, ret);
  100. return ret;
  101. }
  102. timer_priv = dev_get_priv(timer);
  103. if (sysinfo_get(&sysinfo)) {
  104. debug("%s: sysinfo device could not be fetched.\n", __func__);
  105. return -ENOENT;
  106. }
  107. ret = uclass_get_device_by_phandle(UCLASS_SIMPLE_BUS, sysinfo,
  108. "csb", &csb);
  109. if (ret) {
  110. debug("%s: Could not retrieve CSB device (error: %d)",
  111. __func__, ret);
  112. return ret;
  113. }
  114. ret = clk_get_by_index(csb, 0, &clock);
  115. if (ret) {
  116. debug("%s: Could not retrieve clock (error: %d)",
  117. __func__, ret);
  118. return ret;
  119. }
  120. timer_priv->decrementer_count = (clk_get_rate(&clock) / 4)
  121. / CONFIG_SYS_HZ;
  122. /* Enable e300 time base */
  123. setbits_be32(&immr->sysconf.spcr, SPCR_TBEN_MASK);
  124. set_dec(timer_priv->decrementer_count);
  125. /* Switch on interrupts */
  126. set_msr(get_msr() | MSR_EE);
  127. return 0;
  128. }
  129. /**
  130. * timer_interrupt() - Handler for the timer interrupt
  131. * @regs: Array of register values
  132. */
  133. void timer_interrupt(struct pt_regs *regs)
  134. {
  135. struct udevice *timer = gd->timer;
  136. struct mpc83xx_timer_priv *priv;
  137. /*
  138. * During initialization, gd->timer might not be set yet, but the timer
  139. * interrupt may already be enabled. In this case, wait for the
  140. * initialization to complete
  141. */
  142. if (!timer)
  143. return;
  144. priv = dev_get_priv(timer);
  145. /* Restore Decrementer Count */
  146. set_dec(priv->decrementer_count);
  147. priv->timestamp++;
  148. #if defined(CONFIG_WATCHDOG) || defined(CONFIG_HW_WATCHDOG)
  149. if (CFG_SYS_WATCHDOG_FREQ && (priv->timestamp % (CFG_SYS_WATCHDOG_FREQ)) == 0)
  150. schedule();
  151. #endif /* CONFIG_WATCHDOG || CONFIG_HW_WATCHDOG */
  152. #ifdef CONFIG_LED_STATUS
  153. status_led_tick(priv->timestamp);
  154. #endif /* CONFIG_LED_STATUS */
  155. }
  156. void wait_ticks(ulong ticks)
  157. {
  158. ulong end = get_ticks() + ticks;
  159. while (end > get_ticks())
  160. schedule();
  161. }
  162. static u64 mpc83xx_timer_get_count(struct udevice *dev)
  163. {
  164. u32 tbu, tbl;
  165. /*
  166. * To make sure that no tbl overflow occurred between reading tbl and
  167. * tbu, read tbu again, and compare it with the previously read tbu
  168. * value: If they're different, a tbl overflow has occurred.
  169. */
  170. do {
  171. tbu = mftbu();
  172. tbl = mftb();
  173. } while (tbu != mftbu());
  174. return (tbu * 0x10000ULL) + tbl;
  175. }
  176. static int mpc83xx_timer_probe(struct udevice *dev)
  177. {
  178. struct timer_dev_priv *uc_priv = dev_get_uclass_priv(dev);
  179. struct clk clock;
  180. int ret;
  181. ret = interrupt_init();
  182. if (ret) {
  183. debug("%s: interrupt_init failed (err = %d)\n",
  184. dev->name, ret);
  185. return ret;
  186. }
  187. ret = clk_get_by_index(dev, 0, &clock);
  188. if (ret) {
  189. debug("%s: Could not retrieve clock (err = %d)\n",
  190. dev->name, ret);
  191. return ret;
  192. }
  193. uc_priv->clock_rate = (clk_get_rate(&clock) + 3L) / 4L;
  194. return 0;
  195. }
  196. static const struct timer_ops mpc83xx_timer_ops = {
  197. .get_count = mpc83xx_timer_get_count,
  198. };
  199. static const struct udevice_id mpc83xx_timer_ids[] = {
  200. { .compatible = "fsl,mpc83xx-timer" },
  201. { /* sentinel */ }
  202. };
  203. U_BOOT_DRIVER(mpc83xx_timer) = {
  204. .name = "mpc83xx_timer",
  205. .id = UCLASS_TIMER,
  206. .of_match = mpc83xx_timer_ids,
  207. .probe = mpc83xx_timer_probe,
  208. .ops = &mpc83xx_timer_ops,
  209. .priv_auto = sizeof(struct mpc83xx_timer_priv),
  210. };