nomadik-mtu-timer.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2019 Stephan Gerhold <stephan@gerhold.net>
  4. *
  5. * Based on arch/arm/cpu/armv7/u8500/timer.c:
  6. * Copyright (C) 2010 Linaro Limited
  7. * John Rigby <john.rigby@linaro.org>
  8. *
  9. * Based on Linux kernel source and internal ST-Ericsson U-Boot source:
  10. * Copyright (C) 2009 Alessandro Rubini
  11. * Copyright (C) 2010 ST-Ericsson
  12. * Copyright (C) 2010 Linus Walleij for ST-Ericsson
  13. */
  14. #include <common.h>
  15. #include <dm.h>
  16. #include <timer.h>
  17. #include <asm/io.h>
  18. #include <linux/bitops.h>
  19. #define MTU_NUM_TIMERS 4
  20. /* The timers */
  21. struct nomadik_mtu_timer_regs {
  22. u32 lr; /* Load register */
  23. u32 cv; /* Current value */
  24. u32 cr; /* Control register */
  25. u32 bglr; /* Background load register */
  26. };
  27. /* The MTU that contains the timers */
  28. struct nomadik_mtu_regs {
  29. u32 imsc; /* Interrupt mask set/clear */
  30. u32 ris; /* Raw interrupt status */
  31. u32 mis; /* Masked interrupt status */
  32. u32 icr; /* Interrupt clear register */
  33. struct nomadik_mtu_timer_regs timers[MTU_NUM_TIMERS];
  34. };
  35. /* Bits for the control register */
  36. #define MTU_CR_ONESHOT BIT(0) /* if 0 = wraps reloading from BGLR */
  37. #define MTU_CR_32BITS BIT(1) /* if 0 = 16-bit counter */
  38. #define MTU_CR_PRESCALE_SHIFT 2
  39. #define MTU_CR_PRESCALE_1 (0 << MTU_CR_PRESCALE_SHIFT)
  40. #define MTU_CR_PRESCALE_16 (1 << MTU_CR_PRESCALE_SHIFT)
  41. #define MTU_CR_PRESCALE_256 (2 << MTU_CR_PRESCALE_SHIFT)
  42. #define MTU_CR_PERIODIC BIT(6) /* if 0 = free-running */
  43. #define MTU_CR_ENABLE BIT(7)
  44. struct nomadik_mtu_priv {
  45. struct nomadik_mtu_timer_regs *timer;
  46. };
  47. static u64 nomadik_mtu_get_count(struct udevice *dev)
  48. {
  49. struct nomadik_mtu_priv *priv = dev_get_priv(dev);
  50. /* Decrementing counter: invert the value */
  51. return timer_conv_64(~readl(&priv->timer->cv));
  52. }
  53. static int nomadik_mtu_probe(struct udevice *dev)
  54. {
  55. struct timer_dev_priv *uc_priv = dev_get_uclass_priv(dev);
  56. struct nomadik_mtu_priv *priv = dev_get_priv(dev);
  57. struct nomadik_mtu_regs *mtu;
  58. fdt_addr_t addr;
  59. u32 prescale;
  60. addr = dev_read_addr(dev);
  61. if (addr == FDT_ADDR_T_NONE)
  62. return -EINVAL;
  63. mtu = (struct nomadik_mtu_regs *)addr;
  64. priv->timer = mtu->timers; /* Use first timer */
  65. if (!uc_priv->clock_rate)
  66. return -EINVAL;
  67. /* Use divide-by-16 counter if tick rate is more than 32 MHz */
  68. if (uc_priv->clock_rate > 32000000) {
  69. uc_priv->clock_rate /= 16;
  70. prescale = MTU_CR_PRESCALE_16;
  71. } else {
  72. prescale = MTU_CR_PRESCALE_1;
  73. }
  74. /* Configure a free-running, auto-wrap counter with selected prescale */
  75. writel(MTU_CR_ENABLE | prescale | MTU_CR_32BITS, &priv->timer->cr);
  76. return 0;
  77. }
  78. static const struct timer_ops nomadik_mtu_ops = {
  79. .get_count = nomadik_mtu_get_count,
  80. };
  81. static const struct udevice_id nomadik_mtu_ids[] = {
  82. { .compatible = "st,nomadik-mtu" },
  83. {}
  84. };
  85. U_BOOT_DRIVER(nomadik_mtu) = {
  86. .name = "nomadik_mtu",
  87. .id = UCLASS_TIMER,
  88. .of_match = nomadik_mtu_ids,
  89. .priv_auto_alloc_size = sizeof(struct nomadik_mtu_priv),
  90. .probe = nomadik_mtu_probe,
  91. .ops = &nomadik_mtu_ops,
  92. };