clk-iproc-pll.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  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. /* number of delay loops waiting for PLL to lock */
  25. #define LOCK_DELAY 100
  26. /* number of VCO frequency bands */
  27. #define NUM_FREQ_BANDS 8
  28. #define NUM_KP_BANDS 3
  29. enum kp_band {
  30. KP_BAND_MID = 0,
  31. KP_BAND_HIGH,
  32. KP_BAND_HIGH_HIGH
  33. };
  34. static const unsigned int kp_table[NUM_KP_BANDS][NUM_FREQ_BANDS] = {
  35. { 5, 6, 6, 7, 7, 8, 9, 10 },
  36. { 4, 4, 5, 5, 6, 7, 8, 9 },
  37. { 4, 5, 5, 6, 7, 8, 9, 10 },
  38. };
  39. static const unsigned long ref_freq_table[NUM_FREQ_BANDS][2] = {
  40. { 10000000, 12500000 },
  41. { 12500000, 15000000 },
  42. { 15000000, 20000000 },
  43. { 20000000, 25000000 },
  44. { 25000000, 50000000 },
  45. { 50000000, 75000000 },
  46. { 75000000, 100000000 },
  47. { 100000000, 125000000 },
  48. };
  49. enum vco_freq_range {
  50. VCO_LOW = 700000000U,
  51. VCO_MID = 1200000000U,
  52. VCO_HIGH = 2200000000U,
  53. VCO_HIGH_HIGH = 3100000000U,
  54. VCO_MAX = 4000000000U,
  55. };
  56. struct iproc_pll;
  57. struct iproc_clk {
  58. struct clk_hw hw;
  59. const char *name;
  60. struct iproc_pll *pll;
  61. unsigned long rate;
  62. const struct iproc_clk_ctrl *ctrl;
  63. };
  64. struct iproc_pll {
  65. void __iomem *status_base;
  66. void __iomem *control_base;
  67. void __iomem *pwr_base;
  68. void __iomem *asiu_base;
  69. const struct iproc_pll_ctrl *ctrl;
  70. const struct iproc_pll_vco_param *vco_param;
  71. unsigned int num_vco_entries;
  72. struct clk_onecell_data clk_data;
  73. struct iproc_clk *clks;
  74. };
  75. #define to_iproc_clk(hw) container_of(hw, struct iproc_clk, hw)
  76. /*
  77. * Based on the target frequency, find a match from the VCO frequency parameter
  78. * table and return its index
  79. */
  80. static int pll_get_rate_index(struct iproc_pll *pll, unsigned int target_rate)
  81. {
  82. int i;
  83. for (i = 0; i < pll->num_vco_entries; i++)
  84. if (target_rate == pll->vco_param[i].rate)
  85. break;
  86. if (i >= pll->num_vco_entries)
  87. return -EINVAL;
  88. return i;
  89. }
  90. static int get_kp(unsigned long ref_freq, enum kp_band kp_index)
  91. {
  92. int i;
  93. if (ref_freq < ref_freq_table[0][0])
  94. return -EINVAL;
  95. for (i = 0; i < NUM_FREQ_BANDS; i++) {
  96. if (ref_freq >= ref_freq_table[i][0] &&
  97. ref_freq < ref_freq_table[i][1])
  98. return kp_table[kp_index][i];
  99. }
  100. return -EINVAL;
  101. }
  102. static int pll_wait_for_lock(struct iproc_pll *pll)
  103. {
  104. int i;
  105. const struct iproc_pll_ctrl *ctrl = pll->ctrl;
  106. for (i = 0; i < LOCK_DELAY; i++) {
  107. u32 val = readl(pll->status_base + ctrl->status.offset);
  108. if (val & (1 << ctrl->status.shift))
  109. return 0;
  110. udelay(10);
  111. }
  112. return -EIO;
  113. }
  114. static void iproc_pll_write(const struct iproc_pll *pll, void __iomem *base,
  115. const u32 offset, u32 val)
  116. {
  117. const struct iproc_pll_ctrl *ctrl = pll->ctrl;
  118. writel(val, base + offset);
  119. if (unlikely(ctrl->flags & IPROC_CLK_NEEDS_READ_BACK &&
  120. (base == pll->status_base || base == pll->control_base)))
  121. val = readl(base + offset);
  122. }
  123. static void __pll_disable(struct iproc_pll *pll)
  124. {
  125. const struct iproc_pll_ctrl *ctrl = pll->ctrl;
  126. u32 val;
  127. if (ctrl->flags & IPROC_CLK_PLL_ASIU) {
  128. val = readl(pll->asiu_base + ctrl->asiu.offset);
  129. val &= ~(1 << ctrl->asiu.en_shift);
  130. iproc_pll_write(pll, pll->asiu_base, ctrl->asiu.offset, val);
  131. }
  132. if (ctrl->flags & IPROC_CLK_EMBED_PWRCTRL) {
  133. val = readl(pll->control_base + ctrl->aon.offset);
  134. val |= bit_mask(ctrl->aon.pwr_width) << ctrl->aon.pwr_shift;
  135. iproc_pll_write(pll, pll->control_base, ctrl->aon.offset, val);
  136. }
  137. if (pll->pwr_base) {
  138. /* latch input value so core power can be shut down */
  139. val = readl(pll->pwr_base + ctrl->aon.offset);
  140. val |= 1 << ctrl->aon.iso_shift;
  141. iproc_pll_write(pll, pll->pwr_base, ctrl->aon.offset, val);
  142. /* power down the core */
  143. val &= ~(bit_mask(ctrl->aon.pwr_width) << ctrl->aon.pwr_shift);
  144. iproc_pll_write(pll, pll->pwr_base, ctrl->aon.offset, val);
  145. }
  146. }
  147. static int __pll_enable(struct iproc_pll *pll)
  148. {
  149. const struct iproc_pll_ctrl *ctrl = pll->ctrl;
  150. u32 val;
  151. if (ctrl->flags & IPROC_CLK_EMBED_PWRCTRL) {
  152. val = readl(pll->control_base + ctrl->aon.offset);
  153. val &= ~(bit_mask(ctrl->aon.pwr_width) << ctrl->aon.pwr_shift);
  154. iproc_pll_write(pll, pll->control_base, ctrl->aon.offset, val);
  155. }
  156. if (pll->pwr_base) {
  157. /* power up the PLL and make sure it's not latched */
  158. val = readl(pll->pwr_base + ctrl->aon.offset);
  159. val |= bit_mask(ctrl->aon.pwr_width) << ctrl->aon.pwr_shift;
  160. val &= ~(1 << ctrl->aon.iso_shift);
  161. iproc_pll_write(pll, pll->pwr_base, ctrl->aon.offset, val);
  162. }
  163. /* certain PLLs also need to be ungated from the ASIU top level */
  164. if (ctrl->flags & IPROC_CLK_PLL_ASIU) {
  165. val = readl(pll->asiu_base + ctrl->asiu.offset);
  166. val |= (1 << ctrl->asiu.en_shift);
  167. iproc_pll_write(pll, pll->asiu_base, ctrl->asiu.offset, val);
  168. }
  169. return 0;
  170. }
  171. static void __pll_put_in_reset(struct iproc_pll *pll)
  172. {
  173. u32 val;
  174. const struct iproc_pll_ctrl *ctrl = pll->ctrl;
  175. const struct iproc_pll_reset_ctrl *reset = &ctrl->reset;
  176. val = readl(pll->control_base + reset->offset);
  177. val &= ~(1 << reset->reset_shift | 1 << reset->p_reset_shift);
  178. iproc_pll_write(pll, pll->control_base, reset->offset, val);
  179. }
  180. static void __pll_bring_out_reset(struct iproc_pll *pll, unsigned int kp,
  181. unsigned int ka, unsigned int ki)
  182. {
  183. u32 val;
  184. const struct iproc_pll_ctrl *ctrl = pll->ctrl;
  185. const struct iproc_pll_reset_ctrl *reset = &ctrl->reset;
  186. const struct iproc_pll_dig_filter_ctrl *dig_filter = &ctrl->dig_filter;
  187. val = readl(pll->control_base + dig_filter->offset);
  188. val &= ~(bit_mask(dig_filter->ki_width) << dig_filter->ki_shift |
  189. bit_mask(dig_filter->kp_width) << dig_filter->kp_shift |
  190. bit_mask(dig_filter->ka_width) << dig_filter->ka_shift);
  191. val |= ki << dig_filter->ki_shift | kp << dig_filter->kp_shift |
  192. ka << dig_filter->ka_shift;
  193. iproc_pll_write(pll, pll->control_base, dig_filter->offset, val);
  194. val = readl(pll->control_base + reset->offset);
  195. val |= 1 << reset->reset_shift | 1 << reset->p_reset_shift;
  196. iproc_pll_write(pll, pll->control_base, reset->offset, val);
  197. }
  198. static int pll_set_rate(struct iproc_clk *clk, unsigned int rate_index,
  199. unsigned long parent_rate)
  200. {
  201. struct iproc_pll *pll = clk->pll;
  202. const struct iproc_pll_vco_param *vco = &pll->vco_param[rate_index];
  203. const struct iproc_pll_ctrl *ctrl = pll->ctrl;
  204. int ka = 0, ki, kp, ret;
  205. unsigned long rate = vco->rate;
  206. u32 val;
  207. enum kp_band kp_index;
  208. unsigned long ref_freq;
  209. /*
  210. * reference frequency = parent frequency / PDIV
  211. * If PDIV = 0, then it becomes a multiplier (x2)
  212. */
  213. if (vco->pdiv == 0)
  214. ref_freq = parent_rate * 2;
  215. else
  216. ref_freq = parent_rate / vco->pdiv;
  217. /* determine Ki and Kp index based on target VCO frequency */
  218. if (rate >= VCO_LOW && rate < VCO_HIGH) {
  219. ki = 4;
  220. kp_index = KP_BAND_MID;
  221. } else if (rate >= VCO_HIGH && rate && rate < VCO_HIGH_HIGH) {
  222. ki = 3;
  223. kp_index = KP_BAND_HIGH;
  224. } else if (rate >= VCO_HIGH_HIGH && rate < VCO_MAX) {
  225. ki = 3;
  226. kp_index = KP_BAND_HIGH_HIGH;
  227. } else {
  228. pr_err("%s: pll: %s has invalid rate: %lu\n", __func__,
  229. clk->name, rate);
  230. return -EINVAL;
  231. }
  232. kp = get_kp(ref_freq, kp_index);
  233. if (kp < 0) {
  234. pr_err("%s: pll: %s has invalid kp\n", __func__, clk->name);
  235. return kp;
  236. }
  237. ret = __pll_enable(pll);
  238. if (ret) {
  239. pr_err("%s: pll: %s fails to enable\n", __func__, clk->name);
  240. return ret;
  241. }
  242. /* put PLL in reset */
  243. __pll_put_in_reset(pll);
  244. iproc_pll_write(pll, pll->control_base, ctrl->vco_ctrl.u_offset, 0);
  245. val = readl(pll->control_base + ctrl->vco_ctrl.l_offset);
  246. if (rate >= VCO_LOW && rate < VCO_MID)
  247. val |= (1 << PLL_VCO_LOW_SHIFT);
  248. if (rate < VCO_HIGH)
  249. val &= ~(1 << PLL_VCO_HIGH_SHIFT);
  250. else
  251. val |= (1 << PLL_VCO_HIGH_SHIFT);
  252. iproc_pll_write(pll, pll->control_base, ctrl->vco_ctrl.l_offset, val);
  253. /* program integer part of NDIV */
  254. val = readl(pll->control_base + ctrl->ndiv_int.offset);
  255. val &= ~(bit_mask(ctrl->ndiv_int.width) << ctrl->ndiv_int.shift);
  256. val |= vco->ndiv_int << ctrl->ndiv_int.shift;
  257. iproc_pll_write(pll, pll->control_base, ctrl->ndiv_int.offset, val);
  258. /* program fractional part of NDIV */
  259. if (ctrl->flags & IPROC_CLK_PLL_HAS_NDIV_FRAC) {
  260. val = readl(pll->control_base + ctrl->ndiv_frac.offset);
  261. val &= ~(bit_mask(ctrl->ndiv_frac.width) <<
  262. ctrl->ndiv_frac.shift);
  263. val |= vco->ndiv_frac << ctrl->ndiv_frac.shift;
  264. iproc_pll_write(pll, pll->control_base, ctrl->ndiv_frac.offset,
  265. val);
  266. }
  267. /* program PDIV */
  268. val = readl(pll->control_base + ctrl->pdiv.offset);
  269. val &= ~(bit_mask(ctrl->pdiv.width) << ctrl->pdiv.shift);
  270. val |= vco->pdiv << ctrl->pdiv.shift;
  271. iproc_pll_write(pll, pll->control_base, ctrl->pdiv.offset, val);
  272. __pll_bring_out_reset(pll, kp, ka, ki);
  273. ret = pll_wait_for_lock(pll);
  274. if (ret < 0) {
  275. pr_err("%s: pll: %s failed to lock\n", __func__, clk->name);
  276. return ret;
  277. }
  278. return 0;
  279. }
  280. static int iproc_pll_enable(struct clk_hw *hw)
  281. {
  282. struct iproc_clk *clk = to_iproc_clk(hw);
  283. struct iproc_pll *pll = clk->pll;
  284. return __pll_enable(pll);
  285. }
  286. static void iproc_pll_disable(struct clk_hw *hw)
  287. {
  288. struct iproc_clk *clk = to_iproc_clk(hw);
  289. struct iproc_pll *pll = clk->pll;
  290. const struct iproc_pll_ctrl *ctrl = pll->ctrl;
  291. if (ctrl->flags & IPROC_CLK_AON)
  292. return;
  293. __pll_disable(pll);
  294. }
  295. static unsigned long iproc_pll_recalc_rate(struct clk_hw *hw,
  296. unsigned long parent_rate)
  297. {
  298. struct iproc_clk *clk = to_iproc_clk(hw);
  299. struct iproc_pll *pll = clk->pll;
  300. const struct iproc_pll_ctrl *ctrl = pll->ctrl;
  301. u32 val;
  302. u64 ndiv, ndiv_int, ndiv_frac;
  303. unsigned int pdiv;
  304. if (parent_rate == 0)
  305. return 0;
  306. /* PLL needs to be locked */
  307. val = readl(pll->status_base + ctrl->status.offset);
  308. if ((val & (1 << ctrl->status.shift)) == 0) {
  309. clk->rate = 0;
  310. return 0;
  311. }
  312. /*
  313. * PLL output frequency =
  314. *
  315. * ((ndiv_int + ndiv_frac / 2^20) * (parent clock rate / pdiv)
  316. */
  317. val = readl(pll->control_base + ctrl->ndiv_int.offset);
  318. ndiv_int = (val >> ctrl->ndiv_int.shift) &
  319. bit_mask(ctrl->ndiv_int.width);
  320. ndiv = ndiv_int << 20;
  321. if (ctrl->flags & IPROC_CLK_PLL_HAS_NDIV_FRAC) {
  322. val = readl(pll->control_base + ctrl->ndiv_frac.offset);
  323. ndiv_frac = (val >> ctrl->ndiv_frac.shift) &
  324. bit_mask(ctrl->ndiv_frac.width);
  325. ndiv += ndiv_frac;
  326. }
  327. val = readl(pll->control_base + ctrl->pdiv.offset);
  328. pdiv = (val >> ctrl->pdiv.shift) & bit_mask(ctrl->pdiv.width);
  329. clk->rate = (ndiv * parent_rate) >> 20;
  330. if (pdiv == 0)
  331. clk->rate *= 2;
  332. else
  333. clk->rate /= pdiv;
  334. return clk->rate;
  335. }
  336. static long iproc_pll_round_rate(struct clk_hw *hw, unsigned long rate,
  337. unsigned long *parent_rate)
  338. {
  339. unsigned i;
  340. struct iproc_clk *clk = to_iproc_clk(hw);
  341. struct iproc_pll *pll = clk->pll;
  342. if (rate == 0 || *parent_rate == 0 || !pll->vco_param)
  343. return -EINVAL;
  344. for (i = 0; i < pll->num_vco_entries; i++) {
  345. if (rate <= pll->vco_param[i].rate)
  346. break;
  347. }
  348. if (i == pll->num_vco_entries)
  349. i--;
  350. return pll->vco_param[i].rate;
  351. }
  352. static int iproc_pll_set_rate(struct clk_hw *hw, unsigned long rate,
  353. unsigned long parent_rate)
  354. {
  355. struct iproc_clk *clk = to_iproc_clk(hw);
  356. struct iproc_pll *pll = clk->pll;
  357. int rate_index, ret;
  358. rate_index = pll_get_rate_index(pll, rate);
  359. if (rate_index < 0)
  360. return rate_index;
  361. ret = pll_set_rate(clk, rate_index, parent_rate);
  362. return ret;
  363. }
  364. static const struct clk_ops iproc_pll_ops = {
  365. .enable = iproc_pll_enable,
  366. .disable = iproc_pll_disable,
  367. .recalc_rate = iproc_pll_recalc_rate,
  368. .round_rate = iproc_pll_round_rate,
  369. .set_rate = iproc_pll_set_rate,
  370. };
  371. static int iproc_clk_enable(struct clk_hw *hw)
  372. {
  373. struct iproc_clk *clk = to_iproc_clk(hw);
  374. const struct iproc_clk_ctrl *ctrl = clk->ctrl;
  375. struct iproc_pll *pll = clk->pll;
  376. u32 val;
  377. /* channel enable is active low */
  378. val = readl(pll->control_base + ctrl->enable.offset);
  379. val &= ~(1 << ctrl->enable.enable_shift);
  380. iproc_pll_write(pll, pll->control_base, ctrl->enable.offset, val);
  381. /* also make sure channel is not held */
  382. val = readl(pll->control_base + ctrl->enable.offset);
  383. val &= ~(1 << ctrl->enable.hold_shift);
  384. iproc_pll_write(pll, pll->control_base, ctrl->enable.offset, val);
  385. return 0;
  386. }
  387. static void iproc_clk_disable(struct clk_hw *hw)
  388. {
  389. struct iproc_clk *clk = to_iproc_clk(hw);
  390. const struct iproc_clk_ctrl *ctrl = clk->ctrl;
  391. struct iproc_pll *pll = clk->pll;
  392. u32 val;
  393. if (ctrl->flags & IPROC_CLK_AON)
  394. return;
  395. val = readl(pll->control_base + ctrl->enable.offset);
  396. val |= 1 << ctrl->enable.enable_shift;
  397. iproc_pll_write(pll, pll->control_base, ctrl->enable.offset, val);
  398. }
  399. static unsigned long iproc_clk_recalc_rate(struct clk_hw *hw,
  400. unsigned long parent_rate)
  401. {
  402. struct iproc_clk *clk = to_iproc_clk(hw);
  403. const struct iproc_clk_ctrl *ctrl = clk->ctrl;
  404. struct iproc_pll *pll = clk->pll;
  405. u32 val;
  406. unsigned int mdiv;
  407. if (parent_rate == 0)
  408. return 0;
  409. val = readl(pll->control_base + ctrl->mdiv.offset);
  410. mdiv = (val >> ctrl->mdiv.shift) & bit_mask(ctrl->mdiv.width);
  411. if (mdiv == 0)
  412. mdiv = 256;
  413. clk->rate = parent_rate / mdiv;
  414. return clk->rate;
  415. }
  416. static long iproc_clk_round_rate(struct clk_hw *hw, unsigned long rate,
  417. unsigned long *parent_rate)
  418. {
  419. unsigned int div;
  420. if (rate == 0 || *parent_rate == 0)
  421. return -EINVAL;
  422. if (rate == *parent_rate)
  423. return *parent_rate;
  424. div = DIV_ROUND_UP(*parent_rate, rate);
  425. if (div < 2)
  426. return *parent_rate;
  427. if (div > 256)
  428. div = 256;
  429. return *parent_rate / div;
  430. }
  431. static int iproc_clk_set_rate(struct clk_hw *hw, unsigned long rate,
  432. unsigned long parent_rate)
  433. {
  434. struct iproc_clk *clk = to_iproc_clk(hw);
  435. const struct iproc_clk_ctrl *ctrl = clk->ctrl;
  436. struct iproc_pll *pll = clk->pll;
  437. u32 val;
  438. unsigned int div;
  439. if (rate == 0 || parent_rate == 0)
  440. return -EINVAL;
  441. div = DIV_ROUND_UP(parent_rate, rate);
  442. if (div > 256)
  443. return -EINVAL;
  444. val = readl(pll->control_base + ctrl->mdiv.offset);
  445. if (div == 256) {
  446. val &= ~(bit_mask(ctrl->mdiv.width) << ctrl->mdiv.shift);
  447. } else {
  448. val &= ~(bit_mask(ctrl->mdiv.width) << ctrl->mdiv.shift);
  449. val |= div << ctrl->mdiv.shift;
  450. }
  451. iproc_pll_write(pll, pll->control_base, ctrl->mdiv.offset, val);
  452. clk->rate = parent_rate / div;
  453. return 0;
  454. }
  455. static const struct clk_ops iproc_clk_ops = {
  456. .enable = iproc_clk_enable,
  457. .disable = iproc_clk_disable,
  458. .recalc_rate = iproc_clk_recalc_rate,
  459. .round_rate = iproc_clk_round_rate,
  460. .set_rate = iproc_clk_set_rate,
  461. };
  462. /**
  463. * Some PLLs require the PLL SW override bit to be set before changes can be
  464. * applied to the PLL
  465. */
  466. static void iproc_pll_sw_cfg(struct iproc_pll *pll)
  467. {
  468. const struct iproc_pll_ctrl *ctrl = pll->ctrl;
  469. if (ctrl->flags & IPROC_CLK_PLL_NEEDS_SW_CFG) {
  470. u32 val;
  471. val = readl(pll->control_base + ctrl->sw_ctrl.offset);
  472. val |= BIT(ctrl->sw_ctrl.shift);
  473. iproc_pll_write(pll, pll->control_base, ctrl->sw_ctrl.offset,
  474. val);
  475. }
  476. }
  477. void __init iproc_pll_clk_setup(struct device_node *node,
  478. const struct iproc_pll_ctrl *pll_ctrl,
  479. const struct iproc_pll_vco_param *vco,
  480. unsigned int num_vco_entries,
  481. const struct iproc_clk_ctrl *clk_ctrl,
  482. unsigned int num_clks)
  483. {
  484. int i, ret;
  485. struct clk *clk;
  486. struct iproc_pll *pll;
  487. struct iproc_clk *iclk;
  488. struct clk_init_data init;
  489. const char *parent_name;
  490. if (WARN_ON(!pll_ctrl) || WARN_ON(!clk_ctrl))
  491. return;
  492. pll = kzalloc(sizeof(*pll), GFP_KERNEL);
  493. if (WARN_ON(!pll))
  494. return;
  495. pll->clk_data.clk_num = num_clks;
  496. pll->clk_data.clks = kcalloc(num_clks, sizeof(*pll->clk_data.clks),
  497. GFP_KERNEL);
  498. if (WARN_ON(!pll->clk_data.clks))
  499. goto err_clk_data;
  500. pll->clks = kcalloc(num_clks, sizeof(*pll->clks), GFP_KERNEL);
  501. if (WARN_ON(!pll->clks))
  502. goto err_clks;
  503. pll->control_base = of_iomap(node, 0);
  504. if (WARN_ON(!pll->control_base))
  505. goto err_pll_iomap;
  506. /* Some SoCs do not require the pwr_base, thus failing is not fatal */
  507. pll->pwr_base = of_iomap(node, 1);
  508. /* some PLLs require gating control at the top ASIU level */
  509. if (pll_ctrl->flags & IPROC_CLK_PLL_ASIU) {
  510. pll->asiu_base = of_iomap(node, 2);
  511. if (WARN_ON(!pll->asiu_base))
  512. goto err_asiu_iomap;
  513. }
  514. if (pll_ctrl->flags & IPROC_CLK_PLL_SPLIT_STAT_CTRL) {
  515. /* Some SoCs have a split status/control. If this does not
  516. * exist, assume they are unified.
  517. */
  518. pll->status_base = of_iomap(node, 2);
  519. if (!pll->status_base)
  520. goto err_status_iomap;
  521. } else
  522. pll->status_base = pll->control_base;
  523. /* initialize and register the PLL itself */
  524. pll->ctrl = pll_ctrl;
  525. iclk = &pll->clks[0];
  526. iclk->pll = pll;
  527. iclk->name = node->name;
  528. init.name = node->name;
  529. init.ops = &iproc_pll_ops;
  530. init.flags = 0;
  531. parent_name = of_clk_get_parent_name(node, 0);
  532. init.parent_names = (parent_name ? &parent_name : NULL);
  533. init.num_parents = (parent_name ? 1 : 0);
  534. iclk->hw.init = &init;
  535. if (vco) {
  536. pll->num_vco_entries = num_vco_entries;
  537. pll->vco_param = vco;
  538. }
  539. iproc_pll_sw_cfg(pll);
  540. clk = clk_register(NULL, &iclk->hw);
  541. if (WARN_ON(IS_ERR(clk)))
  542. goto err_pll_register;
  543. pll->clk_data.clks[0] = clk;
  544. /* now initialize and register all leaf clocks */
  545. for (i = 1; i < num_clks; i++) {
  546. const char *clk_name;
  547. memset(&init, 0, sizeof(init));
  548. parent_name = node->name;
  549. ret = of_property_read_string_index(node, "clock-output-names",
  550. i, &clk_name);
  551. if (WARN_ON(ret))
  552. goto err_clk_register;
  553. iclk = &pll->clks[i];
  554. iclk->name = clk_name;
  555. iclk->pll = pll;
  556. iclk->ctrl = &clk_ctrl[i];
  557. init.name = clk_name;
  558. init.ops = &iproc_clk_ops;
  559. init.flags = 0;
  560. init.parent_names = (parent_name ? &parent_name : NULL);
  561. init.num_parents = (parent_name ? 1 : 0);
  562. iclk->hw.init = &init;
  563. clk = clk_register(NULL, &iclk->hw);
  564. if (WARN_ON(IS_ERR(clk)))
  565. goto err_clk_register;
  566. pll->clk_data.clks[i] = clk;
  567. }
  568. ret = of_clk_add_provider(node, of_clk_src_onecell_get, &pll->clk_data);
  569. if (WARN_ON(ret))
  570. goto err_clk_register;
  571. return;
  572. err_clk_register:
  573. for (i = 0; i < num_clks; i++)
  574. clk_unregister(pll->clk_data.clks[i]);
  575. err_pll_register:
  576. if (pll->status_base != pll->control_base)
  577. iounmap(pll->status_base);
  578. err_status_iomap:
  579. if (pll->asiu_base)
  580. iounmap(pll->asiu_base);
  581. err_asiu_iomap:
  582. if (pll->pwr_base)
  583. iounmap(pll->pwr_base);
  584. iounmap(pll->control_base);
  585. err_pll_iomap:
  586. kfree(pll->clks);
  587. err_clks:
  588. kfree(pll->clk_data.clks);
  589. err_clk_data:
  590. kfree(pll);
  591. }