blob: a5935e84de3e15fa22bccd81a568da9d16e90468 [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
Masahiro Yamada140e9f12017-02-12 18:21:15 +09007#include <common.h>
Simon Glass11c89f32017-05-17 17:18:03 -06008#include <dm.h>
Masahiro Yamada847e618b82015-09-11 20:17:32 +09009#include <linux/io.h>
10#include <linux/err.h>
Masahiro Yamada165c5582017-10-13 19:21:54 +090011#include <linux/kernel.h>
Masahiro Yamadafba3a662016-03-24 22:32:43 +090012#include <linux/sizes.h>
Masahiro Yamada847e618b82015-09-11 20:17:32 +090013#include <dm/pinctrl.h>
14
15#include "pinctrl-uniphier.h"
16
Masahiro Yamadaa9945202016-09-14 01:06:05 +090017#define UNIPHIER_PINCTRL_PINMUX_BASE 0x1000
18#define UNIPHIER_PINCTRL_LOAD_PINMUX 0x1700
Masahiro Yamadae5299ed2018-05-05 19:53:55 +090019#define UNIPHIER_PINCTRL_DRVCTRL_BASE 0x1800
20#define UNIPHIER_PINCTRL_DRV2CTRL_BASE 0x1900
21#define UNIPHIER_PINCTRL_DRV3CTRL_BASE 0x1980
Masahiro Yamada140e9f12017-02-12 18:21:15 +090022#define UNIPHIER_PINCTRL_PUPDCTRL_BASE 0x1a00
Masahiro Yamadaa9945202016-09-14 01:06:05 +090023#define UNIPHIER_PINCTRL_IECTRL 0x1d00
24
Masahiro Yamada65ef4f72016-06-29 19:39:00 +090025static const char *uniphier_pinctrl_dummy_name = "_dummy";
26
Masahiro Yamada914b5712018-05-05 19:53:54 +090027static int uniphier_pinctrl_get_pins_count(struct udevice *dev)
28{
29 struct uniphier_pinctrl_priv *priv = dev_get_priv(dev);
30 const struct uniphier_pinctrl_pin *pins = priv->socdata->pins;
31 int pins_count = priv->socdata->pins_count;
32
33 /*
34 * We do not list all pins in the pin table to save memory footprint.
35 * Report the max pin number + 1 to fake the framework.
36 */
37 return pins[pins_count - 1].number + 1;
38}
39
40static const char *uniphier_pinctrl_get_pin_name(struct udevice *dev,
41 unsigned int selector)
42{
43 struct uniphier_pinctrl_priv *priv = dev_get_priv(dev);
44 const struct uniphier_pinctrl_pin *pins = priv->socdata->pins;
45 int pins_count = priv->socdata->pins_count;
46 int i;
47
48 for (i = 0; i < pins_count; i++)
49 if (pins[i].number == selector)
50 return pins[i].name;
51
52 return uniphier_pinctrl_dummy_name;
53}
54
Masahiro Yamada847e618b82015-09-11 20:17:32 +090055static int uniphier_pinctrl_get_groups_count(struct udevice *dev)
56{
57 struct uniphier_pinctrl_priv *priv = dev_get_priv(dev);
58
59 return priv->socdata->groups_count;
60}
61
62static const char *uniphier_pinctrl_get_group_name(struct udevice *dev,
63 unsigned selector)
64{
65 struct uniphier_pinctrl_priv *priv = dev_get_priv(dev);
66
Masahiro Yamada65ef4f72016-06-29 19:39:00 +090067 if (!priv->socdata->groups[selector].name)
68 return uniphier_pinctrl_dummy_name;
69
Masahiro Yamada847e618b82015-09-11 20:17:32 +090070 return priv->socdata->groups[selector].name;
71}
72
73static int uniphier_pinmux_get_functions_count(struct udevice *dev)
74{
75 struct uniphier_pinctrl_priv *priv = dev_get_priv(dev);
76
77 return priv->socdata->functions_count;
78}
79
80static const char *uniphier_pinmux_get_function_name(struct udevice *dev,
81 unsigned selector)
82{
83 struct uniphier_pinctrl_priv *priv = dev_get_priv(dev);
84
Masahiro Yamada65ef4f72016-06-29 19:39:00 +090085 if (!priv->socdata->functions[selector])
86 return uniphier_pinctrl_dummy_name;
87
Masahiro Yamada847e618b82015-09-11 20:17:32 +090088 return priv->socdata->functions[selector];
89}
90
Masahiro Yamada140e9f12017-02-12 18:21:15 +090091static int uniphier_pinconf_input_enable_perpin(struct udevice *dev,
92 unsigned int pin, int enable)
Masahiro Yamada73e67752016-03-24 22:32:45 +090093{
94 struct uniphier_pinctrl_priv *priv = dev_get_priv(dev);
95 unsigned reg;
96 u32 mask, tmp;
97
98 reg = UNIPHIER_PINCTRL_IECTRL + pin / 32 * 4;
99 mask = BIT(pin % 32);
100
101 tmp = readl(priv->base + reg);
Masahiro Yamada140e9f12017-02-12 18:21:15 +0900102 if (enable)
103 tmp |= mask;
104 else
105 tmp &= ~mask;
Masahiro Yamada73e67752016-03-24 22:32:45 +0900106 writel(tmp, priv->base + reg);
Masahiro Yamada140e9f12017-02-12 18:21:15 +0900107
108 return 0;
Masahiro Yamada73e67752016-03-24 22:32:45 +0900109}
110
Masahiro Yamada140e9f12017-02-12 18:21:15 +0900111static int uniphier_pinconf_input_enable_legacy(struct udevice *dev,
112 unsigned int pin, int enable)
Masahiro Yamada847e618b82015-09-11 20:17:32 +0900113{
114 struct uniphier_pinctrl_priv *priv = dev_get_priv(dev);
Masahiro Yamada847e618b82015-09-11 20:17:32 +0900115
Masahiro Yamada140e9f12017-02-12 18:21:15 +0900116 /*
117 * Multiple pins share one input enable, per-pin disabling is
118 * impossible.
119 */
120 if (!enable)
121 return -EINVAL;
122
Masahiro Yamada165c5582017-10-13 19:21:54 +0900123 /* Set all bits instead of having a bunch of pin data */
124 writel(U32_MAX, priv->base + UNIPHIER_PINCTRL_IECTRL);
Masahiro Yamada140e9f12017-02-12 18:21:15 +0900125
126 return 0;
Masahiro Yamada847e618b82015-09-11 20:17:32 +0900127}
128
Masahiro Yamada140e9f12017-02-12 18:21:15 +0900129static int uniphier_pinconf_input_enable(struct udevice *dev,
130 unsigned int pin, int enable)
Masahiro Yamada73e67752016-03-24 22:32:45 +0900131{
132 struct uniphier_pinctrl_priv *priv = dev_get_priv(dev);
133
134 if (priv->socdata->caps & UNIPHIER_PINCTRL_CAPS_PERPIN_IECTRL)
Masahiro Yamada140e9f12017-02-12 18:21:15 +0900135 return uniphier_pinconf_input_enable_perpin(dev, pin, enable);
Masahiro Yamada73e67752016-03-24 22:32:45 +0900136 else
Masahiro Yamada140e9f12017-02-12 18:21:15 +0900137 return uniphier_pinconf_input_enable_legacy(dev, pin, enable);
Masahiro Yamada73e67752016-03-24 22:32:45 +0900138}
139
Masahiro Yamada140e9f12017-02-12 18:21:15 +0900140#if CONFIG_IS_ENABLED(PINCONF)
141
142static const struct pinconf_param uniphier_pinconf_params[] = {
143 { "bias-disable", PIN_CONFIG_BIAS_DISABLE, 0 },
144 { "bias-pull-up", PIN_CONFIG_BIAS_PULL_UP, 1 },
145 { "bias-pull-down", PIN_CONFIG_BIAS_PULL_DOWN, 1 },
146 { "bias-pull-pin-default", PIN_CONFIG_BIAS_PULL_PIN_DEFAULT, 1 },
Masahiro Yamadae5299ed2018-05-05 19:53:55 +0900147 { "drive-strength", PIN_CONFIG_DRIVE_STRENGTH, 0 },
Masahiro Yamada140e9f12017-02-12 18:21:15 +0900148 { "input-enable", PIN_CONFIG_INPUT_ENABLE, 1 },
149 { "input-disable", PIN_CONFIG_INPUT_ENABLE, 0 },
150};
151
Masahiro Yamadae5299ed2018-05-05 19:53:55 +0900152static const struct uniphier_pinctrl_pin *
153uniphier_pinctrl_pin_get(struct uniphier_pinctrl_priv *priv, unsigned int pin)
154{
155 const struct uniphier_pinctrl_pin *pins = priv->socdata->pins;
156 int pins_count = priv->socdata->pins_count;
157 int i;
158
159 for (i = 0; i < pins_count; i++)
160 if (pins[i].number == pin)
161 return &pins[i];
162
163 return NULL;
164}
165
Masahiro Yamada140e9f12017-02-12 18:21:15 +0900166static int uniphier_pinconf_bias_set(struct udevice *dev, unsigned int pin,
167 unsigned int param, unsigned int arg)
168{
169 struct uniphier_pinctrl_priv *priv = dev_get_priv(dev);
170 unsigned int enable = 1;
171 unsigned int reg;
172 u32 mask, tmp;
173
174 if (!(priv->socdata->caps & UNIPHIER_PINCTRL_CAPS_PUPD_SIMPLE))
175 return -ENOTSUPP;
176
177 switch (param) {
178 case PIN_CONFIG_BIAS_DISABLE:
179 enable = 0;
180 break;
181 case PIN_CONFIG_BIAS_PULL_UP:
182 case PIN_CONFIG_BIAS_PULL_DOWN:
183 if (arg == 0) /* total bias is not supported */
184 return -EINVAL;
185 break;
186 case PIN_CONFIG_BIAS_PULL_PIN_DEFAULT:
187 if (arg == 0) /* configuration ignored */
188 return 0;
189 default:
190 BUG();
191 }
192
193 reg = UNIPHIER_PINCTRL_PUPDCTRL_BASE + pin / 32 * 4;
194 mask = BIT(pin % 32);
195
196 tmp = readl(priv->base + reg);
197 if (enable)
198 tmp |= mask;
199 else
200 tmp &= ~mask;
201 writel(tmp, priv->base + reg);
202
203 return 0;
204}
205
Masahiro Yamadae5299ed2018-05-05 19:53:55 +0900206static const unsigned int uniphier_pinconf_drv_strengths_1bit[] = {
207 4, 8,
208};
209
210static const unsigned int uniphier_pinconf_drv_strengths_2bit[] = {
211 8, 12, 16, 20,
212};
213
214static const unsigned int uniphier_pinconf_drv_strengths_3bit[] = {
215 4, 5, 7, 9, 11, 12, 14, 16,
216};
217
218static int uniphier_pinconf_drive_set(struct udevice *dev, unsigned int pin,
219 unsigned int strength)
220{
221 struct uniphier_pinctrl_priv *priv = dev_get_priv(dev);
222 const struct uniphier_pinctrl_pin *desc;
223 const unsigned int *strengths;
224 unsigned int base, stride, width, drvctrl, reg, shift;
225 u32 val, mask, tmp;
226
227 desc = uniphier_pinctrl_pin_get(priv, pin);
228 if (WARN_ON(!desc))
229 return -EINVAL;
230
231 switch (uniphier_pin_get_drv_type(desc->data)) {
232 case UNIPHIER_PIN_DRV_1BIT:
233 strengths = uniphier_pinconf_drv_strengths_1bit;
234 base = UNIPHIER_PINCTRL_DRVCTRL_BASE;
235 stride = 1;
236 width = 1;
237 break;
238 case UNIPHIER_PIN_DRV_2BIT:
239 strengths = uniphier_pinconf_drv_strengths_2bit;
240 base = UNIPHIER_PINCTRL_DRV2CTRL_BASE;
241 stride = 2;
242 width = 2;
243 break;
244 case UNIPHIER_PIN_DRV_3BIT:
245 strengths = uniphier_pinconf_drv_strengths_3bit;
246 base = UNIPHIER_PINCTRL_DRV3CTRL_BASE;
247 stride = 4;
248 width = 3;
249 break;
250 default:
251 /* drive strength control is not supported for this pin */
252 return -EINVAL;
253 }
254
255 drvctrl = uniphier_pin_get_drvctrl(desc->data);
256 drvctrl *= stride;
257
258 reg = base + drvctrl / 32 * 4;
259 shift = drvctrl % 32;
260 mask = (1U << width) - 1;
261
262 for (val = 0; val <= mask; val++) {
263 if (strengths[val] > strength)
264 break;
265 }
266
267 if (val == 0) {
268 dev_err(dev, "unsupported drive strength %u mA for pin %s\n",
269 strength, desc->name);
270 return -EINVAL;
271 }
272
273 if (!mask)
274 return 0;
275
276 val--;
277
278 tmp = readl(priv->base + reg);
279 tmp &= ~(mask << shift);
280 tmp |= (mask & val) << shift;
281 writel(tmp, priv->base + reg);
282
283 return 0;
284}
285
Masahiro Yamada914b5712018-05-05 19:53:54 +0900286static int uniphier_pinconf_set(struct udevice *dev, unsigned int pin,
287 unsigned int param, unsigned int arg)
Masahiro Yamada140e9f12017-02-12 18:21:15 +0900288{
289 int ret;
290
291 switch (param) {
292 case PIN_CONFIG_BIAS_DISABLE:
293 case PIN_CONFIG_BIAS_PULL_UP:
294 case PIN_CONFIG_BIAS_PULL_DOWN:
295 case PIN_CONFIG_BIAS_PULL_PIN_DEFAULT:
296 ret = uniphier_pinconf_bias_set(dev, pin, param, arg);
297 break;
Masahiro Yamadae5299ed2018-05-05 19:53:55 +0900298 case PIN_CONFIG_DRIVE_STRENGTH:
299 ret = uniphier_pinconf_drive_set(dev, pin, arg);
300 break;
Masahiro Yamada140e9f12017-02-12 18:21:15 +0900301 case PIN_CONFIG_INPUT_ENABLE:
302 ret = uniphier_pinconf_input_enable(dev, pin, arg);
303 break;
304 default:
Masahiro Yamada4bffb142018-05-05 19:53:52 +0900305 dev_err(dev, "unsupported configuration parameter %u\n", param);
Masahiro Yamada140e9f12017-02-12 18:21:15 +0900306 return -EINVAL;
307 }
308
309 return ret;
310}
311
312static int uniphier_pinconf_group_set(struct udevice *dev,
313 unsigned int group_selector,
314 unsigned int param, unsigned int arg)
315{
316 struct uniphier_pinctrl_priv *priv = dev_get_priv(dev);
317 const struct uniphier_pinctrl_group *grp =
318 &priv->socdata->groups[group_selector];
319 int i, ret;
320
321 for (i = 0; i < grp->num_pins; i++) {
Masahiro Yamada914b5712018-05-05 19:53:54 +0900322 ret = uniphier_pinconf_set(dev, grp->pins[i], param, arg);
Masahiro Yamada140e9f12017-02-12 18:21:15 +0900323 if (ret)
324 return ret;
325 }
326
327 return 0;
328}
329
330#endif /* CONFIG_IS_ENABLED(PINCONF) */
331
Masahiro Yamada847e618b82015-09-11 20:17:32 +0900332static void uniphier_pinmux_set_one(struct udevice *dev, unsigned pin,
Masahiro Yamada9447e132016-06-29 19:38:59 +0900333 int muxval)
Masahiro Yamada847e618b82015-09-11 20:17:32 +0900334{
335 struct uniphier_pinctrl_priv *priv = dev_get_priv(dev);
Masahiro Yamadac3380ed2016-09-17 03:32:58 +0900336 unsigned reg, reg_end, shift, mask;
337 unsigned mux_bits = 8;
338 unsigned reg_stride = 4;
339 bool load_pinctrl = false;
Masahiro Yamada847e618b82015-09-11 20:17:32 +0900340 u32 tmp;
341
Masahiro Yamadaa64e11a2016-03-04 15:56:16 +0900342 /* some pins need input-enabling */
Masahiro Yamada140e9f12017-02-12 18:21:15 +0900343 uniphier_pinconf_input_enable(dev, pin, 1);
Masahiro Yamadaa64e11a2016-03-04 15:56:16 +0900344
Masahiro Yamada9447e132016-06-29 19:38:59 +0900345 if (muxval < 0)
346 return; /* dedicated pin; nothing to do for pin-mux */
347
Masahiro Yamadac3380ed2016-09-17 03:32:58 +0900348 if (priv->socdata->caps & UNIPHIER_PINCTRL_CAPS_MUX_4BIT)
349 mux_bits = 4;
350
Masahiro Yamada7a629ef2016-03-24 22:32:44 +0900351 if (priv->socdata->caps & UNIPHIER_PINCTRL_CAPS_DBGMUX_SEPARATE) {
352 /*
353 * Mode offset bit
354 * Normal 4 * n shift+3:shift
355 * Debug 4 * n shift+7:shift+4
356 */
Masahiro Yamadac3380ed2016-09-17 03:32:58 +0900357 mux_bits /= 2;
Masahiro Yamada7a629ef2016-03-24 22:32:44 +0900358 reg_stride = 8;
359 load_pinctrl = true;
Masahiro Yamada7a629ef2016-03-24 22:32:44 +0900360 }
361
Masahiro Yamada847e618b82015-09-11 20:17:32 +0900362 reg = UNIPHIER_PINCTRL_PINMUX_BASE + pin * mux_bits / 32 * reg_stride;
363 reg_end = reg + reg_stride;
364 shift = pin * mux_bits % 32;
365 mask = (1U << mux_bits) - 1;
366
367 /*
368 * If reg_stride is greater than 4, the MSB of each pinsel shall be
369 * stored in the offset+4.
370 */
371 for (; reg < reg_end; reg += 4) {
372 tmp = readl(priv->base + reg);
373 tmp &= ~(mask << shift);
374 tmp |= (mask & muxval) << shift;
375 writel(tmp, priv->base + reg);
376
377 muxval >>= mux_bits;
378 }
379
Masahiro Yamada7a629ef2016-03-24 22:32:44 +0900380 if (load_pinctrl)
Masahiro Yamada847e618b82015-09-11 20:17:32 +0900381 writel(1, priv->base + UNIPHIER_PINCTRL_LOAD_PINMUX);
Masahiro Yamada847e618b82015-09-11 20:17:32 +0900382}
383
384static int uniphier_pinmux_group_set(struct udevice *dev,
385 unsigned group_selector,
386 unsigned func_selector)
387{
388 struct uniphier_pinctrl_priv *priv = dev_get_priv(dev);
389 const struct uniphier_pinctrl_group *grp =
390 &priv->socdata->groups[group_selector];
391 int i;
392
393 for (i = 0; i < grp->num_pins; i++)
394 uniphier_pinmux_set_one(dev, grp->pins[i], grp->muxvals[i]);
395
396 return 0;
397}
398
399const struct pinctrl_ops uniphier_pinctrl_ops = {
Masahiro Yamada914b5712018-05-05 19:53:54 +0900400 .get_pins_count = uniphier_pinctrl_get_pins_count,
401 .get_pin_name = uniphier_pinctrl_get_pin_name,
Masahiro Yamada847e618b82015-09-11 20:17:32 +0900402 .get_groups_count = uniphier_pinctrl_get_groups_count,
403 .get_group_name = uniphier_pinctrl_get_group_name,
404 .get_functions_count = uniphier_pinmux_get_functions_count,
405 .get_function_name = uniphier_pinmux_get_function_name,
406 .pinmux_group_set = uniphier_pinmux_group_set,
Masahiro Yamada140e9f12017-02-12 18:21:15 +0900407#if CONFIG_IS_ENABLED(PINCONF)
408 .pinconf_num_params = ARRAY_SIZE(uniphier_pinconf_params),
409 .pinconf_params = uniphier_pinconf_params,
Masahiro Yamada914b5712018-05-05 19:53:54 +0900410 .pinconf_set = uniphier_pinconf_set,
Masahiro Yamada140e9f12017-02-12 18:21:15 +0900411 .pinconf_group_set = uniphier_pinconf_group_set,
412#endif
Masahiro Yamada847e618b82015-09-11 20:17:32 +0900413 .set_state = pinctrl_generic_set_state,
414};
415
416int uniphier_pinctrl_probe(struct udevice *dev,
417 struct uniphier_pinctrl_socdata *socdata)
418{
419 struct uniphier_pinctrl_priv *priv = dev_get_priv(dev);
420 fdt_addr_t addr;
Masahiro Yamada847e618b82015-09-11 20:17:32 +0900421
Simon Glassba1dea42017-05-17 17:18:05 -0600422 addr = devfdt_get_addr(dev->parent);
Masahiro Yamada847e618b82015-09-11 20:17:32 +0900423 if (addr == FDT_ADDR_T_NONE)
424 return -EINVAL;
425
Masahiro Yamadafa1f73f2016-07-19 21:56:13 +0900426 priv->base = devm_ioremap(dev, addr, SZ_4K);
Masahiro Yamada847e618b82015-09-11 20:17:32 +0900427 if (!priv->base)
428 return -ENOMEM;
429
430 priv->socdata = socdata;
431
432 return 0;
433}