clk-qoriq.c 38 KB

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