blob: 0f008b3f00750cef7917119698bac7523ceb8e43 [file] [log] [blame]
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001/* 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>
Paul Gortmakera4df3ae2011-07-03 15:15:51 -040017#include <linux/module.h>
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010018#include <linux/pci.h>
19#include <linux/dma-mapping.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Maxim Levitskyccc92c22010-08-10 18:01:42 -070021#include <linux/device.h>
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010022#include <linux/mmc/host.h>
Ameya Palandeb177bc92011-04-05 21:13:13 +030023#include <linux/scatterlist.h>
24#include <linux/io.h>
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010025
26#include "sdhci.h"
27
28/*
29 * PCI registers
30 */
31
32#define PCI_SDHCI_IFPIO 0x00
33#define PCI_SDHCI_IFDMA 0x01
34#define PCI_SDHCI_IFVENDOR 0x02
35
36#define PCI_SLOT_INFO 0x40 /* 8 bits */
37#define PCI_SLOT_INFO_SLOTS(x) ((x >> 4) & 7)
38#define PCI_SLOT_INFO_FIRST_BAR_MASK 0x07
39
40#define MAX_SLOTS 8
41
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010042struct sdhci_pci_chip;
Pierre Ossman44894282008-04-04 19:36:59 +020043struct sdhci_pci_slot;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010044
Pierre Ossman22606402008-03-23 19:33:23 +010045struct sdhci_pci_fixes {
46 unsigned int quirks;
47
Ameya Palandeb177bc92011-04-05 21:13:13 +030048 int (*probe) (struct sdhci_pci_chip *);
Pierre Ossman45211e22008-03-24 13:09:09 +010049
Ameya Palandeb177bc92011-04-05 21:13:13 +030050 int (*probe_slot) (struct sdhci_pci_slot *);
51 void (*remove_slot) (struct sdhci_pci_slot *, int);
Pierre Ossman44894282008-04-04 19:36:59 +020052
Ameya Palandeb177bc92011-04-05 21:13:13 +030053 int (*suspend) (struct sdhci_pci_chip *,
Pierre Ossman44894282008-04-04 19:36:59 +020054 pm_message_t);
Ameya Palandeb177bc92011-04-05 21:13:13 +030055 int (*resume) (struct sdhci_pci_chip *);
Pierre Ossman22606402008-03-23 19:33:23 +010056};
57
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010058struct sdhci_pci_slot {
59 struct sdhci_pci_chip *chip;
60 struct sdhci_host *host;
61
62 int pci_bar;
63};
64
65struct sdhci_pci_chip {
66 struct pci_dev *pdev;
Pierre Ossman22606402008-03-23 19:33:23 +010067
68 unsigned int quirks;
69 const struct sdhci_pci_fixes *fixes;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010070
71 int num_slots; /* Slots on controller */
72 struct sdhci_pci_slot *slots[MAX_SLOTS]; /* Pointers to host slots */
73};
74
Pierre Ossman22606402008-03-23 19:33:23 +010075
76/*****************************************************************************\
77 * *
78 * Hardware specific quirk handling *
79 * *
80\*****************************************************************************/
81
82static int ricoh_probe(struct sdhci_pci_chip *chip)
83{
Chris Ballc99436f2009-09-22 16:45:22 -070084 if (chip->pdev->subsystem_vendor == PCI_VENDOR_ID_SAMSUNG ||
85 chip->pdev->subsystem_vendor == PCI_VENDOR_ID_SONY)
Pierre Ossman22606402008-03-23 19:33:23 +010086 chip->quirks |= SDHCI_QUIRK_NO_CARD_NO_RESET;
Maxim Levitskyccc92c22010-08-10 18:01:42 -070087 return 0;
88}
Pierre Ossman22606402008-03-23 19:33:23 +010089
Maxim Levitskyccc92c22010-08-10 18:01:42 -070090static int ricoh_mmc_probe_slot(struct sdhci_pci_slot *slot)
91{
92 slot->host->caps =
93 ((0x21 << SDHCI_TIMEOUT_CLK_SHIFT)
94 & SDHCI_TIMEOUT_CLK_MASK) |
95
96 ((0x21 << SDHCI_CLOCK_BASE_SHIFT)
97 & SDHCI_CLOCK_BASE_MASK) |
98
99 SDHCI_TIMEOUT_CLK_UNIT |
100 SDHCI_CAN_VDD_330 |
101 SDHCI_CAN_DO_SDMA;
102 return 0;
103}
104
105static int ricoh_mmc_resume(struct sdhci_pci_chip *chip)
106{
107 /* Apply a delay to allow controller to settle */
108 /* Otherwise it becomes confused if card state changed
109 during suspend */
110 msleep(500);
Pierre Ossman22606402008-03-23 19:33:23 +0100111 return 0;
112}
113
114static const struct sdhci_pci_fixes sdhci_ricoh = {
115 .probe = ricoh_probe,
Vasily Khoruzhick84938292010-03-05 13:43:46 -0800116 .quirks = SDHCI_QUIRK_32BIT_DMA_ADDR |
117 SDHCI_QUIRK_FORCE_DMA |
118 SDHCI_QUIRK_CLOCK_BEFORE_RESET,
Pierre Ossman22606402008-03-23 19:33:23 +0100119};
120
Maxim Levitskyccc92c22010-08-10 18:01:42 -0700121static const struct sdhci_pci_fixes sdhci_ricoh_mmc = {
122 .probe_slot = ricoh_mmc_probe_slot,
123 .resume = ricoh_mmc_resume,
124 .quirks = SDHCI_QUIRK_32BIT_DMA_ADDR |
125 SDHCI_QUIRK_CLOCK_BEFORE_RESET |
126 SDHCI_QUIRK_NO_CARD_NO_RESET |
127 SDHCI_QUIRK_MISSING_CAPS
128};
129
Pierre Ossman22606402008-03-23 19:33:23 +0100130static const struct sdhci_pci_fixes sdhci_ene_712 = {
131 .quirks = SDHCI_QUIRK_SINGLE_POWER_WRITE |
132 SDHCI_QUIRK_BROKEN_DMA,
133};
134
135static const struct sdhci_pci_fixes sdhci_ene_714 = {
136 .quirks = SDHCI_QUIRK_SINGLE_POWER_WRITE |
137 SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS |
138 SDHCI_QUIRK_BROKEN_DMA,
139};
140
141static const struct sdhci_pci_fixes sdhci_cafe = {
142 .quirks = SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER |
Andres Salomona0874892009-03-02 21:48:20 +0100143 SDHCI_QUIRK_NO_BUSY_IRQ |
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200144 SDHCI_QUIRK_BROKEN_TIMEOUT_VAL,
Pierre Ossman22606402008-03-23 19:33:23 +0100145};
146
Alan Coxf9ee3ea2010-10-04 15:25:11 +0100147/*
148 * ADMA operation is disabled for Moorestown platform due to
149 * hardware bugs.
150 */
Jacob Pan35ac6f02010-11-09 13:57:29 +0000151static int mrst_hc_probe(struct sdhci_pci_chip *chip)
Alan Coxf9ee3ea2010-10-04 15:25:11 +0100152{
153 /*
Jacob Pan35ac6f02010-11-09 13:57:29 +0000154 * slots number is fixed here for MRST as SDIO3/5 are never used and
155 * have hardware bugs.
Alan Coxf9ee3ea2010-10-04 15:25:11 +0100156 */
157 chip->num_slots = 1;
158 return 0;
159}
160
161static const struct sdhci_pci_fixes sdhci_intel_mrst_hc0 = {
162 .quirks = SDHCI_QUIRK_BROKEN_ADMA | SDHCI_QUIRK_NO_HISPD_BIT,
163};
164
Jacob Pan35ac6f02010-11-09 13:57:29 +0000165static const struct sdhci_pci_fixes sdhci_intel_mrst_hc1_hc2 = {
Alan Coxf9ee3ea2010-10-04 15:25:11 +0100166 .quirks = SDHCI_QUIRK_BROKEN_ADMA | SDHCI_QUIRK_NO_HISPD_BIT,
Jacob Pan35ac6f02010-11-09 13:57:29 +0000167 .probe = mrst_hc_probe,
Alan Coxf9ee3ea2010-10-04 15:25:11 +0100168};
169
Xiaochen Shen29229052010-10-04 15:24:52 +0100170static const struct sdhci_pci_fixes sdhci_intel_mfd_sd = {
171 .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
172};
173
174static const struct sdhci_pci_fixes sdhci_intel_mfd_emmc_sdio = {
175 .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
176};
177
Jennifer Li26daa1e2010-11-17 23:01:59 -0500178/* O2Micro extra registers */
179#define O2_SD_LOCK_WP 0xD3
180#define O2_SD_MULTI_VCC3V 0xEE
181#define O2_SD_CLKREQ 0xEC
182#define O2_SD_CAPS 0xE0
183#define O2_SD_ADMA1 0xE2
184#define O2_SD_ADMA2 0xE7
185#define O2_SD_INF_MOD 0xF1
186
187static int o2_probe(struct sdhci_pci_chip *chip)
188{
189 int ret;
190 u8 scratch;
191
192 switch (chip->pdev->device) {
193 case PCI_DEVICE_ID_O2_8220:
194 case PCI_DEVICE_ID_O2_8221:
195 case PCI_DEVICE_ID_O2_8320:
196 case PCI_DEVICE_ID_O2_8321:
197 /* This extra setup is required due to broken ADMA. */
198 ret = pci_read_config_byte(chip->pdev, O2_SD_LOCK_WP, &scratch);
199 if (ret)
200 return ret;
201 scratch &= 0x7f;
202 pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch);
203
204 /* Set Multi 3 to VCC3V# */
205 pci_write_config_byte(chip->pdev, O2_SD_MULTI_VCC3V, 0x08);
206
207 /* Disable CLK_REQ# support after media DET */
208 ret = pci_read_config_byte(chip->pdev, O2_SD_CLKREQ, &scratch);
209 if (ret)
210 return ret;
211 scratch |= 0x20;
212 pci_write_config_byte(chip->pdev, O2_SD_CLKREQ, scratch);
213
214 /* Choose capabilities, enable SDMA. We have to write 0x01
215 * to the capabilities register first to unlock it.
216 */
217 ret = pci_read_config_byte(chip->pdev, O2_SD_CAPS, &scratch);
218 if (ret)
219 return ret;
220 scratch |= 0x01;
221 pci_write_config_byte(chip->pdev, O2_SD_CAPS, scratch);
222 pci_write_config_byte(chip->pdev, O2_SD_CAPS, 0x73);
223
224 /* Disable ADMA1/2 */
225 pci_write_config_byte(chip->pdev, O2_SD_ADMA1, 0x39);
226 pci_write_config_byte(chip->pdev, O2_SD_ADMA2, 0x08);
227
228 /* Disable the infinite transfer mode */
229 ret = pci_read_config_byte(chip->pdev, O2_SD_INF_MOD, &scratch);
230 if (ret)
231 return ret;
232 scratch |= 0x08;
233 pci_write_config_byte(chip->pdev, O2_SD_INF_MOD, scratch);
234
235 /* Lock WP */
236 ret = pci_read_config_byte(chip->pdev, O2_SD_LOCK_WP, &scratch);
237 if (ret)
238 return ret;
239 scratch |= 0x80;
240 pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch);
241 }
242
Adrian Hunter48ec4d12012-03-24 23:09:18 +0530243 slot->host->mmc->caps2 = MMC_CAP2_BOOTPART_NOACC;
244
Jennifer Li26daa1e2010-11-17 23:01:59 -0500245 return 0;
246}
247
Pierre Ossman45211e22008-03-24 13:09:09 +0100248static int jmicron_pmos(struct sdhci_pci_chip *chip, int on)
249{
250 u8 scratch;
251 int ret;
252
253 ret = pci_read_config_byte(chip->pdev, 0xAE, &scratch);
254 if (ret)
255 return ret;
256
257 /*
258 * Turn PMOS on [bit 0], set over current detection to 2.4 V
259 * [bit 1:2] and enable over current debouncing [bit 6].
260 */
261 if (on)
262 scratch |= 0x47;
263 else
264 scratch &= ~0x47;
265
266 ret = pci_write_config_byte(chip->pdev, 0xAE, scratch);
267 if (ret)
268 return ret;
269
270 return 0;
271}
272
273static int jmicron_probe(struct sdhci_pci_chip *chip)
274{
275 int ret;
Takashi Iwai8f230f42010-12-08 10:04:30 +0100276 u16 mmcdev = 0;
Pierre Ossman45211e22008-03-24 13:09:09 +0100277
Pierre Ossman93fc48c2008-06-28 18:21:41 +0200278 if (chip->pdev->revision == 0) {
279 chip->quirks |= SDHCI_QUIRK_32BIT_DMA_ADDR |
280 SDHCI_QUIRK_32BIT_DMA_SIZE |
Pierre Ossman2134a922008-06-28 18:28:51 +0200281 SDHCI_QUIRK_32BIT_ADMA_SIZE |
Pierre Ossman4a3cba32008-07-29 00:11:16 +0200282 SDHCI_QUIRK_RESET_AFTER_REQUEST |
Pierre Ossman86a6a872009-02-02 21:13:49 +0100283 SDHCI_QUIRK_BROKEN_SMALL_PIO;
Pierre Ossman93fc48c2008-06-28 18:21:41 +0200284 }
285
Pierre Ossman45211e22008-03-24 13:09:09 +0100286 /*
Pierre Ossman44894282008-04-04 19:36:59 +0200287 * JMicron chips can have two interfaces to the same hardware
288 * in order to work around limitations in Microsoft's driver.
289 * We need to make sure we only bind to one of them.
290 *
291 * This code assumes two things:
292 *
293 * 1. The PCI code adds subfunctions in order.
294 *
295 * 2. The MMC interface has a lower subfunction number
296 * than the SD interface.
297 */
Takashi Iwai8f230f42010-12-08 10:04:30 +0100298 if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_SD)
299 mmcdev = PCI_DEVICE_ID_JMICRON_JMB38X_MMC;
300 else if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_SD)
301 mmcdev = PCI_DEVICE_ID_JMICRON_JMB388_ESD;
302
303 if (mmcdev) {
Pierre Ossman44894282008-04-04 19:36:59 +0200304 struct pci_dev *sd_dev;
305
306 sd_dev = NULL;
307 while ((sd_dev = pci_get_device(PCI_VENDOR_ID_JMICRON,
Takashi Iwai8f230f42010-12-08 10:04:30 +0100308 mmcdev, sd_dev)) != NULL) {
Pierre Ossman44894282008-04-04 19:36:59 +0200309 if ((PCI_SLOT(chip->pdev->devfn) ==
310 PCI_SLOT(sd_dev->devfn)) &&
311 (chip->pdev->bus == sd_dev->bus))
312 break;
313 }
314
315 if (sd_dev) {
316 pci_dev_put(sd_dev);
317 dev_info(&chip->pdev->dev, "Refusing to bind to "
318 "secondary interface.\n");
319 return -ENODEV;
320 }
321 }
322
323 /*
Pierre Ossman45211e22008-03-24 13:09:09 +0100324 * JMicron chips need a bit of a nudge to enable the power
325 * output pins.
326 */
327 ret = jmicron_pmos(chip, 1);
328 if (ret) {
329 dev_err(&chip->pdev->dev, "Failure enabling card power\n");
330 return ret;
331 }
332
Takashi Iwai82b0e232011-04-21 20:26:38 +0200333 /* quirk for unsable RO-detection on JM388 chips */
334 if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_SD ||
335 chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD)
336 chip->quirks |= SDHCI_QUIRK_UNSTABLE_RO_DETECT;
337
Pierre Ossman45211e22008-03-24 13:09:09 +0100338 return 0;
339}
340
Pierre Ossman44894282008-04-04 19:36:59 +0200341static void jmicron_enable_mmc(struct sdhci_host *host, int on)
342{
343 u8 scratch;
344
345 scratch = readb(host->ioaddr + 0xC0);
346
347 if (on)
348 scratch |= 0x01;
349 else
350 scratch &= ~0x01;
351
352 writeb(scratch, host->ioaddr + 0xC0);
353}
354
355static int jmicron_probe_slot(struct sdhci_pci_slot *slot)
356{
Pierre Ossman2134a922008-06-28 18:28:51 +0200357 if (slot->chip->pdev->revision == 0) {
358 u16 version;
359
360 version = readl(slot->host->ioaddr + SDHCI_HOST_VERSION);
361 version = (version & SDHCI_VENDOR_VER_MASK) >>
362 SDHCI_VENDOR_VER_SHIFT;
363
364 /*
365 * Older versions of the chip have lots of nasty glitches
366 * in the ADMA engine. It's best just to avoid it
367 * completely.
368 */
369 if (version < 0xAC)
370 slot->host->quirks |= SDHCI_QUIRK_BROKEN_ADMA;
371 }
372
Takashi Iwai8f230f42010-12-08 10:04:30 +0100373 /* JM388 MMC doesn't support 1.8V while SD supports it */
374 if (slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD) {
375 slot->host->ocr_avail_sd = MMC_VDD_32_33 | MMC_VDD_33_34 |
376 MMC_VDD_29_30 | MMC_VDD_30_31 |
377 MMC_VDD_165_195; /* allow 1.8V */
378 slot->host->ocr_avail_mmc = MMC_VDD_32_33 | MMC_VDD_33_34 |
379 MMC_VDD_29_30 | MMC_VDD_30_31; /* no 1.8V for MMC */
380 }
381
Pierre Ossman44894282008-04-04 19:36:59 +0200382 /*
383 * The secondary interface requires a bit set to get the
384 * interrupts.
385 */
Takashi Iwai8f230f42010-12-08 10:04:30 +0100386 if (slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC ||
387 slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD)
Pierre Ossman44894282008-04-04 19:36:59 +0200388 jmicron_enable_mmc(slot->host, 1);
389
Takashi Iwaid75c1082010-12-16 17:54:14 +0100390 slot->host->mmc->caps |= MMC_CAP_BUS_WIDTH_TEST;
391
Pierre Ossman44894282008-04-04 19:36:59 +0200392 return 0;
393}
394
Pierre Ossman1e728592008-04-16 19:13:13 +0200395static void jmicron_remove_slot(struct sdhci_pci_slot *slot, int dead)
Pierre Ossman44894282008-04-04 19:36:59 +0200396{
Pierre Ossman1e728592008-04-16 19:13:13 +0200397 if (dead)
398 return;
399
Takashi Iwai8f230f42010-12-08 10:04:30 +0100400 if (slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC ||
401 slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD)
Pierre Ossman44894282008-04-04 19:36:59 +0200402 jmicron_enable_mmc(slot->host, 0);
403}
404
405static int jmicron_suspend(struct sdhci_pci_chip *chip, pm_message_t state)
406{
407 int i;
408
Takashi Iwai8f230f42010-12-08 10:04:30 +0100409 if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC ||
410 chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD) {
Ameya Palandeb177bc92011-04-05 21:13:13 +0300411 for (i = 0; i < chip->num_slots; i++)
Pierre Ossman44894282008-04-04 19:36:59 +0200412 jmicron_enable_mmc(chip->slots[i]->host, 0);
413 }
414
415 return 0;
416}
417
Pierre Ossman45211e22008-03-24 13:09:09 +0100418static int jmicron_resume(struct sdhci_pci_chip *chip)
419{
Pierre Ossman44894282008-04-04 19:36:59 +0200420 int ret, i;
421
Takashi Iwai8f230f42010-12-08 10:04:30 +0100422 if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC ||
423 chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD) {
Ameya Palandeb177bc92011-04-05 21:13:13 +0300424 for (i = 0; i < chip->num_slots; i++)
Pierre Ossman44894282008-04-04 19:36:59 +0200425 jmicron_enable_mmc(chip->slots[i]->host, 1);
426 }
Pierre Ossman45211e22008-03-24 13:09:09 +0100427
428 ret = jmicron_pmos(chip, 1);
429 if (ret) {
430 dev_err(&chip->pdev->dev, "Failure enabling card power\n");
431 return ret;
432 }
433
434 return 0;
435}
436
Jennifer Li26daa1e2010-11-17 23:01:59 -0500437static const struct sdhci_pci_fixes sdhci_o2 = {
438 .probe = o2_probe,
439};
440
Pierre Ossman22606402008-03-23 19:33:23 +0100441static const struct sdhci_pci_fixes sdhci_jmicron = {
Pierre Ossman45211e22008-03-24 13:09:09 +0100442 .probe = jmicron_probe,
443
Pierre Ossman44894282008-04-04 19:36:59 +0200444 .probe_slot = jmicron_probe_slot,
445 .remove_slot = jmicron_remove_slot,
446
447 .suspend = jmicron_suspend,
Pierre Ossman45211e22008-03-24 13:09:09 +0100448 .resume = jmicron_resume,
Pierre Ossman22606402008-03-23 19:33:23 +0100449};
450
Nicolas Pitrea7a61862009-12-14 18:01:26 -0800451/* SysKonnect CardBus2SDIO extra registers */
452#define SYSKT_CTRL 0x200
453#define SYSKT_RDFIFO_STAT 0x204
454#define SYSKT_WRFIFO_STAT 0x208
455#define SYSKT_POWER_DATA 0x20c
456#define SYSKT_POWER_330 0xef
457#define SYSKT_POWER_300 0xf8
458#define SYSKT_POWER_184 0xcc
459#define SYSKT_POWER_CMD 0x20d
460#define SYSKT_POWER_START (1 << 7)
461#define SYSKT_POWER_STATUS 0x20e
462#define SYSKT_POWER_STATUS_OK (1 << 0)
463#define SYSKT_BOARD_REV 0x210
464#define SYSKT_CHIP_REV 0x211
465#define SYSKT_CONF_DATA 0x212
466#define SYSKT_CONF_DATA_1V8 (1 << 2)
467#define SYSKT_CONF_DATA_2V5 (1 << 1)
468#define SYSKT_CONF_DATA_3V3 (1 << 0)
469
470static int syskt_probe(struct sdhci_pci_chip *chip)
471{
472 if ((chip->pdev->class & 0x0000FF) == PCI_SDHCI_IFVENDOR) {
473 chip->pdev->class &= ~0x0000FF;
474 chip->pdev->class |= PCI_SDHCI_IFDMA;
475 }
476 return 0;
477}
478
479static int syskt_probe_slot(struct sdhci_pci_slot *slot)
480{
481 int tm, ps;
482
483 u8 board_rev = readb(slot->host->ioaddr + SYSKT_BOARD_REV);
484 u8 chip_rev = readb(slot->host->ioaddr + SYSKT_CHIP_REV);
485 dev_info(&slot->chip->pdev->dev, "SysKonnect CardBus2SDIO, "
486 "board rev %d.%d, chip rev %d.%d\n",
487 board_rev >> 4, board_rev & 0xf,
488 chip_rev >> 4, chip_rev & 0xf);
489 if (chip_rev >= 0x20)
490 slot->host->quirks |= SDHCI_QUIRK_FORCE_DMA;
491
492 writeb(SYSKT_POWER_330, slot->host->ioaddr + SYSKT_POWER_DATA);
493 writeb(SYSKT_POWER_START, slot->host->ioaddr + SYSKT_POWER_CMD);
494 udelay(50);
495 tm = 10; /* Wait max 1 ms */
496 do {
497 ps = readw(slot->host->ioaddr + SYSKT_POWER_STATUS);
498 if (ps & SYSKT_POWER_STATUS_OK)
499 break;
500 udelay(100);
501 } while (--tm);
502 if (!tm) {
503 dev_err(&slot->chip->pdev->dev,
504 "power regulator never stabilized");
505 writeb(0, slot->host->ioaddr + SYSKT_POWER_CMD);
506 return -ENODEV;
507 }
508
509 return 0;
510}
511
512static const struct sdhci_pci_fixes sdhci_syskt = {
513 .quirks = SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER,
514 .probe = syskt_probe,
515 .probe_slot = syskt_probe_slot,
516};
517
Harald Welte557b0692009-06-18 16:53:38 +0200518static int via_probe(struct sdhci_pci_chip *chip)
519{
520 if (chip->pdev->revision == 0x10)
521 chip->quirks |= SDHCI_QUIRK_DELAY_AFTER_POWER;
522
523 return 0;
524}
525
526static const struct sdhci_pci_fixes sdhci_via = {
527 .probe = via_probe,
528};
529
Pierre Ossman22606402008-03-23 19:33:23 +0100530static const struct pci_device_id pci_ids[] __devinitdata = {
531 {
532 .vendor = PCI_VENDOR_ID_RICOH,
533 .device = PCI_DEVICE_ID_RICOH_R5C822,
534 .subvendor = PCI_ANY_ID,
535 .subdevice = PCI_ANY_ID,
536 .driver_data = (kernel_ulong_t)&sdhci_ricoh,
537 },
538
539 {
Maxim Levitskyccc92c22010-08-10 18:01:42 -0700540 .vendor = PCI_VENDOR_ID_RICOH,
541 .device = 0x843,
542 .subvendor = PCI_ANY_ID,
543 .subdevice = PCI_ANY_ID,
544 .driver_data = (kernel_ulong_t)&sdhci_ricoh_mmc,
545 },
546
547 {
Pablo Castillo568133e2010-08-10 18:02:01 -0700548 .vendor = PCI_VENDOR_ID_RICOH,
549 .device = 0xe822,
550 .subvendor = PCI_ANY_ID,
551 .subdevice = PCI_ANY_ID,
552 .driver_data = (kernel_ulong_t)&sdhci_ricoh_mmc,
553 },
554
555 {
Manoj Iyer5fd11c02011-02-11 16:25:31 -0600556 .vendor = PCI_VENDOR_ID_RICOH,
557 .device = 0xe823,
558 .subvendor = PCI_ANY_ID,
559 .subdevice = PCI_ANY_ID,
560 .driver_data = (kernel_ulong_t)&sdhci_ricoh_mmc,
561 },
562
563 {
Pierre Ossman22606402008-03-23 19:33:23 +0100564 .vendor = PCI_VENDOR_ID_ENE,
565 .device = PCI_DEVICE_ID_ENE_CB712_SD,
566 .subvendor = PCI_ANY_ID,
567 .subdevice = PCI_ANY_ID,
568 .driver_data = (kernel_ulong_t)&sdhci_ene_712,
569 },
570
571 {
572 .vendor = PCI_VENDOR_ID_ENE,
573 .device = PCI_DEVICE_ID_ENE_CB712_SD_2,
574 .subvendor = PCI_ANY_ID,
575 .subdevice = PCI_ANY_ID,
576 .driver_data = (kernel_ulong_t)&sdhci_ene_712,
577 },
578
579 {
580 .vendor = PCI_VENDOR_ID_ENE,
581 .device = PCI_DEVICE_ID_ENE_CB714_SD,
582 .subvendor = PCI_ANY_ID,
583 .subdevice = PCI_ANY_ID,
584 .driver_data = (kernel_ulong_t)&sdhci_ene_714,
585 },
586
587 {
588 .vendor = PCI_VENDOR_ID_ENE,
589 .device = PCI_DEVICE_ID_ENE_CB714_SD_2,
590 .subvendor = PCI_ANY_ID,
591 .subdevice = PCI_ANY_ID,
592 .driver_data = (kernel_ulong_t)&sdhci_ene_714,
593 },
594
595 {
596 .vendor = PCI_VENDOR_ID_MARVELL,
David Woodhouse8c5eb882008-09-03 09:45:57 +0100597 .device = PCI_DEVICE_ID_MARVELL_88ALP01_SD,
Pierre Ossman22606402008-03-23 19:33:23 +0100598 .subvendor = PCI_ANY_ID,
599 .subdevice = PCI_ANY_ID,
600 .driver_data = (kernel_ulong_t)&sdhci_cafe,
601 },
602
603 {
604 .vendor = PCI_VENDOR_ID_JMICRON,
605 .device = PCI_DEVICE_ID_JMICRON_JMB38X_SD,
606 .subvendor = PCI_ANY_ID,
607 .subdevice = PCI_ANY_ID,
608 .driver_data = (kernel_ulong_t)&sdhci_jmicron,
609 },
610
Pierre Ossman44894282008-04-04 19:36:59 +0200611 {
612 .vendor = PCI_VENDOR_ID_JMICRON,
613 .device = PCI_DEVICE_ID_JMICRON_JMB38X_MMC,
614 .subvendor = PCI_ANY_ID,
615 .subdevice = PCI_ANY_ID,
616 .driver_data = (kernel_ulong_t)&sdhci_jmicron,
617 },
618
Harald Welte557b0692009-06-18 16:53:38 +0200619 {
Takashi Iwai8f230f42010-12-08 10:04:30 +0100620 .vendor = PCI_VENDOR_ID_JMICRON,
621 .device = PCI_DEVICE_ID_JMICRON_JMB388_SD,
622 .subvendor = PCI_ANY_ID,
623 .subdevice = PCI_ANY_ID,
624 .driver_data = (kernel_ulong_t)&sdhci_jmicron,
625 },
626
627 {
628 .vendor = PCI_VENDOR_ID_JMICRON,
629 .device = PCI_DEVICE_ID_JMICRON_JMB388_ESD,
630 .subvendor = PCI_ANY_ID,
631 .subdevice = PCI_ANY_ID,
632 .driver_data = (kernel_ulong_t)&sdhci_jmicron,
633 },
634
635 {
Nicolas Pitrea7a61862009-12-14 18:01:26 -0800636 .vendor = PCI_VENDOR_ID_SYSKONNECT,
637 .device = 0x8000,
638 .subvendor = PCI_ANY_ID,
639 .subdevice = PCI_ANY_ID,
640 .driver_data = (kernel_ulong_t)&sdhci_syskt,
641 },
642
643 {
Harald Welte557b0692009-06-18 16:53:38 +0200644 .vendor = PCI_VENDOR_ID_VIA,
645 .device = 0x95d0,
646 .subvendor = PCI_ANY_ID,
647 .subdevice = PCI_ANY_ID,
648 .driver_data = (kernel_ulong_t)&sdhci_via,
649 },
650
Xiaochen Shen29229052010-10-04 15:24:52 +0100651 {
652 .vendor = PCI_VENDOR_ID_INTEL,
Alan Coxf9ee3ea2010-10-04 15:25:11 +0100653 .device = PCI_DEVICE_ID_INTEL_MRST_SD0,
654 .subvendor = PCI_ANY_ID,
655 .subdevice = PCI_ANY_ID,
656 .driver_data = (kernel_ulong_t)&sdhci_intel_mrst_hc0,
657 },
658
659 {
660 .vendor = PCI_VENDOR_ID_INTEL,
661 .device = PCI_DEVICE_ID_INTEL_MRST_SD1,
662 .subvendor = PCI_ANY_ID,
663 .subdevice = PCI_ANY_ID,
Jacob Pan35ac6f02010-11-09 13:57:29 +0000664 .driver_data = (kernel_ulong_t)&sdhci_intel_mrst_hc1_hc2,
665 },
666
667 {
668 .vendor = PCI_VENDOR_ID_INTEL,
669 .device = PCI_DEVICE_ID_INTEL_MRST_SD2,
670 .subvendor = PCI_ANY_ID,
671 .subdevice = PCI_ANY_ID,
672 .driver_data = (kernel_ulong_t)&sdhci_intel_mrst_hc1_hc2,
Alan Coxf9ee3ea2010-10-04 15:25:11 +0100673 },
674
675 {
676 .vendor = PCI_VENDOR_ID_INTEL,
Xiaochen Shen29229052010-10-04 15:24:52 +0100677 .device = PCI_DEVICE_ID_INTEL_MFD_SD,
678 .subvendor = PCI_ANY_ID,
679 .subdevice = PCI_ANY_ID,
680 .driver_data = (kernel_ulong_t)&sdhci_intel_mfd_sd,
681 },
682
683 {
684 .vendor = PCI_VENDOR_ID_INTEL,
685 .device = PCI_DEVICE_ID_INTEL_MFD_SDIO1,
686 .subvendor = PCI_ANY_ID,
687 .subdevice = PCI_ANY_ID,
688 .driver_data = (kernel_ulong_t)&sdhci_intel_mfd_emmc_sdio,
689 },
690
691 {
692 .vendor = PCI_VENDOR_ID_INTEL,
693 .device = PCI_DEVICE_ID_INTEL_MFD_SDIO2,
694 .subvendor = PCI_ANY_ID,
695 .subdevice = PCI_ANY_ID,
696 .driver_data = (kernel_ulong_t)&sdhci_intel_mfd_emmc_sdio,
697 },
698
699 {
700 .vendor = PCI_VENDOR_ID_INTEL,
701 .device = PCI_DEVICE_ID_INTEL_MFD_EMMC0,
702 .subvendor = PCI_ANY_ID,
703 .subdevice = PCI_ANY_ID,
704 .driver_data = (kernel_ulong_t)&sdhci_intel_mfd_emmc_sdio,
705 },
706
707 {
708 .vendor = PCI_VENDOR_ID_INTEL,
709 .device = PCI_DEVICE_ID_INTEL_MFD_EMMC1,
710 .subvendor = PCI_ANY_ID,
711 .subdevice = PCI_ANY_ID,
712 .driver_data = (kernel_ulong_t)&sdhci_intel_mfd_emmc_sdio,
713 },
714
Jennifer Li26daa1e2010-11-17 23:01:59 -0500715 {
716 .vendor = PCI_VENDOR_ID_O2,
717 .device = PCI_DEVICE_ID_O2_8120,
718 .subvendor = PCI_ANY_ID,
719 .subdevice = PCI_ANY_ID,
720 .driver_data = (kernel_ulong_t)&sdhci_o2,
721 },
722
723 {
724 .vendor = PCI_VENDOR_ID_O2,
725 .device = PCI_DEVICE_ID_O2_8220,
726 .subvendor = PCI_ANY_ID,
727 .subdevice = PCI_ANY_ID,
728 .driver_data = (kernel_ulong_t)&sdhci_o2,
729 },
730
731 {
732 .vendor = PCI_VENDOR_ID_O2,
733 .device = PCI_DEVICE_ID_O2_8221,
734 .subvendor = PCI_ANY_ID,
735 .subdevice = PCI_ANY_ID,
736 .driver_data = (kernel_ulong_t)&sdhci_o2,
737 },
738
739 {
740 .vendor = PCI_VENDOR_ID_O2,
741 .device = PCI_DEVICE_ID_O2_8320,
742 .subvendor = PCI_ANY_ID,
743 .subdevice = PCI_ANY_ID,
744 .driver_data = (kernel_ulong_t)&sdhci_o2,
745 },
746
747 {
748 .vendor = PCI_VENDOR_ID_O2,
749 .device = PCI_DEVICE_ID_O2_8321,
750 .subvendor = PCI_ANY_ID,
751 .subdevice = PCI_ANY_ID,
752 .driver_data = (kernel_ulong_t)&sdhci_o2,
753 },
754
Pierre Ossman22606402008-03-23 19:33:23 +0100755 { /* Generic SD host controller */
756 PCI_DEVICE_CLASS((PCI_CLASS_SYSTEM_SDHCI << 8), 0xFFFF00)
757 },
758
759 { /* end: all zeroes */ },
760};
761
762MODULE_DEVICE_TABLE(pci, pci_ids);
763
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100764/*****************************************************************************\
765 * *
766 * SDHCI core callbacks *
767 * *
768\*****************************************************************************/
769
770static int sdhci_pci_enable_dma(struct sdhci_host *host)
771{
772 struct sdhci_pci_slot *slot;
773 struct pci_dev *pdev;
774 int ret;
775
776 slot = sdhci_priv(host);
777 pdev = slot->chip->pdev;
778
779 if (((pdev->class & 0xFFFF00) == (PCI_CLASS_SYSTEM_SDHCI << 8)) &&
780 ((pdev->class & 0x0000FF) != PCI_SDHCI_IFDMA) &&
Richard Röjforsa13abc72009-09-22 16:45:30 -0700781 (host->flags & SDHCI_USE_SDMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100782 dev_warn(&pdev->dev, "Will use DMA mode even though HW "
783 "doesn't fully claim to support it.\n");
784 }
785
Yang Hongyang284901a2009-04-06 19:01:15 -0700786 ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100787 if (ret)
788 return ret;
789
790 pci_set_master(pdev);
791
792 return 0;
793}
794
795static struct sdhci_ops sdhci_pci_ops = {
796 .enable_dma = sdhci_pci_enable_dma,
797};
798
799/*****************************************************************************\
800 * *
801 * Suspend/resume *
802 * *
803\*****************************************************************************/
804
805#ifdef CONFIG_PM
806
Ameya Palandeb177bc92011-04-05 21:13:13 +0300807static int sdhci_pci_suspend(struct pci_dev *pdev, pm_message_t state)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100808{
809 struct sdhci_pci_chip *chip;
810 struct sdhci_pci_slot *slot;
Daniel Drake5f619702010-11-04 22:20:39 +0000811 mmc_pm_flag_t slot_pm_flags;
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800812 mmc_pm_flag_t pm_flags = 0;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100813 int i, ret;
814
815 chip = pci_get_drvdata(pdev);
816 if (!chip)
817 return 0;
818
Ameya Palandeb177bc92011-04-05 21:13:13 +0300819 for (i = 0; i < chip->num_slots; i++) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100820 slot = chip->slots[i];
821 if (!slot)
822 continue;
823
824 ret = sdhci_suspend_host(slot->host, state);
825
826 if (ret) {
Ameya Palandeb177bc92011-04-05 21:13:13 +0300827 for (i--; i >= 0; i--)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100828 sdhci_resume_host(chip->slots[i]->host);
829 return ret;
830 }
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800831
Daniel Drake5f619702010-11-04 22:20:39 +0000832 slot_pm_flags = slot->host->mmc->pm_flags;
833 if (slot_pm_flags & MMC_PM_WAKE_SDIO_IRQ)
834 sdhci_enable_irq_wakeups(slot->host);
835
836 pm_flags |= slot_pm_flags;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100837 }
838
Pierre Ossman44894282008-04-04 19:36:59 +0200839 if (chip->fixes && chip->fixes->suspend) {
840 ret = chip->fixes->suspend(chip, state);
841 if (ret) {
Ameya Palandeb177bc92011-04-05 21:13:13 +0300842 for (i = chip->num_slots - 1; i >= 0; i--)
Pierre Ossman44894282008-04-04 19:36:59 +0200843 sdhci_resume_host(chip->slots[i]->host);
844 return ret;
845 }
846 }
847
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100848 pci_save_state(pdev);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800849 if (pm_flags & MMC_PM_KEEP_POWER) {
Daniel Drake5f619702010-11-04 22:20:39 +0000850 if (pm_flags & MMC_PM_WAKE_SDIO_IRQ) {
851 pci_pme_active(pdev, true);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800852 pci_enable_wake(pdev, PCI_D3hot, 1);
Daniel Drake5f619702010-11-04 22:20:39 +0000853 }
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800854 pci_set_power_state(pdev, PCI_D3hot);
855 } else {
856 pci_enable_wake(pdev, pci_choose_state(pdev, state), 0);
857 pci_disable_device(pdev);
858 pci_set_power_state(pdev, pci_choose_state(pdev, state));
859 }
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100860
861 return 0;
862}
863
Ameya Palandeb177bc92011-04-05 21:13:13 +0300864static int sdhci_pci_resume(struct pci_dev *pdev)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100865{
866 struct sdhci_pci_chip *chip;
867 struct sdhci_pci_slot *slot;
868 int i, ret;
869
870 chip = pci_get_drvdata(pdev);
871 if (!chip)
872 return 0;
873
874 pci_set_power_state(pdev, PCI_D0);
875 pci_restore_state(pdev);
876 ret = pci_enable_device(pdev);
877 if (ret)
878 return ret;
879
Pierre Ossman45211e22008-03-24 13:09:09 +0100880 if (chip->fixes && chip->fixes->resume) {
881 ret = chip->fixes->resume(chip);
882 if (ret)
883 return ret;
884 }
885
Ameya Palandeb177bc92011-04-05 21:13:13 +0300886 for (i = 0; i < chip->num_slots; i++) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100887 slot = chip->slots[i];
888 if (!slot)
889 continue;
890
891 ret = sdhci_resume_host(slot->host);
892 if (ret)
893 return ret;
894 }
895
896 return 0;
897}
898
899#else /* CONFIG_PM */
900
901#define sdhci_pci_suspend NULL
902#define sdhci_pci_resume NULL
903
904#endif /* CONFIG_PM */
905
906/*****************************************************************************\
907 * *
908 * Device probing/removal *
909 * *
910\*****************************************************************************/
911
912static struct sdhci_pci_slot * __devinit sdhci_pci_probe_slot(
913 struct pci_dev *pdev, struct sdhci_pci_chip *chip, int bar)
914{
915 struct sdhci_pci_slot *slot;
916 struct sdhci_host *host;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100917 int ret;
918
919 if (!(pci_resource_flags(pdev, bar) & IORESOURCE_MEM)) {
920 dev_err(&pdev->dev, "BAR %d is not iomem. Aborting.\n", bar);
921 return ERR_PTR(-ENODEV);
922 }
923
924 if (pci_resource_len(pdev, bar) != 0x100) {
925 dev_err(&pdev->dev, "Invalid iomem size. You may "
926 "experience problems.\n");
927 }
928
929 if ((pdev->class & 0x0000FF) == PCI_SDHCI_IFVENDOR) {
930 dev_err(&pdev->dev, "Vendor specific interface. Aborting.\n");
931 return ERR_PTR(-ENODEV);
932 }
933
934 if ((pdev->class & 0x0000FF) > PCI_SDHCI_IFVENDOR) {
935 dev_err(&pdev->dev, "Unknown interface. Aborting.\n");
936 return ERR_PTR(-ENODEV);
937 }
938
939 host = sdhci_alloc_host(&pdev->dev, sizeof(struct sdhci_pci_slot));
940 if (IS_ERR(host)) {
Dan Carpenterc60a32c2009-04-10 23:31:10 +0200941 dev_err(&pdev->dev, "cannot allocate host\n");
Julia Lawalldc0fd7b2010-05-26 14:42:11 -0700942 return ERR_CAST(host);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100943 }
944
945 slot = sdhci_priv(host);
946
947 slot->chip = chip;
948 slot->host = host;
949 slot->pci_bar = bar;
950
951 host->hw_name = "PCI";
952 host->ops = &sdhci_pci_ops;
953 host->quirks = chip->quirks;
954
955 host->irq = pdev->irq;
956
957 ret = pci_request_region(pdev, bar, mmc_hostname(host->mmc));
958 if (ret) {
959 dev_err(&pdev->dev, "cannot request region\n");
Dan Carpenterc60a32c2009-04-10 23:31:10 +0200960 goto free;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100961 }
962
Arjan van de Ven092f82e2008-09-28 16:15:56 -0700963 host->ioaddr = pci_ioremap_bar(pdev, bar);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100964 if (!host->ioaddr) {
965 dev_err(&pdev->dev, "failed to remap registers\n");
Chris Ball9fdcdbb2011-03-29 00:46:12 -0400966 ret = -ENOMEM;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100967 goto release;
968 }
969
Pierre Ossman44894282008-04-04 19:36:59 +0200970 if (chip->fixes && chip->fixes->probe_slot) {
971 ret = chip->fixes->probe_slot(slot);
972 if (ret)
973 goto unmap;
974 }
975
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800976 host->mmc->pm_caps = MMC_PM_KEEP_POWER | MMC_PM_WAKE_SDIO_IRQ;
977
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100978 ret = sdhci_add_host(host);
979 if (ret)
Pierre Ossman44894282008-04-04 19:36:59 +0200980 goto remove;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100981
982 return slot;
983
Pierre Ossman44894282008-04-04 19:36:59 +0200984remove:
985 if (chip->fixes && chip->fixes->remove_slot)
Pierre Ossman1e728592008-04-16 19:13:13 +0200986 chip->fixes->remove_slot(slot, 0);
Pierre Ossman44894282008-04-04 19:36:59 +0200987
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100988unmap:
989 iounmap(host->ioaddr);
990
991release:
992 pci_release_region(pdev, bar);
Dan Carpenterc60a32c2009-04-10 23:31:10 +0200993
994free:
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100995 sdhci_free_host(host);
996
997 return ERR_PTR(ret);
998}
999
1000static void sdhci_pci_remove_slot(struct sdhci_pci_slot *slot)
1001{
Pierre Ossman1e728592008-04-16 19:13:13 +02001002 int dead;
1003 u32 scratch;
1004
1005 dead = 0;
1006 scratch = readl(slot->host->ioaddr + SDHCI_INT_STATUS);
1007 if (scratch == (u32)-1)
1008 dead = 1;
1009
1010 sdhci_remove_host(slot->host, dead);
Pierre Ossman44894282008-04-04 19:36:59 +02001011
1012 if (slot->chip->fixes && slot->chip->fixes->remove_slot)
Pierre Ossman1e728592008-04-16 19:13:13 +02001013 slot->chip->fixes->remove_slot(slot, dead);
Pierre Ossman44894282008-04-04 19:36:59 +02001014
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001015 pci_release_region(slot->chip->pdev, slot->pci_bar);
Pierre Ossman44894282008-04-04 19:36:59 +02001016
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001017 sdhci_free_host(slot->host);
1018}
1019
1020static int __devinit sdhci_pci_probe(struct pci_dev *pdev,
1021 const struct pci_device_id *ent)
1022{
1023 struct sdhci_pci_chip *chip;
1024 struct sdhci_pci_slot *slot;
1025
Sergei Shtylyovcf5e23e2011-03-17 16:46:17 -04001026 u8 slots, first_bar;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001027 int ret, i;
1028
1029 BUG_ON(pdev == NULL);
1030 BUG_ON(ent == NULL);
1031
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001032 dev_info(&pdev->dev, "SDHCI controller found [%04x:%04x] (rev %x)\n",
Sergei Shtylyovcf5e23e2011-03-17 16:46:17 -04001033 (int)pdev->vendor, (int)pdev->device, (int)pdev->revision);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001034
1035 ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &slots);
1036 if (ret)
1037 return ret;
1038
1039 slots = PCI_SLOT_INFO_SLOTS(slots) + 1;
1040 dev_dbg(&pdev->dev, "found %d slot(s)\n", slots);
1041 if (slots == 0)
1042 return -ENODEV;
1043
1044 BUG_ON(slots > MAX_SLOTS);
1045
1046 ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &first_bar);
1047 if (ret)
1048 return ret;
1049
1050 first_bar &= PCI_SLOT_INFO_FIRST_BAR_MASK;
1051
1052 if (first_bar > 5) {
1053 dev_err(&pdev->dev, "Invalid first BAR. Aborting.\n");
1054 return -ENODEV;
1055 }
1056
1057 ret = pci_enable_device(pdev);
1058 if (ret)
1059 return ret;
1060
1061 chip = kzalloc(sizeof(struct sdhci_pci_chip), GFP_KERNEL);
1062 if (!chip) {
1063 ret = -ENOMEM;
1064 goto err;
1065 }
1066
1067 chip->pdev = pdev;
Ameya Palandeb177bc92011-04-05 21:13:13 +03001068 chip->fixes = (const struct sdhci_pci_fixes *)ent->driver_data;
Pierre Ossman22606402008-03-23 19:33:23 +01001069 if (chip->fixes)
1070 chip->quirks = chip->fixes->quirks;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001071 chip->num_slots = slots;
1072
1073 pci_set_drvdata(pdev, chip);
1074
Pierre Ossman22606402008-03-23 19:33:23 +01001075 if (chip->fixes && chip->fixes->probe) {
1076 ret = chip->fixes->probe(chip);
1077 if (ret)
1078 goto free;
1079 }
1080
Alan Cox225d85f2010-10-04 15:24:21 +01001081 slots = chip->num_slots; /* Quirk may have changed this */
1082
Ameya Palandeb177bc92011-04-05 21:13:13 +03001083 for (i = 0; i < slots; i++) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001084 slot = sdhci_pci_probe_slot(pdev, chip, first_bar + i);
1085 if (IS_ERR(slot)) {
Ameya Palandeb177bc92011-04-05 21:13:13 +03001086 for (i--; i >= 0; i--)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001087 sdhci_pci_remove_slot(chip->slots[i]);
1088 ret = PTR_ERR(slot);
1089 goto free;
1090 }
1091
1092 chip->slots[i] = slot;
1093 }
1094
1095 return 0;
1096
1097free:
1098 pci_set_drvdata(pdev, NULL);
1099 kfree(chip);
1100
1101err:
1102 pci_disable_device(pdev);
1103 return ret;
1104}
1105
1106static void __devexit sdhci_pci_remove(struct pci_dev *pdev)
1107{
1108 int i;
1109 struct sdhci_pci_chip *chip;
1110
1111 chip = pci_get_drvdata(pdev);
1112
1113 if (chip) {
Ameya Palandeb177bc92011-04-05 21:13:13 +03001114 for (i = 0; i < chip->num_slots; i++)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001115 sdhci_pci_remove_slot(chip->slots[i]);
1116
1117 pci_set_drvdata(pdev, NULL);
1118 kfree(chip);
1119 }
1120
1121 pci_disable_device(pdev);
1122}
1123
1124static struct pci_driver sdhci_driver = {
Ameya Palandeb177bc92011-04-05 21:13:13 +03001125 .name = "sdhci-pci",
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001126 .id_table = pci_ids,
Ameya Palandeb177bc92011-04-05 21:13:13 +03001127 .probe = sdhci_pci_probe,
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001128 .remove = __devexit_p(sdhci_pci_remove),
1129 .suspend = sdhci_pci_suspend,
1130 .resume = sdhci_pci_resume,
1131};
1132
1133/*****************************************************************************\
1134 * *
1135 * Driver init/exit *
1136 * *
1137\*****************************************************************************/
1138
1139static int __init sdhci_drv_init(void)
1140{
1141 return pci_register_driver(&sdhci_driver);
1142}
1143
1144static void __exit sdhci_drv_exit(void)
1145{
1146 pci_unregister_driver(&sdhci_driver);
1147}
1148
1149module_init(sdhci_drv_init);
1150module_exit(sdhci_drv_exit);
1151
Pierre Ossman32710e82009-04-08 20:14:54 +02001152MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001153MODULE_DESCRIPTION("Secure Digital Host Controller Interface PCI driver");
1154MODULE_LICENSE("GPL");