blob: 82c5dc41267975c314557834b94c5304b92c87a3 [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>
17#include <linux/pci.h>
18#include <linux/dma-mapping.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
Maxim Levitskyccc92c22010-08-10 18:01:42 -070020#include <linux/device.h>
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010021#include <linux/mmc/host.h>
Ameya Palandeb177bc92011-04-05 21:13:13 +030022#include <linux/scatterlist.h>
23#include <linux/io.h>
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010024
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 Ossmanb8c86fc2008-03-18 17:35:49 +010041struct sdhci_pci_chip;
Pierre Ossman44894282008-04-04 19:36:59 +020042struct sdhci_pci_slot;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010043
Pierre Ossman22606402008-03-23 19:33:23 +010044struct sdhci_pci_fixes {
45 unsigned int quirks;
46
Ameya Palandeb177bc92011-04-05 21:13:13 +030047 int (*probe) (struct sdhci_pci_chip *);
Pierre Ossman45211e22008-03-24 13:09:09 +010048
Ameya Palandeb177bc92011-04-05 21:13:13 +030049 int (*probe_slot) (struct sdhci_pci_slot *);
50 void (*remove_slot) (struct sdhci_pci_slot *, int);
Pierre Ossman44894282008-04-04 19:36:59 +020051
Ameya Palandeb177bc92011-04-05 21:13:13 +030052 int (*suspend) (struct sdhci_pci_chip *,
Pierre Ossman44894282008-04-04 19:36:59 +020053 pm_message_t);
Ameya Palandeb177bc92011-04-05 21:13:13 +030054 int (*resume) (struct sdhci_pci_chip *);
Pierre Ossman22606402008-03-23 19:33:23 +010055};
56
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010057struct sdhci_pci_slot {
58 struct sdhci_pci_chip *chip;
59 struct sdhci_host *host;
60
61 int pci_bar;
62};
63
64struct sdhci_pci_chip {
65 struct pci_dev *pdev;
Pierre Ossman22606402008-03-23 19:33:23 +010066
67 unsigned int quirks;
68 const struct sdhci_pci_fixes *fixes;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010069
70 int num_slots; /* Slots on controller */
71 struct sdhci_pci_slot *slots[MAX_SLOTS]; /* Pointers to host slots */
72};
73
Pierre Ossman22606402008-03-23 19:33:23 +010074
75/*****************************************************************************\
76 * *
77 * Hardware specific quirk handling *
78 * *
79\*****************************************************************************/
80
81static int ricoh_probe(struct sdhci_pci_chip *chip)
82{
Chris Ballc99436f2009-09-22 16:45:22 -070083 if (chip->pdev->subsystem_vendor == PCI_VENDOR_ID_SAMSUNG ||
84 chip->pdev->subsystem_vendor == PCI_VENDOR_ID_SONY)
Pierre Ossman22606402008-03-23 19:33:23 +010085 chip->quirks |= SDHCI_QUIRK_NO_CARD_NO_RESET;
Maxim Levitskyccc92c22010-08-10 18:01:42 -070086 return 0;
87}
Pierre Ossman22606402008-03-23 19:33:23 +010088
Maxim Levitskyccc92c22010-08-10 18:01:42 -070089static int ricoh_mmc_probe_slot(struct sdhci_pci_slot *slot)
90{
91 slot->host->caps =
92 ((0x21 << SDHCI_TIMEOUT_CLK_SHIFT)
93 & SDHCI_TIMEOUT_CLK_MASK) |
94
95 ((0x21 << SDHCI_CLOCK_BASE_SHIFT)
96 & SDHCI_CLOCK_BASE_MASK) |
97
98 SDHCI_TIMEOUT_CLK_UNIT |
99 SDHCI_CAN_VDD_330 |
100 SDHCI_CAN_DO_SDMA;
101 return 0;
102}
103
104static int ricoh_mmc_resume(struct sdhci_pci_chip *chip)
105{
106 /* Apply a delay to allow controller to settle */
107 /* Otherwise it becomes confused if card state changed
108 during suspend */
109 msleep(500);
Pierre Ossman22606402008-03-23 19:33:23 +0100110 return 0;
111}
112
113static const struct sdhci_pci_fixes sdhci_ricoh = {
114 .probe = ricoh_probe,
Vasily Khoruzhick84938292010-03-05 13:43:46 -0800115 .quirks = SDHCI_QUIRK_32BIT_DMA_ADDR |
116 SDHCI_QUIRK_FORCE_DMA |
117 SDHCI_QUIRK_CLOCK_BEFORE_RESET,
Pierre Ossman22606402008-03-23 19:33:23 +0100118};
119
Maxim Levitskyccc92c22010-08-10 18:01:42 -0700120static const struct sdhci_pci_fixes sdhci_ricoh_mmc = {
121 .probe_slot = ricoh_mmc_probe_slot,
122 .resume = ricoh_mmc_resume,
123 .quirks = SDHCI_QUIRK_32BIT_DMA_ADDR |
124 SDHCI_QUIRK_CLOCK_BEFORE_RESET |
125 SDHCI_QUIRK_NO_CARD_NO_RESET |
126 SDHCI_QUIRK_MISSING_CAPS
127};
128
Pierre Ossman22606402008-03-23 19:33:23 +0100129static const struct sdhci_pci_fixes sdhci_ene_712 = {
130 .quirks = SDHCI_QUIRK_SINGLE_POWER_WRITE |
131 SDHCI_QUIRK_BROKEN_DMA,
132};
133
134static const struct sdhci_pci_fixes sdhci_ene_714 = {
135 .quirks = SDHCI_QUIRK_SINGLE_POWER_WRITE |
136 SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS |
137 SDHCI_QUIRK_BROKEN_DMA,
138};
139
140static const struct sdhci_pci_fixes sdhci_cafe = {
141 .quirks = SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER |
Andres Salomona0874892009-03-02 21:48:20 +0100142 SDHCI_QUIRK_NO_BUSY_IRQ |
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200143 SDHCI_QUIRK_BROKEN_TIMEOUT_VAL,
Pierre Ossman22606402008-03-23 19:33:23 +0100144};
145
Alan Coxf9ee3ea2010-10-04 15:25:11 +0100146/*
147 * ADMA operation is disabled for Moorestown platform due to
148 * hardware bugs.
149 */
Jacob Pan35ac6f02010-11-09 13:57:29 +0000150static int mrst_hc_probe(struct sdhci_pci_chip *chip)
Alan Coxf9ee3ea2010-10-04 15:25:11 +0100151{
152 /*
Jacob Pan35ac6f02010-11-09 13:57:29 +0000153 * slots number is fixed here for MRST as SDIO3/5 are never used and
154 * have hardware bugs.
Alan Coxf9ee3ea2010-10-04 15:25:11 +0100155 */
156 chip->num_slots = 1;
157 return 0;
158}
159
160static const struct sdhci_pci_fixes sdhci_intel_mrst_hc0 = {
161 .quirks = SDHCI_QUIRK_BROKEN_ADMA | SDHCI_QUIRK_NO_HISPD_BIT,
162};
163
Jacob Pan35ac6f02010-11-09 13:57:29 +0000164static const struct sdhci_pci_fixes sdhci_intel_mrst_hc1_hc2 = {
Alan Coxf9ee3ea2010-10-04 15:25:11 +0100165 .quirks = SDHCI_QUIRK_BROKEN_ADMA | SDHCI_QUIRK_NO_HISPD_BIT,
Jacob Pan35ac6f02010-11-09 13:57:29 +0000166 .probe = mrst_hc_probe,
Alan Coxf9ee3ea2010-10-04 15:25:11 +0100167};
168
Xiaochen Shen29229052010-10-04 15:24:52 +0100169static const struct sdhci_pci_fixes sdhci_intel_mfd_sd = {
170 .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
171};
172
173static const struct sdhci_pci_fixes sdhci_intel_mfd_emmc_sdio = {
174 .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
175};
176
Jennifer Li26daa1e2010-11-17 23:01:59 -0500177/* O2Micro extra registers */
178#define O2_SD_LOCK_WP 0xD3
179#define O2_SD_MULTI_VCC3V 0xEE
180#define O2_SD_CLKREQ 0xEC
181#define O2_SD_CAPS 0xE0
182#define O2_SD_ADMA1 0xE2
183#define O2_SD_ADMA2 0xE7
184#define O2_SD_INF_MOD 0xF1
185
186static int o2_probe(struct sdhci_pci_chip *chip)
187{
188 int ret;
189 u8 scratch;
190
191 switch (chip->pdev->device) {
192 case PCI_DEVICE_ID_O2_8220:
193 case PCI_DEVICE_ID_O2_8221:
194 case PCI_DEVICE_ID_O2_8320:
195 case PCI_DEVICE_ID_O2_8321:
196 /* This extra setup is required due to broken ADMA. */
197 ret = pci_read_config_byte(chip->pdev, O2_SD_LOCK_WP, &scratch);
198 if (ret)
199 return ret;
200 scratch &= 0x7f;
201 pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch);
202
203 /* Set Multi 3 to VCC3V# */
204 pci_write_config_byte(chip->pdev, O2_SD_MULTI_VCC3V, 0x08);
205
206 /* Disable CLK_REQ# support after media DET */
207 ret = pci_read_config_byte(chip->pdev, O2_SD_CLKREQ, &scratch);
208 if (ret)
209 return ret;
210 scratch |= 0x20;
211 pci_write_config_byte(chip->pdev, O2_SD_CLKREQ, scratch);
212
213 /* Choose capabilities, enable SDMA. We have to write 0x01
214 * to the capabilities register first to unlock it.
215 */
216 ret = pci_read_config_byte(chip->pdev, O2_SD_CAPS, &scratch);
217 if (ret)
218 return ret;
219 scratch |= 0x01;
220 pci_write_config_byte(chip->pdev, O2_SD_CAPS, scratch);
221 pci_write_config_byte(chip->pdev, O2_SD_CAPS, 0x73);
222
223 /* Disable ADMA1/2 */
224 pci_write_config_byte(chip->pdev, O2_SD_ADMA1, 0x39);
225 pci_write_config_byte(chip->pdev, O2_SD_ADMA2, 0x08);
226
227 /* Disable the infinite transfer mode */
228 ret = pci_read_config_byte(chip->pdev, O2_SD_INF_MOD, &scratch);
229 if (ret)
230 return ret;
231 scratch |= 0x08;
232 pci_write_config_byte(chip->pdev, O2_SD_INF_MOD, scratch);
233
234 /* Lock WP */
235 ret = pci_read_config_byte(chip->pdev, O2_SD_LOCK_WP, &scratch);
236 if (ret)
237 return ret;
238 scratch |= 0x80;
239 pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch);
240 }
241
242 return 0;
243}
244
Pierre Ossman45211e22008-03-24 13:09:09 +0100245static int jmicron_pmos(struct sdhci_pci_chip *chip, int on)
246{
247 u8 scratch;
248 int ret;
249
250 ret = pci_read_config_byte(chip->pdev, 0xAE, &scratch);
251 if (ret)
252 return ret;
253
254 /*
255 * Turn PMOS on [bit 0], set over current detection to 2.4 V
256 * [bit 1:2] and enable over current debouncing [bit 6].
257 */
258 if (on)
259 scratch |= 0x47;
260 else
261 scratch &= ~0x47;
262
263 ret = pci_write_config_byte(chip->pdev, 0xAE, scratch);
264 if (ret)
265 return ret;
266
267 return 0;
268}
269
270static int jmicron_probe(struct sdhci_pci_chip *chip)
271{
272 int ret;
Takashi Iwai8f230f42010-12-08 10:04:30 +0100273 u16 mmcdev = 0;
Pierre Ossman45211e22008-03-24 13:09:09 +0100274
Pierre Ossman93fc48c2008-06-28 18:21:41 +0200275 if (chip->pdev->revision == 0) {
276 chip->quirks |= SDHCI_QUIRK_32BIT_DMA_ADDR |
277 SDHCI_QUIRK_32BIT_DMA_SIZE |
Pierre Ossman2134a922008-06-28 18:28:51 +0200278 SDHCI_QUIRK_32BIT_ADMA_SIZE |
Pierre Ossman4a3cba32008-07-29 00:11:16 +0200279 SDHCI_QUIRK_RESET_AFTER_REQUEST |
Pierre Ossman86a6a872009-02-02 21:13:49 +0100280 SDHCI_QUIRK_BROKEN_SMALL_PIO;
Pierre Ossman93fc48c2008-06-28 18:21:41 +0200281 }
282
Pierre Ossman45211e22008-03-24 13:09:09 +0100283 /*
Pierre Ossman44894282008-04-04 19:36:59 +0200284 * JMicron chips can have two interfaces to the same hardware
285 * in order to work around limitations in Microsoft's driver.
286 * We need to make sure we only bind to one of them.
287 *
288 * This code assumes two things:
289 *
290 * 1. The PCI code adds subfunctions in order.
291 *
292 * 2. The MMC interface has a lower subfunction number
293 * than the SD interface.
294 */
Takashi Iwai8f230f42010-12-08 10:04:30 +0100295 if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_SD)
296 mmcdev = PCI_DEVICE_ID_JMICRON_JMB38X_MMC;
297 else if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_SD)
298 mmcdev = PCI_DEVICE_ID_JMICRON_JMB388_ESD;
299
300 if (mmcdev) {
Pierre Ossman44894282008-04-04 19:36:59 +0200301 struct pci_dev *sd_dev;
302
303 sd_dev = NULL;
304 while ((sd_dev = pci_get_device(PCI_VENDOR_ID_JMICRON,
Takashi Iwai8f230f42010-12-08 10:04:30 +0100305 mmcdev, sd_dev)) != NULL) {
Pierre Ossman44894282008-04-04 19:36:59 +0200306 if ((PCI_SLOT(chip->pdev->devfn) ==
307 PCI_SLOT(sd_dev->devfn)) &&
308 (chip->pdev->bus == sd_dev->bus))
309 break;
310 }
311
312 if (sd_dev) {
313 pci_dev_put(sd_dev);
314 dev_info(&chip->pdev->dev, "Refusing to bind to "
315 "secondary interface.\n");
316 return -ENODEV;
317 }
318 }
319
320 /*
Pierre Ossman45211e22008-03-24 13:09:09 +0100321 * JMicron chips need a bit of a nudge to enable the power
322 * output pins.
323 */
324 ret = jmicron_pmos(chip, 1);
325 if (ret) {
326 dev_err(&chip->pdev->dev, "Failure enabling card power\n");
327 return ret;
328 }
329
330 return 0;
331}
332
Pierre Ossman44894282008-04-04 19:36:59 +0200333static void jmicron_enable_mmc(struct sdhci_host *host, int on)
334{
335 u8 scratch;
336
337 scratch = readb(host->ioaddr + 0xC0);
338
339 if (on)
340 scratch |= 0x01;
341 else
342 scratch &= ~0x01;
343
344 writeb(scratch, host->ioaddr + 0xC0);
345}
346
347static int jmicron_probe_slot(struct sdhci_pci_slot *slot)
348{
Pierre Ossman2134a922008-06-28 18:28:51 +0200349 if (slot->chip->pdev->revision == 0) {
350 u16 version;
351
352 version = readl(slot->host->ioaddr + SDHCI_HOST_VERSION);
353 version = (version & SDHCI_VENDOR_VER_MASK) >>
354 SDHCI_VENDOR_VER_SHIFT;
355
356 /*
357 * Older versions of the chip have lots of nasty glitches
358 * in the ADMA engine. It's best just to avoid it
359 * completely.
360 */
361 if (version < 0xAC)
362 slot->host->quirks |= SDHCI_QUIRK_BROKEN_ADMA;
363 }
364
Takashi Iwai8f230f42010-12-08 10:04:30 +0100365 /* JM388 MMC doesn't support 1.8V while SD supports it */
366 if (slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD) {
367 slot->host->ocr_avail_sd = MMC_VDD_32_33 | MMC_VDD_33_34 |
368 MMC_VDD_29_30 | MMC_VDD_30_31 |
369 MMC_VDD_165_195; /* allow 1.8V */
370 slot->host->ocr_avail_mmc = MMC_VDD_32_33 | MMC_VDD_33_34 |
371 MMC_VDD_29_30 | MMC_VDD_30_31; /* no 1.8V for MMC */
372 }
373
Pierre Ossman44894282008-04-04 19:36:59 +0200374 /*
375 * The secondary interface requires a bit set to get the
376 * interrupts.
377 */
Takashi Iwai8f230f42010-12-08 10:04:30 +0100378 if (slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC ||
379 slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD)
Pierre Ossman44894282008-04-04 19:36:59 +0200380 jmicron_enable_mmc(slot->host, 1);
381
Takashi Iwaid75c1082010-12-16 17:54:14 +0100382 slot->host->mmc->caps |= MMC_CAP_BUS_WIDTH_TEST;
383
Pierre Ossman44894282008-04-04 19:36:59 +0200384 return 0;
385}
386
Pierre Ossman1e728592008-04-16 19:13:13 +0200387static void jmicron_remove_slot(struct sdhci_pci_slot *slot, int dead)
Pierre Ossman44894282008-04-04 19:36:59 +0200388{
Pierre Ossman1e728592008-04-16 19:13:13 +0200389 if (dead)
390 return;
391
Takashi Iwai8f230f42010-12-08 10:04:30 +0100392 if (slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC ||
393 slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD)
Pierre Ossman44894282008-04-04 19:36:59 +0200394 jmicron_enable_mmc(slot->host, 0);
395}
396
397static int jmicron_suspend(struct sdhci_pci_chip *chip, pm_message_t state)
398{
399 int i;
400
Takashi Iwai8f230f42010-12-08 10:04:30 +0100401 if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC ||
402 chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD) {
Ameya Palandeb177bc92011-04-05 21:13:13 +0300403 for (i = 0; i < chip->num_slots; i++)
Pierre Ossman44894282008-04-04 19:36:59 +0200404 jmicron_enable_mmc(chip->slots[i]->host, 0);
405 }
406
407 return 0;
408}
409
Pierre Ossman45211e22008-03-24 13:09:09 +0100410static int jmicron_resume(struct sdhci_pci_chip *chip)
411{
Pierre Ossman44894282008-04-04 19:36:59 +0200412 int ret, i;
413
Takashi Iwai8f230f42010-12-08 10:04:30 +0100414 if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC ||
415 chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD) {
Ameya Palandeb177bc92011-04-05 21:13:13 +0300416 for (i = 0; i < chip->num_slots; i++)
Pierre Ossman44894282008-04-04 19:36:59 +0200417 jmicron_enable_mmc(chip->slots[i]->host, 1);
418 }
Pierre Ossman45211e22008-03-24 13:09:09 +0100419
420 ret = jmicron_pmos(chip, 1);
421 if (ret) {
422 dev_err(&chip->pdev->dev, "Failure enabling card power\n");
423 return ret;
424 }
425
426 return 0;
427}
428
Jennifer Li26daa1e2010-11-17 23:01:59 -0500429static const struct sdhci_pci_fixes sdhci_o2 = {
430 .probe = o2_probe,
431};
432
Pierre Ossman22606402008-03-23 19:33:23 +0100433static const struct sdhci_pci_fixes sdhci_jmicron = {
Pierre Ossman45211e22008-03-24 13:09:09 +0100434 .probe = jmicron_probe,
435
Pierre Ossman44894282008-04-04 19:36:59 +0200436 .probe_slot = jmicron_probe_slot,
437 .remove_slot = jmicron_remove_slot,
438
439 .suspend = jmicron_suspend,
Pierre Ossman45211e22008-03-24 13:09:09 +0100440 .resume = jmicron_resume,
Pierre Ossman22606402008-03-23 19:33:23 +0100441};
442
Nicolas Pitrea7a61862009-12-14 18:01:26 -0800443/* SysKonnect CardBus2SDIO extra registers */
444#define SYSKT_CTRL 0x200
445#define SYSKT_RDFIFO_STAT 0x204
446#define SYSKT_WRFIFO_STAT 0x208
447#define SYSKT_POWER_DATA 0x20c
448#define SYSKT_POWER_330 0xef
449#define SYSKT_POWER_300 0xf8
450#define SYSKT_POWER_184 0xcc
451#define SYSKT_POWER_CMD 0x20d
452#define SYSKT_POWER_START (1 << 7)
453#define SYSKT_POWER_STATUS 0x20e
454#define SYSKT_POWER_STATUS_OK (1 << 0)
455#define SYSKT_BOARD_REV 0x210
456#define SYSKT_CHIP_REV 0x211
457#define SYSKT_CONF_DATA 0x212
458#define SYSKT_CONF_DATA_1V8 (1 << 2)
459#define SYSKT_CONF_DATA_2V5 (1 << 1)
460#define SYSKT_CONF_DATA_3V3 (1 << 0)
461
462static int syskt_probe(struct sdhci_pci_chip *chip)
463{
464 if ((chip->pdev->class & 0x0000FF) == PCI_SDHCI_IFVENDOR) {
465 chip->pdev->class &= ~0x0000FF;
466 chip->pdev->class |= PCI_SDHCI_IFDMA;
467 }
468 return 0;
469}
470
471static int syskt_probe_slot(struct sdhci_pci_slot *slot)
472{
473 int tm, ps;
474
475 u8 board_rev = readb(slot->host->ioaddr + SYSKT_BOARD_REV);
476 u8 chip_rev = readb(slot->host->ioaddr + SYSKT_CHIP_REV);
477 dev_info(&slot->chip->pdev->dev, "SysKonnect CardBus2SDIO, "
478 "board rev %d.%d, chip rev %d.%d\n",
479 board_rev >> 4, board_rev & 0xf,
480 chip_rev >> 4, chip_rev & 0xf);
481 if (chip_rev >= 0x20)
482 slot->host->quirks |= SDHCI_QUIRK_FORCE_DMA;
483
484 writeb(SYSKT_POWER_330, slot->host->ioaddr + SYSKT_POWER_DATA);
485 writeb(SYSKT_POWER_START, slot->host->ioaddr + SYSKT_POWER_CMD);
486 udelay(50);
487 tm = 10; /* Wait max 1 ms */
488 do {
489 ps = readw(slot->host->ioaddr + SYSKT_POWER_STATUS);
490 if (ps & SYSKT_POWER_STATUS_OK)
491 break;
492 udelay(100);
493 } while (--tm);
494 if (!tm) {
495 dev_err(&slot->chip->pdev->dev,
496 "power regulator never stabilized");
497 writeb(0, slot->host->ioaddr + SYSKT_POWER_CMD);
498 return -ENODEV;
499 }
500
501 return 0;
502}
503
504static const struct sdhci_pci_fixes sdhci_syskt = {
505 .quirks = SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER,
506 .probe = syskt_probe,
507 .probe_slot = syskt_probe_slot,
508};
509
Harald Welte557b0692009-06-18 16:53:38 +0200510static int via_probe(struct sdhci_pci_chip *chip)
511{
512 if (chip->pdev->revision == 0x10)
513 chip->quirks |= SDHCI_QUIRK_DELAY_AFTER_POWER;
514
515 return 0;
516}
517
518static const struct sdhci_pci_fixes sdhci_via = {
519 .probe = via_probe,
520};
521
Pierre Ossman22606402008-03-23 19:33:23 +0100522static const struct pci_device_id pci_ids[] __devinitdata = {
523 {
524 .vendor = PCI_VENDOR_ID_RICOH,
525 .device = PCI_DEVICE_ID_RICOH_R5C822,
526 .subvendor = PCI_ANY_ID,
527 .subdevice = PCI_ANY_ID,
528 .driver_data = (kernel_ulong_t)&sdhci_ricoh,
529 },
530
531 {
Maxim Levitskyccc92c22010-08-10 18:01:42 -0700532 .vendor = PCI_VENDOR_ID_RICOH,
533 .device = 0x843,
534 .subvendor = PCI_ANY_ID,
535 .subdevice = PCI_ANY_ID,
536 .driver_data = (kernel_ulong_t)&sdhci_ricoh_mmc,
537 },
538
539 {
Pablo Castillo568133e2010-08-10 18:02:01 -0700540 .vendor = PCI_VENDOR_ID_RICOH,
541 .device = 0xe822,
542 .subvendor = PCI_ANY_ID,
543 .subdevice = PCI_ANY_ID,
544 .driver_data = (kernel_ulong_t)&sdhci_ricoh_mmc,
545 },
546
547 {
Manoj Iyer5fd11c02011-02-11 16:25:31 -0600548 .vendor = PCI_VENDOR_ID_RICOH,
549 .device = 0xe823,
550 .subvendor = PCI_ANY_ID,
551 .subdevice = PCI_ANY_ID,
552 .driver_data = (kernel_ulong_t)&sdhci_ricoh_mmc,
553 },
554
555 {
Pierre Ossman22606402008-03-23 19:33:23 +0100556 .vendor = PCI_VENDOR_ID_ENE,
557 .device = PCI_DEVICE_ID_ENE_CB712_SD,
558 .subvendor = PCI_ANY_ID,
559 .subdevice = PCI_ANY_ID,
560 .driver_data = (kernel_ulong_t)&sdhci_ene_712,
561 },
562
563 {
564 .vendor = PCI_VENDOR_ID_ENE,
565 .device = PCI_DEVICE_ID_ENE_CB712_SD_2,
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_CB714_SD,
574 .subvendor = PCI_ANY_ID,
575 .subdevice = PCI_ANY_ID,
576 .driver_data = (kernel_ulong_t)&sdhci_ene_714,
577 },
578
579 {
580 .vendor = PCI_VENDOR_ID_ENE,
581 .device = PCI_DEVICE_ID_ENE_CB714_SD_2,
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_MARVELL,
David Woodhouse8c5eb882008-09-03 09:45:57 +0100589 .device = PCI_DEVICE_ID_MARVELL_88ALP01_SD,
Pierre Ossman22606402008-03-23 19:33:23 +0100590 .subvendor = PCI_ANY_ID,
591 .subdevice = PCI_ANY_ID,
592 .driver_data = (kernel_ulong_t)&sdhci_cafe,
593 },
594
595 {
596 .vendor = PCI_VENDOR_ID_JMICRON,
597 .device = PCI_DEVICE_ID_JMICRON_JMB38X_SD,
598 .subvendor = PCI_ANY_ID,
599 .subdevice = PCI_ANY_ID,
600 .driver_data = (kernel_ulong_t)&sdhci_jmicron,
601 },
602
Pierre Ossman44894282008-04-04 19:36:59 +0200603 {
604 .vendor = PCI_VENDOR_ID_JMICRON,
605 .device = PCI_DEVICE_ID_JMICRON_JMB38X_MMC,
606 .subvendor = PCI_ANY_ID,
607 .subdevice = PCI_ANY_ID,
608 .driver_data = (kernel_ulong_t)&sdhci_jmicron,
609 },
610
Harald Welte557b0692009-06-18 16:53:38 +0200611 {
Takashi Iwai8f230f42010-12-08 10:04:30 +0100612 .vendor = PCI_VENDOR_ID_JMICRON,
613 .device = PCI_DEVICE_ID_JMICRON_JMB388_SD,
614 .subvendor = PCI_ANY_ID,
615 .subdevice = PCI_ANY_ID,
616 .driver_data = (kernel_ulong_t)&sdhci_jmicron,
617 },
618
619 {
620 .vendor = PCI_VENDOR_ID_JMICRON,
621 .device = PCI_DEVICE_ID_JMICRON_JMB388_ESD,
622 .subvendor = PCI_ANY_ID,
623 .subdevice = PCI_ANY_ID,
624 .driver_data = (kernel_ulong_t)&sdhci_jmicron,
625 },
626
627 {
Nicolas Pitrea7a61862009-12-14 18:01:26 -0800628 .vendor = PCI_VENDOR_ID_SYSKONNECT,
629 .device = 0x8000,
630 .subvendor = PCI_ANY_ID,
631 .subdevice = PCI_ANY_ID,
632 .driver_data = (kernel_ulong_t)&sdhci_syskt,
633 },
634
635 {
Harald Welte557b0692009-06-18 16:53:38 +0200636 .vendor = PCI_VENDOR_ID_VIA,
637 .device = 0x95d0,
638 .subvendor = PCI_ANY_ID,
639 .subdevice = PCI_ANY_ID,
640 .driver_data = (kernel_ulong_t)&sdhci_via,
641 },
642
Xiaochen Shen29229052010-10-04 15:24:52 +0100643 {
644 .vendor = PCI_VENDOR_ID_INTEL,
Alan Coxf9ee3ea2010-10-04 15:25:11 +0100645 .device = PCI_DEVICE_ID_INTEL_MRST_SD0,
646 .subvendor = PCI_ANY_ID,
647 .subdevice = PCI_ANY_ID,
648 .driver_data = (kernel_ulong_t)&sdhci_intel_mrst_hc0,
649 },
650
651 {
652 .vendor = PCI_VENDOR_ID_INTEL,
653 .device = PCI_DEVICE_ID_INTEL_MRST_SD1,
654 .subvendor = PCI_ANY_ID,
655 .subdevice = PCI_ANY_ID,
Jacob Pan35ac6f02010-11-09 13:57:29 +0000656 .driver_data = (kernel_ulong_t)&sdhci_intel_mrst_hc1_hc2,
657 },
658
659 {
660 .vendor = PCI_VENDOR_ID_INTEL,
661 .device = PCI_DEVICE_ID_INTEL_MRST_SD2,
662 .subvendor = PCI_ANY_ID,
663 .subdevice = PCI_ANY_ID,
664 .driver_data = (kernel_ulong_t)&sdhci_intel_mrst_hc1_hc2,
Alan Coxf9ee3ea2010-10-04 15:25:11 +0100665 },
666
667 {
668 .vendor = PCI_VENDOR_ID_INTEL,
Xiaochen Shen29229052010-10-04 15:24:52 +0100669 .device = PCI_DEVICE_ID_INTEL_MFD_SD,
670 .subvendor = PCI_ANY_ID,
671 .subdevice = PCI_ANY_ID,
672 .driver_data = (kernel_ulong_t)&sdhci_intel_mfd_sd,
673 },
674
675 {
676 .vendor = PCI_VENDOR_ID_INTEL,
677 .device = PCI_DEVICE_ID_INTEL_MFD_SDIO1,
678 .subvendor = PCI_ANY_ID,
679 .subdevice = PCI_ANY_ID,
680 .driver_data = (kernel_ulong_t)&sdhci_intel_mfd_emmc_sdio,
681 },
682
683 {
684 .vendor = PCI_VENDOR_ID_INTEL,
685 .device = PCI_DEVICE_ID_INTEL_MFD_SDIO2,
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_EMMC0,
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_EMMC1,
702 .subvendor = PCI_ANY_ID,
703 .subdevice = PCI_ANY_ID,
704 .driver_data = (kernel_ulong_t)&sdhci_intel_mfd_emmc_sdio,
705 },
706
Jennifer Li26daa1e2010-11-17 23:01:59 -0500707 {
708 .vendor = PCI_VENDOR_ID_O2,
709 .device = PCI_DEVICE_ID_O2_8120,
710 .subvendor = PCI_ANY_ID,
711 .subdevice = PCI_ANY_ID,
712 .driver_data = (kernel_ulong_t)&sdhci_o2,
713 },
714
715 {
716 .vendor = PCI_VENDOR_ID_O2,
717 .device = PCI_DEVICE_ID_O2_8220,
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_8221,
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_8320,
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_8321,
742 .subvendor = PCI_ANY_ID,
743 .subdevice = PCI_ANY_ID,
744 .driver_data = (kernel_ulong_t)&sdhci_o2,
745 },
746
Pierre Ossman22606402008-03-23 19:33:23 +0100747 { /* Generic SD host controller */
748 PCI_DEVICE_CLASS((PCI_CLASS_SYSTEM_SDHCI << 8), 0xFFFF00)
749 },
750
751 { /* end: all zeroes */ },
752};
753
754MODULE_DEVICE_TABLE(pci, pci_ids);
755
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100756/*****************************************************************************\
757 * *
758 * SDHCI core callbacks *
759 * *
760\*****************************************************************************/
761
762static int sdhci_pci_enable_dma(struct sdhci_host *host)
763{
764 struct sdhci_pci_slot *slot;
765 struct pci_dev *pdev;
766 int ret;
767
768 slot = sdhci_priv(host);
769 pdev = slot->chip->pdev;
770
771 if (((pdev->class & 0xFFFF00) == (PCI_CLASS_SYSTEM_SDHCI << 8)) &&
772 ((pdev->class & 0x0000FF) != PCI_SDHCI_IFDMA) &&
Richard Röjforsa13abc72009-09-22 16:45:30 -0700773 (host->flags & SDHCI_USE_SDMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100774 dev_warn(&pdev->dev, "Will use DMA mode even though HW "
775 "doesn't fully claim to support it.\n");
776 }
777
Yang Hongyang284901a2009-04-06 19:01:15 -0700778 ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100779 if (ret)
780 return ret;
781
782 pci_set_master(pdev);
783
784 return 0;
785}
786
787static struct sdhci_ops sdhci_pci_ops = {
788 .enable_dma = sdhci_pci_enable_dma,
789};
790
791/*****************************************************************************\
792 * *
793 * Suspend/resume *
794 * *
795\*****************************************************************************/
796
797#ifdef CONFIG_PM
798
Ameya Palandeb177bc92011-04-05 21:13:13 +0300799static int sdhci_pci_suspend(struct pci_dev *pdev, pm_message_t state)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100800{
801 struct sdhci_pci_chip *chip;
802 struct sdhci_pci_slot *slot;
Daniel Drake5f619702010-11-04 22:20:39 +0000803 mmc_pm_flag_t slot_pm_flags;
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800804 mmc_pm_flag_t pm_flags = 0;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100805 int i, ret;
806
807 chip = pci_get_drvdata(pdev);
808 if (!chip)
809 return 0;
810
Ameya Palandeb177bc92011-04-05 21:13:13 +0300811 for (i = 0; i < chip->num_slots; i++) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100812 slot = chip->slots[i];
813 if (!slot)
814 continue;
815
816 ret = sdhci_suspend_host(slot->host, state);
817
818 if (ret) {
Ameya Palandeb177bc92011-04-05 21:13:13 +0300819 for (i--; i >= 0; i--)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100820 sdhci_resume_host(chip->slots[i]->host);
821 return ret;
822 }
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800823
Daniel Drake5f619702010-11-04 22:20:39 +0000824 slot_pm_flags = slot->host->mmc->pm_flags;
825 if (slot_pm_flags & MMC_PM_WAKE_SDIO_IRQ)
826 sdhci_enable_irq_wakeups(slot->host);
827
828 pm_flags |= slot_pm_flags;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100829 }
830
Pierre Ossman44894282008-04-04 19:36:59 +0200831 if (chip->fixes && chip->fixes->suspend) {
832 ret = chip->fixes->suspend(chip, state);
833 if (ret) {
Ameya Palandeb177bc92011-04-05 21:13:13 +0300834 for (i = chip->num_slots - 1; i >= 0; i--)
Pierre Ossman44894282008-04-04 19:36:59 +0200835 sdhci_resume_host(chip->slots[i]->host);
836 return ret;
837 }
838 }
839
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100840 pci_save_state(pdev);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800841 if (pm_flags & MMC_PM_KEEP_POWER) {
Daniel Drake5f619702010-11-04 22:20:39 +0000842 if (pm_flags & MMC_PM_WAKE_SDIO_IRQ) {
843 pci_pme_active(pdev, true);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800844 pci_enable_wake(pdev, PCI_D3hot, 1);
Daniel Drake5f619702010-11-04 22:20:39 +0000845 }
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800846 pci_set_power_state(pdev, PCI_D3hot);
847 } else {
848 pci_enable_wake(pdev, pci_choose_state(pdev, state), 0);
849 pci_disable_device(pdev);
850 pci_set_power_state(pdev, pci_choose_state(pdev, state));
851 }
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100852
853 return 0;
854}
855
Ameya Palandeb177bc92011-04-05 21:13:13 +0300856static int sdhci_pci_resume(struct pci_dev *pdev)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100857{
858 struct sdhci_pci_chip *chip;
859 struct sdhci_pci_slot *slot;
860 int i, ret;
861
862 chip = pci_get_drvdata(pdev);
863 if (!chip)
864 return 0;
865
866 pci_set_power_state(pdev, PCI_D0);
867 pci_restore_state(pdev);
868 ret = pci_enable_device(pdev);
869 if (ret)
870 return ret;
871
Pierre Ossman45211e22008-03-24 13:09:09 +0100872 if (chip->fixes && chip->fixes->resume) {
873 ret = chip->fixes->resume(chip);
874 if (ret)
875 return ret;
876 }
877
Ameya Palandeb177bc92011-04-05 21:13:13 +0300878 for (i = 0; i < chip->num_slots; i++) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100879 slot = chip->slots[i];
880 if (!slot)
881 continue;
882
883 ret = sdhci_resume_host(slot->host);
884 if (ret)
885 return ret;
886 }
887
888 return 0;
889}
890
891#else /* CONFIG_PM */
892
893#define sdhci_pci_suspend NULL
894#define sdhci_pci_resume NULL
895
896#endif /* CONFIG_PM */
897
898/*****************************************************************************\
899 * *
900 * Device probing/removal *
901 * *
902\*****************************************************************************/
903
904static struct sdhci_pci_slot * __devinit sdhci_pci_probe_slot(
905 struct pci_dev *pdev, struct sdhci_pci_chip *chip, int bar)
906{
907 struct sdhci_pci_slot *slot;
908 struct sdhci_host *host;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100909 int ret;
910
911 if (!(pci_resource_flags(pdev, bar) & IORESOURCE_MEM)) {
912 dev_err(&pdev->dev, "BAR %d is not iomem. Aborting.\n", bar);
913 return ERR_PTR(-ENODEV);
914 }
915
916 if (pci_resource_len(pdev, bar) != 0x100) {
917 dev_err(&pdev->dev, "Invalid iomem size. You may "
918 "experience problems.\n");
919 }
920
921 if ((pdev->class & 0x0000FF) == PCI_SDHCI_IFVENDOR) {
922 dev_err(&pdev->dev, "Vendor specific interface. Aborting.\n");
923 return ERR_PTR(-ENODEV);
924 }
925
926 if ((pdev->class & 0x0000FF) > PCI_SDHCI_IFVENDOR) {
927 dev_err(&pdev->dev, "Unknown interface. Aborting.\n");
928 return ERR_PTR(-ENODEV);
929 }
930
931 host = sdhci_alloc_host(&pdev->dev, sizeof(struct sdhci_pci_slot));
932 if (IS_ERR(host)) {
Dan Carpenterc60a32c2009-04-10 23:31:10 +0200933 dev_err(&pdev->dev, "cannot allocate host\n");
Julia Lawalldc0fd7b2010-05-26 14:42:11 -0700934 return ERR_CAST(host);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100935 }
936
937 slot = sdhci_priv(host);
938
939 slot->chip = chip;
940 slot->host = host;
941 slot->pci_bar = bar;
942
943 host->hw_name = "PCI";
944 host->ops = &sdhci_pci_ops;
945 host->quirks = chip->quirks;
946
947 host->irq = pdev->irq;
948
949 ret = pci_request_region(pdev, bar, mmc_hostname(host->mmc));
950 if (ret) {
951 dev_err(&pdev->dev, "cannot request region\n");
Dan Carpenterc60a32c2009-04-10 23:31:10 +0200952 goto free;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100953 }
954
Arjan van de Ven092f82e2008-09-28 16:15:56 -0700955 host->ioaddr = pci_ioremap_bar(pdev, bar);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100956 if (!host->ioaddr) {
957 dev_err(&pdev->dev, "failed to remap registers\n");
Chris Ball9fdcdbb2011-03-29 00:46:12 -0400958 ret = -ENOMEM;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100959 goto release;
960 }
961
Pierre Ossman44894282008-04-04 19:36:59 +0200962 if (chip->fixes && chip->fixes->probe_slot) {
963 ret = chip->fixes->probe_slot(slot);
964 if (ret)
965 goto unmap;
966 }
967
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800968 host->mmc->pm_caps = MMC_PM_KEEP_POWER | MMC_PM_WAKE_SDIO_IRQ;
969
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100970 ret = sdhci_add_host(host);
971 if (ret)
Pierre Ossman44894282008-04-04 19:36:59 +0200972 goto remove;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100973
974 return slot;
975
Pierre Ossman44894282008-04-04 19:36:59 +0200976remove:
977 if (chip->fixes && chip->fixes->remove_slot)
Pierre Ossman1e728592008-04-16 19:13:13 +0200978 chip->fixes->remove_slot(slot, 0);
Pierre Ossman44894282008-04-04 19:36:59 +0200979
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100980unmap:
981 iounmap(host->ioaddr);
982
983release:
984 pci_release_region(pdev, bar);
Dan Carpenterc60a32c2009-04-10 23:31:10 +0200985
986free:
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100987 sdhci_free_host(host);
988
989 return ERR_PTR(ret);
990}
991
992static void sdhci_pci_remove_slot(struct sdhci_pci_slot *slot)
993{
Pierre Ossman1e728592008-04-16 19:13:13 +0200994 int dead;
995 u32 scratch;
996
997 dead = 0;
998 scratch = readl(slot->host->ioaddr + SDHCI_INT_STATUS);
999 if (scratch == (u32)-1)
1000 dead = 1;
1001
1002 sdhci_remove_host(slot->host, dead);
Pierre Ossman44894282008-04-04 19:36:59 +02001003
1004 if (slot->chip->fixes && slot->chip->fixes->remove_slot)
Pierre Ossman1e728592008-04-16 19:13:13 +02001005 slot->chip->fixes->remove_slot(slot, dead);
Pierre Ossman44894282008-04-04 19:36:59 +02001006
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001007 pci_release_region(slot->chip->pdev, slot->pci_bar);
Pierre Ossman44894282008-04-04 19:36:59 +02001008
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001009 sdhci_free_host(slot->host);
1010}
1011
1012static int __devinit sdhci_pci_probe(struct pci_dev *pdev,
1013 const struct pci_device_id *ent)
1014{
1015 struct sdhci_pci_chip *chip;
1016 struct sdhci_pci_slot *slot;
1017
Sergei Shtylyovcf5e23e2011-03-17 16:46:17 -04001018 u8 slots, first_bar;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001019 int ret, i;
1020
1021 BUG_ON(pdev == NULL);
1022 BUG_ON(ent == NULL);
1023
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001024 dev_info(&pdev->dev, "SDHCI controller found [%04x:%04x] (rev %x)\n",
Sergei Shtylyovcf5e23e2011-03-17 16:46:17 -04001025 (int)pdev->vendor, (int)pdev->device, (int)pdev->revision);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001026
1027 ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &slots);
1028 if (ret)
1029 return ret;
1030
1031 slots = PCI_SLOT_INFO_SLOTS(slots) + 1;
1032 dev_dbg(&pdev->dev, "found %d slot(s)\n", slots);
1033 if (slots == 0)
1034 return -ENODEV;
1035
1036 BUG_ON(slots > MAX_SLOTS);
1037
1038 ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &first_bar);
1039 if (ret)
1040 return ret;
1041
1042 first_bar &= PCI_SLOT_INFO_FIRST_BAR_MASK;
1043
1044 if (first_bar > 5) {
1045 dev_err(&pdev->dev, "Invalid first BAR. Aborting.\n");
1046 return -ENODEV;
1047 }
1048
1049 ret = pci_enable_device(pdev);
1050 if (ret)
1051 return ret;
1052
1053 chip = kzalloc(sizeof(struct sdhci_pci_chip), GFP_KERNEL);
1054 if (!chip) {
1055 ret = -ENOMEM;
1056 goto err;
1057 }
1058
1059 chip->pdev = pdev;
Ameya Palandeb177bc92011-04-05 21:13:13 +03001060 chip->fixes = (const struct sdhci_pci_fixes *)ent->driver_data;
Pierre Ossman22606402008-03-23 19:33:23 +01001061 if (chip->fixes)
1062 chip->quirks = chip->fixes->quirks;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001063 chip->num_slots = slots;
1064
1065 pci_set_drvdata(pdev, chip);
1066
Pierre Ossman22606402008-03-23 19:33:23 +01001067 if (chip->fixes && chip->fixes->probe) {
1068 ret = chip->fixes->probe(chip);
1069 if (ret)
1070 goto free;
1071 }
1072
Alan Cox225d85f2010-10-04 15:24:21 +01001073 slots = chip->num_slots; /* Quirk may have changed this */
1074
Ameya Palandeb177bc92011-04-05 21:13:13 +03001075 for (i = 0; i < slots; i++) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001076 slot = sdhci_pci_probe_slot(pdev, chip, first_bar + i);
1077 if (IS_ERR(slot)) {
Ameya Palandeb177bc92011-04-05 21:13:13 +03001078 for (i--; i >= 0; i--)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001079 sdhci_pci_remove_slot(chip->slots[i]);
1080 ret = PTR_ERR(slot);
1081 goto free;
1082 }
1083
1084 chip->slots[i] = slot;
1085 }
1086
1087 return 0;
1088
1089free:
1090 pci_set_drvdata(pdev, NULL);
1091 kfree(chip);
1092
1093err:
1094 pci_disable_device(pdev);
1095 return ret;
1096}
1097
1098static void __devexit sdhci_pci_remove(struct pci_dev *pdev)
1099{
1100 int i;
1101 struct sdhci_pci_chip *chip;
1102
1103 chip = pci_get_drvdata(pdev);
1104
1105 if (chip) {
Ameya Palandeb177bc92011-04-05 21:13:13 +03001106 for (i = 0; i < chip->num_slots; i++)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001107 sdhci_pci_remove_slot(chip->slots[i]);
1108
1109 pci_set_drvdata(pdev, NULL);
1110 kfree(chip);
1111 }
1112
1113 pci_disable_device(pdev);
1114}
1115
1116static struct pci_driver sdhci_driver = {
Ameya Palandeb177bc92011-04-05 21:13:13 +03001117 .name = "sdhci-pci",
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001118 .id_table = pci_ids,
Ameya Palandeb177bc92011-04-05 21:13:13 +03001119 .probe = sdhci_pci_probe,
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001120 .remove = __devexit_p(sdhci_pci_remove),
1121 .suspend = sdhci_pci_suspend,
1122 .resume = sdhci_pci_resume,
1123};
1124
1125/*****************************************************************************\
1126 * *
1127 * Driver init/exit *
1128 * *
1129\*****************************************************************************/
1130
1131static int __init sdhci_drv_init(void)
1132{
1133 return pci_register_driver(&sdhci_driver);
1134}
1135
1136static void __exit sdhci_drv_exit(void)
1137{
1138 pci_unregister_driver(&sdhci_driver);
1139}
1140
1141module_init(sdhci_drv_init);
1142module_exit(sdhci_drv_exit);
1143
Pierre Ossman32710e82009-04-08 20:14:54 +02001144MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001145MODULE_DESCRIPTION("Secure Digital Host Controller Interface PCI driver");
1146MODULE_LICENSE("GPL");