blob: eafb65496a3d77c94f72a8a459a81c90e8a1f4d6 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Masahiro Yamada847e618b82015-09-11 20:17:32 +09002/*
Masahiro Yamadafa1f73f2016-07-19 21:56:13 +09003 * Copyright (C) 2015-2016 Socionext Inc.
4 * Author: Masahiro Yamada <yamada.masahiro@socionext.com>
Masahiro Yamada847e618b82015-09-11 20:17:32 +09005 */
6
Simon Glass11c89f32017-05-17 17:18:03 -06007#include <dm.h>
Simon Glass9bc15642020-02-03 07:36:16 -07008#include <dm/device_compat.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -06009#include <linux/bitops.h>
Simon Glassc06c1be2020-05-10 11:40:08 -060010#include <linux/bug.h>
Masahiro Yamada847e618b82015-09-11 20:17:32 +090011#include <linux/io.h>
12#include <linux/err.h>
Masahiro Yamada165c5582017-10-13 19:21:54 +090013#include <linux/kernel.h>
Masahiro Yamadafba3a662016-03-24 22:32:43 +090014#include <linux/sizes.h>
Masahiro Yamada847e618b82015-09-11 20:17:32 +090015#include <dm/pinctrl.h>
16
17#include "pinctrl-uniphier.h"
18
Masahiro Yamadaa9945202016-09-14 01:06:05 +090019#define UNIPHIER_PINCTRL_PINMUX_BASE 0x1000
20#define UNIPHIER_PINCTRL_LOAD_PINMUX 0x1700
Masahiro Yamadae5299ed2018-05-05 19:53:55 +090021#define UNIPHIER_PINCTRL_DRVCTRL_BASE 0x1800
22#define UNIPHIER_PINCTRL_DRV2CTRL_BASE 0x1900
23#define UNIPHIER_PINCTRL_DRV3CTRL_BASE 0x1980
Masahiro Yamada140e9f12017-02-12 18:21:15 +090024#define UNIPHIER_PINCTRL_PUPDCTRL_BASE 0x1a00
Masahiro Yamadaa9945202016-09-14 01:06:05 +090025#define UNIPHIER_PINCTRL_IECTRL 0x1d00
26
Masahiro Yamada65ef4f72016-06-29 19:39:00 +090027static const char *uniphier_pinctrl_dummy_name = "_dummy";
28
Masahiro Yamada914b5712018-05-05 19:53:54 +090029static int uniphier_pinctrl_get_pins_count(struct udevice *dev)
30{
31 struct uniphier_pinctrl_priv *priv = dev_get_priv(dev);
32 const struct uniphier_pinctrl_pin *pins = priv->socdata->pins;
33 int pins_count = priv->socdata->pins_count;
34
Dai Okamura826ffbc2022-12-09 20:33:46 +090035 if (WARN_ON(!pins_count))
36 return 0; /* no table of pins */
37
Masahiro Yamada914b5712018-05-05 19:53:54 +090038 /*
39 * We do not list all pins in the pin table to save memory footprint.
40 * Report the max pin number + 1 to fake the framework.
41 */
42 return pins[pins_count - 1].number + 1;
43}
44
45static const char *uniphier_pinctrl_get_pin_name(struct udevice *dev,
46 unsigned int selector)
47{
48 struct uniphier_pinctrl_priv *priv = dev_get_priv(dev);
49 const struct uniphier_pinctrl_pin *pins = priv->socdata->pins;
50 int pins_count = priv->socdata->pins_count;
51 int i;
52
53 for (i = 0; i < pins_count; i++)
54 if (pins[i].number == selector)
55 return pins[i].name;
56
57 return uniphier_pinctrl_dummy_name;
58}
59
Masahiro Yamada847e618b82015-09-11 20:17:32 +090060static int uniphier_pinctrl_get_groups_count(struct udevice *dev)
61{
62 struct uniphier_pinctrl_priv *priv = dev_get_priv(dev);
63
64 return priv->socdata->groups_count;
65}
66
67static const char *uniphier_pinctrl_get_group_name(struct udevice *dev,
68 unsigned selector)
69{
70 struct uniphier_pinctrl_priv *priv = dev_get_priv(dev);
71
Masahiro Yamada65ef4f72016-06-29 19:39:00 +090072 if (!priv->socdata->groups[selector].name)
73 return uniphier_pinctrl_dummy_name;
74
Masahiro Yamada847e618b82015-09-11 20:17:32 +090075 return priv->socdata->groups[selector].name;
76}
77
78static int uniphier_pinmux_get_functions_count(struct udevice *dev)
79{
80 struct uniphier_pinctrl_priv *priv = dev_get_priv(dev);
81
82 return priv->socdata->functions_count;
83}
84
85static const char *uniphier_pinmux_get_function_name(struct udevice *dev,
86 unsigned selector)
87{
88 struct uniphier_pinctrl_priv *priv = dev_get_priv(dev);
89
Masahiro Yamada65ef4f72016-06-29 19:39:00 +090090 if (!priv->socdata->functions[selector])
91 return uniphier_pinctrl_dummy_name;
92
Masahiro Yamada847e618b82015-09-11 20:17:32 +090093 return priv->socdata->functions[selector];
94}
95
Masahiro Yamada140e9f12017-02-12 18:21:15 +090096static int uniphier_pinconf_input_enable_perpin(struct udevice *dev,
97 unsigned int pin, int enable)
Masahiro Yamada73e67752016-03-24 22:32:45 +090098{
99 struct uniphier_pinctrl_priv *priv = dev_get_priv(dev);
100 unsigned reg;
101 u32 mask, tmp;
102
103 reg = UNIPHIER_PINCTRL_IECTRL + pin / 32 * 4;
104 mask = BIT(pin % 32);
105
106 tmp = readl(priv->base + reg);
Masahiro Yamada140e9f12017-02-12 18:21:15 +0900107 if (enable)
108 tmp |= mask;
109 else
110 tmp &= ~mask;
Masahiro Yamada73e67752016-03-24 22:32:45 +0900111 writel(tmp, priv->base + reg);
Masahiro Yamada140e9f12017-02-12 18:21:15 +0900112
113 return 0;
Masahiro Yamada73e67752016-03-24 22:32:45 +0900114}
115
Masahiro Yamada140e9f12017-02-12 18:21:15 +0900116static int uniphier_pinconf_input_enable_legacy(struct udevice *dev,
117 unsigned int pin, int enable)
Masahiro Yamada847e618b82015-09-11 20:17:32 +0900118{
119 struct uniphier_pinctrl_priv *priv = dev_get_priv(dev);
Masahiro Yamada847e618b82015-09-11 20:17:32 +0900120
Masahiro Yamada140e9f12017-02-12 18:21:15 +0900121 /*
122 * Multiple pins share one input enable, per-pin disabling is
123 * impossible.
124 */
125 if (!enable)
126 return -EINVAL;
127
Masahiro Yamada165c5582017-10-13 19:21:54 +0900128 /* Set all bits instead of having a bunch of pin data */
129 writel(U32_MAX, priv->base + UNIPHIER_PINCTRL_IECTRL);
Masahiro Yamada140e9f12017-02-12 18:21:15 +0900130
131 return 0;
Masahiro Yamada847e618b82015-09-11 20:17:32 +0900132}
133
Masahiro Yamada140e9f12017-02-12 18:21:15 +0900134static int uniphier_pinconf_input_enable(struct udevice *dev,
135 unsigned int pin, int enable)
Masahiro Yamada73e67752016-03-24 22:32:45 +0900136{
137 struct uniphier_pinctrl_priv *priv = dev_get_priv(dev);
138
139 if (priv->socdata->caps & UNIPHIER_PINCTRL_CAPS_PERPIN_IECTRL)
Masahiro Yamada140e9f12017-02-12 18:21:15 +0900140 return uniphier_pinconf_input_enable_perpin(dev, pin, enable);
Masahiro Yamada73e67752016-03-24 22:32:45 +0900141 else
Masahiro Yamada140e9f12017-02-12 18:21:15 +0900142 return uniphier_pinconf_input_enable_legacy(dev, pin, enable);
Masahiro Yamada73e67752016-03-24 22:32:45 +0900143}
144
Masahiro Yamada140e9f12017-02-12 18:21:15 +0900145#if CONFIG_IS_ENABLED(PINCONF)
146
147static const struct pinconf_param uniphier_pinconf_params[] = {
148 { "bias-disable", PIN_CONFIG_BIAS_DISABLE, 0 },
149 { "bias-pull-up", PIN_CONFIG_BIAS_PULL_UP, 1 },
150 { "bias-pull-down", PIN_CONFIG_BIAS_PULL_DOWN, 1 },
151 { "bias-pull-pin-default", PIN_CONFIG_BIAS_PULL_PIN_DEFAULT, 1 },
Masahiro Yamadae5299ed2018-05-05 19:53:55 +0900152 { "drive-strength", PIN_CONFIG_DRIVE_STRENGTH, 0 },
Masahiro Yamada140e9f12017-02-12 18:21:15 +0900153 { "input-enable", PIN_CONFIG_INPUT_ENABLE, 1 },
154 { "input-disable", PIN_CONFIG_INPUT_ENABLE, 0 },
155};
156
Masahiro Yamadae5299ed2018-05-05 19:53:55 +0900157static const struct uniphier_pinctrl_pin *
158uniphier_pinctrl_pin_get(struct uniphier_pinctrl_priv *priv, unsigned int pin)
159{
160 const struct uniphier_pinctrl_pin *pins = priv->socdata->pins;
161 int pins_count = priv->socdata->pins_count;
162 int i;
163
164 for (i = 0; i < pins_count; i++)
165 if (pins[i].number == pin)
166 return &pins[i];
167
168 return NULL;
169}
170
Masahiro Yamada140e9f12017-02-12 18:21:15 +0900171static int uniphier_pinconf_bias_set(struct udevice *dev, unsigned int pin,
172 unsigned int param, unsigned int arg)
173{
174 struct uniphier_pinctrl_priv *priv = dev_get_priv(dev);
175 unsigned int enable = 1;
176 unsigned int reg;
177 u32 mask, tmp;
178
179 if (!(priv->socdata->caps & UNIPHIER_PINCTRL_CAPS_PUPD_SIMPLE))
180 return -ENOTSUPP;
181
182 switch (param) {
183 case PIN_CONFIG_BIAS_DISABLE:
184 enable = 0;
185 break;
186 case PIN_CONFIG_BIAS_PULL_UP:
187 case PIN_CONFIG_BIAS_PULL_DOWN:
188 if (arg == 0) /* total bias is not supported */
189 return -EINVAL;
190 break;
191 case PIN_CONFIG_BIAS_PULL_PIN_DEFAULT:
192 if (arg == 0) /* configuration ignored */
193 return 0;
194 default:
195 BUG();
196 }
197
198 reg = UNIPHIER_PINCTRL_PUPDCTRL_BASE + pin / 32 * 4;
199 mask = BIT(pin % 32);
200
201 tmp = readl(priv->base + reg);
202 if (enable)
203 tmp |= mask;
204 else
205 tmp &= ~mask;
206 writel(tmp, priv->base + reg);
207
208 return 0;
209}
210
Masahiro Yamadae5299ed2018-05-05 19:53:55 +0900211static const unsigned int uniphier_pinconf_drv_strengths_1bit[] = {
212 4, 8,
213};
214
215static const unsigned int uniphier_pinconf_drv_strengths_2bit[] = {
216 8, 12, 16, 20,
217};
218
219static const unsigned int uniphier_pinconf_drv_strengths_3bit[] = {
220 4, 5, 7, 9, 11, 12, 14, 16,
221};
222
223static int uniphier_pinconf_drive_set(struct udevice *dev, unsigned int pin,
224 unsigned int strength)
225{
226 struct uniphier_pinctrl_priv *priv = dev_get_priv(dev);
227 const struct uniphier_pinctrl_pin *desc;
228 const unsigned int *strengths;
229 unsigned int base, stride, width, drvctrl, reg, shift;
230 u32 val, mask, tmp;
231
232 desc = uniphier_pinctrl_pin_get(priv, pin);
233 if (WARN_ON(!desc))
234 return -EINVAL;
235
236 switch (uniphier_pin_get_drv_type(desc->data)) {
237 case UNIPHIER_PIN_DRV_1BIT:
238 strengths = uniphier_pinconf_drv_strengths_1bit;
239 base = UNIPHIER_PINCTRL_DRVCTRL_BASE;
240 stride = 1;
241 width = 1;
242 break;
243 case UNIPHIER_PIN_DRV_2BIT:
244 strengths = uniphier_pinconf_drv_strengths_2bit;
245 base = UNIPHIER_PINCTRL_DRV2CTRL_BASE;
246 stride = 2;
247 width = 2;
248 break;
249 case UNIPHIER_PIN_DRV_3BIT:
250 strengths = uniphier_pinconf_drv_strengths_3bit;
251 base = UNIPHIER_PINCTRL_DRV3CTRL_BASE;
252 stride = 4;
253 width = 3;
254 break;
255 default:
256 /* drive strength control is not supported for this pin */
257 return -EINVAL;
258 }
259
260 drvctrl = uniphier_pin_get_drvctrl(desc->data);
261 drvctrl *= stride;
262
263 reg = base + drvctrl / 32 * 4;
264 shift = drvctrl % 32;
265 mask = (1U << width) - 1;
266
267 for (val = 0; val <= mask; val++) {
268 if (strengths[val] > strength)
269 break;
270 }
271
272 if (val == 0) {
273 dev_err(dev, "unsupported drive strength %u mA for pin %s\n",
274 strength, desc->name);
275 return -EINVAL;
276 }
277
278 if (!mask)
279 return 0;
280
281 val--;
282
283 tmp = readl(priv->base + reg);
284 tmp &= ~(mask << shift);
285 tmp |= (mask & val) << shift;
286 writel(tmp, priv->base + reg);
287
288 return 0;
289}
290
Masahiro Yamada914b5712018-05-05 19:53:54 +0900291static int uniphier_pinconf_set(struct udevice *dev, unsigned int pin,
292 unsigned int param, unsigned int arg)
Masahiro Yamada140e9f12017-02-12 18:21:15 +0900293{
294 int ret;
295
296 switch (param) {
297 case PIN_CONFIG_BIAS_DISABLE:
298 case PIN_CONFIG_BIAS_PULL_UP:
299 case PIN_CONFIG_BIAS_PULL_DOWN:
300 case PIN_CONFIG_BIAS_PULL_PIN_DEFAULT:
301 ret = uniphier_pinconf_bias_set(dev, pin, param, arg);
302 break;
Masahiro Yamadae5299ed2018-05-05 19:53:55 +0900303 case PIN_CONFIG_DRIVE_STRENGTH:
304 ret = uniphier_pinconf_drive_set(dev, pin, arg);
305 break;
Masahiro Yamada140e9f12017-02-12 18:21:15 +0900306 case PIN_CONFIG_INPUT_ENABLE:
307 ret = uniphier_pinconf_input_enable(dev, pin, arg);
308 break;
309 default:
Masahiro Yamada4bffb142018-05-05 19:53:52 +0900310 dev_err(dev, "unsupported configuration parameter %u\n", param);
Masahiro Yamada140e9f12017-02-12 18:21:15 +0900311 return -EINVAL;
312 }
313
314 return ret;
315}
316
317static int uniphier_pinconf_group_set(struct udevice *dev,
318 unsigned int group_selector,
319 unsigned int param, unsigned int arg)
320{
321 struct uniphier_pinctrl_priv *priv = dev_get_priv(dev);
322 const struct uniphier_pinctrl_group *grp =
323 &priv->socdata->groups[group_selector];
324 int i, ret;
325
326 for (i = 0; i < grp->num_pins; i++) {
Masahiro Yamada914b5712018-05-05 19:53:54 +0900327 ret = uniphier_pinconf_set(dev, grp->pins[i], param, arg);
Masahiro Yamada140e9f12017-02-12 18:21:15 +0900328 if (ret)
329 return ret;
330 }
331
332 return 0;
333}
334
335#endif /* CONFIG_IS_ENABLED(PINCONF) */
336
Masahiro Yamada847e618b82015-09-11 20:17:32 +0900337static void uniphier_pinmux_set_one(struct udevice *dev, unsigned pin,
Masahiro Yamada9447e132016-06-29 19:38:59 +0900338 int muxval)
Masahiro Yamada847e618b82015-09-11 20:17:32 +0900339{
340 struct uniphier_pinctrl_priv *priv = dev_get_priv(dev);
Masahiro Yamadac3380ed2016-09-17 03:32:58 +0900341 unsigned reg, reg_end, shift, mask;
342 unsigned mux_bits = 8;
343 unsigned reg_stride = 4;
344 bool load_pinctrl = false;
Masahiro Yamada847e618b82015-09-11 20:17:32 +0900345 u32 tmp;
346
Masahiro Yamadaa64e11a2016-03-04 15:56:16 +0900347 /* some pins need input-enabling */
Masahiro Yamada140e9f12017-02-12 18:21:15 +0900348 uniphier_pinconf_input_enable(dev, pin, 1);
Masahiro Yamadaa64e11a2016-03-04 15:56:16 +0900349
Masahiro Yamada9447e132016-06-29 19:38:59 +0900350 if (muxval < 0)
351 return; /* dedicated pin; nothing to do for pin-mux */
352
Masahiro Yamadac3380ed2016-09-17 03:32:58 +0900353 if (priv->socdata->caps & UNIPHIER_PINCTRL_CAPS_MUX_4BIT)
354 mux_bits = 4;
355
Masahiro Yamada7a629ef2016-03-24 22:32:44 +0900356 if (priv->socdata->caps & UNIPHIER_PINCTRL_CAPS_DBGMUX_SEPARATE) {
357 /*
358 * Mode offset bit
359 * Normal 4 * n shift+3:shift
360 * Debug 4 * n shift+7:shift+4
361 */
Masahiro Yamadac3380ed2016-09-17 03:32:58 +0900362 mux_bits /= 2;
Masahiro Yamada7a629ef2016-03-24 22:32:44 +0900363 reg_stride = 8;
364 load_pinctrl = true;
Masahiro Yamada7a629ef2016-03-24 22:32:44 +0900365 }
366
Masahiro Yamada847e618b82015-09-11 20:17:32 +0900367 reg = UNIPHIER_PINCTRL_PINMUX_BASE + pin * mux_bits / 32 * reg_stride;
368 reg_end = reg + reg_stride;
369 shift = pin * mux_bits % 32;
370 mask = (1U << mux_bits) - 1;
371
372 /*
373 * If reg_stride is greater than 4, the MSB of each pinsel shall be
374 * stored in the offset+4.
375 */
376 for (; reg < reg_end; reg += 4) {
377 tmp = readl(priv->base + reg);
378 tmp &= ~(mask << shift);
379 tmp |= (mask & muxval) << shift;
380 writel(tmp, priv->base + reg);
381
382 muxval >>= mux_bits;
383 }
384
Masahiro Yamada7a629ef2016-03-24 22:32:44 +0900385 if (load_pinctrl)
Masahiro Yamada847e618b82015-09-11 20:17:32 +0900386 writel(1, priv->base + UNIPHIER_PINCTRL_LOAD_PINMUX);
Masahiro Yamada847e618b82015-09-11 20:17:32 +0900387}
388
389static int uniphier_pinmux_group_set(struct udevice *dev,
390 unsigned group_selector,
391 unsigned func_selector)
392{
393 struct uniphier_pinctrl_priv *priv = dev_get_priv(dev);
394 const struct uniphier_pinctrl_group *grp =
395 &priv->socdata->groups[group_selector];
396 int i;
397
398 for (i = 0; i < grp->num_pins; i++)
399 uniphier_pinmux_set_one(dev, grp->pins[i], grp->muxvals[i]);
400
401 return 0;
402}
403
404const struct pinctrl_ops uniphier_pinctrl_ops = {
Masahiro Yamada914b5712018-05-05 19:53:54 +0900405 .get_pins_count = uniphier_pinctrl_get_pins_count,
406 .get_pin_name = uniphier_pinctrl_get_pin_name,
Masahiro Yamada847e618b82015-09-11 20:17:32 +0900407 .get_groups_count = uniphier_pinctrl_get_groups_count,
408 .get_group_name = uniphier_pinctrl_get_group_name,
409 .get_functions_count = uniphier_pinmux_get_functions_count,
410 .get_function_name = uniphier_pinmux_get_function_name,
411 .pinmux_group_set = uniphier_pinmux_group_set,
Masahiro Yamada140e9f12017-02-12 18:21:15 +0900412#if CONFIG_IS_ENABLED(PINCONF)
413 .pinconf_num_params = ARRAY_SIZE(uniphier_pinconf_params),
414 .pinconf_params = uniphier_pinconf_params,
Masahiro Yamada914b5712018-05-05 19:53:54 +0900415 .pinconf_set = uniphier_pinconf_set,
Masahiro Yamada140e9f12017-02-12 18:21:15 +0900416 .pinconf_group_set = uniphier_pinconf_group_set,
417#endif
Masahiro Yamada847e618b82015-09-11 20:17:32 +0900418 .set_state = pinctrl_generic_set_state,
419};
420
421int uniphier_pinctrl_probe(struct udevice *dev,
422 struct uniphier_pinctrl_socdata *socdata)
423{
424 struct uniphier_pinctrl_priv *priv = dev_get_priv(dev);
425 fdt_addr_t addr;
Masahiro Yamada847e618b82015-09-11 20:17:32 +0900426
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +0900427 addr = dev_read_addr(dev->parent);
Masahiro Yamada847e618b82015-09-11 20:17:32 +0900428 if (addr == FDT_ADDR_T_NONE)
429 return -EINVAL;
430
Masahiro Yamadafa1f73f2016-07-19 21:56:13 +0900431 priv->base = devm_ioremap(dev, addr, SZ_4K);
Masahiro Yamada847e618b82015-09-11 20:17:32 +0900432 if (!priv->base)
433 return -ENOMEM;
434
435 priv->socdata = socdata;
436
437 return 0;
438}