clk-pll.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  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. unsigned int pllr;
  113. u16 mul;
  114. u8 div;
  115. regmap_read(pll->regmap, PLL_REG(pll->id), &pllr);
  116. div = PLL_DIV(pllr);
  117. mul = PLL_MUL(pllr, pll->layout);
  118. if (!div || !mul)
  119. return 0;
  120. return (parent_rate / div) * (mul + 1);
  121. }
  122. static long clk_pll_get_best_div_mul(struct clk_pll *pll, unsigned long rate,
  123. unsigned long parent_rate,
  124. u32 *div, u32 *mul,
  125. u32 *index) {
  126. const struct clk_pll_layout *layout = pll->layout;
  127. const struct clk_pll_characteristics *characteristics =
  128. pll->characteristics;
  129. unsigned long bestremainder = ULONG_MAX;
  130. unsigned long maxdiv, mindiv, tmpdiv;
  131. long bestrate = -ERANGE;
  132. unsigned long bestdiv;
  133. unsigned long bestmul;
  134. int i = 0;
  135. /* Check if parent_rate is a valid input rate */
  136. if (parent_rate < characteristics->input.min)
  137. return -ERANGE;
  138. /*
  139. * Calculate minimum divider based on the minimum multiplier, the
  140. * parent_rate and the requested rate.
  141. * Should always be 2 according to the input and output characteristics
  142. * of the PLL blocks.
  143. */
  144. mindiv = (parent_rate * PLL_MUL_MIN) / rate;
  145. if (!mindiv)
  146. mindiv = 1;
  147. if (parent_rate > characteristics->input.max) {
  148. tmpdiv = DIV_ROUND_UP(parent_rate, characteristics->input.max);
  149. if (tmpdiv > PLL_DIV_MAX)
  150. return -ERANGE;
  151. if (tmpdiv > mindiv)
  152. mindiv = tmpdiv;
  153. }
  154. /*
  155. * Calculate the maximum divider which is limited by PLL register
  156. * layout (limited by the MUL or DIV field size).
  157. */
  158. maxdiv = DIV_ROUND_UP(parent_rate * PLL_MUL_MAX(layout), rate);
  159. if (maxdiv > PLL_DIV_MAX)
  160. maxdiv = PLL_DIV_MAX;
  161. /*
  162. * Iterate over the acceptable divider values to find the best
  163. * divider/multiplier pair (the one that generates the closest
  164. * rate to the requested one).
  165. */
  166. for (tmpdiv = mindiv; tmpdiv <= maxdiv; tmpdiv++) {
  167. unsigned long remainder;
  168. unsigned long tmprate;
  169. unsigned long tmpmul;
  170. /*
  171. * Calculate the multiplier associated with the current
  172. * divider that provide the closest rate to the requested one.
  173. */
  174. tmpmul = DIV_ROUND_CLOSEST(rate, parent_rate / tmpdiv);
  175. tmprate = (parent_rate / tmpdiv) * tmpmul;
  176. if (tmprate > rate)
  177. remainder = tmprate - rate;
  178. else
  179. remainder = rate - tmprate;
  180. /*
  181. * Compare the remainder with the best remainder found until
  182. * now and elect a new best multiplier/divider pair if the
  183. * current remainder is smaller than the best one.
  184. */
  185. if (remainder < bestremainder) {
  186. bestremainder = remainder;
  187. bestdiv = tmpdiv;
  188. bestmul = tmpmul;
  189. bestrate = tmprate;
  190. }
  191. /*
  192. * We've found a perfect match!
  193. * Stop searching now and use this multiplier/divider pair.
  194. */
  195. if (!remainder)
  196. break;
  197. }
  198. /* We haven't found any multiplier/divider pair => return -ERANGE */
  199. if (bestrate < 0)
  200. return bestrate;
  201. /* Check if bestrate is a valid output rate */
  202. for (i = 0; i < characteristics->num_output; i++) {
  203. if (bestrate >= characteristics->output[i].min &&
  204. bestrate <= characteristics->output[i].max)
  205. break;
  206. }
  207. if (i >= characteristics->num_output)
  208. return -ERANGE;
  209. if (div)
  210. *div = bestdiv;
  211. if (mul)
  212. *mul = bestmul - 1;
  213. if (index)
  214. *index = i;
  215. return bestrate;
  216. }
  217. static long clk_pll_round_rate(struct clk_hw *hw, unsigned long rate,
  218. unsigned long *parent_rate)
  219. {
  220. struct clk_pll *pll = to_clk_pll(hw);
  221. return clk_pll_get_best_div_mul(pll, rate, *parent_rate,
  222. NULL, NULL, NULL);
  223. }
  224. static int clk_pll_set_rate(struct clk_hw *hw, unsigned long rate,
  225. unsigned long parent_rate)
  226. {
  227. struct clk_pll *pll = to_clk_pll(hw);
  228. long ret;
  229. u32 div;
  230. u32 mul;
  231. u32 index;
  232. ret = clk_pll_get_best_div_mul(pll, rate, parent_rate,
  233. &div, &mul, &index);
  234. if (ret < 0)
  235. return ret;
  236. pll->range = index;
  237. pll->div = div;
  238. pll->mul = mul;
  239. return 0;
  240. }
  241. static const struct clk_ops pll_ops = {
  242. .prepare = clk_pll_prepare,
  243. .unprepare = clk_pll_unprepare,
  244. .is_prepared = clk_pll_is_prepared,
  245. .recalc_rate = clk_pll_recalc_rate,
  246. .round_rate = clk_pll_round_rate,
  247. .set_rate = clk_pll_set_rate,
  248. };
  249. static struct clk_hw * __init
  250. at91_clk_register_pll(struct regmap *regmap, const char *name,
  251. const char *parent_name, u8 id,
  252. const struct clk_pll_layout *layout,
  253. const struct clk_pll_characteristics *characteristics)
  254. {
  255. struct clk_pll *pll;
  256. struct clk_hw *hw;
  257. struct clk_init_data init;
  258. int offset = PLL_REG(id);
  259. unsigned int pllr;
  260. int ret;
  261. if (id > PLL_MAX_ID)
  262. return ERR_PTR(-EINVAL);
  263. pll = kzalloc(sizeof(*pll), GFP_KERNEL);
  264. if (!pll)
  265. return ERR_PTR(-ENOMEM);
  266. init.name = name;
  267. init.ops = &pll_ops;
  268. init.parent_names = &parent_name;
  269. init.num_parents = 1;
  270. init.flags = CLK_SET_RATE_GATE;
  271. pll->id = id;
  272. pll->hw.init = &init;
  273. pll->layout = layout;
  274. pll->characteristics = characteristics;
  275. pll->regmap = regmap;
  276. regmap_read(regmap, offset, &pllr);
  277. pll->div = PLL_DIV(pllr);
  278. pll->mul = PLL_MUL(pllr, layout);
  279. hw = &pll->hw;
  280. ret = clk_hw_register(NULL, &pll->hw);
  281. if (ret) {
  282. kfree(pll);
  283. hw = ERR_PTR(ret);
  284. }
  285. return hw;
  286. }
  287. static const struct clk_pll_layout at91rm9200_pll_layout = {
  288. .pllr_mask = 0x7FFFFFF,
  289. .mul_shift = 16,
  290. .mul_mask = 0x7FF,
  291. };
  292. static const struct clk_pll_layout at91sam9g45_pll_layout = {
  293. .pllr_mask = 0xFFFFFF,
  294. .mul_shift = 16,
  295. .mul_mask = 0xFF,
  296. };
  297. static const struct clk_pll_layout at91sam9g20_pllb_layout = {
  298. .pllr_mask = 0x3FFFFF,
  299. .mul_shift = 16,
  300. .mul_mask = 0x3F,
  301. };
  302. static const struct clk_pll_layout sama5d3_pll_layout = {
  303. .pllr_mask = 0x1FFFFFF,
  304. .mul_shift = 18,
  305. .mul_mask = 0x7F,
  306. };
  307. static struct clk_pll_characteristics * __init
  308. of_at91_clk_pll_get_characteristics(struct device_node *np)
  309. {
  310. int i;
  311. int offset;
  312. u32 tmp;
  313. int num_output;
  314. u32 num_cells;
  315. struct clk_range input;
  316. struct clk_range *output;
  317. u8 *out = NULL;
  318. u16 *icpll = NULL;
  319. struct clk_pll_characteristics *characteristics;
  320. if (of_at91_get_clk_range(np, "atmel,clk-input-range", &input))
  321. return NULL;
  322. if (of_property_read_u32(np, "#atmel,pll-clk-output-range-cells",
  323. &num_cells))
  324. return NULL;
  325. if (num_cells < 2 || num_cells > 4)
  326. return NULL;
  327. if (!of_get_property(np, "atmel,pll-clk-output-ranges", &tmp))
  328. return NULL;
  329. num_output = tmp / (sizeof(u32) * num_cells);
  330. characteristics = kzalloc(sizeof(*characteristics), GFP_KERNEL);
  331. if (!characteristics)
  332. return NULL;
  333. output = kzalloc(sizeof(*output) * num_output, GFP_KERNEL);
  334. if (!output)
  335. goto out_free_characteristics;
  336. if (num_cells > 2) {
  337. out = kzalloc(sizeof(*out) * num_output, GFP_KERNEL);
  338. if (!out)
  339. goto out_free_output;
  340. }
  341. if (num_cells > 3) {
  342. icpll = kzalloc(sizeof(*icpll) * num_output, GFP_KERNEL);
  343. if (!icpll)
  344. goto out_free_output;
  345. }
  346. for (i = 0; i < num_output; i++) {
  347. offset = i * num_cells;
  348. if (of_property_read_u32_index(np,
  349. "atmel,pll-clk-output-ranges",
  350. offset, &tmp))
  351. goto out_free_output;
  352. output[i].min = tmp;
  353. if (of_property_read_u32_index(np,
  354. "atmel,pll-clk-output-ranges",
  355. offset + 1, &tmp))
  356. goto out_free_output;
  357. output[i].max = tmp;
  358. if (num_cells == 2)
  359. continue;
  360. if (of_property_read_u32_index(np,
  361. "atmel,pll-clk-output-ranges",
  362. offset + 2, &tmp))
  363. goto out_free_output;
  364. out[i] = tmp;
  365. if (num_cells == 3)
  366. continue;
  367. if (of_property_read_u32_index(np,
  368. "atmel,pll-clk-output-ranges",
  369. offset + 3, &tmp))
  370. goto out_free_output;
  371. icpll[i] = tmp;
  372. }
  373. characteristics->input = input;
  374. characteristics->num_output = num_output;
  375. characteristics->output = output;
  376. characteristics->out = out;
  377. characteristics->icpll = icpll;
  378. return characteristics;
  379. out_free_output:
  380. kfree(icpll);
  381. kfree(out);
  382. kfree(output);
  383. out_free_characteristics:
  384. kfree(characteristics);
  385. return NULL;
  386. }
  387. static void __init
  388. of_at91_clk_pll_setup(struct device_node *np,
  389. const struct clk_pll_layout *layout)
  390. {
  391. u32 id;
  392. struct clk_hw *hw;
  393. struct regmap *regmap;
  394. const char *parent_name;
  395. const char *name = np->name;
  396. struct clk_pll_characteristics *characteristics;
  397. if (of_property_read_u32(np, "reg", &id))
  398. return;
  399. parent_name = of_clk_get_parent_name(np, 0);
  400. of_property_read_string(np, "clock-output-names", &name);
  401. regmap = syscon_node_to_regmap(of_get_parent(np));
  402. if (IS_ERR(regmap))
  403. return;
  404. characteristics = of_at91_clk_pll_get_characteristics(np);
  405. if (!characteristics)
  406. return;
  407. hw = at91_clk_register_pll(regmap, name, parent_name, id, layout,
  408. characteristics);
  409. if (IS_ERR(hw))
  410. goto out_free_characteristics;
  411. of_clk_add_hw_provider(np, of_clk_hw_simple_get, hw);
  412. return;
  413. out_free_characteristics:
  414. kfree(characteristics);
  415. }
  416. static void __init of_at91rm9200_clk_pll_setup(struct device_node *np)
  417. {
  418. of_at91_clk_pll_setup(np, &at91rm9200_pll_layout);
  419. }
  420. CLK_OF_DECLARE(at91rm9200_clk_pll, "atmel,at91rm9200-clk-pll",
  421. of_at91rm9200_clk_pll_setup);
  422. static void __init of_at91sam9g45_clk_pll_setup(struct device_node *np)
  423. {
  424. of_at91_clk_pll_setup(np, &at91sam9g45_pll_layout);
  425. }
  426. CLK_OF_DECLARE(at91sam9g45_clk_pll, "atmel,at91sam9g45-clk-pll",
  427. of_at91sam9g45_clk_pll_setup);
  428. static void __init of_at91sam9g20_clk_pllb_setup(struct device_node *np)
  429. {
  430. of_at91_clk_pll_setup(np, &at91sam9g20_pllb_layout);
  431. }
  432. CLK_OF_DECLARE(at91sam9g20_clk_pllb, "atmel,at91sam9g20-clk-pllb",
  433. of_at91sam9g20_clk_pllb_setup);
  434. static void __init of_sama5d3_clk_pll_setup(struct device_node *np)
  435. {
  436. of_at91_clk_pll_setup(np, &sama5d3_pll_layout);
  437. }
  438. CLK_OF_DECLARE(sama5d3_clk_pll, "atmel,sama5d3-clk-pll",
  439. of_sama5d3_clk_pll_setup);