4b385b8b395b6526bafb88b8cc1a259681a805f7
[oweals/u-boot.git] / drivers / gpio / mpc8xxx_gpio.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2016
4  * Mario Six, Guntermann & Drunck GmbH, mario.six@gdsys.cc
5  *
6  * based on arch/powerpc/include/asm/mpc85xx_gpio.h, which is
7  *
8  * Copyright 2010 eXMeritus, A Boeing Company
9  */
10
11 #include <common.h>
12 #include <dm.h>
13 #include <mapmem.h>
14 #include <asm/gpio.h>
15
16 struct ccsr_gpio {
17         u32     gpdir;
18         u32     gpodr;
19         u32     gpdat;
20         u32     gpier;
21         u32     gpimr;
22         u32     gpicr;
23 };
24
25 struct mpc8xxx_gpio_data {
26         /* 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
34          * for output pins
35          */
36         u32 dat_shadow;
37         ulong type;
38 };
39
40 enum {
41         MPC8XXX_GPIO_TYPE,
42         MPC5121_GPIO_TYPE,
43 };
44
45 inline u32 gpio_mask(uint gpio)
46 {
47         return (1U << (31 - (gpio)));
48 }
49
50 static inline u32 mpc8xxx_gpio_get_val(struct ccsr_gpio *base, u32 mask)
51 {
52         return in_be32(&base->gpdat) & mask;
53 }
54
55 static inline u32 mpc8xxx_gpio_get_dir(struct ccsr_gpio *base, u32 mask)
56 {
57         return in_be32(&base->gpdir) & mask;
58 }
59
60 static inline int mpc8xxx_gpio_open_drain_val(struct ccsr_gpio *base, u32 mask)
61 {
62         return in_be32(&base->gpodr) & mask;
63 }
64
65 static inline void mpc8xxx_gpio_open_drain_on(struct ccsr_gpio *base, u32
66                                               gpios)
67 {
68         /* GPODR register 1 -> open drain on */
69         setbits_be32(&base->gpodr, gpios);
70 }
71
72 static inline void mpc8xxx_gpio_open_drain_off(struct ccsr_gpio *base,
73                                                u32 gpios)
74 {
75         /* GPODR register 0 -> open drain off (actively driven) */
76         clrbits_be32(&base->gpodr, gpios);
77 }
78
79 static int mpc8xxx_gpio_direction_input(struct udevice *dev, uint gpio)
80 {
81         struct mpc8xxx_gpio_data *data = dev_get_priv(dev);
82         u32 mask = gpio_mask(gpio);
83
84         /* GPDIR register 0 -> input */
85         clrbits_be32(&data->base->gpdir, mask);
86
87         return 0;
88 }
89
90 static int mpc8xxx_gpio_set_value(struct udevice *dev, uint gpio, int value)
91 {
92         struct mpc8xxx_gpio_data *data = dev_get_priv(dev);
93         struct ccsr_gpio *base = data->base;
94         u32 mask = gpio_mask(gpio);
95         u32 gpdir;
96
97         if (value) {
98                 data->dat_shadow |= mask;
99         } else {
100                 data->dat_shadow &= ~mask;
101         }
102
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
108         return 0;
109 }
110
111 static int mpc8xxx_gpio_direction_output(struct udevice *dev, uint gpio,
112                                          int value)
113 {
114         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
120         return mpc8xxx_gpio_set_value(dev, gpio, value);
121 }
122
123 static int mpc8xxx_gpio_get_value(struct udevice *dev, uint gpio)
124 {
125         struct mpc8xxx_gpio_data *data = dev_get_priv(dev);
126
127         if (!!mpc8xxx_gpio_get_dir(data->base, gpio_mask(gpio))) {
128                 /* Output -> use shadowed value */
129                 return !!(data->dat_shadow & gpio_mask(gpio));
130         }
131
132         /* Input -> read value from GPDAT register */
133         return !!mpc8xxx_gpio_get_val(data->base, gpio_mask(gpio));
134 }
135
136 static int mpc8xxx_gpio_get_open_drain(struct udevice *dev, uint gpio)
137 {
138         struct mpc8xxx_gpio_data *data = dev_get_priv(dev);
139
140         return !!mpc8xxx_gpio_open_drain_val(data->base, gpio_mask(gpio));
141 }
142
143 static int mpc8xxx_gpio_set_open_drain(struct udevice *dev, uint gpio,
144                                        int value)
145 {
146         struct mpc8xxx_gpio_data *data = dev_get_priv(dev);
147
148         if (value)
149                 mpc8xxx_gpio_open_drain_on(data->base, gpio_mask(gpio));
150         else
151                 mpc8xxx_gpio_open_drain_off(data->base, gpio_mask(gpio));
152
153         return 0;
154 }
155
156 static int mpc8xxx_gpio_get_function(struct udevice *dev, uint gpio)
157 {
158         struct mpc8xxx_gpio_data *data = dev_get_priv(dev);
159         int dir;
160
161         dir = !!mpc8xxx_gpio_get_dir(data->base, gpio_mask(gpio));
162         return dir ? GPIOF_OUTPUT : GPIOF_INPUT;
163 }
164
165 #if CONFIG_IS_ENABLED(OF_CONTROL)
166 static int mpc8xxx_gpio_ofdata_to_platdata(struct udevice *dev)
167 {
168         struct mpc8xxx_gpio_plat *plat = dev_get_platdata(dev);
169         fdt_addr_t addr;
170         u32 reg[2];
171
172         dev_read_u32_array(dev, "reg", reg, 2);
173         addr = dev_translate_address(dev, reg);
174
175         plat->addr = addr;
176         plat->size = reg[1];
177         plat->ngpios = dev_read_u32_default(dev, "ngpios", 32);
178
179         return 0;
180 }
181 #endif
182
183 static int mpc8xxx_gpio_platdata_to_priv(struct udevice *dev)
184 {
185         struct mpc8xxx_gpio_data *priv = dev_get_priv(dev);
186         struct mpc8xxx_gpio_plat *plat = dev_get_platdata(dev);
187         unsigned long size = plat->size;
188         ulong driver_data = dev_get_driver_data(dev);
189
190         if (size == 0)
191                 size = 0x100;
192
193         priv->addr = plat->addr;
194         priv->base = map_sysmem(plat->addr, size);
195
196         if (!priv->base)
197                 return -ENOMEM;
198
199         priv->gpio_count = plat->ngpios;
200         priv->dat_shadow = 0;
201
202         priv->type = driver_data;
203
204         return 0;
205 }
206
207 static int mpc8xxx_gpio_probe(struct udevice *dev)
208 {
209         struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
210         struct mpc8xxx_gpio_data *data = dev_get_priv(dev);
211         char name[32], *str;
212
213         mpc8xxx_gpio_platdata_to_priv(dev);
214
215         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
227 static 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,
235 };
236
237 static const struct udevice_id mpc8xxx_gpio_ids[] = {
238         { .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 },
245         { /* sentinel */ }
246 };
247
248 U_BOOT_DRIVER(gpio_mpc8xxx) = {
249         .name   = "gpio_mpc8xxx",
250         .id     = UCLASS_GPIO,
251         .ops    = &gpio_mpc8xxx_ops,
252 #if CONFIG_IS_ENABLED(OF_CONTROL)
253         .ofdata_to_platdata = mpc8xxx_gpio_ofdata_to_platdata,
254         .platdata_auto_alloc_size = sizeof(struct mpc8xxx_gpio_plat),
255         .of_match = mpc8xxx_gpio_ids,
256 #endif
257         .probe  = mpc8xxx_gpio_probe,
258         .priv_auto_alloc_size = sizeof(struct mpc8xxx_gpio_data),
259 };