clk-alpha-pll.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115
  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. EXPORT_SYMBOL_GPL(clk_alpha_pll_configure);
  188. static int clk_alpha_pll_hwfsm_enable(struct clk_hw *hw)
  189. {
  190. int ret;
  191. struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
  192. u32 val;
  193. ret = regmap_read(pll->clkr.regmap, PLL_MODE(pll), &val);
  194. if (ret)
  195. return ret;
  196. val |= PLL_FSM_ENA;
  197. if (pll->flags & SUPPORTS_OFFLINE_REQ)
  198. val &= ~PLL_OFFLINE_REQ;
  199. ret = regmap_write(pll->clkr.regmap, PLL_MODE(pll), val);
  200. if (ret)
  201. return ret;
  202. /* Make sure enable request goes through before waiting for update */
  203. mb();
  204. return wait_for_pll_enable_active(pll);
  205. }
  206. static void clk_alpha_pll_hwfsm_disable(struct clk_hw *hw)
  207. {
  208. int ret;
  209. struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
  210. u32 val;
  211. ret = regmap_read(pll->clkr.regmap, PLL_MODE(pll), &val);
  212. if (ret)
  213. return;
  214. if (pll->flags & SUPPORTS_OFFLINE_REQ) {
  215. ret = regmap_update_bits(pll->clkr.regmap, PLL_MODE(pll),
  216. PLL_OFFLINE_REQ, PLL_OFFLINE_REQ);
  217. if (ret)
  218. return;
  219. ret = wait_for_pll_offline(pll);
  220. if (ret)
  221. return;
  222. }
  223. /* Disable hwfsm */
  224. ret = regmap_update_bits(pll->clkr.regmap, PLL_MODE(pll),
  225. PLL_FSM_ENA, 0);
  226. if (ret)
  227. return;
  228. wait_for_pll_disable(pll);
  229. }
  230. static int pll_is_enabled(struct clk_hw *hw, u32 mask)
  231. {
  232. int ret;
  233. struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
  234. u32 val;
  235. ret = regmap_read(pll->clkr.regmap, PLL_MODE(pll), &val);
  236. if (ret)
  237. return ret;
  238. return !!(val & mask);
  239. }
  240. static int clk_alpha_pll_hwfsm_is_enabled(struct clk_hw *hw)
  241. {
  242. return pll_is_enabled(hw, PLL_ACTIVE_FLAG);
  243. }
  244. static int clk_alpha_pll_is_enabled(struct clk_hw *hw)
  245. {
  246. return pll_is_enabled(hw, PLL_LOCK_DET);
  247. }
  248. static int clk_alpha_pll_enable(struct clk_hw *hw)
  249. {
  250. int ret;
  251. struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
  252. u32 val, mask;
  253. mask = PLL_OUTCTRL | PLL_RESET_N | PLL_BYPASSNL;
  254. ret = regmap_read(pll->clkr.regmap, PLL_MODE(pll), &val);
  255. if (ret)
  256. return ret;
  257. /* If in FSM mode, just vote for it */
  258. if (val & PLL_VOTE_FSM_ENA) {
  259. ret = clk_enable_regmap(hw);
  260. if (ret)
  261. return ret;
  262. return wait_for_pll_enable_active(pll);
  263. }
  264. /* Skip if already enabled */
  265. if ((val & mask) == mask)
  266. return 0;
  267. ret = regmap_update_bits(pll->clkr.regmap, PLL_MODE(pll),
  268. PLL_BYPASSNL, PLL_BYPASSNL);
  269. if (ret)
  270. return ret;
  271. /*
  272. * H/W requires a 5us delay between disabling the bypass and
  273. * de-asserting the reset.
  274. */
  275. mb();
  276. udelay(5);
  277. ret = regmap_update_bits(pll->clkr.regmap, PLL_MODE(pll),
  278. PLL_RESET_N, PLL_RESET_N);
  279. if (ret)
  280. return ret;
  281. ret = wait_for_pll_enable_lock(pll);
  282. if (ret)
  283. return ret;
  284. ret = regmap_update_bits(pll->clkr.regmap, PLL_MODE(pll),
  285. PLL_OUTCTRL, PLL_OUTCTRL);
  286. /* Ensure that the write above goes through before returning. */
  287. mb();
  288. return ret;
  289. }
  290. static void clk_alpha_pll_disable(struct clk_hw *hw)
  291. {
  292. int ret;
  293. struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
  294. u32 val, mask;
  295. ret = regmap_read(pll->clkr.regmap, PLL_MODE(pll), &val);
  296. if (ret)
  297. return;
  298. /* If in FSM mode, just unvote it */
  299. if (val & PLL_VOTE_FSM_ENA) {
  300. clk_disable_regmap(hw);
  301. return;
  302. }
  303. mask = PLL_OUTCTRL;
  304. regmap_update_bits(pll->clkr.regmap, PLL_MODE(pll), mask, 0);
  305. /* Delay of 2 output clock ticks required until output is disabled */
  306. mb();
  307. udelay(1);
  308. mask = PLL_RESET_N | PLL_BYPASSNL;
  309. regmap_update_bits(pll->clkr.regmap, PLL_MODE(pll), mask, 0);
  310. }
  311. static unsigned long
  312. alpha_pll_calc_rate(u64 prate, u32 l, u32 a, u32 alpha_width)
  313. {
  314. return (prate * l) + ((prate * a) >> ALPHA_SHIFT(alpha_width));
  315. }
  316. static unsigned long
  317. alpha_pll_round_rate(unsigned long rate, unsigned long prate, u32 *l, u64 *a,
  318. u32 alpha_width)
  319. {
  320. u64 remainder;
  321. u64 quotient;
  322. quotient = rate;
  323. remainder = do_div(quotient, prate);
  324. *l = quotient;
  325. if (!remainder) {
  326. *a = 0;
  327. return rate;
  328. }
  329. /* Upper ALPHA_BITWIDTH bits of Alpha */
  330. quotient = remainder << ALPHA_SHIFT(alpha_width);
  331. remainder = do_div(quotient, prate);
  332. if (remainder)
  333. quotient++;
  334. *a = quotient;
  335. return alpha_pll_calc_rate(prate, *l, *a, alpha_width);
  336. }
  337. static const struct pll_vco *
  338. alpha_pll_find_vco(const struct clk_alpha_pll *pll, unsigned long rate)
  339. {
  340. const struct pll_vco *v = pll->vco_table;
  341. const struct pll_vco *end = v + pll->num_vco;
  342. for (; v < end; v++)
  343. if (rate >= v->min_freq && rate <= v->max_freq)
  344. return v;
  345. return NULL;
  346. }
  347. static unsigned long
  348. clk_alpha_pll_recalc_rate(struct clk_hw *hw, unsigned long parent_rate)
  349. {
  350. u32 l, low, high, ctl;
  351. u64 a = 0, prate = parent_rate;
  352. struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
  353. u32 alpha_width = pll_alpha_width(pll);
  354. regmap_read(pll->clkr.regmap, PLL_L_VAL(pll), &l);
  355. regmap_read(pll->clkr.regmap, PLL_USER_CTL(pll), &ctl);
  356. if (ctl & PLL_ALPHA_EN) {
  357. regmap_read(pll->clkr.regmap, PLL_ALPHA_VAL(pll), &low);
  358. if (alpha_width > 32) {
  359. regmap_read(pll->clkr.regmap, PLL_ALPHA_VAL_U(pll),
  360. &high);
  361. a = (u64)high << 32 | low;
  362. } else {
  363. a = low & GENMASK(alpha_width - 1, 0);
  364. }
  365. if (alpha_width > ALPHA_BITWIDTH)
  366. a >>= alpha_width - ALPHA_BITWIDTH;
  367. }
  368. return alpha_pll_calc_rate(prate, l, a, alpha_width);
  369. }
  370. static int __clk_alpha_pll_update_latch(struct clk_alpha_pll *pll)
  371. {
  372. int ret;
  373. u32 mode;
  374. regmap_read(pll->clkr.regmap, PLL_MODE(pll), &mode);
  375. /* Latch the input to the PLL */
  376. regmap_update_bits(pll->clkr.regmap, PLL_MODE(pll), PLL_UPDATE,
  377. PLL_UPDATE);
  378. /* Wait for 2 reference cycle before checking ACK bit */
  379. udelay(1);
  380. /*
  381. * PLL will latch the new L, Alpha and freq control word.
  382. * PLL will respond by raising PLL_ACK_LATCH output when new programming
  383. * has been latched in and PLL is being updated. When
  384. * UPDATE_LOGIC_BYPASS bit is not set, PLL_UPDATE will be cleared
  385. * automatically by hardware when PLL_ACK_LATCH is asserted by PLL.
  386. */
  387. if (mode & PLL_UPDATE_BYPASS) {
  388. ret = wait_for_pll_update_ack_set(pll);
  389. if (ret)
  390. return ret;
  391. regmap_update_bits(pll->clkr.regmap, PLL_MODE(pll), PLL_UPDATE, 0);
  392. } else {
  393. ret = wait_for_pll_update(pll);
  394. if (ret)
  395. return ret;
  396. }
  397. ret = wait_for_pll_update_ack_clear(pll);
  398. if (ret)
  399. return ret;
  400. /* Wait for PLL output to stabilize */
  401. udelay(10);
  402. return 0;
  403. }
  404. static int clk_alpha_pll_update_latch(struct clk_alpha_pll *pll,
  405. int (*is_enabled)(struct clk_hw *))
  406. {
  407. if (!is_enabled(&pll->clkr.hw) ||
  408. !(pll->flags & SUPPORTS_DYNAMIC_UPDATE))
  409. return 0;
  410. return __clk_alpha_pll_update_latch(pll);
  411. }
  412. static int __clk_alpha_pll_set_rate(struct clk_hw *hw, unsigned long rate,
  413. unsigned long prate,
  414. int (*is_enabled)(struct clk_hw *))
  415. {
  416. struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
  417. const struct pll_vco *vco;
  418. u32 l, alpha_width = pll_alpha_width(pll);
  419. u64 a;
  420. rate = alpha_pll_round_rate(rate, prate, &l, &a, alpha_width);
  421. vco = alpha_pll_find_vco(pll, rate);
  422. if (pll->vco_table && !vco) {
  423. pr_err("alpha pll not in a valid vco range\n");
  424. return -EINVAL;
  425. }
  426. regmap_write(pll->clkr.regmap, PLL_L_VAL(pll), l);
  427. if (alpha_width > ALPHA_BITWIDTH)
  428. a <<= alpha_width - ALPHA_BITWIDTH;
  429. if (alpha_width > 32)
  430. regmap_write(pll->clkr.regmap, PLL_ALPHA_VAL_U(pll), a >> 32);
  431. regmap_write(pll->clkr.regmap, PLL_ALPHA_VAL(pll), a);
  432. if (vco) {
  433. regmap_update_bits(pll->clkr.regmap, PLL_USER_CTL(pll),
  434. PLL_VCO_MASK << PLL_VCO_SHIFT,
  435. vco->val << PLL_VCO_SHIFT);
  436. }
  437. regmap_update_bits(pll->clkr.regmap, PLL_USER_CTL(pll),
  438. PLL_ALPHA_EN, PLL_ALPHA_EN);
  439. return clk_alpha_pll_update_latch(pll, is_enabled);
  440. }
  441. static int clk_alpha_pll_set_rate(struct clk_hw *hw, unsigned long rate,
  442. unsigned long prate)
  443. {
  444. return __clk_alpha_pll_set_rate(hw, rate, prate,
  445. clk_alpha_pll_is_enabled);
  446. }
  447. static int clk_alpha_pll_hwfsm_set_rate(struct clk_hw *hw, unsigned long rate,
  448. unsigned long prate)
  449. {
  450. return __clk_alpha_pll_set_rate(hw, rate, prate,
  451. clk_alpha_pll_hwfsm_is_enabled);
  452. }
  453. static long clk_alpha_pll_round_rate(struct clk_hw *hw, unsigned long rate,
  454. unsigned long *prate)
  455. {
  456. struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
  457. u32 l, alpha_width = pll_alpha_width(pll);
  458. u64 a;
  459. unsigned long min_freq, max_freq;
  460. rate = alpha_pll_round_rate(rate, *prate, &l, &a, alpha_width);
  461. if (!pll->vco_table || alpha_pll_find_vco(pll, rate))
  462. return rate;
  463. min_freq = pll->vco_table[0].min_freq;
  464. max_freq = pll->vco_table[pll->num_vco - 1].max_freq;
  465. return clamp(rate, min_freq, max_freq);
  466. }
  467. static unsigned long
  468. alpha_huayra_pll_calc_rate(u64 prate, u32 l, u32 a)
  469. {
  470. /*
  471. * a contains 16 bit alpha_val in two’s compliment number in the range
  472. * of [-0.5, 0.5).
  473. */
  474. if (a >= BIT(PLL_HUAYRA_ALPHA_WIDTH - 1))
  475. l -= 1;
  476. return (prate * l) + (prate * a >> PLL_HUAYRA_ALPHA_WIDTH);
  477. }
  478. static unsigned long
  479. alpha_huayra_pll_round_rate(unsigned long rate, unsigned long prate,
  480. u32 *l, u32 *a)
  481. {
  482. u64 remainder;
  483. u64 quotient;
  484. quotient = rate;
  485. remainder = do_div(quotient, prate);
  486. *l = quotient;
  487. if (!remainder) {
  488. *a = 0;
  489. return rate;
  490. }
  491. quotient = remainder << PLL_HUAYRA_ALPHA_WIDTH;
  492. remainder = do_div(quotient, prate);
  493. if (remainder)
  494. quotient++;
  495. /*
  496. * alpha_val should be in two’s compliment number in the range
  497. * of [-0.5, 0.5) so if quotient >= 0.5 then increment the l value
  498. * since alpha value will be subtracted in this case.
  499. */
  500. if (quotient >= BIT(PLL_HUAYRA_ALPHA_WIDTH - 1))
  501. *l += 1;
  502. *a = quotient;
  503. return alpha_huayra_pll_calc_rate(prate, *l, *a);
  504. }
  505. static unsigned long
  506. alpha_pll_huayra_recalc_rate(struct clk_hw *hw, unsigned long parent_rate)
  507. {
  508. u64 rate = parent_rate, tmp;
  509. struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
  510. u32 l, alpha = 0, ctl, alpha_m, alpha_n;
  511. regmap_read(pll->clkr.regmap, PLL_L_VAL(pll), &l);
  512. regmap_read(pll->clkr.regmap, PLL_USER_CTL(pll), &ctl);
  513. if (ctl & PLL_ALPHA_EN) {
  514. regmap_read(pll->clkr.regmap, PLL_ALPHA_VAL(pll), &alpha);
  515. /*
  516. * Depending upon alpha_mode, it can be treated as M/N value or
  517. * as a two’s compliment number. When alpha_mode=1,
  518. * pll_alpha_val<15:8>=M and pll_apla_val<7:0>=N
  519. *
  520. * Fout=FIN*(L+(M/N))
  521. *
  522. * M is a signed number (-128 to 127) and N is unsigned
  523. * (0 to 255). M/N has to be within +/-0.5.
  524. *
  525. * When alpha_mode=0, it is a two’s compliment number in the
  526. * range [-0.5, 0.5).
  527. *
  528. * Fout=FIN*(L+(alpha_val)/2^16)
  529. *
  530. * where alpha_val is two’s compliment number.
  531. */
  532. if (!(ctl & PLL_ALPHA_MODE))
  533. return alpha_huayra_pll_calc_rate(rate, l, alpha);
  534. alpha_m = alpha >> PLL_HUAYRA_M_SHIFT & PLL_HUAYRA_M_MASK;
  535. alpha_n = alpha >> PLL_HUAYRA_N_SHIFT & PLL_HUAYRA_N_MASK;
  536. rate *= l;
  537. tmp = parent_rate;
  538. if (alpha_m >= BIT(PLL_HUAYRA_M_WIDTH - 1)) {
  539. alpha_m = BIT(PLL_HUAYRA_M_WIDTH) - alpha_m;
  540. tmp *= alpha_m;
  541. do_div(tmp, alpha_n);
  542. rate -= tmp;
  543. } else {
  544. tmp *= alpha_m;
  545. do_div(tmp, alpha_n);
  546. rate += tmp;
  547. }
  548. return rate;
  549. }
  550. return alpha_huayra_pll_calc_rate(rate, l, alpha);
  551. }
  552. static int alpha_pll_huayra_set_rate(struct clk_hw *hw, unsigned long rate,
  553. unsigned long prate)
  554. {
  555. struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
  556. u32 l, a, ctl, cur_alpha = 0;
  557. rate = alpha_huayra_pll_round_rate(rate, prate, &l, &a);
  558. regmap_read(pll->clkr.regmap, PLL_USER_CTL(pll), &ctl);
  559. if (ctl & PLL_ALPHA_EN)
  560. regmap_read(pll->clkr.regmap, PLL_ALPHA_VAL(pll), &cur_alpha);
  561. /*
  562. * Huayra PLL supports PLL dynamic programming. User can change L_VAL,
  563. * without having to go through the power on sequence.
  564. */
  565. if (clk_alpha_pll_is_enabled(hw)) {
  566. if (cur_alpha != a) {
  567. pr_err("clock needs to be gated %s\n",
  568. clk_hw_get_name(hw));
  569. return -EBUSY;
  570. }
  571. regmap_write(pll->clkr.regmap, PLL_L_VAL(pll), l);
  572. /* Ensure that the write above goes to detect L val change. */
  573. mb();
  574. return wait_for_pll_enable_lock(pll);
  575. }
  576. regmap_write(pll->clkr.regmap, PLL_L_VAL(pll), l);
  577. regmap_write(pll->clkr.regmap, PLL_ALPHA_VAL(pll), a);
  578. if (a == 0)
  579. regmap_update_bits(pll->clkr.regmap, PLL_USER_CTL(pll),
  580. PLL_ALPHA_EN, 0x0);
  581. else
  582. regmap_update_bits(pll->clkr.regmap, PLL_USER_CTL(pll),
  583. PLL_ALPHA_EN | PLL_ALPHA_MODE, PLL_ALPHA_EN);
  584. return 0;
  585. }
  586. static long alpha_pll_huayra_round_rate(struct clk_hw *hw, unsigned long rate,
  587. unsigned long *prate)
  588. {
  589. u32 l, a;
  590. return alpha_huayra_pll_round_rate(rate, *prate, &l, &a);
  591. }
  592. const struct clk_ops clk_alpha_pll_ops = {
  593. .enable = clk_alpha_pll_enable,
  594. .disable = clk_alpha_pll_disable,
  595. .is_enabled = clk_alpha_pll_is_enabled,
  596. .recalc_rate = clk_alpha_pll_recalc_rate,
  597. .round_rate = clk_alpha_pll_round_rate,
  598. .set_rate = clk_alpha_pll_set_rate,
  599. };
  600. EXPORT_SYMBOL_GPL(clk_alpha_pll_ops);
  601. const struct clk_ops clk_alpha_pll_huayra_ops = {
  602. .enable = clk_alpha_pll_enable,
  603. .disable = clk_alpha_pll_disable,
  604. .is_enabled = clk_alpha_pll_is_enabled,
  605. .recalc_rate = alpha_pll_huayra_recalc_rate,
  606. .round_rate = alpha_pll_huayra_round_rate,
  607. .set_rate = alpha_pll_huayra_set_rate,
  608. };
  609. EXPORT_SYMBOL_GPL(clk_alpha_pll_huayra_ops);
  610. const struct clk_ops clk_alpha_pll_hwfsm_ops = {
  611. .enable = clk_alpha_pll_hwfsm_enable,
  612. .disable = clk_alpha_pll_hwfsm_disable,
  613. .is_enabled = clk_alpha_pll_hwfsm_is_enabled,
  614. .recalc_rate = clk_alpha_pll_recalc_rate,
  615. .round_rate = clk_alpha_pll_round_rate,
  616. .set_rate = clk_alpha_pll_hwfsm_set_rate,
  617. };
  618. EXPORT_SYMBOL_GPL(clk_alpha_pll_hwfsm_ops);
  619. static unsigned long
  620. clk_alpha_pll_postdiv_recalc_rate(struct clk_hw *hw, unsigned long parent_rate)
  621. {
  622. struct clk_alpha_pll_postdiv *pll = to_clk_alpha_pll_postdiv(hw);
  623. u32 ctl;
  624. regmap_read(pll->clkr.regmap, PLL_USER_CTL(pll), &ctl);
  625. ctl >>= PLL_POST_DIV_SHIFT;
  626. ctl &= PLL_POST_DIV_MASK(pll);
  627. return parent_rate >> fls(ctl);
  628. }
  629. static const struct clk_div_table clk_alpha_div_table[] = {
  630. { 0x0, 1 },
  631. { 0x1, 2 },
  632. { 0x3, 4 },
  633. { 0x7, 8 },
  634. { 0xf, 16 },
  635. { }
  636. };
  637. static const struct clk_div_table clk_alpha_2bit_div_table[] = {
  638. { 0x0, 1 },
  639. { 0x1, 2 },
  640. { 0x3, 4 },
  641. { }
  642. };
  643. static long
  644. clk_alpha_pll_postdiv_round_rate(struct clk_hw *hw, unsigned long rate,
  645. unsigned long *prate)
  646. {
  647. struct clk_alpha_pll_postdiv *pll = to_clk_alpha_pll_postdiv(hw);
  648. const struct clk_div_table *table;
  649. if (pll->width == 2)
  650. table = clk_alpha_2bit_div_table;
  651. else
  652. table = clk_alpha_div_table;
  653. return divider_round_rate(hw, rate, prate, table,
  654. pll->width, CLK_DIVIDER_POWER_OF_TWO);
  655. }
  656. static long
  657. clk_alpha_pll_postdiv_round_ro_rate(struct clk_hw *hw, unsigned long rate,
  658. unsigned long *prate)
  659. {
  660. struct clk_alpha_pll_postdiv *pll = to_clk_alpha_pll_postdiv(hw);
  661. u32 ctl, div;
  662. regmap_read(pll->clkr.regmap, PLL_USER_CTL(pll), &ctl);
  663. ctl >>= PLL_POST_DIV_SHIFT;
  664. ctl &= BIT(pll->width) - 1;
  665. div = 1 << fls(ctl);
  666. if (clk_hw_get_flags(hw) & CLK_SET_RATE_PARENT)
  667. *prate = clk_hw_round_rate(clk_hw_get_parent(hw), div * rate);
  668. return DIV_ROUND_UP_ULL((u64)*prate, div);
  669. }
  670. static int clk_alpha_pll_postdiv_set_rate(struct clk_hw *hw, unsigned long rate,
  671. unsigned long parent_rate)
  672. {
  673. struct clk_alpha_pll_postdiv *pll = to_clk_alpha_pll_postdiv(hw);
  674. int div;
  675. /* 16 -> 0xf, 8 -> 0x7, 4 -> 0x3, 2 -> 0x1, 1 -> 0x0 */
  676. div = DIV_ROUND_UP_ULL((u64)parent_rate, rate) - 1;
  677. return regmap_update_bits(pll->clkr.regmap, PLL_USER_CTL(pll),
  678. PLL_POST_DIV_MASK(pll) << PLL_POST_DIV_SHIFT,
  679. div << PLL_POST_DIV_SHIFT);
  680. }
  681. const struct clk_ops clk_alpha_pll_postdiv_ops = {
  682. .recalc_rate = clk_alpha_pll_postdiv_recalc_rate,
  683. .round_rate = clk_alpha_pll_postdiv_round_rate,
  684. .set_rate = clk_alpha_pll_postdiv_set_rate,
  685. };
  686. EXPORT_SYMBOL_GPL(clk_alpha_pll_postdiv_ops);
  687. const struct clk_ops clk_alpha_pll_postdiv_ro_ops = {
  688. .round_rate = clk_alpha_pll_postdiv_round_ro_rate,
  689. .recalc_rate = clk_alpha_pll_postdiv_recalc_rate,
  690. };
  691. EXPORT_SYMBOL_GPL(clk_alpha_pll_postdiv_ro_ops);
  692. void clk_fabia_pll_configure(struct clk_alpha_pll *pll, struct regmap *regmap,
  693. const struct alpha_pll_config *config)
  694. {
  695. u32 val, mask;
  696. if (config->l)
  697. regmap_write(regmap, PLL_L_VAL(pll), config->l);
  698. if (config->alpha)
  699. regmap_write(regmap, PLL_FRAC(pll), config->alpha);
  700. if (config->config_ctl_val)
  701. regmap_write(regmap, PLL_CONFIG_CTL(pll),
  702. config->config_ctl_val);
  703. if (config->post_div_mask) {
  704. mask = config->post_div_mask;
  705. val = config->post_div_val;
  706. regmap_update_bits(regmap, PLL_USER_CTL(pll), mask, val);
  707. }
  708. regmap_update_bits(regmap, PLL_MODE(pll), PLL_UPDATE_BYPASS,
  709. PLL_UPDATE_BYPASS);
  710. regmap_update_bits(regmap, PLL_MODE(pll), PLL_RESET_N, PLL_RESET_N);
  711. }
  712. EXPORT_SYMBOL_GPL(clk_fabia_pll_configure);
  713. static int alpha_pll_fabia_enable(struct clk_hw *hw)
  714. {
  715. int ret;
  716. struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
  717. u32 val, opmode_val;
  718. struct regmap *regmap = pll->clkr.regmap;
  719. ret = regmap_read(regmap, PLL_MODE(pll), &val);
  720. if (ret)
  721. return ret;
  722. /* If in FSM mode, just vote for it */
  723. if (val & PLL_VOTE_FSM_ENA) {
  724. ret = clk_enable_regmap(hw);
  725. if (ret)
  726. return ret;
  727. return wait_for_pll_enable_active(pll);
  728. }
  729. ret = regmap_read(regmap, PLL_OPMODE(pll), &opmode_val);
  730. if (ret)
  731. return ret;
  732. /* Skip If PLL is already running */
  733. if ((opmode_val & FABIA_OPMODE_RUN) && (val & PLL_OUTCTRL))
  734. return 0;
  735. ret = regmap_update_bits(regmap, PLL_MODE(pll), PLL_OUTCTRL, 0);
  736. if (ret)
  737. return ret;
  738. ret = regmap_write(regmap, PLL_OPMODE(pll), FABIA_OPMODE_STANDBY);
  739. if (ret)
  740. return ret;
  741. ret = regmap_update_bits(regmap, PLL_MODE(pll), PLL_RESET_N,
  742. PLL_RESET_N);
  743. if (ret)
  744. return ret;
  745. ret = regmap_write(regmap, PLL_OPMODE(pll), FABIA_OPMODE_RUN);
  746. if (ret)
  747. return ret;
  748. ret = wait_for_pll_enable_lock(pll);
  749. if (ret)
  750. return ret;
  751. ret = regmap_update_bits(regmap, PLL_USER_CTL(pll),
  752. FABIA_PLL_OUT_MASK, FABIA_PLL_OUT_MASK);
  753. if (ret)
  754. return ret;
  755. return regmap_update_bits(regmap, PLL_MODE(pll), PLL_OUTCTRL,
  756. PLL_OUTCTRL);
  757. }
  758. static void alpha_pll_fabia_disable(struct clk_hw *hw)
  759. {
  760. int ret;
  761. struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
  762. u32 val;
  763. struct regmap *regmap = pll->clkr.regmap;
  764. ret = regmap_read(regmap, PLL_MODE(pll), &val);
  765. if (ret)
  766. return;
  767. /* If in FSM mode, just unvote it */
  768. if (val & PLL_FSM_ENA) {
  769. clk_disable_regmap(hw);
  770. return;
  771. }
  772. ret = regmap_update_bits(regmap, PLL_MODE(pll), PLL_OUTCTRL, 0);
  773. if (ret)
  774. return;
  775. /* Disable main outputs */
  776. ret = regmap_update_bits(regmap, PLL_USER_CTL(pll), FABIA_PLL_OUT_MASK,
  777. 0);
  778. if (ret)
  779. return;
  780. /* Place the PLL in STANDBY */
  781. regmap_write(regmap, PLL_OPMODE(pll), FABIA_OPMODE_STANDBY);
  782. }
  783. static unsigned long alpha_pll_fabia_recalc_rate(struct clk_hw *hw,
  784. unsigned long parent_rate)
  785. {
  786. struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
  787. u32 l, frac, alpha_width = pll_alpha_width(pll);
  788. regmap_read(pll->clkr.regmap, PLL_L_VAL(pll), &l);
  789. regmap_read(pll->clkr.regmap, PLL_FRAC(pll), &frac);
  790. return alpha_pll_calc_rate(parent_rate, l, frac, alpha_width);
  791. }
  792. static int alpha_pll_fabia_set_rate(struct clk_hw *hw, unsigned long rate,
  793. unsigned long prate)
  794. {
  795. struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
  796. u32 val, l, alpha_width = pll_alpha_width(pll);
  797. u64 a;
  798. unsigned long rrate;
  799. int ret = 0;
  800. ret = regmap_read(pll->clkr.regmap, PLL_MODE(pll), &val);
  801. if (ret)
  802. return ret;
  803. rrate = alpha_pll_round_rate(rate, prate, &l, &a, alpha_width);
  804. /*
  805. * Due to limited number of bits for fractional rate programming, the
  806. * rounded up rate could be marginally higher than the requested rate.
  807. */
  808. if (rrate > (rate + FABIA_PLL_RATE_MARGIN) || rrate < rate) {
  809. pr_err("Call set rate on the PLL with rounded rates!\n");
  810. return -EINVAL;
  811. }
  812. regmap_write(pll->clkr.regmap, PLL_L_VAL(pll), l);
  813. regmap_write(pll->clkr.regmap, PLL_FRAC(pll), a);
  814. return __clk_alpha_pll_update_latch(pll);
  815. }
  816. const struct clk_ops clk_alpha_pll_fabia_ops = {
  817. .enable = alpha_pll_fabia_enable,
  818. .disable = alpha_pll_fabia_disable,
  819. .is_enabled = clk_alpha_pll_is_enabled,
  820. .set_rate = alpha_pll_fabia_set_rate,
  821. .recalc_rate = alpha_pll_fabia_recalc_rate,
  822. .round_rate = clk_alpha_pll_round_rate,
  823. };
  824. EXPORT_SYMBOL_GPL(clk_alpha_pll_fabia_ops);
  825. const struct clk_ops clk_alpha_pll_fixed_fabia_ops = {
  826. .enable = alpha_pll_fabia_enable,
  827. .disable = alpha_pll_fabia_disable,
  828. .is_enabled = clk_alpha_pll_is_enabled,
  829. .recalc_rate = alpha_pll_fabia_recalc_rate,
  830. .round_rate = clk_alpha_pll_round_rate,
  831. };
  832. EXPORT_SYMBOL_GPL(clk_alpha_pll_fixed_fabia_ops);
  833. static unsigned long clk_alpha_pll_postdiv_fabia_recalc_rate(struct clk_hw *hw,
  834. unsigned long parent_rate)
  835. {
  836. struct clk_alpha_pll_postdiv *pll = to_clk_alpha_pll_postdiv(hw);
  837. u32 i, div = 1, val;
  838. int ret;
  839. if (!pll->post_div_table) {
  840. pr_err("Missing the post_div_table for the PLL\n");
  841. return -EINVAL;
  842. }
  843. ret = regmap_read(pll->clkr.regmap, PLL_USER_CTL(pll), &val);
  844. if (ret)
  845. return ret;
  846. val >>= pll->post_div_shift;
  847. val &= BIT(pll->width) - 1;
  848. for (i = 0; i < pll->num_post_div; i++) {
  849. if (pll->post_div_table[i].val == val) {
  850. div = pll->post_div_table[i].div;
  851. break;
  852. }
  853. }
  854. return (parent_rate / div);
  855. }
  856. static long clk_alpha_pll_postdiv_fabia_round_rate(struct clk_hw *hw,
  857. unsigned long rate, unsigned long *prate)
  858. {
  859. struct clk_alpha_pll_postdiv *pll = to_clk_alpha_pll_postdiv(hw);
  860. if (!pll->post_div_table) {
  861. pr_err("Missing the post_div_table for the PLL\n");
  862. return -EINVAL;
  863. }
  864. return divider_round_rate(hw, rate, prate, pll->post_div_table,
  865. pll->width, CLK_DIVIDER_ROUND_CLOSEST);
  866. }
  867. static int clk_alpha_pll_postdiv_fabia_set_rate(struct clk_hw *hw,
  868. unsigned long rate, unsigned long parent_rate)
  869. {
  870. struct clk_alpha_pll_postdiv *pll = to_clk_alpha_pll_postdiv(hw);
  871. int i, val = 0, div, ret;
  872. /*
  873. * If the PLL is in FSM mode, then treat set_rate callback as a
  874. * no-operation.
  875. */
  876. ret = regmap_read(pll->clkr.regmap, PLL_MODE(pll), &val);
  877. if (ret)
  878. return ret;
  879. if (val & PLL_VOTE_FSM_ENA)
  880. return 0;
  881. if (!pll->post_div_table) {
  882. pr_err("Missing the post_div_table for the PLL\n");
  883. return -EINVAL;
  884. }
  885. div = DIV_ROUND_UP_ULL((u64)parent_rate, rate);
  886. for (i = 0; i < pll->num_post_div; i++) {
  887. if (pll->post_div_table[i].div == div) {
  888. val = pll->post_div_table[i].val;
  889. break;
  890. }
  891. }
  892. return regmap_update_bits(pll->clkr.regmap, PLL_USER_CTL(pll),
  893. (BIT(pll->width) - 1) << pll->post_div_shift,
  894. val << pll->post_div_shift);
  895. }
  896. const struct clk_ops clk_alpha_pll_postdiv_fabia_ops = {
  897. .recalc_rate = clk_alpha_pll_postdiv_fabia_recalc_rate,
  898. .round_rate = clk_alpha_pll_postdiv_fabia_round_rate,
  899. .set_rate = clk_alpha_pll_postdiv_fabia_set_rate,
  900. };
  901. EXPORT_SYMBOL_GPL(clk_alpha_pll_postdiv_fabia_ops);