clk-sunxi.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090
  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 "clk-factors.h"
  21. static DEFINE_SPINLOCK(clk_lock);
  22. /* Maximum number of parents our clocks have */
  23. #define SUNXI_MAX_PARENTS 5
  24. /**
  25. * sun4i_osc_clk_setup() - Setup function for gatable oscillator
  26. */
  27. #define SUNXI_OSC24M_GATE 0
  28. static void __init sun4i_osc_clk_setup(struct device_node *node)
  29. {
  30. struct clk *clk;
  31. struct clk_fixed_rate *fixed;
  32. struct clk_gate *gate;
  33. const char *clk_name = node->name;
  34. u32 rate;
  35. if (of_property_read_u32(node, "clock-frequency", &rate))
  36. return;
  37. /* allocate fixed-rate and gate clock structs */
  38. fixed = kzalloc(sizeof(struct clk_fixed_rate), GFP_KERNEL);
  39. if (!fixed)
  40. return;
  41. gate = kzalloc(sizeof(struct clk_gate), GFP_KERNEL);
  42. if (!gate)
  43. goto err_free_fixed;
  44. /* set up gate and fixed rate properties */
  45. gate->reg = of_iomap(node, 0);
  46. gate->bit_idx = SUNXI_OSC24M_GATE;
  47. gate->lock = &clk_lock;
  48. fixed->fixed_rate = rate;
  49. clk = clk_register_composite(NULL, clk_name,
  50. NULL, 0,
  51. NULL, NULL,
  52. &fixed->hw, &clk_fixed_rate_ops,
  53. &gate->hw, &clk_gate_ops,
  54. CLK_IS_ROOT);
  55. if (IS_ERR(clk))
  56. goto err_free_gate;
  57. of_clk_add_provider(node, of_clk_src_simple_get, clk);
  58. clk_register_clkdev(clk, clk_name, NULL);
  59. return;
  60. err_free_gate:
  61. kfree(gate);
  62. err_free_fixed:
  63. kfree(fixed);
  64. }
  65. CLK_OF_DECLARE(sun4i_osc, "allwinner,sun4i-osc-clk", sun4i_osc_clk_setup);
  66. /**
  67. * sun4i_get_pll1_factors() - calculates n, k, m, p factors for PLL1
  68. * PLL1 rate is calculated as follows
  69. * rate = (parent_rate * n * (k + 1) >> p) / (m + 1);
  70. * parent_rate is always 24Mhz
  71. */
  72. static void sun4i_get_pll1_factors(u32 *freq, u32 parent_rate,
  73. u8 *n, u8 *k, u8 *m, u8 *p)
  74. {
  75. u8 div;
  76. /* Normalize value to a 6M multiple */
  77. div = *freq / 6000000;
  78. *freq = 6000000 * div;
  79. /* we were called to round the frequency, we can now return */
  80. if (n == NULL)
  81. return;
  82. /* m is always zero for pll1 */
  83. *m = 0;
  84. /* k is 1 only on these cases */
  85. if (*freq >= 768000000 || *freq == 42000000 || *freq == 54000000)
  86. *k = 1;
  87. else
  88. *k = 0;
  89. /* p will be 3 for divs under 10 */
  90. if (div < 10)
  91. *p = 3;
  92. /* p will be 2 for divs between 10 - 20 and odd divs under 32 */
  93. else if (div < 20 || (div < 32 && (div & 1)))
  94. *p = 2;
  95. /* p will be 1 for even divs under 32, divs under 40 and odd pairs
  96. * of divs between 40-62 */
  97. else if (div < 40 || (div < 64 && (div & 2)))
  98. *p = 1;
  99. /* any other entries have p = 0 */
  100. else
  101. *p = 0;
  102. /* calculate a suitable n based on k and p */
  103. div <<= *p;
  104. div /= (*k + 1);
  105. *n = div / 4;
  106. }
  107. /**
  108. * sun6i_a31_get_pll1_factors() - calculates n, k and m factors for PLL1
  109. * PLL1 rate is calculated as follows
  110. * rate = parent_rate * (n + 1) * (k + 1) / (m + 1);
  111. * parent_rate should always be 24MHz
  112. */
  113. static void sun6i_a31_get_pll1_factors(u32 *freq, u32 parent_rate,
  114. u8 *n, u8 *k, u8 *m, u8 *p)
  115. {
  116. /*
  117. * We can operate only on MHz, this will make our life easier
  118. * later.
  119. */
  120. u32 freq_mhz = *freq / 1000000;
  121. u32 parent_freq_mhz = parent_rate / 1000000;
  122. /*
  123. * Round down the frequency to the closest multiple of either
  124. * 6 or 16
  125. */
  126. u32 round_freq_6 = round_down(freq_mhz, 6);
  127. u32 round_freq_16 = round_down(freq_mhz, 16);
  128. if (round_freq_6 > round_freq_16)
  129. freq_mhz = round_freq_6;
  130. else
  131. freq_mhz = round_freq_16;
  132. *freq = freq_mhz * 1000000;
  133. /*
  134. * If the factors pointer are null, we were just called to
  135. * round down the frequency.
  136. * Exit.
  137. */
  138. if (n == NULL)
  139. return;
  140. /* If the frequency is a multiple of 32 MHz, k is always 3 */
  141. if (!(freq_mhz % 32))
  142. *k = 3;
  143. /* If the frequency is a multiple of 9 MHz, k is always 2 */
  144. else if (!(freq_mhz % 9))
  145. *k = 2;
  146. /* If the frequency is a multiple of 8 MHz, k is always 1 */
  147. else if (!(freq_mhz % 8))
  148. *k = 1;
  149. /* Otherwise, we don't use the k factor */
  150. else
  151. *k = 0;
  152. /*
  153. * If the frequency is a multiple of 2 but not a multiple of
  154. * 3, m is 3. This is the first time we use 6 here, yet we
  155. * will use it on several other places.
  156. * We use this number because it's the lowest frequency we can
  157. * generate (with n = 0, k = 0, m = 3), so every other frequency
  158. * somehow relates to this frequency.
  159. */
  160. if ((freq_mhz % 6) == 2 || (freq_mhz % 6) == 4)
  161. *m = 2;
  162. /*
  163. * If the frequency is a multiple of 6MHz, but the factor is
  164. * odd, m will be 3
  165. */
  166. else if ((freq_mhz / 6) & 1)
  167. *m = 3;
  168. /* Otherwise, we end up with m = 1 */
  169. else
  170. *m = 1;
  171. /* Calculate n thanks to the above factors we already got */
  172. *n = freq_mhz * (*m + 1) / ((*k + 1) * parent_freq_mhz) - 1;
  173. /*
  174. * If n end up being outbound, and that we can still decrease
  175. * m, do it.
  176. */
  177. if ((*n + 1) > 31 && (*m + 1) > 1) {
  178. *n = (*n + 1) / 2 - 1;
  179. *m = (*m + 1) / 2 - 1;
  180. }
  181. }
  182. /**
  183. * sun4i_get_pll5_factors() - calculates n, k factors for PLL5
  184. * PLL5 rate is calculated as follows
  185. * rate = parent_rate * n * (k + 1)
  186. * parent_rate is always 24Mhz
  187. */
  188. static void sun4i_get_pll5_factors(u32 *freq, u32 parent_rate,
  189. u8 *n, u8 *k, u8 *m, u8 *p)
  190. {
  191. u8 div;
  192. /* Normalize value to a parent_rate multiple (24M) */
  193. div = *freq / parent_rate;
  194. *freq = parent_rate * div;
  195. /* we were called to round the frequency, we can now return */
  196. if (n == NULL)
  197. return;
  198. if (div < 31)
  199. *k = 0;
  200. else if (div / 2 < 31)
  201. *k = 1;
  202. else if (div / 3 < 31)
  203. *k = 2;
  204. else
  205. *k = 3;
  206. *n = DIV_ROUND_UP(div, (*k+1));
  207. }
  208. /**
  209. * sun4i_get_apb1_factors() - calculates m, p factors for APB1
  210. * APB1 rate is calculated as follows
  211. * rate = (parent_rate >> p) / (m + 1);
  212. */
  213. static void sun4i_get_apb1_factors(u32 *freq, u32 parent_rate,
  214. u8 *n, u8 *k, u8 *m, u8 *p)
  215. {
  216. u8 calcm, calcp;
  217. if (parent_rate < *freq)
  218. *freq = parent_rate;
  219. parent_rate = (parent_rate + (*freq - 1)) / *freq;
  220. /* Invalid rate! */
  221. if (parent_rate > 32)
  222. return;
  223. if (parent_rate <= 4)
  224. calcp = 0;
  225. else if (parent_rate <= 8)
  226. calcp = 1;
  227. else if (parent_rate <= 16)
  228. calcp = 2;
  229. else
  230. calcp = 3;
  231. calcm = (parent_rate >> calcp) - 1;
  232. *freq = (parent_rate >> calcp) / (calcm + 1);
  233. /* we were called to round the frequency, we can now return */
  234. if (n == NULL)
  235. return;
  236. *m = calcm;
  237. *p = calcp;
  238. }
  239. /**
  240. * sun4i_get_mod0_factors() - calculates m, n factors for MOD0-style clocks
  241. * MMC rate is calculated as follows
  242. * rate = (parent_rate >> p) / (m + 1);
  243. */
  244. static void sun4i_get_mod0_factors(u32 *freq, u32 parent_rate,
  245. u8 *n, u8 *k, u8 *m, u8 *p)
  246. {
  247. u8 div, calcm, calcp;
  248. /* These clocks can only divide, so we will never be able to achieve
  249. * frequencies higher than the parent frequency */
  250. if (*freq > parent_rate)
  251. *freq = parent_rate;
  252. div = parent_rate / *freq;
  253. if (div < 16)
  254. calcp = 0;
  255. else if (div / 2 < 16)
  256. calcp = 1;
  257. else if (div / 4 < 16)
  258. calcp = 2;
  259. else
  260. calcp = 3;
  261. calcm = DIV_ROUND_UP(div, 1 << calcp);
  262. *freq = (parent_rate >> calcp) / calcm;
  263. /* we were called to round the frequency, we can now return */
  264. if (n == NULL)
  265. return;
  266. *m = calcm - 1;
  267. *p = calcp;
  268. }
  269. /**
  270. * sun7i_a20_get_out_factors() - calculates m, p factors for CLK_OUT_A/B
  271. * CLK_OUT rate is calculated as follows
  272. * rate = (parent_rate >> p) / (m + 1);
  273. */
  274. static void sun7i_a20_get_out_factors(u32 *freq, u32 parent_rate,
  275. u8 *n, u8 *k, u8 *m, u8 *p)
  276. {
  277. u8 div, calcm, calcp;
  278. /* These clocks can only divide, so we will never be able to achieve
  279. * frequencies higher than the parent frequency */
  280. if (*freq > parent_rate)
  281. *freq = parent_rate;
  282. div = parent_rate / *freq;
  283. if (div < 32)
  284. calcp = 0;
  285. else if (div / 2 < 32)
  286. calcp = 1;
  287. else if (div / 4 < 32)
  288. calcp = 2;
  289. else
  290. calcp = 3;
  291. calcm = DIV_ROUND_UP(div, 1 << calcp);
  292. *freq = (parent_rate >> calcp) / calcm;
  293. /* we were called to round the frequency, we can now return */
  294. if (n == NULL)
  295. return;
  296. *m = calcm - 1;
  297. *p = calcp;
  298. }
  299. /**
  300. * sunxi_factors_clk_setup() - Setup function for factor clocks
  301. */
  302. #define SUNXI_FACTORS_MUX_MASK 0x3
  303. struct factors_data {
  304. int enable;
  305. int mux;
  306. struct clk_factors_config *table;
  307. void (*getter) (u32 *rate, u32 parent_rate, u8 *n, u8 *k, u8 *m, u8 *p);
  308. };
  309. static struct clk_factors_config sun4i_pll1_config = {
  310. .nshift = 8,
  311. .nwidth = 5,
  312. .kshift = 4,
  313. .kwidth = 2,
  314. .mshift = 0,
  315. .mwidth = 2,
  316. .pshift = 16,
  317. .pwidth = 2,
  318. };
  319. static struct clk_factors_config sun6i_a31_pll1_config = {
  320. .nshift = 8,
  321. .nwidth = 5,
  322. .kshift = 4,
  323. .kwidth = 2,
  324. .mshift = 0,
  325. .mwidth = 2,
  326. };
  327. static struct clk_factors_config sun4i_pll5_config = {
  328. .nshift = 8,
  329. .nwidth = 5,
  330. .kshift = 4,
  331. .kwidth = 2,
  332. };
  333. static struct clk_factors_config sun4i_apb1_config = {
  334. .mshift = 0,
  335. .mwidth = 5,
  336. .pshift = 16,
  337. .pwidth = 2,
  338. };
  339. /* user manual says "n" but it's really "p" */
  340. static struct clk_factors_config sun4i_mod0_config = {
  341. .mshift = 0,
  342. .mwidth = 4,
  343. .pshift = 16,
  344. .pwidth = 2,
  345. };
  346. /* user manual says "n" but it's really "p" */
  347. static struct clk_factors_config sun7i_a20_out_config = {
  348. .mshift = 8,
  349. .mwidth = 5,
  350. .pshift = 20,
  351. .pwidth = 2,
  352. };
  353. static const struct factors_data sun4i_pll1_data __initconst = {
  354. .enable = 31,
  355. .table = &sun4i_pll1_config,
  356. .getter = sun4i_get_pll1_factors,
  357. };
  358. static const struct factors_data sun6i_a31_pll1_data __initconst = {
  359. .enable = 31,
  360. .table = &sun6i_a31_pll1_config,
  361. .getter = sun6i_a31_get_pll1_factors,
  362. };
  363. static const struct factors_data sun4i_pll5_data __initconst = {
  364. .enable = 31,
  365. .table = &sun4i_pll5_config,
  366. .getter = sun4i_get_pll5_factors,
  367. };
  368. static const struct factors_data sun4i_apb1_data __initconst = {
  369. .table = &sun4i_apb1_config,
  370. .getter = sun4i_get_apb1_factors,
  371. };
  372. static const struct factors_data sun4i_mod0_data __initconst = {
  373. .enable = 31,
  374. .mux = 24,
  375. .table = &sun4i_mod0_config,
  376. .getter = sun4i_get_mod0_factors,
  377. };
  378. static const struct factors_data sun7i_a20_out_data __initconst = {
  379. .enable = 31,
  380. .mux = 24,
  381. .table = &sun7i_a20_out_config,
  382. .getter = sun7i_a20_get_out_factors,
  383. };
  384. static struct clk * __init sunxi_factors_clk_setup(struct device_node *node,
  385. const struct factors_data *data)
  386. {
  387. struct clk *clk;
  388. struct clk_factors *factors;
  389. struct clk_gate *gate = NULL;
  390. struct clk_mux *mux = NULL;
  391. struct clk_hw *gate_hw = NULL;
  392. struct clk_hw *mux_hw = NULL;
  393. const char *clk_name = node->name;
  394. const char *parents[SUNXI_MAX_PARENTS];
  395. void *reg;
  396. int i = 0;
  397. reg = of_iomap(node, 0);
  398. /* if we have a mux, we will have >1 parents */
  399. while (i < SUNXI_MAX_PARENTS &&
  400. (parents[i] = of_clk_get_parent_name(node, i)) != NULL)
  401. i++;
  402. /* Nodes should be providing the name via clock-output-names
  403. * but originally our dts didn't, and so we used node->name.
  404. * The new, better nodes look like clk@deadbeef, so we pull the
  405. * name just in this case */
  406. if (!strcmp("clk", clk_name)) {
  407. of_property_read_string_index(node, "clock-output-names",
  408. 0, &clk_name);
  409. }
  410. factors = kzalloc(sizeof(struct clk_factors), GFP_KERNEL);
  411. if (!factors)
  412. return NULL;
  413. /* Add a gate if this factor clock can be gated */
  414. if (data->enable) {
  415. gate = kzalloc(sizeof(struct clk_gate), GFP_KERNEL);
  416. if (!gate) {
  417. kfree(factors);
  418. return NULL;
  419. }
  420. /* set up gate properties */
  421. gate->reg = reg;
  422. gate->bit_idx = data->enable;
  423. gate->lock = &clk_lock;
  424. gate_hw = &gate->hw;
  425. }
  426. /* Add a mux if this factor clock can be muxed */
  427. if (data->mux) {
  428. mux = kzalloc(sizeof(struct clk_mux), GFP_KERNEL);
  429. if (!mux) {
  430. kfree(factors);
  431. kfree(gate);
  432. return NULL;
  433. }
  434. /* set up gate properties */
  435. mux->reg = reg;
  436. mux->shift = data->mux;
  437. mux->mask = SUNXI_FACTORS_MUX_MASK;
  438. mux->lock = &clk_lock;
  439. mux_hw = &mux->hw;
  440. }
  441. /* set up factors properties */
  442. factors->reg = reg;
  443. factors->config = data->table;
  444. factors->get_factors = data->getter;
  445. factors->lock = &clk_lock;
  446. clk = clk_register_composite(NULL, clk_name,
  447. parents, i,
  448. mux_hw, &clk_mux_ops,
  449. &factors->hw, &clk_factors_ops,
  450. gate_hw, &clk_gate_ops, 0);
  451. if (!IS_ERR(clk)) {
  452. of_clk_add_provider(node, of_clk_src_simple_get, clk);
  453. clk_register_clkdev(clk, clk_name, NULL);
  454. }
  455. return clk;
  456. }
  457. /**
  458. * sunxi_mux_clk_setup() - Setup function for muxes
  459. */
  460. #define SUNXI_MUX_GATE_WIDTH 2
  461. struct mux_data {
  462. u8 shift;
  463. };
  464. static const struct mux_data sun4i_cpu_mux_data __initconst = {
  465. .shift = 16,
  466. };
  467. static const struct mux_data sun6i_a31_ahb1_mux_data __initconst = {
  468. .shift = 12,
  469. };
  470. static const struct mux_data sun4i_apb1_mux_data __initconst = {
  471. .shift = 24,
  472. };
  473. static void __init sunxi_mux_clk_setup(struct device_node *node,
  474. struct mux_data *data)
  475. {
  476. struct clk *clk;
  477. const char *clk_name = node->name;
  478. const char *parents[SUNXI_MAX_PARENTS];
  479. void *reg;
  480. int i = 0;
  481. reg = of_iomap(node, 0);
  482. while (i < SUNXI_MAX_PARENTS &&
  483. (parents[i] = of_clk_get_parent_name(node, i)) != NULL)
  484. i++;
  485. clk = clk_register_mux(NULL, clk_name, parents, i,
  486. CLK_SET_RATE_NO_REPARENT, reg,
  487. data->shift, SUNXI_MUX_GATE_WIDTH,
  488. 0, &clk_lock);
  489. if (clk) {
  490. of_clk_add_provider(node, of_clk_src_simple_get, clk);
  491. clk_register_clkdev(clk, clk_name, NULL);
  492. }
  493. }
  494. /**
  495. * sunxi_divider_clk_setup() - Setup function for simple divider clocks
  496. */
  497. struct div_data {
  498. u8 shift;
  499. u8 pow;
  500. u8 width;
  501. };
  502. static const struct div_data sun4i_axi_data __initconst = {
  503. .shift = 0,
  504. .pow = 0,
  505. .width = 2,
  506. };
  507. static const struct div_data sun4i_ahb_data __initconst = {
  508. .shift = 4,
  509. .pow = 1,
  510. .width = 2,
  511. };
  512. static const struct div_data sun4i_apb0_data __initconst = {
  513. .shift = 8,
  514. .pow = 1,
  515. .width = 2,
  516. };
  517. static const struct div_data sun6i_a31_apb2_div_data __initconst = {
  518. .shift = 0,
  519. .pow = 0,
  520. .width = 4,
  521. };
  522. static void __init sunxi_divider_clk_setup(struct device_node *node,
  523. struct div_data *data)
  524. {
  525. struct clk *clk;
  526. const char *clk_name = node->name;
  527. const char *clk_parent;
  528. void *reg;
  529. reg = of_iomap(node, 0);
  530. clk_parent = of_clk_get_parent_name(node, 0);
  531. clk = clk_register_divider(NULL, clk_name, clk_parent, 0,
  532. reg, data->shift, data->width,
  533. data->pow ? CLK_DIVIDER_POWER_OF_TWO : 0,
  534. &clk_lock);
  535. if (clk) {
  536. of_clk_add_provider(node, of_clk_src_simple_get, clk);
  537. clk_register_clkdev(clk, clk_name, NULL);
  538. }
  539. }
  540. /**
  541. * sunxi_gates_clk_setup() - Setup function for leaf gates on clocks
  542. */
  543. #define SUNXI_GATES_MAX_SIZE 64
  544. struct gates_data {
  545. DECLARE_BITMAP(mask, SUNXI_GATES_MAX_SIZE);
  546. };
  547. static const struct gates_data sun4i_axi_gates_data __initconst = {
  548. .mask = {1},
  549. };
  550. static const struct gates_data sun4i_ahb_gates_data __initconst = {
  551. .mask = {0x7F77FFF, 0x14FB3F},
  552. };
  553. static const struct gates_data sun5i_a10s_ahb_gates_data __initconst = {
  554. .mask = {0x147667e7, 0x185915},
  555. };
  556. static const struct gates_data sun5i_a13_ahb_gates_data __initconst = {
  557. .mask = {0x107067e7, 0x185111},
  558. };
  559. static const struct gates_data sun6i_a31_ahb1_gates_data __initconst = {
  560. .mask = {0xEDFE7F62, 0x794F931},
  561. };
  562. static const struct gates_data sun7i_a20_ahb_gates_data __initconst = {
  563. .mask = { 0x12f77fff, 0x16ff3f },
  564. };
  565. static const struct gates_data sun4i_apb0_gates_data __initconst = {
  566. .mask = {0x4EF},
  567. };
  568. static const struct gates_data sun5i_a10s_apb0_gates_data __initconst = {
  569. .mask = {0x469},
  570. };
  571. static const struct gates_data sun5i_a13_apb0_gates_data __initconst = {
  572. .mask = {0x61},
  573. };
  574. static const struct gates_data sun7i_a20_apb0_gates_data __initconst = {
  575. .mask = { 0x4ff },
  576. };
  577. static const struct gates_data sun4i_apb1_gates_data __initconst = {
  578. .mask = {0xFF00F7},
  579. };
  580. static const struct gates_data sun5i_a10s_apb1_gates_data __initconst = {
  581. .mask = {0xf0007},
  582. };
  583. static const struct gates_data sun5i_a13_apb1_gates_data __initconst = {
  584. .mask = {0xa0007},
  585. };
  586. static const struct gates_data sun6i_a31_apb1_gates_data __initconst = {
  587. .mask = {0x3031},
  588. };
  589. static const struct gates_data sun6i_a31_apb2_gates_data __initconst = {
  590. .mask = {0x3F000F},
  591. };
  592. static const struct gates_data sun7i_a20_apb1_gates_data __initconst = {
  593. .mask = { 0xff80ff },
  594. };
  595. static void __init sunxi_gates_clk_setup(struct device_node *node,
  596. struct gates_data *data)
  597. {
  598. struct clk_onecell_data *clk_data;
  599. const char *clk_parent;
  600. const char *clk_name;
  601. void *reg;
  602. int qty;
  603. int i = 0;
  604. int j = 0;
  605. int ignore;
  606. reg = of_iomap(node, 0);
  607. clk_parent = of_clk_get_parent_name(node, 0);
  608. /* Worst-case size approximation and memory allocation */
  609. qty = find_last_bit(data->mask, SUNXI_GATES_MAX_SIZE);
  610. clk_data = kmalloc(sizeof(struct clk_onecell_data), GFP_KERNEL);
  611. if (!clk_data)
  612. return;
  613. clk_data->clks = kzalloc((qty+1) * sizeof(struct clk *), GFP_KERNEL);
  614. if (!clk_data->clks) {
  615. kfree(clk_data);
  616. return;
  617. }
  618. for_each_set_bit(i, data->mask, SUNXI_GATES_MAX_SIZE) {
  619. of_property_read_string_index(node, "clock-output-names",
  620. j, &clk_name);
  621. /* No driver claims this clock, but it should remain gated */
  622. ignore = !strcmp("ahb_sdram", clk_name) ? CLK_IGNORE_UNUSED : 0;
  623. clk_data->clks[i] = clk_register_gate(NULL, clk_name,
  624. clk_parent, ignore,
  625. reg + 4 * (i/32), i % 32,
  626. 0, &clk_lock);
  627. WARN_ON(IS_ERR(clk_data->clks[i]));
  628. j++;
  629. }
  630. /* Adjust to the real max */
  631. clk_data->clk_num = i;
  632. of_clk_add_provider(node, of_clk_src_onecell_get, clk_data);
  633. }
  634. /**
  635. * sunxi_divs_clk_setup() helper data
  636. */
  637. #define SUNXI_DIVS_MAX_QTY 2
  638. #define SUNXI_DIVISOR_WIDTH 2
  639. struct divs_data {
  640. const struct factors_data *factors; /* data for the factor clock */
  641. struct {
  642. u8 fixed; /* is it a fixed divisor? if not... */
  643. struct clk_div_table *table; /* is it a table based divisor? */
  644. u8 shift; /* otherwise it's a normal divisor with this shift */
  645. u8 pow; /* is it power-of-two based? */
  646. u8 gate; /* is it independently gateable? */
  647. } div[SUNXI_DIVS_MAX_QTY];
  648. };
  649. static struct clk_div_table pll6_sata_tbl[] = {
  650. { .val = 0, .div = 6, },
  651. { .val = 1, .div = 12, },
  652. { .val = 2, .div = 18, },
  653. { .val = 3, .div = 24, },
  654. { } /* sentinel */
  655. };
  656. static const struct divs_data pll5_divs_data __initconst = {
  657. .factors = &sun4i_pll5_data,
  658. .div = {
  659. { .shift = 0, .pow = 0, }, /* M, DDR */
  660. { .shift = 16, .pow = 1, }, /* P, other */
  661. }
  662. };
  663. static const struct divs_data pll6_divs_data __initconst = {
  664. .factors = &sun4i_pll5_data,
  665. .div = {
  666. { .shift = 0, .table = pll6_sata_tbl, .gate = 14 }, /* M, SATA */
  667. { .fixed = 2 }, /* P, other */
  668. }
  669. };
  670. /**
  671. * sunxi_divs_clk_setup() - Setup function for leaf divisors on clocks
  672. *
  673. * These clocks look something like this
  674. * ________________________
  675. * | ___divisor 1---|----> to consumer
  676. * parent >--| pll___/___divisor 2---|----> to consumer
  677. * | \_______________|____> to consumer
  678. * |________________________|
  679. */
  680. static void __init sunxi_divs_clk_setup(struct device_node *node,
  681. struct divs_data *data)
  682. {
  683. struct clk_onecell_data *clk_data;
  684. const char *parent = node->name;
  685. const char *clk_name;
  686. struct clk **clks, *pclk;
  687. struct clk_hw *gate_hw, *rate_hw;
  688. const struct clk_ops *rate_ops;
  689. struct clk_gate *gate = NULL;
  690. struct clk_fixed_factor *fix_factor;
  691. struct clk_divider *divider;
  692. void *reg;
  693. int i = 0;
  694. int flags, clkflags;
  695. /* Set up factor clock that we will be dividing */
  696. pclk = sunxi_factors_clk_setup(node, data->factors);
  697. reg = of_iomap(node, 0);
  698. clk_data = kmalloc(sizeof(struct clk_onecell_data), GFP_KERNEL);
  699. if (!clk_data)
  700. return;
  701. clks = kzalloc((SUNXI_DIVS_MAX_QTY+1) * sizeof(*clks), GFP_KERNEL);
  702. if (!clks)
  703. goto free_clkdata;
  704. clk_data->clks = clks;
  705. /* It's not a good idea to have automatic reparenting changing
  706. * our RAM clock! */
  707. clkflags = !strcmp("pll5", parent) ? 0 : CLK_SET_RATE_PARENT;
  708. for (i = 0; i < SUNXI_DIVS_MAX_QTY; i++) {
  709. if (of_property_read_string_index(node, "clock-output-names",
  710. i, &clk_name) != 0)
  711. break;
  712. gate_hw = NULL;
  713. rate_hw = NULL;
  714. rate_ops = NULL;
  715. /* If this leaf clock can be gated, create a gate */
  716. if (data->div[i].gate) {
  717. gate = kzalloc(sizeof(*gate), GFP_KERNEL);
  718. if (!gate)
  719. goto free_clks;
  720. gate->reg = reg;
  721. gate->bit_idx = data->div[i].gate;
  722. gate->lock = &clk_lock;
  723. gate_hw = &gate->hw;
  724. }
  725. /* Leaves can be fixed or configurable divisors */
  726. if (data->div[i].fixed) {
  727. fix_factor = kzalloc(sizeof(*fix_factor), GFP_KERNEL);
  728. if (!fix_factor)
  729. goto free_gate;
  730. fix_factor->mult = 1;
  731. fix_factor->div = data->div[i].fixed;
  732. rate_hw = &fix_factor->hw;
  733. rate_ops = &clk_fixed_factor_ops;
  734. } else {
  735. divider = kzalloc(sizeof(*divider), GFP_KERNEL);
  736. if (!divider)
  737. goto free_gate;
  738. flags = data->div[i].pow ? CLK_DIVIDER_POWER_OF_TWO : 0;
  739. divider->reg = reg;
  740. divider->shift = data->div[i].shift;
  741. divider->width = SUNXI_DIVISOR_WIDTH;
  742. divider->flags = flags;
  743. divider->lock = &clk_lock;
  744. divider->table = data->div[i].table;
  745. rate_hw = &divider->hw;
  746. rate_ops = &clk_divider_ops;
  747. }
  748. /* Wrap the (potential) gate and the divisor on a composite
  749. * clock to unify them */
  750. clks[i] = clk_register_composite(NULL, clk_name, &parent, 1,
  751. NULL, NULL,
  752. rate_hw, rate_ops,
  753. gate_hw, &clk_gate_ops,
  754. clkflags);
  755. WARN_ON(IS_ERR(clk_data->clks[i]));
  756. clk_register_clkdev(clks[i], clk_name, NULL);
  757. }
  758. /* The last clock available on the getter is the parent */
  759. clks[i++] = pclk;
  760. /* Adjust to the real max */
  761. clk_data->clk_num = i;
  762. of_clk_add_provider(node, of_clk_src_onecell_get, clk_data);
  763. return;
  764. free_gate:
  765. kfree(gate);
  766. free_clks:
  767. kfree(clks);
  768. free_clkdata:
  769. kfree(clk_data);
  770. }
  771. /* Matches for factors clocks */
  772. static const struct of_device_id clk_factors_match[] __initconst = {
  773. {.compatible = "allwinner,sun4i-pll1-clk", .data = &sun4i_pll1_data,},
  774. {.compatible = "allwinner,sun6i-a31-pll1-clk", .data = &sun6i_a31_pll1_data,},
  775. {.compatible = "allwinner,sun4i-apb1-clk", .data = &sun4i_apb1_data,},
  776. {.compatible = "allwinner,sun4i-mod0-clk", .data = &sun4i_mod0_data,},
  777. {.compatible = "allwinner,sun7i-a20-out-clk", .data = &sun7i_a20_out_data,},
  778. {}
  779. };
  780. /* Matches for divider clocks */
  781. static const struct of_device_id clk_div_match[] __initconst = {
  782. {.compatible = "allwinner,sun4i-axi-clk", .data = &sun4i_axi_data,},
  783. {.compatible = "allwinner,sun4i-ahb-clk", .data = &sun4i_ahb_data,},
  784. {.compatible = "allwinner,sun4i-apb0-clk", .data = &sun4i_apb0_data,},
  785. {.compatible = "allwinner,sun6i-a31-apb2-div-clk", .data = &sun6i_a31_apb2_div_data,},
  786. {}
  787. };
  788. /* Matches for divided outputs */
  789. static const struct of_device_id clk_divs_match[] __initconst = {
  790. {.compatible = "allwinner,sun4i-pll5-clk", .data = &pll5_divs_data,},
  791. {.compatible = "allwinner,sun4i-pll6-clk", .data = &pll6_divs_data,},
  792. {}
  793. };
  794. /* Matches for mux clocks */
  795. static const struct of_device_id clk_mux_match[] __initconst = {
  796. {.compatible = "allwinner,sun4i-cpu-clk", .data = &sun4i_cpu_mux_data,},
  797. {.compatible = "allwinner,sun4i-apb1-mux-clk", .data = &sun4i_apb1_mux_data,},
  798. {.compatible = "allwinner,sun6i-a31-ahb1-mux-clk", .data = &sun6i_a31_ahb1_mux_data,},
  799. {}
  800. };
  801. /* Matches for gate clocks */
  802. static const struct of_device_id clk_gates_match[] __initconst = {
  803. {.compatible = "allwinner,sun4i-axi-gates-clk", .data = &sun4i_axi_gates_data,},
  804. {.compatible = "allwinner,sun4i-ahb-gates-clk", .data = &sun4i_ahb_gates_data,},
  805. {.compatible = "allwinner,sun5i-a10s-ahb-gates-clk", .data = &sun5i_a10s_ahb_gates_data,},
  806. {.compatible = "allwinner,sun5i-a13-ahb-gates-clk", .data = &sun5i_a13_ahb_gates_data,},
  807. {.compatible = "allwinner,sun6i-a31-ahb1-gates-clk", .data = &sun6i_a31_ahb1_gates_data,},
  808. {.compatible = "allwinner,sun7i-a20-ahb-gates-clk", .data = &sun7i_a20_ahb_gates_data,},
  809. {.compatible = "allwinner,sun4i-apb0-gates-clk", .data = &sun4i_apb0_gates_data,},
  810. {.compatible = "allwinner,sun5i-a10s-apb0-gates-clk", .data = &sun5i_a10s_apb0_gates_data,},
  811. {.compatible = "allwinner,sun5i-a13-apb0-gates-clk", .data = &sun5i_a13_apb0_gates_data,},
  812. {.compatible = "allwinner,sun7i-a20-apb0-gates-clk", .data = &sun7i_a20_apb0_gates_data,},
  813. {.compatible = "allwinner,sun4i-apb1-gates-clk", .data = &sun4i_apb1_gates_data,},
  814. {.compatible = "allwinner,sun5i-a10s-apb1-gates-clk", .data = &sun5i_a10s_apb1_gates_data,},
  815. {.compatible = "allwinner,sun5i-a13-apb1-gates-clk", .data = &sun5i_a13_apb1_gates_data,},
  816. {.compatible = "allwinner,sun6i-a31-apb1-gates-clk", .data = &sun6i_a31_apb1_gates_data,},
  817. {.compatible = "allwinner,sun7i-a20-apb1-gates-clk", .data = &sun7i_a20_apb1_gates_data,},
  818. {.compatible = "allwinner,sun6i-a31-apb2-gates-clk", .data = &sun6i_a31_apb2_gates_data,},
  819. {}
  820. };
  821. static void __init of_sunxi_table_clock_setup(const struct of_device_id *clk_match,
  822. void *function)
  823. {
  824. struct device_node *np;
  825. const struct div_data *data;
  826. const struct of_device_id *match;
  827. void (*setup_function)(struct device_node *, const void *) = function;
  828. for_each_matching_node(np, clk_match) {
  829. match = of_match_node(clk_match, np);
  830. data = match->data;
  831. setup_function(np, data);
  832. }
  833. }
  834. /**
  835. * System clock protection
  836. *
  837. * By enabling these critical clocks, we prevent their accidental gating
  838. * by the framework
  839. */
  840. static void __init sunxi_clock_protect(void)
  841. {
  842. struct clk *clk;
  843. /* memory bus clock - sun5i+ */
  844. clk = clk_get(NULL, "mbus");
  845. if (!IS_ERR(clk)) {
  846. clk_prepare_enable(clk);
  847. clk_put(clk);
  848. }
  849. /* DDR clock - sun4i+ */
  850. clk = clk_get(NULL, "pll5_ddr");
  851. if (!IS_ERR(clk)) {
  852. clk_prepare_enable(clk);
  853. clk_put(clk);
  854. }
  855. }
  856. static void __init sunxi_init_clocks(void)
  857. {
  858. /* Register factor clocks */
  859. of_sunxi_table_clock_setup(clk_factors_match, sunxi_factors_clk_setup);
  860. /* Register divider clocks */
  861. of_sunxi_table_clock_setup(clk_div_match, sunxi_divider_clk_setup);
  862. /* Register divided output clocks */
  863. of_sunxi_table_clock_setup(clk_divs_match, sunxi_divs_clk_setup);
  864. /* Register mux clocks */
  865. of_sunxi_table_clock_setup(clk_mux_match, sunxi_mux_clk_setup);
  866. /* Register gate clocks */
  867. of_sunxi_table_clock_setup(clk_gates_match, sunxi_gates_clk_setup);
  868. /* Enable core system clocks */
  869. sunxi_clock_protect();
  870. }
  871. CLK_OF_DECLARE(sun4i_a10_clk_init, "allwinner,sun4i-a10", sunxi_init_clocks);
  872. CLK_OF_DECLARE(sun5i_a10s_clk_init, "allwinner,sun5i-a10s", sunxi_init_clocks);
  873. CLK_OF_DECLARE(sun5i_a13_clk_init, "allwinner,sun5i-a13", sunxi_init_clocks);
  874. CLK_OF_DECLARE(sun6i_a31_clk_init, "allwinner,sun6i-a31", sunxi_init_clocks);
  875. CLK_OF_DECLARE(sun7i_a20_clk_init, "allwinner,sun7i-a20", sunxi_init_clocks);