clk-alpha-pll.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2015, 2018, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/kernel.h>
  6. #include <linux/export.h>
  7. #include <linux/clk-provider.h>
  8. #include <linux/regmap.h>
  9. #include <linux/delay.h>
  10. #include "clk-alpha-pll.h"
  11. #include "common.h"
  12. #define PLL_MODE(p) ((p)->offset + 0x0)
  13. # define PLL_OUTCTRL BIT(0)
  14. # define PLL_BYPASSNL BIT(1)
  15. # define PLL_RESET_N BIT(2)
  16. # define PLL_OFFLINE_REQ BIT(7)
  17. # define PLL_LOCK_COUNT_SHIFT 8
  18. # define PLL_LOCK_COUNT_MASK 0x3f
  19. # define PLL_BIAS_COUNT_SHIFT 14
  20. # define PLL_BIAS_COUNT_MASK 0x3f
  21. # define PLL_VOTE_FSM_ENA BIT(20)
  22. # define PLL_FSM_ENA BIT(20)
  23. # define PLL_VOTE_FSM_RESET BIT(21)
  24. # define PLL_UPDATE BIT(22)
  25. # define PLL_UPDATE_BYPASS BIT(23)
  26. # define PLL_OFFLINE_ACK BIT(28)
  27. # define ALPHA_PLL_ACK_LATCH BIT(29)
  28. # define PLL_ACTIVE_FLAG BIT(30)
  29. # define PLL_LOCK_DET BIT(31)
  30. #define PLL_L_VAL(p) ((p)->offset + (p)->regs[PLL_OFF_L_VAL])
  31. #define PLL_ALPHA_VAL(p) ((p)->offset + (p)->regs[PLL_OFF_ALPHA_VAL])
  32. #define PLL_ALPHA_VAL_U(p) ((p)->offset + (p)->regs[PLL_OFF_ALPHA_VAL_U])
  33. #define PLL_USER_CTL(p) ((p)->offset + (p)->regs[PLL_OFF_USER_CTL])
  34. # define PLL_POST_DIV_SHIFT 8
  35. # define PLL_POST_DIV_MASK(p) GENMASK((p)->width, 0)
  36. # define PLL_ALPHA_EN BIT(24)
  37. # define PLL_ALPHA_MODE BIT(25)
  38. # define PLL_VCO_SHIFT 20
  39. # define PLL_VCO_MASK 0x3
  40. #define PLL_USER_CTL_U(p) ((p)->offset + (p)->regs[PLL_OFF_USER_CTL_U])
  41. #define PLL_CONFIG_CTL(p) ((p)->offset + (p)->regs[PLL_OFF_CONFIG_CTL])
  42. #define PLL_CONFIG_CTL_U(p) ((p)->offset + (p)->regs[PLL_OFF_CONFIG_CTL_U])
  43. #define PLL_TEST_CTL(p) ((p)->offset + (p)->regs[PLL_OFF_TEST_CTL])
  44. #define PLL_TEST_CTL_U(p) ((p)->offset + (p)->regs[PLL_OFF_TEST_CTL_U])
  45. #define PLL_STATUS(p) ((p)->offset + (p)->regs[PLL_OFF_STATUS])
  46. #define PLL_OPMODE(p) ((p)->offset + (p)->regs[PLL_OFF_OPMODE])
  47. #define PLL_FRAC(p) ((p)->offset + (p)->regs[PLL_OFF_FRAC])
  48. const u8 clk_alpha_pll_regs[][PLL_OFF_MAX_REGS] = {
  49. [CLK_ALPHA_PLL_TYPE_DEFAULT] = {
  50. [PLL_OFF_L_VAL] = 0x04,
  51. [PLL_OFF_ALPHA_VAL] = 0x08,
  52. [PLL_OFF_ALPHA_VAL_U] = 0x0c,
  53. [PLL_OFF_USER_CTL] = 0x10,
  54. [PLL_OFF_USER_CTL_U] = 0x14,
  55. [PLL_OFF_CONFIG_CTL] = 0x18,
  56. [PLL_OFF_TEST_CTL] = 0x1c,
  57. [PLL_OFF_TEST_CTL_U] = 0x20,
  58. [PLL_OFF_STATUS] = 0x24,
  59. },
  60. [CLK_ALPHA_PLL_TYPE_HUAYRA] = {
  61. [PLL_OFF_L_VAL] = 0x04,
  62. [PLL_OFF_ALPHA_VAL] = 0x08,
  63. [PLL_OFF_USER_CTL] = 0x10,
  64. [PLL_OFF_CONFIG_CTL] = 0x14,
  65. [PLL_OFF_CONFIG_CTL_U] = 0x18,
  66. [PLL_OFF_TEST_CTL] = 0x1c,
  67. [PLL_OFF_TEST_CTL_U] = 0x20,
  68. [PLL_OFF_STATUS] = 0x24,
  69. },
  70. [CLK_ALPHA_PLL_TYPE_BRAMMO] = {
  71. [PLL_OFF_L_VAL] = 0x04,
  72. [PLL_OFF_ALPHA_VAL] = 0x08,
  73. [PLL_OFF_ALPHA_VAL_U] = 0x0c,
  74. [PLL_OFF_USER_CTL] = 0x10,
  75. [PLL_OFF_CONFIG_CTL] = 0x18,
  76. [PLL_OFF_TEST_CTL] = 0x1c,
  77. [PLL_OFF_STATUS] = 0x24,
  78. },
  79. [CLK_ALPHA_PLL_TYPE_FABIA] = {
  80. [PLL_OFF_L_VAL] = 0x04,
  81. [PLL_OFF_USER_CTL] = 0x0c,
  82. [PLL_OFF_USER_CTL_U] = 0x10,
  83. [PLL_OFF_CONFIG_CTL] = 0x14,
  84. [PLL_OFF_CONFIG_CTL_U] = 0x18,
  85. [PLL_OFF_TEST_CTL] = 0x1c,
  86. [PLL_OFF_TEST_CTL_U] = 0x20,
  87. [PLL_OFF_STATUS] = 0x24,
  88. [PLL_OFF_OPMODE] = 0x2c,
  89. [PLL_OFF_FRAC] = 0x38,
  90. },
  91. };
  92. EXPORT_SYMBOL_GPL(clk_alpha_pll_regs);
  93. /*
  94. * Even though 40 bits are present, use only 32 for ease of calculation.
  95. */
  96. #define ALPHA_REG_BITWIDTH 40
  97. #define ALPHA_REG_16BIT_WIDTH 16
  98. #define ALPHA_BITWIDTH 32U
  99. #define ALPHA_SHIFT(w) min(w, ALPHA_BITWIDTH)
  100. #define PLL_HUAYRA_M_WIDTH 8
  101. #define PLL_HUAYRA_M_SHIFT 8
  102. #define PLL_HUAYRA_M_MASK 0xff
  103. #define PLL_HUAYRA_N_SHIFT 0
  104. #define PLL_HUAYRA_N_MASK 0xff
  105. #define PLL_HUAYRA_ALPHA_WIDTH 16
  106. #define FABIA_OPMODE_STANDBY 0x0
  107. #define FABIA_OPMODE_RUN 0x1
  108. #define FABIA_PLL_OUT_MASK 0x7
  109. #define FABIA_PLL_RATE_MARGIN 500
  110. #define pll_alpha_width(p) \
  111. ((PLL_ALPHA_VAL_U(p) - PLL_ALPHA_VAL(p) == 4) ? \
  112. ALPHA_REG_BITWIDTH : ALPHA_REG_16BIT_WIDTH)
  113. #define pll_has_64bit_config(p) ((PLL_CONFIG_CTL_U(p) - PLL_CONFIG_CTL(p)) == 4)
  114. #define to_clk_alpha_pll(_hw) container_of(to_clk_regmap(_hw), \
  115. struct clk_alpha_pll, clkr)
  116. #define to_clk_alpha_pll_postdiv(_hw) container_of(to_clk_regmap(_hw), \
  117. struct clk_alpha_pll_postdiv, clkr)
  118. static int wait_for_pll(struct clk_alpha_pll *pll, u32 mask, bool inverse,
  119. const char *action)
  120. {
  121. u32 val;
  122. int count;
  123. int ret;
  124. const char *name = clk_hw_get_name(&pll->clkr.hw);
  125. ret = regmap_read(pll->clkr.regmap, PLL_MODE(pll), &val);
  126. if (ret)
  127. return ret;
  128. for (count = 100; count > 0; count--) {
  129. ret = regmap_read(pll->clkr.regmap, PLL_MODE(pll), &val);
  130. if (ret)
  131. return ret;
  132. if (inverse && !(val & mask))
  133. return 0;
  134. else if ((val & mask) == mask)
  135. return 0;
  136. udelay(1);
  137. }
  138. WARN(1, "%s failed to %s!\n", name, action);
  139. return -ETIMEDOUT;
  140. }
  141. #define wait_for_pll_enable_active(pll) \
  142. wait_for_pll(pll, PLL_ACTIVE_FLAG, 0, "enable")
  143. #define wait_for_pll_enable_lock(pll) \
  144. wait_for_pll(pll, PLL_LOCK_DET, 0, "enable")
  145. #define wait_for_pll_disable(pll) \
  146. wait_for_pll(pll, PLL_ACTIVE_FLAG, 1, "disable")
  147. #define wait_for_pll_offline(pll) \
  148. wait_for_pll(pll, PLL_OFFLINE_ACK, 0, "offline")
  149. #define wait_for_pll_update(pll) \
  150. wait_for_pll(pll, PLL_UPDATE, 1, "update")
  151. #define wait_for_pll_update_ack_set(pll) \
  152. wait_for_pll(pll, ALPHA_PLL_ACK_LATCH, 0, "update_ack_set")
  153. #define wait_for_pll_update_ack_clear(pll) \
  154. wait_for_pll(pll, ALPHA_PLL_ACK_LATCH, 1, "update_ack_clear")
  155. void clk_alpha_pll_configure(struct clk_alpha_pll *pll, struct regmap *regmap,
  156. const struct alpha_pll_config *config)
  157. {
  158. u32 val, mask;
  159. regmap_write(regmap, PLL_L_VAL(pll), config->l);
  160. regmap_write(regmap, PLL_ALPHA_VAL(pll), config->alpha);
  161. regmap_write(regmap, PLL_CONFIG_CTL(pll), config->config_ctl_val);
  162. if (pll_has_64bit_config(pll))
  163. regmap_write(regmap, PLL_CONFIG_CTL_U(pll),
  164. config->config_ctl_hi_val);
  165. if (pll_alpha_width(pll) > 32)
  166. regmap_write(regmap, PLL_ALPHA_VAL_U(pll), config->alpha_hi);
  167. val = config->main_output_mask;
  168. val |= config->aux_output_mask;
  169. val |= config->aux2_output_mask;
  170. val |= config->early_output_mask;
  171. val |= config->pre_div_val;
  172. val |= config->post_div_val;
  173. val |= config->vco_val;
  174. val |= config->alpha_en_mask;
  175. val |= config->alpha_mode_mask;
  176. mask = config->main_output_mask;
  177. mask |= config->aux_output_mask;
  178. mask |= config->aux2_output_mask;
  179. mask |= config->early_output_mask;
  180. mask |= config->pre_div_mask;
  181. mask |= config->post_div_mask;
  182. mask |= config->vco_mask;
  183. regmap_update_bits(regmap, PLL_USER_CTL(pll), mask, val);
  184. if (pll->flags & SUPPORTS_FSM_MODE)
  185. qcom_pll_set_fsm_mode(regmap, PLL_MODE(pll), 6, 0);
  186. }
  187. static int clk_alpha_pll_hwfsm_enable(struct clk_hw *hw)
  188. {
  189. int ret;
  190. struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
  191. u32 val;
  192. ret = regmap_read(pll->clkr.regmap, PLL_MODE(pll), &val);
  193. if (ret)
  194. return ret;
  195. val |= PLL_FSM_ENA;
  196. if (pll->flags & SUPPORTS_OFFLINE_REQ)
  197. val &= ~PLL_OFFLINE_REQ;
  198. ret = regmap_write(pll->clkr.regmap, PLL_MODE(pll), val);
  199. if (ret)
  200. return ret;
  201. /* Make sure enable request goes through before waiting for update */
  202. mb();
  203. return wait_for_pll_enable_active(pll);
  204. }
  205. static void clk_alpha_pll_hwfsm_disable(struct clk_hw *hw)
  206. {
  207. int ret;
  208. struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
  209. u32 val;
  210. ret = regmap_read(pll->clkr.regmap, PLL_MODE(pll), &val);
  211. if (ret)
  212. return;
  213. if (pll->flags & SUPPORTS_OFFLINE_REQ) {
  214. ret = regmap_update_bits(pll->clkr.regmap, PLL_MODE(pll),
  215. PLL_OFFLINE_REQ, PLL_OFFLINE_REQ);
  216. if (ret)
  217. return;
  218. ret = wait_for_pll_offline(pll);
  219. if (ret)
  220. return;
  221. }
  222. /* Disable hwfsm */
  223. ret = regmap_update_bits(pll->clkr.regmap, PLL_MODE(pll),
  224. PLL_FSM_ENA, 0);
  225. if (ret)
  226. return;
  227. wait_for_pll_disable(pll);
  228. }
  229. static int pll_is_enabled(struct clk_hw *hw, u32 mask)
  230. {
  231. int ret;
  232. struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
  233. u32 val;
  234. ret = regmap_read(pll->clkr.regmap, PLL_MODE(pll), &val);
  235. if (ret)
  236. return ret;
  237. return !!(val & mask);
  238. }
  239. static int clk_alpha_pll_hwfsm_is_enabled(struct clk_hw *hw)
  240. {
  241. return pll_is_enabled(hw, PLL_ACTIVE_FLAG);
  242. }
  243. static int clk_alpha_pll_is_enabled(struct clk_hw *hw)
  244. {
  245. return pll_is_enabled(hw, PLL_LOCK_DET);
  246. }
  247. static int clk_alpha_pll_enable(struct clk_hw *hw)
  248. {
  249. int ret;
  250. struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
  251. u32 val, mask;
  252. mask = PLL_OUTCTRL | PLL_RESET_N | PLL_BYPASSNL;
  253. ret = regmap_read(pll->clkr.regmap, PLL_MODE(pll), &val);
  254. if (ret)
  255. return ret;
  256. /* If in FSM mode, just vote for it */
  257. if (val & PLL_VOTE_FSM_ENA) {
  258. ret = clk_enable_regmap(hw);
  259. if (ret)
  260. return ret;
  261. return wait_for_pll_enable_active(pll);
  262. }
  263. /* Skip if already enabled */
  264. if ((val & mask) == mask)
  265. return 0;
  266. ret = regmap_update_bits(pll->clkr.regmap, PLL_MODE(pll),
  267. PLL_BYPASSNL, PLL_BYPASSNL);
  268. if (ret)
  269. return ret;
  270. /*
  271. * H/W requires a 5us delay between disabling the bypass and
  272. * de-asserting the reset.
  273. */
  274. mb();
  275. udelay(5);
  276. ret = regmap_update_bits(pll->clkr.regmap, PLL_MODE(pll),
  277. PLL_RESET_N, PLL_RESET_N);
  278. if (ret)
  279. return ret;
  280. ret = wait_for_pll_enable_lock(pll);
  281. if (ret)
  282. return ret;
  283. ret = regmap_update_bits(pll->clkr.regmap, PLL_MODE(pll),
  284. PLL_OUTCTRL, PLL_OUTCTRL);
  285. /* Ensure that the write above goes through before returning. */
  286. mb();
  287. return ret;
  288. }
  289. static void clk_alpha_pll_disable(struct clk_hw *hw)
  290. {
  291. int ret;
  292. struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
  293. u32 val, mask;
  294. ret = regmap_read(pll->clkr.regmap, PLL_MODE(pll), &val);
  295. if (ret)
  296. return;
  297. /* If in FSM mode, just unvote it */
  298. if (val & PLL_VOTE_FSM_ENA) {
  299. clk_disable_regmap(hw);
  300. return;
  301. }
  302. mask = PLL_OUTCTRL;
  303. regmap_update_bits(pll->clkr.regmap, PLL_MODE(pll), mask, 0);
  304. /* Delay of 2 output clock ticks required until output is disabled */
  305. mb();
  306. udelay(1);
  307. mask = PLL_RESET_N | PLL_BYPASSNL;
  308. regmap_update_bits(pll->clkr.regmap, PLL_MODE(pll), mask, 0);
  309. }
  310. static unsigned long
  311. alpha_pll_calc_rate(u64 prate, u32 l, u32 a, u32 alpha_width)
  312. {
  313. return (prate * l) + ((prate * a) >> ALPHA_SHIFT(alpha_width));
  314. }
  315. static unsigned long
  316. alpha_pll_round_rate(unsigned long rate, unsigned long prate, u32 *l, u64 *a,
  317. u32 alpha_width)
  318. {
  319. u64 remainder;
  320. u64 quotient;
  321. quotient = rate;
  322. remainder = do_div(quotient, prate);
  323. *l = quotient;
  324. if (!remainder) {
  325. *a = 0;
  326. return rate;
  327. }
  328. /* Upper ALPHA_BITWIDTH bits of Alpha */
  329. quotient = remainder << ALPHA_SHIFT(alpha_width);
  330. remainder = do_div(quotient, prate);
  331. if (remainder)
  332. quotient++;
  333. *a = quotient;
  334. return alpha_pll_calc_rate(prate, *l, *a, alpha_width);
  335. }
  336. static const struct pll_vco *
  337. alpha_pll_find_vco(const struct clk_alpha_pll *pll, unsigned long rate)
  338. {
  339. const struct pll_vco *v = pll->vco_table;
  340. const struct pll_vco *end = v + pll->num_vco;
  341. for (; v < end; v++)
  342. if (rate >= v->min_freq && rate <= v->max_freq)
  343. return v;
  344. return NULL;
  345. }
  346. static unsigned long
  347. clk_alpha_pll_recalc_rate(struct clk_hw *hw, unsigned long parent_rate)
  348. {
  349. u32 l, low, high, ctl;
  350. u64 a = 0, prate = parent_rate;
  351. struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
  352. u32 alpha_width = pll_alpha_width(pll);
  353. regmap_read(pll->clkr.regmap, PLL_L_VAL(pll), &l);
  354. regmap_read(pll->clkr.regmap, PLL_USER_CTL(pll), &ctl);
  355. if (ctl & PLL_ALPHA_EN) {
  356. regmap_read(pll->clkr.regmap, PLL_ALPHA_VAL(pll), &low);
  357. if (alpha_width > 32) {
  358. regmap_read(pll->clkr.regmap, PLL_ALPHA_VAL_U(pll),
  359. &high);
  360. a = (u64)high << 32 | low;
  361. } else {
  362. a = low & GENMASK(alpha_width - 1, 0);
  363. }
  364. if (alpha_width > ALPHA_BITWIDTH)
  365. a >>= alpha_width - ALPHA_BITWIDTH;
  366. }
  367. return alpha_pll_calc_rate(prate, l, a, alpha_width);
  368. }
  369. static int __clk_alpha_pll_update_latch(struct clk_alpha_pll *pll)
  370. {
  371. int ret;
  372. u32 mode;
  373. regmap_read(pll->clkr.regmap, PLL_MODE(pll), &mode);
  374. /* Latch the input to the PLL */
  375. regmap_update_bits(pll->clkr.regmap, PLL_MODE(pll), PLL_UPDATE,
  376. PLL_UPDATE);
  377. /* Wait for 2 reference cycle before checking ACK bit */
  378. udelay(1);
  379. /*
  380. * PLL will latch the new L, Alpha and freq control word.
  381. * PLL will respond by raising PLL_ACK_LATCH output when new programming
  382. * has been latched in and PLL is being updated. When
  383. * UPDATE_LOGIC_BYPASS bit is not set, PLL_UPDATE will be cleared
  384. * automatically by hardware when PLL_ACK_LATCH is asserted by PLL.
  385. */
  386. if (mode & PLL_UPDATE_BYPASS) {
  387. ret = wait_for_pll_update_ack_set(pll);
  388. if (ret)
  389. return ret;
  390. regmap_update_bits(pll->clkr.regmap, PLL_MODE(pll), PLL_UPDATE, 0);
  391. } else {
  392. ret = wait_for_pll_update(pll);
  393. if (ret)
  394. return ret;
  395. }
  396. ret = wait_for_pll_update_ack_clear(pll);
  397. if (ret)
  398. return ret;
  399. /* Wait for PLL output to stabilize */
  400. udelay(10);
  401. return 0;
  402. }
  403. static int clk_alpha_pll_update_latch(struct clk_alpha_pll *pll,
  404. int (*is_enabled)(struct clk_hw *))
  405. {
  406. if (!is_enabled(&pll->clkr.hw) ||
  407. !(pll->flags & SUPPORTS_DYNAMIC_UPDATE))
  408. return 0;
  409. return __clk_alpha_pll_update_latch(pll);
  410. }
  411. static int __clk_alpha_pll_set_rate(struct clk_hw *hw, unsigned long rate,
  412. unsigned long prate,
  413. int (*is_enabled)(struct clk_hw *))
  414. {
  415. struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
  416. const struct pll_vco *vco;
  417. u32 l, alpha_width = pll_alpha_width(pll);
  418. u64 a;
  419. rate = alpha_pll_round_rate(rate, prate, &l, &a, alpha_width);
  420. vco = alpha_pll_find_vco(pll, rate);
  421. if (pll->vco_table && !vco) {
  422. pr_err("alpha pll not in a valid vco range\n");
  423. return -EINVAL;
  424. }
  425. regmap_write(pll->clkr.regmap, PLL_L_VAL(pll), l);
  426. if (alpha_width > ALPHA_BITWIDTH)
  427. a <<= alpha_width - ALPHA_BITWIDTH;
  428. if (alpha_width > 32)
  429. regmap_write(pll->clkr.regmap, PLL_ALPHA_VAL_U(pll), a >> 32);
  430. regmap_write(pll->clkr.regmap, PLL_ALPHA_VAL(pll), a);
  431. if (vco) {
  432. regmap_update_bits(pll->clkr.regmap, PLL_USER_CTL(pll),
  433. PLL_VCO_MASK << PLL_VCO_SHIFT,
  434. vco->val << PLL_VCO_SHIFT);
  435. }
  436. regmap_update_bits(pll->clkr.regmap, PLL_USER_CTL(pll),
  437. PLL_ALPHA_EN, PLL_ALPHA_EN);
  438. return clk_alpha_pll_update_latch(pll, is_enabled);
  439. }
  440. static int clk_alpha_pll_set_rate(struct clk_hw *hw, unsigned long rate,
  441. unsigned long prate)
  442. {
  443. return __clk_alpha_pll_set_rate(hw, rate, prate,
  444. clk_alpha_pll_is_enabled);
  445. }
  446. static int clk_alpha_pll_hwfsm_set_rate(struct clk_hw *hw, unsigned long rate,
  447. unsigned long prate)
  448. {
  449. return __clk_alpha_pll_set_rate(hw, rate, prate,
  450. clk_alpha_pll_hwfsm_is_enabled);
  451. }
  452. static long clk_alpha_pll_round_rate(struct clk_hw *hw, unsigned long rate,
  453. unsigned long *prate)
  454. {
  455. struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
  456. u32 l, alpha_width = pll_alpha_width(pll);
  457. u64 a;
  458. unsigned long min_freq, max_freq;
  459. rate = alpha_pll_round_rate(rate, *prate, &l, &a, alpha_width);
  460. if (!pll->vco_table || alpha_pll_find_vco(pll, rate))
  461. return rate;
  462. min_freq = pll->vco_table[0].min_freq;
  463. max_freq = pll->vco_table[pll->num_vco - 1].max_freq;
  464. return clamp(rate, min_freq, max_freq);
  465. }
  466. static unsigned long
  467. alpha_huayra_pll_calc_rate(u64 prate, u32 l, u32 a)
  468. {
  469. /*
  470. * a contains 16 bit alpha_val in two’s compliment number in the range
  471. * of [-0.5, 0.5).
  472. */
  473. if (a >= BIT(PLL_HUAYRA_ALPHA_WIDTH - 1))
  474. l -= 1;
  475. return (prate * l) + (prate * a >> PLL_HUAYRA_ALPHA_WIDTH);
  476. }
  477. static unsigned long
  478. alpha_huayra_pll_round_rate(unsigned long rate, unsigned long prate,
  479. u32 *l, u32 *a)
  480. {
  481. u64 remainder;
  482. u64 quotient;
  483. quotient = rate;
  484. remainder = do_div(quotient, prate);
  485. *l = quotient;
  486. if (!remainder) {
  487. *a = 0;
  488. return rate;
  489. }
  490. quotient = remainder << PLL_HUAYRA_ALPHA_WIDTH;
  491. remainder = do_div(quotient, prate);
  492. if (remainder)
  493. quotient++;
  494. /*
  495. * alpha_val should be in two’s compliment number in the range
  496. * of [-0.5, 0.5) so if quotient >= 0.5 then increment the l value
  497. * since alpha value will be subtracted in this case.
  498. */
  499. if (quotient >= BIT(PLL_HUAYRA_ALPHA_WIDTH - 1))
  500. *l += 1;
  501. *a = quotient;
  502. return alpha_huayra_pll_calc_rate(prate, *l, *a);
  503. }
  504. static unsigned long
  505. alpha_pll_huayra_recalc_rate(struct clk_hw *hw, unsigned long parent_rate)
  506. {
  507. u64 rate = parent_rate, tmp;
  508. struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
  509. u32 l, alpha = 0, ctl, alpha_m, alpha_n;
  510. regmap_read(pll->clkr.regmap, PLL_L_VAL(pll), &l);
  511. regmap_read(pll->clkr.regmap, PLL_USER_CTL(pll), &ctl);
  512. if (ctl & PLL_ALPHA_EN) {
  513. regmap_read(pll->clkr.regmap, PLL_ALPHA_VAL(pll), &alpha);
  514. /*
  515. * Depending upon alpha_mode, it can be treated as M/N value or
  516. * as a two’s compliment number. When alpha_mode=1,
  517. * pll_alpha_val<15:8>=M and pll_apla_val<7:0>=N
  518. *
  519. * Fout=FIN*(L+(M/N))
  520. *
  521. * M is a signed number (-128 to 127) and N is unsigned
  522. * (0 to 255). M/N has to be within +/-0.5.
  523. *
  524. * When alpha_mode=0, it is a two’s compliment number in the
  525. * range [-0.5, 0.5).
  526. *
  527. * Fout=FIN*(L+(alpha_val)/2^16)
  528. *
  529. * where alpha_val is two’s compliment number.
  530. */
  531. if (!(ctl & PLL_ALPHA_MODE))
  532. return alpha_huayra_pll_calc_rate(rate, l, alpha);
  533. alpha_m = alpha >> PLL_HUAYRA_M_SHIFT & PLL_HUAYRA_M_MASK;
  534. alpha_n = alpha >> PLL_HUAYRA_N_SHIFT & PLL_HUAYRA_N_MASK;
  535. rate *= l;
  536. tmp = parent_rate;
  537. if (alpha_m >= BIT(PLL_HUAYRA_M_WIDTH - 1)) {
  538. alpha_m = BIT(PLL_HUAYRA_M_WIDTH) - alpha_m;
  539. tmp *= alpha_m;
  540. do_div(tmp, alpha_n);
  541. rate -= tmp;
  542. } else {
  543. tmp *= alpha_m;
  544. do_div(tmp, alpha_n);
  545. rate += tmp;
  546. }
  547. return rate;
  548. }
  549. return alpha_huayra_pll_calc_rate(rate, l, alpha);
  550. }
  551. static int alpha_pll_huayra_set_rate(struct clk_hw *hw, unsigned long rate,
  552. unsigned long prate)
  553. {
  554. struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
  555. u32 l, a, ctl, cur_alpha = 0;
  556. rate = alpha_huayra_pll_round_rate(rate, prate, &l, &a);
  557. regmap_read(pll->clkr.regmap, PLL_USER_CTL(pll), &ctl);
  558. if (ctl & PLL_ALPHA_EN)
  559. regmap_read(pll->clkr.regmap, PLL_ALPHA_VAL(pll), &cur_alpha);
  560. /*
  561. * Huayra PLL supports PLL dynamic programming. User can change L_VAL,
  562. * without having to go through the power on sequence.
  563. */
  564. if (clk_alpha_pll_is_enabled(hw)) {
  565. if (cur_alpha != a) {
  566. pr_err("clock needs to be gated %s\n",
  567. clk_hw_get_name(hw));
  568. return -EBUSY;
  569. }
  570. regmap_write(pll->clkr.regmap, PLL_L_VAL(pll), l);
  571. /* Ensure that the write above goes to detect L val change. */
  572. mb();
  573. return wait_for_pll_enable_lock(pll);
  574. }
  575. regmap_write(pll->clkr.regmap, PLL_L_VAL(pll), l);
  576. regmap_write(pll->clkr.regmap, PLL_ALPHA_VAL(pll), a);
  577. if (a == 0)
  578. regmap_update_bits(pll->clkr.regmap, PLL_USER_CTL(pll),
  579. PLL_ALPHA_EN, 0x0);
  580. else
  581. regmap_update_bits(pll->clkr.regmap, PLL_USER_CTL(pll),
  582. PLL_ALPHA_EN | PLL_ALPHA_MODE, PLL_ALPHA_EN);
  583. return 0;
  584. }
  585. static long alpha_pll_huayra_round_rate(struct clk_hw *hw, unsigned long rate,
  586. unsigned long *prate)
  587. {
  588. u32 l, a;
  589. return alpha_huayra_pll_round_rate(rate, *prate, &l, &a);
  590. }
  591. const struct clk_ops clk_alpha_pll_ops = {
  592. .enable = clk_alpha_pll_enable,
  593. .disable = clk_alpha_pll_disable,
  594. .is_enabled = clk_alpha_pll_is_enabled,
  595. .recalc_rate = clk_alpha_pll_recalc_rate,
  596. .round_rate = clk_alpha_pll_round_rate,
  597. .set_rate = clk_alpha_pll_set_rate,
  598. };
  599. EXPORT_SYMBOL_GPL(clk_alpha_pll_ops);
  600. const struct clk_ops clk_alpha_pll_huayra_ops = {
  601. .enable = clk_alpha_pll_enable,
  602. .disable = clk_alpha_pll_disable,
  603. .is_enabled = clk_alpha_pll_is_enabled,
  604. .recalc_rate = alpha_pll_huayra_recalc_rate,
  605. .round_rate = alpha_pll_huayra_round_rate,
  606. .set_rate = alpha_pll_huayra_set_rate,
  607. };
  608. EXPORT_SYMBOL_GPL(clk_alpha_pll_huayra_ops);
  609. const struct clk_ops clk_alpha_pll_hwfsm_ops = {
  610. .enable = clk_alpha_pll_hwfsm_enable,
  611. .disable = clk_alpha_pll_hwfsm_disable,
  612. .is_enabled = clk_alpha_pll_hwfsm_is_enabled,
  613. .recalc_rate = clk_alpha_pll_recalc_rate,
  614. .round_rate = clk_alpha_pll_round_rate,
  615. .set_rate = clk_alpha_pll_hwfsm_set_rate,
  616. };
  617. EXPORT_SYMBOL_GPL(clk_alpha_pll_hwfsm_ops);
  618. static unsigned long
  619. clk_alpha_pll_postdiv_recalc_rate(struct clk_hw *hw, unsigned long parent_rate)
  620. {
  621. struct clk_alpha_pll_postdiv *pll = to_clk_alpha_pll_postdiv(hw);
  622. u32 ctl;
  623. regmap_read(pll->clkr.regmap, PLL_USER_CTL(pll), &ctl);
  624. ctl >>= PLL_POST_DIV_SHIFT;
  625. ctl &= PLL_POST_DIV_MASK(pll);
  626. return parent_rate >> fls(ctl);
  627. }
  628. static const struct clk_div_table clk_alpha_div_table[] = {
  629. { 0x0, 1 },
  630. { 0x1, 2 },
  631. { 0x3, 4 },
  632. { 0x7, 8 },
  633. { 0xf, 16 },
  634. { }
  635. };
  636. static const struct clk_div_table clk_alpha_2bit_div_table[] = {
  637. { 0x0, 1 },
  638. { 0x1, 2 },
  639. { 0x3, 4 },
  640. { }
  641. };
  642. static long
  643. clk_alpha_pll_postdiv_round_rate(struct clk_hw *hw, unsigned long rate,
  644. unsigned long *prate)
  645. {
  646. struct clk_alpha_pll_postdiv *pll = to_clk_alpha_pll_postdiv(hw);
  647. const struct clk_div_table *table;
  648. if (pll->width == 2)
  649. table = clk_alpha_2bit_div_table;
  650. else
  651. table = clk_alpha_div_table;
  652. return divider_round_rate(hw, rate, prate, table,
  653. pll->width, CLK_DIVIDER_POWER_OF_TWO);
  654. }
  655. static long
  656. clk_alpha_pll_postdiv_round_ro_rate(struct clk_hw *hw, unsigned long rate,
  657. unsigned long *prate)
  658. {
  659. struct clk_alpha_pll_postdiv *pll = to_clk_alpha_pll_postdiv(hw);
  660. u32 ctl, div;
  661. regmap_read(pll->clkr.regmap, PLL_USER_CTL(pll), &ctl);
  662. ctl >>= PLL_POST_DIV_SHIFT;
  663. ctl &= BIT(pll->width) - 1;
  664. div = 1 << fls(ctl);
  665. if (clk_hw_get_flags(hw) & CLK_SET_RATE_PARENT)
  666. *prate = clk_hw_round_rate(clk_hw_get_parent(hw), div * rate);
  667. return DIV_ROUND_UP_ULL((u64)*prate, div);
  668. }
  669. static int clk_alpha_pll_postdiv_set_rate(struct clk_hw *hw, unsigned long rate,
  670. unsigned long parent_rate)
  671. {
  672. struct clk_alpha_pll_postdiv *pll = to_clk_alpha_pll_postdiv(hw);
  673. int div;
  674. /* 16 -> 0xf, 8 -> 0x7, 4 -> 0x3, 2 -> 0x1, 1 -> 0x0 */
  675. div = DIV_ROUND_UP_ULL((u64)parent_rate, rate) - 1;
  676. return regmap_update_bits(pll->clkr.regmap, PLL_USER_CTL(pll),
  677. PLL_POST_DIV_MASK(pll) << PLL_POST_DIV_SHIFT,
  678. div << PLL_POST_DIV_SHIFT);
  679. }
  680. const struct clk_ops clk_alpha_pll_postdiv_ops = {
  681. .recalc_rate = clk_alpha_pll_postdiv_recalc_rate,
  682. .round_rate = clk_alpha_pll_postdiv_round_rate,
  683. .set_rate = clk_alpha_pll_postdiv_set_rate,
  684. };
  685. EXPORT_SYMBOL_GPL(clk_alpha_pll_postdiv_ops);
  686. const struct clk_ops clk_alpha_pll_postdiv_ro_ops = {
  687. .round_rate = clk_alpha_pll_postdiv_round_ro_rate,
  688. .recalc_rate = clk_alpha_pll_postdiv_recalc_rate,
  689. };
  690. EXPORT_SYMBOL_GPL(clk_alpha_pll_postdiv_ro_ops);
  691. void clk_fabia_pll_configure(struct clk_alpha_pll *pll, struct regmap *regmap,
  692. const struct alpha_pll_config *config)
  693. {
  694. u32 val, mask;
  695. if (config->l)
  696. regmap_write(regmap, PLL_L_VAL(pll), config->l);
  697. if (config->alpha)
  698. regmap_write(regmap, PLL_FRAC(pll), config->alpha);
  699. if (config->config_ctl_val)
  700. regmap_write(regmap, PLL_CONFIG_CTL(pll),
  701. config->config_ctl_val);
  702. if (config->post_div_mask) {
  703. mask = config->post_div_mask;
  704. val = config->post_div_val;
  705. regmap_update_bits(regmap, PLL_USER_CTL(pll), mask, val);
  706. }
  707. regmap_update_bits(regmap, PLL_MODE(pll), PLL_UPDATE_BYPASS,
  708. PLL_UPDATE_BYPASS);
  709. regmap_update_bits(regmap, PLL_MODE(pll), PLL_RESET_N, PLL_RESET_N);
  710. }
  711. EXPORT_SYMBOL_GPL(clk_fabia_pll_configure);
  712. static int alpha_pll_fabia_enable(struct clk_hw *hw)
  713. {
  714. int ret;
  715. struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
  716. u32 val, opmode_val;
  717. struct regmap *regmap = pll->clkr.regmap;
  718. ret = regmap_read(regmap, PLL_MODE(pll), &val);
  719. if (ret)
  720. return ret;
  721. /* If in FSM mode, just vote for it */
  722. if (val & PLL_VOTE_FSM_ENA) {
  723. ret = clk_enable_regmap(hw);
  724. if (ret)
  725. return ret;
  726. return wait_for_pll_enable_active(pll);
  727. }
  728. ret = regmap_read(regmap, PLL_OPMODE(pll), &opmode_val);
  729. if (ret)
  730. return ret;
  731. /* Skip If PLL is already running */
  732. if ((opmode_val & FABIA_OPMODE_RUN) && (val & PLL_OUTCTRL))
  733. return 0;
  734. ret = regmap_update_bits(regmap, PLL_MODE(pll), PLL_OUTCTRL, 0);
  735. if (ret)
  736. return ret;
  737. ret = regmap_write(regmap, PLL_OPMODE(pll), FABIA_OPMODE_STANDBY);
  738. if (ret)
  739. return ret;
  740. ret = regmap_update_bits(regmap, PLL_MODE(pll), PLL_RESET_N,
  741. PLL_RESET_N);
  742. if (ret)
  743. return ret;
  744. ret = regmap_write(regmap, PLL_OPMODE(pll), FABIA_OPMODE_RUN);
  745. if (ret)
  746. return ret;
  747. ret = wait_for_pll_enable_lock(pll);
  748. if (ret)
  749. return ret;
  750. ret = regmap_update_bits(regmap, PLL_USER_CTL(pll),
  751. FABIA_PLL_OUT_MASK, FABIA_PLL_OUT_MASK);
  752. if (ret)
  753. return ret;
  754. return regmap_update_bits(regmap, PLL_MODE(pll), PLL_OUTCTRL,
  755. PLL_OUTCTRL);
  756. }
  757. static void alpha_pll_fabia_disable(struct clk_hw *hw)
  758. {
  759. int ret;
  760. struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
  761. u32 val;
  762. struct regmap *regmap = pll->clkr.regmap;
  763. ret = regmap_read(regmap, PLL_MODE(pll), &val);
  764. if (ret)
  765. return;
  766. /* If in FSM mode, just unvote it */
  767. if (val & PLL_FSM_ENA) {
  768. clk_disable_regmap(hw);
  769. return;
  770. }
  771. ret = regmap_update_bits(regmap, PLL_MODE(pll), PLL_OUTCTRL, 0);
  772. if (ret)
  773. return;
  774. /* Disable main outputs */
  775. ret = regmap_update_bits(regmap, PLL_USER_CTL(pll), FABIA_PLL_OUT_MASK,
  776. 0);
  777. if (ret)
  778. return;
  779. /* Place the PLL in STANDBY */
  780. regmap_write(regmap, PLL_OPMODE(pll), FABIA_OPMODE_STANDBY);
  781. }
  782. static unsigned long alpha_pll_fabia_recalc_rate(struct clk_hw *hw,
  783. unsigned long parent_rate)
  784. {
  785. struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
  786. u32 l, frac, alpha_width = pll_alpha_width(pll);
  787. regmap_read(pll->clkr.regmap, PLL_L_VAL(pll), &l);
  788. regmap_read(pll->clkr.regmap, PLL_FRAC(pll), &frac);
  789. return alpha_pll_calc_rate(parent_rate, l, frac, alpha_width);
  790. }
  791. static int alpha_pll_fabia_set_rate(struct clk_hw *hw, unsigned long rate,
  792. unsigned long prate)
  793. {
  794. struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
  795. u32 val, l, alpha_width = pll_alpha_width(pll);
  796. u64 a;
  797. unsigned long rrate;
  798. int ret = 0;
  799. ret = regmap_read(pll->clkr.regmap, PLL_MODE(pll), &val);
  800. if (ret)
  801. return ret;
  802. rrate = alpha_pll_round_rate(rate, prate, &l, &a, alpha_width);
  803. /*
  804. * Due to limited number of bits for fractional rate programming, the
  805. * rounded up rate could be marginally higher than the requested rate.
  806. */
  807. if (rrate > (rate + FABIA_PLL_RATE_MARGIN) || rrate < rate) {
  808. pr_err("Call set rate on the PLL with rounded rates!\n");
  809. return -EINVAL;
  810. }
  811. regmap_write(pll->clkr.regmap, PLL_L_VAL(pll), l);
  812. regmap_write(pll->clkr.regmap, PLL_FRAC(pll), a);
  813. return __clk_alpha_pll_update_latch(pll);
  814. }
  815. const struct clk_ops clk_alpha_pll_fabia_ops = {
  816. .enable = alpha_pll_fabia_enable,
  817. .disable = alpha_pll_fabia_disable,
  818. .is_enabled = clk_alpha_pll_is_enabled,
  819. .set_rate = alpha_pll_fabia_set_rate,
  820. .recalc_rate = alpha_pll_fabia_recalc_rate,
  821. .round_rate = clk_alpha_pll_round_rate,
  822. };
  823. EXPORT_SYMBOL_GPL(clk_alpha_pll_fabia_ops);
  824. const struct clk_ops clk_alpha_pll_fixed_fabia_ops = {
  825. .enable = alpha_pll_fabia_enable,
  826. .disable = alpha_pll_fabia_disable,
  827. .is_enabled = clk_alpha_pll_is_enabled,
  828. .recalc_rate = alpha_pll_fabia_recalc_rate,
  829. .round_rate = clk_alpha_pll_round_rate,
  830. };
  831. EXPORT_SYMBOL_GPL(clk_alpha_pll_fixed_fabia_ops);
  832. static unsigned long clk_alpha_pll_postdiv_fabia_recalc_rate(struct clk_hw *hw,
  833. unsigned long parent_rate)
  834. {
  835. struct clk_alpha_pll_postdiv *pll = to_clk_alpha_pll_postdiv(hw);
  836. u32 i, div = 1, val;
  837. int ret;
  838. if (!pll->post_div_table) {
  839. pr_err("Missing the post_div_table for the PLL\n");
  840. return -EINVAL;
  841. }
  842. ret = regmap_read(pll->clkr.regmap, PLL_USER_CTL(pll), &val);
  843. if (ret)
  844. return ret;
  845. val >>= pll->post_div_shift;
  846. val &= BIT(pll->width) - 1;
  847. for (i = 0; i < pll->num_post_div; i++) {
  848. if (pll->post_div_table[i].val == val) {
  849. div = pll->post_div_table[i].div;
  850. break;
  851. }
  852. }
  853. return (parent_rate / div);
  854. }
  855. static long clk_alpha_pll_postdiv_fabia_round_rate(struct clk_hw *hw,
  856. unsigned long rate, unsigned long *prate)
  857. {
  858. struct clk_alpha_pll_postdiv *pll = to_clk_alpha_pll_postdiv(hw);
  859. if (!pll->post_div_table) {
  860. pr_err("Missing the post_div_table for the PLL\n");
  861. return -EINVAL;
  862. }
  863. return divider_round_rate(hw, rate, prate, pll->post_div_table,
  864. pll->width, CLK_DIVIDER_ROUND_CLOSEST);
  865. }
  866. static int clk_alpha_pll_postdiv_fabia_set_rate(struct clk_hw *hw,
  867. unsigned long rate, unsigned long parent_rate)
  868. {
  869. struct clk_alpha_pll_postdiv *pll = to_clk_alpha_pll_postdiv(hw);
  870. int i, val = 0, div, ret;
  871. /*
  872. * If the PLL is in FSM mode, then treat set_rate callback as a
  873. * no-operation.
  874. */
  875. ret = regmap_read(pll->clkr.regmap, PLL_MODE(pll), &val);
  876. if (ret)
  877. return ret;
  878. if (val & PLL_VOTE_FSM_ENA)
  879. return 0;
  880. if (!pll->post_div_table) {
  881. pr_err("Missing the post_div_table for the PLL\n");
  882. return -EINVAL;
  883. }
  884. div = DIV_ROUND_UP_ULL((u64)parent_rate, rate);
  885. for (i = 0; i < pll->num_post_div; i++) {
  886. if (pll->post_div_table[i].div == div) {
  887. val = pll->post_div_table[i].val;
  888. break;
  889. }
  890. }
  891. return regmap_update_bits(pll->clkr.regmap, PLL_USER_CTL(pll),
  892. (BIT(pll->width) - 1) << pll->post_div_shift,
  893. val << pll->post_div_shift);
  894. }
  895. const struct clk_ops clk_alpha_pll_postdiv_fabia_ops = {
  896. .recalc_rate = clk_alpha_pll_postdiv_fabia_recalc_rate,
  897. .round_rate = clk_alpha_pll_postdiv_fabia_round_rate,
  898. .set_rate = clk_alpha_pll_postdiv_fabia_set_rate,
  899. };
  900. EXPORT_SYMBOL_GPL(clk_alpha_pll_postdiv_fabia_ops);