clk-sam9x60-pll.c 20 KB

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