Guennadi Liakhovetski | 7c6a856 | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 |
| 3 | * Guennadi Liakhovetski, DENX Software Engineering, <lg@denx.de> |
| 4 | * |
Stefano Babic | 7faee91 | 2011-08-21 10:45:44 +0200 | [diff] [blame] | 5 | * Copyright (C) 2011 |
| 6 | * Stefano Babic, DENX Software Engineering, <sbabic@denx.de> |
| 7 | * |
Guennadi Liakhovetski | 7c6a856 | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 8 | * See file CREDITS for list of people who contributed to this |
| 9 | * project. |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or |
| 12 | * modify it under the terms of the GNU General Public License as |
| 13 | * published by the Free Software Foundation; either version 2 of |
| 14 | * the License, or (at your option) any later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License |
| 22 | * along with this program; if not, write to the Free Software |
| 23 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 24 | * MA 02111-1307 USA |
| 25 | */ |
| 26 | #include <common.h> |
Stefano Babic | d77fe99 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 27 | #include <asm/arch/imx-regs.h> |
Stefano Babic | 7faee91 | 2011-08-21 10:45:44 +0200 | [diff] [blame] | 28 | #include <asm/gpio.h> |
Stefano Babic | d77fe99 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 29 | #include <asm/io.h> |
Fabio Estevam | 893eafb | 2011-04-09 10:43:24 +0000 | [diff] [blame] | 30 | #include <errno.h> |
Guennadi Liakhovetski | 7c6a856 | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 31 | |
Stefano Babic | 7faee91 | 2011-08-21 10:45:44 +0200 | [diff] [blame] | 32 | enum mxc_gpio_direction { |
| 33 | MXC_GPIO_DIRECTION_IN, |
| 34 | MXC_GPIO_DIRECTION_OUT, |
| 35 | }; |
| 36 | |
Vikram Narayanan | 10999cf | 2012-04-10 04:26:08 +0000 | [diff] [blame] | 37 | #define GPIO_TO_PORT(n) (n / 32) |
Stefano Babic | 7faee91 | 2011-08-21 10:45:44 +0200 | [diff] [blame] | 38 | |
Guennadi Liakhovetski | 7c6a856 | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 39 | /* GPIO port description */ |
| 40 | static unsigned long gpio_ports[] = { |
Stefano Babic | d77fe99 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 41 | [0] = GPIO1_BASE_ADDR, |
| 42 | [1] = GPIO2_BASE_ADDR, |
| 43 | [2] = GPIO3_BASE_ADDR, |
Timo Ketola | 4fce1db | 2012-04-18 22:55:29 +0000 | [diff] [blame] | 44 | #if defined(CONFIG_MX25) || defined(CONFIG_MX51) || defined(CONFIG_MX53) || \ |
| 45 | defined(CONFIG_MX6Q) |
Stefano Babic | d77fe99 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 46 | [3] = GPIO4_BASE_ADDR, |
| 47 | #endif |
Jason Liu | e581b01 | 2011-11-25 00:18:03 +0000 | [diff] [blame] | 48 | #if defined(CONFIG_MX53) || defined(CONFIG_MX6Q) |
Liu Hui-R64343 | a71164c | 2011-01-03 22:27:38 +0000 | [diff] [blame] | 49 | [4] = GPIO5_BASE_ADDR, |
| 50 | [5] = GPIO6_BASE_ADDR, |
| 51 | [6] = GPIO7_BASE_ADDR, |
| 52 | #endif |
Guennadi Liakhovetski | 7c6a856 | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 53 | }; |
| 54 | |
Stefano Babic | 7faee91 | 2011-08-21 10:45:44 +0200 | [diff] [blame] | 55 | static int mxc_gpio_direction(unsigned int gpio, |
| 56 | enum mxc_gpio_direction direction) |
Guennadi Liakhovetski | 7c6a856 | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 57 | { |
Vikram Narayanan | fbdf6bc | 2012-04-10 04:26:20 +0000 | [diff] [blame] | 58 | unsigned int port = GPIO_TO_PORT(gpio); |
Stefano Babic | d77fe99 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 59 | struct gpio_regs *regs; |
Guennadi Liakhovetski | 7c6a856 | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 60 | u32 l; |
| 61 | |
| 62 | if (port >= ARRAY_SIZE(gpio_ports)) |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 63 | return -1; |
Guennadi Liakhovetski | 7c6a856 | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 64 | |
| 65 | gpio &= 0x1f; |
| 66 | |
Stefano Babic | d77fe99 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 67 | regs = (struct gpio_regs *)gpio_ports[port]; |
| 68 | |
| 69 | l = readl(®s->gpio_dir); |
| 70 | |
Guennadi Liakhovetski | 7c6a856 | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 71 | switch (direction) { |
Stefano Babic | d77fe99 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 72 | case MXC_GPIO_DIRECTION_OUT: |
Guennadi Liakhovetski | 7c6a856 | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 73 | l |= 1 << gpio; |
| 74 | break; |
Stefano Babic | d77fe99 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 75 | case MXC_GPIO_DIRECTION_IN: |
Guennadi Liakhovetski | 7c6a856 | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 76 | l &= ~(1 << gpio); |
| 77 | } |
Stefano Babic | d77fe99 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 78 | writel(l, ®s->gpio_dir); |
Guennadi Liakhovetski | 7c6a856 | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 79 | |
| 80 | return 0; |
| 81 | } |
| 82 | |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 83 | int gpio_set_value(unsigned gpio, int value) |
Guennadi Liakhovetski | 7c6a856 | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 84 | { |
Vikram Narayanan | fbdf6bc | 2012-04-10 04:26:20 +0000 | [diff] [blame] | 85 | unsigned int port = GPIO_TO_PORT(gpio); |
Stefano Babic | d77fe99 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 86 | struct gpio_regs *regs; |
Guennadi Liakhovetski | 7c6a856 | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 87 | u32 l; |
| 88 | |
| 89 | if (port >= ARRAY_SIZE(gpio_ports)) |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 90 | return -1; |
Guennadi Liakhovetski | 7c6a856 | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 91 | |
| 92 | gpio &= 0x1f; |
| 93 | |
Stefano Babic | d77fe99 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 94 | regs = (struct gpio_regs *)gpio_ports[port]; |
| 95 | |
| 96 | l = readl(®s->gpio_dr); |
Guennadi Liakhovetski | 7c6a856 | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 97 | if (value) |
| 98 | l |= 1 << gpio; |
| 99 | else |
| 100 | l &= ~(1 << gpio); |
Stefano Babic | d77fe99 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 101 | writel(l, ®s->gpio_dr); |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 102 | |
| 103 | return 0; |
Guennadi Liakhovetski | 7c6a856 | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 104 | } |
Stefano Babic | a44d2a5 | 2010-04-13 12:07:00 +0200 | [diff] [blame] | 105 | |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 106 | int gpio_get_value(unsigned gpio) |
Stefano Babic | a44d2a5 | 2010-04-13 12:07:00 +0200 | [diff] [blame] | 107 | { |
Vikram Narayanan | fbdf6bc | 2012-04-10 04:26:20 +0000 | [diff] [blame] | 108 | unsigned int port = GPIO_TO_PORT(gpio); |
Stefano Babic | d77fe99 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 109 | struct gpio_regs *regs; |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 110 | u32 val; |
Stefano Babic | a44d2a5 | 2010-04-13 12:07:00 +0200 | [diff] [blame] | 111 | |
| 112 | if (port >= ARRAY_SIZE(gpio_ports)) |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 113 | return -1; |
Stefano Babic | a44d2a5 | 2010-04-13 12:07:00 +0200 | [diff] [blame] | 114 | |
| 115 | gpio &= 0x1f; |
| 116 | |
Stefano Babic | d77fe99 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 117 | regs = (struct gpio_regs *)gpio_ports[port]; |
| 118 | |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 119 | val = (readl(®s->gpio_dr) >> gpio) & 0x01; |
Stefano Babic | a44d2a5 | 2010-04-13 12:07:00 +0200 | [diff] [blame] | 120 | |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 121 | return val; |
Stefano Babic | a44d2a5 | 2010-04-13 12:07:00 +0200 | [diff] [blame] | 122 | } |
Stefano Babic | 7faee91 | 2011-08-21 10:45:44 +0200 | [diff] [blame] | 123 | |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 124 | int gpio_request(unsigned gpio, const char *label) |
Stefano Babic | 7faee91 | 2011-08-21 10:45:44 +0200 | [diff] [blame] | 125 | { |
Vikram Narayanan | fbdf6bc | 2012-04-10 04:26:20 +0000 | [diff] [blame] | 126 | unsigned int port = GPIO_TO_PORT(gpio); |
Stefano Babic | 7faee91 | 2011-08-21 10:45:44 +0200 | [diff] [blame] | 127 | if (port >= ARRAY_SIZE(gpio_ports)) |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 128 | return -1; |
Stefano Babic | 7faee91 | 2011-08-21 10:45:44 +0200 | [diff] [blame] | 129 | return 0; |
| 130 | } |
| 131 | |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 132 | int gpio_free(unsigned gpio) |
Stefano Babic | 7faee91 | 2011-08-21 10:45:44 +0200 | [diff] [blame] | 133 | { |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 134 | return 0; |
Stefano Babic | 7faee91 | 2011-08-21 10:45:44 +0200 | [diff] [blame] | 135 | } |
| 136 | |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 137 | int gpio_direction_input(unsigned gpio) |
Stefano Babic | 7faee91 | 2011-08-21 10:45:44 +0200 | [diff] [blame] | 138 | { |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 139 | return mxc_gpio_direction(gpio, MXC_GPIO_DIRECTION_IN); |
Stefano Babic | 7faee91 | 2011-08-21 10:45:44 +0200 | [diff] [blame] | 140 | } |
| 141 | |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 142 | int gpio_direction_output(unsigned gpio, int value) |
Stefano Babic | 7faee91 | 2011-08-21 10:45:44 +0200 | [diff] [blame] | 143 | { |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 144 | int ret = mxc_gpio_direction(gpio, MXC_GPIO_DIRECTION_OUT); |
Stefano Babic | 7faee91 | 2011-08-21 10:45:44 +0200 | [diff] [blame] | 145 | |
| 146 | if (ret < 0) |
| 147 | return ret; |
| 148 | |
Vikram Narayanan | c0a17fe | 2012-04-10 04:26:30 +0000 | [diff] [blame] | 149 | return gpio_set_value(gpio, value); |
Stefano Babic | 7faee91 | 2011-08-21 10:45:44 +0200 | [diff] [blame] | 150 | } |