blob: 2012f994c8cda7dda9ffc7d4d64cd6c3d536df97 [file] [log] [blame]
Guennadi Liakhovetskib30de3c2009-02-07 01:18:07 +01001/*
2 * Copyright (C) 2009
3 * Guennadi Liakhovetski, DENX Software Engineering, <lg@denx.de>
4 *
Stefano Babicd8e0ca82011-08-21 10:45:44 +02005 * Copyright (C) 2011
6 * Stefano Babic, DENX Software Engineering, <sbabic@denx.de>
7 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02008 * SPDX-License-Identifier: GPL-2.0+
Guennadi Liakhovetskib30de3c2009-02-07 01:18:07 +01009 */
10#include <common.h>
Simon Glass441d0cf2014-10-01 19:57:26 -060011#include <errno.h>
12#include <dm.h>
13#include <malloc.h>
Stefano Babicc4ea1422010-07-06 17:05:06 +020014#include <asm/arch/imx-regs.h>
Stefano Babicd8e0ca82011-08-21 10:45:44 +020015#include <asm/gpio.h>
Stefano Babicc4ea1422010-07-06 17:05:06 +020016#include <asm/io.h>
Guennadi Liakhovetskib30de3c2009-02-07 01:18:07 +010017
Stefano Babicd8e0ca82011-08-21 10:45:44 +020018enum mxc_gpio_direction {
19 MXC_GPIO_DIRECTION_IN,
20 MXC_GPIO_DIRECTION_OUT,
21};
22
Simon Glass441d0cf2014-10-01 19:57:26 -060023#define GPIO_PER_BANK 32
24
25struct mxc_gpio_plat {
Peng Fan637a7692015-02-10 14:46:33 +080026 int bank_index;
Simon Glass441d0cf2014-10-01 19:57:26 -060027 struct gpio_regs *regs;
28};
29
30struct mxc_bank_info {
Simon Glass441d0cf2014-10-01 19:57:26 -060031 struct gpio_regs *regs;
32};
33
34#ifndef CONFIG_DM_GPIO
Vikram Narayanan8d28c212012-04-10 04:26:08 +000035#define GPIO_TO_PORT(n) (n / 32)
Stefano Babicd8e0ca82011-08-21 10:45:44 +020036
Guennadi Liakhovetskib30de3c2009-02-07 01:18:07 +010037/* GPIO port description */
38static unsigned long gpio_ports[] = {
Stefano Babicc4ea1422010-07-06 17:05:06 +020039 [0] = GPIO1_BASE_ADDR,
40 [1] = GPIO2_BASE_ADDR,
41 [2] = GPIO3_BASE_ADDR,
treme71c39d2012-08-25 05:30:33 +000042#if defined(CONFIG_MX25) || defined(CONFIG_MX27) || defined(CONFIG_MX51) || \
Troy Kisky5ea6d7c2012-10-23 10:57:47 +000043 defined(CONFIG_MX53) || defined(CONFIG_MX6)
Stefano Babicc4ea1422010-07-06 17:05:06 +020044 [3] = GPIO4_BASE_ADDR,
45#endif
Troy Kisky5ea6d7c2012-10-23 10:57:47 +000046#if defined(CONFIG_MX27) || defined(CONFIG_MX53) || defined(CONFIG_MX6)
Liu Hui-R6434301643ec2011-01-03 22:27:38 +000047 [4] = GPIO5_BASE_ADDR,
48 [5] = GPIO6_BASE_ADDR,
treme71c39d2012-08-25 05:30:33 +000049#endif
Troy Kisky5ea6d7c2012-10-23 10:57:47 +000050#if defined(CONFIG_MX53) || defined(CONFIG_MX6)
Liu Hui-R6434301643ec2011-01-03 22:27:38 +000051 [6] = GPIO7_BASE_ADDR,
52#endif
Guennadi Liakhovetskib30de3c2009-02-07 01:18:07 +010053};
54
Stefano Babicd8e0ca82011-08-21 10:45:44 +020055static int mxc_gpio_direction(unsigned int gpio,
56 enum mxc_gpio_direction direction)
Guennadi Liakhovetskib30de3c2009-02-07 01:18:07 +010057{
Vikram Narayananbe282552012-04-10 04:26:20 +000058 unsigned int port = GPIO_TO_PORT(gpio);
Stefano Babicc4ea1422010-07-06 17:05:06 +020059 struct gpio_regs *regs;
Guennadi Liakhovetskib30de3c2009-02-07 01:18:07 +010060 u32 l;
61
62 if (port >= ARRAY_SIZE(gpio_ports))
Joe Hershberger365d6072011-11-11 15:55:36 -060063 return -1;
Guennadi Liakhovetskib30de3c2009-02-07 01:18:07 +010064
65 gpio &= 0x1f;
66
Stefano Babicc4ea1422010-07-06 17:05:06 +020067 regs = (struct gpio_regs *)gpio_ports[port];
68
69 l = readl(&regs->gpio_dir);
70
Guennadi Liakhovetskib30de3c2009-02-07 01:18:07 +010071 switch (direction) {
Stefano Babicc4ea1422010-07-06 17:05:06 +020072 case MXC_GPIO_DIRECTION_OUT:
Guennadi Liakhovetskib30de3c2009-02-07 01:18:07 +010073 l |= 1 << gpio;
74 break;
Stefano Babicc4ea1422010-07-06 17:05:06 +020075 case MXC_GPIO_DIRECTION_IN:
Guennadi Liakhovetskib30de3c2009-02-07 01:18:07 +010076 l &= ~(1 << gpio);
77 }
Stefano Babicc4ea1422010-07-06 17:05:06 +020078 writel(l, &regs->gpio_dir);
Guennadi Liakhovetskib30de3c2009-02-07 01:18:07 +010079
80 return 0;
81}
82
Joe Hershberger365d6072011-11-11 15:55:36 -060083int gpio_set_value(unsigned gpio, int value)
Guennadi Liakhovetskib30de3c2009-02-07 01:18:07 +010084{
Vikram Narayananbe282552012-04-10 04:26:20 +000085 unsigned int port = GPIO_TO_PORT(gpio);
Stefano Babicc4ea1422010-07-06 17:05:06 +020086 struct gpio_regs *regs;
Guennadi Liakhovetskib30de3c2009-02-07 01:18:07 +010087 u32 l;
88
89 if (port >= ARRAY_SIZE(gpio_ports))
Joe Hershberger365d6072011-11-11 15:55:36 -060090 return -1;
Guennadi Liakhovetskib30de3c2009-02-07 01:18:07 +010091
92 gpio &= 0x1f;
93
Stefano Babicc4ea1422010-07-06 17:05:06 +020094 regs = (struct gpio_regs *)gpio_ports[port];
95
96 l = readl(&regs->gpio_dr);
Guennadi Liakhovetskib30de3c2009-02-07 01:18:07 +010097 if (value)
98 l |= 1 << gpio;
99 else
100 l &= ~(1 << gpio);
Stefano Babicc4ea1422010-07-06 17:05:06 +0200101 writel(l, &regs->gpio_dr);
Joe Hershberger365d6072011-11-11 15:55:36 -0600102
103 return 0;
Guennadi Liakhovetskib30de3c2009-02-07 01:18:07 +0100104}
Stefano Babic7d27cd02010-04-13 12:07:00 +0200105
Joe Hershberger365d6072011-11-11 15:55:36 -0600106int gpio_get_value(unsigned gpio)
Stefano Babic7d27cd02010-04-13 12:07:00 +0200107{
Vikram Narayananbe282552012-04-10 04:26:20 +0000108 unsigned int port = GPIO_TO_PORT(gpio);
Stefano Babicc4ea1422010-07-06 17:05:06 +0200109 struct gpio_regs *regs;
Joe Hershberger365d6072011-11-11 15:55:36 -0600110 u32 val;
Stefano Babic7d27cd02010-04-13 12:07:00 +0200111
112 if (port >= ARRAY_SIZE(gpio_ports))
Joe Hershberger365d6072011-11-11 15:55:36 -0600113 return -1;
Stefano Babic7d27cd02010-04-13 12:07:00 +0200114
115 gpio &= 0x1f;
116
Stefano Babicc4ea1422010-07-06 17:05:06 +0200117 regs = (struct gpio_regs *)gpio_ports[port];
118
Benoît Thébaudeau5dafa452012-08-20 10:55:41 +0000119 val = (readl(&regs->gpio_psr) >> gpio) & 0x01;
Stefano Babic7d27cd02010-04-13 12:07:00 +0200120
Joe Hershberger365d6072011-11-11 15:55:36 -0600121 return val;
Stefano Babic7d27cd02010-04-13 12:07:00 +0200122}
Stefano Babicd8e0ca82011-08-21 10:45:44 +0200123
Joe Hershberger365d6072011-11-11 15:55:36 -0600124int gpio_request(unsigned gpio, const char *label)
Stefano Babicd8e0ca82011-08-21 10:45:44 +0200125{
Vikram Narayananbe282552012-04-10 04:26:20 +0000126 unsigned int port = GPIO_TO_PORT(gpio);
Stefano Babicd8e0ca82011-08-21 10:45:44 +0200127 if (port >= ARRAY_SIZE(gpio_ports))
Joe Hershberger365d6072011-11-11 15:55:36 -0600128 return -1;
Stefano Babicd8e0ca82011-08-21 10:45:44 +0200129 return 0;
130}
131
Joe Hershberger365d6072011-11-11 15:55:36 -0600132int gpio_free(unsigned gpio)
Stefano Babicd8e0ca82011-08-21 10:45:44 +0200133{
Joe Hershberger365d6072011-11-11 15:55:36 -0600134 return 0;
Stefano Babicd8e0ca82011-08-21 10:45:44 +0200135}
136
Joe Hershberger365d6072011-11-11 15:55:36 -0600137int gpio_direction_input(unsigned gpio)
Stefano Babicd8e0ca82011-08-21 10:45:44 +0200138{
Joe Hershberger365d6072011-11-11 15:55:36 -0600139 return mxc_gpio_direction(gpio, MXC_GPIO_DIRECTION_IN);
Stefano Babicd8e0ca82011-08-21 10:45:44 +0200140}
141
Joe Hershberger365d6072011-11-11 15:55:36 -0600142int gpio_direction_output(unsigned gpio, int value)
Stefano Babicd8e0ca82011-08-21 10:45:44 +0200143{
Dirk Behme04c79cb2013-07-15 15:58:27 +0200144 int ret = gpio_set_value(gpio, value);
Stefano Babicd8e0ca82011-08-21 10:45:44 +0200145
146 if (ret < 0)
147 return ret;
148
Dirk Behme04c79cb2013-07-15 15:58:27 +0200149 return mxc_gpio_direction(gpio, MXC_GPIO_DIRECTION_OUT);
Stefano Babicd8e0ca82011-08-21 10:45:44 +0200150}
Simon Glass441d0cf2014-10-01 19:57:26 -0600151#endif
152
153#ifdef CONFIG_DM_GPIO
Peng Fan99c0ae12015-02-10 14:46:34 +0800154#include <fdtdec.h>
155DECLARE_GLOBAL_DATA_PTR;
156
Simon Glass441d0cf2014-10-01 19:57:26 -0600157static int mxc_gpio_is_output(struct gpio_regs *regs, int offset)
158{
159 u32 val;
160
161 val = readl(&regs->gpio_dir);
162
163 return val & (1 << offset) ? 1 : 0;
164}
165
166static void mxc_gpio_bank_direction(struct gpio_regs *regs, int offset,
167 enum mxc_gpio_direction direction)
168{
169 u32 l;
170
171 l = readl(&regs->gpio_dir);
172
173 switch (direction) {
174 case MXC_GPIO_DIRECTION_OUT:
175 l |= 1 << offset;
176 break;
177 case MXC_GPIO_DIRECTION_IN:
178 l &= ~(1 << offset);
179 }
180 writel(l, &regs->gpio_dir);
181}
182
183static void mxc_gpio_bank_set_value(struct gpio_regs *regs, int offset,
184 int value)
185{
186 u32 l;
187
188 l = readl(&regs->gpio_dr);
189 if (value)
190 l |= 1 << offset;
191 else
192 l &= ~(1 << offset);
193 writel(l, &regs->gpio_dr);
194}
195
196static int mxc_gpio_bank_get_value(struct gpio_regs *regs, int offset)
197{
198 return (readl(&regs->gpio_psr) >> offset) & 0x01;
199}
200
Simon Glass441d0cf2014-10-01 19:57:26 -0600201/* set GPIO pin 'gpio' as an input */
202static int mxc_gpio_direction_input(struct udevice *dev, unsigned offset)
203{
204 struct mxc_bank_info *bank = dev_get_priv(dev);
Simon Glass441d0cf2014-10-01 19:57:26 -0600205
206 /* Configure GPIO direction as input. */
207 mxc_gpio_bank_direction(bank->regs, offset, MXC_GPIO_DIRECTION_IN);
208
209 return 0;
210}
211
212/* set GPIO pin 'gpio' as an output, with polarity 'value' */
213static int mxc_gpio_direction_output(struct udevice *dev, unsigned offset,
214 int value)
215{
216 struct mxc_bank_info *bank = dev_get_priv(dev);
Simon Glass441d0cf2014-10-01 19:57:26 -0600217
218 /* Configure GPIO output value. */
219 mxc_gpio_bank_set_value(bank->regs, offset, value);
220
221 /* Configure GPIO direction as output. */
222 mxc_gpio_bank_direction(bank->regs, offset, MXC_GPIO_DIRECTION_OUT);
223
224 return 0;
225}
226
227/* read GPIO IN value of pin 'gpio' */
228static int mxc_gpio_get_value(struct udevice *dev, unsigned offset)
229{
230 struct mxc_bank_info *bank = dev_get_priv(dev);
Simon Glass441d0cf2014-10-01 19:57:26 -0600231
232 return mxc_gpio_bank_get_value(bank->regs, offset);
233}
234
235/* write GPIO OUT value to pin 'gpio' */
236static int mxc_gpio_set_value(struct udevice *dev, unsigned offset,
237 int value)
238{
239 struct mxc_bank_info *bank = dev_get_priv(dev);
Simon Glass441d0cf2014-10-01 19:57:26 -0600240
241 mxc_gpio_bank_set_value(bank->regs, offset, value);
242
243 return 0;
244}
245
Simon Glass441d0cf2014-10-01 19:57:26 -0600246static int mxc_gpio_get_function(struct udevice *dev, unsigned offset)
247{
248 struct mxc_bank_info *bank = dev_get_priv(dev);
249
Simon Glass441d0cf2014-10-01 19:57:26 -0600250 /* GPIOF_FUNC is not implemented yet */
251 if (mxc_gpio_is_output(bank->regs, offset))
252 return GPIOF_OUTPUT;
253 else
254 return GPIOF_INPUT;
255}
256
257static const struct dm_gpio_ops gpio_mxc_ops = {
Simon Glass441d0cf2014-10-01 19:57:26 -0600258 .direction_input = mxc_gpio_direction_input,
259 .direction_output = mxc_gpio_direction_output,
260 .get_value = mxc_gpio_get_value,
261 .set_value = mxc_gpio_set_value,
262 .get_function = mxc_gpio_get_function,
Simon Glass441d0cf2014-10-01 19:57:26 -0600263};
264
Simon Glass441d0cf2014-10-01 19:57:26 -0600265static int mxc_gpio_probe(struct udevice *dev)
266{
267 struct mxc_bank_info *bank = dev_get_priv(dev);
268 struct mxc_gpio_plat *plat = dev_get_platdata(dev);
Simon Glasse564f052015-03-05 12:25:20 -0700269 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glass441d0cf2014-10-01 19:57:26 -0600270 int banknum;
271 char name[18], *str;
272
Peng Fan637a7692015-02-10 14:46:33 +0800273 banknum = plat->bank_index;
Simon Glass441d0cf2014-10-01 19:57:26 -0600274 sprintf(name, "GPIO%d_", banknum + 1);
275 str = strdup(name);
276 if (!str)
277 return -ENOMEM;
278 uc_priv->bank_name = str;
279 uc_priv->gpio_count = GPIO_PER_BANK;
280 bank->regs = plat->regs;
281
282 return 0;
283}
284
Peng Fan99c0ae12015-02-10 14:46:34 +0800285static int mxc_gpio_bind(struct udevice *dev)
286{
287 struct mxc_gpio_plat *plat = dev->platdata;
288 fdt_addr_t addr;
289
290 /*
291 * If platdata already exsits, directly return.
292 * Actually only when DT is not supported, platdata
293 * is statically initialized in U_BOOT_DEVICES.Here
294 * will return.
295 */
296 if (plat)
297 return 0;
298
299 addr = dev_get_addr(dev);
300 if (addr == FDT_ADDR_T_NONE)
301 return -ENODEV;
302
303 /*
304 * TODO:
305 * When every board is converted to driver model and DT is supported,
306 * this can be done by auto-alloc feature, but not using calloc
307 * to alloc memory for platdata.
308 */
309 plat = calloc(1, sizeof(*plat));
310 if (!plat)
311 return -ENOMEM;
312
313 plat->regs = (struct gpio_regs *)addr;
314 plat->bank_index = dev->req_seq;
315 dev->platdata = plat;
316
317 return 0;
318}
319
320static const struct udevice_id mxc_gpio_ids[] = {
321 { .compatible = "fsl,imx35-gpio" },
322 { }
323};
324
Simon Glass441d0cf2014-10-01 19:57:26 -0600325U_BOOT_DRIVER(gpio_mxc) = {
326 .name = "gpio_mxc",
327 .id = UCLASS_GPIO,
328 .ops = &gpio_mxc_ops,
329 .probe = mxc_gpio_probe,
330 .priv_auto_alloc_size = sizeof(struct mxc_bank_info),
Peng Fan99c0ae12015-02-10 14:46:34 +0800331 .of_match = mxc_gpio_ids,
332 .bind = mxc_gpio_bind,
333};
334
335#ifndef CONFIG_OF_CONTROL
336static const struct mxc_gpio_plat mxc_plat[] = {
337 { 0, (struct gpio_regs *)GPIO1_BASE_ADDR },
338 { 1, (struct gpio_regs *)GPIO2_BASE_ADDR },
339 { 2, (struct gpio_regs *)GPIO3_BASE_ADDR },
340#if defined(CONFIG_MX25) || defined(CONFIG_MX27) || defined(CONFIG_MX51) || \
341 defined(CONFIG_MX53) || defined(CONFIG_MX6)
342 { 3, (struct gpio_regs *)GPIO4_BASE_ADDR },
343#endif
344#if defined(CONFIG_MX27) || defined(CONFIG_MX53) || defined(CONFIG_MX6)
345 { 4, (struct gpio_regs *)GPIO5_BASE_ADDR },
346 { 5, (struct gpio_regs *)GPIO6_BASE_ADDR },
347#endif
348#if defined(CONFIG_MX53) || defined(CONFIG_MX6)
349 { 6, (struct gpio_regs *)GPIO7_BASE_ADDR },
350#endif
Simon Glass441d0cf2014-10-01 19:57:26 -0600351};
352
353U_BOOT_DEVICES(mxc_gpios) = {
354 { "gpio_mxc", &mxc_plat[0] },
355 { "gpio_mxc", &mxc_plat[1] },
356 { "gpio_mxc", &mxc_plat[2] },
357#if defined(CONFIG_MX25) || defined(CONFIG_MX27) || defined(CONFIG_MX51) || \
358 defined(CONFIG_MX53) || defined(CONFIG_MX6)
359 { "gpio_mxc", &mxc_plat[3] },
360#endif
361#if defined(CONFIG_MX27) || defined(CONFIG_MX53) || defined(CONFIG_MX6)
362 { "gpio_mxc", &mxc_plat[4] },
363 { "gpio_mxc", &mxc_plat[5] },
364#endif
365#if defined(CONFIG_MX53) || defined(CONFIG_MX6)
366 { "gpio_mxc", &mxc_plat[6] },
367#endif
368};
369#endif
Peng Fan99c0ae12015-02-10 14:46:34 +0800370#endif