clkgen-pll.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  1. /*
  2. * Copyright (C) 2014 STMicroelectronics (R&D) Limited
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. */
  10. /*
  11. * Authors:
  12. * Stephen Gallimore <stephen.gallimore@st.com>,
  13. * Pankaj Dev <pankaj.dev@st.com>.
  14. */
  15. #include <linux/slab.h>
  16. #include <linux/of_address.h>
  17. #include <linux/clk.h>
  18. #include <linux/clk-provider.h>
  19. #include <linux/iopoll.h>
  20. #include "clkgen.h"
  21. static DEFINE_SPINLOCK(clkgena_c32_odf_lock);
  22. DEFINE_SPINLOCK(clkgen_a9_lock);
  23. /*
  24. * PLL configuration register bits for PLL3200 C32
  25. */
  26. #define C32_NDIV_MASK (0xff)
  27. #define C32_IDF_MASK (0x7)
  28. #define C32_ODF_MASK (0x3f)
  29. #define C32_LDF_MASK (0x7f)
  30. #define C32_CP_MASK (0x1f)
  31. #define C32_MAX_ODFS (4)
  32. /*
  33. * PLL configuration register bits for PLL4600 C28
  34. */
  35. #define C28_NDIV_MASK (0xff)
  36. #define C28_IDF_MASK (0x7)
  37. #define C28_ODF_MASK (0x3f)
  38. struct clkgen_pll_data {
  39. struct clkgen_field pdn_status;
  40. struct clkgen_field pdn_ctrl;
  41. struct clkgen_field locked_status;
  42. struct clkgen_field mdiv;
  43. struct clkgen_field ndiv;
  44. struct clkgen_field pdiv;
  45. struct clkgen_field idf;
  46. struct clkgen_field ldf;
  47. struct clkgen_field cp;
  48. unsigned int num_odfs;
  49. struct clkgen_field odf[C32_MAX_ODFS];
  50. struct clkgen_field odf_gate[C32_MAX_ODFS];
  51. bool switch2pll_en;
  52. struct clkgen_field switch2pll;
  53. spinlock_t *lock;
  54. const struct clk_ops *ops;
  55. };
  56. static const struct clk_ops stm_pll3200c32_ops;
  57. static const struct clk_ops stm_pll3200c32_a9_ops;
  58. static const struct clk_ops stm_pll4600c28_ops;
  59. static const struct clkgen_pll_data st_pll3200c32_407_a0 = {
  60. /* 407 A0 */
  61. .pdn_status = CLKGEN_FIELD(0x2a0, 0x1, 8),
  62. .pdn_ctrl = CLKGEN_FIELD(0x2a0, 0x1, 8),
  63. .locked_status = CLKGEN_FIELD(0x2a0, 0x1, 24),
  64. .ndiv = CLKGEN_FIELD(0x2a4, C32_NDIV_MASK, 16),
  65. .idf = CLKGEN_FIELD(0x2a4, C32_IDF_MASK, 0x0),
  66. .num_odfs = 1,
  67. .odf = { CLKGEN_FIELD(0x2b4, C32_ODF_MASK, 0) },
  68. .odf_gate = { CLKGEN_FIELD(0x2b4, 0x1, 6) },
  69. .ops = &stm_pll3200c32_ops,
  70. };
  71. static const struct clkgen_pll_data st_pll3200c32_cx_0 = {
  72. /* 407 C0 PLL0 */
  73. .pdn_status = CLKGEN_FIELD(0x2a0, 0x1, 8),
  74. .pdn_ctrl = CLKGEN_FIELD(0x2a0, 0x1, 8),
  75. .locked_status = CLKGEN_FIELD(0x2a0, 0x1, 24),
  76. .ndiv = CLKGEN_FIELD(0x2a4, C32_NDIV_MASK, 16),
  77. .idf = CLKGEN_FIELD(0x2a4, C32_IDF_MASK, 0x0),
  78. .num_odfs = 1,
  79. .odf = { CLKGEN_FIELD(0x2b4, C32_ODF_MASK, 0) },
  80. .odf_gate = { CLKGEN_FIELD(0x2b4, 0x1, 6) },
  81. .ops = &stm_pll3200c32_ops,
  82. };
  83. static const struct clkgen_pll_data st_pll3200c32_cx_1 = {
  84. /* 407 C0 PLL1 */
  85. .pdn_status = CLKGEN_FIELD(0x2c8, 0x1, 8),
  86. .pdn_ctrl = CLKGEN_FIELD(0x2c8, 0x1, 8),
  87. .locked_status = CLKGEN_FIELD(0x2c8, 0x1, 24),
  88. .ndiv = CLKGEN_FIELD(0x2cc, C32_NDIV_MASK, 16),
  89. .idf = CLKGEN_FIELD(0x2cc, C32_IDF_MASK, 0x0),
  90. .num_odfs = 1,
  91. .odf = { CLKGEN_FIELD(0x2dc, C32_ODF_MASK, 0) },
  92. .odf_gate = { CLKGEN_FIELD(0x2dc, 0x1, 6) },
  93. .ops = &stm_pll3200c32_ops,
  94. };
  95. static const struct clkgen_pll_data st_pll3200c32_407_a9 = {
  96. /* 407 A9 */
  97. .pdn_status = CLKGEN_FIELD(0x1a8, 0x1, 0),
  98. .pdn_ctrl = CLKGEN_FIELD(0x1a8, 0x1, 0),
  99. .locked_status = CLKGEN_FIELD(0x87c, 0x1, 0),
  100. .ndiv = CLKGEN_FIELD(0x1b0, C32_NDIV_MASK, 0),
  101. .idf = CLKGEN_FIELD(0x1a8, C32_IDF_MASK, 25),
  102. .num_odfs = 1,
  103. .odf = { CLKGEN_FIELD(0x1b0, C32_ODF_MASK, 8) },
  104. .odf_gate = { CLKGEN_FIELD(0x1ac, 0x1, 28) },
  105. .switch2pll_en = true,
  106. .cp = CLKGEN_FIELD(0x1a8, C32_CP_MASK, 1),
  107. .switch2pll = CLKGEN_FIELD(0x1a4, 0x1, 1),
  108. .lock = &clkgen_a9_lock,
  109. .ops = &stm_pll3200c32_a9_ops,
  110. };
  111. static struct clkgen_pll_data st_pll4600c28_418_a9 = {
  112. /* 418 A9 */
  113. .pdn_status = CLKGEN_FIELD(0x1a8, 0x1, 0),
  114. .pdn_ctrl = CLKGEN_FIELD(0x1a8, 0x1, 0),
  115. .locked_status = CLKGEN_FIELD(0x87c, 0x1, 0),
  116. .ndiv = CLKGEN_FIELD(0x1b0, C28_NDIV_MASK, 0),
  117. .idf = CLKGEN_FIELD(0x1a8, C28_IDF_MASK, 25),
  118. .num_odfs = 1,
  119. .odf = { CLKGEN_FIELD(0x1b0, C28_ODF_MASK, 8) },
  120. .odf_gate = { CLKGEN_FIELD(0x1ac, 0x1, 28) },
  121. .switch2pll_en = true,
  122. .switch2pll = CLKGEN_FIELD(0x1a4, 0x1, 1),
  123. .lock = &clkgen_a9_lock,
  124. .ops = &stm_pll4600c28_ops,
  125. };
  126. /**
  127. * DOC: Clock Generated by PLL, rate set and enabled by bootloader
  128. *
  129. * Traits of this clock:
  130. * prepare - clk_(un)prepare only ensures parent is (un)prepared
  131. * enable - clk_enable/disable only ensures parent is enabled
  132. * rate - rate is fixed. No clk_set_rate support
  133. * parent - fixed parent. No clk_set_parent support
  134. */
  135. /**
  136. * PLL clock that is integrated in the ClockGenA instances on the STiH415
  137. * and STiH416.
  138. *
  139. * @hw: handle between common and hardware-specific interfaces.
  140. * @type: PLL instance type.
  141. * @regs_base: base of the PLL configuration register(s).
  142. *
  143. */
  144. struct clkgen_pll {
  145. struct clk_hw hw;
  146. struct clkgen_pll_data *data;
  147. void __iomem *regs_base;
  148. spinlock_t *lock;
  149. u32 ndiv;
  150. u32 idf;
  151. u32 odf;
  152. u32 cp;
  153. };
  154. #define to_clkgen_pll(_hw) container_of(_hw, struct clkgen_pll, hw)
  155. struct stm_pll {
  156. unsigned long mdiv;
  157. unsigned long ndiv;
  158. unsigned long pdiv;
  159. unsigned long odf;
  160. unsigned long idf;
  161. unsigned long ldf;
  162. unsigned long cp;
  163. };
  164. static int clkgen_pll_is_locked(struct clk_hw *hw)
  165. {
  166. struct clkgen_pll *pll = to_clkgen_pll(hw);
  167. u32 locked = CLKGEN_READ(pll, locked_status);
  168. return !!locked;
  169. }
  170. static int clkgen_pll_is_enabled(struct clk_hw *hw)
  171. {
  172. struct clkgen_pll *pll = to_clkgen_pll(hw);
  173. u32 poweroff = CLKGEN_READ(pll, pdn_status);
  174. return !poweroff;
  175. }
  176. static int __clkgen_pll_enable(struct clk_hw *hw)
  177. {
  178. struct clkgen_pll *pll = to_clkgen_pll(hw);
  179. void __iomem *base = pll->regs_base;
  180. struct clkgen_field *field = &pll->data->locked_status;
  181. int ret = 0;
  182. u32 reg;
  183. if (clkgen_pll_is_enabled(hw))
  184. return 0;
  185. CLKGEN_WRITE(pll, pdn_ctrl, 0);
  186. ret = readl_relaxed_poll_timeout(base + field->offset, reg,
  187. !!((reg >> field->shift) & field->mask), 0, 10000);
  188. if (!ret) {
  189. if (pll->data->switch2pll_en)
  190. CLKGEN_WRITE(pll, switch2pll, 0);
  191. pr_debug("%s:%s enabled\n", __clk_get_name(hw->clk), __func__);
  192. }
  193. return ret;
  194. }
  195. static int clkgen_pll_enable(struct clk_hw *hw)
  196. {
  197. struct clkgen_pll *pll = to_clkgen_pll(hw);
  198. unsigned long flags = 0;
  199. int ret = 0;
  200. if (pll->lock)
  201. spin_lock_irqsave(pll->lock, flags);
  202. ret = __clkgen_pll_enable(hw);
  203. if (pll->lock)
  204. spin_unlock_irqrestore(pll->lock, flags);
  205. return ret;
  206. }
  207. static void __clkgen_pll_disable(struct clk_hw *hw)
  208. {
  209. struct clkgen_pll *pll = to_clkgen_pll(hw);
  210. if (!clkgen_pll_is_enabled(hw))
  211. return;
  212. if (pll->data->switch2pll_en)
  213. CLKGEN_WRITE(pll, switch2pll, 1);
  214. CLKGEN_WRITE(pll, pdn_ctrl, 1);
  215. pr_debug("%s:%s disabled\n", __clk_get_name(hw->clk), __func__);
  216. }
  217. static void clkgen_pll_disable(struct clk_hw *hw)
  218. {
  219. struct clkgen_pll *pll = to_clkgen_pll(hw);
  220. unsigned long flags = 0;
  221. if (pll->lock)
  222. spin_lock_irqsave(pll->lock, flags);
  223. __clkgen_pll_disable(hw);
  224. if (pll->lock)
  225. spin_unlock_irqrestore(pll->lock, flags);
  226. }
  227. static int clk_pll3200c32_get_params(unsigned long input, unsigned long output,
  228. struct stm_pll *pll)
  229. {
  230. unsigned long i, n;
  231. unsigned long deviation = ~0;
  232. unsigned long new_freq;
  233. long new_deviation;
  234. /* Charge pump table: highest ndiv value for cp=6 to 25 */
  235. static const unsigned char cp_table[] = {
  236. 48, 56, 64, 72, 80, 88, 96, 104, 112, 120,
  237. 128, 136, 144, 152, 160, 168, 176, 184, 192
  238. };
  239. /* Output clock range: 800Mhz to 1600Mhz */
  240. if (output < 800000000 || output > 1600000000)
  241. return -EINVAL;
  242. input /= 1000;
  243. output /= 1000;
  244. for (i = 1; i <= 7 && deviation; i++) {
  245. n = i * output / (2 * input);
  246. /* Checks */
  247. if (n < 8)
  248. continue;
  249. if (n > 200)
  250. break;
  251. new_freq = (input * 2 * n) / i;
  252. new_deviation = abs(new_freq - output);
  253. if (!new_deviation || new_deviation < deviation) {
  254. pll->idf = i;
  255. pll->ndiv = n;
  256. deviation = new_deviation;
  257. }
  258. }
  259. if (deviation == ~0) /* No solution found */
  260. return -EINVAL;
  261. /* Computing recommended charge pump value */
  262. for (pll->cp = 6; pll->ndiv > cp_table[pll->cp-6]; (pll->cp)++)
  263. ;
  264. return 0;
  265. }
  266. static int clk_pll3200c32_get_rate(unsigned long input, struct stm_pll *pll,
  267. unsigned long *rate)
  268. {
  269. if (!pll->idf)
  270. pll->idf = 1;
  271. *rate = ((2 * (input / 1000) * pll->ndiv) / pll->idf) * 1000;
  272. return 0;
  273. }
  274. static unsigned long recalc_stm_pll3200c32(struct clk_hw *hw,
  275. unsigned long parent_rate)
  276. {
  277. struct clkgen_pll *pll = to_clkgen_pll(hw);
  278. unsigned long ndiv, idf;
  279. unsigned long rate = 0;
  280. if (!clkgen_pll_is_enabled(hw) || !clkgen_pll_is_locked(hw))
  281. return 0;
  282. ndiv = CLKGEN_READ(pll, ndiv);
  283. idf = CLKGEN_READ(pll, idf);
  284. if (idf)
  285. /* Note: input is divided to avoid overflow */
  286. rate = ((2 * (parent_rate/1000) * ndiv) / idf) * 1000;
  287. pr_debug("%s:%s rate %lu\n", clk_hw_get_name(hw), __func__, rate);
  288. return rate;
  289. }
  290. static long round_rate_stm_pll3200c32(struct clk_hw *hw, unsigned long rate,
  291. unsigned long *prate)
  292. {
  293. struct stm_pll params;
  294. if (!clk_pll3200c32_get_params(*prate, rate, &params))
  295. clk_pll3200c32_get_rate(*prate, &params, &rate);
  296. else {
  297. pr_debug("%s: %s rate %ld Invalid\n", __func__,
  298. __clk_get_name(hw->clk), rate);
  299. return 0;
  300. }
  301. pr_debug("%s: %s new rate %ld [ndiv=%u] [idf=%u]\n",
  302. __func__, __clk_get_name(hw->clk),
  303. rate, (unsigned int)params.ndiv,
  304. (unsigned int)params.idf);
  305. return rate;
  306. }
  307. static int set_rate_stm_pll3200c32(struct clk_hw *hw, unsigned long rate,
  308. unsigned long parent_rate)
  309. {
  310. struct clkgen_pll *pll = to_clkgen_pll(hw);
  311. struct stm_pll params;
  312. long hwrate = 0;
  313. unsigned long flags = 0;
  314. if (!rate || !parent_rate)
  315. return -EINVAL;
  316. if (!clk_pll3200c32_get_params(parent_rate, rate, &params))
  317. clk_pll3200c32_get_rate(parent_rate, &params, &hwrate);
  318. pr_debug("%s: %s new rate %ld [ndiv=0x%x] [idf=0x%x]\n",
  319. __func__, __clk_get_name(hw->clk),
  320. hwrate, (unsigned int)params.ndiv,
  321. (unsigned int)params.idf);
  322. if (!hwrate)
  323. return -EINVAL;
  324. pll->ndiv = params.ndiv;
  325. pll->idf = params.idf;
  326. pll->cp = params.cp;
  327. __clkgen_pll_disable(hw);
  328. if (pll->lock)
  329. spin_lock_irqsave(pll->lock, flags);
  330. CLKGEN_WRITE(pll, ndiv, pll->ndiv);
  331. CLKGEN_WRITE(pll, idf, pll->idf);
  332. CLKGEN_WRITE(pll, cp, pll->cp);
  333. if (pll->lock)
  334. spin_unlock_irqrestore(pll->lock, flags);
  335. __clkgen_pll_enable(hw);
  336. return 0;
  337. }
  338. /* PLL output structure
  339. * FVCO >> /2 >> FVCOBY2 (no output)
  340. * |> Divider (ODF) >> PHI
  341. *
  342. * FVCOby2 output = (input * 2 * NDIV) / IDF (assuming FRAC_CONTROL==L)
  343. *
  344. * Rules:
  345. * 4Mhz <= INFF input <= 350Mhz
  346. * 4Mhz <= INFIN (INFF / IDF) <= 50Mhz
  347. * 19.05Mhz <= FVCOby2 output (PHI w ODF=1) <= 3000Mhz
  348. * 1 <= i (register/dec value for IDF) <= 7
  349. * 8 <= n (register/dec value for NDIV) <= 246
  350. */
  351. static int clk_pll4600c28_get_params(unsigned long input, unsigned long output,
  352. struct stm_pll *pll)
  353. {
  354. unsigned long i, infin, n;
  355. unsigned long deviation = ~0;
  356. unsigned long new_freq, new_deviation;
  357. /* Output clock range: 19Mhz to 3000Mhz */
  358. if (output < 19000000 || output > 3000000000u)
  359. return -EINVAL;
  360. /* For better jitter, IDF should be smallest and NDIV must be maximum */
  361. for (i = 1; i <= 7 && deviation; i++) {
  362. /* INFIN checks */
  363. infin = input / i;
  364. if (infin < 4000000 || infin > 50000000)
  365. continue; /* Invalid case */
  366. n = output / (infin * 2);
  367. if (n < 8 || n > 246)
  368. continue; /* Invalid case */
  369. if (n < 246)
  370. n++; /* To work around 'y' when n=x.y */
  371. for (; n >= 8 && deviation; n--) {
  372. new_freq = infin * 2 * n;
  373. if (new_freq < output)
  374. break; /* Optimization: shorting loop */
  375. new_deviation = new_freq - output;
  376. if (!new_deviation || new_deviation < deviation) {
  377. pll->idf = i;
  378. pll->ndiv = n;
  379. deviation = new_deviation;
  380. }
  381. }
  382. }
  383. if (deviation == ~0) /* No solution found */
  384. return -EINVAL;
  385. return 0;
  386. }
  387. static int clk_pll4600c28_get_rate(unsigned long input, struct stm_pll *pll,
  388. unsigned long *rate)
  389. {
  390. if (!pll->idf)
  391. pll->idf = 1;
  392. *rate = (input / pll->idf) * 2 * pll->ndiv;
  393. return 0;
  394. }
  395. static unsigned long recalc_stm_pll4600c28(struct clk_hw *hw,
  396. unsigned long parent_rate)
  397. {
  398. struct clkgen_pll *pll = to_clkgen_pll(hw);
  399. struct stm_pll params;
  400. unsigned long rate;
  401. if (!clkgen_pll_is_enabled(hw) || !clkgen_pll_is_locked(hw))
  402. return 0;
  403. params.ndiv = CLKGEN_READ(pll, ndiv);
  404. params.idf = CLKGEN_READ(pll, idf);
  405. clk_pll4600c28_get_rate(parent_rate, &params, &rate);
  406. pr_debug("%s:%s rate %lu\n", __clk_get_name(hw->clk), __func__, rate);
  407. return rate;
  408. }
  409. static long round_rate_stm_pll4600c28(struct clk_hw *hw, unsigned long rate,
  410. unsigned long *prate)
  411. {
  412. struct stm_pll params;
  413. if (!clk_pll4600c28_get_params(*prate, rate, &params)) {
  414. clk_pll4600c28_get_rate(*prate, &params, &rate);
  415. } else {
  416. pr_debug("%s: %s rate %ld Invalid\n", __func__,
  417. __clk_get_name(hw->clk), rate);
  418. return 0;
  419. }
  420. pr_debug("%s: %s new rate %ld [ndiv=%u] [idf=%u]\n",
  421. __func__, __clk_get_name(hw->clk),
  422. rate, (unsigned int)params.ndiv,
  423. (unsigned int)params.idf);
  424. return rate;
  425. }
  426. static int set_rate_stm_pll4600c28(struct clk_hw *hw, unsigned long rate,
  427. unsigned long parent_rate)
  428. {
  429. struct clkgen_pll *pll = to_clkgen_pll(hw);
  430. struct stm_pll params;
  431. long hwrate;
  432. unsigned long flags = 0;
  433. if (!rate || !parent_rate)
  434. return -EINVAL;
  435. if (!clk_pll4600c28_get_params(parent_rate, rate, &params)) {
  436. clk_pll4600c28_get_rate(parent_rate, &params, &hwrate);
  437. } else {
  438. pr_debug("%s: %s rate %ld Invalid\n", __func__,
  439. __clk_get_name(hw->clk), rate);
  440. return -EINVAL;
  441. }
  442. pr_debug("%s: %s new rate %ld [ndiv=0x%x] [idf=0x%x]\n",
  443. __func__, __clk_get_name(hw->clk),
  444. hwrate, (unsigned int)params.ndiv,
  445. (unsigned int)params.idf);
  446. if (!hwrate)
  447. return -EINVAL;
  448. pll->ndiv = params.ndiv;
  449. pll->idf = params.idf;
  450. __clkgen_pll_disable(hw);
  451. if (pll->lock)
  452. spin_lock_irqsave(pll->lock, flags);
  453. CLKGEN_WRITE(pll, ndiv, pll->ndiv);
  454. CLKGEN_WRITE(pll, idf, pll->idf);
  455. if (pll->lock)
  456. spin_unlock_irqrestore(pll->lock, flags);
  457. __clkgen_pll_enable(hw);
  458. return 0;
  459. }
  460. static const struct clk_ops stm_pll3200c32_ops = {
  461. .enable = clkgen_pll_enable,
  462. .disable = clkgen_pll_disable,
  463. .is_enabled = clkgen_pll_is_enabled,
  464. .recalc_rate = recalc_stm_pll3200c32,
  465. };
  466. static const struct clk_ops stm_pll3200c32_a9_ops = {
  467. .enable = clkgen_pll_enable,
  468. .disable = clkgen_pll_disable,
  469. .is_enabled = clkgen_pll_is_enabled,
  470. .recalc_rate = recalc_stm_pll3200c32,
  471. .round_rate = round_rate_stm_pll3200c32,
  472. .set_rate = set_rate_stm_pll3200c32,
  473. };
  474. static const struct clk_ops stm_pll4600c28_ops = {
  475. .enable = clkgen_pll_enable,
  476. .disable = clkgen_pll_disable,
  477. .is_enabled = clkgen_pll_is_enabled,
  478. .recalc_rate = recalc_stm_pll4600c28,
  479. .round_rate = round_rate_stm_pll4600c28,
  480. .set_rate = set_rate_stm_pll4600c28,
  481. };
  482. static struct clk * __init clkgen_pll_register(const char *parent_name,
  483. struct clkgen_pll_data *pll_data,
  484. void __iomem *reg, unsigned long pll_flags,
  485. const char *clk_name, spinlock_t *lock)
  486. {
  487. struct clkgen_pll *pll;
  488. struct clk *clk;
  489. struct clk_init_data init;
  490. pll = kzalloc(sizeof(*pll), GFP_KERNEL);
  491. if (!pll)
  492. return ERR_PTR(-ENOMEM);
  493. init.name = clk_name;
  494. init.ops = pll_data->ops;
  495. init.flags = pll_flags | CLK_IS_BASIC | CLK_GET_RATE_NOCACHE;
  496. init.parent_names = &parent_name;
  497. init.num_parents = 1;
  498. pll->data = pll_data;
  499. pll->regs_base = reg;
  500. pll->hw.init = &init;
  501. pll->lock = lock;
  502. clk = clk_register(NULL, &pll->hw);
  503. if (IS_ERR(clk)) {
  504. kfree(pll);
  505. return clk;
  506. }
  507. pr_debug("%s: parent %s rate %lu\n",
  508. __clk_get_name(clk),
  509. __clk_get_name(clk_get_parent(clk)),
  510. clk_get_rate(clk));
  511. return clk;
  512. }
  513. static void __iomem * __init clkgen_get_register_base(
  514. struct device_node *np)
  515. {
  516. struct device_node *pnode;
  517. void __iomem *reg = NULL;
  518. pnode = of_get_parent(np);
  519. if (!pnode)
  520. return NULL;
  521. reg = of_iomap(pnode, 0);
  522. of_node_put(pnode);
  523. return reg;
  524. }
  525. static struct clk * __init clkgen_odf_register(const char *parent_name,
  526. void __iomem *reg,
  527. struct clkgen_pll_data *pll_data,
  528. unsigned long pll_flags, int odf,
  529. spinlock_t *odf_lock,
  530. const char *odf_name)
  531. {
  532. struct clk *clk;
  533. unsigned long flags;
  534. struct clk_gate *gate;
  535. struct clk_divider *div;
  536. flags = pll_flags | CLK_GET_RATE_NOCACHE | CLK_SET_RATE_PARENT;
  537. gate = kzalloc(sizeof(*gate), GFP_KERNEL);
  538. if (!gate)
  539. return ERR_PTR(-ENOMEM);
  540. gate->flags = CLK_GATE_SET_TO_DISABLE;
  541. gate->reg = reg + pll_data->odf_gate[odf].offset;
  542. gate->bit_idx = pll_data->odf_gate[odf].shift;
  543. gate->lock = odf_lock;
  544. div = kzalloc(sizeof(*div), GFP_KERNEL);
  545. if (!div) {
  546. kfree(gate);
  547. return ERR_PTR(-ENOMEM);
  548. }
  549. div->flags = CLK_DIVIDER_ONE_BASED | CLK_DIVIDER_ALLOW_ZERO;
  550. div->reg = reg + pll_data->odf[odf].offset;
  551. div->shift = pll_data->odf[odf].shift;
  552. div->width = fls(pll_data->odf[odf].mask);
  553. div->lock = odf_lock;
  554. clk = clk_register_composite(NULL, odf_name, &parent_name, 1,
  555. NULL, NULL,
  556. &div->hw, &clk_divider_ops,
  557. &gate->hw, &clk_gate_ops,
  558. flags);
  559. if (IS_ERR(clk))
  560. return clk;
  561. pr_debug("%s: parent %s rate %lu\n",
  562. __clk_get_name(clk),
  563. __clk_get_name(clk_get_parent(clk)),
  564. clk_get_rate(clk));
  565. return clk;
  566. }
  567. static void __init clkgen_c32_pll_setup(struct device_node *np,
  568. struct clkgen_pll_data *data)
  569. {
  570. struct clk *clk;
  571. const char *parent_name, *pll_name;
  572. void __iomem *pll_base;
  573. int num_odfs, odf;
  574. struct clk_onecell_data *clk_data;
  575. unsigned long pll_flags = 0;
  576. parent_name = of_clk_get_parent_name(np, 0);
  577. if (!parent_name)
  578. return;
  579. pll_base = clkgen_get_register_base(np);
  580. if (!pll_base)
  581. return;
  582. of_clk_detect_critical(np, 0, &pll_flags);
  583. clk = clkgen_pll_register(parent_name, data, pll_base, pll_flags,
  584. np->name, data->lock);
  585. if (IS_ERR(clk))
  586. return;
  587. pll_name = __clk_get_name(clk);
  588. num_odfs = data->num_odfs;
  589. clk_data = kzalloc(sizeof(*clk_data), GFP_KERNEL);
  590. if (!clk_data)
  591. return;
  592. clk_data->clk_num = num_odfs;
  593. clk_data->clks = kzalloc(clk_data->clk_num * sizeof(struct clk *),
  594. GFP_KERNEL);
  595. if (!clk_data->clks)
  596. goto err;
  597. for (odf = 0; odf < num_odfs; odf++) {
  598. struct clk *clk;
  599. const char *clk_name;
  600. unsigned long odf_flags = 0;
  601. if (of_property_read_string_index(np, "clock-output-names",
  602. odf, &clk_name))
  603. return;
  604. of_clk_detect_critical(np, odf, &odf_flags);
  605. clk = clkgen_odf_register(pll_name, pll_base, data, odf_flags,
  606. odf, &clkgena_c32_odf_lock, clk_name);
  607. if (IS_ERR(clk))
  608. goto err;
  609. clk_data->clks[odf] = clk;
  610. }
  611. of_clk_add_provider(np, of_clk_src_onecell_get, clk_data);
  612. return;
  613. err:
  614. kfree(pll_name);
  615. kfree(clk_data->clks);
  616. kfree(clk_data);
  617. }
  618. static void __init clkgen_c32_pll0_setup(struct device_node *np)
  619. {
  620. clkgen_c32_pll_setup(np,
  621. (struct clkgen_pll_data *) &st_pll3200c32_cx_0);
  622. }
  623. CLK_OF_DECLARE(c32_pll0, "st,clkgen-pll0", clkgen_c32_pll0_setup);
  624. static void __init clkgen_c32_pll1_setup(struct device_node *np)
  625. {
  626. clkgen_c32_pll_setup(np,
  627. (struct clkgen_pll_data *) &st_pll3200c32_cx_1);
  628. }
  629. CLK_OF_DECLARE(c32_pll1, "st,clkgen-pll1", clkgen_c32_pll1_setup);
  630. static void __init clkgen_c32_plla9_setup(struct device_node *np)
  631. {
  632. clkgen_c32_pll_setup(np,
  633. (struct clkgen_pll_data *) &st_pll3200c32_407_a9);
  634. }
  635. CLK_OF_DECLARE(c32_plla9, "st,stih407-clkgen-plla9", clkgen_c32_plla9_setup);
  636. static void __init clkgen_c28_plla9_setup(struct device_node *np)
  637. {
  638. clkgen_c32_pll_setup(np,
  639. (struct clkgen_pll_data *) &st_pll4600c28_418_a9);
  640. }
  641. CLK_OF_DECLARE(c28_plla9, "st,stih418-clkgen-plla9", clkgen_c28_plla9_setup);