berlin2-avpll.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2014 Marvell Technology Group Ltd.
  4. *
  5. * Sebastian Hesselbarth <[email protected]>
  6. * Alexandre Belloni <[email protected]>
  7. */
  8. #include <linux/clk-provider.h>
  9. #include <linux/io.h>
  10. #include <linux/kernel.h>
  11. #include <linux/of.h>
  12. #include <linux/of_address.h>
  13. #include <linux/slab.h>
  14. #include "berlin2-avpll.h"
  15. /*
  16. * Berlin2 SoCs comprise up to two PLLs called AVPLL built upon a
  17. * VCO with 8 channels each, channel 8 is the odd-one-out and does
  18. * not provide mul/div.
  19. *
  20. * Unfortunately, its registers are not named but just numbered. To
  21. * get in at least some kind of structure, we split each AVPLL into
  22. * the VCOs and each channel into separate clock drivers.
  23. *
  24. * Also, here and there the VCO registers are a bit different with
  25. * respect to bit shifts. Make sure to add a comment for those.
  26. */
  27. #define NUM_CHANNELS 8
  28. #define AVPLL_CTRL(x) ((x) * 0x4)
  29. #define VCO_CTRL0 AVPLL_CTRL(0)
  30. /* BG2/BG2CDs VCO_B has an additional shift of 4 for its VCO_CTRL0 reg */
  31. #define VCO_RESET BIT(0)
  32. #define VCO_POWERUP BIT(1)
  33. #define VCO_INTERPOL_SHIFT 2
  34. #define VCO_INTERPOL_MASK (0xf << VCO_INTERPOL_SHIFT)
  35. #define VCO_REG1V45_SEL_SHIFT 6
  36. #define VCO_REG1V45_SEL(x) ((x) << VCO_REG1V45_SEL_SHIFT)
  37. #define VCO_REG1V45_SEL_1V40 VCO_REG1V45_SEL(0)
  38. #define VCO_REG1V45_SEL_1V45 VCO_REG1V45_SEL(1)
  39. #define VCO_REG1V45_SEL_1V50 VCO_REG1V45_SEL(2)
  40. #define VCO_REG1V45_SEL_1V55 VCO_REG1V45_SEL(3)
  41. #define VCO_REG1V45_SEL_MASK VCO_REG1V45_SEL(3)
  42. #define VCO_REG0V9_SEL_SHIFT 8
  43. #define VCO_REG0V9_SEL_MASK (0xf << VCO_REG0V9_SEL_SHIFT)
  44. #define VCO_VTHCAL_SHIFT 12
  45. #define VCO_VTHCAL(x) ((x) << VCO_VTHCAL_SHIFT)
  46. #define VCO_VTHCAL_0V90 VCO_VTHCAL(0)
  47. #define VCO_VTHCAL_0V95 VCO_VTHCAL(1)
  48. #define VCO_VTHCAL_1V00 VCO_VTHCAL(2)
  49. #define VCO_VTHCAL_1V05 VCO_VTHCAL(3)
  50. #define VCO_VTHCAL_MASK VCO_VTHCAL(3)
  51. #define VCO_KVCOEXT_SHIFT 14
  52. #define VCO_KVCOEXT_MASK (0x3 << VCO_KVCOEXT_SHIFT)
  53. #define VCO_KVCOEXT_ENABLE BIT(17)
  54. #define VCO_V2IEXT_SHIFT 18
  55. #define VCO_V2IEXT_MASK (0xf << VCO_V2IEXT_SHIFT)
  56. #define VCO_V2IEXT_ENABLE BIT(22)
  57. #define VCO_SPEED_SHIFT 23
  58. #define VCO_SPEED(x) ((x) << VCO_SPEED_SHIFT)
  59. #define VCO_SPEED_1G08_1G21 VCO_SPEED(0)
  60. #define VCO_SPEED_1G21_1G40 VCO_SPEED(1)
  61. #define VCO_SPEED_1G40_1G61 VCO_SPEED(2)
  62. #define VCO_SPEED_1G61_1G86 VCO_SPEED(3)
  63. #define VCO_SPEED_1G86_2G00 VCO_SPEED(4)
  64. #define VCO_SPEED_2G00_2G22 VCO_SPEED(5)
  65. #define VCO_SPEED_2G22 VCO_SPEED(6)
  66. #define VCO_SPEED_MASK VCO_SPEED(0x7)
  67. #define VCO_CLKDET_ENABLE BIT(26)
  68. #define VCO_CTRL1 AVPLL_CTRL(1)
  69. #define VCO_REFDIV_SHIFT 0
  70. #define VCO_REFDIV(x) ((x) << VCO_REFDIV_SHIFT)
  71. #define VCO_REFDIV_1 VCO_REFDIV(0)
  72. #define VCO_REFDIV_2 VCO_REFDIV(1)
  73. #define VCO_REFDIV_4 VCO_REFDIV(2)
  74. #define VCO_REFDIV_3 VCO_REFDIV(3)
  75. #define VCO_REFDIV_MASK VCO_REFDIV(0x3f)
  76. #define VCO_FBDIV_SHIFT 6
  77. #define VCO_FBDIV(x) ((x) << VCO_FBDIV_SHIFT)
  78. #define VCO_FBDIV_MASK VCO_FBDIV(0xff)
  79. #define VCO_ICP_SHIFT 14
  80. /* PLL Charge Pump Current = 10uA * (x + 1) */
  81. #define VCO_ICP(x) ((x) << VCO_ICP_SHIFT)
  82. #define VCO_ICP_MASK VCO_ICP(0xf)
  83. #define VCO_LOAD_CAP BIT(18)
  84. #define VCO_CALIBRATION_START BIT(19)
  85. #define VCO_FREQOFFSETn(x) AVPLL_CTRL(3 + (x))
  86. #define VCO_FREQOFFSET_MASK 0x7ffff
  87. #define VCO_CTRL10 AVPLL_CTRL(10)
  88. #define VCO_POWERUP_CH1 BIT(20)
  89. #define VCO_CTRL11 AVPLL_CTRL(11)
  90. #define VCO_CTRL12 AVPLL_CTRL(12)
  91. #define VCO_CTRL13 AVPLL_CTRL(13)
  92. #define VCO_CTRL14 AVPLL_CTRL(14)
  93. #define VCO_CTRL15 AVPLL_CTRL(15)
  94. #define VCO_SYNC1n(x) AVPLL_CTRL(15 + (x))
  95. #define VCO_SYNC1_MASK 0x1ffff
  96. #define VCO_SYNC2n(x) AVPLL_CTRL(23 + (x))
  97. #define VCO_SYNC2_MASK 0x1ffff
  98. #define VCO_CTRL30 AVPLL_CTRL(30)
  99. #define VCO_DPLL_CH1_ENABLE BIT(17)
  100. struct berlin2_avpll_vco {
  101. struct clk_hw hw;
  102. void __iomem *base;
  103. u8 flags;
  104. };
  105. #define to_avpll_vco(hw) container_of(hw, struct berlin2_avpll_vco, hw)
  106. static int berlin2_avpll_vco_is_enabled(struct clk_hw *hw)
  107. {
  108. struct berlin2_avpll_vco *vco = to_avpll_vco(hw);
  109. u32 reg;
  110. reg = readl_relaxed(vco->base + VCO_CTRL0);
  111. if (vco->flags & BERLIN2_AVPLL_BIT_QUIRK)
  112. reg >>= 4;
  113. return !!(reg & VCO_POWERUP);
  114. }
  115. static int berlin2_avpll_vco_enable(struct clk_hw *hw)
  116. {
  117. struct berlin2_avpll_vco *vco = to_avpll_vco(hw);
  118. u32 reg;
  119. reg = readl_relaxed(vco->base + VCO_CTRL0);
  120. if (vco->flags & BERLIN2_AVPLL_BIT_QUIRK)
  121. reg |= VCO_POWERUP << 4;
  122. else
  123. reg |= VCO_POWERUP;
  124. writel_relaxed(reg, vco->base + VCO_CTRL0);
  125. return 0;
  126. }
  127. static void berlin2_avpll_vco_disable(struct clk_hw *hw)
  128. {
  129. struct berlin2_avpll_vco *vco = to_avpll_vco(hw);
  130. u32 reg;
  131. reg = readl_relaxed(vco->base + VCO_CTRL0);
  132. if (vco->flags & BERLIN2_AVPLL_BIT_QUIRK)
  133. reg &= ~(VCO_POWERUP << 4);
  134. else
  135. reg &= ~VCO_POWERUP;
  136. writel_relaxed(reg, vco->base + VCO_CTRL0);
  137. }
  138. static u8 vco_refdiv[] = { 1, 2, 4, 3 };
  139. static unsigned long
  140. berlin2_avpll_vco_recalc_rate(struct clk_hw *hw, unsigned long parent_rate)
  141. {
  142. struct berlin2_avpll_vco *vco = to_avpll_vco(hw);
  143. u32 reg, refdiv, fbdiv;
  144. u64 freq = parent_rate;
  145. /* AVPLL VCO frequency: Fvco = (Fref / refdiv) * fbdiv */
  146. reg = readl_relaxed(vco->base + VCO_CTRL1);
  147. refdiv = (reg & VCO_REFDIV_MASK) >> VCO_REFDIV_SHIFT;
  148. refdiv = vco_refdiv[refdiv];
  149. fbdiv = (reg & VCO_FBDIV_MASK) >> VCO_FBDIV_SHIFT;
  150. freq *= fbdiv;
  151. do_div(freq, refdiv);
  152. return (unsigned long)freq;
  153. }
  154. static const struct clk_ops berlin2_avpll_vco_ops = {
  155. .is_enabled = berlin2_avpll_vco_is_enabled,
  156. .enable = berlin2_avpll_vco_enable,
  157. .disable = berlin2_avpll_vco_disable,
  158. .recalc_rate = berlin2_avpll_vco_recalc_rate,
  159. };
  160. int __init berlin2_avpll_vco_register(void __iomem *base,
  161. const char *name, const char *parent_name,
  162. u8 vco_flags, unsigned long flags)
  163. {
  164. struct berlin2_avpll_vco *vco;
  165. struct clk_init_data init;
  166. vco = kzalloc(sizeof(*vco), GFP_KERNEL);
  167. if (!vco)
  168. return -ENOMEM;
  169. vco->base = base;
  170. vco->flags = vco_flags;
  171. vco->hw.init = &init;
  172. init.name = name;
  173. init.ops = &berlin2_avpll_vco_ops;
  174. init.parent_names = &parent_name;
  175. init.num_parents = 1;
  176. init.flags = flags;
  177. return clk_hw_register(NULL, &vco->hw);
  178. }
  179. struct berlin2_avpll_channel {
  180. struct clk_hw hw;
  181. void __iomem *base;
  182. u8 flags;
  183. u8 index;
  184. };
  185. #define to_avpll_channel(hw) container_of(hw, struct berlin2_avpll_channel, hw)
  186. static int berlin2_avpll_channel_is_enabled(struct clk_hw *hw)
  187. {
  188. struct berlin2_avpll_channel *ch = to_avpll_channel(hw);
  189. u32 reg;
  190. if (ch->index == 7)
  191. return 1;
  192. reg = readl_relaxed(ch->base + VCO_CTRL10);
  193. reg &= VCO_POWERUP_CH1 << ch->index;
  194. return !!reg;
  195. }
  196. static int berlin2_avpll_channel_enable(struct clk_hw *hw)
  197. {
  198. struct berlin2_avpll_channel *ch = to_avpll_channel(hw);
  199. u32 reg;
  200. reg = readl_relaxed(ch->base + VCO_CTRL10);
  201. reg |= VCO_POWERUP_CH1 << ch->index;
  202. writel_relaxed(reg, ch->base + VCO_CTRL10);
  203. return 0;
  204. }
  205. static void berlin2_avpll_channel_disable(struct clk_hw *hw)
  206. {
  207. struct berlin2_avpll_channel *ch = to_avpll_channel(hw);
  208. u32 reg;
  209. reg = readl_relaxed(ch->base + VCO_CTRL10);
  210. reg &= ~(VCO_POWERUP_CH1 << ch->index);
  211. writel_relaxed(reg, ch->base + VCO_CTRL10);
  212. }
  213. static const u8 div_hdmi[] = { 1, 2, 4, 6 };
  214. static const u8 div_av1[] = { 1, 2, 5, 5 };
  215. static unsigned long
  216. berlin2_avpll_channel_recalc_rate(struct clk_hw *hw, unsigned long parent_rate)
  217. {
  218. struct berlin2_avpll_channel *ch = to_avpll_channel(hw);
  219. u32 reg, div_av2, div_av3, divider = 1;
  220. u64 freq = parent_rate;
  221. reg = readl_relaxed(ch->base + VCO_CTRL30);
  222. if ((reg & (VCO_DPLL_CH1_ENABLE << ch->index)) == 0)
  223. goto skip_div;
  224. /*
  225. * Fch = (Fref * sync2) /
  226. * (sync1 * div_hdmi * div_av1 * div_av2 * div_av3)
  227. */
  228. reg = readl_relaxed(ch->base + VCO_SYNC1n(ch->index));
  229. /* BG2/BG2CDs SYNC1 reg on AVPLL_B channel 1 is shifted by 4 */
  230. if (ch->flags & BERLIN2_AVPLL_BIT_QUIRK && ch->index == 0)
  231. reg >>= 4;
  232. divider = reg & VCO_SYNC1_MASK;
  233. reg = readl_relaxed(ch->base + VCO_SYNC2n(ch->index));
  234. freq *= reg & VCO_SYNC2_MASK;
  235. /* Channel 8 has no dividers */
  236. if (ch->index == 7)
  237. goto skip_div;
  238. /*
  239. * HDMI divider start at VCO_CTRL11, bit 7; MSB is enable, lower 2 bit
  240. * determine divider.
  241. */
  242. reg = readl_relaxed(ch->base + VCO_CTRL11) >> 7;
  243. reg = (reg >> (ch->index * 3));
  244. if (reg & BIT(2))
  245. divider *= div_hdmi[reg & 0x3];
  246. /*
  247. * AV1 divider start at VCO_CTRL11, bit 28; MSB is enable, lower 2 bit
  248. * determine divider.
  249. */
  250. if (ch->index == 0) {
  251. reg = readl_relaxed(ch->base + VCO_CTRL11);
  252. reg >>= 28;
  253. } else {
  254. reg = readl_relaxed(ch->base + VCO_CTRL12);
  255. reg >>= (ch->index-1) * 3;
  256. }
  257. if (reg & BIT(2))
  258. divider *= div_av1[reg & 0x3];
  259. /*
  260. * AV2 divider start at VCO_CTRL12, bit 18; each 7 bits wide,
  261. * zero is not a valid value.
  262. */
  263. if (ch->index < 2) {
  264. reg = readl_relaxed(ch->base + VCO_CTRL12);
  265. reg >>= 18 + (ch->index * 7);
  266. } else if (ch->index < 7) {
  267. reg = readl_relaxed(ch->base + VCO_CTRL13);
  268. reg >>= (ch->index - 2) * 7;
  269. } else {
  270. reg = readl_relaxed(ch->base + VCO_CTRL14);
  271. }
  272. div_av2 = reg & 0x7f;
  273. if (div_av2)
  274. divider *= div_av2;
  275. /*
  276. * AV3 divider start at VCO_CTRL14, bit 7; each 4 bits wide.
  277. * AV2/AV3 form a fractional divider, where only specfic values for AV3
  278. * are allowed. AV3 != 0 divides by AV2/2, AV3=0 is bypass.
  279. */
  280. if (ch->index < 6) {
  281. reg = readl_relaxed(ch->base + VCO_CTRL14);
  282. reg >>= 7 + (ch->index * 4);
  283. } else {
  284. reg = readl_relaxed(ch->base + VCO_CTRL15);
  285. }
  286. div_av3 = reg & 0xf;
  287. if (div_av2 && div_av3)
  288. freq *= 2;
  289. skip_div:
  290. do_div(freq, divider);
  291. return (unsigned long)freq;
  292. }
  293. static const struct clk_ops berlin2_avpll_channel_ops = {
  294. .is_enabled = berlin2_avpll_channel_is_enabled,
  295. .enable = berlin2_avpll_channel_enable,
  296. .disable = berlin2_avpll_channel_disable,
  297. .recalc_rate = berlin2_avpll_channel_recalc_rate,
  298. };
  299. /*
  300. * Another nice quirk:
  301. * On some production SoCs, AVPLL channels are scrambled with respect
  302. * to the channel numbering in the registers but still referenced by
  303. * their original channel numbers. We deal with it by having a flag
  304. * and a translation table for the index.
  305. */
  306. static const u8 quirk_index[] __initconst = { 0, 6, 5, 4, 3, 2, 1, 7 };
  307. int __init berlin2_avpll_channel_register(void __iomem *base,
  308. const char *name, u8 index, const char *parent_name,
  309. u8 ch_flags, unsigned long flags)
  310. {
  311. struct berlin2_avpll_channel *ch;
  312. struct clk_init_data init;
  313. ch = kzalloc(sizeof(*ch), GFP_KERNEL);
  314. if (!ch)
  315. return -ENOMEM;
  316. ch->base = base;
  317. if (ch_flags & BERLIN2_AVPLL_SCRAMBLE_QUIRK)
  318. ch->index = quirk_index[index];
  319. else
  320. ch->index = index;
  321. ch->flags = ch_flags;
  322. ch->hw.init = &init;
  323. init.name = name;
  324. init.ops = &berlin2_avpll_channel_ops;
  325. init.parent_names = &parent_name;
  326. init.num_parents = 1;
  327. init.flags = flags;
  328. return clk_hw_register(NULL, &ch->hw);
  329. }