clkgen-fsyn.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2014 STMicroelectronics R&D Ltd
  4. */
  5. /*
  6. * Authors:
  7. * Stephen Gallimore <stephen.gallimore@st.com>,
  8. * Pankaj Dev <pankaj.dev@st.com>.
  9. */
  10. #include <linux/slab.h>
  11. #include <linux/of_address.h>
  12. #include <linux/clk.h>
  13. #include <linux/clk-provider.h>
  14. #include "clkgen.h"
  15. /*
  16. * Maximum input clock to the PLL before we divide it down by 2
  17. * although in reality in actual systems this has never been seen to
  18. * be used.
  19. */
  20. #define QUADFS_NDIV_THRESHOLD 30000000
  21. #define PLL_BW_GOODREF (0L)
  22. #define PLL_BW_VBADREF (1L)
  23. #define PLL_BW_BADREF (2L)
  24. #define PLL_BW_VGOODREF (3L)
  25. #define QUADFS_MAX_CHAN 4
  26. struct stm_fs {
  27. unsigned long ndiv;
  28. unsigned long mdiv;
  29. unsigned long pe;
  30. unsigned long sdiv;
  31. unsigned long nsdiv;
  32. };
  33. struct clkgen_quadfs_data {
  34. bool reset_present;
  35. bool bwfilter_present;
  36. bool lockstatus_present;
  37. bool powerup_polarity;
  38. bool standby_polarity;
  39. bool nsdiv_present;
  40. bool nrst_present;
  41. struct clkgen_field ndiv;
  42. struct clkgen_field ref_bw;
  43. struct clkgen_field nreset;
  44. struct clkgen_field npda;
  45. struct clkgen_field lock_status;
  46. struct clkgen_field nrst[QUADFS_MAX_CHAN];
  47. struct clkgen_field nsb[QUADFS_MAX_CHAN];
  48. struct clkgen_field en[QUADFS_MAX_CHAN];
  49. struct clkgen_field mdiv[QUADFS_MAX_CHAN];
  50. struct clkgen_field pe[QUADFS_MAX_CHAN];
  51. struct clkgen_field sdiv[QUADFS_MAX_CHAN];
  52. struct clkgen_field nsdiv[QUADFS_MAX_CHAN];
  53. const struct clk_ops *pll_ops;
  54. int (*get_params)(unsigned long, unsigned long, struct stm_fs *);
  55. int (*get_rate)(unsigned long , const struct stm_fs *,
  56. unsigned long *);
  57. };
  58. struct clkgen_clk_out {
  59. const char *name;
  60. unsigned long flags;
  61. };
  62. struct clkgen_quadfs_data_clks {
  63. struct clkgen_quadfs_data *data;
  64. const struct clkgen_clk_out *outputs;
  65. };
  66. static const struct clk_ops st_quadfs_pll_c32_ops;
  67. static int clk_fs660c32_dig_get_params(unsigned long input,
  68. unsigned long output, struct stm_fs *fs);
  69. static int clk_fs660c32_dig_get_rate(unsigned long, const struct stm_fs *,
  70. unsigned long *);
  71. static const struct clkgen_quadfs_data st_fs660c32_C = {
  72. .nrst_present = true,
  73. .nrst = { CLKGEN_FIELD(0x2f0, 0x1, 0),
  74. CLKGEN_FIELD(0x2f0, 0x1, 1),
  75. CLKGEN_FIELD(0x2f0, 0x1, 2),
  76. CLKGEN_FIELD(0x2f0, 0x1, 3) },
  77. .npda = CLKGEN_FIELD(0x2f0, 0x1, 12),
  78. .nsb = { CLKGEN_FIELD(0x2f0, 0x1, 8),
  79. CLKGEN_FIELD(0x2f0, 0x1, 9),
  80. CLKGEN_FIELD(0x2f0, 0x1, 10),
  81. CLKGEN_FIELD(0x2f0, 0x1, 11) },
  82. .nsdiv_present = true,
  83. .nsdiv = { CLKGEN_FIELD(0x304, 0x1, 24),
  84. CLKGEN_FIELD(0x308, 0x1, 24),
  85. CLKGEN_FIELD(0x30c, 0x1, 24),
  86. CLKGEN_FIELD(0x310, 0x1, 24) },
  87. .mdiv = { CLKGEN_FIELD(0x304, 0x1f, 15),
  88. CLKGEN_FIELD(0x308, 0x1f, 15),
  89. CLKGEN_FIELD(0x30c, 0x1f, 15),
  90. CLKGEN_FIELD(0x310, 0x1f, 15) },
  91. .en = { CLKGEN_FIELD(0x2fc, 0x1, 0),
  92. CLKGEN_FIELD(0x2fc, 0x1, 1),
  93. CLKGEN_FIELD(0x2fc, 0x1, 2),
  94. CLKGEN_FIELD(0x2fc, 0x1, 3) },
  95. .ndiv = CLKGEN_FIELD(0x2f4, 0x7, 16),
  96. .pe = { CLKGEN_FIELD(0x304, 0x7fff, 0),
  97. CLKGEN_FIELD(0x308, 0x7fff, 0),
  98. CLKGEN_FIELD(0x30c, 0x7fff, 0),
  99. CLKGEN_FIELD(0x310, 0x7fff, 0) },
  100. .sdiv = { CLKGEN_FIELD(0x304, 0xf, 20),
  101. CLKGEN_FIELD(0x308, 0xf, 20),
  102. CLKGEN_FIELD(0x30c, 0xf, 20),
  103. CLKGEN_FIELD(0x310, 0xf, 20) },
  104. .lockstatus_present = true,
  105. .lock_status = CLKGEN_FIELD(0x2f0, 0x1, 24),
  106. .powerup_polarity = 1,
  107. .standby_polarity = 1,
  108. .pll_ops = &st_quadfs_pll_c32_ops,
  109. .get_params = clk_fs660c32_dig_get_params,
  110. .get_rate = clk_fs660c32_dig_get_rate,
  111. };
  112. static const struct clkgen_clk_out st_fs660c32_C_clks[] = {
  113. { .name = "clk-s-c0-fs0-ch0", },
  114. { .name = "clk-s-c0-fs0-ch1", },
  115. { .name = "clk-s-c0-fs0-ch2", },
  116. { .name = "clk-s-c0-fs0-ch3", },
  117. };
  118. static const struct clkgen_quadfs_data_clks st_fs660c32_C_data = {
  119. .data = (struct clkgen_quadfs_data *)&st_fs660c32_C,
  120. .outputs = st_fs660c32_C_clks,
  121. };
  122. static const struct clkgen_quadfs_data st_fs660c32_D = {
  123. .nrst_present = true,
  124. .nrst = { CLKGEN_FIELD(0x2a0, 0x1, 0),
  125. CLKGEN_FIELD(0x2a0, 0x1, 1),
  126. CLKGEN_FIELD(0x2a0, 0x1, 2),
  127. CLKGEN_FIELD(0x2a0, 0x1, 3) },
  128. .ndiv = CLKGEN_FIELD(0x2a4, 0x7, 16),
  129. .pe = { CLKGEN_FIELD(0x2b4, 0x7fff, 0),
  130. CLKGEN_FIELD(0x2b8, 0x7fff, 0),
  131. CLKGEN_FIELD(0x2bc, 0x7fff, 0),
  132. CLKGEN_FIELD(0x2c0, 0x7fff, 0) },
  133. .sdiv = { CLKGEN_FIELD(0x2b4, 0xf, 20),
  134. CLKGEN_FIELD(0x2b8, 0xf, 20),
  135. CLKGEN_FIELD(0x2bc, 0xf, 20),
  136. CLKGEN_FIELD(0x2c0, 0xf, 20) },
  137. .npda = CLKGEN_FIELD(0x2a0, 0x1, 12),
  138. .nsb = { CLKGEN_FIELD(0x2a0, 0x1, 8),
  139. CLKGEN_FIELD(0x2a0, 0x1, 9),
  140. CLKGEN_FIELD(0x2a0, 0x1, 10),
  141. CLKGEN_FIELD(0x2a0, 0x1, 11) },
  142. .nsdiv_present = true,
  143. .nsdiv = { CLKGEN_FIELD(0x2b4, 0x1, 24),
  144. CLKGEN_FIELD(0x2b8, 0x1, 24),
  145. CLKGEN_FIELD(0x2bc, 0x1, 24),
  146. CLKGEN_FIELD(0x2c0, 0x1, 24) },
  147. .mdiv = { CLKGEN_FIELD(0x2b4, 0x1f, 15),
  148. CLKGEN_FIELD(0x2b8, 0x1f, 15),
  149. CLKGEN_FIELD(0x2bc, 0x1f, 15),
  150. CLKGEN_FIELD(0x2c0, 0x1f, 15) },
  151. .en = { CLKGEN_FIELD(0x2ac, 0x1, 0),
  152. CLKGEN_FIELD(0x2ac, 0x1, 1),
  153. CLKGEN_FIELD(0x2ac, 0x1, 2),
  154. CLKGEN_FIELD(0x2ac, 0x1, 3) },
  155. .lockstatus_present = true,
  156. .lock_status = CLKGEN_FIELD(0x2A0, 0x1, 24),
  157. .powerup_polarity = 1,
  158. .standby_polarity = 1,
  159. .pll_ops = &st_quadfs_pll_c32_ops,
  160. .get_params = clk_fs660c32_dig_get_params,
  161. .get_rate = clk_fs660c32_dig_get_rate,};
  162. static const struct clkgen_quadfs_data_clks st_fs660c32_D_data = {
  163. .data = (struct clkgen_quadfs_data *)&st_fs660c32_D,
  164. };
  165. static const struct clkgen_clk_out st_fs660c32_D0_clks[] = {
  166. { .name = "clk-s-d0-fs0-ch0", },
  167. { .name = "clk-s-d0-fs0-ch1", },
  168. { .name = "clk-s-d0-fs0-ch2", },
  169. { .name = "clk-s-d0-fs0-ch3", },
  170. };
  171. static const struct clkgen_quadfs_data_clks st_fs660c32_D0_data = {
  172. .data = (struct clkgen_quadfs_data *)&st_fs660c32_D,
  173. .outputs = st_fs660c32_D0_clks,
  174. };
  175. static const struct clkgen_clk_out st_fs660c32_D2_clks[] = {
  176. { .name = "clk-s-d2-fs0-ch0", },
  177. { .name = "clk-s-d2-fs0-ch1", },
  178. { .name = "clk-s-d2-fs0-ch2", },
  179. { .name = "clk-s-d2-fs0-ch3", },
  180. };
  181. static const struct clkgen_quadfs_data_clks st_fs660c32_D2_data = {
  182. .data = (struct clkgen_quadfs_data *)&st_fs660c32_D,
  183. .outputs = st_fs660c32_D2_clks,
  184. };
  185. static const struct clkgen_clk_out st_fs660c32_D3_clks[] = {
  186. { .name = "clk-s-d3-fs0-ch0", },
  187. { .name = "clk-s-d3-fs0-ch1", },
  188. { .name = "clk-s-d3-fs0-ch2", },
  189. { .name = "clk-s-d3-fs0-ch3", },
  190. };
  191. static const struct clkgen_quadfs_data_clks st_fs660c32_D3_data = {
  192. .data = (struct clkgen_quadfs_data *)&st_fs660c32_D,
  193. .outputs = st_fs660c32_D3_clks,
  194. };
  195. /**
  196. * DOC: A Frequency Synthesizer that multiples its input clock by a fixed factor
  197. *
  198. * Traits of this clock:
  199. * prepare - clk_(un)prepare only ensures parent is (un)prepared
  200. * enable - clk_enable and clk_disable are functional & control the Fsyn
  201. * rate - inherits rate from parent. set_rate/round_rate/recalc_rate
  202. * parent - fixed parent. No clk_set_parent support
  203. */
  204. /**
  205. * struct st_clk_quadfs_pll - A pll which outputs a fixed multiplier of
  206. * its parent clock, found inside a type of
  207. * ST quad channel frequency synthesizer block
  208. *
  209. * @hw: handle between common and hardware-specific interfaces.
  210. * @regs_base: base address of the configuration registers.
  211. * @lock: spinlock.
  212. * @data: local driver data
  213. * @ndiv: regmap field for the ndiv control.
  214. */
  215. struct st_clk_quadfs_pll {
  216. struct clk_hw hw;
  217. void __iomem *regs_base;
  218. spinlock_t *lock;
  219. struct clkgen_quadfs_data *data;
  220. u32 ndiv;
  221. };
  222. #define to_quadfs_pll(_hw) container_of(_hw, struct st_clk_quadfs_pll, hw)
  223. static int quadfs_pll_enable(struct clk_hw *hw)
  224. {
  225. struct st_clk_quadfs_pll *pll = to_quadfs_pll(hw);
  226. unsigned long flags = 0, timeout = jiffies + msecs_to_jiffies(10);
  227. if (pll->lock)
  228. spin_lock_irqsave(pll->lock, flags);
  229. /*
  230. * Bring block out of reset if we have reset control.
  231. */
  232. if (pll->data->reset_present)
  233. CLKGEN_WRITE(pll, nreset, 1);
  234. /*
  235. * Use a fixed input clock noise bandwidth filter for the moment
  236. */
  237. if (pll->data->bwfilter_present)
  238. CLKGEN_WRITE(pll, ref_bw, PLL_BW_GOODREF);
  239. CLKGEN_WRITE(pll, ndiv, pll->ndiv);
  240. /*
  241. * Power up the PLL
  242. */
  243. CLKGEN_WRITE(pll, npda, !pll->data->powerup_polarity);
  244. if (pll->lock)
  245. spin_unlock_irqrestore(pll->lock, flags);
  246. if (pll->data->lockstatus_present)
  247. while (!CLKGEN_READ(pll, lock_status)) {
  248. if (time_after(jiffies, timeout))
  249. return -ETIMEDOUT;
  250. cpu_relax();
  251. }
  252. return 0;
  253. }
  254. static void quadfs_pll_disable(struct clk_hw *hw)
  255. {
  256. struct st_clk_quadfs_pll *pll = to_quadfs_pll(hw);
  257. unsigned long flags = 0;
  258. if (pll->lock)
  259. spin_lock_irqsave(pll->lock, flags);
  260. /*
  261. * Powerdown the PLL and then put block into soft reset if we have
  262. * reset control.
  263. */
  264. CLKGEN_WRITE(pll, npda, pll->data->powerup_polarity);
  265. if (pll->data->reset_present)
  266. CLKGEN_WRITE(pll, nreset, 0);
  267. if (pll->lock)
  268. spin_unlock_irqrestore(pll->lock, flags);
  269. }
  270. static int quadfs_pll_is_enabled(struct clk_hw *hw)
  271. {
  272. struct st_clk_quadfs_pll *pll = to_quadfs_pll(hw);
  273. u32 npda = CLKGEN_READ(pll, npda);
  274. return pll->data->powerup_polarity ? !npda : !!npda;
  275. }
  276. static int clk_fs660c32_vco_get_rate(unsigned long input, struct stm_fs *fs,
  277. unsigned long *rate)
  278. {
  279. unsigned long nd = fs->ndiv + 16; /* ndiv value */
  280. *rate = input * nd;
  281. return 0;
  282. }
  283. static unsigned long quadfs_pll_fs660c32_recalc_rate(struct clk_hw *hw,
  284. unsigned long parent_rate)
  285. {
  286. struct st_clk_quadfs_pll *pll = to_quadfs_pll(hw);
  287. unsigned long rate = 0;
  288. struct stm_fs params;
  289. params.ndiv = CLKGEN_READ(pll, ndiv);
  290. if (clk_fs660c32_vco_get_rate(parent_rate, &params, &rate))
  291. pr_err("%s:%s error calculating rate\n",
  292. clk_hw_get_name(hw), __func__);
  293. pll->ndiv = params.ndiv;
  294. return rate;
  295. }
  296. static int clk_fs660c32_vco_get_params(unsigned long input,
  297. unsigned long output, struct stm_fs *fs)
  298. {
  299. /* Formula
  300. VCO frequency = (fin x ndiv) / pdiv
  301. ndiv = VCOfreq * pdiv / fin
  302. */
  303. unsigned long pdiv = 1, n;
  304. /* Output clock range: 384Mhz to 660Mhz */
  305. if (output < 384000000 || output > 660000000)
  306. return -EINVAL;
  307. if (input > 40000000)
  308. /* This means that PDIV would be 2 instead of 1.
  309. Not supported today. */
  310. return -EINVAL;
  311. input /= 1000;
  312. output /= 1000;
  313. n = output * pdiv / input;
  314. if (n < 16)
  315. n = 16;
  316. fs->ndiv = n - 16; /* Converting formula value to reg value */
  317. return 0;
  318. }
  319. static int quadfs_pll_fs660c32_determine_rate(struct clk_hw *hw,
  320. struct clk_rate_request *req)
  321. {
  322. struct stm_fs params;
  323. if (clk_fs660c32_vco_get_params(req->best_parent_rate, req->rate, &params))
  324. return 0;
  325. clk_fs660c32_vco_get_rate(req->best_parent_rate, &params, &req->rate);
  326. pr_debug("%s: %s new rate %ld [ndiv=%u]\n",
  327. __func__, clk_hw_get_name(hw),
  328. req->rate, (unsigned int)params.ndiv);
  329. return 0;
  330. }
  331. static int quadfs_pll_fs660c32_set_rate(struct clk_hw *hw, unsigned long rate,
  332. unsigned long parent_rate)
  333. {
  334. struct st_clk_quadfs_pll *pll = to_quadfs_pll(hw);
  335. struct stm_fs params;
  336. long hwrate = 0;
  337. unsigned long flags = 0;
  338. int ret;
  339. if (!rate || !parent_rate)
  340. return -EINVAL;
  341. ret = clk_fs660c32_vco_get_params(parent_rate, rate, &params);
  342. if (ret)
  343. return ret;
  344. clk_fs660c32_vco_get_rate(parent_rate, &params, &hwrate);
  345. pr_debug("%s: %s new rate %ld [ndiv=0x%x]\n",
  346. __func__, clk_hw_get_name(hw),
  347. hwrate, (unsigned int)params.ndiv);
  348. if (!hwrate)
  349. return -EINVAL;
  350. pll->ndiv = params.ndiv;
  351. if (pll->lock)
  352. spin_lock_irqsave(pll->lock, flags);
  353. CLKGEN_WRITE(pll, ndiv, pll->ndiv);
  354. if (pll->lock)
  355. spin_unlock_irqrestore(pll->lock, flags);
  356. return 0;
  357. }
  358. static const struct clk_ops st_quadfs_pll_c32_ops = {
  359. .enable = quadfs_pll_enable,
  360. .disable = quadfs_pll_disable,
  361. .is_enabled = quadfs_pll_is_enabled,
  362. .recalc_rate = quadfs_pll_fs660c32_recalc_rate,
  363. .determine_rate = quadfs_pll_fs660c32_determine_rate,
  364. .set_rate = quadfs_pll_fs660c32_set_rate,
  365. };
  366. static struct clk * __init st_clk_register_quadfs_pll(
  367. const char *name, const char *parent_name,
  368. struct clkgen_quadfs_data *quadfs, void __iomem *reg,
  369. spinlock_t *lock)
  370. {
  371. struct st_clk_quadfs_pll *pll;
  372. struct clk *clk;
  373. struct clk_init_data init;
  374. /*
  375. * Sanity check required pointers.
  376. */
  377. if (WARN_ON(!name || !parent_name))
  378. return ERR_PTR(-EINVAL);
  379. pll = kzalloc_obj(*pll);
  380. if (!pll)
  381. return ERR_PTR(-ENOMEM);
  382. init.name = name;
  383. init.ops = quadfs->pll_ops;
  384. init.flags = CLK_GET_RATE_NOCACHE;
  385. init.parent_names = &parent_name;
  386. init.num_parents = 1;
  387. pll->data = quadfs;
  388. pll->regs_base = reg;
  389. pll->lock = lock;
  390. pll->hw.init = &init;
  391. clk = clk_register(NULL, &pll->hw);
  392. if (IS_ERR(clk))
  393. kfree(pll);
  394. return clk;
  395. }
  396. /**
  397. * DOC: A digital frequency synthesizer
  398. *
  399. * Traits of this clock:
  400. * prepare - clk_(un)prepare only ensures parent is (un)prepared
  401. * enable - clk_enable and clk_disable are functional
  402. * rate - set rate is functional
  403. * parent - fixed parent. No clk_set_parent support
  404. */
  405. /*
  406. * struct st_clk_quadfs_fsynth - One clock output from a four channel digital
  407. * frequency synthesizer (fsynth) block.
  408. *
  409. * @hw: handle between common and hardware-specific interfaces
  410. *
  411. * @nsb: regmap field in the output control register for the digital
  412. * standby of this fsynth channel. This control is active low so
  413. * the channel is in standby when the control bit is cleared.
  414. *
  415. * @nsdiv: regmap field in the output control register for
  416. * for the optional divide by 3 of this fsynth channel. This control
  417. * is active low so the divide by 3 is active when the control bit is
  418. * cleared and the divide is bypassed when the bit is set.
  419. */
  420. struct st_clk_quadfs_fsynth {
  421. struct clk_hw hw;
  422. void __iomem *regs_base;
  423. spinlock_t *lock;
  424. struct clkgen_quadfs_data *data;
  425. u32 chan;
  426. /*
  427. * Cached hardware values from set_rate so we can program the
  428. * hardware in enable. There are two reasons for this:
  429. *
  430. * 1. The registers may not be writable until the parent has been
  431. * enabled.
  432. *
  433. * 2. It restores the clock rate when a driver does an enable
  434. * on PM restore, after a suspend to RAM has lost the hardware
  435. * setup.
  436. */
  437. u32 md;
  438. u32 pe;
  439. u32 sdiv;
  440. u32 nsdiv;
  441. };
  442. #define to_quadfs_fsynth(_hw) \
  443. container_of(_hw, struct st_clk_quadfs_fsynth, hw)
  444. static void quadfs_fsynth_program_enable(struct st_clk_quadfs_fsynth *fs)
  445. {
  446. /*
  447. * Pulse the program enable register lsb to make the hardware take
  448. * notice of the new md/pe values with a glitchless transition.
  449. */
  450. CLKGEN_WRITE(fs, en[fs->chan], 1);
  451. CLKGEN_WRITE(fs, en[fs->chan], 0);
  452. }
  453. static void quadfs_fsynth_program_rate(struct st_clk_quadfs_fsynth *fs)
  454. {
  455. unsigned long flags = 0;
  456. /*
  457. * Ensure the md/pe parameters are ignored while we are
  458. * reprogramming them so we can get a glitchless change
  459. * when fine tuning the speed of a running clock.
  460. */
  461. CLKGEN_WRITE(fs, en[fs->chan], 0);
  462. CLKGEN_WRITE(fs, mdiv[fs->chan], fs->md);
  463. CLKGEN_WRITE(fs, pe[fs->chan], fs->pe);
  464. CLKGEN_WRITE(fs, sdiv[fs->chan], fs->sdiv);
  465. if (fs->lock)
  466. spin_lock_irqsave(fs->lock, flags);
  467. if (fs->data->nsdiv_present)
  468. CLKGEN_WRITE(fs, nsdiv[fs->chan], fs->nsdiv);
  469. if (fs->lock)
  470. spin_unlock_irqrestore(fs->lock, flags);
  471. }
  472. static int quadfs_fsynth_enable(struct clk_hw *hw)
  473. {
  474. struct st_clk_quadfs_fsynth *fs = to_quadfs_fsynth(hw);
  475. unsigned long flags = 0;
  476. pr_debug("%s: %s\n", __func__, clk_hw_get_name(hw));
  477. quadfs_fsynth_program_rate(fs);
  478. if (fs->lock)
  479. spin_lock_irqsave(fs->lock, flags);
  480. CLKGEN_WRITE(fs, nsb[fs->chan], !fs->data->standby_polarity);
  481. if (fs->data->nrst_present)
  482. CLKGEN_WRITE(fs, nrst[fs->chan], 0);
  483. if (fs->lock)
  484. spin_unlock_irqrestore(fs->lock, flags);
  485. quadfs_fsynth_program_enable(fs);
  486. return 0;
  487. }
  488. static void quadfs_fsynth_disable(struct clk_hw *hw)
  489. {
  490. struct st_clk_quadfs_fsynth *fs = to_quadfs_fsynth(hw);
  491. unsigned long flags = 0;
  492. pr_debug("%s: %s\n", __func__, clk_hw_get_name(hw));
  493. if (fs->lock)
  494. spin_lock_irqsave(fs->lock, flags);
  495. CLKGEN_WRITE(fs, nsb[fs->chan], fs->data->standby_polarity);
  496. if (fs->lock)
  497. spin_unlock_irqrestore(fs->lock, flags);
  498. }
  499. static int quadfs_fsynth_is_enabled(struct clk_hw *hw)
  500. {
  501. struct st_clk_quadfs_fsynth *fs = to_quadfs_fsynth(hw);
  502. u32 nsb = CLKGEN_READ(fs, nsb[fs->chan]);
  503. pr_debug("%s: %s enable bit = 0x%x\n",
  504. __func__, clk_hw_get_name(hw), nsb);
  505. return fs->data->standby_polarity ? !nsb : !!nsb;
  506. }
  507. #define P20 (uint64_t)(1 << 20)
  508. static int clk_fs660c32_dig_get_rate(unsigned long input,
  509. const struct stm_fs *fs, unsigned long *rate)
  510. {
  511. unsigned long s = (1 << fs->sdiv);
  512. unsigned long ns;
  513. uint64_t res;
  514. /*
  515. * 'nsdiv' is a register value ('BIN') which is translated
  516. * to a decimal value according to following rules.
  517. *
  518. * nsdiv ns.dec
  519. * 0 3
  520. * 1 1
  521. */
  522. ns = (fs->nsdiv == 1) ? 1 : 3;
  523. res = (P20 * (32 + fs->mdiv) + 32 * fs->pe) * s * ns;
  524. *rate = (unsigned long)div64_u64(input * P20 * 32, res);
  525. return 0;
  526. }
  527. static int clk_fs660c32_get_pe(int m, int si, unsigned long *deviation,
  528. signed long input, unsigned long output, uint64_t *p,
  529. struct stm_fs *fs)
  530. {
  531. unsigned long new_freq, new_deviation;
  532. struct stm_fs fs_tmp;
  533. uint64_t val;
  534. val = (uint64_t)output << si;
  535. *p = (uint64_t)input * P20 - (32LL + (uint64_t)m) * val * (P20 / 32LL);
  536. *p = div64_u64(*p, val);
  537. if (*p > 32767LL)
  538. return 1;
  539. fs_tmp.mdiv = (unsigned long) m;
  540. fs_tmp.pe = (unsigned long)*p;
  541. fs_tmp.sdiv = si;
  542. fs_tmp.nsdiv = 1;
  543. clk_fs660c32_dig_get_rate(input, &fs_tmp, &new_freq);
  544. new_deviation = abs(output - new_freq);
  545. if (new_deviation < *deviation) {
  546. fs->mdiv = m;
  547. fs->pe = (unsigned long)*p;
  548. fs->sdiv = si;
  549. fs->nsdiv = 1;
  550. *deviation = new_deviation;
  551. }
  552. return 0;
  553. }
  554. static int clk_fs660c32_dig_get_params(unsigned long input,
  555. unsigned long output, struct stm_fs *fs)
  556. {
  557. int si; /* sdiv_reg (8 downto 0) */
  558. int m; /* md value */
  559. unsigned long new_freq, new_deviation;
  560. /* initial condition to say: "infinite deviation" */
  561. unsigned long deviation = ~0;
  562. uint64_t p, p1, p2; /* pe value */
  563. int r1, r2;
  564. struct stm_fs fs_tmp;
  565. for (si = 0; (si <= 8) && deviation; si++) {
  566. /* Boundary test to avoid useless iteration */
  567. r1 = clk_fs660c32_get_pe(0, si, &deviation,
  568. input, output, &p1, fs);
  569. r2 = clk_fs660c32_get_pe(31, si, &deviation,
  570. input, output, &p2, fs);
  571. /* No solution */
  572. if (r1 && r2 && (p1 > p2))
  573. continue;
  574. /* Try to find best deviation */
  575. for (m = 1; (m < 31) && deviation; m++)
  576. clk_fs660c32_get_pe(m, si, &deviation,
  577. input, output, &p, fs);
  578. }
  579. if (deviation == ~0) /* No solution found */
  580. return -1;
  581. /* pe fine tuning if deviation not 0: +/- 2 around computed pe value */
  582. if (deviation) {
  583. fs_tmp.mdiv = fs->mdiv;
  584. fs_tmp.sdiv = fs->sdiv;
  585. fs_tmp.nsdiv = fs->nsdiv;
  586. if (fs->pe > 2)
  587. p2 = fs->pe - 2;
  588. else
  589. p2 = 0;
  590. for (; p2 < 32768ll && (p2 <= (fs->pe + 2)); p2++) {
  591. fs_tmp.pe = (unsigned long)p2;
  592. clk_fs660c32_dig_get_rate(input, &fs_tmp, &new_freq);
  593. new_deviation = abs(output - new_freq);
  594. /* Check if this is a better solution */
  595. if (new_deviation < deviation) {
  596. fs->pe = (unsigned long)p2;
  597. deviation = new_deviation;
  598. }
  599. }
  600. }
  601. return 0;
  602. }
  603. static int quadfs_fsynt_get_hw_value_for_recalc(struct st_clk_quadfs_fsynth *fs,
  604. struct stm_fs *params)
  605. {
  606. /*
  607. * Get the initial hardware values for recalc_rate
  608. */
  609. params->mdiv = CLKGEN_READ(fs, mdiv[fs->chan]);
  610. params->pe = CLKGEN_READ(fs, pe[fs->chan]);
  611. params->sdiv = CLKGEN_READ(fs, sdiv[fs->chan]);
  612. if (fs->data->nsdiv_present)
  613. params->nsdiv = CLKGEN_READ(fs, nsdiv[fs->chan]);
  614. else
  615. params->nsdiv = 1;
  616. /*
  617. * If All are NULL then assume no clock rate is programmed.
  618. */
  619. if (!params->mdiv && !params->pe && !params->sdiv)
  620. return 1;
  621. fs->md = params->mdiv;
  622. fs->pe = params->pe;
  623. fs->sdiv = params->sdiv;
  624. fs->nsdiv = params->nsdiv;
  625. return 0;
  626. }
  627. static long quadfs_find_best_rate(struct clk_hw *hw, unsigned long drate,
  628. unsigned long prate, struct stm_fs *params)
  629. {
  630. struct st_clk_quadfs_fsynth *fs = to_quadfs_fsynth(hw);
  631. int (*clk_fs_get_rate)(unsigned long ,
  632. const struct stm_fs *, unsigned long *);
  633. int (*clk_fs_get_params)(unsigned long, unsigned long, struct stm_fs *);
  634. unsigned long rate = 0;
  635. clk_fs_get_rate = fs->data->get_rate;
  636. clk_fs_get_params = fs->data->get_params;
  637. if (!clk_fs_get_params(prate, drate, params))
  638. clk_fs_get_rate(prate, params, &rate);
  639. return rate;
  640. }
  641. static unsigned long quadfs_recalc_rate(struct clk_hw *hw,
  642. unsigned long parent_rate)
  643. {
  644. struct st_clk_quadfs_fsynth *fs = to_quadfs_fsynth(hw);
  645. unsigned long rate = 0;
  646. struct stm_fs params;
  647. int (*clk_fs_get_rate)(unsigned long ,
  648. const struct stm_fs *, unsigned long *);
  649. clk_fs_get_rate = fs->data->get_rate;
  650. if (quadfs_fsynt_get_hw_value_for_recalc(fs, &params))
  651. return 0;
  652. if (clk_fs_get_rate(parent_rate, &params, &rate)) {
  653. pr_err("%s:%s error calculating rate\n",
  654. clk_hw_get_name(hw), __func__);
  655. }
  656. pr_debug("%s:%s rate %lu\n", clk_hw_get_name(hw), __func__, rate);
  657. return rate;
  658. }
  659. static int quadfs_determine_rate(struct clk_hw *hw,
  660. struct clk_rate_request *req)
  661. {
  662. struct stm_fs params;
  663. req->rate = quadfs_find_best_rate(hw, req->rate,
  664. req->best_parent_rate, &params);
  665. pr_debug("%s: %s new rate %ld [sdiv=0x%x,md=0x%x,pe=0x%x,nsdiv3=%u]\n",
  666. __func__, clk_hw_get_name(hw),
  667. req->rate, (unsigned int)params.sdiv,
  668. (unsigned int)params.mdiv,
  669. (unsigned int)params.pe, (unsigned int)params.nsdiv);
  670. return 0;
  671. }
  672. static void quadfs_program_and_enable(struct st_clk_quadfs_fsynth *fs,
  673. struct stm_fs *params)
  674. {
  675. fs->md = params->mdiv;
  676. fs->pe = params->pe;
  677. fs->sdiv = params->sdiv;
  678. fs->nsdiv = params->nsdiv;
  679. /*
  680. * In some integrations you can only change the fsynth programming when
  681. * the parent entity containing it is enabled.
  682. */
  683. quadfs_fsynth_program_rate(fs);
  684. quadfs_fsynth_program_enable(fs);
  685. }
  686. static int quadfs_set_rate(struct clk_hw *hw, unsigned long rate,
  687. unsigned long parent_rate)
  688. {
  689. struct st_clk_quadfs_fsynth *fs = to_quadfs_fsynth(hw);
  690. struct stm_fs params;
  691. long hwrate;
  692. if (!rate || !parent_rate)
  693. return -EINVAL;
  694. memset(&params, 0, sizeof(struct stm_fs));
  695. hwrate = quadfs_find_best_rate(hw, rate, parent_rate, &params);
  696. if (!hwrate)
  697. return -EINVAL;
  698. quadfs_program_and_enable(fs, &params);
  699. return 0;
  700. }
  701. static const struct clk_ops st_quadfs_ops = {
  702. .enable = quadfs_fsynth_enable,
  703. .disable = quadfs_fsynth_disable,
  704. .is_enabled = quadfs_fsynth_is_enabled,
  705. .determine_rate = quadfs_determine_rate,
  706. .set_rate = quadfs_set_rate,
  707. .recalc_rate = quadfs_recalc_rate,
  708. };
  709. static struct clk * __init st_clk_register_quadfs_fsynth(
  710. const char *name, const char *parent_name,
  711. struct clkgen_quadfs_data *quadfs, void __iomem *reg, u32 chan,
  712. unsigned long flags, spinlock_t *lock)
  713. {
  714. struct st_clk_quadfs_fsynth *fs;
  715. struct clk *clk;
  716. struct clk_init_data init;
  717. /*
  718. * Sanity check required pointers, note that nsdiv3 is optional.
  719. */
  720. if (WARN_ON(!name || !parent_name))
  721. return ERR_PTR(-EINVAL);
  722. fs = kzalloc_obj(*fs);
  723. if (!fs)
  724. return ERR_PTR(-ENOMEM);
  725. init.name = name;
  726. init.ops = &st_quadfs_ops;
  727. init.flags = flags | CLK_GET_RATE_NOCACHE;
  728. init.parent_names = &parent_name;
  729. init.num_parents = 1;
  730. fs->data = quadfs;
  731. fs->regs_base = reg;
  732. fs->chan = chan;
  733. fs->lock = lock;
  734. fs->hw.init = &init;
  735. clk = clk_register(NULL, &fs->hw);
  736. if (IS_ERR(clk))
  737. kfree(fs);
  738. return clk;
  739. }
  740. static void __init st_of_create_quadfs_fsynths(
  741. struct device_node *np, const char *pll_name,
  742. struct clkgen_quadfs_data_clks *quadfs, void __iomem *reg,
  743. spinlock_t *lock)
  744. {
  745. struct clk_onecell_data *clk_data;
  746. int fschan;
  747. clk_data = kzalloc_obj(*clk_data);
  748. if (!clk_data)
  749. return;
  750. clk_data->clk_num = QUADFS_MAX_CHAN;
  751. clk_data->clks = kzalloc_objs(struct clk *, QUADFS_MAX_CHAN);
  752. if (!clk_data->clks) {
  753. kfree(clk_data);
  754. return;
  755. }
  756. for (fschan = 0; fschan < QUADFS_MAX_CHAN; fschan++) {
  757. struct clk *clk;
  758. const char *clk_name;
  759. unsigned long flags = 0;
  760. if (quadfs->outputs) {
  761. clk_name = quadfs->outputs[fschan].name;
  762. flags = quadfs->outputs[fschan].flags;
  763. } else {
  764. if (of_property_read_string_index(np,
  765. "clock-output-names",
  766. fschan, &clk_name))
  767. break;
  768. of_clk_detect_critical(np, fschan, &flags);
  769. }
  770. /*
  771. * If we read an empty clock name then the channel is unused
  772. */
  773. if (*clk_name == '\0')
  774. continue;
  775. clk = st_clk_register_quadfs_fsynth(clk_name, pll_name,
  776. quadfs->data, reg, fschan,
  777. flags, lock);
  778. /*
  779. * If there was an error registering this clock output, clean
  780. * up and move on to the next one.
  781. */
  782. if (!IS_ERR(clk)) {
  783. clk_data->clks[fschan] = clk;
  784. pr_debug("%s: parent %s rate %u\n",
  785. __clk_get_name(clk),
  786. __clk_get_name(clk_get_parent(clk)),
  787. (unsigned int)clk_get_rate(clk));
  788. }
  789. }
  790. of_clk_add_provider(np, of_clk_src_onecell_get, clk_data);
  791. }
  792. static void __init st_of_quadfs_setup(struct device_node *np,
  793. struct clkgen_quadfs_data_clks *datac)
  794. {
  795. struct clk *clk;
  796. const char *pll_name, *clk_parent_name;
  797. void __iomem *reg;
  798. spinlock_t *lock;
  799. struct device_node *parent_np;
  800. /*
  801. * First check for reg property within the node to keep backward
  802. * compatibility, then if reg doesn't exist look at the parent node
  803. */
  804. reg = of_iomap(np, 0);
  805. if (!reg) {
  806. parent_np = of_get_parent(np);
  807. reg = of_iomap(parent_np, 0);
  808. of_node_put(parent_np);
  809. if (!reg) {
  810. pr_err("%s: Failed to get base address\n", __func__);
  811. return;
  812. }
  813. }
  814. clk_parent_name = of_clk_get_parent_name(np, 0);
  815. if (!clk_parent_name)
  816. return;
  817. pll_name = kasprintf(GFP_KERNEL, "%pOFn.pll", np);
  818. if (!pll_name)
  819. return;
  820. lock = kzalloc_obj(*lock);
  821. if (!lock)
  822. goto err_exit;
  823. spin_lock_init(lock);
  824. clk = st_clk_register_quadfs_pll(pll_name, clk_parent_name, datac->data,
  825. reg, lock);
  826. if (IS_ERR(clk)) {
  827. kfree(lock);
  828. goto err_exit;
  829. } else
  830. pr_debug("%s: parent %s rate %u\n",
  831. __clk_get_name(clk),
  832. __clk_get_name(clk_get_parent(clk)),
  833. (unsigned int)clk_get_rate(clk));
  834. st_of_create_quadfs_fsynths(np, pll_name, datac, reg, lock);
  835. err_exit:
  836. kfree(pll_name); /* No longer need local copy of the PLL name */
  837. }
  838. static void __init st_of_quadfs660C_setup(struct device_node *np)
  839. {
  840. st_of_quadfs_setup(np,
  841. (struct clkgen_quadfs_data_clks *) &st_fs660c32_C_data);
  842. }
  843. CLK_OF_DECLARE(quadfs660C, "st,quadfs-pll", st_of_quadfs660C_setup);
  844. static void __init st_of_quadfs660D_setup(struct device_node *np)
  845. {
  846. st_of_quadfs_setup(np,
  847. (struct clkgen_quadfs_data_clks *) &st_fs660c32_D_data);
  848. }
  849. CLK_OF_DECLARE(quadfs660D, "st,quadfs", st_of_quadfs660D_setup);
  850. static void __init st_of_quadfs660D0_setup(struct device_node *np)
  851. {
  852. st_of_quadfs_setup(np,
  853. (struct clkgen_quadfs_data_clks *) &st_fs660c32_D0_data);
  854. }
  855. CLK_OF_DECLARE(quadfs660D0, "st,quadfs-d0", st_of_quadfs660D0_setup);
  856. static void __init st_of_quadfs660D2_setup(struct device_node *np)
  857. {
  858. st_of_quadfs_setup(np,
  859. (struct clkgen_quadfs_data_clks *) &st_fs660c32_D2_data);
  860. }
  861. CLK_OF_DECLARE(quadfs660D2, "st,quadfs-d2", st_of_quadfs660D2_setup);
  862. static void __init st_of_quadfs660D3_setup(struct device_node *np)
  863. {
  864. st_of_quadfs_setup(np,
  865. (struct clkgen_quadfs_data_clks *) &st_fs660c32_D3_data);
  866. }
  867. CLK_OF_DECLARE(quadfs660D3, "st,quadfs-d3", st_of_quadfs660D3_setup);