blob: f8a17f98f3a98182394b6b4c0fe6660962a13878 [file] [log] [blame]
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001/* linux/drivers/mmc/host/sdhci-pci.c - SDHCI on PCI bus interface
2 *
3 * Copyright (C) 2005-2008 Pierre Ossman, All Rights Reserved.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or (at
8 * your option) any later version.
9 *
10 * Thanks to the following companies for their support:
11 *
12 * - JMicron (hardware and technical support)
13 */
14
15#include <linux/delay.h>
16#include <linux/highmem.h>
17#include <linux/pci.h>
18#include <linux/dma-mapping.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
Maxim Levitskyccc92c22010-08-10 18:01:42 -070020#include <linux/device.h>
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010021#include <linux/mmc/host.h>
Ameya Palandeb177bc92011-04-05 21:13:13 +030022#include <linux/scatterlist.h>
23#include <linux/io.h>
Adrian Hunter0f201652011-08-29 16:42:13 +030024#include <linux/gpio.h>
25#include <linux/sfi.h>
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010026
27#include "sdhci.h"
28
29/*
30 * PCI registers
31 */
32
33#define PCI_SDHCI_IFPIO 0x00
34#define PCI_SDHCI_IFDMA 0x01
35#define PCI_SDHCI_IFVENDOR 0x02
36
37#define PCI_SLOT_INFO 0x40 /* 8 bits */
38#define PCI_SLOT_INFO_SLOTS(x) ((x >> 4) & 7)
39#define PCI_SLOT_INFO_FIRST_BAR_MASK 0x07
40
41#define MAX_SLOTS 8
42
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010043struct sdhci_pci_chip;
Pierre Ossman44894282008-04-04 19:36:59 +020044struct sdhci_pci_slot;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010045
Pierre Ossman22606402008-03-23 19:33:23 +010046struct sdhci_pci_fixes {
47 unsigned int quirks;
48
Ameya Palandeb177bc92011-04-05 21:13:13 +030049 int (*probe) (struct sdhci_pci_chip *);
Pierre Ossman45211e22008-03-24 13:09:09 +010050
Ameya Palandeb177bc92011-04-05 21:13:13 +030051 int (*probe_slot) (struct sdhci_pci_slot *);
52 void (*remove_slot) (struct sdhci_pci_slot *, int);
Pierre Ossman44894282008-04-04 19:36:59 +020053
Ameya Palandeb177bc92011-04-05 21:13:13 +030054 int (*suspend) (struct sdhci_pci_chip *,
Pierre Ossman44894282008-04-04 19:36:59 +020055 pm_message_t);
Ameya Palandeb177bc92011-04-05 21:13:13 +030056 int (*resume) (struct sdhci_pci_chip *);
Pierre Ossman22606402008-03-23 19:33:23 +010057};
58
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010059struct sdhci_pci_slot {
60 struct sdhci_pci_chip *chip;
61 struct sdhci_host *host;
62
63 int pci_bar;
Adrian Hunter0f201652011-08-29 16:42:13 +030064 int rst_n_gpio;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010065};
66
67struct sdhci_pci_chip {
68 struct pci_dev *pdev;
Pierre Ossman22606402008-03-23 19:33:23 +010069
70 unsigned int quirks;
71 const struct sdhci_pci_fixes *fixes;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010072
73 int num_slots; /* Slots on controller */
74 struct sdhci_pci_slot *slots[MAX_SLOTS]; /* Pointers to host slots */
75};
76
Pierre Ossman22606402008-03-23 19:33:23 +010077
78/*****************************************************************************\
79 * *
80 * Hardware specific quirk handling *
81 * *
82\*****************************************************************************/
83
84static int ricoh_probe(struct sdhci_pci_chip *chip)
85{
Chris Ballc99436f2009-09-22 16:45:22 -070086 if (chip->pdev->subsystem_vendor == PCI_VENDOR_ID_SAMSUNG ||
87 chip->pdev->subsystem_vendor == PCI_VENDOR_ID_SONY)
Pierre Ossman22606402008-03-23 19:33:23 +010088 chip->quirks |= SDHCI_QUIRK_NO_CARD_NO_RESET;
Maxim Levitskyccc92c22010-08-10 18:01:42 -070089 return 0;
90}
Pierre Ossman22606402008-03-23 19:33:23 +010091
Maxim Levitskyccc92c22010-08-10 18:01:42 -070092static int ricoh_mmc_probe_slot(struct sdhci_pci_slot *slot)
93{
94 slot->host->caps =
95 ((0x21 << SDHCI_TIMEOUT_CLK_SHIFT)
96 & SDHCI_TIMEOUT_CLK_MASK) |
97
98 ((0x21 << SDHCI_CLOCK_BASE_SHIFT)
99 & SDHCI_CLOCK_BASE_MASK) |
100
101 SDHCI_TIMEOUT_CLK_UNIT |
102 SDHCI_CAN_VDD_330 |
103 SDHCI_CAN_DO_SDMA;
104 return 0;
105}
106
107static int ricoh_mmc_resume(struct sdhci_pci_chip *chip)
108{
109 /* Apply a delay to allow controller to settle */
110 /* Otherwise it becomes confused if card state changed
111 during suspend */
112 msleep(500);
Pierre Ossman22606402008-03-23 19:33:23 +0100113 return 0;
114}
115
116static const struct sdhci_pci_fixes sdhci_ricoh = {
117 .probe = ricoh_probe,
Vasily Khoruzhick84938292010-03-05 13:43:46 -0800118 .quirks = SDHCI_QUIRK_32BIT_DMA_ADDR |
119 SDHCI_QUIRK_FORCE_DMA |
120 SDHCI_QUIRK_CLOCK_BEFORE_RESET,
Pierre Ossman22606402008-03-23 19:33:23 +0100121};
122
Maxim Levitskyccc92c22010-08-10 18:01:42 -0700123static const struct sdhci_pci_fixes sdhci_ricoh_mmc = {
124 .probe_slot = ricoh_mmc_probe_slot,
125 .resume = ricoh_mmc_resume,
126 .quirks = SDHCI_QUIRK_32BIT_DMA_ADDR |
127 SDHCI_QUIRK_CLOCK_BEFORE_RESET |
128 SDHCI_QUIRK_NO_CARD_NO_RESET |
129 SDHCI_QUIRK_MISSING_CAPS
130};
131
Pierre Ossman22606402008-03-23 19:33:23 +0100132static const struct sdhci_pci_fixes sdhci_ene_712 = {
133 .quirks = SDHCI_QUIRK_SINGLE_POWER_WRITE |
134 SDHCI_QUIRK_BROKEN_DMA,
135};
136
137static const struct sdhci_pci_fixes sdhci_ene_714 = {
138 .quirks = SDHCI_QUIRK_SINGLE_POWER_WRITE |
139 SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS |
140 SDHCI_QUIRK_BROKEN_DMA,
141};
142
143static const struct sdhci_pci_fixes sdhci_cafe = {
144 .quirks = SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER |
Andres Salomona0874892009-03-02 21:48:20 +0100145 SDHCI_QUIRK_NO_BUSY_IRQ |
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200146 SDHCI_QUIRK_BROKEN_TIMEOUT_VAL,
Pierre Ossman22606402008-03-23 19:33:23 +0100147};
148
Major Lee68077b02011-06-29 14:23:46 +0300149static int mrst_hc_probe_slot(struct sdhci_pci_slot *slot)
150{
151 slot->host->mmc->caps |= MMC_CAP_8_BIT_DATA;
152 return 0;
153}
154
Alan Coxf9ee3ea2010-10-04 15:25:11 +0100155/*
156 * ADMA operation is disabled for Moorestown platform due to
157 * hardware bugs.
158 */
Jacob Pan35ac6f02010-11-09 13:57:29 +0000159static int mrst_hc_probe(struct sdhci_pci_chip *chip)
Alan Coxf9ee3ea2010-10-04 15:25:11 +0100160{
161 /*
Jacob Pan35ac6f02010-11-09 13:57:29 +0000162 * slots number is fixed here for MRST as SDIO3/5 are never used and
163 * have hardware bugs.
Alan Coxf9ee3ea2010-10-04 15:25:11 +0100164 */
165 chip->num_slots = 1;
166 return 0;
167}
168
Adrian Hunter0f201652011-08-29 16:42:13 +0300169/* Medfield eMMC hardware reset GPIOs */
170static int mfd_emmc0_rst_gpio = -EINVAL;
171static int mfd_emmc1_rst_gpio = -EINVAL;
172
173static int mfd_emmc_gpio_parse(struct sfi_table_header *table)
174{
175 struct sfi_table_simple *sb = (struct sfi_table_simple *)table;
176 struct sfi_gpio_table_entry *entry;
177 int i, num;
178
179 num = SFI_GET_NUM_ENTRIES(sb, struct sfi_gpio_table_entry);
180 entry = (struct sfi_gpio_table_entry *)sb->pentry;
181
182 for (i = 0; i < num; i++, entry++) {
183 if (!strncmp(entry->pin_name, "emmc0_rst", SFI_NAME_LEN))
184 mfd_emmc0_rst_gpio = entry->pin_no;
185 else if (!strncmp(entry->pin_name, "emmc1_rst", SFI_NAME_LEN))
186 mfd_emmc1_rst_gpio = entry->pin_no;
187 }
188
189 return 0;
190}
191
Adrian Hunter0d013bc2011-06-29 14:23:47 +0300192static int mfd_emmc_probe_slot(struct sdhci_pci_slot *slot)
193{
Adrian Hunter0f201652011-08-29 16:42:13 +0300194 const char *name = NULL;
195 int gpio = -EINVAL;
196
197 sfi_table_parse(SFI_SIG_GPIO, NULL, NULL, mfd_emmc_gpio_parse);
198
199 switch (slot->chip->pdev->device) {
200 case PCI_DEVICE_ID_INTEL_MFD_EMMC0:
201 gpio = mfd_emmc0_rst_gpio;
202 name = "eMMC0_reset";
203 break;
204 case PCI_DEVICE_ID_INTEL_MFD_EMMC1:
205 gpio = mfd_emmc1_rst_gpio;
206 name = "eMMC1_reset";
207 break;
208 }
209
210 if (!gpio_request(gpio, name)) {
211 gpio_direction_output(gpio, 1);
212 slot->rst_n_gpio = gpio;
213 slot->host->mmc->caps |= MMC_CAP_HW_RESET;
214 }
215
Adrian Hunter0d013bc2011-06-29 14:23:47 +0300216 slot->host->mmc->caps |= MMC_CAP_8_BIT_DATA;
Adrian Hunter0f201652011-08-29 16:42:13 +0300217
Adrian Hunterf7c56ef2011-09-23 12:48:21 +0300218 slot->host->mmc->caps2 = MMC_CAP2_BOOTPART_NOACC;
219
Adrian Hunter0d013bc2011-06-29 14:23:47 +0300220 return 0;
221}
222
Adrian Hunter0f201652011-08-29 16:42:13 +0300223static void mfd_emmc_remove_slot(struct sdhci_pci_slot *slot, int dead)
224{
225 gpio_free(slot->rst_n_gpio);
226}
227
Alan Coxf9ee3ea2010-10-04 15:25:11 +0100228static const struct sdhci_pci_fixes sdhci_intel_mrst_hc0 = {
229 .quirks = SDHCI_QUIRK_BROKEN_ADMA | SDHCI_QUIRK_NO_HISPD_BIT,
Major Lee68077b02011-06-29 14:23:46 +0300230 .probe_slot = mrst_hc_probe_slot,
Alan Coxf9ee3ea2010-10-04 15:25:11 +0100231};
232
Jacob Pan35ac6f02010-11-09 13:57:29 +0000233static const struct sdhci_pci_fixes sdhci_intel_mrst_hc1_hc2 = {
Alan Coxf9ee3ea2010-10-04 15:25:11 +0100234 .quirks = SDHCI_QUIRK_BROKEN_ADMA | SDHCI_QUIRK_NO_HISPD_BIT,
Jacob Pan35ac6f02010-11-09 13:57:29 +0000235 .probe = mrst_hc_probe,
Alan Coxf9ee3ea2010-10-04 15:25:11 +0100236};
237
Xiaochen Shen29229052010-10-04 15:24:52 +0100238static const struct sdhci_pci_fixes sdhci_intel_mfd_sd = {
239 .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
240};
241
Adrian Hunter0d013bc2011-06-29 14:23:47 +0300242static const struct sdhci_pci_fixes sdhci_intel_mfd_sdio = {
Xiaochen Shen29229052010-10-04 15:24:52 +0100243 .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
244};
245
Adrian Hunter0d013bc2011-06-29 14:23:47 +0300246static const struct sdhci_pci_fixes sdhci_intel_mfd_emmc = {
247 .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
248 .probe_slot = mfd_emmc_probe_slot,
Adrian Hunter0f201652011-08-29 16:42:13 +0300249 .remove_slot = mfd_emmc_remove_slot,
Adrian Hunter0d013bc2011-06-29 14:23:47 +0300250};
251
Jennifer Li26daa1e2010-11-17 23:01:59 -0500252/* O2Micro extra registers */
253#define O2_SD_LOCK_WP 0xD3
254#define O2_SD_MULTI_VCC3V 0xEE
255#define O2_SD_CLKREQ 0xEC
256#define O2_SD_CAPS 0xE0
257#define O2_SD_ADMA1 0xE2
258#define O2_SD_ADMA2 0xE7
259#define O2_SD_INF_MOD 0xF1
260
261static int o2_probe(struct sdhci_pci_chip *chip)
262{
263 int ret;
264 u8 scratch;
265
266 switch (chip->pdev->device) {
267 case PCI_DEVICE_ID_O2_8220:
268 case PCI_DEVICE_ID_O2_8221:
269 case PCI_DEVICE_ID_O2_8320:
270 case PCI_DEVICE_ID_O2_8321:
271 /* This extra setup is required due to broken ADMA. */
272 ret = pci_read_config_byte(chip->pdev, O2_SD_LOCK_WP, &scratch);
273 if (ret)
274 return ret;
275 scratch &= 0x7f;
276 pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch);
277
278 /* Set Multi 3 to VCC3V# */
279 pci_write_config_byte(chip->pdev, O2_SD_MULTI_VCC3V, 0x08);
280
281 /* Disable CLK_REQ# support after media DET */
282 ret = pci_read_config_byte(chip->pdev, O2_SD_CLKREQ, &scratch);
283 if (ret)
284 return ret;
285 scratch |= 0x20;
286 pci_write_config_byte(chip->pdev, O2_SD_CLKREQ, scratch);
287
288 /* Choose capabilities, enable SDMA. We have to write 0x01
289 * to the capabilities register first to unlock it.
290 */
291 ret = pci_read_config_byte(chip->pdev, O2_SD_CAPS, &scratch);
292 if (ret)
293 return ret;
294 scratch |= 0x01;
295 pci_write_config_byte(chip->pdev, O2_SD_CAPS, scratch);
296 pci_write_config_byte(chip->pdev, O2_SD_CAPS, 0x73);
297
298 /* Disable ADMA1/2 */
299 pci_write_config_byte(chip->pdev, O2_SD_ADMA1, 0x39);
300 pci_write_config_byte(chip->pdev, O2_SD_ADMA2, 0x08);
301
302 /* Disable the infinite transfer mode */
303 ret = pci_read_config_byte(chip->pdev, O2_SD_INF_MOD, &scratch);
304 if (ret)
305 return ret;
306 scratch |= 0x08;
307 pci_write_config_byte(chip->pdev, O2_SD_INF_MOD, scratch);
308
309 /* Lock WP */
310 ret = pci_read_config_byte(chip->pdev, O2_SD_LOCK_WP, &scratch);
311 if (ret)
312 return ret;
313 scratch |= 0x80;
314 pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch);
315 }
316
317 return 0;
318}
319
Pierre Ossman45211e22008-03-24 13:09:09 +0100320static int jmicron_pmos(struct sdhci_pci_chip *chip, int on)
321{
322 u8 scratch;
323 int ret;
324
325 ret = pci_read_config_byte(chip->pdev, 0xAE, &scratch);
326 if (ret)
327 return ret;
328
329 /*
330 * Turn PMOS on [bit 0], set over current detection to 2.4 V
331 * [bit 1:2] and enable over current debouncing [bit 6].
332 */
333 if (on)
334 scratch |= 0x47;
335 else
336 scratch &= ~0x47;
337
338 ret = pci_write_config_byte(chip->pdev, 0xAE, scratch);
339 if (ret)
340 return ret;
341
342 return 0;
343}
344
345static int jmicron_probe(struct sdhci_pci_chip *chip)
346{
347 int ret;
Takashi Iwai8f230f42010-12-08 10:04:30 +0100348 u16 mmcdev = 0;
Pierre Ossman45211e22008-03-24 13:09:09 +0100349
Pierre Ossman93fc48c2008-06-28 18:21:41 +0200350 if (chip->pdev->revision == 0) {
351 chip->quirks |= SDHCI_QUIRK_32BIT_DMA_ADDR |
352 SDHCI_QUIRK_32BIT_DMA_SIZE |
Pierre Ossman2134a922008-06-28 18:28:51 +0200353 SDHCI_QUIRK_32BIT_ADMA_SIZE |
Pierre Ossman4a3cba32008-07-29 00:11:16 +0200354 SDHCI_QUIRK_RESET_AFTER_REQUEST |
Pierre Ossman86a6a872009-02-02 21:13:49 +0100355 SDHCI_QUIRK_BROKEN_SMALL_PIO;
Pierre Ossman93fc48c2008-06-28 18:21:41 +0200356 }
357
Pierre Ossman45211e22008-03-24 13:09:09 +0100358 /*
Pierre Ossman44894282008-04-04 19:36:59 +0200359 * JMicron chips can have two interfaces to the same hardware
360 * in order to work around limitations in Microsoft's driver.
361 * We need to make sure we only bind to one of them.
362 *
363 * This code assumes two things:
364 *
365 * 1. The PCI code adds subfunctions in order.
366 *
367 * 2. The MMC interface has a lower subfunction number
368 * than the SD interface.
369 */
Takashi Iwai8f230f42010-12-08 10:04:30 +0100370 if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_SD)
371 mmcdev = PCI_DEVICE_ID_JMICRON_JMB38X_MMC;
372 else if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_SD)
373 mmcdev = PCI_DEVICE_ID_JMICRON_JMB388_ESD;
374
375 if (mmcdev) {
Pierre Ossman44894282008-04-04 19:36:59 +0200376 struct pci_dev *sd_dev;
377
378 sd_dev = NULL;
379 while ((sd_dev = pci_get_device(PCI_VENDOR_ID_JMICRON,
Takashi Iwai8f230f42010-12-08 10:04:30 +0100380 mmcdev, sd_dev)) != NULL) {
Pierre Ossman44894282008-04-04 19:36:59 +0200381 if ((PCI_SLOT(chip->pdev->devfn) ==
382 PCI_SLOT(sd_dev->devfn)) &&
383 (chip->pdev->bus == sd_dev->bus))
384 break;
385 }
386
387 if (sd_dev) {
388 pci_dev_put(sd_dev);
389 dev_info(&chip->pdev->dev, "Refusing to bind to "
390 "secondary interface.\n");
391 return -ENODEV;
392 }
393 }
394
395 /*
Pierre Ossman45211e22008-03-24 13:09:09 +0100396 * JMicron chips need a bit of a nudge to enable the power
397 * output pins.
398 */
399 ret = jmicron_pmos(chip, 1);
400 if (ret) {
401 dev_err(&chip->pdev->dev, "Failure enabling card power\n");
402 return ret;
403 }
404
Takashi Iwai82b0e232011-04-21 20:26:38 +0200405 /* quirk for unsable RO-detection on JM388 chips */
406 if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_SD ||
407 chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD)
408 chip->quirks |= SDHCI_QUIRK_UNSTABLE_RO_DETECT;
409
Pierre Ossman45211e22008-03-24 13:09:09 +0100410 return 0;
411}
412
Pierre Ossman44894282008-04-04 19:36:59 +0200413static void jmicron_enable_mmc(struct sdhci_host *host, int on)
414{
415 u8 scratch;
416
417 scratch = readb(host->ioaddr + 0xC0);
418
419 if (on)
420 scratch |= 0x01;
421 else
422 scratch &= ~0x01;
423
424 writeb(scratch, host->ioaddr + 0xC0);
425}
426
427static int jmicron_probe_slot(struct sdhci_pci_slot *slot)
428{
Pierre Ossman2134a922008-06-28 18:28:51 +0200429 if (slot->chip->pdev->revision == 0) {
430 u16 version;
431
432 version = readl(slot->host->ioaddr + SDHCI_HOST_VERSION);
433 version = (version & SDHCI_VENDOR_VER_MASK) >>
434 SDHCI_VENDOR_VER_SHIFT;
435
436 /*
437 * Older versions of the chip have lots of nasty glitches
438 * in the ADMA engine. It's best just to avoid it
439 * completely.
440 */
441 if (version < 0xAC)
442 slot->host->quirks |= SDHCI_QUIRK_BROKEN_ADMA;
443 }
444
Takashi Iwai8f230f42010-12-08 10:04:30 +0100445 /* JM388 MMC doesn't support 1.8V while SD supports it */
446 if (slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD) {
447 slot->host->ocr_avail_sd = MMC_VDD_32_33 | MMC_VDD_33_34 |
448 MMC_VDD_29_30 | MMC_VDD_30_31 |
449 MMC_VDD_165_195; /* allow 1.8V */
450 slot->host->ocr_avail_mmc = MMC_VDD_32_33 | MMC_VDD_33_34 |
451 MMC_VDD_29_30 | MMC_VDD_30_31; /* no 1.8V for MMC */
452 }
453
Pierre Ossman44894282008-04-04 19:36:59 +0200454 /*
455 * The secondary interface requires a bit set to get the
456 * interrupts.
457 */
Takashi Iwai8f230f42010-12-08 10:04:30 +0100458 if (slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC ||
459 slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD)
Pierre Ossman44894282008-04-04 19:36:59 +0200460 jmicron_enable_mmc(slot->host, 1);
461
Takashi Iwaid75c1082010-12-16 17:54:14 +0100462 slot->host->mmc->caps |= MMC_CAP_BUS_WIDTH_TEST;
463
Pierre Ossman44894282008-04-04 19:36:59 +0200464 return 0;
465}
466
Pierre Ossman1e728592008-04-16 19:13:13 +0200467static void jmicron_remove_slot(struct sdhci_pci_slot *slot, int dead)
Pierre Ossman44894282008-04-04 19:36:59 +0200468{
Pierre Ossman1e728592008-04-16 19:13:13 +0200469 if (dead)
470 return;
471
Takashi Iwai8f230f42010-12-08 10:04:30 +0100472 if (slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC ||
473 slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD)
Pierre Ossman44894282008-04-04 19:36:59 +0200474 jmicron_enable_mmc(slot->host, 0);
475}
476
477static int jmicron_suspend(struct sdhci_pci_chip *chip, pm_message_t state)
478{
479 int i;
480
Takashi Iwai8f230f42010-12-08 10:04:30 +0100481 if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC ||
482 chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD) {
Ameya Palandeb177bc92011-04-05 21:13:13 +0300483 for (i = 0; i < chip->num_slots; i++)
Pierre Ossman44894282008-04-04 19:36:59 +0200484 jmicron_enable_mmc(chip->slots[i]->host, 0);
485 }
486
487 return 0;
488}
489
Pierre Ossman45211e22008-03-24 13:09:09 +0100490static int jmicron_resume(struct sdhci_pci_chip *chip)
491{
Pierre Ossman44894282008-04-04 19:36:59 +0200492 int ret, i;
493
Takashi Iwai8f230f42010-12-08 10:04:30 +0100494 if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC ||
495 chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD) {
Ameya Palandeb177bc92011-04-05 21:13:13 +0300496 for (i = 0; i < chip->num_slots; i++)
Pierre Ossman44894282008-04-04 19:36:59 +0200497 jmicron_enable_mmc(chip->slots[i]->host, 1);
498 }
Pierre Ossman45211e22008-03-24 13:09:09 +0100499
500 ret = jmicron_pmos(chip, 1);
501 if (ret) {
502 dev_err(&chip->pdev->dev, "Failure enabling card power\n");
503 return ret;
504 }
505
506 return 0;
507}
508
Jennifer Li26daa1e2010-11-17 23:01:59 -0500509static const struct sdhci_pci_fixes sdhci_o2 = {
510 .probe = o2_probe,
511};
512
Pierre Ossman22606402008-03-23 19:33:23 +0100513static const struct sdhci_pci_fixes sdhci_jmicron = {
Pierre Ossman45211e22008-03-24 13:09:09 +0100514 .probe = jmicron_probe,
515
Pierre Ossman44894282008-04-04 19:36:59 +0200516 .probe_slot = jmicron_probe_slot,
517 .remove_slot = jmicron_remove_slot,
518
519 .suspend = jmicron_suspend,
Pierre Ossman45211e22008-03-24 13:09:09 +0100520 .resume = jmicron_resume,
Pierre Ossman22606402008-03-23 19:33:23 +0100521};
522
Nicolas Pitrea7a61862009-12-14 18:01:26 -0800523/* SysKonnect CardBus2SDIO extra registers */
524#define SYSKT_CTRL 0x200
525#define SYSKT_RDFIFO_STAT 0x204
526#define SYSKT_WRFIFO_STAT 0x208
527#define SYSKT_POWER_DATA 0x20c
528#define SYSKT_POWER_330 0xef
529#define SYSKT_POWER_300 0xf8
530#define SYSKT_POWER_184 0xcc
531#define SYSKT_POWER_CMD 0x20d
532#define SYSKT_POWER_START (1 << 7)
533#define SYSKT_POWER_STATUS 0x20e
534#define SYSKT_POWER_STATUS_OK (1 << 0)
535#define SYSKT_BOARD_REV 0x210
536#define SYSKT_CHIP_REV 0x211
537#define SYSKT_CONF_DATA 0x212
538#define SYSKT_CONF_DATA_1V8 (1 << 2)
539#define SYSKT_CONF_DATA_2V5 (1 << 1)
540#define SYSKT_CONF_DATA_3V3 (1 << 0)
541
542static int syskt_probe(struct sdhci_pci_chip *chip)
543{
544 if ((chip->pdev->class & 0x0000FF) == PCI_SDHCI_IFVENDOR) {
545 chip->pdev->class &= ~0x0000FF;
546 chip->pdev->class |= PCI_SDHCI_IFDMA;
547 }
548 return 0;
549}
550
551static int syskt_probe_slot(struct sdhci_pci_slot *slot)
552{
553 int tm, ps;
554
555 u8 board_rev = readb(slot->host->ioaddr + SYSKT_BOARD_REV);
556 u8 chip_rev = readb(slot->host->ioaddr + SYSKT_CHIP_REV);
557 dev_info(&slot->chip->pdev->dev, "SysKonnect CardBus2SDIO, "
558 "board rev %d.%d, chip rev %d.%d\n",
559 board_rev >> 4, board_rev & 0xf,
560 chip_rev >> 4, chip_rev & 0xf);
561 if (chip_rev >= 0x20)
562 slot->host->quirks |= SDHCI_QUIRK_FORCE_DMA;
563
564 writeb(SYSKT_POWER_330, slot->host->ioaddr + SYSKT_POWER_DATA);
565 writeb(SYSKT_POWER_START, slot->host->ioaddr + SYSKT_POWER_CMD);
566 udelay(50);
567 tm = 10; /* Wait max 1 ms */
568 do {
569 ps = readw(slot->host->ioaddr + SYSKT_POWER_STATUS);
570 if (ps & SYSKT_POWER_STATUS_OK)
571 break;
572 udelay(100);
573 } while (--tm);
574 if (!tm) {
575 dev_err(&slot->chip->pdev->dev,
576 "power regulator never stabilized");
577 writeb(0, slot->host->ioaddr + SYSKT_POWER_CMD);
578 return -ENODEV;
579 }
580
581 return 0;
582}
583
584static const struct sdhci_pci_fixes sdhci_syskt = {
585 .quirks = SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER,
586 .probe = syskt_probe,
587 .probe_slot = syskt_probe_slot,
588};
589
Harald Welte557b0692009-06-18 16:53:38 +0200590static int via_probe(struct sdhci_pci_chip *chip)
591{
592 if (chip->pdev->revision == 0x10)
593 chip->quirks |= SDHCI_QUIRK_DELAY_AFTER_POWER;
594
595 return 0;
596}
597
598static const struct sdhci_pci_fixes sdhci_via = {
599 .probe = via_probe,
600};
601
Pierre Ossman22606402008-03-23 19:33:23 +0100602static const struct pci_device_id pci_ids[] __devinitdata = {
603 {
604 .vendor = PCI_VENDOR_ID_RICOH,
605 .device = PCI_DEVICE_ID_RICOH_R5C822,
606 .subvendor = PCI_ANY_ID,
607 .subdevice = PCI_ANY_ID,
608 .driver_data = (kernel_ulong_t)&sdhci_ricoh,
609 },
610
611 {
Maxim Levitskyccc92c22010-08-10 18:01:42 -0700612 .vendor = PCI_VENDOR_ID_RICOH,
613 .device = 0x843,
614 .subvendor = PCI_ANY_ID,
615 .subdevice = PCI_ANY_ID,
616 .driver_data = (kernel_ulong_t)&sdhci_ricoh_mmc,
617 },
618
619 {
Pablo Castillo568133e2010-08-10 18:02:01 -0700620 .vendor = PCI_VENDOR_ID_RICOH,
621 .device = 0xe822,
622 .subvendor = PCI_ANY_ID,
623 .subdevice = PCI_ANY_ID,
624 .driver_data = (kernel_ulong_t)&sdhci_ricoh_mmc,
625 },
626
627 {
Manoj Iyer5fd11c02011-02-11 16:25:31 -0600628 .vendor = PCI_VENDOR_ID_RICOH,
629 .device = 0xe823,
630 .subvendor = PCI_ANY_ID,
631 .subdevice = PCI_ANY_ID,
632 .driver_data = (kernel_ulong_t)&sdhci_ricoh_mmc,
633 },
634
635 {
Pierre Ossman22606402008-03-23 19:33:23 +0100636 .vendor = PCI_VENDOR_ID_ENE,
637 .device = PCI_DEVICE_ID_ENE_CB712_SD,
638 .subvendor = PCI_ANY_ID,
639 .subdevice = PCI_ANY_ID,
640 .driver_data = (kernel_ulong_t)&sdhci_ene_712,
641 },
642
643 {
644 .vendor = PCI_VENDOR_ID_ENE,
645 .device = PCI_DEVICE_ID_ENE_CB712_SD_2,
646 .subvendor = PCI_ANY_ID,
647 .subdevice = PCI_ANY_ID,
648 .driver_data = (kernel_ulong_t)&sdhci_ene_712,
649 },
650
651 {
652 .vendor = PCI_VENDOR_ID_ENE,
653 .device = PCI_DEVICE_ID_ENE_CB714_SD,
654 .subvendor = PCI_ANY_ID,
655 .subdevice = PCI_ANY_ID,
656 .driver_data = (kernel_ulong_t)&sdhci_ene_714,
657 },
658
659 {
660 .vendor = PCI_VENDOR_ID_ENE,
661 .device = PCI_DEVICE_ID_ENE_CB714_SD_2,
662 .subvendor = PCI_ANY_ID,
663 .subdevice = PCI_ANY_ID,
664 .driver_data = (kernel_ulong_t)&sdhci_ene_714,
665 },
666
667 {
668 .vendor = PCI_VENDOR_ID_MARVELL,
David Woodhouse8c5eb882008-09-03 09:45:57 +0100669 .device = PCI_DEVICE_ID_MARVELL_88ALP01_SD,
Pierre Ossman22606402008-03-23 19:33:23 +0100670 .subvendor = PCI_ANY_ID,
671 .subdevice = PCI_ANY_ID,
672 .driver_data = (kernel_ulong_t)&sdhci_cafe,
673 },
674
675 {
676 .vendor = PCI_VENDOR_ID_JMICRON,
677 .device = PCI_DEVICE_ID_JMICRON_JMB38X_SD,
678 .subvendor = PCI_ANY_ID,
679 .subdevice = PCI_ANY_ID,
680 .driver_data = (kernel_ulong_t)&sdhci_jmicron,
681 },
682
Pierre Ossman44894282008-04-04 19:36:59 +0200683 {
684 .vendor = PCI_VENDOR_ID_JMICRON,
685 .device = PCI_DEVICE_ID_JMICRON_JMB38X_MMC,
686 .subvendor = PCI_ANY_ID,
687 .subdevice = PCI_ANY_ID,
688 .driver_data = (kernel_ulong_t)&sdhci_jmicron,
689 },
690
Harald Welte557b0692009-06-18 16:53:38 +0200691 {
Takashi Iwai8f230f42010-12-08 10:04:30 +0100692 .vendor = PCI_VENDOR_ID_JMICRON,
693 .device = PCI_DEVICE_ID_JMICRON_JMB388_SD,
694 .subvendor = PCI_ANY_ID,
695 .subdevice = PCI_ANY_ID,
696 .driver_data = (kernel_ulong_t)&sdhci_jmicron,
697 },
698
699 {
700 .vendor = PCI_VENDOR_ID_JMICRON,
701 .device = PCI_DEVICE_ID_JMICRON_JMB388_ESD,
702 .subvendor = PCI_ANY_ID,
703 .subdevice = PCI_ANY_ID,
704 .driver_data = (kernel_ulong_t)&sdhci_jmicron,
705 },
706
707 {
Nicolas Pitrea7a61862009-12-14 18:01:26 -0800708 .vendor = PCI_VENDOR_ID_SYSKONNECT,
709 .device = 0x8000,
710 .subvendor = PCI_ANY_ID,
711 .subdevice = PCI_ANY_ID,
712 .driver_data = (kernel_ulong_t)&sdhci_syskt,
713 },
714
715 {
Harald Welte557b0692009-06-18 16:53:38 +0200716 .vendor = PCI_VENDOR_ID_VIA,
717 .device = 0x95d0,
718 .subvendor = PCI_ANY_ID,
719 .subdevice = PCI_ANY_ID,
720 .driver_data = (kernel_ulong_t)&sdhci_via,
721 },
722
Xiaochen Shen29229052010-10-04 15:24:52 +0100723 {
724 .vendor = PCI_VENDOR_ID_INTEL,
Alan Coxf9ee3ea2010-10-04 15:25:11 +0100725 .device = PCI_DEVICE_ID_INTEL_MRST_SD0,
726 .subvendor = PCI_ANY_ID,
727 .subdevice = PCI_ANY_ID,
728 .driver_data = (kernel_ulong_t)&sdhci_intel_mrst_hc0,
729 },
730
731 {
732 .vendor = PCI_VENDOR_ID_INTEL,
733 .device = PCI_DEVICE_ID_INTEL_MRST_SD1,
734 .subvendor = PCI_ANY_ID,
735 .subdevice = PCI_ANY_ID,
Jacob Pan35ac6f02010-11-09 13:57:29 +0000736 .driver_data = (kernel_ulong_t)&sdhci_intel_mrst_hc1_hc2,
737 },
738
739 {
740 .vendor = PCI_VENDOR_ID_INTEL,
741 .device = PCI_DEVICE_ID_INTEL_MRST_SD2,
742 .subvendor = PCI_ANY_ID,
743 .subdevice = PCI_ANY_ID,
744 .driver_data = (kernel_ulong_t)&sdhci_intel_mrst_hc1_hc2,
Alan Coxf9ee3ea2010-10-04 15:25:11 +0100745 },
746
747 {
748 .vendor = PCI_VENDOR_ID_INTEL,
Xiaochen Shen29229052010-10-04 15:24:52 +0100749 .device = PCI_DEVICE_ID_INTEL_MFD_SD,
750 .subvendor = PCI_ANY_ID,
751 .subdevice = PCI_ANY_ID,
752 .driver_data = (kernel_ulong_t)&sdhci_intel_mfd_sd,
753 },
754
755 {
756 .vendor = PCI_VENDOR_ID_INTEL,
757 .device = PCI_DEVICE_ID_INTEL_MFD_SDIO1,
758 .subvendor = PCI_ANY_ID,
759 .subdevice = PCI_ANY_ID,
Adrian Hunter0d013bc2011-06-29 14:23:47 +0300760 .driver_data = (kernel_ulong_t)&sdhci_intel_mfd_sdio,
Xiaochen Shen29229052010-10-04 15:24:52 +0100761 },
762
763 {
764 .vendor = PCI_VENDOR_ID_INTEL,
765 .device = PCI_DEVICE_ID_INTEL_MFD_SDIO2,
766 .subvendor = PCI_ANY_ID,
767 .subdevice = PCI_ANY_ID,
Adrian Hunter0d013bc2011-06-29 14:23:47 +0300768 .driver_data = (kernel_ulong_t)&sdhci_intel_mfd_sdio,
Xiaochen Shen29229052010-10-04 15:24:52 +0100769 },
770
771 {
772 .vendor = PCI_VENDOR_ID_INTEL,
773 .device = PCI_DEVICE_ID_INTEL_MFD_EMMC0,
774 .subvendor = PCI_ANY_ID,
775 .subdevice = PCI_ANY_ID,
Adrian Hunter0d013bc2011-06-29 14:23:47 +0300776 .driver_data = (kernel_ulong_t)&sdhci_intel_mfd_emmc,
Xiaochen Shen29229052010-10-04 15:24:52 +0100777 },
778
779 {
780 .vendor = PCI_VENDOR_ID_INTEL,
781 .device = PCI_DEVICE_ID_INTEL_MFD_EMMC1,
782 .subvendor = PCI_ANY_ID,
783 .subdevice = PCI_ANY_ID,
Adrian Hunter0d013bc2011-06-29 14:23:47 +0300784 .driver_data = (kernel_ulong_t)&sdhci_intel_mfd_emmc,
Xiaochen Shen29229052010-10-04 15:24:52 +0100785 },
786
Jennifer Li26daa1e2010-11-17 23:01:59 -0500787 {
788 .vendor = PCI_VENDOR_ID_O2,
789 .device = PCI_DEVICE_ID_O2_8120,
790 .subvendor = PCI_ANY_ID,
791 .subdevice = PCI_ANY_ID,
792 .driver_data = (kernel_ulong_t)&sdhci_o2,
793 },
794
795 {
796 .vendor = PCI_VENDOR_ID_O2,
797 .device = PCI_DEVICE_ID_O2_8220,
798 .subvendor = PCI_ANY_ID,
799 .subdevice = PCI_ANY_ID,
800 .driver_data = (kernel_ulong_t)&sdhci_o2,
801 },
802
803 {
804 .vendor = PCI_VENDOR_ID_O2,
805 .device = PCI_DEVICE_ID_O2_8221,
806 .subvendor = PCI_ANY_ID,
807 .subdevice = PCI_ANY_ID,
808 .driver_data = (kernel_ulong_t)&sdhci_o2,
809 },
810
811 {
812 .vendor = PCI_VENDOR_ID_O2,
813 .device = PCI_DEVICE_ID_O2_8320,
814 .subvendor = PCI_ANY_ID,
815 .subdevice = PCI_ANY_ID,
816 .driver_data = (kernel_ulong_t)&sdhci_o2,
817 },
818
819 {
820 .vendor = PCI_VENDOR_ID_O2,
821 .device = PCI_DEVICE_ID_O2_8321,
822 .subvendor = PCI_ANY_ID,
823 .subdevice = PCI_ANY_ID,
824 .driver_data = (kernel_ulong_t)&sdhci_o2,
825 },
826
Pierre Ossman22606402008-03-23 19:33:23 +0100827 { /* Generic SD host controller */
828 PCI_DEVICE_CLASS((PCI_CLASS_SYSTEM_SDHCI << 8), 0xFFFF00)
829 },
830
831 { /* end: all zeroes */ },
832};
833
834MODULE_DEVICE_TABLE(pci, pci_ids);
835
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100836/*****************************************************************************\
837 * *
838 * SDHCI core callbacks *
839 * *
840\*****************************************************************************/
841
842static int sdhci_pci_enable_dma(struct sdhci_host *host)
843{
844 struct sdhci_pci_slot *slot;
845 struct pci_dev *pdev;
846 int ret;
847
848 slot = sdhci_priv(host);
849 pdev = slot->chip->pdev;
850
851 if (((pdev->class & 0xFFFF00) == (PCI_CLASS_SYSTEM_SDHCI << 8)) &&
852 ((pdev->class & 0x0000FF) != PCI_SDHCI_IFDMA) &&
Richard Röjforsa13abc72009-09-22 16:45:30 -0700853 (host->flags & SDHCI_USE_SDMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100854 dev_warn(&pdev->dev, "Will use DMA mode even though HW "
855 "doesn't fully claim to support it.\n");
856 }
857
Yang Hongyang284901a2009-04-06 19:01:15 -0700858 ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100859 if (ret)
860 return ret;
861
862 pci_set_master(pdev);
863
864 return 0;
865}
866
Major Lee68077b02011-06-29 14:23:46 +0300867static int sdhci_pci_8bit_width(struct sdhci_host *host, int width)
868{
869 u8 ctrl;
870
871 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
872
873 switch (width) {
874 case MMC_BUS_WIDTH_8:
875 ctrl |= SDHCI_CTRL_8BITBUS;
876 ctrl &= ~SDHCI_CTRL_4BITBUS;
877 break;
878 case MMC_BUS_WIDTH_4:
879 ctrl |= SDHCI_CTRL_4BITBUS;
880 ctrl &= ~SDHCI_CTRL_8BITBUS;
881 break;
882 default:
883 ctrl &= ~(SDHCI_CTRL_8BITBUS | SDHCI_CTRL_4BITBUS);
884 break;
885 }
886
887 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
888
889 return 0;
890}
891
Adrian Hunter0f201652011-08-29 16:42:13 +0300892static void sdhci_pci_hw_reset(struct sdhci_host *host)
893{
894 struct sdhci_pci_slot *slot = sdhci_priv(host);
895 int rst_n_gpio = slot->rst_n_gpio;
896
897 if (!gpio_is_valid(rst_n_gpio))
898 return;
899 gpio_set_value_cansleep(rst_n_gpio, 0);
900 /* For eMMC, minimum is 1us but give it 10us for good measure */
901 udelay(10);
902 gpio_set_value_cansleep(rst_n_gpio, 1);
903 /* For eMMC, minimum is 200us but give it 300us for good measure */
904 usleep_range(300, 1000);
905}
906
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100907static struct sdhci_ops sdhci_pci_ops = {
908 .enable_dma = sdhci_pci_enable_dma,
Major Lee68077b02011-06-29 14:23:46 +0300909 .platform_8bit_width = sdhci_pci_8bit_width,
Adrian Hunter0f201652011-08-29 16:42:13 +0300910 .hw_reset = sdhci_pci_hw_reset,
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100911};
912
913/*****************************************************************************\
914 * *
915 * Suspend/resume *
916 * *
917\*****************************************************************************/
918
919#ifdef CONFIG_PM
920
Ameya Palandeb177bc92011-04-05 21:13:13 +0300921static int sdhci_pci_suspend(struct pci_dev *pdev, pm_message_t state)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100922{
923 struct sdhci_pci_chip *chip;
924 struct sdhci_pci_slot *slot;
Daniel Drake5f619702010-11-04 22:20:39 +0000925 mmc_pm_flag_t slot_pm_flags;
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800926 mmc_pm_flag_t pm_flags = 0;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100927 int i, ret;
928
929 chip = pci_get_drvdata(pdev);
930 if (!chip)
931 return 0;
932
Ameya Palandeb177bc92011-04-05 21:13:13 +0300933 for (i = 0; i < chip->num_slots; i++) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100934 slot = chip->slots[i];
935 if (!slot)
936 continue;
937
938 ret = sdhci_suspend_host(slot->host, state);
939
940 if (ret) {
Ameya Palandeb177bc92011-04-05 21:13:13 +0300941 for (i--; i >= 0; i--)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100942 sdhci_resume_host(chip->slots[i]->host);
943 return ret;
944 }
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800945
Daniel Drake5f619702010-11-04 22:20:39 +0000946 slot_pm_flags = slot->host->mmc->pm_flags;
947 if (slot_pm_flags & MMC_PM_WAKE_SDIO_IRQ)
948 sdhci_enable_irq_wakeups(slot->host);
949
950 pm_flags |= slot_pm_flags;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100951 }
952
Pierre Ossman44894282008-04-04 19:36:59 +0200953 if (chip->fixes && chip->fixes->suspend) {
954 ret = chip->fixes->suspend(chip, state);
955 if (ret) {
Ameya Palandeb177bc92011-04-05 21:13:13 +0300956 for (i = chip->num_slots - 1; i >= 0; i--)
Pierre Ossman44894282008-04-04 19:36:59 +0200957 sdhci_resume_host(chip->slots[i]->host);
958 return ret;
959 }
960 }
961
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100962 pci_save_state(pdev);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800963 if (pm_flags & MMC_PM_KEEP_POWER) {
Daniel Drake5f619702010-11-04 22:20:39 +0000964 if (pm_flags & MMC_PM_WAKE_SDIO_IRQ) {
965 pci_pme_active(pdev, true);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800966 pci_enable_wake(pdev, PCI_D3hot, 1);
Daniel Drake5f619702010-11-04 22:20:39 +0000967 }
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800968 pci_set_power_state(pdev, PCI_D3hot);
969 } else {
970 pci_enable_wake(pdev, pci_choose_state(pdev, state), 0);
971 pci_disable_device(pdev);
972 pci_set_power_state(pdev, pci_choose_state(pdev, state));
973 }
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100974
975 return 0;
976}
977
Ameya Palandeb177bc92011-04-05 21:13:13 +0300978static int sdhci_pci_resume(struct pci_dev *pdev)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100979{
980 struct sdhci_pci_chip *chip;
981 struct sdhci_pci_slot *slot;
982 int i, ret;
983
984 chip = pci_get_drvdata(pdev);
985 if (!chip)
986 return 0;
987
988 pci_set_power_state(pdev, PCI_D0);
989 pci_restore_state(pdev);
990 ret = pci_enable_device(pdev);
991 if (ret)
992 return ret;
993
Pierre Ossman45211e22008-03-24 13:09:09 +0100994 if (chip->fixes && chip->fixes->resume) {
995 ret = chip->fixes->resume(chip);
996 if (ret)
997 return ret;
998 }
999
Ameya Palandeb177bc92011-04-05 21:13:13 +03001000 for (i = 0; i < chip->num_slots; i++) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001001 slot = chip->slots[i];
1002 if (!slot)
1003 continue;
1004
1005 ret = sdhci_resume_host(slot->host);
1006 if (ret)
1007 return ret;
1008 }
1009
1010 return 0;
1011}
1012
1013#else /* CONFIG_PM */
1014
1015#define sdhci_pci_suspend NULL
1016#define sdhci_pci_resume NULL
1017
1018#endif /* CONFIG_PM */
1019
1020/*****************************************************************************\
1021 * *
1022 * Device probing/removal *
1023 * *
1024\*****************************************************************************/
1025
1026static struct sdhci_pci_slot * __devinit sdhci_pci_probe_slot(
1027 struct pci_dev *pdev, struct sdhci_pci_chip *chip, int bar)
1028{
1029 struct sdhci_pci_slot *slot;
1030 struct sdhci_host *host;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001031 int ret;
1032
1033 if (!(pci_resource_flags(pdev, bar) & IORESOURCE_MEM)) {
1034 dev_err(&pdev->dev, "BAR %d is not iomem. Aborting.\n", bar);
1035 return ERR_PTR(-ENODEV);
1036 }
1037
1038 if (pci_resource_len(pdev, bar) != 0x100) {
1039 dev_err(&pdev->dev, "Invalid iomem size. You may "
1040 "experience problems.\n");
1041 }
1042
1043 if ((pdev->class & 0x0000FF) == PCI_SDHCI_IFVENDOR) {
1044 dev_err(&pdev->dev, "Vendor specific interface. Aborting.\n");
1045 return ERR_PTR(-ENODEV);
1046 }
1047
1048 if ((pdev->class & 0x0000FF) > PCI_SDHCI_IFVENDOR) {
1049 dev_err(&pdev->dev, "Unknown interface. Aborting.\n");
1050 return ERR_PTR(-ENODEV);
1051 }
1052
1053 host = sdhci_alloc_host(&pdev->dev, sizeof(struct sdhci_pci_slot));
1054 if (IS_ERR(host)) {
Dan Carpenterc60a32c2009-04-10 23:31:10 +02001055 dev_err(&pdev->dev, "cannot allocate host\n");
Julia Lawalldc0fd7b2010-05-26 14:42:11 -07001056 return ERR_CAST(host);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001057 }
1058
1059 slot = sdhci_priv(host);
1060
1061 slot->chip = chip;
1062 slot->host = host;
1063 slot->pci_bar = bar;
Adrian Hunter0f201652011-08-29 16:42:13 +03001064 slot->rst_n_gpio = -EINVAL;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001065
1066 host->hw_name = "PCI";
1067 host->ops = &sdhci_pci_ops;
1068 host->quirks = chip->quirks;
1069
1070 host->irq = pdev->irq;
1071
1072 ret = pci_request_region(pdev, bar, mmc_hostname(host->mmc));
1073 if (ret) {
1074 dev_err(&pdev->dev, "cannot request region\n");
Dan Carpenterc60a32c2009-04-10 23:31:10 +02001075 goto free;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001076 }
1077
Arjan van de Ven092f82e2008-09-28 16:15:56 -07001078 host->ioaddr = pci_ioremap_bar(pdev, bar);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001079 if (!host->ioaddr) {
1080 dev_err(&pdev->dev, "failed to remap registers\n");
Chris Ball9fdcdbb2011-03-29 00:46:12 -04001081 ret = -ENOMEM;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001082 goto release;
1083 }
1084
Pierre Ossman44894282008-04-04 19:36:59 +02001085 if (chip->fixes && chip->fixes->probe_slot) {
1086 ret = chip->fixes->probe_slot(slot);
1087 if (ret)
1088 goto unmap;
1089 }
1090
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08001091 host->mmc->pm_caps = MMC_PM_KEEP_POWER | MMC_PM_WAKE_SDIO_IRQ;
1092
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001093 ret = sdhci_add_host(host);
1094 if (ret)
Pierre Ossman44894282008-04-04 19:36:59 +02001095 goto remove;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001096
1097 return slot;
1098
Pierre Ossman44894282008-04-04 19:36:59 +02001099remove:
1100 if (chip->fixes && chip->fixes->remove_slot)
Pierre Ossman1e728592008-04-16 19:13:13 +02001101 chip->fixes->remove_slot(slot, 0);
Pierre Ossman44894282008-04-04 19:36:59 +02001102
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001103unmap:
1104 iounmap(host->ioaddr);
1105
1106release:
1107 pci_release_region(pdev, bar);
Dan Carpenterc60a32c2009-04-10 23:31:10 +02001108
1109free:
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001110 sdhci_free_host(host);
1111
1112 return ERR_PTR(ret);
1113}
1114
1115static void sdhci_pci_remove_slot(struct sdhci_pci_slot *slot)
1116{
Pierre Ossman1e728592008-04-16 19:13:13 +02001117 int dead;
1118 u32 scratch;
1119
1120 dead = 0;
1121 scratch = readl(slot->host->ioaddr + SDHCI_INT_STATUS);
1122 if (scratch == (u32)-1)
1123 dead = 1;
1124
1125 sdhci_remove_host(slot->host, dead);
Pierre Ossman44894282008-04-04 19:36:59 +02001126
1127 if (slot->chip->fixes && slot->chip->fixes->remove_slot)
Pierre Ossman1e728592008-04-16 19:13:13 +02001128 slot->chip->fixes->remove_slot(slot, dead);
Pierre Ossman44894282008-04-04 19:36:59 +02001129
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001130 pci_release_region(slot->chip->pdev, slot->pci_bar);
Pierre Ossman44894282008-04-04 19:36:59 +02001131
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001132 sdhci_free_host(slot->host);
1133}
1134
1135static int __devinit sdhci_pci_probe(struct pci_dev *pdev,
1136 const struct pci_device_id *ent)
1137{
1138 struct sdhci_pci_chip *chip;
1139 struct sdhci_pci_slot *slot;
1140
Sergei Shtylyovcf5e23e2011-03-17 16:46:17 -04001141 u8 slots, first_bar;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001142 int ret, i;
1143
1144 BUG_ON(pdev == NULL);
1145 BUG_ON(ent == NULL);
1146
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001147 dev_info(&pdev->dev, "SDHCI controller found [%04x:%04x] (rev %x)\n",
Sergei Shtylyovcf5e23e2011-03-17 16:46:17 -04001148 (int)pdev->vendor, (int)pdev->device, (int)pdev->revision);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001149
1150 ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &slots);
1151 if (ret)
1152 return ret;
1153
1154 slots = PCI_SLOT_INFO_SLOTS(slots) + 1;
1155 dev_dbg(&pdev->dev, "found %d slot(s)\n", slots);
1156 if (slots == 0)
1157 return -ENODEV;
1158
1159 BUG_ON(slots > MAX_SLOTS);
1160
1161 ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &first_bar);
1162 if (ret)
1163 return ret;
1164
1165 first_bar &= PCI_SLOT_INFO_FIRST_BAR_MASK;
1166
1167 if (first_bar > 5) {
1168 dev_err(&pdev->dev, "Invalid first BAR. Aborting.\n");
1169 return -ENODEV;
1170 }
1171
1172 ret = pci_enable_device(pdev);
1173 if (ret)
1174 return ret;
1175
1176 chip = kzalloc(sizeof(struct sdhci_pci_chip), GFP_KERNEL);
1177 if (!chip) {
1178 ret = -ENOMEM;
1179 goto err;
1180 }
1181
1182 chip->pdev = pdev;
Ameya Palandeb177bc92011-04-05 21:13:13 +03001183 chip->fixes = (const struct sdhci_pci_fixes *)ent->driver_data;
Pierre Ossman22606402008-03-23 19:33:23 +01001184 if (chip->fixes)
1185 chip->quirks = chip->fixes->quirks;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001186 chip->num_slots = slots;
1187
1188 pci_set_drvdata(pdev, chip);
1189
Pierre Ossman22606402008-03-23 19:33:23 +01001190 if (chip->fixes && chip->fixes->probe) {
1191 ret = chip->fixes->probe(chip);
1192 if (ret)
1193 goto free;
1194 }
1195
Alan Cox225d85f2010-10-04 15:24:21 +01001196 slots = chip->num_slots; /* Quirk may have changed this */
1197
Ameya Palandeb177bc92011-04-05 21:13:13 +03001198 for (i = 0; i < slots; i++) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001199 slot = sdhci_pci_probe_slot(pdev, chip, first_bar + i);
1200 if (IS_ERR(slot)) {
Ameya Palandeb177bc92011-04-05 21:13:13 +03001201 for (i--; i >= 0; i--)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001202 sdhci_pci_remove_slot(chip->slots[i]);
1203 ret = PTR_ERR(slot);
1204 goto free;
1205 }
1206
1207 chip->slots[i] = slot;
1208 }
1209
1210 return 0;
1211
1212free:
1213 pci_set_drvdata(pdev, NULL);
1214 kfree(chip);
1215
1216err:
1217 pci_disable_device(pdev);
1218 return ret;
1219}
1220
1221static void __devexit sdhci_pci_remove(struct pci_dev *pdev)
1222{
1223 int i;
1224 struct sdhci_pci_chip *chip;
1225
1226 chip = pci_get_drvdata(pdev);
1227
1228 if (chip) {
Ameya Palandeb177bc92011-04-05 21:13:13 +03001229 for (i = 0; i < chip->num_slots; i++)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001230 sdhci_pci_remove_slot(chip->slots[i]);
1231
1232 pci_set_drvdata(pdev, NULL);
1233 kfree(chip);
1234 }
1235
1236 pci_disable_device(pdev);
1237}
1238
1239static struct pci_driver sdhci_driver = {
Ameya Palandeb177bc92011-04-05 21:13:13 +03001240 .name = "sdhci-pci",
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001241 .id_table = pci_ids,
Ameya Palandeb177bc92011-04-05 21:13:13 +03001242 .probe = sdhci_pci_probe,
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001243 .remove = __devexit_p(sdhci_pci_remove),
1244 .suspend = sdhci_pci_suspend,
1245 .resume = sdhci_pci_resume,
1246};
1247
1248/*****************************************************************************\
1249 * *
1250 * Driver init/exit *
1251 * *
1252\*****************************************************************************/
1253
1254static int __init sdhci_drv_init(void)
1255{
1256 return pci_register_driver(&sdhci_driver);
1257}
1258
1259static void __exit sdhci_drv_exit(void)
1260{
1261 pci_unregister_driver(&sdhci_driver);
1262}
1263
1264module_init(sdhci_drv_init);
1265module_exit(sdhci_drv_exit);
1266
Pierre Ossman32710e82009-04-08 20:14:54 +02001267MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001268MODULE_DESCRIPTION("Secure Digital Host Controller Interface PCI driver");
1269MODULE_LICENSE("GPL");