clock.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093
  1. /*
  2. * Alchemy clocks.
  3. *
  4. * Exposes all configurable internal clock sources to the clk framework.
  5. *
  6. * We have:
  7. * - Root source, usually 12MHz supplied by an external crystal
  8. * - 3 PLLs which generate multiples of root rate [AUX, CPU, AUX2]
  9. *
  10. * Dividers:
  11. * - 6 clock dividers with:
  12. * * selectable source [one of the PLLs],
  13. * * output divided between [2 .. 512 in steps of 2] (!Au1300)
  14. * or [1 .. 256 in steps of 1] (Au1300),
  15. * * can be enabled individually.
  16. *
  17. * - up to 6 "internal" (fixed) consumers which:
  18. * * take either AUXPLL or one of the above 6 dividers as input,
  19. * * divide this input by 1, 2, or 4 (and 3 on Au1300).
  20. * * can be disabled separately.
  21. *
  22. * Misc clocks:
  23. * - sysbus clock: CPU core clock (CPUPLL) divided by 2, 3 or 4.
  24. * depends on board design and should be set by bootloader, read-only.
  25. * - peripheral clock: half the rate of sysbus clock, source for a lot
  26. * of peripheral blocks, read-only.
  27. * - memory clock: clk rate to main memory chips, depends on board
  28. * design and is read-only,
  29. * - lrclk: the static bus clock signal for synchronous operation.
  30. * depends on board design, must be set by bootloader,
  31. * but may be required to correctly configure devices attached to
  32. * the static bus. The Au1000/1500/1100 manuals call it LCLK, on
  33. * later models it's called RCLK.
  34. */
  35. #include <linux/init.h>
  36. #include <linux/io.h>
  37. #include <linux/clk-provider.h>
  38. #include <linux/clkdev.h>
  39. #include <linux/slab.h>
  40. #include <linux/spinlock.h>
  41. #include <linux/types.h>
  42. #include <asm/mach-au1x00/au1000.h>
  43. /* Base clock: 12MHz is the default in all databooks, and I haven't
  44. * found any board yet which uses a different rate.
  45. */
  46. #define ALCHEMY_ROOTCLK_RATE 12000000
  47. /*
  48. * the internal sources which can be driven by the PLLs and dividers.
  49. * Names taken from the databooks, refer to them for more information,
  50. * especially which ones are share a clock line.
  51. */
  52. static const char * const alchemy_au1300_intclknames[] = {
  53. "lcd_intclk", "gpemgp_clk", "maempe_clk", "maebsa_clk",
  54. "EXTCLK0", "EXTCLK1"
  55. };
  56. static const char * const alchemy_au1200_intclknames[] = {
  57. "lcd_intclk", NULL, NULL, NULL, "EXTCLK0", "EXTCLK1"
  58. };
  59. static const char * const alchemy_au1550_intclknames[] = {
  60. "usb_clk", "psc0_intclk", "psc1_intclk", "pci_clko",
  61. "EXTCLK0", "EXTCLK1"
  62. };
  63. static const char * const alchemy_au1100_intclknames[] = {
  64. "usb_clk", "lcd_intclk", NULL, "i2s_clk", "EXTCLK0", "EXTCLK1"
  65. };
  66. static const char * const alchemy_au1500_intclknames[] = {
  67. NULL, "usbd_clk", "usbh_clk", "pci_clko", "EXTCLK0", "EXTCLK1"
  68. };
  69. static const char * const alchemy_au1000_intclknames[] = {
  70. "irda_clk", "usbd_clk", "usbh_clk", "i2s_clk", "EXTCLK0",
  71. "EXTCLK1"
  72. };
  73. /* aliases for a few on-chip sources which are either shared
  74. * or have gone through name changes.
  75. */
  76. static struct clk_aliastable {
  77. char *alias;
  78. char *base;
  79. int cputype;
  80. } alchemy_clk_aliases[] __initdata = {
  81. { "usbh_clk", "usb_clk", ALCHEMY_CPU_AU1100 },
  82. { "usbd_clk", "usb_clk", ALCHEMY_CPU_AU1100 },
  83. { "irda_clk", "usb_clk", ALCHEMY_CPU_AU1100 },
  84. { "usbh_clk", "usb_clk", ALCHEMY_CPU_AU1550 },
  85. { "usbd_clk", "usb_clk", ALCHEMY_CPU_AU1550 },
  86. { "psc2_intclk", "usb_clk", ALCHEMY_CPU_AU1550 },
  87. { "psc3_intclk", "EXTCLK0", ALCHEMY_CPU_AU1550 },
  88. { "psc0_intclk", "EXTCLK0", ALCHEMY_CPU_AU1200 },
  89. { "psc1_intclk", "EXTCLK1", ALCHEMY_CPU_AU1200 },
  90. { "psc0_intclk", "EXTCLK0", ALCHEMY_CPU_AU1300 },
  91. { "psc2_intclk", "EXTCLK0", ALCHEMY_CPU_AU1300 },
  92. { "psc1_intclk", "EXTCLK1", ALCHEMY_CPU_AU1300 },
  93. { "psc3_intclk", "EXTCLK1", ALCHEMY_CPU_AU1300 },
  94. { NULL, NULL, 0 },
  95. };
  96. #define IOMEM(x) ((void __iomem *)(KSEG1ADDR(CPHYSADDR(x))))
  97. /* access locks to SYS_FREQCTRL0/1 and SYS_CLKSRC registers */
  98. static spinlock_t alchemy_clk_fg0_lock;
  99. static spinlock_t alchemy_clk_fg1_lock;
  100. static spinlock_t alchemy_clk_csrc_lock;
  101. /* CPU Core clock *****************************************************/
  102. static unsigned long alchemy_clk_cpu_recalc(struct clk_hw *hw,
  103. unsigned long parent_rate)
  104. {
  105. unsigned long t;
  106. /*
  107. * On early Au1000, sys_cpupll was write-only. Since these
  108. * silicon versions of Au1000 are not sold, we don't bend
  109. * over backwards trying to determine the frequency.
  110. */
  111. if (unlikely(au1xxx_cpu_has_pll_wo()))
  112. t = 396000000;
  113. else {
  114. t = alchemy_rdsys(AU1000_SYS_CPUPLL) & 0x7f;
  115. t *= parent_rate;
  116. }
  117. return t;
  118. }
  119. static struct clk_ops alchemy_clkops_cpu = {
  120. .recalc_rate = alchemy_clk_cpu_recalc,
  121. };
  122. static struct clk __init *alchemy_clk_setup_cpu(const char *parent_name,
  123. int ctype)
  124. {
  125. struct clk_init_data id;
  126. struct clk_hw *h;
  127. h = kzalloc(sizeof(*h), GFP_KERNEL);
  128. if (!h)
  129. return ERR_PTR(-ENOMEM);
  130. id.name = ALCHEMY_CPU_CLK;
  131. id.parent_names = &parent_name;
  132. id.num_parents = 1;
  133. id.flags = CLK_IS_BASIC;
  134. id.ops = &alchemy_clkops_cpu;
  135. h->init = &id;
  136. return clk_register(NULL, h);
  137. }
  138. /* AUXPLLs ************************************************************/
  139. struct alchemy_auxpll_clk {
  140. struct clk_hw hw;
  141. unsigned long reg; /* au1300 has also AUXPLL2 */
  142. int maxmult; /* max multiplier */
  143. };
  144. #define to_auxpll_clk(x) container_of(x, struct alchemy_auxpll_clk, hw)
  145. static unsigned long alchemy_clk_aux_recalc(struct clk_hw *hw,
  146. unsigned long parent_rate)
  147. {
  148. struct alchemy_auxpll_clk *a = to_auxpll_clk(hw);
  149. return (alchemy_rdsys(a->reg) & 0xff) * parent_rate;
  150. }
  151. static int alchemy_clk_aux_setr(struct clk_hw *hw,
  152. unsigned long rate,
  153. unsigned long parent_rate)
  154. {
  155. struct alchemy_auxpll_clk *a = to_auxpll_clk(hw);
  156. unsigned long d = rate;
  157. if (rate)
  158. d /= parent_rate;
  159. else
  160. d = 0;
  161. /* minimum is 84MHz, max is 756-1032 depending on variant */
  162. if (((d < 7) && (d != 0)) || (d > a->maxmult))
  163. return -EINVAL;
  164. alchemy_wrsys(d, a->reg);
  165. return 0;
  166. }
  167. static long alchemy_clk_aux_roundr(struct clk_hw *hw,
  168. unsigned long rate,
  169. unsigned long *parent_rate)
  170. {
  171. struct alchemy_auxpll_clk *a = to_auxpll_clk(hw);
  172. unsigned long mult;
  173. if (!rate || !*parent_rate)
  174. return 0;
  175. mult = rate / (*parent_rate);
  176. if (mult && (mult < 7))
  177. mult = 7;
  178. if (mult > a->maxmult)
  179. mult = a->maxmult;
  180. return (*parent_rate) * mult;
  181. }
  182. static struct clk_ops alchemy_clkops_aux = {
  183. .recalc_rate = alchemy_clk_aux_recalc,
  184. .set_rate = alchemy_clk_aux_setr,
  185. .round_rate = alchemy_clk_aux_roundr,
  186. };
  187. static struct clk __init *alchemy_clk_setup_aux(const char *parent_name,
  188. char *name, int maxmult,
  189. unsigned long reg)
  190. {
  191. struct clk_init_data id;
  192. struct clk *c;
  193. struct alchemy_auxpll_clk *a;
  194. a = kzalloc(sizeof(*a), GFP_KERNEL);
  195. if (!a)
  196. return ERR_PTR(-ENOMEM);
  197. id.name = name;
  198. id.parent_names = &parent_name;
  199. id.num_parents = 1;
  200. id.flags = CLK_GET_RATE_NOCACHE;
  201. id.ops = &alchemy_clkops_aux;
  202. a->reg = reg;
  203. a->maxmult = maxmult;
  204. a->hw.init = &id;
  205. c = clk_register(NULL, &a->hw);
  206. if (!IS_ERR(c))
  207. clk_register_clkdev(c, name, NULL);
  208. else
  209. kfree(a);
  210. return c;
  211. }
  212. /* sysbus_clk *********************************************************/
  213. static struct clk __init *alchemy_clk_setup_sysbus(const char *pn)
  214. {
  215. unsigned long v = (alchemy_rdsys(AU1000_SYS_POWERCTRL) & 3) + 2;
  216. struct clk *c;
  217. c = clk_register_fixed_factor(NULL, ALCHEMY_SYSBUS_CLK,
  218. pn, 0, 1, v);
  219. if (!IS_ERR(c))
  220. clk_register_clkdev(c, ALCHEMY_SYSBUS_CLK, NULL);
  221. return c;
  222. }
  223. /* Peripheral Clock ***************************************************/
  224. static struct clk __init *alchemy_clk_setup_periph(const char *pn)
  225. {
  226. /* Peripheral clock runs at half the rate of sysbus clk */
  227. struct clk *c;
  228. c = clk_register_fixed_factor(NULL, ALCHEMY_PERIPH_CLK,
  229. pn, 0, 1, 2);
  230. if (!IS_ERR(c))
  231. clk_register_clkdev(c, ALCHEMY_PERIPH_CLK, NULL);
  232. return c;
  233. }
  234. /* mem clock **********************************************************/
  235. static struct clk __init *alchemy_clk_setup_mem(const char *pn, int ct)
  236. {
  237. void __iomem *addr = IOMEM(AU1000_MEM_PHYS_ADDR);
  238. unsigned long v;
  239. struct clk *c;
  240. int div;
  241. switch (ct) {
  242. case ALCHEMY_CPU_AU1550:
  243. case ALCHEMY_CPU_AU1200:
  244. v = __raw_readl(addr + AU1550_MEM_SDCONFIGB);
  245. div = (v & (1 << 15)) ? 1 : 2;
  246. break;
  247. case ALCHEMY_CPU_AU1300:
  248. v = __raw_readl(addr + AU1550_MEM_SDCONFIGB);
  249. div = (v & (1 << 31)) ? 1 : 2;
  250. break;
  251. case ALCHEMY_CPU_AU1000:
  252. case ALCHEMY_CPU_AU1500:
  253. case ALCHEMY_CPU_AU1100:
  254. default:
  255. div = 2;
  256. break;
  257. }
  258. c = clk_register_fixed_factor(NULL, ALCHEMY_MEM_CLK, pn,
  259. 0, 1, div);
  260. if (!IS_ERR(c))
  261. clk_register_clkdev(c, ALCHEMY_MEM_CLK, NULL);
  262. return c;
  263. }
  264. /* lrclk: external synchronous static bus clock ***********************/
  265. static struct clk __init *alchemy_clk_setup_lrclk(const char *pn)
  266. {
  267. /* MEM_STCFG0[15:13] = divisor.
  268. * L/RCLK = periph_clk / (divisor + 1)
  269. * On Au1000, Au1500, Au1100 it's called LCLK,
  270. * on later models it's called RCLK, but it's the same thing.
  271. */
  272. struct clk *c;
  273. unsigned long v = alchemy_rdsmem(AU1000_MEM_STCFG0) >> 13;
  274. v = (v & 7) + 1;
  275. c = clk_register_fixed_factor(NULL, ALCHEMY_LR_CLK,
  276. pn, 0, 1, v);
  277. if (!IS_ERR(c))
  278. clk_register_clkdev(c, ALCHEMY_LR_CLK, NULL);
  279. return c;
  280. }
  281. /* Clock dividers and muxes *******************************************/
  282. /* data for fgen and csrc mux-dividers */
  283. struct alchemy_fgcs_clk {
  284. struct clk_hw hw;
  285. spinlock_t *reglock; /* register lock */
  286. unsigned long reg; /* SYS_FREQCTRL0/1 */
  287. int shift; /* offset in register */
  288. int parent; /* parent before disable [Au1300] */
  289. int isen; /* is it enabled? */
  290. int *dt; /* dividertable for csrc */
  291. };
  292. #define to_fgcs_clk(x) container_of(x, struct alchemy_fgcs_clk, hw)
  293. static long alchemy_calc_div(unsigned long rate, unsigned long prate,
  294. int scale, int maxdiv, unsigned long *rv)
  295. {
  296. long div1, div2;
  297. div1 = prate / rate;
  298. if ((prate / div1) > rate)
  299. div1++;
  300. if (scale == 2) { /* only div-by-multiple-of-2 possible */
  301. if (div1 & 1)
  302. div1++; /* stay <=prate */
  303. }
  304. div2 = (div1 / scale) - 1; /* value to write to register */
  305. if (div2 > maxdiv)
  306. div2 = maxdiv;
  307. if (rv)
  308. *rv = div2;
  309. div1 = ((div2 + 1) * scale);
  310. return div1;
  311. }
  312. static long alchemy_clk_fgcs_detr(struct clk_hw *hw, unsigned long rate,
  313. unsigned long *best_parent_rate,
  314. struct clk_hw **best_parent_clk,
  315. int scale, int maxdiv)
  316. {
  317. struct clk *pc, *bpc, *free;
  318. long tdv, tpr, pr, nr, br, bpr, diff, lastdiff;
  319. int j;
  320. lastdiff = INT_MAX;
  321. bpr = 0;
  322. bpc = NULL;
  323. br = -EINVAL;
  324. free = NULL;
  325. /* look at the rates each enabled parent supplies and select
  326. * the one that gets closest to but not over the requested rate.
  327. */
  328. for (j = 0; j < 7; j++) {
  329. pc = clk_get_parent_by_index(hw->clk, j);
  330. if (!pc)
  331. break;
  332. /* if this parent is currently unused, remember it.
  333. * XXX: we would actually want clk_has_active_children()
  334. * but this is a good-enough approximation for now.
  335. */
  336. if (!__clk_is_prepared(pc)) {
  337. if (!free)
  338. free = pc;
  339. }
  340. pr = clk_get_rate(pc);
  341. if (pr < rate)
  342. continue;
  343. /* what can hardware actually provide */
  344. tdv = alchemy_calc_div(rate, pr, scale, maxdiv, NULL);
  345. nr = pr / tdv;
  346. diff = rate - nr;
  347. if (nr > rate)
  348. continue;
  349. if (diff < lastdiff) {
  350. lastdiff = diff;
  351. bpr = pr;
  352. bpc = pc;
  353. br = nr;
  354. }
  355. if (diff == 0)
  356. break;
  357. }
  358. /* if we couldn't get the exact rate we wanted from the enabled
  359. * parents, maybe we can tell an available disabled/inactive one
  360. * to give us a rate we can divide down to the requested rate.
  361. */
  362. if (lastdiff && free) {
  363. for (j = (maxdiv == 4) ? 1 : scale; j <= maxdiv; j += scale) {
  364. tpr = rate * j;
  365. if (tpr < 0)
  366. break;
  367. pr = clk_round_rate(free, tpr);
  368. tdv = alchemy_calc_div(rate, pr, scale, maxdiv, NULL);
  369. nr = pr / tdv;
  370. diff = rate - nr;
  371. if (nr > rate)
  372. continue;
  373. if (diff < lastdiff) {
  374. lastdiff = diff;
  375. bpr = pr;
  376. bpc = free;
  377. br = nr;
  378. }
  379. if (diff == 0)
  380. break;
  381. }
  382. }
  383. *best_parent_rate = bpr;
  384. *best_parent_clk = __clk_get_hw(bpc);
  385. return br;
  386. }
  387. static int alchemy_clk_fgv1_en(struct clk_hw *hw)
  388. {
  389. struct alchemy_fgcs_clk *c = to_fgcs_clk(hw);
  390. unsigned long v, flags;
  391. spin_lock_irqsave(c->reglock, flags);
  392. v = alchemy_rdsys(c->reg);
  393. v |= (1 << 1) << c->shift;
  394. alchemy_wrsys(v, c->reg);
  395. spin_unlock_irqrestore(c->reglock, flags);
  396. return 0;
  397. }
  398. static int alchemy_clk_fgv1_isen(struct clk_hw *hw)
  399. {
  400. struct alchemy_fgcs_clk *c = to_fgcs_clk(hw);
  401. unsigned long v = alchemy_rdsys(c->reg) >> (c->shift + 1);
  402. return v & 1;
  403. }
  404. static void alchemy_clk_fgv1_dis(struct clk_hw *hw)
  405. {
  406. struct alchemy_fgcs_clk *c = to_fgcs_clk(hw);
  407. unsigned long v, flags;
  408. spin_lock_irqsave(c->reglock, flags);
  409. v = alchemy_rdsys(c->reg);
  410. v &= ~((1 << 1) << c->shift);
  411. alchemy_wrsys(v, c->reg);
  412. spin_unlock_irqrestore(c->reglock, flags);
  413. }
  414. static int alchemy_clk_fgv1_setp(struct clk_hw *hw, u8 index)
  415. {
  416. struct alchemy_fgcs_clk *c = to_fgcs_clk(hw);
  417. unsigned long v, flags;
  418. spin_lock_irqsave(c->reglock, flags);
  419. v = alchemy_rdsys(c->reg);
  420. if (index)
  421. v |= (1 << c->shift);
  422. else
  423. v &= ~(1 << c->shift);
  424. alchemy_wrsys(v, c->reg);
  425. spin_unlock_irqrestore(c->reglock, flags);
  426. return 0;
  427. }
  428. static u8 alchemy_clk_fgv1_getp(struct clk_hw *hw)
  429. {
  430. struct alchemy_fgcs_clk *c = to_fgcs_clk(hw);
  431. return (alchemy_rdsys(c->reg) >> c->shift) & 1;
  432. }
  433. static int alchemy_clk_fgv1_setr(struct clk_hw *hw, unsigned long rate,
  434. unsigned long parent_rate)
  435. {
  436. struct alchemy_fgcs_clk *c = to_fgcs_clk(hw);
  437. unsigned long div, v, flags, ret;
  438. int sh = c->shift + 2;
  439. if (!rate || !parent_rate || rate > (parent_rate / 2))
  440. return -EINVAL;
  441. ret = alchemy_calc_div(rate, parent_rate, 2, 512, &div);
  442. spin_lock_irqsave(c->reglock, flags);
  443. v = alchemy_rdsys(c->reg);
  444. v &= ~(0xff << sh);
  445. v |= div << sh;
  446. alchemy_wrsys(v, c->reg);
  447. spin_unlock_irqrestore(c->reglock, flags);
  448. return 0;
  449. }
  450. static unsigned long alchemy_clk_fgv1_recalc(struct clk_hw *hw,
  451. unsigned long parent_rate)
  452. {
  453. struct alchemy_fgcs_clk *c = to_fgcs_clk(hw);
  454. unsigned long v = alchemy_rdsys(c->reg) >> (c->shift + 2);
  455. v = ((v & 0xff) + 1) * 2;
  456. return parent_rate / v;
  457. }
  458. static long alchemy_clk_fgv1_detr(struct clk_hw *hw, unsigned long rate,
  459. unsigned long *best_parent_rate,
  460. struct clk_hw **best_parent_clk)
  461. {
  462. return alchemy_clk_fgcs_detr(hw, rate, best_parent_rate,
  463. best_parent_clk, 2, 512);
  464. }
  465. /* Au1000, Au1100, Au15x0, Au12x0 */
  466. static struct clk_ops alchemy_clkops_fgenv1 = {
  467. .recalc_rate = alchemy_clk_fgv1_recalc,
  468. .determine_rate = alchemy_clk_fgv1_detr,
  469. .set_rate = alchemy_clk_fgv1_setr,
  470. .set_parent = alchemy_clk_fgv1_setp,
  471. .get_parent = alchemy_clk_fgv1_getp,
  472. .enable = alchemy_clk_fgv1_en,
  473. .disable = alchemy_clk_fgv1_dis,
  474. .is_enabled = alchemy_clk_fgv1_isen,
  475. };
  476. static void __alchemy_clk_fgv2_en(struct alchemy_fgcs_clk *c)
  477. {
  478. unsigned long v = alchemy_rdsys(c->reg);
  479. v &= ~(3 << c->shift);
  480. v |= (c->parent & 3) << c->shift;
  481. alchemy_wrsys(v, c->reg);
  482. c->isen = 1;
  483. }
  484. static int alchemy_clk_fgv2_en(struct clk_hw *hw)
  485. {
  486. struct alchemy_fgcs_clk *c = to_fgcs_clk(hw);
  487. unsigned long flags;
  488. /* enable by setting the previous parent clock */
  489. spin_lock_irqsave(c->reglock, flags);
  490. __alchemy_clk_fgv2_en(c);
  491. spin_unlock_irqrestore(c->reglock, flags);
  492. return 0;
  493. }
  494. static int alchemy_clk_fgv2_isen(struct clk_hw *hw)
  495. {
  496. struct alchemy_fgcs_clk *c = to_fgcs_clk(hw);
  497. return ((alchemy_rdsys(c->reg) >> c->shift) & 3) != 0;
  498. }
  499. static void alchemy_clk_fgv2_dis(struct clk_hw *hw)
  500. {
  501. struct alchemy_fgcs_clk *c = to_fgcs_clk(hw);
  502. unsigned long v, flags;
  503. spin_lock_irqsave(c->reglock, flags);
  504. v = alchemy_rdsys(c->reg);
  505. v &= ~(3 << c->shift); /* set input mux to "disabled" state */
  506. alchemy_wrsys(v, c->reg);
  507. c->isen = 0;
  508. spin_unlock_irqrestore(c->reglock, flags);
  509. }
  510. static int alchemy_clk_fgv2_setp(struct clk_hw *hw, u8 index)
  511. {
  512. struct alchemy_fgcs_clk *c = to_fgcs_clk(hw);
  513. unsigned long flags;
  514. spin_lock_irqsave(c->reglock, flags);
  515. c->parent = index + 1; /* value to write to register */
  516. if (c->isen)
  517. __alchemy_clk_fgv2_en(c);
  518. spin_unlock_irqrestore(c->reglock, flags);
  519. return 0;
  520. }
  521. static u8 alchemy_clk_fgv2_getp(struct clk_hw *hw)
  522. {
  523. struct alchemy_fgcs_clk *c = to_fgcs_clk(hw);
  524. unsigned long flags, v;
  525. spin_lock_irqsave(c->reglock, flags);
  526. v = c->parent - 1;
  527. spin_unlock_irqrestore(c->reglock, flags);
  528. return v;
  529. }
  530. /* fg0-2 and fg4-6 share a "scale"-bit. With this bit cleared, the
  531. * dividers behave exactly as on previous models (dividers are multiples
  532. * of 2); with the bit set, dividers are multiples of 1, halving their
  533. * range, but making them also much more flexible.
  534. */
  535. static int alchemy_clk_fgv2_setr(struct clk_hw *hw, unsigned long rate,
  536. unsigned long parent_rate)
  537. {
  538. struct alchemy_fgcs_clk *c = to_fgcs_clk(hw);
  539. int sh = c->shift + 2;
  540. unsigned long div, v, flags, ret;
  541. if (!rate || !parent_rate || rate > parent_rate)
  542. return -EINVAL;
  543. v = alchemy_rdsys(c->reg) & (1 << 30); /* test "scale" bit */
  544. ret = alchemy_calc_div(rate, parent_rate, v ? 1 : 2,
  545. v ? 256 : 512, &div);
  546. spin_lock_irqsave(c->reglock, flags);
  547. v = alchemy_rdsys(c->reg);
  548. v &= ~(0xff << sh);
  549. v |= (div & 0xff) << sh;
  550. alchemy_wrsys(v, c->reg);
  551. spin_unlock_irqrestore(c->reglock, flags);
  552. return 0;
  553. }
  554. static unsigned long alchemy_clk_fgv2_recalc(struct clk_hw *hw,
  555. unsigned long parent_rate)
  556. {
  557. struct alchemy_fgcs_clk *c = to_fgcs_clk(hw);
  558. int sh = c->shift + 2;
  559. unsigned long v, t;
  560. v = alchemy_rdsys(c->reg);
  561. t = parent_rate / (((v >> sh) & 0xff) + 1);
  562. if ((v & (1 << 30)) == 0) /* test scale bit */
  563. t /= 2;
  564. return t;
  565. }
  566. static long alchemy_clk_fgv2_detr(struct clk_hw *hw, unsigned long rate,
  567. unsigned long *best_parent_rate,
  568. struct clk_hw **best_parent_clk)
  569. {
  570. struct alchemy_fgcs_clk *c = to_fgcs_clk(hw);
  571. int scale, maxdiv;
  572. if (alchemy_rdsys(c->reg) & (1 << 30)) {
  573. scale = 1;
  574. maxdiv = 256;
  575. } else {
  576. scale = 2;
  577. maxdiv = 512;
  578. }
  579. return alchemy_clk_fgcs_detr(hw, rate, best_parent_rate,
  580. best_parent_clk, scale, maxdiv);
  581. }
  582. /* Au1300 larger input mux, no separate disable bit, flexible divider */
  583. static struct clk_ops alchemy_clkops_fgenv2 = {
  584. .recalc_rate = alchemy_clk_fgv2_recalc,
  585. .determine_rate = alchemy_clk_fgv2_detr,
  586. .set_rate = alchemy_clk_fgv2_setr,
  587. .set_parent = alchemy_clk_fgv2_setp,
  588. .get_parent = alchemy_clk_fgv2_getp,
  589. .enable = alchemy_clk_fgv2_en,
  590. .disable = alchemy_clk_fgv2_dis,
  591. .is_enabled = alchemy_clk_fgv2_isen,
  592. };
  593. static const char * const alchemy_clk_fgv1_parents[] = {
  594. ALCHEMY_CPU_CLK, ALCHEMY_AUXPLL_CLK
  595. };
  596. static const char * const alchemy_clk_fgv2_parents[] = {
  597. ALCHEMY_AUXPLL2_CLK, ALCHEMY_CPU_CLK, ALCHEMY_AUXPLL_CLK
  598. };
  599. static const char * const alchemy_clk_fgen_names[] = {
  600. ALCHEMY_FG0_CLK, ALCHEMY_FG1_CLK, ALCHEMY_FG2_CLK,
  601. ALCHEMY_FG3_CLK, ALCHEMY_FG4_CLK, ALCHEMY_FG5_CLK };
  602. static int __init alchemy_clk_init_fgens(int ctype)
  603. {
  604. struct clk *c;
  605. struct clk_init_data id;
  606. struct alchemy_fgcs_clk *a;
  607. unsigned long v;
  608. int i, ret;
  609. switch (ctype) {
  610. case ALCHEMY_CPU_AU1000...ALCHEMY_CPU_AU1200:
  611. id.ops = &alchemy_clkops_fgenv1;
  612. id.parent_names = (const char **)alchemy_clk_fgv1_parents;
  613. id.num_parents = 2;
  614. break;
  615. case ALCHEMY_CPU_AU1300:
  616. id.ops = &alchemy_clkops_fgenv2;
  617. id.parent_names = (const char **)alchemy_clk_fgv2_parents;
  618. id.num_parents = 3;
  619. break;
  620. default:
  621. return -ENODEV;
  622. }
  623. id.flags = CLK_SET_RATE_PARENT | CLK_GET_RATE_NOCACHE;
  624. a = kzalloc((sizeof(*a)) * 6, GFP_KERNEL);
  625. if (!a)
  626. return -ENOMEM;
  627. spin_lock_init(&alchemy_clk_fg0_lock);
  628. spin_lock_init(&alchemy_clk_fg1_lock);
  629. ret = 0;
  630. for (i = 0; i < 6; i++) {
  631. id.name = alchemy_clk_fgen_names[i];
  632. a->shift = 10 * (i < 3 ? i : i - 3);
  633. if (i > 2) {
  634. a->reg = AU1000_SYS_FREQCTRL1;
  635. a->reglock = &alchemy_clk_fg1_lock;
  636. } else {
  637. a->reg = AU1000_SYS_FREQCTRL0;
  638. a->reglock = &alchemy_clk_fg0_lock;
  639. }
  640. /* default to first parent if bootloader has set
  641. * the mux to disabled state.
  642. */
  643. if (ctype == ALCHEMY_CPU_AU1300) {
  644. v = alchemy_rdsys(a->reg);
  645. a->parent = (v >> a->shift) & 3;
  646. if (!a->parent) {
  647. a->parent = 1;
  648. a->isen = 0;
  649. } else
  650. a->isen = 1;
  651. }
  652. a->hw.init = &id;
  653. c = clk_register(NULL, &a->hw);
  654. if (IS_ERR(c))
  655. ret++;
  656. else
  657. clk_register_clkdev(c, id.name, NULL);
  658. a++;
  659. }
  660. return ret;
  661. }
  662. /* internal sources muxes *********************************************/
  663. static int alchemy_clk_csrc_isen(struct clk_hw *hw)
  664. {
  665. struct alchemy_fgcs_clk *c = to_fgcs_clk(hw);
  666. unsigned long v = alchemy_rdsys(c->reg);
  667. return (((v >> c->shift) >> 2) & 7) != 0;
  668. }
  669. static void __alchemy_clk_csrc_en(struct alchemy_fgcs_clk *c)
  670. {
  671. unsigned long v = alchemy_rdsys(c->reg);
  672. v &= ~((7 << 2) << c->shift);
  673. v |= ((c->parent & 7) << 2) << c->shift;
  674. alchemy_wrsys(v, c->reg);
  675. c->isen = 1;
  676. }
  677. static int alchemy_clk_csrc_en(struct clk_hw *hw)
  678. {
  679. struct alchemy_fgcs_clk *c = to_fgcs_clk(hw);
  680. unsigned long flags;
  681. /* enable by setting the previous parent clock */
  682. spin_lock_irqsave(c->reglock, flags);
  683. __alchemy_clk_csrc_en(c);
  684. spin_unlock_irqrestore(c->reglock, flags);
  685. return 0;
  686. }
  687. static void alchemy_clk_csrc_dis(struct clk_hw *hw)
  688. {
  689. struct alchemy_fgcs_clk *c = to_fgcs_clk(hw);
  690. unsigned long v, flags;
  691. spin_lock_irqsave(c->reglock, flags);
  692. v = alchemy_rdsys(c->reg);
  693. v &= ~((3 << 2) << c->shift); /* mux to "disabled" state */
  694. alchemy_wrsys(v, c->reg);
  695. c->isen = 0;
  696. spin_unlock_irqrestore(c->reglock, flags);
  697. }
  698. static int alchemy_clk_csrc_setp(struct clk_hw *hw, u8 index)
  699. {
  700. struct alchemy_fgcs_clk *c = to_fgcs_clk(hw);
  701. unsigned long flags;
  702. spin_lock_irqsave(c->reglock, flags);
  703. c->parent = index + 1; /* value to write to register */
  704. if (c->isen)
  705. __alchemy_clk_csrc_en(c);
  706. spin_unlock_irqrestore(c->reglock, flags);
  707. return 0;
  708. }
  709. static u8 alchemy_clk_csrc_getp(struct clk_hw *hw)
  710. {
  711. struct alchemy_fgcs_clk *c = to_fgcs_clk(hw);
  712. return c->parent - 1;
  713. }
  714. static unsigned long alchemy_clk_csrc_recalc(struct clk_hw *hw,
  715. unsigned long parent_rate)
  716. {
  717. struct alchemy_fgcs_clk *c = to_fgcs_clk(hw);
  718. unsigned long v = (alchemy_rdsys(c->reg) >> c->shift) & 3;
  719. return parent_rate / c->dt[v];
  720. }
  721. static int alchemy_clk_csrc_setr(struct clk_hw *hw, unsigned long rate,
  722. unsigned long parent_rate)
  723. {
  724. struct alchemy_fgcs_clk *c = to_fgcs_clk(hw);
  725. unsigned long d, v, flags;
  726. int i;
  727. if (!rate || !parent_rate || rate > parent_rate)
  728. return -EINVAL;
  729. d = (parent_rate + (rate / 2)) / rate;
  730. if (d > 4)
  731. return -EINVAL;
  732. if ((d == 3) && (c->dt[2] != 3))
  733. d = 4;
  734. for (i = 0; i < 4; i++)
  735. if (c->dt[i] == d)
  736. break;
  737. if (i >= 4)
  738. return -EINVAL; /* oops */
  739. spin_lock_irqsave(c->reglock, flags);
  740. v = alchemy_rdsys(c->reg);
  741. v &= ~(3 << c->shift);
  742. v |= (i & 3) << c->shift;
  743. alchemy_wrsys(v, c->reg);
  744. spin_unlock_irqrestore(c->reglock, flags);
  745. return 0;
  746. }
  747. static long alchemy_clk_csrc_detr(struct clk_hw *hw, unsigned long rate,
  748. unsigned long *best_parent_rate,
  749. struct clk_hw **best_parent_clk)
  750. {
  751. struct alchemy_fgcs_clk *c = to_fgcs_clk(hw);
  752. int scale = c->dt[2] == 3 ? 1 : 2; /* au1300 check */
  753. return alchemy_clk_fgcs_detr(hw, rate, best_parent_rate,
  754. best_parent_clk, scale, 4);
  755. }
  756. static struct clk_ops alchemy_clkops_csrc = {
  757. .recalc_rate = alchemy_clk_csrc_recalc,
  758. .determine_rate = alchemy_clk_csrc_detr,
  759. .set_rate = alchemy_clk_csrc_setr,
  760. .set_parent = alchemy_clk_csrc_setp,
  761. .get_parent = alchemy_clk_csrc_getp,
  762. .enable = alchemy_clk_csrc_en,
  763. .disable = alchemy_clk_csrc_dis,
  764. .is_enabled = alchemy_clk_csrc_isen,
  765. };
  766. static const char * const alchemy_clk_csrc_parents[] = {
  767. /* disabled at index 0 */ ALCHEMY_AUXPLL_CLK,
  768. ALCHEMY_FG0_CLK, ALCHEMY_FG1_CLK, ALCHEMY_FG2_CLK,
  769. ALCHEMY_FG3_CLK, ALCHEMY_FG4_CLK, ALCHEMY_FG5_CLK
  770. };
  771. /* divider tables */
  772. static int alchemy_csrc_dt1[] = { 1, 4, 1, 2 }; /* rest */
  773. static int alchemy_csrc_dt2[] = { 1, 4, 3, 2 }; /* Au1300 */
  774. static int __init alchemy_clk_setup_imux(int ctype)
  775. {
  776. struct alchemy_fgcs_clk *a;
  777. const char * const *names;
  778. struct clk_init_data id;
  779. unsigned long v;
  780. int i, ret, *dt;
  781. struct clk *c;
  782. id.ops = &alchemy_clkops_csrc;
  783. id.parent_names = (const char **)alchemy_clk_csrc_parents;
  784. id.num_parents = 7;
  785. id.flags = CLK_SET_RATE_PARENT | CLK_GET_RATE_NOCACHE;
  786. dt = alchemy_csrc_dt1;
  787. switch (ctype) {
  788. case ALCHEMY_CPU_AU1000:
  789. names = alchemy_au1000_intclknames;
  790. break;
  791. case ALCHEMY_CPU_AU1500:
  792. names = alchemy_au1500_intclknames;
  793. break;
  794. case ALCHEMY_CPU_AU1100:
  795. names = alchemy_au1100_intclknames;
  796. break;
  797. case ALCHEMY_CPU_AU1550:
  798. names = alchemy_au1550_intclknames;
  799. break;
  800. case ALCHEMY_CPU_AU1200:
  801. names = alchemy_au1200_intclknames;
  802. break;
  803. case ALCHEMY_CPU_AU1300:
  804. dt = alchemy_csrc_dt2;
  805. names = alchemy_au1300_intclknames;
  806. break;
  807. default:
  808. return -ENODEV;
  809. }
  810. a = kzalloc((sizeof(*a)) * 6, GFP_KERNEL);
  811. if (!a)
  812. return -ENOMEM;
  813. spin_lock_init(&alchemy_clk_csrc_lock);
  814. ret = 0;
  815. for (i = 0; i < 6; i++) {
  816. id.name = names[i];
  817. if (!id.name)
  818. goto next;
  819. a->shift = i * 5;
  820. a->reg = AU1000_SYS_CLKSRC;
  821. a->reglock = &alchemy_clk_csrc_lock;
  822. a->dt = dt;
  823. /* default to first parent clock if mux is initially
  824. * set to disabled state.
  825. */
  826. v = alchemy_rdsys(a->reg);
  827. a->parent = ((v >> a->shift) >> 2) & 7;
  828. if (!a->parent) {
  829. a->parent = 1;
  830. a->isen = 0;
  831. } else
  832. a->isen = 1;
  833. a->hw.init = &id;
  834. c = clk_register(NULL, &a->hw);
  835. if (IS_ERR(c))
  836. ret++;
  837. else
  838. clk_register_clkdev(c, id.name, NULL);
  839. next:
  840. a++;
  841. }
  842. return ret;
  843. }
  844. /**********************************************************************/
  845. #define ERRCK(x) \
  846. if (IS_ERR(x)) { \
  847. ret = PTR_ERR(x); \
  848. goto out; \
  849. }
  850. static int __init alchemy_clk_init(void)
  851. {
  852. int ctype = alchemy_get_cputype(), ret, i;
  853. struct clk_aliastable *t = alchemy_clk_aliases;
  854. struct clk *c;
  855. /* Root of the Alchemy clock tree: external 12MHz crystal osc */
  856. c = clk_register_fixed_rate(NULL, ALCHEMY_ROOT_CLK, NULL,
  857. CLK_IS_ROOT,
  858. ALCHEMY_ROOTCLK_RATE);
  859. ERRCK(c)
  860. /* CPU core clock */
  861. c = alchemy_clk_setup_cpu(ALCHEMY_ROOT_CLK, ctype);
  862. ERRCK(c)
  863. /* AUXPLLs: max 1GHz on Au1300, 748MHz on older models */
  864. i = (ctype == ALCHEMY_CPU_AU1300) ? 84 : 63;
  865. c = alchemy_clk_setup_aux(ALCHEMY_ROOT_CLK, ALCHEMY_AUXPLL_CLK,
  866. i, AU1000_SYS_AUXPLL);
  867. ERRCK(c)
  868. if (ctype == ALCHEMY_CPU_AU1300) {
  869. c = alchemy_clk_setup_aux(ALCHEMY_ROOT_CLK,
  870. ALCHEMY_AUXPLL2_CLK, i,
  871. AU1300_SYS_AUXPLL2);
  872. ERRCK(c)
  873. }
  874. /* sysbus clock: cpu core clock divided by 2, 3 or 4 */
  875. c = alchemy_clk_setup_sysbus(ALCHEMY_CPU_CLK);
  876. ERRCK(c)
  877. /* peripheral clock: runs at half rate of sysbus clk */
  878. c = alchemy_clk_setup_periph(ALCHEMY_SYSBUS_CLK);
  879. ERRCK(c)
  880. /* SDR/DDR memory clock */
  881. c = alchemy_clk_setup_mem(ALCHEMY_SYSBUS_CLK, ctype);
  882. ERRCK(c)
  883. /* L/RCLK: external static bus clock for synchronous mode */
  884. c = alchemy_clk_setup_lrclk(ALCHEMY_PERIPH_CLK);
  885. ERRCK(c)
  886. /* Frequency dividers 0-5 */
  887. ret = alchemy_clk_init_fgens(ctype);
  888. if (ret) {
  889. ret = -ENODEV;
  890. goto out;
  891. }
  892. /* diving muxes for internal sources */
  893. ret = alchemy_clk_setup_imux(ctype);
  894. if (ret) {
  895. ret = -ENODEV;
  896. goto out;
  897. }
  898. /* set up aliases drivers might look for */
  899. while (t->base) {
  900. if (t->cputype == ctype)
  901. clk_add_alias(t->alias, NULL, t->base, NULL);
  902. t++;
  903. }
  904. pr_info("Alchemy clocktree installed\n");
  905. return 0;
  906. out:
  907. return ret;
  908. }
  909. postcore_initcall(alchemy_clk_init);