Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  *  linux/drivers/mmc/host/wbsd.c - Winbond W83L51xD SD/MMC driver
0004  *
0005  *  Copyright (C) 2004-2007 Pierre Ossman, All Rights Reserved.
0006  *
0007  * Warning!
0008  *
0009  * Changes to the FIFO system should be done with extreme care since
0010  * the hardware is full of bugs related to the FIFO. Known issues are:
0011  *
0012  * - FIFO size field in FSR is always zero.
0013  *
0014  * - FIFO interrupts tend not to work as they should. Interrupts are
0015  *   triggered only for full/empty events, not for threshold values.
0016  *
0017  * - On APIC systems the FIFO empty interrupt is sometimes lost.
0018  */
0019 
0020 #include <linux/module.h>
0021 #include <linux/moduleparam.h>
0022 #include <linux/init.h>
0023 #include <linux/ioport.h>
0024 #include <linux/platform_device.h>
0025 #include <linux/interrupt.h>
0026 #include <linux/dma-mapping.h>
0027 #include <linux/delay.h>
0028 #include <linux/pnp.h>
0029 #include <linux/highmem.h>
0030 #include <linux/mmc/host.h>
0031 #include <linux/mmc/mmc.h>
0032 #include <linux/mmc/sd.h>
0033 #include <linux/scatterlist.h>
0034 #include <linux/slab.h>
0035 
0036 #include <asm/io.h>
0037 #include <asm/dma.h>
0038 
0039 #include "wbsd.h"
0040 
0041 #define DRIVER_NAME "wbsd"
0042 
0043 #define DBG(x...) \
0044     pr_debug(DRIVER_NAME ": " x)
0045 #define DBGF(f, x...) \
0046     pr_debug(DRIVER_NAME " [%s()]: " f, __func__ , ##x)
0047 
0048 /*
0049  * Device resources
0050  */
0051 
0052 #ifdef CONFIG_PNP
0053 
0054 static const struct pnp_device_id pnp_dev_table[] = {
0055     { "WEC0517", 0 },
0056     { "WEC0518", 0 },
0057     { "", 0 },
0058 };
0059 
0060 MODULE_DEVICE_TABLE(pnp, pnp_dev_table);
0061 
0062 #endif /* CONFIG_PNP */
0063 
0064 static const int config_ports[] = { 0x2E, 0x4E };
0065 static const int unlock_codes[] = { 0x83, 0x87 };
0066 
0067 static const int valid_ids[] = {
0068     0x7112,
0069 };
0070 
0071 #ifdef CONFIG_PNP
0072 static unsigned int param_nopnp = 0;
0073 #else
0074 static const unsigned int param_nopnp = 1;
0075 #endif
0076 static unsigned int param_io = 0x248;
0077 static unsigned int param_irq = 6;
0078 static int param_dma = 2;
0079 
0080 /*
0081  * Basic functions
0082  */
0083 
0084 static inline void wbsd_unlock_config(struct wbsd_host *host)
0085 {
0086     BUG_ON(host->config == 0);
0087 
0088     outb(host->unlock_code, host->config);
0089     outb(host->unlock_code, host->config);
0090 }
0091 
0092 static inline void wbsd_lock_config(struct wbsd_host *host)
0093 {
0094     BUG_ON(host->config == 0);
0095 
0096     outb(LOCK_CODE, host->config);
0097 }
0098 
0099 static inline void wbsd_write_config(struct wbsd_host *host, u8 reg, u8 value)
0100 {
0101     BUG_ON(host->config == 0);
0102 
0103     outb(reg, host->config);
0104     outb(value, host->config + 1);
0105 }
0106 
0107 static inline u8 wbsd_read_config(struct wbsd_host *host, u8 reg)
0108 {
0109     BUG_ON(host->config == 0);
0110 
0111     outb(reg, host->config);
0112     return inb(host->config + 1);
0113 }
0114 
0115 static inline void wbsd_write_index(struct wbsd_host *host, u8 index, u8 value)
0116 {
0117     outb(index, host->base + WBSD_IDXR);
0118     outb(value, host->base + WBSD_DATAR);
0119 }
0120 
0121 static inline u8 wbsd_read_index(struct wbsd_host *host, u8 index)
0122 {
0123     outb(index, host->base + WBSD_IDXR);
0124     return inb(host->base + WBSD_DATAR);
0125 }
0126 
0127 /*
0128  * Common routines
0129  */
0130 
0131 static void wbsd_init_device(struct wbsd_host *host)
0132 {
0133     u8 setup, ier;
0134 
0135     /*
0136      * Reset chip (SD/MMC part) and fifo.
0137      */
0138     setup = wbsd_read_index(host, WBSD_IDX_SETUP);
0139     setup |= WBSD_FIFO_RESET | WBSD_SOFT_RESET;
0140     wbsd_write_index(host, WBSD_IDX_SETUP, setup);
0141 
0142     /*
0143      * Set DAT3 to input
0144      */
0145     setup &= ~WBSD_DAT3_H;
0146     wbsd_write_index(host, WBSD_IDX_SETUP, setup);
0147     host->flags &= ~WBSD_FIGNORE_DETECT;
0148 
0149     /*
0150      * Read back default clock.
0151      */
0152     host->clk = wbsd_read_index(host, WBSD_IDX_CLK);
0153 
0154     /*
0155      * Power down port.
0156      */
0157     outb(WBSD_POWER_N, host->base + WBSD_CSR);
0158 
0159     /*
0160      * Set maximum timeout.
0161      */
0162     wbsd_write_index(host, WBSD_IDX_TAAC, 0x7F);
0163 
0164     /*
0165      * Test for card presence
0166      */
0167     if (inb(host->base + WBSD_CSR) & WBSD_CARDPRESENT)
0168         host->flags |= WBSD_FCARD_PRESENT;
0169     else
0170         host->flags &= ~WBSD_FCARD_PRESENT;
0171 
0172     /*
0173      * Enable interesting interrupts.
0174      */
0175     ier = 0;
0176     ier |= WBSD_EINT_CARD;
0177     ier |= WBSD_EINT_FIFO_THRE;
0178     ier |= WBSD_EINT_CRC;
0179     ier |= WBSD_EINT_TIMEOUT;
0180     ier |= WBSD_EINT_TC;
0181 
0182     outb(ier, host->base + WBSD_EIR);
0183 
0184     /*
0185      * Clear interrupts.
0186      */
0187     inb(host->base + WBSD_ISR);
0188 }
0189 
0190 static void wbsd_reset(struct wbsd_host *host)
0191 {
0192     u8 setup;
0193 
0194     pr_err("%s: Resetting chip\n", mmc_hostname(host->mmc));
0195 
0196     /*
0197      * Soft reset of chip (SD/MMC part).
0198      */
0199     setup = wbsd_read_index(host, WBSD_IDX_SETUP);
0200     setup |= WBSD_SOFT_RESET;
0201     wbsd_write_index(host, WBSD_IDX_SETUP, setup);
0202 }
0203 
0204 static void wbsd_request_end(struct wbsd_host *host, struct mmc_request *mrq)
0205 {
0206     unsigned long dmaflags;
0207 
0208     if (host->dma >= 0) {
0209         /*
0210          * Release ISA DMA controller.
0211          */
0212         dmaflags = claim_dma_lock();
0213         disable_dma(host->dma);
0214         clear_dma_ff(host->dma);
0215         release_dma_lock(dmaflags);
0216 
0217         /*
0218          * Disable DMA on host.
0219          */
0220         wbsd_write_index(host, WBSD_IDX_DMA, 0);
0221     }
0222 
0223     host->mrq = NULL;
0224 
0225     /*
0226      * MMC layer might call back into the driver so first unlock.
0227      */
0228     spin_unlock(&host->lock);
0229     mmc_request_done(host->mmc, mrq);
0230     spin_lock(&host->lock);
0231 }
0232 
0233 /*
0234  * Scatter/gather functions
0235  */
0236 
0237 static inline void wbsd_init_sg(struct wbsd_host *host, struct mmc_data *data)
0238 {
0239     /*
0240      * Get info. about SG list from data structure.
0241      */
0242     host->cur_sg = data->sg;
0243     host->num_sg = data->sg_len;
0244 
0245     host->offset = 0;
0246     host->remain = host->cur_sg->length;
0247 }
0248 
0249 static inline int wbsd_next_sg(struct wbsd_host *host)
0250 {
0251     /*
0252      * Skip to next SG entry.
0253      */
0254     host->cur_sg++;
0255     host->num_sg--;
0256 
0257     /*
0258      * Any entries left?
0259      */
0260     if (host->num_sg > 0) {
0261         host->offset = 0;
0262         host->remain = host->cur_sg->length;
0263     }
0264 
0265     return host->num_sg;
0266 }
0267 
0268 static inline char *wbsd_map_sg(struct wbsd_host *host)
0269 {
0270     return kmap_atomic(sg_page(host->cur_sg)) + host->cur_sg->offset;
0271 }
0272 
0273 static inline void wbsd_sg_to_dma(struct wbsd_host *host, struct mmc_data *data)
0274 {
0275     size_t len = 0;
0276     int i;
0277 
0278     for (i = 0; i < data->sg_len; i++)
0279         len += data->sg[i].length;
0280     sg_copy_to_buffer(data->sg, data->sg_len, host->dma_buffer, len);
0281 }
0282 
0283 static inline void wbsd_dma_to_sg(struct wbsd_host *host, struct mmc_data *data)
0284 {
0285     size_t len = 0;
0286     int i;
0287 
0288     for (i = 0; i < data->sg_len; i++)
0289         len += data->sg[i].length;
0290     sg_copy_from_buffer(data->sg, data->sg_len, host->dma_buffer, len);
0291 }
0292 
0293 /*
0294  * Command handling
0295  */
0296 
0297 static inline void wbsd_get_short_reply(struct wbsd_host *host,
0298                     struct mmc_command *cmd)
0299 {
0300     /*
0301      * Correct response type?
0302      */
0303     if (wbsd_read_index(host, WBSD_IDX_RSPLEN) != WBSD_RSP_SHORT) {
0304         cmd->error = -EILSEQ;
0305         return;
0306     }
0307 
0308     cmd->resp[0]  = wbsd_read_index(host, WBSD_IDX_RESP12) << 24;
0309     cmd->resp[0] |= wbsd_read_index(host, WBSD_IDX_RESP13) << 16;
0310     cmd->resp[0] |= wbsd_read_index(host, WBSD_IDX_RESP14) << 8;
0311     cmd->resp[0] |= wbsd_read_index(host, WBSD_IDX_RESP15) << 0;
0312     cmd->resp[1]  = wbsd_read_index(host, WBSD_IDX_RESP16) << 24;
0313 }
0314 
0315 static inline void wbsd_get_long_reply(struct wbsd_host *host,
0316     struct mmc_command *cmd)
0317 {
0318     int i;
0319 
0320     /*
0321      * Correct response type?
0322      */
0323     if (wbsd_read_index(host, WBSD_IDX_RSPLEN) != WBSD_RSP_LONG) {
0324         cmd->error = -EILSEQ;
0325         return;
0326     }
0327 
0328     for (i = 0; i < 4; i++) {
0329         cmd->resp[i] =
0330             wbsd_read_index(host, WBSD_IDX_RESP1 + i * 4) << 24;
0331         cmd->resp[i] |=
0332             wbsd_read_index(host, WBSD_IDX_RESP2 + i * 4) << 16;
0333         cmd->resp[i] |=
0334             wbsd_read_index(host, WBSD_IDX_RESP3 + i * 4) << 8;
0335         cmd->resp[i] |=
0336             wbsd_read_index(host, WBSD_IDX_RESP4 + i * 4) << 0;
0337     }
0338 }
0339 
0340 static void wbsd_send_command(struct wbsd_host *host, struct mmc_command *cmd)
0341 {
0342     int i;
0343     u8 status, isr;
0344 
0345     /*
0346      * Clear accumulated ISR. The interrupt routine
0347      * will fill this one with events that occur during
0348      * transfer.
0349      */
0350     host->isr = 0;
0351 
0352     /*
0353      * Send the command (CRC calculated by host).
0354      */
0355     outb(cmd->opcode, host->base + WBSD_CMDR);
0356     for (i = 3; i >= 0; i--)
0357         outb((cmd->arg >> (i * 8)) & 0xff, host->base + WBSD_CMDR);
0358 
0359     cmd->error = 0;
0360 
0361     /*
0362      * Wait for the request to complete.
0363      */
0364     do {
0365         status = wbsd_read_index(host, WBSD_IDX_STATUS);
0366     } while (status & WBSD_CARDTRAFFIC);
0367 
0368     /*
0369      * Do we expect a reply?
0370      */
0371     if (cmd->flags & MMC_RSP_PRESENT) {
0372         /*
0373          * Read back status.
0374          */
0375         isr = host->isr;
0376 
0377         /* Card removed? */
0378         if (isr & WBSD_INT_CARD)
0379             cmd->error = -ENOMEDIUM;
0380         /* Timeout? */
0381         else if (isr & WBSD_INT_TIMEOUT)
0382             cmd->error = -ETIMEDOUT;
0383         /* CRC? */
0384         else if ((cmd->flags & MMC_RSP_CRC) && (isr & WBSD_INT_CRC))
0385             cmd->error = -EILSEQ;
0386         /* All ok */
0387         else {
0388             if (cmd->flags & MMC_RSP_136)
0389                 wbsd_get_long_reply(host, cmd);
0390             else
0391                 wbsd_get_short_reply(host, cmd);
0392         }
0393     }
0394 }
0395 
0396 /*
0397  * Data functions
0398  */
0399 
0400 static void wbsd_empty_fifo(struct wbsd_host *host)
0401 {
0402     struct mmc_data *data = host->mrq->cmd->data;
0403     char *buffer;
0404     int i, idx, fsr, fifo;
0405 
0406     /*
0407      * Handle excessive data.
0408      */
0409     if (host->num_sg == 0)
0410         return;
0411 
0412     buffer = wbsd_map_sg(host) + host->offset;
0413     idx = 0;
0414 
0415     /*
0416      * Drain the fifo. This has a tendency to loop longer
0417      * than the FIFO length (usually one block).
0418      */
0419     while (!((fsr = inb(host->base + WBSD_FSR)) & WBSD_FIFO_EMPTY)) {
0420         /*
0421          * The size field in the FSR is broken so we have to
0422          * do some guessing.
0423          */
0424         if (fsr & WBSD_FIFO_FULL)
0425             fifo = 16;
0426         else if (fsr & WBSD_FIFO_FUTHRE)
0427             fifo = 8;
0428         else
0429             fifo = 1;
0430 
0431         for (i = 0; i < fifo; i++) {
0432             buffer[idx++] = inb(host->base + WBSD_DFR);
0433             host->offset++;
0434             host->remain--;
0435 
0436             data->bytes_xfered++;
0437 
0438             /*
0439              * End of scatter list entry?
0440              */
0441             if (host->remain == 0) {
0442                 kunmap_atomic(buffer);
0443                 /*
0444                  * Get next entry. Check if last.
0445                  */
0446                 if (!wbsd_next_sg(host))
0447                     return;
0448 
0449                 buffer = wbsd_map_sg(host);
0450                 idx = 0;
0451             }
0452         }
0453     }
0454     kunmap_atomic(buffer);
0455 
0456     /*
0457      * This is a very dirty hack to solve a
0458      * hardware problem. The chip doesn't trigger
0459      * FIFO threshold interrupts properly.
0460      */
0461     if ((data->blocks * data->blksz - data->bytes_xfered) < 16)
0462         tasklet_schedule(&host->fifo_tasklet);
0463 }
0464 
0465 static void wbsd_fill_fifo(struct wbsd_host *host)
0466 {
0467     struct mmc_data *data = host->mrq->cmd->data;
0468     char *buffer;
0469     int i, idx, fsr, fifo;
0470 
0471     /*
0472      * Check that we aren't being called after the
0473      * entire buffer has been transferred.
0474      */
0475     if (host->num_sg == 0)
0476         return;
0477 
0478     buffer = wbsd_map_sg(host) + host->offset;
0479     idx = 0;
0480 
0481     /*
0482      * Fill the fifo. This has a tendency to loop longer
0483      * than the FIFO length (usually one block).
0484      */
0485     while (!((fsr = inb(host->base + WBSD_FSR)) & WBSD_FIFO_FULL)) {
0486         /*
0487          * The size field in the FSR is broken so we have to
0488          * do some guessing.
0489          */
0490         if (fsr & WBSD_FIFO_EMPTY)
0491             fifo = 0;
0492         else if (fsr & WBSD_FIFO_EMTHRE)
0493             fifo = 8;
0494         else
0495             fifo = 15;
0496 
0497         for (i = 16; i > fifo; i--) {
0498             outb(buffer[idx], host->base + WBSD_DFR);
0499             host->offset++;
0500             host->remain--;
0501 
0502             data->bytes_xfered++;
0503 
0504             /*
0505              * End of scatter list entry?
0506              */
0507             if (host->remain == 0) {
0508                 kunmap_atomic(buffer);
0509                 /*
0510                  * Get next entry. Check if last.
0511                  */
0512                 if (!wbsd_next_sg(host))
0513                     return;
0514 
0515                 buffer = wbsd_map_sg(host);
0516                 idx = 0;
0517             }
0518         }
0519     }
0520     kunmap_atomic(buffer);
0521 
0522     /*
0523      * The controller stops sending interrupts for
0524      * 'FIFO empty' under certain conditions. So we
0525      * need to be a bit more pro-active.
0526      */
0527     tasklet_schedule(&host->fifo_tasklet);
0528 }
0529 
0530 static void wbsd_prepare_data(struct wbsd_host *host, struct mmc_data *data)
0531 {
0532     u16 blksize;
0533     u8 setup;
0534     unsigned long dmaflags;
0535     unsigned int size;
0536 
0537     /*
0538      * Calculate size.
0539      */
0540     size = data->blocks * data->blksz;
0541 
0542     /*
0543      * Check timeout values for overflow.
0544      * (Yes, some cards cause this value to overflow).
0545      */
0546     if (data->timeout_ns > 127000000)
0547         wbsd_write_index(host, WBSD_IDX_TAAC, 127);
0548     else {
0549         wbsd_write_index(host, WBSD_IDX_TAAC,
0550             data->timeout_ns / 1000000);
0551     }
0552 
0553     if (data->timeout_clks > 255)
0554         wbsd_write_index(host, WBSD_IDX_NSAC, 255);
0555     else
0556         wbsd_write_index(host, WBSD_IDX_NSAC, data->timeout_clks);
0557 
0558     /*
0559      * Inform the chip of how large blocks will be
0560      * sent. It needs this to determine when to
0561      * calculate CRC.
0562      *
0563      * Space for CRC must be included in the size.
0564      * Two bytes are needed for each data line.
0565      */
0566     if (host->bus_width == MMC_BUS_WIDTH_1) {
0567         blksize = data->blksz + 2;
0568 
0569         wbsd_write_index(host, WBSD_IDX_PBSMSB, (blksize >> 4) & 0xF0);
0570         wbsd_write_index(host, WBSD_IDX_PBSLSB, blksize & 0xFF);
0571     } else if (host->bus_width == MMC_BUS_WIDTH_4) {
0572         blksize = data->blksz + 2 * 4;
0573 
0574         wbsd_write_index(host, WBSD_IDX_PBSMSB,
0575             ((blksize >> 4) & 0xF0) | WBSD_DATA_WIDTH);
0576         wbsd_write_index(host, WBSD_IDX_PBSLSB, blksize & 0xFF);
0577     } else {
0578         data->error = -EINVAL;
0579         return;
0580     }
0581 
0582     /*
0583      * Clear the FIFO. This is needed even for DMA
0584      * transfers since the chip still uses the FIFO
0585      * internally.
0586      */
0587     setup = wbsd_read_index(host, WBSD_IDX_SETUP);
0588     setup |= WBSD_FIFO_RESET;
0589     wbsd_write_index(host, WBSD_IDX_SETUP, setup);
0590 
0591     /*
0592      * DMA transfer?
0593      */
0594     if (host->dma >= 0) {
0595         /*
0596          * The buffer for DMA is only 64 kB.
0597          */
0598         BUG_ON(size > 0x10000);
0599         if (size > 0x10000) {
0600             data->error = -EINVAL;
0601             return;
0602         }
0603 
0604         /*
0605          * Transfer data from the SG list to
0606          * the DMA buffer.
0607          */
0608         if (data->flags & MMC_DATA_WRITE)
0609             wbsd_sg_to_dma(host, data);
0610 
0611         /*
0612          * Initialise the ISA DMA controller.
0613          */
0614         dmaflags = claim_dma_lock();
0615         disable_dma(host->dma);
0616         clear_dma_ff(host->dma);
0617         if (data->flags & MMC_DATA_READ)
0618             set_dma_mode(host->dma, DMA_MODE_READ & ~0x40);
0619         else
0620             set_dma_mode(host->dma, DMA_MODE_WRITE & ~0x40);
0621         set_dma_addr(host->dma, host->dma_addr);
0622         set_dma_count(host->dma, size);
0623 
0624         enable_dma(host->dma);
0625         release_dma_lock(dmaflags);
0626 
0627         /*
0628          * Enable DMA on the host.
0629          */
0630         wbsd_write_index(host, WBSD_IDX_DMA, WBSD_DMA_ENABLE);
0631     } else {
0632         /*
0633          * This flag is used to keep printk
0634          * output to a minimum.
0635          */
0636         host->firsterr = 1;
0637 
0638         /*
0639          * Initialise the SG list.
0640          */
0641         wbsd_init_sg(host, data);
0642 
0643         /*
0644          * Turn off DMA.
0645          */
0646         wbsd_write_index(host, WBSD_IDX_DMA, 0);
0647 
0648         /*
0649          * Set up FIFO threshold levels (and fill
0650          * buffer if doing a write).
0651          */
0652         if (data->flags & MMC_DATA_READ) {
0653             wbsd_write_index(host, WBSD_IDX_FIFOEN,
0654                 WBSD_FIFOEN_FULL | 8);
0655         } else {
0656             wbsd_write_index(host, WBSD_IDX_FIFOEN,
0657                 WBSD_FIFOEN_EMPTY | 8);
0658             wbsd_fill_fifo(host);
0659         }
0660     }
0661 
0662     data->error = 0;
0663 }
0664 
0665 static void wbsd_finish_data(struct wbsd_host *host, struct mmc_data *data)
0666 {
0667     unsigned long dmaflags;
0668     int count;
0669     u8 status;
0670 
0671     WARN_ON(host->mrq == NULL);
0672 
0673     /*
0674      * Send a stop command if needed.
0675      */
0676     if (data->stop)
0677         wbsd_send_command(host, data->stop);
0678 
0679     /*
0680      * Wait for the controller to leave data
0681      * transfer state.
0682      */
0683     do {
0684         status = wbsd_read_index(host, WBSD_IDX_STATUS);
0685     } while (status & (WBSD_BLOCK_READ | WBSD_BLOCK_WRITE));
0686 
0687     /*
0688      * DMA transfer?
0689      */
0690     if (host->dma >= 0) {
0691         /*
0692          * Disable DMA on the host.
0693          */
0694         wbsd_write_index(host, WBSD_IDX_DMA, 0);
0695 
0696         /*
0697          * Turn of ISA DMA controller.
0698          */
0699         dmaflags = claim_dma_lock();
0700         disable_dma(host->dma);
0701         clear_dma_ff(host->dma);
0702         count = get_dma_residue(host->dma);
0703         release_dma_lock(dmaflags);
0704 
0705         data->bytes_xfered = host->mrq->data->blocks *
0706             host->mrq->data->blksz - count;
0707         data->bytes_xfered -= data->bytes_xfered % data->blksz;
0708 
0709         /*
0710          * Any leftover data?
0711          */
0712         if (count) {
0713             pr_err("%s: Incomplete DMA transfer. "
0714                 "%d bytes left.\n",
0715                 mmc_hostname(host->mmc), count);
0716 
0717             if (!data->error)
0718                 data->error = -EIO;
0719         } else {
0720             /*
0721              * Transfer data from DMA buffer to
0722              * SG list.
0723              */
0724             if (data->flags & MMC_DATA_READ)
0725                 wbsd_dma_to_sg(host, data);
0726         }
0727 
0728         if (data->error) {
0729             if (data->bytes_xfered)
0730                 data->bytes_xfered -= data->blksz;
0731         }
0732     }
0733 
0734     wbsd_request_end(host, host->mrq);
0735 }
0736 
0737 /*****************************************************************************\
0738  *                                                                           *
0739  * MMC layer callbacks                                                       *
0740  *                                                                           *
0741 \*****************************************************************************/
0742 
0743 static void wbsd_request(struct mmc_host *mmc, struct mmc_request *mrq)
0744 {
0745     struct wbsd_host *host = mmc_priv(mmc);
0746     struct mmc_command *cmd;
0747 
0748     /*
0749      * Disable tasklets to avoid a deadlock.
0750      */
0751     spin_lock_bh(&host->lock);
0752 
0753     BUG_ON(host->mrq != NULL);
0754 
0755     cmd = mrq->cmd;
0756 
0757     host->mrq = mrq;
0758 
0759     /*
0760      * Check that there is actually a card in the slot.
0761      */
0762     if (!(host->flags & WBSD_FCARD_PRESENT)) {
0763         cmd->error = -ENOMEDIUM;
0764         goto done;
0765     }
0766 
0767     if (cmd->data) {
0768         /*
0769          * The hardware is so delightfully stupid that it has a list
0770          * of "data" commands. If a command isn't on this list, it'll
0771          * just go back to the idle state and won't send any data
0772          * interrupts.
0773          */
0774         switch (cmd->opcode) {
0775         case SD_SWITCH_VOLTAGE:
0776         case MMC_READ_SINGLE_BLOCK:
0777         case MMC_READ_MULTIPLE_BLOCK:
0778         case MMC_WRITE_DAT_UNTIL_STOP:
0779         case MMC_WRITE_BLOCK:
0780         case MMC_WRITE_MULTIPLE_BLOCK:
0781         case MMC_PROGRAM_CID:
0782         case MMC_PROGRAM_CSD:
0783         case MMC_SEND_WRITE_PROT:
0784         case MMC_LOCK_UNLOCK:
0785         case MMC_GEN_CMD:
0786             break;
0787 
0788         /* ACMDs. We don't keep track of state, so we just treat them
0789          * like any other command. */
0790         case SD_APP_SEND_SCR:
0791             break;
0792 
0793         default:
0794             pr_warn("%s: Data command %d is not supported by this controller\n",
0795                 mmc_hostname(host->mmc), cmd->opcode);
0796             cmd->error = -EINVAL;
0797 
0798             goto done;
0799         }
0800     }
0801 
0802     /*
0803      * Does the request include data?
0804      */
0805     if (cmd->data) {
0806         wbsd_prepare_data(host, cmd->data);
0807 
0808         if (cmd->data->error)
0809             goto done;
0810     }
0811 
0812     wbsd_send_command(host, cmd);
0813 
0814     /*
0815      * If this is a data transfer the request
0816      * will be finished after the data has
0817      * transferred.
0818      */
0819     if (cmd->data && !cmd->error) {
0820         /*
0821          * Dirty fix for hardware bug.
0822          */
0823         if (host->dma == -1)
0824             tasklet_schedule(&host->fifo_tasklet);
0825 
0826         spin_unlock_bh(&host->lock);
0827 
0828         return;
0829     }
0830 
0831 done:
0832     wbsd_request_end(host, mrq);
0833 
0834     spin_unlock_bh(&host->lock);
0835 }
0836 
0837 static void wbsd_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
0838 {
0839     struct wbsd_host *host = mmc_priv(mmc);
0840     u8 clk, setup, pwr;
0841 
0842     spin_lock_bh(&host->lock);
0843 
0844     /*
0845      * Reset the chip on each power off.
0846      * Should clear out any weird states.
0847      */
0848     if (ios->power_mode == MMC_POWER_OFF)
0849         wbsd_init_device(host);
0850 
0851     if (ios->clock >= 24000000)
0852         clk = WBSD_CLK_24M;
0853     else if (ios->clock >= 16000000)
0854         clk = WBSD_CLK_16M;
0855     else if (ios->clock >= 12000000)
0856         clk = WBSD_CLK_12M;
0857     else
0858         clk = WBSD_CLK_375K;
0859 
0860     /*
0861      * Only write to the clock register when
0862      * there is an actual change.
0863      */
0864     if (clk != host->clk) {
0865         wbsd_write_index(host, WBSD_IDX_CLK, clk);
0866         host->clk = clk;
0867     }
0868 
0869     /*
0870      * Power up card.
0871      */
0872     if (ios->power_mode != MMC_POWER_OFF) {
0873         pwr = inb(host->base + WBSD_CSR);
0874         pwr &= ~WBSD_POWER_N;
0875         outb(pwr, host->base + WBSD_CSR);
0876     }
0877 
0878     /*
0879      * MMC cards need to have pin 1 high during init.
0880      * It wreaks havoc with the card detection though so
0881      * that needs to be disabled.
0882      */
0883     setup = wbsd_read_index(host, WBSD_IDX_SETUP);
0884     if (ios->chip_select == MMC_CS_HIGH) {
0885         BUG_ON(ios->bus_width != MMC_BUS_WIDTH_1);
0886         setup |= WBSD_DAT3_H;
0887         host->flags |= WBSD_FIGNORE_DETECT;
0888     } else {
0889         if (setup & WBSD_DAT3_H) {
0890             setup &= ~WBSD_DAT3_H;
0891 
0892             /*
0893              * We cannot resume card detection immediately
0894              * because of capacitance and delays in the chip.
0895              */
0896             mod_timer(&host->ignore_timer, jiffies + HZ / 100);
0897         }
0898     }
0899     wbsd_write_index(host, WBSD_IDX_SETUP, setup);
0900 
0901     /*
0902      * Store bus width for later. Will be used when
0903      * setting up the data transfer.
0904      */
0905     host->bus_width = ios->bus_width;
0906 
0907     spin_unlock_bh(&host->lock);
0908 }
0909 
0910 static int wbsd_get_ro(struct mmc_host *mmc)
0911 {
0912     struct wbsd_host *host = mmc_priv(mmc);
0913     u8 csr;
0914 
0915     spin_lock_bh(&host->lock);
0916 
0917     csr = inb(host->base + WBSD_CSR);
0918     csr |= WBSD_MSLED;
0919     outb(csr, host->base + WBSD_CSR);
0920 
0921     mdelay(1);
0922 
0923     csr = inb(host->base + WBSD_CSR);
0924     csr &= ~WBSD_MSLED;
0925     outb(csr, host->base + WBSD_CSR);
0926 
0927     spin_unlock_bh(&host->lock);
0928 
0929     return !!(csr & WBSD_WRPT);
0930 }
0931 
0932 static const struct mmc_host_ops wbsd_ops = {
0933     .request    = wbsd_request,
0934     .set_ios    = wbsd_set_ios,
0935     .get_ro     = wbsd_get_ro,
0936 };
0937 
0938 /*****************************************************************************\
0939  *                                                                           *
0940  * Interrupt handling                                                        *
0941  *                                                                           *
0942 \*****************************************************************************/
0943 
0944 /*
0945  * Helper function to reset detection ignore
0946  */
0947 
0948 static void wbsd_reset_ignore(struct timer_list *t)
0949 {
0950     struct wbsd_host *host = from_timer(host, t, ignore_timer);
0951 
0952     BUG_ON(host == NULL);
0953 
0954     DBG("Resetting card detection ignore\n");
0955 
0956     spin_lock_bh(&host->lock);
0957 
0958     host->flags &= ~WBSD_FIGNORE_DETECT;
0959 
0960     /*
0961      * Card status might have changed during the
0962      * blackout.
0963      */
0964     tasklet_schedule(&host->card_tasklet);
0965 
0966     spin_unlock_bh(&host->lock);
0967 }
0968 
0969 /*
0970  * Tasklets
0971  */
0972 
0973 static inline struct mmc_data *wbsd_get_data(struct wbsd_host *host)
0974 {
0975     WARN_ON(!host->mrq);
0976     if (!host->mrq)
0977         return NULL;
0978 
0979     WARN_ON(!host->mrq->cmd);
0980     if (!host->mrq->cmd)
0981         return NULL;
0982 
0983     WARN_ON(!host->mrq->cmd->data);
0984     if (!host->mrq->cmd->data)
0985         return NULL;
0986 
0987     return host->mrq->cmd->data;
0988 }
0989 
0990 static void wbsd_tasklet_card(struct tasklet_struct *t)
0991 {
0992     struct wbsd_host *host = from_tasklet(host, t, card_tasklet);
0993     u8 csr;
0994     int delay = -1;
0995 
0996     spin_lock(&host->lock);
0997 
0998     if (host->flags & WBSD_FIGNORE_DETECT) {
0999         spin_unlock(&host->lock);
1000         return;
1001     }
1002 
1003     csr = inb(host->base + WBSD_CSR);
1004     WARN_ON(csr == 0xff);
1005 
1006     if (csr & WBSD_CARDPRESENT) {
1007         if (!(host->flags & WBSD_FCARD_PRESENT)) {
1008             DBG("Card inserted\n");
1009             host->flags |= WBSD_FCARD_PRESENT;
1010 
1011             delay = 500;
1012         }
1013     } else if (host->flags & WBSD_FCARD_PRESENT) {
1014         DBG("Card removed\n");
1015         host->flags &= ~WBSD_FCARD_PRESENT;
1016 
1017         if (host->mrq) {
1018             pr_err("%s: Card removed during transfer!\n",
1019                 mmc_hostname(host->mmc));
1020             wbsd_reset(host);
1021 
1022             host->mrq->cmd->error = -ENOMEDIUM;
1023             tasklet_schedule(&host->finish_tasklet);
1024         }
1025 
1026         delay = 0;
1027     }
1028 
1029     /*
1030      * Unlock first since we might get a call back.
1031      */
1032 
1033     spin_unlock(&host->lock);
1034 
1035     if (delay != -1)
1036         mmc_detect_change(host->mmc, msecs_to_jiffies(delay));
1037 }
1038 
1039 static void wbsd_tasklet_fifo(struct tasklet_struct *t)
1040 {
1041     struct wbsd_host *host = from_tasklet(host, t, fifo_tasklet);
1042     struct mmc_data *data;
1043 
1044     spin_lock(&host->lock);
1045 
1046     if (!host->mrq)
1047         goto end;
1048 
1049     data = wbsd_get_data(host);
1050     if (!data)
1051         goto end;
1052 
1053     if (data->flags & MMC_DATA_WRITE)
1054         wbsd_fill_fifo(host);
1055     else
1056         wbsd_empty_fifo(host);
1057 
1058     /*
1059      * Done?
1060      */
1061     if (host->num_sg == 0) {
1062         wbsd_write_index(host, WBSD_IDX_FIFOEN, 0);
1063         tasklet_schedule(&host->finish_tasklet);
1064     }
1065 
1066 end:
1067     spin_unlock(&host->lock);
1068 }
1069 
1070 static void wbsd_tasklet_crc(struct tasklet_struct *t)
1071 {
1072     struct wbsd_host *host = from_tasklet(host, t, crc_tasklet);
1073     struct mmc_data *data;
1074 
1075     spin_lock(&host->lock);
1076 
1077     if (!host->mrq)
1078         goto end;
1079 
1080     data = wbsd_get_data(host);
1081     if (!data)
1082         goto end;
1083 
1084     DBGF("CRC error\n");
1085 
1086     data->error = -EILSEQ;
1087 
1088     tasklet_schedule(&host->finish_tasklet);
1089 
1090 end:
1091     spin_unlock(&host->lock);
1092 }
1093 
1094 static void wbsd_tasklet_timeout(struct tasklet_struct *t)
1095 {
1096     struct wbsd_host *host = from_tasklet(host, t, timeout_tasklet);
1097     struct mmc_data *data;
1098 
1099     spin_lock(&host->lock);
1100 
1101     if (!host->mrq)
1102         goto end;
1103 
1104     data = wbsd_get_data(host);
1105     if (!data)
1106         goto end;
1107 
1108     DBGF("Timeout\n");
1109 
1110     data->error = -ETIMEDOUT;
1111 
1112     tasklet_schedule(&host->finish_tasklet);
1113 
1114 end:
1115     spin_unlock(&host->lock);
1116 }
1117 
1118 static void wbsd_tasklet_finish(struct tasklet_struct *t)
1119 {
1120     struct wbsd_host *host = from_tasklet(host, t, finish_tasklet);
1121     struct mmc_data *data;
1122 
1123     spin_lock(&host->lock);
1124 
1125     WARN_ON(!host->mrq);
1126     if (!host->mrq)
1127         goto end;
1128 
1129     data = wbsd_get_data(host);
1130     if (!data)
1131         goto end;
1132 
1133     wbsd_finish_data(host, data);
1134 
1135 end:
1136     spin_unlock(&host->lock);
1137 }
1138 
1139 /*
1140  * Interrupt handling
1141  */
1142 
1143 static irqreturn_t wbsd_irq(int irq, void *dev_id)
1144 {
1145     struct wbsd_host *host = dev_id;
1146     int isr;
1147 
1148     isr = inb(host->base + WBSD_ISR);
1149 
1150     /*
1151      * Was it actually our hardware that caused the interrupt?
1152      */
1153     if (isr == 0xff || isr == 0x00)
1154         return IRQ_NONE;
1155 
1156     host->isr |= isr;
1157 
1158     /*
1159      * Schedule tasklets as needed.
1160      */
1161     if (isr & WBSD_INT_CARD)
1162         tasklet_schedule(&host->card_tasklet);
1163     if (isr & WBSD_INT_FIFO_THRE)
1164         tasklet_schedule(&host->fifo_tasklet);
1165     if (isr & WBSD_INT_CRC)
1166         tasklet_hi_schedule(&host->crc_tasklet);
1167     if (isr & WBSD_INT_TIMEOUT)
1168         tasklet_hi_schedule(&host->timeout_tasklet);
1169     if (isr & WBSD_INT_TC)
1170         tasklet_schedule(&host->finish_tasklet);
1171 
1172     return IRQ_HANDLED;
1173 }
1174 
1175 /*****************************************************************************\
1176  *                                                                           *
1177  * Device initialisation and shutdown                                        *
1178  *                                                                           *
1179 \*****************************************************************************/
1180 
1181 /*
1182  * Allocate/free MMC structure.
1183  */
1184 
1185 static int wbsd_alloc_mmc(struct device *dev)
1186 {
1187     struct mmc_host *mmc;
1188     struct wbsd_host *host;
1189 
1190     /*
1191      * Allocate MMC structure.
1192      */
1193     mmc = mmc_alloc_host(sizeof(struct wbsd_host), dev);
1194     if (!mmc)
1195         return -ENOMEM;
1196 
1197     host = mmc_priv(mmc);
1198     host->mmc = mmc;
1199 
1200     host->dma = -1;
1201 
1202     /*
1203      * Set host parameters.
1204      */
1205     mmc->ops = &wbsd_ops;
1206     mmc->f_min = 375000;
1207     mmc->f_max = 24000000;
1208     mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
1209     mmc->caps = MMC_CAP_4_BIT_DATA;
1210 
1211     spin_lock_init(&host->lock);
1212 
1213     /*
1214      * Set up timers
1215      */
1216     timer_setup(&host->ignore_timer, wbsd_reset_ignore, 0);
1217 
1218     /*
1219      * Maximum number of segments. Worst case is one sector per segment
1220      * so this will be 64kB/512.
1221      */
1222     mmc->max_segs = 128;
1223 
1224     /*
1225      * Maximum request size. Also limited by 64KiB buffer.
1226      */
1227     mmc->max_req_size = 65536;
1228 
1229     /*
1230      * Maximum segment size. Could be one segment with the maximum number
1231      * of bytes.
1232      */
1233     mmc->max_seg_size = mmc->max_req_size;
1234 
1235     /*
1236      * Maximum block size. We have 12 bits (= 4095) but have to subtract
1237      * space for CRC. So the maximum is 4095 - 4*2 = 4087.
1238      */
1239     mmc->max_blk_size = 4087;
1240 
1241     /*
1242      * Maximum block count. There is no real limit so the maximum
1243      * request size will be the only restriction.
1244      */
1245     mmc->max_blk_count = mmc->max_req_size;
1246 
1247     dev_set_drvdata(dev, mmc);
1248 
1249     return 0;
1250 }
1251 
1252 static void wbsd_free_mmc(struct device *dev)
1253 {
1254     struct mmc_host *mmc;
1255     struct wbsd_host *host;
1256 
1257     mmc = dev_get_drvdata(dev);
1258     if (!mmc)
1259         return;
1260 
1261     host = mmc_priv(mmc);
1262     BUG_ON(host == NULL);
1263 
1264     del_timer_sync(&host->ignore_timer);
1265 
1266     mmc_free_host(mmc);
1267 
1268     dev_set_drvdata(dev, NULL);
1269 }
1270 
1271 /*
1272  * Scan for known chip id:s
1273  */
1274 
1275 static int wbsd_scan(struct wbsd_host *host)
1276 {
1277     int i, j, k;
1278     int id;
1279 
1280     /*
1281      * Iterate through all ports, all codes to
1282      * find hardware that is in our known list.
1283      */
1284     for (i = 0; i < ARRAY_SIZE(config_ports); i++) {
1285         if (!request_region(config_ports[i], 2, DRIVER_NAME))
1286             continue;
1287 
1288         for (j = 0; j < ARRAY_SIZE(unlock_codes); j++) {
1289             id = 0xFFFF;
1290 
1291             host->config = config_ports[i];
1292             host->unlock_code = unlock_codes[j];
1293 
1294             wbsd_unlock_config(host);
1295 
1296             outb(WBSD_CONF_ID_HI, config_ports[i]);
1297             id = inb(config_ports[i] + 1) << 8;
1298 
1299             outb(WBSD_CONF_ID_LO, config_ports[i]);
1300             id |= inb(config_ports[i] + 1);
1301 
1302             wbsd_lock_config(host);
1303 
1304             for (k = 0; k < ARRAY_SIZE(valid_ids); k++) {
1305                 if (id == valid_ids[k]) {
1306                     host->chip_id = id;
1307 
1308                     return 0;
1309                 }
1310             }
1311 
1312             if (id != 0xFFFF) {
1313                 DBG("Unknown hardware (id %x) found at %x\n",
1314                     id, config_ports[i]);
1315             }
1316         }
1317 
1318         release_region(config_ports[i], 2);
1319     }
1320 
1321     host->config = 0;
1322     host->unlock_code = 0;
1323 
1324     return -ENODEV;
1325 }
1326 
1327 /*
1328  * Allocate/free io port ranges
1329  */
1330 
1331 static int wbsd_request_region(struct wbsd_host *host, int base)
1332 {
1333     if (base & 0x7)
1334         return -EINVAL;
1335 
1336     if (!request_region(base, 8, DRIVER_NAME))
1337         return -EIO;
1338 
1339     host->base = base;
1340 
1341     return 0;
1342 }
1343 
1344 static void wbsd_release_regions(struct wbsd_host *host)
1345 {
1346     if (host->base)
1347         release_region(host->base, 8);
1348 
1349     host->base = 0;
1350 
1351     if (host->config)
1352         release_region(host->config, 2);
1353 
1354     host->config = 0;
1355 }
1356 
1357 /*
1358  * Allocate/free DMA port and buffer
1359  */
1360 
1361 static void wbsd_request_dma(struct wbsd_host *host, int dma)
1362 {
1363     if (dma < 0)
1364         return;
1365 
1366     if (request_dma(dma, DRIVER_NAME))
1367         goto err;
1368 
1369     /*
1370      * We need to allocate a special buffer in
1371      * order for ISA to be able to DMA to it.
1372      */
1373     host->dma_buffer = kmalloc(WBSD_DMA_SIZE,
1374         GFP_NOIO | GFP_DMA | __GFP_RETRY_MAYFAIL | __GFP_NOWARN);
1375     if (!host->dma_buffer)
1376         goto free;
1377 
1378     /*
1379      * Translate the address to a physical address.
1380      */
1381     host->dma_addr = dma_map_single(mmc_dev(host->mmc), host->dma_buffer,
1382         WBSD_DMA_SIZE, DMA_BIDIRECTIONAL);
1383     if (dma_mapping_error(mmc_dev(host->mmc), host->dma_addr))
1384         goto kfree;
1385 
1386     /*
1387      * ISA DMA must be aligned on a 64k basis.
1388      */
1389     if ((host->dma_addr & 0xffff) != 0)
1390         goto unmap;
1391     /*
1392      * ISA cannot access memory above 16 MB.
1393      */
1394     else if (host->dma_addr >= 0x1000000)
1395         goto unmap;
1396 
1397     host->dma = dma;
1398 
1399     return;
1400 
1401 unmap:
1402     /*
1403      * If we've gotten here then there is some kind of alignment bug
1404      */
1405     BUG_ON(1);
1406 
1407     dma_unmap_single(mmc_dev(host->mmc), host->dma_addr,
1408         WBSD_DMA_SIZE, DMA_BIDIRECTIONAL);
1409     host->dma_addr = 0;
1410 
1411 kfree:
1412     kfree(host->dma_buffer);
1413     host->dma_buffer = NULL;
1414 
1415 free:
1416     free_dma(dma);
1417 
1418 err:
1419     pr_warn(DRIVER_NAME ": Unable to allocate DMA %d - falling back on FIFO\n",
1420         dma);
1421 }
1422 
1423 static void wbsd_release_dma(struct wbsd_host *host)
1424 {
1425     /*
1426      * host->dma_addr is valid here iff host->dma_buffer is not NULL.
1427      */
1428     if (host->dma_buffer) {
1429         dma_unmap_single(mmc_dev(host->mmc), host->dma_addr,
1430             WBSD_DMA_SIZE, DMA_BIDIRECTIONAL);
1431         kfree(host->dma_buffer);
1432     }
1433     if (host->dma >= 0)
1434         free_dma(host->dma);
1435 
1436     host->dma = -1;
1437     host->dma_buffer = NULL;
1438     host->dma_addr = 0;
1439 }
1440 
1441 /*
1442  * Allocate/free IRQ.
1443  */
1444 
1445 static int wbsd_request_irq(struct wbsd_host *host, int irq)
1446 {
1447     int ret;
1448 
1449     /*
1450      * Set up tasklets. Must be done before requesting interrupt.
1451      */
1452     tasklet_setup(&host->card_tasklet, wbsd_tasklet_card);
1453     tasklet_setup(&host->fifo_tasklet, wbsd_tasklet_fifo);
1454     tasklet_setup(&host->crc_tasklet, wbsd_tasklet_crc);
1455     tasklet_setup(&host->timeout_tasklet, wbsd_tasklet_timeout);
1456     tasklet_setup(&host->finish_tasklet, wbsd_tasklet_finish);
1457 
1458     /*
1459      * Allocate interrupt.
1460      */
1461     ret = request_irq(irq, wbsd_irq, IRQF_SHARED, DRIVER_NAME, host);
1462     if (ret)
1463         return ret;
1464 
1465     host->irq = irq;
1466 
1467     return 0;
1468 }
1469 
1470 static void  wbsd_release_irq(struct wbsd_host *host)
1471 {
1472     if (!host->irq)
1473         return;
1474 
1475     free_irq(host->irq, host);
1476 
1477     host->irq = 0;
1478 
1479     tasklet_kill(&host->card_tasklet);
1480     tasklet_kill(&host->fifo_tasklet);
1481     tasklet_kill(&host->crc_tasklet);
1482     tasklet_kill(&host->timeout_tasklet);
1483     tasklet_kill(&host->finish_tasklet);
1484 }
1485 
1486 /*
1487  * Allocate all resources for the host.
1488  */
1489 
1490 static int wbsd_request_resources(struct wbsd_host *host,
1491     int base, int irq, int dma)
1492 {
1493     int ret;
1494 
1495     /*
1496      * Allocate I/O ports.
1497      */
1498     ret = wbsd_request_region(host, base);
1499     if (ret)
1500         return ret;
1501 
1502     /*
1503      * Allocate interrupt.
1504      */
1505     ret = wbsd_request_irq(host, irq);
1506     if (ret)
1507         return ret;
1508 
1509     /*
1510      * Allocate DMA.
1511      */
1512     wbsd_request_dma(host, dma);
1513 
1514     return 0;
1515 }
1516 
1517 /*
1518  * Release all resources for the host.
1519  */
1520 
1521 static void wbsd_release_resources(struct wbsd_host *host)
1522 {
1523     wbsd_release_dma(host);
1524     wbsd_release_irq(host);
1525     wbsd_release_regions(host);
1526 }
1527 
1528 /*
1529  * Configure the resources the chip should use.
1530  */
1531 
1532 static void wbsd_chip_config(struct wbsd_host *host)
1533 {
1534     wbsd_unlock_config(host);
1535 
1536     /*
1537      * Reset the chip.
1538      */
1539     wbsd_write_config(host, WBSD_CONF_SWRST, 1);
1540     wbsd_write_config(host, WBSD_CONF_SWRST, 0);
1541 
1542     /*
1543      * Select SD/MMC function.
1544      */
1545     wbsd_write_config(host, WBSD_CONF_DEVICE, DEVICE_SD);
1546 
1547     /*
1548      * Set up card detection.
1549      */
1550     wbsd_write_config(host, WBSD_CONF_PINS, WBSD_PINS_DETECT_GP11);
1551 
1552     /*
1553      * Configure chip
1554      */
1555     wbsd_write_config(host, WBSD_CONF_PORT_HI, host->base >> 8);
1556     wbsd_write_config(host, WBSD_CONF_PORT_LO, host->base & 0xff);
1557 
1558     wbsd_write_config(host, WBSD_CONF_IRQ, host->irq);
1559 
1560     if (host->dma >= 0)
1561         wbsd_write_config(host, WBSD_CONF_DRQ, host->dma);
1562 
1563     /*
1564      * Enable and power up chip.
1565      */
1566     wbsd_write_config(host, WBSD_CONF_ENABLE, 1);
1567     wbsd_write_config(host, WBSD_CONF_POWER, 0x20);
1568 
1569     wbsd_lock_config(host);
1570 }
1571 
1572 /*
1573  * Check that configured resources are correct.
1574  */
1575 
1576 static int wbsd_chip_validate(struct wbsd_host *host)
1577 {
1578     int base, irq, dma;
1579 
1580     wbsd_unlock_config(host);
1581 
1582     /*
1583      * Select SD/MMC function.
1584      */
1585     wbsd_write_config(host, WBSD_CONF_DEVICE, DEVICE_SD);
1586 
1587     /*
1588      * Read configuration.
1589      */
1590     base = wbsd_read_config(host, WBSD_CONF_PORT_HI) << 8;
1591     base |= wbsd_read_config(host, WBSD_CONF_PORT_LO);
1592 
1593     irq = wbsd_read_config(host, WBSD_CONF_IRQ);
1594 
1595     dma = wbsd_read_config(host, WBSD_CONF_DRQ);
1596 
1597     wbsd_lock_config(host);
1598 
1599     /*
1600      * Validate against given configuration.
1601      */
1602     if (base != host->base)
1603         return 0;
1604     if (irq != host->irq)
1605         return 0;
1606     if ((dma != host->dma) && (host->dma != -1))
1607         return 0;
1608 
1609     return 1;
1610 }
1611 
1612 /*
1613  * Powers down the SD function
1614  */
1615 
1616 static void wbsd_chip_poweroff(struct wbsd_host *host)
1617 {
1618     wbsd_unlock_config(host);
1619 
1620     wbsd_write_config(host, WBSD_CONF_DEVICE, DEVICE_SD);
1621     wbsd_write_config(host, WBSD_CONF_ENABLE, 0);
1622 
1623     wbsd_lock_config(host);
1624 }
1625 
1626 /*****************************************************************************\
1627  *                                                                           *
1628  * Devices setup and shutdown                                                *
1629  *                                                                           *
1630 \*****************************************************************************/
1631 
1632 static int wbsd_init(struct device *dev, int base, int irq, int dma,
1633     int pnp)
1634 {
1635     struct wbsd_host *host = NULL;
1636     struct mmc_host *mmc = NULL;
1637     int ret;
1638 
1639     ret = wbsd_alloc_mmc(dev);
1640     if (ret)
1641         return ret;
1642 
1643     mmc = dev_get_drvdata(dev);
1644     host = mmc_priv(mmc);
1645 
1646     /*
1647      * Scan for hardware.
1648      */
1649     ret = wbsd_scan(host);
1650     if (ret) {
1651         if (pnp && (ret == -ENODEV)) {
1652             pr_warn(DRIVER_NAME ": Unable to confirm device presence - you may experience lock-ups\n");
1653         } else {
1654             wbsd_free_mmc(dev);
1655             return ret;
1656         }
1657     }
1658 
1659     /*
1660      * Request resources.
1661      */
1662     ret = wbsd_request_resources(host, base, irq, dma);
1663     if (ret) {
1664         wbsd_release_resources(host);
1665         wbsd_free_mmc(dev);
1666         return ret;
1667     }
1668 
1669     /*
1670      * See if chip needs to be configured.
1671      */
1672     if (pnp) {
1673         if ((host->config != 0) && !wbsd_chip_validate(host)) {
1674             pr_warn(DRIVER_NAME ": PnP active but chip not configured! You probably have a buggy BIOS. Configuring chip manually.\n");
1675             wbsd_chip_config(host);
1676         }
1677     } else
1678         wbsd_chip_config(host);
1679 
1680     /*
1681      * Power Management stuff. No idea how this works.
1682      * Not tested.
1683      */
1684 #ifdef CONFIG_PM
1685     if (host->config) {
1686         wbsd_unlock_config(host);
1687         wbsd_write_config(host, WBSD_CONF_PME, 0xA0);
1688         wbsd_lock_config(host);
1689     }
1690 #endif
1691     /*
1692      * Allow device to initialise itself properly.
1693      */
1694     mdelay(5);
1695 
1696     /*
1697      * Reset the chip into a known state.
1698      */
1699     wbsd_init_device(host);
1700 
1701     mmc_add_host(mmc);
1702 
1703     pr_info("%s: W83L51xD", mmc_hostname(mmc));
1704     if (host->chip_id != 0)
1705         printk(" id %x", (int)host->chip_id);
1706     printk(" at 0x%x irq %d", (int)host->base, (int)host->irq);
1707     if (host->dma >= 0)
1708         printk(" dma %d", (int)host->dma);
1709     else
1710         printk(" FIFO");
1711     if (pnp)
1712         printk(" PnP");
1713     printk("\n");
1714 
1715     return 0;
1716 }
1717 
1718 static void wbsd_shutdown(struct device *dev, int pnp)
1719 {
1720     struct mmc_host *mmc = dev_get_drvdata(dev);
1721     struct wbsd_host *host;
1722 
1723     if (!mmc)
1724         return;
1725 
1726     host = mmc_priv(mmc);
1727 
1728     mmc_remove_host(mmc);
1729 
1730     /*
1731      * Power down the SD/MMC function.
1732      */
1733     if (!pnp)
1734         wbsd_chip_poweroff(host);
1735 
1736     wbsd_release_resources(host);
1737 
1738     wbsd_free_mmc(dev);
1739 }
1740 
1741 /*
1742  * Non-PnP
1743  */
1744 
1745 static int wbsd_probe(struct platform_device *dev)
1746 {
1747     /* Use the module parameters for resources */
1748     return wbsd_init(&dev->dev, param_io, param_irq, param_dma, 0);
1749 }
1750 
1751 static int wbsd_remove(struct platform_device *dev)
1752 {
1753     wbsd_shutdown(&dev->dev, 0);
1754 
1755     return 0;
1756 }
1757 
1758 /*
1759  * PnP
1760  */
1761 
1762 #ifdef CONFIG_PNP
1763 
1764 static int
1765 wbsd_pnp_probe(struct pnp_dev *pnpdev, const struct pnp_device_id *dev_id)
1766 {
1767     int io, irq, dma;
1768 
1769     /*
1770      * Get resources from PnP layer.
1771      */
1772     io = pnp_port_start(pnpdev, 0);
1773     irq = pnp_irq(pnpdev, 0);
1774     if (pnp_dma_valid(pnpdev, 0))
1775         dma = pnp_dma(pnpdev, 0);
1776     else
1777         dma = -1;
1778 
1779     DBGF("PnP resources: port %3x irq %d dma %d\n", io, irq, dma);
1780 
1781     return wbsd_init(&pnpdev->dev, io, irq, dma, 1);
1782 }
1783 
1784 static void wbsd_pnp_remove(struct pnp_dev *dev)
1785 {
1786     wbsd_shutdown(&dev->dev, 1);
1787 }
1788 
1789 #endif /* CONFIG_PNP */
1790 
1791 /*
1792  * Power management
1793  */
1794 
1795 #ifdef CONFIG_PM
1796 
1797 static int wbsd_platform_suspend(struct platform_device *dev,
1798                  pm_message_t state)
1799 {
1800     struct mmc_host *mmc = platform_get_drvdata(dev);
1801     struct wbsd_host *host;
1802 
1803     if (mmc == NULL)
1804         return 0;
1805 
1806     DBGF("Suspending...\n");
1807 
1808     host = mmc_priv(mmc);
1809 
1810     wbsd_chip_poweroff(host);
1811     return 0;
1812 }
1813 
1814 static int wbsd_platform_resume(struct platform_device *dev)
1815 {
1816     struct mmc_host *mmc = platform_get_drvdata(dev);
1817     struct wbsd_host *host;
1818 
1819     if (mmc == NULL)
1820         return 0;
1821 
1822     DBGF("Resuming...\n");
1823 
1824     host = mmc_priv(mmc);
1825 
1826     wbsd_chip_config(host);
1827 
1828     /*
1829      * Allow device to initialise itself properly.
1830      */
1831     mdelay(5);
1832 
1833     wbsd_init_device(host);
1834     return 0;
1835 }
1836 
1837 #ifdef CONFIG_PNP
1838 
1839 static int wbsd_pnp_suspend(struct pnp_dev *pnp_dev, pm_message_t state)
1840 {
1841     struct mmc_host *mmc = dev_get_drvdata(&pnp_dev->dev);
1842 
1843     if (mmc == NULL)
1844         return 0;
1845 
1846     DBGF("Suspending...\n");
1847     return 0;
1848 }
1849 
1850 static int wbsd_pnp_resume(struct pnp_dev *pnp_dev)
1851 {
1852     struct mmc_host *mmc = dev_get_drvdata(&pnp_dev->dev);
1853     struct wbsd_host *host;
1854 
1855     if (mmc == NULL)
1856         return 0;
1857 
1858     DBGF("Resuming...\n");
1859 
1860     host = mmc_priv(mmc);
1861 
1862     /*
1863      * See if chip needs to be configured.
1864      */
1865     if (host->config != 0) {
1866         if (!wbsd_chip_validate(host)) {
1867             pr_warn(DRIVER_NAME ": PnP active but chip not configured! You probably have a buggy BIOS. Configuring chip manually.\n");
1868             wbsd_chip_config(host);
1869         }
1870     }
1871 
1872     /*
1873      * Allow device to initialise itself properly.
1874      */
1875     mdelay(5);
1876 
1877     wbsd_init_device(host);
1878     return 0;
1879 }
1880 
1881 #endif /* CONFIG_PNP */
1882 
1883 #else /* CONFIG_PM */
1884 
1885 #define wbsd_platform_suspend NULL
1886 #define wbsd_platform_resume NULL
1887 
1888 #define wbsd_pnp_suspend NULL
1889 #define wbsd_pnp_resume NULL
1890 
1891 #endif /* CONFIG_PM */
1892 
1893 static struct platform_device *wbsd_device;
1894 
1895 static struct platform_driver wbsd_driver = {
1896     .probe      = wbsd_probe,
1897     .remove     = wbsd_remove,
1898 
1899     .suspend    = wbsd_platform_suspend,
1900     .resume     = wbsd_platform_resume,
1901     .driver     = {
1902         .name   = DRIVER_NAME,
1903         .probe_type = PROBE_PREFER_ASYNCHRONOUS,
1904     },
1905 };
1906 
1907 #ifdef CONFIG_PNP
1908 
1909 static struct pnp_driver wbsd_pnp_driver = {
1910     .name       = DRIVER_NAME,
1911     .id_table   = pnp_dev_table,
1912     .probe      = wbsd_pnp_probe,
1913     .remove     = wbsd_pnp_remove,
1914 
1915     .suspend    = wbsd_pnp_suspend,
1916     .resume     = wbsd_pnp_resume,
1917 };
1918 
1919 #endif /* CONFIG_PNP */
1920 
1921 /*
1922  * Module loading/unloading
1923  */
1924 
1925 static int __init wbsd_drv_init(void)
1926 {
1927     int result;
1928 
1929     pr_info(DRIVER_NAME
1930         ": Winbond W83L51xD SD/MMC card interface driver\n");
1931     pr_info(DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
1932 
1933 #ifdef CONFIG_PNP
1934 
1935     if (!param_nopnp) {
1936         result = pnp_register_driver(&wbsd_pnp_driver);
1937         if (result < 0)
1938             return result;
1939     }
1940 #endif /* CONFIG_PNP */
1941 
1942     if (param_nopnp) {
1943         result = platform_driver_register(&wbsd_driver);
1944         if (result < 0)
1945             return result;
1946 
1947         wbsd_device = platform_device_alloc(DRIVER_NAME, -1);
1948         if (!wbsd_device) {
1949             platform_driver_unregister(&wbsd_driver);
1950             return -ENOMEM;
1951         }
1952 
1953         result = platform_device_add(wbsd_device);
1954         if (result) {
1955             platform_device_put(wbsd_device);
1956             platform_driver_unregister(&wbsd_driver);
1957             return result;
1958         }
1959     }
1960 
1961     return 0;
1962 }
1963 
1964 static void __exit wbsd_drv_exit(void)
1965 {
1966 #ifdef CONFIG_PNP
1967 
1968     if (!param_nopnp)
1969         pnp_unregister_driver(&wbsd_pnp_driver);
1970 
1971 #endif /* CONFIG_PNP */
1972 
1973     if (param_nopnp) {
1974         platform_device_unregister(wbsd_device);
1975 
1976         platform_driver_unregister(&wbsd_driver);
1977     }
1978 
1979     DBG("unloaded\n");
1980 }
1981 
1982 module_init(wbsd_drv_init);
1983 module_exit(wbsd_drv_exit);
1984 #ifdef CONFIG_PNP
1985 module_param_hw_named(nopnp, param_nopnp, uint, other, 0444);
1986 #endif
1987 module_param_hw_named(io, param_io, uint, ioport, 0444);
1988 module_param_hw_named(irq, param_irq, uint, irq, 0444);
1989 module_param_hw_named(dma, param_dma, int, dma, 0444);
1990 
1991 MODULE_LICENSE("GPL");
1992 MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
1993 MODULE_DESCRIPTION("Winbond W83L51xD SD/MMC card interface driver");
1994 
1995 #ifdef CONFIG_PNP
1996 MODULE_PARM_DESC(nopnp, "Scan for device instead of relying on PNP. (default 0)");
1997 #endif
1998 MODULE_PARM_DESC(io, "I/O base to allocate. Must be 8 byte aligned. (default 0x248)");
1999 MODULE_PARM_DESC(irq, "IRQ to allocate. (default 6)");
2000 MODULE_PARM_DESC(dma, "DMA channel to allocate. -1 for no DMA. (default 2)");