clk-sunxi.c 10 KB

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