25 #include <linux/module.h>
37 #include <linux/slab.h>
44 #define DRIVER_NAME "wbsd"
47 pr_debug(DRIVER_NAME ": " x)
48 #define DBGF(f, x...) \
49 pr_debug(DRIVER_NAME " [%s()]: " f, __func__ , ##x)
67 static const int config_ports[] = { 0x2E, 0x4E };
68 static const int unlock_codes[] = { 0x83, 0x87 };
70 static const int valid_ids[] = {
75 static unsigned int param_nopnp = 0;
77 static const unsigned int param_nopnp = 1;
79 static unsigned int param_io = 0x248;
80 static unsigned int param_irq = 6;
81 static int param_dma = 2;
193 static void wbsd_reset(
struct wbsd_host *host)
209 unsigned long dmaflags;
211 if (host->
dma >= 0) {
217 clear_dma_ff(host->
dma);
231 spin_unlock(&host->
lock);
233 spin_lock(&host->
lock);
252 static inline int wbsd_next_sg(
struct wbsd_host *host)
271 static inline char *wbsd_sg_to_buffer(
struct wbsd_host *host)
273 return sg_virt(host->
cur_sg);
286 for (i = 0; i < len; i++) {
287 sgbuf = sg_virt(&sg[i]);
303 for (i = 0; i < len; i++) {
304 sgbuf = sg_virt(&sg[i]);
314 static inline void wbsd_get_short_reply(
struct wbsd_host *host,
332 static inline void wbsd_get_long_reply(
struct wbsd_host *host,
345 for (i = 0; i < 4; i++) {
373 for (i = 3; i >= 0; i--)
406 wbsd_get_long_reply(host, cmd);
408 wbsd_get_short_reply(host, cmd);
417 static void wbsd_empty_fifo(
struct wbsd_host *host)
429 buffer = wbsd_sg_to_buffer(host) + host->
offset;
447 for (i = 0; i < fifo; i++) {
462 if (!wbsd_next_sg(host))
465 buffer = wbsd_sg_to_buffer(host);
479 static void wbsd_fill_fifo(
struct wbsd_host *host)
492 buffer = wbsd_sg_to_buffer(host) + host->
offset;
503 if (fsr & WBSD_FIFO_EMPTY)
510 for (i = 16; i > fifo; i--) {
525 if (!wbsd_next_sg(host))
528 buffer = wbsd_sg_to_buffer(host);
545 unsigned long dmaflags;
578 blksize = data->
blksz + 2;
583 blksize = data->
blksz + 2 * 4;
605 if (host->
dma >= 0) {
610 if (size > 0x10000) {
620 wbsd_sg_to_dma(host, data);
627 clear_dma_ff(host->
dma);
652 wbsd_init_sg(host, data);
669 wbsd_fill_fifo(host);
678 unsigned long dmaflags;
688 wbsd_send_command(host, data->
stop);
701 if (host->
dma >= 0) {
712 clear_dma_ff(host->
dma);
724 pr_err(
"%s: Incomplete DMA transfer. "
736 wbsd_dma_to_sg(host, data);
745 wbsd_request_end(host, host->
mrq);
762 spin_lock_bh(&host->
lock);
805 #ifdef CONFIG_MMC_DEBUG
807 "supported by this controller.\n",
820 wbsd_prepare_data(host, cmd->
data);
822 if (cmd->
data->error)
826 wbsd_send_command(host, cmd);
840 spin_unlock_bh(&host->
lock);
846 wbsd_request_end(host, mrq);
848 spin_unlock_bh(&host->
lock);
856 spin_lock_bh(&host->
lock);
863 wbsd_init_device(host);
865 if (ios->
clock >= 24000000)
867 else if (ios->
clock >= 16000000)
869 else if (ios->
clock >= 12000000)
878 if (clk != host->
clk) {
904 setup &= ~WBSD_DAT3_H;
921 spin_unlock_bh(&host->
lock);
924 static int wbsd_get_ro(
struct mmc_host *mmc)
929 spin_lock_bh(&host->
lock);
941 spin_unlock_bh(&host->
lock);
947 .request = wbsd_request,
948 .set_ios = wbsd_set_ios,
949 .get_ro = wbsd_get_ro,
962 static void wbsd_reset_ignore(
unsigned long data)
968 DBG(
"Resetting card detection ignore\n");
970 spin_lock_bh(&host->
lock);
980 spin_unlock_bh(&host->
lock);
998 if (!host->
mrq->cmd->data)
1001 return host->
mrq->cmd->data;
1004 static void wbsd_tasklet_card(
unsigned long param)
1010 spin_lock(&host->
lock);
1013 spin_unlock(&host->
lock);
1022 DBG(
"Card inserted\n");
1028 DBG(
"Card removed\n");
1032 pr_err(
"%s: Card removed during transfer!\n",
1047 spin_unlock(&host->
lock);
1053 static void wbsd_tasklet_fifo(
unsigned long param)
1058 spin_lock(&host->
lock);
1063 data = wbsd_get_data(host);
1068 wbsd_fill_fifo(host);
1070 wbsd_empty_fifo(host);
1081 spin_unlock(&host->
lock);
1084 static void wbsd_tasklet_crc(
unsigned long param)
1089 spin_lock(&host->
lock);
1094 data = wbsd_get_data(host);
1098 DBGF(
"CRC error\n");
1105 spin_unlock(&host->
lock);
1108 static void wbsd_tasklet_timeout(
unsigned long param)
1113 spin_lock(&host->
lock);
1118 data = wbsd_get_data(host);
1129 spin_unlock(&host->
lock);
1132 static void wbsd_tasklet_finish(
unsigned long param)
1137 spin_lock(&host->
lock);
1143 data = wbsd_get_data(host);
1147 wbsd_finish_data(host, data);
1150 spin_unlock(&host->
lock);
1167 if (isr == 0xff || isr == 0x00)
1175 if (isr & WBSD_INT_CARD)
1181 if (isr & WBSD_INT_TIMEOUT)
1211 host = mmc_priv(mmc);
1219 mmc->
ops = &wbsd_ops;
1220 mmc->
f_min = 375000;
1221 mmc->
f_max = 24000000;
1268 static void wbsd_free_mmc(
struct device *dev)
1277 host = mmc_priv(mmc);
1300 for (i = 0; i <
ARRAY_SIZE(config_ports); i++) {
1304 for (j = 0; j <
ARRAY_SIZE(unlock_codes); j++) {
1307 host->
config = config_ports[
i];
1310 wbsd_unlock_config(host);
1313 id =
inb(config_ports[i] + 1) << 8;
1316 id |=
inb(config_ports[i] + 1);
1318 wbsd_lock_config(host);
1320 for (k = 0; k <
ARRAY_SIZE(valid_ids); k++) {
1321 if (
id == valid_ids[k]) {
1329 DBG(
"Unknown hardware (id %x) found at %x\n",
1330 id, config_ports[i]);
1360 static void wbsd_release_regions(
struct wbsd_host *host)
1403 if ((host->
dma_addr & 0xffff) != 0)
1408 else if (host->
dma_addr >= 0x1000000)
1433 "Falling back on FIFO.\n", dma);
1436 static void wbsd_release_dma(
struct wbsd_host *host)
1463 (
unsigned long)host);
1465 (
unsigned long)host);
1467 (
unsigned long)host);
1469 (
unsigned long)host);
1471 (
unsigned long)host);
1485 static void wbsd_release_irq(
struct wbsd_host *host)
1506 int base,
int irq,
int dma)
1513 ret = wbsd_request_region(host, base);
1520 ret = wbsd_request_irq(host, irq);
1527 wbsd_request_dma(host, dma);
1536 static void wbsd_release_resources(
struct wbsd_host *host)
1538 wbsd_release_dma(host);
1539 wbsd_release_irq(host);
1540 wbsd_release_regions(host);
1547 static void wbsd_chip_config(
struct wbsd_host *host)
1549 wbsd_unlock_config(host);
1584 wbsd_lock_config(host);
1591 static int wbsd_chip_validate(
struct wbsd_host *host)
1595 wbsd_unlock_config(host);
1612 wbsd_lock_config(host);
1617 if (base != host->
base)
1619 if (irq != host->
irq)
1621 if ((dma != host->
dma) && (host->
dma != -1))
1631 static void wbsd_chip_poweroff(
struct wbsd_host *host)
1633 wbsd_unlock_config(host);
1638 wbsd_lock_config(host);
1647 static int __devinit wbsd_init(
struct device *dev,
int base,
int irq,
int dma,
1654 ret = wbsd_alloc_mmc(dev);
1659 host = mmc_priv(mmc);
1664 ret = wbsd_scan(host);
1666 if (pnp && (ret == -
ENODEV)) {
1668 ": Unable to confirm device presence. You may "
1669 "experience lock-ups.\n");
1679 ret = wbsd_request_resources(host, base, irq, dma);
1681 wbsd_release_resources(host);
1690 if ((host->
config != 0) && !wbsd_chip_validate(host)) {
1692 ": PnP active but chip not configured! "
1693 "You probably have a buggy BIOS. "
1694 "Configuring chip manually.\n");
1695 wbsd_chip_config(host);
1698 wbsd_chip_config(host);
1706 wbsd_unlock_config(host);
1708 wbsd_lock_config(host);
1719 wbsd_init_device(host);
1746 host = mmc_priv(mmc);
1754 wbsd_chip_poweroff(host);
1756 wbsd_release_resources(host);
1768 return wbsd_init(&dev->
dev, param_io, param_irq, param_dma, 0);
1773 wbsd_shutdown(&dev->
dev, 0);
1792 io = pnp_port_start(pnpdev, 0);
1794 if (pnp_dma_valid(pnpdev, 0))
1799 DBGF(
"PnP resources: port %3x irq %d dma %d\n", io, irq, dma);
1801 return wbsd_init(&pnpdev->
dev, io, irq, dma, 1);
1806 wbsd_shutdown(&dev->
dev, 1);
1824 static int wbsd_resume(
struct wbsd_host *host)
1828 wbsd_init_device(host);
1836 struct mmc_host *mmc = platform_get_drvdata(dev);
1843 DBGF(
"Suspending...\n");
1845 host = mmc_priv(mmc);
1847 ret = wbsd_suspend(host, state);
1851 wbsd_chip_poweroff(host);
1858 struct mmc_host *mmc = platform_get_drvdata(dev);
1864 DBGF(
"Resuming...\n");
1866 host = mmc_priv(mmc);
1868 wbsd_chip_config(host);
1875 return wbsd_resume(host);
1888 DBGF(
"Suspending...\n");
1890 host = mmc_priv(mmc);
1892 return wbsd_suspend(host, state);
1903 DBGF(
"Resuming...\n");
1905 host = mmc_priv(mmc);
1911 if (!wbsd_chip_validate(host)) {
1913 ": PnP active but chip not configured! "
1914 "You probably have a buggy BIOS. "
1915 "Configuring chip manually.\n");
1916 wbsd_chip_config(host);
1925 return wbsd_resume(host);
1932 #define wbsd_platform_suspend NULL
1933 #define wbsd_platform_resume NULL
1935 #define wbsd_pnp_suspend NULL
1936 #define wbsd_pnp_resume NULL
1943 .probe = wbsd_probe,
1958 .id_table = pnp_dev_table,
1959 .probe = wbsd_pnp_probe,
1972 static int __init wbsd_drv_init(
void)
1977 ": Winbond W83L51xD SD/MMC card interface driver\n");
2011 static void __exit wbsd_drv_exit(
void)
2043 MODULE_PARM_DESC(nopnp,
"Scan for device instead of relying on PNP. (default 0)");
2045 MODULE_PARM_DESC(io,
"I/O base to allocate. Must be 8 byte aligned. (default 0x248)");
2047 MODULE_PARM_DESC(dma,
"DMA channel to allocate. -1 for no DMA. (default 2)");