Tom Rix | 0c872ec | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2009 Wind River Systems, Inc. |
| 3 | * Tom Rix <Tom.Rix@windriver.com> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU General Public License as |
| 7 | * published by the Free Software Foundation; either version 2 of |
| 8 | * the License, or (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 18 | * MA 02111-1307 USA |
| 19 | * |
| 20 | * This work is derived from the linux 2.6.27 kernel source |
| 21 | * To fetch, use the kernel repository |
| 22 | * git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git |
| 23 | * Use the v2.6.27 tag. |
| 24 | * |
| 25 | * Below is the original's header including its copyright |
| 26 | * |
| 27 | * linux/arch/arm/plat-omap/gpio.c |
| 28 | * |
| 29 | * Support functions for OMAP GPIO |
| 30 | * |
| 31 | * Copyright (C) 2003-2005 Nokia Corporation |
| 32 | * Written by Juha Yrjölä <juha.yrjola@nokia.com> |
| 33 | * |
| 34 | * This program is free software; you can redistribute it and/or modify |
| 35 | * it under the terms of the GNU General Public License version 2 as |
| 36 | * published by the Free Software Foundation. |
| 37 | */ |
| 38 | #include <common.h> |
Aneesh V | 25223a6 | 2011-07-21 09:29:29 -0400 | [diff] [blame^] | 39 | #include <asm/omap_gpio.h> |
Tom Rix | 0c872ec | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 40 | #include <asm/io.h> |
| 41 | #include <asm/errno.h> |
| 42 | |
Aneesh V | 25223a6 | 2011-07-21 09:29:29 -0400 | [diff] [blame^] | 43 | static inline const struct gpio_bank *get_gpio_bank(int gpio) |
Tom Rix | 0c872ec | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 44 | { |
Aneesh V | 25223a6 | 2011-07-21 09:29:29 -0400 | [diff] [blame^] | 45 | return &omap_gpio_bank[gpio >> 5]; |
Tom Rix | 0c872ec | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | static inline int get_gpio_index(int gpio) |
| 49 | { |
| 50 | return gpio & 0x1f; |
| 51 | } |
| 52 | |
| 53 | static inline int gpio_valid(int gpio) |
| 54 | { |
| 55 | if (gpio < 0) |
| 56 | return -1; |
| 57 | if (gpio < 192) |
| 58 | return 0; |
| 59 | return -1; |
| 60 | } |
| 61 | |
| 62 | static int check_gpio(int gpio) |
| 63 | { |
| 64 | if (gpio_valid(gpio) < 0) { |
| 65 | printf("ERROR : check_gpio: invalid GPIO %d\n", gpio); |
| 66 | return -1; |
| 67 | } |
| 68 | return 0; |
| 69 | } |
| 70 | |
Aneesh V | 25223a6 | 2011-07-21 09:29:29 -0400 | [diff] [blame^] | 71 | static void _set_gpio_direction(const struct gpio_bank *bank, int gpio, |
| 72 | int is_input) |
Tom Rix | 0c872ec | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 73 | { |
| 74 | void *reg = bank->base; |
| 75 | u32 l; |
| 76 | |
| 77 | switch (bank->method) { |
| 78 | case METHOD_GPIO_24XX: |
Aneesh V | 25223a6 | 2011-07-21 09:29:29 -0400 | [diff] [blame^] | 79 | reg += OMAP_GPIO_OE; |
Tom Rix | 0c872ec | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 80 | break; |
| 81 | default: |
| 82 | return; |
| 83 | } |
| 84 | l = __raw_readl(reg); |
| 85 | if (is_input) |
| 86 | l |= 1 << gpio; |
| 87 | else |
| 88 | l &= ~(1 << gpio); |
| 89 | __raw_writel(l, reg); |
| 90 | } |
| 91 | |
| 92 | void omap_set_gpio_direction(int gpio, int is_input) |
| 93 | { |
Aneesh V | 25223a6 | 2011-07-21 09:29:29 -0400 | [diff] [blame^] | 94 | const struct gpio_bank *bank; |
Tom Rix | 0c872ec | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 95 | |
| 96 | if (check_gpio(gpio) < 0) |
| 97 | return; |
| 98 | bank = get_gpio_bank(gpio); |
| 99 | _set_gpio_direction(bank, get_gpio_index(gpio), is_input); |
| 100 | } |
| 101 | |
Aneesh V | 25223a6 | 2011-07-21 09:29:29 -0400 | [diff] [blame^] | 102 | static void _set_gpio_dataout(const struct gpio_bank *bank, int gpio, |
| 103 | int enable) |
Tom Rix | 0c872ec | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 104 | { |
| 105 | void *reg = bank->base; |
| 106 | u32 l = 0; |
| 107 | |
| 108 | switch (bank->method) { |
| 109 | case METHOD_GPIO_24XX: |
| 110 | if (enable) |
Aneesh V | 25223a6 | 2011-07-21 09:29:29 -0400 | [diff] [blame^] | 111 | reg += OMAP_GPIO_SETDATAOUT; |
Tom Rix | 0c872ec | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 112 | else |
Aneesh V | 25223a6 | 2011-07-21 09:29:29 -0400 | [diff] [blame^] | 113 | reg += OMAP_GPIO_CLEARDATAOUT; |
Tom Rix | 0c872ec | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 114 | l = 1 << gpio; |
| 115 | break; |
| 116 | default: |
| 117 | printf("omap3-gpio unknown bank method %s %d\n", |
| 118 | __FILE__, __LINE__); |
| 119 | return; |
| 120 | } |
| 121 | __raw_writel(l, reg); |
| 122 | } |
| 123 | |
| 124 | void omap_set_gpio_dataout(int gpio, int enable) |
| 125 | { |
Aneesh V | 25223a6 | 2011-07-21 09:29:29 -0400 | [diff] [blame^] | 126 | const struct gpio_bank *bank; |
Tom Rix | 0c872ec | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 127 | |
| 128 | if (check_gpio(gpio) < 0) |
| 129 | return; |
| 130 | bank = get_gpio_bank(gpio); |
| 131 | _set_gpio_dataout(bank, get_gpio_index(gpio), enable); |
| 132 | } |
| 133 | |
| 134 | int omap_get_gpio_datain(int gpio) |
| 135 | { |
Aneesh V | 25223a6 | 2011-07-21 09:29:29 -0400 | [diff] [blame^] | 136 | const struct gpio_bank *bank; |
Tom Rix | 0c872ec | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 137 | void *reg; |
| 138 | |
| 139 | if (check_gpio(gpio) < 0) |
| 140 | return -EINVAL; |
| 141 | bank = get_gpio_bank(gpio); |
| 142 | reg = bank->base; |
| 143 | switch (bank->method) { |
| 144 | case METHOD_GPIO_24XX: |
Aneesh V | 25223a6 | 2011-07-21 09:29:29 -0400 | [diff] [blame^] | 145 | reg += OMAP_GPIO_DATAIN; |
Tom Rix | 0c872ec | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 146 | break; |
| 147 | default: |
| 148 | return -EINVAL; |
| 149 | } |
| 150 | return (__raw_readl(reg) |
| 151 | & (1 << get_gpio_index(gpio))) != 0; |
| 152 | } |
| 153 | |
Aneesh V | 25223a6 | 2011-07-21 09:29:29 -0400 | [diff] [blame^] | 154 | static void _reset_gpio(const struct gpio_bank *bank, int gpio) |
Tom Rix | 0c872ec | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 155 | { |
| 156 | _set_gpio_direction(bank, get_gpio_index(gpio), 1); |
| 157 | } |
| 158 | |
| 159 | int omap_request_gpio(int gpio) |
| 160 | { |
| 161 | if (check_gpio(gpio) < 0) |
| 162 | return -EINVAL; |
| 163 | |
| 164 | return 0; |
| 165 | } |
| 166 | |
| 167 | void omap_free_gpio(int gpio) |
| 168 | { |
Aneesh V | 25223a6 | 2011-07-21 09:29:29 -0400 | [diff] [blame^] | 169 | const struct gpio_bank *bank; |
Tom Rix | 0c872ec | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 170 | |
| 171 | if (check_gpio(gpio) < 0) |
| 172 | return; |
| 173 | bank = get_gpio_bank(gpio); |
| 174 | |
| 175 | _reset_gpio(bank, gpio); |
| 176 | } |