mpc83xx_timer.c 5.4 KB

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