clk_vexpress_osc.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2018 Arm Ltd
  4. * Author: Liviu Dudau <liviu.dudau@foss.arm.com>
  5. *
  6. */
  7. #define DEBUG
  8. #include <common.h>
  9. #include <clk-uclass.h>
  10. #include <dm.h>
  11. #include <log.h>
  12. #include <dm/device_compat.h>
  13. #include <dm/lists.h>
  14. #include <errno.h>
  15. #include <misc.h>
  16. #include <linux/bitops.h>
  17. #define CLK_FUNCTION BIT(20)
  18. struct vexpress_osc_clk_priv {
  19. u8 osc;
  20. ulong rate_min;
  21. ulong rate_max;
  22. };
  23. static ulong vexpress_osc_clk_get_rate(struct clk *clk)
  24. {
  25. int err;
  26. u32 data;
  27. struct udevice *vexpress_cfg = dev_get_parent(clk->dev);
  28. struct vexpress_osc_clk_priv *priv = dev_get_priv(clk->dev);
  29. data = CLK_FUNCTION | priv->osc;
  30. err = misc_read(vexpress_cfg, 0, &data, sizeof(data));
  31. if (err < 0)
  32. return err;
  33. return data;
  34. }
  35. #ifndef CONFIG_SPL_BUILD
  36. static ulong vexpress_osc_clk_set_rate(struct clk *clk, ulong rate)
  37. {
  38. int err;
  39. u32 buffer[2];
  40. struct udevice *vexpress_cfg = dev_get_parent(clk->dev);
  41. struct vexpress_osc_clk_priv *priv = dev_get_priv(clk->dev);
  42. if (rate < priv->rate_min || rate > priv->rate_max)
  43. return -EINVAL;
  44. /*
  45. * we are sending the parent the info about the oscillator
  46. * and the value we want to set
  47. */
  48. buffer[0] = CLK_FUNCTION | priv->osc;
  49. buffer[1] = rate;
  50. err = misc_write(vexpress_cfg, 0, buffer, 2 * sizeof(u32));
  51. if (err < 0)
  52. return err;
  53. return rate;
  54. }
  55. #endif
  56. static struct clk_ops vexpress_osc_clk_ops = {
  57. .get_rate = vexpress_osc_clk_get_rate,
  58. #ifndef CONFIG_SPL_BUILD
  59. .set_rate = vexpress_osc_clk_set_rate,
  60. #endif
  61. };
  62. static int vexpress_osc_clk_probe(struct udevice *dev)
  63. {
  64. struct vexpress_osc_clk_priv *priv = dev_get_priv(dev);
  65. u32 values[2];
  66. int err;
  67. err = dev_read_u32_array(dev, "freq-range", values, 2);
  68. if (err)
  69. return err;
  70. priv->rate_min = values[0];
  71. priv->rate_max = values[1];
  72. err = dev_read_u32_array(dev, "arm,vexpress-sysreg,func", values, 2);
  73. if (err)
  74. return err;
  75. if (values[0] != 1) {
  76. dev_err(dev, "Invalid VExpress function for clock, must be '1'");
  77. return -EINVAL;
  78. }
  79. priv->osc = values[1];
  80. debug("clk \"%s%d\", min freq %luHz, max freq %luHz\n", dev->name,
  81. priv->osc, priv->rate_min, priv->rate_max);
  82. return 0;
  83. }
  84. static const struct udevice_id vexpress_osc_clk_ids[] = {
  85. { .compatible = "arm,vexpress-osc", },
  86. {}
  87. };
  88. U_BOOT_DRIVER(vexpress_osc_clk) = {
  89. .name = "vexpress_osc_clk",
  90. .id = UCLASS_CLK,
  91. .of_match = vexpress_osc_clk_ids,
  92. .ops = &vexpress_osc_clk_ops,
  93. .priv_auto = sizeof(struct vexpress_osc_clk_priv),
  94. .probe = vexpress_osc_clk_probe,
  95. };