clk-bcm2835.c 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353
  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_device.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 long bcm2835_pll_round_rate(struct clk_hw *hw, unsigned long rate,
  490. unsigned long *parent_rate)
  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. rate = clamp(rate, data->min_rate, data->max_rate);
  496. bcm2835_pll_choose_ndiv_and_fdiv(rate, *parent_rate, &ndiv, &fdiv);
  497. return bcm2835_pll_rate_from_divisors(*parent_rate, ndiv, fdiv, 1);
  498. }
  499. static unsigned long bcm2835_pll_get_rate(struct clk_hw *hw,
  500. unsigned long parent_rate)
  501. {
  502. struct bcm2835_pll *pll = container_of(hw, struct bcm2835_pll, hw);
  503. struct bcm2835_cprman *cprman = pll->cprman;
  504. const struct bcm2835_pll_data *data = pll->data;
  505. u32 a2wctrl = cprman_read(cprman, data->a2w_ctrl_reg);
  506. u32 ndiv, pdiv, fdiv;
  507. bool using_prediv;
  508. if (parent_rate == 0)
  509. return 0;
  510. fdiv = cprman_read(cprman, data->frac_reg) & A2W_PLL_FRAC_MASK;
  511. ndiv = (a2wctrl & A2W_PLL_CTRL_NDIV_MASK) >> A2W_PLL_CTRL_NDIV_SHIFT;
  512. pdiv = (a2wctrl & A2W_PLL_CTRL_PDIV_MASK) >> A2W_PLL_CTRL_PDIV_SHIFT;
  513. using_prediv = cprman_read(cprman, data->ana_reg_base + 4) &
  514. bcm2835_pll_get_prediv_mask(cprman, data);
  515. if (using_prediv) {
  516. ndiv *= 2;
  517. fdiv *= 2;
  518. }
  519. return bcm2835_pll_rate_from_divisors(parent_rate, ndiv, fdiv, pdiv);
  520. }
  521. static void bcm2835_pll_off(struct clk_hw *hw)
  522. {
  523. struct bcm2835_pll *pll = container_of(hw, struct bcm2835_pll, hw);
  524. struct bcm2835_cprman *cprman = pll->cprman;
  525. const struct bcm2835_pll_data *data = pll->data;
  526. spin_lock(&cprman->regs_lock);
  527. cprman_write(cprman, data->cm_ctrl_reg, CM_PLL_ANARST);
  528. cprman_write(cprman, data->a2w_ctrl_reg,
  529. cprman_read(cprman, data->a2w_ctrl_reg) |
  530. A2W_PLL_CTRL_PWRDN);
  531. spin_unlock(&cprman->regs_lock);
  532. }
  533. static int bcm2835_pll_on(struct clk_hw *hw)
  534. {
  535. struct bcm2835_pll *pll = container_of(hw, struct bcm2835_pll, hw);
  536. struct bcm2835_cprman *cprman = pll->cprman;
  537. const struct bcm2835_pll_data *data = pll->data;
  538. ktime_t timeout;
  539. cprman_write(cprman, data->a2w_ctrl_reg,
  540. cprman_read(cprman, data->a2w_ctrl_reg) &
  541. ~A2W_PLL_CTRL_PWRDN);
  542. /* Take the PLL out of reset. */
  543. spin_lock(&cprman->regs_lock);
  544. cprman_write(cprman, data->cm_ctrl_reg,
  545. cprman_read(cprman, data->cm_ctrl_reg) & ~CM_PLL_ANARST);
  546. spin_unlock(&cprman->regs_lock);
  547. /* Wait for the PLL to lock. */
  548. timeout = ktime_add_ns(ktime_get(), LOCK_TIMEOUT_NS);
  549. while (!(cprman_read(cprman, CM_LOCK) & data->lock_mask)) {
  550. if (ktime_after(ktime_get(), timeout)) {
  551. dev_err(cprman->dev, "%s: couldn't lock PLL\n",
  552. clk_hw_get_name(hw));
  553. return -ETIMEDOUT;
  554. }
  555. cpu_relax();
  556. }
  557. cprman_write(cprman, data->a2w_ctrl_reg,
  558. cprman_read(cprman, data->a2w_ctrl_reg) |
  559. A2W_PLL_CTRL_PRST_DISABLE);
  560. return 0;
  561. }
  562. static void
  563. bcm2835_pll_write_ana(struct bcm2835_cprman *cprman, u32 ana_reg_base, u32 *ana)
  564. {
  565. int i;
  566. /*
  567. * ANA register setup is done as a series of writes to
  568. * ANA3-ANA0, in that order. This lets us write all 4
  569. * registers as a single cycle of the serdes interface (taking
  570. * 100 xosc clocks), whereas if we were to update ana0, 1, and
  571. * 3 individually through their partial-write registers, each
  572. * would be their own serdes cycle.
  573. */
  574. for (i = 3; i >= 0; i--)
  575. cprman_write(cprman, ana_reg_base + i * 4, ana[i]);
  576. }
  577. static int bcm2835_pll_set_rate(struct clk_hw *hw,
  578. unsigned long rate, unsigned long parent_rate)
  579. {
  580. struct bcm2835_pll *pll = container_of(hw, struct bcm2835_pll, hw);
  581. struct bcm2835_cprman *cprman = pll->cprman;
  582. const struct bcm2835_pll_data *data = pll->data;
  583. u32 prediv_mask = bcm2835_pll_get_prediv_mask(cprman, data);
  584. bool was_using_prediv, use_fb_prediv, do_ana_setup_first;
  585. u32 ndiv, fdiv, a2w_ctl;
  586. u32 ana[4];
  587. int i;
  588. if (rate > data->max_fb_rate) {
  589. use_fb_prediv = true;
  590. rate /= 2;
  591. } else {
  592. use_fb_prediv = false;
  593. }
  594. bcm2835_pll_choose_ndiv_and_fdiv(rate, parent_rate, &ndiv, &fdiv);
  595. for (i = 3; i >= 0; i--)
  596. ana[i] = cprman_read(cprman, data->ana_reg_base + i * 4);
  597. was_using_prediv = ana[1] & prediv_mask;
  598. ana[0] &= ~data->ana->mask0;
  599. ana[0] |= data->ana->set0;
  600. ana[1] &= ~data->ana->mask1;
  601. ana[1] |= data->ana->set1;
  602. ana[3] &= ~data->ana->mask3;
  603. ana[3] |= data->ana->set3;
  604. if (was_using_prediv && !use_fb_prediv) {
  605. ana[1] &= ~prediv_mask;
  606. do_ana_setup_first = true;
  607. } else if (!was_using_prediv && use_fb_prediv) {
  608. ana[1] |= prediv_mask;
  609. do_ana_setup_first = false;
  610. } else {
  611. do_ana_setup_first = true;
  612. }
  613. /* Unmask the reference clock from the oscillator. */
  614. spin_lock(&cprman->regs_lock);
  615. cprman_write(cprman, A2W_XOSC_CTRL,
  616. cprman_read(cprman, A2W_XOSC_CTRL) |
  617. data->reference_enable_mask);
  618. spin_unlock(&cprman->regs_lock);
  619. if (do_ana_setup_first)
  620. bcm2835_pll_write_ana(cprman, data->ana_reg_base, ana);
  621. /* Set the PLL multiplier from the oscillator. */
  622. cprman_write(cprman, data->frac_reg, fdiv);
  623. a2w_ctl = cprman_read(cprman, data->a2w_ctrl_reg);
  624. a2w_ctl &= ~A2W_PLL_CTRL_NDIV_MASK;
  625. a2w_ctl |= ndiv << A2W_PLL_CTRL_NDIV_SHIFT;
  626. a2w_ctl &= ~A2W_PLL_CTRL_PDIV_MASK;
  627. a2w_ctl |= 1 << A2W_PLL_CTRL_PDIV_SHIFT;
  628. cprman_write(cprman, data->a2w_ctrl_reg, a2w_ctl);
  629. if (!do_ana_setup_first)
  630. bcm2835_pll_write_ana(cprman, data->ana_reg_base, ana);
  631. return 0;
  632. }
  633. static void bcm2835_pll_debug_init(struct clk_hw *hw,
  634. struct dentry *dentry)
  635. {
  636. struct bcm2835_pll *pll = container_of(hw, struct bcm2835_pll, hw);
  637. struct bcm2835_cprman *cprman = pll->cprman;
  638. const struct bcm2835_pll_data *data = pll->data;
  639. struct debugfs_reg32 *regs;
  640. regs = devm_kcalloc(cprman->dev, 7, sizeof(*regs), GFP_KERNEL);
  641. if (!regs)
  642. return;
  643. regs[0].name = "cm_ctrl";
  644. regs[0].offset = data->cm_ctrl_reg;
  645. regs[1].name = "a2w_ctrl";
  646. regs[1].offset = data->a2w_ctrl_reg;
  647. regs[2].name = "frac";
  648. regs[2].offset = data->frac_reg;
  649. regs[3].name = "ana0";
  650. regs[3].offset = data->ana_reg_base + 0 * 4;
  651. regs[4].name = "ana1";
  652. regs[4].offset = data->ana_reg_base + 1 * 4;
  653. regs[5].name = "ana2";
  654. regs[5].offset = data->ana_reg_base + 2 * 4;
  655. regs[6].name = "ana3";
  656. regs[6].offset = data->ana_reg_base + 3 * 4;
  657. bcm2835_debugfs_regset(cprman, 0, regs, 7, dentry);
  658. }
  659. static const struct clk_ops bcm2835_pll_clk_ops = {
  660. .is_prepared = bcm2835_pll_is_on,
  661. .prepare = bcm2835_pll_on,
  662. .unprepare = bcm2835_pll_off,
  663. .recalc_rate = bcm2835_pll_get_rate,
  664. .set_rate = bcm2835_pll_set_rate,
  665. .round_rate = bcm2835_pll_round_rate,
  666. .debug_init = bcm2835_pll_debug_init,
  667. };
  668. struct bcm2835_pll_divider {
  669. struct clk_divider div;
  670. struct bcm2835_cprman *cprman;
  671. const struct bcm2835_pll_divider_data *data;
  672. };
  673. static struct bcm2835_pll_divider *
  674. bcm2835_pll_divider_from_hw(struct clk_hw *hw)
  675. {
  676. return container_of(hw, struct bcm2835_pll_divider, div.hw);
  677. }
  678. static int bcm2835_pll_divider_is_on(struct clk_hw *hw)
  679. {
  680. struct bcm2835_pll_divider *divider = bcm2835_pll_divider_from_hw(hw);
  681. struct bcm2835_cprman *cprman = divider->cprman;
  682. const struct bcm2835_pll_divider_data *data = divider->data;
  683. return !(cprman_read(cprman, data->a2w_reg) & A2W_PLL_CHANNEL_DISABLE);
  684. }
  685. static int bcm2835_pll_divider_determine_rate(struct clk_hw *hw,
  686. struct clk_rate_request *req)
  687. {
  688. return clk_divider_ops.determine_rate(hw, req);
  689. }
  690. static unsigned long bcm2835_pll_divider_get_rate(struct clk_hw *hw,
  691. unsigned long parent_rate)
  692. {
  693. return clk_divider_ops.recalc_rate(hw, parent_rate);
  694. }
  695. static void bcm2835_pll_divider_off(struct clk_hw *hw)
  696. {
  697. struct bcm2835_pll_divider *divider = bcm2835_pll_divider_from_hw(hw);
  698. struct bcm2835_cprman *cprman = divider->cprman;
  699. const struct bcm2835_pll_divider_data *data = divider->data;
  700. spin_lock(&cprman->regs_lock);
  701. cprman_write(cprman, data->cm_reg,
  702. (cprman_read(cprman, data->cm_reg) &
  703. ~data->load_mask) | data->hold_mask);
  704. cprman_write(cprman, data->a2w_reg,
  705. cprman_read(cprman, data->a2w_reg) |
  706. A2W_PLL_CHANNEL_DISABLE);
  707. spin_unlock(&cprman->regs_lock);
  708. }
  709. static int bcm2835_pll_divider_on(struct clk_hw *hw)
  710. {
  711. struct bcm2835_pll_divider *divider = bcm2835_pll_divider_from_hw(hw);
  712. struct bcm2835_cprman *cprman = divider->cprman;
  713. const struct bcm2835_pll_divider_data *data = divider->data;
  714. spin_lock(&cprman->regs_lock);
  715. cprman_write(cprman, data->a2w_reg,
  716. cprman_read(cprman, data->a2w_reg) &
  717. ~A2W_PLL_CHANNEL_DISABLE);
  718. cprman_write(cprman, data->cm_reg,
  719. cprman_read(cprman, data->cm_reg) & ~data->hold_mask);
  720. spin_unlock(&cprman->regs_lock);
  721. return 0;
  722. }
  723. static int bcm2835_pll_divider_set_rate(struct clk_hw *hw,
  724. unsigned long rate,
  725. unsigned long parent_rate)
  726. {
  727. struct bcm2835_pll_divider *divider = bcm2835_pll_divider_from_hw(hw);
  728. struct bcm2835_cprman *cprman = divider->cprman;
  729. const struct bcm2835_pll_divider_data *data = divider->data;
  730. u32 cm, div, max_div = 1 << A2W_PLL_DIV_BITS;
  731. div = DIV_ROUND_UP_ULL(parent_rate, rate);
  732. div = min(div, max_div);
  733. if (div == max_div)
  734. div = 0;
  735. cprman_write(cprman, data->a2w_reg, div);
  736. cm = cprman_read(cprman, data->cm_reg);
  737. cprman_write(cprman, data->cm_reg, cm | data->load_mask);
  738. cprman_write(cprman, data->cm_reg, cm & ~data->load_mask);
  739. return 0;
  740. }
  741. static void bcm2835_pll_divider_debug_init(struct clk_hw *hw,
  742. struct dentry *dentry)
  743. {
  744. struct bcm2835_pll_divider *divider = bcm2835_pll_divider_from_hw(hw);
  745. struct bcm2835_cprman *cprman = divider->cprman;
  746. const struct bcm2835_pll_divider_data *data = divider->data;
  747. struct debugfs_reg32 *regs;
  748. regs = devm_kcalloc(cprman->dev, 7, sizeof(*regs), GFP_KERNEL);
  749. if (!regs)
  750. return;
  751. regs[0].name = "cm";
  752. regs[0].offset = data->cm_reg;
  753. regs[1].name = "a2w";
  754. regs[1].offset = data->a2w_reg;
  755. bcm2835_debugfs_regset(cprman, 0, regs, 2, dentry);
  756. }
  757. static const struct clk_ops bcm2835_pll_divider_clk_ops = {
  758. .is_prepared = bcm2835_pll_divider_is_on,
  759. .prepare = bcm2835_pll_divider_on,
  760. .unprepare = bcm2835_pll_divider_off,
  761. .recalc_rate = bcm2835_pll_divider_get_rate,
  762. .set_rate = bcm2835_pll_divider_set_rate,
  763. .determine_rate = bcm2835_pll_divider_determine_rate,
  764. .debug_init = bcm2835_pll_divider_debug_init,
  765. };
  766. /*
  767. * The CM dividers do fixed-point division, so we can't use the
  768. * generic integer divider code like the PLL dividers do (and we can't
  769. * fake it by having some fixed shifts preceding it in the clock tree,
  770. * because we'd run out of bits in a 32-bit unsigned long).
  771. */
  772. struct bcm2835_clock {
  773. struct clk_hw hw;
  774. struct bcm2835_cprman *cprman;
  775. const struct bcm2835_clock_data *data;
  776. };
  777. static struct bcm2835_clock *bcm2835_clock_from_hw(struct clk_hw *hw)
  778. {
  779. return container_of(hw, struct bcm2835_clock, hw);
  780. }
  781. static int bcm2835_clock_is_on(struct clk_hw *hw)
  782. {
  783. struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
  784. struct bcm2835_cprman *cprman = clock->cprman;
  785. const struct bcm2835_clock_data *data = clock->data;
  786. return (cprman_read(cprman, data->ctl_reg) & CM_ENABLE) != 0;
  787. }
  788. static u32 bcm2835_clock_choose_div(struct clk_hw *hw,
  789. unsigned long rate,
  790. unsigned long parent_rate)
  791. {
  792. struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
  793. const struct bcm2835_clock_data *data = clock->data;
  794. u32 unused_frac_mask =
  795. GENMASK(CM_DIV_FRAC_BITS - data->frac_bits, 0) >> 1;
  796. u64 temp = (u64)parent_rate << CM_DIV_FRAC_BITS;
  797. u32 div, mindiv, maxdiv;
  798. do_div(temp, rate);
  799. div = temp;
  800. div &= ~unused_frac_mask;
  801. /* different clamping limits apply for a mash clock */
  802. if (data->is_mash_clock) {
  803. /* clamp to min divider of 2 */
  804. mindiv = 2 << CM_DIV_FRAC_BITS;
  805. /* clamp to the highest possible integer divider */
  806. maxdiv = (BIT(data->int_bits) - 1) << CM_DIV_FRAC_BITS;
  807. } else {
  808. /* clamp to min divider of 1 */
  809. mindiv = 1 << CM_DIV_FRAC_BITS;
  810. /* clamp to the highest possible fractional divider */
  811. maxdiv = GENMASK(data->int_bits + CM_DIV_FRAC_BITS - 1,
  812. CM_DIV_FRAC_BITS - data->frac_bits);
  813. }
  814. /* apply the clamping limits */
  815. div = max_t(u32, div, mindiv);
  816. div = min_t(u32, div, maxdiv);
  817. return div;
  818. }
  819. static unsigned long bcm2835_clock_rate_from_divisor(struct bcm2835_clock *clock,
  820. unsigned long parent_rate,
  821. u32 div)
  822. {
  823. const struct bcm2835_clock_data *data = clock->data;
  824. u64 temp;
  825. if (data->int_bits == 0 && data->frac_bits == 0)
  826. return parent_rate;
  827. /*
  828. * The divisor is a 12.12 fixed point field, but only some of
  829. * the bits are populated in any given clock.
  830. */
  831. div >>= CM_DIV_FRAC_BITS - data->frac_bits;
  832. div &= (1 << (data->int_bits + data->frac_bits)) - 1;
  833. if (div == 0)
  834. return 0;
  835. temp = (u64)parent_rate << data->frac_bits;
  836. do_div(temp, div);
  837. return temp;
  838. }
  839. static unsigned long bcm2835_round_rate(unsigned long rate)
  840. {
  841. unsigned long scaler;
  842. unsigned long limit;
  843. limit = rate / 100000;
  844. scaler = 1;
  845. while (scaler < limit)
  846. scaler *= 10;
  847. /*
  848. * If increasing a clock by less than 0.1% changes it
  849. * from ..999.. to ..000.., round up.
  850. */
  851. if ((rate + scaler - 1) / scaler % 1000 == 0)
  852. rate = roundup(rate, scaler);
  853. return rate;
  854. }
  855. static unsigned long bcm2835_clock_get_rate(struct clk_hw *hw,
  856. unsigned long parent_rate)
  857. {
  858. struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
  859. struct bcm2835_cprman *cprman = clock->cprman;
  860. const struct bcm2835_clock_data *data = clock->data;
  861. unsigned long rate;
  862. u32 div;
  863. if (data->int_bits == 0 && data->frac_bits == 0)
  864. return parent_rate;
  865. div = cprman_read(cprman, data->div_reg);
  866. rate = bcm2835_clock_rate_from_divisor(clock, parent_rate, div);
  867. if (data->round_up)
  868. rate = bcm2835_round_rate(rate);
  869. return rate;
  870. }
  871. static void bcm2835_clock_wait_busy(struct bcm2835_clock *clock)
  872. {
  873. struct bcm2835_cprman *cprman = clock->cprman;
  874. const struct bcm2835_clock_data *data = clock->data;
  875. ktime_t timeout = ktime_add_ns(ktime_get(), LOCK_TIMEOUT_NS);
  876. while (cprman_read(cprman, data->ctl_reg) & CM_BUSY) {
  877. if (ktime_after(ktime_get(), timeout)) {
  878. dev_err(cprman->dev, "%s: couldn't lock PLL\n",
  879. clk_hw_get_name(&clock->hw));
  880. return;
  881. }
  882. cpu_relax();
  883. }
  884. }
  885. static void bcm2835_clock_off(struct clk_hw *hw)
  886. {
  887. struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
  888. struct bcm2835_cprman *cprman = clock->cprman;
  889. const struct bcm2835_clock_data *data = clock->data;
  890. spin_lock(&cprman->regs_lock);
  891. cprman_write(cprman, data->ctl_reg,
  892. cprman_read(cprman, data->ctl_reg) & ~CM_ENABLE);
  893. spin_unlock(&cprman->regs_lock);
  894. /* BUSY will remain high until the divider completes its cycle. */
  895. bcm2835_clock_wait_busy(clock);
  896. }
  897. static int bcm2835_clock_on(struct clk_hw *hw)
  898. {
  899. struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
  900. struct bcm2835_cprman *cprman = clock->cprman;
  901. const struct bcm2835_clock_data *data = clock->data;
  902. spin_lock(&cprman->regs_lock);
  903. cprman_write(cprman, data->ctl_reg,
  904. cprman_read(cprman, data->ctl_reg) |
  905. CM_ENABLE |
  906. CM_GATE);
  907. spin_unlock(&cprman->regs_lock);
  908. /* Debug code to measure the clock once it's turned on to see
  909. * if it's ticking at the rate we expect.
  910. */
  911. if (data->tcnt_mux && false) {
  912. dev_info(cprman->dev,
  913. "clk %s: rate %ld, measure %ld\n",
  914. data->name,
  915. clk_hw_get_rate(hw),
  916. bcm2835_measure_tcnt_mux(cprman, data->tcnt_mux));
  917. }
  918. return 0;
  919. }
  920. static int bcm2835_clock_set_rate(struct clk_hw *hw,
  921. unsigned long rate, unsigned long parent_rate)
  922. {
  923. struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
  924. struct bcm2835_cprman *cprman = clock->cprman;
  925. const struct bcm2835_clock_data *data = clock->data;
  926. u32 div = bcm2835_clock_choose_div(hw, rate, parent_rate);
  927. u32 ctl;
  928. spin_lock(&cprman->regs_lock);
  929. /*
  930. * Setting up frac support
  931. *
  932. * In principle it is recommended to stop/start the clock first,
  933. * but as we set CLK_SET_RATE_GATE during registration of the
  934. * clock this requirement should be take care of by the
  935. * clk-framework.
  936. */
  937. ctl = cprman_read(cprman, data->ctl_reg) & ~CM_FRAC;
  938. ctl |= (div & CM_DIV_FRAC_MASK) ? CM_FRAC : 0;
  939. cprman_write(cprman, data->ctl_reg, ctl);
  940. cprman_write(cprman, data->div_reg, div);
  941. spin_unlock(&cprman->regs_lock);
  942. return 0;
  943. }
  944. static bool
  945. bcm2835_clk_is_pllc(struct clk_hw *hw)
  946. {
  947. if (!hw)
  948. return false;
  949. return strncmp(clk_hw_get_name(hw), "pllc", 4) == 0;
  950. }
  951. static unsigned long bcm2835_clock_choose_div_and_prate(struct clk_hw *hw,
  952. int parent_idx,
  953. unsigned long rate,
  954. u32 *div,
  955. unsigned long *prate,
  956. unsigned long *avgrate)
  957. {
  958. struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
  959. struct bcm2835_cprman *cprman = clock->cprman;
  960. const struct bcm2835_clock_data *data = clock->data;
  961. unsigned long best_rate = 0;
  962. u32 curdiv, mindiv, maxdiv;
  963. struct clk_hw *parent;
  964. parent = clk_hw_get_parent_by_index(hw, parent_idx);
  965. if (!(BIT(parent_idx) & data->set_rate_parent)) {
  966. *prate = clk_hw_get_rate(parent);
  967. *div = bcm2835_clock_choose_div(hw, rate, *prate);
  968. *avgrate = bcm2835_clock_rate_from_divisor(clock, *prate, *div);
  969. if (data->low_jitter && (*div & CM_DIV_FRAC_MASK)) {
  970. unsigned long high, low;
  971. u32 int_div = *div & ~CM_DIV_FRAC_MASK;
  972. high = bcm2835_clock_rate_from_divisor(clock, *prate,
  973. int_div);
  974. int_div += CM_DIV_FRAC_MASK + 1;
  975. low = bcm2835_clock_rate_from_divisor(clock, *prate,
  976. int_div);
  977. /*
  978. * Return a value which is the maximum deviation
  979. * below the ideal rate, for use as a metric.
  980. */
  981. return *avgrate - max(*avgrate - low, high - *avgrate);
  982. }
  983. return *avgrate;
  984. }
  985. if (data->frac_bits)
  986. dev_warn(cprman->dev,
  987. "frac bits are not used when propagating rate change");
  988. /* clamp to min divider of 2 if we're dealing with a mash clock */
  989. mindiv = data->is_mash_clock ? 2 : 1;
  990. maxdiv = BIT(data->int_bits) - 1;
  991. /* TODO: Be smart, and only test a subset of the available divisors. */
  992. for (curdiv = mindiv; curdiv <= maxdiv; curdiv++) {
  993. unsigned long tmp_rate;
  994. tmp_rate = clk_hw_round_rate(parent, rate * curdiv);
  995. tmp_rate /= curdiv;
  996. if (curdiv == mindiv ||
  997. (tmp_rate > best_rate && tmp_rate <= rate))
  998. best_rate = tmp_rate;
  999. if (best_rate == rate)
  1000. break;
  1001. }
  1002. *div = curdiv << CM_DIV_FRAC_BITS;
  1003. *prate = curdiv * best_rate;
  1004. *avgrate = best_rate;
  1005. return best_rate;
  1006. }
  1007. static int bcm2835_clock_determine_rate(struct clk_hw *hw,
  1008. struct clk_rate_request *req)
  1009. {
  1010. struct clk_hw *parent, *best_parent = NULL;
  1011. bool current_parent_is_pllc;
  1012. unsigned long rate, best_rate = 0;
  1013. unsigned long prate, best_prate = 0;
  1014. unsigned long avgrate, best_avgrate = 0;
  1015. size_t i;
  1016. u32 div;
  1017. current_parent_is_pllc = bcm2835_clk_is_pllc(clk_hw_get_parent(hw));
  1018. /*
  1019. * Select parent clock that results in the closest but lower rate
  1020. */
  1021. for (i = 0; i < clk_hw_get_num_parents(hw); ++i) {
  1022. parent = clk_hw_get_parent_by_index(hw, i);
  1023. if (!parent)
  1024. continue;
  1025. /*
  1026. * Don't choose a PLLC-derived clock as our parent
  1027. * unless it had been manually set that way. PLLC's
  1028. * frequency gets adjusted by the firmware due to
  1029. * over-temp or under-voltage conditions, without
  1030. * prior notification to our clock consumer.
  1031. */
  1032. if (bcm2835_clk_is_pllc(parent) && !current_parent_is_pllc)
  1033. continue;
  1034. rate = bcm2835_clock_choose_div_and_prate(hw, i, req->rate,
  1035. &div, &prate,
  1036. &avgrate);
  1037. if (abs(req->rate - rate) < abs(req->rate - best_rate)) {
  1038. best_parent = parent;
  1039. best_prate = prate;
  1040. best_rate = rate;
  1041. best_avgrate = avgrate;
  1042. }
  1043. }
  1044. if (!best_parent)
  1045. return -EINVAL;
  1046. req->best_parent_hw = best_parent;
  1047. req->best_parent_rate = best_prate;
  1048. req->rate = best_avgrate;
  1049. return 0;
  1050. }
  1051. static int bcm2835_clock_set_parent(struct clk_hw *hw, u8 index)
  1052. {
  1053. struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
  1054. struct bcm2835_cprman *cprman = clock->cprman;
  1055. const struct bcm2835_clock_data *data = clock->data;
  1056. u8 src = (index << CM_SRC_SHIFT) & CM_SRC_MASK;
  1057. cprman_write(cprman, data->ctl_reg, src);
  1058. return 0;
  1059. }
  1060. static u8 bcm2835_clock_get_parent(struct clk_hw *hw)
  1061. {
  1062. struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
  1063. struct bcm2835_cprman *cprman = clock->cprman;
  1064. const struct bcm2835_clock_data *data = clock->data;
  1065. u32 src = cprman_read(cprman, data->ctl_reg);
  1066. return (src & CM_SRC_MASK) >> CM_SRC_SHIFT;
  1067. }
  1068. static const struct debugfs_reg32 bcm2835_debugfs_clock_reg32[] = {
  1069. {
  1070. .name = "ctl",
  1071. .offset = 0,
  1072. },
  1073. {
  1074. .name = "div",
  1075. .offset = 4,
  1076. },
  1077. };
  1078. static void bcm2835_clock_debug_init(struct clk_hw *hw,
  1079. struct dentry *dentry)
  1080. {
  1081. struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
  1082. struct bcm2835_cprman *cprman = clock->cprman;
  1083. const struct bcm2835_clock_data *data = clock->data;
  1084. bcm2835_debugfs_regset(cprman, data->ctl_reg,
  1085. bcm2835_debugfs_clock_reg32,
  1086. ARRAY_SIZE(bcm2835_debugfs_clock_reg32),
  1087. dentry);
  1088. }
  1089. static const struct clk_ops bcm2835_clock_clk_ops = {
  1090. .is_prepared = bcm2835_clock_is_on,
  1091. .prepare = bcm2835_clock_on,
  1092. .unprepare = bcm2835_clock_off,
  1093. .recalc_rate = bcm2835_clock_get_rate,
  1094. .set_rate = bcm2835_clock_set_rate,
  1095. .determine_rate = bcm2835_clock_determine_rate,
  1096. .set_parent = bcm2835_clock_set_parent,
  1097. .get_parent = bcm2835_clock_get_parent,
  1098. .debug_init = bcm2835_clock_debug_init,
  1099. };
  1100. static int bcm2835_vpu_clock_is_on(struct clk_hw *hw)
  1101. {
  1102. return true;
  1103. }
  1104. /*
  1105. * The VPU clock can never be disabled (it doesn't have an ENABLE
  1106. * bit), so it gets its own set of clock ops.
  1107. */
  1108. static const struct clk_ops bcm2835_vpu_clock_clk_ops = {
  1109. .is_prepared = bcm2835_vpu_clock_is_on,
  1110. .recalc_rate = bcm2835_clock_get_rate,
  1111. .set_rate = bcm2835_clock_set_rate,
  1112. .determine_rate = bcm2835_clock_determine_rate,
  1113. .set_parent = bcm2835_clock_set_parent,
  1114. .get_parent = bcm2835_clock_get_parent,
  1115. .debug_init = bcm2835_clock_debug_init,
  1116. };
  1117. static struct clk_hw *bcm2835_register_pll(struct bcm2835_cprman *cprman,
  1118. const void *data)
  1119. {
  1120. const struct bcm2835_pll_data *pll_data = data;
  1121. struct bcm2835_pll *pll;
  1122. struct clk_init_data init;
  1123. int ret;
  1124. memset(&init, 0, sizeof(init));
  1125. /* All of the PLLs derive from the external oscillator. */
  1126. init.parent_names = &cprman->real_parent_names[0];
  1127. init.num_parents = 1;
  1128. init.name = pll_data->name;
  1129. init.ops = &bcm2835_pll_clk_ops;
  1130. init.flags = pll_data->flags | CLK_IGNORE_UNUSED;
  1131. pll = kzalloc(sizeof(*pll), GFP_KERNEL);
  1132. if (!pll)
  1133. return NULL;
  1134. pll->cprman = cprman;
  1135. pll->data = pll_data;
  1136. pll->hw.init = &init;
  1137. ret = devm_clk_hw_register(cprman->dev, &pll->hw);
  1138. if (ret) {
  1139. kfree(pll);
  1140. return NULL;
  1141. }
  1142. return &pll->hw;
  1143. }
  1144. static struct clk_hw *
  1145. bcm2835_register_pll_divider(struct bcm2835_cprman *cprman,
  1146. const void *data)
  1147. {
  1148. const struct bcm2835_pll_divider_data *divider_data = data;
  1149. struct bcm2835_pll_divider *divider;
  1150. struct clk_init_data init;
  1151. const char *divider_name;
  1152. int ret;
  1153. if (divider_data->fixed_divider != 1) {
  1154. divider_name = devm_kasprintf(cprman->dev, GFP_KERNEL,
  1155. "%s_prediv", divider_data->name);
  1156. if (!divider_name)
  1157. return NULL;
  1158. } else {
  1159. divider_name = divider_data->name;
  1160. }
  1161. memset(&init, 0, sizeof(init));
  1162. init.parent_names = &divider_data->source_pll;
  1163. init.num_parents = 1;
  1164. init.name = divider_name;
  1165. init.ops = &bcm2835_pll_divider_clk_ops;
  1166. init.flags = divider_data->flags | CLK_IGNORE_UNUSED;
  1167. divider = devm_kzalloc(cprman->dev, sizeof(*divider), GFP_KERNEL);
  1168. if (!divider)
  1169. return NULL;
  1170. divider->div.reg = cprman->regs + divider_data->a2w_reg;
  1171. divider->div.shift = A2W_PLL_DIV_SHIFT;
  1172. divider->div.width = A2W_PLL_DIV_BITS;
  1173. divider->div.flags = CLK_DIVIDER_MAX_AT_ZERO;
  1174. divider->div.lock = &cprman->regs_lock;
  1175. divider->div.hw.init = &init;
  1176. divider->div.table = NULL;
  1177. divider->cprman = cprman;
  1178. divider->data = divider_data;
  1179. ret = devm_clk_hw_register(cprman->dev, &divider->div.hw);
  1180. if (ret)
  1181. return ERR_PTR(ret);
  1182. /*
  1183. * PLLH's channels have a fixed divide by 10 afterwards, which
  1184. * is what our consumers are actually using.
  1185. */
  1186. if (divider_data->fixed_divider != 1) {
  1187. return clk_hw_register_fixed_factor(cprman->dev,
  1188. divider_data->name,
  1189. divider_name,
  1190. CLK_SET_RATE_PARENT,
  1191. 1,
  1192. divider_data->fixed_divider);
  1193. }
  1194. return &divider->div.hw;
  1195. }
  1196. static struct clk_hw *bcm2835_register_clock(struct bcm2835_cprman *cprman,
  1197. const void *data)
  1198. {
  1199. const struct bcm2835_clock_data *clock_data = data;
  1200. struct bcm2835_clock *clock;
  1201. struct clk_init_data init;
  1202. const char *parents[1 << CM_SRC_BITS];
  1203. size_t i;
  1204. int ret;
  1205. /*
  1206. * Replace our strings referencing parent clocks with the
  1207. * actual clock-output-name of the parent.
  1208. */
  1209. for (i = 0; i < clock_data->num_mux_parents; i++) {
  1210. parents[i] = clock_data->parents[i];
  1211. ret = match_string(cprman_parent_names,
  1212. ARRAY_SIZE(cprman_parent_names),
  1213. parents[i]);
  1214. if (ret >= 0)
  1215. parents[i] = cprman->real_parent_names[ret];
  1216. }
  1217. memset(&init, 0, sizeof(init));
  1218. init.parent_names = parents;
  1219. init.num_parents = clock_data->num_mux_parents;
  1220. init.name = clock_data->name;
  1221. init.flags = clock_data->flags | CLK_IGNORE_UNUSED;
  1222. /*
  1223. * Pass the CLK_SET_RATE_PARENT flag if we are allowed to propagate
  1224. * rate changes on at least of the parents.
  1225. */
  1226. if (clock_data->set_rate_parent)
  1227. init.flags |= CLK_SET_RATE_PARENT;
  1228. if (clock_data->is_vpu_clock) {
  1229. init.ops = &bcm2835_vpu_clock_clk_ops;
  1230. } else {
  1231. init.ops = &bcm2835_clock_clk_ops;
  1232. init.flags |= CLK_SET_RATE_GATE | CLK_SET_PARENT_GATE;
  1233. /* If the clock wasn't actually enabled at boot, it's not
  1234. * critical.
  1235. */
  1236. if (!(cprman_read(cprman, clock_data->ctl_reg) & CM_ENABLE))
  1237. init.flags &= ~CLK_IS_CRITICAL;
  1238. }
  1239. clock = devm_kzalloc(cprman->dev, sizeof(*clock), GFP_KERNEL);
  1240. if (!clock)
  1241. return NULL;
  1242. clock->cprman = cprman;
  1243. clock->data = clock_data;
  1244. clock->hw.init = &init;
  1245. ret = devm_clk_hw_register(cprman->dev, &clock->hw);
  1246. if (ret)
  1247. return ERR_PTR(ret);
  1248. return &clock->hw;
  1249. }
  1250. static struct clk_hw *bcm2835_register_gate(struct bcm2835_cprman *cprman,
  1251. const void *data)
  1252. {
  1253. const struct bcm2835_gate_data *gate_data = data;
  1254. return clk_hw_register_gate(cprman->dev, gate_data->name,
  1255. gate_data->parent,
  1256. CLK_IGNORE_UNUSED | CLK_SET_RATE_GATE,
  1257. cprman->regs + gate_data->ctl_reg,
  1258. CM_GATE_BIT, 0, &cprman->regs_lock);
  1259. }
  1260. struct bcm2835_clk_desc {
  1261. struct clk_hw *(*clk_register)(struct bcm2835_cprman *cprman,
  1262. const void *data);
  1263. unsigned int supported;
  1264. const void *data;
  1265. };
  1266. /* assignment helper macros for different clock types */
  1267. #define _REGISTER(f, s, ...) { .clk_register = f, \
  1268. .supported = s, \
  1269. .data = __VA_ARGS__ }
  1270. #define REGISTER_PLL(s, ...) _REGISTER(&bcm2835_register_pll, \
  1271. s, \
  1272. &(struct bcm2835_pll_data) \
  1273. {__VA_ARGS__})
  1274. #define REGISTER_PLL_DIV(s, ...) _REGISTER(&bcm2835_register_pll_divider, \
  1275. s, \
  1276. &(struct bcm2835_pll_divider_data) \
  1277. {__VA_ARGS__})
  1278. #define REGISTER_CLK(s, ...) _REGISTER(&bcm2835_register_clock, \
  1279. s, \
  1280. &(struct bcm2835_clock_data) \
  1281. {__VA_ARGS__})
  1282. #define REGISTER_GATE(s, ...) _REGISTER(&bcm2835_register_gate, \
  1283. s, \
  1284. &(struct bcm2835_gate_data) \
  1285. {__VA_ARGS__})
  1286. /* parent mux arrays plus helper macros */
  1287. /* main oscillator parent mux */
  1288. static const char *const bcm2835_clock_osc_parents[] = {
  1289. "gnd",
  1290. "xosc",
  1291. "testdebug0",
  1292. "testdebug1"
  1293. };
  1294. #define REGISTER_OSC_CLK(s, ...) REGISTER_CLK( \
  1295. s, \
  1296. .num_mux_parents = ARRAY_SIZE(bcm2835_clock_osc_parents), \
  1297. .parents = bcm2835_clock_osc_parents, \
  1298. __VA_ARGS__)
  1299. /* main peripherial parent mux */
  1300. static const char *const bcm2835_clock_per_parents[] = {
  1301. "gnd",
  1302. "xosc",
  1303. "testdebug0",
  1304. "testdebug1",
  1305. "plla_per",
  1306. "pllc_per",
  1307. "plld_per",
  1308. "pllh_aux",
  1309. };
  1310. #define REGISTER_PER_CLK(s, ...) REGISTER_CLK( \
  1311. s, \
  1312. .num_mux_parents = ARRAY_SIZE(bcm2835_clock_per_parents), \
  1313. .parents = bcm2835_clock_per_parents, \
  1314. __VA_ARGS__)
  1315. /*
  1316. * Restrict clock sources for the PCM peripheral to the oscillator and
  1317. * PLLD_PER because other source may have varying rates or be switched
  1318. * off.
  1319. *
  1320. * Prevent other sources from being selected by replacing their names in
  1321. * the list of potential parents with dummy entries (entry index is
  1322. * significant).
  1323. */
  1324. static const char *const bcm2835_pcm_per_parents[] = {
  1325. "-",
  1326. "xosc",
  1327. "-",
  1328. "-",
  1329. "-",
  1330. "-",
  1331. "plld_per",
  1332. "-",
  1333. };
  1334. #define REGISTER_PCM_CLK(s, ...) REGISTER_CLK( \
  1335. s, \
  1336. .num_mux_parents = ARRAY_SIZE(bcm2835_pcm_per_parents), \
  1337. .parents = bcm2835_pcm_per_parents, \
  1338. __VA_ARGS__)
  1339. /* main vpu parent mux */
  1340. static const char *const bcm2835_clock_vpu_parents[] = {
  1341. "gnd",
  1342. "xosc",
  1343. "testdebug0",
  1344. "testdebug1",
  1345. "plla_core",
  1346. "pllc_core0",
  1347. "plld_core",
  1348. "pllh_aux",
  1349. "pllc_core1",
  1350. "pllc_core2",
  1351. };
  1352. #define REGISTER_VPU_CLK(s, ...) REGISTER_CLK( \
  1353. s, \
  1354. .num_mux_parents = ARRAY_SIZE(bcm2835_clock_vpu_parents), \
  1355. .parents = bcm2835_clock_vpu_parents, \
  1356. __VA_ARGS__)
  1357. /*
  1358. * DSI parent clocks. The DSI byte/DDR/DDR2 clocks come from the DSI
  1359. * analog PHY. The _inv variants are generated internally to cprman,
  1360. * but we don't use them so they aren't hooked up.
  1361. */
  1362. static const char *const bcm2835_clock_dsi0_parents[] = {
  1363. "gnd",
  1364. "xosc",
  1365. "testdebug0",
  1366. "testdebug1",
  1367. "dsi0_ddr",
  1368. "dsi0_ddr_inv",
  1369. "dsi0_ddr2",
  1370. "dsi0_ddr2_inv",
  1371. "dsi0_byte",
  1372. "dsi0_byte_inv",
  1373. };
  1374. static const char *const bcm2835_clock_dsi1_parents[] = {
  1375. "gnd",
  1376. "xosc",
  1377. "testdebug0",
  1378. "testdebug1",
  1379. "dsi1_ddr",
  1380. "dsi1_ddr_inv",
  1381. "dsi1_ddr2",
  1382. "dsi1_ddr2_inv",
  1383. "dsi1_byte",
  1384. "dsi1_byte_inv",
  1385. };
  1386. #define REGISTER_DSI0_CLK(s, ...) REGISTER_CLK( \
  1387. s, \
  1388. .num_mux_parents = ARRAY_SIZE(bcm2835_clock_dsi0_parents), \
  1389. .parents = bcm2835_clock_dsi0_parents, \
  1390. __VA_ARGS__)
  1391. #define REGISTER_DSI1_CLK(s, ...) REGISTER_CLK( \
  1392. s, \
  1393. .num_mux_parents = ARRAY_SIZE(bcm2835_clock_dsi1_parents), \
  1394. .parents = bcm2835_clock_dsi1_parents, \
  1395. __VA_ARGS__)
  1396. /*
  1397. * the real definition of all the pll, pll_dividers and clocks
  1398. * these make use of the above REGISTER_* macros
  1399. */
  1400. static const struct bcm2835_clk_desc clk_desc_array[] = {
  1401. /* the PLL + PLL dividers */
  1402. /*
  1403. * PLLA is the auxiliary PLL, used to drive the CCP2
  1404. * (Compact Camera Port 2) transmitter clock.
  1405. *
  1406. * It is in the PX LDO power domain, which is on when the
  1407. * AUDIO domain is on.
  1408. */
  1409. [BCM2835_PLLA] = REGISTER_PLL(
  1410. SOC_ALL,
  1411. .name = "plla",
  1412. .cm_ctrl_reg = CM_PLLA,
  1413. .a2w_ctrl_reg = A2W_PLLA_CTRL,
  1414. .frac_reg = A2W_PLLA_FRAC,
  1415. .ana_reg_base = A2W_PLLA_ANA0,
  1416. .reference_enable_mask = A2W_XOSC_CTRL_PLLA_ENABLE,
  1417. .lock_mask = CM_LOCK_FLOCKA,
  1418. .ana = &bcm2835_ana_default,
  1419. .min_rate = 600000000u,
  1420. .max_rate = 2400000000u,
  1421. .max_fb_rate = BCM2835_MAX_FB_RATE),
  1422. [BCM2835_PLLA_CORE] = REGISTER_PLL_DIV(
  1423. SOC_ALL,
  1424. .name = "plla_core",
  1425. .source_pll = "plla",
  1426. .cm_reg = CM_PLLA,
  1427. .a2w_reg = A2W_PLLA_CORE,
  1428. .load_mask = CM_PLLA_LOADCORE,
  1429. .hold_mask = CM_PLLA_HOLDCORE,
  1430. .fixed_divider = 1,
  1431. .flags = CLK_SET_RATE_PARENT),
  1432. [BCM2835_PLLA_PER] = REGISTER_PLL_DIV(
  1433. SOC_ALL,
  1434. .name = "plla_per",
  1435. .source_pll = "plla",
  1436. .cm_reg = CM_PLLA,
  1437. .a2w_reg = A2W_PLLA_PER,
  1438. .load_mask = CM_PLLA_LOADPER,
  1439. .hold_mask = CM_PLLA_HOLDPER,
  1440. .fixed_divider = 1,
  1441. .flags = CLK_SET_RATE_PARENT),
  1442. [BCM2835_PLLA_DSI0] = REGISTER_PLL_DIV(
  1443. SOC_ALL,
  1444. .name = "plla_dsi0",
  1445. .source_pll = "plla",
  1446. .cm_reg = CM_PLLA,
  1447. .a2w_reg = A2W_PLLA_DSI0,
  1448. .load_mask = CM_PLLA_LOADDSI0,
  1449. .hold_mask = CM_PLLA_HOLDDSI0,
  1450. .fixed_divider = 1),
  1451. [BCM2835_PLLA_CCP2] = REGISTER_PLL_DIV(
  1452. SOC_ALL,
  1453. .name = "plla_ccp2",
  1454. .source_pll = "plla",
  1455. .cm_reg = CM_PLLA,
  1456. .a2w_reg = A2W_PLLA_CCP2,
  1457. .load_mask = CM_PLLA_LOADCCP2,
  1458. .hold_mask = CM_PLLA_HOLDCCP2,
  1459. .fixed_divider = 1,
  1460. .flags = CLK_SET_RATE_PARENT),
  1461. /* PLLB is used for the ARM's clock. */
  1462. [BCM2835_PLLB] = REGISTER_PLL(
  1463. SOC_ALL,
  1464. .name = "pllb",
  1465. .cm_ctrl_reg = CM_PLLB,
  1466. .a2w_ctrl_reg = A2W_PLLB_CTRL,
  1467. .frac_reg = A2W_PLLB_FRAC,
  1468. .ana_reg_base = A2W_PLLB_ANA0,
  1469. .reference_enable_mask = A2W_XOSC_CTRL_PLLB_ENABLE,
  1470. .lock_mask = CM_LOCK_FLOCKB,
  1471. .ana = &bcm2835_ana_default,
  1472. .min_rate = 600000000u,
  1473. .max_rate = 3000000000u,
  1474. .max_fb_rate = BCM2835_MAX_FB_RATE,
  1475. .flags = CLK_GET_RATE_NOCACHE),
  1476. [BCM2835_PLLB_ARM] = REGISTER_PLL_DIV(
  1477. SOC_ALL,
  1478. .name = "pllb_arm",
  1479. .source_pll = "pllb",
  1480. .cm_reg = CM_PLLB,
  1481. .a2w_reg = A2W_PLLB_ARM,
  1482. .load_mask = CM_PLLB_LOADARM,
  1483. .hold_mask = CM_PLLB_HOLDARM,
  1484. .fixed_divider = 1,
  1485. .flags = CLK_SET_RATE_PARENT | CLK_GET_RATE_NOCACHE),
  1486. /*
  1487. * PLLC is the core PLL, used to drive the core VPU clock.
  1488. *
  1489. * It is in the PX LDO power domain, which is on when the
  1490. * AUDIO domain is on.
  1491. */
  1492. [BCM2835_PLLC] = REGISTER_PLL(
  1493. SOC_ALL,
  1494. .name = "pllc",
  1495. .cm_ctrl_reg = CM_PLLC,
  1496. .a2w_ctrl_reg = A2W_PLLC_CTRL,
  1497. .frac_reg = A2W_PLLC_FRAC,
  1498. .ana_reg_base = A2W_PLLC_ANA0,
  1499. .reference_enable_mask = A2W_XOSC_CTRL_PLLC_ENABLE,
  1500. .lock_mask = CM_LOCK_FLOCKC,
  1501. .ana = &bcm2835_ana_default,
  1502. .min_rate = 600000000u,
  1503. .max_rate = 3000000000u,
  1504. .max_fb_rate = BCM2835_MAX_FB_RATE),
  1505. [BCM2835_PLLC_CORE0] = REGISTER_PLL_DIV(
  1506. SOC_ALL,
  1507. .name = "pllc_core0",
  1508. .source_pll = "pllc",
  1509. .cm_reg = CM_PLLC,
  1510. .a2w_reg = A2W_PLLC_CORE0,
  1511. .load_mask = CM_PLLC_LOADCORE0,
  1512. .hold_mask = CM_PLLC_HOLDCORE0,
  1513. .fixed_divider = 1,
  1514. .flags = CLK_SET_RATE_PARENT),
  1515. [BCM2835_PLLC_CORE1] = REGISTER_PLL_DIV(
  1516. SOC_ALL,
  1517. .name = "pllc_core1",
  1518. .source_pll = "pllc",
  1519. .cm_reg = CM_PLLC,
  1520. .a2w_reg = A2W_PLLC_CORE1,
  1521. .load_mask = CM_PLLC_LOADCORE1,
  1522. .hold_mask = CM_PLLC_HOLDCORE1,
  1523. .fixed_divider = 1,
  1524. .flags = CLK_SET_RATE_PARENT),
  1525. [BCM2835_PLLC_CORE2] = REGISTER_PLL_DIV(
  1526. SOC_ALL,
  1527. .name = "pllc_core2",
  1528. .source_pll = "pllc",
  1529. .cm_reg = CM_PLLC,
  1530. .a2w_reg = A2W_PLLC_CORE2,
  1531. .load_mask = CM_PLLC_LOADCORE2,
  1532. .hold_mask = CM_PLLC_HOLDCORE2,
  1533. .fixed_divider = 1,
  1534. .flags = CLK_SET_RATE_PARENT),
  1535. [BCM2835_PLLC_PER] = REGISTER_PLL_DIV(
  1536. SOC_ALL,
  1537. .name = "pllc_per",
  1538. .source_pll = "pllc",
  1539. .cm_reg = CM_PLLC,
  1540. .a2w_reg = A2W_PLLC_PER,
  1541. .load_mask = CM_PLLC_LOADPER,
  1542. .hold_mask = CM_PLLC_HOLDPER,
  1543. .fixed_divider = 1,
  1544. .flags = CLK_IS_CRITICAL | CLK_SET_RATE_PARENT),
  1545. /*
  1546. * PLLD is the display PLL, used to drive DSI display panels.
  1547. *
  1548. * It is in the PX LDO power domain, which is on when the
  1549. * AUDIO domain is on.
  1550. */
  1551. [BCM2835_PLLD] = REGISTER_PLL(
  1552. SOC_ALL,
  1553. .name = "plld",
  1554. .cm_ctrl_reg = CM_PLLD,
  1555. .a2w_ctrl_reg = A2W_PLLD_CTRL,
  1556. .frac_reg = A2W_PLLD_FRAC,
  1557. .ana_reg_base = A2W_PLLD_ANA0,
  1558. .reference_enable_mask = A2W_XOSC_CTRL_DDR_ENABLE,
  1559. .lock_mask = CM_LOCK_FLOCKD,
  1560. .ana = &bcm2835_ana_default,
  1561. .min_rate = 600000000u,
  1562. .max_rate = 2400000000u,
  1563. .max_fb_rate = BCM2835_MAX_FB_RATE),
  1564. [BCM2835_PLLD_CORE] = REGISTER_PLL_DIV(
  1565. SOC_ALL,
  1566. .name = "plld_core",
  1567. .source_pll = "plld",
  1568. .cm_reg = CM_PLLD,
  1569. .a2w_reg = A2W_PLLD_CORE,
  1570. .load_mask = CM_PLLD_LOADCORE,
  1571. .hold_mask = CM_PLLD_HOLDCORE,
  1572. .fixed_divider = 1,
  1573. .flags = CLK_SET_RATE_PARENT),
  1574. /*
  1575. * VPU firmware assumes that PLLD_PER isn't disabled by the ARM core.
  1576. * Otherwise this could cause firmware lookups. That's why we mark
  1577. * it as critical.
  1578. */
  1579. [BCM2835_PLLD_PER] = REGISTER_PLL_DIV(
  1580. SOC_ALL,
  1581. .name = "plld_per",
  1582. .source_pll = "plld",
  1583. .cm_reg = CM_PLLD,
  1584. .a2w_reg = A2W_PLLD_PER,
  1585. .load_mask = CM_PLLD_LOADPER,
  1586. .hold_mask = CM_PLLD_HOLDPER,
  1587. .fixed_divider = 1,
  1588. .flags = CLK_IS_CRITICAL | CLK_SET_RATE_PARENT),
  1589. [BCM2835_PLLD_DSI0] = REGISTER_PLL_DIV(
  1590. SOC_ALL,
  1591. .name = "plld_dsi0",
  1592. .source_pll = "plld",
  1593. .cm_reg = CM_PLLD,
  1594. .a2w_reg = A2W_PLLD_DSI0,
  1595. .load_mask = CM_PLLD_LOADDSI0,
  1596. .hold_mask = CM_PLLD_HOLDDSI0,
  1597. .fixed_divider = 1),
  1598. [BCM2835_PLLD_DSI1] = REGISTER_PLL_DIV(
  1599. SOC_ALL,
  1600. .name = "plld_dsi1",
  1601. .source_pll = "plld",
  1602. .cm_reg = CM_PLLD,
  1603. .a2w_reg = A2W_PLLD_DSI1,
  1604. .load_mask = CM_PLLD_LOADDSI1,
  1605. .hold_mask = CM_PLLD_HOLDDSI1,
  1606. .fixed_divider = 1),
  1607. /*
  1608. * PLLH is used to supply the pixel clock or the AUX clock for the
  1609. * TV encoder.
  1610. *
  1611. * It is in the HDMI power domain.
  1612. */
  1613. [BCM2835_PLLH] = REGISTER_PLL(
  1614. SOC_BCM2835,
  1615. "pllh",
  1616. .cm_ctrl_reg = CM_PLLH,
  1617. .a2w_ctrl_reg = A2W_PLLH_CTRL,
  1618. .frac_reg = A2W_PLLH_FRAC,
  1619. .ana_reg_base = A2W_PLLH_ANA0,
  1620. .reference_enable_mask = A2W_XOSC_CTRL_PLLC_ENABLE,
  1621. .lock_mask = CM_LOCK_FLOCKH,
  1622. .ana = &bcm2835_ana_pllh,
  1623. .min_rate = 600000000u,
  1624. .max_rate = 3000000000u,
  1625. .max_fb_rate = BCM2835_MAX_FB_RATE),
  1626. [BCM2835_PLLH_RCAL] = REGISTER_PLL_DIV(
  1627. SOC_BCM2835,
  1628. .name = "pllh_rcal",
  1629. .source_pll = "pllh",
  1630. .cm_reg = CM_PLLH,
  1631. .a2w_reg = A2W_PLLH_RCAL,
  1632. .load_mask = CM_PLLH_LOADRCAL,
  1633. .hold_mask = 0,
  1634. .fixed_divider = 10,
  1635. .flags = CLK_SET_RATE_PARENT),
  1636. [BCM2835_PLLH_AUX] = REGISTER_PLL_DIV(
  1637. SOC_BCM2835,
  1638. .name = "pllh_aux",
  1639. .source_pll = "pllh",
  1640. .cm_reg = CM_PLLH,
  1641. .a2w_reg = A2W_PLLH_AUX,
  1642. .load_mask = CM_PLLH_LOADAUX,
  1643. .hold_mask = 0,
  1644. .fixed_divider = 1,
  1645. .flags = CLK_SET_RATE_PARENT),
  1646. [BCM2835_PLLH_PIX] = REGISTER_PLL_DIV(
  1647. SOC_BCM2835,
  1648. .name = "pllh_pix",
  1649. .source_pll = "pllh",
  1650. .cm_reg = CM_PLLH,
  1651. .a2w_reg = A2W_PLLH_PIX,
  1652. .load_mask = CM_PLLH_LOADPIX,
  1653. .hold_mask = 0,
  1654. .fixed_divider = 10,
  1655. .flags = CLK_SET_RATE_PARENT),
  1656. /* the clocks */
  1657. /* clocks with oscillator parent mux */
  1658. /* One Time Programmable Memory clock. Maximum 10Mhz. */
  1659. [BCM2835_CLOCK_OTP] = REGISTER_OSC_CLK(
  1660. SOC_ALL,
  1661. .name = "otp",
  1662. .ctl_reg = CM_OTPCTL,
  1663. .div_reg = CM_OTPDIV,
  1664. .int_bits = 4,
  1665. .frac_bits = 0,
  1666. .tcnt_mux = 6),
  1667. /*
  1668. * Used for a 1Mhz clock for the system clocksource, and also used
  1669. * bythe watchdog timer and the camera pulse generator.
  1670. */
  1671. [BCM2835_CLOCK_TIMER] = REGISTER_OSC_CLK(
  1672. SOC_ALL,
  1673. .name = "timer",
  1674. .ctl_reg = CM_TIMERCTL,
  1675. .div_reg = CM_TIMERDIV,
  1676. .int_bits = 6,
  1677. .frac_bits = 12),
  1678. /*
  1679. * Clock for the temperature sensor.
  1680. * Generally run at 2Mhz, max 5Mhz.
  1681. */
  1682. [BCM2835_CLOCK_TSENS] = REGISTER_OSC_CLK(
  1683. SOC_ALL,
  1684. .name = "tsens",
  1685. .ctl_reg = CM_TSENSCTL,
  1686. .div_reg = CM_TSENSDIV,
  1687. .int_bits = 5,
  1688. .frac_bits = 0),
  1689. [BCM2835_CLOCK_TEC] = REGISTER_OSC_CLK(
  1690. SOC_ALL,
  1691. .name = "tec",
  1692. .ctl_reg = CM_TECCTL,
  1693. .div_reg = CM_TECDIV,
  1694. .int_bits = 6,
  1695. .frac_bits = 0),
  1696. /* clocks with vpu parent mux */
  1697. [BCM2835_CLOCK_H264] = REGISTER_VPU_CLK(
  1698. SOC_ALL,
  1699. .name = "h264",
  1700. .ctl_reg = CM_H264CTL,
  1701. .div_reg = CM_H264DIV,
  1702. .int_bits = 4,
  1703. .frac_bits = 8,
  1704. .tcnt_mux = 1),
  1705. [BCM2835_CLOCK_ISP] = REGISTER_VPU_CLK(
  1706. SOC_ALL,
  1707. .name = "isp",
  1708. .ctl_reg = CM_ISPCTL,
  1709. .div_reg = CM_ISPDIV,
  1710. .int_bits = 4,
  1711. .frac_bits = 8,
  1712. .tcnt_mux = 2),
  1713. /*
  1714. * Secondary SDRAM clock. Used for low-voltage modes when the PLL
  1715. * in the SDRAM controller can't be used.
  1716. */
  1717. [BCM2835_CLOCK_SDRAM] = REGISTER_VPU_CLK(
  1718. SOC_ALL,
  1719. .name = "sdram",
  1720. .ctl_reg = CM_SDCCTL,
  1721. .div_reg = CM_SDCDIV,
  1722. .int_bits = 6,
  1723. .frac_bits = 0,
  1724. .tcnt_mux = 3),
  1725. [BCM2835_CLOCK_V3D] = REGISTER_VPU_CLK(
  1726. SOC_ALL,
  1727. .name = "v3d",
  1728. .ctl_reg = CM_V3DCTL,
  1729. .div_reg = CM_V3DDIV,
  1730. .int_bits = 4,
  1731. .frac_bits = 8,
  1732. .tcnt_mux = 4),
  1733. /*
  1734. * VPU clock. This doesn't have an enable bit, since it drives
  1735. * the bus for everything else, and is special so it doesn't need
  1736. * to be gated for rate changes. It is also known as "clk_audio"
  1737. * in various hardware documentation.
  1738. */
  1739. [BCM2835_CLOCK_VPU] = REGISTER_VPU_CLK(
  1740. SOC_ALL,
  1741. .name = "vpu",
  1742. .ctl_reg = CM_VPUCTL,
  1743. .div_reg = CM_VPUDIV,
  1744. .int_bits = 12,
  1745. .frac_bits = 8,
  1746. .flags = CLK_IS_CRITICAL,
  1747. .is_vpu_clock = true,
  1748. .tcnt_mux = 5),
  1749. /* clocks with per parent mux */
  1750. [BCM2835_CLOCK_AVEO] = REGISTER_PER_CLK(
  1751. SOC_ALL,
  1752. .name = "aveo",
  1753. .ctl_reg = CM_AVEOCTL,
  1754. .div_reg = CM_AVEODIV,
  1755. .int_bits = 4,
  1756. .frac_bits = 0,
  1757. .tcnt_mux = 38),
  1758. [BCM2835_CLOCK_CAM0] = REGISTER_PER_CLK(
  1759. SOC_ALL,
  1760. .name = "cam0",
  1761. .ctl_reg = CM_CAM0CTL,
  1762. .div_reg = CM_CAM0DIV,
  1763. .int_bits = 4,
  1764. .frac_bits = 8,
  1765. .tcnt_mux = 14),
  1766. [BCM2835_CLOCK_CAM1] = REGISTER_PER_CLK(
  1767. SOC_ALL,
  1768. .name = "cam1",
  1769. .ctl_reg = CM_CAM1CTL,
  1770. .div_reg = CM_CAM1DIV,
  1771. .int_bits = 4,
  1772. .frac_bits = 8,
  1773. .tcnt_mux = 15),
  1774. [BCM2835_CLOCK_DFT] = REGISTER_PER_CLK(
  1775. SOC_ALL,
  1776. .name = "dft",
  1777. .ctl_reg = CM_DFTCTL,
  1778. .div_reg = CM_DFTDIV,
  1779. .int_bits = 5,
  1780. .frac_bits = 0),
  1781. [BCM2835_CLOCK_DPI] = REGISTER_PER_CLK(
  1782. SOC_ALL,
  1783. .name = "dpi",
  1784. .ctl_reg = CM_DPICTL,
  1785. .div_reg = CM_DPIDIV,
  1786. .int_bits = 4,
  1787. .frac_bits = 8,
  1788. .tcnt_mux = 17),
  1789. /* Arasan EMMC clock */
  1790. [BCM2835_CLOCK_EMMC] = REGISTER_PER_CLK(
  1791. SOC_ALL,
  1792. .name = "emmc",
  1793. .ctl_reg = CM_EMMCCTL,
  1794. .div_reg = CM_EMMCDIV,
  1795. .int_bits = 4,
  1796. .frac_bits = 8,
  1797. .tcnt_mux = 39),
  1798. /* EMMC2 clock (only available for BCM2711) */
  1799. [BCM2711_CLOCK_EMMC2] = REGISTER_PER_CLK(
  1800. SOC_BCM2711,
  1801. .name = "emmc2",
  1802. .ctl_reg = CM_EMMC2CTL,
  1803. .div_reg = CM_EMMC2DIV,
  1804. .int_bits = 4,
  1805. .frac_bits = 8,
  1806. .tcnt_mux = 42),
  1807. /* General purpose (GPIO) clocks */
  1808. [BCM2835_CLOCK_GP0] = REGISTER_PER_CLK(
  1809. SOC_ALL,
  1810. .name = "gp0",
  1811. .ctl_reg = CM_GP0CTL,
  1812. .div_reg = CM_GP0DIV,
  1813. .int_bits = 12,
  1814. .frac_bits = 12,
  1815. .is_mash_clock = true,
  1816. .tcnt_mux = 20),
  1817. [BCM2835_CLOCK_GP1] = REGISTER_PER_CLK(
  1818. SOC_ALL,
  1819. .name = "gp1",
  1820. .ctl_reg = CM_GP1CTL,
  1821. .div_reg = CM_GP1DIV,
  1822. .int_bits = 12,
  1823. .frac_bits = 12,
  1824. .flags = CLK_IS_CRITICAL,
  1825. .is_mash_clock = true,
  1826. .tcnt_mux = 21),
  1827. [BCM2835_CLOCK_GP2] = REGISTER_PER_CLK(
  1828. SOC_ALL,
  1829. .name = "gp2",
  1830. .ctl_reg = CM_GP2CTL,
  1831. .div_reg = CM_GP2DIV,
  1832. .int_bits = 12,
  1833. .frac_bits = 12,
  1834. .flags = CLK_IS_CRITICAL),
  1835. /* HDMI state machine */
  1836. [BCM2835_CLOCK_HSM] = REGISTER_PER_CLK(
  1837. SOC_ALL,
  1838. .name = "hsm",
  1839. .ctl_reg = CM_HSMCTL,
  1840. .div_reg = CM_HSMDIV,
  1841. .int_bits = 4,
  1842. .frac_bits = 8,
  1843. .tcnt_mux = 22),
  1844. [BCM2835_CLOCK_PCM] = REGISTER_PCM_CLK(
  1845. SOC_ALL,
  1846. .name = "pcm",
  1847. .ctl_reg = CM_PCMCTL,
  1848. .div_reg = CM_PCMDIV,
  1849. .int_bits = 12,
  1850. .frac_bits = 12,
  1851. .is_mash_clock = true,
  1852. .low_jitter = true,
  1853. .tcnt_mux = 23),
  1854. [BCM2835_CLOCK_PWM] = REGISTER_PER_CLK(
  1855. SOC_ALL,
  1856. .name = "pwm",
  1857. .ctl_reg = CM_PWMCTL,
  1858. .div_reg = CM_PWMDIV,
  1859. .int_bits = 12,
  1860. .frac_bits = 12,
  1861. .is_mash_clock = true,
  1862. .tcnt_mux = 24),
  1863. [BCM2835_CLOCK_SLIM] = REGISTER_PER_CLK(
  1864. SOC_ALL,
  1865. .name = "slim",
  1866. .ctl_reg = CM_SLIMCTL,
  1867. .div_reg = CM_SLIMDIV,
  1868. .int_bits = 12,
  1869. .frac_bits = 12,
  1870. .is_mash_clock = true,
  1871. .tcnt_mux = 25),
  1872. [BCM2835_CLOCK_SMI] = REGISTER_PER_CLK(
  1873. SOC_ALL,
  1874. .name = "smi",
  1875. .ctl_reg = CM_SMICTL,
  1876. .div_reg = CM_SMIDIV,
  1877. .int_bits = 4,
  1878. .frac_bits = 8,
  1879. .tcnt_mux = 27),
  1880. [BCM2835_CLOCK_UART] = REGISTER_PER_CLK(
  1881. SOC_ALL,
  1882. .name = "uart",
  1883. .ctl_reg = CM_UARTCTL,
  1884. .div_reg = CM_UARTDIV,
  1885. .int_bits = 10,
  1886. .frac_bits = 12,
  1887. .tcnt_mux = 28,
  1888. .round_up = true),
  1889. /* TV encoder clock. Only operating frequency is 108Mhz. */
  1890. [BCM2835_CLOCK_VEC] = REGISTER_PER_CLK(
  1891. SOC_ALL,
  1892. .name = "vec",
  1893. .ctl_reg = CM_VECCTL,
  1894. .div_reg = CM_VECDIV,
  1895. .int_bits = 4,
  1896. .frac_bits = 0,
  1897. /*
  1898. * Allow rate change propagation only on PLLH_AUX which is
  1899. * assigned index 7 in the parent array.
  1900. */
  1901. .set_rate_parent = BIT(7),
  1902. .tcnt_mux = 29),
  1903. /* dsi clocks */
  1904. [BCM2835_CLOCK_DSI0E] = REGISTER_PER_CLK(
  1905. SOC_ALL,
  1906. .name = "dsi0e",
  1907. .ctl_reg = CM_DSI0ECTL,
  1908. .div_reg = CM_DSI0EDIV,
  1909. .int_bits = 4,
  1910. .frac_bits = 8,
  1911. .tcnt_mux = 18),
  1912. [BCM2835_CLOCK_DSI1E] = REGISTER_PER_CLK(
  1913. SOC_ALL,
  1914. .name = "dsi1e",
  1915. .ctl_reg = CM_DSI1ECTL,
  1916. .div_reg = CM_DSI1EDIV,
  1917. .int_bits = 4,
  1918. .frac_bits = 8,
  1919. .tcnt_mux = 19),
  1920. [BCM2835_CLOCK_DSI0P] = REGISTER_DSI0_CLK(
  1921. SOC_ALL,
  1922. .name = "dsi0p",
  1923. .ctl_reg = CM_DSI0PCTL,
  1924. .div_reg = CM_DSI0PDIV,
  1925. .int_bits = 0,
  1926. .frac_bits = 0,
  1927. .tcnt_mux = 12),
  1928. [BCM2835_CLOCK_DSI1P] = REGISTER_DSI1_CLK(
  1929. SOC_ALL,
  1930. .name = "dsi1p",
  1931. .ctl_reg = CM_DSI1PCTL,
  1932. .div_reg = CM_DSI1PDIV,
  1933. .int_bits = 0,
  1934. .frac_bits = 0,
  1935. .tcnt_mux = 13),
  1936. /* the gates */
  1937. /*
  1938. * CM_PERIICTL (and CM_PERIACTL, CM_SYSCTL and CM_VPUCTL if
  1939. * you have the debug bit set in the power manager, which we
  1940. * don't bother exposing) are individual gates off of the
  1941. * non-stop vpu clock.
  1942. */
  1943. [BCM2835_CLOCK_PERI_IMAGE] = REGISTER_GATE(
  1944. SOC_ALL,
  1945. .name = "peri_image",
  1946. .parent = "vpu",
  1947. .ctl_reg = CM_PERIICTL),
  1948. };
  1949. /*
  1950. * Permanently take a reference on the parent of the SDRAM clock.
  1951. *
  1952. * While the SDRAM is being driven by its dedicated PLL most of the
  1953. * time, there is a little loop running in the firmware that
  1954. * periodically switches the SDRAM to using our CM clock to do PVT
  1955. * recalibration, with the assumption that the previously configured
  1956. * SDRAM parent is still enabled and running.
  1957. */
  1958. static int bcm2835_mark_sdc_parent_critical(struct clk *sdc)
  1959. {
  1960. struct clk *parent = clk_get_parent(sdc);
  1961. if (IS_ERR(parent))
  1962. return PTR_ERR(parent);
  1963. return clk_prepare_enable(parent);
  1964. }
  1965. static int bcm2835_clk_probe(struct platform_device *pdev)
  1966. {
  1967. struct device *dev = &pdev->dev;
  1968. struct clk_hw **hws;
  1969. struct bcm2835_cprman *cprman;
  1970. const struct bcm2835_clk_desc *desc;
  1971. const size_t asize = ARRAY_SIZE(clk_desc_array);
  1972. const struct cprman_plat_data *pdata;
  1973. size_t i;
  1974. int ret;
  1975. pdata = of_device_get_match_data(&pdev->dev);
  1976. if (!pdata)
  1977. return -ENODEV;
  1978. cprman = devm_kzalloc(dev,
  1979. struct_size(cprman, onecell.hws, asize),
  1980. GFP_KERNEL);
  1981. if (!cprman)
  1982. return -ENOMEM;
  1983. spin_lock_init(&cprman->regs_lock);
  1984. cprman->dev = dev;
  1985. cprman->regs = devm_platform_ioremap_resource(pdev, 0);
  1986. if (IS_ERR(cprman->regs))
  1987. return PTR_ERR(cprman->regs);
  1988. memcpy(cprman->real_parent_names, cprman_parent_names,
  1989. sizeof(cprman_parent_names));
  1990. of_clk_parent_fill(dev->of_node, cprman->real_parent_names,
  1991. ARRAY_SIZE(cprman_parent_names));
  1992. /*
  1993. * Make sure the external oscillator has been registered.
  1994. *
  1995. * The other (DSI) clocks are not present on older device
  1996. * trees, which we still need to support for backwards
  1997. * compatibility.
  1998. */
  1999. if (!cprman->real_parent_names[0])
  2000. return -ENODEV;
  2001. platform_set_drvdata(pdev, cprman);
  2002. cprman->onecell.num = asize;
  2003. cprman->soc = pdata->soc;
  2004. hws = cprman->onecell.hws;
  2005. for (i = 0; i < asize; i++) {
  2006. desc = &clk_desc_array[i];
  2007. if (desc->clk_register && desc->data &&
  2008. (desc->supported & pdata->soc)) {
  2009. hws[i] = desc->clk_register(cprman, desc->data);
  2010. }
  2011. }
  2012. ret = bcm2835_mark_sdc_parent_critical(hws[BCM2835_CLOCK_SDRAM]->clk);
  2013. if (ret)
  2014. return ret;
  2015. return of_clk_add_hw_provider(dev->of_node, of_clk_hw_onecell_get,
  2016. &cprman->onecell);
  2017. }
  2018. static const struct cprman_plat_data cprman_bcm2835_plat_data = {
  2019. .soc = SOC_BCM2835,
  2020. };
  2021. static const struct cprman_plat_data cprman_bcm2711_plat_data = {
  2022. .soc = SOC_BCM2711,
  2023. };
  2024. static const struct of_device_id bcm2835_clk_of_match[] = {
  2025. { .compatible = "brcm,bcm2835-cprman", .data = &cprman_bcm2835_plat_data },
  2026. { .compatible = "brcm,bcm2711-cprman", .data = &cprman_bcm2711_plat_data },
  2027. {}
  2028. };
  2029. MODULE_DEVICE_TABLE(of, bcm2835_clk_of_match);
  2030. static struct platform_driver bcm2835_clk_driver = {
  2031. .driver = {
  2032. .name = "bcm2835-clk",
  2033. .of_match_table = bcm2835_clk_of_match,
  2034. },
  2035. .probe = bcm2835_clk_probe,
  2036. };
  2037. builtin_platform_driver(bcm2835_clk_driver);
  2038. MODULE_AUTHOR("Eric Anholt <[email protected]>");
  2039. MODULE_DESCRIPTION("BCM2835 clock driver");
  2040. MODULE_LICENSE("GPL");