blob: 4b385b8b395b6526bafb88b8cc1a259681a805f7 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +02002/*
3 * (C) Copyright 2016
Mario Sixb4893582018-03-06 08:04:58 +01004 * Mario Six, Guntermann & Drunck GmbH, mario.six@gdsys.cc
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +02005 *
6 * based on arch/powerpc/include/asm/mpc85xx_gpio.h, which is
7 *
8 * Copyright 2010 eXMeritus, A Boeing Company
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +02009 */
10
11#include <common.h>
12#include <dm.h>
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +020013#include <mapmem.h>
Mario Sixd7c6f712018-01-15 11:07:49 +010014#include <asm/gpio.h>
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +020015
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +020016struct ccsr_gpio {
17 u32 gpdir;
18 u32 gpodr;
19 u32 gpdat;
20 u32 gpier;
21 u32 gpimr;
22 u32 gpicr;
23};
24
Mario Sixd006f682018-01-15 11:07:48 +010025struct mpc8xxx_gpio_data {
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +020026 /* The bank's register base in memory */
27 struct ccsr_gpio __iomem *base;
28 /* The address of the registers; used to identify the bank */
29 ulong addr;
30 /* The GPIO count of the bank */
31 uint gpio_count;
32 /* The GPDAT register cannot be used to determine the value of output
33 * pins on MPC8572/MPC8536, so we shadow it and use the shadowed value
Mario Sixcc94b172018-01-15 11:07:46 +010034 * for output pins
35 */
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +020036 u32 dat_shadow;
Mario Sixd7c6f712018-01-15 11:07:49 +010037 ulong type;
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +020038};
39
Mario Sixd7c6f712018-01-15 11:07:49 +010040enum {
41 MPC8XXX_GPIO_TYPE,
42 MPC5121_GPIO_TYPE,
43};
44
Mario Sixcc94b172018-01-15 11:07:46 +010045inline u32 gpio_mask(uint gpio)
46{
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +020047 return (1U << (31 - (gpio)));
48}
49
Mario Sixd006f682018-01-15 11:07:48 +010050static inline u32 mpc8xxx_gpio_get_val(struct ccsr_gpio *base, u32 mask)
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +020051{
52 return in_be32(&base->gpdat) & mask;
53}
54
Mario Sixd006f682018-01-15 11:07:48 +010055static inline u32 mpc8xxx_gpio_get_dir(struct ccsr_gpio *base, u32 mask)
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +020056{
57 return in_be32(&base->gpdir) & mask;
58}
59
Mario Sixd006f682018-01-15 11:07:48 +010060static inline int mpc8xxx_gpio_open_drain_val(struct ccsr_gpio *base, u32 mask)
mario.six@gdsys.cc7b4cf8b2016-05-25 15:15:22 +020061{
62 return in_be32(&base->gpodr) & mask;
63}
64
Mario Sixd006f682018-01-15 11:07:48 +010065static inline void mpc8xxx_gpio_open_drain_on(struct ccsr_gpio *base, u32
mario.six@gdsys.cc7b4cf8b2016-05-25 15:15:22 +020066 gpios)
67{
68 /* GPODR register 1 -> open drain on */
69 setbits_be32(&base->gpodr, gpios);
70}
71
Mario Sixd006f682018-01-15 11:07:48 +010072static inline void mpc8xxx_gpio_open_drain_off(struct ccsr_gpio *base,
mario.six@gdsys.cc7b4cf8b2016-05-25 15:15:22 +020073 u32 gpios)
74{
75 /* GPODR register 0 -> open drain off (actively driven) */
76 clrbits_be32(&base->gpodr, gpios);
77}
78
Mario Sixd006f682018-01-15 11:07:48 +010079static int mpc8xxx_gpio_direction_input(struct udevice *dev, uint gpio)
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +020080{
Mario Sixd006f682018-01-15 11:07:48 +010081 struct mpc8xxx_gpio_data *data = dev_get_priv(dev);
Rasmus Villemoesbb36b0d2020-01-28 12:04:33 +000082 u32 mask = gpio_mask(gpio);
83
84 /* GPDIR register 0 -> input */
85 clrbits_be32(&data->base->gpdir, mask);
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +020086
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +020087 return 0;
88}
89
Mario Sixd006f682018-01-15 11:07:48 +010090static int mpc8xxx_gpio_set_value(struct udevice *dev, uint gpio, int value)
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +020091{
Mario Sixd006f682018-01-15 11:07:48 +010092 struct mpc8xxx_gpio_data *data = dev_get_priv(dev);
Rasmus Villemoes62985292020-01-28 12:04:34 +000093 struct ccsr_gpio *base = data->base;
94 u32 mask = gpio_mask(gpio);
95 u32 gpdir;
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +020096
97 if (value) {
Rasmus Villemoes62985292020-01-28 12:04:34 +000098 data->dat_shadow |= mask;
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +020099 } else {
Rasmus Villemoes62985292020-01-28 12:04:34 +0000100 data->dat_shadow &= ~mask;
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +0200101 }
Rasmus Villemoes62985292020-01-28 12:04:34 +0000102
103 gpdir = in_be32(&base->gpdir);
104 gpdir |= gpio_mask(gpio);
105 out_be32(&base->gpdat, gpdir & data->dat_shadow);
106 out_be32(&base->gpdir, gpdir);
107
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +0200108 return 0;
109}
110
Mario Sixd006f682018-01-15 11:07:48 +0100111static int mpc8xxx_gpio_direction_output(struct udevice *dev, uint gpio,
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +0200112 int value)
113{
Mario Sixd7c6f712018-01-15 11:07:49 +0100114 struct mpc8xxx_gpio_data *data = dev_get_priv(dev);
115
116 /* GPIO 28..31 are input only on MPC5121 */
117 if (data->type == MPC5121_GPIO_TYPE && gpio >= 28)
118 return -EINVAL;
119
Mario Sixd006f682018-01-15 11:07:48 +0100120 return mpc8xxx_gpio_set_value(dev, gpio, value);
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +0200121}
122
Mario Sixd006f682018-01-15 11:07:48 +0100123static int mpc8xxx_gpio_get_value(struct udevice *dev, uint gpio)
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +0200124{
Mario Sixd006f682018-01-15 11:07:48 +0100125 struct mpc8xxx_gpio_data *data = dev_get_priv(dev);
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +0200126
Mario Sixd006f682018-01-15 11:07:48 +0100127 if (!!mpc8xxx_gpio_get_dir(data->base, gpio_mask(gpio))) {
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +0200128 /* Output -> use shadowed value */
129 return !!(data->dat_shadow & gpio_mask(gpio));
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +0200130 }
Mario Sixcc94b172018-01-15 11:07:46 +0100131
132 /* Input -> read value from GPDAT register */
Mario Sixd006f682018-01-15 11:07:48 +0100133 return !!mpc8xxx_gpio_get_val(data->base, gpio_mask(gpio));
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +0200134}
135
Mario Sixd006f682018-01-15 11:07:48 +0100136static int mpc8xxx_gpio_get_open_drain(struct udevice *dev, uint gpio)
mario.six@gdsys.cc7b4cf8b2016-05-25 15:15:22 +0200137{
Mario Sixd006f682018-01-15 11:07:48 +0100138 struct mpc8xxx_gpio_data *data = dev_get_priv(dev);
mario.six@gdsys.cc7b4cf8b2016-05-25 15:15:22 +0200139
Mario Sixd006f682018-01-15 11:07:48 +0100140 return !!mpc8xxx_gpio_open_drain_val(data->base, gpio_mask(gpio));
mario.six@gdsys.cc7b4cf8b2016-05-25 15:15:22 +0200141}
142
Mario Sixd006f682018-01-15 11:07:48 +0100143static int mpc8xxx_gpio_set_open_drain(struct udevice *dev, uint gpio,
mario.six@gdsys.cc7b4cf8b2016-05-25 15:15:22 +0200144 int value)
145{
Mario Sixd006f682018-01-15 11:07:48 +0100146 struct mpc8xxx_gpio_data *data = dev_get_priv(dev);
mario.six@gdsys.cc7b4cf8b2016-05-25 15:15:22 +0200147
Mario Sixcc94b172018-01-15 11:07:46 +0100148 if (value)
Mario Sixd006f682018-01-15 11:07:48 +0100149 mpc8xxx_gpio_open_drain_on(data->base, gpio_mask(gpio));
Mario Sixcc94b172018-01-15 11:07:46 +0100150 else
Mario Sixd006f682018-01-15 11:07:48 +0100151 mpc8xxx_gpio_open_drain_off(data->base, gpio_mask(gpio));
Mario Sixcc94b172018-01-15 11:07:46 +0100152
mario.six@gdsys.cc7b4cf8b2016-05-25 15:15:22 +0200153 return 0;
154}
155
Mario Sixd006f682018-01-15 11:07:48 +0100156static int mpc8xxx_gpio_get_function(struct udevice *dev, uint gpio)
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +0200157{
Mario Sixd006f682018-01-15 11:07:48 +0100158 struct mpc8xxx_gpio_data *data = dev_get_priv(dev);
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +0200159 int dir;
160
Mario Sixd006f682018-01-15 11:07:48 +0100161 dir = !!mpc8xxx_gpio_get_dir(data->base, gpio_mask(gpio));
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +0200162 return dir ? GPIOF_OUTPUT : GPIOF_INPUT;
163}
164
Hamish Martin47ca9fc2016-06-14 10:17:05 +1200165#if CONFIG_IS_ENABLED(OF_CONTROL)
Mario Sixd006f682018-01-15 11:07:48 +0100166static int mpc8xxx_gpio_ofdata_to_platdata(struct udevice *dev)
Mario Sixcc94b172018-01-15 11:07:46 +0100167{
Mario Sixd006f682018-01-15 11:07:48 +0100168 struct mpc8xxx_gpio_plat *plat = dev_get_platdata(dev);
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +0200169 fdt_addr_t addr;
Mario Sixf99b6bf2018-01-15 11:07:50 +0100170 u32 reg[2];
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +0200171
Mario Sixf99b6bf2018-01-15 11:07:50 +0100172 dev_read_u32_array(dev, "reg", reg, 2);
173 addr = dev_translate_address(dev, reg);
174
Hamish Martin47ca9fc2016-06-14 10:17:05 +1200175 plat->addr = addr;
Mario Sixf99b6bf2018-01-15 11:07:50 +0100176 plat->size = reg[1];
177 plat->ngpios = dev_read_u32_default(dev, "ngpios", 32);
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +0200178
Hamish Martin47ca9fc2016-06-14 10:17:05 +1200179 return 0;
180}
181#endif
182
Mario Sixd006f682018-01-15 11:07:48 +0100183static int mpc8xxx_gpio_platdata_to_priv(struct udevice *dev)
Hamish Martin47ca9fc2016-06-14 10:17:05 +1200184{
Mario Sixd006f682018-01-15 11:07:48 +0100185 struct mpc8xxx_gpio_data *priv = dev_get_priv(dev);
186 struct mpc8xxx_gpio_plat *plat = dev_get_platdata(dev);
Hamish Martin47ca9fc2016-06-14 10:17:05 +1200187 unsigned long size = plat->size;
Mario Sixd7c6f712018-01-15 11:07:49 +0100188 ulong driver_data = dev_get_driver_data(dev);
Hamish Martin47ca9fc2016-06-14 10:17:05 +1200189
190 if (size == 0)
191 size = 0x100;
192
193 priv->addr = plat->addr;
Mario Sixf99b6bf2018-01-15 11:07:50 +0100194 priv->base = map_sysmem(plat->addr, size);
Hamish Martin47ca9fc2016-06-14 10:17:05 +1200195
196 if (!priv->base)
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +0200197 return -ENOMEM;
198
Hamish Martin47ca9fc2016-06-14 10:17:05 +1200199 priv->gpio_count = plat->ngpios;
200 priv->dat_shadow = 0;
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +0200201
Mario Sixd006f682018-01-15 11:07:48 +0100202 priv->type = driver_data;
203
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +0200204 return 0;
205}
206
Mario Sixd006f682018-01-15 11:07:48 +0100207static int mpc8xxx_gpio_probe(struct udevice *dev)
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +0200208{
209 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Mario Sixd006f682018-01-15 11:07:48 +0100210 struct mpc8xxx_gpio_data *data = dev_get_priv(dev);
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +0200211 char name[32], *str;
212
Mario Sixd006f682018-01-15 11:07:48 +0100213 mpc8xxx_gpio_platdata_to_priv(dev);
Hamish Martin47ca9fc2016-06-14 10:17:05 +1200214
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +0200215 snprintf(name, sizeof(name), "MPC@%lx_", data->addr);
216 str = strdup(name);
217
218 if (!str)
219 return -ENOMEM;
220
221 uc_priv->bank_name = str;
222 uc_priv->gpio_count = data->gpio_count;
223
224 return 0;
225}
226
Mario Sixd006f682018-01-15 11:07:48 +0100227static const struct dm_gpio_ops gpio_mpc8xxx_ops = {
228 .direction_input = mpc8xxx_gpio_direction_input,
229 .direction_output = mpc8xxx_gpio_direction_output,
230 .get_value = mpc8xxx_gpio_get_value,
231 .set_value = mpc8xxx_gpio_set_value,
232 .get_open_drain = mpc8xxx_gpio_get_open_drain,
233 .set_open_drain = mpc8xxx_gpio_set_open_drain,
234 .get_function = mpc8xxx_gpio_get_function,
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +0200235};
236
Mario Sixd006f682018-01-15 11:07:48 +0100237static const struct udevice_id mpc8xxx_gpio_ids[] = {
Mario Sixd7c6f712018-01-15 11:07:49 +0100238 { .compatible = "fsl,pq3-gpio", .data = MPC8XXX_GPIO_TYPE },
239 { .compatible = "fsl,mpc8308-gpio", .data = MPC8XXX_GPIO_TYPE },
240 { .compatible = "fsl,mpc8349-gpio", .data = MPC8XXX_GPIO_TYPE },
241 { .compatible = "fsl,mpc8572-gpio", .data = MPC8XXX_GPIO_TYPE},
242 { .compatible = "fsl,mpc8610-gpio", .data = MPC8XXX_GPIO_TYPE},
243 { .compatible = "fsl,mpc5121-gpio", .data = MPC5121_GPIO_TYPE, },
244 { .compatible = "fsl,qoriq-gpio", .data = MPC8XXX_GPIO_TYPE },
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +0200245 { /* sentinel */ }
246};
247
Mario Sixd006f682018-01-15 11:07:48 +0100248U_BOOT_DRIVER(gpio_mpc8xxx) = {
249 .name = "gpio_mpc8xxx",
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +0200250 .id = UCLASS_GPIO,
Mario Sixd006f682018-01-15 11:07:48 +0100251 .ops = &gpio_mpc8xxx_ops,
Hamish Martin47ca9fc2016-06-14 10:17:05 +1200252#if CONFIG_IS_ENABLED(OF_CONTROL)
Mario Sixd006f682018-01-15 11:07:48 +0100253 .ofdata_to_platdata = mpc8xxx_gpio_ofdata_to_platdata,
254 .platdata_auto_alloc_size = sizeof(struct mpc8xxx_gpio_plat),
255 .of_match = mpc8xxx_gpio_ids,
Hamish Martin47ca9fc2016-06-14 10:17:05 +1200256#endif
Mario Sixd006f682018-01-15 11:07:48 +0100257 .probe = mpc8xxx_gpio_probe,
258 .priv_auto_alloc_size = sizeof(struct mpc8xxx_gpio_data),
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +0200259};