clock.c 27 KB

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