blob: d1ec3c82b5473e6e5d9104ac196fac3171d605cd [file] [log] [blame]
Claudiu Beznea57db4c42020-09-07 17:46:52 +03001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * SAMA7G5 PMC clock support.
4 *
5 * Copyright (C) 2020 Microchip Technology Inc. and its subsidiaries
6 *
7 * Author: Claudiu Beznea <claudiu.beznea@microchip.com>
8 *
9 * Based on drivers/clk/at91/sama7g5.c from Linux.
10 */
11
12#include <common.h>
13#include <clk-uclass.h>
14#include <dm.h>
15#include <dt-bindings/clk/at91.h>
16#include <linux/clk-provider.h>
17
18#include "pmc.h"
19
20/**
21 * Clock identifiers to be used in conjunction with macros like
22 * AT91_TO_CLK_ID()
23 *
24 * @ID_MD_SLCK: TD slow clock identifier
25 * @ID_TD_SLCK: MD slow clock identifier
26 * @ID_MAIN_XTAL: Main Xtal clock identifier
27 * @ID_MAIN_RC: Main RC clock identifier
28 * @ID_MAIN_RC_OSC: Main RC Oscillator clock identifier
29 * @ID_MAIN_OSC: Main Oscillator clock identifier
30 * @ID_MAINCK: MAINCK clock identifier
31 * @ID_PLL_CPU_FRAC: CPU PLL fractional clock identifier
32 * @ID_PLL_CPU_DIV: CPU PLL divider clock identifier
33 * @ID_PLL_SYS_FRAC: SYS PLL fractional clock identifier
34 * @ID_PLL_SYS_DIV: SYS PLL divider clock identifier
35 * @ID_PLL_DDR_FRAC: DDR PLL fractional clock identifier
36 * @ID_PLL_DDR_DIV: DDR PLL divider clock identifier
37 * @ID_PLL_IMG_FRAC: IMC PLL fractional clock identifier
38 * @ID_PLL_IMG_DIV: IMG PLL divider clock identifier
39 * @ID_PLL_BAUD_FRAC: Baud PLL fractional clock identifier
40 * @ID_PLL_BAUD_DIV: Baud PLL divider clock identifier
41 * @ID_PLL_AUDIO_FRAC: Audio PLL fractional clock identifier
42 * @ID_PLL_AUDIO_DIVPMC: Audio PLL PMC divider clock identifier
43 * @ID_PLL_AUDIO_DIVIO: Audio PLL IO divider clock identifier
44 * @ID_PLL_ETH_FRAC: Ethernet PLL fractional clock identifier
45 * @ID_PLL_ETH_DIV: Ethernet PLL divider clock identifier
46
Claudiu Beznea8b5bbad2021-07-16 08:43:48 +030047 * @ID_MCK0_PRES: MCK0 PRES clock identifier
48 * @ID_MCK0_DIV: MCK0 DIV clock identifier
Claudiu Beznea57db4c42020-09-07 17:46:52 +030049 * @ID_MCK1: MCK1 clock identifier
50 * @ID_MCK2: MCK2 clock identifier
51 * @ID_MCK3: MCK3 clock identifier
52 * @ID_MCK4: MCK4 clock identifier
53
54 * @ID_UTMI: UTMI clock identifier
55
56 * @ID_PROG0: Programmable 0 clock identifier
57 * @ID_PROG1: Programmable 1 clock identifier
58 * @ID_PROG2: Programmable 2 clock identifier
59 * @ID_PROG3: Programmable 3 clock identifier
60 * @ID_PROG4: Programmable 4 clock identifier
61 * @ID_PROG5: Programmable 5 clock identifier
62 * @ID_PROG6: Programmable 6 clock identifier
63 * @ID_PROG7: Programmable 7 clock identifier
64
65 * @ID_PCK0: System clock 0 clock identifier
66 * @ID_PCK1: System clock 1 clock identifier
67 * @ID_PCK2: System clock 2 clock identifier
68 * @ID_PCK3: System clock 3 clock identifier
69 * @ID_PCK4: System clock 4 clock identifier
70 * @ID_PCK5: System clock 5 clock identifier
71 * @ID_PCK6: System clock 6 clock identifier
72 * @ID_PCK7: System clock 7 clock identifier
73 */
74enum pmc_clk_ids {
75 ID_MD_SLCK = 0,
76 ID_TD_SLCK = 1,
77 ID_MAIN_XTAL = 2,
78 ID_MAIN_RC = 3,
79 ID_MAIN_RC_OSC = 4,
80 ID_MAIN_OSC = 5,
81 ID_MAINCK = 6,
82
83 ID_PLL_CPU_FRAC = 7,
84 ID_PLL_CPU_DIV = 8,
85 ID_PLL_SYS_FRAC = 9,
86 ID_PLL_SYS_DIV = 10,
87 ID_PLL_DDR_FRAC = 11,
88 ID_PLL_DDR_DIV = 12,
89 ID_PLL_IMG_FRAC = 13,
90 ID_PLL_IMG_DIV = 14,
91 ID_PLL_BAUD_FRAC = 15,
92 ID_PLL_BAUD_DIV = 16,
93 ID_PLL_AUDIO_FRAC = 17,
94 ID_PLL_AUDIO_DIVPMC = 18,
95 ID_PLL_AUDIO_DIVIO = 19,
96 ID_PLL_ETH_FRAC = 20,
97 ID_PLL_ETH_DIV = 21,
98
Claudiu Beznea8b5bbad2021-07-16 08:43:48 +030099 ID_MCK0_DIV = 22,
Claudiu Beznea57db4c42020-09-07 17:46:52 +0300100 ID_MCK1 = 23,
101 ID_MCK2 = 24,
102 ID_MCK3 = 25,
103 ID_MCK4 = 26,
104
105 ID_UTMI = 27,
106
107 ID_PROG0 = 28,
108 ID_PROG1 = 29,
109 ID_PROG2 = 30,
110 ID_PROG3 = 31,
111 ID_PROG4 = 32,
112 ID_PROG5 = 33,
113 ID_PROG6 = 34,
114 ID_PROG7 = 35,
115
116 ID_PCK0 = 36,
117 ID_PCK1 = 37,
118 ID_PCK2 = 38,
119 ID_PCK3 = 39,
120 ID_PCK4 = 40,
121 ID_PCK5 = 41,
122 ID_PCK6 = 42,
123 ID_PCK7 = 43,
124
Claudiu Beznea8b5bbad2021-07-16 08:43:48 +0300125 ID_MCK0_PRES = 44,
126
Claudiu Beznea57db4c42020-09-07 17:46:52 +0300127 ID_MAX,
128};
129
130/**
131 * PLL type identifiers
132 * @PLL_TYPE_FRAC: fractional PLL identifier
133 * @PLL_TYPE_DIV: divider PLL identifier
134 */
135enum pll_type {
136 PLL_TYPE_FRAC,
137 PLL_TYPE_DIV,
138};
139
140/* Clock names used as parents for multiple clocks. */
141static const char *clk_names[] = {
142 [ID_MAIN_RC_OSC] = "main_rc_osc",
143 [ID_MAIN_OSC] = "main_osc",
144 [ID_MAINCK] = "mainck",
145 [ID_PLL_CPU_DIV] = "cpupll_divpmcck",
146 [ID_PLL_SYS_DIV] = "syspll_divpmcck",
147 [ID_PLL_DDR_DIV] = "ddrpll_divpmcck",
148 [ID_PLL_IMG_DIV] = "imgpll_divpmcck",
149 [ID_PLL_BAUD_DIV] = "baudpll_divpmcck",
150 [ID_PLL_AUDIO_DIVPMC] = "audiopll_divpmcck",
151 [ID_PLL_AUDIO_DIVIO] = "audiopll_diviock",
152 [ID_PLL_ETH_DIV] = "ethpll_divpmcck",
Claudiu Beznea8b5bbad2021-07-16 08:43:48 +0300153 [ID_MCK0_DIV] = "mck0_div",
154 [ID_MCK0_PRES] = "mck0_pres",
Claudiu Beznea57db4c42020-09-07 17:46:52 +0300155};
156
157/* Fractional PLL output range. */
158static const struct clk_range pll_outputs[] = {
159 { .min = 2343750, .max = 1200000000 },
160};
161
162/* PLL characteristics. */
163static const struct clk_pll_characteristics pll_characteristics = {
164 .input = { .min = 12000000, .max = 50000000 },
165 .num_output = ARRAY_SIZE(pll_outputs),
166 .output = pll_outputs,
167};
168
169/* Layout for fractional PLLs. */
170static const struct clk_pll_layout pll_layout_frac = {
171 .mul_mask = GENMASK(31, 24),
172 .frac_mask = GENMASK(21, 0),
173 .mul_shift = 24,
174 .frac_shift = 0,
175};
176
177/* Layout for DIVPMC dividers. */
178static const struct clk_pll_layout pll_layout_divpmc = {
179 .div_mask = GENMASK(7, 0),
180 .endiv_mask = BIT(29),
181 .div_shift = 0,
182 .endiv_shift = 29,
183};
184
185/* Layout for DIVIO dividers. */
186static const struct clk_pll_layout pll_layout_divio = {
187 .div_mask = GENMASK(19, 12),
188 .endiv_mask = BIT(30),
189 .div_shift = 12,
190 .endiv_shift = 30,
191};
192
193/* MCK0 characteristics. */
194static const struct clk_master_characteristics mck0_characteristics = {
195 .output = { .min = 140000000, .max = 200000000 },
Eugen Hristev4eea0882020-07-01 10:44:21 +0300196 .divisors = { 1, 2, 4, 3, 5 },
Claudiu Beznea57db4c42020-09-07 17:46:52 +0300197 .have_div3_pres = 1,
198};
199
200/* MCK0 layout. */
201static const struct clk_master_layout mck0_layout = {
Eugen Hristev4eea0882020-07-01 10:44:21 +0300202 .mask = 0x773,
Claudiu Beznea57db4c42020-09-07 17:46:52 +0300203 .pres_shift = 4,
204 .offset = 0x28,
205};
206
207/* Programmable clock layout. */
208static const struct clk_programmable_layout programmable_layout = {
209 .pres_mask = 0xff,
210 .pres_shift = 8,
211 .css_mask = 0x1f,
212 .have_slck_mck = 0,
213 .is_pres_direct = 1,
214};
215
216/* Peripheral clock layout. */
217static const struct clk_pcr_layout sama7g5_pcr_layout = {
218 .offset = 0x88,
219 .cmd = BIT(31),
220 .gckcss_mask = GENMASK(12, 8),
221 .pid_mask = GENMASK(6, 0),
222 .div_mask = GENMASK(15, 14),
223};
224
225/**
226 * PLL clocks description
227 * @n: clock name
228 * @p: clock parent
229 * @l: clock layout
230 * @t: clock type
231 * @c: true if clock is critical and cannot be disabled
232 * @id: clock id corresponding to PLL driver
233 * @cid: clock id corresponding to clock subsystem
234 */
235static const struct {
236 const char *n;
237 const char *p;
238 const struct clk_pll_layout *l;
239 u8 t;
240 u8 c;
241 u8 id;
242 u8 cid;
243} sama7g5_plls[] = {
244 {
245 .n = "cpupll_fracck",
246 .p = "mainck",
247 .l = &pll_layout_frac,
248 .t = PLL_TYPE_FRAC,
249 .c = 1,
250 .id = 0,
251 .cid = ID_PLL_CPU_FRAC,
252 },
253
254 {
255 .n = "cpupll_divpmcck",
256 .p = "cpupll_fracck",
257 .l = &pll_layout_divpmc,
258 .t = PLL_TYPE_DIV,
259 .c = 1,
260 .id = 0,
261 .cid = ID_PLL_CPU_DIV,
262 },
263
264 {
265 .n = "syspll_fracck",
266 .p = "mainck",
267 .l = &pll_layout_frac,
268 .t = PLL_TYPE_FRAC,
269 .c = 1,
270 .id = 1,
271 .cid = ID_PLL_SYS_FRAC,
272 },
273
274 {
275 .n = "syspll_divpmcck",
276 .p = "syspll_fracck",
277 .l = &pll_layout_divpmc,
278 .t = PLL_TYPE_DIV,
279 .c = 1,
280 .id = 1,
281 .cid = ID_PLL_SYS_DIV,
282 },
283
284 {
285 .n = "ddrpll_fracck",
286 .p = "mainck",
287 .l = &pll_layout_frac,
288 .t = PLL_TYPE_FRAC,
289 .c = 1,
290 .id = 2,
291 .cid = ID_PLL_DDR_FRAC,
292 },
293
294 {
295 .n = "ddrpll_divpmcck",
296 .p = "ddrpll_fracck",
297 .l = &pll_layout_divpmc,
298 .t = PLL_TYPE_DIV,
299 .c = 1,
300 .id = 2,
301 .cid = ID_PLL_DDR_DIV,
302 },
303
304 {
305 .n = "imgpll_fracck",
306 .p = "mainck",
307 .l = &pll_layout_frac,
308 .t = PLL_TYPE_FRAC,
309 .id = 3,
310 .cid = ID_PLL_IMG_FRAC,
311 },
312
313 {
314 .n = "imgpll_divpmcck",
315 .p = "imgpll_fracck",
316 .l = &pll_layout_divpmc,
317 .t = PLL_TYPE_DIV,
318 .id = 3,
319 .cid = ID_PLL_IMG_DIV
320 },
321
322 {
323 .n = "baudpll_fracck",
324 .p = "mainck",
325 .l = &pll_layout_frac,
326 .t = PLL_TYPE_FRAC,
327 .id = 4,
328 .cid = ID_PLL_BAUD_FRAC,
329 },
330
331 {
332 .n = "baudpll_divpmcck",
333 .p = "baudpll_fracck",
334 .l = &pll_layout_divpmc,
335 .t = PLL_TYPE_DIV,
336 .id = 4,
337 .cid = ID_PLL_BAUD_DIV,
338 },
339
340 {
341 .n = "audiopll_fracck",
342 .p = "main_osc",
343 .l = &pll_layout_frac,
344 .t = PLL_TYPE_FRAC,
345 .id = 5,
346 .cid = ID_PLL_AUDIO_FRAC,
347 },
348
349 {
350 .n = "audiopll_divpmcck",
351 .p = "audiopll_fracck",
352 .l = &pll_layout_divpmc,
353 .t = PLL_TYPE_DIV,
354 .id = 5,
355 .cid = ID_PLL_AUDIO_DIVPMC,
356 },
357
358 {
359 .n = "audiopll_diviock",
360 .p = "audiopll_fracck",
361 .l = &pll_layout_divio,
362 .t = PLL_TYPE_DIV,
363 .id = 5,
364 .cid = ID_PLL_AUDIO_DIVIO,
365 },
366
367 {
368 .n = "ethpll_fracck",
369 .p = "main_osc",
370 .l = &pll_layout_frac,
371 .t = PLL_TYPE_FRAC,
372 .id = 6,
373 .cid = ID_PLL_ETH_FRAC,
374 },
375
376 {
377 .n = "ethpll_divpmcck",
378 .p = "ethpll_fracck",
379 .l = &pll_layout_divpmc,
380 .t = PLL_TYPE_DIV,
381 .id = 6,
382 .cid = ID_PLL_ETH_DIV,
383 },
384};
385
386/**
387 * Master clock (MCK[1..4]) description
388 * @n: clock name
389 * @ep: extra parents names array
390 * @ep_chg_chg_id: index in parents array that specifies the changeable
391 * parent
392 * @ep_count: extra parents count
393 * @ep_mux_table: mux table for extra parents
394 * @ep_clk_mux_table: mux table to deal with subsystem clock ids
395 * @id: clock id corresponding to MCK driver
396 * @cid: clock id corresponding to clock subsystem
397 * @c: true if clock is critical and cannot be disabled
398 */
399static const struct {
400 const char *n;
401 const char *ep[4];
402 u8 ep_count;
403 u8 ep_mux_table[4];
404 u8 ep_clk_mux_table[4];
405 u8 id;
406 u8 cid;
407 u8 c;
408} sama7g5_mckx[] = {
409 {
410 .n = "mck1",
411 .id = 1,
412 .cid = ID_MCK1,
413 .ep = { "syspll_divpmcck", },
414 .ep_mux_table = { 5, },
415 .ep_clk_mux_table = { ID_PLL_SYS_DIV, },
416 .ep_count = 1,
417 .c = 1,
418 },
419
420 {
421 .n = "mck2",
422 .id = 2,
423 .cid = ID_MCK2,
424 .ep = { "ddrpll_divpmcck", },
425 .ep_mux_table = { 6, },
426 .ep_clk_mux_table = { ID_PLL_DDR_DIV, },
427 .ep_count = 1,
428 .c = 1,
429 },
430
431 {
432 .n = "mck3",
433 .id = 3,
434 .cid = ID_MCK3,
435 .ep = { "syspll_divpmcck", "ddrpll_divpmcck", "imgpll_divpmcck", },
436 .ep_mux_table = { 5, 6, 7, },
437 .ep_clk_mux_table = { ID_PLL_SYS_DIV, ID_PLL_DDR_DIV, ID_PLL_IMG_DIV, },
438 .ep_count = 3,
439 },
440
441 {
442 .n = "mck4",
443 .id = 4,
444 .cid = ID_MCK4,
445 .ep = { "syspll_divpmcck", },
446 .ep_mux_table = { 5, },
447 .ep_clk_mux_table = { ID_PLL_SYS_DIV, },
448 .ep_count = 1,
449 .c = 1,
450 },
451};
452
453/**
454 * Programmable clock description
455 * @n: clock name
456 * @cid: clock id corresponding to clock subsystem
457 */
458static const struct {
459 const char *n;
460 u8 cid;
461} sama7g5_prog[] = {
462 { .n = "prog0", .cid = ID_PROG0, },
463 { .n = "prog1", .cid = ID_PROG1, },
464 { .n = "prog2", .cid = ID_PROG2, },
465 { .n = "prog3", .cid = ID_PROG3, },
466 { .n = "prog4", .cid = ID_PROG4, },
467 { .n = "prog5", .cid = ID_PROG5, },
468 { .n = "prog6", .cid = ID_PROG6, },
469 { .n = "prog7", .cid = ID_PROG7, },
470};
471
472/* Mux table for programmable clocks. */
473static u32 sama7g5_prog_mux_table[] = { 0, 1, 2, 3, 5, 6, 7, 8, 9, 10, };
474
475/**
476 * System clock description
477 * @n: clock name
478 * @p: parent clock name
479 * @id: clock id corresponding to system clock driver
480 * @cid: clock id corresponding to clock subsystem
481 */
482static const struct {
483 const char *n;
484 const char *p;
485 u8 id;
486 u8 cid;
487} sama7g5_systemck[] = {
488 { .n = "pck0", .p = "prog0", .id = 8, .cid = ID_PCK0, },
489 { .n = "pck1", .p = "prog1", .id = 9, .cid = ID_PCK1, },
490 { .n = "pck2", .p = "prog2", .id = 10, .cid = ID_PCK2, },
491 { .n = "pck3", .p = "prog3", .id = 11, .cid = ID_PCK3, },
492 { .n = "pck4", .p = "prog4", .id = 12, .cid = ID_PCK4, },
493 { .n = "pck5", .p = "prog5", .id = 13, .cid = ID_PCK5, },
494 { .n = "pck6", .p = "prog6", .id = 14, .cid = ID_PCK6, },
495 { .n = "pck7", .p = "prog7", .id = 15, .cid = ID_PCK7, },
496};
497
498/**
499 * Peripheral clock description
500 * @n: clock name
501 * @p: clock parent name
502 * @r: clock range values
503 * @id: clock id
504 */
505static const struct {
506 const char *n;
507 const char *p;
508 struct clk_range r;
509 u8 id;
510} sama7g5_periphck[] = {
Claudiu Beznea8b5bbad2021-07-16 08:43:48 +0300511 { .n = "pioA_clk", .p = "mck0_div", .id = 11, },
Claudiu Beznea57db4c42020-09-07 17:46:52 +0300512 { .n = "sfr_clk", .p = "mck1", .id = 19, },
513 { .n = "hsmc_clk", .p = "mck1", .id = 21, },
514 { .n = "xdmac0_clk", .p = "mck1", .id = 22, },
515 { .n = "xdmac1_clk", .p = "mck1", .id = 23, },
516 { .n = "xdmac2_clk", .p = "mck1", .id = 24, },
517 { .n = "acc_clk", .p = "mck1", .id = 25, },
518 { .n = "aes_clk", .p = "mck1", .id = 27, },
519 { .n = "tzaesbasc_clk", .p = "mck1", .id = 28, },
520 { .n = "asrc_clk", .p = "mck1", .id = 30, .r = { .max = 200000000, }, },
Claudiu Beznea8b5bbad2021-07-16 08:43:48 +0300521 { .n = "cpkcc_clk", .p = "mck0_div", .id = 32, },
Claudiu Beznea57db4c42020-09-07 17:46:52 +0300522 { .n = "csi_clk", .p = "mck3", .id = 33, .r = { .max = 266000000, }, },
523 { .n = "csi2dc_clk", .p = "mck3", .id = 34, .r = { .max = 266000000, }, },
524 { .n = "eic_clk", .p = "mck1", .id = 37, },
525 { .n = "flex0_clk", .p = "mck1", .id = 38, },
526 { .n = "flex1_clk", .p = "mck1", .id = 39, },
527 { .n = "flex2_clk", .p = "mck1", .id = 40, },
528 { .n = "flex3_clk", .p = "mck1", .id = 41, },
529 { .n = "flex4_clk", .p = "mck1", .id = 42, },
530 { .n = "flex5_clk", .p = "mck1", .id = 43, },
531 { .n = "flex6_clk", .p = "mck1", .id = 44, },
532 { .n = "flex7_clk", .p = "mck1", .id = 45, },
533 { .n = "flex8_clk", .p = "mck1", .id = 46, },
534 { .n = "flex9_clk", .p = "mck1", .id = 47, },
535 { .n = "flex10_clk", .p = "mck1", .id = 48, },
536 { .n = "flex11_clk", .p = "mck1", .id = 49, },
537 { .n = "gmac0_clk", .p = "mck1", .id = 51, },
538 { .n = "gmac1_clk", .p = "mck1", .id = 52, },
539 { .n = "gmac0_tsu_clk", .p = "mck1", .id = 53, },
540 { .n = "gmac1_tsu_clk", .p = "mck1", .id = 54, },
541 { .n = "icm_clk", .p = "mck1", .id = 55, },
542 { .n = "isc_clk", .p = "mck3", .id = 56, .r = { .max = 266000000, }, },
543 { .n = "i2smcc0_clk", .p = "mck1", .id = 57, .r = { .max = 200000000, }, },
544 { .n = "i2smcc1_clk", .p = "mck1", .id = 58, .r = { .max = 200000000, }, },
545 { .n = "matrix_clk", .p = "mck1", .id = 60, },
546 { .n = "mcan0_clk", .p = "mck1", .id = 61, .r = { .max = 200000000, }, },
547 { .n = "mcan1_clk", .p = "mck1", .id = 62, .r = { .max = 200000000, }, },
548 { .n = "mcan2_clk", .p = "mck1", .id = 63, .r = { .max = 200000000, }, },
549 { .n = "mcan3_clk", .p = "mck1", .id = 64, .r = { .max = 200000000, }, },
550 { .n = "mcan4_clk", .p = "mck1", .id = 65, .r = { .max = 200000000, }, },
551 { .n = "mcan5_clk", .p = "mck1", .id = 66, .r = { .max = 200000000, }, },
552 { .n = "pdmc0_clk", .p = "mck1", .id = 68, .r = { .max = 200000000, }, },
553 { .n = "pdmc1_clk", .p = "mck1", .id = 69, .r = { .max = 200000000, }, },
554 { .n = "pit64b0_clk", .p = "mck1", .id = 70, },
555 { .n = "pit64b1_clk", .p = "mck1", .id = 71, },
556 { .n = "pit64b2_clk", .p = "mck1", .id = 72, },
557 { .n = "pit64b3_clk", .p = "mck1", .id = 73, },
558 { .n = "pit64b4_clk", .p = "mck1", .id = 74, },
559 { .n = "pit64b5_clk", .p = "mck1", .id = 75, },
560 { .n = "pwm_clk", .p = "mck1", .id = 77, },
561 { .n = "qspi0_clk", .p = "mck1", .id = 78, },
562 { .n = "qspi1_clk", .p = "mck1", .id = 79, },
563 { .n = "sdmmc0_clk", .p = "mck1", .id = 80, },
564 { .n = "sdmmc1_clk", .p = "mck1", .id = 81, },
565 { .n = "sdmmc2_clk", .p = "mck1", .id = 82, },
566 { .n = "sha_clk", .p = "mck1", .id = 83, },
567 { .n = "spdifrx_clk", .p = "mck1", .id = 84, .r = { .max = 200000000, }, },
568 { .n = "spdiftx_clk", .p = "mck1", .id = 85, .r = { .max = 200000000, }, },
569 { .n = "ssc0_clk", .p = "mck1", .id = 86, .r = { .max = 200000000, }, },
570 { .n = "ssc1_clk", .p = "mck1", .id = 87, .r = { .max = 200000000, }, },
571 { .n = "tcb0_ch0_clk", .p = "mck1", .id = 88, .r = { .max = 200000000, }, },
572 { .n = "tcb0_ch1_clk", .p = "mck1", .id = 89, .r = { .max = 200000000, }, },
573 { .n = "tcb0_ch2_clk", .p = "mck1", .id = 90, .r = { .max = 200000000, }, },
574 { .n = "tcb1_ch0_clk", .p = "mck1", .id = 91, .r = { .max = 200000000, }, },
575 { .n = "tcb1_ch1_clk", .p = "mck1", .id = 92, .r = { .max = 200000000, }, },
576 { .n = "tcb1_ch2_clk", .p = "mck1", .id = 93, .r = { .max = 200000000, }, },
577 { .n = "tcpca_clk", .p = "mck1", .id = 94, },
578 { .n = "tcpcb_clk", .p = "mck1", .id = 95, },
579 { .n = "tdes_clk", .p = "mck1", .id = 96, },
580 { .n = "trng_clk", .p = "mck1", .id = 97, },
581 { .n = "udphsa_clk", .p = "mck1", .id = 104, },
582 { .n = "udphsb_clk", .p = "mck1", .id = 105, },
583 { .n = "uhphs_clk", .p = "mck1", .id = 106, },
584};
585
586/**
587 * Generic clock description
588 * @n: clock name
589 * @ep: extra parents names
590 * @ep_mux_table: extra parents mux table
591 * @ep_clk_mux_table: extra parents clock mux table (for CCF)
592 * @r: clock output range
593 * @ep_count: extra parents count
594 * @id: clock id
595 */
596static const struct {
597 const char *n;
598 const char *ep[8];
599 const char ep_mux_table[8];
600 const char ep_clk_mux_table[8];
601 struct clk_range r;
602 u8 ep_count;
603 u8 id;
604} sama7g5_gck[] = {
605 {
606 .n = "adc_gclk",
607 .id = 26,
608 .r = { .max = 100000000, },
609 .ep = { "syspll_divpmcck", "imgpll_divpmcck", "audiopll_divpmcck", },
610 .ep_mux_table = { 5, 7, 9, },
611 .ep_clk_mux_table = { ID_PLL_SYS_DIV, ID_PLL_IMG_DIV,
612 ID_PLL_AUDIO_DIVPMC, },
613 .ep_count = 3,
614 },
615
616 {
617 .n = "asrc_gclk",
618 .id = 30,
619 .r = { .max = 200000000 },
620 .ep = { "audiopll_divpmcck", },
621 .ep_mux_table = { 9, },
622 .ep_clk_mux_table = { ID_PLL_AUDIO_DIVPMC, },
623 .ep_count = 1,
624 },
625
626 {
627 .n = "csi_gclk",
628 .id = 33,
629 .r = { .max = 27000000 },
630 .ep = { "ddrpll_divpmcck", "imgpll_divpmcck", },
631 .ep_clk_mux_table = { ID_PLL_DDR_DIV, ID_PLL_IMG_DIV, },
632 .ep_mux_table = { 6, 7, },
633 .ep_count = 2,
634 },
635
636 {
637 .n = "flex0_gclk",
638 .id = 38,
639 .r = { .max = 200000000 },
640 .ep = { "syspll_divpmcck", "baudpll_divpmcck", },
641 .ep_mux_table = { 5, 8, },
642 .ep_clk_mux_table = { ID_PLL_SYS_DIV, ID_PLL_BAUD_DIV, },
643 .ep_count = 2,
644 },
645
646 {
647 .n = "flex1_gclk",
648 .id = 39,
649 .r = { .max = 200000000 },
650 .ep = { "syspll_divpmcck", "baudpll_divpmcck", },
651 .ep_mux_table = { 5, 8, },
652 .ep_clk_mux_table = { ID_PLL_SYS_DIV, ID_PLL_BAUD_DIV, },
653 .ep_count = 2,
654 },
655
656 {
657 .n = "flex2_gclk",
658 .id = 40,
659 .r = { .max = 200000000 },
660 .ep = { "syspll_divpmcck", "baudpll_divpmcck", },
661 .ep_mux_table = { 5, 8, },
662 .ep_clk_mux_table = { ID_PLL_SYS_DIV, ID_PLL_BAUD_DIV, },
663 .ep_count = 2,
664 },
665
666 {
667 .n = "flex3_gclk",
668 .id = 41,
669 .r = { .max = 200000000 },
670 .ep = { "syspll_divpmcck", "baudpll_divpmcck", },
671 .ep_mux_table = { 5, 8, },
672 .ep_clk_mux_table = { ID_PLL_SYS_DIV, ID_PLL_BAUD_DIV, },
673 .ep_count = 2,
674 },
675
676 {
677 .n = "flex4_gclk",
678 .id = 42,
679 .r = { .max = 200000000 },
680 .ep = { "syspll_divpmcck", "baudpll_divpmcck", },
681 .ep_mux_table = { 5, 8, },
682 .ep_clk_mux_table = { ID_PLL_SYS_DIV, ID_PLL_BAUD_DIV, },
683 .ep_count = 2,
684 },
685
686 {
687 .n = "flex5_gclk",
688 .id = 43,
689 .r = { .max = 200000000 },
690 .ep = { "syspll_divpmcck", "baudpll_divpmcck", },
691 .ep_mux_table = { 5, 8, },
692 .ep_clk_mux_table = { ID_PLL_SYS_DIV, ID_PLL_BAUD_DIV, },
693 .ep_count = 2,
694 },
695
696 {
697 .n = "flex6_gclk",
698 .id = 44,
699 .r = { .max = 200000000 },
700 .ep = { "syspll_divpmcck", "baudpll_divpmcck", },
701 .ep_mux_table = { 5, 8, },
702 .ep_clk_mux_table = { ID_PLL_SYS_DIV, ID_PLL_BAUD_DIV, },
703 .ep_count = 2,
704 },
705
706 {
707 .n = "flex7_gclk",
708 .id = 45,
709 .r = { .max = 200000000 },
710 .ep = { "syspll_divpmcck", "baudpll_divpmcck", },
711 .ep_mux_table = { 5, 8, },
712 .ep_clk_mux_table = { ID_PLL_SYS_DIV, ID_PLL_BAUD_DIV, },
713 .ep_count = 2,
714 },
715
716 {
717 .n = "flex8_gclk",
718 .id = 46,
719 .r = { .max = 200000000 },
720 .ep = { "syspll_divpmcck", "baudpll_divpmcck", },
721 .ep_mux_table = { 5, 8, },
722 .ep_clk_mux_table = { ID_PLL_SYS_DIV, ID_PLL_BAUD_DIV, },
723 .ep_count = 2,
724 },
725
726 {
727 .n = "flex9_gclk",
728 .id = 47,
729 .r = { .max = 200000000 },
730 .ep = { "syspll_divpmcck", "baudpll_divpmcck", },
731 .ep_mux_table = { 5, 8, },
732 .ep_clk_mux_table = { ID_PLL_SYS_DIV, ID_PLL_BAUD_DIV, },
733 .ep_count = 2,
734 },
735
736 {
737 .n = "flex10_gclk",
738 .id = 48,
739 .r = { .max = 200000000 },
740 .ep = { "syspll_divpmcck", "baudpll_divpmcck", },
741 .ep_mux_table = { 5, 8, },
742 .ep_clk_mux_table = { ID_PLL_SYS_DIV, ID_PLL_BAUD_DIV, },
743 .ep_count = 2,
744 },
745
746 {
747 .n = "flex11_gclk",
748 .id = 49,
749 .r = { .max = 200000000 },
750 .ep = { "syspll_divpmcck", "baudpll_divpmcck", },
751 .ep_mux_table = { 5, 8, },
752 .ep_clk_mux_table = { ID_PLL_SYS_DIV, ID_PLL_BAUD_DIV, },
753 .ep_count = 2,
754 },
755
756 {
757 .n = "gmac0_gclk",
758 .id = 51,
759 .r = { .max = 125000000 },
760 .ep = { "ethpll_divpmcck", },
761 .ep_clk_mux_table = { ID_PLL_ETH_DIV, },
762 .ep_mux_table = { 10, },
763 .ep_count = 1,
764 },
765
766 {
767 .n = "gmac1_gclk",
768 .id = 52,
769 .r = { .max = 50000000 },
770 .ep = { "ethpll_divpmcck", },
771 .ep_mux_table = { 10, },
772 .ep_clk_mux_table = { ID_PLL_ETH_DIV, },
773 .ep_count = 1,
774 },
775
776 {
777 .n = "gmac0_tsu_gclk",
778 .id = 53,
779 .r = { .max = 300000000 },
780 .ep = { "audiopll_divpmcck", "ethpll_divpmcck", },
781 .ep_mux_table = { 9, 10, },
782 .ep_clk_mux_table = { ID_PLL_AUDIO_DIVPMC, ID_PLL_ETH_DIV, },
783 .ep_count = 2,
784 },
785
786 {
787 .n = "gmac1_tsu_gclk",
788 .id = 54,
789 .r = { .max = 300000000 },
790 .ep = { "audiopll_divpmcck", "ethpll_divpmcck", },
791 .ep_mux_table = { 9, 10, },
792 .ep_clk_mux_table = { ID_PLL_AUDIO_DIVPMC, ID_PLL_ETH_DIV, },
793 .ep_count = 2,
794 },
795
796 {
797 .n = "i2smcc0_gclk",
798 .id = 57,
799 .r = { .max = 100000000 },
800 .ep = { "syspll_divpmcck", "audiopll_divpmcck", },
801 .ep_mux_table = { 5, 9, },
802 .ep_clk_mux_table = { ID_PLL_SYS_DIV, ID_PLL_AUDIO_DIVPMC, },
803 .ep_count = 2,
804 },
805
806 {
807 .n = "i2smcc1_gclk",
808 .id = 58,
809 .r = { .max = 100000000 },
810 .ep = { "syspll_divpmcck", "audiopll_divpmcck", },
811 .ep_mux_table = { 5, 9, },
812 .ep_clk_mux_table = { ID_PLL_SYS_DIV, ID_PLL_AUDIO_DIVPMC, },
813 .ep_count = 2,
814 },
815
816 {
817 .n = "mcan0_gclk",
818 .id = 61,
819 .r = { .max = 200000000 },
820 .ep = { "syspll_divpmcck", "baudpll_divpmcck", },
821 .ep_mux_table = { 5, 8, },
822 .ep_clk_mux_table = { ID_PLL_SYS_DIV, ID_PLL_BAUD_DIV, },
823 .ep_count = 2,
824 },
825
826 {
827 .n = "mcan1_gclk",
828 .id = 62,
829 .r = { .max = 200000000 },
830 .ep = { "syspll_divpmcck", "baudpll_divpmcck", },
831 .ep_mux_table = { 5, 8, },
832 .ep_clk_mux_table = { ID_PLL_SYS_DIV, ID_PLL_BAUD_DIV, },
833 .ep_count = 2,
834 },
835
836 {
837 .n = "mcan2_gclk",
838 .id = 63,
839 .r = { .max = 200000000 },
840 .ep = { "syspll_divpmcck", "baudpll_divpmcck", },
841 .ep_mux_table = { 5, 8, },
842 .ep_clk_mux_table = { ID_PLL_SYS_DIV, ID_PLL_BAUD_DIV, },
843 .ep_count = 2,
844 },
845
846 {
847 .n = "mcan3_gclk",
848 .id = 64,
849 .r = { .max = 200000000 },
850 .ep = { "syspll_divpmcck", "baudpll_divpmcck", },
851 .ep_mux_table = { 5, 8, },
852 .ep_clk_mux_table = { ID_PLL_SYS_DIV, ID_PLL_BAUD_DIV, },
853 .ep_count = 2,
854 },
855
856 {
857 .n = "mcan4_gclk",
858 .id = 65,
859 .r = { .max = 200000000 },
860 .ep = { "syspll_divpmcck", "baudpll_divpmcck", },
861 .ep_mux_table = { 5, 8, },
862 .ep_clk_mux_table = { ID_PLL_SYS_DIV, ID_PLL_BAUD_DIV, },
863 .ep_count = 2,
864 },
865
866 {
867 .n = "mcan5_gclk",
868 .id = 66,
869 .r = { .max = 200000000 },
870 .ep = { "syspll_divpmcck", "baudpll_divpmcck", },
871 .ep_mux_table = { 5, 8, },
872 .ep_clk_mux_table = { ID_PLL_SYS_DIV, ID_PLL_BAUD_DIV, },
873 .ep_count = 2,
874 },
875
876 {
877 .n = "pdmc0_gclk",
878 .id = 68,
879 .r = { .max = 50000000 },
880 .ep = { "syspll_divpmcck", "baudpll_divpmcck", },
881 .ep_mux_table = { 5, 8, },
882 .ep_clk_mux_table = { ID_PLL_SYS_DIV, ID_PLL_BAUD_DIV, },
883 .ep_count = 2,
884 },
885
886 {
887 .n = "pdmc1_gclk",
888 .id = 69,
889 .r = { .max = 50000000, },
890 .ep = { "syspll_divpmcck", "baudpll_divpmcck", },
891 .ep_mux_table = { 5, 8, },
892 .ep_clk_mux_table = { ID_PLL_SYS_DIV, ID_PLL_BAUD_DIV, },
893 .ep_count = 2,
894 },
895
896 {
897 .n = "pit64b0_gclk",
898 .id = 70,
899 .r = { .max = 200000000 },
900 .ep = { "syspll_divpmcck", "imgpll_divpmcck", "baudpll_divpmcck",
901 "audiopll_divpmcck", "ethpll_divpmcck", },
902 .ep_mux_table = { 5, 7, 8, 9, 10, },
903 .ep_clk_mux_table = { ID_PLL_SYS_DIV, ID_PLL_IMG_DIV,
904 ID_PLL_BAUD_DIV, ID_PLL_AUDIO_DIVPMC,
905 ID_PLL_ETH_DIV, },
906 .ep_count = 5,
907 },
908
909 {
910 .n = "pit64b1_gclk",
911 .id = 71,
912 .r = { .max = 200000000 },
913 .ep = { "syspll_divpmcck", "imgpll_divpmcck", "baudpll_divpmcck",
914 "audiopll_divpmcck", "ethpll_divpmcck", },
915 .ep_mux_table = { 5, 7, 8, 9, 10, },
916 .ep_clk_mux_table = { ID_PLL_SYS_DIV, ID_PLL_IMG_DIV,
917 ID_PLL_BAUD_DIV, ID_PLL_AUDIO_DIVPMC,
918 ID_PLL_ETH_DIV, },
919 .ep_count = 5,
920 },
921
922 {
923 .n = "pit64b2_gclk",
924 .id = 72,
925 .r = { .max = 200000000 },
926 .ep = { "syspll_divpmcck", "imgpll_divpmcck", "baudpll_divpmcck",
927 "audiopll_divpmcck", "ethpll_divpmcck", },
928 .ep_mux_table = { 5, 7, 8, 9, 10, },
929 .ep_clk_mux_table = { ID_PLL_SYS_DIV, ID_PLL_IMG_DIV,
930 ID_PLL_BAUD_DIV, ID_PLL_AUDIO_DIVPMC,
931 ID_PLL_ETH_DIV, },
932 .ep_count = 5,
933 },
934
935 {
936 .n = "pit64b3_gclk",
937 .id = 73,
938 .r = { .max = 200000000 },
939 .ep = { "syspll_divpmcck", "imgpll_divpmcck", "baudpll_divpmcck",
940 "audiopll_divpmcck", "ethpll_divpmcck", },
941 .ep_mux_table = { 5, 7, 8, 9, 10, },
942 .ep_clk_mux_table = { ID_PLL_SYS_DIV, ID_PLL_IMG_DIV,
943 ID_PLL_BAUD_DIV, ID_PLL_AUDIO_DIVPMC,
944 ID_PLL_ETH_DIV, },
945 .ep_count = 5,
946 },
947
948 {
949 .n = "pit64b4_gclk",
950 .id = 74,
951 .r = { .max = 200000000 },
952 .ep = { "syspll_divpmcck", "imgpll_divpmcck", "baudpll_divpmcck",
953 "audiopll_divpmcck", "ethpll_divpmcck", },
954 .ep_mux_table = { 5, 7, 8, 9, 10, },
955 .ep_clk_mux_table = { ID_PLL_SYS_DIV, ID_PLL_IMG_DIV,
956 ID_PLL_BAUD_DIV, ID_PLL_AUDIO_DIVPMC,
957 ID_PLL_ETH_DIV, },
958 .ep_count = 5,
959 },
960
961 {
962 .n = "pit64b5_gclk",
963 .id = 75,
964 .r = { .max = 200000000 },
965 .ep = { "syspll_divpmcck", "imgpll_divpmcck", "baudpll_divpmcck",
966 "audiopll_divpmcck", "ethpll_divpmcck", },
967 .ep_mux_table = { 5, 7, 8, 9, 10, },
968 .ep_clk_mux_table = { ID_PLL_SYS_DIV, ID_PLL_IMG_DIV,
969 ID_PLL_BAUD_DIV, ID_PLL_AUDIO_DIVPMC,
970 ID_PLL_ETH_DIV, },
971 .ep_count = 5,
972 },
973
974 {
975 .n = "qspi0_gclk",
976 .id = 78,
977 .r = { .max = 200000000 },
978 .ep = { "syspll_divpmcck", "baudpll_divpmcck", },
979 .ep_mux_table = { 5, 8, },
980 .ep_clk_mux_table = { ID_PLL_SYS_DIV, ID_PLL_IMG_DIV,
981 ID_PLL_BAUD_DIV, ID_PLL_AUDIO_DIVPMC,
982 ID_PLL_ETH_DIV, },
983 .ep_count = 2,
984 },
985
986 {
987 .n = "qspi1_gclk",
988 .id = 79,
989 .r = { .max = 200000000 },
990 .ep = { "syspll_divpmcck", "baudpll_divpmcck", },
991 .ep_mux_table = { 5, 8, },
992 .ep_clk_mux_table = { ID_PLL_SYS_DIV, ID_PLL_BAUD_DIV, },
993 .ep_count = 2,
994 },
995
996 {
997 .n = "sdmmc0_gclk",
998 .id = 80,
999 .r = { .max = 208000000 },
1000 .ep = { "syspll_divpmcck", "baudpll_divpmcck", },
1001 .ep_mux_table = { 5, 8, },
1002 .ep_clk_mux_table = { ID_PLL_SYS_DIV, ID_PLL_BAUD_DIV, },
1003 .ep_count = 2,
1004 },
1005
1006 {
1007 .n = "sdmmc1_gclk",
1008 .id = 81,
1009 .r = { .max = 208000000 },
1010 .ep = { "syspll_divpmcck", "baudpll_divpmcck", },
1011 .ep_mux_table = { 5, 8, },
1012 .ep_clk_mux_table = { ID_PLL_SYS_DIV, ID_PLL_BAUD_DIV, },
1013 .ep_count = 2,
1014 },
1015
1016 {
1017 .n = "sdmmc2_gclk",
1018 .id = 82,
1019 .r = { .max = 208000000 },
1020 .ep = { "syspll_divpmcck", "baudpll_divpmcck", },
1021 .ep_mux_table = { 5, 8, },
1022 .ep_clk_mux_table = { ID_PLL_SYS_DIV, ID_PLL_BAUD_DIV, },
1023 .ep_count = 2,
1024 },
1025
1026 {
1027 .n = "spdifrx_gclk",
1028 .id = 84,
1029 .r = { .max = 150000000 },
1030 .ep = { "syspll_divpmcck", "audiopll_divpmcck", },
1031 .ep_mux_table = { 5, 9, },
1032 .ep_clk_mux_table = { ID_PLL_SYS_DIV, ID_PLL_AUDIO_DIVPMC, },
1033 .ep_count = 2,
1034 },
1035
1036 {
1037 .n = "spdiftx_gclk",
1038 .id = 85,
1039 .r = { .max = 25000000 },
1040 .ep = { "syspll_divpmcck", "audiopll_divpmcck", },
1041 .ep_mux_table = { 5, 9, },
1042 .ep_clk_mux_table = { ID_PLL_SYS_DIV, ID_PLL_AUDIO_DIVPMC, },
1043 .ep_count = 2,
1044 },
1045
1046 {
1047 .n = "tcb0_ch0_gclk",
1048 .id = 88,
1049 .r = { .max = 200000000 },
1050 .ep = { "syspll_divpmcck", "imgpll_divpmcck", "baudpll_divpmcck",
1051 "audiopll_divpmcck", "ethpll_divpmcck", },
1052 .ep_mux_table = { 5, 7, 8, 9, 10, },
1053 .ep_clk_mux_table = { ID_PLL_SYS_DIV, ID_PLL_IMG_DIV,
1054 ID_PLL_BAUD_DIV, ID_PLL_AUDIO_DIVPMC,
1055 ID_PLL_ETH_DIV, },
1056 .ep_count = 5,
1057 },
1058
1059 {
1060 .n = "tcb1_ch0_gclk",
1061 .id = 91,
1062 .r = { .max = 200000000 },
1063 .ep = { "syspll_divpmcck", "imgpll_divpmcck", "baudpll_divpmcck",
1064 "audiopll_divpmcck", "ethpll_divpmcck", },
1065 .ep_mux_table = { 5, 7, 8, 9, 10, },
1066 .ep_clk_mux_table = { ID_PLL_SYS_DIV, ID_PLL_IMG_DIV,
1067 ID_PLL_BAUD_DIV, ID_PLL_AUDIO_DIVPMC,
1068 ID_PLL_ETH_DIV, },
1069 .ep_count = 5,
1070 },
1071};
1072
1073/**
1074 * Clock setup description
1075 * @cid: clock id corresponding to clock subsystem
1076 * @pid: parent clock id corresponding to clock subsystem
1077 * @rate: clock rate
1078 * @prate: parent rate
1079 */
1080static const struct pmc_clk_setup {
1081 unsigned int cid;
1082 unsigned int pid;
1083 unsigned long rate;
1084 unsigned long prate;
1085} sama7g5_clk_setup[] = {
1086 {
1087 .cid = AT91_TO_CLK_ID(PMC_TYPE_CORE, ID_PLL_ETH_FRAC),
1088 .rate = 625000000,
1089 },
1090
1091 {
1092 .cid = AT91_TO_CLK_ID(PMC_TYPE_CORE, ID_PLL_ETH_DIV),
1093 .rate = 625000000,
1094 },
1095};
1096
1097#define SAMA7G5_MAX_MUX_ALLOCS (64)
1098
1099#define prepare_mux_table(_allocs, _index, _dst, _src, _num, _label) \
1100 do { \
1101 int _i; \
1102 if ((_index) >= SAMA7G5_MAX_MUX_ALLOCS) { \
1103 debug("%s(): AT91: MUX: insufficient space\n", \
1104 __func__); \
1105 goto _label; \
1106 } \
1107 (_dst) = kzalloc(sizeof(*(_dst)) * (_num), GFP_KERNEL); \
1108 if (!(_dst)) \
1109 goto _label; \
1110 (_allocs)[(_index)++] = (_dst); \
1111 for (_i = 0; _i < (_num); _i++) \
1112 (_dst)[_i] = (_src)[_i]; \
1113 } while (0)
1114
1115static int sama7g5_clk_probe(struct udevice *dev)
1116{
1117 void __iomem *base = (void *)devfdt_get_addr(dev);
1118 unsigned int *clkmuxallocs[SAMA7G5_MAX_MUX_ALLOCS];
1119 unsigned int *muxallocs[SAMA7G5_MAX_MUX_ALLOCS];
1120 const char *p[10];
1121 unsigned int cm[10], m[10], *tmpclkmux, *tmpmux;
1122 struct clk clk, *c, *parent;
1123 bool main_osc_bypass;
1124 int ret, muxallocindex = 0, clkmuxallocindex = 0, i, j;
1125
1126 if (IS_ERR(base))
1127 return PTR_ERR(base);
1128
1129 memset(muxallocs, 0, ARRAY_SIZE(muxallocs));
1130 memset(clkmuxallocs, 0, ARRAY_SIZE(clkmuxallocs));
1131
1132 ret = clk_get_by_index(dev, 0, &clk);
1133 if (ret)
1134 return ret;
1135 ret = clk_get_by_id(clk.id, &c);
1136 if (ret)
1137 return ret;
1138 clk_names[ID_TD_SLCK] = kmemdup(clk_hw_get_name(c),
1139 strlen(clk_hw_get_name(c)) + 1, GFP_KERNEL);
1140 if (!clk_names[ID_TD_SLCK])
1141 return -ENOMEM;
1142
1143 ret = clk_get_by_index(dev, 1, &clk);
1144 if (ret)
1145 return ret;
1146 ret = clk_get_by_id(clk.id, &c);
1147 if (ret)
1148 return ret;
1149 clk_names[ID_MD_SLCK] = kmemdup(clk_hw_get_name(c),
1150 strlen(clk_hw_get_name(c)) + 1, GFP_KERNEL);
1151 if (!clk_names[ID_MD_SLCK])
1152 return -ENOMEM;
1153
1154 ret = clk_get_by_index(dev, 2, &clk);
1155 if (ret)
1156 return ret;
1157 clk_names[ID_MAIN_XTAL] = kmemdup(clk_hw_get_name(&clk),
1158 strlen(clk_hw_get_name(&clk)) + 1, GFP_KERNEL);
1159 if (!clk_names[ID_MAIN_XTAL])
1160 return -ENOMEM;
1161
1162 ret = clk_get_by_index(dev, 3, &clk);
1163 if (ret)
1164 goto fail;
1165 clk_names[ID_MAIN_RC] = kmemdup(clk_hw_get_name(&clk),
1166 strlen(clk_hw_get_name(&clk)) + 1, GFP_KERNEL);
1167 if (ret)
1168 goto fail;
1169
1170 main_osc_bypass = dev_read_bool(dev, "atmel,main-osc-bypass");
1171
1172 /* Register main rc oscillator. */
1173 clk_dm(AT91_TO_CLK_ID(PMC_TYPE_CORE, ID_MAIN_RC_OSC),
1174 at91_clk_main_rc(base, clk_names[ID_MAIN_RC_OSC],
1175 clk_names[ID_MAIN_RC]));
1176
1177 /* Register main oscillator. */
1178 clk_dm(AT91_TO_CLK_ID(PMC_TYPE_CORE, ID_MAIN_OSC),
1179 at91_clk_main_osc(base, clk_names[ID_MAIN_OSC],
1180 clk_names[ID_MAIN_XTAL], main_osc_bypass));
1181
1182 /* Register mainck. */
1183 p[0] = clk_names[ID_MAIN_RC_OSC];
1184 p[1] = clk_names[ID_MAIN_OSC];
1185 cm[0] = AT91_TO_CLK_ID(PMC_TYPE_CORE, ID_MAIN_RC_OSC);
1186 cm[1] = AT91_TO_CLK_ID(PMC_TYPE_CORE, ID_MAIN_OSC);
1187 prepare_mux_table(clkmuxallocs, clkmuxallocindex, tmpclkmux, cm, 2,
1188 fail);
1189 clk_dm(AT91_TO_CLK_ID(PMC_TYPE_CORE, ID_MAINCK),
1190 at91_clk_sam9x5_main(base, clk_names[ID_MAINCK], p,
1191 2, tmpclkmux, PMC_TYPE_CORE));
1192
1193 /* Register PLL fracs clocks. */
1194 for (i = 0; i < ARRAY_SIZE(sama7g5_plls); i++) {
1195 if (sama7g5_plls[i].t != PLL_TYPE_FRAC)
1196 continue;
1197
1198 clk_dm(AT91_TO_CLK_ID(PMC_TYPE_CORE, sama7g5_plls[i].cid),
1199 sam9x60_clk_register_frac_pll(base, sama7g5_plls[i].n,
1200 sama7g5_plls[i].p, sama7g5_plls[i].id,
1201 &pll_characteristics, sama7g5_plls[i].l,
1202 sama7g5_plls[i].c));
1203 }
1204
1205 /* Register PLL div clocks. */
1206 for (i = 0; i < ARRAY_SIZE(sama7g5_plls); i++) {
1207 if (sama7g5_plls[i].t != PLL_TYPE_DIV)
1208 continue;
1209
1210 clk_dm(AT91_TO_CLK_ID(PMC_TYPE_CORE, sama7g5_plls[i].cid),
1211 sam9x60_clk_register_div_pll(base, sama7g5_plls[i].n,
1212 sama7g5_plls[i].p, sama7g5_plls[i].id,
1213 &pll_characteristics, sama7g5_plls[i].l,
1214 sama7g5_plls[i].c));
1215 }
1216
Claudiu Beznea8b5bbad2021-07-16 08:43:48 +03001217 /* Register MCK0_PRES clock. */
Claudiu Beznea57db4c42020-09-07 17:46:52 +03001218 p[0] = clk_names[ID_MD_SLCK];
1219 p[1] = clk_names[ID_MAINCK];
1220 p[2] = clk_names[ID_PLL_CPU_DIV];
1221 p[3] = clk_names[ID_PLL_SYS_DIV];
1222 cm[0] = AT91_TO_CLK_ID(PMC_TYPE_CORE, ID_MD_SLCK);
1223 cm[1] = AT91_TO_CLK_ID(PMC_TYPE_CORE, ID_MAINCK);
1224 cm[2] = AT91_TO_CLK_ID(PMC_TYPE_CORE, ID_PLL_CPU_DIV);
1225 cm[3] = AT91_TO_CLK_ID(PMC_TYPE_CORE, ID_PLL_SYS_DIV);
1226 prepare_mux_table(clkmuxallocs, clkmuxallocindex, tmpclkmux, cm, 2,
1227 fail);
Claudiu Beznea8b5bbad2021-07-16 08:43:48 +03001228 clk_dm(AT91_TO_CLK_ID(PMC_TYPE_CORE, ID_MCK0_PRES),
1229 at91_clk_register_master_pres(base, clk_names[ID_MCK0_PRES], p,
Claudiu Beznea57db4c42020-09-07 17:46:52 +03001230 4, &mck0_layout, &mck0_characteristics, tmpclkmux));
1231
Claudiu Beznea8b5bbad2021-07-16 08:43:48 +03001232 clk_dm(AT91_TO_CLK_ID(PMC_TYPE_CORE, ID_MCK0_DIV),
1233 at91_clk_register_master_div(base, clk_names[ID_MCK0_DIV],
1234 clk_names[ID_MCK0_PRES], &mck0_layout, &mck0_characteristics));
1235
Claudiu Beznea57db4c42020-09-07 17:46:52 +03001236 /* Register MCK1-4 clocks. */
1237 p[0] = clk_names[ID_MD_SLCK];
1238 p[1] = clk_names[ID_TD_SLCK];
1239 p[2] = clk_names[ID_MAINCK];
Claudiu Beznea8b5bbad2021-07-16 08:43:48 +03001240 p[3] = clk_names[ID_MCK0_DIV];
Claudiu Beznea57db4c42020-09-07 17:46:52 +03001241 m[0] = 0;
1242 m[1] = 1;
1243 m[2] = 2;
1244 m[3] = 3;
1245 cm[0] = AT91_TO_CLK_ID(PMC_TYPE_CORE, ID_MD_SLCK);
1246 cm[1] = AT91_TO_CLK_ID(PMC_TYPE_CORE, ID_TD_SLCK);
1247 cm[2] = AT91_TO_CLK_ID(PMC_TYPE_CORE, ID_MAINCK);
Claudiu Beznea8b5bbad2021-07-16 08:43:48 +03001248 cm[3] = AT91_TO_CLK_ID(PMC_TYPE_CORE, ID_MCK0_DIV);
Claudiu Beznea57db4c42020-09-07 17:46:52 +03001249 for (i = 0; i < ARRAY_SIZE(sama7g5_mckx); i++) {
1250 for (j = 0; j < sama7g5_mckx[i].ep_count; j++) {
1251 p[4 + j] = sama7g5_mckx[i].ep[j];
1252 m[4 + j] = sama7g5_mckx[i].ep_mux_table[j];
1253 cm[4 + j] = AT91_TO_CLK_ID(PMC_TYPE_CORE,
1254 sama7g5_mckx[i].ep_clk_mux_table[j]);
1255 }
1256
1257 prepare_mux_table(clkmuxallocs, clkmuxallocindex, tmpclkmux, cm,
1258 4 + sama7g5_mckx[i].ep_count, fail);
1259 prepare_mux_table(muxallocs, muxallocindex, tmpmux, m,
1260 4 + sama7g5_mckx[i].ep_count, fail);
1261
1262 clk_dm(AT91_TO_CLK_ID(PMC_TYPE_CORE, sama7g5_mckx[i].cid),
1263 at91_clk_sama7g5_register_master(base,
1264 sama7g5_mckx[i].n, p, 4 + sama7g5_mckx[i].ep_count,
1265 tmpmux, tmpclkmux, sama7g5_mckx[i].c,
1266 sama7g5_mckx[i].id));
1267 }
1268
1269 /* Register UTMI clock. */
1270 clk_dm(AT91_TO_CLK_ID(PMC_TYPE_CORE, ID_UTMI),
1271 at91_clk_sama7g5_register_utmi(base, "utmick",
1272 clk_names[ID_MAIN_XTAL]));
1273
1274 /* Register programmable clocks. */
1275 p[0] = clk_names[ID_MD_SLCK];
1276 p[1] = clk_names[ID_TD_SLCK];
1277 p[2] = clk_names[ID_MAINCK];
Claudiu Beznea8b5bbad2021-07-16 08:43:48 +03001278 p[3] = clk_names[ID_MCK0_DIV];
Claudiu Beznea57db4c42020-09-07 17:46:52 +03001279 p[4] = clk_names[ID_PLL_SYS_DIV];
1280 p[5] = clk_names[ID_PLL_DDR_DIV];
1281 p[6] = clk_names[ID_PLL_IMG_DIV];
1282 p[7] = clk_names[ID_PLL_BAUD_DIV];
1283 p[8] = clk_names[ID_PLL_AUDIO_DIVPMC];
1284 p[9] = clk_names[ID_PLL_ETH_DIV];
1285 cm[0] = AT91_TO_CLK_ID(PMC_TYPE_CORE, ID_MD_SLCK);
1286 cm[1] = AT91_TO_CLK_ID(PMC_TYPE_CORE, ID_TD_SLCK);
1287 cm[2] = AT91_TO_CLK_ID(PMC_TYPE_CORE, ID_MAINCK);
Claudiu Beznea8b5bbad2021-07-16 08:43:48 +03001288 cm[3] = AT91_TO_CLK_ID(PMC_TYPE_CORE, ID_MCK0_DIV);
Claudiu Beznea57db4c42020-09-07 17:46:52 +03001289 cm[4] = AT91_TO_CLK_ID(PMC_TYPE_CORE, ID_PLL_SYS_DIV);
1290 cm[5] = AT91_TO_CLK_ID(PMC_TYPE_CORE, ID_PLL_DDR_DIV);
1291 cm[6] = AT91_TO_CLK_ID(PMC_TYPE_CORE, ID_PLL_IMG_DIV);
1292 cm[7] = AT91_TO_CLK_ID(PMC_TYPE_CORE, ID_PLL_BAUD_DIV);
1293 cm[8] = AT91_TO_CLK_ID(PMC_TYPE_CORE, ID_PLL_AUDIO_DIVPMC);
1294 cm[9] = AT91_TO_CLK_ID(PMC_TYPE_CORE, ID_PLL_ETH_DIV);
1295 for (i = 0; i < ARRAY_SIZE(sama7g5_prog); i++) {
1296 prepare_mux_table(clkmuxallocs, clkmuxallocindex, tmpclkmux, cm,
1297 10, fail);
1298
1299 clk_dm(AT91_TO_CLK_ID(PMC_TYPE_CORE, sama7g5_prog[i].cid),
1300 at91_clk_register_programmable(base, sama7g5_prog[i].n,
1301 p, 10, i, &programmable_layout, tmpclkmux,
1302 sama7g5_prog_mux_table));
1303 }
1304
1305 /* System clocks. */
1306 for (i = 0; i < ARRAY_SIZE(sama7g5_systemck); i++) {
1307 clk_dm(AT91_TO_CLK_ID(PMC_TYPE_SYSTEM, sama7g5_systemck[i].cid),
1308 at91_clk_register_system(base, sama7g5_systemck[i].n,
1309 sama7g5_systemck[i].p, sama7g5_systemck[i].id));
1310 }
1311
1312 /* Peripheral clocks. */
1313 for (i = 0; i < ARRAY_SIZE(sama7g5_periphck); i++) {
1314 clk_dm(AT91_TO_CLK_ID(PMC_TYPE_PERIPHERAL,
1315 sama7g5_periphck[i].id),
1316 at91_clk_register_sam9x5_peripheral(base,
1317 &sama7g5_pcr_layout, sama7g5_periphck[i].n,
1318 sama7g5_periphck[i].p, sama7g5_periphck[i].id,
1319 &sama7g5_periphck[i].r));
1320 }
1321
1322 /* Generic clocks. */
1323 p[0] = clk_names[ID_MD_SLCK];
1324 p[1] = clk_names[ID_TD_SLCK];
1325 p[2] = clk_names[ID_MAINCK];
Claudiu Beznea8b5bbad2021-07-16 08:43:48 +03001326 p[3] = clk_names[ID_MCK0_DIV];
Claudiu Beznea57db4c42020-09-07 17:46:52 +03001327 m[0] = 0;
1328 m[1] = 1;
1329 m[2] = 2;
1330 m[3] = 3;
1331 cm[0] = AT91_TO_CLK_ID(PMC_TYPE_CORE, ID_MD_SLCK);
1332 cm[1] = AT91_TO_CLK_ID(PMC_TYPE_CORE, ID_TD_SLCK);
1333 cm[2] = AT91_TO_CLK_ID(PMC_TYPE_CORE, ID_MAINCK);
Claudiu Beznea8b5bbad2021-07-16 08:43:48 +03001334 cm[3] = AT91_TO_CLK_ID(PMC_TYPE_CORE, ID_MCK0_DIV);
Claudiu Beznea57db4c42020-09-07 17:46:52 +03001335 for (i = 0; i < ARRAY_SIZE(sama7g5_gck); i++) {
1336 for (j = 0; j < sama7g5_gck[i].ep_count; j++) {
1337 p[4 + j] = sama7g5_gck[i].ep[j];
1338 m[4 + j] = sama7g5_gck[i].ep_mux_table[j];
1339 cm[4 + j] = AT91_TO_CLK_ID(PMC_TYPE_CORE,
1340 sama7g5_gck[i].ep_clk_mux_table[j]);
1341 }
1342
1343 prepare_mux_table(clkmuxallocs, clkmuxallocindex, tmpclkmux, cm,
1344 4 + sama7g5_gck[i].ep_count, fail);
1345 prepare_mux_table(muxallocs, muxallocindex, tmpmux, m,
1346 4 + sama7g5_gck[i].ep_count, fail);
1347
1348 clk_dm(AT91_TO_CLK_ID(PMC_TYPE_GCK, sama7g5_gck[i].id),
1349 at91_clk_register_generic(base, &sama7g5_pcr_layout,
1350 sama7g5_gck[i].n, p, tmpclkmux, tmpmux,
1351 4 + sama7g5_gck[i].ep_count, sama7g5_gck[i].id,
1352 &sama7g5_gck[i].r));
1353 }
1354
1355 /* Setup clocks. */
1356 for (i = 0; i < ARRAY_SIZE(sama7g5_clk_setup); i++) {
1357 ret = clk_get_by_id(sama7g5_clk_setup[i].cid, &c);
1358 if (ret)
1359 goto fail;
1360
1361 if (sama7g5_clk_setup[i].pid) {
1362 ret = clk_get_by_id(sama7g5_clk_setup[i].pid, &parent);
1363 if (ret)
1364 goto fail;
1365
1366 ret = clk_set_parent(c, parent);
1367 if (ret)
1368 goto fail;
1369
1370 if (sama7g5_clk_setup[i].prate) {
1371 ret = clk_set_rate(parent,
1372 sama7g5_clk_setup[i].prate);
1373 if (ret < 0)
1374 goto fail;
1375 }
1376 }
1377
1378 if (sama7g5_clk_setup[i].rate) {
1379 ret = clk_set_rate(c, sama7g5_clk_setup[i].rate);
1380 if (ret < 0)
1381 goto fail;
1382 }
1383 }
1384
1385 return 0;
1386
1387fail:
1388 for (i = 0; i < ARRAY_SIZE(muxallocs); i++)
1389 kfree(muxallocs[i]);
1390
1391 for (i = 0; i < ARRAY_SIZE(clkmuxallocs); i++)
1392 kfree(clkmuxallocs[i]);
1393
1394 return -ENOMEM;
1395}
1396
1397static const struct udevice_id sama7g5_clk_ids[] = {
1398 { .compatible = "microchip,sama7g5-pmc" },
1399 { /* Sentinel. */ },
1400};
1401
1402U_BOOT_DRIVER(at91_sama7g5_pmc) = {
1403 .name = "at91-sama7g5-pmc",
1404 .id = UCLASS_CLK,
1405 .of_match = sama7g5_clk_ids,
1406 .ops = &at91_clk_ops,
1407 .probe = sama7g5_clk_probe,
1408 .flags = DM_FLAG_PRE_RELOC,
1409};