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 <[email protected]>,
  8. * Pankaj Dev <[email protected]>.
  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 long quadfs_pll_fs660c32_round_rate(struct clk_hw *hw,
  320. unsigned long rate,
  321. unsigned long *prate)
  322. {
  323. struct stm_fs params;
  324. if (clk_fs660c32_vco_get_params(*prate, rate, &params))
  325. return rate;
  326. clk_fs660c32_vco_get_rate(*prate, &params, &rate);
  327. pr_debug("%s: %s new rate %ld [ndiv=%u]\n",
  328. __func__, clk_hw_get_name(hw),
  329. rate, (unsigned int)params.ndiv);
  330. return rate;
  331. }
  332. static int quadfs_pll_fs660c32_set_rate(struct clk_hw *hw, unsigned long rate,
  333. unsigned long parent_rate)
  334. {
  335. struct st_clk_quadfs_pll *pll = to_quadfs_pll(hw);
  336. struct stm_fs params;
  337. long hwrate = 0;
  338. unsigned long flags = 0;
  339. int ret;
  340. if (!rate || !parent_rate)
  341. return -EINVAL;
  342. ret = clk_fs660c32_vco_get_params(parent_rate, rate, &params);
  343. if (ret)
  344. return ret;
  345. clk_fs660c32_vco_get_rate(parent_rate, &params, &hwrate);
  346. pr_debug("%s: %s new rate %ld [ndiv=0x%x]\n",
  347. __func__, clk_hw_get_name(hw),
  348. hwrate, (unsigned int)params.ndiv);
  349. if (!hwrate)
  350. return -EINVAL;
  351. pll->ndiv = params.ndiv;
  352. if (pll->lock)
  353. spin_lock_irqsave(pll->lock, flags);
  354. CLKGEN_WRITE(pll, ndiv, pll->ndiv);
  355. if (pll->lock)
  356. spin_unlock_irqrestore(pll->lock, flags);
  357. return 0;
  358. }
  359. static const struct clk_ops st_quadfs_pll_c32_ops = {
  360. .enable = quadfs_pll_enable,
  361. .disable = quadfs_pll_disable,
  362. .is_enabled = quadfs_pll_is_enabled,
  363. .recalc_rate = quadfs_pll_fs660c32_recalc_rate,
  364. .round_rate = quadfs_pll_fs660c32_round_rate,
  365. .set_rate = quadfs_pll_fs660c32_set_rate,
  366. };
  367. static struct clk * __init st_clk_register_quadfs_pll(
  368. const char *name, const char *parent_name,
  369. struct clkgen_quadfs_data *quadfs, void __iomem *reg,
  370. spinlock_t *lock)
  371. {
  372. struct st_clk_quadfs_pll *pll;
  373. struct clk *clk;
  374. struct clk_init_data init;
  375. /*
  376. * Sanity check required pointers.
  377. */
  378. if (WARN_ON(!name || !parent_name))
  379. return ERR_PTR(-EINVAL);
  380. pll = kzalloc(sizeof(*pll), GFP_KERNEL);
  381. if (!pll)
  382. return ERR_PTR(-ENOMEM);
  383. init.name = name;
  384. init.ops = quadfs->pll_ops;
  385. init.flags = CLK_GET_RATE_NOCACHE;
  386. init.parent_names = &parent_name;
  387. init.num_parents = 1;
  388. pll->data = quadfs;
  389. pll->regs_base = reg;
  390. pll->lock = lock;
  391. pll->hw.init = &init;
  392. clk = clk_register(NULL, &pll->hw);
  393. if (IS_ERR(clk))
  394. kfree(pll);
  395. return clk;
  396. }
  397. /**
  398. * DOC: A digital frequency synthesizer
  399. *
  400. * Traits of this clock:
  401. * prepare - clk_(un)prepare only ensures parent is (un)prepared
  402. * enable - clk_enable and clk_disable are functional
  403. * rate - set rate is functional
  404. * parent - fixed parent. No clk_set_parent support
  405. */
  406. /*
  407. * struct st_clk_quadfs_fsynth - One clock output from a four channel digital
  408. * frequency synthesizer (fsynth) block.
  409. *
  410. * @hw: handle between common and hardware-specific interfaces
  411. *
  412. * @nsb: regmap field in the output control register for the digital
  413. * standby of this fsynth channel. This control is active low so
  414. * the channel is in standby when the control bit is cleared.
  415. *
  416. * @nsdiv: regmap field in the output control register for
  417. * for the optional divide by 3 of this fsynth channel. This control
  418. * is active low so the divide by 3 is active when the control bit is
  419. * cleared and the divide is bypassed when the bit is set.
  420. */
  421. struct st_clk_quadfs_fsynth {
  422. struct clk_hw hw;
  423. void __iomem *regs_base;
  424. spinlock_t *lock;
  425. struct clkgen_quadfs_data *data;
  426. u32 chan;
  427. /*
  428. * Cached hardware values from set_rate so we can program the
  429. * hardware in enable. There are two reasons for this:
  430. *
  431. * 1. The registers may not be writable until the parent has been
  432. * enabled.
  433. *
  434. * 2. It restores the clock rate when a driver does an enable
  435. * on PM restore, after a suspend to RAM has lost the hardware
  436. * setup.
  437. */
  438. u32 md;
  439. u32 pe;
  440. u32 sdiv;
  441. u32 nsdiv;
  442. };
  443. #define to_quadfs_fsynth(_hw) \
  444. container_of(_hw, struct st_clk_quadfs_fsynth, hw)
  445. static void quadfs_fsynth_program_enable(struct st_clk_quadfs_fsynth *fs)
  446. {
  447. /*
  448. * Pulse the program enable register lsb to make the hardware take
  449. * notice of the new md/pe values with a glitchless transition.
  450. */
  451. CLKGEN_WRITE(fs, en[fs->chan], 1);
  452. CLKGEN_WRITE(fs, en[fs->chan], 0);
  453. }
  454. static void quadfs_fsynth_program_rate(struct st_clk_quadfs_fsynth *fs)
  455. {
  456. unsigned long flags = 0;
  457. /*
  458. * Ensure the md/pe parameters are ignored while we are
  459. * reprogramming them so we can get a glitchless change
  460. * when fine tuning the speed of a running clock.
  461. */
  462. CLKGEN_WRITE(fs, en[fs->chan], 0);
  463. CLKGEN_WRITE(fs, mdiv[fs->chan], fs->md);
  464. CLKGEN_WRITE(fs, pe[fs->chan], fs->pe);
  465. CLKGEN_WRITE(fs, sdiv[fs->chan], fs->sdiv);
  466. if (fs->lock)
  467. spin_lock_irqsave(fs->lock, flags);
  468. if (fs->data->nsdiv_present)
  469. CLKGEN_WRITE(fs, nsdiv[fs->chan], fs->nsdiv);
  470. if (fs->lock)
  471. spin_unlock_irqrestore(fs->lock, flags);
  472. }
  473. static int quadfs_fsynth_enable(struct clk_hw *hw)
  474. {
  475. struct st_clk_quadfs_fsynth *fs = to_quadfs_fsynth(hw);
  476. unsigned long flags = 0;
  477. pr_debug("%s: %s\n", __func__, clk_hw_get_name(hw));
  478. quadfs_fsynth_program_rate(fs);
  479. if (fs->lock)
  480. spin_lock_irqsave(fs->lock, flags);
  481. CLKGEN_WRITE(fs, nsb[fs->chan], !fs->data->standby_polarity);
  482. if (fs->data->nrst_present)
  483. CLKGEN_WRITE(fs, nrst[fs->chan], 0);
  484. if (fs->lock)
  485. spin_unlock_irqrestore(fs->lock, flags);
  486. quadfs_fsynth_program_enable(fs);
  487. return 0;
  488. }
  489. static void quadfs_fsynth_disable(struct clk_hw *hw)
  490. {
  491. struct st_clk_quadfs_fsynth *fs = to_quadfs_fsynth(hw);
  492. unsigned long flags = 0;
  493. pr_debug("%s: %s\n", __func__, clk_hw_get_name(hw));
  494. if (fs->lock)
  495. spin_lock_irqsave(fs->lock, flags);
  496. CLKGEN_WRITE(fs, nsb[fs->chan], fs->data->standby_polarity);
  497. if (fs->lock)
  498. spin_unlock_irqrestore(fs->lock, flags);
  499. }
  500. static int quadfs_fsynth_is_enabled(struct clk_hw *hw)
  501. {
  502. struct st_clk_quadfs_fsynth *fs = to_quadfs_fsynth(hw);
  503. u32 nsb = CLKGEN_READ(fs, nsb[fs->chan]);
  504. pr_debug("%s: %s enable bit = 0x%x\n",
  505. __func__, clk_hw_get_name(hw), nsb);
  506. return fs->data->standby_polarity ? !nsb : !!nsb;
  507. }
  508. #define P20 (uint64_t)(1 << 20)
  509. static int clk_fs660c32_dig_get_rate(unsigned long input,
  510. const struct stm_fs *fs, unsigned long *rate)
  511. {
  512. unsigned long s = (1 << fs->sdiv);
  513. unsigned long ns;
  514. uint64_t res;
  515. /*
  516. * 'nsdiv' is a register value ('BIN') which is translated
  517. * to a decimal value according to following rules.
  518. *
  519. * nsdiv ns.dec
  520. * 0 3
  521. * 1 1
  522. */
  523. ns = (fs->nsdiv == 1) ? 1 : 3;
  524. res = (P20 * (32 + fs->mdiv) + 32 * fs->pe) * s * ns;
  525. *rate = (unsigned long)div64_u64(input * P20 * 32, res);
  526. return 0;
  527. }
  528. static int clk_fs660c32_get_pe(int m, int si, unsigned long *deviation,
  529. signed long input, unsigned long output, uint64_t *p,
  530. struct stm_fs *fs)
  531. {
  532. unsigned long new_freq, new_deviation;
  533. struct stm_fs fs_tmp;
  534. uint64_t val;
  535. val = (uint64_t)output << si;
  536. *p = (uint64_t)input * P20 - (32LL + (uint64_t)m) * val * (P20 / 32LL);
  537. *p = div64_u64(*p, val);
  538. if (*p > 32767LL)
  539. return 1;
  540. fs_tmp.mdiv = (unsigned long) m;
  541. fs_tmp.pe = (unsigned long)*p;
  542. fs_tmp.sdiv = si;
  543. fs_tmp.nsdiv = 1;
  544. clk_fs660c32_dig_get_rate(input, &fs_tmp, &new_freq);
  545. new_deviation = abs(output - new_freq);
  546. if (new_deviation < *deviation) {
  547. fs->mdiv = m;
  548. fs->pe = (unsigned long)*p;
  549. fs->sdiv = si;
  550. fs->nsdiv = 1;
  551. *deviation = new_deviation;
  552. }
  553. return 0;
  554. }
  555. static int clk_fs660c32_dig_get_params(unsigned long input,
  556. unsigned long output, struct stm_fs *fs)
  557. {
  558. int si; /* sdiv_reg (8 downto 0) */
  559. int m; /* md value */
  560. unsigned long new_freq, new_deviation;
  561. /* initial condition to say: "infinite deviation" */
  562. unsigned long deviation = ~0;
  563. uint64_t p, p1, p2; /* pe value */
  564. int r1, r2;
  565. struct stm_fs fs_tmp;
  566. for (si = 0; (si <= 8) && deviation; si++) {
  567. /* Boundary test to avoid useless iteration */
  568. r1 = clk_fs660c32_get_pe(0, si, &deviation,
  569. input, output, &p1, fs);
  570. r2 = clk_fs660c32_get_pe(31, si, &deviation,
  571. input, output, &p2, fs);
  572. /* No solution */
  573. if (r1 && r2 && (p1 > p2))
  574. continue;
  575. /* Try to find best deviation */
  576. for (m = 1; (m < 31) && deviation; m++)
  577. clk_fs660c32_get_pe(m, si, &deviation,
  578. input, output, &p, fs);
  579. }
  580. if (deviation == ~0) /* No solution found */
  581. return -1;
  582. /* pe fine tuning if deviation not 0: +/- 2 around computed pe value */
  583. if (deviation) {
  584. fs_tmp.mdiv = fs->mdiv;
  585. fs_tmp.sdiv = fs->sdiv;
  586. fs_tmp.nsdiv = fs->nsdiv;
  587. if (fs->pe > 2)
  588. p2 = fs->pe - 2;
  589. else
  590. p2 = 0;
  591. for (; p2 < 32768ll && (p2 <= (fs->pe + 2)); p2++) {
  592. fs_tmp.pe = (unsigned long)p2;
  593. clk_fs660c32_dig_get_rate(input, &fs_tmp, &new_freq);
  594. new_deviation = abs(output - new_freq);
  595. /* Check if this is a better solution */
  596. if (new_deviation < deviation) {
  597. fs->pe = (unsigned long)p2;
  598. deviation = new_deviation;
  599. }
  600. }
  601. }
  602. return 0;
  603. }
  604. static int quadfs_fsynt_get_hw_value_for_recalc(struct st_clk_quadfs_fsynth *fs,
  605. struct stm_fs *params)
  606. {
  607. /*
  608. * Get the initial hardware values for recalc_rate
  609. */
  610. params->mdiv = CLKGEN_READ(fs, mdiv[fs->chan]);
  611. params->pe = CLKGEN_READ(fs, pe[fs->chan]);
  612. params->sdiv = CLKGEN_READ(fs, sdiv[fs->chan]);
  613. if (fs->data->nsdiv_present)
  614. params->nsdiv = CLKGEN_READ(fs, nsdiv[fs->chan]);
  615. else
  616. params->nsdiv = 1;
  617. /*
  618. * If All are NULL then assume no clock rate is programmed.
  619. */
  620. if (!params->mdiv && !params->pe && !params->sdiv)
  621. return 1;
  622. fs->md = params->mdiv;
  623. fs->pe = params->pe;
  624. fs->sdiv = params->sdiv;
  625. fs->nsdiv = params->nsdiv;
  626. return 0;
  627. }
  628. static long quadfs_find_best_rate(struct clk_hw *hw, unsigned long drate,
  629. unsigned long prate, struct stm_fs *params)
  630. {
  631. struct st_clk_quadfs_fsynth *fs = to_quadfs_fsynth(hw);
  632. int (*clk_fs_get_rate)(unsigned long ,
  633. const struct stm_fs *, unsigned long *);
  634. int (*clk_fs_get_params)(unsigned long, unsigned long, struct stm_fs *);
  635. unsigned long rate = 0;
  636. clk_fs_get_rate = fs->data->get_rate;
  637. clk_fs_get_params = fs->data->get_params;
  638. if (!clk_fs_get_params(prate, drate, params))
  639. clk_fs_get_rate(prate, params, &rate);
  640. return rate;
  641. }
  642. static unsigned long quadfs_recalc_rate(struct clk_hw *hw,
  643. unsigned long parent_rate)
  644. {
  645. struct st_clk_quadfs_fsynth *fs = to_quadfs_fsynth(hw);
  646. unsigned long rate = 0;
  647. struct stm_fs params;
  648. int (*clk_fs_get_rate)(unsigned long ,
  649. const struct stm_fs *, unsigned long *);
  650. clk_fs_get_rate = fs->data->get_rate;
  651. if (quadfs_fsynt_get_hw_value_for_recalc(fs, &params))
  652. return 0;
  653. if (clk_fs_get_rate(parent_rate, &params, &rate)) {
  654. pr_err("%s:%s error calculating rate\n",
  655. clk_hw_get_name(hw), __func__);
  656. }
  657. pr_debug("%s:%s rate %lu\n", clk_hw_get_name(hw), __func__, rate);
  658. return rate;
  659. }
  660. static long quadfs_round_rate(struct clk_hw *hw, unsigned long rate,
  661. unsigned long *prate)
  662. {
  663. struct stm_fs params;
  664. rate = quadfs_find_best_rate(hw, rate, *prate, &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. rate, (unsigned int)params.sdiv, (unsigned int)params.mdiv,
  668. (unsigned int)params.pe, (unsigned int)params.nsdiv);
  669. return rate;
  670. }
  671. static void quadfs_program_and_enable(struct st_clk_quadfs_fsynth *fs,
  672. struct stm_fs *params)
  673. {
  674. fs->md = params->mdiv;
  675. fs->pe = params->pe;
  676. fs->sdiv = params->sdiv;
  677. fs->nsdiv = params->nsdiv;
  678. /*
  679. * In some integrations you can only change the fsynth programming when
  680. * the parent entity containing it is enabled.
  681. */
  682. quadfs_fsynth_program_rate(fs);
  683. quadfs_fsynth_program_enable(fs);
  684. }
  685. static int quadfs_set_rate(struct clk_hw *hw, unsigned long rate,
  686. unsigned long parent_rate)
  687. {
  688. struct st_clk_quadfs_fsynth *fs = to_quadfs_fsynth(hw);
  689. struct stm_fs params;
  690. long hwrate;
  691. if (!rate || !parent_rate)
  692. return -EINVAL;
  693. memset(&params, 0, sizeof(struct stm_fs));
  694. hwrate = quadfs_find_best_rate(hw, rate, parent_rate, &params);
  695. if (!hwrate)
  696. return -EINVAL;
  697. quadfs_program_and_enable(fs, &params);
  698. return 0;
  699. }
  700. static const struct clk_ops st_quadfs_ops = {
  701. .enable = quadfs_fsynth_enable,
  702. .disable = quadfs_fsynth_disable,
  703. .is_enabled = quadfs_fsynth_is_enabled,
  704. .round_rate = quadfs_round_rate,
  705. .set_rate = quadfs_set_rate,
  706. .recalc_rate = quadfs_recalc_rate,
  707. };
  708. static struct clk * __init st_clk_register_quadfs_fsynth(
  709. const char *name, const char *parent_name,
  710. struct clkgen_quadfs_data *quadfs, void __iomem *reg, u32 chan,
  711. unsigned long flags, spinlock_t *lock)
  712. {
  713. struct st_clk_quadfs_fsynth *fs;
  714. struct clk *clk;
  715. struct clk_init_data init;
  716. /*
  717. * Sanity check required pointers, note that nsdiv3 is optional.
  718. */
  719. if (WARN_ON(!name || !parent_name))
  720. return ERR_PTR(-EINVAL);
  721. fs = kzalloc(sizeof(*fs), GFP_KERNEL);
  722. if (!fs)
  723. return ERR_PTR(-ENOMEM);
  724. init.name = name;
  725. init.ops = &st_quadfs_ops;
  726. init.flags = flags | CLK_GET_RATE_NOCACHE;
  727. init.parent_names = &parent_name;
  728. init.num_parents = 1;
  729. fs->data = quadfs;
  730. fs->regs_base = reg;
  731. fs->chan = chan;
  732. fs->lock = lock;
  733. fs->hw.init = &init;
  734. clk = clk_register(NULL, &fs->hw);
  735. if (IS_ERR(clk))
  736. kfree(fs);
  737. return clk;
  738. }
  739. static void __init st_of_create_quadfs_fsynths(
  740. struct device_node *np, const char *pll_name,
  741. struct clkgen_quadfs_data_clks *quadfs, void __iomem *reg,
  742. spinlock_t *lock)
  743. {
  744. struct clk_onecell_data *clk_data;
  745. int fschan;
  746. clk_data = kzalloc(sizeof(*clk_data), GFP_KERNEL);
  747. if (!clk_data)
  748. return;
  749. clk_data->clk_num = QUADFS_MAX_CHAN;
  750. clk_data->clks = kcalloc(QUADFS_MAX_CHAN, sizeof(struct clk *),
  751. GFP_KERNEL);
  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(sizeof(*lock), GFP_KERNEL);
  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);