clk-bcm2835.c 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2010,2015 Broadcom
  4. * Copyright (C) 2012 Stephen Warren
  5. */
  6. /**
  7. * DOC: BCM2835 CPRMAN (clock manager for the "audio" domain)
  8. *
  9. * The clock tree on the 2835 has several levels. There's a root
  10. * oscillator running at 19.2Mhz. After the oscillator there are 5
  11. * PLLs, roughly divided as "camera", "ARM", "core", "DSI displays",
  12. * and "HDMI displays". Those 5 PLLs each can divide their output to
  13. * produce up to 4 channels. Finally, there is the level of clocks to
  14. * be consumed by other hardware components (like "H264" or "HDMI
  15. * state machine"), which divide off of some subset of the PLL
  16. * channels.
  17. *
  18. * All of the clocks in the tree are exposed in the DT, because the DT
  19. * may want to make assignments of the final layer of clocks to the
  20. * PLL channels, and some components of the hardware will actually
  21. * skip layers of the tree (for example, the pixel clock comes
  22. * directly from the PLLH PIX channel without using a CM_*CTL clock
  23. * generator).
  24. */
  25. #include <linux/clk-provider.h>
  26. #include <linux/clkdev.h>
  27. #include <linux/clk.h>
  28. #include <linux/debugfs.h>
  29. #include <linux/delay.h>
  30. #include <linux/io.h>
  31. #include <linux/math.h>
  32. #include <linux/module.h>
  33. #include <linux/of.h>
  34. #include <linux/platform_device.h>
  35. #include <linux/slab.h>
  36. #include <dt-bindings/clock/bcm2835.h>
  37. #define CM_PASSWORD 0x5a000000
  38. #define CM_GNRICCTL 0x000
  39. #define CM_GNRICDIV 0x004
  40. # define CM_DIV_FRAC_BITS 12
  41. # define CM_DIV_FRAC_MASK GENMASK(CM_DIV_FRAC_BITS - 1, 0)
  42. #define CM_VPUCTL 0x008
  43. #define CM_VPUDIV 0x00c
  44. #define CM_SYSCTL 0x010
  45. #define CM_SYSDIV 0x014
  46. #define CM_PERIACTL 0x018
  47. #define CM_PERIADIV 0x01c
  48. #define CM_PERIICTL 0x020
  49. #define CM_PERIIDIV 0x024
  50. #define CM_H264CTL 0x028
  51. #define CM_H264DIV 0x02c
  52. #define CM_ISPCTL 0x030
  53. #define CM_ISPDIV 0x034
  54. #define CM_V3DCTL 0x038
  55. #define CM_V3DDIV 0x03c
  56. #define CM_CAM0CTL 0x040
  57. #define CM_CAM0DIV 0x044
  58. #define CM_CAM1CTL 0x048
  59. #define CM_CAM1DIV 0x04c
  60. #define CM_CCP2CTL 0x050
  61. #define CM_CCP2DIV 0x054
  62. #define CM_DSI0ECTL 0x058
  63. #define CM_DSI0EDIV 0x05c
  64. #define CM_DSI0PCTL 0x060
  65. #define CM_DSI0PDIV 0x064
  66. #define CM_DPICTL 0x068
  67. #define CM_DPIDIV 0x06c
  68. #define CM_GP0CTL 0x070
  69. #define CM_GP0DIV 0x074
  70. #define CM_GP1CTL 0x078
  71. #define CM_GP1DIV 0x07c
  72. #define CM_GP2CTL 0x080
  73. #define CM_GP2DIV 0x084
  74. #define CM_HSMCTL 0x088
  75. #define CM_HSMDIV 0x08c
  76. #define CM_OTPCTL 0x090
  77. #define CM_OTPDIV 0x094
  78. #define CM_PCMCTL 0x098
  79. #define CM_PCMDIV 0x09c
  80. #define CM_PWMCTL 0x0a0
  81. #define CM_PWMDIV 0x0a4
  82. #define CM_SLIMCTL 0x0a8
  83. #define CM_SLIMDIV 0x0ac
  84. #define CM_SMICTL 0x0b0
  85. #define CM_SMIDIV 0x0b4
  86. /* no definition for 0x0b8 and 0x0bc */
  87. #define CM_TCNTCTL 0x0c0
  88. # define CM_TCNT_SRC1_SHIFT 12
  89. #define CM_TCNTCNT 0x0c4
  90. #define CM_TECCTL 0x0c8
  91. #define CM_TECDIV 0x0cc
  92. #define CM_TD0CTL 0x0d0
  93. #define CM_TD0DIV 0x0d4
  94. #define CM_TD1CTL 0x0d8
  95. #define CM_TD1DIV 0x0dc
  96. #define CM_TSENSCTL 0x0e0
  97. #define CM_TSENSDIV 0x0e4
  98. #define CM_TIMERCTL 0x0e8
  99. #define CM_TIMERDIV 0x0ec
  100. #define CM_UARTCTL 0x0f0
  101. #define CM_UARTDIV 0x0f4
  102. #define CM_VECCTL 0x0f8
  103. #define CM_VECDIV 0x0fc
  104. #define CM_PULSECTL 0x190
  105. #define CM_PULSEDIV 0x194
  106. #define CM_SDCCTL 0x1a8
  107. #define CM_SDCDIV 0x1ac
  108. #define CM_ARMCTL 0x1b0
  109. #define CM_AVEOCTL 0x1b8
  110. #define CM_AVEODIV 0x1bc
  111. #define CM_EMMCCTL 0x1c0
  112. #define CM_EMMCDIV 0x1c4
  113. #define CM_EMMC2CTL 0x1d0
  114. #define CM_EMMC2DIV 0x1d4
  115. /* General bits for the CM_*CTL regs */
  116. # define CM_ENABLE BIT(4)
  117. # define CM_KILL BIT(5)
  118. # define CM_GATE_BIT 6
  119. # define CM_GATE BIT(CM_GATE_BIT)
  120. # define CM_BUSY BIT(7)
  121. # define CM_BUSYD BIT(8)
  122. # define CM_FRAC BIT(9)
  123. # define CM_SRC_SHIFT 0
  124. # define CM_SRC_BITS 4
  125. # define CM_SRC_MASK 0xf
  126. # define CM_SRC_GND 0
  127. # define CM_SRC_OSC 1
  128. # define CM_SRC_TESTDEBUG0 2
  129. # define CM_SRC_TESTDEBUG1 3
  130. # define CM_SRC_PLLA_CORE 4
  131. # define CM_SRC_PLLA_PER 4
  132. # define CM_SRC_PLLC_CORE0 5
  133. # define CM_SRC_PLLC_PER 5
  134. # define CM_SRC_PLLC_CORE1 8
  135. # define CM_SRC_PLLD_CORE 6
  136. # define CM_SRC_PLLD_PER 6
  137. # define CM_SRC_PLLH_AUX 7
  138. # define CM_SRC_PLLC_CORE1 8
  139. # define CM_SRC_PLLC_CORE2 9
  140. #define CM_OSCCOUNT 0x100
  141. #define CM_PLLA 0x104
  142. # define CM_PLL_ANARST BIT(8)
  143. # define CM_PLLA_HOLDPER BIT(7)
  144. # define CM_PLLA_LOADPER BIT(6)
  145. # define CM_PLLA_HOLDCORE BIT(5)
  146. # define CM_PLLA_LOADCORE BIT(4)
  147. # define CM_PLLA_HOLDCCP2 BIT(3)
  148. # define CM_PLLA_LOADCCP2 BIT(2)
  149. # define CM_PLLA_HOLDDSI0 BIT(1)
  150. # define CM_PLLA_LOADDSI0 BIT(0)
  151. #define CM_PLLC 0x108
  152. # define CM_PLLC_HOLDPER BIT(7)
  153. # define CM_PLLC_LOADPER BIT(6)
  154. # define CM_PLLC_HOLDCORE2 BIT(5)
  155. # define CM_PLLC_LOADCORE2 BIT(4)
  156. # define CM_PLLC_HOLDCORE1 BIT(3)
  157. # define CM_PLLC_LOADCORE1 BIT(2)
  158. # define CM_PLLC_HOLDCORE0 BIT(1)
  159. # define CM_PLLC_LOADCORE0 BIT(0)
  160. #define CM_PLLD 0x10c
  161. # define CM_PLLD_HOLDPER BIT(7)
  162. # define CM_PLLD_LOADPER BIT(6)
  163. # define CM_PLLD_HOLDCORE BIT(5)
  164. # define CM_PLLD_LOADCORE BIT(4)
  165. # define CM_PLLD_HOLDDSI1 BIT(3)
  166. # define CM_PLLD_LOADDSI1 BIT(2)
  167. # define CM_PLLD_HOLDDSI0 BIT(1)
  168. # define CM_PLLD_LOADDSI0 BIT(0)
  169. #define CM_PLLH 0x110
  170. # define CM_PLLH_LOADRCAL BIT(2)
  171. # define CM_PLLH_LOADAUX BIT(1)
  172. # define CM_PLLH_LOADPIX BIT(0)
  173. #define CM_LOCK 0x114
  174. # define CM_LOCK_FLOCKH BIT(12)
  175. # define CM_LOCK_FLOCKD BIT(11)
  176. # define CM_LOCK_FLOCKC BIT(10)
  177. # define CM_LOCK_FLOCKB BIT(9)
  178. # define CM_LOCK_FLOCKA BIT(8)
  179. #define CM_EVENT 0x118
  180. #define CM_DSI1ECTL 0x158
  181. #define CM_DSI1EDIV 0x15c
  182. #define CM_DSI1PCTL 0x160
  183. #define CM_DSI1PDIV 0x164
  184. #define CM_DFTCTL 0x168
  185. #define CM_DFTDIV 0x16c
  186. #define CM_PLLB 0x170
  187. # define CM_PLLB_HOLDARM BIT(1)
  188. # define CM_PLLB_LOADARM BIT(0)
  189. #define A2W_PLLA_CTRL 0x1100
  190. #define A2W_PLLC_CTRL 0x1120
  191. #define A2W_PLLD_CTRL 0x1140
  192. #define A2W_PLLH_CTRL 0x1160
  193. #define A2W_PLLB_CTRL 0x11e0
  194. # define A2W_PLL_CTRL_PRST_DISABLE BIT(17)
  195. # define A2W_PLL_CTRL_PWRDN BIT(16)
  196. # define A2W_PLL_CTRL_PDIV_MASK 0x000007000
  197. # define A2W_PLL_CTRL_PDIV_SHIFT 12
  198. # define A2W_PLL_CTRL_NDIV_MASK 0x0000003ff
  199. # define A2W_PLL_CTRL_NDIV_SHIFT 0
  200. #define A2W_PLLA_ANA0 0x1010
  201. #define A2W_PLLC_ANA0 0x1030
  202. #define A2W_PLLD_ANA0 0x1050
  203. #define A2W_PLLH_ANA0 0x1070
  204. #define A2W_PLLB_ANA0 0x10f0
  205. #define A2W_PLL_KA_SHIFT 7
  206. #define A2W_PLL_KA_MASK GENMASK(9, 7)
  207. #define A2W_PLL_KI_SHIFT 19
  208. #define A2W_PLL_KI_MASK GENMASK(21, 19)
  209. #define A2W_PLL_KP_SHIFT 15
  210. #define A2W_PLL_KP_MASK GENMASK(18, 15)
  211. #define A2W_PLLH_KA_SHIFT 19
  212. #define A2W_PLLH_KA_MASK GENMASK(21, 19)
  213. #define A2W_PLLH_KI_LOW_SHIFT 22
  214. #define A2W_PLLH_KI_LOW_MASK GENMASK(23, 22)
  215. #define A2W_PLLH_KI_HIGH_SHIFT 0
  216. #define A2W_PLLH_KI_HIGH_MASK GENMASK(0, 0)
  217. #define A2W_PLLH_KP_SHIFT 1
  218. #define A2W_PLLH_KP_MASK GENMASK(4, 1)
  219. #define A2W_XOSC_CTRL 0x1190
  220. # define A2W_XOSC_CTRL_PLLB_ENABLE BIT(7)
  221. # define A2W_XOSC_CTRL_PLLA_ENABLE BIT(6)
  222. # define A2W_XOSC_CTRL_PLLD_ENABLE BIT(5)
  223. # define A2W_XOSC_CTRL_DDR_ENABLE BIT(4)
  224. # define A2W_XOSC_CTRL_CPR1_ENABLE BIT(3)
  225. # define A2W_XOSC_CTRL_USB_ENABLE BIT(2)
  226. # define A2W_XOSC_CTRL_HDMI_ENABLE BIT(1)
  227. # define A2W_XOSC_CTRL_PLLC_ENABLE BIT(0)
  228. #define A2W_PLLA_FRAC 0x1200
  229. #define A2W_PLLC_FRAC 0x1220
  230. #define A2W_PLLD_FRAC 0x1240
  231. #define A2W_PLLH_FRAC 0x1260
  232. #define A2W_PLLB_FRAC 0x12e0
  233. # define A2W_PLL_FRAC_MASK ((1 << A2W_PLL_FRAC_BITS) - 1)
  234. # define A2W_PLL_FRAC_BITS 20
  235. #define A2W_PLL_CHANNEL_DISABLE BIT(8)
  236. #define A2W_PLL_DIV_BITS 8
  237. #define A2W_PLL_DIV_SHIFT 0
  238. #define A2W_PLLA_DSI0 0x1300
  239. #define A2W_PLLA_CORE 0x1400
  240. #define A2W_PLLA_PER 0x1500
  241. #define A2W_PLLA_CCP2 0x1600
  242. #define A2W_PLLC_CORE2 0x1320
  243. #define A2W_PLLC_CORE1 0x1420
  244. #define A2W_PLLC_PER 0x1520
  245. #define A2W_PLLC_CORE0 0x1620
  246. #define A2W_PLLD_DSI0 0x1340
  247. #define A2W_PLLD_CORE 0x1440
  248. #define A2W_PLLD_PER 0x1540
  249. #define A2W_PLLD_DSI1 0x1640
  250. #define A2W_PLLH_AUX 0x1360
  251. #define A2W_PLLH_RCAL 0x1460
  252. #define A2W_PLLH_PIX 0x1560
  253. #define A2W_PLLH_STS 0x1660
  254. #define A2W_PLLH_CTRLR 0x1960
  255. #define A2W_PLLH_FRACR 0x1a60
  256. #define A2W_PLLH_AUXR 0x1b60
  257. #define A2W_PLLH_RCALR 0x1c60
  258. #define A2W_PLLH_PIXR 0x1d60
  259. #define A2W_PLLH_STSR 0x1e60
  260. #define A2W_PLLB_ARM 0x13e0
  261. #define A2W_PLLB_SP0 0x14e0
  262. #define A2W_PLLB_SP1 0x15e0
  263. #define A2W_PLLB_SP2 0x16e0
  264. #define LOCK_TIMEOUT_NS 100000000
  265. #define BCM2835_MAX_FB_RATE 1750000000u
  266. #define SOC_BCM2835 BIT(0)
  267. #define SOC_BCM2711 BIT(1)
  268. #define SOC_ALL (SOC_BCM2835 | SOC_BCM2711)
  269. /*
  270. * Names of clocks used within the driver that need to be replaced
  271. * with an external parent's name. This array is in the order that
  272. * the clocks node in the DT references external clocks.
  273. */
  274. static const char *const cprman_parent_names[] = {
  275. "xosc",
  276. "dsi0_byte",
  277. "dsi0_ddr2",
  278. "dsi0_ddr",
  279. "dsi1_byte",
  280. "dsi1_ddr2",
  281. "dsi1_ddr",
  282. };
  283. struct bcm2835_cprman {
  284. struct device *dev;
  285. void __iomem *regs;
  286. spinlock_t regs_lock; /* spinlock for all clocks */
  287. unsigned int soc;
  288. /*
  289. * Real names of cprman clock parents looked up through
  290. * of_clk_get_parent_name(), which will be used in the
  291. * parent_names[] arrays for clock registration.
  292. */
  293. const char *real_parent_names[ARRAY_SIZE(cprman_parent_names)];
  294. /* Must be last */
  295. struct clk_hw_onecell_data onecell;
  296. };
  297. struct cprman_plat_data {
  298. unsigned int soc;
  299. };
  300. static inline void cprman_write(struct bcm2835_cprman *cprman, u32 reg, u32 val)
  301. {
  302. writel(CM_PASSWORD | val, cprman->regs + reg);
  303. }
  304. static inline u32 cprman_read(struct bcm2835_cprman *cprman, u32 reg)
  305. {
  306. return readl(cprman->regs + reg);
  307. }
  308. /* Does a cycle of measuring a clock through the TCNT clock, which may
  309. * source from many other clocks in the system.
  310. */
  311. static unsigned long bcm2835_measure_tcnt_mux(struct bcm2835_cprman *cprman,
  312. u32 tcnt_mux)
  313. {
  314. u32 osccount = 19200; /* 1ms */
  315. u32 count;
  316. ktime_t timeout;
  317. spin_lock(&cprman->regs_lock);
  318. cprman_write(cprman, CM_TCNTCTL, CM_KILL);
  319. cprman_write(cprman, CM_TCNTCTL,
  320. (tcnt_mux & CM_SRC_MASK) |
  321. (tcnt_mux >> CM_SRC_BITS) << CM_TCNT_SRC1_SHIFT);
  322. cprman_write(cprman, CM_OSCCOUNT, osccount);
  323. /* do a kind delay at the start */
  324. mdelay(1);
  325. /* Finish off whatever is left of OSCCOUNT */
  326. timeout = ktime_add_ns(ktime_get(), LOCK_TIMEOUT_NS);
  327. while (cprman_read(cprman, CM_OSCCOUNT)) {
  328. if (ktime_after(ktime_get(), timeout)) {
  329. dev_err(cprman->dev, "timeout waiting for OSCCOUNT\n");
  330. count = 0;
  331. goto out;
  332. }
  333. cpu_relax();
  334. }
  335. /* Wait for BUSY to clear. */
  336. timeout = ktime_add_ns(ktime_get(), LOCK_TIMEOUT_NS);
  337. while (cprman_read(cprman, CM_TCNTCTL) & CM_BUSY) {
  338. if (ktime_after(ktime_get(), timeout)) {
  339. dev_err(cprman->dev, "timeout waiting for !BUSY\n");
  340. count = 0;
  341. goto out;
  342. }
  343. cpu_relax();
  344. }
  345. count = cprman_read(cprman, CM_TCNTCNT);
  346. cprman_write(cprman, CM_TCNTCTL, 0);
  347. out:
  348. spin_unlock(&cprman->regs_lock);
  349. return count * 1000;
  350. }
  351. static void bcm2835_debugfs_regset(struct bcm2835_cprman *cprman, u32 base,
  352. const struct debugfs_reg32 *regs,
  353. size_t nregs, struct dentry *dentry)
  354. {
  355. struct debugfs_regset32 *regset;
  356. regset = devm_kzalloc(cprman->dev, sizeof(*regset), GFP_KERNEL);
  357. if (!regset)
  358. return;
  359. regset->regs = regs;
  360. regset->nregs = nregs;
  361. regset->base = cprman->regs + base;
  362. debugfs_create_regset32("regdump", S_IRUGO, dentry, regset);
  363. }
  364. struct bcm2835_pll_data {
  365. const char *name;
  366. u32 cm_ctrl_reg;
  367. u32 a2w_ctrl_reg;
  368. u32 frac_reg;
  369. u32 ana_reg_base;
  370. u32 reference_enable_mask;
  371. /* Bit in CM_LOCK to indicate when the PLL has locked. */
  372. u32 lock_mask;
  373. u32 flags;
  374. const struct bcm2835_pll_ana_bits *ana;
  375. unsigned long min_rate;
  376. unsigned long max_rate;
  377. /*
  378. * Highest rate for the VCO before we have to use the
  379. * pre-divide-by-2.
  380. */
  381. unsigned long max_fb_rate;
  382. };
  383. struct bcm2835_pll_ana_bits {
  384. u32 mask0;
  385. u32 set0;
  386. u32 mask1;
  387. u32 set1;
  388. u32 mask3;
  389. u32 set3;
  390. u32 fb_prediv_mask;
  391. };
  392. static const struct bcm2835_pll_ana_bits bcm2835_ana_default = {
  393. .mask0 = 0,
  394. .set0 = 0,
  395. .mask1 = A2W_PLL_KI_MASK | A2W_PLL_KP_MASK,
  396. .set1 = (2 << A2W_PLL_KI_SHIFT) | (8 << A2W_PLL_KP_SHIFT),
  397. .mask3 = A2W_PLL_KA_MASK,
  398. .set3 = (2 << A2W_PLL_KA_SHIFT),
  399. .fb_prediv_mask = BIT(14),
  400. };
  401. static const struct bcm2835_pll_ana_bits bcm2835_ana_pllh = {
  402. .mask0 = A2W_PLLH_KA_MASK | A2W_PLLH_KI_LOW_MASK,
  403. .set0 = (2 << A2W_PLLH_KA_SHIFT) | (2 << A2W_PLLH_KI_LOW_SHIFT),
  404. .mask1 = A2W_PLLH_KI_HIGH_MASK | A2W_PLLH_KP_MASK,
  405. .set1 = (6 << A2W_PLLH_KP_SHIFT),
  406. .mask3 = 0,
  407. .set3 = 0,
  408. .fb_prediv_mask = BIT(11),
  409. };
  410. struct bcm2835_pll_divider_data {
  411. const char *name;
  412. const char *source_pll;
  413. u32 cm_reg;
  414. u32 a2w_reg;
  415. u32 load_mask;
  416. u32 hold_mask;
  417. u32 fixed_divider;
  418. u32 flags;
  419. };
  420. struct bcm2835_clock_data {
  421. const char *name;
  422. const char *const *parents;
  423. int num_mux_parents;
  424. /* Bitmap encoding which parents accept rate change propagation. */
  425. unsigned int set_rate_parent;
  426. u32 ctl_reg;
  427. u32 div_reg;
  428. /* Number of integer bits in the divider */
  429. u32 int_bits;
  430. /* Number of fractional bits in the divider */
  431. u32 frac_bits;
  432. u32 flags;
  433. bool is_vpu_clock;
  434. bool is_mash_clock;
  435. bool low_jitter;
  436. u32 tcnt_mux;
  437. bool round_up;
  438. };
  439. struct bcm2835_gate_data {
  440. const char *name;
  441. const char *parent;
  442. u32 ctl_reg;
  443. };
  444. struct bcm2835_pll {
  445. struct clk_hw hw;
  446. struct bcm2835_cprman *cprman;
  447. const struct bcm2835_pll_data *data;
  448. };
  449. static int bcm2835_pll_is_on(struct clk_hw *hw)
  450. {
  451. struct bcm2835_pll *pll = container_of(hw, struct bcm2835_pll, hw);
  452. struct bcm2835_cprman *cprman = pll->cprman;
  453. const struct bcm2835_pll_data *data = pll->data;
  454. return cprman_read(cprman, data->a2w_ctrl_reg) &
  455. A2W_PLL_CTRL_PRST_DISABLE;
  456. }
  457. static u32 bcm2835_pll_get_prediv_mask(struct bcm2835_cprman *cprman,
  458. const struct bcm2835_pll_data *data)
  459. {
  460. /*
  461. * On BCM2711 there isn't a pre-divisor available in the PLL feedback
  462. * loop. Bits 13:14 of ANA1 (PLLA,PLLB,PLLC,PLLD) have been re-purposed
  463. * for to for VCO RANGE bits.
  464. */
  465. if (cprman->soc & SOC_BCM2711)
  466. return 0;
  467. return data->ana->fb_prediv_mask;
  468. }
  469. static void bcm2835_pll_choose_ndiv_and_fdiv(unsigned long rate,
  470. unsigned long parent_rate,
  471. u32 *ndiv, u32 *fdiv)
  472. {
  473. u64 div;
  474. div = (u64)rate << A2W_PLL_FRAC_BITS;
  475. do_div(div, parent_rate);
  476. *ndiv = div >> A2W_PLL_FRAC_BITS;
  477. *fdiv = div & ((1 << A2W_PLL_FRAC_BITS) - 1);
  478. }
  479. static long bcm2835_pll_rate_from_divisors(unsigned long parent_rate,
  480. u32 ndiv, u32 fdiv, u32 pdiv)
  481. {
  482. u64 rate;
  483. if (pdiv == 0)
  484. return 0;
  485. rate = (u64)parent_rate * ((ndiv << A2W_PLL_FRAC_BITS) + fdiv);
  486. do_div(rate, pdiv);
  487. return rate >> A2W_PLL_FRAC_BITS;
  488. }
  489. static int bcm2835_pll_determine_rate(struct clk_hw *hw,
  490. struct clk_rate_request *req)
  491. {
  492. struct bcm2835_pll *pll = container_of(hw, struct bcm2835_pll, hw);
  493. const struct bcm2835_pll_data *data = pll->data;
  494. u32 ndiv, fdiv;
  495. req->rate = clamp(req->rate, data->min_rate, data->max_rate);
  496. bcm2835_pll_choose_ndiv_and_fdiv(req->rate, req->best_parent_rate,
  497. &ndiv, &fdiv);
  498. req->rate = bcm2835_pll_rate_from_divisors(req->best_parent_rate,
  499. ndiv, fdiv,
  500. 1);
  501. return 0;
  502. }
  503. static unsigned long bcm2835_pll_get_rate(struct clk_hw *hw,
  504. unsigned long parent_rate)
  505. {
  506. struct bcm2835_pll *pll = container_of(hw, struct bcm2835_pll, hw);
  507. struct bcm2835_cprman *cprman = pll->cprman;
  508. const struct bcm2835_pll_data *data = pll->data;
  509. u32 a2wctrl = cprman_read(cprman, data->a2w_ctrl_reg);
  510. u32 ndiv, pdiv, fdiv;
  511. bool using_prediv;
  512. if (parent_rate == 0)
  513. return 0;
  514. fdiv = cprman_read(cprman, data->frac_reg) & A2W_PLL_FRAC_MASK;
  515. ndiv = (a2wctrl & A2W_PLL_CTRL_NDIV_MASK) >> A2W_PLL_CTRL_NDIV_SHIFT;
  516. pdiv = (a2wctrl & A2W_PLL_CTRL_PDIV_MASK) >> A2W_PLL_CTRL_PDIV_SHIFT;
  517. using_prediv = cprman_read(cprman, data->ana_reg_base + 4) &
  518. bcm2835_pll_get_prediv_mask(cprman, data);
  519. if (using_prediv) {
  520. ndiv *= 2;
  521. fdiv *= 2;
  522. }
  523. return bcm2835_pll_rate_from_divisors(parent_rate, ndiv, fdiv, pdiv);
  524. }
  525. static void bcm2835_pll_off(struct clk_hw *hw)
  526. {
  527. struct bcm2835_pll *pll = container_of(hw, struct bcm2835_pll, hw);
  528. struct bcm2835_cprman *cprman = pll->cprman;
  529. const struct bcm2835_pll_data *data = pll->data;
  530. spin_lock(&cprman->regs_lock);
  531. cprman_write(cprman, data->cm_ctrl_reg, CM_PLL_ANARST);
  532. cprman_write(cprman, data->a2w_ctrl_reg,
  533. cprman_read(cprman, data->a2w_ctrl_reg) |
  534. A2W_PLL_CTRL_PWRDN);
  535. spin_unlock(&cprman->regs_lock);
  536. }
  537. static int bcm2835_pll_on(struct clk_hw *hw)
  538. {
  539. struct bcm2835_pll *pll = container_of(hw, struct bcm2835_pll, hw);
  540. struct bcm2835_cprman *cprman = pll->cprman;
  541. const struct bcm2835_pll_data *data = pll->data;
  542. ktime_t timeout;
  543. cprman_write(cprman, data->a2w_ctrl_reg,
  544. cprman_read(cprman, data->a2w_ctrl_reg) &
  545. ~A2W_PLL_CTRL_PWRDN);
  546. /* Take the PLL out of reset. */
  547. spin_lock(&cprman->regs_lock);
  548. cprman_write(cprman, data->cm_ctrl_reg,
  549. cprman_read(cprman, data->cm_ctrl_reg) & ~CM_PLL_ANARST);
  550. spin_unlock(&cprman->regs_lock);
  551. /* Wait for the PLL to lock. */
  552. timeout = ktime_add_ns(ktime_get(), LOCK_TIMEOUT_NS);
  553. while (!(cprman_read(cprman, CM_LOCK) & data->lock_mask)) {
  554. if (ktime_after(ktime_get(), timeout)) {
  555. dev_err(cprman->dev, "%s: couldn't lock PLL\n",
  556. clk_hw_get_name(hw));
  557. return -ETIMEDOUT;
  558. }
  559. cpu_relax();
  560. }
  561. cprman_write(cprman, data->a2w_ctrl_reg,
  562. cprman_read(cprman, data->a2w_ctrl_reg) |
  563. A2W_PLL_CTRL_PRST_DISABLE);
  564. return 0;
  565. }
  566. static void
  567. bcm2835_pll_write_ana(struct bcm2835_cprman *cprman, u32 ana_reg_base, u32 *ana)
  568. {
  569. int i;
  570. /*
  571. * ANA register setup is done as a series of writes to
  572. * ANA3-ANA0, in that order. This lets us write all 4
  573. * registers as a single cycle of the serdes interface (taking
  574. * 100 xosc clocks), whereas if we were to update ana0, 1, and
  575. * 3 individually through their partial-write registers, each
  576. * would be their own serdes cycle.
  577. */
  578. for (i = 3; i >= 0; i--)
  579. cprman_write(cprman, ana_reg_base + i * 4, ana[i]);
  580. }
  581. static int bcm2835_pll_set_rate(struct clk_hw *hw,
  582. unsigned long rate, unsigned long parent_rate)
  583. {
  584. struct bcm2835_pll *pll = container_of(hw, struct bcm2835_pll, hw);
  585. struct bcm2835_cprman *cprman = pll->cprman;
  586. const struct bcm2835_pll_data *data = pll->data;
  587. u32 prediv_mask = bcm2835_pll_get_prediv_mask(cprman, data);
  588. bool was_using_prediv, use_fb_prediv, do_ana_setup_first;
  589. u32 ndiv, fdiv, a2w_ctl;
  590. u32 ana[4];
  591. int i;
  592. if (rate > data->max_fb_rate) {
  593. use_fb_prediv = true;
  594. rate /= 2;
  595. } else {
  596. use_fb_prediv = false;
  597. }
  598. bcm2835_pll_choose_ndiv_and_fdiv(rate, parent_rate, &ndiv, &fdiv);
  599. for (i = 3; i >= 0; i--)
  600. ana[i] = cprman_read(cprman, data->ana_reg_base + i * 4);
  601. was_using_prediv = ana[1] & prediv_mask;
  602. ana[0] &= ~data->ana->mask0;
  603. ana[0] |= data->ana->set0;
  604. ana[1] &= ~data->ana->mask1;
  605. ana[1] |= data->ana->set1;
  606. ana[3] &= ~data->ana->mask3;
  607. ana[3] |= data->ana->set3;
  608. if (was_using_prediv && !use_fb_prediv) {
  609. ana[1] &= ~prediv_mask;
  610. do_ana_setup_first = true;
  611. } else if (!was_using_prediv && use_fb_prediv) {
  612. ana[1] |= prediv_mask;
  613. do_ana_setup_first = false;
  614. } else {
  615. do_ana_setup_first = true;
  616. }
  617. /* Unmask the reference clock from the oscillator. */
  618. spin_lock(&cprman->regs_lock);
  619. cprman_write(cprman, A2W_XOSC_CTRL,
  620. cprman_read(cprman, A2W_XOSC_CTRL) |
  621. data->reference_enable_mask);
  622. spin_unlock(&cprman->regs_lock);
  623. if (do_ana_setup_first)
  624. bcm2835_pll_write_ana(cprman, data->ana_reg_base, ana);
  625. /* Set the PLL multiplier from the oscillator. */
  626. cprman_write(cprman, data->frac_reg, fdiv);
  627. a2w_ctl = cprman_read(cprman, data->a2w_ctrl_reg);
  628. a2w_ctl &= ~A2W_PLL_CTRL_NDIV_MASK;
  629. a2w_ctl |= ndiv << A2W_PLL_CTRL_NDIV_SHIFT;
  630. a2w_ctl &= ~A2W_PLL_CTRL_PDIV_MASK;
  631. a2w_ctl |= 1 << A2W_PLL_CTRL_PDIV_SHIFT;
  632. cprman_write(cprman, data->a2w_ctrl_reg, a2w_ctl);
  633. if (!do_ana_setup_first)
  634. bcm2835_pll_write_ana(cprman, data->ana_reg_base, ana);
  635. return 0;
  636. }
  637. static void bcm2835_pll_debug_init(struct clk_hw *hw,
  638. struct dentry *dentry)
  639. {
  640. struct bcm2835_pll *pll = container_of(hw, struct bcm2835_pll, hw);
  641. struct bcm2835_cprman *cprman = pll->cprman;
  642. const struct bcm2835_pll_data *data = pll->data;
  643. struct debugfs_reg32 *regs;
  644. regs = devm_kcalloc(cprman->dev, 7, sizeof(*regs), GFP_KERNEL);
  645. if (!regs)
  646. return;
  647. regs[0].name = "cm_ctrl";
  648. regs[0].offset = data->cm_ctrl_reg;
  649. regs[1].name = "a2w_ctrl";
  650. regs[1].offset = data->a2w_ctrl_reg;
  651. regs[2].name = "frac";
  652. regs[2].offset = data->frac_reg;
  653. regs[3].name = "ana0";
  654. regs[3].offset = data->ana_reg_base + 0 * 4;
  655. regs[4].name = "ana1";
  656. regs[4].offset = data->ana_reg_base + 1 * 4;
  657. regs[5].name = "ana2";
  658. regs[5].offset = data->ana_reg_base + 2 * 4;
  659. regs[6].name = "ana3";
  660. regs[6].offset = data->ana_reg_base + 3 * 4;
  661. bcm2835_debugfs_regset(cprman, 0, regs, 7, dentry);
  662. }
  663. static const struct clk_ops bcm2835_pll_clk_ops = {
  664. .is_prepared = bcm2835_pll_is_on,
  665. .prepare = bcm2835_pll_on,
  666. .unprepare = bcm2835_pll_off,
  667. .recalc_rate = bcm2835_pll_get_rate,
  668. .set_rate = bcm2835_pll_set_rate,
  669. .determine_rate = bcm2835_pll_determine_rate,
  670. .debug_init = bcm2835_pll_debug_init,
  671. };
  672. struct bcm2835_pll_divider {
  673. struct clk_divider div;
  674. struct bcm2835_cprman *cprman;
  675. const struct bcm2835_pll_divider_data *data;
  676. };
  677. static struct bcm2835_pll_divider *
  678. bcm2835_pll_divider_from_hw(struct clk_hw *hw)
  679. {
  680. return container_of(hw, struct bcm2835_pll_divider, div.hw);
  681. }
  682. static int bcm2835_pll_divider_is_on(struct clk_hw *hw)
  683. {
  684. struct bcm2835_pll_divider *divider = bcm2835_pll_divider_from_hw(hw);
  685. struct bcm2835_cprman *cprman = divider->cprman;
  686. const struct bcm2835_pll_divider_data *data = divider->data;
  687. return !(cprman_read(cprman, data->a2w_reg) & A2W_PLL_CHANNEL_DISABLE);
  688. }
  689. static int bcm2835_pll_divider_determine_rate(struct clk_hw *hw,
  690. struct clk_rate_request *req)
  691. {
  692. return clk_divider_ops.determine_rate(hw, req);
  693. }
  694. static unsigned long bcm2835_pll_divider_get_rate(struct clk_hw *hw,
  695. unsigned long parent_rate)
  696. {
  697. return clk_divider_ops.recalc_rate(hw, parent_rate);
  698. }
  699. static void bcm2835_pll_divider_off(struct clk_hw *hw)
  700. {
  701. struct bcm2835_pll_divider *divider = bcm2835_pll_divider_from_hw(hw);
  702. struct bcm2835_cprman *cprman = divider->cprman;
  703. const struct bcm2835_pll_divider_data *data = divider->data;
  704. spin_lock(&cprman->regs_lock);
  705. cprman_write(cprman, data->cm_reg,
  706. (cprman_read(cprman, data->cm_reg) &
  707. ~data->load_mask) | data->hold_mask);
  708. cprman_write(cprman, data->a2w_reg,
  709. cprman_read(cprman, data->a2w_reg) |
  710. A2W_PLL_CHANNEL_DISABLE);
  711. spin_unlock(&cprman->regs_lock);
  712. }
  713. static int bcm2835_pll_divider_on(struct clk_hw *hw)
  714. {
  715. struct bcm2835_pll_divider *divider = bcm2835_pll_divider_from_hw(hw);
  716. struct bcm2835_cprman *cprman = divider->cprman;
  717. const struct bcm2835_pll_divider_data *data = divider->data;
  718. spin_lock(&cprman->regs_lock);
  719. cprman_write(cprman, data->a2w_reg,
  720. cprman_read(cprman, data->a2w_reg) &
  721. ~A2W_PLL_CHANNEL_DISABLE);
  722. cprman_write(cprman, data->cm_reg,
  723. cprman_read(cprman, data->cm_reg) & ~data->hold_mask);
  724. spin_unlock(&cprman->regs_lock);
  725. return 0;
  726. }
  727. static int bcm2835_pll_divider_set_rate(struct clk_hw *hw,
  728. unsigned long rate,
  729. unsigned long parent_rate)
  730. {
  731. struct bcm2835_pll_divider *divider = bcm2835_pll_divider_from_hw(hw);
  732. struct bcm2835_cprman *cprman = divider->cprman;
  733. const struct bcm2835_pll_divider_data *data = divider->data;
  734. u32 cm, div, max_div = 1 << A2W_PLL_DIV_BITS;
  735. div = DIV_ROUND_UP_ULL(parent_rate, rate);
  736. div = min(div, max_div);
  737. if (div == max_div)
  738. div = 0;
  739. cprman_write(cprman, data->a2w_reg, div);
  740. cm = cprman_read(cprman, data->cm_reg);
  741. cprman_write(cprman, data->cm_reg, cm | data->load_mask);
  742. cprman_write(cprman, data->cm_reg, cm & ~data->load_mask);
  743. return 0;
  744. }
  745. static void bcm2835_pll_divider_debug_init(struct clk_hw *hw,
  746. struct dentry *dentry)
  747. {
  748. struct bcm2835_pll_divider *divider = bcm2835_pll_divider_from_hw(hw);
  749. struct bcm2835_cprman *cprman = divider->cprman;
  750. const struct bcm2835_pll_divider_data *data = divider->data;
  751. struct debugfs_reg32 *regs;
  752. regs = devm_kcalloc(cprman->dev, 7, sizeof(*regs), GFP_KERNEL);
  753. if (!regs)
  754. return;
  755. regs[0].name = "cm";
  756. regs[0].offset = data->cm_reg;
  757. regs[1].name = "a2w";
  758. regs[1].offset = data->a2w_reg;
  759. bcm2835_debugfs_regset(cprman, 0, regs, 2, dentry);
  760. }
  761. static const struct clk_ops bcm2835_pll_divider_clk_ops = {
  762. .is_prepared = bcm2835_pll_divider_is_on,
  763. .prepare = bcm2835_pll_divider_on,
  764. .unprepare = bcm2835_pll_divider_off,
  765. .recalc_rate = bcm2835_pll_divider_get_rate,
  766. .set_rate = bcm2835_pll_divider_set_rate,
  767. .determine_rate = bcm2835_pll_divider_determine_rate,
  768. .debug_init = bcm2835_pll_divider_debug_init,
  769. };
  770. /*
  771. * The CM dividers do fixed-point division, so we can't use the
  772. * generic integer divider code like the PLL dividers do (and we can't
  773. * fake it by having some fixed shifts preceding it in the clock tree,
  774. * because we'd run out of bits in a 32-bit unsigned long).
  775. */
  776. struct bcm2835_clock {
  777. struct clk_hw hw;
  778. struct bcm2835_cprman *cprman;
  779. const struct bcm2835_clock_data *data;
  780. };
  781. static struct bcm2835_clock *bcm2835_clock_from_hw(struct clk_hw *hw)
  782. {
  783. return container_of(hw, struct bcm2835_clock, hw);
  784. }
  785. static int bcm2835_clock_is_on(struct clk_hw *hw)
  786. {
  787. struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
  788. struct bcm2835_cprman *cprman = clock->cprman;
  789. const struct bcm2835_clock_data *data = clock->data;
  790. return (cprman_read(cprman, data->ctl_reg) & CM_ENABLE) != 0;
  791. }
  792. static u32 bcm2835_clock_choose_div(struct clk_hw *hw,
  793. unsigned long rate,
  794. unsigned long parent_rate)
  795. {
  796. struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
  797. const struct bcm2835_clock_data *data = clock->data;
  798. u32 unused_frac_mask =
  799. GENMASK(CM_DIV_FRAC_BITS - data->frac_bits, 0) >> 1;
  800. u64 temp = (u64)parent_rate << CM_DIV_FRAC_BITS;
  801. u32 div, mindiv, maxdiv;
  802. do_div(temp, rate);
  803. div = temp;
  804. div &= ~unused_frac_mask;
  805. /* different clamping limits apply for a mash clock */
  806. if (data->is_mash_clock) {
  807. /* clamp to min divider of 2 */
  808. mindiv = 2 << CM_DIV_FRAC_BITS;
  809. /* clamp to the highest possible integer divider */
  810. maxdiv = (BIT(data->int_bits) - 1) << CM_DIV_FRAC_BITS;
  811. } else {
  812. /* clamp to min divider of 1 */
  813. mindiv = 1 << CM_DIV_FRAC_BITS;
  814. /* clamp to the highest possible fractional divider */
  815. maxdiv = GENMASK(data->int_bits + CM_DIV_FRAC_BITS - 1,
  816. CM_DIV_FRAC_BITS - data->frac_bits);
  817. }
  818. /* apply the clamping limits */
  819. div = max_t(u32, div, mindiv);
  820. div = min_t(u32, div, maxdiv);
  821. return div;
  822. }
  823. static unsigned long bcm2835_clock_rate_from_divisor(struct bcm2835_clock *clock,
  824. unsigned long parent_rate,
  825. u32 div)
  826. {
  827. const struct bcm2835_clock_data *data = clock->data;
  828. u64 temp;
  829. if (data->int_bits == 0 && data->frac_bits == 0)
  830. return parent_rate;
  831. /*
  832. * The divisor is a 12.12 fixed point field, but only some of
  833. * the bits are populated in any given clock.
  834. */
  835. div >>= CM_DIV_FRAC_BITS - data->frac_bits;
  836. div &= (1 << (data->int_bits + data->frac_bits)) - 1;
  837. if (div == 0)
  838. return 0;
  839. temp = (u64)parent_rate << data->frac_bits;
  840. do_div(temp, div);
  841. return temp;
  842. }
  843. static unsigned long bcm2835_round_rate(unsigned long rate)
  844. {
  845. unsigned long scaler;
  846. unsigned long limit;
  847. limit = rate / 100000;
  848. scaler = 1;
  849. while (scaler < limit)
  850. scaler *= 10;
  851. /*
  852. * If increasing a clock by less than 0.1% changes it
  853. * from ..999.. to ..000.., round up.
  854. */
  855. if ((rate + scaler - 1) / scaler % 1000 == 0)
  856. rate = roundup(rate, scaler);
  857. return rate;
  858. }
  859. static unsigned long bcm2835_clock_get_rate(struct clk_hw *hw,
  860. unsigned long parent_rate)
  861. {
  862. struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
  863. struct bcm2835_cprman *cprman = clock->cprman;
  864. const struct bcm2835_clock_data *data = clock->data;
  865. unsigned long rate;
  866. u32 div;
  867. if (data->int_bits == 0 && data->frac_bits == 0)
  868. return parent_rate;
  869. div = cprman_read(cprman, data->div_reg);
  870. rate = bcm2835_clock_rate_from_divisor(clock, parent_rate, div);
  871. if (data->round_up)
  872. rate = bcm2835_round_rate(rate);
  873. return rate;
  874. }
  875. static void bcm2835_clock_wait_busy(struct bcm2835_clock *clock)
  876. {
  877. struct bcm2835_cprman *cprman = clock->cprman;
  878. const struct bcm2835_clock_data *data = clock->data;
  879. ktime_t timeout = ktime_add_ns(ktime_get(), LOCK_TIMEOUT_NS);
  880. while (cprman_read(cprman, data->ctl_reg) & CM_BUSY) {
  881. if (ktime_after(ktime_get(), timeout)) {
  882. dev_err(cprman->dev, "%s: couldn't lock PLL\n",
  883. clk_hw_get_name(&clock->hw));
  884. return;
  885. }
  886. cpu_relax();
  887. }
  888. }
  889. static void bcm2835_clock_off(struct clk_hw *hw)
  890. {
  891. struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
  892. struct bcm2835_cprman *cprman = clock->cprman;
  893. const struct bcm2835_clock_data *data = clock->data;
  894. spin_lock(&cprman->regs_lock);
  895. cprman_write(cprman, data->ctl_reg,
  896. cprman_read(cprman, data->ctl_reg) & ~CM_ENABLE);
  897. spin_unlock(&cprman->regs_lock);
  898. /* BUSY will remain high until the divider completes its cycle. */
  899. bcm2835_clock_wait_busy(clock);
  900. }
  901. static int bcm2835_clock_on(struct clk_hw *hw)
  902. {
  903. struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
  904. struct bcm2835_cprman *cprman = clock->cprman;
  905. const struct bcm2835_clock_data *data = clock->data;
  906. spin_lock(&cprman->regs_lock);
  907. cprman_write(cprman, data->ctl_reg,
  908. cprman_read(cprman, data->ctl_reg) |
  909. CM_ENABLE |
  910. CM_GATE);
  911. spin_unlock(&cprman->regs_lock);
  912. /* Debug code to measure the clock once it's turned on to see
  913. * if it's ticking at the rate we expect.
  914. */
  915. if (data->tcnt_mux && false) {
  916. dev_info(cprman->dev,
  917. "clk %s: rate %ld, measure %ld\n",
  918. data->name,
  919. clk_hw_get_rate(hw),
  920. bcm2835_measure_tcnt_mux(cprman, data->tcnt_mux));
  921. }
  922. return 0;
  923. }
  924. static int bcm2835_clock_set_rate(struct clk_hw *hw,
  925. unsigned long rate, unsigned long parent_rate)
  926. {
  927. struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
  928. struct bcm2835_cprman *cprman = clock->cprman;
  929. const struct bcm2835_clock_data *data = clock->data;
  930. u32 div = bcm2835_clock_choose_div(hw, rate, parent_rate);
  931. u32 ctl;
  932. spin_lock(&cprman->regs_lock);
  933. /*
  934. * Setting up frac support
  935. *
  936. * In principle it is recommended to stop/start the clock first,
  937. * but as we set CLK_SET_RATE_GATE during registration of the
  938. * clock this requirement should be take care of by the
  939. * clk-framework.
  940. */
  941. ctl = cprman_read(cprman, data->ctl_reg) & ~CM_FRAC;
  942. ctl |= (div & CM_DIV_FRAC_MASK) ? CM_FRAC : 0;
  943. cprman_write(cprman, data->ctl_reg, ctl);
  944. cprman_write(cprman, data->div_reg, div);
  945. spin_unlock(&cprman->regs_lock);
  946. return 0;
  947. }
  948. static bool
  949. bcm2835_clk_is_pllc(struct clk_hw *hw)
  950. {
  951. if (!hw)
  952. return false;
  953. return strncmp(clk_hw_get_name(hw), "pllc", 4) == 0;
  954. }
  955. static unsigned long bcm2835_clock_choose_div_and_prate(struct clk_hw *hw,
  956. int parent_idx,
  957. unsigned long rate,
  958. u32 *div,
  959. unsigned long *prate,
  960. unsigned long *avgrate)
  961. {
  962. struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
  963. struct bcm2835_cprman *cprman = clock->cprman;
  964. const struct bcm2835_clock_data *data = clock->data;
  965. unsigned long best_rate = 0;
  966. u32 curdiv, mindiv, maxdiv;
  967. struct clk_hw *parent;
  968. parent = clk_hw_get_parent_by_index(hw, parent_idx);
  969. if (!(BIT(parent_idx) & data->set_rate_parent)) {
  970. *prate = clk_hw_get_rate(parent);
  971. *div = bcm2835_clock_choose_div(hw, rate, *prate);
  972. *avgrate = bcm2835_clock_rate_from_divisor(clock, *prate, *div);
  973. if (data->low_jitter && (*div & CM_DIV_FRAC_MASK)) {
  974. unsigned long high, low;
  975. u32 int_div = *div & ~CM_DIV_FRAC_MASK;
  976. high = bcm2835_clock_rate_from_divisor(clock, *prate,
  977. int_div);
  978. int_div += CM_DIV_FRAC_MASK + 1;
  979. low = bcm2835_clock_rate_from_divisor(clock, *prate,
  980. int_div);
  981. /*
  982. * Return a value which is the maximum deviation
  983. * below the ideal rate, for use as a metric.
  984. */
  985. return *avgrate - max(*avgrate - low, high - *avgrate);
  986. }
  987. return *avgrate;
  988. }
  989. if (data->frac_bits)
  990. dev_warn(cprman->dev,
  991. "frac bits are not used when propagating rate change");
  992. /* clamp to min divider of 2 if we're dealing with a mash clock */
  993. mindiv = data->is_mash_clock ? 2 : 1;
  994. maxdiv = BIT(data->int_bits) - 1;
  995. /* TODO: Be smart, and only test a subset of the available divisors. */
  996. for (curdiv = mindiv; curdiv <= maxdiv; curdiv++) {
  997. unsigned long tmp_rate;
  998. tmp_rate = clk_hw_round_rate(parent, rate * curdiv);
  999. tmp_rate /= curdiv;
  1000. if (curdiv == mindiv ||
  1001. (tmp_rate > best_rate && tmp_rate <= rate))
  1002. best_rate = tmp_rate;
  1003. if (best_rate == rate)
  1004. break;
  1005. }
  1006. *div = curdiv << CM_DIV_FRAC_BITS;
  1007. *prate = curdiv * best_rate;
  1008. *avgrate = best_rate;
  1009. return best_rate;
  1010. }
  1011. static int bcm2835_clock_determine_rate(struct clk_hw *hw,
  1012. struct clk_rate_request *req)
  1013. {
  1014. struct clk_hw *parent, *best_parent = NULL;
  1015. bool current_parent_is_pllc;
  1016. unsigned long rate, best_rate = 0;
  1017. unsigned long prate, best_prate = 0;
  1018. unsigned long avgrate, best_avgrate = 0;
  1019. size_t i;
  1020. u32 div;
  1021. current_parent_is_pllc = bcm2835_clk_is_pllc(clk_hw_get_parent(hw));
  1022. /*
  1023. * Select parent clock that results in the closest but lower rate
  1024. */
  1025. for (i = 0; i < clk_hw_get_num_parents(hw); ++i) {
  1026. parent = clk_hw_get_parent_by_index(hw, i);
  1027. if (!parent)
  1028. continue;
  1029. /*
  1030. * Don't choose a PLLC-derived clock as our parent
  1031. * unless it had been manually set that way. PLLC's
  1032. * frequency gets adjusted by the firmware due to
  1033. * over-temp or under-voltage conditions, without
  1034. * prior notification to our clock consumer.
  1035. */
  1036. if (bcm2835_clk_is_pllc(parent) && !current_parent_is_pllc)
  1037. continue;
  1038. rate = bcm2835_clock_choose_div_and_prate(hw, i, req->rate,
  1039. &div, &prate,
  1040. &avgrate);
  1041. if (abs(req->rate - rate) < abs(req->rate - best_rate)) {
  1042. best_parent = parent;
  1043. best_prate = prate;
  1044. best_rate = rate;
  1045. best_avgrate = avgrate;
  1046. }
  1047. }
  1048. if (!best_parent)
  1049. return -EINVAL;
  1050. req->best_parent_hw = best_parent;
  1051. req->best_parent_rate = best_prate;
  1052. req->rate = best_avgrate;
  1053. return 0;
  1054. }
  1055. static int bcm2835_clock_set_parent(struct clk_hw *hw, u8 index)
  1056. {
  1057. struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
  1058. struct bcm2835_cprman *cprman = clock->cprman;
  1059. const struct bcm2835_clock_data *data = clock->data;
  1060. u8 src = (index << CM_SRC_SHIFT) & CM_SRC_MASK;
  1061. cprman_write(cprman, data->ctl_reg, src);
  1062. return 0;
  1063. }
  1064. static u8 bcm2835_clock_get_parent(struct clk_hw *hw)
  1065. {
  1066. struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
  1067. struct bcm2835_cprman *cprman = clock->cprman;
  1068. const struct bcm2835_clock_data *data = clock->data;
  1069. u32 src = cprman_read(cprman, data->ctl_reg);
  1070. return (src & CM_SRC_MASK) >> CM_SRC_SHIFT;
  1071. }
  1072. static const struct debugfs_reg32 bcm2835_debugfs_clock_reg32[] = {
  1073. {
  1074. .name = "ctl",
  1075. .offset = 0,
  1076. },
  1077. {
  1078. .name = "div",
  1079. .offset = 4,
  1080. },
  1081. };
  1082. static void bcm2835_clock_debug_init(struct clk_hw *hw,
  1083. struct dentry *dentry)
  1084. {
  1085. struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
  1086. struct bcm2835_cprman *cprman = clock->cprman;
  1087. const struct bcm2835_clock_data *data = clock->data;
  1088. bcm2835_debugfs_regset(cprman, data->ctl_reg,
  1089. bcm2835_debugfs_clock_reg32,
  1090. ARRAY_SIZE(bcm2835_debugfs_clock_reg32),
  1091. dentry);
  1092. }
  1093. static const struct clk_ops bcm2835_clock_clk_ops = {
  1094. .is_prepared = bcm2835_clock_is_on,
  1095. .prepare = bcm2835_clock_on,
  1096. .unprepare = bcm2835_clock_off,
  1097. .recalc_rate = bcm2835_clock_get_rate,
  1098. .set_rate = bcm2835_clock_set_rate,
  1099. .determine_rate = bcm2835_clock_determine_rate,
  1100. .set_parent = bcm2835_clock_set_parent,
  1101. .get_parent = bcm2835_clock_get_parent,
  1102. .debug_init = bcm2835_clock_debug_init,
  1103. };
  1104. static int bcm2835_vpu_clock_is_on(struct clk_hw *hw)
  1105. {
  1106. return true;
  1107. }
  1108. /*
  1109. * The VPU clock can never be disabled (it doesn't have an ENABLE
  1110. * bit), so it gets its own set of clock ops.
  1111. */
  1112. static const struct clk_ops bcm2835_vpu_clock_clk_ops = {
  1113. .is_prepared = bcm2835_vpu_clock_is_on,
  1114. .recalc_rate = bcm2835_clock_get_rate,
  1115. .set_rate = bcm2835_clock_set_rate,
  1116. .determine_rate = bcm2835_clock_determine_rate,
  1117. .set_parent = bcm2835_clock_set_parent,
  1118. .get_parent = bcm2835_clock_get_parent,
  1119. .debug_init = bcm2835_clock_debug_init,
  1120. };
  1121. static struct clk_hw *bcm2835_register_pll(struct bcm2835_cprman *cprman,
  1122. const void *data)
  1123. {
  1124. const struct bcm2835_pll_data *pll_data = data;
  1125. struct bcm2835_pll *pll;
  1126. struct clk_init_data init;
  1127. int ret;
  1128. memset(&init, 0, sizeof(init));
  1129. /* All of the PLLs derive from the external oscillator. */
  1130. init.parent_names = &cprman->real_parent_names[0];
  1131. init.num_parents = 1;
  1132. init.name = pll_data->name;
  1133. init.ops = &bcm2835_pll_clk_ops;
  1134. init.flags = pll_data->flags | CLK_IGNORE_UNUSED;
  1135. pll = kzalloc_obj(*pll);
  1136. if (!pll)
  1137. return NULL;
  1138. pll->cprman = cprman;
  1139. pll->data = pll_data;
  1140. pll->hw.init = &init;
  1141. ret = devm_clk_hw_register(cprman->dev, &pll->hw);
  1142. if (ret) {
  1143. kfree(pll);
  1144. return NULL;
  1145. }
  1146. return &pll->hw;
  1147. }
  1148. static struct clk_hw *
  1149. bcm2835_register_pll_divider(struct bcm2835_cprman *cprman,
  1150. const void *data)
  1151. {
  1152. const struct bcm2835_pll_divider_data *divider_data = data;
  1153. struct bcm2835_pll_divider *divider;
  1154. struct clk_init_data init;
  1155. const char *divider_name;
  1156. int ret;
  1157. if (divider_data->fixed_divider != 1) {
  1158. divider_name = devm_kasprintf(cprman->dev, GFP_KERNEL,
  1159. "%s_prediv", divider_data->name);
  1160. if (!divider_name)
  1161. return NULL;
  1162. } else {
  1163. divider_name = divider_data->name;
  1164. }
  1165. memset(&init, 0, sizeof(init));
  1166. init.parent_names = &divider_data->source_pll;
  1167. init.num_parents = 1;
  1168. init.name = divider_name;
  1169. init.ops = &bcm2835_pll_divider_clk_ops;
  1170. init.flags = divider_data->flags | CLK_IGNORE_UNUSED;
  1171. divider = devm_kzalloc(cprman->dev, sizeof(*divider), GFP_KERNEL);
  1172. if (!divider)
  1173. return NULL;
  1174. divider->div.reg = cprman->regs + divider_data->a2w_reg;
  1175. divider->div.shift = A2W_PLL_DIV_SHIFT;
  1176. divider->div.width = A2W_PLL_DIV_BITS;
  1177. divider->div.flags = CLK_DIVIDER_MAX_AT_ZERO;
  1178. divider->div.lock = &cprman->regs_lock;
  1179. divider->div.hw.init = &init;
  1180. divider->div.table = NULL;
  1181. divider->cprman = cprman;
  1182. divider->data = divider_data;
  1183. ret = devm_clk_hw_register(cprman->dev, &divider->div.hw);
  1184. if (ret)
  1185. return ERR_PTR(ret);
  1186. /*
  1187. * PLLH's channels have a fixed divide by 10 afterwards, which
  1188. * is what our consumers are actually using.
  1189. */
  1190. if (divider_data->fixed_divider != 1) {
  1191. return clk_hw_register_fixed_factor(cprman->dev,
  1192. divider_data->name,
  1193. divider_name,
  1194. CLK_SET_RATE_PARENT,
  1195. 1,
  1196. divider_data->fixed_divider);
  1197. }
  1198. return &divider->div.hw;
  1199. }
  1200. static struct clk_hw *bcm2835_register_clock(struct bcm2835_cprman *cprman,
  1201. const void *data)
  1202. {
  1203. const struct bcm2835_clock_data *clock_data = data;
  1204. struct bcm2835_clock *clock;
  1205. struct clk_init_data init;
  1206. const char *parents[1 << CM_SRC_BITS];
  1207. size_t i;
  1208. int ret;
  1209. /*
  1210. * Replace our strings referencing parent clocks with the
  1211. * actual clock-output-name of the parent.
  1212. */
  1213. for (i = 0; i < clock_data->num_mux_parents; i++) {
  1214. parents[i] = clock_data->parents[i];
  1215. ret = match_string(cprman_parent_names,
  1216. ARRAY_SIZE(cprman_parent_names),
  1217. parents[i]);
  1218. if (ret >= 0)
  1219. parents[i] = cprman->real_parent_names[ret];
  1220. }
  1221. memset(&init, 0, sizeof(init));
  1222. init.parent_names = parents;
  1223. init.num_parents = clock_data->num_mux_parents;
  1224. init.name = clock_data->name;
  1225. init.flags = clock_data->flags | CLK_IGNORE_UNUSED;
  1226. /*
  1227. * Pass the CLK_SET_RATE_PARENT flag if we are allowed to propagate
  1228. * rate changes on at least of the parents.
  1229. */
  1230. if (clock_data->set_rate_parent)
  1231. init.flags |= CLK_SET_RATE_PARENT;
  1232. if (clock_data->is_vpu_clock) {
  1233. init.ops = &bcm2835_vpu_clock_clk_ops;
  1234. } else {
  1235. init.ops = &bcm2835_clock_clk_ops;
  1236. init.flags |= CLK_SET_RATE_GATE | CLK_SET_PARENT_GATE;
  1237. /* If the clock wasn't actually enabled at boot, it's not
  1238. * critical.
  1239. */
  1240. if (!(cprman_read(cprman, clock_data->ctl_reg) & CM_ENABLE))
  1241. init.flags &= ~CLK_IS_CRITICAL;
  1242. }
  1243. clock = devm_kzalloc(cprman->dev, sizeof(*clock), GFP_KERNEL);
  1244. if (!clock)
  1245. return NULL;
  1246. clock->cprman = cprman;
  1247. clock->data = clock_data;
  1248. clock->hw.init = &init;
  1249. ret = devm_clk_hw_register(cprman->dev, &clock->hw);
  1250. if (ret)
  1251. return ERR_PTR(ret);
  1252. return &clock->hw;
  1253. }
  1254. static struct clk_hw *bcm2835_register_gate(struct bcm2835_cprman *cprman,
  1255. const void *data)
  1256. {
  1257. const struct bcm2835_gate_data *gate_data = data;
  1258. return clk_hw_register_gate(cprman->dev, gate_data->name,
  1259. gate_data->parent,
  1260. CLK_IGNORE_UNUSED | CLK_SET_RATE_GATE,
  1261. cprman->regs + gate_data->ctl_reg,
  1262. CM_GATE_BIT, 0, &cprman->regs_lock);
  1263. }
  1264. struct bcm2835_clk_desc {
  1265. struct clk_hw *(*clk_register)(struct bcm2835_cprman *cprman,
  1266. const void *data);
  1267. unsigned int supported;
  1268. const void *data;
  1269. };
  1270. /* assignment helper macros for different clock types */
  1271. #define _REGISTER(f, s, ...) { .clk_register = f, \
  1272. .supported = s, \
  1273. .data = __VA_ARGS__ }
  1274. #define REGISTER_PLL(s, ...) _REGISTER(&bcm2835_register_pll, \
  1275. s, \
  1276. &(struct bcm2835_pll_data) \
  1277. {__VA_ARGS__})
  1278. #define REGISTER_PLL_DIV(s, ...) _REGISTER(&bcm2835_register_pll_divider, \
  1279. s, \
  1280. &(struct bcm2835_pll_divider_data) \
  1281. {__VA_ARGS__})
  1282. #define REGISTER_CLK(s, ...) _REGISTER(&bcm2835_register_clock, \
  1283. s, \
  1284. &(struct bcm2835_clock_data) \
  1285. {__VA_ARGS__})
  1286. #define REGISTER_GATE(s, ...) _REGISTER(&bcm2835_register_gate, \
  1287. s, \
  1288. &(struct bcm2835_gate_data) \
  1289. {__VA_ARGS__})
  1290. /* parent mux arrays plus helper macros */
  1291. /* main oscillator parent mux */
  1292. static const char *const bcm2835_clock_osc_parents[] = {
  1293. "gnd",
  1294. "xosc",
  1295. "testdebug0",
  1296. "testdebug1"
  1297. };
  1298. #define REGISTER_OSC_CLK(s, ...) REGISTER_CLK( \
  1299. s, \
  1300. .num_mux_parents = ARRAY_SIZE(bcm2835_clock_osc_parents), \
  1301. .parents = bcm2835_clock_osc_parents, \
  1302. __VA_ARGS__)
  1303. /* main peripheral parent mux */
  1304. static const char *const bcm2835_clock_per_parents[] = {
  1305. "gnd",
  1306. "xosc",
  1307. "testdebug0",
  1308. "testdebug1",
  1309. "plla_per",
  1310. "pllc_per",
  1311. "plld_per",
  1312. "pllh_aux",
  1313. };
  1314. #define REGISTER_PER_CLK(s, ...) REGISTER_CLK( \
  1315. s, \
  1316. .num_mux_parents = ARRAY_SIZE(bcm2835_clock_per_parents), \
  1317. .parents = bcm2835_clock_per_parents, \
  1318. __VA_ARGS__)
  1319. /*
  1320. * Restrict clock sources for the PCM peripheral to the oscillator and
  1321. * PLLD_PER because other source may have varying rates or be switched
  1322. * off.
  1323. *
  1324. * Prevent other sources from being selected by replacing their names in
  1325. * the list of potential parents with dummy entries (entry index is
  1326. * significant).
  1327. */
  1328. static const char *const bcm2835_pcm_per_parents[] = {
  1329. "-",
  1330. "xosc",
  1331. "-",
  1332. "-",
  1333. "-",
  1334. "-",
  1335. "plld_per",
  1336. "-",
  1337. };
  1338. #define REGISTER_PCM_CLK(s, ...) REGISTER_CLK( \
  1339. s, \
  1340. .num_mux_parents = ARRAY_SIZE(bcm2835_pcm_per_parents), \
  1341. .parents = bcm2835_pcm_per_parents, \
  1342. __VA_ARGS__)
  1343. /* main vpu parent mux */
  1344. static const char *const bcm2835_clock_vpu_parents[] = {
  1345. "gnd",
  1346. "xosc",
  1347. "testdebug0",
  1348. "testdebug1",
  1349. "plla_core",
  1350. "pllc_core0",
  1351. "plld_core",
  1352. "pllh_aux",
  1353. "pllc_core1",
  1354. "pllc_core2",
  1355. };
  1356. #define REGISTER_VPU_CLK(s, ...) REGISTER_CLK( \
  1357. s, \
  1358. .num_mux_parents = ARRAY_SIZE(bcm2835_clock_vpu_parents), \
  1359. .parents = bcm2835_clock_vpu_parents, \
  1360. __VA_ARGS__)
  1361. /*
  1362. * DSI parent clocks. The DSI byte/DDR/DDR2 clocks come from the DSI
  1363. * analog PHY. The _inv variants are generated internally to cprman,
  1364. * but we don't use them so they aren't hooked up.
  1365. */
  1366. static const char *const bcm2835_clock_dsi0_parents[] = {
  1367. "gnd",
  1368. "xosc",
  1369. "testdebug0",
  1370. "testdebug1",
  1371. "dsi0_ddr",
  1372. "dsi0_ddr_inv",
  1373. "dsi0_ddr2",
  1374. "dsi0_ddr2_inv",
  1375. "dsi0_byte",
  1376. "dsi0_byte_inv",
  1377. };
  1378. static const char *const bcm2835_clock_dsi1_parents[] = {
  1379. "gnd",
  1380. "xosc",
  1381. "testdebug0",
  1382. "testdebug1",
  1383. "dsi1_ddr",
  1384. "dsi1_ddr_inv",
  1385. "dsi1_ddr2",
  1386. "dsi1_ddr2_inv",
  1387. "dsi1_byte",
  1388. "dsi1_byte_inv",
  1389. };
  1390. #define REGISTER_DSI0_CLK(s, ...) REGISTER_CLK( \
  1391. s, \
  1392. .num_mux_parents = ARRAY_SIZE(bcm2835_clock_dsi0_parents), \
  1393. .parents = bcm2835_clock_dsi0_parents, \
  1394. __VA_ARGS__)
  1395. #define REGISTER_DSI1_CLK(s, ...) REGISTER_CLK( \
  1396. s, \
  1397. .num_mux_parents = ARRAY_SIZE(bcm2835_clock_dsi1_parents), \
  1398. .parents = bcm2835_clock_dsi1_parents, \
  1399. __VA_ARGS__)
  1400. /*
  1401. * the real definition of all the pll, pll_dividers and clocks
  1402. * these make use of the above REGISTER_* macros
  1403. */
  1404. static const struct bcm2835_clk_desc clk_desc_array[] = {
  1405. /* the PLL + PLL dividers */
  1406. /*
  1407. * PLLA is the auxiliary PLL, used to drive the CCP2
  1408. * (Compact Camera Port 2) transmitter clock.
  1409. *
  1410. * It is in the PX LDO power domain, which is on when the
  1411. * AUDIO domain is on.
  1412. */
  1413. [BCM2835_PLLA] = REGISTER_PLL(
  1414. SOC_ALL,
  1415. .name = "plla",
  1416. .cm_ctrl_reg = CM_PLLA,
  1417. .a2w_ctrl_reg = A2W_PLLA_CTRL,
  1418. .frac_reg = A2W_PLLA_FRAC,
  1419. .ana_reg_base = A2W_PLLA_ANA0,
  1420. .reference_enable_mask = A2W_XOSC_CTRL_PLLA_ENABLE,
  1421. .lock_mask = CM_LOCK_FLOCKA,
  1422. .ana = &bcm2835_ana_default,
  1423. .min_rate = 600000000u,
  1424. .max_rate = 2400000000u,
  1425. .max_fb_rate = BCM2835_MAX_FB_RATE),
  1426. [BCM2835_PLLA_CORE] = REGISTER_PLL_DIV(
  1427. SOC_ALL,
  1428. .name = "plla_core",
  1429. .source_pll = "plla",
  1430. .cm_reg = CM_PLLA,
  1431. .a2w_reg = A2W_PLLA_CORE,
  1432. .load_mask = CM_PLLA_LOADCORE,
  1433. .hold_mask = CM_PLLA_HOLDCORE,
  1434. .fixed_divider = 1,
  1435. .flags = CLK_SET_RATE_PARENT),
  1436. [BCM2835_PLLA_PER] = REGISTER_PLL_DIV(
  1437. SOC_ALL,
  1438. .name = "plla_per",
  1439. .source_pll = "plla",
  1440. .cm_reg = CM_PLLA,
  1441. .a2w_reg = A2W_PLLA_PER,
  1442. .load_mask = CM_PLLA_LOADPER,
  1443. .hold_mask = CM_PLLA_HOLDPER,
  1444. .fixed_divider = 1,
  1445. .flags = CLK_SET_RATE_PARENT),
  1446. [BCM2835_PLLA_DSI0] = REGISTER_PLL_DIV(
  1447. SOC_ALL,
  1448. .name = "plla_dsi0",
  1449. .source_pll = "plla",
  1450. .cm_reg = CM_PLLA,
  1451. .a2w_reg = A2W_PLLA_DSI0,
  1452. .load_mask = CM_PLLA_LOADDSI0,
  1453. .hold_mask = CM_PLLA_HOLDDSI0,
  1454. .fixed_divider = 1),
  1455. [BCM2835_PLLA_CCP2] = REGISTER_PLL_DIV(
  1456. SOC_ALL,
  1457. .name = "plla_ccp2",
  1458. .source_pll = "plla",
  1459. .cm_reg = CM_PLLA,
  1460. .a2w_reg = A2W_PLLA_CCP2,
  1461. .load_mask = CM_PLLA_LOADCCP2,
  1462. .hold_mask = CM_PLLA_HOLDCCP2,
  1463. .fixed_divider = 1,
  1464. .flags = CLK_SET_RATE_PARENT),
  1465. /* PLLB is used for the ARM's clock. */
  1466. [BCM2835_PLLB] = REGISTER_PLL(
  1467. SOC_ALL,
  1468. .name = "pllb",
  1469. .cm_ctrl_reg = CM_PLLB,
  1470. .a2w_ctrl_reg = A2W_PLLB_CTRL,
  1471. .frac_reg = A2W_PLLB_FRAC,
  1472. .ana_reg_base = A2W_PLLB_ANA0,
  1473. .reference_enable_mask = A2W_XOSC_CTRL_PLLB_ENABLE,
  1474. .lock_mask = CM_LOCK_FLOCKB,
  1475. .ana = &bcm2835_ana_default,
  1476. .min_rate = 600000000u,
  1477. .max_rate = 3000000000u,
  1478. .max_fb_rate = BCM2835_MAX_FB_RATE,
  1479. .flags = CLK_GET_RATE_NOCACHE),
  1480. [BCM2835_PLLB_ARM] = REGISTER_PLL_DIV(
  1481. SOC_ALL,
  1482. .name = "pllb_arm",
  1483. .source_pll = "pllb",
  1484. .cm_reg = CM_PLLB,
  1485. .a2w_reg = A2W_PLLB_ARM,
  1486. .load_mask = CM_PLLB_LOADARM,
  1487. .hold_mask = CM_PLLB_HOLDARM,
  1488. .fixed_divider = 1,
  1489. .flags = CLK_SET_RATE_PARENT | CLK_GET_RATE_NOCACHE),
  1490. /*
  1491. * PLLC is the core PLL, used to drive the core VPU clock.
  1492. *
  1493. * It is in the PX LDO power domain, which is on when the
  1494. * AUDIO domain is on.
  1495. */
  1496. [BCM2835_PLLC] = REGISTER_PLL(
  1497. SOC_ALL,
  1498. .name = "pllc",
  1499. .cm_ctrl_reg = CM_PLLC,
  1500. .a2w_ctrl_reg = A2W_PLLC_CTRL,
  1501. .frac_reg = A2W_PLLC_FRAC,
  1502. .ana_reg_base = A2W_PLLC_ANA0,
  1503. .reference_enable_mask = A2W_XOSC_CTRL_PLLC_ENABLE,
  1504. .lock_mask = CM_LOCK_FLOCKC,
  1505. .ana = &bcm2835_ana_default,
  1506. .min_rate = 600000000u,
  1507. .max_rate = 3000000000u,
  1508. .max_fb_rate = BCM2835_MAX_FB_RATE),
  1509. [BCM2835_PLLC_CORE0] = REGISTER_PLL_DIV(
  1510. SOC_ALL,
  1511. .name = "pllc_core0",
  1512. .source_pll = "pllc",
  1513. .cm_reg = CM_PLLC,
  1514. .a2w_reg = A2W_PLLC_CORE0,
  1515. .load_mask = CM_PLLC_LOADCORE0,
  1516. .hold_mask = CM_PLLC_HOLDCORE0,
  1517. .fixed_divider = 1,
  1518. .flags = CLK_SET_RATE_PARENT),
  1519. [BCM2835_PLLC_CORE1] = REGISTER_PLL_DIV(
  1520. SOC_ALL,
  1521. .name = "pllc_core1",
  1522. .source_pll = "pllc",
  1523. .cm_reg = CM_PLLC,
  1524. .a2w_reg = A2W_PLLC_CORE1,
  1525. .load_mask = CM_PLLC_LOADCORE1,
  1526. .hold_mask = CM_PLLC_HOLDCORE1,
  1527. .fixed_divider = 1,
  1528. .flags = CLK_SET_RATE_PARENT),
  1529. [BCM2835_PLLC_CORE2] = REGISTER_PLL_DIV(
  1530. SOC_ALL,
  1531. .name = "pllc_core2",
  1532. .source_pll = "pllc",
  1533. .cm_reg = CM_PLLC,
  1534. .a2w_reg = A2W_PLLC_CORE2,
  1535. .load_mask = CM_PLLC_LOADCORE2,
  1536. .hold_mask = CM_PLLC_HOLDCORE2,
  1537. .fixed_divider = 1,
  1538. .flags = CLK_SET_RATE_PARENT),
  1539. [BCM2835_PLLC_PER] = REGISTER_PLL_DIV(
  1540. SOC_ALL,
  1541. .name = "pllc_per",
  1542. .source_pll = "pllc",
  1543. .cm_reg = CM_PLLC,
  1544. .a2w_reg = A2W_PLLC_PER,
  1545. .load_mask = CM_PLLC_LOADPER,
  1546. .hold_mask = CM_PLLC_HOLDPER,
  1547. .fixed_divider = 1,
  1548. .flags = CLK_IS_CRITICAL | CLK_SET_RATE_PARENT),
  1549. /*
  1550. * PLLD is the display PLL, used to drive DSI display panels.
  1551. *
  1552. * It is in the PX LDO power domain, which is on when the
  1553. * AUDIO domain is on.
  1554. */
  1555. [BCM2835_PLLD] = REGISTER_PLL(
  1556. SOC_ALL,
  1557. .name = "plld",
  1558. .cm_ctrl_reg = CM_PLLD,
  1559. .a2w_ctrl_reg = A2W_PLLD_CTRL,
  1560. .frac_reg = A2W_PLLD_FRAC,
  1561. .ana_reg_base = A2W_PLLD_ANA0,
  1562. .reference_enable_mask = A2W_XOSC_CTRL_DDR_ENABLE,
  1563. .lock_mask = CM_LOCK_FLOCKD,
  1564. .ana = &bcm2835_ana_default,
  1565. .min_rate = 600000000u,
  1566. .max_rate = 2400000000u,
  1567. .max_fb_rate = BCM2835_MAX_FB_RATE),
  1568. [BCM2835_PLLD_CORE] = REGISTER_PLL_DIV(
  1569. SOC_ALL,
  1570. .name = "plld_core",
  1571. .source_pll = "plld",
  1572. .cm_reg = CM_PLLD,
  1573. .a2w_reg = A2W_PLLD_CORE,
  1574. .load_mask = CM_PLLD_LOADCORE,
  1575. .hold_mask = CM_PLLD_HOLDCORE,
  1576. .fixed_divider = 1,
  1577. .flags = CLK_SET_RATE_PARENT),
  1578. /*
  1579. * VPU firmware assumes that PLLD_PER isn't disabled by the ARM core.
  1580. * Otherwise this could cause firmware lookups. That's why we mark
  1581. * it as critical.
  1582. */
  1583. [BCM2835_PLLD_PER] = REGISTER_PLL_DIV(
  1584. SOC_ALL,
  1585. .name = "plld_per",
  1586. .source_pll = "plld",
  1587. .cm_reg = CM_PLLD,
  1588. .a2w_reg = A2W_PLLD_PER,
  1589. .load_mask = CM_PLLD_LOADPER,
  1590. .hold_mask = CM_PLLD_HOLDPER,
  1591. .fixed_divider = 1,
  1592. .flags = CLK_IS_CRITICAL | CLK_SET_RATE_PARENT),
  1593. [BCM2835_PLLD_DSI0] = REGISTER_PLL_DIV(
  1594. SOC_ALL,
  1595. .name = "plld_dsi0",
  1596. .source_pll = "plld",
  1597. .cm_reg = CM_PLLD,
  1598. .a2w_reg = A2W_PLLD_DSI0,
  1599. .load_mask = CM_PLLD_LOADDSI0,
  1600. .hold_mask = CM_PLLD_HOLDDSI0,
  1601. .fixed_divider = 1),
  1602. [BCM2835_PLLD_DSI1] = REGISTER_PLL_DIV(
  1603. SOC_ALL,
  1604. .name = "plld_dsi1",
  1605. .source_pll = "plld",
  1606. .cm_reg = CM_PLLD,
  1607. .a2w_reg = A2W_PLLD_DSI1,
  1608. .load_mask = CM_PLLD_LOADDSI1,
  1609. .hold_mask = CM_PLLD_HOLDDSI1,
  1610. .fixed_divider = 1),
  1611. /*
  1612. * PLLH is used to supply the pixel clock or the AUX clock for the
  1613. * TV encoder.
  1614. *
  1615. * It is in the HDMI power domain.
  1616. */
  1617. [BCM2835_PLLH] = REGISTER_PLL(
  1618. SOC_BCM2835,
  1619. "pllh",
  1620. .cm_ctrl_reg = CM_PLLH,
  1621. .a2w_ctrl_reg = A2W_PLLH_CTRL,
  1622. .frac_reg = A2W_PLLH_FRAC,
  1623. .ana_reg_base = A2W_PLLH_ANA0,
  1624. .reference_enable_mask = A2W_XOSC_CTRL_PLLC_ENABLE,
  1625. .lock_mask = CM_LOCK_FLOCKH,
  1626. .ana = &bcm2835_ana_pllh,
  1627. .min_rate = 600000000u,
  1628. .max_rate = 3000000000u,
  1629. .max_fb_rate = BCM2835_MAX_FB_RATE),
  1630. [BCM2835_PLLH_RCAL] = REGISTER_PLL_DIV(
  1631. SOC_BCM2835,
  1632. .name = "pllh_rcal",
  1633. .source_pll = "pllh",
  1634. .cm_reg = CM_PLLH,
  1635. .a2w_reg = A2W_PLLH_RCAL,
  1636. .load_mask = CM_PLLH_LOADRCAL,
  1637. .hold_mask = 0,
  1638. .fixed_divider = 10,
  1639. .flags = CLK_SET_RATE_PARENT),
  1640. [BCM2835_PLLH_AUX] = REGISTER_PLL_DIV(
  1641. SOC_BCM2835,
  1642. .name = "pllh_aux",
  1643. .source_pll = "pllh",
  1644. .cm_reg = CM_PLLH,
  1645. .a2w_reg = A2W_PLLH_AUX,
  1646. .load_mask = CM_PLLH_LOADAUX,
  1647. .hold_mask = 0,
  1648. .fixed_divider = 1,
  1649. .flags = CLK_SET_RATE_PARENT),
  1650. [BCM2835_PLLH_PIX] = REGISTER_PLL_DIV(
  1651. SOC_BCM2835,
  1652. .name = "pllh_pix",
  1653. .source_pll = "pllh",
  1654. .cm_reg = CM_PLLH,
  1655. .a2w_reg = A2W_PLLH_PIX,
  1656. .load_mask = CM_PLLH_LOADPIX,
  1657. .hold_mask = 0,
  1658. .fixed_divider = 10,
  1659. .flags = CLK_SET_RATE_PARENT),
  1660. /* the clocks */
  1661. /* clocks with oscillator parent mux */
  1662. /* One Time Programmable Memory clock. Maximum 10Mhz. */
  1663. [BCM2835_CLOCK_OTP] = REGISTER_OSC_CLK(
  1664. SOC_ALL,
  1665. .name = "otp",
  1666. .ctl_reg = CM_OTPCTL,
  1667. .div_reg = CM_OTPDIV,
  1668. .int_bits = 4,
  1669. .frac_bits = 0,
  1670. .tcnt_mux = 6),
  1671. /*
  1672. * Used for a 1Mhz clock for the system clocksource, and also used
  1673. * bythe watchdog timer and the camera pulse generator.
  1674. */
  1675. [BCM2835_CLOCK_TIMER] = REGISTER_OSC_CLK(
  1676. SOC_ALL,
  1677. .name = "timer",
  1678. .ctl_reg = CM_TIMERCTL,
  1679. .div_reg = CM_TIMERDIV,
  1680. .int_bits = 6,
  1681. .frac_bits = 12),
  1682. /*
  1683. * Clock for the temperature sensor.
  1684. * Generally run at 2Mhz, max 5Mhz.
  1685. */
  1686. [BCM2835_CLOCK_TSENS] = REGISTER_OSC_CLK(
  1687. SOC_ALL,
  1688. .name = "tsens",
  1689. .ctl_reg = CM_TSENSCTL,
  1690. .div_reg = CM_TSENSDIV,
  1691. .int_bits = 5,
  1692. .frac_bits = 0),
  1693. [BCM2835_CLOCK_TEC] = REGISTER_OSC_CLK(
  1694. SOC_ALL,
  1695. .name = "tec",
  1696. .ctl_reg = CM_TECCTL,
  1697. .div_reg = CM_TECDIV,
  1698. .int_bits = 6,
  1699. .frac_bits = 0),
  1700. /* clocks with vpu parent mux */
  1701. [BCM2835_CLOCK_H264] = REGISTER_VPU_CLK(
  1702. SOC_ALL,
  1703. .name = "h264",
  1704. .ctl_reg = CM_H264CTL,
  1705. .div_reg = CM_H264DIV,
  1706. .int_bits = 4,
  1707. .frac_bits = 8,
  1708. .tcnt_mux = 1),
  1709. [BCM2835_CLOCK_ISP] = REGISTER_VPU_CLK(
  1710. SOC_ALL,
  1711. .name = "isp",
  1712. .ctl_reg = CM_ISPCTL,
  1713. .div_reg = CM_ISPDIV,
  1714. .int_bits = 4,
  1715. .frac_bits = 8,
  1716. .tcnt_mux = 2),
  1717. /*
  1718. * Secondary SDRAM clock. Used for low-voltage modes when the PLL
  1719. * in the SDRAM controller can't be used.
  1720. */
  1721. [BCM2835_CLOCK_SDRAM] = REGISTER_VPU_CLK(
  1722. SOC_ALL,
  1723. .name = "sdram",
  1724. .ctl_reg = CM_SDCCTL,
  1725. .div_reg = CM_SDCDIV,
  1726. .int_bits = 6,
  1727. .frac_bits = 0,
  1728. .tcnt_mux = 3),
  1729. [BCM2835_CLOCK_V3D] = REGISTER_VPU_CLK(
  1730. SOC_ALL,
  1731. .name = "v3d",
  1732. .ctl_reg = CM_V3DCTL,
  1733. .div_reg = CM_V3DDIV,
  1734. .int_bits = 4,
  1735. .frac_bits = 8,
  1736. .tcnt_mux = 4),
  1737. /*
  1738. * VPU clock. This doesn't have an enable bit, since it drives
  1739. * the bus for everything else, and is special so it doesn't need
  1740. * to be gated for rate changes. It is also known as "clk_audio"
  1741. * in various hardware documentation.
  1742. */
  1743. [BCM2835_CLOCK_VPU] = REGISTER_VPU_CLK(
  1744. SOC_ALL,
  1745. .name = "vpu",
  1746. .ctl_reg = CM_VPUCTL,
  1747. .div_reg = CM_VPUDIV,
  1748. .int_bits = 12,
  1749. .frac_bits = 8,
  1750. .flags = CLK_IS_CRITICAL,
  1751. .is_vpu_clock = true,
  1752. .tcnt_mux = 5),
  1753. /* clocks with per parent mux */
  1754. [BCM2835_CLOCK_AVEO] = REGISTER_PER_CLK(
  1755. SOC_ALL,
  1756. .name = "aveo",
  1757. .ctl_reg = CM_AVEOCTL,
  1758. .div_reg = CM_AVEODIV,
  1759. .int_bits = 4,
  1760. .frac_bits = 0,
  1761. .tcnt_mux = 38),
  1762. [BCM2835_CLOCK_CAM0] = REGISTER_PER_CLK(
  1763. SOC_ALL,
  1764. .name = "cam0",
  1765. .ctl_reg = CM_CAM0CTL,
  1766. .div_reg = CM_CAM0DIV,
  1767. .int_bits = 4,
  1768. .frac_bits = 8,
  1769. .tcnt_mux = 14),
  1770. [BCM2835_CLOCK_CAM1] = REGISTER_PER_CLK(
  1771. SOC_ALL,
  1772. .name = "cam1",
  1773. .ctl_reg = CM_CAM1CTL,
  1774. .div_reg = CM_CAM1DIV,
  1775. .int_bits = 4,
  1776. .frac_bits = 8,
  1777. .tcnt_mux = 15),
  1778. [BCM2835_CLOCK_DFT] = REGISTER_PER_CLK(
  1779. SOC_ALL,
  1780. .name = "dft",
  1781. .ctl_reg = CM_DFTCTL,
  1782. .div_reg = CM_DFTDIV,
  1783. .int_bits = 5,
  1784. .frac_bits = 0),
  1785. [BCM2835_CLOCK_DPI] = REGISTER_PER_CLK(
  1786. SOC_ALL,
  1787. .name = "dpi",
  1788. .ctl_reg = CM_DPICTL,
  1789. .div_reg = CM_DPIDIV,
  1790. .int_bits = 4,
  1791. .frac_bits = 8,
  1792. .tcnt_mux = 17),
  1793. /* Arasan EMMC clock */
  1794. [BCM2835_CLOCK_EMMC] = REGISTER_PER_CLK(
  1795. SOC_ALL,
  1796. .name = "emmc",
  1797. .ctl_reg = CM_EMMCCTL,
  1798. .div_reg = CM_EMMCDIV,
  1799. .int_bits = 4,
  1800. .frac_bits = 8,
  1801. .tcnt_mux = 39),
  1802. /* EMMC2 clock (only available for BCM2711) */
  1803. [BCM2711_CLOCK_EMMC2] = REGISTER_PER_CLK(
  1804. SOC_BCM2711,
  1805. .name = "emmc2",
  1806. .ctl_reg = CM_EMMC2CTL,
  1807. .div_reg = CM_EMMC2DIV,
  1808. .int_bits = 4,
  1809. .frac_bits = 8,
  1810. .tcnt_mux = 42),
  1811. /* General purpose (GPIO) clocks */
  1812. [BCM2835_CLOCK_GP0] = REGISTER_PER_CLK(
  1813. SOC_ALL,
  1814. .name = "gp0",
  1815. .ctl_reg = CM_GP0CTL,
  1816. .div_reg = CM_GP0DIV,
  1817. .int_bits = 12,
  1818. .frac_bits = 12,
  1819. .is_mash_clock = true,
  1820. .tcnt_mux = 20),
  1821. [BCM2835_CLOCK_GP1] = REGISTER_PER_CLK(
  1822. SOC_ALL,
  1823. .name = "gp1",
  1824. .ctl_reg = CM_GP1CTL,
  1825. .div_reg = CM_GP1DIV,
  1826. .int_bits = 12,
  1827. .frac_bits = 12,
  1828. .flags = CLK_IS_CRITICAL,
  1829. .is_mash_clock = true,
  1830. .tcnt_mux = 21),
  1831. [BCM2835_CLOCK_GP2] = REGISTER_PER_CLK(
  1832. SOC_ALL,
  1833. .name = "gp2",
  1834. .ctl_reg = CM_GP2CTL,
  1835. .div_reg = CM_GP2DIV,
  1836. .int_bits = 12,
  1837. .frac_bits = 12,
  1838. .flags = CLK_IS_CRITICAL),
  1839. /* HDMI state machine */
  1840. [BCM2835_CLOCK_HSM] = REGISTER_PER_CLK(
  1841. SOC_ALL,
  1842. .name = "hsm",
  1843. .ctl_reg = CM_HSMCTL,
  1844. .div_reg = CM_HSMDIV,
  1845. .int_bits = 4,
  1846. .frac_bits = 8,
  1847. .tcnt_mux = 22),
  1848. [BCM2835_CLOCK_PCM] = REGISTER_PCM_CLK(
  1849. SOC_ALL,
  1850. .name = "pcm",
  1851. .ctl_reg = CM_PCMCTL,
  1852. .div_reg = CM_PCMDIV,
  1853. .int_bits = 12,
  1854. .frac_bits = 12,
  1855. .is_mash_clock = true,
  1856. .low_jitter = true,
  1857. .tcnt_mux = 23),
  1858. [BCM2835_CLOCK_PWM] = REGISTER_PER_CLK(
  1859. SOC_ALL,
  1860. .name = "pwm",
  1861. .ctl_reg = CM_PWMCTL,
  1862. .div_reg = CM_PWMDIV,
  1863. .int_bits = 12,
  1864. .frac_bits = 12,
  1865. .is_mash_clock = true,
  1866. .tcnt_mux = 24),
  1867. [BCM2835_CLOCK_SLIM] = REGISTER_PER_CLK(
  1868. SOC_ALL,
  1869. .name = "slim",
  1870. .ctl_reg = CM_SLIMCTL,
  1871. .div_reg = CM_SLIMDIV,
  1872. .int_bits = 12,
  1873. .frac_bits = 12,
  1874. .is_mash_clock = true,
  1875. .tcnt_mux = 25),
  1876. [BCM2835_CLOCK_SMI] = REGISTER_PER_CLK(
  1877. SOC_ALL,
  1878. .name = "smi",
  1879. .ctl_reg = CM_SMICTL,
  1880. .div_reg = CM_SMIDIV,
  1881. .int_bits = 4,
  1882. .frac_bits = 8,
  1883. .tcnt_mux = 27),
  1884. [BCM2835_CLOCK_UART] = REGISTER_PER_CLK(
  1885. SOC_ALL,
  1886. .name = "uart",
  1887. .ctl_reg = CM_UARTCTL,
  1888. .div_reg = CM_UARTDIV,
  1889. .int_bits = 10,
  1890. .frac_bits = 12,
  1891. .tcnt_mux = 28,
  1892. .round_up = true),
  1893. /* TV encoder clock. Only operating frequency is 108Mhz. */
  1894. [BCM2835_CLOCK_VEC] = REGISTER_PER_CLK(
  1895. SOC_ALL,
  1896. .name = "vec",
  1897. .ctl_reg = CM_VECCTL,
  1898. .div_reg = CM_VECDIV,
  1899. .int_bits = 4,
  1900. .frac_bits = 0,
  1901. /*
  1902. * Allow rate change propagation only on PLLH_AUX which is
  1903. * assigned index 7 in the parent array.
  1904. */
  1905. .set_rate_parent = BIT(7),
  1906. .tcnt_mux = 29),
  1907. /* dsi clocks */
  1908. [BCM2835_CLOCK_DSI0E] = REGISTER_PER_CLK(
  1909. SOC_ALL,
  1910. .name = "dsi0e",
  1911. .ctl_reg = CM_DSI0ECTL,
  1912. .div_reg = CM_DSI0EDIV,
  1913. .int_bits = 4,
  1914. .frac_bits = 8,
  1915. .tcnt_mux = 18),
  1916. [BCM2835_CLOCK_DSI1E] = REGISTER_PER_CLK(
  1917. SOC_ALL,
  1918. .name = "dsi1e",
  1919. .ctl_reg = CM_DSI1ECTL,
  1920. .div_reg = CM_DSI1EDIV,
  1921. .int_bits = 4,
  1922. .frac_bits = 8,
  1923. .tcnt_mux = 19),
  1924. [BCM2835_CLOCK_DSI0P] = REGISTER_DSI0_CLK(
  1925. SOC_ALL,
  1926. .name = "dsi0p",
  1927. .ctl_reg = CM_DSI0PCTL,
  1928. .div_reg = CM_DSI0PDIV,
  1929. .int_bits = 0,
  1930. .frac_bits = 0,
  1931. .tcnt_mux = 12),
  1932. [BCM2835_CLOCK_DSI1P] = REGISTER_DSI1_CLK(
  1933. SOC_ALL,
  1934. .name = "dsi1p",
  1935. .ctl_reg = CM_DSI1PCTL,
  1936. .div_reg = CM_DSI1PDIV,
  1937. .int_bits = 0,
  1938. .frac_bits = 0,
  1939. .tcnt_mux = 13),
  1940. /* the gates */
  1941. /*
  1942. * CM_PERIICTL (and CM_PERIACTL, CM_SYSCTL and CM_VPUCTL if
  1943. * you have the debug bit set in the power manager, which we
  1944. * don't bother exposing) are individual gates off of the
  1945. * non-stop vpu clock.
  1946. */
  1947. [BCM2835_CLOCK_PERI_IMAGE] = REGISTER_GATE(
  1948. SOC_ALL,
  1949. .name = "peri_image",
  1950. .parent = "vpu",
  1951. .ctl_reg = CM_PERIICTL),
  1952. };
  1953. /*
  1954. * Permanently take a reference on the parent of the SDRAM clock.
  1955. *
  1956. * While the SDRAM is being driven by its dedicated PLL most of the
  1957. * time, there is a little loop running in the firmware that
  1958. * periodically switches the SDRAM to using our CM clock to do PVT
  1959. * recalibration, with the assumption that the previously configured
  1960. * SDRAM parent is still enabled and running.
  1961. */
  1962. static int bcm2835_mark_sdc_parent_critical(struct clk *sdc)
  1963. {
  1964. struct clk *parent = clk_get_parent(sdc);
  1965. if (IS_ERR(parent))
  1966. return PTR_ERR(parent);
  1967. return clk_prepare_enable(parent);
  1968. }
  1969. static int bcm2835_clk_probe(struct platform_device *pdev)
  1970. {
  1971. struct device *dev = &pdev->dev;
  1972. struct clk_hw **hws;
  1973. struct bcm2835_cprman *cprman;
  1974. const struct bcm2835_clk_desc *desc;
  1975. const size_t asize = ARRAY_SIZE(clk_desc_array);
  1976. const struct cprman_plat_data *pdata;
  1977. size_t i;
  1978. int ret;
  1979. pdata = of_device_get_match_data(&pdev->dev);
  1980. if (!pdata)
  1981. return -ENODEV;
  1982. cprman = devm_kzalloc(dev,
  1983. struct_size(cprman, onecell.hws, asize),
  1984. GFP_KERNEL);
  1985. if (!cprman)
  1986. return -ENOMEM;
  1987. spin_lock_init(&cprman->regs_lock);
  1988. cprman->dev = dev;
  1989. cprman->regs = devm_platform_ioremap_resource(pdev, 0);
  1990. if (IS_ERR(cprman->regs))
  1991. return PTR_ERR(cprman->regs);
  1992. memcpy(cprman->real_parent_names, cprman_parent_names,
  1993. sizeof(cprman_parent_names));
  1994. of_clk_parent_fill(dev->of_node, cprman->real_parent_names,
  1995. ARRAY_SIZE(cprman_parent_names));
  1996. /*
  1997. * Make sure the external oscillator has been registered.
  1998. *
  1999. * The other (DSI) clocks are not present on older device
  2000. * trees, which we still need to support for backwards
  2001. * compatibility.
  2002. */
  2003. if (!cprman->real_parent_names[0])
  2004. return -ENODEV;
  2005. platform_set_drvdata(pdev, cprman);
  2006. cprman->onecell.num = asize;
  2007. cprman->soc = pdata->soc;
  2008. hws = cprman->onecell.hws;
  2009. for (i = 0; i < asize; i++) {
  2010. desc = &clk_desc_array[i];
  2011. if (desc->clk_register && desc->data &&
  2012. (desc->supported & pdata->soc)) {
  2013. hws[i] = desc->clk_register(cprman, desc->data);
  2014. }
  2015. }
  2016. ret = bcm2835_mark_sdc_parent_critical(hws[BCM2835_CLOCK_SDRAM]->clk);
  2017. if (ret)
  2018. return ret;
  2019. return of_clk_add_hw_provider(dev->of_node, of_clk_hw_onecell_get,
  2020. &cprman->onecell);
  2021. }
  2022. static const struct cprman_plat_data cprman_bcm2835_plat_data = {
  2023. .soc = SOC_BCM2835,
  2024. };
  2025. static const struct cprman_plat_data cprman_bcm2711_plat_data = {
  2026. .soc = SOC_BCM2711,
  2027. };
  2028. static const struct of_device_id bcm2835_clk_of_match[] = {
  2029. { .compatible = "brcm,bcm2835-cprman", .data = &cprman_bcm2835_plat_data },
  2030. { .compatible = "brcm,bcm2711-cprman", .data = &cprman_bcm2711_plat_data },
  2031. {}
  2032. };
  2033. MODULE_DEVICE_TABLE(of, bcm2835_clk_of_match);
  2034. static struct platform_driver bcm2835_clk_driver = {
  2035. .driver = {
  2036. .name = "bcm2835-clk",
  2037. .of_match_table = bcm2835_clk_of_match,
  2038. },
  2039. .probe = bcm2835_clk_probe,
  2040. };
  2041. builtin_platform_driver(bcm2835_clk_driver);
  2042. MODULE_AUTHOR("Eric Anholt <eric@anholt.net>");
  2043. MODULE_DESCRIPTION("BCM2835 clock driver");