blob: fbbebe251e016cf8b24906aa6a0b4ad31f64cd40 [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 Gortmaker88b47672011-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>
Adrian Hunter0f201652011-08-29 16:42:13 +030025#include <linux/gpio.h>
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030026#include <linux/pm_runtime.h>
Adrian Hunter52c506f2011-12-27 15:48:43 +020027#include <linux/mmc/sdhci-pci-data.h>
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010028
29#include "sdhci.h"
30
31/*
Alexander Stein296e0b02012-03-14 08:38:58 +010032 * PCI device IDs
33 */
34#define PCI_DEVICE_ID_INTEL_PCH_SDIO0 0x8809
35#define PCI_DEVICE_ID_INTEL_PCH_SDIO1 0x880a
36
37/*
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010038 * PCI registers
39 */
40
41#define PCI_SDHCI_IFPIO 0x00
42#define PCI_SDHCI_IFDMA 0x01
43#define PCI_SDHCI_IFVENDOR 0x02
44
45#define PCI_SLOT_INFO 0x40 /* 8 bits */
46#define PCI_SLOT_INFO_SLOTS(x) ((x >> 4) & 7)
47#define PCI_SLOT_INFO_FIRST_BAR_MASK 0x07
48
49#define MAX_SLOTS 8
50
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010051struct sdhci_pci_chip;
Pierre Ossman44894282008-04-04 19:36:59 +020052struct sdhci_pci_slot;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010053
Pierre Ossman22606402008-03-23 19:33:23 +010054struct sdhci_pci_fixes {
55 unsigned int quirks;
Adrian Hunterf3c55a72012-02-07 14:48:55 +020056 unsigned int quirks2;
Adrian Hunterc43fd772011-10-17 10:52:44 +030057 bool allow_runtime_pm;
Pierre Ossman22606402008-03-23 19:33:23 +010058
Ameya Palandeb177bc92011-04-05 21:13:13 +030059 int (*probe) (struct sdhci_pci_chip *);
Pierre Ossman45211e22008-03-24 13:09:09 +010060
Ameya Palandeb177bc92011-04-05 21:13:13 +030061 int (*probe_slot) (struct sdhci_pci_slot *);
62 void (*remove_slot) (struct sdhci_pci_slot *, int);
Pierre Ossman44894282008-04-04 19:36:59 +020063
Manuel Lauss29495aa2011-11-03 11:09:45 +010064 int (*suspend) (struct sdhci_pci_chip *);
Ameya Palandeb177bc92011-04-05 21:13:13 +030065 int (*resume) (struct sdhci_pci_chip *);
Pierre Ossman22606402008-03-23 19:33:23 +010066};
67
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010068struct sdhci_pci_slot {
69 struct sdhci_pci_chip *chip;
70 struct sdhci_host *host;
Adrian Hunter52c506f2011-12-27 15:48:43 +020071 struct sdhci_pci_data *data;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010072
73 int pci_bar;
Adrian Hunter0f201652011-08-29 16:42:13 +030074 int rst_n_gpio;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030075 int cd_gpio;
76 int cd_irq;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010077};
78
79struct sdhci_pci_chip {
80 struct pci_dev *pdev;
Pierre Ossman22606402008-03-23 19:33:23 +010081
82 unsigned int quirks;
Adrian Hunterf3c55a72012-02-07 14:48:55 +020083 unsigned int quirks2;
Adrian Hunterc43fd772011-10-17 10:52:44 +030084 bool allow_runtime_pm;
Pierre Ossman22606402008-03-23 19:33:23 +010085 const struct sdhci_pci_fixes *fixes;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010086
87 int num_slots; /* Slots on controller */
88 struct sdhci_pci_slot *slots[MAX_SLOTS]; /* Pointers to host slots */
89};
90
Pierre Ossman22606402008-03-23 19:33:23 +010091
92/*****************************************************************************\
93 * *
94 * Hardware specific quirk handling *
95 * *
96\*****************************************************************************/
97
98static int ricoh_probe(struct sdhci_pci_chip *chip)
99{
Chris Ballc99436f2009-09-22 16:45:22 -0700100 if (chip->pdev->subsystem_vendor == PCI_VENDOR_ID_SAMSUNG ||
101 chip->pdev->subsystem_vendor == PCI_VENDOR_ID_SONY)
Pierre Ossman22606402008-03-23 19:33:23 +0100102 chip->quirks |= SDHCI_QUIRK_NO_CARD_NO_RESET;
Maxim Levitskyccc92c22010-08-10 18:01:42 -0700103 return 0;
104}
Pierre Ossman22606402008-03-23 19:33:23 +0100105
Maxim Levitskyccc92c22010-08-10 18:01:42 -0700106static int ricoh_mmc_probe_slot(struct sdhci_pci_slot *slot)
107{
108 slot->host->caps =
109 ((0x21 << SDHCI_TIMEOUT_CLK_SHIFT)
110 & SDHCI_TIMEOUT_CLK_MASK) |
111
112 ((0x21 << SDHCI_CLOCK_BASE_SHIFT)
113 & SDHCI_CLOCK_BASE_MASK) |
114
115 SDHCI_TIMEOUT_CLK_UNIT |
116 SDHCI_CAN_VDD_330 |
117 SDHCI_CAN_DO_SDMA;
118 return 0;
119}
120
121static int ricoh_mmc_resume(struct sdhci_pci_chip *chip)
122{
123 /* Apply a delay to allow controller to settle */
124 /* Otherwise it becomes confused if card state changed
125 during suspend */
126 msleep(500);
Pierre Ossman22606402008-03-23 19:33:23 +0100127 return 0;
128}
129
130static const struct sdhci_pci_fixes sdhci_ricoh = {
131 .probe = ricoh_probe,
Vasily Khoruzhick84938292010-03-05 13:43:46 -0800132 .quirks = SDHCI_QUIRK_32BIT_DMA_ADDR |
133 SDHCI_QUIRK_FORCE_DMA |
134 SDHCI_QUIRK_CLOCK_BEFORE_RESET,
Pierre Ossman22606402008-03-23 19:33:23 +0100135};
136
Maxim Levitskyccc92c22010-08-10 18:01:42 -0700137static const struct sdhci_pci_fixes sdhci_ricoh_mmc = {
138 .probe_slot = ricoh_mmc_probe_slot,
139 .resume = ricoh_mmc_resume,
140 .quirks = SDHCI_QUIRK_32BIT_DMA_ADDR |
141 SDHCI_QUIRK_CLOCK_BEFORE_RESET |
142 SDHCI_QUIRK_NO_CARD_NO_RESET |
143 SDHCI_QUIRK_MISSING_CAPS
144};
145
Pierre Ossman22606402008-03-23 19:33:23 +0100146static const struct sdhci_pci_fixes sdhci_ene_712 = {
147 .quirks = SDHCI_QUIRK_SINGLE_POWER_WRITE |
148 SDHCI_QUIRK_BROKEN_DMA,
149};
150
151static const struct sdhci_pci_fixes sdhci_ene_714 = {
152 .quirks = SDHCI_QUIRK_SINGLE_POWER_WRITE |
153 SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS |
154 SDHCI_QUIRK_BROKEN_DMA,
155};
156
157static const struct sdhci_pci_fixes sdhci_cafe = {
158 .quirks = SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER |
Andres Salomona0874892009-03-02 21:48:20 +0100159 SDHCI_QUIRK_NO_BUSY_IRQ |
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200160 SDHCI_QUIRK_BROKEN_TIMEOUT_VAL,
Pierre Ossman22606402008-03-23 19:33:23 +0100161};
162
Major Lee68077b02011-06-29 14:23:46 +0300163static int mrst_hc_probe_slot(struct sdhci_pci_slot *slot)
164{
165 slot->host->mmc->caps |= MMC_CAP_8_BIT_DATA;
166 return 0;
167}
168
Alan Coxf9ee3ea2010-10-04 15:25:11 +0100169/*
170 * ADMA operation is disabled for Moorestown platform due to
171 * hardware bugs.
172 */
Jacob Pan35ac6f02010-11-09 13:57:29 +0000173static int mrst_hc_probe(struct sdhci_pci_chip *chip)
Alan Coxf9ee3ea2010-10-04 15:25:11 +0100174{
175 /*
Jacob Pan35ac6f02010-11-09 13:57:29 +0000176 * slots number is fixed here for MRST as SDIO3/5 are never used and
177 * have hardware bugs.
Alan Coxf9ee3ea2010-10-04 15:25:11 +0100178 */
179 chip->num_slots = 1;
180 return 0;
181}
182
Alexander Stein296e0b02012-03-14 08:38:58 +0100183static int pch_hc_probe_slot(struct sdhci_pci_slot *slot)
184{
185 slot->host->mmc->caps |= MMC_CAP_8_BIT_DATA;
186 return 0;
187}
188
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300189#ifdef CONFIG_PM_RUNTIME
190
Adrian Hunterc5e027a2011-12-27 15:48:44 +0200191static irqreturn_t sdhci_pci_sd_cd(int irq, void *dev_id)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300192{
193 struct sdhci_pci_slot *slot = dev_id;
194 struct sdhci_host *host = slot->host;
195
196 mmc_detect_change(host->mmc, msecs_to_jiffies(200));
197 return IRQ_HANDLED;
198}
199
Adrian Hunterc5e027a2011-12-27 15:48:44 +0200200static void sdhci_pci_add_own_cd(struct sdhci_pci_slot *slot)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300201{
Adrian Hunterc5e027a2011-12-27 15:48:44 +0200202 int err, irq, gpio = slot->cd_gpio;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300203
204 slot->cd_gpio = -EINVAL;
205 slot->cd_irq = -EINVAL;
206
Adrian Hunterc5e027a2011-12-27 15:48:44 +0200207 if (!gpio_is_valid(gpio))
208 return;
209
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300210 err = gpio_request(gpio, "sd_cd");
211 if (err < 0)
212 goto out;
213
214 err = gpio_direction_input(gpio);
215 if (err < 0)
216 goto out_free;
217
218 irq = gpio_to_irq(gpio);
219 if (irq < 0)
220 goto out_free;
221
Adrian Hunterc5e027a2011-12-27 15:48:44 +0200222 err = request_irq(irq, sdhci_pci_sd_cd, IRQF_TRIGGER_RISING |
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300223 IRQF_TRIGGER_FALLING, "sd_cd", slot);
224 if (err)
225 goto out_free;
226
227 slot->cd_gpio = gpio;
228 slot->cd_irq = irq;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300229
Adrian Hunterc5e027a2011-12-27 15:48:44 +0200230 return;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300231
232out_free:
233 gpio_free(gpio);
234out:
235 dev_warn(&slot->chip->pdev->dev, "failed to setup card detect wake up\n");
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300236}
237
Adrian Hunterc5e027a2011-12-27 15:48:44 +0200238static void sdhci_pci_remove_own_cd(struct sdhci_pci_slot *slot)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300239{
240 if (slot->cd_irq >= 0)
241 free_irq(slot->cd_irq, slot);
Adrian Hunterc5e027a2011-12-27 15:48:44 +0200242 if (gpio_is_valid(slot->cd_gpio))
243 gpio_free(slot->cd_gpio);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300244}
245
246#else
247
Adrian Hunterc5e027a2011-12-27 15:48:44 +0200248static inline void sdhci_pci_add_own_cd(struct sdhci_pci_slot *slot)
249{
250}
251
252static inline void sdhci_pci_remove_own_cd(struct sdhci_pci_slot *slot)
253{
254}
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300255
256#endif
257
Adrian Hunter0d013bc2011-06-29 14:23:47 +0300258static int mfd_emmc_probe_slot(struct sdhci_pci_slot *slot)
259{
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300260 slot->host->mmc->caps |= MMC_CAP_8_BIT_DATA | MMC_CAP_NONREMOVABLE;
Adrian Hunterda721cf2012-02-07 14:48:53 +0200261 slot->host->mmc->caps2 |= MMC_CAP2_BOOTPART_NOACC |
262 MMC_CAP2_HC_ERASE_SZ;
Adrian Hunter0d013bc2011-06-29 14:23:47 +0300263 return 0;
264}
265
Adrian Hunter93933502011-12-27 15:48:47 +0200266static int mfd_sdio_probe_slot(struct sdhci_pci_slot *slot)
267{
Adrian Hunter012e4672012-01-30 14:27:18 +0200268 slot->host->mmc->caps |= MMC_CAP_POWER_OFF_CARD | MMC_CAP_NONREMOVABLE;
Adrian Hunter93933502011-12-27 15:48:47 +0200269 return 0;
270}
271
Alan Coxf9ee3ea2010-10-04 15:25:11 +0100272static const struct sdhci_pci_fixes sdhci_intel_mrst_hc0 = {
273 .quirks = SDHCI_QUIRK_BROKEN_ADMA | SDHCI_QUIRK_NO_HISPD_BIT,
Major Lee68077b02011-06-29 14:23:46 +0300274 .probe_slot = mrst_hc_probe_slot,
Alan Coxf9ee3ea2010-10-04 15:25:11 +0100275};
276
Jacob Pan35ac6f02010-11-09 13:57:29 +0000277static const struct sdhci_pci_fixes sdhci_intel_mrst_hc1_hc2 = {
Alan Coxf9ee3ea2010-10-04 15:25:11 +0100278 .quirks = SDHCI_QUIRK_BROKEN_ADMA | SDHCI_QUIRK_NO_HISPD_BIT,
Jacob Pan35ac6f02010-11-09 13:57:29 +0000279 .probe = mrst_hc_probe,
Alan Coxf9ee3ea2010-10-04 15:25:11 +0100280};
281
Xiaochen Shen29229052010-10-04 15:24:52 +0100282static const struct sdhci_pci_fixes sdhci_intel_mfd_sd = {
283 .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
Adrian Hunterc43fd772011-10-17 10:52:44 +0300284 .allow_runtime_pm = true,
Xiaochen Shen29229052010-10-04 15:24:52 +0100285};
286
Adrian Hunter0d013bc2011-06-29 14:23:47 +0300287static const struct sdhci_pci_fixes sdhci_intel_mfd_sdio = {
Xiaochen Shen29229052010-10-04 15:24:52 +0100288 .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
Adrian Hunterf3c55a72012-02-07 14:48:55 +0200289 .quirks2 = SDHCI_QUIRK2_HOST_OFF_CARD_ON,
Adrian Hunterc43fd772011-10-17 10:52:44 +0300290 .allow_runtime_pm = true,
Adrian Hunter93933502011-12-27 15:48:47 +0200291 .probe_slot = mfd_sdio_probe_slot,
Xiaochen Shen29229052010-10-04 15:24:52 +0100292};
293
Adrian Hunter0d013bc2011-06-29 14:23:47 +0300294static const struct sdhci_pci_fixes sdhci_intel_mfd_emmc = {
295 .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
Adrian Hunterc43fd772011-10-17 10:52:44 +0300296 .allow_runtime_pm = true,
Adrian Hunter0d013bc2011-06-29 14:23:47 +0300297 .probe_slot = mfd_emmc_probe_slot,
298};
299
Alexander Stein296e0b02012-03-14 08:38:58 +0100300static const struct sdhci_pci_fixes sdhci_intel_pch_sdio = {
301 .quirks = SDHCI_QUIRK_BROKEN_ADMA,
302 .probe_slot = pch_hc_probe_slot,
303};
304
Jennifer Li26daa1e2010-11-17 23:01:59 -0500305/* O2Micro extra registers */
306#define O2_SD_LOCK_WP 0xD3
307#define O2_SD_MULTI_VCC3V 0xEE
308#define O2_SD_CLKREQ 0xEC
309#define O2_SD_CAPS 0xE0
310#define O2_SD_ADMA1 0xE2
311#define O2_SD_ADMA2 0xE7
312#define O2_SD_INF_MOD 0xF1
313
314static int o2_probe(struct sdhci_pci_chip *chip)
315{
316 int ret;
317 u8 scratch;
318
319 switch (chip->pdev->device) {
320 case PCI_DEVICE_ID_O2_8220:
321 case PCI_DEVICE_ID_O2_8221:
322 case PCI_DEVICE_ID_O2_8320:
323 case PCI_DEVICE_ID_O2_8321:
324 /* This extra setup is required due to broken ADMA. */
325 ret = pci_read_config_byte(chip->pdev, O2_SD_LOCK_WP, &scratch);
326 if (ret)
327 return ret;
328 scratch &= 0x7f;
329 pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch);
330
331 /* Set Multi 3 to VCC3V# */
332 pci_write_config_byte(chip->pdev, O2_SD_MULTI_VCC3V, 0x08);
333
334 /* Disable CLK_REQ# support after media DET */
335 ret = pci_read_config_byte(chip->pdev, O2_SD_CLKREQ, &scratch);
336 if (ret)
337 return ret;
338 scratch |= 0x20;
339 pci_write_config_byte(chip->pdev, O2_SD_CLKREQ, scratch);
340
341 /* Choose capabilities, enable SDMA. We have to write 0x01
342 * to the capabilities register first to unlock it.
343 */
344 ret = pci_read_config_byte(chip->pdev, O2_SD_CAPS, &scratch);
345 if (ret)
346 return ret;
347 scratch |= 0x01;
348 pci_write_config_byte(chip->pdev, O2_SD_CAPS, scratch);
349 pci_write_config_byte(chip->pdev, O2_SD_CAPS, 0x73);
350
351 /* Disable ADMA1/2 */
352 pci_write_config_byte(chip->pdev, O2_SD_ADMA1, 0x39);
353 pci_write_config_byte(chip->pdev, O2_SD_ADMA2, 0x08);
354
355 /* Disable the infinite transfer mode */
356 ret = pci_read_config_byte(chip->pdev, O2_SD_INF_MOD, &scratch);
357 if (ret)
358 return ret;
359 scratch |= 0x08;
360 pci_write_config_byte(chip->pdev, O2_SD_INF_MOD, scratch);
361
362 /* Lock WP */
363 ret = pci_read_config_byte(chip->pdev, O2_SD_LOCK_WP, &scratch);
364 if (ret)
365 return ret;
366 scratch |= 0x80;
367 pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch);
368 }
369
370 return 0;
371}
372
Pierre Ossman45211e22008-03-24 13:09:09 +0100373static int jmicron_pmos(struct sdhci_pci_chip *chip, int on)
374{
375 u8 scratch;
376 int ret;
377
378 ret = pci_read_config_byte(chip->pdev, 0xAE, &scratch);
379 if (ret)
380 return ret;
381
382 /*
383 * Turn PMOS on [bit 0], set over current detection to 2.4 V
384 * [bit 1:2] and enable over current debouncing [bit 6].
385 */
386 if (on)
387 scratch |= 0x47;
388 else
389 scratch &= ~0x47;
390
391 ret = pci_write_config_byte(chip->pdev, 0xAE, scratch);
392 if (ret)
393 return ret;
394
395 return 0;
396}
397
398static int jmicron_probe(struct sdhci_pci_chip *chip)
399{
400 int ret;
Takashi Iwai8f230f42010-12-08 10:04:30 +0100401 u16 mmcdev = 0;
Pierre Ossman45211e22008-03-24 13:09:09 +0100402
Pierre Ossman93fc48c2008-06-28 18:21:41 +0200403 if (chip->pdev->revision == 0) {
404 chip->quirks |= SDHCI_QUIRK_32BIT_DMA_ADDR |
405 SDHCI_QUIRK_32BIT_DMA_SIZE |
Pierre Ossman2134a922008-06-28 18:28:51 +0200406 SDHCI_QUIRK_32BIT_ADMA_SIZE |
Pierre Ossman4a3cba32008-07-29 00:11:16 +0200407 SDHCI_QUIRK_RESET_AFTER_REQUEST |
Pierre Ossman86a6a872009-02-02 21:13:49 +0100408 SDHCI_QUIRK_BROKEN_SMALL_PIO;
Pierre Ossman93fc48c2008-06-28 18:21:41 +0200409 }
410
Pierre Ossman45211e22008-03-24 13:09:09 +0100411 /*
Pierre Ossman44894282008-04-04 19:36:59 +0200412 * JMicron chips can have two interfaces to the same hardware
413 * in order to work around limitations in Microsoft's driver.
414 * We need to make sure we only bind to one of them.
415 *
416 * This code assumes two things:
417 *
418 * 1. The PCI code adds subfunctions in order.
419 *
420 * 2. The MMC interface has a lower subfunction number
421 * than the SD interface.
422 */
Takashi Iwai8f230f42010-12-08 10:04:30 +0100423 if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_SD)
424 mmcdev = PCI_DEVICE_ID_JMICRON_JMB38X_MMC;
425 else if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_SD)
426 mmcdev = PCI_DEVICE_ID_JMICRON_JMB388_ESD;
427
428 if (mmcdev) {
Pierre Ossman44894282008-04-04 19:36:59 +0200429 struct pci_dev *sd_dev;
430
431 sd_dev = NULL;
432 while ((sd_dev = pci_get_device(PCI_VENDOR_ID_JMICRON,
Takashi Iwai8f230f42010-12-08 10:04:30 +0100433 mmcdev, sd_dev)) != NULL) {
Pierre Ossman44894282008-04-04 19:36:59 +0200434 if ((PCI_SLOT(chip->pdev->devfn) ==
435 PCI_SLOT(sd_dev->devfn)) &&
436 (chip->pdev->bus == sd_dev->bus))
437 break;
438 }
439
440 if (sd_dev) {
441 pci_dev_put(sd_dev);
442 dev_info(&chip->pdev->dev, "Refusing to bind to "
443 "secondary interface.\n");
444 return -ENODEV;
445 }
446 }
447
448 /*
Pierre Ossman45211e22008-03-24 13:09:09 +0100449 * JMicron chips need a bit of a nudge to enable the power
450 * output pins.
451 */
452 ret = jmicron_pmos(chip, 1);
453 if (ret) {
454 dev_err(&chip->pdev->dev, "Failure enabling card power\n");
455 return ret;
456 }
457
Takashi Iwai82b0e232011-04-21 20:26:38 +0200458 /* quirk for unsable RO-detection on JM388 chips */
459 if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_SD ||
460 chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD)
461 chip->quirks |= SDHCI_QUIRK_UNSTABLE_RO_DETECT;
462
Pierre Ossman45211e22008-03-24 13:09:09 +0100463 return 0;
464}
465
Pierre Ossman44894282008-04-04 19:36:59 +0200466static void jmicron_enable_mmc(struct sdhci_host *host, int on)
467{
468 u8 scratch;
469
470 scratch = readb(host->ioaddr + 0xC0);
471
472 if (on)
473 scratch |= 0x01;
474 else
475 scratch &= ~0x01;
476
477 writeb(scratch, host->ioaddr + 0xC0);
478}
479
480static int jmicron_probe_slot(struct sdhci_pci_slot *slot)
481{
Pierre Ossman2134a922008-06-28 18:28:51 +0200482 if (slot->chip->pdev->revision == 0) {
483 u16 version;
484
485 version = readl(slot->host->ioaddr + SDHCI_HOST_VERSION);
486 version = (version & SDHCI_VENDOR_VER_MASK) >>
487 SDHCI_VENDOR_VER_SHIFT;
488
489 /*
490 * Older versions of the chip have lots of nasty glitches
491 * in the ADMA engine. It's best just to avoid it
492 * completely.
493 */
494 if (version < 0xAC)
495 slot->host->quirks |= SDHCI_QUIRK_BROKEN_ADMA;
496 }
497
Takashi Iwai8f230f42010-12-08 10:04:30 +0100498 /* JM388 MMC doesn't support 1.8V while SD supports it */
499 if (slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD) {
500 slot->host->ocr_avail_sd = MMC_VDD_32_33 | MMC_VDD_33_34 |
501 MMC_VDD_29_30 | MMC_VDD_30_31 |
502 MMC_VDD_165_195; /* allow 1.8V */
503 slot->host->ocr_avail_mmc = MMC_VDD_32_33 | MMC_VDD_33_34 |
504 MMC_VDD_29_30 | MMC_VDD_30_31; /* no 1.8V for MMC */
505 }
506
Pierre Ossman44894282008-04-04 19:36:59 +0200507 /*
508 * The secondary interface requires a bit set to get the
509 * interrupts.
510 */
Takashi Iwai8f230f42010-12-08 10:04:30 +0100511 if (slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC ||
512 slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD)
Pierre Ossman44894282008-04-04 19:36:59 +0200513 jmicron_enable_mmc(slot->host, 1);
514
Takashi Iwaid75c1082010-12-16 17:54:14 +0100515 slot->host->mmc->caps |= MMC_CAP_BUS_WIDTH_TEST;
516
Pierre Ossman44894282008-04-04 19:36:59 +0200517 return 0;
518}
519
Pierre Ossman1e728592008-04-16 19:13:13 +0200520static void jmicron_remove_slot(struct sdhci_pci_slot *slot, int dead)
Pierre Ossman44894282008-04-04 19:36:59 +0200521{
Pierre Ossman1e728592008-04-16 19:13:13 +0200522 if (dead)
523 return;
524
Takashi Iwai8f230f42010-12-08 10:04:30 +0100525 if (slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC ||
526 slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD)
Pierre Ossman44894282008-04-04 19:36:59 +0200527 jmicron_enable_mmc(slot->host, 0);
528}
529
Manuel Lauss29495aa2011-11-03 11:09:45 +0100530static int jmicron_suspend(struct sdhci_pci_chip *chip)
Pierre Ossman44894282008-04-04 19:36:59 +0200531{
532 int i;
533
Takashi Iwai8f230f42010-12-08 10:04:30 +0100534 if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC ||
535 chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD) {
Ameya Palandeb177bc92011-04-05 21:13:13 +0300536 for (i = 0; i < chip->num_slots; i++)
Pierre Ossman44894282008-04-04 19:36:59 +0200537 jmicron_enable_mmc(chip->slots[i]->host, 0);
538 }
539
540 return 0;
541}
542
Pierre Ossman45211e22008-03-24 13:09:09 +0100543static int jmicron_resume(struct sdhci_pci_chip *chip)
544{
Pierre Ossman44894282008-04-04 19:36:59 +0200545 int ret, i;
546
Takashi Iwai8f230f42010-12-08 10:04:30 +0100547 if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC ||
548 chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD) {
Ameya Palandeb177bc92011-04-05 21:13:13 +0300549 for (i = 0; i < chip->num_slots; i++)
Pierre Ossman44894282008-04-04 19:36:59 +0200550 jmicron_enable_mmc(chip->slots[i]->host, 1);
551 }
Pierre Ossman45211e22008-03-24 13:09:09 +0100552
553 ret = jmicron_pmos(chip, 1);
554 if (ret) {
555 dev_err(&chip->pdev->dev, "Failure enabling card power\n");
556 return ret;
557 }
558
559 return 0;
560}
561
Jennifer Li26daa1e2010-11-17 23:01:59 -0500562static const struct sdhci_pci_fixes sdhci_o2 = {
563 .probe = o2_probe,
564};
565
Pierre Ossman22606402008-03-23 19:33:23 +0100566static const struct sdhci_pci_fixes sdhci_jmicron = {
Pierre Ossman45211e22008-03-24 13:09:09 +0100567 .probe = jmicron_probe,
568
Pierre Ossman44894282008-04-04 19:36:59 +0200569 .probe_slot = jmicron_probe_slot,
570 .remove_slot = jmicron_remove_slot,
571
572 .suspend = jmicron_suspend,
Pierre Ossman45211e22008-03-24 13:09:09 +0100573 .resume = jmicron_resume,
Pierre Ossman22606402008-03-23 19:33:23 +0100574};
575
Nicolas Pitrea7a61862009-12-14 18:01:26 -0800576/* SysKonnect CardBus2SDIO extra registers */
577#define SYSKT_CTRL 0x200
578#define SYSKT_RDFIFO_STAT 0x204
579#define SYSKT_WRFIFO_STAT 0x208
580#define SYSKT_POWER_DATA 0x20c
581#define SYSKT_POWER_330 0xef
582#define SYSKT_POWER_300 0xf8
583#define SYSKT_POWER_184 0xcc
584#define SYSKT_POWER_CMD 0x20d
585#define SYSKT_POWER_START (1 << 7)
586#define SYSKT_POWER_STATUS 0x20e
587#define SYSKT_POWER_STATUS_OK (1 << 0)
588#define SYSKT_BOARD_REV 0x210
589#define SYSKT_CHIP_REV 0x211
590#define SYSKT_CONF_DATA 0x212
591#define SYSKT_CONF_DATA_1V8 (1 << 2)
592#define SYSKT_CONF_DATA_2V5 (1 << 1)
593#define SYSKT_CONF_DATA_3V3 (1 << 0)
594
595static int syskt_probe(struct sdhci_pci_chip *chip)
596{
597 if ((chip->pdev->class & 0x0000FF) == PCI_SDHCI_IFVENDOR) {
598 chip->pdev->class &= ~0x0000FF;
599 chip->pdev->class |= PCI_SDHCI_IFDMA;
600 }
601 return 0;
602}
603
604static int syskt_probe_slot(struct sdhci_pci_slot *slot)
605{
606 int tm, ps;
607
608 u8 board_rev = readb(slot->host->ioaddr + SYSKT_BOARD_REV);
609 u8 chip_rev = readb(slot->host->ioaddr + SYSKT_CHIP_REV);
610 dev_info(&slot->chip->pdev->dev, "SysKonnect CardBus2SDIO, "
611 "board rev %d.%d, chip rev %d.%d\n",
612 board_rev >> 4, board_rev & 0xf,
613 chip_rev >> 4, chip_rev & 0xf);
614 if (chip_rev >= 0x20)
615 slot->host->quirks |= SDHCI_QUIRK_FORCE_DMA;
616
617 writeb(SYSKT_POWER_330, slot->host->ioaddr + SYSKT_POWER_DATA);
618 writeb(SYSKT_POWER_START, slot->host->ioaddr + SYSKT_POWER_CMD);
619 udelay(50);
620 tm = 10; /* Wait max 1 ms */
621 do {
622 ps = readw(slot->host->ioaddr + SYSKT_POWER_STATUS);
623 if (ps & SYSKT_POWER_STATUS_OK)
624 break;
625 udelay(100);
626 } while (--tm);
627 if (!tm) {
628 dev_err(&slot->chip->pdev->dev,
629 "power regulator never stabilized");
630 writeb(0, slot->host->ioaddr + SYSKT_POWER_CMD);
631 return -ENODEV;
632 }
633
634 return 0;
635}
636
637static const struct sdhci_pci_fixes sdhci_syskt = {
638 .quirks = SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER,
639 .probe = syskt_probe,
640 .probe_slot = syskt_probe_slot,
641};
642
Harald Welte557b0692009-06-18 16:53:38 +0200643static int via_probe(struct sdhci_pci_chip *chip)
644{
645 if (chip->pdev->revision == 0x10)
646 chip->quirks |= SDHCI_QUIRK_DELAY_AFTER_POWER;
647
648 return 0;
649}
650
651static const struct sdhci_pci_fixes sdhci_via = {
652 .probe = via_probe,
653};
654
Pierre Ossman22606402008-03-23 19:33:23 +0100655static const struct pci_device_id pci_ids[] __devinitdata = {
656 {
657 .vendor = PCI_VENDOR_ID_RICOH,
658 .device = PCI_DEVICE_ID_RICOH_R5C822,
659 .subvendor = PCI_ANY_ID,
660 .subdevice = PCI_ANY_ID,
661 .driver_data = (kernel_ulong_t)&sdhci_ricoh,
662 },
663
664 {
Maxim Levitskyccc92c22010-08-10 18:01:42 -0700665 .vendor = PCI_VENDOR_ID_RICOH,
666 .device = 0x843,
667 .subvendor = PCI_ANY_ID,
668 .subdevice = PCI_ANY_ID,
669 .driver_data = (kernel_ulong_t)&sdhci_ricoh_mmc,
670 },
671
672 {
Pablo Castillo568133e2010-08-10 18:02:01 -0700673 .vendor = PCI_VENDOR_ID_RICOH,
674 .device = 0xe822,
675 .subvendor = PCI_ANY_ID,
676 .subdevice = PCI_ANY_ID,
677 .driver_data = (kernel_ulong_t)&sdhci_ricoh_mmc,
678 },
679
680 {
Manoj Iyer5fd11c02011-02-11 16:25:31 -0600681 .vendor = PCI_VENDOR_ID_RICOH,
682 .device = 0xe823,
683 .subvendor = PCI_ANY_ID,
684 .subdevice = PCI_ANY_ID,
685 .driver_data = (kernel_ulong_t)&sdhci_ricoh_mmc,
686 },
687
688 {
Pierre Ossman22606402008-03-23 19:33:23 +0100689 .vendor = PCI_VENDOR_ID_ENE,
690 .device = PCI_DEVICE_ID_ENE_CB712_SD,
691 .subvendor = PCI_ANY_ID,
692 .subdevice = PCI_ANY_ID,
693 .driver_data = (kernel_ulong_t)&sdhci_ene_712,
694 },
695
696 {
697 .vendor = PCI_VENDOR_ID_ENE,
698 .device = PCI_DEVICE_ID_ENE_CB712_SD_2,
699 .subvendor = PCI_ANY_ID,
700 .subdevice = PCI_ANY_ID,
701 .driver_data = (kernel_ulong_t)&sdhci_ene_712,
702 },
703
704 {
705 .vendor = PCI_VENDOR_ID_ENE,
706 .device = PCI_DEVICE_ID_ENE_CB714_SD,
707 .subvendor = PCI_ANY_ID,
708 .subdevice = PCI_ANY_ID,
709 .driver_data = (kernel_ulong_t)&sdhci_ene_714,
710 },
711
712 {
713 .vendor = PCI_VENDOR_ID_ENE,
714 .device = PCI_DEVICE_ID_ENE_CB714_SD_2,
715 .subvendor = PCI_ANY_ID,
716 .subdevice = PCI_ANY_ID,
717 .driver_data = (kernel_ulong_t)&sdhci_ene_714,
718 },
719
720 {
721 .vendor = PCI_VENDOR_ID_MARVELL,
David Woodhouse8c5eb882008-09-03 09:45:57 +0100722 .device = PCI_DEVICE_ID_MARVELL_88ALP01_SD,
Pierre Ossman22606402008-03-23 19:33:23 +0100723 .subvendor = PCI_ANY_ID,
724 .subdevice = PCI_ANY_ID,
725 .driver_data = (kernel_ulong_t)&sdhci_cafe,
726 },
727
728 {
729 .vendor = PCI_VENDOR_ID_JMICRON,
730 .device = PCI_DEVICE_ID_JMICRON_JMB38X_SD,
731 .subvendor = PCI_ANY_ID,
732 .subdevice = PCI_ANY_ID,
733 .driver_data = (kernel_ulong_t)&sdhci_jmicron,
734 },
735
Pierre Ossman44894282008-04-04 19:36:59 +0200736 {
737 .vendor = PCI_VENDOR_ID_JMICRON,
738 .device = PCI_DEVICE_ID_JMICRON_JMB38X_MMC,
739 .subvendor = PCI_ANY_ID,
740 .subdevice = PCI_ANY_ID,
741 .driver_data = (kernel_ulong_t)&sdhci_jmicron,
742 },
743
Harald Welte557b0692009-06-18 16:53:38 +0200744 {
Takashi Iwai8f230f42010-12-08 10:04:30 +0100745 .vendor = PCI_VENDOR_ID_JMICRON,
746 .device = PCI_DEVICE_ID_JMICRON_JMB388_SD,
747 .subvendor = PCI_ANY_ID,
748 .subdevice = PCI_ANY_ID,
749 .driver_data = (kernel_ulong_t)&sdhci_jmicron,
750 },
751
752 {
753 .vendor = PCI_VENDOR_ID_JMICRON,
754 .device = PCI_DEVICE_ID_JMICRON_JMB388_ESD,
755 .subvendor = PCI_ANY_ID,
756 .subdevice = PCI_ANY_ID,
757 .driver_data = (kernel_ulong_t)&sdhci_jmicron,
758 },
759
760 {
Nicolas Pitrea7a61862009-12-14 18:01:26 -0800761 .vendor = PCI_VENDOR_ID_SYSKONNECT,
762 .device = 0x8000,
763 .subvendor = PCI_ANY_ID,
764 .subdevice = PCI_ANY_ID,
765 .driver_data = (kernel_ulong_t)&sdhci_syskt,
766 },
767
768 {
Harald Welte557b0692009-06-18 16:53:38 +0200769 .vendor = PCI_VENDOR_ID_VIA,
770 .device = 0x95d0,
771 .subvendor = PCI_ANY_ID,
772 .subdevice = PCI_ANY_ID,
773 .driver_data = (kernel_ulong_t)&sdhci_via,
774 },
775
Xiaochen Shen29229052010-10-04 15:24:52 +0100776 {
777 .vendor = PCI_VENDOR_ID_INTEL,
Alan Coxf9ee3ea2010-10-04 15:25:11 +0100778 .device = PCI_DEVICE_ID_INTEL_MRST_SD0,
779 .subvendor = PCI_ANY_ID,
780 .subdevice = PCI_ANY_ID,
781 .driver_data = (kernel_ulong_t)&sdhci_intel_mrst_hc0,
782 },
783
784 {
785 .vendor = PCI_VENDOR_ID_INTEL,
786 .device = PCI_DEVICE_ID_INTEL_MRST_SD1,
787 .subvendor = PCI_ANY_ID,
788 .subdevice = PCI_ANY_ID,
Jacob Pan35ac6f02010-11-09 13:57:29 +0000789 .driver_data = (kernel_ulong_t)&sdhci_intel_mrst_hc1_hc2,
790 },
791
792 {
793 .vendor = PCI_VENDOR_ID_INTEL,
794 .device = PCI_DEVICE_ID_INTEL_MRST_SD2,
795 .subvendor = PCI_ANY_ID,
796 .subdevice = PCI_ANY_ID,
797 .driver_data = (kernel_ulong_t)&sdhci_intel_mrst_hc1_hc2,
Alan Coxf9ee3ea2010-10-04 15:25:11 +0100798 },
799
800 {
801 .vendor = PCI_VENDOR_ID_INTEL,
Xiaochen Shen29229052010-10-04 15:24:52 +0100802 .device = PCI_DEVICE_ID_INTEL_MFD_SD,
803 .subvendor = PCI_ANY_ID,
804 .subdevice = PCI_ANY_ID,
805 .driver_data = (kernel_ulong_t)&sdhci_intel_mfd_sd,
806 },
807
808 {
809 .vendor = PCI_VENDOR_ID_INTEL,
810 .device = PCI_DEVICE_ID_INTEL_MFD_SDIO1,
811 .subvendor = PCI_ANY_ID,
812 .subdevice = PCI_ANY_ID,
Adrian Hunter0d013bc2011-06-29 14:23:47 +0300813 .driver_data = (kernel_ulong_t)&sdhci_intel_mfd_sdio,
Xiaochen Shen29229052010-10-04 15:24:52 +0100814 },
815
816 {
817 .vendor = PCI_VENDOR_ID_INTEL,
818 .device = PCI_DEVICE_ID_INTEL_MFD_SDIO2,
819 .subvendor = PCI_ANY_ID,
820 .subdevice = PCI_ANY_ID,
Adrian Hunter0d013bc2011-06-29 14:23:47 +0300821 .driver_data = (kernel_ulong_t)&sdhci_intel_mfd_sdio,
Xiaochen Shen29229052010-10-04 15:24:52 +0100822 },
823
824 {
825 .vendor = PCI_VENDOR_ID_INTEL,
826 .device = PCI_DEVICE_ID_INTEL_MFD_EMMC0,
827 .subvendor = PCI_ANY_ID,
828 .subdevice = PCI_ANY_ID,
Adrian Hunter0d013bc2011-06-29 14:23:47 +0300829 .driver_data = (kernel_ulong_t)&sdhci_intel_mfd_emmc,
Xiaochen Shen29229052010-10-04 15:24:52 +0100830 },
831
832 {
833 .vendor = PCI_VENDOR_ID_INTEL,
834 .device = PCI_DEVICE_ID_INTEL_MFD_EMMC1,
835 .subvendor = PCI_ANY_ID,
836 .subdevice = PCI_ANY_ID,
Adrian Hunter0d013bc2011-06-29 14:23:47 +0300837 .driver_data = (kernel_ulong_t)&sdhci_intel_mfd_emmc,
Xiaochen Shen29229052010-10-04 15:24:52 +0100838 },
839
Jennifer Li26daa1e2010-11-17 23:01:59 -0500840 {
Alexander Stein296e0b02012-03-14 08:38:58 +0100841 .vendor = PCI_VENDOR_ID_INTEL,
842 .device = PCI_DEVICE_ID_INTEL_PCH_SDIO0,
843 .subvendor = PCI_ANY_ID,
844 .subdevice = PCI_ANY_ID,
845 .driver_data = (kernel_ulong_t)&sdhci_intel_pch_sdio,
846 },
847
848 {
849 .vendor = PCI_VENDOR_ID_INTEL,
850 .device = PCI_DEVICE_ID_INTEL_PCH_SDIO1,
851 .subvendor = PCI_ANY_ID,
852 .subdevice = PCI_ANY_ID,
853 .driver_data = (kernel_ulong_t)&sdhci_intel_pch_sdio,
854 },
855
856 {
Jennifer Li26daa1e2010-11-17 23:01:59 -0500857 .vendor = PCI_VENDOR_ID_O2,
858 .device = PCI_DEVICE_ID_O2_8120,
859 .subvendor = PCI_ANY_ID,
860 .subdevice = PCI_ANY_ID,
861 .driver_data = (kernel_ulong_t)&sdhci_o2,
862 },
863
864 {
865 .vendor = PCI_VENDOR_ID_O2,
866 .device = PCI_DEVICE_ID_O2_8220,
867 .subvendor = PCI_ANY_ID,
868 .subdevice = PCI_ANY_ID,
869 .driver_data = (kernel_ulong_t)&sdhci_o2,
870 },
871
872 {
873 .vendor = PCI_VENDOR_ID_O2,
874 .device = PCI_DEVICE_ID_O2_8221,
875 .subvendor = PCI_ANY_ID,
876 .subdevice = PCI_ANY_ID,
877 .driver_data = (kernel_ulong_t)&sdhci_o2,
878 },
879
880 {
881 .vendor = PCI_VENDOR_ID_O2,
882 .device = PCI_DEVICE_ID_O2_8320,
883 .subvendor = PCI_ANY_ID,
884 .subdevice = PCI_ANY_ID,
885 .driver_data = (kernel_ulong_t)&sdhci_o2,
886 },
887
888 {
889 .vendor = PCI_VENDOR_ID_O2,
890 .device = PCI_DEVICE_ID_O2_8321,
891 .subvendor = PCI_ANY_ID,
892 .subdevice = PCI_ANY_ID,
893 .driver_data = (kernel_ulong_t)&sdhci_o2,
894 },
895
Pierre Ossman22606402008-03-23 19:33:23 +0100896 { /* Generic SD host controller */
897 PCI_DEVICE_CLASS((PCI_CLASS_SYSTEM_SDHCI << 8), 0xFFFF00)
898 },
899
900 { /* end: all zeroes */ },
901};
902
903MODULE_DEVICE_TABLE(pci, pci_ids);
904
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100905/*****************************************************************************\
906 * *
907 * SDHCI core callbacks *
908 * *
909\*****************************************************************************/
910
911static int sdhci_pci_enable_dma(struct sdhci_host *host)
912{
913 struct sdhci_pci_slot *slot;
914 struct pci_dev *pdev;
915 int ret;
916
917 slot = sdhci_priv(host);
918 pdev = slot->chip->pdev;
919
920 if (((pdev->class & 0xFFFF00) == (PCI_CLASS_SYSTEM_SDHCI << 8)) &&
921 ((pdev->class & 0x0000FF) != PCI_SDHCI_IFDMA) &&
Richard Röjforsa13abc72009-09-22 16:45:30 -0700922 (host->flags & SDHCI_USE_SDMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100923 dev_warn(&pdev->dev, "Will use DMA mode even though HW "
924 "doesn't fully claim to support it.\n");
925 }
926
Yang Hongyang284901a2009-04-06 19:01:15 -0700927 ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100928 if (ret)
929 return ret;
930
931 pci_set_master(pdev);
932
933 return 0;
934}
935
Major Lee68077b02011-06-29 14:23:46 +0300936static int sdhci_pci_8bit_width(struct sdhci_host *host, int width)
937{
938 u8 ctrl;
939
940 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
941
942 switch (width) {
943 case MMC_BUS_WIDTH_8:
944 ctrl |= SDHCI_CTRL_8BITBUS;
945 ctrl &= ~SDHCI_CTRL_4BITBUS;
946 break;
947 case MMC_BUS_WIDTH_4:
948 ctrl |= SDHCI_CTRL_4BITBUS;
949 ctrl &= ~SDHCI_CTRL_8BITBUS;
950 break;
951 default:
952 ctrl &= ~(SDHCI_CTRL_8BITBUS | SDHCI_CTRL_4BITBUS);
953 break;
954 }
955
956 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
957
958 return 0;
959}
960
Adrian Hunter0f201652011-08-29 16:42:13 +0300961static void sdhci_pci_hw_reset(struct sdhci_host *host)
962{
963 struct sdhci_pci_slot *slot = sdhci_priv(host);
964 int rst_n_gpio = slot->rst_n_gpio;
965
966 if (!gpio_is_valid(rst_n_gpio))
967 return;
968 gpio_set_value_cansleep(rst_n_gpio, 0);
969 /* For eMMC, minimum is 1us but give it 10us for good measure */
970 udelay(10);
971 gpio_set_value_cansleep(rst_n_gpio, 1);
972 /* For eMMC, minimum is 200us but give it 300us for good measure */
973 usleep_range(300, 1000);
974}
975
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100976static struct sdhci_ops sdhci_pci_ops = {
977 .enable_dma = sdhci_pci_enable_dma,
Major Lee68077b02011-06-29 14:23:46 +0300978 .platform_8bit_width = sdhci_pci_8bit_width,
Adrian Hunter0f201652011-08-29 16:42:13 +0300979 .hw_reset = sdhci_pci_hw_reset,
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100980};
981
982/*****************************************************************************\
983 * *
984 * Suspend/resume *
985 * *
986\*****************************************************************************/
987
988#ifdef CONFIG_PM
989
Manuel Lauss29495aa2011-11-03 11:09:45 +0100990static int sdhci_pci_suspend(struct device *dev)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100991{
Manuel Lauss29495aa2011-11-03 11:09:45 +0100992 struct pci_dev *pdev = to_pci_dev(dev);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100993 struct sdhci_pci_chip *chip;
994 struct sdhci_pci_slot *slot;
Daniel Drake5f619702010-11-04 22:20:39 +0000995 mmc_pm_flag_t slot_pm_flags;
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800996 mmc_pm_flag_t pm_flags = 0;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100997 int i, ret;
998
999 chip = pci_get_drvdata(pdev);
1000 if (!chip)
1001 return 0;
1002
Ameya Palandeb177bc92011-04-05 21:13:13 +03001003 for (i = 0; i < chip->num_slots; i++) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001004 slot = chip->slots[i];
1005 if (!slot)
1006 continue;
1007
Manuel Lauss29495aa2011-11-03 11:09:45 +01001008 ret = sdhci_suspend_host(slot->host);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001009
Axel Linb678b912011-12-03 15:28:05 +08001010 if (ret)
1011 goto err_pci_suspend;
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08001012
Daniel Drake5f619702010-11-04 22:20:39 +00001013 slot_pm_flags = slot->host->mmc->pm_flags;
1014 if (slot_pm_flags & MMC_PM_WAKE_SDIO_IRQ)
1015 sdhci_enable_irq_wakeups(slot->host);
1016
1017 pm_flags |= slot_pm_flags;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001018 }
1019
Pierre Ossman44894282008-04-04 19:36:59 +02001020 if (chip->fixes && chip->fixes->suspend) {
Manuel Lauss29495aa2011-11-03 11:09:45 +01001021 ret = chip->fixes->suspend(chip);
Axel Linb678b912011-12-03 15:28:05 +08001022 if (ret)
1023 goto err_pci_suspend;
Pierre Ossman44894282008-04-04 19:36:59 +02001024 }
1025
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001026 pci_save_state(pdev);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08001027 if (pm_flags & MMC_PM_KEEP_POWER) {
Daniel Drake5f619702010-11-04 22:20:39 +00001028 if (pm_flags & MMC_PM_WAKE_SDIO_IRQ) {
1029 pci_pme_active(pdev, true);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08001030 pci_enable_wake(pdev, PCI_D3hot, 1);
Daniel Drake5f619702010-11-04 22:20:39 +00001031 }
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08001032 pci_set_power_state(pdev, PCI_D3hot);
1033 } else {
Manuel Lauss29495aa2011-11-03 11:09:45 +01001034 pci_enable_wake(pdev, PCI_D3hot, 0);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08001035 pci_disable_device(pdev);
Manuel Lauss29495aa2011-11-03 11:09:45 +01001036 pci_set_power_state(pdev, PCI_D3hot);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08001037 }
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001038
1039 return 0;
Axel Linb678b912011-12-03 15:28:05 +08001040
1041err_pci_suspend:
1042 while (--i >= 0)
1043 sdhci_resume_host(chip->slots[i]->host);
1044 return ret;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001045}
1046
Manuel Lauss29495aa2011-11-03 11:09:45 +01001047static int sdhci_pci_resume(struct device *dev)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001048{
Manuel Lauss29495aa2011-11-03 11:09:45 +01001049 struct pci_dev *pdev = to_pci_dev(dev);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001050 struct sdhci_pci_chip *chip;
1051 struct sdhci_pci_slot *slot;
1052 int i, ret;
1053
1054 chip = pci_get_drvdata(pdev);
1055 if (!chip)
1056 return 0;
1057
1058 pci_set_power_state(pdev, PCI_D0);
1059 pci_restore_state(pdev);
1060 ret = pci_enable_device(pdev);
1061 if (ret)
1062 return ret;
1063
Pierre Ossman45211e22008-03-24 13:09:09 +01001064 if (chip->fixes && chip->fixes->resume) {
1065 ret = chip->fixes->resume(chip);
1066 if (ret)
1067 return ret;
1068 }
1069
Ameya Palandeb177bc92011-04-05 21:13:13 +03001070 for (i = 0; i < chip->num_slots; i++) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001071 slot = chip->slots[i];
1072 if (!slot)
1073 continue;
1074
1075 ret = sdhci_resume_host(slot->host);
1076 if (ret)
1077 return ret;
1078 }
1079
1080 return 0;
1081}
1082
1083#else /* CONFIG_PM */
1084
1085#define sdhci_pci_suspend NULL
1086#define sdhci_pci_resume NULL
1087
1088#endif /* CONFIG_PM */
1089
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001090#ifdef CONFIG_PM_RUNTIME
1091
1092static int sdhci_pci_runtime_suspend(struct device *dev)
1093{
1094 struct pci_dev *pdev = container_of(dev, struct pci_dev, dev);
1095 struct sdhci_pci_chip *chip;
1096 struct sdhci_pci_slot *slot;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001097 int i, ret;
1098
1099 chip = pci_get_drvdata(pdev);
1100 if (!chip)
1101 return 0;
1102
1103 for (i = 0; i < chip->num_slots; i++) {
1104 slot = chip->slots[i];
1105 if (!slot)
1106 continue;
1107
1108 ret = sdhci_runtime_suspend_host(slot->host);
1109
Axel Linb678b912011-12-03 15:28:05 +08001110 if (ret)
1111 goto err_pci_runtime_suspend;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001112 }
1113
1114 if (chip->fixes && chip->fixes->suspend) {
Manuel Lauss29495aa2011-11-03 11:09:45 +01001115 ret = chip->fixes->suspend(chip);
Axel Linb678b912011-12-03 15:28:05 +08001116 if (ret)
1117 goto err_pci_runtime_suspend;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001118 }
1119
1120 return 0;
Axel Linb678b912011-12-03 15:28:05 +08001121
1122err_pci_runtime_suspend:
1123 while (--i >= 0)
1124 sdhci_runtime_resume_host(chip->slots[i]->host);
1125 return ret;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001126}
1127
1128static int sdhci_pci_runtime_resume(struct device *dev)
1129{
1130 struct pci_dev *pdev = container_of(dev, struct pci_dev, dev);
1131 struct sdhci_pci_chip *chip;
1132 struct sdhci_pci_slot *slot;
1133 int i, ret;
1134
1135 chip = pci_get_drvdata(pdev);
1136 if (!chip)
1137 return 0;
1138
1139 if (chip->fixes && chip->fixes->resume) {
1140 ret = chip->fixes->resume(chip);
1141 if (ret)
1142 return ret;
1143 }
1144
1145 for (i = 0; i < chip->num_slots; i++) {
1146 slot = chip->slots[i];
1147 if (!slot)
1148 continue;
1149
1150 ret = sdhci_runtime_resume_host(slot->host);
1151 if (ret)
1152 return ret;
1153 }
1154
1155 return 0;
1156}
1157
1158static int sdhci_pci_runtime_idle(struct device *dev)
1159{
1160 return 0;
1161}
1162
1163#else
1164
1165#define sdhci_pci_runtime_suspend NULL
1166#define sdhci_pci_runtime_resume NULL
1167#define sdhci_pci_runtime_idle NULL
1168
1169#endif
1170
1171static const struct dev_pm_ops sdhci_pci_pm_ops = {
Manuel Lauss29495aa2011-11-03 11:09:45 +01001172 .suspend = sdhci_pci_suspend,
1173 .resume = sdhci_pci_resume,
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001174 .runtime_suspend = sdhci_pci_runtime_suspend,
1175 .runtime_resume = sdhci_pci_runtime_resume,
1176 .runtime_idle = sdhci_pci_runtime_idle,
1177};
1178
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001179/*****************************************************************************\
1180 * *
1181 * Device probing/removal *
1182 * *
1183\*****************************************************************************/
1184
1185static struct sdhci_pci_slot * __devinit sdhci_pci_probe_slot(
Adrian Hunter52c506f2011-12-27 15:48:43 +02001186 struct pci_dev *pdev, struct sdhci_pci_chip *chip, int first_bar,
1187 int slotno)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001188{
1189 struct sdhci_pci_slot *slot;
1190 struct sdhci_host *host;
Adrian Hunter52c506f2011-12-27 15:48:43 +02001191 int ret, bar = first_bar + slotno;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001192
1193 if (!(pci_resource_flags(pdev, bar) & IORESOURCE_MEM)) {
1194 dev_err(&pdev->dev, "BAR %d is not iomem. Aborting.\n", bar);
1195 return ERR_PTR(-ENODEV);
1196 }
1197
1198 if (pci_resource_len(pdev, bar) != 0x100) {
1199 dev_err(&pdev->dev, "Invalid iomem size. You may "
1200 "experience problems.\n");
1201 }
1202
1203 if ((pdev->class & 0x0000FF) == PCI_SDHCI_IFVENDOR) {
1204 dev_err(&pdev->dev, "Vendor specific interface. Aborting.\n");
1205 return ERR_PTR(-ENODEV);
1206 }
1207
1208 if ((pdev->class & 0x0000FF) > PCI_SDHCI_IFVENDOR) {
1209 dev_err(&pdev->dev, "Unknown interface. Aborting.\n");
1210 return ERR_PTR(-ENODEV);
1211 }
1212
1213 host = sdhci_alloc_host(&pdev->dev, sizeof(struct sdhci_pci_slot));
1214 if (IS_ERR(host)) {
Dan Carpenterc60a32c2009-04-10 23:31:10 +02001215 dev_err(&pdev->dev, "cannot allocate host\n");
Julia Lawalldc0fd7b2010-05-26 14:42:11 -07001216 return ERR_CAST(host);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001217 }
1218
1219 slot = sdhci_priv(host);
1220
1221 slot->chip = chip;
1222 slot->host = host;
1223 slot->pci_bar = bar;
Adrian Hunter0f201652011-08-29 16:42:13 +03001224 slot->rst_n_gpio = -EINVAL;
Adrian Hunterc5e027a2011-12-27 15:48:44 +02001225 slot->cd_gpio = -EINVAL;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001226
Adrian Hunter52c506f2011-12-27 15:48:43 +02001227 /* Retrieve platform data if there is any */
1228 if (*sdhci_pci_get_data)
1229 slot->data = sdhci_pci_get_data(pdev, slotno);
1230
1231 if (slot->data) {
1232 if (slot->data->setup) {
1233 ret = slot->data->setup(slot->data);
1234 if (ret) {
1235 dev_err(&pdev->dev, "platform setup failed\n");
1236 goto free;
1237 }
1238 }
Adrian Hunterc5e027a2011-12-27 15:48:44 +02001239 slot->rst_n_gpio = slot->data->rst_n_gpio;
1240 slot->cd_gpio = slot->data->cd_gpio;
Adrian Hunter52c506f2011-12-27 15:48:43 +02001241 }
1242
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001243 host->hw_name = "PCI";
1244 host->ops = &sdhci_pci_ops;
1245 host->quirks = chip->quirks;
Adrian Hunterf3c55a72012-02-07 14:48:55 +02001246 host->quirks2 = chip->quirks2;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001247
1248 host->irq = pdev->irq;
1249
1250 ret = pci_request_region(pdev, bar, mmc_hostname(host->mmc));
1251 if (ret) {
1252 dev_err(&pdev->dev, "cannot request region\n");
Adrian Hunter52c506f2011-12-27 15:48:43 +02001253 goto cleanup;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001254 }
1255
Arjan van de Ven092f82e2008-09-28 16:15:56 -07001256 host->ioaddr = pci_ioremap_bar(pdev, bar);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001257 if (!host->ioaddr) {
1258 dev_err(&pdev->dev, "failed to remap registers\n");
Chris Ball9fdcdbb2011-03-29 00:46:12 -04001259 ret = -ENOMEM;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001260 goto release;
1261 }
1262
Pierre Ossman44894282008-04-04 19:36:59 +02001263 if (chip->fixes && chip->fixes->probe_slot) {
1264 ret = chip->fixes->probe_slot(slot);
1265 if (ret)
1266 goto unmap;
1267 }
1268
Adrian Hunterc5e027a2011-12-27 15:48:44 +02001269 if (gpio_is_valid(slot->rst_n_gpio)) {
1270 if (!gpio_request(slot->rst_n_gpio, "eMMC_reset")) {
1271 gpio_direction_output(slot->rst_n_gpio, 1);
1272 slot->host->mmc->caps |= MMC_CAP_HW_RESET;
1273 } else {
1274 dev_warn(&pdev->dev, "failed to request rst_n_gpio\n");
1275 slot->rst_n_gpio = -EINVAL;
1276 }
1277 }
1278
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08001279 host->mmc->pm_caps = MMC_PM_KEEP_POWER | MMC_PM_WAKE_SDIO_IRQ;
1280
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001281 ret = sdhci_add_host(host);
1282 if (ret)
Pierre Ossman44894282008-04-04 19:36:59 +02001283 goto remove;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001284
Adrian Hunterc5e027a2011-12-27 15:48:44 +02001285 sdhci_pci_add_own_cd(slot);
1286
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001287 return slot;
1288
Pierre Ossman44894282008-04-04 19:36:59 +02001289remove:
Adrian Hunterc5e027a2011-12-27 15:48:44 +02001290 if (gpio_is_valid(slot->rst_n_gpio))
1291 gpio_free(slot->rst_n_gpio);
1292
Pierre Ossman44894282008-04-04 19:36:59 +02001293 if (chip->fixes && chip->fixes->remove_slot)
Pierre Ossman1e728592008-04-16 19:13:13 +02001294 chip->fixes->remove_slot(slot, 0);
Pierre Ossman44894282008-04-04 19:36:59 +02001295
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001296unmap:
1297 iounmap(host->ioaddr);
1298
1299release:
1300 pci_release_region(pdev, bar);
Dan Carpenterc60a32c2009-04-10 23:31:10 +02001301
Adrian Hunter52c506f2011-12-27 15:48:43 +02001302cleanup:
1303 if (slot->data && slot->data->cleanup)
1304 slot->data->cleanup(slot->data);
1305
Dan Carpenterc60a32c2009-04-10 23:31:10 +02001306free:
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001307 sdhci_free_host(host);
1308
1309 return ERR_PTR(ret);
1310}
1311
1312static void sdhci_pci_remove_slot(struct sdhci_pci_slot *slot)
1313{
Pierre Ossman1e728592008-04-16 19:13:13 +02001314 int dead;
1315 u32 scratch;
1316
Adrian Hunterc5e027a2011-12-27 15:48:44 +02001317 sdhci_pci_remove_own_cd(slot);
1318
Pierre Ossman1e728592008-04-16 19:13:13 +02001319 dead = 0;
1320 scratch = readl(slot->host->ioaddr + SDHCI_INT_STATUS);
1321 if (scratch == (u32)-1)
1322 dead = 1;
1323
1324 sdhci_remove_host(slot->host, dead);
Pierre Ossman44894282008-04-04 19:36:59 +02001325
Adrian Hunterc5e027a2011-12-27 15:48:44 +02001326 if (gpio_is_valid(slot->rst_n_gpio))
1327 gpio_free(slot->rst_n_gpio);
1328
Pierre Ossman44894282008-04-04 19:36:59 +02001329 if (slot->chip->fixes && slot->chip->fixes->remove_slot)
Pierre Ossman1e728592008-04-16 19:13:13 +02001330 slot->chip->fixes->remove_slot(slot, dead);
Pierre Ossman44894282008-04-04 19:36:59 +02001331
Adrian Hunter52c506f2011-12-27 15:48:43 +02001332 if (slot->data && slot->data->cleanup)
1333 slot->data->cleanup(slot->data);
1334
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001335 pci_release_region(slot->chip->pdev, slot->pci_bar);
Pierre Ossman44894282008-04-04 19:36:59 +02001336
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001337 sdhci_free_host(slot->host);
1338}
1339
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001340static void __devinit sdhci_pci_runtime_pm_allow(struct device *dev)
1341{
1342 pm_runtime_put_noidle(dev);
1343 pm_runtime_allow(dev);
1344 pm_runtime_set_autosuspend_delay(dev, 50);
1345 pm_runtime_use_autosuspend(dev);
1346 pm_suspend_ignore_children(dev, 1);
1347}
1348
1349static void __devexit sdhci_pci_runtime_pm_forbid(struct device *dev)
1350{
1351 pm_runtime_forbid(dev);
1352 pm_runtime_get_noresume(dev);
1353}
1354
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001355static int __devinit sdhci_pci_probe(struct pci_dev *pdev,
1356 const struct pci_device_id *ent)
1357{
1358 struct sdhci_pci_chip *chip;
1359 struct sdhci_pci_slot *slot;
1360
Sergei Shtylyovcf5e23e2011-03-17 16:46:17 -04001361 u8 slots, first_bar;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001362 int ret, i;
1363
1364 BUG_ON(pdev == NULL);
1365 BUG_ON(ent == NULL);
1366
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001367 dev_info(&pdev->dev, "SDHCI controller found [%04x:%04x] (rev %x)\n",
Sergei Shtylyovcf5e23e2011-03-17 16:46:17 -04001368 (int)pdev->vendor, (int)pdev->device, (int)pdev->revision);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001369
1370 ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &slots);
1371 if (ret)
1372 return ret;
1373
1374 slots = PCI_SLOT_INFO_SLOTS(slots) + 1;
1375 dev_dbg(&pdev->dev, "found %d slot(s)\n", slots);
1376 if (slots == 0)
1377 return -ENODEV;
1378
1379 BUG_ON(slots > MAX_SLOTS);
1380
1381 ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &first_bar);
1382 if (ret)
1383 return ret;
1384
1385 first_bar &= PCI_SLOT_INFO_FIRST_BAR_MASK;
1386
1387 if (first_bar > 5) {
1388 dev_err(&pdev->dev, "Invalid first BAR. Aborting.\n");
1389 return -ENODEV;
1390 }
1391
1392 ret = pci_enable_device(pdev);
1393 if (ret)
1394 return ret;
1395
1396 chip = kzalloc(sizeof(struct sdhci_pci_chip), GFP_KERNEL);
1397 if (!chip) {
1398 ret = -ENOMEM;
1399 goto err;
1400 }
1401
1402 chip->pdev = pdev;
Ameya Palandeb177bc92011-04-05 21:13:13 +03001403 chip->fixes = (const struct sdhci_pci_fixes *)ent->driver_data;
Adrian Hunterc43fd772011-10-17 10:52:44 +03001404 if (chip->fixes) {
Pierre Ossman22606402008-03-23 19:33:23 +01001405 chip->quirks = chip->fixes->quirks;
Adrian Hunterf3c55a72012-02-07 14:48:55 +02001406 chip->quirks2 = chip->fixes->quirks2;
Adrian Hunterc43fd772011-10-17 10:52:44 +03001407 chip->allow_runtime_pm = chip->fixes->allow_runtime_pm;
1408 }
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001409 chip->num_slots = slots;
1410
1411 pci_set_drvdata(pdev, chip);
1412
Pierre Ossman22606402008-03-23 19:33:23 +01001413 if (chip->fixes && chip->fixes->probe) {
1414 ret = chip->fixes->probe(chip);
1415 if (ret)
1416 goto free;
1417 }
1418
Alan Cox225d85f2010-10-04 15:24:21 +01001419 slots = chip->num_slots; /* Quirk may have changed this */
1420
Alexander Steine6039832012-03-13 18:16:40 +01001421 pci_enable_msi(pdev);
1422
Ameya Palandeb177bc92011-04-05 21:13:13 +03001423 for (i = 0; i < slots; i++) {
Adrian Hunter52c506f2011-12-27 15:48:43 +02001424 slot = sdhci_pci_probe_slot(pdev, chip, first_bar, i);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001425 if (IS_ERR(slot)) {
Ameya Palandeb177bc92011-04-05 21:13:13 +03001426 for (i--; i >= 0; i--)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001427 sdhci_pci_remove_slot(chip->slots[i]);
1428 ret = PTR_ERR(slot);
1429 goto free;
1430 }
1431
1432 chip->slots[i] = slot;
1433 }
1434
Adrian Hunterc43fd772011-10-17 10:52:44 +03001435 if (chip->allow_runtime_pm)
1436 sdhci_pci_runtime_pm_allow(&pdev->dev);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001437
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001438 return 0;
1439
1440free:
Alexander Steine6039832012-03-13 18:16:40 +01001441 pci_disable_msi(pdev);
1442
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001443 pci_set_drvdata(pdev, NULL);
1444 kfree(chip);
1445
1446err:
1447 pci_disable_device(pdev);
1448 return ret;
1449}
1450
1451static void __devexit sdhci_pci_remove(struct pci_dev *pdev)
1452{
1453 int i;
1454 struct sdhci_pci_chip *chip;
1455
1456 chip = pci_get_drvdata(pdev);
1457
1458 if (chip) {
Adrian Hunterc43fd772011-10-17 10:52:44 +03001459 if (chip->allow_runtime_pm)
1460 sdhci_pci_runtime_pm_forbid(&pdev->dev);
1461
Ameya Palandeb177bc92011-04-05 21:13:13 +03001462 for (i = 0; i < chip->num_slots; i++)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001463 sdhci_pci_remove_slot(chip->slots[i]);
1464
Alexander Steine6039832012-03-13 18:16:40 +01001465 pci_disable_msi(pdev);
1466
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001467 pci_set_drvdata(pdev, NULL);
1468 kfree(chip);
1469 }
1470
1471 pci_disable_device(pdev);
1472}
1473
1474static struct pci_driver sdhci_driver = {
Ameya Palandeb177bc92011-04-05 21:13:13 +03001475 .name = "sdhci-pci",
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001476 .id_table = pci_ids,
Ameya Palandeb177bc92011-04-05 21:13:13 +03001477 .probe = sdhci_pci_probe,
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001478 .remove = __devexit_p(sdhci_pci_remove),
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001479 .driver = {
1480 .pm = &sdhci_pci_pm_ops
1481 },
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001482};
1483
1484/*****************************************************************************\
1485 * *
1486 * Driver init/exit *
1487 * *
1488\*****************************************************************************/
1489
1490static int __init sdhci_drv_init(void)
1491{
1492 return pci_register_driver(&sdhci_driver);
1493}
1494
1495static void __exit sdhci_drv_exit(void)
1496{
1497 pci_unregister_driver(&sdhci_driver);
1498}
1499
1500module_init(sdhci_drv_init);
1501module_exit(sdhci_drv_exit);
1502
Pierre Ossman32710e82009-04-08 20:14:54 +02001503MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001504MODULE_DESCRIPTION("Secure Digital Host Controller Interface PCI driver");
1505MODULE_LICENSE("GPL");