blob: 67c87097af79497170c9ceaaa7c52bc3921cf2bc [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
243 return 0;
244}
245
Pierre Ossman45211e22008-03-24 13:09:09 +0100246static int jmicron_pmos(struct sdhci_pci_chip *chip, int on)
247{
248 u8 scratch;
249 int ret;
250
251 ret = pci_read_config_byte(chip->pdev, 0xAE, &scratch);
252 if (ret)
253 return ret;
254
255 /*
256 * Turn PMOS on [bit 0], set over current detection to 2.4 V
257 * [bit 1:2] and enable over current debouncing [bit 6].
258 */
259 if (on)
260 scratch |= 0x47;
261 else
262 scratch &= ~0x47;
263
264 ret = pci_write_config_byte(chip->pdev, 0xAE, scratch);
265 if (ret)
266 return ret;
267
268 return 0;
269}
270
271static int jmicron_probe(struct sdhci_pci_chip *chip)
272{
273 int ret;
Takashi Iwai8f230f42010-12-08 10:04:30 +0100274 u16 mmcdev = 0;
Pierre Ossman45211e22008-03-24 13:09:09 +0100275
Pierre Ossman93fc48c2008-06-28 18:21:41 +0200276 if (chip->pdev->revision == 0) {
277 chip->quirks |= SDHCI_QUIRK_32BIT_DMA_ADDR |
278 SDHCI_QUIRK_32BIT_DMA_SIZE |
Pierre Ossman2134a922008-06-28 18:28:51 +0200279 SDHCI_QUIRK_32BIT_ADMA_SIZE |
Pierre Ossman4a3cba32008-07-29 00:11:16 +0200280 SDHCI_QUIRK_RESET_AFTER_REQUEST |
Pierre Ossman86a6a872009-02-02 21:13:49 +0100281 SDHCI_QUIRK_BROKEN_SMALL_PIO;
Pierre Ossman93fc48c2008-06-28 18:21:41 +0200282 }
283
Pierre Ossman45211e22008-03-24 13:09:09 +0100284 /*
Pierre Ossman44894282008-04-04 19:36:59 +0200285 * JMicron chips can have two interfaces to the same hardware
286 * in order to work around limitations in Microsoft's driver.
287 * We need to make sure we only bind to one of them.
288 *
289 * This code assumes two things:
290 *
291 * 1. The PCI code adds subfunctions in order.
292 *
293 * 2. The MMC interface has a lower subfunction number
294 * than the SD interface.
295 */
Takashi Iwai8f230f42010-12-08 10:04:30 +0100296 if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_SD)
297 mmcdev = PCI_DEVICE_ID_JMICRON_JMB38X_MMC;
298 else if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_SD)
299 mmcdev = PCI_DEVICE_ID_JMICRON_JMB388_ESD;
300
301 if (mmcdev) {
Pierre Ossman44894282008-04-04 19:36:59 +0200302 struct pci_dev *sd_dev;
303
304 sd_dev = NULL;
305 while ((sd_dev = pci_get_device(PCI_VENDOR_ID_JMICRON,
Takashi Iwai8f230f42010-12-08 10:04:30 +0100306 mmcdev, sd_dev)) != NULL) {
Pierre Ossman44894282008-04-04 19:36:59 +0200307 if ((PCI_SLOT(chip->pdev->devfn) ==
308 PCI_SLOT(sd_dev->devfn)) &&
309 (chip->pdev->bus == sd_dev->bus))
310 break;
311 }
312
313 if (sd_dev) {
314 pci_dev_put(sd_dev);
315 dev_info(&chip->pdev->dev, "Refusing to bind to "
316 "secondary interface.\n");
317 return -ENODEV;
318 }
319 }
320
321 /*
Pierre Ossman45211e22008-03-24 13:09:09 +0100322 * JMicron chips need a bit of a nudge to enable the power
323 * output pins.
324 */
325 ret = jmicron_pmos(chip, 1);
326 if (ret) {
327 dev_err(&chip->pdev->dev, "Failure enabling card power\n");
328 return ret;
329 }
330
Takashi Iwai82b0e232011-04-21 20:26:38 +0200331 /* quirk for unsable RO-detection on JM388 chips */
332 if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_SD ||
333 chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD)
334 chip->quirks |= SDHCI_QUIRK_UNSTABLE_RO_DETECT;
335
Pierre Ossman45211e22008-03-24 13:09:09 +0100336 return 0;
337}
338
Pierre Ossman44894282008-04-04 19:36:59 +0200339static void jmicron_enable_mmc(struct sdhci_host *host, int on)
340{
341 u8 scratch;
342
343 scratch = readb(host->ioaddr + 0xC0);
344
345 if (on)
346 scratch |= 0x01;
347 else
348 scratch &= ~0x01;
349
350 writeb(scratch, host->ioaddr + 0xC0);
351}
352
353static int jmicron_probe_slot(struct sdhci_pci_slot *slot)
354{
Pierre Ossman2134a922008-06-28 18:28:51 +0200355 if (slot->chip->pdev->revision == 0) {
356 u16 version;
357
358 version = readl(slot->host->ioaddr + SDHCI_HOST_VERSION);
359 version = (version & SDHCI_VENDOR_VER_MASK) >>
360 SDHCI_VENDOR_VER_SHIFT;
361
362 /*
363 * Older versions of the chip have lots of nasty glitches
364 * in the ADMA engine. It's best just to avoid it
365 * completely.
366 */
367 if (version < 0xAC)
368 slot->host->quirks |= SDHCI_QUIRK_BROKEN_ADMA;
369 }
370
Takashi Iwai8f230f42010-12-08 10:04:30 +0100371 /* JM388 MMC doesn't support 1.8V while SD supports it */
372 if (slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD) {
373 slot->host->ocr_avail_sd = MMC_VDD_32_33 | MMC_VDD_33_34 |
374 MMC_VDD_29_30 | MMC_VDD_30_31 |
375 MMC_VDD_165_195; /* allow 1.8V */
376 slot->host->ocr_avail_mmc = MMC_VDD_32_33 | MMC_VDD_33_34 |
377 MMC_VDD_29_30 | MMC_VDD_30_31; /* no 1.8V for MMC */
378 }
379
Pierre Ossman44894282008-04-04 19:36:59 +0200380 /*
381 * The secondary interface requires a bit set to get the
382 * interrupts.
383 */
Takashi Iwai8f230f42010-12-08 10:04:30 +0100384 if (slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC ||
385 slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD)
Pierre Ossman44894282008-04-04 19:36:59 +0200386 jmicron_enable_mmc(slot->host, 1);
387
Takashi Iwaid75c1082010-12-16 17:54:14 +0100388 slot->host->mmc->caps |= MMC_CAP_BUS_WIDTH_TEST;
389
Pierre Ossman44894282008-04-04 19:36:59 +0200390 return 0;
391}
392
Pierre Ossman1e728592008-04-16 19:13:13 +0200393static void jmicron_remove_slot(struct sdhci_pci_slot *slot, int dead)
Pierre Ossman44894282008-04-04 19:36:59 +0200394{
Pierre Ossman1e728592008-04-16 19:13:13 +0200395 if (dead)
396 return;
397
Takashi Iwai8f230f42010-12-08 10:04:30 +0100398 if (slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC ||
399 slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD)
Pierre Ossman44894282008-04-04 19:36:59 +0200400 jmicron_enable_mmc(slot->host, 0);
401}
402
403static int jmicron_suspend(struct sdhci_pci_chip *chip, pm_message_t state)
404{
405 int i;
406
Takashi Iwai8f230f42010-12-08 10:04:30 +0100407 if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC ||
408 chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD) {
Ameya Palandeb177bc92011-04-05 21:13:13 +0300409 for (i = 0; i < chip->num_slots; i++)
Pierre Ossman44894282008-04-04 19:36:59 +0200410 jmicron_enable_mmc(chip->slots[i]->host, 0);
411 }
412
413 return 0;
414}
415
Pierre Ossman45211e22008-03-24 13:09:09 +0100416static int jmicron_resume(struct sdhci_pci_chip *chip)
417{
Pierre Ossman44894282008-04-04 19:36:59 +0200418 int ret, i;
419
Takashi Iwai8f230f42010-12-08 10:04:30 +0100420 if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC ||
421 chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD) {
Ameya Palandeb177bc92011-04-05 21:13:13 +0300422 for (i = 0; i < chip->num_slots; i++)
Pierre Ossman44894282008-04-04 19:36:59 +0200423 jmicron_enable_mmc(chip->slots[i]->host, 1);
424 }
Pierre Ossman45211e22008-03-24 13:09:09 +0100425
426 ret = jmicron_pmos(chip, 1);
427 if (ret) {
428 dev_err(&chip->pdev->dev, "Failure enabling card power\n");
429 return ret;
430 }
431
432 return 0;
433}
434
Jennifer Li26daa1e2010-11-17 23:01:59 -0500435static const struct sdhci_pci_fixes sdhci_o2 = {
436 .probe = o2_probe,
437};
438
Pierre Ossman22606402008-03-23 19:33:23 +0100439static const struct sdhci_pci_fixes sdhci_jmicron = {
Pierre Ossman45211e22008-03-24 13:09:09 +0100440 .probe = jmicron_probe,
441
Pierre Ossman44894282008-04-04 19:36:59 +0200442 .probe_slot = jmicron_probe_slot,
443 .remove_slot = jmicron_remove_slot,
444
445 .suspend = jmicron_suspend,
Pierre Ossman45211e22008-03-24 13:09:09 +0100446 .resume = jmicron_resume,
Pierre Ossman22606402008-03-23 19:33:23 +0100447};
448
Nicolas Pitrea7a61862009-12-14 18:01:26 -0800449/* SysKonnect CardBus2SDIO extra registers */
450#define SYSKT_CTRL 0x200
451#define SYSKT_RDFIFO_STAT 0x204
452#define SYSKT_WRFIFO_STAT 0x208
453#define SYSKT_POWER_DATA 0x20c
454#define SYSKT_POWER_330 0xef
455#define SYSKT_POWER_300 0xf8
456#define SYSKT_POWER_184 0xcc
457#define SYSKT_POWER_CMD 0x20d
458#define SYSKT_POWER_START (1 << 7)
459#define SYSKT_POWER_STATUS 0x20e
460#define SYSKT_POWER_STATUS_OK (1 << 0)
461#define SYSKT_BOARD_REV 0x210
462#define SYSKT_CHIP_REV 0x211
463#define SYSKT_CONF_DATA 0x212
464#define SYSKT_CONF_DATA_1V8 (1 << 2)
465#define SYSKT_CONF_DATA_2V5 (1 << 1)
466#define SYSKT_CONF_DATA_3V3 (1 << 0)
467
468static int syskt_probe(struct sdhci_pci_chip *chip)
469{
470 if ((chip->pdev->class & 0x0000FF) == PCI_SDHCI_IFVENDOR) {
471 chip->pdev->class &= ~0x0000FF;
472 chip->pdev->class |= PCI_SDHCI_IFDMA;
473 }
474 return 0;
475}
476
477static int syskt_probe_slot(struct sdhci_pci_slot *slot)
478{
479 int tm, ps;
480
481 u8 board_rev = readb(slot->host->ioaddr + SYSKT_BOARD_REV);
482 u8 chip_rev = readb(slot->host->ioaddr + SYSKT_CHIP_REV);
483 dev_info(&slot->chip->pdev->dev, "SysKonnect CardBus2SDIO, "
484 "board rev %d.%d, chip rev %d.%d\n",
485 board_rev >> 4, board_rev & 0xf,
486 chip_rev >> 4, chip_rev & 0xf);
487 if (chip_rev >= 0x20)
488 slot->host->quirks |= SDHCI_QUIRK_FORCE_DMA;
489
490 writeb(SYSKT_POWER_330, slot->host->ioaddr + SYSKT_POWER_DATA);
491 writeb(SYSKT_POWER_START, slot->host->ioaddr + SYSKT_POWER_CMD);
492 udelay(50);
493 tm = 10; /* Wait max 1 ms */
494 do {
495 ps = readw(slot->host->ioaddr + SYSKT_POWER_STATUS);
496 if (ps & SYSKT_POWER_STATUS_OK)
497 break;
498 udelay(100);
499 } while (--tm);
500 if (!tm) {
501 dev_err(&slot->chip->pdev->dev,
502 "power regulator never stabilized");
503 writeb(0, slot->host->ioaddr + SYSKT_POWER_CMD);
504 return -ENODEV;
505 }
506
507 return 0;
508}
509
510static const struct sdhci_pci_fixes sdhci_syskt = {
511 .quirks = SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER,
512 .probe = syskt_probe,
513 .probe_slot = syskt_probe_slot,
514};
515
Harald Welte557b0692009-06-18 16:53:38 +0200516static int via_probe(struct sdhci_pci_chip *chip)
517{
518 if (chip->pdev->revision == 0x10)
519 chip->quirks |= SDHCI_QUIRK_DELAY_AFTER_POWER;
520
521 return 0;
522}
523
524static const struct sdhci_pci_fixes sdhci_via = {
525 .probe = via_probe,
526};
527
Pierre Ossman22606402008-03-23 19:33:23 +0100528static const struct pci_device_id pci_ids[] __devinitdata = {
529 {
530 .vendor = PCI_VENDOR_ID_RICOH,
531 .device = PCI_DEVICE_ID_RICOH_R5C822,
532 .subvendor = PCI_ANY_ID,
533 .subdevice = PCI_ANY_ID,
534 .driver_data = (kernel_ulong_t)&sdhci_ricoh,
535 },
536
537 {
Maxim Levitskyccc92c22010-08-10 18:01:42 -0700538 .vendor = PCI_VENDOR_ID_RICOH,
539 .device = 0x843,
540 .subvendor = PCI_ANY_ID,
541 .subdevice = PCI_ANY_ID,
542 .driver_data = (kernel_ulong_t)&sdhci_ricoh_mmc,
543 },
544
545 {
Pablo Castillo568133e2010-08-10 18:02:01 -0700546 .vendor = PCI_VENDOR_ID_RICOH,
547 .device = 0xe822,
548 .subvendor = PCI_ANY_ID,
549 .subdevice = PCI_ANY_ID,
550 .driver_data = (kernel_ulong_t)&sdhci_ricoh_mmc,
551 },
552
553 {
Manoj Iyer5fd11c02011-02-11 16:25:31 -0600554 .vendor = PCI_VENDOR_ID_RICOH,
555 .device = 0xe823,
556 .subvendor = PCI_ANY_ID,
557 .subdevice = PCI_ANY_ID,
558 .driver_data = (kernel_ulong_t)&sdhci_ricoh_mmc,
559 },
560
561 {
Pierre Ossman22606402008-03-23 19:33:23 +0100562 .vendor = PCI_VENDOR_ID_ENE,
563 .device = PCI_DEVICE_ID_ENE_CB712_SD,
564 .subvendor = PCI_ANY_ID,
565 .subdevice = PCI_ANY_ID,
566 .driver_data = (kernel_ulong_t)&sdhci_ene_712,
567 },
568
569 {
570 .vendor = PCI_VENDOR_ID_ENE,
571 .device = PCI_DEVICE_ID_ENE_CB712_SD_2,
572 .subvendor = PCI_ANY_ID,
573 .subdevice = PCI_ANY_ID,
574 .driver_data = (kernel_ulong_t)&sdhci_ene_712,
575 },
576
577 {
578 .vendor = PCI_VENDOR_ID_ENE,
579 .device = PCI_DEVICE_ID_ENE_CB714_SD,
580 .subvendor = PCI_ANY_ID,
581 .subdevice = PCI_ANY_ID,
582 .driver_data = (kernel_ulong_t)&sdhci_ene_714,
583 },
584
585 {
586 .vendor = PCI_VENDOR_ID_ENE,
587 .device = PCI_DEVICE_ID_ENE_CB714_SD_2,
588 .subvendor = PCI_ANY_ID,
589 .subdevice = PCI_ANY_ID,
590 .driver_data = (kernel_ulong_t)&sdhci_ene_714,
591 },
592
593 {
594 .vendor = PCI_VENDOR_ID_MARVELL,
David Woodhouse8c5eb882008-09-03 09:45:57 +0100595 .device = PCI_DEVICE_ID_MARVELL_88ALP01_SD,
Pierre Ossman22606402008-03-23 19:33:23 +0100596 .subvendor = PCI_ANY_ID,
597 .subdevice = PCI_ANY_ID,
598 .driver_data = (kernel_ulong_t)&sdhci_cafe,
599 },
600
601 {
602 .vendor = PCI_VENDOR_ID_JMICRON,
603 .device = PCI_DEVICE_ID_JMICRON_JMB38X_SD,
604 .subvendor = PCI_ANY_ID,
605 .subdevice = PCI_ANY_ID,
606 .driver_data = (kernel_ulong_t)&sdhci_jmicron,
607 },
608
Pierre Ossman44894282008-04-04 19:36:59 +0200609 {
610 .vendor = PCI_VENDOR_ID_JMICRON,
611 .device = PCI_DEVICE_ID_JMICRON_JMB38X_MMC,
612 .subvendor = PCI_ANY_ID,
613 .subdevice = PCI_ANY_ID,
614 .driver_data = (kernel_ulong_t)&sdhci_jmicron,
615 },
616
Harald Welte557b0692009-06-18 16:53:38 +0200617 {
Takashi Iwai8f230f42010-12-08 10:04:30 +0100618 .vendor = PCI_VENDOR_ID_JMICRON,
619 .device = PCI_DEVICE_ID_JMICRON_JMB388_SD,
620 .subvendor = PCI_ANY_ID,
621 .subdevice = PCI_ANY_ID,
622 .driver_data = (kernel_ulong_t)&sdhci_jmicron,
623 },
624
625 {
626 .vendor = PCI_VENDOR_ID_JMICRON,
627 .device = PCI_DEVICE_ID_JMICRON_JMB388_ESD,
628 .subvendor = PCI_ANY_ID,
629 .subdevice = PCI_ANY_ID,
630 .driver_data = (kernel_ulong_t)&sdhci_jmicron,
631 },
632
633 {
Nicolas Pitrea7a61862009-12-14 18:01:26 -0800634 .vendor = PCI_VENDOR_ID_SYSKONNECT,
635 .device = 0x8000,
636 .subvendor = PCI_ANY_ID,
637 .subdevice = PCI_ANY_ID,
638 .driver_data = (kernel_ulong_t)&sdhci_syskt,
639 },
640
641 {
Harald Welte557b0692009-06-18 16:53:38 +0200642 .vendor = PCI_VENDOR_ID_VIA,
643 .device = 0x95d0,
644 .subvendor = PCI_ANY_ID,
645 .subdevice = PCI_ANY_ID,
646 .driver_data = (kernel_ulong_t)&sdhci_via,
647 },
648
Xiaochen Shen29229052010-10-04 15:24:52 +0100649 {
650 .vendor = PCI_VENDOR_ID_INTEL,
Alan Coxf9ee3ea2010-10-04 15:25:11 +0100651 .device = PCI_DEVICE_ID_INTEL_MRST_SD0,
652 .subvendor = PCI_ANY_ID,
653 .subdevice = PCI_ANY_ID,
654 .driver_data = (kernel_ulong_t)&sdhci_intel_mrst_hc0,
655 },
656
657 {
658 .vendor = PCI_VENDOR_ID_INTEL,
659 .device = PCI_DEVICE_ID_INTEL_MRST_SD1,
660 .subvendor = PCI_ANY_ID,
661 .subdevice = PCI_ANY_ID,
Jacob Pan35ac6f02010-11-09 13:57:29 +0000662 .driver_data = (kernel_ulong_t)&sdhci_intel_mrst_hc1_hc2,
663 },
664
665 {
666 .vendor = PCI_VENDOR_ID_INTEL,
667 .device = PCI_DEVICE_ID_INTEL_MRST_SD2,
668 .subvendor = PCI_ANY_ID,
669 .subdevice = PCI_ANY_ID,
670 .driver_data = (kernel_ulong_t)&sdhci_intel_mrst_hc1_hc2,
Alan Coxf9ee3ea2010-10-04 15:25:11 +0100671 },
672
673 {
674 .vendor = PCI_VENDOR_ID_INTEL,
Xiaochen Shen29229052010-10-04 15:24:52 +0100675 .device = PCI_DEVICE_ID_INTEL_MFD_SD,
676 .subvendor = PCI_ANY_ID,
677 .subdevice = PCI_ANY_ID,
678 .driver_data = (kernel_ulong_t)&sdhci_intel_mfd_sd,
679 },
680
681 {
682 .vendor = PCI_VENDOR_ID_INTEL,
683 .device = PCI_DEVICE_ID_INTEL_MFD_SDIO1,
684 .subvendor = PCI_ANY_ID,
685 .subdevice = PCI_ANY_ID,
686 .driver_data = (kernel_ulong_t)&sdhci_intel_mfd_emmc_sdio,
687 },
688
689 {
690 .vendor = PCI_VENDOR_ID_INTEL,
691 .device = PCI_DEVICE_ID_INTEL_MFD_SDIO2,
692 .subvendor = PCI_ANY_ID,
693 .subdevice = PCI_ANY_ID,
694 .driver_data = (kernel_ulong_t)&sdhci_intel_mfd_emmc_sdio,
695 },
696
697 {
698 .vendor = PCI_VENDOR_ID_INTEL,
699 .device = PCI_DEVICE_ID_INTEL_MFD_EMMC0,
700 .subvendor = PCI_ANY_ID,
701 .subdevice = PCI_ANY_ID,
702 .driver_data = (kernel_ulong_t)&sdhci_intel_mfd_emmc_sdio,
703 },
704
705 {
706 .vendor = PCI_VENDOR_ID_INTEL,
707 .device = PCI_DEVICE_ID_INTEL_MFD_EMMC1,
708 .subvendor = PCI_ANY_ID,
709 .subdevice = PCI_ANY_ID,
710 .driver_data = (kernel_ulong_t)&sdhci_intel_mfd_emmc_sdio,
711 },
712
Jennifer Li26daa1e2010-11-17 23:01:59 -0500713 {
714 .vendor = PCI_VENDOR_ID_O2,
715 .device = PCI_DEVICE_ID_O2_8120,
716 .subvendor = PCI_ANY_ID,
717 .subdevice = PCI_ANY_ID,
718 .driver_data = (kernel_ulong_t)&sdhci_o2,
719 },
720
721 {
722 .vendor = PCI_VENDOR_ID_O2,
723 .device = PCI_DEVICE_ID_O2_8220,
724 .subvendor = PCI_ANY_ID,
725 .subdevice = PCI_ANY_ID,
726 .driver_data = (kernel_ulong_t)&sdhci_o2,
727 },
728
729 {
730 .vendor = PCI_VENDOR_ID_O2,
731 .device = PCI_DEVICE_ID_O2_8221,
732 .subvendor = PCI_ANY_ID,
733 .subdevice = PCI_ANY_ID,
734 .driver_data = (kernel_ulong_t)&sdhci_o2,
735 },
736
737 {
738 .vendor = PCI_VENDOR_ID_O2,
739 .device = PCI_DEVICE_ID_O2_8320,
740 .subvendor = PCI_ANY_ID,
741 .subdevice = PCI_ANY_ID,
742 .driver_data = (kernel_ulong_t)&sdhci_o2,
743 },
744
745 {
746 .vendor = PCI_VENDOR_ID_O2,
747 .device = PCI_DEVICE_ID_O2_8321,
748 .subvendor = PCI_ANY_ID,
749 .subdevice = PCI_ANY_ID,
750 .driver_data = (kernel_ulong_t)&sdhci_o2,
751 },
752
Pierre Ossman22606402008-03-23 19:33:23 +0100753 { /* Generic SD host controller */
754 PCI_DEVICE_CLASS((PCI_CLASS_SYSTEM_SDHCI << 8), 0xFFFF00)
755 },
756
757 { /* end: all zeroes */ },
758};
759
760MODULE_DEVICE_TABLE(pci, pci_ids);
761
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100762/*****************************************************************************\
763 * *
764 * SDHCI core callbacks *
765 * *
766\*****************************************************************************/
767
768static int sdhci_pci_enable_dma(struct sdhci_host *host)
769{
770 struct sdhci_pci_slot *slot;
771 struct pci_dev *pdev;
772 int ret;
773
774 slot = sdhci_priv(host);
775 pdev = slot->chip->pdev;
776
777 if (((pdev->class & 0xFFFF00) == (PCI_CLASS_SYSTEM_SDHCI << 8)) &&
778 ((pdev->class & 0x0000FF) != PCI_SDHCI_IFDMA) &&
Richard Röjforsa13abc72009-09-22 16:45:30 -0700779 (host->flags & SDHCI_USE_SDMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100780 dev_warn(&pdev->dev, "Will use DMA mode even though HW "
781 "doesn't fully claim to support it.\n");
782 }
783
Yang Hongyang284901a2009-04-06 19:01:15 -0700784 ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100785 if (ret)
786 return ret;
787
788 pci_set_master(pdev);
789
790 return 0;
791}
792
793static struct sdhci_ops sdhci_pci_ops = {
794 .enable_dma = sdhci_pci_enable_dma,
795};
796
797/*****************************************************************************\
798 * *
799 * Suspend/resume *
800 * *
801\*****************************************************************************/
802
803#ifdef CONFIG_PM
804
Ameya Palandeb177bc92011-04-05 21:13:13 +0300805static int sdhci_pci_suspend(struct pci_dev *pdev, pm_message_t state)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100806{
807 struct sdhci_pci_chip *chip;
808 struct sdhci_pci_slot *slot;
Daniel Drake5f619702010-11-04 22:20:39 +0000809 mmc_pm_flag_t slot_pm_flags;
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800810 mmc_pm_flag_t pm_flags = 0;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100811 int i, ret;
812
813 chip = pci_get_drvdata(pdev);
814 if (!chip)
815 return 0;
816
Ameya Palandeb177bc92011-04-05 21:13:13 +0300817 for (i = 0; i < chip->num_slots; i++) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100818 slot = chip->slots[i];
819 if (!slot)
820 continue;
821
822 ret = sdhci_suspend_host(slot->host, state);
823
824 if (ret) {
Ameya Palandeb177bc92011-04-05 21:13:13 +0300825 for (i--; i >= 0; i--)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100826 sdhci_resume_host(chip->slots[i]->host);
827 return ret;
828 }
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800829
Daniel Drake5f619702010-11-04 22:20:39 +0000830 slot_pm_flags = slot->host->mmc->pm_flags;
831 if (slot_pm_flags & MMC_PM_WAKE_SDIO_IRQ)
832 sdhci_enable_irq_wakeups(slot->host);
833
834 pm_flags |= slot_pm_flags;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100835 }
836
Pierre Ossman44894282008-04-04 19:36:59 +0200837 if (chip->fixes && chip->fixes->suspend) {
838 ret = chip->fixes->suspend(chip, state);
839 if (ret) {
Ameya Palandeb177bc92011-04-05 21:13:13 +0300840 for (i = chip->num_slots - 1; i >= 0; i--)
Pierre Ossman44894282008-04-04 19:36:59 +0200841 sdhci_resume_host(chip->slots[i]->host);
842 return ret;
843 }
844 }
845
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100846 pci_save_state(pdev);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800847 if (pm_flags & MMC_PM_KEEP_POWER) {
Daniel Drake5f619702010-11-04 22:20:39 +0000848 if (pm_flags & MMC_PM_WAKE_SDIO_IRQ) {
849 pci_pme_active(pdev, true);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800850 pci_enable_wake(pdev, PCI_D3hot, 1);
Daniel Drake5f619702010-11-04 22:20:39 +0000851 }
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800852 pci_set_power_state(pdev, PCI_D3hot);
853 } else {
854 pci_enable_wake(pdev, pci_choose_state(pdev, state), 0);
855 pci_disable_device(pdev);
856 pci_set_power_state(pdev, pci_choose_state(pdev, state));
857 }
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100858
859 return 0;
860}
861
Ameya Palandeb177bc92011-04-05 21:13:13 +0300862static int sdhci_pci_resume(struct pci_dev *pdev)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100863{
864 struct sdhci_pci_chip *chip;
865 struct sdhci_pci_slot *slot;
866 int i, ret;
867
868 chip = pci_get_drvdata(pdev);
869 if (!chip)
870 return 0;
871
872 pci_set_power_state(pdev, PCI_D0);
873 pci_restore_state(pdev);
874 ret = pci_enable_device(pdev);
875 if (ret)
876 return ret;
877
Pierre Ossman45211e22008-03-24 13:09:09 +0100878 if (chip->fixes && chip->fixes->resume) {
879 ret = chip->fixes->resume(chip);
880 if (ret)
881 return ret;
882 }
883
Ameya Palandeb177bc92011-04-05 21:13:13 +0300884 for (i = 0; i < chip->num_slots; i++) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100885 slot = chip->slots[i];
886 if (!slot)
887 continue;
888
889 ret = sdhci_resume_host(slot->host);
890 if (ret)
891 return ret;
892 }
893
894 return 0;
895}
896
897#else /* CONFIG_PM */
898
899#define sdhci_pci_suspend NULL
900#define sdhci_pci_resume NULL
901
902#endif /* CONFIG_PM */
903
904/*****************************************************************************\
905 * *
906 * Device probing/removal *
907 * *
908\*****************************************************************************/
909
910static struct sdhci_pci_slot * __devinit sdhci_pci_probe_slot(
911 struct pci_dev *pdev, struct sdhci_pci_chip *chip, int bar)
912{
913 struct sdhci_pci_slot *slot;
914 struct sdhci_host *host;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100915 int ret;
916
917 if (!(pci_resource_flags(pdev, bar) & IORESOURCE_MEM)) {
918 dev_err(&pdev->dev, "BAR %d is not iomem. Aborting.\n", bar);
919 return ERR_PTR(-ENODEV);
920 }
921
922 if (pci_resource_len(pdev, bar) != 0x100) {
923 dev_err(&pdev->dev, "Invalid iomem size. You may "
924 "experience problems.\n");
925 }
926
927 if ((pdev->class & 0x0000FF) == PCI_SDHCI_IFVENDOR) {
928 dev_err(&pdev->dev, "Vendor specific interface. Aborting.\n");
929 return ERR_PTR(-ENODEV);
930 }
931
932 if ((pdev->class & 0x0000FF) > PCI_SDHCI_IFVENDOR) {
933 dev_err(&pdev->dev, "Unknown interface. Aborting.\n");
934 return ERR_PTR(-ENODEV);
935 }
936
937 host = sdhci_alloc_host(&pdev->dev, sizeof(struct sdhci_pci_slot));
938 if (IS_ERR(host)) {
Dan Carpenterc60a32c2009-04-10 23:31:10 +0200939 dev_err(&pdev->dev, "cannot allocate host\n");
Julia Lawalldc0fd7b2010-05-26 14:42:11 -0700940 return ERR_CAST(host);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100941 }
942
943 slot = sdhci_priv(host);
944
945 slot->chip = chip;
946 slot->host = host;
947 slot->pci_bar = bar;
948
949 host->hw_name = "PCI";
950 host->ops = &sdhci_pci_ops;
951 host->quirks = chip->quirks;
952
953 host->irq = pdev->irq;
954
955 ret = pci_request_region(pdev, bar, mmc_hostname(host->mmc));
956 if (ret) {
957 dev_err(&pdev->dev, "cannot request region\n");
Dan Carpenterc60a32c2009-04-10 23:31:10 +0200958 goto free;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100959 }
960
Arjan van de Ven092f82e2008-09-28 16:15:56 -0700961 host->ioaddr = pci_ioremap_bar(pdev, bar);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100962 if (!host->ioaddr) {
963 dev_err(&pdev->dev, "failed to remap registers\n");
Chris Ball9fdcdbb2011-03-29 00:46:12 -0400964 ret = -ENOMEM;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100965 goto release;
966 }
967
Pierre Ossman44894282008-04-04 19:36:59 +0200968 if (chip->fixes && chip->fixes->probe_slot) {
969 ret = chip->fixes->probe_slot(slot);
970 if (ret)
971 goto unmap;
972 }
973
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800974 host->mmc->pm_caps = MMC_PM_KEEP_POWER | MMC_PM_WAKE_SDIO_IRQ;
975
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100976 ret = sdhci_add_host(host);
977 if (ret)
Pierre Ossman44894282008-04-04 19:36:59 +0200978 goto remove;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100979
980 return slot;
981
Pierre Ossman44894282008-04-04 19:36:59 +0200982remove:
983 if (chip->fixes && chip->fixes->remove_slot)
Pierre Ossman1e728592008-04-16 19:13:13 +0200984 chip->fixes->remove_slot(slot, 0);
Pierre Ossman44894282008-04-04 19:36:59 +0200985
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100986unmap:
987 iounmap(host->ioaddr);
988
989release:
990 pci_release_region(pdev, bar);
Dan Carpenterc60a32c2009-04-10 23:31:10 +0200991
992free:
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100993 sdhci_free_host(host);
994
995 return ERR_PTR(ret);
996}
997
998static void sdhci_pci_remove_slot(struct sdhci_pci_slot *slot)
999{
Pierre Ossman1e728592008-04-16 19:13:13 +02001000 int dead;
1001 u32 scratch;
1002
1003 dead = 0;
1004 scratch = readl(slot->host->ioaddr + SDHCI_INT_STATUS);
1005 if (scratch == (u32)-1)
1006 dead = 1;
1007
1008 sdhci_remove_host(slot->host, dead);
Pierre Ossman44894282008-04-04 19:36:59 +02001009
1010 if (slot->chip->fixes && slot->chip->fixes->remove_slot)
Pierre Ossman1e728592008-04-16 19:13:13 +02001011 slot->chip->fixes->remove_slot(slot, dead);
Pierre Ossman44894282008-04-04 19:36:59 +02001012
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001013 pci_release_region(slot->chip->pdev, slot->pci_bar);
Pierre Ossman44894282008-04-04 19:36:59 +02001014
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001015 sdhci_free_host(slot->host);
1016}
1017
1018static int __devinit sdhci_pci_probe(struct pci_dev *pdev,
1019 const struct pci_device_id *ent)
1020{
1021 struct sdhci_pci_chip *chip;
1022 struct sdhci_pci_slot *slot;
1023
Sergei Shtylyovcf5e23e2011-03-17 16:46:17 -04001024 u8 slots, first_bar;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001025 int ret, i;
1026
1027 BUG_ON(pdev == NULL);
1028 BUG_ON(ent == NULL);
1029
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001030 dev_info(&pdev->dev, "SDHCI controller found [%04x:%04x] (rev %x)\n",
Sergei Shtylyovcf5e23e2011-03-17 16:46:17 -04001031 (int)pdev->vendor, (int)pdev->device, (int)pdev->revision);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001032
1033 ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &slots);
1034 if (ret)
1035 return ret;
1036
1037 slots = PCI_SLOT_INFO_SLOTS(slots) + 1;
1038 dev_dbg(&pdev->dev, "found %d slot(s)\n", slots);
1039 if (slots == 0)
1040 return -ENODEV;
1041
1042 BUG_ON(slots > MAX_SLOTS);
1043
1044 ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &first_bar);
1045 if (ret)
1046 return ret;
1047
1048 first_bar &= PCI_SLOT_INFO_FIRST_BAR_MASK;
1049
1050 if (first_bar > 5) {
1051 dev_err(&pdev->dev, "Invalid first BAR. Aborting.\n");
1052 return -ENODEV;
1053 }
1054
1055 ret = pci_enable_device(pdev);
1056 if (ret)
1057 return ret;
1058
1059 chip = kzalloc(sizeof(struct sdhci_pci_chip), GFP_KERNEL);
1060 if (!chip) {
1061 ret = -ENOMEM;
1062 goto err;
1063 }
1064
1065 chip->pdev = pdev;
Ameya Palandeb177bc92011-04-05 21:13:13 +03001066 chip->fixes = (const struct sdhci_pci_fixes *)ent->driver_data;
Pierre Ossman22606402008-03-23 19:33:23 +01001067 if (chip->fixes)
1068 chip->quirks = chip->fixes->quirks;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001069 chip->num_slots = slots;
1070
1071 pci_set_drvdata(pdev, chip);
1072
Pierre Ossman22606402008-03-23 19:33:23 +01001073 if (chip->fixes && chip->fixes->probe) {
1074 ret = chip->fixes->probe(chip);
1075 if (ret)
1076 goto free;
1077 }
1078
Alan Cox225d85f2010-10-04 15:24:21 +01001079 slots = chip->num_slots; /* Quirk may have changed this */
1080
Ameya Palandeb177bc92011-04-05 21:13:13 +03001081 for (i = 0; i < slots; i++) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001082 slot = sdhci_pci_probe_slot(pdev, chip, first_bar + i);
1083 if (IS_ERR(slot)) {
Ameya Palandeb177bc92011-04-05 21:13:13 +03001084 for (i--; i >= 0; i--)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001085 sdhci_pci_remove_slot(chip->slots[i]);
1086 ret = PTR_ERR(slot);
1087 goto free;
1088 }
1089
1090 chip->slots[i] = slot;
1091 }
1092
1093 return 0;
1094
1095free:
1096 pci_set_drvdata(pdev, NULL);
1097 kfree(chip);
1098
1099err:
1100 pci_disable_device(pdev);
1101 return ret;
1102}
1103
1104static void __devexit sdhci_pci_remove(struct pci_dev *pdev)
1105{
1106 int i;
1107 struct sdhci_pci_chip *chip;
1108
1109 chip = pci_get_drvdata(pdev);
1110
1111 if (chip) {
Ameya Palandeb177bc92011-04-05 21:13:13 +03001112 for (i = 0; i < chip->num_slots; i++)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001113 sdhci_pci_remove_slot(chip->slots[i]);
1114
1115 pci_set_drvdata(pdev, NULL);
1116 kfree(chip);
1117 }
1118
1119 pci_disable_device(pdev);
1120}
1121
1122static struct pci_driver sdhci_driver = {
Ameya Palandeb177bc92011-04-05 21:13:13 +03001123 .name = "sdhci-pci",
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001124 .id_table = pci_ids,
Ameya Palandeb177bc92011-04-05 21:13:13 +03001125 .probe = sdhci_pci_probe,
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001126 .remove = __devexit_p(sdhci_pci_remove),
1127 .suspend = sdhci_pci_suspend,
1128 .resume = sdhci_pci_resume,
1129};
1130
1131/*****************************************************************************\
1132 * *
1133 * Driver init/exit *
1134 * *
1135\*****************************************************************************/
1136
1137static int __init sdhci_drv_init(void)
1138{
1139 return pci_register_driver(&sdhci_driver);
1140}
1141
1142static void __exit sdhci_drv_exit(void)
1143{
1144 pci_unregister_driver(&sdhci_driver);
1145}
1146
1147module_init(sdhci_drv_init);
1148module_exit(sdhci_drv_exit);
1149
Pierre Ossman32710e82009-04-08 20:14:54 +02001150MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001151MODULE_DESCRIPTION("Secure Digital Host Controller Interface PCI driver");
1152MODULE_LICENSE("GPL");