blob: 14c2ffcac18bc306b3e985a0a8c88404733bb34b [file] [log] [blame]
Claudiu Bezneabaf5b522020-10-07 18:17:08 +03001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2020 Microchip Technology Inc. and its subsidiaries
4 *
5 * Author: Claudiu Beznea <claudiu.beznea@microchip.com>
6 *
7 * Based on sam9x60.c on Linux.
8 */
9
10#include <common.h>
11#include <clk-uclass.h>
12#include <dm.h>
13#include <dt-bindings/clk/at91.h>
14#include <linux/clk-provider.h>
15
16#include "pmc.h"
17
18/**
19 * Clock identifiers to be used in conjunction with macros like
20 * AT91_TO_CLK_ID()
21 *
22 * @ID_MD_SLCK: TD slow clock identifier
23 * @ID_TD_SLCK: MD slow clock identifier
24 * @ID_MAIN_XTAL: Main Xtal clock identifier
25 * @ID_MAIN_RC: Main RC clock identifier
26 * @ID_MAIN_RC_OSC: Main RC Oscillator clock identifier
27 * @ID_MAIN_OSC: Main Oscillator clock identifier
28 * @ID_MAINCK: MAINCK clock identifier
29 * @ID_PLL_U_FRAC: UPLL fractional clock identifier
30 * @ID_PLL_U_DIV: UPLL divider clock identifier
31 * @ID_PLL_A_FRAC: APLL fractional clock identifier
32 * @ID_PLL_A_DIV: APLL divider clock identifier
33
Claudiu Beznea8b5bbad2021-07-16 08:43:48 +030034 * @ID_MCK_DIV: MCK DIV clock identifier
Claudiu Bezneabaf5b522020-10-07 18:17:08 +030035
36 * @ID_UTMI: UTMI clock identifier
37
38 * @ID_PROG0: Programmable 0 clock identifier
39 * @ID_PROG1: Programmable 1 clock identifier
40
41 * @ID_PCK0: PCK0 system clock identifier
42 * @ID_PCK1: PCK1 system clock identifier
43 * @ID_DDR: DDR system clock identifier
44 * @ID_QSPI: QSPI system clock identifier
45 *
Claudiu Beznea8b5bbad2021-07-16 08:43:48 +030046 * @ID_MCK_PRES: MCK PRES clock identifier
47 *
Claudiu Bezneabaf5b522020-10-07 18:17:08 +030048 * Note: if changing the values of this enums please sync them with
49 * device tree
50 */
51enum pmc_clk_ids {
52 ID_MD_SLCK = 0,
53 ID_TD_SLCK = 1,
54 ID_MAIN_XTAL = 2,
55 ID_MAIN_RC = 3,
56 ID_MAIN_RC_OSC = 4,
57 ID_MAIN_OSC = 5,
58 ID_MAINCK = 6,
59
60 ID_PLL_U_FRAC = 7,
61 ID_PLL_U_DIV = 8,
62 ID_PLL_A_FRAC = 9,
63 ID_PLL_A_DIV = 10,
64
Claudiu Beznea8b5bbad2021-07-16 08:43:48 +030065 ID_MCK_DIV = 11,
Claudiu Bezneabaf5b522020-10-07 18:17:08 +030066
67 ID_UTMI = 12,
68
69 ID_PROG0 = 13,
70 ID_PROG1 = 14,
71
72 ID_PCK0 = 15,
73 ID_PCK1 = 16,
74
75 ID_DDR = 17,
76 ID_QSPI = 18,
77
Claudiu Beznea8b5bbad2021-07-16 08:43:48 +030078 ID_MCK_PRES = 19,
Sergiu Moga790b9702023-03-08 16:39:51 +020079 ID_USBCK = 20,
80 ID_UHPCK = 21,
Claudiu Beznea8b5bbad2021-07-16 08:43:48 +030081
Claudiu Bezneabaf5b522020-10-07 18:17:08 +030082 ID_MAX,
83};
84
85/**
86 * PLL type identifiers
87 * @PLL_TYPE_FRAC: fractional PLL identifier
88 * @PLL_TYPE_DIV: divider PLL identifier
89 */
90enum pll_type {
91 PLL_TYPE_FRAC,
92 PLL_TYPE_DIV,
93};
94
95/* Clock names used as parents for multiple clocks. */
96static const char *clk_names[] = {
97 [ID_MAIN_RC_OSC] = "main_rc_osc",
98 [ID_MAIN_OSC] = "main_osc",
99 [ID_MAINCK] = "mainck",
100 [ID_PLL_U_DIV] = "upll_divpmcck",
101 [ID_PLL_A_DIV] = "plla_divpmcck",
Claudiu Beznea8b5bbad2021-07-16 08:43:48 +0300102 [ID_MCK_PRES] = "mck_pres",
103 [ID_MCK_DIV] = "mck_div",
Sergiu Moga790b9702023-03-08 16:39:51 +0200104 [ID_USBCK] = "usbck",
Claudiu Bezneabaf5b522020-10-07 18:17:08 +0300105};
106
107/* Fractional PLL output range. */
108static const struct clk_range plla_outputs[] = {
109 { .min = 2343750, .max = 1200000000 },
110};
111
112static const struct clk_range upll_outputs[] = {
113 { .min = 300000000, .max = 500000000 },
114};
115
116/* PLL characteristics. */
117static const struct clk_pll_characteristics apll_characteristics = {
118 .input = { .min = 12000000, .max = 48000000 },
119 .num_output = ARRAY_SIZE(plla_outputs),
120 .output = plla_outputs,
121};
122
123static const struct clk_pll_characteristics upll_characteristics = {
124 .input = { .min = 12000000, .max = 48000000 },
125 .num_output = ARRAY_SIZE(upll_outputs),
126 .output = upll_outputs,
127 .upll = true,
128};
129
130/* Layout for fractional PLLs. */
131static const struct clk_pll_layout pll_layout_frac = {
132 .mul_mask = GENMASK(31, 24),
133 .frac_mask = GENMASK(21, 0),
134 .mul_shift = 24,
135 .frac_shift = 0,
136};
137
138/* Layout for DIV PLLs. */
139static const struct clk_pll_layout pll_layout_div = {
140 .div_mask = GENMASK(7, 0),
141 .endiv_mask = BIT(29),
142 .div_shift = 0,
143 .endiv_shift = 29,
144};
145
146/* MCK characteristics. */
147static const struct clk_master_characteristics mck_characteristics = {
148 .output = { .min = 140000000, .max = 200000000 },
149 .divisors = { 1, 2, 4, 3 },
150 .have_div3_pres = 1,
151};
152
153/* MCK layout. */
154static const struct clk_master_layout mck_layout = {
155 .mask = 0x373,
156 .pres_shift = 4,
157 .offset = 0x28,
158};
159
160/* Programmable clock layout. */
161static const struct clk_programmable_layout programmable_layout = {
162 .pres_mask = 0xff,
163 .pres_shift = 8,
164 .css_mask = 0x1f,
165 .have_slck_mck = 0,
166 .is_pres_direct = 1,
167};
168
169/* Peripheral clock layout. */
170static const struct clk_pcr_layout pcr_layout = {
171 .offset = 0x88,
172 .cmd = BIT(31),
173 .gckcss_mask = GENMASK(12, 8),
174 .pid_mask = GENMASK(6, 0),
175};
176
Sergiu Moga790b9702023-03-08 16:39:51 +0200177/* USB clock layout */
178static const struct clk_usbck_layout usbck_layout = {
179 .offset = 0x38,
180 .usbs_mask = GENMASK(1, 0),
181 .usbdiv_mask = GENMASK(11, 8),
182};
183
Claudiu Bezneabaf5b522020-10-07 18:17:08 +0300184/**
185 * PLL clocks description
186 * @n: clock name
187 * @p: clock parent
188 * @l: clock layout
189 * @t: clock type
190 * @f: true if clock is fixed and not changeable by driver
191 * @id: clock id corresponding to PLL driver
192 * @cid: clock id corresponding to clock subsystem
193 */
194static const struct {
195 const char *n;
196 const char *p;
197 const struct clk_pll_layout *l;
198 const struct clk_pll_characteristics *c;
199 u8 t;
200 u8 f;
201 u8 id;
202 u8 cid;
203} sam9x60_plls[] = {
204 {
205 .n = "plla_fracck",
206 .p = "mainck",
207 .l = &pll_layout_frac,
208 .c = &apll_characteristics,
209 .t = PLL_TYPE_FRAC,
210 .f = 1,
211 .id = 0,
212 .cid = ID_PLL_A_FRAC,
213 },
214
215 {
216 .n = "plla_divpmcck",
217 .p = "plla_fracck",
218 .l = &pll_layout_div,
219 .c = &apll_characteristics,
220 .t = PLL_TYPE_DIV,
221 .f = 1,
222 .id = 0,
223 .cid = ID_PLL_A_DIV,
224 },
225
226 {
227 .n = "upll_fracck",
228 .p = "main_osc",
229 .l = &pll_layout_frac,
230 .c = &upll_characteristics,
231 .t = PLL_TYPE_FRAC,
232 .f = 1,
233 .id = 1,
234 .cid = ID_PLL_U_FRAC,
235 },
236
237 {
238 .n = "upll_divpmcck",
239 .p = "upll_fracck",
240 .l = &pll_layout_div,
241 .c = &upll_characteristics,
242 .t = PLL_TYPE_DIV,
243 .f = 1,
244 .id = 1,
245 .cid = ID_PLL_U_DIV,
246 },
247};
248
249/**
250 * Programmable clock description
251 * @n: clock name
252 * @cid: clock id corresponding to clock subsystem
253 */
254static const struct {
255 const char *n;
256 u8 cid;
257} sam9x60_prog[] = {
258 { .n = "prog0", .cid = ID_PROG0, },
259 { .n = "prog1", .cid = ID_PROG1, },
260};
261
262/* Mux table for programmable clocks. */
263static u32 sam9x60_prog_mux_table[] = { 0, 1, 2, 3, 4, 5, };
264
265/**
266 * System clock description
267 * @n: clock name
268 * @p: parent clock name
269 * @id: clock id corresponding to system clock driver
270 * @cid: clock id corresponding to clock subsystem
271 */
272static const struct {
273 const char *n;
274 const char *p;
275 u8 id;
276 u8 cid;
277} sam9x60_systemck[] = {
Mihai Saind469b1a2022-07-19 16:51:59 +0300278 { .n = "ddrck", .p = "mck_div", .id = 2, .cid = ID_DDR, },
Sergiu Moga790b9702023-03-08 16:39:51 +0200279 { .n = "uhpck", .p = "usbck", .id = 6, .cid = ID_UHPCK },
Claudiu Bezneabaf5b522020-10-07 18:17:08 +0300280 { .n = "pck0", .p = "prog0", .id = 8, .cid = ID_PCK0, },
281 { .n = "pck1", .p = "prog1", .id = 9, .cid = ID_PCK1, },
Mihai Saind469b1a2022-07-19 16:51:59 +0300282 { .n = "qspick", .p = "mck_div", .id = 19, .cid = ID_QSPI, },
Claudiu Bezneabaf5b522020-10-07 18:17:08 +0300283};
284
285/**
286 * Peripheral clock description
287 * @n: clock name
288 * @id: clock id
289 */
290static const struct {
291 const char *n;
292 u8 id;
293} sam9x60_periphck[] = {
294 { .n = "pioA_clk", .id = 2, },
295 { .n = "pioB_clk", .id = 3, },
296 { .n = "pioC_clk", .id = 4, },
297 { .n = "flex0_clk", .id = 5, },
298 { .n = "flex1_clk", .id = 6, },
299 { .n = "flex2_clk", .id = 7, },
300 { .n = "flex3_clk", .id = 8, },
301 { .n = "flex6_clk", .id = 9, },
302 { .n = "flex7_clk", .id = 10, },
303 { .n = "flex8_clk", .id = 11, },
304 { .n = "sdmmc0_clk", .id = 12, },
305 { .n = "flex4_clk", .id = 13, },
306 { .n = "flex5_clk", .id = 14, },
307 { .n = "flex9_clk", .id = 15, },
308 { .n = "flex10_clk", .id = 16, },
309 { .n = "tcb0_clk", .id = 17, },
310 { .n = "pwm_clk", .id = 18, },
311 { .n = "adc_clk", .id = 19, },
312 { .n = "dma0_clk", .id = 20, },
313 { .n = "matrix_clk", .id = 21, },
314 { .n = "uhphs_clk", .id = 22, },
315 { .n = "udphs_clk", .id = 23, },
316 { .n = "macb0_clk", .id = 24, },
317 { .n = "lcd_clk", .id = 25, },
318 { .n = "sdmmc1_clk", .id = 26, },
319 { .n = "macb1_clk", .id = 27, },
320 { .n = "ssc_clk", .id = 28, },
321 { .n = "can0_clk", .id = 29, },
322 { .n = "can1_clk", .id = 30, },
323 { .n = "flex11_clk", .id = 32, },
324 { .n = "flex12_clk", .id = 33, },
325 { .n = "i2s_clk", .id = 34, },
326 { .n = "qspi_clk", .id = 35, },
327 { .n = "gfx2d_clk", .id = 36, },
328 { .n = "pit64b_clk", .id = 37, },
329 { .n = "trng_clk", .id = 38, },
330 { .n = "aes_clk", .id = 39, },
331 { .n = "tdes_clk", .id = 40, },
332 { .n = "sha_clk", .id = 41, },
333 { .n = "classd_clk", .id = 42, },
334 { .n = "isi_clk", .id = 43, },
335 { .n = "pioD_clk", .id = 44, },
336 { .n = "tcb1_clk", .id = 45, },
337 { .n = "dbgu_clk", .id = 47, },
338 { .n = "mpddr_clk", .id = 49, },
339};
340
341/**
342 * Generic clock description
343 * @n: clock name
344 * @ep: extra parents parents names
345 * @ep_mux_table: extra parents mux table
346 * @ep_clk_mux_table: extra parents clock mux table (for CCF)
347 * @r: clock output range
348 * @ep_count: extra parents count
349 * @id: clock id
350 */
351static const struct {
352 const char *n;
353 struct clk_range r;
354 u8 id;
355} sam9x60_gck[] = {
356 { .n = "flex0_gclk", .id = 5, },
357 { .n = "flex1_gclk", .id = 6, },
358 { .n = "flex2_gclk", .id = 7, },
359 { .n = "flex3_gclk", .id = 8, },
360 { .n = "flex6_gclk", .id = 9, },
361 { .n = "flex7_gclk", .id = 10, },
362 { .n = "flex8_gclk", .id = 11, },
363 { .n = "sdmmc0_gclk", .id = 12, .r = { .min = 0, .max = 105000000 }, },
364 { .n = "flex4_gclk", .id = 13, },
365 { .n = "flex5_gclk", .id = 14, },
366 { .n = "flex9_gclk", .id = 15, },
367 { .n = "flex10_gclk", .id = 16, },
368 { .n = "tcb0_gclk", .id = 17, },
369 { .n = "adc_gclk", .id = 19, },
370 { .n = "lcd_gclk", .id = 25, .r = { .min = 0, .max = 140000000 }, },
371 { .n = "sdmmc1_gclk", .id = 26, .r = { .min = 0, .max = 105000000 }, },
372 { .n = "flex11_gclk", .id = 32, },
373 { .n = "flex12_gclk", .id = 33, },
374 { .n = "i2s_gclk", .id = 34, .r = { .min = 0, .max = 105000000 }, },
375 { .n = "pit64b_gclk", .id = 37, },
376 { .n = "classd_gclk", .id = 42, .r = { .min = 0, .max = 100000000 }, },
377 { .n = "tcb1_gclk", .id = 45, },
378 { .n = "dbgu_gclk", .id = 47, },
379};
380
381#define prepare_mux_table(_allocs, _index, _dst, _src, _num, _label) \
382 do { \
383 int _i; \
384 (_dst) = kzalloc(sizeof(*(_dst)) * (_num), GFP_KERNEL); \
385 if (!(_dst)) { \
386 ret = -ENOMEM; \
387 goto _label; \
388 } \
389 (_allocs)[(_index)++] = (_dst); \
390 for (_i = 0; _i < (_num); _i++) \
391 (_dst)[_i] = (_src)[_i]; \
392 } while (0)
393
394static int sam9x60_clk_probe(struct udevice *dev)
395{
396 void __iomem *base = (void *)devfdt_get_addr_ptr(dev);
397 unsigned int *clkmuxallocs[64], *muxallocs[64];
398 const char *p[10];
399 unsigned int cm[10], m[10], *tmpclkmux, *tmpmux;
400 struct clk clk, *c;
Claudiu Bezneabaf5b522020-10-07 18:17:08 +0300401 int ret, muxallocindex = 0, clkmuxallocindex = 0, i;
402 static const struct clk_range r = { 0, 0 };
403
404 if (!base)
405 return -EINVAL;
406
407 memset(muxallocs, 0, ARRAY_SIZE(muxallocs));
408 memset(clkmuxallocs, 0, ARRAY_SIZE(clkmuxallocs));
409
410 ret = clk_get_by_index(dev, 0, &clk);
411 if (ret)
412 return ret;
413
414 ret = clk_get_by_id(clk.id, &c);
415 if (ret)
416 return ret;
417
418 clk_names[ID_TD_SLCK] = kmemdup(clk_hw_get_name(c),
419 strlen(clk_hw_get_name(c)) + 1,
420 GFP_KERNEL);
421 if (!clk_names[ID_TD_SLCK])
422 return -ENOMEM;
423
424 ret = clk_get_by_index(dev, 1, &clk);
425 if (ret)
426 return ret;
427
428 ret = clk_get_by_id(clk.id, &c);
429 if (ret)
430 return ret;
431
432 clk_names[ID_MD_SLCK] = kmemdup(clk_hw_get_name(c),
433 strlen(clk_hw_get_name(c)) + 1,
434 GFP_KERNEL);
435 if (!clk_names[ID_MD_SLCK])
436 return -ENOMEM;
437
438 ret = clk_get_by_index(dev, 2, &clk);
439 if (ret)
440 return ret;
441
442 clk_names[ID_MAIN_XTAL] = kmemdup(clk_hw_get_name(&clk),
443 strlen(clk_hw_get_name(&clk)) + 1,
444 GFP_KERNEL);
445 if (!clk_names[ID_MAIN_XTAL])
446 return -ENOMEM;
447
448 ret = clk_get_by_index(dev, 3, &clk);
449 if (ret)
450 goto fail;
451
452 clk_names[ID_MAIN_RC] = kmemdup(clk_hw_get_name(&clk),
453 strlen(clk_hw_get_name(&clk)) + 1,
454 GFP_KERNEL);
455 if (ret)
456 goto fail;
457
Claudiu Bezneabaf5b522020-10-07 18:17:08 +0300458 /* Register main rc oscillator. */
459 c = at91_clk_main_rc(base, clk_names[ID_MAIN_RC_OSC],
460 clk_names[ID_MAIN_RC]);
461 if (IS_ERR(c)) {
462 ret = PTR_ERR(c);
463 goto fail;
464 }
465 clk_dm(AT91_TO_CLK_ID(PMC_TYPE_CORE, ID_MAIN_RC_OSC), c);
466
467 /* Register main oscillator. */
468 c = at91_clk_main_osc(base, clk_names[ID_MAIN_OSC],
Claudiu Beznea84503cd2020-12-02 13:39:33 +0200469 clk_names[ID_MAIN_XTAL], false);
Claudiu Bezneabaf5b522020-10-07 18:17:08 +0300470 if (IS_ERR(c)) {
471 ret = PTR_ERR(c);
472 goto fail;
473 }
474 clk_dm(AT91_TO_CLK_ID(PMC_TYPE_CORE, ID_MAIN_OSC), c);
475
476 /* Register mainck. */
477 p[0] = clk_names[ID_MAIN_RC_OSC];
478 p[1] = clk_names[ID_MAIN_OSC];
479 cm[0] = AT91_TO_CLK_ID(PMC_TYPE_CORE, ID_MAIN_RC_OSC);
480 cm[1] = AT91_TO_CLK_ID(PMC_TYPE_CORE, ID_MAIN_OSC);
481 prepare_mux_table(clkmuxallocs, clkmuxallocindex, tmpclkmux, cm, 2,
482 fail);
483 c = at91_clk_sam9x5_main(base, clk_names[ID_MAINCK], p,
484 2, tmpclkmux, PMC_TYPE_CORE);
485 if (IS_ERR(c)) {
486 ret = PTR_ERR(c);
487 goto fail;
488 }
489 clk_dm(AT91_TO_CLK_ID(PMC_TYPE_CORE, ID_MAINCK), c);
490
491 /* Register PLL fracs clocks. */
492 for (i = 0; i < ARRAY_SIZE(sam9x60_plls); i++) {
493 if (sam9x60_plls[i].t != PLL_TYPE_FRAC)
494 continue;
495
496 c = sam9x60_clk_register_frac_pll(base, sam9x60_plls[i].n,
497 sam9x60_plls[i].p,
498 sam9x60_plls[i].id,
499 sam9x60_plls[i].c,
500 sam9x60_plls[i].l,
501 sam9x60_plls[i].f);
502 if (IS_ERR(c)) {
503 ret = PTR_ERR(c);
504 goto fail;
505 }
506 clk_dm(AT91_TO_CLK_ID(PMC_TYPE_CORE, sam9x60_plls[i].cid), c);
507 }
508
509 /* Register PLL div clocks. */
510 for (i = 0; i < ARRAY_SIZE(sam9x60_plls); i++) {
511 if (sam9x60_plls[i].t != PLL_TYPE_DIV)
512 continue;
513
514 c = sam9x60_clk_register_div_pll(base, sam9x60_plls[i].n,
515 sam9x60_plls[i].p,
516 sam9x60_plls[i].id,
517 sam9x60_plls[i].c,
518 sam9x60_plls[i].l,
519 sam9x60_plls[i].f);
520 if (IS_ERR(c)) {
521 ret = PTR_ERR(c);
522 goto fail;
523 }
524 clk_dm(AT91_TO_CLK_ID(PMC_TYPE_CORE, sam9x60_plls[i].cid), c);
525 }
526
Claudiu Beznea8b5bbad2021-07-16 08:43:48 +0300527 /* Register MCK pres clock. */
Claudiu Bezneabaf5b522020-10-07 18:17:08 +0300528 p[0] = clk_names[ID_MD_SLCK];
529 p[1] = clk_names[ID_MAINCK];
530 p[2] = clk_names[ID_PLL_A_DIV];
531 p[3] = clk_names[ID_PLL_U_DIV];
532 cm[0] = AT91_TO_CLK_ID(PMC_TYPE_CORE, ID_MD_SLCK);
533 cm[1] = AT91_TO_CLK_ID(PMC_TYPE_CORE, ID_MAINCK);
534 cm[2] = AT91_TO_CLK_ID(PMC_TYPE_CORE, ID_PLL_A_DIV);
535 cm[3] = AT91_TO_CLK_ID(PMC_TYPE_CORE, ID_PLL_U_DIV);
536 prepare_mux_table(clkmuxallocs, clkmuxallocindex, tmpclkmux, cm, 4,
537 fail);
Claudiu Beznea8b5bbad2021-07-16 08:43:48 +0300538 c = at91_clk_register_master_pres(base, clk_names[ID_MCK_PRES], p, 4,
539 &mck_layout, &mck_characteristics,
540 tmpclkmux);
541 if (IS_ERR(c)) {
542 ret = PTR_ERR(c);
543 goto fail;
544 }
545 clk_dm(AT91_TO_CLK_ID(PMC_TYPE_CORE, ID_MCK_PRES), c);
546
547 /* Register MCK div clock. */
548 c = at91_clk_register_master_div(base, clk_names[ID_MCK_DIV],
549 clk_names[ID_MCK_PRES],
550 &mck_layout, &mck_characteristics);
Claudiu Bezneabaf5b522020-10-07 18:17:08 +0300551 if (IS_ERR(c)) {
552 ret = PTR_ERR(c);
553 goto fail;
554 }
Claudiu Beznea8b5bbad2021-07-16 08:43:48 +0300555 clk_dm(AT91_TO_CLK_ID(PMC_TYPE_CORE, ID_MCK_DIV), c);
Claudiu Bezneabaf5b522020-10-07 18:17:08 +0300556
Sergiu Moga790b9702023-03-08 16:39:51 +0200557 /* Register usbck. */
558 p[0] = clk_names[ID_PLL_A_DIV];
559 p[1] = clk_names[ID_PLL_U_DIV];
560 p[2] = clk_names[ID_MAIN_XTAL];
561 m[0] = 0;
562 m[1] = 1;
563 m[2] = 2;
564 cm[0] = AT91_TO_CLK_ID(PMC_TYPE_CORE, ID_PLL_A_DIV);
565 cm[1] = AT91_TO_CLK_ID(PMC_TYPE_CORE, ID_PLL_U_DIV);
566 cm[2] = AT91_TO_CLK_ID(PMC_TYPE_CORE, ID_MAIN_XTAL);
567 prepare_mux_table(clkmuxallocs, clkmuxallocindex, tmpclkmux, cm,
568 3, fail);
569 prepare_mux_table(muxallocs, muxallocindex, tmpmux, m, 3, fail);
570 c = sam9x60_clk_register_usb(base, clk_names[ID_USBCK], p, 3,
571 &usbck_layout, tmpclkmux, tmpmux,
572 ID_USBCK);
573 if (IS_ERR(c)) {
574 ret = PTR_ERR(c);
575 goto fail;
576 }
577 clk_dm(AT91_TO_CLK_ID(PMC_TYPE_CORE, ID_USBCK), c);
578
Claudiu Bezneabaf5b522020-10-07 18:17:08 +0300579 /* Register programmable clocks. */
580 p[0] = clk_names[ID_MD_SLCK];
581 p[1] = clk_names[ID_TD_SLCK];
582 p[2] = clk_names[ID_MAINCK];
Claudiu Beznea8b5bbad2021-07-16 08:43:48 +0300583 p[3] = clk_names[ID_MCK_DIV];
Claudiu Bezneabaf5b522020-10-07 18:17:08 +0300584 p[4] = clk_names[ID_PLL_A_DIV];
585 p[5] = clk_names[ID_PLL_U_DIV];
586 cm[0] = AT91_TO_CLK_ID(PMC_TYPE_CORE, ID_MD_SLCK);
587 cm[1] = AT91_TO_CLK_ID(PMC_TYPE_CORE, ID_TD_SLCK);
588 cm[2] = AT91_TO_CLK_ID(PMC_TYPE_CORE, ID_MAINCK);
Claudiu Beznea8b5bbad2021-07-16 08:43:48 +0300589 cm[3] = AT91_TO_CLK_ID(PMC_TYPE_CORE, ID_MCK_DIV);
Claudiu Bezneabaf5b522020-10-07 18:17:08 +0300590 cm[4] = AT91_TO_CLK_ID(PMC_TYPE_CORE, ID_PLL_A_DIV);
591 cm[5] = AT91_TO_CLK_ID(PMC_TYPE_CORE, ID_PLL_U_DIV);
592 for (i = 0; i < ARRAY_SIZE(sam9x60_prog); i++) {
593 prepare_mux_table(clkmuxallocs, clkmuxallocindex, tmpclkmux, cm,
594 6, fail);
595
596 c = at91_clk_register_programmable(base, sam9x60_prog[i].n, p,
597 10, i, &programmable_layout,
598 tmpclkmux,
599 sam9x60_prog_mux_table);
600 if (IS_ERR(c)) {
601 ret = PTR_ERR(c);
602 goto fail;
603 }
604 clk_dm(AT91_TO_CLK_ID(PMC_TYPE_CORE, sam9x60_prog[i].cid), c);
605 }
606
607 /* System clocks. */
608 for (i = 0; i < ARRAY_SIZE(sam9x60_systemck); i++) {
609 c = at91_clk_register_system(base, sam9x60_systemck[i].n,
610 sam9x60_systemck[i].p,
611 sam9x60_systemck[i].id);
612 if (IS_ERR(c)) {
613 ret = PTR_ERR(c);
614 goto fail;
615 }
616 clk_dm(AT91_TO_CLK_ID(PMC_TYPE_SYSTEM, sam9x60_systemck[i].cid),
617 c);
618 }
619
620 /* Peripheral clocks. */
621 for (i = 0; i < ARRAY_SIZE(sam9x60_periphck); i++) {
622 c = at91_clk_register_sam9x5_peripheral(base, &pcr_layout,
623 sam9x60_periphck[i].n,
Claudiu Beznea8b5bbad2021-07-16 08:43:48 +0300624 clk_names[ID_MCK_DIV],
Claudiu Bezneabaf5b522020-10-07 18:17:08 +0300625 sam9x60_periphck[i].id,
626 &r);
627 if (IS_ERR(c)) {
628 ret = PTR_ERR(c);
629 goto fail;
630 }
631 clk_dm(AT91_TO_CLK_ID(PMC_TYPE_PERIPHERAL,
632 sam9x60_periphck[i].id), c);
633 }
634
635 /* Generic clocks. */
636 p[0] = clk_names[ID_MD_SLCK];
637 p[1] = clk_names[ID_TD_SLCK];
638 p[2] = clk_names[ID_MAINCK];
Claudiu Beznea8b5bbad2021-07-16 08:43:48 +0300639 p[3] = clk_names[ID_MCK_DIV];
Claudiu Bezneabaf5b522020-10-07 18:17:08 +0300640 p[4] = clk_names[ID_PLL_A_DIV];
641 p[5] = clk_names[ID_PLL_U_DIV];
642 m[0] = 0;
643 m[1] = 1;
644 m[2] = 2;
645 m[3] = 3;
646 m[4] = 4;
647 m[5] = 5;
648 cm[0] = AT91_TO_CLK_ID(PMC_TYPE_CORE, ID_MD_SLCK);
649 cm[1] = AT91_TO_CLK_ID(PMC_TYPE_CORE, ID_TD_SLCK);
650 cm[2] = AT91_TO_CLK_ID(PMC_TYPE_CORE, ID_MAINCK);
Claudiu Beznea8b5bbad2021-07-16 08:43:48 +0300651 cm[3] = AT91_TO_CLK_ID(PMC_TYPE_CORE, ID_MCK_DIV);
Claudiu Bezneabaf5b522020-10-07 18:17:08 +0300652 cm[4] = AT91_TO_CLK_ID(PMC_TYPE_CORE, ID_PLL_A_DIV);
653 cm[5] = AT91_TO_CLK_ID(PMC_TYPE_CORE, ID_PLL_U_DIV);
654 for (i = 0; i < ARRAY_SIZE(sam9x60_gck); i++) {
655 prepare_mux_table(clkmuxallocs, clkmuxallocindex, tmpclkmux, cm,
656 6, fail);
657 prepare_mux_table(muxallocs, muxallocindex, tmpmux, m,
658 6, fail);
659
660 c = at91_clk_register_generic(base, &pcr_layout,
661 sam9x60_gck[i].n, p, tmpclkmux,
662 tmpmux, 6, sam9x60_gck[i].id,
663 &sam9x60_gck[i].r);
664 if (IS_ERR(c)) {
665 ret = PTR_ERR(c);
666 goto fail;
667 }
668 clk_dm(AT91_TO_CLK_ID(PMC_TYPE_GCK, sam9x60_gck[i].id), c);
669 }
670
671 return 0;
672
673fail:
674 for (i = 0; i < ARRAY_SIZE(muxallocs); i++)
675 kfree(muxallocs[i]);
676
677 for (i = 0; i < ARRAY_SIZE(clkmuxallocs); i++)
678 kfree(clkmuxallocs[i]);
679
680 return ret;
681}
682
683static const struct udevice_id sam9x60_clk_ids[] = {
684 { .compatible = "microchip,sam9x60-pmc" },
685 { /* Sentinel. */ },
686};
687
688U_BOOT_DRIVER(at91_sam9x60_pmc) = {
689 .name = "at91-sam9x60-pmc",
690 .id = UCLASS_CLK,
691 .of_match = sam9x60_clk_ids,
692 .ops = &at91_clk_ops,
693 .probe = sam9x60_clk_probe,
694 .flags = DM_FLAG_PRE_RELOC,
695};