clk-qoriq.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436
  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. &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 %pOFn: %d\n",
  830. __func__, np, 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 %pOFn: %d\n",
  1034. __func__, np, 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(): %pOFn: of_iomap() failed\n", __func__, np);
  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. }
  1199. }
  1200. if (has_erratum_a4510())
  1201. clockgen.info.flags |= CG_CMUX_GE_PLAT;
  1202. clockgen.sysclk = create_sysclk("cg-sysclk");
  1203. clockgen.coreclk = create_coreclk("cg-coreclk");
  1204. create_plls(&clockgen);
  1205. create_muxes(&clockgen);
  1206. if (clockgen.info.init_periph)
  1207. clockgen.info.init_periph(&clockgen);
  1208. ret = of_clk_add_provider(np, clockgen_clk_get, &clockgen);
  1209. if (ret) {
  1210. pr_err("%s: Couldn't register clk provider for node %pOFn: %d\n",
  1211. __func__, np, ret);
  1212. }
  1213. return;
  1214. err:
  1215. iounmap(clockgen.regs);
  1216. clockgen.regs = NULL;
  1217. }
  1218. CLK_OF_DECLARE(qoriq_clockgen_1, "fsl,qoriq-clockgen-1.0", clockgen_init);
  1219. CLK_OF_DECLARE(qoriq_clockgen_2, "fsl,qoriq-clockgen-2.0", clockgen_init);
  1220. CLK_OF_DECLARE(qoriq_clockgen_ls1012a, "fsl,ls1012a-clockgen", clockgen_init);
  1221. CLK_OF_DECLARE(qoriq_clockgen_ls1021a, "fsl,ls1021a-clockgen", clockgen_init);
  1222. CLK_OF_DECLARE(qoriq_clockgen_ls1043a, "fsl,ls1043a-clockgen", clockgen_init);
  1223. CLK_OF_DECLARE(qoriq_clockgen_ls1046a, "fsl,ls1046a-clockgen", clockgen_init);
  1224. CLK_OF_DECLARE(qoriq_clockgen_ls1088a, "fsl,ls1088a-clockgen", clockgen_init);
  1225. CLK_OF_DECLARE(qoriq_clockgen_ls2080a, "fsl,ls2080a-clockgen", clockgen_init);
  1226. /* Legacy nodes */
  1227. CLK_OF_DECLARE(qoriq_sysclk_1, "fsl,qoriq-sysclk-1.0", sysclk_init);
  1228. CLK_OF_DECLARE(qoriq_sysclk_2, "fsl,qoriq-sysclk-2.0", sysclk_init);
  1229. CLK_OF_DECLARE(qoriq_core_pll_1, "fsl,qoriq-core-pll-1.0", core_pll_init);
  1230. CLK_OF_DECLARE(qoriq_core_pll_2, "fsl,qoriq-core-pll-2.0", core_pll_init);
  1231. CLK_OF_DECLARE(qoriq_core_mux_1, "fsl,qoriq-core-mux-1.0", core_mux_init);
  1232. CLK_OF_DECLARE(qoriq_core_mux_2, "fsl,qoriq-core-mux-2.0", core_mux_init);
  1233. CLK_OF_DECLARE(qoriq_pltfrm_pll_1, "fsl,qoriq-platform-pll-1.0", pltfrm_pll_init);
  1234. CLK_OF_DECLARE(qoriq_pltfrm_pll_2, "fsl,qoriq-platform-pll-2.0", pltfrm_pll_init);