| Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 1 | /*  linux/drivers/mmc/host/sdhci-pci.c - SDHCI on PCI bus interface | 
 | 2 |  * | 
 | 3 |  *  Copyright (C) 2005-2008 Pierre Ossman, All Rights Reserved. | 
 | 4 |  * | 
 | 5 |  * This program is free software; you can redistribute it and/or modify | 
 | 6 |  * it under the terms of the GNU General Public License as published by | 
 | 7 |  * the Free Software Foundation; either version 2 of the License, or (at | 
 | 8 |  * your option) any later version. | 
 | 9 |  * | 
 | 10 |  * Thanks to the following companies for their support: | 
 | 11 |  * | 
 | 12 |  *     - JMicron (hardware and technical support) | 
 | 13 |  */ | 
 | 14 |  | 
 | 15 | #include <linux/delay.h> | 
 | 16 | #include <linux/highmem.h> | 
 | 17 | #include <linux/pci.h> | 
 | 18 | #include <linux/dma-mapping.h> | 
 | 19 |  | 
 | 20 | #include <linux/mmc/host.h> | 
 | 21 |  | 
 | 22 | #include <asm/scatterlist.h> | 
 | 23 | #include <asm/io.h> | 
 | 24 |  | 
 | 25 | #include "sdhci.h" | 
 | 26 |  | 
 | 27 | /* | 
 | 28 |  * PCI registers | 
 | 29 |  */ | 
 | 30 |  | 
 | 31 | #define PCI_SDHCI_IFPIO			0x00 | 
 | 32 | #define PCI_SDHCI_IFDMA			0x01 | 
 | 33 | #define PCI_SDHCI_IFVENDOR		0x02 | 
 | 34 |  | 
 | 35 | #define PCI_SLOT_INFO			0x40	/* 8 bits */ | 
 | 36 | #define  PCI_SLOT_INFO_SLOTS(x)		((x >> 4) & 7) | 
 | 37 | #define  PCI_SLOT_INFO_FIRST_BAR_MASK	0x07 | 
 | 38 |  | 
 | 39 | #define MAX_SLOTS			8 | 
 | 40 |  | 
| Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 41 | struct sdhci_pci_chip; | 
| Pierre Ossman | 4489428 | 2008-04-04 19:36:59 +0200 | [diff] [blame] | 42 | struct sdhci_pci_slot; | 
| Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 43 |  | 
| Pierre Ossman | 2260640 | 2008-03-23 19:33:23 +0100 | [diff] [blame] | 44 | struct sdhci_pci_fixes { | 
 | 45 | 	unsigned int		quirks; | 
 | 46 |  | 
 | 47 | 	int			(*probe)(struct sdhci_pci_chip*); | 
| Pierre Ossman | 45211e2 | 2008-03-24 13:09:09 +0100 | [diff] [blame] | 48 |  | 
| Pierre Ossman | 4489428 | 2008-04-04 19:36:59 +0200 | [diff] [blame] | 49 | 	int			(*probe_slot)(struct sdhci_pci_slot*); | 
| Pierre Ossman | 1e72859 | 2008-04-16 19:13:13 +0200 | [diff] [blame] | 50 | 	void			(*remove_slot)(struct sdhci_pci_slot*, int); | 
| Pierre Ossman | 4489428 | 2008-04-04 19:36:59 +0200 | [diff] [blame] | 51 |  | 
 | 52 | 	int			(*suspend)(struct sdhci_pci_chip*, | 
 | 53 | 					pm_message_t); | 
| Pierre Ossman | 45211e2 | 2008-03-24 13:09:09 +0100 | [diff] [blame] | 54 | 	int			(*resume)(struct sdhci_pci_chip*); | 
| Pierre Ossman | 2260640 | 2008-03-23 19:33:23 +0100 | [diff] [blame] | 55 | }; | 
 | 56 |  | 
| Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 57 | struct sdhci_pci_slot { | 
 | 58 | 	struct sdhci_pci_chip	*chip; | 
 | 59 | 	struct sdhci_host	*host; | 
 | 60 |  | 
 | 61 | 	int			pci_bar; | 
 | 62 | }; | 
 | 63 |  | 
 | 64 | struct sdhci_pci_chip { | 
 | 65 | 	struct pci_dev		*pdev; | 
| Pierre Ossman | 2260640 | 2008-03-23 19:33:23 +0100 | [diff] [blame] | 66 |  | 
 | 67 | 	unsigned int		quirks; | 
 | 68 | 	const struct sdhci_pci_fixes *fixes; | 
| Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 69 |  | 
 | 70 | 	int			num_slots;	/* Slots on controller */ | 
 | 71 | 	struct sdhci_pci_slot	*slots[MAX_SLOTS]; /* Pointers to host slots */ | 
 | 72 | }; | 
 | 73 |  | 
| Pierre Ossman | 2260640 | 2008-03-23 19:33:23 +0100 | [diff] [blame] | 74 |  | 
 | 75 | /*****************************************************************************\ | 
 | 76 |  *                                                                           * | 
 | 77 |  * Hardware specific quirk handling                                          * | 
 | 78 |  *                                                                           * | 
 | 79 | \*****************************************************************************/ | 
 | 80 |  | 
 | 81 | static int ricoh_probe(struct sdhci_pci_chip *chip) | 
 | 82 | { | 
 | 83 | 	if (chip->pdev->subsystem_vendor == PCI_VENDOR_ID_IBM) | 
 | 84 | 		chip->quirks |= SDHCI_QUIRK_CLOCK_BEFORE_RESET; | 
 | 85 |  | 
 | 86 | 	if (chip->pdev->subsystem_vendor == PCI_VENDOR_ID_SAMSUNG) | 
 | 87 | 		chip->quirks |= SDHCI_QUIRK_NO_CARD_NO_RESET; | 
 | 88 |  | 
 | 89 | 	return 0; | 
 | 90 | } | 
 | 91 |  | 
 | 92 | static const struct sdhci_pci_fixes sdhci_ricoh = { | 
 | 93 | 	.probe		= ricoh_probe, | 
| Pierre Ossman | 309d973 | 2008-05-28 09:54:50 +0200 | [diff] [blame] | 94 | 	.quirks		= SDHCI_QUIRK_32BIT_DMA_ADDR, | 
| Pierre Ossman | 2260640 | 2008-03-23 19:33:23 +0100 | [diff] [blame] | 95 | }; | 
 | 96 |  | 
 | 97 | static const struct sdhci_pci_fixes sdhci_ene_712 = { | 
 | 98 | 	.quirks		= SDHCI_QUIRK_SINGLE_POWER_WRITE | | 
 | 99 | 			  SDHCI_QUIRK_BROKEN_DMA, | 
 | 100 | }; | 
 | 101 |  | 
 | 102 | static const struct sdhci_pci_fixes sdhci_ene_714 = { | 
 | 103 | 	.quirks		= SDHCI_QUIRK_SINGLE_POWER_WRITE | | 
 | 104 | 			  SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS | | 
 | 105 | 			  SDHCI_QUIRK_BROKEN_DMA, | 
 | 106 | }; | 
 | 107 |  | 
 | 108 | static const struct sdhci_pci_fixes sdhci_cafe = { | 
 | 109 | 	.quirks		= SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER | | 
| Pierre Ossman | ee53ab5 | 2008-07-05 00:25:15 +0200 | [diff] [blame] | 110 | 			  SDHCI_QUIRK_BROKEN_TIMEOUT_VAL, | 
| Pierre Ossman | 2260640 | 2008-03-23 19:33:23 +0100 | [diff] [blame] | 111 | }; | 
 | 112 |  | 
| Pierre Ossman | 45211e2 | 2008-03-24 13:09:09 +0100 | [diff] [blame] | 113 | static int jmicron_pmos(struct sdhci_pci_chip *chip, int on) | 
 | 114 | { | 
 | 115 | 	u8 scratch; | 
 | 116 | 	int ret; | 
 | 117 |  | 
 | 118 | 	ret = pci_read_config_byte(chip->pdev, 0xAE, &scratch); | 
 | 119 | 	if (ret) | 
 | 120 | 		return ret; | 
 | 121 |  | 
 | 122 | 	/* | 
 | 123 | 	 * Turn PMOS on [bit 0], set over current detection to 2.4 V | 
 | 124 | 	 * [bit 1:2] and enable over current debouncing [bit 6]. | 
 | 125 | 	 */ | 
 | 126 | 	if (on) | 
 | 127 | 		scratch |= 0x47; | 
 | 128 | 	else | 
 | 129 | 		scratch &= ~0x47; | 
 | 130 |  | 
 | 131 | 	ret = pci_write_config_byte(chip->pdev, 0xAE, scratch); | 
 | 132 | 	if (ret) | 
 | 133 | 		return ret; | 
 | 134 |  | 
 | 135 | 	return 0; | 
 | 136 | } | 
 | 137 |  | 
 | 138 | static int jmicron_probe(struct sdhci_pci_chip *chip) | 
 | 139 | { | 
 | 140 | 	int ret; | 
 | 141 |  | 
| Pierre Ossman | 93fc48c | 2008-06-28 18:21:41 +0200 | [diff] [blame] | 142 | 	if (chip->pdev->revision == 0) { | 
 | 143 | 		chip->quirks |= SDHCI_QUIRK_32BIT_DMA_ADDR | | 
 | 144 | 			  SDHCI_QUIRK_32BIT_DMA_SIZE | | 
| Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 145 | 			  SDHCI_QUIRK_32BIT_ADMA_SIZE | | 
| Pierre Ossman | 4a3cba3 | 2008-07-29 00:11:16 +0200 | [diff] [blame] | 146 | 			  SDHCI_QUIRK_RESET_AFTER_REQUEST | | 
 | 147 | 			  SDHCI_QUIRK_BROKEN_SMALL_PIO; | 
| Pierre Ossman | 93fc48c | 2008-06-28 18:21:41 +0200 | [diff] [blame] | 148 | 	} | 
 | 149 |  | 
| Pierre Ossman | 45211e2 | 2008-03-24 13:09:09 +0100 | [diff] [blame] | 150 | 	/* | 
| Pierre Ossman | 4489428 | 2008-04-04 19:36:59 +0200 | [diff] [blame] | 151 | 	 * JMicron chips can have two interfaces to the same hardware | 
 | 152 | 	 * in order to work around limitations in Microsoft's driver. | 
 | 153 | 	 * We need to make sure we only bind to one of them. | 
 | 154 | 	 * | 
 | 155 | 	 * This code assumes two things: | 
 | 156 | 	 * | 
 | 157 | 	 * 1. The PCI code adds subfunctions in order. | 
 | 158 | 	 * | 
 | 159 | 	 * 2. The MMC interface has a lower subfunction number | 
 | 160 | 	 *    than the SD interface. | 
 | 161 | 	 */ | 
 | 162 | 	if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_SD) { | 
 | 163 | 		struct pci_dev *sd_dev; | 
 | 164 |  | 
 | 165 | 		sd_dev = NULL; | 
 | 166 | 		while ((sd_dev = pci_get_device(PCI_VENDOR_ID_JMICRON, | 
 | 167 | 			PCI_DEVICE_ID_JMICRON_JMB38X_MMC, sd_dev)) != NULL) { | 
 | 168 | 			if ((PCI_SLOT(chip->pdev->devfn) == | 
 | 169 | 				PCI_SLOT(sd_dev->devfn)) && | 
 | 170 | 				(chip->pdev->bus == sd_dev->bus)) | 
 | 171 | 				break; | 
 | 172 | 		} | 
 | 173 |  | 
 | 174 | 		if (sd_dev) { | 
 | 175 | 			pci_dev_put(sd_dev); | 
 | 176 | 			dev_info(&chip->pdev->dev, "Refusing to bind to " | 
 | 177 | 				"secondary interface.\n"); | 
 | 178 | 			return -ENODEV; | 
 | 179 | 		} | 
 | 180 | 	} | 
 | 181 |  | 
 | 182 | 	/* | 
| Pierre Ossman | 45211e2 | 2008-03-24 13:09:09 +0100 | [diff] [blame] | 183 | 	 * JMicron chips need a bit of a nudge to enable the power | 
 | 184 | 	 * output pins. | 
 | 185 | 	 */ | 
 | 186 | 	ret = jmicron_pmos(chip, 1); | 
 | 187 | 	if (ret) { | 
 | 188 | 		dev_err(&chip->pdev->dev, "Failure enabling card power\n"); | 
 | 189 | 		return ret; | 
 | 190 | 	} | 
 | 191 |  | 
 | 192 | 	return 0; | 
 | 193 | } | 
 | 194 |  | 
| Pierre Ossman | 4489428 | 2008-04-04 19:36:59 +0200 | [diff] [blame] | 195 | static void jmicron_enable_mmc(struct sdhci_host *host, int on) | 
 | 196 | { | 
 | 197 | 	u8 scratch; | 
 | 198 |  | 
 | 199 | 	scratch = readb(host->ioaddr + 0xC0); | 
 | 200 |  | 
 | 201 | 	if (on) | 
 | 202 | 		scratch |= 0x01; | 
 | 203 | 	else | 
 | 204 | 		scratch &= ~0x01; | 
 | 205 |  | 
 | 206 | 	writeb(scratch, host->ioaddr + 0xC0); | 
 | 207 | } | 
 | 208 |  | 
 | 209 | static int jmicron_probe_slot(struct sdhci_pci_slot *slot) | 
 | 210 | { | 
| Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 211 | 	if (slot->chip->pdev->revision == 0) { | 
 | 212 | 		u16 version; | 
 | 213 |  | 
 | 214 | 		version = readl(slot->host->ioaddr + SDHCI_HOST_VERSION); | 
 | 215 | 		version = (version & SDHCI_VENDOR_VER_MASK) >> | 
 | 216 | 			SDHCI_VENDOR_VER_SHIFT; | 
 | 217 |  | 
 | 218 | 		/* | 
 | 219 | 		 * Older versions of the chip have lots of nasty glitches | 
 | 220 | 		 * in the ADMA engine. It's best just to avoid it | 
 | 221 | 		 * completely. | 
 | 222 | 		 */ | 
 | 223 | 		if (version < 0xAC) | 
 | 224 | 			slot->host->quirks |= SDHCI_QUIRK_BROKEN_ADMA; | 
 | 225 | 	} | 
 | 226 |  | 
| Pierre Ossman | 4489428 | 2008-04-04 19:36:59 +0200 | [diff] [blame] | 227 | 	/* | 
 | 228 | 	 * The secondary interface requires a bit set to get the | 
 | 229 | 	 * interrupts. | 
 | 230 | 	 */ | 
 | 231 | 	if (slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC) | 
 | 232 | 		jmicron_enable_mmc(slot->host, 1); | 
 | 233 |  | 
 | 234 | 	return 0; | 
 | 235 | } | 
 | 236 |  | 
| Pierre Ossman | 1e72859 | 2008-04-16 19:13:13 +0200 | [diff] [blame] | 237 | static void jmicron_remove_slot(struct sdhci_pci_slot *slot, int dead) | 
| Pierre Ossman | 4489428 | 2008-04-04 19:36:59 +0200 | [diff] [blame] | 238 | { | 
| Pierre Ossman | 1e72859 | 2008-04-16 19:13:13 +0200 | [diff] [blame] | 239 | 	if (dead) | 
 | 240 | 		return; | 
 | 241 |  | 
| Pierre Ossman | 4489428 | 2008-04-04 19:36:59 +0200 | [diff] [blame] | 242 | 	if (slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC) | 
 | 243 | 		jmicron_enable_mmc(slot->host, 0); | 
 | 244 | } | 
 | 245 |  | 
 | 246 | static int jmicron_suspend(struct sdhci_pci_chip *chip, pm_message_t state) | 
 | 247 | { | 
 | 248 | 	int i; | 
 | 249 |  | 
 | 250 | 	if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC) { | 
 | 251 | 		for (i = 0;i < chip->num_slots;i++) | 
 | 252 | 			jmicron_enable_mmc(chip->slots[i]->host, 0); | 
 | 253 | 	} | 
 | 254 |  | 
 | 255 | 	return 0; | 
 | 256 | } | 
 | 257 |  | 
| Pierre Ossman | 45211e2 | 2008-03-24 13:09:09 +0100 | [diff] [blame] | 258 | static int jmicron_resume(struct sdhci_pci_chip *chip) | 
 | 259 | { | 
| Pierre Ossman | 4489428 | 2008-04-04 19:36:59 +0200 | [diff] [blame] | 260 | 	int ret, i; | 
 | 261 |  | 
 | 262 | 	if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC) { | 
 | 263 | 		for (i = 0;i < chip->num_slots;i++) | 
 | 264 | 			jmicron_enable_mmc(chip->slots[i]->host, 1); | 
 | 265 | 	} | 
| Pierre Ossman | 45211e2 | 2008-03-24 13:09:09 +0100 | [diff] [blame] | 266 |  | 
 | 267 | 	ret = jmicron_pmos(chip, 1); | 
 | 268 | 	if (ret) { | 
 | 269 | 		dev_err(&chip->pdev->dev, "Failure enabling card power\n"); | 
 | 270 | 		return ret; | 
 | 271 | 	} | 
 | 272 |  | 
 | 273 | 	return 0; | 
 | 274 | } | 
 | 275 |  | 
| Pierre Ossman | 2260640 | 2008-03-23 19:33:23 +0100 | [diff] [blame] | 276 | static const struct sdhci_pci_fixes sdhci_jmicron = { | 
| Pierre Ossman | 45211e2 | 2008-03-24 13:09:09 +0100 | [diff] [blame] | 277 | 	.probe		= jmicron_probe, | 
 | 278 |  | 
| Pierre Ossman | 4489428 | 2008-04-04 19:36:59 +0200 | [diff] [blame] | 279 | 	.probe_slot	= jmicron_probe_slot, | 
 | 280 | 	.remove_slot	= jmicron_remove_slot, | 
 | 281 |  | 
 | 282 | 	.suspend	= jmicron_suspend, | 
| Pierre Ossman | 45211e2 | 2008-03-24 13:09:09 +0100 | [diff] [blame] | 283 | 	.resume		= jmicron_resume, | 
| Pierre Ossman | 2260640 | 2008-03-23 19:33:23 +0100 | [diff] [blame] | 284 | }; | 
 | 285 |  | 
 | 286 | static const struct pci_device_id pci_ids[] __devinitdata = { | 
 | 287 | 	{ | 
 | 288 | 		.vendor		= PCI_VENDOR_ID_RICOH, | 
 | 289 | 		.device		= PCI_DEVICE_ID_RICOH_R5C822, | 
 | 290 | 		.subvendor	= PCI_ANY_ID, | 
 | 291 | 		.subdevice	= PCI_ANY_ID, | 
 | 292 | 		.driver_data	= (kernel_ulong_t)&sdhci_ricoh, | 
 | 293 | 	}, | 
 | 294 |  | 
 | 295 | 	{ | 
 | 296 | 		.vendor		= PCI_VENDOR_ID_ENE, | 
 | 297 | 		.device		= PCI_DEVICE_ID_ENE_CB712_SD, | 
 | 298 | 		.subvendor	= PCI_ANY_ID, | 
 | 299 | 		.subdevice	= PCI_ANY_ID, | 
 | 300 | 		.driver_data	= (kernel_ulong_t)&sdhci_ene_712, | 
 | 301 | 	}, | 
 | 302 |  | 
 | 303 | 	{ | 
 | 304 | 		.vendor		= PCI_VENDOR_ID_ENE, | 
 | 305 | 		.device		= PCI_DEVICE_ID_ENE_CB712_SD_2, | 
 | 306 | 		.subvendor	= PCI_ANY_ID, | 
 | 307 | 		.subdevice	= PCI_ANY_ID, | 
 | 308 | 		.driver_data	= (kernel_ulong_t)&sdhci_ene_712, | 
 | 309 | 	}, | 
 | 310 |  | 
 | 311 | 	{ | 
 | 312 | 		.vendor		= PCI_VENDOR_ID_ENE, | 
 | 313 | 		.device		= PCI_DEVICE_ID_ENE_CB714_SD, | 
 | 314 | 		.subvendor	= PCI_ANY_ID, | 
 | 315 | 		.subdevice	= PCI_ANY_ID, | 
 | 316 | 		.driver_data	= (kernel_ulong_t)&sdhci_ene_714, | 
 | 317 | 	}, | 
 | 318 |  | 
 | 319 | 	{ | 
 | 320 | 		.vendor		= PCI_VENDOR_ID_ENE, | 
 | 321 | 		.device		= PCI_DEVICE_ID_ENE_CB714_SD_2, | 
 | 322 | 		.subvendor	= PCI_ANY_ID, | 
 | 323 | 		.subdevice	= PCI_ANY_ID, | 
 | 324 | 		.driver_data	= (kernel_ulong_t)&sdhci_ene_714, | 
 | 325 | 	}, | 
 | 326 |  | 
 | 327 | 	{ | 
 | 328 | 		.vendor         = PCI_VENDOR_ID_MARVELL, | 
 | 329 | 		.device         = PCI_DEVICE_ID_MARVELL_CAFE_SD, | 
 | 330 | 		.subvendor      = PCI_ANY_ID, | 
 | 331 | 		.subdevice      = PCI_ANY_ID, | 
 | 332 | 		.driver_data    = (kernel_ulong_t)&sdhci_cafe, | 
 | 333 | 	}, | 
 | 334 |  | 
 | 335 | 	{ | 
 | 336 | 		.vendor		= PCI_VENDOR_ID_JMICRON, | 
 | 337 | 		.device		= PCI_DEVICE_ID_JMICRON_JMB38X_SD, | 
 | 338 | 		.subvendor	= PCI_ANY_ID, | 
 | 339 | 		.subdevice	= PCI_ANY_ID, | 
 | 340 | 		.driver_data	= (kernel_ulong_t)&sdhci_jmicron, | 
 | 341 | 	}, | 
 | 342 |  | 
| Pierre Ossman | 4489428 | 2008-04-04 19:36:59 +0200 | [diff] [blame] | 343 | 	{ | 
 | 344 | 		.vendor		= PCI_VENDOR_ID_JMICRON, | 
 | 345 | 		.device		= PCI_DEVICE_ID_JMICRON_JMB38X_MMC, | 
 | 346 | 		.subvendor	= PCI_ANY_ID, | 
 | 347 | 		.subdevice	= PCI_ANY_ID, | 
 | 348 | 		.driver_data	= (kernel_ulong_t)&sdhci_jmicron, | 
 | 349 | 	}, | 
 | 350 |  | 
| Pierre Ossman | 2260640 | 2008-03-23 19:33:23 +0100 | [diff] [blame] | 351 | 	{	/* Generic SD host controller */ | 
 | 352 | 		PCI_DEVICE_CLASS((PCI_CLASS_SYSTEM_SDHCI << 8), 0xFFFF00) | 
 | 353 | 	}, | 
 | 354 |  | 
 | 355 | 	{ /* end: all zeroes */ }, | 
 | 356 | }; | 
 | 357 |  | 
 | 358 | MODULE_DEVICE_TABLE(pci, pci_ids); | 
 | 359 |  | 
| Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 360 | /*****************************************************************************\ | 
 | 361 |  *                                                                           * | 
 | 362 |  * SDHCI core callbacks                                                      * | 
 | 363 |  *                                                                           * | 
 | 364 | \*****************************************************************************/ | 
 | 365 |  | 
 | 366 | static int sdhci_pci_enable_dma(struct sdhci_host *host) | 
 | 367 | { | 
 | 368 | 	struct sdhci_pci_slot *slot; | 
 | 369 | 	struct pci_dev *pdev; | 
 | 370 | 	int ret; | 
 | 371 |  | 
 | 372 | 	slot = sdhci_priv(host); | 
 | 373 | 	pdev = slot->chip->pdev; | 
 | 374 |  | 
 | 375 | 	if (((pdev->class & 0xFFFF00) == (PCI_CLASS_SYSTEM_SDHCI << 8)) && | 
 | 376 | 		((pdev->class & 0x0000FF) != PCI_SDHCI_IFDMA) && | 
 | 377 | 		(host->flags & SDHCI_USE_DMA)) { | 
 | 378 | 		dev_warn(&pdev->dev, "Will use DMA mode even though HW " | 
 | 379 | 			"doesn't fully claim to support it.\n"); | 
 | 380 | 	} | 
 | 381 |  | 
 | 382 | 	ret = pci_set_dma_mask(pdev, DMA_32BIT_MASK); | 
 | 383 | 	if (ret) | 
 | 384 | 		return ret; | 
 | 385 |  | 
 | 386 | 	pci_set_master(pdev); | 
 | 387 |  | 
 | 388 | 	return 0; | 
 | 389 | } | 
 | 390 |  | 
 | 391 | static struct sdhci_ops sdhci_pci_ops = { | 
 | 392 | 	.enable_dma	= sdhci_pci_enable_dma, | 
 | 393 | }; | 
 | 394 |  | 
 | 395 | /*****************************************************************************\ | 
 | 396 |  *                                                                           * | 
 | 397 |  * Suspend/resume                                                            * | 
 | 398 |  *                                                                           * | 
 | 399 | \*****************************************************************************/ | 
 | 400 |  | 
 | 401 | #ifdef CONFIG_PM | 
 | 402 |  | 
 | 403 | static int sdhci_pci_suspend (struct pci_dev *pdev, pm_message_t state) | 
 | 404 | { | 
 | 405 | 	struct sdhci_pci_chip *chip; | 
 | 406 | 	struct sdhci_pci_slot *slot; | 
 | 407 | 	int i, ret; | 
 | 408 |  | 
 | 409 | 	chip = pci_get_drvdata(pdev); | 
 | 410 | 	if (!chip) | 
 | 411 | 		return 0; | 
 | 412 |  | 
 | 413 | 	for (i = 0;i < chip->num_slots;i++) { | 
 | 414 | 		slot = chip->slots[i]; | 
 | 415 | 		if (!slot) | 
 | 416 | 			continue; | 
 | 417 |  | 
 | 418 | 		ret = sdhci_suspend_host(slot->host, state); | 
 | 419 |  | 
 | 420 | 		if (ret) { | 
 | 421 | 			for (i--;i >= 0;i--) | 
 | 422 | 				sdhci_resume_host(chip->slots[i]->host); | 
 | 423 | 			return ret; | 
 | 424 | 		} | 
 | 425 | 	} | 
 | 426 |  | 
| Pierre Ossman | 4489428 | 2008-04-04 19:36:59 +0200 | [diff] [blame] | 427 | 	if (chip->fixes && chip->fixes->suspend) { | 
 | 428 | 		ret = chip->fixes->suspend(chip, state); | 
 | 429 | 		if (ret) { | 
 | 430 | 			for (i = chip->num_slots - 1;i >= 0;i--) | 
 | 431 | 				sdhci_resume_host(chip->slots[i]->host); | 
 | 432 | 			return ret; | 
 | 433 | 		} | 
 | 434 | 	} | 
 | 435 |  | 
| Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 436 | 	pci_save_state(pdev); | 
 | 437 | 	pci_enable_wake(pdev, pci_choose_state(pdev, state), 0); | 
 | 438 | 	pci_disable_device(pdev); | 
 | 439 | 	pci_set_power_state(pdev, pci_choose_state(pdev, state)); | 
 | 440 |  | 
 | 441 | 	return 0; | 
 | 442 | } | 
 | 443 |  | 
 | 444 | static int sdhci_pci_resume (struct pci_dev *pdev) | 
 | 445 | { | 
 | 446 | 	struct sdhci_pci_chip *chip; | 
 | 447 | 	struct sdhci_pci_slot *slot; | 
 | 448 | 	int i, ret; | 
 | 449 |  | 
 | 450 | 	chip = pci_get_drvdata(pdev); | 
 | 451 | 	if (!chip) | 
 | 452 | 		return 0; | 
 | 453 |  | 
 | 454 | 	pci_set_power_state(pdev, PCI_D0); | 
 | 455 | 	pci_restore_state(pdev); | 
 | 456 | 	ret = pci_enable_device(pdev); | 
 | 457 | 	if (ret) | 
 | 458 | 		return ret; | 
 | 459 |  | 
| Pierre Ossman | 45211e2 | 2008-03-24 13:09:09 +0100 | [diff] [blame] | 460 | 	if (chip->fixes && chip->fixes->resume) { | 
 | 461 | 		ret = chip->fixes->resume(chip); | 
 | 462 | 		if (ret) | 
 | 463 | 			return ret; | 
 | 464 | 	} | 
 | 465 |  | 
| Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 466 | 	for (i = 0;i < chip->num_slots;i++) { | 
 | 467 | 		slot = chip->slots[i]; | 
 | 468 | 		if (!slot) | 
 | 469 | 			continue; | 
 | 470 |  | 
 | 471 | 		ret = sdhci_resume_host(slot->host); | 
 | 472 | 		if (ret) | 
 | 473 | 			return ret; | 
 | 474 | 	} | 
 | 475 |  | 
 | 476 | 	return 0; | 
 | 477 | } | 
 | 478 |  | 
 | 479 | #else /* CONFIG_PM */ | 
 | 480 |  | 
 | 481 | #define sdhci_pci_suspend NULL | 
 | 482 | #define sdhci_pci_resume NULL | 
 | 483 |  | 
 | 484 | #endif /* CONFIG_PM */ | 
 | 485 |  | 
 | 486 | /*****************************************************************************\ | 
 | 487 |  *                                                                           * | 
 | 488 |  * Device probing/removal                                                    * | 
 | 489 |  *                                                                           * | 
 | 490 | \*****************************************************************************/ | 
 | 491 |  | 
 | 492 | static struct sdhci_pci_slot * __devinit sdhci_pci_probe_slot( | 
 | 493 | 	struct pci_dev *pdev, struct sdhci_pci_chip *chip, int bar) | 
 | 494 | { | 
 | 495 | 	struct sdhci_pci_slot *slot; | 
 | 496 | 	struct sdhci_host *host; | 
 | 497 |  | 
 | 498 | 	resource_size_t addr; | 
 | 499 |  | 
 | 500 | 	int ret; | 
 | 501 |  | 
 | 502 | 	if (!(pci_resource_flags(pdev, bar) & IORESOURCE_MEM)) { | 
 | 503 | 		dev_err(&pdev->dev, "BAR %d is not iomem. Aborting.\n", bar); | 
 | 504 | 		return ERR_PTR(-ENODEV); | 
 | 505 | 	} | 
 | 506 |  | 
 | 507 | 	if (pci_resource_len(pdev, bar) != 0x100) { | 
 | 508 | 		dev_err(&pdev->dev, "Invalid iomem size. You may " | 
 | 509 | 			"experience problems.\n"); | 
 | 510 | 	} | 
 | 511 |  | 
 | 512 | 	if ((pdev->class & 0x0000FF) == PCI_SDHCI_IFVENDOR) { | 
 | 513 | 		dev_err(&pdev->dev, "Vendor specific interface. Aborting.\n"); | 
 | 514 | 		return ERR_PTR(-ENODEV); | 
 | 515 | 	} | 
 | 516 |  | 
 | 517 | 	if ((pdev->class & 0x0000FF) > PCI_SDHCI_IFVENDOR) { | 
 | 518 | 		dev_err(&pdev->dev, "Unknown interface. Aborting.\n"); | 
 | 519 | 		return ERR_PTR(-ENODEV); | 
 | 520 | 	} | 
 | 521 |  | 
 | 522 | 	host = sdhci_alloc_host(&pdev->dev, sizeof(struct sdhci_pci_slot)); | 
 | 523 | 	if (IS_ERR(host)) { | 
 | 524 | 		ret = PTR_ERR(host); | 
 | 525 | 		goto unmap; | 
 | 526 | 	} | 
 | 527 |  | 
 | 528 | 	slot = sdhci_priv(host); | 
 | 529 |  | 
 | 530 | 	slot->chip = chip; | 
 | 531 | 	slot->host = host; | 
 | 532 | 	slot->pci_bar = bar; | 
 | 533 |  | 
 | 534 | 	host->hw_name = "PCI"; | 
 | 535 | 	host->ops = &sdhci_pci_ops; | 
 | 536 | 	host->quirks = chip->quirks; | 
 | 537 |  | 
 | 538 | 	host->irq = pdev->irq; | 
 | 539 |  | 
 | 540 | 	ret = pci_request_region(pdev, bar, mmc_hostname(host->mmc)); | 
 | 541 | 	if (ret) { | 
 | 542 | 		dev_err(&pdev->dev, "cannot request region\n"); | 
 | 543 | 		return ERR_PTR(ret); | 
 | 544 | 	} | 
 | 545 |  | 
 | 546 | 	addr = pci_resource_start(pdev, bar); | 
 | 547 | 	host->ioaddr = ioremap_nocache(addr, pci_resource_len(pdev, bar)); | 
 | 548 | 	if (!host->ioaddr) { | 
 | 549 | 		dev_err(&pdev->dev, "failed to remap registers\n"); | 
 | 550 | 		goto release; | 
 | 551 | 	} | 
 | 552 |  | 
| Pierre Ossman | 4489428 | 2008-04-04 19:36:59 +0200 | [diff] [blame] | 553 | 	if (chip->fixes && chip->fixes->probe_slot) { | 
 | 554 | 		ret = chip->fixes->probe_slot(slot); | 
 | 555 | 		if (ret) | 
 | 556 | 			goto unmap; | 
 | 557 | 	} | 
 | 558 |  | 
| Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 559 | 	ret = sdhci_add_host(host); | 
 | 560 | 	if (ret) | 
| Pierre Ossman | 4489428 | 2008-04-04 19:36:59 +0200 | [diff] [blame] | 561 | 		goto remove; | 
| Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 562 |  | 
 | 563 | 	return slot; | 
 | 564 |  | 
| Pierre Ossman | 4489428 | 2008-04-04 19:36:59 +0200 | [diff] [blame] | 565 | remove: | 
 | 566 | 	if (chip->fixes && chip->fixes->remove_slot) | 
| Pierre Ossman | 1e72859 | 2008-04-16 19:13:13 +0200 | [diff] [blame] | 567 | 		chip->fixes->remove_slot(slot, 0); | 
| Pierre Ossman | 4489428 | 2008-04-04 19:36:59 +0200 | [diff] [blame] | 568 |  | 
| Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 569 | unmap: | 
 | 570 | 	iounmap(host->ioaddr); | 
 | 571 |  | 
 | 572 | release: | 
 | 573 | 	pci_release_region(pdev, bar); | 
 | 574 | 	sdhci_free_host(host); | 
 | 575 |  | 
 | 576 | 	return ERR_PTR(ret); | 
 | 577 | } | 
 | 578 |  | 
 | 579 | static void sdhci_pci_remove_slot(struct sdhci_pci_slot *slot) | 
 | 580 | { | 
| Pierre Ossman | 1e72859 | 2008-04-16 19:13:13 +0200 | [diff] [blame] | 581 | 	int dead; | 
 | 582 | 	u32 scratch; | 
 | 583 |  | 
 | 584 | 	dead = 0; | 
 | 585 | 	scratch = readl(slot->host->ioaddr + SDHCI_INT_STATUS); | 
 | 586 | 	if (scratch == (u32)-1) | 
 | 587 | 		dead = 1; | 
 | 588 |  | 
 | 589 | 	sdhci_remove_host(slot->host, dead); | 
| Pierre Ossman | 4489428 | 2008-04-04 19:36:59 +0200 | [diff] [blame] | 590 |  | 
 | 591 | 	if (slot->chip->fixes && slot->chip->fixes->remove_slot) | 
| Pierre Ossman | 1e72859 | 2008-04-16 19:13:13 +0200 | [diff] [blame] | 592 | 		slot->chip->fixes->remove_slot(slot, dead); | 
| Pierre Ossman | 4489428 | 2008-04-04 19:36:59 +0200 | [diff] [blame] | 593 |  | 
| Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 594 | 	pci_release_region(slot->chip->pdev, slot->pci_bar); | 
| Pierre Ossman | 4489428 | 2008-04-04 19:36:59 +0200 | [diff] [blame] | 595 |  | 
| Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 596 | 	sdhci_free_host(slot->host); | 
 | 597 | } | 
 | 598 |  | 
 | 599 | static int __devinit sdhci_pci_probe(struct pci_dev *pdev, | 
 | 600 | 				     const struct pci_device_id *ent) | 
 | 601 | { | 
 | 602 | 	struct sdhci_pci_chip *chip; | 
 | 603 | 	struct sdhci_pci_slot *slot; | 
 | 604 |  | 
 | 605 | 	u8 slots, rev, first_bar; | 
 | 606 | 	int ret, i; | 
 | 607 |  | 
 | 608 | 	BUG_ON(pdev == NULL); | 
 | 609 | 	BUG_ON(ent == NULL); | 
 | 610 |  | 
 | 611 | 	pci_read_config_byte(pdev, PCI_CLASS_REVISION, &rev); | 
 | 612 |  | 
 | 613 | 	dev_info(&pdev->dev, "SDHCI controller found [%04x:%04x] (rev %x)\n", | 
 | 614 | 		 (int)pdev->vendor, (int)pdev->device, (int)rev); | 
 | 615 |  | 
 | 616 | 	ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &slots); | 
 | 617 | 	if (ret) | 
 | 618 | 		return ret; | 
 | 619 |  | 
 | 620 | 	slots = PCI_SLOT_INFO_SLOTS(slots) + 1; | 
 | 621 | 	dev_dbg(&pdev->dev, "found %d slot(s)\n", slots); | 
 | 622 | 	if (slots == 0) | 
 | 623 | 		return -ENODEV; | 
 | 624 |  | 
 | 625 | 	BUG_ON(slots > MAX_SLOTS); | 
 | 626 |  | 
 | 627 | 	ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &first_bar); | 
 | 628 | 	if (ret) | 
 | 629 | 		return ret; | 
 | 630 |  | 
 | 631 | 	first_bar &= PCI_SLOT_INFO_FIRST_BAR_MASK; | 
 | 632 |  | 
 | 633 | 	if (first_bar > 5) { | 
 | 634 | 		dev_err(&pdev->dev, "Invalid first BAR. Aborting.\n"); | 
 | 635 | 		return -ENODEV; | 
 | 636 | 	} | 
 | 637 |  | 
 | 638 | 	ret = pci_enable_device(pdev); | 
 | 639 | 	if (ret) | 
 | 640 | 		return ret; | 
 | 641 |  | 
 | 642 | 	chip = kzalloc(sizeof(struct sdhci_pci_chip), GFP_KERNEL); | 
 | 643 | 	if (!chip) { | 
 | 644 | 		ret = -ENOMEM; | 
 | 645 | 		goto err; | 
 | 646 | 	} | 
 | 647 |  | 
 | 648 | 	chip->pdev = pdev; | 
| Pierre Ossman | 2260640 | 2008-03-23 19:33:23 +0100 | [diff] [blame] | 649 | 	chip->fixes = (const struct sdhci_pci_fixes*)ent->driver_data; | 
 | 650 | 	if (chip->fixes) | 
 | 651 | 		chip->quirks = chip->fixes->quirks; | 
| Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 652 | 	chip->num_slots = slots; | 
 | 653 |  | 
 | 654 | 	pci_set_drvdata(pdev, chip); | 
 | 655 |  | 
| Pierre Ossman | 2260640 | 2008-03-23 19:33:23 +0100 | [diff] [blame] | 656 | 	if (chip->fixes && chip->fixes->probe) { | 
 | 657 | 		ret = chip->fixes->probe(chip); | 
 | 658 | 		if (ret) | 
 | 659 | 			goto free; | 
 | 660 | 	} | 
 | 661 |  | 
| Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 662 | 	for (i = 0;i < slots;i++) { | 
 | 663 | 		slot = sdhci_pci_probe_slot(pdev, chip, first_bar + i); | 
 | 664 | 		if (IS_ERR(slot)) { | 
 | 665 | 			for (i--;i >= 0;i--) | 
 | 666 | 				sdhci_pci_remove_slot(chip->slots[i]); | 
 | 667 | 			ret = PTR_ERR(slot); | 
 | 668 | 			goto free; | 
 | 669 | 		} | 
 | 670 |  | 
 | 671 | 		chip->slots[i] = slot; | 
 | 672 | 	} | 
 | 673 |  | 
 | 674 | 	return 0; | 
 | 675 |  | 
 | 676 | free: | 
 | 677 | 	pci_set_drvdata(pdev, NULL); | 
 | 678 | 	kfree(chip); | 
 | 679 |  | 
 | 680 | err: | 
 | 681 | 	pci_disable_device(pdev); | 
 | 682 | 	return ret; | 
 | 683 | } | 
 | 684 |  | 
 | 685 | static void __devexit sdhci_pci_remove(struct pci_dev *pdev) | 
 | 686 | { | 
 | 687 | 	int i; | 
 | 688 | 	struct sdhci_pci_chip *chip; | 
 | 689 |  | 
 | 690 | 	chip = pci_get_drvdata(pdev); | 
 | 691 |  | 
 | 692 | 	if (chip) { | 
 | 693 | 		for (i = 0;i < chip->num_slots; i++) | 
 | 694 | 			sdhci_pci_remove_slot(chip->slots[i]); | 
 | 695 |  | 
 | 696 | 		pci_set_drvdata(pdev, NULL); | 
 | 697 | 		kfree(chip); | 
 | 698 | 	} | 
 | 699 |  | 
 | 700 | 	pci_disable_device(pdev); | 
 | 701 | } | 
 | 702 |  | 
 | 703 | static struct pci_driver sdhci_driver = { | 
 | 704 | 	.name = 	"sdhci-pci", | 
 | 705 | 	.id_table =	pci_ids, | 
 | 706 | 	.probe = 	sdhci_pci_probe, | 
 | 707 | 	.remove =	__devexit_p(sdhci_pci_remove), | 
 | 708 | 	.suspend =	sdhci_pci_suspend, | 
 | 709 | 	.resume	=	sdhci_pci_resume, | 
 | 710 | }; | 
 | 711 |  | 
 | 712 | /*****************************************************************************\ | 
 | 713 |  *                                                                           * | 
 | 714 |  * Driver init/exit                                                          * | 
 | 715 |  *                                                                           * | 
 | 716 | \*****************************************************************************/ | 
 | 717 |  | 
 | 718 | static int __init sdhci_drv_init(void) | 
 | 719 | { | 
 | 720 | 	return pci_register_driver(&sdhci_driver); | 
 | 721 | } | 
 | 722 |  | 
 | 723 | static void __exit sdhci_drv_exit(void) | 
 | 724 | { | 
 | 725 | 	pci_unregister_driver(&sdhci_driver); | 
 | 726 | } | 
 | 727 |  | 
 | 728 | module_init(sdhci_drv_init); | 
 | 729 | module_exit(sdhci_drv_exit); | 
 | 730 |  | 
 | 731 | MODULE_AUTHOR("Pierre Ossman <drzeus@drzeus.cx>"); | 
 | 732 | MODULE_DESCRIPTION("Secure Digital Host Controller Interface PCI driver"); | 
 | 733 | MODULE_LICENSE("GPL"); |