berlin2-avpll.c 11 KB

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