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 //8bit mode offset 0x414 (21~18) 0x450 bit0: sd0 bit1: sd1,  bit3: sd0 8bits
81 
82 
83 static struct aspeed_sig_desc sdio2_link[] = {
84 	{ 0x4B4, GENMASK(23, 16), 0	},
85 	{ 0x450, BIT(1), 0		},
86 	{ 0x414, GENMASK(23, 16), 0	},
87 };
88 
89 //sdio1 414 (23~16) = 0, 4b4 (23~16) = 1, 450 bit1 = 1
90 static struct aspeed_sig_desc sdio1_link[] = {
91 	{ 0x414, GENMASK(15, 8), 0	},
92 	{ 0x4b4, GENMASK(23, 16), 0	},
93 	{ 0x450, BIT(0), 0	},
94 };
95 
96 static struct aspeed_sig_desc emmc_link[] = {
97 	{ 0x400, GENMASK(31, 24), 0 },
98 	{ 0x404, GENMASK(3, 0), 0 },
99 };
100 
101 static const struct aspeed_group_config ast2600_groups[] = {
102 	{ "MAC1LINK", 1, mac1_link },
103 	{ "MAC2LINK", 1, mac2_link },
104 	{ "MAC3LINK", 1, mac3_link },
105 	{ "MAC4LINK", 1, mac4_link },
106 	{ "MDIO1", 1, mdio1_link },
107 	{ "MDIO2", 1, mdio2_link },
108 	{ "MDIO3", 1, mdio3_link },
109 	{ "MDIO4", 1, mdio4_link },
110 	{ "SDIO1", ARRAY_SIZE(sdio2_link), sdio2_link },
111 	{ "SDIO0", ARRAY_SIZE(sdio1_link), sdio1_link },
112 	{ "EMMC", ARRAY_SIZE(emmc_link), emmc_link },
113 };
114 
115 static int ast2600_pinctrl_get_groups_count(struct udevice *dev)
116 {
117 	debug("PINCTRL: get_(functions/groups)_count\n");
118 
119 	return ARRAY_SIZE(ast2600_groups);
120 }
121 
122 static const char *ast2600_pinctrl_get_group_name(struct udevice *dev,
123 						  unsigned selector)
124 {
125 	debug("PINCTRL: get_(function/group)_name %u\n", selector);
126 
127 	return ast2600_groups[selector].group_name;
128 }
129 
130 static int ast2600_pinctrl_group_set(struct udevice *dev, unsigned selector,
131 				     unsigned func_selector)
132 {
133 	struct ast2600_pinctrl_priv *priv = dev_get_priv(dev);
134 	const struct aspeed_group_config *config;
135 	const struct aspeed_sig_desc *descs;
136 	u32 ctrl_reg = (u32)&priv->scu;
137 
138 	debug("PINCTRL: group_set <%u, %u>\n", selector, func_selector);
139 	if (selector >= ARRAY_SIZE(ast2600_groups))
140 		return -EINVAL;
141 
142 	config = &ast2600_groups[selector];
143 	descs = config->descs;
144 
145 	setbits_le32(ctrl_reg + descs->offset, descs->reg_set);
146 
147 	return 0;
148 }
149 
150 static struct pinctrl_ops ast2600_pinctrl_ops = {
151 	.set_state = pinctrl_generic_set_state,
152 	.get_groups_count = ast2600_pinctrl_get_groups_count,
153 	.get_group_name = ast2600_pinctrl_get_group_name,
154 	.get_functions_count = ast2600_pinctrl_get_groups_count,
155 	.get_function_name = ast2600_pinctrl_get_group_name,
156 	.pinmux_group_set = ast2600_pinctrl_group_set,
157 };
158 
159 static const struct udevice_id ast2600_pinctrl_ids[] = {
160 	{ .compatible = "aspeed,g6-pinctrl" },
161 	{ }
162 };
163 
164 U_BOOT_DRIVER(pinctrl_aspeed) = {
165 	.name = "aspeed_ast2600_pinctrl",
166 	.id = UCLASS_PINCTRL,
167 	.of_match = ast2600_pinctrl_ids,
168 	.priv_auto_alloc_size = sizeof(struct ast2600_pinctrl_priv),
169 	.ops = &ast2600_pinctrl_ops,
170 	.probe = ast2600_pinctrl_probe,
171 };
172