clk-sunxi.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186
  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.h>
  17. #include <linux/clk-provider.h>
  18. #include <linux/clkdev.h>
  19. #include <linux/of.h>
  20. #include <linux/of_address.h>
  21. #include <linux/reset-controller.h>
  22. #include <linux/slab.h>
  23. #include <linux/spinlock.h>
  24. #include <linux/log2.h>
  25. #include "clk-factors.h"
  26. static DEFINE_SPINLOCK(clk_lock);
  27. /* Maximum number of parents our clocks have */
  28. #define SUNXI_MAX_PARENTS 5
  29. /**
  30. * sun4i_get_pll1_factors() - calculates n, k, m, p factors for PLL1
  31. * PLL1 rate is calculated as follows
  32. * rate = (parent_rate * n * (k + 1) >> p) / (m + 1);
  33. * parent_rate is always 24Mhz
  34. */
  35. static void sun4i_get_pll1_factors(struct factors_request *req)
  36. {
  37. u8 div;
  38. /* Normalize value to a 6M multiple */
  39. div = req->rate / 6000000;
  40. req->rate = 6000000 * div;
  41. /* m is always zero for pll1 */
  42. req->m = 0;
  43. /* k is 1 only on these cases */
  44. if (req->rate >= 768000000 || req->rate == 42000000 ||
  45. req->rate == 54000000)
  46. req->k = 1;
  47. else
  48. req->k = 0;
  49. /* p will be 3 for divs under 10 */
  50. if (div < 10)
  51. req->p = 3;
  52. /* p will be 2 for divs between 10 - 20 and odd divs under 32 */
  53. else if (div < 20 || (div < 32 && (div & 1)))
  54. req->p = 2;
  55. /* p will be 1 for even divs under 32, divs under 40 and odd pairs
  56. * of divs between 40-62 */
  57. else if (div < 40 || (div < 64 && (div & 2)))
  58. req->p = 1;
  59. /* any other entries have p = 0 */
  60. else
  61. req->p = 0;
  62. /* calculate a suitable n based on k and p */
  63. div <<= req->p;
  64. div /= (req->k + 1);
  65. req->n = div / 4;
  66. }
  67. /**
  68. * sun6i_a31_get_pll1_factors() - calculates n, k and m factors for PLL1
  69. * PLL1 rate is calculated as follows
  70. * rate = parent_rate * (n + 1) * (k + 1) / (m + 1);
  71. * parent_rate should always be 24MHz
  72. */
  73. static void sun6i_a31_get_pll1_factors(struct factors_request *req)
  74. {
  75. /*
  76. * We can operate only on MHz, this will make our life easier
  77. * later.
  78. */
  79. u32 freq_mhz = req->rate / 1000000;
  80. u32 parent_freq_mhz = req->parent_rate / 1000000;
  81. /*
  82. * Round down the frequency to the closest multiple of either
  83. * 6 or 16
  84. */
  85. u32 round_freq_6 = round_down(freq_mhz, 6);
  86. u32 round_freq_16 = round_down(freq_mhz, 16);
  87. if (round_freq_6 > round_freq_16)
  88. freq_mhz = round_freq_6;
  89. else
  90. freq_mhz = round_freq_16;
  91. req->rate = freq_mhz * 1000000;
  92. /* If the frequency is a multiple of 32 MHz, k is always 3 */
  93. if (!(freq_mhz % 32))
  94. req->k = 3;
  95. /* If the frequency is a multiple of 9 MHz, k is always 2 */
  96. else if (!(freq_mhz % 9))
  97. req->k = 2;
  98. /* If the frequency is a multiple of 8 MHz, k is always 1 */
  99. else if (!(freq_mhz % 8))
  100. req->k = 1;
  101. /* Otherwise, we don't use the k factor */
  102. else
  103. req->k = 0;
  104. /*
  105. * If the frequency is a multiple of 2 but not a multiple of
  106. * 3, m is 3. This is the first time we use 6 here, yet we
  107. * will use it on several other places.
  108. * We use this number because it's the lowest frequency we can
  109. * generate (with n = 0, k = 0, m = 3), so every other frequency
  110. * somehow relates to this frequency.
  111. */
  112. if ((freq_mhz % 6) == 2 || (freq_mhz % 6) == 4)
  113. req->m = 2;
  114. /*
  115. * If the frequency is a multiple of 6MHz, but the factor is
  116. * odd, m will be 3
  117. */
  118. else if ((freq_mhz / 6) & 1)
  119. req->m = 3;
  120. /* Otherwise, we end up with m = 1 */
  121. else
  122. req->m = 1;
  123. /* Calculate n thanks to the above factors we already got */
  124. req->n = freq_mhz * (req->m + 1) / ((req->k + 1) * parent_freq_mhz)
  125. - 1;
  126. /*
  127. * If n end up being outbound, and that we can still decrease
  128. * m, do it.
  129. */
  130. if ((req->n + 1) > 31 && (req->m + 1) > 1) {
  131. req->n = (req->n + 1) / 2 - 1;
  132. req->m = (req->m + 1) / 2 - 1;
  133. }
  134. }
  135. /**
  136. * sun8i_a23_get_pll1_factors() - calculates n, k, m, p factors for PLL1
  137. * PLL1 rate is calculated as follows
  138. * rate = (parent_rate * (n + 1) * (k + 1) >> p) / (m + 1);
  139. * parent_rate is always 24Mhz
  140. */
  141. static void sun8i_a23_get_pll1_factors(struct factors_request *req)
  142. {
  143. u8 div;
  144. /* Normalize value to a 6M multiple */
  145. div = req->rate / 6000000;
  146. req->rate = 6000000 * div;
  147. /* m is always zero for pll1 */
  148. req->m = 0;
  149. /* k is 1 only on these cases */
  150. if (req->rate >= 768000000 || req->rate == 42000000 ||
  151. req->rate == 54000000)
  152. req->k = 1;
  153. else
  154. req->k = 0;
  155. /* p will be 2 for divs under 20 and odd divs under 32 */
  156. if (div < 20 || (div < 32 && (div & 1)))
  157. req->p = 2;
  158. /* p will be 1 for even divs under 32, divs under 40 and odd pairs
  159. * of divs between 40-62 */
  160. else if (div < 40 || (div < 64 && (div & 2)))
  161. req->p = 1;
  162. /* any other entries have p = 0 */
  163. else
  164. req->p = 0;
  165. /* calculate a suitable n based on k and p */
  166. div <<= req->p;
  167. div /= (req->k + 1);
  168. req->n = div / 4 - 1;
  169. }
  170. /**
  171. * sun4i_get_pll5_factors() - calculates n, k factors for PLL5
  172. * PLL5 rate is calculated as follows
  173. * rate = parent_rate * n * (k + 1)
  174. * parent_rate is always 24Mhz
  175. */
  176. static void sun4i_get_pll5_factors(struct factors_request *req)
  177. {
  178. u8 div;
  179. /* Normalize value to a parent_rate multiple (24M) */
  180. div = req->rate / req->parent_rate;
  181. req->rate = req->parent_rate * div;
  182. if (div < 31)
  183. req->k = 0;
  184. else if (div / 2 < 31)
  185. req->k = 1;
  186. else if (div / 3 < 31)
  187. req->k = 2;
  188. else
  189. req->k = 3;
  190. req->n = DIV_ROUND_UP(div, (req->k + 1));
  191. }
  192. /**
  193. * sun6i_a31_get_pll6_factors() - calculates n, k factors for A31 PLL6x2
  194. * PLL6x2 rate is calculated as follows
  195. * rate = parent_rate * (n + 1) * (k + 1)
  196. * parent_rate is always 24Mhz
  197. */
  198. static void sun6i_a31_get_pll6_factors(struct factors_request *req)
  199. {
  200. u8 div;
  201. /* Normalize value to a parent_rate multiple (24M) */
  202. div = req->rate / req->parent_rate;
  203. req->rate = req->parent_rate * div;
  204. req->k = div / 32;
  205. if (req->k > 3)
  206. req->k = 3;
  207. req->n = DIV_ROUND_UP(div, (req->k + 1)) - 1;
  208. }
  209. /**
  210. * sun5i_a13_get_ahb_factors() - calculates m, p factors for AHB
  211. * AHB rate is calculated as follows
  212. * rate = parent_rate >> p
  213. */
  214. static void sun5i_a13_get_ahb_factors(struct factors_request *req)
  215. {
  216. u32 div;
  217. /* divide only */
  218. if (req->parent_rate < req->rate)
  219. req->rate = req->parent_rate;
  220. /*
  221. * user manual says valid speed is 8k ~ 276M, but tests show it
  222. * can work at speeds up to 300M, just after reparenting to pll6
  223. */
  224. if (req->rate < 8000)
  225. req->rate = 8000;
  226. if (req->rate > 300000000)
  227. req->rate = 300000000;
  228. div = order_base_2(DIV_ROUND_UP(req->parent_rate, req->rate));
  229. /* p = 0 ~ 3 */
  230. if (div > 3)
  231. div = 3;
  232. req->rate = req->parent_rate >> div;
  233. req->p = div;
  234. }
  235. #define SUN6I_AHB1_PARENT_PLL6 3
  236. /**
  237. * sun6i_a31_get_ahb_factors() - calculates m, p factors for AHB
  238. * AHB rate is calculated as follows
  239. * rate = parent_rate >> p
  240. *
  241. * if parent is pll6, then
  242. * parent_rate = pll6 rate / (m + 1)
  243. */
  244. static void sun6i_get_ahb1_factors(struct factors_request *req)
  245. {
  246. u8 div, calcp, calcm = 1;
  247. /*
  248. * clock can only divide, so we will never be able to achieve
  249. * frequencies higher than the parent frequency
  250. */
  251. if (req->parent_rate && req->rate > req->parent_rate)
  252. req->rate = req->parent_rate;
  253. div = DIV_ROUND_UP(req->parent_rate, req->rate);
  254. /* calculate pre-divider if parent is pll6 */
  255. if (req->parent_index == SUN6I_AHB1_PARENT_PLL6) {
  256. if (div < 4)
  257. calcp = 0;
  258. else if (div / 2 < 4)
  259. calcp = 1;
  260. else if (div / 4 < 4)
  261. calcp = 2;
  262. else
  263. calcp = 3;
  264. calcm = DIV_ROUND_UP(div, 1 << calcp);
  265. } else {
  266. calcp = __roundup_pow_of_two(div);
  267. calcp = calcp > 3 ? 3 : calcp;
  268. }
  269. req->rate = (req->parent_rate / calcm) >> calcp;
  270. req->p = calcp;
  271. req->m = calcm - 1;
  272. }
  273. /**
  274. * sun6i_ahb1_recalc() - calculates AHB clock rate from m, p factors and
  275. * parent index
  276. */
  277. static void sun6i_ahb1_recalc(struct factors_request *req)
  278. {
  279. req->rate = req->parent_rate;
  280. /* apply pre-divider first if parent is pll6 */
  281. if (req->parent_index == SUN6I_AHB1_PARENT_PLL6)
  282. req->rate /= req->m + 1;
  283. /* clk divider */
  284. req->rate >>= req->p;
  285. }
  286. /**
  287. * sun4i_get_apb1_factors() - calculates m, p factors for APB1
  288. * APB1 rate is calculated as follows
  289. * rate = (parent_rate >> p) / (m + 1);
  290. */
  291. static void sun4i_get_apb1_factors(struct factors_request *req)
  292. {
  293. u8 calcm, calcp;
  294. int div;
  295. if (req->parent_rate < req->rate)
  296. req->rate = req->parent_rate;
  297. div = DIV_ROUND_UP(req->parent_rate, req->rate);
  298. /* Invalid rate! */
  299. if (div > 32)
  300. return;
  301. if (div <= 4)
  302. calcp = 0;
  303. else if (div <= 8)
  304. calcp = 1;
  305. else if (div <= 16)
  306. calcp = 2;
  307. else
  308. calcp = 3;
  309. calcm = (div >> calcp) - 1;
  310. req->rate = (req->parent_rate >> calcp) / (calcm + 1);
  311. req->m = calcm;
  312. req->p = calcp;
  313. }
  314. /**
  315. * sun7i_a20_get_out_factors() - calculates m, p factors for CLK_OUT_A/B
  316. * CLK_OUT rate is calculated as follows
  317. * rate = (parent_rate >> p) / (m + 1);
  318. */
  319. static void sun7i_a20_get_out_factors(struct factors_request *req)
  320. {
  321. u8 div, calcm, calcp;
  322. /* These clocks can only divide, so we will never be able to achieve
  323. * frequencies higher than the parent frequency */
  324. if (req->rate > req->parent_rate)
  325. req->rate = req->parent_rate;
  326. div = DIV_ROUND_UP(req->parent_rate, req->rate);
  327. if (div < 32)
  328. calcp = 0;
  329. else if (div / 2 < 32)
  330. calcp = 1;
  331. else if (div / 4 < 32)
  332. calcp = 2;
  333. else
  334. calcp = 3;
  335. calcm = DIV_ROUND_UP(div, 1 << calcp);
  336. req->rate = (req->parent_rate >> calcp) / calcm;
  337. req->m = calcm - 1;
  338. req->p = calcp;
  339. }
  340. /**
  341. * sunxi_factors_clk_setup() - Setup function for factor clocks
  342. */
  343. static const struct clk_factors_config sun4i_pll1_config = {
  344. .nshift = 8,
  345. .nwidth = 5,
  346. .kshift = 4,
  347. .kwidth = 2,
  348. .mshift = 0,
  349. .mwidth = 2,
  350. .pshift = 16,
  351. .pwidth = 2,
  352. };
  353. static const struct clk_factors_config sun6i_a31_pll1_config = {
  354. .nshift = 8,
  355. .nwidth = 5,
  356. .kshift = 4,
  357. .kwidth = 2,
  358. .mshift = 0,
  359. .mwidth = 2,
  360. .n_start = 1,
  361. };
  362. static const struct clk_factors_config sun8i_a23_pll1_config = {
  363. .nshift = 8,
  364. .nwidth = 5,
  365. .kshift = 4,
  366. .kwidth = 2,
  367. .mshift = 0,
  368. .mwidth = 2,
  369. .pshift = 16,
  370. .pwidth = 2,
  371. .n_start = 1,
  372. };
  373. static const struct clk_factors_config sun4i_pll5_config = {
  374. .nshift = 8,
  375. .nwidth = 5,
  376. .kshift = 4,
  377. .kwidth = 2,
  378. };
  379. static const struct clk_factors_config sun6i_a31_pll6_config = {
  380. .nshift = 8,
  381. .nwidth = 5,
  382. .kshift = 4,
  383. .kwidth = 2,
  384. .n_start = 1,
  385. };
  386. static const struct clk_factors_config sun5i_a13_ahb_config = {
  387. .pshift = 4,
  388. .pwidth = 2,
  389. };
  390. static const struct clk_factors_config sun6i_ahb1_config = {
  391. .mshift = 6,
  392. .mwidth = 2,
  393. .pshift = 4,
  394. .pwidth = 2,
  395. };
  396. static const struct clk_factors_config sun4i_apb1_config = {
  397. .mshift = 0,
  398. .mwidth = 5,
  399. .pshift = 16,
  400. .pwidth = 2,
  401. };
  402. /* user manual says "n" but it's really "p" */
  403. static const struct clk_factors_config sun7i_a20_out_config = {
  404. .mshift = 8,
  405. .mwidth = 5,
  406. .pshift = 20,
  407. .pwidth = 2,
  408. };
  409. static const struct factors_data sun4i_pll1_data __initconst = {
  410. .enable = 31,
  411. .table = &sun4i_pll1_config,
  412. .getter = sun4i_get_pll1_factors,
  413. };
  414. static const struct factors_data sun6i_a31_pll1_data __initconst = {
  415. .enable = 31,
  416. .table = &sun6i_a31_pll1_config,
  417. .getter = sun6i_a31_get_pll1_factors,
  418. };
  419. static const struct factors_data sun8i_a23_pll1_data __initconst = {
  420. .enable = 31,
  421. .table = &sun8i_a23_pll1_config,
  422. .getter = sun8i_a23_get_pll1_factors,
  423. };
  424. static const struct factors_data sun7i_a20_pll4_data __initconst = {
  425. .enable = 31,
  426. .table = &sun4i_pll5_config,
  427. .getter = sun4i_get_pll5_factors,
  428. };
  429. static const struct factors_data sun4i_pll5_data __initconst = {
  430. .enable = 31,
  431. .table = &sun4i_pll5_config,
  432. .getter = sun4i_get_pll5_factors,
  433. };
  434. static const struct factors_data sun6i_a31_pll6_data __initconst = {
  435. .enable = 31,
  436. .table = &sun6i_a31_pll6_config,
  437. .getter = sun6i_a31_get_pll6_factors,
  438. };
  439. static const struct factors_data sun5i_a13_ahb_data __initconst = {
  440. .mux = 6,
  441. .muxmask = BIT(1) | BIT(0),
  442. .table = &sun5i_a13_ahb_config,
  443. .getter = sun5i_a13_get_ahb_factors,
  444. };
  445. static const struct factors_data sun6i_ahb1_data __initconst = {
  446. .mux = 12,
  447. .muxmask = BIT(1) | BIT(0),
  448. .table = &sun6i_ahb1_config,
  449. .getter = sun6i_get_ahb1_factors,
  450. .recalc = sun6i_ahb1_recalc,
  451. };
  452. static const struct factors_data sun4i_apb1_data __initconst = {
  453. .mux = 24,
  454. .muxmask = BIT(1) | BIT(0),
  455. .table = &sun4i_apb1_config,
  456. .getter = sun4i_get_apb1_factors,
  457. };
  458. static const struct factors_data sun7i_a20_out_data __initconst = {
  459. .enable = 31,
  460. .mux = 24,
  461. .muxmask = BIT(1) | BIT(0),
  462. .table = &sun7i_a20_out_config,
  463. .getter = sun7i_a20_get_out_factors,
  464. };
  465. static struct clk * __init sunxi_factors_clk_setup(struct device_node *node,
  466. const struct factors_data *data)
  467. {
  468. void __iomem *reg;
  469. reg = of_iomap(node, 0);
  470. if (!reg) {
  471. pr_err("Could not get registers for factors-clk: %s\n",
  472. node->name);
  473. return NULL;
  474. }
  475. return sunxi_factors_register(node, data, &clk_lock, reg);
  476. }
  477. static void __init sun4i_pll1_clk_setup(struct device_node *node)
  478. {
  479. sunxi_factors_clk_setup(node, &sun4i_pll1_data);
  480. }
  481. CLK_OF_DECLARE(sun4i_pll1, "allwinner,sun4i-a10-pll1-clk",
  482. sun4i_pll1_clk_setup);
  483. static void __init sun6i_pll1_clk_setup(struct device_node *node)
  484. {
  485. sunxi_factors_clk_setup(node, &sun6i_a31_pll1_data);
  486. }
  487. CLK_OF_DECLARE(sun6i_pll1, "allwinner,sun6i-a31-pll1-clk",
  488. sun6i_pll1_clk_setup);
  489. static void __init sun8i_pll1_clk_setup(struct device_node *node)
  490. {
  491. sunxi_factors_clk_setup(node, &sun8i_a23_pll1_data);
  492. }
  493. CLK_OF_DECLARE(sun8i_pll1, "allwinner,sun8i-a23-pll1-clk",
  494. sun8i_pll1_clk_setup);
  495. static void __init sun7i_pll4_clk_setup(struct device_node *node)
  496. {
  497. sunxi_factors_clk_setup(node, &sun7i_a20_pll4_data);
  498. }
  499. CLK_OF_DECLARE(sun7i_pll4, "allwinner,sun7i-a20-pll4-clk",
  500. sun7i_pll4_clk_setup);
  501. static void __init sun5i_ahb_clk_setup(struct device_node *node)
  502. {
  503. sunxi_factors_clk_setup(node, &sun5i_a13_ahb_data);
  504. }
  505. CLK_OF_DECLARE(sun5i_ahb, "allwinner,sun5i-a13-ahb-clk",
  506. sun5i_ahb_clk_setup);
  507. static void __init sun6i_ahb1_clk_setup(struct device_node *node)
  508. {
  509. sunxi_factors_clk_setup(node, &sun6i_ahb1_data);
  510. }
  511. CLK_OF_DECLARE(sun6i_a31_ahb1, "allwinner,sun6i-a31-ahb1-clk",
  512. sun6i_ahb1_clk_setup);
  513. static void __init sun4i_apb1_clk_setup(struct device_node *node)
  514. {
  515. sunxi_factors_clk_setup(node, &sun4i_apb1_data);
  516. }
  517. CLK_OF_DECLARE(sun4i_apb1, "allwinner,sun4i-a10-apb1-clk",
  518. sun4i_apb1_clk_setup);
  519. static void __init sun7i_out_clk_setup(struct device_node *node)
  520. {
  521. sunxi_factors_clk_setup(node, &sun7i_a20_out_data);
  522. }
  523. CLK_OF_DECLARE(sun7i_out, "allwinner,sun7i-a20-out-clk",
  524. sun7i_out_clk_setup);
  525. /**
  526. * sunxi_mux_clk_setup() - Setup function for muxes
  527. */
  528. #define SUNXI_MUX_GATE_WIDTH 2
  529. struct mux_data {
  530. u8 shift;
  531. };
  532. static const struct mux_data sun4i_cpu_mux_data __initconst = {
  533. .shift = 16,
  534. };
  535. static const struct mux_data sun6i_a31_ahb1_mux_data __initconst = {
  536. .shift = 12,
  537. };
  538. static const struct mux_data sun8i_h3_ahb2_mux_data __initconst = {
  539. .shift = 0,
  540. };
  541. static struct clk * __init sunxi_mux_clk_setup(struct device_node *node,
  542. const struct mux_data *data)
  543. {
  544. struct clk *clk;
  545. const char *clk_name = node->name;
  546. const char *parents[SUNXI_MAX_PARENTS];
  547. void __iomem *reg;
  548. int i;
  549. reg = of_iomap(node, 0);
  550. if (!reg) {
  551. pr_err("Could not map registers for mux-clk: %s\n",
  552. of_node_full_name(node));
  553. return NULL;
  554. }
  555. i = of_clk_parent_fill(node, parents, SUNXI_MAX_PARENTS);
  556. if (of_property_read_string(node, "clock-output-names", &clk_name)) {
  557. pr_err("%s: could not read clock-output-names from \"%s\"\n",
  558. __func__, of_node_full_name(node));
  559. goto out_unmap;
  560. }
  561. clk = clk_register_mux(NULL, clk_name, parents, i,
  562. CLK_SET_RATE_PARENT, reg,
  563. data->shift, SUNXI_MUX_GATE_WIDTH,
  564. 0, &clk_lock);
  565. if (IS_ERR(clk)) {
  566. pr_err("%s: failed to register mux clock %s: %ld\n", __func__,
  567. clk_name, PTR_ERR(clk));
  568. goto out_unmap;
  569. }
  570. if (of_clk_add_provider(node, of_clk_src_simple_get, clk)) {
  571. pr_err("%s: failed to add clock provider for %s\n",
  572. __func__, clk_name);
  573. clk_unregister_divider(clk);
  574. goto out_unmap;
  575. }
  576. return clk;
  577. out_unmap:
  578. iounmap(reg);
  579. return NULL;
  580. }
  581. static void __init sun4i_cpu_clk_setup(struct device_node *node)
  582. {
  583. struct clk *clk;
  584. clk = sunxi_mux_clk_setup(node, &sun4i_cpu_mux_data);
  585. if (!clk)
  586. return;
  587. /* Protect CPU clock */
  588. __clk_get(clk);
  589. clk_prepare_enable(clk);
  590. }
  591. CLK_OF_DECLARE(sun4i_cpu, "allwinner,sun4i-a10-cpu-clk",
  592. sun4i_cpu_clk_setup);
  593. static void __init sun6i_ahb1_mux_clk_setup(struct device_node *node)
  594. {
  595. sunxi_mux_clk_setup(node, &sun6i_a31_ahb1_mux_data);
  596. }
  597. CLK_OF_DECLARE(sun6i_ahb1_mux, "allwinner,sun6i-a31-ahb1-mux-clk",
  598. sun6i_ahb1_mux_clk_setup);
  599. static void __init sun8i_ahb2_clk_setup(struct device_node *node)
  600. {
  601. sunxi_mux_clk_setup(node, &sun8i_h3_ahb2_mux_data);
  602. }
  603. CLK_OF_DECLARE(sun8i_ahb2, "allwinner,sun8i-h3-ahb2-clk",
  604. sun8i_ahb2_clk_setup);
  605. /**
  606. * sunxi_divider_clk_setup() - Setup function for simple divider clocks
  607. */
  608. struct div_data {
  609. u8 shift;
  610. u8 pow;
  611. u8 width;
  612. const struct clk_div_table *table;
  613. };
  614. static const struct div_data sun4i_axi_data __initconst = {
  615. .shift = 0,
  616. .pow = 0,
  617. .width = 2,
  618. };
  619. static const struct clk_div_table sun8i_a23_axi_table[] __initconst = {
  620. { .val = 0, .div = 1 },
  621. { .val = 1, .div = 2 },
  622. { .val = 2, .div = 3 },
  623. { .val = 3, .div = 4 },
  624. { .val = 4, .div = 4 },
  625. { .val = 5, .div = 4 },
  626. { .val = 6, .div = 4 },
  627. { .val = 7, .div = 4 },
  628. { } /* sentinel */
  629. };
  630. static const struct div_data sun8i_a23_axi_data __initconst = {
  631. .width = 3,
  632. .table = sun8i_a23_axi_table,
  633. };
  634. static const struct div_data sun4i_ahb_data __initconst = {
  635. .shift = 4,
  636. .pow = 1,
  637. .width = 2,
  638. };
  639. static const struct clk_div_table sun4i_apb0_table[] __initconst = {
  640. { .val = 0, .div = 2 },
  641. { .val = 1, .div = 2 },
  642. { .val = 2, .div = 4 },
  643. { .val = 3, .div = 8 },
  644. { } /* sentinel */
  645. };
  646. static const struct div_data sun4i_apb0_data __initconst = {
  647. .shift = 8,
  648. .pow = 1,
  649. .width = 2,
  650. .table = sun4i_apb0_table,
  651. };
  652. static void __init sunxi_divider_clk_setup(struct device_node *node,
  653. const struct div_data *data)
  654. {
  655. struct clk *clk;
  656. const char *clk_name = node->name;
  657. const char *clk_parent;
  658. void __iomem *reg;
  659. reg = of_iomap(node, 0);
  660. if (!reg) {
  661. pr_err("Could not map registers for mux-clk: %s\n",
  662. of_node_full_name(node));
  663. return;
  664. }
  665. clk_parent = of_clk_get_parent_name(node, 0);
  666. if (of_property_read_string(node, "clock-output-names", &clk_name)) {
  667. pr_err("%s: could not read clock-output-names from \"%s\"\n",
  668. __func__, of_node_full_name(node));
  669. goto out_unmap;
  670. }
  671. clk = clk_register_divider_table(NULL, clk_name, clk_parent, 0,
  672. reg, data->shift, data->width,
  673. data->pow ? CLK_DIVIDER_POWER_OF_TWO : 0,
  674. data->table, &clk_lock);
  675. if (IS_ERR(clk)) {
  676. pr_err("%s: failed to register divider clock %s: %ld\n",
  677. __func__, clk_name, PTR_ERR(clk));
  678. goto out_unmap;
  679. }
  680. if (of_clk_add_provider(node, of_clk_src_simple_get, clk)) {
  681. pr_err("%s: failed to add clock provider for %s\n",
  682. __func__, clk_name);
  683. goto out_unregister;
  684. }
  685. if (clk_register_clkdev(clk, clk_name, NULL)) {
  686. of_clk_del_provider(node);
  687. goto out_unregister;
  688. }
  689. return;
  690. out_unregister:
  691. clk_unregister_divider(clk);
  692. out_unmap:
  693. iounmap(reg);
  694. }
  695. static void __init sun4i_ahb_clk_setup(struct device_node *node)
  696. {
  697. sunxi_divider_clk_setup(node, &sun4i_ahb_data);
  698. }
  699. CLK_OF_DECLARE(sun4i_ahb, "allwinner,sun4i-a10-ahb-clk",
  700. sun4i_ahb_clk_setup);
  701. static void __init sun4i_apb0_clk_setup(struct device_node *node)
  702. {
  703. sunxi_divider_clk_setup(node, &sun4i_apb0_data);
  704. }
  705. CLK_OF_DECLARE(sun4i_apb0, "allwinner,sun4i-a10-apb0-clk",
  706. sun4i_apb0_clk_setup);
  707. static void __init sun4i_axi_clk_setup(struct device_node *node)
  708. {
  709. sunxi_divider_clk_setup(node, &sun4i_axi_data);
  710. }
  711. CLK_OF_DECLARE(sun4i_axi, "allwinner,sun4i-a10-axi-clk",
  712. sun4i_axi_clk_setup);
  713. static void __init sun8i_axi_clk_setup(struct device_node *node)
  714. {
  715. sunxi_divider_clk_setup(node, &sun8i_a23_axi_data);
  716. }
  717. CLK_OF_DECLARE(sun8i_axi, "allwinner,sun8i-a23-axi-clk",
  718. sun8i_axi_clk_setup);
  719. /**
  720. * sunxi_gates_clk_setup() - Setup function for leaf gates on clocks
  721. */
  722. #define SUNXI_GATES_MAX_SIZE 64
  723. struct gates_data {
  724. DECLARE_BITMAP(mask, SUNXI_GATES_MAX_SIZE);
  725. };
  726. /**
  727. * sunxi_divs_clk_setup() helper data
  728. */
  729. #define SUNXI_DIVS_MAX_QTY 4
  730. #define SUNXI_DIVISOR_WIDTH 2
  731. struct divs_data {
  732. const struct factors_data *factors; /* data for the factor clock */
  733. int ndivs; /* number of outputs */
  734. /*
  735. * List of outputs. Refer to the diagram for sunxi_divs_clk_setup():
  736. * self or base factor clock refers to the output from the pll
  737. * itself. The remaining refer to fixed or configurable divider
  738. * outputs.
  739. */
  740. struct {
  741. u8 self; /* is it the base factor clock? (only one) */
  742. u8 fixed; /* is it a fixed divisor? if not... */
  743. struct clk_div_table *table; /* is it a table based divisor? */
  744. u8 shift; /* otherwise it's a normal divisor with this shift */
  745. u8 pow; /* is it power-of-two based? */
  746. u8 gate; /* is it independently gateable? */
  747. } div[SUNXI_DIVS_MAX_QTY];
  748. };
  749. static struct clk_div_table pll6_sata_tbl[] = {
  750. { .val = 0, .div = 6, },
  751. { .val = 1, .div = 12, },
  752. { .val = 2, .div = 18, },
  753. { .val = 3, .div = 24, },
  754. { } /* sentinel */
  755. };
  756. static const struct divs_data pll5_divs_data __initconst = {
  757. .factors = &sun4i_pll5_data,
  758. .ndivs = 2,
  759. .div = {
  760. { .shift = 0, .pow = 0, }, /* M, DDR */
  761. { .shift = 16, .pow = 1, }, /* P, other */
  762. /* No output for the base factor clock */
  763. }
  764. };
  765. static const struct divs_data pll6_divs_data __initconst = {
  766. .factors = &sun4i_pll5_data,
  767. .ndivs = 4,
  768. .div = {
  769. { .shift = 0, .table = pll6_sata_tbl, .gate = 14 }, /* M, SATA */
  770. { .fixed = 2 }, /* P, other */
  771. { .self = 1 }, /* base factor clock, 2x */
  772. { .fixed = 4 }, /* pll6 / 4, used as ahb input */
  773. }
  774. };
  775. static const struct divs_data sun6i_a31_pll6_divs_data __initconst = {
  776. .factors = &sun6i_a31_pll6_data,
  777. .ndivs = 2,
  778. .div = {
  779. { .fixed = 2 }, /* normal output */
  780. { .self = 1 }, /* base factor clock, 2x */
  781. }
  782. };
  783. /**
  784. * sunxi_divs_clk_setup() - Setup function for leaf divisors on clocks
  785. *
  786. * These clocks look something like this
  787. * ________________________
  788. * | ___divisor 1---|----> to consumer
  789. * parent >--| pll___/___divisor 2---|----> to consumer
  790. * | \_______________|____> to consumer
  791. * |________________________|
  792. */
  793. static struct clk ** __init sunxi_divs_clk_setup(struct device_node *node,
  794. const struct divs_data *data)
  795. {
  796. struct clk_onecell_data *clk_data;
  797. const char *parent;
  798. const char *clk_name;
  799. struct clk **clks, *pclk;
  800. struct clk_hw *gate_hw, *rate_hw;
  801. const struct clk_ops *rate_ops;
  802. struct clk_gate *gate = NULL;
  803. struct clk_fixed_factor *fix_factor;
  804. struct clk_divider *divider;
  805. struct factors_data factors = *data->factors;
  806. char *derived_name = NULL;
  807. void __iomem *reg;
  808. int ndivs = SUNXI_DIVS_MAX_QTY, i = 0;
  809. int flags, clkflags;
  810. /* if number of children known, use it */
  811. if (data->ndivs)
  812. ndivs = data->ndivs;
  813. /* Try to find a name for base factor clock */
  814. for (i = 0; i < ndivs; i++) {
  815. if (data->div[i].self) {
  816. of_property_read_string_index(node, "clock-output-names",
  817. i, &factors.name);
  818. break;
  819. }
  820. }
  821. /* If we don't have a .self clk use the first output-name up to '_' */
  822. if (factors.name == NULL) {
  823. char *endp;
  824. of_property_read_string_index(node, "clock-output-names",
  825. 0, &clk_name);
  826. endp = strchr(clk_name, '_');
  827. if (endp) {
  828. derived_name = kstrndup(clk_name, endp - clk_name,
  829. GFP_KERNEL);
  830. factors.name = derived_name;
  831. } else {
  832. factors.name = clk_name;
  833. }
  834. }
  835. /* Set up factor clock that we will be dividing */
  836. pclk = sunxi_factors_clk_setup(node, &factors);
  837. if (!pclk)
  838. return NULL;
  839. parent = __clk_get_name(pclk);
  840. kfree(derived_name);
  841. reg = of_iomap(node, 0);
  842. if (!reg) {
  843. pr_err("Could not map registers for divs-clk: %s\n",
  844. of_node_full_name(node));
  845. return NULL;
  846. }
  847. clk_data = kmalloc(sizeof(struct clk_onecell_data), GFP_KERNEL);
  848. if (!clk_data)
  849. goto out_unmap;
  850. clks = kcalloc(ndivs, sizeof(*clks), GFP_KERNEL);
  851. if (!clks)
  852. goto free_clkdata;
  853. clk_data->clks = clks;
  854. /* It's not a good idea to have automatic reparenting changing
  855. * our RAM clock! */
  856. clkflags = !strcmp("pll5", parent) ? 0 : CLK_SET_RATE_PARENT;
  857. for (i = 0; i < ndivs; i++) {
  858. if (of_property_read_string_index(node, "clock-output-names",
  859. i, &clk_name) != 0)
  860. break;
  861. /* If this is the base factor clock, only update clks */
  862. if (data->div[i].self) {
  863. clk_data->clks[i] = pclk;
  864. continue;
  865. }
  866. gate_hw = NULL;
  867. rate_hw = NULL;
  868. rate_ops = NULL;
  869. /* If this leaf clock can be gated, create a gate */
  870. if (data->div[i].gate) {
  871. gate = kzalloc(sizeof(*gate), GFP_KERNEL);
  872. if (!gate)
  873. goto free_clks;
  874. gate->reg = reg;
  875. gate->bit_idx = data->div[i].gate;
  876. gate->lock = &clk_lock;
  877. gate_hw = &gate->hw;
  878. }
  879. /* Leaves can be fixed or configurable divisors */
  880. if (data->div[i].fixed) {
  881. fix_factor = kzalloc(sizeof(*fix_factor), GFP_KERNEL);
  882. if (!fix_factor)
  883. goto free_gate;
  884. fix_factor->mult = 1;
  885. fix_factor->div = data->div[i].fixed;
  886. rate_hw = &fix_factor->hw;
  887. rate_ops = &clk_fixed_factor_ops;
  888. } else {
  889. divider = kzalloc(sizeof(*divider), GFP_KERNEL);
  890. if (!divider)
  891. goto free_gate;
  892. flags = data->div[i].pow ? CLK_DIVIDER_POWER_OF_TWO : 0;
  893. divider->reg = reg;
  894. divider->shift = data->div[i].shift;
  895. divider->width = SUNXI_DIVISOR_WIDTH;
  896. divider->flags = flags;
  897. divider->lock = &clk_lock;
  898. divider->table = data->div[i].table;
  899. rate_hw = &divider->hw;
  900. rate_ops = &clk_divider_ops;
  901. }
  902. /* Wrap the (potential) gate and the divisor on a composite
  903. * clock to unify them */
  904. clks[i] = clk_register_composite(NULL, clk_name, &parent, 1,
  905. NULL, NULL,
  906. rate_hw, rate_ops,
  907. gate_hw, &clk_gate_ops,
  908. clkflags);
  909. WARN_ON(IS_ERR(clk_data->clks[i]));
  910. }
  911. /* Adjust to the real max */
  912. clk_data->clk_num = i;
  913. if (of_clk_add_provider(node, of_clk_src_onecell_get, clk_data)) {
  914. pr_err("%s: failed to add clock provider for %s\n",
  915. __func__, clk_name);
  916. goto free_gate;
  917. }
  918. return clks;
  919. free_gate:
  920. kfree(gate);
  921. free_clks:
  922. kfree(clks);
  923. free_clkdata:
  924. kfree(clk_data);
  925. out_unmap:
  926. iounmap(reg);
  927. return NULL;
  928. }
  929. static void __init sun4i_pll5_clk_setup(struct device_node *node)
  930. {
  931. struct clk **clks;
  932. clks = sunxi_divs_clk_setup(node, &pll5_divs_data);
  933. if (!clks)
  934. return;
  935. /* Protect PLL5_DDR */
  936. __clk_get(clks[0]);
  937. clk_prepare_enable(clks[0]);
  938. }
  939. CLK_OF_DECLARE(sun4i_pll5, "allwinner,sun4i-a10-pll5-clk",
  940. sun4i_pll5_clk_setup);
  941. static void __init sun4i_pll6_clk_setup(struct device_node *node)
  942. {
  943. sunxi_divs_clk_setup(node, &pll6_divs_data);
  944. }
  945. CLK_OF_DECLARE(sun4i_pll6, "allwinner,sun4i-a10-pll6-clk",
  946. sun4i_pll6_clk_setup);
  947. static void __init sun6i_pll6_clk_setup(struct device_node *node)
  948. {
  949. sunxi_divs_clk_setup(node, &sun6i_a31_pll6_divs_data);
  950. }
  951. CLK_OF_DECLARE(sun6i_pll6, "allwinner,sun6i-a31-pll6-clk",
  952. sun6i_pll6_clk_setup);
  953. /*
  954. * sun6i display
  955. *
  956. * rate = parent_rate / (m + 1);
  957. */
  958. static void sun6i_display_factors(struct factors_request *req)
  959. {
  960. u8 m;
  961. if (req->rate > req->parent_rate)
  962. req->rate = req->parent_rate;
  963. m = DIV_ROUND_UP(req->parent_rate, req->rate);
  964. req->rate = req->parent_rate / m;
  965. req->m = m - 1;
  966. }
  967. static const struct clk_factors_config sun6i_display_config = {
  968. .mshift = 0,
  969. .mwidth = 4,
  970. };
  971. static const struct factors_data sun6i_display_data __initconst = {
  972. .enable = 31,
  973. .mux = 24,
  974. .muxmask = BIT(2) | BIT(1) | BIT(0),
  975. .table = &sun6i_display_config,
  976. .getter = sun6i_display_factors,
  977. };
  978. static void __init sun6i_display_setup(struct device_node *node)
  979. {
  980. sunxi_factors_clk_setup(node, &sun6i_display_data);
  981. }
  982. CLK_OF_DECLARE(sun6i_display, "allwinner,sun6i-a31-display-clk",
  983. sun6i_display_setup);