aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/mmc/host
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2011-05-25 19:55:55 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2011-05-25 19:55:55 -0400
commit8c1c77ff9be27137fa7cbbf51efedef1a2ae915b (patch)
treecdbd09cac5f5d1c6eb5ec4257dc478c6acca70c5 /drivers/mmc/host
parentf3ae1c75203535f65448517e46c8dd70a56b6c71 (diff)
parent08ee80cc397ac1a306ca689a22ede954d92d0db1 (diff)
Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/cjb/mmc
* 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/cjb/mmc: (75 commits) mmc: core: eMMC bus width may not work on all platforms mmc: sdhci: Auto-CMD23 fixes. mmc: sdhci: Auto-CMD23 support. mmc: core: Block CMD23 support for UHS104/SDXC cards. mmc: sdhci: Implement MMC_CAP_CMD23 for SDHCI. mmc: core: Use CMD23 for multiblock transfers when we can. mmc: quirks: Add/remove quirks conditional support. mmc: Add new VUB300 USB-to-SD/SDIO/MMC driver mmc: sdhci-pxa: Add quirks for DMA/ADMA to match h/w mmc: core: duplicated trial with same freq in mmc_rescan_try_freq() mmc: core: add support for eMMC Dual Data Rate mmc: core: eMMC signal voltage does not use CMD11 mmc: sdhci-pxa: add platform code for UHS signaling mmc: sdhci: add hooks for setting UHS in platform specific code mmc: core: clear MMC_PM_KEEP_POWER flag on resume mmc: dw_mmc: fixed wrong regulator_enable in suspend/resume mmc: sdhi: allow powering down controller with no card inserted mmc: tmio: runtime suspend the controller, where possible mmc: sdhi: support up to 3 interrupt sources mmc: sdhi: print physical base address and clock rate ...
Diffstat (limited to 'drivers/mmc/host')
-rw-r--r--drivers/mmc/host/Kconfig33
-rw-r--r--drivers/mmc/host/Makefile1
-rw-r--r--drivers/mmc/host/dw_mmc.c6
-rw-r--r--drivers/mmc/host/sdhci-pci.c49
-rw-r--r--drivers/mmc/host/sdhci-pxa.c48
-rw-r--r--drivers/mmc/host/sdhci-tegra.c2
-rw-r--r--drivers/mmc/host/sdhci.c854
-rw-r--r--drivers/mmc/host/sdhci.h59
-rw-r--r--drivers/mmc/host/sh_mmcif.c126
-rw-r--r--drivers/mmc/host/sh_mobile_sdhi.c50
-rw-r--r--drivers/mmc/host/tmio_mmc.c32
-rw-r--r--drivers/mmc/host/tmio_mmc.h16
-rw-r--r--drivers/mmc/host/tmio_mmc_dma.c21
-rw-r--r--drivers/mmc/host/tmio_mmc_pio.c184
-rw-r--r--drivers/mmc/host/vub300.c2506
15 files changed, 3826 insertions, 161 deletions
diff --git a/drivers/mmc/host/Kconfig b/drivers/mmc/host/Kconfig
index 94df40531c38..56dbf3f6ad08 100644
--- a/drivers/mmc/host/Kconfig
+++ b/drivers/mmc/host/Kconfig
@@ -154,7 +154,7 @@ config MMC_SDHCI_DOVE
154 If unsure, say N. 154 If unsure, say N.
155 155
156config MMC_SDHCI_TEGRA 156config MMC_SDHCI_TEGRA
157 tristate "SDHCI platform support for the Tegra SD/MMC Controller" 157 bool "SDHCI platform support for the Tegra SD/MMC Controller"
158 depends on MMC_SDHCI_PLTFM && ARCH_TEGRA 158 depends on MMC_SDHCI_PLTFM && ARCH_TEGRA
159 select MMC_SDHCI_IO_ACCESSORS 159 select MMC_SDHCI_IO_ACCESSORS
160 help 160 help
@@ -535,6 +535,37 @@ config MMC_JZ4740
535 If you have a board based on such a SoC and with a SD/MMC slot, 535 If you have a board based on such a SoC and with a SD/MMC slot,
536 say Y or M here. 536 say Y or M here.
537 537
538config MMC_VUB300
539 tristate "VUB300 USB to SDIO/SD/MMC Host Controller support"
540 depends on USB
541 help
542 This selects support for Elan Digital Systems' VUB300 chip.
543
544 The VUB300 is a USB-SDIO Host Controller Interface chip
545 that enables the host computer to use SDIO/SD/MMC cards
546 via a USB 2.0 or USB 1.1 host.
547
548 The VUB300 chip will be found in both physically separate
549 USB to SDIO/SD/MMC adapters and embedded on some motherboards.
550
551 The VUB300 chip supports SD and MMC memory cards in addition
552 to single and multifunction SDIO cards.
553
554 Some SDIO cards will need a firmware file to be loaded and
555 sent to VUB300 chip in order to achieve better data throughput.
556 Download these "Offload Pseudocode" from Elan Digital Systems'
557 web-site http://www.elandigitalsystems.com/support/downloads.php
558 and put them in /lib/firmware. Note that without these additional
559 firmware files the VUB300 chip will still function, but not at
560 the best obtainable data rate.
561
562 To compile this mmc host controller driver as a module,
563 choose M here: the module will be called vub300.
564
565 If you have a computer with an embedded VUB300 chip
566 or if you intend connecting a USB adapter based on a
567 VUB300 chip say Y or M here.
568
538config MMC_USHC 569config MMC_USHC
539 tristate "USB SD Host Controller (USHC) support" 570 tristate "USB SD Host Controller (USHC) support"
540 depends on USB 571 depends on USB
diff --git a/drivers/mmc/host/Makefile b/drivers/mmc/host/Makefile
index 4f1df0aae574..58a5cf73d6e9 100644
--- a/drivers/mmc/host/Makefile
+++ b/drivers/mmc/host/Makefile
@@ -41,6 +41,7 @@ obj-$(CONFIG_SDH_BFIN) += bfin_sdh.o
41obj-$(CONFIG_MMC_DW) += dw_mmc.o 41obj-$(CONFIG_MMC_DW) += dw_mmc.o
42obj-$(CONFIG_MMC_SH_MMCIF) += sh_mmcif.o 42obj-$(CONFIG_MMC_SH_MMCIF) += sh_mmcif.o
43obj-$(CONFIG_MMC_JZ4740) += jz4740_mmc.o 43obj-$(CONFIG_MMC_JZ4740) += jz4740_mmc.o
44obj-$(CONFIG_MMC_VUB300) += vub300.o
44obj-$(CONFIG_MMC_USHC) += ushc.o 45obj-$(CONFIG_MMC_USHC) += ushc.o
45 46
46obj-$(CONFIG_MMC_SDHCI_PLTFM) += sdhci-platform.o 47obj-$(CONFIG_MMC_SDHCI_PLTFM) += sdhci-platform.o
diff --git a/drivers/mmc/host/dw_mmc.c b/drivers/mmc/host/dw_mmc.c
index 87e1f57ec9ba..66dcddb9c205 100644
--- a/drivers/mmc/host/dw_mmc.c
+++ b/drivers/mmc/host/dw_mmc.c
@@ -1769,9 +1769,6 @@ static int dw_mci_suspend(struct platform_device *pdev, pm_message_t mesg)
1769 int i, ret; 1769 int i, ret;
1770 struct dw_mci *host = platform_get_drvdata(pdev); 1770 struct dw_mci *host = platform_get_drvdata(pdev);
1771 1771
1772 if (host->vmmc)
1773 regulator_enable(host->vmmc);
1774
1775 for (i = 0; i < host->num_slots; i++) { 1772 for (i = 0; i < host->num_slots; i++) {
1776 struct dw_mci_slot *slot = host->slot[i]; 1773 struct dw_mci_slot *slot = host->slot[i];
1777 if (!slot) 1774 if (!slot)
@@ -1798,6 +1795,9 @@ static int dw_mci_resume(struct platform_device *pdev)
1798 int i, ret; 1795 int i, ret;
1799 struct dw_mci *host = platform_get_drvdata(pdev); 1796 struct dw_mci *host = platform_get_drvdata(pdev);
1800 1797
1798 if (host->vmmc)
1799 regulator_enable(host->vmmc);
1800
1801 if (host->dma_ops->init) 1801 if (host->dma_ops->init)
1802 host->dma_ops->init(host); 1802 host->dma_ops->init(host);
1803 1803
diff --git a/drivers/mmc/host/sdhci-pci.c b/drivers/mmc/host/sdhci-pci.c
index f8b5f37007b2..936bbca19c0a 100644
--- a/drivers/mmc/host/sdhci-pci.c
+++ b/drivers/mmc/host/sdhci-pci.c
@@ -18,11 +18,9 @@
18#include <linux/dma-mapping.h> 18#include <linux/dma-mapping.h>
19#include <linux/slab.h> 19#include <linux/slab.h>
20#include <linux/device.h> 20#include <linux/device.h>
21
22#include <linux/mmc/host.h> 21#include <linux/mmc/host.h>
23 22#include <linux/scatterlist.h>
24#include <asm/scatterlist.h> 23#include <linux/io.h>
25#include <asm/io.h>
26 24
27#include "sdhci.h" 25#include "sdhci.h"
28 26
@@ -46,14 +44,14 @@ struct sdhci_pci_slot;
46struct sdhci_pci_fixes { 44struct sdhci_pci_fixes {
47 unsigned int quirks; 45 unsigned int quirks;
48 46
49 int (*probe)(struct sdhci_pci_chip*); 47 int (*probe) (struct sdhci_pci_chip *);
50 48
51 int (*probe_slot)(struct sdhci_pci_slot*); 49 int (*probe_slot) (struct sdhci_pci_slot *);
52 void (*remove_slot)(struct sdhci_pci_slot*, int); 50 void (*remove_slot) (struct sdhci_pci_slot *, int);
53 51
54 int (*suspend)(struct sdhci_pci_chip*, 52 int (*suspend) (struct sdhci_pci_chip *,
55 pm_message_t); 53 pm_message_t);
56 int (*resume)(struct sdhci_pci_chip*); 54 int (*resume) (struct sdhci_pci_chip *);
57}; 55};
58 56
59struct sdhci_pci_slot { 57struct sdhci_pci_slot {
@@ -329,6 +327,11 @@ static int jmicron_probe(struct sdhci_pci_chip *chip)
329 return ret; 327 return ret;
330 } 328 }
331 329
330 /* quirk for unsable RO-detection on JM388 chips */
331 if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_SD ||
332 chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD)
333 chip->quirks |= SDHCI_QUIRK_UNSTABLE_RO_DETECT;
334
332 return 0; 335 return 0;
333} 336}
334 337
@@ -402,7 +405,7 @@ static int jmicron_suspend(struct sdhci_pci_chip *chip, pm_message_t state)
402 405
403 if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC || 406 if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC ||
404 chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD) { 407 chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD) {
405 for (i = 0;i < chip->num_slots;i++) 408 for (i = 0; i < chip->num_slots; i++)
406 jmicron_enable_mmc(chip->slots[i]->host, 0); 409 jmicron_enable_mmc(chip->slots[i]->host, 0);
407 } 410 }
408 411
@@ -415,7 +418,7 @@ static int jmicron_resume(struct sdhci_pci_chip *chip)
415 418
416 if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC || 419 if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC ||
417 chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD) { 420 chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD) {
418 for (i = 0;i < chip->num_slots;i++) 421 for (i = 0; i < chip->num_slots; i++)
419 jmicron_enable_mmc(chip->slots[i]->host, 1); 422 jmicron_enable_mmc(chip->slots[i]->host, 1);
420 } 423 }
421 424
@@ -798,7 +801,7 @@ static struct sdhci_ops sdhci_pci_ops = {
798 801
799#ifdef CONFIG_PM 802#ifdef CONFIG_PM
800 803
801static int sdhci_pci_suspend (struct pci_dev *pdev, pm_message_t state) 804static int sdhci_pci_suspend(struct pci_dev *pdev, pm_message_t state)
802{ 805{
803 struct sdhci_pci_chip *chip; 806 struct sdhci_pci_chip *chip;
804 struct sdhci_pci_slot *slot; 807 struct sdhci_pci_slot *slot;
@@ -810,7 +813,7 @@ static int sdhci_pci_suspend (struct pci_dev *pdev, pm_message_t state)
810 if (!chip) 813 if (!chip)
811 return 0; 814 return 0;
812 815
813 for (i = 0;i < chip->num_slots;i++) { 816 for (i = 0; i < chip->num_slots; i++) {
814 slot = chip->slots[i]; 817 slot = chip->slots[i];
815 if (!slot) 818 if (!slot)
816 continue; 819 continue;
@@ -818,7 +821,7 @@ static int sdhci_pci_suspend (struct pci_dev *pdev, pm_message_t state)
818 ret = sdhci_suspend_host(slot->host, state); 821 ret = sdhci_suspend_host(slot->host, state);
819 822
820 if (ret) { 823 if (ret) {
821 for (i--;i >= 0;i--) 824 for (i--; i >= 0; i--)
822 sdhci_resume_host(chip->slots[i]->host); 825 sdhci_resume_host(chip->slots[i]->host);
823 return ret; 826 return ret;
824 } 827 }
@@ -833,7 +836,7 @@ static int sdhci_pci_suspend (struct pci_dev *pdev, pm_message_t state)
833 if (chip->fixes && chip->fixes->suspend) { 836 if (chip->fixes && chip->fixes->suspend) {
834 ret = chip->fixes->suspend(chip, state); 837 ret = chip->fixes->suspend(chip, state);
835 if (ret) { 838 if (ret) {
836 for (i = chip->num_slots - 1;i >= 0;i--) 839 for (i = chip->num_slots - 1; i >= 0; i--)
837 sdhci_resume_host(chip->slots[i]->host); 840 sdhci_resume_host(chip->slots[i]->host);
838 return ret; 841 return ret;
839 } 842 }
@@ -855,7 +858,7 @@ static int sdhci_pci_suspend (struct pci_dev *pdev, pm_message_t state)
855 return 0; 858 return 0;
856} 859}
857 860
858static int sdhci_pci_resume (struct pci_dev *pdev) 861static int sdhci_pci_resume(struct pci_dev *pdev)
859{ 862{
860 struct sdhci_pci_chip *chip; 863 struct sdhci_pci_chip *chip;
861 struct sdhci_pci_slot *slot; 864 struct sdhci_pci_slot *slot;
@@ -877,7 +880,7 @@ static int sdhci_pci_resume (struct pci_dev *pdev)
877 return ret; 880 return ret;
878 } 881 }
879 882
880 for (i = 0;i < chip->num_slots;i++) { 883 for (i = 0; i < chip->num_slots; i++) {
881 slot = chip->slots[i]; 884 slot = chip->slots[i];
882 if (!slot) 885 if (!slot)
883 continue; 886 continue;
@@ -1059,7 +1062,7 @@ static int __devinit sdhci_pci_probe(struct pci_dev *pdev,
1059 } 1062 }
1060 1063
1061 chip->pdev = pdev; 1064 chip->pdev = pdev;
1062 chip->fixes = (const struct sdhci_pci_fixes*)ent->driver_data; 1065 chip->fixes = (const struct sdhci_pci_fixes *)ent->driver_data;
1063 if (chip->fixes) 1066 if (chip->fixes)
1064 chip->quirks = chip->fixes->quirks; 1067 chip->quirks = chip->fixes->quirks;
1065 chip->num_slots = slots; 1068 chip->num_slots = slots;
@@ -1074,10 +1077,10 @@ static int __devinit sdhci_pci_probe(struct pci_dev *pdev,
1074 1077
1075 slots = chip->num_slots; /* Quirk may have changed this */ 1078 slots = chip->num_slots; /* Quirk may have changed this */
1076 1079
1077 for (i = 0;i < slots;i++) { 1080 for (i = 0; i < slots; i++) {
1078 slot = sdhci_pci_probe_slot(pdev, chip, first_bar + i); 1081 slot = sdhci_pci_probe_slot(pdev, chip, first_bar + i);
1079 if (IS_ERR(slot)) { 1082 if (IS_ERR(slot)) {
1080 for (i--;i >= 0;i--) 1083 for (i--; i >= 0; i--)
1081 sdhci_pci_remove_slot(chip->slots[i]); 1084 sdhci_pci_remove_slot(chip->slots[i]);
1082 ret = PTR_ERR(slot); 1085 ret = PTR_ERR(slot);
1083 goto free; 1086 goto free;
@@ -1105,7 +1108,7 @@ static void __devexit sdhci_pci_remove(struct pci_dev *pdev)
1105 chip = pci_get_drvdata(pdev); 1108 chip = pci_get_drvdata(pdev);
1106 1109
1107 if (chip) { 1110 if (chip) {
1108 for (i = 0;i < chip->num_slots; i++) 1111 for (i = 0; i < chip->num_slots; i++)
1109 sdhci_pci_remove_slot(chip->slots[i]); 1112 sdhci_pci_remove_slot(chip->slots[i]);
1110 1113
1111 pci_set_drvdata(pdev, NULL); 1114 pci_set_drvdata(pdev, NULL);
@@ -1116,9 +1119,9 @@ static void __devexit sdhci_pci_remove(struct pci_dev *pdev)
1116} 1119}
1117 1120
1118static struct pci_driver sdhci_driver = { 1121static struct pci_driver sdhci_driver = {
1119 .name = "sdhci-pci", 1122 .name = "sdhci-pci",
1120 .id_table = pci_ids, 1123 .id_table = pci_ids,
1121 .probe = sdhci_pci_probe, 1124 .probe = sdhci_pci_probe,
1122 .remove = __devexit_p(sdhci_pci_remove), 1125 .remove = __devexit_p(sdhci_pci_remove),
1123 .suspend = sdhci_pci_suspend, 1126 .suspend = sdhci_pci_suspend,
1124 .resume = sdhci_pci_resume, 1127 .resume = sdhci_pci_resume,
diff --git a/drivers/mmc/host/sdhci-pxa.c b/drivers/mmc/host/sdhci-pxa.c
index 5a61208cbc66..089c9a68b7b1 100644
--- a/drivers/mmc/host/sdhci-pxa.c
+++ b/drivers/mmc/host/sdhci-pxa.c
@@ -69,7 +69,45 @@ static void set_clock(struct sdhci_host *host, unsigned int clock)
69 } 69 }
70} 70}
71 71
72static int set_uhs_signaling(struct sdhci_host *host, unsigned int uhs)
73{
74 u16 ctrl_2;
75
76 /*
77 * Set V18_EN -- UHS modes do not work without this.
78 * does not change signaling voltage
79 */
80 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
81
82 /* Select Bus Speed Mode for host */
83 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
84 switch (uhs) {
85 case MMC_TIMING_UHS_SDR12:
86 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
87 break;
88 case MMC_TIMING_UHS_SDR25:
89 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
90 break;
91 case MMC_TIMING_UHS_SDR50:
92 ctrl_2 |= SDHCI_CTRL_UHS_SDR50 | SDHCI_CTRL_VDD_180;
93 break;
94 case MMC_TIMING_UHS_SDR104:
95 ctrl_2 |= SDHCI_CTRL_UHS_SDR104 | SDHCI_CTRL_VDD_180;
96 break;
97 case MMC_TIMING_UHS_DDR50:
98 ctrl_2 |= SDHCI_CTRL_UHS_DDR50 | SDHCI_CTRL_VDD_180;
99 break;
100 }
101
102 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
103 pr_debug("%s:%s uhs = %d, ctrl_2 = %04X\n",
104 __func__, mmc_hostname(host->mmc), uhs, ctrl_2);
105
106 return 0;
107}
108
72static struct sdhci_ops sdhci_pxa_ops = { 109static struct sdhci_ops sdhci_pxa_ops = {
110 .set_uhs_signaling = set_uhs_signaling,
73 .set_clock = set_clock, 111 .set_clock = set_clock,
74}; 112};
75 113
@@ -136,11 +174,19 @@ static int __devinit sdhci_pxa_probe(struct platform_device *pdev)
136 host->hw_name = "MMC"; 174 host->hw_name = "MMC";
137 host->ops = &sdhci_pxa_ops; 175 host->ops = &sdhci_pxa_ops;
138 host->irq = irq; 176 host->irq = irq;
139 host->quirks = SDHCI_QUIRK_BROKEN_ADMA | SDHCI_QUIRK_BROKEN_TIMEOUT_VAL; 177 host->quirks = SDHCI_QUIRK_BROKEN_ADMA
178 | SDHCI_QUIRK_BROKEN_TIMEOUT_VAL
179 | SDHCI_QUIRK_32BIT_DMA_ADDR
180 | SDHCI_QUIRK_32BIT_DMA_SIZE
181 | SDHCI_QUIRK_32BIT_ADMA_SIZE
182 | SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC;
140 183
141 if (pdata->quirks) 184 if (pdata->quirks)
142 host->quirks |= pdata->quirks; 185 host->quirks |= pdata->quirks;
143 186
187 /* enable 1/8V DDR capable */
188 host->mmc->caps |= MMC_CAP_1_8V_DDR;
189
144 /* If slot design supports 8 bit data, indicate this to MMC. */ 190 /* If slot design supports 8 bit data, indicate this to MMC. */
145 if (pdata->flags & PXA_FLAG_SD_8_BIT_CAPABLE_SLOT) 191 if (pdata->flags & PXA_FLAG_SD_8_BIT_CAPABLE_SLOT)
146 host->mmc->caps |= MMC_CAP_8_BIT_DATA; 192 host->mmc->caps |= MMC_CAP_8_BIT_DATA;
diff --git a/drivers/mmc/host/sdhci-tegra.c b/drivers/mmc/host/sdhci-tegra.c
index f7e1f964395f..343c97edba32 100644
--- a/drivers/mmc/host/sdhci-tegra.c
+++ b/drivers/mmc/host/sdhci-tegra.c
@@ -184,6 +184,8 @@ static int tegra_sdhci_pltfm_init(struct sdhci_host *host,
184 clk_enable(clk); 184 clk_enable(clk);
185 pltfm_host->clk = clk; 185 pltfm_host->clk = clk;
186 186
187 host->mmc->pm_caps = plat->pm_flags;
188
187 if (plat->is_8bit) 189 if (plat->is_8bit)
188 host->mmc->caps |= MMC_CAP_8_BIT_DATA; 190 host->mmc->caps |= MMC_CAP_8_BIT_DATA;
189 191
diff --git a/drivers/mmc/host/sdhci.c b/drivers/mmc/host/sdhci.c
index 5d20661bc357..58d5436ff649 100644
--- a/drivers/mmc/host/sdhci.c
+++ b/drivers/mmc/host/sdhci.c
@@ -38,13 +38,16 @@
38#define SDHCI_USE_LEDS_CLASS 38#define SDHCI_USE_LEDS_CLASS
39#endif 39#endif
40 40
41#define MAX_TUNING_LOOP 40
42
41static unsigned int debug_quirks = 0; 43static unsigned int debug_quirks = 0;
42 44
43static void sdhci_prepare_data(struct sdhci_host *, struct mmc_data *);
44static void sdhci_finish_data(struct sdhci_host *); 45static void sdhci_finish_data(struct sdhci_host *);
45 46
46static void sdhci_send_command(struct sdhci_host *, struct mmc_command *); 47static void sdhci_send_command(struct sdhci_host *, struct mmc_command *);
47static void sdhci_finish_command(struct sdhci_host *); 48static void sdhci_finish_command(struct sdhci_host *);
49static int sdhci_execute_tuning(struct mmc_host *mmc);
50static void sdhci_tuning_timer(unsigned long data);
48 51
49static void sdhci_dumpregs(struct sdhci_host *host) 52static void sdhci_dumpregs(struct sdhci_host *host)
50{ 53{
@@ -84,6 +87,8 @@ static void sdhci_dumpregs(struct sdhci_host *host)
84 printk(KERN_DEBUG DRIVER_NAME ": Cmd: 0x%08x | Max curr: 0x%08x\n", 87 printk(KERN_DEBUG DRIVER_NAME ": Cmd: 0x%08x | Max curr: 0x%08x\n",
85 sdhci_readw(host, SDHCI_COMMAND), 88 sdhci_readw(host, SDHCI_COMMAND),
86 sdhci_readl(host, SDHCI_MAX_CURRENT)); 89 sdhci_readl(host, SDHCI_MAX_CURRENT));
90 printk(KERN_DEBUG DRIVER_NAME ": Host ctl2: 0x%08x\n",
91 sdhci_readw(host, SDHCI_HOST_CONTROL2));
87 92
88 if (host->flags & SDHCI_USE_ADMA) 93 if (host->flags & SDHCI_USE_ADMA)
89 printk(KERN_DEBUG DRIVER_NAME ": ADMA Err: 0x%08x | ADMA Ptr: 0x%08x\n", 94 printk(KERN_DEBUG DRIVER_NAME ": ADMA Err: 0x%08x | ADMA Ptr: 0x%08x\n",
@@ -157,6 +162,9 @@ static void sdhci_reset(struct sdhci_host *host, u8 mask)
157 if (host->quirks & SDHCI_QUIRK_RESTORE_IRQS_AFTER_RESET) 162 if (host->quirks & SDHCI_QUIRK_RESTORE_IRQS_AFTER_RESET)
158 ier = sdhci_readl(host, SDHCI_INT_ENABLE); 163 ier = sdhci_readl(host, SDHCI_INT_ENABLE);
159 164
165 if (host->ops->platform_reset_enter)
166 host->ops->platform_reset_enter(host, mask);
167
160 sdhci_writeb(host, mask, SDHCI_SOFTWARE_RESET); 168 sdhci_writeb(host, mask, SDHCI_SOFTWARE_RESET);
161 169
162 if (mask & SDHCI_RESET_ALL) 170 if (mask & SDHCI_RESET_ALL)
@@ -177,6 +185,9 @@ static void sdhci_reset(struct sdhci_host *host, u8 mask)
177 mdelay(1); 185 mdelay(1);
178 } 186 }
179 187
188 if (host->ops->platform_reset_exit)
189 host->ops->platform_reset_exit(host, mask);
190
180 if (host->quirks & SDHCI_QUIRK_RESTORE_IRQS_AFTER_RESET) 191 if (host->quirks & SDHCI_QUIRK_RESTORE_IRQS_AFTER_RESET)
181 sdhci_clear_set_irqs(host, SDHCI_INT_ALL_MASK, ier); 192 sdhci_clear_set_irqs(host, SDHCI_INT_ALL_MASK, ier);
182} 193}
@@ -591,9 +602,10 @@ static void sdhci_adma_table_post(struct sdhci_host *host,
591 data->sg_len, direction); 602 data->sg_len, direction);
592} 603}
593 604
594static u8 sdhci_calc_timeout(struct sdhci_host *host, struct mmc_data *data) 605static u8 sdhci_calc_timeout(struct sdhci_host *host, struct mmc_command *cmd)
595{ 606{
596 u8 count; 607 u8 count;
608 struct mmc_data *data = cmd->data;
597 unsigned target_timeout, current_timeout; 609 unsigned target_timeout, current_timeout;
598 610
599 /* 611 /*
@@ -605,9 +617,16 @@ static u8 sdhci_calc_timeout(struct sdhci_host *host, struct mmc_data *data)
605 if (host->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL) 617 if (host->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL)
606 return 0xE; 618 return 0xE;
607 619
620 /* Unspecified timeout, assume max */
621 if (!data && !cmd->cmd_timeout_ms)
622 return 0xE;
623
608 /* timeout in us */ 624 /* timeout in us */
609 target_timeout = data->timeout_ns / 1000 + 625 if (!data)
610 data->timeout_clks / host->clock; 626 target_timeout = cmd->cmd_timeout_ms * 1000;
627 else
628 target_timeout = data->timeout_ns / 1000 +
629 data->timeout_clks / host->clock;
611 630
612 if (host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK) 631 if (host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)
613 host->timeout_clk = host->clock / 1000; 632 host->timeout_clk = host->clock / 1000;
@@ -622,6 +641,7 @@ static u8 sdhci_calc_timeout(struct sdhci_host *host, struct mmc_data *data)
622 * => 641 * =>
623 * (1) / (2) > 2^6 642 * (1) / (2) > 2^6
624 */ 643 */
644 BUG_ON(!host->timeout_clk);
625 count = 0; 645 count = 0;
626 current_timeout = (1 << 13) * 1000 / host->timeout_clk; 646 current_timeout = (1 << 13) * 1000 / host->timeout_clk;
627 while (current_timeout < target_timeout) { 647 while (current_timeout < target_timeout) {
@@ -632,8 +652,8 @@ static u8 sdhci_calc_timeout(struct sdhci_host *host, struct mmc_data *data)
632 } 652 }
633 653
634 if (count >= 0xF) { 654 if (count >= 0xF) {
635 printk(KERN_WARNING "%s: Too large timeout requested!\n", 655 printk(KERN_WARNING "%s: Too large timeout requested for CMD%d!\n",
636 mmc_hostname(host->mmc)); 656 mmc_hostname(host->mmc), cmd->opcode);
637 count = 0xE; 657 count = 0xE;
638 } 658 }
639 659
@@ -651,15 +671,21 @@ static void sdhci_set_transfer_irqs(struct sdhci_host *host)
651 sdhci_clear_set_irqs(host, dma_irqs, pio_irqs); 671 sdhci_clear_set_irqs(host, dma_irqs, pio_irqs);
652} 672}
653 673
654static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_data *data) 674static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_command *cmd)
655{ 675{
656 u8 count; 676 u8 count;
657 u8 ctrl; 677 u8 ctrl;
678 struct mmc_data *data = cmd->data;
658 int ret; 679 int ret;
659 680
660 WARN_ON(host->data); 681 WARN_ON(host->data);
661 682
662 if (data == NULL) 683 if (data || (cmd->flags & MMC_RSP_BUSY)) {
684 count = sdhci_calc_timeout(host, cmd);
685 sdhci_writeb(host, count, SDHCI_TIMEOUT_CONTROL);
686 }
687
688 if (!data)
663 return; 689 return;
664 690
665 /* Sanity checks */ 691 /* Sanity checks */
@@ -669,9 +695,7 @@ static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_data *data)
669 695
670 host->data = data; 696 host->data = data;
671 host->data_early = 0; 697 host->data_early = 0;
672 698 host->data->bytes_xfered = 0;
673 count = sdhci_calc_timeout(host, data);
674 sdhci_writeb(host, count, SDHCI_TIMEOUT_CONTROL);
675 699
676 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) 700 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))
677 host->flags |= SDHCI_REQ_USE_DMA; 701 host->flags |= SDHCI_REQ_USE_DMA;
@@ -807,15 +831,17 @@ static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_data *data)
807 831
808 sdhci_set_transfer_irqs(host); 832 sdhci_set_transfer_irqs(host);
809 833
810 /* We do not handle DMA boundaries, so set it to max (512 KiB) */ 834 /* Set the DMA boundary value and block size */
811 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, data->blksz), SDHCI_BLOCK_SIZE); 835 sdhci_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
836 data->blksz), SDHCI_BLOCK_SIZE);
812 sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT); 837 sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
813} 838}
814 839
815static void sdhci_set_transfer_mode(struct sdhci_host *host, 840static void sdhci_set_transfer_mode(struct sdhci_host *host,
816 struct mmc_data *data) 841 struct mmc_command *cmd)
817{ 842{
818 u16 mode; 843 u16 mode;
844 struct mmc_data *data = cmd->data;
819 845
820 if (data == NULL) 846 if (data == NULL)
821 return; 847 return;
@@ -823,12 +849,20 @@ static void sdhci_set_transfer_mode(struct sdhci_host *host,
823 WARN_ON(!host->data); 849 WARN_ON(!host->data);
824 850
825 mode = SDHCI_TRNS_BLK_CNT_EN; 851 mode = SDHCI_TRNS_BLK_CNT_EN;
826 if (data->blocks > 1) { 852 if (mmc_op_multi(cmd->opcode) || data->blocks > 1) {
827 if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12) 853 mode |= SDHCI_TRNS_MULTI;
828 mode |= SDHCI_TRNS_MULTI | SDHCI_TRNS_ACMD12; 854 /*
829 else 855 * If we are sending CMD23, CMD12 never gets sent
830 mode |= SDHCI_TRNS_MULTI; 856 * on successful completion (so no Auto-CMD12).
857 */
858 if (!host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD12))
859 mode |= SDHCI_TRNS_AUTO_CMD12;
860 else if (host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD23)) {
861 mode |= SDHCI_TRNS_AUTO_CMD23;
862 sdhci_writel(host, host->mrq->sbc->arg, SDHCI_ARGUMENT2);
863 }
831 } 864 }
865
832 if (data->flags & MMC_DATA_READ) 866 if (data->flags & MMC_DATA_READ)
833 mode |= SDHCI_TRNS_READ; 867 mode |= SDHCI_TRNS_READ;
834 if (host->flags & SDHCI_REQ_USE_DMA) 868 if (host->flags & SDHCI_REQ_USE_DMA)
@@ -868,7 +902,15 @@ static void sdhci_finish_data(struct sdhci_host *host)
868 else 902 else
869 data->bytes_xfered = data->blksz * data->blocks; 903 data->bytes_xfered = data->blksz * data->blocks;
870 904
871 if (data->stop) { 905 /*
906 * Need to send CMD12 if -
907 * a) open-ended multiblock transfer (no CMD23)
908 * b) error in multiblock transfer
909 */
910 if (data->stop &&
911 (data->error ||
912 !host->mrq->sbc)) {
913
872 /* 914 /*
873 * The controller needs a reset of internal state machines 915 * The controller needs a reset of internal state machines
874 * upon error conditions. 916 * upon error conditions.
@@ -920,11 +962,11 @@ static void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
920 962
921 host->cmd = cmd; 963 host->cmd = cmd;
922 964
923 sdhci_prepare_data(host, cmd->data); 965 sdhci_prepare_data(host, cmd);
924 966
925 sdhci_writel(host, cmd->arg, SDHCI_ARGUMENT); 967 sdhci_writel(host, cmd->arg, SDHCI_ARGUMENT);
926 968
927 sdhci_set_transfer_mode(host, cmd->data); 969 sdhci_set_transfer_mode(host, cmd);
928 970
929 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) { 971 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
930 printk(KERN_ERR "%s: Unsupported response type!\n", 972 printk(KERN_ERR "%s: Unsupported response type!\n",
@@ -947,7 +989,9 @@ static void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
947 flags |= SDHCI_CMD_CRC; 989 flags |= SDHCI_CMD_CRC;
948 if (cmd->flags & MMC_RSP_OPCODE) 990 if (cmd->flags & MMC_RSP_OPCODE)
949 flags |= SDHCI_CMD_INDEX; 991 flags |= SDHCI_CMD_INDEX;
950 if (cmd->data) 992
993 /* CMD19 is special in that the Data Present Select should be set */
994 if (cmd->data || (cmd->opcode == MMC_SEND_TUNING_BLOCK))
951 flags |= SDHCI_CMD_DATA; 995 flags |= SDHCI_CMD_DATA;
952 996
953 sdhci_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND); 997 sdhci_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
@@ -977,19 +1021,27 @@ static void sdhci_finish_command(struct sdhci_host *host)
977 1021
978 host->cmd->error = 0; 1022 host->cmd->error = 0;
979 1023
980 if (host->data && host->data_early) 1024 /* Finished CMD23, now send actual command. */
981 sdhci_finish_data(host); 1025 if (host->cmd == host->mrq->sbc) {
1026 host->cmd = NULL;
1027 sdhci_send_command(host, host->mrq->cmd);
1028 } else {
982 1029
983 if (!host->cmd->data) 1030 /* Processed actual command. */
984 tasklet_schedule(&host->finish_tasklet); 1031 if (host->data && host->data_early)
1032 sdhci_finish_data(host);
985 1033
986 host->cmd = NULL; 1034 if (!host->cmd->data)
1035 tasklet_schedule(&host->finish_tasklet);
1036
1037 host->cmd = NULL;
1038 }
987} 1039}
988 1040
989static void sdhci_set_clock(struct sdhci_host *host, unsigned int clock) 1041static void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
990{ 1042{
991 int div; 1043 int div = 0; /* Initialized for compiler warning */
992 u16 clk; 1044 u16 clk = 0;
993 unsigned long timeout; 1045 unsigned long timeout;
994 1046
995 if (clock == host->clock) 1047 if (clock == host->clock)
@@ -1007,14 +1059,45 @@ static void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
1007 goto out; 1059 goto out;
1008 1060
1009 if (host->version >= SDHCI_SPEC_300) { 1061 if (host->version >= SDHCI_SPEC_300) {
1010 /* Version 3.00 divisors must be a multiple of 2. */ 1062 /*
1011 if (host->max_clk <= clock) 1063 * Check if the Host Controller supports Programmable Clock
1012 div = 1; 1064 * Mode.
1013 else { 1065 */
1014 for (div = 2; div < SDHCI_MAX_DIV_SPEC_300; div += 2) { 1066 if (host->clk_mul) {
1015 if ((host->max_clk / div) <= clock) 1067 u16 ctrl;
1016 break; 1068
1069 /*
1070 * We need to figure out whether the Host Driver needs
1071 * to select Programmable Clock Mode, or the value can
1072 * be set automatically by the Host Controller based on
1073 * the Preset Value registers.
1074 */
1075 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1076 if (!(ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
1077 for (div = 1; div <= 1024; div++) {
1078 if (((host->max_clk * host->clk_mul) /
1079 div) <= clock)
1080 break;
1081 }
1082 /*
1083 * Set Programmable Clock Mode in the Clock
1084 * Control register.
1085 */
1086 clk = SDHCI_PROG_CLOCK_MODE;
1087 div--;
1017 } 1088 }
1089 } else {
1090 /* Version 3.00 divisors must be a multiple of 2. */
1091 if (host->max_clk <= clock)
1092 div = 1;
1093 else {
1094 for (div = 2; div < SDHCI_MAX_DIV_SPEC_300;
1095 div += 2) {
1096 if ((host->max_clk / div) <= clock)
1097 break;
1098 }
1099 }
1100 div >>= 1;
1018 } 1101 }
1019 } else { 1102 } else {
1020 /* Version 2.00 divisors must be a power of 2. */ 1103 /* Version 2.00 divisors must be a power of 2. */
@@ -1022,10 +1105,10 @@ static void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
1022 if ((host->max_clk / div) <= clock) 1105 if ((host->max_clk / div) <= clock)
1023 break; 1106 break;
1024 } 1107 }
1108 div >>= 1;
1025 } 1109 }
1026 div >>= 1;
1027 1110
1028 clk = (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT; 1111 clk |= (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
1029 clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN) 1112 clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
1030 << SDHCI_DIVIDER_HI_SHIFT; 1113 << SDHCI_DIVIDER_HI_SHIFT;
1031 clk |= SDHCI_CLOCK_INT_EN; 1114 clk |= SDHCI_CLOCK_INT_EN;
@@ -1131,7 +1214,12 @@ static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1131#ifndef SDHCI_USE_LEDS_CLASS 1214#ifndef SDHCI_USE_LEDS_CLASS
1132 sdhci_activate_led(host); 1215 sdhci_activate_led(host);
1133#endif 1216#endif
1134 if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12) { 1217
1218 /*
1219 * Ensure we don't send the STOP for non-SET_BLOCK_COUNTED
1220 * requests if Auto-CMD12 is enabled.
1221 */
1222 if (!mrq->sbc && (host->flags & SDHCI_AUTO_CMD12)) {
1135 if (mrq->stop) { 1223 if (mrq->stop) {
1136 mrq->data->stop = NULL; 1224 mrq->data->stop = NULL;
1137 mrq->stop = NULL; 1225 mrq->stop = NULL;
@@ -1150,8 +1238,30 @@ static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1150 if (!present || host->flags & SDHCI_DEVICE_DEAD) { 1238 if (!present || host->flags & SDHCI_DEVICE_DEAD) {
1151 host->mrq->cmd->error = -ENOMEDIUM; 1239 host->mrq->cmd->error = -ENOMEDIUM;
1152 tasklet_schedule(&host->finish_tasklet); 1240 tasklet_schedule(&host->finish_tasklet);
1153 } else 1241 } else {
1154 sdhci_send_command(host, mrq->cmd); 1242 u32 present_state;
1243
1244 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1245 /*
1246 * Check if the re-tuning timer has already expired and there
1247 * is no on-going data transfer. If so, we need to execute
1248 * tuning procedure before sending command.
1249 */
1250 if ((host->flags & SDHCI_NEEDS_RETUNING) &&
1251 !(present_state & (SDHCI_DOING_WRITE | SDHCI_DOING_READ))) {
1252 spin_unlock_irqrestore(&host->lock, flags);
1253 sdhci_execute_tuning(mmc);
1254 spin_lock_irqsave(&host->lock, flags);
1255
1256 /* Restore original mmc_request structure */
1257 host->mrq = mrq;
1258 }
1259
1260 if (mrq->sbc && !(host->flags & SDHCI_AUTO_CMD23))
1261 sdhci_send_command(host, mrq->sbc);
1262 else
1263 sdhci_send_command(host, mrq->cmd);
1264 }
1155 1265
1156 mmiowb(); 1266 mmiowb();
1157 spin_unlock_irqrestore(&host->lock, flags); 1267 spin_unlock_irqrestore(&host->lock, flags);
@@ -1222,7 +1332,84 @@ static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1222 else 1332 else
1223 ctrl &= ~SDHCI_CTRL_HISPD; 1333 ctrl &= ~SDHCI_CTRL_HISPD;
1224 1334
1225 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL); 1335 if (host->version >= SDHCI_SPEC_300) {
1336 u16 clk, ctrl_2;
1337 unsigned int clock;
1338
1339 /* In case of UHS-I modes, set High Speed Enable */
1340 if ((ios->timing == MMC_TIMING_UHS_SDR50) ||
1341 (ios->timing == MMC_TIMING_UHS_SDR104) ||
1342 (ios->timing == MMC_TIMING_UHS_DDR50) ||
1343 (ios->timing == MMC_TIMING_UHS_SDR25) ||
1344 (ios->timing == MMC_TIMING_UHS_SDR12))
1345 ctrl |= SDHCI_CTRL_HISPD;
1346
1347 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1348 if (!(ctrl_2 & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
1349 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1350 /*
1351 * We only need to set Driver Strength if the
1352 * preset value enable is not set.
1353 */
1354 ctrl_2 &= ~SDHCI_CTRL_DRV_TYPE_MASK;
1355 if (ios->drv_type == MMC_SET_DRIVER_TYPE_A)
1356 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_A;
1357 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_C)
1358 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_C;
1359
1360 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
1361 } else {
1362 /*
1363 * According to SDHC Spec v3.00, if the Preset Value
1364 * Enable in the Host Control 2 register is set, we
1365 * need to reset SD Clock Enable before changing High
1366 * Speed Enable to avoid generating clock gliches.
1367 */
1368
1369 /* Reset SD Clock Enable */
1370 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1371 clk &= ~SDHCI_CLOCK_CARD_EN;
1372 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1373
1374 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1375
1376 /* Re-enable SD Clock */
1377 clock = host->clock;
1378 host->clock = 0;
1379 sdhci_set_clock(host, clock);
1380 }
1381
1382
1383 /* Reset SD Clock Enable */
1384 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1385 clk &= ~SDHCI_CLOCK_CARD_EN;
1386 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1387
1388 if (host->ops->set_uhs_signaling)
1389 host->ops->set_uhs_signaling(host, ios->timing);
1390 else {
1391 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1392 /* Select Bus Speed Mode for host */
1393 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
1394 if (ios->timing == MMC_TIMING_UHS_SDR12)
1395 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
1396 else if (ios->timing == MMC_TIMING_UHS_SDR25)
1397 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
1398 else if (ios->timing == MMC_TIMING_UHS_SDR50)
1399 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
1400 else if (ios->timing == MMC_TIMING_UHS_SDR104)
1401 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
1402 else if (ios->timing == MMC_TIMING_UHS_DDR50)
1403 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
1404 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
1405 }
1406
1407 /* Re-enable SD Clock */
1408 clock = host->clock;
1409 host->clock = 0;
1410 sdhci_set_clock(host, clock);
1411 } else
1412 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1226 1413
1227 /* 1414 /*
1228 * Some (ENE) controllers go apeshit on some ios operation, 1415 * Some (ENE) controllers go apeshit on some ios operation,
@@ -1237,14 +1424,11 @@ out:
1237 spin_unlock_irqrestore(&host->lock, flags); 1424 spin_unlock_irqrestore(&host->lock, flags);
1238} 1425}
1239 1426
1240static int sdhci_get_ro(struct mmc_host *mmc) 1427static int check_ro(struct sdhci_host *host)
1241{ 1428{
1242 struct sdhci_host *host;
1243 unsigned long flags; 1429 unsigned long flags;
1244 int is_readonly; 1430 int is_readonly;
1245 1431
1246 host = mmc_priv(mmc);
1247
1248 spin_lock_irqsave(&host->lock, flags); 1432 spin_lock_irqsave(&host->lock, flags);
1249 1433
1250 if (host->flags & SDHCI_DEVICE_DEAD) 1434 if (host->flags & SDHCI_DEVICE_DEAD)
@@ -1262,6 +1446,29 @@ static int sdhci_get_ro(struct mmc_host *mmc)
1262 !is_readonly : is_readonly; 1446 !is_readonly : is_readonly;
1263} 1447}
1264 1448
1449#define SAMPLE_COUNT 5
1450
1451static int sdhci_get_ro(struct mmc_host *mmc)
1452{
1453 struct sdhci_host *host;
1454 int i, ro_count;
1455
1456 host = mmc_priv(mmc);
1457
1458 if (!(host->quirks & SDHCI_QUIRK_UNSTABLE_RO_DETECT))
1459 return check_ro(host);
1460
1461 ro_count = 0;
1462 for (i = 0; i < SAMPLE_COUNT; i++) {
1463 if (check_ro(host)) {
1464 if (++ro_count > SAMPLE_COUNT / 2)
1465 return 1;
1466 }
1467 msleep(30);
1468 }
1469 return 0;
1470}
1471
1265static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable) 1472static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
1266{ 1473{
1267 struct sdhci_host *host; 1474 struct sdhci_host *host;
@@ -1284,11 +1491,322 @@ out:
1284 spin_unlock_irqrestore(&host->lock, flags); 1491 spin_unlock_irqrestore(&host->lock, flags);
1285} 1492}
1286 1493
1494static int sdhci_start_signal_voltage_switch(struct mmc_host *mmc,
1495 struct mmc_ios *ios)
1496{
1497 struct sdhci_host *host;
1498 u8 pwr;
1499 u16 clk, ctrl;
1500 u32 present_state;
1501
1502 host = mmc_priv(mmc);
1503
1504 /*
1505 * Signal Voltage Switching is only applicable for Host Controllers
1506 * v3.00 and above.
1507 */
1508 if (host->version < SDHCI_SPEC_300)
1509 return 0;
1510
1511 /*
1512 * We first check whether the request is to set signalling voltage
1513 * to 3.3V. If so, we change the voltage to 3.3V and return quickly.
1514 */
1515 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1516 if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_330) {
1517 /* Set 1.8V Signal Enable in the Host Control2 register to 0 */
1518 ctrl &= ~SDHCI_CTRL_VDD_180;
1519 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1520
1521 /* Wait for 5ms */
1522 usleep_range(5000, 5500);
1523
1524 /* 3.3V regulator output should be stable within 5 ms */
1525 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1526 if (!(ctrl & SDHCI_CTRL_VDD_180))
1527 return 0;
1528 else {
1529 printk(KERN_INFO DRIVER_NAME ": Switching to 3.3V "
1530 "signalling voltage failed\n");
1531 return -EIO;
1532 }
1533 } else if (!(ctrl & SDHCI_CTRL_VDD_180) &&
1534 (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_180)) {
1535 /* Stop SDCLK */
1536 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1537 clk &= ~SDHCI_CLOCK_CARD_EN;
1538 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1539
1540 /* Check whether DAT[3:0] is 0000 */
1541 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1542 if (!((present_state & SDHCI_DATA_LVL_MASK) >>
1543 SDHCI_DATA_LVL_SHIFT)) {
1544 /*
1545 * Enable 1.8V Signal Enable in the Host Control2
1546 * register
1547 */
1548 ctrl |= SDHCI_CTRL_VDD_180;
1549 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1550
1551 /* Wait for 5ms */
1552 usleep_range(5000, 5500);
1553
1554 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1555 if (ctrl & SDHCI_CTRL_VDD_180) {
1556 /* Provide SDCLK again and wait for 1ms*/
1557 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1558 clk |= SDHCI_CLOCK_CARD_EN;
1559 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1560 usleep_range(1000, 1500);
1561
1562 /*
1563 * If DAT[3:0] level is 1111b, then the card
1564 * was successfully switched to 1.8V signaling.
1565 */
1566 present_state = sdhci_readl(host,
1567 SDHCI_PRESENT_STATE);
1568 if ((present_state & SDHCI_DATA_LVL_MASK) ==
1569 SDHCI_DATA_LVL_MASK)
1570 return 0;
1571 }
1572 }
1573
1574 /*
1575 * If we are here, that means the switch to 1.8V signaling
1576 * failed. We power cycle the card, and retry initialization
1577 * sequence by setting S18R to 0.
1578 */
1579 pwr = sdhci_readb(host, SDHCI_POWER_CONTROL);
1580 pwr &= ~SDHCI_POWER_ON;
1581 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1582
1583 /* Wait for 1ms as per the spec */
1584 usleep_range(1000, 1500);
1585 pwr |= SDHCI_POWER_ON;
1586 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1587
1588 printk(KERN_INFO DRIVER_NAME ": Switching to 1.8V signalling "
1589 "voltage failed, retrying with S18R set to 0\n");
1590 return -EAGAIN;
1591 } else
1592 /* No signal voltage switch required */
1593 return 0;
1594}
1595
1596static int sdhci_execute_tuning(struct mmc_host *mmc)
1597{
1598 struct sdhci_host *host;
1599 u16 ctrl;
1600 u32 ier;
1601 int tuning_loop_counter = MAX_TUNING_LOOP;
1602 unsigned long timeout;
1603 int err = 0;
1604
1605 host = mmc_priv(mmc);
1606
1607 disable_irq(host->irq);
1608 spin_lock(&host->lock);
1609
1610 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1611
1612 /*
1613 * Host Controller needs tuning only in case of SDR104 mode
1614 * and for SDR50 mode when Use Tuning for SDR50 is set in
1615 * Capabilities register.
1616 */
1617 if (((ctrl & SDHCI_CTRL_UHS_MASK) == SDHCI_CTRL_UHS_SDR104) ||
1618 (((ctrl & SDHCI_CTRL_UHS_MASK) == SDHCI_CTRL_UHS_SDR50) &&
1619 (host->flags & SDHCI_SDR50_NEEDS_TUNING)))
1620 ctrl |= SDHCI_CTRL_EXEC_TUNING;
1621 else {
1622 spin_unlock(&host->lock);
1623 enable_irq(host->irq);
1624 return 0;
1625 }
1626
1627 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1628
1629 /*
1630 * As per the Host Controller spec v3.00, tuning command
1631 * generates Buffer Read Ready interrupt, so enable that.
1632 *
1633 * Note: The spec clearly says that when tuning sequence
1634 * is being performed, the controller does not generate
1635 * interrupts other than Buffer Read Ready interrupt. But
1636 * to make sure we don't hit a controller bug, we _only_
1637 * enable Buffer Read Ready interrupt here.
1638 */
1639 ier = sdhci_readl(host, SDHCI_INT_ENABLE);
1640 sdhci_clear_set_irqs(host, ier, SDHCI_INT_DATA_AVAIL);
1641
1642 /*
1643 * Issue CMD19 repeatedly till Execute Tuning is set to 0 or the number
1644 * of loops reaches 40 times or a timeout of 150ms occurs.
1645 */
1646 timeout = 150;
1647 do {
1648 struct mmc_command cmd = {0};
1649 struct mmc_request mrq = {0};
1650
1651 if (!tuning_loop_counter && !timeout)
1652 break;
1653
1654 cmd.opcode = MMC_SEND_TUNING_BLOCK;
1655 cmd.arg = 0;
1656 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1657 cmd.retries = 0;
1658 cmd.data = NULL;
1659 cmd.error = 0;
1660
1661 mrq.cmd = &cmd;
1662 host->mrq = &mrq;
1663
1664 /*
1665 * In response to CMD19, the card sends 64 bytes of tuning
1666 * block to the Host Controller. So we set the block size
1667 * to 64 here.
1668 */
1669 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64), SDHCI_BLOCK_SIZE);
1670
1671 /*
1672 * The tuning block is sent by the card to the host controller.
1673 * So we set the TRNS_READ bit in the Transfer Mode register.
1674 * This also takes care of setting DMA Enable and Multi Block
1675 * Select in the same register to 0.
1676 */
1677 sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
1678
1679 sdhci_send_command(host, &cmd);
1680
1681 host->cmd = NULL;
1682 host->mrq = NULL;
1683
1684 spin_unlock(&host->lock);
1685 enable_irq(host->irq);
1686
1687 /* Wait for Buffer Read Ready interrupt */
1688 wait_event_interruptible_timeout(host->buf_ready_int,
1689 (host->tuning_done == 1),
1690 msecs_to_jiffies(50));
1691 disable_irq(host->irq);
1692 spin_lock(&host->lock);
1693
1694 if (!host->tuning_done) {
1695 printk(KERN_INFO DRIVER_NAME ": Timeout waiting for "
1696 "Buffer Read Ready interrupt during tuning "
1697 "procedure, falling back to fixed sampling "
1698 "clock\n");
1699 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1700 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
1701 ctrl &= ~SDHCI_CTRL_EXEC_TUNING;
1702 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1703
1704 err = -EIO;
1705 goto out;
1706 }
1707
1708 host->tuning_done = 0;
1709
1710 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1711 tuning_loop_counter--;
1712 timeout--;
1713 mdelay(1);
1714 } while (ctrl & SDHCI_CTRL_EXEC_TUNING);
1715
1716 /*
1717 * The Host Driver has exhausted the maximum number of loops allowed,
1718 * so use fixed sampling frequency.
1719 */
1720 if (!tuning_loop_counter || !timeout) {
1721 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
1722 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1723 } else {
1724 if (!(ctrl & SDHCI_CTRL_TUNED_CLK)) {
1725 printk(KERN_INFO DRIVER_NAME ": Tuning procedure"
1726 " failed, falling back to fixed sampling"
1727 " clock\n");
1728 err = -EIO;
1729 }
1730 }
1731
1732out:
1733 /*
1734 * If this is the very first time we are here, we start the retuning
1735 * timer. Since only during the first time, SDHCI_NEEDS_RETUNING
1736 * flag won't be set, we check this condition before actually starting
1737 * the timer.
1738 */
1739 if (!(host->flags & SDHCI_NEEDS_RETUNING) && host->tuning_count &&
1740 (host->tuning_mode == SDHCI_TUNING_MODE_1)) {
1741 mod_timer(&host->tuning_timer, jiffies +
1742 host->tuning_count * HZ);
1743 /* Tuning mode 1 limits the maximum data length to 4MB */
1744 mmc->max_blk_count = (4 * 1024 * 1024) / mmc->max_blk_size;
1745 } else {
1746 host->flags &= ~SDHCI_NEEDS_RETUNING;
1747 /* Reload the new initial value for timer */
1748 if (host->tuning_mode == SDHCI_TUNING_MODE_1)
1749 mod_timer(&host->tuning_timer, jiffies +
1750 host->tuning_count * HZ);
1751 }
1752
1753 /*
1754 * In case tuning fails, host controllers which support re-tuning can
1755 * try tuning again at a later time, when the re-tuning timer expires.
1756 * So for these controllers, we return 0. Since there might be other
1757 * controllers who do not have this capability, we return error for
1758 * them.
1759 */
1760 if (err && host->tuning_count &&
1761 host->tuning_mode == SDHCI_TUNING_MODE_1)
1762 err = 0;
1763
1764 sdhci_clear_set_irqs(host, SDHCI_INT_DATA_AVAIL, ier);
1765 spin_unlock(&host->lock);
1766 enable_irq(host->irq);
1767
1768 return err;
1769}
1770
1771static void sdhci_enable_preset_value(struct mmc_host *mmc, bool enable)
1772{
1773 struct sdhci_host *host;
1774 u16 ctrl;
1775 unsigned long flags;
1776
1777 host = mmc_priv(mmc);
1778
1779 /* Host Controller v3.00 defines preset value registers */
1780 if (host->version < SDHCI_SPEC_300)
1781 return;
1782
1783 spin_lock_irqsave(&host->lock, flags);
1784
1785 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1786
1787 /*
1788 * We only enable or disable Preset Value if they are not already
1789 * enabled or disabled respectively. Otherwise, we bail out.
1790 */
1791 if (enable && !(ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
1792 ctrl |= SDHCI_CTRL_PRESET_VAL_ENABLE;
1793 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1794 } else if (!enable && (ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
1795 ctrl &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
1796 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1797 }
1798
1799 spin_unlock_irqrestore(&host->lock, flags);
1800}
1801
1287static const struct mmc_host_ops sdhci_ops = { 1802static const struct mmc_host_ops sdhci_ops = {
1288 .request = sdhci_request, 1803 .request = sdhci_request,
1289 .set_ios = sdhci_set_ios, 1804 .set_ios = sdhci_set_ios,
1290 .get_ro = sdhci_get_ro, 1805 .get_ro = sdhci_get_ro,
1291 .enable_sdio_irq = sdhci_enable_sdio_irq, 1806 .enable_sdio_irq = sdhci_enable_sdio_irq,
1807 .start_signal_voltage_switch = sdhci_start_signal_voltage_switch,
1808 .execute_tuning = sdhci_execute_tuning,
1809 .enable_preset_value = sdhci_enable_preset_value,
1292}; 1810};
1293 1811
1294/*****************************************************************************\ 1812/*****************************************************************************\
@@ -1345,6 +1863,9 @@ static void sdhci_tasklet_finish(unsigned long param)
1345 1863
1346 del_timer(&host->timer); 1864 del_timer(&host->timer);
1347 1865
1866 if (host->version >= SDHCI_SPEC_300)
1867 del_timer(&host->tuning_timer);
1868
1348 mrq = host->mrq; 1869 mrq = host->mrq;
1349 1870
1350 /* 1871 /*
@@ -1418,6 +1939,20 @@ static void sdhci_timeout_timer(unsigned long data)
1418 spin_unlock_irqrestore(&host->lock, flags); 1939 spin_unlock_irqrestore(&host->lock, flags);
1419} 1940}
1420 1941
1942static void sdhci_tuning_timer(unsigned long data)
1943{
1944 struct sdhci_host *host;
1945 unsigned long flags;
1946
1947 host = (struct sdhci_host *)data;
1948
1949 spin_lock_irqsave(&host->lock, flags);
1950
1951 host->flags |= SDHCI_NEEDS_RETUNING;
1952
1953 spin_unlock_irqrestore(&host->lock, flags);
1954}
1955
1421/*****************************************************************************\ 1956/*****************************************************************************\
1422 * * 1957 * *
1423 * Interrupt handling * 1958 * Interrupt handling *
@@ -1506,6 +2041,16 @@ static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
1506{ 2041{
1507 BUG_ON(intmask == 0); 2042 BUG_ON(intmask == 0);
1508 2043
2044 /* CMD19 generates _only_ Buffer Read Ready interrupt */
2045 if (intmask & SDHCI_INT_DATA_AVAIL) {
2046 if (SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND)) ==
2047 MMC_SEND_TUNING_BLOCK) {
2048 host->tuning_done = 1;
2049 wake_up(&host->buf_ready_int);
2050 return;
2051 }
2052 }
2053
1509 if (!host->data) { 2054 if (!host->data) {
1510 /* 2055 /*
1511 * The "data complete" interrupt is also used to 2056 * The "data complete" interrupt is also used to
@@ -1551,10 +2096,28 @@ static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
1551 * We currently don't do anything fancy with DMA 2096 * We currently don't do anything fancy with DMA
1552 * boundaries, but as we can't disable the feature 2097 * boundaries, but as we can't disable the feature
1553 * we need to at least restart the transfer. 2098 * we need to at least restart the transfer.
2099 *
2100 * According to the spec sdhci_readl(host, SDHCI_DMA_ADDRESS)
2101 * should return a valid address to continue from, but as
2102 * some controllers are faulty, don't trust them.
1554 */ 2103 */
1555 if (intmask & SDHCI_INT_DMA_END) 2104 if (intmask & SDHCI_INT_DMA_END) {
1556 sdhci_writel(host, sdhci_readl(host, SDHCI_DMA_ADDRESS), 2105 u32 dmastart, dmanow;
1557 SDHCI_DMA_ADDRESS); 2106 dmastart = sg_dma_address(host->data->sg);
2107 dmanow = dmastart + host->data->bytes_xfered;
2108 /*
2109 * Force update to the next DMA block boundary.
2110 */
2111 dmanow = (dmanow &
2112 ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) +
2113 SDHCI_DEFAULT_BOUNDARY_SIZE;
2114 host->data->bytes_xfered = dmanow - dmastart;
2115 DBG("%s: DMA base 0x%08x, transferred 0x%06x bytes,"
2116 " next 0x%08x\n",
2117 mmc_hostname(host->mmc), dmastart,
2118 host->data->bytes_xfered, dmanow);
2119 sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS);
2120 }
1558 2121
1559 if (intmask & SDHCI_INT_DATA_END) { 2122 if (intmask & SDHCI_INT_DATA_END) {
1560 if (host->cmd) { 2123 if (host->cmd) {
@@ -1664,6 +2227,14 @@ int sdhci_suspend_host(struct sdhci_host *host, pm_message_t state)
1664 2227
1665 sdhci_disable_card_detection(host); 2228 sdhci_disable_card_detection(host);
1666 2229
2230 /* Disable tuning since we are suspending */
2231 if (host->version >= SDHCI_SPEC_300 && host->tuning_count &&
2232 host->tuning_mode == SDHCI_TUNING_MODE_1) {
2233 host->flags &= ~SDHCI_NEEDS_RETUNING;
2234 mod_timer(&host->tuning_timer, jiffies +
2235 host->tuning_count * HZ);
2236 }
2237
1667 ret = mmc_suspend_host(host->mmc); 2238 ret = mmc_suspend_host(host->mmc);
1668 if (ret) 2239 if (ret)
1669 return ret; 2240 return ret;
@@ -1705,6 +2276,11 @@ int sdhci_resume_host(struct sdhci_host *host)
1705 ret = mmc_resume_host(host->mmc); 2276 ret = mmc_resume_host(host->mmc);
1706 sdhci_enable_card_detection(host); 2277 sdhci_enable_card_detection(host);
1707 2278
2279 /* Set the re-tuning expiration flag */
2280 if ((host->version >= SDHCI_SPEC_300) && host->tuning_count &&
2281 (host->tuning_mode == SDHCI_TUNING_MODE_1))
2282 host->flags |= SDHCI_NEEDS_RETUNING;
2283
1708 return ret; 2284 return ret;
1709} 2285}
1710 2286
@@ -1751,7 +2327,9 @@ EXPORT_SYMBOL_GPL(sdhci_alloc_host);
1751int sdhci_add_host(struct sdhci_host *host) 2327int sdhci_add_host(struct sdhci_host *host)
1752{ 2328{
1753 struct mmc_host *mmc; 2329 struct mmc_host *mmc;
1754 unsigned int caps, ocr_avail; 2330 u32 caps[2];
2331 u32 max_current_caps;
2332 unsigned int ocr_avail;
1755 int ret; 2333 int ret;
1756 2334
1757 WARN_ON(host == NULL); 2335 WARN_ON(host == NULL);
@@ -1774,12 +2352,15 @@ int sdhci_add_host(struct sdhci_host *host)
1774 host->version); 2352 host->version);
1775 } 2353 }
1776 2354
1777 caps = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ? host->caps : 2355 caps[0] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ? host->caps :
1778 sdhci_readl(host, SDHCI_CAPABILITIES); 2356 sdhci_readl(host, SDHCI_CAPABILITIES);
1779 2357
2358 caps[1] = (host->version >= SDHCI_SPEC_300) ?
2359 sdhci_readl(host, SDHCI_CAPABILITIES_1) : 0;
2360
1780 if (host->quirks & SDHCI_QUIRK_FORCE_DMA) 2361 if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
1781 host->flags |= SDHCI_USE_SDMA; 2362 host->flags |= SDHCI_USE_SDMA;
1782 else if (!(caps & SDHCI_CAN_DO_SDMA)) 2363 else if (!(caps[0] & SDHCI_CAN_DO_SDMA))
1783 DBG("Controller doesn't have SDMA capability\n"); 2364 DBG("Controller doesn't have SDMA capability\n");
1784 else 2365 else
1785 host->flags |= SDHCI_USE_SDMA; 2366 host->flags |= SDHCI_USE_SDMA;
@@ -1790,7 +2371,8 @@ int sdhci_add_host(struct sdhci_host *host)
1790 host->flags &= ~SDHCI_USE_SDMA; 2371 host->flags &= ~SDHCI_USE_SDMA;
1791 } 2372 }
1792 2373
1793 if ((host->version >= SDHCI_SPEC_200) && (caps & SDHCI_CAN_DO_ADMA2)) 2374 if ((host->version >= SDHCI_SPEC_200) &&
2375 (caps[0] & SDHCI_CAN_DO_ADMA2))
1794 host->flags |= SDHCI_USE_ADMA; 2376 host->flags |= SDHCI_USE_ADMA;
1795 2377
1796 if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) && 2378 if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
@@ -1840,10 +2422,10 @@ int sdhci_add_host(struct sdhci_host *host)
1840 } 2422 }
1841 2423
1842 if (host->version >= SDHCI_SPEC_300) 2424 if (host->version >= SDHCI_SPEC_300)
1843 host->max_clk = (caps & SDHCI_CLOCK_V3_BASE_MASK) 2425 host->max_clk = (caps[0] & SDHCI_CLOCK_V3_BASE_MASK)
1844 >> SDHCI_CLOCK_BASE_SHIFT; 2426 >> SDHCI_CLOCK_BASE_SHIFT;
1845 else 2427 else
1846 host->max_clk = (caps & SDHCI_CLOCK_BASE_MASK) 2428 host->max_clk = (caps[0] & SDHCI_CLOCK_BASE_MASK)
1847 >> SDHCI_CLOCK_BASE_SHIFT; 2429 >> SDHCI_CLOCK_BASE_SHIFT;
1848 2430
1849 host->max_clk *= 1000000; 2431 host->max_clk *= 1000000;
@@ -1859,7 +2441,7 @@ int sdhci_add_host(struct sdhci_host *host)
1859 } 2441 }
1860 2442
1861 host->timeout_clk = 2443 host->timeout_clk =
1862 (caps & SDHCI_TIMEOUT_CLK_MASK) >> SDHCI_TIMEOUT_CLK_SHIFT; 2444 (caps[0] & SDHCI_TIMEOUT_CLK_MASK) >> SDHCI_TIMEOUT_CLK_SHIFT;
1863 if (host->timeout_clk == 0) { 2445 if (host->timeout_clk == 0) {
1864 if (host->ops->get_timeout_clock) { 2446 if (host->ops->get_timeout_clock) {
1865 host->timeout_clk = host->ops->get_timeout_clock(host); 2447 host->timeout_clk = host->ops->get_timeout_clock(host);
@@ -1871,22 +2453,55 @@ int sdhci_add_host(struct sdhci_host *host)
1871 return -ENODEV; 2453 return -ENODEV;
1872 } 2454 }
1873 } 2455 }
1874 if (caps & SDHCI_TIMEOUT_CLK_UNIT) 2456 if (caps[0] & SDHCI_TIMEOUT_CLK_UNIT)
1875 host->timeout_clk *= 1000; 2457 host->timeout_clk *= 1000;
1876 2458
1877 /* 2459 /*
2460 * In case of Host Controller v3.00, find out whether clock
2461 * multiplier is supported.
2462 */
2463 host->clk_mul = (caps[1] & SDHCI_CLOCK_MUL_MASK) >>
2464 SDHCI_CLOCK_MUL_SHIFT;
2465
2466 /*
2467 * In case the value in Clock Multiplier is 0, then programmable
2468 * clock mode is not supported, otherwise the actual clock
2469 * multiplier is one more than the value of Clock Multiplier
2470 * in the Capabilities Register.
2471 */
2472 if (host->clk_mul)
2473 host->clk_mul += 1;
2474
2475 /*
1878 * Set host parameters. 2476 * Set host parameters.
1879 */ 2477 */
1880 mmc->ops = &sdhci_ops; 2478 mmc->ops = &sdhci_ops;
2479 mmc->f_max = host->max_clk;
1881 if (host->ops->get_min_clock) 2480 if (host->ops->get_min_clock)
1882 mmc->f_min = host->ops->get_min_clock(host); 2481 mmc->f_min = host->ops->get_min_clock(host);
1883 else if (host->version >= SDHCI_SPEC_300) 2482 else if (host->version >= SDHCI_SPEC_300) {
1884 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_300; 2483 if (host->clk_mul) {
1885 else 2484 mmc->f_min = (host->max_clk * host->clk_mul) / 1024;
2485 mmc->f_max = host->max_clk * host->clk_mul;
2486 } else
2487 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_300;
2488 } else
1886 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_200; 2489 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_200;
1887 2490
1888 mmc->f_max = host->max_clk; 2491 mmc->caps |= MMC_CAP_SDIO_IRQ | MMC_CAP_ERASE | MMC_CAP_CMD23;
1889 mmc->caps |= MMC_CAP_SDIO_IRQ; 2492
2493 if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12)
2494 host->flags |= SDHCI_AUTO_CMD12;
2495
2496 /* Auto-CMD23 stuff only works in ADMA or PIO. */
2497 if ((host->version >= SDHCI_SPEC_300) &&
2498 ((host->flags & SDHCI_USE_ADMA) ||
2499 !(host->flags & SDHCI_USE_SDMA))) {
2500 host->flags |= SDHCI_AUTO_CMD23;
2501 DBG("%s: Auto-CMD23 available\n", mmc_hostname(mmc));
2502 } else {
2503 DBG("%s: Auto-CMD23 unavailable\n", mmc_hostname(mmc));
2504 }
1890 2505
1891 /* 2506 /*
1892 * A controller may support 8-bit width, but the board itself 2507 * A controller may support 8-bit width, but the board itself
@@ -1898,21 +2513,113 @@ int sdhci_add_host(struct sdhci_host *host)
1898 if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA)) 2513 if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA))
1899 mmc->caps |= MMC_CAP_4_BIT_DATA; 2514 mmc->caps |= MMC_CAP_4_BIT_DATA;
1900 2515
1901 if (caps & SDHCI_CAN_DO_HISPD) 2516 if (caps[0] & SDHCI_CAN_DO_HISPD)
1902 mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED; 2517 mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
1903 2518
1904 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) && 2519 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) &&
1905 mmc_card_is_removable(mmc)) 2520 mmc_card_is_removable(mmc))
1906 mmc->caps |= MMC_CAP_NEEDS_POLL; 2521 mmc->caps |= MMC_CAP_NEEDS_POLL;
1907 2522
2523 /* UHS-I mode(s) supported by the host controller. */
2524 if (host->version >= SDHCI_SPEC_300)
2525 mmc->caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25;
2526
2527 /* SDR104 supports also implies SDR50 support */
2528 if (caps[1] & SDHCI_SUPPORT_SDR104)
2529 mmc->caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50;
2530 else if (caps[1] & SDHCI_SUPPORT_SDR50)
2531 mmc->caps |= MMC_CAP_UHS_SDR50;
2532
2533 if (caps[1] & SDHCI_SUPPORT_DDR50)
2534 mmc->caps |= MMC_CAP_UHS_DDR50;
2535
2536 /* Does the host needs tuning for SDR50? */
2537 if (caps[1] & SDHCI_USE_SDR50_TUNING)
2538 host->flags |= SDHCI_SDR50_NEEDS_TUNING;
2539
2540 /* Driver Type(s) (A, C, D) supported by the host */
2541 if (caps[1] & SDHCI_DRIVER_TYPE_A)
2542 mmc->caps |= MMC_CAP_DRIVER_TYPE_A;
2543 if (caps[1] & SDHCI_DRIVER_TYPE_C)
2544 mmc->caps |= MMC_CAP_DRIVER_TYPE_C;
2545 if (caps[1] & SDHCI_DRIVER_TYPE_D)
2546 mmc->caps |= MMC_CAP_DRIVER_TYPE_D;
2547
2548 /* Initial value for re-tuning timer count */
2549 host->tuning_count = (caps[1] & SDHCI_RETUNING_TIMER_COUNT_MASK) >>
2550 SDHCI_RETUNING_TIMER_COUNT_SHIFT;
2551
2552 /*
2553 * In case Re-tuning Timer is not disabled, the actual value of
2554 * re-tuning timer will be 2 ^ (n - 1).
2555 */
2556 if (host->tuning_count)
2557 host->tuning_count = 1 << (host->tuning_count - 1);
2558
2559 /* Re-tuning mode supported by the Host Controller */
2560 host->tuning_mode = (caps[1] & SDHCI_RETUNING_MODE_MASK) >>
2561 SDHCI_RETUNING_MODE_SHIFT;
2562
1908 ocr_avail = 0; 2563 ocr_avail = 0;
1909 if (caps & SDHCI_CAN_VDD_330) 2564 /*
2565 * According to SD Host Controller spec v3.00, if the Host System
2566 * can afford more than 150mA, Host Driver should set XPC to 1. Also
2567 * the value is meaningful only if Voltage Support in the Capabilities
2568 * register is set. The actual current value is 4 times the register
2569 * value.
2570 */
2571 max_current_caps = sdhci_readl(host, SDHCI_MAX_CURRENT);
2572
2573 if (caps[0] & SDHCI_CAN_VDD_330) {
2574 int max_current_330;
2575
1910 ocr_avail |= MMC_VDD_32_33 | MMC_VDD_33_34; 2576 ocr_avail |= MMC_VDD_32_33 | MMC_VDD_33_34;
1911 if (caps & SDHCI_CAN_VDD_300) 2577
2578 max_current_330 = ((max_current_caps &
2579 SDHCI_MAX_CURRENT_330_MASK) >>
2580 SDHCI_MAX_CURRENT_330_SHIFT) *
2581 SDHCI_MAX_CURRENT_MULTIPLIER;
2582
2583 if (max_current_330 > 150)
2584 mmc->caps |= MMC_CAP_SET_XPC_330;
2585 }
2586 if (caps[0] & SDHCI_CAN_VDD_300) {
2587 int max_current_300;
2588
1912 ocr_avail |= MMC_VDD_29_30 | MMC_VDD_30_31; 2589 ocr_avail |= MMC_VDD_29_30 | MMC_VDD_30_31;
1913 if (caps & SDHCI_CAN_VDD_180) 2590
2591 max_current_300 = ((max_current_caps &
2592 SDHCI_MAX_CURRENT_300_MASK) >>
2593 SDHCI_MAX_CURRENT_300_SHIFT) *
2594 SDHCI_MAX_CURRENT_MULTIPLIER;
2595
2596 if (max_current_300 > 150)
2597 mmc->caps |= MMC_CAP_SET_XPC_300;
2598 }
2599 if (caps[0] & SDHCI_CAN_VDD_180) {
2600 int max_current_180;
2601
1914 ocr_avail |= MMC_VDD_165_195; 2602 ocr_avail |= MMC_VDD_165_195;
1915 2603
2604 max_current_180 = ((max_current_caps &
2605 SDHCI_MAX_CURRENT_180_MASK) >>
2606 SDHCI_MAX_CURRENT_180_SHIFT) *
2607 SDHCI_MAX_CURRENT_MULTIPLIER;
2608
2609 if (max_current_180 > 150)
2610 mmc->caps |= MMC_CAP_SET_XPC_180;
2611
2612 /* Maximum current capabilities of the host at 1.8V */
2613 if (max_current_180 >= 800)
2614 mmc->caps |= MMC_CAP_MAX_CURRENT_800;
2615 else if (max_current_180 >= 600)
2616 mmc->caps |= MMC_CAP_MAX_CURRENT_600;
2617 else if (max_current_180 >= 400)
2618 mmc->caps |= MMC_CAP_MAX_CURRENT_400;
2619 else
2620 mmc->caps |= MMC_CAP_MAX_CURRENT_200;
2621 }
2622
1916 mmc->ocr_avail = ocr_avail; 2623 mmc->ocr_avail = ocr_avail;
1917 mmc->ocr_avail_sdio = ocr_avail; 2624 mmc->ocr_avail_sdio = ocr_avail;
1918 if (host->ocr_avail_sdio) 2625 if (host->ocr_avail_sdio)
@@ -1972,7 +2679,7 @@ int sdhci_add_host(struct sdhci_host *host)
1972 if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) { 2679 if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) {
1973 mmc->max_blk_size = 2; 2680 mmc->max_blk_size = 2;
1974 } else { 2681 } else {
1975 mmc->max_blk_size = (caps & SDHCI_MAX_BLOCK_MASK) >> 2682 mmc->max_blk_size = (caps[0] & SDHCI_MAX_BLOCK_MASK) >>
1976 SDHCI_MAX_BLOCK_SHIFT; 2683 SDHCI_MAX_BLOCK_SHIFT;
1977 if (mmc->max_blk_size >= 3) { 2684 if (mmc->max_blk_size >= 3) {
1978 printk(KERN_WARNING "%s: Invalid maximum block size, " 2685 printk(KERN_WARNING "%s: Invalid maximum block size, "
@@ -1998,6 +2705,15 @@ int sdhci_add_host(struct sdhci_host *host)
1998 2705
1999 setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host); 2706 setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
2000 2707
2708 if (host->version >= SDHCI_SPEC_300) {
2709 init_waitqueue_head(&host->buf_ready_int);
2710
2711 /* Initialize re-tuning timer */
2712 init_timer(&host->tuning_timer);
2713 host->tuning_timer.data = (unsigned long)host;
2714 host->tuning_timer.function = sdhci_tuning_timer;
2715 }
2716
2001 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED, 2717 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
2002 mmc_hostname(mmc), host); 2718 mmc_hostname(mmc), host);
2003 if (ret) 2719 if (ret)
@@ -2091,6 +2807,8 @@ void sdhci_remove_host(struct sdhci_host *host, int dead)
2091 free_irq(host->irq, host); 2807 free_irq(host->irq, host);
2092 2808
2093 del_timer_sync(&host->timer); 2809 del_timer_sync(&host->timer);
2810 if (host->version >= SDHCI_SPEC_300)
2811 del_timer_sync(&host->tuning_timer);
2094 2812
2095 tasklet_kill(&host->card_tasklet); 2813 tasklet_kill(&host->card_tasklet);
2096 tasklet_kill(&host->finish_tasklet); 2814 tasklet_kill(&host->finish_tasklet);
diff --git a/drivers/mmc/host/sdhci.h b/drivers/mmc/host/sdhci.h
index 25e8bde600d1..745c42fa41ed 100644
--- a/drivers/mmc/host/sdhci.h
+++ b/drivers/mmc/host/sdhci.h
@@ -25,6 +25,7 @@
25 */ 25 */
26 26
27#define SDHCI_DMA_ADDRESS 0x00 27#define SDHCI_DMA_ADDRESS 0x00
28#define SDHCI_ARGUMENT2 SDHCI_DMA_ADDRESS
28 29
29#define SDHCI_BLOCK_SIZE 0x04 30#define SDHCI_BLOCK_SIZE 0x04
30#define SDHCI_MAKE_BLKSZ(dma, blksz) (((dma & 0x7) << 12) | (blksz & 0xFFF)) 31#define SDHCI_MAKE_BLKSZ(dma, blksz) (((dma & 0x7) << 12) | (blksz & 0xFFF))
@@ -36,7 +37,8 @@
36#define SDHCI_TRANSFER_MODE 0x0C 37#define SDHCI_TRANSFER_MODE 0x0C
37#define SDHCI_TRNS_DMA 0x01 38#define SDHCI_TRNS_DMA 0x01
38#define SDHCI_TRNS_BLK_CNT_EN 0x02 39#define SDHCI_TRNS_BLK_CNT_EN 0x02
39#define SDHCI_TRNS_ACMD12 0x04 40#define SDHCI_TRNS_AUTO_CMD12 0x04
41#define SDHCI_TRNS_AUTO_CMD23 0x08
40#define SDHCI_TRNS_READ 0x10 42#define SDHCI_TRNS_READ 0x10
41#define SDHCI_TRNS_MULTI 0x20 43#define SDHCI_TRNS_MULTI 0x20
42 44
@@ -68,8 +70,10 @@
68#define SDHCI_DATA_AVAILABLE 0x00000800 70#define SDHCI_DATA_AVAILABLE 0x00000800
69#define SDHCI_CARD_PRESENT 0x00010000 71#define SDHCI_CARD_PRESENT 0x00010000
70#define SDHCI_WRITE_PROTECT 0x00080000 72#define SDHCI_WRITE_PROTECT 0x00080000
73#define SDHCI_DATA_LVL_MASK 0x00F00000
74#define SDHCI_DATA_LVL_SHIFT 20
71 75
72#define SDHCI_HOST_CONTROL 0x28 76#define SDHCI_HOST_CONTROL 0x28
73#define SDHCI_CTRL_LED 0x01 77#define SDHCI_CTRL_LED 0x01
74#define SDHCI_CTRL_4BITBUS 0x02 78#define SDHCI_CTRL_4BITBUS 0x02
75#define SDHCI_CTRL_HISPD 0x04 79#define SDHCI_CTRL_HISPD 0x04
@@ -99,6 +103,7 @@
99#define SDHCI_DIV_MASK 0xFF 103#define SDHCI_DIV_MASK 0xFF
100#define SDHCI_DIV_MASK_LEN 8 104#define SDHCI_DIV_MASK_LEN 8
101#define SDHCI_DIV_HI_MASK 0x300 105#define SDHCI_DIV_HI_MASK 0x300
106#define SDHCI_PROG_CLOCK_MODE 0x0020
102#define SDHCI_CLOCK_CARD_EN 0x0004 107#define SDHCI_CLOCK_CARD_EN 0x0004
103#define SDHCI_CLOCK_INT_STABLE 0x0002 108#define SDHCI_CLOCK_INT_STABLE 0x0002
104#define SDHCI_CLOCK_INT_EN 0x0001 109#define SDHCI_CLOCK_INT_EN 0x0001
@@ -146,7 +151,22 @@
146 151
147#define SDHCI_ACMD12_ERR 0x3C 152#define SDHCI_ACMD12_ERR 0x3C
148 153
149/* 3E-3F reserved */ 154#define SDHCI_HOST_CONTROL2 0x3E
155#define SDHCI_CTRL_UHS_MASK 0x0007
156#define SDHCI_CTRL_UHS_SDR12 0x0000
157#define SDHCI_CTRL_UHS_SDR25 0x0001
158#define SDHCI_CTRL_UHS_SDR50 0x0002
159#define SDHCI_CTRL_UHS_SDR104 0x0003
160#define SDHCI_CTRL_UHS_DDR50 0x0004
161#define SDHCI_CTRL_VDD_180 0x0008
162#define SDHCI_CTRL_DRV_TYPE_MASK 0x0030
163#define SDHCI_CTRL_DRV_TYPE_B 0x0000
164#define SDHCI_CTRL_DRV_TYPE_A 0x0010
165#define SDHCI_CTRL_DRV_TYPE_C 0x0020
166#define SDHCI_CTRL_DRV_TYPE_D 0x0030
167#define SDHCI_CTRL_EXEC_TUNING 0x0040
168#define SDHCI_CTRL_TUNED_CLK 0x0080
169#define SDHCI_CTRL_PRESET_VAL_ENABLE 0x8000
150 170
151#define SDHCI_CAPABILITIES 0x40 171#define SDHCI_CAPABILITIES 0x40
152#define SDHCI_TIMEOUT_CLK_MASK 0x0000003F 172#define SDHCI_TIMEOUT_CLK_MASK 0x0000003F
@@ -167,9 +187,30 @@
167#define SDHCI_CAN_VDD_180 0x04000000 187#define SDHCI_CAN_VDD_180 0x04000000
168#define SDHCI_CAN_64BIT 0x10000000 188#define SDHCI_CAN_64BIT 0x10000000
169 189
190#define SDHCI_SUPPORT_SDR50 0x00000001
191#define SDHCI_SUPPORT_SDR104 0x00000002
192#define SDHCI_SUPPORT_DDR50 0x00000004
193#define SDHCI_DRIVER_TYPE_A 0x00000010
194#define SDHCI_DRIVER_TYPE_C 0x00000020
195#define SDHCI_DRIVER_TYPE_D 0x00000040
196#define SDHCI_RETUNING_TIMER_COUNT_MASK 0x00000F00
197#define SDHCI_RETUNING_TIMER_COUNT_SHIFT 8
198#define SDHCI_USE_SDR50_TUNING 0x00002000
199#define SDHCI_RETUNING_MODE_MASK 0x0000C000
200#define SDHCI_RETUNING_MODE_SHIFT 14
201#define SDHCI_CLOCK_MUL_MASK 0x00FF0000
202#define SDHCI_CLOCK_MUL_SHIFT 16
203
170#define SDHCI_CAPABILITIES_1 0x44 204#define SDHCI_CAPABILITIES_1 0x44
171 205
172#define SDHCI_MAX_CURRENT 0x48 206#define SDHCI_MAX_CURRENT 0x48
207#define SDHCI_MAX_CURRENT_330_MASK 0x0000FF
208#define SDHCI_MAX_CURRENT_330_SHIFT 0
209#define SDHCI_MAX_CURRENT_300_MASK 0x00FF00
210#define SDHCI_MAX_CURRENT_300_SHIFT 8
211#define SDHCI_MAX_CURRENT_180_MASK 0xFF0000
212#define SDHCI_MAX_CURRENT_180_SHIFT 16
213#define SDHCI_MAX_CURRENT_MULTIPLIER 4
173 214
174/* 4C-4F reserved for more max current */ 215/* 4C-4F reserved for more max current */
175 216
@@ -202,6 +243,12 @@
202#define SDHCI_MAX_DIV_SPEC_200 256 243#define SDHCI_MAX_DIV_SPEC_200 256
203#define SDHCI_MAX_DIV_SPEC_300 2046 244#define SDHCI_MAX_DIV_SPEC_300 2046
204 245
246/*
247 * Host SDMA buffer boundary. Valid values from 4K to 512K in powers of 2.
248 */
249#define SDHCI_DEFAULT_BOUNDARY_SIZE (512 * 1024)
250#define SDHCI_DEFAULT_BOUNDARY_ARG (ilog2(SDHCI_DEFAULT_BOUNDARY_SIZE) - 12)
251
205struct sdhci_ops { 252struct sdhci_ops {
206#ifdef CONFIG_MMC_SDHCI_IO_ACCESSORS 253#ifdef CONFIG_MMC_SDHCI_IO_ACCESSORS
207 u32 (*read_l)(struct sdhci_host *host, int reg); 254 u32 (*read_l)(struct sdhci_host *host, int reg);
@@ -223,6 +270,10 @@ struct sdhci_ops {
223 void (*platform_send_init_74_clocks)(struct sdhci_host *host, 270 void (*platform_send_init_74_clocks)(struct sdhci_host *host,
224 u8 power_mode); 271 u8 power_mode);
225 unsigned int (*get_ro)(struct sdhci_host *host); 272 unsigned int (*get_ro)(struct sdhci_host *host);
273 void (*platform_reset_enter)(struct sdhci_host *host, u8 mask);
274 void (*platform_reset_exit)(struct sdhci_host *host, u8 mask);
275 int (*set_uhs_signaling)(struct sdhci_host *host, unsigned int uhs);
276
226}; 277};
227 278
228#ifdef CONFIG_MMC_SDHCI_IO_ACCESSORS 279#ifdef CONFIG_MMC_SDHCI_IO_ACCESSORS
diff --git a/drivers/mmc/host/sh_mmcif.c b/drivers/mmc/host/sh_mmcif.c
index af97015a2fc7..14f8edbaa195 100644
--- a/drivers/mmc/host/sh_mmcif.c
+++ b/drivers/mmc/host/sh_mmcif.c
@@ -29,6 +29,8 @@
29#include <linux/mmc/sh_mmcif.h> 29#include <linux/mmc/sh_mmcif.h>
30#include <linux/pagemap.h> 30#include <linux/pagemap.h>
31#include <linux/platform_device.h> 31#include <linux/platform_device.h>
32#include <linux/pm_runtime.h>
33#include <linux/spinlock.h>
32 34
33#define DRIVER_NAME "sh_mmcif" 35#define DRIVER_NAME "sh_mmcif"
34#define DRIVER_VERSION "2010-04-28" 36#define DRIVER_VERSION "2010-04-28"
@@ -153,6 +155,12 @@
153#define CLKDEV_MMC_DATA 20000000 /* 20MHz */ 155#define CLKDEV_MMC_DATA 20000000 /* 20MHz */
154#define CLKDEV_INIT 400000 /* 400 KHz */ 156#define CLKDEV_INIT 400000 /* 400 KHz */
155 157
158enum mmcif_state {
159 STATE_IDLE,
160 STATE_REQUEST,
161 STATE_IOS,
162};
163
156struct sh_mmcif_host { 164struct sh_mmcif_host {
157 struct mmc_host *mmc; 165 struct mmc_host *mmc;
158 struct mmc_data *data; 166 struct mmc_data *data;
@@ -164,6 +172,9 @@ struct sh_mmcif_host {
164 long timeout; 172 long timeout;
165 void __iomem *addr; 173 void __iomem *addr;
166 struct completion intr_wait; 174 struct completion intr_wait;
175 enum mmcif_state state;
176 spinlock_t lock;
177 bool power;
167 178
168 /* DMA support */ 179 /* DMA support */
169 struct dma_chan *chan_rx; 180 struct dma_chan *chan_rx;
@@ -798,17 +809,31 @@ static void sh_mmcif_stop_cmd(struct sh_mmcif_host *host,
798static void sh_mmcif_request(struct mmc_host *mmc, struct mmc_request *mrq) 809static void sh_mmcif_request(struct mmc_host *mmc, struct mmc_request *mrq)
799{ 810{
800 struct sh_mmcif_host *host = mmc_priv(mmc); 811 struct sh_mmcif_host *host = mmc_priv(mmc);
812 unsigned long flags;
813
814 spin_lock_irqsave(&host->lock, flags);
815 if (host->state != STATE_IDLE) {
816 spin_unlock_irqrestore(&host->lock, flags);
817 mrq->cmd->error = -EAGAIN;
818 mmc_request_done(mmc, mrq);
819 return;
820 }
821
822 host->state = STATE_REQUEST;
823 spin_unlock_irqrestore(&host->lock, flags);
801 824
802 switch (mrq->cmd->opcode) { 825 switch (mrq->cmd->opcode) {
803 /* MMCIF does not support SD/SDIO command */ 826 /* MMCIF does not support SD/SDIO command */
804 case SD_IO_SEND_OP_COND: 827 case SD_IO_SEND_OP_COND:
805 case MMC_APP_CMD: 828 case MMC_APP_CMD:
829 host->state = STATE_IDLE;
806 mrq->cmd->error = -ETIMEDOUT; 830 mrq->cmd->error = -ETIMEDOUT;
807 mmc_request_done(mmc, mrq); 831 mmc_request_done(mmc, mrq);
808 return; 832 return;
809 case MMC_SEND_EXT_CSD: /* = SD_SEND_IF_COND (8) */ 833 case MMC_SEND_EXT_CSD: /* = SD_SEND_IF_COND (8) */
810 if (!mrq->data) { 834 if (!mrq->data) {
811 /* send_if_cond cmd (not support) */ 835 /* send_if_cond cmd (not support) */
836 host->state = STATE_IDLE;
812 mrq->cmd->error = -ETIMEDOUT; 837 mrq->cmd->error = -ETIMEDOUT;
813 mmc_request_done(mmc, mrq); 838 mmc_request_done(mmc, mrq);
814 return; 839 return;
@@ -830,12 +855,9 @@ static void sh_mmcif_request(struct mmc_host *mmc, struct mmc_request *mrq)
830 sh_mmcif_start_cmd(host, mrq, mrq->cmd); 855 sh_mmcif_start_cmd(host, mrq, mrq->cmd);
831 host->data = NULL; 856 host->data = NULL;
832 857
833 if (mrq->cmd->error != 0) { 858 if (!mrq->cmd->error && mrq->stop)
834 mmc_request_done(mmc, mrq);
835 return;
836 }
837 if (mrq->stop)
838 sh_mmcif_stop_cmd(host, mrq, mrq->stop); 859 sh_mmcif_stop_cmd(host, mrq, mrq->stop);
860 host->state = STATE_IDLE;
839 mmc_request_done(mmc, mrq); 861 mmc_request_done(mmc, mrq);
840} 862}
841 863
@@ -843,15 +865,39 @@ static void sh_mmcif_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
843{ 865{
844 struct sh_mmcif_host *host = mmc_priv(mmc); 866 struct sh_mmcif_host *host = mmc_priv(mmc);
845 struct sh_mmcif_plat_data *p = host->pd->dev.platform_data; 867 struct sh_mmcif_plat_data *p = host->pd->dev.platform_data;
868 unsigned long flags;
869
870 spin_lock_irqsave(&host->lock, flags);
871 if (host->state != STATE_IDLE) {
872 spin_unlock_irqrestore(&host->lock, flags);
873 return;
874 }
875
876 host->state = STATE_IOS;
877 spin_unlock_irqrestore(&host->lock, flags);
846 878
847 if (ios->power_mode == MMC_POWER_UP) { 879 if (ios->power_mode == MMC_POWER_UP) {
848 if (p->set_pwr) 880 if (p->set_pwr)
849 p->set_pwr(host->pd, ios->power_mode); 881 p->set_pwr(host->pd, ios->power_mode);
882 if (!host->power) {
883 /* See if we also get DMA */
884 sh_mmcif_request_dma(host, host->pd->dev.platform_data);
885 pm_runtime_get_sync(&host->pd->dev);
886 host->power = true;
887 }
850 } else if (ios->power_mode == MMC_POWER_OFF || !ios->clock) { 888 } else if (ios->power_mode == MMC_POWER_OFF || !ios->clock) {
851 /* clock stop */ 889 /* clock stop */
852 sh_mmcif_clock_control(host, 0); 890 sh_mmcif_clock_control(host, 0);
853 if (ios->power_mode == MMC_POWER_OFF && p->down_pwr) 891 if (ios->power_mode == MMC_POWER_OFF) {
854 p->down_pwr(host->pd); 892 if (host->power) {
893 pm_runtime_put(&host->pd->dev);
894 sh_mmcif_release_dma(host);
895 host->power = false;
896 }
897 if (p->down_pwr)
898 p->down_pwr(host->pd);
899 }
900 host->state = STATE_IDLE;
855 return; 901 return;
856 } 902 }
857 903
@@ -859,6 +905,7 @@ static void sh_mmcif_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
859 sh_mmcif_clock_control(host, ios->clock); 905 sh_mmcif_clock_control(host, ios->clock);
860 906
861 host->bus_width = ios->bus_width; 907 host->bus_width = ios->bus_width;
908 host->state = STATE_IDLE;
862} 909}
863 910
864static int sh_mmcif_get_cd(struct mmc_host *mmc) 911static int sh_mmcif_get_cd(struct mmc_host *mmc)
@@ -925,7 +972,7 @@ static irqreturn_t sh_mmcif_intr(int irq, void *dev_id)
925 sh_mmcif_bitclr(host, MMCIF_CE_INT_MASK, state); 972 sh_mmcif_bitclr(host, MMCIF_CE_INT_MASK, state);
926 err = 1; 973 err = 1;
927 } else { 974 } else {
928 dev_dbg(&host->pd->dev, "Not support int\n"); 975 dev_dbg(&host->pd->dev, "Unsupported interrupt: 0x%x\n", state);
929 sh_mmcif_writel(host->addr, MMCIF_CE_INT, ~state); 976 sh_mmcif_writel(host->addr, MMCIF_CE_INT, ~state);
930 sh_mmcif_bitclr(host, MMCIF_CE_INT_MASK, state); 977 sh_mmcif_bitclr(host, MMCIF_CE_INT_MASK, state);
931 err = 1; 978 err = 1;
@@ -996,6 +1043,7 @@ static int __devinit sh_mmcif_probe(struct platform_device *pdev)
996 host->pd = pdev; 1043 host->pd = pdev;
997 1044
998 init_completion(&host->intr_wait); 1045 init_completion(&host->intr_wait);
1046 spin_lock_init(&host->lock);
999 1047
1000 mmc->ops = &sh_mmcif_ops; 1048 mmc->ops = &sh_mmcif_ops;
1001 mmc->f_max = host->clk; 1049 mmc->f_max = host->clk;
@@ -1020,24 +1068,29 @@ static int __devinit sh_mmcif_probe(struct platform_device *pdev)
1020 sh_mmcif_sync_reset(host); 1068 sh_mmcif_sync_reset(host);
1021 platform_set_drvdata(pdev, host); 1069 platform_set_drvdata(pdev, host);
1022 1070
1023 /* See if we also get DMA */ 1071 pm_runtime_enable(&pdev->dev);
1024 sh_mmcif_request_dma(host, pd); 1072 host->power = false;
1073
1074 ret = pm_runtime_resume(&pdev->dev);
1075 if (ret < 0)
1076 goto clean_up2;
1025 1077
1026 mmc_add_host(mmc); 1078 mmc_add_host(mmc);
1027 1079
1080 sh_mmcif_writel(host->addr, MMCIF_CE_INT_MASK, MASK_ALL);
1081
1028 ret = request_irq(irq[0], sh_mmcif_intr, 0, "sh_mmc:error", host); 1082 ret = request_irq(irq[0], sh_mmcif_intr, 0, "sh_mmc:error", host);
1029 if (ret) { 1083 if (ret) {
1030 dev_err(&pdev->dev, "request_irq error (sh_mmc:error)\n"); 1084 dev_err(&pdev->dev, "request_irq error (sh_mmc:error)\n");
1031 goto clean_up2; 1085 goto clean_up3;
1032 } 1086 }
1033 ret = request_irq(irq[1], sh_mmcif_intr, 0, "sh_mmc:int", host); 1087 ret = request_irq(irq[1], sh_mmcif_intr, 0, "sh_mmc:int", host);
1034 if (ret) { 1088 if (ret) {
1035 free_irq(irq[0], host); 1089 free_irq(irq[0], host);
1036 dev_err(&pdev->dev, "request_irq error (sh_mmc:int)\n"); 1090 dev_err(&pdev->dev, "request_irq error (sh_mmc:int)\n");
1037 goto clean_up2; 1091 goto clean_up3;
1038 } 1092 }
1039 1093
1040 sh_mmcif_writel(host->addr, MMCIF_CE_INT_MASK, MASK_ALL);
1041 sh_mmcif_detect(host->mmc); 1094 sh_mmcif_detect(host->mmc);
1042 1095
1043 dev_info(&pdev->dev, "driver version %s\n", DRIVER_VERSION); 1096 dev_info(&pdev->dev, "driver version %s\n", DRIVER_VERSION);
@@ -1045,7 +1098,11 @@ static int __devinit sh_mmcif_probe(struct platform_device *pdev)
1045 sh_mmcif_readl(host->addr, MMCIF_CE_VERSION) & 0x0000ffff); 1098 sh_mmcif_readl(host->addr, MMCIF_CE_VERSION) & 0x0000ffff);
1046 return ret; 1099 return ret;
1047 1100
1101clean_up3:
1102 mmc_remove_host(mmc);
1103 pm_runtime_suspend(&pdev->dev);
1048clean_up2: 1104clean_up2:
1105 pm_runtime_disable(&pdev->dev);
1049 clk_disable(host->hclk); 1106 clk_disable(host->hclk);
1050clean_up1: 1107clean_up1:
1051 mmc_free_host(mmc); 1108 mmc_free_host(mmc);
@@ -1060,14 +1117,14 @@ static int __devexit sh_mmcif_remove(struct platform_device *pdev)
1060 struct sh_mmcif_host *host = platform_get_drvdata(pdev); 1117 struct sh_mmcif_host *host = platform_get_drvdata(pdev);
1061 int irq[2]; 1118 int irq[2];
1062 1119
1120 pm_runtime_get_sync(&pdev->dev);
1121
1063 mmc_remove_host(host->mmc); 1122 mmc_remove_host(host->mmc);
1064 sh_mmcif_release_dma(host); 1123 sh_mmcif_writel(host->addr, MMCIF_CE_INT_MASK, MASK_ALL);
1065 1124
1066 if (host->addr) 1125 if (host->addr)
1067 iounmap(host->addr); 1126 iounmap(host->addr);
1068 1127
1069 sh_mmcif_writel(host->addr, MMCIF_CE_INT_MASK, MASK_ALL);
1070
1071 irq[0] = platform_get_irq(pdev, 0); 1128 irq[0] = platform_get_irq(pdev, 0);
1072 irq[1] = platform_get_irq(pdev, 1); 1129 irq[1] = platform_get_irq(pdev, 1);
1073 1130
@@ -1078,15 +1135,52 @@ static int __devexit sh_mmcif_remove(struct platform_device *pdev)
1078 1135
1079 clk_disable(host->hclk); 1136 clk_disable(host->hclk);
1080 mmc_free_host(host->mmc); 1137 mmc_free_host(host->mmc);
1138 pm_runtime_put_sync(&pdev->dev);
1139 pm_runtime_disable(&pdev->dev);
1081 1140
1082 return 0; 1141 return 0;
1083} 1142}
1084 1143
1144#ifdef CONFIG_PM
1145static int sh_mmcif_suspend(struct device *dev)
1146{
1147 struct platform_device *pdev = to_platform_device(dev);
1148 struct sh_mmcif_host *host = platform_get_drvdata(pdev);
1149 int ret = mmc_suspend_host(host->mmc);
1150
1151 if (!ret) {
1152 sh_mmcif_writel(host->addr, MMCIF_CE_INT_MASK, MASK_ALL);
1153 clk_disable(host->hclk);
1154 }
1155
1156 return ret;
1157}
1158
1159static int sh_mmcif_resume(struct device *dev)
1160{
1161 struct platform_device *pdev = to_platform_device(dev);
1162 struct sh_mmcif_host *host = platform_get_drvdata(pdev);
1163
1164 clk_enable(host->hclk);
1165
1166 return mmc_resume_host(host->mmc);
1167}
1168#else
1169#define sh_mmcif_suspend NULL
1170#define sh_mmcif_resume NULL
1171#endif /* CONFIG_PM */
1172
1173static const struct dev_pm_ops sh_mmcif_dev_pm_ops = {
1174 .suspend = sh_mmcif_suspend,
1175 .resume = sh_mmcif_resume,
1176};
1177
1085static struct platform_driver sh_mmcif_driver = { 1178static struct platform_driver sh_mmcif_driver = {
1086 .probe = sh_mmcif_probe, 1179 .probe = sh_mmcif_probe,
1087 .remove = sh_mmcif_remove, 1180 .remove = sh_mmcif_remove,
1088 .driver = { 1181 .driver = {
1089 .name = DRIVER_NAME, 1182 .name = DRIVER_NAME,
1183 .pm = &sh_mmcif_dev_pm_ops,
1090 }, 1184 },
1091}; 1185};
1092 1186
diff --git a/drivers/mmc/host/sh_mobile_sdhi.c b/drivers/mmc/host/sh_mobile_sdhi.c
index cc701236d16f..b3654293017b 100644
--- a/drivers/mmc/host/sh_mobile_sdhi.c
+++ b/drivers/mmc/host/sh_mobile_sdhi.c
@@ -62,7 +62,7 @@ static int __devinit sh_mobile_sdhi_probe(struct platform_device *pdev)
62 struct sh_mobile_sdhi_info *p = pdev->dev.platform_data; 62 struct sh_mobile_sdhi_info *p = pdev->dev.platform_data;
63 struct tmio_mmc_host *host; 63 struct tmio_mmc_host *host;
64 char clk_name[8]; 64 char clk_name[8];
65 int ret; 65 int i, irq, ret;
66 66
67 priv = kzalloc(sizeof(struct sh_mobile_sdhi), GFP_KERNEL); 67 priv = kzalloc(sizeof(struct sh_mobile_sdhi), GFP_KERNEL);
68 if (priv == NULL) { 68 if (priv == NULL) {
@@ -71,6 +71,7 @@ static int __devinit sh_mobile_sdhi_probe(struct platform_device *pdev)
71 } 71 }
72 72
73 mmc_data = &priv->mmc_data; 73 mmc_data = &priv->mmc_data;
74 p->pdata = mmc_data;
74 75
75 snprintf(clk_name, sizeof(clk_name), "sdhi%d", pdev->id); 76 snprintf(clk_name, sizeof(clk_name), "sdhi%d", pdev->id);
76 priv->clk = clk_get(&pdev->dev, clk_name); 77 priv->clk = clk_get(&pdev->dev, clk_name);
@@ -116,11 +117,36 @@ static int __devinit sh_mobile_sdhi_probe(struct platform_device *pdev)
116 if (ret < 0) 117 if (ret < 0)
117 goto eprobe; 118 goto eprobe;
118 119
119 pr_info("%s at 0x%08lx irq %d\n", mmc_hostname(host->mmc), 120 for (i = 0; i < 3; i++) {
120 (unsigned long)host->ctl, host->irq); 121 irq = platform_get_irq(pdev, i);
122 if (irq < 0) {
123 if (i) {
124 continue;
125 } else {
126 ret = irq;
127 goto eirq;
128 }
129 }
130 ret = request_irq(irq, tmio_mmc_irq, 0,
131 dev_name(&pdev->dev), host);
132 if (ret) {
133 while (i--) {
134 irq = platform_get_irq(pdev, i);
135 if (irq >= 0)
136 free_irq(irq, host);
137 }
138 goto eirq;
139 }
140 }
141 dev_info(&pdev->dev, "%s base at 0x%08lx clock rate %u MHz\n",
142 mmc_hostname(host->mmc), (unsigned long)
143 (platform_get_resource(pdev,IORESOURCE_MEM, 0)->start),
144 mmc_data->hclk / 1000000);
121 145
122 return ret; 146 return ret;
123 147
148eirq:
149 tmio_mmc_host_remove(host);
124eprobe: 150eprobe:
125 clk_disable(priv->clk); 151 clk_disable(priv->clk);
126 clk_put(priv->clk); 152 clk_put(priv->clk);
@@ -134,6 +160,16 @@ static int sh_mobile_sdhi_remove(struct platform_device *pdev)
134 struct mmc_host *mmc = platform_get_drvdata(pdev); 160 struct mmc_host *mmc = platform_get_drvdata(pdev);
135 struct tmio_mmc_host *host = mmc_priv(mmc); 161 struct tmio_mmc_host *host = mmc_priv(mmc);
136 struct sh_mobile_sdhi *priv = container_of(host->pdata, struct sh_mobile_sdhi, mmc_data); 162 struct sh_mobile_sdhi *priv = container_of(host->pdata, struct sh_mobile_sdhi, mmc_data);
163 struct sh_mobile_sdhi_info *p = pdev->dev.platform_data;
164 int i, irq;
165
166 p->pdata = NULL;
167
168 for (i = 0; i < 3; i++) {
169 irq = platform_get_irq(pdev, i);
170 if (irq >= 0)
171 free_irq(irq, host);
172 }
137 173
138 tmio_mmc_host_remove(host); 174 tmio_mmc_host_remove(host);
139 clk_disable(priv->clk); 175 clk_disable(priv->clk);
@@ -143,10 +179,18 @@ static int sh_mobile_sdhi_remove(struct platform_device *pdev)
143 return 0; 179 return 0;
144} 180}
145 181
182static const struct dev_pm_ops tmio_mmc_dev_pm_ops = {
183 .suspend = tmio_mmc_host_suspend,
184 .resume = tmio_mmc_host_resume,
185 .runtime_suspend = tmio_mmc_host_runtime_suspend,
186 .runtime_resume = tmio_mmc_host_runtime_resume,
187};
188
146static struct platform_driver sh_mobile_sdhi_driver = { 189static struct platform_driver sh_mobile_sdhi_driver = {
147 .driver = { 190 .driver = {
148 .name = "sh_mobile_sdhi", 191 .name = "sh_mobile_sdhi",
149 .owner = THIS_MODULE, 192 .owner = THIS_MODULE,
193 .pm = &tmio_mmc_dev_pm_ops,
150 }, 194 },
151 .probe = sh_mobile_sdhi_probe, 195 .probe = sh_mobile_sdhi_probe,
152 .remove = __devexit_p(sh_mobile_sdhi_remove), 196 .remove = __devexit_p(sh_mobile_sdhi_remove),
diff --git a/drivers/mmc/host/tmio_mmc.c b/drivers/mmc/host/tmio_mmc.c
index 79c568461d59..14479f9ef53f 100644
--- a/drivers/mmc/host/tmio_mmc.c
+++ b/drivers/mmc/host/tmio_mmc.c
@@ -30,7 +30,7 @@ static int tmio_mmc_suspend(struct platform_device *dev, pm_message_t state)
30 struct mmc_host *mmc = platform_get_drvdata(dev); 30 struct mmc_host *mmc = platform_get_drvdata(dev);
31 int ret; 31 int ret;
32 32
33 ret = mmc_suspend_host(mmc); 33 ret = tmio_mmc_host_suspend(&dev->dev);
34 34
35 /* Tell MFD core it can disable us now.*/ 35 /* Tell MFD core it can disable us now.*/
36 if (!ret && cell->disable) 36 if (!ret && cell->disable)
@@ -46,15 +46,12 @@ static int tmio_mmc_resume(struct platform_device *dev)
46 int ret = 0; 46 int ret = 0;
47 47
48 /* Tell the MFD core we are ready to be enabled */ 48 /* Tell the MFD core we are ready to be enabled */
49 if (cell->resume) { 49 if (cell->resume)
50 ret = cell->resume(dev); 50 ret = cell->resume(dev);
51 if (ret)
52 goto out;
53 }
54 51
55 mmc_resume_host(mmc); 52 if (!ret)
53 ret = tmio_mmc_host_resume(&dev->dev);
56 54
57out:
58 return ret; 55 return ret;
59} 56}
60#else 57#else
@@ -67,7 +64,7 @@ static int __devinit tmio_mmc_probe(struct platform_device *pdev)
67 const struct mfd_cell *cell = mfd_get_cell(pdev); 64 const struct mfd_cell *cell = mfd_get_cell(pdev);
68 struct tmio_mmc_data *pdata; 65 struct tmio_mmc_data *pdata;
69 struct tmio_mmc_host *host; 66 struct tmio_mmc_host *host;
70 int ret = -EINVAL; 67 int ret = -EINVAL, irq;
71 68
72 if (pdev->num_resources != 2) 69 if (pdev->num_resources != 2)
73 goto out; 70 goto out;
@@ -76,6 +73,12 @@ static int __devinit tmio_mmc_probe(struct platform_device *pdev)
76 if (!pdata || !pdata->hclk) 73 if (!pdata || !pdata->hclk)
77 goto out; 74 goto out;
78 75
76 irq = platform_get_irq(pdev, 0);
77 if (irq < 0) {
78 ret = irq;
79 goto out;
80 }
81
79 /* Tell the MFD core we are ready to be enabled */ 82 /* Tell the MFD core we are ready to be enabled */
80 if (cell->enable) { 83 if (cell->enable) {
81 ret = cell->enable(pdev); 84 ret = cell->enable(pdev);
@@ -87,11 +90,18 @@ static int __devinit tmio_mmc_probe(struct platform_device *pdev)
87 if (ret) 90 if (ret)
88 goto cell_disable; 91 goto cell_disable;
89 92
93 ret = request_irq(irq, tmio_mmc_irq, IRQF_DISABLED |
94 IRQF_TRIGGER_FALLING, dev_name(&pdev->dev), host);
95 if (ret)
96 goto host_remove;
97
90 pr_info("%s at 0x%08lx irq %d\n", mmc_hostname(host->mmc), 98 pr_info("%s at 0x%08lx irq %d\n", mmc_hostname(host->mmc),
91 (unsigned long)host->ctl, host->irq); 99 (unsigned long)host->ctl, irq);
92 100
93 return 0; 101 return 0;
94 102
103host_remove:
104 tmio_mmc_host_remove(host);
95cell_disable: 105cell_disable:
96 if (cell->disable) 106 if (cell->disable)
97 cell->disable(pdev); 107 cell->disable(pdev);
@@ -107,7 +117,9 @@ static int __devexit tmio_mmc_remove(struct platform_device *pdev)
107 platform_set_drvdata(pdev, NULL); 117 platform_set_drvdata(pdev, NULL);
108 118
109 if (mmc) { 119 if (mmc) {
110 tmio_mmc_host_remove(mmc_priv(mmc)); 120 struct tmio_mmc_host *host = mmc_priv(mmc);
121 free_irq(platform_get_irq(pdev, 0), host);
122 tmio_mmc_host_remove(host);
111 if (cell->disable) 123 if (cell->disable)
112 cell->disable(pdev); 124 cell->disable(pdev);
113 } 125 }
diff --git a/drivers/mmc/host/tmio_mmc.h b/drivers/mmc/host/tmio_mmc.h
index 099ed49a259b..8260bc2c34e3 100644
--- a/drivers/mmc/host/tmio_mmc.h
+++ b/drivers/mmc/host/tmio_mmc.h
@@ -19,6 +19,7 @@
19#include <linux/highmem.h> 19#include <linux/highmem.h>
20#include <linux/mmc/tmio.h> 20#include <linux/mmc/tmio.h>
21#include <linux/pagemap.h> 21#include <linux/pagemap.h>
22#include <linux/spinlock.h>
22 23
23/* Definitions for values the CTRL_SDIO_STATUS register can take. */ 24/* Definitions for values the CTRL_SDIO_STATUS register can take. */
24#define TMIO_SDIO_STAT_IOIRQ 0x0001 25#define TMIO_SDIO_STAT_IOIRQ 0x0001
@@ -44,13 +45,14 @@ struct tmio_mmc_host {
44 struct mmc_request *mrq; 45 struct mmc_request *mrq;
45 struct mmc_data *data; 46 struct mmc_data *data;
46 struct mmc_host *mmc; 47 struct mmc_host *mmc;
47 int irq;
48 unsigned int sdio_irq_enabled; 48 unsigned int sdio_irq_enabled;
49 49
50 /* Callbacks for clock / power control */ 50 /* Callbacks for clock / power control */
51 void (*set_pwr)(struct platform_device *host, int state); 51 void (*set_pwr)(struct platform_device *host, int state);
52 void (*set_clk_div)(struct platform_device *host, int state); 52 void (*set_clk_div)(struct platform_device *host, int state);
53 53
54 int pm_error;
55
54 /* pio related stuff */ 56 /* pio related stuff */
55 struct scatterlist *sg_ptr; 57 struct scatterlist *sg_ptr;
56 struct scatterlist *sg_orig; 58 struct scatterlist *sg_orig;
@@ -83,6 +85,7 @@ void tmio_mmc_do_data_irq(struct tmio_mmc_host *host);
83 85
84void tmio_mmc_enable_mmc_irqs(struct tmio_mmc_host *host, u32 i); 86void tmio_mmc_enable_mmc_irqs(struct tmio_mmc_host *host, u32 i);
85void tmio_mmc_disable_mmc_irqs(struct tmio_mmc_host *host, u32 i); 87void tmio_mmc_disable_mmc_irqs(struct tmio_mmc_host *host, u32 i);
88irqreturn_t tmio_mmc_irq(int irq, void *devid);
86 89
87static inline char *tmio_mmc_kmap_atomic(struct scatterlist *sg, 90static inline char *tmio_mmc_kmap_atomic(struct scatterlist *sg,
88 unsigned long *flags) 91 unsigned long *flags)
@@ -120,4 +123,15 @@ static inline void tmio_mmc_release_dma(struct tmio_mmc_host *host)
120} 123}
121#endif 124#endif
122 125
126#ifdef CONFIG_PM
127int tmio_mmc_host_suspend(struct device *dev);
128int tmio_mmc_host_resume(struct device *dev);
129#else
130#define tmio_mmc_host_suspend NULL
131#define tmio_mmc_host_resume NULL
132#endif
133
134int tmio_mmc_host_runtime_suspend(struct device *dev);
135int tmio_mmc_host_runtime_resume(struct device *dev);
136
123#endif 137#endif
diff --git a/drivers/mmc/host/tmio_mmc_dma.c b/drivers/mmc/host/tmio_mmc_dma.c
index d3de74ab633e..25f1ad6cbe09 100644
--- a/drivers/mmc/host/tmio_mmc_dma.c
+++ b/drivers/mmc/host/tmio_mmc_dma.c
@@ -256,7 +256,10 @@ static bool tmio_mmc_filter(struct dma_chan *chan, void *arg)
256void tmio_mmc_request_dma(struct tmio_mmc_host *host, struct tmio_mmc_data *pdata) 256void tmio_mmc_request_dma(struct tmio_mmc_host *host, struct tmio_mmc_data *pdata)
257{ 257{
258 /* We can only either use DMA for both Tx and Rx or not use it at all */ 258 /* We can only either use DMA for both Tx and Rx or not use it at all */
259 if (pdata->dma) { 259 if (!pdata->dma)
260 return;
261
262 if (!host->chan_tx && !host->chan_rx) {
260 dma_cap_mask_t mask; 263 dma_cap_mask_t mask;
261 264
262 dma_cap_zero(mask); 265 dma_cap_zero(mask);
@@ -284,18 +287,18 @@ void tmio_mmc_request_dma(struct tmio_mmc_host *host, struct tmio_mmc_data *pdat
284 287
285 tasklet_init(&host->dma_complete, tmio_mmc_tasklet_fn, (unsigned long)host); 288 tasklet_init(&host->dma_complete, tmio_mmc_tasklet_fn, (unsigned long)host);
286 tasklet_init(&host->dma_issue, tmio_mmc_issue_tasklet_fn, (unsigned long)host); 289 tasklet_init(&host->dma_issue, tmio_mmc_issue_tasklet_fn, (unsigned long)host);
290 }
287 291
288 tmio_mmc_enable_dma(host, true); 292 tmio_mmc_enable_dma(host, true);
293
294 return;
289 295
290 return;
291ebouncebuf: 296ebouncebuf:
292 dma_release_channel(host->chan_rx); 297 dma_release_channel(host->chan_rx);
293 host->chan_rx = NULL; 298 host->chan_rx = NULL;
294ereqrx: 299ereqrx:
295 dma_release_channel(host->chan_tx); 300 dma_release_channel(host->chan_tx);
296 host->chan_tx = NULL; 301 host->chan_tx = NULL;
297 return;
298 }
299} 302}
300 303
301void tmio_mmc_release_dma(struct tmio_mmc_host *host) 304void tmio_mmc_release_dma(struct tmio_mmc_host *host)
diff --git a/drivers/mmc/host/tmio_mmc_pio.c b/drivers/mmc/host/tmio_mmc_pio.c
index 710339a85c84..ad6347bb02dd 100644
--- a/drivers/mmc/host/tmio_mmc_pio.c
+++ b/drivers/mmc/host/tmio_mmc_pio.c
@@ -39,6 +39,7 @@
39#include <linux/module.h> 39#include <linux/module.h>
40#include <linux/pagemap.h> 40#include <linux/pagemap.h>
41#include <linux/platform_device.h> 41#include <linux/platform_device.h>
42#include <linux/pm_runtime.h>
42#include <linux/scatterlist.h> 43#include <linux/scatterlist.h>
43#include <linux/workqueue.h> 44#include <linux/workqueue.h>
44#include <linux/spinlock.h> 45#include <linux/spinlock.h>
@@ -243,8 +244,12 @@ static void tmio_mmc_reset_work(struct work_struct *work)
243 spin_lock_irqsave(&host->lock, flags); 244 spin_lock_irqsave(&host->lock, flags);
244 mrq = host->mrq; 245 mrq = host->mrq;
245 246
246 /* request already finished */ 247 /*
247 if (!mrq 248 * is request already finished? Since we use a non-blocking
249 * cancel_delayed_work(), it can happen, that a .set_ios() call preempts
250 * us, so, have to check for IS_ERR(host->mrq)
251 */
252 if (IS_ERR_OR_NULL(mrq)
248 || time_is_after_jiffies(host->last_req_ts + 253 || time_is_after_jiffies(host->last_req_ts +
249 msecs_to_jiffies(2000))) { 254 msecs_to_jiffies(2000))) {
250 spin_unlock_irqrestore(&host->lock, flags); 255 spin_unlock_irqrestore(&host->lock, flags);
@@ -264,16 +269,19 @@ static void tmio_mmc_reset_work(struct work_struct *work)
264 269
265 host->cmd = NULL; 270 host->cmd = NULL;
266 host->data = NULL; 271 host->data = NULL;
267 host->mrq = NULL;
268 host->force_pio = false; 272 host->force_pio = false;
269 273
270 spin_unlock_irqrestore(&host->lock, flags); 274 spin_unlock_irqrestore(&host->lock, flags);
271 275
272 tmio_mmc_reset(host); 276 tmio_mmc_reset(host);
273 277
278 /* Ready for new calls */
279 host->mrq = NULL;
280
274 mmc_request_done(host->mmc, mrq); 281 mmc_request_done(host->mmc, mrq);
275} 282}
276 283
284/* called with host->lock held, interrupts disabled */
277static void tmio_mmc_finish_request(struct tmio_mmc_host *host) 285static void tmio_mmc_finish_request(struct tmio_mmc_host *host)
278{ 286{
279 struct mmc_request *mrq = host->mrq; 287 struct mmc_request *mrq = host->mrq;
@@ -281,13 +289,15 @@ static void tmio_mmc_finish_request(struct tmio_mmc_host *host)
281 if (!mrq) 289 if (!mrq)
282 return; 290 return;
283 291
284 host->mrq = NULL;
285 host->cmd = NULL; 292 host->cmd = NULL;
286 host->data = NULL; 293 host->data = NULL;
287 host->force_pio = false; 294 host->force_pio = false;
288 295
289 cancel_delayed_work(&host->delayed_reset_work); 296 cancel_delayed_work(&host->delayed_reset_work);
290 297
298 host->mrq = NULL;
299
300 /* FIXME: mmc_request_done() can schedule! */
291 mmc_request_done(host->mmc, mrq); 301 mmc_request_done(host->mmc, mrq);
292} 302}
293 303
@@ -554,7 +564,7 @@ out:
554 spin_unlock(&host->lock); 564 spin_unlock(&host->lock);
555} 565}
556 566
557static irqreturn_t tmio_mmc_irq(int irq, void *devid) 567irqreturn_t tmio_mmc_irq(int irq, void *devid)
558{ 568{
559 struct tmio_mmc_host *host = devid; 569 struct tmio_mmc_host *host = devid;
560 struct tmio_mmc_data *pdata = host->pdata; 570 struct tmio_mmc_data *pdata = host->pdata;
@@ -649,6 +659,7 @@ static irqreturn_t tmio_mmc_irq(int irq, void *devid)
649out: 659out:
650 return IRQ_HANDLED; 660 return IRQ_HANDLED;
651} 661}
662EXPORT_SYMBOL(tmio_mmc_irq);
652 663
653static int tmio_mmc_start_data(struct tmio_mmc_host *host, 664static int tmio_mmc_start_data(struct tmio_mmc_host *host,
654 struct mmc_data *data) 665 struct mmc_data *data)
@@ -685,15 +696,27 @@ static int tmio_mmc_start_data(struct tmio_mmc_host *host,
685static void tmio_mmc_request(struct mmc_host *mmc, struct mmc_request *mrq) 696static void tmio_mmc_request(struct mmc_host *mmc, struct mmc_request *mrq)
686{ 697{
687 struct tmio_mmc_host *host = mmc_priv(mmc); 698 struct tmio_mmc_host *host = mmc_priv(mmc);
699 unsigned long flags;
688 int ret; 700 int ret;
689 701
690 if (host->mrq) 702 spin_lock_irqsave(&host->lock, flags);
703
704 if (host->mrq) {
691 pr_debug("request not null\n"); 705 pr_debug("request not null\n");
706 if (IS_ERR(host->mrq)) {
707 spin_unlock_irqrestore(&host->lock, flags);
708 mrq->cmd->error = -EAGAIN;
709 mmc_request_done(mmc, mrq);
710 return;
711 }
712 }
692 713
693 host->last_req_ts = jiffies; 714 host->last_req_ts = jiffies;
694 wmb(); 715 wmb();
695 host->mrq = mrq; 716 host->mrq = mrq;
696 717
718 spin_unlock_irqrestore(&host->lock, flags);
719
697 if (mrq->data) { 720 if (mrq->data) {
698 ret = tmio_mmc_start_data(host, mrq->data); 721 ret = tmio_mmc_start_data(host, mrq->data);
699 if (ret) 722 if (ret)
@@ -708,8 +731,8 @@ static void tmio_mmc_request(struct mmc_host *mmc, struct mmc_request *mrq)
708 } 731 }
709 732
710fail: 733fail:
711 host->mrq = NULL;
712 host->force_pio = false; 734 host->force_pio = false;
735 host->mrq = NULL;
713 mrq->cmd->error = ret; 736 mrq->cmd->error = ret;
714 mmc_request_done(mmc, mrq); 737 mmc_request_done(mmc, mrq);
715} 738}
@@ -723,19 +746,54 @@ fail:
723static void tmio_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios) 746static void tmio_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
724{ 747{
725 struct tmio_mmc_host *host = mmc_priv(mmc); 748 struct tmio_mmc_host *host = mmc_priv(mmc);
749 struct tmio_mmc_data *pdata = host->pdata;
750 unsigned long flags;
751
752 spin_lock_irqsave(&host->lock, flags);
753 if (host->mrq) {
754 if (IS_ERR(host->mrq)) {
755 dev_dbg(&host->pdev->dev,
756 "%s.%d: concurrent .set_ios(), clk %u, mode %u\n",
757 current->comm, task_pid_nr(current),
758 ios->clock, ios->power_mode);
759 host->mrq = ERR_PTR(-EINTR);
760 } else {
761 dev_dbg(&host->pdev->dev,
762 "%s.%d: CMD%u active since %lu, now %lu!\n",
763 current->comm, task_pid_nr(current),
764 host->mrq->cmd->opcode, host->last_req_ts, jiffies);
765 }
766 spin_unlock_irqrestore(&host->lock, flags);
767 return;
768 }
769
770 host->mrq = ERR_PTR(-EBUSY);
771
772 spin_unlock_irqrestore(&host->lock, flags);
726 773
727 if (ios->clock) 774 if (ios->clock)
728 tmio_mmc_set_clock(host, ios->clock); 775 tmio_mmc_set_clock(host, ios->clock);
729 776
730 /* Power sequence - OFF -> UP -> ON */ 777 /* Power sequence - OFF -> UP -> ON */
731 if (ios->power_mode == MMC_POWER_UP) { 778 if (ios->power_mode == MMC_POWER_UP) {
779 if ((pdata->flags & TMIO_MMC_HAS_COLD_CD) && !pdata->power) {
780 pm_runtime_get_sync(&host->pdev->dev);
781 pdata->power = true;
782 }
732 /* power up SD bus */ 783 /* power up SD bus */
733 if (host->set_pwr) 784 if (host->set_pwr)
734 host->set_pwr(host->pdev, 1); 785 host->set_pwr(host->pdev, 1);
735 } else if (ios->power_mode == MMC_POWER_OFF || !ios->clock) { 786 } else if (ios->power_mode == MMC_POWER_OFF || !ios->clock) {
736 /* power down SD bus */ 787 /* power down SD bus */
737 if (ios->power_mode == MMC_POWER_OFF && host->set_pwr) 788 if (ios->power_mode == MMC_POWER_OFF) {
738 host->set_pwr(host->pdev, 0); 789 if (host->set_pwr)
790 host->set_pwr(host->pdev, 0);
791 if ((pdata->flags & TMIO_MMC_HAS_COLD_CD) &&
792 pdata->power) {
793 pdata->power = false;
794 pm_runtime_put(&host->pdev->dev);
795 }
796 }
739 tmio_mmc_clk_stop(host); 797 tmio_mmc_clk_stop(host);
740 } else { 798 } else {
741 /* start bus clock */ 799 /* start bus clock */
@@ -753,6 +811,12 @@ static void tmio_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
753 811
754 /* Let things settle. delay taken from winCE driver */ 812 /* Let things settle. delay taken from winCE driver */
755 udelay(140); 813 udelay(140);
814 if (PTR_ERR(host->mrq) == -EINTR)
815 dev_dbg(&host->pdev->dev,
816 "%s.%d: IOS interrupted: clk %u, mode %u",
817 current->comm, task_pid_nr(current),
818 ios->clock, ios->power_mode);
819 host->mrq = NULL;
756} 820}
757 821
758static int tmio_mmc_get_ro(struct mmc_host *mmc) 822static int tmio_mmc_get_ro(struct mmc_host *mmc)
@@ -801,6 +865,7 @@ int __devinit tmio_mmc_host_probe(struct tmio_mmc_host **host,
801 if (!mmc) 865 if (!mmc)
802 return -ENOMEM; 866 return -ENOMEM;
803 867
868 pdata->dev = &pdev->dev;
804 _host = mmc_priv(mmc); 869 _host = mmc_priv(mmc);
805 _host->pdata = pdata; 870 _host->pdata = pdata;
806 _host->mmc = mmc; 871 _host->mmc = mmc;
@@ -834,24 +899,19 @@ int __devinit tmio_mmc_host_probe(struct tmio_mmc_host **host,
834 else 899 else
835 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34; 900 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
836 901
837 tmio_mmc_clk_stop(_host); 902 pdata->power = false;
838 tmio_mmc_reset(_host); 903 pm_runtime_enable(&pdev->dev);
839 904 ret = pm_runtime_resume(&pdev->dev);
840 ret = platform_get_irq(pdev, 0);
841 if (ret < 0) 905 if (ret < 0)
842 goto unmap_ctl; 906 goto pm_disable;
843 907
844 _host->irq = ret; 908 tmio_mmc_clk_stop(_host);
909 tmio_mmc_reset(_host);
845 910
846 tmio_mmc_disable_mmc_irqs(_host, TMIO_MASK_ALL); 911 tmio_mmc_disable_mmc_irqs(_host, TMIO_MASK_ALL);
847 if (pdata->flags & TMIO_MMC_SDIO_IRQ) 912 if (pdata->flags & TMIO_MMC_SDIO_IRQ)
848 tmio_mmc_enable_sdio_irq(mmc, 0); 913 tmio_mmc_enable_sdio_irq(mmc, 0);
849 914
850 ret = request_irq(_host->irq, tmio_mmc_irq, IRQF_DISABLED |
851 IRQF_TRIGGER_FALLING, dev_name(&pdev->dev), _host);
852 if (ret)
853 goto unmap_ctl;
854
855 spin_lock_init(&_host->lock); 915 spin_lock_init(&_host->lock);
856 916
857 /* Init delayed work for request timeouts */ 917 /* Init delayed work for request timeouts */
@@ -860,6 +920,10 @@ int __devinit tmio_mmc_host_probe(struct tmio_mmc_host **host,
860 /* See if we also get DMA */ 920 /* See if we also get DMA */
861 tmio_mmc_request_dma(_host, pdata); 921 tmio_mmc_request_dma(_host, pdata);
862 922
923 /* We have to keep the device powered for its card detection to work */
924 if (!(pdata->flags & TMIO_MMC_HAS_COLD_CD))
925 pm_runtime_get_noresume(&pdev->dev);
926
863 mmc_add_host(mmc); 927 mmc_add_host(mmc);
864 928
865 /* Unmask the IRQs we want to know about */ 929 /* Unmask the IRQs we want to know about */
@@ -874,7 +938,8 @@ int __devinit tmio_mmc_host_probe(struct tmio_mmc_host **host,
874 938
875 return 0; 939 return 0;
876 940
877unmap_ctl: 941pm_disable:
942 pm_runtime_disable(&pdev->dev);
878 iounmap(_host->ctl); 943 iounmap(_host->ctl);
879host_free: 944host_free:
880 mmc_free_host(mmc); 945 mmc_free_host(mmc);
@@ -885,13 +950,88 @@ EXPORT_SYMBOL(tmio_mmc_host_probe);
885 950
886void tmio_mmc_host_remove(struct tmio_mmc_host *host) 951void tmio_mmc_host_remove(struct tmio_mmc_host *host)
887{ 952{
953 struct platform_device *pdev = host->pdev;
954
955 /*
956 * We don't have to manipulate pdata->power here: if there is a card in
957 * the slot, the runtime PM is active and our .runtime_resume() will not
958 * be run. If there is no card in the slot and the platform can suspend
959 * the controller, the runtime PM is suspended and pdata->power == false,
960 * so, our .runtime_resume() will not try to detect a card in the slot.
961 */
962 if (host->pdata->flags & TMIO_MMC_HAS_COLD_CD)
963 pm_runtime_get_sync(&pdev->dev);
964
888 mmc_remove_host(host->mmc); 965 mmc_remove_host(host->mmc);
889 cancel_delayed_work_sync(&host->delayed_reset_work); 966 cancel_delayed_work_sync(&host->delayed_reset_work);
890 tmio_mmc_release_dma(host); 967 tmio_mmc_release_dma(host);
891 free_irq(host->irq, host); 968
969 pm_runtime_put_sync(&pdev->dev);
970 pm_runtime_disable(&pdev->dev);
971
892 iounmap(host->ctl); 972 iounmap(host->ctl);
893 mmc_free_host(host->mmc); 973 mmc_free_host(host->mmc);
894} 974}
895EXPORT_SYMBOL(tmio_mmc_host_remove); 975EXPORT_SYMBOL(tmio_mmc_host_remove);
896 976
977#ifdef CONFIG_PM
978int tmio_mmc_host_suspend(struct device *dev)
979{
980 struct mmc_host *mmc = dev_get_drvdata(dev);
981 struct tmio_mmc_host *host = mmc_priv(mmc);
982 int ret = mmc_suspend_host(mmc);
983
984 if (!ret)
985 tmio_mmc_disable_mmc_irqs(host, TMIO_MASK_ALL);
986
987 host->pm_error = pm_runtime_put_sync(dev);
988
989 return ret;
990}
991EXPORT_SYMBOL(tmio_mmc_host_suspend);
992
993int tmio_mmc_host_resume(struct device *dev)
994{
995 struct mmc_host *mmc = dev_get_drvdata(dev);
996 struct tmio_mmc_host *host = mmc_priv(mmc);
997
998 /* The MMC core will perform the complete set up */
999 host->pdata->power = false;
1000
1001 if (!host->pm_error)
1002 pm_runtime_get_sync(dev);
1003
1004 tmio_mmc_reset(mmc_priv(mmc));
1005 tmio_mmc_request_dma(host, host->pdata);
1006
1007 return mmc_resume_host(mmc);
1008}
1009EXPORT_SYMBOL(tmio_mmc_host_resume);
1010
1011#endif /* CONFIG_PM */
1012
1013int tmio_mmc_host_runtime_suspend(struct device *dev)
1014{
1015 return 0;
1016}
1017EXPORT_SYMBOL(tmio_mmc_host_runtime_suspend);
1018
1019int tmio_mmc_host_runtime_resume(struct device *dev)
1020{
1021 struct mmc_host *mmc = dev_get_drvdata(dev);
1022 struct tmio_mmc_host *host = mmc_priv(mmc);
1023 struct tmio_mmc_data *pdata = host->pdata;
1024
1025 tmio_mmc_reset(host);
1026
1027 if (pdata->power) {
1028 /* Only entered after a card-insert interrupt */
1029 tmio_mmc_set_ios(mmc, &mmc->ios);
1030 mmc_detect_change(mmc, msecs_to_jiffies(100));
1031 }
1032
1033 return 0;
1034}
1035EXPORT_SYMBOL(tmio_mmc_host_runtime_resume);
1036
897MODULE_LICENSE("GPL v2"); 1037MODULE_LICENSE("GPL v2");
diff --git a/drivers/mmc/host/vub300.c b/drivers/mmc/host/vub300.c
new file mode 100644
index 000000000000..cbb03305b77b
--- /dev/null
+++ b/drivers/mmc/host/vub300.c
@@ -0,0 +1,2506 @@
1/*
2 * Remote VUB300 SDIO/SDmem Host Controller Driver
3 *
4 * Copyright (C) 2010 Elan Digital Systems Limited
5 *
6 * based on USB Skeleton driver - 2.2
7 *
8 * Copyright (C) 2001-2004 Greg Kroah-Hartman (greg@kroah.com)
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation, version 2
13 *
14 * VUB300: is a USB 2.0 client device with a single SDIO/SDmem/MMC slot
15 * Any SDIO/SDmem/MMC device plugged into the VUB300 will appear,
16 * by virtue of this driver, to have been plugged into a local
17 * SDIO host controller, similar to, say, a PCI Ricoh controller
18 * This is because this kernel device driver is both a USB 2.0
19 * client device driver AND an MMC host controller driver. Thus
20 * if there is an existing driver for the inserted SDIO/SDmem/MMC
21 * device then that driver will be used by the kernel to manage
22 * the device in exactly the same fashion as if it had been
23 * directly plugged into, say, a local pci bus Ricoh controller
24 *
25 * RANT: this driver was written using a display 128x48 - converting it
26 * to a line width of 80 makes it very difficult to support. In
27 * particular functions have been broken down into sub functions
28 * and the original meaningful names have been shortened into
29 * cryptic ones.
30 * The problem is that executing a fragment of code subject to
31 * two conditions means an indentation of 24, thus leaving only
32 * 56 characters for a C statement. And that is quite ridiculous!
33 *
34 * Data types: data passed to/from the VUB300 is fixed to a number of
35 * bits and driver data fields reflect that limit by using
36 * u8, u16, u32
37 */
38#include <linux/kernel.h>
39#include <linux/errno.h>
40#include <linux/init.h>
41#include <linux/slab.h>
42#include <linux/module.h>
43#include <linux/kref.h>
44#include <linux/uaccess.h>
45#include <linux/usb.h>
46#include <linux/mutex.h>
47#include <linux/mmc/host.h>
48#include <linux/mmc/card.h>
49#include <linux/mmc/sdio_func.h>
50#include <linux/mmc/sdio_ids.h>
51#include <linux/workqueue.h>
52#include <linux/ctype.h>
53#include <linux/firmware.h>
54#include <linux/scatterlist.h>
55
56struct host_controller_info {
57 u8 info_size;
58 u16 firmware_version;
59 u8 number_of_ports;
60} __packed;
61
62#define FIRMWARE_BLOCK_BOUNDARY 1024
63struct sd_command_header {
64 u8 header_size;
65 u8 header_type;
66 u8 port_number;
67 u8 command_type; /* Bit7 - Rd/Wr */
68 u8 command_index;
69 u8 transfer_size[4]; /* ReadSize + ReadSize */
70 u8 response_type;
71 u8 arguments[4];
72 u8 block_count[2];
73 u8 block_size[2];
74 u8 block_boundary[2];
75 u8 reserved[44]; /* to pad out to 64 bytes */
76} __packed;
77
78struct sd_irqpoll_header {
79 u8 header_size;
80 u8 header_type;
81 u8 port_number;
82 u8 command_type; /* Bit7 - Rd/Wr */
83 u8 padding[16]; /* don't ask why !! */
84 u8 poll_timeout_msb;
85 u8 poll_timeout_lsb;
86 u8 reserved[42]; /* to pad out to 64 bytes */
87} __packed;
88
89struct sd_common_header {
90 u8 header_size;
91 u8 header_type;
92 u8 port_number;
93} __packed;
94
95struct sd_response_header {
96 u8 header_size;
97 u8 header_type;
98 u8 port_number;
99 u8 command_type;
100 u8 command_index;
101 u8 command_response[0];
102} __packed;
103
104struct sd_status_header {
105 u8 header_size;
106 u8 header_type;
107 u8 port_number;
108 u16 port_flags;
109 u32 sdio_clock;
110 u16 host_header_size;
111 u16 func_header_size;
112 u16 ctrl_header_size;
113} __packed;
114
115struct sd_error_header {
116 u8 header_size;
117 u8 header_type;
118 u8 port_number;
119 u8 error_code;
120} __packed;
121
122struct sd_interrupt_header {
123 u8 header_size;
124 u8 header_type;
125 u8 port_number;
126} __packed;
127
128struct offload_registers_access {
129 u8 command_byte[4];
130 u8 Respond_Byte[4];
131} __packed;
132
133#define INTERRUPT_REGISTER_ACCESSES 15
134struct sd_offloaded_interrupt {
135 u8 header_size;
136 u8 header_type;
137 u8 port_number;
138 struct offload_registers_access reg[INTERRUPT_REGISTER_ACCESSES];
139} __packed;
140
141struct sd_register_header {
142 u8 header_size;
143 u8 header_type;
144 u8 port_number;
145 u8 command_type;
146 u8 command_index;
147 u8 command_response[6];
148} __packed;
149
150#define PIGGYBACK_REGISTER_ACCESSES 14
151struct sd_offloaded_piggyback {
152 struct sd_register_header sdio;
153 struct offload_registers_access reg[PIGGYBACK_REGISTER_ACCESSES];
154} __packed;
155
156union sd_response {
157 struct sd_common_header common;
158 struct sd_status_header status;
159 struct sd_error_header error;
160 struct sd_interrupt_header interrupt;
161 struct sd_response_header response;
162 struct sd_offloaded_interrupt irq;
163 struct sd_offloaded_piggyback pig;
164} __packed;
165
166union sd_command {
167 struct sd_command_header head;
168 struct sd_irqpoll_header poll;
169} __packed;
170
171enum SD_RESPONSE_TYPE {
172 SDRT_UNSPECIFIED = 0,
173 SDRT_NONE,
174 SDRT_1,
175 SDRT_1B,
176 SDRT_2,
177 SDRT_3,
178 SDRT_4,
179 SDRT_5,
180 SDRT_5B,
181 SDRT_6,
182 SDRT_7,
183};
184
185#define RESPONSE_INTERRUPT 0x01
186#define RESPONSE_ERROR 0x02
187#define RESPONSE_STATUS 0x03
188#define RESPONSE_IRQ_DISABLED 0x05
189#define RESPONSE_IRQ_ENABLED 0x06
190#define RESPONSE_PIGGYBACKED 0x07
191#define RESPONSE_NO_INTERRUPT 0x08
192#define RESPONSE_PIG_DISABLED 0x09
193#define RESPONSE_PIG_ENABLED 0x0A
194#define SD_ERROR_1BIT_TIMEOUT 0x01
195#define SD_ERROR_4BIT_TIMEOUT 0x02
196#define SD_ERROR_1BIT_CRC_WRONG 0x03
197#define SD_ERROR_4BIT_CRC_WRONG 0x04
198#define SD_ERROR_1BIT_CRC_ERROR 0x05
199#define SD_ERROR_4BIT_CRC_ERROR 0x06
200#define SD_ERROR_NO_CMD_ENDBIT 0x07
201#define SD_ERROR_NO_1BIT_DATEND 0x08
202#define SD_ERROR_NO_4BIT_DATEND 0x09
203#define SD_ERROR_1BIT_UNEXPECTED_TIMEOUT 0x0A
204#define SD_ERROR_4BIT_UNEXPECTED_TIMEOUT 0x0B
205#define SD_ERROR_ILLEGAL_COMMAND 0x0C
206#define SD_ERROR_NO_DEVICE 0x0D
207#define SD_ERROR_TRANSFER_LENGTH 0x0E
208#define SD_ERROR_1BIT_DATA_TIMEOUT 0x0F
209#define SD_ERROR_4BIT_DATA_TIMEOUT 0x10
210#define SD_ERROR_ILLEGAL_STATE 0x11
211#define SD_ERROR_UNKNOWN_ERROR 0x12
212#define SD_ERROR_RESERVED_ERROR 0x13
213#define SD_ERROR_INVALID_FUNCTION 0x14
214#define SD_ERROR_OUT_OF_RANGE 0x15
215#define SD_ERROR_STAT_CMD 0x16
216#define SD_ERROR_STAT_DATA 0x17
217#define SD_ERROR_STAT_CMD_TIMEOUT 0x18
218#define SD_ERROR_SDCRDY_STUCK 0x19
219#define SD_ERROR_UNHANDLED 0x1A
220#define SD_ERROR_OVERRUN 0x1B
221#define SD_ERROR_PIO_TIMEOUT 0x1C
222
223#define FUN(c) (0x000007 & (c->arg>>28))
224#define REG(c) (0x01FFFF & (c->arg>>9))
225
226static int limit_speed_to_24_MHz;
227module_param(limit_speed_to_24_MHz, bool, 0644);
228MODULE_PARM_DESC(limit_speed_to_24_MHz, "Limit Max SDIO Clock Speed to 24 MHz");
229
230static int pad_input_to_usb_pkt;
231module_param(pad_input_to_usb_pkt, bool, 0644);
232MODULE_PARM_DESC(pad_input_to_usb_pkt,
233 "Pad USB data input transfers to whole USB Packet");
234
235static int disable_offload_processing;
236module_param(disable_offload_processing, bool, 0644);
237MODULE_PARM_DESC(disable_offload_processing, "Disable Offload Processing");
238
239static int force_1_bit_data_xfers;
240module_param(force_1_bit_data_xfers, bool, 0644);
241MODULE_PARM_DESC(force_1_bit_data_xfers,
242 "Force SDIO Data Transfers to 1-bit Mode");
243
244static int force_polling_for_irqs;
245module_param(force_polling_for_irqs, bool, 0644);
246MODULE_PARM_DESC(force_polling_for_irqs, "Force Polling for SDIO interrupts");
247
248static int firmware_irqpoll_timeout = 1024;
249module_param(firmware_irqpoll_timeout, int, 0644);
250MODULE_PARM_DESC(firmware_irqpoll_timeout, "VUB300 firmware irqpoll timeout");
251
252static int force_max_req_size = 128;
253module_param(force_max_req_size, int, 0644);
254MODULE_PARM_DESC(force_max_req_size, "set max request size in kBytes");
255
256#ifdef SMSC_DEVELOPMENT_BOARD
257static int firmware_rom_wait_states = 0x04;
258#else
259static int firmware_rom_wait_states = 0x1C;
260#endif
261
262module_param(firmware_rom_wait_states, bool, 0644);
263MODULE_PARM_DESC(firmware_rom_wait_states,
264 "ROM wait states byte=RRRIIEEE (Reserved Internal External)");
265
266#define ELAN_VENDOR_ID 0x2201
267#define VUB300_VENDOR_ID 0x0424
268#define VUB300_PRODUCT_ID 0x012C
269static struct usb_device_id vub300_table[] = {
270 {USB_DEVICE(ELAN_VENDOR_ID, VUB300_PRODUCT_ID)},
271 {USB_DEVICE(VUB300_VENDOR_ID, VUB300_PRODUCT_ID)},
272 {} /* Terminating entry */
273};
274MODULE_DEVICE_TABLE(usb, vub300_table);
275
276static struct workqueue_struct *cmndworkqueue;
277static struct workqueue_struct *pollworkqueue;
278static struct workqueue_struct *deadworkqueue;
279
280static inline int interface_to_InterfaceNumber(struct usb_interface *interface)
281{
282 if (!interface)
283 return -1;
284 if (!interface->cur_altsetting)
285 return -1;
286 return interface->cur_altsetting->desc.bInterfaceNumber;
287}
288
289struct sdio_register {
290 unsigned func_num:3;
291 unsigned sdio_reg:17;
292 unsigned activate:1;
293 unsigned prepared:1;
294 unsigned regvalue:8;
295 unsigned response:8;
296 unsigned sparebit:26;
297};
298
299struct vub300_mmc_host {
300 struct usb_device *udev;
301 struct usb_interface *interface;
302 struct kref kref;
303 struct mutex cmd_mutex;
304 struct mutex irq_mutex;
305 char vub_name[3 + (9 * 8) + 4 + 1]; /* max of 7 sdio fn's */
306 u8 cmnd_out_ep; /* EndPoint for commands */
307 u8 cmnd_res_ep; /* EndPoint for responses */
308 u8 data_out_ep; /* EndPoint for out data */
309 u8 data_inp_ep; /* EndPoint for inp data */
310 bool card_powered;
311 bool card_present;
312 bool read_only;
313 bool large_usb_packets;
314 bool app_spec; /* ApplicationSpecific */
315 bool irq_enabled; /* by the MMC CORE */
316 bool irq_disabled; /* in the firmware */
317 unsigned bus_width:4;
318 u8 total_offload_count;
319 u8 dynamic_register_count;
320 u8 resp_len;
321 u32 datasize;
322 int errors;
323 int usb_transport_fail;
324 int usb_timed_out;
325 int irqs_queued;
326 struct sdio_register sdio_register[16];
327 struct offload_interrupt_function_register {
328#define MAXREGBITS 4
329#define MAXREGS (1<<MAXREGBITS)
330#define MAXREGMASK (MAXREGS-1)
331 u8 offload_count;
332 u32 offload_point;
333 struct offload_registers_access reg[MAXREGS];
334 } fn[8];
335 u16 fbs[8]; /* Function Block Size */
336 struct mmc_command *cmd;
337 struct mmc_request *req;
338 struct mmc_data *data;
339 struct mmc_host *mmc;
340 struct urb *urb;
341 struct urb *command_out_urb;
342 struct urb *command_res_urb;
343 struct completion command_complete;
344 struct completion irqpoll_complete;
345 union sd_command cmnd;
346 union sd_response resp;
347 struct timer_list sg_transfer_timer;
348 struct usb_sg_request sg_request;
349 struct timer_list inactivity_timer;
350 struct work_struct deadwork;
351 struct work_struct cmndwork;
352 struct delayed_work pollwork;
353 struct host_controller_info hc_info;
354 struct sd_status_header system_port_status;
355 u8 padded_buffer[64];
356};
357
358#define kref_to_vub300_mmc_host(d) container_of(d, struct vub300_mmc_host, kref)
359#define SET_TRANSFER_PSEUDOCODE 21
360#define SET_INTERRUPT_PSEUDOCODE 20
361#define SET_FAILURE_MODE 18
362#define SET_ROM_WAIT_STATES 16
363#define SET_IRQ_ENABLE 13
364#define SET_CLOCK_SPEED 11
365#define SET_FUNCTION_BLOCK_SIZE 9
366#define SET_SD_DATA_MODE 6
367#define SET_SD_POWER 4
368#define ENTER_DFU_MODE 3
369#define GET_HC_INF0 1
370#define GET_SYSTEM_PORT_STATUS 0
371
372static void vub300_delete(struct kref *kref)
373{ /* kref callback - softirq */
374 struct vub300_mmc_host *vub300 = kref_to_vub300_mmc_host(kref);
375 struct mmc_host *mmc = vub300->mmc;
376 usb_free_urb(vub300->command_out_urb);
377 vub300->command_out_urb = NULL;
378 usb_free_urb(vub300->command_res_urb);
379 vub300->command_res_urb = NULL;
380 usb_put_dev(vub300->udev);
381 mmc_free_host(mmc);
382 /*
383 * and hence also frees vub300
384 * which is contained at the end of struct mmc
385 */
386}
387
388static void vub300_queue_cmnd_work(struct vub300_mmc_host *vub300)
389{
390 kref_get(&vub300->kref);
391 if (queue_work(cmndworkqueue, &vub300->cmndwork)) {
392 /*
393 * then the cmndworkqueue was not previously
394 * running and the above get ref is obvious
395 * required and will be put when the thread
396 * terminates by a specific call
397 */
398 } else {
399 /*
400 * the cmndworkqueue was already running from
401 * a previous invocation and thus to keep the
402 * kref counts correct we must undo the get
403 */
404 kref_put(&vub300->kref, vub300_delete);
405 }
406}
407
408static void vub300_queue_poll_work(struct vub300_mmc_host *vub300, int delay)
409{
410 kref_get(&vub300->kref);
411 if (queue_delayed_work(pollworkqueue, &vub300->pollwork, delay)) {
412 /*
413 * then the pollworkqueue was not previously
414 * running and the above get ref is obvious
415 * required and will be put when the thread
416 * terminates by a specific call
417 */
418 } else {
419 /*
420 * the pollworkqueue was already running from
421 * a previous invocation and thus to keep the
422 * kref counts correct we must undo the get
423 */
424 kref_put(&vub300->kref, vub300_delete);
425 }
426}
427
428static void vub300_queue_dead_work(struct vub300_mmc_host *vub300)
429{
430 kref_get(&vub300->kref);
431 if (queue_work(deadworkqueue, &vub300->deadwork)) {
432 /*
433 * then the deadworkqueue was not previously
434 * running and the above get ref is obvious
435 * required and will be put when the thread
436 * terminates by a specific call
437 */
438 } else {
439 /*
440 * the deadworkqueue was already running from
441 * a previous invocation and thus to keep the
442 * kref counts correct we must undo the get
443 */
444 kref_put(&vub300->kref, vub300_delete);
445 }
446}
447
448static void irqpoll_res_completed(struct urb *urb)
449{ /* urb completion handler - hardirq */
450 struct vub300_mmc_host *vub300 = (struct vub300_mmc_host *)urb->context;
451 if (urb->status)
452 vub300->usb_transport_fail = urb->status;
453 complete(&vub300->irqpoll_complete);
454}
455
456static void irqpoll_out_completed(struct urb *urb)
457{ /* urb completion handler - hardirq */
458 struct vub300_mmc_host *vub300 = (struct vub300_mmc_host *)urb->context;
459 if (urb->status) {
460 vub300->usb_transport_fail = urb->status;
461 complete(&vub300->irqpoll_complete);
462 return;
463 } else {
464 int ret;
465 unsigned int pipe =
466 usb_rcvbulkpipe(vub300->udev, vub300->cmnd_res_ep);
467 usb_fill_bulk_urb(vub300->command_res_urb, vub300->udev, pipe,
468 &vub300->resp, sizeof(vub300->resp),
469 irqpoll_res_completed, vub300);
470 vub300->command_res_urb->actual_length = 0;
471 ret = usb_submit_urb(vub300->command_res_urb, GFP_ATOMIC);
472 if (ret) {
473 vub300->usb_transport_fail = ret;
474 complete(&vub300->irqpoll_complete);
475 }
476 return;
477 }
478}
479
480static void send_irqpoll(struct vub300_mmc_host *vub300)
481{
482 /* cmd_mutex is held by vub300_pollwork_thread */
483 int retval;
484 int timeout = 0xFFFF & (0x0001FFFF - firmware_irqpoll_timeout);
485 vub300->cmnd.poll.header_size = 22;
486 vub300->cmnd.poll.header_type = 1;
487 vub300->cmnd.poll.port_number = 0;
488 vub300->cmnd.poll.command_type = 2;
489 vub300->cmnd.poll.poll_timeout_lsb = 0xFF & (unsigned)timeout;
490 vub300->cmnd.poll.poll_timeout_msb = 0xFF & (unsigned)(timeout >> 8);
491 usb_fill_bulk_urb(vub300->command_out_urb, vub300->udev,
492 usb_sndbulkpipe(vub300->udev, vub300->cmnd_out_ep)
493 , &vub300->cmnd, sizeof(vub300->cmnd)
494 , irqpoll_out_completed, vub300);
495 retval = usb_submit_urb(vub300->command_out_urb, GFP_KERNEL);
496 if (0 > retval) {
497 vub300->usb_transport_fail = retval;
498 vub300_queue_poll_work(vub300, 1);
499 complete(&vub300->irqpoll_complete);
500 return;
501 } else {
502 return;
503 }
504}
505
506static void new_system_port_status(struct vub300_mmc_host *vub300)
507{
508 int old_card_present = vub300->card_present;
509 int new_card_present =
510 (0x0001 & vub300->system_port_status.port_flags) ? 1 : 0;
511 vub300->read_only =
512 (0x0010 & vub300->system_port_status.port_flags) ? 1 : 0;
513 if (new_card_present && !old_card_present) {
514 dev_info(&vub300->udev->dev, "card just inserted\n");
515 vub300->card_present = 1;
516 vub300->bus_width = 0;
517 if (disable_offload_processing)
518 strncpy(vub300->vub_name, "EMPTY Processing Disabled",
519 sizeof(vub300->vub_name));
520 else
521 vub300->vub_name[0] = 0;
522 mmc_detect_change(vub300->mmc, 1);
523 } else if (!new_card_present && old_card_present) {
524 dev_info(&vub300->udev->dev, "card just ejected\n");
525 vub300->card_present = 0;
526 mmc_detect_change(vub300->mmc, 0);
527 } else {
528 /* no change */
529 }
530}
531
532static void __add_offloaded_reg_to_fifo(struct vub300_mmc_host *vub300,
533 struct offload_registers_access
534 *register_access, u8 func)
535{
536 u8 r = vub300->fn[func].offload_point + vub300->fn[func].offload_count;
537 memcpy(&vub300->fn[func].reg[MAXREGMASK & r], register_access,
538 sizeof(struct offload_registers_access));
539 vub300->fn[func].offload_count += 1;
540 vub300->total_offload_count += 1;
541}
542
543static void add_offloaded_reg(struct vub300_mmc_host *vub300,
544 struct offload_registers_access *register_access)
545{
546 u32 Register = ((0x03 & register_access->command_byte[0]) << 15)
547 | ((0xFF & register_access->command_byte[1]) << 7)
548 | ((0xFE & register_access->command_byte[2]) >> 1);
549 u8 func = ((0x70 & register_access->command_byte[0]) >> 4);
550 u8 regs = vub300->dynamic_register_count;
551 u8 i = 0;
552 while (0 < regs-- && 1 == vub300->sdio_register[i].activate) {
553 if (vub300->sdio_register[i].func_num == func &&
554 vub300->sdio_register[i].sdio_reg == Register) {
555 if (vub300->sdio_register[i].prepared == 0)
556 vub300->sdio_register[i].prepared = 1;
557 vub300->sdio_register[i].response =
558 register_access->Respond_Byte[2];
559 vub300->sdio_register[i].regvalue =
560 register_access->Respond_Byte[3];
561 return;
562 } else {
563 i += 1;
564 continue;
565 }
566 };
567 __add_offloaded_reg_to_fifo(vub300, register_access, func);
568}
569
570static void check_vub300_port_status(struct vub300_mmc_host *vub300)
571{
572 /*
573 * cmd_mutex is held by vub300_pollwork_thread,
574 * vub300_deadwork_thread or vub300_cmndwork_thread
575 */
576 int retval;
577 retval =
578 usb_control_msg(vub300->udev, usb_rcvctrlpipe(vub300->udev, 0),
579 GET_SYSTEM_PORT_STATUS,
580 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
581 0x0000, 0x0000, &vub300->system_port_status,
582 sizeof(vub300->system_port_status), HZ);
583 if (sizeof(vub300->system_port_status) == retval)
584 new_system_port_status(vub300);
585}
586
587static void __vub300_irqpoll_response(struct vub300_mmc_host *vub300)
588{
589 /* cmd_mutex is held by vub300_pollwork_thread */
590 if (vub300->command_res_urb->actual_length == 0)
591 return;
592
593 switch (vub300->resp.common.header_type) {
594 case RESPONSE_INTERRUPT:
595 mutex_lock(&vub300->irq_mutex);
596 if (vub300->irq_enabled)
597 mmc_signal_sdio_irq(vub300->mmc);
598 else
599 vub300->irqs_queued += 1;
600 vub300->irq_disabled = 1;
601 mutex_unlock(&vub300->irq_mutex);
602 break;
603 case RESPONSE_ERROR:
604 if (vub300->resp.error.error_code == SD_ERROR_NO_DEVICE)
605 check_vub300_port_status(vub300);
606 break;
607 case RESPONSE_STATUS:
608 vub300->system_port_status = vub300->resp.status;
609 new_system_port_status(vub300);
610 if (!vub300->card_present)
611 vub300_queue_poll_work(vub300, HZ / 5);
612 break;
613 case RESPONSE_IRQ_DISABLED:
614 {
615 int offloaded_data_length = vub300->resp.common.header_size - 3;
616 int register_count = offloaded_data_length >> 3;
617 int ri = 0;
618 while (register_count--) {
619 add_offloaded_reg(vub300, &vub300->resp.irq.reg[ri]);
620 ri += 1;
621 }
622 mutex_lock(&vub300->irq_mutex);
623 if (vub300->irq_enabled)
624 mmc_signal_sdio_irq(vub300->mmc);
625 else
626 vub300->irqs_queued += 1;
627 vub300->irq_disabled = 1;
628 mutex_unlock(&vub300->irq_mutex);
629 break;
630 }
631 case RESPONSE_IRQ_ENABLED:
632 {
633 int offloaded_data_length = vub300->resp.common.header_size - 3;
634 int register_count = offloaded_data_length >> 3;
635 int ri = 0;
636 while (register_count--) {
637 add_offloaded_reg(vub300, &vub300->resp.irq.reg[ri]);
638 ri += 1;
639 }
640 mutex_lock(&vub300->irq_mutex);
641 if (vub300->irq_enabled)
642 mmc_signal_sdio_irq(vub300->mmc);
643 else if (vub300->irqs_queued)
644 vub300->irqs_queued += 1;
645 else
646 vub300->irqs_queued += 1;
647 vub300->irq_disabled = 0;
648 mutex_unlock(&vub300->irq_mutex);
649 break;
650 }
651 case RESPONSE_NO_INTERRUPT:
652 vub300_queue_poll_work(vub300, 1);
653 break;
654 default:
655 break;
656 }
657}
658
659static void __do_poll(struct vub300_mmc_host *vub300)
660{
661 /* cmd_mutex is held by vub300_pollwork_thread */
662 long commretval;
663 mod_timer(&vub300->inactivity_timer, jiffies + HZ);
664 init_completion(&vub300->irqpoll_complete);
665 send_irqpoll(vub300);
666 commretval = wait_for_completion_timeout(&vub300->irqpoll_complete,
667 msecs_to_jiffies(500));
668 if (vub300->usb_transport_fail) {
669 /* no need to do anything */
670 } else if (commretval == 0) {
671 vub300->usb_timed_out = 1;
672 usb_kill_urb(vub300->command_out_urb);
673 usb_kill_urb(vub300->command_res_urb);
674 } else if (commretval < 0) {
675 vub300_queue_poll_work(vub300, 1);
676 } else { /* commretval > 0 */
677 __vub300_irqpoll_response(vub300);
678 }
679}
680
681/* this thread runs only when the driver
682 * is trying to poll the device for an IRQ
683 */
684static void vub300_pollwork_thread(struct work_struct *work)
685{ /* NOT irq */
686 struct vub300_mmc_host *vub300 = container_of(work,
687 struct vub300_mmc_host, pollwork.work);
688 if (!vub300->interface) {
689 kref_put(&vub300->kref, vub300_delete);
690 return;
691 }
692 mutex_lock(&vub300->cmd_mutex);
693 if (vub300->cmd) {
694 vub300_queue_poll_work(vub300, 1);
695 } else if (!vub300->card_present) {
696 /* no need to do anything */
697 } else { /* vub300->card_present */
698 mutex_lock(&vub300->irq_mutex);
699 if (!vub300->irq_enabled) {
700 mutex_unlock(&vub300->irq_mutex);
701 } else if (vub300->irqs_queued) {
702 vub300->irqs_queued -= 1;
703 mmc_signal_sdio_irq(vub300->mmc);
704 mod_timer(&vub300->inactivity_timer, jiffies + HZ);
705 mutex_unlock(&vub300->irq_mutex);
706 } else { /* NOT vub300->irqs_queued */
707 mutex_unlock(&vub300->irq_mutex);
708 __do_poll(vub300);
709 }
710 }
711 mutex_unlock(&vub300->cmd_mutex);
712 kref_put(&vub300->kref, vub300_delete);
713}
714
715static void vub300_deadwork_thread(struct work_struct *work)
716{ /* NOT irq */
717 struct vub300_mmc_host *vub300 =
718 container_of(work, struct vub300_mmc_host, deadwork);
719 if (!vub300->interface) {
720 kref_put(&vub300->kref, vub300_delete);
721 return;
722 }
723 mutex_lock(&vub300->cmd_mutex);
724 if (vub300->cmd) {
725 /*
726 * a command got in as the inactivity
727 * timer expired - so we just let the
728 * processing of the command show if
729 * the device is dead
730 */
731 } else if (vub300->card_present) {
732 check_vub300_port_status(vub300);
733 } else if (vub300->mmc && vub300->mmc->card &&
734 mmc_card_present(vub300->mmc->card)) {
735 /*
736 * the MMC core must not have responded
737 * to the previous indication - lets
738 * hope that it eventually does so we
739 * will just ignore this for now
740 */
741 } else {
742 check_vub300_port_status(vub300);
743 }
744 mod_timer(&vub300->inactivity_timer, jiffies + HZ);
745 mutex_unlock(&vub300->cmd_mutex);
746 kref_put(&vub300->kref, vub300_delete);
747}
748
749static void vub300_inactivity_timer_expired(unsigned long data)
750{ /* softirq */
751 struct vub300_mmc_host *vub300 = (struct vub300_mmc_host *)data;
752 if (!vub300->interface) {
753 kref_put(&vub300->kref, vub300_delete);
754 } else if (vub300->cmd) {
755 mod_timer(&vub300->inactivity_timer, jiffies + HZ);
756 } else {
757 vub300_queue_dead_work(vub300);
758 mod_timer(&vub300->inactivity_timer, jiffies + HZ);
759 }
760}
761
762static int vub300_response_error(u8 error_code)
763{
764 switch (error_code) {
765 case SD_ERROR_PIO_TIMEOUT:
766 case SD_ERROR_1BIT_TIMEOUT:
767 case SD_ERROR_4BIT_TIMEOUT:
768 return -ETIMEDOUT;
769 case SD_ERROR_STAT_DATA:
770 case SD_ERROR_OVERRUN:
771 case SD_ERROR_STAT_CMD:
772 case SD_ERROR_STAT_CMD_TIMEOUT:
773 case SD_ERROR_SDCRDY_STUCK:
774 case SD_ERROR_UNHANDLED:
775 case SD_ERROR_1BIT_CRC_WRONG:
776 case SD_ERROR_4BIT_CRC_WRONG:
777 case SD_ERROR_1BIT_CRC_ERROR:
778 case SD_ERROR_4BIT_CRC_ERROR:
779 case SD_ERROR_NO_CMD_ENDBIT:
780 case SD_ERROR_NO_1BIT_DATEND:
781 case SD_ERROR_NO_4BIT_DATEND:
782 case SD_ERROR_1BIT_DATA_TIMEOUT:
783 case SD_ERROR_4BIT_DATA_TIMEOUT:
784 case SD_ERROR_1BIT_UNEXPECTED_TIMEOUT:
785 case SD_ERROR_4BIT_UNEXPECTED_TIMEOUT:
786 return -EILSEQ;
787 case 33:
788 return -EILSEQ;
789 case SD_ERROR_ILLEGAL_COMMAND:
790 return -EINVAL;
791 case SD_ERROR_NO_DEVICE:
792 return -ENOMEDIUM;
793 default:
794 return -ENODEV;
795 }
796}
797
798static void command_res_completed(struct urb *urb)
799{ /* urb completion handler - hardirq */
800 struct vub300_mmc_host *vub300 = (struct vub300_mmc_host *)urb->context;
801 if (urb->status) {
802 /* we have to let the initiator handle the error */
803 } else if (vub300->command_res_urb->actual_length == 0) {
804 /*
805 * we have seen this happen once or twice and
806 * we suspect a buggy USB host controller
807 */
808 } else if (!vub300->data) {
809 /* this means that the command (typically CMD52) suceeded */
810 } else if (vub300->resp.common.header_type != 0x02) {
811 /*
812 * this is an error response from the VUB300 chip
813 * and we let the initiator handle it
814 */
815 } else if (vub300->urb) {
816 vub300->cmd->error =
817 vub300_response_error(vub300->resp.error.error_code);
818 usb_unlink_urb(vub300->urb);
819 } else {
820 vub300->cmd->error =
821 vub300_response_error(vub300->resp.error.error_code);
822 usb_sg_cancel(&vub300->sg_request);
823 }
824 complete(&vub300->command_complete); /* got_response_in */
825}
826
827static void command_out_completed(struct urb *urb)
828{ /* urb completion handler - hardirq */
829 struct vub300_mmc_host *vub300 = (struct vub300_mmc_host *)urb->context;
830 if (urb->status) {
831 complete(&vub300->command_complete);
832 } else {
833 int ret;
834 unsigned int pipe =
835 usb_rcvbulkpipe(vub300->udev, vub300->cmnd_res_ep);
836 usb_fill_bulk_urb(vub300->command_res_urb, vub300->udev, pipe,
837 &vub300->resp, sizeof(vub300->resp),
838 command_res_completed, vub300);
839 vub300->command_res_urb->actual_length = 0;
840 ret = usb_submit_urb(vub300->command_res_urb, GFP_ATOMIC);
841 if (ret == 0) {
842 /*
843 * the urb completion handler will call
844 * our completion handler
845 */
846 } else {
847 /*
848 * and thus we only call it directly
849 * when it will not be called
850 */
851 complete(&vub300->command_complete);
852 }
853 }
854}
855
856/*
857 * the STUFF bits are masked out for the comparisons
858 */
859static void snoop_block_size_and_bus_width(struct vub300_mmc_host *vub300,
860 u32 cmd_arg)
861{
862 if ((0xFBFFFE00 & cmd_arg) == 0x80022200)
863 vub300->fbs[1] = (cmd_arg << 8) | (0x00FF & vub300->fbs[1]);
864 else if ((0xFBFFFE00 & cmd_arg) == 0x80022000)
865 vub300->fbs[1] = (0xFF & cmd_arg) | (0xFF00 & vub300->fbs[1]);
866 else if ((0xFBFFFE00 & cmd_arg) == 0x80042200)
867 vub300->fbs[2] = (cmd_arg << 8) | (0x00FF & vub300->fbs[2]);
868 else if ((0xFBFFFE00 & cmd_arg) == 0x80042000)
869 vub300->fbs[2] = (0xFF & cmd_arg) | (0xFF00 & vub300->fbs[2]);
870 else if ((0xFBFFFE00 & cmd_arg) == 0x80062200)
871 vub300->fbs[3] = (cmd_arg << 8) | (0x00FF & vub300->fbs[3]);
872 else if ((0xFBFFFE00 & cmd_arg) == 0x80062000)
873 vub300->fbs[3] = (0xFF & cmd_arg) | (0xFF00 & vub300->fbs[3]);
874 else if ((0xFBFFFE00 & cmd_arg) == 0x80082200)
875 vub300->fbs[4] = (cmd_arg << 8) | (0x00FF & vub300->fbs[4]);
876 else if ((0xFBFFFE00 & cmd_arg) == 0x80082000)
877 vub300->fbs[4] = (0xFF & cmd_arg) | (0xFF00 & vub300->fbs[4]);
878 else if ((0xFBFFFE00 & cmd_arg) == 0x800A2200)
879 vub300->fbs[5] = (cmd_arg << 8) | (0x00FF & vub300->fbs[5]);
880 else if ((0xFBFFFE00 & cmd_arg) == 0x800A2000)
881 vub300->fbs[5] = (0xFF & cmd_arg) | (0xFF00 & vub300->fbs[5]);
882 else if ((0xFBFFFE00 & cmd_arg) == 0x800C2200)
883 vub300->fbs[6] = (cmd_arg << 8) | (0x00FF & vub300->fbs[6]);
884 else if ((0xFBFFFE00 & cmd_arg) == 0x800C2000)
885 vub300->fbs[6] = (0xFF & cmd_arg) | (0xFF00 & vub300->fbs[6]);
886 else if ((0xFBFFFE00 & cmd_arg) == 0x800E2200)
887 vub300->fbs[7] = (cmd_arg << 8) | (0x00FF & vub300->fbs[7]);
888 else if ((0xFBFFFE00 & cmd_arg) == 0x800E2000)
889 vub300->fbs[7] = (0xFF & cmd_arg) | (0xFF00 & vub300->fbs[7]);
890 else if ((0xFBFFFE03 & cmd_arg) == 0x80000E00)
891 vub300->bus_width = 1;
892 else if ((0xFBFFFE03 & cmd_arg) == 0x80000E02)
893 vub300->bus_width = 4;
894}
895
896static void send_command(struct vub300_mmc_host *vub300)
897{
898 /* cmd_mutex is held by vub300_cmndwork_thread */
899 struct mmc_command *cmd = vub300->cmd;
900 struct mmc_data *data = vub300->data;
901 int retval;
902 int i;
903 u8 response_type;
904 if (vub300->app_spec) {
905 switch (cmd->opcode) {
906 case 6:
907 response_type = SDRT_1;
908 vub300->resp_len = 6;
909 if (0x00000000 == (0x00000003 & cmd->arg))
910 vub300->bus_width = 1;
911 else if (0x00000002 == (0x00000003 & cmd->arg))
912 vub300->bus_width = 4;
913 else
914 dev_err(&vub300->udev->dev,
915 "unexpected ACMD6 bus_width=%d\n",
916 0x00000003 & cmd->arg);
917 break;
918 case 13:
919 response_type = SDRT_1;
920 vub300->resp_len = 6;
921 break;
922 case 22:
923 response_type = SDRT_1;
924 vub300->resp_len = 6;
925 break;
926 case 23:
927 response_type = SDRT_1;
928 vub300->resp_len = 6;
929 break;
930 case 41:
931 response_type = SDRT_3;
932 vub300->resp_len = 6;
933 break;
934 case 42:
935 response_type = SDRT_1;
936 vub300->resp_len = 6;
937 break;
938 case 51:
939 response_type = SDRT_1;
940 vub300->resp_len = 6;
941 break;
942 case 55:
943 response_type = SDRT_1;
944 vub300->resp_len = 6;
945 break;
946 default:
947 vub300->resp_len = 0;
948 cmd->error = -EINVAL;
949 complete(&vub300->command_complete);
950 return;
951 }
952 vub300->app_spec = 0;
953 } else {
954 switch (cmd->opcode) {
955 case 0:
956 response_type = SDRT_NONE;
957 vub300->resp_len = 0;
958 break;
959 case 1:
960 response_type = SDRT_3;
961 vub300->resp_len = 6;
962 break;
963 case 2:
964 response_type = SDRT_2;
965 vub300->resp_len = 17;
966 break;
967 case 3:
968 response_type = SDRT_6;
969 vub300->resp_len = 6;
970 break;
971 case 4:
972 response_type = SDRT_NONE;
973 vub300->resp_len = 0;
974 break;
975 case 5:
976 response_type = SDRT_4;
977 vub300->resp_len = 6;
978 break;
979 case 6:
980 response_type = SDRT_1;
981 vub300->resp_len = 6;
982 break;
983 case 7:
984 response_type = SDRT_1B;
985 vub300->resp_len = 6;
986 break;
987 case 8:
988 response_type = SDRT_7;
989 vub300->resp_len = 6;
990 break;
991 case 9:
992 response_type = SDRT_2;
993 vub300->resp_len = 17;
994 break;
995 case 10:
996 response_type = SDRT_2;
997 vub300->resp_len = 17;
998 break;
999 case 12:
1000 response_type = SDRT_1B;
1001 vub300->resp_len = 6;
1002 break;
1003 case 13:
1004 response_type = SDRT_1;
1005 vub300->resp_len = 6;
1006 break;
1007 case 15:
1008 response_type = SDRT_NONE;
1009 vub300->resp_len = 0;
1010 break;
1011 case 16:
1012 for (i = 0; i < ARRAY_SIZE(vub300->fbs); i++)
1013 vub300->fbs[i] = 0xFFFF & cmd->arg;
1014 response_type = SDRT_1;
1015 vub300->resp_len = 6;
1016 break;
1017 case 17:
1018 case 18:
1019 case 24:
1020 case 25:
1021 case 27:
1022 response_type = SDRT_1;
1023 vub300->resp_len = 6;
1024 break;
1025 case 28:
1026 case 29:
1027 response_type = SDRT_1B;
1028 vub300->resp_len = 6;
1029 break;
1030 case 30:
1031 case 32:
1032 case 33:
1033 response_type = SDRT_1;
1034 vub300->resp_len = 6;
1035 break;
1036 case 38:
1037 response_type = SDRT_1B;
1038 vub300->resp_len = 6;
1039 break;
1040 case 42:
1041 response_type = SDRT_1;
1042 vub300->resp_len = 6;
1043 break;
1044 case 52:
1045 response_type = SDRT_5;
1046 vub300->resp_len = 6;
1047 snoop_block_size_and_bus_width(vub300, cmd->arg);
1048 break;
1049 case 53:
1050 response_type = SDRT_5;
1051 vub300->resp_len = 6;
1052 break;
1053 case 55:
1054 response_type = SDRT_1;
1055 vub300->resp_len = 6;
1056 vub300->app_spec = 1;
1057 break;
1058 case 56:
1059 response_type = SDRT_1;
1060 vub300->resp_len = 6;
1061 break;
1062 default:
1063 vub300->resp_len = 0;
1064 cmd->error = -EINVAL;
1065 complete(&vub300->command_complete);
1066 return;
1067 }
1068 }
1069 /*
1070 * it is a shame that we can not use "sizeof(struct sd_command_header)"
1071 * this is because the packet _must_ be padded to 64 bytes
1072 */
1073 vub300->cmnd.head.header_size = 20;
1074 vub300->cmnd.head.header_type = 0x00;
1075 vub300->cmnd.head.port_number = 0; /* "0" means port 1 */
1076 vub300->cmnd.head.command_type = 0x00; /* standard read command */
1077 vub300->cmnd.head.response_type = response_type;
1078 vub300->cmnd.head.command_index = cmd->opcode;
1079 vub300->cmnd.head.arguments[0] = cmd->arg >> 24;
1080 vub300->cmnd.head.arguments[1] = cmd->arg >> 16;
1081 vub300->cmnd.head.arguments[2] = cmd->arg >> 8;
1082 vub300->cmnd.head.arguments[3] = cmd->arg >> 0;
1083 if (cmd->opcode == 52) {
1084 int fn = 0x7 & (cmd->arg >> 28);
1085 vub300->cmnd.head.block_count[0] = 0;
1086 vub300->cmnd.head.block_count[1] = 0;
1087 vub300->cmnd.head.block_size[0] = (vub300->fbs[fn] >> 8) & 0xFF;
1088 vub300->cmnd.head.block_size[1] = (vub300->fbs[fn] >> 0) & 0xFF;
1089 vub300->cmnd.head.command_type = 0x00;
1090 vub300->cmnd.head.transfer_size[0] = 0;
1091 vub300->cmnd.head.transfer_size[1] = 0;
1092 vub300->cmnd.head.transfer_size[2] = 0;
1093 vub300->cmnd.head.transfer_size[3] = 0;
1094 } else if (!data) {
1095 vub300->cmnd.head.block_count[0] = 0;
1096 vub300->cmnd.head.block_count[1] = 0;
1097 vub300->cmnd.head.block_size[0] = (vub300->fbs[0] >> 8) & 0xFF;
1098 vub300->cmnd.head.block_size[1] = (vub300->fbs[0] >> 0) & 0xFF;
1099 vub300->cmnd.head.command_type = 0x00;
1100 vub300->cmnd.head.transfer_size[0] = 0;
1101 vub300->cmnd.head.transfer_size[1] = 0;
1102 vub300->cmnd.head.transfer_size[2] = 0;
1103 vub300->cmnd.head.transfer_size[3] = 0;
1104 } else if (cmd->opcode == 53) {
1105 int fn = 0x7 & (cmd->arg >> 28);
1106 if (0x08 & vub300->cmnd.head.arguments[0]) { /* BLOCK MODE */
1107 vub300->cmnd.head.block_count[0] =
1108 (data->blocks >> 8) & 0xFF;
1109 vub300->cmnd.head.block_count[1] =
1110 (data->blocks >> 0) & 0xFF;
1111 vub300->cmnd.head.block_size[0] =
1112 (data->blksz >> 8) & 0xFF;
1113 vub300->cmnd.head.block_size[1] =
1114 (data->blksz >> 0) & 0xFF;
1115 } else { /* BYTE MODE */
1116 vub300->cmnd.head.block_count[0] = 0;
1117 vub300->cmnd.head.block_count[1] = 0;
1118 vub300->cmnd.head.block_size[0] =
1119 (vub300->datasize >> 8) & 0xFF;
1120 vub300->cmnd.head.block_size[1] =
1121 (vub300->datasize >> 0) & 0xFF;
1122 }
1123 vub300->cmnd.head.command_type =
1124 (MMC_DATA_READ & data->flags) ? 0x00 : 0x80;
1125 vub300->cmnd.head.transfer_size[0] =
1126 (vub300->datasize >> 24) & 0xFF;
1127 vub300->cmnd.head.transfer_size[1] =
1128 (vub300->datasize >> 16) & 0xFF;
1129 vub300->cmnd.head.transfer_size[2] =
1130 (vub300->datasize >> 8) & 0xFF;
1131 vub300->cmnd.head.transfer_size[3] =
1132 (vub300->datasize >> 0) & 0xFF;
1133 if (vub300->datasize < vub300->fbs[fn]) {
1134 vub300->cmnd.head.block_count[0] = 0;
1135 vub300->cmnd.head.block_count[1] = 0;
1136 }
1137 } else {
1138 vub300->cmnd.head.block_count[0] = (data->blocks >> 8) & 0xFF;
1139 vub300->cmnd.head.block_count[1] = (data->blocks >> 0) & 0xFF;
1140 vub300->cmnd.head.block_size[0] = (data->blksz >> 8) & 0xFF;
1141 vub300->cmnd.head.block_size[1] = (data->blksz >> 0) & 0xFF;
1142 vub300->cmnd.head.command_type =
1143 (MMC_DATA_READ & data->flags) ? 0x00 : 0x80;
1144 vub300->cmnd.head.transfer_size[0] =
1145 (vub300->datasize >> 24) & 0xFF;
1146 vub300->cmnd.head.transfer_size[1] =
1147 (vub300->datasize >> 16) & 0xFF;
1148 vub300->cmnd.head.transfer_size[2] =
1149 (vub300->datasize >> 8) & 0xFF;
1150 vub300->cmnd.head.transfer_size[3] =
1151 (vub300->datasize >> 0) & 0xFF;
1152 if (vub300->datasize < vub300->fbs[0]) {
1153 vub300->cmnd.head.block_count[0] = 0;
1154 vub300->cmnd.head.block_count[1] = 0;
1155 }
1156 }
1157 if (vub300->cmnd.head.block_size[0] || vub300->cmnd.head.block_size[1]) {
1158 u16 block_size = vub300->cmnd.head.block_size[1] |
1159 (vub300->cmnd.head.block_size[0] << 8);
1160 u16 block_boundary = FIRMWARE_BLOCK_BOUNDARY -
1161 (FIRMWARE_BLOCK_BOUNDARY % block_size);
1162 vub300->cmnd.head.block_boundary[0] =
1163 (block_boundary >> 8) & 0xFF;
1164 vub300->cmnd.head.block_boundary[1] =
1165 (block_boundary >> 0) & 0xFF;
1166 } else {
1167 vub300->cmnd.head.block_boundary[0] = 0;
1168 vub300->cmnd.head.block_boundary[1] = 0;
1169 }
1170 usb_fill_bulk_urb(vub300->command_out_urb, vub300->udev,
1171 usb_sndbulkpipe(vub300->udev, vub300->cmnd_out_ep),
1172 &vub300->cmnd, sizeof(vub300->cmnd),
1173 command_out_completed, vub300);
1174 retval = usb_submit_urb(vub300->command_out_urb, GFP_KERNEL);
1175 if (retval < 0) {
1176 cmd->error = retval;
1177 complete(&vub300->command_complete);
1178 return;
1179 } else {
1180 return;
1181 }
1182}
1183
1184/*
1185 * timer callback runs in atomic mode
1186 * so it cannot call usb_kill_urb()
1187 */
1188static void vub300_sg_timed_out(unsigned long data)
1189{
1190 struct vub300_mmc_host *vub300 = (struct vub300_mmc_host *)data;
1191 vub300->usb_timed_out = 1;
1192 usb_sg_cancel(&vub300->sg_request);
1193 usb_unlink_urb(vub300->command_out_urb);
1194 usb_unlink_urb(vub300->command_res_urb);
1195}
1196
1197static u16 roundup_to_multiple_of_64(u16 number)
1198{
1199 return 0xFFC0 & (0x3F + number);
1200}
1201
1202/*
1203 * this is a separate function to solve the 80 column width restriction
1204 */
1205static void __download_offload_pseudocode(struct vub300_mmc_host *vub300,
1206 const struct firmware *fw)
1207{
1208 u8 register_count = 0;
1209 u16 ts = 0;
1210 u16 interrupt_size = 0;
1211 const u8 *data = fw->data;
1212 int size = fw->size;
1213 u8 c;
1214 dev_info(&vub300->udev->dev, "using %s for SDIO offload processing\n",
1215 vub300->vub_name);
1216 do {
1217 c = *data++;
1218 } while (size-- && c); /* skip comment */
1219 dev_info(&vub300->udev->dev, "using offload firmware %s %s\n", fw->data,
1220 vub300->vub_name);
1221 if (size < 4) {
1222 dev_err(&vub300->udev->dev,
1223 "corrupt offload pseudocode in firmware %s\n",
1224 vub300->vub_name);
1225 strncpy(vub300->vub_name, "corrupt offload pseudocode",
1226 sizeof(vub300->vub_name));
1227 return;
1228 }
1229 interrupt_size += *data++;
1230 size -= 1;
1231 interrupt_size <<= 8;
1232 interrupt_size += *data++;
1233 size -= 1;
1234 if (interrupt_size < size) {
1235 u16 xfer_length = roundup_to_multiple_of_64(interrupt_size);
1236 u8 *xfer_buffer = kmalloc(xfer_length, GFP_KERNEL);
1237 if (xfer_buffer) {
1238 int retval;
1239 memcpy(xfer_buffer, data, interrupt_size);
1240 memset(xfer_buffer + interrupt_size, 0,
1241 xfer_length - interrupt_size);
1242 size -= interrupt_size;
1243 data += interrupt_size;
1244 retval =
1245 usb_control_msg(vub300->udev,
1246 usb_sndctrlpipe(vub300->udev, 0),
1247 SET_INTERRUPT_PSEUDOCODE,
1248 USB_DIR_OUT | USB_TYPE_VENDOR |
1249 USB_RECIP_DEVICE, 0x0000, 0x0000,
1250 xfer_buffer, xfer_length, HZ);
1251 kfree(xfer_buffer);
1252 if (retval < 0) {
1253 strncpy(vub300->vub_name,
1254 "SDIO pseudocode download failed",
1255 sizeof(vub300->vub_name));
1256 return;
1257 }
1258 } else {
1259 dev_err(&vub300->udev->dev,
1260 "not enough memory for xfer buffer to send"
1261 " INTERRUPT_PSEUDOCODE for %s %s\n", fw->data,
1262 vub300->vub_name);
1263 strncpy(vub300->vub_name,
1264 "SDIO interrupt pseudocode download failed",
1265 sizeof(vub300->vub_name));
1266 return;
1267 }
1268 } else {
1269 dev_err(&vub300->udev->dev,
1270 "corrupt interrupt pseudocode in firmware %s %s\n",
1271 fw->data, vub300->vub_name);
1272 strncpy(vub300->vub_name, "corrupt interrupt pseudocode",
1273 sizeof(vub300->vub_name));
1274 return;
1275 }
1276 ts += *data++;
1277 size -= 1;
1278 ts <<= 8;
1279 ts += *data++;
1280 size -= 1;
1281 if (ts < size) {
1282 u16 xfer_length = roundup_to_multiple_of_64(ts);
1283 u8 *xfer_buffer = kmalloc(xfer_length, GFP_KERNEL);
1284 if (xfer_buffer) {
1285 int retval;
1286 memcpy(xfer_buffer, data, ts);
1287 memset(xfer_buffer + ts, 0,
1288 xfer_length - ts);
1289 size -= ts;
1290 data += ts;
1291 retval =
1292 usb_control_msg(vub300->udev,
1293 usb_sndctrlpipe(vub300->udev, 0),
1294 SET_TRANSFER_PSEUDOCODE,
1295 USB_DIR_OUT | USB_TYPE_VENDOR |
1296 USB_RECIP_DEVICE, 0x0000, 0x0000,
1297 xfer_buffer, xfer_length, HZ);
1298 kfree(xfer_buffer);
1299 if (retval < 0) {
1300 strncpy(vub300->vub_name,
1301 "SDIO pseudocode download failed",
1302 sizeof(vub300->vub_name));
1303 return;
1304 }
1305 } else {
1306 dev_err(&vub300->udev->dev,
1307 "not enough memory for xfer buffer to send"
1308 " TRANSFER_PSEUDOCODE for %s %s\n", fw->data,
1309 vub300->vub_name);
1310 strncpy(vub300->vub_name,
1311 "SDIO transfer pseudocode download failed",
1312 sizeof(vub300->vub_name));
1313 return;
1314 }
1315 } else {
1316 dev_err(&vub300->udev->dev,
1317 "corrupt transfer pseudocode in firmware %s %s\n",
1318 fw->data, vub300->vub_name);
1319 strncpy(vub300->vub_name, "corrupt transfer pseudocode",
1320 sizeof(vub300->vub_name));
1321 return;
1322 }
1323 register_count += *data++;
1324 size -= 1;
1325 if (register_count * 4 == size) {
1326 int I = vub300->dynamic_register_count = register_count;
1327 int i = 0;
1328 while (I--) {
1329 unsigned int func_num = 0;
1330 vub300->sdio_register[i].func_num = *data++;
1331 size -= 1;
1332 func_num += *data++;
1333 size -= 1;
1334 func_num <<= 8;
1335 func_num += *data++;
1336 size -= 1;
1337 func_num <<= 8;
1338 func_num += *data++;
1339 size -= 1;
1340 vub300->sdio_register[i].sdio_reg = func_num;
1341 vub300->sdio_register[i].activate = 1;
1342 vub300->sdio_register[i].prepared = 0;
1343 i += 1;
1344 }
1345 dev_info(&vub300->udev->dev,
1346 "initialized %d dynamic pseudocode registers\n",
1347 vub300->dynamic_register_count);
1348 return;
1349 } else {
1350 dev_err(&vub300->udev->dev,
1351 "corrupt dynamic registers in firmware %s\n",
1352 vub300->vub_name);
1353 strncpy(vub300->vub_name, "corrupt dynamic registers",
1354 sizeof(vub300->vub_name));
1355 return;
1356 }
1357}
1358
1359/*
1360 * if the binary containing the EMPTY PseudoCode can not be found
1361 * vub300->vub_name is set anyway in order to prevent an automatic retry
1362 */
1363static void download_offload_pseudocode(struct vub300_mmc_host *vub300)
1364{
1365 struct mmc_card *card = vub300->mmc->card;
1366 int sdio_funcs = card->sdio_funcs;
1367 const struct firmware *fw = NULL;
1368 int l = snprintf(vub300->vub_name, sizeof(vub300->vub_name),
1369 "vub_%04X%04X", card->cis.vendor, card->cis.device);
1370 int n = 0;
1371 int retval;
1372 for (n = 0; n < sdio_funcs; n++) {
1373 struct sdio_func *sf = card->sdio_func[n];
1374 l += snprintf(vub300->vub_name + l,
1375 sizeof(vub300->vub_name) - l, "_%04X%04X",
1376 sf->vendor, sf->device);
1377 };
1378 snprintf(vub300->vub_name + l, sizeof(vub300->vub_name) - l, ".bin");
1379 dev_info(&vub300->udev->dev, "requesting offload firmware %s\n",
1380 vub300->vub_name);
1381 retval = request_firmware(&fw, vub300->vub_name, &card->dev);
1382 if (retval < 0) {
1383 strncpy(vub300->vub_name, "vub_default.bin",
1384 sizeof(vub300->vub_name));
1385 retval = request_firmware(&fw, vub300->vub_name, &card->dev);
1386 if (retval < 0) {
1387 strncpy(vub300->vub_name,
1388 "no SDIO offload firmware found",
1389 sizeof(vub300->vub_name));
1390 } else {
1391 __download_offload_pseudocode(vub300, fw);
1392 release_firmware(fw);
1393 }
1394 } else {
1395 __download_offload_pseudocode(vub300, fw);
1396 release_firmware(fw);
1397 }
1398}
1399
1400static void vub300_usb_bulk_msg_completion(struct urb *urb)
1401{ /* urb completion handler - hardirq */
1402 complete((struct completion *)urb->context);
1403}
1404
1405static int vub300_usb_bulk_msg(struct vub300_mmc_host *vub300,
1406 unsigned int pipe, void *data, int len,
1407 int *actual_length, int timeout_msecs)
1408{
1409 /* cmd_mutex is held by vub300_cmndwork_thread */
1410 struct usb_device *usb_dev = vub300->udev;
1411 struct completion done;
1412 int retval;
1413 vub300->urb = usb_alloc_urb(0, GFP_KERNEL);
1414 if (!vub300->urb)
1415 return -ENOMEM;
1416 usb_fill_bulk_urb(vub300->urb, usb_dev, pipe, data, len,
1417 vub300_usb_bulk_msg_completion, NULL);
1418 init_completion(&done);
1419 vub300->urb->context = &done;
1420 vub300->urb->actual_length = 0;
1421 retval = usb_submit_urb(vub300->urb, GFP_KERNEL);
1422 if (unlikely(retval))
1423 goto out;
1424 if (!wait_for_completion_timeout
1425 (&done, msecs_to_jiffies(timeout_msecs))) {
1426 retval = -ETIMEDOUT;
1427 usb_kill_urb(vub300->urb);
1428 } else {
1429 retval = vub300->urb->status;
1430 }
1431out:
1432 *actual_length = vub300->urb->actual_length;
1433 usb_free_urb(vub300->urb);
1434 vub300->urb = NULL;
1435 return retval;
1436}
1437
1438static int __command_read_data(struct vub300_mmc_host *vub300,
1439 struct mmc_command *cmd, struct mmc_data *data)
1440{
1441 /* cmd_mutex is held by vub300_cmndwork_thread */
1442 int linear_length = vub300->datasize;
1443 int padded_length = vub300->large_usb_packets ?
1444 ((511 + linear_length) >> 9) << 9 :
1445 ((63 + linear_length) >> 6) << 6;
1446 if ((padded_length == linear_length) || !pad_input_to_usb_pkt) {
1447 int result;
1448 unsigned pipe;
1449 pipe = usb_rcvbulkpipe(vub300->udev, vub300->data_inp_ep);
1450 result = usb_sg_init(&vub300->sg_request, vub300->udev,
1451 pipe, 0, data->sg,
1452 data->sg_len, 0, GFP_KERNEL);
1453 if (result < 0) {
1454 usb_unlink_urb(vub300->command_out_urb);
1455 usb_unlink_urb(vub300->command_res_urb);
1456 cmd->error = result;
1457 data->bytes_xfered = 0;
1458 return 0;
1459 } else {
1460 vub300->sg_transfer_timer.expires =
1461 jiffies + msecs_to_jiffies(2000 +
1462 (linear_length / 16384));
1463 add_timer(&vub300->sg_transfer_timer);
1464 usb_sg_wait(&vub300->sg_request);
1465 del_timer(&vub300->sg_transfer_timer);
1466 if (vub300->sg_request.status < 0) {
1467 cmd->error = vub300->sg_request.status;
1468 data->bytes_xfered = 0;
1469 return 0;
1470 } else {
1471 data->bytes_xfered = vub300->datasize;
1472 return linear_length;
1473 }
1474 }
1475 } else {
1476 u8 *buf = kmalloc(padded_length, GFP_KERNEL);
1477 if (buf) {
1478 int result;
1479 unsigned pipe = usb_rcvbulkpipe(vub300->udev,
1480 vub300->data_inp_ep);
1481 int actual_length = 0;
1482 result = vub300_usb_bulk_msg(vub300, pipe, buf,
1483 padded_length, &actual_length,
1484 2000 + (padded_length / 16384));
1485 if (result < 0) {
1486 cmd->error = result;
1487 data->bytes_xfered = 0;
1488 kfree(buf);
1489 return 0;
1490 } else if (actual_length < linear_length) {
1491 cmd->error = -EREMOTEIO;
1492 data->bytes_xfered = 0;
1493 kfree(buf);
1494 return 0;
1495 } else {
1496 sg_copy_from_buffer(data->sg, data->sg_len, buf,
1497 linear_length);
1498 kfree(buf);
1499 data->bytes_xfered = vub300->datasize;
1500 return linear_length;
1501 }
1502 } else {
1503 cmd->error = -ENOMEM;
1504 data->bytes_xfered = 0;
1505 return 0;
1506 }
1507 }
1508}
1509
1510static int __command_write_data(struct vub300_mmc_host *vub300,
1511 struct mmc_command *cmd, struct mmc_data *data)
1512{
1513 /* cmd_mutex is held by vub300_cmndwork_thread */
1514 unsigned pipe = usb_sndbulkpipe(vub300->udev, vub300->data_out_ep);
1515 int linear_length = vub300->datasize;
1516 int modulo_64_length = linear_length & 0x003F;
1517 int modulo_512_length = linear_length & 0x01FF;
1518 if (linear_length < 64) {
1519 int result;
1520 int actual_length;
1521 sg_copy_to_buffer(data->sg, data->sg_len,
1522 vub300->padded_buffer,
1523 sizeof(vub300->padded_buffer));
1524 memset(vub300->padded_buffer + linear_length, 0,
1525 sizeof(vub300->padded_buffer) - linear_length);
1526 result = vub300_usb_bulk_msg(vub300, pipe, vub300->padded_buffer,
1527 sizeof(vub300->padded_buffer),
1528 &actual_length, 2000 +
1529 (sizeof(vub300->padded_buffer) /
1530 16384));
1531 if (result < 0) {
1532 cmd->error = result;
1533 data->bytes_xfered = 0;
1534 } else {
1535 data->bytes_xfered = vub300->datasize;
1536 }
1537 } else if ((!vub300->large_usb_packets && (0 < modulo_64_length)) ||
1538 (vub300->large_usb_packets && (64 > modulo_512_length))
1539 ) { /* don't you just love these work-rounds */
1540 int padded_length = ((63 + linear_length) >> 6) << 6;
1541 u8 *buf = kmalloc(padded_length, GFP_KERNEL);
1542 if (buf) {
1543 int result;
1544 int actual_length;
1545 sg_copy_to_buffer(data->sg, data->sg_len, buf,
1546 padded_length);
1547 memset(buf + linear_length, 0,
1548 padded_length - linear_length);
1549 result =
1550 vub300_usb_bulk_msg(vub300, pipe, buf,
1551 padded_length, &actual_length,
1552 2000 + padded_length / 16384);
1553 kfree(buf);
1554 if (result < 0) {
1555 cmd->error = result;
1556 data->bytes_xfered = 0;
1557 } else {
1558 data->bytes_xfered = vub300->datasize;
1559 }
1560 } else {
1561 cmd->error = -ENOMEM;
1562 data->bytes_xfered = 0;
1563 }
1564 } else { /* no data padding required */
1565 int result;
1566 unsigned char buf[64 * 4];
1567 sg_copy_to_buffer(data->sg, data->sg_len, buf, sizeof(buf));
1568 result = usb_sg_init(&vub300->sg_request, vub300->udev,
1569 pipe, 0, data->sg,
1570 data->sg_len, 0, GFP_KERNEL);
1571 if (result < 0) {
1572 usb_unlink_urb(vub300->command_out_urb);
1573 usb_unlink_urb(vub300->command_res_urb);
1574 cmd->error = result;
1575 data->bytes_xfered = 0;
1576 } else {
1577 vub300->sg_transfer_timer.expires =
1578 jiffies + msecs_to_jiffies(2000 +
1579 linear_length / 16384);
1580 add_timer(&vub300->sg_transfer_timer);
1581 usb_sg_wait(&vub300->sg_request);
1582 if (cmd->error) {
1583 data->bytes_xfered = 0;
1584 } else {
1585 del_timer(&vub300->sg_transfer_timer);
1586 if (vub300->sg_request.status < 0) {
1587 cmd->error = vub300->sg_request.status;
1588 data->bytes_xfered = 0;
1589 } else {
1590 data->bytes_xfered = vub300->datasize;
1591 }
1592 }
1593 }
1594 }
1595 return linear_length;
1596}
1597
1598static void __vub300_command_response(struct vub300_mmc_host *vub300,
1599 struct mmc_command *cmd,
1600 struct mmc_data *data, int data_length)
1601{
1602 /* cmd_mutex is held by vub300_cmndwork_thread */
1603 long respretval;
1604 int msec_timeout = 1000 + data_length / 4;
1605 respretval =
1606 wait_for_completion_timeout(&vub300->command_complete,
1607 msecs_to_jiffies(msec_timeout));
1608 if (respretval == 0) { /* TIMED OUT */
1609 /* we don't know which of "out" and "res" if any failed */
1610 int result;
1611 vub300->usb_timed_out = 1;
1612 usb_kill_urb(vub300->command_out_urb);
1613 usb_kill_urb(vub300->command_res_urb);
1614 cmd->error = -ETIMEDOUT;
1615 result = usb_lock_device_for_reset(vub300->udev,
1616 vub300->interface);
1617 if (result == 0) {
1618 result = usb_reset_device(vub300->udev);
1619 usb_unlock_device(vub300->udev);
1620 }
1621 } else if (respretval < 0) {
1622 /* we don't know which of "out" and "res" if any failed */
1623 usb_kill_urb(vub300->command_out_urb);
1624 usb_kill_urb(vub300->command_res_urb);
1625 cmd->error = respretval;
1626 } else if (cmd->error) {
1627 /*
1628 * the error occured sending the command
1629 * or recieving the response
1630 */
1631 } else if (vub300->command_out_urb->status) {
1632 vub300->usb_transport_fail = vub300->command_out_urb->status;
1633 cmd->error = -EPROTO == vub300->command_out_urb->status ?
1634 -ESHUTDOWN : vub300->command_out_urb->status;
1635 } else if (vub300->command_res_urb->status) {
1636 vub300->usb_transport_fail = vub300->command_res_urb->status;
1637 cmd->error = -EPROTO == vub300->command_res_urb->status ?
1638 -ESHUTDOWN : vub300->command_res_urb->status;
1639 } else if (vub300->resp.common.header_type == 0x00) {
1640 /*
1641 * the command completed successfully
1642 * and there was no piggybacked data
1643 */
1644 } else if (vub300->resp.common.header_type == RESPONSE_ERROR) {
1645 cmd->error =
1646 vub300_response_error(vub300->resp.error.error_code);
1647 if (vub300->data)
1648 usb_sg_cancel(&vub300->sg_request);
1649 } else if (vub300->resp.common.header_type == RESPONSE_PIGGYBACKED) {
1650 int offloaded_data_length =
1651 vub300->resp.common.header_size -
1652 sizeof(struct sd_register_header);
1653 int register_count = offloaded_data_length >> 3;
1654 int ri = 0;
1655 while (register_count--) {
1656 add_offloaded_reg(vub300, &vub300->resp.pig.reg[ri]);
1657 ri += 1;
1658 }
1659 vub300->resp.common.header_size =
1660 sizeof(struct sd_register_header);
1661 vub300->resp.common.header_type = 0x00;
1662 cmd->error = 0;
1663 } else if (vub300->resp.common.header_type == RESPONSE_PIG_DISABLED) {
1664 int offloaded_data_length =
1665 vub300->resp.common.header_size -
1666 sizeof(struct sd_register_header);
1667 int register_count = offloaded_data_length >> 3;
1668 int ri = 0;
1669 while (register_count--) {
1670 add_offloaded_reg(vub300, &vub300->resp.pig.reg[ri]);
1671 ri += 1;
1672 }
1673 mutex_lock(&vub300->irq_mutex);
1674 if (vub300->irqs_queued) {
1675 vub300->irqs_queued += 1;
1676 } else if (vub300->irq_enabled) {
1677 vub300->irqs_queued += 1;
1678 vub300_queue_poll_work(vub300, 0);
1679 } else {
1680 vub300->irqs_queued += 1;
1681 }
1682 vub300->irq_disabled = 1;
1683 mutex_unlock(&vub300->irq_mutex);
1684 vub300->resp.common.header_size =
1685 sizeof(struct sd_register_header);
1686 vub300->resp.common.header_type = 0x00;
1687 cmd->error = 0;
1688 } else if (vub300->resp.common.header_type == RESPONSE_PIG_ENABLED) {
1689 int offloaded_data_length =
1690 vub300->resp.common.header_size -
1691 sizeof(struct sd_register_header);
1692 int register_count = offloaded_data_length >> 3;
1693 int ri = 0;
1694 while (register_count--) {
1695 add_offloaded_reg(vub300, &vub300->resp.pig.reg[ri]);
1696 ri += 1;
1697 }
1698 mutex_lock(&vub300->irq_mutex);
1699 if (vub300->irqs_queued) {
1700 vub300->irqs_queued += 1;
1701 } else if (vub300->irq_enabled) {
1702 vub300->irqs_queued += 1;
1703 vub300_queue_poll_work(vub300, 0);
1704 } else {
1705 vub300->irqs_queued += 1;
1706 }
1707 vub300->irq_disabled = 0;
1708 mutex_unlock(&vub300->irq_mutex);
1709 vub300->resp.common.header_size =
1710 sizeof(struct sd_register_header);
1711 vub300->resp.common.header_type = 0x00;
1712 cmd->error = 0;
1713 } else {
1714 cmd->error = -EINVAL;
1715 }
1716}
1717
1718static void construct_request_response(struct vub300_mmc_host *vub300,
1719 struct mmc_command *cmd)
1720{
1721 int resp_len = vub300->resp_len;
1722 int less_cmd = (17 == resp_len) ? resp_len : resp_len - 1;
1723 int bytes = 3 & less_cmd;
1724 int words = less_cmd >> 2;
1725 u8 *r = vub300->resp.response.command_response;
1726 if (bytes == 3) {
1727 cmd->resp[words] = (r[1 + (words << 2)] << 24)
1728 | (r[2 + (words << 2)] << 16)
1729 | (r[3 + (words << 2)] << 8);
1730 } else if (bytes == 2) {
1731 cmd->resp[words] = (r[1 + (words << 2)] << 24)
1732 | (r[2 + (words << 2)] << 16);
1733 } else if (bytes == 1) {
1734 cmd->resp[words] = (r[1 + (words << 2)] << 24);
1735 }
1736 while (words-- > 0) {
1737 cmd->resp[words] = (r[1 + (words << 2)] << 24)
1738 | (r[2 + (words << 2)] << 16)
1739 | (r[3 + (words << 2)] << 8)
1740 | (r[4 + (words << 2)] << 0);
1741 }
1742 if ((cmd->opcode == 53) && (0x000000FF & cmd->resp[0]))
1743 cmd->resp[0] &= 0xFFFFFF00;
1744}
1745
1746/* this thread runs only when there is an upper level command req outstanding */
1747static void vub300_cmndwork_thread(struct work_struct *work)
1748{
1749 struct vub300_mmc_host *vub300 =
1750 container_of(work, struct vub300_mmc_host, cmndwork);
1751 if (!vub300->interface) {
1752 kref_put(&vub300->kref, vub300_delete);
1753 return;
1754 } else {
1755 struct mmc_request *req = vub300->req;
1756 struct mmc_command *cmd = vub300->cmd;
1757 struct mmc_data *data = vub300->data;
1758 int data_length;
1759 mutex_lock(&vub300->cmd_mutex);
1760 init_completion(&vub300->command_complete);
1761 if (likely(vub300->vub_name[0]) || !vub300->mmc->card ||
1762 !mmc_card_present(vub300->mmc->card)) {
1763 /*
1764 * the name of the EMPTY Pseudo firmware file
1765 * is used as a flag to indicate that the file
1766 * has been already downloaded to the VUB300 chip
1767 */
1768 } else if (0 == vub300->mmc->card->sdio_funcs) {
1769 strncpy(vub300->vub_name, "SD memory device",
1770 sizeof(vub300->vub_name));
1771 } else {
1772 download_offload_pseudocode(vub300);
1773 }
1774 send_command(vub300);
1775 if (!data)
1776 data_length = 0;
1777 else if (MMC_DATA_READ & data->flags)
1778 data_length = __command_read_data(vub300, cmd, data);
1779 else
1780 data_length = __command_write_data(vub300, cmd, data);
1781 __vub300_command_response(vub300, cmd, data, data_length);
1782 vub300->req = NULL;
1783 vub300->cmd = NULL;
1784 vub300->data = NULL;
1785 if (cmd->error) {
1786 if (cmd->error == -ENOMEDIUM)
1787 check_vub300_port_status(vub300);
1788 mutex_unlock(&vub300->cmd_mutex);
1789 mmc_request_done(vub300->mmc, req);
1790 kref_put(&vub300->kref, vub300_delete);
1791 return;
1792 } else {
1793 construct_request_response(vub300, cmd);
1794 vub300->resp_len = 0;
1795 mutex_unlock(&vub300->cmd_mutex);
1796 kref_put(&vub300->kref, vub300_delete);
1797 mmc_request_done(vub300->mmc, req);
1798 return;
1799 }
1800 }
1801}
1802
1803static int examine_cyclic_buffer(struct vub300_mmc_host *vub300,
1804 struct mmc_command *cmd, u8 Function)
1805{
1806 /* cmd_mutex is held by vub300_mmc_request */
1807 u8 cmd0 = 0xFF & (cmd->arg >> 24);
1808 u8 cmd1 = 0xFF & (cmd->arg >> 16);
1809 u8 cmd2 = 0xFF & (cmd->arg >> 8);
1810 u8 cmd3 = 0xFF & (cmd->arg >> 0);
1811 int first = MAXREGMASK & vub300->fn[Function].offload_point;
1812 struct offload_registers_access *rf = &vub300->fn[Function].reg[first];
1813 if (cmd0 == rf->command_byte[0] &&
1814 cmd1 == rf->command_byte[1] &&
1815 cmd2 == rf->command_byte[2] &&
1816 cmd3 == rf->command_byte[3]) {
1817 u8 checksum = 0x00;
1818 cmd->resp[1] = checksum << 24;
1819 cmd->resp[0] = (rf->Respond_Byte[0] << 24)
1820 | (rf->Respond_Byte[1] << 16)
1821 | (rf->Respond_Byte[2] << 8)
1822 | (rf->Respond_Byte[3] << 0);
1823 vub300->fn[Function].offload_point += 1;
1824 vub300->fn[Function].offload_count -= 1;
1825 vub300->total_offload_count -= 1;
1826 return 1;
1827 } else {
1828 int delta = 1; /* because it does not match the first one */
1829 u8 register_count = vub300->fn[Function].offload_count - 1;
1830 u32 register_point = vub300->fn[Function].offload_point + 1;
1831 while (0 < register_count) {
1832 int point = MAXREGMASK & register_point;
1833 struct offload_registers_access *r =
1834 &vub300->fn[Function].reg[point];
1835 if (cmd0 == r->command_byte[0] &&
1836 cmd1 == r->command_byte[1] &&
1837 cmd2 == r->command_byte[2] &&
1838 cmd3 == r->command_byte[3]) {
1839 u8 checksum = 0x00;
1840 cmd->resp[1] = checksum << 24;
1841 cmd->resp[0] = (r->Respond_Byte[0] << 24)
1842 | (r->Respond_Byte[1] << 16)
1843 | (r->Respond_Byte[2] << 8)
1844 | (r->Respond_Byte[3] << 0);
1845 vub300->fn[Function].offload_point += delta;
1846 vub300->fn[Function].offload_count -= delta;
1847 vub300->total_offload_count -= delta;
1848 return 1;
1849 } else {
1850 register_point += 1;
1851 register_count -= 1;
1852 delta += 1;
1853 continue;
1854 }
1855 }
1856 return 0;
1857 }
1858}
1859
1860static int satisfy_request_from_offloaded_data(struct vub300_mmc_host *vub300,
1861 struct mmc_command *cmd)
1862{
1863 /* cmd_mutex is held by vub300_mmc_request */
1864 u8 regs = vub300->dynamic_register_count;
1865 u8 i = 0;
1866 u8 func = FUN(cmd);
1867 u32 reg = REG(cmd);
1868 while (0 < regs--) {
1869 if ((vub300->sdio_register[i].func_num == func) &&
1870 (vub300->sdio_register[i].sdio_reg == reg)) {
1871 if (!vub300->sdio_register[i].prepared) {
1872 return 0;
1873 } else if ((0x80000000 & cmd->arg) == 0x80000000) {
1874 /*
1875 * a write to a dynamic register
1876 * nullifies our offloaded value
1877 */
1878 vub300->sdio_register[i].prepared = 0;
1879 return 0;
1880 } else {
1881 u8 checksum = 0x00;
1882 u8 rsp0 = 0x00;
1883 u8 rsp1 = 0x00;
1884 u8 rsp2 = vub300->sdio_register[i].response;
1885 u8 rsp3 = vub300->sdio_register[i].regvalue;
1886 vub300->sdio_register[i].prepared = 0;
1887 cmd->resp[1] = checksum << 24;
1888 cmd->resp[0] = (rsp0 << 24)
1889 | (rsp1 << 16)
1890 | (rsp2 << 8)
1891 | (rsp3 << 0);
1892 return 1;
1893 }
1894 } else {
1895 i += 1;
1896 continue;
1897 }
1898 };
1899 if (vub300->total_offload_count == 0)
1900 return 0;
1901 else if (vub300->fn[func].offload_count == 0)
1902 return 0;
1903 else
1904 return examine_cyclic_buffer(vub300, cmd, func);
1905}
1906
1907static void vub300_mmc_request(struct mmc_host *mmc, struct mmc_request *req)
1908{ /* NOT irq */
1909 struct mmc_command *cmd = req->cmd;
1910 struct vub300_mmc_host *vub300 = mmc_priv(mmc);
1911 if (!vub300->interface) {
1912 cmd->error = -ESHUTDOWN;
1913 mmc_request_done(mmc, req);
1914 return;
1915 } else {
1916 struct mmc_data *data = req->data;
1917 if (!vub300->card_powered) {
1918 cmd->error = -ENOMEDIUM;
1919 mmc_request_done(mmc, req);
1920 return;
1921 }
1922 if (!vub300->card_present) {
1923 cmd->error = -ENOMEDIUM;
1924 mmc_request_done(mmc, req);
1925 return;
1926 }
1927 if (vub300->usb_transport_fail) {
1928 cmd->error = vub300->usb_transport_fail;
1929 mmc_request_done(mmc, req);
1930 return;
1931 }
1932 if (!vub300->interface) {
1933 cmd->error = -ENODEV;
1934 mmc_request_done(mmc, req);
1935 return;
1936 }
1937 kref_get(&vub300->kref);
1938 mutex_lock(&vub300->cmd_mutex);
1939 mod_timer(&vub300->inactivity_timer, jiffies + HZ);
1940 /*
1941 * for performance we have to return immediately
1942 * if the requested data has been offloaded
1943 */
1944 if (cmd->opcode == 52 &&
1945 satisfy_request_from_offloaded_data(vub300, cmd)) {
1946 cmd->error = 0;
1947 mutex_unlock(&vub300->cmd_mutex);
1948 kref_put(&vub300->kref, vub300_delete);
1949 mmc_request_done(mmc, req);
1950 return;
1951 } else {
1952 vub300->cmd = cmd;
1953 vub300->req = req;
1954 vub300->data = data;
1955 if (data)
1956 vub300->datasize = data->blksz * data->blocks;
1957 else
1958 vub300->datasize = 0;
1959 vub300_queue_cmnd_work(vub300);
1960 mutex_unlock(&vub300->cmd_mutex);
1961 kref_put(&vub300->kref, vub300_delete);
1962 /*
1963 * the kernel lock diagnostics complain
1964 * if the cmd_mutex * is "passed on"
1965 * to the cmndwork thread,
1966 * so we must release it now
1967 * and re-acquire it in the cmndwork thread
1968 */
1969 }
1970 }
1971}
1972
1973static void __set_clock_speed(struct vub300_mmc_host *vub300, u8 buf[8],
1974 struct mmc_ios *ios)
1975{
1976 int buf_array_size = 8; /* ARRAY_SIZE(buf) does not work !!! */
1977 int retval;
1978 u32 kHzClock;
1979 if (ios->clock >= 48000000)
1980 kHzClock = 48000;
1981 else if (ios->clock >= 24000000)
1982 kHzClock = 24000;
1983 else if (ios->clock >= 20000000)
1984 kHzClock = 20000;
1985 else if (ios->clock >= 15000000)
1986 kHzClock = 15000;
1987 else if (ios->clock >= 200000)
1988 kHzClock = 200;
1989 else
1990 kHzClock = 0;
1991 {
1992 int i;
1993 u64 c = kHzClock;
1994 for (i = 0; i < buf_array_size; i++) {
1995 buf[i] = c;
1996 c >>= 8;
1997 }
1998 }
1999 retval =
2000 usb_control_msg(vub300->udev, usb_sndctrlpipe(vub300->udev, 0),
2001 SET_CLOCK_SPEED,
2002 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
2003 0x00, 0x00, buf, buf_array_size, HZ);
2004 if (retval != 8) {
2005 dev_err(&vub300->udev->dev, "SET_CLOCK_SPEED"
2006 " %dkHz failed with retval=%d\n", kHzClock, retval);
2007 } else {
2008 dev_dbg(&vub300->udev->dev, "SET_CLOCK_SPEED"
2009 " %dkHz\n", kHzClock);
2010 }
2011}
2012
2013static void vub300_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
2014{ /* NOT irq */
2015 struct vub300_mmc_host *vub300 = mmc_priv(mmc);
2016 if (!vub300->interface)
2017 return;
2018 kref_get(&vub300->kref);
2019 mutex_lock(&vub300->cmd_mutex);
2020 if ((ios->power_mode == MMC_POWER_OFF) && vub300->card_powered) {
2021 vub300->card_powered = 0;
2022 usb_control_msg(vub300->udev, usb_sndctrlpipe(vub300->udev, 0),
2023 SET_SD_POWER,
2024 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
2025 0x0000, 0x0000, NULL, 0, HZ);
2026 /* must wait for the VUB300 u-proc to boot up */
2027 msleep(600);
2028 } else if ((ios->power_mode == MMC_POWER_UP) && !vub300->card_powered) {
2029 usb_control_msg(vub300->udev, usb_sndctrlpipe(vub300->udev, 0),
2030 SET_SD_POWER,
2031 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
2032 0x0001, 0x0000, NULL, 0, HZ);
2033 msleep(600);
2034 vub300->card_powered = 1;
2035 } else if (ios->power_mode == MMC_POWER_ON) {
2036 u8 *buf = kmalloc(8, GFP_KERNEL);
2037 if (buf) {
2038 __set_clock_speed(vub300, buf, ios);
2039 kfree(buf);
2040 }
2041 } else {
2042 /* this should mean no change of state */
2043 }
2044 mutex_unlock(&vub300->cmd_mutex);
2045 kref_put(&vub300->kref, vub300_delete);
2046}
2047
2048static int vub300_mmc_get_ro(struct mmc_host *mmc)
2049{
2050 struct vub300_mmc_host *vub300 = mmc_priv(mmc);
2051 return vub300->read_only;
2052}
2053
2054static void vub300_enable_sdio_irq(struct mmc_host *mmc, int enable)
2055{ /* NOT irq */
2056 struct vub300_mmc_host *vub300 = mmc_priv(mmc);
2057 if (!vub300->interface)
2058 return;
2059 kref_get(&vub300->kref);
2060 if (enable) {
2061 mutex_lock(&vub300->irq_mutex);
2062 if (vub300->irqs_queued) {
2063 vub300->irqs_queued -= 1;
2064 mmc_signal_sdio_irq(vub300->mmc);
2065 } else if (vub300->irq_disabled) {
2066 vub300->irq_disabled = 0;
2067 vub300->irq_enabled = 1;
2068 vub300_queue_poll_work(vub300, 0);
2069 } else if (vub300->irq_enabled) {
2070 /* this should not happen, so we will just ignore it */
2071 } else {
2072 vub300->irq_enabled = 1;
2073 vub300_queue_poll_work(vub300, 0);
2074 }
2075 mutex_unlock(&vub300->irq_mutex);
2076 } else {
2077 vub300->irq_enabled = 0;
2078 }
2079 kref_put(&vub300->kref, vub300_delete);
2080}
2081
2082void vub300_init_card(struct mmc_host *mmc, struct mmc_card *card)
2083{ /* NOT irq */
2084 struct vub300_mmc_host *vub300 = mmc_priv(mmc);
2085 dev_info(&vub300->udev->dev, "NO host QUIRKS for this card\n");
2086}
2087
2088static struct mmc_host_ops vub300_mmc_ops = {
2089 .request = vub300_mmc_request,
2090 .set_ios = vub300_mmc_set_ios,
2091 .get_ro = vub300_mmc_get_ro,
2092 .enable_sdio_irq = vub300_enable_sdio_irq,
2093 .init_card = vub300_init_card,
2094};
2095
2096static int vub300_probe(struct usb_interface *interface,
2097 const struct usb_device_id *id)
2098{ /* NOT irq */
2099 struct vub300_mmc_host *vub300 = NULL;
2100 struct usb_host_interface *iface_desc;
2101 struct usb_device *udev = usb_get_dev(interface_to_usbdev(interface));
2102 int i;
2103 int retval = -ENOMEM;
2104 struct urb *command_out_urb;
2105 struct urb *command_res_urb;
2106 struct mmc_host *mmc;
2107 char manufacturer[48];
2108 char product[32];
2109 char serial_number[32];
2110 usb_string(udev, udev->descriptor.iManufacturer, manufacturer,
2111 sizeof(manufacturer));
2112 usb_string(udev, udev->descriptor.iProduct, product, sizeof(product));
2113 usb_string(udev, udev->descriptor.iSerialNumber, serial_number,
2114 sizeof(serial_number));
2115 dev_info(&udev->dev, "probing VID:PID(%04X:%04X) %s %s %s\n",
2116 udev->descriptor.idVendor, udev->descriptor.idProduct,
2117 manufacturer, product, serial_number);
2118 command_out_urb = usb_alloc_urb(0, GFP_KERNEL);
2119 if (!command_out_urb) {
2120 retval = -ENOMEM;
2121 dev_err(&vub300->udev->dev,
2122 "not enough memory for the command_out_urb\n");
2123 goto error0;
2124 }
2125 command_res_urb = usb_alloc_urb(0, GFP_KERNEL);
2126 if (!command_res_urb) {
2127 retval = -ENOMEM;
2128 dev_err(&vub300->udev->dev,
2129 "not enough memory for the command_res_urb\n");
2130 goto error1;
2131 }
2132 /* this also allocates memory for our VUB300 mmc host device */
2133 mmc = mmc_alloc_host(sizeof(struct vub300_mmc_host), &udev->dev);
2134 if (!mmc) {
2135 retval = -ENOMEM;
2136 dev_err(&vub300->udev->dev,
2137 "not enough memory for the mmc_host\n");
2138 goto error4;
2139 }
2140 /* MMC core transfer sizes tunable parameters */
2141 mmc->caps = 0;
2142 if (!force_1_bit_data_xfers)
2143 mmc->caps |= MMC_CAP_4_BIT_DATA;
2144 if (!force_polling_for_irqs)
2145 mmc->caps |= MMC_CAP_SDIO_IRQ;
2146 mmc->caps &= ~MMC_CAP_NEEDS_POLL;
2147 /*
2148 * MMC_CAP_NEEDS_POLL causes core.c:mmc_rescan() to poll
2149 * for devices which results in spurious CMD7's being
2150 * issued which stops some SDIO cards from working
2151 */
2152 if (limit_speed_to_24_MHz) {
2153 mmc->caps |= MMC_CAP_MMC_HIGHSPEED;
2154 mmc->caps |= MMC_CAP_SD_HIGHSPEED;
2155 mmc->f_max = 24000000;
2156 dev_info(&udev->dev, "limiting SDIO speed to 24_MHz\n");
2157 } else {
2158 mmc->caps |= MMC_CAP_MMC_HIGHSPEED;
2159 mmc->caps |= MMC_CAP_SD_HIGHSPEED;
2160 mmc->f_max = 48000000;
2161 }
2162 mmc->f_min = 200000;
2163 mmc->max_blk_count = 511;
2164 mmc->max_blk_size = 512;
2165 mmc->max_segs = 128;
2166 if (force_max_req_size)
2167 mmc->max_req_size = force_max_req_size * 1024;
2168 else
2169 mmc->max_req_size = 64 * 1024;
2170 mmc->max_seg_size = mmc->max_req_size;
2171 mmc->ocr_avail = 0;
2172 mmc->ocr_avail |= MMC_VDD_165_195;
2173 mmc->ocr_avail |= MMC_VDD_20_21;
2174 mmc->ocr_avail |= MMC_VDD_21_22;
2175 mmc->ocr_avail |= MMC_VDD_22_23;
2176 mmc->ocr_avail |= MMC_VDD_23_24;
2177 mmc->ocr_avail |= MMC_VDD_24_25;
2178 mmc->ocr_avail |= MMC_VDD_25_26;
2179 mmc->ocr_avail |= MMC_VDD_26_27;
2180 mmc->ocr_avail |= MMC_VDD_27_28;
2181 mmc->ocr_avail |= MMC_VDD_28_29;
2182 mmc->ocr_avail |= MMC_VDD_29_30;
2183 mmc->ocr_avail |= MMC_VDD_30_31;
2184 mmc->ocr_avail |= MMC_VDD_31_32;
2185 mmc->ocr_avail |= MMC_VDD_32_33;
2186 mmc->ocr_avail |= MMC_VDD_33_34;
2187 mmc->ocr_avail |= MMC_VDD_34_35;
2188 mmc->ocr_avail |= MMC_VDD_35_36;
2189 mmc->ops = &vub300_mmc_ops;
2190 vub300 = mmc_priv(mmc);
2191 vub300->mmc = mmc;
2192 vub300->card_powered = 0;
2193 vub300->bus_width = 0;
2194 vub300->cmnd.head.block_size[0] = 0x00;
2195 vub300->cmnd.head.block_size[1] = 0x00;
2196 vub300->app_spec = 0;
2197 mutex_init(&vub300->cmd_mutex);
2198 mutex_init(&vub300->irq_mutex);
2199 vub300->command_out_urb = command_out_urb;
2200 vub300->command_res_urb = command_res_urb;
2201 vub300->usb_timed_out = 0;
2202 vub300->dynamic_register_count = 0;
2203
2204 for (i = 0; i < ARRAY_SIZE(vub300->fn); i++) {
2205 vub300->fn[i].offload_point = 0;
2206 vub300->fn[i].offload_count = 0;
2207 }
2208
2209 vub300->total_offload_count = 0;
2210 vub300->irq_enabled = 0;
2211 vub300->irq_disabled = 0;
2212 vub300->irqs_queued = 0;
2213
2214 for (i = 0; i < ARRAY_SIZE(vub300->sdio_register); i++)
2215 vub300->sdio_register[i++].activate = 0;
2216
2217 vub300->udev = udev;
2218 vub300->interface = interface;
2219 vub300->cmnd_res_ep = 0;
2220 vub300->cmnd_out_ep = 0;
2221 vub300->data_inp_ep = 0;
2222 vub300->data_out_ep = 0;
2223
2224 for (i = 0; i < ARRAY_SIZE(vub300->fbs); i++)
2225 vub300->fbs[i] = 512;
2226
2227 /*
2228 * set up the endpoint information
2229 *
2230 * use the first pair of bulk-in and bulk-out
2231 * endpoints for Command/Response+Interrupt
2232 *
2233 * use the second pair of bulk-in and bulk-out
2234 * endpoints for Data In/Out
2235 */
2236 vub300->large_usb_packets = 0;
2237 iface_desc = interface->cur_altsetting;
2238 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
2239 struct usb_endpoint_descriptor *endpoint =
2240 &iface_desc->endpoint[i].desc;
2241 dev_info(&vub300->udev->dev,
2242 "vub300 testing %s EndPoint(%d) %02X\n",
2243 usb_endpoint_is_bulk_in(endpoint) ? "BULK IN" :
2244 usb_endpoint_is_bulk_out(endpoint) ? "BULK OUT" :
2245 "UNKNOWN", i, endpoint->bEndpointAddress);
2246 if (endpoint->wMaxPacketSize > 64)
2247 vub300->large_usb_packets = 1;
2248 if (usb_endpoint_is_bulk_in(endpoint)) {
2249 if (!vub300->cmnd_res_ep) {
2250 vub300->cmnd_res_ep =
2251 endpoint->bEndpointAddress;
2252 } else if (!vub300->data_inp_ep) {
2253 vub300->data_inp_ep =
2254 endpoint->bEndpointAddress;
2255 } else {
2256 dev_warn(&vub300->udev->dev,
2257 "ignoring"
2258 " unexpected bulk_in endpoint");
2259 }
2260 } else if (usb_endpoint_is_bulk_out(endpoint)) {
2261 if (!vub300->cmnd_out_ep) {
2262 vub300->cmnd_out_ep =
2263 endpoint->bEndpointAddress;
2264 } else if (!vub300->data_out_ep) {
2265 vub300->data_out_ep =
2266 endpoint->bEndpointAddress;
2267 } else {
2268 dev_warn(&vub300->udev->dev,
2269 "ignoring"
2270 " unexpected bulk_out endpoint");
2271 }
2272 } else {
2273 dev_warn(&vub300->udev->dev,
2274 "vub300 ignoring EndPoint(%d) %02X", i,
2275 endpoint->bEndpointAddress);
2276 }
2277 }
2278 if (vub300->cmnd_res_ep && vub300->cmnd_out_ep &&
2279 vub300->data_inp_ep && vub300->data_out_ep) {
2280 dev_info(&vub300->udev->dev,
2281 "vub300 %s packets"
2282 " using EndPoints %02X %02X %02X %02X\n",
2283 vub300->large_usb_packets ? "LARGE" : "SMALL",
2284 vub300->cmnd_out_ep, vub300->cmnd_res_ep,
2285 vub300->data_out_ep, vub300->data_inp_ep);
2286 /* we have the expected EndPoints */
2287 } else {
2288 dev_err(&vub300->udev->dev,
2289 "Could not find two sets of bulk-in/out endpoint pairs\n");
2290 retval = -EINVAL;
2291 goto error5;
2292 }
2293 retval =
2294 usb_control_msg(vub300->udev, usb_rcvctrlpipe(vub300->udev, 0),
2295 GET_HC_INF0,
2296 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
2297 0x0000, 0x0000, &vub300->hc_info,
2298 sizeof(vub300->hc_info), HZ);
2299 if (retval < 0)
2300 goto error5;
2301 retval =
2302 usb_control_msg(vub300->udev, usb_rcvctrlpipe(vub300->udev, 0),
2303 SET_ROM_WAIT_STATES,
2304 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
2305 firmware_rom_wait_states, 0x0000, NULL, 0, HZ);
2306 if (retval < 0)
2307 goto error5;
2308 dev_info(&vub300->udev->dev,
2309 "operating_mode = %s %s %d MHz %s %d byte USB packets\n",
2310 (mmc->caps & MMC_CAP_SDIO_IRQ) ? "IRQs" : "POLL",
2311 (mmc->caps & MMC_CAP_4_BIT_DATA) ? "4-bit" : "1-bit",
2312 mmc->f_max / 1000000,
2313 pad_input_to_usb_pkt ? "padding input data to" : "with",
2314 vub300->large_usb_packets ? 512 : 64);
2315 retval =
2316 usb_control_msg(vub300->udev, usb_rcvctrlpipe(vub300->udev, 0),
2317 GET_SYSTEM_PORT_STATUS,
2318 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
2319 0x0000, 0x0000, &vub300->system_port_status,
2320 sizeof(vub300->system_port_status), HZ);
2321 if (retval < 0) {
2322 goto error4;
2323 } else if (sizeof(vub300->system_port_status) == retval) {
2324 vub300->card_present =
2325 (0x0001 & vub300->system_port_status.port_flags) ? 1 : 0;
2326 vub300->read_only =
2327 (0x0010 & vub300->system_port_status.port_flags) ? 1 : 0;
2328 } else {
2329 goto error4;
2330 }
2331 usb_set_intfdata(interface, vub300);
2332 INIT_DELAYED_WORK(&vub300->pollwork, vub300_pollwork_thread);
2333 INIT_WORK(&vub300->cmndwork, vub300_cmndwork_thread);
2334 INIT_WORK(&vub300->deadwork, vub300_deadwork_thread);
2335 kref_init(&vub300->kref);
2336 init_timer(&vub300->sg_transfer_timer);
2337 vub300->sg_transfer_timer.data = (unsigned long)vub300;
2338 vub300->sg_transfer_timer.function = vub300_sg_timed_out;
2339 kref_get(&vub300->kref);
2340 init_timer(&vub300->inactivity_timer);
2341 vub300->inactivity_timer.data = (unsigned long)vub300;
2342 vub300->inactivity_timer.function = vub300_inactivity_timer_expired;
2343 vub300->inactivity_timer.expires = jiffies + HZ;
2344 add_timer(&vub300->inactivity_timer);
2345 if (vub300->card_present)
2346 dev_info(&vub300->udev->dev,
2347 "USB vub300 remote SDIO host controller[%d]"
2348 "connected with SD/SDIO card inserted\n",
2349 interface_to_InterfaceNumber(interface));
2350 else
2351 dev_info(&vub300->udev->dev,
2352 "USB vub300 remote SDIO host controller[%d]"
2353 "connected with no SD/SDIO card inserted\n",
2354 interface_to_InterfaceNumber(interface));
2355 mmc_add_host(mmc);
2356 return 0;
2357error5:
2358 mmc_free_host(mmc);
2359 /*
2360 * and hence also frees vub300
2361 * which is contained at the end of struct mmc
2362 */
2363error4:
2364 usb_free_urb(command_out_urb);
2365error1:
2366 usb_free_urb(command_res_urb);
2367error0:
2368 return retval;
2369}
2370
2371static void vub300_disconnect(struct usb_interface *interface)
2372{ /* NOT irq */
2373 struct vub300_mmc_host *vub300 = usb_get_intfdata(interface);
2374 if (!vub300 || !vub300->mmc) {
2375 return;
2376 } else {
2377 struct mmc_host *mmc = vub300->mmc;
2378 if (!vub300->mmc) {
2379 return;
2380 } else {
2381 int ifnum = interface_to_InterfaceNumber(interface);
2382 usb_set_intfdata(interface, NULL);
2383 /* prevent more I/O from starting */
2384 vub300->interface = NULL;
2385 kref_put(&vub300->kref, vub300_delete);
2386 mmc_remove_host(mmc);
2387 pr_info("USB vub300 remote SDIO host controller[%d]"
2388 " now disconnected", ifnum);
2389 return;
2390 }
2391 }
2392}
2393
2394#ifdef CONFIG_PM
2395static int vub300_suspend(struct usb_interface *intf, pm_message_t message)
2396{
2397 struct vub300_mmc_host *vub300 = usb_get_intfdata(intf);
2398 if (!vub300 || !vub300->mmc) {
2399 return 0;
2400 } else {
2401 struct mmc_host *mmc = vub300->mmc;
2402 mmc_suspend_host(mmc);
2403 return 0;
2404 }
2405}
2406
2407static int vub300_resume(struct usb_interface *intf)
2408{
2409 struct vub300_mmc_host *vub300 = usb_get_intfdata(intf);
2410 if (!vub300 || !vub300->mmc) {
2411 return 0;
2412 } else {
2413 struct mmc_host *mmc = vub300->mmc;
2414 mmc_resume_host(mmc);
2415 return 0;
2416 }
2417}
2418#else
2419#define vub300_suspend NULL
2420#define vub300_resume NULL
2421#endif
2422static int vub300_pre_reset(struct usb_interface *intf)
2423{ /* NOT irq */
2424 struct vub300_mmc_host *vub300 = usb_get_intfdata(intf);
2425 mutex_lock(&vub300->cmd_mutex);
2426 return 0;
2427}
2428
2429static int vub300_post_reset(struct usb_interface *intf)
2430{ /* NOT irq */
2431 struct vub300_mmc_host *vub300 = usb_get_intfdata(intf);
2432 /* we are sure no URBs are active - no locking needed */
2433 vub300->errors = -EPIPE;
2434 mutex_unlock(&vub300->cmd_mutex);
2435 return 0;
2436}
2437
2438static struct usb_driver vub300_driver = {
2439 .name = "vub300",
2440 .probe = vub300_probe,
2441 .disconnect = vub300_disconnect,
2442 .suspend = vub300_suspend,
2443 .resume = vub300_resume,
2444 .pre_reset = vub300_pre_reset,
2445 .post_reset = vub300_post_reset,
2446 .id_table = vub300_table,
2447 .supports_autosuspend = 1,
2448};
2449
2450static int __init vub300_init(void)
2451{ /* NOT irq */
2452 int result;
2453
2454 pr_info("VUB300 Driver rom wait states = %02X irqpoll timeout = %04X",
2455 firmware_rom_wait_states, 0x0FFFF & firmware_irqpoll_timeout);
2456 cmndworkqueue = create_singlethread_workqueue("kvub300c");
2457 if (!cmndworkqueue) {
2458 pr_err("not enough memory for the REQUEST workqueue");
2459 result = -ENOMEM;
2460 goto out1;
2461 }
2462 pollworkqueue = create_singlethread_workqueue("kvub300p");
2463 if (!pollworkqueue) {
2464 pr_err("not enough memory for the IRQPOLL workqueue");
2465 result = -ENOMEM;
2466 goto out2;
2467 }
2468 deadworkqueue = create_singlethread_workqueue("kvub300d");
2469 if (!deadworkqueue) {
2470 pr_err("not enough memory for the EXPIRED workqueue");
2471 result = -ENOMEM;
2472 goto out3;
2473 }
2474 result = usb_register(&vub300_driver);
2475 if (result) {
2476 pr_err("usb_register failed. Error number %d", result);
2477 goto out4;
2478 }
2479 return 0;
2480out4:
2481 destroy_workqueue(deadworkqueue);
2482out3:
2483 destroy_workqueue(pollworkqueue);
2484out2:
2485 destroy_workqueue(cmndworkqueue);
2486out1:
2487 return result;
2488}
2489
2490static void __exit vub300_exit(void)
2491{
2492 usb_deregister(&vub300_driver);
2493 flush_workqueue(cmndworkqueue);
2494 flush_workqueue(pollworkqueue);
2495 flush_workqueue(deadworkqueue);
2496 destroy_workqueue(cmndworkqueue);
2497 destroy_workqueue(pollworkqueue);
2498 destroy_workqueue(deadworkqueue);
2499}
2500
2501module_init(vub300_init);
2502module_exit(vub300_exit);
2503
2504MODULE_AUTHOR("Tony Olech <tony.olech@elandigitalsystems.com>");
2505MODULE_DESCRIPTION("VUB300 USB to SD/MMC/SDIO adapter driver");
2506MODULE_LICENSE("GPL");