clock-commonclk.c 36 KB

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