clk-sunxi.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401
  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/of.h>
  19. #include <linux/of_address.h>
  20. #include <linux/reset-controller.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/log2.h>
  23. #include "clk-factors.h"
  24. static DEFINE_SPINLOCK(clk_lock);
  25. /**
  26. * sun6i_a31_ahb1_clk_setup() - Setup function for a31 ahb1 composite clk
  27. */
  28. #define SUN6I_AHB1_MAX_PARENTS 4
  29. #define SUN6I_AHB1_MUX_PARENT_PLL6 3
  30. #define SUN6I_AHB1_MUX_SHIFT 12
  31. /* un-shifted mask is what mux_clk expects */
  32. #define SUN6I_AHB1_MUX_MASK 0x3
  33. #define SUN6I_AHB1_MUX_GET_PARENT(reg) ((reg >> SUN6I_AHB1_MUX_SHIFT) & \
  34. SUN6I_AHB1_MUX_MASK)
  35. #define SUN6I_AHB1_DIV_SHIFT 4
  36. #define SUN6I_AHB1_DIV_MASK (0x3 << SUN6I_AHB1_DIV_SHIFT)
  37. #define SUN6I_AHB1_DIV_GET(reg) ((reg & SUN6I_AHB1_DIV_MASK) >> \
  38. SUN6I_AHB1_DIV_SHIFT)
  39. #define SUN6I_AHB1_DIV_SET(reg, div) ((reg & ~SUN6I_AHB1_DIV_MASK) | \
  40. (div << SUN6I_AHB1_DIV_SHIFT))
  41. #define SUN6I_AHB1_PLL6_DIV_SHIFT 6
  42. #define SUN6I_AHB1_PLL6_DIV_MASK (0x3 << SUN6I_AHB1_PLL6_DIV_SHIFT)
  43. #define SUN6I_AHB1_PLL6_DIV_GET(reg) ((reg & SUN6I_AHB1_PLL6_DIV_MASK) >> \
  44. SUN6I_AHB1_PLL6_DIV_SHIFT)
  45. #define SUN6I_AHB1_PLL6_DIV_SET(reg, div) ((reg & ~SUN6I_AHB1_PLL6_DIV_MASK) | \
  46. (div << SUN6I_AHB1_PLL6_DIV_SHIFT))
  47. struct sun6i_ahb1_clk {
  48. struct clk_hw hw;
  49. void __iomem *reg;
  50. };
  51. #define to_sun6i_ahb1_clk(_hw) container_of(_hw, struct sun6i_ahb1_clk, hw)
  52. static unsigned long sun6i_ahb1_clk_recalc_rate(struct clk_hw *hw,
  53. unsigned long parent_rate)
  54. {
  55. struct sun6i_ahb1_clk *ahb1 = to_sun6i_ahb1_clk(hw);
  56. unsigned long rate;
  57. u32 reg;
  58. /* Fetch the register value */
  59. reg = readl(ahb1->reg);
  60. /* apply pre-divider first if parent is pll6 */
  61. if (SUN6I_AHB1_MUX_GET_PARENT(reg) == SUN6I_AHB1_MUX_PARENT_PLL6)
  62. parent_rate /= SUN6I_AHB1_PLL6_DIV_GET(reg) + 1;
  63. /* clk divider */
  64. rate = parent_rate >> SUN6I_AHB1_DIV_GET(reg);
  65. return rate;
  66. }
  67. static long sun6i_ahb1_clk_round(unsigned long rate, u8 *divp, u8 *pre_divp,
  68. u8 parent, unsigned long parent_rate)
  69. {
  70. u8 div, calcp, calcm = 1;
  71. /*
  72. * clock can only divide, so we will never be able to achieve
  73. * frequencies higher than the parent frequency
  74. */
  75. if (parent_rate && rate > parent_rate)
  76. rate = parent_rate;
  77. div = DIV_ROUND_UP(parent_rate, rate);
  78. /* calculate pre-divider if parent is pll6 */
  79. if (parent == SUN6I_AHB1_MUX_PARENT_PLL6) {
  80. if (div < 4)
  81. calcp = 0;
  82. else if (div / 2 < 4)
  83. calcp = 1;
  84. else if (div / 4 < 4)
  85. calcp = 2;
  86. else
  87. calcp = 3;
  88. calcm = DIV_ROUND_UP(div, 1 << calcp);
  89. } else {
  90. calcp = __roundup_pow_of_two(div);
  91. calcp = calcp > 3 ? 3 : calcp;
  92. }
  93. /* we were asked to pass back divider values */
  94. if (divp) {
  95. *divp = calcp;
  96. *pre_divp = calcm - 1;
  97. }
  98. return (parent_rate / calcm) >> calcp;
  99. }
  100. static long sun6i_ahb1_clk_determine_rate(struct clk_hw *hw, unsigned long rate,
  101. unsigned long min_rate,
  102. unsigned long max_rate,
  103. unsigned long *best_parent_rate,
  104. struct clk_hw **best_parent_clk)
  105. {
  106. struct clk *clk = hw->clk, *parent, *best_parent = NULL;
  107. int i, num_parents;
  108. unsigned long parent_rate, best = 0, child_rate, best_child_rate = 0;
  109. /* find the parent that can help provide the fastest rate <= rate */
  110. num_parents = __clk_get_num_parents(clk);
  111. for (i = 0; i < num_parents; i++) {
  112. parent = clk_get_parent_by_index(clk, i);
  113. if (!parent)
  114. continue;
  115. if (__clk_get_flags(clk) & CLK_SET_RATE_PARENT)
  116. parent_rate = __clk_round_rate(parent, rate);
  117. else
  118. parent_rate = __clk_get_rate(parent);
  119. child_rate = sun6i_ahb1_clk_round(rate, NULL, NULL, i,
  120. parent_rate);
  121. if (child_rate <= rate && child_rate > best_child_rate) {
  122. best_parent = parent;
  123. best = parent_rate;
  124. best_child_rate = child_rate;
  125. }
  126. }
  127. if (best_parent)
  128. *best_parent_clk = __clk_get_hw(best_parent);
  129. *best_parent_rate = best;
  130. return best_child_rate;
  131. }
  132. static int sun6i_ahb1_clk_set_rate(struct clk_hw *hw, unsigned long rate,
  133. unsigned long parent_rate)
  134. {
  135. struct sun6i_ahb1_clk *ahb1 = to_sun6i_ahb1_clk(hw);
  136. unsigned long flags;
  137. u8 div, pre_div, parent;
  138. u32 reg;
  139. spin_lock_irqsave(&clk_lock, flags);
  140. reg = readl(ahb1->reg);
  141. /* need to know which parent is used to apply pre-divider */
  142. parent = SUN6I_AHB1_MUX_GET_PARENT(reg);
  143. sun6i_ahb1_clk_round(rate, &div, &pre_div, parent, parent_rate);
  144. reg = SUN6I_AHB1_DIV_SET(reg, div);
  145. reg = SUN6I_AHB1_PLL6_DIV_SET(reg, pre_div);
  146. writel(reg, ahb1->reg);
  147. spin_unlock_irqrestore(&clk_lock, flags);
  148. return 0;
  149. }
  150. static const struct clk_ops sun6i_ahb1_clk_ops = {
  151. .determine_rate = sun6i_ahb1_clk_determine_rate,
  152. .recalc_rate = sun6i_ahb1_clk_recalc_rate,
  153. .set_rate = sun6i_ahb1_clk_set_rate,
  154. };
  155. static void __init sun6i_ahb1_clk_setup(struct device_node *node)
  156. {
  157. struct clk *clk;
  158. struct sun6i_ahb1_clk *ahb1;
  159. struct clk_mux *mux;
  160. const char *clk_name = node->name;
  161. const char *parents[SUN6I_AHB1_MAX_PARENTS];
  162. void __iomem *reg;
  163. int i = 0;
  164. reg = of_io_request_and_map(node, 0, of_node_full_name(node));
  165. if (IS_ERR(reg))
  166. return;
  167. /* we have a mux, we will have >1 parents */
  168. while (i < SUN6I_AHB1_MAX_PARENTS &&
  169. (parents[i] = of_clk_get_parent_name(node, i)) != NULL)
  170. i++;
  171. of_property_read_string(node, "clock-output-names", &clk_name);
  172. ahb1 = kzalloc(sizeof(struct sun6i_ahb1_clk), GFP_KERNEL);
  173. if (!ahb1)
  174. return;
  175. mux = kzalloc(sizeof(struct clk_mux), GFP_KERNEL);
  176. if (!mux) {
  177. kfree(ahb1);
  178. return;
  179. }
  180. /* set up clock properties */
  181. mux->reg = reg;
  182. mux->shift = SUN6I_AHB1_MUX_SHIFT;
  183. mux->mask = SUN6I_AHB1_MUX_MASK;
  184. mux->lock = &clk_lock;
  185. ahb1->reg = reg;
  186. clk = clk_register_composite(NULL, clk_name, parents, i,
  187. &mux->hw, &clk_mux_ops,
  188. &ahb1->hw, &sun6i_ahb1_clk_ops,
  189. NULL, NULL, 0);
  190. if (!IS_ERR(clk)) {
  191. of_clk_add_provider(node, of_clk_src_simple_get, clk);
  192. clk_register_clkdev(clk, clk_name, NULL);
  193. }
  194. }
  195. CLK_OF_DECLARE(sun6i_a31_ahb1, "allwinner,sun6i-a31-ahb1-clk", sun6i_ahb1_clk_setup);
  196. /* Maximum number of parents our clocks have */
  197. #define SUNXI_MAX_PARENTS 5
  198. /**
  199. * sun4i_get_pll1_factors() - calculates n, k, m, p factors for PLL1
  200. * PLL1 rate is calculated as follows
  201. * rate = (parent_rate * n * (k + 1) >> p) / (m + 1);
  202. * parent_rate is always 24Mhz
  203. */
  204. static void sun4i_get_pll1_factors(u32 *freq, u32 parent_rate,
  205. u8 *n, u8 *k, u8 *m, u8 *p)
  206. {
  207. u8 div;
  208. /* Normalize value to a 6M multiple */
  209. div = *freq / 6000000;
  210. *freq = 6000000 * div;
  211. /* we were called to round the frequency, we can now return */
  212. if (n == NULL)
  213. return;
  214. /* m is always zero for pll1 */
  215. *m = 0;
  216. /* k is 1 only on these cases */
  217. if (*freq >= 768000000 || *freq == 42000000 || *freq == 54000000)
  218. *k = 1;
  219. else
  220. *k = 0;
  221. /* p will be 3 for divs under 10 */
  222. if (div < 10)
  223. *p = 3;
  224. /* p will be 2 for divs between 10 - 20 and odd divs under 32 */
  225. else if (div < 20 || (div < 32 && (div & 1)))
  226. *p = 2;
  227. /* p will be 1 for even divs under 32, divs under 40 and odd pairs
  228. * of divs between 40-62 */
  229. else if (div < 40 || (div < 64 && (div & 2)))
  230. *p = 1;
  231. /* any other entries have p = 0 */
  232. else
  233. *p = 0;
  234. /* calculate a suitable n based on k and p */
  235. div <<= *p;
  236. div /= (*k + 1);
  237. *n = div / 4;
  238. }
  239. /**
  240. * sun6i_a31_get_pll1_factors() - calculates n, k and m factors for PLL1
  241. * PLL1 rate is calculated as follows
  242. * rate = parent_rate * (n + 1) * (k + 1) / (m + 1);
  243. * parent_rate should always be 24MHz
  244. */
  245. static void sun6i_a31_get_pll1_factors(u32 *freq, u32 parent_rate,
  246. u8 *n, u8 *k, u8 *m, u8 *p)
  247. {
  248. /*
  249. * We can operate only on MHz, this will make our life easier
  250. * later.
  251. */
  252. u32 freq_mhz = *freq / 1000000;
  253. u32 parent_freq_mhz = parent_rate / 1000000;
  254. /*
  255. * Round down the frequency to the closest multiple of either
  256. * 6 or 16
  257. */
  258. u32 round_freq_6 = round_down(freq_mhz, 6);
  259. u32 round_freq_16 = round_down(freq_mhz, 16);
  260. if (round_freq_6 > round_freq_16)
  261. freq_mhz = round_freq_6;
  262. else
  263. freq_mhz = round_freq_16;
  264. *freq = freq_mhz * 1000000;
  265. /*
  266. * If the factors pointer are null, we were just called to
  267. * round down the frequency.
  268. * Exit.
  269. */
  270. if (n == NULL)
  271. return;
  272. /* If the frequency is a multiple of 32 MHz, k is always 3 */
  273. if (!(freq_mhz % 32))
  274. *k = 3;
  275. /* If the frequency is a multiple of 9 MHz, k is always 2 */
  276. else if (!(freq_mhz % 9))
  277. *k = 2;
  278. /* If the frequency is a multiple of 8 MHz, k is always 1 */
  279. else if (!(freq_mhz % 8))
  280. *k = 1;
  281. /* Otherwise, we don't use the k factor */
  282. else
  283. *k = 0;
  284. /*
  285. * If the frequency is a multiple of 2 but not a multiple of
  286. * 3, m is 3. This is the first time we use 6 here, yet we
  287. * will use it on several other places.
  288. * We use this number because it's the lowest frequency we can
  289. * generate (with n = 0, k = 0, m = 3), so every other frequency
  290. * somehow relates to this frequency.
  291. */
  292. if ((freq_mhz % 6) == 2 || (freq_mhz % 6) == 4)
  293. *m = 2;
  294. /*
  295. * If the frequency is a multiple of 6MHz, but the factor is
  296. * odd, m will be 3
  297. */
  298. else if ((freq_mhz / 6) & 1)
  299. *m = 3;
  300. /* Otherwise, we end up with m = 1 */
  301. else
  302. *m = 1;
  303. /* Calculate n thanks to the above factors we already got */
  304. *n = freq_mhz * (*m + 1) / ((*k + 1) * parent_freq_mhz) - 1;
  305. /*
  306. * If n end up being outbound, and that we can still decrease
  307. * m, do it.
  308. */
  309. if ((*n + 1) > 31 && (*m + 1) > 1) {
  310. *n = (*n + 1) / 2 - 1;
  311. *m = (*m + 1) / 2 - 1;
  312. }
  313. }
  314. /**
  315. * sun8i_a23_get_pll1_factors() - calculates n, k, m, p factors for PLL1
  316. * PLL1 rate is calculated as follows
  317. * rate = (parent_rate * (n + 1) * (k + 1) >> p) / (m + 1);
  318. * parent_rate is always 24Mhz
  319. */
  320. static void sun8i_a23_get_pll1_factors(u32 *freq, u32 parent_rate,
  321. u8 *n, u8 *k, u8 *m, u8 *p)
  322. {
  323. u8 div;
  324. /* Normalize value to a 6M multiple */
  325. div = *freq / 6000000;
  326. *freq = 6000000 * div;
  327. /* we were called to round the frequency, we can now return */
  328. if (n == NULL)
  329. return;
  330. /* m is always zero for pll1 */
  331. *m = 0;
  332. /* k is 1 only on these cases */
  333. if (*freq >= 768000000 || *freq == 42000000 || *freq == 54000000)
  334. *k = 1;
  335. else
  336. *k = 0;
  337. /* p will be 2 for divs under 20 and odd divs under 32 */
  338. if (div < 20 || (div < 32 && (div & 1)))
  339. *p = 2;
  340. /* p will be 1 for even divs under 32, divs under 40 and odd pairs
  341. * of divs between 40-62 */
  342. else if (div < 40 || (div < 64 && (div & 2)))
  343. *p = 1;
  344. /* any other entries have p = 0 */
  345. else
  346. *p = 0;
  347. /* calculate a suitable n based on k and p */
  348. div <<= *p;
  349. div /= (*k + 1);
  350. *n = div / 4 - 1;
  351. }
  352. /**
  353. * sun4i_get_pll5_factors() - calculates n, k factors for PLL5
  354. * PLL5 rate is calculated as follows
  355. * rate = parent_rate * n * (k + 1)
  356. * parent_rate is always 24Mhz
  357. */
  358. static void sun4i_get_pll5_factors(u32 *freq, u32 parent_rate,
  359. u8 *n, u8 *k, u8 *m, u8 *p)
  360. {
  361. u8 div;
  362. /* Normalize value to a parent_rate multiple (24M) */
  363. div = *freq / parent_rate;
  364. *freq = parent_rate * div;
  365. /* we were called to round the frequency, we can now return */
  366. if (n == NULL)
  367. return;
  368. if (div < 31)
  369. *k = 0;
  370. else if (div / 2 < 31)
  371. *k = 1;
  372. else if (div / 3 < 31)
  373. *k = 2;
  374. else
  375. *k = 3;
  376. *n = DIV_ROUND_UP(div, (*k+1));
  377. }
  378. /**
  379. * sun6i_a31_get_pll6_factors() - calculates n, k factors for A31 PLL6x2
  380. * PLL6x2 rate is calculated as follows
  381. * rate = parent_rate * (n + 1) * (k + 1)
  382. * parent_rate is always 24Mhz
  383. */
  384. static void sun6i_a31_get_pll6_factors(u32 *freq, u32 parent_rate,
  385. u8 *n, u8 *k, u8 *m, u8 *p)
  386. {
  387. u8 div;
  388. /* Normalize value to a parent_rate multiple (24M) */
  389. div = *freq / parent_rate;
  390. *freq = parent_rate * div;
  391. /* we were called to round the frequency, we can now return */
  392. if (n == NULL)
  393. return;
  394. *k = div / 32;
  395. if (*k > 3)
  396. *k = 3;
  397. *n = DIV_ROUND_UP(div, (*k+1)) - 1;
  398. }
  399. /**
  400. * sun5i_a13_get_ahb_factors() - calculates m, p factors for AHB
  401. * AHB rate is calculated as follows
  402. * rate = parent_rate >> p
  403. */
  404. static void sun5i_a13_get_ahb_factors(u32 *freq, u32 parent_rate,
  405. u8 *n, u8 *k, u8 *m, u8 *p)
  406. {
  407. u32 div;
  408. /* divide only */
  409. if (parent_rate < *freq)
  410. *freq = parent_rate;
  411. /*
  412. * user manual says valid speed is 8k ~ 276M, but tests show it
  413. * can work at speeds up to 300M, just after reparenting to pll6
  414. */
  415. if (*freq < 8000)
  416. *freq = 8000;
  417. if (*freq > 300000000)
  418. *freq = 300000000;
  419. div = order_base_2(DIV_ROUND_UP(parent_rate, *freq));
  420. /* p = 0 ~ 3 */
  421. if (div > 3)
  422. div = 3;
  423. *freq = parent_rate >> div;
  424. /* we were called to round the frequency, we can now return */
  425. if (p == NULL)
  426. return;
  427. *p = div;
  428. }
  429. /**
  430. * sun4i_get_apb1_factors() - calculates m, p factors for APB1
  431. * APB1 rate is calculated as follows
  432. * rate = (parent_rate >> p) / (m + 1);
  433. */
  434. static void sun4i_get_apb1_factors(u32 *freq, u32 parent_rate,
  435. u8 *n, u8 *k, u8 *m, u8 *p)
  436. {
  437. u8 calcm, calcp;
  438. if (parent_rate < *freq)
  439. *freq = parent_rate;
  440. parent_rate = DIV_ROUND_UP(parent_rate, *freq);
  441. /* Invalid rate! */
  442. if (parent_rate > 32)
  443. return;
  444. if (parent_rate <= 4)
  445. calcp = 0;
  446. else if (parent_rate <= 8)
  447. calcp = 1;
  448. else if (parent_rate <= 16)
  449. calcp = 2;
  450. else
  451. calcp = 3;
  452. calcm = (parent_rate >> calcp) - 1;
  453. *freq = (parent_rate >> calcp) / (calcm + 1);
  454. /* we were called to round the frequency, we can now return */
  455. if (n == NULL)
  456. return;
  457. *m = calcm;
  458. *p = calcp;
  459. }
  460. /**
  461. * sun7i_a20_get_out_factors() - calculates m, p factors for CLK_OUT_A/B
  462. * CLK_OUT rate is calculated as follows
  463. * rate = (parent_rate >> p) / (m + 1);
  464. */
  465. static void sun7i_a20_get_out_factors(u32 *freq, u32 parent_rate,
  466. u8 *n, u8 *k, u8 *m, u8 *p)
  467. {
  468. u8 div, calcm, calcp;
  469. /* These clocks can only divide, so we will never be able to achieve
  470. * frequencies higher than the parent frequency */
  471. if (*freq > parent_rate)
  472. *freq = parent_rate;
  473. div = DIV_ROUND_UP(parent_rate, *freq);
  474. if (div < 32)
  475. calcp = 0;
  476. else if (div / 2 < 32)
  477. calcp = 1;
  478. else if (div / 4 < 32)
  479. calcp = 2;
  480. else
  481. calcp = 3;
  482. calcm = DIV_ROUND_UP(div, 1 << calcp);
  483. *freq = (parent_rate >> calcp) / calcm;
  484. /* we were called to round the frequency, we can now return */
  485. if (n == NULL)
  486. return;
  487. *m = calcm - 1;
  488. *p = calcp;
  489. }
  490. /**
  491. * sunxi_factors_clk_setup() - Setup function for factor clocks
  492. */
  493. static struct clk_factors_config sun4i_pll1_config = {
  494. .nshift = 8,
  495. .nwidth = 5,
  496. .kshift = 4,
  497. .kwidth = 2,
  498. .mshift = 0,
  499. .mwidth = 2,
  500. .pshift = 16,
  501. .pwidth = 2,
  502. };
  503. static struct clk_factors_config sun6i_a31_pll1_config = {
  504. .nshift = 8,
  505. .nwidth = 5,
  506. .kshift = 4,
  507. .kwidth = 2,
  508. .mshift = 0,
  509. .mwidth = 2,
  510. .n_start = 1,
  511. };
  512. static struct clk_factors_config sun8i_a23_pll1_config = {
  513. .nshift = 8,
  514. .nwidth = 5,
  515. .kshift = 4,
  516. .kwidth = 2,
  517. .mshift = 0,
  518. .mwidth = 2,
  519. .pshift = 16,
  520. .pwidth = 2,
  521. .n_start = 1,
  522. };
  523. static struct clk_factors_config sun4i_pll5_config = {
  524. .nshift = 8,
  525. .nwidth = 5,
  526. .kshift = 4,
  527. .kwidth = 2,
  528. };
  529. static struct clk_factors_config sun6i_a31_pll6_config = {
  530. .nshift = 8,
  531. .nwidth = 5,
  532. .kshift = 4,
  533. .kwidth = 2,
  534. .n_start = 1,
  535. };
  536. static struct clk_factors_config sun5i_a13_ahb_config = {
  537. .pshift = 4,
  538. .pwidth = 2,
  539. };
  540. static struct clk_factors_config sun4i_apb1_config = {
  541. .mshift = 0,
  542. .mwidth = 5,
  543. .pshift = 16,
  544. .pwidth = 2,
  545. };
  546. /* user manual says "n" but it's really "p" */
  547. static struct clk_factors_config sun7i_a20_out_config = {
  548. .mshift = 8,
  549. .mwidth = 5,
  550. .pshift = 20,
  551. .pwidth = 2,
  552. };
  553. static const struct factors_data sun4i_pll1_data __initconst = {
  554. .enable = 31,
  555. .table = &sun4i_pll1_config,
  556. .getter = sun4i_get_pll1_factors,
  557. };
  558. static const struct factors_data sun6i_a31_pll1_data __initconst = {
  559. .enable = 31,
  560. .table = &sun6i_a31_pll1_config,
  561. .getter = sun6i_a31_get_pll1_factors,
  562. };
  563. static const struct factors_data sun8i_a23_pll1_data __initconst = {
  564. .enable = 31,
  565. .table = &sun8i_a23_pll1_config,
  566. .getter = sun8i_a23_get_pll1_factors,
  567. };
  568. static const struct factors_data sun7i_a20_pll4_data __initconst = {
  569. .enable = 31,
  570. .table = &sun4i_pll5_config,
  571. .getter = sun4i_get_pll5_factors,
  572. };
  573. static const struct factors_data sun4i_pll5_data __initconst = {
  574. .enable = 31,
  575. .table = &sun4i_pll5_config,
  576. .getter = sun4i_get_pll5_factors,
  577. .name = "pll5",
  578. };
  579. static const struct factors_data sun4i_pll6_data __initconst = {
  580. .enable = 31,
  581. .table = &sun4i_pll5_config,
  582. .getter = sun4i_get_pll5_factors,
  583. .name = "pll6",
  584. };
  585. static const struct factors_data sun6i_a31_pll6_data __initconst = {
  586. .enable = 31,
  587. .table = &sun6i_a31_pll6_config,
  588. .getter = sun6i_a31_get_pll6_factors,
  589. .name = "pll6x2",
  590. };
  591. static const struct factors_data sun5i_a13_ahb_data __initconst = {
  592. .mux = 6,
  593. .muxmask = BIT(1) | BIT(0),
  594. .table = &sun5i_a13_ahb_config,
  595. .getter = sun5i_a13_get_ahb_factors,
  596. };
  597. static const struct factors_data sun4i_apb1_data __initconst = {
  598. .mux = 24,
  599. .muxmask = BIT(1) | BIT(0),
  600. .table = &sun4i_apb1_config,
  601. .getter = sun4i_get_apb1_factors,
  602. };
  603. static const struct factors_data sun7i_a20_out_data __initconst = {
  604. .enable = 31,
  605. .mux = 24,
  606. .muxmask = BIT(1) | BIT(0),
  607. .table = &sun7i_a20_out_config,
  608. .getter = sun7i_a20_get_out_factors,
  609. };
  610. static struct clk * __init sunxi_factors_clk_setup(struct device_node *node,
  611. const struct factors_data *data)
  612. {
  613. void __iomem *reg;
  614. reg = of_iomap(node, 0);
  615. if (!reg) {
  616. pr_err("Could not get registers for factors-clk: %s\n",
  617. node->name);
  618. return NULL;
  619. }
  620. return sunxi_factors_register(node, data, &clk_lock, reg);
  621. }
  622. /**
  623. * sunxi_mux_clk_setup() - Setup function for muxes
  624. */
  625. #define SUNXI_MUX_GATE_WIDTH 2
  626. struct mux_data {
  627. u8 shift;
  628. };
  629. static const struct mux_data sun4i_cpu_mux_data __initconst = {
  630. .shift = 16,
  631. };
  632. static const struct mux_data sun6i_a31_ahb1_mux_data __initconst = {
  633. .shift = 12,
  634. };
  635. static void __init sunxi_mux_clk_setup(struct device_node *node,
  636. struct mux_data *data)
  637. {
  638. struct clk *clk;
  639. const char *clk_name = node->name;
  640. const char *parents[SUNXI_MAX_PARENTS];
  641. void __iomem *reg;
  642. int i = 0;
  643. reg = of_iomap(node, 0);
  644. while (i < SUNXI_MAX_PARENTS &&
  645. (parents[i] = of_clk_get_parent_name(node, i)) != NULL)
  646. i++;
  647. of_property_read_string(node, "clock-output-names", &clk_name);
  648. clk = clk_register_mux(NULL, clk_name, parents, i,
  649. CLK_SET_RATE_PARENT, reg,
  650. data->shift, SUNXI_MUX_GATE_WIDTH,
  651. 0, &clk_lock);
  652. if (clk) {
  653. of_clk_add_provider(node, of_clk_src_simple_get, clk);
  654. clk_register_clkdev(clk, clk_name, NULL);
  655. }
  656. }
  657. /**
  658. * sunxi_divider_clk_setup() - Setup function for simple divider clocks
  659. */
  660. struct div_data {
  661. u8 shift;
  662. u8 pow;
  663. u8 width;
  664. const struct clk_div_table *table;
  665. };
  666. static const struct div_data sun4i_axi_data __initconst = {
  667. .shift = 0,
  668. .pow = 0,
  669. .width = 2,
  670. };
  671. static const struct clk_div_table sun8i_a23_axi_table[] __initconst = {
  672. { .val = 0, .div = 1 },
  673. { .val = 1, .div = 2 },
  674. { .val = 2, .div = 3 },
  675. { .val = 3, .div = 4 },
  676. { .val = 4, .div = 4 },
  677. { .val = 5, .div = 4 },
  678. { .val = 6, .div = 4 },
  679. { .val = 7, .div = 4 },
  680. { } /* sentinel */
  681. };
  682. static const struct div_data sun8i_a23_axi_data __initconst = {
  683. .width = 3,
  684. .table = sun8i_a23_axi_table,
  685. };
  686. static const struct div_data sun4i_ahb_data __initconst = {
  687. .shift = 4,
  688. .pow = 1,
  689. .width = 2,
  690. };
  691. static const struct clk_div_table sun4i_apb0_table[] __initconst = {
  692. { .val = 0, .div = 2 },
  693. { .val = 1, .div = 2 },
  694. { .val = 2, .div = 4 },
  695. { .val = 3, .div = 8 },
  696. { } /* sentinel */
  697. };
  698. static const struct div_data sun4i_apb0_data __initconst = {
  699. .shift = 8,
  700. .pow = 1,
  701. .width = 2,
  702. .table = sun4i_apb0_table,
  703. };
  704. static void __init sunxi_divider_clk_setup(struct device_node *node,
  705. struct div_data *data)
  706. {
  707. struct clk *clk;
  708. const char *clk_name = node->name;
  709. const char *clk_parent;
  710. void __iomem *reg;
  711. reg = of_iomap(node, 0);
  712. clk_parent = of_clk_get_parent_name(node, 0);
  713. of_property_read_string(node, "clock-output-names", &clk_name);
  714. clk = clk_register_divider_table(NULL, clk_name, clk_parent, 0,
  715. reg, data->shift, data->width,
  716. data->pow ? CLK_DIVIDER_POWER_OF_TWO : 0,
  717. data->table, &clk_lock);
  718. if (clk) {
  719. of_clk_add_provider(node, of_clk_src_simple_get, clk);
  720. clk_register_clkdev(clk, clk_name, NULL);
  721. }
  722. }
  723. /**
  724. * sunxi_gates_clk_setup() - Setup function for leaf gates on clocks
  725. */
  726. #define SUNXI_GATES_MAX_SIZE 64
  727. struct gates_data {
  728. DECLARE_BITMAP(mask, SUNXI_GATES_MAX_SIZE);
  729. };
  730. static const struct gates_data sun4i_axi_gates_data __initconst = {
  731. .mask = {1},
  732. };
  733. static const struct gates_data sun4i_ahb_gates_data __initconst = {
  734. .mask = {0x7F77FFF, 0x14FB3F},
  735. };
  736. static const struct gates_data sun5i_a10s_ahb_gates_data __initconst = {
  737. .mask = {0x147667e7, 0x185915},
  738. };
  739. static const struct gates_data sun5i_a13_ahb_gates_data __initconst = {
  740. .mask = {0x107067e7, 0x185111},
  741. };
  742. static const struct gates_data sun6i_a31_ahb1_gates_data __initconst = {
  743. .mask = {0xEDFE7F62, 0x794F931},
  744. };
  745. static const struct gates_data sun7i_a20_ahb_gates_data __initconst = {
  746. .mask = { 0x12f77fff, 0x16ff3f },
  747. };
  748. static const struct gates_data sun8i_a23_ahb1_gates_data __initconst = {
  749. .mask = {0x25386742, 0x2505111},
  750. };
  751. static const struct gates_data sun9i_a80_ahb0_gates_data __initconst = {
  752. .mask = {0xF5F12B},
  753. };
  754. static const struct gates_data sun9i_a80_ahb1_gates_data __initconst = {
  755. .mask = {0x1E20003},
  756. };
  757. static const struct gates_data sun9i_a80_ahb2_gates_data __initconst = {
  758. .mask = {0x9B7},
  759. };
  760. static const struct gates_data sun4i_apb0_gates_data __initconst = {
  761. .mask = {0x4EF},
  762. };
  763. static const struct gates_data sun5i_a10s_apb0_gates_data __initconst = {
  764. .mask = {0x469},
  765. };
  766. static const struct gates_data sun5i_a13_apb0_gates_data __initconst = {
  767. .mask = {0x61},
  768. };
  769. static const struct gates_data sun7i_a20_apb0_gates_data __initconst = {
  770. .mask = { 0x4ff },
  771. };
  772. static const struct gates_data sun9i_a80_apb0_gates_data __initconst = {
  773. .mask = {0xEB822},
  774. };
  775. static const struct gates_data sun4i_apb1_gates_data __initconst = {
  776. .mask = {0xFF00F7},
  777. };
  778. static const struct gates_data sun5i_a10s_apb1_gates_data __initconst = {
  779. .mask = {0xf0007},
  780. };
  781. static const struct gates_data sun5i_a13_apb1_gates_data __initconst = {
  782. .mask = {0xa0007},
  783. };
  784. static const struct gates_data sun6i_a31_apb1_gates_data __initconst = {
  785. .mask = {0x3031},
  786. };
  787. static const struct gates_data sun8i_a23_apb1_gates_data __initconst = {
  788. .mask = {0x3021},
  789. };
  790. static const struct gates_data sun6i_a31_apb2_gates_data __initconst = {
  791. .mask = {0x3F000F},
  792. };
  793. static const struct gates_data sun7i_a20_apb1_gates_data __initconst = {
  794. .mask = { 0xff80ff },
  795. };
  796. static const struct gates_data sun9i_a80_apb1_gates_data __initconst = {
  797. .mask = {0x3F001F},
  798. };
  799. static const struct gates_data sun8i_a23_apb2_gates_data __initconst = {
  800. .mask = {0x1F0007},
  801. };
  802. static void __init sunxi_gates_clk_setup(struct device_node *node,
  803. struct gates_data *data)
  804. {
  805. struct clk_onecell_data *clk_data;
  806. const char *clk_parent;
  807. const char *clk_name;
  808. void __iomem *reg;
  809. int qty;
  810. int i = 0;
  811. int j = 0;
  812. reg = of_iomap(node, 0);
  813. clk_parent = of_clk_get_parent_name(node, 0);
  814. /* Worst-case size approximation and memory allocation */
  815. qty = find_last_bit(data->mask, SUNXI_GATES_MAX_SIZE);
  816. clk_data = kmalloc(sizeof(struct clk_onecell_data), GFP_KERNEL);
  817. if (!clk_data)
  818. return;
  819. clk_data->clks = kzalloc((qty+1) * sizeof(struct clk *), GFP_KERNEL);
  820. if (!clk_data->clks) {
  821. kfree(clk_data);
  822. return;
  823. }
  824. for_each_set_bit(i, data->mask, SUNXI_GATES_MAX_SIZE) {
  825. of_property_read_string_index(node, "clock-output-names",
  826. j, &clk_name);
  827. clk_data->clks[i] = clk_register_gate(NULL, clk_name,
  828. clk_parent, 0,
  829. reg + 4 * (i/32), i % 32,
  830. 0, &clk_lock);
  831. WARN_ON(IS_ERR(clk_data->clks[i]));
  832. clk_register_clkdev(clk_data->clks[i], clk_name, NULL);
  833. j++;
  834. }
  835. /* Adjust to the real max */
  836. clk_data->clk_num = i;
  837. of_clk_add_provider(node, of_clk_src_onecell_get, clk_data);
  838. }
  839. /**
  840. * sunxi_divs_clk_setup() helper data
  841. */
  842. #define SUNXI_DIVS_MAX_QTY 4
  843. #define SUNXI_DIVISOR_WIDTH 2
  844. struct divs_data {
  845. const struct factors_data *factors; /* data for the factor clock */
  846. int ndivs; /* number of outputs */
  847. /*
  848. * List of outputs. Refer to the diagram for sunxi_divs_clk_setup():
  849. * self or base factor clock refers to the output from the pll
  850. * itself. The remaining refer to fixed or configurable divider
  851. * outputs.
  852. */
  853. struct {
  854. u8 self; /* is it the base factor clock? (only one) */
  855. u8 fixed; /* is it a fixed divisor? if not... */
  856. struct clk_div_table *table; /* is it a table based divisor? */
  857. u8 shift; /* otherwise it's a normal divisor with this shift */
  858. u8 pow; /* is it power-of-two based? */
  859. u8 gate; /* is it independently gateable? */
  860. } div[SUNXI_DIVS_MAX_QTY];
  861. };
  862. static struct clk_div_table pll6_sata_tbl[] = {
  863. { .val = 0, .div = 6, },
  864. { .val = 1, .div = 12, },
  865. { .val = 2, .div = 18, },
  866. { .val = 3, .div = 24, },
  867. { } /* sentinel */
  868. };
  869. static const struct divs_data pll5_divs_data __initconst = {
  870. .factors = &sun4i_pll5_data,
  871. .ndivs = 2,
  872. .div = {
  873. { .shift = 0, .pow = 0, }, /* M, DDR */
  874. { .shift = 16, .pow = 1, }, /* P, other */
  875. /* No output for the base factor clock */
  876. }
  877. };
  878. static const struct divs_data pll6_divs_data __initconst = {
  879. .factors = &sun4i_pll6_data,
  880. .ndivs = 4,
  881. .div = {
  882. { .shift = 0, .table = pll6_sata_tbl, .gate = 14 }, /* M, SATA */
  883. { .fixed = 2 }, /* P, other */
  884. { .self = 1 }, /* base factor clock, 2x */
  885. { .fixed = 4 }, /* pll6 / 4, used as ahb input */
  886. }
  887. };
  888. static const struct divs_data sun6i_a31_pll6_divs_data __initconst = {
  889. .factors = &sun6i_a31_pll6_data,
  890. .ndivs = 2,
  891. .div = {
  892. { .fixed = 2 }, /* normal output */
  893. { .self = 1 }, /* base factor clock, 2x */
  894. }
  895. };
  896. /**
  897. * sunxi_divs_clk_setup() - Setup function for leaf divisors on clocks
  898. *
  899. * These clocks look something like this
  900. * ________________________
  901. * | ___divisor 1---|----> to consumer
  902. * parent >--| pll___/___divisor 2---|----> to consumer
  903. * | \_______________|____> to consumer
  904. * |________________________|
  905. */
  906. static void __init sunxi_divs_clk_setup(struct device_node *node,
  907. struct divs_data *data)
  908. {
  909. struct clk_onecell_data *clk_data;
  910. const char *parent;
  911. const char *clk_name;
  912. struct clk **clks, *pclk;
  913. struct clk_hw *gate_hw, *rate_hw;
  914. const struct clk_ops *rate_ops;
  915. struct clk_gate *gate = NULL;
  916. struct clk_fixed_factor *fix_factor;
  917. struct clk_divider *divider;
  918. void __iomem *reg;
  919. int ndivs = SUNXI_DIVS_MAX_QTY, i = 0;
  920. int flags, clkflags;
  921. /* if number of children known, use it */
  922. if (data->ndivs)
  923. ndivs = data->ndivs;
  924. /* Set up factor clock that we will be dividing */
  925. pclk = sunxi_factors_clk_setup(node, data->factors);
  926. parent = __clk_get_name(pclk);
  927. reg = of_iomap(node, 0);
  928. clk_data = kmalloc(sizeof(struct clk_onecell_data), GFP_KERNEL);
  929. if (!clk_data)
  930. return;
  931. clks = kcalloc(ndivs, sizeof(*clks), GFP_KERNEL);
  932. if (!clks)
  933. goto free_clkdata;
  934. clk_data->clks = clks;
  935. /* It's not a good idea to have automatic reparenting changing
  936. * our RAM clock! */
  937. clkflags = !strcmp("pll5", parent) ? 0 : CLK_SET_RATE_PARENT;
  938. for (i = 0; i < ndivs; i++) {
  939. if (of_property_read_string_index(node, "clock-output-names",
  940. i, &clk_name) != 0)
  941. break;
  942. /* If this is the base factor clock, only update clks */
  943. if (data->div[i].self) {
  944. clk_data->clks[i] = pclk;
  945. continue;
  946. }
  947. gate_hw = NULL;
  948. rate_hw = NULL;
  949. rate_ops = NULL;
  950. /* If this leaf clock can be gated, create a gate */
  951. if (data->div[i].gate) {
  952. gate = kzalloc(sizeof(*gate), GFP_KERNEL);
  953. if (!gate)
  954. goto free_clks;
  955. gate->reg = reg;
  956. gate->bit_idx = data->div[i].gate;
  957. gate->lock = &clk_lock;
  958. gate_hw = &gate->hw;
  959. }
  960. /* Leaves can be fixed or configurable divisors */
  961. if (data->div[i].fixed) {
  962. fix_factor = kzalloc(sizeof(*fix_factor), GFP_KERNEL);
  963. if (!fix_factor)
  964. goto free_gate;
  965. fix_factor->mult = 1;
  966. fix_factor->div = data->div[i].fixed;
  967. rate_hw = &fix_factor->hw;
  968. rate_ops = &clk_fixed_factor_ops;
  969. } else {
  970. divider = kzalloc(sizeof(*divider), GFP_KERNEL);
  971. if (!divider)
  972. goto free_gate;
  973. flags = data->div[i].pow ? CLK_DIVIDER_POWER_OF_TWO : 0;
  974. divider->reg = reg;
  975. divider->shift = data->div[i].shift;
  976. divider->width = SUNXI_DIVISOR_WIDTH;
  977. divider->flags = flags;
  978. divider->lock = &clk_lock;
  979. divider->table = data->div[i].table;
  980. rate_hw = &divider->hw;
  981. rate_ops = &clk_divider_ops;
  982. }
  983. /* Wrap the (potential) gate and the divisor on a composite
  984. * clock to unify them */
  985. clks[i] = clk_register_composite(NULL, clk_name, &parent, 1,
  986. NULL, NULL,
  987. rate_hw, rate_ops,
  988. gate_hw, &clk_gate_ops,
  989. clkflags);
  990. WARN_ON(IS_ERR(clk_data->clks[i]));
  991. clk_register_clkdev(clks[i], clk_name, NULL);
  992. }
  993. /* Adjust to the real max */
  994. clk_data->clk_num = i;
  995. of_clk_add_provider(node, of_clk_src_onecell_get, clk_data);
  996. return;
  997. free_gate:
  998. kfree(gate);
  999. free_clks:
  1000. kfree(clks);
  1001. free_clkdata:
  1002. kfree(clk_data);
  1003. }
  1004. /* Matches for factors clocks */
  1005. static const struct of_device_id clk_factors_match[] __initconst = {
  1006. {.compatible = "allwinner,sun4i-a10-pll1-clk", .data = &sun4i_pll1_data,},
  1007. {.compatible = "allwinner,sun6i-a31-pll1-clk", .data = &sun6i_a31_pll1_data,},
  1008. {.compatible = "allwinner,sun8i-a23-pll1-clk", .data = &sun8i_a23_pll1_data,},
  1009. {.compatible = "allwinner,sun7i-a20-pll4-clk", .data = &sun7i_a20_pll4_data,},
  1010. {.compatible = "allwinner,sun5i-a13-ahb-clk", .data = &sun5i_a13_ahb_data,},
  1011. {.compatible = "allwinner,sun4i-a10-apb1-clk", .data = &sun4i_apb1_data,},
  1012. {.compatible = "allwinner,sun7i-a20-out-clk", .data = &sun7i_a20_out_data,},
  1013. {}
  1014. };
  1015. /* Matches for divider clocks */
  1016. static const struct of_device_id clk_div_match[] __initconst = {
  1017. {.compatible = "allwinner,sun4i-a10-axi-clk", .data = &sun4i_axi_data,},
  1018. {.compatible = "allwinner,sun8i-a23-axi-clk", .data = &sun8i_a23_axi_data,},
  1019. {.compatible = "allwinner,sun4i-a10-ahb-clk", .data = &sun4i_ahb_data,},
  1020. {.compatible = "allwinner,sun4i-a10-apb0-clk", .data = &sun4i_apb0_data,},
  1021. {}
  1022. };
  1023. /* Matches for divided outputs */
  1024. static const struct of_device_id clk_divs_match[] __initconst = {
  1025. {.compatible = "allwinner,sun4i-a10-pll5-clk", .data = &pll5_divs_data,},
  1026. {.compatible = "allwinner,sun4i-a10-pll6-clk", .data = &pll6_divs_data,},
  1027. {.compatible = "allwinner,sun6i-a31-pll6-clk", .data = &sun6i_a31_pll6_divs_data,},
  1028. {}
  1029. };
  1030. /* Matches for mux clocks */
  1031. static const struct of_device_id clk_mux_match[] __initconst = {
  1032. {.compatible = "allwinner,sun4i-a10-cpu-clk", .data = &sun4i_cpu_mux_data,},
  1033. {.compatible = "allwinner,sun6i-a31-ahb1-mux-clk", .data = &sun6i_a31_ahb1_mux_data,},
  1034. {}
  1035. };
  1036. /* Matches for gate clocks */
  1037. static const struct of_device_id clk_gates_match[] __initconst = {
  1038. {.compatible = "allwinner,sun4i-a10-axi-gates-clk", .data = &sun4i_axi_gates_data,},
  1039. {.compatible = "allwinner,sun4i-a10-ahb-gates-clk", .data = &sun4i_ahb_gates_data,},
  1040. {.compatible = "allwinner,sun5i-a10s-ahb-gates-clk", .data = &sun5i_a10s_ahb_gates_data,},
  1041. {.compatible = "allwinner,sun5i-a13-ahb-gates-clk", .data = &sun5i_a13_ahb_gates_data,},
  1042. {.compatible = "allwinner,sun6i-a31-ahb1-gates-clk", .data = &sun6i_a31_ahb1_gates_data,},
  1043. {.compatible = "allwinner,sun7i-a20-ahb-gates-clk", .data = &sun7i_a20_ahb_gates_data,},
  1044. {.compatible = "allwinner,sun8i-a23-ahb1-gates-clk", .data = &sun8i_a23_ahb1_gates_data,},
  1045. {.compatible = "allwinner,sun9i-a80-ahb0-gates-clk", .data = &sun9i_a80_ahb0_gates_data,},
  1046. {.compatible = "allwinner,sun9i-a80-ahb1-gates-clk", .data = &sun9i_a80_ahb1_gates_data,},
  1047. {.compatible = "allwinner,sun9i-a80-ahb2-gates-clk", .data = &sun9i_a80_ahb2_gates_data,},
  1048. {.compatible = "allwinner,sun4i-a10-apb0-gates-clk", .data = &sun4i_apb0_gates_data,},
  1049. {.compatible = "allwinner,sun5i-a10s-apb0-gates-clk", .data = &sun5i_a10s_apb0_gates_data,},
  1050. {.compatible = "allwinner,sun5i-a13-apb0-gates-clk", .data = &sun5i_a13_apb0_gates_data,},
  1051. {.compatible = "allwinner,sun7i-a20-apb0-gates-clk", .data = &sun7i_a20_apb0_gates_data,},
  1052. {.compatible = "allwinner,sun9i-a80-apb0-gates-clk", .data = &sun9i_a80_apb0_gates_data,},
  1053. {.compatible = "allwinner,sun4i-a10-apb1-gates-clk", .data = &sun4i_apb1_gates_data,},
  1054. {.compatible = "allwinner,sun5i-a10s-apb1-gates-clk", .data = &sun5i_a10s_apb1_gates_data,},
  1055. {.compatible = "allwinner,sun5i-a13-apb1-gates-clk", .data = &sun5i_a13_apb1_gates_data,},
  1056. {.compatible = "allwinner,sun6i-a31-apb1-gates-clk", .data = &sun6i_a31_apb1_gates_data,},
  1057. {.compatible = "allwinner,sun7i-a20-apb1-gates-clk", .data = &sun7i_a20_apb1_gates_data,},
  1058. {.compatible = "allwinner,sun8i-a23-apb1-gates-clk", .data = &sun8i_a23_apb1_gates_data,},
  1059. {.compatible = "allwinner,sun9i-a80-apb1-gates-clk", .data = &sun9i_a80_apb1_gates_data,},
  1060. {.compatible = "allwinner,sun6i-a31-apb2-gates-clk", .data = &sun6i_a31_apb2_gates_data,},
  1061. {.compatible = "allwinner,sun8i-a23-apb2-gates-clk", .data = &sun8i_a23_apb2_gates_data,},
  1062. {}
  1063. };
  1064. static void __init of_sunxi_table_clock_setup(const struct of_device_id *clk_match,
  1065. void *function)
  1066. {
  1067. struct device_node *np;
  1068. const struct div_data *data;
  1069. const struct of_device_id *match;
  1070. void (*setup_function)(struct device_node *, const void *) = function;
  1071. for_each_matching_node_and_match(np, clk_match, &match) {
  1072. data = match->data;
  1073. setup_function(np, data);
  1074. }
  1075. }
  1076. static void __init sunxi_init_clocks(const char *clocks[], int nclocks)
  1077. {
  1078. unsigned int i;
  1079. /* Register divided output clocks */
  1080. of_sunxi_table_clock_setup(clk_divs_match, sunxi_divs_clk_setup);
  1081. /* Register factor clocks */
  1082. of_sunxi_table_clock_setup(clk_factors_match, sunxi_factors_clk_setup);
  1083. /* Register divider clocks */
  1084. of_sunxi_table_clock_setup(clk_div_match, sunxi_divider_clk_setup);
  1085. /* Register mux clocks */
  1086. of_sunxi_table_clock_setup(clk_mux_match, sunxi_mux_clk_setup);
  1087. /* Register gate clocks */
  1088. of_sunxi_table_clock_setup(clk_gates_match, sunxi_gates_clk_setup);
  1089. /* Protect the clocks that needs to stay on */
  1090. for (i = 0; i < nclocks; i++) {
  1091. struct clk *clk = clk_get(NULL, clocks[i]);
  1092. if (!IS_ERR(clk))
  1093. clk_prepare_enable(clk);
  1094. }
  1095. }
  1096. static const char *sun4i_a10_critical_clocks[] __initdata = {
  1097. "pll5_ddr",
  1098. "ahb_sdram",
  1099. };
  1100. static void __init sun4i_a10_init_clocks(struct device_node *node)
  1101. {
  1102. sunxi_init_clocks(sun4i_a10_critical_clocks,
  1103. ARRAY_SIZE(sun4i_a10_critical_clocks));
  1104. }
  1105. CLK_OF_DECLARE(sun4i_a10_clk_init, "allwinner,sun4i-a10", sun4i_a10_init_clocks);
  1106. static const char *sun5i_critical_clocks[] __initdata = {
  1107. "cpu",
  1108. "pll5_ddr",
  1109. "ahb_sdram",
  1110. };
  1111. static void __init sun5i_init_clocks(struct device_node *node)
  1112. {
  1113. sunxi_init_clocks(sun5i_critical_clocks,
  1114. ARRAY_SIZE(sun5i_critical_clocks));
  1115. }
  1116. CLK_OF_DECLARE(sun5i_a10s_clk_init, "allwinner,sun5i-a10s", sun5i_init_clocks);
  1117. CLK_OF_DECLARE(sun5i_a13_clk_init, "allwinner,sun5i-a13", sun5i_init_clocks);
  1118. CLK_OF_DECLARE(sun5i_r8_clk_init, "allwinner,sun5i-r8", sun5i_init_clocks);
  1119. CLK_OF_DECLARE(sun7i_a20_clk_init, "allwinner,sun7i-a20", sun5i_init_clocks);
  1120. static const char *sun6i_critical_clocks[] __initdata = {
  1121. "cpu",
  1122. };
  1123. static void __init sun6i_init_clocks(struct device_node *node)
  1124. {
  1125. sunxi_init_clocks(sun6i_critical_clocks,
  1126. ARRAY_SIZE(sun6i_critical_clocks));
  1127. }
  1128. CLK_OF_DECLARE(sun6i_a31_clk_init, "allwinner,sun6i-a31", sun6i_init_clocks);
  1129. CLK_OF_DECLARE(sun6i_a31s_clk_init, "allwinner,sun6i-a31s", sun6i_init_clocks);
  1130. CLK_OF_DECLARE(sun8i_a23_clk_init, "allwinner,sun8i-a23", sun6i_init_clocks);
  1131. static void __init sun9i_init_clocks(struct device_node *node)
  1132. {
  1133. sunxi_init_clocks(NULL, 0);
  1134. }
  1135. CLK_OF_DECLARE(sun9i_a80_clk_init, "allwinner,sun9i-a80", sun9i_init_clocks);