clock-commonclk.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2013 DENX Software Engineering
  4. *
  5. * Gerhard Sittig, <[email protected]>
  6. *
  7. * common clock driver support for the MPC512x platform
  8. */
  9. #include <linux/bitops.h>
  10. #include <linux/clk.h>
  11. #include <linux/clk-provider.h>
  12. #include <linux/clkdev.h>
  13. #include <linux/device.h>
  14. #include <linux/errno.h>
  15. #include <linux/io.h>
  16. #include <linux/of.h>
  17. #include <linux/of_address.h>
  18. #include <asm/mpc5121.h>
  19. #include <dt-bindings/clock/mpc512x-clock.h>
  20. #include "mpc512x.h" /* our public mpc5121_clk_init() API */
  21. /* helpers to keep the MCLK intermediates "somewhere" in our table */
  22. enum {
  23. MCLK_IDX_MUX0,
  24. MCLK_IDX_EN0,
  25. MCLK_IDX_DIV0,
  26. MCLK_MAX_IDX,
  27. };
  28. #define NR_PSCS 12
  29. #define NR_MSCANS 4
  30. #define NR_SPDIFS 1
  31. #define NR_OUTCLK 4
  32. #define NR_MCLKS (NR_PSCS + NR_MSCANS + NR_SPDIFS + NR_OUTCLK)
  33. /* extend the public set of clocks by adding internal slots for management */
  34. enum {
  35. /* arrange for adjacent numbers after the public set */
  36. MPC512x_CLK_START_PRIVATE = MPC512x_CLK_LAST_PUBLIC,
  37. /* clocks which aren't announced to the public */
  38. MPC512x_CLK_DDR,
  39. MPC512x_CLK_MEM,
  40. MPC512x_CLK_IIM,
  41. /* intermediates in div+gate combos or fractional dividers */
  42. MPC512x_CLK_DDR_UG,
  43. MPC512x_CLK_SDHC_x4,
  44. MPC512x_CLK_SDHC_UG,
  45. MPC512x_CLK_SDHC2_UG,
  46. MPC512x_CLK_DIU_x4,
  47. MPC512x_CLK_DIU_UG,
  48. MPC512x_CLK_MBX_BUS_UG,
  49. MPC512x_CLK_MBX_UG,
  50. MPC512x_CLK_MBX_3D_UG,
  51. MPC512x_CLK_PCI_UG,
  52. MPC512x_CLK_NFC_UG,
  53. MPC512x_CLK_LPC_UG,
  54. MPC512x_CLK_SPDIF_TX_IN,
  55. /* intermediates for the mux+gate+div+mux MCLK generation */
  56. MPC512x_CLK_MCLKS_FIRST,
  57. MPC512x_CLK_MCLKS_LAST = MPC512x_CLK_MCLKS_FIRST
  58. + NR_MCLKS * MCLK_MAX_IDX,
  59. /* internal, symbolic spec for the number of slots */
  60. MPC512x_CLK_LAST_PRIVATE,
  61. };
  62. /* data required for the OF clock provider registration */
  63. static struct clk *clks[MPC512x_CLK_LAST_PRIVATE];
  64. static struct clk_onecell_data clk_data;
  65. /* CCM register access */
  66. static struct mpc512x_ccm __iomem *clkregs;
  67. static DEFINE_SPINLOCK(clklock);
  68. /* SoC variants {{{ */
  69. /*
  70. * tell SoC variants apart as they are rather similar yet not identical,
  71. * cache the result in an enum to not repeatedly run the expensive OF test
  72. *
  73. * MPC5123 is an MPC5121 without the MBX graphics accelerator
  74. *
  75. * MPC5125 has many more differences: no MBX, no AXE, no VIU, no SPDIF,
  76. * no PATA, no SATA, no PCI, two FECs (of different compatibility name),
  77. * only 10 PSCs (of different compatibility name), two SDHCs, different
  78. * NFC IP block, output clocks, system PLL status query, different CPMF
  79. * interpretation, no CFM, different fourth PSC/CAN mux0 input -- yet
  80. * those differences can get folded into this clock provider support
  81. * code and don't warrant a separate highly redundant implementation
  82. */
  83. static enum soc_type {
  84. MPC512x_SOC_MPC5121,
  85. MPC512x_SOC_MPC5123,
  86. MPC512x_SOC_MPC5125,
  87. } soc;
  88. static void __init mpc512x_clk_determine_soc(void)
  89. {
  90. if (of_machine_is_compatible("fsl,mpc5121")) {
  91. soc = MPC512x_SOC_MPC5121;
  92. return;
  93. }
  94. if (of_machine_is_compatible("fsl,mpc5123")) {
  95. soc = MPC512x_SOC_MPC5123;
  96. return;
  97. }
  98. if (of_machine_is_compatible("fsl,mpc5125")) {
  99. soc = MPC512x_SOC_MPC5125;
  100. return;
  101. }
  102. }
  103. static bool __init soc_has_mbx(void)
  104. {
  105. if (soc == MPC512x_SOC_MPC5121)
  106. return true;
  107. return false;
  108. }
  109. static bool __init soc_has_axe(void)
  110. {
  111. if (soc == MPC512x_SOC_MPC5125)
  112. return false;
  113. return true;
  114. }
  115. static bool __init soc_has_viu(void)
  116. {
  117. if (soc == MPC512x_SOC_MPC5125)
  118. return false;
  119. return true;
  120. }
  121. static bool __init soc_has_spdif(void)
  122. {
  123. if (soc == MPC512x_SOC_MPC5125)
  124. return false;
  125. return true;
  126. }
  127. static bool __init soc_has_pata(void)
  128. {
  129. if (soc == MPC512x_SOC_MPC5125)
  130. return false;
  131. return true;
  132. }
  133. static bool __init soc_has_sata(void)
  134. {
  135. if (soc == MPC512x_SOC_MPC5125)
  136. return false;
  137. return true;
  138. }
  139. static bool __init soc_has_pci(void)
  140. {
  141. if (soc == MPC512x_SOC_MPC5125)
  142. return false;
  143. return true;
  144. }
  145. static bool __init soc_has_fec2(void)
  146. {
  147. if (soc == MPC512x_SOC_MPC5125)
  148. return true;
  149. return false;
  150. }
  151. static int __init soc_max_pscnum(void)
  152. {
  153. if (soc == MPC512x_SOC_MPC5125)
  154. return 10;
  155. return 12;
  156. }
  157. static bool __init soc_has_sdhc2(void)
  158. {
  159. if (soc == MPC512x_SOC_MPC5125)
  160. return true;
  161. return false;
  162. }
  163. static bool __init soc_has_nfc_5125(void)
  164. {
  165. if (soc == MPC512x_SOC_MPC5125)
  166. return true;
  167. return false;
  168. }
  169. static bool __init soc_has_outclk(void)
  170. {
  171. if (soc == MPC512x_SOC_MPC5125)
  172. return true;
  173. return false;
  174. }
  175. static bool __init soc_has_cpmf_0_bypass(void)
  176. {
  177. if (soc == MPC512x_SOC_MPC5125)
  178. return true;
  179. return false;
  180. }
  181. static bool __init soc_has_mclk_mux0_canin(void)
  182. {
  183. if (soc == MPC512x_SOC_MPC5125)
  184. return true;
  185. return false;
  186. }
  187. /* }}} SoC variants */
  188. /* common clk API wrappers {{{ */
  189. /* convenience wrappers around the common clk API */
  190. static inline struct clk *mpc512x_clk_fixed(const char *name, int rate)
  191. {
  192. return clk_register_fixed_rate(NULL, name, NULL, 0, rate);
  193. }
  194. static inline struct clk *mpc512x_clk_factor(
  195. const char *name, const char *parent_name,
  196. int mul, int div)
  197. {
  198. int clkflags;
  199. clkflags = CLK_SET_RATE_PARENT;
  200. return clk_register_fixed_factor(NULL, name, parent_name, clkflags,
  201. mul, div);
  202. }
  203. static inline struct clk *mpc512x_clk_divider(
  204. const char *name, const char *parent_name, u8 clkflags,
  205. u32 __iomem *reg, u8 pos, u8 len, int divflags)
  206. {
  207. divflags |= CLK_DIVIDER_BIG_ENDIAN;
  208. return clk_register_divider(NULL, name, parent_name, clkflags,
  209. reg, pos, len, divflags, &clklock);
  210. }
  211. static inline struct clk *mpc512x_clk_divtable(
  212. const char *name, const char *parent_name,
  213. u32 __iomem *reg, u8 pos, u8 len,
  214. const struct clk_div_table *divtab)
  215. {
  216. u8 divflags;
  217. divflags = CLK_DIVIDER_BIG_ENDIAN;
  218. return clk_register_divider_table(NULL, name, parent_name, 0,
  219. reg, pos, len, divflags,
  220. divtab, &clklock);
  221. }
  222. static inline struct clk *mpc512x_clk_gated(
  223. const char *name, const char *parent_name,
  224. u32 __iomem *reg, u8 pos)
  225. {
  226. int clkflags;
  227. u8 gateflags;
  228. clkflags = CLK_SET_RATE_PARENT;
  229. gateflags = CLK_GATE_BIG_ENDIAN;
  230. return clk_register_gate(NULL, name, parent_name, clkflags,
  231. reg, pos, gateflags, &clklock);
  232. }
  233. static inline struct clk *mpc512x_clk_muxed(const char *name,
  234. const char **parent_names, int parent_count,
  235. u32 __iomem *reg, u8 pos, u8 len)
  236. {
  237. int clkflags;
  238. u8 muxflags;
  239. clkflags = CLK_SET_RATE_PARENT;
  240. muxflags = CLK_MUX_BIG_ENDIAN;
  241. return clk_register_mux(NULL, name,
  242. parent_names, parent_count, clkflags,
  243. reg, pos, len, muxflags, &clklock);
  244. }
  245. /* }}} common clk API wrappers */
  246. /* helper to isolate a bit field from a register */
  247. static inline int get_bit_field(uint32_t __iomem *reg, uint8_t pos, uint8_t len)
  248. {
  249. uint32_t val;
  250. val = in_be32(reg);
  251. val >>= pos;
  252. val &= (1 << len) - 1;
  253. return val;
  254. }
  255. /* get the SPMF and translate it into the "sys pll" multiplier */
  256. static int __init get_spmf_mult(void)
  257. {
  258. static int spmf_to_mult[] = {
  259. 68, 1, 12, 16, 20, 24, 28, 32,
  260. 36, 40, 44, 48, 52, 56, 60, 64,
  261. };
  262. int spmf;
  263. spmf = get_bit_field(&clkregs->spmr, 24, 4);
  264. return spmf_to_mult[spmf];
  265. }
  266. /*
  267. * get the SYS_DIV value and translate it into a divide factor
  268. *
  269. * values returned from here are a multiple of the real factor since the
  270. * divide ratio is fractional
  271. */
  272. static int __init get_sys_div_x2(void)
  273. {
  274. static int sysdiv_code_to_x2[] = {
  275. 4, 5, 6, 7, 8, 9, 10, 14,
  276. 12, 16, 18, 22, 20, 24, 26, 30,
  277. 28, 32, 34, 38, 36, 40, 42, 46,
  278. 44, 48, 50, 54, 52, 56, 58, 62,
  279. 60, 64, 66,
  280. };
  281. int divcode;
  282. divcode = get_bit_field(&clkregs->scfr2, 26, 6);
  283. return sysdiv_code_to_x2[divcode];
  284. }
  285. /*
  286. * get the CPMF value and translate it into a multiplier factor
  287. *
  288. * values returned from here are a multiple of the real factor since the
  289. * multiplier ratio is fractional
  290. */
  291. static int __init get_cpmf_mult_x2(void)
  292. {
  293. static int cpmf_to_mult_x36[] = {
  294. /* 0b000 is "times 36" */
  295. 72, 2, 2, 3, 4, 5, 6, 7,
  296. };
  297. static int cpmf_to_mult_0by[] = {
  298. /* 0b000 is "bypass" */
  299. 2, 2, 2, 3, 4, 5, 6, 7,
  300. };
  301. int *cpmf_to_mult;
  302. int cpmf;
  303. cpmf = get_bit_field(&clkregs->spmr, 16, 4);
  304. if (soc_has_cpmf_0_bypass())
  305. cpmf_to_mult = cpmf_to_mult_0by;
  306. else
  307. cpmf_to_mult = cpmf_to_mult_x36;
  308. return cpmf_to_mult[cpmf];
  309. }
  310. /*
  311. * some of the clock dividers do scale in a linear way, yet not all of
  312. * their bit combinations are legal; use a divider table to get a
  313. * resulting set of applicable divider values
  314. */
  315. /* applies to the IPS_DIV, and PCI_DIV values */
  316. static const struct clk_div_table divtab_2346[] = {
  317. { .val = 2, .div = 2, },
  318. { .val = 3, .div = 3, },
  319. { .val = 4, .div = 4, },
  320. { .val = 6, .div = 6, },
  321. { .div = 0, },
  322. };
  323. /* applies to the MBX_DIV, LPC_DIV, and NFC_DIV values */
  324. static const struct clk_div_table divtab_1234[] = {
  325. { .val = 1, .div = 1, },
  326. { .val = 2, .div = 2, },
  327. { .val = 3, .div = 3, },
  328. { .val = 4, .div = 4, },
  329. { .div = 0, },
  330. };
  331. static int __init get_freq_from_dt(char *propname)
  332. {
  333. struct device_node *np;
  334. const unsigned int *prop;
  335. int val;
  336. val = 0;
  337. np = of_find_compatible_node(NULL, NULL, "fsl,mpc5121-immr");
  338. if (np) {
  339. prop = of_get_property(np, propname, NULL);
  340. if (prop)
  341. val = *prop;
  342. of_node_put(np);
  343. }
  344. return val;
  345. }
  346. static void __init mpc512x_clk_preset_data(void)
  347. {
  348. size_t i;
  349. for (i = 0; i < ARRAY_SIZE(clks); i++)
  350. clks[i] = ERR_PTR(-ENODEV);
  351. }
  352. /*
  353. * - receives the "bus frequency" from the caller (that's the IPS clock
  354. * rate, the historical source of clock information)
  355. * - fetches the system PLL multiplier and divider values as well as the
  356. * IPS divider value from hardware
  357. * - determines the REF clock rate either from the XTAL/OSC spec (if
  358. * there is a device tree node describing the oscillator) or from the
  359. * IPS bus clock (supported for backwards compatibility, such that
  360. * setups without XTAL/OSC specs keep working)
  361. * - creates the "ref" clock item in the clock tree, such that
  362. * subsequent code can create the remainder of the hierarchy (REF ->
  363. * SYS -> CSB -> IPS) from the REF clock rate and the returned mul/div
  364. * values
  365. */
  366. static void __init mpc512x_clk_setup_ref_clock(struct device_node *np, int bus_freq,
  367. int *sys_mul, int *sys_div,
  368. int *ips_div)
  369. {
  370. struct clk *osc_clk;
  371. int calc_freq;
  372. /* fetch mul/div factors from the hardware */
  373. *sys_mul = get_spmf_mult();
  374. *sys_mul *= 2; /* compensate for the fractional divider */
  375. *sys_div = get_sys_div_x2();
  376. *ips_div = get_bit_field(&clkregs->scfr1, 23, 3);
  377. /* lookup the oscillator clock for its rate */
  378. osc_clk = of_clk_get_by_name(np, "osc");
  379. /*
  380. * either descend from OSC to REF (and in bypassing verify the
  381. * IPS rate), or backtrack from IPS and multiplier values that
  382. * were fetched from hardware to REF and thus to the OSC value
  383. *
  384. * in either case the REF clock gets created here and the
  385. * remainder of the clock tree can get spanned from there
  386. */
  387. if (!IS_ERR(osc_clk)) {
  388. clks[MPC512x_CLK_REF] = mpc512x_clk_factor("ref", "osc", 1, 1);
  389. calc_freq = clk_get_rate(clks[MPC512x_CLK_REF]);
  390. calc_freq *= *sys_mul;
  391. calc_freq /= *sys_div;
  392. calc_freq /= 2;
  393. calc_freq /= *ips_div;
  394. if (bus_freq && calc_freq != bus_freq)
  395. pr_warn("calc rate %d != OF spec %d\n",
  396. calc_freq, bus_freq);
  397. } else {
  398. calc_freq = bus_freq; /* start with IPS */
  399. calc_freq *= *ips_div; /* IPS -> CSB */
  400. calc_freq *= 2; /* CSB -> SYS */
  401. calc_freq *= *sys_div; /* SYS -> PLL out */
  402. calc_freq /= *sys_mul; /* PLL out -> REF == OSC */
  403. clks[MPC512x_CLK_REF] = mpc512x_clk_fixed("ref", calc_freq);
  404. }
  405. }
  406. /* MCLK helpers {{{ */
  407. /*
  408. * helper code for the MCLK subtree setup
  409. *
  410. * the overview in section 5.2.4 of the MPC5121e Reference Manual rev4
  411. * suggests that all instances of the "PSC clock generation" are equal,
  412. * and that one might re-use the PSC setup for MSCAN clock generation
  413. * (section 5.2.5) as well, at least the logic if not the data for
  414. * description
  415. *
  416. * the details (starting at page 5-20) show differences in the specific
  417. * inputs of the first mux stage ("can clk in", "spdif tx"), and the
  418. * factual non-availability of the second mux stage (it's present yet
  419. * only one input is valid)
  420. *
  421. * the MSCAN clock related registers (starting at page 5-35) all
  422. * reference "spdif clk" at the first mux stage and don't mention any
  423. * "can clk" at all, which somehow is unexpected
  424. *
  425. * TODO re-check the document, and clarify whether the RM is correct in
  426. * the overview or in the details, and whether the difference is a
  427. * clipboard induced error or results from chip revisions
  428. *
  429. * it turns out that the RM rev4 as of 2012-06 talks about "can" for the
  430. * PSCs while RM rev3 as of 2008-10 talks about "spdif", so I guess that
  431. * first a doc update is required which better reflects reality in the
  432. * SoC before the implementation should follow while no questions remain
  433. */
  434. /*
  435. * note that this declaration raises a checkpatch warning, but
  436. * it's the very data type dictated by <linux/clk-provider.h>,
  437. * "fixing" this warning will break compilation
  438. */
  439. static const char *parent_names_mux0_spdif[] = {
  440. "sys", "ref", "psc-mclk-in", "spdif-tx",
  441. };
  442. static const char *parent_names_mux0_canin[] = {
  443. "sys", "ref", "psc-mclk-in", "can-clk-in",
  444. };
  445. enum mclk_type {
  446. MCLK_TYPE_PSC,
  447. MCLK_TYPE_MSCAN,
  448. MCLK_TYPE_SPDIF,
  449. MCLK_TYPE_OUTCLK,
  450. };
  451. struct mclk_setup_data {
  452. enum mclk_type type;
  453. bool has_mclk1;
  454. const char *name_mux0;
  455. const char *name_en0;
  456. const char *name_div0;
  457. const char *parent_names_mux1[2];
  458. const char *name_mclk;
  459. };
  460. #define MCLK_SETUP_DATA_PSC(id) { \
  461. MCLK_TYPE_PSC, 0, \
  462. "psc" #id "-mux0", \
  463. "psc" #id "-en0", \
  464. "psc" #id "_mclk_div", \
  465. { "psc" #id "_mclk_div", "dummy", }, \
  466. "psc" #id "_mclk", \
  467. }
  468. #define MCLK_SETUP_DATA_MSCAN(id) { \
  469. MCLK_TYPE_MSCAN, 0, \
  470. "mscan" #id "-mux0", \
  471. "mscan" #id "-en0", \
  472. "mscan" #id "_mclk_div", \
  473. { "mscan" #id "_mclk_div", "dummy", }, \
  474. "mscan" #id "_mclk", \
  475. }
  476. #define MCLK_SETUP_DATA_SPDIF { \
  477. MCLK_TYPE_SPDIF, 1, \
  478. "spdif-mux0", \
  479. "spdif-en0", \
  480. "spdif_mclk_div", \
  481. { "spdif_mclk_div", "spdif-rx", }, \
  482. "spdif_mclk", \
  483. }
  484. #define MCLK_SETUP_DATA_OUTCLK(id) { \
  485. MCLK_TYPE_OUTCLK, 0, \
  486. "out" #id "-mux0", \
  487. "out" #id "-en0", \
  488. "out" #id "_mclk_div", \
  489. { "out" #id "_mclk_div", "dummy", }, \
  490. "out" #id "_clk", \
  491. }
  492. static struct mclk_setup_data mclk_psc_data[] = {
  493. MCLK_SETUP_DATA_PSC(0),
  494. MCLK_SETUP_DATA_PSC(1),
  495. MCLK_SETUP_DATA_PSC(2),
  496. MCLK_SETUP_DATA_PSC(3),
  497. MCLK_SETUP_DATA_PSC(4),
  498. MCLK_SETUP_DATA_PSC(5),
  499. MCLK_SETUP_DATA_PSC(6),
  500. MCLK_SETUP_DATA_PSC(7),
  501. MCLK_SETUP_DATA_PSC(8),
  502. MCLK_SETUP_DATA_PSC(9),
  503. MCLK_SETUP_DATA_PSC(10),
  504. MCLK_SETUP_DATA_PSC(11),
  505. };
  506. static struct mclk_setup_data mclk_mscan_data[] = {
  507. MCLK_SETUP_DATA_MSCAN(0),
  508. MCLK_SETUP_DATA_MSCAN(1),
  509. MCLK_SETUP_DATA_MSCAN(2),
  510. MCLK_SETUP_DATA_MSCAN(3),
  511. };
  512. static struct mclk_setup_data mclk_spdif_data[] = {
  513. MCLK_SETUP_DATA_SPDIF,
  514. };
  515. static struct mclk_setup_data mclk_outclk_data[] = {
  516. MCLK_SETUP_DATA_OUTCLK(0),
  517. MCLK_SETUP_DATA_OUTCLK(1),
  518. MCLK_SETUP_DATA_OUTCLK(2),
  519. MCLK_SETUP_DATA_OUTCLK(3),
  520. };
  521. /* setup the MCLK clock subtree of an individual PSC/MSCAN/SPDIF */
  522. static void __init mpc512x_clk_setup_mclk(struct mclk_setup_data *entry, size_t idx)
  523. {
  524. size_t clks_idx_pub, clks_idx_int;
  525. u32 __iomem *mccr_reg; /* MCLK control register (mux, en, div) */
  526. int div;
  527. /* derive a few parameters from the component type and index */
  528. switch (entry->type) {
  529. case MCLK_TYPE_PSC:
  530. clks_idx_pub = MPC512x_CLK_PSC0_MCLK + idx;
  531. clks_idx_int = MPC512x_CLK_MCLKS_FIRST
  532. + (idx) * MCLK_MAX_IDX;
  533. mccr_reg = &clkregs->psc_ccr[idx];
  534. break;
  535. case MCLK_TYPE_MSCAN:
  536. clks_idx_pub = MPC512x_CLK_MSCAN0_MCLK + idx;
  537. clks_idx_int = MPC512x_CLK_MCLKS_FIRST
  538. + (NR_PSCS + idx) * MCLK_MAX_IDX;
  539. mccr_reg = &clkregs->mscan_ccr[idx];
  540. break;
  541. case MCLK_TYPE_SPDIF:
  542. clks_idx_pub = MPC512x_CLK_SPDIF_MCLK;
  543. clks_idx_int = MPC512x_CLK_MCLKS_FIRST
  544. + (NR_PSCS + NR_MSCANS) * MCLK_MAX_IDX;
  545. mccr_reg = &clkregs->spccr;
  546. break;
  547. case MCLK_TYPE_OUTCLK:
  548. clks_idx_pub = MPC512x_CLK_OUT0_CLK + idx;
  549. clks_idx_int = MPC512x_CLK_MCLKS_FIRST
  550. + (NR_PSCS + NR_MSCANS + NR_SPDIFS + idx)
  551. * MCLK_MAX_IDX;
  552. mccr_reg = &clkregs->out_ccr[idx];
  553. break;
  554. default:
  555. return;
  556. }
  557. /*
  558. * this was grabbed from the PPC_CLOCK implementation, which
  559. * enforced a specific MCLK divider while the clock was gated
  560. * during setup (that's a documented hardware requirement)
  561. *
  562. * the PPC_CLOCK implementation might even have violated the
  563. * "MCLK <= IPS" constraint, the fixed divider value of 1
  564. * results in a divider of 2 and thus MCLK = SYS/2 which equals
  565. * CSB which is greater than IPS; the serial port setup may have
  566. * adjusted the divider which the clock setup might have left in
  567. * an undesirable state
  568. *
  569. * initial setup is:
  570. * - MCLK 0 from SYS
  571. * - MCLK DIV such to not exceed the IPS clock
  572. * - MCLK 0 enabled
  573. * - MCLK 1 from MCLK DIV
  574. */
  575. div = clk_get_rate(clks[MPC512x_CLK_SYS]);
  576. div /= clk_get_rate(clks[MPC512x_CLK_IPS]);
  577. out_be32(mccr_reg, (0 << 16));
  578. out_be32(mccr_reg, (0 << 16) | ((div - 1) << 17));
  579. out_be32(mccr_reg, (1 << 16) | ((div - 1) << 17));
  580. /*
  581. * create the 'struct clk' items of the MCLK's clock subtree
  582. *
  583. * note that by design we always create all nodes and won't take
  584. * shortcuts here, because
  585. * - the "internal" MCLK_DIV and MCLK_OUT signal in turn are
  586. * selectable inputs to the CFM while those who "actually use"
  587. * the PSC/MSCAN/SPDIF (serial drivers et al) need the MCLK
  588. * for their bitrate
  589. * - in the absence of "aliases" for clocks we need to create
  590. * individual 'struct clk' items for whatever might get
  591. * referenced or looked up, even if several of those items are
  592. * identical from the logical POV (their rate value)
  593. * - for easier future maintenance and for better reflection of
  594. * the SoC's documentation, it appears appropriate to generate
  595. * clock items even for those muxers which actually are NOPs
  596. * (those with two inputs of which one is reserved)
  597. */
  598. clks[clks_idx_int + MCLK_IDX_MUX0] = mpc512x_clk_muxed(
  599. entry->name_mux0,
  600. soc_has_mclk_mux0_canin()
  601. ? &parent_names_mux0_canin[0]
  602. : &parent_names_mux0_spdif[0],
  603. ARRAY_SIZE(parent_names_mux0_spdif),
  604. mccr_reg, 14, 2);
  605. clks[clks_idx_int + MCLK_IDX_EN0] = mpc512x_clk_gated(
  606. entry->name_en0, entry->name_mux0,
  607. mccr_reg, 16);
  608. clks[clks_idx_int + MCLK_IDX_DIV0] = mpc512x_clk_divider(
  609. entry->name_div0,
  610. entry->name_en0, CLK_SET_RATE_GATE,
  611. mccr_reg, 17, 15, 0);
  612. if (entry->has_mclk1) {
  613. clks[clks_idx_pub] = mpc512x_clk_muxed(
  614. entry->name_mclk,
  615. &entry->parent_names_mux1[0],
  616. ARRAY_SIZE(entry->parent_names_mux1),
  617. mccr_reg, 7, 1);
  618. } else {
  619. clks[clks_idx_pub] = mpc512x_clk_factor(
  620. entry->name_mclk,
  621. entry->parent_names_mux1[0],
  622. 1, 1);
  623. }
  624. }
  625. /* }}} MCLK helpers */
  626. static void __init mpc512x_clk_setup_clock_tree(struct device_node *np, int busfreq)
  627. {
  628. int sys_mul, sys_div, ips_div;
  629. int mul, div;
  630. size_t mclk_idx;
  631. int freq;
  632. /*
  633. * developer's notes:
  634. * - consider whether to handle clocks which have both gates and
  635. * dividers via intermediates or by means of composites
  636. * - fractional dividers appear to not map well to composites
  637. * since they can be seen as a fixed multiplier and an
  638. * adjustable divider, while composites can only combine at
  639. * most one of a mux, div, and gate each into one 'struct clk'
  640. * item
  641. * - PSC/MSCAN/SPDIF clock generation OTOH already is very
  642. * specific and cannot get mapped to composites (at least not
  643. * a single one, maybe two of them, but then some of these
  644. * intermediate clock signals get referenced elsewhere (e.g.
  645. * in the clock frequency measurement, CFM) and thus need
  646. * publicly available names
  647. * - the current source layout appropriately reflects the
  648. * hardware setup, and it works, so it's questionable whether
  649. * further changes will result in big enough a benefit
  650. */
  651. /* regardless of whether XTAL/OSC exists, have REF created */
  652. mpc512x_clk_setup_ref_clock(np, busfreq, &sys_mul, &sys_div, &ips_div);
  653. /* now setup the REF -> SYS -> CSB -> IPS hierarchy */
  654. clks[MPC512x_CLK_SYS] = mpc512x_clk_factor("sys", "ref",
  655. sys_mul, sys_div);
  656. clks[MPC512x_CLK_CSB] = mpc512x_clk_factor("csb", "sys", 1, 2);
  657. clks[MPC512x_CLK_IPS] = mpc512x_clk_divtable("ips", "csb",
  658. &clkregs->scfr1, 23, 3,
  659. divtab_2346);
  660. /* now setup anything below SYS and CSB and IPS */
  661. clks[MPC512x_CLK_DDR_UG] = mpc512x_clk_factor("ddr-ug", "sys", 1, 2);
  662. /*
  663. * the Reference Manual discusses that for SDHC only even divide
  664. * ratios are supported because clock domain synchronization
  665. * between 'per' and 'ipg' is broken;
  666. * keep the divider's bit 0 cleared (per reset value), and only
  667. * allow to setup the divider's bits 7:1, which results in that
  668. * only even divide ratios can get configured upon rate changes;
  669. * keep the "x4" name because this bit shift hack is an internal
  670. * implementation detail, the "fractional divider with quarters"
  671. * semantics remains
  672. */
  673. clks[MPC512x_CLK_SDHC_x4] = mpc512x_clk_factor("sdhc-x4", "csb", 2, 1);
  674. clks[MPC512x_CLK_SDHC_UG] = mpc512x_clk_divider("sdhc-ug", "sdhc-x4", 0,
  675. &clkregs->scfr2, 1, 7,
  676. CLK_DIVIDER_ONE_BASED);
  677. if (soc_has_sdhc2()) {
  678. clks[MPC512x_CLK_SDHC2_UG] = mpc512x_clk_divider(
  679. "sdhc2-ug", "sdhc-x4", 0, &clkregs->scfr2,
  680. 9, 7, CLK_DIVIDER_ONE_BASED);
  681. }
  682. clks[MPC512x_CLK_DIU_x4] = mpc512x_clk_factor("diu-x4", "csb", 4, 1);
  683. clks[MPC512x_CLK_DIU_UG] = mpc512x_clk_divider("diu-ug", "diu-x4", 0,
  684. &clkregs->scfr1, 0, 8,
  685. CLK_DIVIDER_ONE_BASED);
  686. /*
  687. * the "power architecture PLL" was setup from data which was
  688. * sampled from the reset config word, at this point in time the
  689. * configuration can be considered fixed and read only (i.e. no
  690. * longer adjustable, or no longer in need of adjustment), which
  691. * is why we don't register a PLL here but assume fixed factors
  692. */
  693. mul = get_cpmf_mult_x2();
  694. div = 2; /* compensate for the fractional factor */
  695. clks[MPC512x_CLK_E300] = mpc512x_clk_factor("e300", "csb", mul, div);
  696. if (soc_has_mbx()) {
  697. clks[MPC512x_CLK_MBX_BUS_UG] = mpc512x_clk_factor(
  698. "mbx-bus-ug", "csb", 1, 2);
  699. clks[MPC512x_CLK_MBX_UG] = mpc512x_clk_divtable(
  700. "mbx-ug", "mbx-bus-ug", &clkregs->scfr1,
  701. 14, 3, divtab_1234);
  702. clks[MPC512x_CLK_MBX_3D_UG] = mpc512x_clk_factor(
  703. "mbx-3d-ug", "mbx-ug", 1, 1);
  704. }
  705. if (soc_has_pci()) {
  706. clks[MPC512x_CLK_PCI_UG] = mpc512x_clk_divtable(
  707. "pci-ug", "csb", &clkregs->scfr1,
  708. 20, 3, divtab_2346);
  709. }
  710. if (soc_has_nfc_5125()) {
  711. /*
  712. * XXX TODO implement 5125 NFC clock setup logic,
  713. * with high/low period counters in clkregs->scfr3,
  714. * currently there are no users so it's ENOIMPL
  715. */
  716. clks[MPC512x_CLK_NFC_UG] = ERR_PTR(-ENOTSUPP);
  717. } else {
  718. clks[MPC512x_CLK_NFC_UG] = mpc512x_clk_divtable(
  719. "nfc-ug", "ips", &clkregs->scfr1,
  720. 8, 3, divtab_1234);
  721. }
  722. clks[MPC512x_CLK_LPC_UG] = mpc512x_clk_divtable("lpc-ug", "ips",
  723. &clkregs->scfr1, 11, 3,
  724. divtab_1234);
  725. clks[MPC512x_CLK_LPC] = mpc512x_clk_gated("lpc", "lpc-ug",
  726. &clkregs->sccr1, 30);
  727. clks[MPC512x_CLK_NFC] = mpc512x_clk_gated("nfc", "nfc-ug",
  728. &clkregs->sccr1, 29);
  729. if (soc_has_pata()) {
  730. clks[MPC512x_CLK_PATA] = mpc512x_clk_gated(
  731. "pata", "ips", &clkregs->sccr1, 28);
  732. }
  733. /* for PSCs there is a "registers" gate and a bitrate MCLK subtree */
  734. for (mclk_idx = 0; mclk_idx < soc_max_pscnum(); mclk_idx++) {
  735. char name[12];
  736. snprintf(name, sizeof(name), "psc%d", mclk_idx);
  737. clks[MPC512x_CLK_PSC0 + mclk_idx] = mpc512x_clk_gated(
  738. name, "ips", &clkregs->sccr1, 27 - mclk_idx);
  739. mpc512x_clk_setup_mclk(&mclk_psc_data[mclk_idx], mclk_idx);
  740. }
  741. clks[MPC512x_CLK_PSC_FIFO] = mpc512x_clk_gated("psc-fifo", "ips",
  742. &clkregs->sccr1, 15);
  743. if (soc_has_sata()) {
  744. clks[MPC512x_CLK_SATA] = mpc512x_clk_gated(
  745. "sata", "ips", &clkregs->sccr1, 14);
  746. }
  747. clks[MPC512x_CLK_FEC] = mpc512x_clk_gated("fec", "ips",
  748. &clkregs->sccr1, 13);
  749. if (soc_has_pci()) {
  750. clks[MPC512x_CLK_PCI] = mpc512x_clk_gated(
  751. "pci", "pci-ug", &clkregs->sccr1, 11);
  752. }
  753. clks[MPC512x_CLK_DDR] = mpc512x_clk_gated("ddr", "ddr-ug",
  754. &clkregs->sccr1, 10);
  755. if (soc_has_fec2()) {
  756. clks[MPC512x_CLK_FEC2] = mpc512x_clk_gated(
  757. "fec2", "ips", &clkregs->sccr1, 9);
  758. }
  759. clks[MPC512x_CLK_DIU] = mpc512x_clk_gated("diu", "diu-ug",
  760. &clkregs->sccr2, 31);
  761. if (soc_has_axe()) {
  762. clks[MPC512x_CLK_AXE] = mpc512x_clk_gated(
  763. "axe", "csb", &clkregs->sccr2, 30);
  764. }
  765. clks[MPC512x_CLK_MEM] = mpc512x_clk_gated("mem", "ips",
  766. &clkregs->sccr2, 29);
  767. clks[MPC512x_CLK_USB1] = mpc512x_clk_gated("usb1", "csb",
  768. &clkregs->sccr2, 28);
  769. clks[MPC512x_CLK_USB2] = mpc512x_clk_gated("usb2", "csb",
  770. &clkregs->sccr2, 27);
  771. clks[MPC512x_CLK_I2C] = mpc512x_clk_gated("i2c", "ips",
  772. &clkregs->sccr2, 26);
  773. /* MSCAN differs from PSC with just one gate for multiple components */
  774. clks[MPC512x_CLK_BDLC] = mpc512x_clk_gated("bdlc", "ips",
  775. &clkregs->sccr2, 25);
  776. for (mclk_idx = 0; mclk_idx < ARRAY_SIZE(mclk_mscan_data); mclk_idx++)
  777. mpc512x_clk_setup_mclk(&mclk_mscan_data[mclk_idx], mclk_idx);
  778. clks[MPC512x_CLK_SDHC] = mpc512x_clk_gated("sdhc", "sdhc-ug",
  779. &clkregs->sccr2, 24);
  780. /* there is only one SPDIF component, which shares MCLK support code */
  781. if (soc_has_spdif()) {
  782. clks[MPC512x_CLK_SPDIF] = mpc512x_clk_gated(
  783. "spdif", "ips", &clkregs->sccr2, 23);
  784. mpc512x_clk_setup_mclk(&mclk_spdif_data[0], 0);
  785. }
  786. if (soc_has_mbx()) {
  787. clks[MPC512x_CLK_MBX_BUS] = mpc512x_clk_gated(
  788. "mbx-bus", "mbx-bus-ug", &clkregs->sccr2, 22);
  789. clks[MPC512x_CLK_MBX] = mpc512x_clk_gated(
  790. "mbx", "mbx-ug", &clkregs->sccr2, 21);
  791. clks[MPC512x_CLK_MBX_3D] = mpc512x_clk_gated(
  792. "mbx-3d", "mbx-3d-ug", &clkregs->sccr2, 20);
  793. }
  794. clks[MPC512x_CLK_IIM] = mpc512x_clk_gated("iim", "csb",
  795. &clkregs->sccr2, 19);
  796. if (soc_has_viu()) {
  797. clks[MPC512x_CLK_VIU] = mpc512x_clk_gated(
  798. "viu", "csb", &clkregs->sccr2, 18);
  799. }
  800. if (soc_has_sdhc2()) {
  801. clks[MPC512x_CLK_SDHC2] = mpc512x_clk_gated(
  802. "sdhc-2", "sdhc2-ug", &clkregs->sccr2, 17);
  803. }
  804. if (soc_has_outclk()) {
  805. size_t idx; /* used as mclk_idx, just to trim line length */
  806. for (idx = 0; idx < ARRAY_SIZE(mclk_outclk_data); idx++)
  807. mpc512x_clk_setup_mclk(&mclk_outclk_data[idx], idx);
  808. }
  809. /*
  810. * externally provided clocks (when implemented in hardware,
  811. * device tree may specify values which otherwise were unknown)
  812. */
  813. freq = get_freq_from_dt("psc_mclk_in");
  814. if (!freq)
  815. freq = 25000000;
  816. clks[MPC512x_CLK_PSC_MCLK_IN] = mpc512x_clk_fixed("psc_mclk_in", freq);
  817. if (soc_has_mclk_mux0_canin()) {
  818. freq = get_freq_from_dt("can_clk_in");
  819. clks[MPC512x_CLK_CAN_CLK_IN] = mpc512x_clk_fixed(
  820. "can_clk_in", freq);
  821. } else {
  822. freq = get_freq_from_dt("spdif_tx_in");
  823. clks[MPC512x_CLK_SPDIF_TX_IN] = mpc512x_clk_fixed(
  824. "spdif_tx_in", freq);
  825. freq = get_freq_from_dt("spdif_rx_in");
  826. clks[MPC512x_CLK_SPDIF_TX_IN] = mpc512x_clk_fixed(
  827. "spdif_rx_in", freq);
  828. }
  829. /* fixed frequency for AC97, always 24.567MHz */
  830. clks[MPC512x_CLK_AC97] = mpc512x_clk_fixed("ac97", 24567000);
  831. /*
  832. * pre-enable those "internal" clock items which never get
  833. * claimed by any peripheral driver, to not have the clock
  834. * subsystem disable them late at startup
  835. */
  836. clk_prepare_enable(clks[MPC512x_CLK_DUMMY]);
  837. clk_prepare_enable(clks[MPC512x_CLK_E300]); /* PowerPC CPU */
  838. clk_prepare_enable(clks[MPC512x_CLK_DDR]); /* DRAM */
  839. clk_prepare_enable(clks[MPC512x_CLK_MEM]); /* SRAM */
  840. clk_prepare_enable(clks[MPC512x_CLK_IPS]); /* SoC periph */
  841. clk_prepare_enable(clks[MPC512x_CLK_LPC]); /* boot media */
  842. }
  843. /*
  844. * registers the set of public clocks (those listed in the dt-bindings/
  845. * header file) for OF lookups, keeps the intermediates private to us
  846. */
  847. static void __init mpc5121_clk_register_of_provider(struct device_node *np)
  848. {
  849. clk_data.clks = clks;
  850. clk_data.clk_num = MPC512x_CLK_LAST_PUBLIC + 1; /* _not_ ARRAY_SIZE() */
  851. of_clk_add_provider(np, of_clk_src_onecell_get, &clk_data);
  852. }
  853. /*
  854. * temporary support for the period of time between introduction of CCF
  855. * support and the adjustment of peripheral drivers to OF based lookups
  856. */
  857. static void __init mpc5121_clk_provide_migration_support(void)
  858. {
  859. struct device_node *np;
  860. /*
  861. * pre-enable those clock items which are not yet appropriately
  862. * acquired by their peripheral driver
  863. *
  864. * the PCI clock cannot get acquired by its peripheral driver,
  865. * because for this platform the driver won't probe(), instead
  866. * initialization is done from within the .setup_arch() routine
  867. * at a point in time where the clock provider has not been
  868. * setup yet and thus isn't available yet
  869. *
  870. * so we "pre-enable" the clock here, to not have the clock
  871. * subsystem automatically disable this item in a late init call
  872. *
  873. * this PCI clock pre-enable workaround only applies when there
  874. * are device tree nodes for PCI and thus the peripheral driver
  875. * has attached to bridges, otherwise the PCI clock remains
  876. * unused and so it gets disabled
  877. */
  878. clk_prepare_enable(clks[MPC512x_CLK_PSC3_MCLK]);/* serial console */
  879. np = of_find_compatible_node(NULL, "pci", "fsl,mpc5121-pci");
  880. of_node_put(np);
  881. if (np)
  882. clk_prepare_enable(clks[MPC512x_CLK_PCI]);
  883. }
  884. /*
  885. * those macros are not exactly pretty, but they encapsulate a lot
  886. * of copy'n'paste heavy code which is even more ugly, and reduce
  887. * the potential for inconsistencies in those many code copies
  888. */
  889. #define FOR_NODES(compatname) \
  890. for_each_compatible_node(np, NULL, compatname)
  891. #define NODE_PREP do { \
  892. of_address_to_resource(np, 0, &res); \
  893. snprintf(devname, sizeof(devname), "%pa.%s", &res.start, np->name); \
  894. } while (0)
  895. #define NODE_CHK(clkname, clkitem, regnode, regflag) do { \
  896. struct clk *clk; \
  897. clk = of_clk_get_by_name(np, clkname); \
  898. if (IS_ERR(clk)) { \
  899. clk = clkitem; \
  900. clk_register_clkdev(clk, clkname, devname); \
  901. if (regnode) \
  902. clk_register_clkdev(clk, clkname, np->name); \
  903. did_register |= DID_REG_ ## regflag; \
  904. pr_debug("clock alias name '%s' for dev '%s' pointer %p\n", \
  905. clkname, devname, clk); \
  906. } else { \
  907. clk_put(clk); \
  908. } \
  909. } while (0)
  910. /*
  911. * register source code provided fallback results for clock lookups,
  912. * these get consulted when OF based clock lookup fails (that is in the
  913. * case of not yet adjusted device tree data, where clock related specs
  914. * are missing)
  915. */
  916. static void __init mpc5121_clk_provide_backwards_compat(void)
  917. {
  918. enum did_reg_flags {
  919. DID_REG_PSC = BIT(0),
  920. DID_REG_PSCFIFO = BIT(1),
  921. DID_REG_NFC = BIT(2),
  922. DID_REG_CAN = BIT(3),
  923. DID_REG_I2C = BIT(4),
  924. DID_REG_DIU = BIT(5),
  925. DID_REG_VIU = BIT(6),
  926. DID_REG_FEC = BIT(7),
  927. DID_REG_USB = BIT(8),
  928. DID_REG_PATA = BIT(9),
  929. };
  930. int did_register;
  931. struct device_node *np;
  932. struct resource res;
  933. int idx;
  934. char devname[32];
  935. did_register = 0;
  936. FOR_NODES(mpc512x_select_psc_compat()) {
  937. NODE_PREP;
  938. idx = (res.start >> 8) & 0xf;
  939. NODE_CHK("ipg", clks[MPC512x_CLK_PSC0 + idx], 0, PSC);
  940. NODE_CHK("mclk", clks[MPC512x_CLK_PSC0_MCLK + idx], 0, PSC);
  941. }
  942. FOR_NODES("fsl,mpc5121-psc-fifo") {
  943. NODE_PREP;
  944. NODE_CHK("ipg", clks[MPC512x_CLK_PSC_FIFO], 1, PSCFIFO);
  945. }
  946. FOR_NODES("fsl,mpc5121-nfc") {
  947. NODE_PREP;
  948. NODE_CHK("ipg", clks[MPC512x_CLK_NFC], 0, NFC);
  949. }
  950. FOR_NODES("fsl,mpc5121-mscan") {
  951. NODE_PREP;
  952. idx = 0;
  953. idx += (res.start & 0x2000) ? 2 : 0;
  954. idx += (res.start & 0x0080) ? 1 : 0;
  955. NODE_CHK("ipg", clks[MPC512x_CLK_BDLC], 0, CAN);
  956. NODE_CHK("mclk", clks[MPC512x_CLK_MSCAN0_MCLK + idx], 0, CAN);
  957. }
  958. /*
  959. * do register the 'ips', 'sys', and 'ref' names globally
  960. * instead of inside each individual CAN node, as there is no
  961. * potential for a name conflict (in contrast to 'ipg' and 'mclk')
  962. */
  963. if (did_register & DID_REG_CAN) {
  964. clk_register_clkdev(clks[MPC512x_CLK_IPS], "ips", NULL);
  965. clk_register_clkdev(clks[MPC512x_CLK_SYS], "sys", NULL);
  966. clk_register_clkdev(clks[MPC512x_CLK_REF], "ref", NULL);
  967. }
  968. FOR_NODES("fsl,mpc5121-i2c") {
  969. NODE_PREP;
  970. NODE_CHK("ipg", clks[MPC512x_CLK_I2C], 0, I2C);
  971. }
  972. /*
  973. * workaround for the fact that the I2C driver does an "anonymous"
  974. * lookup (NULL name spec, which yields the first clock spec) for
  975. * which we cannot register an alias -- a _global_ 'ipg' alias that
  976. * is not bound to any device name and returns the I2C clock item
  977. * is not a good idea
  978. *
  979. * so we have the lookup in the peripheral driver fail, which is
  980. * silent and non-fatal, and pre-enable the clock item here such
  981. * that register access is possible
  982. *
  983. * see commit b3bfce2b "i2c: mpc: cleanup clock API use" for
  984. * details, adjusting s/NULL/"ipg"/ in i2c-mpc.c would make this
  985. * workaround obsolete
  986. */
  987. if (did_register & DID_REG_I2C)
  988. clk_prepare_enable(clks[MPC512x_CLK_I2C]);
  989. FOR_NODES("fsl,mpc5121-diu") {
  990. NODE_PREP;
  991. NODE_CHK("ipg", clks[MPC512x_CLK_DIU], 1, DIU);
  992. }
  993. FOR_NODES("fsl,mpc5121-viu") {
  994. NODE_PREP;
  995. NODE_CHK("ipg", clks[MPC512x_CLK_VIU], 0, VIU);
  996. }
  997. /*
  998. * note that 2771399a "fs_enet: cleanup clock API use" did use the
  999. * "per" string for the clock lookup in contrast to the "ipg" name
  1000. * which most other nodes are using -- this is not a fatal thing
  1001. * but just something to keep in mind when doing compatibility
  1002. * registration, it's a non-issue with up-to-date device tree data
  1003. */
  1004. FOR_NODES("fsl,mpc5121-fec") {
  1005. NODE_PREP;
  1006. NODE_CHK("per", clks[MPC512x_CLK_FEC], 0, FEC);
  1007. }
  1008. FOR_NODES("fsl,mpc5121-fec-mdio") {
  1009. NODE_PREP;
  1010. NODE_CHK("per", clks[MPC512x_CLK_FEC], 0, FEC);
  1011. }
  1012. /*
  1013. * MPC5125 has two FECs: FEC1 at 0x2800, FEC2 at 0x4800;
  1014. * the clock items don't "form an array" since FEC2 was
  1015. * added only later and was not allowed to shift all other
  1016. * clock item indices, so the numbers aren't adjacent
  1017. */
  1018. FOR_NODES("fsl,mpc5125-fec") {
  1019. NODE_PREP;
  1020. if (res.start & 0x4000)
  1021. idx = MPC512x_CLK_FEC2;
  1022. else
  1023. idx = MPC512x_CLK_FEC;
  1024. NODE_CHK("per", clks[idx], 0, FEC);
  1025. }
  1026. FOR_NODES("fsl,mpc5121-usb2-dr") {
  1027. NODE_PREP;
  1028. idx = (res.start & 0x4000) ? 1 : 0;
  1029. NODE_CHK("ipg", clks[MPC512x_CLK_USB1 + idx], 0, USB);
  1030. }
  1031. FOR_NODES("fsl,mpc5121-pata") {
  1032. NODE_PREP;
  1033. NODE_CHK("ipg", clks[MPC512x_CLK_PATA], 0, PATA);
  1034. }
  1035. /*
  1036. * try to collapse diagnostics into a single line of output yet
  1037. * provide a full list of what is missing, to avoid noise in the
  1038. * absence of up-to-date device tree data -- backwards
  1039. * compatibility to old DTBs is a requirement, updates may be
  1040. * desirable or preferrable but are not at all mandatory
  1041. */
  1042. if (did_register) {
  1043. pr_notice("device tree lacks clock specs, adding fallbacks (0x%x,%s%s%s%s%s%s%s%s%s%s)\n",
  1044. did_register,
  1045. (did_register & DID_REG_PSC) ? " PSC" : "",
  1046. (did_register & DID_REG_PSCFIFO) ? " PSCFIFO" : "",
  1047. (did_register & DID_REG_NFC) ? " NFC" : "",
  1048. (did_register & DID_REG_CAN) ? " CAN" : "",
  1049. (did_register & DID_REG_I2C) ? " I2C" : "",
  1050. (did_register & DID_REG_DIU) ? " DIU" : "",
  1051. (did_register & DID_REG_VIU) ? " VIU" : "",
  1052. (did_register & DID_REG_FEC) ? " FEC" : "",
  1053. (did_register & DID_REG_USB) ? " USB" : "",
  1054. (did_register & DID_REG_PATA) ? " PATA" : "");
  1055. } else {
  1056. pr_debug("device tree has clock specs, no fallbacks added\n");
  1057. }
  1058. }
  1059. /*
  1060. * The "fixed-clock" nodes (which includes the oscillator node if the board's
  1061. * DT provides one) has already been scanned by the of_clk_init() in
  1062. * time_init().
  1063. */
  1064. int __init mpc5121_clk_init(void)
  1065. {
  1066. struct device_node *clk_np;
  1067. int busfreq;
  1068. /* map the clock control registers */
  1069. clk_np = of_find_compatible_node(NULL, NULL, "fsl,mpc5121-clock");
  1070. if (!clk_np)
  1071. return -ENODEV;
  1072. clkregs = of_iomap(clk_np, 0);
  1073. WARN_ON(!clkregs);
  1074. /* determine the SoC variant we run on */
  1075. mpc512x_clk_determine_soc();
  1076. /* invalidate all not yet registered clock slots */
  1077. mpc512x_clk_preset_data();
  1078. /*
  1079. * add a dummy clock for those situations where a clock spec is
  1080. * required yet no real clock is involved
  1081. */
  1082. clks[MPC512x_CLK_DUMMY] = mpc512x_clk_fixed("dummy", 0);
  1083. /*
  1084. * have all the real nodes in the clock tree populated from REF
  1085. * down to all leaves, either starting from the OSC node or from
  1086. * a REF root that was created from the IPS bus clock input
  1087. */
  1088. busfreq = get_freq_from_dt("bus-frequency");
  1089. mpc512x_clk_setup_clock_tree(clk_np, busfreq);
  1090. /* register as an OF clock provider */
  1091. mpc5121_clk_register_of_provider(clk_np);
  1092. of_node_put(clk_np);
  1093. /*
  1094. * unbreak not yet adjusted peripheral drivers during migration
  1095. * towards fully operational common clock support, and allow
  1096. * operation in the absence of clock related device tree specs
  1097. */
  1098. mpc5121_clk_provide_migration_support();
  1099. mpc5121_clk_provide_backwards_compat();
  1100. return 0;
  1101. }