linux/arch/blackfin/kernel/bfin_gpio.c
<<
>>
Prefs
   1/*
   2 * GPIO Abstraction Layer
   3 *
   4 * Copyright 2006-2010 Analog Devices Inc.
   5 *
   6 * Licensed under the GPL-2 or later
   7 */
   8
   9#include <linux/delay.h>
  10#include <linux/module.h>
  11#include <linux/err.h>
  12#include <linux/proc_fs.h>
  13#include <linux/seq_file.h>
  14#include <linux/gpio/driver.h>
  15/* FIXME: consumer API required for gpio_set_value() etc, get rid of this */
  16#include <linux/gpio.h>
  17#include <linux/irq.h>
  18#include <asm/gpio.h>
  19#include <asm/irq_handler.h>
  20#include <asm/portmux.h>
  21
  22#if ANOMALY_05000311 || ANOMALY_05000323
  23enum {
  24        AWA_data = SYSCR,
  25        AWA_data_clear = SYSCR,
  26        AWA_data_set = SYSCR,
  27        AWA_toggle = SYSCR,
  28        AWA_maska = BFIN_UART_SCR,
  29        AWA_maska_clear = BFIN_UART_SCR,
  30        AWA_maska_set = BFIN_UART_SCR,
  31        AWA_maska_toggle = BFIN_UART_SCR,
  32        AWA_maskb = BFIN_UART_GCTL,
  33        AWA_maskb_clear = BFIN_UART_GCTL,
  34        AWA_maskb_set = BFIN_UART_GCTL,
  35        AWA_maskb_toggle = BFIN_UART_GCTL,
  36        AWA_dir = SPORT1_STAT,
  37        AWA_polar = SPORT1_STAT,
  38        AWA_edge = SPORT1_STAT,
  39        AWA_both = SPORT1_STAT,
  40#if ANOMALY_05000311
  41        AWA_inen = TIMER_ENABLE,
  42#elif ANOMALY_05000323
  43        AWA_inen = DMA1_1_CONFIG,
  44#endif
  45};
  46        /* Anomaly Workaround */
  47#define AWA_DUMMY_READ(name) bfin_read16(AWA_ ## name)
  48#else
  49#define AWA_DUMMY_READ(...)  do { } while (0)
  50#endif
  51
  52static struct gpio_port_t * const gpio_array[] = {
  53#if defined(BF533_FAMILY) || defined(BF538_FAMILY)
  54        (struct gpio_port_t *) FIO_FLAG_D,
  55#elif defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
  56        (struct gpio_port_t *) PORTFIO,
  57        (struct gpio_port_t *) PORTGIO,
  58        (struct gpio_port_t *) PORTHIO,
  59#elif defined(BF561_FAMILY)
  60        (struct gpio_port_t *) FIO0_FLAG_D,
  61        (struct gpio_port_t *) FIO1_FLAG_D,
  62        (struct gpio_port_t *) FIO2_FLAG_D,
  63#else
  64# error no gpio arrays defined
  65#endif
  66};
  67
  68#if defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
  69static unsigned short * const port_fer[] = {
  70        (unsigned short *) PORTF_FER,
  71        (unsigned short *) PORTG_FER,
  72        (unsigned short *) PORTH_FER,
  73};
  74
  75# if !defined(BF537_FAMILY)
  76static unsigned short * const port_mux[] = {
  77        (unsigned short *) PORTF_MUX,
  78        (unsigned short *) PORTG_MUX,
  79        (unsigned short *) PORTH_MUX,
  80};
  81
  82static const
  83u8 pmux_offset[][16] = {
  84#  if defined(CONFIG_BF52x)
  85        { 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 4, 6, 8, 8, 10, 10 }, /* PORTF */
  86        { 0, 0, 0, 0, 0, 2, 2, 4, 4, 6, 8, 10, 10, 10, 12, 12 }, /* PORTG */
  87        { 0, 0, 0, 0, 0, 0, 0, 0, 2, 4, 4, 4, 4, 4, 4, 4 }, /* PORTH */
  88#  elif defined(CONFIG_BF51x)
  89        { 0, 2, 2, 2, 2, 2, 2, 4, 6, 6, 6, 8, 8, 8, 8, 10 }, /* PORTF */
  90        { 0, 0, 0, 2, 4, 6, 6, 6, 8, 10, 10, 12, 14, 14, 14, 14 }, /* PORTG */
  91        { 0, 0, 0, 0, 2, 2, 4, 6, 10, 10, 10, 10, 10, 10, 10, 10 }, /* PORTH */
  92#  endif
  93};
  94# endif
  95
  96#elif defined(BF538_FAMILY)
  97static unsigned short * const port_fer[] = {
  98        (unsigned short *) PORTCIO_FER,
  99        (unsigned short *) PORTDIO_FER,
 100        (unsigned short *) PORTEIO_FER,
 101};
 102#endif
 103
 104#define RESOURCE_LABEL_SIZE     16
 105
 106static struct str_ident {
 107        char name[RESOURCE_LABEL_SIZE];
 108} str_ident[MAX_RESOURCES];
 109
 110#if defined(CONFIG_PM)
 111static struct gpio_port_s gpio_bank_saved[GPIO_BANK_NUM];
 112# ifdef BF538_FAMILY
 113static unsigned short port_fer_saved[3];
 114# endif
 115#endif
 116
 117static void gpio_error(unsigned gpio)
 118{
 119        printk(KERN_ERR "bfin-gpio: GPIO %d wasn't requested!\n", gpio);
 120}
 121
 122static void set_label(unsigned short ident, const char *label)
 123{
 124        if (label) {
 125                strncpy(str_ident[ident].name, label,
 126                         RESOURCE_LABEL_SIZE);
 127                str_ident[ident].name[RESOURCE_LABEL_SIZE - 1] = 0;
 128        }
 129}
 130
 131static char *get_label(unsigned short ident)
 132{
 133        return (*str_ident[ident].name ? str_ident[ident].name : "UNKNOWN");
 134}
 135
 136static int cmp_label(unsigned short ident, const char *label)
 137{
 138        if (label == NULL) {
 139                dump_stack();
 140                printk(KERN_ERR "Please provide none-null label\n");
 141        }
 142
 143        if (label)
 144                return strcmp(str_ident[ident].name, label);
 145        else
 146                return -EINVAL;
 147}
 148
 149#define map_entry(m, i)      reserved_##m##_map[gpio_bank(i)]
 150#define is_reserved(m, i, e) (map_entry(m, i) & gpio_bit(i))
 151#define reserve(m, i)        (map_entry(m, i) |= gpio_bit(i))
 152#define unreserve(m, i)      (map_entry(m, i) &= ~gpio_bit(i))
 153#define DECLARE_RESERVED_MAP(m, c) static unsigned short reserved_##m##_map[c]
 154
 155DECLARE_RESERVED_MAP(gpio, GPIO_BANK_NUM);
 156DECLARE_RESERVED_MAP(peri, DIV_ROUND_UP(MAX_RESOURCES, GPIO_BANKSIZE));
 157DECLARE_RESERVED_MAP(gpio_irq, GPIO_BANK_NUM);
 158
 159inline int check_gpio(unsigned gpio)
 160{
 161        if (gpio >= MAX_BLACKFIN_GPIOS)
 162                return -EINVAL;
 163        return 0;
 164}
 165
 166static void port_setup(unsigned gpio, unsigned short usage)
 167{
 168#if defined(BF538_FAMILY)
 169        /*
 170         * BF538/9 Port C,D and E are special.
 171         * Inverted PORT_FER polarity on CDE and no PORF_FER on F
 172         * Regular PORT F GPIOs are handled here, CDE are exclusively
 173         * managed by GPIOLIB
 174         */
 175
 176        if (gpio < MAX_BLACKFIN_GPIOS || gpio >= MAX_RESOURCES)
 177                return;
 178
 179        gpio -= MAX_BLACKFIN_GPIOS;
 180
 181        if (usage == GPIO_USAGE)
 182                *port_fer[gpio_bank(gpio)] |= gpio_bit(gpio);
 183        else
 184                *port_fer[gpio_bank(gpio)] &= ~gpio_bit(gpio);
 185        SSYNC();
 186        return;
 187#endif
 188
 189        if (check_gpio(gpio))
 190                return;
 191
 192#if defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
 193        if (usage == GPIO_USAGE)
 194                *port_fer[gpio_bank(gpio)] &= ~gpio_bit(gpio);
 195        else
 196                *port_fer[gpio_bank(gpio)] |= gpio_bit(gpio);
 197        SSYNC();
 198#endif
 199}
 200
 201#ifdef BF537_FAMILY
 202static const s8 port_mux[] = {
 203        [GPIO_PF0] = 3,
 204        [GPIO_PF1] = 3,
 205        [GPIO_PF2] = 4,
 206        [GPIO_PF3] = 4,
 207        [GPIO_PF4] = 5,
 208        [GPIO_PF5] = 6,
 209        [GPIO_PF6] = 7,
 210        [GPIO_PF7] = 8,
 211        [GPIO_PF8 ... GPIO_PF15] = -1,
 212        [GPIO_PG0 ... GPIO_PG7] = -1,
 213        [GPIO_PG8] = 9,
 214        [GPIO_PG9] = 9,
 215        [GPIO_PG10] = 10,
 216        [GPIO_PG11] = 10,
 217        [GPIO_PG12] = 10,
 218        [GPIO_PG13] = 11,
 219        [GPIO_PG14] = 11,
 220        [GPIO_PG15] = 11,
 221        [GPIO_PH0 ... GPIO_PH15] = -1,
 222        [PORT_PJ0 ... PORT_PJ3] = -1,
 223        [PORT_PJ4] = 1,
 224        [PORT_PJ5] = 1,
 225        [PORT_PJ6 ... PORT_PJ9] = -1,
 226        [PORT_PJ10] = 0,
 227        [PORT_PJ11] = 0,
 228};
 229
 230static int portmux_group_check(unsigned short per)
 231{
 232        u16 ident = P_IDENT(per);
 233        u16 function = P_FUNCT2MUX(per);
 234        s8 offset = port_mux[ident];
 235        u16 m, pmux, pfunc, mask;
 236
 237        if (offset < 0)
 238                return 0;
 239
 240        pmux = bfin_read_PORT_MUX();
 241        for (m = 0; m < ARRAY_SIZE(port_mux); ++m) {
 242                if (m == ident)
 243                        continue;
 244                if (port_mux[m] != offset)
 245                        continue;
 246                if (!is_reserved(peri, m, 1))
 247                        continue;
 248
 249                if (offset == 1)
 250                        mask = 3;
 251                else
 252                        mask = 1;
 253
 254                pfunc = (pmux >> offset) & mask;
 255                if (pfunc != (function & mask)) {
 256                        pr_err("pin group conflict! request pin %d func %d conflict with pin %d func %d\n",
 257                                ident, function, m, pfunc);
 258                        return -EINVAL;
 259                }
 260        }
 261
 262        return 0;
 263}
 264
 265static void portmux_setup(unsigned short per)
 266{
 267        u16 ident = P_IDENT(per);
 268        u16 function = P_FUNCT2MUX(per);
 269        s8 offset = port_mux[ident];
 270        u16 pmux, mask;
 271
 272        if (offset == -1)
 273                return;
 274
 275        pmux = bfin_read_PORT_MUX();
 276        if (offset == 1)
 277                mask = 3;
 278        else
 279                mask = 1;
 280
 281        pmux &= ~(mask << offset);
 282        pmux |= ((function & mask) << offset);
 283
 284        bfin_write_PORT_MUX(pmux);
 285}
 286#elif defined(CONFIG_BF52x) || defined(CONFIG_BF51x)
 287static int portmux_group_check(unsigned short per)
 288{
 289        u16 ident = P_IDENT(per);
 290        u16 function = P_FUNCT2MUX(per);
 291        u8 offset = pmux_offset[gpio_bank(ident)][gpio_sub_n(ident)];
 292        u16 pin, gpiopin, pfunc;
 293
 294        for (pin = 0; pin < GPIO_BANKSIZE; ++pin) {
 295                if (offset != pmux_offset[gpio_bank(ident)][pin])
 296                        continue;
 297
 298                gpiopin = gpio_bank(ident) * GPIO_BANKSIZE + pin;
 299                if (gpiopin == ident)
 300                        continue;
 301                if (!is_reserved(peri, gpiopin, 1))
 302                        continue;
 303
 304                pfunc = *port_mux[gpio_bank(ident)];
 305                pfunc = (pfunc >> offset) & 3;
 306                if (pfunc != function) {
 307                        pr_err("pin group conflict! request pin %d func %d conflict with pin %d func %d\n",
 308                                ident, function, gpiopin, pfunc);
 309                        return -EINVAL;
 310                }
 311        }
 312
 313        return 0;
 314}
 315
 316inline void portmux_setup(unsigned short per)
 317{
 318        u16 ident = P_IDENT(per);
 319        u16 function = P_FUNCT2MUX(per);
 320        u8 offset = pmux_offset[gpio_bank(ident)][gpio_sub_n(ident)];
 321        u16 pmux;
 322
 323        pmux = *port_mux[gpio_bank(ident)];
 324        if  (((pmux >> offset) & 3) == function)
 325                return;
 326        pmux &= ~(3 << offset);
 327        pmux |= (function & 3) << offset;
 328        *port_mux[gpio_bank(ident)] = pmux;
 329        SSYNC();
 330}
 331#else
 332# define portmux_setup(...)  do { } while (0)
 333static int portmux_group_check(unsigned short per)
 334{
 335        return 0;
 336}
 337#endif
 338
 339/***********************************************************
 340*
 341* FUNCTIONS: Blackfin General Purpose Ports Access Functions
 342*
 343* INPUTS/OUTPUTS:
 344* gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS
 345*
 346*
 347* DESCRIPTION: These functions abstract direct register access
 348*              to Blackfin processor General Purpose
 349*              Ports Regsiters
 350*
 351* CAUTION: These functions do not belong to the GPIO Driver API
 352*************************************************************
 353* MODIFICATION HISTORY :
 354**************************************************************/
 355
 356/* Set a specific bit */
 357
 358#define SET_GPIO(name) \
 359void set_gpio_ ## name(unsigned gpio, unsigned short arg) \
 360{ \
 361        unsigned long flags; \
 362        flags = hard_local_irq_save(); \
 363        if (arg) \
 364                gpio_array[gpio_bank(gpio)]->name |= gpio_bit(gpio); \
 365        else \
 366                gpio_array[gpio_bank(gpio)]->name &= ~gpio_bit(gpio); \
 367        AWA_DUMMY_READ(name); \
 368        hard_local_irq_restore(flags); \
 369} \
 370EXPORT_SYMBOL(set_gpio_ ## name);
 371
 372SET_GPIO(dir)   /* set_gpio_dir() */
 373SET_GPIO(inen)  /* set_gpio_inen() */
 374SET_GPIO(polar) /* set_gpio_polar() */
 375SET_GPIO(edge)  /* set_gpio_edge() */
 376SET_GPIO(both)  /* set_gpio_both() */
 377
 378
 379#define SET_GPIO_SC(name) \
 380void set_gpio_ ## name(unsigned gpio, unsigned short arg) \
 381{ \
 382        unsigned long flags; \
 383        if (ANOMALY_05000311 || ANOMALY_05000323) \
 384                flags = hard_local_irq_save(); \
 385        if (arg) \
 386                gpio_array[gpio_bank(gpio)]->name ## _set = gpio_bit(gpio); \
 387        else \
 388                gpio_array[gpio_bank(gpio)]->name ## _clear = gpio_bit(gpio); \
 389        if (ANOMALY_05000311 || ANOMALY_05000323) { \
 390                AWA_DUMMY_READ(name); \
 391                hard_local_irq_restore(flags); \
 392        } \
 393} \
 394EXPORT_SYMBOL(set_gpio_ ## name);
 395
 396SET_GPIO_SC(maska)
 397SET_GPIO_SC(maskb)
 398SET_GPIO_SC(data)
 399
 400void set_gpio_toggle(unsigned gpio)
 401{
 402        unsigned long flags;
 403        if (ANOMALY_05000311 || ANOMALY_05000323)
 404                flags = hard_local_irq_save();
 405        gpio_array[gpio_bank(gpio)]->toggle = gpio_bit(gpio);
 406        if (ANOMALY_05000311 || ANOMALY_05000323) {
 407                AWA_DUMMY_READ(toggle);
 408                hard_local_irq_restore(flags);
 409        }
 410}
 411EXPORT_SYMBOL(set_gpio_toggle);
 412
 413
 414/*Set current PORT date (16-bit word)*/
 415
 416#define SET_GPIO_P(name) \
 417void set_gpiop_ ## name(unsigned gpio, unsigned short arg) \
 418{ \
 419        unsigned long flags; \
 420        if (ANOMALY_05000311 || ANOMALY_05000323) \
 421                flags = hard_local_irq_save(); \
 422        gpio_array[gpio_bank(gpio)]->name = arg; \
 423        if (ANOMALY_05000311 || ANOMALY_05000323) { \
 424                AWA_DUMMY_READ(name); \
 425                hard_local_irq_restore(flags); \
 426        } \
 427} \
 428EXPORT_SYMBOL(set_gpiop_ ## name);
 429
 430SET_GPIO_P(data)
 431SET_GPIO_P(dir)
 432SET_GPIO_P(inen)
 433SET_GPIO_P(polar)
 434SET_GPIO_P(edge)
 435SET_GPIO_P(both)
 436SET_GPIO_P(maska)
 437SET_GPIO_P(maskb)
 438
 439/* Get a specific bit */
 440#define GET_GPIO(name) \
 441unsigned short get_gpio_ ## name(unsigned gpio) \
 442{ \
 443        unsigned long flags; \
 444        unsigned short ret; \
 445        if (ANOMALY_05000311 || ANOMALY_05000323) \
 446                flags = hard_local_irq_save(); \
 447        ret = 0x01 & (gpio_array[gpio_bank(gpio)]->name >> gpio_sub_n(gpio)); \
 448        if (ANOMALY_05000311 || ANOMALY_05000323) { \
 449                AWA_DUMMY_READ(name); \
 450                hard_local_irq_restore(flags); \
 451        } \
 452        return ret; \
 453} \
 454EXPORT_SYMBOL(get_gpio_ ## name);
 455
 456GET_GPIO(data)
 457GET_GPIO(dir)
 458GET_GPIO(inen)
 459GET_GPIO(polar)
 460GET_GPIO(edge)
 461GET_GPIO(both)
 462GET_GPIO(maska)
 463GET_GPIO(maskb)
 464
 465/*Get current PORT date (16-bit word)*/
 466
 467#define GET_GPIO_P(name) \
 468unsigned short get_gpiop_ ## name(unsigned gpio) \
 469{ \
 470        unsigned long flags; \
 471        unsigned short ret; \
 472        if (ANOMALY_05000311 || ANOMALY_05000323) \
 473                flags = hard_local_irq_save(); \
 474        ret = (gpio_array[gpio_bank(gpio)]->name); \
 475        if (ANOMALY_05000311 || ANOMALY_05000323) { \
 476                AWA_DUMMY_READ(name); \
 477                hard_local_irq_restore(flags); \
 478        } \
 479        return ret; \
 480} \
 481EXPORT_SYMBOL(get_gpiop_ ## name);
 482
 483GET_GPIO_P(data)
 484GET_GPIO_P(dir)
 485GET_GPIO_P(inen)
 486GET_GPIO_P(polar)
 487GET_GPIO_P(edge)
 488GET_GPIO_P(both)
 489GET_GPIO_P(maska)
 490GET_GPIO_P(maskb)
 491
 492
 493#ifdef CONFIG_PM
 494DECLARE_RESERVED_MAP(wakeup, GPIO_BANK_NUM);
 495
 496static const unsigned int sic_iwr_irqs[] = {
 497#if defined(BF533_FAMILY)
 498        IRQ_PROG_INTB
 499#elif defined(BF537_FAMILY)
 500        IRQ_PF_INTB_WATCH, IRQ_PORTG_INTB, IRQ_PH_INTB_MAC_TX
 501#elif defined(BF538_FAMILY)
 502        IRQ_PORTF_INTB
 503#elif defined(CONFIG_BF52x) || defined(CONFIG_BF51x)
 504        IRQ_PORTF_INTB, IRQ_PORTG_INTB, IRQ_PORTH_INTB
 505#elif defined(BF561_FAMILY)
 506        IRQ_PROG0_INTB, IRQ_PROG1_INTB, IRQ_PROG2_INTB
 507#else
 508# error no SIC_IWR defined
 509#endif
 510};
 511
 512/***********************************************************
 513*
 514* FUNCTIONS: Blackfin PM Setup API
 515*
 516* INPUTS/OUTPUTS:
 517* gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS
 518* type -
 519*       PM_WAKE_RISING
 520*       PM_WAKE_FALLING
 521*       PM_WAKE_HIGH
 522*       PM_WAKE_LOW
 523*       PM_WAKE_BOTH_EDGES
 524*
 525* DESCRIPTION: Blackfin PM Driver API
 526*
 527* CAUTION:
 528*************************************************************
 529* MODIFICATION HISTORY :
 530**************************************************************/
 531int bfin_gpio_pm_wakeup_ctrl(unsigned gpio, unsigned ctrl)
 532{
 533        unsigned long flags;
 534
 535        if (check_gpio(gpio) < 0)
 536                return -EINVAL;
 537
 538        flags = hard_local_irq_save();
 539        if (ctrl)
 540                reserve(wakeup, gpio);
 541        else
 542                unreserve(wakeup, gpio);
 543
 544        set_gpio_maskb(gpio, ctrl);
 545        hard_local_irq_restore(flags);
 546
 547        return 0;
 548}
 549
 550int bfin_gpio_pm_standby_ctrl(unsigned ctrl)
 551{
 552        u16 bank, mask, i;
 553
 554        for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
 555                mask = map_entry(wakeup, i);
 556                bank = gpio_bank(i);
 557
 558                if (mask)
 559                        bfin_internal_set_wake(sic_iwr_irqs[bank], ctrl);
 560        }
 561        return 0;
 562}
 563
 564void bfin_gpio_pm_hibernate_suspend(void)
 565{
 566        int i, bank;
 567
 568#ifdef BF538_FAMILY
 569        for (i = 0; i < ARRAY_SIZE(port_fer_saved); ++i)
 570                port_fer_saved[i] = *port_fer[i];
 571#endif
 572
 573        for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
 574                bank = gpio_bank(i);
 575
 576#if defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
 577                gpio_bank_saved[bank].fer = *port_fer[bank];
 578#if defined(CONFIG_BF52x) || defined(CONFIG_BF51x)
 579                gpio_bank_saved[bank].mux = *port_mux[bank];
 580#else
 581                if (bank == 0)
 582                        gpio_bank_saved[bank].mux = bfin_read_PORT_MUX();
 583#endif
 584#endif
 585                gpio_bank_saved[bank].data  = gpio_array[bank]->data;
 586                gpio_bank_saved[bank].inen  = gpio_array[bank]->inen;
 587                gpio_bank_saved[bank].polar = gpio_array[bank]->polar;
 588                gpio_bank_saved[bank].dir   = gpio_array[bank]->dir;
 589                gpio_bank_saved[bank].edge  = gpio_array[bank]->edge;
 590                gpio_bank_saved[bank].both  = gpio_array[bank]->both;
 591                gpio_bank_saved[bank].maska = gpio_array[bank]->maska;
 592        }
 593
 594#ifdef BFIN_SPECIAL_GPIO_BANKS
 595        bfin_special_gpio_pm_hibernate_suspend();
 596#endif
 597
 598        AWA_DUMMY_READ(maska);
 599}
 600
 601void bfin_gpio_pm_hibernate_restore(void)
 602{
 603        int i, bank;
 604
 605#ifdef BF538_FAMILY
 606        for (i = 0; i < ARRAY_SIZE(port_fer_saved); ++i)
 607                *port_fer[i] = port_fer_saved[i];
 608#endif
 609
 610        for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
 611                bank = gpio_bank(i);
 612
 613#if defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
 614#if defined(CONFIG_BF52x) || defined(CONFIG_BF51x)
 615                *port_mux[bank] = gpio_bank_saved[bank].mux;
 616#else
 617                if (bank == 0)
 618                        bfin_write_PORT_MUX(gpio_bank_saved[bank].mux);
 619#endif
 620                *port_fer[bank] = gpio_bank_saved[bank].fer;
 621#endif
 622                gpio_array[bank]->inen  = gpio_bank_saved[bank].inen;
 623                gpio_array[bank]->data_set = gpio_bank_saved[bank].data
 624                                                & gpio_bank_saved[bank].dir;
 625                gpio_array[bank]->dir   = gpio_bank_saved[bank].dir;
 626                gpio_array[bank]->polar = gpio_bank_saved[bank].polar;
 627                gpio_array[bank]->edge  = gpio_bank_saved[bank].edge;
 628                gpio_array[bank]->both  = gpio_bank_saved[bank].both;
 629                gpio_array[bank]->maska = gpio_bank_saved[bank].maska;
 630        }
 631
 632#ifdef BFIN_SPECIAL_GPIO_BANKS
 633        bfin_special_gpio_pm_hibernate_restore();
 634#endif
 635
 636        AWA_DUMMY_READ(maska);
 637}
 638
 639
 640#endif
 641
 642/***********************************************************
 643*
 644* FUNCTIONS:    Blackfin Peripheral Resource Allocation
 645*               and PortMux Setup
 646*
 647* INPUTS/OUTPUTS:
 648* per   Peripheral Identifier
 649* label String
 650*
 651* DESCRIPTION: Blackfin Peripheral Resource Allocation and Setup API
 652*
 653* CAUTION:
 654*************************************************************
 655* MODIFICATION HISTORY :
 656**************************************************************/
 657
 658int peripheral_request(unsigned short per, const char *label)
 659{
 660        unsigned long flags;
 661        unsigned short ident = P_IDENT(per);
 662
 663        /*
 664         * Don't cares are pins with only one dedicated function
 665         */
 666
 667        if (per & P_DONTCARE)
 668                return 0;
 669
 670        if (!(per & P_DEFINED))
 671                return -ENODEV;
 672
 673        BUG_ON(ident >= MAX_RESOURCES);
 674
 675        flags = hard_local_irq_save();
 676
 677        /* If a pin can be muxed as either GPIO or peripheral, make
 678         * sure it is not already a GPIO pin when we request it.
 679         */
 680        if (unlikely(!check_gpio(ident) && is_reserved(gpio, ident, 1))) {
 681                if (system_state == SYSTEM_BOOTING)
 682                        dump_stack();
 683                printk(KERN_ERR
 684                       "%s: Peripheral %d is already reserved as GPIO by %s !\n",
 685                       __func__, ident, get_label(ident));
 686                hard_local_irq_restore(flags);
 687                return -EBUSY;
 688        }
 689
 690        if (unlikely(is_reserved(peri, ident, 1))) {
 691
 692                /*
 693                 * Pin functions like AMC address strobes my
 694                 * be requested and used by several drivers
 695                 */
 696
 697                if (!(per & P_MAYSHARE)) {
 698                        /*
 699                         * Allow that the identical pin function can
 700                         * be requested from the same driver twice
 701                         */
 702
 703                        if (cmp_label(ident, label) == 0)
 704                                goto anyway;
 705
 706                        if (system_state == SYSTEM_BOOTING)
 707                                dump_stack();
 708                        printk(KERN_ERR
 709                               "%s: Peripheral %d function %d is already reserved by %s !\n",
 710                               __func__, ident, P_FUNCT2MUX(per), get_label(ident));
 711                        hard_local_irq_restore(flags);
 712                        return -EBUSY;
 713                }
 714        }
 715
 716        if (unlikely(portmux_group_check(per))) {
 717                hard_local_irq_restore(flags);
 718                return -EBUSY;
 719        }
 720 anyway:
 721        reserve(peri, ident);
 722
 723        portmux_setup(per);
 724        port_setup(ident, PERIPHERAL_USAGE);
 725
 726        hard_local_irq_restore(flags);
 727        set_label(ident, label);
 728
 729        return 0;
 730}
 731EXPORT_SYMBOL(peripheral_request);
 732
 733int peripheral_request_list(const unsigned short per[], const char *label)
 734{
 735        u16 cnt;
 736        int ret;
 737
 738        for (cnt = 0; per[cnt] != 0; cnt++) {
 739
 740                ret = peripheral_request(per[cnt], label);
 741
 742                if (ret < 0) {
 743                        for ( ; cnt > 0; cnt--)
 744                                peripheral_free(per[cnt - 1]);
 745
 746                        return ret;
 747                }
 748        }
 749
 750        return 0;
 751}
 752EXPORT_SYMBOL(peripheral_request_list);
 753
 754void peripheral_free(unsigned short per)
 755{
 756        unsigned long flags;
 757        unsigned short ident = P_IDENT(per);
 758
 759        if (per & P_DONTCARE)
 760                return;
 761
 762        if (!(per & P_DEFINED))
 763                return;
 764
 765        flags = hard_local_irq_save();
 766
 767        if (unlikely(!is_reserved(peri, ident, 0))) {
 768                hard_local_irq_restore(flags);
 769                return;
 770        }
 771
 772        if (!(per & P_MAYSHARE))
 773                port_setup(ident, GPIO_USAGE);
 774
 775        unreserve(peri, ident);
 776
 777        set_label(ident, "free");
 778
 779        hard_local_irq_restore(flags);
 780}
 781EXPORT_SYMBOL(peripheral_free);
 782
 783void peripheral_free_list(const unsigned short per[])
 784{
 785        u16 cnt;
 786        for (cnt = 0; per[cnt] != 0; cnt++)
 787                peripheral_free(per[cnt]);
 788}
 789EXPORT_SYMBOL(peripheral_free_list);
 790
 791/***********************************************************
 792*
 793* FUNCTIONS: Blackfin GPIO Driver
 794*
 795* INPUTS/OUTPUTS:
 796* gpio  PIO Number between 0 and MAX_BLACKFIN_GPIOS
 797* label String
 798*
 799* DESCRIPTION: Blackfin GPIO Driver API
 800*
 801* CAUTION:
 802*************************************************************
 803* MODIFICATION HISTORY :
 804**************************************************************/
 805
 806int bfin_gpio_request(unsigned gpio, const char *label)
 807{
 808        unsigned long flags;
 809
 810        if (check_gpio(gpio) < 0)
 811                return -EINVAL;
 812
 813        flags = hard_local_irq_save();
 814
 815        /*
 816         * Allow that the identical GPIO can
 817         * be requested from the same driver twice
 818         * Do nothing and return -
 819         */
 820
 821        if (cmp_label(gpio, label) == 0) {
 822                hard_local_irq_restore(flags);
 823                return 0;
 824        }
 825
 826        if (unlikely(is_reserved(gpio, gpio, 1))) {
 827                if (system_state == SYSTEM_BOOTING)
 828                        dump_stack();
 829                printk(KERN_ERR "bfin-gpio: GPIO %d is already reserved by %s !\n",
 830                       gpio, get_label(gpio));
 831                hard_local_irq_restore(flags);
 832                return -EBUSY;
 833        }
 834        if (unlikely(is_reserved(peri, gpio, 1))) {
 835                if (system_state == SYSTEM_BOOTING)
 836                        dump_stack();
 837                printk(KERN_ERR
 838                       "bfin-gpio: GPIO %d is already reserved as Peripheral by %s !\n",
 839                       gpio, get_label(gpio));
 840                hard_local_irq_restore(flags);
 841                return -EBUSY;
 842        }
 843        if (unlikely(is_reserved(gpio_irq, gpio, 1))) {
 844                printk(KERN_NOTICE "bfin-gpio: GPIO %d is already reserved as gpio-irq!"
 845                       " (Documentation/blackfin/bfin-gpio-notes.txt)\n", gpio);
 846        } else {        /* Reset POLAR setting when acquiring a gpio for the first time */
 847                set_gpio_polar(gpio, 0);
 848        }
 849
 850        reserve(gpio, gpio);
 851        set_label(gpio, label);
 852
 853        hard_local_irq_restore(flags);
 854
 855        port_setup(gpio, GPIO_USAGE);
 856
 857        return 0;
 858}
 859EXPORT_SYMBOL(bfin_gpio_request);
 860
 861void bfin_gpio_free(unsigned gpio)
 862{
 863        unsigned long flags;
 864
 865        if (check_gpio(gpio) < 0)
 866                return;
 867
 868        might_sleep();
 869
 870        flags = hard_local_irq_save();
 871
 872        if (unlikely(!is_reserved(gpio, gpio, 0))) {
 873                if (system_state == SYSTEM_BOOTING)
 874                        dump_stack();
 875                gpio_error(gpio);
 876                hard_local_irq_restore(flags);
 877                return;
 878        }
 879
 880        unreserve(gpio, gpio);
 881
 882        set_label(gpio, "free");
 883
 884        hard_local_irq_restore(flags);
 885}
 886EXPORT_SYMBOL(bfin_gpio_free);
 887
 888#ifdef BFIN_SPECIAL_GPIO_BANKS
 889DECLARE_RESERVED_MAP(special_gpio, gpio_bank(MAX_RESOURCES));
 890
 891int bfin_special_gpio_request(unsigned gpio, const char *label)
 892{
 893        unsigned long flags;
 894
 895        flags = hard_local_irq_save();
 896
 897        /*
 898         * Allow that the identical GPIO can
 899         * be requested from the same driver twice
 900         * Do nothing and return -
 901         */
 902
 903        if (cmp_label(gpio, label) == 0) {
 904                hard_local_irq_restore(flags);
 905                return 0;
 906        }
 907
 908        if (unlikely(is_reserved(special_gpio, gpio, 1))) {
 909                hard_local_irq_restore(flags);
 910                printk(KERN_ERR "bfin-gpio: GPIO %d is already reserved by %s !\n",
 911                       gpio, get_label(gpio));
 912
 913                return -EBUSY;
 914        }
 915        if (unlikely(is_reserved(peri, gpio, 1))) {
 916                hard_local_irq_restore(flags);
 917                printk(KERN_ERR
 918                       "bfin-gpio: GPIO %d is already reserved as Peripheral by %s !\n",
 919                       gpio, get_label(gpio));
 920
 921                return -EBUSY;
 922        }
 923
 924        reserve(special_gpio, gpio);
 925        reserve(peri, gpio);
 926
 927        set_label(gpio, label);
 928        hard_local_irq_restore(flags);
 929        port_setup(gpio, GPIO_USAGE);
 930
 931        return 0;
 932}
 933EXPORT_SYMBOL(bfin_special_gpio_request);
 934
 935void bfin_special_gpio_free(unsigned gpio)
 936{
 937        unsigned long flags;
 938
 939        might_sleep();
 940
 941        flags = hard_local_irq_save();
 942
 943        if (unlikely(!is_reserved(special_gpio, gpio, 0))) {
 944                gpio_error(gpio);
 945                hard_local_irq_restore(flags);
 946                return;
 947        }
 948
 949        unreserve(special_gpio, gpio);
 950        unreserve(peri, gpio);
 951        set_label(gpio, "free");
 952        hard_local_irq_restore(flags);
 953}
 954EXPORT_SYMBOL(bfin_special_gpio_free);
 955#endif
 956
 957
 958int bfin_gpio_irq_request(unsigned gpio, const char *label)
 959{
 960        unsigned long flags;
 961
 962        if (check_gpio(gpio) < 0)
 963                return -EINVAL;
 964
 965        flags = hard_local_irq_save();
 966
 967        if (unlikely(is_reserved(peri, gpio, 1))) {
 968                if (system_state == SYSTEM_BOOTING)
 969                        dump_stack();
 970                printk(KERN_ERR
 971                       "bfin-gpio: GPIO %d is already reserved as Peripheral by %s !\n",
 972                       gpio, get_label(gpio));
 973                hard_local_irq_restore(flags);
 974                return -EBUSY;
 975        }
 976        if (unlikely(is_reserved(gpio, gpio, 1)))
 977                printk(KERN_NOTICE "bfin-gpio: GPIO %d is already reserved by %s! "
 978                       "(Documentation/blackfin/bfin-gpio-notes.txt)\n",
 979                       gpio, get_label(gpio));
 980
 981        reserve(gpio_irq, gpio);
 982        set_label(gpio, label);
 983
 984        hard_local_irq_restore(flags);
 985
 986        port_setup(gpio, GPIO_USAGE);
 987
 988        return 0;
 989}
 990
 991void bfin_gpio_irq_free(unsigned gpio)
 992{
 993        unsigned long flags;
 994
 995        if (check_gpio(gpio) < 0)
 996                return;
 997
 998        flags = hard_local_irq_save();
 999
1000        if (unlikely(!is_reserved(gpio_irq, gpio, 0))) {
1001                if (system_state == SYSTEM_BOOTING)
1002                        dump_stack();
1003                gpio_error(gpio);
1004                hard_local_irq_restore(flags);
1005                return;
1006        }
1007
1008        unreserve(gpio_irq, gpio);
1009
1010        set_label(gpio, "free");
1011
1012        hard_local_irq_restore(flags);
1013}
1014
1015static inline void __bfin_gpio_direction_input(unsigned gpio)
1016{
1017        gpio_array[gpio_bank(gpio)]->dir &= ~gpio_bit(gpio);
1018        gpio_array[gpio_bank(gpio)]->inen |= gpio_bit(gpio);
1019}
1020
1021int bfin_gpio_direction_input(unsigned gpio)
1022{
1023        unsigned long flags;
1024
1025        if (unlikely(!is_reserved(gpio, gpio, 0))) {
1026                gpio_error(gpio);
1027                return -EINVAL;
1028        }
1029
1030        flags = hard_local_irq_save();
1031        __bfin_gpio_direction_input(gpio);
1032        AWA_DUMMY_READ(inen);
1033        hard_local_irq_restore(flags);
1034
1035        return 0;
1036}
1037EXPORT_SYMBOL(bfin_gpio_direction_input);
1038
1039void bfin_gpio_irq_prepare(unsigned gpio)
1040{
1041        port_setup(gpio, GPIO_USAGE);
1042}
1043
1044void bfin_gpio_set_value(unsigned gpio, int arg)
1045{
1046        if (arg)
1047                gpio_array[gpio_bank(gpio)]->data_set = gpio_bit(gpio);
1048        else
1049                gpio_array[gpio_bank(gpio)]->data_clear = gpio_bit(gpio);
1050}
1051EXPORT_SYMBOL(bfin_gpio_set_value);
1052
1053int bfin_gpio_direction_output(unsigned gpio, int value)
1054{
1055        unsigned long flags;
1056
1057        if (unlikely(!is_reserved(gpio, gpio, 0))) {
1058                gpio_error(gpio);
1059                return -EINVAL;
1060        }
1061
1062        flags = hard_local_irq_save();
1063
1064        gpio_array[gpio_bank(gpio)]->inen &= ~gpio_bit(gpio);
1065        gpio_set_value(gpio, value);
1066        gpio_array[gpio_bank(gpio)]->dir |= gpio_bit(gpio);
1067
1068        AWA_DUMMY_READ(dir);
1069        hard_local_irq_restore(flags);
1070
1071        return 0;
1072}
1073EXPORT_SYMBOL(bfin_gpio_direction_output);
1074
1075int bfin_gpio_get_value(unsigned gpio)
1076{
1077        unsigned long flags;
1078
1079        if (unlikely(get_gpio_edge(gpio))) {
1080                int ret;
1081                flags = hard_local_irq_save();
1082                set_gpio_edge(gpio, 0);
1083                ret = get_gpio_data(gpio);
1084                set_gpio_edge(gpio, 1);
1085                hard_local_irq_restore(flags);
1086                return ret;
1087        } else
1088                return get_gpio_data(gpio);
1089}
1090EXPORT_SYMBOL(bfin_gpio_get_value);
1091
1092/* If we are booting from SPI and our board lacks a strong enough pull up,
1093 * the core can reset and execute the bootrom faster than the resistor can
1094 * pull the signal logically high.  To work around this (common) error in
1095 * board design, we explicitly set the pin back to GPIO mode, force /CS
1096 * high, and wait for the electrons to do their thing.
1097 *
1098 * This function only makes sense to be called from reset code, but it
1099 * lives here as we need to force all the GPIO states w/out going through
1100 * BUG() checks and such.
1101 */
1102void bfin_reset_boot_spi_cs(unsigned short pin)
1103{
1104        unsigned short gpio = P_IDENT(pin);
1105        port_setup(gpio, GPIO_USAGE);
1106        gpio_array[gpio_bank(gpio)]->data_set = gpio_bit(gpio);
1107        AWA_DUMMY_READ(data_set);
1108        udelay(1);
1109}
1110
1111#if defined(CONFIG_PROC_FS)
1112static int gpio_proc_show(struct seq_file *m, void *v)
1113{
1114        int c, irq, gpio;
1115
1116        for (c = 0; c < MAX_RESOURCES; c++) {
1117                irq = is_reserved(gpio_irq, c, 1);
1118                gpio = is_reserved(gpio, c, 1);
1119                if (!check_gpio(c) && (gpio || irq))
1120                        seq_printf(m, "GPIO_%d: \t%s%s \t\tGPIO %s\n", c,
1121                                 get_label(c), (gpio && irq) ? " *" : "",
1122                                 get_gpio_dir(c) ? "OUTPUT" : "INPUT");
1123                else if (is_reserved(peri, c, 1))
1124                        seq_printf(m, "GPIO_%d: \t%s \t\tPeripheral\n", c, get_label(c));
1125                else
1126                        continue;
1127        }
1128
1129        return 0;
1130}
1131
1132static int gpio_proc_open(struct inode *inode, struct file *file)
1133{
1134        return single_open(file, gpio_proc_show, NULL);
1135}
1136
1137static const struct file_operations gpio_proc_ops = {
1138        .open           = gpio_proc_open,
1139        .read           = seq_read,
1140        .llseek         = seq_lseek,
1141        .release        = single_release,
1142};
1143
1144static __init int gpio_register_proc(void)
1145{
1146        struct proc_dir_entry *proc_gpio;
1147
1148        proc_gpio = proc_create("gpio", 0, NULL, &gpio_proc_ops);
1149        return proc_gpio == NULL;
1150}
1151__initcall(gpio_register_proc);
1152#endif
1153
1154#ifdef CONFIG_GPIOLIB
1155static int bfin_gpiolib_direction_input(struct gpio_chip *chip, unsigned gpio)
1156{
1157        return bfin_gpio_direction_input(gpio);
1158}
1159
1160static int bfin_gpiolib_direction_output(struct gpio_chip *chip, unsigned gpio, int level)
1161{
1162        return bfin_gpio_direction_output(gpio, level);
1163}
1164
1165static int bfin_gpiolib_get_value(struct gpio_chip *chip, unsigned gpio)
1166{
1167        return !!bfin_gpio_get_value(gpio);
1168}
1169
1170static void bfin_gpiolib_set_value(struct gpio_chip *chip, unsigned gpio, int value)
1171{
1172        return bfin_gpio_set_value(gpio, value);
1173}
1174
1175static int bfin_gpiolib_gpio_request(struct gpio_chip *chip, unsigned gpio)
1176{
1177        return bfin_gpio_request(gpio, chip->label);
1178}
1179
1180static void bfin_gpiolib_gpio_free(struct gpio_chip *chip, unsigned gpio)
1181{
1182        return bfin_gpio_free(gpio);
1183}
1184
1185static int bfin_gpiolib_gpio_to_irq(struct gpio_chip *chip, unsigned gpio)
1186{
1187        return gpio + GPIO_IRQ_BASE;
1188}
1189
1190static struct gpio_chip bfin_chip = {
1191        .label                  = "BFIN-GPIO",
1192        .direction_input        = bfin_gpiolib_direction_input,
1193        .get                    = bfin_gpiolib_get_value,
1194        .direction_output       = bfin_gpiolib_direction_output,
1195        .set                    = bfin_gpiolib_set_value,
1196        .request                = bfin_gpiolib_gpio_request,
1197        .free                   = bfin_gpiolib_gpio_free,
1198        .to_irq                 = bfin_gpiolib_gpio_to_irq,
1199        .base                   = 0,
1200        .ngpio                  = MAX_BLACKFIN_GPIOS,
1201};
1202
1203static int __init bfin_gpiolib_setup(void)
1204{
1205        return gpiochip_add_data(&bfin_chip, NULL);
1206}
1207arch_initcall(bfin_gpiolib_setup);
1208#endif
1209