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