Lines Matching refs:host

3  *  linux/drivers/mmc/host/wbsd.c - Winbond W83L51xD SD/MMC driver
30 #include <linux/mmc/host.h>
84 static inline void wbsd_unlock_config(struct wbsd_host *host)
86 BUG_ON(host->config == 0);
88 outb(host->unlock_code, host->config);
89 outb(host->unlock_code, host->config);
92 static inline void wbsd_lock_config(struct wbsd_host *host)
94 BUG_ON(host->config == 0);
96 outb(LOCK_CODE, host->config);
99 static inline void wbsd_write_config(struct wbsd_host *host, u8 reg, u8 value)
101 BUG_ON(host->config == 0);
103 outb(reg, host->config);
104 outb(value, host->config + 1);
107 static inline u8 wbsd_read_config(struct wbsd_host *host, u8 reg)
109 BUG_ON(host->config == 0);
111 outb(reg, host->config);
112 return inb(host->config + 1);
115 static inline void wbsd_write_index(struct wbsd_host *host, u8 index, u8 value)
117 outb(index, host->base + WBSD_IDXR);
118 outb(value, host->base + WBSD_DATAR);
121 static inline u8 wbsd_read_index(struct wbsd_host *host, u8 index)
123 outb(index, host->base + WBSD_IDXR);
124 return inb(host->base + WBSD_DATAR);
131 static void wbsd_init_device(struct wbsd_host *host)
138 setup = wbsd_read_index(host, WBSD_IDX_SETUP);
140 wbsd_write_index(host, WBSD_IDX_SETUP, setup);
146 wbsd_write_index(host, WBSD_IDX_SETUP, setup);
147 host->flags &= ~WBSD_FIGNORE_DETECT;
152 host->clk = wbsd_read_index(host, WBSD_IDX_CLK);
157 outb(WBSD_POWER_N, host->base + WBSD_CSR);
162 wbsd_write_index(host, WBSD_IDX_TAAC, 0x7F);
167 if (inb(host->base + WBSD_CSR) & WBSD_CARDPRESENT)
168 host->flags |= WBSD_FCARD_PRESENT;
170 host->flags &= ~WBSD_FCARD_PRESENT;
182 outb(ier, host->base + WBSD_EIR);
187 inb(host->base + WBSD_ISR);
190 static void wbsd_reset(struct wbsd_host *host)
194 pr_err("%s: Resetting chip\n", mmc_hostname(host->mmc));
199 setup = wbsd_read_index(host, WBSD_IDX_SETUP);
201 wbsd_write_index(host, WBSD_IDX_SETUP, setup);
204 static void wbsd_request_end(struct wbsd_host *host, struct mmc_request *mrq)
208 if (host->dma >= 0) {
213 disable_dma(host->dma);
214 clear_dma_ff(host->dma);
218 * Disable DMA on host.
220 wbsd_write_index(host, WBSD_IDX_DMA, 0);
223 host->mrq = NULL;
228 spin_unlock(&host->lock);
229 mmc_request_done(host->mmc, mrq);
230 spin_lock(&host->lock);
237 static inline void wbsd_init_sg(struct wbsd_host *host, struct mmc_data *data)
242 host->cur_sg = data->sg;
243 host->num_sg = data->sg_len;
245 host->offset = 0;
246 host->remain = host->cur_sg->length;
249 static inline int wbsd_next_sg(struct wbsd_host *host)
254 host->cur_sg++;
255 host->num_sg--;
260 if (host->num_sg > 0) {
261 host->offset = 0;
262 host->remain = host->cur_sg->length;
265 return host->num_sg;
268 static inline char *wbsd_map_sg(struct wbsd_host *host)
270 return kmap_local_page(sg_page(host->cur_sg)) + host->cur_sg->offset;
273 static inline void wbsd_sg_to_dma(struct wbsd_host *host, struct mmc_data *data)
280 sg_copy_to_buffer(data->sg, data->sg_len, host->dma_buffer, len);
283 static inline void wbsd_dma_to_sg(struct wbsd_host *host, struct mmc_data *data)
290 sg_copy_from_buffer(data->sg, data->sg_len, host->dma_buffer, len);
297 static inline void wbsd_get_short_reply(struct wbsd_host *host,
303 if (wbsd_read_index(host, WBSD_IDX_RSPLEN) != WBSD_RSP_SHORT) {
308 cmd->resp[0] = wbsd_read_index(host, WBSD_IDX_RESP12) << 24;
309 cmd->resp[0] |= wbsd_read_index(host, WBSD_IDX_RESP13) << 16;
310 cmd->resp[0] |= wbsd_read_index(host, WBSD_IDX_RESP14) << 8;
311 cmd->resp[0] |= wbsd_read_index(host, WBSD_IDX_RESP15) << 0;
312 cmd->resp[1] = wbsd_read_index(host, WBSD_IDX_RESP16) << 24;
315 static inline void wbsd_get_long_reply(struct wbsd_host *host,
323 if (wbsd_read_index(host, WBSD_IDX_RSPLEN) != WBSD_RSP_LONG) {
330 wbsd_read_index(host, WBSD_IDX_RESP1 + i * 4) << 24;
332 wbsd_read_index(host, WBSD_IDX_RESP2 + i * 4) << 16;
334 wbsd_read_index(host, WBSD_IDX_RESP3 + i * 4) << 8;
336 wbsd_read_index(host, WBSD_IDX_RESP4 + i * 4) << 0;
340 static void wbsd_send_command(struct wbsd_host *host, struct mmc_command *cmd)
350 host->isr = 0;
353 * Send the command (CRC calculated by host).
355 outb(cmd->opcode, host->base + WBSD_CMDR);
357 outb((cmd->arg >> (i * 8)) & 0xff, host->base + WBSD_CMDR);
365 status = wbsd_read_index(host, WBSD_IDX_STATUS);
375 isr = host->isr;
389 wbsd_get_long_reply(host, cmd);
391 wbsd_get_short_reply(host, cmd);
400 static void wbsd_empty_fifo(struct wbsd_host *host)
402 struct mmc_data *data = host->mrq->cmd->data;
409 if (host->num_sg == 0)
412 buffer = wbsd_map_sg(host) + host->offset;
419 while (!((fsr = inb(host->base + WBSD_FSR)) & WBSD_FIFO_EMPTY)) {
432 buffer[idx++] = inb(host->base + WBSD_DFR);
433 host->offset++;
434 host->remain--;
441 if (host->remain == 0) {
446 if (!wbsd_next_sg(host))
449 buffer = wbsd_map_sg(host);
462 tasklet_schedule(&host->fifo_tasklet);
465 static void wbsd_fill_fifo(struct wbsd_host *host)
467 struct mmc_data *data = host->mrq->cmd->data;
475 if (host->num_sg == 0)
478 buffer = wbsd_map_sg(host) + host->offset;
485 while (!((fsr = inb(host->base + WBSD_FSR)) & WBSD_FIFO_FULL)) {
498 outb(buffer[idx], host->base + WBSD_DFR);
499 host->offset++;
500 host->remain--;
507 if (host->remain == 0) {
512 if (!wbsd_next_sg(host))
515 buffer = wbsd_map_sg(host);
527 tasklet_schedule(&host->fifo_tasklet);
530 static void wbsd_prepare_data(struct wbsd_host *host, struct mmc_data *data)
547 wbsd_write_index(host, WBSD_IDX_TAAC, 127);
549 wbsd_write_index(host, WBSD_IDX_TAAC,
554 wbsd_write_index(host, WBSD_IDX_NSAC, 255);
556 wbsd_write_index(host, WBSD_IDX_NSAC, data->timeout_clks);
566 if (host->bus_width == MMC_BUS_WIDTH_1) {
569 wbsd_write_index(host, WBSD_IDX_PBSMSB, (blksize >> 4) & 0xF0);
570 wbsd_write_index(host, WBSD_IDX_PBSLSB, blksize & 0xFF);
571 } else if (host->bus_width == MMC_BUS_WIDTH_4) {
574 wbsd_write_index(host, WBSD_IDX_PBSMSB,
576 wbsd_write_index(host, WBSD_IDX_PBSLSB, blksize & 0xFF);
587 setup = wbsd_read_index(host, WBSD_IDX_SETUP);
589 wbsd_write_index(host, WBSD_IDX_SETUP, setup);
594 if (host->dma >= 0) {
609 wbsd_sg_to_dma(host, data);
615 disable_dma(host->dma);
616 clear_dma_ff(host->dma);
618 set_dma_mode(host->dma, DMA_MODE_READ & ~0x40);
620 set_dma_mode(host->dma, DMA_MODE_WRITE & ~0x40);
621 set_dma_addr(host->dma, host->dma_addr);
622 set_dma_count(host->dma, size);
624 enable_dma(host->dma);
628 * Enable DMA on the host.
630 wbsd_write_index(host, WBSD_IDX_DMA, WBSD_DMA_ENABLE);
636 host->firsterr = 1;
641 wbsd_init_sg(host, data);
646 wbsd_write_index(host, WBSD_IDX_DMA, 0);
653 wbsd_write_index(host, WBSD_IDX_FIFOEN,
656 wbsd_write_index(host, WBSD_IDX_FIFOEN,
658 wbsd_fill_fifo(host);
665 static void wbsd_finish_data(struct wbsd_host *host, struct mmc_data *data)
671 WARN_ON(host->mrq == NULL);
677 wbsd_send_command(host, data->stop);
684 status = wbsd_read_index(host, WBSD_IDX_STATUS);
690 if (host->dma >= 0) {
692 * Disable DMA on the host.
694 wbsd_write_index(host, WBSD_IDX_DMA, 0);
700 disable_dma(host->dma);
701 clear_dma_ff(host->dma);
702 count = get_dma_residue(host->dma);
705 data->bytes_xfered = host->mrq->data->blocks *
706 host->mrq->data->blksz - count;
715 mmc_hostname(host->mmc), count);
725 wbsd_dma_to_sg(host, data);
734 wbsd_request_end(host, host->mrq);
745 struct wbsd_host *host = mmc_priv(mmc);
751 spin_lock_bh(&host->lock);
753 BUG_ON(host->mrq != NULL);
757 host->mrq = mrq;
762 if (!(host->flags & WBSD_FCARD_PRESENT)) {
795 mmc_hostname(host->mmc), cmd->opcode);
806 wbsd_prepare_data(host, cmd->data);
812 wbsd_send_command(host, cmd);
823 if (host->dma == -1)
824 tasklet_schedule(&host->fifo_tasklet);
826 spin_unlock_bh(&host->lock);
832 wbsd_request_end(host, mrq);
834 spin_unlock_bh(&host->lock);
839 struct wbsd_host *host = mmc_priv(mmc);
842 spin_lock_bh(&host->lock);
849 wbsd_init_device(host);
864 if (clk != host->clk) {
865 wbsd_write_index(host, WBSD_IDX_CLK, clk);
866 host->clk = clk;
873 pwr = inb(host->base + WBSD_CSR);
875 outb(pwr, host->base + WBSD_CSR);
883 setup = wbsd_read_index(host, WBSD_IDX_SETUP);
887 host->flags |= WBSD_FIGNORE_DETECT;
896 mod_timer(&host->ignore_timer, jiffies + HZ / 100);
899 wbsd_write_index(host, WBSD_IDX_SETUP, setup);
905 host->bus_width = ios->bus_width;
907 spin_unlock_bh(&host->lock);
912 struct wbsd_host *host = mmc_priv(mmc);
915 spin_lock_bh(&host->lock);
917 csr = inb(host->base + WBSD_CSR);
919 outb(csr, host->base + WBSD_CSR);
923 csr = inb(host->base + WBSD_CSR);
925 outb(csr, host->base + WBSD_CSR);
927 spin_unlock_bh(&host->lock);
950 struct wbsd_host *host = from_timer(host, t, ignore_timer);
952 BUG_ON(host == NULL);
956 spin_lock_bh(&host->lock);
958 host->flags &= ~WBSD_FIGNORE_DETECT;
964 tasklet_schedule(&host->card_tasklet);
966 spin_unlock_bh(&host->lock);
973 static inline struct mmc_data *wbsd_get_data(struct wbsd_host *host)
975 WARN_ON(!host->mrq);
976 if (!host->mrq)
979 WARN_ON(!host->mrq->cmd);
980 if (!host->mrq->cmd)
983 WARN_ON(!host->mrq->cmd->data);
984 if (!host->mrq->cmd->data)
987 return host->mrq->cmd->data;
992 struct wbsd_host *host = from_tasklet(host, t, card_tasklet);
996 spin_lock(&host->lock);
998 if (host->flags & WBSD_FIGNORE_DETECT) {
999 spin_unlock(&host->lock);
1003 csr = inb(host->base + WBSD_CSR);
1007 if (!(host->flags & WBSD_FCARD_PRESENT)) {
1009 host->flags |= WBSD_FCARD_PRESENT;
1013 } else if (host->flags & WBSD_FCARD_PRESENT) {
1015 host->flags &= ~WBSD_FCARD_PRESENT;
1017 if (host->mrq) {
1019 mmc_hostname(host->mmc));
1020 wbsd_reset(host);
1022 host->mrq->cmd->error = -ENOMEDIUM;
1023 tasklet_schedule(&host->finish_tasklet);
1033 spin_unlock(&host->lock);
1036 mmc_detect_change(host->mmc, msecs_to_jiffies(delay));
1041 struct wbsd_host *host = from_tasklet(host, t, fifo_tasklet);
1044 spin_lock(&host->lock);
1046 if (!host->mrq)
1049 data = wbsd_get_data(host);
1054 wbsd_fill_fifo(host);
1056 wbsd_empty_fifo(host);
1061 if (host->num_sg == 0) {
1062 wbsd_write_index(host, WBSD_IDX_FIFOEN, 0);
1063 tasklet_schedule(&host->finish_tasklet);
1067 spin_unlock(&host->lock);
1072 struct wbsd_host *host = from_tasklet(host, t, crc_tasklet);
1075 spin_lock(&host->lock);
1077 if (!host->mrq)
1080 data = wbsd_get_data(host);
1088 tasklet_schedule(&host->finish_tasklet);
1091 spin_unlock(&host->lock);
1096 struct wbsd_host *host = from_tasklet(host, t, timeout_tasklet);
1099 spin_lock(&host->lock);
1101 if (!host->mrq)
1104 data = wbsd_get_data(host);
1112 tasklet_schedule(&host->finish_tasklet);
1115 spin_unlock(&host->lock);
1120 struct wbsd_host *host = from_tasklet(host, t, finish_tasklet);
1123 spin_lock(&host->lock);
1125 WARN_ON(!host->mrq);
1126 if (!host->mrq)
1129 data = wbsd_get_data(host);
1133 wbsd_finish_data(host, data);
1136 spin_unlock(&host->lock);
1145 struct wbsd_host *host = dev_id;
1148 isr = inb(host->base + WBSD_ISR);
1156 host->isr |= isr;
1162 tasklet_schedule(&host->card_tasklet);
1164 tasklet_schedule(&host->fifo_tasklet);
1166 tasklet_hi_schedule(&host->crc_tasklet);
1168 tasklet_hi_schedule(&host->timeout_tasklet);
1170 tasklet_schedule(&host->finish_tasklet);
1188 struct wbsd_host *host;
1197 host = mmc_priv(mmc);
1198 host->mmc = mmc;
1200 host->dma = -1;
1203 * Set host parameters.
1211 spin_lock_init(&host->lock);
1216 timer_setup(&host->ignore_timer, wbsd_reset_ignore, 0);
1255 struct wbsd_host *host;
1261 host = mmc_priv(mmc);
1262 BUG_ON(host == NULL);
1264 del_timer_sync(&host->ignore_timer);
1273 static int wbsd_scan(struct wbsd_host *host)
1287 host->config = config_ports[i];
1288 host->unlock_code = unlock_codes[j];
1290 wbsd_unlock_config(host);
1298 wbsd_lock_config(host);
1302 host->chip_id = id;
1317 host->config = 0;
1318 host->unlock_code = 0;
1327 static int wbsd_request_region(struct wbsd_host *host, int base)
1335 host->base = base;
1340 static void wbsd_release_regions(struct wbsd_host *host)
1342 if (host->base)
1343 release_region(host->base, 8);
1345 host->base = 0;
1347 if (host->config)
1348 release_region(host->config, 2);
1350 host->config = 0;
1357 static void wbsd_request_dma(struct wbsd_host *host, int dma)
1369 host->dma_buffer = kmalloc(WBSD_DMA_SIZE,
1371 if (!host->dma_buffer)
1377 host->dma_addr = dma_map_single(mmc_dev(host->mmc), host->dma_buffer,
1379 if (dma_mapping_error(mmc_dev(host->mmc), host->dma_addr))
1385 if ((host->dma_addr & 0xffff) != 0)
1390 else if (host->dma_addr >= 0x1000000)
1393 host->dma = dma;
1403 dma_unmap_single(mmc_dev(host->mmc), host->dma_addr,
1405 host->dma_addr = 0;
1408 kfree(host->dma_buffer);
1409 host->dma_buffer = NULL;
1419 static void wbsd_release_dma(struct wbsd_host *host)
1422 * host->dma_addr is valid here iff host->dma_buffer is not NULL.
1424 if (host->dma_buffer) {
1425 dma_unmap_single(mmc_dev(host->mmc), host->dma_addr,
1427 kfree(host->dma_buffer);
1429 if (host->dma >= 0)
1430 free_dma(host->dma);
1432 host->dma = -1;
1433 host->dma_buffer = NULL;
1434 host->dma_addr = 0;
1441 static int wbsd_request_irq(struct wbsd_host *host, int irq)
1448 tasklet_setup(&host->card_tasklet, wbsd_tasklet_card);
1449 tasklet_setup(&host->fifo_tasklet, wbsd_tasklet_fifo);
1450 tasklet_setup(&host->crc_tasklet, wbsd_tasklet_crc);
1451 tasklet_setup(&host->timeout_tasklet, wbsd_tasklet_timeout);
1452 tasklet_setup(&host->finish_tasklet, wbsd_tasklet_finish);
1457 ret = request_irq(irq, wbsd_irq, IRQF_SHARED, DRIVER_NAME, host);
1461 host->irq = irq;
1466 static void wbsd_release_irq(struct wbsd_host *host)
1468 if (!host->irq)
1471 free_irq(host->irq, host);
1473 host->irq = 0;
1475 tasklet_kill(&host->card_tasklet);
1476 tasklet_kill(&host->fifo_tasklet);
1477 tasklet_kill(&host->crc_tasklet);
1478 tasklet_kill(&host->timeout_tasklet);
1479 tasklet_kill(&host->finish_tasklet);
1483 * Allocate all resources for the host.
1486 static int wbsd_request_resources(struct wbsd_host *host,
1494 ret = wbsd_request_region(host, base);
1501 ret = wbsd_request_irq(host, irq);
1508 wbsd_request_dma(host, dma);
1514 * Release all resources for the host.
1517 static void wbsd_release_resources(struct wbsd_host *host)
1519 wbsd_release_dma(host);
1520 wbsd_release_irq(host);
1521 wbsd_release_regions(host);
1528 static void wbsd_chip_config(struct wbsd_host *host)
1530 wbsd_unlock_config(host);
1535 wbsd_write_config(host, WBSD_CONF_SWRST, 1);
1536 wbsd_write_config(host, WBSD_CONF_SWRST, 0);
1541 wbsd_write_config(host, WBSD_CONF_DEVICE, DEVICE_SD);
1546 wbsd_write_config(host, WBSD_CONF_PINS, WBSD_PINS_DETECT_GP11);
1551 wbsd_write_config(host, WBSD_CONF_PORT_HI, host->base >> 8);
1552 wbsd_write_config(host, WBSD_CONF_PORT_LO, host->base & 0xff);
1554 wbsd_write_config(host, WBSD_CONF_IRQ, host->irq);
1556 if (host->dma >= 0)
1557 wbsd_write_config(host, WBSD_CONF_DRQ, host->dma);
1562 wbsd_write_config(host, WBSD_CONF_ENABLE, 1);
1563 wbsd_write_config(host, WBSD_CONF_POWER, 0x20);
1565 wbsd_lock_config(host);
1572 static int wbsd_chip_validate(struct wbsd_host *host)
1576 wbsd_unlock_config(host);
1581 wbsd_write_config(host, WBSD_CONF_DEVICE, DEVICE_SD);
1586 base = wbsd_read_config(host, WBSD_CONF_PORT_HI) << 8;
1587 base |= wbsd_read_config(host, WBSD_CONF_PORT_LO);
1589 irq = wbsd_read_config(host, WBSD_CONF_IRQ);
1591 dma = wbsd_read_config(host, WBSD_CONF_DRQ);
1593 wbsd_lock_config(host);
1598 if (base != host->base)
1600 if (irq != host->irq)
1602 if ((dma != host->dma) && (host->dma != -1))
1612 static void wbsd_chip_poweroff(struct wbsd_host *host)
1614 wbsd_unlock_config(host);
1616 wbsd_write_config(host, WBSD_CONF_DEVICE, DEVICE_SD);
1617 wbsd_write_config(host, WBSD_CONF_ENABLE, 0);
1619 wbsd_lock_config(host);
1631 struct wbsd_host *host = NULL;
1640 host = mmc_priv(mmc);
1645 ret = wbsd_scan(host);
1658 ret = wbsd_request_resources(host, base, irq, dma);
1660 wbsd_release_resources(host);
1669 if ((host->config != 0) && !wbsd_chip_validate(host)) {
1671 wbsd_chip_config(host);
1674 wbsd_chip_config(host);
1681 if (host->config) {
1682 wbsd_unlock_config(host);
1683 wbsd_write_config(host, WBSD_CONF_PME, 0xA0);
1684 wbsd_lock_config(host);
1695 wbsd_init_device(host);
1700 wbsd_chip_poweroff(host);
1702 wbsd_release_resources(host);
1708 if (host->chip_id != 0)
1709 printk(" id %x", (int)host->chip_id);
1710 printk(" at 0x%x irq %d", (int)host->base, (int)host->irq);
1711 if (host->dma >= 0)
1712 printk(" dma %d", (int)host->dma);
1725 struct wbsd_host *host;
1730 host = mmc_priv(mmc);
1738 wbsd_chip_poweroff(host);
1740 wbsd_release_resources(host);
1803 struct wbsd_host *host;
1810 host = mmc_priv(mmc);
1812 wbsd_chip_poweroff(host);
1819 struct wbsd_host *host;
1826 host = mmc_priv(mmc);
1828 wbsd_chip_config(host);
1835 wbsd_init_device(host);
1855 struct wbsd_host *host;
1862 host = mmc_priv(mmc);
1867 if (host->config != 0) {
1868 if (!wbsd_chip_validate(host)) {
1870 wbsd_chip_config(host);
1879 wbsd_init_device(host);