blob: d2e77fb21b26247e7cb0fda103114b7e9d42dda7 [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>
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030027#include <linux/pm_runtime.h>
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010028
29#include "sdhci.h"
30
31/*
32 * PCI registers
33 */
34
35#define PCI_SDHCI_IFPIO 0x00
36#define PCI_SDHCI_IFDMA 0x01
37#define PCI_SDHCI_IFVENDOR 0x02
38
39#define PCI_SLOT_INFO 0x40 /* 8 bits */
40#define PCI_SLOT_INFO_SLOTS(x) ((x >> 4) & 7)
41#define PCI_SLOT_INFO_FIRST_BAR_MASK 0x07
42
43#define MAX_SLOTS 8
44
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010045struct sdhci_pci_chip;
Pierre Ossman44894282008-04-04 19:36:59 +020046struct sdhci_pci_slot;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010047
Pierre Ossman22606402008-03-23 19:33:23 +010048struct sdhci_pci_fixes {
49 unsigned int quirks;
Adrian Hunterc43fd772011-10-17 10:52:44 +030050 bool allow_runtime_pm;
Pierre Ossman22606402008-03-23 19:33:23 +010051
Ameya Palandeb177bc92011-04-05 21:13:13 +030052 int (*probe) (struct sdhci_pci_chip *);
Pierre Ossman45211e22008-03-24 13:09:09 +010053
Ameya Palandeb177bc92011-04-05 21:13:13 +030054 int (*probe_slot) (struct sdhci_pci_slot *);
55 void (*remove_slot) (struct sdhci_pci_slot *, int);
Pierre Ossman44894282008-04-04 19:36:59 +020056
Manuel Lauss29495aa2011-11-03 11:09:45 +010057 int (*suspend) (struct sdhci_pci_chip *);
Ameya Palandeb177bc92011-04-05 21:13:13 +030058 int (*resume) (struct sdhci_pci_chip *);
Pierre Ossman22606402008-03-23 19:33:23 +010059};
60
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010061struct sdhci_pci_slot {
62 struct sdhci_pci_chip *chip;
63 struct sdhci_host *host;
64
65 int pci_bar;
Adrian Hunter0f201652011-08-29 16:42:13 +030066 int rst_n_gpio;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030067 int cd_gpio;
68 int cd_irq;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010069};
70
71struct sdhci_pci_chip {
72 struct pci_dev *pdev;
Pierre Ossman22606402008-03-23 19:33:23 +010073
74 unsigned int quirks;
Adrian Hunterc43fd772011-10-17 10:52:44 +030075 bool allow_runtime_pm;
Pierre Ossman22606402008-03-23 19:33:23 +010076 const struct sdhci_pci_fixes *fixes;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010077
78 int num_slots; /* Slots on controller */
79 struct sdhci_pci_slot *slots[MAX_SLOTS]; /* Pointers to host slots */
80};
81
Pierre Ossman22606402008-03-23 19:33:23 +010082
83/*****************************************************************************\
84 * *
85 * Hardware specific quirk handling *
86 * *
87\*****************************************************************************/
88
89static int ricoh_probe(struct sdhci_pci_chip *chip)
90{
Chris Ballc99436f2009-09-22 16:45:22 -070091 if (chip->pdev->subsystem_vendor == PCI_VENDOR_ID_SAMSUNG ||
92 chip->pdev->subsystem_vendor == PCI_VENDOR_ID_SONY)
Pierre Ossman22606402008-03-23 19:33:23 +010093 chip->quirks |= SDHCI_QUIRK_NO_CARD_NO_RESET;
Maxim Levitskyccc92c22010-08-10 18:01:42 -070094 return 0;
95}
Pierre Ossman22606402008-03-23 19:33:23 +010096
Maxim Levitskyccc92c22010-08-10 18:01:42 -070097static int ricoh_mmc_probe_slot(struct sdhci_pci_slot *slot)
98{
99 slot->host->caps =
100 ((0x21 << SDHCI_TIMEOUT_CLK_SHIFT)
101 & SDHCI_TIMEOUT_CLK_MASK) |
102
103 ((0x21 << SDHCI_CLOCK_BASE_SHIFT)
104 & SDHCI_CLOCK_BASE_MASK) |
105
106 SDHCI_TIMEOUT_CLK_UNIT |
107 SDHCI_CAN_VDD_330 |
108 SDHCI_CAN_DO_SDMA;
109 return 0;
110}
111
112static int ricoh_mmc_resume(struct sdhci_pci_chip *chip)
113{
114 /* Apply a delay to allow controller to settle */
115 /* Otherwise it becomes confused if card state changed
116 during suspend */
117 msleep(500);
Pierre Ossman22606402008-03-23 19:33:23 +0100118 return 0;
119}
120
121static const struct sdhci_pci_fixes sdhci_ricoh = {
122 .probe = ricoh_probe,
Vasily Khoruzhick84938292010-03-05 13:43:46 -0800123 .quirks = SDHCI_QUIRK_32BIT_DMA_ADDR |
124 SDHCI_QUIRK_FORCE_DMA |
125 SDHCI_QUIRK_CLOCK_BEFORE_RESET,
Pierre Ossman22606402008-03-23 19:33:23 +0100126};
127
Maxim Levitskyccc92c22010-08-10 18:01:42 -0700128static const struct sdhci_pci_fixes sdhci_ricoh_mmc = {
129 .probe_slot = ricoh_mmc_probe_slot,
130 .resume = ricoh_mmc_resume,
131 .quirks = SDHCI_QUIRK_32BIT_DMA_ADDR |
132 SDHCI_QUIRK_CLOCK_BEFORE_RESET |
133 SDHCI_QUIRK_NO_CARD_NO_RESET |
134 SDHCI_QUIRK_MISSING_CAPS
135};
136
Pierre Ossman22606402008-03-23 19:33:23 +0100137static const struct sdhci_pci_fixes sdhci_ene_712 = {
138 .quirks = SDHCI_QUIRK_SINGLE_POWER_WRITE |
139 SDHCI_QUIRK_BROKEN_DMA,
140};
141
142static const struct sdhci_pci_fixes sdhci_ene_714 = {
143 .quirks = SDHCI_QUIRK_SINGLE_POWER_WRITE |
144 SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS |
145 SDHCI_QUIRK_BROKEN_DMA,
146};
147
148static const struct sdhci_pci_fixes sdhci_cafe = {
149 .quirks = SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER |
Andres Salomona0874892009-03-02 21:48:20 +0100150 SDHCI_QUIRK_NO_BUSY_IRQ |
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200151 SDHCI_QUIRK_BROKEN_TIMEOUT_VAL,
Pierre Ossman22606402008-03-23 19:33:23 +0100152};
153
Major Lee68077b02011-06-29 14:23:46 +0300154static int mrst_hc_probe_slot(struct sdhci_pci_slot *slot)
155{
156 slot->host->mmc->caps |= MMC_CAP_8_BIT_DATA;
157 return 0;
158}
159
Alan Coxf9ee3ea2010-10-04 15:25:11 +0100160/*
161 * ADMA operation is disabled for Moorestown platform due to
162 * hardware bugs.
163 */
Jacob Pan35ac6f02010-11-09 13:57:29 +0000164static int mrst_hc_probe(struct sdhci_pci_chip *chip)
Alan Coxf9ee3ea2010-10-04 15:25:11 +0100165{
166 /*
Jacob Pan35ac6f02010-11-09 13:57:29 +0000167 * slots number is fixed here for MRST as SDIO3/5 are never used and
168 * have hardware bugs.
Alan Coxf9ee3ea2010-10-04 15:25:11 +0100169 */
170 chip->num_slots = 1;
171 return 0;
172}
173
Adrian Hunter0f201652011-08-29 16:42:13 +0300174/* Medfield eMMC hardware reset GPIOs */
175static int mfd_emmc0_rst_gpio = -EINVAL;
176static int mfd_emmc1_rst_gpio = -EINVAL;
177
178static int mfd_emmc_gpio_parse(struct sfi_table_header *table)
179{
180 struct sfi_table_simple *sb = (struct sfi_table_simple *)table;
181 struct sfi_gpio_table_entry *entry;
182 int i, num;
183
184 num = SFI_GET_NUM_ENTRIES(sb, struct sfi_gpio_table_entry);
185 entry = (struct sfi_gpio_table_entry *)sb->pentry;
186
187 for (i = 0; i < num; i++, entry++) {
188 if (!strncmp(entry->pin_name, "emmc0_rst", SFI_NAME_LEN))
189 mfd_emmc0_rst_gpio = entry->pin_no;
190 else if (!strncmp(entry->pin_name, "emmc1_rst", SFI_NAME_LEN))
191 mfd_emmc1_rst_gpio = entry->pin_no;
192 }
193
194 return 0;
195}
196
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300197#ifdef CONFIG_PM_RUNTIME
198
199static irqreturn_t mfd_sd_cd(int irq, void *dev_id)
200{
201 struct sdhci_pci_slot *slot = dev_id;
202 struct sdhci_host *host = slot->host;
203
204 mmc_detect_change(host->mmc, msecs_to_jiffies(200));
205 return IRQ_HANDLED;
206}
207
208#define MFLD_SD_CD_PIN 69
209
210static int mfd_sd_probe_slot(struct sdhci_pci_slot *slot)
211{
212 int err, irq, gpio = MFLD_SD_CD_PIN;
213
214 slot->cd_gpio = -EINVAL;
215 slot->cd_irq = -EINVAL;
216
217 err = gpio_request(gpio, "sd_cd");
218 if (err < 0)
219 goto out;
220
221 err = gpio_direction_input(gpio);
222 if (err < 0)
223 goto out_free;
224
225 irq = gpio_to_irq(gpio);
226 if (irq < 0)
227 goto out_free;
228
229 err = request_irq(irq, mfd_sd_cd, IRQF_TRIGGER_RISING |
230 IRQF_TRIGGER_FALLING, "sd_cd", slot);
231 if (err)
232 goto out_free;
233
234 slot->cd_gpio = gpio;
235 slot->cd_irq = irq;
236 slot->host->quirks2 |= SDHCI_QUIRK2_OWN_CARD_DETECTION;
237
238 return 0;
239
240out_free:
241 gpio_free(gpio);
242out:
243 dev_warn(&slot->chip->pdev->dev, "failed to setup card detect wake up\n");
244 return 0;
245}
246
247static void mfd_sd_remove_slot(struct sdhci_pci_slot *slot, int dead)
248{
249 if (slot->cd_irq >= 0)
250 free_irq(slot->cd_irq, slot);
251 gpio_free(slot->cd_gpio);
252}
253
254#else
255
256#define mfd_sd_probe_slot NULL
257#define mfd_sd_remove_slot NULL
258
259#endif
260
Adrian Hunter0d013bc2011-06-29 14:23:47 +0300261static int mfd_emmc_probe_slot(struct sdhci_pci_slot *slot)
262{
Adrian Hunter0f201652011-08-29 16:42:13 +0300263 const char *name = NULL;
264 int gpio = -EINVAL;
265
266 sfi_table_parse(SFI_SIG_GPIO, NULL, NULL, mfd_emmc_gpio_parse);
267
268 switch (slot->chip->pdev->device) {
269 case PCI_DEVICE_ID_INTEL_MFD_EMMC0:
270 gpio = mfd_emmc0_rst_gpio;
271 name = "eMMC0_reset";
272 break;
273 case PCI_DEVICE_ID_INTEL_MFD_EMMC1:
274 gpio = mfd_emmc1_rst_gpio;
275 name = "eMMC1_reset";
276 break;
277 }
278
279 if (!gpio_request(gpio, name)) {
280 gpio_direction_output(gpio, 1);
281 slot->rst_n_gpio = gpio;
282 slot->host->mmc->caps |= MMC_CAP_HW_RESET;
283 }
284
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300285 slot->host->mmc->caps |= MMC_CAP_8_BIT_DATA | MMC_CAP_NONREMOVABLE;
Adrian Hunter0f201652011-08-29 16:42:13 +0300286
Adrian Hunterf7c56ef2011-09-23 12:48:21 +0300287 slot->host->mmc->caps2 = MMC_CAP2_BOOTPART_NOACC;
288
Adrian Hunter0d013bc2011-06-29 14:23:47 +0300289 return 0;
290}
291
Adrian Hunter0f201652011-08-29 16:42:13 +0300292static void mfd_emmc_remove_slot(struct sdhci_pci_slot *slot, int dead)
293{
294 gpio_free(slot->rst_n_gpio);
295}
296
Alan Coxf9ee3ea2010-10-04 15:25:11 +0100297static const struct sdhci_pci_fixes sdhci_intel_mrst_hc0 = {
298 .quirks = SDHCI_QUIRK_BROKEN_ADMA | SDHCI_QUIRK_NO_HISPD_BIT,
Major Lee68077b02011-06-29 14:23:46 +0300299 .probe_slot = mrst_hc_probe_slot,
Alan Coxf9ee3ea2010-10-04 15:25:11 +0100300};
301
Jacob Pan35ac6f02010-11-09 13:57:29 +0000302static const struct sdhci_pci_fixes sdhci_intel_mrst_hc1_hc2 = {
Alan Coxf9ee3ea2010-10-04 15:25:11 +0100303 .quirks = SDHCI_QUIRK_BROKEN_ADMA | SDHCI_QUIRK_NO_HISPD_BIT,
Jacob Pan35ac6f02010-11-09 13:57:29 +0000304 .probe = mrst_hc_probe,
Alan Coxf9ee3ea2010-10-04 15:25:11 +0100305};
306
Xiaochen Shen29229052010-10-04 15:24:52 +0100307static const struct sdhci_pci_fixes sdhci_intel_mfd_sd = {
308 .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
Adrian Hunterc43fd772011-10-17 10:52:44 +0300309 .allow_runtime_pm = true,
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300310 .probe_slot = mfd_sd_probe_slot,
311 .remove_slot = mfd_sd_remove_slot,
Xiaochen Shen29229052010-10-04 15:24:52 +0100312};
313
Adrian Hunter0d013bc2011-06-29 14:23:47 +0300314static const struct sdhci_pci_fixes sdhci_intel_mfd_sdio = {
Xiaochen Shen29229052010-10-04 15:24:52 +0100315 .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
Adrian Hunterc43fd772011-10-17 10:52:44 +0300316 .allow_runtime_pm = true,
Xiaochen Shen29229052010-10-04 15:24:52 +0100317};
318
Adrian Hunter0d013bc2011-06-29 14:23:47 +0300319static const struct sdhci_pci_fixes sdhci_intel_mfd_emmc = {
320 .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
Adrian Hunterc43fd772011-10-17 10:52:44 +0300321 .allow_runtime_pm = true,
Adrian Hunter0d013bc2011-06-29 14:23:47 +0300322 .probe_slot = mfd_emmc_probe_slot,
Adrian Hunter0f201652011-08-29 16:42:13 +0300323 .remove_slot = mfd_emmc_remove_slot,
Adrian Hunter0d013bc2011-06-29 14:23:47 +0300324};
325
Jennifer Li26daa1e2010-11-17 23:01:59 -0500326/* O2Micro extra registers */
327#define O2_SD_LOCK_WP 0xD3
328#define O2_SD_MULTI_VCC3V 0xEE
329#define O2_SD_CLKREQ 0xEC
330#define O2_SD_CAPS 0xE0
331#define O2_SD_ADMA1 0xE2
332#define O2_SD_ADMA2 0xE7
333#define O2_SD_INF_MOD 0xF1
334
335static int o2_probe(struct sdhci_pci_chip *chip)
336{
337 int ret;
338 u8 scratch;
339
340 switch (chip->pdev->device) {
341 case PCI_DEVICE_ID_O2_8220:
342 case PCI_DEVICE_ID_O2_8221:
343 case PCI_DEVICE_ID_O2_8320:
344 case PCI_DEVICE_ID_O2_8321:
345 /* This extra setup is required due to broken ADMA. */
346 ret = pci_read_config_byte(chip->pdev, O2_SD_LOCK_WP, &scratch);
347 if (ret)
348 return ret;
349 scratch &= 0x7f;
350 pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch);
351
352 /* Set Multi 3 to VCC3V# */
353 pci_write_config_byte(chip->pdev, O2_SD_MULTI_VCC3V, 0x08);
354
355 /* Disable CLK_REQ# support after media DET */
356 ret = pci_read_config_byte(chip->pdev, O2_SD_CLKREQ, &scratch);
357 if (ret)
358 return ret;
359 scratch |= 0x20;
360 pci_write_config_byte(chip->pdev, O2_SD_CLKREQ, scratch);
361
362 /* Choose capabilities, enable SDMA. We have to write 0x01
363 * to the capabilities register first to unlock it.
364 */
365 ret = pci_read_config_byte(chip->pdev, O2_SD_CAPS, &scratch);
366 if (ret)
367 return ret;
368 scratch |= 0x01;
369 pci_write_config_byte(chip->pdev, O2_SD_CAPS, scratch);
370 pci_write_config_byte(chip->pdev, O2_SD_CAPS, 0x73);
371
372 /* Disable ADMA1/2 */
373 pci_write_config_byte(chip->pdev, O2_SD_ADMA1, 0x39);
374 pci_write_config_byte(chip->pdev, O2_SD_ADMA2, 0x08);
375
376 /* Disable the infinite transfer mode */
377 ret = pci_read_config_byte(chip->pdev, O2_SD_INF_MOD, &scratch);
378 if (ret)
379 return ret;
380 scratch |= 0x08;
381 pci_write_config_byte(chip->pdev, O2_SD_INF_MOD, scratch);
382
383 /* Lock WP */
384 ret = pci_read_config_byte(chip->pdev, O2_SD_LOCK_WP, &scratch);
385 if (ret)
386 return ret;
387 scratch |= 0x80;
388 pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch);
389 }
390
391 return 0;
392}
393
Pierre Ossman45211e22008-03-24 13:09:09 +0100394static int jmicron_pmos(struct sdhci_pci_chip *chip, int on)
395{
396 u8 scratch;
397 int ret;
398
399 ret = pci_read_config_byte(chip->pdev, 0xAE, &scratch);
400 if (ret)
401 return ret;
402
403 /*
404 * Turn PMOS on [bit 0], set over current detection to 2.4 V
405 * [bit 1:2] and enable over current debouncing [bit 6].
406 */
407 if (on)
408 scratch |= 0x47;
409 else
410 scratch &= ~0x47;
411
412 ret = pci_write_config_byte(chip->pdev, 0xAE, scratch);
413 if (ret)
414 return ret;
415
416 return 0;
417}
418
419static int jmicron_probe(struct sdhci_pci_chip *chip)
420{
421 int ret;
Takashi Iwai8f230f42010-12-08 10:04:30 +0100422 u16 mmcdev = 0;
Pierre Ossman45211e22008-03-24 13:09:09 +0100423
Pierre Ossman93fc48c2008-06-28 18:21:41 +0200424 if (chip->pdev->revision == 0) {
425 chip->quirks |= SDHCI_QUIRK_32BIT_DMA_ADDR |
426 SDHCI_QUIRK_32BIT_DMA_SIZE |
Pierre Ossman2134a922008-06-28 18:28:51 +0200427 SDHCI_QUIRK_32BIT_ADMA_SIZE |
Pierre Ossman4a3cba32008-07-29 00:11:16 +0200428 SDHCI_QUIRK_RESET_AFTER_REQUEST |
Pierre Ossman86a6a872009-02-02 21:13:49 +0100429 SDHCI_QUIRK_BROKEN_SMALL_PIO;
Pierre Ossman93fc48c2008-06-28 18:21:41 +0200430 }
431
Pierre Ossman45211e22008-03-24 13:09:09 +0100432 /*
Pierre Ossman44894282008-04-04 19:36:59 +0200433 * JMicron chips can have two interfaces to the same hardware
434 * in order to work around limitations in Microsoft's driver.
435 * We need to make sure we only bind to one of them.
436 *
437 * This code assumes two things:
438 *
439 * 1. The PCI code adds subfunctions in order.
440 *
441 * 2. The MMC interface has a lower subfunction number
442 * than the SD interface.
443 */
Takashi Iwai8f230f42010-12-08 10:04:30 +0100444 if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_SD)
445 mmcdev = PCI_DEVICE_ID_JMICRON_JMB38X_MMC;
446 else if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_SD)
447 mmcdev = PCI_DEVICE_ID_JMICRON_JMB388_ESD;
448
449 if (mmcdev) {
Pierre Ossman44894282008-04-04 19:36:59 +0200450 struct pci_dev *sd_dev;
451
452 sd_dev = NULL;
453 while ((sd_dev = pci_get_device(PCI_VENDOR_ID_JMICRON,
Takashi Iwai8f230f42010-12-08 10:04:30 +0100454 mmcdev, sd_dev)) != NULL) {
Pierre Ossman44894282008-04-04 19:36:59 +0200455 if ((PCI_SLOT(chip->pdev->devfn) ==
456 PCI_SLOT(sd_dev->devfn)) &&
457 (chip->pdev->bus == sd_dev->bus))
458 break;
459 }
460
461 if (sd_dev) {
462 pci_dev_put(sd_dev);
463 dev_info(&chip->pdev->dev, "Refusing to bind to "
464 "secondary interface.\n");
465 return -ENODEV;
466 }
467 }
468
469 /*
Pierre Ossman45211e22008-03-24 13:09:09 +0100470 * JMicron chips need a bit of a nudge to enable the power
471 * output pins.
472 */
473 ret = jmicron_pmos(chip, 1);
474 if (ret) {
475 dev_err(&chip->pdev->dev, "Failure enabling card power\n");
476 return ret;
477 }
478
Takashi Iwai82b0e232011-04-21 20:26:38 +0200479 /* quirk for unsable RO-detection on JM388 chips */
480 if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_SD ||
481 chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD)
482 chip->quirks |= SDHCI_QUIRK_UNSTABLE_RO_DETECT;
483
Pierre Ossman45211e22008-03-24 13:09:09 +0100484 return 0;
485}
486
Pierre Ossman44894282008-04-04 19:36:59 +0200487static void jmicron_enable_mmc(struct sdhci_host *host, int on)
488{
489 u8 scratch;
490
491 scratch = readb(host->ioaddr + 0xC0);
492
493 if (on)
494 scratch |= 0x01;
495 else
496 scratch &= ~0x01;
497
498 writeb(scratch, host->ioaddr + 0xC0);
499}
500
501static int jmicron_probe_slot(struct sdhci_pci_slot *slot)
502{
Pierre Ossman2134a922008-06-28 18:28:51 +0200503 if (slot->chip->pdev->revision == 0) {
504 u16 version;
505
506 version = readl(slot->host->ioaddr + SDHCI_HOST_VERSION);
507 version = (version & SDHCI_VENDOR_VER_MASK) >>
508 SDHCI_VENDOR_VER_SHIFT;
509
510 /*
511 * Older versions of the chip have lots of nasty glitches
512 * in the ADMA engine. It's best just to avoid it
513 * completely.
514 */
515 if (version < 0xAC)
516 slot->host->quirks |= SDHCI_QUIRK_BROKEN_ADMA;
517 }
518
Takashi Iwai8f230f42010-12-08 10:04:30 +0100519 /* JM388 MMC doesn't support 1.8V while SD supports it */
520 if (slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD) {
521 slot->host->ocr_avail_sd = MMC_VDD_32_33 | MMC_VDD_33_34 |
522 MMC_VDD_29_30 | MMC_VDD_30_31 |
523 MMC_VDD_165_195; /* allow 1.8V */
524 slot->host->ocr_avail_mmc = MMC_VDD_32_33 | MMC_VDD_33_34 |
525 MMC_VDD_29_30 | MMC_VDD_30_31; /* no 1.8V for MMC */
526 }
527
Pierre Ossman44894282008-04-04 19:36:59 +0200528 /*
529 * The secondary interface requires a bit set to get the
530 * interrupts.
531 */
Takashi Iwai8f230f42010-12-08 10:04:30 +0100532 if (slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC ||
533 slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD)
Pierre Ossman44894282008-04-04 19:36:59 +0200534 jmicron_enable_mmc(slot->host, 1);
535
Takashi Iwaid75c1082010-12-16 17:54:14 +0100536 slot->host->mmc->caps |= MMC_CAP_BUS_WIDTH_TEST;
537
Pierre Ossman44894282008-04-04 19:36:59 +0200538 return 0;
539}
540
Pierre Ossman1e728592008-04-16 19:13:13 +0200541static void jmicron_remove_slot(struct sdhci_pci_slot *slot, int dead)
Pierre Ossman44894282008-04-04 19:36:59 +0200542{
Pierre Ossman1e728592008-04-16 19:13:13 +0200543 if (dead)
544 return;
545
Takashi Iwai8f230f42010-12-08 10:04:30 +0100546 if (slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC ||
547 slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD)
Pierre Ossman44894282008-04-04 19:36:59 +0200548 jmicron_enable_mmc(slot->host, 0);
549}
550
Manuel Lauss29495aa2011-11-03 11:09:45 +0100551static int jmicron_suspend(struct sdhci_pci_chip *chip)
Pierre Ossman44894282008-04-04 19:36:59 +0200552{
553 int i;
554
Takashi Iwai8f230f42010-12-08 10:04:30 +0100555 if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC ||
556 chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD) {
Ameya Palandeb177bc92011-04-05 21:13:13 +0300557 for (i = 0; i < chip->num_slots; i++)
Pierre Ossman44894282008-04-04 19:36:59 +0200558 jmicron_enable_mmc(chip->slots[i]->host, 0);
559 }
560
561 return 0;
562}
563
Pierre Ossman45211e22008-03-24 13:09:09 +0100564static int jmicron_resume(struct sdhci_pci_chip *chip)
565{
Pierre Ossman44894282008-04-04 19:36:59 +0200566 int ret, i;
567
Takashi Iwai8f230f42010-12-08 10:04:30 +0100568 if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC ||
569 chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD) {
Ameya Palandeb177bc92011-04-05 21:13:13 +0300570 for (i = 0; i < chip->num_slots; i++)
Pierre Ossman44894282008-04-04 19:36:59 +0200571 jmicron_enable_mmc(chip->slots[i]->host, 1);
572 }
Pierre Ossman45211e22008-03-24 13:09:09 +0100573
574 ret = jmicron_pmos(chip, 1);
575 if (ret) {
576 dev_err(&chip->pdev->dev, "Failure enabling card power\n");
577 return ret;
578 }
579
580 return 0;
581}
582
Jennifer Li26daa1e2010-11-17 23:01:59 -0500583static const struct sdhci_pci_fixes sdhci_o2 = {
584 .probe = o2_probe,
585};
586
Pierre Ossman22606402008-03-23 19:33:23 +0100587static const struct sdhci_pci_fixes sdhci_jmicron = {
Pierre Ossman45211e22008-03-24 13:09:09 +0100588 .probe = jmicron_probe,
589
Pierre Ossman44894282008-04-04 19:36:59 +0200590 .probe_slot = jmicron_probe_slot,
591 .remove_slot = jmicron_remove_slot,
592
593 .suspend = jmicron_suspend,
Pierre Ossman45211e22008-03-24 13:09:09 +0100594 .resume = jmicron_resume,
Pierre Ossman22606402008-03-23 19:33:23 +0100595};
596
Nicolas Pitrea7a61862009-12-14 18:01:26 -0800597/* SysKonnect CardBus2SDIO extra registers */
598#define SYSKT_CTRL 0x200
599#define SYSKT_RDFIFO_STAT 0x204
600#define SYSKT_WRFIFO_STAT 0x208
601#define SYSKT_POWER_DATA 0x20c
602#define SYSKT_POWER_330 0xef
603#define SYSKT_POWER_300 0xf8
604#define SYSKT_POWER_184 0xcc
605#define SYSKT_POWER_CMD 0x20d
606#define SYSKT_POWER_START (1 << 7)
607#define SYSKT_POWER_STATUS 0x20e
608#define SYSKT_POWER_STATUS_OK (1 << 0)
609#define SYSKT_BOARD_REV 0x210
610#define SYSKT_CHIP_REV 0x211
611#define SYSKT_CONF_DATA 0x212
612#define SYSKT_CONF_DATA_1V8 (1 << 2)
613#define SYSKT_CONF_DATA_2V5 (1 << 1)
614#define SYSKT_CONF_DATA_3V3 (1 << 0)
615
616static int syskt_probe(struct sdhci_pci_chip *chip)
617{
618 if ((chip->pdev->class & 0x0000FF) == PCI_SDHCI_IFVENDOR) {
619 chip->pdev->class &= ~0x0000FF;
620 chip->pdev->class |= PCI_SDHCI_IFDMA;
621 }
622 return 0;
623}
624
625static int syskt_probe_slot(struct sdhci_pci_slot *slot)
626{
627 int tm, ps;
628
629 u8 board_rev = readb(slot->host->ioaddr + SYSKT_BOARD_REV);
630 u8 chip_rev = readb(slot->host->ioaddr + SYSKT_CHIP_REV);
631 dev_info(&slot->chip->pdev->dev, "SysKonnect CardBus2SDIO, "
632 "board rev %d.%d, chip rev %d.%d\n",
633 board_rev >> 4, board_rev & 0xf,
634 chip_rev >> 4, chip_rev & 0xf);
635 if (chip_rev >= 0x20)
636 slot->host->quirks |= SDHCI_QUIRK_FORCE_DMA;
637
638 writeb(SYSKT_POWER_330, slot->host->ioaddr + SYSKT_POWER_DATA);
639 writeb(SYSKT_POWER_START, slot->host->ioaddr + SYSKT_POWER_CMD);
640 udelay(50);
641 tm = 10; /* Wait max 1 ms */
642 do {
643 ps = readw(slot->host->ioaddr + SYSKT_POWER_STATUS);
644 if (ps & SYSKT_POWER_STATUS_OK)
645 break;
646 udelay(100);
647 } while (--tm);
648 if (!tm) {
649 dev_err(&slot->chip->pdev->dev,
650 "power regulator never stabilized");
651 writeb(0, slot->host->ioaddr + SYSKT_POWER_CMD);
652 return -ENODEV;
653 }
654
655 return 0;
656}
657
658static const struct sdhci_pci_fixes sdhci_syskt = {
659 .quirks = SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER,
660 .probe = syskt_probe,
661 .probe_slot = syskt_probe_slot,
662};
663
Harald Welte557b0692009-06-18 16:53:38 +0200664static int via_probe(struct sdhci_pci_chip *chip)
665{
666 if (chip->pdev->revision == 0x10)
667 chip->quirks |= SDHCI_QUIRK_DELAY_AFTER_POWER;
668
669 return 0;
670}
671
672static const struct sdhci_pci_fixes sdhci_via = {
673 .probe = via_probe,
674};
675
Pierre Ossman22606402008-03-23 19:33:23 +0100676static const struct pci_device_id pci_ids[] __devinitdata = {
677 {
678 .vendor = PCI_VENDOR_ID_RICOH,
679 .device = PCI_DEVICE_ID_RICOH_R5C822,
680 .subvendor = PCI_ANY_ID,
681 .subdevice = PCI_ANY_ID,
682 .driver_data = (kernel_ulong_t)&sdhci_ricoh,
683 },
684
685 {
Maxim Levitskyccc92c22010-08-10 18:01:42 -0700686 .vendor = PCI_VENDOR_ID_RICOH,
687 .device = 0x843,
688 .subvendor = PCI_ANY_ID,
689 .subdevice = PCI_ANY_ID,
690 .driver_data = (kernel_ulong_t)&sdhci_ricoh_mmc,
691 },
692
693 {
Pablo Castillo568133e2010-08-10 18:02:01 -0700694 .vendor = PCI_VENDOR_ID_RICOH,
695 .device = 0xe822,
696 .subvendor = PCI_ANY_ID,
697 .subdevice = PCI_ANY_ID,
698 .driver_data = (kernel_ulong_t)&sdhci_ricoh_mmc,
699 },
700
701 {
Manoj Iyer5fd11c02011-02-11 16:25:31 -0600702 .vendor = PCI_VENDOR_ID_RICOH,
703 .device = 0xe823,
704 .subvendor = PCI_ANY_ID,
705 .subdevice = PCI_ANY_ID,
706 .driver_data = (kernel_ulong_t)&sdhci_ricoh_mmc,
707 },
708
709 {
Pierre Ossman22606402008-03-23 19:33:23 +0100710 .vendor = PCI_VENDOR_ID_ENE,
711 .device = PCI_DEVICE_ID_ENE_CB712_SD,
712 .subvendor = PCI_ANY_ID,
713 .subdevice = PCI_ANY_ID,
714 .driver_data = (kernel_ulong_t)&sdhci_ene_712,
715 },
716
717 {
718 .vendor = PCI_VENDOR_ID_ENE,
719 .device = PCI_DEVICE_ID_ENE_CB712_SD_2,
720 .subvendor = PCI_ANY_ID,
721 .subdevice = PCI_ANY_ID,
722 .driver_data = (kernel_ulong_t)&sdhci_ene_712,
723 },
724
725 {
726 .vendor = PCI_VENDOR_ID_ENE,
727 .device = PCI_DEVICE_ID_ENE_CB714_SD,
728 .subvendor = PCI_ANY_ID,
729 .subdevice = PCI_ANY_ID,
730 .driver_data = (kernel_ulong_t)&sdhci_ene_714,
731 },
732
733 {
734 .vendor = PCI_VENDOR_ID_ENE,
735 .device = PCI_DEVICE_ID_ENE_CB714_SD_2,
736 .subvendor = PCI_ANY_ID,
737 .subdevice = PCI_ANY_ID,
738 .driver_data = (kernel_ulong_t)&sdhci_ene_714,
739 },
740
741 {
742 .vendor = PCI_VENDOR_ID_MARVELL,
David Woodhouse8c5eb882008-09-03 09:45:57 +0100743 .device = PCI_DEVICE_ID_MARVELL_88ALP01_SD,
Pierre Ossman22606402008-03-23 19:33:23 +0100744 .subvendor = PCI_ANY_ID,
745 .subdevice = PCI_ANY_ID,
746 .driver_data = (kernel_ulong_t)&sdhci_cafe,
747 },
748
749 {
750 .vendor = PCI_VENDOR_ID_JMICRON,
751 .device = PCI_DEVICE_ID_JMICRON_JMB38X_SD,
752 .subvendor = PCI_ANY_ID,
753 .subdevice = PCI_ANY_ID,
754 .driver_data = (kernel_ulong_t)&sdhci_jmicron,
755 },
756
Pierre Ossman44894282008-04-04 19:36:59 +0200757 {
758 .vendor = PCI_VENDOR_ID_JMICRON,
759 .device = PCI_DEVICE_ID_JMICRON_JMB38X_MMC,
760 .subvendor = PCI_ANY_ID,
761 .subdevice = PCI_ANY_ID,
762 .driver_data = (kernel_ulong_t)&sdhci_jmicron,
763 },
764
Harald Welte557b0692009-06-18 16:53:38 +0200765 {
Takashi Iwai8f230f42010-12-08 10:04:30 +0100766 .vendor = PCI_VENDOR_ID_JMICRON,
767 .device = PCI_DEVICE_ID_JMICRON_JMB388_SD,
768 .subvendor = PCI_ANY_ID,
769 .subdevice = PCI_ANY_ID,
770 .driver_data = (kernel_ulong_t)&sdhci_jmicron,
771 },
772
773 {
774 .vendor = PCI_VENDOR_ID_JMICRON,
775 .device = PCI_DEVICE_ID_JMICRON_JMB388_ESD,
776 .subvendor = PCI_ANY_ID,
777 .subdevice = PCI_ANY_ID,
778 .driver_data = (kernel_ulong_t)&sdhci_jmicron,
779 },
780
781 {
Nicolas Pitrea7a61862009-12-14 18:01:26 -0800782 .vendor = PCI_VENDOR_ID_SYSKONNECT,
783 .device = 0x8000,
784 .subvendor = PCI_ANY_ID,
785 .subdevice = PCI_ANY_ID,
786 .driver_data = (kernel_ulong_t)&sdhci_syskt,
787 },
788
789 {
Harald Welte557b0692009-06-18 16:53:38 +0200790 .vendor = PCI_VENDOR_ID_VIA,
791 .device = 0x95d0,
792 .subvendor = PCI_ANY_ID,
793 .subdevice = PCI_ANY_ID,
794 .driver_data = (kernel_ulong_t)&sdhci_via,
795 },
796
Xiaochen Shen29229052010-10-04 15:24:52 +0100797 {
798 .vendor = PCI_VENDOR_ID_INTEL,
Alan Coxf9ee3ea2010-10-04 15:25:11 +0100799 .device = PCI_DEVICE_ID_INTEL_MRST_SD0,
800 .subvendor = PCI_ANY_ID,
801 .subdevice = PCI_ANY_ID,
802 .driver_data = (kernel_ulong_t)&sdhci_intel_mrst_hc0,
803 },
804
805 {
806 .vendor = PCI_VENDOR_ID_INTEL,
807 .device = PCI_DEVICE_ID_INTEL_MRST_SD1,
808 .subvendor = PCI_ANY_ID,
809 .subdevice = PCI_ANY_ID,
Jacob Pan35ac6f02010-11-09 13:57:29 +0000810 .driver_data = (kernel_ulong_t)&sdhci_intel_mrst_hc1_hc2,
811 },
812
813 {
814 .vendor = PCI_VENDOR_ID_INTEL,
815 .device = PCI_DEVICE_ID_INTEL_MRST_SD2,
816 .subvendor = PCI_ANY_ID,
817 .subdevice = PCI_ANY_ID,
818 .driver_data = (kernel_ulong_t)&sdhci_intel_mrst_hc1_hc2,
Alan Coxf9ee3ea2010-10-04 15:25:11 +0100819 },
820
821 {
822 .vendor = PCI_VENDOR_ID_INTEL,
Xiaochen Shen29229052010-10-04 15:24:52 +0100823 .device = PCI_DEVICE_ID_INTEL_MFD_SD,
824 .subvendor = PCI_ANY_ID,
825 .subdevice = PCI_ANY_ID,
826 .driver_data = (kernel_ulong_t)&sdhci_intel_mfd_sd,
827 },
828
829 {
830 .vendor = PCI_VENDOR_ID_INTEL,
831 .device = PCI_DEVICE_ID_INTEL_MFD_SDIO1,
832 .subvendor = PCI_ANY_ID,
833 .subdevice = PCI_ANY_ID,
Adrian Hunter0d013bc2011-06-29 14:23:47 +0300834 .driver_data = (kernel_ulong_t)&sdhci_intel_mfd_sdio,
Xiaochen Shen29229052010-10-04 15:24:52 +0100835 },
836
837 {
838 .vendor = PCI_VENDOR_ID_INTEL,
839 .device = PCI_DEVICE_ID_INTEL_MFD_SDIO2,
840 .subvendor = PCI_ANY_ID,
841 .subdevice = PCI_ANY_ID,
Adrian Hunter0d013bc2011-06-29 14:23:47 +0300842 .driver_data = (kernel_ulong_t)&sdhci_intel_mfd_sdio,
Xiaochen Shen29229052010-10-04 15:24:52 +0100843 },
844
845 {
846 .vendor = PCI_VENDOR_ID_INTEL,
847 .device = PCI_DEVICE_ID_INTEL_MFD_EMMC0,
848 .subvendor = PCI_ANY_ID,
849 .subdevice = PCI_ANY_ID,
Adrian Hunter0d013bc2011-06-29 14:23:47 +0300850 .driver_data = (kernel_ulong_t)&sdhci_intel_mfd_emmc,
Xiaochen Shen29229052010-10-04 15:24:52 +0100851 },
852
853 {
854 .vendor = PCI_VENDOR_ID_INTEL,
855 .device = PCI_DEVICE_ID_INTEL_MFD_EMMC1,
856 .subvendor = PCI_ANY_ID,
857 .subdevice = PCI_ANY_ID,
Adrian Hunter0d013bc2011-06-29 14:23:47 +0300858 .driver_data = (kernel_ulong_t)&sdhci_intel_mfd_emmc,
Xiaochen Shen29229052010-10-04 15:24:52 +0100859 },
860
Jennifer Li26daa1e2010-11-17 23:01:59 -0500861 {
862 .vendor = PCI_VENDOR_ID_O2,
863 .device = PCI_DEVICE_ID_O2_8120,
864 .subvendor = PCI_ANY_ID,
865 .subdevice = PCI_ANY_ID,
866 .driver_data = (kernel_ulong_t)&sdhci_o2,
867 },
868
869 {
870 .vendor = PCI_VENDOR_ID_O2,
871 .device = PCI_DEVICE_ID_O2_8220,
872 .subvendor = PCI_ANY_ID,
873 .subdevice = PCI_ANY_ID,
874 .driver_data = (kernel_ulong_t)&sdhci_o2,
875 },
876
877 {
878 .vendor = PCI_VENDOR_ID_O2,
879 .device = PCI_DEVICE_ID_O2_8221,
880 .subvendor = PCI_ANY_ID,
881 .subdevice = PCI_ANY_ID,
882 .driver_data = (kernel_ulong_t)&sdhci_o2,
883 },
884
885 {
886 .vendor = PCI_VENDOR_ID_O2,
887 .device = PCI_DEVICE_ID_O2_8320,
888 .subvendor = PCI_ANY_ID,
889 .subdevice = PCI_ANY_ID,
890 .driver_data = (kernel_ulong_t)&sdhci_o2,
891 },
892
893 {
894 .vendor = PCI_VENDOR_ID_O2,
895 .device = PCI_DEVICE_ID_O2_8321,
896 .subvendor = PCI_ANY_ID,
897 .subdevice = PCI_ANY_ID,
898 .driver_data = (kernel_ulong_t)&sdhci_o2,
899 },
900
Pierre Ossman22606402008-03-23 19:33:23 +0100901 { /* Generic SD host controller */
902 PCI_DEVICE_CLASS((PCI_CLASS_SYSTEM_SDHCI << 8), 0xFFFF00)
903 },
904
905 { /* end: all zeroes */ },
906};
907
908MODULE_DEVICE_TABLE(pci, pci_ids);
909
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100910/*****************************************************************************\
911 * *
912 * SDHCI core callbacks *
913 * *
914\*****************************************************************************/
915
916static int sdhci_pci_enable_dma(struct sdhci_host *host)
917{
918 struct sdhci_pci_slot *slot;
919 struct pci_dev *pdev;
920 int ret;
921
922 slot = sdhci_priv(host);
923 pdev = slot->chip->pdev;
924
925 if (((pdev->class & 0xFFFF00) == (PCI_CLASS_SYSTEM_SDHCI << 8)) &&
926 ((pdev->class & 0x0000FF) != PCI_SDHCI_IFDMA) &&
Richard Röjforsa13abc72009-09-22 16:45:30 -0700927 (host->flags & SDHCI_USE_SDMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100928 dev_warn(&pdev->dev, "Will use DMA mode even though HW "
929 "doesn't fully claim to support it.\n");
930 }
931
Yang Hongyang284901a2009-04-06 19:01:15 -0700932 ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100933 if (ret)
934 return ret;
935
936 pci_set_master(pdev);
937
938 return 0;
939}
940
Major Lee68077b02011-06-29 14:23:46 +0300941static int sdhci_pci_8bit_width(struct sdhci_host *host, int width)
942{
943 u8 ctrl;
944
945 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
946
947 switch (width) {
948 case MMC_BUS_WIDTH_8:
949 ctrl |= SDHCI_CTRL_8BITBUS;
950 ctrl &= ~SDHCI_CTRL_4BITBUS;
951 break;
952 case MMC_BUS_WIDTH_4:
953 ctrl |= SDHCI_CTRL_4BITBUS;
954 ctrl &= ~SDHCI_CTRL_8BITBUS;
955 break;
956 default:
957 ctrl &= ~(SDHCI_CTRL_8BITBUS | SDHCI_CTRL_4BITBUS);
958 break;
959 }
960
961 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
962
963 return 0;
964}
965
Adrian Hunter0f201652011-08-29 16:42:13 +0300966static void sdhci_pci_hw_reset(struct sdhci_host *host)
967{
968 struct sdhci_pci_slot *slot = sdhci_priv(host);
969 int rst_n_gpio = slot->rst_n_gpio;
970
971 if (!gpio_is_valid(rst_n_gpio))
972 return;
973 gpio_set_value_cansleep(rst_n_gpio, 0);
974 /* For eMMC, minimum is 1us but give it 10us for good measure */
975 udelay(10);
976 gpio_set_value_cansleep(rst_n_gpio, 1);
977 /* For eMMC, minimum is 200us but give it 300us for good measure */
978 usleep_range(300, 1000);
979}
980
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100981static struct sdhci_ops sdhci_pci_ops = {
982 .enable_dma = sdhci_pci_enable_dma,
Major Lee68077b02011-06-29 14:23:46 +0300983 .platform_8bit_width = sdhci_pci_8bit_width,
Adrian Hunter0f201652011-08-29 16:42:13 +0300984 .hw_reset = sdhci_pci_hw_reset,
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100985};
986
987/*****************************************************************************\
988 * *
989 * Suspend/resume *
990 * *
991\*****************************************************************************/
992
993#ifdef CONFIG_PM
994
Manuel Lauss29495aa2011-11-03 11:09:45 +0100995static int sdhci_pci_suspend(struct device *dev)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100996{
Manuel Lauss29495aa2011-11-03 11:09:45 +0100997 struct pci_dev *pdev = to_pci_dev(dev);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100998 struct sdhci_pci_chip *chip;
999 struct sdhci_pci_slot *slot;
Daniel Drake5f619702010-11-04 22:20:39 +00001000 mmc_pm_flag_t slot_pm_flags;
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08001001 mmc_pm_flag_t pm_flags = 0;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001002 int i, ret;
1003
1004 chip = pci_get_drvdata(pdev);
1005 if (!chip)
1006 return 0;
1007
Ameya Palandeb177bc92011-04-05 21:13:13 +03001008 for (i = 0; i < chip->num_slots; i++) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001009 slot = chip->slots[i];
1010 if (!slot)
1011 continue;
1012
Manuel Lauss29495aa2011-11-03 11:09:45 +01001013 ret = sdhci_suspend_host(slot->host);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001014
Axel Linb678b912011-12-03 15:28:05 +08001015 if (ret)
1016 goto err_pci_suspend;
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08001017
Daniel Drake5f619702010-11-04 22:20:39 +00001018 slot_pm_flags = slot->host->mmc->pm_flags;
1019 if (slot_pm_flags & MMC_PM_WAKE_SDIO_IRQ)
1020 sdhci_enable_irq_wakeups(slot->host);
1021
1022 pm_flags |= slot_pm_flags;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001023 }
1024
Pierre Ossman44894282008-04-04 19:36:59 +02001025 if (chip->fixes && chip->fixes->suspend) {
Manuel Lauss29495aa2011-11-03 11:09:45 +01001026 ret = chip->fixes->suspend(chip);
Axel Linb678b912011-12-03 15:28:05 +08001027 if (ret)
1028 goto err_pci_suspend;
Pierre Ossman44894282008-04-04 19:36:59 +02001029 }
1030
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001031 pci_save_state(pdev);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08001032 if (pm_flags & MMC_PM_KEEP_POWER) {
Daniel Drake5f619702010-11-04 22:20:39 +00001033 if (pm_flags & MMC_PM_WAKE_SDIO_IRQ) {
1034 pci_pme_active(pdev, true);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08001035 pci_enable_wake(pdev, PCI_D3hot, 1);
Daniel Drake5f619702010-11-04 22:20:39 +00001036 }
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08001037 pci_set_power_state(pdev, PCI_D3hot);
1038 } else {
Manuel Lauss29495aa2011-11-03 11:09:45 +01001039 pci_enable_wake(pdev, PCI_D3hot, 0);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08001040 pci_disable_device(pdev);
Manuel Lauss29495aa2011-11-03 11:09:45 +01001041 pci_set_power_state(pdev, PCI_D3hot);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08001042 }
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001043
1044 return 0;
Axel Linb678b912011-12-03 15:28:05 +08001045
1046err_pci_suspend:
1047 while (--i >= 0)
1048 sdhci_resume_host(chip->slots[i]->host);
1049 return ret;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001050}
1051
Manuel Lauss29495aa2011-11-03 11:09:45 +01001052static int sdhci_pci_resume(struct device *dev)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001053{
Manuel Lauss29495aa2011-11-03 11:09:45 +01001054 struct pci_dev *pdev = to_pci_dev(dev);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001055 struct sdhci_pci_chip *chip;
1056 struct sdhci_pci_slot *slot;
1057 int i, ret;
1058
1059 chip = pci_get_drvdata(pdev);
1060 if (!chip)
1061 return 0;
1062
1063 pci_set_power_state(pdev, PCI_D0);
1064 pci_restore_state(pdev);
1065 ret = pci_enable_device(pdev);
1066 if (ret)
1067 return ret;
1068
Pierre Ossman45211e22008-03-24 13:09:09 +01001069 if (chip->fixes && chip->fixes->resume) {
1070 ret = chip->fixes->resume(chip);
1071 if (ret)
1072 return ret;
1073 }
1074
Ameya Palandeb177bc92011-04-05 21:13:13 +03001075 for (i = 0; i < chip->num_slots; i++) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001076 slot = chip->slots[i];
1077 if (!slot)
1078 continue;
1079
1080 ret = sdhci_resume_host(slot->host);
1081 if (ret)
1082 return ret;
1083 }
1084
1085 return 0;
1086}
1087
1088#else /* CONFIG_PM */
1089
1090#define sdhci_pci_suspend NULL
1091#define sdhci_pci_resume NULL
1092
1093#endif /* CONFIG_PM */
1094
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001095#ifdef CONFIG_PM_RUNTIME
1096
1097static int sdhci_pci_runtime_suspend(struct device *dev)
1098{
1099 struct pci_dev *pdev = container_of(dev, struct pci_dev, dev);
1100 struct sdhci_pci_chip *chip;
1101 struct sdhci_pci_slot *slot;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001102 int i, ret;
1103
1104 chip = pci_get_drvdata(pdev);
1105 if (!chip)
1106 return 0;
1107
1108 for (i = 0; i < chip->num_slots; i++) {
1109 slot = chip->slots[i];
1110 if (!slot)
1111 continue;
1112
1113 ret = sdhci_runtime_suspend_host(slot->host);
1114
Axel Linb678b912011-12-03 15:28:05 +08001115 if (ret)
1116 goto err_pci_runtime_suspend;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001117 }
1118
1119 if (chip->fixes && chip->fixes->suspend) {
Manuel Lauss29495aa2011-11-03 11:09:45 +01001120 ret = chip->fixes->suspend(chip);
Axel Linb678b912011-12-03 15:28:05 +08001121 if (ret)
1122 goto err_pci_runtime_suspend;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001123 }
1124
1125 return 0;
Axel Linb678b912011-12-03 15:28:05 +08001126
1127err_pci_runtime_suspend:
1128 while (--i >= 0)
1129 sdhci_runtime_resume_host(chip->slots[i]->host);
1130 return ret;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001131}
1132
1133static int sdhci_pci_runtime_resume(struct device *dev)
1134{
1135 struct pci_dev *pdev = container_of(dev, struct pci_dev, dev);
1136 struct sdhci_pci_chip *chip;
1137 struct sdhci_pci_slot *slot;
1138 int i, ret;
1139
1140 chip = pci_get_drvdata(pdev);
1141 if (!chip)
1142 return 0;
1143
1144 if (chip->fixes && chip->fixes->resume) {
1145 ret = chip->fixes->resume(chip);
1146 if (ret)
1147 return ret;
1148 }
1149
1150 for (i = 0; i < chip->num_slots; i++) {
1151 slot = chip->slots[i];
1152 if (!slot)
1153 continue;
1154
1155 ret = sdhci_runtime_resume_host(slot->host);
1156 if (ret)
1157 return ret;
1158 }
1159
1160 return 0;
1161}
1162
1163static int sdhci_pci_runtime_idle(struct device *dev)
1164{
1165 return 0;
1166}
1167
1168#else
1169
1170#define sdhci_pci_runtime_suspend NULL
1171#define sdhci_pci_runtime_resume NULL
1172#define sdhci_pci_runtime_idle NULL
1173
1174#endif
1175
1176static const struct dev_pm_ops sdhci_pci_pm_ops = {
Manuel Lauss29495aa2011-11-03 11:09:45 +01001177 .suspend = sdhci_pci_suspend,
1178 .resume = sdhci_pci_resume,
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001179 .runtime_suspend = sdhci_pci_runtime_suspend,
1180 .runtime_resume = sdhci_pci_runtime_resume,
1181 .runtime_idle = sdhci_pci_runtime_idle,
1182};
1183
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001184/*****************************************************************************\
1185 * *
1186 * Device probing/removal *
1187 * *
1188\*****************************************************************************/
1189
1190static struct sdhci_pci_slot * __devinit sdhci_pci_probe_slot(
1191 struct pci_dev *pdev, struct sdhci_pci_chip *chip, int bar)
1192{
1193 struct sdhci_pci_slot *slot;
1194 struct sdhci_host *host;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001195 int ret;
1196
1197 if (!(pci_resource_flags(pdev, bar) & IORESOURCE_MEM)) {
1198 dev_err(&pdev->dev, "BAR %d is not iomem. Aborting.\n", bar);
1199 return ERR_PTR(-ENODEV);
1200 }
1201
1202 if (pci_resource_len(pdev, bar) != 0x100) {
1203 dev_err(&pdev->dev, "Invalid iomem size. You may "
1204 "experience problems.\n");
1205 }
1206
1207 if ((pdev->class & 0x0000FF) == PCI_SDHCI_IFVENDOR) {
1208 dev_err(&pdev->dev, "Vendor specific interface. Aborting.\n");
1209 return ERR_PTR(-ENODEV);
1210 }
1211
1212 if ((pdev->class & 0x0000FF) > PCI_SDHCI_IFVENDOR) {
1213 dev_err(&pdev->dev, "Unknown interface. Aborting.\n");
1214 return ERR_PTR(-ENODEV);
1215 }
1216
1217 host = sdhci_alloc_host(&pdev->dev, sizeof(struct sdhci_pci_slot));
1218 if (IS_ERR(host)) {
Dan Carpenterc60a32c2009-04-10 23:31:10 +02001219 dev_err(&pdev->dev, "cannot allocate host\n");
Julia Lawalldc0fd7b2010-05-26 14:42:11 -07001220 return ERR_CAST(host);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001221 }
1222
1223 slot = sdhci_priv(host);
1224
1225 slot->chip = chip;
1226 slot->host = host;
1227 slot->pci_bar = bar;
Adrian Hunter0f201652011-08-29 16:42:13 +03001228 slot->rst_n_gpio = -EINVAL;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001229
1230 host->hw_name = "PCI";
1231 host->ops = &sdhci_pci_ops;
1232 host->quirks = chip->quirks;
1233
1234 host->irq = pdev->irq;
1235
1236 ret = pci_request_region(pdev, bar, mmc_hostname(host->mmc));
1237 if (ret) {
1238 dev_err(&pdev->dev, "cannot request region\n");
Dan Carpenterc60a32c2009-04-10 23:31:10 +02001239 goto free;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001240 }
1241
Arjan van de Ven092f82e2008-09-28 16:15:56 -07001242 host->ioaddr = pci_ioremap_bar(pdev, bar);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001243 if (!host->ioaddr) {
1244 dev_err(&pdev->dev, "failed to remap registers\n");
Chris Ball9fdcdbb2011-03-29 00:46:12 -04001245 ret = -ENOMEM;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001246 goto release;
1247 }
1248
Pierre Ossman44894282008-04-04 19:36:59 +02001249 if (chip->fixes && chip->fixes->probe_slot) {
1250 ret = chip->fixes->probe_slot(slot);
1251 if (ret)
1252 goto unmap;
1253 }
1254
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08001255 host->mmc->pm_caps = MMC_PM_KEEP_POWER | MMC_PM_WAKE_SDIO_IRQ;
1256
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001257 ret = sdhci_add_host(host);
1258 if (ret)
Pierre Ossman44894282008-04-04 19:36:59 +02001259 goto remove;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001260
1261 return slot;
1262
Pierre Ossman44894282008-04-04 19:36:59 +02001263remove:
1264 if (chip->fixes && chip->fixes->remove_slot)
Pierre Ossman1e728592008-04-16 19:13:13 +02001265 chip->fixes->remove_slot(slot, 0);
Pierre Ossman44894282008-04-04 19:36:59 +02001266
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001267unmap:
1268 iounmap(host->ioaddr);
1269
1270release:
1271 pci_release_region(pdev, bar);
Dan Carpenterc60a32c2009-04-10 23:31:10 +02001272
1273free:
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001274 sdhci_free_host(host);
1275
1276 return ERR_PTR(ret);
1277}
1278
1279static void sdhci_pci_remove_slot(struct sdhci_pci_slot *slot)
1280{
Pierre Ossman1e728592008-04-16 19:13:13 +02001281 int dead;
1282 u32 scratch;
1283
1284 dead = 0;
1285 scratch = readl(slot->host->ioaddr + SDHCI_INT_STATUS);
1286 if (scratch == (u32)-1)
1287 dead = 1;
1288
1289 sdhci_remove_host(slot->host, dead);
Pierre Ossman44894282008-04-04 19:36:59 +02001290
1291 if (slot->chip->fixes && slot->chip->fixes->remove_slot)
Pierre Ossman1e728592008-04-16 19:13:13 +02001292 slot->chip->fixes->remove_slot(slot, dead);
Pierre Ossman44894282008-04-04 19:36:59 +02001293
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001294 pci_release_region(slot->chip->pdev, slot->pci_bar);
Pierre Ossman44894282008-04-04 19:36:59 +02001295
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001296 sdhci_free_host(slot->host);
1297}
1298
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001299static void __devinit sdhci_pci_runtime_pm_allow(struct device *dev)
1300{
1301 pm_runtime_put_noidle(dev);
1302 pm_runtime_allow(dev);
1303 pm_runtime_set_autosuspend_delay(dev, 50);
1304 pm_runtime_use_autosuspend(dev);
1305 pm_suspend_ignore_children(dev, 1);
1306}
1307
1308static void __devexit sdhci_pci_runtime_pm_forbid(struct device *dev)
1309{
1310 pm_runtime_forbid(dev);
1311 pm_runtime_get_noresume(dev);
1312}
1313
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001314static int __devinit sdhci_pci_probe(struct pci_dev *pdev,
1315 const struct pci_device_id *ent)
1316{
1317 struct sdhci_pci_chip *chip;
1318 struct sdhci_pci_slot *slot;
1319
Sergei Shtylyovcf5e23e2011-03-17 16:46:17 -04001320 u8 slots, first_bar;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001321 int ret, i;
1322
1323 BUG_ON(pdev == NULL);
1324 BUG_ON(ent == NULL);
1325
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001326 dev_info(&pdev->dev, "SDHCI controller found [%04x:%04x] (rev %x)\n",
Sergei Shtylyovcf5e23e2011-03-17 16:46:17 -04001327 (int)pdev->vendor, (int)pdev->device, (int)pdev->revision);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001328
1329 ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &slots);
1330 if (ret)
1331 return ret;
1332
1333 slots = PCI_SLOT_INFO_SLOTS(slots) + 1;
1334 dev_dbg(&pdev->dev, "found %d slot(s)\n", slots);
1335 if (slots == 0)
1336 return -ENODEV;
1337
1338 BUG_ON(slots > MAX_SLOTS);
1339
1340 ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &first_bar);
1341 if (ret)
1342 return ret;
1343
1344 first_bar &= PCI_SLOT_INFO_FIRST_BAR_MASK;
1345
1346 if (first_bar > 5) {
1347 dev_err(&pdev->dev, "Invalid first BAR. Aborting.\n");
1348 return -ENODEV;
1349 }
1350
1351 ret = pci_enable_device(pdev);
1352 if (ret)
1353 return ret;
1354
1355 chip = kzalloc(sizeof(struct sdhci_pci_chip), GFP_KERNEL);
1356 if (!chip) {
1357 ret = -ENOMEM;
1358 goto err;
1359 }
1360
1361 chip->pdev = pdev;
Ameya Palandeb177bc92011-04-05 21:13:13 +03001362 chip->fixes = (const struct sdhci_pci_fixes *)ent->driver_data;
Adrian Hunterc43fd772011-10-17 10:52:44 +03001363 if (chip->fixes) {
Pierre Ossman22606402008-03-23 19:33:23 +01001364 chip->quirks = chip->fixes->quirks;
Adrian Hunterc43fd772011-10-17 10:52:44 +03001365 chip->allow_runtime_pm = chip->fixes->allow_runtime_pm;
1366 }
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001367 chip->num_slots = slots;
1368
1369 pci_set_drvdata(pdev, chip);
1370
Pierre Ossman22606402008-03-23 19:33:23 +01001371 if (chip->fixes && chip->fixes->probe) {
1372 ret = chip->fixes->probe(chip);
1373 if (ret)
1374 goto free;
1375 }
1376
Alan Cox225d85f2010-10-04 15:24:21 +01001377 slots = chip->num_slots; /* Quirk may have changed this */
1378
Ameya Palandeb177bc92011-04-05 21:13:13 +03001379 for (i = 0; i < slots; i++) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001380 slot = sdhci_pci_probe_slot(pdev, chip, first_bar + i);
1381 if (IS_ERR(slot)) {
Ameya Palandeb177bc92011-04-05 21:13:13 +03001382 for (i--; i >= 0; i--)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001383 sdhci_pci_remove_slot(chip->slots[i]);
1384 ret = PTR_ERR(slot);
1385 goto free;
1386 }
1387
1388 chip->slots[i] = slot;
1389 }
1390
Adrian Hunterc43fd772011-10-17 10:52:44 +03001391 if (chip->allow_runtime_pm)
1392 sdhci_pci_runtime_pm_allow(&pdev->dev);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001393
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001394 return 0;
1395
1396free:
1397 pci_set_drvdata(pdev, NULL);
1398 kfree(chip);
1399
1400err:
1401 pci_disable_device(pdev);
1402 return ret;
1403}
1404
1405static void __devexit sdhci_pci_remove(struct pci_dev *pdev)
1406{
1407 int i;
1408 struct sdhci_pci_chip *chip;
1409
1410 chip = pci_get_drvdata(pdev);
1411
1412 if (chip) {
Adrian Hunterc43fd772011-10-17 10:52:44 +03001413 if (chip->allow_runtime_pm)
1414 sdhci_pci_runtime_pm_forbid(&pdev->dev);
1415
Ameya Palandeb177bc92011-04-05 21:13:13 +03001416 for (i = 0; i < chip->num_slots; i++)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001417 sdhci_pci_remove_slot(chip->slots[i]);
1418
1419 pci_set_drvdata(pdev, NULL);
1420 kfree(chip);
1421 }
1422
1423 pci_disable_device(pdev);
1424}
1425
1426static struct pci_driver sdhci_driver = {
Ameya Palandeb177bc92011-04-05 21:13:13 +03001427 .name = "sdhci-pci",
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001428 .id_table = pci_ids,
Ameya Palandeb177bc92011-04-05 21:13:13 +03001429 .probe = sdhci_pci_probe,
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001430 .remove = __devexit_p(sdhci_pci_remove),
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001431 .driver = {
1432 .pm = &sdhci_pci_pm_ops
1433 },
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001434};
1435
1436/*****************************************************************************\
1437 * *
1438 * Driver init/exit *
1439 * *
1440\*****************************************************************************/
1441
1442static int __init sdhci_drv_init(void)
1443{
1444 return pci_register_driver(&sdhci_driver);
1445}
1446
1447static void __exit sdhci_drv_exit(void)
1448{
1449 pci_unregister_driver(&sdhci_driver);
1450}
1451
1452module_init(sdhci_drv_init);
1453module_exit(sdhci_drv_exit);
1454
Pierre Ossman32710e82009-04-08 20:14:54 +02001455MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001456MODULE_DESCRIPTION("Secure Digital Host Controller Interface PCI driver");
1457MODULE_LICENSE("GPL");