blob: c7acf116597336b189e57ba3e7a49cdd98b99922 [file] [log] [blame]
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +02001/*
2 * (C) Copyright 2016
Mario Sixb4893582018-03-06 08:04:58 +01003 * Mario Six, Guntermann & Drunck GmbH, mario.six@gdsys.cc
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +02004 *
5 * based on arch/powerpc/include/asm/mpc85xx_gpio.h, which is
6 *
7 * Copyright 2010 eXMeritus, A Boeing Company
8 *
9 * SPDX-License-Identifier: GPL-2.0+
10 */
11
12#include <common.h>
13#include <dm.h>
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +020014#include <mapmem.h>
Mario Sixd7c6f712018-01-15 11:07:49 +010015#include <asm/gpio.h>
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +020016
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +020017struct ccsr_gpio {
18 u32 gpdir;
19 u32 gpodr;
20 u32 gpdat;
21 u32 gpier;
22 u32 gpimr;
23 u32 gpicr;
24};
25
Mario Sixd006f682018-01-15 11:07:48 +010026struct mpc8xxx_gpio_data {
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +020027 /* The bank's register base in memory */
28 struct ccsr_gpio __iomem *base;
29 /* The address of the registers; used to identify the bank */
30 ulong addr;
31 /* The GPIO count of the bank */
32 uint gpio_count;
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
Mario Sixcc94b172018-01-15 11:07:46 +010035 * for output pins
36 */
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +020037 u32 dat_shadow;
Mario Sixd7c6f712018-01-15 11:07:49 +010038 ulong type;
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +020039};
40
Mario Sixd7c6f712018-01-15 11:07:49 +010041enum {
42 MPC8XXX_GPIO_TYPE,
43 MPC5121_GPIO_TYPE,
44};
45
Mario Sixcc94b172018-01-15 11:07:46 +010046inline u32 gpio_mask(uint gpio)
47{
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +020048 return (1U << (31 - (gpio)));
49}
50
Mario Sixd006f682018-01-15 11:07:48 +010051static inline u32 mpc8xxx_gpio_get_val(struct ccsr_gpio *base, u32 mask)
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +020052{
53 return in_be32(&base->gpdat) & mask;
54}
55
Mario Sixd006f682018-01-15 11:07:48 +010056static inline u32 mpc8xxx_gpio_get_dir(struct ccsr_gpio *base, u32 mask)
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +020057{
58 return in_be32(&base->gpdir) & mask;
59}
60
Mario Sixd006f682018-01-15 11:07:48 +010061static inline void mpc8xxx_gpio_set_in(struct ccsr_gpio *base, u32 gpios)
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +020062{
63 clrbits_be32(&base->gpdat, gpios);
64 /* GPDIR register 0 -> input */
65 clrbits_be32(&base->gpdir, gpios);
66}
67
Mario Sixd006f682018-01-15 11:07:48 +010068static inline void mpc8xxx_gpio_set_low(struct ccsr_gpio *base, u32 gpios)
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +020069{
70 clrbits_be32(&base->gpdat, gpios);
71 /* GPDIR register 1 -> output */
72 setbits_be32(&base->gpdir, gpios);
73}
74
Mario Sixd006f682018-01-15 11:07:48 +010075static inline void mpc8xxx_gpio_set_high(struct ccsr_gpio *base, u32 gpios)
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +020076{
77 setbits_be32(&base->gpdat, gpios);
78 /* GPDIR register 1 -> output */
79 setbits_be32(&base->gpdir, gpios);
80}
81
Mario Sixd006f682018-01-15 11:07:48 +010082static inline int mpc8xxx_gpio_open_drain_val(struct ccsr_gpio *base, u32 mask)
mario.six@gdsys.cc7b4cf8b2016-05-25 15:15:22 +020083{
84 return in_be32(&base->gpodr) & mask;
85}
86
Mario Sixd006f682018-01-15 11:07:48 +010087static inline void mpc8xxx_gpio_open_drain_on(struct ccsr_gpio *base, u32
mario.six@gdsys.cc7b4cf8b2016-05-25 15:15:22 +020088 gpios)
89{
90 /* GPODR register 1 -> open drain on */
91 setbits_be32(&base->gpodr, gpios);
92}
93
Mario Sixd006f682018-01-15 11:07:48 +010094static inline void mpc8xxx_gpio_open_drain_off(struct ccsr_gpio *base,
mario.six@gdsys.cc7b4cf8b2016-05-25 15:15:22 +020095 u32 gpios)
96{
97 /* GPODR register 0 -> open drain off (actively driven) */
98 clrbits_be32(&base->gpodr, gpios);
99}
100
Mario Sixd006f682018-01-15 11:07:48 +0100101static int mpc8xxx_gpio_direction_input(struct udevice *dev, uint gpio)
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +0200102{
Mario Sixd006f682018-01-15 11:07:48 +0100103 struct mpc8xxx_gpio_data *data = dev_get_priv(dev);
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +0200104
Mario Sixd006f682018-01-15 11:07:48 +0100105 mpc8xxx_gpio_set_in(data->base, gpio_mask(gpio));
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +0200106 return 0;
107}
108
Mario Sixd006f682018-01-15 11:07:48 +0100109static int mpc8xxx_gpio_set_value(struct udevice *dev, uint gpio, int value)
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +0200110{
Mario Sixd006f682018-01-15 11:07:48 +0100111 struct mpc8xxx_gpio_data *data = dev_get_priv(dev);
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +0200112
113 if (value) {
114 data->dat_shadow |= gpio_mask(gpio);
Mario Sixd006f682018-01-15 11:07:48 +0100115 mpc8xxx_gpio_set_high(data->base, gpio_mask(gpio));
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +0200116 } else {
117 data->dat_shadow &= ~gpio_mask(gpio);
Mario Sixd006f682018-01-15 11:07:48 +0100118 mpc8xxx_gpio_set_low(data->base, gpio_mask(gpio));
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +0200119 }
120 return 0;
121}
122
Mario Sixd006f682018-01-15 11:07:48 +0100123static int mpc8xxx_gpio_direction_output(struct udevice *dev, uint gpio,
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +0200124 int value)
125{
Mario Sixd7c6f712018-01-15 11:07:49 +0100126 struct mpc8xxx_gpio_data *data = dev_get_priv(dev);
127
128 /* GPIO 28..31 are input only on MPC5121 */
129 if (data->type == MPC5121_GPIO_TYPE && gpio >= 28)
130 return -EINVAL;
131
Mario Sixd006f682018-01-15 11:07:48 +0100132 return mpc8xxx_gpio_set_value(dev, gpio, value);
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +0200133}
134
Mario Sixd006f682018-01-15 11:07:48 +0100135static int mpc8xxx_gpio_get_value(struct udevice *dev, uint gpio)
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +0200136{
Mario Sixd006f682018-01-15 11:07:48 +0100137 struct mpc8xxx_gpio_data *data = dev_get_priv(dev);
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +0200138
Mario Sixd006f682018-01-15 11:07:48 +0100139 if (!!mpc8xxx_gpio_get_dir(data->base, gpio_mask(gpio))) {
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +0200140 /* Output -> use shadowed value */
141 return !!(data->dat_shadow & gpio_mask(gpio));
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +0200142 }
Mario Sixcc94b172018-01-15 11:07:46 +0100143
144 /* Input -> read value from GPDAT register */
Mario Sixd006f682018-01-15 11:07:48 +0100145 return !!mpc8xxx_gpio_get_val(data->base, gpio_mask(gpio));
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +0200146}
147
Mario Sixd006f682018-01-15 11:07:48 +0100148static int mpc8xxx_gpio_get_open_drain(struct udevice *dev, uint gpio)
mario.six@gdsys.cc7b4cf8b2016-05-25 15:15:22 +0200149{
Mario Sixd006f682018-01-15 11:07:48 +0100150 struct mpc8xxx_gpio_data *data = dev_get_priv(dev);
mario.six@gdsys.cc7b4cf8b2016-05-25 15:15:22 +0200151
Mario Sixd006f682018-01-15 11:07:48 +0100152 return !!mpc8xxx_gpio_open_drain_val(data->base, gpio_mask(gpio));
mario.six@gdsys.cc7b4cf8b2016-05-25 15:15:22 +0200153}
154
Mario Sixd006f682018-01-15 11:07:48 +0100155static int mpc8xxx_gpio_set_open_drain(struct udevice *dev, uint gpio,
mario.six@gdsys.cc7b4cf8b2016-05-25 15:15:22 +0200156 int value)
157{
Mario Sixd006f682018-01-15 11:07:48 +0100158 struct mpc8xxx_gpio_data *data = dev_get_priv(dev);
mario.six@gdsys.cc7b4cf8b2016-05-25 15:15:22 +0200159
Mario Sixcc94b172018-01-15 11:07:46 +0100160 if (value)
Mario Sixd006f682018-01-15 11:07:48 +0100161 mpc8xxx_gpio_open_drain_on(data->base, gpio_mask(gpio));
Mario Sixcc94b172018-01-15 11:07:46 +0100162 else
Mario Sixd006f682018-01-15 11:07:48 +0100163 mpc8xxx_gpio_open_drain_off(data->base, gpio_mask(gpio));
Mario Sixcc94b172018-01-15 11:07:46 +0100164
mario.six@gdsys.cc7b4cf8b2016-05-25 15:15:22 +0200165 return 0;
166}
167
Mario Sixd006f682018-01-15 11:07:48 +0100168static int mpc8xxx_gpio_get_function(struct udevice *dev, uint gpio)
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +0200169{
Mario Sixd006f682018-01-15 11:07:48 +0100170 struct mpc8xxx_gpio_data *data = dev_get_priv(dev);
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +0200171 int dir;
172
Mario Sixd006f682018-01-15 11:07:48 +0100173 dir = !!mpc8xxx_gpio_get_dir(data->base, gpio_mask(gpio));
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +0200174 return dir ? GPIOF_OUTPUT : GPIOF_INPUT;
175}
176
Hamish Martin47ca9fc2016-06-14 10:17:05 +1200177#if CONFIG_IS_ENABLED(OF_CONTROL)
Mario Sixd006f682018-01-15 11:07:48 +0100178static int mpc8xxx_gpio_ofdata_to_platdata(struct udevice *dev)
Mario Sixcc94b172018-01-15 11:07:46 +0100179{
Mario Sixd006f682018-01-15 11:07:48 +0100180 struct mpc8xxx_gpio_plat *plat = dev_get_platdata(dev);
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +0200181 fdt_addr_t addr;
Mario Sixf99b6bf2018-01-15 11:07:50 +0100182 u32 reg[2];
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +0200183
Mario Sixf99b6bf2018-01-15 11:07:50 +0100184 dev_read_u32_array(dev, "reg", reg, 2);
185 addr = dev_translate_address(dev, reg);
186
Hamish Martin47ca9fc2016-06-14 10:17:05 +1200187 plat->addr = addr;
Mario Sixf99b6bf2018-01-15 11:07:50 +0100188 plat->size = reg[1];
189 plat->ngpios = dev_read_u32_default(dev, "ngpios", 32);
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +0200190
Hamish Martin47ca9fc2016-06-14 10:17:05 +1200191 return 0;
192}
193#endif
194
Mario Sixd006f682018-01-15 11:07:48 +0100195static int mpc8xxx_gpio_platdata_to_priv(struct udevice *dev)
Hamish Martin47ca9fc2016-06-14 10:17:05 +1200196{
Mario Sixd006f682018-01-15 11:07:48 +0100197 struct mpc8xxx_gpio_data *priv = dev_get_priv(dev);
198 struct mpc8xxx_gpio_plat *plat = dev_get_platdata(dev);
Hamish Martin47ca9fc2016-06-14 10:17:05 +1200199 unsigned long size = plat->size;
Mario Sixd7c6f712018-01-15 11:07:49 +0100200 ulong driver_data = dev_get_driver_data(dev);
Hamish Martin47ca9fc2016-06-14 10:17:05 +1200201
202 if (size == 0)
203 size = 0x100;
204
205 priv->addr = plat->addr;
Mario Sixf99b6bf2018-01-15 11:07:50 +0100206 priv->base = map_sysmem(plat->addr, size);
Hamish Martin47ca9fc2016-06-14 10:17:05 +1200207
208 if (!priv->base)
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +0200209 return -ENOMEM;
210
Hamish Martin47ca9fc2016-06-14 10:17:05 +1200211 priv->gpio_count = plat->ngpios;
212 priv->dat_shadow = 0;
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +0200213
Mario Sixd006f682018-01-15 11:07:48 +0100214 priv->type = driver_data;
215
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +0200216 return 0;
217}
218
Mario Sixd006f682018-01-15 11:07:48 +0100219static int mpc8xxx_gpio_probe(struct udevice *dev)
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +0200220{
221 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Mario Sixd006f682018-01-15 11:07:48 +0100222 struct mpc8xxx_gpio_data *data = dev_get_priv(dev);
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +0200223 char name[32], *str;
224
Mario Sixd006f682018-01-15 11:07:48 +0100225 mpc8xxx_gpio_platdata_to_priv(dev);
Hamish Martin47ca9fc2016-06-14 10:17:05 +1200226
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +0200227 snprintf(name, sizeof(name), "MPC@%lx_", data->addr);
228 str = strdup(name);
229
230 if (!str)
231 return -ENOMEM;
232
233 uc_priv->bank_name = str;
234 uc_priv->gpio_count = data->gpio_count;
235
236 return 0;
237}
238
Mario Sixd006f682018-01-15 11:07:48 +0100239static 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,
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +0200247};
248
Mario Sixd006f682018-01-15 11:07:48 +0100249static const struct udevice_id mpc8xxx_gpio_ids[] = {
Mario Sixd7c6f712018-01-15 11:07:49 +0100250 { .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 },
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +0200257 { /* sentinel */ }
258};
259
Mario Sixd006f682018-01-15 11:07:48 +0100260U_BOOT_DRIVER(gpio_mpc8xxx) = {
261 .name = "gpio_mpc8xxx",
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +0200262 .id = UCLASS_GPIO,
Mario Sixd006f682018-01-15 11:07:48 +0100263 .ops = &gpio_mpc8xxx_ops,
Hamish Martin47ca9fc2016-06-14 10:17:05 +1200264#if CONFIG_IS_ENABLED(OF_CONTROL)
Mario Sixd006f682018-01-15 11:07:48 +0100265 .ofdata_to_platdata = mpc8xxx_gpio_ofdata_to_platdata,
266 .platdata_auto_alloc_size = sizeof(struct mpc8xxx_gpio_plat),
267 .of_match = mpc8xxx_gpio_ids,
Hamish Martin47ca9fc2016-06-14 10:17:05 +1200268#endif
Mario Sixd006f682018-01-15 11:07:48 +0100269 .probe = mpc8xxx_gpio_probe,
270 .priv_auto_alloc_size = sizeof(struct mpc8xxx_gpio_data),
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +0200271};