clk-msc313-cpupll.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2019 Daniel Palmer <daniel@thingy.jp>
  4. */
  5. #include <linux/clk-provider.h>
  6. #include <linux/device.h>
  7. #include <linux/kernel.h>
  8. #include <linux/of_address.h>
  9. #include <linux/platform_device.h>
  10. /*
  11. * This IP is not documented outside of the messy vendor driver.
  12. * Below is what we think the registers look like based on looking at
  13. * the vendor code and poking at the hardware:
  14. *
  15. * 0x140 -- LPF low. Seems to store one half of the clock transition
  16. * 0x144 /
  17. * 0x148 -- LPF high. Seems to store one half of the clock transition
  18. * 0x14c /
  19. * 0x150 -- vendor code says "toggle lpf enable"
  20. * 0x154 -- mu?
  21. * 0x15c -- lpf_update_count?
  22. * 0x160 -- vendor code says "switch to LPF". Clock source config? Register bank?
  23. * 0x164 -- vendor code says "from low to high" which seems to mean transition from LPF low to
  24. * LPF high.
  25. * 0x174 -- Seems to be the PLL lock status bit
  26. * 0x180 -- Seems to be the current frequency, this might need to be populated by software?
  27. * 0x184 / The vendor driver uses these to set the initial value of LPF low
  28. *
  29. * Frequency seems to be calculated like this:
  30. * (parent clock (432mhz) / register_magic_value) * 16 * 524288
  31. * Only the lower 24 bits of the resulting value will be used. In addition, the
  32. * PLL doesn't seem to be able to lock on frequencies lower than 220 MHz, as
  33. * divisor 0xfb586f (220 MHz) works but 0xfb7fff locks up.
  34. *
  35. * Vendor values:
  36. * frequency - register value
  37. *
  38. * 400000000 - 0x0067AE14
  39. * 600000000 - 0x00451EB8,
  40. * 800000000 - 0x0033D70A,
  41. * 1000000000 - 0x002978d4,
  42. */
  43. #define REG_LPF_LOW_L 0x140
  44. #define REG_LPF_LOW_H 0x144
  45. #define REG_LPF_HIGH_BOTTOM 0x148
  46. #define REG_LPF_HIGH_TOP 0x14c
  47. #define REG_LPF_TOGGLE 0x150
  48. #define REG_LPF_MYSTERYTWO 0x154
  49. #define REG_LPF_UPDATE_COUNT 0x15c
  50. #define REG_LPF_MYSTERYONE 0x160
  51. #define REG_LPF_TRANSITIONCTRL 0x164
  52. #define REG_LPF_LOCK 0x174
  53. #define REG_CURRENT 0x180
  54. #define LPF_LOCK_TIMEOUT 100000000
  55. #define MULTIPLIER_1 16
  56. #define MULTIPLIER_2 524288
  57. #define MULTIPLIER (MULTIPLIER_1 * MULTIPLIER_2)
  58. struct msc313_cpupll {
  59. void __iomem *base;
  60. struct clk_hw clk_hw;
  61. };
  62. #define to_cpupll(_hw) container_of(_hw, struct msc313_cpupll, clk_hw)
  63. static u32 msc313_cpupll_reg_read32(struct msc313_cpupll *cpupll, unsigned int reg)
  64. {
  65. u32 value;
  66. value = ioread16(cpupll->base + reg + 4) << 16;
  67. value |= ioread16(cpupll->base + reg);
  68. return value;
  69. }
  70. static void msc313_cpupll_reg_write32(struct msc313_cpupll *cpupll, unsigned int reg, u32 value)
  71. {
  72. u16 l = value & 0xffff, h = (value >> 16) & 0xffff;
  73. iowrite16(l, cpupll->base + reg);
  74. iowrite16(h, cpupll->base + reg + 4);
  75. }
  76. static void msc313_cpupll_setfreq(struct msc313_cpupll *cpupll, u32 regvalue)
  77. {
  78. ktime_t timeout;
  79. msc313_cpupll_reg_write32(cpupll, REG_LPF_HIGH_BOTTOM, regvalue);
  80. iowrite16(0x1, cpupll->base + REG_LPF_MYSTERYONE);
  81. iowrite16(0x6, cpupll->base + REG_LPF_MYSTERYTWO);
  82. iowrite16(0x8, cpupll->base + REG_LPF_UPDATE_COUNT);
  83. iowrite16(BIT(12), cpupll->base + REG_LPF_TRANSITIONCTRL);
  84. iowrite16(0, cpupll->base + REG_LPF_TOGGLE);
  85. iowrite16(1, cpupll->base + REG_LPF_TOGGLE);
  86. timeout = ktime_add_ns(ktime_get(), LPF_LOCK_TIMEOUT);
  87. while (!(ioread16(cpupll->base + REG_LPF_LOCK))) {
  88. if (ktime_after(ktime_get(), timeout)) {
  89. pr_err("timeout waiting for LPF_LOCK\n");
  90. return;
  91. }
  92. cpu_relax();
  93. }
  94. iowrite16(0, cpupll->base + REG_LPF_TOGGLE);
  95. msc313_cpupll_reg_write32(cpupll, REG_LPF_LOW_L, regvalue);
  96. }
  97. static unsigned long msc313_cpupll_frequencyforreg(u32 reg, unsigned long parent_rate)
  98. {
  99. unsigned long long prescaled = ((unsigned long long)parent_rate) * MULTIPLIER;
  100. if (prescaled == 0 || reg == 0)
  101. return 0;
  102. return DIV_ROUND_DOWN_ULL(prescaled, reg);
  103. }
  104. static u32 msc313_cpupll_regforfrequecy(unsigned long rate, unsigned long parent_rate)
  105. {
  106. unsigned long long prescaled = ((unsigned long long)parent_rate) * MULTIPLIER;
  107. if (prescaled == 0 || rate == 0)
  108. return 0;
  109. return DIV_ROUND_UP_ULL(prescaled, rate);
  110. }
  111. static unsigned long msc313_cpupll_recalc_rate(struct clk_hw *hw, unsigned long parent_rate)
  112. {
  113. struct msc313_cpupll *cpupll = to_cpupll(hw);
  114. return msc313_cpupll_frequencyforreg(msc313_cpupll_reg_read32(cpupll, REG_LPF_LOW_L),
  115. parent_rate);
  116. }
  117. static long msc313_cpupll_round_rate(struct clk_hw *hw, unsigned long rate,
  118. unsigned long *parent_rate)
  119. {
  120. u32 reg = msc313_cpupll_regforfrequecy(rate, *parent_rate);
  121. long rounded = msc313_cpupll_frequencyforreg(reg, *parent_rate);
  122. /*
  123. * This is my poor attempt at making sure the resulting
  124. * rate doesn't overshoot the requested rate.
  125. */
  126. for (; rounded >= rate && reg > 0; reg--)
  127. rounded = msc313_cpupll_frequencyforreg(reg, *parent_rate);
  128. return rounded;
  129. }
  130. static int msc313_cpupll_set_rate(struct clk_hw *hw, unsigned long rate, unsigned long parent_rate)
  131. {
  132. struct msc313_cpupll *cpupll = to_cpupll(hw);
  133. u32 reg = msc313_cpupll_regforfrequecy(rate, parent_rate);
  134. msc313_cpupll_setfreq(cpupll, reg);
  135. return 0;
  136. }
  137. static const struct clk_ops msc313_cpupll_ops = {
  138. .recalc_rate = msc313_cpupll_recalc_rate,
  139. .round_rate = msc313_cpupll_round_rate,
  140. .set_rate = msc313_cpupll_set_rate,
  141. };
  142. static const struct of_device_id msc313_cpupll_of_match[] = {
  143. { .compatible = "mstar,msc313-cpupll" },
  144. {}
  145. };
  146. static int msc313_cpupll_probe(struct platform_device *pdev)
  147. {
  148. struct clk_init_data clk_init = {};
  149. struct clk_parent_data cpupll_parent = { .index = 0 };
  150. struct device *dev = &pdev->dev;
  151. struct msc313_cpupll *cpupll;
  152. int ret;
  153. cpupll = devm_kzalloc(&pdev->dev, sizeof(*cpupll), GFP_KERNEL);
  154. if (!cpupll)
  155. return -ENOMEM;
  156. cpupll->base = devm_platform_ioremap_resource(pdev, 0);
  157. if (IS_ERR(cpupll->base))
  158. return PTR_ERR(cpupll->base);
  159. /* LPF might not contain the current frequency so fix that up */
  160. msc313_cpupll_reg_write32(cpupll, REG_LPF_LOW_L,
  161. msc313_cpupll_reg_read32(cpupll, REG_CURRENT));
  162. clk_init.name = dev_name(dev);
  163. clk_init.ops = &msc313_cpupll_ops;
  164. clk_init.parent_data = &cpupll_parent;
  165. clk_init.num_parents = 1;
  166. cpupll->clk_hw.init = &clk_init;
  167. ret = devm_clk_hw_register(dev, &cpupll->clk_hw);
  168. if (ret)
  169. return ret;
  170. return devm_of_clk_add_hw_provider(&pdev->dev, of_clk_hw_simple_get, &cpupll->clk_hw);
  171. }
  172. static struct platform_driver msc313_cpupll_driver = {
  173. .driver = {
  174. .name = "mstar-msc313-cpupll",
  175. .of_match_table = msc313_cpupll_of_match,
  176. },
  177. .probe = msc313_cpupll_probe,
  178. };
  179. builtin_platform_driver(msc313_cpupll_driver);