clk-pll.c 13 KB

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