Skip to contentSkip to conversation

Secure Digital Host Controller id and regs

PATCH In review Pierre Ossman 3 participants 10 messages Feb 11, 2006 – Feb 18, 2006

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/2 Secure Digital Host Controller id and regs

Class code and register definitions for the Secure Digital Host Controller standard.

---

 include/linux/pci_ids.h  |    1 +
 include/linux/pci_regs.h |    3 +++
 2 files changed, 4 insertions(+), 0 deletions(-)
include/linux/pci_ids.h +1 -0
index 7a61ccd..5fa8ebe 100644
@@ -69,6 +69,7 @@
 #define PCI_CLASS_SYSTEM_TIMER		0x0802
 #define PCI_CLASS_SYSTEM_RTC		0x0803
 #define PCI_CLASS_SYSTEM_PCI_HOTPLUG	0x0804
+#define PCI_CLASS_SYSTEM_SDHCI		0x0805
 #define PCI_CLASS_SYSTEM_OTHER		0x0880
 
 #define PCI_BASE_CLASS_INPUT		0x09
include/linux/pci_regs.h +3 -0
index d27a78b..e6deda5 100644
@@ -108,6 +108,9 @@
 #define PCI_INTERRUPT_PIN	0x3d	/* 8 bits */
 #define PCI_MIN_GNT		0x3e	/* 8 bits */
 #define PCI_MAX_LAT		0x3f	/* 8 bits */
+#define PCI_SLOT_INFO		0x40	/* 8 bits */
+#define  PCI_SLOT_INFO_SLOTS(x)		((x >> 4) & 7)
+#define  PCI_SLOT_INFO_FIRST_BAR_MASK	0x07
 
 /* Header type 1 (PCI-to-PCI bridges) */
 #define PCI_PRIMARY_BUS		0x18	/* Primary bus number */
#1 lore raw

PATCH 2/2 Secure Digital Host Controller Interface driver

Driver for the Secure Digital Host Controller Interface specification.

---

 MAINTAINERS          |    7 
 drivers/mmc/Kconfig  |   11 
 drivers/mmc/Makefile |    1 
 drivers/mmc/sdhci.c  | 1243 ++++++++++++++++++++++++++++++++++++++++++++++++++
 drivers/mmc/sdhci.h  |  179 +++++++
 5 files changed, 1441 insertions(+), 0 deletions(-)
MAINTAINERS +7 -0
index b22db52..8cceada 100644
@@ -2474,6 +2474,13 @@ M:	[email protected]
 L:	[email protected]
 S:	Maintained
 
+SECURE DIGITAL HOST CONTROLLER INTERFACE DRIVER
+P:	Pierre Ossman
+M:	[email protected]
+L:	[email protected]
+W:	http://mmc.drzeus.cx/wiki/Linux/Drivers/sdhci
+S:	Maintained
+
 SKGE, SKY2 10/100/1000 GIGABIT ETHERNET DRIVERS
 P:	Stephen Hemminger
 M:	[email protected]
drivers/mmc/Kconfig +11 -0
index fa01b76..446b019 100644
@@ -58,6 +58,17 @@ config MMC_PXA
 
 	  If unsure, say N.
 
+config MMC_SDHCI
+	tristate "Secure Digital Host Controller Interface support  (EXPERIMENTAL)"
+	depends on PCI && MMC && EXPERIMENTAL
+	help
+	  This select the generic Secure Digital Host Controller Interface.
+	  It is used by manufacturers such as Texas Instruments(R), Ricoh(R)
+	  and Toshiba(R). Most controllers found in laptops are of this type.
+	  If you have a controller with this interface, say Y or M here.
+
+	  If unsure, say N.
+
 config MMC_WBSD
 	tristate "Winbond W83L51xD SD/MMC Card Interface support"
 	depends on MMC && ISA_DMA_API
drivers/mmc/Makefile +1 -0
index e351e71..769d545 100644
@@ -17,6 +17,7 @@ obj-$(CONFIG_MMC_BLOCK)		+= mmc_block.o
 #
 obj-$(CONFIG_MMC_ARMMMCI)	+= mmci.o
 obj-$(CONFIG_MMC_PXA)		+= pxamci.o
+obj-$(CONFIG_MMC_SDHCI)		+= sdhci.o
 obj-$(CONFIG_MMC_WBSD)		+= wbsd.o
 obj-$(CONFIG_MMC_AU1X)		+= au1xmmc.o
 
drivers/mmc/sdhci.c new file +1243 -0

1,243 lines not on this page · raw message

drivers/mmc/sdhci.h new file +179 -0

179 lines not on this page · raw message

#2 lore raw

Re: [PATCH 2/2] [MMC] Secure Digital Host Controller Interface driver

Quoted 27 lines · Pierre Ossman

Driver for the Secure Digital Host Controller Interface specification.

...

drivers/mmc/sdhci.c new file +22 -0
+static void sdhci_transfer_pio(struct sdhci_host *host)
+{
+	char *buffer;
+	u32 mask;
+	int bytes, size;
+	unsigned long max_jiffies;
+
+	BUG_ON(!host->data);
+
+	if (host->num_sg == 0)
+		return;
+
+	bytes = 0;
+	if (host->data->flags & MMC_DATA_READ)
+		mask = SDHCI_DATA_AVAILABLE;
+	else
+		mask = SDHCI_SPACE_AVAILABLE;
+
+	buffer = sdhci_kmap_sg(host) + host->offset;
+
+	/* Transfer shouldn't take more than 5 s */
+	max_jiffies = jiffies + HZ * 5;

drivers/mmc/sdhci.c line 209

Quoted 10 lines
drivers/mmc/sdhci.c new file +10 -0
+
+	while (host->size > 0) {
+		if (time_after(jiffies, max_jiffies)) {
+			printk(KERN_ERR "%s: PIO transfer stalled. "
+				"Please report this to "
+				BUGMAIL ".\n", mmc_hostname(host->mmc));
+			sdhci_dumpregs(host);
+			host->data->error = MMC_ERR_FAILED;
+			sdhci_finish_data(host);
+			return;

This returns with the atomic kmap still held. Ugly things will happen.

Quoted 10 lines
drivers/mmc/sdhci.c new file +10 -0
+static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
+{
+	struct sdhci_host *host;
+	unsigned long flags;
+	u8 ctrl;
+
+	host = mmc_priv(mmc);
+
+	spin_lock_irqsave(&host->lock, flags);
+

drivers/mmc/sdhci.c line 582

Quoted 10 lines
drivers/mmc/sdhci.c new file +10 -0
+	DBG("clock %uHz busmode %u powermode %u cs %u Vdd %u width %u\n",
+	     ios->clock, ios->bus_mode, ios->power_mode, ios->chip_select,
+	     ios->vdd, ios->bus_width);
+
+	/*
+	 * Reset the chip on each power off.
+	 * Should clear out any weird states.
+	 */
+	if (ios->power_mode == MMC_POWER_OFF)
+		sdhci_init(host);

This can do a 50 millisecond busywait with interrupts disabled. I'd suggest you pop the lock and do a schedule_timeout().

drivers/mmc/sdhci.c line 613

Quoted 13 lines
drivers/mmc/sdhci.c new file +13 -0
+static int sdhci_get_ro(struct mmc_host *mmc)
+{
+	struct sdhci_host *host;
+	unsigned long flags;
+	int present;
+
+	host = mmc_priv(mmc);
+
+	spin_lock_irqsave(&host->lock, flags);
+
+	present = readl(host->ioaddr + SDHCI_PRESENT_STATE);
+
+	spin_unlock_irqrestore(&host->lock, flags);

Does a readl() need a lock?

drivers/mmc/sdhci.c line 1070

Quoted 3 lines
drivers/mmc/sdhci.c new file +3 -0
+	init_timer(&host->timer);
+	host->timer.data = (unsigned long)host;
+	host->timer.function = sdhci_timeout_timer;

setup_timer().

Quoted 16 lines
drivers/mmc/sdhci.c new file +16 -0
+static void sdhci_remove_slot(struct pci_dev *pdev, int slot)
+{
+	struct sdhci_chip *chip;
+	struct mmc_host *mmc;
+	struct sdhci_host *host;
+
+	chip = pci_get_drvdata(pdev);
+	host = chip->hosts[slot];
+	mmc = host->mmc;
+
+	chip->hosts[slot] = NULL;
+
+	mmc_remove_host(mmc);
+
+	del_timer_sync(&host->timer);
+

drivers/mmc/sdhci.c line 1124

Quoted 10 lines
drivers/mmc/sdhci.c new file +10 -0
+	sdhci_reset(host, SDHCI_RESET_ALL);
+
+	tasklet_kill(&host->card_tasklet);
+	tasklet_kill(&host->finish_tasklet);
+
+	iounmap(host->ioaddr);
+
+	pci_release_region(pdev, host->bar);
+
+	free_irq(host->irq, host);

The mmc_detect_change() callback could run right here. Is everything which it touches still allocated and in an appropriate state?

Quoted 2 lines
(quoted diff) +2 -0
+	mmc_free_host(mmc);
+}
#3 lore raw

Re: [PATCH 2/2] [MMC] Secure Digital Host Controller Interface driver

Quoted 31 lines · Andrew Morton
Quoted 30 lines · Pierre Ossman

Driver for the Secure Digital Host Controller Interface specification.

...

drivers/mmc/sdhci.c new file +26 -0
+static void sdhci_transfer_pio(struct sdhci_host *host)
+{
+	char *buffer;
+	u32 mask;
+	int bytes, size;
+	unsigned long max_jiffies;
+
+	BUG_ON(!host->data);
+
+	if (host->num_sg == 0)
+		return;
+
+	bytes = 0;
+	if (host->data->flags & MMC_DATA_READ)
+		mask = SDHCI_DATA_AVAILABLE;
+	else
+		mask = SDHCI_SPACE_AVAILABLE;
+
+	buffer = sdhci_kmap_sg(host) + host->offset;
+
+	/* Transfer shouldn't take more than 5 s */
+	max_jiffies = jiffies + HZ * 5;
+
+	while (host->size > 0) {
+		if (time_after(jiffies, max_jiffies)) {
+			printk(KERN_ERR "%s: PIO transfer stalled. "
Quoted 9 lines

drivers/mmc/sdhci.c line 209

Quoted 6 lines
drivers/mmc/sdhci.c new file +6 -0
+				"Please report this to "
+				BUGMAIL ".\n", mmc_hostname(host->mmc));
+			sdhci_dumpregs(host);
+			host->data->error = MMC_ERR_FAILED;
+			sdhci_finish_data(host);
+			return;

This returns with the atomic kmap still held. Ugly things will happen.

Ouch. Missed that. Thanks for pointing it out.

Quoted 15 lines
Quoted 15 lines
drivers/mmc/sdhci.c new file +15 -0
+static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
+{
+	struct sdhci_host *host;
+	unsigned long flags;
+	u8 ctrl;
+
+	host = mmc_priv(mmc);
+
+	spin_lock_irqsave(&host->lock, flags);
+
+	DBG("clock %uHz busmode %u powermode %u cs %u Vdd %u width %u\n",
+	     ios->clock, ios->bus_mode, ios->power_mode, ios->chip_select,
+	     ios->vdd, ios->bus_width);
+
+	/*
Quoted 9 lines

drivers/mmc/sdhci.c line 582

Quoted 5 lines
drivers/mmc/sdhci.c new file +5 -0
+	 * Reset the chip on each power off.
+	 * Should clear out any weird states.
+	 */
+	if (ios->power_mode == MMC_POWER_OFF)
+		sdhci_init(host);

This can do a 50 millisecond busywait with interrupts disabled. I'd suggest you pop the lock and do a schedule_timeout().

I'll have to have an extra look at what can happen in interrupt land first.

Not sure we can use a scheduled wait though. set_ios could be reachable from interrupt context.

Quoted 7 lines
Quoted 7 lines
drivers/mmc/sdhci.c new file +7 -0
+static int sdhci_get_ro(struct mmc_host *mmc)
+{
+	struct sdhci_host *host;
+	unsigned long flags;
+	int present;
+
+	host = mmc_priv(mmc);
Quoted 9 lines

drivers/mmc/sdhci.c line 613

Quoted 6 lines
drivers/mmc/sdhci.c new file +6 -0
+
+	spin_lock_irqsave(&host->lock, flags);
+
+	present = readl(host->ioaddr + SDHCI_PRESENT_STATE);
+
+	spin_unlock_irqrestore(&host->lock, flags);

Does a readl() need a lock?

Probably not. It's more a consistency thing. :)

Quoted 27 lines

drivers/mmc/sdhci.c line 1070

Quoted 3 lines
drivers/mmc/sdhci.c new file +3 -0
+	init_timer(&host->timer);
+	host->timer.data = (unsigned long)host;
+	host->timer.function = sdhci_timeout_timer;

setup_timer().

Quoted 21 lines
drivers/mmc/sdhci.c new file +21 -0
+static void sdhci_remove_slot(struct pci_dev *pdev, int slot)
+{
+	struct sdhci_chip *chip;
+	struct mmc_host *mmc;
+	struct sdhci_host *host;
+
+	chip = pci_get_drvdata(pdev);
+	host = chip->hosts[slot];
+	mmc = host->mmc;
+
+	chip->hosts[slot] = NULL;
+
+	mmc_remove_host(mmc);
+
+	del_timer_sync(&host->timer);
+
+	sdhci_reset(host, SDHCI_RESET_ALL);
+
+	tasklet_kill(&host->card_tasklet);
+	tasklet_kill(&host->finish_tasklet);
+
Quoted 9 lines

drivers/mmc/sdhci.c line 1124

Quoted 5 lines
drivers/mmc/sdhci.c new file +5 -0
+	iounmap(host->ioaddr);
+
+	pci_release_region(pdev, host->bar);
+
+	free_irq(host->irq, host);

The mmc_detect_change() callback could run right here. Is everything which it touches still allocated and in an appropriate state?

No, it's not. But the host should be left alone after mmc_remove_host(). Otherwise I don't see a way to deallocate structures in a safe order.

Quoted 4 lines
Quoted 3 lines
(quoted diff) +2 -0
+	mmc_free_host(mmc);
+}
#4 lore raw

Re: [PATCH 2/2] [MMC] Secure Digital Host Controller Interface driver

In reply to Pierre Ossman
Quoted 24 lines · On Sat, 11 Feb 2006 01:15:26 +0100 Pierre Ossman wrote:
drivers/mmc/sdhci.c new file +24 -0
+static int __devinit sdhci_probe_slot(struct pci_dev *pdev, int slot)
+{
+	int ret;
+	struct sdhci_chip *chip;
+	struct mmc_host *mmc;
+	struct sdhci_host *host;
+
+	u8 first_bar;
+	unsigned int caps;
+
+	chip = pci_get_drvdata(pdev);
+	BUG_ON(!chip);
+
+	ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &first_bar);
+	if (ret)
+		return ret;
+
+	first_bar &= PCI_SLOT_INFO_FIRST_BAR_MASK;
+
+	mmc = mmc_alloc_host(sizeof(struct sdhci_host), &pdev->dev);
+	if (!mmc)
+		return -ENOMEM;
+
+	host = mmc_priv(mmc);

drivers/mmc/sdhci.c line 987

Quoted 10 lines
drivers/mmc/sdhci.c new file +10 -0
+	host->mmc = mmc;
+
+	host->bar = first_bar + slot;
+
+	host->addr = pci_resource_start(pdev, host->bar);
+	host->irq = pdev->irq;
+
+	DBG("slot %d at 0x%08lx, irq %d\n", slot, host->addr, host->irq);
+
+	BUG_ON(!(pci_resource_flags(pdev, first_bar + slot) & IORESOURCE_MEM));

Oopsing the kernel when a broken device is found does not look nice. Especially when we are not really sure that the device is broken (because we don't have the official SDHCI spec).

Also, it would be good to sanity check all parameters you fetch from the device - e.g., host->bar must be <= 5, pci_resource_len() of it must be at least 0x100 - just to be safe in case we don't know about some thing which exists in the official spec, but is not used by devices encountered while writing and testing the driver.

Quoted 6 lines
drivers/mmc/sdhci.c new file +6 -0
+
+	snprintf(host->slot_descr, 20, "sdhci:slot%d", slot);
+
+	ret = pci_request_region(pdev, host->bar, host->slot_descr);
+	if (ret)
+		goto free;

drivers/mmc/sdhci.c line 1003

Quoted 10 lines
drivers/mmc/sdhci.c new file +10 -0
+
+	host->ioaddr = ioremap_nocache(host->addr,
+		pci_resource_len(pdev, host->bar));
+	if (!host->ioaddr) {
+		ret = -ENOMEM;
+		goto release;
+	}
+
+	ret = request_irq(host->irq, sdhci_irq, SA_SHIRQ,
+		host->slot_descr, host);

The interrupt handler can be called immediately after request_irq() completes (even if you are sure that the device itself cannot generate interrupts at this point, the interrupt line can be shared). And host->lock is not yet initialized - oops...

Quoted 111 lines
drivers/mmc/sdhci.c new file +111 -0
+	if (ret)
+		goto unmap;
+
+	caps = readl(host->ioaddr + SDHCI_CAPABILITIES);
+
+	if ((caps & SDHCI_CAN_DO_DMA) && ((pdev->class & 0x0000FF) == 0x01))
+		host->flags |= SDHCI_USE_DMA;
+
+	if (host->flags & SDHCI_USE_DMA) {
+		if (pci_set_dma_mask(pdev, DMA_32BIT_MASK)) {
+			printk(KERN_WARNING "%s: No suitable DMA available. "
+				"Falling back to PIO.\n", host->slot_descr);
+			host->flags &= ~SDHCI_USE_DMA;
+		}
+	}
+
+	if (host->flags & SDHCI_USE_DMA)
+		pci_set_master(pdev);
+	else /* XXX: Hack to get MMC layer to avoid highmem */
+		pdev->dma_mask = 0;
+
+	host->max_clk = (caps & SDHCI_CLOCK_BASE_MASK) >> SDHCI_CLOCK_BASE_SHIFT;
+	host->max_clk *= 1000000;
+
+	/*
+	 * Set host parameters.
+	 */
+	mmc->ops = &sdhci_ops;
+	mmc->f_min = host->max_clk / 256;
+	mmc->f_max = host->max_clk;
+	mmc->ocr_avail = MMC_VDD_32_33|MMC_VDD_33_34;
+	mmc->caps = MMC_CAP_4_BIT_DATA;
+
+	spin_lock_init(&host->lock);
+
+	/*
+	 * Maximum number of segments. Hardware cannot do scatter lists.
+	 */
+	if (host->flags & SDHCI_USE_DMA)
+		mmc->max_hw_segs = 1;
+	else
+		mmc->max_hw_segs = 16;
+	mmc->max_phys_segs = 16;
+
+	/*
+	 * Maximum number of sectors in one transfer. Limited by sector
+	 * count register.
+	 */
+	mmc->max_sectors = 0x3FFF;
+
+	/*
+	 * Maximum segment size. Could be one segment with the maximum number
+	 * of sectors.
+	 */
+	mmc->max_seg_size = mmc->max_sectors * 512;
+
+	/*
+	 * Init tasklets.
+	 */
+	tasklet_init(&host->card_tasklet,
+		sdhci_tasklet_card, (unsigned long)host);
+	tasklet_init(&host->finish_tasklet,
+		sdhci_tasklet_finish, (unsigned long)host);
+
+	init_timer(&host->timer);
+	host->timer.data = (unsigned long)host;
+	host->timer.function = sdhci_timeout_timer;
+
+	sdhci_init(host);
+
+#ifdef CONFIG_MMC_DEBUG
+	sdhci_dumpregs(host);
+#endif
+
+	host->chip = chip;
+	chip->hosts[slot] = host;
+
+	mmc_add_host(mmc);
+
+	printk(KERN_INFO "%s: SDHCI at 0x%08lx irq %d %s\n", mmc_hostname(mmc),
+		host->addr, host->irq,
+		(host->flags & SDHCI_USE_DMA)?"DMA":"PIO");
+
+	return 0;
+
+unmap:
+	iounmap(host->ioaddr);
+release:
+	pci_release_region(pdev, host->bar);
+free:
+	mmc_free_host(mmc);
+
+	return ret;
+}
+
+static void sdhci_remove_slot(struct pci_dev *pdev, int slot)
+{
+	struct sdhci_chip *chip;
+	struct mmc_host *mmc;
+	struct sdhci_host *host;
+
+	chip = pci_get_drvdata(pdev);
+	host = chip->hosts[slot];
+	mmc = host->mmc;
+
+	chip->hosts[slot] = NULL;
+
+	mmc_remove_host(mmc);
+
+	del_timer_sync(&host->timer);
+

drivers/mmc/sdhci.c line 1124

Quoted 10 lines
drivers/mmc/sdhci.c new file +10 -0
+	sdhci_reset(host, SDHCI_RESET_ALL);
+
+	tasklet_kill(&host->card_tasklet);
+	tasklet_kill(&host->finish_tasklet);
+
+	iounmap(host->ioaddr);
+
+	pci_release_region(pdev, host->bar);
+
+	free_irq(host->irq, host);

The same problem as with request_irq(), just from the other side - until free_irq() returns, you may still get calls to your interrupt handler, and host->ioaddr is already unmapped - oops again.

drivers/mmc/sdhci.c line 1137

Quoted 13 lines
drivers/mmc/sdhci.c new file +13 -0
+
+	mmc_free_host(mmc);
+}
+
+static int __devinit sdhci_probe(struct pci_dev *pdev,
+	const struct pci_device_id *ent)
+{
+	int ret, i;
+	u8 slots;
+	struct sdhci_chip *chip;
+
+	BUG_ON(pdev == NULL);
+	BUG_ON(ent == NULL);

IMHO these BUG_ON() calls are overkill.

[...]

Quoted 1 line

+typedef struct sdhci_host *sdhci_host_p;

The general policy seems to be "typedefs are evil"...

#5 lore raw

Re: [PATCH 2/2] [MMC] Secure Digital Host Controller Interface driver

Quoted 5 lines · Sergey Vlasov

The interrupt handler can be called immediately after request_irq() completes (even if you are sure that the device itself cannot generate interrupts at this point, the interrupt line can be shared). And host->lock is not yet initialized - oops...

Ah. I'll fix that.

Quoted 5 lines

The same problem as with request_irq(), just from the other side - until free_irq() returns, you may still get calls to your interrupt handler, and host->ioaddr is already unmapped - oops again.

Ditto.

Quoted 7 lines
Quoted 7 lines
drivers/mmc/sdhci.c new file +7 -0
+
+	mmc_free_host(mmc);
+}
+
+static int __devinit sdhci_probe(struct pci_dev *pdev,
+	const struct pci_device_id *ent)
+{
Quoted 9 lines

drivers/mmc/sdhci.c line 1137

Quoted 6 lines
drivers/mmc/sdhci.c new file +6 -0
+	int ret, i;
+	u8 slots;
+	struct sdhci_chip *chip;
+
+	BUG_ON(pdev == NULL);
+	BUG_ON(ent == NULL);

IMHO these BUG_ON() calls are overkill.

I prefer BUG_ON():s since they print a line number. A page fault induced oops just gives me a byte offset into the compiled function.

Quoted 4 lines

[...]

Quoted 1 line

+typedef struct sdhci_host *sdhci_host_p;

The general policy seems to be "typedefs are evil"...

Fair enough. It didn't get much usage anyway.

Thanks for the code review. :)

Rgds
Pierre

#6 lore raw

Re: [PATCH 2/2] [MMC] Secure Digital Host Controller Interface driver

In reply to Sergey Vlasov

Patch with your suggested improvements.

Rgds
Pierre

From: Pierre Ossman <[email protected]>

---

 drivers/mmc/sdhci.c |   53 ++++++++++++++++++++++++++++++++++++---------------
 drivers/mmc/sdhci.h |   12 +++++++++---
 2 files changed, 46 insertions(+), 19 deletions(-)
drivers/mmc/sdhci.c +37 -16
index 4e12fe0..667928d 100644
@@ -204,6 +204,9 @@ static void sdhci_transfer_pio(struct sd
 				"Please report this to "
 				BUGMAIL ".\n", mmc_hostname(host->mmc));
 			sdhci_dumpregs(host);
+
+			sdhci_kunmap_sg(host);
+
 			host->data->error = MMC_ERR_FAILED;
 			sdhci_finish_data(host);
 			return;
@@ -578,8 +581,12 @@ static void sdhci_set_ios(struct mmc_hos
 	 * Reset the chip on each power off.
 	 * Should clear out any weird states.
 	 */
-	if (ios->power_mode == MMC_POWER_OFF)
+	if (ios->power_mode == MMC_POWER_OFF) {
+		writel(0, host->ioaddr + SDHCI_SIGNAL_ENABLE);
+		spin_unlock_irqrestore(&host->lock, flags);
 		sdhci_init(host);
+		spin_lock_irqsave(&host->lock, flags);
+	}
 
 	sdhci_set_clock(host, ios->clock);
 
@@ -970,6 +977,21 @@ static int __devinit sdhci_probe_slot(st
 
 	first_bar &= PCI_SLOT_INFO_FIRST_BAR_MASK;
 
+	if (first_bar > 5) {
+		printk(KERN_ERR DRIVER_NAME ": Invalid first BAR. Aborting.\n");
+		return -ENODEV;
+	}
+
+	if (!(pci_resource_flags(pdev, first_bar + slot) & IORESOURCE_MEM)) {
+		printk(KERN_ERR DRIVER_NAME ": BAR is not iomem. Aborting.\n");
+		return -ENODEV;
+	}
+
+	if (pci_resource_len(pdev, first_bar + slot) != 0x100) {
+		printk(KERN_ERR DRIVER_NAME ": Invalid iomem size. Aborting.\n");
+		return -ENODEV;
+	}
+
 	mmc = mmc_alloc_host(sizeof(struct sdhci_host), &pdev->dev);
 	if (!mmc)
 		return -ENOMEM;
@@ -984,8 +1006,6 @@ static int __devinit sdhci_probe_slot(st
 
 	DBG("slot %d at 0x%08lx, irq %d\n", slot, host->addr, host->irq);
 
-	BUG_ON(!(pci_resource_flags(pdev, first_bar + slot) & IORESOURCE_MEM));
-
 	snprintf(host->slot_descr, 20, "sdhci:slot%d", slot);
 
 	ret = pci_request_region(pdev, host->bar, host->slot_descr);
@@ -999,11 +1019,6 @@ static int __devinit sdhci_probe_slot(st
 		goto release;
 	}
 
-	ret = request_irq(host->irq, sdhci_irq, SA_SHIRQ,
-		host->slot_descr, host);
-	if (ret)
-		goto unmap;
-
 	caps = readl(host->ioaddr + SDHCI_CAPABILITIES);
 
 	if ((caps & SDHCI_CAN_DO_DMA) && ((pdev->class & 0x0000FF) == 0x01))
@@ -1065,9 +1080,12 @@ static int __devinit sdhci_probe_slot(st
 	tasklet_init(&host->finish_tasklet,
 		sdhci_tasklet_finish, (unsigned long)host);
 
-	init_timer(&host->timer);
-	host->timer.data = (unsigned long)host;
-	host->timer.function = sdhci_timeout_timer;
+	setup_timer(&host->timer, sdhci_timeout_timer, (int)host);
+
+	ret = request_irq(host->irq, sdhci_irq, SA_SHIRQ,
+		host->slot_descr, host);
+	if (ret)
+		goto unmap;
 
 	sdhci_init(host);
 
@@ -1087,6 +1105,9 @@ static int __devinit sdhci_probe_slot(st
 	return 0;
 
 unmap:
+	tasklet_kill(&host->card_tasklet);
+	tasklet_kill(&host->finish_tasklet);
+
 	iounmap(host->ioaddr);
 release:
 	pci_release_region(pdev, host->bar);
@@ -1110,10 +1131,12 @@ static void sdhci_remove_slot(struct pci
 
 	mmc_remove_host(mmc);
 
-	del_timer_sync(&host->timer);
-
 	sdhci_reset(host, SDHCI_RESET_ALL);
 
+	free_irq(host->irq, host);
+
+	del_timer_sync(&host->timer);
+
 	tasklet_kill(&host->card_tasklet);
 	tasklet_kill(&host->finish_tasklet);
 
@@ -1121,8 +1144,6 @@ static void sdhci_remove_slot(struct pci
 
 	pci_release_region(pdev, host->bar);
 
-	free_irq(host->irq, host);
-
 	mmc_free_host(mmc);
 }
 
@@ -1152,7 +1173,7 @@ static int __devinit sdhci_probe(struct 
 		return ret;
 
 	chip = kzalloc(sizeof(struct sdhci_chip) +
-		sizeof(sdhci_host_p) * slots, GFP_KERNEL);
+		sizeof(struct sdhci_host*) * slots, GFP_KERNEL);
 	if (!chip) {
 		ret = -ENOMEM;
 		goto err;
drivers/mmc/sdhci.h +9 -3
index 286d4d1..3b270ef 100644
@@ -9,6 +9,14 @@
  */
 
 /*
+ * PCI registers
+ */
+
+#define PCI_SLOT_INFO			0x40	/* 8 bits */
+#define  PCI_SLOT_INFO_SLOTS(x)		((x >> 4) & 7)
+#define  PCI_SLOT_INFO_FIRST_BAR_MASK	0x07
+
+/*
  * Controller registers
  */
 
@@ -169,11 +177,9 @@ struct sdhci_host {
 	struct timer_list	timer;		/* Timer for timeouts */
 };
 
-typedef struct sdhci_host *sdhci_host_p;
-
 struct sdhci_chip {
 	struct pci_dev		*pdev;
 
 	int			num_slots;	/* Slots on controller */
-	sdhci_host_p		hosts[0];	/* Pointers to hosts */
+	struct sdhci_host	*hosts[0];	/* Pointers to hosts */
 };
#7 lore raw
In reply to Pierre Ossman
Quoted 27 lines · On Sat, 11 Feb 2006 01:15:23 +0100 Pierre Ossman wrote:

Class code and register definitions for the Secure Digital Host Controller standard.

---

 include/linux/pci_ids.h  |    1 +
 include/linux/pci_regs.h |    3 +++
 2 files changed, 4 insertions(+), 0 deletions(-)
include/linux/pci_ids.h +1 -0
index 7a61ccd..5fa8ebe 100644
@@ -69,6 +69,7 @@
 #define PCI_CLASS_SYSTEM_TIMER		0x0802
 #define PCI_CLASS_SYSTEM_RTC		0x0803
 #define PCI_CLASS_SYSTEM_PCI_HOTPLUG	0x0804
+#define PCI_CLASS_SYSTEM_SDHCI		0x0805
 #define PCI_CLASS_SYSTEM_OTHER		0x0880
 
 #define PCI_BASE_CLASS_INPUT		0x09
include/linux/pci_regs.h +0 -0
index d27a78b..e6deda5 100644

include/linux/pci_regs.h line 113

Quoted 6 lines
include/linux/pci_regs.h +3 -0
@@ -108,6 +108,9 @@
 #define PCI_INTERRUPT_PIN	0x3d	/* 8 bits */
 #define PCI_MIN_GNT		0x3e	/* 8 bits */
 #define PCI_MAX_LAT		0x3f	/* 8 bits */
+#define PCI_SLOT_INFO		0x40	/* 8 bits */
+#define  PCI_SLOT_INFO_SLOTS(x)		((x >> 4) & 7)
+#define  PCI_SLOT_INFO_FIRST_BAR_MASK	0x07

Does this really belong here? This register is specific to the SDHCI class, while all other definitions in pci_regs.h apply to all PCI devices.

drivers/mmc/sdhci.h seems to be a more logical place for SLOT_INFO definitions.

Quoted 4 lines
include/linux/pci_regs.h +0 -0
 /* Header type 1 (PCI-to-PCI bridges) */
 #define PCI_PRIMARY_BUS		0x18	/* Primary bus number */
 
#8 lore raw
Quoted 18 lines · Sergey Vlasov

include/linux/pci_regs.h line 113

Quoted 11 lines · On Sat, 11 Feb 2006 01:15:23 +0100 Pierre Ossman wrote:
include/linux/pci_regs.h +3 -0
index d27a78b..e6deda5 100644
@@ -108,6 +108,9 @@
 #define PCI_INTERRUPT_PIN	0x3d	/* 8 bits */
 #define PCI_MIN_GNT		0x3e	/* 8 bits */
 #define PCI_MAX_LAT		0x3f	/* 8 bits */
+#define PCI_SLOT_INFO		0x40	/* 8 bits */
+#define  PCI_SLOT_INFO_SLOTS(x)		((x >> 4) & 7)
+#define  PCI_SLOT_INFO_FIRST_BAR_MASK	0x07

Does this really belong here? This register is specific to the SDHCI class, while all other definitions in pci_regs.h apply to all PCI devices.

Quoted 3 lines

drivers/mmc/sdhci.h seems to be a more logical place for SLOT_INFO definitions.

Possibly. I wasn't sure of the appropriate header for PCI information. Although specific for the SDHCI hosts, it's still a PCI register and not a device register.

If the consensus is that this should be in the driver header then I'll gladly move it.

Rgds
Pierre

#9 lore raw
In reply to Sergey Vlasov
Quoted 16 lines · Sergey Vlasov

include/linux/pci_regs.h line 113

Quoted 11 lines
include/linux/pci_regs.h +3 -0
index d27a78b..e6deda5 100644
@@ -108,6 +108,9 @@
 #define PCI_INTERRUPT_PIN	0x3d	/* 8 bits */
 #define PCI_MIN_GNT		0x3e	/* 8 bits */
 #define PCI_MAX_LAT		0x3f	/* 8 bits */
+#define PCI_SLOT_INFO		0x40	/* 8 bits */
+#define  PCI_SLOT_INFO_SLOTS(x)		((x >> 4) & 7)
+#define  PCI_SLOT_INFO_FIRST_BAR_MASK	0x07

Does this really belong here? This register is specific to the SDHCI class, while all other definitions in pci_regs.h apply to all PCI devices.

Quoted 3 lines

drivers/mmc/sdhci.h seems to be a more logical place for SLOT_INFO definitions.

Fixed here. (It will be added in a -fix patch for sdhci)

Rgds
Pierre

From: Pierre Ossman <[email protected]>

---

 include/linux/pci_regs.h |    3 ---
 1 files changed, 0 insertions(+), 3 deletions(-)
include/linux/pci_regs.h +0 -3
index e6deda5..d27a78b 100644
@@ -108,9 +108,6 @@
 #define PCI_INTERRUPT_PIN	0x3d	/* 8 bits */
 #define PCI_MIN_GNT		0x3e	/* 8 bits */
 #define PCI_MAX_LAT		0x3f	/* 8 bits */
-#define PCI_SLOT_INFO		0x40	/* 8 bits */
-#define  PCI_SLOT_INFO_SLOTS(x)		((x >> 4) & 7)
-#define  PCI_SLOT_INFO_FIRST_BAR_MASK	0x07
 
 /* Header type 1 (PCI-to-PCI bridges) */
 #define PCI_PRIMARY_BUS		0x18	/* Primary bus number */