1 // SPDX-License-Identifier: GPL-2.0+
5 * (C) Copyright 2014 DENX Software Engineering GmbH
10 #include <asm/arch-lpc32xx/cpu.h>
11 #include <asm/arch-lpc32xx/gpio.h>
12 #include <asm-generic/gpio.h>
16 * LPC32xx GPIOs work in banks but are non-homogeneous:
17 * - each bank holds a different number of GPIOs
18 * - some GPIOs are input/ouput, some input only, some output only;
19 * - some GPIOs have different meanings as an input and as an output;
20 * - some GPIOs are controlled on a given port and bit index, but
21 * read on another one.
23 * In order to keep this code simple, GPIOS are considered here as
24 * homogeneous and linear, from 0 to 159.
28 * Client code is responsible for properly using valid GPIO numbers,
29 * including cases where a single physical GPIO has differing numbers
30 * for setting its direction, reading it and/or writing to it.
34 * Please read NOTE in description of lpc32xx_gpio_get_function().
37 #define LPC32XX_GPIOS 160
39 struct lpc32xx_gpio_priv {
40 struct gpio_regs *regs;
41 /* GPIO FUNCTION: SEE WARNING #2 */
42 signed char function[LPC32XX_GPIOS];
46 * We have 4 GPIO ports of 32 bits each
48 * Port mapping offset (32 bits each):
52 * - Port 3: GPO / GPIO (output): 96
58 #define GPIO_TO_PORT(gpio) ((gpio / 32) & 7)
59 #define GPIO_TO_RANK(gpio) (gpio % 32)
60 #define GPIO_TO_MASK(gpio) (1 << (gpio % 32))
63 * Configure a GPIO number 'offset' as input
66 static int lpc32xx_gpio_direction_input(struct udevice *dev, unsigned offset)
69 struct lpc32xx_gpio_priv *gpio_priv = dev_get_priv(dev);
70 struct gpio_regs *regs = gpio_priv->regs;
72 port = GPIO_TO_PORT(offset);
73 mask = GPIO_TO_MASK(offset);
77 writel(mask, ®s->p0_dir_clr);
80 writel(mask, ®s->p1_dir_clr);
83 /* ports 2 and 3 share a common direction */
84 writel(mask, ®s->p2_p3_dir_clr);
87 /* Setup direction only for GPIO_xx. */
88 if ((mask >= 25) && (mask <= 30))
89 writel(mask, ®s->p2_p3_dir_clr);
92 /* GPI_xx; nothing to do. */
98 /* GPIO FUNCTION: SEE WARNING #2 */
99 gpio_priv->function[offset] = GPIOF_INPUT;
105 * Get the value of a GPIO
108 static int lpc32xx_gpio_get_value(struct udevice *dev, unsigned offset)
110 int port, rank, mask, value;
111 struct lpc32xx_gpio_priv *gpio_priv = dev_get_priv(dev);
112 struct gpio_regs *regs = gpio_priv->regs;
114 port = GPIO_TO_PORT(offset);
118 value = readl(®s->p0_inp_state);
121 value = readl(®s->p1_inp_state);
124 value = readl(®s->p2_inp_state);
127 /* Read GPO_xx and GPIO_xx (as output) using p3_outp_state. */
128 value = readl(®s->p3_outp_state);
131 /* Read GPI_xx and GPIO_xx (as input) using p3_inp_state. */
132 value = readl(®s->p3_inp_state);
138 rank = GPIO_TO_RANK(offset);
139 mask = GPIO_TO_MASK(offset);
141 return (value & mask) >> rank;
148 static int gpio_set(struct udevice *dev, unsigned gpio)
151 struct lpc32xx_gpio_priv *gpio_priv = dev_get_priv(dev);
152 struct gpio_regs *regs = gpio_priv->regs;
154 port = GPIO_TO_PORT(gpio);
155 mask = GPIO_TO_MASK(gpio);
159 writel(mask, ®s->p0_outp_set);
162 writel(mask, ®s->p1_outp_set);
165 writel(mask, ®s->p2_outp_set);
168 writel(mask, ®s->p3_outp_set);
171 /* GPI_xx; invalid. */
182 static int gpio_clr(struct udevice *dev, unsigned gpio)
185 struct lpc32xx_gpio_priv *gpio_priv = dev_get_priv(dev);
186 struct gpio_regs *regs = gpio_priv->regs;
188 port = GPIO_TO_PORT(gpio);
189 mask = GPIO_TO_MASK(gpio);
193 writel(mask, ®s->p0_outp_clr);
196 writel(mask, ®s->p1_outp_clr);
199 writel(mask, ®s->p2_outp_clr);
202 writel(mask, ®s->p3_outp_clr);
205 /* GPI_xx; invalid. */
213 * Set the value of a GPIO
216 static int lpc32xx_gpio_set_value(struct udevice *dev, unsigned offset,
220 return gpio_set(dev, offset);
222 return gpio_clr(dev, offset);
226 * Configure a GPIO number 'offset' as output with given initial value.
229 static int lpc32xx_gpio_direction_output(struct udevice *dev, unsigned offset,
233 struct lpc32xx_gpio_priv *gpio_priv = dev_get_priv(dev);
234 struct gpio_regs *regs = gpio_priv->regs;
236 port = GPIO_TO_PORT(offset);
237 mask = GPIO_TO_MASK(offset);
241 writel(mask, ®s->p0_dir_set);
244 writel(mask, ®s->p1_dir_set);
247 /* ports 2 and 3 share a common direction */
248 writel(mask, ®s->p2_p3_dir_set);
251 /* Setup direction only for GPIO_xx. */
252 if ((mask >= 25) && (mask <= 30))
253 writel(mask, ®s->p2_p3_dir_set);
256 /* GPI_xx; invalid. */
261 /* GPIO FUNCTION: SEE WARNING #2 */
262 gpio_priv->function[offset] = GPIOF_OUTPUT;
264 return lpc32xx_gpio_set_value(dev, offset, value);
268 * GPIO functions are supposed to be computed from their current
269 * configuration, but that's way too complicated in LPC32XX. A simpler
270 * approach is used, where the GPIO functions are cached in an array.
271 * When the GPIO is in use, its function is either "input" or "output"
272 * depending on its direction, otherwise its function is "unknown".
276 * THIS APPROACH WAS CHOSEN DU TO THE COMPLEX NATURE OF THE LPC32XX
277 * GPIOS; DO NOT TAKE THIS AS AN EXAMPLE FOR NEW CODE.
280 static int lpc32xx_gpio_get_function(struct udevice *dev, unsigned offset)
282 struct lpc32xx_gpio_priv *gpio_priv = dev_get_priv(dev);
283 return gpio_priv->function[offset];
286 static const struct dm_gpio_ops gpio_lpc32xx_ops = {
287 .direction_input = lpc32xx_gpio_direction_input,
288 .direction_output = lpc32xx_gpio_direction_output,
289 .get_value = lpc32xx_gpio_get_value,
290 .set_value = lpc32xx_gpio_set_value,
291 .get_function = lpc32xx_gpio_get_function,
294 static int lpc32xx_gpio_probe(struct udevice *dev)
296 struct lpc32xx_gpio_priv *gpio_priv = dev_get_priv(dev);
297 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
299 if (dev_of_offset(dev) == -1) {
300 /* Tell the uclass how many GPIOs we have */
301 uc_priv->gpio_count = LPC32XX_GPIOS;
304 /* set base address for GPIO registers */
305 gpio_priv->regs = (struct gpio_regs *)GPIO_BASE;
307 /* all GPIO functions are unknown until requested */
308 /* GPIO FUNCTION: SEE WARNING #2 */
309 memset(gpio_priv->function, GPIOF_UNKNOWN, sizeof(gpio_priv->function));
314 U_BOOT_DRIVER(gpio_lpc32xx) = {
315 .name = "gpio_lpc32xx",
317 .ops = &gpio_lpc32xx_ops,
318 .probe = lpc32xx_gpio_probe,
319 .priv_auto = sizeof(struct lpc32xx_gpio_priv),