clk-sunxi.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  1. /*
  2. * Copyright 2013 Emilio López
  3. *
  4. * Emilio López <emilio@elopez.com.ar>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #include <linux/clk-provider.h>
  17. #include <linux/clkdev.h>
  18. #include <linux/clk/sunxi.h>
  19. #include <linux/of.h>
  20. #include <linux/of_address.h>
  21. #include "clk-factors.h"
  22. static DEFINE_SPINLOCK(clk_lock);
  23. /**
  24. * sun4i_osc_clk_setup() - Setup function for gatable oscillator
  25. */
  26. #define SUNXI_OSC24M_GATE 0
  27. static void __init sun4i_osc_clk_setup(struct device_node *node)
  28. {
  29. struct clk *clk;
  30. struct clk_fixed_rate *fixed;
  31. struct clk_gate *gate;
  32. const char *clk_name = node->name;
  33. u32 rate;
  34. /* allocate fixed-rate and gate clock structs */
  35. fixed = kzalloc(sizeof(struct clk_fixed_rate), GFP_KERNEL);
  36. if (!fixed)
  37. return;
  38. gate = kzalloc(sizeof(struct clk_gate), GFP_KERNEL);
  39. if (!gate) {
  40. kfree(fixed);
  41. return;
  42. }
  43. if (of_property_read_u32(node, "clock-frequency", &rate))
  44. return;
  45. /* set up gate and fixed rate properties */
  46. gate->reg = of_iomap(node, 0);
  47. gate->bit_idx = SUNXI_OSC24M_GATE;
  48. gate->lock = &clk_lock;
  49. fixed->fixed_rate = rate;
  50. clk = clk_register_composite(NULL, clk_name,
  51. NULL, 0,
  52. NULL, NULL,
  53. &fixed->hw, &clk_fixed_rate_ops,
  54. &gate->hw, &clk_gate_ops,
  55. CLK_IS_ROOT);
  56. if (!IS_ERR(clk)) {
  57. of_clk_add_provider(node, of_clk_src_simple_get, clk);
  58. clk_register_clkdev(clk, clk_name, NULL);
  59. }
  60. }
  61. CLK_OF_DECLARE(sun4i_osc, "allwinner,sun4i-osc-clk", sun4i_osc_clk_setup);
  62. /**
  63. * sun4i_get_pll1_factors() - calculates n, k, m, p factors for PLL1
  64. * PLL1 rate is calculated as follows
  65. * rate = (parent_rate * n * (k + 1) >> p) / (m + 1);
  66. * parent_rate is always 24Mhz
  67. */
  68. static void sun4i_get_pll1_factors(u32 *freq, u32 parent_rate,
  69. u8 *n, u8 *k, u8 *m, u8 *p)
  70. {
  71. u8 div;
  72. /* Normalize value to a 6M multiple */
  73. div = *freq / 6000000;
  74. *freq = 6000000 * div;
  75. /* we were called to round the frequency, we can now return */
  76. if (n == NULL)
  77. return;
  78. /* m is always zero for pll1 */
  79. *m = 0;
  80. /* k is 1 only on these cases */
  81. if (*freq >= 768000000 || *freq == 42000000 || *freq == 54000000)
  82. *k = 1;
  83. else
  84. *k = 0;
  85. /* p will be 3 for divs under 10 */
  86. if (div < 10)
  87. *p = 3;
  88. /* p will be 2 for divs between 10 - 20 and odd divs under 32 */
  89. else if (div < 20 || (div < 32 && (div & 1)))
  90. *p = 2;
  91. /* p will be 1 for even divs under 32, divs under 40 and odd pairs
  92. * of divs between 40-62 */
  93. else if (div < 40 || (div < 64 && (div & 2)))
  94. *p = 1;
  95. /* any other entries have p = 0 */
  96. else
  97. *p = 0;
  98. /* calculate a suitable n based on k and p */
  99. div <<= *p;
  100. div /= (*k + 1);
  101. *n = div / 4;
  102. }
  103. /**
  104. * sun6i_a31_get_pll1_factors() - calculates n, k and m factors for PLL1
  105. * PLL1 rate is calculated as follows
  106. * rate = parent_rate * (n + 1) * (k + 1) / (m + 1);
  107. * parent_rate should always be 24MHz
  108. */
  109. static void sun6i_a31_get_pll1_factors(u32 *freq, u32 parent_rate,
  110. u8 *n, u8 *k, u8 *m, u8 *p)
  111. {
  112. /*
  113. * We can operate only on MHz, this will make our life easier
  114. * later.
  115. */
  116. u32 freq_mhz = *freq / 1000000;
  117. u32 parent_freq_mhz = parent_rate / 1000000;
  118. /*
  119. * Round down the frequency to the closest multiple of either
  120. * 6 or 16
  121. */
  122. u32 round_freq_6 = round_down(freq_mhz, 6);
  123. u32 round_freq_16 = round_down(freq_mhz, 16);
  124. if (round_freq_6 > round_freq_16)
  125. freq_mhz = round_freq_6;
  126. else
  127. freq_mhz = round_freq_16;
  128. *freq = freq_mhz * 1000000;
  129. /*
  130. * If the factors pointer are null, we were just called to
  131. * round down the frequency.
  132. * Exit.
  133. */
  134. if (n == NULL)
  135. return;
  136. /* If the frequency is a multiple of 32 MHz, k is always 3 */
  137. if (!(freq_mhz % 32))
  138. *k = 3;
  139. /* If the frequency is a multiple of 9 MHz, k is always 2 */
  140. else if (!(freq_mhz % 9))
  141. *k = 2;
  142. /* If the frequency is a multiple of 8 MHz, k is always 1 */
  143. else if (!(freq_mhz % 8))
  144. *k = 1;
  145. /* Otherwise, we don't use the k factor */
  146. else
  147. *k = 0;
  148. /*
  149. * If the frequency is a multiple of 2 but not a multiple of
  150. * 3, m is 3. This is the first time we use 6 here, yet we
  151. * will use it on several other places.
  152. * We use this number because it's the lowest frequency we can
  153. * generate (with n = 0, k = 0, m = 3), so every other frequency
  154. * somehow relates to this frequency.
  155. */
  156. if ((freq_mhz % 6) == 2 || (freq_mhz % 6) == 4)
  157. *m = 2;
  158. /*
  159. * If the frequency is a multiple of 6MHz, but the factor is
  160. * odd, m will be 3
  161. */
  162. else if ((freq_mhz / 6) & 1)
  163. *m = 3;
  164. /* Otherwise, we end up with m = 1 */
  165. else
  166. *m = 1;
  167. /* Calculate n thanks to the above factors we already got */
  168. *n = freq_mhz * (*m + 1) / ((*k + 1) * parent_freq_mhz) - 1;
  169. /*
  170. * If n end up being outbound, and that we can still decrease
  171. * m, do it.
  172. */
  173. if ((*n + 1) > 31 && (*m + 1) > 1) {
  174. *n = (*n + 1) / 2 - 1;
  175. *m = (*m + 1) / 2 - 1;
  176. }
  177. }
  178. /**
  179. * sun4i_get_apb1_factors() - calculates m, p factors for APB1
  180. * APB1 rate is calculated as follows
  181. * rate = (parent_rate >> p) / (m + 1);
  182. */
  183. static void sun4i_get_apb1_factors(u32 *freq, u32 parent_rate,
  184. u8 *n, u8 *k, u8 *m, u8 *p)
  185. {
  186. u8 calcm, calcp;
  187. if (parent_rate < *freq)
  188. *freq = parent_rate;
  189. parent_rate = (parent_rate + (*freq - 1)) / *freq;
  190. /* Invalid rate! */
  191. if (parent_rate > 32)
  192. return;
  193. if (parent_rate <= 4)
  194. calcp = 0;
  195. else if (parent_rate <= 8)
  196. calcp = 1;
  197. else if (parent_rate <= 16)
  198. calcp = 2;
  199. else
  200. calcp = 3;
  201. calcm = (parent_rate >> calcp) - 1;
  202. *freq = (parent_rate >> calcp) / (calcm + 1);
  203. /* we were called to round the frequency, we can now return */
  204. if (n == NULL)
  205. return;
  206. *m = calcm;
  207. *p = calcp;
  208. }
  209. /**
  210. * sunxi_factors_clk_setup() - Setup function for factor clocks
  211. */
  212. struct factors_data {
  213. struct clk_factors_config *table;
  214. void (*getter) (u32 *rate, u32 parent_rate, u8 *n, u8 *k, u8 *m, u8 *p);
  215. };
  216. static struct clk_factors_config sun4i_pll1_config = {
  217. .nshift = 8,
  218. .nwidth = 5,
  219. .kshift = 4,
  220. .kwidth = 2,
  221. .mshift = 0,
  222. .mwidth = 2,
  223. .pshift = 16,
  224. .pwidth = 2,
  225. };
  226. static struct clk_factors_config sun6i_a31_pll1_config = {
  227. .nshift = 8,
  228. .nwidth = 5,
  229. .kshift = 4,
  230. .kwidth = 2,
  231. .mshift = 0,
  232. .mwidth = 2,
  233. };
  234. static struct clk_factors_config sun4i_apb1_config = {
  235. .mshift = 0,
  236. .mwidth = 5,
  237. .pshift = 16,
  238. .pwidth = 2,
  239. };
  240. static const struct factors_data sun4i_pll1_data __initconst = {
  241. .table = &sun4i_pll1_config,
  242. .getter = sun4i_get_pll1_factors,
  243. };
  244. static const struct factors_data sun6i_a31_pll1_data __initconst = {
  245. .table = &sun6i_a31_pll1_config,
  246. .getter = sun6i_a31_get_pll1_factors,
  247. };
  248. static const struct factors_data sun4i_apb1_data __initconst = {
  249. .table = &sun4i_apb1_config,
  250. .getter = sun4i_get_apb1_factors,
  251. };
  252. static void __init sunxi_factors_clk_setup(struct device_node *node,
  253. struct factors_data *data)
  254. {
  255. struct clk *clk;
  256. const char *clk_name = node->name;
  257. const char *parent;
  258. void *reg;
  259. reg = of_iomap(node, 0);
  260. parent = of_clk_get_parent_name(node, 0);
  261. clk = clk_register_factors(NULL, clk_name, parent, 0, reg,
  262. data->table, data->getter, &clk_lock);
  263. if (!IS_ERR(clk)) {
  264. of_clk_add_provider(node, of_clk_src_simple_get, clk);
  265. clk_register_clkdev(clk, clk_name, NULL);
  266. }
  267. }
  268. /**
  269. * sunxi_mux_clk_setup() - Setup function for muxes
  270. */
  271. #define SUNXI_MUX_GATE_WIDTH 2
  272. struct mux_data {
  273. u8 shift;
  274. };
  275. static const struct mux_data sun4i_cpu_mux_data __initconst = {
  276. .shift = 16,
  277. };
  278. static const struct mux_data sun6i_a31_ahb1_mux_data __initconst = {
  279. .shift = 12,
  280. };
  281. static const struct mux_data sun4i_apb1_mux_data __initconst = {
  282. .shift = 24,
  283. };
  284. static void __init sunxi_mux_clk_setup(struct device_node *node,
  285. struct mux_data *data)
  286. {
  287. struct clk *clk;
  288. const char *clk_name = node->name;
  289. const char *parents[5];
  290. void *reg;
  291. int i = 0;
  292. reg = of_iomap(node, 0);
  293. while (i < 5 && (parents[i] = of_clk_get_parent_name(node, i)) != NULL)
  294. i++;
  295. clk = clk_register_mux(NULL, clk_name, parents, i,
  296. CLK_SET_RATE_NO_REPARENT, reg,
  297. data->shift, SUNXI_MUX_GATE_WIDTH,
  298. 0, &clk_lock);
  299. if (clk) {
  300. of_clk_add_provider(node, of_clk_src_simple_get, clk);
  301. clk_register_clkdev(clk, clk_name, NULL);
  302. }
  303. }
  304. /**
  305. * sunxi_divider_clk_setup() - Setup function for simple divider clocks
  306. */
  307. struct div_data {
  308. u8 shift;
  309. u8 pow;
  310. u8 width;
  311. };
  312. static const struct div_data sun4i_axi_data __initconst = {
  313. .shift = 0,
  314. .pow = 0,
  315. .width = 2,
  316. };
  317. static const struct div_data sun4i_ahb_data __initconst = {
  318. .shift = 4,
  319. .pow = 1,
  320. .width = 2,
  321. };
  322. static const struct div_data sun4i_apb0_data __initconst = {
  323. .shift = 8,
  324. .pow = 1,
  325. .width = 2,
  326. };
  327. static const struct div_data sun6i_a31_apb2_div_data __initconst = {
  328. .shift = 0,
  329. .pow = 0,
  330. .width = 4,
  331. };
  332. static void __init sunxi_divider_clk_setup(struct device_node *node,
  333. struct div_data *data)
  334. {
  335. struct clk *clk;
  336. const char *clk_name = node->name;
  337. const char *clk_parent;
  338. void *reg;
  339. reg = of_iomap(node, 0);
  340. clk_parent = of_clk_get_parent_name(node, 0);
  341. clk = clk_register_divider(NULL, clk_name, clk_parent, 0,
  342. reg, data->shift, data->width,
  343. data->pow ? CLK_DIVIDER_POWER_OF_TWO : 0,
  344. &clk_lock);
  345. if (clk) {
  346. of_clk_add_provider(node, of_clk_src_simple_get, clk);
  347. clk_register_clkdev(clk, clk_name, NULL);
  348. }
  349. }
  350. /**
  351. * sunxi_gates_clk_setup() - Setup function for leaf gates on clocks
  352. */
  353. #define SUNXI_GATES_MAX_SIZE 64
  354. struct gates_data {
  355. DECLARE_BITMAP(mask, SUNXI_GATES_MAX_SIZE);
  356. };
  357. static const struct gates_data sun4i_axi_gates_data __initconst = {
  358. .mask = {1},
  359. };
  360. static const struct gates_data sun4i_ahb_gates_data __initconst = {
  361. .mask = {0x7F77FFF, 0x14FB3F},
  362. };
  363. static const struct gates_data sun5i_a10s_ahb_gates_data __initconst = {
  364. .mask = {0x147667e7, 0x185915},
  365. };
  366. static const struct gates_data sun5i_a13_ahb_gates_data __initconst = {
  367. .mask = {0x107067e7, 0x185111},
  368. };
  369. static const struct gates_data sun6i_a31_ahb1_gates_data __initconst = {
  370. .mask = {0xEDFE7F62, 0x794F931},
  371. };
  372. static const struct gates_data sun7i_a20_ahb_gates_data __initconst = {
  373. .mask = { 0x12f77fff, 0x16ff3f },
  374. };
  375. static const struct gates_data sun4i_apb0_gates_data __initconst = {
  376. .mask = {0x4EF},
  377. };
  378. static const struct gates_data sun5i_a10s_apb0_gates_data __initconst = {
  379. .mask = {0x469},
  380. };
  381. static const struct gates_data sun5i_a13_apb0_gates_data __initconst = {
  382. .mask = {0x61},
  383. };
  384. static const struct gates_data sun7i_a20_apb0_gates_data __initconst = {
  385. .mask = { 0x4ff },
  386. };
  387. static const struct gates_data sun4i_apb1_gates_data __initconst = {
  388. .mask = {0xFF00F7},
  389. };
  390. static const struct gates_data sun5i_a10s_apb1_gates_data __initconst = {
  391. .mask = {0xf0007},
  392. };
  393. static const struct gates_data sun5i_a13_apb1_gates_data __initconst = {
  394. .mask = {0xa0007},
  395. };
  396. static const struct gates_data sun6i_a31_apb1_gates_data __initconst = {
  397. .mask = {0x3031},
  398. };
  399. static const struct gates_data sun6i_a31_apb2_gates_data __initconst = {
  400. .mask = {0x3F000F},
  401. };
  402. static const struct gates_data sun7i_a20_apb1_gates_data __initconst = {
  403. .mask = { 0xff80ff },
  404. };
  405. static void __init sunxi_gates_clk_setup(struct device_node *node,
  406. struct gates_data *data)
  407. {
  408. struct clk_onecell_data *clk_data;
  409. const char *clk_parent;
  410. const char *clk_name;
  411. void *reg;
  412. int qty;
  413. int i = 0;
  414. int j = 0;
  415. int ignore;
  416. reg = of_iomap(node, 0);
  417. clk_parent = of_clk_get_parent_name(node, 0);
  418. /* Worst-case size approximation and memory allocation */
  419. qty = find_last_bit(data->mask, SUNXI_GATES_MAX_SIZE);
  420. clk_data = kmalloc(sizeof(struct clk_onecell_data), GFP_KERNEL);
  421. if (!clk_data)
  422. return;
  423. clk_data->clks = kzalloc((qty+1) * sizeof(struct clk *), GFP_KERNEL);
  424. if (!clk_data->clks) {
  425. kfree(clk_data);
  426. return;
  427. }
  428. for_each_set_bit(i, data->mask, SUNXI_GATES_MAX_SIZE) {
  429. of_property_read_string_index(node, "clock-output-names",
  430. j, &clk_name);
  431. /* No driver claims this clock, but it should remain gated */
  432. ignore = !strcmp("ahb_sdram", clk_name) ? CLK_IGNORE_UNUSED : 0;
  433. clk_data->clks[i] = clk_register_gate(NULL, clk_name,
  434. clk_parent, ignore,
  435. reg + 4 * (i/32), i % 32,
  436. 0, &clk_lock);
  437. WARN_ON(IS_ERR(clk_data->clks[i]));
  438. j++;
  439. }
  440. /* Adjust to the real max */
  441. clk_data->clk_num = i;
  442. of_clk_add_provider(node, of_clk_src_onecell_get, clk_data);
  443. }
  444. /* Matches for factors clocks */
  445. static const struct of_device_id clk_factors_match[] __initconst = {
  446. {.compatible = "allwinner,sun4i-pll1-clk", .data = &sun4i_pll1_data,},
  447. {.compatible = "allwinner,sun6i-a31-pll1-clk", .data = &sun6i_a31_pll1_data,},
  448. {.compatible = "allwinner,sun4i-apb1-clk", .data = &sun4i_apb1_data,},
  449. {}
  450. };
  451. /* Matches for divider clocks */
  452. static const struct of_device_id clk_div_match[] __initconst = {
  453. {.compatible = "allwinner,sun4i-axi-clk", .data = &sun4i_axi_data,},
  454. {.compatible = "allwinner,sun4i-ahb-clk", .data = &sun4i_ahb_data,},
  455. {.compatible = "allwinner,sun4i-apb0-clk", .data = &sun4i_apb0_data,},
  456. {.compatible = "allwinner,sun6i-a31-apb2-div-clk", .data = &sun6i_a31_apb2_div_data,},
  457. {}
  458. };
  459. /* Matches for mux clocks */
  460. static const struct of_device_id clk_mux_match[] __initconst = {
  461. {.compatible = "allwinner,sun4i-cpu-clk", .data = &sun4i_cpu_mux_data,},
  462. {.compatible = "allwinner,sun4i-apb1-mux-clk", .data = &sun4i_apb1_mux_data,},
  463. {.compatible = "allwinner,sun6i-a31-ahb1-mux-clk", .data = &sun6i_a31_ahb1_mux_data,},
  464. {}
  465. };
  466. /* Matches for gate clocks */
  467. static const struct of_device_id clk_gates_match[] __initconst = {
  468. {.compatible = "allwinner,sun4i-axi-gates-clk", .data = &sun4i_axi_gates_data,},
  469. {.compatible = "allwinner,sun4i-ahb-gates-clk", .data = &sun4i_ahb_gates_data,},
  470. {.compatible = "allwinner,sun5i-a10s-ahb-gates-clk", .data = &sun5i_a10s_ahb_gates_data,},
  471. {.compatible = "allwinner,sun5i-a13-ahb-gates-clk", .data = &sun5i_a13_ahb_gates_data,},
  472. {.compatible = "allwinner,sun6i-a31-ahb1-gates-clk", .data = &sun6i_a31_ahb1_gates_data,},
  473. {.compatible = "allwinner,sun7i-a20-ahb-gates-clk", .data = &sun7i_a20_ahb_gates_data,},
  474. {.compatible = "allwinner,sun4i-apb0-gates-clk", .data = &sun4i_apb0_gates_data,},
  475. {.compatible = "allwinner,sun5i-a10s-apb0-gates-clk", .data = &sun5i_a10s_apb0_gates_data,},
  476. {.compatible = "allwinner,sun5i-a13-apb0-gates-clk", .data = &sun5i_a13_apb0_gates_data,},
  477. {.compatible = "allwinner,sun7i-a20-apb0-gates-clk", .data = &sun7i_a20_apb0_gates_data,},
  478. {.compatible = "allwinner,sun4i-apb1-gates-clk", .data = &sun4i_apb1_gates_data,},
  479. {.compatible = "allwinner,sun5i-a10s-apb1-gates-clk", .data = &sun5i_a10s_apb1_gates_data,},
  480. {.compatible = "allwinner,sun5i-a13-apb1-gates-clk", .data = &sun5i_a13_apb1_gates_data,},
  481. {.compatible = "allwinner,sun6i-a31-apb1-gates-clk", .data = &sun6i_a31_apb1_gates_data,},
  482. {.compatible = "allwinner,sun7i-a20-apb1-gates-clk", .data = &sun7i_a20_apb1_gates_data,},
  483. {.compatible = "allwinner,sun6i-a31-apb2-gates-clk", .data = &sun6i_a31_apb2_gates_data,},
  484. {}
  485. };
  486. static void __init of_sunxi_table_clock_setup(const struct of_device_id *clk_match,
  487. void *function)
  488. {
  489. struct device_node *np;
  490. const struct div_data *data;
  491. const struct of_device_id *match;
  492. void (*setup_function)(struct device_node *, const void *) = function;
  493. for_each_matching_node(np, clk_match) {
  494. match = of_match_node(clk_match, np);
  495. data = match->data;
  496. setup_function(np, data);
  497. }
  498. }
  499. void __init sunxi_init_clocks(void)
  500. {
  501. /* Register all the simple and basic clocks on DT */
  502. of_clk_init(NULL);
  503. /* Register factor clocks */
  504. of_sunxi_table_clock_setup(clk_factors_match, sunxi_factors_clk_setup);
  505. /* Register divider clocks */
  506. of_sunxi_table_clock_setup(clk_div_match, sunxi_divider_clk_setup);
  507. /* Register mux clocks */
  508. of_sunxi_table_clock_setup(clk_mux_match, sunxi_mux_clk_setup);
  509. /* Register gate clocks */
  510. of_sunxi_table_clock_setup(clk_gates_match, sunxi_gates_clk_setup);
  511. }