blob: bdca3f2f715665a4e679f08e97888f1e243bdb73 [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>
Simon Glass9bc15642020-02-03 07:36:16 -07009#include <dm/device_compat.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060010#include <linux/bitops.h>
Simon Glassc06c1be2020-05-10 11:40:08 -060011#include <linux/bug.h>
Masahiro Yamada847e618b82015-09-11 20:17:32 +090012#include <linux/io.h>
13#include <linux/err.h>
Masahiro Yamada165c5582017-10-13 19:21:54 +090014#include <linux/kernel.h>
Masahiro Yamadafba3a662016-03-24 22:32:43 +090015#include <linux/sizes.h>
Masahiro Yamada847e618b82015-09-11 20:17:32 +090016#include <dm/pinctrl.h>
17
18#include "pinctrl-uniphier.h"
19
Masahiro Yamadaa9945202016-09-14 01:06:05 +090020#define UNIPHIER_PINCTRL_PINMUX_BASE 0x1000
21#define UNIPHIER_PINCTRL_LOAD_PINMUX 0x1700
Masahiro Yamadae5299ed2018-05-05 19:53:55 +090022#define UNIPHIER_PINCTRL_DRVCTRL_BASE 0x1800
23#define UNIPHIER_PINCTRL_DRV2CTRL_BASE 0x1900
24#define UNIPHIER_PINCTRL_DRV3CTRL_BASE 0x1980
Masahiro Yamada140e9f12017-02-12 18:21:15 +090025#define UNIPHIER_PINCTRL_PUPDCTRL_BASE 0x1a00
Masahiro Yamadaa9945202016-09-14 01:06:05 +090026#define UNIPHIER_PINCTRL_IECTRL 0x1d00
27
Masahiro Yamada65ef4f72016-06-29 19:39:00 +090028static const char *uniphier_pinctrl_dummy_name = "_dummy";
29
Masahiro Yamada914b5712018-05-05 19:53:54 +090030static int uniphier_pinctrl_get_pins_count(struct udevice *dev)
31{
32 struct uniphier_pinctrl_priv *priv = dev_get_priv(dev);
33 const struct uniphier_pinctrl_pin *pins = priv->socdata->pins;
34 int pins_count = priv->socdata->pins_count;
35
Dai Okamura826ffbc2022-12-09 20:33:46 +090036 if (WARN_ON(!pins_count))
37 return 0; /* no table of pins */
38
Masahiro Yamada914b5712018-05-05 19:53:54 +090039 /*
40 * We do not list all pins in the pin table to save memory footprint.
41 * Report the max pin number + 1 to fake the framework.
42 */
43 return pins[pins_count - 1].number + 1;
44}
45
46static const char *uniphier_pinctrl_get_pin_name(struct udevice *dev,
47 unsigned int selector)
48{
49 struct uniphier_pinctrl_priv *priv = dev_get_priv(dev);
50 const struct uniphier_pinctrl_pin *pins = priv->socdata->pins;
51 int pins_count = priv->socdata->pins_count;
52 int i;
53
54 for (i = 0; i < pins_count; i++)
55 if (pins[i].number == selector)
56 return pins[i].name;
57
58 return uniphier_pinctrl_dummy_name;
59}
60
Masahiro Yamada847e618b82015-09-11 20:17:32 +090061static int uniphier_pinctrl_get_groups_count(struct udevice *dev)
62{
63 struct uniphier_pinctrl_priv *priv = dev_get_priv(dev);
64
65 return priv->socdata->groups_count;
66}
67
68static const char *uniphier_pinctrl_get_group_name(struct udevice *dev,
69 unsigned selector)
70{
71 struct uniphier_pinctrl_priv *priv = dev_get_priv(dev);
72
Masahiro Yamada65ef4f72016-06-29 19:39:00 +090073 if (!priv->socdata->groups[selector].name)
74 return uniphier_pinctrl_dummy_name;
75
Masahiro Yamada847e618b82015-09-11 20:17:32 +090076 return priv->socdata->groups[selector].name;
77}
78
79static int uniphier_pinmux_get_functions_count(struct udevice *dev)
80{
81 struct uniphier_pinctrl_priv *priv = dev_get_priv(dev);
82
83 return priv->socdata->functions_count;
84}
85
86static const char *uniphier_pinmux_get_function_name(struct udevice *dev,
87 unsigned selector)
88{
89 struct uniphier_pinctrl_priv *priv = dev_get_priv(dev);
90
Masahiro Yamada65ef4f72016-06-29 19:39:00 +090091 if (!priv->socdata->functions[selector])
92 return uniphier_pinctrl_dummy_name;
93
Masahiro Yamada847e618b82015-09-11 20:17:32 +090094 return priv->socdata->functions[selector];
95}
96
Masahiro Yamada140e9f12017-02-12 18:21:15 +090097static int uniphier_pinconf_input_enable_perpin(struct udevice *dev,
98 unsigned int pin, int enable)
Masahiro Yamada73e67752016-03-24 22:32:45 +090099{
100 struct uniphier_pinctrl_priv *priv = dev_get_priv(dev);
101 unsigned reg;
102 u32 mask, tmp;
103
104 reg = UNIPHIER_PINCTRL_IECTRL + pin / 32 * 4;
105 mask = BIT(pin % 32);
106
107 tmp = readl(priv->base + reg);
Masahiro Yamada140e9f12017-02-12 18:21:15 +0900108 if (enable)
109 tmp |= mask;
110 else
111 tmp &= ~mask;
Masahiro Yamada73e67752016-03-24 22:32:45 +0900112 writel(tmp, priv->base + reg);
Masahiro Yamada140e9f12017-02-12 18:21:15 +0900113
114 return 0;
Masahiro Yamada73e67752016-03-24 22:32:45 +0900115}
116
Masahiro Yamada140e9f12017-02-12 18:21:15 +0900117static int uniphier_pinconf_input_enable_legacy(struct udevice *dev,
118 unsigned int pin, int enable)
Masahiro Yamada847e618b82015-09-11 20:17:32 +0900119{
120 struct uniphier_pinctrl_priv *priv = dev_get_priv(dev);
Masahiro Yamada847e618b82015-09-11 20:17:32 +0900121
Masahiro Yamada140e9f12017-02-12 18:21:15 +0900122 /*
123 * Multiple pins share one input enable, per-pin disabling is
124 * impossible.
125 */
126 if (!enable)
127 return -EINVAL;
128
Masahiro Yamada165c5582017-10-13 19:21:54 +0900129 /* Set all bits instead of having a bunch of pin data */
130 writel(U32_MAX, priv->base + UNIPHIER_PINCTRL_IECTRL);
Masahiro Yamada140e9f12017-02-12 18:21:15 +0900131
132 return 0;
Masahiro Yamada847e618b82015-09-11 20:17:32 +0900133}
134
Masahiro Yamada140e9f12017-02-12 18:21:15 +0900135static int uniphier_pinconf_input_enable(struct udevice *dev,
136 unsigned int pin, int enable)
Masahiro Yamada73e67752016-03-24 22:32:45 +0900137{
138 struct uniphier_pinctrl_priv *priv = dev_get_priv(dev);
139
140 if (priv->socdata->caps & UNIPHIER_PINCTRL_CAPS_PERPIN_IECTRL)
Masahiro Yamada140e9f12017-02-12 18:21:15 +0900141 return uniphier_pinconf_input_enable_perpin(dev, pin, enable);
Masahiro Yamada73e67752016-03-24 22:32:45 +0900142 else
Masahiro Yamada140e9f12017-02-12 18:21:15 +0900143 return uniphier_pinconf_input_enable_legacy(dev, pin, enable);
Masahiro Yamada73e67752016-03-24 22:32:45 +0900144}
145
Masahiro Yamada140e9f12017-02-12 18:21:15 +0900146#if CONFIG_IS_ENABLED(PINCONF)
147
148static const struct pinconf_param uniphier_pinconf_params[] = {
149 { "bias-disable", PIN_CONFIG_BIAS_DISABLE, 0 },
150 { "bias-pull-up", PIN_CONFIG_BIAS_PULL_UP, 1 },
151 { "bias-pull-down", PIN_CONFIG_BIAS_PULL_DOWN, 1 },
152 { "bias-pull-pin-default", PIN_CONFIG_BIAS_PULL_PIN_DEFAULT, 1 },
Masahiro Yamadae5299ed2018-05-05 19:53:55 +0900153 { "drive-strength", PIN_CONFIG_DRIVE_STRENGTH, 0 },
Masahiro Yamada140e9f12017-02-12 18:21:15 +0900154 { "input-enable", PIN_CONFIG_INPUT_ENABLE, 1 },
155 { "input-disable", PIN_CONFIG_INPUT_ENABLE, 0 },
156};
157
Masahiro Yamadae5299ed2018-05-05 19:53:55 +0900158static const struct uniphier_pinctrl_pin *
159uniphier_pinctrl_pin_get(struct uniphier_pinctrl_priv *priv, unsigned int pin)
160{
161 const struct uniphier_pinctrl_pin *pins = priv->socdata->pins;
162 int pins_count = priv->socdata->pins_count;
163 int i;
164
165 for (i = 0; i < pins_count; i++)
166 if (pins[i].number == pin)
167 return &pins[i];
168
169 return NULL;
170}
171
Masahiro Yamada140e9f12017-02-12 18:21:15 +0900172static int uniphier_pinconf_bias_set(struct udevice *dev, unsigned int pin,
173 unsigned int param, unsigned int arg)
174{
175 struct uniphier_pinctrl_priv *priv = dev_get_priv(dev);
176 unsigned int enable = 1;
177 unsigned int reg;
178 u32 mask, tmp;
179
180 if (!(priv->socdata->caps & UNIPHIER_PINCTRL_CAPS_PUPD_SIMPLE))
181 return -ENOTSUPP;
182
183 switch (param) {
184 case PIN_CONFIG_BIAS_DISABLE:
185 enable = 0;
186 break;
187 case PIN_CONFIG_BIAS_PULL_UP:
188 case PIN_CONFIG_BIAS_PULL_DOWN:
189 if (arg == 0) /* total bias is not supported */
190 return -EINVAL;
191 break;
192 case PIN_CONFIG_BIAS_PULL_PIN_DEFAULT:
193 if (arg == 0) /* configuration ignored */
194 return 0;
195 default:
196 BUG();
197 }
198
199 reg = UNIPHIER_PINCTRL_PUPDCTRL_BASE + pin / 32 * 4;
200 mask = BIT(pin % 32);
201
202 tmp = readl(priv->base + reg);
203 if (enable)
204 tmp |= mask;
205 else
206 tmp &= ~mask;
207 writel(tmp, priv->base + reg);
208
209 return 0;
210}
211
Masahiro Yamadae5299ed2018-05-05 19:53:55 +0900212static const unsigned int uniphier_pinconf_drv_strengths_1bit[] = {
213 4, 8,
214};
215
216static const unsigned int uniphier_pinconf_drv_strengths_2bit[] = {
217 8, 12, 16, 20,
218};
219
220static const unsigned int uniphier_pinconf_drv_strengths_3bit[] = {
221 4, 5, 7, 9, 11, 12, 14, 16,
222};
223
224static int uniphier_pinconf_drive_set(struct udevice *dev, unsigned int pin,
225 unsigned int strength)
226{
227 struct uniphier_pinctrl_priv *priv = dev_get_priv(dev);
228 const struct uniphier_pinctrl_pin *desc;
229 const unsigned int *strengths;
230 unsigned int base, stride, width, drvctrl, reg, shift;
231 u32 val, mask, tmp;
232
233 desc = uniphier_pinctrl_pin_get(priv, pin);
234 if (WARN_ON(!desc))
235 return -EINVAL;
236
237 switch (uniphier_pin_get_drv_type(desc->data)) {
238 case UNIPHIER_PIN_DRV_1BIT:
239 strengths = uniphier_pinconf_drv_strengths_1bit;
240 base = UNIPHIER_PINCTRL_DRVCTRL_BASE;
241 stride = 1;
242 width = 1;
243 break;
244 case UNIPHIER_PIN_DRV_2BIT:
245 strengths = uniphier_pinconf_drv_strengths_2bit;
246 base = UNIPHIER_PINCTRL_DRV2CTRL_BASE;
247 stride = 2;
248 width = 2;
249 break;
250 case UNIPHIER_PIN_DRV_3BIT:
251 strengths = uniphier_pinconf_drv_strengths_3bit;
252 base = UNIPHIER_PINCTRL_DRV3CTRL_BASE;
253 stride = 4;
254 width = 3;
255 break;
256 default:
257 /* drive strength control is not supported for this pin */
258 return -EINVAL;
259 }
260
261 drvctrl = uniphier_pin_get_drvctrl(desc->data);
262 drvctrl *= stride;
263
264 reg = base + drvctrl / 32 * 4;
265 shift = drvctrl % 32;
266 mask = (1U << width) - 1;
267
268 for (val = 0; val <= mask; val++) {
269 if (strengths[val] > strength)
270 break;
271 }
272
273 if (val == 0) {
274 dev_err(dev, "unsupported drive strength %u mA for pin %s\n",
275 strength, desc->name);
276 return -EINVAL;
277 }
278
279 if (!mask)
280 return 0;
281
282 val--;
283
284 tmp = readl(priv->base + reg);
285 tmp &= ~(mask << shift);
286 tmp |= (mask & val) << shift;
287 writel(tmp, priv->base + reg);
288
289 return 0;
290}
291
Masahiro Yamada914b5712018-05-05 19:53:54 +0900292static int uniphier_pinconf_set(struct udevice *dev, unsigned int pin,
293 unsigned int param, unsigned int arg)
Masahiro Yamada140e9f12017-02-12 18:21:15 +0900294{
295 int ret;
296
297 switch (param) {
298 case PIN_CONFIG_BIAS_DISABLE:
299 case PIN_CONFIG_BIAS_PULL_UP:
300 case PIN_CONFIG_BIAS_PULL_DOWN:
301 case PIN_CONFIG_BIAS_PULL_PIN_DEFAULT:
302 ret = uniphier_pinconf_bias_set(dev, pin, param, arg);
303 break;
Masahiro Yamadae5299ed2018-05-05 19:53:55 +0900304 case PIN_CONFIG_DRIVE_STRENGTH:
305 ret = uniphier_pinconf_drive_set(dev, pin, arg);
306 break;
Masahiro Yamada140e9f12017-02-12 18:21:15 +0900307 case PIN_CONFIG_INPUT_ENABLE:
308 ret = uniphier_pinconf_input_enable(dev, pin, arg);
309 break;
310 default:
Masahiro Yamada4bffb142018-05-05 19:53:52 +0900311 dev_err(dev, "unsupported configuration parameter %u\n", param);
Masahiro Yamada140e9f12017-02-12 18:21:15 +0900312 return -EINVAL;
313 }
314
315 return ret;
316}
317
318static int uniphier_pinconf_group_set(struct udevice *dev,
319 unsigned int group_selector,
320 unsigned int param, unsigned int arg)
321{
322 struct uniphier_pinctrl_priv *priv = dev_get_priv(dev);
323 const struct uniphier_pinctrl_group *grp =
324 &priv->socdata->groups[group_selector];
325 int i, ret;
326
327 for (i = 0; i < grp->num_pins; i++) {
Masahiro Yamada914b5712018-05-05 19:53:54 +0900328 ret = uniphier_pinconf_set(dev, grp->pins[i], param, arg);
Masahiro Yamada140e9f12017-02-12 18:21:15 +0900329 if (ret)
330 return ret;
331 }
332
333 return 0;
334}
335
336#endif /* CONFIG_IS_ENABLED(PINCONF) */
337
Masahiro Yamada847e618b82015-09-11 20:17:32 +0900338static void uniphier_pinmux_set_one(struct udevice *dev, unsigned pin,
Masahiro Yamada9447e132016-06-29 19:38:59 +0900339 int muxval)
Masahiro Yamada847e618b82015-09-11 20:17:32 +0900340{
341 struct uniphier_pinctrl_priv *priv = dev_get_priv(dev);
Masahiro Yamadac3380ed2016-09-17 03:32:58 +0900342 unsigned reg, reg_end, shift, mask;
343 unsigned mux_bits = 8;
344 unsigned reg_stride = 4;
345 bool load_pinctrl = false;
Masahiro Yamada847e618b82015-09-11 20:17:32 +0900346 u32 tmp;
347
Masahiro Yamadaa64e11a2016-03-04 15:56:16 +0900348 /* some pins need input-enabling */
Masahiro Yamada140e9f12017-02-12 18:21:15 +0900349 uniphier_pinconf_input_enable(dev, pin, 1);
Masahiro Yamadaa64e11a2016-03-04 15:56:16 +0900350
Masahiro Yamada9447e132016-06-29 19:38:59 +0900351 if (muxval < 0)
352 return; /* dedicated pin; nothing to do for pin-mux */
353
Masahiro Yamadac3380ed2016-09-17 03:32:58 +0900354 if (priv->socdata->caps & UNIPHIER_PINCTRL_CAPS_MUX_4BIT)
355 mux_bits = 4;
356
Masahiro Yamada7a629ef2016-03-24 22:32:44 +0900357 if (priv->socdata->caps & UNIPHIER_PINCTRL_CAPS_DBGMUX_SEPARATE) {
358 /*
359 * Mode offset bit
360 * Normal 4 * n shift+3:shift
361 * Debug 4 * n shift+7:shift+4
362 */
Masahiro Yamadac3380ed2016-09-17 03:32:58 +0900363 mux_bits /= 2;
Masahiro Yamada7a629ef2016-03-24 22:32:44 +0900364 reg_stride = 8;
365 load_pinctrl = true;
Masahiro Yamada7a629ef2016-03-24 22:32:44 +0900366 }
367
Masahiro Yamada847e618b82015-09-11 20:17:32 +0900368 reg = UNIPHIER_PINCTRL_PINMUX_BASE + pin * mux_bits / 32 * reg_stride;
369 reg_end = reg + reg_stride;
370 shift = pin * mux_bits % 32;
371 mask = (1U << mux_bits) - 1;
372
373 /*
374 * If reg_stride is greater than 4, the MSB of each pinsel shall be
375 * stored in the offset+4.
376 */
377 for (; reg < reg_end; reg += 4) {
378 tmp = readl(priv->base + reg);
379 tmp &= ~(mask << shift);
380 tmp |= (mask & muxval) << shift;
381 writel(tmp, priv->base + reg);
382
383 muxval >>= mux_bits;
384 }
385
Masahiro Yamada7a629ef2016-03-24 22:32:44 +0900386 if (load_pinctrl)
Masahiro Yamada847e618b82015-09-11 20:17:32 +0900387 writel(1, priv->base + UNIPHIER_PINCTRL_LOAD_PINMUX);
Masahiro Yamada847e618b82015-09-11 20:17:32 +0900388}
389
390static int uniphier_pinmux_group_set(struct udevice *dev,
391 unsigned group_selector,
392 unsigned func_selector)
393{
394 struct uniphier_pinctrl_priv *priv = dev_get_priv(dev);
395 const struct uniphier_pinctrl_group *grp =
396 &priv->socdata->groups[group_selector];
397 int i;
398
399 for (i = 0; i < grp->num_pins; i++)
400 uniphier_pinmux_set_one(dev, grp->pins[i], grp->muxvals[i]);
401
402 return 0;
403}
404
405const struct pinctrl_ops uniphier_pinctrl_ops = {
Masahiro Yamada914b5712018-05-05 19:53:54 +0900406 .get_pins_count = uniphier_pinctrl_get_pins_count,
407 .get_pin_name = uniphier_pinctrl_get_pin_name,
Masahiro Yamada847e618b82015-09-11 20:17:32 +0900408 .get_groups_count = uniphier_pinctrl_get_groups_count,
409 .get_group_name = uniphier_pinctrl_get_group_name,
410 .get_functions_count = uniphier_pinmux_get_functions_count,
411 .get_function_name = uniphier_pinmux_get_function_name,
412 .pinmux_group_set = uniphier_pinmux_group_set,
Masahiro Yamada140e9f12017-02-12 18:21:15 +0900413#if CONFIG_IS_ENABLED(PINCONF)
414 .pinconf_num_params = ARRAY_SIZE(uniphier_pinconf_params),
415 .pinconf_params = uniphier_pinconf_params,
Masahiro Yamada914b5712018-05-05 19:53:54 +0900416 .pinconf_set = uniphier_pinconf_set,
Masahiro Yamada140e9f12017-02-12 18:21:15 +0900417 .pinconf_group_set = uniphier_pinconf_group_set,
418#endif
Masahiro Yamada847e618b82015-09-11 20:17:32 +0900419 .set_state = pinctrl_generic_set_state,
420};
421
422int uniphier_pinctrl_probe(struct udevice *dev,
423 struct uniphier_pinctrl_socdata *socdata)
424{
425 struct uniphier_pinctrl_priv *priv = dev_get_priv(dev);
426 fdt_addr_t addr;
Masahiro Yamada847e618b82015-09-11 20:17:32 +0900427
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +0900428 addr = dev_read_addr(dev->parent);
Masahiro Yamada847e618b82015-09-11 20:17:32 +0900429 if (addr == FDT_ADDR_T_NONE)
430 return -EINVAL;
431
Masahiro Yamadafa1f73f2016-07-19 21:56:13 +0900432 priv->base = devm_ioremap(dev, addr, SZ_4K);
Masahiro Yamada847e618b82015-09-11 20:17:32 +0900433 if (!priv->base)
434 return -ENOMEM;
435
436 priv->socdata = socdata;
437
438 return 0;
439}