Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 1 | /* |
| 2 | * File: arch/blackfin/kernel/bfin_gpio.c |
| 3 | * Based on: |
| 4 | * Author: Michael Hennerich (hennerich@blackfin.uclinux.org) |
| 5 | * |
| 6 | * Created: |
| 7 | * Description: GPIO Abstraction Layer |
| 8 | * |
| 9 | * Modified: |
| 10 | * Copyright 2006 Analog Devices Inc. |
| 11 | * |
| 12 | * Bugs: Enter bugs at http://blackfin.uclinux.org/ |
| 13 | * |
| 14 | * This program is free software; you can redistribute it and/or modify |
| 15 | * it under the terms of the GNU General Public License as published by |
| 16 | * the Free Software Foundation; either version 2 of the License, or |
| 17 | * (at your option) any later version. |
| 18 | * |
| 19 | * This program is distributed in the hope that it will be useful, |
| 20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 | * GNU General Public License for more details. |
| 23 | * |
| 24 | * You should have received a copy of the GNU General Public License |
| 25 | * along with this program; if not, see the file COPYING, or write |
| 26 | * to the Free Software Foundation, Inc., |
| 27 | * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 28 | */ |
| 29 | |
| 30 | /* |
| 31 | * Number BF537/6/4 BF561 BF533/2/1 |
| 32 | * |
| 33 | * GPIO_0 PF0 PF0 PF0 |
| 34 | * GPIO_1 PF1 PF1 PF1 |
| 35 | * GPIO_2 PF2 PF2 PF2 |
| 36 | * GPIO_3 PF3 PF3 PF3 |
| 37 | * GPIO_4 PF4 PF4 PF4 |
| 38 | * GPIO_5 PF5 PF5 PF5 |
| 39 | * GPIO_6 PF6 PF6 PF6 |
| 40 | * GPIO_7 PF7 PF7 PF7 |
| 41 | * GPIO_8 PF8 PF8 PF8 |
| 42 | * GPIO_9 PF9 PF9 PF9 |
| 43 | * GPIO_10 PF10 PF10 PF10 |
| 44 | * GPIO_11 PF11 PF11 PF11 |
| 45 | * GPIO_12 PF12 PF12 PF12 |
| 46 | * GPIO_13 PF13 PF13 PF13 |
| 47 | * GPIO_14 PF14 PF14 PF14 |
| 48 | * GPIO_15 PF15 PF15 PF15 |
| 49 | * GPIO_16 PG0 PF16 |
| 50 | * GPIO_17 PG1 PF17 |
| 51 | * GPIO_18 PG2 PF18 |
| 52 | * GPIO_19 PG3 PF19 |
| 53 | * GPIO_20 PG4 PF20 |
| 54 | * GPIO_21 PG5 PF21 |
| 55 | * GPIO_22 PG6 PF22 |
| 56 | * GPIO_23 PG7 PF23 |
| 57 | * GPIO_24 PG8 PF24 |
| 58 | * GPIO_25 PG9 PF25 |
| 59 | * GPIO_26 PG10 PF26 |
| 60 | * GPIO_27 PG11 PF27 |
| 61 | * GPIO_28 PG12 PF28 |
| 62 | * GPIO_29 PG13 PF29 |
| 63 | * GPIO_30 PG14 PF30 |
| 64 | * GPIO_31 PG15 PF31 |
| 65 | * GPIO_32 PH0 PF32 |
| 66 | * GPIO_33 PH1 PF33 |
| 67 | * GPIO_34 PH2 PF34 |
| 68 | * GPIO_35 PH3 PF35 |
| 69 | * GPIO_36 PH4 PF36 |
| 70 | * GPIO_37 PH5 PF37 |
| 71 | * GPIO_38 PH6 PF38 |
| 72 | * GPIO_39 PH7 PF39 |
| 73 | * GPIO_40 PH8 PF40 |
| 74 | * GPIO_41 PH9 PF41 |
| 75 | * GPIO_42 PH10 PF42 |
| 76 | * GPIO_43 PH11 PF43 |
| 77 | * GPIO_44 PH12 PF44 |
| 78 | * GPIO_45 PH13 PF45 |
| 79 | * GPIO_46 PH14 PF46 |
| 80 | * GPIO_47 PH15 PF47 |
| 81 | */ |
| 82 | |
| 83 | #include <linux/module.h> |
| 84 | #include <linux/err.h> |
| 85 | #include <asm/blackfin.h> |
| 86 | #include <asm/gpio.h> |
Michael Hennerich | c58c214 | 2007-10-04 00:35:05 +0800 | [diff] [blame] | 87 | #include <asm/portmux.h> |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 88 | #include <linux/irq.h> |
| 89 | |
| 90 | #ifdef BF533_FAMILY |
| 91 | static struct gpio_port_t *gpio_bankb[gpio_bank(MAX_BLACKFIN_GPIOS)] = { |
| 92 | (struct gpio_port_t *) FIO_FLAG_D, |
| 93 | }; |
| 94 | #endif |
| 95 | |
| 96 | #ifdef BF537_FAMILY |
| 97 | static struct gpio_port_t *gpio_bankb[gpio_bank(MAX_BLACKFIN_GPIOS)] = { |
| 98 | (struct gpio_port_t *) PORTFIO, |
| 99 | (struct gpio_port_t *) PORTGIO, |
| 100 | (struct gpio_port_t *) PORTHIO, |
| 101 | }; |
| 102 | |
| 103 | static unsigned short *port_fer[gpio_bank(MAX_BLACKFIN_GPIOS)] = { |
| 104 | (unsigned short *) PORTF_FER, |
| 105 | (unsigned short *) PORTG_FER, |
| 106 | (unsigned short *) PORTH_FER, |
| 107 | }; |
| 108 | |
| 109 | #endif |
| 110 | |
| 111 | #ifdef BF561_FAMILY |
| 112 | static struct gpio_port_t *gpio_bankb[gpio_bank(MAX_BLACKFIN_GPIOS)] = { |
| 113 | (struct gpio_port_t *) FIO0_FLAG_D, |
| 114 | (struct gpio_port_t *) FIO1_FLAG_D, |
| 115 | (struct gpio_port_t *) FIO2_FLAG_D, |
| 116 | }; |
| 117 | #endif |
| 118 | |
Michael Hennerich | c58c214 | 2007-10-04 00:35:05 +0800 | [diff] [blame] | 119 | static unsigned short reserved_gpio_map[gpio_bank(MAX_BLACKFIN_GPIOS)]; |
| 120 | static unsigned short reserved_peri_map[gpio_bank(MAX_BLACKFIN_GPIOS + 16)]; |
| 121 | char *str_ident = NULL; |
| 122 | |
| 123 | #define RESOURCE_LABEL_SIZE 16 |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 124 | |
| 125 | #ifdef CONFIG_PM |
| 126 | static unsigned short wakeup_map[gpio_bank(MAX_BLACKFIN_GPIOS)]; |
| 127 | static unsigned char wakeup_flags_map[MAX_BLACKFIN_GPIOS]; |
| 128 | static struct gpio_port_s gpio_bank_saved[gpio_bank(MAX_BLACKFIN_GPIOS)]; |
| 129 | |
| 130 | #ifdef BF533_FAMILY |
| 131 | static unsigned int sic_iwr_irqs[gpio_bank(MAX_BLACKFIN_GPIOS)] = {IRQ_PROG_INTB}; |
| 132 | #endif |
| 133 | |
| 134 | #ifdef BF537_FAMILY |
| 135 | static unsigned int sic_iwr_irqs[gpio_bank(MAX_BLACKFIN_GPIOS)] = {IRQ_PROG_INTB, IRQ_PORTG_INTB, IRQ_MAC_TX}; |
| 136 | #endif |
| 137 | |
| 138 | #ifdef BF561_FAMILY |
| 139 | static unsigned int sic_iwr_irqs[gpio_bank(MAX_BLACKFIN_GPIOS)] = {IRQ_PROG0_INTB, IRQ_PROG1_INTB, IRQ_PROG2_INTB}; |
| 140 | #endif |
| 141 | |
| 142 | #endif /* CONFIG_PM */ |
| 143 | |
| 144 | inline int check_gpio(unsigned short gpio) |
| 145 | { |
Michael Hennerich | e7613aa | 2007-06-11 16:37:57 +0800 | [diff] [blame] | 146 | if (gpio >= MAX_BLACKFIN_GPIOS) |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 147 | return -EINVAL; |
| 148 | return 0; |
| 149 | } |
| 150 | |
Michael Hennerich | c58c214 | 2007-10-04 00:35:05 +0800 | [diff] [blame] | 151 | static void set_label(unsigned short ident, const char *label) |
| 152 | { |
| 153 | |
| 154 | if (label && str_ident) { |
| 155 | strncpy(str_ident + ident * RESOURCE_LABEL_SIZE, label, |
| 156 | RESOURCE_LABEL_SIZE); |
| 157 | str_ident[ident * RESOURCE_LABEL_SIZE + |
| 158 | RESOURCE_LABEL_SIZE - 1] = 0; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | static char *get_label(unsigned short ident) |
| 163 | { |
| 164 | if (!str_ident) |
| 165 | return "UNKNOWN"; |
| 166 | |
| 167 | return (str_ident[ident * RESOURCE_LABEL_SIZE] ? |
| 168 | (str_ident + ident * RESOURCE_LABEL_SIZE) : "UNKNOWN"); |
| 169 | } |
| 170 | |
| 171 | static int cmp_label(unsigned short ident, const char *label) |
| 172 | { |
| 173 | if (label && str_ident) |
| 174 | return strncmp(str_ident + ident * RESOURCE_LABEL_SIZE, |
| 175 | label, strlen(label)); |
| 176 | else |
| 177 | return -EINVAL; |
| 178 | } |
| 179 | |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 180 | #ifdef BF537_FAMILY |
Mike Frysinger | a161bb0 | 2007-05-21 18:09:14 +0800 | [diff] [blame] | 181 | static void port_setup(unsigned short gpio, unsigned short usage) |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 182 | { |
Michael Hennerich | cda6a20 | 2007-10-04 00:36:18 +0800 | [diff] [blame] | 183 | if (!check_gpio(gpio)) { |
| 184 | if (usage == GPIO_USAGE) { |
| 185 | *port_fer[gpio_bank(gpio)] &= ~gpio_bit(gpio); |
| 186 | } else |
| 187 | *port_fer[gpio_bank(gpio)] |= gpio_bit(gpio); |
| 188 | SSYNC(); |
| 189 | } |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 190 | } |
| 191 | #else |
| 192 | # define port_setup(...) do { } while (0) |
| 193 | #endif |
| 194 | |
Michael Hennerich | c58c214 | 2007-10-04 00:35:05 +0800 | [diff] [blame] | 195 | #ifdef BF537_FAMILY |
| 196 | |
| 197 | #define PMUX_LUT_RES 0 |
| 198 | #define PMUX_LUT_OFFSET 1 |
| 199 | #define PMUX_LUT_ENTRIES 41 |
| 200 | #define PMUX_LUT_SIZE 2 |
| 201 | |
| 202 | static unsigned short port_mux_lut[PMUX_LUT_ENTRIES][PMUX_LUT_SIZE] = { |
| 203 | {P_PPI0_D13, 11}, {P_PPI0_D14, 11}, {P_PPI0_D15, 11}, |
| 204 | {P_SPORT1_TFS, 11}, {P_SPORT1_TSCLK, 11}, {P_SPORT1_DTPRI, 11}, |
| 205 | {P_PPI0_D10, 10}, {P_PPI0_D11, 10}, {P_PPI0_D12, 10}, |
| 206 | {P_SPORT1_RSCLK, 10}, {P_SPORT1_RFS, 10}, {P_SPORT1_DRPRI, 10}, |
| 207 | {P_PPI0_D8, 9}, {P_PPI0_D9, 9}, {P_SPORT1_DRSEC, 9}, |
| 208 | {P_SPORT1_DTSEC, 9}, {P_TMR2, 8}, {P_PPI0_FS3, 8}, {P_TMR3, 7}, |
| 209 | {P_SPI0_SSEL4, 7}, {P_TMR4, 6}, {P_SPI0_SSEL5, 6}, {P_TMR5, 5}, |
| 210 | {P_SPI0_SSEL6, 5}, {P_UART1_RX, 4}, {P_UART1_TX, 4}, {P_TMR6, 4}, |
| 211 | {P_TMR7, 4}, {P_UART0_RX, 3}, {P_UART0_TX, 3}, {P_DMAR0, 3}, |
| 212 | {P_DMAR1, 3}, {P_SPORT0_DTSEC, 1}, {P_SPORT0_DRSEC, 1}, |
| 213 | {P_CAN0_RX, 1}, {P_CAN0_TX, 1}, {P_SPI0_SSEL7, 1}, |
| 214 | {P_SPORT0_TFS, 0}, {P_SPORT0_DTPRI, 0}, {P_SPI0_SSEL2, 0}, |
| 215 | {P_SPI0_SSEL3, 0} |
| 216 | }; |
| 217 | |
| 218 | static void portmux_setup(unsigned short per, unsigned short function) |
| 219 | { |
| 220 | u16 y, muxreg, offset; |
| 221 | |
| 222 | for (y = 0; y < PMUX_LUT_ENTRIES; y++) { |
| 223 | if (port_mux_lut[y][PMUX_LUT_RES] == per) { |
| 224 | |
| 225 | /* SET PORTMUX REG */ |
| 226 | |
| 227 | offset = port_mux_lut[y][PMUX_LUT_OFFSET]; |
| 228 | muxreg = bfin_read_PORT_MUX(); |
| 229 | |
| 230 | if (offset != 1) { |
| 231 | muxreg &= ~(1 << offset); |
| 232 | } else { |
| 233 | muxreg &= ~(3 << 1); |
| 234 | } |
| 235 | |
| 236 | muxreg |= (function << offset); |
| 237 | bfin_write_PORT_MUX(muxreg); |
| 238 | } |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | #else |
| 243 | # define portmux_setup(...) do { } while (0) |
| 244 | #endif |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 245 | |
Mike Frysinger | a161bb0 | 2007-05-21 18:09:14 +0800 | [diff] [blame] | 246 | static void default_gpio(unsigned short gpio) |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 247 | { |
Mike Frysinger | 1f83b8f | 2007-07-12 22:58:21 +0800 | [diff] [blame] | 248 | unsigned short bank, bitmask; |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 249 | |
| 250 | bank = gpio_bank(gpio); |
| 251 | bitmask = gpio_bit(gpio); |
| 252 | |
| 253 | gpio_bankb[bank]->maska_clear = bitmask; |
| 254 | gpio_bankb[bank]->maskb_clear = bitmask; |
| 255 | SSYNC(); |
| 256 | gpio_bankb[bank]->inen &= ~bitmask; |
| 257 | gpio_bankb[bank]->dir &= ~bitmask; |
| 258 | gpio_bankb[bank]->polar &= ~bitmask; |
| 259 | gpio_bankb[bank]->both &= ~bitmask; |
| 260 | gpio_bankb[bank]->edge &= ~bitmask; |
| 261 | } |
| 262 | |
Mike Frysinger | a161bb0 | 2007-05-21 18:09:14 +0800 | [diff] [blame] | 263 | static int __init bfin_gpio_init(void) |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 264 | { |
Michael Hennerich | c58c214 | 2007-10-04 00:35:05 +0800 | [diff] [blame] | 265 | |
| 266 | str_ident = kzalloc(RESOURCE_LABEL_SIZE * 256, GFP_KERNEL); |
| 267 | if (!str_ident) |
| 268 | return -ENOMEM; |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 269 | |
| 270 | printk(KERN_INFO "Blackfin GPIO Controller\n"); |
| 271 | |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 272 | return 0; |
Michael Hennerich | c58c214 | 2007-10-04 00:35:05 +0800 | [diff] [blame] | 273 | |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | arch_initcall(bfin_gpio_init); |
| 277 | |
| 278 | |
| 279 | /*********************************************************** |
| 280 | * |
| 281 | * FUNCTIONS: Blackfin General Purpose Ports Access Functions |
| 282 | * |
| 283 | * INPUTS/OUTPUTS: |
| 284 | * gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS |
| 285 | * |
| 286 | * |
| 287 | * DESCRIPTION: These functions abstract direct register access |
| 288 | * to Blackfin processor General Purpose |
| 289 | * Ports Regsiters |
| 290 | * |
| 291 | * CAUTION: These functions do not belong to the GPIO Driver API |
| 292 | ************************************************************* |
| 293 | * MODIFICATION HISTORY : |
| 294 | **************************************************************/ |
| 295 | |
| 296 | /* Set a specific bit */ |
| 297 | |
| 298 | #define SET_GPIO(name) \ |
| 299 | void set_gpio_ ## name(unsigned short gpio, unsigned short arg) \ |
| 300 | { \ |
| 301 | unsigned long flags; \ |
Michael Hennerich | c58c214 | 2007-10-04 00:35:05 +0800 | [diff] [blame] | 302 | BUG_ON(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))); \ |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 303 | local_irq_save(flags); \ |
| 304 | if (arg) \ |
| 305 | gpio_bankb[gpio_bank(gpio)]->name |= gpio_bit(gpio); \ |
| 306 | else \ |
| 307 | gpio_bankb[gpio_bank(gpio)]->name &= ~gpio_bit(gpio); \ |
| 308 | local_irq_restore(flags); \ |
| 309 | } \ |
| 310 | EXPORT_SYMBOL(set_gpio_ ## name); |
| 311 | |
| 312 | SET_GPIO(dir) |
| 313 | SET_GPIO(inen) |
| 314 | SET_GPIO(polar) |
| 315 | SET_GPIO(edge) |
| 316 | SET_GPIO(both) |
| 317 | |
| 318 | |
| 319 | #define SET_GPIO_SC(name) \ |
| 320 | void set_gpio_ ## name(unsigned short gpio, unsigned short arg) \ |
| 321 | { \ |
Michael Hennerich | c58c214 | 2007-10-04 00:35:05 +0800 | [diff] [blame] | 322 | BUG_ON(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))); \ |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 323 | if (arg) \ |
| 324 | gpio_bankb[gpio_bank(gpio)]->name ## _set = gpio_bit(gpio); \ |
| 325 | else \ |
| 326 | gpio_bankb[gpio_bank(gpio)]->name ## _clear = gpio_bit(gpio); \ |
| 327 | } \ |
| 328 | EXPORT_SYMBOL(set_gpio_ ## name); |
| 329 | |
| 330 | SET_GPIO_SC(maska) |
| 331 | SET_GPIO_SC(maskb) |
| 332 | |
Mike Frysinger | 1aafd90 | 2007-07-25 11:19:14 +0800 | [diff] [blame] | 333 | #if ANOMALY_05000311 |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 334 | void set_gpio_data(unsigned short gpio, unsigned short arg) |
| 335 | { |
| 336 | unsigned long flags; |
Michael Hennerich | c58c214 | 2007-10-04 00:35:05 +0800 | [diff] [blame] | 337 | BUG_ON(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 338 | local_irq_save(flags); |
| 339 | if (arg) |
| 340 | gpio_bankb[gpio_bank(gpio)]->data_set = gpio_bit(gpio); |
| 341 | else |
| 342 | gpio_bankb[gpio_bank(gpio)]->data_clear = gpio_bit(gpio); |
| 343 | bfin_read_CHIPID(); |
| 344 | local_irq_restore(flags); |
| 345 | } |
| 346 | EXPORT_SYMBOL(set_gpio_data); |
| 347 | #else |
| 348 | SET_GPIO_SC(data) |
| 349 | #endif |
| 350 | |
| 351 | |
Mike Frysinger | 1aafd90 | 2007-07-25 11:19:14 +0800 | [diff] [blame] | 352 | #if ANOMALY_05000311 |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 353 | void set_gpio_toggle(unsigned short gpio) |
| 354 | { |
| 355 | unsigned long flags; |
Michael Hennerich | c58c214 | 2007-10-04 00:35:05 +0800 | [diff] [blame] | 356 | BUG_ON(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 357 | local_irq_save(flags); |
| 358 | gpio_bankb[gpio_bank(gpio)]->toggle = gpio_bit(gpio); |
| 359 | bfin_read_CHIPID(); |
| 360 | local_irq_restore(flags); |
| 361 | } |
| 362 | #else |
| 363 | void set_gpio_toggle(unsigned short gpio) |
| 364 | { |
Michael Hennerich | c58c214 | 2007-10-04 00:35:05 +0800 | [diff] [blame] | 365 | BUG_ON(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 366 | gpio_bankb[gpio_bank(gpio)]->toggle = gpio_bit(gpio); |
| 367 | } |
| 368 | #endif |
| 369 | EXPORT_SYMBOL(set_gpio_toggle); |
| 370 | |
| 371 | |
| 372 | /*Set current PORT date (16-bit word)*/ |
| 373 | |
| 374 | #define SET_GPIO_P(name) \ |
| 375 | void set_gpiop_ ## name(unsigned short gpio, unsigned short arg) \ |
| 376 | { \ |
| 377 | gpio_bankb[gpio_bank(gpio)]->name = arg; \ |
| 378 | } \ |
| 379 | EXPORT_SYMBOL(set_gpiop_ ## name); |
| 380 | |
| 381 | SET_GPIO_P(dir) |
| 382 | SET_GPIO_P(inen) |
| 383 | SET_GPIO_P(polar) |
| 384 | SET_GPIO_P(edge) |
| 385 | SET_GPIO_P(both) |
| 386 | SET_GPIO_P(maska) |
| 387 | SET_GPIO_P(maskb) |
| 388 | |
| 389 | |
Mike Frysinger | 1aafd90 | 2007-07-25 11:19:14 +0800 | [diff] [blame] | 390 | #if ANOMALY_05000311 |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 391 | void set_gpiop_data(unsigned short gpio, unsigned short arg) |
| 392 | { |
| 393 | unsigned long flags; |
| 394 | local_irq_save(flags); |
| 395 | gpio_bankb[gpio_bank(gpio)]->data = arg; |
| 396 | bfin_read_CHIPID(); |
| 397 | local_irq_restore(flags); |
| 398 | } |
| 399 | EXPORT_SYMBOL(set_gpiop_data); |
| 400 | #else |
| 401 | SET_GPIO_P(data) |
| 402 | #endif |
| 403 | |
| 404 | |
| 405 | |
| 406 | /* Get a specific bit */ |
| 407 | |
| 408 | #define GET_GPIO(name) \ |
| 409 | unsigned short get_gpio_ ## name(unsigned short gpio) \ |
| 410 | { \ |
| 411 | return (0x01 & (gpio_bankb[gpio_bank(gpio)]->name >> gpio_sub_n(gpio))); \ |
| 412 | } \ |
| 413 | EXPORT_SYMBOL(get_gpio_ ## name); |
| 414 | |
| 415 | GET_GPIO(dir) |
| 416 | GET_GPIO(inen) |
| 417 | GET_GPIO(polar) |
| 418 | GET_GPIO(edge) |
| 419 | GET_GPIO(both) |
| 420 | GET_GPIO(maska) |
| 421 | GET_GPIO(maskb) |
| 422 | |
| 423 | |
Mike Frysinger | 1aafd90 | 2007-07-25 11:19:14 +0800 | [diff] [blame] | 424 | #if ANOMALY_05000311 |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 425 | unsigned short get_gpio_data(unsigned short gpio) |
| 426 | { |
| 427 | unsigned long flags; |
| 428 | unsigned short ret; |
Michael Hennerich | c58c214 | 2007-10-04 00:35:05 +0800 | [diff] [blame] | 429 | BUG_ON(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 430 | local_irq_save(flags); |
| 431 | ret = 0x01 & (gpio_bankb[gpio_bank(gpio)]->data >> gpio_sub_n(gpio)); |
| 432 | bfin_read_CHIPID(); |
| 433 | local_irq_restore(flags); |
| 434 | return ret; |
| 435 | } |
| 436 | EXPORT_SYMBOL(get_gpio_data); |
| 437 | #else |
| 438 | GET_GPIO(data) |
| 439 | #endif |
| 440 | |
| 441 | /*Get current PORT date (16-bit word)*/ |
| 442 | |
| 443 | #define GET_GPIO_P(name) \ |
| 444 | unsigned short get_gpiop_ ## name(unsigned short gpio) \ |
| 445 | { \ |
| 446 | return (gpio_bankb[gpio_bank(gpio)]->name);\ |
| 447 | } \ |
| 448 | EXPORT_SYMBOL(get_gpiop_ ## name); |
| 449 | |
| 450 | GET_GPIO_P(dir) |
| 451 | GET_GPIO_P(inen) |
| 452 | GET_GPIO_P(polar) |
| 453 | GET_GPIO_P(edge) |
| 454 | GET_GPIO_P(both) |
| 455 | GET_GPIO_P(maska) |
| 456 | GET_GPIO_P(maskb) |
| 457 | |
Mike Frysinger | 1aafd90 | 2007-07-25 11:19:14 +0800 | [diff] [blame] | 458 | #if ANOMALY_05000311 |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 459 | unsigned short get_gpiop_data(unsigned short gpio) |
| 460 | { |
| 461 | unsigned long flags; |
| 462 | unsigned short ret; |
| 463 | local_irq_save(flags); |
| 464 | ret = gpio_bankb[gpio_bank(gpio)]->data; |
| 465 | bfin_read_CHIPID(); |
| 466 | local_irq_restore(flags); |
| 467 | return ret; |
| 468 | } |
| 469 | EXPORT_SYMBOL(get_gpiop_data); |
| 470 | #else |
| 471 | GET_GPIO_P(data) |
| 472 | #endif |
| 473 | |
| 474 | #ifdef CONFIG_PM |
| 475 | /*********************************************************** |
| 476 | * |
| 477 | * FUNCTIONS: Blackfin PM Setup API |
| 478 | * |
| 479 | * INPUTS/OUTPUTS: |
| 480 | * gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS |
| 481 | * type - |
| 482 | * PM_WAKE_RISING |
| 483 | * PM_WAKE_FALLING |
| 484 | * PM_WAKE_HIGH |
| 485 | * PM_WAKE_LOW |
| 486 | * PM_WAKE_BOTH_EDGES |
| 487 | * |
| 488 | * DESCRIPTION: Blackfin PM Driver API |
| 489 | * |
| 490 | * CAUTION: |
| 491 | ************************************************************* |
| 492 | * MODIFICATION HISTORY : |
| 493 | **************************************************************/ |
| 494 | int gpio_pm_wakeup_request(unsigned short gpio, unsigned char type) |
| 495 | { |
| 496 | unsigned long flags; |
| 497 | |
| 498 | if ((check_gpio(gpio) < 0) || !type) |
| 499 | return -EINVAL; |
| 500 | |
| 501 | local_irq_save(flags); |
| 502 | |
| 503 | wakeup_map[gpio_bank(gpio)] |= gpio_bit(gpio); |
| 504 | wakeup_flags_map[gpio] = type; |
| 505 | local_irq_restore(flags); |
| 506 | |
| 507 | return 0; |
| 508 | } |
| 509 | EXPORT_SYMBOL(gpio_pm_wakeup_request); |
| 510 | |
| 511 | void gpio_pm_wakeup_free(unsigned short gpio) |
| 512 | { |
| 513 | unsigned long flags; |
| 514 | |
| 515 | if (check_gpio(gpio) < 0) |
| 516 | return; |
| 517 | |
| 518 | local_irq_save(flags); |
| 519 | |
| 520 | wakeup_map[gpio_bank(gpio)] &= ~gpio_bit(gpio); |
| 521 | |
| 522 | local_irq_restore(flags); |
| 523 | } |
| 524 | EXPORT_SYMBOL(gpio_pm_wakeup_free); |
| 525 | |
| 526 | static int bfin_gpio_wakeup_type(unsigned short gpio, unsigned char type) |
| 527 | { |
| 528 | port_setup(gpio, GPIO_USAGE); |
| 529 | set_gpio_dir(gpio, 0); |
| 530 | set_gpio_inen(gpio, 1); |
| 531 | |
| 532 | if (type & (PM_WAKE_RISING | PM_WAKE_FALLING)) |
| 533 | set_gpio_edge(gpio, 1); |
| 534 | else |
| 535 | set_gpio_edge(gpio, 0); |
| 536 | |
| 537 | if ((type & (PM_WAKE_BOTH_EDGES)) == (PM_WAKE_BOTH_EDGES)) |
| 538 | set_gpio_both(gpio, 1); |
| 539 | else |
| 540 | set_gpio_both(gpio, 0); |
| 541 | |
| 542 | if ((type & (PM_WAKE_FALLING | PM_WAKE_LOW))) |
| 543 | set_gpio_polar(gpio, 1); |
| 544 | else |
| 545 | set_gpio_polar(gpio, 0); |
| 546 | |
| 547 | SSYNC(); |
| 548 | |
| 549 | return 0; |
| 550 | } |
| 551 | |
| 552 | u32 gpio_pm_setup(void) |
| 553 | { |
| 554 | u32 sic_iwr = 0; |
| 555 | u16 bank, mask, i, gpio; |
| 556 | |
Mike Frysinger | 1f83b8f | 2007-07-12 22:58:21 +0800 | [diff] [blame] | 557 | for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) { |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 558 | mask = wakeup_map[gpio_bank(i)]; |
| 559 | bank = gpio_bank(i); |
| 560 | |
| 561 | gpio_bank_saved[bank].maskb = gpio_bankb[bank]->maskb; |
| 562 | gpio_bankb[bank]->maskb = 0; |
| 563 | |
| 564 | if (mask) { |
| 565 | #ifdef BF537_FAMILY |
| 566 | gpio_bank_saved[bank].fer = *port_fer[bank]; |
| 567 | #endif |
| 568 | gpio_bank_saved[bank].inen = gpio_bankb[bank]->inen; |
| 569 | gpio_bank_saved[bank].polar = gpio_bankb[bank]->polar; |
| 570 | gpio_bank_saved[bank].dir = gpio_bankb[bank]->dir; |
| 571 | gpio_bank_saved[bank].edge = gpio_bankb[bank]->edge; |
| 572 | gpio_bank_saved[bank].both = gpio_bankb[bank]->both; |
Michael Hennerich | c58c214 | 2007-10-04 00:35:05 +0800 | [diff] [blame] | 573 | gpio_bank_saved[bank].reserved = |
| 574 | reserved_gpio_map[bank]; |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 575 | |
| 576 | gpio = i; |
| 577 | |
| 578 | while (mask) { |
| 579 | if (mask & 1) { |
Michael Hennerich | c58c214 | 2007-10-04 00:35:05 +0800 | [diff] [blame] | 580 | reserved_gpio_map[gpio_bank(gpio)] |= |
Michael Hennerich | 581d62a | 2007-06-14 13:30:23 +0800 | [diff] [blame] | 581 | gpio_bit(gpio); |
| 582 | bfin_gpio_wakeup_type(gpio, |
| 583 | wakeup_flags_map[gpio]); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 584 | set_gpio_data(gpio, 0); /*Clear*/ |
| 585 | } |
| 586 | gpio++; |
| 587 | mask >>= 1; |
| 588 | } |
| 589 | |
Michael Hennerich | 581d62a | 2007-06-14 13:30:23 +0800 | [diff] [blame] | 590 | sic_iwr |= 1 << |
| 591 | (sic_iwr_irqs[bank] - (IRQ_CORETMR + 1)); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 592 | gpio_bankb[bank]->maskb_set = wakeup_map[gpio_bank(i)]; |
| 593 | } |
| 594 | } |
| 595 | |
| 596 | if (sic_iwr) |
| 597 | return sic_iwr; |
| 598 | else |
| 599 | return IWR_ENABLE_ALL; |
| 600 | } |
| 601 | |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 602 | void gpio_pm_restore(void) |
| 603 | { |
| 604 | u16 bank, mask, i; |
| 605 | |
Mike Frysinger | 1f83b8f | 2007-07-12 22:58:21 +0800 | [diff] [blame] | 606 | for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) { |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 607 | mask = wakeup_map[gpio_bank(i)]; |
| 608 | bank = gpio_bank(i); |
| 609 | |
| 610 | if (mask) { |
| 611 | #ifdef BF537_FAMILY |
| 612 | *port_fer[bank] = gpio_bank_saved[bank].fer; |
| 613 | #endif |
| 614 | gpio_bankb[bank]->inen = gpio_bank_saved[bank].inen; |
| 615 | gpio_bankb[bank]->dir = gpio_bank_saved[bank].dir; |
| 616 | gpio_bankb[bank]->polar = gpio_bank_saved[bank].polar; |
| 617 | gpio_bankb[bank]->edge = gpio_bank_saved[bank].edge; |
| 618 | gpio_bankb[bank]->both = gpio_bank_saved[bank].both; |
Michael Hennerich | 581d62a | 2007-06-14 13:30:23 +0800 | [diff] [blame] | 619 | |
Michael Hennerich | c58c214 | 2007-10-04 00:35:05 +0800 | [diff] [blame] | 620 | reserved_gpio_map[bank] = |
| 621 | gpio_bank_saved[bank].reserved; |
Michael Hennerich | 581d62a | 2007-06-14 13:30:23 +0800 | [diff] [blame] | 622 | |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 623 | } |
| 624 | |
| 625 | gpio_bankb[bank]->maskb = gpio_bank_saved[bank].maskb; |
| 626 | } |
| 627 | } |
| 628 | |
| 629 | #endif |
| 630 | |
Michael Hennerich | c58c214 | 2007-10-04 00:35:05 +0800 | [diff] [blame] | 631 | |
| 632 | |
| 633 | |
| 634 | int peripheral_request(unsigned short per, const char *label) |
| 635 | { |
| 636 | unsigned long flags; |
| 637 | unsigned short ident = P_IDENT(per); |
| 638 | |
| 639 | /* |
| 640 | * Don't cares are pins with only one dedicated function |
| 641 | */ |
| 642 | |
| 643 | if (per & P_DONTCARE) |
| 644 | return 0; |
| 645 | |
| 646 | if (!(per & P_DEFINED)) |
| 647 | return -ENODEV; |
| 648 | |
Michael Hennerich | c58c214 | 2007-10-04 00:35:05 +0800 | [diff] [blame] | 649 | local_irq_save(flags); |
| 650 | |
Michael Hennerich | cda6a20 | 2007-10-04 00:36:18 +0800 | [diff] [blame] | 651 | if (!check_gpio(ident)) { |
| 652 | |
Michael Hennerich | c58c214 | 2007-10-04 00:35:05 +0800 | [diff] [blame] | 653 | if (unlikely(reserved_gpio_map[gpio_bank(ident)] & gpio_bit(ident))) { |
| 654 | printk(KERN_ERR |
| 655 | "%s: Peripheral %d is already reserved as GPIO by %s !\n", |
| 656 | __FUNCTION__, ident, get_label(ident)); |
| 657 | dump_stack(); |
| 658 | local_irq_restore(flags); |
| 659 | return -EBUSY; |
| 660 | } |
| 661 | |
Michael Hennerich | cda6a20 | 2007-10-04 00:36:18 +0800 | [diff] [blame] | 662 | } |
| 663 | |
Michael Hennerich | c58c214 | 2007-10-04 00:35:05 +0800 | [diff] [blame] | 664 | if (unlikely(reserved_peri_map[gpio_bank(ident)] & gpio_bit(ident))) { |
| 665 | |
| 666 | /* |
| 667 | * Pin functions like AMC address strobes my |
| 668 | * be requested and used by several drivers |
| 669 | */ |
| 670 | |
| 671 | if (!(per & P_MAYSHARE)) { |
| 672 | |
| 673 | /* |
| 674 | * Allow that the identical pin function can |
| 675 | * be requested from the same driver twice |
| 676 | */ |
| 677 | |
| 678 | if (cmp_label(ident, label) == 0) |
| 679 | goto anyway; |
| 680 | |
| 681 | printk(KERN_ERR |
| 682 | "%s: Peripheral %d function %d is already" |
| 683 | "reserved by %s !\n", |
| 684 | __FUNCTION__, ident, P_FUNCT2MUX(per), |
| 685 | get_label(ident)); |
| 686 | dump_stack(); |
| 687 | local_irq_restore(flags); |
| 688 | return -EBUSY; |
| 689 | } |
| 690 | |
| 691 | } |
| 692 | |
| 693 | anyway: |
| 694 | |
| 695 | |
| 696 | portmux_setup(per, P_FUNCT2MUX(per)); |
| 697 | |
| 698 | port_setup(ident, PERIPHERAL_USAGE); |
| 699 | |
| 700 | reserved_peri_map[gpio_bank(ident)] |= gpio_bit(ident); |
| 701 | local_irq_restore(flags); |
| 702 | set_label(ident, label); |
| 703 | |
| 704 | return 0; |
| 705 | } |
| 706 | EXPORT_SYMBOL(peripheral_request); |
| 707 | |
| 708 | int peripheral_request_list(unsigned short per[], const char *label) |
| 709 | { |
| 710 | u16 cnt; |
| 711 | int ret; |
| 712 | |
| 713 | for (cnt = 0; per[cnt] != 0; cnt++) { |
Michael Hennerich | 314c98d | 2007-07-24 18:03:45 +0800 | [diff] [blame^] | 714 | |
Michael Hennerich | c58c214 | 2007-10-04 00:35:05 +0800 | [diff] [blame] | 715 | ret = peripheral_request(per[cnt], label); |
Michael Hennerich | 314c98d | 2007-07-24 18:03:45 +0800 | [diff] [blame^] | 716 | |
| 717 | if (ret < 0) { |
| 718 | for ( ; cnt > 0; cnt--) { |
| 719 | peripheral_free(per[cnt - 1]); |
| 720 | } |
| 721 | return ret; |
| 722 | } |
Michael Hennerich | c58c214 | 2007-10-04 00:35:05 +0800 | [diff] [blame] | 723 | } |
| 724 | |
| 725 | return 0; |
| 726 | } |
| 727 | EXPORT_SYMBOL(peripheral_request_list); |
| 728 | |
| 729 | void peripheral_free(unsigned short per) |
| 730 | { |
| 731 | unsigned long flags; |
| 732 | unsigned short ident = P_IDENT(per); |
| 733 | |
| 734 | if (per & P_DONTCARE) |
| 735 | return; |
| 736 | |
| 737 | if (!(per & P_DEFINED)) |
| 738 | return; |
| 739 | |
| 740 | if (check_gpio(ident) < 0) |
| 741 | return; |
| 742 | |
| 743 | local_irq_save(flags); |
| 744 | |
| 745 | if (unlikely(!(reserved_peri_map[gpio_bank(ident)] |
| 746 | & gpio_bit(ident)))) { |
| 747 | local_irq_restore(flags); |
| 748 | return; |
| 749 | } |
| 750 | |
| 751 | if (!(per & P_MAYSHARE)) { |
| 752 | port_setup(ident, GPIO_USAGE); |
| 753 | } |
| 754 | |
| 755 | reserved_peri_map[gpio_bank(ident)] &= ~gpio_bit(ident); |
| 756 | |
| 757 | local_irq_restore(flags); |
| 758 | } |
| 759 | EXPORT_SYMBOL(peripheral_free); |
| 760 | |
| 761 | void peripheral_free_list(unsigned short per[]) |
| 762 | { |
| 763 | u16 cnt; |
| 764 | |
| 765 | for (cnt = 0; per[cnt] != 0; cnt++) { |
| 766 | peripheral_free(per[cnt]); |
| 767 | } |
| 768 | |
| 769 | } |
| 770 | EXPORT_SYMBOL(peripheral_free_list); |
| 771 | |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 772 | /*********************************************************** |
| 773 | * |
| 774 | * FUNCTIONS: Blackfin GPIO Driver |
| 775 | * |
| 776 | * INPUTS/OUTPUTS: |
| 777 | * gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS |
| 778 | * |
| 779 | * |
| 780 | * DESCRIPTION: Blackfin GPIO Driver API |
| 781 | * |
| 782 | * CAUTION: |
| 783 | ************************************************************* |
| 784 | * MODIFICATION HISTORY : |
| 785 | **************************************************************/ |
| 786 | |
| 787 | int gpio_request(unsigned short gpio, const char *label) |
| 788 | { |
| 789 | unsigned long flags; |
| 790 | |
| 791 | if (check_gpio(gpio) < 0) |
| 792 | return -EINVAL; |
| 793 | |
| 794 | local_irq_save(flags); |
| 795 | |
Michael Hennerich | c58c214 | 2007-10-04 00:35:05 +0800 | [diff] [blame] | 796 | if (unlikely(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))) { |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 797 | printk(KERN_ERR "bfin-gpio: GPIO %d is already reserved!\n", gpio); |
| 798 | dump_stack(); |
| 799 | local_irq_restore(flags); |
| 800 | return -EBUSY; |
| 801 | } |
Michael Hennerich | c58c214 | 2007-10-04 00:35:05 +0800 | [diff] [blame] | 802 | reserved_gpio_map[gpio_bank(gpio)] |= gpio_bit(gpio); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 803 | |
| 804 | local_irq_restore(flags); |
| 805 | |
| 806 | port_setup(gpio, GPIO_USAGE); |
| 807 | |
| 808 | return 0; |
| 809 | } |
| 810 | EXPORT_SYMBOL(gpio_request); |
| 811 | |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 812 | void gpio_free(unsigned short gpio) |
| 813 | { |
| 814 | unsigned long flags; |
| 815 | |
| 816 | if (check_gpio(gpio) < 0) |
| 817 | return; |
| 818 | |
| 819 | local_irq_save(flags); |
| 820 | |
Michael Hennerich | c58c214 | 2007-10-04 00:35:05 +0800 | [diff] [blame] | 821 | if (unlikely(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio)))) { |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 822 | printk(KERN_ERR "bfin-gpio: GPIO %d wasn't reserved!\n", gpio); |
| 823 | dump_stack(); |
| 824 | local_irq_restore(flags); |
| 825 | return; |
| 826 | } |
| 827 | |
| 828 | default_gpio(gpio); |
| 829 | |
Michael Hennerich | c58c214 | 2007-10-04 00:35:05 +0800 | [diff] [blame] | 830 | reserved_gpio_map[gpio_bank(gpio)] &= ~gpio_bit(gpio); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 831 | |
| 832 | local_irq_restore(flags); |
| 833 | } |
| 834 | EXPORT_SYMBOL(gpio_free); |
| 835 | |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 836 | void gpio_direction_input(unsigned short gpio) |
| 837 | { |
| 838 | unsigned long flags; |
| 839 | |
Michael Hennerich | c58c214 | 2007-10-04 00:35:05 +0800 | [diff] [blame] | 840 | BUG_ON(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 841 | |
| 842 | local_irq_save(flags); |
| 843 | gpio_bankb[gpio_bank(gpio)]->dir &= ~gpio_bit(gpio); |
| 844 | gpio_bankb[gpio_bank(gpio)]->inen |= gpio_bit(gpio); |
| 845 | local_irq_restore(flags); |
| 846 | } |
| 847 | EXPORT_SYMBOL(gpio_direction_input); |
| 848 | |
| 849 | void gpio_direction_output(unsigned short gpio) |
| 850 | { |
| 851 | unsigned long flags; |
| 852 | |
Michael Hennerich | c58c214 | 2007-10-04 00:35:05 +0800 | [diff] [blame] | 853 | BUG_ON(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 854 | |
| 855 | local_irq_save(flags); |
| 856 | gpio_bankb[gpio_bank(gpio)]->inen &= ~gpio_bit(gpio); |
| 857 | gpio_bankb[gpio_bank(gpio)]->dir |= gpio_bit(gpio); |
| 858 | local_irq_restore(flags); |
| 859 | } |
| 860 | EXPORT_SYMBOL(gpio_direction_output); |