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> |
| 39 | #include <asm/arch/gpio.h> |
| 40 | #include <asm/io.h> |
| 41 | #include <asm/errno.h> |
| 42 | |
| 43 | static struct gpio_bank gpio_bank_34xx[6] = { |
| 44 | { (void *)OMAP34XX_GPIO1_BASE, METHOD_GPIO_24XX }, |
| 45 | { (void *)OMAP34XX_GPIO2_BASE, METHOD_GPIO_24XX }, |
| 46 | { (void *)OMAP34XX_GPIO3_BASE, METHOD_GPIO_24XX }, |
| 47 | { (void *)OMAP34XX_GPIO4_BASE, METHOD_GPIO_24XX }, |
| 48 | { (void *)OMAP34XX_GPIO5_BASE, METHOD_GPIO_24XX }, |
| 49 | { (void *)OMAP34XX_GPIO6_BASE, METHOD_GPIO_24XX }, |
| 50 | }; |
| 51 | |
| 52 | static struct gpio_bank *gpio_bank = &gpio_bank_34xx[0]; |
| 53 | |
| 54 | static inline struct gpio_bank *get_gpio_bank(int gpio) |
| 55 | { |
| 56 | return &gpio_bank[gpio >> 5]; |
| 57 | } |
| 58 | |
| 59 | static inline int get_gpio_index(int gpio) |
| 60 | { |
| 61 | return gpio & 0x1f; |
| 62 | } |
| 63 | |
| 64 | static inline int gpio_valid(int gpio) |
| 65 | { |
| 66 | if (gpio < 0) |
| 67 | return -1; |
| 68 | if (gpio < 192) |
| 69 | return 0; |
| 70 | return -1; |
| 71 | } |
| 72 | |
| 73 | static int check_gpio(int gpio) |
| 74 | { |
| 75 | if (gpio_valid(gpio) < 0) { |
| 76 | printf("ERROR : check_gpio: invalid GPIO %d\n", gpio); |
| 77 | return -1; |
| 78 | } |
| 79 | return 0; |
| 80 | } |
| 81 | |
| 82 | static void _set_gpio_direction(struct gpio_bank *bank, int gpio, int is_input) |
| 83 | { |
| 84 | void *reg = bank->base; |
| 85 | u32 l; |
| 86 | |
| 87 | switch (bank->method) { |
| 88 | case METHOD_GPIO_24XX: |
| 89 | reg += OMAP24XX_GPIO_OE; |
| 90 | break; |
| 91 | default: |
| 92 | return; |
| 93 | } |
| 94 | l = __raw_readl(reg); |
| 95 | if (is_input) |
| 96 | l |= 1 << gpio; |
| 97 | else |
| 98 | l &= ~(1 << gpio); |
| 99 | __raw_writel(l, reg); |
| 100 | } |
| 101 | |
| 102 | void omap_set_gpio_direction(int gpio, int is_input) |
| 103 | { |
| 104 | struct gpio_bank *bank; |
| 105 | |
| 106 | if (check_gpio(gpio) < 0) |
| 107 | return; |
| 108 | bank = get_gpio_bank(gpio); |
| 109 | _set_gpio_direction(bank, get_gpio_index(gpio), is_input); |
| 110 | } |
| 111 | |
| 112 | static void _set_gpio_dataout(struct gpio_bank *bank, int gpio, int enable) |
| 113 | { |
| 114 | void *reg = bank->base; |
| 115 | u32 l = 0; |
| 116 | |
| 117 | switch (bank->method) { |
| 118 | case METHOD_GPIO_24XX: |
| 119 | if (enable) |
| 120 | reg += OMAP24XX_GPIO_SETDATAOUT; |
| 121 | else |
| 122 | reg += OMAP24XX_GPIO_CLEARDATAOUT; |
| 123 | l = 1 << gpio; |
| 124 | break; |
| 125 | default: |
| 126 | printf("omap3-gpio unknown bank method %s %d\n", |
| 127 | __FILE__, __LINE__); |
| 128 | return; |
| 129 | } |
| 130 | __raw_writel(l, reg); |
| 131 | } |
| 132 | |
| 133 | void omap_set_gpio_dataout(int gpio, int enable) |
| 134 | { |
| 135 | struct gpio_bank *bank; |
| 136 | |
| 137 | if (check_gpio(gpio) < 0) |
| 138 | return; |
| 139 | bank = get_gpio_bank(gpio); |
| 140 | _set_gpio_dataout(bank, get_gpio_index(gpio), enable); |
| 141 | } |
| 142 | |
| 143 | int omap_get_gpio_datain(int gpio) |
| 144 | { |
| 145 | struct gpio_bank *bank; |
| 146 | void *reg; |
| 147 | |
| 148 | if (check_gpio(gpio) < 0) |
| 149 | return -EINVAL; |
| 150 | bank = get_gpio_bank(gpio); |
| 151 | reg = bank->base; |
| 152 | switch (bank->method) { |
| 153 | case METHOD_GPIO_24XX: |
| 154 | reg += OMAP24XX_GPIO_DATAIN; |
| 155 | break; |
| 156 | default: |
| 157 | return -EINVAL; |
| 158 | } |
| 159 | return (__raw_readl(reg) |
| 160 | & (1 << get_gpio_index(gpio))) != 0; |
| 161 | } |
| 162 | |
| 163 | static void _reset_gpio(struct gpio_bank *bank, int gpio) |
| 164 | { |
| 165 | _set_gpio_direction(bank, get_gpio_index(gpio), 1); |
| 166 | } |
| 167 | |
| 168 | int omap_request_gpio(int gpio) |
| 169 | { |
| 170 | if (check_gpio(gpio) < 0) |
| 171 | return -EINVAL; |
| 172 | |
| 173 | return 0; |
| 174 | } |
| 175 | |
| 176 | void omap_free_gpio(int gpio) |
| 177 | { |
| 178 | struct gpio_bank *bank; |
| 179 | |
| 180 | if (check_gpio(gpio) < 0) |
| 181 | return; |
| 182 | bank = get_gpio_bank(gpio); |
| 183 | |
| 184 | _reset_gpio(bank, gpio); |
| 185 | } |