clk-stm32f4.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619
  1. /*
  2. * Author: Daniel Thompson <daniel.thompson@linaro.org>
  3. *
  4. * Inspired by clk-asm9260.c .
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with
  16. * this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <linux/clk-provider.h>
  19. #include <linux/err.h>
  20. #include <linux/io.h>
  21. #include <linux/iopoll.h>
  22. #include <linux/ioport.h>
  23. #include <linux/slab.h>
  24. #include <linux/spinlock.h>
  25. #include <linux/of.h>
  26. #include <linux/of_address.h>
  27. #include <linux/regmap.h>
  28. #include <linux/mfd/syscon.h>
  29. /*
  30. * Include list of clocks wich are not derived from system clock (SYSCLOCK)
  31. * The index of these clocks is the secondary index of DT bindings
  32. *
  33. */
  34. #include <dt-bindings/clock/stm32fx-clock.h>
  35. #define STM32F4_RCC_CR 0x00
  36. #define STM32F4_RCC_PLLCFGR 0x04
  37. #define STM32F4_RCC_CFGR 0x08
  38. #define STM32F4_RCC_AHB1ENR 0x30
  39. #define STM32F4_RCC_AHB2ENR 0x34
  40. #define STM32F4_RCC_AHB3ENR 0x38
  41. #define STM32F4_RCC_APB1ENR 0x40
  42. #define STM32F4_RCC_APB2ENR 0x44
  43. #define STM32F4_RCC_BDCR 0x70
  44. #define STM32F4_RCC_CSR 0x74
  45. #define STM32F4_RCC_PLLI2SCFGR 0x84
  46. #define STM32F4_RCC_PLLSAICFGR 0x88
  47. #define STM32F4_RCC_DCKCFGR 0x8c
  48. #define STM32F7_RCC_DCKCFGR2 0x90
  49. #define NONE -1
  50. #define NO_IDX NONE
  51. #define NO_MUX NONE
  52. #define NO_GATE NONE
  53. struct stm32f4_gate_data {
  54. u8 offset;
  55. u8 bit_idx;
  56. const char *name;
  57. const char *parent_name;
  58. unsigned long flags;
  59. };
  60. static const struct stm32f4_gate_data stm32f429_gates[] __initconst = {
  61. { STM32F4_RCC_AHB1ENR, 0, "gpioa", "ahb_div" },
  62. { STM32F4_RCC_AHB1ENR, 1, "gpiob", "ahb_div" },
  63. { STM32F4_RCC_AHB1ENR, 2, "gpioc", "ahb_div" },
  64. { STM32F4_RCC_AHB1ENR, 3, "gpiod", "ahb_div" },
  65. { STM32F4_RCC_AHB1ENR, 4, "gpioe", "ahb_div" },
  66. { STM32F4_RCC_AHB1ENR, 5, "gpiof", "ahb_div" },
  67. { STM32F4_RCC_AHB1ENR, 6, "gpiog", "ahb_div" },
  68. { STM32F4_RCC_AHB1ENR, 7, "gpioh", "ahb_div" },
  69. { STM32F4_RCC_AHB1ENR, 8, "gpioi", "ahb_div" },
  70. { STM32F4_RCC_AHB1ENR, 9, "gpioj", "ahb_div" },
  71. { STM32F4_RCC_AHB1ENR, 10, "gpiok", "ahb_div" },
  72. { STM32F4_RCC_AHB1ENR, 12, "crc", "ahb_div" },
  73. { STM32F4_RCC_AHB1ENR, 18, "bkpsra", "ahb_div" },
  74. { STM32F4_RCC_AHB1ENR, 20, "ccmdatam", "ahb_div" },
  75. { STM32F4_RCC_AHB1ENR, 21, "dma1", "ahb_div" },
  76. { STM32F4_RCC_AHB1ENR, 22, "dma2", "ahb_div" },
  77. { STM32F4_RCC_AHB1ENR, 23, "dma2d", "ahb_div" },
  78. { STM32F4_RCC_AHB1ENR, 25, "ethmac", "ahb_div" },
  79. { STM32F4_RCC_AHB1ENR, 26, "ethmactx", "ahb_div" },
  80. { STM32F4_RCC_AHB1ENR, 27, "ethmacrx", "ahb_div" },
  81. { STM32F4_RCC_AHB1ENR, 28, "ethmacptp", "ahb_div" },
  82. { STM32F4_RCC_AHB1ENR, 29, "otghs", "ahb_div" },
  83. { STM32F4_RCC_AHB1ENR, 30, "otghsulpi", "ahb_div" },
  84. { STM32F4_RCC_AHB2ENR, 0, "dcmi", "ahb_div" },
  85. { STM32F4_RCC_AHB2ENR, 4, "cryp", "ahb_div" },
  86. { STM32F4_RCC_AHB2ENR, 5, "hash", "ahb_div" },
  87. { STM32F4_RCC_AHB2ENR, 6, "rng", "pll48" },
  88. { STM32F4_RCC_AHB2ENR, 7, "otgfs", "pll48" },
  89. { STM32F4_RCC_AHB3ENR, 0, "fmc", "ahb_div",
  90. CLK_IGNORE_UNUSED },
  91. { STM32F4_RCC_APB1ENR, 0, "tim2", "apb1_mul" },
  92. { STM32F4_RCC_APB1ENR, 1, "tim3", "apb1_mul" },
  93. { STM32F4_RCC_APB1ENR, 2, "tim4", "apb1_mul" },
  94. { STM32F4_RCC_APB1ENR, 3, "tim5", "apb1_mul" },
  95. { STM32F4_RCC_APB1ENR, 4, "tim6", "apb1_mul" },
  96. { STM32F4_RCC_APB1ENR, 5, "tim7", "apb1_mul" },
  97. { STM32F4_RCC_APB1ENR, 6, "tim12", "apb1_mul" },
  98. { STM32F4_RCC_APB1ENR, 7, "tim13", "apb1_mul" },
  99. { STM32F4_RCC_APB1ENR, 8, "tim14", "apb1_mul" },
  100. { STM32F4_RCC_APB1ENR, 11, "wwdg", "apb1_div" },
  101. { STM32F4_RCC_APB1ENR, 14, "spi2", "apb1_div" },
  102. { STM32F4_RCC_APB1ENR, 15, "spi3", "apb1_div" },
  103. { STM32F4_RCC_APB1ENR, 17, "uart2", "apb1_div" },
  104. { STM32F4_RCC_APB1ENR, 18, "uart3", "apb1_div" },
  105. { STM32F4_RCC_APB1ENR, 19, "uart4", "apb1_div" },
  106. { STM32F4_RCC_APB1ENR, 20, "uart5", "apb1_div" },
  107. { STM32F4_RCC_APB1ENR, 21, "i2c1", "apb1_div" },
  108. { STM32F4_RCC_APB1ENR, 22, "i2c2", "apb1_div" },
  109. { STM32F4_RCC_APB1ENR, 23, "i2c3", "apb1_div" },
  110. { STM32F4_RCC_APB1ENR, 25, "can1", "apb1_div" },
  111. { STM32F4_RCC_APB1ENR, 26, "can2", "apb1_div" },
  112. { STM32F4_RCC_APB1ENR, 28, "pwr", "apb1_div" },
  113. { STM32F4_RCC_APB1ENR, 29, "dac", "apb1_div" },
  114. { STM32F4_RCC_APB1ENR, 30, "uart7", "apb1_div" },
  115. { STM32F4_RCC_APB1ENR, 31, "uart8", "apb1_div" },
  116. { STM32F4_RCC_APB2ENR, 0, "tim1", "apb2_mul" },
  117. { STM32F4_RCC_APB2ENR, 1, "tim8", "apb2_mul" },
  118. { STM32F4_RCC_APB2ENR, 4, "usart1", "apb2_div" },
  119. { STM32F4_RCC_APB2ENR, 5, "usart6", "apb2_div" },
  120. { STM32F4_RCC_APB2ENR, 8, "adc1", "apb2_div" },
  121. { STM32F4_RCC_APB2ENR, 9, "adc2", "apb2_div" },
  122. { STM32F4_RCC_APB2ENR, 10, "adc3", "apb2_div" },
  123. { STM32F4_RCC_APB2ENR, 11, "sdio", "pll48" },
  124. { STM32F4_RCC_APB2ENR, 12, "spi1", "apb2_div" },
  125. { STM32F4_RCC_APB2ENR, 13, "spi4", "apb2_div" },
  126. { STM32F4_RCC_APB2ENR, 14, "syscfg", "apb2_div" },
  127. { STM32F4_RCC_APB2ENR, 16, "tim9", "apb2_mul" },
  128. { STM32F4_RCC_APB2ENR, 17, "tim10", "apb2_mul" },
  129. { STM32F4_RCC_APB2ENR, 18, "tim11", "apb2_mul" },
  130. { STM32F4_RCC_APB2ENR, 20, "spi5", "apb2_div" },
  131. { STM32F4_RCC_APB2ENR, 21, "spi6", "apb2_div" },
  132. { STM32F4_RCC_APB2ENR, 22, "sai1", "apb2_div" },
  133. { STM32F4_RCC_APB2ENR, 26, "ltdc", "apb2_div" },
  134. };
  135. static const struct stm32f4_gate_data stm32f469_gates[] __initconst = {
  136. { STM32F4_RCC_AHB1ENR, 0, "gpioa", "ahb_div" },
  137. { STM32F4_RCC_AHB1ENR, 1, "gpiob", "ahb_div" },
  138. { STM32F4_RCC_AHB1ENR, 2, "gpioc", "ahb_div" },
  139. { STM32F4_RCC_AHB1ENR, 3, "gpiod", "ahb_div" },
  140. { STM32F4_RCC_AHB1ENR, 4, "gpioe", "ahb_div" },
  141. { STM32F4_RCC_AHB1ENR, 5, "gpiof", "ahb_div" },
  142. { STM32F4_RCC_AHB1ENR, 6, "gpiog", "ahb_div" },
  143. { STM32F4_RCC_AHB1ENR, 7, "gpioh", "ahb_div" },
  144. { STM32F4_RCC_AHB1ENR, 8, "gpioi", "ahb_div" },
  145. { STM32F4_RCC_AHB1ENR, 9, "gpioj", "ahb_div" },
  146. { STM32F4_RCC_AHB1ENR, 10, "gpiok", "ahb_div" },
  147. { STM32F4_RCC_AHB1ENR, 12, "crc", "ahb_div" },
  148. { STM32F4_RCC_AHB1ENR, 18, "bkpsra", "ahb_div" },
  149. { STM32F4_RCC_AHB1ENR, 20, "ccmdatam", "ahb_div" },
  150. { STM32F4_RCC_AHB1ENR, 21, "dma1", "ahb_div" },
  151. { STM32F4_RCC_AHB1ENR, 22, "dma2", "ahb_div" },
  152. { STM32F4_RCC_AHB1ENR, 23, "dma2d", "ahb_div" },
  153. { STM32F4_RCC_AHB1ENR, 25, "ethmac", "ahb_div" },
  154. { STM32F4_RCC_AHB1ENR, 26, "ethmactx", "ahb_div" },
  155. { STM32F4_RCC_AHB1ENR, 27, "ethmacrx", "ahb_div" },
  156. { STM32F4_RCC_AHB1ENR, 28, "ethmacptp", "ahb_div" },
  157. { STM32F4_RCC_AHB1ENR, 29, "otghs", "ahb_div" },
  158. { STM32F4_RCC_AHB1ENR, 30, "otghsulpi", "ahb_div" },
  159. { STM32F4_RCC_AHB2ENR, 0, "dcmi", "ahb_div" },
  160. { STM32F4_RCC_AHB2ENR, 4, "cryp", "ahb_div" },
  161. { STM32F4_RCC_AHB2ENR, 5, "hash", "ahb_div" },
  162. { STM32F4_RCC_AHB2ENR, 6, "rng", "pll48" },
  163. { STM32F4_RCC_AHB2ENR, 7, "otgfs", "pll48" },
  164. { STM32F4_RCC_AHB3ENR, 0, "fmc", "ahb_div",
  165. CLK_IGNORE_UNUSED },
  166. { STM32F4_RCC_AHB3ENR, 1, "qspi", "ahb_div",
  167. CLK_IGNORE_UNUSED },
  168. { STM32F4_RCC_APB1ENR, 0, "tim2", "apb1_mul" },
  169. { STM32F4_RCC_APB1ENR, 1, "tim3", "apb1_mul" },
  170. { STM32F4_RCC_APB1ENR, 2, "tim4", "apb1_mul" },
  171. { STM32F4_RCC_APB1ENR, 3, "tim5", "apb1_mul" },
  172. { STM32F4_RCC_APB1ENR, 4, "tim6", "apb1_mul" },
  173. { STM32F4_RCC_APB1ENR, 5, "tim7", "apb1_mul" },
  174. { STM32F4_RCC_APB1ENR, 6, "tim12", "apb1_mul" },
  175. { STM32F4_RCC_APB1ENR, 7, "tim13", "apb1_mul" },
  176. { STM32F4_RCC_APB1ENR, 8, "tim14", "apb1_mul" },
  177. { STM32F4_RCC_APB1ENR, 11, "wwdg", "apb1_div" },
  178. { STM32F4_RCC_APB1ENR, 14, "spi2", "apb1_div" },
  179. { STM32F4_RCC_APB1ENR, 15, "spi3", "apb1_div" },
  180. { STM32F4_RCC_APB1ENR, 17, "uart2", "apb1_div" },
  181. { STM32F4_RCC_APB1ENR, 18, "uart3", "apb1_div" },
  182. { STM32F4_RCC_APB1ENR, 19, "uart4", "apb1_div" },
  183. { STM32F4_RCC_APB1ENR, 20, "uart5", "apb1_div" },
  184. { STM32F4_RCC_APB1ENR, 21, "i2c1", "apb1_div" },
  185. { STM32F4_RCC_APB1ENR, 22, "i2c2", "apb1_div" },
  186. { STM32F4_RCC_APB1ENR, 23, "i2c3", "apb1_div" },
  187. { STM32F4_RCC_APB1ENR, 25, "can1", "apb1_div" },
  188. { STM32F4_RCC_APB1ENR, 26, "can2", "apb1_div" },
  189. { STM32F4_RCC_APB1ENR, 28, "pwr", "apb1_div" },
  190. { STM32F4_RCC_APB1ENR, 29, "dac", "apb1_div" },
  191. { STM32F4_RCC_APB1ENR, 30, "uart7", "apb1_div" },
  192. { STM32F4_RCC_APB1ENR, 31, "uart8", "apb1_div" },
  193. { STM32F4_RCC_APB2ENR, 0, "tim1", "apb2_mul" },
  194. { STM32F4_RCC_APB2ENR, 1, "tim8", "apb2_mul" },
  195. { STM32F4_RCC_APB2ENR, 4, "usart1", "apb2_div" },
  196. { STM32F4_RCC_APB2ENR, 5, "usart6", "apb2_div" },
  197. { STM32F4_RCC_APB2ENR, 8, "adc1", "apb2_div" },
  198. { STM32F4_RCC_APB2ENR, 9, "adc2", "apb2_div" },
  199. { STM32F4_RCC_APB2ENR, 10, "adc3", "apb2_div" },
  200. { STM32F4_RCC_APB2ENR, 11, "sdio", "sdmux" },
  201. { STM32F4_RCC_APB2ENR, 12, "spi1", "apb2_div" },
  202. { STM32F4_RCC_APB2ENR, 13, "spi4", "apb2_div" },
  203. { STM32F4_RCC_APB2ENR, 14, "syscfg", "apb2_div" },
  204. { STM32F4_RCC_APB2ENR, 16, "tim9", "apb2_mul" },
  205. { STM32F4_RCC_APB2ENR, 17, "tim10", "apb2_mul" },
  206. { STM32F4_RCC_APB2ENR, 18, "tim11", "apb2_mul" },
  207. { STM32F4_RCC_APB2ENR, 20, "spi5", "apb2_div" },
  208. { STM32F4_RCC_APB2ENR, 21, "spi6", "apb2_div" },
  209. { STM32F4_RCC_APB2ENR, 22, "sai1", "apb2_div" },
  210. { STM32F4_RCC_APB2ENR, 26, "ltdc", "apb2_div" },
  211. };
  212. static const struct stm32f4_gate_data stm32f746_gates[] __initconst = {
  213. { STM32F4_RCC_AHB1ENR, 0, "gpioa", "ahb_div" },
  214. { STM32F4_RCC_AHB1ENR, 1, "gpiob", "ahb_div" },
  215. { STM32F4_RCC_AHB1ENR, 2, "gpioc", "ahb_div" },
  216. { STM32F4_RCC_AHB1ENR, 3, "gpiod", "ahb_div" },
  217. { STM32F4_RCC_AHB1ENR, 4, "gpioe", "ahb_div" },
  218. { STM32F4_RCC_AHB1ENR, 5, "gpiof", "ahb_div" },
  219. { STM32F4_RCC_AHB1ENR, 6, "gpiog", "ahb_div" },
  220. { STM32F4_RCC_AHB1ENR, 7, "gpioh", "ahb_div" },
  221. { STM32F4_RCC_AHB1ENR, 8, "gpioi", "ahb_div" },
  222. { STM32F4_RCC_AHB1ENR, 9, "gpioj", "ahb_div" },
  223. { STM32F4_RCC_AHB1ENR, 10, "gpiok", "ahb_div" },
  224. { STM32F4_RCC_AHB1ENR, 12, "crc", "ahb_div" },
  225. { STM32F4_RCC_AHB1ENR, 18, "bkpsra", "ahb_div" },
  226. { STM32F4_RCC_AHB1ENR, 20, "dtcmram", "ahb_div" },
  227. { STM32F4_RCC_AHB1ENR, 21, "dma1", "ahb_div" },
  228. { STM32F4_RCC_AHB1ENR, 22, "dma2", "ahb_div" },
  229. { STM32F4_RCC_AHB1ENR, 23, "dma2d", "ahb_div" },
  230. { STM32F4_RCC_AHB1ENR, 25, "ethmac", "ahb_div" },
  231. { STM32F4_RCC_AHB1ENR, 26, "ethmactx", "ahb_div" },
  232. { STM32F4_RCC_AHB1ENR, 27, "ethmacrx", "ahb_div" },
  233. { STM32F4_RCC_AHB1ENR, 28, "ethmacptp", "ahb_div" },
  234. { STM32F4_RCC_AHB1ENR, 29, "otghs", "ahb_div" },
  235. { STM32F4_RCC_AHB1ENR, 30, "otghsulpi", "ahb_div" },
  236. { STM32F4_RCC_AHB2ENR, 0, "dcmi", "ahb_div" },
  237. { STM32F4_RCC_AHB2ENR, 4, "cryp", "ahb_div" },
  238. { STM32F4_RCC_AHB2ENR, 5, "hash", "ahb_div" },
  239. { STM32F4_RCC_AHB2ENR, 6, "rng", "pll48" },
  240. { STM32F4_RCC_AHB2ENR, 7, "otgfs", "pll48" },
  241. { STM32F4_RCC_AHB3ENR, 0, "fmc", "ahb_div",
  242. CLK_IGNORE_UNUSED },
  243. { STM32F4_RCC_AHB3ENR, 1, "qspi", "ahb_div",
  244. CLK_IGNORE_UNUSED },
  245. { STM32F4_RCC_APB1ENR, 0, "tim2", "apb1_mul" },
  246. { STM32F4_RCC_APB1ENR, 1, "tim3", "apb1_mul" },
  247. { STM32F4_RCC_APB1ENR, 2, "tim4", "apb1_mul" },
  248. { STM32F4_RCC_APB1ENR, 3, "tim5", "apb1_mul" },
  249. { STM32F4_RCC_APB1ENR, 4, "tim6", "apb1_mul" },
  250. { STM32F4_RCC_APB1ENR, 5, "tim7", "apb1_mul" },
  251. { STM32F4_RCC_APB1ENR, 6, "tim12", "apb1_mul" },
  252. { STM32F4_RCC_APB1ENR, 7, "tim13", "apb1_mul" },
  253. { STM32F4_RCC_APB1ENR, 8, "tim14", "apb1_mul" },
  254. { STM32F4_RCC_APB1ENR, 11, "wwdg", "apb1_div" },
  255. { STM32F4_RCC_APB1ENR, 14, "spi2", "apb1_div" },
  256. { STM32F4_RCC_APB1ENR, 15, "spi3", "apb1_div" },
  257. { STM32F4_RCC_APB1ENR, 16, "spdifrx", "apb1_div" },
  258. { STM32F4_RCC_APB1ENR, 25, "can1", "apb1_div" },
  259. { STM32F4_RCC_APB1ENR, 26, "can2", "apb1_div" },
  260. { STM32F4_RCC_APB1ENR, 27, "cec", "apb1_div" },
  261. { STM32F4_RCC_APB1ENR, 28, "pwr", "apb1_div" },
  262. { STM32F4_RCC_APB1ENR, 29, "dac", "apb1_div" },
  263. { STM32F4_RCC_APB2ENR, 0, "tim1", "apb2_mul" },
  264. { STM32F4_RCC_APB2ENR, 1, "tim8", "apb2_mul" },
  265. { STM32F4_RCC_APB2ENR, 8, "adc1", "apb2_div" },
  266. { STM32F4_RCC_APB2ENR, 9, "adc2", "apb2_div" },
  267. { STM32F4_RCC_APB2ENR, 10, "adc3", "apb2_div" },
  268. { STM32F4_RCC_APB2ENR, 11, "sdmmc", "sdmux" },
  269. { STM32F4_RCC_APB2ENR, 12, "spi1", "apb2_div" },
  270. { STM32F4_RCC_APB2ENR, 13, "spi4", "apb2_div" },
  271. { STM32F4_RCC_APB2ENR, 14, "syscfg", "apb2_div" },
  272. { STM32F4_RCC_APB2ENR, 16, "tim9", "apb2_mul" },
  273. { STM32F4_RCC_APB2ENR, 17, "tim10", "apb2_mul" },
  274. { STM32F4_RCC_APB2ENR, 18, "tim11", "apb2_mul" },
  275. { STM32F4_RCC_APB2ENR, 20, "spi5", "apb2_div" },
  276. { STM32F4_RCC_APB2ENR, 21, "spi6", "apb2_div" },
  277. { STM32F4_RCC_APB2ENR, 22, "sai1", "apb2_div" },
  278. { STM32F4_RCC_APB2ENR, 23, "sai2", "apb2_div" },
  279. { STM32F4_RCC_APB2ENR, 26, "ltdc", "apb2_div" },
  280. };
  281. /*
  282. * This bitmask tells us which bit offsets (0..192) on STM32F4[23]xxx
  283. * have gate bits associated with them. Its combined hweight is 71.
  284. */
  285. #define MAX_GATE_MAP 3
  286. static const u64 stm32f42xx_gate_map[MAX_GATE_MAP] = { 0x000000f17ef417ffull,
  287. 0x0000000000000001ull,
  288. 0x04777f33f6fec9ffull };
  289. static const u64 stm32f46xx_gate_map[MAX_GATE_MAP] = { 0x000000f17ef417ffull,
  290. 0x0000000000000003ull,
  291. 0x0c777f33f6fec9ffull };
  292. static const u64 stm32f746_gate_map[MAX_GATE_MAP] = { 0x000000f17ef417ffull,
  293. 0x0000000000000003ull,
  294. 0x04f77f033e01c9ffull };
  295. static const u64 *stm32f4_gate_map;
  296. static struct clk_hw **clks;
  297. static DEFINE_SPINLOCK(stm32f4_clk_lock);
  298. static void __iomem *base;
  299. static struct regmap *pdrm;
  300. static int stm32fx_end_primary_clk;
  301. /*
  302. * "Multiplier" device for APBx clocks.
  303. *
  304. * The APBx dividers are power-of-two dividers and, if *not* running in 1:1
  305. * mode, they also tap out the one of the low order state bits to run the
  306. * timers. ST datasheets represent this feature as a (conditional) clock
  307. * multiplier.
  308. */
  309. struct clk_apb_mul {
  310. struct clk_hw hw;
  311. u8 bit_idx;
  312. };
  313. #define to_clk_apb_mul(_hw) container_of(_hw, struct clk_apb_mul, hw)
  314. static unsigned long clk_apb_mul_recalc_rate(struct clk_hw *hw,
  315. unsigned long parent_rate)
  316. {
  317. struct clk_apb_mul *am = to_clk_apb_mul(hw);
  318. if (readl(base + STM32F4_RCC_CFGR) & BIT(am->bit_idx))
  319. return parent_rate * 2;
  320. return parent_rate;
  321. }
  322. static long clk_apb_mul_round_rate(struct clk_hw *hw, unsigned long rate,
  323. unsigned long *prate)
  324. {
  325. struct clk_apb_mul *am = to_clk_apb_mul(hw);
  326. unsigned long mult = 1;
  327. if (readl(base + STM32F4_RCC_CFGR) & BIT(am->bit_idx))
  328. mult = 2;
  329. if (clk_hw_get_flags(hw) & CLK_SET_RATE_PARENT) {
  330. unsigned long best_parent = rate / mult;
  331. *prate = clk_hw_round_rate(clk_hw_get_parent(hw), best_parent);
  332. }
  333. return *prate * mult;
  334. }
  335. static int clk_apb_mul_set_rate(struct clk_hw *hw, unsigned long rate,
  336. unsigned long parent_rate)
  337. {
  338. /*
  339. * We must report success but we can do so unconditionally because
  340. * clk_apb_mul_round_rate returns values that ensure this call is a
  341. * nop.
  342. */
  343. return 0;
  344. }
  345. static const struct clk_ops clk_apb_mul_factor_ops = {
  346. .round_rate = clk_apb_mul_round_rate,
  347. .set_rate = clk_apb_mul_set_rate,
  348. .recalc_rate = clk_apb_mul_recalc_rate,
  349. };
  350. static struct clk *clk_register_apb_mul(struct device *dev, const char *name,
  351. const char *parent_name,
  352. unsigned long flags, u8 bit_idx)
  353. {
  354. struct clk_apb_mul *am;
  355. struct clk_init_data init;
  356. struct clk *clk;
  357. am = kzalloc(sizeof(*am), GFP_KERNEL);
  358. if (!am)
  359. return ERR_PTR(-ENOMEM);
  360. am->bit_idx = bit_idx;
  361. am->hw.init = &init;
  362. init.name = name;
  363. init.ops = &clk_apb_mul_factor_ops;
  364. init.flags = flags;
  365. init.parent_names = &parent_name;
  366. init.num_parents = 1;
  367. clk = clk_register(dev, &am->hw);
  368. if (IS_ERR(clk))
  369. kfree(am);
  370. return clk;
  371. }
  372. enum {
  373. PLL,
  374. PLL_I2S,
  375. PLL_SAI,
  376. };
  377. static const struct clk_div_table pll_divp_table[] = {
  378. { 0, 2 }, { 1, 4 }, { 2, 6 }, { 3, 8 }, { 0 }
  379. };
  380. static const struct clk_div_table pll_divq_table[] = {
  381. { 2, 2 }, { 3, 3 }, { 4, 4 }, { 5, 5 }, { 6, 6 }, { 7, 7 },
  382. { 8, 8 }, { 9, 9 }, { 10, 10 }, { 11, 11 }, { 12, 12 }, { 13, 13 },
  383. { 14, 14 }, { 15, 15 },
  384. { 0 }
  385. };
  386. static const struct clk_div_table pll_divr_table[] = {
  387. { 2, 2 }, { 3, 3 }, { 4, 4 }, { 5, 5 }, { 6, 6 }, { 7, 7 }, { 0 }
  388. };
  389. struct stm32f4_pll {
  390. spinlock_t *lock;
  391. struct clk_gate gate;
  392. u8 offset;
  393. u8 bit_rdy_idx;
  394. u8 status;
  395. u8 n_start;
  396. };
  397. #define to_stm32f4_pll(_gate) container_of(_gate, struct stm32f4_pll, gate)
  398. struct stm32f4_pll_post_div_data {
  399. int idx;
  400. u8 pll_num;
  401. const char *name;
  402. const char *parent;
  403. u8 flag;
  404. u8 offset;
  405. u8 shift;
  406. u8 width;
  407. u8 flag_div;
  408. const struct clk_div_table *div_table;
  409. };
  410. struct stm32f4_vco_data {
  411. const char *vco_name;
  412. u8 offset;
  413. u8 bit_idx;
  414. u8 bit_rdy_idx;
  415. };
  416. static const struct stm32f4_vco_data vco_data[] = {
  417. { "vco", STM32F4_RCC_PLLCFGR, 24, 25 },
  418. { "vco-i2s", STM32F4_RCC_PLLI2SCFGR, 26, 27 },
  419. { "vco-sai", STM32F4_RCC_PLLSAICFGR, 28, 29 },
  420. };
  421. static const struct clk_div_table post_divr_table[] = {
  422. { 0, 2 }, { 1, 4 }, { 2, 8 }, { 3, 16 }, { 0 }
  423. };
  424. #define MAX_POST_DIV 3
  425. static const struct stm32f4_pll_post_div_data post_div_data[MAX_POST_DIV] = {
  426. { CLK_I2SQ_PDIV, PLL_I2S, "plli2s-q-div", "plli2s-q",
  427. CLK_SET_RATE_PARENT, STM32F4_RCC_DCKCFGR, 0, 5, 0, NULL},
  428. { CLK_SAIQ_PDIV, PLL_SAI, "pllsai-q-div", "pllsai-q",
  429. CLK_SET_RATE_PARENT, STM32F4_RCC_DCKCFGR, 8, 5, 0, NULL },
  430. { NO_IDX, PLL_SAI, "pllsai-r-div", "pllsai-r", CLK_SET_RATE_PARENT,
  431. STM32F4_RCC_DCKCFGR, 16, 2, 0, post_divr_table },
  432. };
  433. struct stm32f4_div_data {
  434. u8 shift;
  435. u8 width;
  436. u8 flag_div;
  437. const struct clk_div_table *div_table;
  438. };
  439. #define MAX_PLL_DIV 3
  440. static const struct stm32f4_div_data div_data[MAX_PLL_DIV] = {
  441. { 16, 2, 0, pll_divp_table },
  442. { 24, 4, 0, pll_divq_table },
  443. { 28, 3, 0, pll_divr_table },
  444. };
  445. struct stm32f4_pll_data {
  446. u8 pll_num;
  447. u8 n_start;
  448. const char *div_name[MAX_PLL_DIV];
  449. };
  450. static const struct stm32f4_pll_data stm32f429_pll[MAX_PLL_DIV] = {
  451. { PLL, 192, { "pll", "pll48", NULL } },
  452. { PLL_I2S, 192, { NULL, "plli2s-q", "plli2s-r" } },
  453. { PLL_SAI, 49, { NULL, "pllsai-q", "pllsai-r" } },
  454. };
  455. static const struct stm32f4_pll_data stm32f469_pll[MAX_PLL_DIV] = {
  456. { PLL, 50, { "pll", "pll-q", NULL } },
  457. { PLL_I2S, 50, { "plli2s-p", "plli2s-q", "plli2s-r" } },
  458. { PLL_SAI, 50, { "pllsai-p", "pllsai-q", "pllsai-r" } },
  459. };
  460. static int stm32f4_pll_is_enabled(struct clk_hw *hw)
  461. {
  462. return clk_gate_ops.is_enabled(hw);
  463. }
  464. #define PLL_TIMEOUT 10000
  465. static int stm32f4_pll_enable(struct clk_hw *hw)
  466. {
  467. struct clk_gate *gate = to_clk_gate(hw);
  468. struct stm32f4_pll *pll = to_stm32f4_pll(gate);
  469. int bit_status;
  470. unsigned int timeout = PLL_TIMEOUT;
  471. if (clk_gate_ops.is_enabled(hw))
  472. return 0;
  473. clk_gate_ops.enable(hw);
  474. do {
  475. bit_status = !(readl(gate->reg) & BIT(pll->bit_rdy_idx));
  476. } while (bit_status && --timeout);
  477. return bit_status;
  478. }
  479. static void stm32f4_pll_disable(struct clk_hw *hw)
  480. {
  481. clk_gate_ops.disable(hw);
  482. }
  483. static unsigned long stm32f4_pll_recalc(struct clk_hw *hw,
  484. unsigned long parent_rate)
  485. {
  486. struct clk_gate *gate = to_clk_gate(hw);
  487. struct stm32f4_pll *pll = to_stm32f4_pll(gate);
  488. unsigned long n;
  489. n = (readl(base + pll->offset) >> 6) & 0x1ff;
  490. return parent_rate * n;
  491. }
  492. static long stm32f4_pll_round_rate(struct clk_hw *hw, unsigned long rate,
  493. unsigned long *prate)
  494. {
  495. struct clk_gate *gate = to_clk_gate(hw);
  496. struct stm32f4_pll *pll = to_stm32f4_pll(gate);
  497. unsigned long n;
  498. n = rate / *prate;
  499. if (n < pll->n_start)
  500. n = pll->n_start;
  501. else if (n > 432)
  502. n = 432;
  503. return *prate * n;
  504. }
  505. static int stm32f4_pll_set_rate(struct clk_hw *hw, unsigned long rate,
  506. unsigned long parent_rate)
  507. {
  508. struct clk_gate *gate = to_clk_gate(hw);
  509. struct stm32f4_pll *pll = to_stm32f4_pll(gate);
  510. unsigned long n;
  511. unsigned long val;
  512. int pll_state;
  513. pll_state = stm32f4_pll_is_enabled(hw);
  514. if (pll_state)
  515. stm32f4_pll_disable(hw);
  516. n = rate / parent_rate;
  517. val = readl(base + pll->offset) & ~(0x1ff << 6);
  518. writel(val | ((n & 0x1ff) << 6), base + pll->offset);
  519. if (pll_state)
  520. stm32f4_pll_enable(hw);
  521. return 0;
  522. }
  523. static const struct clk_ops stm32f4_pll_gate_ops = {
  524. .enable = stm32f4_pll_enable,
  525. .disable = stm32f4_pll_disable,
  526. .is_enabled = stm32f4_pll_is_enabled,
  527. .recalc_rate = stm32f4_pll_recalc,
  528. .round_rate = stm32f4_pll_round_rate,
  529. .set_rate = stm32f4_pll_set_rate,
  530. };
  531. struct stm32f4_pll_div {
  532. struct clk_divider div;
  533. struct clk_hw *hw_pll;
  534. };
  535. #define to_pll_div_clk(_div) container_of(_div, struct stm32f4_pll_div, div)
  536. static unsigned long stm32f4_pll_div_recalc_rate(struct clk_hw *hw,
  537. unsigned long parent_rate)
  538. {
  539. return clk_divider_ops.recalc_rate(hw, parent_rate);
  540. }
  541. static long stm32f4_pll_div_round_rate(struct clk_hw *hw, unsigned long rate,
  542. unsigned long *prate)
  543. {
  544. return clk_divider_ops.round_rate(hw, rate, prate);
  545. }
  546. static int stm32f4_pll_div_set_rate(struct clk_hw *hw, unsigned long rate,
  547. unsigned long parent_rate)
  548. {
  549. int pll_state, ret;
  550. struct clk_divider *div = to_clk_divider(hw);
  551. struct stm32f4_pll_div *pll_div = to_pll_div_clk(div);
  552. pll_state = stm32f4_pll_is_enabled(pll_div->hw_pll);
  553. if (pll_state)
  554. stm32f4_pll_disable(pll_div->hw_pll);
  555. ret = clk_divider_ops.set_rate(hw, rate, parent_rate);
  556. if (pll_state)
  557. stm32f4_pll_enable(pll_div->hw_pll);
  558. return ret;
  559. }
  560. static const struct clk_ops stm32f4_pll_div_ops = {
  561. .recalc_rate = stm32f4_pll_div_recalc_rate,
  562. .round_rate = stm32f4_pll_div_round_rate,
  563. .set_rate = stm32f4_pll_div_set_rate,
  564. };
  565. static struct clk_hw *clk_register_pll_div(const char *name,
  566. const char *parent_name, unsigned long flags,
  567. void __iomem *reg, u8 shift, u8 width,
  568. u8 clk_divider_flags, const struct clk_div_table *table,
  569. struct clk_hw *pll_hw, spinlock_t *lock)
  570. {
  571. struct stm32f4_pll_div *pll_div;
  572. struct clk_hw *hw;
  573. struct clk_init_data init;
  574. int ret;
  575. /* allocate the divider */
  576. pll_div = kzalloc(sizeof(*pll_div), GFP_KERNEL);
  577. if (!pll_div)
  578. return ERR_PTR(-ENOMEM);
  579. init.name = name;
  580. init.ops = &stm32f4_pll_div_ops;
  581. init.flags = flags;
  582. init.parent_names = (parent_name ? &parent_name : NULL);
  583. init.num_parents = (parent_name ? 1 : 0);
  584. /* struct clk_divider assignments */
  585. pll_div->div.reg = reg;
  586. pll_div->div.shift = shift;
  587. pll_div->div.width = width;
  588. pll_div->div.flags = clk_divider_flags;
  589. pll_div->div.lock = lock;
  590. pll_div->div.table = table;
  591. pll_div->div.hw.init = &init;
  592. pll_div->hw_pll = pll_hw;
  593. /* register the clock */
  594. hw = &pll_div->div.hw;
  595. ret = clk_hw_register(NULL, hw);
  596. if (ret) {
  597. kfree(pll_div);
  598. hw = ERR_PTR(ret);
  599. }
  600. return hw;
  601. }
  602. static struct clk_hw *stm32f4_rcc_register_pll(const char *pllsrc,
  603. const struct stm32f4_pll_data *data, spinlock_t *lock)
  604. {
  605. struct stm32f4_pll *pll;
  606. struct clk_init_data init = { NULL };
  607. void __iomem *reg;
  608. struct clk_hw *pll_hw;
  609. int ret;
  610. int i;
  611. const struct stm32f4_vco_data *vco;
  612. pll = kzalloc(sizeof(*pll), GFP_KERNEL);
  613. if (!pll)
  614. return ERR_PTR(-ENOMEM);
  615. vco = &vco_data[data->pll_num];
  616. init.name = vco->vco_name;
  617. init.ops = &stm32f4_pll_gate_ops;
  618. init.flags = CLK_SET_RATE_GATE;
  619. init.parent_names = &pllsrc;
  620. init.num_parents = 1;
  621. pll->gate.lock = lock;
  622. pll->gate.reg = base + STM32F4_RCC_CR;
  623. pll->gate.bit_idx = vco->bit_idx;
  624. pll->gate.hw.init = &init;
  625. pll->offset = vco->offset;
  626. pll->n_start = data->n_start;
  627. pll->bit_rdy_idx = vco->bit_rdy_idx;
  628. pll->status = (readl(base + STM32F4_RCC_CR) >> vco->bit_idx) & 0x1;
  629. reg = base + pll->offset;
  630. pll_hw = &pll->gate.hw;
  631. ret = clk_hw_register(NULL, pll_hw);
  632. if (ret) {
  633. kfree(pll);
  634. return ERR_PTR(ret);
  635. }
  636. for (i = 0; i < MAX_PLL_DIV; i++)
  637. if (data->div_name[i])
  638. clk_register_pll_div(data->div_name[i],
  639. vco->vco_name,
  640. 0,
  641. reg,
  642. div_data[i].shift,
  643. div_data[i].width,
  644. div_data[i].flag_div,
  645. div_data[i].div_table,
  646. pll_hw,
  647. lock);
  648. return pll_hw;
  649. }
  650. /*
  651. * Converts the primary and secondary indices (as they appear in DT) to an
  652. * offset into our struct clock array.
  653. */
  654. static int stm32f4_rcc_lookup_clk_idx(u8 primary, u8 secondary)
  655. {
  656. u64 table[MAX_GATE_MAP];
  657. if (primary == 1) {
  658. if (WARN_ON(secondary >= stm32fx_end_primary_clk))
  659. return -EINVAL;
  660. return secondary;
  661. }
  662. memcpy(table, stm32f4_gate_map, sizeof(table));
  663. /* only bits set in table can be used as indices */
  664. if (WARN_ON(secondary >= BITS_PER_BYTE * sizeof(table) ||
  665. 0 == (table[BIT_ULL_WORD(secondary)] &
  666. BIT_ULL_MASK(secondary))))
  667. return -EINVAL;
  668. /* mask out bits above our current index */
  669. table[BIT_ULL_WORD(secondary)] &=
  670. GENMASK_ULL(secondary % BITS_PER_LONG_LONG, 0);
  671. return stm32fx_end_primary_clk - 1 + hweight64(table[0]) +
  672. (BIT_ULL_WORD(secondary) >= 1 ? hweight64(table[1]) : 0) +
  673. (BIT_ULL_WORD(secondary) >= 2 ? hweight64(table[2]) : 0);
  674. }
  675. static struct clk_hw *
  676. stm32f4_rcc_lookup_clk(struct of_phandle_args *clkspec, void *data)
  677. {
  678. int i = stm32f4_rcc_lookup_clk_idx(clkspec->args[0], clkspec->args[1]);
  679. if (i < 0)
  680. return ERR_PTR(-EINVAL);
  681. return clks[i];
  682. }
  683. #define to_rgclk(_rgate) container_of(_rgate, struct stm32_rgate, gate)
  684. static inline void disable_power_domain_write_protection(void)
  685. {
  686. if (pdrm)
  687. regmap_update_bits(pdrm, 0x00, (1 << 8), (1 << 8));
  688. }
  689. static inline void enable_power_domain_write_protection(void)
  690. {
  691. if (pdrm)
  692. regmap_update_bits(pdrm, 0x00, (1 << 8), (0 << 8));
  693. }
  694. static inline void sofware_reset_backup_domain(void)
  695. {
  696. unsigned long val;
  697. val = readl(base + STM32F4_RCC_BDCR);
  698. writel(val | BIT(16), base + STM32F4_RCC_BDCR);
  699. writel(val & ~BIT(16), base + STM32F4_RCC_BDCR);
  700. }
  701. struct stm32_rgate {
  702. struct clk_gate gate;
  703. u8 bit_rdy_idx;
  704. };
  705. #define RGATE_TIMEOUT 50000
  706. static int rgclk_enable(struct clk_hw *hw)
  707. {
  708. struct clk_gate *gate = to_clk_gate(hw);
  709. struct stm32_rgate *rgate = to_rgclk(gate);
  710. int bit_status;
  711. unsigned int timeout = RGATE_TIMEOUT;
  712. if (clk_gate_ops.is_enabled(hw))
  713. return 0;
  714. disable_power_domain_write_protection();
  715. clk_gate_ops.enable(hw);
  716. do {
  717. bit_status = !(readl(gate->reg) & BIT(rgate->bit_rdy_idx));
  718. if (bit_status)
  719. udelay(100);
  720. } while (bit_status && --timeout);
  721. enable_power_domain_write_protection();
  722. return bit_status;
  723. }
  724. static void rgclk_disable(struct clk_hw *hw)
  725. {
  726. clk_gate_ops.disable(hw);
  727. }
  728. static int rgclk_is_enabled(struct clk_hw *hw)
  729. {
  730. return clk_gate_ops.is_enabled(hw);
  731. }
  732. static const struct clk_ops rgclk_ops = {
  733. .enable = rgclk_enable,
  734. .disable = rgclk_disable,
  735. .is_enabled = rgclk_is_enabled,
  736. };
  737. static struct clk_hw *clk_register_rgate(struct device *dev, const char *name,
  738. const char *parent_name, unsigned long flags,
  739. void __iomem *reg, u8 bit_idx, u8 bit_rdy_idx,
  740. u8 clk_gate_flags, spinlock_t *lock)
  741. {
  742. struct stm32_rgate *rgate;
  743. struct clk_init_data init = { NULL };
  744. struct clk_hw *hw;
  745. int ret;
  746. rgate = kzalloc(sizeof(*rgate), GFP_KERNEL);
  747. if (!rgate)
  748. return ERR_PTR(-ENOMEM);
  749. init.name = name;
  750. init.ops = &rgclk_ops;
  751. init.flags = flags;
  752. init.parent_names = &parent_name;
  753. init.num_parents = 1;
  754. rgate->bit_rdy_idx = bit_rdy_idx;
  755. rgate->gate.lock = lock;
  756. rgate->gate.reg = reg;
  757. rgate->gate.bit_idx = bit_idx;
  758. rgate->gate.hw.init = &init;
  759. hw = &rgate->gate.hw;
  760. ret = clk_hw_register(dev, hw);
  761. if (ret) {
  762. kfree(rgate);
  763. hw = ERR_PTR(ret);
  764. }
  765. return hw;
  766. }
  767. static int cclk_gate_enable(struct clk_hw *hw)
  768. {
  769. int ret;
  770. disable_power_domain_write_protection();
  771. ret = clk_gate_ops.enable(hw);
  772. enable_power_domain_write_protection();
  773. return ret;
  774. }
  775. static void cclk_gate_disable(struct clk_hw *hw)
  776. {
  777. disable_power_domain_write_protection();
  778. clk_gate_ops.disable(hw);
  779. enable_power_domain_write_protection();
  780. }
  781. static int cclk_gate_is_enabled(struct clk_hw *hw)
  782. {
  783. return clk_gate_ops.is_enabled(hw);
  784. }
  785. static const struct clk_ops cclk_gate_ops = {
  786. .enable = cclk_gate_enable,
  787. .disable = cclk_gate_disable,
  788. .is_enabled = cclk_gate_is_enabled,
  789. };
  790. static u8 cclk_mux_get_parent(struct clk_hw *hw)
  791. {
  792. return clk_mux_ops.get_parent(hw);
  793. }
  794. static int cclk_mux_set_parent(struct clk_hw *hw, u8 index)
  795. {
  796. int ret;
  797. disable_power_domain_write_protection();
  798. sofware_reset_backup_domain();
  799. ret = clk_mux_ops.set_parent(hw, index);
  800. enable_power_domain_write_protection();
  801. return ret;
  802. }
  803. static const struct clk_ops cclk_mux_ops = {
  804. .get_parent = cclk_mux_get_parent,
  805. .set_parent = cclk_mux_set_parent,
  806. };
  807. static struct clk_hw *stm32_register_cclk(struct device *dev, const char *name,
  808. const char * const *parent_names, int num_parents,
  809. void __iomem *reg, u8 bit_idx, u8 shift, unsigned long flags,
  810. spinlock_t *lock)
  811. {
  812. struct clk_hw *hw;
  813. struct clk_gate *gate;
  814. struct clk_mux *mux;
  815. gate = kzalloc(sizeof(*gate), GFP_KERNEL);
  816. if (!gate) {
  817. hw = ERR_PTR(-EINVAL);
  818. goto fail;
  819. }
  820. mux = kzalloc(sizeof(*mux), GFP_KERNEL);
  821. if (!mux) {
  822. kfree(gate);
  823. hw = ERR_PTR(-EINVAL);
  824. goto fail;
  825. }
  826. gate->reg = reg;
  827. gate->bit_idx = bit_idx;
  828. gate->flags = 0;
  829. gate->lock = lock;
  830. mux->reg = reg;
  831. mux->shift = shift;
  832. mux->mask = 3;
  833. mux->flags = 0;
  834. hw = clk_hw_register_composite(dev, name, parent_names, num_parents,
  835. &mux->hw, &cclk_mux_ops,
  836. NULL, NULL,
  837. &gate->hw, &cclk_gate_ops,
  838. flags);
  839. if (IS_ERR(hw)) {
  840. kfree(gate);
  841. kfree(mux);
  842. }
  843. fail:
  844. return hw;
  845. }
  846. static const char *sys_parents[] __initdata = { "hsi", NULL, "pll" };
  847. static const struct clk_div_table ahb_div_table[] = {
  848. { 0x0, 1 }, { 0x1, 1 }, { 0x2, 1 }, { 0x3, 1 },
  849. { 0x4, 1 }, { 0x5, 1 }, { 0x6, 1 }, { 0x7, 1 },
  850. { 0x8, 2 }, { 0x9, 4 }, { 0xa, 8 }, { 0xb, 16 },
  851. { 0xc, 64 }, { 0xd, 128 }, { 0xe, 256 }, { 0xf, 512 },
  852. { 0 },
  853. };
  854. static const struct clk_div_table apb_div_table[] = {
  855. { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 },
  856. { 4, 2 }, { 5, 4 }, { 6, 8 }, { 7, 16 },
  857. { 0 },
  858. };
  859. static const char *rtc_parents[4] = {
  860. "no-clock", "lse", "lsi", "hse-rtc"
  861. };
  862. static const char *lcd_parent[1] = { "pllsai-r-div" };
  863. static const char *i2s_parents[2] = { "plli2s-r", NULL };
  864. static const char *sai_parents[4] = { "pllsai-q-div", "plli2s-q-div", NULL,
  865. "no-clock" };
  866. static const char *pll48_parents[2] = { "pll-q", "pllsai-p" };
  867. static const char *sdmux_parents[2] = { "pll48", "sys" };
  868. static const char *hdmi_parents[2] = { "lse", "hsi_div488" };
  869. static const char *spdif_parent[1] = { "plli2s-p" };
  870. static const char *lptim_parent[4] = { "apb1_mul", "lsi", "hsi", "lse" };
  871. static const char *uart_parents1[4] = { "apb2_div", "sys", "hsi", "lse" };
  872. static const char *uart_parents2[4] = { "apb1_div", "sys", "hsi", "lse" };
  873. static const char *i2c_parents[4] = { "apb1_div", "sys", "hsi", "no-clock" };
  874. struct stm32_aux_clk {
  875. int idx;
  876. const char *name;
  877. const char * const *parent_names;
  878. int num_parents;
  879. int offset_mux;
  880. u8 shift;
  881. u8 mask;
  882. int offset_gate;
  883. u8 bit_idx;
  884. unsigned long flags;
  885. };
  886. struct stm32f4_clk_data {
  887. const struct stm32f4_gate_data *gates_data;
  888. const u64 *gates_map;
  889. int gates_num;
  890. const struct stm32f4_pll_data *pll_data;
  891. const struct stm32_aux_clk *aux_clk;
  892. int aux_clk_num;
  893. int end_primary;
  894. };
  895. static const struct stm32_aux_clk stm32f429_aux_clk[] = {
  896. {
  897. CLK_LCD, "lcd-tft", lcd_parent, ARRAY_SIZE(lcd_parent),
  898. NO_MUX, 0, 0,
  899. STM32F4_RCC_APB2ENR, 26,
  900. CLK_SET_RATE_PARENT
  901. },
  902. {
  903. CLK_I2S, "i2s", i2s_parents, ARRAY_SIZE(i2s_parents),
  904. STM32F4_RCC_CFGR, 23, 1,
  905. NO_GATE, 0,
  906. CLK_SET_RATE_PARENT
  907. },
  908. {
  909. CLK_SAI1, "sai1-a", sai_parents, ARRAY_SIZE(sai_parents),
  910. STM32F4_RCC_DCKCFGR, 20, 3,
  911. STM32F4_RCC_APB2ENR, 22,
  912. CLK_SET_RATE_PARENT
  913. },
  914. {
  915. CLK_SAI2, "sai1-b", sai_parents, ARRAY_SIZE(sai_parents),
  916. STM32F4_RCC_DCKCFGR, 22, 3,
  917. STM32F4_RCC_APB2ENR, 22,
  918. CLK_SET_RATE_PARENT
  919. },
  920. };
  921. static const struct stm32_aux_clk stm32f469_aux_clk[] = {
  922. {
  923. CLK_LCD, "lcd-tft", lcd_parent, ARRAY_SIZE(lcd_parent),
  924. NO_MUX, 0, 0,
  925. STM32F4_RCC_APB2ENR, 26,
  926. CLK_SET_RATE_PARENT
  927. },
  928. {
  929. CLK_I2S, "i2s", i2s_parents, ARRAY_SIZE(i2s_parents),
  930. STM32F4_RCC_CFGR, 23, 1,
  931. NO_GATE, 0,
  932. CLK_SET_RATE_PARENT
  933. },
  934. {
  935. CLK_SAI1, "sai1-a", sai_parents, ARRAY_SIZE(sai_parents),
  936. STM32F4_RCC_DCKCFGR, 20, 3,
  937. STM32F4_RCC_APB2ENR, 22,
  938. CLK_SET_RATE_PARENT
  939. },
  940. {
  941. CLK_SAI2, "sai1-b", sai_parents, ARRAY_SIZE(sai_parents),
  942. STM32F4_RCC_DCKCFGR, 22, 3,
  943. STM32F4_RCC_APB2ENR, 22,
  944. CLK_SET_RATE_PARENT
  945. },
  946. {
  947. NO_IDX, "pll48", pll48_parents, ARRAY_SIZE(pll48_parents),
  948. STM32F4_RCC_DCKCFGR, 27, 1,
  949. NO_GATE, 0,
  950. 0
  951. },
  952. {
  953. NO_IDX, "sdmux", sdmux_parents, ARRAY_SIZE(sdmux_parents),
  954. STM32F4_RCC_DCKCFGR, 28, 1,
  955. NO_GATE, 0,
  956. 0
  957. },
  958. };
  959. static const struct stm32_aux_clk stm32f746_aux_clk[] = {
  960. {
  961. CLK_LCD, "lcd-tft", lcd_parent, ARRAY_SIZE(lcd_parent),
  962. NO_MUX, 0, 0,
  963. STM32F4_RCC_APB2ENR, 26,
  964. CLK_SET_RATE_PARENT
  965. },
  966. {
  967. CLK_I2S, "i2s", i2s_parents, ARRAY_SIZE(i2s_parents),
  968. STM32F4_RCC_CFGR, 23, 1,
  969. NO_GATE, 0,
  970. CLK_SET_RATE_PARENT
  971. },
  972. {
  973. CLK_SAI1, "sai1_clk", sai_parents, ARRAY_SIZE(sai_parents),
  974. STM32F4_RCC_DCKCFGR, 20, 3,
  975. STM32F4_RCC_APB2ENR, 22,
  976. CLK_SET_RATE_PARENT
  977. },
  978. {
  979. CLK_SAI2, "sai2_clk", sai_parents, ARRAY_SIZE(sai_parents),
  980. STM32F4_RCC_DCKCFGR, 22, 3,
  981. STM32F4_RCC_APB2ENR, 23,
  982. CLK_SET_RATE_PARENT
  983. },
  984. {
  985. NO_IDX, "pll48", pll48_parents, ARRAY_SIZE(pll48_parents),
  986. STM32F7_RCC_DCKCFGR2, 27, 1,
  987. NO_GATE, 0,
  988. 0
  989. },
  990. {
  991. NO_IDX, "sdmux", sdmux_parents, ARRAY_SIZE(sdmux_parents),
  992. STM32F7_RCC_DCKCFGR2, 28, 1,
  993. NO_GATE, 0,
  994. 0
  995. },
  996. {
  997. CLK_HDMI_CEC, "hdmi-cec",
  998. hdmi_parents, ARRAY_SIZE(hdmi_parents),
  999. STM32F7_RCC_DCKCFGR2, 26, 1,
  1000. NO_GATE, 0,
  1001. 0
  1002. },
  1003. {
  1004. CLK_SPDIF, "spdif-rx",
  1005. spdif_parent, ARRAY_SIZE(spdif_parent),
  1006. STM32F7_RCC_DCKCFGR2, 22, 3,
  1007. STM32F4_RCC_APB2ENR, 23,
  1008. CLK_SET_RATE_PARENT
  1009. },
  1010. {
  1011. CLK_USART1, "usart1",
  1012. uart_parents1, ARRAY_SIZE(uart_parents1),
  1013. STM32F7_RCC_DCKCFGR2, 0, 3,
  1014. STM32F4_RCC_APB2ENR, 4,
  1015. CLK_SET_RATE_PARENT,
  1016. },
  1017. {
  1018. CLK_USART2, "usart2",
  1019. uart_parents2, ARRAY_SIZE(uart_parents1),
  1020. STM32F7_RCC_DCKCFGR2, 2, 3,
  1021. STM32F4_RCC_APB1ENR, 17,
  1022. CLK_SET_RATE_PARENT,
  1023. },
  1024. {
  1025. CLK_USART3, "usart3",
  1026. uart_parents2, ARRAY_SIZE(uart_parents1),
  1027. STM32F7_RCC_DCKCFGR2, 4, 3,
  1028. STM32F4_RCC_APB1ENR, 18,
  1029. CLK_SET_RATE_PARENT,
  1030. },
  1031. {
  1032. CLK_UART4, "uart4",
  1033. uart_parents2, ARRAY_SIZE(uart_parents1),
  1034. STM32F7_RCC_DCKCFGR2, 6, 3,
  1035. STM32F4_RCC_APB1ENR, 19,
  1036. CLK_SET_RATE_PARENT,
  1037. },
  1038. {
  1039. CLK_UART5, "uart5",
  1040. uart_parents2, ARRAY_SIZE(uart_parents1),
  1041. STM32F7_RCC_DCKCFGR2, 8, 3,
  1042. STM32F4_RCC_APB1ENR, 20,
  1043. CLK_SET_RATE_PARENT,
  1044. },
  1045. {
  1046. CLK_USART6, "usart6",
  1047. uart_parents1, ARRAY_SIZE(uart_parents1),
  1048. STM32F7_RCC_DCKCFGR2, 10, 3,
  1049. STM32F4_RCC_APB2ENR, 5,
  1050. CLK_SET_RATE_PARENT,
  1051. },
  1052. {
  1053. CLK_UART7, "uart7",
  1054. uart_parents2, ARRAY_SIZE(uart_parents1),
  1055. STM32F7_RCC_DCKCFGR2, 12, 3,
  1056. STM32F4_RCC_APB1ENR, 30,
  1057. CLK_SET_RATE_PARENT,
  1058. },
  1059. {
  1060. CLK_UART8, "uart8",
  1061. uart_parents2, ARRAY_SIZE(uart_parents1),
  1062. STM32F7_RCC_DCKCFGR2, 14, 3,
  1063. STM32F4_RCC_APB1ENR, 31,
  1064. CLK_SET_RATE_PARENT,
  1065. },
  1066. {
  1067. CLK_I2C1, "i2c1",
  1068. i2c_parents, ARRAY_SIZE(i2c_parents),
  1069. STM32F7_RCC_DCKCFGR2, 16, 3,
  1070. STM32F4_RCC_APB1ENR, 21,
  1071. CLK_SET_RATE_PARENT,
  1072. },
  1073. {
  1074. CLK_I2C2, "i2c2",
  1075. i2c_parents, ARRAY_SIZE(i2c_parents),
  1076. STM32F7_RCC_DCKCFGR2, 18, 3,
  1077. STM32F4_RCC_APB1ENR, 22,
  1078. CLK_SET_RATE_PARENT,
  1079. },
  1080. {
  1081. CLK_I2C3, "i2c3",
  1082. i2c_parents, ARRAY_SIZE(i2c_parents),
  1083. STM32F7_RCC_DCKCFGR2, 20, 3,
  1084. STM32F4_RCC_APB1ENR, 23,
  1085. CLK_SET_RATE_PARENT,
  1086. },
  1087. {
  1088. CLK_I2C4, "i2c4",
  1089. i2c_parents, ARRAY_SIZE(i2c_parents),
  1090. STM32F7_RCC_DCKCFGR2, 22, 3,
  1091. STM32F4_RCC_APB1ENR, 24,
  1092. CLK_SET_RATE_PARENT,
  1093. },
  1094. {
  1095. CLK_LPTIMER, "lptim1",
  1096. lptim_parent, ARRAY_SIZE(lptim_parent),
  1097. STM32F7_RCC_DCKCFGR2, 24, 3,
  1098. STM32F4_RCC_APB1ENR, 9,
  1099. CLK_SET_RATE_PARENT
  1100. },
  1101. };
  1102. static const struct stm32f4_clk_data stm32f429_clk_data = {
  1103. .end_primary = END_PRIMARY_CLK,
  1104. .gates_data = stm32f429_gates,
  1105. .gates_map = stm32f42xx_gate_map,
  1106. .gates_num = ARRAY_SIZE(stm32f429_gates),
  1107. .pll_data = stm32f429_pll,
  1108. .aux_clk = stm32f429_aux_clk,
  1109. .aux_clk_num = ARRAY_SIZE(stm32f429_aux_clk),
  1110. };
  1111. static const struct stm32f4_clk_data stm32f469_clk_data = {
  1112. .end_primary = END_PRIMARY_CLK,
  1113. .gates_data = stm32f469_gates,
  1114. .gates_map = stm32f46xx_gate_map,
  1115. .gates_num = ARRAY_SIZE(stm32f469_gates),
  1116. .pll_data = stm32f469_pll,
  1117. .aux_clk = stm32f469_aux_clk,
  1118. .aux_clk_num = ARRAY_SIZE(stm32f469_aux_clk),
  1119. };
  1120. static const struct stm32f4_clk_data stm32f746_clk_data = {
  1121. .end_primary = END_PRIMARY_CLK_F7,
  1122. .gates_data = stm32f746_gates,
  1123. .gates_map = stm32f746_gate_map,
  1124. .gates_num = ARRAY_SIZE(stm32f746_gates),
  1125. .pll_data = stm32f469_pll,
  1126. .aux_clk = stm32f746_aux_clk,
  1127. .aux_clk_num = ARRAY_SIZE(stm32f746_aux_clk),
  1128. };
  1129. static const struct of_device_id stm32f4_of_match[] = {
  1130. {
  1131. .compatible = "st,stm32f42xx-rcc",
  1132. .data = &stm32f429_clk_data
  1133. },
  1134. {
  1135. .compatible = "st,stm32f469-rcc",
  1136. .data = &stm32f469_clk_data
  1137. },
  1138. {
  1139. .compatible = "st,stm32f746-rcc",
  1140. .data = &stm32f746_clk_data
  1141. },
  1142. {}
  1143. };
  1144. static struct clk_hw *stm32_register_aux_clk(const char *name,
  1145. const char * const *parent_names, int num_parents,
  1146. int offset_mux, u8 shift, u8 mask,
  1147. int offset_gate, u8 bit_idx,
  1148. unsigned long flags, spinlock_t *lock)
  1149. {
  1150. struct clk_hw *hw;
  1151. struct clk_gate *gate = NULL;
  1152. struct clk_mux *mux = NULL;
  1153. struct clk_hw *mux_hw = NULL, *gate_hw = NULL;
  1154. const struct clk_ops *mux_ops = NULL, *gate_ops = NULL;
  1155. if (offset_gate != NO_GATE) {
  1156. gate = kzalloc(sizeof(*gate), GFP_KERNEL);
  1157. if (!gate) {
  1158. hw = ERR_PTR(-EINVAL);
  1159. goto fail;
  1160. }
  1161. gate->reg = base + offset_gate;
  1162. gate->bit_idx = bit_idx;
  1163. gate->flags = 0;
  1164. gate->lock = lock;
  1165. gate_hw = &gate->hw;
  1166. gate_ops = &clk_gate_ops;
  1167. }
  1168. if (offset_mux != NO_MUX) {
  1169. mux = kzalloc(sizeof(*mux), GFP_KERNEL);
  1170. if (!mux) {
  1171. hw = ERR_PTR(-EINVAL);
  1172. goto fail;
  1173. }
  1174. mux->reg = base + offset_mux;
  1175. mux->shift = shift;
  1176. mux->mask = mask;
  1177. mux->flags = 0;
  1178. mux_hw = &mux->hw;
  1179. mux_ops = &clk_mux_ops;
  1180. }
  1181. if (mux_hw == NULL && gate_hw == NULL) {
  1182. hw = ERR_PTR(-EINVAL);
  1183. goto fail;
  1184. }
  1185. hw = clk_hw_register_composite(NULL, name, parent_names, num_parents,
  1186. mux_hw, mux_ops,
  1187. NULL, NULL,
  1188. gate_hw, gate_ops,
  1189. flags);
  1190. fail:
  1191. if (IS_ERR(hw)) {
  1192. kfree(gate);
  1193. kfree(mux);
  1194. }
  1195. return hw;
  1196. }
  1197. static void __init stm32f4_rcc_init(struct device_node *np)
  1198. {
  1199. const char *hse_clk, *i2s_in_clk;
  1200. int n;
  1201. const struct of_device_id *match;
  1202. const struct stm32f4_clk_data *data;
  1203. unsigned long pllcfgr;
  1204. const char *pllsrc;
  1205. unsigned long pllm;
  1206. base = of_iomap(np, 0);
  1207. if (!base) {
  1208. pr_err("%s: unable to map resource", np->name);
  1209. return;
  1210. }
  1211. pdrm = syscon_regmap_lookup_by_phandle(np, "st,syscfg");
  1212. if (IS_ERR(pdrm)) {
  1213. pdrm = NULL;
  1214. pr_warn("%s: Unable to get syscfg\n", __func__);
  1215. }
  1216. match = of_match_node(stm32f4_of_match, np);
  1217. if (WARN_ON(!match))
  1218. return;
  1219. data = match->data;
  1220. stm32fx_end_primary_clk = data->end_primary;
  1221. clks = kmalloc_array(data->gates_num + stm32fx_end_primary_clk,
  1222. sizeof(*clks), GFP_KERNEL);
  1223. if (!clks)
  1224. goto fail;
  1225. stm32f4_gate_map = data->gates_map;
  1226. hse_clk = of_clk_get_parent_name(np, 0);
  1227. i2s_in_clk = of_clk_get_parent_name(np, 1);
  1228. i2s_parents[1] = i2s_in_clk;
  1229. sai_parents[2] = i2s_in_clk;
  1230. clks[CLK_HSI] = clk_hw_register_fixed_rate_with_accuracy(NULL, "hsi",
  1231. NULL, 0, 16000000, 160000);
  1232. pllcfgr = readl(base + STM32F4_RCC_PLLCFGR);
  1233. pllsrc = pllcfgr & BIT(22) ? hse_clk : "hsi";
  1234. pllm = pllcfgr & 0x3f;
  1235. clk_hw_register_fixed_factor(NULL, "vco_in", pllsrc,
  1236. 0, 1, pllm);
  1237. stm32f4_rcc_register_pll("vco_in", &data->pll_data[0],
  1238. &stm32f4_clk_lock);
  1239. clks[PLL_VCO_I2S] = stm32f4_rcc_register_pll("vco_in",
  1240. &data->pll_data[1], &stm32f4_clk_lock);
  1241. clks[PLL_VCO_SAI] = stm32f4_rcc_register_pll("vco_in",
  1242. &data->pll_data[2], &stm32f4_clk_lock);
  1243. for (n = 0; n < MAX_POST_DIV; n++) {
  1244. const struct stm32f4_pll_post_div_data *post_div;
  1245. struct clk_hw *hw;
  1246. post_div = &post_div_data[n];
  1247. hw = clk_register_pll_div(post_div->name,
  1248. post_div->parent,
  1249. post_div->flag,
  1250. base + post_div->offset,
  1251. post_div->shift,
  1252. post_div->width,
  1253. post_div->flag_div,
  1254. post_div->div_table,
  1255. clks[post_div->pll_num],
  1256. &stm32f4_clk_lock);
  1257. if (post_div->idx != NO_IDX)
  1258. clks[post_div->idx] = hw;
  1259. }
  1260. sys_parents[1] = hse_clk;
  1261. clks[CLK_SYSCLK] = clk_hw_register_mux_table(
  1262. NULL, "sys", sys_parents, ARRAY_SIZE(sys_parents), 0,
  1263. base + STM32F4_RCC_CFGR, 0, 3, 0, NULL, &stm32f4_clk_lock);
  1264. clk_register_divider_table(NULL, "ahb_div", "sys",
  1265. CLK_SET_RATE_PARENT, base + STM32F4_RCC_CFGR,
  1266. 4, 4, 0, ahb_div_table, &stm32f4_clk_lock);
  1267. clk_register_divider_table(NULL, "apb1_div", "ahb_div",
  1268. CLK_SET_RATE_PARENT, base + STM32F4_RCC_CFGR,
  1269. 10, 3, 0, apb_div_table, &stm32f4_clk_lock);
  1270. clk_register_apb_mul(NULL, "apb1_mul", "apb1_div",
  1271. CLK_SET_RATE_PARENT, 12);
  1272. clk_register_divider_table(NULL, "apb2_div", "ahb_div",
  1273. CLK_SET_RATE_PARENT, base + STM32F4_RCC_CFGR,
  1274. 13, 3, 0, apb_div_table, &stm32f4_clk_lock);
  1275. clk_register_apb_mul(NULL, "apb2_mul", "apb2_div",
  1276. CLK_SET_RATE_PARENT, 15);
  1277. clks[SYSTICK] = clk_hw_register_fixed_factor(NULL, "systick", "ahb_div",
  1278. 0, 1, 8);
  1279. clks[FCLK] = clk_hw_register_fixed_factor(NULL, "fclk", "ahb_div",
  1280. 0, 1, 1);
  1281. for (n = 0; n < data->gates_num; n++) {
  1282. const struct stm32f4_gate_data *gd;
  1283. unsigned int secondary;
  1284. int idx;
  1285. gd = &data->gates_data[n];
  1286. secondary = 8 * (gd->offset - STM32F4_RCC_AHB1ENR) +
  1287. gd->bit_idx;
  1288. idx = stm32f4_rcc_lookup_clk_idx(0, secondary);
  1289. if (idx < 0)
  1290. goto fail;
  1291. clks[idx] = clk_hw_register_gate(
  1292. NULL, gd->name, gd->parent_name, gd->flags,
  1293. base + gd->offset, gd->bit_idx, 0, &stm32f4_clk_lock);
  1294. if (IS_ERR(clks[idx])) {
  1295. pr_err("%pOF: Unable to register leaf clock %s\n",
  1296. np, gd->name);
  1297. goto fail;
  1298. }
  1299. }
  1300. clks[CLK_LSI] = clk_register_rgate(NULL, "lsi", "clk-lsi", 0,
  1301. base + STM32F4_RCC_CSR, 0, 1, 0, &stm32f4_clk_lock);
  1302. if (IS_ERR(clks[CLK_LSI])) {
  1303. pr_err("Unable to register lsi clock\n");
  1304. goto fail;
  1305. }
  1306. clks[CLK_LSE] = clk_register_rgate(NULL, "lse", "clk-lse", 0,
  1307. base + STM32F4_RCC_BDCR, 0, 1, 0, &stm32f4_clk_lock);
  1308. if (IS_ERR(clks[CLK_LSE])) {
  1309. pr_err("Unable to register lse clock\n");
  1310. goto fail;
  1311. }
  1312. clks[CLK_HSE_RTC] = clk_hw_register_divider(NULL, "hse-rtc", "clk-hse",
  1313. 0, base + STM32F4_RCC_CFGR, 16, 5, 0,
  1314. &stm32f4_clk_lock);
  1315. if (IS_ERR(clks[CLK_HSE_RTC])) {
  1316. pr_err("Unable to register hse-rtc clock\n");
  1317. goto fail;
  1318. }
  1319. clks[CLK_RTC] = stm32_register_cclk(NULL, "rtc", rtc_parents, 4,
  1320. base + STM32F4_RCC_BDCR, 15, 8, 0, &stm32f4_clk_lock);
  1321. if (IS_ERR(clks[CLK_RTC])) {
  1322. pr_err("Unable to register rtc clock\n");
  1323. goto fail;
  1324. }
  1325. for (n = 0; n < data->aux_clk_num; n++) {
  1326. const struct stm32_aux_clk *aux_clk;
  1327. struct clk_hw *hw;
  1328. aux_clk = &data->aux_clk[n];
  1329. hw = stm32_register_aux_clk(aux_clk->name,
  1330. aux_clk->parent_names, aux_clk->num_parents,
  1331. aux_clk->offset_mux, aux_clk->shift,
  1332. aux_clk->mask, aux_clk->offset_gate,
  1333. aux_clk->bit_idx, aux_clk->flags,
  1334. &stm32f4_clk_lock);
  1335. if (IS_ERR(hw)) {
  1336. pr_warn("Unable to register %s clk\n", aux_clk->name);
  1337. continue;
  1338. }
  1339. if (aux_clk->idx != NO_IDX)
  1340. clks[aux_clk->idx] = hw;
  1341. }
  1342. if (of_device_is_compatible(np, "st,stm32f746-rcc"))
  1343. clk_hw_register_fixed_factor(NULL, "hsi_div488", "hsi", 0,
  1344. 1, 488);
  1345. of_clk_add_hw_provider(np, stm32f4_rcc_lookup_clk, NULL);
  1346. return;
  1347. fail:
  1348. kfree(clks);
  1349. iounmap(base);
  1350. }
  1351. CLK_OF_DECLARE_DRIVER(stm32f42xx_rcc, "st,stm32f42xx-rcc", stm32f4_rcc_init);
  1352. CLK_OF_DECLARE_DRIVER(stm32f46xx_rcc, "st,stm32f469-rcc", stm32f4_rcc_init);
  1353. CLK_OF_DECLARE_DRIVER(stm32f746_rcc, "st,stm32f746-rcc", stm32f4_rcc_init);