clk-iproc-pll.c 22 KB

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