clock.c 27 KB

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