clk-stm32f4.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286
  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 NONE -1
  49. #define NO_IDX NONE
  50. #define NO_MUX NONE
  51. #define NO_GATE NONE
  52. struct stm32f4_gate_data {
  53. u8 offset;
  54. u8 bit_idx;
  55. const char *name;
  56. const char *parent_name;
  57. unsigned long flags;
  58. };
  59. static const struct stm32f4_gate_data stm32f429_gates[] __initconst = {
  60. { STM32F4_RCC_AHB1ENR, 0, "gpioa", "ahb_div" },
  61. { STM32F4_RCC_AHB1ENR, 1, "gpiob", "ahb_div" },
  62. { STM32F4_RCC_AHB1ENR, 2, "gpioc", "ahb_div" },
  63. { STM32F4_RCC_AHB1ENR, 3, "gpiod", "ahb_div" },
  64. { STM32F4_RCC_AHB1ENR, 4, "gpioe", "ahb_div" },
  65. { STM32F4_RCC_AHB1ENR, 5, "gpiof", "ahb_div" },
  66. { STM32F4_RCC_AHB1ENR, 6, "gpiog", "ahb_div" },
  67. { STM32F4_RCC_AHB1ENR, 7, "gpioh", "ahb_div" },
  68. { STM32F4_RCC_AHB1ENR, 8, "gpioi", "ahb_div" },
  69. { STM32F4_RCC_AHB1ENR, 9, "gpioj", "ahb_div" },
  70. { STM32F4_RCC_AHB1ENR, 10, "gpiok", "ahb_div" },
  71. { STM32F4_RCC_AHB1ENR, 12, "crc", "ahb_div" },
  72. { STM32F4_RCC_AHB1ENR, 18, "bkpsra", "ahb_div" },
  73. { STM32F4_RCC_AHB1ENR, 20, "ccmdatam", "ahb_div" },
  74. { STM32F4_RCC_AHB1ENR, 21, "dma1", "ahb_div" },
  75. { STM32F4_RCC_AHB1ENR, 22, "dma2", "ahb_div" },
  76. { STM32F4_RCC_AHB1ENR, 23, "dma2d", "ahb_div" },
  77. { STM32F4_RCC_AHB1ENR, 25, "ethmac", "ahb_div" },
  78. { STM32F4_RCC_AHB1ENR, 26, "ethmactx", "ahb_div" },
  79. { STM32F4_RCC_AHB1ENR, 27, "ethmacrx", "ahb_div" },
  80. { STM32F4_RCC_AHB1ENR, 28, "ethmacptp", "ahb_div" },
  81. { STM32F4_RCC_AHB1ENR, 29, "otghs", "ahb_div" },
  82. { STM32F4_RCC_AHB1ENR, 30, "otghsulpi", "ahb_div" },
  83. { STM32F4_RCC_AHB2ENR, 0, "dcmi", "ahb_div" },
  84. { STM32F4_RCC_AHB2ENR, 4, "cryp", "ahb_div" },
  85. { STM32F4_RCC_AHB2ENR, 5, "hash", "ahb_div" },
  86. { STM32F4_RCC_AHB2ENR, 6, "rng", "pll48" },
  87. { STM32F4_RCC_AHB2ENR, 7, "otgfs", "pll48" },
  88. { STM32F4_RCC_AHB3ENR, 0, "fmc", "ahb_div",
  89. CLK_IGNORE_UNUSED },
  90. { STM32F4_RCC_APB1ENR, 0, "tim2", "apb1_mul" },
  91. { STM32F4_RCC_APB1ENR, 1, "tim3", "apb1_mul" },
  92. { STM32F4_RCC_APB1ENR, 2, "tim4", "apb1_mul" },
  93. { STM32F4_RCC_APB1ENR, 3, "tim5", "apb1_mul" },
  94. { STM32F4_RCC_APB1ENR, 4, "tim6", "apb1_mul" },
  95. { STM32F4_RCC_APB1ENR, 5, "tim7", "apb1_mul" },
  96. { STM32F4_RCC_APB1ENR, 6, "tim12", "apb1_mul" },
  97. { STM32F4_RCC_APB1ENR, 7, "tim13", "apb1_mul" },
  98. { STM32F4_RCC_APB1ENR, 8, "tim14", "apb1_mul" },
  99. { STM32F4_RCC_APB1ENR, 11, "wwdg", "apb1_div" },
  100. { STM32F4_RCC_APB1ENR, 14, "spi2", "apb1_div" },
  101. { STM32F4_RCC_APB1ENR, 15, "spi3", "apb1_div" },
  102. { STM32F4_RCC_APB1ENR, 17, "uart2", "apb1_div" },
  103. { STM32F4_RCC_APB1ENR, 18, "uart3", "apb1_div" },
  104. { STM32F4_RCC_APB1ENR, 19, "uart4", "apb1_div" },
  105. { STM32F4_RCC_APB1ENR, 20, "uart5", "apb1_div" },
  106. { STM32F4_RCC_APB1ENR, 21, "i2c1", "apb1_div" },
  107. { STM32F4_RCC_APB1ENR, 22, "i2c2", "apb1_div" },
  108. { STM32F4_RCC_APB1ENR, 23, "i2c3", "apb1_div" },
  109. { STM32F4_RCC_APB1ENR, 25, "can1", "apb1_div" },
  110. { STM32F4_RCC_APB1ENR, 26, "can2", "apb1_div" },
  111. { STM32F4_RCC_APB1ENR, 28, "pwr", "apb1_div" },
  112. { STM32F4_RCC_APB1ENR, 29, "dac", "apb1_div" },
  113. { STM32F4_RCC_APB1ENR, 30, "uart7", "apb1_div" },
  114. { STM32F4_RCC_APB1ENR, 31, "uart8", "apb1_div" },
  115. { STM32F4_RCC_APB2ENR, 0, "tim1", "apb2_mul" },
  116. { STM32F4_RCC_APB2ENR, 1, "tim8", "apb2_mul" },
  117. { STM32F4_RCC_APB2ENR, 4, "usart1", "apb2_div" },
  118. { STM32F4_RCC_APB2ENR, 5, "usart6", "apb2_div" },
  119. { STM32F4_RCC_APB2ENR, 8, "adc1", "apb2_div" },
  120. { STM32F4_RCC_APB2ENR, 9, "adc2", "apb2_div" },
  121. { STM32F4_RCC_APB2ENR, 10, "adc3", "apb2_div" },
  122. { STM32F4_RCC_APB2ENR, 11, "sdio", "pll48" },
  123. { STM32F4_RCC_APB2ENR, 12, "spi1", "apb2_div" },
  124. { STM32F4_RCC_APB2ENR, 13, "spi4", "apb2_div" },
  125. { STM32F4_RCC_APB2ENR, 14, "syscfg", "apb2_div" },
  126. { STM32F4_RCC_APB2ENR, 16, "tim9", "apb2_mul" },
  127. { STM32F4_RCC_APB2ENR, 17, "tim10", "apb2_mul" },
  128. { STM32F4_RCC_APB2ENR, 18, "tim11", "apb2_mul" },
  129. { STM32F4_RCC_APB2ENR, 20, "spi5", "apb2_div" },
  130. { STM32F4_RCC_APB2ENR, 21, "spi6", "apb2_div" },
  131. { STM32F4_RCC_APB2ENR, 22, "sai1", "apb2_div" },
  132. { STM32F4_RCC_APB2ENR, 26, "ltdc", "apb2_div" },
  133. };
  134. static const struct stm32f4_gate_data stm32f469_gates[] __initconst = {
  135. { STM32F4_RCC_AHB1ENR, 0, "gpioa", "ahb_div" },
  136. { STM32F4_RCC_AHB1ENR, 1, "gpiob", "ahb_div" },
  137. { STM32F4_RCC_AHB1ENR, 2, "gpioc", "ahb_div" },
  138. { STM32F4_RCC_AHB1ENR, 3, "gpiod", "ahb_div" },
  139. { STM32F4_RCC_AHB1ENR, 4, "gpioe", "ahb_div" },
  140. { STM32F4_RCC_AHB1ENR, 5, "gpiof", "ahb_div" },
  141. { STM32F4_RCC_AHB1ENR, 6, "gpiog", "ahb_div" },
  142. { STM32F4_RCC_AHB1ENR, 7, "gpioh", "ahb_div" },
  143. { STM32F4_RCC_AHB1ENR, 8, "gpioi", "ahb_div" },
  144. { STM32F4_RCC_AHB1ENR, 9, "gpioj", "ahb_div" },
  145. { STM32F4_RCC_AHB1ENR, 10, "gpiok", "ahb_div" },
  146. { STM32F4_RCC_AHB1ENR, 12, "crc", "ahb_div" },
  147. { STM32F4_RCC_AHB1ENR, 18, "bkpsra", "ahb_div" },
  148. { STM32F4_RCC_AHB1ENR, 20, "ccmdatam", "ahb_div" },
  149. { STM32F4_RCC_AHB1ENR, 21, "dma1", "ahb_div" },
  150. { STM32F4_RCC_AHB1ENR, 22, "dma2", "ahb_div" },
  151. { STM32F4_RCC_AHB1ENR, 23, "dma2d", "ahb_div" },
  152. { STM32F4_RCC_AHB1ENR, 25, "ethmac", "ahb_div" },
  153. { STM32F4_RCC_AHB1ENR, 26, "ethmactx", "ahb_div" },
  154. { STM32F4_RCC_AHB1ENR, 27, "ethmacrx", "ahb_div" },
  155. { STM32F4_RCC_AHB1ENR, 28, "ethmacptp", "ahb_div" },
  156. { STM32F4_RCC_AHB1ENR, 29, "otghs", "ahb_div" },
  157. { STM32F4_RCC_AHB1ENR, 30, "otghsulpi", "ahb_div" },
  158. { STM32F4_RCC_AHB2ENR, 0, "dcmi", "ahb_div" },
  159. { STM32F4_RCC_AHB2ENR, 4, "cryp", "ahb_div" },
  160. { STM32F4_RCC_AHB2ENR, 5, "hash", "ahb_div" },
  161. { STM32F4_RCC_AHB2ENR, 6, "rng", "pll48" },
  162. { STM32F4_RCC_AHB2ENR, 7, "otgfs", "pll48" },
  163. { STM32F4_RCC_AHB3ENR, 0, "fmc", "ahb_div",
  164. CLK_IGNORE_UNUSED },
  165. { STM32F4_RCC_AHB3ENR, 1, "qspi", "ahb_div",
  166. CLK_IGNORE_UNUSED },
  167. { STM32F4_RCC_APB1ENR, 0, "tim2", "apb1_mul" },
  168. { STM32F4_RCC_APB1ENR, 1, "tim3", "apb1_mul" },
  169. { STM32F4_RCC_APB1ENR, 2, "tim4", "apb1_mul" },
  170. { STM32F4_RCC_APB1ENR, 3, "tim5", "apb1_mul" },
  171. { STM32F4_RCC_APB1ENR, 4, "tim6", "apb1_mul" },
  172. { STM32F4_RCC_APB1ENR, 5, "tim7", "apb1_mul" },
  173. { STM32F4_RCC_APB1ENR, 6, "tim12", "apb1_mul" },
  174. { STM32F4_RCC_APB1ENR, 7, "tim13", "apb1_mul" },
  175. { STM32F4_RCC_APB1ENR, 8, "tim14", "apb1_mul" },
  176. { STM32F4_RCC_APB1ENR, 11, "wwdg", "apb1_div" },
  177. { STM32F4_RCC_APB1ENR, 14, "spi2", "apb1_div" },
  178. { STM32F4_RCC_APB1ENR, 15, "spi3", "apb1_div" },
  179. { STM32F4_RCC_APB1ENR, 17, "uart2", "apb1_div" },
  180. { STM32F4_RCC_APB1ENR, 18, "uart3", "apb1_div" },
  181. { STM32F4_RCC_APB1ENR, 19, "uart4", "apb1_div" },
  182. { STM32F4_RCC_APB1ENR, 20, "uart5", "apb1_div" },
  183. { STM32F4_RCC_APB1ENR, 21, "i2c1", "apb1_div" },
  184. { STM32F4_RCC_APB1ENR, 22, "i2c2", "apb1_div" },
  185. { STM32F4_RCC_APB1ENR, 23, "i2c3", "apb1_div" },
  186. { STM32F4_RCC_APB1ENR, 25, "can1", "apb1_div" },
  187. { STM32F4_RCC_APB1ENR, 26, "can2", "apb1_div" },
  188. { STM32F4_RCC_APB1ENR, 28, "pwr", "apb1_div" },
  189. { STM32F4_RCC_APB1ENR, 29, "dac", "apb1_div" },
  190. { STM32F4_RCC_APB1ENR, 30, "uart7", "apb1_div" },
  191. { STM32F4_RCC_APB1ENR, 31, "uart8", "apb1_div" },
  192. { STM32F4_RCC_APB2ENR, 0, "tim1", "apb2_mul" },
  193. { STM32F4_RCC_APB2ENR, 1, "tim8", "apb2_mul" },
  194. { STM32F4_RCC_APB2ENR, 4, "usart1", "apb2_div" },
  195. { STM32F4_RCC_APB2ENR, 5, "usart6", "apb2_div" },
  196. { STM32F4_RCC_APB2ENR, 8, "adc1", "apb2_div" },
  197. { STM32F4_RCC_APB2ENR, 9, "adc2", "apb2_div" },
  198. { STM32F4_RCC_APB2ENR, 10, "adc3", "apb2_div" },
  199. { STM32F4_RCC_APB2ENR, 11, "sdio", "pll48" },
  200. { STM32F4_RCC_APB2ENR, 12, "spi1", "apb2_div" },
  201. { STM32F4_RCC_APB2ENR, 13, "spi4", "apb2_div" },
  202. { STM32F4_RCC_APB2ENR, 14, "syscfg", "apb2_div" },
  203. { STM32F4_RCC_APB2ENR, 16, "tim9", "apb2_mul" },
  204. { STM32F4_RCC_APB2ENR, 17, "tim10", "apb2_mul" },
  205. { STM32F4_RCC_APB2ENR, 18, "tim11", "apb2_mul" },
  206. { STM32F4_RCC_APB2ENR, 20, "spi5", "apb2_div" },
  207. { STM32F4_RCC_APB2ENR, 21, "spi6", "apb2_div" },
  208. { STM32F4_RCC_APB2ENR, 22, "sai1", "apb2_div" },
  209. { STM32F4_RCC_APB2ENR, 26, "ltdc", "apb2_div" },
  210. };
  211. /*
  212. * This bitmask tells us which bit offsets (0..192) on STM32F4[23]xxx
  213. * have gate bits associated with them. Its combined hweight is 71.
  214. */
  215. #define MAX_GATE_MAP 3
  216. static const u64 stm32f42xx_gate_map[MAX_GATE_MAP] = { 0x000000f17ef417ffull,
  217. 0x0000000000000001ull,
  218. 0x04777f33f6fec9ffull };
  219. static const u64 stm32f46xx_gate_map[MAX_GATE_MAP] = { 0x000000f17ef417ffull,
  220. 0x0000000000000003ull,
  221. 0x0c777f33f6fec9ffull };
  222. static const u64 *stm32f4_gate_map;
  223. static struct clk_hw **clks;
  224. static DEFINE_SPINLOCK(stm32f4_clk_lock);
  225. static void __iomem *base;
  226. static struct regmap *pdrm;
  227. /*
  228. * "Multiplier" device for APBx clocks.
  229. *
  230. * The APBx dividers are power-of-two dividers and, if *not* running in 1:1
  231. * mode, they also tap out the one of the low order state bits to run the
  232. * timers. ST datasheets represent this feature as a (conditional) clock
  233. * multiplier.
  234. */
  235. struct clk_apb_mul {
  236. struct clk_hw hw;
  237. u8 bit_idx;
  238. };
  239. #define to_clk_apb_mul(_hw) container_of(_hw, struct clk_apb_mul, hw)
  240. static unsigned long clk_apb_mul_recalc_rate(struct clk_hw *hw,
  241. unsigned long parent_rate)
  242. {
  243. struct clk_apb_mul *am = to_clk_apb_mul(hw);
  244. if (readl(base + STM32F4_RCC_CFGR) & BIT(am->bit_idx))
  245. return parent_rate * 2;
  246. return parent_rate;
  247. }
  248. static long clk_apb_mul_round_rate(struct clk_hw *hw, unsigned long rate,
  249. unsigned long *prate)
  250. {
  251. struct clk_apb_mul *am = to_clk_apb_mul(hw);
  252. unsigned long mult = 1;
  253. if (readl(base + STM32F4_RCC_CFGR) & BIT(am->bit_idx))
  254. mult = 2;
  255. if (clk_hw_get_flags(hw) & CLK_SET_RATE_PARENT) {
  256. unsigned long best_parent = rate / mult;
  257. *prate = clk_hw_round_rate(clk_hw_get_parent(hw), best_parent);
  258. }
  259. return *prate * mult;
  260. }
  261. static int clk_apb_mul_set_rate(struct clk_hw *hw, unsigned long rate,
  262. unsigned long parent_rate)
  263. {
  264. /*
  265. * We must report success but we can do so unconditionally because
  266. * clk_apb_mul_round_rate returns values that ensure this call is a
  267. * nop.
  268. */
  269. return 0;
  270. }
  271. static const struct clk_ops clk_apb_mul_factor_ops = {
  272. .round_rate = clk_apb_mul_round_rate,
  273. .set_rate = clk_apb_mul_set_rate,
  274. .recalc_rate = clk_apb_mul_recalc_rate,
  275. };
  276. static struct clk *clk_register_apb_mul(struct device *dev, const char *name,
  277. const char *parent_name,
  278. unsigned long flags, u8 bit_idx)
  279. {
  280. struct clk_apb_mul *am;
  281. struct clk_init_data init;
  282. struct clk *clk;
  283. am = kzalloc(sizeof(*am), GFP_KERNEL);
  284. if (!am)
  285. return ERR_PTR(-ENOMEM);
  286. am->bit_idx = bit_idx;
  287. am->hw.init = &init;
  288. init.name = name;
  289. init.ops = &clk_apb_mul_factor_ops;
  290. init.flags = flags;
  291. init.parent_names = &parent_name;
  292. init.num_parents = 1;
  293. clk = clk_register(dev, &am->hw);
  294. if (IS_ERR(clk))
  295. kfree(am);
  296. return clk;
  297. }
  298. enum {
  299. PLL,
  300. PLL_I2S,
  301. PLL_SAI,
  302. };
  303. static const struct clk_div_table pll_divp_table[] = {
  304. { 0, 2 }, { 1, 4 }, { 2, 6 }, { 3, 8 }, { 0 }
  305. };
  306. static const struct clk_div_table pll_divr_table[] = {
  307. { 2, 2 }, { 3, 3 }, { 4, 4 }, { 5, 5 }, { 6, 6 }, { 7, 7 }, { 0 }
  308. };
  309. struct stm32f4_pll {
  310. spinlock_t *lock;
  311. struct clk_gate gate;
  312. u8 offset;
  313. u8 bit_rdy_idx;
  314. u8 status;
  315. u8 n_start;
  316. };
  317. #define to_stm32f4_pll(_gate) container_of(_gate, struct stm32f4_pll, gate)
  318. struct stm32f4_pll_post_div_data {
  319. int idx;
  320. u8 pll_num;
  321. const char *name;
  322. const char *parent;
  323. u8 flag;
  324. u8 offset;
  325. u8 shift;
  326. u8 width;
  327. u8 flag_div;
  328. const struct clk_div_table *div_table;
  329. };
  330. struct stm32f4_vco_data {
  331. const char *vco_name;
  332. u8 offset;
  333. u8 bit_idx;
  334. u8 bit_rdy_idx;
  335. };
  336. static const struct stm32f4_vco_data vco_data[] = {
  337. { "vco", STM32F4_RCC_PLLCFGR, 24, 25 },
  338. { "vco-i2s", STM32F4_RCC_PLLI2SCFGR, 26, 27 },
  339. { "vco-sai", STM32F4_RCC_PLLSAICFGR, 28, 29 },
  340. };
  341. static const struct clk_div_table post_divr_table[] = {
  342. { 0, 2 }, { 1, 4 }, { 2, 8 }, { 3, 16 }, { 0 }
  343. };
  344. #define MAX_POST_DIV 3
  345. static const struct stm32f4_pll_post_div_data post_div_data[MAX_POST_DIV] = {
  346. { CLK_I2SQ_PDIV, PLL_I2S, "plli2s-q-div", "plli2s-q",
  347. CLK_SET_RATE_PARENT, STM32F4_RCC_DCKCFGR, 0, 5, 0, NULL},
  348. { CLK_SAIQ_PDIV, PLL_SAI, "pllsai-q-div", "pllsai-q",
  349. CLK_SET_RATE_PARENT, STM32F4_RCC_DCKCFGR, 8, 5, 0, NULL },
  350. { NO_IDX, PLL_SAI, "pllsai-r-div", "pllsai-r", CLK_SET_RATE_PARENT,
  351. STM32F4_RCC_DCKCFGR, 16, 2, 0, post_divr_table },
  352. };
  353. struct stm32f4_div_data {
  354. u8 shift;
  355. u8 width;
  356. u8 flag_div;
  357. const struct clk_div_table *div_table;
  358. };
  359. #define MAX_PLL_DIV 3
  360. static const struct stm32f4_div_data div_data[MAX_PLL_DIV] = {
  361. { 16, 2, 0, pll_divp_table },
  362. { 24, 4, CLK_DIVIDER_ONE_BASED, NULL },
  363. { 28, 3, 0, pll_divr_table },
  364. };
  365. struct stm32f4_pll_data {
  366. u8 pll_num;
  367. u8 n_start;
  368. const char *div_name[MAX_PLL_DIV];
  369. };
  370. static const struct stm32f4_pll_data stm32f429_pll[MAX_PLL_DIV] = {
  371. { PLL, 192, { "pll", "pll48", NULL } },
  372. { PLL_I2S, 192, { NULL, "plli2s-q", "plli2s-r" } },
  373. { PLL_SAI, 49, { NULL, "pllsai-q", "pllsai-r" } },
  374. };
  375. static const struct stm32f4_pll_data stm32f469_pll[MAX_PLL_DIV] = {
  376. { PLL, 50, { "pll", "pll-q", NULL } },
  377. { PLL_I2S, 50, { "plli2s-p", "plli2s-q", "plli2s-r" } },
  378. { PLL_SAI, 50, { "pllsai-p", "pllsai-q", "pllsai-r" } },
  379. };
  380. static int stm32f4_pll_is_enabled(struct clk_hw *hw)
  381. {
  382. return clk_gate_ops.is_enabled(hw);
  383. }
  384. static int stm32f4_pll_enable(struct clk_hw *hw)
  385. {
  386. struct clk_gate *gate = to_clk_gate(hw);
  387. struct stm32f4_pll *pll = to_stm32f4_pll(gate);
  388. int ret = 0;
  389. unsigned long reg;
  390. ret = clk_gate_ops.enable(hw);
  391. ret = readl_relaxed_poll_timeout_atomic(base + STM32F4_RCC_CR, reg,
  392. reg & (1 << pll->bit_rdy_idx), 0, 10000);
  393. return ret;
  394. }
  395. static void stm32f4_pll_disable(struct clk_hw *hw)
  396. {
  397. clk_gate_ops.disable(hw);
  398. }
  399. static unsigned long stm32f4_pll_recalc(struct clk_hw *hw,
  400. unsigned long parent_rate)
  401. {
  402. struct clk_gate *gate = to_clk_gate(hw);
  403. struct stm32f4_pll *pll = to_stm32f4_pll(gate);
  404. unsigned long n;
  405. n = (readl(base + pll->offset) >> 6) & 0x1ff;
  406. return parent_rate * n;
  407. }
  408. static long stm32f4_pll_round_rate(struct clk_hw *hw, unsigned long rate,
  409. unsigned long *prate)
  410. {
  411. struct clk_gate *gate = to_clk_gate(hw);
  412. struct stm32f4_pll *pll = to_stm32f4_pll(gate);
  413. unsigned long n;
  414. n = rate / *prate;
  415. if (n < pll->n_start)
  416. n = pll->n_start;
  417. else if (n > 432)
  418. n = 432;
  419. return *prate * n;
  420. }
  421. static int stm32f4_pll_set_rate(struct clk_hw *hw, unsigned long rate,
  422. unsigned long parent_rate)
  423. {
  424. struct clk_gate *gate = to_clk_gate(hw);
  425. struct stm32f4_pll *pll = to_stm32f4_pll(gate);
  426. unsigned long n;
  427. unsigned long val;
  428. int pll_state;
  429. pll_state = stm32f4_pll_is_enabled(hw);
  430. if (pll_state)
  431. stm32f4_pll_disable(hw);
  432. n = rate / parent_rate;
  433. val = readl(base + pll->offset) & ~(0x1ff << 6);
  434. writel(val | ((n & 0x1ff) << 6), base + pll->offset);
  435. if (pll_state)
  436. stm32f4_pll_enable(hw);
  437. return 0;
  438. }
  439. static const struct clk_ops stm32f4_pll_gate_ops = {
  440. .enable = stm32f4_pll_enable,
  441. .disable = stm32f4_pll_disable,
  442. .is_enabled = stm32f4_pll_is_enabled,
  443. .recalc_rate = stm32f4_pll_recalc,
  444. .round_rate = stm32f4_pll_round_rate,
  445. .set_rate = stm32f4_pll_set_rate,
  446. };
  447. struct stm32f4_pll_div {
  448. struct clk_divider div;
  449. struct clk_hw *hw_pll;
  450. };
  451. #define to_pll_div_clk(_div) container_of(_div, struct stm32f4_pll_div, div)
  452. static unsigned long stm32f4_pll_div_recalc_rate(struct clk_hw *hw,
  453. unsigned long parent_rate)
  454. {
  455. return clk_divider_ops.recalc_rate(hw, parent_rate);
  456. }
  457. static long stm32f4_pll_div_round_rate(struct clk_hw *hw, unsigned long rate,
  458. unsigned long *prate)
  459. {
  460. return clk_divider_ops.round_rate(hw, rate, prate);
  461. }
  462. static int stm32f4_pll_div_set_rate(struct clk_hw *hw, unsigned long rate,
  463. unsigned long parent_rate)
  464. {
  465. int pll_state, ret;
  466. struct clk_divider *div = to_clk_divider(hw);
  467. struct stm32f4_pll_div *pll_div = to_pll_div_clk(div);
  468. pll_state = stm32f4_pll_is_enabled(pll_div->hw_pll);
  469. if (pll_state)
  470. stm32f4_pll_disable(pll_div->hw_pll);
  471. ret = clk_divider_ops.set_rate(hw, rate, parent_rate);
  472. if (pll_state)
  473. stm32f4_pll_enable(pll_div->hw_pll);
  474. return ret;
  475. }
  476. static const struct clk_ops stm32f4_pll_div_ops = {
  477. .recalc_rate = stm32f4_pll_div_recalc_rate,
  478. .round_rate = stm32f4_pll_div_round_rate,
  479. .set_rate = stm32f4_pll_div_set_rate,
  480. };
  481. static struct clk_hw *clk_register_pll_div(const char *name,
  482. const char *parent_name, unsigned long flags,
  483. void __iomem *reg, u8 shift, u8 width,
  484. u8 clk_divider_flags, const struct clk_div_table *table,
  485. struct clk_hw *pll_hw, spinlock_t *lock)
  486. {
  487. struct stm32f4_pll_div *pll_div;
  488. struct clk_hw *hw;
  489. struct clk_init_data init;
  490. int ret;
  491. /* allocate the divider */
  492. pll_div = kzalloc(sizeof(*pll_div), GFP_KERNEL);
  493. if (!pll_div)
  494. return ERR_PTR(-ENOMEM);
  495. init.name = name;
  496. init.ops = &stm32f4_pll_div_ops;
  497. init.flags = flags;
  498. init.parent_names = (parent_name ? &parent_name : NULL);
  499. init.num_parents = (parent_name ? 1 : 0);
  500. /* struct clk_divider assignments */
  501. pll_div->div.reg = reg;
  502. pll_div->div.shift = shift;
  503. pll_div->div.width = width;
  504. pll_div->div.flags = clk_divider_flags;
  505. pll_div->div.lock = lock;
  506. pll_div->div.table = table;
  507. pll_div->div.hw.init = &init;
  508. pll_div->hw_pll = pll_hw;
  509. /* register the clock */
  510. hw = &pll_div->div.hw;
  511. ret = clk_hw_register(NULL, hw);
  512. if (ret) {
  513. kfree(pll_div);
  514. hw = ERR_PTR(ret);
  515. }
  516. return hw;
  517. }
  518. static struct clk_hw *stm32f4_rcc_register_pll(const char *pllsrc,
  519. const struct stm32f4_pll_data *data, spinlock_t *lock)
  520. {
  521. struct stm32f4_pll *pll;
  522. struct clk_init_data init = { NULL };
  523. void __iomem *reg;
  524. struct clk_hw *pll_hw;
  525. int ret;
  526. int i;
  527. const struct stm32f4_vco_data *vco;
  528. pll = kzalloc(sizeof(*pll), GFP_KERNEL);
  529. if (!pll)
  530. return ERR_PTR(-ENOMEM);
  531. vco = &vco_data[data->pll_num];
  532. init.name = vco->vco_name;
  533. init.ops = &stm32f4_pll_gate_ops;
  534. init.flags = CLK_SET_RATE_GATE;
  535. init.parent_names = &pllsrc;
  536. init.num_parents = 1;
  537. pll->gate.lock = lock;
  538. pll->gate.reg = base + STM32F4_RCC_CR;
  539. pll->gate.bit_idx = vco->bit_idx;
  540. pll->gate.hw.init = &init;
  541. pll->offset = vco->offset;
  542. pll->n_start = data->n_start;
  543. pll->bit_rdy_idx = vco->bit_rdy_idx;
  544. pll->status = (readl(base + STM32F4_RCC_CR) >> vco->bit_idx) & 0x1;
  545. reg = base + pll->offset;
  546. pll_hw = &pll->gate.hw;
  547. ret = clk_hw_register(NULL, pll_hw);
  548. if (ret) {
  549. kfree(pll);
  550. return ERR_PTR(ret);
  551. }
  552. for (i = 0; i < MAX_PLL_DIV; i++)
  553. if (data->div_name[i])
  554. clk_register_pll_div(data->div_name[i],
  555. vco->vco_name,
  556. 0,
  557. reg,
  558. div_data[i].shift,
  559. div_data[i].width,
  560. div_data[i].flag_div,
  561. div_data[i].div_table,
  562. pll_hw,
  563. lock);
  564. return pll_hw;
  565. }
  566. /*
  567. * Converts the primary and secondary indices (as they appear in DT) to an
  568. * offset into our struct clock array.
  569. */
  570. static int stm32f4_rcc_lookup_clk_idx(u8 primary, u8 secondary)
  571. {
  572. u64 table[MAX_GATE_MAP];
  573. if (primary == 1) {
  574. if (WARN_ON(secondary >= END_PRIMARY_CLK))
  575. return -EINVAL;
  576. return secondary;
  577. }
  578. memcpy(table, stm32f4_gate_map, sizeof(table));
  579. /* only bits set in table can be used as indices */
  580. if (WARN_ON(secondary >= BITS_PER_BYTE * sizeof(table) ||
  581. 0 == (table[BIT_ULL_WORD(secondary)] &
  582. BIT_ULL_MASK(secondary))))
  583. return -EINVAL;
  584. /* mask out bits above our current index */
  585. table[BIT_ULL_WORD(secondary)] &=
  586. GENMASK_ULL(secondary % BITS_PER_LONG_LONG, 0);
  587. return END_PRIMARY_CLK - 1 + hweight64(table[0]) +
  588. (BIT_ULL_WORD(secondary) >= 1 ? hweight64(table[1]) : 0) +
  589. (BIT_ULL_WORD(secondary) >= 2 ? hweight64(table[2]) : 0);
  590. }
  591. static struct clk_hw *
  592. stm32f4_rcc_lookup_clk(struct of_phandle_args *clkspec, void *data)
  593. {
  594. int i = stm32f4_rcc_lookup_clk_idx(clkspec->args[0], clkspec->args[1]);
  595. if (i < 0)
  596. return ERR_PTR(-EINVAL);
  597. return clks[i];
  598. }
  599. #define to_rgclk(_rgate) container_of(_rgate, struct stm32_rgate, gate)
  600. static inline void disable_power_domain_write_protection(void)
  601. {
  602. if (pdrm)
  603. regmap_update_bits(pdrm, 0x00, (1 << 8), (1 << 8));
  604. }
  605. static inline void enable_power_domain_write_protection(void)
  606. {
  607. if (pdrm)
  608. regmap_update_bits(pdrm, 0x00, (1 << 8), (0 << 8));
  609. }
  610. static inline void sofware_reset_backup_domain(void)
  611. {
  612. unsigned long val;
  613. val = readl(base + STM32F4_RCC_BDCR);
  614. writel(val | BIT(16), base + STM32F4_RCC_BDCR);
  615. writel(val & ~BIT(16), base + STM32F4_RCC_BDCR);
  616. }
  617. struct stm32_rgate {
  618. struct clk_gate gate;
  619. u8 bit_rdy_idx;
  620. };
  621. #define RTC_TIMEOUT 1000000
  622. static int rgclk_enable(struct clk_hw *hw)
  623. {
  624. struct clk_gate *gate = to_clk_gate(hw);
  625. struct stm32_rgate *rgate = to_rgclk(gate);
  626. u32 reg;
  627. int ret;
  628. disable_power_domain_write_protection();
  629. clk_gate_ops.enable(hw);
  630. ret = readl_relaxed_poll_timeout_atomic(gate->reg, reg,
  631. reg & rgate->bit_rdy_idx, 1000, RTC_TIMEOUT);
  632. enable_power_domain_write_protection();
  633. return ret;
  634. }
  635. static void rgclk_disable(struct clk_hw *hw)
  636. {
  637. clk_gate_ops.disable(hw);
  638. }
  639. static int rgclk_is_enabled(struct clk_hw *hw)
  640. {
  641. return clk_gate_ops.is_enabled(hw);
  642. }
  643. static const struct clk_ops rgclk_ops = {
  644. .enable = rgclk_enable,
  645. .disable = rgclk_disable,
  646. .is_enabled = rgclk_is_enabled,
  647. };
  648. static struct clk_hw *clk_register_rgate(struct device *dev, const char *name,
  649. const char *parent_name, unsigned long flags,
  650. void __iomem *reg, u8 bit_idx, u8 bit_rdy_idx,
  651. u8 clk_gate_flags, spinlock_t *lock)
  652. {
  653. struct stm32_rgate *rgate;
  654. struct clk_init_data init = { NULL };
  655. struct clk_hw *hw;
  656. int ret;
  657. rgate = kzalloc(sizeof(*rgate), GFP_KERNEL);
  658. if (!rgate)
  659. return ERR_PTR(-ENOMEM);
  660. init.name = name;
  661. init.ops = &rgclk_ops;
  662. init.flags = flags;
  663. init.parent_names = &parent_name;
  664. init.num_parents = 1;
  665. rgate->bit_rdy_idx = bit_rdy_idx;
  666. rgate->gate.lock = lock;
  667. rgate->gate.reg = reg;
  668. rgate->gate.bit_idx = bit_idx;
  669. rgate->gate.hw.init = &init;
  670. hw = &rgate->gate.hw;
  671. ret = clk_hw_register(dev, hw);
  672. if (ret) {
  673. kfree(rgate);
  674. hw = ERR_PTR(ret);
  675. }
  676. return hw;
  677. }
  678. static int cclk_gate_enable(struct clk_hw *hw)
  679. {
  680. int ret;
  681. disable_power_domain_write_protection();
  682. ret = clk_gate_ops.enable(hw);
  683. enable_power_domain_write_protection();
  684. return ret;
  685. }
  686. static void cclk_gate_disable(struct clk_hw *hw)
  687. {
  688. disable_power_domain_write_protection();
  689. clk_gate_ops.disable(hw);
  690. enable_power_domain_write_protection();
  691. }
  692. static int cclk_gate_is_enabled(struct clk_hw *hw)
  693. {
  694. return clk_gate_ops.is_enabled(hw);
  695. }
  696. static const struct clk_ops cclk_gate_ops = {
  697. .enable = cclk_gate_enable,
  698. .disable = cclk_gate_disable,
  699. .is_enabled = cclk_gate_is_enabled,
  700. };
  701. static u8 cclk_mux_get_parent(struct clk_hw *hw)
  702. {
  703. return clk_mux_ops.get_parent(hw);
  704. }
  705. static int cclk_mux_set_parent(struct clk_hw *hw, u8 index)
  706. {
  707. int ret;
  708. disable_power_domain_write_protection();
  709. sofware_reset_backup_domain();
  710. ret = clk_mux_ops.set_parent(hw, index);
  711. enable_power_domain_write_protection();
  712. return ret;
  713. }
  714. static const struct clk_ops cclk_mux_ops = {
  715. .get_parent = cclk_mux_get_parent,
  716. .set_parent = cclk_mux_set_parent,
  717. };
  718. static struct clk_hw *stm32_register_cclk(struct device *dev, const char *name,
  719. const char * const *parent_names, int num_parents,
  720. void __iomem *reg, u8 bit_idx, u8 shift, unsigned long flags,
  721. spinlock_t *lock)
  722. {
  723. struct clk_hw *hw;
  724. struct clk_gate *gate;
  725. struct clk_mux *mux;
  726. gate = kzalloc(sizeof(*gate), GFP_KERNEL);
  727. if (!gate) {
  728. hw = ERR_PTR(-EINVAL);
  729. goto fail;
  730. }
  731. mux = kzalloc(sizeof(*mux), GFP_KERNEL);
  732. if (!mux) {
  733. kfree(gate);
  734. hw = ERR_PTR(-EINVAL);
  735. goto fail;
  736. }
  737. gate->reg = reg;
  738. gate->bit_idx = bit_idx;
  739. gate->flags = 0;
  740. gate->lock = lock;
  741. mux->reg = reg;
  742. mux->shift = shift;
  743. mux->mask = 3;
  744. mux->flags = 0;
  745. hw = clk_hw_register_composite(dev, name, parent_names, num_parents,
  746. &mux->hw, &cclk_mux_ops,
  747. NULL, NULL,
  748. &gate->hw, &cclk_gate_ops,
  749. flags);
  750. if (IS_ERR(hw)) {
  751. kfree(gate);
  752. kfree(mux);
  753. }
  754. fail:
  755. return hw;
  756. }
  757. static const char *sys_parents[] __initdata = { "hsi", NULL, "pll" };
  758. static const struct clk_div_table ahb_div_table[] = {
  759. { 0x0, 1 }, { 0x1, 1 }, { 0x2, 1 }, { 0x3, 1 },
  760. { 0x4, 1 }, { 0x5, 1 }, { 0x6, 1 }, { 0x7, 1 },
  761. { 0x8, 2 }, { 0x9, 4 }, { 0xa, 8 }, { 0xb, 16 },
  762. { 0xc, 64 }, { 0xd, 128 }, { 0xe, 256 }, { 0xf, 512 },
  763. { 0 },
  764. };
  765. static const struct clk_div_table apb_div_table[] = {
  766. { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 },
  767. { 4, 2 }, { 5, 4 }, { 6, 8 }, { 7, 16 },
  768. { 0 },
  769. };
  770. static const char *rtc_parents[4] = {
  771. "no-clock", "lse", "lsi", "hse-rtc"
  772. };
  773. static const char *lcd_parent[1] = { "pllsai-r-div" };
  774. static const char *i2s_parents[2] = { "plli2s-r", NULL };
  775. static const char *sai_parents[4] = { "pllsai-q-div", "plli2s-q-div", NULL,
  776. "no-clock" };
  777. struct stm32_aux_clk {
  778. int idx;
  779. const char *name;
  780. const char * const *parent_names;
  781. int num_parents;
  782. int offset_mux;
  783. u8 shift;
  784. u8 mask;
  785. int offset_gate;
  786. u8 bit_idx;
  787. unsigned long flags;
  788. };
  789. struct stm32f4_clk_data {
  790. const struct stm32f4_gate_data *gates_data;
  791. const u64 *gates_map;
  792. int gates_num;
  793. const struct stm32f4_pll_data *pll_data;
  794. const struct stm32_aux_clk *aux_clk;
  795. int aux_clk_num;
  796. };
  797. static const struct stm32_aux_clk stm32f429_aux_clk[] = {
  798. {
  799. CLK_LCD, "lcd-tft", lcd_parent, ARRAY_SIZE(lcd_parent),
  800. NO_MUX, 0, 0,
  801. STM32F4_RCC_APB2ENR, 26,
  802. CLK_SET_RATE_PARENT
  803. },
  804. {
  805. CLK_I2S, "i2s", i2s_parents, ARRAY_SIZE(i2s_parents),
  806. STM32F4_RCC_CFGR, 23, 1,
  807. NO_GATE, 0,
  808. CLK_SET_RATE_PARENT
  809. },
  810. {
  811. CLK_SAI1, "sai1-a", sai_parents, ARRAY_SIZE(sai_parents),
  812. STM32F4_RCC_DCKCFGR, 20, 3,
  813. STM32F4_RCC_APB2ENR, 22,
  814. CLK_SET_RATE_PARENT
  815. },
  816. {
  817. CLK_SAI2, "sai1-b", sai_parents, ARRAY_SIZE(sai_parents),
  818. STM32F4_RCC_DCKCFGR, 22, 3,
  819. STM32F4_RCC_APB2ENR, 22,
  820. CLK_SET_RATE_PARENT
  821. },
  822. };
  823. static const struct stm32f4_clk_data stm32f429_clk_data = {
  824. .gates_data = stm32f429_gates,
  825. .gates_map = stm32f42xx_gate_map,
  826. .gates_num = ARRAY_SIZE(stm32f429_gates),
  827. .pll_data = stm32f429_pll,
  828. .aux_clk = stm32f429_aux_clk,
  829. .aux_clk_num = ARRAY_SIZE(stm32f429_aux_clk),
  830. };
  831. static const struct stm32f4_clk_data stm32f469_clk_data = {
  832. .gates_data = stm32f469_gates,
  833. .gates_map = stm32f46xx_gate_map,
  834. .gates_num = ARRAY_SIZE(stm32f469_gates),
  835. .pll_data = stm32f469_pll,
  836. .aux_clk = stm32f429_aux_clk,
  837. .aux_clk_num = ARRAY_SIZE(stm32f429_aux_clk),
  838. };
  839. static const struct of_device_id stm32f4_of_match[] = {
  840. {
  841. .compatible = "st,stm32f42xx-rcc",
  842. .data = &stm32f429_clk_data
  843. },
  844. {
  845. .compatible = "st,stm32f469-rcc",
  846. .data = &stm32f469_clk_data
  847. },
  848. {}
  849. };
  850. static struct clk_hw *stm32_register_aux_clk(const char *name,
  851. const char * const *parent_names, int num_parents,
  852. int offset_mux, u8 shift, u8 mask,
  853. int offset_gate, u8 bit_idx,
  854. unsigned long flags, spinlock_t *lock)
  855. {
  856. struct clk_hw *hw;
  857. struct clk_gate *gate;
  858. struct clk_mux *mux = NULL;
  859. struct clk_hw *mux_hw = NULL, *gate_hw = NULL;
  860. const struct clk_ops *mux_ops = NULL, *gate_ops = NULL;
  861. if (offset_gate != NO_GATE) {
  862. gate = kzalloc(sizeof(*gate), GFP_KERNEL);
  863. if (!gate) {
  864. hw = ERR_PTR(-EINVAL);
  865. goto fail;
  866. }
  867. gate->reg = base + offset_gate;
  868. gate->bit_idx = bit_idx;
  869. gate->flags = 0;
  870. gate->lock = lock;
  871. gate_hw = &gate->hw;
  872. gate_ops = &clk_gate_ops;
  873. }
  874. if (offset_mux != NO_MUX) {
  875. mux = kzalloc(sizeof(*mux), GFP_KERNEL);
  876. if (!mux) {
  877. kfree(gate);
  878. hw = ERR_PTR(-EINVAL);
  879. goto fail;
  880. }
  881. mux->reg = base + offset_mux;
  882. mux->shift = shift;
  883. mux->mask = mask;
  884. mux->flags = 0;
  885. mux_hw = &mux->hw;
  886. mux_ops = &clk_mux_ops;
  887. }
  888. if (mux_hw == NULL && gate_hw == NULL)
  889. return ERR_PTR(-EINVAL);
  890. hw = clk_hw_register_composite(NULL, name, parent_names, num_parents,
  891. mux_hw, mux_ops,
  892. NULL, NULL,
  893. gate_hw, gate_ops,
  894. flags);
  895. if (IS_ERR(hw)) {
  896. kfree(gate);
  897. kfree(mux);
  898. }
  899. fail:
  900. return hw;
  901. }
  902. static void __init stm32f4_rcc_init(struct device_node *np)
  903. {
  904. const char *hse_clk, *i2s_in_clk;
  905. int n;
  906. const struct of_device_id *match;
  907. const struct stm32f4_clk_data *data;
  908. unsigned long pllcfgr;
  909. const char *pllsrc;
  910. unsigned long pllm;
  911. base = of_iomap(np, 0);
  912. if (!base) {
  913. pr_err("%s: unable to map resource", np->name);
  914. return;
  915. }
  916. pdrm = syscon_regmap_lookup_by_phandle(np, "st,syscfg");
  917. if (IS_ERR(pdrm)) {
  918. pdrm = NULL;
  919. pr_warn("%s: Unable to get syscfg\n", __func__);
  920. }
  921. match = of_match_node(stm32f4_of_match, np);
  922. if (WARN_ON(!match))
  923. return;
  924. data = match->data;
  925. clks = kmalloc_array(data->gates_num + END_PRIMARY_CLK,
  926. sizeof(*clks), GFP_KERNEL);
  927. if (!clks)
  928. goto fail;
  929. stm32f4_gate_map = data->gates_map;
  930. hse_clk = of_clk_get_parent_name(np, 0);
  931. i2s_in_clk = of_clk_get_parent_name(np, 1);
  932. i2s_parents[1] = i2s_in_clk;
  933. sai_parents[2] = i2s_in_clk;
  934. clk_register_fixed_rate_with_accuracy(NULL, "hsi", NULL, 0,
  935. 16000000, 160000);
  936. pllcfgr = readl(base + STM32F4_RCC_PLLCFGR);
  937. pllsrc = pllcfgr & BIT(22) ? hse_clk : "hsi";
  938. pllm = pllcfgr & 0x3f;
  939. clk_hw_register_fixed_factor(NULL, "vco_in", pllsrc,
  940. 0, 1, pllm);
  941. stm32f4_rcc_register_pll("vco_in", &data->pll_data[0],
  942. &stm32f4_clk_lock);
  943. clks[PLL_VCO_I2S] = stm32f4_rcc_register_pll("vco_in",
  944. &data->pll_data[1], &stm32f4_clk_lock);
  945. clks[PLL_VCO_SAI] = stm32f4_rcc_register_pll("vco_in",
  946. &data->pll_data[2], &stm32f4_clk_lock);
  947. for (n = 0; n < MAX_POST_DIV; n++) {
  948. const struct stm32f4_pll_post_div_data *post_div;
  949. struct clk_hw *hw;
  950. post_div = &post_div_data[n];
  951. hw = clk_register_pll_div(post_div->name,
  952. post_div->parent,
  953. post_div->flag,
  954. base + post_div->offset,
  955. post_div->shift,
  956. post_div->width,
  957. post_div->flag_div,
  958. post_div->div_table,
  959. clks[post_div->pll_num],
  960. &stm32f4_clk_lock);
  961. if (post_div->idx != NO_IDX)
  962. clks[post_div->idx] = hw;
  963. }
  964. sys_parents[1] = hse_clk;
  965. clk_register_mux_table(
  966. NULL, "sys", sys_parents, ARRAY_SIZE(sys_parents), 0,
  967. base + STM32F4_RCC_CFGR, 0, 3, 0, NULL, &stm32f4_clk_lock);
  968. clk_register_divider_table(NULL, "ahb_div", "sys",
  969. CLK_SET_RATE_PARENT, base + STM32F4_RCC_CFGR,
  970. 4, 4, 0, ahb_div_table, &stm32f4_clk_lock);
  971. clk_register_divider_table(NULL, "apb1_div", "ahb_div",
  972. CLK_SET_RATE_PARENT, base + STM32F4_RCC_CFGR,
  973. 10, 3, 0, apb_div_table, &stm32f4_clk_lock);
  974. clk_register_apb_mul(NULL, "apb1_mul", "apb1_div",
  975. CLK_SET_RATE_PARENT, 12);
  976. clk_register_divider_table(NULL, "apb2_div", "ahb_div",
  977. CLK_SET_RATE_PARENT, base + STM32F4_RCC_CFGR,
  978. 13, 3, 0, apb_div_table, &stm32f4_clk_lock);
  979. clk_register_apb_mul(NULL, "apb2_mul", "apb2_div",
  980. CLK_SET_RATE_PARENT, 15);
  981. clks[SYSTICK] = clk_hw_register_fixed_factor(NULL, "systick", "ahb_div",
  982. 0, 1, 8);
  983. clks[FCLK] = clk_hw_register_fixed_factor(NULL, "fclk", "ahb_div",
  984. 0, 1, 1);
  985. for (n = 0; n < data->gates_num; n++) {
  986. const struct stm32f4_gate_data *gd;
  987. unsigned int secondary;
  988. int idx;
  989. gd = &data->gates_data[n];
  990. secondary = 8 * (gd->offset - STM32F4_RCC_AHB1ENR) +
  991. gd->bit_idx;
  992. idx = stm32f4_rcc_lookup_clk_idx(0, secondary);
  993. if (idx < 0)
  994. goto fail;
  995. clks[idx] = clk_hw_register_gate(
  996. NULL, gd->name, gd->parent_name, gd->flags,
  997. base + gd->offset, gd->bit_idx, 0, &stm32f4_clk_lock);
  998. if (IS_ERR(clks[idx])) {
  999. pr_err("%s: Unable to register leaf clock %s\n",
  1000. np->full_name, gd->name);
  1001. goto fail;
  1002. }
  1003. }
  1004. clks[CLK_LSI] = clk_register_rgate(NULL, "lsi", "clk-lsi", 0,
  1005. base + STM32F4_RCC_CSR, 0, 2, 0, &stm32f4_clk_lock);
  1006. if (IS_ERR(clks[CLK_LSI])) {
  1007. pr_err("Unable to register lsi clock\n");
  1008. goto fail;
  1009. }
  1010. clks[CLK_LSE] = clk_register_rgate(NULL, "lse", "clk-lse", 0,
  1011. base + STM32F4_RCC_BDCR, 0, 2, 0, &stm32f4_clk_lock);
  1012. if (IS_ERR(clks[CLK_LSE])) {
  1013. pr_err("Unable to register lse clock\n");
  1014. goto fail;
  1015. }
  1016. clks[CLK_HSE_RTC] = clk_hw_register_divider(NULL, "hse-rtc", "clk-hse",
  1017. 0, base + STM32F4_RCC_CFGR, 16, 5, 0,
  1018. &stm32f4_clk_lock);
  1019. if (IS_ERR(clks[CLK_HSE_RTC])) {
  1020. pr_err("Unable to register hse-rtc clock\n");
  1021. goto fail;
  1022. }
  1023. clks[CLK_RTC] = stm32_register_cclk(NULL, "rtc", rtc_parents, 4,
  1024. base + STM32F4_RCC_BDCR, 15, 8, 0, &stm32f4_clk_lock);
  1025. if (IS_ERR(clks[CLK_RTC])) {
  1026. pr_err("Unable to register rtc clock\n");
  1027. goto fail;
  1028. }
  1029. for (n = 0; n < data->aux_clk_num; n++) {
  1030. const struct stm32_aux_clk *aux_clk;
  1031. struct clk_hw *hw;
  1032. aux_clk = &data->aux_clk[n];
  1033. hw = stm32_register_aux_clk(aux_clk->name,
  1034. aux_clk->parent_names, aux_clk->num_parents,
  1035. aux_clk->offset_mux, aux_clk->shift,
  1036. aux_clk->mask, aux_clk->offset_gate,
  1037. aux_clk->bit_idx, aux_clk->flags,
  1038. &stm32f4_clk_lock);
  1039. if (IS_ERR(hw)) {
  1040. pr_warn("Unable to register %s clk\n", aux_clk->name);
  1041. continue;
  1042. }
  1043. if (aux_clk->idx != NO_IDX)
  1044. clks[aux_clk->idx] = hw;
  1045. }
  1046. of_clk_add_hw_provider(np, stm32f4_rcc_lookup_clk, NULL);
  1047. return;
  1048. fail:
  1049. kfree(clks);
  1050. iounmap(base);
  1051. }
  1052. CLK_OF_DECLARE(stm32f42xx_rcc, "st,stm32f42xx-rcc", stm32f4_rcc_init);
  1053. CLK_OF_DECLARE(stm32f46xx_rcc, "st,stm32f469-rcc", stm32f4_rcc_init);