clk-bcm2835.c 58 KB

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