clk-alpha-pll.c 29 KB

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