1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) ASPEED Technology Inc.
4  */
5 
6 #include <common.h>
7 #include <dm.h>
8 #include <errno.h>
9 #include <asm/io.h>
10 #include <linux/bitops.h>
11 #include <asm/arch/pinctrl.h>
12 #include <asm/arch/scu_ast2600.h>
13 #include <dm/pinctrl.h>
14 #include "pinctrl-aspeed.h"
15 /*
16  * This driver works with very simple configuration that has the same name
17  * for group and function. This way it is compatible with the Linux Kernel
18  * driver.
19  */
20 
21 struct ast2600_pinctrl_priv {
22 	struct ast2600_scu *scu;
23 };
24 
25 static int ast2600_pinctrl_probe(struct udevice *dev)
26 {
27 	struct ast2600_pinctrl_priv *priv = dev_get_priv(dev);
28 	struct udevice *clk_dev;
29 	int ret = 0;
30 
31 	/* find SCU base address from clock device */
32 	ret = uclass_get_device_by_driver(UCLASS_CLK, DM_GET_DRIVER(aspeed_scu),
33                                           &clk_dev);
34     if (ret) {
35             debug("clock device not found\n");
36             return ret;
37     }
38 
39 	priv->scu = devfdt_get_addr_ptr(clk_dev);
40 	if (IS_ERR(priv->scu)) {
41 	        debug("%s(): can't get SCU\n", __func__);
42 	        return PTR_ERR(priv->scu);
43 	}
44 
45 	return 0;
46 }
47 
48 static struct aspeed_sig_desc i2c1_link[] = {
49 	{ 0x418, GENMASK(9, 8), 1 },
50 	{ 0x4B8, GENMASK(9, 8), 0 },
51 };
52 
53 static struct aspeed_sig_desc i2c2_link[] = {
54 	{ 0x418, GENMASK(11, 10), 1 },
55 	{ 0x4B8, GENMASK(11, 10), 0 },
56 };
57 
58 static struct aspeed_sig_desc i2c3_link[] = {
59 	{ 0x418, GENMASK(13, 12), 1 },
60 	{ 0x4B8, GENMASK(13, 12), 0 },
61 };
62 
63 static struct aspeed_sig_desc i2c4_link[] = {
64 	{ 0x418, GENMASK(15, 14), 1 },
65 	{ 0x4B8, GENMASK(15, 14), 0 },
66 };
67 
68 static struct aspeed_sig_desc i2c5_link[] = {
69 	{ 0x418, GENMASK(17, 16), 0 },
70 };
71 
72 static struct aspeed_sig_desc i2c6_link[] = {
73 	{ 0x418, GENMASK(19, 18), 0 },
74 };
75 
76 static struct aspeed_sig_desc i2c7_link[] = {
77 	{ 0x418, GENMASK(21, 20), 0 },
78 };
79 
80 static struct aspeed_sig_desc i2c8_link[] = {
81 	{ 0x418, GENMASK(23, 22), 0 },
82 };
83 
84 static struct aspeed_sig_desc i2c9_link[] = {
85 	{ 0x418, GENMASK(25, 24), 0 },
86 };
87 
88 static struct aspeed_sig_desc i2c10_link[] = {
89 	{ 0x418, GENMASK(27, 26), 0 },
90 };
91 
92 static struct aspeed_sig_desc i2c11_link[] = {
93 	{ 0x410, GENMASK(1, 0), 1 },
94 	{ 0x4B0, GENMASK(1, 0), 0 },
95 };
96 
97 static struct aspeed_sig_desc i2c12_link[] = {
98 	{ 0x410, GENMASK(3, 2), 1 },
99 	{ 0x4B0, GENMASK(3, 2), 0 },
100 };
101 
102 static struct aspeed_sig_desc i2c13_link[] = {
103 	{ 0x410, GENMASK(5, 4), 1 },
104 	{ 0x4B0, GENMASK(5, 4), 0 },
105 };
106 
107 static struct aspeed_sig_desc i2c14_link[] = {
108 	{ 0x410, GENMASK(7, 6), 1 },
109 	{ 0x4B0, GENMASK(7, 6), 0 },
110 };
111 
112 static struct aspeed_sig_desc i2c15_link[] = {
113 	{ 0x414, GENMASK(29, 28), 1 },
114 	{ 0x4B4, GENMASK(29, 28), 0 },
115 };
116 
117 static struct aspeed_sig_desc i2c16_link[] = {
118 	{ 0x414, GENMASK(31, 30), 1 },
119 	{ 0x4B4, GENMASK(31, 30), 0 },
120 };
121 
122 
123 static struct aspeed_sig_desc mac1_link[] = {
124 	{ 0x410, BIT(4), 0 },
125 #ifndef CONFIG_FPGA_ASPEED
126 	{ 0x470, BIT(4), 1 },
127 #endif
128 };
129 
130 static struct aspeed_sig_desc mac2_link[] = {
131 	{ 0x410, BIT(5), 0	},
132 	{ 0x470, BIT(5), 1 },
133 };
134 
135 static struct aspeed_sig_desc mac3_link[] = {
136 	{ 0x410, BIT(6), 0		},
137 	{ 0x470, BIT(6), 1      	},
138 };
139 
140 static struct aspeed_sig_desc mac4_link[] = {
141 	{ 0x410, BIT(7), 0		},
142 	{ 0x470, BIT(7), 1		},
143 };
144 
145 
146 static struct aspeed_sig_desc rgmii1[] = {
147 #ifndef CONFIG_FPGA_ASPEED
148 	{ 0x500, BIT(6), 0         	},
149 	{ 0x400, GENMASK(11, 0), 0 	},
150 #endif
151 };
152 
153 static struct aspeed_sig_desc rgmii2[] = {
154 	{ 0x500, BIT(7), 0		},
155 	{ 0x400, GENMASK(23, 12), 0 	},
156 };
157 
158 static struct aspeed_sig_desc rgmii3[] = {
159 	{ 0x510, BIT(0), 0         },
160 	{ 0x410, GENMASK(27, 16), 0	},
161 };
162 
163 static struct aspeed_sig_desc rgmii4[] = {
164 	{ 0x510, BIT(1), 0         },
165 	{ 0x410, GENMASK(31, 28), 1	},
166 	{ 0x4b0, GENMASK(31, 28), 0	},
167 	{ 0x474, GENMASK(7, 0), 1	},
168 	{ 0x414, GENMASK(7, 0), 1	},
169 	{ 0x4b4, GENMASK(7, 0), 0	},
170 };
171 
172 static struct aspeed_sig_desc rmii1[] = {
173 	{ 0x504, BIT(6), 0         	},
174 	{ 0x400, GENMASK(3, 0), 0	},
175 	{ 0x400, GENMASK(11, 6), 0	},
176 };
177 
178 static struct aspeed_sig_desc rmii2[] = {
179 	{ 0x504, BIT(7), 0         	},
180 	{ 0x400, GENMASK(15, 12), 0	},
181 	{ 0x400, GENMASK(23, 18), 0	},
182 };
183 
184 static struct aspeed_sig_desc rmii3[] = {
185 	{ 0x514, BIT(0), 0         	},
186 	{ 0x410, GENMASK(27, 22), 0	},
187 	{ 0x410, GENMASK(19, 16), 0	},
188 };
189 
190 static struct aspeed_sig_desc rmii4[] = {
191 	{ 0x514, BIT(1), 0         	},
192 	{ 0x410, GENMASK(7, 2), 1	},
193 	{ 0x410, GENMASK(31, 28), 1	},
194 	{ 0x414, GENMASK(7, 2), 1	},
195 	{ 0x4B0, GENMASK(31, 28), 0	},
196 	{ 0x4B4, GENMASK(7, 2), 0	},
197 };
198 
199 static struct aspeed_sig_desc rmii1_rclk_oe[] = {
200 	{ 0x340, BIT(29), 0         	},
201 };
202 static struct aspeed_sig_desc rmii2_rclk_oe[] = {
203 	{ 0x340, BIT(30), 0         	},
204 };
205 
206 static struct aspeed_sig_desc rmii3_rclk_oe[] = {
207 	{ 0x350, BIT(29), 0         	},
208 };
209 static struct aspeed_sig_desc rmii4_rclk_oe[] = {
210 	{ 0x350, BIT(30), 0         	},
211 };
212 
213 static struct aspeed_sig_desc mdio1_link[] = {
214 	{ 0x430, BIT(17) | BIT(16), 0	},
215 };
216 
217 static struct aspeed_sig_desc mdio2_link[] = {
218 	{ 0x470, BIT(13) | BIT(12), 1	},
219 	{ 0x410, BIT(13) | BIT(12), 0	},
220 };
221 
222 static struct aspeed_sig_desc mdio3_link[] = {
223 	{ 0x470, BIT(1) | BIT(0), 1	},
224 	{ 0x410, BIT(1) | BIT(0), 0	},
225 };
226 
227 static struct aspeed_sig_desc mdio4_link[] = {
228 	{ 0x470, BIT(3) | BIT(2), 1	},
229 	{ 0x410, BIT(3) | BIT(2), 0	},
230 };
231 
232 static struct aspeed_sig_desc sdio2_link[] = {
233 	{ 0x414, GENMASK(23, 16), 1	},
234 	{ 0x4B4, GENMASK(23, 16), 0	},
235 	{ 0x450, BIT(1), 0		},
236 };
237 
238 static struct aspeed_sig_desc sdio1_link[] = {
239 	{ 0x414, GENMASK(15, 8), 0	},
240 };
241 
242 //when sdio1 8bits, sdio2 can't use
243 static struct aspeed_sig_desc sdio1_8bit_link[] = {
244 	{ 0x414, GENMASK(15, 8), 0	},
245 	{ 0x4b4, GENMASK(21, 18), 0	},
246 	{ 0x450, BIT(3), 0	},
247 	{ 0x450, BIT(1), 1	},
248 };
249 
250 static struct aspeed_sig_desc emmc_link[] = {
251 	{ 0x400, GENMASK(31, 24), 0 },
252 };
253 
254 static struct aspeed_sig_desc emmcg8_link[] = {
255 	{ 0x400, GENMASK(31, 24), 0 },
256 	{ 0x404, GENMASK(3, 0), 0 },
257 //because it is strap use 0x4 to clear
258 	{ 0x504, BIT(3), 0 },
259 	{ 0x504, BIT(5), 0 },
260 };
261 
262 static struct aspeed_sig_desc fmcquad_link[] = {
263 	{ 0x438, GENMASK(5, 4), 0 },
264 };
265 
266 static struct aspeed_sig_desc spi1_link[] = {
267 	{ 0x438, GENMASK(13, 11), 0 },
268 };
269 
270 static struct aspeed_sig_desc spi1abr_link[] = {
271 	{ 0x438, BIT(9), 0 },
272 };
273 
274 static struct aspeed_sig_desc spi1cs1_link[] = {
275 	{ 0x438, BIT(8), 0 },
276 };
277 
278 static struct aspeed_sig_desc spi1wp_link[] = {
279 	{ 0x438, BIT(10), 0 },
280 };
281 
282 static struct aspeed_sig_desc spi1quad_link[] = {
283 	{ 0x438, GENMASK(15, 14), 0 },
284 };
285 
286 static struct aspeed_sig_desc spi2_link[] = {
287 	{ 0x434, GENMASK(29, 27) | BIT(24), 0 },
288 };
289 
290 static struct aspeed_sig_desc spi2cs1_link[] = {
291 	{ 0x434, BIT(25), 0 },
292 };
293 
294 static struct aspeed_sig_desc spi2cs2_link[] = {
295 	{ 0x434, BIT(26), 0 },
296 };
297 
298 static struct aspeed_sig_desc spi2quad_link[] = {
299 	{ 0x434, GENMASK(31, 30), 0 },
300 };
301 
302 static struct aspeed_sig_desc fsi1[] = {
303 	{ 0xd48, GENMASK(21, 20), 0 },
304 };
305 
306 static struct aspeed_sig_desc fsi2[] = {
307 	{ 0xd48, GENMASK(23, 22), 0 },
308 };
309 
310 static struct aspeed_sig_desc usb2ad_link[] = {
311 	{ 0x440, BIT(24), 0 },
312 	{ 0x440, BIT(25), 1 },
313 };
314 
315 static struct aspeed_sig_desc usb2ah_link[] = {
316 	{ 0x440, BIT(24), 1 },
317 	{ 0x440, BIT(25), 0 },
318 };
319 
320 static struct aspeed_sig_desc usb2bh_link[] = {
321 	{ 0x440, BIT(28), 1 },
322 	{ 0x440, BIT(29), 0 },
323 };
324 
325 static struct aspeed_sig_desc pcie0rc_link[] = {
326 	{ 0x40, BIT(21), 0 },
327 };
328 
329 static struct aspeed_sig_desc pcie1rc_link[] = {
330 	{ 0x40, BIT(19), 0 },	//SSPRST# output enable
331 	{ 0x500, BIT(24), 0 },	//dedicate rc reset
332 };
333 
334 static const struct aspeed_group_config ast2600_groups[] = {
335 	{ "MAC1LINK", ARRAY_SIZE(mac1_link), mac1_link },
336 	{ "MAC2LINK", ARRAY_SIZE(mac2_link), mac2_link },
337 	{ "MAC3LINK", ARRAY_SIZE(mac3_link), mac3_link },
338 	{ "MAC4LINK", ARRAY_SIZE(mac4_link), mac4_link },
339 	{ "RGMII1", ARRAY_SIZE(rgmii1), rgmii1 },
340 	{ "RGMII2", ARRAY_SIZE(rgmii2), rgmii2 },
341 	{ "RGMII3", ARRAY_SIZE(rgmii3), rgmii3 },
342 	{ "RGMII4", ARRAY_SIZE(rgmii4), rgmii4 },
343 	{ "RMII1", ARRAY_SIZE(rmii1), rmii1 },
344 	{ "RMII2", ARRAY_SIZE(rmii2), rmii2 },
345 	{ "RMII3", ARRAY_SIZE(rmii3), rmii3 },
346 	{ "RMII4", ARRAY_SIZE(rmii4), rmii4 },
347 	{ "RMII1RCLK", ARRAY_SIZE(rmii1_rclk_oe), rmii1_rclk_oe },
348 	{ "RMII2RCLK", ARRAY_SIZE(rmii2_rclk_oe), rmii2_rclk_oe },
349 	{ "RMII3RCLK", ARRAY_SIZE(rmii3_rclk_oe), rmii3_rclk_oe },
350 	{ "RMII4RCLK", ARRAY_SIZE(rmii4_rclk_oe), rmii4_rclk_oe },
351 	{ "MDIO1", ARRAY_SIZE(mdio1_link), mdio1_link },
352 	{ "MDIO2", ARRAY_SIZE(mdio2_link), mdio2_link },
353 	{ "MDIO3", ARRAY_SIZE(mdio3_link), mdio3_link },
354 	{ "MDIO4", ARRAY_SIZE(mdio4_link), mdio4_link },
355 	{ "SD1", ARRAY_SIZE(sdio1_link), sdio1_link },
356 	{ "SD1_8bits", ARRAY_SIZE(sdio1_8bit_link), sdio1_8bit_link },
357 	{ "SD2", ARRAY_SIZE(sdio2_link), sdio2_link },
358 	{ "EMMC", ARRAY_SIZE(emmc_link), emmc_link },
359 	{ "EMMCG8", ARRAY_SIZE(emmcg8_link), emmcg8_link },
360 	{ "FMCQUAD", ARRAY_SIZE(fmcquad_link), fmcquad_link },
361 	{ "SPI1", ARRAY_SIZE(spi1_link), spi1_link },
362 	{ "SPI1ABR", ARRAY_SIZE(spi1abr_link), spi1abr_link },
363 	{ "SPI1CS1", ARRAY_SIZE(spi1cs1_link), spi1cs1_link },
364 	{ "SPI1WP", ARRAY_SIZE(spi1wp_link), spi1wp_link },
365 	{ "SPI1QUAD", ARRAY_SIZE(spi1quad_link), spi1quad_link },
366 	{ "SPI2", ARRAY_SIZE(spi2_link), spi2_link },
367 	{ "SPI2CS1", ARRAY_SIZE(spi2cs1_link), spi2cs1_link },
368 	{ "SPI2CS2", ARRAY_SIZE(spi2cs2_link), spi2cs2_link },
369 	{ "SPI2QUAD", ARRAY_SIZE(spi2quad_link), spi2quad_link },
370 	{ "I2C1", ARRAY_SIZE(i2c1_link), i2c1_link },
371 	{ "I2C2", ARRAY_SIZE(i2c2_link), i2c2_link },
372 	{ "I2C3", ARRAY_SIZE(i2c3_link), i2c3_link },
373 	{ "I2C4", ARRAY_SIZE(i2c4_link), i2c4_link },
374 	{ "I2C5", ARRAY_SIZE(i2c5_link), i2c5_link },
375 	{ "I2C6", ARRAY_SIZE(i2c6_link), i2c6_link },
376 	{ "I2C7", ARRAY_SIZE(i2c7_link), i2c7_link },
377 	{ "I2C8", ARRAY_SIZE(i2c8_link), i2c8_link },
378 	{ "I2C9", ARRAY_SIZE(i2c9_link), i2c9_link },
379 	{ "I2C10", ARRAY_SIZE(i2c10_link), i2c10_link },
380 	{ "I2C11", ARRAY_SIZE(i2c11_link), i2c11_link },
381 	{ "I2C12", ARRAY_SIZE(i2c12_link), i2c12_link },
382 	{ "I2C13", ARRAY_SIZE(i2c13_link), i2c13_link },
383 	{ "I2C14", ARRAY_SIZE(i2c14_link), i2c14_link },
384 	{ "I2C15", ARRAY_SIZE(i2c15_link), i2c15_link },
385 	{ "I2C16", ARRAY_SIZE(i2c16_link), i2c16_link },
386 	{ "FSI1", ARRAY_SIZE(fsi1), fsi1 },
387 	{ "FSI2", ARRAY_SIZE(fsi2), fsi2 },
388 	{ "USB2AD", ARRAY_SIZE(usb2ad_link), usb2ad_link },
389 	{ "USB2AH", ARRAY_SIZE(usb2ah_link), usb2ah_link },
390 	{ "USB2BH", ARRAY_SIZE(usb2bh_link), usb2bh_link },
391 	{ "PCIE0RC", ARRAY_SIZE(pcie0rc_link), pcie0rc_link },
392 	{ "PCIE1RC", ARRAY_SIZE(pcie1rc_link), pcie1rc_link },
393 };
394 
395 static int ast2600_pinctrl_get_groups_count(struct udevice *dev)
396 {
397 	debug("PINCTRL: get_(functions/groups)_count\n");
398 
399 	return ARRAY_SIZE(ast2600_groups);
400 }
401 
402 static const char *ast2600_pinctrl_get_group_name(struct udevice *dev,
403 						  unsigned selector)
404 {
405 	debug("PINCTRL: get_(function/group)_name %u\n", selector);
406 
407 	return ast2600_groups[selector].group_name;
408 }
409 
410 static int ast2600_pinctrl_group_set(struct udevice *dev, unsigned selector,
411 				     unsigned func_selector)
412 {
413 	struct ast2600_pinctrl_priv *priv = dev_get_priv(dev);
414 	const struct aspeed_group_config *config;
415 	const struct aspeed_sig_desc *descs;
416 	u32 ctrl_reg = (u32)priv->scu;
417 	u32 i;
418 
419 	debug("PINCTRL: group_set <%u, %u>\n", selector, func_selector);
420 	if (selector >= ARRAY_SIZE(ast2600_groups))
421 		return -EINVAL;
422 
423 	config = &ast2600_groups[selector];
424 	for (i = 0; i < config->ndescs; i++) {
425 		descs = &config->descs[i];
426 		if (descs->clr) {
427 			clrbits_le32((u32)ctrl_reg + descs->offset,
428 				     descs->reg_set);
429 		} else {
430 			setbits_le32((u32)ctrl_reg + descs->offset,
431 				     descs->reg_set);
432 		}
433 	}
434 
435 	return 0;
436 }
437 
438 static struct pinctrl_ops ast2600_pinctrl_ops = {
439 	.set_state = pinctrl_generic_set_state,
440 	.get_groups_count = ast2600_pinctrl_get_groups_count,
441 	.get_group_name = ast2600_pinctrl_get_group_name,
442 	.get_functions_count = ast2600_pinctrl_get_groups_count,
443 	.get_function_name = ast2600_pinctrl_get_group_name,
444 	.pinmux_group_set = ast2600_pinctrl_group_set,
445 };
446 
447 static const struct udevice_id ast2600_pinctrl_ids[] = {
448 	{ .compatible = "aspeed,g6-pinctrl" },
449 	{ }
450 };
451 
452 U_BOOT_DRIVER(pinctrl_aspeed) = {
453 	.name = "aspeed_ast2600_pinctrl",
454 	.id = UCLASS_PINCTRL,
455 	.of_match = ast2600_pinctrl_ids,
456 	.priv_auto_alloc_size = sizeof(struct ast2600_pinctrl_priv),
457 	.ops = &ast2600_pinctrl_ops,
458 	.probe = ast2600_pinctrl_probe,
459 };
460