clk-sunxi.c 30 KB

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