1 // SPDX-License-Identifier: GPL-2.0+
4 * Mario Six, Guntermann & Drunck GmbH, mario.six@gdsys.cc
6 * based on arch/powerpc/include/asm/mpc85xx_gpio.h, which is
8 * Copyright 2010 eXMeritus, A Boeing Company
9 * Copyright 2020-2021 NXP
17 #include <dm/of_access.h>
19 struct mpc8xxx_gpio_data {
20 /* The bank's register base in memory */
21 struct ccsr_gpio __iomem *base;
22 /* The address of the registers; used to identify the bank */
24 /* The GPIO count of the bank */
26 /* The GPDAT register cannot be used to determine the value of output
27 * pins on MPC8572/MPC8536, so we shadow it and use the shadowed value
40 inline u32 gpio_mask(uint gpio)
42 return (1U << (31 - (gpio)));
45 static inline u32 mpc8xxx_gpio_get_val(struct udevice *dev, u32 mask)
47 struct mpc8xxx_gpio_data *data = dev_get_priv(dev);
49 if (data->little_endian)
50 return in_le32(&data->base->gpdat) & mask;
52 return in_be32(&data->base->gpdat) & mask;
55 static inline u32 mpc8xxx_gpio_get_dir(struct udevice *dev, u32 mask)
57 struct mpc8xxx_gpio_data *data = dev_get_priv(dev);
59 if (data->little_endian)
60 return in_le32(&data->base->gpdir) & mask;
62 return in_be32(&data->base->gpdir) & mask;
65 static inline int mpc8xxx_gpio_open_drain_val(struct udevice *dev, u32 mask)
67 struct mpc8xxx_gpio_data *data = dev_get_priv(dev);
69 if (data->little_endian)
70 return in_le32(&data->base->gpodr) & mask;
72 return in_be32(&data->base->gpodr) & mask;
75 static inline void mpc8xxx_gpio_open_drain_on(struct udevice *dev, u32
78 struct mpc8xxx_gpio_data *data = dev_get_priv(dev);
79 /* GPODR register 1 -> open drain on */
80 if (data->little_endian)
81 setbits_le32(&data->base->gpodr, gpios);
83 setbits_be32(&data->base->gpodr, gpios);
86 static inline void mpc8xxx_gpio_open_drain_off(struct udevice *dev,
89 struct mpc8xxx_gpio_data *data = dev_get_priv(dev);
90 /* GPODR register 0 -> open drain off (actively driven) */
91 if (data->little_endian)
92 clrbits_le32(&data->base->gpodr, gpios);
94 clrbits_be32(&data->base->gpodr, gpios);
97 static int mpc8xxx_gpio_direction_input(struct udevice *dev, uint gpio)
99 struct mpc8xxx_gpio_data *data = dev_get_priv(dev);
100 u32 mask = gpio_mask(gpio);
102 /* GPDIR register 0 -> input */
103 if (data->little_endian)
104 clrbits_le32(&data->base->gpdir, mask);
106 clrbits_be32(&data->base->gpdir, mask);
111 static int mpc8xxx_gpio_set_value(struct udevice *dev, uint gpio, int value)
113 struct mpc8xxx_gpio_data *data = dev_get_priv(dev);
114 struct ccsr_gpio *base = data->base;
115 u32 mask = gpio_mask(gpio);
119 data->dat_shadow |= mask;
121 data->dat_shadow &= ~mask;
124 if (data->little_endian)
125 gpdir = in_le32(&base->gpdir);
127 gpdir = in_be32(&base->gpdir);
129 gpdir |= gpio_mask(gpio);
131 if (data->little_endian) {
132 out_le32(&base->gpdat, gpdir & data->dat_shadow);
133 out_le32(&base->gpdir, gpdir);
135 out_be32(&base->gpdat, gpdir & data->dat_shadow);
136 out_be32(&base->gpdir, gpdir);
142 static int mpc8xxx_gpio_direction_output(struct udevice *dev, uint gpio,
145 struct mpc8xxx_gpio_data *data = dev_get_priv(dev);
147 /* GPIO 28..31 are input only on MPC5121 */
148 if (data->type == MPC5121_GPIO_TYPE && gpio >= 28)
151 return mpc8xxx_gpio_set_value(dev, gpio, value);
154 static int mpc8xxx_gpio_get_value(struct udevice *dev, uint gpio)
156 struct mpc8xxx_gpio_data *data = dev_get_priv(dev);
158 if (!!mpc8xxx_gpio_get_dir(dev, gpio_mask(gpio))) {
159 /* Output -> use shadowed value */
160 return !!(data->dat_shadow & gpio_mask(gpio));
163 /* Input -> read value from GPDAT register */
164 return !!mpc8xxx_gpio_get_val(dev, gpio_mask(gpio));
167 static int mpc8xxx_gpio_get_function(struct udevice *dev, uint gpio)
171 dir = !!mpc8xxx_gpio_get_dir(dev, gpio_mask(gpio));
172 return dir ? GPIOF_OUTPUT : GPIOF_INPUT;
175 #if CONFIG_IS_ENABLED(OF_CONTROL)
176 static int mpc8xxx_gpio_of_to_plat(struct udevice *dev)
178 struct mpc8xxx_gpio_plat *plat = dev_get_plat(dev);
179 struct mpc8xxx_gpio_data *data = dev_get_priv(dev);
181 if (dev_read_bool(dev, "little-endian"))
182 data->little_endian = true;
184 plat->addr = dev_read_addr_size_index(dev, 0, (fdt_size_t *)&plat->size);
185 plat->ngpios = dev_read_u32_default(dev, "ngpios", 32);
191 static int mpc8xxx_gpio_plat_to_priv(struct udevice *dev)
193 struct mpc8xxx_gpio_data *priv = dev_get_priv(dev);
194 struct mpc8xxx_gpio_plat *plat = dev_get_plat(dev);
195 unsigned long size = plat->size;
196 ulong driver_data = dev_get_driver_data(dev);
201 priv->addr = plat->addr;
202 priv->base = map_sysmem(plat->addr, size);
207 priv->gpio_count = plat->ngpios;
208 priv->dat_shadow = 0;
210 priv->type = driver_data;
215 static int mpc8xxx_gpio_probe(struct udevice *dev)
217 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
218 struct mpc8xxx_gpio_data *data = dev_get_priv(dev);
221 mpc8xxx_gpio_plat_to_priv(dev);
223 snprintf(name, sizeof(name), "MPC@%.8llx",
224 (unsigned long long)data->addr);
230 if (device_is_compatible(dev, "fsl,qoriq-gpio")) {
231 if (data->little_endian)
232 out_le32(&data->base->gpibe, 0xffffffff);
234 out_be32(&data->base->gpibe, 0xffffffff);
237 uc_priv->bank_name = str;
238 uc_priv->gpio_count = data->gpio_count;
243 static const struct dm_gpio_ops gpio_mpc8xxx_ops = {
244 .direction_input = mpc8xxx_gpio_direction_input,
245 .direction_output = mpc8xxx_gpio_direction_output,
246 .get_value = mpc8xxx_gpio_get_value,
247 .set_value = mpc8xxx_gpio_set_value,
248 .get_function = mpc8xxx_gpio_get_function,
251 static const struct udevice_id mpc8xxx_gpio_ids[] = {
252 { .compatible = "fsl,pq3-gpio", .data = MPC8XXX_GPIO_TYPE },
253 { .compatible = "fsl,mpc8308-gpio", .data = MPC8XXX_GPIO_TYPE },
254 { .compatible = "fsl,mpc8349-gpio", .data = MPC8XXX_GPIO_TYPE },
255 { .compatible = "fsl,mpc8572-gpio", .data = MPC8XXX_GPIO_TYPE},
256 { .compatible = "fsl,mpc8610-gpio", .data = MPC8XXX_GPIO_TYPE},
257 { .compatible = "fsl,mpc5121-gpio", .data = MPC5121_GPIO_TYPE, },
258 { .compatible = "fsl,qoriq-gpio", .data = MPC8XXX_GPIO_TYPE },
262 U_BOOT_DRIVER(gpio_mpc8xxx) = {
263 .name = "gpio_mpc8xxx",
265 .ops = &gpio_mpc8xxx_ops,
266 #if CONFIG_IS_ENABLED(OF_CONTROL)
267 .of_to_plat = mpc8xxx_gpio_of_to_plat,
268 .plat_auto = sizeof(struct mpc8xxx_gpio_plat),
269 .of_match = mpc8xxx_gpio_ids,
271 .probe = mpc8xxx_gpio_probe,
272 .priv_auto = sizeof(struct mpc8xxx_gpio_data),