ostm_timer.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Renesas RZ/A1 R7S72100 OSTM Timer driver
  4. *
  5. * Copyright (C) 2019 Marek Vasut <marek.vasut@gmail.com>
  6. */
  7. #include <common.h>
  8. #include <malloc.h>
  9. #include <asm/io.h>
  10. #include <dm.h>
  11. #include <clk.h>
  12. #include <timer.h>
  13. #include <linux/bitops.h>
  14. #define OSTM_CMP 0x00
  15. #define OSTM_CNT 0x04
  16. #define OSTM_TE 0x10
  17. #define OSTM_TS 0x14
  18. #define OSTM_TT 0x18
  19. #define OSTM_CTL 0x20
  20. #define OSTM_CTL_D BIT(1)
  21. DECLARE_GLOBAL_DATA_PTR;
  22. struct ostm_priv {
  23. fdt_addr_t regs;
  24. };
  25. static int ostm_get_count(struct udevice *dev, u64 *count)
  26. {
  27. struct ostm_priv *priv = dev_get_priv(dev);
  28. *count = timer_conv_64(readl(priv->regs + OSTM_CNT));
  29. return 0;
  30. }
  31. static int ostm_probe(struct udevice *dev)
  32. {
  33. struct timer_dev_priv *uc_priv = dev_get_uclass_priv(dev);
  34. struct ostm_priv *priv = dev_get_priv(dev);
  35. #if CONFIG_IS_ENABLED(CLK)
  36. struct clk clk;
  37. int ret;
  38. ret = clk_get_by_index(dev, 0, &clk);
  39. if (ret)
  40. return ret;
  41. uc_priv->clock_rate = clk_get_rate(&clk);
  42. clk_free(&clk);
  43. #else
  44. uc_priv->clock_rate = CONFIG_SYS_CLK_FREQ / 2;
  45. #endif
  46. readb(priv->regs + OSTM_CTL);
  47. writeb(OSTM_CTL_D, priv->regs + OSTM_CTL);
  48. setbits_8(priv->regs + OSTM_TT, BIT(0));
  49. writel(0xffffffff, priv->regs + OSTM_CMP);
  50. setbits_8(priv->regs + OSTM_TS, BIT(0));
  51. return 0;
  52. }
  53. static int ostm_ofdata_to_platdata(struct udevice *dev)
  54. {
  55. struct ostm_priv *priv = dev_get_priv(dev);
  56. priv->regs = dev_read_addr(dev);
  57. return 0;
  58. }
  59. static const struct timer_ops ostm_ops = {
  60. .get_count = ostm_get_count,
  61. };
  62. static const struct udevice_id ostm_ids[] = {
  63. { .compatible = "renesas,ostm" },
  64. {}
  65. };
  66. U_BOOT_DRIVER(ostm_timer) = {
  67. .name = "ostm-timer",
  68. .id = UCLASS_TIMER,
  69. .ops = &ostm_ops,
  70. .probe = ostm_probe,
  71. .of_match = ostm_ids,
  72. .ofdata_to_platdata = ostm_ofdata_to_platdata,
  73. .priv_auto_alloc_size = sizeof(struct ostm_priv),
  74. };