clk-sunxi.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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. * sunxi_osc_clk_setup() - Setup function for gatable oscillator
  25. */
  26. #define SUNXI_OSC24M_GATE 0
  27. static void __init sunxi_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(sunxi_osc, "allwinner,sun4i-osc-clk", sunxi_osc_clk_setup);
  62. /**
  63. * sunxi_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 sunxi_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. * sunxi_get_apb1_factors() - calculates m, p factors for APB1
  105. * APB1 rate is calculated as follows
  106. * rate = (parent_rate >> p) / (m + 1);
  107. */
  108. static void sunxi_get_apb1_factors(u32 *freq, u32 parent_rate,
  109. u8 *n, u8 *k, u8 *m, u8 *p)
  110. {
  111. u8 calcm, calcp;
  112. if (parent_rate < *freq)
  113. *freq = parent_rate;
  114. parent_rate = (parent_rate + (*freq - 1)) / *freq;
  115. /* Invalid rate! */
  116. if (parent_rate > 32)
  117. return;
  118. if (parent_rate <= 4)
  119. calcp = 0;
  120. else if (parent_rate <= 8)
  121. calcp = 1;
  122. else if (parent_rate <= 16)
  123. calcp = 2;
  124. else
  125. calcp = 3;
  126. calcm = (parent_rate >> calcp) - 1;
  127. *freq = (parent_rate >> calcp) / (calcm + 1);
  128. /* we were called to round the frequency, we can now return */
  129. if (n == NULL)
  130. return;
  131. *m = calcm;
  132. *p = calcp;
  133. }
  134. /**
  135. * sunxi_factors_clk_setup() - Setup function for factor clocks
  136. */
  137. struct factors_data {
  138. struct clk_factors_config *table;
  139. void (*getter) (u32 *rate, u32 parent_rate, u8 *n, u8 *k, u8 *m, u8 *p);
  140. };
  141. static struct clk_factors_config pll1_config = {
  142. .nshift = 8,
  143. .nwidth = 5,
  144. .kshift = 4,
  145. .kwidth = 2,
  146. .mshift = 0,
  147. .mwidth = 2,
  148. .pshift = 16,
  149. .pwidth = 2,
  150. };
  151. static struct clk_factors_config apb1_config = {
  152. .mshift = 0,
  153. .mwidth = 5,
  154. .pshift = 16,
  155. .pwidth = 2,
  156. };
  157. static const __initconst struct factors_data pll1_data = {
  158. .table = &pll1_config,
  159. .getter = sunxi_get_pll1_factors,
  160. };
  161. static const __initconst struct factors_data apb1_data = {
  162. .table = &apb1_config,
  163. .getter = sunxi_get_apb1_factors,
  164. };
  165. static void __init sunxi_factors_clk_setup(struct device_node *node,
  166. struct factors_data *data)
  167. {
  168. struct clk *clk;
  169. const char *clk_name = node->name;
  170. const char *parent;
  171. void *reg;
  172. reg = of_iomap(node, 0);
  173. parent = of_clk_get_parent_name(node, 0);
  174. clk = clk_register_factors(NULL, clk_name, parent, 0, reg,
  175. data->table, data->getter, &clk_lock);
  176. if (!IS_ERR(clk)) {
  177. of_clk_add_provider(node, of_clk_src_simple_get, clk);
  178. clk_register_clkdev(clk, clk_name, NULL);
  179. }
  180. }
  181. /**
  182. * sunxi_mux_clk_setup() - Setup function for muxes
  183. */
  184. #define SUNXI_MUX_GATE_WIDTH 2
  185. struct mux_data {
  186. u8 shift;
  187. };
  188. static const __initconst struct mux_data cpu_mux_data = {
  189. .shift = 16,
  190. };
  191. static const __initconst struct mux_data apb1_mux_data = {
  192. .shift = 24,
  193. };
  194. static void __init sunxi_mux_clk_setup(struct device_node *node,
  195. struct mux_data *data)
  196. {
  197. struct clk *clk;
  198. const char *clk_name = node->name;
  199. const char *parents[5];
  200. void *reg;
  201. int i = 0;
  202. reg = of_iomap(node, 0);
  203. while (i < 5 && (parents[i] = of_clk_get_parent_name(node, i)) != NULL)
  204. i++;
  205. clk = clk_register_mux(NULL, clk_name, parents, i,
  206. CLK_SET_RATE_NO_REPARENT, reg,
  207. data->shift, SUNXI_MUX_GATE_WIDTH,
  208. 0, &clk_lock);
  209. if (clk) {
  210. of_clk_add_provider(node, of_clk_src_simple_get, clk);
  211. clk_register_clkdev(clk, clk_name, NULL);
  212. }
  213. }
  214. /**
  215. * sunxi_divider_clk_setup() - Setup function for simple divider clocks
  216. */
  217. #define SUNXI_DIVISOR_WIDTH 2
  218. struct div_data {
  219. u8 shift;
  220. u8 pow;
  221. };
  222. static const __initconst struct div_data axi_data = {
  223. .shift = 0,
  224. .pow = 0,
  225. };
  226. static const __initconst struct div_data ahb_data = {
  227. .shift = 4,
  228. .pow = 1,
  229. };
  230. static const __initconst struct div_data apb0_data = {
  231. .shift = 8,
  232. .pow = 1,
  233. };
  234. static void __init sunxi_divider_clk_setup(struct device_node *node,
  235. struct div_data *data)
  236. {
  237. struct clk *clk;
  238. const char *clk_name = node->name;
  239. const char *clk_parent;
  240. void *reg;
  241. reg = of_iomap(node, 0);
  242. clk_parent = of_clk_get_parent_name(node, 0);
  243. clk = clk_register_divider(NULL, clk_name, clk_parent, 0,
  244. reg, data->shift, SUNXI_DIVISOR_WIDTH,
  245. data->pow ? CLK_DIVIDER_POWER_OF_TWO : 0,
  246. &clk_lock);
  247. if (clk) {
  248. of_clk_add_provider(node, of_clk_src_simple_get, clk);
  249. clk_register_clkdev(clk, clk_name, NULL);
  250. }
  251. }
  252. /**
  253. * sunxi_gates_clk_setup() - Setup function for leaf gates on clocks
  254. */
  255. #define SUNXI_GATES_MAX_SIZE 64
  256. struct gates_data {
  257. DECLARE_BITMAP(mask, SUNXI_GATES_MAX_SIZE);
  258. };
  259. static const __initconst struct gates_data sun4i_axi_gates_data = {
  260. .mask = {1},
  261. };
  262. static const __initconst struct gates_data sun4i_ahb_gates_data = {
  263. .mask = {0x7F77FFF, 0x14FB3F},
  264. };
  265. static const __initconst struct gates_data sun5i_a13_ahb_gates_data = {
  266. .mask = {0x107067e7, 0x185111},
  267. };
  268. static const __initconst struct gates_data sun4i_apb0_gates_data = {
  269. .mask = {0x4EF},
  270. };
  271. static const __initconst struct gates_data sun5i_a13_apb0_gates_data = {
  272. .mask = {0x61},
  273. };
  274. static const __initconst struct gates_data sun4i_apb1_gates_data = {
  275. .mask = {0xFF00F7},
  276. };
  277. static const __initconst struct gates_data sun5i_a13_apb1_gates_data = {
  278. .mask = {0xa0007},
  279. };
  280. static void __init sunxi_gates_clk_setup(struct device_node *node,
  281. struct gates_data *data)
  282. {
  283. struct clk_onecell_data *clk_data;
  284. const char *clk_parent;
  285. const char *clk_name;
  286. void *reg;
  287. int qty;
  288. int i = 0;
  289. int j = 0;
  290. int ignore;
  291. reg = of_iomap(node, 0);
  292. clk_parent = of_clk_get_parent_name(node, 0);
  293. /* Worst-case size approximation and memory allocation */
  294. qty = find_last_bit(data->mask, SUNXI_GATES_MAX_SIZE);
  295. clk_data = kmalloc(sizeof(struct clk_onecell_data), GFP_KERNEL);
  296. if (!clk_data)
  297. return;
  298. clk_data->clks = kzalloc((qty+1) * sizeof(struct clk *), GFP_KERNEL);
  299. if (!clk_data->clks) {
  300. kfree(clk_data);
  301. return;
  302. }
  303. for_each_set_bit(i, data->mask, SUNXI_GATES_MAX_SIZE) {
  304. of_property_read_string_index(node, "clock-output-names",
  305. j, &clk_name);
  306. /* No driver claims this clock, but it should remain gated */
  307. ignore = !strcmp("ahb_sdram", clk_name) ? CLK_IGNORE_UNUSED : 0;
  308. clk_data->clks[i] = clk_register_gate(NULL, clk_name,
  309. clk_parent, ignore,
  310. reg + 4 * (i/32), i % 32,
  311. 0, &clk_lock);
  312. WARN_ON(IS_ERR(clk_data->clks[i]));
  313. j++;
  314. }
  315. /* Adjust to the real max */
  316. clk_data->clk_num = i;
  317. of_clk_add_provider(node, of_clk_src_onecell_get, clk_data);
  318. }
  319. /* Matches for factors clocks */
  320. static const __initconst struct of_device_id clk_factors_match[] = {
  321. {.compatible = "allwinner,sun4i-pll1-clk", .data = &pll1_data,},
  322. {.compatible = "allwinner,sun4i-apb1-clk", .data = &apb1_data,},
  323. {}
  324. };
  325. /* Matches for divider clocks */
  326. static const __initconst struct of_device_id clk_div_match[] = {
  327. {.compatible = "allwinner,sun4i-axi-clk", .data = &axi_data,},
  328. {.compatible = "allwinner,sun4i-ahb-clk", .data = &ahb_data,},
  329. {.compatible = "allwinner,sun4i-apb0-clk", .data = &apb0_data,},
  330. {}
  331. };
  332. /* Matches for mux clocks */
  333. static const __initconst struct of_device_id clk_mux_match[] = {
  334. {.compatible = "allwinner,sun4i-cpu-clk", .data = &cpu_mux_data,},
  335. {.compatible = "allwinner,sun4i-apb1-mux-clk", .data = &apb1_mux_data,},
  336. {}
  337. };
  338. /* Matches for gate clocks */
  339. static const __initconst struct of_device_id clk_gates_match[] = {
  340. {.compatible = "allwinner,sun4i-axi-gates-clk", .data = &sun4i_axi_gates_data,},
  341. {.compatible = "allwinner,sun4i-ahb-gates-clk", .data = &sun4i_ahb_gates_data,},
  342. {.compatible = "allwinner,sun5i-a13-ahb-gates-clk", .data = &sun5i_a13_ahb_gates_data,},
  343. {.compatible = "allwinner,sun4i-apb0-gates-clk", .data = &sun4i_apb0_gates_data,},
  344. {.compatible = "allwinner,sun5i-a13-apb0-gates-clk", .data = &sun5i_a13_apb0_gates_data,},
  345. {.compatible = "allwinner,sun4i-apb1-gates-clk", .data = &sun4i_apb1_gates_data,},
  346. {.compatible = "allwinner,sun5i-a13-apb1-gates-clk", .data = &sun5i_a13_apb1_gates_data,},
  347. {}
  348. };
  349. static void __init of_sunxi_table_clock_setup(const struct of_device_id *clk_match,
  350. void *function)
  351. {
  352. struct device_node *np;
  353. const struct div_data *data;
  354. const struct of_device_id *match;
  355. void (*setup_function)(struct device_node *, const void *) = function;
  356. for_each_matching_node(np, clk_match) {
  357. match = of_match_node(clk_match, np);
  358. data = match->data;
  359. setup_function(np, data);
  360. }
  361. }
  362. void __init sunxi_init_clocks(void)
  363. {
  364. /* Register all the simple and basic clocks on DT */
  365. of_clk_init(NULL);
  366. /* Register factor clocks */
  367. of_sunxi_table_clock_setup(clk_factors_match, sunxi_factors_clk_setup);
  368. /* Register divider clocks */
  369. of_sunxi_table_clock_setup(clk_div_match, sunxi_divider_clk_setup);
  370. /* Register mux clocks */
  371. of_sunxi_table_clock_setup(clk_mux_match, sunxi_mux_clk_setup);
  372. /* Register gate clocks */
  373. of_sunxi_table_clock_setup(clk_gates_match, sunxi_gates_clk_setup);
  374. }