rcar-cpg-lib.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Renesas RCar Gen3 CPG MSSR driver
  4. *
  5. * Copyright (C) 2017 Marek Vasut <marek.vasut@gmail.com>
  6. *
  7. * Based on the following driver from Linux kernel:
  8. * r8a7796 Clock Pulse Generator / Module Standby and Software Reset
  9. *
  10. * Copyright (C) 2016 Glider bvba
  11. */
  12. #include <common.h>
  13. #include <clk-uclass.h>
  14. #include <dm.h>
  15. #include <errno.h>
  16. #include <log.h>
  17. #include <wait_bit.h>
  18. #include <asm/global_data.h>
  19. #include <asm/io.h>
  20. #include <linux/bitfield.h>
  21. #include <linux/bitops.h>
  22. #include <linux/clk-provider.h>
  23. #include <dt-bindings/clock/renesas-cpg-mssr.h>
  24. #include "renesas-cpg-mssr.h"
  25. #include "rcar-gen3-cpg.h"
  26. #include "rcar-cpg-lib.h"
  27. #define SDnSRCFC_SHIFT 2
  28. #define STPnHCK_TABLE (CPG_SDCKCR_STPnHCK >> SDnSRCFC_SHIFT)
  29. /* Non-constant mask variant of FIELD_GET/FIELD_PREP */
  30. #define field_get(_mask, _reg) (((_reg) & (_mask)) >> (ffs(_mask) - 1))
  31. #define field_prep(_mask, _val) (((_val) << (ffs(_mask) - 1)) & (_mask))
  32. static const struct clk_div_table cpg_sdh_div_table[] = {
  33. { 0, 1 }, { 1, 2 }, { STPnHCK_TABLE | 2, 4 }, { STPnHCK_TABLE | 3, 8 },
  34. { STPnHCK_TABLE | 4, 16 }, { 0, 0 },
  35. };
  36. static const struct clk_div_table cpg_sd_div_table[] = {
  37. { 0, 2 }, { 1, 4 }, { 0, 0 },
  38. };
  39. static const struct clk_div_table cpg_rpc_div_table[] = {
  40. { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 }, { 0, 0 },
  41. };
  42. static unsigned int rcar_clk_get_table_div(const struct clk_div_table *table,
  43. const u32 value)
  44. {
  45. const struct clk_div_table *clkt;
  46. for (clkt = table; clkt->div; clkt++)
  47. if (clkt->val == value)
  48. return clkt->div;
  49. return 0;
  50. }
  51. static int rcar_clk_get_table_val(const struct clk_div_table *table,
  52. unsigned int div)
  53. {
  54. const struct clk_div_table *clkt;
  55. for (clkt = table; clkt->div; clkt++)
  56. if (clkt->div == div)
  57. return clkt->val;
  58. return -EINVAL;
  59. }
  60. s64 rcar_clk_get_rate64_div_table(unsigned int parent, u64 parent_rate,
  61. void __iomem *reg, const u32 mask,
  62. const struct clk_div_table *table, char *name)
  63. {
  64. u32 value, div;
  65. u64 rate;
  66. value = field_get(mask, readl(reg));
  67. div = rcar_clk_get_table_div(table, value);
  68. if (!div)
  69. return -EINVAL;
  70. rate = parent_rate / div;
  71. debug("%s[%i] %s clk: parent=%i div=%u => rate=%llu\n",
  72. __func__, __LINE__, name, parent, div, rate);
  73. return rate;
  74. }
  75. int rcar_clk_set_rate64_div_table(unsigned int parent, u64 parent_rate, ulong rate,
  76. void __iomem *reg, const u32 mask,
  77. const struct clk_div_table *table, char *name)
  78. {
  79. u32 value = 0, div = 0;
  80. div = DIV_ROUND_CLOSEST(parent_rate, rate);
  81. value = rcar_clk_get_table_val(table, div);
  82. if (value < 0)
  83. return value;
  84. clrsetbits_le32(reg, mask, field_prep(mask, value));
  85. debug("%s[%i] %s clk: parent=%i div=%u rate=%lu => val=%u\n",
  86. __func__, __LINE__, name, parent, div, rate, value);
  87. return 0;
  88. }
  89. s64 rcar_clk_get_rate64_rpc(unsigned int parent, u64 parent_rate, void __iomem *reg)
  90. {
  91. return rcar_clk_get_rate64_div_table(parent, parent_rate, reg,
  92. CPG_RPCCKCR_DIV_PRE_MASK,
  93. cpg_rpc_div_table, "RPC");
  94. }
  95. u64 rcar_clk_get_rate64_rpcd2(unsigned int parent, u64 parent_rate)
  96. {
  97. u64 rate = 0;
  98. rate = parent_rate / 2;
  99. debug("%s[%i] RPCD2 clk: parent=%i => rate=%llu\n",
  100. __func__, __LINE__, parent, rate);
  101. return rate;
  102. }
  103. s64 rcar_clk_get_rate64_sdh(unsigned int parent, u64 parent_rate, void __iomem *reg)
  104. {
  105. /*
  106. * This takes STPnHCK and STPnCK bits into consideration
  107. * in the table look up too, hence the inobvious GENMASK
  108. * below. Bits [7:5] always read zero, so this is OKish.
  109. */
  110. return rcar_clk_get_rate64_div_table(parent, parent_rate, reg,
  111. CPG_SDCKCR_SRCFC_MASK |
  112. GENMASK(9, 5),
  113. cpg_sdh_div_table, "SDH");
  114. }
  115. s64 rcar_clk_get_rate64_sd(unsigned int parent, u64 parent_rate, void __iomem *reg)
  116. {
  117. return rcar_clk_get_rate64_div_table(parent, parent_rate, reg,
  118. CPG_SDCKCR_FC_MASK,
  119. cpg_sd_div_table, "SD");
  120. }
  121. int rcar_clk_set_rate64_sdh(unsigned int parent, u64 parent_rate, ulong rate,
  122. void __iomem *reg)
  123. {
  124. /*
  125. * This takes STPnHCK and STPnCK bits into consideration
  126. * in the table look up too, hence the inobvious GENMASK
  127. * below. Bits [7:5] always read zero, so this is OKish.
  128. */
  129. return rcar_clk_set_rate64_div_table(parent, parent_rate, rate, reg,
  130. CPG_SDCKCR_SRCFC_MASK |
  131. GENMASK(9, 5),
  132. cpg_sdh_div_table, "SDH");
  133. }
  134. int rcar_clk_set_rate64_sd(unsigned int parent, u64 parent_rate, ulong rate,
  135. void __iomem *reg)
  136. {
  137. return rcar_clk_set_rate64_div_table(parent, parent_rate, rate, reg,
  138. CPG_SDCKCR_FC_MASK,
  139. cpg_sd_div_table, "SD");
  140. }