clk-qoriq.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437
  1. /*
  2. * Copyright 2013 Freescale Semiconductor, Inc.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. * clock driver for Freescale QorIQ SoCs.
  9. */
  10. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  11. #include <linux/clk.h>
  12. #include <linux/clk-provider.h>
  13. #include <linux/clkdev.h>
  14. #include <linux/fsl/guts.h>
  15. #include <linux/io.h>
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/of_address.h>
  19. #include <linux/of_platform.h>
  20. #include <linux/of.h>
  21. #include <linux/slab.h>
  22. #define PLL_DIV1 0
  23. #define PLL_DIV2 1
  24. #define PLL_DIV3 2
  25. #define PLL_DIV4 3
  26. #define PLATFORM_PLL 0
  27. #define CGA_PLL1 1
  28. #define CGA_PLL2 2
  29. #define CGA_PLL3 3
  30. #define CGA_PLL4 4 /* only on clockgen-1.0, which lacks CGB */
  31. #define CGB_PLL1 4
  32. #define CGB_PLL2 5
  33. struct clockgen_pll_div {
  34. struct clk *clk;
  35. char name[32];
  36. };
  37. struct clockgen_pll {
  38. struct clockgen_pll_div div[8];
  39. };
  40. #define CLKSEL_VALID 1
  41. #define CLKSEL_80PCT 2 /* Only allowed if PLL <= 80% of max cpu freq */
  42. struct clockgen_sourceinfo {
  43. u32 flags; /* CLKSEL_xxx */
  44. int pll; /* CGx_PLLn */
  45. int div; /* PLL_DIVn */
  46. };
  47. #define NUM_MUX_PARENTS 16
  48. struct clockgen_muxinfo {
  49. struct clockgen_sourceinfo clksel[NUM_MUX_PARENTS];
  50. };
  51. #define NUM_HWACCEL 5
  52. #define NUM_CMUX 8
  53. struct clockgen;
  54. /*
  55. * cmux freq must be >= platform pll.
  56. * If not set, cmux freq must be >= platform pll/2
  57. */
  58. #define CG_CMUX_GE_PLAT 1
  59. #define CG_PLL_8BIT 2 /* PLLCnGSR[CFG] is 8 bits, not 6 */
  60. #define CG_VER3 4 /* version 3 cg: reg layout different */
  61. #define CG_LITTLE_ENDIAN 8
  62. struct clockgen_chipinfo {
  63. const char *compat, *guts_compat;
  64. const struct clockgen_muxinfo *cmux_groups[2];
  65. const struct clockgen_muxinfo *hwaccel[NUM_HWACCEL];
  66. void (*init_periph)(struct clockgen *cg);
  67. int cmux_to_group[NUM_CMUX]; /* -1 terminates if fewer than NUM_CMUX */
  68. u32 pll_mask; /* 1 << n bit set if PLL n is valid */
  69. u32 flags; /* CG_xxx */
  70. };
  71. struct clockgen {
  72. struct device_node *node;
  73. void __iomem *regs;
  74. struct clockgen_chipinfo info; /* mutable copy */
  75. struct clk *sysclk, *coreclk;
  76. struct clockgen_pll pll[6];
  77. struct clk *cmux[NUM_CMUX];
  78. struct clk *hwaccel[NUM_HWACCEL];
  79. struct clk *fman[2];
  80. struct ccsr_guts __iomem *guts;
  81. };
  82. static struct clockgen clockgen;
  83. static void cg_out(struct clockgen *cg, u32 val, u32 __iomem *reg)
  84. {
  85. if (cg->info.flags & CG_LITTLE_ENDIAN)
  86. iowrite32(val, reg);
  87. else
  88. iowrite32be(val, reg);
  89. }
  90. static u32 cg_in(struct clockgen *cg, u32 __iomem *reg)
  91. {
  92. u32 val;
  93. if (cg->info.flags & CG_LITTLE_ENDIAN)
  94. val = ioread32(reg);
  95. else
  96. val = ioread32be(reg);
  97. return val;
  98. }
  99. static const struct clockgen_muxinfo p2041_cmux_grp1 = {
  100. {
  101. [0] = { CLKSEL_VALID, CGA_PLL1, PLL_DIV1 },
  102. [1] = { CLKSEL_VALID, CGA_PLL1, PLL_DIV2 },
  103. [4] = { CLKSEL_VALID, CGA_PLL2, PLL_DIV1 },
  104. }
  105. };
  106. static const struct clockgen_muxinfo p2041_cmux_grp2 = {
  107. {
  108. [0] = { CLKSEL_VALID, CGA_PLL1, PLL_DIV1 },
  109. [4] = { CLKSEL_VALID, CGA_PLL2, PLL_DIV1 },
  110. [5] = { CLKSEL_VALID, CGA_PLL2, PLL_DIV2 },
  111. }
  112. };
  113. static const struct clockgen_muxinfo p5020_cmux_grp1 = {
  114. {
  115. [0] = { CLKSEL_VALID, CGA_PLL1, PLL_DIV1 },
  116. [1] = { CLKSEL_VALID, CGA_PLL1, PLL_DIV2 },
  117. [4] = { CLKSEL_VALID | CLKSEL_80PCT, CGA_PLL2, PLL_DIV1 },
  118. }
  119. };
  120. static const struct clockgen_muxinfo p5020_cmux_grp2 = {
  121. {
  122. [0] = { CLKSEL_VALID | CLKSEL_80PCT, CGA_PLL1, PLL_DIV1 },
  123. [4] = { CLKSEL_VALID, CGA_PLL2, PLL_DIV1 },
  124. [5] = { CLKSEL_VALID, CGA_PLL2, PLL_DIV2 },
  125. }
  126. };
  127. static const struct clockgen_muxinfo p5040_cmux_grp1 = {
  128. {
  129. [0] = { CLKSEL_VALID, CGA_PLL1, PLL_DIV1 },
  130. [1] = { CLKSEL_VALID, CGA_PLL1, PLL_DIV2 },
  131. [4] = { CLKSEL_VALID | CLKSEL_80PCT, CGA_PLL2, PLL_DIV1 },
  132. [5] = { CLKSEL_VALID | CLKSEL_80PCT, CGA_PLL2, PLL_DIV2 },
  133. }
  134. };
  135. static const struct clockgen_muxinfo p5040_cmux_grp2 = {
  136. {
  137. [0] = { CLKSEL_VALID | CLKSEL_80PCT, CGA_PLL1, PLL_DIV1 },
  138. [1] = { CLKSEL_VALID | CLKSEL_80PCT, CGA_PLL1, PLL_DIV2 },
  139. [4] = { CLKSEL_VALID, CGA_PLL2, PLL_DIV1 },
  140. [5] = { CLKSEL_VALID, CGA_PLL2, PLL_DIV2 },
  141. }
  142. };
  143. static const struct clockgen_muxinfo p4080_cmux_grp1 = {
  144. {
  145. [0] = { CLKSEL_VALID, CGA_PLL1, PLL_DIV1 },
  146. [1] = { CLKSEL_VALID, CGA_PLL1, PLL_DIV2 },
  147. [4] = { CLKSEL_VALID, CGA_PLL2, PLL_DIV1 },
  148. [5] = { CLKSEL_VALID, CGA_PLL2, PLL_DIV2 },
  149. [8] = { CLKSEL_VALID | CLKSEL_80PCT, CGA_PLL3, PLL_DIV1 },
  150. }
  151. };
  152. static const struct clockgen_muxinfo p4080_cmux_grp2 = {
  153. {
  154. [0] = { CLKSEL_VALID | CLKSEL_80PCT, CGA_PLL1, PLL_DIV1 },
  155. [8] = { CLKSEL_VALID, CGA_PLL3, PLL_DIV1 },
  156. [9] = { CLKSEL_VALID, CGA_PLL3, PLL_DIV2 },
  157. [12] = { CLKSEL_VALID, CGA_PLL4, PLL_DIV1 },
  158. [13] = { CLKSEL_VALID, CGA_PLL4, PLL_DIV2 },
  159. }
  160. };
  161. static const struct clockgen_muxinfo t1023_cmux = {
  162. {
  163. [0] = { CLKSEL_VALID, CGA_PLL1, PLL_DIV1 },
  164. [1] = { CLKSEL_VALID, CGA_PLL1, PLL_DIV2 },
  165. }
  166. };
  167. static const struct clockgen_muxinfo t1040_cmux = {
  168. {
  169. [0] = { CLKSEL_VALID, CGA_PLL1, PLL_DIV1 },
  170. [1] = { CLKSEL_VALID, CGA_PLL1, PLL_DIV2 },
  171. [4] = { CLKSEL_VALID, CGA_PLL2, PLL_DIV1 },
  172. [5] = { CLKSEL_VALID, CGA_PLL2, PLL_DIV2 },
  173. }
  174. };
  175. static const struct clockgen_muxinfo clockgen2_cmux_cga = {
  176. {
  177. { CLKSEL_VALID, CGA_PLL1, PLL_DIV1 },
  178. { CLKSEL_VALID, CGA_PLL1, PLL_DIV2 },
  179. { CLKSEL_VALID, CGA_PLL1, PLL_DIV4 },
  180. {},
  181. { CLKSEL_VALID, CGA_PLL2, PLL_DIV1 },
  182. { CLKSEL_VALID, CGA_PLL2, PLL_DIV2 },
  183. { CLKSEL_VALID, CGA_PLL2, PLL_DIV4 },
  184. {},
  185. { CLKSEL_VALID, CGA_PLL3, PLL_DIV1 },
  186. { CLKSEL_VALID, CGA_PLL3, PLL_DIV2 },
  187. { CLKSEL_VALID, CGA_PLL3, PLL_DIV4 },
  188. },
  189. };
  190. static const struct clockgen_muxinfo clockgen2_cmux_cga12 = {
  191. {
  192. { CLKSEL_VALID, CGA_PLL1, PLL_DIV1 },
  193. { CLKSEL_VALID, CGA_PLL1, PLL_DIV2 },
  194. { CLKSEL_VALID, CGA_PLL1, PLL_DIV4 },
  195. {},
  196. { CLKSEL_VALID, CGA_PLL2, PLL_DIV1 },
  197. { CLKSEL_VALID, CGA_PLL2, PLL_DIV2 },
  198. { CLKSEL_VALID, CGA_PLL2, PLL_DIV4 },
  199. },
  200. };
  201. static const struct clockgen_muxinfo clockgen2_cmux_cgb = {
  202. {
  203. { CLKSEL_VALID, CGB_PLL1, PLL_DIV1 },
  204. { CLKSEL_VALID, CGB_PLL1, PLL_DIV2 },
  205. { CLKSEL_VALID, CGB_PLL1, PLL_DIV4 },
  206. {},
  207. { CLKSEL_VALID, CGB_PLL2, PLL_DIV1 },
  208. { CLKSEL_VALID, CGB_PLL2, PLL_DIV2 },
  209. { CLKSEL_VALID, CGB_PLL2, PLL_DIV4 },
  210. },
  211. };
  212. static const struct clockgen_muxinfo ls1043a_hwa1 = {
  213. {
  214. {},
  215. {},
  216. { CLKSEL_VALID, CGA_PLL1, PLL_DIV2 },
  217. { CLKSEL_VALID, CGA_PLL1, PLL_DIV3 },
  218. {},
  219. {},
  220. { CLKSEL_VALID, CGA_PLL2, PLL_DIV2 },
  221. { CLKSEL_VALID, CGA_PLL2, PLL_DIV3 },
  222. },
  223. };
  224. static const struct clockgen_muxinfo ls1043a_hwa2 = {
  225. {
  226. {},
  227. { CLKSEL_VALID, CGA_PLL2, PLL_DIV1 },
  228. {},
  229. { CLKSEL_VALID, CGA_PLL2, PLL_DIV3 },
  230. },
  231. };
  232. static const struct clockgen_muxinfo ls1046a_hwa1 = {
  233. {
  234. {},
  235. {},
  236. { CLKSEL_VALID, CGA_PLL1, PLL_DIV2 },
  237. { CLKSEL_VALID, CGA_PLL1, PLL_DIV3 },
  238. { CLKSEL_VALID, CGA_PLL1, PLL_DIV4 },
  239. { CLKSEL_VALID, PLATFORM_PLL, PLL_DIV1 },
  240. { CLKSEL_VALID, CGA_PLL2, PLL_DIV2 },
  241. { CLKSEL_VALID, CGA_PLL2, PLL_DIV3 },
  242. },
  243. };
  244. static const struct clockgen_muxinfo ls1046a_hwa2 = {
  245. {
  246. {},
  247. { CLKSEL_VALID, CGA_PLL2, PLL_DIV1 },
  248. { CLKSEL_VALID, CGA_PLL2, PLL_DIV2 },
  249. { CLKSEL_VALID, CGA_PLL2, PLL_DIV3 },
  250. {},
  251. {},
  252. { CLKSEL_VALID, CGA_PLL1, PLL_DIV2 },
  253. },
  254. };
  255. static const struct clockgen_muxinfo ls1012a_cmux = {
  256. {
  257. [0] = { CLKSEL_VALID, CGA_PLL1, PLL_DIV1 },
  258. {},
  259. [2] = { CLKSEL_VALID, CGA_PLL1, PLL_DIV2 },
  260. }
  261. };
  262. static const struct clockgen_muxinfo t1023_hwa1 = {
  263. {
  264. {},
  265. { CLKSEL_VALID, CGA_PLL1, PLL_DIV1 },
  266. { CLKSEL_VALID, CGA_PLL1, PLL_DIV2 },
  267. { CLKSEL_VALID, CGA_PLL1, PLL_DIV3 },
  268. },
  269. };
  270. static const struct clockgen_muxinfo t1023_hwa2 = {
  271. {
  272. [6] = { CLKSEL_VALID, CGA_PLL1, PLL_DIV2 },
  273. },
  274. };
  275. static const struct clockgen_muxinfo t2080_hwa1 = {
  276. {
  277. {},
  278. { CLKSEL_VALID, CGA_PLL1, PLL_DIV1 },
  279. { CLKSEL_VALID, CGA_PLL1, PLL_DIV2 },
  280. { CLKSEL_VALID, CGA_PLL1, PLL_DIV3 },
  281. { CLKSEL_VALID, CGA_PLL1, PLL_DIV4 },
  282. { CLKSEL_VALID, PLATFORM_PLL, PLL_DIV1 },
  283. { CLKSEL_VALID, CGA_PLL2, PLL_DIV2 },
  284. { CLKSEL_VALID, CGA_PLL2, PLL_DIV3 },
  285. },
  286. };
  287. static const struct clockgen_muxinfo t2080_hwa2 = {
  288. {
  289. {},
  290. { CLKSEL_VALID, CGA_PLL2, PLL_DIV1 },
  291. { CLKSEL_VALID, CGA_PLL2, PLL_DIV2 },
  292. { CLKSEL_VALID, CGA_PLL2, PLL_DIV3 },
  293. { CLKSEL_VALID, CGA_PLL2, PLL_DIV4 },
  294. { CLKSEL_VALID, PLATFORM_PLL, PLL_DIV1 },
  295. { CLKSEL_VALID, CGA_PLL1, PLL_DIV2 },
  296. { CLKSEL_VALID, CGA_PLL1, PLL_DIV3 },
  297. },
  298. };
  299. static const struct clockgen_muxinfo t4240_hwa1 = {
  300. {
  301. { CLKSEL_VALID, PLATFORM_PLL, PLL_DIV2 },
  302. { CLKSEL_VALID, CGA_PLL1, PLL_DIV1 },
  303. { CLKSEL_VALID, CGA_PLL1, PLL_DIV2 },
  304. { CLKSEL_VALID, CGA_PLL1, PLL_DIV3 },
  305. { CLKSEL_VALID, CGA_PLL1, PLL_DIV4 },
  306. {},
  307. { CLKSEL_VALID, CGA_PLL2, PLL_DIV2 },
  308. { CLKSEL_VALID, CGA_PLL2, PLL_DIV3 },
  309. },
  310. };
  311. static const struct clockgen_muxinfo t4240_hwa4 = {
  312. {
  313. [2] = { CLKSEL_VALID, CGB_PLL1, PLL_DIV2 },
  314. [3] = { CLKSEL_VALID, CGB_PLL1, PLL_DIV3 },
  315. [4] = { CLKSEL_VALID, CGB_PLL1, PLL_DIV4 },
  316. [5] = { CLKSEL_VALID, PLATFORM_PLL, PLL_DIV1 },
  317. [6] = { CLKSEL_VALID, CGB_PLL2, PLL_DIV2 },
  318. },
  319. };
  320. static const struct clockgen_muxinfo t4240_hwa5 = {
  321. {
  322. [2] = { CLKSEL_VALID, CGB_PLL2, PLL_DIV2 },
  323. [3] = { CLKSEL_VALID, CGB_PLL2, PLL_DIV3 },
  324. [4] = { CLKSEL_VALID, CGB_PLL2, PLL_DIV4 },
  325. [5] = { CLKSEL_VALID, PLATFORM_PLL, PLL_DIV1 },
  326. [6] = { CLKSEL_VALID, CGB_PLL1, PLL_DIV2 },
  327. [7] = { CLKSEL_VALID, CGB_PLL1, PLL_DIV3 },
  328. },
  329. };
  330. #define RCWSR7_FM1_CLK_SEL 0x40000000
  331. #define RCWSR7_FM2_CLK_SEL 0x20000000
  332. #define RCWSR7_HWA_ASYNC_DIV 0x04000000
  333. static void __init p2041_init_periph(struct clockgen *cg)
  334. {
  335. u32 reg;
  336. reg = ioread32be(&cg->guts->rcwsr[7]);
  337. if (reg & RCWSR7_FM1_CLK_SEL)
  338. cg->fman[0] = cg->pll[CGA_PLL2].div[PLL_DIV2].clk;
  339. else
  340. cg->fman[0] = cg->pll[PLATFORM_PLL].div[PLL_DIV2].clk;
  341. }
  342. static void __init p4080_init_periph(struct clockgen *cg)
  343. {
  344. u32 reg;
  345. reg = ioread32be(&cg->guts->rcwsr[7]);
  346. if (reg & RCWSR7_FM1_CLK_SEL)
  347. cg->fman[0] = cg->pll[CGA_PLL3].div[PLL_DIV2].clk;
  348. else
  349. cg->fman[0] = cg->pll[PLATFORM_PLL].div[PLL_DIV2].clk;
  350. if (reg & RCWSR7_FM2_CLK_SEL)
  351. cg->fman[1] = cg->pll[CGA_PLL3].div[PLL_DIV2].clk;
  352. else
  353. cg->fman[1] = cg->pll[PLATFORM_PLL].div[PLL_DIV2].clk;
  354. }
  355. static void __init p5020_init_periph(struct clockgen *cg)
  356. {
  357. u32 reg;
  358. int div = PLL_DIV2;
  359. reg = ioread32be(&cg->guts->rcwsr[7]);
  360. if (reg & RCWSR7_HWA_ASYNC_DIV)
  361. div = PLL_DIV4;
  362. if (reg & RCWSR7_FM1_CLK_SEL)
  363. cg->fman[0] = cg->pll[CGA_PLL2].div[div].clk;
  364. else
  365. cg->fman[0] = cg->pll[PLATFORM_PLL].div[PLL_DIV2].clk;
  366. }
  367. static void __init p5040_init_periph(struct clockgen *cg)
  368. {
  369. u32 reg;
  370. int div = PLL_DIV2;
  371. reg = ioread32be(&cg->guts->rcwsr[7]);
  372. if (reg & RCWSR7_HWA_ASYNC_DIV)
  373. div = PLL_DIV4;
  374. if (reg & RCWSR7_FM1_CLK_SEL)
  375. cg->fman[0] = cg->pll[CGA_PLL3].div[div].clk;
  376. else
  377. cg->fman[0] = cg->pll[PLATFORM_PLL].div[PLL_DIV2].clk;
  378. if (reg & RCWSR7_FM2_CLK_SEL)
  379. cg->fman[1] = cg->pll[CGA_PLL3].div[div].clk;
  380. else
  381. cg->fman[1] = cg->pll[PLATFORM_PLL].div[PLL_DIV2].clk;
  382. }
  383. static void __init t1023_init_periph(struct clockgen *cg)
  384. {
  385. cg->fman[0] = cg->hwaccel[1];
  386. }
  387. static void __init t1040_init_periph(struct clockgen *cg)
  388. {
  389. cg->fman[0] = cg->pll[PLATFORM_PLL].div[PLL_DIV1].clk;
  390. }
  391. static void __init t2080_init_periph(struct clockgen *cg)
  392. {
  393. cg->fman[0] = cg->hwaccel[0];
  394. }
  395. static void __init t4240_init_periph(struct clockgen *cg)
  396. {
  397. cg->fman[0] = cg->hwaccel[3];
  398. cg->fman[1] = cg->hwaccel[4];
  399. }
  400. static const struct clockgen_chipinfo chipinfo[] = {
  401. {
  402. .compat = "fsl,b4420-clockgen",
  403. .guts_compat = "fsl,b4860-device-config",
  404. .init_periph = t2080_init_periph,
  405. .cmux_groups = {
  406. &clockgen2_cmux_cga12, &clockgen2_cmux_cgb
  407. },
  408. .hwaccel = {
  409. &t2080_hwa1
  410. },
  411. .cmux_to_group = {
  412. 0, 1, 1, 1, -1
  413. },
  414. .pll_mask = 0x3f,
  415. .flags = CG_PLL_8BIT,
  416. },
  417. {
  418. .compat = "fsl,b4860-clockgen",
  419. .guts_compat = "fsl,b4860-device-config",
  420. .init_periph = t2080_init_periph,
  421. .cmux_groups = {
  422. &clockgen2_cmux_cga12, &clockgen2_cmux_cgb
  423. },
  424. .hwaccel = {
  425. &t2080_hwa1
  426. },
  427. .cmux_to_group = {
  428. 0, 1, 1, 1, -1
  429. },
  430. .pll_mask = 0x3f,
  431. .flags = CG_PLL_8BIT,
  432. },
  433. {
  434. .compat = "fsl,ls1021a-clockgen",
  435. .cmux_groups = {
  436. &t1023_cmux
  437. },
  438. .cmux_to_group = {
  439. 0, -1
  440. },
  441. .pll_mask = 0x03,
  442. },
  443. {
  444. .compat = "fsl,ls1043a-clockgen",
  445. .init_periph = t2080_init_periph,
  446. .cmux_groups = {
  447. &t1040_cmux
  448. },
  449. .hwaccel = {
  450. &ls1043a_hwa1, &ls1043a_hwa2
  451. },
  452. .cmux_to_group = {
  453. 0, -1
  454. },
  455. .pll_mask = 0x07,
  456. .flags = CG_PLL_8BIT,
  457. },
  458. {
  459. .compat = "fsl,ls1046a-clockgen",
  460. .init_periph = t2080_init_periph,
  461. .cmux_groups = {
  462. &t1040_cmux
  463. },
  464. .hwaccel = {
  465. &ls1046a_hwa1, &ls1046a_hwa2
  466. },
  467. .cmux_to_group = {
  468. 0, -1
  469. },
  470. .pll_mask = 0x07,
  471. .flags = CG_PLL_8BIT,
  472. },
  473. {
  474. .compat = "fsl,ls1088a-clockgen",
  475. .cmux_groups = {
  476. &clockgen2_cmux_cga12
  477. },
  478. .cmux_to_group = {
  479. 0, 0, -1
  480. },
  481. .pll_mask = 0x07,
  482. .flags = CG_VER3 | CG_LITTLE_ENDIAN,
  483. },
  484. {
  485. .compat = "fsl,ls1012a-clockgen",
  486. .cmux_groups = {
  487. &ls1012a_cmux
  488. },
  489. .cmux_to_group = {
  490. 0, -1
  491. },
  492. .pll_mask = 0x03,
  493. },
  494. {
  495. .compat = "fsl,ls2080a-clockgen",
  496. .cmux_groups = {
  497. &clockgen2_cmux_cga12, &clockgen2_cmux_cgb
  498. },
  499. .cmux_to_group = {
  500. 0, 0, 1, 1, -1
  501. },
  502. .pll_mask = 0x37,
  503. .flags = CG_VER3 | CG_LITTLE_ENDIAN,
  504. },
  505. {
  506. .compat = "fsl,p2041-clockgen",
  507. .guts_compat = "fsl,qoriq-device-config-1.0",
  508. .init_periph = p2041_init_periph,
  509. .cmux_groups = {
  510. &p2041_cmux_grp1, &p2041_cmux_grp2
  511. },
  512. .cmux_to_group = {
  513. 0, 0, 1, 1, -1
  514. },
  515. .pll_mask = 0x07,
  516. },
  517. {
  518. .compat = "fsl,p3041-clockgen",
  519. .guts_compat = "fsl,qoriq-device-config-1.0",
  520. .init_periph = p2041_init_periph,
  521. .cmux_groups = {
  522. &p2041_cmux_grp1, &p2041_cmux_grp2
  523. },
  524. .cmux_to_group = {
  525. 0, 0, 1, 1, -1
  526. },
  527. .pll_mask = 0x07,
  528. },
  529. {
  530. .compat = "fsl,p4080-clockgen",
  531. .guts_compat = "fsl,qoriq-device-config-1.0",
  532. .init_periph = p4080_init_periph,
  533. .cmux_groups = {
  534. &p4080_cmux_grp1, &p4080_cmux_grp2
  535. },
  536. .cmux_to_group = {
  537. 0, 0, 0, 0, 1, 1, 1, 1
  538. },
  539. .pll_mask = 0x1f,
  540. },
  541. {
  542. .compat = "fsl,p5020-clockgen",
  543. .guts_compat = "fsl,qoriq-device-config-1.0",
  544. .init_periph = p5020_init_periph,
  545. .cmux_groups = {
  546. &p5020_cmux_grp1, &p5020_cmux_grp2
  547. },
  548. .cmux_to_group = {
  549. 0, 1, -1
  550. },
  551. .pll_mask = 0x07,
  552. },
  553. {
  554. .compat = "fsl,p5040-clockgen",
  555. .guts_compat = "fsl,p5040-device-config",
  556. .init_periph = p5040_init_periph,
  557. .cmux_groups = {
  558. &p5040_cmux_grp1, &p5040_cmux_grp2
  559. },
  560. .cmux_to_group = {
  561. 0, 0, 1, 1, -1
  562. },
  563. .pll_mask = 0x0f,
  564. },
  565. {
  566. .compat = "fsl,t1023-clockgen",
  567. .guts_compat = "fsl,t1023-device-config",
  568. .init_periph = t1023_init_periph,
  569. .cmux_groups = {
  570. &t1023_cmux
  571. },
  572. .hwaccel = {
  573. &t1023_hwa1, &t1023_hwa2
  574. },
  575. .cmux_to_group = {
  576. 0, 0, -1
  577. },
  578. .pll_mask = 0x03,
  579. .flags = CG_PLL_8BIT,
  580. },
  581. {
  582. .compat = "fsl,t1040-clockgen",
  583. .guts_compat = "fsl,t1040-device-config",
  584. .init_periph = t1040_init_periph,
  585. .cmux_groups = {
  586. &t1040_cmux
  587. },
  588. .cmux_to_group = {
  589. 0, 0, 0, 0, -1
  590. },
  591. .pll_mask = 0x07,
  592. .flags = CG_PLL_8BIT,
  593. },
  594. {
  595. .compat = "fsl,t2080-clockgen",
  596. .guts_compat = "fsl,t2080-device-config",
  597. .init_periph = t2080_init_periph,
  598. .cmux_groups = {
  599. &clockgen2_cmux_cga12
  600. },
  601. .hwaccel = {
  602. &t2080_hwa1, &t2080_hwa2
  603. },
  604. .cmux_to_group = {
  605. 0, -1
  606. },
  607. .pll_mask = 0x07,
  608. .flags = CG_PLL_8BIT,
  609. },
  610. {
  611. .compat = "fsl,t4240-clockgen",
  612. .guts_compat = "fsl,t4240-device-config",
  613. .init_periph = t4240_init_periph,
  614. .cmux_groups = {
  615. &clockgen2_cmux_cga, &clockgen2_cmux_cgb
  616. },
  617. .hwaccel = {
  618. &t4240_hwa1, NULL, NULL, &t4240_hwa4, &t4240_hwa5
  619. },
  620. .cmux_to_group = {
  621. 0, 0, 1, -1
  622. },
  623. .pll_mask = 0x3f,
  624. .flags = CG_PLL_8BIT,
  625. },
  626. {},
  627. };
  628. struct mux_hwclock {
  629. struct clk_hw hw;
  630. struct clockgen *cg;
  631. const struct clockgen_muxinfo *info;
  632. u32 __iomem *reg;
  633. u8 parent_to_clksel[NUM_MUX_PARENTS];
  634. s8 clksel_to_parent[NUM_MUX_PARENTS];
  635. int num_parents;
  636. };
  637. #define to_mux_hwclock(p) container_of(p, struct mux_hwclock, hw)
  638. #define CLKSEL_MASK 0x78000000
  639. #define CLKSEL_SHIFT 27
  640. static int mux_set_parent(struct clk_hw *hw, u8 idx)
  641. {
  642. struct mux_hwclock *hwc = to_mux_hwclock(hw);
  643. u32 clksel;
  644. if (idx >= hwc->num_parents)
  645. return -EINVAL;
  646. clksel = hwc->parent_to_clksel[idx];
  647. cg_out(hwc->cg, (clksel << CLKSEL_SHIFT) & CLKSEL_MASK, hwc->reg);
  648. return 0;
  649. }
  650. static u8 mux_get_parent(struct clk_hw *hw)
  651. {
  652. struct mux_hwclock *hwc = to_mux_hwclock(hw);
  653. u32 clksel;
  654. s8 ret;
  655. clksel = (cg_in(hwc->cg, hwc->reg) & CLKSEL_MASK) >> CLKSEL_SHIFT;
  656. ret = hwc->clksel_to_parent[clksel];
  657. if (ret < 0) {
  658. pr_err("%s: mux at %p has bad clksel\n", __func__, hwc->reg);
  659. return 0;
  660. }
  661. return ret;
  662. }
  663. static const struct clk_ops cmux_ops = {
  664. .get_parent = mux_get_parent,
  665. .set_parent = mux_set_parent,
  666. };
  667. /*
  668. * Don't allow setting for now, as the clock options haven't been
  669. * sanitized for additional restrictions.
  670. */
  671. static const struct clk_ops hwaccel_ops = {
  672. .get_parent = mux_get_parent,
  673. };
  674. static const struct clockgen_pll_div *get_pll_div(struct clockgen *cg,
  675. struct mux_hwclock *hwc,
  676. int idx)
  677. {
  678. int pll, div;
  679. if (!(hwc->info->clksel[idx].flags & CLKSEL_VALID))
  680. return NULL;
  681. pll = hwc->info->clksel[idx].pll;
  682. div = hwc->info->clksel[idx].div;
  683. return &cg->pll[pll].div[div];
  684. }
  685. static struct clk * __init create_mux_common(struct clockgen *cg,
  686. struct mux_hwclock *hwc,
  687. const struct clk_ops *ops,
  688. unsigned long min_rate,
  689. unsigned long max_rate,
  690. unsigned long pct80_rate,
  691. const char *fmt, int idx)
  692. {
  693. struct clk_init_data init = {};
  694. struct clk *clk;
  695. const struct clockgen_pll_div *div;
  696. const char *parent_names[NUM_MUX_PARENTS];
  697. char name[32];
  698. int i, j;
  699. snprintf(name, sizeof(name), fmt, idx);
  700. for (i = 0, j = 0; i < NUM_MUX_PARENTS; i++) {
  701. unsigned long rate;
  702. hwc->clksel_to_parent[i] = -1;
  703. div = get_pll_div(cg, hwc, i);
  704. if (!div)
  705. continue;
  706. rate = clk_get_rate(div->clk);
  707. if (hwc->info->clksel[i].flags & CLKSEL_80PCT &&
  708. rate > pct80_rate)
  709. continue;
  710. if (rate < min_rate)
  711. continue;
  712. if (rate > max_rate)
  713. continue;
  714. parent_names[j] = div->name;
  715. hwc->parent_to_clksel[j] = i;
  716. hwc->clksel_to_parent[i] = j;
  717. j++;
  718. }
  719. init.name = name;
  720. init.ops = ops;
  721. init.parent_names = parent_names;
  722. init.num_parents = hwc->num_parents = j;
  723. init.flags = 0;
  724. hwc->hw.init = &init;
  725. hwc->cg = cg;
  726. clk = clk_register(NULL, &hwc->hw);
  727. if (IS_ERR(clk)) {
  728. pr_err("%s: Couldn't register %s: %ld\n", __func__, name,
  729. PTR_ERR(clk));
  730. kfree(hwc);
  731. return NULL;
  732. }
  733. return clk;
  734. }
  735. static struct clk * __init create_one_cmux(struct clockgen *cg, int idx)
  736. {
  737. struct mux_hwclock *hwc;
  738. const struct clockgen_pll_div *div;
  739. unsigned long plat_rate, min_rate;
  740. u64 max_rate, pct80_rate;
  741. u32 clksel;
  742. hwc = kzalloc(sizeof(*hwc), GFP_KERNEL);
  743. if (!hwc)
  744. return NULL;
  745. if (cg->info.flags & CG_VER3)
  746. hwc->reg = cg->regs + 0x70000 + 0x20 * idx;
  747. else
  748. hwc->reg = cg->regs + 0x20 * idx;
  749. hwc->info = cg->info.cmux_groups[cg->info.cmux_to_group[idx]];
  750. /*
  751. * Find the rate for the default clksel, and treat it as the
  752. * maximum rated core frequency. If this is an incorrect
  753. * assumption, certain clock options (possibly including the
  754. * default clksel) may be inappropriately excluded on certain
  755. * chips.
  756. */
  757. clksel = (cg_in(cg, hwc->reg) & CLKSEL_MASK) >> CLKSEL_SHIFT;
  758. div = get_pll_div(cg, hwc, clksel);
  759. if (!div) {
  760. kfree(hwc);
  761. return NULL;
  762. }
  763. max_rate = clk_get_rate(div->clk);
  764. pct80_rate = max_rate * 8;
  765. do_div(pct80_rate, 10);
  766. plat_rate = clk_get_rate(cg->pll[PLATFORM_PLL].div[PLL_DIV1].clk);
  767. if (cg->info.flags & CG_CMUX_GE_PLAT)
  768. min_rate = plat_rate;
  769. else
  770. min_rate = plat_rate / 2;
  771. return create_mux_common(cg, hwc, &cmux_ops, min_rate, max_rate,
  772. pct80_rate, "cg-cmux%d", idx);
  773. }
  774. static struct clk * __init create_one_hwaccel(struct clockgen *cg, int idx)
  775. {
  776. struct mux_hwclock *hwc;
  777. hwc = kzalloc(sizeof(*hwc), GFP_KERNEL);
  778. if (!hwc)
  779. return NULL;
  780. hwc->reg = cg->regs + 0x20 * idx + 0x10;
  781. hwc->info = cg->info.hwaccel[idx];
  782. return create_mux_common(cg, hwc, &hwaccel_ops, 0, ULONG_MAX, 0,
  783. "cg-hwaccel%d", idx);
  784. }
  785. static void __init create_muxes(struct clockgen *cg)
  786. {
  787. int i;
  788. for (i = 0; i < ARRAY_SIZE(cg->cmux); i++) {
  789. if (cg->info.cmux_to_group[i] < 0)
  790. break;
  791. if (cg->info.cmux_to_group[i] >=
  792. ARRAY_SIZE(cg->info.cmux_groups)) {
  793. WARN_ON_ONCE(1);
  794. continue;
  795. }
  796. cg->cmux[i] = create_one_cmux(cg, i);
  797. }
  798. for (i = 0; i < ARRAY_SIZE(cg->hwaccel); i++) {
  799. if (!cg->info.hwaccel[i])
  800. continue;
  801. cg->hwaccel[i] = create_one_hwaccel(cg, i);
  802. }
  803. }
  804. static void __init clockgen_init(struct device_node *np);
  805. /*
  806. * Legacy nodes may get probed before the parent clockgen node.
  807. * It is assumed that device trees with legacy nodes will not
  808. * contain a "clocks" property -- otherwise the input clocks may
  809. * not be initialized at this point.
  810. */
  811. static void __init legacy_init_clockgen(struct device_node *np)
  812. {
  813. if (!clockgen.node)
  814. clockgen_init(of_get_parent(np));
  815. }
  816. /* Legacy node */
  817. static void __init core_mux_init(struct device_node *np)
  818. {
  819. struct clk *clk;
  820. struct resource res;
  821. int idx, rc;
  822. legacy_init_clockgen(np);
  823. if (of_address_to_resource(np, 0, &res))
  824. return;
  825. idx = (res.start & 0xf0) >> 5;
  826. clk = clockgen.cmux[idx];
  827. rc = of_clk_add_provider(np, of_clk_src_simple_get, clk);
  828. if (rc) {
  829. pr_err("%s: Couldn't register clk provider for node %s: %d\n",
  830. __func__, np->name, rc);
  831. return;
  832. }
  833. }
  834. static struct clk __init
  835. *sysclk_from_fixed(struct device_node *node, const char *name)
  836. {
  837. u32 rate;
  838. if (of_property_read_u32(node, "clock-frequency", &rate))
  839. return ERR_PTR(-ENODEV);
  840. return clk_register_fixed_rate(NULL, name, NULL, 0, rate);
  841. }
  842. static struct clk __init *input_clock(const char *name, struct clk *clk)
  843. {
  844. const char *input_name;
  845. /* Register the input clock under the desired name. */
  846. input_name = __clk_get_name(clk);
  847. clk = clk_register_fixed_factor(NULL, name, input_name,
  848. 0, 1, 1);
  849. if (IS_ERR(clk))
  850. pr_err("%s: Couldn't register %s: %ld\n", __func__, name,
  851. PTR_ERR(clk));
  852. return clk;
  853. }
  854. static struct clk __init *input_clock_by_name(const char *name,
  855. const char *dtname)
  856. {
  857. struct clk *clk;
  858. clk = of_clk_get_by_name(clockgen.node, dtname);
  859. if (IS_ERR(clk))
  860. return clk;
  861. return input_clock(name, clk);
  862. }
  863. static struct clk __init *input_clock_by_index(const char *name, int idx)
  864. {
  865. struct clk *clk;
  866. clk = of_clk_get(clockgen.node, 0);
  867. if (IS_ERR(clk))
  868. return clk;
  869. return input_clock(name, clk);
  870. }
  871. static struct clk * __init create_sysclk(const char *name)
  872. {
  873. struct device_node *sysclk;
  874. struct clk *clk;
  875. clk = sysclk_from_fixed(clockgen.node, name);
  876. if (!IS_ERR(clk))
  877. return clk;
  878. clk = input_clock_by_name(name, "sysclk");
  879. if (!IS_ERR(clk))
  880. return clk;
  881. clk = input_clock_by_index(name, 0);
  882. if (!IS_ERR(clk))
  883. return clk;
  884. sysclk = of_get_child_by_name(clockgen.node, "sysclk");
  885. if (sysclk) {
  886. clk = sysclk_from_fixed(sysclk, name);
  887. if (!IS_ERR(clk))
  888. return clk;
  889. }
  890. pr_err("%s: No input sysclk\n", __func__);
  891. return NULL;
  892. }
  893. static struct clk * __init create_coreclk(const char *name)
  894. {
  895. struct clk *clk;
  896. clk = input_clock_by_name(name, "coreclk");
  897. if (!IS_ERR(clk))
  898. return clk;
  899. /*
  900. * This indicates a mix of legacy nodes with the new coreclk
  901. * mechanism, which should never happen. If this error occurs,
  902. * don't use the wrong input clock just because coreclk isn't
  903. * ready yet.
  904. */
  905. if (WARN_ON(PTR_ERR(clk) == -EPROBE_DEFER))
  906. return clk;
  907. return NULL;
  908. }
  909. /* Legacy node */
  910. static void __init sysclk_init(struct device_node *node)
  911. {
  912. struct clk *clk;
  913. legacy_init_clockgen(node);
  914. clk = clockgen.sysclk;
  915. if (clk)
  916. of_clk_add_provider(node, of_clk_src_simple_get, clk);
  917. }
  918. #define PLL_KILL BIT(31)
  919. static void __init create_one_pll(struct clockgen *cg, int idx)
  920. {
  921. u32 __iomem *reg;
  922. u32 mult;
  923. struct clockgen_pll *pll = &cg->pll[idx];
  924. const char *input = "cg-sysclk";
  925. int i;
  926. if (!(cg->info.pll_mask & (1 << idx)))
  927. return;
  928. if (cg->coreclk && idx != PLATFORM_PLL) {
  929. if (IS_ERR(cg->coreclk))
  930. return;
  931. input = "cg-coreclk";
  932. }
  933. if (cg->info.flags & CG_VER3) {
  934. switch (idx) {
  935. case PLATFORM_PLL:
  936. reg = cg->regs + 0x60080;
  937. break;
  938. case CGA_PLL1:
  939. reg = cg->regs + 0x80;
  940. break;
  941. case CGA_PLL2:
  942. reg = cg->regs + 0xa0;
  943. break;
  944. case CGB_PLL1:
  945. reg = cg->regs + 0x10080;
  946. break;
  947. case CGB_PLL2:
  948. reg = cg->regs + 0x100a0;
  949. break;
  950. default:
  951. WARN_ONCE(1, "index %d\n", idx);
  952. return;
  953. }
  954. } else {
  955. if (idx == PLATFORM_PLL)
  956. reg = cg->regs + 0xc00;
  957. else
  958. reg = cg->regs + 0x800 + 0x20 * (idx - 1);
  959. }
  960. /* Get the multiple of PLL */
  961. mult = cg_in(cg, reg);
  962. /* Check if this PLL is disabled */
  963. if (mult & PLL_KILL) {
  964. pr_debug("%s(): pll %p disabled\n", __func__, reg);
  965. return;
  966. }
  967. if ((cg->info.flags & CG_VER3) ||
  968. ((cg->info.flags & CG_PLL_8BIT) && idx != PLATFORM_PLL))
  969. mult = (mult & GENMASK(8, 1)) >> 1;
  970. else
  971. mult = (mult & GENMASK(6, 1)) >> 1;
  972. for (i = 0; i < ARRAY_SIZE(pll->div); i++) {
  973. struct clk *clk;
  974. int ret;
  975. /*
  976. * For platform PLL, there are 8 divider clocks.
  977. * For core PLL, there are 4 divider clocks at most.
  978. */
  979. if (idx != PLATFORM_PLL && i >= 4)
  980. break;
  981. snprintf(pll->div[i].name, sizeof(pll->div[i].name),
  982. "cg-pll%d-div%d", idx, i + 1);
  983. clk = clk_register_fixed_factor(NULL,
  984. pll->div[i].name, input, 0, mult, i + 1);
  985. if (IS_ERR(clk)) {
  986. pr_err("%s: %s: register failed %ld\n",
  987. __func__, pll->div[i].name, PTR_ERR(clk));
  988. continue;
  989. }
  990. pll->div[i].clk = clk;
  991. ret = clk_register_clkdev(clk, pll->div[i].name, NULL);
  992. if (ret != 0)
  993. pr_err("%s: %s: register to lookup table failed %ld\n",
  994. __func__, pll->div[i].name, PTR_ERR(clk));
  995. }
  996. }
  997. static void __init create_plls(struct clockgen *cg)
  998. {
  999. int i;
  1000. for (i = 0; i < ARRAY_SIZE(cg->pll); i++)
  1001. create_one_pll(cg, i);
  1002. }
  1003. static void __init legacy_pll_init(struct device_node *np, int idx)
  1004. {
  1005. struct clockgen_pll *pll;
  1006. struct clk_onecell_data *onecell_data;
  1007. struct clk **subclks;
  1008. int count, rc;
  1009. legacy_init_clockgen(np);
  1010. pll = &clockgen.pll[idx];
  1011. count = of_property_count_strings(np, "clock-output-names");
  1012. BUILD_BUG_ON(ARRAY_SIZE(pll->div) < 4);
  1013. subclks = kcalloc(4, sizeof(struct clk *), GFP_KERNEL);
  1014. if (!subclks)
  1015. return;
  1016. onecell_data = kmalloc(sizeof(*onecell_data), GFP_KERNEL);
  1017. if (!onecell_data)
  1018. goto err_clks;
  1019. if (count <= 3) {
  1020. subclks[0] = pll->div[0].clk;
  1021. subclks[1] = pll->div[1].clk;
  1022. subclks[2] = pll->div[3].clk;
  1023. } else {
  1024. subclks[0] = pll->div[0].clk;
  1025. subclks[1] = pll->div[1].clk;
  1026. subclks[2] = pll->div[2].clk;
  1027. subclks[3] = pll->div[3].clk;
  1028. }
  1029. onecell_data->clks = subclks;
  1030. onecell_data->clk_num = count;
  1031. rc = of_clk_add_provider(np, of_clk_src_onecell_get, onecell_data);
  1032. if (rc) {
  1033. pr_err("%s: Couldn't register clk provider for node %s: %d\n",
  1034. __func__, np->name, rc);
  1035. goto err_cell;
  1036. }
  1037. return;
  1038. err_cell:
  1039. kfree(onecell_data);
  1040. err_clks:
  1041. kfree(subclks);
  1042. }
  1043. /* Legacy node */
  1044. static void __init pltfrm_pll_init(struct device_node *np)
  1045. {
  1046. legacy_pll_init(np, PLATFORM_PLL);
  1047. }
  1048. /* Legacy node */
  1049. static void __init core_pll_init(struct device_node *np)
  1050. {
  1051. struct resource res;
  1052. int idx;
  1053. if (of_address_to_resource(np, 0, &res))
  1054. return;
  1055. if ((res.start & 0xfff) == 0xc00) {
  1056. /*
  1057. * ls1021a devtree labels the platform PLL
  1058. * with the core PLL compatible
  1059. */
  1060. pltfrm_pll_init(np);
  1061. } else {
  1062. idx = (res.start & 0xf0) >> 5;
  1063. legacy_pll_init(np, CGA_PLL1 + idx);
  1064. }
  1065. }
  1066. static struct clk *clockgen_clk_get(struct of_phandle_args *clkspec, void *data)
  1067. {
  1068. struct clockgen *cg = data;
  1069. struct clk *clk;
  1070. struct clockgen_pll *pll;
  1071. u32 type, idx;
  1072. if (clkspec->args_count < 2) {
  1073. pr_err("%s: insufficient phandle args\n", __func__);
  1074. return ERR_PTR(-EINVAL);
  1075. }
  1076. type = clkspec->args[0];
  1077. idx = clkspec->args[1];
  1078. switch (type) {
  1079. case 0:
  1080. if (idx != 0)
  1081. goto bad_args;
  1082. clk = cg->sysclk;
  1083. break;
  1084. case 1:
  1085. if (idx >= ARRAY_SIZE(cg->cmux))
  1086. goto bad_args;
  1087. clk = cg->cmux[idx];
  1088. break;
  1089. case 2:
  1090. if (idx >= ARRAY_SIZE(cg->hwaccel))
  1091. goto bad_args;
  1092. clk = cg->hwaccel[idx];
  1093. break;
  1094. case 3:
  1095. if (idx >= ARRAY_SIZE(cg->fman))
  1096. goto bad_args;
  1097. clk = cg->fman[idx];
  1098. break;
  1099. case 4:
  1100. pll = &cg->pll[PLATFORM_PLL];
  1101. if (idx >= ARRAY_SIZE(pll->div))
  1102. goto bad_args;
  1103. clk = pll->div[idx].clk;
  1104. break;
  1105. case 5:
  1106. if (idx != 0)
  1107. goto bad_args;
  1108. clk = cg->coreclk;
  1109. if (IS_ERR(clk))
  1110. clk = NULL;
  1111. break;
  1112. default:
  1113. goto bad_args;
  1114. }
  1115. if (!clk)
  1116. return ERR_PTR(-ENOENT);
  1117. return clk;
  1118. bad_args:
  1119. pr_err("%s: Bad phandle args %u %u\n", __func__, type, idx);
  1120. return ERR_PTR(-EINVAL);
  1121. }
  1122. #ifdef CONFIG_PPC
  1123. #include <asm/mpc85xx.h>
  1124. static const u32 a4510_svrs[] __initconst = {
  1125. (SVR_P2040 << 8) | 0x10, /* P2040 1.0 */
  1126. (SVR_P2040 << 8) | 0x11, /* P2040 1.1 */
  1127. (SVR_P2041 << 8) | 0x10, /* P2041 1.0 */
  1128. (SVR_P2041 << 8) | 0x11, /* P2041 1.1 */
  1129. (SVR_P3041 << 8) | 0x10, /* P3041 1.0 */
  1130. (SVR_P3041 << 8) | 0x11, /* P3041 1.1 */
  1131. (SVR_P4040 << 8) | 0x20, /* P4040 2.0 */
  1132. (SVR_P4080 << 8) | 0x20, /* P4080 2.0 */
  1133. (SVR_P5010 << 8) | 0x10, /* P5010 1.0 */
  1134. (SVR_P5010 << 8) | 0x20, /* P5010 2.0 */
  1135. (SVR_P5020 << 8) | 0x10, /* P5020 1.0 */
  1136. (SVR_P5021 << 8) | 0x10, /* P5021 1.0 */
  1137. (SVR_P5040 << 8) | 0x10, /* P5040 1.0 */
  1138. };
  1139. #define SVR_SECURITY 0x80000 /* The Security (E) bit */
  1140. static bool __init has_erratum_a4510(void)
  1141. {
  1142. u32 svr = mfspr(SPRN_SVR);
  1143. int i;
  1144. svr &= ~SVR_SECURITY;
  1145. for (i = 0; i < ARRAY_SIZE(a4510_svrs); i++) {
  1146. if (svr == a4510_svrs[i])
  1147. return true;
  1148. }
  1149. return false;
  1150. }
  1151. #else
  1152. static bool __init has_erratum_a4510(void)
  1153. {
  1154. return false;
  1155. }
  1156. #endif
  1157. static void __init clockgen_init(struct device_node *np)
  1158. {
  1159. int i, ret;
  1160. bool is_old_ls1021a = false;
  1161. /* May have already been called by a legacy probe */
  1162. if (clockgen.node)
  1163. return;
  1164. clockgen.node = np;
  1165. clockgen.regs = of_iomap(np, 0);
  1166. if (!clockgen.regs &&
  1167. of_device_is_compatible(of_root, "fsl,ls1021a")) {
  1168. /* Compatibility hack for old, broken device trees */
  1169. clockgen.regs = ioremap(0x1ee1000, 0x1000);
  1170. is_old_ls1021a = true;
  1171. }
  1172. if (!clockgen.regs) {
  1173. pr_err("%s(): %s: of_iomap() failed\n", __func__, np->name);
  1174. return;
  1175. }
  1176. for (i = 0; i < ARRAY_SIZE(chipinfo); i++) {
  1177. if (of_device_is_compatible(np, chipinfo[i].compat))
  1178. break;
  1179. if (is_old_ls1021a &&
  1180. !strcmp(chipinfo[i].compat, "fsl,ls1021a-clockgen"))
  1181. break;
  1182. }
  1183. if (i == ARRAY_SIZE(chipinfo)) {
  1184. pr_err("%s: unknown clockgen node %pOF\n", __func__, np);
  1185. goto err;
  1186. }
  1187. clockgen.info = chipinfo[i];
  1188. if (clockgen.info.guts_compat) {
  1189. struct device_node *guts;
  1190. guts = of_find_compatible_node(NULL, NULL,
  1191. clockgen.info.guts_compat);
  1192. if (guts) {
  1193. clockgen.guts = of_iomap(guts, 0);
  1194. if (!clockgen.guts) {
  1195. pr_err("%s: Couldn't map %pOF regs\n", __func__,
  1196. guts);
  1197. }
  1198. of_node_put(guts);
  1199. }
  1200. }
  1201. if (has_erratum_a4510())
  1202. clockgen.info.flags |= CG_CMUX_GE_PLAT;
  1203. clockgen.sysclk = create_sysclk("cg-sysclk");
  1204. clockgen.coreclk = create_coreclk("cg-coreclk");
  1205. create_plls(&clockgen);
  1206. create_muxes(&clockgen);
  1207. if (clockgen.info.init_periph)
  1208. clockgen.info.init_periph(&clockgen);
  1209. ret = of_clk_add_provider(np, clockgen_clk_get, &clockgen);
  1210. if (ret) {
  1211. pr_err("%s: Couldn't register clk provider for node %s: %d\n",
  1212. __func__, np->name, ret);
  1213. }
  1214. return;
  1215. err:
  1216. iounmap(clockgen.regs);
  1217. clockgen.regs = NULL;
  1218. }
  1219. CLK_OF_DECLARE(qoriq_clockgen_1, "fsl,qoriq-clockgen-1.0", clockgen_init);
  1220. CLK_OF_DECLARE(qoriq_clockgen_2, "fsl,qoriq-clockgen-2.0", clockgen_init);
  1221. CLK_OF_DECLARE(qoriq_clockgen_ls1012a, "fsl,ls1012a-clockgen", clockgen_init);
  1222. CLK_OF_DECLARE(qoriq_clockgen_ls1021a, "fsl,ls1021a-clockgen", clockgen_init);
  1223. CLK_OF_DECLARE(qoriq_clockgen_ls1043a, "fsl,ls1043a-clockgen", clockgen_init);
  1224. CLK_OF_DECLARE(qoriq_clockgen_ls1046a, "fsl,ls1046a-clockgen", clockgen_init);
  1225. CLK_OF_DECLARE(qoriq_clockgen_ls1088a, "fsl,ls1088a-clockgen", clockgen_init);
  1226. CLK_OF_DECLARE(qoriq_clockgen_ls2080a, "fsl,ls2080a-clockgen", clockgen_init);
  1227. /* Legacy nodes */
  1228. CLK_OF_DECLARE(qoriq_sysclk_1, "fsl,qoriq-sysclk-1.0", sysclk_init);
  1229. CLK_OF_DECLARE(qoriq_sysclk_2, "fsl,qoriq-sysclk-2.0", sysclk_init);
  1230. CLK_OF_DECLARE(qoriq_core_pll_1, "fsl,qoriq-core-pll-1.0", core_pll_init);
  1231. CLK_OF_DECLARE(qoriq_core_pll_2, "fsl,qoriq-core-pll-2.0", core_pll_init);
  1232. CLK_OF_DECLARE(qoriq_core_mux_1, "fsl,qoriq-core-mux-1.0", core_mux_init);
  1233. CLK_OF_DECLARE(qoriq_core_mux_2, "fsl,qoriq-core-mux-2.0", core_mux_init);
  1234. CLK_OF_DECLARE(qoriq_pltfrm_pll_1, "fsl,qoriq-platform-pll-1.0", pltfrm_pll_init);
  1235. CLK_OF_DECLARE(qoriq_pltfrm_pll_2, "fsl,qoriq-platform-pll-2.0", pltfrm_pll_init);