clk-sam9x60-pll.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2019 Microchip Technology Inc.
  4. *
  5. */
  6. #include <linux/bitfield.h>
  7. #include <linux/clk.h>
  8. #include <linux/clk-provider.h>
  9. #include <linux/clkdev.h>
  10. #include <linux/clk/at91_pmc.h>
  11. #include <linux/of.h>
  12. #include <linux/mfd/syscon.h>
  13. #include <linux/regmap.h>
  14. #include "pmc.h"
  15. #define PMC_PLL_CTRL0_DIV_MSK GENMASK(7, 0)
  16. #define PMC_PLL_CTRL1_MUL_MSK GENMASK(31, 24)
  17. #define PMC_PLL_CTRL1_FRACR_MSK GENMASK(21, 0)
  18. #define PLL_DIV_MAX (FIELD_GET(PMC_PLL_CTRL0_DIV_MSK, UINT_MAX) + 1)
  19. #define UPLL_DIV 2
  20. #define PLL_MUL_MAX (FIELD_GET(PMC_PLL_CTRL1_MUL_MSK, UINT_MAX) + 1)
  21. #define PLL_MAX_ID 7
  22. struct sam9x60_pll_core {
  23. struct regmap *regmap;
  24. spinlock_t *lock;
  25. const struct clk_pll_characteristics *characteristics;
  26. const struct clk_pll_layout *layout;
  27. struct clk_hw hw;
  28. u8 id;
  29. };
  30. struct sam9x60_frac {
  31. struct sam9x60_pll_core core;
  32. struct at91_clk_pms pms;
  33. u32 frac;
  34. u16 mul;
  35. };
  36. struct sam9x60_div {
  37. struct sam9x60_pll_core core;
  38. struct at91_clk_pms pms;
  39. u8 div;
  40. u8 safe_div;
  41. };
  42. #define to_sam9x60_pll_core(hw) container_of(hw, struct sam9x60_pll_core, hw)
  43. #define to_sam9x60_frac(core) container_of(core, struct sam9x60_frac, core)
  44. #define to_sam9x60_div(core) container_of(core, struct sam9x60_div, core)
  45. static struct sam9x60_div *notifier_div;
  46. static inline bool sam9x60_pll_ready(struct regmap *regmap, int id)
  47. {
  48. unsigned int status;
  49. regmap_read(regmap, AT91_PMC_PLL_ISR0, &status);
  50. return !!(status & BIT(id));
  51. }
  52. static bool sam9x60_frac_pll_ready(struct regmap *regmap, u8 id)
  53. {
  54. return sam9x60_pll_ready(regmap, id);
  55. }
  56. static unsigned long sam9x60_frac_pll_recalc_rate(struct clk_hw *hw,
  57. unsigned long parent_rate)
  58. {
  59. struct sam9x60_pll_core *core = to_sam9x60_pll_core(hw);
  60. struct sam9x60_frac *frac = to_sam9x60_frac(core);
  61. unsigned long freq;
  62. freq = parent_rate * (frac->mul + 1) +
  63. DIV_ROUND_CLOSEST_ULL((u64)parent_rate * frac->frac, (1 << 22));
  64. if (core->layout->div2)
  65. freq >>= 1;
  66. return freq;
  67. }
  68. static int sam9x60_frac_pll_set(struct sam9x60_pll_core *core)
  69. {
  70. struct sam9x60_frac *frac = to_sam9x60_frac(core);
  71. struct regmap *regmap = core->regmap;
  72. unsigned int val, cfrac, cmul;
  73. unsigned long flags;
  74. spin_lock_irqsave(core->lock, flags);
  75. regmap_update_bits(regmap, AT91_PMC_PLL_UPDT,
  76. AT91_PMC_PLL_UPDT_ID_MSK, core->id);
  77. regmap_read(regmap, AT91_PMC_PLL_CTRL1, &val);
  78. cmul = (val & core->layout->mul_mask) >> core->layout->mul_shift;
  79. cfrac = (val & core->layout->frac_mask) >> core->layout->frac_shift;
  80. if (sam9x60_frac_pll_ready(regmap, core->id) &&
  81. (cmul == frac->mul && cfrac == frac->frac))
  82. goto unlock;
  83. /* Recommended value for PMC_PLL_ACR */
  84. if (core->characteristics->upll)
  85. val = AT91_PMC_PLL_ACR_DEFAULT_UPLL;
  86. else
  87. val = AT91_PMC_PLL_ACR_DEFAULT_PLLA;
  88. regmap_write(regmap, AT91_PMC_PLL_ACR, val);
  89. regmap_write(regmap, AT91_PMC_PLL_CTRL1,
  90. (frac->mul << core->layout->mul_shift) |
  91. (frac->frac << core->layout->frac_shift));
  92. if (core->characteristics->upll) {
  93. /* Enable the UTMI internal bandgap */
  94. val |= AT91_PMC_PLL_ACR_UTMIBG;
  95. regmap_write(regmap, AT91_PMC_PLL_ACR, val);
  96. udelay(10);
  97. /* Enable the UTMI internal regulator */
  98. val |= AT91_PMC_PLL_ACR_UTMIVR;
  99. regmap_write(regmap, AT91_PMC_PLL_ACR, val);
  100. udelay(10);
  101. }
  102. regmap_update_bits(regmap, AT91_PMC_PLL_UPDT,
  103. AT91_PMC_PLL_UPDT_UPDATE | AT91_PMC_PLL_UPDT_ID_MSK,
  104. AT91_PMC_PLL_UPDT_UPDATE | core->id);
  105. regmap_update_bits(regmap, AT91_PMC_PLL_CTRL0,
  106. AT91_PMC_PLL_CTRL0_ENLOCK | AT91_PMC_PLL_CTRL0_ENPLL,
  107. AT91_PMC_PLL_CTRL0_ENLOCK | AT91_PMC_PLL_CTRL0_ENPLL);
  108. regmap_update_bits(regmap, AT91_PMC_PLL_UPDT,
  109. AT91_PMC_PLL_UPDT_UPDATE | AT91_PMC_PLL_UPDT_ID_MSK,
  110. AT91_PMC_PLL_UPDT_UPDATE | core->id);
  111. while (!sam9x60_pll_ready(regmap, core->id))
  112. cpu_relax();
  113. unlock:
  114. spin_unlock_irqrestore(core->lock, flags);
  115. return 0;
  116. }
  117. static int sam9x60_frac_pll_prepare(struct clk_hw *hw)
  118. {
  119. struct sam9x60_pll_core *core = to_sam9x60_pll_core(hw);
  120. return sam9x60_frac_pll_set(core);
  121. }
  122. static void sam9x60_frac_pll_unprepare(struct clk_hw *hw)
  123. {
  124. struct sam9x60_pll_core *core = to_sam9x60_pll_core(hw);
  125. struct regmap *regmap = core->regmap;
  126. unsigned long flags;
  127. spin_lock_irqsave(core->lock, flags);
  128. regmap_update_bits(regmap, AT91_PMC_PLL_UPDT,
  129. AT91_PMC_PLL_UPDT_ID_MSK, core->id);
  130. regmap_update_bits(regmap, AT91_PMC_PLL_CTRL0, AT91_PMC_PLL_CTRL0_ENPLL, 0);
  131. if (core->characteristics->upll)
  132. regmap_update_bits(regmap, AT91_PMC_PLL_ACR,
  133. AT91_PMC_PLL_ACR_UTMIBG | AT91_PMC_PLL_ACR_UTMIVR, 0);
  134. regmap_update_bits(regmap, AT91_PMC_PLL_UPDT,
  135. AT91_PMC_PLL_UPDT_UPDATE | AT91_PMC_PLL_UPDT_ID_MSK,
  136. AT91_PMC_PLL_UPDT_UPDATE | core->id);
  137. spin_unlock_irqrestore(core->lock, flags);
  138. }
  139. static int sam9x60_frac_pll_is_prepared(struct clk_hw *hw)
  140. {
  141. struct sam9x60_pll_core *core = to_sam9x60_pll_core(hw);
  142. return sam9x60_pll_ready(core->regmap, core->id);
  143. }
  144. static long sam9x60_frac_pll_compute_mul_frac(struct sam9x60_pll_core *core,
  145. unsigned long rate,
  146. unsigned long parent_rate,
  147. bool update)
  148. {
  149. struct sam9x60_frac *frac = to_sam9x60_frac(core);
  150. unsigned long tmprate, remainder;
  151. unsigned long nmul = 0;
  152. unsigned long nfrac = 0;
  153. if (rate < core->characteristics->core_output[0].min ||
  154. rate > core->characteristics->core_output[0].max)
  155. return -ERANGE;
  156. /*
  157. * Calculate the multiplier associated with the current
  158. * divider that provide the closest rate to the requested one.
  159. */
  160. nmul = mult_frac(rate, 1, parent_rate);
  161. tmprate = mult_frac(parent_rate, nmul, 1);
  162. remainder = rate - tmprate;
  163. if (remainder) {
  164. nfrac = DIV_ROUND_CLOSEST_ULL((u64)remainder * (1 << 22),
  165. parent_rate);
  166. tmprate += DIV_ROUND_CLOSEST_ULL((u64)nfrac * parent_rate,
  167. (1 << 22));
  168. }
  169. /* Check if resulted rate is a valid. */
  170. if (tmprate < core->characteristics->core_output[0].min ||
  171. tmprate > core->characteristics->core_output[0].max)
  172. return -ERANGE;
  173. if (update) {
  174. frac->mul = nmul - 1;
  175. frac->frac = nfrac;
  176. }
  177. return tmprate;
  178. }
  179. static long sam9x60_frac_pll_round_rate(struct clk_hw *hw, unsigned long rate,
  180. unsigned long *parent_rate)
  181. {
  182. struct sam9x60_pll_core *core = to_sam9x60_pll_core(hw);
  183. return sam9x60_frac_pll_compute_mul_frac(core, rate, *parent_rate, false);
  184. }
  185. static int sam9x60_frac_pll_set_rate(struct clk_hw *hw, unsigned long rate,
  186. unsigned long parent_rate)
  187. {
  188. struct sam9x60_pll_core *core = to_sam9x60_pll_core(hw);
  189. return sam9x60_frac_pll_compute_mul_frac(core, rate, parent_rate, true);
  190. }
  191. static int sam9x60_frac_pll_set_rate_chg(struct clk_hw *hw, unsigned long rate,
  192. unsigned long parent_rate)
  193. {
  194. struct sam9x60_pll_core *core = to_sam9x60_pll_core(hw);
  195. struct sam9x60_frac *frac = to_sam9x60_frac(core);
  196. struct regmap *regmap = core->regmap;
  197. unsigned long irqflags;
  198. unsigned int val, cfrac, cmul;
  199. long ret;
  200. ret = sam9x60_frac_pll_compute_mul_frac(core, rate, parent_rate, true);
  201. if (ret <= 0)
  202. return ret;
  203. spin_lock_irqsave(core->lock, irqflags);
  204. regmap_update_bits(regmap, AT91_PMC_PLL_UPDT, AT91_PMC_PLL_UPDT_ID_MSK,
  205. core->id);
  206. regmap_read(regmap, AT91_PMC_PLL_CTRL1, &val);
  207. cmul = (val & core->layout->mul_mask) >> core->layout->mul_shift;
  208. cfrac = (val & core->layout->frac_mask) >> core->layout->frac_shift;
  209. if (cmul == frac->mul && cfrac == frac->frac)
  210. goto unlock;
  211. regmap_write(regmap, AT91_PMC_PLL_CTRL1,
  212. (frac->mul << core->layout->mul_shift) |
  213. (frac->frac << core->layout->frac_shift));
  214. regmap_update_bits(regmap, AT91_PMC_PLL_UPDT,
  215. AT91_PMC_PLL_UPDT_UPDATE | AT91_PMC_PLL_UPDT_ID_MSK,
  216. AT91_PMC_PLL_UPDT_UPDATE | core->id);
  217. regmap_update_bits(regmap, AT91_PMC_PLL_CTRL0,
  218. AT91_PMC_PLL_CTRL0_ENLOCK | AT91_PMC_PLL_CTRL0_ENPLL,
  219. AT91_PMC_PLL_CTRL0_ENLOCK |
  220. AT91_PMC_PLL_CTRL0_ENPLL);
  221. regmap_update_bits(regmap, AT91_PMC_PLL_UPDT,
  222. AT91_PMC_PLL_UPDT_UPDATE | AT91_PMC_PLL_UPDT_ID_MSK,
  223. AT91_PMC_PLL_UPDT_UPDATE | core->id);
  224. while (!sam9x60_pll_ready(regmap, core->id))
  225. cpu_relax();
  226. unlock:
  227. spin_unlock_irqrestore(core->lock, irqflags);
  228. return ret;
  229. }
  230. static int sam9x60_frac_pll_save_context(struct clk_hw *hw)
  231. {
  232. struct sam9x60_pll_core *core = to_sam9x60_pll_core(hw);
  233. struct sam9x60_frac *frac = to_sam9x60_frac(core);
  234. frac->pms.status = sam9x60_pll_ready(core->regmap, core->id);
  235. return 0;
  236. }
  237. static void sam9x60_frac_pll_restore_context(struct clk_hw *hw)
  238. {
  239. struct sam9x60_pll_core *core = to_sam9x60_pll_core(hw);
  240. struct sam9x60_frac *frac = to_sam9x60_frac(core);
  241. if (frac->pms.status)
  242. sam9x60_frac_pll_set(core);
  243. }
  244. static const struct clk_ops sam9x60_frac_pll_ops = {
  245. .prepare = sam9x60_frac_pll_prepare,
  246. .unprepare = sam9x60_frac_pll_unprepare,
  247. .is_prepared = sam9x60_frac_pll_is_prepared,
  248. .recalc_rate = sam9x60_frac_pll_recalc_rate,
  249. .round_rate = sam9x60_frac_pll_round_rate,
  250. .set_rate = sam9x60_frac_pll_set_rate,
  251. .save_context = sam9x60_frac_pll_save_context,
  252. .restore_context = sam9x60_frac_pll_restore_context,
  253. };
  254. static const struct clk_ops sam9x60_frac_pll_ops_chg = {
  255. .prepare = sam9x60_frac_pll_prepare,
  256. .unprepare = sam9x60_frac_pll_unprepare,
  257. .is_prepared = sam9x60_frac_pll_is_prepared,
  258. .recalc_rate = sam9x60_frac_pll_recalc_rate,
  259. .round_rate = sam9x60_frac_pll_round_rate,
  260. .set_rate = sam9x60_frac_pll_set_rate_chg,
  261. .save_context = sam9x60_frac_pll_save_context,
  262. .restore_context = sam9x60_frac_pll_restore_context,
  263. };
  264. /* This function should be called with spinlock acquired. */
  265. static void sam9x60_div_pll_set_div(struct sam9x60_pll_core *core, u32 div,
  266. bool enable)
  267. {
  268. struct regmap *regmap = core->regmap;
  269. u32 ena_msk = enable ? core->layout->endiv_mask : 0;
  270. u32 ena_val = enable ? (1 << core->layout->endiv_shift) : 0;
  271. regmap_update_bits(regmap, AT91_PMC_PLL_CTRL0,
  272. core->layout->div_mask | ena_msk,
  273. (div << core->layout->div_shift) | ena_val);
  274. regmap_update_bits(regmap, AT91_PMC_PLL_UPDT,
  275. AT91_PMC_PLL_UPDT_UPDATE | AT91_PMC_PLL_UPDT_ID_MSK,
  276. AT91_PMC_PLL_UPDT_UPDATE | core->id);
  277. while (!sam9x60_pll_ready(regmap, core->id))
  278. cpu_relax();
  279. }
  280. static int sam9x60_div_pll_set(struct sam9x60_pll_core *core)
  281. {
  282. struct sam9x60_div *div = to_sam9x60_div(core);
  283. struct regmap *regmap = core->regmap;
  284. unsigned long flags;
  285. unsigned int val, cdiv;
  286. spin_lock_irqsave(core->lock, flags);
  287. regmap_update_bits(regmap, AT91_PMC_PLL_UPDT,
  288. AT91_PMC_PLL_UPDT_ID_MSK, core->id);
  289. regmap_read(regmap, AT91_PMC_PLL_CTRL0, &val);
  290. cdiv = (val & core->layout->div_mask) >> core->layout->div_shift;
  291. /* Stop if enabled an nothing changed. */
  292. if (!!(val & core->layout->endiv_mask) && cdiv == div->div)
  293. goto unlock;
  294. sam9x60_div_pll_set_div(core, div->div, 1);
  295. unlock:
  296. spin_unlock_irqrestore(core->lock, flags);
  297. return 0;
  298. }
  299. static int sam9x60_div_pll_prepare(struct clk_hw *hw)
  300. {
  301. struct sam9x60_pll_core *core = to_sam9x60_pll_core(hw);
  302. return sam9x60_div_pll_set(core);
  303. }
  304. static void sam9x60_div_pll_unprepare(struct clk_hw *hw)
  305. {
  306. struct sam9x60_pll_core *core = to_sam9x60_pll_core(hw);
  307. struct regmap *regmap = core->regmap;
  308. unsigned long flags;
  309. spin_lock_irqsave(core->lock, flags);
  310. regmap_update_bits(regmap, AT91_PMC_PLL_UPDT,
  311. AT91_PMC_PLL_UPDT_ID_MSK, core->id);
  312. regmap_update_bits(regmap, AT91_PMC_PLL_CTRL0,
  313. core->layout->endiv_mask, 0);
  314. regmap_update_bits(regmap, AT91_PMC_PLL_UPDT,
  315. AT91_PMC_PLL_UPDT_UPDATE | AT91_PMC_PLL_UPDT_ID_MSK,
  316. AT91_PMC_PLL_UPDT_UPDATE | core->id);
  317. spin_unlock_irqrestore(core->lock, flags);
  318. }
  319. static int sam9x60_div_pll_is_prepared(struct clk_hw *hw)
  320. {
  321. struct sam9x60_pll_core *core = to_sam9x60_pll_core(hw);
  322. struct regmap *regmap = core->regmap;
  323. unsigned long flags;
  324. unsigned int val;
  325. spin_lock_irqsave(core->lock, flags);
  326. regmap_update_bits(regmap, AT91_PMC_PLL_UPDT,
  327. AT91_PMC_PLL_UPDT_ID_MSK, core->id);
  328. regmap_read(regmap, AT91_PMC_PLL_CTRL0, &val);
  329. spin_unlock_irqrestore(core->lock, flags);
  330. return !!(val & core->layout->endiv_mask);
  331. }
  332. static unsigned long sam9x60_div_pll_recalc_rate(struct clk_hw *hw,
  333. unsigned long parent_rate)
  334. {
  335. struct sam9x60_pll_core *core = to_sam9x60_pll_core(hw);
  336. struct sam9x60_div *div = to_sam9x60_div(core);
  337. return DIV_ROUND_CLOSEST_ULL(parent_rate, (div->div + 1));
  338. }
  339. static unsigned long sam9x60_fixed_div_pll_recalc_rate(struct clk_hw *hw,
  340. unsigned long parent_rate)
  341. {
  342. return parent_rate >> 1;
  343. }
  344. static long sam9x60_div_pll_compute_div(struct sam9x60_pll_core *core,
  345. unsigned long *parent_rate,
  346. unsigned long rate)
  347. {
  348. const struct clk_pll_characteristics *characteristics =
  349. core->characteristics;
  350. struct clk_hw *parent = clk_hw_get_parent(&core->hw);
  351. unsigned long tmp_rate, tmp_parent_rate, tmp_diff;
  352. long best_diff = -1, best_rate = -EINVAL;
  353. u32 divid;
  354. if (!rate)
  355. return 0;
  356. if (rate < characteristics->output[0].min ||
  357. rate > characteristics->output[0].max)
  358. return -ERANGE;
  359. for (divid = 1; divid < core->layout->div_mask; divid++) {
  360. tmp_parent_rate = clk_hw_round_rate(parent, rate * divid);
  361. if (!tmp_parent_rate)
  362. continue;
  363. tmp_rate = DIV_ROUND_CLOSEST_ULL(tmp_parent_rate, divid);
  364. tmp_diff = abs(rate - tmp_rate);
  365. if (best_diff < 0 || best_diff > tmp_diff) {
  366. *parent_rate = tmp_parent_rate;
  367. best_rate = tmp_rate;
  368. best_diff = tmp_diff;
  369. }
  370. if (!best_diff)
  371. break;
  372. }
  373. if (best_rate < characteristics->output[0].min ||
  374. best_rate > characteristics->output[0].max)
  375. return -ERANGE;
  376. return best_rate;
  377. }
  378. static long sam9x60_div_pll_round_rate(struct clk_hw *hw, unsigned long rate,
  379. unsigned long *parent_rate)
  380. {
  381. struct sam9x60_pll_core *core = to_sam9x60_pll_core(hw);
  382. return sam9x60_div_pll_compute_div(core, parent_rate, rate);
  383. }
  384. static int sam9x60_div_pll_set_rate(struct clk_hw *hw, unsigned long rate,
  385. unsigned long parent_rate)
  386. {
  387. struct sam9x60_pll_core *core = to_sam9x60_pll_core(hw);
  388. struct sam9x60_div *div = to_sam9x60_div(core);
  389. div->div = DIV_ROUND_CLOSEST(parent_rate, rate) - 1;
  390. return 0;
  391. }
  392. static int sam9x60_div_pll_set_rate_chg(struct clk_hw *hw, unsigned long rate,
  393. unsigned long parent_rate)
  394. {
  395. struct sam9x60_pll_core *core = to_sam9x60_pll_core(hw);
  396. struct sam9x60_div *div = to_sam9x60_div(core);
  397. struct regmap *regmap = core->regmap;
  398. unsigned long irqflags;
  399. unsigned int val, cdiv;
  400. div->div = DIV_ROUND_CLOSEST(parent_rate, rate) - 1;
  401. spin_lock_irqsave(core->lock, irqflags);
  402. regmap_update_bits(regmap, AT91_PMC_PLL_UPDT, AT91_PMC_PLL_UPDT_ID_MSK,
  403. core->id);
  404. regmap_read(regmap, AT91_PMC_PLL_CTRL0, &val);
  405. cdiv = (val & core->layout->div_mask) >> core->layout->div_shift;
  406. /* Stop if nothing changed. */
  407. if (cdiv == div->div)
  408. goto unlock;
  409. sam9x60_div_pll_set_div(core, div->div, 0);
  410. unlock:
  411. spin_unlock_irqrestore(core->lock, irqflags);
  412. return 0;
  413. }
  414. static int sam9x60_div_pll_save_context(struct clk_hw *hw)
  415. {
  416. struct sam9x60_pll_core *core = to_sam9x60_pll_core(hw);
  417. struct sam9x60_div *div = to_sam9x60_div(core);
  418. div->pms.status = sam9x60_div_pll_is_prepared(hw);
  419. return 0;
  420. }
  421. static void sam9x60_div_pll_restore_context(struct clk_hw *hw)
  422. {
  423. struct sam9x60_pll_core *core = to_sam9x60_pll_core(hw);
  424. struct sam9x60_div *div = to_sam9x60_div(core);
  425. if (div->pms.status)
  426. sam9x60_div_pll_set(core);
  427. }
  428. static int sam9x60_div_pll_notifier_fn(struct notifier_block *notifier,
  429. unsigned long code, void *data)
  430. {
  431. struct sam9x60_div *div = notifier_div;
  432. struct sam9x60_pll_core core = div->core;
  433. struct regmap *regmap = core.regmap;
  434. unsigned long irqflags;
  435. u32 val, cdiv;
  436. int ret = NOTIFY_DONE;
  437. if (code != PRE_RATE_CHANGE)
  438. return ret;
  439. /*
  440. * We switch to safe divider to avoid overclocking of other domains
  441. * feed by us while the frac PLL (our parent) is changed.
  442. */
  443. div->div = div->safe_div;
  444. spin_lock_irqsave(core.lock, irqflags);
  445. regmap_update_bits(regmap, AT91_PMC_PLL_UPDT, AT91_PMC_PLL_UPDT_ID_MSK,
  446. core.id);
  447. regmap_read(regmap, AT91_PMC_PLL_CTRL0, &val);
  448. cdiv = (val & core.layout->div_mask) >> core.layout->div_shift;
  449. /* Stop if nothing changed. */
  450. if (cdiv == div->safe_div)
  451. goto unlock;
  452. sam9x60_div_pll_set_div(&core, div->div, 0);
  453. ret = NOTIFY_OK;
  454. unlock:
  455. spin_unlock_irqrestore(core.lock, irqflags);
  456. return ret;
  457. }
  458. static struct notifier_block sam9x60_div_pll_notifier = {
  459. .notifier_call = sam9x60_div_pll_notifier_fn,
  460. };
  461. static const struct clk_ops sam9x60_div_pll_ops = {
  462. .prepare = sam9x60_div_pll_prepare,
  463. .unprepare = sam9x60_div_pll_unprepare,
  464. .is_prepared = sam9x60_div_pll_is_prepared,
  465. .recalc_rate = sam9x60_div_pll_recalc_rate,
  466. .round_rate = sam9x60_div_pll_round_rate,
  467. .set_rate = sam9x60_div_pll_set_rate,
  468. .save_context = sam9x60_div_pll_save_context,
  469. .restore_context = sam9x60_div_pll_restore_context,
  470. };
  471. static const struct clk_ops sam9x60_div_pll_ops_chg = {
  472. .prepare = sam9x60_div_pll_prepare,
  473. .unprepare = sam9x60_div_pll_unprepare,
  474. .is_prepared = sam9x60_div_pll_is_prepared,
  475. .recalc_rate = sam9x60_div_pll_recalc_rate,
  476. .round_rate = sam9x60_div_pll_round_rate,
  477. .set_rate = sam9x60_div_pll_set_rate_chg,
  478. .save_context = sam9x60_div_pll_save_context,
  479. .restore_context = sam9x60_div_pll_restore_context,
  480. };
  481. static const struct clk_ops sam9x60_fixed_div_pll_ops = {
  482. .prepare = sam9x60_div_pll_prepare,
  483. .unprepare = sam9x60_div_pll_unprepare,
  484. .is_prepared = sam9x60_div_pll_is_prepared,
  485. .recalc_rate = sam9x60_fixed_div_pll_recalc_rate,
  486. .round_rate = sam9x60_div_pll_round_rate,
  487. .save_context = sam9x60_div_pll_save_context,
  488. .restore_context = sam9x60_div_pll_restore_context,
  489. };
  490. struct clk_hw * __init
  491. sam9x60_clk_register_frac_pll(struct regmap *regmap, spinlock_t *lock,
  492. const char *name, const char *parent_name,
  493. struct clk_hw *parent_hw, u8 id,
  494. const struct clk_pll_characteristics *characteristics,
  495. const struct clk_pll_layout *layout, u32 flags)
  496. {
  497. struct sam9x60_frac *frac;
  498. struct clk_hw *hw;
  499. struct clk_init_data init = {};
  500. unsigned long parent_rate, irqflags;
  501. unsigned int val;
  502. int ret;
  503. if (id > PLL_MAX_ID || !lock || !parent_hw)
  504. return ERR_PTR(-EINVAL);
  505. frac = kzalloc(sizeof(*frac), GFP_KERNEL);
  506. if (!frac)
  507. return ERR_PTR(-ENOMEM);
  508. init.name = name;
  509. if (parent_name)
  510. init.parent_names = &parent_name;
  511. else
  512. init.parent_hws = (const struct clk_hw **)&parent_hw;
  513. init.num_parents = 1;
  514. if (flags & CLK_SET_RATE_GATE)
  515. init.ops = &sam9x60_frac_pll_ops;
  516. else
  517. init.ops = &sam9x60_frac_pll_ops_chg;
  518. init.flags = flags;
  519. frac->core.id = id;
  520. frac->core.hw.init = &init;
  521. frac->core.characteristics = characteristics;
  522. frac->core.layout = layout;
  523. frac->core.regmap = regmap;
  524. frac->core.lock = lock;
  525. spin_lock_irqsave(frac->core.lock, irqflags);
  526. if (sam9x60_pll_ready(regmap, id)) {
  527. regmap_update_bits(regmap, AT91_PMC_PLL_UPDT,
  528. AT91_PMC_PLL_UPDT_ID_MSK, id);
  529. regmap_read(regmap, AT91_PMC_PLL_CTRL1, &val);
  530. frac->mul = FIELD_GET(PMC_PLL_CTRL1_MUL_MSK, val);
  531. frac->frac = FIELD_GET(PMC_PLL_CTRL1_FRACR_MSK, val);
  532. } else {
  533. /*
  534. * This means the PLL is not setup by bootloaders. In this
  535. * case we need to set the minimum rate for it. Otherwise
  536. * a clock child of this PLL may be enabled before setting
  537. * its rate leading to enabling this PLL with unsupported
  538. * rate. This will lead to PLL not being locked at all.
  539. */
  540. parent_rate = clk_hw_get_rate(parent_hw);
  541. if (!parent_rate) {
  542. hw = ERR_PTR(-EINVAL);
  543. goto free;
  544. }
  545. ret = sam9x60_frac_pll_compute_mul_frac(&frac->core,
  546. characteristics->core_output[0].min,
  547. parent_rate, true);
  548. if (ret < 0) {
  549. hw = ERR_PTR(ret);
  550. goto free;
  551. }
  552. }
  553. spin_unlock_irqrestore(frac->core.lock, irqflags);
  554. hw = &frac->core.hw;
  555. ret = clk_hw_register(NULL, hw);
  556. if (ret) {
  557. kfree(frac);
  558. hw = ERR_PTR(ret);
  559. }
  560. return hw;
  561. free:
  562. spin_unlock_irqrestore(frac->core.lock, irqflags);
  563. kfree(frac);
  564. return hw;
  565. }
  566. struct clk_hw * __init
  567. sam9x60_clk_register_div_pll(struct regmap *regmap, spinlock_t *lock,
  568. const char *name, const char *parent_name,
  569. struct clk_hw *parent_hw, u8 id,
  570. const struct clk_pll_characteristics *characteristics,
  571. const struct clk_pll_layout *layout, u32 flags,
  572. u32 safe_div)
  573. {
  574. struct sam9x60_div *div;
  575. struct clk_hw *hw;
  576. struct clk_init_data init = {};
  577. unsigned long irqflags;
  578. unsigned int val;
  579. int ret;
  580. /* We only support one changeable PLL. */
  581. if (id > PLL_MAX_ID || !lock || (safe_div && notifier_div))
  582. return ERR_PTR(-EINVAL);
  583. if (safe_div >= PLL_DIV_MAX)
  584. safe_div = PLL_DIV_MAX - 1;
  585. div = kzalloc(sizeof(*div), GFP_KERNEL);
  586. if (!div)
  587. return ERR_PTR(-ENOMEM);
  588. init.name = name;
  589. if (parent_hw)
  590. init.parent_hws = (const struct clk_hw **)&parent_hw;
  591. else
  592. init.parent_names = &parent_name;
  593. init.num_parents = 1;
  594. if (layout->div2)
  595. init.ops = &sam9x60_fixed_div_pll_ops;
  596. else if (flags & CLK_SET_RATE_GATE)
  597. init.ops = &sam9x60_div_pll_ops;
  598. else
  599. init.ops = &sam9x60_div_pll_ops_chg;
  600. init.flags = flags;
  601. div->core.id = id;
  602. div->core.hw.init = &init;
  603. div->core.characteristics = characteristics;
  604. div->core.layout = layout;
  605. div->core.regmap = regmap;
  606. div->core.lock = lock;
  607. div->safe_div = safe_div;
  608. spin_lock_irqsave(div->core.lock, irqflags);
  609. regmap_update_bits(regmap, AT91_PMC_PLL_UPDT,
  610. AT91_PMC_PLL_UPDT_ID_MSK, id);
  611. regmap_read(regmap, AT91_PMC_PLL_CTRL0, &val);
  612. div->div = FIELD_GET(PMC_PLL_CTRL0_DIV_MSK, val);
  613. spin_unlock_irqrestore(div->core.lock, irqflags);
  614. hw = &div->core.hw;
  615. ret = clk_hw_register(NULL, hw);
  616. if (ret) {
  617. kfree(div);
  618. hw = ERR_PTR(ret);
  619. } else if (div->safe_div) {
  620. notifier_div = div;
  621. clk_notifier_register(hw->clk, &sam9x60_div_pll_notifier);
  622. }
  623. return hw;
  624. }