clock-commonclk.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220
  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-provider.h>
  15. #include <linux/clkdev.h>
  16. #include <linux/device.h>
  17. #include <linux/errno.h>
  18. #include <linux/io.h>
  19. #include <linux/of.h>
  20. #include <linux/of_address.h>
  21. #include <asm/mpc5121.h>
  22. #include <dt-bindings/clock/mpc512x-clock.h>
  23. #include "mpc512x.h" /* our public mpc5121_clk_init() API */
  24. /* helpers to keep the MCLK intermediates "somewhere" in our table */
  25. enum {
  26. MCLK_IDX_MUX0,
  27. MCLK_IDX_EN0,
  28. MCLK_IDX_DIV0,
  29. MCLK_MAX_IDX,
  30. };
  31. #define NR_PSCS 12
  32. #define NR_MSCANS 4
  33. #define NR_SPDIFS 1
  34. #define NR_OUTCLK 4
  35. #define NR_MCLKS (NR_PSCS + NR_MSCANS + NR_SPDIFS + NR_OUTCLK)
  36. /* extend the public set of clocks by adding internal slots for management */
  37. enum {
  38. /* arrange for adjacent numbers after the public set */
  39. MPC512x_CLK_START_PRIVATE = MPC512x_CLK_LAST_PUBLIC,
  40. /* clocks which aren't announced to the public */
  41. MPC512x_CLK_DDR,
  42. MPC512x_CLK_MEM,
  43. MPC512x_CLK_IIM,
  44. /* intermediates in div+gate combos or fractional dividers */
  45. MPC512x_CLK_DDR_UG,
  46. MPC512x_CLK_SDHC_x4,
  47. MPC512x_CLK_SDHC_UG,
  48. MPC512x_CLK_SDHC2_UG,
  49. MPC512x_CLK_DIU_x4,
  50. MPC512x_CLK_DIU_UG,
  51. MPC512x_CLK_MBX_BUS_UG,
  52. MPC512x_CLK_MBX_UG,
  53. MPC512x_CLK_MBX_3D_UG,
  54. MPC512x_CLK_PCI_UG,
  55. MPC512x_CLK_NFC_UG,
  56. MPC512x_CLK_LPC_UG,
  57. MPC512x_CLK_SPDIF_TX_IN,
  58. /* intermediates for the mux+gate+div+mux MCLK generation */
  59. MPC512x_CLK_MCLKS_FIRST,
  60. MPC512x_CLK_MCLKS_LAST = MPC512x_CLK_MCLKS_FIRST
  61. + NR_MCLKS * MCLK_MAX_IDX,
  62. /* internal, symbolic spec for the number of slots */
  63. MPC512x_CLK_LAST_PRIVATE,
  64. };
  65. /* data required for the OF clock provider registration */
  66. static struct clk *clks[MPC512x_CLK_LAST_PRIVATE];
  67. static struct clk_onecell_data clk_data;
  68. /* CCM register access */
  69. static struct mpc512x_ccm __iomem *clkregs;
  70. static DEFINE_SPINLOCK(clklock);
  71. /* SoC variants {{{ */
  72. /*
  73. * tell SoC variants apart as they are rather similar yet not identical,
  74. * cache the result in an enum to not repeatedly run the expensive OF test
  75. *
  76. * MPC5123 is an MPC5121 without the MBX graphics accelerator
  77. *
  78. * MPC5125 has many more differences: no MBX, no AXE, no VIU, no SPDIF,
  79. * no PATA, no SATA, no PCI, two FECs (of different compatibility name),
  80. * only 10 PSCs (of different compatibility name), two SDHCs, different
  81. * NFC IP block, output clocks, system PLL status query, different CPMF
  82. * interpretation, no CFM, different fourth PSC/CAN mux0 input -- yet
  83. * those differences can get folded into this clock provider support
  84. * code and don't warrant a separate highly redundant implementation
  85. */
  86. static enum soc_type {
  87. MPC512x_SOC_MPC5121,
  88. MPC512x_SOC_MPC5123,
  89. MPC512x_SOC_MPC5125,
  90. } soc;
  91. static void mpc512x_clk_determine_soc(void)
  92. {
  93. if (of_machine_is_compatible("fsl,mpc5121")) {
  94. soc = MPC512x_SOC_MPC5121;
  95. return;
  96. }
  97. if (of_machine_is_compatible("fsl,mpc5123")) {
  98. soc = MPC512x_SOC_MPC5123;
  99. return;
  100. }
  101. if (of_machine_is_compatible("fsl,mpc5125")) {
  102. soc = MPC512x_SOC_MPC5125;
  103. return;
  104. }
  105. }
  106. static bool soc_has_mbx(void)
  107. {
  108. if (soc == MPC512x_SOC_MPC5121)
  109. return true;
  110. return false;
  111. }
  112. static bool soc_has_axe(void)
  113. {
  114. if (soc == MPC512x_SOC_MPC5125)
  115. return false;
  116. return true;
  117. }
  118. static bool soc_has_viu(void)
  119. {
  120. if (soc == MPC512x_SOC_MPC5125)
  121. return false;
  122. return true;
  123. }
  124. static bool soc_has_spdif(void)
  125. {
  126. if (soc == MPC512x_SOC_MPC5125)
  127. return false;
  128. return true;
  129. }
  130. static bool soc_has_pata(void)
  131. {
  132. if (soc == MPC512x_SOC_MPC5125)
  133. return false;
  134. return true;
  135. }
  136. static bool soc_has_sata(void)
  137. {
  138. if (soc == MPC512x_SOC_MPC5125)
  139. return false;
  140. return true;
  141. }
  142. static bool soc_has_pci(void)
  143. {
  144. if (soc == MPC512x_SOC_MPC5125)
  145. return false;
  146. return true;
  147. }
  148. static bool soc_has_fec2(void)
  149. {
  150. if (soc == MPC512x_SOC_MPC5125)
  151. return true;
  152. return false;
  153. }
  154. static int soc_max_pscnum(void)
  155. {
  156. if (soc == MPC512x_SOC_MPC5125)
  157. return 10;
  158. return 12;
  159. }
  160. static bool soc_has_sdhc2(void)
  161. {
  162. if (soc == MPC512x_SOC_MPC5125)
  163. return true;
  164. return false;
  165. }
  166. static bool soc_has_nfc_5125(void)
  167. {
  168. if (soc == MPC512x_SOC_MPC5125)
  169. return true;
  170. return false;
  171. }
  172. static bool soc_has_outclk(void)
  173. {
  174. if (soc == MPC512x_SOC_MPC5125)
  175. return true;
  176. return false;
  177. }
  178. static bool soc_has_cpmf_0_bypass(void)
  179. {
  180. if (soc == MPC512x_SOC_MPC5125)
  181. return true;
  182. return false;
  183. }
  184. static bool soc_has_mclk_mux0_canin(void)
  185. {
  186. if (soc == MPC512x_SOC_MPC5125)
  187. return true;
  188. return false;
  189. }
  190. /* }}} SoC variants */
  191. /* common clk API wrappers {{{ */
  192. /* convenience wrappers around the common clk API */
  193. static inline struct clk *mpc512x_clk_fixed(const char *name, int rate)
  194. {
  195. return clk_register_fixed_rate(NULL, name, NULL, CLK_IS_ROOT, rate);
  196. }
  197. static inline struct clk *mpc512x_clk_factor(
  198. const char *name, const char *parent_name,
  199. int mul, int div)
  200. {
  201. int clkflags;
  202. clkflags = CLK_SET_RATE_PARENT;
  203. return clk_register_fixed_factor(NULL, name, parent_name, clkflags,
  204. mul, div);
  205. }
  206. static inline struct clk *mpc512x_clk_divider(
  207. const char *name, const char *parent_name, u8 clkflags,
  208. u32 __iomem *reg, u8 pos, u8 len, int divflags)
  209. {
  210. return clk_register_divider(NULL, name, parent_name, clkflags,
  211. reg, pos, len, divflags, &clklock);
  212. }
  213. static inline struct clk *mpc512x_clk_divtable(
  214. const char *name, const char *parent_name,
  215. u32 __iomem *reg, u8 pos, u8 len,
  216. const struct clk_div_table *divtab)
  217. {
  218. u8 divflags;
  219. divflags = 0;
  220. return clk_register_divider_table(NULL, name, parent_name, 0,
  221. reg, pos, len, divflags,
  222. divtab, &clklock);
  223. }
  224. static inline struct clk *mpc512x_clk_gated(
  225. const char *name, const char *parent_name,
  226. u32 __iomem *reg, u8 pos)
  227. {
  228. int clkflags;
  229. clkflags = CLK_SET_RATE_PARENT;
  230. return clk_register_gate(NULL, name, parent_name, clkflags,
  231. reg, pos, 0, &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 = 0;
  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 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 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 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 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 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 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 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 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 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. * individial '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 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 componsites (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 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 mpc5121_clk_provide_migration_support(void)
  858. {
  859. /*
  860. * pre-enable those clock items which are not yet appropriately
  861. * acquired by their peripheral driver
  862. *
  863. * the PCI clock cannot get acquired by its peripheral driver,
  864. * because for this platform the driver won't probe(), instead
  865. * initialization is done from within the .setup_arch() routine
  866. * at a point in time where the clock provider has not been
  867. * setup yet and thus isn't available yet
  868. *
  869. * so we "pre-enable" the clock here, to not have the clock
  870. * subsystem automatically disable this item in a late init call
  871. *
  872. * this PCI clock pre-enable workaround only applies when there
  873. * are device tree nodes for PCI and thus the peripheral driver
  874. * has attached to bridges, otherwise the PCI clock remains
  875. * unused and so it gets disabled
  876. */
  877. clk_prepare_enable(clks[MPC512x_CLK_PSC3_MCLK]);/* serial console */
  878. if (of_find_compatible_node(NULL, "pci", "fsl,mpc5121-pci"))
  879. clk_prepare_enable(clks[MPC512x_CLK_PCI]);
  880. }
  881. /*
  882. * those macros are not exactly pretty, but they encapsulate a lot
  883. * of copy'n'paste heavy code which is even more ugly, and reduce
  884. * the potential for inconsistencies in those many code copies
  885. */
  886. #define FOR_NODES(compatname) \
  887. for_each_compatible_node(np, NULL, compatname)
  888. #define NODE_PREP do { \
  889. of_address_to_resource(np, 0, &res); \
  890. snprintf(devname, sizeof(devname), "%08x.%s", res.start, np->name); \
  891. } while (0)
  892. #define NODE_CHK(clkname, clkitem, regnode, regflag) do { \
  893. struct clk *clk; \
  894. clk = of_clk_get_by_name(np, clkname); \
  895. if (IS_ERR(clk)) { \
  896. clk = clkitem; \
  897. clk_register_clkdev(clk, clkname, devname); \
  898. if (regnode) \
  899. clk_register_clkdev(clk, clkname, np->name); \
  900. did_register |= DID_REG_ ## regflag; \
  901. pr_debug("clock alias name '%s' for dev '%s' pointer %p\n", \
  902. clkname, devname, clk); \
  903. } else { \
  904. clk_put(clk); \
  905. } \
  906. } while (0)
  907. /*
  908. * register source code provided fallback results for clock lookups,
  909. * these get consulted when OF based clock lookup fails (that is in the
  910. * case of not yet adjusted device tree data, where clock related specs
  911. * are missing)
  912. */
  913. static void mpc5121_clk_provide_backwards_compat(void)
  914. {
  915. enum did_reg_flags {
  916. DID_REG_PSC = BIT(0),
  917. DID_REG_PSCFIFO = BIT(1),
  918. DID_REG_NFC = BIT(2),
  919. DID_REG_CAN = BIT(3),
  920. DID_REG_I2C = BIT(4),
  921. DID_REG_DIU = BIT(5),
  922. DID_REG_VIU = BIT(6),
  923. DID_REG_FEC = BIT(7),
  924. DID_REG_USB = BIT(8),
  925. DID_REG_PATA = BIT(9),
  926. };
  927. int did_register;
  928. struct device_node *np;
  929. struct resource res;
  930. int idx;
  931. char devname[32];
  932. did_register = 0;
  933. FOR_NODES(mpc512x_select_psc_compat()) {
  934. NODE_PREP;
  935. idx = (res.start >> 8) & 0xf;
  936. NODE_CHK("ipg", clks[MPC512x_CLK_PSC0 + idx], 0, PSC);
  937. NODE_CHK("mclk", clks[MPC512x_CLK_PSC0_MCLK + idx], 0, PSC);
  938. }
  939. FOR_NODES("fsl,mpc5121-psc-fifo") {
  940. NODE_PREP;
  941. NODE_CHK("ipg", clks[MPC512x_CLK_PSC_FIFO], 1, PSCFIFO);
  942. }
  943. FOR_NODES("fsl,mpc5121-nfc") {
  944. NODE_PREP;
  945. NODE_CHK("ipg", clks[MPC512x_CLK_NFC], 0, NFC);
  946. }
  947. FOR_NODES("fsl,mpc5121-mscan") {
  948. NODE_PREP;
  949. idx = 0;
  950. idx += (res.start & 0x2000) ? 2 : 0;
  951. idx += (res.start & 0x0080) ? 1 : 0;
  952. NODE_CHK("ipg", clks[MPC512x_CLK_BDLC], 0, CAN);
  953. NODE_CHK("mclk", clks[MPC512x_CLK_MSCAN0_MCLK + idx], 0, CAN);
  954. }
  955. /*
  956. * do register the 'ips', 'sys', and 'ref' names globally
  957. * instead of inside each individual CAN node, as there is no
  958. * potential for a name conflict (in contrast to 'ipg' and 'mclk')
  959. */
  960. if (did_register & DID_REG_CAN) {
  961. clk_register_clkdev(clks[MPC512x_CLK_IPS], "ips", NULL);
  962. clk_register_clkdev(clks[MPC512x_CLK_SYS], "sys", NULL);
  963. clk_register_clkdev(clks[MPC512x_CLK_REF], "ref", NULL);
  964. }
  965. FOR_NODES("fsl,mpc5121-i2c") {
  966. NODE_PREP;
  967. NODE_CHK("ipg", clks[MPC512x_CLK_I2C], 0, I2C);
  968. }
  969. /*
  970. * workaround for the fact that the I2C driver does an "anonymous"
  971. * lookup (NULL name spec, which yields the first clock spec) for
  972. * which we cannot register an alias -- a _global_ 'ipg' alias that
  973. * is not bound to any device name and returns the I2C clock item
  974. * is not a good idea
  975. *
  976. * so we have the lookup in the peripheral driver fail, which is
  977. * silent and non-fatal, and pre-enable the clock item here such
  978. * that register access is possible
  979. *
  980. * see commit b3bfce2b "i2c: mpc: cleanup clock API use" for
  981. * details, adjusting s/NULL/"ipg"/ in i2c-mpc.c would make this
  982. * workaround obsolete
  983. */
  984. if (did_register & DID_REG_I2C)
  985. clk_prepare_enable(clks[MPC512x_CLK_I2C]);
  986. FOR_NODES("fsl,mpc5121-diu") {
  987. NODE_PREP;
  988. NODE_CHK("ipg", clks[MPC512x_CLK_DIU], 1, DIU);
  989. }
  990. FOR_NODES("fsl,mpc5121-viu") {
  991. NODE_PREP;
  992. NODE_CHK("ipg", clks[MPC512x_CLK_VIU], 0, VIU);
  993. }
  994. /*
  995. * note that 2771399a "fs_enet: cleanup clock API use" did use the
  996. * "per" string for the clock lookup in contrast to the "ipg" name
  997. * which most other nodes are using -- this is not a fatal thing
  998. * but just something to keep in mind when doing compatibility
  999. * registration, it's a non-issue with up-to-date device tree data
  1000. */
  1001. FOR_NODES("fsl,mpc5121-fec") {
  1002. NODE_PREP;
  1003. NODE_CHK("per", clks[MPC512x_CLK_FEC], 0, FEC);
  1004. }
  1005. FOR_NODES("fsl,mpc5121-fec-mdio") {
  1006. NODE_PREP;
  1007. NODE_CHK("per", clks[MPC512x_CLK_FEC], 0, FEC);
  1008. }
  1009. /*
  1010. * MPC5125 has two FECs: FEC1 at 0x2800, FEC2 at 0x4800;
  1011. * the clock items don't "form an array" since FEC2 was
  1012. * added only later and was not allowed to shift all other
  1013. * clock item indices, so the numbers aren't adjacent
  1014. */
  1015. FOR_NODES("fsl,mpc5125-fec") {
  1016. NODE_PREP;
  1017. if (res.start & 0x4000)
  1018. idx = MPC512x_CLK_FEC2;
  1019. else
  1020. idx = MPC512x_CLK_FEC;
  1021. NODE_CHK("per", clks[idx], 0, FEC);
  1022. }
  1023. FOR_NODES("fsl,mpc5121-usb2-dr") {
  1024. NODE_PREP;
  1025. idx = (res.start & 0x4000) ? 1 : 0;
  1026. NODE_CHK("ipg", clks[MPC512x_CLK_USB1 + idx], 0, USB);
  1027. }
  1028. FOR_NODES("fsl,mpc5121-pata") {
  1029. NODE_PREP;
  1030. NODE_CHK("ipg", clks[MPC512x_CLK_PATA], 0, PATA);
  1031. }
  1032. /*
  1033. * try to collapse diagnostics into a single line of output yet
  1034. * provide a full list of what is missing, to avoid noise in the
  1035. * absence of up-to-date device tree data -- backwards
  1036. * compatibility to old DTBs is a requirement, updates may be
  1037. * desirable or preferrable but are not at all mandatory
  1038. */
  1039. if (did_register) {
  1040. pr_notice("device tree lacks clock specs, adding fallbacks (0x%x,%s%s%s%s%s%s%s%s%s%s)\n",
  1041. did_register,
  1042. (did_register & DID_REG_PSC) ? " PSC" : "",
  1043. (did_register & DID_REG_PSCFIFO) ? " PSCFIFO" : "",
  1044. (did_register & DID_REG_NFC) ? " NFC" : "",
  1045. (did_register & DID_REG_CAN) ? " CAN" : "",
  1046. (did_register & DID_REG_I2C) ? " I2C" : "",
  1047. (did_register & DID_REG_DIU) ? " DIU" : "",
  1048. (did_register & DID_REG_VIU) ? " VIU" : "",
  1049. (did_register & DID_REG_FEC) ? " FEC" : "",
  1050. (did_register & DID_REG_USB) ? " USB" : "",
  1051. (did_register & DID_REG_PATA) ? " PATA" : "");
  1052. } else {
  1053. pr_debug("device tree has clock specs, no fallbacks added\n");
  1054. }
  1055. }
  1056. /*
  1057. * The "fixed-clock" nodes (which includes the oscillator node if the board's
  1058. * DT provides one) has already been scanned by the of_clk_init() in
  1059. * time_init().
  1060. */
  1061. int __init mpc5121_clk_init(void)
  1062. {
  1063. struct device_node *clk_np;
  1064. int busfreq;
  1065. /* map the clock control registers */
  1066. clk_np = of_find_compatible_node(NULL, NULL, "fsl,mpc5121-clock");
  1067. if (!clk_np)
  1068. return -ENODEV;
  1069. clkregs = of_iomap(clk_np, 0);
  1070. WARN_ON(!clkregs);
  1071. /* determine the SoC variant we run on */
  1072. mpc512x_clk_determine_soc();
  1073. /* invalidate all not yet registered clock slots */
  1074. mpc512x_clk_preset_data();
  1075. /*
  1076. * add a dummy clock for those situations where a clock spec is
  1077. * required yet no real clock is involved
  1078. */
  1079. clks[MPC512x_CLK_DUMMY] = mpc512x_clk_fixed("dummy", 0);
  1080. /*
  1081. * have all the real nodes in the clock tree populated from REF
  1082. * down to all leaves, either starting from the OSC node or from
  1083. * a REF root that was created from the IPS bus clock input
  1084. */
  1085. busfreq = get_freq_from_dt("bus-frequency");
  1086. mpc512x_clk_setup_clock_tree(clk_np, busfreq);
  1087. /* register as an OF clock provider */
  1088. mpc5121_clk_register_of_provider(clk_np);
  1089. /*
  1090. * unbreak not yet adjusted peripheral drivers during migration
  1091. * towards fully operational common clock support, and allow
  1092. * operation in the absence of clock related device tree specs
  1093. */
  1094. mpc5121_clk_provide_migration_support();
  1095. mpc5121_clk_provide_backwards_compat();
  1096. return 0;
  1097. }