clk-sunxi.c 29 KB

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