clk-pll.c 13 KB

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