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 /*
17  * This driver works with very simple configuration that has the same name
18  * for group and function. This way it is compatible with the Linux Kernel
19  * driver.
20  */
21 
22 struct ast2600_pinctrl_priv {
23 	struct ast2600_scu *scu;
24 };
25 
26 static int ast2600_pinctrl_probe(struct udevice *dev)
27 {
28 	struct ast2600_pinctrl_priv *priv = dev_get_priv(dev);
29 	struct udevice *clk_dev;
30 	int ret = 0;
31 
32 	/* find SCU base address from clock device */
33 	ret = uclass_get_device_by_driver(UCLASS_CLK, DM_GET_DRIVER(aspeed_scu),
34                                           &clk_dev);
35     if (ret) {
36             debug("clock device not found\n");
37             return ret;
38     }
39 
40 	priv->scu = devfdt_get_addr_ptr(clk_dev);
41 	if (IS_ERR(priv->scu)) {
42 	        debug("%s(): can't get SCU\n", __func__);
43 	        return PTR_ERR(priv->scu);
44 	}
45 
46 	return 0;
47 }
48 
49 static struct aspeed_sig_desc mac1_link[] = {
50 	{ 0x410, BIT(4), 0	},
51 };
52 
53 static struct aspeed_sig_desc mac2_link[] = {
54 	{ 0x410, BIT(5), 0	},
55 };
56 
57 static struct aspeed_sig_desc mac3_link[] = {
58 	{ 0x410, BIT(6), 0	},
59 };
60 
61 static struct aspeed_sig_desc mac4_link[] = {
62 	{ 0x410, BIT(7), 0	},
63 };
64 
65 static struct aspeed_sig_desc mdio1_link[] = {
66 	{ 0x430, BIT(17), 0	},
67 };
68 
69 static struct aspeed_sig_desc mdio2_link[] = {
70 	{ 0x410, BIT(14) | BIT(13), 0	},
71 };
72 
73 static struct aspeed_sig_desc mdio3_link[] = {
74 	{ 0x410, BIT(1) | BIT(0), 0	},
75 };
76 
77 static struct aspeed_sig_desc mdio4_link[] = {
78 	{ 0x410, BIT(3) | BIT(2), 0	},
79 };
80 
81 //8bit mode offset 0x414 (21~18) 0x450 bit0: sd0 bit1: sd1,  bit3: sd0 8bits
82 
83 
84 static struct aspeed_sig_desc sdio2_link[] = {
85 	{ 0x4B4, GENMASK(23, 16), 0	},
86 	{ 0x450, BIT(1), 0		},
87 	{ 0x414, GENMASK(23, 16), 0	},
88 };
89 
90 //sdio1 414 (23~16) = 0, 4b4 (23~16) = 1, 450 bit1 = 1
91 static struct aspeed_sig_desc sdio1_link[] = {
92 	{ 0x414, GENMASK(15, 8), 0	},
93 	{ 0x4b4, GENMASK(23, 16), 0	},
94 	{ 0x450, BIT(0), 0	},
95 };
96 
97 static struct aspeed_sig_desc emmc_link[] = {
98 	{ 0x400, GENMASK(31, 24), 0 },
99 	{ 0x404, GENMASK(3, 0), 0 },
100 };
101 
102 static const struct aspeed_group_config ast2600_groups[] = {
103 	{ "MAC1LINK", 1, mac1_link },
104 	{ "MAC2LINK", 1, mac2_link },
105 	{ "MAC3LINK", 1, mac3_link },
106 	{ "MAC4LINK", 1, mac4_link },
107 	{ "MDIO1LINK", 1, mdio1_link },
108 	{ "MDIO2LINK", 1, mdio2_link },
109 	{ "MDIO3LINK", 1, mdio3_link },
110 	{ "MDIO4LINK", 1, mdio4_link },
111 	{ "SDIO1", ARRAY_SIZE(sdio2_link), sdio2_link },
112 	{ "SDIO0", ARRAY_SIZE(sdio1_link), sdio1_link },
113 	{ "EMMC", ARRAY_SIZE(emmc_link), emmc_link },
114 };
115 
116 static int ast2600_pinctrl_get_groups_count(struct udevice *dev)
117 {
118 	printf("PINCTRL: get_(functions/groups)_count\n");
119 
120 	return ARRAY_SIZE(ast2600_groups);
121 }
122 
123 static const char *ast2600_pinctrl_get_group_name(struct udevice *dev,
124 						  unsigned selector)
125 {
126 	printf("PINCTRL: get_(function/group)_name %u\n", selector);
127 
128 	return ast2600_groups[selector].group_name;
129 }
130 
131 static int ast2600_pinctrl_group_set(struct udevice *dev, unsigned selector,
132 				     unsigned func_selector)
133 {
134 	struct ast2600_pinctrl_priv *priv = dev_get_priv(dev);
135 	const struct aspeed_group_config *config;
136 	const struct aspeed_sig_desc *descs;
137 	u32 ctrl_reg = (u32)&priv->scu;
138 
139 	printf("PINCTRL: group_set <%u, %u>\n", selector, func_selector);
140 	if (selector >= ARRAY_SIZE(ast2600_groups))
141 		return -EINVAL;
142 
143 	config = &ast2600_groups[selector];
144 	descs = config->descs;
145 
146 	setbits_le32(ctrl_reg + descs->offset, descs->reg_set);
147 
148 	return 0;
149 }
150 
151 static struct pinctrl_ops ast2600_pinctrl_ops = {
152 	.set_state = pinctrl_generic_set_state,
153 	.get_groups_count = ast2600_pinctrl_get_groups_count,
154 	.get_group_name = ast2600_pinctrl_get_group_name,
155 	.get_functions_count = ast2600_pinctrl_get_groups_count,
156 	.get_function_name = ast2600_pinctrl_get_group_name,
157 	.pinmux_group_set = ast2600_pinctrl_group_set,
158 };
159 
160 static const struct udevice_id ast2600_pinctrl_ids[] = {
161 	{ .compatible = "aspeed,g6-pinctrl" },
162 	{ }
163 };
164 
165 U_BOOT_DRIVER(pinctrl_aspeed) = {
166 	.name = "aspeed_ast2600_pinctrl",
167 	.id = UCLASS_PINCTRL,
168 	.of_match = ast2600_pinctrl_ids,
169 	.priv_auto_alloc_size = sizeof(struct ast2600_pinctrl_priv),
170 	.ops = &ast2600_pinctrl_ops,
171 	.probe = ast2600_pinctrl_probe,
172 };
173