clock.c 27 KB

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