xref: /openbmc/linux/drivers/clk/at91/sama5d3.c (revision 0969b242f7b8e5de0a1c28f81d8558fd678c066d)
1*0969b242SAlexandre Belloni // SPDX-License-Identifier: GPL-2.0
2*0969b242SAlexandre Belloni #include <linux/clk-provider.h>
3*0969b242SAlexandre Belloni #include <linux/mfd/syscon.h>
4*0969b242SAlexandre Belloni #include <linux/slab.h>
5*0969b242SAlexandre Belloni 
6*0969b242SAlexandre Belloni #include <dt-bindings/clock/at91.h>
7*0969b242SAlexandre Belloni 
8*0969b242SAlexandre Belloni #include "pmc.h"
9*0969b242SAlexandre Belloni 
10*0969b242SAlexandre Belloni static const struct clk_master_characteristics mck_characteristics = {
11*0969b242SAlexandre Belloni 	.output = { .min = 0, .max = 166000000 },
12*0969b242SAlexandre Belloni 	.divisors = { 1, 2, 4, 3 },
13*0969b242SAlexandre Belloni };
14*0969b242SAlexandre Belloni 
15*0969b242SAlexandre Belloni static u8 plla_out[] = { 0 };
16*0969b242SAlexandre Belloni 
17*0969b242SAlexandre Belloni static u16 plla_icpll[] = { 0 };
18*0969b242SAlexandre Belloni 
19*0969b242SAlexandre Belloni static const struct clk_range plla_outputs[] = {
20*0969b242SAlexandre Belloni 	{ .min = 400000000, .max = 1000000000 },
21*0969b242SAlexandre Belloni };
22*0969b242SAlexandre Belloni 
23*0969b242SAlexandre Belloni static const struct clk_pll_characteristics plla_characteristics = {
24*0969b242SAlexandre Belloni 	.input = { .min = 8000000, .max = 50000000 },
25*0969b242SAlexandre Belloni 	.num_output = ARRAY_SIZE(plla_outputs),
26*0969b242SAlexandre Belloni 	.output = plla_outputs,
27*0969b242SAlexandre Belloni 	.icpll = plla_icpll,
28*0969b242SAlexandre Belloni 	.out = plla_out,
29*0969b242SAlexandre Belloni };
30*0969b242SAlexandre Belloni 
31*0969b242SAlexandre Belloni static const struct clk_pcr_layout sama5d3_pcr_layout = {
32*0969b242SAlexandre Belloni 	.offset = 0x10c,
33*0969b242SAlexandre Belloni 	.cmd = BIT(12),
34*0969b242SAlexandre Belloni 	.pid_mask = GENMASK(6, 0),
35*0969b242SAlexandre Belloni 	.div_mask = GENMASK(17, 16),
36*0969b242SAlexandre Belloni };
37*0969b242SAlexandre Belloni 
38*0969b242SAlexandre Belloni static const struct {
39*0969b242SAlexandre Belloni 	char *n;
40*0969b242SAlexandre Belloni 	char *p;
41*0969b242SAlexandre Belloni 	u8 id;
42*0969b242SAlexandre Belloni } sama5d3_systemck[] = {
43*0969b242SAlexandre Belloni 	{ .n = "ddrck", .p = "masterck", .id = 2 },
44*0969b242SAlexandre Belloni 	{ .n = "lcdck", .p = "masterck", .id = 3 },
45*0969b242SAlexandre Belloni 	{ .n = "smdck", .p = "smdclk",   .id = 4 },
46*0969b242SAlexandre Belloni 	{ .n = "uhpck", .p = "usbck",    .id = 6 },
47*0969b242SAlexandre Belloni 	{ .n = "udpck", .p = "usbck",    .id = 7 },
48*0969b242SAlexandre Belloni 	{ .n = "pck0",  .p = "prog0",    .id = 8 },
49*0969b242SAlexandre Belloni 	{ .n = "pck1",  .p = "prog1",    .id = 9 },
50*0969b242SAlexandre Belloni 	{ .n = "pck2",  .p = "prog2",    .id = 10 },
51*0969b242SAlexandre Belloni };
52*0969b242SAlexandre Belloni 
53*0969b242SAlexandre Belloni static const struct {
54*0969b242SAlexandre Belloni 	char *n;
55*0969b242SAlexandre Belloni 	u8 id;
56*0969b242SAlexandre Belloni 	struct clk_range r;
57*0969b242SAlexandre Belloni } sama5d3_periphck[] = {
58*0969b242SAlexandre Belloni 	{ .n = "dbgu_clk", .id = 2, },
59*0969b242SAlexandre Belloni 	{ .n = "hsmc_clk", .id = 5, },
60*0969b242SAlexandre Belloni 	{ .n = "pioA_clk", .id = 6, },
61*0969b242SAlexandre Belloni 	{ .n = "pioB_clk", .id = 7, },
62*0969b242SAlexandre Belloni 	{ .n = "pioC_clk", .id = 8, },
63*0969b242SAlexandre Belloni 	{ .n = "pioD_clk", .id = 9, },
64*0969b242SAlexandre Belloni 	{ .n = "pioE_clk", .id = 10, },
65*0969b242SAlexandre Belloni 	{ .n = "usart0_clk", .id = 12, .r = { .min = 0, .max = 83000000 }, },
66*0969b242SAlexandre Belloni 	{ .n = "usart1_clk", .id = 13, .r = { .min = 0, .max = 83000000 }, },
67*0969b242SAlexandre Belloni 	{ .n = "usart2_clk", .id = 14, .r = { .min = 0, .max = 83000000 }, },
68*0969b242SAlexandre Belloni 	{ .n = "usart3_clk", .id = 15, .r = { .min = 0, .max = 83000000 }, },
69*0969b242SAlexandre Belloni 	{ .n = "uart0_clk", .id = 16, .r = { .min = 0, .max = 83000000 }, },
70*0969b242SAlexandre Belloni 	{ .n = "uart1_clk", .id = 17, .r = { .min = 0, .max = 83000000 }, },
71*0969b242SAlexandre Belloni 	{ .n = "twi0_clk", .id = 18, .r = { .min = 0, .max = 41500000 }, },
72*0969b242SAlexandre Belloni 	{ .n = "twi1_clk", .id = 19, .r = { .min = 0, .max = 41500000 }, },
73*0969b242SAlexandre Belloni 	{ .n = "twi2_clk", .id = 20, .r = { .min = 0, .max = 41500000 }, },
74*0969b242SAlexandre Belloni 	{ .n = "mci0_clk", .id = 21, },
75*0969b242SAlexandre Belloni 	{ .n = "mci1_clk", .id = 22, },
76*0969b242SAlexandre Belloni 	{ .n = "mci2_clk", .id = 23, },
77*0969b242SAlexandre Belloni 	{ .n = "spi0_clk", .id = 24, .r = { .min = 0, .max = 166000000 }, },
78*0969b242SAlexandre Belloni 	{ .n = "spi1_clk", .id = 25, .r = { .min = 0, .max = 166000000 }, },
79*0969b242SAlexandre Belloni 	{ .n = "tcb0_clk", .id = 26, .r = { .min = 0, .max = 166000000 }, },
80*0969b242SAlexandre Belloni 	{ .n = "tcb1_clk", .id = 27, .r = { .min = 0, .max = 166000000 }, },
81*0969b242SAlexandre Belloni 	{ .n = "pwm_clk", .id = 28, },
82*0969b242SAlexandre Belloni 	{ .n = "adc_clk", .id = 29, .r = { .min = 0, .max = 83000000 }, },
83*0969b242SAlexandre Belloni 	{ .n = "dma0_clk", .id = 30, },
84*0969b242SAlexandre Belloni 	{ .n = "dma1_clk", .id = 31, },
85*0969b242SAlexandre Belloni 	{ .n = "uhphs_clk", .id = 32, },
86*0969b242SAlexandre Belloni 	{ .n = "udphs_clk", .id = 33, },
87*0969b242SAlexandre Belloni 	{ .n = "macb0_clk", .id = 34, },
88*0969b242SAlexandre Belloni 	{ .n = "macb1_clk", .id = 35, },
89*0969b242SAlexandre Belloni 	{ .n = "lcdc_clk", .id = 36, },
90*0969b242SAlexandre Belloni 	{ .n = "isi_clk", .id = 37, },
91*0969b242SAlexandre Belloni 	{ .n = "ssc0_clk", .id = 38, .r = { .min = 0, .max = 83000000 }, },
92*0969b242SAlexandre Belloni 	{ .n = "ssc1_clk", .id = 39, .r = { .min = 0, .max = 83000000 }, },
93*0969b242SAlexandre Belloni 	{ .n = "can0_clk", .id = 40, .r = { .min = 0, .max = 83000000 }, },
94*0969b242SAlexandre Belloni 	{ .n = "can1_clk", .id = 41, .r = { .min = 0, .max = 83000000 }, },
95*0969b242SAlexandre Belloni 	{ .n = "sha_clk", .id = 42, },
96*0969b242SAlexandre Belloni 	{ .n = "aes_clk", .id = 43, },
97*0969b242SAlexandre Belloni 	{ .n = "tdes_clk", .id = 44, },
98*0969b242SAlexandre Belloni 	{ .n = "trng_clk", .id = 45, },
99*0969b242SAlexandre Belloni 	{ .n = "fuse_clk", .id = 48, },
100*0969b242SAlexandre Belloni 	{ .n = "mpddr_clk", .id = 49, },
101*0969b242SAlexandre Belloni };
102*0969b242SAlexandre Belloni 
103*0969b242SAlexandre Belloni static void __init sama5d3_pmc_setup(struct device_node *np)
104*0969b242SAlexandre Belloni {
105*0969b242SAlexandre Belloni 	const char *slck_name, *mainxtal_name;
106*0969b242SAlexandre Belloni 	struct pmc_data *sama5d3_pmc;
107*0969b242SAlexandre Belloni 	const char *parent_names[5];
108*0969b242SAlexandre Belloni 	struct regmap *regmap;
109*0969b242SAlexandre Belloni 	struct clk_hw *hw;
110*0969b242SAlexandre Belloni 	int i;
111*0969b242SAlexandre Belloni 	bool bypass;
112*0969b242SAlexandre Belloni 
113*0969b242SAlexandre Belloni 	i = of_property_match_string(np, "clock-names", "slow_clk");
114*0969b242SAlexandre Belloni 	if (i < 0)
115*0969b242SAlexandre Belloni 		return;
116*0969b242SAlexandre Belloni 
117*0969b242SAlexandre Belloni 	slck_name = of_clk_get_parent_name(np, i);
118*0969b242SAlexandre Belloni 
119*0969b242SAlexandre Belloni 	i = of_property_match_string(np, "clock-names", "main_xtal");
120*0969b242SAlexandre Belloni 	if (i < 0)
121*0969b242SAlexandre Belloni 		return;
122*0969b242SAlexandre Belloni 	mainxtal_name = of_clk_get_parent_name(np, i);
123*0969b242SAlexandre Belloni 
124*0969b242SAlexandre Belloni 	regmap = syscon_node_to_regmap(np);
125*0969b242SAlexandre Belloni 	if (IS_ERR(regmap))
126*0969b242SAlexandre Belloni 		return;
127*0969b242SAlexandre Belloni 
128*0969b242SAlexandre Belloni 	sama5d3_pmc = pmc_data_allocate(PMC_MAIN + 1,
129*0969b242SAlexandre Belloni 					nck(sama5d3_systemck),
130*0969b242SAlexandre Belloni 					nck(sama5d3_periphck), 0);
131*0969b242SAlexandre Belloni 	if (!sama5d3_pmc)
132*0969b242SAlexandre Belloni 		return;
133*0969b242SAlexandre Belloni 
134*0969b242SAlexandre Belloni 	hw = at91_clk_register_main_rc_osc(regmap, "main_rc_osc", 12000000,
135*0969b242SAlexandre Belloni 					   50000000);
136*0969b242SAlexandre Belloni 	if (IS_ERR(hw))
137*0969b242SAlexandre Belloni 		goto err_free;
138*0969b242SAlexandre Belloni 
139*0969b242SAlexandre Belloni 	bypass = of_property_read_bool(np, "atmel,osc-bypass");
140*0969b242SAlexandre Belloni 
141*0969b242SAlexandre Belloni 	hw = at91_clk_register_main_osc(regmap, "main_osc", mainxtal_name,
142*0969b242SAlexandre Belloni 					bypass);
143*0969b242SAlexandre Belloni 	if (IS_ERR(hw))
144*0969b242SAlexandre Belloni 		goto err_free;
145*0969b242SAlexandre Belloni 
146*0969b242SAlexandre Belloni 	parent_names[0] = "main_rc_osc";
147*0969b242SAlexandre Belloni 	parent_names[1] = "main_osc";
148*0969b242SAlexandre Belloni 	hw = at91_clk_register_sam9x5_main(regmap, "mainck", parent_names, 2);
149*0969b242SAlexandre Belloni 	if (IS_ERR(hw))
150*0969b242SAlexandre Belloni 		goto err_free;
151*0969b242SAlexandre Belloni 
152*0969b242SAlexandre Belloni 	hw = at91_clk_register_pll(regmap, "pllack", "mainck", 0,
153*0969b242SAlexandre Belloni 				   &sama5d3_pll_layout, &plla_characteristics);
154*0969b242SAlexandre Belloni 	if (IS_ERR(hw))
155*0969b242SAlexandre Belloni 		goto err_free;
156*0969b242SAlexandre Belloni 
157*0969b242SAlexandre Belloni 	hw = at91_clk_register_plldiv(regmap, "plladivck", "pllack");
158*0969b242SAlexandre Belloni 	if (IS_ERR(hw))
159*0969b242SAlexandre Belloni 		goto err_free;
160*0969b242SAlexandre Belloni 
161*0969b242SAlexandre Belloni 	hw = at91_clk_register_utmi(regmap, NULL, "utmick", "mainck");
162*0969b242SAlexandre Belloni 	if (IS_ERR(hw))
163*0969b242SAlexandre Belloni 		goto err_free;
164*0969b242SAlexandre Belloni 
165*0969b242SAlexandre Belloni 	sama5d3_pmc->chws[PMC_UTMI] = hw;
166*0969b242SAlexandre Belloni 
167*0969b242SAlexandre Belloni 	parent_names[0] = slck_name;
168*0969b242SAlexandre Belloni 	parent_names[1] = "mainck";
169*0969b242SAlexandre Belloni 	parent_names[2] = "plladivck";
170*0969b242SAlexandre Belloni 	parent_names[3] = "utmick";
171*0969b242SAlexandre Belloni 	hw = at91_clk_register_master(regmap, "masterck", 4, parent_names,
172*0969b242SAlexandre Belloni 				      &at91sam9x5_master_layout,
173*0969b242SAlexandre Belloni 				      &mck_characteristics);
174*0969b242SAlexandre Belloni 	if (IS_ERR(hw))
175*0969b242SAlexandre Belloni 		goto err_free;
176*0969b242SAlexandre Belloni 
177*0969b242SAlexandre Belloni 	sama5d3_pmc->chws[PMC_MCK] = hw;
178*0969b242SAlexandre Belloni 
179*0969b242SAlexandre Belloni 	parent_names[0] = "plladivck";
180*0969b242SAlexandre Belloni 	parent_names[1] = "utmick";
181*0969b242SAlexandre Belloni 	hw = at91sam9x5_clk_register_usb(regmap, "usbck", parent_names, 2);
182*0969b242SAlexandre Belloni 	if (IS_ERR(hw))
183*0969b242SAlexandre Belloni 		goto err_free;
184*0969b242SAlexandre Belloni 
185*0969b242SAlexandre Belloni 	hw = at91sam9x5_clk_register_smd(regmap, "smdclk", parent_names, 2);
186*0969b242SAlexandre Belloni 	if (IS_ERR(hw))
187*0969b242SAlexandre Belloni 		goto err_free;
188*0969b242SAlexandre Belloni 
189*0969b242SAlexandre Belloni 	parent_names[0] = slck_name;
190*0969b242SAlexandre Belloni 	parent_names[1] = "mainck";
191*0969b242SAlexandre Belloni 	parent_names[2] = "plladivck";
192*0969b242SAlexandre Belloni 	parent_names[3] = "utmick";
193*0969b242SAlexandre Belloni 	parent_names[4] = "masterck";
194*0969b242SAlexandre Belloni 	for (i = 0; i < 3; i++) {
195*0969b242SAlexandre Belloni 		char name[6];
196*0969b242SAlexandre Belloni 
197*0969b242SAlexandre Belloni 		snprintf(name, sizeof(name), "prog%d", i);
198*0969b242SAlexandre Belloni 
199*0969b242SAlexandre Belloni 		hw = at91_clk_register_programmable(regmap, name,
200*0969b242SAlexandre Belloni 						    parent_names, 5, i,
201*0969b242SAlexandre Belloni 						    &at91sam9x5_programmable_layout);
202*0969b242SAlexandre Belloni 		if (IS_ERR(hw))
203*0969b242SAlexandre Belloni 			goto err_free;
204*0969b242SAlexandre Belloni 	}
205*0969b242SAlexandre Belloni 
206*0969b242SAlexandre Belloni 	for (i = 0; i < ARRAY_SIZE(sama5d3_systemck); i++) {
207*0969b242SAlexandre Belloni 		hw = at91_clk_register_system(regmap, sama5d3_systemck[i].n,
208*0969b242SAlexandre Belloni 					      sama5d3_systemck[i].p,
209*0969b242SAlexandre Belloni 					      sama5d3_systemck[i].id);
210*0969b242SAlexandre Belloni 		if (IS_ERR(hw))
211*0969b242SAlexandre Belloni 			goto err_free;
212*0969b242SAlexandre Belloni 
213*0969b242SAlexandre Belloni 		sama5d3_pmc->shws[sama5d3_systemck[i].id] = hw;
214*0969b242SAlexandre Belloni 	}
215*0969b242SAlexandre Belloni 
216*0969b242SAlexandre Belloni 	for (i = 0; i < ARRAY_SIZE(sama5d3_periphck); i++) {
217*0969b242SAlexandre Belloni 		hw = at91_clk_register_sam9x5_peripheral(regmap, &pmc_pcr_lock,
218*0969b242SAlexandre Belloni 							 &sama5d3_pcr_layout,
219*0969b242SAlexandre Belloni 							 sama5d3_periphck[i].n,
220*0969b242SAlexandre Belloni 							 "masterck",
221*0969b242SAlexandre Belloni 							 sama5d3_periphck[i].id,
222*0969b242SAlexandre Belloni 							 &sama5d3_periphck[i].r);
223*0969b242SAlexandre Belloni 		if (IS_ERR(hw))
224*0969b242SAlexandre Belloni 			goto err_free;
225*0969b242SAlexandre Belloni 
226*0969b242SAlexandre Belloni 		sama5d3_pmc->phws[sama5d3_periphck[i].id] = hw;
227*0969b242SAlexandre Belloni 	}
228*0969b242SAlexandre Belloni 
229*0969b242SAlexandre Belloni 	of_clk_add_hw_provider(np, of_clk_hw_pmc_get, sama5d3_pmc);
230*0969b242SAlexandre Belloni 
231*0969b242SAlexandre Belloni 	return;
232*0969b242SAlexandre Belloni 
233*0969b242SAlexandre Belloni err_free:
234*0969b242SAlexandre Belloni 	pmc_data_free(sama5d3_pmc);
235*0969b242SAlexandre Belloni }
236*0969b242SAlexandre Belloni /*
237*0969b242SAlexandre Belloni  * The TCB is used as the clocksource so its clock is needed early. This means
238*0969b242SAlexandre Belloni  * this can't be a platform driver.
239*0969b242SAlexandre Belloni  */
240*0969b242SAlexandre Belloni CLK_OF_DECLARE_DRIVER(sama5d3_pmc, "atmel,sama5d3-pmc", sama5d3_pmc_setup);
241