clk-sunxi.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203
  1. /*
  2. * Copyright 2013 Emilio López
  3. *
  4. * Emilio López <emilio@elopez.com.ar>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #include <linux/clk-provider.h>
  17. #include <linux/clkdev.h>
  18. #include <linux/of.h>
  19. #include <linux/of_address.h>
  20. #include <linux/reset-controller.h>
  21. #include <linux/spinlock.h>
  22. #include "clk-factors.h"
  23. static DEFINE_SPINLOCK(clk_lock);
  24. /* Maximum number of parents our clocks have */
  25. #define SUNXI_MAX_PARENTS 5
  26. /**
  27. * sun4i_get_pll1_factors() - calculates n, k, m, p factors for PLL1
  28. * PLL1 rate is calculated as follows
  29. * rate = (parent_rate * n * (k + 1) >> p) / (m + 1);
  30. * parent_rate is always 24Mhz
  31. */
  32. static void sun4i_get_pll1_factors(u32 *freq, u32 parent_rate,
  33. u8 *n, u8 *k, u8 *m, u8 *p)
  34. {
  35. u8 div;
  36. /* Normalize value to a 6M multiple */
  37. div = *freq / 6000000;
  38. *freq = 6000000 * div;
  39. /* we were called to round the frequency, we can now return */
  40. if (n == NULL)
  41. return;
  42. /* m is always zero for pll1 */
  43. *m = 0;
  44. /* k is 1 only on these cases */
  45. if (*freq >= 768000000 || *freq == 42000000 || *freq == 54000000)
  46. *k = 1;
  47. else
  48. *k = 0;
  49. /* p will be 3 for divs under 10 */
  50. if (div < 10)
  51. *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. *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. *p = 1;
  59. /* any other entries have p = 0 */
  60. else
  61. *p = 0;
  62. /* calculate a suitable n based on k and p */
  63. div <<= *p;
  64. div /= (*k + 1);
  65. *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(u32 *freq, u32 parent_rate,
  74. u8 *n, u8 *k, u8 *m, u8 *p)
  75. {
  76. /*
  77. * We can operate only on MHz, this will make our life easier
  78. * later.
  79. */
  80. u32 freq_mhz = *freq / 1000000;
  81. u32 parent_freq_mhz = parent_rate / 1000000;
  82. /*
  83. * Round down the frequency to the closest multiple of either
  84. * 6 or 16
  85. */
  86. u32 round_freq_6 = round_down(freq_mhz, 6);
  87. u32 round_freq_16 = round_down(freq_mhz, 16);
  88. if (round_freq_6 > round_freq_16)
  89. freq_mhz = round_freq_6;
  90. else
  91. freq_mhz = round_freq_16;
  92. *freq = freq_mhz * 1000000;
  93. /*
  94. * If the factors pointer are null, we were just called to
  95. * round down the frequency.
  96. * Exit.
  97. */
  98. if (n == NULL)
  99. return;
  100. /* If the frequency is a multiple of 32 MHz, k is always 3 */
  101. if (!(freq_mhz % 32))
  102. *k = 3;
  103. /* If the frequency is a multiple of 9 MHz, k is always 2 */
  104. else if (!(freq_mhz % 9))
  105. *k = 2;
  106. /* If the frequency is a multiple of 8 MHz, k is always 1 */
  107. else if (!(freq_mhz % 8))
  108. *k = 1;
  109. /* Otherwise, we don't use the k factor */
  110. else
  111. *k = 0;
  112. /*
  113. * If the frequency is a multiple of 2 but not a multiple of
  114. * 3, m is 3. This is the first time we use 6 here, yet we
  115. * will use it on several other places.
  116. * We use this number because it's the lowest frequency we can
  117. * generate (with n = 0, k = 0, m = 3), so every other frequency
  118. * somehow relates to this frequency.
  119. */
  120. if ((freq_mhz % 6) == 2 || (freq_mhz % 6) == 4)
  121. *m = 2;
  122. /*
  123. * If the frequency is a multiple of 6MHz, but the factor is
  124. * odd, m will be 3
  125. */
  126. else if ((freq_mhz / 6) & 1)
  127. *m = 3;
  128. /* Otherwise, we end up with m = 1 */
  129. else
  130. *m = 1;
  131. /* Calculate n thanks to the above factors we already got */
  132. *n = freq_mhz * (*m + 1) / ((*k + 1) * parent_freq_mhz) - 1;
  133. /*
  134. * If n end up being outbound, and that we can still decrease
  135. * m, do it.
  136. */
  137. if ((*n + 1) > 31 && (*m + 1) > 1) {
  138. *n = (*n + 1) / 2 - 1;
  139. *m = (*m + 1) / 2 - 1;
  140. }
  141. }
  142. /**
  143. * sun8i_a23_get_pll1_factors() - calculates n, k, m, p factors for PLL1
  144. * PLL1 rate is calculated as follows
  145. * rate = (parent_rate * (n + 1) * (k + 1) >> p) / (m + 1);
  146. * parent_rate is always 24Mhz
  147. */
  148. static void sun8i_a23_get_pll1_factors(u32 *freq, u32 parent_rate,
  149. u8 *n, u8 *k, u8 *m, u8 *p)
  150. {
  151. u8 div;
  152. /* Normalize value to a 6M multiple */
  153. div = *freq / 6000000;
  154. *freq = 6000000 * div;
  155. /* we were called to round the frequency, we can now return */
  156. if (n == NULL)
  157. return;
  158. /* m is always zero for pll1 */
  159. *m = 0;
  160. /* k is 1 only on these cases */
  161. if (*freq >= 768000000 || *freq == 42000000 || *freq == 54000000)
  162. *k = 1;
  163. else
  164. *k = 0;
  165. /* p will be 2 for divs under 20 and odd divs under 32 */
  166. if (div < 20 || (div < 32 && (div & 1)))
  167. *p = 2;
  168. /* p will be 1 for even divs under 32, divs under 40 and odd pairs
  169. * of divs between 40-62 */
  170. else if (div < 40 || (div < 64 && (div & 2)))
  171. *p = 1;
  172. /* any other entries have p = 0 */
  173. else
  174. *p = 0;
  175. /* calculate a suitable n based on k and p */
  176. div <<= *p;
  177. div /= (*k + 1);
  178. *n = div / 4 - 1;
  179. }
  180. /**
  181. * sun4i_get_pll5_factors() - calculates n, k factors for PLL5
  182. * PLL5 rate is calculated as follows
  183. * rate = parent_rate * n * (k + 1)
  184. * parent_rate is always 24Mhz
  185. */
  186. static void sun4i_get_pll5_factors(u32 *freq, u32 parent_rate,
  187. u8 *n, u8 *k, u8 *m, u8 *p)
  188. {
  189. u8 div;
  190. /* Normalize value to a parent_rate multiple (24M) */
  191. div = *freq / parent_rate;
  192. *freq = parent_rate * div;
  193. /* we were called to round the frequency, we can now return */
  194. if (n == NULL)
  195. return;
  196. if (div < 31)
  197. *k = 0;
  198. else if (div / 2 < 31)
  199. *k = 1;
  200. else if (div / 3 < 31)
  201. *k = 2;
  202. else
  203. *k = 3;
  204. *n = DIV_ROUND_UP(div, (*k+1));
  205. }
  206. /**
  207. * sun6i_a31_get_pll6_factors() - calculates n, k factors for A31 PLL6
  208. * PLL6 rate is calculated as follows
  209. * rate = parent_rate * n * (k + 1) / 2
  210. * parent_rate is always 24Mhz
  211. */
  212. static void sun6i_a31_get_pll6_factors(u32 *freq, u32 parent_rate,
  213. u8 *n, u8 *k, u8 *m, u8 *p)
  214. {
  215. u8 div;
  216. /*
  217. * We always have 24MHz / 2, so we can just say that our
  218. * parent clock is 12MHz.
  219. */
  220. parent_rate = parent_rate / 2;
  221. /* Normalize value to a parent_rate multiple (24M / 2) */
  222. div = *freq / parent_rate;
  223. *freq = parent_rate * div;
  224. /* we were called to round the frequency, we can now return */
  225. if (n == NULL)
  226. return;
  227. *k = div / 32;
  228. if (*k > 3)
  229. *k = 3;
  230. *n = DIV_ROUND_UP(div, (*k+1));
  231. }
  232. /**
  233. * sun4i_get_apb1_factors() - calculates m, p factors for APB1
  234. * APB1 rate is calculated as follows
  235. * rate = (parent_rate >> p) / (m + 1);
  236. */
  237. static void sun4i_get_apb1_factors(u32 *freq, u32 parent_rate,
  238. u8 *n, u8 *k, u8 *m, u8 *p)
  239. {
  240. u8 calcm, calcp;
  241. if (parent_rate < *freq)
  242. *freq = parent_rate;
  243. parent_rate = DIV_ROUND_UP(parent_rate, *freq);
  244. /* Invalid rate! */
  245. if (parent_rate > 32)
  246. return;
  247. if (parent_rate <= 4)
  248. calcp = 0;
  249. else if (parent_rate <= 8)
  250. calcp = 1;
  251. else if (parent_rate <= 16)
  252. calcp = 2;
  253. else
  254. calcp = 3;
  255. calcm = (parent_rate >> calcp) - 1;
  256. *freq = (parent_rate >> calcp) / (calcm + 1);
  257. /* we were called to round the frequency, we can now return */
  258. if (n == NULL)
  259. return;
  260. *m = calcm;
  261. *p = calcp;
  262. }
  263. /**
  264. * sun7i_a20_get_out_factors() - calculates m, p factors for CLK_OUT_A/B
  265. * CLK_OUT rate is calculated as follows
  266. * rate = (parent_rate >> p) / (m + 1);
  267. */
  268. static void sun7i_a20_get_out_factors(u32 *freq, u32 parent_rate,
  269. u8 *n, u8 *k, u8 *m, u8 *p)
  270. {
  271. u8 div, calcm, calcp;
  272. /* These clocks can only divide, so we will never be able to achieve
  273. * frequencies higher than the parent frequency */
  274. if (*freq > parent_rate)
  275. *freq = parent_rate;
  276. div = DIV_ROUND_UP(parent_rate, *freq);
  277. if (div < 32)
  278. calcp = 0;
  279. else if (div / 2 < 32)
  280. calcp = 1;
  281. else if (div / 4 < 32)
  282. calcp = 2;
  283. else
  284. calcp = 3;
  285. calcm = DIV_ROUND_UP(div, 1 << calcp);
  286. *freq = (parent_rate >> calcp) / calcm;
  287. /* we were called to round the frequency, we can now return */
  288. if (n == NULL)
  289. return;
  290. *m = calcm - 1;
  291. *p = calcp;
  292. }
  293. /**
  294. * clk_sunxi_mmc_phase_control() - configures MMC clock phase control
  295. */
  296. void clk_sunxi_mmc_phase_control(struct clk *clk, u8 sample, u8 output)
  297. {
  298. #define to_clk_composite(_hw) container_of(_hw, struct clk_composite, hw)
  299. #define to_clk_factors(_hw) container_of(_hw, struct clk_factors, hw)
  300. struct clk_hw *hw = __clk_get_hw(clk);
  301. struct clk_composite *composite = to_clk_composite(hw);
  302. struct clk_hw *rate_hw = composite->rate_hw;
  303. struct clk_factors *factors = to_clk_factors(rate_hw);
  304. unsigned long flags = 0;
  305. u32 reg;
  306. if (factors->lock)
  307. spin_lock_irqsave(factors->lock, flags);
  308. reg = readl(factors->reg);
  309. /* set sample clock phase control */
  310. reg &= ~(0x7 << 20);
  311. reg |= ((sample & 0x7) << 20);
  312. /* set output clock phase control */
  313. reg &= ~(0x7 << 8);
  314. reg |= ((output & 0x7) << 8);
  315. writel(reg, factors->reg);
  316. if (factors->lock)
  317. spin_unlock_irqrestore(factors->lock, flags);
  318. }
  319. EXPORT_SYMBOL(clk_sunxi_mmc_phase_control);
  320. /**
  321. * sunxi_factors_clk_setup() - Setup function for factor clocks
  322. */
  323. static struct clk_factors_config sun4i_pll1_config = {
  324. .nshift = 8,
  325. .nwidth = 5,
  326. .kshift = 4,
  327. .kwidth = 2,
  328. .mshift = 0,
  329. .mwidth = 2,
  330. .pshift = 16,
  331. .pwidth = 2,
  332. };
  333. static struct clk_factors_config sun6i_a31_pll1_config = {
  334. .nshift = 8,
  335. .nwidth = 5,
  336. .kshift = 4,
  337. .kwidth = 2,
  338. .mshift = 0,
  339. .mwidth = 2,
  340. .n_start = 1,
  341. };
  342. static struct clk_factors_config sun8i_a23_pll1_config = {
  343. .nshift = 8,
  344. .nwidth = 5,
  345. .kshift = 4,
  346. .kwidth = 2,
  347. .mshift = 0,
  348. .mwidth = 2,
  349. .pshift = 16,
  350. .pwidth = 2,
  351. .n_start = 1,
  352. };
  353. static struct clk_factors_config sun4i_pll5_config = {
  354. .nshift = 8,
  355. .nwidth = 5,
  356. .kshift = 4,
  357. .kwidth = 2,
  358. };
  359. static struct clk_factors_config sun6i_a31_pll6_config = {
  360. .nshift = 8,
  361. .nwidth = 5,
  362. .kshift = 4,
  363. .kwidth = 2,
  364. };
  365. static struct clk_factors_config sun4i_apb1_config = {
  366. .mshift = 0,
  367. .mwidth = 5,
  368. .pshift = 16,
  369. .pwidth = 2,
  370. };
  371. /* user manual says "n" but it's really "p" */
  372. static struct clk_factors_config sun7i_a20_out_config = {
  373. .mshift = 8,
  374. .mwidth = 5,
  375. .pshift = 20,
  376. .pwidth = 2,
  377. };
  378. static const struct factors_data sun4i_pll1_data __initconst = {
  379. .enable = 31,
  380. .table = &sun4i_pll1_config,
  381. .getter = sun4i_get_pll1_factors,
  382. };
  383. static const struct factors_data sun6i_a31_pll1_data __initconst = {
  384. .enable = 31,
  385. .table = &sun6i_a31_pll1_config,
  386. .getter = sun6i_a31_get_pll1_factors,
  387. };
  388. static const struct factors_data sun8i_a23_pll1_data __initconst = {
  389. .enable = 31,
  390. .table = &sun8i_a23_pll1_config,
  391. .getter = sun8i_a23_get_pll1_factors,
  392. };
  393. static const struct factors_data sun7i_a20_pll4_data __initconst = {
  394. .enable = 31,
  395. .table = &sun4i_pll5_config,
  396. .getter = sun4i_get_pll5_factors,
  397. };
  398. static const struct factors_data sun4i_pll5_data __initconst = {
  399. .enable = 31,
  400. .table = &sun4i_pll5_config,
  401. .getter = sun4i_get_pll5_factors,
  402. .name = "pll5",
  403. };
  404. static const struct factors_data sun4i_pll6_data __initconst = {
  405. .enable = 31,
  406. .table = &sun4i_pll5_config,
  407. .getter = sun4i_get_pll5_factors,
  408. .name = "pll6",
  409. };
  410. static const struct factors_data sun6i_a31_pll6_data __initconst = {
  411. .enable = 31,
  412. .table = &sun6i_a31_pll6_config,
  413. .getter = sun6i_a31_get_pll6_factors,
  414. };
  415. static const struct factors_data sun4i_apb1_data __initconst = {
  416. .table = &sun4i_apb1_config,
  417. .getter = sun4i_get_apb1_factors,
  418. };
  419. static const struct factors_data sun7i_a20_out_data __initconst = {
  420. .enable = 31,
  421. .mux = 24,
  422. .table = &sun7i_a20_out_config,
  423. .getter = sun7i_a20_get_out_factors,
  424. };
  425. static struct clk * __init sunxi_factors_clk_setup(struct device_node *node,
  426. const struct factors_data *data)
  427. {
  428. return sunxi_factors_register(node, data, &clk_lock);
  429. }
  430. /**
  431. * sunxi_mux_clk_setup() - Setup function for muxes
  432. */
  433. #define SUNXI_MUX_GATE_WIDTH 2
  434. struct mux_data {
  435. u8 shift;
  436. };
  437. static const struct mux_data sun4i_cpu_mux_data __initconst = {
  438. .shift = 16,
  439. };
  440. static const struct mux_data sun6i_a31_ahb1_mux_data __initconst = {
  441. .shift = 12,
  442. };
  443. static const struct mux_data sun4i_apb1_mux_data __initconst = {
  444. .shift = 24,
  445. };
  446. static void __init sunxi_mux_clk_setup(struct device_node *node,
  447. struct mux_data *data)
  448. {
  449. struct clk *clk;
  450. const char *clk_name = node->name;
  451. const char *parents[SUNXI_MAX_PARENTS];
  452. void __iomem *reg;
  453. int i = 0;
  454. reg = of_iomap(node, 0);
  455. while (i < SUNXI_MAX_PARENTS &&
  456. (parents[i] = of_clk_get_parent_name(node, i)) != NULL)
  457. i++;
  458. of_property_read_string(node, "clock-output-names", &clk_name);
  459. clk = clk_register_mux(NULL, clk_name, parents, i,
  460. CLK_SET_RATE_NO_REPARENT, reg,
  461. data->shift, SUNXI_MUX_GATE_WIDTH,
  462. 0, &clk_lock);
  463. if (clk) {
  464. of_clk_add_provider(node, of_clk_src_simple_get, clk);
  465. clk_register_clkdev(clk, clk_name, NULL);
  466. }
  467. }
  468. /**
  469. * sunxi_divider_clk_setup() - Setup function for simple divider clocks
  470. */
  471. struct div_data {
  472. u8 shift;
  473. u8 pow;
  474. u8 width;
  475. const struct clk_div_table *table;
  476. };
  477. static const struct div_data sun4i_axi_data __initconst = {
  478. .shift = 0,
  479. .pow = 0,
  480. .width = 2,
  481. };
  482. static const struct clk_div_table sun8i_a23_axi_table[] __initconst = {
  483. { .val = 0, .div = 1 },
  484. { .val = 1, .div = 2 },
  485. { .val = 2, .div = 3 },
  486. { .val = 3, .div = 4 },
  487. { .val = 4, .div = 4 },
  488. { .val = 5, .div = 4 },
  489. { .val = 6, .div = 4 },
  490. { .val = 7, .div = 4 },
  491. { } /* sentinel */
  492. };
  493. static const struct div_data sun8i_a23_axi_data __initconst = {
  494. .width = 3,
  495. .table = sun8i_a23_axi_table,
  496. };
  497. static const struct div_data sun4i_ahb_data __initconst = {
  498. .shift = 4,
  499. .pow = 1,
  500. .width = 2,
  501. };
  502. static const struct clk_div_table sun4i_apb0_table[] __initconst = {
  503. { .val = 0, .div = 2 },
  504. { .val = 1, .div = 2 },
  505. { .val = 2, .div = 4 },
  506. { .val = 3, .div = 8 },
  507. { } /* sentinel */
  508. };
  509. static const struct div_data sun4i_apb0_data __initconst = {
  510. .shift = 8,
  511. .pow = 1,
  512. .width = 2,
  513. .table = sun4i_apb0_table,
  514. };
  515. static const struct div_data sun6i_a31_apb2_div_data __initconst = {
  516. .shift = 0,
  517. .pow = 0,
  518. .width = 4,
  519. };
  520. static void __init sunxi_divider_clk_setup(struct device_node *node,
  521. struct div_data *data)
  522. {
  523. struct clk *clk;
  524. const char *clk_name = node->name;
  525. const char *clk_parent;
  526. void __iomem *reg;
  527. reg = of_iomap(node, 0);
  528. clk_parent = of_clk_get_parent_name(node, 0);
  529. of_property_read_string(node, "clock-output-names", &clk_name);
  530. clk = clk_register_divider_table(NULL, clk_name, clk_parent, 0,
  531. reg, data->shift, data->width,
  532. data->pow ? CLK_DIVIDER_POWER_OF_TWO : 0,
  533. data->table, &clk_lock);
  534. if (clk) {
  535. of_clk_add_provider(node, of_clk_src_simple_get, clk);
  536. clk_register_clkdev(clk, clk_name, NULL);
  537. }
  538. }
  539. /**
  540. * sunxi_gates_reset... - reset bits in leaf gate clk registers handling
  541. */
  542. struct gates_reset_data {
  543. void __iomem *reg;
  544. spinlock_t *lock;
  545. struct reset_controller_dev rcdev;
  546. };
  547. static int sunxi_gates_reset_assert(struct reset_controller_dev *rcdev,
  548. unsigned long id)
  549. {
  550. struct gates_reset_data *data = container_of(rcdev,
  551. struct gates_reset_data,
  552. rcdev);
  553. unsigned long flags;
  554. u32 reg;
  555. spin_lock_irqsave(data->lock, flags);
  556. reg = readl(data->reg);
  557. writel(reg & ~BIT(id), data->reg);
  558. spin_unlock_irqrestore(data->lock, flags);
  559. return 0;
  560. }
  561. static int sunxi_gates_reset_deassert(struct reset_controller_dev *rcdev,
  562. unsigned long id)
  563. {
  564. struct gates_reset_data *data = container_of(rcdev,
  565. struct gates_reset_data,
  566. rcdev);
  567. unsigned long flags;
  568. u32 reg;
  569. spin_lock_irqsave(data->lock, flags);
  570. reg = readl(data->reg);
  571. writel(reg | BIT(id), data->reg);
  572. spin_unlock_irqrestore(data->lock, flags);
  573. return 0;
  574. }
  575. static struct reset_control_ops sunxi_gates_reset_ops = {
  576. .assert = sunxi_gates_reset_assert,
  577. .deassert = sunxi_gates_reset_deassert,
  578. };
  579. /**
  580. * sunxi_gates_clk_setup() - Setup function for leaf gates on clocks
  581. */
  582. #define SUNXI_GATES_MAX_SIZE 64
  583. struct gates_data {
  584. DECLARE_BITMAP(mask, SUNXI_GATES_MAX_SIZE);
  585. u32 reset_mask;
  586. };
  587. static const struct gates_data sun4i_axi_gates_data __initconst = {
  588. .mask = {1},
  589. };
  590. static const struct gates_data sun4i_ahb_gates_data __initconst = {
  591. .mask = {0x7F77FFF, 0x14FB3F},
  592. };
  593. static const struct gates_data sun5i_a10s_ahb_gates_data __initconst = {
  594. .mask = {0x147667e7, 0x185915},
  595. };
  596. static const struct gates_data sun5i_a13_ahb_gates_data __initconst = {
  597. .mask = {0x107067e7, 0x185111},
  598. };
  599. static const struct gates_data sun6i_a31_ahb1_gates_data __initconst = {
  600. .mask = {0xEDFE7F62, 0x794F931},
  601. };
  602. static const struct gates_data sun7i_a20_ahb_gates_data __initconst = {
  603. .mask = { 0x12f77fff, 0x16ff3f },
  604. };
  605. static const struct gates_data sun8i_a23_ahb1_gates_data __initconst = {
  606. .mask = {0x25386742, 0x2505111},
  607. };
  608. static const struct gates_data sun4i_apb0_gates_data __initconst = {
  609. .mask = {0x4EF},
  610. };
  611. static const struct gates_data sun5i_a10s_apb0_gates_data __initconst = {
  612. .mask = {0x469},
  613. };
  614. static const struct gates_data sun5i_a13_apb0_gates_data __initconst = {
  615. .mask = {0x61},
  616. };
  617. static const struct gates_data sun7i_a20_apb0_gates_data __initconst = {
  618. .mask = { 0x4ff },
  619. };
  620. static const struct gates_data sun4i_apb1_gates_data __initconst = {
  621. .mask = {0xFF00F7},
  622. };
  623. static const struct gates_data sun5i_a10s_apb1_gates_data __initconst = {
  624. .mask = {0xf0007},
  625. };
  626. static const struct gates_data sun5i_a13_apb1_gates_data __initconst = {
  627. .mask = {0xa0007},
  628. };
  629. static const struct gates_data sun6i_a31_apb1_gates_data __initconst = {
  630. .mask = {0x3031},
  631. };
  632. static const struct gates_data sun8i_a23_apb1_gates_data __initconst = {
  633. .mask = {0x3021},
  634. };
  635. static const struct gates_data sun6i_a31_apb2_gates_data __initconst = {
  636. .mask = {0x3F000F},
  637. };
  638. static const struct gates_data sun7i_a20_apb1_gates_data __initconst = {
  639. .mask = { 0xff80ff },
  640. };
  641. static const struct gates_data sun8i_a23_apb2_gates_data __initconst = {
  642. .mask = {0x1F0007},
  643. };
  644. static const struct gates_data sun4i_a10_usb_gates_data __initconst = {
  645. .mask = {0x1C0},
  646. .reset_mask = 0x07,
  647. };
  648. static const struct gates_data sun5i_a13_usb_gates_data __initconst = {
  649. .mask = {0x140},
  650. .reset_mask = 0x03,
  651. };
  652. static const struct gates_data sun6i_a31_usb_gates_data __initconst = {
  653. .mask = { BIT(18) | BIT(17) | BIT(16) | BIT(10) | BIT(9) | BIT(8) },
  654. .reset_mask = BIT(2) | BIT(1) | BIT(0),
  655. };
  656. static void __init sunxi_gates_clk_setup(struct device_node *node,
  657. struct gates_data *data)
  658. {
  659. struct clk_onecell_data *clk_data;
  660. struct gates_reset_data *reset_data;
  661. const char *clk_parent;
  662. const char *clk_name;
  663. void __iomem *reg;
  664. int qty;
  665. int i = 0;
  666. int j = 0;
  667. reg = of_iomap(node, 0);
  668. clk_parent = of_clk_get_parent_name(node, 0);
  669. /* Worst-case size approximation and memory allocation */
  670. qty = find_last_bit(data->mask, SUNXI_GATES_MAX_SIZE);
  671. clk_data = kmalloc(sizeof(struct clk_onecell_data), GFP_KERNEL);
  672. if (!clk_data)
  673. return;
  674. clk_data->clks = kzalloc((qty+1) * sizeof(struct clk *), GFP_KERNEL);
  675. if (!clk_data->clks) {
  676. kfree(clk_data);
  677. return;
  678. }
  679. for_each_set_bit(i, data->mask, SUNXI_GATES_MAX_SIZE) {
  680. of_property_read_string_index(node, "clock-output-names",
  681. j, &clk_name);
  682. clk_data->clks[i] = clk_register_gate(NULL, clk_name,
  683. clk_parent, 0,
  684. reg + 4 * (i/32), i % 32,
  685. 0, &clk_lock);
  686. WARN_ON(IS_ERR(clk_data->clks[i]));
  687. clk_register_clkdev(clk_data->clks[i], clk_name, NULL);
  688. j++;
  689. }
  690. /* Adjust to the real max */
  691. clk_data->clk_num = i;
  692. of_clk_add_provider(node, of_clk_src_onecell_get, clk_data);
  693. /* Register a reset controler for gates with reset bits */
  694. if (data->reset_mask == 0)
  695. return;
  696. reset_data = kzalloc(sizeof(*reset_data), GFP_KERNEL);
  697. if (!reset_data)
  698. return;
  699. reset_data->reg = reg;
  700. reset_data->lock = &clk_lock;
  701. reset_data->rcdev.nr_resets = __fls(data->reset_mask) + 1;
  702. reset_data->rcdev.ops = &sunxi_gates_reset_ops;
  703. reset_data->rcdev.of_node = node;
  704. reset_controller_register(&reset_data->rcdev);
  705. }
  706. /**
  707. * sunxi_divs_clk_setup() helper data
  708. */
  709. #define SUNXI_DIVS_MAX_QTY 2
  710. #define SUNXI_DIVISOR_WIDTH 2
  711. struct divs_data {
  712. const struct factors_data *factors; /* data for the factor clock */
  713. struct {
  714. u8 fixed; /* is it a fixed divisor? if not... */
  715. struct clk_div_table *table; /* is it a table based divisor? */
  716. u8 shift; /* otherwise it's a normal divisor with this shift */
  717. u8 pow; /* is it power-of-two based? */
  718. u8 gate; /* is it independently gateable? */
  719. } div[SUNXI_DIVS_MAX_QTY];
  720. };
  721. static struct clk_div_table pll6_sata_tbl[] = {
  722. { .val = 0, .div = 6, },
  723. { .val = 1, .div = 12, },
  724. { .val = 2, .div = 18, },
  725. { .val = 3, .div = 24, },
  726. { } /* sentinel */
  727. };
  728. static const struct divs_data pll5_divs_data __initconst = {
  729. .factors = &sun4i_pll5_data,
  730. .div = {
  731. { .shift = 0, .pow = 0, }, /* M, DDR */
  732. { .shift = 16, .pow = 1, }, /* P, other */
  733. }
  734. };
  735. static const struct divs_data pll6_divs_data __initconst = {
  736. .factors = &sun4i_pll6_data,
  737. .div = {
  738. { .shift = 0, .table = pll6_sata_tbl, .gate = 14 }, /* M, SATA */
  739. { .fixed = 2 }, /* P, other */
  740. }
  741. };
  742. /**
  743. * sunxi_divs_clk_setup() - Setup function for leaf divisors on clocks
  744. *
  745. * These clocks look something like this
  746. * ________________________
  747. * | ___divisor 1---|----> to consumer
  748. * parent >--| pll___/___divisor 2---|----> to consumer
  749. * | \_______________|____> to consumer
  750. * |________________________|
  751. */
  752. static void __init sunxi_divs_clk_setup(struct device_node *node,
  753. struct divs_data *data)
  754. {
  755. struct clk_onecell_data *clk_data;
  756. const char *parent;
  757. const char *clk_name;
  758. struct clk **clks, *pclk;
  759. struct clk_hw *gate_hw, *rate_hw;
  760. const struct clk_ops *rate_ops;
  761. struct clk_gate *gate = NULL;
  762. struct clk_fixed_factor *fix_factor;
  763. struct clk_divider *divider;
  764. void __iomem *reg;
  765. int i = 0;
  766. int flags, clkflags;
  767. /* Set up factor clock that we will be dividing */
  768. pclk = sunxi_factors_clk_setup(node, data->factors);
  769. parent = __clk_get_name(pclk);
  770. reg = of_iomap(node, 0);
  771. clk_data = kmalloc(sizeof(struct clk_onecell_data), GFP_KERNEL);
  772. if (!clk_data)
  773. return;
  774. clks = kzalloc((SUNXI_DIVS_MAX_QTY+1) * sizeof(*clks), GFP_KERNEL);
  775. if (!clks)
  776. goto free_clkdata;
  777. clk_data->clks = clks;
  778. /* It's not a good idea to have automatic reparenting changing
  779. * our RAM clock! */
  780. clkflags = !strcmp("pll5", parent) ? 0 : CLK_SET_RATE_PARENT;
  781. for (i = 0; i < SUNXI_DIVS_MAX_QTY; i++) {
  782. if (of_property_read_string_index(node, "clock-output-names",
  783. i, &clk_name) != 0)
  784. break;
  785. gate_hw = NULL;
  786. rate_hw = NULL;
  787. rate_ops = NULL;
  788. /* If this leaf clock can be gated, create a gate */
  789. if (data->div[i].gate) {
  790. gate = kzalloc(sizeof(*gate), GFP_KERNEL);
  791. if (!gate)
  792. goto free_clks;
  793. gate->reg = reg;
  794. gate->bit_idx = data->div[i].gate;
  795. gate->lock = &clk_lock;
  796. gate_hw = &gate->hw;
  797. }
  798. /* Leaves can be fixed or configurable divisors */
  799. if (data->div[i].fixed) {
  800. fix_factor = kzalloc(sizeof(*fix_factor), GFP_KERNEL);
  801. if (!fix_factor)
  802. goto free_gate;
  803. fix_factor->mult = 1;
  804. fix_factor->div = data->div[i].fixed;
  805. rate_hw = &fix_factor->hw;
  806. rate_ops = &clk_fixed_factor_ops;
  807. } else {
  808. divider = kzalloc(sizeof(*divider), GFP_KERNEL);
  809. if (!divider)
  810. goto free_gate;
  811. flags = data->div[i].pow ? CLK_DIVIDER_POWER_OF_TWO : 0;
  812. divider->reg = reg;
  813. divider->shift = data->div[i].shift;
  814. divider->width = SUNXI_DIVISOR_WIDTH;
  815. divider->flags = flags;
  816. divider->lock = &clk_lock;
  817. divider->table = data->div[i].table;
  818. rate_hw = &divider->hw;
  819. rate_ops = &clk_divider_ops;
  820. }
  821. /* Wrap the (potential) gate and the divisor on a composite
  822. * clock to unify them */
  823. clks[i] = clk_register_composite(NULL, clk_name, &parent, 1,
  824. NULL, NULL,
  825. rate_hw, rate_ops,
  826. gate_hw, &clk_gate_ops,
  827. clkflags);
  828. WARN_ON(IS_ERR(clk_data->clks[i]));
  829. clk_register_clkdev(clks[i], clk_name, NULL);
  830. }
  831. /* The last clock available on the getter is the parent */
  832. clks[i++] = pclk;
  833. /* Adjust to the real max */
  834. clk_data->clk_num = i;
  835. of_clk_add_provider(node, of_clk_src_onecell_get, clk_data);
  836. return;
  837. free_gate:
  838. kfree(gate);
  839. free_clks:
  840. kfree(clks);
  841. free_clkdata:
  842. kfree(clk_data);
  843. }
  844. /* Matches for factors clocks */
  845. static const struct of_device_id clk_factors_match[] __initconst = {
  846. {.compatible = "allwinner,sun4i-a10-pll1-clk", .data = &sun4i_pll1_data,},
  847. {.compatible = "allwinner,sun6i-a31-pll1-clk", .data = &sun6i_a31_pll1_data,},
  848. {.compatible = "allwinner,sun8i-a23-pll1-clk", .data = &sun8i_a23_pll1_data,},
  849. {.compatible = "allwinner,sun7i-a20-pll4-clk", .data = &sun7i_a20_pll4_data,},
  850. {.compatible = "allwinner,sun6i-a31-pll6-clk", .data = &sun6i_a31_pll6_data,},
  851. {.compatible = "allwinner,sun4i-a10-apb1-clk", .data = &sun4i_apb1_data,},
  852. {.compatible = "allwinner,sun7i-a20-out-clk", .data = &sun7i_a20_out_data,},
  853. {}
  854. };
  855. /* Matches for divider clocks */
  856. static const struct of_device_id clk_div_match[] __initconst = {
  857. {.compatible = "allwinner,sun4i-a10-axi-clk", .data = &sun4i_axi_data,},
  858. {.compatible = "allwinner,sun8i-a23-axi-clk", .data = &sun8i_a23_axi_data,},
  859. {.compatible = "allwinner,sun4i-a10-ahb-clk", .data = &sun4i_ahb_data,},
  860. {.compatible = "allwinner,sun4i-a10-apb0-clk", .data = &sun4i_apb0_data,},
  861. {.compatible = "allwinner,sun6i-a31-apb2-div-clk", .data = &sun6i_a31_apb2_div_data,},
  862. {}
  863. };
  864. /* Matches for divided outputs */
  865. static const struct of_device_id clk_divs_match[] __initconst = {
  866. {.compatible = "allwinner,sun4i-a10-pll5-clk", .data = &pll5_divs_data,},
  867. {.compatible = "allwinner,sun4i-a10-pll6-clk", .data = &pll6_divs_data,},
  868. {}
  869. };
  870. /* Matches for mux clocks */
  871. static const struct of_device_id clk_mux_match[] __initconst = {
  872. {.compatible = "allwinner,sun4i-a10-cpu-clk", .data = &sun4i_cpu_mux_data,},
  873. {.compatible = "allwinner,sun4i-a10-apb1-mux-clk", .data = &sun4i_apb1_mux_data,},
  874. {.compatible = "allwinner,sun6i-a31-ahb1-mux-clk", .data = &sun6i_a31_ahb1_mux_data,},
  875. {}
  876. };
  877. /* Matches for gate clocks */
  878. static const struct of_device_id clk_gates_match[] __initconst = {
  879. {.compatible = "allwinner,sun4i-a10-axi-gates-clk", .data = &sun4i_axi_gates_data,},
  880. {.compatible = "allwinner,sun4i-a10-ahb-gates-clk", .data = &sun4i_ahb_gates_data,},
  881. {.compatible = "allwinner,sun5i-a10s-ahb-gates-clk", .data = &sun5i_a10s_ahb_gates_data,},
  882. {.compatible = "allwinner,sun5i-a13-ahb-gates-clk", .data = &sun5i_a13_ahb_gates_data,},
  883. {.compatible = "allwinner,sun6i-a31-ahb1-gates-clk", .data = &sun6i_a31_ahb1_gates_data,},
  884. {.compatible = "allwinner,sun7i-a20-ahb-gates-clk", .data = &sun7i_a20_ahb_gates_data,},
  885. {.compatible = "allwinner,sun8i-a23-ahb1-gates-clk", .data = &sun8i_a23_ahb1_gates_data,},
  886. {.compatible = "allwinner,sun4i-a10-apb0-gates-clk", .data = &sun4i_apb0_gates_data,},
  887. {.compatible = "allwinner,sun5i-a10s-apb0-gates-clk", .data = &sun5i_a10s_apb0_gates_data,},
  888. {.compatible = "allwinner,sun5i-a13-apb0-gates-clk", .data = &sun5i_a13_apb0_gates_data,},
  889. {.compatible = "allwinner,sun7i-a20-apb0-gates-clk", .data = &sun7i_a20_apb0_gates_data,},
  890. {.compatible = "allwinner,sun4i-a10-apb1-gates-clk", .data = &sun4i_apb1_gates_data,},
  891. {.compatible = "allwinner,sun5i-a10s-apb1-gates-clk", .data = &sun5i_a10s_apb1_gates_data,},
  892. {.compatible = "allwinner,sun5i-a13-apb1-gates-clk", .data = &sun5i_a13_apb1_gates_data,},
  893. {.compatible = "allwinner,sun6i-a31-apb1-gates-clk", .data = &sun6i_a31_apb1_gates_data,},
  894. {.compatible = "allwinner,sun7i-a20-apb1-gates-clk", .data = &sun7i_a20_apb1_gates_data,},
  895. {.compatible = "allwinner,sun8i-a23-apb1-gates-clk", .data = &sun8i_a23_apb1_gates_data,},
  896. {.compatible = "allwinner,sun6i-a31-apb2-gates-clk", .data = &sun6i_a31_apb2_gates_data,},
  897. {.compatible = "allwinner,sun8i-a23-apb2-gates-clk", .data = &sun8i_a23_apb2_gates_data,},
  898. {.compatible = "allwinner,sun4i-a10-usb-clk", .data = &sun4i_a10_usb_gates_data,},
  899. {.compatible = "allwinner,sun5i-a13-usb-clk", .data = &sun5i_a13_usb_gates_data,},
  900. {.compatible = "allwinner,sun6i-a31-usb-clk", .data = &sun6i_a31_usb_gates_data,},
  901. {}
  902. };
  903. static void __init of_sunxi_table_clock_setup(const struct of_device_id *clk_match,
  904. void *function)
  905. {
  906. struct device_node *np;
  907. const struct div_data *data;
  908. const struct of_device_id *match;
  909. void (*setup_function)(struct device_node *, const void *) = function;
  910. for_each_matching_node_and_match(np, clk_match, &match) {
  911. data = match->data;
  912. setup_function(np, data);
  913. }
  914. }
  915. static void __init sunxi_init_clocks(const char *clocks[], int nclocks)
  916. {
  917. unsigned int i;
  918. /* Register factor clocks */
  919. of_sunxi_table_clock_setup(clk_factors_match, sunxi_factors_clk_setup);
  920. /* Register divider clocks */
  921. of_sunxi_table_clock_setup(clk_div_match, sunxi_divider_clk_setup);
  922. /* Register divided output clocks */
  923. of_sunxi_table_clock_setup(clk_divs_match, sunxi_divs_clk_setup);
  924. /* Register mux clocks */
  925. of_sunxi_table_clock_setup(clk_mux_match, sunxi_mux_clk_setup);
  926. /* Register gate clocks */
  927. of_sunxi_table_clock_setup(clk_gates_match, sunxi_gates_clk_setup);
  928. /* Protect the clocks that needs to stay on */
  929. for (i = 0; i < nclocks; i++) {
  930. struct clk *clk = clk_get(NULL, clocks[i]);
  931. if (!IS_ERR(clk))
  932. clk_prepare_enable(clk);
  933. }
  934. }
  935. static const char *sun4i_a10_critical_clocks[] __initdata = {
  936. "pll5_ddr",
  937. "ahb_sdram",
  938. };
  939. static void __init sun4i_a10_init_clocks(struct device_node *node)
  940. {
  941. sunxi_init_clocks(sun4i_a10_critical_clocks,
  942. ARRAY_SIZE(sun4i_a10_critical_clocks));
  943. }
  944. CLK_OF_DECLARE(sun4i_a10_clk_init, "allwinner,sun4i-a10", sun4i_a10_init_clocks);
  945. static const char *sun5i_critical_clocks[] __initdata = {
  946. "pll5_ddr",
  947. "ahb_sdram",
  948. };
  949. static void __init sun5i_init_clocks(struct device_node *node)
  950. {
  951. sunxi_init_clocks(sun5i_critical_clocks,
  952. ARRAY_SIZE(sun5i_critical_clocks));
  953. }
  954. CLK_OF_DECLARE(sun5i_a10s_clk_init, "allwinner,sun5i-a10s", sun5i_init_clocks);
  955. CLK_OF_DECLARE(sun5i_a13_clk_init, "allwinner,sun5i-a13", sun5i_init_clocks);
  956. CLK_OF_DECLARE(sun7i_a20_clk_init, "allwinner,sun7i-a20", sun5i_init_clocks);
  957. static const char *sun6i_critical_clocks[] __initdata = {
  958. "cpu",
  959. "ahb1_sdram",
  960. };
  961. static void __init sun6i_init_clocks(struct device_node *node)
  962. {
  963. sunxi_init_clocks(sun6i_critical_clocks,
  964. ARRAY_SIZE(sun6i_critical_clocks));
  965. }
  966. CLK_OF_DECLARE(sun6i_a31_clk_init, "allwinner,sun6i-a31", sun6i_init_clocks);
  967. CLK_OF_DECLARE(sun8i_a23_clk_init, "allwinner,sun8i-a23", sun6i_init_clocks);