ostm_timer.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 u64 ostm_get_count(struct udevice *dev)
  26. {
  27. struct ostm_priv *priv = dev_get_priv(dev);
  28. return timer_conv_64(readl(priv->regs + OSTM_CNT));
  29. }
  30. static int ostm_probe(struct udevice *dev)
  31. {
  32. struct timer_dev_priv *uc_priv = dev_get_uclass_priv(dev);
  33. struct ostm_priv *priv = dev_get_priv(dev);
  34. #if CONFIG_IS_ENABLED(CLK)
  35. struct clk clk;
  36. int ret;
  37. ret = clk_get_by_index(dev, 0, &clk);
  38. if (ret)
  39. return ret;
  40. uc_priv->clock_rate = clk_get_rate(&clk);
  41. clk_free(&clk);
  42. #else
  43. uc_priv->clock_rate = CONFIG_SYS_CLK_FREQ / 2;
  44. #endif
  45. readb(priv->regs + OSTM_CTL);
  46. writeb(OSTM_CTL_D, priv->regs + OSTM_CTL);
  47. setbits_8(priv->regs + OSTM_TT, BIT(0));
  48. writel(0xffffffff, priv->regs + OSTM_CMP);
  49. setbits_8(priv->regs + OSTM_TS, BIT(0));
  50. return 0;
  51. }
  52. static int ostm_ofdata_to_platdata(struct udevice *dev)
  53. {
  54. struct ostm_priv *priv = dev_get_priv(dev);
  55. priv->regs = dev_read_addr(dev);
  56. return 0;
  57. }
  58. static const struct timer_ops ostm_ops = {
  59. .get_count = ostm_get_count,
  60. };
  61. static const struct udevice_id ostm_ids[] = {
  62. { .compatible = "renesas,ostm" },
  63. {}
  64. };
  65. U_BOOT_DRIVER(ostm_timer) = {
  66. .name = "ostm-timer",
  67. .id = UCLASS_TIMER,
  68. .ops = &ostm_ops,
  69. .probe = ostm_probe,
  70. .of_match = ostm_ids,
  71. .ofdata_to_platdata = ostm_ofdata_to_platdata,
  72. .priv_auto_alloc_size = sizeof(struct ostm_priv),
  73. };