clk-sunxi.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235
  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 PLL6x2
  208. * PLL6x2 rate is calculated as follows
  209. * rate = parent_rate * (n + 1) * (k + 1)
  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. /* Normalize value to a parent_rate multiple (24M) */
  217. div = *freq / parent_rate;
  218. *freq = parent_rate * div;
  219. /* we were called to round the frequency, we can now return */
  220. if (n == NULL)
  221. return;
  222. *k = div / 32;
  223. if (*k > 3)
  224. *k = 3;
  225. *n = DIV_ROUND_UP(div, (*k+1)) - 1;
  226. }
  227. /**
  228. * sun4i_get_apb1_factors() - calculates m, p factors for APB1
  229. * APB1 rate is calculated as follows
  230. * rate = (parent_rate >> p) / (m + 1);
  231. */
  232. static void sun4i_get_apb1_factors(u32 *freq, u32 parent_rate,
  233. u8 *n, u8 *k, u8 *m, u8 *p)
  234. {
  235. u8 calcm, calcp;
  236. if (parent_rate < *freq)
  237. *freq = parent_rate;
  238. parent_rate = DIV_ROUND_UP(parent_rate, *freq);
  239. /* Invalid rate! */
  240. if (parent_rate > 32)
  241. return;
  242. if (parent_rate <= 4)
  243. calcp = 0;
  244. else if (parent_rate <= 8)
  245. calcp = 1;
  246. else if (parent_rate <= 16)
  247. calcp = 2;
  248. else
  249. calcp = 3;
  250. calcm = (parent_rate >> calcp) - 1;
  251. *freq = (parent_rate >> calcp) / (calcm + 1);
  252. /* we were called to round the frequency, we can now return */
  253. if (n == NULL)
  254. return;
  255. *m = calcm;
  256. *p = calcp;
  257. }
  258. /**
  259. * sun7i_a20_get_out_factors() - calculates m, p factors for CLK_OUT_A/B
  260. * CLK_OUT rate is calculated as follows
  261. * rate = (parent_rate >> p) / (m + 1);
  262. */
  263. static void sun7i_a20_get_out_factors(u32 *freq, u32 parent_rate,
  264. u8 *n, u8 *k, u8 *m, u8 *p)
  265. {
  266. u8 div, calcm, calcp;
  267. /* These clocks can only divide, so we will never be able to achieve
  268. * frequencies higher than the parent frequency */
  269. if (*freq > parent_rate)
  270. *freq = parent_rate;
  271. div = DIV_ROUND_UP(parent_rate, *freq);
  272. if (div < 32)
  273. calcp = 0;
  274. else if (div / 2 < 32)
  275. calcp = 1;
  276. else if (div / 4 < 32)
  277. calcp = 2;
  278. else
  279. calcp = 3;
  280. calcm = DIV_ROUND_UP(div, 1 << calcp);
  281. *freq = (parent_rate >> calcp) / calcm;
  282. /* we were called to round the frequency, we can now return */
  283. if (n == NULL)
  284. return;
  285. *m = calcm - 1;
  286. *p = calcp;
  287. }
  288. /**
  289. * clk_sunxi_mmc_phase_control() - configures MMC clock phase control
  290. */
  291. void clk_sunxi_mmc_phase_control(struct clk *clk, u8 sample, u8 output)
  292. {
  293. #define to_clk_composite(_hw) container_of(_hw, struct clk_composite, hw)
  294. #define to_clk_factors(_hw) container_of(_hw, struct clk_factors, hw)
  295. struct clk_hw *hw = __clk_get_hw(clk);
  296. struct clk_composite *composite = to_clk_composite(hw);
  297. struct clk_hw *rate_hw = composite->rate_hw;
  298. struct clk_factors *factors = to_clk_factors(rate_hw);
  299. unsigned long flags = 0;
  300. u32 reg;
  301. if (factors->lock)
  302. spin_lock_irqsave(factors->lock, flags);
  303. reg = readl(factors->reg);
  304. /* set sample clock phase control */
  305. reg &= ~(0x7 << 20);
  306. reg |= ((sample & 0x7) << 20);
  307. /* set output clock phase control */
  308. reg &= ~(0x7 << 8);
  309. reg |= ((output & 0x7) << 8);
  310. writel(reg, factors->reg);
  311. if (factors->lock)
  312. spin_unlock_irqrestore(factors->lock, flags);
  313. }
  314. EXPORT_SYMBOL(clk_sunxi_mmc_phase_control);
  315. /**
  316. * sunxi_factors_clk_setup() - Setup function for factor clocks
  317. */
  318. static struct clk_factors_config sun4i_pll1_config = {
  319. .nshift = 8,
  320. .nwidth = 5,
  321. .kshift = 4,
  322. .kwidth = 2,
  323. .mshift = 0,
  324. .mwidth = 2,
  325. .pshift = 16,
  326. .pwidth = 2,
  327. };
  328. static struct clk_factors_config sun6i_a31_pll1_config = {
  329. .nshift = 8,
  330. .nwidth = 5,
  331. .kshift = 4,
  332. .kwidth = 2,
  333. .mshift = 0,
  334. .mwidth = 2,
  335. };
  336. static struct clk_factors_config sun8i_a23_pll1_config = {
  337. .nshift = 8,
  338. .nwidth = 5,
  339. .kshift = 4,
  340. .kwidth = 2,
  341. .mshift = 0,
  342. .mwidth = 2,
  343. .pshift = 16,
  344. .pwidth = 2,
  345. .n_start = 1,
  346. };
  347. static struct clk_factors_config sun4i_pll5_config = {
  348. .nshift = 8,
  349. .nwidth = 5,
  350. .kshift = 4,
  351. .kwidth = 2,
  352. };
  353. static struct clk_factors_config sun6i_a31_pll6_config = {
  354. .nshift = 8,
  355. .nwidth = 5,
  356. .kshift = 4,
  357. .kwidth = 2,
  358. .n_start = 1,
  359. };
  360. static struct clk_factors_config sun4i_apb1_config = {
  361. .mshift = 0,
  362. .mwidth = 5,
  363. .pshift = 16,
  364. .pwidth = 2,
  365. };
  366. /* user manual says "n" but it's really "p" */
  367. static struct clk_factors_config sun7i_a20_out_config = {
  368. .mshift = 8,
  369. .mwidth = 5,
  370. .pshift = 20,
  371. .pwidth = 2,
  372. };
  373. static const struct factors_data sun4i_pll1_data __initconst = {
  374. .enable = 31,
  375. .table = &sun4i_pll1_config,
  376. .getter = sun4i_get_pll1_factors,
  377. };
  378. static const struct factors_data sun6i_a31_pll1_data __initconst = {
  379. .enable = 31,
  380. .table = &sun6i_a31_pll1_config,
  381. .getter = sun6i_a31_get_pll1_factors,
  382. };
  383. static const struct factors_data sun8i_a23_pll1_data __initconst = {
  384. .enable = 31,
  385. .table = &sun8i_a23_pll1_config,
  386. .getter = sun8i_a23_get_pll1_factors,
  387. };
  388. static const struct factors_data sun7i_a20_pll4_data __initconst = {
  389. .enable = 31,
  390. .table = &sun4i_pll5_config,
  391. .getter = sun4i_get_pll5_factors,
  392. };
  393. static const struct factors_data sun4i_pll5_data __initconst = {
  394. .enable = 31,
  395. .table = &sun4i_pll5_config,
  396. .getter = sun4i_get_pll5_factors,
  397. .name = "pll5",
  398. };
  399. static const struct factors_data sun4i_pll6_data __initconst = {
  400. .enable = 31,
  401. .table = &sun4i_pll5_config,
  402. .getter = sun4i_get_pll5_factors,
  403. .name = "pll6",
  404. };
  405. static const struct factors_data sun6i_a31_pll6_data __initconst = {
  406. .enable = 31,
  407. .table = &sun6i_a31_pll6_config,
  408. .getter = sun6i_a31_get_pll6_factors,
  409. .name = "pll6x2",
  410. };
  411. static const struct factors_data sun4i_apb1_data __initconst = {
  412. .mux = 24,
  413. .muxmask = BIT(1) | BIT(0),
  414. .table = &sun4i_apb1_config,
  415. .getter = sun4i_get_apb1_factors,
  416. };
  417. static const struct factors_data sun7i_a20_out_data __initconst = {
  418. .enable = 31,
  419. .mux = 24,
  420. .muxmask = BIT(1) | BIT(0),
  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 void __init sunxi_mux_clk_setup(struct device_node *node,
  443. struct mux_data *data)
  444. {
  445. struct clk *clk;
  446. const char *clk_name = node->name;
  447. const char *parents[SUNXI_MAX_PARENTS];
  448. void __iomem *reg;
  449. int i = 0;
  450. reg = of_iomap(node, 0);
  451. while (i < SUNXI_MAX_PARENTS &&
  452. (parents[i] = of_clk_get_parent_name(node, i)) != NULL)
  453. i++;
  454. of_property_read_string(node, "clock-output-names", &clk_name);
  455. clk = clk_register_mux(NULL, clk_name, parents, i,
  456. CLK_SET_RATE_NO_REPARENT, reg,
  457. data->shift, SUNXI_MUX_GATE_WIDTH,
  458. 0, &clk_lock);
  459. if (clk) {
  460. of_clk_add_provider(node, of_clk_src_simple_get, clk);
  461. clk_register_clkdev(clk, clk_name, NULL);
  462. }
  463. }
  464. /**
  465. * sunxi_divider_clk_setup() - Setup function for simple divider clocks
  466. */
  467. struct div_data {
  468. u8 shift;
  469. u8 pow;
  470. u8 width;
  471. const struct clk_div_table *table;
  472. };
  473. static const struct div_data sun4i_axi_data __initconst = {
  474. .shift = 0,
  475. .pow = 0,
  476. .width = 2,
  477. };
  478. static const struct clk_div_table sun8i_a23_axi_table[] __initconst = {
  479. { .val = 0, .div = 1 },
  480. { .val = 1, .div = 2 },
  481. { .val = 2, .div = 3 },
  482. { .val = 3, .div = 4 },
  483. { .val = 4, .div = 4 },
  484. { .val = 5, .div = 4 },
  485. { .val = 6, .div = 4 },
  486. { .val = 7, .div = 4 },
  487. { } /* sentinel */
  488. };
  489. static const struct div_data sun8i_a23_axi_data __initconst = {
  490. .width = 3,
  491. .table = sun8i_a23_axi_table,
  492. };
  493. static const struct div_data sun4i_ahb_data __initconst = {
  494. .shift = 4,
  495. .pow = 1,
  496. .width = 2,
  497. };
  498. static const struct clk_div_table sun4i_apb0_table[] __initconst = {
  499. { .val = 0, .div = 2 },
  500. { .val = 1, .div = 2 },
  501. { .val = 2, .div = 4 },
  502. { .val = 3, .div = 8 },
  503. { } /* sentinel */
  504. };
  505. static const struct div_data sun4i_apb0_data __initconst = {
  506. .shift = 8,
  507. .pow = 1,
  508. .width = 2,
  509. .table = sun4i_apb0_table,
  510. };
  511. static void __init sunxi_divider_clk_setup(struct device_node *node,
  512. struct div_data *data)
  513. {
  514. struct clk *clk;
  515. const char *clk_name = node->name;
  516. const char *clk_parent;
  517. void __iomem *reg;
  518. reg = of_iomap(node, 0);
  519. clk_parent = of_clk_get_parent_name(node, 0);
  520. of_property_read_string(node, "clock-output-names", &clk_name);
  521. clk = clk_register_divider_table(NULL, clk_name, clk_parent, 0,
  522. reg, data->shift, data->width,
  523. data->pow ? CLK_DIVIDER_POWER_OF_TWO : 0,
  524. data->table, &clk_lock);
  525. if (clk) {
  526. of_clk_add_provider(node, of_clk_src_simple_get, clk);
  527. clk_register_clkdev(clk, clk_name, NULL);
  528. }
  529. }
  530. /**
  531. * sunxi_gates_reset... - reset bits in leaf gate clk registers handling
  532. */
  533. struct gates_reset_data {
  534. void __iomem *reg;
  535. spinlock_t *lock;
  536. struct reset_controller_dev rcdev;
  537. };
  538. static int sunxi_gates_reset_assert(struct reset_controller_dev *rcdev,
  539. unsigned long id)
  540. {
  541. struct gates_reset_data *data = container_of(rcdev,
  542. struct gates_reset_data,
  543. rcdev);
  544. unsigned long flags;
  545. u32 reg;
  546. spin_lock_irqsave(data->lock, flags);
  547. reg = readl(data->reg);
  548. writel(reg & ~BIT(id), data->reg);
  549. spin_unlock_irqrestore(data->lock, flags);
  550. return 0;
  551. }
  552. static int sunxi_gates_reset_deassert(struct reset_controller_dev *rcdev,
  553. unsigned long id)
  554. {
  555. struct gates_reset_data *data = container_of(rcdev,
  556. struct gates_reset_data,
  557. rcdev);
  558. unsigned long flags;
  559. u32 reg;
  560. spin_lock_irqsave(data->lock, flags);
  561. reg = readl(data->reg);
  562. writel(reg | BIT(id), data->reg);
  563. spin_unlock_irqrestore(data->lock, flags);
  564. return 0;
  565. }
  566. static struct reset_control_ops sunxi_gates_reset_ops = {
  567. .assert = sunxi_gates_reset_assert,
  568. .deassert = sunxi_gates_reset_deassert,
  569. };
  570. /**
  571. * sunxi_gates_clk_setup() - Setup function for leaf gates on clocks
  572. */
  573. #define SUNXI_GATES_MAX_SIZE 64
  574. struct gates_data {
  575. DECLARE_BITMAP(mask, SUNXI_GATES_MAX_SIZE);
  576. u32 reset_mask;
  577. };
  578. static const struct gates_data sun4i_axi_gates_data __initconst = {
  579. .mask = {1},
  580. };
  581. static const struct gates_data sun4i_ahb_gates_data __initconst = {
  582. .mask = {0x7F77FFF, 0x14FB3F},
  583. };
  584. static const struct gates_data sun5i_a10s_ahb_gates_data __initconst = {
  585. .mask = {0x147667e7, 0x185915},
  586. };
  587. static const struct gates_data sun5i_a13_ahb_gates_data __initconst = {
  588. .mask = {0x107067e7, 0x185111},
  589. };
  590. static const struct gates_data sun6i_a31_ahb1_gates_data __initconst = {
  591. .mask = {0xEDFE7F62, 0x794F931},
  592. };
  593. static const struct gates_data sun7i_a20_ahb_gates_data __initconst = {
  594. .mask = { 0x12f77fff, 0x16ff3f },
  595. };
  596. static const struct gates_data sun8i_a23_ahb1_gates_data __initconst = {
  597. .mask = {0x25386742, 0x2505111},
  598. };
  599. static const struct gates_data sun9i_a80_ahb0_gates_data __initconst = {
  600. .mask = {0xF5F12B},
  601. };
  602. static const struct gates_data sun9i_a80_ahb1_gates_data __initconst = {
  603. .mask = {0x1E20003},
  604. };
  605. static const struct gates_data sun9i_a80_ahb2_gates_data __initconst = {
  606. .mask = {0x9B7},
  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 sun9i_a80_apb0_gates_data __initconst = {
  621. .mask = {0xEB822},
  622. };
  623. static const struct gates_data sun4i_apb1_gates_data __initconst = {
  624. .mask = {0xFF00F7},
  625. };
  626. static const struct gates_data sun5i_a10s_apb1_gates_data __initconst = {
  627. .mask = {0xf0007},
  628. };
  629. static const struct gates_data sun5i_a13_apb1_gates_data __initconst = {
  630. .mask = {0xa0007},
  631. };
  632. static const struct gates_data sun6i_a31_apb1_gates_data __initconst = {
  633. .mask = {0x3031},
  634. };
  635. static const struct gates_data sun8i_a23_apb1_gates_data __initconst = {
  636. .mask = {0x3021},
  637. };
  638. static const struct gates_data sun6i_a31_apb2_gates_data __initconst = {
  639. .mask = {0x3F000F},
  640. };
  641. static const struct gates_data sun7i_a20_apb1_gates_data __initconst = {
  642. .mask = { 0xff80ff },
  643. };
  644. static const struct gates_data sun9i_a80_apb1_gates_data __initconst = {
  645. .mask = {0x3F001F},
  646. };
  647. static const struct gates_data sun8i_a23_apb2_gates_data __initconst = {
  648. .mask = {0x1F0007},
  649. };
  650. static const struct gates_data sun4i_a10_usb_gates_data __initconst = {
  651. .mask = {0x1C0},
  652. .reset_mask = 0x07,
  653. };
  654. static const struct gates_data sun5i_a13_usb_gates_data __initconst = {
  655. .mask = {0x140},
  656. .reset_mask = 0x03,
  657. };
  658. static const struct gates_data sun6i_a31_usb_gates_data __initconst = {
  659. .mask = { BIT(18) | BIT(17) | BIT(16) | BIT(10) | BIT(9) | BIT(8) },
  660. .reset_mask = BIT(2) | BIT(1) | BIT(0),
  661. };
  662. static void __init sunxi_gates_clk_setup(struct device_node *node,
  663. struct gates_data *data)
  664. {
  665. struct clk_onecell_data *clk_data;
  666. struct gates_reset_data *reset_data;
  667. const char *clk_parent;
  668. const char *clk_name;
  669. void __iomem *reg;
  670. int qty;
  671. int i = 0;
  672. int j = 0;
  673. reg = of_iomap(node, 0);
  674. clk_parent = of_clk_get_parent_name(node, 0);
  675. /* Worst-case size approximation and memory allocation */
  676. qty = find_last_bit(data->mask, SUNXI_GATES_MAX_SIZE);
  677. clk_data = kmalloc(sizeof(struct clk_onecell_data), GFP_KERNEL);
  678. if (!clk_data)
  679. return;
  680. clk_data->clks = kzalloc((qty+1) * sizeof(struct clk *), GFP_KERNEL);
  681. if (!clk_data->clks) {
  682. kfree(clk_data);
  683. return;
  684. }
  685. for_each_set_bit(i, data->mask, SUNXI_GATES_MAX_SIZE) {
  686. of_property_read_string_index(node, "clock-output-names",
  687. j, &clk_name);
  688. clk_data->clks[i] = clk_register_gate(NULL, clk_name,
  689. clk_parent, 0,
  690. reg + 4 * (i/32), i % 32,
  691. 0, &clk_lock);
  692. WARN_ON(IS_ERR(clk_data->clks[i]));
  693. clk_register_clkdev(clk_data->clks[i], clk_name, NULL);
  694. j++;
  695. }
  696. /* Adjust to the real max */
  697. clk_data->clk_num = i;
  698. of_clk_add_provider(node, of_clk_src_onecell_get, clk_data);
  699. /* Register a reset controler for gates with reset bits */
  700. if (data->reset_mask == 0)
  701. return;
  702. reset_data = kzalloc(sizeof(*reset_data), GFP_KERNEL);
  703. if (!reset_data)
  704. return;
  705. reset_data->reg = reg;
  706. reset_data->lock = &clk_lock;
  707. reset_data->rcdev.nr_resets = __fls(data->reset_mask) + 1;
  708. reset_data->rcdev.ops = &sunxi_gates_reset_ops;
  709. reset_data->rcdev.of_node = node;
  710. reset_controller_register(&reset_data->rcdev);
  711. }
  712. /**
  713. * sunxi_divs_clk_setup() helper data
  714. */
  715. #define SUNXI_DIVS_MAX_QTY 2
  716. #define SUNXI_DIVISOR_WIDTH 2
  717. struct divs_data {
  718. const struct factors_data *factors; /* data for the factor clock */
  719. int ndivs; /* number of children */
  720. struct {
  721. u8 fixed; /* is it a fixed divisor? if not... */
  722. struct clk_div_table *table; /* is it a table based divisor? */
  723. u8 shift; /* otherwise it's a normal divisor with this shift */
  724. u8 pow; /* is it power-of-two based? */
  725. u8 gate; /* is it independently gateable? */
  726. } div[SUNXI_DIVS_MAX_QTY];
  727. };
  728. static struct clk_div_table pll6_sata_tbl[] = {
  729. { .val = 0, .div = 6, },
  730. { .val = 1, .div = 12, },
  731. { .val = 2, .div = 18, },
  732. { .val = 3, .div = 24, },
  733. { } /* sentinel */
  734. };
  735. static const struct divs_data pll5_divs_data __initconst = {
  736. .factors = &sun4i_pll5_data,
  737. .ndivs = 2,
  738. .div = {
  739. { .shift = 0, .pow = 0, }, /* M, DDR */
  740. { .shift = 16, .pow = 1, }, /* P, other */
  741. }
  742. };
  743. static const struct divs_data pll6_divs_data __initconst = {
  744. .factors = &sun4i_pll6_data,
  745. .ndivs = 2,
  746. .div = {
  747. { .shift = 0, .table = pll6_sata_tbl, .gate = 14 }, /* M, SATA */
  748. { .fixed = 2 }, /* P, other */
  749. }
  750. };
  751. static const struct divs_data sun6i_a31_pll6_divs_data __initconst = {
  752. .factors = &sun6i_a31_pll6_data,
  753. .ndivs = 1,
  754. .div = {
  755. { .fixed = 2 }, /* normal output */
  756. }
  757. };
  758. /**
  759. * sunxi_divs_clk_setup() - Setup function for leaf divisors on clocks
  760. *
  761. * These clocks look something like this
  762. * ________________________
  763. * | ___divisor 1---|----> to consumer
  764. * parent >--| pll___/___divisor 2---|----> to consumer
  765. * | \_______________|____> to consumer
  766. * |________________________|
  767. */
  768. static void __init sunxi_divs_clk_setup(struct device_node *node,
  769. struct divs_data *data)
  770. {
  771. struct clk_onecell_data *clk_data;
  772. const char *parent;
  773. const char *clk_name;
  774. struct clk **clks, *pclk;
  775. struct clk_hw *gate_hw, *rate_hw;
  776. const struct clk_ops *rate_ops;
  777. struct clk_gate *gate = NULL;
  778. struct clk_fixed_factor *fix_factor;
  779. struct clk_divider *divider;
  780. void __iomem *reg;
  781. int ndivs = SUNXI_DIVS_MAX_QTY, i = 0;
  782. int flags, clkflags;
  783. /* Set up factor clock that we will be dividing */
  784. pclk = sunxi_factors_clk_setup(node, data->factors);
  785. parent = __clk_get_name(pclk);
  786. reg = of_iomap(node, 0);
  787. clk_data = kmalloc(sizeof(struct clk_onecell_data), GFP_KERNEL);
  788. if (!clk_data)
  789. return;
  790. clks = kzalloc((SUNXI_DIVS_MAX_QTY+1) * sizeof(*clks), GFP_KERNEL);
  791. if (!clks)
  792. goto free_clkdata;
  793. clk_data->clks = clks;
  794. /* It's not a good idea to have automatic reparenting changing
  795. * our RAM clock! */
  796. clkflags = !strcmp("pll5", parent) ? 0 : CLK_SET_RATE_PARENT;
  797. /* if number of children known, use it */
  798. if (data->ndivs)
  799. ndivs = data->ndivs;
  800. for (i = 0; i < ndivs; i++) {
  801. if (of_property_read_string_index(node, "clock-output-names",
  802. i, &clk_name) != 0)
  803. break;
  804. gate_hw = NULL;
  805. rate_hw = NULL;
  806. rate_ops = NULL;
  807. /* If this leaf clock can be gated, create a gate */
  808. if (data->div[i].gate) {
  809. gate = kzalloc(sizeof(*gate), GFP_KERNEL);
  810. if (!gate)
  811. goto free_clks;
  812. gate->reg = reg;
  813. gate->bit_idx = data->div[i].gate;
  814. gate->lock = &clk_lock;
  815. gate_hw = &gate->hw;
  816. }
  817. /* Leaves can be fixed or configurable divisors */
  818. if (data->div[i].fixed) {
  819. fix_factor = kzalloc(sizeof(*fix_factor), GFP_KERNEL);
  820. if (!fix_factor)
  821. goto free_gate;
  822. fix_factor->mult = 1;
  823. fix_factor->div = data->div[i].fixed;
  824. rate_hw = &fix_factor->hw;
  825. rate_ops = &clk_fixed_factor_ops;
  826. } else {
  827. divider = kzalloc(sizeof(*divider), GFP_KERNEL);
  828. if (!divider)
  829. goto free_gate;
  830. flags = data->div[i].pow ? CLK_DIVIDER_POWER_OF_TWO : 0;
  831. divider->reg = reg;
  832. divider->shift = data->div[i].shift;
  833. divider->width = SUNXI_DIVISOR_WIDTH;
  834. divider->flags = flags;
  835. divider->lock = &clk_lock;
  836. divider->table = data->div[i].table;
  837. rate_hw = &divider->hw;
  838. rate_ops = &clk_divider_ops;
  839. }
  840. /* Wrap the (potential) gate and the divisor on a composite
  841. * clock to unify them */
  842. clks[i] = clk_register_composite(NULL, clk_name, &parent, 1,
  843. NULL, NULL,
  844. rate_hw, rate_ops,
  845. gate_hw, &clk_gate_ops,
  846. clkflags);
  847. WARN_ON(IS_ERR(clk_data->clks[i]));
  848. clk_register_clkdev(clks[i], clk_name, NULL);
  849. }
  850. /* The last clock available on the getter is the parent */
  851. clks[i++] = pclk;
  852. /* Adjust to the real max */
  853. clk_data->clk_num = i;
  854. of_clk_add_provider(node, of_clk_src_onecell_get, clk_data);
  855. return;
  856. free_gate:
  857. kfree(gate);
  858. free_clks:
  859. kfree(clks);
  860. free_clkdata:
  861. kfree(clk_data);
  862. }
  863. /* Matches for factors clocks */
  864. static const struct of_device_id clk_factors_match[] __initconst = {
  865. {.compatible = "allwinner,sun4i-a10-pll1-clk", .data = &sun4i_pll1_data,},
  866. {.compatible = "allwinner,sun6i-a31-pll1-clk", .data = &sun6i_a31_pll1_data,},
  867. {.compatible = "allwinner,sun8i-a23-pll1-clk", .data = &sun8i_a23_pll1_data,},
  868. {.compatible = "allwinner,sun7i-a20-pll4-clk", .data = &sun7i_a20_pll4_data,},
  869. {.compatible = "allwinner,sun4i-a10-apb1-clk", .data = &sun4i_apb1_data,},
  870. {.compatible = "allwinner,sun7i-a20-out-clk", .data = &sun7i_a20_out_data,},
  871. {}
  872. };
  873. /* Matches for divider clocks */
  874. static const struct of_device_id clk_div_match[] __initconst = {
  875. {.compatible = "allwinner,sun4i-a10-axi-clk", .data = &sun4i_axi_data,},
  876. {.compatible = "allwinner,sun8i-a23-axi-clk", .data = &sun8i_a23_axi_data,},
  877. {.compatible = "allwinner,sun4i-a10-ahb-clk", .data = &sun4i_ahb_data,},
  878. {.compatible = "allwinner,sun4i-a10-apb0-clk", .data = &sun4i_apb0_data,},
  879. {}
  880. };
  881. /* Matches for divided outputs */
  882. static const struct of_device_id clk_divs_match[] __initconst = {
  883. {.compatible = "allwinner,sun4i-a10-pll5-clk", .data = &pll5_divs_data,},
  884. {.compatible = "allwinner,sun4i-a10-pll6-clk", .data = &pll6_divs_data,},
  885. {.compatible = "allwinner,sun6i-a31-pll6-clk", .data = &sun6i_a31_pll6_divs_data,},
  886. {}
  887. };
  888. /* Matches for mux clocks */
  889. static const struct of_device_id clk_mux_match[] __initconst = {
  890. {.compatible = "allwinner,sun4i-a10-cpu-clk", .data = &sun4i_cpu_mux_data,},
  891. {.compatible = "allwinner,sun6i-a31-ahb1-mux-clk", .data = &sun6i_a31_ahb1_mux_data,},
  892. {}
  893. };
  894. /* Matches for gate clocks */
  895. static const struct of_device_id clk_gates_match[] __initconst = {
  896. {.compatible = "allwinner,sun4i-a10-axi-gates-clk", .data = &sun4i_axi_gates_data,},
  897. {.compatible = "allwinner,sun4i-a10-ahb-gates-clk", .data = &sun4i_ahb_gates_data,},
  898. {.compatible = "allwinner,sun5i-a10s-ahb-gates-clk", .data = &sun5i_a10s_ahb_gates_data,},
  899. {.compatible = "allwinner,sun5i-a13-ahb-gates-clk", .data = &sun5i_a13_ahb_gates_data,},
  900. {.compatible = "allwinner,sun6i-a31-ahb1-gates-clk", .data = &sun6i_a31_ahb1_gates_data,},
  901. {.compatible = "allwinner,sun7i-a20-ahb-gates-clk", .data = &sun7i_a20_ahb_gates_data,},
  902. {.compatible = "allwinner,sun8i-a23-ahb1-gates-clk", .data = &sun8i_a23_ahb1_gates_data,},
  903. {.compatible = "allwinner,sun9i-a80-ahb0-gates-clk", .data = &sun9i_a80_ahb0_gates_data,},
  904. {.compatible = "allwinner,sun9i-a80-ahb1-gates-clk", .data = &sun9i_a80_ahb1_gates_data,},
  905. {.compatible = "allwinner,sun9i-a80-ahb2-gates-clk", .data = &sun9i_a80_ahb2_gates_data,},
  906. {.compatible = "allwinner,sun4i-a10-apb0-gates-clk", .data = &sun4i_apb0_gates_data,},
  907. {.compatible = "allwinner,sun5i-a10s-apb0-gates-clk", .data = &sun5i_a10s_apb0_gates_data,},
  908. {.compatible = "allwinner,sun5i-a13-apb0-gates-clk", .data = &sun5i_a13_apb0_gates_data,},
  909. {.compatible = "allwinner,sun7i-a20-apb0-gates-clk", .data = &sun7i_a20_apb0_gates_data,},
  910. {.compatible = "allwinner,sun9i-a80-apb0-gates-clk", .data = &sun9i_a80_apb0_gates_data,},
  911. {.compatible = "allwinner,sun4i-a10-apb1-gates-clk", .data = &sun4i_apb1_gates_data,},
  912. {.compatible = "allwinner,sun5i-a10s-apb1-gates-clk", .data = &sun5i_a10s_apb1_gates_data,},
  913. {.compatible = "allwinner,sun5i-a13-apb1-gates-clk", .data = &sun5i_a13_apb1_gates_data,},
  914. {.compatible = "allwinner,sun6i-a31-apb1-gates-clk", .data = &sun6i_a31_apb1_gates_data,},
  915. {.compatible = "allwinner,sun7i-a20-apb1-gates-clk", .data = &sun7i_a20_apb1_gates_data,},
  916. {.compatible = "allwinner,sun8i-a23-apb1-gates-clk", .data = &sun8i_a23_apb1_gates_data,},
  917. {.compatible = "allwinner,sun9i-a80-apb1-gates-clk", .data = &sun9i_a80_apb1_gates_data,},
  918. {.compatible = "allwinner,sun6i-a31-apb2-gates-clk", .data = &sun6i_a31_apb2_gates_data,},
  919. {.compatible = "allwinner,sun8i-a23-apb2-gates-clk", .data = &sun8i_a23_apb2_gates_data,},
  920. {.compatible = "allwinner,sun4i-a10-usb-clk", .data = &sun4i_a10_usb_gates_data,},
  921. {.compatible = "allwinner,sun5i-a13-usb-clk", .data = &sun5i_a13_usb_gates_data,},
  922. {.compatible = "allwinner,sun6i-a31-usb-clk", .data = &sun6i_a31_usb_gates_data,},
  923. {}
  924. };
  925. static void __init of_sunxi_table_clock_setup(const struct of_device_id *clk_match,
  926. void *function)
  927. {
  928. struct device_node *np;
  929. const struct div_data *data;
  930. const struct of_device_id *match;
  931. void (*setup_function)(struct device_node *, const void *) = function;
  932. for_each_matching_node_and_match(np, clk_match, &match) {
  933. data = match->data;
  934. setup_function(np, data);
  935. }
  936. }
  937. static void __init sunxi_init_clocks(const char *clocks[], int nclocks)
  938. {
  939. unsigned int i;
  940. /* Register factor clocks */
  941. of_sunxi_table_clock_setup(clk_factors_match, sunxi_factors_clk_setup);
  942. /* Register divider clocks */
  943. of_sunxi_table_clock_setup(clk_div_match, sunxi_divider_clk_setup);
  944. /* Register divided output clocks */
  945. of_sunxi_table_clock_setup(clk_divs_match, sunxi_divs_clk_setup);
  946. /* Register mux clocks */
  947. of_sunxi_table_clock_setup(clk_mux_match, sunxi_mux_clk_setup);
  948. /* Register gate clocks */
  949. of_sunxi_table_clock_setup(clk_gates_match, sunxi_gates_clk_setup);
  950. /* Protect the clocks that needs to stay on */
  951. for (i = 0; i < nclocks; i++) {
  952. struct clk *clk = clk_get(NULL, clocks[i]);
  953. if (!IS_ERR(clk))
  954. clk_prepare_enable(clk);
  955. }
  956. }
  957. static const char *sun4i_a10_critical_clocks[] __initdata = {
  958. "pll5_ddr",
  959. "ahb_sdram",
  960. };
  961. static void __init sun4i_a10_init_clocks(struct device_node *node)
  962. {
  963. sunxi_init_clocks(sun4i_a10_critical_clocks,
  964. ARRAY_SIZE(sun4i_a10_critical_clocks));
  965. }
  966. CLK_OF_DECLARE(sun4i_a10_clk_init, "allwinner,sun4i-a10", sun4i_a10_init_clocks);
  967. static const char *sun5i_critical_clocks[] __initdata = {
  968. "pll5_ddr",
  969. "ahb_sdram",
  970. };
  971. static void __init sun5i_init_clocks(struct device_node *node)
  972. {
  973. sunxi_init_clocks(sun5i_critical_clocks,
  974. ARRAY_SIZE(sun5i_critical_clocks));
  975. }
  976. CLK_OF_DECLARE(sun5i_a10s_clk_init, "allwinner,sun5i-a10s", sun5i_init_clocks);
  977. CLK_OF_DECLARE(sun5i_a13_clk_init, "allwinner,sun5i-a13", sun5i_init_clocks);
  978. CLK_OF_DECLARE(sun7i_a20_clk_init, "allwinner,sun7i-a20", sun5i_init_clocks);
  979. static const char *sun6i_critical_clocks[] __initdata = {
  980. "cpu",
  981. "ahb1_sdram",
  982. };
  983. static void __init sun6i_init_clocks(struct device_node *node)
  984. {
  985. sunxi_init_clocks(sun6i_critical_clocks,
  986. ARRAY_SIZE(sun6i_critical_clocks));
  987. }
  988. CLK_OF_DECLARE(sun6i_a31_clk_init, "allwinner,sun6i-a31", sun6i_init_clocks);
  989. CLK_OF_DECLARE(sun8i_a23_clk_init, "allwinner,sun8i-a23", sun6i_init_clocks);
  990. static void __init sun9i_init_clocks(struct device_node *node)
  991. {
  992. sunxi_init_clocks(NULL, 0);
  993. }
  994. CLK_OF_DECLARE(sun9i_a80_clk_init, "allwinner,sun9i-a80", sun9i_init_clocks);