clk-pll.c 12 KB

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