linux/drivers/pcmcia/vrc4173_cardu.c
<<
>>
Prefs
   1/*
   2 * FILE NAME
   3 *      drivers/pcmcia/vrc4173_cardu.c
   4 *
   5 * BRIEF MODULE DESCRIPTION
   6 *      NEC VRC4173 CARDU driver for Socket Services
   7 *      (This device doesn't support CardBus. it is supporting only 16bit PC Card.)
   8 *
   9 * Copyright 2002,2003 Yoichi Yuasa <yuasa@linux-mips.org>
  10 *
  11 *  This program is free software; you can redistribute it and/or modify it
  12 *  under the terms of the GNU General Public License as published by the
  13 *  Free Software Foundation; either version 2 of the License, or (at your
  14 *  option) any later version.
  15 *
  16 *  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17 *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18 *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  19 *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  20 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  21 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  22 *  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23 *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
  24 *  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  25 *  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26 *
  27 *  You should have received a copy of the GNU General Public License along
  28 *  with this program; if not, write to the Free Software Foundation, Inc.,
  29 *  675 Mass Ave, Cambridge, MA 02139, USA.
  30 */
  31#include <linux/init.h>
  32#include <linux/module.h>
  33#include <linux/pci.h>
  34#include <linux/spinlock.h>
  35#include <linux/types.h>
  36
  37#include <asm/io.h>
  38
  39#include <pcmcia/ss.h>
  40
  41#include "vrc4173_cardu.h"
  42
  43MODULE_DESCRIPTION("NEC VRC4173 CARDU driver for Socket Services");
  44MODULE_AUTHOR("Yoichi Yuasa <yuasa@linux-mips.org>");
  45MODULE_LICENSE("GPL");
  46
  47static int vrc4173_cardu_slots;
  48
  49static vrc4173_socket_t cardu_sockets[CARDU_MAX_SOCKETS];
  50
  51extern struct socket_info_t *pcmcia_register_socket (int slot,
  52                                                     struct pccard_operations *vtable,
  53                                                     int use_bus_pm);
  54extern void pcmcia_unregister_socket(struct socket_info_t *s);
  55
  56static inline uint8_t exca_readb(vrc4173_socket_t *socket, uint16_t offset)
  57{
  58        return readb(socket->base + EXCA_REGS_BASE + offset);
  59}
  60
  61static inline uint16_t exca_readw(vrc4173_socket_t *socket, uint16_t offset)
  62{
  63        uint16_t val;
  64
  65        val = readb(socket->base + EXCA_REGS_BASE + offset);
  66        val |= (u16)readb(socket->base + EXCA_REGS_BASE + offset + 1) << 8;
  67
  68        return val;
  69}
  70
  71static inline void exca_writeb(vrc4173_socket_t *socket, uint16_t offset, uint8_t val)
  72{
  73        writeb(val, socket->base + EXCA_REGS_BASE + offset);
  74}
  75
  76static inline void exca_writew(vrc4173_socket_t *socket, uint8_t offset, uint16_t val)
  77{
  78        writeb((u8)val, socket->base + EXCA_REGS_BASE + offset);
  79        writeb((u8)(val >> 8), socket->base + EXCA_REGS_BASE + offset + 1);
  80}
  81
  82static inline uint32_t cardbus_socket_readl(vrc4173_socket_t *socket, u16 offset)
  83{
  84        return readl(socket->base + CARDBUS_SOCKET_REGS_BASE + offset);
  85}
  86
  87static inline void cardbus_socket_writel(vrc4173_socket_t *socket, u16 offset, uint32_t val)
  88{
  89        writel(val, socket->base + CARDBUS_SOCKET_REGS_BASE + offset);
  90}
  91
  92static void cardu_pciregs_init(struct pci_dev *dev)
  93{
  94        u32 syscnt;
  95        u16 brgcnt;
  96        u8 devcnt;
  97
  98        pci_write_config_dword(dev, 0x1c, 0x10000000);
  99        pci_write_config_dword(dev, 0x20, 0x17fff000);
 100        pci_write_config_dword(dev, 0x2c, 0);
 101        pci_write_config_dword(dev, 0x30, 0xfffc);
 102
 103        pci_read_config_word(dev, BRGCNT, &brgcnt);
 104        brgcnt &= ~IREQ_INT;
 105        pci_write_config_word(dev, BRGCNT, brgcnt);
 106
 107        pci_read_config_dword(dev, SYSCNT, &syscnt);
 108        syscnt &= ~(BAD_VCC_REQ_DISB|PCPCI_EN|CH_ASSIGN_MASK|SUB_ID_WR_EN|PCI_CLK_RIN);
 109        syscnt |= (CH_ASSIGN_NODMA|ASYN_INT_MODE);
 110        pci_write_config_dword(dev, SYSCNT, syscnt);
 111
 112        pci_read_config_byte(dev, DEVCNT, &devcnt);
 113        devcnt &= ~(ZOOM_VIDEO_EN|SR_PCI_INT_SEL_MASK|PCI_INT_MODE|IRQ_MODE);
 114        devcnt |= (SR_PCI_INT_SEL_NONE|IFG);
 115        pci_write_config_byte(dev, DEVCNT, devcnt);
 116
 117        pci_write_config_byte(dev, CHIPCNT, S_PREF_DISB);
 118
 119        pci_write_config_byte(dev, SERRDIS, 0);
 120}
 121
 122static int cardu_init(unsigned int slot)
 123{
 124        vrc4173_socket_t *socket = &cardu_sockets[slot];
 125
 126        cardu_pciregs_init(socket->dev);
 127
 128        /* CARD_SC bits are cleared by reading CARD_SC. */
 129        exca_writeb(socket, GLO_CNT, 0);
 130
 131        socket->cap.features |= SS_CAP_PCCARD | SS_CAP_PAGE_REGS;
 132        socket->cap.irq_mask = 0;
 133        socket->cap.map_size = 0x1000;
 134        socket->cap.pci_irq  = socket->dev->irq;
 135        socket->events = 0;
 136        spin_lock_init(socket->event_lock);
 137
 138        /* Enable PC Card status interrupts */
 139        exca_writeb(socket, CARD_SCI, CARD_DT_EN|RDY_EN|BAT_WAR_EN|BAT_DEAD_EN);
 140
 141        return 0;
 142}
 143
 144static int cardu_register_callback(unsigned int sock,
 145                                           void (*handler)(void *, unsigned int),
 146                                           void * info)
 147{
 148        vrc4173_socket_t *socket = &cardu_sockets[sock];
 149
 150        socket->handler = handler;
 151        socket->info = info;
 152
 153        return 0;
 154}
 155
 156static int cardu_inquire_socket(unsigned int sock, socket_cap_t *cap)
 157{
 158        vrc4173_socket_t *socket = &cardu_sockets[sock];
 159
 160        *cap = socket->cap;
 161
 162        return 0;
 163}
 164
 165static int cardu_get_status(unsigned int sock, u_int *value)
 166{
 167        vrc4173_socket_t *socket = &cardu_sockets[sock];
 168        uint32_t state;
 169        uint8_t status;
 170        u_int val = 0;
 171
 172        status = exca_readb(socket, IF_STATUS);
 173        if (status & CARD_PWR) val |= SS_POWERON;
 174        if (status & READY) val |= SS_READY;
 175        if (status & CARD_WP) val |= SS_WRPROT;
 176        if ((status & (CARD_DETECT1|CARD_DETECT2)) == (CARD_DETECT1|CARD_DETECT2))
 177                val |= SS_DETECT;
 178        if (exca_readb(socket, INT_GEN_CNT) & CARD_TYPE_IO) {
 179                if (status & STSCHG) val |= SS_STSCHG;
 180        } else {
 181                status &= BV_DETECT_MASK;
 182                if (status != BV_DETECT_GOOD) {
 183                        if (status == BV_DETECT_WARN) val |= SS_BATWARN;
 184                        else val |= SS_BATDEAD;
 185                }
 186        }
 187
 188        state = cardbus_socket_readl(socket, SKT_PRE_STATE);
 189        if (state & VOL_3V_CARD_DT) val |= SS_3VCARD;
 190        if (state & VOL_XV_CARD_DT) val |= SS_XVCARD;
 191        if (state & CB_CARD_DT) val |= SS_CARDBUS;
 192        if (!(state &
 193              (VOL_YV_CARD_DT|VOL_XV_CARD_DT|VOL_3V_CARD_DT|VOL_5V_CARD_DT|CCD20|CCD10)))
 194                val |= SS_PENDING;
 195
 196        *value = val;
 197
 198        return 0;
 199}
 200
 201static inline uint8_t set_Vcc_value(u_char Vcc)
 202{
 203        switch (Vcc) {
 204        case 33:
 205                return VCC_3V;
 206        case 50:
 207                return VCC_5V;
 208        }
 209
 210        return VCC_0V;
 211}
 212
 213static inline uint8_t set_Vpp_value(u_char Vpp)
 214{
 215        switch (Vpp) {
 216        case 33:
 217        case 50:
 218                return VPP_VCC;
 219        case 120:
 220                return VPP_12V;
 221        }
 222
 223        return VPP_0V;
 224}
 225
 226static int cardu_set_socket(unsigned int sock, socket_state_t *state)
 227{
 228        vrc4173_socket_t *socket = &cardu_sockets[sock];
 229        uint8_t val;
 230
 231        if (((state->Vpp == 33) || (state->Vpp == 50)) && (state->Vpp != state->Vcc))
 232                        return -EINVAL;
 233
 234        val = set_Vcc_value(state->Vcc);
 235        val |= set_Vpp_value(state->Vpp);
 236        if (state->flags & SS_OUTPUT_ENA) val |= CARD_OUT_EN;
 237        exca_writeb(socket, PWR_CNT, val);
 238
 239        val = exca_readb(socket, INT_GEN_CNT) & CARD_REST0;
 240        if (state->flags & SS_RESET) val &= ~CARD_REST0;
 241        else val |= CARD_REST0;
 242        if (state->flags & SS_IOCARD) val |= CARD_TYPE_IO;
 243        exca_writeb(socket, INT_GEN_CNT, val);
 244
 245        return 0;
 246}
 247
 248static int cardu_get_io_map(unsigned int sock, struct pccard_io_map *io)
 249{
 250        vrc4173_socket_t *socket = &cardu_sockets[sock];
 251        uint8_t ioctl, window;
 252        u_char map;
 253
 254        map = io->map;
 255        if (map > 1)
 256                return -EINVAL;
 257
 258        io->start = exca_readw(socket, IO_WIN_SA(map));
 259        io->stop = exca_readw(socket, IO_WIN_EA(map));
 260
 261        ioctl = exca_readb(socket, IO_WIN_CNT);
 262        window = exca_readb(socket, ADR_WIN_EN);
 263        io->flags  = (window & IO_WIN_EN(map)) ? MAP_ACTIVE : 0;
 264        if (ioctl & IO_WIN_DATA_AUTOSZ(map))
 265                io->flags |= MAP_AUTOSZ;
 266        else if (ioctl & IO_WIN_DATA_16BIT(map))
 267                io->flags |= MAP_16BIT;
 268
 269        return 0;
 270}
 271
 272static int cardu_set_io_map(unsigned int sock, struct pccard_io_map *io)
 273{
 274        vrc4173_socket_t *socket = &cardu_sockets[sock];
 275        uint16_t ioctl;
 276        uint8_t window, enable;
 277        u_char map;
 278
 279        map = io->map;
 280        if (map > 1)
 281                return -EINVAL;
 282
 283        window = exca_readb(socket, ADR_WIN_EN);
 284        enable = IO_WIN_EN(map);
 285
 286        if (window & enable) {
 287                window &= ~enable;
 288                exca_writeb(socket, ADR_WIN_EN, window);
 289        }
 290
 291        exca_writew(socket, IO_WIN_SA(map), io->start);
 292        exca_writew(socket, IO_WIN_EA(map), io->stop);
 293
 294        ioctl = exca_readb(socket, IO_WIN_CNT) & ~IO_WIN_CNT_MASK(map);
 295        if (io->flags & MAP_AUTOSZ) ioctl |= IO_WIN_DATA_AUTOSZ(map);
 296        else if (io->flags & MAP_16BIT) ioctl |= IO_WIN_DATA_16BIT(map);
 297        exca_writeb(socket, IO_WIN_CNT, ioctl);
 298
 299        if (io->flags & MAP_ACTIVE)
 300                exca_writeb(socket, ADR_WIN_EN, window | enable);
 301
 302        return 0;
 303}
 304
 305static int cardu_get_mem_map(unsigned int sock, struct pccard_mem_map *mem)
 306{
 307        vrc4173_socket_t *socket = &cardu_sockets[sock];
 308        uint32_t start, stop, offset, page;
 309        uint8_t window;
 310        u_char map;
 311
 312        map = mem->map;
 313        if (map > 4)
 314                return -EINVAL;
 315
 316        window = exca_readb(socket, ADR_WIN_EN);
 317        mem->flags = (window & MEM_WIN_EN(map)) ? MAP_ACTIVE : 0;
 318
 319        start = exca_readw(socket, MEM_WIN_SA(map));
 320        mem->flags |= (start & MEM_WIN_DSIZE) ? MAP_16BIT : 0;
 321        start = (start & 0x0fff) << 12;
 322
 323        stop = exca_readw(socket, MEM_WIN_EA(map));
 324        stop = ((stop & 0x0fff) << 12) + 0x0fff;
 325
 326        offset = exca_readw(socket, MEM_WIN_OA(map));
 327        mem->flags |= (offset & MEM_WIN_WP) ? MAP_WRPROT : 0;
 328        mem->flags |= (offset & MEM_WIN_REGSET) ? MAP_ATTRIB : 0;
 329        offset = ((offset & 0x3fff) << 12) + start;
 330        mem->card_start = offset & 0x03ffffff;
 331
 332        page = exca_readb(socket, MEM_WIN_SAU(map)) << 24;
 333        mem->sys_start = start + page;
 334        mem->sys_stop = start + page;
 335
 336        return 0;
 337}
 338
 339static int cardu_set_mem_map(unsigned int sock, struct pccard_mem_map *mem)
 340{
 341        vrc4173_socket_t *socket = &cardu_sockets[sock];
 342        uint16_t value;
 343        uint8_t window, enable;
 344        u_long sys_start, sys_stop, card_start;
 345        u_char map;
 346
 347        map = mem->map;
 348        sys_start = mem->sys_start;
 349        sys_stop = mem->sys_stop;
 350        card_start = mem->card_start;
 351
 352        if (map > 4 || sys_start > sys_stop || ((sys_start ^ sys_stop) >> 24) ||
 353            (card_start >> 26))
 354                return -EINVAL;
 355
 356        window = exca_readb(socket, ADR_WIN_EN);
 357        enable = MEM_WIN_EN(map);
 358        if (window & enable) {
 359                window &= ~enable;
 360                exca_writeb(socket, ADR_WIN_EN, window);
 361        }
 362
 363        exca_writeb(socket, MEM_WIN_SAU(map), sys_start >> 24);
 364
 365        value = (sys_start >> 12) & 0x0fff;
 366        if (mem->flags & MAP_16BIT) value |= MEM_WIN_DSIZE;
 367        exca_writew(socket, MEM_WIN_SA(map), value);
 368
 369        value = (sys_stop >> 12) & 0x0fff;
 370        exca_writew(socket, MEM_WIN_EA(map), value);
 371
 372        value = ((card_start - sys_start) >> 12) & 0x3fff;
 373        if (mem->flags & MAP_WRPROT) value |= MEM_WIN_WP;
 374        if (mem->flags & MAP_ATTRIB) value |= MEM_WIN_REGSET;
 375        exca_writew(socket, MEM_WIN_OA(map), value);
 376
 377        if (mem->flags & MAP_ACTIVE)
 378                exca_writeb(socket, ADR_WIN_EN, window | enable);
 379
 380        return 0;
 381}
 382
 383static void cardu_proc_setup(unsigned int sock, struct proc_dir_entry *base)
 384{
 385}
 386
 387static struct pccard_operations cardu_operations = {
 388        .init                   = cardu_init,
 389        .register_callback      = cardu_register_callback,
 390        .inquire_socket         = cardu_inquire_socket,
 391        .get_status             = cardu_get_status,
 392        .set_socket             = cardu_set_socket,
 393        .get_io_map             = cardu_get_io_map,
 394        .set_io_map             = cardu_set_io_map,
 395        .get_mem_map            = cardu_get_mem_map,
 396        .set_mem_map            = cardu_set_mem_map,
 397        .proc_setup             = cardu_proc_setup,
 398};
 399
 400static void cardu_bh(void *data)
 401{
 402        vrc4173_socket_t *socket = (vrc4173_socket_t *)data;
 403        uint16_t events;
 404
 405        spin_lock_irq(&socket->event_lock);
 406        events = socket->events;
 407        socket->events = 0;
 408        spin_unlock_irq(&socket->event_lock);
 409
 410        if (socket->handler)
 411                socket->handler(socket->info, events);
 412}
 413
 414static uint16_t get_events(vrc4173_socket_t *socket)
 415{
 416        uint16_t events = 0;
 417        uint8_t csc, status;
 418
 419        status = exca_readb(socket, IF_STATUS);
 420        csc = exca_readb(socket, CARD_SC);
 421        if ((csc & CARD_DT_CHG) &&
 422            ((status & (CARD_DETECT1|CARD_DETECT2)) == (CARD_DETECT1|CARD_DETECT2)))
 423                events |= SS_DETECT;
 424
 425        if ((csc & RDY_CHG) && (status & READY))
 426                events |= SS_READY;
 427
 428        if (exca_readb(socket, INT_GEN_CNT) & CARD_TYPE_IO) {
 429                if ((csc & BAT_DEAD_ST_CHG) && (status & STSCHG))
 430                        events |= SS_STSCHG;
 431        } else {
 432                if (csc & (BAT_WAR_CHG|BAT_DEAD_ST_CHG)) {
 433                        if ((status & BV_DETECT_MASK) != BV_DETECT_GOOD) {
 434                                if (status == BV_DETECT_WARN) events |= SS_BATWARN;
 435                                else events |= SS_BATDEAD;
 436                        }
 437                }
 438        }
 439
 440        return events;
 441}
 442
 443static void cardu_interrupt(int irq, void *dev_id)
 444{
 445        vrc4173_socket_t *socket = (vrc4173_socket_t *)dev_id;
 446        uint16_t events;
 447
 448        INIT_WORK(&socket->tq_work, cardu_bh, socket);
 449
 450        events = get_events(socket);
 451        if (events) {
 452                spin_lock(&socket->event_lock);
 453                socket->events |= events;
 454                spin_unlock(&socket->event_lock);
 455                schedule_work(&socket->tq_work);
 456        }
 457}
 458
 459static int vrc4173_cardu_probe(struct pci_dev *dev,
 460                                         const struct pci_device_id *ent)
 461{
 462        vrc4173_socket_t *socket;
 463        unsigned long start, len, flags;
 464        int slot, err, ret;
 465
 466        slot = vrc4173_cardu_slots++;
 467        socket = &cardu_sockets[slot];
 468        if (socket->noprobe != 0)
 469                return -EBUSY;
 470
 471        sprintf(socket->name, "NEC VRC4173 CARDU%1d", slot+1);
 472
 473        if ((err = pci_enable_device(dev)) < 0)
 474                return err;
 475
 476        start = pci_resource_start(dev, 0);
 477        if (start == 0) {
 478                ret = -ENODEV;
 479                goto disable;
 480        }
 481
 482        len = pci_resource_len(dev, 0);
 483        if (len == 0) {
 484                ret = -ENODEV;
 485                goto disable;
 486        }
 487
 488        flags = pci_resource_flags(dev, 0);
 489        if ((flags & IORESOURCE_MEM) == 0) {
 490                ret = -EBUSY;
 491                goto disable;
 492        }
 493
 494        err = pci_request_regions(dev, socket->name);
 495        if (err < 0) {
 496                ret = err;
 497                goto disable;
 498        }
 499
 500        socket->base = ioremap(start, len);
 501        if (socket->base == NULL) {
 502                ret = -ENODEV;
 503                goto release;
 504        }
 505
 506        socket->dev = dev;
 507
 508        socket->pcmcia_socket = pcmcia_register_socket(slot, &cardu_operations, 1);
 509        if (socket->pcmcia_socket == NULL) {
 510                ret =  -ENOMEM;
 511                goto unmap;
 512        }
 513
 514        if (request_irq(dev->irq, cardu_interrupt, IRQF_SHARED, socket->name, socket) < 0) {
 515                ret = -EBUSY;
 516                goto unregister;
 517        }
 518
 519        printk(KERN_INFO "%s at %#08lx, IRQ %d\n", socket->name, start, dev->irq);
 520
 521        return 0;
 522
 523unregister:
 524        pcmcia_unregister_socket(socket->pcmcia_socket);
 525        socket->pcmcia_socket = NULL;
 526unmap:
 527        iounmap(socket->base);
 528        socket->base = NULL;
 529release:
 530        pci_release_regions(dev);
 531disable:
 532        pci_disable_device(dev);
 533        return ret;
 534}
 535
 536static int vrc4173_cardu_setup(char *options)
 537{
 538        if (options == NULL || *options == '\0')
 539                return 1;
 540
 541        if (strncmp(options, "cardu1:", 7) == 0) {
 542                options += 7;
 543                if (*options != '\0') {
 544                        if (strncmp(options, "noprobe", 7) == 0) {
 545                                cardu_sockets[CARDU1].noprobe = 1;
 546                                options += 7;
 547                        }
 548
 549                        if (*options != ',')
 550                                return 1;
 551                } else
 552                        return 1;
 553        }
 554
 555        if (strncmp(options, "cardu2:", 7) == 0) {
 556                options += 7;
 557                if ((*options != '\0') && (strncmp(options, "noprobe", 7) == 0))
 558                        cardu_sockets[CARDU2].noprobe = 1;
 559        }
 560
 561        return 1;
 562}
 563
 564__setup("vrc4173_cardu=", vrc4173_cardu_setup);
 565
 566static const struct pci_device_id vrc4173_cardu_id_table[] = {
 567        { PCI_DEVICE(PCI_VENDOR_ID_NEC, PCI_DEVICE_ID_NEC_NAPCCARD) },
 568        {0, }
 569};
 570
 571static struct pci_driver vrc4173_cardu_driver = {
 572        .name           = "NEC VRC4173 CARDU",
 573        .probe          = vrc4173_cardu_probe,
 574        .id_table       = vrc4173_cardu_id_table,
 575};
 576
 577static int vrc4173_cardu_init(void)
 578{
 579        vrc4173_cardu_slots = 0;
 580
 581        return pci_register_driver(&vrc4173_cardu_driver);
 582}
 583
 584static void vrc4173_cardu_exit(void)
 585{
 586        pci_unregister_driver(&vrc4173_cardu_driver);
 587}
 588
 589module_init(vrc4173_cardu_init);
 590module_exit(vrc4173_cardu_exit);
 591MODULE_DEVICE_TABLE(pci, vrc4173_cardu_id_table);
 592