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 mac1_link[] = {
49 	{ 0x410, BIT(4), 0	},
50 };
51 
52 static struct aspeed_sig_desc mac2_link[] = {
53 	{ 0x410, BIT(5), 0	},
54 };
55 
56 static struct aspeed_sig_desc mac3_link[] = {
57 	{ 0x410, BIT(6), 0	},
58 };
59 
60 static struct aspeed_sig_desc mac4_link[] = {
61 	{ 0x410, BIT(7), 0	},
62 };
63 
64 static struct aspeed_sig_desc mdio1_link[] = {
65 	{ 0x430, BIT(17), 0	},
66 };
67 
68 static struct aspeed_sig_desc mdio2_link[] = {
69 	{ 0x410, BIT(14) | BIT(13), 0	},
70 };
71 
72 static struct aspeed_sig_desc mdio3_link[] = {
73 	{ 0x410, BIT(1) | BIT(0), 0	},
74 };
75 
76 static struct aspeed_sig_desc mdio4_link[] = {
77 	{ 0x410, BIT(3) | BIT(2), 0	},
78 };
79 
80 static struct aspeed_sig_desc sdio2_link[] = {
81 	{ 0x414, GENMASK(23, 16), 1	},
82 	{ 0x4B4, GENMASK(23, 16), 0	},
83 	{ 0x450, BIT(1), 0		},
84 };
85 
86 static struct aspeed_sig_desc sdio1_link[] = {
87 	{ 0x414, GENMASK(15, 8), 0	},
88 };
89 
90 //when sdio1 8bits, sdio2 can't use
91 static struct aspeed_sig_desc sdio1_8bit_link[] = {
92 	{ 0x414, GENMASK(15, 8), 0	},
93 	{ 0x4b4, GENMASK(21, 18), 0	},
94 	{ 0x450, BIT(3), 0	},
95 	{ 0x450, BIT(1), 1	},
96 };
97 
98 static struct aspeed_sig_desc emmc_link[] = {
99 	{ 0x400, GENMASK(31, 24), 0 },
100 	{ 0x404, GENMASK(3, 0), 0 },
101 	{ 0x500, BIT(3), 1 },
102 	{ 0x500, BIT(5), 1 },
103 };
104 
105 static const struct aspeed_group_config ast2600_groups[] = {
106 	{ "MAC1LINK", 1, mac1_link },
107 	{ "MAC2LINK", 1, mac2_link },
108 	{ "MAC3LINK", 1, mac3_link },
109 	{ "MAC4LINK", 1, mac4_link },
110 	{ "MDIO1", 1, mdio1_link },
111 	{ "MDIO2", 1, mdio2_link },
112 	{ "MDIO3", 1, mdio3_link },
113 	{ "MDIO4", 1, mdio4_link },
114 	{ "SD1", ARRAY_SIZE(sdio1_link), sdio1_link },
115 	{ "SD1_8bits", ARRAY_SIZE(sdio1_8bit_link), sdio1_8bit_link },
116 	{ "SD2", ARRAY_SIZE(sdio2_link), sdio2_link },
117 	{ "EMMC", ARRAY_SIZE(emmc_link), emmc_link },
118 };
119 
120 static int ast2600_pinctrl_get_groups_count(struct udevice *dev)
121 {
122 	debug("PINCTRL: get_(functions/groups)_count\n");
123 
124 	return ARRAY_SIZE(ast2600_groups);
125 }
126 
127 static const char *ast2600_pinctrl_get_group_name(struct udevice *dev,
128 						  unsigned selector)
129 {
130 	debug("PINCTRL: get_(function/group)_name %u\n", selector);
131 
132 	return ast2600_groups[selector].group_name;
133 }
134 
135 static int ast2600_pinctrl_group_set(struct udevice *dev, unsigned selector,
136 				     unsigned func_selector)
137 {
138 	struct ast2600_pinctrl_priv *priv = dev_get_priv(dev);
139 	const struct aspeed_group_config *config;
140 	const struct aspeed_sig_desc *descs;
141 	u32 ctrl_reg = (u32)&priv->scu;
142 
143 	debug("PINCTRL: group_set <%u, %u>\n", selector, func_selector);
144 	if (selector >= ARRAY_SIZE(ast2600_groups))
145 		return -EINVAL;
146 
147 	config = &ast2600_groups[selector];
148 	descs = config->descs;
149 
150 	setbits_le32(ctrl_reg + descs->offset, descs->reg_set);
151 
152 	return 0;
153 }
154 
155 static struct pinctrl_ops ast2600_pinctrl_ops = {
156 	.set_state = pinctrl_generic_set_state,
157 	.get_groups_count = ast2600_pinctrl_get_groups_count,
158 	.get_group_name = ast2600_pinctrl_get_group_name,
159 	.get_functions_count = ast2600_pinctrl_get_groups_count,
160 	.get_function_name = ast2600_pinctrl_get_group_name,
161 	.pinmux_group_set = ast2600_pinctrl_group_set,
162 };
163 
164 static const struct udevice_id ast2600_pinctrl_ids[] = {
165 	{ .compatible = "aspeed,g6-pinctrl" },
166 	{ }
167 };
168 
169 U_BOOT_DRIVER(pinctrl_aspeed) = {
170 	.name = "aspeed_ast2600_pinctrl",
171 	.id = UCLASS_PINCTRL,
172 	.of_match = ast2600_pinctrl_ids,
173 	.priv_auto_alloc_size = sizeof(struct ast2600_pinctrl_priv),
174 	.ops = &ast2600_pinctrl_ops,
175 	.probe = ast2600_pinctrl_probe,
176 };
177