clk-sunxi.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173
  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. unsigned long flags)
  544. {
  545. struct clk *clk;
  546. const char *clk_name = node->name;
  547. const char *parents[SUNXI_MAX_PARENTS];
  548. void __iomem *reg;
  549. int i;
  550. reg = of_iomap(node, 0);
  551. if (!reg) {
  552. pr_err("Could not map registers for mux-clk: %pOF\n", 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 \"%pOF\"\n",
  558. __func__, node);
  559. goto out_unmap;
  560. }
  561. clk = clk_register_mux(NULL, clk_name, parents, i,
  562. CLK_SET_RATE_PARENT | flags, 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. /* Protect CPU clock */
  584. sunxi_mux_clk_setup(node, &sun4i_cpu_mux_data, CLK_IS_CRITICAL);
  585. }
  586. CLK_OF_DECLARE(sun4i_cpu, "allwinner,sun4i-a10-cpu-clk",
  587. sun4i_cpu_clk_setup);
  588. static void __init sun6i_ahb1_mux_clk_setup(struct device_node *node)
  589. {
  590. sunxi_mux_clk_setup(node, &sun6i_a31_ahb1_mux_data, 0);
  591. }
  592. CLK_OF_DECLARE(sun6i_ahb1_mux, "allwinner,sun6i-a31-ahb1-mux-clk",
  593. sun6i_ahb1_mux_clk_setup);
  594. static void __init sun8i_ahb2_clk_setup(struct device_node *node)
  595. {
  596. sunxi_mux_clk_setup(node, &sun8i_h3_ahb2_mux_data, 0);
  597. }
  598. CLK_OF_DECLARE(sun8i_ahb2, "allwinner,sun8i-h3-ahb2-clk",
  599. sun8i_ahb2_clk_setup);
  600. /**
  601. * sunxi_divider_clk_setup() - Setup function for simple divider clocks
  602. */
  603. struct div_data {
  604. u8 shift;
  605. u8 pow;
  606. u8 width;
  607. const struct clk_div_table *table;
  608. };
  609. static const struct div_data sun4i_axi_data __initconst = {
  610. .shift = 0,
  611. .pow = 0,
  612. .width = 2,
  613. };
  614. static const struct clk_div_table sun8i_a23_axi_table[] __initconst = {
  615. { .val = 0, .div = 1 },
  616. { .val = 1, .div = 2 },
  617. { .val = 2, .div = 3 },
  618. { .val = 3, .div = 4 },
  619. { .val = 4, .div = 4 },
  620. { .val = 5, .div = 4 },
  621. { .val = 6, .div = 4 },
  622. { .val = 7, .div = 4 },
  623. { } /* sentinel */
  624. };
  625. static const struct div_data sun8i_a23_axi_data __initconst = {
  626. .width = 3,
  627. .table = sun8i_a23_axi_table,
  628. };
  629. static const struct div_data sun4i_ahb_data __initconst = {
  630. .shift = 4,
  631. .pow = 1,
  632. .width = 2,
  633. };
  634. static const struct clk_div_table sun4i_apb0_table[] __initconst = {
  635. { .val = 0, .div = 2 },
  636. { .val = 1, .div = 2 },
  637. { .val = 2, .div = 4 },
  638. { .val = 3, .div = 8 },
  639. { } /* sentinel */
  640. };
  641. static const struct div_data sun4i_apb0_data __initconst = {
  642. .shift = 8,
  643. .pow = 1,
  644. .width = 2,
  645. .table = sun4i_apb0_table,
  646. };
  647. static void __init sunxi_divider_clk_setup(struct device_node *node,
  648. const struct div_data *data)
  649. {
  650. struct clk *clk;
  651. const char *clk_name = node->name;
  652. const char *clk_parent;
  653. void __iomem *reg;
  654. reg = of_iomap(node, 0);
  655. if (!reg) {
  656. pr_err("Could not map registers for mux-clk: %pOF\n", node);
  657. return;
  658. }
  659. clk_parent = of_clk_get_parent_name(node, 0);
  660. if (of_property_read_string(node, "clock-output-names", &clk_name)) {
  661. pr_err("%s: could not read clock-output-names from \"%pOF\"\n",
  662. __func__, node);
  663. goto out_unmap;
  664. }
  665. clk = clk_register_divider_table(NULL, clk_name, clk_parent, 0,
  666. reg, data->shift, data->width,
  667. data->pow ? CLK_DIVIDER_POWER_OF_TWO : 0,
  668. data->table, &clk_lock);
  669. if (IS_ERR(clk)) {
  670. pr_err("%s: failed to register divider clock %s: %ld\n",
  671. __func__, clk_name, PTR_ERR(clk));
  672. goto out_unmap;
  673. }
  674. if (of_clk_add_provider(node, of_clk_src_simple_get, clk)) {
  675. pr_err("%s: failed to add clock provider for %s\n",
  676. __func__, clk_name);
  677. goto out_unregister;
  678. }
  679. if (clk_register_clkdev(clk, clk_name, NULL)) {
  680. of_clk_del_provider(node);
  681. goto out_unregister;
  682. }
  683. return;
  684. out_unregister:
  685. clk_unregister_divider(clk);
  686. out_unmap:
  687. iounmap(reg);
  688. }
  689. static void __init sun4i_ahb_clk_setup(struct device_node *node)
  690. {
  691. sunxi_divider_clk_setup(node, &sun4i_ahb_data);
  692. }
  693. CLK_OF_DECLARE(sun4i_ahb, "allwinner,sun4i-a10-ahb-clk",
  694. sun4i_ahb_clk_setup);
  695. static void __init sun4i_apb0_clk_setup(struct device_node *node)
  696. {
  697. sunxi_divider_clk_setup(node, &sun4i_apb0_data);
  698. }
  699. CLK_OF_DECLARE(sun4i_apb0, "allwinner,sun4i-a10-apb0-clk",
  700. sun4i_apb0_clk_setup);
  701. static void __init sun4i_axi_clk_setup(struct device_node *node)
  702. {
  703. sunxi_divider_clk_setup(node, &sun4i_axi_data);
  704. }
  705. CLK_OF_DECLARE(sun4i_axi, "allwinner,sun4i-a10-axi-clk",
  706. sun4i_axi_clk_setup);
  707. static void __init sun8i_axi_clk_setup(struct device_node *node)
  708. {
  709. sunxi_divider_clk_setup(node, &sun8i_a23_axi_data);
  710. }
  711. CLK_OF_DECLARE(sun8i_axi, "allwinner,sun8i-a23-axi-clk",
  712. sun8i_axi_clk_setup);
  713. /**
  714. * sunxi_gates_clk_setup() - Setup function for leaf gates on clocks
  715. */
  716. #define SUNXI_GATES_MAX_SIZE 64
  717. struct gates_data {
  718. DECLARE_BITMAP(mask, SUNXI_GATES_MAX_SIZE);
  719. };
  720. /**
  721. * sunxi_divs_clk_setup() helper data
  722. */
  723. #define SUNXI_DIVS_MAX_QTY 4
  724. #define SUNXI_DIVISOR_WIDTH 2
  725. struct divs_data {
  726. const struct factors_data *factors; /* data for the factor clock */
  727. int ndivs; /* number of outputs */
  728. /*
  729. * List of outputs. Refer to the diagram for sunxi_divs_clk_setup():
  730. * self or base factor clock refers to the output from the pll
  731. * itself. The remaining refer to fixed or configurable divider
  732. * outputs.
  733. */
  734. struct {
  735. u8 self; /* is it the base factor clock? (only one) */
  736. u8 fixed; /* is it a fixed divisor? if not... */
  737. struct clk_div_table *table; /* is it a table based divisor? */
  738. u8 shift; /* otherwise it's a normal divisor with this shift */
  739. u8 pow; /* is it power-of-two based? */
  740. u8 gate; /* is it independently gateable? */
  741. bool critical;
  742. } div[SUNXI_DIVS_MAX_QTY];
  743. };
  744. static struct clk_div_table pll6_sata_tbl[] = {
  745. { .val = 0, .div = 6, },
  746. { .val = 1, .div = 12, },
  747. { .val = 2, .div = 18, },
  748. { .val = 3, .div = 24, },
  749. { } /* sentinel */
  750. };
  751. static const struct divs_data pll5_divs_data __initconst = {
  752. .factors = &sun4i_pll5_data,
  753. .ndivs = 2,
  754. .div = {
  755. /* Protect PLL5_DDR */
  756. { .shift = 0, .pow = 0, .critical = true }, /* M, DDR */
  757. { .shift = 16, .pow = 1, }, /* P, other */
  758. /* No output for the base factor clock */
  759. }
  760. };
  761. static const struct divs_data pll6_divs_data __initconst = {
  762. .factors = &sun4i_pll5_data,
  763. .ndivs = 4,
  764. .div = {
  765. { .shift = 0, .table = pll6_sata_tbl, .gate = 14 }, /* M, SATA */
  766. { .fixed = 2 }, /* P, other */
  767. { .self = 1 }, /* base factor clock, 2x */
  768. { .fixed = 4 }, /* pll6 / 4, used as ahb input */
  769. }
  770. };
  771. static const struct divs_data sun6i_a31_pll6_divs_data __initconst = {
  772. .factors = &sun6i_a31_pll6_data,
  773. .ndivs = 2,
  774. .div = {
  775. { .fixed = 2 }, /* normal output */
  776. { .self = 1 }, /* base factor clock, 2x */
  777. }
  778. };
  779. /**
  780. * sunxi_divs_clk_setup() - Setup function for leaf divisors on clocks
  781. *
  782. * These clocks look something like this
  783. * ________________________
  784. * | ___divisor 1---|----> to consumer
  785. * parent >--| pll___/___divisor 2---|----> to consumer
  786. * | \_______________|____> to consumer
  787. * |________________________|
  788. */
  789. static struct clk ** __init sunxi_divs_clk_setup(struct device_node *node,
  790. const struct divs_data *data)
  791. {
  792. struct clk_onecell_data *clk_data;
  793. const char *parent;
  794. const char *clk_name;
  795. struct clk **clks, *pclk;
  796. struct clk_hw *gate_hw, *rate_hw;
  797. const struct clk_ops *rate_ops;
  798. struct clk_gate *gate = NULL;
  799. struct clk_fixed_factor *fix_factor;
  800. struct clk_divider *divider;
  801. struct factors_data factors = *data->factors;
  802. char *derived_name = NULL;
  803. void __iomem *reg;
  804. int ndivs = SUNXI_DIVS_MAX_QTY, i = 0;
  805. int flags, clkflags;
  806. /* if number of children known, use it */
  807. if (data->ndivs)
  808. ndivs = data->ndivs;
  809. /* Try to find a name for base factor clock */
  810. for (i = 0; i < ndivs; i++) {
  811. if (data->div[i].self) {
  812. of_property_read_string_index(node, "clock-output-names",
  813. i, &factors.name);
  814. break;
  815. }
  816. }
  817. /* If we don't have a .self clk use the first output-name up to '_' */
  818. if (factors.name == NULL) {
  819. char *endp;
  820. of_property_read_string_index(node, "clock-output-names",
  821. 0, &clk_name);
  822. endp = strchr(clk_name, '_');
  823. if (endp) {
  824. derived_name = kstrndup(clk_name, endp - clk_name,
  825. GFP_KERNEL);
  826. factors.name = derived_name;
  827. } else {
  828. factors.name = clk_name;
  829. }
  830. }
  831. /* Set up factor clock that we will be dividing */
  832. pclk = sunxi_factors_clk_setup(node, &factors);
  833. if (!pclk)
  834. return NULL;
  835. parent = __clk_get_name(pclk);
  836. kfree(derived_name);
  837. reg = of_iomap(node, 0);
  838. if (!reg) {
  839. pr_err("Could not map registers for divs-clk: %pOF\n", node);
  840. return NULL;
  841. }
  842. clk_data = kmalloc(sizeof(struct clk_onecell_data), GFP_KERNEL);
  843. if (!clk_data)
  844. goto out_unmap;
  845. clks = kcalloc(ndivs, sizeof(*clks), GFP_KERNEL);
  846. if (!clks)
  847. goto free_clkdata;
  848. clk_data->clks = clks;
  849. /* It's not a good idea to have automatic reparenting changing
  850. * our RAM clock! */
  851. clkflags = !strcmp("pll5", parent) ? 0 : CLK_SET_RATE_PARENT;
  852. for (i = 0; i < ndivs; i++) {
  853. if (of_property_read_string_index(node, "clock-output-names",
  854. i, &clk_name) != 0)
  855. break;
  856. /* If this is the base factor clock, only update clks */
  857. if (data->div[i].self) {
  858. clk_data->clks[i] = pclk;
  859. continue;
  860. }
  861. gate_hw = NULL;
  862. rate_hw = NULL;
  863. rate_ops = NULL;
  864. /* If this leaf clock can be gated, create a gate */
  865. if (data->div[i].gate) {
  866. gate = kzalloc(sizeof(*gate), GFP_KERNEL);
  867. if (!gate)
  868. goto free_clks;
  869. gate->reg = reg;
  870. gate->bit_idx = data->div[i].gate;
  871. gate->lock = &clk_lock;
  872. gate_hw = &gate->hw;
  873. }
  874. /* Leaves can be fixed or configurable divisors */
  875. if (data->div[i].fixed) {
  876. fix_factor = kzalloc(sizeof(*fix_factor), GFP_KERNEL);
  877. if (!fix_factor)
  878. goto free_gate;
  879. fix_factor->mult = 1;
  880. fix_factor->div = data->div[i].fixed;
  881. rate_hw = &fix_factor->hw;
  882. rate_ops = &clk_fixed_factor_ops;
  883. } else {
  884. divider = kzalloc(sizeof(*divider), GFP_KERNEL);
  885. if (!divider)
  886. goto free_gate;
  887. flags = data->div[i].pow ? CLK_DIVIDER_POWER_OF_TWO : 0;
  888. divider->reg = reg;
  889. divider->shift = data->div[i].shift;
  890. divider->width = SUNXI_DIVISOR_WIDTH;
  891. divider->flags = flags;
  892. divider->lock = &clk_lock;
  893. divider->table = data->div[i].table;
  894. rate_hw = &divider->hw;
  895. rate_ops = &clk_divider_ops;
  896. }
  897. /* Wrap the (potential) gate and the divisor on a composite
  898. * clock to unify them */
  899. clks[i] = clk_register_composite(NULL, clk_name, &parent, 1,
  900. NULL, NULL,
  901. rate_hw, rate_ops,
  902. gate_hw, &clk_gate_ops,
  903. clkflags |
  904. data->div[i].critical ?
  905. CLK_IS_CRITICAL : 0);
  906. WARN_ON(IS_ERR(clk_data->clks[i]));
  907. }
  908. /* Adjust to the real max */
  909. clk_data->clk_num = i;
  910. if (of_clk_add_provider(node, of_clk_src_onecell_get, clk_data)) {
  911. pr_err("%s: failed to add clock provider for %s\n",
  912. __func__, clk_name);
  913. goto free_gate;
  914. }
  915. return clks;
  916. free_gate:
  917. kfree(gate);
  918. free_clks:
  919. kfree(clks);
  920. free_clkdata:
  921. kfree(clk_data);
  922. out_unmap:
  923. iounmap(reg);
  924. return NULL;
  925. }
  926. static void __init sun4i_pll5_clk_setup(struct device_node *node)
  927. {
  928. sunxi_divs_clk_setup(node, &pll5_divs_data);
  929. }
  930. CLK_OF_DECLARE(sun4i_pll5, "allwinner,sun4i-a10-pll5-clk",
  931. sun4i_pll5_clk_setup);
  932. static void __init sun4i_pll6_clk_setup(struct device_node *node)
  933. {
  934. sunxi_divs_clk_setup(node, &pll6_divs_data);
  935. }
  936. CLK_OF_DECLARE(sun4i_pll6, "allwinner,sun4i-a10-pll6-clk",
  937. sun4i_pll6_clk_setup);
  938. static void __init sun6i_pll6_clk_setup(struct device_node *node)
  939. {
  940. sunxi_divs_clk_setup(node, &sun6i_a31_pll6_divs_data);
  941. }
  942. CLK_OF_DECLARE(sun6i_pll6, "allwinner,sun6i-a31-pll6-clk",
  943. sun6i_pll6_clk_setup);
  944. /*
  945. * sun6i display
  946. *
  947. * rate = parent_rate / (m + 1);
  948. */
  949. static void sun6i_display_factors(struct factors_request *req)
  950. {
  951. u8 m;
  952. if (req->rate > req->parent_rate)
  953. req->rate = req->parent_rate;
  954. m = DIV_ROUND_UP(req->parent_rate, req->rate);
  955. req->rate = req->parent_rate / m;
  956. req->m = m - 1;
  957. }
  958. static const struct clk_factors_config sun6i_display_config = {
  959. .mshift = 0,
  960. .mwidth = 4,
  961. };
  962. static const struct factors_data sun6i_display_data __initconst = {
  963. .enable = 31,
  964. .mux = 24,
  965. .muxmask = BIT(2) | BIT(1) | BIT(0),
  966. .table = &sun6i_display_config,
  967. .getter = sun6i_display_factors,
  968. };
  969. static void __init sun6i_display_setup(struct device_node *node)
  970. {
  971. sunxi_factors_clk_setup(node, &sun6i_display_data);
  972. }
  973. CLK_OF_DECLARE(sun6i_display, "allwinner,sun6i-a31-display-clk",
  974. sun6i_display_setup);