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 | * |
Stefano Babic | d8e0ca8 | 2011-08-21 10:45:44 +0200 | [diff] [blame] | 5 | * Copyright (C) 2011 |
| 6 | * Stefano Babic, DENX Software Engineering, <sbabic@denx.de> |
| 7 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame^] | 8 | * SPDX-License-Identifier: GPL-2.0+ |
Guennadi Liakhovetski | b30de3c | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 9 | */ |
| 10 | #include <common.h> |
Stefano Babic | c4ea142 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 11 | #include <asm/arch/imx-regs.h> |
Stefano Babic | d8e0ca8 | 2011-08-21 10:45:44 +0200 | [diff] [blame] | 12 | #include <asm/gpio.h> |
Stefano Babic | c4ea142 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 13 | #include <asm/io.h> |
Fabio Estevam | e53bcd9 | 2011-04-09 10:43:24 +0000 | [diff] [blame] | 14 | #include <errno.h> |
Guennadi Liakhovetski | b30de3c | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 15 | |
Stefano Babic | d8e0ca8 | 2011-08-21 10:45:44 +0200 | [diff] [blame] | 16 | enum mxc_gpio_direction { |
| 17 | MXC_GPIO_DIRECTION_IN, |
| 18 | MXC_GPIO_DIRECTION_OUT, |
| 19 | }; |
| 20 | |
Vikram Narayanan | 8d28c21 | 2012-04-10 04:26:08 +0000 | [diff] [blame] | 21 | #define GPIO_TO_PORT(n) (n / 32) |
Stefano Babic | d8e0ca8 | 2011-08-21 10:45:44 +0200 | [diff] [blame] | 22 | |
Guennadi Liakhovetski | b30de3c | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 23 | /* GPIO port description */ |
| 24 | static unsigned long gpio_ports[] = { |
Stefano Babic | c4ea142 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 25 | [0] = GPIO1_BASE_ADDR, |
| 26 | [1] = GPIO2_BASE_ADDR, |
| 27 | [2] = GPIO3_BASE_ADDR, |
trem | e71c39d | 2012-08-25 05:30:33 +0000 | [diff] [blame] | 28 | #if defined(CONFIG_MX25) || defined(CONFIG_MX27) || defined(CONFIG_MX51) || \ |
Troy Kisky | 5ea6d7c | 2012-10-23 10:57:47 +0000 | [diff] [blame] | 29 | defined(CONFIG_MX53) || defined(CONFIG_MX6) |
Stefano Babic | c4ea142 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 30 | [3] = GPIO4_BASE_ADDR, |
| 31 | #endif |
Troy Kisky | 5ea6d7c | 2012-10-23 10:57:47 +0000 | [diff] [blame] | 32 | #if defined(CONFIG_MX27) || defined(CONFIG_MX53) || defined(CONFIG_MX6) |
Liu Hui-R64343 | 01643ec | 2011-01-03 22:27:38 +0000 | [diff] [blame] | 33 | [4] = GPIO5_BASE_ADDR, |
| 34 | [5] = GPIO6_BASE_ADDR, |
trem | e71c39d | 2012-08-25 05:30:33 +0000 | [diff] [blame] | 35 | #endif |
Troy Kisky | 5ea6d7c | 2012-10-23 10:57:47 +0000 | [diff] [blame] | 36 | #if defined(CONFIG_MX53) || defined(CONFIG_MX6) |
Liu Hui-R64343 | 01643ec | 2011-01-03 22:27:38 +0000 | [diff] [blame] | 37 | [6] = GPIO7_BASE_ADDR, |
| 38 | #endif |
Guennadi Liakhovetski | b30de3c | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 39 | }; |
| 40 | |
Stefano Babic | d8e0ca8 | 2011-08-21 10:45:44 +0200 | [diff] [blame] | 41 | static int mxc_gpio_direction(unsigned int gpio, |
| 42 | enum mxc_gpio_direction direction) |
Guennadi Liakhovetski | b30de3c | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 43 | { |
Vikram Narayanan | be28255 | 2012-04-10 04:26:20 +0000 | [diff] [blame] | 44 | unsigned int port = GPIO_TO_PORT(gpio); |
Stefano Babic | c4ea142 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 45 | struct gpio_regs *regs; |
Guennadi Liakhovetski | b30de3c | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 46 | u32 l; |
| 47 | |
| 48 | if (port >= ARRAY_SIZE(gpio_ports)) |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 49 | return -1; |
Guennadi Liakhovetski | b30de3c | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 50 | |
| 51 | gpio &= 0x1f; |
| 52 | |
Stefano Babic | c4ea142 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 53 | regs = (struct gpio_regs *)gpio_ports[port]; |
| 54 | |
| 55 | l = readl(®s->gpio_dir); |
| 56 | |
Guennadi Liakhovetski | b30de3c | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 57 | switch (direction) { |
Stefano Babic | c4ea142 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 58 | case MXC_GPIO_DIRECTION_OUT: |
Guennadi Liakhovetski | b30de3c | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 59 | l |= 1 << gpio; |
| 60 | break; |
Stefano Babic | c4ea142 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 61 | case MXC_GPIO_DIRECTION_IN: |
Guennadi Liakhovetski | b30de3c | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 62 | l &= ~(1 << gpio); |
| 63 | } |
Stefano Babic | c4ea142 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 64 | writel(l, ®s->gpio_dir); |
Guennadi Liakhovetski | b30de3c | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 65 | |
| 66 | return 0; |
| 67 | } |
| 68 | |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 69 | int gpio_set_value(unsigned gpio, int value) |
Guennadi Liakhovetski | b30de3c | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 70 | { |
Vikram Narayanan | be28255 | 2012-04-10 04:26:20 +0000 | [diff] [blame] | 71 | unsigned int port = GPIO_TO_PORT(gpio); |
Stefano Babic | c4ea142 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 72 | struct gpio_regs *regs; |
Guennadi Liakhovetski | b30de3c | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 73 | u32 l; |
| 74 | |
| 75 | if (port >= ARRAY_SIZE(gpio_ports)) |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 76 | return -1; |
Guennadi Liakhovetski | b30de3c | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 77 | |
| 78 | gpio &= 0x1f; |
| 79 | |
Stefano Babic | c4ea142 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 80 | regs = (struct gpio_regs *)gpio_ports[port]; |
| 81 | |
| 82 | l = readl(®s->gpio_dr); |
Guennadi Liakhovetski | b30de3c | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 83 | if (value) |
| 84 | l |= 1 << gpio; |
| 85 | else |
| 86 | l &= ~(1 << gpio); |
Stefano Babic | c4ea142 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 87 | writel(l, ®s->gpio_dr); |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 88 | |
| 89 | return 0; |
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 | |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 92 | int gpio_get_value(unsigned gpio) |
Stefano Babic | 7d27cd0 | 2010-04-13 12:07:00 +0200 | [diff] [blame] | 93 | { |
Vikram Narayanan | be28255 | 2012-04-10 04:26:20 +0000 | [diff] [blame] | 94 | unsigned int port = GPIO_TO_PORT(gpio); |
Stefano Babic | c4ea142 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 95 | struct gpio_regs *regs; |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 96 | u32 val; |
Stefano Babic | 7d27cd0 | 2010-04-13 12:07:00 +0200 | [diff] [blame] | 97 | |
| 98 | if (port >= ARRAY_SIZE(gpio_ports)) |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 99 | return -1; |
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 | |
Benoît Thébaudeau | 5dafa45 | 2012-08-20 10:55:41 +0000 | [diff] [blame] | 105 | val = (readl(®s->gpio_psr) >> gpio) & 0x01; |
Stefano Babic | 7d27cd0 | 2010-04-13 12:07:00 +0200 | [diff] [blame] | 106 | |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 107 | return val; |
Stefano Babic | 7d27cd0 | 2010-04-13 12:07:00 +0200 | [diff] [blame] | 108 | } |
Stefano Babic | d8e0ca8 | 2011-08-21 10:45:44 +0200 | [diff] [blame] | 109 | |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 110 | int gpio_request(unsigned gpio, const char *label) |
Stefano Babic | d8e0ca8 | 2011-08-21 10:45:44 +0200 | [diff] [blame] | 111 | { |
Vikram Narayanan | be28255 | 2012-04-10 04:26:20 +0000 | [diff] [blame] | 112 | unsigned int port = GPIO_TO_PORT(gpio); |
Stefano Babic | d8e0ca8 | 2011-08-21 10:45:44 +0200 | [diff] [blame] | 113 | if (port >= ARRAY_SIZE(gpio_ports)) |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 114 | return -1; |
Stefano Babic | d8e0ca8 | 2011-08-21 10:45:44 +0200 | [diff] [blame] | 115 | return 0; |
| 116 | } |
| 117 | |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 118 | int gpio_free(unsigned gpio) |
Stefano Babic | d8e0ca8 | 2011-08-21 10:45:44 +0200 | [diff] [blame] | 119 | { |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 120 | return 0; |
Stefano Babic | d8e0ca8 | 2011-08-21 10:45:44 +0200 | [diff] [blame] | 121 | } |
| 122 | |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 123 | int gpio_direction_input(unsigned gpio) |
Stefano Babic | d8e0ca8 | 2011-08-21 10:45:44 +0200 | [diff] [blame] | 124 | { |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 125 | return mxc_gpio_direction(gpio, MXC_GPIO_DIRECTION_IN); |
Stefano Babic | d8e0ca8 | 2011-08-21 10:45:44 +0200 | [diff] [blame] | 126 | } |
| 127 | |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 128 | int gpio_direction_output(unsigned gpio, int value) |
Stefano Babic | d8e0ca8 | 2011-08-21 10:45:44 +0200 | [diff] [blame] | 129 | { |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 130 | int ret = mxc_gpio_direction(gpio, MXC_GPIO_DIRECTION_OUT); |
Stefano Babic | d8e0ca8 | 2011-08-21 10:45:44 +0200 | [diff] [blame] | 131 | |
| 132 | if (ret < 0) |
| 133 | return ret; |
| 134 | |
Vikram Narayanan | d71c9c9 | 2012-04-10 04:26:30 +0000 | [diff] [blame] | 135 | return gpio_set_value(gpio, value); |
Stefano Babic | d8e0ca8 | 2011-08-21 10:45:44 +0200 | [diff] [blame] | 136 | } |