clk-sunxi.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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 (clk) {
  57. of_clk_add_provider(node, of_clk_src_simple_get, clk);
  58. clk_register_clkdev(clk, clk_name, NULL);
  59. }
  60. }
  61. /**
  62. * sunxi_get_pll1_factors() - calculates n, k, m, p factors for PLL1
  63. * PLL1 rate is calculated as follows
  64. * rate = (parent_rate * n * (k + 1) >> p) / (m + 1);
  65. * parent_rate is always 24Mhz
  66. */
  67. static void sunxi_get_pll1_factors(u32 *freq, u32 parent_rate,
  68. u8 *n, u8 *k, u8 *m, u8 *p)
  69. {
  70. u8 div;
  71. /* Normalize value to a 6M multiple */
  72. div = *freq / 6000000;
  73. *freq = 6000000 * div;
  74. /* we were called to round the frequency, we can now return */
  75. if (n == NULL)
  76. return;
  77. /* m is always zero for pll1 */
  78. *m = 0;
  79. /* k is 1 only on these cases */
  80. if (*freq >= 768000000 || *freq == 42000000 || *freq == 54000000)
  81. *k = 1;
  82. else
  83. *k = 0;
  84. /* p will be 3 for divs under 10 */
  85. if (div < 10)
  86. *p = 3;
  87. /* p will be 2 for divs between 10 - 20 and odd divs under 32 */
  88. else if (div < 20 || (div < 32 && (div & 1)))
  89. *p = 2;
  90. /* p will be 1 for even divs under 32, divs under 40 and odd pairs
  91. * of divs between 40-62 */
  92. else if (div < 40 || (div < 64 && (div & 2)))
  93. *p = 1;
  94. /* any other entries have p = 0 */
  95. else
  96. *p = 0;
  97. /* calculate a suitable n based on k and p */
  98. div <<= *p;
  99. div /= (*k + 1);
  100. *n = div / 4;
  101. }
  102. /**
  103. * sunxi_get_apb1_factors() - calculates m, p factors for APB1
  104. * APB1 rate is calculated as follows
  105. * rate = (parent_rate >> p) / (m + 1);
  106. */
  107. static void sunxi_get_apb1_factors(u32 *freq, u32 parent_rate,
  108. u8 *n, u8 *k, u8 *m, u8 *p)
  109. {
  110. u8 calcm, calcp;
  111. if (parent_rate < *freq)
  112. *freq = parent_rate;
  113. parent_rate = (parent_rate + (*freq - 1)) / *freq;
  114. /* Invalid rate! */
  115. if (parent_rate > 32)
  116. return;
  117. if (parent_rate <= 4)
  118. calcp = 0;
  119. else if (parent_rate <= 8)
  120. calcp = 1;
  121. else if (parent_rate <= 16)
  122. calcp = 2;
  123. else
  124. calcp = 3;
  125. calcm = (parent_rate >> calcp) - 1;
  126. *freq = (parent_rate >> calcp) / (calcm + 1);
  127. /* we were called to round the frequency, we can now return */
  128. if (n == NULL)
  129. return;
  130. *m = calcm;
  131. *p = calcp;
  132. }
  133. /**
  134. * sunxi_factors_clk_setup() - Setup function for factor clocks
  135. */
  136. struct factors_data {
  137. struct clk_factors_config *table;
  138. void (*getter) (u32 *rate, u32 parent_rate, u8 *n, u8 *k, u8 *m, u8 *p);
  139. };
  140. static struct clk_factors_config pll1_config = {
  141. .nshift = 8,
  142. .nwidth = 5,
  143. .kshift = 4,
  144. .kwidth = 2,
  145. .mshift = 0,
  146. .mwidth = 2,
  147. .pshift = 16,
  148. .pwidth = 2,
  149. };
  150. static struct clk_factors_config apb1_config = {
  151. .mshift = 0,
  152. .mwidth = 5,
  153. .pshift = 16,
  154. .pwidth = 2,
  155. };
  156. static const __initconst struct factors_data pll1_data = {
  157. .table = &pll1_config,
  158. .getter = sunxi_get_pll1_factors,
  159. };
  160. static const __initconst struct factors_data apb1_data = {
  161. .table = &apb1_config,
  162. .getter = sunxi_get_apb1_factors,
  163. };
  164. static void __init sunxi_factors_clk_setup(struct device_node *node,
  165. struct factors_data *data)
  166. {
  167. struct clk *clk;
  168. const char *clk_name = node->name;
  169. const char *parent;
  170. void *reg;
  171. reg = of_iomap(node, 0);
  172. parent = of_clk_get_parent_name(node, 0);
  173. clk = clk_register_factors(NULL, clk_name, parent, 0, reg,
  174. data->table, data->getter, &clk_lock);
  175. if (clk) {
  176. of_clk_add_provider(node, of_clk_src_simple_get, clk);
  177. clk_register_clkdev(clk, clk_name, NULL);
  178. }
  179. }
  180. /**
  181. * sunxi_mux_clk_setup() - Setup function for muxes
  182. */
  183. #define SUNXI_MUX_GATE_WIDTH 2
  184. struct mux_data {
  185. u8 shift;
  186. };
  187. static const __initconst struct mux_data cpu_data = {
  188. .shift = 16,
  189. };
  190. static const __initconst struct mux_data apb1_mux_data = {
  191. .shift = 24,
  192. };
  193. static void __init sunxi_mux_clk_setup(struct device_node *node,
  194. struct mux_data *data)
  195. {
  196. struct clk *clk;
  197. const char *clk_name = node->name;
  198. const char *parents[5];
  199. void *reg;
  200. int i = 0;
  201. reg = of_iomap(node, 0);
  202. while (i < 5 && (parents[i] = of_clk_get_parent_name(node, i)) != NULL)
  203. i++;
  204. clk = clk_register_mux(NULL, clk_name, parents, i, 0, reg,
  205. data->shift, SUNXI_MUX_GATE_WIDTH,
  206. 0, &clk_lock);
  207. if (clk) {
  208. of_clk_add_provider(node, of_clk_src_simple_get, clk);
  209. clk_register_clkdev(clk, clk_name, NULL);
  210. }
  211. }
  212. /**
  213. * sunxi_divider_clk_setup() - Setup function for simple divider clocks
  214. */
  215. #define SUNXI_DIVISOR_WIDTH 2
  216. struct div_data {
  217. u8 shift;
  218. u8 pow;
  219. };
  220. static const __initconst struct div_data axi_data = {
  221. .shift = 0,
  222. .pow = 0,
  223. };
  224. static const __initconst struct div_data ahb_data = {
  225. .shift = 4,
  226. .pow = 1,
  227. };
  228. static const __initconst struct div_data apb0_data = {
  229. .shift = 8,
  230. .pow = 1,
  231. };
  232. static void __init sunxi_divider_clk_setup(struct device_node *node,
  233. struct div_data *data)
  234. {
  235. struct clk *clk;
  236. const char *clk_name = node->name;
  237. const char *clk_parent;
  238. void *reg;
  239. reg = of_iomap(node, 0);
  240. clk_parent = of_clk_get_parent_name(node, 0);
  241. clk = clk_register_divider(NULL, clk_name, clk_parent, 0,
  242. reg, data->shift, SUNXI_DIVISOR_WIDTH,
  243. data->pow ? CLK_DIVIDER_POWER_OF_TWO : 0,
  244. &clk_lock);
  245. if (clk) {
  246. of_clk_add_provider(node, of_clk_src_simple_get, clk);
  247. clk_register_clkdev(clk, clk_name, NULL);
  248. }
  249. }
  250. /**
  251. * sunxi_gates_clk_setup() - Setup function for leaf gates on clocks
  252. */
  253. #define SUNXI_GATES_MAX_SIZE 64
  254. struct gates_data {
  255. DECLARE_BITMAP(mask, SUNXI_GATES_MAX_SIZE);
  256. };
  257. static const __initconst struct gates_data axi_gates_data = {
  258. .mask = {1},
  259. };
  260. static const __initconst struct gates_data ahb_gates_data = {
  261. .mask = {0x7F77FFF, 0x14FB3F},
  262. };
  263. static const __initconst struct gates_data apb0_gates_data = {
  264. .mask = {0x4EF},
  265. };
  266. static const __initconst struct gates_data apb1_gates_data = {
  267. .mask = {0xFF00F7},
  268. };
  269. static void __init sunxi_gates_clk_setup(struct device_node *node,
  270. struct gates_data *data)
  271. {
  272. struct clk_onecell_data *clk_data;
  273. const char *clk_parent;
  274. const char *clk_name;
  275. void *reg;
  276. int qty;
  277. int i = 0;
  278. int j = 0;
  279. int ignore;
  280. reg = of_iomap(node, 0);
  281. clk_parent = of_clk_get_parent_name(node, 0);
  282. /* Worst-case size approximation and memory allocation */
  283. qty = find_last_bit(data->mask, SUNXI_GATES_MAX_SIZE);
  284. clk_data = kmalloc(sizeof(struct clk_onecell_data), GFP_KERNEL);
  285. if (!clk_data)
  286. return;
  287. clk_data->clks = kzalloc((qty+1) * sizeof(struct clk *), GFP_KERNEL);
  288. if (!clk_data->clks) {
  289. kfree(clk_data);
  290. return;
  291. }
  292. for_each_set_bit(i, data->mask, SUNXI_GATES_MAX_SIZE) {
  293. of_property_read_string_index(node, "clock-output-names",
  294. j, &clk_name);
  295. /* No driver claims this clock, but it should remain gated */
  296. ignore = !strcmp("ahb_sdram", clk_name) ? CLK_IGNORE_UNUSED : 0;
  297. clk_data->clks[i] = clk_register_gate(NULL, clk_name,
  298. clk_parent, ignore,
  299. reg + 4 * (i/32), i % 32,
  300. 0, &clk_lock);
  301. WARN_ON(IS_ERR(clk_data->clks[i]));
  302. j++;
  303. }
  304. /* Adjust to the real max */
  305. clk_data->clk_num = i;
  306. of_clk_add_provider(node, of_clk_src_onecell_get, clk_data);
  307. }
  308. /* Matches for of_clk_init */
  309. static const __initconst struct of_device_id clk_match[] = {
  310. {.compatible = "allwinner,sun4i-osc-clk", .data = sunxi_osc_clk_setup,},
  311. {}
  312. };
  313. /* Matches for factors clocks */
  314. static const __initconst struct of_device_id clk_factors_match[] = {
  315. {.compatible = "allwinner,sun4i-pll1-clk", .data = &pll1_data,},
  316. {.compatible = "allwinner,sun4i-apb1-clk", .data = &apb1_data,},
  317. {}
  318. };
  319. /* Matches for divider clocks */
  320. static const __initconst struct of_device_id clk_div_match[] = {
  321. {.compatible = "allwinner,sun4i-axi-clk", .data = &axi_data,},
  322. {.compatible = "allwinner,sun4i-ahb-clk", .data = &ahb_data,},
  323. {.compatible = "allwinner,sun4i-apb0-clk", .data = &apb0_data,},
  324. {}
  325. };
  326. /* Matches for mux clocks */
  327. static const __initconst struct of_device_id clk_mux_match[] = {
  328. {.compatible = "allwinner,sun4i-cpu-clk", .data = &cpu_data,},
  329. {.compatible = "allwinner,sun4i-apb1-mux-clk", .data = &apb1_mux_data,},
  330. {}
  331. };
  332. /* Matches for gate clocks */
  333. static const __initconst struct of_device_id clk_gates_match[] = {
  334. {.compatible = "allwinner,sun4i-axi-gates-clk", .data = &axi_gates_data,},
  335. {.compatible = "allwinner,sun4i-ahb-gates-clk", .data = &ahb_gates_data,},
  336. {.compatible = "allwinner,sun4i-apb0-gates-clk", .data = &apb0_gates_data,},
  337. {.compatible = "allwinner,sun4i-apb1-gates-clk", .data = &apb1_gates_data,},
  338. {}
  339. };
  340. static void __init of_sunxi_table_clock_setup(const struct of_device_id *clk_match,
  341. void *function)
  342. {
  343. struct device_node *np;
  344. const struct div_data *data;
  345. const struct of_device_id *match;
  346. void (*setup_function)(struct device_node *, const void *) = function;
  347. for_each_matching_node(np, clk_match) {
  348. match = of_match_node(clk_match, np);
  349. data = match->data;
  350. setup_function(np, data);
  351. }
  352. }
  353. void __init sunxi_init_clocks(void)
  354. {
  355. /* Register all the simple sunxi clocks on DT */
  356. of_clk_init(clk_match);
  357. /* Register factor clocks */
  358. of_sunxi_table_clock_setup(clk_factors_match, sunxi_factors_clk_setup);
  359. /* Register divider clocks */
  360. of_sunxi_table_clock_setup(clk_div_match, sunxi_divider_clk_setup);
  361. /* Register mux clocks */
  362. of_sunxi_table_clock_setup(clk_mux_match, sunxi_mux_clk_setup);
  363. /* Register gate clocks */
  364. of_sunxi_table_clock_setup(clk_gates_match, sunxi_gates_clk_setup);
  365. }