clock.c 27 KB

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