clk-pll.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. /*
  2. * Copyright (C) 2013 Boris BREZILLON <b.brezillon@overkiz.com>
  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 as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. */
  10. #include <linux/clk-provider.h>
  11. #include <linux/clkdev.h>
  12. #include <linux/clk/at91_pmc.h>
  13. #include <linux/of.h>
  14. #include <linux/mfd/syscon.h>
  15. #include <linux/regmap.h>
  16. #include "pmc.h"
  17. #define PLL_STATUS_MASK(id) (1 << (1 + (id)))
  18. #define PLL_REG(id) (AT91_CKGR_PLLAR + ((id) * 4))
  19. #define PLL_DIV_MASK 0xff
  20. #define PLL_DIV_MAX PLL_DIV_MASK
  21. #define PLL_DIV(reg) ((reg) & PLL_DIV_MASK)
  22. #define PLL_MUL(reg, layout) (((reg) >> (layout)->mul_shift) & \
  23. (layout)->mul_mask)
  24. #define PLL_MUL_MIN 2
  25. #define PLL_MUL_MASK(layout) ((layout)->mul_mask)
  26. #define PLL_MUL_MAX(layout) (PLL_MUL_MASK(layout) + 1)
  27. #define PLL_ICPR_SHIFT(id) ((id) * 16)
  28. #define PLL_ICPR_MASK(id) (0xffff << PLL_ICPR_SHIFT(id))
  29. #define PLL_MAX_COUNT 0x3f
  30. #define PLL_COUNT_SHIFT 8
  31. #define PLL_OUT_SHIFT 14
  32. #define PLL_MAX_ID 1
  33. struct clk_pll_characteristics {
  34. struct clk_range input;
  35. int num_output;
  36. struct clk_range *output;
  37. u16 *icpll;
  38. u8 *out;
  39. };
  40. struct clk_pll_layout {
  41. u32 pllr_mask;
  42. u16 mul_mask;
  43. u8 mul_shift;
  44. };
  45. #define to_clk_pll(hw) container_of(hw, struct clk_pll, hw)
  46. struct clk_pll {
  47. struct clk_hw hw;
  48. struct regmap *regmap;
  49. u8 id;
  50. u8 div;
  51. u8 range;
  52. u16 mul;
  53. const struct clk_pll_layout *layout;
  54. const struct clk_pll_characteristics *characteristics;
  55. };
  56. static inline bool clk_pll_ready(struct regmap *regmap, int id)
  57. {
  58. unsigned int status;
  59. regmap_read(regmap, AT91_PMC_SR, &status);
  60. return status & PLL_STATUS_MASK(id) ? 1 : 0;
  61. }
  62. static int clk_pll_prepare(struct clk_hw *hw)
  63. {
  64. struct clk_pll *pll = to_clk_pll(hw);
  65. struct regmap *regmap = pll->regmap;
  66. const struct clk_pll_layout *layout = pll->layout;
  67. const struct clk_pll_characteristics *characteristics =
  68. pll->characteristics;
  69. u8 id = pll->id;
  70. u32 mask = PLL_STATUS_MASK(id);
  71. int offset = PLL_REG(id);
  72. u8 out = 0;
  73. unsigned int pllr;
  74. unsigned int status;
  75. u8 div;
  76. u16 mul;
  77. regmap_read(regmap, offset, &pllr);
  78. div = PLL_DIV(pllr);
  79. mul = PLL_MUL(pllr, layout);
  80. regmap_read(regmap, AT91_PMC_SR, &status);
  81. if ((status & mask) &&
  82. (div == pll->div && mul == pll->mul))
  83. return 0;
  84. if (characteristics->out)
  85. out = characteristics->out[pll->range];
  86. if (characteristics->icpll)
  87. regmap_update_bits(regmap, AT91_PMC_PLLICPR, PLL_ICPR_MASK(id),
  88. characteristics->icpll[pll->range] << PLL_ICPR_SHIFT(id));
  89. regmap_update_bits(regmap, offset, layout->pllr_mask,
  90. pll->div | (PLL_MAX_COUNT << PLL_COUNT_SHIFT) |
  91. (out << PLL_OUT_SHIFT) |
  92. ((pll->mul & layout->mul_mask) << layout->mul_shift));
  93. while (!clk_pll_ready(regmap, pll->id))
  94. cpu_relax();
  95. return 0;
  96. }
  97. static int clk_pll_is_prepared(struct clk_hw *hw)
  98. {
  99. struct clk_pll *pll = to_clk_pll(hw);
  100. return clk_pll_ready(pll->regmap, pll->id);
  101. }
  102. static void clk_pll_unprepare(struct clk_hw *hw)
  103. {
  104. struct clk_pll *pll = to_clk_pll(hw);
  105. unsigned int mask = pll->layout->pllr_mask;
  106. regmap_update_bits(pll->regmap, PLL_REG(pll->id), mask, ~mask);
  107. }
  108. static unsigned long clk_pll_recalc_rate(struct clk_hw *hw,
  109. unsigned long parent_rate)
  110. {
  111. struct clk_pll *pll = to_clk_pll(hw);
  112. if (!pll->div || !pll->mul)
  113. return 0;
  114. return (parent_rate / pll->div) * (pll->mul + 1);
  115. }
  116. static long clk_pll_get_best_div_mul(struct clk_pll *pll, unsigned long rate,
  117. unsigned long parent_rate,
  118. u32 *div, u32 *mul,
  119. u32 *index) {
  120. const struct clk_pll_layout *layout = pll->layout;
  121. const struct clk_pll_characteristics *characteristics =
  122. pll->characteristics;
  123. unsigned long bestremainder = ULONG_MAX;
  124. unsigned long maxdiv, mindiv, tmpdiv;
  125. long bestrate = -ERANGE;
  126. unsigned long bestdiv;
  127. unsigned long bestmul;
  128. int i = 0;
  129. /* Check if parent_rate is a valid input rate */
  130. if (parent_rate < characteristics->input.min)
  131. return -ERANGE;
  132. /*
  133. * Calculate minimum divider based on the minimum multiplier, the
  134. * parent_rate and the requested rate.
  135. * Should always be 2 according to the input and output characteristics
  136. * of the PLL blocks.
  137. */
  138. mindiv = (parent_rate * PLL_MUL_MIN) / rate;
  139. if (!mindiv)
  140. mindiv = 1;
  141. if (parent_rate > characteristics->input.max) {
  142. tmpdiv = DIV_ROUND_UP(parent_rate, characteristics->input.max);
  143. if (tmpdiv > PLL_DIV_MAX)
  144. return -ERANGE;
  145. if (tmpdiv > mindiv)
  146. mindiv = tmpdiv;
  147. }
  148. /*
  149. * Calculate the maximum divider which is limited by PLL register
  150. * layout (limited by the MUL or DIV field size).
  151. */
  152. maxdiv = DIV_ROUND_UP(parent_rate * PLL_MUL_MAX(layout), rate);
  153. if (maxdiv > PLL_DIV_MAX)
  154. maxdiv = PLL_DIV_MAX;
  155. /*
  156. * Iterate over the acceptable divider values to find the best
  157. * divider/multiplier pair (the one that generates the closest
  158. * rate to the requested one).
  159. */
  160. for (tmpdiv = mindiv; tmpdiv <= maxdiv; tmpdiv++) {
  161. unsigned long remainder;
  162. unsigned long tmprate;
  163. unsigned long tmpmul;
  164. /*
  165. * Calculate the multiplier associated with the current
  166. * divider that provide the closest rate to the requested one.
  167. */
  168. tmpmul = DIV_ROUND_CLOSEST(rate, parent_rate / tmpdiv);
  169. tmprate = (parent_rate / tmpdiv) * tmpmul;
  170. if (tmprate > rate)
  171. remainder = tmprate - rate;
  172. else
  173. remainder = rate - tmprate;
  174. /*
  175. * Compare the remainder with the best remainder found until
  176. * now and elect a new best multiplier/divider pair if the
  177. * current remainder is smaller than the best one.
  178. */
  179. if (remainder < bestremainder) {
  180. bestremainder = remainder;
  181. bestdiv = tmpdiv;
  182. bestmul = tmpmul;
  183. bestrate = tmprate;
  184. }
  185. /*
  186. * We've found a perfect match!
  187. * Stop searching now and use this multiplier/divider pair.
  188. */
  189. if (!remainder)
  190. break;
  191. }
  192. /* We haven't found any multiplier/divider pair => return -ERANGE */
  193. if (bestrate < 0)
  194. return bestrate;
  195. /* Check if bestrate is a valid output rate */
  196. for (i = 0; i < characteristics->num_output; i++) {
  197. if (bestrate >= characteristics->output[i].min &&
  198. bestrate <= characteristics->output[i].max)
  199. break;
  200. }
  201. if (i >= characteristics->num_output)
  202. return -ERANGE;
  203. if (div)
  204. *div = bestdiv;
  205. if (mul)
  206. *mul = bestmul - 1;
  207. if (index)
  208. *index = i;
  209. return bestrate;
  210. }
  211. static long clk_pll_round_rate(struct clk_hw *hw, unsigned long rate,
  212. unsigned long *parent_rate)
  213. {
  214. struct clk_pll *pll = to_clk_pll(hw);
  215. return clk_pll_get_best_div_mul(pll, rate, *parent_rate,
  216. NULL, NULL, NULL);
  217. }
  218. static int clk_pll_set_rate(struct clk_hw *hw, unsigned long rate,
  219. unsigned long parent_rate)
  220. {
  221. struct clk_pll *pll = to_clk_pll(hw);
  222. long ret;
  223. u32 div;
  224. u32 mul;
  225. u32 index;
  226. ret = clk_pll_get_best_div_mul(pll, rate, parent_rate,
  227. &div, &mul, &index);
  228. if (ret < 0)
  229. return ret;
  230. pll->range = index;
  231. pll->div = div;
  232. pll->mul = mul;
  233. return 0;
  234. }
  235. static const struct clk_ops pll_ops = {
  236. .prepare = clk_pll_prepare,
  237. .unprepare = clk_pll_unprepare,
  238. .is_prepared = clk_pll_is_prepared,
  239. .recalc_rate = clk_pll_recalc_rate,
  240. .round_rate = clk_pll_round_rate,
  241. .set_rate = clk_pll_set_rate,
  242. };
  243. static struct clk_hw * __init
  244. at91_clk_register_pll(struct regmap *regmap, const char *name,
  245. const char *parent_name, u8 id,
  246. const struct clk_pll_layout *layout,
  247. const struct clk_pll_characteristics *characteristics)
  248. {
  249. struct clk_pll *pll;
  250. struct clk_hw *hw;
  251. struct clk_init_data init;
  252. int offset = PLL_REG(id);
  253. unsigned int pllr;
  254. int ret;
  255. if (id > PLL_MAX_ID)
  256. return ERR_PTR(-EINVAL);
  257. pll = kzalloc(sizeof(*pll), GFP_KERNEL);
  258. if (!pll)
  259. return ERR_PTR(-ENOMEM);
  260. init.name = name;
  261. init.ops = &pll_ops;
  262. init.parent_names = &parent_name;
  263. init.num_parents = 1;
  264. init.flags = CLK_SET_RATE_GATE;
  265. pll->id = id;
  266. pll->hw.init = &init;
  267. pll->layout = layout;
  268. pll->characteristics = characteristics;
  269. pll->regmap = regmap;
  270. regmap_read(regmap, offset, &pllr);
  271. pll->div = PLL_DIV(pllr);
  272. pll->mul = PLL_MUL(pllr, layout);
  273. hw = &pll->hw;
  274. ret = clk_hw_register(NULL, &pll->hw);
  275. if (ret) {
  276. kfree(pll);
  277. hw = ERR_PTR(ret);
  278. }
  279. return hw;
  280. }
  281. static const struct clk_pll_layout at91rm9200_pll_layout = {
  282. .pllr_mask = 0x7FFFFFF,
  283. .mul_shift = 16,
  284. .mul_mask = 0x7FF,
  285. };
  286. static const struct clk_pll_layout at91sam9g45_pll_layout = {
  287. .pllr_mask = 0xFFFFFF,
  288. .mul_shift = 16,
  289. .mul_mask = 0xFF,
  290. };
  291. static const struct clk_pll_layout at91sam9g20_pllb_layout = {
  292. .pllr_mask = 0x3FFFFF,
  293. .mul_shift = 16,
  294. .mul_mask = 0x3F,
  295. };
  296. static const struct clk_pll_layout sama5d3_pll_layout = {
  297. .pllr_mask = 0x1FFFFFF,
  298. .mul_shift = 18,
  299. .mul_mask = 0x7F,
  300. };
  301. static struct clk_pll_characteristics * __init
  302. of_at91_clk_pll_get_characteristics(struct device_node *np)
  303. {
  304. int i;
  305. int offset;
  306. u32 tmp;
  307. int num_output;
  308. u32 num_cells;
  309. struct clk_range input;
  310. struct clk_range *output;
  311. u8 *out = NULL;
  312. u16 *icpll = NULL;
  313. struct clk_pll_characteristics *characteristics;
  314. if (of_at91_get_clk_range(np, "atmel,clk-input-range", &input))
  315. return NULL;
  316. if (of_property_read_u32(np, "#atmel,pll-clk-output-range-cells",
  317. &num_cells))
  318. return NULL;
  319. if (num_cells < 2 || num_cells > 4)
  320. return NULL;
  321. if (!of_get_property(np, "atmel,pll-clk-output-ranges", &tmp))
  322. return NULL;
  323. num_output = tmp / (sizeof(u32) * num_cells);
  324. characteristics = kzalloc(sizeof(*characteristics), GFP_KERNEL);
  325. if (!characteristics)
  326. return NULL;
  327. output = kcalloc(num_output, sizeof(*output), GFP_KERNEL);
  328. if (!output)
  329. goto out_free_characteristics;
  330. if (num_cells > 2) {
  331. out = kcalloc(num_output, sizeof(*out), GFP_KERNEL);
  332. if (!out)
  333. goto out_free_output;
  334. }
  335. if (num_cells > 3) {
  336. icpll = kcalloc(num_output, sizeof(*icpll), GFP_KERNEL);
  337. if (!icpll)
  338. goto out_free_output;
  339. }
  340. for (i = 0; i < num_output; i++) {
  341. offset = i * num_cells;
  342. if (of_property_read_u32_index(np,
  343. "atmel,pll-clk-output-ranges",
  344. offset, &tmp))
  345. goto out_free_output;
  346. output[i].min = tmp;
  347. if (of_property_read_u32_index(np,
  348. "atmel,pll-clk-output-ranges",
  349. offset + 1, &tmp))
  350. goto out_free_output;
  351. output[i].max = tmp;
  352. if (num_cells == 2)
  353. continue;
  354. if (of_property_read_u32_index(np,
  355. "atmel,pll-clk-output-ranges",
  356. offset + 2, &tmp))
  357. goto out_free_output;
  358. out[i] = tmp;
  359. if (num_cells == 3)
  360. continue;
  361. if (of_property_read_u32_index(np,
  362. "atmel,pll-clk-output-ranges",
  363. offset + 3, &tmp))
  364. goto out_free_output;
  365. icpll[i] = tmp;
  366. }
  367. characteristics->input = input;
  368. characteristics->num_output = num_output;
  369. characteristics->output = output;
  370. characteristics->out = out;
  371. characteristics->icpll = icpll;
  372. return characteristics;
  373. out_free_output:
  374. kfree(icpll);
  375. kfree(out);
  376. kfree(output);
  377. out_free_characteristics:
  378. kfree(characteristics);
  379. return NULL;
  380. }
  381. static void __init
  382. of_at91_clk_pll_setup(struct device_node *np,
  383. const struct clk_pll_layout *layout)
  384. {
  385. u32 id;
  386. struct clk_hw *hw;
  387. struct regmap *regmap;
  388. const char *parent_name;
  389. const char *name = np->name;
  390. struct clk_pll_characteristics *characteristics;
  391. if (of_property_read_u32(np, "reg", &id))
  392. return;
  393. parent_name = of_clk_get_parent_name(np, 0);
  394. of_property_read_string(np, "clock-output-names", &name);
  395. regmap = syscon_node_to_regmap(of_get_parent(np));
  396. if (IS_ERR(regmap))
  397. return;
  398. characteristics = of_at91_clk_pll_get_characteristics(np);
  399. if (!characteristics)
  400. return;
  401. hw = at91_clk_register_pll(regmap, name, parent_name, id, layout,
  402. characteristics);
  403. if (IS_ERR(hw))
  404. goto out_free_characteristics;
  405. of_clk_add_hw_provider(np, of_clk_hw_simple_get, hw);
  406. return;
  407. out_free_characteristics:
  408. kfree(characteristics);
  409. }
  410. static void __init of_at91rm9200_clk_pll_setup(struct device_node *np)
  411. {
  412. of_at91_clk_pll_setup(np, &at91rm9200_pll_layout);
  413. }
  414. CLK_OF_DECLARE(at91rm9200_clk_pll, "atmel,at91rm9200-clk-pll",
  415. of_at91rm9200_clk_pll_setup);
  416. static void __init of_at91sam9g45_clk_pll_setup(struct device_node *np)
  417. {
  418. of_at91_clk_pll_setup(np, &at91sam9g45_pll_layout);
  419. }
  420. CLK_OF_DECLARE(at91sam9g45_clk_pll, "atmel,at91sam9g45-clk-pll",
  421. of_at91sam9g45_clk_pll_setup);
  422. static void __init of_at91sam9g20_clk_pllb_setup(struct device_node *np)
  423. {
  424. of_at91_clk_pll_setup(np, &at91sam9g20_pllb_layout);
  425. }
  426. CLK_OF_DECLARE(at91sam9g20_clk_pllb, "atmel,at91sam9g20-clk-pllb",
  427. of_at91sam9g20_clk_pllb_setup);
  428. static void __init of_sama5d3_clk_pll_setup(struct device_node *np)
  429. {
  430. of_at91_clk_pll_setup(np, &sama5d3_pll_layout);
  431. }
  432. CLK_OF_DECLARE(sama5d3_clk_pll, "atmel,sama5d3-clk-pll",
  433. of_sama5d3_clk_pll_setup);