clock.c 27 KB

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