blob: 3f7b7d24416b4bb59b31f9d4a02fe3c41039ac20 [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_NAME_SIZE 20
24#define GPIO_PER_BANK 32
25
26struct mxc_gpio_plat {
27 struct gpio_regs *regs;
28};
29
30struct mxc_bank_info {
31 char label[GPIO_PER_BANK][GPIO_NAME_SIZE];
32 struct gpio_regs *regs;
33};
34
35#ifndef CONFIG_DM_GPIO
Vikram Narayanan8d28c212012-04-10 04:26:08 +000036#define GPIO_TO_PORT(n) (n / 32)
Stefano Babicd8e0ca82011-08-21 10:45:44 +020037
Guennadi Liakhovetskib30de3c2009-02-07 01:18:07 +010038/* GPIO port description */
39static unsigned long gpio_ports[] = {
Stefano Babicc4ea1422010-07-06 17:05:06 +020040 [0] = GPIO1_BASE_ADDR,
41 [1] = GPIO2_BASE_ADDR,
42 [2] = GPIO3_BASE_ADDR,
treme71c39d2012-08-25 05:30:33 +000043#if defined(CONFIG_MX25) || defined(CONFIG_MX27) || defined(CONFIG_MX51) || \
Troy Kisky5ea6d7c2012-10-23 10:57:47 +000044 defined(CONFIG_MX53) || defined(CONFIG_MX6)
Stefano Babicc4ea1422010-07-06 17:05:06 +020045 [3] = GPIO4_BASE_ADDR,
46#endif
Troy Kisky5ea6d7c2012-10-23 10:57:47 +000047#if defined(CONFIG_MX27) || defined(CONFIG_MX53) || defined(CONFIG_MX6)
Liu Hui-R6434301643ec2011-01-03 22:27:38 +000048 [4] = GPIO5_BASE_ADDR,
49 [5] = GPIO6_BASE_ADDR,
treme71c39d2012-08-25 05:30:33 +000050#endif
Troy Kisky5ea6d7c2012-10-23 10:57:47 +000051#if defined(CONFIG_MX53) || defined(CONFIG_MX6)
Liu Hui-R6434301643ec2011-01-03 22:27:38 +000052 [6] = GPIO7_BASE_ADDR,
53#endif
Guennadi Liakhovetskib30de3c2009-02-07 01:18:07 +010054};
55
Stefano Babicd8e0ca82011-08-21 10:45:44 +020056static int mxc_gpio_direction(unsigned int gpio,
57 enum mxc_gpio_direction direction)
Guennadi Liakhovetskib30de3c2009-02-07 01:18:07 +010058{
Vikram Narayananbe282552012-04-10 04:26:20 +000059 unsigned int port = GPIO_TO_PORT(gpio);
Stefano Babicc4ea1422010-07-06 17:05:06 +020060 struct gpio_regs *regs;
Guennadi Liakhovetskib30de3c2009-02-07 01:18:07 +010061 u32 l;
62
63 if (port >= ARRAY_SIZE(gpio_ports))
Joe Hershberger365d6072011-11-11 15:55:36 -060064 return -1;
Guennadi Liakhovetskib30de3c2009-02-07 01:18:07 +010065
66 gpio &= 0x1f;
67
Stefano Babicc4ea1422010-07-06 17:05:06 +020068 regs = (struct gpio_regs *)gpio_ports[port];
69
70 l = readl(&regs->gpio_dir);
71
Guennadi Liakhovetskib30de3c2009-02-07 01:18:07 +010072 switch (direction) {
Stefano Babicc4ea1422010-07-06 17:05:06 +020073 case MXC_GPIO_DIRECTION_OUT:
Guennadi Liakhovetskib30de3c2009-02-07 01:18:07 +010074 l |= 1 << gpio;
75 break;
Stefano Babicc4ea1422010-07-06 17:05:06 +020076 case MXC_GPIO_DIRECTION_IN:
Guennadi Liakhovetskib30de3c2009-02-07 01:18:07 +010077 l &= ~(1 << gpio);
78 }
Stefano Babicc4ea1422010-07-06 17:05:06 +020079 writel(l, &regs->gpio_dir);
Guennadi Liakhovetskib30de3c2009-02-07 01:18:07 +010080
81 return 0;
82}
83
Joe Hershberger365d6072011-11-11 15:55:36 -060084int gpio_set_value(unsigned gpio, int value)
Guennadi Liakhovetskib30de3c2009-02-07 01:18:07 +010085{
Vikram Narayananbe282552012-04-10 04:26:20 +000086 unsigned int port = GPIO_TO_PORT(gpio);
Stefano Babicc4ea1422010-07-06 17:05:06 +020087 struct gpio_regs *regs;
Guennadi Liakhovetskib30de3c2009-02-07 01:18:07 +010088 u32 l;
89
90 if (port >= ARRAY_SIZE(gpio_ports))
Joe Hershberger365d6072011-11-11 15:55:36 -060091 return -1;
Guennadi Liakhovetskib30de3c2009-02-07 01:18:07 +010092
93 gpio &= 0x1f;
94
Stefano Babicc4ea1422010-07-06 17:05:06 +020095 regs = (struct gpio_regs *)gpio_ports[port];
96
97 l = readl(&regs->gpio_dr);
Guennadi Liakhovetskib30de3c2009-02-07 01:18:07 +010098 if (value)
99 l |= 1 << gpio;
100 else
101 l &= ~(1 << gpio);
Stefano Babicc4ea1422010-07-06 17:05:06 +0200102 writel(l, &regs->gpio_dr);
Joe Hershberger365d6072011-11-11 15:55:36 -0600103
104 return 0;
Guennadi Liakhovetskib30de3c2009-02-07 01:18:07 +0100105}
Stefano Babic7d27cd02010-04-13 12:07:00 +0200106
Joe Hershberger365d6072011-11-11 15:55:36 -0600107int gpio_get_value(unsigned gpio)
Stefano Babic7d27cd02010-04-13 12:07:00 +0200108{
Vikram Narayananbe282552012-04-10 04:26:20 +0000109 unsigned int port = GPIO_TO_PORT(gpio);
Stefano Babicc4ea1422010-07-06 17:05:06 +0200110 struct gpio_regs *regs;
Joe Hershberger365d6072011-11-11 15:55:36 -0600111 u32 val;
Stefano Babic7d27cd02010-04-13 12:07:00 +0200112
113 if (port >= ARRAY_SIZE(gpio_ports))
Joe Hershberger365d6072011-11-11 15:55:36 -0600114 return -1;
Stefano Babic7d27cd02010-04-13 12:07:00 +0200115
116 gpio &= 0x1f;
117
Stefano Babicc4ea1422010-07-06 17:05:06 +0200118 regs = (struct gpio_regs *)gpio_ports[port];
119
Benoît Thébaudeau5dafa452012-08-20 10:55:41 +0000120 val = (readl(&regs->gpio_psr) >> gpio) & 0x01;
Stefano Babic7d27cd02010-04-13 12:07:00 +0200121
Joe Hershberger365d6072011-11-11 15:55:36 -0600122 return val;
Stefano Babic7d27cd02010-04-13 12:07:00 +0200123}
Stefano Babicd8e0ca82011-08-21 10:45:44 +0200124
Joe Hershberger365d6072011-11-11 15:55:36 -0600125int gpio_request(unsigned gpio, const char *label)
Stefano Babicd8e0ca82011-08-21 10:45:44 +0200126{
Vikram Narayananbe282552012-04-10 04:26:20 +0000127 unsigned int port = GPIO_TO_PORT(gpio);
Stefano Babicd8e0ca82011-08-21 10:45:44 +0200128 if (port >= ARRAY_SIZE(gpio_ports))
Joe Hershberger365d6072011-11-11 15:55:36 -0600129 return -1;
Stefano Babicd8e0ca82011-08-21 10:45:44 +0200130 return 0;
131}
132
Joe Hershberger365d6072011-11-11 15:55:36 -0600133int gpio_free(unsigned gpio)
Stefano Babicd8e0ca82011-08-21 10:45:44 +0200134{
Joe Hershberger365d6072011-11-11 15:55:36 -0600135 return 0;
Stefano Babicd8e0ca82011-08-21 10:45:44 +0200136}
137
Joe Hershberger365d6072011-11-11 15:55:36 -0600138int gpio_direction_input(unsigned gpio)
Stefano Babicd8e0ca82011-08-21 10:45:44 +0200139{
Joe Hershberger365d6072011-11-11 15:55:36 -0600140 return mxc_gpio_direction(gpio, MXC_GPIO_DIRECTION_IN);
Stefano Babicd8e0ca82011-08-21 10:45:44 +0200141}
142
Joe Hershberger365d6072011-11-11 15:55:36 -0600143int gpio_direction_output(unsigned gpio, int value)
Stefano Babicd8e0ca82011-08-21 10:45:44 +0200144{
Dirk Behme04c79cb2013-07-15 15:58:27 +0200145 int ret = gpio_set_value(gpio, value);
Stefano Babicd8e0ca82011-08-21 10:45:44 +0200146
147 if (ret < 0)
148 return ret;
149
Dirk Behme04c79cb2013-07-15 15:58:27 +0200150 return mxc_gpio_direction(gpio, MXC_GPIO_DIRECTION_OUT);
Stefano Babicd8e0ca82011-08-21 10:45:44 +0200151}
Simon Glass441d0cf2014-10-01 19:57:26 -0600152#endif
153
154#ifdef CONFIG_DM_GPIO
155/**
156 * gpio_is_requested() - check if a GPIO has been requested
157 *
158 * @bank: Bank to check
159 * @offset: GPIO offset within bank to check
160 * @return true if marked as requested, false if not
161 */
162static inline bool gpio_is_requested(struct mxc_bank_info *bank, int offset)
163{
164 return *bank->label[offset] != '\0';
165}
166
167static int mxc_gpio_is_output(struct gpio_regs *regs, int offset)
168{
169 u32 val;
170
171 val = readl(&regs->gpio_dir);
172
173 return val & (1 << offset) ? 1 : 0;
174}
175
176static void mxc_gpio_bank_direction(struct gpio_regs *regs, int offset,
177 enum mxc_gpio_direction direction)
178{
179 u32 l;
180
181 l = readl(&regs->gpio_dir);
182
183 switch (direction) {
184 case MXC_GPIO_DIRECTION_OUT:
185 l |= 1 << offset;
186 break;
187 case MXC_GPIO_DIRECTION_IN:
188 l &= ~(1 << offset);
189 }
190 writel(l, &regs->gpio_dir);
191}
192
193static void mxc_gpio_bank_set_value(struct gpio_regs *regs, int offset,
194 int value)
195{
196 u32 l;
197
198 l = readl(&regs->gpio_dr);
199 if (value)
200 l |= 1 << offset;
201 else
202 l &= ~(1 << offset);
203 writel(l, &regs->gpio_dr);
204}
205
206static int mxc_gpio_bank_get_value(struct gpio_regs *regs, int offset)
207{
208 return (readl(&regs->gpio_psr) >> offset) & 0x01;
209}
210
211static int mxc_gpio_bank_get_output_value(struct gpio_regs *regs, int offset)
212{
213 return (readl(&regs->gpio_dr) >> offset) & 0x01;
214}
215
216static int check_requested(struct udevice *dev, unsigned offset,
217 const char *func)
218{
219 struct mxc_bank_info *bank = dev_get_priv(dev);
220 struct gpio_dev_priv *uc_priv = dev->uclass_priv;
221
222 if (!gpio_is_requested(bank, offset)) {
223 printf("mxc_gpio: %s: error: gpio %s%d not requested\n",
224 func, uc_priv->bank_name, offset);
225 return -EPERM;
226 }
227
228 return 0;
229}
230
231/* set GPIO pin 'gpio' as an input */
232static int mxc_gpio_direction_input(struct udevice *dev, unsigned offset)
233{
234 struct mxc_bank_info *bank = dev_get_priv(dev);
235 int ret;
236
237 ret = check_requested(dev, offset, __func__);
238 if (ret)
239 return ret;
240
241 /* Configure GPIO direction as input. */
242 mxc_gpio_bank_direction(bank->regs, offset, MXC_GPIO_DIRECTION_IN);
243
244 return 0;
245}
246
247/* set GPIO pin 'gpio' as an output, with polarity 'value' */
248static int mxc_gpio_direction_output(struct udevice *dev, unsigned offset,
249 int value)
250{
251 struct mxc_bank_info *bank = dev_get_priv(dev);
252 int ret;
253
254 ret = check_requested(dev, offset, __func__);
255 if (ret)
256 return ret;
257
258 /* Configure GPIO output value. */
259 mxc_gpio_bank_set_value(bank->regs, offset, value);
260
261 /* Configure GPIO direction as output. */
262 mxc_gpio_bank_direction(bank->regs, offset, MXC_GPIO_DIRECTION_OUT);
263
264 return 0;
265}
266
267/* read GPIO IN value of pin 'gpio' */
268static int mxc_gpio_get_value(struct udevice *dev, unsigned offset)
269{
270 struct mxc_bank_info *bank = dev_get_priv(dev);
271 int ret;
272
273 ret = check_requested(dev, offset, __func__);
274 if (ret)
275 return ret;
276
277 return mxc_gpio_bank_get_value(bank->regs, offset);
278}
279
280/* write GPIO OUT value to pin 'gpio' */
281static int mxc_gpio_set_value(struct udevice *dev, unsigned offset,
282 int value)
283{
284 struct mxc_bank_info *bank = dev_get_priv(dev);
285 int ret;
286
287 ret = check_requested(dev, offset, __func__);
288 if (ret)
289 return ret;
290
291 mxc_gpio_bank_set_value(bank->regs, offset, value);
292
293 return 0;
294}
295
296static int mxc_gpio_get_state(struct udevice *dev, unsigned int offset,
297 char *buf, int bufsize)
298{
299 struct gpio_dev_priv *uc_priv = dev->uclass_priv;
300 struct mxc_bank_info *bank = dev_get_priv(dev);
301 const char *label;
302 bool requested;
303 bool is_output;
304 int size;
305
306 label = bank->label[offset];
307 is_output = mxc_gpio_is_output(bank->regs, offset);
308 size = snprintf(buf, bufsize, "%s%d: ",
309 uc_priv->bank_name ? uc_priv->bank_name : "", offset);
310 buf += size;
311 bufsize -= size;
312 requested = gpio_is_requested(bank, offset);
313 snprintf(buf, bufsize, "%s: %d [%c]%s%s",
314 is_output ? "out" : " in",
315 is_output ?
316 mxc_gpio_bank_get_output_value(bank->regs, offset) :
317 mxc_gpio_bank_get_value(bank->regs, offset),
318 requested ? 'x' : ' ',
319 requested ? " " : "",
320 label);
321
322 return 0;
323}
324
325static int mxc_gpio_request(struct udevice *dev, unsigned offset,
326 const char *label)
327{
328 struct mxc_bank_info *bank = dev_get_priv(dev);
329
330 if (gpio_is_requested(bank, offset))
331 return -EBUSY;
332
333 strncpy(bank->label[offset], label, GPIO_NAME_SIZE);
334 bank->label[offset][GPIO_NAME_SIZE - 1] = '\0';
335
336 return 0;
337}
338
339static int mxc_gpio_free(struct udevice *dev, unsigned offset)
340{
341 struct mxc_bank_info *bank = dev_get_priv(dev);
342 int ret;
343
344 ret = check_requested(dev, offset, __func__);
345 if (ret)
346 return ret;
347 bank->label[offset][0] = '\0';
348
349 return 0;
350}
351
352static int mxc_gpio_get_function(struct udevice *dev, unsigned offset)
353{
354 struct mxc_bank_info *bank = dev_get_priv(dev);
355
356 if (!gpio_is_requested(bank, offset))
357 return GPIOF_UNUSED;
358
359 /* GPIOF_FUNC is not implemented yet */
360 if (mxc_gpio_is_output(bank->regs, offset))
361 return GPIOF_OUTPUT;
362 else
363 return GPIOF_INPUT;
364}
365
366static const struct dm_gpio_ops gpio_mxc_ops = {
367 .request = mxc_gpio_request,
368 .free = mxc_gpio_free,
369 .direction_input = mxc_gpio_direction_input,
370 .direction_output = mxc_gpio_direction_output,
371 .get_value = mxc_gpio_get_value,
372 .set_value = mxc_gpio_set_value,
373 .get_function = mxc_gpio_get_function,
374 .get_state = mxc_gpio_get_state,
375};
376
377static const struct mxc_gpio_plat mxc_plat[] = {
378 { (struct gpio_regs *)GPIO1_BASE_ADDR },
379 { (struct gpio_regs *)GPIO2_BASE_ADDR },
380 { (struct gpio_regs *)GPIO3_BASE_ADDR },
381#if defined(CONFIG_MX25) || defined(CONFIG_MX27) || defined(CONFIG_MX51) || \
382 defined(CONFIG_MX53) || defined(CONFIG_MX6)
383 { (struct gpio_regs *)GPIO4_BASE_ADDR },
384#endif
385#if defined(CONFIG_MX27) || defined(CONFIG_MX53) || defined(CONFIG_MX6)
386 { (struct gpio_regs *)GPIO5_BASE_ADDR },
387 { (struct gpio_regs *)GPIO6_BASE_ADDR },
388#endif
389#if defined(CONFIG_MX53) || defined(CONFIG_MX6)
390 { (struct gpio_regs *)GPIO7_BASE_ADDR },
391#endif
392};
393
394static int mxc_gpio_probe(struct udevice *dev)
395{
396 struct mxc_bank_info *bank = dev_get_priv(dev);
397 struct mxc_gpio_plat *plat = dev_get_platdata(dev);
398 struct gpio_dev_priv *uc_priv = dev->uclass_priv;
399 int banknum;
400 char name[18], *str;
401
402 banknum = plat - mxc_plat;
403 sprintf(name, "GPIO%d_", banknum + 1);
404 str = strdup(name);
405 if (!str)
406 return -ENOMEM;
407 uc_priv->bank_name = str;
408 uc_priv->gpio_count = GPIO_PER_BANK;
409 bank->regs = plat->regs;
410
411 return 0;
412}
413
414U_BOOT_DRIVER(gpio_mxc) = {
415 .name = "gpio_mxc",
416 .id = UCLASS_GPIO,
417 .ops = &gpio_mxc_ops,
418 .probe = mxc_gpio_probe,
419 .priv_auto_alloc_size = sizeof(struct mxc_bank_info),
420};
421
422U_BOOT_DEVICES(mxc_gpios) = {
423 { "gpio_mxc", &mxc_plat[0] },
424 { "gpio_mxc", &mxc_plat[1] },
425 { "gpio_mxc", &mxc_plat[2] },
426#if defined(CONFIG_MX25) || defined(CONFIG_MX27) || defined(CONFIG_MX51) || \
427 defined(CONFIG_MX53) || defined(CONFIG_MX6)
428 { "gpio_mxc", &mxc_plat[3] },
429#endif
430#if defined(CONFIG_MX27) || defined(CONFIG_MX53) || defined(CONFIG_MX6)
431 { "gpio_mxc", &mxc_plat[4] },
432 { "gpio_mxc", &mxc_plat[5] },
433#endif
434#if defined(CONFIG_MX53) || defined(CONFIG_MX6)
435 { "gpio_mxc", &mxc_plat[6] },
436#endif
437};
438#endif