clk-iproc-pll.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869
  1. /*
  2. * Copyright (C) 2014 Broadcom Corporation
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation version 2.
  7. *
  8. * This program is distributed "as is" WITHOUT ANY WARRANTY of any
  9. * kind, whether express or implied; without even the implied warranty
  10. * of 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/err.h>
  15. #include <linux/clk-provider.h>
  16. #include <linux/io.h>
  17. #include <linux/of.h>
  18. #include <linux/clkdev.h>
  19. #include <linux/of_address.h>
  20. #include <linux/delay.h>
  21. #include "clk-iproc.h"
  22. #define PLL_VCO_HIGH_SHIFT 19
  23. #define PLL_VCO_LOW_SHIFT 30
  24. /*
  25. * PLL MACRO_SELECT modes 0 to 5 choose pre-calculated PLL output frequencies
  26. * from a look-up table. Mode 7 allows user to manipulate PLL clock dividers
  27. */
  28. #define PLL_USER_MODE 7
  29. /* number of delay loops waiting for PLL to lock */
  30. #define LOCK_DELAY 100
  31. /* number of VCO frequency bands */
  32. #define NUM_FREQ_BANDS 8
  33. #define NUM_KP_BANDS 3
  34. enum kp_band {
  35. KP_BAND_MID = 0,
  36. KP_BAND_HIGH,
  37. KP_BAND_HIGH_HIGH
  38. };
  39. static const unsigned int kp_table[NUM_KP_BANDS][NUM_FREQ_BANDS] = {
  40. { 5, 6, 6, 7, 7, 8, 9, 10 },
  41. { 4, 4, 5, 5, 6, 7, 8, 9 },
  42. { 4, 5, 5, 6, 7, 8, 9, 10 },
  43. };
  44. static const unsigned long ref_freq_table[NUM_FREQ_BANDS][2] = {
  45. { 10000000, 12500000 },
  46. { 12500000, 15000000 },
  47. { 15000000, 20000000 },
  48. { 20000000, 25000000 },
  49. { 25000000, 50000000 },
  50. { 50000000, 75000000 },
  51. { 75000000, 100000000 },
  52. { 100000000, 125000000 },
  53. };
  54. enum vco_freq_range {
  55. VCO_LOW = 700000000U,
  56. VCO_MID = 1200000000U,
  57. VCO_HIGH = 2200000000U,
  58. VCO_HIGH_HIGH = 3100000000U,
  59. VCO_MAX = 4000000000U,
  60. };
  61. struct iproc_pll {
  62. void __iomem *status_base;
  63. void __iomem *control_base;
  64. void __iomem *pwr_base;
  65. void __iomem *asiu_base;
  66. const struct iproc_pll_ctrl *ctrl;
  67. const struct iproc_pll_vco_param *vco_param;
  68. unsigned int num_vco_entries;
  69. };
  70. struct iproc_clk {
  71. struct clk_hw hw;
  72. struct iproc_pll *pll;
  73. const struct iproc_clk_ctrl *ctrl;
  74. };
  75. #define to_iproc_clk(hw) container_of(hw, struct iproc_clk, hw)
  76. static int pll_calc_param(unsigned long target_rate,
  77. unsigned long parent_rate,
  78. struct iproc_pll_vco_param *vco_out)
  79. {
  80. u64 ndiv_int, ndiv_frac, residual;
  81. ndiv_int = target_rate / parent_rate;
  82. if (!ndiv_int || (ndiv_int > 255))
  83. return -EINVAL;
  84. residual = target_rate - (ndiv_int * parent_rate);
  85. residual <<= 20;
  86. /*
  87. * Add half of the divisor so the result will be rounded to closest
  88. * instead of rounded down.
  89. */
  90. residual += (parent_rate / 2);
  91. ndiv_frac = div64_u64((u64)residual, (u64)parent_rate);
  92. vco_out->ndiv_int = ndiv_int;
  93. vco_out->ndiv_frac = ndiv_frac;
  94. vco_out->pdiv = 1;
  95. vco_out->rate = vco_out->ndiv_int * parent_rate;
  96. residual = (u64)vco_out->ndiv_frac * (u64)parent_rate;
  97. residual >>= 20;
  98. vco_out->rate += residual;
  99. return 0;
  100. }
  101. /*
  102. * Based on the target frequency, find a match from the VCO frequency parameter
  103. * table and return its index
  104. */
  105. static int pll_get_rate_index(struct iproc_pll *pll, unsigned int target_rate)
  106. {
  107. int i;
  108. for (i = 0; i < pll->num_vco_entries; i++)
  109. if (target_rate == pll->vco_param[i].rate)
  110. break;
  111. if (i >= pll->num_vco_entries)
  112. return -EINVAL;
  113. return i;
  114. }
  115. static int get_kp(unsigned long ref_freq, enum kp_band kp_index)
  116. {
  117. int i;
  118. if (ref_freq < ref_freq_table[0][0])
  119. return -EINVAL;
  120. for (i = 0; i < NUM_FREQ_BANDS; i++) {
  121. if (ref_freq >= ref_freq_table[i][0] &&
  122. ref_freq < ref_freq_table[i][1])
  123. return kp_table[kp_index][i];
  124. }
  125. return -EINVAL;
  126. }
  127. static int pll_wait_for_lock(struct iproc_pll *pll)
  128. {
  129. int i;
  130. const struct iproc_pll_ctrl *ctrl = pll->ctrl;
  131. for (i = 0; i < LOCK_DELAY; i++) {
  132. u32 val = readl(pll->status_base + ctrl->status.offset);
  133. if (val & (1 << ctrl->status.shift))
  134. return 0;
  135. udelay(10);
  136. }
  137. return -EIO;
  138. }
  139. static void iproc_pll_write(const struct iproc_pll *pll, void __iomem *base,
  140. const u32 offset, u32 val)
  141. {
  142. const struct iproc_pll_ctrl *ctrl = pll->ctrl;
  143. writel(val, base + offset);
  144. if (unlikely(ctrl->flags & IPROC_CLK_NEEDS_READ_BACK &&
  145. (base == pll->status_base || base == pll->control_base)))
  146. val = readl(base + offset);
  147. }
  148. static void __pll_disable(struct iproc_pll *pll)
  149. {
  150. const struct iproc_pll_ctrl *ctrl = pll->ctrl;
  151. u32 val;
  152. if (ctrl->flags & IPROC_CLK_PLL_ASIU) {
  153. val = readl(pll->asiu_base + ctrl->asiu.offset);
  154. val &= ~(1 << ctrl->asiu.en_shift);
  155. iproc_pll_write(pll, pll->asiu_base, ctrl->asiu.offset, val);
  156. }
  157. if (ctrl->flags & IPROC_CLK_EMBED_PWRCTRL) {
  158. val = readl(pll->control_base + ctrl->aon.offset);
  159. val |= bit_mask(ctrl->aon.pwr_width) << ctrl->aon.pwr_shift;
  160. iproc_pll_write(pll, pll->control_base, ctrl->aon.offset, val);
  161. }
  162. if (pll->pwr_base) {
  163. /* latch input value so core power can be shut down */
  164. val = readl(pll->pwr_base + ctrl->aon.offset);
  165. val |= 1 << ctrl->aon.iso_shift;
  166. iproc_pll_write(pll, pll->pwr_base, ctrl->aon.offset, val);
  167. /* power down the core */
  168. val &= ~(bit_mask(ctrl->aon.pwr_width) << ctrl->aon.pwr_shift);
  169. iproc_pll_write(pll, pll->pwr_base, ctrl->aon.offset, val);
  170. }
  171. }
  172. static int __pll_enable(struct iproc_pll *pll)
  173. {
  174. const struct iproc_pll_ctrl *ctrl = pll->ctrl;
  175. u32 val;
  176. if (ctrl->flags & IPROC_CLK_EMBED_PWRCTRL) {
  177. val = readl(pll->control_base + ctrl->aon.offset);
  178. val &= ~(bit_mask(ctrl->aon.pwr_width) << ctrl->aon.pwr_shift);
  179. iproc_pll_write(pll, pll->control_base, ctrl->aon.offset, val);
  180. }
  181. if (pll->pwr_base) {
  182. /* power up the PLL and make sure it's not latched */
  183. val = readl(pll->pwr_base + ctrl->aon.offset);
  184. val |= bit_mask(ctrl->aon.pwr_width) << ctrl->aon.pwr_shift;
  185. val &= ~(1 << ctrl->aon.iso_shift);
  186. iproc_pll_write(pll, pll->pwr_base, ctrl->aon.offset, val);
  187. }
  188. /* certain PLLs also need to be ungated from the ASIU top level */
  189. if (ctrl->flags & IPROC_CLK_PLL_ASIU) {
  190. val = readl(pll->asiu_base + ctrl->asiu.offset);
  191. val |= (1 << ctrl->asiu.en_shift);
  192. iproc_pll_write(pll, pll->asiu_base, ctrl->asiu.offset, val);
  193. }
  194. return 0;
  195. }
  196. static void __pll_put_in_reset(struct iproc_pll *pll)
  197. {
  198. u32 val;
  199. const struct iproc_pll_ctrl *ctrl = pll->ctrl;
  200. const struct iproc_pll_reset_ctrl *reset = &ctrl->reset;
  201. val = readl(pll->control_base + reset->offset);
  202. if (ctrl->flags & IPROC_CLK_PLL_RESET_ACTIVE_LOW)
  203. val |= BIT(reset->reset_shift) | BIT(reset->p_reset_shift);
  204. else
  205. val &= ~(BIT(reset->reset_shift) | BIT(reset->p_reset_shift));
  206. iproc_pll_write(pll, pll->control_base, reset->offset, val);
  207. }
  208. static void __pll_bring_out_reset(struct iproc_pll *pll, unsigned int kp,
  209. unsigned int ka, unsigned int ki)
  210. {
  211. u32 val;
  212. const struct iproc_pll_ctrl *ctrl = pll->ctrl;
  213. const struct iproc_pll_reset_ctrl *reset = &ctrl->reset;
  214. const struct iproc_pll_dig_filter_ctrl *dig_filter = &ctrl->dig_filter;
  215. val = readl(pll->control_base + dig_filter->offset);
  216. val &= ~(bit_mask(dig_filter->ki_width) << dig_filter->ki_shift |
  217. bit_mask(dig_filter->kp_width) << dig_filter->kp_shift |
  218. bit_mask(dig_filter->ka_width) << dig_filter->ka_shift);
  219. val |= ki << dig_filter->ki_shift | kp << dig_filter->kp_shift |
  220. ka << dig_filter->ka_shift;
  221. iproc_pll_write(pll, pll->control_base, dig_filter->offset, val);
  222. val = readl(pll->control_base + reset->offset);
  223. if (ctrl->flags & IPROC_CLK_PLL_RESET_ACTIVE_LOW)
  224. val &= ~(BIT(reset->reset_shift) | BIT(reset->p_reset_shift));
  225. else
  226. val |= BIT(reset->reset_shift) | BIT(reset->p_reset_shift);
  227. iproc_pll_write(pll, pll->control_base, reset->offset, val);
  228. }
  229. /*
  230. * Determines if the change to be applied to the PLL is minor (just an update
  231. * or the fractional divider). If so, then we can avoid going through a
  232. * disruptive reset and lock sequence.
  233. */
  234. static bool pll_fractional_change_only(struct iproc_pll *pll,
  235. struct iproc_pll_vco_param *vco)
  236. {
  237. const struct iproc_pll_ctrl *ctrl = pll->ctrl;
  238. u32 val;
  239. u32 ndiv_int;
  240. unsigned int pdiv;
  241. /* PLL needs to be locked */
  242. val = readl(pll->status_base + ctrl->status.offset);
  243. if ((val & (1 << ctrl->status.shift)) == 0)
  244. return false;
  245. val = readl(pll->control_base + ctrl->ndiv_int.offset);
  246. ndiv_int = (val >> ctrl->ndiv_int.shift) &
  247. bit_mask(ctrl->ndiv_int.width);
  248. if (ndiv_int != vco->ndiv_int)
  249. return false;
  250. val = readl(pll->control_base + ctrl->pdiv.offset);
  251. pdiv = (val >> ctrl->pdiv.shift) & bit_mask(ctrl->pdiv.width);
  252. if (pdiv != vco->pdiv)
  253. return false;
  254. return true;
  255. }
  256. static int pll_set_rate(struct iproc_clk *clk, struct iproc_pll_vco_param *vco,
  257. unsigned long parent_rate)
  258. {
  259. struct iproc_pll *pll = clk->pll;
  260. const struct iproc_pll_ctrl *ctrl = pll->ctrl;
  261. int ka = 0, ki, kp, ret;
  262. unsigned long rate = vco->rate;
  263. u32 val;
  264. enum kp_band kp_index;
  265. unsigned long ref_freq;
  266. const char *clk_name = clk_hw_get_name(&clk->hw);
  267. /*
  268. * reference frequency = parent frequency / PDIV
  269. * If PDIV = 0, then it becomes a multiplier (x2)
  270. */
  271. if (vco->pdiv == 0)
  272. ref_freq = parent_rate * 2;
  273. else
  274. ref_freq = parent_rate / vco->pdiv;
  275. /* determine Ki and Kp index based on target VCO frequency */
  276. if (rate >= VCO_LOW && rate < VCO_HIGH) {
  277. ki = 4;
  278. kp_index = KP_BAND_MID;
  279. } else if (rate >= VCO_HIGH && rate < VCO_HIGH_HIGH) {
  280. ki = 3;
  281. kp_index = KP_BAND_HIGH;
  282. } else if (rate >= VCO_HIGH_HIGH && rate < VCO_MAX) {
  283. ki = 3;
  284. kp_index = KP_BAND_HIGH_HIGH;
  285. } else {
  286. pr_err("%s: pll: %s has invalid rate: %lu\n", __func__,
  287. clk_name, rate);
  288. return -EINVAL;
  289. }
  290. kp = get_kp(ref_freq, kp_index);
  291. if (kp < 0) {
  292. pr_err("%s: pll: %s has invalid kp\n", __func__, clk_name);
  293. return kp;
  294. }
  295. ret = __pll_enable(pll);
  296. if (ret) {
  297. pr_err("%s: pll: %s fails to enable\n", __func__, clk_name);
  298. return ret;
  299. }
  300. if (pll_fractional_change_only(clk->pll, vco)) {
  301. /* program fractional part of NDIV */
  302. if (ctrl->flags & IPROC_CLK_PLL_HAS_NDIV_FRAC) {
  303. val = readl(pll->control_base + ctrl->ndiv_frac.offset);
  304. val &= ~(bit_mask(ctrl->ndiv_frac.width) <<
  305. ctrl->ndiv_frac.shift);
  306. val |= vco->ndiv_frac << ctrl->ndiv_frac.shift;
  307. iproc_pll_write(pll, pll->control_base,
  308. ctrl->ndiv_frac.offset, val);
  309. return 0;
  310. }
  311. }
  312. /* put PLL in reset */
  313. __pll_put_in_reset(pll);
  314. /* set PLL in user mode before modifying PLL controls */
  315. if (ctrl->flags & IPROC_CLK_PLL_USER_MODE_ON) {
  316. val = readl(pll->control_base + ctrl->macro_mode.offset);
  317. val &= ~(bit_mask(ctrl->macro_mode.width) <<
  318. ctrl->macro_mode.shift);
  319. val |= PLL_USER_MODE << ctrl->macro_mode.shift;
  320. iproc_pll_write(pll, pll->control_base,
  321. ctrl->macro_mode.offset, val);
  322. }
  323. iproc_pll_write(pll, pll->control_base, ctrl->vco_ctrl.u_offset, 0);
  324. val = readl(pll->control_base + ctrl->vco_ctrl.l_offset);
  325. if (rate >= VCO_LOW && rate < VCO_MID)
  326. val |= (1 << PLL_VCO_LOW_SHIFT);
  327. if (rate < VCO_HIGH)
  328. val &= ~(1 << PLL_VCO_HIGH_SHIFT);
  329. else
  330. val |= (1 << PLL_VCO_HIGH_SHIFT);
  331. iproc_pll_write(pll, pll->control_base, ctrl->vco_ctrl.l_offset, val);
  332. /* program integer part of NDIV */
  333. val = readl(pll->control_base + ctrl->ndiv_int.offset);
  334. val &= ~(bit_mask(ctrl->ndiv_int.width) << ctrl->ndiv_int.shift);
  335. val |= vco->ndiv_int << ctrl->ndiv_int.shift;
  336. iproc_pll_write(pll, pll->control_base, ctrl->ndiv_int.offset, val);
  337. /* program fractional part of NDIV */
  338. if (ctrl->flags & IPROC_CLK_PLL_HAS_NDIV_FRAC) {
  339. val = readl(pll->control_base + ctrl->ndiv_frac.offset);
  340. val &= ~(bit_mask(ctrl->ndiv_frac.width) <<
  341. ctrl->ndiv_frac.shift);
  342. val |= vco->ndiv_frac << ctrl->ndiv_frac.shift;
  343. iproc_pll_write(pll, pll->control_base, ctrl->ndiv_frac.offset,
  344. val);
  345. }
  346. /* program PDIV */
  347. val = readl(pll->control_base + ctrl->pdiv.offset);
  348. val &= ~(bit_mask(ctrl->pdiv.width) << ctrl->pdiv.shift);
  349. val |= vco->pdiv << ctrl->pdiv.shift;
  350. iproc_pll_write(pll, pll->control_base, ctrl->pdiv.offset, val);
  351. __pll_bring_out_reset(pll, kp, ka, ki);
  352. ret = pll_wait_for_lock(pll);
  353. if (ret < 0) {
  354. pr_err("%s: pll: %s failed to lock\n", __func__, clk_name);
  355. return ret;
  356. }
  357. return 0;
  358. }
  359. static int iproc_pll_enable(struct clk_hw *hw)
  360. {
  361. struct iproc_clk *clk = to_iproc_clk(hw);
  362. struct iproc_pll *pll = clk->pll;
  363. return __pll_enable(pll);
  364. }
  365. static void iproc_pll_disable(struct clk_hw *hw)
  366. {
  367. struct iproc_clk *clk = to_iproc_clk(hw);
  368. struct iproc_pll *pll = clk->pll;
  369. const struct iproc_pll_ctrl *ctrl = pll->ctrl;
  370. if (ctrl->flags & IPROC_CLK_AON)
  371. return;
  372. __pll_disable(pll);
  373. }
  374. static unsigned long iproc_pll_recalc_rate(struct clk_hw *hw,
  375. unsigned long parent_rate)
  376. {
  377. struct iproc_clk *clk = to_iproc_clk(hw);
  378. struct iproc_pll *pll = clk->pll;
  379. const struct iproc_pll_ctrl *ctrl = pll->ctrl;
  380. u32 val;
  381. u64 ndiv, ndiv_int, ndiv_frac;
  382. unsigned int pdiv;
  383. unsigned long rate;
  384. if (parent_rate == 0)
  385. return 0;
  386. /* PLL needs to be locked */
  387. val = readl(pll->status_base + ctrl->status.offset);
  388. if ((val & (1 << ctrl->status.shift)) == 0)
  389. return 0;
  390. /*
  391. * PLL output frequency =
  392. *
  393. * ((ndiv_int + ndiv_frac / 2^20) * (parent clock rate / pdiv)
  394. */
  395. val = readl(pll->control_base + ctrl->ndiv_int.offset);
  396. ndiv_int = (val >> ctrl->ndiv_int.shift) &
  397. bit_mask(ctrl->ndiv_int.width);
  398. ndiv = ndiv_int << 20;
  399. if (ctrl->flags & IPROC_CLK_PLL_HAS_NDIV_FRAC) {
  400. val = readl(pll->control_base + ctrl->ndiv_frac.offset);
  401. ndiv_frac = (val >> ctrl->ndiv_frac.shift) &
  402. bit_mask(ctrl->ndiv_frac.width);
  403. ndiv += ndiv_frac;
  404. }
  405. val = readl(pll->control_base + ctrl->pdiv.offset);
  406. pdiv = (val >> ctrl->pdiv.shift) & bit_mask(ctrl->pdiv.width);
  407. rate = (ndiv * parent_rate) >> 20;
  408. if (pdiv == 0)
  409. rate *= 2;
  410. else
  411. rate /= pdiv;
  412. return rate;
  413. }
  414. static int iproc_pll_determine_rate(struct clk_hw *hw,
  415. struct clk_rate_request *req)
  416. {
  417. unsigned int i;
  418. struct iproc_clk *clk = to_iproc_clk(hw);
  419. struct iproc_pll *pll = clk->pll;
  420. const struct iproc_pll_ctrl *ctrl = pll->ctrl;
  421. unsigned long diff, best_diff;
  422. unsigned int best_idx = 0;
  423. int ret;
  424. if (req->rate == 0 || req->best_parent_rate == 0)
  425. return -EINVAL;
  426. if (ctrl->flags & IPROC_CLK_PLL_CALC_PARAM) {
  427. struct iproc_pll_vco_param vco_param;
  428. ret = pll_calc_param(req->rate, req->best_parent_rate,
  429. &vco_param);
  430. if (ret)
  431. return ret;
  432. req->rate = vco_param.rate;
  433. return 0;
  434. }
  435. if (!pll->vco_param)
  436. return -EINVAL;
  437. best_diff = ULONG_MAX;
  438. for (i = 0; i < pll->num_vco_entries; i++) {
  439. diff = abs(req->rate - pll->vco_param[i].rate);
  440. if (diff <= best_diff) {
  441. best_diff = diff;
  442. best_idx = i;
  443. }
  444. /* break now if perfect match */
  445. if (diff == 0)
  446. break;
  447. }
  448. req->rate = pll->vco_param[best_idx].rate;
  449. return 0;
  450. }
  451. static int iproc_pll_set_rate(struct clk_hw *hw, unsigned long rate,
  452. unsigned long parent_rate)
  453. {
  454. struct iproc_clk *clk = to_iproc_clk(hw);
  455. struct iproc_pll *pll = clk->pll;
  456. const struct iproc_pll_ctrl *ctrl = pll->ctrl;
  457. struct iproc_pll_vco_param vco_param;
  458. int rate_index, ret;
  459. if (ctrl->flags & IPROC_CLK_PLL_CALC_PARAM) {
  460. ret = pll_calc_param(rate, parent_rate, &vco_param);
  461. if (ret)
  462. return ret;
  463. } else {
  464. rate_index = pll_get_rate_index(pll, rate);
  465. if (rate_index < 0)
  466. return rate_index;
  467. vco_param = pll->vco_param[rate_index];
  468. }
  469. ret = pll_set_rate(clk, &vco_param, parent_rate);
  470. return ret;
  471. }
  472. static const struct clk_ops iproc_pll_ops = {
  473. .enable = iproc_pll_enable,
  474. .disable = iproc_pll_disable,
  475. .recalc_rate = iproc_pll_recalc_rate,
  476. .determine_rate = iproc_pll_determine_rate,
  477. .set_rate = iproc_pll_set_rate,
  478. };
  479. static int iproc_clk_enable(struct clk_hw *hw)
  480. {
  481. struct iproc_clk *clk = to_iproc_clk(hw);
  482. const struct iproc_clk_ctrl *ctrl = clk->ctrl;
  483. struct iproc_pll *pll = clk->pll;
  484. u32 val;
  485. /* channel enable is active low */
  486. val = readl(pll->control_base + ctrl->enable.offset);
  487. val &= ~(1 << ctrl->enable.enable_shift);
  488. iproc_pll_write(pll, pll->control_base, ctrl->enable.offset, val);
  489. /* also make sure channel is not held */
  490. val = readl(pll->control_base + ctrl->enable.offset);
  491. val &= ~(1 << ctrl->enable.hold_shift);
  492. iproc_pll_write(pll, pll->control_base, ctrl->enable.offset, val);
  493. return 0;
  494. }
  495. static void iproc_clk_disable(struct clk_hw *hw)
  496. {
  497. struct iproc_clk *clk = to_iproc_clk(hw);
  498. const struct iproc_clk_ctrl *ctrl = clk->ctrl;
  499. struct iproc_pll *pll = clk->pll;
  500. u32 val;
  501. if (ctrl->flags & IPROC_CLK_AON)
  502. return;
  503. val = readl(pll->control_base + ctrl->enable.offset);
  504. val |= 1 << ctrl->enable.enable_shift;
  505. iproc_pll_write(pll, pll->control_base, ctrl->enable.offset, val);
  506. }
  507. static unsigned long iproc_clk_recalc_rate(struct clk_hw *hw,
  508. unsigned long parent_rate)
  509. {
  510. struct iproc_clk *clk = to_iproc_clk(hw);
  511. const struct iproc_clk_ctrl *ctrl = clk->ctrl;
  512. struct iproc_pll *pll = clk->pll;
  513. u32 val;
  514. unsigned int mdiv;
  515. unsigned long rate;
  516. if (parent_rate == 0)
  517. return 0;
  518. val = readl(pll->control_base + ctrl->mdiv.offset);
  519. mdiv = (val >> ctrl->mdiv.shift) & bit_mask(ctrl->mdiv.width);
  520. if (mdiv == 0)
  521. mdiv = 256;
  522. if (ctrl->flags & IPROC_CLK_MCLK_DIV_BY_2)
  523. rate = parent_rate / (mdiv * 2);
  524. else
  525. rate = parent_rate / mdiv;
  526. return rate;
  527. }
  528. static int iproc_clk_determine_rate(struct clk_hw *hw,
  529. struct clk_rate_request *req)
  530. {
  531. unsigned int bestdiv;
  532. if (req->rate == 0)
  533. return -EINVAL;
  534. if (req->rate == req->best_parent_rate)
  535. return 0;
  536. bestdiv = DIV_ROUND_CLOSEST(req->best_parent_rate, req->rate);
  537. if (bestdiv < 2)
  538. req->rate = req->best_parent_rate;
  539. if (bestdiv > 256)
  540. bestdiv = 256;
  541. req->rate = req->best_parent_rate / bestdiv;
  542. return 0;
  543. }
  544. static int iproc_clk_set_rate(struct clk_hw *hw, unsigned long rate,
  545. unsigned long parent_rate)
  546. {
  547. struct iproc_clk *clk = to_iproc_clk(hw);
  548. const struct iproc_clk_ctrl *ctrl = clk->ctrl;
  549. struct iproc_pll *pll = clk->pll;
  550. u32 val;
  551. unsigned int div;
  552. if (rate == 0 || parent_rate == 0)
  553. return -EINVAL;
  554. div = DIV_ROUND_CLOSEST(parent_rate, rate);
  555. if (ctrl->flags & IPROC_CLK_MCLK_DIV_BY_2)
  556. div /= 2;
  557. if (div > 256)
  558. return -EINVAL;
  559. val = readl(pll->control_base + ctrl->mdiv.offset);
  560. if (div == 256) {
  561. val &= ~(bit_mask(ctrl->mdiv.width) << ctrl->mdiv.shift);
  562. } else {
  563. val &= ~(bit_mask(ctrl->mdiv.width) << ctrl->mdiv.shift);
  564. val |= div << ctrl->mdiv.shift;
  565. }
  566. iproc_pll_write(pll, pll->control_base, ctrl->mdiv.offset, val);
  567. return 0;
  568. }
  569. static const struct clk_ops iproc_clk_ops = {
  570. .enable = iproc_clk_enable,
  571. .disable = iproc_clk_disable,
  572. .recalc_rate = iproc_clk_recalc_rate,
  573. .determine_rate = iproc_clk_determine_rate,
  574. .set_rate = iproc_clk_set_rate,
  575. };
  576. /**
  577. * Some PLLs require the PLL SW override bit to be set before changes can be
  578. * applied to the PLL
  579. */
  580. static void iproc_pll_sw_cfg(struct iproc_pll *pll)
  581. {
  582. const struct iproc_pll_ctrl *ctrl = pll->ctrl;
  583. if (ctrl->flags & IPROC_CLK_PLL_NEEDS_SW_CFG) {
  584. u32 val;
  585. val = readl(pll->control_base + ctrl->sw_ctrl.offset);
  586. val |= BIT(ctrl->sw_ctrl.shift);
  587. iproc_pll_write(pll, pll->control_base, ctrl->sw_ctrl.offset,
  588. val);
  589. }
  590. }
  591. void iproc_pll_clk_setup(struct device_node *node,
  592. const struct iproc_pll_ctrl *pll_ctrl,
  593. const struct iproc_pll_vco_param *vco,
  594. unsigned int num_vco_entries,
  595. const struct iproc_clk_ctrl *clk_ctrl,
  596. unsigned int num_clks)
  597. {
  598. int i, ret;
  599. struct iproc_pll *pll;
  600. struct iproc_clk *iclk;
  601. struct clk_init_data init;
  602. const char *parent_name;
  603. struct iproc_clk *iclk_array;
  604. struct clk_hw_onecell_data *clk_data;
  605. if (WARN_ON(!pll_ctrl) || WARN_ON(!clk_ctrl))
  606. return;
  607. pll = kzalloc(sizeof(*pll), GFP_KERNEL);
  608. if (WARN_ON(!pll))
  609. return;
  610. clk_data = kzalloc(struct_size(clk_data, hws, num_clks), GFP_KERNEL);
  611. if (WARN_ON(!clk_data))
  612. goto err_clk_data;
  613. clk_data->num = num_clks;
  614. iclk_array = kcalloc(num_clks, sizeof(struct iproc_clk), GFP_KERNEL);
  615. if (WARN_ON(!iclk_array))
  616. goto err_clks;
  617. pll->control_base = of_iomap(node, 0);
  618. if (WARN_ON(!pll->control_base))
  619. goto err_pll_iomap;
  620. /* Some SoCs do not require the pwr_base, thus failing is not fatal */
  621. pll->pwr_base = of_iomap(node, 1);
  622. /* some PLLs require gating control at the top ASIU level */
  623. if (pll_ctrl->flags & IPROC_CLK_PLL_ASIU) {
  624. pll->asiu_base = of_iomap(node, 2);
  625. if (WARN_ON(!pll->asiu_base))
  626. goto err_asiu_iomap;
  627. }
  628. if (pll_ctrl->flags & IPROC_CLK_PLL_SPLIT_STAT_CTRL) {
  629. /* Some SoCs have a split status/control. If this does not
  630. * exist, assume they are unified.
  631. */
  632. pll->status_base = of_iomap(node, 2);
  633. if (!pll->status_base)
  634. goto err_status_iomap;
  635. } else
  636. pll->status_base = pll->control_base;
  637. /* initialize and register the PLL itself */
  638. pll->ctrl = pll_ctrl;
  639. iclk = &iclk_array[0];
  640. iclk->pll = pll;
  641. init.name = node->name;
  642. init.ops = &iproc_pll_ops;
  643. init.flags = 0;
  644. parent_name = of_clk_get_parent_name(node, 0);
  645. init.parent_names = (parent_name ? &parent_name : NULL);
  646. init.num_parents = (parent_name ? 1 : 0);
  647. iclk->hw.init = &init;
  648. if (vco) {
  649. pll->num_vco_entries = num_vco_entries;
  650. pll->vco_param = vco;
  651. }
  652. iproc_pll_sw_cfg(pll);
  653. ret = clk_hw_register(NULL, &iclk->hw);
  654. if (WARN_ON(ret))
  655. goto err_pll_register;
  656. clk_data->hws[0] = &iclk->hw;
  657. /* now initialize and register all leaf clocks */
  658. for (i = 1; i < num_clks; i++) {
  659. const char *clk_name;
  660. memset(&init, 0, sizeof(init));
  661. parent_name = node->name;
  662. ret = of_property_read_string_index(node, "clock-output-names",
  663. i, &clk_name);
  664. if (WARN_ON(ret))
  665. goto err_clk_register;
  666. iclk = &iclk_array[i];
  667. iclk->pll = pll;
  668. iclk->ctrl = &clk_ctrl[i];
  669. init.name = clk_name;
  670. init.ops = &iproc_clk_ops;
  671. init.flags = 0;
  672. init.parent_names = (parent_name ? &parent_name : NULL);
  673. init.num_parents = (parent_name ? 1 : 0);
  674. iclk->hw.init = &init;
  675. ret = clk_hw_register(NULL, &iclk->hw);
  676. if (WARN_ON(ret))
  677. goto err_clk_register;
  678. clk_data->hws[i] = &iclk->hw;
  679. }
  680. ret = of_clk_add_hw_provider(node, of_clk_hw_onecell_get, clk_data);
  681. if (WARN_ON(ret))
  682. goto err_clk_register;
  683. return;
  684. err_clk_register:
  685. while (--i >= 0)
  686. clk_hw_unregister(clk_data->hws[i]);
  687. err_pll_register:
  688. if (pll->status_base != pll->control_base)
  689. iounmap(pll->status_base);
  690. err_status_iomap:
  691. if (pll->asiu_base)
  692. iounmap(pll->asiu_base);
  693. err_asiu_iomap:
  694. if (pll->pwr_base)
  695. iounmap(pll->pwr_base);
  696. iounmap(pll->control_base);
  697. err_pll_iomap:
  698. kfree(iclk_array);
  699. err_clks:
  700. kfree(clk_data);
  701. err_clk_data:
  702. kfree(pll);
  703. }