clk-sunxi.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202
  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. };
  341. static struct clk_factors_config sun8i_a23_pll1_config = {
  342. .nshift = 8,
  343. .nwidth = 5,
  344. .kshift = 4,
  345. .kwidth = 2,
  346. .mshift = 0,
  347. .mwidth = 2,
  348. .pshift = 16,
  349. .pwidth = 2,
  350. .n_start = 1,
  351. };
  352. static struct clk_factors_config sun4i_pll5_config = {
  353. .nshift = 8,
  354. .nwidth = 5,
  355. .kshift = 4,
  356. .kwidth = 2,
  357. };
  358. static struct clk_factors_config sun6i_a31_pll6_config = {
  359. .nshift = 8,
  360. .nwidth = 5,
  361. .kshift = 4,
  362. .kwidth = 2,
  363. };
  364. static struct clk_factors_config sun4i_apb1_config = {
  365. .mshift = 0,
  366. .mwidth = 5,
  367. .pshift = 16,
  368. .pwidth = 2,
  369. };
  370. /* user manual says "n" but it's really "p" */
  371. static struct clk_factors_config sun7i_a20_out_config = {
  372. .mshift = 8,
  373. .mwidth = 5,
  374. .pshift = 20,
  375. .pwidth = 2,
  376. };
  377. static const struct factors_data sun4i_pll1_data __initconst = {
  378. .enable = 31,
  379. .table = &sun4i_pll1_config,
  380. .getter = sun4i_get_pll1_factors,
  381. };
  382. static const struct factors_data sun6i_a31_pll1_data __initconst = {
  383. .enable = 31,
  384. .table = &sun6i_a31_pll1_config,
  385. .getter = sun6i_a31_get_pll1_factors,
  386. };
  387. static const struct factors_data sun8i_a23_pll1_data __initconst = {
  388. .enable = 31,
  389. .table = &sun8i_a23_pll1_config,
  390. .getter = sun8i_a23_get_pll1_factors,
  391. };
  392. static const struct factors_data sun7i_a20_pll4_data __initconst = {
  393. .enable = 31,
  394. .table = &sun4i_pll5_config,
  395. .getter = sun4i_get_pll5_factors,
  396. };
  397. static const struct factors_data sun4i_pll5_data __initconst = {
  398. .enable = 31,
  399. .table = &sun4i_pll5_config,
  400. .getter = sun4i_get_pll5_factors,
  401. .name = "pll5",
  402. };
  403. static const struct factors_data sun4i_pll6_data __initconst = {
  404. .enable = 31,
  405. .table = &sun4i_pll5_config,
  406. .getter = sun4i_get_pll5_factors,
  407. .name = "pll6",
  408. };
  409. static const struct factors_data sun6i_a31_pll6_data __initconst = {
  410. .enable = 31,
  411. .table = &sun6i_a31_pll6_config,
  412. .getter = sun6i_a31_get_pll6_factors,
  413. };
  414. static const struct factors_data sun4i_apb1_data __initconst = {
  415. .table = &sun4i_apb1_config,
  416. .getter = sun4i_get_apb1_factors,
  417. };
  418. static const struct factors_data sun7i_a20_out_data __initconst = {
  419. .enable = 31,
  420. .mux = 24,
  421. .table = &sun7i_a20_out_config,
  422. .getter = sun7i_a20_get_out_factors,
  423. };
  424. static struct clk * __init sunxi_factors_clk_setup(struct device_node *node,
  425. const struct factors_data *data)
  426. {
  427. return sunxi_factors_register(node, data, &clk_lock);
  428. }
  429. /**
  430. * sunxi_mux_clk_setup() - Setup function for muxes
  431. */
  432. #define SUNXI_MUX_GATE_WIDTH 2
  433. struct mux_data {
  434. u8 shift;
  435. };
  436. static const struct mux_data sun4i_cpu_mux_data __initconst = {
  437. .shift = 16,
  438. };
  439. static const struct mux_data sun6i_a31_ahb1_mux_data __initconst = {
  440. .shift = 12,
  441. };
  442. static const struct mux_data sun4i_apb1_mux_data __initconst = {
  443. .shift = 24,
  444. };
  445. static void __init sunxi_mux_clk_setup(struct device_node *node,
  446. struct mux_data *data)
  447. {
  448. struct clk *clk;
  449. const char *clk_name = node->name;
  450. const char *parents[SUNXI_MAX_PARENTS];
  451. void __iomem *reg;
  452. int i = 0;
  453. reg = of_iomap(node, 0);
  454. while (i < SUNXI_MAX_PARENTS &&
  455. (parents[i] = of_clk_get_parent_name(node, i)) != NULL)
  456. i++;
  457. of_property_read_string(node, "clock-output-names", &clk_name);
  458. clk = clk_register_mux(NULL, clk_name, parents, i,
  459. CLK_SET_RATE_NO_REPARENT, reg,
  460. data->shift, SUNXI_MUX_GATE_WIDTH,
  461. 0, &clk_lock);
  462. if (clk) {
  463. of_clk_add_provider(node, of_clk_src_simple_get, clk);
  464. clk_register_clkdev(clk, clk_name, NULL);
  465. }
  466. }
  467. /**
  468. * sunxi_divider_clk_setup() - Setup function for simple divider clocks
  469. */
  470. struct div_data {
  471. u8 shift;
  472. u8 pow;
  473. u8 width;
  474. const struct clk_div_table *table;
  475. };
  476. static const struct div_data sun4i_axi_data __initconst = {
  477. .shift = 0,
  478. .pow = 0,
  479. .width = 2,
  480. };
  481. static const struct clk_div_table sun8i_a23_axi_table[] __initconst = {
  482. { .val = 0, .div = 1 },
  483. { .val = 1, .div = 2 },
  484. { .val = 2, .div = 3 },
  485. { .val = 3, .div = 4 },
  486. { .val = 4, .div = 4 },
  487. { .val = 5, .div = 4 },
  488. { .val = 6, .div = 4 },
  489. { .val = 7, .div = 4 },
  490. { } /* sentinel */
  491. };
  492. static const struct div_data sun8i_a23_axi_data __initconst = {
  493. .width = 3,
  494. .table = sun8i_a23_axi_table,
  495. };
  496. static const struct div_data sun4i_ahb_data __initconst = {
  497. .shift = 4,
  498. .pow = 1,
  499. .width = 2,
  500. };
  501. static const struct clk_div_table sun4i_apb0_table[] __initconst = {
  502. { .val = 0, .div = 2 },
  503. { .val = 1, .div = 2 },
  504. { .val = 2, .div = 4 },
  505. { .val = 3, .div = 8 },
  506. { } /* sentinel */
  507. };
  508. static const struct div_data sun4i_apb0_data __initconst = {
  509. .shift = 8,
  510. .pow = 1,
  511. .width = 2,
  512. .table = sun4i_apb0_table,
  513. };
  514. static const struct div_data sun6i_a31_apb2_div_data __initconst = {
  515. .shift = 0,
  516. .pow = 0,
  517. .width = 4,
  518. };
  519. static void __init sunxi_divider_clk_setup(struct device_node *node,
  520. struct div_data *data)
  521. {
  522. struct clk *clk;
  523. const char *clk_name = node->name;
  524. const char *clk_parent;
  525. void __iomem *reg;
  526. reg = of_iomap(node, 0);
  527. clk_parent = of_clk_get_parent_name(node, 0);
  528. of_property_read_string(node, "clock-output-names", &clk_name);
  529. clk = clk_register_divider_table(NULL, clk_name, clk_parent, 0,
  530. reg, data->shift, data->width,
  531. data->pow ? CLK_DIVIDER_POWER_OF_TWO : 0,
  532. data->table, &clk_lock);
  533. if (clk) {
  534. of_clk_add_provider(node, of_clk_src_simple_get, clk);
  535. clk_register_clkdev(clk, clk_name, NULL);
  536. }
  537. }
  538. /**
  539. * sunxi_gates_reset... - reset bits in leaf gate clk registers handling
  540. */
  541. struct gates_reset_data {
  542. void __iomem *reg;
  543. spinlock_t *lock;
  544. struct reset_controller_dev rcdev;
  545. };
  546. static int sunxi_gates_reset_assert(struct reset_controller_dev *rcdev,
  547. unsigned long id)
  548. {
  549. struct gates_reset_data *data = container_of(rcdev,
  550. struct gates_reset_data,
  551. rcdev);
  552. unsigned long flags;
  553. u32 reg;
  554. spin_lock_irqsave(data->lock, flags);
  555. reg = readl(data->reg);
  556. writel(reg & ~BIT(id), data->reg);
  557. spin_unlock_irqrestore(data->lock, flags);
  558. return 0;
  559. }
  560. static int sunxi_gates_reset_deassert(struct reset_controller_dev *rcdev,
  561. unsigned long id)
  562. {
  563. struct gates_reset_data *data = container_of(rcdev,
  564. struct gates_reset_data,
  565. rcdev);
  566. unsigned long flags;
  567. u32 reg;
  568. spin_lock_irqsave(data->lock, flags);
  569. reg = readl(data->reg);
  570. writel(reg | BIT(id), data->reg);
  571. spin_unlock_irqrestore(data->lock, flags);
  572. return 0;
  573. }
  574. static struct reset_control_ops sunxi_gates_reset_ops = {
  575. .assert = sunxi_gates_reset_assert,
  576. .deassert = sunxi_gates_reset_deassert,
  577. };
  578. /**
  579. * sunxi_gates_clk_setup() - Setup function for leaf gates on clocks
  580. */
  581. #define SUNXI_GATES_MAX_SIZE 64
  582. struct gates_data {
  583. DECLARE_BITMAP(mask, SUNXI_GATES_MAX_SIZE);
  584. u32 reset_mask;
  585. };
  586. static const struct gates_data sun4i_axi_gates_data __initconst = {
  587. .mask = {1},
  588. };
  589. static const struct gates_data sun4i_ahb_gates_data __initconst = {
  590. .mask = {0x7F77FFF, 0x14FB3F},
  591. };
  592. static const struct gates_data sun5i_a10s_ahb_gates_data __initconst = {
  593. .mask = {0x147667e7, 0x185915},
  594. };
  595. static const struct gates_data sun5i_a13_ahb_gates_data __initconst = {
  596. .mask = {0x107067e7, 0x185111},
  597. };
  598. static const struct gates_data sun6i_a31_ahb1_gates_data __initconst = {
  599. .mask = {0xEDFE7F62, 0x794F931},
  600. };
  601. static const struct gates_data sun7i_a20_ahb_gates_data __initconst = {
  602. .mask = { 0x12f77fff, 0x16ff3f },
  603. };
  604. static const struct gates_data sun8i_a23_ahb1_gates_data __initconst = {
  605. .mask = {0x25386742, 0x2505111},
  606. };
  607. static const struct gates_data sun4i_apb0_gates_data __initconst = {
  608. .mask = {0x4EF},
  609. };
  610. static const struct gates_data sun5i_a10s_apb0_gates_data __initconst = {
  611. .mask = {0x469},
  612. };
  613. static const struct gates_data sun5i_a13_apb0_gates_data __initconst = {
  614. .mask = {0x61},
  615. };
  616. static const struct gates_data sun7i_a20_apb0_gates_data __initconst = {
  617. .mask = { 0x4ff },
  618. };
  619. static const struct gates_data sun4i_apb1_gates_data __initconst = {
  620. .mask = {0xFF00F7},
  621. };
  622. static const struct gates_data sun5i_a10s_apb1_gates_data __initconst = {
  623. .mask = {0xf0007},
  624. };
  625. static const struct gates_data sun5i_a13_apb1_gates_data __initconst = {
  626. .mask = {0xa0007},
  627. };
  628. static const struct gates_data sun6i_a31_apb1_gates_data __initconst = {
  629. .mask = {0x3031},
  630. };
  631. static const struct gates_data sun8i_a23_apb1_gates_data __initconst = {
  632. .mask = {0x3021},
  633. };
  634. static const struct gates_data sun6i_a31_apb2_gates_data __initconst = {
  635. .mask = {0x3F000F},
  636. };
  637. static const struct gates_data sun7i_a20_apb1_gates_data __initconst = {
  638. .mask = { 0xff80ff },
  639. };
  640. static const struct gates_data sun8i_a23_apb2_gates_data __initconst = {
  641. .mask = {0x1F0007},
  642. };
  643. static const struct gates_data sun4i_a10_usb_gates_data __initconst = {
  644. .mask = {0x1C0},
  645. .reset_mask = 0x07,
  646. };
  647. static const struct gates_data sun5i_a13_usb_gates_data __initconst = {
  648. .mask = {0x140},
  649. .reset_mask = 0x03,
  650. };
  651. static const struct gates_data sun6i_a31_usb_gates_data __initconst = {
  652. .mask = { BIT(18) | BIT(17) | BIT(16) | BIT(10) | BIT(9) | BIT(8) },
  653. .reset_mask = BIT(2) | BIT(1) | BIT(0),
  654. };
  655. static void __init sunxi_gates_clk_setup(struct device_node *node,
  656. struct gates_data *data)
  657. {
  658. struct clk_onecell_data *clk_data;
  659. struct gates_reset_data *reset_data;
  660. const char *clk_parent;
  661. const char *clk_name;
  662. void __iomem *reg;
  663. int qty;
  664. int i = 0;
  665. int j = 0;
  666. reg = of_iomap(node, 0);
  667. clk_parent = of_clk_get_parent_name(node, 0);
  668. /* Worst-case size approximation and memory allocation */
  669. qty = find_last_bit(data->mask, SUNXI_GATES_MAX_SIZE);
  670. clk_data = kmalloc(sizeof(struct clk_onecell_data), GFP_KERNEL);
  671. if (!clk_data)
  672. return;
  673. clk_data->clks = kzalloc((qty+1) * sizeof(struct clk *), GFP_KERNEL);
  674. if (!clk_data->clks) {
  675. kfree(clk_data);
  676. return;
  677. }
  678. for_each_set_bit(i, data->mask, SUNXI_GATES_MAX_SIZE) {
  679. of_property_read_string_index(node, "clock-output-names",
  680. j, &clk_name);
  681. clk_data->clks[i] = clk_register_gate(NULL, clk_name,
  682. clk_parent, 0,
  683. reg + 4 * (i/32), i % 32,
  684. 0, &clk_lock);
  685. WARN_ON(IS_ERR(clk_data->clks[i]));
  686. clk_register_clkdev(clk_data->clks[i], clk_name, NULL);
  687. j++;
  688. }
  689. /* Adjust to the real max */
  690. clk_data->clk_num = i;
  691. of_clk_add_provider(node, of_clk_src_onecell_get, clk_data);
  692. /* Register a reset controler for gates with reset bits */
  693. if (data->reset_mask == 0)
  694. return;
  695. reset_data = kzalloc(sizeof(*reset_data), GFP_KERNEL);
  696. if (!reset_data)
  697. return;
  698. reset_data->reg = reg;
  699. reset_data->lock = &clk_lock;
  700. reset_data->rcdev.nr_resets = __fls(data->reset_mask) + 1;
  701. reset_data->rcdev.ops = &sunxi_gates_reset_ops;
  702. reset_data->rcdev.of_node = node;
  703. reset_controller_register(&reset_data->rcdev);
  704. }
  705. /**
  706. * sunxi_divs_clk_setup() helper data
  707. */
  708. #define SUNXI_DIVS_MAX_QTY 2
  709. #define SUNXI_DIVISOR_WIDTH 2
  710. struct divs_data {
  711. const struct factors_data *factors; /* data for the factor clock */
  712. struct {
  713. u8 fixed; /* is it a fixed divisor? if not... */
  714. struct clk_div_table *table; /* is it a table based divisor? */
  715. u8 shift; /* otherwise it's a normal divisor with this shift */
  716. u8 pow; /* is it power-of-two based? */
  717. u8 gate; /* is it independently gateable? */
  718. } div[SUNXI_DIVS_MAX_QTY];
  719. };
  720. static struct clk_div_table pll6_sata_tbl[] = {
  721. { .val = 0, .div = 6, },
  722. { .val = 1, .div = 12, },
  723. { .val = 2, .div = 18, },
  724. { .val = 3, .div = 24, },
  725. { } /* sentinel */
  726. };
  727. static const struct divs_data pll5_divs_data __initconst = {
  728. .factors = &sun4i_pll5_data,
  729. .div = {
  730. { .shift = 0, .pow = 0, }, /* M, DDR */
  731. { .shift = 16, .pow = 1, }, /* P, other */
  732. }
  733. };
  734. static const struct divs_data pll6_divs_data __initconst = {
  735. .factors = &sun4i_pll6_data,
  736. .div = {
  737. { .shift = 0, .table = pll6_sata_tbl, .gate = 14 }, /* M, SATA */
  738. { .fixed = 2 }, /* P, other */
  739. }
  740. };
  741. /**
  742. * sunxi_divs_clk_setup() - Setup function for leaf divisors on clocks
  743. *
  744. * These clocks look something like this
  745. * ________________________
  746. * | ___divisor 1---|----> to consumer
  747. * parent >--| pll___/___divisor 2---|----> to consumer
  748. * | \_______________|____> to consumer
  749. * |________________________|
  750. */
  751. static void __init sunxi_divs_clk_setup(struct device_node *node,
  752. struct divs_data *data)
  753. {
  754. struct clk_onecell_data *clk_data;
  755. const char *parent;
  756. const char *clk_name;
  757. struct clk **clks, *pclk;
  758. struct clk_hw *gate_hw, *rate_hw;
  759. const struct clk_ops *rate_ops;
  760. struct clk_gate *gate = NULL;
  761. struct clk_fixed_factor *fix_factor;
  762. struct clk_divider *divider;
  763. void __iomem *reg;
  764. int i = 0;
  765. int flags, clkflags;
  766. /* Set up factor clock that we will be dividing */
  767. pclk = sunxi_factors_clk_setup(node, data->factors);
  768. parent = __clk_get_name(pclk);
  769. reg = of_iomap(node, 0);
  770. clk_data = kmalloc(sizeof(struct clk_onecell_data), GFP_KERNEL);
  771. if (!clk_data)
  772. return;
  773. clks = kzalloc((SUNXI_DIVS_MAX_QTY+1) * sizeof(*clks), GFP_KERNEL);
  774. if (!clks)
  775. goto free_clkdata;
  776. clk_data->clks = clks;
  777. /* It's not a good idea to have automatic reparenting changing
  778. * our RAM clock! */
  779. clkflags = !strcmp("pll5", parent) ? 0 : CLK_SET_RATE_PARENT;
  780. for (i = 0; i < SUNXI_DIVS_MAX_QTY; i++) {
  781. if (of_property_read_string_index(node, "clock-output-names",
  782. i, &clk_name) != 0)
  783. break;
  784. gate_hw = NULL;
  785. rate_hw = NULL;
  786. rate_ops = NULL;
  787. /* If this leaf clock can be gated, create a gate */
  788. if (data->div[i].gate) {
  789. gate = kzalloc(sizeof(*gate), GFP_KERNEL);
  790. if (!gate)
  791. goto free_clks;
  792. gate->reg = reg;
  793. gate->bit_idx = data->div[i].gate;
  794. gate->lock = &clk_lock;
  795. gate_hw = &gate->hw;
  796. }
  797. /* Leaves can be fixed or configurable divisors */
  798. if (data->div[i].fixed) {
  799. fix_factor = kzalloc(sizeof(*fix_factor), GFP_KERNEL);
  800. if (!fix_factor)
  801. goto free_gate;
  802. fix_factor->mult = 1;
  803. fix_factor->div = data->div[i].fixed;
  804. rate_hw = &fix_factor->hw;
  805. rate_ops = &clk_fixed_factor_ops;
  806. } else {
  807. divider = kzalloc(sizeof(*divider), GFP_KERNEL);
  808. if (!divider)
  809. goto free_gate;
  810. flags = data->div[i].pow ? CLK_DIVIDER_POWER_OF_TWO : 0;
  811. divider->reg = reg;
  812. divider->shift = data->div[i].shift;
  813. divider->width = SUNXI_DIVISOR_WIDTH;
  814. divider->flags = flags;
  815. divider->lock = &clk_lock;
  816. divider->table = data->div[i].table;
  817. rate_hw = &divider->hw;
  818. rate_ops = &clk_divider_ops;
  819. }
  820. /* Wrap the (potential) gate and the divisor on a composite
  821. * clock to unify them */
  822. clks[i] = clk_register_composite(NULL, clk_name, &parent, 1,
  823. NULL, NULL,
  824. rate_hw, rate_ops,
  825. gate_hw, &clk_gate_ops,
  826. clkflags);
  827. WARN_ON(IS_ERR(clk_data->clks[i]));
  828. clk_register_clkdev(clks[i], clk_name, NULL);
  829. }
  830. /* The last clock available on the getter is the parent */
  831. clks[i++] = pclk;
  832. /* Adjust to the real max */
  833. clk_data->clk_num = i;
  834. of_clk_add_provider(node, of_clk_src_onecell_get, clk_data);
  835. return;
  836. free_gate:
  837. kfree(gate);
  838. free_clks:
  839. kfree(clks);
  840. free_clkdata:
  841. kfree(clk_data);
  842. }
  843. /* Matches for factors clocks */
  844. static const struct of_device_id clk_factors_match[] __initconst = {
  845. {.compatible = "allwinner,sun4i-a10-pll1-clk", .data = &sun4i_pll1_data,},
  846. {.compatible = "allwinner,sun6i-a31-pll1-clk", .data = &sun6i_a31_pll1_data,},
  847. {.compatible = "allwinner,sun8i-a23-pll1-clk", .data = &sun8i_a23_pll1_data,},
  848. {.compatible = "allwinner,sun7i-a20-pll4-clk", .data = &sun7i_a20_pll4_data,},
  849. {.compatible = "allwinner,sun6i-a31-pll6-clk", .data = &sun6i_a31_pll6_data,},
  850. {.compatible = "allwinner,sun4i-a10-apb1-clk", .data = &sun4i_apb1_data,},
  851. {.compatible = "allwinner,sun7i-a20-out-clk", .data = &sun7i_a20_out_data,},
  852. {}
  853. };
  854. /* Matches for divider clocks */
  855. static const struct of_device_id clk_div_match[] __initconst = {
  856. {.compatible = "allwinner,sun4i-a10-axi-clk", .data = &sun4i_axi_data,},
  857. {.compatible = "allwinner,sun8i-a23-axi-clk", .data = &sun8i_a23_axi_data,},
  858. {.compatible = "allwinner,sun4i-a10-ahb-clk", .data = &sun4i_ahb_data,},
  859. {.compatible = "allwinner,sun4i-a10-apb0-clk", .data = &sun4i_apb0_data,},
  860. {.compatible = "allwinner,sun6i-a31-apb2-div-clk", .data = &sun6i_a31_apb2_div_data,},
  861. {}
  862. };
  863. /* Matches for divided outputs */
  864. static const struct of_device_id clk_divs_match[] __initconst = {
  865. {.compatible = "allwinner,sun4i-a10-pll5-clk", .data = &pll5_divs_data,},
  866. {.compatible = "allwinner,sun4i-a10-pll6-clk", .data = &pll6_divs_data,},
  867. {}
  868. };
  869. /* Matches for mux clocks */
  870. static const struct of_device_id clk_mux_match[] __initconst = {
  871. {.compatible = "allwinner,sun4i-a10-cpu-clk", .data = &sun4i_cpu_mux_data,},
  872. {.compatible = "allwinner,sun4i-a10-apb1-mux-clk", .data = &sun4i_apb1_mux_data,},
  873. {.compatible = "allwinner,sun6i-a31-ahb1-mux-clk", .data = &sun6i_a31_ahb1_mux_data,},
  874. {}
  875. };
  876. /* Matches for gate clocks */
  877. static const struct of_device_id clk_gates_match[] __initconst = {
  878. {.compatible = "allwinner,sun4i-a10-axi-gates-clk", .data = &sun4i_axi_gates_data,},
  879. {.compatible = "allwinner,sun4i-a10-ahb-gates-clk", .data = &sun4i_ahb_gates_data,},
  880. {.compatible = "allwinner,sun5i-a10s-ahb-gates-clk", .data = &sun5i_a10s_ahb_gates_data,},
  881. {.compatible = "allwinner,sun5i-a13-ahb-gates-clk", .data = &sun5i_a13_ahb_gates_data,},
  882. {.compatible = "allwinner,sun6i-a31-ahb1-gates-clk", .data = &sun6i_a31_ahb1_gates_data,},
  883. {.compatible = "allwinner,sun7i-a20-ahb-gates-clk", .data = &sun7i_a20_ahb_gates_data,},
  884. {.compatible = "allwinner,sun8i-a23-ahb1-gates-clk", .data = &sun8i_a23_ahb1_gates_data,},
  885. {.compatible = "allwinner,sun4i-a10-apb0-gates-clk", .data = &sun4i_apb0_gates_data,},
  886. {.compatible = "allwinner,sun5i-a10s-apb0-gates-clk", .data = &sun5i_a10s_apb0_gates_data,},
  887. {.compatible = "allwinner,sun5i-a13-apb0-gates-clk", .data = &sun5i_a13_apb0_gates_data,},
  888. {.compatible = "allwinner,sun7i-a20-apb0-gates-clk", .data = &sun7i_a20_apb0_gates_data,},
  889. {.compatible = "allwinner,sun4i-a10-apb1-gates-clk", .data = &sun4i_apb1_gates_data,},
  890. {.compatible = "allwinner,sun5i-a10s-apb1-gates-clk", .data = &sun5i_a10s_apb1_gates_data,},
  891. {.compatible = "allwinner,sun5i-a13-apb1-gates-clk", .data = &sun5i_a13_apb1_gates_data,},
  892. {.compatible = "allwinner,sun6i-a31-apb1-gates-clk", .data = &sun6i_a31_apb1_gates_data,},
  893. {.compatible = "allwinner,sun7i-a20-apb1-gates-clk", .data = &sun7i_a20_apb1_gates_data,},
  894. {.compatible = "allwinner,sun8i-a23-apb1-gates-clk", .data = &sun8i_a23_apb1_gates_data,},
  895. {.compatible = "allwinner,sun6i-a31-apb2-gates-clk", .data = &sun6i_a31_apb2_gates_data,},
  896. {.compatible = "allwinner,sun8i-a23-apb2-gates-clk", .data = &sun8i_a23_apb2_gates_data,},
  897. {.compatible = "allwinner,sun4i-a10-usb-clk", .data = &sun4i_a10_usb_gates_data,},
  898. {.compatible = "allwinner,sun5i-a13-usb-clk", .data = &sun5i_a13_usb_gates_data,},
  899. {.compatible = "allwinner,sun6i-a31-usb-clk", .data = &sun6i_a31_usb_gates_data,},
  900. {}
  901. };
  902. static void __init of_sunxi_table_clock_setup(const struct of_device_id *clk_match,
  903. void *function)
  904. {
  905. struct device_node *np;
  906. const struct div_data *data;
  907. const struct of_device_id *match;
  908. void (*setup_function)(struct device_node *, const void *) = function;
  909. for_each_matching_node_and_match(np, clk_match, &match) {
  910. data = match->data;
  911. setup_function(np, data);
  912. }
  913. }
  914. static void __init sunxi_init_clocks(const char *clocks[], int nclocks)
  915. {
  916. unsigned int i;
  917. /* Register factor clocks */
  918. of_sunxi_table_clock_setup(clk_factors_match, sunxi_factors_clk_setup);
  919. /* Register divider clocks */
  920. of_sunxi_table_clock_setup(clk_div_match, sunxi_divider_clk_setup);
  921. /* Register divided output clocks */
  922. of_sunxi_table_clock_setup(clk_divs_match, sunxi_divs_clk_setup);
  923. /* Register mux clocks */
  924. of_sunxi_table_clock_setup(clk_mux_match, sunxi_mux_clk_setup);
  925. /* Register gate clocks */
  926. of_sunxi_table_clock_setup(clk_gates_match, sunxi_gates_clk_setup);
  927. /* Protect the clocks that needs to stay on */
  928. for (i = 0; i < nclocks; i++) {
  929. struct clk *clk = clk_get(NULL, clocks[i]);
  930. if (!IS_ERR(clk))
  931. clk_prepare_enable(clk);
  932. }
  933. }
  934. static const char *sun4i_a10_critical_clocks[] __initdata = {
  935. "pll5_ddr",
  936. "ahb_sdram",
  937. };
  938. static void __init sun4i_a10_init_clocks(struct device_node *node)
  939. {
  940. sunxi_init_clocks(sun4i_a10_critical_clocks,
  941. ARRAY_SIZE(sun4i_a10_critical_clocks));
  942. }
  943. CLK_OF_DECLARE(sun4i_a10_clk_init, "allwinner,sun4i-a10", sun4i_a10_init_clocks);
  944. static const char *sun5i_critical_clocks[] __initdata = {
  945. "pll5_ddr",
  946. "ahb_sdram",
  947. };
  948. static void __init sun5i_init_clocks(struct device_node *node)
  949. {
  950. sunxi_init_clocks(sun5i_critical_clocks,
  951. ARRAY_SIZE(sun5i_critical_clocks));
  952. }
  953. CLK_OF_DECLARE(sun5i_a10s_clk_init, "allwinner,sun5i-a10s", sun5i_init_clocks);
  954. CLK_OF_DECLARE(sun5i_a13_clk_init, "allwinner,sun5i-a13", sun5i_init_clocks);
  955. CLK_OF_DECLARE(sun7i_a20_clk_init, "allwinner,sun7i-a20", sun5i_init_clocks);
  956. static const char *sun6i_critical_clocks[] __initdata = {
  957. "cpu",
  958. "ahb1_sdram",
  959. };
  960. static void __init sun6i_init_clocks(struct device_node *node)
  961. {
  962. sunxi_init_clocks(sun6i_critical_clocks,
  963. ARRAY_SIZE(sun6i_critical_clocks));
  964. }
  965. CLK_OF_DECLARE(sun6i_a31_clk_init, "allwinner,sun6i-a31", sun6i_init_clocks);
  966. CLK_OF_DECLARE(sun8i_a23_clk_init, "allwinner,sun8i-a23", sun6i_init_clocks);