1
2
3
4
5
6
7
8
9#include <common.h>
10#include <bitfield.h>
11#include <dm.h>
12#include <errno.h>
13#include <regmap.h>
14#include <syscon.h>
15#include <asm/global_data.h>
16#include <asm/io.h>
17#include <dm/pinctrl.h>
18#include <linux/bug.h>
19#include <linux/libfdt.h>
20
21DECLARE_GLOBAL_DATA_PTR;
22
23#define MAX_STI_PINCONF_ENTRIES 7
24
25#define OE (1 << 27)
26
27#define PU (1 << 26)
28
29#define OD (1 << 25)
30
31
32
33#define IN (0)
34
35#define IN_PU (PU)
36
37#define OUT (OE)
38
39#define OUT_PU (OE | PU)
40
41#define BIDIR (OE | OD)
42
43#define BIDIR_PU (OE | PU | OD)
44
45struct sti_pinctrl_plat {
46 struct regmap *regmap;
47};
48
49struct sti_pin_desc {
50 unsigned char bank;
51 unsigned char pin;
52 unsigned char alt;
53 int dir;
54};
55
56
57
58
59void sti_alternate_select(struct udevice *dev, struct sti_pin_desc *pin_desc)
60{
61 struct sti_pinctrl_plat *plat = dev_get_plat(dev);
62 unsigned long sysconf, *sysconfreg;
63 int alt = pin_desc->alt;
64 int bank = pin_desc->bank;
65 int pin = pin_desc->pin;
66
67 sysconfreg = (unsigned long *)plat->regmap->ranges[0].start;
68
69 switch (bank) {
70 case 0 ... 5:
71 sysconfreg += bank;
72 break;
73 case 10 ... 20:
74 sysconfreg += bank - 10;
75 break;
76 case 30 ... 35:
77 sysconfreg += bank - 30;
78 break;
79 case 40 ... 42:
80 sysconfreg += bank - 40;
81 break;
82 default:
83 BUG();
84 return;
85 }
86
87 sysconf = readl(sysconfreg);
88 sysconf = bitfield_replace(sysconf, pin * 4, 3, alt);
89 writel(sysconf, sysconfreg);
90}
91
92
93void sti_pin_configure(struct udevice *dev, struct sti_pin_desc *pin_desc)
94{
95 struct sti_pinctrl_plat *plat = dev_get_plat(dev);
96 int bit;
97 int oe = 0, pu = 0, od = 0;
98 unsigned long *sysconfreg;
99 int bank = pin_desc->bank;
100
101 sysconfreg = (unsigned long *)plat->regmap->ranges[0].start + 40;
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127 int flashss = 0;
128
129 switch (pin_desc->dir) {
130 case IN:
131 oe = 0; pu = 0; od = 0;
132 break;
133 case IN_PU:
134 oe = 0; pu = 1; od = 0;
135 break;
136 case OUT:
137 oe = 1; pu = 0; od = 0;
138 break;
139 case BIDIR:
140 oe = 1; pu = 0; od = 1;
141 break;
142 case BIDIR_PU:
143 oe = 1; pu = 1; od = 1;
144 break;
145
146 default:
147 pr_err("%s invalid direction value: 0x%x\n",
148 __func__, pin_desc->dir);
149 BUG();
150 break;
151 }
152
153 switch (bank) {
154 case 0 ... 5:
155 sysconfreg += bank / 4;
156 break;
157 case 10 ... 20:
158 bank -= 10;
159 sysconfreg += bank / 4;
160 break;
161 case 30 ... 35:
162 bank -= 30;
163 sysconfreg += bank / 4;
164 break;
165 case 40 ... 42:
166 bank -= 40;
167 sysconfreg += bank / 4;
168 flashss = 1;
169 break;
170 default:
171 BUG();
172 return;
173 }
174
175 bit = ((bank * 8) + pin_desc->pin) % 32;
176
177
178
179
180
181 if (!flashss) {
182 if (oe)
183 generic_set_bit(bit, sysconfreg);
184 else
185 generic_clear_bit(bit, sysconfreg);
186 }
187
188 sysconfreg += 10;
189
190
191
192
193
194
195 if (!flashss) {
196 if (pu)
197 generic_set_bit(bit, sysconfreg);
198 else
199 generic_clear_bit(bit, sysconfreg);
200 }
201
202 sysconfreg += 10;
203
204
205 if (od)
206 generic_set_bit(bit, sysconfreg);
207 else
208 generic_clear_bit(bit, sysconfreg);
209}
210
211
212static int sti_pinctrl_set_state(struct udevice *dev, struct udevice *config)
213{
214 struct fdtdec_phandle_args args;
215 const void *blob = gd->fdt_blob;
216 const char *prop_name;
217 int node = dev_of_offset(config);
218 int property_offset, prop_len;
219 int pinconf_node, ret, count;
220 const char *bank_name;
221 u32 cells[MAX_STI_PINCONF_ENTRIES];
222
223 struct sti_pin_desc pin_desc;
224
225
226 pinconf_node = fdt_subnode_offset(blob, node, "st,pins");
227
228
229
230
231
232
233 fdt_for_each_property_offset(property_offset, blob, pinconf_node) {
234 fdt_getprop_by_offset(blob, property_offset, &prop_name,
235 &prop_len);
236
237
238 ret = fdtdec_parse_phandle_with_args(blob, pinconf_node,
239 prop_name, "#gpio-cells",
240 0, 0, &args);
241 if (ret < 0) {
242 pr_err("Can't get the gpio bank phandle: %d\n", ret);
243 return ret;
244 }
245
246 bank_name = fdt_getprop(blob, args.node, "st,bank-name",
247 &count);
248 if (count < 0) {
249 pr_err("Can't find bank-name property %d\n", count);
250 return -EINVAL;
251 }
252
253 pin_desc.bank = trailing_strtoln(bank_name, NULL);
254
255 count = fdtdec_get_int_array_count(blob, pinconf_node,
256 prop_name, cells,
257 ARRAY_SIZE(cells));
258 if (count < 0) {
259 pr_err("Bad pin configuration array %d\n", count);
260 return -EINVAL;
261 }
262
263 if (count > MAX_STI_PINCONF_ENTRIES) {
264 pr_err("Unsupported pinconf array count %d\n", count);
265 return -EINVAL;
266 }
267
268 pin_desc.pin = cells[1];
269 pin_desc.alt = cells[2];
270 pin_desc.dir = cells[3];
271
272 sti_alternate_select(dev, &pin_desc);
273 sti_pin_configure(dev, &pin_desc);
274 };
275
276 return 0;
277}
278
279static int sti_pinctrl_probe(struct udevice *dev)
280{
281 struct sti_pinctrl_plat *plat = dev_get_plat(dev);
282 struct udevice *syscon;
283 int err;
284
285
286 err = uclass_get_device_by_phandle(UCLASS_SYSCON, dev,
287 "st,syscfg", &syscon);
288 if (err) {
289 pr_err("unable to find syscon device\n");
290 return err;
291 }
292
293 plat->regmap = syscon_get_regmap(syscon);
294 if (!plat->regmap) {
295 pr_err("unable to find regmap\n");
296 return -ENODEV;
297 }
298
299 return 0;
300}
301
302static const struct udevice_id sti_pinctrl_ids[] = {
303 { .compatible = "st,stih407-sbc-pinctrl" },
304 { .compatible = "st,stih407-front-pinctrl" },
305 { .compatible = "st,stih407-rear-pinctrl" },
306 { .compatible = "st,stih407-flash-pinctrl" },
307 { }
308};
309
310const struct pinctrl_ops sti_pinctrl_ops = {
311 .set_state = sti_pinctrl_set_state,
312};
313
314U_BOOT_DRIVER(pinctrl_sti) = {
315 .name = "pinctrl_sti",
316 .id = UCLASS_PINCTRL,
317 .of_match = sti_pinctrl_ids,
318 .ops = &sti_pinctrl_ops,
319 .probe = sti_pinctrl_probe,
320 .plat_auto = sizeof(struct sti_pinctrl_plat),
321 .ops = &sti_pinctrl_ops,
322};
323