blob: 4487b8430aaf09c4542660e218d9e652c49d090e [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>
26#include <linux/sfi.h>
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010027
28#include "sdhci.h"
29
30/*
31 * PCI registers
32 */
33
34#define PCI_SDHCI_IFPIO 0x00
35#define PCI_SDHCI_IFDMA 0x01
36#define PCI_SDHCI_IFVENDOR 0x02
37
38#define PCI_SLOT_INFO 0x40 /* 8 bits */
39#define PCI_SLOT_INFO_SLOTS(x) ((x >> 4) & 7)
40#define PCI_SLOT_INFO_FIRST_BAR_MASK 0x07
41
42#define MAX_SLOTS 8
43
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010044struct sdhci_pci_chip;
Pierre Ossman44894282008-04-04 19:36:59 +020045struct sdhci_pci_slot;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010046
Pierre Ossman22606402008-03-23 19:33:23 +010047struct sdhci_pci_fixes {
48 unsigned int quirks;
49
Ameya Palandeb177bc92011-04-05 21:13:13 +030050 int (*probe) (struct sdhci_pci_chip *);
Pierre Ossman45211e22008-03-24 13:09:09 +010051
Ameya Palandeb177bc92011-04-05 21:13:13 +030052 int (*probe_slot) (struct sdhci_pci_slot *);
53 void (*remove_slot) (struct sdhci_pci_slot *, int);
Pierre Ossman44894282008-04-04 19:36:59 +020054
Ameya Palandeb177bc92011-04-05 21:13:13 +030055 int (*suspend) (struct sdhci_pci_chip *,
Pierre Ossman44894282008-04-04 19:36:59 +020056 pm_message_t);
Ameya Palandeb177bc92011-04-05 21:13:13 +030057 int (*resume) (struct sdhci_pci_chip *);
Pierre Ossman22606402008-03-23 19:33:23 +010058};
59
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010060struct sdhci_pci_slot {
61 struct sdhci_pci_chip *chip;
62 struct sdhci_host *host;
63
64 int pci_bar;
Adrian Hunter0f201652011-08-29 16:42:13 +030065 int rst_n_gpio;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010066};
67
68struct sdhci_pci_chip {
69 struct pci_dev *pdev;
Pierre Ossman22606402008-03-23 19:33:23 +010070
71 unsigned int quirks;
72 const struct sdhci_pci_fixes *fixes;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010073
74 int num_slots; /* Slots on controller */
75 struct sdhci_pci_slot *slots[MAX_SLOTS]; /* Pointers to host slots */
76};
77
Pierre Ossman22606402008-03-23 19:33:23 +010078
79/*****************************************************************************\
80 * *
81 * Hardware specific quirk handling *
82 * *
83\*****************************************************************************/
84
85static int ricoh_probe(struct sdhci_pci_chip *chip)
86{
Chris Ballc99436f2009-09-22 16:45:22 -070087 if (chip->pdev->subsystem_vendor == PCI_VENDOR_ID_SAMSUNG ||
88 chip->pdev->subsystem_vendor == PCI_VENDOR_ID_SONY)
Pierre Ossman22606402008-03-23 19:33:23 +010089 chip->quirks |= SDHCI_QUIRK_NO_CARD_NO_RESET;
Maxim Levitskyccc92c22010-08-10 18:01:42 -070090 return 0;
91}
Pierre Ossman22606402008-03-23 19:33:23 +010092
Maxim Levitskyccc92c22010-08-10 18:01:42 -070093static int ricoh_mmc_probe_slot(struct sdhci_pci_slot *slot)
94{
95 slot->host->caps =
96 ((0x21 << SDHCI_TIMEOUT_CLK_SHIFT)
97 & SDHCI_TIMEOUT_CLK_MASK) |
98
99 ((0x21 << SDHCI_CLOCK_BASE_SHIFT)
100 & SDHCI_CLOCK_BASE_MASK) |
101
102 SDHCI_TIMEOUT_CLK_UNIT |
103 SDHCI_CAN_VDD_330 |
104 SDHCI_CAN_DO_SDMA;
105 return 0;
106}
107
108static int ricoh_mmc_resume(struct sdhci_pci_chip *chip)
109{
110 /* Apply a delay to allow controller to settle */
111 /* Otherwise it becomes confused if card state changed
112 during suspend */
113 msleep(500);
Pierre Ossman22606402008-03-23 19:33:23 +0100114 return 0;
115}
116
117static const struct sdhci_pci_fixes sdhci_ricoh = {
118 .probe = ricoh_probe,
Vasily Khoruzhick84938292010-03-05 13:43:46 -0800119 .quirks = SDHCI_QUIRK_32BIT_DMA_ADDR |
120 SDHCI_QUIRK_FORCE_DMA |
121 SDHCI_QUIRK_CLOCK_BEFORE_RESET,
Pierre Ossman22606402008-03-23 19:33:23 +0100122};
123
Maxim Levitskyccc92c22010-08-10 18:01:42 -0700124static const struct sdhci_pci_fixes sdhci_ricoh_mmc = {
125 .probe_slot = ricoh_mmc_probe_slot,
126 .resume = ricoh_mmc_resume,
127 .quirks = SDHCI_QUIRK_32BIT_DMA_ADDR |
128 SDHCI_QUIRK_CLOCK_BEFORE_RESET |
129 SDHCI_QUIRK_NO_CARD_NO_RESET |
130 SDHCI_QUIRK_MISSING_CAPS
131};
132
Pierre Ossman22606402008-03-23 19:33:23 +0100133static const struct sdhci_pci_fixes sdhci_ene_712 = {
134 .quirks = SDHCI_QUIRK_SINGLE_POWER_WRITE |
135 SDHCI_QUIRK_BROKEN_DMA,
136};
137
138static const struct sdhci_pci_fixes sdhci_ene_714 = {
139 .quirks = SDHCI_QUIRK_SINGLE_POWER_WRITE |
140 SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS |
141 SDHCI_QUIRK_BROKEN_DMA,
142};
143
144static const struct sdhci_pci_fixes sdhci_cafe = {
145 .quirks = SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER |
Andres Salomona0874892009-03-02 21:48:20 +0100146 SDHCI_QUIRK_NO_BUSY_IRQ |
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200147 SDHCI_QUIRK_BROKEN_TIMEOUT_VAL,
Pierre Ossman22606402008-03-23 19:33:23 +0100148};
149
Major Lee68077b02011-06-29 14:23:46 +0300150static int mrst_hc_probe_slot(struct sdhci_pci_slot *slot)
151{
152 slot->host->mmc->caps |= MMC_CAP_8_BIT_DATA;
153 return 0;
154}
155
Alan Coxf9ee3ea2010-10-04 15:25:11 +0100156/*
157 * ADMA operation is disabled for Moorestown platform due to
158 * hardware bugs.
159 */
Jacob Pan35ac6f02010-11-09 13:57:29 +0000160static int mrst_hc_probe(struct sdhci_pci_chip *chip)
Alan Coxf9ee3ea2010-10-04 15:25:11 +0100161{
162 /*
Jacob Pan35ac6f02010-11-09 13:57:29 +0000163 * slots number is fixed here for MRST as SDIO3/5 are never used and
164 * have hardware bugs.
Alan Coxf9ee3ea2010-10-04 15:25:11 +0100165 */
166 chip->num_slots = 1;
167 return 0;
168}
169
Adrian Hunter0f201652011-08-29 16:42:13 +0300170/* Medfield eMMC hardware reset GPIOs */
171static int mfd_emmc0_rst_gpio = -EINVAL;
172static int mfd_emmc1_rst_gpio = -EINVAL;
173
174static int mfd_emmc_gpio_parse(struct sfi_table_header *table)
175{
176 struct sfi_table_simple *sb = (struct sfi_table_simple *)table;
177 struct sfi_gpio_table_entry *entry;
178 int i, num;
179
180 num = SFI_GET_NUM_ENTRIES(sb, struct sfi_gpio_table_entry);
181 entry = (struct sfi_gpio_table_entry *)sb->pentry;
182
183 for (i = 0; i < num; i++, entry++) {
184 if (!strncmp(entry->pin_name, "emmc0_rst", SFI_NAME_LEN))
185 mfd_emmc0_rst_gpio = entry->pin_no;
186 else if (!strncmp(entry->pin_name, "emmc1_rst", SFI_NAME_LEN))
187 mfd_emmc1_rst_gpio = entry->pin_no;
188 }
189
190 return 0;
191}
192
Adrian Hunter0d013bc2011-06-29 14:23:47 +0300193static int mfd_emmc_probe_slot(struct sdhci_pci_slot *slot)
194{
Adrian Hunter0f201652011-08-29 16:42:13 +0300195 const char *name = NULL;
196 int gpio = -EINVAL;
197
198 sfi_table_parse(SFI_SIG_GPIO, NULL, NULL, mfd_emmc_gpio_parse);
199
200 switch (slot->chip->pdev->device) {
201 case PCI_DEVICE_ID_INTEL_MFD_EMMC0:
202 gpio = mfd_emmc0_rst_gpio;
203 name = "eMMC0_reset";
204 break;
205 case PCI_DEVICE_ID_INTEL_MFD_EMMC1:
206 gpio = mfd_emmc1_rst_gpio;
207 name = "eMMC1_reset";
208 break;
209 }
210
211 if (!gpio_request(gpio, name)) {
212 gpio_direction_output(gpio, 1);
213 slot->rst_n_gpio = gpio;
214 slot->host->mmc->caps |= MMC_CAP_HW_RESET;
215 }
216
Adrian Hunter0d013bc2011-06-29 14:23:47 +0300217 slot->host->mmc->caps |= MMC_CAP_8_BIT_DATA;
Adrian Hunter0f201652011-08-29 16:42:13 +0300218
Adrian Hunterf7c56ef2011-09-23 12:48:21 +0300219 slot->host->mmc->caps2 = MMC_CAP2_BOOTPART_NOACC;
220
Adrian Hunter0d013bc2011-06-29 14:23:47 +0300221 return 0;
222}
223
Adrian Hunter0f201652011-08-29 16:42:13 +0300224static void mfd_emmc_remove_slot(struct sdhci_pci_slot *slot, int dead)
225{
226 gpio_free(slot->rst_n_gpio);
227}
228
Alan Coxf9ee3ea2010-10-04 15:25:11 +0100229static const struct sdhci_pci_fixes sdhci_intel_mrst_hc0 = {
230 .quirks = SDHCI_QUIRK_BROKEN_ADMA | SDHCI_QUIRK_NO_HISPD_BIT,
Major Lee68077b02011-06-29 14:23:46 +0300231 .probe_slot = mrst_hc_probe_slot,
Alan Coxf9ee3ea2010-10-04 15:25:11 +0100232};
233
Jacob Pan35ac6f02010-11-09 13:57:29 +0000234static const struct sdhci_pci_fixes sdhci_intel_mrst_hc1_hc2 = {
Alan Coxf9ee3ea2010-10-04 15:25:11 +0100235 .quirks = SDHCI_QUIRK_BROKEN_ADMA | SDHCI_QUIRK_NO_HISPD_BIT,
Jacob Pan35ac6f02010-11-09 13:57:29 +0000236 .probe = mrst_hc_probe,
Alan Coxf9ee3ea2010-10-04 15:25:11 +0100237};
238
Xiaochen Shen29229052010-10-04 15:24:52 +0100239static const struct sdhci_pci_fixes sdhci_intel_mfd_sd = {
240 .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
241};
242
Adrian Hunter0d013bc2011-06-29 14:23:47 +0300243static const struct sdhci_pci_fixes sdhci_intel_mfd_sdio = {
Xiaochen Shen29229052010-10-04 15:24:52 +0100244 .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
245};
246
Adrian Hunter0d013bc2011-06-29 14:23:47 +0300247static const struct sdhci_pci_fixes sdhci_intel_mfd_emmc = {
248 .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
249 .probe_slot = mfd_emmc_probe_slot,
Adrian Hunter0f201652011-08-29 16:42:13 +0300250 .remove_slot = mfd_emmc_remove_slot,
Adrian Hunter0d013bc2011-06-29 14:23:47 +0300251};
252
Jennifer Li26daa1e2010-11-17 23:01:59 -0500253/* O2Micro extra registers */
254#define O2_SD_LOCK_WP 0xD3
255#define O2_SD_MULTI_VCC3V 0xEE
256#define O2_SD_CLKREQ 0xEC
257#define O2_SD_CAPS 0xE0
258#define O2_SD_ADMA1 0xE2
259#define O2_SD_ADMA2 0xE7
260#define O2_SD_INF_MOD 0xF1
261
262static int o2_probe(struct sdhci_pci_chip *chip)
263{
264 int ret;
265 u8 scratch;
266
267 switch (chip->pdev->device) {
268 case PCI_DEVICE_ID_O2_8220:
269 case PCI_DEVICE_ID_O2_8221:
270 case PCI_DEVICE_ID_O2_8320:
271 case PCI_DEVICE_ID_O2_8321:
272 /* This extra setup is required due to broken ADMA. */
273 ret = pci_read_config_byte(chip->pdev, O2_SD_LOCK_WP, &scratch);
274 if (ret)
275 return ret;
276 scratch &= 0x7f;
277 pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch);
278
279 /* Set Multi 3 to VCC3V# */
280 pci_write_config_byte(chip->pdev, O2_SD_MULTI_VCC3V, 0x08);
281
282 /* Disable CLK_REQ# support after media DET */
283 ret = pci_read_config_byte(chip->pdev, O2_SD_CLKREQ, &scratch);
284 if (ret)
285 return ret;
286 scratch |= 0x20;
287 pci_write_config_byte(chip->pdev, O2_SD_CLKREQ, scratch);
288
289 /* Choose capabilities, enable SDMA. We have to write 0x01
290 * to the capabilities register first to unlock it.
291 */
292 ret = pci_read_config_byte(chip->pdev, O2_SD_CAPS, &scratch);
293 if (ret)
294 return ret;
295 scratch |= 0x01;
296 pci_write_config_byte(chip->pdev, O2_SD_CAPS, scratch);
297 pci_write_config_byte(chip->pdev, O2_SD_CAPS, 0x73);
298
299 /* Disable ADMA1/2 */
300 pci_write_config_byte(chip->pdev, O2_SD_ADMA1, 0x39);
301 pci_write_config_byte(chip->pdev, O2_SD_ADMA2, 0x08);
302
303 /* Disable the infinite transfer mode */
304 ret = pci_read_config_byte(chip->pdev, O2_SD_INF_MOD, &scratch);
305 if (ret)
306 return ret;
307 scratch |= 0x08;
308 pci_write_config_byte(chip->pdev, O2_SD_INF_MOD, scratch);
309
310 /* Lock WP */
311 ret = pci_read_config_byte(chip->pdev, O2_SD_LOCK_WP, &scratch);
312 if (ret)
313 return ret;
314 scratch |= 0x80;
315 pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch);
316 }
317
318 return 0;
319}
320
Pierre Ossman45211e22008-03-24 13:09:09 +0100321static int jmicron_pmos(struct sdhci_pci_chip *chip, int on)
322{
323 u8 scratch;
324 int ret;
325
326 ret = pci_read_config_byte(chip->pdev, 0xAE, &scratch);
327 if (ret)
328 return ret;
329
330 /*
331 * Turn PMOS on [bit 0], set over current detection to 2.4 V
332 * [bit 1:2] and enable over current debouncing [bit 6].
333 */
334 if (on)
335 scratch |= 0x47;
336 else
337 scratch &= ~0x47;
338
339 ret = pci_write_config_byte(chip->pdev, 0xAE, scratch);
340 if (ret)
341 return ret;
342
343 return 0;
344}
345
346static int jmicron_probe(struct sdhci_pci_chip *chip)
347{
348 int ret;
Takashi Iwai8f230f42010-12-08 10:04:30 +0100349 u16 mmcdev = 0;
Pierre Ossman45211e22008-03-24 13:09:09 +0100350
Pierre Ossman93fc48c2008-06-28 18:21:41 +0200351 if (chip->pdev->revision == 0) {
352 chip->quirks |= SDHCI_QUIRK_32BIT_DMA_ADDR |
353 SDHCI_QUIRK_32BIT_DMA_SIZE |
Pierre Ossman2134a922008-06-28 18:28:51 +0200354 SDHCI_QUIRK_32BIT_ADMA_SIZE |
Pierre Ossman4a3cba32008-07-29 00:11:16 +0200355 SDHCI_QUIRK_RESET_AFTER_REQUEST |
Pierre Ossman86a6a872009-02-02 21:13:49 +0100356 SDHCI_QUIRK_BROKEN_SMALL_PIO;
Pierre Ossman93fc48c2008-06-28 18:21:41 +0200357 }
358
Pierre Ossman45211e22008-03-24 13:09:09 +0100359 /*
Pierre Ossman44894282008-04-04 19:36:59 +0200360 * JMicron chips can have two interfaces to the same hardware
361 * in order to work around limitations in Microsoft's driver.
362 * We need to make sure we only bind to one of them.
363 *
364 * This code assumes two things:
365 *
366 * 1. The PCI code adds subfunctions in order.
367 *
368 * 2. The MMC interface has a lower subfunction number
369 * than the SD interface.
370 */
Takashi Iwai8f230f42010-12-08 10:04:30 +0100371 if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_SD)
372 mmcdev = PCI_DEVICE_ID_JMICRON_JMB38X_MMC;
373 else if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_SD)
374 mmcdev = PCI_DEVICE_ID_JMICRON_JMB388_ESD;
375
376 if (mmcdev) {
Pierre Ossman44894282008-04-04 19:36:59 +0200377 struct pci_dev *sd_dev;
378
379 sd_dev = NULL;
380 while ((sd_dev = pci_get_device(PCI_VENDOR_ID_JMICRON,
Takashi Iwai8f230f42010-12-08 10:04:30 +0100381 mmcdev, sd_dev)) != NULL) {
Pierre Ossman44894282008-04-04 19:36:59 +0200382 if ((PCI_SLOT(chip->pdev->devfn) ==
383 PCI_SLOT(sd_dev->devfn)) &&
384 (chip->pdev->bus == sd_dev->bus))
385 break;
386 }
387
388 if (sd_dev) {
389 pci_dev_put(sd_dev);
390 dev_info(&chip->pdev->dev, "Refusing to bind to "
391 "secondary interface.\n");
392 return -ENODEV;
393 }
394 }
395
396 /*
Pierre Ossman45211e22008-03-24 13:09:09 +0100397 * JMicron chips need a bit of a nudge to enable the power
398 * output pins.
399 */
400 ret = jmicron_pmos(chip, 1);
401 if (ret) {
402 dev_err(&chip->pdev->dev, "Failure enabling card power\n");
403 return ret;
404 }
405
Takashi Iwai82b0e232011-04-21 20:26:38 +0200406 /* quirk for unsable RO-detection on JM388 chips */
407 if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_SD ||
408 chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD)
409 chip->quirks |= SDHCI_QUIRK_UNSTABLE_RO_DETECT;
410
Pierre Ossman45211e22008-03-24 13:09:09 +0100411 return 0;
412}
413
Pierre Ossman44894282008-04-04 19:36:59 +0200414static void jmicron_enable_mmc(struct sdhci_host *host, int on)
415{
416 u8 scratch;
417
418 scratch = readb(host->ioaddr + 0xC0);
419
420 if (on)
421 scratch |= 0x01;
422 else
423 scratch &= ~0x01;
424
425 writeb(scratch, host->ioaddr + 0xC0);
426}
427
428static int jmicron_probe_slot(struct sdhci_pci_slot *slot)
429{
Pierre Ossman2134a922008-06-28 18:28:51 +0200430 if (slot->chip->pdev->revision == 0) {
431 u16 version;
432
433 version = readl(slot->host->ioaddr + SDHCI_HOST_VERSION);
434 version = (version & SDHCI_VENDOR_VER_MASK) >>
435 SDHCI_VENDOR_VER_SHIFT;
436
437 /*
438 * Older versions of the chip have lots of nasty glitches
439 * in the ADMA engine. It's best just to avoid it
440 * completely.
441 */
442 if (version < 0xAC)
443 slot->host->quirks |= SDHCI_QUIRK_BROKEN_ADMA;
444 }
445
Takashi Iwai8f230f42010-12-08 10:04:30 +0100446 /* JM388 MMC doesn't support 1.8V while SD supports it */
447 if (slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD) {
448 slot->host->ocr_avail_sd = MMC_VDD_32_33 | MMC_VDD_33_34 |
449 MMC_VDD_29_30 | MMC_VDD_30_31 |
450 MMC_VDD_165_195; /* allow 1.8V */
451 slot->host->ocr_avail_mmc = MMC_VDD_32_33 | MMC_VDD_33_34 |
452 MMC_VDD_29_30 | MMC_VDD_30_31; /* no 1.8V for MMC */
453 }
454
Pierre Ossman44894282008-04-04 19:36:59 +0200455 /*
456 * The secondary interface requires a bit set to get the
457 * interrupts.
458 */
Takashi Iwai8f230f42010-12-08 10:04:30 +0100459 if (slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC ||
460 slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD)
Pierre Ossman44894282008-04-04 19:36:59 +0200461 jmicron_enable_mmc(slot->host, 1);
462
Takashi Iwaid75c1082010-12-16 17:54:14 +0100463 slot->host->mmc->caps |= MMC_CAP_BUS_WIDTH_TEST;
464
Pierre Ossman44894282008-04-04 19:36:59 +0200465 return 0;
466}
467
Pierre Ossman1e728592008-04-16 19:13:13 +0200468static void jmicron_remove_slot(struct sdhci_pci_slot *slot, int dead)
Pierre Ossman44894282008-04-04 19:36:59 +0200469{
Pierre Ossman1e728592008-04-16 19:13:13 +0200470 if (dead)
471 return;
472
Takashi Iwai8f230f42010-12-08 10:04:30 +0100473 if (slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC ||
474 slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD)
Pierre Ossman44894282008-04-04 19:36:59 +0200475 jmicron_enable_mmc(slot->host, 0);
476}
477
478static int jmicron_suspend(struct sdhci_pci_chip *chip, pm_message_t state)
479{
480 int i;
481
Takashi Iwai8f230f42010-12-08 10:04:30 +0100482 if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC ||
483 chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD) {
Ameya Palandeb177bc92011-04-05 21:13:13 +0300484 for (i = 0; i < chip->num_slots; i++)
Pierre Ossman44894282008-04-04 19:36:59 +0200485 jmicron_enable_mmc(chip->slots[i]->host, 0);
486 }
487
488 return 0;
489}
490
Pierre Ossman45211e22008-03-24 13:09:09 +0100491static int jmicron_resume(struct sdhci_pci_chip *chip)
492{
Pierre Ossman44894282008-04-04 19:36:59 +0200493 int ret, i;
494
Takashi Iwai8f230f42010-12-08 10:04:30 +0100495 if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC ||
496 chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD) {
Ameya Palandeb177bc92011-04-05 21:13:13 +0300497 for (i = 0; i < chip->num_slots; i++)
Pierre Ossman44894282008-04-04 19:36:59 +0200498 jmicron_enable_mmc(chip->slots[i]->host, 1);
499 }
Pierre Ossman45211e22008-03-24 13:09:09 +0100500
501 ret = jmicron_pmos(chip, 1);
502 if (ret) {
503 dev_err(&chip->pdev->dev, "Failure enabling card power\n");
504 return ret;
505 }
506
507 return 0;
508}
509
Jennifer Li26daa1e2010-11-17 23:01:59 -0500510static const struct sdhci_pci_fixes sdhci_o2 = {
511 .probe = o2_probe,
512};
513
Pierre Ossman22606402008-03-23 19:33:23 +0100514static const struct sdhci_pci_fixes sdhci_jmicron = {
Pierre Ossman45211e22008-03-24 13:09:09 +0100515 .probe = jmicron_probe,
516
Pierre Ossman44894282008-04-04 19:36:59 +0200517 .probe_slot = jmicron_probe_slot,
518 .remove_slot = jmicron_remove_slot,
519
520 .suspend = jmicron_suspend,
Pierre Ossman45211e22008-03-24 13:09:09 +0100521 .resume = jmicron_resume,
Pierre Ossman22606402008-03-23 19:33:23 +0100522};
523
Nicolas Pitrea7a61862009-12-14 18:01:26 -0800524/* SysKonnect CardBus2SDIO extra registers */
525#define SYSKT_CTRL 0x200
526#define SYSKT_RDFIFO_STAT 0x204
527#define SYSKT_WRFIFO_STAT 0x208
528#define SYSKT_POWER_DATA 0x20c
529#define SYSKT_POWER_330 0xef
530#define SYSKT_POWER_300 0xf8
531#define SYSKT_POWER_184 0xcc
532#define SYSKT_POWER_CMD 0x20d
533#define SYSKT_POWER_START (1 << 7)
534#define SYSKT_POWER_STATUS 0x20e
535#define SYSKT_POWER_STATUS_OK (1 << 0)
536#define SYSKT_BOARD_REV 0x210
537#define SYSKT_CHIP_REV 0x211
538#define SYSKT_CONF_DATA 0x212
539#define SYSKT_CONF_DATA_1V8 (1 << 2)
540#define SYSKT_CONF_DATA_2V5 (1 << 1)
541#define SYSKT_CONF_DATA_3V3 (1 << 0)
542
543static int syskt_probe(struct sdhci_pci_chip *chip)
544{
545 if ((chip->pdev->class & 0x0000FF) == PCI_SDHCI_IFVENDOR) {
546 chip->pdev->class &= ~0x0000FF;
547 chip->pdev->class |= PCI_SDHCI_IFDMA;
548 }
549 return 0;
550}
551
552static int syskt_probe_slot(struct sdhci_pci_slot *slot)
553{
554 int tm, ps;
555
556 u8 board_rev = readb(slot->host->ioaddr + SYSKT_BOARD_REV);
557 u8 chip_rev = readb(slot->host->ioaddr + SYSKT_CHIP_REV);
558 dev_info(&slot->chip->pdev->dev, "SysKonnect CardBus2SDIO, "
559 "board rev %d.%d, chip rev %d.%d\n",
560 board_rev >> 4, board_rev & 0xf,
561 chip_rev >> 4, chip_rev & 0xf);
562 if (chip_rev >= 0x20)
563 slot->host->quirks |= SDHCI_QUIRK_FORCE_DMA;
564
565 writeb(SYSKT_POWER_330, slot->host->ioaddr + SYSKT_POWER_DATA);
566 writeb(SYSKT_POWER_START, slot->host->ioaddr + SYSKT_POWER_CMD);
567 udelay(50);
568 tm = 10; /* Wait max 1 ms */
569 do {
570 ps = readw(slot->host->ioaddr + SYSKT_POWER_STATUS);
571 if (ps & SYSKT_POWER_STATUS_OK)
572 break;
573 udelay(100);
574 } while (--tm);
575 if (!tm) {
576 dev_err(&slot->chip->pdev->dev,
577 "power regulator never stabilized");
578 writeb(0, slot->host->ioaddr + SYSKT_POWER_CMD);
579 return -ENODEV;
580 }
581
582 return 0;
583}
584
585static const struct sdhci_pci_fixes sdhci_syskt = {
586 .quirks = SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER,
587 .probe = syskt_probe,
588 .probe_slot = syskt_probe_slot,
589};
590
Harald Welte557b0692009-06-18 16:53:38 +0200591static int via_probe(struct sdhci_pci_chip *chip)
592{
593 if (chip->pdev->revision == 0x10)
594 chip->quirks |= SDHCI_QUIRK_DELAY_AFTER_POWER;
595
596 return 0;
597}
598
599static const struct sdhci_pci_fixes sdhci_via = {
600 .probe = via_probe,
601};
602
Pierre Ossman22606402008-03-23 19:33:23 +0100603static const struct pci_device_id pci_ids[] __devinitdata = {
604 {
605 .vendor = PCI_VENDOR_ID_RICOH,
606 .device = PCI_DEVICE_ID_RICOH_R5C822,
607 .subvendor = PCI_ANY_ID,
608 .subdevice = PCI_ANY_ID,
609 .driver_data = (kernel_ulong_t)&sdhci_ricoh,
610 },
611
612 {
Maxim Levitskyccc92c22010-08-10 18:01:42 -0700613 .vendor = PCI_VENDOR_ID_RICOH,
614 .device = 0x843,
615 .subvendor = PCI_ANY_ID,
616 .subdevice = PCI_ANY_ID,
617 .driver_data = (kernel_ulong_t)&sdhci_ricoh_mmc,
618 },
619
620 {
Pablo Castillo568133e2010-08-10 18:02:01 -0700621 .vendor = PCI_VENDOR_ID_RICOH,
622 .device = 0xe822,
623 .subvendor = PCI_ANY_ID,
624 .subdevice = PCI_ANY_ID,
625 .driver_data = (kernel_ulong_t)&sdhci_ricoh_mmc,
626 },
627
628 {
Manoj Iyer5fd11c02011-02-11 16:25:31 -0600629 .vendor = PCI_VENDOR_ID_RICOH,
630 .device = 0xe823,
631 .subvendor = PCI_ANY_ID,
632 .subdevice = PCI_ANY_ID,
633 .driver_data = (kernel_ulong_t)&sdhci_ricoh_mmc,
634 },
635
636 {
Pierre Ossman22606402008-03-23 19:33:23 +0100637 .vendor = PCI_VENDOR_ID_ENE,
638 .device = PCI_DEVICE_ID_ENE_CB712_SD,
639 .subvendor = PCI_ANY_ID,
640 .subdevice = PCI_ANY_ID,
641 .driver_data = (kernel_ulong_t)&sdhci_ene_712,
642 },
643
644 {
645 .vendor = PCI_VENDOR_ID_ENE,
646 .device = PCI_DEVICE_ID_ENE_CB712_SD_2,
647 .subvendor = PCI_ANY_ID,
648 .subdevice = PCI_ANY_ID,
649 .driver_data = (kernel_ulong_t)&sdhci_ene_712,
650 },
651
652 {
653 .vendor = PCI_VENDOR_ID_ENE,
654 .device = PCI_DEVICE_ID_ENE_CB714_SD,
655 .subvendor = PCI_ANY_ID,
656 .subdevice = PCI_ANY_ID,
657 .driver_data = (kernel_ulong_t)&sdhci_ene_714,
658 },
659
660 {
661 .vendor = PCI_VENDOR_ID_ENE,
662 .device = PCI_DEVICE_ID_ENE_CB714_SD_2,
663 .subvendor = PCI_ANY_ID,
664 .subdevice = PCI_ANY_ID,
665 .driver_data = (kernel_ulong_t)&sdhci_ene_714,
666 },
667
668 {
669 .vendor = PCI_VENDOR_ID_MARVELL,
David Woodhouse8c5eb882008-09-03 09:45:57 +0100670 .device = PCI_DEVICE_ID_MARVELL_88ALP01_SD,
Pierre Ossman22606402008-03-23 19:33:23 +0100671 .subvendor = PCI_ANY_ID,
672 .subdevice = PCI_ANY_ID,
673 .driver_data = (kernel_ulong_t)&sdhci_cafe,
674 },
675
676 {
677 .vendor = PCI_VENDOR_ID_JMICRON,
678 .device = PCI_DEVICE_ID_JMICRON_JMB38X_SD,
679 .subvendor = PCI_ANY_ID,
680 .subdevice = PCI_ANY_ID,
681 .driver_data = (kernel_ulong_t)&sdhci_jmicron,
682 },
683
Pierre Ossman44894282008-04-04 19:36:59 +0200684 {
685 .vendor = PCI_VENDOR_ID_JMICRON,
686 .device = PCI_DEVICE_ID_JMICRON_JMB38X_MMC,
687 .subvendor = PCI_ANY_ID,
688 .subdevice = PCI_ANY_ID,
689 .driver_data = (kernel_ulong_t)&sdhci_jmicron,
690 },
691
Harald Welte557b0692009-06-18 16:53:38 +0200692 {
Takashi Iwai8f230f42010-12-08 10:04:30 +0100693 .vendor = PCI_VENDOR_ID_JMICRON,
694 .device = PCI_DEVICE_ID_JMICRON_JMB388_SD,
695 .subvendor = PCI_ANY_ID,
696 .subdevice = PCI_ANY_ID,
697 .driver_data = (kernel_ulong_t)&sdhci_jmicron,
698 },
699
700 {
701 .vendor = PCI_VENDOR_ID_JMICRON,
702 .device = PCI_DEVICE_ID_JMICRON_JMB388_ESD,
703 .subvendor = PCI_ANY_ID,
704 .subdevice = PCI_ANY_ID,
705 .driver_data = (kernel_ulong_t)&sdhci_jmicron,
706 },
707
708 {
Nicolas Pitrea7a61862009-12-14 18:01:26 -0800709 .vendor = PCI_VENDOR_ID_SYSKONNECT,
710 .device = 0x8000,
711 .subvendor = PCI_ANY_ID,
712 .subdevice = PCI_ANY_ID,
713 .driver_data = (kernel_ulong_t)&sdhci_syskt,
714 },
715
716 {
Harald Welte557b0692009-06-18 16:53:38 +0200717 .vendor = PCI_VENDOR_ID_VIA,
718 .device = 0x95d0,
719 .subvendor = PCI_ANY_ID,
720 .subdevice = PCI_ANY_ID,
721 .driver_data = (kernel_ulong_t)&sdhci_via,
722 },
723
Xiaochen Shen29229052010-10-04 15:24:52 +0100724 {
725 .vendor = PCI_VENDOR_ID_INTEL,
Alan Coxf9ee3ea2010-10-04 15:25:11 +0100726 .device = PCI_DEVICE_ID_INTEL_MRST_SD0,
727 .subvendor = PCI_ANY_ID,
728 .subdevice = PCI_ANY_ID,
729 .driver_data = (kernel_ulong_t)&sdhci_intel_mrst_hc0,
730 },
731
732 {
733 .vendor = PCI_VENDOR_ID_INTEL,
734 .device = PCI_DEVICE_ID_INTEL_MRST_SD1,
735 .subvendor = PCI_ANY_ID,
736 .subdevice = PCI_ANY_ID,
Jacob Pan35ac6f02010-11-09 13:57:29 +0000737 .driver_data = (kernel_ulong_t)&sdhci_intel_mrst_hc1_hc2,
738 },
739
740 {
741 .vendor = PCI_VENDOR_ID_INTEL,
742 .device = PCI_DEVICE_ID_INTEL_MRST_SD2,
743 .subvendor = PCI_ANY_ID,
744 .subdevice = PCI_ANY_ID,
745 .driver_data = (kernel_ulong_t)&sdhci_intel_mrst_hc1_hc2,
Alan Coxf9ee3ea2010-10-04 15:25:11 +0100746 },
747
748 {
749 .vendor = PCI_VENDOR_ID_INTEL,
Xiaochen Shen29229052010-10-04 15:24:52 +0100750 .device = PCI_DEVICE_ID_INTEL_MFD_SD,
751 .subvendor = PCI_ANY_ID,
752 .subdevice = PCI_ANY_ID,
753 .driver_data = (kernel_ulong_t)&sdhci_intel_mfd_sd,
754 },
755
756 {
757 .vendor = PCI_VENDOR_ID_INTEL,
758 .device = PCI_DEVICE_ID_INTEL_MFD_SDIO1,
759 .subvendor = PCI_ANY_ID,
760 .subdevice = PCI_ANY_ID,
Adrian Hunter0d013bc2011-06-29 14:23:47 +0300761 .driver_data = (kernel_ulong_t)&sdhci_intel_mfd_sdio,
Xiaochen Shen29229052010-10-04 15:24:52 +0100762 },
763
764 {
765 .vendor = PCI_VENDOR_ID_INTEL,
766 .device = PCI_DEVICE_ID_INTEL_MFD_SDIO2,
767 .subvendor = PCI_ANY_ID,
768 .subdevice = PCI_ANY_ID,
Adrian Hunter0d013bc2011-06-29 14:23:47 +0300769 .driver_data = (kernel_ulong_t)&sdhci_intel_mfd_sdio,
Xiaochen Shen29229052010-10-04 15:24:52 +0100770 },
771
772 {
773 .vendor = PCI_VENDOR_ID_INTEL,
774 .device = PCI_DEVICE_ID_INTEL_MFD_EMMC0,
775 .subvendor = PCI_ANY_ID,
776 .subdevice = PCI_ANY_ID,
Adrian Hunter0d013bc2011-06-29 14:23:47 +0300777 .driver_data = (kernel_ulong_t)&sdhci_intel_mfd_emmc,
Xiaochen Shen29229052010-10-04 15:24:52 +0100778 },
779
780 {
781 .vendor = PCI_VENDOR_ID_INTEL,
782 .device = PCI_DEVICE_ID_INTEL_MFD_EMMC1,
783 .subvendor = PCI_ANY_ID,
784 .subdevice = PCI_ANY_ID,
Adrian Hunter0d013bc2011-06-29 14:23:47 +0300785 .driver_data = (kernel_ulong_t)&sdhci_intel_mfd_emmc,
Xiaochen Shen29229052010-10-04 15:24:52 +0100786 },
787
Jennifer Li26daa1e2010-11-17 23:01:59 -0500788 {
789 .vendor = PCI_VENDOR_ID_O2,
790 .device = PCI_DEVICE_ID_O2_8120,
791 .subvendor = PCI_ANY_ID,
792 .subdevice = PCI_ANY_ID,
793 .driver_data = (kernel_ulong_t)&sdhci_o2,
794 },
795
796 {
797 .vendor = PCI_VENDOR_ID_O2,
798 .device = PCI_DEVICE_ID_O2_8220,
799 .subvendor = PCI_ANY_ID,
800 .subdevice = PCI_ANY_ID,
801 .driver_data = (kernel_ulong_t)&sdhci_o2,
802 },
803
804 {
805 .vendor = PCI_VENDOR_ID_O2,
806 .device = PCI_DEVICE_ID_O2_8221,
807 .subvendor = PCI_ANY_ID,
808 .subdevice = PCI_ANY_ID,
809 .driver_data = (kernel_ulong_t)&sdhci_o2,
810 },
811
812 {
813 .vendor = PCI_VENDOR_ID_O2,
814 .device = PCI_DEVICE_ID_O2_8320,
815 .subvendor = PCI_ANY_ID,
816 .subdevice = PCI_ANY_ID,
817 .driver_data = (kernel_ulong_t)&sdhci_o2,
818 },
819
820 {
821 .vendor = PCI_VENDOR_ID_O2,
822 .device = PCI_DEVICE_ID_O2_8321,
823 .subvendor = PCI_ANY_ID,
824 .subdevice = PCI_ANY_ID,
825 .driver_data = (kernel_ulong_t)&sdhci_o2,
826 },
827
Pierre Ossman22606402008-03-23 19:33:23 +0100828 { /* Generic SD host controller */
829 PCI_DEVICE_CLASS((PCI_CLASS_SYSTEM_SDHCI << 8), 0xFFFF00)
830 },
831
832 { /* end: all zeroes */ },
833};
834
835MODULE_DEVICE_TABLE(pci, pci_ids);
836
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100837/*****************************************************************************\
838 * *
839 * SDHCI core callbacks *
840 * *
841\*****************************************************************************/
842
843static int sdhci_pci_enable_dma(struct sdhci_host *host)
844{
845 struct sdhci_pci_slot *slot;
846 struct pci_dev *pdev;
847 int ret;
848
849 slot = sdhci_priv(host);
850 pdev = slot->chip->pdev;
851
852 if (((pdev->class & 0xFFFF00) == (PCI_CLASS_SYSTEM_SDHCI << 8)) &&
853 ((pdev->class & 0x0000FF) != PCI_SDHCI_IFDMA) &&
Richard Röjforsa13abc72009-09-22 16:45:30 -0700854 (host->flags & SDHCI_USE_SDMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100855 dev_warn(&pdev->dev, "Will use DMA mode even though HW "
856 "doesn't fully claim to support it.\n");
857 }
858
Yang Hongyang284901a2009-04-06 19:01:15 -0700859 ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100860 if (ret)
861 return ret;
862
863 pci_set_master(pdev);
864
865 return 0;
866}
867
Major Lee68077b02011-06-29 14:23:46 +0300868static int sdhci_pci_8bit_width(struct sdhci_host *host, int width)
869{
870 u8 ctrl;
871
872 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
873
874 switch (width) {
875 case MMC_BUS_WIDTH_8:
876 ctrl |= SDHCI_CTRL_8BITBUS;
877 ctrl &= ~SDHCI_CTRL_4BITBUS;
878 break;
879 case MMC_BUS_WIDTH_4:
880 ctrl |= SDHCI_CTRL_4BITBUS;
881 ctrl &= ~SDHCI_CTRL_8BITBUS;
882 break;
883 default:
884 ctrl &= ~(SDHCI_CTRL_8BITBUS | SDHCI_CTRL_4BITBUS);
885 break;
886 }
887
888 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
889
890 return 0;
891}
892
Adrian Hunter0f201652011-08-29 16:42:13 +0300893static void sdhci_pci_hw_reset(struct sdhci_host *host)
894{
895 struct sdhci_pci_slot *slot = sdhci_priv(host);
896 int rst_n_gpio = slot->rst_n_gpio;
897
898 if (!gpio_is_valid(rst_n_gpio))
899 return;
900 gpio_set_value_cansleep(rst_n_gpio, 0);
901 /* For eMMC, minimum is 1us but give it 10us for good measure */
902 udelay(10);
903 gpio_set_value_cansleep(rst_n_gpio, 1);
904 /* For eMMC, minimum is 200us but give it 300us for good measure */
905 usleep_range(300, 1000);
906}
907
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100908static struct sdhci_ops sdhci_pci_ops = {
909 .enable_dma = sdhci_pci_enable_dma,
Major Lee68077b02011-06-29 14:23:46 +0300910 .platform_8bit_width = sdhci_pci_8bit_width,
Adrian Hunter0f201652011-08-29 16:42:13 +0300911 .hw_reset = sdhci_pci_hw_reset,
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100912};
913
914/*****************************************************************************\
915 * *
916 * Suspend/resume *
917 * *
918\*****************************************************************************/
919
920#ifdef CONFIG_PM
921
Ameya Palandeb177bc92011-04-05 21:13:13 +0300922static int sdhci_pci_suspend(struct pci_dev *pdev, pm_message_t state)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100923{
924 struct sdhci_pci_chip *chip;
925 struct sdhci_pci_slot *slot;
Daniel Drake5f619702010-11-04 22:20:39 +0000926 mmc_pm_flag_t slot_pm_flags;
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800927 mmc_pm_flag_t pm_flags = 0;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100928 int i, ret;
929
930 chip = pci_get_drvdata(pdev);
931 if (!chip)
932 return 0;
933
Ameya Palandeb177bc92011-04-05 21:13:13 +0300934 for (i = 0; i < chip->num_slots; i++) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100935 slot = chip->slots[i];
936 if (!slot)
937 continue;
938
939 ret = sdhci_suspend_host(slot->host, state);
940
941 if (ret) {
Ameya Palandeb177bc92011-04-05 21:13:13 +0300942 for (i--; i >= 0; i--)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100943 sdhci_resume_host(chip->slots[i]->host);
944 return ret;
945 }
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800946
Daniel Drake5f619702010-11-04 22:20:39 +0000947 slot_pm_flags = slot->host->mmc->pm_flags;
948 if (slot_pm_flags & MMC_PM_WAKE_SDIO_IRQ)
949 sdhci_enable_irq_wakeups(slot->host);
950
951 pm_flags |= slot_pm_flags;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100952 }
953
Pierre Ossman44894282008-04-04 19:36:59 +0200954 if (chip->fixes && chip->fixes->suspend) {
955 ret = chip->fixes->suspend(chip, state);
956 if (ret) {
Ameya Palandeb177bc92011-04-05 21:13:13 +0300957 for (i = chip->num_slots - 1; i >= 0; i--)
Pierre Ossman44894282008-04-04 19:36:59 +0200958 sdhci_resume_host(chip->slots[i]->host);
959 return ret;
960 }
961 }
962
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100963 pci_save_state(pdev);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800964 if (pm_flags & MMC_PM_KEEP_POWER) {
Daniel Drake5f619702010-11-04 22:20:39 +0000965 if (pm_flags & MMC_PM_WAKE_SDIO_IRQ) {
966 pci_pme_active(pdev, true);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800967 pci_enable_wake(pdev, PCI_D3hot, 1);
Daniel Drake5f619702010-11-04 22:20:39 +0000968 }
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800969 pci_set_power_state(pdev, PCI_D3hot);
970 } else {
971 pci_enable_wake(pdev, pci_choose_state(pdev, state), 0);
972 pci_disable_device(pdev);
973 pci_set_power_state(pdev, pci_choose_state(pdev, state));
974 }
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100975
976 return 0;
977}
978
Ameya Palandeb177bc92011-04-05 21:13:13 +0300979static int sdhci_pci_resume(struct pci_dev *pdev)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100980{
981 struct sdhci_pci_chip *chip;
982 struct sdhci_pci_slot *slot;
983 int i, ret;
984
985 chip = pci_get_drvdata(pdev);
986 if (!chip)
987 return 0;
988
989 pci_set_power_state(pdev, PCI_D0);
990 pci_restore_state(pdev);
991 ret = pci_enable_device(pdev);
992 if (ret)
993 return ret;
994
Pierre Ossman45211e22008-03-24 13:09:09 +0100995 if (chip->fixes && chip->fixes->resume) {
996 ret = chip->fixes->resume(chip);
997 if (ret)
998 return ret;
999 }
1000
Ameya Palandeb177bc92011-04-05 21:13:13 +03001001 for (i = 0; i < chip->num_slots; i++) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001002 slot = chip->slots[i];
1003 if (!slot)
1004 continue;
1005
1006 ret = sdhci_resume_host(slot->host);
1007 if (ret)
1008 return ret;
1009 }
1010
1011 return 0;
1012}
1013
1014#else /* CONFIG_PM */
1015
1016#define sdhci_pci_suspend NULL
1017#define sdhci_pci_resume NULL
1018
1019#endif /* CONFIG_PM */
1020
1021/*****************************************************************************\
1022 * *
1023 * Device probing/removal *
1024 * *
1025\*****************************************************************************/
1026
1027static struct sdhci_pci_slot * __devinit sdhci_pci_probe_slot(
1028 struct pci_dev *pdev, struct sdhci_pci_chip *chip, int bar)
1029{
1030 struct sdhci_pci_slot *slot;
1031 struct sdhci_host *host;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001032 int ret;
1033
1034 if (!(pci_resource_flags(pdev, bar) & IORESOURCE_MEM)) {
1035 dev_err(&pdev->dev, "BAR %d is not iomem. Aborting.\n", bar);
1036 return ERR_PTR(-ENODEV);
1037 }
1038
1039 if (pci_resource_len(pdev, bar) != 0x100) {
1040 dev_err(&pdev->dev, "Invalid iomem size. You may "
1041 "experience problems.\n");
1042 }
1043
1044 if ((pdev->class & 0x0000FF) == PCI_SDHCI_IFVENDOR) {
1045 dev_err(&pdev->dev, "Vendor specific interface. Aborting.\n");
1046 return ERR_PTR(-ENODEV);
1047 }
1048
1049 if ((pdev->class & 0x0000FF) > PCI_SDHCI_IFVENDOR) {
1050 dev_err(&pdev->dev, "Unknown interface. Aborting.\n");
1051 return ERR_PTR(-ENODEV);
1052 }
1053
1054 host = sdhci_alloc_host(&pdev->dev, sizeof(struct sdhci_pci_slot));
1055 if (IS_ERR(host)) {
Dan Carpenterc60a32c2009-04-10 23:31:10 +02001056 dev_err(&pdev->dev, "cannot allocate host\n");
Julia Lawalldc0fd7b2010-05-26 14:42:11 -07001057 return ERR_CAST(host);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001058 }
1059
1060 slot = sdhci_priv(host);
1061
1062 slot->chip = chip;
1063 slot->host = host;
1064 slot->pci_bar = bar;
Adrian Hunter0f201652011-08-29 16:42:13 +03001065 slot->rst_n_gpio = -EINVAL;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001066
1067 host->hw_name = "PCI";
1068 host->ops = &sdhci_pci_ops;
1069 host->quirks = chip->quirks;
1070
1071 host->irq = pdev->irq;
1072
1073 ret = pci_request_region(pdev, bar, mmc_hostname(host->mmc));
1074 if (ret) {
1075 dev_err(&pdev->dev, "cannot request region\n");
Dan Carpenterc60a32c2009-04-10 23:31:10 +02001076 goto free;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001077 }
1078
Arjan van de Ven092f82e2008-09-28 16:15:56 -07001079 host->ioaddr = pci_ioremap_bar(pdev, bar);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001080 if (!host->ioaddr) {
1081 dev_err(&pdev->dev, "failed to remap registers\n");
Chris Ball9fdcdbb2011-03-29 00:46:12 -04001082 ret = -ENOMEM;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001083 goto release;
1084 }
1085
Pierre Ossman44894282008-04-04 19:36:59 +02001086 if (chip->fixes && chip->fixes->probe_slot) {
1087 ret = chip->fixes->probe_slot(slot);
1088 if (ret)
1089 goto unmap;
1090 }
1091
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08001092 host->mmc->pm_caps = MMC_PM_KEEP_POWER | MMC_PM_WAKE_SDIO_IRQ;
1093
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001094 ret = sdhci_add_host(host);
1095 if (ret)
Pierre Ossman44894282008-04-04 19:36:59 +02001096 goto remove;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001097
1098 return slot;
1099
Pierre Ossman44894282008-04-04 19:36:59 +02001100remove:
1101 if (chip->fixes && chip->fixes->remove_slot)
Pierre Ossman1e728592008-04-16 19:13:13 +02001102 chip->fixes->remove_slot(slot, 0);
Pierre Ossman44894282008-04-04 19:36:59 +02001103
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001104unmap:
1105 iounmap(host->ioaddr);
1106
1107release:
1108 pci_release_region(pdev, bar);
Dan Carpenterc60a32c2009-04-10 23:31:10 +02001109
1110free:
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001111 sdhci_free_host(host);
1112
1113 return ERR_PTR(ret);
1114}
1115
1116static void sdhci_pci_remove_slot(struct sdhci_pci_slot *slot)
1117{
Pierre Ossman1e728592008-04-16 19:13:13 +02001118 int dead;
1119 u32 scratch;
1120
1121 dead = 0;
1122 scratch = readl(slot->host->ioaddr + SDHCI_INT_STATUS);
1123 if (scratch == (u32)-1)
1124 dead = 1;
1125
1126 sdhci_remove_host(slot->host, dead);
Pierre Ossman44894282008-04-04 19:36:59 +02001127
1128 if (slot->chip->fixes && slot->chip->fixes->remove_slot)
Pierre Ossman1e728592008-04-16 19:13:13 +02001129 slot->chip->fixes->remove_slot(slot, dead);
Pierre Ossman44894282008-04-04 19:36:59 +02001130
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001131 pci_release_region(slot->chip->pdev, slot->pci_bar);
Pierre Ossman44894282008-04-04 19:36:59 +02001132
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001133 sdhci_free_host(slot->host);
1134}
1135
1136static int __devinit sdhci_pci_probe(struct pci_dev *pdev,
1137 const struct pci_device_id *ent)
1138{
1139 struct sdhci_pci_chip *chip;
1140 struct sdhci_pci_slot *slot;
1141
Sergei Shtylyovcf5e23e2011-03-17 16:46:17 -04001142 u8 slots, first_bar;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001143 int ret, i;
1144
1145 BUG_ON(pdev == NULL);
1146 BUG_ON(ent == NULL);
1147
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001148 dev_info(&pdev->dev, "SDHCI controller found [%04x:%04x] (rev %x)\n",
Sergei Shtylyovcf5e23e2011-03-17 16:46:17 -04001149 (int)pdev->vendor, (int)pdev->device, (int)pdev->revision);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001150
1151 ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &slots);
1152 if (ret)
1153 return ret;
1154
1155 slots = PCI_SLOT_INFO_SLOTS(slots) + 1;
1156 dev_dbg(&pdev->dev, "found %d slot(s)\n", slots);
1157 if (slots == 0)
1158 return -ENODEV;
1159
1160 BUG_ON(slots > MAX_SLOTS);
1161
1162 ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &first_bar);
1163 if (ret)
1164 return ret;
1165
1166 first_bar &= PCI_SLOT_INFO_FIRST_BAR_MASK;
1167
1168 if (first_bar > 5) {
1169 dev_err(&pdev->dev, "Invalid first BAR. Aborting.\n");
1170 return -ENODEV;
1171 }
1172
1173 ret = pci_enable_device(pdev);
1174 if (ret)
1175 return ret;
1176
1177 chip = kzalloc(sizeof(struct sdhci_pci_chip), GFP_KERNEL);
1178 if (!chip) {
1179 ret = -ENOMEM;
1180 goto err;
1181 }
1182
1183 chip->pdev = pdev;
Ameya Palandeb177bc92011-04-05 21:13:13 +03001184 chip->fixes = (const struct sdhci_pci_fixes *)ent->driver_data;
Pierre Ossman22606402008-03-23 19:33:23 +01001185 if (chip->fixes)
1186 chip->quirks = chip->fixes->quirks;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001187 chip->num_slots = slots;
1188
1189 pci_set_drvdata(pdev, chip);
1190
Pierre Ossman22606402008-03-23 19:33:23 +01001191 if (chip->fixes && chip->fixes->probe) {
1192 ret = chip->fixes->probe(chip);
1193 if (ret)
1194 goto free;
1195 }
1196
Alan Cox225d85f2010-10-04 15:24:21 +01001197 slots = chip->num_slots; /* Quirk may have changed this */
1198
Ameya Palandeb177bc92011-04-05 21:13:13 +03001199 for (i = 0; i < slots; i++) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001200 slot = sdhci_pci_probe_slot(pdev, chip, first_bar + i);
1201 if (IS_ERR(slot)) {
Ameya Palandeb177bc92011-04-05 21:13:13 +03001202 for (i--; i >= 0; i--)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001203 sdhci_pci_remove_slot(chip->slots[i]);
1204 ret = PTR_ERR(slot);
1205 goto free;
1206 }
1207
1208 chip->slots[i] = slot;
1209 }
1210
1211 return 0;
1212
1213free:
1214 pci_set_drvdata(pdev, NULL);
1215 kfree(chip);
1216
1217err:
1218 pci_disable_device(pdev);
1219 return ret;
1220}
1221
1222static void __devexit sdhci_pci_remove(struct pci_dev *pdev)
1223{
1224 int i;
1225 struct sdhci_pci_chip *chip;
1226
1227 chip = pci_get_drvdata(pdev);
1228
1229 if (chip) {
Ameya Palandeb177bc92011-04-05 21:13:13 +03001230 for (i = 0; i < chip->num_slots; i++)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001231 sdhci_pci_remove_slot(chip->slots[i]);
1232
1233 pci_set_drvdata(pdev, NULL);
1234 kfree(chip);
1235 }
1236
1237 pci_disable_device(pdev);
1238}
1239
1240static struct pci_driver sdhci_driver = {
Ameya Palandeb177bc92011-04-05 21:13:13 +03001241 .name = "sdhci-pci",
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001242 .id_table = pci_ids,
Ameya Palandeb177bc92011-04-05 21:13:13 +03001243 .probe = sdhci_pci_probe,
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001244 .remove = __devexit_p(sdhci_pci_remove),
1245 .suspend = sdhci_pci_suspend,
1246 .resume = sdhci_pci_resume,
1247};
1248
1249/*****************************************************************************\
1250 * *
1251 * Driver init/exit *
1252 * *
1253\*****************************************************************************/
1254
1255static int __init sdhci_drv_init(void)
1256{
1257 return pci_register_driver(&sdhci_driver);
1258}
1259
1260static void __exit sdhci_drv_exit(void)
1261{
1262 pci_unregister_driver(&sdhci_driver);
1263}
1264
1265module_init(sdhci_drv_init);
1266module_exit(sdhci_drv_exit);
1267
Pierre Ossman32710e82009-04-08 20:14:54 +02001268MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001269MODULE_DESCRIPTION("Secure Digital Host Controller Interface PCI driver");
1270MODULE_LICENSE("GPL");