Lines Matching refs:host

2  *  linux/drivers/mmc/host/wbsd.c - Winbond W83L51xD SD/MMC driver
35 #include <linux/mmc/host.h>
87 static inline void wbsd_unlock_config(struct wbsd_host *host)
89 BUG_ON(host->config == 0);
91 outb(host->unlock_code, host->config);
92 outb(host->unlock_code, host->config);
95 static inline void wbsd_lock_config(struct wbsd_host *host)
97 BUG_ON(host->config == 0);
99 outb(LOCK_CODE, host->config);
102 static inline void wbsd_write_config(struct wbsd_host *host, u8 reg, u8 value)
104 BUG_ON(host->config == 0);
106 outb(reg, host->config);
107 outb(value, host->config + 1);
110 static inline u8 wbsd_read_config(struct wbsd_host *host, u8 reg)
112 BUG_ON(host->config == 0);
114 outb(reg, host->config);
115 return inb(host->config + 1);
118 static inline void wbsd_write_index(struct wbsd_host *host, u8 index, u8 value)
120 outb(index, host->base + WBSD_IDXR);
121 outb(value, host->base + WBSD_DATAR);
124 static inline u8 wbsd_read_index(struct wbsd_host *host, u8 index)
126 outb(index, host->base + WBSD_IDXR);
127 return inb(host->base + WBSD_DATAR);
134 static void wbsd_init_device(struct wbsd_host *host)
141 setup = wbsd_read_index(host, WBSD_IDX_SETUP);
143 wbsd_write_index(host, WBSD_IDX_SETUP, setup);
149 wbsd_write_index(host, WBSD_IDX_SETUP, setup);
150 host->flags &= ~WBSD_FIGNORE_DETECT;
155 host->clk = wbsd_read_index(host, WBSD_IDX_CLK);
160 outb(WBSD_POWER_N, host->base + WBSD_CSR);
165 wbsd_write_index(host, WBSD_IDX_TAAC, 0x7F);
170 if (inb(host->base + WBSD_CSR) & WBSD_CARDPRESENT)
171 host->flags |= WBSD_FCARD_PRESENT;
173 host->flags &= ~WBSD_FCARD_PRESENT;
185 outb(ier, host->base + WBSD_EIR);
190 inb(host->base + WBSD_ISR);
193 static void wbsd_reset(struct wbsd_host *host)
197 pr_err("%s: Resetting chip\n", mmc_hostname(host->mmc));
202 setup = wbsd_read_index(host, WBSD_IDX_SETUP);
204 wbsd_write_index(host, WBSD_IDX_SETUP, setup);
207 static void wbsd_request_end(struct wbsd_host *host, struct mmc_request *mrq)
211 if (host->dma >= 0) {
216 disable_dma(host->dma);
217 clear_dma_ff(host->dma);
221 * Disable DMA on host.
223 wbsd_write_index(host, WBSD_IDX_DMA, 0);
226 host->mrq = NULL;
231 spin_unlock(&host->lock);
232 mmc_request_done(host->mmc, mrq);
233 spin_lock(&host->lock);
240 static inline void wbsd_init_sg(struct wbsd_host *host, struct mmc_data *data)
245 host->cur_sg = data->sg;
246 host->num_sg = data->sg_len;
248 host->offset = 0;
249 host->remain = host->cur_sg->length;
252 static inline int wbsd_next_sg(struct wbsd_host *host)
257 host->cur_sg++;
258 host->num_sg--;
263 if (host->num_sg > 0) {
264 host->offset = 0;
265 host->remain = host->cur_sg->length;
268 return host->num_sg;
271 static inline char *wbsd_sg_to_buffer(struct wbsd_host *host)
273 return sg_virt(host->cur_sg);
276 static inline void wbsd_sg_to_dma(struct wbsd_host *host, struct mmc_data *data)
280 char *dmabuf = host->dma_buffer;
293 static inline void wbsd_dma_to_sg(struct wbsd_host *host, struct mmc_data *data)
297 char *dmabuf = host->dma_buffer;
314 static inline void wbsd_get_short_reply(struct wbsd_host *host,
320 if (wbsd_read_index(host, WBSD_IDX_RSPLEN) != WBSD_RSP_SHORT) {
325 cmd->resp[0] = wbsd_read_index(host, WBSD_IDX_RESP12) << 24;
326 cmd->resp[0] |= wbsd_read_index(host, WBSD_IDX_RESP13) << 16;
327 cmd->resp[0] |= wbsd_read_index(host, WBSD_IDX_RESP14) << 8;
328 cmd->resp[0] |= wbsd_read_index(host, WBSD_IDX_RESP15) << 0;
329 cmd->resp[1] = wbsd_read_index(host, WBSD_IDX_RESP16) << 24;
332 static inline void wbsd_get_long_reply(struct wbsd_host *host,
340 if (wbsd_read_index(host, WBSD_IDX_RSPLEN) != WBSD_RSP_LONG) {
347 wbsd_read_index(host, WBSD_IDX_RESP1 + i * 4) << 24;
349 wbsd_read_index(host, WBSD_IDX_RESP2 + i * 4) << 16;
351 wbsd_read_index(host, WBSD_IDX_RESP3 + i * 4) << 8;
353 wbsd_read_index(host, WBSD_IDX_RESP4 + i * 4) << 0;
357 static void wbsd_send_command(struct wbsd_host *host, struct mmc_command *cmd)
367 host->isr = 0;
370 * Send the command (CRC calculated by host).
372 outb(cmd->opcode, host->base + WBSD_CMDR);
374 outb((cmd->arg >> (i * 8)) & 0xff, host->base + WBSD_CMDR);
382 status = wbsd_read_index(host, WBSD_IDX_STATUS);
392 isr = host->isr;
406 wbsd_get_long_reply(host, cmd);
408 wbsd_get_short_reply(host, cmd);
417 static void wbsd_empty_fifo(struct wbsd_host *host)
419 struct mmc_data *data = host->mrq->cmd->data;
426 if (host->num_sg == 0)
429 buffer = wbsd_sg_to_buffer(host) + host->offset;
435 while (!((fsr = inb(host->base + WBSD_FSR)) & WBSD_FIFO_EMPTY)) {
448 *buffer = inb(host->base + WBSD_DFR);
450 host->offset++;
451 host->remain--;
458 if (host->remain == 0) {
462 if (!wbsd_next_sg(host))
465 buffer = wbsd_sg_to_buffer(host);
476 tasklet_schedule(&host->fifo_tasklet);
479 static void wbsd_fill_fifo(struct wbsd_host *host)
481 struct mmc_data *data = host->mrq->cmd->data;
489 if (host->num_sg == 0)
492 buffer = wbsd_sg_to_buffer(host) + host->offset;
498 while (!((fsr = inb(host->base + WBSD_FSR)) & WBSD_FIFO_FULL)) {
511 outb(*buffer, host->base + WBSD_DFR);
513 host->offset++;
514 host->remain--;
521 if (host->remain == 0) {
525 if (!wbsd_next_sg(host))
528 buffer = wbsd_sg_to_buffer(host);
538 tasklet_schedule(&host->fifo_tasklet);
541 static void wbsd_prepare_data(struct wbsd_host *host, struct mmc_data *data)
558 wbsd_write_index(host, WBSD_IDX_TAAC, 127);
560 wbsd_write_index(host, WBSD_IDX_TAAC,
565 wbsd_write_index(host, WBSD_IDX_NSAC, 255);
567 wbsd_write_index(host, WBSD_IDX_NSAC, data->timeout_clks);
577 if (host->bus_width == MMC_BUS_WIDTH_1) {
580 wbsd_write_index(host, WBSD_IDX_PBSMSB, (blksize >> 4) & 0xF0);
581 wbsd_write_index(host, WBSD_IDX_PBSLSB, blksize & 0xFF);
582 } else if (host->bus_width == MMC_BUS_WIDTH_4) {
585 wbsd_write_index(host, WBSD_IDX_PBSMSB,
587 wbsd_write_index(host, WBSD_IDX_PBSLSB, blksize & 0xFF);
598 setup = wbsd_read_index(host, WBSD_IDX_SETUP);
600 wbsd_write_index(host, WBSD_IDX_SETUP, setup);
605 if (host->dma >= 0) {
620 wbsd_sg_to_dma(host, data);
626 disable_dma(host->dma);
627 clear_dma_ff(host->dma);
629 set_dma_mode(host->dma, DMA_MODE_READ & ~0x40);
631 set_dma_mode(host->dma, DMA_MODE_WRITE & ~0x40);
632 set_dma_addr(host->dma, host->dma_addr);
633 set_dma_count(host->dma, size);
635 enable_dma(host->dma);
639 * Enable DMA on the host.
641 wbsd_write_index(host, WBSD_IDX_DMA, WBSD_DMA_ENABLE);
647 host->firsterr = 1;
652 wbsd_init_sg(host, data);
657 wbsd_write_index(host, WBSD_IDX_DMA, 0);
664 wbsd_write_index(host, WBSD_IDX_FIFOEN,
667 wbsd_write_index(host, WBSD_IDX_FIFOEN,
669 wbsd_fill_fifo(host);
676 static void wbsd_finish_data(struct wbsd_host *host, struct mmc_data *data)
682 WARN_ON(host->mrq == NULL);
688 wbsd_send_command(host, data->stop);
695 status = wbsd_read_index(host, WBSD_IDX_STATUS);
701 if (host->dma >= 0) {
703 * Disable DMA on the host.
705 wbsd_write_index(host, WBSD_IDX_DMA, 0);
711 disable_dma(host->dma);
712 clear_dma_ff(host->dma);
713 count = get_dma_residue(host->dma);
716 data->bytes_xfered = host->mrq->data->blocks *
717 host->mrq->data->blksz - count;
726 mmc_hostname(host->mmc), count);
736 wbsd_dma_to_sg(host, data);
745 wbsd_request_end(host, host->mrq);
756 struct wbsd_host *host = mmc_priv(mmc);
762 spin_lock_bh(&host->lock);
764 BUG_ON(host->mrq != NULL);
768 host->mrq = mrq;
773 if (!(host->flags & WBSD_FCARD_PRESENT)) {
807 mmc_hostname(host->mmc), cmd->opcode);
819 wbsd_prepare_data(host, cmd->data);
825 wbsd_send_command(host, cmd);
836 if (host->dma == -1)
837 tasklet_schedule(&host->fifo_tasklet);
839 spin_unlock_bh(&host->lock);
845 wbsd_request_end(host, mrq);
847 spin_unlock_bh(&host->lock);
852 struct wbsd_host *host = mmc_priv(mmc);
855 spin_lock_bh(&host->lock);
862 wbsd_init_device(host);
877 if (clk != host->clk) {
878 wbsd_write_index(host, WBSD_IDX_CLK, clk);
879 host->clk = clk;
886 pwr = inb(host->base + WBSD_CSR);
888 outb(pwr, host->base + WBSD_CSR);
896 setup = wbsd_read_index(host, WBSD_IDX_SETUP);
900 host->flags |= WBSD_FIGNORE_DETECT;
909 mod_timer(&host->ignore_timer, jiffies + HZ / 100);
912 wbsd_write_index(host, WBSD_IDX_SETUP, setup);
918 host->bus_width = ios->bus_width;
920 spin_unlock_bh(&host->lock);
925 struct wbsd_host *host = mmc_priv(mmc);
928 spin_lock_bh(&host->lock);
930 csr = inb(host->base + WBSD_CSR);
932 outb(csr, host->base + WBSD_CSR);
936 csr = inb(host->base + WBSD_CSR);
938 outb(csr, host->base + WBSD_CSR);
940 spin_unlock_bh(&host->lock);
963 struct wbsd_host *host = (struct wbsd_host *)data;
965 BUG_ON(host == NULL);
969 spin_lock_bh(&host->lock);
971 host->flags &= ~WBSD_FIGNORE_DETECT;
977 tasklet_schedule(&host->card_tasklet);
979 spin_unlock_bh(&host->lock);
986 static inline struct mmc_data *wbsd_get_data(struct wbsd_host *host)
988 WARN_ON(!host->mrq);
989 if (!host->mrq)
992 WARN_ON(!host->mrq->cmd);
993 if (!host->mrq->cmd)
996 WARN_ON(!host->mrq->cmd->data);
997 if (!host->mrq->cmd->data)
1000 return host->mrq->cmd->data;
1005 struct wbsd_host *host = (struct wbsd_host *)param;
1009 spin_lock(&host->lock);
1011 if (host->flags & WBSD_FIGNORE_DETECT) {
1012 spin_unlock(&host->lock);
1016 csr = inb(host->base + WBSD_CSR);
1020 if (!(host->flags & WBSD_FCARD_PRESENT)) {
1022 host->flags |= WBSD_FCARD_PRESENT;
1026 } else if (host->flags & WBSD_FCARD_PRESENT) {
1028 host->flags &= ~WBSD_FCARD_PRESENT;
1030 if (host->mrq) {
1032 mmc_hostname(host->mmc));
1033 wbsd_reset(host);
1035 host->mrq->cmd->error = -ENOMEDIUM;
1036 tasklet_schedule(&host->finish_tasklet);
1046 spin_unlock(&host->lock);
1049 mmc_detect_change(host->mmc, msecs_to_jiffies(delay));
1054 struct wbsd_host *host = (struct wbsd_host *)param;
1057 spin_lock(&host->lock);
1059 if (!host->mrq)
1062 data = wbsd_get_data(host);
1067 wbsd_fill_fifo(host);
1069 wbsd_empty_fifo(host);
1074 if (host->num_sg == 0) {
1075 wbsd_write_index(host, WBSD_IDX_FIFOEN, 0);
1076 tasklet_schedule(&host->finish_tasklet);
1080 spin_unlock(&host->lock);
1085 struct wbsd_host *host = (struct wbsd_host *)param;
1088 spin_lock(&host->lock);
1090 if (!host->mrq)
1093 data = wbsd_get_data(host);
1101 tasklet_schedule(&host->finish_tasklet);
1104 spin_unlock(&host->lock);
1109 struct wbsd_host *host = (struct wbsd_host *)param;
1112 spin_lock(&host->lock);
1114 if (!host->mrq)
1117 data = wbsd_get_data(host);
1125 tasklet_schedule(&host->finish_tasklet);
1128 spin_unlock(&host->lock);
1133 struct wbsd_host *host = (struct wbsd_host *)param;
1136 spin_lock(&host->lock);
1138 WARN_ON(!host->mrq);
1139 if (!host->mrq)
1142 data = wbsd_get_data(host);
1146 wbsd_finish_data(host, data);
1149 spin_unlock(&host->lock);
1158 struct wbsd_host *host = dev_id;
1161 isr = inb(host->base + WBSD_ISR);
1169 host->isr |= isr;
1175 tasklet_schedule(&host->card_tasklet);
1177 tasklet_schedule(&host->fifo_tasklet);
1179 tasklet_hi_schedule(&host->crc_tasklet);
1181 tasklet_hi_schedule(&host->timeout_tasklet);
1183 tasklet_schedule(&host->finish_tasklet);
1201 struct wbsd_host *host;
1210 host = mmc_priv(mmc);
1211 host->mmc = mmc;
1213 host->dma = -1;
1216 * Set host parameters.
1224 spin_lock_init(&host->lock);
1229 init_timer(&host->ignore_timer);
1230 host->ignore_timer.data = (unsigned long)host;
1231 host->ignore_timer.function = wbsd_reset_ignore;
1270 struct wbsd_host *host;
1276 host = mmc_priv(mmc);
1277 BUG_ON(host == NULL);
1279 del_timer_sync(&host->ignore_timer);
1290 static int wbsd_scan(struct wbsd_host *host)
1306 host->config = config_ports[i];
1307 host->unlock_code = unlock_codes[j];
1309 wbsd_unlock_config(host);
1317 wbsd_lock_config(host);
1321 host->chip_id = id;
1336 host->config = 0;
1337 host->unlock_code = 0;
1346 static int wbsd_request_region(struct wbsd_host *host, int base)
1354 host->base = base;
1359 static void wbsd_release_regions(struct wbsd_host *host)
1361 if (host->base)
1362 release_region(host->base, 8);
1364 host->base = 0;
1366 if (host->config)
1367 release_region(host->config, 2);
1369 host->config = 0;
1376 static void wbsd_request_dma(struct wbsd_host *host, int dma)
1388 host->dma_buffer = kmalloc(WBSD_DMA_SIZE,
1390 if (!host->dma_buffer)
1396 host->dma_addr = dma_map_single(mmc_dev(host->mmc), host->dma_buffer,
1402 if ((host->dma_addr & 0xffff) != 0)
1407 else if (host->dma_addr >= 0x1000000)
1410 host->dma = dma;
1420 dma_unmap_single(mmc_dev(host->mmc), host->dma_addr,
1422 host->dma_addr = 0;
1424 kfree(host->dma_buffer);
1425 host->dma_buffer = NULL;
1435 static void wbsd_release_dma(struct wbsd_host *host)
1437 if (host->dma_addr) {
1438 dma_unmap_single(mmc_dev(host->mmc), host->dma_addr,
1441 kfree(host->dma_buffer);
1442 if (host->dma >= 0)
1443 free_dma(host->dma);
1445 host->dma = -1;
1446 host->dma_buffer = NULL;
1447 host->dma_addr = 0;
1454 static int wbsd_request_irq(struct wbsd_host *host, int irq)
1461 tasklet_init(&host->card_tasklet, wbsd_tasklet_card,
1462 (unsigned long)host);
1463 tasklet_init(&host->fifo_tasklet, wbsd_tasklet_fifo,
1464 (unsigned long)host);
1465 tasklet_init(&host->crc_tasklet, wbsd_tasklet_crc,
1466 (unsigned long)host);
1467 tasklet_init(&host->timeout_tasklet, wbsd_tasklet_timeout,
1468 (unsigned long)host);
1469 tasklet_init(&host->finish_tasklet, wbsd_tasklet_finish,
1470 (unsigned long)host);
1475 ret = request_irq(irq, wbsd_irq, IRQF_SHARED, DRIVER_NAME, host);
1479 host->irq = irq;
1484 static void wbsd_release_irq(struct wbsd_host *host)
1486 if (!host->irq)
1489 free_irq(host->irq, host);
1491 host->irq = 0;
1493 tasklet_kill(&host->card_tasklet);
1494 tasklet_kill(&host->fifo_tasklet);
1495 tasklet_kill(&host->crc_tasklet);
1496 tasklet_kill(&host->timeout_tasklet);
1497 tasklet_kill(&host->finish_tasklet);
1501 * Allocate all resources for the host.
1504 static int wbsd_request_resources(struct wbsd_host *host,
1512 ret = wbsd_request_region(host, base);
1519 ret = wbsd_request_irq(host, irq);
1526 wbsd_request_dma(host, dma);
1532 * Release all resources for the host.
1535 static void wbsd_release_resources(struct wbsd_host *host)
1537 wbsd_release_dma(host);
1538 wbsd_release_irq(host);
1539 wbsd_release_regions(host);
1546 static void wbsd_chip_config(struct wbsd_host *host)
1548 wbsd_unlock_config(host);
1553 wbsd_write_config(host, WBSD_CONF_SWRST, 1);
1554 wbsd_write_config(host, WBSD_CONF_SWRST, 0);
1559 wbsd_write_config(host, WBSD_CONF_DEVICE, DEVICE_SD);
1564 wbsd_write_config(host, WBSD_CONF_PINS, WBSD_PINS_DETECT_GP11);
1569 wbsd_write_config(host, WBSD_CONF_PORT_HI, host->base >> 8);
1570 wbsd_write_config(host, WBSD_CONF_PORT_LO, host->base & 0xff);
1572 wbsd_write_config(host, WBSD_CONF_IRQ, host->irq);
1574 if (host->dma >= 0)
1575 wbsd_write_config(host, WBSD_CONF_DRQ, host->dma);
1580 wbsd_write_config(host, WBSD_CONF_ENABLE, 1);
1581 wbsd_write_config(host, WBSD_CONF_POWER, 0x20);
1583 wbsd_lock_config(host);
1590 static int wbsd_chip_validate(struct wbsd_host *host)
1594 wbsd_unlock_config(host);
1599 wbsd_write_config(host, WBSD_CONF_DEVICE, DEVICE_SD);
1604 base = wbsd_read_config(host, WBSD_CONF_PORT_HI) << 8;
1605 base |= wbsd_read_config(host, WBSD_CONF_PORT_LO);
1607 irq = wbsd_read_config(host, WBSD_CONF_IRQ);
1609 dma = wbsd_read_config(host, WBSD_CONF_DRQ);
1611 wbsd_lock_config(host);
1616 if (base != host->base)
1618 if (irq != host->irq)
1620 if ((dma != host->dma) && (host->dma != -1))
1630 static void wbsd_chip_poweroff(struct wbsd_host *host)
1632 wbsd_unlock_config(host);
1634 wbsd_write_config(host, WBSD_CONF_DEVICE, DEVICE_SD);
1635 wbsd_write_config(host, WBSD_CONF_ENABLE, 0);
1637 wbsd_lock_config(host);
1649 struct wbsd_host *host = NULL;
1658 host = mmc_priv(mmc);
1663 ret = wbsd_scan(host);
1676 ret = wbsd_request_resources(host, base, irq, dma);
1678 wbsd_release_resources(host);
1687 if ((host->config != 0) && !wbsd_chip_validate(host)) {
1689 wbsd_chip_config(host);
1692 wbsd_chip_config(host);
1699 if (host->config) {
1700 wbsd_unlock_config(host);
1701 wbsd_write_config(host, WBSD_CONF_PME, 0xA0);
1702 wbsd_lock_config(host);
1713 wbsd_init_device(host);
1718 if (host->chip_id != 0)
1719 printk(" id %x", (int)host->chip_id);
1720 printk(" at 0x%x irq %d", (int)host->base, (int)host->irq);
1721 if (host->dma >= 0)
1722 printk(" dma %d", (int)host->dma);
1735 struct wbsd_host *host;
1740 host = mmc_priv(mmc);
1748 wbsd_chip_poweroff(host);
1750 wbsd_release_resources(host);
1815 struct wbsd_host *host;
1822 host = mmc_priv(mmc);
1824 wbsd_chip_poweroff(host);
1831 struct wbsd_host *host;
1838 host = mmc_priv(mmc);
1840 wbsd_chip_config(host);
1847 wbsd_init_device(host);
1867 struct wbsd_host *host;
1874 host = mmc_priv(mmc);
1879 if (host->config != 0) {
1880 if (!wbsd_chip_validate(host)) {
1882 wbsd_chip_config(host);
1891 wbsd_init_device(host);