Guennadi Liakhovetski | b30de3c | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 |
| 3 | * Guennadi Liakhovetski, DENX Software Engineering, <lg@denx.de> |
| 4 | * |
| 5 | * See file CREDITS for list of people who contributed to this |
| 6 | * project. |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License as |
| 10 | * published by the Free Software Foundation; either version 2 of |
| 11 | * the License, or (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 21 | * MA 02111-1307 USA |
| 22 | */ |
| 23 | #include <common.h> |
Stefano Babic | c4ea142 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 24 | #include <asm/arch/imx-regs.h> |
Stefano Babic | c4ea142 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 25 | #include <asm/io.h> |
| 26 | #include <mxc_gpio.h> |
Fabio Estevam | e53bcd9 | 2011-04-09 10:43:24 +0000 | [diff] [blame] | 27 | #include <errno.h> |
Guennadi Liakhovetski | b30de3c | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 28 | |
| 29 | /* GPIO port description */ |
| 30 | static unsigned long gpio_ports[] = { |
Stefano Babic | c4ea142 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 31 | [0] = GPIO1_BASE_ADDR, |
| 32 | [1] = GPIO2_BASE_ADDR, |
| 33 | [2] = GPIO3_BASE_ADDR, |
Liu Hui-R64343 | 01643ec | 2011-01-03 22:27:38 +0000 | [diff] [blame] | 34 | #if defined(CONFIG_MX51) || defined(CONFIG_MX53) |
Stefano Babic | c4ea142 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 35 | [3] = GPIO4_BASE_ADDR, |
| 36 | #endif |
Liu Hui-R64343 | 01643ec | 2011-01-03 22:27:38 +0000 | [diff] [blame] | 37 | #if defined(CONFIG_MX53) |
| 38 | [4] = GPIO5_BASE_ADDR, |
| 39 | [5] = GPIO6_BASE_ADDR, |
| 40 | [6] = GPIO7_BASE_ADDR, |
| 41 | #endif |
Guennadi Liakhovetski | b30de3c | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 42 | }; |
| 43 | |
Stefano Babic | c4ea142 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 44 | int mxc_gpio_direction(unsigned int gpio, enum mxc_gpio_direction direction) |
Guennadi Liakhovetski | b30de3c | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 45 | { |
| 46 | unsigned int port = gpio >> 5; |
Stefano Babic | c4ea142 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 47 | struct gpio_regs *regs; |
Guennadi Liakhovetski | b30de3c | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 48 | u32 l; |
| 49 | |
| 50 | if (port >= ARRAY_SIZE(gpio_ports)) |
Fabio Estevam | e53bcd9 | 2011-04-09 10:43:24 +0000 | [diff] [blame] | 51 | return -EINVAL; |
Guennadi Liakhovetski | b30de3c | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 52 | |
| 53 | gpio &= 0x1f; |
| 54 | |
Stefano Babic | c4ea142 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 55 | regs = (struct gpio_regs *)gpio_ports[port]; |
| 56 | |
| 57 | l = readl(®s->gpio_dir); |
| 58 | |
Guennadi Liakhovetski | b30de3c | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 59 | switch (direction) { |
Stefano Babic | c4ea142 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 60 | case MXC_GPIO_DIRECTION_OUT: |
Guennadi Liakhovetski | b30de3c | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 61 | l |= 1 << gpio; |
| 62 | break; |
Stefano Babic | c4ea142 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 63 | case MXC_GPIO_DIRECTION_IN: |
Guennadi Liakhovetski | b30de3c | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 64 | l &= ~(1 << gpio); |
| 65 | } |
Stefano Babic | c4ea142 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 66 | writel(l, ®s->gpio_dir); |
Guennadi Liakhovetski | b30de3c | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 67 | |
| 68 | return 0; |
| 69 | } |
| 70 | |
Stefano Babic | c4ea142 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 71 | void mxc_gpio_set(unsigned int gpio, unsigned int value) |
Guennadi Liakhovetski | b30de3c | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 72 | { |
| 73 | unsigned int port = gpio >> 5; |
Stefano Babic | c4ea142 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 74 | struct gpio_regs *regs; |
Guennadi Liakhovetski | b30de3c | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 75 | u32 l; |
| 76 | |
| 77 | if (port >= ARRAY_SIZE(gpio_ports)) |
| 78 | return; |
| 79 | |
| 80 | gpio &= 0x1f; |
| 81 | |
Stefano Babic | c4ea142 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 82 | regs = (struct gpio_regs *)gpio_ports[port]; |
| 83 | |
| 84 | l = readl(®s->gpio_dr); |
Guennadi Liakhovetski | b30de3c | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 85 | if (value) |
| 86 | l |= 1 << gpio; |
| 87 | else |
| 88 | l &= ~(1 << gpio); |
Stefano Babic | c4ea142 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 89 | writel(l, ®s->gpio_dr); |
Guennadi Liakhovetski | b30de3c | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 90 | } |
Stefano Babic | 7d27cd0 | 2010-04-13 12:07:00 +0200 | [diff] [blame] | 91 | |
Stefano Babic | c4ea142 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 92 | int mxc_gpio_get(unsigned int gpio) |
Stefano Babic | 7d27cd0 | 2010-04-13 12:07:00 +0200 | [diff] [blame] | 93 | { |
| 94 | unsigned int port = gpio >> 5; |
Stefano Babic | c4ea142 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 95 | struct gpio_regs *regs; |
Stefano Babic | 7d27cd0 | 2010-04-13 12:07:00 +0200 | [diff] [blame] | 96 | u32 l; |
| 97 | |
| 98 | if (port >= ARRAY_SIZE(gpio_ports)) |
Fabio Estevam | e53bcd9 | 2011-04-09 10:43:24 +0000 | [diff] [blame] | 99 | return -EINVAL; |
Stefano Babic | 7d27cd0 | 2010-04-13 12:07:00 +0200 | [diff] [blame] | 100 | |
| 101 | gpio &= 0x1f; |
| 102 | |
Stefano Babic | c4ea142 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 103 | regs = (struct gpio_regs *)gpio_ports[port]; |
| 104 | |
| 105 | l = (readl(®s->gpio_dr) >> gpio) & 0x01; |
Stefano Babic | 7d27cd0 | 2010-04-13 12:07:00 +0200 | [diff] [blame] | 106 | |
| 107 | return l; |
| 108 | } |