clk-qoriq.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429
  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[4];
  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. &p2041_cmux_grp1, &p2041_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. snprintf(pll->div[i].name, sizeof(pll->div[i].name),
  976. "cg-pll%d-div%d", idx, i + 1);
  977. clk = clk_register_fixed_factor(NULL,
  978. pll->div[i].name, input, 0, mult, i + 1);
  979. if (IS_ERR(clk)) {
  980. pr_err("%s: %s: register failed %ld\n",
  981. __func__, pll->div[i].name, PTR_ERR(clk));
  982. continue;
  983. }
  984. pll->div[i].clk = clk;
  985. ret = clk_register_clkdev(clk, pll->div[i].name, NULL);
  986. if (ret != 0)
  987. pr_err("%s: %s: register to lookup table failed %ld\n",
  988. __func__, pll->div[i].name, PTR_ERR(clk));
  989. }
  990. }
  991. static void __init create_plls(struct clockgen *cg)
  992. {
  993. int i;
  994. for (i = 0; i < ARRAY_SIZE(cg->pll); i++)
  995. create_one_pll(cg, i);
  996. }
  997. static void __init legacy_pll_init(struct device_node *np, int idx)
  998. {
  999. struct clockgen_pll *pll;
  1000. struct clk_onecell_data *onecell_data;
  1001. struct clk **subclks;
  1002. int count, rc;
  1003. legacy_init_clockgen(np);
  1004. pll = &clockgen.pll[idx];
  1005. count = of_property_count_strings(np, "clock-output-names");
  1006. BUILD_BUG_ON(ARRAY_SIZE(pll->div) < 4);
  1007. subclks = kcalloc(4, sizeof(struct clk *), GFP_KERNEL);
  1008. if (!subclks)
  1009. return;
  1010. onecell_data = kmalloc(sizeof(*onecell_data), GFP_KERNEL);
  1011. if (!onecell_data)
  1012. goto err_clks;
  1013. if (count <= 3) {
  1014. subclks[0] = pll->div[0].clk;
  1015. subclks[1] = pll->div[1].clk;
  1016. subclks[2] = pll->div[3].clk;
  1017. } else {
  1018. subclks[0] = pll->div[0].clk;
  1019. subclks[1] = pll->div[1].clk;
  1020. subclks[2] = pll->div[2].clk;
  1021. subclks[3] = pll->div[3].clk;
  1022. }
  1023. onecell_data->clks = subclks;
  1024. onecell_data->clk_num = count;
  1025. rc = of_clk_add_provider(np, of_clk_src_onecell_get, onecell_data);
  1026. if (rc) {
  1027. pr_err("%s: Couldn't register clk provider for node %s: %d\n",
  1028. __func__, np->name, rc);
  1029. goto err_cell;
  1030. }
  1031. return;
  1032. err_cell:
  1033. kfree(onecell_data);
  1034. err_clks:
  1035. kfree(subclks);
  1036. }
  1037. /* Legacy node */
  1038. static void __init pltfrm_pll_init(struct device_node *np)
  1039. {
  1040. legacy_pll_init(np, PLATFORM_PLL);
  1041. }
  1042. /* Legacy node */
  1043. static void __init core_pll_init(struct device_node *np)
  1044. {
  1045. struct resource res;
  1046. int idx;
  1047. if (of_address_to_resource(np, 0, &res))
  1048. return;
  1049. if ((res.start & 0xfff) == 0xc00) {
  1050. /*
  1051. * ls1021a devtree labels the platform PLL
  1052. * with the core PLL compatible
  1053. */
  1054. pltfrm_pll_init(np);
  1055. } else {
  1056. idx = (res.start & 0xf0) >> 5;
  1057. legacy_pll_init(np, CGA_PLL1 + idx);
  1058. }
  1059. }
  1060. static struct clk *clockgen_clk_get(struct of_phandle_args *clkspec, void *data)
  1061. {
  1062. struct clockgen *cg = data;
  1063. struct clk *clk;
  1064. struct clockgen_pll *pll;
  1065. u32 type, idx;
  1066. if (clkspec->args_count < 2) {
  1067. pr_err("%s: insufficient phandle args\n", __func__);
  1068. return ERR_PTR(-EINVAL);
  1069. }
  1070. type = clkspec->args[0];
  1071. idx = clkspec->args[1];
  1072. switch (type) {
  1073. case 0:
  1074. if (idx != 0)
  1075. goto bad_args;
  1076. clk = cg->sysclk;
  1077. break;
  1078. case 1:
  1079. if (idx >= ARRAY_SIZE(cg->cmux))
  1080. goto bad_args;
  1081. clk = cg->cmux[idx];
  1082. break;
  1083. case 2:
  1084. if (idx >= ARRAY_SIZE(cg->hwaccel))
  1085. goto bad_args;
  1086. clk = cg->hwaccel[idx];
  1087. break;
  1088. case 3:
  1089. if (idx >= ARRAY_SIZE(cg->fman))
  1090. goto bad_args;
  1091. clk = cg->fman[idx];
  1092. break;
  1093. case 4:
  1094. pll = &cg->pll[PLATFORM_PLL];
  1095. if (idx >= ARRAY_SIZE(pll->div))
  1096. goto bad_args;
  1097. clk = pll->div[idx].clk;
  1098. break;
  1099. case 5:
  1100. if (idx != 0)
  1101. goto bad_args;
  1102. clk = cg->coreclk;
  1103. if (IS_ERR(clk))
  1104. clk = NULL;
  1105. break;
  1106. default:
  1107. goto bad_args;
  1108. }
  1109. if (!clk)
  1110. return ERR_PTR(-ENOENT);
  1111. return clk;
  1112. bad_args:
  1113. pr_err("%s: Bad phandle args %u %u\n", __func__, type, idx);
  1114. return ERR_PTR(-EINVAL);
  1115. }
  1116. #ifdef CONFIG_PPC
  1117. #include <asm/mpc85xx.h>
  1118. static const u32 a4510_svrs[] __initconst = {
  1119. (SVR_P2040 << 8) | 0x10, /* P2040 1.0 */
  1120. (SVR_P2040 << 8) | 0x11, /* P2040 1.1 */
  1121. (SVR_P2041 << 8) | 0x10, /* P2041 1.0 */
  1122. (SVR_P2041 << 8) | 0x11, /* P2041 1.1 */
  1123. (SVR_P3041 << 8) | 0x10, /* P3041 1.0 */
  1124. (SVR_P3041 << 8) | 0x11, /* P3041 1.1 */
  1125. (SVR_P4040 << 8) | 0x20, /* P4040 2.0 */
  1126. (SVR_P4080 << 8) | 0x20, /* P4080 2.0 */
  1127. (SVR_P5010 << 8) | 0x10, /* P5010 1.0 */
  1128. (SVR_P5010 << 8) | 0x20, /* P5010 2.0 */
  1129. (SVR_P5020 << 8) | 0x10, /* P5020 1.0 */
  1130. (SVR_P5021 << 8) | 0x10, /* P5021 1.0 */
  1131. (SVR_P5040 << 8) | 0x10, /* P5040 1.0 */
  1132. };
  1133. #define SVR_SECURITY 0x80000 /* The Security (E) bit */
  1134. static bool __init has_erratum_a4510(void)
  1135. {
  1136. u32 svr = mfspr(SPRN_SVR);
  1137. int i;
  1138. svr &= ~SVR_SECURITY;
  1139. for (i = 0; i < ARRAY_SIZE(a4510_svrs); i++) {
  1140. if (svr == a4510_svrs[i])
  1141. return true;
  1142. }
  1143. return false;
  1144. }
  1145. #else
  1146. static bool __init has_erratum_a4510(void)
  1147. {
  1148. return false;
  1149. }
  1150. #endif
  1151. static void __init clockgen_init(struct device_node *np)
  1152. {
  1153. int i, ret;
  1154. bool is_old_ls1021a = false;
  1155. /* May have already been called by a legacy probe */
  1156. if (clockgen.node)
  1157. return;
  1158. clockgen.node = np;
  1159. clockgen.regs = of_iomap(np, 0);
  1160. if (!clockgen.regs &&
  1161. of_device_is_compatible(of_root, "fsl,ls1021a")) {
  1162. /* Compatibility hack for old, broken device trees */
  1163. clockgen.regs = ioremap(0x1ee1000, 0x1000);
  1164. is_old_ls1021a = true;
  1165. }
  1166. if (!clockgen.regs) {
  1167. pr_err("%s(): %s: of_iomap() failed\n", __func__, np->name);
  1168. return;
  1169. }
  1170. for (i = 0; i < ARRAY_SIZE(chipinfo); i++) {
  1171. if (of_device_is_compatible(np, chipinfo[i].compat))
  1172. break;
  1173. if (is_old_ls1021a &&
  1174. !strcmp(chipinfo[i].compat, "fsl,ls1021a-clockgen"))
  1175. break;
  1176. }
  1177. if (i == ARRAY_SIZE(chipinfo)) {
  1178. pr_err("%s: unknown clockgen node %pOF\n", __func__, np);
  1179. goto err;
  1180. }
  1181. clockgen.info = chipinfo[i];
  1182. if (clockgen.info.guts_compat) {
  1183. struct device_node *guts;
  1184. guts = of_find_compatible_node(NULL, NULL,
  1185. clockgen.info.guts_compat);
  1186. if (guts) {
  1187. clockgen.guts = of_iomap(guts, 0);
  1188. if (!clockgen.guts) {
  1189. pr_err("%s: Couldn't map %pOF regs\n", __func__,
  1190. guts);
  1191. }
  1192. }
  1193. }
  1194. if (has_erratum_a4510())
  1195. clockgen.info.flags |= CG_CMUX_GE_PLAT;
  1196. clockgen.sysclk = create_sysclk("cg-sysclk");
  1197. clockgen.coreclk = create_coreclk("cg-coreclk");
  1198. create_plls(&clockgen);
  1199. create_muxes(&clockgen);
  1200. if (clockgen.info.init_periph)
  1201. clockgen.info.init_periph(&clockgen);
  1202. ret = of_clk_add_provider(np, clockgen_clk_get, &clockgen);
  1203. if (ret) {
  1204. pr_err("%s: Couldn't register clk provider for node %s: %d\n",
  1205. __func__, np->name, ret);
  1206. }
  1207. return;
  1208. err:
  1209. iounmap(clockgen.regs);
  1210. clockgen.regs = NULL;
  1211. }
  1212. CLK_OF_DECLARE(qoriq_clockgen_1, "fsl,qoriq-clockgen-1.0", clockgen_init);
  1213. CLK_OF_DECLARE(qoriq_clockgen_2, "fsl,qoriq-clockgen-2.0", clockgen_init);
  1214. CLK_OF_DECLARE(qoriq_clockgen_ls1012a, "fsl,ls1012a-clockgen", clockgen_init);
  1215. CLK_OF_DECLARE(qoriq_clockgen_ls1021a, "fsl,ls1021a-clockgen", clockgen_init);
  1216. CLK_OF_DECLARE(qoriq_clockgen_ls1043a, "fsl,ls1043a-clockgen", clockgen_init);
  1217. CLK_OF_DECLARE(qoriq_clockgen_ls1046a, "fsl,ls1046a-clockgen", clockgen_init);
  1218. CLK_OF_DECLARE(qoriq_clockgen_ls1088a, "fsl,ls1088a-clockgen", clockgen_init);
  1219. CLK_OF_DECLARE(qoriq_clockgen_ls2080a, "fsl,ls2080a-clockgen", clockgen_init);
  1220. /* Legacy nodes */
  1221. CLK_OF_DECLARE(qoriq_sysclk_1, "fsl,qoriq-sysclk-1.0", sysclk_init);
  1222. CLK_OF_DECLARE(qoriq_sysclk_2, "fsl,qoriq-sysclk-2.0", sysclk_init);
  1223. CLK_OF_DECLARE(qoriq_core_pll_1, "fsl,qoriq-core-pll-1.0", core_pll_init);
  1224. CLK_OF_DECLARE(qoriq_core_pll_2, "fsl,qoriq-core-pll-2.0", core_pll_init);
  1225. CLK_OF_DECLARE(qoriq_core_mux_1, "fsl,qoriq-core-mux-1.0", core_mux_init);
  1226. CLK_OF_DECLARE(qoriq_core_mux_2, "fsl,qoriq-core-mux-2.0", core_mux_init);
  1227. CLK_OF_DECLARE(qoriq_pltfrm_pll_1, "fsl,qoriq-platform-pll-1.0", pltfrm_pll_init);
  1228. CLK_OF_DECLARE(qoriq_pltfrm_pll_2, "fsl,qoriq-platform-pll-2.0", pltfrm_pll_init);