clkgen-fsyn.c 26 KB

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