clk-stm32f4.c 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Author: Daniel Thompson <daniel.thompson@linaro.org>
  4. *
  5. * Inspired by clk-asm9260.c .
  6. */
  7. #include <linux/bitfield.h>
  8. #include <linux/clk-provider.h>
  9. #include <linux/err.h>
  10. #include <linux/io.h>
  11. #include <linux/iopoll.h>
  12. #include <linux/ioport.h>
  13. #include <linux/slab.h>
  14. #include <linux/spinlock.h>
  15. #include <linux/of.h>
  16. #include <linux/of_address.h>
  17. #include <linux/regmap.h>
  18. #include <linux/mfd/syscon.h>
  19. /*
  20. * Include list of clocks which are not derived from system clock (SYSCLOCK)
  21. * The index of these clocks is the secondary index of DT bindings
  22. *
  23. */
  24. #include <dt-bindings/clock/stm32fx-clock.h>
  25. #define STM32F4_RCC_CR 0x00
  26. #define STM32F4_RCC_PLLCFGR 0x04
  27. #define STM32F4_RCC_CFGR 0x08
  28. #define STM32F4_RCC_AHB1ENR 0x30
  29. #define STM32F4_RCC_AHB2ENR 0x34
  30. #define STM32F4_RCC_AHB3ENR 0x38
  31. #define STM32F4_RCC_APB1ENR 0x40
  32. #define STM32F4_RCC_APB2ENR 0x44
  33. #define STM32F4_RCC_BDCR 0x70
  34. #define STM32F4_RCC_CSR 0x74
  35. #define STM32F4_RCC_SSCGR 0x80
  36. #define STM32F4_RCC_PLLI2SCFGR 0x84
  37. #define STM32F4_RCC_PLLSAICFGR 0x88
  38. #define STM32F4_RCC_DCKCFGR 0x8c
  39. #define STM32F7_RCC_DCKCFGR2 0x90
  40. #define STM32F4_RCC_PLLCFGR_N_MASK GENMASK(14, 6)
  41. #define STM32F4_RCC_SSCGR_SSCGEN BIT(31)
  42. #define STM32F4_RCC_SSCGR_SPREADSEL BIT(30)
  43. #define STM32F4_RCC_SSCGR_RESERVED_MASK GENMASK(29, 28)
  44. #define STM32F4_RCC_SSCGR_INCSTEP_MASK GENMASK(27, 13)
  45. #define STM32F4_RCC_SSCGR_MODPER_MASK GENMASK(12, 0)
  46. #define NONE -1
  47. #define NO_IDX NONE
  48. #define NO_MUX NONE
  49. #define NO_GATE NONE
  50. struct stm32f4_gate_data {
  51. u8 offset;
  52. u8 bit_idx;
  53. const char *name;
  54. const char *parent_name;
  55. unsigned long flags;
  56. };
  57. static const struct stm32f4_gate_data stm32f429_gates[] __initconst = {
  58. { STM32F4_RCC_AHB1ENR, 0, "gpioa", "ahb_div" },
  59. { STM32F4_RCC_AHB1ENR, 1, "gpiob", "ahb_div" },
  60. { STM32F4_RCC_AHB1ENR, 2, "gpioc", "ahb_div" },
  61. { STM32F4_RCC_AHB1ENR, 3, "gpiod", "ahb_div" },
  62. { STM32F4_RCC_AHB1ENR, 4, "gpioe", "ahb_div" },
  63. { STM32F4_RCC_AHB1ENR, 5, "gpiof", "ahb_div" },
  64. { STM32F4_RCC_AHB1ENR, 6, "gpiog", "ahb_div" },
  65. { STM32F4_RCC_AHB1ENR, 7, "gpioh", "ahb_div" },
  66. { STM32F4_RCC_AHB1ENR, 8, "gpioi", "ahb_div" },
  67. { STM32F4_RCC_AHB1ENR, 9, "gpioj", "ahb_div" },
  68. { STM32F4_RCC_AHB1ENR, 10, "gpiok", "ahb_div" },
  69. { STM32F4_RCC_AHB1ENR, 12, "crc", "ahb_div" },
  70. { STM32F4_RCC_AHB1ENR, 18, "bkpsra", "ahb_div" },
  71. { STM32F4_RCC_AHB1ENR, 20, "ccmdatam", "ahb_div" },
  72. { STM32F4_RCC_AHB1ENR, 21, "dma1", "ahb_div" },
  73. { STM32F4_RCC_AHB1ENR, 22, "dma2", "ahb_div" },
  74. { STM32F4_RCC_AHB1ENR, 23, "dma2d", "ahb_div" },
  75. { STM32F4_RCC_AHB1ENR, 25, "ethmac", "ahb_div" },
  76. { STM32F4_RCC_AHB1ENR, 26, "ethmactx", "ahb_div" },
  77. { STM32F4_RCC_AHB1ENR, 27, "ethmacrx", "ahb_div" },
  78. { STM32F4_RCC_AHB1ENR, 28, "ethmacptp", "ahb_div" },
  79. { STM32F4_RCC_AHB1ENR, 29, "otghs", "ahb_div" },
  80. { STM32F4_RCC_AHB1ENR, 30, "otghsulpi", "ahb_div" },
  81. { STM32F4_RCC_AHB2ENR, 0, "dcmi", "ahb_div" },
  82. { STM32F4_RCC_AHB2ENR, 4, "cryp", "ahb_div" },
  83. { STM32F4_RCC_AHB2ENR, 5, "hash", "ahb_div" },
  84. { STM32F4_RCC_AHB2ENR, 6, "rng", "pll48" },
  85. { STM32F4_RCC_AHB2ENR, 7, "otgfs", "pll48" },
  86. { STM32F4_RCC_AHB3ENR, 0, "fmc", "ahb_div",
  87. CLK_IGNORE_UNUSED },
  88. { STM32F4_RCC_APB1ENR, 0, "tim2", "apb1_mul" },
  89. { STM32F4_RCC_APB1ENR, 1, "tim3", "apb1_mul" },
  90. { STM32F4_RCC_APB1ENR, 2, "tim4", "apb1_mul" },
  91. { STM32F4_RCC_APB1ENR, 3, "tim5", "apb1_mul" },
  92. { STM32F4_RCC_APB1ENR, 4, "tim6", "apb1_mul" },
  93. { STM32F4_RCC_APB1ENR, 5, "tim7", "apb1_mul" },
  94. { STM32F4_RCC_APB1ENR, 6, "tim12", "apb1_mul" },
  95. { STM32F4_RCC_APB1ENR, 7, "tim13", "apb1_mul" },
  96. { STM32F4_RCC_APB1ENR, 8, "tim14", "apb1_mul" },
  97. { STM32F4_RCC_APB1ENR, 11, "wwdg", "apb1_div" },
  98. { STM32F4_RCC_APB1ENR, 14, "spi2", "apb1_div" },
  99. { STM32F4_RCC_APB1ENR, 15, "spi3", "apb1_div" },
  100. { STM32F4_RCC_APB1ENR, 17, "uart2", "apb1_div" },
  101. { STM32F4_RCC_APB1ENR, 18, "uart3", "apb1_div" },
  102. { STM32F4_RCC_APB1ENR, 19, "uart4", "apb1_div" },
  103. { STM32F4_RCC_APB1ENR, 20, "uart5", "apb1_div" },
  104. { STM32F4_RCC_APB1ENR, 21, "i2c1", "apb1_div" },
  105. { STM32F4_RCC_APB1ENR, 22, "i2c2", "apb1_div" },
  106. { STM32F4_RCC_APB1ENR, 23, "i2c3", "apb1_div" },
  107. { STM32F4_RCC_APB1ENR, 25, "can1", "apb1_div" },
  108. { STM32F4_RCC_APB1ENR, 26, "can2", "apb1_div" },
  109. { STM32F4_RCC_APB1ENR, 28, "pwr", "apb1_div" },
  110. { STM32F4_RCC_APB1ENR, 29, "dac", "apb1_div" },
  111. { STM32F4_RCC_APB1ENR, 30, "uart7", "apb1_div" },
  112. { STM32F4_RCC_APB1ENR, 31, "uart8", "apb1_div" },
  113. { STM32F4_RCC_APB2ENR, 0, "tim1", "apb2_mul" },
  114. { STM32F4_RCC_APB2ENR, 1, "tim8", "apb2_mul" },
  115. { STM32F4_RCC_APB2ENR, 4, "usart1", "apb2_div" },
  116. { STM32F4_RCC_APB2ENR, 5, "usart6", "apb2_div" },
  117. { STM32F4_RCC_APB2ENR, 8, "adc1", "apb2_div" },
  118. { STM32F4_RCC_APB2ENR, 9, "adc2", "apb2_div" },
  119. { STM32F4_RCC_APB2ENR, 10, "adc3", "apb2_div" },
  120. { STM32F4_RCC_APB2ENR, 11, "sdio", "pll48" },
  121. { STM32F4_RCC_APB2ENR, 12, "spi1", "apb2_div" },
  122. { STM32F4_RCC_APB2ENR, 13, "spi4", "apb2_div" },
  123. { STM32F4_RCC_APB2ENR, 14, "syscfg", "apb2_div" },
  124. { STM32F4_RCC_APB2ENR, 16, "tim9", "apb2_mul" },
  125. { STM32F4_RCC_APB2ENR, 17, "tim10", "apb2_mul" },
  126. { STM32F4_RCC_APB2ENR, 18, "tim11", "apb2_mul" },
  127. { STM32F4_RCC_APB2ENR, 20, "spi5", "apb2_div" },
  128. { STM32F4_RCC_APB2ENR, 21, "spi6", "apb2_div" },
  129. { STM32F4_RCC_APB2ENR, 22, "sai1", "apb2_div" },
  130. };
  131. static const struct stm32f4_gate_data stm32f469_gates[] __initconst = {
  132. { STM32F4_RCC_AHB1ENR, 0, "gpioa", "ahb_div" },
  133. { STM32F4_RCC_AHB1ENR, 1, "gpiob", "ahb_div" },
  134. { STM32F4_RCC_AHB1ENR, 2, "gpioc", "ahb_div" },
  135. { STM32F4_RCC_AHB1ENR, 3, "gpiod", "ahb_div" },
  136. { STM32F4_RCC_AHB1ENR, 4, "gpioe", "ahb_div" },
  137. { STM32F4_RCC_AHB1ENR, 5, "gpiof", "ahb_div" },
  138. { STM32F4_RCC_AHB1ENR, 6, "gpiog", "ahb_div" },
  139. { STM32F4_RCC_AHB1ENR, 7, "gpioh", "ahb_div" },
  140. { STM32F4_RCC_AHB1ENR, 8, "gpioi", "ahb_div" },
  141. { STM32F4_RCC_AHB1ENR, 9, "gpioj", "ahb_div" },
  142. { STM32F4_RCC_AHB1ENR, 10, "gpiok", "ahb_div" },
  143. { STM32F4_RCC_AHB1ENR, 12, "crc", "ahb_div" },
  144. { STM32F4_RCC_AHB1ENR, 18, "bkpsra", "ahb_div" },
  145. { STM32F4_RCC_AHB1ENR, 20, "ccmdatam", "ahb_div" },
  146. { STM32F4_RCC_AHB1ENR, 21, "dma1", "ahb_div" },
  147. { STM32F4_RCC_AHB1ENR, 22, "dma2", "ahb_div" },
  148. { STM32F4_RCC_AHB1ENR, 23, "dma2d", "ahb_div" },
  149. { STM32F4_RCC_AHB1ENR, 25, "ethmac", "ahb_div" },
  150. { STM32F4_RCC_AHB1ENR, 26, "ethmactx", "ahb_div" },
  151. { STM32F4_RCC_AHB1ENR, 27, "ethmacrx", "ahb_div" },
  152. { STM32F4_RCC_AHB1ENR, 28, "ethmacptp", "ahb_div" },
  153. { STM32F4_RCC_AHB1ENR, 29, "otghs", "ahb_div" },
  154. { STM32F4_RCC_AHB1ENR, 30, "otghsulpi", "ahb_div" },
  155. { STM32F4_RCC_AHB2ENR, 0, "dcmi", "ahb_div" },
  156. { STM32F4_RCC_AHB2ENR, 4, "cryp", "ahb_div" },
  157. { STM32F4_RCC_AHB2ENR, 5, "hash", "ahb_div" },
  158. { STM32F4_RCC_AHB2ENR, 6, "rng", "pll48" },
  159. { STM32F4_RCC_AHB2ENR, 7, "otgfs", "pll48" },
  160. { STM32F4_RCC_AHB3ENR, 0, "fmc", "ahb_div",
  161. CLK_IGNORE_UNUSED },
  162. { STM32F4_RCC_AHB3ENR, 1, "qspi", "ahb_div",
  163. CLK_IGNORE_UNUSED },
  164. { STM32F4_RCC_APB1ENR, 0, "tim2", "apb1_mul" },
  165. { STM32F4_RCC_APB1ENR, 1, "tim3", "apb1_mul" },
  166. { STM32F4_RCC_APB1ENR, 2, "tim4", "apb1_mul" },
  167. { STM32F4_RCC_APB1ENR, 3, "tim5", "apb1_mul" },
  168. { STM32F4_RCC_APB1ENR, 4, "tim6", "apb1_mul" },
  169. { STM32F4_RCC_APB1ENR, 5, "tim7", "apb1_mul" },
  170. { STM32F4_RCC_APB1ENR, 6, "tim12", "apb1_mul" },
  171. { STM32F4_RCC_APB1ENR, 7, "tim13", "apb1_mul" },
  172. { STM32F4_RCC_APB1ENR, 8, "tim14", "apb1_mul" },
  173. { STM32F4_RCC_APB1ENR, 11, "wwdg", "apb1_div" },
  174. { STM32F4_RCC_APB1ENR, 14, "spi2", "apb1_div" },
  175. { STM32F4_RCC_APB1ENR, 15, "spi3", "apb1_div" },
  176. { STM32F4_RCC_APB1ENR, 17, "uart2", "apb1_div" },
  177. { STM32F4_RCC_APB1ENR, 18, "uart3", "apb1_div" },
  178. { STM32F4_RCC_APB1ENR, 19, "uart4", "apb1_div" },
  179. { STM32F4_RCC_APB1ENR, 20, "uart5", "apb1_div" },
  180. { STM32F4_RCC_APB1ENR, 21, "i2c1", "apb1_div" },
  181. { STM32F4_RCC_APB1ENR, 22, "i2c2", "apb1_div" },
  182. { STM32F4_RCC_APB1ENR, 23, "i2c3", "apb1_div" },
  183. { STM32F4_RCC_APB1ENR, 25, "can1", "apb1_div" },
  184. { STM32F4_RCC_APB1ENR, 26, "can2", "apb1_div" },
  185. { STM32F4_RCC_APB1ENR, 28, "pwr", "apb1_div" },
  186. { STM32F4_RCC_APB1ENR, 29, "dac", "apb1_div" },
  187. { STM32F4_RCC_APB1ENR, 30, "uart7", "apb1_div" },
  188. { STM32F4_RCC_APB1ENR, 31, "uart8", "apb1_div" },
  189. { STM32F4_RCC_APB2ENR, 0, "tim1", "apb2_mul" },
  190. { STM32F4_RCC_APB2ENR, 1, "tim8", "apb2_mul" },
  191. { STM32F4_RCC_APB2ENR, 4, "usart1", "apb2_div" },
  192. { STM32F4_RCC_APB2ENR, 5, "usart6", "apb2_div" },
  193. { STM32F4_RCC_APB2ENR, 8, "adc1", "apb2_div" },
  194. { STM32F4_RCC_APB2ENR, 9, "adc2", "apb2_div" },
  195. { STM32F4_RCC_APB2ENR, 10, "adc3", "apb2_div" },
  196. { STM32F4_RCC_APB2ENR, 11, "sdio", "sdmux" },
  197. { STM32F4_RCC_APB2ENR, 12, "spi1", "apb2_div" },
  198. { STM32F4_RCC_APB2ENR, 13, "spi4", "apb2_div" },
  199. { STM32F4_RCC_APB2ENR, 14, "syscfg", "apb2_div" },
  200. { STM32F4_RCC_APB2ENR, 16, "tim9", "apb2_mul" },
  201. { STM32F4_RCC_APB2ENR, 17, "tim10", "apb2_mul" },
  202. { STM32F4_RCC_APB2ENR, 18, "tim11", "apb2_mul" },
  203. { STM32F4_RCC_APB2ENR, 20, "spi5", "apb2_div" },
  204. { STM32F4_RCC_APB2ENR, 21, "spi6", "apb2_div" },
  205. { STM32F4_RCC_APB2ENR, 22, "sai1", "apb2_div" },
  206. };
  207. static const struct stm32f4_gate_data stm32f746_gates[] __initconst = {
  208. { STM32F4_RCC_AHB1ENR, 0, "gpioa", "ahb_div" },
  209. { STM32F4_RCC_AHB1ENR, 1, "gpiob", "ahb_div" },
  210. { STM32F4_RCC_AHB1ENR, 2, "gpioc", "ahb_div" },
  211. { STM32F4_RCC_AHB1ENR, 3, "gpiod", "ahb_div" },
  212. { STM32F4_RCC_AHB1ENR, 4, "gpioe", "ahb_div" },
  213. { STM32F4_RCC_AHB1ENR, 5, "gpiof", "ahb_div" },
  214. { STM32F4_RCC_AHB1ENR, 6, "gpiog", "ahb_div" },
  215. { STM32F4_RCC_AHB1ENR, 7, "gpioh", "ahb_div" },
  216. { STM32F4_RCC_AHB1ENR, 8, "gpioi", "ahb_div" },
  217. { STM32F4_RCC_AHB1ENR, 9, "gpioj", "ahb_div" },
  218. { STM32F4_RCC_AHB1ENR, 10, "gpiok", "ahb_div" },
  219. { STM32F4_RCC_AHB1ENR, 12, "crc", "ahb_div" },
  220. { STM32F4_RCC_AHB1ENR, 18, "bkpsra", "ahb_div" },
  221. { STM32F4_RCC_AHB1ENR, 20, "dtcmram", "ahb_div" },
  222. { STM32F4_RCC_AHB1ENR, 21, "dma1", "ahb_div" },
  223. { STM32F4_RCC_AHB1ENR, 22, "dma2", "ahb_div" },
  224. { STM32F4_RCC_AHB1ENR, 23, "dma2d", "ahb_div" },
  225. { STM32F4_RCC_AHB1ENR, 25, "ethmac", "ahb_div" },
  226. { STM32F4_RCC_AHB1ENR, 26, "ethmactx", "ahb_div" },
  227. { STM32F4_RCC_AHB1ENR, 27, "ethmacrx", "ahb_div" },
  228. { STM32F4_RCC_AHB1ENR, 28, "ethmacptp", "ahb_div" },
  229. { STM32F4_RCC_AHB1ENR, 29, "otghs", "ahb_div" },
  230. { STM32F4_RCC_AHB1ENR, 30, "otghsulpi", "ahb_div" },
  231. { STM32F4_RCC_AHB2ENR, 0, "dcmi", "ahb_div" },
  232. { STM32F4_RCC_AHB2ENR, 4, "cryp", "ahb_div" },
  233. { STM32F4_RCC_AHB2ENR, 5, "hash", "ahb_div" },
  234. { STM32F4_RCC_AHB2ENR, 6, "rng", "pll48" },
  235. { STM32F4_RCC_AHB2ENR, 7, "otgfs", "pll48" },
  236. { STM32F4_RCC_AHB3ENR, 0, "fmc", "ahb_div",
  237. CLK_IGNORE_UNUSED },
  238. { STM32F4_RCC_AHB3ENR, 1, "qspi", "ahb_div",
  239. CLK_IGNORE_UNUSED },
  240. { STM32F4_RCC_APB1ENR, 0, "tim2", "apb1_mul" },
  241. { STM32F4_RCC_APB1ENR, 1, "tim3", "apb1_mul" },
  242. { STM32F4_RCC_APB1ENR, 2, "tim4", "apb1_mul" },
  243. { STM32F4_RCC_APB1ENR, 3, "tim5", "apb1_mul" },
  244. { STM32F4_RCC_APB1ENR, 4, "tim6", "apb1_mul" },
  245. { STM32F4_RCC_APB1ENR, 5, "tim7", "apb1_mul" },
  246. { STM32F4_RCC_APB1ENR, 6, "tim12", "apb1_mul" },
  247. { STM32F4_RCC_APB1ENR, 7, "tim13", "apb1_mul" },
  248. { STM32F4_RCC_APB1ENR, 8, "tim14", "apb1_mul" },
  249. { STM32F4_RCC_APB1ENR, 11, "wwdg", "apb1_div" },
  250. { STM32F4_RCC_APB1ENR, 14, "spi2", "apb1_div" },
  251. { STM32F4_RCC_APB1ENR, 15, "spi3", "apb1_div" },
  252. { STM32F4_RCC_APB1ENR, 16, "spdifrx", "apb1_div" },
  253. { STM32F4_RCC_APB1ENR, 25, "can1", "apb1_div" },
  254. { STM32F4_RCC_APB1ENR, 26, "can2", "apb1_div" },
  255. { STM32F4_RCC_APB1ENR, 27, "cec", "apb1_div" },
  256. { STM32F4_RCC_APB1ENR, 28, "pwr", "apb1_div" },
  257. { STM32F4_RCC_APB1ENR, 29, "dac", "apb1_div" },
  258. { STM32F4_RCC_APB2ENR, 0, "tim1", "apb2_mul" },
  259. { STM32F4_RCC_APB2ENR, 1, "tim8", "apb2_mul" },
  260. { STM32F4_RCC_APB2ENR, 7, "sdmmc2", "sdmux" },
  261. { STM32F4_RCC_APB2ENR, 8, "adc1", "apb2_div" },
  262. { STM32F4_RCC_APB2ENR, 9, "adc2", "apb2_div" },
  263. { STM32F4_RCC_APB2ENR, 10, "adc3", "apb2_div" },
  264. { STM32F4_RCC_APB2ENR, 11, "sdmmc", "sdmux" },
  265. { STM32F4_RCC_APB2ENR, 12, "spi1", "apb2_div" },
  266. { STM32F4_RCC_APB2ENR, 13, "spi4", "apb2_div" },
  267. { STM32F4_RCC_APB2ENR, 14, "syscfg", "apb2_div" },
  268. { STM32F4_RCC_APB2ENR, 16, "tim9", "apb2_mul" },
  269. { STM32F4_RCC_APB2ENR, 17, "tim10", "apb2_mul" },
  270. { STM32F4_RCC_APB2ENR, 18, "tim11", "apb2_mul" },
  271. { STM32F4_RCC_APB2ENR, 20, "spi5", "apb2_div" },
  272. { STM32F4_RCC_APB2ENR, 21, "spi6", "apb2_div" },
  273. { STM32F4_RCC_APB2ENR, 22, "sai1", "apb2_div" },
  274. { STM32F4_RCC_APB2ENR, 23, "sai2", "apb2_div" },
  275. };
  276. static const struct stm32f4_gate_data stm32f769_gates[] __initconst = {
  277. { STM32F4_RCC_AHB1ENR, 0, "gpioa", "ahb_div" },
  278. { STM32F4_RCC_AHB1ENR, 1, "gpiob", "ahb_div" },
  279. { STM32F4_RCC_AHB1ENR, 2, "gpioc", "ahb_div" },
  280. { STM32F4_RCC_AHB1ENR, 3, "gpiod", "ahb_div" },
  281. { STM32F4_RCC_AHB1ENR, 4, "gpioe", "ahb_div" },
  282. { STM32F4_RCC_AHB1ENR, 5, "gpiof", "ahb_div" },
  283. { STM32F4_RCC_AHB1ENR, 6, "gpiog", "ahb_div" },
  284. { STM32F4_RCC_AHB1ENR, 7, "gpioh", "ahb_div" },
  285. { STM32F4_RCC_AHB1ENR, 8, "gpioi", "ahb_div" },
  286. { STM32F4_RCC_AHB1ENR, 9, "gpioj", "ahb_div" },
  287. { STM32F4_RCC_AHB1ENR, 10, "gpiok", "ahb_div" },
  288. { STM32F4_RCC_AHB1ENR, 12, "crc", "ahb_div" },
  289. { STM32F4_RCC_AHB1ENR, 18, "bkpsra", "ahb_div" },
  290. { STM32F4_RCC_AHB1ENR, 20, "dtcmram", "ahb_div" },
  291. { STM32F4_RCC_AHB1ENR, 21, "dma1", "ahb_div" },
  292. { STM32F4_RCC_AHB1ENR, 22, "dma2", "ahb_div" },
  293. { STM32F4_RCC_AHB1ENR, 23, "dma2d", "ahb_div" },
  294. { STM32F4_RCC_AHB1ENR, 25, "ethmac", "ahb_div" },
  295. { STM32F4_RCC_AHB1ENR, 26, "ethmactx", "ahb_div" },
  296. { STM32F4_RCC_AHB1ENR, 27, "ethmacrx", "ahb_div" },
  297. { STM32F4_RCC_AHB1ENR, 28, "ethmacptp", "ahb_div" },
  298. { STM32F4_RCC_AHB1ENR, 29, "otghs", "ahb_div" },
  299. { STM32F4_RCC_AHB1ENR, 30, "otghsulpi", "ahb_div" },
  300. { STM32F4_RCC_AHB2ENR, 0, "dcmi", "ahb_div" },
  301. { STM32F4_RCC_AHB2ENR, 1, "jpeg", "ahb_div" },
  302. { STM32F4_RCC_AHB2ENR, 4, "cryp", "ahb_div" },
  303. { STM32F4_RCC_AHB2ENR, 5, "hash", "ahb_div" },
  304. { STM32F4_RCC_AHB2ENR, 6, "rng", "pll48" },
  305. { STM32F4_RCC_AHB2ENR, 7, "otgfs", "pll48" },
  306. { STM32F4_RCC_AHB3ENR, 0, "fmc", "ahb_div",
  307. CLK_IGNORE_UNUSED },
  308. { STM32F4_RCC_AHB3ENR, 1, "qspi", "ahb_div",
  309. CLK_IGNORE_UNUSED },
  310. { STM32F4_RCC_APB1ENR, 0, "tim2", "apb1_mul" },
  311. { STM32F4_RCC_APB1ENR, 1, "tim3", "apb1_mul" },
  312. { STM32F4_RCC_APB1ENR, 2, "tim4", "apb1_mul" },
  313. { STM32F4_RCC_APB1ENR, 3, "tim5", "apb1_mul" },
  314. { STM32F4_RCC_APB1ENR, 4, "tim6", "apb1_mul" },
  315. { STM32F4_RCC_APB1ENR, 5, "tim7", "apb1_mul" },
  316. { STM32F4_RCC_APB1ENR, 6, "tim12", "apb1_mul" },
  317. { STM32F4_RCC_APB1ENR, 7, "tim13", "apb1_mul" },
  318. { STM32F4_RCC_APB1ENR, 8, "tim14", "apb1_mul" },
  319. { STM32F4_RCC_APB1ENR, 10, "rtcapb", "apb1_mul" },
  320. { STM32F4_RCC_APB1ENR, 11, "wwdg", "apb1_div" },
  321. { STM32F4_RCC_APB1ENR, 13, "can3", "apb1_div" },
  322. { STM32F4_RCC_APB1ENR, 14, "spi2", "apb1_div" },
  323. { STM32F4_RCC_APB1ENR, 15, "spi3", "apb1_div" },
  324. { STM32F4_RCC_APB1ENR, 16, "spdifrx", "apb1_div" },
  325. { STM32F4_RCC_APB1ENR, 25, "can1", "apb1_div" },
  326. { STM32F4_RCC_APB1ENR, 26, "can2", "apb1_div" },
  327. { STM32F4_RCC_APB1ENR, 27, "cec", "apb1_div" },
  328. { STM32F4_RCC_APB1ENR, 28, "pwr", "apb1_div" },
  329. { STM32F4_RCC_APB1ENR, 29, "dac", "apb1_div" },
  330. { STM32F4_RCC_APB2ENR, 0, "tim1", "apb2_mul" },
  331. { STM32F4_RCC_APB2ENR, 1, "tim8", "apb2_mul" },
  332. { STM32F4_RCC_APB2ENR, 7, "sdmmc2", "sdmux2" },
  333. { STM32F4_RCC_APB2ENR, 8, "adc1", "apb2_div" },
  334. { STM32F4_RCC_APB2ENR, 9, "adc2", "apb2_div" },
  335. { STM32F4_RCC_APB2ENR, 10, "adc3", "apb2_div" },
  336. { STM32F4_RCC_APB2ENR, 11, "sdmmc1", "sdmux1" },
  337. { STM32F4_RCC_APB2ENR, 12, "spi1", "apb2_div" },
  338. { STM32F4_RCC_APB2ENR, 13, "spi4", "apb2_div" },
  339. { STM32F4_RCC_APB2ENR, 14, "syscfg", "apb2_div" },
  340. { STM32F4_RCC_APB2ENR, 16, "tim9", "apb2_mul" },
  341. { STM32F4_RCC_APB2ENR, 17, "tim10", "apb2_mul" },
  342. { STM32F4_RCC_APB2ENR, 18, "tim11", "apb2_mul" },
  343. { STM32F4_RCC_APB2ENR, 20, "spi5", "apb2_div" },
  344. { STM32F4_RCC_APB2ENR, 21, "spi6", "apb2_div" },
  345. { STM32F4_RCC_APB2ENR, 22, "sai1", "apb2_div" },
  346. { STM32F4_RCC_APB2ENR, 23, "sai2", "apb2_div" },
  347. { STM32F4_RCC_APB2ENR, 30, "mdio", "apb2_div" },
  348. };
  349. enum stm32f4_pll_ssc_mod_type {
  350. STM32F4_PLL_SSC_CENTER_SPREAD,
  351. STM32F4_PLL_SSC_DOWN_SPREAD,
  352. };
  353. static const char * const stm32f4_ssc_mod_methods[] __initconst = {
  354. [STM32F4_PLL_SSC_DOWN_SPREAD] = "down-spread",
  355. [STM32F4_PLL_SSC_CENTER_SPREAD] = "center-spread",
  356. };
  357. /*
  358. * This bitmask tells us which bit offsets (0..192) on STM32F4[23]xxx
  359. * have gate bits associated with them. Its combined hweight is 71.
  360. */
  361. #define MAX_GATE_MAP 3
  362. static const u64 stm32f42xx_gate_map[MAX_GATE_MAP] = { 0x000000f17ef417ffull,
  363. 0x0000000000000001ull,
  364. 0x04777f33f6fec9ffull };
  365. static const u64 stm32f46xx_gate_map[MAX_GATE_MAP] = { 0x000000f17ef417ffull,
  366. 0x0000000000000003ull,
  367. 0x0c777f33f6fec9ffull };
  368. static const u64 stm32f746_gate_map[MAX_GATE_MAP] = { 0x000000f17ef417ffull,
  369. 0x0000000000000003ull,
  370. 0x04f77f833e01c9ffull };
  371. static const u64 stm32f769_gate_map[MAX_GATE_MAP] = { 0x000000f37ef417ffull,
  372. 0x0000000000000003ull,
  373. 0x44F77F833E01EDFFull };
  374. static const u64 *stm32f4_gate_map;
  375. static struct clk_hw **clks;
  376. static DEFINE_SPINLOCK(stm32f4_clk_lock);
  377. static void __iomem *base;
  378. static struct regmap *pdrm;
  379. static int stm32fx_end_primary_clk;
  380. /*
  381. * "Multiplier" device for APBx clocks.
  382. *
  383. * The APBx dividers are power-of-two dividers and, if *not* running in 1:1
  384. * mode, they also tap out the one of the low order state bits to run the
  385. * timers. ST datasheets represent this feature as a (conditional) clock
  386. * multiplier.
  387. */
  388. struct clk_apb_mul {
  389. struct clk_hw hw;
  390. u8 bit_idx;
  391. };
  392. #define to_clk_apb_mul(_hw) container_of(_hw, struct clk_apb_mul, hw)
  393. static unsigned long clk_apb_mul_recalc_rate(struct clk_hw *hw,
  394. unsigned long parent_rate)
  395. {
  396. struct clk_apb_mul *am = to_clk_apb_mul(hw);
  397. if (readl(base + STM32F4_RCC_CFGR) & BIT(am->bit_idx))
  398. return parent_rate * 2;
  399. return parent_rate;
  400. }
  401. static int clk_apb_mul_determine_rate(struct clk_hw *hw,
  402. struct clk_rate_request *req)
  403. {
  404. struct clk_apb_mul *am = to_clk_apb_mul(hw);
  405. unsigned long mult = 1;
  406. if (readl(base + STM32F4_RCC_CFGR) & BIT(am->bit_idx))
  407. mult = 2;
  408. if (clk_hw_get_flags(hw) & CLK_SET_RATE_PARENT) {
  409. unsigned long best_parent = req->rate / mult;
  410. req->best_parent_rate = clk_hw_round_rate(clk_hw_get_parent(hw), best_parent);
  411. }
  412. req->rate = req->best_parent_rate * mult;
  413. return 0;
  414. }
  415. static int clk_apb_mul_set_rate(struct clk_hw *hw, unsigned long rate,
  416. unsigned long parent_rate)
  417. {
  418. /*
  419. * We must report success but we can do so unconditionally because
  420. * clk_apb_mul_round_rate returns values that ensure this call is a
  421. * nop.
  422. */
  423. return 0;
  424. }
  425. static const struct clk_ops clk_apb_mul_factor_ops = {
  426. .determine_rate = clk_apb_mul_determine_rate,
  427. .set_rate = clk_apb_mul_set_rate,
  428. .recalc_rate = clk_apb_mul_recalc_rate,
  429. };
  430. static struct clk *clk_register_apb_mul(struct device *dev, const char *name,
  431. const char *parent_name,
  432. unsigned long flags, u8 bit_idx)
  433. {
  434. struct clk_apb_mul *am;
  435. struct clk_init_data init;
  436. struct clk *clk;
  437. am = kzalloc_obj(*am);
  438. if (!am)
  439. return ERR_PTR(-ENOMEM);
  440. am->bit_idx = bit_idx;
  441. am->hw.init = &init;
  442. init.name = name;
  443. init.ops = &clk_apb_mul_factor_ops;
  444. init.flags = flags;
  445. init.parent_names = &parent_name;
  446. init.num_parents = 1;
  447. clk = clk_register(dev, &am->hw);
  448. if (IS_ERR(clk))
  449. kfree(am);
  450. return clk;
  451. }
  452. enum {
  453. PLL,
  454. PLL_I2S,
  455. PLL_SAI,
  456. };
  457. static const struct clk_div_table pll_divp_table[] = {
  458. { 0, 2 }, { 1, 4 }, { 2, 6 }, { 3, 8 }, { 0 }
  459. };
  460. static const struct clk_div_table pll_divq_table[] = {
  461. { 2, 2 }, { 3, 3 }, { 4, 4 }, { 5, 5 }, { 6, 6 }, { 7, 7 },
  462. { 8, 8 }, { 9, 9 }, { 10, 10 }, { 11, 11 }, { 12, 12 }, { 13, 13 },
  463. { 14, 14 }, { 15, 15 },
  464. { 0 }
  465. };
  466. static const struct clk_div_table pll_divr_table[] = {
  467. { 2, 2 }, { 3, 3 }, { 4, 4 }, { 5, 5 }, { 6, 6 }, { 7, 7 }, { 0 }
  468. };
  469. struct stm32f4_pll_ssc {
  470. unsigned int mod_freq;
  471. unsigned int mod_depth;
  472. enum stm32f4_pll_ssc_mod_type mod_type;
  473. };
  474. struct stm32f4_pll {
  475. spinlock_t *lock;
  476. struct clk_gate gate;
  477. u8 offset;
  478. u8 bit_rdy_idx;
  479. u8 status;
  480. u8 n_start;
  481. bool ssc_enable;
  482. struct stm32f4_pll_ssc ssc_conf;
  483. };
  484. #define to_stm32f4_pll(_gate) container_of(_gate, struct stm32f4_pll, gate)
  485. struct stm32f4_pll_post_div_data {
  486. int idx;
  487. int pll_idx;
  488. const char *name;
  489. const char *parent;
  490. u8 flag;
  491. u8 offset;
  492. u8 shift;
  493. u8 width;
  494. u8 flag_div;
  495. const struct clk_div_table *div_table;
  496. };
  497. struct stm32f4_vco_data {
  498. const char *vco_name;
  499. u8 offset;
  500. u8 bit_idx;
  501. u8 bit_rdy_idx;
  502. bool sscg;
  503. };
  504. static const struct stm32f4_vco_data vco_data[] = {
  505. { "vco", STM32F4_RCC_PLLCFGR, 24, 25 },
  506. { "vco-i2s", STM32F4_RCC_PLLI2SCFGR, 26, 27 },
  507. { "vco-sai", STM32F4_RCC_PLLSAICFGR, 28, 29 },
  508. };
  509. static const struct clk_div_table post_divr_table[] = {
  510. { 0, 2 }, { 1, 4 }, { 2, 8 }, { 3, 16 }, { 0 }
  511. };
  512. #define MAX_POST_DIV 3
  513. static const struct stm32f4_pll_post_div_data post_div_data[MAX_POST_DIV] = {
  514. { CLK_I2SQ_PDIV, PLL_VCO_I2S, "plli2s-q-div", "plli2s-q",
  515. CLK_SET_RATE_PARENT, STM32F4_RCC_DCKCFGR, 0, 5, 0, NULL},
  516. { CLK_SAIQ_PDIV, PLL_VCO_SAI, "pllsai-q-div", "pllsai-q",
  517. CLK_SET_RATE_PARENT, STM32F4_RCC_DCKCFGR, 8, 5, 0, NULL },
  518. { NO_IDX, PLL_VCO_SAI, "pllsai-r-div", "pllsai-r", CLK_SET_RATE_PARENT,
  519. STM32F4_RCC_DCKCFGR, 16, 2, 0, post_divr_table },
  520. };
  521. struct stm32f4_div_data {
  522. u8 shift;
  523. u8 width;
  524. u8 flag_div;
  525. const struct clk_div_table *div_table;
  526. };
  527. #define MAX_PLL_DIV 3
  528. static const struct stm32f4_div_data div_data[MAX_PLL_DIV] = {
  529. { 16, 2, 0, pll_divp_table },
  530. { 24, 4, 0, pll_divq_table },
  531. { 28, 3, 0, pll_divr_table },
  532. };
  533. struct stm32f4_pll_data {
  534. u8 pll_num;
  535. u8 n_start;
  536. const char *div_name[MAX_PLL_DIV];
  537. };
  538. static const struct stm32f4_pll_data stm32f429_pll[MAX_PLL_DIV] = {
  539. { PLL, 192, { "pll", "pll48", NULL } },
  540. { PLL_I2S, 192, { NULL, "plli2s-q", "plli2s-r" } },
  541. { PLL_SAI, 49, { NULL, "pllsai-q", "pllsai-r" } },
  542. };
  543. static const struct stm32f4_pll_data stm32f469_pll[MAX_PLL_DIV] = {
  544. { PLL, 50, { "pll", "pll-q", "pll-r" } },
  545. { PLL_I2S, 50, { "plli2s-p", "plli2s-q", "plli2s-r" } },
  546. { PLL_SAI, 50, { "pllsai-p", "pllsai-q", "pllsai-r" } },
  547. };
  548. static int stm32f4_pll_is_enabled(struct clk_hw *hw)
  549. {
  550. return clk_gate_ops.is_enabled(hw);
  551. }
  552. #define PLL_TIMEOUT 10000
  553. static int stm32f4_pll_enable(struct clk_hw *hw)
  554. {
  555. struct clk_gate *gate = to_clk_gate(hw);
  556. struct stm32f4_pll *pll = to_stm32f4_pll(gate);
  557. int bit_status;
  558. unsigned int timeout = PLL_TIMEOUT;
  559. if (clk_gate_ops.is_enabled(hw))
  560. return 0;
  561. clk_gate_ops.enable(hw);
  562. do {
  563. bit_status = !(readl(gate->reg) & BIT(pll->bit_rdy_idx));
  564. } while (bit_status && --timeout);
  565. return bit_status;
  566. }
  567. static void stm32f4_pll_disable(struct clk_hw *hw)
  568. {
  569. clk_gate_ops.disable(hw);
  570. }
  571. static unsigned long stm32f4_pll_recalc(struct clk_hw *hw,
  572. unsigned long parent_rate)
  573. {
  574. struct clk_gate *gate = to_clk_gate(hw);
  575. struct stm32f4_pll *pll = to_stm32f4_pll(gate);
  576. unsigned long val;
  577. unsigned long n;
  578. val = readl(base + pll->offset);
  579. n = FIELD_GET(STM32F4_RCC_PLLCFGR_N_MASK, val);
  580. return parent_rate * n;
  581. }
  582. static int stm32f4_pll_determine_rate(struct clk_hw *hw,
  583. struct clk_rate_request *req)
  584. {
  585. struct clk_gate *gate = to_clk_gate(hw);
  586. struct stm32f4_pll *pll = to_stm32f4_pll(gate);
  587. unsigned long n;
  588. n = req->rate / req->best_parent_rate;
  589. if (n < pll->n_start)
  590. n = pll->n_start;
  591. else if (n > 432)
  592. n = 432;
  593. req->rate = req->best_parent_rate * n;
  594. return 0;
  595. }
  596. static void stm32f4_pll_set_ssc(struct clk_hw *hw, unsigned long parent_rate,
  597. unsigned int ndiv)
  598. {
  599. struct clk_gate *gate = to_clk_gate(hw);
  600. struct stm32f4_pll *pll = to_stm32f4_pll(gate);
  601. struct stm32f4_pll_ssc *ssc = &pll->ssc_conf;
  602. u32 modeper, incstep;
  603. u32 sscgr;
  604. sscgr = readl(base + STM32F4_RCC_SSCGR);
  605. /* reserved field must be kept at reset value */
  606. sscgr &= STM32F4_RCC_SSCGR_RESERVED_MASK;
  607. modeper = DIV_ROUND_CLOSEST(parent_rate, 4 * ssc->mod_freq);
  608. incstep = DIV_ROUND_CLOSEST(((1 << 15) - 1) * ssc->mod_depth * ndiv,
  609. 5 * 10000 * modeper);
  610. sscgr |= STM32F4_RCC_SSCGR_SSCGEN |
  611. FIELD_PREP(STM32F4_RCC_SSCGR_INCSTEP_MASK, incstep) |
  612. FIELD_PREP(STM32F4_RCC_SSCGR_MODPER_MASK, modeper);
  613. if (ssc->mod_type)
  614. sscgr |= STM32F4_RCC_SSCGR_SPREADSEL;
  615. writel(sscgr, base + STM32F4_RCC_SSCGR);
  616. }
  617. static int stm32f4_pll_set_rate(struct clk_hw *hw, unsigned long rate,
  618. unsigned long parent_rate)
  619. {
  620. struct clk_gate *gate = to_clk_gate(hw);
  621. struct stm32f4_pll *pll = to_stm32f4_pll(gate);
  622. unsigned long n;
  623. unsigned long val;
  624. int pll_state;
  625. pll_state = stm32f4_pll_is_enabled(hw);
  626. if (pll_state)
  627. stm32f4_pll_disable(hw);
  628. n = rate / parent_rate;
  629. val = readl(base + pll->offset) & ~STM32F4_RCC_PLLCFGR_N_MASK;
  630. val |= FIELD_PREP(STM32F4_RCC_PLLCFGR_N_MASK, n);
  631. writel(val, base + pll->offset);
  632. if (pll->ssc_enable)
  633. stm32f4_pll_set_ssc(hw, parent_rate, n);
  634. if (pll_state)
  635. stm32f4_pll_enable(hw);
  636. return 0;
  637. }
  638. static const struct clk_ops stm32f4_pll_gate_ops = {
  639. .enable = stm32f4_pll_enable,
  640. .disable = stm32f4_pll_disable,
  641. .is_enabled = stm32f4_pll_is_enabled,
  642. .recalc_rate = stm32f4_pll_recalc,
  643. .determine_rate = stm32f4_pll_determine_rate,
  644. .set_rate = stm32f4_pll_set_rate,
  645. };
  646. struct stm32f4_pll_div {
  647. struct clk_divider div;
  648. struct clk_hw *hw_pll;
  649. };
  650. #define to_pll_div_clk(_div) container_of(_div, struct stm32f4_pll_div, div)
  651. static unsigned long stm32f4_pll_div_recalc_rate(struct clk_hw *hw,
  652. unsigned long parent_rate)
  653. {
  654. return clk_divider_ops.recalc_rate(hw, parent_rate);
  655. }
  656. static int stm32f4_pll_div_determine_rate(struct clk_hw *hw,
  657. struct clk_rate_request *req)
  658. {
  659. return clk_divider_ops.determine_rate(hw, req);
  660. }
  661. static int stm32f4_pll_div_set_rate(struct clk_hw *hw, unsigned long rate,
  662. unsigned long parent_rate)
  663. {
  664. int pll_state, ret;
  665. struct clk_divider *div = to_clk_divider(hw);
  666. struct stm32f4_pll_div *pll_div = to_pll_div_clk(div);
  667. pll_state = stm32f4_pll_is_enabled(pll_div->hw_pll);
  668. if (pll_state)
  669. stm32f4_pll_disable(pll_div->hw_pll);
  670. ret = clk_divider_ops.set_rate(hw, rate, parent_rate);
  671. if (pll_state)
  672. stm32f4_pll_enable(pll_div->hw_pll);
  673. return ret;
  674. }
  675. static const struct clk_ops stm32f4_pll_div_ops = {
  676. .recalc_rate = stm32f4_pll_div_recalc_rate,
  677. .determine_rate = stm32f4_pll_div_determine_rate,
  678. .set_rate = stm32f4_pll_div_set_rate,
  679. };
  680. static struct clk_hw *clk_register_pll_div(const char *name,
  681. const char *parent_name, unsigned long flags,
  682. void __iomem *reg, u8 shift, u8 width,
  683. u8 clk_divider_flags, const struct clk_div_table *table,
  684. struct clk_hw *pll_hw, spinlock_t *lock)
  685. {
  686. struct stm32f4_pll_div *pll_div;
  687. struct clk_hw *hw;
  688. struct clk_init_data init;
  689. int ret;
  690. /* allocate the divider */
  691. pll_div = kzalloc_obj(*pll_div);
  692. if (!pll_div)
  693. return ERR_PTR(-ENOMEM);
  694. init.name = name;
  695. init.ops = &stm32f4_pll_div_ops;
  696. init.flags = flags;
  697. init.parent_names = (parent_name ? &parent_name : NULL);
  698. init.num_parents = (parent_name ? 1 : 0);
  699. /* struct clk_divider assignments */
  700. pll_div->div.reg = reg;
  701. pll_div->div.shift = shift;
  702. pll_div->div.width = width;
  703. pll_div->div.flags = clk_divider_flags;
  704. pll_div->div.lock = lock;
  705. pll_div->div.table = table;
  706. pll_div->div.hw.init = &init;
  707. pll_div->hw_pll = pll_hw;
  708. /* register the clock */
  709. hw = &pll_div->div.hw;
  710. ret = clk_hw_register(NULL, hw);
  711. if (ret) {
  712. kfree(pll_div);
  713. hw = ERR_PTR(ret);
  714. }
  715. return hw;
  716. }
  717. static int __init stm32f4_pll_init_ssc(struct clk_hw *hw,
  718. const struct stm32f4_pll_ssc *conf)
  719. {
  720. struct clk_gate *gate = to_clk_gate(hw);
  721. struct stm32f4_pll *pll = to_stm32f4_pll(gate);
  722. struct clk_hw *parent;
  723. unsigned long parent_rate;
  724. int pll_state;
  725. unsigned long n, val;
  726. parent = clk_hw_get_parent(hw);
  727. if (!parent) {
  728. pr_err("%s: failed to get clock parent\n", __func__);
  729. return -ENODEV;
  730. }
  731. parent_rate = clk_hw_get_rate(parent);
  732. pll->ssc_enable = true;
  733. memcpy(&pll->ssc_conf, conf, sizeof(pll->ssc_conf));
  734. pll_state = stm32f4_pll_is_enabled(hw);
  735. if (pll_state)
  736. stm32f4_pll_disable(hw);
  737. val = readl(base + pll->offset);
  738. n = FIELD_GET(STM32F4_RCC_PLLCFGR_N_MASK, val);
  739. pr_debug("%s: pll: %s, parent: %s, parent-rate: %lu, n: %lu\n",
  740. __func__, clk_hw_get_name(hw), clk_hw_get_name(parent),
  741. parent_rate, n);
  742. stm32f4_pll_set_ssc(hw, parent_rate, n);
  743. if (pll_state)
  744. stm32f4_pll_enable(hw);
  745. return 0;
  746. }
  747. static int __init stm32f4_pll_ssc_parse_dt(struct device_node *np,
  748. struct stm32f4_pll_ssc *conf)
  749. {
  750. int ret;
  751. if (!conf)
  752. return -EINVAL;
  753. ret = of_property_read_u32(np, "st,ssc-modfreq-hz", &conf->mod_freq);
  754. if (ret)
  755. return ret;
  756. ret = of_property_read_u32(np, "st,ssc-moddepth-permyriad",
  757. &conf->mod_depth);
  758. if (ret) {
  759. pr_err("%pOF: missing st,ssc-moddepth-permyriad\n", np);
  760. return ret;
  761. }
  762. ret = fwnode_property_match_property_string(of_fwnode_handle(np),
  763. "st,ssc-modmethod",
  764. stm32f4_ssc_mod_methods,
  765. ARRAY_SIZE(stm32f4_ssc_mod_methods));
  766. if (ret < 0) {
  767. pr_err("%pOF: failed to get st,ssc-modmethod\n", np);
  768. return ret;
  769. }
  770. conf->mod_type = ret;
  771. pr_debug("%pOF: SSCG settings: mod_freq: %d, mod_depth: %d mod_method: %s [%d]\n",
  772. np, conf->mod_freq, conf->mod_depth,
  773. stm32f4_ssc_mod_methods[ret], conf->mod_type);
  774. return 0;
  775. }
  776. static struct clk_hw *stm32f4_rcc_register_pll(const char *pllsrc,
  777. const struct stm32f4_pll_data *data, spinlock_t *lock)
  778. {
  779. struct stm32f4_pll *pll;
  780. struct clk_init_data init = { NULL };
  781. void __iomem *reg;
  782. struct clk_hw *pll_hw;
  783. int ret;
  784. int i;
  785. const struct stm32f4_vco_data *vco;
  786. pll = kzalloc_obj(*pll);
  787. if (!pll)
  788. return ERR_PTR(-ENOMEM);
  789. vco = &vco_data[data->pll_num];
  790. init.name = vco->vco_name;
  791. init.ops = &stm32f4_pll_gate_ops;
  792. init.flags = CLK_SET_RATE_GATE;
  793. init.parent_names = &pllsrc;
  794. init.num_parents = 1;
  795. pll->gate.lock = lock;
  796. pll->gate.reg = base + STM32F4_RCC_CR;
  797. pll->gate.bit_idx = vco->bit_idx;
  798. pll->gate.hw.init = &init;
  799. pll->offset = vco->offset;
  800. pll->n_start = data->n_start;
  801. pll->bit_rdy_idx = vco->bit_rdy_idx;
  802. pll->status = (readl(base + STM32F4_RCC_CR) >> vco->bit_idx) & 0x1;
  803. reg = base + pll->offset;
  804. pll_hw = &pll->gate.hw;
  805. ret = clk_hw_register(NULL, pll_hw);
  806. if (ret) {
  807. kfree(pll);
  808. return ERR_PTR(ret);
  809. }
  810. for (i = 0; i < MAX_PLL_DIV; i++)
  811. if (data->div_name[i])
  812. clk_register_pll_div(data->div_name[i],
  813. vco->vco_name,
  814. 0,
  815. reg,
  816. div_data[i].shift,
  817. div_data[i].width,
  818. div_data[i].flag_div,
  819. div_data[i].div_table,
  820. pll_hw,
  821. lock);
  822. return pll_hw;
  823. }
  824. /*
  825. * Converts the primary and secondary indices (as they appear in DT) to an
  826. * offset into our struct clock array.
  827. */
  828. static int stm32f4_rcc_lookup_clk_idx(u8 primary, u8 secondary)
  829. {
  830. u64 table[MAX_GATE_MAP];
  831. if (primary == 1) {
  832. if (WARN_ON(secondary >= stm32fx_end_primary_clk))
  833. return -EINVAL;
  834. return secondary;
  835. }
  836. memcpy(table, stm32f4_gate_map, sizeof(table));
  837. /* only bits set in table can be used as indices */
  838. if (WARN_ON(secondary >= BITS_PER_BYTE * sizeof(table) ||
  839. 0 == (table[BIT_ULL_WORD(secondary)] &
  840. BIT_ULL_MASK(secondary))))
  841. return -EINVAL;
  842. /* mask out bits above our current index */
  843. table[BIT_ULL_WORD(secondary)] &=
  844. GENMASK_ULL(secondary % BITS_PER_LONG_LONG, 0);
  845. return stm32fx_end_primary_clk - 1 + hweight64(table[0]) +
  846. (BIT_ULL_WORD(secondary) >= 1 ? hweight64(table[1]) : 0) +
  847. (BIT_ULL_WORD(secondary) >= 2 ? hweight64(table[2]) : 0);
  848. }
  849. static struct clk_hw *
  850. stm32f4_rcc_lookup_clk(struct of_phandle_args *clkspec, void *data)
  851. {
  852. int i = stm32f4_rcc_lookup_clk_idx(clkspec->args[0], clkspec->args[1]);
  853. if (i < 0)
  854. return ERR_PTR(-EINVAL);
  855. return clks[i];
  856. }
  857. #define to_rgclk(_rgate) container_of(_rgate, struct stm32_rgate, gate)
  858. static inline void disable_power_domain_write_protection(void)
  859. {
  860. if (pdrm)
  861. regmap_update_bits(pdrm, 0x00, (1 << 8), (1 << 8));
  862. }
  863. static inline void enable_power_domain_write_protection(void)
  864. {
  865. if (pdrm)
  866. regmap_update_bits(pdrm, 0x00, (1 << 8), (0 << 8));
  867. }
  868. static inline void sofware_reset_backup_domain(void)
  869. {
  870. unsigned long val;
  871. val = readl(base + STM32F4_RCC_BDCR);
  872. writel(val | BIT(16), base + STM32F4_RCC_BDCR);
  873. writel(val & ~BIT(16), base + STM32F4_RCC_BDCR);
  874. }
  875. struct stm32_rgate {
  876. struct clk_gate gate;
  877. u8 bit_rdy_idx;
  878. };
  879. #define RGATE_TIMEOUT 50000
  880. static int rgclk_enable(struct clk_hw *hw)
  881. {
  882. struct clk_gate *gate = to_clk_gate(hw);
  883. struct stm32_rgate *rgate = to_rgclk(gate);
  884. int bit_status;
  885. unsigned int timeout = RGATE_TIMEOUT;
  886. if (clk_gate_ops.is_enabled(hw))
  887. return 0;
  888. disable_power_domain_write_protection();
  889. clk_gate_ops.enable(hw);
  890. do {
  891. bit_status = !(readl(gate->reg) & BIT(rgate->bit_rdy_idx));
  892. if (bit_status)
  893. udelay(100);
  894. } while (bit_status && --timeout);
  895. enable_power_domain_write_protection();
  896. return bit_status;
  897. }
  898. static void rgclk_disable(struct clk_hw *hw)
  899. {
  900. clk_gate_ops.disable(hw);
  901. }
  902. static int rgclk_is_enabled(struct clk_hw *hw)
  903. {
  904. return clk_gate_ops.is_enabled(hw);
  905. }
  906. static const struct clk_ops rgclk_ops = {
  907. .enable = rgclk_enable,
  908. .disable = rgclk_disable,
  909. .is_enabled = rgclk_is_enabled,
  910. };
  911. static struct clk_hw *clk_register_rgate(struct device *dev, const char *name,
  912. const char *parent_name, unsigned long flags,
  913. void __iomem *reg, u8 bit_idx, u8 bit_rdy_idx,
  914. u8 clk_gate_flags, spinlock_t *lock)
  915. {
  916. struct stm32_rgate *rgate;
  917. struct clk_init_data init = { NULL };
  918. struct clk_hw *hw;
  919. int ret;
  920. rgate = kzalloc_obj(*rgate);
  921. if (!rgate)
  922. return ERR_PTR(-ENOMEM);
  923. init.name = name;
  924. init.ops = &rgclk_ops;
  925. init.flags = flags;
  926. init.parent_names = &parent_name;
  927. init.num_parents = 1;
  928. rgate->bit_rdy_idx = bit_rdy_idx;
  929. rgate->gate.lock = lock;
  930. rgate->gate.reg = reg;
  931. rgate->gate.bit_idx = bit_idx;
  932. rgate->gate.hw.init = &init;
  933. hw = &rgate->gate.hw;
  934. ret = clk_hw_register(dev, hw);
  935. if (ret) {
  936. kfree(rgate);
  937. hw = ERR_PTR(ret);
  938. }
  939. return hw;
  940. }
  941. static int cclk_gate_enable(struct clk_hw *hw)
  942. {
  943. int ret;
  944. disable_power_domain_write_protection();
  945. ret = clk_gate_ops.enable(hw);
  946. enable_power_domain_write_protection();
  947. return ret;
  948. }
  949. static void cclk_gate_disable(struct clk_hw *hw)
  950. {
  951. disable_power_domain_write_protection();
  952. clk_gate_ops.disable(hw);
  953. enable_power_domain_write_protection();
  954. }
  955. static int cclk_gate_is_enabled(struct clk_hw *hw)
  956. {
  957. return clk_gate_ops.is_enabled(hw);
  958. }
  959. static const struct clk_ops cclk_gate_ops = {
  960. .enable = cclk_gate_enable,
  961. .disable = cclk_gate_disable,
  962. .is_enabled = cclk_gate_is_enabled,
  963. };
  964. static u8 cclk_mux_get_parent(struct clk_hw *hw)
  965. {
  966. return clk_mux_ops.get_parent(hw);
  967. }
  968. static int cclk_mux_set_parent(struct clk_hw *hw, u8 index)
  969. {
  970. int ret;
  971. disable_power_domain_write_protection();
  972. sofware_reset_backup_domain();
  973. ret = clk_mux_ops.set_parent(hw, index);
  974. enable_power_domain_write_protection();
  975. return ret;
  976. }
  977. static const struct clk_ops cclk_mux_ops = {
  978. .determine_rate = clk_hw_determine_rate_no_reparent,
  979. .get_parent = cclk_mux_get_parent,
  980. .set_parent = cclk_mux_set_parent,
  981. };
  982. static struct clk_hw *stm32_register_cclk(struct device *dev, const char *name,
  983. const char * const *parent_names, int num_parents,
  984. void __iomem *reg, u8 bit_idx, u8 shift, unsigned long flags,
  985. spinlock_t *lock)
  986. {
  987. struct clk_hw *hw;
  988. struct clk_gate *gate;
  989. struct clk_mux *mux;
  990. gate = kzalloc_obj(*gate);
  991. if (!gate) {
  992. hw = ERR_PTR(-EINVAL);
  993. goto fail;
  994. }
  995. mux = kzalloc_obj(*mux);
  996. if (!mux) {
  997. kfree(gate);
  998. hw = ERR_PTR(-EINVAL);
  999. goto fail;
  1000. }
  1001. gate->reg = reg;
  1002. gate->bit_idx = bit_idx;
  1003. gate->flags = 0;
  1004. gate->lock = lock;
  1005. mux->reg = reg;
  1006. mux->shift = shift;
  1007. mux->mask = 3;
  1008. mux->flags = 0;
  1009. hw = clk_hw_register_composite(dev, name, parent_names, num_parents,
  1010. &mux->hw, &cclk_mux_ops,
  1011. NULL, NULL,
  1012. &gate->hw, &cclk_gate_ops,
  1013. flags);
  1014. if (IS_ERR(hw)) {
  1015. kfree(gate);
  1016. kfree(mux);
  1017. }
  1018. fail:
  1019. return hw;
  1020. }
  1021. static const char *sys_parents[] __initdata = { "hsi", NULL, "pll" };
  1022. static const struct clk_div_table ahb_div_table[] = {
  1023. { 0x0, 1 }, { 0x1, 1 }, { 0x2, 1 }, { 0x3, 1 },
  1024. { 0x4, 1 }, { 0x5, 1 }, { 0x6, 1 }, { 0x7, 1 },
  1025. { 0x8, 2 }, { 0x9, 4 }, { 0xa, 8 }, { 0xb, 16 },
  1026. { 0xc, 64 }, { 0xd, 128 }, { 0xe, 256 }, { 0xf, 512 },
  1027. { 0 },
  1028. };
  1029. static const struct clk_div_table apb_div_table[] = {
  1030. { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 },
  1031. { 4, 2 }, { 5, 4 }, { 6, 8 }, { 7, 16 },
  1032. { 0 },
  1033. };
  1034. static const char *rtc_parents[4] = {
  1035. "no-clock", "lse", "lsi", "hse-rtc"
  1036. };
  1037. static const char *pll_src = "pll-src";
  1038. static const char *pllsrc_parent[2] = { "hsi", NULL };
  1039. static const char *dsi_parent[2] = { NULL, "pll-r" };
  1040. static const char *lcd_parent[1] = { "pllsai-r-div" };
  1041. static const char *i2s_parents[2] = { "plli2s-r", NULL };
  1042. static const char *sai_parents[4] = { "pllsai-q-div", "plli2s-q-div", NULL,
  1043. "no-clock" };
  1044. static const char *pll48_parents[2] = { "pll-q", "pllsai-p" };
  1045. static const char *sdmux_parents[2] = { "pll48", "sys" };
  1046. static const char *hdmi_parents[2] = { "lse", "hsi_div488" };
  1047. static const char *spdif_parent[1] = { "plli2s-p" };
  1048. static const char *lptim_parent[4] = { "apb1_mul", "lsi", "hsi", "lse" };
  1049. static const char *uart_parents1[4] = { "apb2_div", "sys", "hsi", "lse" };
  1050. static const char *uart_parents2[4] = { "apb1_div", "sys", "hsi", "lse" };
  1051. static const char *i2c_parents[4] = { "apb1_div", "sys", "hsi", "no-clock" };
  1052. static const char * const dfsdm1_src[] = { "apb2_div", "sys" };
  1053. static const char * const adsfdm1_parent[] = { "sai1_clk", "sai2_clk" };
  1054. struct stm32_aux_clk {
  1055. int idx;
  1056. const char *name;
  1057. const char * const *parent_names;
  1058. int num_parents;
  1059. int offset_mux;
  1060. u8 shift;
  1061. u8 mask;
  1062. int offset_gate;
  1063. u8 bit_idx;
  1064. unsigned long flags;
  1065. };
  1066. struct stm32f4_clk_data {
  1067. const struct stm32f4_gate_data *gates_data;
  1068. const u64 *gates_map;
  1069. int gates_num;
  1070. const struct stm32f4_pll_data *pll_data;
  1071. const struct stm32_aux_clk *aux_clk;
  1072. int aux_clk_num;
  1073. int end_primary;
  1074. };
  1075. static const struct stm32_aux_clk stm32f429_aux_clk[] = {
  1076. {
  1077. CLK_LCD, "lcd-tft", lcd_parent, ARRAY_SIZE(lcd_parent),
  1078. NO_MUX, 0, 0,
  1079. STM32F4_RCC_APB2ENR, 26,
  1080. CLK_SET_RATE_PARENT
  1081. },
  1082. {
  1083. CLK_I2S, "i2s", i2s_parents, ARRAY_SIZE(i2s_parents),
  1084. STM32F4_RCC_CFGR, 23, 1,
  1085. NO_GATE, 0,
  1086. CLK_SET_RATE_PARENT
  1087. },
  1088. {
  1089. CLK_SAI1, "sai1-a", sai_parents, ARRAY_SIZE(sai_parents),
  1090. STM32F4_RCC_DCKCFGR, 20, 3,
  1091. STM32F4_RCC_APB2ENR, 22,
  1092. CLK_SET_RATE_PARENT
  1093. },
  1094. {
  1095. CLK_SAI2, "sai1-b", sai_parents, ARRAY_SIZE(sai_parents),
  1096. STM32F4_RCC_DCKCFGR, 22, 3,
  1097. STM32F4_RCC_APB2ENR, 22,
  1098. CLK_SET_RATE_PARENT
  1099. },
  1100. };
  1101. static const struct stm32_aux_clk stm32f469_aux_clk[] = {
  1102. {
  1103. CLK_LCD, "lcd-tft", lcd_parent, ARRAY_SIZE(lcd_parent),
  1104. NO_MUX, 0, 0,
  1105. STM32F4_RCC_APB2ENR, 26,
  1106. CLK_SET_RATE_PARENT
  1107. },
  1108. {
  1109. CLK_I2S, "i2s", i2s_parents, ARRAY_SIZE(i2s_parents),
  1110. STM32F4_RCC_CFGR, 23, 1,
  1111. NO_GATE, 0,
  1112. CLK_SET_RATE_PARENT
  1113. },
  1114. {
  1115. CLK_SAI1, "sai1-a", sai_parents, ARRAY_SIZE(sai_parents),
  1116. STM32F4_RCC_DCKCFGR, 20, 3,
  1117. STM32F4_RCC_APB2ENR, 22,
  1118. CLK_SET_RATE_PARENT
  1119. },
  1120. {
  1121. CLK_SAI2, "sai1-b", sai_parents, ARRAY_SIZE(sai_parents),
  1122. STM32F4_RCC_DCKCFGR, 22, 3,
  1123. STM32F4_RCC_APB2ENR, 22,
  1124. CLK_SET_RATE_PARENT
  1125. },
  1126. {
  1127. NO_IDX, "pll48", pll48_parents, ARRAY_SIZE(pll48_parents),
  1128. STM32F4_RCC_DCKCFGR, 27, 1,
  1129. NO_GATE, 0,
  1130. 0
  1131. },
  1132. {
  1133. NO_IDX, "sdmux", sdmux_parents, ARRAY_SIZE(sdmux_parents),
  1134. STM32F4_RCC_DCKCFGR, 28, 1,
  1135. NO_GATE, 0,
  1136. 0
  1137. },
  1138. {
  1139. CLK_F469_DSI, "dsi", dsi_parent, ARRAY_SIZE(dsi_parent),
  1140. STM32F4_RCC_DCKCFGR, 29, 1,
  1141. STM32F4_RCC_APB2ENR, 27,
  1142. CLK_SET_RATE_PARENT | CLK_SET_RATE_NO_REPARENT
  1143. },
  1144. };
  1145. static const struct stm32_aux_clk stm32f746_aux_clk[] = {
  1146. {
  1147. CLK_LCD, "lcd-tft", lcd_parent, ARRAY_SIZE(lcd_parent),
  1148. NO_MUX, 0, 0,
  1149. STM32F4_RCC_APB2ENR, 26,
  1150. CLK_SET_RATE_PARENT
  1151. },
  1152. {
  1153. CLK_I2S, "i2s", i2s_parents, ARRAY_SIZE(i2s_parents),
  1154. STM32F4_RCC_CFGR, 23, 1,
  1155. NO_GATE, 0,
  1156. CLK_SET_RATE_PARENT
  1157. },
  1158. {
  1159. CLK_SAI1, "sai1_clk", sai_parents, ARRAY_SIZE(sai_parents),
  1160. STM32F4_RCC_DCKCFGR, 20, 3,
  1161. STM32F4_RCC_APB2ENR, 22,
  1162. CLK_SET_RATE_PARENT
  1163. },
  1164. {
  1165. CLK_SAI2, "sai2_clk", sai_parents, ARRAY_SIZE(sai_parents),
  1166. STM32F4_RCC_DCKCFGR, 22, 3,
  1167. STM32F4_RCC_APB2ENR, 23,
  1168. CLK_SET_RATE_PARENT
  1169. },
  1170. {
  1171. NO_IDX, "pll48", pll48_parents, ARRAY_SIZE(pll48_parents),
  1172. STM32F7_RCC_DCKCFGR2, 27, 1,
  1173. NO_GATE, 0,
  1174. 0
  1175. },
  1176. {
  1177. NO_IDX, "sdmux", sdmux_parents, ARRAY_SIZE(sdmux_parents),
  1178. STM32F7_RCC_DCKCFGR2, 28, 1,
  1179. NO_GATE, 0,
  1180. 0
  1181. },
  1182. {
  1183. CLK_HDMI_CEC, "hdmi-cec",
  1184. hdmi_parents, ARRAY_SIZE(hdmi_parents),
  1185. STM32F7_RCC_DCKCFGR2, 26, 1,
  1186. NO_GATE, 0,
  1187. 0
  1188. },
  1189. {
  1190. CLK_SPDIF, "spdif-rx",
  1191. spdif_parent, ARRAY_SIZE(spdif_parent),
  1192. STM32F7_RCC_DCKCFGR2, 22, 3,
  1193. STM32F4_RCC_APB2ENR, 23,
  1194. CLK_SET_RATE_PARENT
  1195. },
  1196. {
  1197. CLK_USART1, "usart1",
  1198. uart_parents1, ARRAY_SIZE(uart_parents1),
  1199. STM32F7_RCC_DCKCFGR2, 0, 3,
  1200. STM32F4_RCC_APB2ENR, 4,
  1201. CLK_SET_RATE_PARENT,
  1202. },
  1203. {
  1204. CLK_USART2, "usart2",
  1205. uart_parents2, ARRAY_SIZE(uart_parents1),
  1206. STM32F7_RCC_DCKCFGR2, 2, 3,
  1207. STM32F4_RCC_APB1ENR, 17,
  1208. CLK_SET_RATE_PARENT,
  1209. },
  1210. {
  1211. CLK_USART3, "usart3",
  1212. uart_parents2, ARRAY_SIZE(uart_parents1),
  1213. STM32F7_RCC_DCKCFGR2, 4, 3,
  1214. STM32F4_RCC_APB1ENR, 18,
  1215. CLK_SET_RATE_PARENT,
  1216. },
  1217. {
  1218. CLK_UART4, "uart4",
  1219. uart_parents2, ARRAY_SIZE(uart_parents1),
  1220. STM32F7_RCC_DCKCFGR2, 6, 3,
  1221. STM32F4_RCC_APB1ENR, 19,
  1222. CLK_SET_RATE_PARENT,
  1223. },
  1224. {
  1225. CLK_UART5, "uart5",
  1226. uart_parents2, ARRAY_SIZE(uart_parents1),
  1227. STM32F7_RCC_DCKCFGR2, 8, 3,
  1228. STM32F4_RCC_APB1ENR, 20,
  1229. CLK_SET_RATE_PARENT,
  1230. },
  1231. {
  1232. CLK_USART6, "usart6",
  1233. uart_parents1, ARRAY_SIZE(uart_parents1),
  1234. STM32F7_RCC_DCKCFGR2, 10, 3,
  1235. STM32F4_RCC_APB2ENR, 5,
  1236. CLK_SET_RATE_PARENT,
  1237. },
  1238. {
  1239. CLK_UART7, "uart7",
  1240. uart_parents2, ARRAY_SIZE(uart_parents1),
  1241. STM32F7_RCC_DCKCFGR2, 12, 3,
  1242. STM32F4_RCC_APB1ENR, 30,
  1243. CLK_SET_RATE_PARENT,
  1244. },
  1245. {
  1246. CLK_UART8, "uart8",
  1247. uart_parents2, ARRAY_SIZE(uart_parents1),
  1248. STM32F7_RCC_DCKCFGR2, 14, 3,
  1249. STM32F4_RCC_APB1ENR, 31,
  1250. CLK_SET_RATE_PARENT,
  1251. },
  1252. {
  1253. CLK_I2C1, "i2c1",
  1254. i2c_parents, ARRAY_SIZE(i2c_parents),
  1255. STM32F7_RCC_DCKCFGR2, 16, 3,
  1256. STM32F4_RCC_APB1ENR, 21,
  1257. CLK_SET_RATE_PARENT,
  1258. },
  1259. {
  1260. CLK_I2C2, "i2c2",
  1261. i2c_parents, ARRAY_SIZE(i2c_parents),
  1262. STM32F7_RCC_DCKCFGR2, 18, 3,
  1263. STM32F4_RCC_APB1ENR, 22,
  1264. CLK_SET_RATE_PARENT,
  1265. },
  1266. {
  1267. CLK_I2C3, "i2c3",
  1268. i2c_parents, ARRAY_SIZE(i2c_parents),
  1269. STM32F7_RCC_DCKCFGR2, 20, 3,
  1270. STM32F4_RCC_APB1ENR, 23,
  1271. CLK_SET_RATE_PARENT,
  1272. },
  1273. {
  1274. CLK_I2C4, "i2c4",
  1275. i2c_parents, ARRAY_SIZE(i2c_parents),
  1276. STM32F7_RCC_DCKCFGR2, 22, 3,
  1277. STM32F4_RCC_APB1ENR, 24,
  1278. CLK_SET_RATE_PARENT,
  1279. },
  1280. {
  1281. CLK_LPTIMER, "lptim1",
  1282. lptim_parent, ARRAY_SIZE(lptim_parent),
  1283. STM32F7_RCC_DCKCFGR2, 24, 3,
  1284. STM32F4_RCC_APB1ENR, 9,
  1285. CLK_SET_RATE_PARENT
  1286. },
  1287. };
  1288. static const struct stm32_aux_clk stm32f769_aux_clk[] = {
  1289. {
  1290. CLK_LCD, "lcd-tft", lcd_parent, ARRAY_SIZE(lcd_parent),
  1291. NO_MUX, 0, 0,
  1292. STM32F4_RCC_APB2ENR, 26,
  1293. CLK_SET_RATE_PARENT
  1294. },
  1295. {
  1296. CLK_I2S, "i2s", i2s_parents, ARRAY_SIZE(i2s_parents),
  1297. STM32F4_RCC_CFGR, 23, 1,
  1298. NO_GATE, 0,
  1299. CLK_SET_RATE_PARENT
  1300. },
  1301. {
  1302. CLK_SAI1, "sai1_clk", sai_parents, ARRAY_SIZE(sai_parents),
  1303. STM32F4_RCC_DCKCFGR, 20, 3,
  1304. STM32F4_RCC_APB2ENR, 22,
  1305. CLK_SET_RATE_PARENT
  1306. },
  1307. {
  1308. CLK_SAI2, "sai2_clk", sai_parents, ARRAY_SIZE(sai_parents),
  1309. STM32F4_RCC_DCKCFGR, 22, 3,
  1310. STM32F4_RCC_APB2ENR, 23,
  1311. CLK_SET_RATE_PARENT
  1312. },
  1313. {
  1314. NO_IDX, "pll48", pll48_parents, ARRAY_SIZE(pll48_parents),
  1315. STM32F7_RCC_DCKCFGR2, 27, 1,
  1316. NO_GATE, 0,
  1317. 0
  1318. },
  1319. {
  1320. NO_IDX, "sdmux1", sdmux_parents, ARRAY_SIZE(sdmux_parents),
  1321. STM32F7_RCC_DCKCFGR2, 28, 1,
  1322. NO_GATE, 0,
  1323. 0
  1324. },
  1325. {
  1326. NO_IDX, "sdmux2", sdmux_parents, ARRAY_SIZE(sdmux_parents),
  1327. STM32F7_RCC_DCKCFGR2, 29, 1,
  1328. NO_GATE, 0,
  1329. 0
  1330. },
  1331. {
  1332. CLK_HDMI_CEC, "hdmi-cec",
  1333. hdmi_parents, ARRAY_SIZE(hdmi_parents),
  1334. STM32F7_RCC_DCKCFGR2, 26, 1,
  1335. NO_GATE, 0,
  1336. 0
  1337. },
  1338. {
  1339. CLK_SPDIF, "spdif-rx",
  1340. spdif_parent, ARRAY_SIZE(spdif_parent),
  1341. STM32F7_RCC_DCKCFGR2, 22, 3,
  1342. STM32F4_RCC_APB2ENR, 23,
  1343. CLK_SET_RATE_PARENT
  1344. },
  1345. {
  1346. CLK_USART1, "usart1",
  1347. uart_parents1, ARRAY_SIZE(uart_parents1),
  1348. STM32F7_RCC_DCKCFGR2, 0, 3,
  1349. STM32F4_RCC_APB2ENR, 4,
  1350. CLK_SET_RATE_PARENT,
  1351. },
  1352. {
  1353. CLK_USART2, "usart2",
  1354. uart_parents2, ARRAY_SIZE(uart_parents1),
  1355. STM32F7_RCC_DCKCFGR2, 2, 3,
  1356. STM32F4_RCC_APB1ENR, 17,
  1357. CLK_SET_RATE_PARENT,
  1358. },
  1359. {
  1360. CLK_USART3, "usart3",
  1361. uart_parents2, ARRAY_SIZE(uart_parents1),
  1362. STM32F7_RCC_DCKCFGR2, 4, 3,
  1363. STM32F4_RCC_APB1ENR, 18,
  1364. CLK_SET_RATE_PARENT,
  1365. },
  1366. {
  1367. CLK_UART4, "uart4",
  1368. uart_parents2, ARRAY_SIZE(uart_parents1),
  1369. STM32F7_RCC_DCKCFGR2, 6, 3,
  1370. STM32F4_RCC_APB1ENR, 19,
  1371. CLK_SET_RATE_PARENT,
  1372. },
  1373. {
  1374. CLK_UART5, "uart5",
  1375. uart_parents2, ARRAY_SIZE(uart_parents1),
  1376. STM32F7_RCC_DCKCFGR2, 8, 3,
  1377. STM32F4_RCC_APB1ENR, 20,
  1378. CLK_SET_RATE_PARENT,
  1379. },
  1380. {
  1381. CLK_USART6, "usart6",
  1382. uart_parents1, ARRAY_SIZE(uart_parents1),
  1383. STM32F7_RCC_DCKCFGR2, 10, 3,
  1384. STM32F4_RCC_APB2ENR, 5,
  1385. CLK_SET_RATE_PARENT,
  1386. },
  1387. {
  1388. CLK_UART7, "uart7",
  1389. uart_parents2, ARRAY_SIZE(uart_parents1),
  1390. STM32F7_RCC_DCKCFGR2, 12, 3,
  1391. STM32F4_RCC_APB1ENR, 30,
  1392. CLK_SET_RATE_PARENT,
  1393. },
  1394. {
  1395. CLK_UART8, "uart8",
  1396. uart_parents2, ARRAY_SIZE(uart_parents1),
  1397. STM32F7_RCC_DCKCFGR2, 14, 3,
  1398. STM32F4_RCC_APB1ENR, 31,
  1399. CLK_SET_RATE_PARENT,
  1400. },
  1401. {
  1402. CLK_I2C1, "i2c1",
  1403. i2c_parents, ARRAY_SIZE(i2c_parents),
  1404. STM32F7_RCC_DCKCFGR2, 16, 3,
  1405. STM32F4_RCC_APB1ENR, 21,
  1406. CLK_SET_RATE_PARENT,
  1407. },
  1408. {
  1409. CLK_I2C2, "i2c2",
  1410. i2c_parents, ARRAY_SIZE(i2c_parents),
  1411. STM32F7_RCC_DCKCFGR2, 18, 3,
  1412. STM32F4_RCC_APB1ENR, 22,
  1413. CLK_SET_RATE_PARENT,
  1414. },
  1415. {
  1416. CLK_I2C3, "i2c3",
  1417. i2c_parents, ARRAY_SIZE(i2c_parents),
  1418. STM32F7_RCC_DCKCFGR2, 20, 3,
  1419. STM32F4_RCC_APB1ENR, 23,
  1420. CLK_SET_RATE_PARENT,
  1421. },
  1422. {
  1423. CLK_I2C4, "i2c4",
  1424. i2c_parents, ARRAY_SIZE(i2c_parents),
  1425. STM32F7_RCC_DCKCFGR2, 22, 3,
  1426. STM32F4_RCC_APB1ENR, 24,
  1427. CLK_SET_RATE_PARENT,
  1428. },
  1429. {
  1430. CLK_LPTIMER, "lptim1",
  1431. lptim_parent, ARRAY_SIZE(lptim_parent),
  1432. STM32F7_RCC_DCKCFGR2, 24, 3,
  1433. STM32F4_RCC_APB1ENR, 9,
  1434. CLK_SET_RATE_PARENT
  1435. },
  1436. {
  1437. CLK_F769_DSI, "dsi",
  1438. dsi_parent, ARRAY_SIZE(dsi_parent),
  1439. STM32F7_RCC_DCKCFGR2, 0, 1,
  1440. STM32F4_RCC_APB2ENR, 27,
  1441. CLK_SET_RATE_PARENT
  1442. },
  1443. {
  1444. CLK_DFSDM1, "dfsdm1",
  1445. dfsdm1_src, ARRAY_SIZE(dfsdm1_src),
  1446. STM32F4_RCC_DCKCFGR, 25, 1,
  1447. STM32F4_RCC_APB2ENR, 29,
  1448. CLK_SET_RATE_PARENT
  1449. },
  1450. {
  1451. CLK_ADFSDM1, "adfsdm1",
  1452. adsfdm1_parent, ARRAY_SIZE(adsfdm1_parent),
  1453. STM32F4_RCC_DCKCFGR, 26, 1,
  1454. STM32F4_RCC_APB2ENR, 29,
  1455. CLK_SET_RATE_PARENT
  1456. },
  1457. };
  1458. static const struct stm32f4_clk_data stm32f429_clk_data = {
  1459. .end_primary = END_PRIMARY_CLK,
  1460. .gates_data = stm32f429_gates,
  1461. .gates_map = stm32f42xx_gate_map,
  1462. .gates_num = ARRAY_SIZE(stm32f429_gates),
  1463. .pll_data = stm32f429_pll,
  1464. .aux_clk = stm32f429_aux_clk,
  1465. .aux_clk_num = ARRAY_SIZE(stm32f429_aux_clk),
  1466. };
  1467. static const struct stm32f4_clk_data stm32f469_clk_data = {
  1468. .end_primary = END_PRIMARY_CLK,
  1469. .gates_data = stm32f469_gates,
  1470. .gates_map = stm32f46xx_gate_map,
  1471. .gates_num = ARRAY_SIZE(stm32f469_gates),
  1472. .pll_data = stm32f469_pll,
  1473. .aux_clk = stm32f469_aux_clk,
  1474. .aux_clk_num = ARRAY_SIZE(stm32f469_aux_clk),
  1475. };
  1476. static const struct stm32f4_clk_data stm32f746_clk_data = {
  1477. .end_primary = END_PRIMARY_CLK_F7,
  1478. .gates_data = stm32f746_gates,
  1479. .gates_map = stm32f746_gate_map,
  1480. .gates_num = ARRAY_SIZE(stm32f746_gates),
  1481. .pll_data = stm32f469_pll,
  1482. .aux_clk = stm32f746_aux_clk,
  1483. .aux_clk_num = ARRAY_SIZE(stm32f746_aux_clk),
  1484. };
  1485. static const struct stm32f4_clk_data stm32f769_clk_data = {
  1486. .end_primary = END_PRIMARY_CLK_F7,
  1487. .gates_data = stm32f769_gates,
  1488. .gates_map = stm32f769_gate_map,
  1489. .gates_num = ARRAY_SIZE(stm32f769_gates),
  1490. .pll_data = stm32f469_pll,
  1491. .aux_clk = stm32f769_aux_clk,
  1492. .aux_clk_num = ARRAY_SIZE(stm32f769_aux_clk),
  1493. };
  1494. static const struct of_device_id stm32f4_of_match[] = {
  1495. {
  1496. .compatible = "st,stm32f42xx-rcc",
  1497. .data = &stm32f429_clk_data
  1498. },
  1499. {
  1500. .compatible = "st,stm32f469-rcc",
  1501. .data = &stm32f469_clk_data
  1502. },
  1503. {
  1504. .compatible = "st,stm32f746-rcc",
  1505. .data = &stm32f746_clk_data
  1506. },
  1507. {
  1508. .compatible = "st,stm32f769-rcc",
  1509. .data = &stm32f769_clk_data
  1510. },
  1511. {}
  1512. };
  1513. static struct clk_hw *stm32_register_aux_clk(const char *name,
  1514. const char * const *parent_names, int num_parents,
  1515. int offset_mux, u8 shift, u8 mask,
  1516. int offset_gate, u8 bit_idx,
  1517. unsigned long flags, spinlock_t *lock)
  1518. {
  1519. struct clk_hw *hw;
  1520. struct clk_gate *gate = NULL;
  1521. struct clk_mux *mux = NULL;
  1522. struct clk_hw *mux_hw = NULL, *gate_hw = NULL;
  1523. const struct clk_ops *mux_ops = NULL, *gate_ops = NULL;
  1524. if (offset_gate != NO_GATE) {
  1525. gate = kzalloc_obj(*gate);
  1526. if (!gate) {
  1527. hw = ERR_PTR(-EINVAL);
  1528. goto fail;
  1529. }
  1530. gate->reg = base + offset_gate;
  1531. gate->bit_idx = bit_idx;
  1532. gate->flags = 0;
  1533. gate->lock = lock;
  1534. gate_hw = &gate->hw;
  1535. gate_ops = &clk_gate_ops;
  1536. }
  1537. if (offset_mux != NO_MUX) {
  1538. mux = kzalloc_obj(*mux);
  1539. if (!mux) {
  1540. hw = ERR_PTR(-EINVAL);
  1541. goto fail;
  1542. }
  1543. mux->reg = base + offset_mux;
  1544. mux->shift = shift;
  1545. mux->mask = mask;
  1546. mux->flags = 0;
  1547. mux_hw = &mux->hw;
  1548. mux_ops = &clk_mux_ops;
  1549. }
  1550. if (mux_hw == NULL && gate_hw == NULL) {
  1551. hw = ERR_PTR(-EINVAL);
  1552. goto fail;
  1553. }
  1554. hw = clk_hw_register_composite(NULL, name, parent_names, num_parents,
  1555. mux_hw, mux_ops,
  1556. NULL, NULL,
  1557. gate_hw, gate_ops,
  1558. flags);
  1559. fail:
  1560. if (IS_ERR(hw)) {
  1561. kfree(gate);
  1562. kfree(mux);
  1563. }
  1564. return hw;
  1565. }
  1566. static void __init stm32f4_rcc_init(struct device_node *np)
  1567. {
  1568. const char *hse_clk, *i2s_in_clk;
  1569. int n;
  1570. const struct of_device_id *match;
  1571. const struct stm32f4_clk_data *data;
  1572. unsigned long pllm;
  1573. struct clk_hw *pll_src_hw, *pll_vco_hw;
  1574. struct stm32f4_pll_ssc ssc_conf;
  1575. base = of_iomap(np, 0);
  1576. if (!base) {
  1577. pr_err("%pOFn: unable to map resource\n", np);
  1578. return;
  1579. }
  1580. pdrm = syscon_regmap_lookup_by_phandle(np, "st,syscfg");
  1581. if (IS_ERR(pdrm)) {
  1582. pdrm = NULL;
  1583. pr_warn("%s: Unable to get syscfg\n", __func__);
  1584. }
  1585. match = of_match_node(stm32f4_of_match, np);
  1586. if (WARN_ON(!match))
  1587. return;
  1588. data = match->data;
  1589. stm32fx_end_primary_clk = data->end_primary;
  1590. clks = kmalloc_objs(*clks, data->gates_num + stm32fx_end_primary_clk);
  1591. if (!clks)
  1592. goto fail;
  1593. stm32f4_gate_map = data->gates_map;
  1594. hse_clk = of_clk_get_parent_name(np, 0);
  1595. dsi_parent[0] = hse_clk;
  1596. pllsrc_parent[1] = hse_clk;
  1597. i2s_in_clk = of_clk_get_parent_name(np, 1);
  1598. i2s_parents[1] = i2s_in_clk;
  1599. sai_parents[2] = i2s_in_clk;
  1600. if (of_device_is_compatible(np, "st,stm32f769-rcc")) {
  1601. clk_hw_register_gate(NULL, "dfsdm1_apb", "apb2_div", 0,
  1602. base + STM32F4_RCC_APB2ENR, 29,
  1603. CLK_IGNORE_UNUSED, &stm32f4_clk_lock);
  1604. dsi_parent[0] = pll_src;
  1605. sai_parents[3] = pll_src;
  1606. }
  1607. clks[CLK_HSI] = clk_hw_register_fixed_rate_with_accuracy(NULL, "hsi",
  1608. NULL, 0, 16000000, 160000);
  1609. pll_src_hw = clk_hw_register_mux(NULL, pll_src, pllsrc_parent,
  1610. ARRAY_SIZE(pllsrc_parent), 0,
  1611. base + STM32F4_RCC_PLLCFGR, 22, 1, 0,
  1612. &stm32f4_clk_lock);
  1613. pllm = readl(base + STM32F4_RCC_PLLCFGR) & 0x3f;
  1614. clk_hw_register_fixed_factor(NULL, "vco_in", pll_src,
  1615. 0, 1, pllm);
  1616. pll_vco_hw = stm32f4_rcc_register_pll("vco_in", &data->pll_data[0],
  1617. &stm32f4_clk_lock);
  1618. clks[PLL_VCO_I2S] = stm32f4_rcc_register_pll("vco_in",
  1619. &data->pll_data[1], &stm32f4_clk_lock);
  1620. clks[PLL_VCO_SAI] = stm32f4_rcc_register_pll("vco_in",
  1621. &data->pll_data[2], &stm32f4_clk_lock);
  1622. for (n = 0; n < MAX_POST_DIV; n++) {
  1623. const struct stm32f4_pll_post_div_data *post_div;
  1624. struct clk_hw *hw;
  1625. post_div = &post_div_data[n];
  1626. hw = clk_register_pll_div(post_div->name,
  1627. post_div->parent,
  1628. post_div->flag,
  1629. base + post_div->offset,
  1630. post_div->shift,
  1631. post_div->width,
  1632. post_div->flag_div,
  1633. post_div->div_table,
  1634. clks[post_div->pll_idx],
  1635. &stm32f4_clk_lock);
  1636. if (post_div->idx != NO_IDX)
  1637. clks[post_div->idx] = hw;
  1638. }
  1639. sys_parents[1] = hse_clk;
  1640. clks[CLK_SYSCLK] = clk_hw_register_mux_table(
  1641. NULL, "sys", sys_parents, ARRAY_SIZE(sys_parents), 0,
  1642. base + STM32F4_RCC_CFGR, 0, 3, 0, NULL, &stm32f4_clk_lock);
  1643. clk_register_divider_table(NULL, "ahb_div", "sys",
  1644. CLK_SET_RATE_PARENT, base + STM32F4_RCC_CFGR,
  1645. 4, 4, 0, ahb_div_table, &stm32f4_clk_lock);
  1646. clk_register_divider_table(NULL, "apb1_div", "ahb_div",
  1647. CLK_SET_RATE_PARENT, base + STM32F4_RCC_CFGR,
  1648. 10, 3, 0, apb_div_table, &stm32f4_clk_lock);
  1649. clk_register_apb_mul(NULL, "apb1_mul", "apb1_div",
  1650. CLK_SET_RATE_PARENT, 12);
  1651. clk_register_divider_table(NULL, "apb2_div", "ahb_div",
  1652. CLK_SET_RATE_PARENT, base + STM32F4_RCC_CFGR,
  1653. 13, 3, 0, apb_div_table, &stm32f4_clk_lock);
  1654. clk_register_apb_mul(NULL, "apb2_mul", "apb2_div",
  1655. CLK_SET_RATE_PARENT, 15);
  1656. clks[SYSTICK] = clk_hw_register_fixed_factor(NULL, "systick", "ahb_div",
  1657. 0, 1, 8);
  1658. clks[FCLK] = clk_hw_register_fixed_factor(NULL, "fclk", "ahb_div",
  1659. 0, 1, 1);
  1660. for (n = 0; n < data->gates_num; n++) {
  1661. const struct stm32f4_gate_data *gd;
  1662. unsigned int secondary;
  1663. int idx;
  1664. gd = &data->gates_data[n];
  1665. secondary = 8 * (gd->offset - STM32F4_RCC_AHB1ENR) +
  1666. gd->bit_idx;
  1667. idx = stm32f4_rcc_lookup_clk_idx(0, secondary);
  1668. if (idx < 0)
  1669. goto fail;
  1670. clks[idx] = clk_hw_register_gate(
  1671. NULL, gd->name, gd->parent_name, gd->flags,
  1672. base + gd->offset, gd->bit_idx, 0, &stm32f4_clk_lock);
  1673. if (IS_ERR(clks[idx])) {
  1674. pr_err("%pOF: Unable to register leaf clock %s\n",
  1675. np, gd->name);
  1676. goto fail;
  1677. }
  1678. }
  1679. clks[CLK_LSI] = clk_register_rgate(NULL, "lsi", "clk-lsi", 0,
  1680. base + STM32F4_RCC_CSR, 0, 1, 0, &stm32f4_clk_lock);
  1681. if (IS_ERR(clks[CLK_LSI])) {
  1682. pr_err("Unable to register lsi clock\n");
  1683. goto fail;
  1684. }
  1685. clks[CLK_LSE] = clk_register_rgate(NULL, "lse", "clk-lse", 0,
  1686. base + STM32F4_RCC_BDCR, 0, 1, 0, &stm32f4_clk_lock);
  1687. if (IS_ERR(clks[CLK_LSE])) {
  1688. pr_err("Unable to register lse clock\n");
  1689. goto fail;
  1690. }
  1691. clks[CLK_HSE_RTC] = clk_hw_register_divider(NULL, "hse-rtc", "clk-hse",
  1692. 0, base + STM32F4_RCC_CFGR, 16, 5, 0,
  1693. &stm32f4_clk_lock);
  1694. if (IS_ERR(clks[CLK_HSE_RTC])) {
  1695. pr_err("Unable to register hse-rtc clock\n");
  1696. goto fail;
  1697. }
  1698. clks[CLK_RTC] = stm32_register_cclk(NULL, "rtc", rtc_parents, 4,
  1699. base + STM32F4_RCC_BDCR, 15, 8, 0, &stm32f4_clk_lock);
  1700. if (IS_ERR(clks[CLK_RTC])) {
  1701. pr_err("Unable to register rtc clock\n");
  1702. goto fail;
  1703. }
  1704. for (n = 0; n < data->aux_clk_num; n++) {
  1705. const struct stm32_aux_clk *aux_clk;
  1706. struct clk_hw *hw;
  1707. aux_clk = &data->aux_clk[n];
  1708. hw = stm32_register_aux_clk(aux_clk->name,
  1709. aux_clk->parent_names, aux_clk->num_parents,
  1710. aux_clk->offset_mux, aux_clk->shift,
  1711. aux_clk->mask, aux_clk->offset_gate,
  1712. aux_clk->bit_idx, aux_clk->flags,
  1713. &stm32f4_clk_lock);
  1714. if (IS_ERR(hw)) {
  1715. pr_warn("Unable to register %s clk\n", aux_clk->name);
  1716. continue;
  1717. }
  1718. if (aux_clk->idx != NO_IDX)
  1719. clks[aux_clk->idx] = hw;
  1720. }
  1721. if (of_device_is_compatible(np, "st,stm32f746-rcc")) {
  1722. clk_hw_register_fixed_factor(NULL, "hsi_div488", "hsi", 0,
  1723. 1, 488);
  1724. clks[CLK_PLL_SRC] = pll_src_hw;
  1725. }
  1726. of_clk_add_hw_provider(np, stm32f4_rcc_lookup_clk, NULL);
  1727. if (!stm32f4_pll_ssc_parse_dt(np, &ssc_conf))
  1728. stm32f4_pll_init_ssc(pll_vco_hw, &ssc_conf);
  1729. return;
  1730. fail:
  1731. kfree(clks);
  1732. iounmap(base);
  1733. }
  1734. CLK_OF_DECLARE_DRIVER(stm32f42xx_rcc, "st,stm32f42xx-rcc", stm32f4_rcc_init);
  1735. CLK_OF_DECLARE_DRIVER(stm32f46xx_rcc, "st,stm32f469-rcc", stm32f4_rcc_init);
  1736. CLK_OF_DECLARE_DRIVER(stm32f746_rcc, "st,stm32f746-rcc", stm32f4_rcc_init);
  1737. CLK_OF_DECLARE_DRIVER(stm32f769_rcc, "st,stm32f769-rcc", stm32f4_rcc_init);