Blame view

drivers/clk/clk-stm32f4.c 50.7 KB
9952f6918   Thomas Gleixner   treewide: Replace...
1
  // SPDX-License-Identifier: GPL-2.0-only
358bdf892   Daniel Thompson   clk: stm32: Add c...
2
3
4
5
  /*
   * Author: Daniel Thompson <daniel.thompson@linaro.org>
   *
   * Inspired by clk-asm9260.c .
358bdf892   Daniel Thompson   clk: stm32: Add c...
6
7
8
9
10
   */
  
  #include <linux/clk-provider.h>
  #include <linux/err.h>
  #include <linux/io.h>
861adc44d   Gabriel Fernandez   clk: stm32f4: Add...
11
12
  #include <linux/iopoll.h>
  #include <linux/ioport.h>
358bdf892   Daniel Thompson   clk: stm32: Add c...
13
14
15
16
  #include <linux/slab.h>
  #include <linux/spinlock.h>
  #include <linux/of.h>
  #include <linux/of_address.h>
861adc44d   Gabriel Fernandez   clk: stm32f4: Add...
17
18
  #include <linux/regmap.h>
  #include <linux/mfd/syscon.h>
358bdf892   Daniel Thompson   clk: stm32: Add c...
19

83135ad3c   Gabriel Fernandez   clk: stm32f4: Add...
20
21
22
23
24
25
26
27
  /*
   * Include list of clocks wich are not derived from system clock (SYSCLOCK)
   * The index of these clocks is the secondary index of DT bindings
   *
   */
  #include <dt-bindings/clock/stm32fx-clock.h>
  
  #define STM32F4_RCC_CR			0x00
358bdf892   Daniel Thompson   clk: stm32: Add c...
28
29
30
31
32
33
34
  #define STM32F4_RCC_PLLCFGR		0x04
  #define STM32F4_RCC_CFGR		0x08
  #define STM32F4_RCC_AHB1ENR		0x30
  #define STM32F4_RCC_AHB2ENR		0x34
  #define STM32F4_RCC_AHB3ENR		0x38
  #define STM32F4_RCC_APB1ENR		0x40
  #define STM32F4_RCC_APB2ENR		0x44
861adc44d   Gabriel Fernandez   clk: stm32f4: Add...
35
36
  #define STM32F4_RCC_BDCR		0x70
  #define STM32F4_RCC_CSR			0x74
83135ad3c   Gabriel Fernandez   clk: stm32f4: Add...
37
38
  #define STM32F4_RCC_PLLI2SCFGR		0x84
  #define STM32F4_RCC_PLLSAICFGR		0x88
517633ef6   Gabriel Fernandez   clk: stm32f4: Add...
39
  #define STM32F4_RCC_DCKCFGR		0x8c
88c9b70bb   Gabriel Fernandez   clk: stm32f7: Int...
40
  #define STM32F7_RCC_DCKCFGR2		0x90
517633ef6   Gabriel Fernandez   clk: stm32f4: Add...
41
42
43
  
  #define NONE -1
  #define NO_IDX  NONE
daf2d117c   Gabriel Fernandez   clk: stm32f4: Add...
44
45
  #define NO_MUX  NONE
  #define NO_GATE NONE
358bdf892   Daniel Thompson   clk: stm32: Add c...
46
47
48
49
50
51
52
53
  
  struct stm32f4_gate_data {
  	u8	offset;
  	u8	bit_idx;
  	const char *name;
  	const char *parent_name;
  	unsigned long flags;
  };
a064a07f7   Gabriel Fernandez   clk: stm32f469: A...
54
  static const struct stm32f4_gate_data stm32f429_gates[] __initconst = {
358bdf892   Daniel Thompson   clk: stm32: Add c...
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
  	{ STM32F4_RCC_AHB1ENR,  0,	"gpioa",	"ahb_div" },
  	{ STM32F4_RCC_AHB1ENR,  1,	"gpiob",	"ahb_div" },
  	{ STM32F4_RCC_AHB1ENR,  2,	"gpioc",	"ahb_div" },
  	{ STM32F4_RCC_AHB1ENR,  3,	"gpiod",	"ahb_div" },
  	{ STM32F4_RCC_AHB1ENR,  4,	"gpioe",	"ahb_div" },
  	{ STM32F4_RCC_AHB1ENR,  5,	"gpiof",	"ahb_div" },
  	{ STM32F4_RCC_AHB1ENR,  6,	"gpiog",	"ahb_div" },
  	{ STM32F4_RCC_AHB1ENR,  7,	"gpioh",	"ahb_div" },
  	{ STM32F4_RCC_AHB1ENR,  8,	"gpioi",	"ahb_div" },
  	{ STM32F4_RCC_AHB1ENR,  9,	"gpioj",	"ahb_div" },
  	{ STM32F4_RCC_AHB1ENR, 10,	"gpiok",	"ahb_div" },
  	{ STM32F4_RCC_AHB1ENR, 12,	"crc",		"ahb_div" },
  	{ STM32F4_RCC_AHB1ENR, 18,	"bkpsra",	"ahb_div" },
  	{ STM32F4_RCC_AHB1ENR, 20,	"ccmdatam",	"ahb_div" },
  	{ STM32F4_RCC_AHB1ENR, 21,	"dma1",		"ahb_div" },
  	{ STM32F4_RCC_AHB1ENR, 22,	"dma2",		"ahb_div" },
  	{ STM32F4_RCC_AHB1ENR, 23,	"dma2d",	"ahb_div" },
  	{ STM32F4_RCC_AHB1ENR, 25,	"ethmac",	"ahb_div" },
  	{ STM32F4_RCC_AHB1ENR, 26,	"ethmactx",	"ahb_div" },
  	{ STM32F4_RCC_AHB1ENR, 27,	"ethmacrx",	"ahb_div" },
  	{ STM32F4_RCC_AHB1ENR, 28,	"ethmacptp",	"ahb_div" },
  	{ STM32F4_RCC_AHB1ENR, 29,	"otghs",	"ahb_div" },
  	{ STM32F4_RCC_AHB1ENR, 30,	"otghsulpi",	"ahb_div" },
  
  	{ STM32F4_RCC_AHB2ENR,  0,	"dcmi",		"ahb_div" },
  	{ STM32F4_RCC_AHB2ENR,  4,	"cryp",		"ahb_div" },
  	{ STM32F4_RCC_AHB2ENR,  5,	"hash",		"ahb_div" },
  	{ STM32F4_RCC_AHB2ENR,  6,	"rng",		"pll48" },
  	{ STM32F4_RCC_AHB2ENR,  7,	"otgfs",	"pll48" },
  
  	{ STM32F4_RCC_AHB3ENR,  0,	"fmc",		"ahb_div",
  		CLK_IGNORE_UNUSED },
  
  	{ STM32F4_RCC_APB1ENR,  0,	"tim2",		"apb1_mul" },
  	{ STM32F4_RCC_APB1ENR,  1,	"tim3",		"apb1_mul" },
  	{ STM32F4_RCC_APB1ENR,  2,	"tim4",		"apb1_mul" },
  	{ STM32F4_RCC_APB1ENR,  3,	"tim5",		"apb1_mul" },
  	{ STM32F4_RCC_APB1ENR,  4,	"tim6",		"apb1_mul" },
  	{ STM32F4_RCC_APB1ENR,  5,	"tim7",		"apb1_mul" },
  	{ STM32F4_RCC_APB1ENR,  6,	"tim12",	"apb1_mul" },
  	{ STM32F4_RCC_APB1ENR,  7,	"tim13",	"apb1_mul" },
  	{ STM32F4_RCC_APB1ENR,  8,	"tim14",	"apb1_mul" },
  	{ STM32F4_RCC_APB1ENR, 11,	"wwdg",		"apb1_div" },
  	{ STM32F4_RCC_APB1ENR, 14,	"spi2",		"apb1_div" },
  	{ STM32F4_RCC_APB1ENR, 15,	"spi3",		"apb1_div" },
  	{ STM32F4_RCC_APB1ENR, 17,	"uart2",	"apb1_div" },
  	{ STM32F4_RCC_APB1ENR, 18,	"uart3",	"apb1_div" },
  	{ STM32F4_RCC_APB1ENR, 19,	"uart4",	"apb1_div" },
  	{ STM32F4_RCC_APB1ENR, 20,	"uart5",	"apb1_div" },
  	{ STM32F4_RCC_APB1ENR, 21,	"i2c1",		"apb1_div" },
  	{ STM32F4_RCC_APB1ENR, 22,	"i2c2",		"apb1_div" },
  	{ STM32F4_RCC_APB1ENR, 23,	"i2c3",		"apb1_div" },
  	{ STM32F4_RCC_APB1ENR, 25,	"can1",		"apb1_div" },
  	{ STM32F4_RCC_APB1ENR, 26,	"can2",		"apb1_div" },
  	{ STM32F4_RCC_APB1ENR, 28,	"pwr",		"apb1_div" },
  	{ STM32F4_RCC_APB1ENR, 29,	"dac",		"apb1_div" },
  	{ STM32F4_RCC_APB1ENR, 30,	"uart7",	"apb1_div" },
  	{ STM32F4_RCC_APB1ENR, 31,	"uart8",	"apb1_div" },
  
  	{ STM32F4_RCC_APB2ENR,  0,	"tim1",		"apb2_mul" },
  	{ STM32F4_RCC_APB2ENR,  1,	"tim8",		"apb2_mul" },
  	{ STM32F4_RCC_APB2ENR,  4,	"usart1",	"apb2_div" },
  	{ STM32F4_RCC_APB2ENR,  5,	"usart6",	"apb2_div" },
  	{ STM32F4_RCC_APB2ENR,  8,	"adc1",		"apb2_div" },
  	{ STM32F4_RCC_APB2ENR,  9,	"adc2",		"apb2_div" },
  	{ STM32F4_RCC_APB2ENR, 10,	"adc3",		"apb2_div" },
  	{ STM32F4_RCC_APB2ENR, 11,	"sdio",		"pll48" },
  	{ STM32F4_RCC_APB2ENR, 12,	"spi1",		"apb2_div" },
  	{ STM32F4_RCC_APB2ENR, 13,	"spi4",		"apb2_div" },
  	{ STM32F4_RCC_APB2ENR, 14,	"syscfg",	"apb2_div" },
  	{ STM32F4_RCC_APB2ENR, 16,	"tim9",		"apb2_mul" },
  	{ STM32F4_RCC_APB2ENR, 17,	"tim10",	"apb2_mul" },
  	{ STM32F4_RCC_APB2ENR, 18,	"tim11",	"apb2_mul" },
  	{ STM32F4_RCC_APB2ENR, 20,	"spi5",		"apb2_div" },
  	{ STM32F4_RCC_APB2ENR, 21,	"spi6",		"apb2_div" },
  	{ STM32F4_RCC_APB2ENR, 22,	"sai1",		"apb2_div" },
  	{ STM32F4_RCC_APB2ENR, 26,	"ltdc",		"apb2_div" },
  };
a064a07f7   Gabriel Fernandez   clk: stm32f469: A...
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
  static const struct stm32f4_gate_data stm32f469_gates[] __initconst = {
  	{ STM32F4_RCC_AHB1ENR,  0,	"gpioa",	"ahb_div" },
  	{ STM32F4_RCC_AHB1ENR,  1,	"gpiob",	"ahb_div" },
  	{ STM32F4_RCC_AHB1ENR,  2,	"gpioc",	"ahb_div" },
  	{ STM32F4_RCC_AHB1ENR,  3,	"gpiod",	"ahb_div" },
  	{ STM32F4_RCC_AHB1ENR,  4,	"gpioe",	"ahb_div" },
  	{ STM32F4_RCC_AHB1ENR,  5,	"gpiof",	"ahb_div" },
  	{ STM32F4_RCC_AHB1ENR,  6,	"gpiog",	"ahb_div" },
  	{ STM32F4_RCC_AHB1ENR,  7,	"gpioh",	"ahb_div" },
  	{ STM32F4_RCC_AHB1ENR,  8,	"gpioi",	"ahb_div" },
  	{ STM32F4_RCC_AHB1ENR,  9,	"gpioj",	"ahb_div" },
  	{ STM32F4_RCC_AHB1ENR, 10,	"gpiok",	"ahb_div" },
  	{ STM32F4_RCC_AHB1ENR, 12,	"crc",		"ahb_div" },
  	{ STM32F4_RCC_AHB1ENR, 18,	"bkpsra",	"ahb_div" },
  	{ STM32F4_RCC_AHB1ENR, 20,	"ccmdatam",	"ahb_div" },
  	{ STM32F4_RCC_AHB1ENR, 21,	"dma1",		"ahb_div" },
  	{ STM32F4_RCC_AHB1ENR, 22,	"dma2",		"ahb_div" },
  	{ STM32F4_RCC_AHB1ENR, 23,	"dma2d",	"ahb_div" },
  	{ STM32F4_RCC_AHB1ENR, 25,	"ethmac",	"ahb_div" },
  	{ STM32F4_RCC_AHB1ENR, 26,	"ethmactx",	"ahb_div" },
  	{ STM32F4_RCC_AHB1ENR, 27,	"ethmacrx",	"ahb_div" },
  	{ STM32F4_RCC_AHB1ENR, 28,	"ethmacptp",	"ahb_div" },
  	{ STM32F4_RCC_AHB1ENR, 29,	"otghs",	"ahb_div" },
  	{ STM32F4_RCC_AHB1ENR, 30,	"otghsulpi",	"ahb_div" },
  
  	{ STM32F4_RCC_AHB2ENR,  0,	"dcmi",		"ahb_div" },
  	{ STM32F4_RCC_AHB2ENR,  4,	"cryp",		"ahb_div" },
  	{ STM32F4_RCC_AHB2ENR,  5,	"hash",		"ahb_div" },
  	{ STM32F4_RCC_AHB2ENR,  6,	"rng",		"pll48" },
  	{ STM32F4_RCC_AHB2ENR,  7,	"otgfs",	"pll48" },
  
  	{ STM32F4_RCC_AHB3ENR,  0,	"fmc",		"ahb_div",
  		CLK_IGNORE_UNUSED },
  	{ STM32F4_RCC_AHB3ENR,  1,	"qspi",		"ahb_div",
  		CLK_IGNORE_UNUSED },
  
  	{ STM32F4_RCC_APB1ENR,  0,	"tim2",		"apb1_mul" },
  	{ STM32F4_RCC_APB1ENR,  1,	"tim3",		"apb1_mul" },
  	{ STM32F4_RCC_APB1ENR,  2,	"tim4",		"apb1_mul" },
  	{ STM32F4_RCC_APB1ENR,  3,	"tim5",		"apb1_mul" },
  	{ STM32F4_RCC_APB1ENR,  4,	"tim6",		"apb1_mul" },
  	{ STM32F4_RCC_APB1ENR,  5,	"tim7",		"apb1_mul" },
  	{ STM32F4_RCC_APB1ENR,  6,	"tim12",	"apb1_mul" },
  	{ STM32F4_RCC_APB1ENR,  7,	"tim13",	"apb1_mul" },
  	{ STM32F4_RCC_APB1ENR,  8,	"tim14",	"apb1_mul" },
  	{ STM32F4_RCC_APB1ENR, 11,	"wwdg",		"apb1_div" },
  	{ STM32F4_RCC_APB1ENR, 14,	"spi2",		"apb1_div" },
  	{ STM32F4_RCC_APB1ENR, 15,	"spi3",		"apb1_div" },
  	{ STM32F4_RCC_APB1ENR, 17,	"uart2",	"apb1_div" },
  	{ STM32F4_RCC_APB1ENR, 18,	"uart3",	"apb1_div" },
  	{ STM32F4_RCC_APB1ENR, 19,	"uart4",	"apb1_div" },
  	{ STM32F4_RCC_APB1ENR, 20,	"uart5",	"apb1_div" },
  	{ STM32F4_RCC_APB1ENR, 21,	"i2c1",		"apb1_div" },
  	{ STM32F4_RCC_APB1ENR, 22,	"i2c2",		"apb1_div" },
  	{ STM32F4_RCC_APB1ENR, 23,	"i2c3",		"apb1_div" },
  	{ STM32F4_RCC_APB1ENR, 25,	"can1",		"apb1_div" },
  	{ STM32F4_RCC_APB1ENR, 26,	"can2",		"apb1_div" },
  	{ STM32F4_RCC_APB1ENR, 28,	"pwr",		"apb1_div" },
  	{ STM32F4_RCC_APB1ENR, 29,	"dac",		"apb1_div" },
  	{ STM32F4_RCC_APB1ENR, 30,	"uart7",	"apb1_div" },
  	{ STM32F4_RCC_APB1ENR, 31,	"uart8",	"apb1_div" },
  
  	{ STM32F4_RCC_APB2ENR,  0,	"tim1",		"apb2_mul" },
  	{ STM32F4_RCC_APB2ENR,  1,	"tim8",		"apb2_mul" },
  	{ STM32F4_RCC_APB2ENR,  4,	"usart1",	"apb2_div" },
  	{ STM32F4_RCC_APB2ENR,  5,	"usart6",	"apb2_div" },
  	{ STM32F4_RCC_APB2ENR,  8,	"adc1",		"apb2_div" },
  	{ STM32F4_RCC_APB2ENR,  9,	"adc2",		"apb2_div" },
  	{ STM32F4_RCC_APB2ENR, 10,	"adc3",		"apb2_div" },
844ca23f5   Gabriel Fernandez   clk: stm32f4: SDI...
202
  	{ STM32F4_RCC_APB2ENR, 11,	"sdio",		"sdmux" },
a064a07f7   Gabriel Fernandez   clk: stm32f469: A...
203
204
205
206
207
208
209
210
211
212
213
  	{ STM32F4_RCC_APB2ENR, 12,	"spi1",		"apb2_div" },
  	{ STM32F4_RCC_APB2ENR, 13,	"spi4",		"apb2_div" },
  	{ STM32F4_RCC_APB2ENR, 14,	"syscfg",	"apb2_div" },
  	{ STM32F4_RCC_APB2ENR, 16,	"tim9",		"apb2_mul" },
  	{ STM32F4_RCC_APB2ENR, 17,	"tim10",	"apb2_mul" },
  	{ STM32F4_RCC_APB2ENR, 18,	"tim11",	"apb2_mul" },
  	{ STM32F4_RCC_APB2ENR, 20,	"spi5",		"apb2_div" },
  	{ STM32F4_RCC_APB2ENR, 21,	"spi6",		"apb2_div" },
  	{ STM32F4_RCC_APB2ENR, 22,	"sai1",		"apb2_div" },
  	{ STM32F4_RCC_APB2ENR, 26,	"ltdc",		"apb2_div" },
  };
88c9b70bb   Gabriel Fernandez   clk: stm32f7: Int...
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
  static const struct stm32f4_gate_data stm32f746_gates[] __initconst = {
  	{ STM32F4_RCC_AHB1ENR,  0,	"gpioa",	"ahb_div" },
  	{ STM32F4_RCC_AHB1ENR,  1,	"gpiob",	"ahb_div" },
  	{ STM32F4_RCC_AHB1ENR,  2,	"gpioc",	"ahb_div" },
  	{ STM32F4_RCC_AHB1ENR,  3,	"gpiod",	"ahb_div" },
  	{ STM32F4_RCC_AHB1ENR,  4,	"gpioe",	"ahb_div" },
  	{ STM32F4_RCC_AHB1ENR,  5,	"gpiof",	"ahb_div" },
  	{ STM32F4_RCC_AHB1ENR,  6,	"gpiog",	"ahb_div" },
  	{ STM32F4_RCC_AHB1ENR,  7,	"gpioh",	"ahb_div" },
  	{ STM32F4_RCC_AHB1ENR,  8,	"gpioi",	"ahb_div" },
  	{ STM32F4_RCC_AHB1ENR,  9,	"gpioj",	"ahb_div" },
  	{ STM32F4_RCC_AHB1ENR, 10,	"gpiok",	"ahb_div" },
  	{ STM32F4_RCC_AHB1ENR, 12,	"crc",		"ahb_div" },
  	{ STM32F4_RCC_AHB1ENR, 18,	"bkpsra",	"ahb_div" },
  	{ STM32F4_RCC_AHB1ENR, 20,	"dtcmram",	"ahb_div" },
  	{ STM32F4_RCC_AHB1ENR, 21,	"dma1",		"ahb_div" },
  	{ STM32F4_RCC_AHB1ENR, 22,	"dma2",		"ahb_div" },
  	{ STM32F4_RCC_AHB1ENR, 23,	"dma2d",	"ahb_div" },
  	{ STM32F4_RCC_AHB1ENR, 25,	"ethmac",	"ahb_div" },
  	{ STM32F4_RCC_AHB1ENR, 26,	"ethmactx",	"ahb_div" },
  	{ STM32F4_RCC_AHB1ENR, 27,	"ethmacrx",	"ahb_div" },
  	{ STM32F4_RCC_AHB1ENR, 28,	"ethmacptp",	"ahb_div" },
  	{ STM32F4_RCC_AHB1ENR, 29,	"otghs",	"ahb_div" },
  	{ STM32F4_RCC_AHB1ENR, 30,	"otghsulpi",	"ahb_div" },
  
  	{ STM32F4_RCC_AHB2ENR,  0,	"dcmi",		"ahb_div" },
  	{ STM32F4_RCC_AHB2ENR,  4,	"cryp",		"ahb_div" },
  	{ STM32F4_RCC_AHB2ENR,  5,	"hash",		"ahb_div" },
  	{ STM32F4_RCC_AHB2ENR,  6,	"rng",		"pll48"   },
  	{ STM32F4_RCC_AHB2ENR,  7,	"otgfs",	"pll48"   },
  
  	{ STM32F4_RCC_AHB3ENR,  0,	"fmc",		"ahb_div",
  		CLK_IGNORE_UNUSED },
  	{ STM32F4_RCC_AHB3ENR,  1,	"qspi",		"ahb_div",
  		CLK_IGNORE_UNUSED },
  
  	{ STM32F4_RCC_APB1ENR,  0,	"tim2",		"apb1_mul" },
  	{ STM32F4_RCC_APB1ENR,  1,	"tim3",		"apb1_mul" },
  	{ STM32F4_RCC_APB1ENR,  2,	"tim4",		"apb1_mul" },
  	{ STM32F4_RCC_APB1ENR,  3,	"tim5",		"apb1_mul" },
  	{ STM32F4_RCC_APB1ENR,  4,	"tim6",		"apb1_mul" },
  	{ STM32F4_RCC_APB1ENR,  5,	"tim7",		"apb1_mul" },
  	{ STM32F4_RCC_APB1ENR,  6,	"tim12",	"apb1_mul" },
  	{ STM32F4_RCC_APB1ENR,  7,	"tim13",	"apb1_mul" },
  	{ STM32F4_RCC_APB1ENR,  8,	"tim14",	"apb1_mul" },
  	{ STM32F4_RCC_APB1ENR, 11,	"wwdg",		"apb1_div" },
  	{ STM32F4_RCC_APB1ENR, 14,	"spi2",		"apb1_div" },
  	{ STM32F4_RCC_APB1ENR, 15,	"spi3",		"apb1_div" },
  	{ STM32F4_RCC_APB1ENR, 16,	"spdifrx",	"apb1_div" },
  	{ STM32F4_RCC_APB1ENR, 25,	"can1",		"apb1_div" },
  	{ STM32F4_RCC_APB1ENR, 26,	"can2",		"apb1_div" },
  	{ STM32F4_RCC_APB1ENR, 27,	"cec",		"apb1_div" },
  	{ STM32F4_RCC_APB1ENR, 28,	"pwr",		"apb1_div" },
  	{ STM32F4_RCC_APB1ENR, 29,	"dac",		"apb1_div" },
  
  	{ STM32F4_RCC_APB2ENR,  0,	"tim1",		"apb2_mul" },
  	{ STM32F4_RCC_APB2ENR,  1,	"tim8",		"apb2_mul" },
f9acf1057   Patrice Chotard   clk: stm32: Add c...
271
  	{ STM32F4_RCC_APB2ENR,  7,	"sdmmc2",	"sdmux"    },
88c9b70bb   Gabriel Fernandez   clk: stm32f7: Int...
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
  	{ STM32F4_RCC_APB2ENR,  8,	"adc1",		"apb2_div" },
  	{ STM32F4_RCC_APB2ENR,  9,	"adc2",		"apb2_div" },
  	{ STM32F4_RCC_APB2ENR, 10,	"adc3",		"apb2_div" },
  	{ STM32F4_RCC_APB2ENR, 11,	"sdmmc",	"sdmux"    },
  	{ STM32F4_RCC_APB2ENR, 12,	"spi1",		"apb2_div" },
  	{ STM32F4_RCC_APB2ENR, 13,	"spi4",		"apb2_div" },
  	{ STM32F4_RCC_APB2ENR, 14,	"syscfg",	"apb2_div" },
  	{ STM32F4_RCC_APB2ENR, 16,	"tim9",		"apb2_mul" },
  	{ STM32F4_RCC_APB2ENR, 17,	"tim10",	"apb2_mul" },
  	{ STM32F4_RCC_APB2ENR, 18,	"tim11",	"apb2_mul" },
  	{ STM32F4_RCC_APB2ENR, 20,	"spi5",		"apb2_div" },
  	{ STM32F4_RCC_APB2ENR, 21,	"spi6",		"apb2_div" },
  	{ STM32F4_RCC_APB2ENR, 22,	"sai1",		"apb2_div" },
  	{ STM32F4_RCC_APB2ENR, 23,	"sai2",		"apb2_div" },
  	{ STM32F4_RCC_APB2ENR, 26,	"ltdc",		"apb2_div" },
  };
936289f04   Gabriel Fernandez   clk: stm32: Intro...
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
  static const struct stm32f4_gate_data stm32f769_gates[] __initconst = {
  	{ STM32F4_RCC_AHB1ENR,  0,	"gpioa",	"ahb_div" },
  	{ STM32F4_RCC_AHB1ENR,  1,	"gpiob",	"ahb_div" },
  	{ STM32F4_RCC_AHB1ENR,  2,	"gpioc",	"ahb_div" },
  	{ STM32F4_RCC_AHB1ENR,  3,	"gpiod",	"ahb_div" },
  	{ STM32F4_RCC_AHB1ENR,  4,	"gpioe",	"ahb_div" },
  	{ STM32F4_RCC_AHB1ENR,  5,	"gpiof",	"ahb_div" },
  	{ STM32F4_RCC_AHB1ENR,  6,	"gpiog",	"ahb_div" },
  	{ STM32F4_RCC_AHB1ENR,  7,	"gpioh",	"ahb_div" },
  	{ STM32F4_RCC_AHB1ENR,  8,	"gpioi",	"ahb_div" },
  	{ STM32F4_RCC_AHB1ENR,  9,	"gpioj",	"ahb_div" },
  	{ STM32F4_RCC_AHB1ENR, 10,	"gpiok",	"ahb_div" },
  	{ STM32F4_RCC_AHB1ENR, 12,	"crc",		"ahb_div" },
  	{ STM32F4_RCC_AHB1ENR, 18,	"bkpsra",	"ahb_div" },
  	{ STM32F4_RCC_AHB1ENR, 20,	"dtcmram",	"ahb_div" },
  	{ STM32F4_RCC_AHB1ENR, 21,	"dma1",		"ahb_div" },
  	{ STM32F4_RCC_AHB1ENR, 22,	"dma2",		"ahb_div" },
  	{ STM32F4_RCC_AHB1ENR, 23,	"dma2d",	"ahb_div" },
  	{ STM32F4_RCC_AHB1ENR, 25,	"ethmac",	"ahb_div" },
  	{ STM32F4_RCC_AHB1ENR, 26,	"ethmactx",	"ahb_div" },
  	{ STM32F4_RCC_AHB1ENR, 27,	"ethmacrx",	"ahb_div" },
  	{ STM32F4_RCC_AHB1ENR, 28,	"ethmacptp",	"ahb_div" },
  	{ STM32F4_RCC_AHB1ENR, 29,	"otghs",	"ahb_div" },
  	{ STM32F4_RCC_AHB1ENR, 30,	"otghsulpi",	"ahb_div" },
  
  	{ STM32F4_RCC_AHB2ENR,  0,	"dcmi",		"ahb_div" },
  	{ STM32F4_RCC_AHB2ENR,  1,	"jpeg",		"ahb_div" },
  	{ STM32F4_RCC_AHB2ENR,  4,	"cryp",		"ahb_div" },
  	{ STM32F4_RCC_AHB2ENR,  5,	"hash",		"ahb_div" },
  	{ STM32F4_RCC_AHB2ENR,  6,	"rng",		"pll48"   },
  	{ STM32F4_RCC_AHB2ENR,  7,	"otgfs",	"pll48"   },
  
  	{ STM32F4_RCC_AHB3ENR,  0,	"fmc",		"ahb_div",
  		CLK_IGNORE_UNUSED },
  	{ STM32F4_RCC_AHB3ENR,  1,	"qspi",		"ahb_div",
  		CLK_IGNORE_UNUSED },
  
  	{ STM32F4_RCC_APB1ENR,  0,	"tim2",		"apb1_mul" },
  	{ STM32F4_RCC_APB1ENR,  1,	"tim3",		"apb1_mul" },
  	{ STM32F4_RCC_APB1ENR,  2,	"tim4",		"apb1_mul" },
  	{ STM32F4_RCC_APB1ENR,  3,	"tim5",		"apb1_mul" },
  	{ STM32F4_RCC_APB1ENR,  4,	"tim6",		"apb1_mul" },
  	{ STM32F4_RCC_APB1ENR,  5,	"tim7",		"apb1_mul" },
  	{ STM32F4_RCC_APB1ENR,  6,	"tim12",	"apb1_mul" },
  	{ STM32F4_RCC_APB1ENR,  7,	"tim13",	"apb1_mul" },
  	{ STM32F4_RCC_APB1ENR,  8,	"tim14",	"apb1_mul" },
  	{ STM32F4_RCC_APB1ENR, 10,	"rtcapb",	"apb1_mul" },
  	{ STM32F4_RCC_APB1ENR, 11,	"wwdg",		"apb1_div" },
  	{ STM32F4_RCC_APB1ENR, 13,	"can3",		"apb1_div" },
  	{ STM32F4_RCC_APB1ENR, 14,	"spi2",		"apb1_div" },
  	{ STM32F4_RCC_APB1ENR, 15,	"spi3",		"apb1_div" },
  	{ STM32F4_RCC_APB1ENR, 16,	"spdifrx",	"apb1_div" },
  	{ STM32F4_RCC_APB1ENR, 25,	"can1",		"apb1_div" },
  	{ STM32F4_RCC_APB1ENR, 26,	"can2",		"apb1_div" },
  	{ STM32F4_RCC_APB1ENR, 27,	"cec",		"apb1_div" },
  	{ STM32F4_RCC_APB1ENR, 28,	"pwr",		"apb1_div" },
  	{ STM32F4_RCC_APB1ENR, 29,	"dac",		"apb1_div" },
  
  	{ STM32F4_RCC_APB2ENR,  0,	"tim1",		"apb2_mul" },
  	{ STM32F4_RCC_APB2ENR,  1,	"tim8",		"apb2_mul" },
  	{ STM32F4_RCC_APB2ENR,  7,	"sdmmc2",	"sdmux2" },
  	{ STM32F4_RCC_APB2ENR,  8,	"adc1",		"apb2_div" },
  	{ STM32F4_RCC_APB2ENR,  9,	"adc2",		"apb2_div" },
  	{ STM32F4_RCC_APB2ENR, 10,	"adc3",		"apb2_div" },
  	{ STM32F4_RCC_APB2ENR, 11,	"sdmmc1",	"sdmux1" },
  	{ STM32F4_RCC_APB2ENR, 12,	"spi1",		"apb2_div" },
  	{ STM32F4_RCC_APB2ENR, 13,	"spi4",		"apb2_div" },
  	{ STM32F4_RCC_APB2ENR, 14,	"syscfg",	"apb2_div" },
  	{ STM32F4_RCC_APB2ENR, 16,	"tim9",		"apb2_mul" },
  	{ STM32F4_RCC_APB2ENR, 17,	"tim10",	"apb2_mul" },
  	{ STM32F4_RCC_APB2ENR, 18,	"tim11",	"apb2_mul" },
  	{ STM32F4_RCC_APB2ENR, 20,	"spi5",		"apb2_div" },
  	{ STM32F4_RCC_APB2ENR, 21,	"spi6",		"apb2_div" },
  	{ STM32F4_RCC_APB2ENR, 22,	"sai1",		"apb2_div" },
  	{ STM32F4_RCC_APB2ENR, 23,	"sai2",		"apb2_div" },
  	{ STM32F4_RCC_APB2ENR, 26,	"ltdc",		"apb2_div" },
  	{ STM32F4_RCC_APB2ENR, 30,	"mdio",		"apb2_div" },
  };
358bdf892   Daniel Thompson   clk: stm32: Add c...
366
367
368
369
  /*
   * This bitmask tells us which bit offsets (0..192) on STM32F4[23]xxx
   * have gate bits associated with them. Its combined hweight is 71.
   */
a064a07f7   Gabriel Fernandez   clk: stm32f469: A...
370
371
372
373
374
375
376
377
378
  #define MAX_GATE_MAP 3
  
  static const u64 stm32f42xx_gate_map[MAX_GATE_MAP] = { 0x000000f17ef417ffull,
  						       0x0000000000000001ull,
  						       0x04777f33f6fec9ffull };
  
  static const u64 stm32f46xx_gate_map[MAX_GATE_MAP] = { 0x000000f17ef417ffull,
  						       0x0000000000000003ull,
  						       0x0c777f33f6fec9ffull };
88c9b70bb   Gabriel Fernandez   clk: stm32f7: Int...
379
380
  static const u64 stm32f746_gate_map[MAX_GATE_MAP] = { 0x000000f17ef417ffull,
  						      0x0000000000000003ull,
f9acf1057   Patrice Chotard   clk: stm32: Add c...
381
  						      0x04f77f833e01c9ffull };
88c9b70bb   Gabriel Fernandez   clk: stm32f7: Int...
382

936289f04   Gabriel Fernandez   clk: stm32: Intro...
383
384
385
  static const u64 stm32f769_gate_map[MAX_GATE_MAP] = { 0x000000f37ef417ffull,
  						      0x0000000000000003ull,
  						      0x44F77F833E01EDFFull };
a064a07f7   Gabriel Fernandez   clk: stm32f469: A...
386
387
388
  static const u64 *stm32f4_gate_map;
  
  static struct clk_hw **clks;
358bdf892   Daniel Thompson   clk: stm32: Add c...
389

358bdf892   Daniel Thompson   clk: stm32: Add c...
390
391
  static DEFINE_SPINLOCK(stm32f4_clk_lock);
  static void __iomem *base;
861adc44d   Gabriel Fernandez   clk: stm32f4: Add...
392
  static struct regmap *pdrm;
88c9b70bb   Gabriel Fernandez   clk: stm32f7: Int...
393
  static int stm32fx_end_primary_clk;
358bdf892   Daniel Thompson   clk: stm32: Add c...
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
  /*
   * "Multiplier" device for APBx clocks.
   *
   * The APBx dividers are power-of-two dividers and, if *not* running in 1:1
   * mode, they also tap out the one of the low order state bits to run the
   * timers. ST datasheets represent this feature as a (conditional) clock
   * multiplier.
   */
  struct clk_apb_mul {
  	struct clk_hw hw;
  	u8 bit_idx;
  };
  
  #define to_clk_apb_mul(_hw) container_of(_hw, struct clk_apb_mul, hw)
  
  static unsigned long clk_apb_mul_recalc_rate(struct clk_hw *hw,
  					     unsigned long parent_rate)
  {
  	struct clk_apb_mul *am = to_clk_apb_mul(hw);
  
  	if (readl(base + STM32F4_RCC_CFGR) & BIT(am->bit_idx))
  		return parent_rate * 2;
  
  	return parent_rate;
  }
  
  static long clk_apb_mul_round_rate(struct clk_hw *hw, unsigned long rate,
  				   unsigned long *prate)
  {
  	struct clk_apb_mul *am = to_clk_apb_mul(hw);
  	unsigned long mult = 1;
  
  	if (readl(base + STM32F4_RCC_CFGR) & BIT(am->bit_idx))
  		mult = 2;
98d8a60ec   Stephen Boyd   clk: Convert __cl...
428
  	if (clk_hw_get_flags(hw) & CLK_SET_RATE_PARENT) {
358bdf892   Daniel Thompson   clk: stm32: Add c...
429
  		unsigned long best_parent = rate / mult;
17ae4b40b   Stephen Boyd   clk: stm32f4: Con...
430
  		*prate = clk_hw_round_rate(clk_hw_get_parent(hw), best_parent);
358bdf892   Daniel Thompson   clk: stm32: Add c...
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
  	}
  
  	return *prate * mult;
  }
  
  static int clk_apb_mul_set_rate(struct clk_hw *hw, unsigned long rate,
  				unsigned long parent_rate)
  {
  	/*
  	 * We must report success but we can do so unconditionally because
  	 * clk_apb_mul_round_rate returns values that ensure this call is a
  	 * nop.
  	 */
  
  	return 0;
  }
  
  static const struct clk_ops clk_apb_mul_factor_ops = {
  	.round_rate = clk_apb_mul_round_rate,
  	.set_rate = clk_apb_mul_set_rate,
  	.recalc_rate = clk_apb_mul_recalc_rate,
  };
  
  static struct clk *clk_register_apb_mul(struct device *dev, const char *name,
  					const char *parent_name,
  					unsigned long flags, u8 bit_idx)
  {
  	struct clk_apb_mul *am;
  	struct clk_init_data init;
  	struct clk *clk;
  
  	am = kzalloc(sizeof(*am), GFP_KERNEL);
  	if (!am)
  		return ERR_PTR(-ENOMEM);
  
  	am->bit_idx = bit_idx;
  	am->hw.init = &init;
  
  	init.name = name;
  	init.ops = &clk_apb_mul_factor_ops;
  	init.flags = flags;
  	init.parent_names = &parent_name;
  	init.num_parents = 1;
  
  	clk = clk_register(dev, &am->hw);
  
  	if (IS_ERR(clk))
  		kfree(am);
  
  	return clk;
  }
83135ad3c   Gabriel Fernandez   clk: stm32f4: Add...
482
483
484
485
486
487
488
489
490
  enum {
  	PLL,
  	PLL_I2S,
  	PLL_SAI,
  };
  
  static const struct clk_div_table pll_divp_table[] = {
  	{ 0, 2 }, { 1, 4 }, { 2, 6 }, { 3, 8 }, { 0 }
  };
ef1891047   Gabriel Fernandez   clk: stm32f4: fix...
491
492
493
494
495
496
  static const struct clk_div_table pll_divq_table[] = {
  	{ 2, 2 }, { 3, 3 }, { 4, 4 }, { 5, 5 }, { 6, 6 }, { 7, 7 },
  	{ 8, 8 }, { 9, 9 }, { 10, 10 }, { 11, 11 }, { 12, 12 }, { 13, 13 },
  	{ 14, 14 }, { 15, 15 },
  	{ 0 }
  };
83135ad3c   Gabriel Fernandez   clk: stm32f4: Add...
497
498
499
500
501
502
503
504
505
506
507
508
509
510
  static const struct clk_div_table pll_divr_table[] = {
  	{ 2, 2 }, { 3, 3 }, { 4, 4 }, { 5, 5 }, { 6, 6 }, { 7, 7 }, { 0 }
  };
  
  struct stm32f4_pll {
  	spinlock_t *lock;
  	struct	clk_gate gate;
  	u8 offset;
  	u8 bit_rdy_idx;
  	u8 status;
  	u8 n_start;
  };
  
  #define to_stm32f4_pll(_gate) container_of(_gate, struct stm32f4_pll, gate)
517633ef6   Gabriel Fernandez   clk: stm32f4: Add...
511
512
513
514
515
516
517
518
519
520
521
522
  struct stm32f4_pll_post_div_data {
  	int idx;
  	u8 pll_num;
  	const char *name;
  	const char *parent;
  	u8 flag;
  	u8 offset;
  	u8 shift;
  	u8 width;
  	u8 flag_div;
  	const struct clk_div_table *div_table;
  };
83135ad3c   Gabriel Fernandez   clk: stm32f4: Add...
523
524
525
526
527
528
529
530
531
532
533
534
  struct stm32f4_vco_data {
  	const char *vco_name;
  	u8 offset;
  	u8 bit_idx;
  	u8 bit_rdy_idx;
  };
  
  static const struct stm32f4_vco_data  vco_data[] = {
  	{ "vco",     STM32F4_RCC_PLLCFGR,    24, 25 },
  	{ "vco-i2s", STM32F4_RCC_PLLI2SCFGR, 26, 27 },
  	{ "vco-sai", STM32F4_RCC_PLLSAICFGR, 28, 29 },
  };
517633ef6   Gabriel Fernandez   clk: stm32f4: Add...
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
  
  static const struct clk_div_table post_divr_table[] = {
  	{ 0, 2 }, { 1, 4 }, { 2, 8 }, { 3, 16 }, { 0 }
  };
  
  #define MAX_POST_DIV 3
  static const struct stm32f4_pll_post_div_data  post_div_data[MAX_POST_DIV] = {
  	{ CLK_I2SQ_PDIV, PLL_I2S, "plli2s-q-div", "plli2s-q",
  		CLK_SET_RATE_PARENT, STM32F4_RCC_DCKCFGR, 0, 5, 0, NULL},
  
  	{ CLK_SAIQ_PDIV, PLL_SAI, "pllsai-q-div", "pllsai-q",
  		CLK_SET_RATE_PARENT, STM32F4_RCC_DCKCFGR, 8, 5, 0, NULL },
  
  	{ NO_IDX, PLL_SAI, "pllsai-r-div", "pllsai-r", CLK_SET_RATE_PARENT,
  		STM32F4_RCC_DCKCFGR, 16, 2, 0, post_divr_table },
  };
83135ad3c   Gabriel Fernandez   clk: stm32f4: Add...
551
552
553
554
555
556
557
558
559
  struct stm32f4_div_data {
  	u8 shift;
  	u8 width;
  	u8 flag_div;
  	const struct clk_div_table *div_table;
  };
  
  #define MAX_PLL_DIV 3
  static const struct stm32f4_div_data  div_data[MAX_PLL_DIV] = {
ef1891047   Gabriel Fernandez   clk: stm32f4: fix...
560
561
562
  	{ 16, 2, 0, pll_divp_table },
  	{ 24, 4, 0, pll_divq_table },
  	{ 28, 3, 0, pll_divr_table },
83135ad3c   Gabriel Fernandez   clk: stm32f4: Add...
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
  };
  
  struct stm32f4_pll_data {
  	u8 pll_num;
  	u8 n_start;
  	const char *div_name[MAX_PLL_DIV];
  };
  
  static const struct stm32f4_pll_data stm32f429_pll[MAX_PLL_DIV] = {
  	{ PLL,	   192, { "pll", "pll48",    NULL	} },
  	{ PLL_I2S, 192, { NULL,  "plli2s-q", "plli2s-r" } },
  	{ PLL_SAI,  49, { NULL,  "pllsai-q", "pllsai-r" } },
  };
  
  static const struct stm32f4_pll_data stm32f469_pll[MAX_PLL_DIV] = {
2f05b6b92   Gabriel Fernandez   clk: stm32: Add D...
578
  	{ PLL,	   50, { "pll",	     "pll-q",    "pll-r"    } },
83135ad3c   Gabriel Fernandez   clk: stm32f4: Add...
579
580
581
582
583
584
585
586
  	{ PLL_I2S, 50, { "plli2s-p", "plli2s-q", "plli2s-r" } },
  	{ PLL_SAI, 50, { "pllsai-p", "pllsai-q", "pllsai-r" } },
  };
  
  static int stm32f4_pll_is_enabled(struct clk_hw *hw)
  {
  	return clk_gate_ops.is_enabled(hw);
  }
ac03d8b3a   Gabriel Fernandez   clk: stm32f4: fix...
587
  #define PLL_TIMEOUT 10000
83135ad3c   Gabriel Fernandez   clk: stm32f4: Add...
588
589
590
591
  static int stm32f4_pll_enable(struct clk_hw *hw)
  {
  	struct clk_gate *gate = to_clk_gate(hw);
  	struct stm32f4_pll *pll = to_stm32f4_pll(gate);
ac03d8b3a   Gabriel Fernandez   clk: stm32f4: fix...
592
593
  	int bit_status;
  	unsigned int timeout = PLL_TIMEOUT;
83135ad3c   Gabriel Fernandez   clk: stm32f4: Add...
594

ac03d8b3a   Gabriel Fernandez   clk: stm32f4: fix...
595
596
597
598
  	if (clk_gate_ops.is_enabled(hw))
  		return 0;
  
  	clk_gate_ops.enable(hw);
83135ad3c   Gabriel Fernandez   clk: stm32f4: Add...
599

ac03d8b3a   Gabriel Fernandez   clk: stm32f4: fix...
600
601
  	do {
  		bit_status = !(readl(gate->reg) & BIT(pll->bit_rdy_idx));
83135ad3c   Gabriel Fernandez   clk: stm32f4: Add...
602

ac03d8b3a   Gabriel Fernandez   clk: stm32f4: fix...
603
604
605
  	} while (bit_status && --timeout);
  
  	return bit_status;
83135ad3c   Gabriel Fernandez   clk: stm32f4: Add...
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
  }
  
  static void stm32f4_pll_disable(struct clk_hw *hw)
  {
  	clk_gate_ops.disable(hw);
  }
  
  static unsigned long stm32f4_pll_recalc(struct clk_hw *hw,
  		unsigned long parent_rate)
  {
  	struct clk_gate *gate = to_clk_gate(hw);
  	struct stm32f4_pll *pll = to_stm32f4_pll(gate);
  	unsigned long n;
  
  	n = (readl(base + pll->offset) >> 6) & 0x1ff;
  
  	return parent_rate * n;
  }
  
  static long stm32f4_pll_round_rate(struct clk_hw *hw, unsigned long rate,
  		unsigned long *prate)
358bdf892   Daniel Thompson   clk: stm32: Add c...
627
  {
83135ad3c   Gabriel Fernandez   clk: stm32f4: Add...
628
629
630
  	struct clk_gate *gate = to_clk_gate(hw);
  	struct stm32f4_pll *pll = to_stm32f4_pll(gate);
  	unsigned long n;
358bdf892   Daniel Thompson   clk: stm32: Add c...
631

83135ad3c   Gabriel Fernandez   clk: stm32f4: Add...
632
  	n = rate / *prate;
358bdf892   Daniel Thompson   clk: stm32: Add c...
633

83135ad3c   Gabriel Fernandez   clk: stm32f4: Add...
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
  	if (n < pll->n_start)
  		n = pll->n_start;
  	else if (n > 432)
  		n = 432;
  
  	return *prate * n;
  }
  
  static int stm32f4_pll_set_rate(struct clk_hw *hw, unsigned long rate,
  				unsigned long parent_rate)
  {
  	struct clk_gate *gate = to_clk_gate(hw);
  	struct stm32f4_pll *pll = to_stm32f4_pll(gate);
  
  	unsigned long n;
  	unsigned long val;
  	int pll_state;
  
  	pll_state = stm32f4_pll_is_enabled(hw);
  
  	if (pll_state)
  		stm32f4_pll_disable(hw);
  
  	n = rate  / parent_rate;
  
  	val = readl(base + pll->offset) & ~(0x1ff << 6);
  
  	writel(val | ((n & 0x1ff) <<  6), base + pll->offset);
  
  	if (pll_state)
  		stm32f4_pll_enable(hw);
  
  	return 0;
  }
  
  static const struct clk_ops stm32f4_pll_gate_ops = {
  	.enable		= stm32f4_pll_enable,
  	.disable	= stm32f4_pll_disable,
  	.is_enabled	= stm32f4_pll_is_enabled,
  	.recalc_rate	= stm32f4_pll_recalc,
  	.round_rate	= stm32f4_pll_round_rate,
  	.set_rate	= stm32f4_pll_set_rate,
  };
  
  struct stm32f4_pll_div {
  	struct clk_divider div;
  	struct clk_hw *hw_pll;
  };
  
  #define to_pll_div_clk(_div) container_of(_div, struct stm32f4_pll_div, div)
  
  static unsigned long stm32f4_pll_div_recalc_rate(struct clk_hw *hw,
  		unsigned long parent_rate)
  {
  	return clk_divider_ops.recalc_rate(hw, parent_rate);
  }
  
  static long stm32f4_pll_div_round_rate(struct clk_hw *hw, unsigned long rate,
  				unsigned long *prate)
  {
  	return clk_divider_ops.round_rate(hw, rate, prate);
  }
  
  static int stm32f4_pll_div_set_rate(struct clk_hw *hw, unsigned long rate,
  				unsigned long parent_rate)
358bdf892   Daniel Thompson   clk: stm32: Add c...
699
  {
83135ad3c   Gabriel Fernandez   clk: stm32f4: Add...
700
701
702
703
704
705
706
707
708
709
710
  	int pll_state, ret;
  
  	struct clk_divider *div = to_clk_divider(hw);
  	struct stm32f4_pll_div *pll_div = to_pll_div_clk(div);
  
  	pll_state = stm32f4_pll_is_enabled(pll_div->hw_pll);
  
  	if (pll_state)
  		stm32f4_pll_disable(pll_div->hw_pll);
  
  	ret = clk_divider_ops.set_rate(hw, rate, parent_rate);
358bdf892   Daniel Thompson   clk: stm32: Add c...
711

83135ad3c   Gabriel Fernandez   clk: stm32f4: Add...
712
713
  	if (pll_state)
  		stm32f4_pll_enable(pll_div->hw_pll);
358bdf892   Daniel Thompson   clk: stm32: Add c...
714

83135ad3c   Gabriel Fernandez   clk: stm32f4: Add...
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
  	return ret;
  }
  
  static const struct clk_ops stm32f4_pll_div_ops = {
  	.recalc_rate = stm32f4_pll_div_recalc_rate,
  	.round_rate = stm32f4_pll_div_round_rate,
  	.set_rate = stm32f4_pll_div_set_rate,
  };
  
  static struct clk_hw *clk_register_pll_div(const char *name,
  		const char *parent_name, unsigned long flags,
  		void __iomem *reg, u8 shift, u8 width,
  		u8 clk_divider_flags, const struct clk_div_table *table,
  		struct clk_hw *pll_hw, spinlock_t *lock)
  {
  	struct stm32f4_pll_div *pll_div;
  	struct clk_hw *hw;
  	struct clk_init_data init;
  	int ret;
  
  	/* allocate the divider */
  	pll_div = kzalloc(sizeof(*pll_div), GFP_KERNEL);
  	if (!pll_div)
  		return ERR_PTR(-ENOMEM);
  
  	init.name = name;
  	init.ops = &stm32f4_pll_div_ops;
  	init.flags = flags;
  	init.parent_names = (parent_name ? &parent_name : NULL);
  	init.num_parents = (parent_name ? 1 : 0);
  
  	/* struct clk_divider assignments */
  	pll_div->div.reg = reg;
  	pll_div->div.shift = shift;
  	pll_div->div.width = width;
  	pll_div->div.flags = clk_divider_flags;
  	pll_div->div.lock = lock;
  	pll_div->div.table = table;
  	pll_div->div.hw.init = &init;
  
  	pll_div->hw_pll = pll_hw;
  
  	/* register the clock */
  	hw = &pll_div->div.hw;
  	ret = clk_hw_register(NULL, hw);
  	if (ret) {
  		kfree(pll_div);
  		hw = ERR_PTR(ret);
  	}
  
  	return hw;
  }
  
  static struct clk_hw *stm32f4_rcc_register_pll(const char *pllsrc,
  		const struct stm32f4_pll_data *data,  spinlock_t *lock)
  {
  	struct stm32f4_pll *pll;
  	struct clk_init_data init = { NULL };
  	void __iomem *reg;
  	struct clk_hw *pll_hw;
  	int ret;
  	int i;
  	const struct stm32f4_vco_data *vco;
  
  
  	pll = kzalloc(sizeof(*pll), GFP_KERNEL);
  	if (!pll)
  		return ERR_PTR(-ENOMEM);
  
  	vco = &vco_data[data->pll_num];
  
  	init.name = vco->vco_name;
  	init.ops = &stm32f4_pll_gate_ops;
  	init.flags = CLK_SET_RATE_GATE;
  	init.parent_names = &pllsrc;
  	init.num_parents = 1;
  
  	pll->gate.lock = lock;
  	pll->gate.reg = base + STM32F4_RCC_CR;
  	pll->gate.bit_idx = vco->bit_idx;
  	pll->gate.hw.init = &init;
  
  	pll->offset = vco->offset;
  	pll->n_start = data->n_start;
  	pll->bit_rdy_idx = vco->bit_rdy_idx;
  	pll->status = (readl(base + STM32F4_RCC_CR) >> vco->bit_idx) & 0x1;
  
  	reg = base + pll->offset;
  
  	pll_hw = &pll->gate.hw;
  	ret = clk_hw_register(NULL, pll_hw);
  	if (ret) {
  		kfree(pll);
  		return ERR_PTR(ret);
  	}
  
  	for (i = 0; i < MAX_PLL_DIV; i++)
  		if (data->div_name[i])
  			clk_register_pll_div(data->div_name[i],
  					vco->vco_name,
  					0,
  					reg,
  					div_data[i].shift,
  					div_data[i].width,
  					div_data[i].flag_div,
  					div_data[i].div_table,
  					pll_hw,
  					lock);
  	return pll_hw;
358bdf892   Daniel Thompson   clk: stm32: Add c...
824
825
826
827
828
829
830
831
  }
  
  /*
   * Converts the primary and secondary indices (as they appear in DT) to an
   * offset into our struct clock array.
   */
  static int stm32f4_rcc_lookup_clk_idx(u8 primary, u8 secondary)
  {
a064a07f7   Gabriel Fernandez   clk: stm32f469: A...
832
  	u64 table[MAX_GATE_MAP];
358bdf892   Daniel Thompson   clk: stm32: Add c...
833
834
  
  	if (primary == 1) {
88c9b70bb   Gabriel Fernandez   clk: stm32f7: Int...
835
  		if (WARN_ON(secondary >= stm32fx_end_primary_clk))
358bdf892   Daniel Thompson   clk: stm32: Add c...
836
837
838
  			return -EINVAL;
  		return secondary;
  	}
a064a07f7   Gabriel Fernandez   clk: stm32f469: A...
839
  	memcpy(table, stm32f4_gate_map, sizeof(table));
358bdf892   Daniel Thompson   clk: stm32: Add c...
840
841
  
  	/* only bits set in table can be used as indices */
15ab38273   Daniel Thompson   clk: stm32: Fix o...
842
  	if (WARN_ON(secondary >= BITS_PER_BYTE * sizeof(table) ||
358bdf892   Daniel Thompson   clk: stm32: Add c...
843
844
845
846
847
848
849
  		    0 == (table[BIT_ULL_WORD(secondary)] &
  			  BIT_ULL_MASK(secondary))))
  		return -EINVAL;
  
  	/* mask out bits above our current index */
  	table[BIT_ULL_WORD(secondary)] &=
  	    GENMASK_ULL(secondary % BITS_PER_LONG_LONG, 0);
88c9b70bb   Gabriel Fernandez   clk: stm32f7: Int...
850
  	return stm32fx_end_primary_clk - 1 + hweight64(table[0]) +
358bdf892   Daniel Thompson   clk: stm32: Add c...
851
852
853
  	       (BIT_ULL_WORD(secondary) >= 1 ? hweight64(table[1]) : 0) +
  	       (BIT_ULL_WORD(secondary) >= 2 ? hweight64(table[2]) : 0);
  }
4e950d1ef   Stephen Boyd   clk: stm32f4: Mig...
854
  static struct clk_hw *
358bdf892   Daniel Thompson   clk: stm32: Add c...
855
856
857
858
859
860
861
862
863
  stm32f4_rcc_lookup_clk(struct of_phandle_args *clkspec, void *data)
  {
  	int i = stm32f4_rcc_lookup_clk_idx(clkspec->args[0], clkspec->args[1]);
  
  	if (i < 0)
  		return ERR_PTR(-EINVAL);
  
  	return clks[i];
  }
861adc44d   Gabriel Fernandez   clk: stm32f4: Add...
864
865
866
867
868
869
870
871
872
873
874
875
876
  #define to_rgclk(_rgate) container_of(_rgate, struct stm32_rgate, gate)
  
  static inline void disable_power_domain_write_protection(void)
  {
  	if (pdrm)
  		regmap_update_bits(pdrm, 0x00, (1 << 8), (1 << 8));
  }
  
  static inline void enable_power_domain_write_protection(void)
  {
  	if (pdrm)
  		regmap_update_bits(pdrm, 0x00, (1 << 8), (0 << 8));
  }
4261a881c   Gabriel Fernandez   clk: stm32f4: Add...
877
878
879
880
881
882
883
884
  static inline void sofware_reset_backup_domain(void)
  {
  	unsigned long val;
  
  	val = readl(base + STM32F4_RCC_BDCR);
  	writel(val | BIT(16), base + STM32F4_RCC_BDCR);
  	writel(val & ~BIT(16), base + STM32F4_RCC_BDCR);
  }
861adc44d   Gabriel Fernandez   clk: stm32f4: Add...
885
886
887
888
  struct stm32_rgate {
  	struct	clk_gate gate;
  	u8	bit_rdy_idx;
  };
ac03d8b3a   Gabriel Fernandez   clk: stm32f4: fix...
889
  #define RGATE_TIMEOUT 50000
861adc44d   Gabriel Fernandez   clk: stm32f4: Add...
890
891
892
893
894
  
  static int rgclk_enable(struct clk_hw *hw)
  {
  	struct clk_gate *gate = to_clk_gate(hw);
  	struct stm32_rgate *rgate = to_rgclk(gate);
ac03d8b3a   Gabriel Fernandez   clk: stm32f4: fix...
895
896
897
898
899
  	int bit_status;
  	unsigned int timeout = RGATE_TIMEOUT;
  
  	if (clk_gate_ops.is_enabled(hw))
  		return 0;
861adc44d   Gabriel Fernandez   clk: stm32f4: Add...
900
901
902
903
  
  	disable_power_domain_write_protection();
  
  	clk_gate_ops.enable(hw);
ac03d8b3a   Gabriel Fernandez   clk: stm32f4: fix...
904
905
906
907
908
909
  	do {
  		bit_status = !(readl(gate->reg) & BIT(rgate->bit_rdy_idx));
  		if (bit_status)
  			udelay(100);
  
  	} while (bit_status && --timeout);
861adc44d   Gabriel Fernandez   clk: stm32f4: Add...
910
911
  
  	enable_power_domain_write_protection();
ac03d8b3a   Gabriel Fernandez   clk: stm32f4: fix...
912
913
  
  	return bit_status;
861adc44d   Gabriel Fernandez   clk: stm32f4: Add...
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
  }
  
  static void rgclk_disable(struct clk_hw *hw)
  {
  	clk_gate_ops.disable(hw);
  }
  
  static int rgclk_is_enabled(struct clk_hw *hw)
  {
  	return clk_gate_ops.is_enabled(hw);
  }
  
  static const struct clk_ops rgclk_ops = {
  	.enable = rgclk_enable,
  	.disable = rgclk_disable,
  	.is_enabled = rgclk_is_enabled,
  };
  
  static struct clk_hw *clk_register_rgate(struct device *dev, const char *name,
  		const char *parent_name, unsigned long flags,
  		void __iomem *reg, u8 bit_idx, u8 bit_rdy_idx,
  		u8 clk_gate_flags, spinlock_t *lock)
  {
  	struct stm32_rgate *rgate;
  	struct clk_init_data init = { NULL };
  	struct clk_hw *hw;
  	int ret;
  
  	rgate = kzalloc(sizeof(*rgate), GFP_KERNEL);
  	if (!rgate)
  		return ERR_PTR(-ENOMEM);
  
  	init.name = name;
  	init.ops = &rgclk_ops;
  	init.flags = flags;
  	init.parent_names = &parent_name;
  	init.num_parents = 1;
  
  	rgate->bit_rdy_idx = bit_rdy_idx;
  
  	rgate->gate.lock = lock;
  	rgate->gate.reg = reg;
  	rgate->gate.bit_idx = bit_idx;
  	rgate->gate.hw.init = &init;
  
  	hw = &rgate->gate.hw;
  	ret = clk_hw_register(dev, hw);
  	if (ret) {
  		kfree(rgate);
  		hw = ERR_PTR(ret);
  	}
  
  	return hw;
  }
4261a881c   Gabriel Fernandez   clk: stm32f4: Add...
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
  static int cclk_gate_enable(struct clk_hw *hw)
  {
  	int ret;
  
  	disable_power_domain_write_protection();
  
  	ret = clk_gate_ops.enable(hw);
  
  	enable_power_domain_write_protection();
  
  	return ret;
  }
  
  static void cclk_gate_disable(struct clk_hw *hw)
  {
  	disable_power_domain_write_protection();
  
  	clk_gate_ops.disable(hw);
  
  	enable_power_domain_write_protection();
  }
  
  static int cclk_gate_is_enabled(struct clk_hw *hw)
  {
  	return clk_gate_ops.is_enabled(hw);
  }
  
  static const struct clk_ops cclk_gate_ops = {
  	.enable		= cclk_gate_enable,
  	.disable	= cclk_gate_disable,
  	.is_enabled	= cclk_gate_is_enabled,
  };
  
  static u8 cclk_mux_get_parent(struct clk_hw *hw)
  {
  	return clk_mux_ops.get_parent(hw);
  }
  
  static int cclk_mux_set_parent(struct clk_hw *hw, u8 index)
  {
  	int ret;
  
  	disable_power_domain_write_protection();
  
  	sofware_reset_backup_domain();
  
  	ret = clk_mux_ops.set_parent(hw, index);
  
  	enable_power_domain_write_protection();
  
  	return ret;
  }
  
  static const struct clk_ops cclk_mux_ops = {
  	.get_parent = cclk_mux_get_parent,
  	.set_parent = cclk_mux_set_parent,
  };
  
  static struct clk_hw *stm32_register_cclk(struct device *dev, const char *name,
  		const char * const *parent_names, int num_parents,
  		void __iomem *reg, u8 bit_idx, u8 shift, unsigned long flags,
  		spinlock_t *lock)
  {
  	struct clk_hw *hw;
  	struct clk_gate *gate;
  	struct clk_mux *mux;
  
  	gate = kzalloc(sizeof(*gate), GFP_KERNEL);
  	if (!gate) {
  		hw = ERR_PTR(-EINVAL);
  		goto fail;
  	}
  
  	mux = kzalloc(sizeof(*mux), GFP_KERNEL);
  	if (!mux) {
  		kfree(gate);
  		hw = ERR_PTR(-EINVAL);
  		goto fail;
  	}
  
  	gate->reg = reg;
  	gate->bit_idx = bit_idx;
  	gate->flags = 0;
  	gate->lock = lock;
  
  	mux->reg = reg;
  	mux->shift = shift;
  	mux->mask = 3;
  	mux->flags = 0;
  
  	hw = clk_hw_register_composite(dev, name, parent_names, num_parents,
  			&mux->hw, &cclk_mux_ops,
  			NULL, NULL,
  			&gate->hw, &cclk_gate_ops,
  			flags);
  
  	if (IS_ERR(hw)) {
  		kfree(gate);
  		kfree(mux);
  	}
  
  fail:
  	return hw;
  }
358bdf892   Daniel Thompson   clk: stm32: Add c...
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
  static const char *sys_parents[] __initdata =   { "hsi", NULL, "pll" };
  
  static const struct clk_div_table ahb_div_table[] = {
  	{ 0x0,   1 }, { 0x1,   1 }, { 0x2,   1 }, { 0x3,   1 },
  	{ 0x4,   1 }, { 0x5,   1 }, { 0x6,   1 }, { 0x7,   1 },
  	{ 0x8,   2 }, { 0x9,   4 }, { 0xa,   8 }, { 0xb,  16 },
  	{ 0xc,  64 }, { 0xd, 128 }, { 0xe, 256 }, { 0xf, 512 },
  	{ 0 },
  };
  
  static const struct clk_div_table apb_div_table[] = {
  	{ 0,  1 }, { 0,  1 }, { 0,  1 }, { 0,  1 },
  	{ 4,  2 }, { 5,  4 }, { 6,  8 }, { 7, 16 },
  	{ 0 },
  };
4261a881c   Gabriel Fernandez   clk: stm32f4: Add...
1087
1088
1089
  static const char *rtc_parents[4] = {
  	"no-clock", "lse", "lsi", "hse-rtc"
  };
936289f04   Gabriel Fernandez   clk: stm32: Intro...
1090
1091
1092
  static const char *pll_src = "pll-src";
  
  static const char *pllsrc_parent[2] = { "hsi", NULL };
2f05b6b92   Gabriel Fernandez   clk: stm32: Add D...
1093
  static const char *dsi_parent[2] = { NULL, "pll-r" };
daf2d117c   Gabriel Fernandez   clk: stm32f4: Add...
1094
  static const char *lcd_parent[1] = { "pllsai-r-div" };
12696bac3   Gabriel Fernandez   clk: stm32f4: Add...
1095
  static const char *i2s_parents[2] = { "plli2s-r", NULL };
62710c121   Gabriel Fernandez   clk: stm32f4: Add...
1096
1097
  static const char *sai_parents[4] = { "pllsai-q-div", "plli2s-q-div", NULL,
  	"no-clock" };
844ca23f5   Gabriel Fernandez   clk: stm32f4: SDI...
1098
1099
1100
  static const char *pll48_parents[2] = { "pll-q", "pllsai-p" };
  
  static const char *sdmux_parents[2] = { "pll48", "sys" };
88c9b70bb   Gabriel Fernandez   clk: stm32f7: Int...
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
  static const char *hdmi_parents[2] = { "lse", "hsi_div488" };
  
  static const char *spdif_parent[1] = { "plli2s-p" };
  
  static const char *lptim_parent[4] = { "apb1_mul", "lsi", "hsi", "lse" };
  
  static const char *uart_parents1[4] = { "apb2_div", "sys", "hsi", "lse" };
  static const char *uart_parents2[4] = { "apb1_div", "sys", "hsi", "lse" };
  
  static const char *i2c_parents[4] = { "apb1_div", "sys", "hsi", "no-clock" };
936289f04   Gabriel Fernandez   clk: stm32: Intro...
1111
1112
  static const char * const dfsdm1_src[] = { "apb2_div", "sys" };
  static const char * const adsfdm1_parent[] = { "sai1_clk", "sai2_clk" };
daf2d117c   Gabriel Fernandez   clk: stm32f4: Add...
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
  struct stm32_aux_clk {
  	int idx;
  	const char *name;
  	const char * const *parent_names;
  	int num_parents;
  	int offset_mux;
  	u8 shift;
  	u8 mask;
  	int offset_gate;
  	u8 bit_idx;
  	unsigned long flags;
  };
a064a07f7   Gabriel Fernandez   clk: stm32f469: A...
1125
1126
1127
1128
  struct stm32f4_clk_data {
  	const struct stm32f4_gate_data *gates_data;
  	const u64 *gates_map;
  	int gates_num;
83135ad3c   Gabriel Fernandez   clk: stm32f4: Add...
1129
  	const struct stm32f4_pll_data *pll_data;
daf2d117c   Gabriel Fernandez   clk: stm32f4: Add...
1130
1131
  	const struct stm32_aux_clk *aux_clk;
  	int aux_clk_num;
88c9b70bb   Gabriel Fernandez   clk: stm32f7: Int...
1132
  	int end_primary;
daf2d117c   Gabriel Fernandez   clk: stm32f4: Add...
1133
1134
1135
1136
1137
1138
1139
1140
1141
  };
  
  static const struct stm32_aux_clk stm32f429_aux_clk[] = {
  	{
  		CLK_LCD, "lcd-tft", lcd_parent, ARRAY_SIZE(lcd_parent),
  		NO_MUX, 0, 0,
  		STM32F4_RCC_APB2ENR, 26,
  		CLK_SET_RATE_PARENT
  	},
12696bac3   Gabriel Fernandez   clk: stm32f4: Add...
1142
1143
1144
1145
1146
1147
  	{
  		CLK_I2S, "i2s", i2s_parents, ARRAY_SIZE(i2s_parents),
  		STM32F4_RCC_CFGR, 23, 1,
  		NO_GATE, 0,
  		CLK_SET_RATE_PARENT
  	},
62710c121   Gabriel Fernandez   clk: stm32f4: Add...
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
  	{
  		CLK_SAI1, "sai1-a", sai_parents, ARRAY_SIZE(sai_parents),
  		STM32F4_RCC_DCKCFGR, 20, 3,
  		STM32F4_RCC_APB2ENR, 22,
  		CLK_SET_RATE_PARENT
  	},
  	{
  		CLK_SAI2, "sai1-b", sai_parents, ARRAY_SIZE(sai_parents),
  		STM32F4_RCC_DCKCFGR, 22, 3,
  		STM32F4_RCC_APB2ENR, 22,
  		CLK_SET_RATE_PARENT
  	},
a064a07f7   Gabriel Fernandez   clk: stm32f469: A...
1160
  };
844ca23f5   Gabriel Fernandez   clk: stm32f4: SDI...
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
  static const struct stm32_aux_clk stm32f469_aux_clk[] = {
  	{
  		CLK_LCD, "lcd-tft", lcd_parent, ARRAY_SIZE(lcd_parent),
  		NO_MUX, 0, 0,
  		STM32F4_RCC_APB2ENR, 26,
  		CLK_SET_RATE_PARENT
  	},
  	{
  		CLK_I2S, "i2s", i2s_parents, ARRAY_SIZE(i2s_parents),
  		STM32F4_RCC_CFGR, 23, 1,
  		NO_GATE, 0,
  		CLK_SET_RATE_PARENT
  	},
  	{
  		CLK_SAI1, "sai1-a", sai_parents, ARRAY_SIZE(sai_parents),
  		STM32F4_RCC_DCKCFGR, 20, 3,
  		STM32F4_RCC_APB2ENR, 22,
  		CLK_SET_RATE_PARENT
  	},
  	{
  		CLK_SAI2, "sai1-b", sai_parents, ARRAY_SIZE(sai_parents),
  		STM32F4_RCC_DCKCFGR, 22, 3,
  		STM32F4_RCC_APB2ENR, 22,
  		CLK_SET_RATE_PARENT
  	},
  	{
  		NO_IDX, "pll48", pll48_parents, ARRAY_SIZE(pll48_parents),
  		STM32F4_RCC_DCKCFGR, 27, 1,
  		NO_GATE, 0,
  		0
  	},
  	{
  		NO_IDX, "sdmux", sdmux_parents, ARRAY_SIZE(sdmux_parents),
  		STM32F4_RCC_DCKCFGR, 28, 1,
  		NO_GATE, 0,
  		0
  	},
2f05b6b92   Gabriel Fernandez   clk: stm32: Add D...
1198
1199
1200
1201
1202
1203
  	{
  		CLK_F469_DSI, "dsi", dsi_parent, ARRAY_SIZE(dsi_parent),
  		STM32F4_RCC_DCKCFGR, 29, 1,
  		STM32F4_RCC_APB2ENR, 27,
  		CLK_SET_RATE_PARENT | CLK_SET_RATE_NO_REPARENT
  	},
a064a07f7   Gabriel Fernandez   clk: stm32f469: A...
1204
  };
88c9b70bb   Gabriel Fernandez   clk: stm32f7: Int...
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
  static const struct stm32_aux_clk stm32f746_aux_clk[] = {
  	{
  		CLK_LCD, "lcd-tft", lcd_parent, ARRAY_SIZE(lcd_parent),
  		NO_MUX, 0, 0,
  		STM32F4_RCC_APB2ENR, 26,
  		CLK_SET_RATE_PARENT
  	},
  	{
  		CLK_I2S, "i2s", i2s_parents, ARRAY_SIZE(i2s_parents),
  		STM32F4_RCC_CFGR, 23, 1,
  		NO_GATE, 0,
  		CLK_SET_RATE_PARENT
  	},
  	{
  		CLK_SAI1, "sai1_clk", sai_parents, ARRAY_SIZE(sai_parents),
  		STM32F4_RCC_DCKCFGR, 20, 3,
  		STM32F4_RCC_APB2ENR, 22,
  		CLK_SET_RATE_PARENT
  	},
  	{
  		CLK_SAI2, "sai2_clk", sai_parents, ARRAY_SIZE(sai_parents),
  		STM32F4_RCC_DCKCFGR, 22, 3,
  		STM32F4_RCC_APB2ENR, 23,
  		CLK_SET_RATE_PARENT
  	},
  	{
  		NO_IDX, "pll48", pll48_parents, ARRAY_SIZE(pll48_parents),
  		STM32F7_RCC_DCKCFGR2, 27, 1,
  		NO_GATE, 0,
  		0
  	},
  	{
  		NO_IDX, "sdmux", sdmux_parents, ARRAY_SIZE(sdmux_parents),
  		STM32F7_RCC_DCKCFGR2, 28, 1,
  		NO_GATE, 0,
  		0
  	},
  	{
  		CLK_HDMI_CEC, "hdmi-cec",
  		hdmi_parents, ARRAY_SIZE(hdmi_parents),
  		STM32F7_RCC_DCKCFGR2, 26, 1,
  		NO_GATE, 0,
  		0
  	},
  	{
  		CLK_SPDIF, "spdif-rx",
  		spdif_parent, ARRAY_SIZE(spdif_parent),
  		STM32F7_RCC_DCKCFGR2, 22, 3,
  		STM32F4_RCC_APB2ENR, 23,
  		CLK_SET_RATE_PARENT
  	},
  	{
  		CLK_USART1, "usart1",
  		uart_parents1, ARRAY_SIZE(uart_parents1),
  		STM32F7_RCC_DCKCFGR2, 0, 3,
  		STM32F4_RCC_APB2ENR, 4,
  		CLK_SET_RATE_PARENT,
  	},
  	{
  		CLK_USART2, "usart2",
  		uart_parents2, ARRAY_SIZE(uart_parents1),
  		STM32F7_RCC_DCKCFGR2, 2, 3,
  		STM32F4_RCC_APB1ENR, 17,
  		CLK_SET_RATE_PARENT,
  	},
  	{
  		CLK_USART3, "usart3",
  		uart_parents2, ARRAY_SIZE(uart_parents1),
  		STM32F7_RCC_DCKCFGR2, 4, 3,
  		STM32F4_RCC_APB1ENR, 18,
  		CLK_SET_RATE_PARENT,
  	},
  	{
  		CLK_UART4, "uart4",
  		uart_parents2, ARRAY_SIZE(uart_parents1),
  		STM32F7_RCC_DCKCFGR2, 6, 3,
  		STM32F4_RCC_APB1ENR, 19,
  		CLK_SET_RATE_PARENT,
  	},
  	{
  		CLK_UART5, "uart5",
  		uart_parents2, ARRAY_SIZE(uart_parents1),
  		STM32F7_RCC_DCKCFGR2, 8, 3,
  		STM32F4_RCC_APB1ENR, 20,
  		CLK_SET_RATE_PARENT,
  	},
  	{
  		CLK_USART6, "usart6",
  		uart_parents1, ARRAY_SIZE(uart_parents1),
  		STM32F7_RCC_DCKCFGR2, 10, 3,
  		STM32F4_RCC_APB2ENR, 5,
  		CLK_SET_RATE_PARENT,
  	},
  
  	{
  		CLK_UART7, "uart7",
  		uart_parents2, ARRAY_SIZE(uart_parents1),
  		STM32F7_RCC_DCKCFGR2, 12, 3,
  		STM32F4_RCC_APB1ENR, 30,
  		CLK_SET_RATE_PARENT,
  	},
  	{
  		CLK_UART8, "uart8",
  		uart_parents2, ARRAY_SIZE(uart_parents1),
  		STM32F7_RCC_DCKCFGR2, 14, 3,
  		STM32F4_RCC_APB1ENR, 31,
  		CLK_SET_RATE_PARENT,
  	},
  	{
  		CLK_I2C1, "i2c1",
  		i2c_parents, ARRAY_SIZE(i2c_parents),
  		STM32F7_RCC_DCKCFGR2, 16, 3,
  		STM32F4_RCC_APB1ENR, 21,
  		CLK_SET_RATE_PARENT,
  	},
  	{
  		CLK_I2C2, "i2c2",
  		i2c_parents, ARRAY_SIZE(i2c_parents),
  		STM32F7_RCC_DCKCFGR2, 18, 3,
  		STM32F4_RCC_APB1ENR, 22,
  		CLK_SET_RATE_PARENT,
  	},
  	{
  		CLK_I2C3, "i2c3",
  		i2c_parents, ARRAY_SIZE(i2c_parents),
  		STM32F7_RCC_DCKCFGR2, 20, 3,
  		STM32F4_RCC_APB1ENR, 23,
  		CLK_SET_RATE_PARENT,
  	},
  	{
  		CLK_I2C4, "i2c4",
  		i2c_parents, ARRAY_SIZE(i2c_parents),
  		STM32F7_RCC_DCKCFGR2, 22, 3,
  		STM32F4_RCC_APB1ENR, 24,
  		CLK_SET_RATE_PARENT,
  	},
  
  	{
  		CLK_LPTIMER, "lptim1",
  		lptim_parent, ARRAY_SIZE(lptim_parent),
  		STM32F7_RCC_DCKCFGR2, 24, 3,
  		STM32F4_RCC_APB1ENR, 9,
  		CLK_SET_RATE_PARENT
  	},
  };
936289f04   Gabriel Fernandez   clk: stm32: Intro...
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
  static const struct stm32_aux_clk stm32f769_aux_clk[] = {
  	{
  		CLK_LCD, "lcd-tft", lcd_parent, ARRAY_SIZE(lcd_parent),
  		NO_MUX, 0, 0,
  		STM32F4_RCC_APB2ENR, 26,
  		CLK_SET_RATE_PARENT
  	},
  	{
  		CLK_I2S, "i2s", i2s_parents, ARRAY_SIZE(i2s_parents),
  		STM32F4_RCC_CFGR, 23, 1,
  		NO_GATE, 0,
  		CLK_SET_RATE_PARENT
  	},
  	{
  		CLK_SAI1, "sai1_clk", sai_parents, ARRAY_SIZE(sai_parents),
  		STM32F4_RCC_DCKCFGR, 20, 3,
  		STM32F4_RCC_APB2ENR, 22,
  		CLK_SET_RATE_PARENT
  	},
  	{
  		CLK_SAI2, "sai2_clk", sai_parents, ARRAY_SIZE(sai_parents),
  		STM32F4_RCC_DCKCFGR, 22, 3,
  		STM32F4_RCC_APB2ENR, 23,
  		CLK_SET_RATE_PARENT
  	},
  	{
  		NO_IDX, "pll48", pll48_parents, ARRAY_SIZE(pll48_parents),
  		STM32F7_RCC_DCKCFGR2, 27, 1,
  		NO_GATE, 0,
  		0
  	},
  	{
  		NO_IDX, "sdmux1", sdmux_parents, ARRAY_SIZE(sdmux_parents),
  		STM32F7_RCC_DCKCFGR2, 28, 1,
  		NO_GATE, 0,
  		0
  	},
  	{
  		NO_IDX, "sdmux2", sdmux_parents, ARRAY_SIZE(sdmux_parents),
  		STM32F7_RCC_DCKCFGR2, 29, 1,
  		NO_GATE, 0,
  		0
  	},
  	{
  		CLK_HDMI_CEC, "hdmi-cec",
  		hdmi_parents, ARRAY_SIZE(hdmi_parents),
  		STM32F7_RCC_DCKCFGR2, 26, 1,
  		NO_GATE, 0,
  		0
  	},
  	{
  		CLK_SPDIF, "spdif-rx",
  		spdif_parent, ARRAY_SIZE(spdif_parent),
  		STM32F7_RCC_DCKCFGR2, 22, 3,
  		STM32F4_RCC_APB2ENR, 23,
  		CLK_SET_RATE_PARENT
  	},
  	{
  		CLK_USART1, "usart1",
  		uart_parents1, ARRAY_SIZE(uart_parents1),
  		STM32F7_RCC_DCKCFGR2, 0, 3,
  		STM32F4_RCC_APB2ENR, 4,
  		CLK_SET_RATE_PARENT,
  	},
  	{
  		CLK_USART2, "usart2",
  		uart_parents2, ARRAY_SIZE(uart_parents1),
  		STM32F7_RCC_DCKCFGR2, 2, 3,
  		STM32F4_RCC_APB1ENR, 17,
  		CLK_SET_RATE_PARENT,
  	},
  	{
  		CLK_USART3, "usart3",
  		uart_parents2, ARRAY_SIZE(uart_parents1),
  		STM32F7_RCC_DCKCFGR2, 4, 3,
  		STM32F4_RCC_APB1ENR, 18,
  		CLK_SET_RATE_PARENT,
  	},
  	{
  		CLK_UART4, "uart4",
  		uart_parents2, ARRAY_SIZE(uart_parents1),
  		STM32F7_RCC_DCKCFGR2, 6, 3,
  		STM32F4_RCC_APB1ENR, 19,
  		CLK_SET_RATE_PARENT,
  	},
  	{
  		CLK_UART5, "uart5",
  		uart_parents2, ARRAY_SIZE(uart_parents1),
  		STM32F7_RCC_DCKCFGR2, 8, 3,
  		STM32F4_RCC_APB1ENR, 20,
  		CLK_SET_RATE_PARENT,
  	},
  	{
  		CLK_USART6, "usart6",
  		uart_parents1, ARRAY_SIZE(uart_parents1),
  		STM32F7_RCC_DCKCFGR2, 10, 3,
  		STM32F4_RCC_APB2ENR, 5,
  		CLK_SET_RATE_PARENT,
  	},
  	{
  		CLK_UART7, "uart7",
  		uart_parents2, ARRAY_SIZE(uart_parents1),
  		STM32F7_RCC_DCKCFGR2, 12, 3,
  		STM32F4_RCC_APB1ENR, 30,
  		CLK_SET_RATE_PARENT,
  	},
  	{
  		CLK_UART8, "uart8",
  		uart_parents2, ARRAY_SIZE(uart_parents1),
  		STM32F7_RCC_DCKCFGR2, 14, 3,
  		STM32F4_RCC_APB1ENR, 31,
  		CLK_SET_RATE_PARENT,
  	},
  	{
  		CLK_I2C1, "i2c1",
  		i2c_parents, ARRAY_SIZE(i2c_parents),
  		STM32F7_RCC_DCKCFGR2, 16, 3,
  		STM32F4_RCC_APB1ENR, 21,
  		CLK_SET_RATE_PARENT,
  	},
  	{
  		CLK_I2C2, "i2c2",
  		i2c_parents, ARRAY_SIZE(i2c_parents),
  		STM32F7_RCC_DCKCFGR2, 18, 3,
  		STM32F4_RCC_APB1ENR, 22,
  		CLK_SET_RATE_PARENT,
  	},
  	{
  		CLK_I2C3, "i2c3",
  		i2c_parents, ARRAY_SIZE(i2c_parents),
  		STM32F7_RCC_DCKCFGR2, 20, 3,
  		STM32F4_RCC_APB1ENR, 23,
  		CLK_SET_RATE_PARENT,
  	},
  	{
  		CLK_I2C4, "i2c4",
  		i2c_parents, ARRAY_SIZE(i2c_parents),
  		STM32F7_RCC_DCKCFGR2, 22, 3,
  		STM32F4_RCC_APB1ENR, 24,
  		CLK_SET_RATE_PARENT,
  	},
  	{
  		CLK_LPTIMER, "lptim1",
  		lptim_parent, ARRAY_SIZE(lptim_parent),
  		STM32F7_RCC_DCKCFGR2, 24, 3,
  		STM32F4_RCC_APB1ENR, 9,
  		CLK_SET_RATE_PARENT
  	},
  	{
  		CLK_F769_DSI, "dsi",
  		dsi_parent, ARRAY_SIZE(dsi_parent),
  		STM32F7_RCC_DCKCFGR2, 0, 1,
  		STM32F4_RCC_APB2ENR, 27,
  		CLK_SET_RATE_PARENT
  	},
  	{
  		CLK_DFSDM1, "dfsdm1",
  		dfsdm1_src, ARRAY_SIZE(dfsdm1_src),
  		STM32F4_RCC_DCKCFGR, 25, 1,
  		STM32F4_RCC_APB2ENR, 29,
  		CLK_SET_RATE_PARENT
  	},
  	{
  		CLK_ADFSDM1, "adfsdm1",
  		adsfdm1_parent, ARRAY_SIZE(adsfdm1_parent),
  		STM32F4_RCC_DCKCFGR, 26, 1,
  		STM32F4_RCC_APB2ENR, 29,
  		CLK_SET_RATE_PARENT
  	},
  };
a064a07f7   Gabriel Fernandez   clk: stm32f469: A...
1520
  static const struct stm32f4_clk_data stm32f429_clk_data = {
88c9b70bb   Gabriel Fernandez   clk: stm32f7: Int...
1521
  	.end_primary	= END_PRIMARY_CLK,
a064a07f7   Gabriel Fernandez   clk: stm32f469: A...
1522
1523
1524
  	.gates_data	= stm32f429_gates,
  	.gates_map	= stm32f42xx_gate_map,
  	.gates_num	= ARRAY_SIZE(stm32f429_gates),
83135ad3c   Gabriel Fernandez   clk: stm32f4: Add...
1525
  	.pll_data	= stm32f429_pll,
daf2d117c   Gabriel Fernandez   clk: stm32f4: Add...
1526
1527
  	.aux_clk	= stm32f429_aux_clk,
  	.aux_clk_num	= ARRAY_SIZE(stm32f429_aux_clk),
a064a07f7   Gabriel Fernandez   clk: stm32f469: A...
1528
1529
1530
  };
  
  static const struct stm32f4_clk_data stm32f469_clk_data = {
88c9b70bb   Gabriel Fernandez   clk: stm32f7: Int...
1531
  	.end_primary	= END_PRIMARY_CLK,
a064a07f7   Gabriel Fernandez   clk: stm32f469: A...
1532
1533
1534
  	.gates_data	= stm32f469_gates,
  	.gates_map	= stm32f46xx_gate_map,
  	.gates_num	= ARRAY_SIZE(stm32f469_gates),
83135ad3c   Gabriel Fernandez   clk: stm32f4: Add...
1535
  	.pll_data	= stm32f469_pll,
844ca23f5   Gabriel Fernandez   clk: stm32f4: SDI...
1536
1537
  	.aux_clk	= stm32f469_aux_clk,
  	.aux_clk_num	= ARRAY_SIZE(stm32f469_aux_clk),
a064a07f7   Gabriel Fernandez   clk: stm32f469: A...
1538
  };
88c9b70bb   Gabriel Fernandez   clk: stm32f7: Int...
1539
1540
1541
1542
1543
1544
1545
1546
1547
  static const struct stm32f4_clk_data stm32f746_clk_data = {
  	.end_primary	= END_PRIMARY_CLK_F7,
  	.gates_data	= stm32f746_gates,
  	.gates_map	= stm32f746_gate_map,
  	.gates_num	= ARRAY_SIZE(stm32f746_gates),
  	.pll_data	= stm32f469_pll,
  	.aux_clk	= stm32f746_aux_clk,
  	.aux_clk_num	= ARRAY_SIZE(stm32f746_aux_clk),
  };
936289f04   Gabriel Fernandez   clk: stm32: Intro...
1548
1549
1550
1551
1552
1553
1554
1555
1556
  static const struct stm32f4_clk_data stm32f769_clk_data = {
  	.end_primary	= END_PRIMARY_CLK_F7,
  	.gates_data	= stm32f769_gates,
  	.gates_map	= stm32f769_gate_map,
  	.gates_num	= ARRAY_SIZE(stm32f769_gates),
  	.pll_data	= stm32f469_pll,
  	.aux_clk	= stm32f769_aux_clk,
  	.aux_clk_num	= ARRAY_SIZE(stm32f769_aux_clk),
  };
a064a07f7   Gabriel Fernandez   clk: stm32f469: A...
1557
1558
1559
1560
1561
1562
1563
1564
1565
  static const struct of_device_id stm32f4_of_match[] = {
  	{
  		.compatible = "st,stm32f42xx-rcc",
  		.data = &stm32f429_clk_data
  	},
  	{
  		.compatible = "st,stm32f469-rcc",
  		.data = &stm32f469_clk_data
  	},
88c9b70bb   Gabriel Fernandez   clk: stm32f7: Int...
1566
1567
1568
1569
  	{
  		.compatible = "st,stm32f746-rcc",
  		.data = &stm32f746_clk_data
  	},
936289f04   Gabriel Fernandez   clk: stm32: Intro...
1570
1571
1572
1573
  	{
  		.compatible = "st,stm32f769-rcc",
  		.data = &stm32f769_clk_data
  	},
a064a07f7   Gabriel Fernandez   clk: stm32f469: A...
1574
1575
  	{}
  };
daf2d117c   Gabriel Fernandez   clk: stm32f4: Add...
1576
1577
1578
1579
1580
1581
1582
  static struct clk_hw *stm32_register_aux_clk(const char *name,
  		const char * const *parent_names, int num_parents,
  		int offset_mux, u8 shift, u8 mask,
  		int offset_gate, u8 bit_idx,
  		unsigned long flags, spinlock_t *lock)
  {
  	struct clk_hw *hw;
89d5dcc48   Arnd Bergmann   clk: stm32f4: avo...
1583
  	struct clk_gate *gate = NULL;
daf2d117c   Gabriel Fernandez   clk: stm32f4: Add...
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
  	struct clk_mux *mux = NULL;
  	struct clk_hw *mux_hw = NULL, *gate_hw = NULL;
  	const struct clk_ops *mux_ops = NULL, *gate_ops = NULL;
  
  	if (offset_gate != NO_GATE) {
  		gate = kzalloc(sizeof(*gate), GFP_KERNEL);
  		if (!gate) {
  			hw = ERR_PTR(-EINVAL);
  			goto fail;
  		}
  
  		gate->reg = base + offset_gate;
  		gate->bit_idx = bit_idx;
  		gate->flags = 0;
  		gate->lock = lock;
  		gate_hw = &gate->hw;
  		gate_ops = &clk_gate_ops;
  	}
  
  	if (offset_mux != NO_MUX) {
  		mux = kzalloc(sizeof(*mux), GFP_KERNEL);
  		if (!mux) {
daf2d117c   Gabriel Fernandez   clk: stm32f4: Add...
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
  			hw = ERR_PTR(-EINVAL);
  			goto fail;
  		}
  
  		mux->reg = base + offset_mux;
  		mux->shift = shift;
  		mux->mask = mask;
  		mux->flags = 0;
  		mux_hw = &mux->hw;
  		mux_ops = &clk_mux_ops;
  	}
89d5dcc48   Arnd Bergmann   clk: stm32f4: avo...
1617
1618
1619
1620
  	if (mux_hw == NULL && gate_hw == NULL) {
  		hw = ERR_PTR(-EINVAL);
  		goto fail;
  	}
daf2d117c   Gabriel Fernandez   clk: stm32f4: Add...
1621
1622
1623
1624
1625
1626
  
  	hw = clk_hw_register_composite(NULL, name, parent_names, num_parents,
  			mux_hw, mux_ops,
  			NULL, NULL,
  			gate_hw, gate_ops,
  			flags);
89d5dcc48   Arnd Bergmann   clk: stm32f4: avo...
1627
  fail:
daf2d117c   Gabriel Fernandez   clk: stm32f4: Add...
1628
1629
1630
1631
  	if (IS_ERR(hw)) {
  		kfree(gate);
  		kfree(mux);
  	}
89d5dcc48   Arnd Bergmann   clk: stm32f4: avo...
1632

daf2d117c   Gabriel Fernandez   clk: stm32f4: Add...
1633
1634
  	return hw;
  }
358bdf892   Daniel Thompson   clk: stm32: Add c...
1635
1636
  static void __init stm32f4_rcc_init(struct device_node *np)
  {
12696bac3   Gabriel Fernandez   clk: stm32f4: Add...
1637
  	const char *hse_clk, *i2s_in_clk;
358bdf892   Daniel Thompson   clk: stm32: Add c...
1638
  	int n;
a064a07f7   Gabriel Fernandez   clk: stm32f469: A...
1639
1640
  	const struct of_device_id *match;
  	const struct stm32f4_clk_data *data;
83135ad3c   Gabriel Fernandez   clk: stm32f4: Add...
1641
  	unsigned long pllm;
936289f04   Gabriel Fernandez   clk: stm32: Intro...
1642
  	struct clk_hw *pll_src_hw;
358bdf892   Daniel Thompson   clk: stm32: Add c...
1643
1644
1645
  
  	base = of_iomap(np, 0);
  	if (!base) {
e665f029a   Rob Herring   clk: Convert to u...
1646
1647
  		pr_err("%pOFn: unable to map resource
  ", np);
358bdf892   Daniel Thompson   clk: stm32: Add c...
1648
1649
  		return;
  	}
861adc44d   Gabriel Fernandez   clk: stm32f4: Add...
1650
1651
1652
1653
1654
1655
  	pdrm = syscon_regmap_lookup_by_phandle(np, "st,syscfg");
  	if (IS_ERR(pdrm)) {
  		pdrm = NULL;
  		pr_warn("%s: Unable to get syscfg
  ", __func__);
  	}
a064a07f7   Gabriel Fernandez   clk: stm32f469: A...
1656
1657
1658
1659
1660
  	match = of_match_node(stm32f4_of_match, np);
  	if (WARN_ON(!match))
  		return;
  
  	data = match->data;
88c9b70bb   Gabriel Fernandez   clk: stm32f7: Int...
1661
1662
1663
  	stm32fx_end_primary_clk = data->end_primary;
  
  	clks = kmalloc_array(data->gates_num + stm32fx_end_primary_clk,
a064a07f7   Gabriel Fernandez   clk: stm32f469: A...
1664
1665
1666
1667
1668
  			sizeof(*clks), GFP_KERNEL);
  	if (!clks)
  		goto fail;
  
  	stm32f4_gate_map = data->gates_map;
358bdf892   Daniel Thompson   clk: stm32: Add c...
1669
  	hse_clk = of_clk_get_parent_name(np, 0);
2f05b6b92   Gabriel Fernandez   clk: stm32: Add D...
1670
  	dsi_parent[0] = hse_clk;
936289f04   Gabriel Fernandez   clk: stm32: Intro...
1671
  	pllsrc_parent[1] = hse_clk;
358bdf892   Daniel Thompson   clk: stm32: Add c...
1672

12696bac3   Gabriel Fernandez   clk: stm32f4: Add...
1673
1674
1675
  	i2s_in_clk = of_clk_get_parent_name(np, 1);
  
  	i2s_parents[1] = i2s_in_clk;
62710c121   Gabriel Fernandez   clk: stm32f4: Add...
1676
  	sai_parents[2] = i2s_in_clk;
12696bac3   Gabriel Fernandez   clk: stm32f4: Add...
1677

936289f04   Gabriel Fernandez   clk: stm32: Intro...
1678
1679
1680
1681
1682
1683
1684
  	if (of_device_is_compatible(np, "st,stm32f769-rcc")) {
  		clk_hw_register_gate(NULL, "dfsdm1_apb", "apb2_div", 0,
  				     base + STM32F4_RCC_APB2ENR, 29,
  				     CLK_IGNORE_UNUSED, &stm32f4_clk_lock);
  		dsi_parent[0] = pll_src;
  		sai_parents[3] = pll_src;
  	}
88c9b70bb   Gabriel Fernandez   clk: stm32f7: Int...
1685
1686
  	clks[CLK_HSI] = clk_hw_register_fixed_rate_with_accuracy(NULL, "hsi",
  			NULL, 0, 16000000, 160000);
936289f04   Gabriel Fernandez   clk: stm32: Intro...
1687
1688
1689
1690
1691
1692
  	pll_src_hw = clk_hw_register_mux(NULL, pll_src, pllsrc_parent,
  					 ARRAY_SIZE(pllsrc_parent), 0,
  					 base + STM32F4_RCC_PLLCFGR, 22, 1, 0,
  					 &stm32f4_clk_lock);
  
  	pllm = readl(base + STM32F4_RCC_PLLCFGR) & 0x3f;
83135ad3c   Gabriel Fernandez   clk: stm32f4: Add...
1693

936289f04   Gabriel Fernandez   clk: stm32: Intro...
1694
1695
  	clk_hw_register_fixed_factor(NULL, "vco_in", pll_src,
  				     0, 1, pllm);
83135ad3c   Gabriel Fernandez   clk: stm32f4: Add...
1696
1697
1698
1699
1700
1701
1702
1703
1704
  
  	stm32f4_rcc_register_pll("vco_in", &data->pll_data[0],
  			&stm32f4_clk_lock);
  
  	clks[PLL_VCO_I2S] = stm32f4_rcc_register_pll("vco_in",
  			&data->pll_data[1], &stm32f4_clk_lock);
  
  	clks[PLL_VCO_SAI] = stm32f4_rcc_register_pll("vco_in",
  			&data->pll_data[2], &stm32f4_clk_lock);
358bdf892   Daniel Thompson   clk: stm32: Add c...
1705

517633ef6   Gabriel Fernandez   clk: stm32f4: Add...
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
  	for (n = 0; n < MAX_POST_DIV; n++) {
  		const struct stm32f4_pll_post_div_data *post_div;
  		struct clk_hw *hw;
  
  		post_div = &post_div_data[n];
  
  		hw = clk_register_pll_div(post_div->name,
  				post_div->parent,
  				post_div->flag,
  				base + post_div->offset,
  				post_div->shift,
  				post_div->width,
  				post_div->flag_div,
  				post_div->div_table,
  				clks[post_div->pll_num],
  				&stm32f4_clk_lock);
  
  		if (post_div->idx != NO_IDX)
  			clks[post_div->idx] = hw;
  	}
358bdf892   Daniel Thompson   clk: stm32: Add c...
1726
1727
  
  	sys_parents[1] = hse_clk;
88c9b70bb   Gabriel Fernandez   clk: stm32f7: Int...
1728
1729
  
  	clks[CLK_SYSCLK] = clk_hw_register_mux_table(
358bdf892   Daniel Thompson   clk: stm32: Add c...
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
  	    NULL, "sys", sys_parents, ARRAY_SIZE(sys_parents), 0,
  	    base + STM32F4_RCC_CFGR, 0, 3, 0, NULL, &stm32f4_clk_lock);
  
  	clk_register_divider_table(NULL, "ahb_div", "sys",
  				   CLK_SET_RATE_PARENT, base + STM32F4_RCC_CFGR,
  				   4, 4, 0, ahb_div_table, &stm32f4_clk_lock);
  
  	clk_register_divider_table(NULL, "apb1_div", "ahb_div",
  				   CLK_SET_RATE_PARENT, base + STM32F4_RCC_CFGR,
  				   10, 3, 0, apb_div_table, &stm32f4_clk_lock);
  	clk_register_apb_mul(NULL, "apb1_mul", "apb1_div",
  			     CLK_SET_RATE_PARENT, 12);
  
  	clk_register_divider_table(NULL, "apb2_div", "ahb_div",
  				   CLK_SET_RATE_PARENT, base + STM32F4_RCC_CFGR,
  				   13, 3, 0, apb_div_table, &stm32f4_clk_lock);
  	clk_register_apb_mul(NULL, "apb2_mul", "apb2_div",
  			     CLK_SET_RATE_PARENT, 15);
4e950d1ef   Stephen Boyd   clk: stm32f4: Mig...
1748
  	clks[SYSTICK] = clk_hw_register_fixed_factor(NULL, "systick", "ahb_div",
358bdf892   Daniel Thompson   clk: stm32: Add c...
1749
  						  0, 1, 8);
4e950d1ef   Stephen Boyd   clk: stm32f4: Mig...
1750
  	clks[FCLK] = clk_hw_register_fixed_factor(NULL, "fclk", "ahb_div",
358bdf892   Daniel Thompson   clk: stm32: Add c...
1751
  					       0, 1, 1);
a064a07f7   Gabriel Fernandez   clk: stm32f469: A...
1752
1753
1754
1755
1756
1757
1758
1759
1760
  	for (n = 0; n < data->gates_num; n++) {
  		const struct stm32f4_gate_data *gd;
  		unsigned int secondary;
  		int idx;
  
  		gd = &data->gates_data[n];
  		secondary = 8 * (gd->offset - STM32F4_RCC_AHB1ENR) +
  			gd->bit_idx;
  		idx = stm32f4_rcc_lookup_clk_idx(0, secondary);
358bdf892   Daniel Thompson   clk: stm32: Add c...
1761
1762
1763
  
  		if (idx < 0)
  			goto fail;
4e950d1ef   Stephen Boyd   clk: stm32f4: Mig...
1764
  		clks[idx] = clk_hw_register_gate(
358bdf892   Daniel Thompson   clk: stm32: Add c...
1765
1766
  		    NULL, gd->name, gd->parent_name, gd->flags,
  		    base + gd->offset, gd->bit_idx, 0, &stm32f4_clk_lock);
334e125b4   Christophe JAILLET   clk: stm32f4: fix...
1767
  		if (IS_ERR(clks[idx])) {
166739312   Rob Herring   clk: Convert to u...
1768
1769
1770
  			pr_err("%pOF: Unable to register leaf clock %s
  ",
  			       np, gd->name);
358bdf892   Daniel Thompson   clk: stm32: Add c...
1771
1772
1773
  			goto fail;
  		}
  	}
861adc44d   Gabriel Fernandez   clk: stm32f4: Add...
1774
  	clks[CLK_LSI] = clk_register_rgate(NULL, "lsi", "clk-lsi", 0,
ac03d8b3a   Gabriel Fernandez   clk: stm32f4: fix...
1775
  			base + STM32F4_RCC_CSR, 0, 1, 0, &stm32f4_clk_lock);
861adc44d   Gabriel Fernandez   clk: stm32f4: Add...
1776
1777
1778
1779
1780
1781
1782
1783
  
  	if (IS_ERR(clks[CLK_LSI])) {
  		pr_err("Unable to register lsi clock
  ");
  		goto fail;
  	}
  
  	clks[CLK_LSE] = clk_register_rgate(NULL, "lse", "clk-lse", 0,
ac03d8b3a   Gabriel Fernandez   clk: stm32f4: fix...
1784
  			base + STM32F4_RCC_BDCR, 0, 1, 0, &stm32f4_clk_lock);
861adc44d   Gabriel Fernandez   clk: stm32f4: Add...
1785
1786
1787
1788
1789
1790
  
  	if (IS_ERR(clks[CLK_LSE])) {
  		pr_err("Unable to register lse clock
  ");
  		goto fail;
  	}
4261a881c   Gabriel Fernandez   clk: stm32f4: Add...
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
  	clks[CLK_HSE_RTC] = clk_hw_register_divider(NULL, "hse-rtc", "clk-hse",
  			0, base + STM32F4_RCC_CFGR, 16, 5, 0,
  			&stm32f4_clk_lock);
  
  	if (IS_ERR(clks[CLK_HSE_RTC])) {
  		pr_err("Unable to register hse-rtc clock
  ");
  		goto fail;
  	}
  
  	clks[CLK_RTC] = stm32_register_cclk(NULL, "rtc", rtc_parents, 4,
  			base + STM32F4_RCC_BDCR, 15, 8, 0, &stm32f4_clk_lock);
  
  	if (IS_ERR(clks[CLK_RTC])) {
  		pr_err("Unable to register rtc clock
  ");
  		goto fail;
  	}
daf2d117c   Gabriel Fernandez   clk: stm32f4: Add...
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
  	for (n = 0; n < data->aux_clk_num; n++) {
  		const struct stm32_aux_clk *aux_clk;
  		struct clk_hw *hw;
  
  		aux_clk = &data->aux_clk[n];
  
  		hw = stm32_register_aux_clk(aux_clk->name,
  				aux_clk->parent_names, aux_clk->num_parents,
  				aux_clk->offset_mux, aux_clk->shift,
  				aux_clk->mask, aux_clk->offset_gate,
  				aux_clk->bit_idx, aux_clk->flags,
  				&stm32f4_clk_lock);
  
  		if (IS_ERR(hw)) {
  			pr_warn("Unable to register %s clk
  ", aux_clk->name);
  			continue;
  		}
  
  		if (aux_clk->idx != NO_IDX)
  			clks[aux_clk->idx] = hw;
  	}
936289f04   Gabriel Fernandez   clk: stm32: Intro...
1831
  	if (of_device_is_compatible(np, "st,stm32f746-rcc")) {
88c9b70bb   Gabriel Fernandez   clk: stm32f7: Int...
1832
1833
1834
  
  		clk_hw_register_fixed_factor(NULL, "hsi_div488", "hsi", 0,
  				1, 488);
936289f04   Gabriel Fernandez   clk: stm32: Intro...
1835
1836
  		clks[CLK_PLL_SRC] = pll_src_hw;
  	}
4e950d1ef   Stephen Boyd   clk: stm32f4: Mig...
1837
  	of_clk_add_hw_provider(np, stm32f4_rcc_lookup_clk, NULL);
936289f04   Gabriel Fernandez   clk: stm32: Intro...
1838

358bdf892   Daniel Thompson   clk: stm32: Add c...
1839
1840
  	return;
  fail:
a064a07f7   Gabriel Fernandez   clk: stm32f469: A...
1841
  	kfree(clks);
358bdf892   Daniel Thompson   clk: stm32: Add c...
1842
1843
  	iounmap(base);
  }
3868f132c   Gabriel Fernandez   clk: stm32f4: Use...
1844
1845
  CLK_OF_DECLARE_DRIVER(stm32f42xx_rcc, "st,stm32f42xx-rcc", stm32f4_rcc_init);
  CLK_OF_DECLARE_DRIVER(stm32f46xx_rcc, "st,stm32f469-rcc", stm32f4_rcc_init);
88c9b70bb   Gabriel Fernandez   clk: stm32f7: Int...
1846
  CLK_OF_DECLARE_DRIVER(stm32f746_rcc, "st,stm32f746-rcc", stm32f4_rcc_init);
936289f04   Gabriel Fernandez   clk: stm32: Intro...
1847
  CLK_OF_DECLARE_DRIVER(stm32f769_rcc, "st,stm32f769-rcc", stm32f4_rcc_init);