3 * Mario Six, Guntermann & Drunck GmbH, mario.six@gdsys.cc
5 * based on arch/powerpc/include/asm/mpc85xx_gpio.h, which is
7 * Copyright 2010 eXMeritus, A Boeing Company
9 * SPDX-License-Identifier: GPL-2.0+
26 struct mpc8xxx_gpio_data {
27 /* The bank's register base in memory */
28 struct ccsr_gpio __iomem *base;
29 /* The address of the registers; used to identify the bank */
31 /* The GPIO count of the bank */
33 /* The GPDAT register cannot be used to determine the value of output
34 * pins on MPC8572/MPC8536, so we shadow it and use the shadowed value
46 inline u32 gpio_mask(uint gpio)
48 return (1U << (31 - (gpio)));
51 static inline u32 mpc8xxx_gpio_get_val(struct ccsr_gpio *base, u32 mask)
53 return in_be32(&base->gpdat) & mask;
56 static inline u32 mpc8xxx_gpio_get_dir(struct ccsr_gpio *base, u32 mask)
58 return in_be32(&base->gpdir) & mask;
61 static inline void mpc8xxx_gpio_set_in(struct ccsr_gpio *base, u32 gpios)
63 clrbits_be32(&base->gpdat, gpios);
64 /* GPDIR register 0 -> input */
65 clrbits_be32(&base->gpdir, gpios);
68 static inline void mpc8xxx_gpio_set_low(struct ccsr_gpio *base, u32 gpios)
70 clrbits_be32(&base->gpdat, gpios);
71 /* GPDIR register 1 -> output */
72 setbits_be32(&base->gpdir, gpios);
75 static inline void mpc8xxx_gpio_set_high(struct ccsr_gpio *base, u32 gpios)
77 setbits_be32(&base->gpdat, gpios);
78 /* GPDIR register 1 -> output */
79 setbits_be32(&base->gpdir, gpios);
82 static inline int mpc8xxx_gpio_open_drain_val(struct ccsr_gpio *base, u32 mask)
84 return in_be32(&base->gpodr) & mask;
87 static inline void mpc8xxx_gpio_open_drain_on(struct ccsr_gpio *base, u32
90 /* GPODR register 1 -> open drain on */
91 setbits_be32(&base->gpodr, gpios);
94 static inline void mpc8xxx_gpio_open_drain_off(struct ccsr_gpio *base,
97 /* GPODR register 0 -> open drain off (actively driven) */
98 clrbits_be32(&base->gpodr, gpios);
101 static int mpc8xxx_gpio_direction_input(struct udevice *dev, uint gpio)
103 struct mpc8xxx_gpio_data *data = dev_get_priv(dev);
105 mpc8xxx_gpio_set_in(data->base, gpio_mask(gpio));
109 static int mpc8xxx_gpio_set_value(struct udevice *dev, uint gpio, int value)
111 struct mpc8xxx_gpio_data *data = dev_get_priv(dev);
114 data->dat_shadow |= gpio_mask(gpio);
115 mpc8xxx_gpio_set_high(data->base, gpio_mask(gpio));
117 data->dat_shadow &= ~gpio_mask(gpio);
118 mpc8xxx_gpio_set_low(data->base, gpio_mask(gpio));
123 static int mpc8xxx_gpio_direction_output(struct udevice *dev, uint gpio,
126 struct mpc8xxx_gpio_data *data = dev_get_priv(dev);
128 /* GPIO 28..31 are input only on MPC5121 */
129 if (data->type == MPC5121_GPIO_TYPE && gpio >= 28)
132 return mpc8xxx_gpio_set_value(dev, gpio, value);
135 static int mpc8xxx_gpio_get_value(struct udevice *dev, uint gpio)
137 struct mpc8xxx_gpio_data *data = dev_get_priv(dev);
139 if (!!mpc8xxx_gpio_get_dir(data->base, gpio_mask(gpio))) {
140 /* Output -> use shadowed value */
141 return !!(data->dat_shadow & gpio_mask(gpio));
144 /* Input -> read value from GPDAT register */
145 return !!mpc8xxx_gpio_get_val(data->base, gpio_mask(gpio));
148 static int mpc8xxx_gpio_get_open_drain(struct udevice *dev, uint gpio)
150 struct mpc8xxx_gpio_data *data = dev_get_priv(dev);
152 return !!mpc8xxx_gpio_open_drain_val(data->base, gpio_mask(gpio));
155 static int mpc8xxx_gpio_set_open_drain(struct udevice *dev, uint gpio,
158 struct mpc8xxx_gpio_data *data = dev_get_priv(dev);
161 mpc8xxx_gpio_open_drain_on(data->base, gpio_mask(gpio));
163 mpc8xxx_gpio_open_drain_off(data->base, gpio_mask(gpio));
168 static int mpc8xxx_gpio_get_function(struct udevice *dev, uint gpio)
170 struct mpc8xxx_gpio_data *data = dev_get_priv(dev);
173 dir = !!mpc8xxx_gpio_get_dir(data->base, gpio_mask(gpio));
174 return dir ? GPIOF_OUTPUT : GPIOF_INPUT;
177 #if CONFIG_IS_ENABLED(OF_CONTROL)
178 static int mpc8xxx_gpio_ofdata_to_platdata(struct udevice *dev)
180 struct mpc8xxx_gpio_plat *plat = dev_get_platdata(dev);
184 dev_read_u32_array(dev, "reg", reg, 2);
185 addr = dev_translate_address(dev, reg);
189 plat->ngpios = dev_read_u32_default(dev, "ngpios", 32);
195 static int mpc8xxx_gpio_platdata_to_priv(struct udevice *dev)
197 struct mpc8xxx_gpio_data *priv = dev_get_priv(dev);
198 struct mpc8xxx_gpio_plat *plat = dev_get_platdata(dev);
199 unsigned long size = plat->size;
200 ulong driver_data = dev_get_driver_data(dev);
205 priv->addr = plat->addr;
206 priv->base = map_sysmem(plat->addr, size);
211 priv->gpio_count = plat->ngpios;
212 priv->dat_shadow = 0;
214 priv->type = driver_data;
219 static int mpc8xxx_gpio_probe(struct udevice *dev)
221 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
222 struct mpc8xxx_gpio_data *data = dev_get_priv(dev);
225 mpc8xxx_gpio_platdata_to_priv(dev);
227 snprintf(name, sizeof(name), "MPC@%lx_", data->addr);
233 uc_priv->bank_name = str;
234 uc_priv->gpio_count = data->gpio_count;
239 static const struct dm_gpio_ops gpio_mpc8xxx_ops = {
240 .direction_input = mpc8xxx_gpio_direction_input,
241 .direction_output = mpc8xxx_gpio_direction_output,
242 .get_value = mpc8xxx_gpio_get_value,
243 .set_value = mpc8xxx_gpio_set_value,
244 .get_open_drain = mpc8xxx_gpio_get_open_drain,
245 .set_open_drain = mpc8xxx_gpio_set_open_drain,
246 .get_function = mpc8xxx_gpio_get_function,
249 static const struct udevice_id mpc8xxx_gpio_ids[] = {
250 { .compatible = "fsl,pq3-gpio", .data = MPC8XXX_GPIO_TYPE },
251 { .compatible = "fsl,mpc8308-gpio", .data = MPC8XXX_GPIO_TYPE },
252 { .compatible = "fsl,mpc8349-gpio", .data = MPC8XXX_GPIO_TYPE },
253 { .compatible = "fsl,mpc8572-gpio", .data = MPC8XXX_GPIO_TYPE},
254 { .compatible = "fsl,mpc8610-gpio", .data = MPC8XXX_GPIO_TYPE},
255 { .compatible = "fsl,mpc5121-gpio", .data = MPC5121_GPIO_TYPE, },
256 { .compatible = "fsl,qoriq-gpio", .data = MPC8XXX_GPIO_TYPE },
260 U_BOOT_DRIVER(gpio_mpc8xxx) = {
261 .name = "gpio_mpc8xxx",
263 .ops = &gpio_mpc8xxx_ops,
264 #if CONFIG_IS_ENABLED(OF_CONTROL)
265 .ofdata_to_platdata = mpc8xxx_gpio_ofdata_to_platdata,
266 .platdata_auto_alloc_size = sizeof(struct mpc8xxx_gpio_plat),
267 .of_match = mpc8xxx_gpio_ids,
269 .probe = mpc8xxx_gpio_probe,
270 .priv_auto_alloc_size = sizeof(struct mpc8xxx_gpio_data),