clk-pll.c 12 KB

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