blob: e4ebbc117c96dbe82b23c7a4d15c7bb8135bd8fc [file] [log] [blame]
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +02001/*
2 * (C) Copyright 2016
3 * Mario Six, Guntermann & Drunck GmbH, six@gdsys.de
4 *
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>
14#include <asm/gpio.h>
15#include <mapmem.h>
16
17DECLARE_GLOBAL_DATA_PTR;
18
19struct ccsr_gpio {
20 u32 gpdir;
21 u32 gpodr;
22 u32 gpdat;
23 u32 gpier;
24 u32 gpimr;
25 u32 gpicr;
26};
27
Mario Sixd006f682018-01-15 11:07:48 +010028struct mpc8xxx_gpio_data {
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +020029 /* The bank's register base in memory */
30 struct ccsr_gpio __iomem *base;
31 /* The address of the registers; used to identify the bank */
32 ulong addr;
33 /* The GPIO count of the bank */
34 uint gpio_count;
35 /* The GPDAT register cannot be used to determine the value of output
36 * pins on MPC8572/MPC8536, so we shadow it and use the shadowed value
Mario Sixcc94b172018-01-15 11:07:46 +010037 * for output pins
38 */
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +020039 u32 dat_shadow;
40};
41
Mario Sixcc94b172018-01-15 11:07:46 +010042inline u32 gpio_mask(uint gpio)
43{
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +020044 return (1U << (31 - (gpio)));
45}
46
Mario Sixd006f682018-01-15 11:07:48 +010047static inline u32 mpc8xxx_gpio_get_val(struct ccsr_gpio *base, u32 mask)
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +020048{
49 return in_be32(&base->gpdat) & mask;
50}
51
Mario Sixd006f682018-01-15 11:07:48 +010052static inline u32 mpc8xxx_gpio_get_dir(struct ccsr_gpio *base, u32 mask)
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +020053{
54 return in_be32(&base->gpdir) & mask;
55}
56
Mario Sixd006f682018-01-15 11:07:48 +010057static inline void mpc8xxx_gpio_set_in(struct ccsr_gpio *base, u32 gpios)
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +020058{
59 clrbits_be32(&base->gpdat, gpios);
60 /* GPDIR register 0 -> input */
61 clrbits_be32(&base->gpdir, gpios);
62}
63
Mario Sixd006f682018-01-15 11:07:48 +010064static inline void mpc8xxx_gpio_set_low(struct ccsr_gpio *base, u32 gpios)
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +020065{
66 clrbits_be32(&base->gpdat, gpios);
67 /* GPDIR register 1 -> output */
68 setbits_be32(&base->gpdir, gpios);
69}
70
Mario Sixd006f682018-01-15 11:07:48 +010071static inline void mpc8xxx_gpio_set_high(struct ccsr_gpio *base, u32 gpios)
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +020072{
73 setbits_be32(&base->gpdat, gpios);
74 /* GPDIR register 1 -> output */
75 setbits_be32(&base->gpdir, gpios);
76}
77
Mario Sixd006f682018-01-15 11:07:48 +010078static inline int mpc8xxx_gpio_open_drain_val(struct ccsr_gpio *base, u32 mask)
mario.six@gdsys.cc7b4cf8b2016-05-25 15:15:22 +020079{
80 return in_be32(&base->gpodr) & mask;
81}
82
Mario Sixd006f682018-01-15 11:07:48 +010083static inline void mpc8xxx_gpio_open_drain_on(struct ccsr_gpio *base, u32
mario.six@gdsys.cc7b4cf8b2016-05-25 15:15:22 +020084 gpios)
85{
86 /* GPODR register 1 -> open drain on */
87 setbits_be32(&base->gpodr, gpios);
88}
89
Mario Sixd006f682018-01-15 11:07:48 +010090static inline void mpc8xxx_gpio_open_drain_off(struct ccsr_gpio *base,
mario.six@gdsys.cc7b4cf8b2016-05-25 15:15:22 +020091 u32 gpios)
92{
93 /* GPODR register 0 -> open drain off (actively driven) */
94 clrbits_be32(&base->gpodr, gpios);
95}
96
Mario Sixd006f682018-01-15 11:07:48 +010097static int mpc8xxx_gpio_direction_input(struct udevice *dev, uint gpio)
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +020098{
Mario Sixd006f682018-01-15 11:07:48 +010099 struct mpc8xxx_gpio_data *data = dev_get_priv(dev);
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +0200100
Mario Sixd006f682018-01-15 11:07:48 +0100101 mpc8xxx_gpio_set_in(data->base, gpio_mask(gpio));
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +0200102 return 0;
103}
104
Mario Sixd006f682018-01-15 11:07:48 +0100105static int mpc8xxx_gpio_set_value(struct udevice *dev, uint gpio, int value)
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +0200106{
Mario Sixd006f682018-01-15 11:07:48 +0100107 struct mpc8xxx_gpio_data *data = dev_get_priv(dev);
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +0200108
109 if (value) {
110 data->dat_shadow |= gpio_mask(gpio);
Mario Sixd006f682018-01-15 11:07:48 +0100111 mpc8xxx_gpio_set_high(data->base, gpio_mask(gpio));
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +0200112 } else {
113 data->dat_shadow &= ~gpio_mask(gpio);
Mario Sixd006f682018-01-15 11:07:48 +0100114 mpc8xxx_gpio_set_low(data->base, gpio_mask(gpio));
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +0200115 }
116 return 0;
117}
118
Mario Sixd006f682018-01-15 11:07:48 +0100119static int mpc8xxx_gpio_direction_output(struct udevice *dev, uint gpio,
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +0200120 int value)
121{
Mario Sixd006f682018-01-15 11:07:48 +0100122 return mpc8xxx_gpio_set_value(dev, gpio, value);
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +0200123}
124
Mario Sixd006f682018-01-15 11:07:48 +0100125static int mpc8xxx_gpio_get_value(struct udevice *dev, uint gpio)
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +0200126{
Mario Sixd006f682018-01-15 11:07:48 +0100127 struct mpc8xxx_gpio_data *data = dev_get_priv(dev);
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +0200128
Mario Sixd006f682018-01-15 11:07:48 +0100129 if (!!mpc8xxx_gpio_get_dir(data->base, gpio_mask(gpio))) {
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +0200130 /* Output -> use shadowed value */
131 return !!(data->dat_shadow & gpio_mask(gpio));
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +0200132 }
Mario Sixcc94b172018-01-15 11:07:46 +0100133
134 /* Input -> read value from GPDAT register */
Mario Sixd006f682018-01-15 11:07:48 +0100135 return !!mpc8xxx_gpio_get_val(data->base, gpio_mask(gpio));
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +0200136}
137
Mario Sixd006f682018-01-15 11:07:48 +0100138static int mpc8xxx_gpio_get_open_drain(struct udevice *dev, uint gpio)
mario.six@gdsys.cc7b4cf8b2016-05-25 15:15:22 +0200139{
Mario Sixd006f682018-01-15 11:07:48 +0100140 struct mpc8xxx_gpio_data *data = dev_get_priv(dev);
mario.six@gdsys.cc7b4cf8b2016-05-25 15:15:22 +0200141
Mario Sixd006f682018-01-15 11:07:48 +0100142 return !!mpc8xxx_gpio_open_drain_val(data->base, gpio_mask(gpio));
mario.six@gdsys.cc7b4cf8b2016-05-25 15:15:22 +0200143}
144
Mario Sixd006f682018-01-15 11:07:48 +0100145static int mpc8xxx_gpio_set_open_drain(struct udevice *dev, uint gpio,
mario.six@gdsys.cc7b4cf8b2016-05-25 15:15:22 +0200146 int value)
147{
Mario Sixd006f682018-01-15 11:07:48 +0100148 struct mpc8xxx_gpio_data *data = dev_get_priv(dev);
mario.six@gdsys.cc7b4cf8b2016-05-25 15:15:22 +0200149
Mario Sixcc94b172018-01-15 11:07:46 +0100150 if (value)
Mario Sixd006f682018-01-15 11:07:48 +0100151 mpc8xxx_gpio_open_drain_on(data->base, gpio_mask(gpio));
Mario Sixcc94b172018-01-15 11:07:46 +0100152 else
Mario Sixd006f682018-01-15 11:07:48 +0100153 mpc8xxx_gpio_open_drain_off(data->base, gpio_mask(gpio));
Mario Sixcc94b172018-01-15 11:07:46 +0100154
mario.six@gdsys.cc7b4cf8b2016-05-25 15:15:22 +0200155 return 0;
156}
157
Mario Sixd006f682018-01-15 11:07:48 +0100158static int mpc8xxx_gpio_get_function(struct udevice *dev, uint gpio)
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +0200159{
Mario Sixd006f682018-01-15 11:07:48 +0100160 struct mpc8xxx_gpio_data *data = dev_get_priv(dev);
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +0200161 int dir;
162
Mario Sixd006f682018-01-15 11:07:48 +0100163 dir = !!mpc8xxx_gpio_get_dir(data->base, gpio_mask(gpio));
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +0200164 return dir ? GPIOF_OUTPUT : GPIOF_INPUT;
165}
166
Hamish Martin47ca9fc2016-06-14 10:17:05 +1200167#if CONFIG_IS_ENABLED(OF_CONTROL)
Mario Sixd006f682018-01-15 11:07:48 +0100168static int mpc8xxx_gpio_ofdata_to_platdata(struct udevice *dev)
Mario Sixcc94b172018-01-15 11:07:46 +0100169{
Mario Sixd006f682018-01-15 11:07:48 +0100170 struct mpc8xxx_gpio_plat *plat = dev_get_platdata(dev);
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +0200171 fdt_addr_t addr;
172 fdt_size_t size;
173
Simon Glassdd79d6e2017-01-17 16:52:55 -0700174 addr = fdtdec_get_addr_size_auto_noparent(gd->fdt_blob,
Mario Sixcc94b172018-01-15 11:07:46 +0100175 dev_of_offset(dev),
176 "reg", 0, &size, false);
Hamish Martin47ca9fc2016-06-14 10:17:05 +1200177 plat->addr = addr;
178 plat->size = size;
Simon Glassdd79d6e2017-01-17 16:52:55 -0700179 plat->ngpios = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
180 "ngpios", 32);
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +0200181
Hamish Martin47ca9fc2016-06-14 10:17:05 +1200182 return 0;
183}
184#endif
185
Mario Sixd006f682018-01-15 11:07:48 +0100186static int mpc8xxx_gpio_platdata_to_priv(struct udevice *dev)
Hamish Martin47ca9fc2016-06-14 10:17:05 +1200187{
Mario Sixd006f682018-01-15 11:07:48 +0100188 struct mpc8xxx_gpio_data *priv = dev_get_priv(dev);
189 struct mpc8xxx_gpio_plat *plat = dev_get_platdata(dev);
Hamish Martin47ca9fc2016-06-14 10:17:05 +1200190 unsigned long size = plat->size;
191
192 if (size == 0)
193 size = 0x100;
194
195 priv->addr = plat->addr;
196 priv->base = map_sysmem(CONFIG_SYS_IMMR + plat->addr, size);
197
198 if (!priv->base)
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +0200199 return -ENOMEM;
200
Hamish Martin47ca9fc2016-06-14 10:17:05 +1200201 priv->gpio_count = plat->ngpios;
202 priv->dat_shadow = 0;
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +0200203
Mario Sixd006f682018-01-15 11:07:48 +0100204 priv->type = driver_data;
205
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +0200206 return 0;
207}
208
Mario Sixd006f682018-01-15 11:07:48 +0100209static int mpc8xxx_gpio_probe(struct udevice *dev)
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +0200210{
211 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Mario Sixd006f682018-01-15 11:07:48 +0100212 struct mpc8xxx_gpio_data *data = dev_get_priv(dev);
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +0200213 char name[32], *str;
214
Mario Sixd006f682018-01-15 11:07:48 +0100215 mpc8xxx_gpio_platdata_to_priv(dev);
Hamish Martin47ca9fc2016-06-14 10:17:05 +1200216
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +0200217 snprintf(name, sizeof(name), "MPC@%lx_", data->addr);
218 str = strdup(name);
219
220 if (!str)
221 return -ENOMEM;
222
223 uc_priv->bank_name = str;
224 uc_priv->gpio_count = data->gpio_count;
225
226 return 0;
227}
228
Mario Sixd006f682018-01-15 11:07:48 +0100229static const struct dm_gpio_ops gpio_mpc8xxx_ops = {
230 .direction_input = mpc8xxx_gpio_direction_input,
231 .direction_output = mpc8xxx_gpio_direction_output,
232 .get_value = mpc8xxx_gpio_get_value,
233 .set_value = mpc8xxx_gpio_set_value,
234 .get_open_drain = mpc8xxx_gpio_get_open_drain,
235 .set_open_drain = mpc8xxx_gpio_set_open_drain,
236 .get_function = mpc8xxx_gpio_get_function,
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +0200237};
238
Mario Sixd006f682018-01-15 11:07:48 +0100239static const struct udevice_id mpc8xxx_gpio_ids[] = {
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +0200240 { .compatible = "fsl,pq3-gpio" },
241 { /* sentinel */ }
242};
243
Mario Sixd006f682018-01-15 11:07:48 +0100244U_BOOT_DRIVER(gpio_mpc8xxx) = {
245 .name = "gpio_mpc8xxx",
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +0200246 .id = UCLASS_GPIO,
Mario Sixd006f682018-01-15 11:07:48 +0100247 .ops = &gpio_mpc8xxx_ops,
Hamish Martin47ca9fc2016-06-14 10:17:05 +1200248#if CONFIG_IS_ENABLED(OF_CONTROL)
Mario Sixd006f682018-01-15 11:07:48 +0100249 .ofdata_to_platdata = mpc8xxx_gpio_ofdata_to_platdata,
250 .platdata_auto_alloc_size = sizeof(struct mpc8xxx_gpio_plat),
251 .of_match = mpc8xxx_gpio_ids,
Hamish Martin47ca9fc2016-06-14 10:17:05 +1200252#endif
Mario Sixd006f682018-01-15 11:07:48 +0100253 .probe = mpc8xxx_gpio_probe,
254 .priv_auto_alloc_size = sizeof(struct mpc8xxx_gpio_data),
mario.six@gdsys.cc5b59a352016-05-25 15:15:20 +0200255};