blob: d8e72ada194322d70a363146ef39f55d22ee5a8a [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Guennadi Liakhovetskib30de3c2009-02-07 01:18:07 +01002/*
3 * Copyright (C) 2009
4 * Guennadi Liakhovetski, DENX Software Engineering, <lg@denx.de>
5 *
Stefano Babicd8e0ca82011-08-21 10:45:44 +02006 * Copyright (C) 2011
7 * Stefano Babic, DENX Software Engineering, <sbabic@denx.de>
Guennadi Liakhovetskib30de3c2009-02-07 01:18:07 +01008 */
9#include <common.h>
Simon Glass441d0cf2014-10-01 19:57:26 -060010#include <errno.h>
11#include <dm.h>
12#include <malloc.h>
Stefano Babicc4ea1422010-07-06 17:05:06 +020013#include <asm/arch/imx-regs.h>
Stefano Babicd8e0ca82011-08-21 10:45:44 +020014#include <asm/gpio.h>
Stefano Babicc4ea1422010-07-06 17:05:06 +020015#include <asm/io.h>
Guennadi Liakhovetskib30de3c2009-02-07 01:18:07 +010016
Stefano Babicd8e0ca82011-08-21 10:45:44 +020017enum mxc_gpio_direction {
18 MXC_GPIO_DIRECTION_IN,
19 MXC_GPIO_DIRECTION_OUT,
20};
21
Simon Glass441d0cf2014-10-01 19:57:26 -060022#define GPIO_PER_BANK 32
23
24struct mxc_gpio_plat {
Peng Fan637a7692015-02-10 14:46:33 +080025 int bank_index;
Simon Glass441d0cf2014-10-01 19:57:26 -060026 struct gpio_regs *regs;
27};
28
29struct mxc_bank_info {
Simon Glass441d0cf2014-10-01 19:57:26 -060030 struct gpio_regs *regs;
31};
32
33#ifndef CONFIG_DM_GPIO
Vikram Narayanan8d28c212012-04-10 04:26:08 +000034#define GPIO_TO_PORT(n) (n / 32)
Stefano Babicd8e0ca82011-08-21 10:45:44 +020035
Guennadi Liakhovetskib30de3c2009-02-07 01:18:07 +010036/* GPIO port description */
37static unsigned long gpio_ports[] = {
Stefano Babicc4ea1422010-07-06 17:05:06 +020038 [0] = GPIO1_BASE_ADDR,
39 [1] = GPIO2_BASE_ADDR,
40 [2] = GPIO3_BASE_ADDR,
treme71c39d2012-08-25 05:30:33 +000041#if defined(CONFIG_MX25) || defined(CONFIG_MX27) || defined(CONFIG_MX51) || \
Adrian Alonso26dd3462015-08-11 11:19:51 -050042 defined(CONFIG_MX53) || defined(CONFIG_MX6) || \
Peng Fan8953d862018-01-10 13:20:42 +080043 defined(CONFIG_MX7) || defined(CONFIG_MX8M)
Stefano Babicc4ea1422010-07-06 17:05:06 +020044 [3] = GPIO4_BASE_ADDR,
45#endif
Adrian Alonso26dd3462015-08-11 11:19:51 -050046#if defined(CONFIG_MX27) || defined(CONFIG_MX53) || defined(CONFIG_MX6) || \
Peng Fan8953d862018-01-10 13:20:42 +080047 defined(CONFIG_MX7) || defined(CONFIG_MX8M)
Liu Hui-R6434301643ec2011-01-03 22:27:38 +000048 [4] = GPIO5_BASE_ADDR,
Peng Fan8953d862018-01-10 13:20:42 +080049#if !(defined(CONFIG_MX6UL) || defined(CONFIG_MX6ULL) || defined(CONFIG_MX8M))
Liu Hui-R6434301643ec2011-01-03 22:27:38 +000050 [5] = GPIO6_BASE_ADDR,
treme71c39d2012-08-25 05:30:33 +000051#endif
Peng Fanf2753b02015-07-20 19:28:31 +080052#endif
Adrian Alonso26dd3462015-08-11 11:19:51 -050053#if defined(CONFIG_MX53) || defined(CONFIG_MX6) || defined(CONFIG_MX7)
Fabio Estevam290e7cf2018-01-03 12:33:05 -020054#if !(defined(CONFIG_MX6UL) || defined(CONFIG_MX6ULL))
Liu Hui-R6434301643ec2011-01-03 22:27:38 +000055 [6] = GPIO7_BASE_ADDR,
56#endif
Peng Fanf2753b02015-07-20 19:28:31 +080057#endif
Guennadi Liakhovetskib30de3c2009-02-07 01:18:07 +010058};
59
Stefano Babicd8e0ca82011-08-21 10:45:44 +020060static int mxc_gpio_direction(unsigned int gpio,
61 enum mxc_gpio_direction direction)
Guennadi Liakhovetskib30de3c2009-02-07 01:18:07 +010062{
Vikram Narayananbe282552012-04-10 04:26:20 +000063 unsigned int port = GPIO_TO_PORT(gpio);
Stefano Babicc4ea1422010-07-06 17:05:06 +020064 struct gpio_regs *regs;
Guennadi Liakhovetskib30de3c2009-02-07 01:18:07 +010065 u32 l;
66
67 if (port >= ARRAY_SIZE(gpio_ports))
Joe Hershberger365d6072011-11-11 15:55:36 -060068 return -1;
Guennadi Liakhovetskib30de3c2009-02-07 01:18:07 +010069
70 gpio &= 0x1f;
71
Stefano Babicc4ea1422010-07-06 17:05:06 +020072 regs = (struct gpio_regs *)gpio_ports[port];
73
74 l = readl(&regs->gpio_dir);
75
Guennadi Liakhovetskib30de3c2009-02-07 01:18:07 +010076 switch (direction) {
Stefano Babicc4ea1422010-07-06 17:05:06 +020077 case MXC_GPIO_DIRECTION_OUT:
Guennadi Liakhovetskib30de3c2009-02-07 01:18:07 +010078 l |= 1 << gpio;
79 break;
Stefano Babicc4ea1422010-07-06 17:05:06 +020080 case MXC_GPIO_DIRECTION_IN:
Guennadi Liakhovetskib30de3c2009-02-07 01:18:07 +010081 l &= ~(1 << gpio);
82 }
Stefano Babicc4ea1422010-07-06 17:05:06 +020083 writel(l, &regs->gpio_dir);
Guennadi Liakhovetskib30de3c2009-02-07 01:18:07 +010084
85 return 0;
86}
87
Joe Hershberger365d6072011-11-11 15:55:36 -060088int gpio_set_value(unsigned gpio, int value)
Guennadi Liakhovetskib30de3c2009-02-07 01:18:07 +010089{
Vikram Narayananbe282552012-04-10 04:26:20 +000090 unsigned int port = GPIO_TO_PORT(gpio);
Stefano Babicc4ea1422010-07-06 17:05:06 +020091 struct gpio_regs *regs;
Guennadi Liakhovetskib30de3c2009-02-07 01:18:07 +010092 u32 l;
93
94 if (port >= ARRAY_SIZE(gpio_ports))
Joe Hershberger365d6072011-11-11 15:55:36 -060095 return -1;
Guennadi Liakhovetskib30de3c2009-02-07 01:18:07 +010096
97 gpio &= 0x1f;
98
Stefano Babicc4ea1422010-07-06 17:05:06 +020099 regs = (struct gpio_regs *)gpio_ports[port];
100
101 l = readl(&regs->gpio_dr);
Guennadi Liakhovetskib30de3c2009-02-07 01:18:07 +0100102 if (value)
103 l |= 1 << gpio;
104 else
105 l &= ~(1 << gpio);
Stefano Babicc4ea1422010-07-06 17:05:06 +0200106 writel(l, &regs->gpio_dr);
Joe Hershberger365d6072011-11-11 15:55:36 -0600107
108 return 0;
Guennadi Liakhovetskib30de3c2009-02-07 01:18:07 +0100109}
Stefano Babic7d27cd02010-04-13 12:07:00 +0200110
Joe Hershberger365d6072011-11-11 15:55:36 -0600111int gpio_get_value(unsigned gpio)
Stefano Babic7d27cd02010-04-13 12:07:00 +0200112{
Vikram Narayananbe282552012-04-10 04:26:20 +0000113 unsigned int port = GPIO_TO_PORT(gpio);
Stefano Babicc4ea1422010-07-06 17:05:06 +0200114 struct gpio_regs *regs;
Joe Hershberger365d6072011-11-11 15:55:36 -0600115 u32 val;
Stefano Babic7d27cd02010-04-13 12:07:00 +0200116
117 if (port >= ARRAY_SIZE(gpio_ports))
Joe Hershberger365d6072011-11-11 15:55:36 -0600118 return -1;
Stefano Babic7d27cd02010-04-13 12:07:00 +0200119
120 gpio &= 0x1f;
121
Stefano Babicc4ea1422010-07-06 17:05:06 +0200122 regs = (struct gpio_regs *)gpio_ports[port];
123
Benoît Thébaudeau5dafa452012-08-20 10:55:41 +0000124 val = (readl(&regs->gpio_psr) >> gpio) & 0x01;
Stefano Babic7d27cd02010-04-13 12:07:00 +0200125
Joe Hershberger365d6072011-11-11 15:55:36 -0600126 return val;
Stefano Babic7d27cd02010-04-13 12:07:00 +0200127}
Stefano Babicd8e0ca82011-08-21 10:45:44 +0200128
Joe Hershberger365d6072011-11-11 15:55:36 -0600129int gpio_request(unsigned gpio, const char *label)
Stefano Babicd8e0ca82011-08-21 10:45:44 +0200130{
Vikram Narayananbe282552012-04-10 04:26:20 +0000131 unsigned int port = GPIO_TO_PORT(gpio);
Stefano Babicd8e0ca82011-08-21 10:45:44 +0200132 if (port >= ARRAY_SIZE(gpio_ports))
Joe Hershberger365d6072011-11-11 15:55:36 -0600133 return -1;
Stefano Babicd8e0ca82011-08-21 10:45:44 +0200134 return 0;
135}
136
Joe Hershberger365d6072011-11-11 15:55:36 -0600137int gpio_free(unsigned gpio)
Stefano Babicd8e0ca82011-08-21 10:45:44 +0200138{
Joe Hershberger365d6072011-11-11 15:55:36 -0600139 return 0;
Stefano Babicd8e0ca82011-08-21 10:45:44 +0200140}
141
Joe Hershberger365d6072011-11-11 15:55:36 -0600142int gpio_direction_input(unsigned gpio)
Stefano Babicd8e0ca82011-08-21 10:45:44 +0200143{
Joe Hershberger365d6072011-11-11 15:55:36 -0600144 return mxc_gpio_direction(gpio, MXC_GPIO_DIRECTION_IN);
Stefano Babicd8e0ca82011-08-21 10:45:44 +0200145}
146
Joe Hershberger365d6072011-11-11 15:55:36 -0600147int gpio_direction_output(unsigned gpio, int value)
Stefano Babicd8e0ca82011-08-21 10:45:44 +0200148{
Dirk Behme04c79cb2013-07-15 15:58:27 +0200149 int ret = gpio_set_value(gpio, value);
Stefano Babicd8e0ca82011-08-21 10:45:44 +0200150
151 if (ret < 0)
152 return ret;
153
Dirk Behme04c79cb2013-07-15 15:58:27 +0200154 return mxc_gpio_direction(gpio, MXC_GPIO_DIRECTION_OUT);
Stefano Babicd8e0ca82011-08-21 10:45:44 +0200155}
Simon Glass441d0cf2014-10-01 19:57:26 -0600156#endif
157
158#ifdef CONFIG_DM_GPIO
Peng Fan99c0ae12015-02-10 14:46:34 +0800159#include <fdtdec.h>
Simon Glass441d0cf2014-10-01 19:57:26 -0600160static int mxc_gpio_is_output(struct gpio_regs *regs, int offset)
161{
162 u32 val;
163
164 val = readl(&regs->gpio_dir);
165
166 return val & (1 << offset) ? 1 : 0;
167}
168
169static void mxc_gpio_bank_direction(struct gpio_regs *regs, int offset,
170 enum mxc_gpio_direction direction)
171{
172 u32 l;
173
174 l = readl(&regs->gpio_dir);
175
176 switch (direction) {
177 case MXC_GPIO_DIRECTION_OUT:
178 l |= 1 << offset;
179 break;
180 case MXC_GPIO_DIRECTION_IN:
181 l &= ~(1 << offset);
182 }
183 writel(l, &regs->gpio_dir);
184}
185
186static void mxc_gpio_bank_set_value(struct gpio_regs *regs, int offset,
187 int value)
188{
189 u32 l;
190
191 l = readl(&regs->gpio_dr);
192 if (value)
193 l |= 1 << offset;
194 else
195 l &= ~(1 << offset);
196 writel(l, &regs->gpio_dr);
197}
198
199static int mxc_gpio_bank_get_value(struct gpio_regs *regs, int offset)
200{
201 return (readl(&regs->gpio_psr) >> offset) & 0x01;
202}
203
Simon Glass441d0cf2014-10-01 19:57:26 -0600204/* set GPIO pin 'gpio' as an input */
205static int mxc_gpio_direction_input(struct udevice *dev, unsigned offset)
206{
207 struct mxc_bank_info *bank = dev_get_priv(dev);
Simon Glass441d0cf2014-10-01 19:57:26 -0600208
209 /* Configure GPIO direction as input. */
210 mxc_gpio_bank_direction(bank->regs, offset, MXC_GPIO_DIRECTION_IN);
211
212 return 0;
213}
214
215/* set GPIO pin 'gpio' as an output, with polarity 'value' */
216static int mxc_gpio_direction_output(struct udevice *dev, unsigned offset,
217 int value)
218{
219 struct mxc_bank_info *bank = dev_get_priv(dev);
Simon Glass441d0cf2014-10-01 19:57:26 -0600220
221 /* Configure GPIO output value. */
222 mxc_gpio_bank_set_value(bank->regs, offset, value);
223
224 /* Configure GPIO direction as output. */
225 mxc_gpio_bank_direction(bank->regs, offset, MXC_GPIO_DIRECTION_OUT);
226
227 return 0;
228}
229
230/* read GPIO IN value of pin 'gpio' */
231static int mxc_gpio_get_value(struct udevice *dev, unsigned offset)
232{
233 struct mxc_bank_info *bank = dev_get_priv(dev);
Simon Glass441d0cf2014-10-01 19:57:26 -0600234
235 return mxc_gpio_bank_get_value(bank->regs, offset);
236}
237
238/* write GPIO OUT value to pin 'gpio' */
239static int mxc_gpio_set_value(struct udevice *dev, unsigned offset,
240 int value)
241{
242 struct mxc_bank_info *bank = dev_get_priv(dev);
Simon Glass441d0cf2014-10-01 19:57:26 -0600243
244 mxc_gpio_bank_set_value(bank->regs, offset, value);
245
246 return 0;
247}
248
Simon Glass441d0cf2014-10-01 19:57:26 -0600249static int mxc_gpio_get_function(struct udevice *dev, unsigned offset)
250{
251 struct mxc_bank_info *bank = dev_get_priv(dev);
252
Simon Glass441d0cf2014-10-01 19:57:26 -0600253 /* GPIOF_FUNC is not implemented yet */
254 if (mxc_gpio_is_output(bank->regs, offset))
255 return GPIOF_OUTPUT;
256 else
257 return GPIOF_INPUT;
258}
259
260static const struct dm_gpio_ops gpio_mxc_ops = {
Simon Glass441d0cf2014-10-01 19:57:26 -0600261 .direction_input = mxc_gpio_direction_input,
262 .direction_output = mxc_gpio_direction_output,
263 .get_value = mxc_gpio_get_value,
264 .set_value = mxc_gpio_set_value,
265 .get_function = mxc_gpio_get_function,
Simon Glass441d0cf2014-10-01 19:57:26 -0600266};
267
Simon Glass441d0cf2014-10-01 19:57:26 -0600268static int mxc_gpio_probe(struct udevice *dev)
269{
270 struct mxc_bank_info *bank = dev_get_priv(dev);
271 struct mxc_gpio_plat *plat = dev_get_platdata(dev);
Simon Glasse564f052015-03-05 12:25:20 -0700272 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glass441d0cf2014-10-01 19:57:26 -0600273 int banknum;
274 char name[18], *str;
275
Peng Fan637a7692015-02-10 14:46:33 +0800276 banknum = plat->bank_index;
Simon Glass441d0cf2014-10-01 19:57:26 -0600277 sprintf(name, "GPIO%d_", banknum + 1);
278 str = strdup(name);
279 if (!str)
280 return -ENOMEM;
281 uc_priv->bank_name = str;
282 uc_priv->gpio_count = GPIO_PER_BANK;
283 bank->regs = plat->regs;
284
285 return 0;
286}
287
Peng Fan99c0ae12015-02-10 14:46:34 +0800288static int mxc_gpio_bind(struct udevice *dev)
289{
290 struct mxc_gpio_plat *plat = dev->platdata;
291 fdt_addr_t addr;
292
293 /*
294 * If platdata already exsits, directly return.
295 * Actually only when DT is not supported, platdata
296 * is statically initialized in U_BOOT_DEVICES.Here
297 * will return.
298 */
299 if (plat)
300 return 0;
301
Simon Glassa821c4a2017-05-17 17:18:05 -0600302 addr = devfdt_get_addr(dev);
Peng Fan99c0ae12015-02-10 14:46:34 +0800303 if (addr == FDT_ADDR_T_NONE)
Simon Glass7c843192017-09-17 16:54:53 -0600304 return -EINVAL;
Peng Fan99c0ae12015-02-10 14:46:34 +0800305
306 /*
307 * TODO:
308 * When every board is converted to driver model and DT is supported,
309 * this can be done by auto-alloc feature, but not using calloc
310 * to alloc memory for platdata.
Simon Glass4d686042017-09-17 16:54:52 -0600311 *
312 * For example mxc_plat below uses platform data rather than device
313 * tree.
314 *
315 * NOTE: DO NOT COPY this code if you are using device tree.
Peng Fan99c0ae12015-02-10 14:46:34 +0800316 */
317 plat = calloc(1, sizeof(*plat));
318 if (!plat)
319 return -ENOMEM;
320
321 plat->regs = (struct gpio_regs *)addr;
322 plat->bank_index = dev->req_seq;
323 dev->platdata = plat;
324
325 return 0;
326}
327
328static const struct udevice_id mxc_gpio_ids[] = {
329 { .compatible = "fsl,imx35-gpio" },
330 { }
331};
332
Simon Glass441d0cf2014-10-01 19:57:26 -0600333U_BOOT_DRIVER(gpio_mxc) = {
334 .name = "gpio_mxc",
335 .id = UCLASS_GPIO,
336 .ops = &gpio_mxc_ops,
337 .probe = mxc_gpio_probe,
338 .priv_auto_alloc_size = sizeof(struct mxc_bank_info),
Peng Fan99c0ae12015-02-10 14:46:34 +0800339 .of_match = mxc_gpio_ids,
340 .bind = mxc_gpio_bind,
341};
342
Masahiro Yamada0f925822015-08-12 07:31:55 +0900343#if !CONFIG_IS_ENABLED(OF_CONTROL)
Peng Fan99c0ae12015-02-10 14:46:34 +0800344static const struct mxc_gpio_plat mxc_plat[] = {
345 { 0, (struct gpio_regs *)GPIO1_BASE_ADDR },
346 { 1, (struct gpio_regs *)GPIO2_BASE_ADDR },
347 { 2, (struct gpio_regs *)GPIO3_BASE_ADDR },
348#if defined(CONFIG_MX25) || defined(CONFIG_MX27) || defined(CONFIG_MX51) || \
Peng Fan8953d862018-01-10 13:20:42 +0800349 defined(CONFIG_MX53) || defined(CONFIG_MX6) || \
350 defined(CONFIG_MX8M)
Peng Fan99c0ae12015-02-10 14:46:34 +0800351 { 3, (struct gpio_regs *)GPIO4_BASE_ADDR },
352#endif
Peng Fan8953d862018-01-10 13:20:42 +0800353#if defined(CONFIG_MX27) || defined(CONFIG_MX53) || defined(CONFIG_MX6) || \
354 defined(CONFIG_MX8M)
Peng Fan99c0ae12015-02-10 14:46:34 +0800355 { 4, (struct gpio_regs *)GPIO5_BASE_ADDR },
Peng Fan8953d862018-01-10 13:20:42 +0800356#ifndef CONFIG_MX8M
Peng Fan99c0ae12015-02-10 14:46:34 +0800357 { 5, (struct gpio_regs *)GPIO6_BASE_ADDR },
358#endif
Peng Fan8953d862018-01-10 13:20:42 +0800359#endif
Peng Fan99c0ae12015-02-10 14:46:34 +0800360#if defined(CONFIG_MX53) || defined(CONFIG_MX6)
361 { 6, (struct gpio_regs *)GPIO7_BASE_ADDR },
362#endif
Simon Glass441d0cf2014-10-01 19:57:26 -0600363};
364
365U_BOOT_DEVICES(mxc_gpios) = {
366 { "gpio_mxc", &mxc_plat[0] },
367 { "gpio_mxc", &mxc_plat[1] },
368 { "gpio_mxc", &mxc_plat[2] },
369#if defined(CONFIG_MX25) || defined(CONFIG_MX27) || defined(CONFIG_MX51) || \
Peng Fan8953d862018-01-10 13:20:42 +0800370 defined(CONFIG_MX53) || defined(CONFIG_MX6) || \
371 defined(CONFIG_MX8M)
Simon Glass441d0cf2014-10-01 19:57:26 -0600372 { "gpio_mxc", &mxc_plat[3] },
373#endif
Peng Fan8953d862018-01-10 13:20:42 +0800374#if defined(CONFIG_MX27) || defined(CONFIG_MX53) || defined(CONFIG_MX6) || \
375 defined(CONFIG_MX8M)
Simon Glass441d0cf2014-10-01 19:57:26 -0600376 { "gpio_mxc", &mxc_plat[4] },
Peng Fan8953d862018-01-10 13:20:42 +0800377#ifndef CONFIG_MX8M
Simon Glass441d0cf2014-10-01 19:57:26 -0600378 { "gpio_mxc", &mxc_plat[5] },
379#endif
Peng Fan8953d862018-01-10 13:20:42 +0800380#endif
Simon Glass441d0cf2014-10-01 19:57:26 -0600381#if defined(CONFIG_MX53) || defined(CONFIG_MX6)
382 { "gpio_mxc", &mxc_plat[6] },
383#endif
384};
385#endif
Peng Fan99c0ae12015-02-10 14:46:34 +0800386#endif