1 // SPDX-License-Identifier: GPL-2.0+
5 * (C) Copyright 2014 DENX Software Engineering GmbH
6 * Written-by: Albert ARIBAUD <albert.aribaud@3adev.fr>
11 #include <asm/arch-lpc32xx/cpu.h>
12 #include <asm/arch-lpc32xx/gpio.h>
13 #include <asm-generic/gpio.h>
17 * LPC32xx GPIOs work in banks but are non-homogeneous:
18 * - each bank holds a different number of GPIOs
19 * - some GPIOs are input/ouput, some input only, some output only;
20 * - some GPIOs have different meanings as an input and as an output;
21 * - some GPIOs are controlled on a given port and bit index, but
22 * read on another one.
24 * In order to keep this code simple, GPIOS are considered here as
25 * homogeneous and linear, from 0 to 159.
29 * Client code is responsible for properly using valid GPIO numbers,
30 * including cases where a single physical GPIO has differing numbers
31 * for setting its direction, reading it and/or writing to it.
35 * Please read NOTE in description of lpc32xx_gpio_get_function().
38 #define LPC32XX_GPIOS 160
40 struct lpc32xx_gpio_priv
{
41 struct gpio_regs
*regs
;
42 /* GPIO FUNCTION: SEE WARNING #2 */
43 signed char function
[LPC32XX_GPIOS
];
47 * We have 4 GPIO ports of 32 bits each
49 * Port mapping offset (32 bits each):
53 * - Port 3: GPO / GPIO (output): 96
59 #define GPIO_TO_PORT(gpio) ((gpio / 32) & 7)
60 #define GPIO_TO_RANK(gpio) (gpio % 32)
61 #define GPIO_TO_MASK(gpio) (1 << (gpio % 32))
64 * Configure a GPIO number 'offset' as input
67 static int lpc32xx_gpio_direction_input(struct udevice
*dev
, unsigned offset
)
70 struct lpc32xx_gpio_priv
*gpio_priv
= dev_get_priv(dev
);
71 struct gpio_regs
*regs
= gpio_priv
->regs
;
73 port
= GPIO_TO_PORT(offset
);
74 mask
= GPIO_TO_MASK(offset
);
78 writel(mask
, ®s
->p0_dir_clr
);
81 writel(mask
, ®s
->p1_dir_clr
);
84 /* ports 2 and 3 share a common direction */
85 writel(mask
, ®s
->p2_p3_dir_clr
);
88 /* Setup direction only for GPIO_xx. */
89 if ((mask
>= 25) && (mask
<= 30))
90 writel(mask
, ®s
->p2_p3_dir_clr
);
93 /* GPI_xx; nothing to do. */
99 /* GPIO FUNCTION: SEE WARNING #2 */
100 gpio_priv
->function
[offset
] = GPIOF_INPUT
;
106 * Get the value of a GPIO
109 static int lpc32xx_gpio_get_value(struct udevice
*dev
, unsigned offset
)
111 int port
, rank
, mask
, value
;
112 struct lpc32xx_gpio_priv
*gpio_priv
= dev_get_priv(dev
);
113 struct gpio_regs
*regs
= gpio_priv
->regs
;
115 port
= GPIO_TO_PORT(offset
);
119 value
= readl(®s
->p0_inp_state
);
122 value
= readl(®s
->p1_inp_state
);
125 value
= readl(®s
->p2_inp_state
);
128 /* Read GPO_xx and GPIO_xx (as output) using p3_outp_state. */
129 value
= readl(®s
->p3_outp_state
);
132 /* Read GPI_xx and GPIO_xx (as input) using p3_inp_state. */
133 value
= readl(®s
->p3_inp_state
);
139 rank
= GPIO_TO_RANK(offset
);
140 mask
= GPIO_TO_MASK(offset
);
142 return (value
& mask
) >> rank
;
149 static int gpio_set(struct udevice
*dev
, unsigned gpio
)
152 struct lpc32xx_gpio_priv
*gpio_priv
= dev_get_priv(dev
);
153 struct gpio_regs
*regs
= gpio_priv
->regs
;
155 port
= GPIO_TO_PORT(gpio
);
156 mask
= GPIO_TO_MASK(gpio
);
160 writel(mask
, ®s
->p0_outp_set
);
163 writel(mask
, ®s
->p1_outp_set
);
166 writel(mask
, ®s
->p2_outp_set
);
169 writel(mask
, ®s
->p3_outp_set
);
172 /* GPI_xx; invalid. */
183 static int gpio_clr(struct udevice
*dev
, unsigned gpio
)
186 struct lpc32xx_gpio_priv
*gpio_priv
= dev_get_priv(dev
);
187 struct gpio_regs
*regs
= gpio_priv
->regs
;
189 port
= GPIO_TO_PORT(gpio
);
190 mask
= GPIO_TO_MASK(gpio
);
194 writel(mask
, ®s
->p0_outp_clr
);
197 writel(mask
, ®s
->p1_outp_clr
);
200 writel(mask
, ®s
->p2_outp_clr
);
203 writel(mask
, ®s
->p3_outp_clr
);
206 /* GPI_xx; invalid. */
214 * Set the value of a GPIO
217 static int lpc32xx_gpio_set_value(struct udevice
*dev
, unsigned offset
,
221 return gpio_set(dev
, offset
);
223 return gpio_clr(dev
, offset
);
227 * Configure a GPIO number 'offset' as output with given initial value.
230 static int lpc32xx_gpio_direction_output(struct udevice
*dev
, unsigned offset
,
234 struct lpc32xx_gpio_priv
*gpio_priv
= dev_get_priv(dev
);
235 struct gpio_regs
*regs
= gpio_priv
->regs
;
237 port
= GPIO_TO_PORT(offset
);
238 mask
= GPIO_TO_MASK(offset
);
242 writel(mask
, ®s
->p0_dir_set
);
245 writel(mask
, ®s
->p1_dir_set
);
248 /* ports 2 and 3 share a common direction */
249 writel(mask
, ®s
->p2_p3_dir_set
);
252 /* Setup direction only for GPIO_xx. */
253 if ((mask
>= 25) && (mask
<= 30))
254 writel(mask
, ®s
->p2_p3_dir_set
);
257 /* GPI_xx; invalid. */
262 /* GPIO FUNCTION: SEE WARNING #2 */
263 gpio_priv
->function
[offset
] = GPIOF_OUTPUT
;
265 return lpc32xx_gpio_set_value(dev
, offset
, value
);
269 * GPIO functions are supposed to be computed from their current
270 * configuration, but that's way too complicated in LPC32XX. A simpler
271 * approach is used, where the GPIO functions are cached in an array.
272 * When the GPIO is in use, its function is either "input" or "output"
273 * depending on its direction, otherwise its function is "unknown".
277 * THIS APPROACH WAS CHOSEN DU TO THE COMPLEX NATURE OF THE LPC32XX
278 * GPIOS; DO NOT TAKE THIS AS AN EXAMPLE FOR NEW CODE.
281 static int lpc32xx_gpio_get_function(struct udevice
*dev
, unsigned offset
)
283 struct lpc32xx_gpio_priv
*gpio_priv
= dev_get_priv(dev
);
284 return gpio_priv
->function
[offset
];
287 static const struct dm_gpio_ops gpio_lpc32xx_ops
= {
288 .direction_input
= lpc32xx_gpio_direction_input
,
289 .direction_output
= lpc32xx_gpio_direction_output
,
290 .get_value
= lpc32xx_gpio_get_value
,
291 .set_value
= lpc32xx_gpio_set_value
,
292 .get_function
= lpc32xx_gpio_get_function
,
295 static int lpc32xx_gpio_probe(struct udevice
*dev
)
297 struct lpc32xx_gpio_priv
*gpio_priv
= dev_get_priv(dev
);
298 struct gpio_dev_priv
*uc_priv
= dev
->uclass_priv
;
300 if (dev_of_offset(dev
) == -1) {
301 /* Tell the uclass how many GPIOs we have */
302 uc_priv
->gpio_count
= LPC32XX_GPIOS
;
305 /* set base address for GPIO registers */
306 gpio_priv
->regs
= (struct gpio_regs
*)GPIO_BASE
;
308 /* all GPIO functions are unknown until requested */
309 /* GPIO FUNCTION: SEE WARNING #2 */
310 memset(gpio_priv
->function
, GPIOF_UNKNOWN
, sizeof(gpio_priv
->function
));
315 U_BOOT_DRIVER(gpio_lpc32xx
) = {
316 .name
= "gpio_lpc32xx",
318 .ops
= &gpio_lpc32xx_ops
,
319 .probe
= lpc32xx_gpio_probe
,
320 .priv_auto_alloc_size
= sizeof(struct lpc32xx_gpio_priv
),