clock.c 27 KB

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