clock.c 27 KB

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