blob: 4e8f324e2c846fc62170ee7d100f62c73eb341b9 [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>
Adrian Hunter52c506f2011-12-27 15:48:43 +020028#include <linux/mmc/sdhci-pci-data.h>
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010029
30#include "sdhci.h"
31
32/*
33 * PCI registers
34 */
35
36#define PCI_SDHCI_IFPIO 0x00
37#define PCI_SDHCI_IFDMA 0x01
38#define PCI_SDHCI_IFVENDOR 0x02
39
40#define PCI_SLOT_INFO 0x40 /* 8 bits */
41#define PCI_SLOT_INFO_SLOTS(x) ((x >> 4) & 7)
42#define PCI_SLOT_INFO_FIRST_BAR_MASK 0x07
43
44#define MAX_SLOTS 8
45
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010046struct sdhci_pci_chip;
Pierre Ossman44894282008-04-04 19:36:59 +020047struct sdhci_pci_slot;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010048
Pierre Ossman22606402008-03-23 19:33:23 +010049struct sdhci_pci_fixes {
50 unsigned int quirks;
Adrian Hunterc43fd772011-10-17 10:52:44 +030051 bool allow_runtime_pm;
Pierre Ossman22606402008-03-23 19:33:23 +010052
Ameya Palandeb177bc92011-04-05 21:13:13 +030053 int (*probe) (struct sdhci_pci_chip *);
Pierre Ossman45211e22008-03-24 13:09:09 +010054
Ameya Palandeb177bc92011-04-05 21:13:13 +030055 int (*probe_slot) (struct sdhci_pci_slot *);
56 void (*remove_slot) (struct sdhci_pci_slot *, int);
Pierre Ossman44894282008-04-04 19:36:59 +020057
Manuel Lauss29495aa2011-11-03 11:09:45 +010058 int (*suspend) (struct sdhci_pci_chip *);
Ameya Palandeb177bc92011-04-05 21:13:13 +030059 int (*resume) (struct sdhci_pci_chip *);
Pierre Ossman22606402008-03-23 19:33:23 +010060};
61
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010062struct sdhci_pci_slot {
63 struct sdhci_pci_chip *chip;
64 struct sdhci_host *host;
Adrian Hunter52c506f2011-12-27 15:48:43 +020065 struct sdhci_pci_data *data;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010066
67 int pci_bar;
Adrian Hunter0f201652011-08-29 16:42:13 +030068 int rst_n_gpio;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030069 int cd_gpio;
70 int cd_irq;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010071};
72
73struct sdhci_pci_chip {
74 struct pci_dev *pdev;
Pierre Ossman22606402008-03-23 19:33:23 +010075
76 unsigned int quirks;
Adrian Hunterc43fd772011-10-17 10:52:44 +030077 bool allow_runtime_pm;
Pierre Ossman22606402008-03-23 19:33:23 +010078 const struct sdhci_pci_fixes *fixes;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010079
80 int num_slots; /* Slots on controller */
81 struct sdhci_pci_slot *slots[MAX_SLOTS]; /* Pointers to host slots */
82};
83
Pierre Ossman22606402008-03-23 19:33:23 +010084
85/*****************************************************************************\
86 * *
87 * Hardware specific quirk handling *
88 * *
89\*****************************************************************************/
90
91static int ricoh_probe(struct sdhci_pci_chip *chip)
92{
Chris Ballc99436f2009-09-22 16:45:22 -070093 if (chip->pdev->subsystem_vendor == PCI_VENDOR_ID_SAMSUNG ||
94 chip->pdev->subsystem_vendor == PCI_VENDOR_ID_SONY)
Pierre Ossman22606402008-03-23 19:33:23 +010095 chip->quirks |= SDHCI_QUIRK_NO_CARD_NO_RESET;
Maxim Levitskyccc92c22010-08-10 18:01:42 -070096 return 0;
97}
Pierre Ossman22606402008-03-23 19:33:23 +010098
Maxim Levitskyccc92c22010-08-10 18:01:42 -070099static int ricoh_mmc_probe_slot(struct sdhci_pci_slot *slot)
100{
101 slot->host->caps =
102 ((0x21 << SDHCI_TIMEOUT_CLK_SHIFT)
103 & SDHCI_TIMEOUT_CLK_MASK) |
104
105 ((0x21 << SDHCI_CLOCK_BASE_SHIFT)
106 & SDHCI_CLOCK_BASE_MASK) |
107
108 SDHCI_TIMEOUT_CLK_UNIT |
109 SDHCI_CAN_VDD_330 |
110 SDHCI_CAN_DO_SDMA;
111 return 0;
112}
113
114static int ricoh_mmc_resume(struct sdhci_pci_chip *chip)
115{
116 /* Apply a delay to allow controller to settle */
117 /* Otherwise it becomes confused if card state changed
118 during suspend */
119 msleep(500);
Pierre Ossman22606402008-03-23 19:33:23 +0100120 return 0;
121}
122
123static const struct sdhci_pci_fixes sdhci_ricoh = {
124 .probe = ricoh_probe,
Vasily Khoruzhick84938292010-03-05 13:43:46 -0800125 .quirks = SDHCI_QUIRK_32BIT_DMA_ADDR |
126 SDHCI_QUIRK_FORCE_DMA |
127 SDHCI_QUIRK_CLOCK_BEFORE_RESET,
Pierre Ossman22606402008-03-23 19:33:23 +0100128};
129
Maxim Levitskyccc92c22010-08-10 18:01:42 -0700130static const struct sdhci_pci_fixes sdhci_ricoh_mmc = {
131 .probe_slot = ricoh_mmc_probe_slot,
132 .resume = ricoh_mmc_resume,
133 .quirks = SDHCI_QUIRK_32BIT_DMA_ADDR |
134 SDHCI_QUIRK_CLOCK_BEFORE_RESET |
135 SDHCI_QUIRK_NO_CARD_NO_RESET |
136 SDHCI_QUIRK_MISSING_CAPS
137};
138
Pierre Ossman22606402008-03-23 19:33:23 +0100139static const struct sdhci_pci_fixes sdhci_ene_712 = {
140 .quirks = SDHCI_QUIRK_SINGLE_POWER_WRITE |
141 SDHCI_QUIRK_BROKEN_DMA,
142};
143
144static const struct sdhci_pci_fixes sdhci_ene_714 = {
145 .quirks = SDHCI_QUIRK_SINGLE_POWER_WRITE |
146 SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS |
147 SDHCI_QUIRK_BROKEN_DMA,
148};
149
150static const struct sdhci_pci_fixes sdhci_cafe = {
151 .quirks = SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER |
Andres Salomona0874892009-03-02 21:48:20 +0100152 SDHCI_QUIRK_NO_BUSY_IRQ |
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200153 SDHCI_QUIRK_BROKEN_TIMEOUT_VAL,
Pierre Ossman22606402008-03-23 19:33:23 +0100154};
155
Major Lee68077b02011-06-29 14:23:46 +0300156static int mrst_hc_probe_slot(struct sdhci_pci_slot *slot)
157{
158 slot->host->mmc->caps |= MMC_CAP_8_BIT_DATA;
159 return 0;
160}
161
Alan Coxf9ee3ea2010-10-04 15:25:11 +0100162/*
163 * ADMA operation is disabled for Moorestown platform due to
164 * hardware bugs.
165 */
Jacob Pan35ac6f02010-11-09 13:57:29 +0000166static int mrst_hc_probe(struct sdhci_pci_chip *chip)
Alan Coxf9ee3ea2010-10-04 15:25:11 +0100167{
168 /*
Jacob Pan35ac6f02010-11-09 13:57:29 +0000169 * slots number is fixed here for MRST as SDIO3/5 are never used and
170 * have hardware bugs.
Alan Coxf9ee3ea2010-10-04 15:25:11 +0100171 */
172 chip->num_slots = 1;
173 return 0;
174}
175
Adrian Hunter0f201652011-08-29 16:42:13 +0300176/* Medfield eMMC hardware reset GPIOs */
177static int mfd_emmc0_rst_gpio = -EINVAL;
178static int mfd_emmc1_rst_gpio = -EINVAL;
179
180static int mfd_emmc_gpio_parse(struct sfi_table_header *table)
181{
182 struct sfi_table_simple *sb = (struct sfi_table_simple *)table;
183 struct sfi_gpio_table_entry *entry;
184 int i, num;
185
186 num = SFI_GET_NUM_ENTRIES(sb, struct sfi_gpio_table_entry);
187 entry = (struct sfi_gpio_table_entry *)sb->pentry;
188
189 for (i = 0; i < num; i++, entry++) {
190 if (!strncmp(entry->pin_name, "emmc0_rst", SFI_NAME_LEN))
191 mfd_emmc0_rst_gpio = entry->pin_no;
192 else if (!strncmp(entry->pin_name, "emmc1_rst", SFI_NAME_LEN))
193 mfd_emmc1_rst_gpio = entry->pin_no;
194 }
195
196 return 0;
197}
198
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300199#ifdef CONFIG_PM_RUNTIME
200
201static irqreturn_t mfd_sd_cd(int irq, void *dev_id)
202{
203 struct sdhci_pci_slot *slot = dev_id;
204 struct sdhci_host *host = slot->host;
205
206 mmc_detect_change(host->mmc, msecs_to_jiffies(200));
207 return IRQ_HANDLED;
208}
209
210#define MFLD_SD_CD_PIN 69
211
212static int mfd_sd_probe_slot(struct sdhci_pci_slot *slot)
213{
214 int err, irq, gpio = MFLD_SD_CD_PIN;
215
216 slot->cd_gpio = -EINVAL;
217 slot->cd_irq = -EINVAL;
218
219 err = gpio_request(gpio, "sd_cd");
220 if (err < 0)
221 goto out;
222
223 err = gpio_direction_input(gpio);
224 if (err < 0)
225 goto out_free;
226
227 irq = gpio_to_irq(gpio);
228 if (irq < 0)
229 goto out_free;
230
231 err = request_irq(irq, mfd_sd_cd, IRQF_TRIGGER_RISING |
232 IRQF_TRIGGER_FALLING, "sd_cd", slot);
233 if (err)
234 goto out_free;
235
236 slot->cd_gpio = gpio;
237 slot->cd_irq = irq;
238 slot->host->quirks2 |= SDHCI_QUIRK2_OWN_CARD_DETECTION;
239
240 return 0;
241
242out_free:
243 gpio_free(gpio);
244out:
245 dev_warn(&slot->chip->pdev->dev, "failed to setup card detect wake up\n");
246 return 0;
247}
248
249static void mfd_sd_remove_slot(struct sdhci_pci_slot *slot, int dead)
250{
251 if (slot->cd_irq >= 0)
252 free_irq(slot->cd_irq, slot);
253 gpio_free(slot->cd_gpio);
254}
255
256#else
257
258#define mfd_sd_probe_slot NULL
259#define mfd_sd_remove_slot NULL
260
261#endif
262
Adrian Hunter0d013bc2011-06-29 14:23:47 +0300263static int mfd_emmc_probe_slot(struct sdhci_pci_slot *slot)
264{
Adrian Hunter0f201652011-08-29 16:42:13 +0300265 const char *name = NULL;
266 int gpio = -EINVAL;
267
268 sfi_table_parse(SFI_SIG_GPIO, NULL, NULL, mfd_emmc_gpio_parse);
269
270 switch (slot->chip->pdev->device) {
271 case PCI_DEVICE_ID_INTEL_MFD_EMMC0:
272 gpio = mfd_emmc0_rst_gpio;
273 name = "eMMC0_reset";
274 break;
275 case PCI_DEVICE_ID_INTEL_MFD_EMMC1:
276 gpio = mfd_emmc1_rst_gpio;
277 name = "eMMC1_reset";
278 break;
279 }
280
281 if (!gpio_request(gpio, name)) {
282 gpio_direction_output(gpio, 1);
283 slot->rst_n_gpio = gpio;
284 slot->host->mmc->caps |= MMC_CAP_HW_RESET;
285 }
286
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300287 slot->host->mmc->caps |= MMC_CAP_8_BIT_DATA | MMC_CAP_NONREMOVABLE;
Adrian Hunter0f201652011-08-29 16:42:13 +0300288
Adrian Hunterf7c56ef2011-09-23 12:48:21 +0300289 slot->host->mmc->caps2 = MMC_CAP2_BOOTPART_NOACC;
290
Adrian Hunter0d013bc2011-06-29 14:23:47 +0300291 return 0;
292}
293
Adrian Hunter0f201652011-08-29 16:42:13 +0300294static void mfd_emmc_remove_slot(struct sdhci_pci_slot *slot, int dead)
295{
296 gpio_free(slot->rst_n_gpio);
297}
298
Alan Coxf9ee3ea2010-10-04 15:25:11 +0100299static const struct sdhci_pci_fixes sdhci_intel_mrst_hc0 = {
300 .quirks = SDHCI_QUIRK_BROKEN_ADMA | SDHCI_QUIRK_NO_HISPD_BIT,
Major Lee68077b02011-06-29 14:23:46 +0300301 .probe_slot = mrst_hc_probe_slot,
Alan Coxf9ee3ea2010-10-04 15:25:11 +0100302};
303
Jacob Pan35ac6f02010-11-09 13:57:29 +0000304static const struct sdhci_pci_fixes sdhci_intel_mrst_hc1_hc2 = {
Alan Coxf9ee3ea2010-10-04 15:25:11 +0100305 .quirks = SDHCI_QUIRK_BROKEN_ADMA | SDHCI_QUIRK_NO_HISPD_BIT,
Jacob Pan35ac6f02010-11-09 13:57:29 +0000306 .probe = mrst_hc_probe,
Alan Coxf9ee3ea2010-10-04 15:25:11 +0100307};
308
Xiaochen Shen29229052010-10-04 15:24:52 +0100309static const struct sdhci_pci_fixes sdhci_intel_mfd_sd = {
310 .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
Adrian Hunterc43fd772011-10-17 10:52:44 +0300311 .allow_runtime_pm = true,
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300312 .probe_slot = mfd_sd_probe_slot,
313 .remove_slot = mfd_sd_remove_slot,
Xiaochen Shen29229052010-10-04 15:24:52 +0100314};
315
Adrian Hunter0d013bc2011-06-29 14:23:47 +0300316static const struct sdhci_pci_fixes sdhci_intel_mfd_sdio = {
Xiaochen Shen29229052010-10-04 15:24:52 +0100317 .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
Adrian Hunterc43fd772011-10-17 10:52:44 +0300318 .allow_runtime_pm = true,
Xiaochen Shen29229052010-10-04 15:24:52 +0100319};
320
Adrian Hunter0d013bc2011-06-29 14:23:47 +0300321static const struct sdhci_pci_fixes sdhci_intel_mfd_emmc = {
322 .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
Adrian Hunterc43fd772011-10-17 10:52:44 +0300323 .allow_runtime_pm = true,
Adrian Hunter0d013bc2011-06-29 14:23:47 +0300324 .probe_slot = mfd_emmc_probe_slot,
Adrian Hunter0f201652011-08-29 16:42:13 +0300325 .remove_slot = mfd_emmc_remove_slot,
Adrian Hunter0d013bc2011-06-29 14:23:47 +0300326};
327
Jennifer Li26daa1e2010-11-17 23:01:59 -0500328/* O2Micro extra registers */
329#define O2_SD_LOCK_WP 0xD3
330#define O2_SD_MULTI_VCC3V 0xEE
331#define O2_SD_CLKREQ 0xEC
332#define O2_SD_CAPS 0xE0
333#define O2_SD_ADMA1 0xE2
334#define O2_SD_ADMA2 0xE7
335#define O2_SD_INF_MOD 0xF1
336
337static int o2_probe(struct sdhci_pci_chip *chip)
338{
339 int ret;
340 u8 scratch;
341
342 switch (chip->pdev->device) {
343 case PCI_DEVICE_ID_O2_8220:
344 case PCI_DEVICE_ID_O2_8221:
345 case PCI_DEVICE_ID_O2_8320:
346 case PCI_DEVICE_ID_O2_8321:
347 /* This extra setup is required due to broken ADMA. */
348 ret = pci_read_config_byte(chip->pdev, O2_SD_LOCK_WP, &scratch);
349 if (ret)
350 return ret;
351 scratch &= 0x7f;
352 pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch);
353
354 /* Set Multi 3 to VCC3V# */
355 pci_write_config_byte(chip->pdev, O2_SD_MULTI_VCC3V, 0x08);
356
357 /* Disable CLK_REQ# support after media DET */
358 ret = pci_read_config_byte(chip->pdev, O2_SD_CLKREQ, &scratch);
359 if (ret)
360 return ret;
361 scratch |= 0x20;
362 pci_write_config_byte(chip->pdev, O2_SD_CLKREQ, scratch);
363
364 /* Choose capabilities, enable SDMA. We have to write 0x01
365 * to the capabilities register first to unlock it.
366 */
367 ret = pci_read_config_byte(chip->pdev, O2_SD_CAPS, &scratch);
368 if (ret)
369 return ret;
370 scratch |= 0x01;
371 pci_write_config_byte(chip->pdev, O2_SD_CAPS, scratch);
372 pci_write_config_byte(chip->pdev, O2_SD_CAPS, 0x73);
373
374 /* Disable ADMA1/2 */
375 pci_write_config_byte(chip->pdev, O2_SD_ADMA1, 0x39);
376 pci_write_config_byte(chip->pdev, O2_SD_ADMA2, 0x08);
377
378 /* Disable the infinite transfer mode */
379 ret = pci_read_config_byte(chip->pdev, O2_SD_INF_MOD, &scratch);
380 if (ret)
381 return ret;
382 scratch |= 0x08;
383 pci_write_config_byte(chip->pdev, O2_SD_INF_MOD, scratch);
384
385 /* Lock WP */
386 ret = pci_read_config_byte(chip->pdev, O2_SD_LOCK_WP, &scratch);
387 if (ret)
388 return ret;
389 scratch |= 0x80;
390 pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch);
391 }
392
393 return 0;
394}
395
Pierre Ossman45211e22008-03-24 13:09:09 +0100396static int jmicron_pmos(struct sdhci_pci_chip *chip, int on)
397{
398 u8 scratch;
399 int ret;
400
401 ret = pci_read_config_byte(chip->pdev, 0xAE, &scratch);
402 if (ret)
403 return ret;
404
405 /*
406 * Turn PMOS on [bit 0], set over current detection to 2.4 V
407 * [bit 1:2] and enable over current debouncing [bit 6].
408 */
409 if (on)
410 scratch |= 0x47;
411 else
412 scratch &= ~0x47;
413
414 ret = pci_write_config_byte(chip->pdev, 0xAE, scratch);
415 if (ret)
416 return ret;
417
418 return 0;
419}
420
421static int jmicron_probe(struct sdhci_pci_chip *chip)
422{
423 int ret;
Takashi Iwai8f230f42010-12-08 10:04:30 +0100424 u16 mmcdev = 0;
Pierre Ossman45211e22008-03-24 13:09:09 +0100425
Pierre Ossman93fc48c2008-06-28 18:21:41 +0200426 if (chip->pdev->revision == 0) {
427 chip->quirks |= SDHCI_QUIRK_32BIT_DMA_ADDR |
428 SDHCI_QUIRK_32BIT_DMA_SIZE |
Pierre Ossman2134a922008-06-28 18:28:51 +0200429 SDHCI_QUIRK_32BIT_ADMA_SIZE |
Pierre Ossman4a3cba32008-07-29 00:11:16 +0200430 SDHCI_QUIRK_RESET_AFTER_REQUEST |
Pierre Ossman86a6a872009-02-02 21:13:49 +0100431 SDHCI_QUIRK_BROKEN_SMALL_PIO;
Pierre Ossman93fc48c2008-06-28 18:21:41 +0200432 }
433
Pierre Ossman45211e22008-03-24 13:09:09 +0100434 /*
Pierre Ossman44894282008-04-04 19:36:59 +0200435 * JMicron chips can have two interfaces to the same hardware
436 * in order to work around limitations in Microsoft's driver.
437 * We need to make sure we only bind to one of them.
438 *
439 * This code assumes two things:
440 *
441 * 1. The PCI code adds subfunctions in order.
442 *
443 * 2. The MMC interface has a lower subfunction number
444 * than the SD interface.
445 */
Takashi Iwai8f230f42010-12-08 10:04:30 +0100446 if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_SD)
447 mmcdev = PCI_DEVICE_ID_JMICRON_JMB38X_MMC;
448 else if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_SD)
449 mmcdev = PCI_DEVICE_ID_JMICRON_JMB388_ESD;
450
451 if (mmcdev) {
Pierre Ossman44894282008-04-04 19:36:59 +0200452 struct pci_dev *sd_dev;
453
454 sd_dev = NULL;
455 while ((sd_dev = pci_get_device(PCI_VENDOR_ID_JMICRON,
Takashi Iwai8f230f42010-12-08 10:04:30 +0100456 mmcdev, sd_dev)) != NULL) {
Pierre Ossman44894282008-04-04 19:36:59 +0200457 if ((PCI_SLOT(chip->pdev->devfn) ==
458 PCI_SLOT(sd_dev->devfn)) &&
459 (chip->pdev->bus == sd_dev->bus))
460 break;
461 }
462
463 if (sd_dev) {
464 pci_dev_put(sd_dev);
465 dev_info(&chip->pdev->dev, "Refusing to bind to "
466 "secondary interface.\n");
467 return -ENODEV;
468 }
469 }
470
471 /*
Pierre Ossman45211e22008-03-24 13:09:09 +0100472 * JMicron chips need a bit of a nudge to enable the power
473 * output pins.
474 */
475 ret = jmicron_pmos(chip, 1);
476 if (ret) {
477 dev_err(&chip->pdev->dev, "Failure enabling card power\n");
478 return ret;
479 }
480
Takashi Iwai82b0e232011-04-21 20:26:38 +0200481 /* quirk for unsable RO-detection on JM388 chips */
482 if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_SD ||
483 chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD)
484 chip->quirks |= SDHCI_QUIRK_UNSTABLE_RO_DETECT;
485
Pierre Ossman45211e22008-03-24 13:09:09 +0100486 return 0;
487}
488
Pierre Ossman44894282008-04-04 19:36:59 +0200489static void jmicron_enable_mmc(struct sdhci_host *host, int on)
490{
491 u8 scratch;
492
493 scratch = readb(host->ioaddr + 0xC0);
494
495 if (on)
496 scratch |= 0x01;
497 else
498 scratch &= ~0x01;
499
500 writeb(scratch, host->ioaddr + 0xC0);
501}
502
503static int jmicron_probe_slot(struct sdhci_pci_slot *slot)
504{
Pierre Ossman2134a922008-06-28 18:28:51 +0200505 if (slot->chip->pdev->revision == 0) {
506 u16 version;
507
508 version = readl(slot->host->ioaddr + SDHCI_HOST_VERSION);
509 version = (version & SDHCI_VENDOR_VER_MASK) >>
510 SDHCI_VENDOR_VER_SHIFT;
511
512 /*
513 * Older versions of the chip have lots of nasty glitches
514 * in the ADMA engine. It's best just to avoid it
515 * completely.
516 */
517 if (version < 0xAC)
518 slot->host->quirks |= SDHCI_QUIRK_BROKEN_ADMA;
519 }
520
Takashi Iwai8f230f42010-12-08 10:04:30 +0100521 /* JM388 MMC doesn't support 1.8V while SD supports it */
522 if (slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD) {
523 slot->host->ocr_avail_sd = MMC_VDD_32_33 | MMC_VDD_33_34 |
524 MMC_VDD_29_30 | MMC_VDD_30_31 |
525 MMC_VDD_165_195; /* allow 1.8V */
526 slot->host->ocr_avail_mmc = MMC_VDD_32_33 | MMC_VDD_33_34 |
527 MMC_VDD_29_30 | MMC_VDD_30_31; /* no 1.8V for MMC */
528 }
529
Pierre Ossman44894282008-04-04 19:36:59 +0200530 /*
531 * The secondary interface requires a bit set to get the
532 * interrupts.
533 */
Takashi Iwai8f230f42010-12-08 10:04:30 +0100534 if (slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC ||
535 slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD)
Pierre Ossman44894282008-04-04 19:36:59 +0200536 jmicron_enable_mmc(slot->host, 1);
537
Takashi Iwaid75c1082010-12-16 17:54:14 +0100538 slot->host->mmc->caps |= MMC_CAP_BUS_WIDTH_TEST;
539
Pierre Ossman44894282008-04-04 19:36:59 +0200540 return 0;
541}
542
Pierre Ossman1e728592008-04-16 19:13:13 +0200543static void jmicron_remove_slot(struct sdhci_pci_slot *slot, int dead)
Pierre Ossman44894282008-04-04 19:36:59 +0200544{
Pierre Ossman1e728592008-04-16 19:13:13 +0200545 if (dead)
546 return;
547
Takashi Iwai8f230f42010-12-08 10:04:30 +0100548 if (slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC ||
549 slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD)
Pierre Ossman44894282008-04-04 19:36:59 +0200550 jmicron_enable_mmc(slot->host, 0);
551}
552
Manuel Lauss29495aa2011-11-03 11:09:45 +0100553static int jmicron_suspend(struct sdhci_pci_chip *chip)
Pierre Ossman44894282008-04-04 19:36:59 +0200554{
555 int i;
556
Takashi Iwai8f230f42010-12-08 10:04:30 +0100557 if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC ||
558 chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD) {
Ameya Palandeb177bc92011-04-05 21:13:13 +0300559 for (i = 0; i < chip->num_slots; i++)
Pierre Ossman44894282008-04-04 19:36:59 +0200560 jmicron_enable_mmc(chip->slots[i]->host, 0);
561 }
562
563 return 0;
564}
565
Pierre Ossman45211e22008-03-24 13:09:09 +0100566static int jmicron_resume(struct sdhci_pci_chip *chip)
567{
Pierre Ossman44894282008-04-04 19:36:59 +0200568 int ret, i;
569
Takashi Iwai8f230f42010-12-08 10:04:30 +0100570 if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC ||
571 chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD) {
Ameya Palandeb177bc92011-04-05 21:13:13 +0300572 for (i = 0; i < chip->num_slots; i++)
Pierre Ossman44894282008-04-04 19:36:59 +0200573 jmicron_enable_mmc(chip->slots[i]->host, 1);
574 }
Pierre Ossman45211e22008-03-24 13:09:09 +0100575
576 ret = jmicron_pmos(chip, 1);
577 if (ret) {
578 dev_err(&chip->pdev->dev, "Failure enabling card power\n");
579 return ret;
580 }
581
582 return 0;
583}
584
Jennifer Li26daa1e2010-11-17 23:01:59 -0500585static const struct sdhci_pci_fixes sdhci_o2 = {
586 .probe = o2_probe,
587};
588
Pierre Ossman22606402008-03-23 19:33:23 +0100589static const struct sdhci_pci_fixes sdhci_jmicron = {
Pierre Ossman45211e22008-03-24 13:09:09 +0100590 .probe = jmicron_probe,
591
Pierre Ossman44894282008-04-04 19:36:59 +0200592 .probe_slot = jmicron_probe_slot,
593 .remove_slot = jmicron_remove_slot,
594
595 .suspend = jmicron_suspend,
Pierre Ossman45211e22008-03-24 13:09:09 +0100596 .resume = jmicron_resume,
Pierre Ossman22606402008-03-23 19:33:23 +0100597};
598
Nicolas Pitrea7a61862009-12-14 18:01:26 -0800599/* SysKonnect CardBus2SDIO extra registers */
600#define SYSKT_CTRL 0x200
601#define SYSKT_RDFIFO_STAT 0x204
602#define SYSKT_WRFIFO_STAT 0x208
603#define SYSKT_POWER_DATA 0x20c
604#define SYSKT_POWER_330 0xef
605#define SYSKT_POWER_300 0xf8
606#define SYSKT_POWER_184 0xcc
607#define SYSKT_POWER_CMD 0x20d
608#define SYSKT_POWER_START (1 << 7)
609#define SYSKT_POWER_STATUS 0x20e
610#define SYSKT_POWER_STATUS_OK (1 << 0)
611#define SYSKT_BOARD_REV 0x210
612#define SYSKT_CHIP_REV 0x211
613#define SYSKT_CONF_DATA 0x212
614#define SYSKT_CONF_DATA_1V8 (1 << 2)
615#define SYSKT_CONF_DATA_2V5 (1 << 1)
616#define SYSKT_CONF_DATA_3V3 (1 << 0)
617
618static int syskt_probe(struct sdhci_pci_chip *chip)
619{
620 if ((chip->pdev->class & 0x0000FF) == PCI_SDHCI_IFVENDOR) {
621 chip->pdev->class &= ~0x0000FF;
622 chip->pdev->class |= PCI_SDHCI_IFDMA;
623 }
624 return 0;
625}
626
627static int syskt_probe_slot(struct sdhci_pci_slot *slot)
628{
629 int tm, ps;
630
631 u8 board_rev = readb(slot->host->ioaddr + SYSKT_BOARD_REV);
632 u8 chip_rev = readb(slot->host->ioaddr + SYSKT_CHIP_REV);
633 dev_info(&slot->chip->pdev->dev, "SysKonnect CardBus2SDIO, "
634 "board rev %d.%d, chip rev %d.%d\n",
635 board_rev >> 4, board_rev & 0xf,
636 chip_rev >> 4, chip_rev & 0xf);
637 if (chip_rev >= 0x20)
638 slot->host->quirks |= SDHCI_QUIRK_FORCE_DMA;
639
640 writeb(SYSKT_POWER_330, slot->host->ioaddr + SYSKT_POWER_DATA);
641 writeb(SYSKT_POWER_START, slot->host->ioaddr + SYSKT_POWER_CMD);
642 udelay(50);
643 tm = 10; /* Wait max 1 ms */
644 do {
645 ps = readw(slot->host->ioaddr + SYSKT_POWER_STATUS);
646 if (ps & SYSKT_POWER_STATUS_OK)
647 break;
648 udelay(100);
649 } while (--tm);
650 if (!tm) {
651 dev_err(&slot->chip->pdev->dev,
652 "power regulator never stabilized");
653 writeb(0, slot->host->ioaddr + SYSKT_POWER_CMD);
654 return -ENODEV;
655 }
656
657 return 0;
658}
659
660static const struct sdhci_pci_fixes sdhci_syskt = {
661 .quirks = SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER,
662 .probe = syskt_probe,
663 .probe_slot = syskt_probe_slot,
664};
665
Harald Welte557b0692009-06-18 16:53:38 +0200666static int via_probe(struct sdhci_pci_chip *chip)
667{
668 if (chip->pdev->revision == 0x10)
669 chip->quirks |= SDHCI_QUIRK_DELAY_AFTER_POWER;
670
671 return 0;
672}
673
674static const struct sdhci_pci_fixes sdhci_via = {
675 .probe = via_probe,
676};
677
Pierre Ossman22606402008-03-23 19:33:23 +0100678static const struct pci_device_id pci_ids[] __devinitdata = {
679 {
680 .vendor = PCI_VENDOR_ID_RICOH,
681 .device = PCI_DEVICE_ID_RICOH_R5C822,
682 .subvendor = PCI_ANY_ID,
683 .subdevice = PCI_ANY_ID,
684 .driver_data = (kernel_ulong_t)&sdhci_ricoh,
685 },
686
687 {
Maxim Levitskyccc92c22010-08-10 18:01:42 -0700688 .vendor = PCI_VENDOR_ID_RICOH,
689 .device = 0x843,
690 .subvendor = PCI_ANY_ID,
691 .subdevice = PCI_ANY_ID,
692 .driver_data = (kernel_ulong_t)&sdhci_ricoh_mmc,
693 },
694
695 {
Pablo Castillo568133e2010-08-10 18:02:01 -0700696 .vendor = PCI_VENDOR_ID_RICOH,
697 .device = 0xe822,
698 .subvendor = PCI_ANY_ID,
699 .subdevice = PCI_ANY_ID,
700 .driver_data = (kernel_ulong_t)&sdhci_ricoh_mmc,
701 },
702
703 {
Manoj Iyer5fd11c02011-02-11 16:25:31 -0600704 .vendor = PCI_VENDOR_ID_RICOH,
705 .device = 0xe823,
706 .subvendor = PCI_ANY_ID,
707 .subdevice = PCI_ANY_ID,
708 .driver_data = (kernel_ulong_t)&sdhci_ricoh_mmc,
709 },
710
711 {
Pierre Ossman22606402008-03-23 19:33:23 +0100712 .vendor = PCI_VENDOR_ID_ENE,
713 .device = PCI_DEVICE_ID_ENE_CB712_SD,
714 .subvendor = PCI_ANY_ID,
715 .subdevice = PCI_ANY_ID,
716 .driver_data = (kernel_ulong_t)&sdhci_ene_712,
717 },
718
719 {
720 .vendor = PCI_VENDOR_ID_ENE,
721 .device = PCI_DEVICE_ID_ENE_CB712_SD_2,
722 .subvendor = PCI_ANY_ID,
723 .subdevice = PCI_ANY_ID,
724 .driver_data = (kernel_ulong_t)&sdhci_ene_712,
725 },
726
727 {
728 .vendor = PCI_VENDOR_ID_ENE,
729 .device = PCI_DEVICE_ID_ENE_CB714_SD,
730 .subvendor = PCI_ANY_ID,
731 .subdevice = PCI_ANY_ID,
732 .driver_data = (kernel_ulong_t)&sdhci_ene_714,
733 },
734
735 {
736 .vendor = PCI_VENDOR_ID_ENE,
737 .device = PCI_DEVICE_ID_ENE_CB714_SD_2,
738 .subvendor = PCI_ANY_ID,
739 .subdevice = PCI_ANY_ID,
740 .driver_data = (kernel_ulong_t)&sdhci_ene_714,
741 },
742
743 {
744 .vendor = PCI_VENDOR_ID_MARVELL,
David Woodhouse8c5eb882008-09-03 09:45:57 +0100745 .device = PCI_DEVICE_ID_MARVELL_88ALP01_SD,
Pierre Ossman22606402008-03-23 19:33:23 +0100746 .subvendor = PCI_ANY_ID,
747 .subdevice = PCI_ANY_ID,
748 .driver_data = (kernel_ulong_t)&sdhci_cafe,
749 },
750
751 {
752 .vendor = PCI_VENDOR_ID_JMICRON,
753 .device = PCI_DEVICE_ID_JMICRON_JMB38X_SD,
754 .subvendor = PCI_ANY_ID,
755 .subdevice = PCI_ANY_ID,
756 .driver_data = (kernel_ulong_t)&sdhci_jmicron,
757 },
758
Pierre Ossman44894282008-04-04 19:36:59 +0200759 {
760 .vendor = PCI_VENDOR_ID_JMICRON,
761 .device = PCI_DEVICE_ID_JMICRON_JMB38X_MMC,
762 .subvendor = PCI_ANY_ID,
763 .subdevice = PCI_ANY_ID,
764 .driver_data = (kernel_ulong_t)&sdhci_jmicron,
765 },
766
Harald Welte557b0692009-06-18 16:53:38 +0200767 {
Takashi Iwai8f230f42010-12-08 10:04:30 +0100768 .vendor = PCI_VENDOR_ID_JMICRON,
769 .device = PCI_DEVICE_ID_JMICRON_JMB388_SD,
770 .subvendor = PCI_ANY_ID,
771 .subdevice = PCI_ANY_ID,
772 .driver_data = (kernel_ulong_t)&sdhci_jmicron,
773 },
774
775 {
776 .vendor = PCI_VENDOR_ID_JMICRON,
777 .device = PCI_DEVICE_ID_JMICRON_JMB388_ESD,
778 .subvendor = PCI_ANY_ID,
779 .subdevice = PCI_ANY_ID,
780 .driver_data = (kernel_ulong_t)&sdhci_jmicron,
781 },
782
783 {
Nicolas Pitrea7a61862009-12-14 18:01:26 -0800784 .vendor = PCI_VENDOR_ID_SYSKONNECT,
785 .device = 0x8000,
786 .subvendor = PCI_ANY_ID,
787 .subdevice = PCI_ANY_ID,
788 .driver_data = (kernel_ulong_t)&sdhci_syskt,
789 },
790
791 {
Harald Welte557b0692009-06-18 16:53:38 +0200792 .vendor = PCI_VENDOR_ID_VIA,
793 .device = 0x95d0,
794 .subvendor = PCI_ANY_ID,
795 .subdevice = PCI_ANY_ID,
796 .driver_data = (kernel_ulong_t)&sdhci_via,
797 },
798
Xiaochen Shen29229052010-10-04 15:24:52 +0100799 {
800 .vendor = PCI_VENDOR_ID_INTEL,
Alan Coxf9ee3ea2010-10-04 15:25:11 +0100801 .device = PCI_DEVICE_ID_INTEL_MRST_SD0,
802 .subvendor = PCI_ANY_ID,
803 .subdevice = PCI_ANY_ID,
804 .driver_data = (kernel_ulong_t)&sdhci_intel_mrst_hc0,
805 },
806
807 {
808 .vendor = PCI_VENDOR_ID_INTEL,
809 .device = PCI_DEVICE_ID_INTEL_MRST_SD1,
810 .subvendor = PCI_ANY_ID,
811 .subdevice = PCI_ANY_ID,
Jacob Pan35ac6f02010-11-09 13:57:29 +0000812 .driver_data = (kernel_ulong_t)&sdhci_intel_mrst_hc1_hc2,
813 },
814
815 {
816 .vendor = PCI_VENDOR_ID_INTEL,
817 .device = PCI_DEVICE_ID_INTEL_MRST_SD2,
818 .subvendor = PCI_ANY_ID,
819 .subdevice = PCI_ANY_ID,
820 .driver_data = (kernel_ulong_t)&sdhci_intel_mrst_hc1_hc2,
Alan Coxf9ee3ea2010-10-04 15:25:11 +0100821 },
822
823 {
824 .vendor = PCI_VENDOR_ID_INTEL,
Xiaochen Shen29229052010-10-04 15:24:52 +0100825 .device = PCI_DEVICE_ID_INTEL_MFD_SD,
826 .subvendor = PCI_ANY_ID,
827 .subdevice = PCI_ANY_ID,
828 .driver_data = (kernel_ulong_t)&sdhci_intel_mfd_sd,
829 },
830
831 {
832 .vendor = PCI_VENDOR_ID_INTEL,
833 .device = PCI_DEVICE_ID_INTEL_MFD_SDIO1,
834 .subvendor = PCI_ANY_ID,
835 .subdevice = PCI_ANY_ID,
Adrian Hunter0d013bc2011-06-29 14:23:47 +0300836 .driver_data = (kernel_ulong_t)&sdhci_intel_mfd_sdio,
Xiaochen Shen29229052010-10-04 15:24:52 +0100837 },
838
839 {
840 .vendor = PCI_VENDOR_ID_INTEL,
841 .device = PCI_DEVICE_ID_INTEL_MFD_SDIO2,
842 .subvendor = PCI_ANY_ID,
843 .subdevice = PCI_ANY_ID,
Adrian Hunter0d013bc2011-06-29 14:23:47 +0300844 .driver_data = (kernel_ulong_t)&sdhci_intel_mfd_sdio,
Xiaochen Shen29229052010-10-04 15:24:52 +0100845 },
846
847 {
848 .vendor = PCI_VENDOR_ID_INTEL,
849 .device = PCI_DEVICE_ID_INTEL_MFD_EMMC0,
850 .subvendor = PCI_ANY_ID,
851 .subdevice = PCI_ANY_ID,
Adrian Hunter0d013bc2011-06-29 14:23:47 +0300852 .driver_data = (kernel_ulong_t)&sdhci_intel_mfd_emmc,
Xiaochen Shen29229052010-10-04 15:24:52 +0100853 },
854
855 {
856 .vendor = PCI_VENDOR_ID_INTEL,
857 .device = PCI_DEVICE_ID_INTEL_MFD_EMMC1,
858 .subvendor = PCI_ANY_ID,
859 .subdevice = PCI_ANY_ID,
Adrian Hunter0d013bc2011-06-29 14:23:47 +0300860 .driver_data = (kernel_ulong_t)&sdhci_intel_mfd_emmc,
Xiaochen Shen29229052010-10-04 15:24:52 +0100861 },
862
Jennifer Li26daa1e2010-11-17 23:01:59 -0500863 {
864 .vendor = PCI_VENDOR_ID_O2,
865 .device = PCI_DEVICE_ID_O2_8120,
866 .subvendor = PCI_ANY_ID,
867 .subdevice = PCI_ANY_ID,
868 .driver_data = (kernel_ulong_t)&sdhci_o2,
869 },
870
871 {
872 .vendor = PCI_VENDOR_ID_O2,
873 .device = PCI_DEVICE_ID_O2_8220,
874 .subvendor = PCI_ANY_ID,
875 .subdevice = PCI_ANY_ID,
876 .driver_data = (kernel_ulong_t)&sdhci_o2,
877 },
878
879 {
880 .vendor = PCI_VENDOR_ID_O2,
881 .device = PCI_DEVICE_ID_O2_8221,
882 .subvendor = PCI_ANY_ID,
883 .subdevice = PCI_ANY_ID,
884 .driver_data = (kernel_ulong_t)&sdhci_o2,
885 },
886
887 {
888 .vendor = PCI_VENDOR_ID_O2,
889 .device = PCI_DEVICE_ID_O2_8320,
890 .subvendor = PCI_ANY_ID,
891 .subdevice = PCI_ANY_ID,
892 .driver_data = (kernel_ulong_t)&sdhci_o2,
893 },
894
895 {
896 .vendor = PCI_VENDOR_ID_O2,
897 .device = PCI_DEVICE_ID_O2_8321,
898 .subvendor = PCI_ANY_ID,
899 .subdevice = PCI_ANY_ID,
900 .driver_data = (kernel_ulong_t)&sdhci_o2,
901 },
902
Pierre Ossman22606402008-03-23 19:33:23 +0100903 { /* Generic SD host controller */
904 PCI_DEVICE_CLASS((PCI_CLASS_SYSTEM_SDHCI << 8), 0xFFFF00)
905 },
906
907 { /* end: all zeroes */ },
908};
909
910MODULE_DEVICE_TABLE(pci, pci_ids);
911
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100912/*****************************************************************************\
913 * *
914 * SDHCI core callbacks *
915 * *
916\*****************************************************************************/
917
918static int sdhci_pci_enable_dma(struct sdhci_host *host)
919{
920 struct sdhci_pci_slot *slot;
921 struct pci_dev *pdev;
922 int ret;
923
924 slot = sdhci_priv(host);
925 pdev = slot->chip->pdev;
926
927 if (((pdev->class & 0xFFFF00) == (PCI_CLASS_SYSTEM_SDHCI << 8)) &&
928 ((pdev->class & 0x0000FF) != PCI_SDHCI_IFDMA) &&
Richard Röjforsa13abc72009-09-22 16:45:30 -0700929 (host->flags & SDHCI_USE_SDMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100930 dev_warn(&pdev->dev, "Will use DMA mode even though HW "
931 "doesn't fully claim to support it.\n");
932 }
933
Yang Hongyang284901a2009-04-06 19:01:15 -0700934 ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100935 if (ret)
936 return ret;
937
938 pci_set_master(pdev);
939
940 return 0;
941}
942
Major Lee68077b02011-06-29 14:23:46 +0300943static int sdhci_pci_8bit_width(struct sdhci_host *host, int width)
944{
945 u8 ctrl;
946
947 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
948
949 switch (width) {
950 case MMC_BUS_WIDTH_8:
951 ctrl |= SDHCI_CTRL_8BITBUS;
952 ctrl &= ~SDHCI_CTRL_4BITBUS;
953 break;
954 case MMC_BUS_WIDTH_4:
955 ctrl |= SDHCI_CTRL_4BITBUS;
956 ctrl &= ~SDHCI_CTRL_8BITBUS;
957 break;
958 default:
959 ctrl &= ~(SDHCI_CTRL_8BITBUS | SDHCI_CTRL_4BITBUS);
960 break;
961 }
962
963 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
964
965 return 0;
966}
967
Adrian Hunter0f201652011-08-29 16:42:13 +0300968static void sdhci_pci_hw_reset(struct sdhci_host *host)
969{
970 struct sdhci_pci_slot *slot = sdhci_priv(host);
971 int rst_n_gpio = slot->rst_n_gpio;
972
973 if (!gpio_is_valid(rst_n_gpio))
974 return;
975 gpio_set_value_cansleep(rst_n_gpio, 0);
976 /* For eMMC, minimum is 1us but give it 10us for good measure */
977 udelay(10);
978 gpio_set_value_cansleep(rst_n_gpio, 1);
979 /* For eMMC, minimum is 200us but give it 300us for good measure */
980 usleep_range(300, 1000);
981}
982
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100983static struct sdhci_ops sdhci_pci_ops = {
984 .enable_dma = sdhci_pci_enable_dma,
Major Lee68077b02011-06-29 14:23:46 +0300985 .platform_8bit_width = sdhci_pci_8bit_width,
Adrian Hunter0f201652011-08-29 16:42:13 +0300986 .hw_reset = sdhci_pci_hw_reset,
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100987};
988
989/*****************************************************************************\
990 * *
991 * Suspend/resume *
992 * *
993\*****************************************************************************/
994
995#ifdef CONFIG_PM
996
Manuel Lauss29495aa2011-11-03 11:09:45 +0100997static int sdhci_pci_suspend(struct device *dev)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100998{
Manuel Lauss29495aa2011-11-03 11:09:45 +0100999 struct pci_dev *pdev = to_pci_dev(dev);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001000 struct sdhci_pci_chip *chip;
1001 struct sdhci_pci_slot *slot;
Daniel Drake5f619702010-11-04 22:20:39 +00001002 mmc_pm_flag_t slot_pm_flags;
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08001003 mmc_pm_flag_t pm_flags = 0;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001004 int i, ret;
1005
1006 chip = pci_get_drvdata(pdev);
1007 if (!chip)
1008 return 0;
1009
Ameya Palandeb177bc92011-04-05 21:13:13 +03001010 for (i = 0; i < chip->num_slots; i++) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001011 slot = chip->slots[i];
1012 if (!slot)
1013 continue;
1014
Manuel Lauss29495aa2011-11-03 11:09:45 +01001015 ret = sdhci_suspend_host(slot->host);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001016
Axel Linb678b912011-12-03 15:28:05 +08001017 if (ret)
1018 goto err_pci_suspend;
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08001019
Daniel Drake5f619702010-11-04 22:20:39 +00001020 slot_pm_flags = slot->host->mmc->pm_flags;
1021 if (slot_pm_flags & MMC_PM_WAKE_SDIO_IRQ)
1022 sdhci_enable_irq_wakeups(slot->host);
1023
1024 pm_flags |= slot_pm_flags;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001025 }
1026
Pierre Ossman44894282008-04-04 19:36:59 +02001027 if (chip->fixes && chip->fixes->suspend) {
Manuel Lauss29495aa2011-11-03 11:09:45 +01001028 ret = chip->fixes->suspend(chip);
Axel Linb678b912011-12-03 15:28:05 +08001029 if (ret)
1030 goto err_pci_suspend;
Pierre Ossman44894282008-04-04 19:36:59 +02001031 }
1032
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001033 pci_save_state(pdev);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08001034 if (pm_flags & MMC_PM_KEEP_POWER) {
Daniel Drake5f619702010-11-04 22:20:39 +00001035 if (pm_flags & MMC_PM_WAKE_SDIO_IRQ) {
1036 pci_pme_active(pdev, true);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08001037 pci_enable_wake(pdev, PCI_D3hot, 1);
Daniel Drake5f619702010-11-04 22:20:39 +00001038 }
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08001039 pci_set_power_state(pdev, PCI_D3hot);
1040 } else {
Manuel Lauss29495aa2011-11-03 11:09:45 +01001041 pci_enable_wake(pdev, PCI_D3hot, 0);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08001042 pci_disable_device(pdev);
Manuel Lauss29495aa2011-11-03 11:09:45 +01001043 pci_set_power_state(pdev, PCI_D3hot);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08001044 }
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001045
1046 return 0;
Axel Linb678b912011-12-03 15:28:05 +08001047
1048err_pci_suspend:
1049 while (--i >= 0)
1050 sdhci_resume_host(chip->slots[i]->host);
1051 return ret;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001052}
1053
Manuel Lauss29495aa2011-11-03 11:09:45 +01001054static int sdhci_pci_resume(struct device *dev)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001055{
Manuel Lauss29495aa2011-11-03 11:09:45 +01001056 struct pci_dev *pdev = to_pci_dev(dev);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001057 struct sdhci_pci_chip *chip;
1058 struct sdhci_pci_slot *slot;
1059 int i, ret;
1060
1061 chip = pci_get_drvdata(pdev);
1062 if (!chip)
1063 return 0;
1064
1065 pci_set_power_state(pdev, PCI_D0);
1066 pci_restore_state(pdev);
1067 ret = pci_enable_device(pdev);
1068 if (ret)
1069 return ret;
1070
Pierre Ossman45211e22008-03-24 13:09:09 +01001071 if (chip->fixes && chip->fixes->resume) {
1072 ret = chip->fixes->resume(chip);
1073 if (ret)
1074 return ret;
1075 }
1076
Ameya Palandeb177bc92011-04-05 21:13:13 +03001077 for (i = 0; i < chip->num_slots; i++) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001078 slot = chip->slots[i];
1079 if (!slot)
1080 continue;
1081
1082 ret = sdhci_resume_host(slot->host);
1083 if (ret)
1084 return ret;
1085 }
1086
1087 return 0;
1088}
1089
1090#else /* CONFIG_PM */
1091
1092#define sdhci_pci_suspend NULL
1093#define sdhci_pci_resume NULL
1094
1095#endif /* CONFIG_PM */
1096
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001097#ifdef CONFIG_PM_RUNTIME
1098
1099static int sdhci_pci_runtime_suspend(struct device *dev)
1100{
1101 struct pci_dev *pdev = container_of(dev, struct pci_dev, dev);
1102 struct sdhci_pci_chip *chip;
1103 struct sdhci_pci_slot *slot;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001104 int i, ret;
1105
1106 chip = pci_get_drvdata(pdev);
1107 if (!chip)
1108 return 0;
1109
1110 for (i = 0; i < chip->num_slots; i++) {
1111 slot = chip->slots[i];
1112 if (!slot)
1113 continue;
1114
1115 ret = sdhci_runtime_suspend_host(slot->host);
1116
Axel Linb678b912011-12-03 15:28:05 +08001117 if (ret)
1118 goto err_pci_runtime_suspend;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001119 }
1120
1121 if (chip->fixes && chip->fixes->suspend) {
Manuel Lauss29495aa2011-11-03 11:09:45 +01001122 ret = chip->fixes->suspend(chip);
Axel Linb678b912011-12-03 15:28:05 +08001123 if (ret)
1124 goto err_pci_runtime_suspend;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001125 }
1126
1127 return 0;
Axel Linb678b912011-12-03 15:28:05 +08001128
1129err_pci_runtime_suspend:
1130 while (--i >= 0)
1131 sdhci_runtime_resume_host(chip->slots[i]->host);
1132 return ret;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001133}
1134
1135static int sdhci_pci_runtime_resume(struct device *dev)
1136{
1137 struct pci_dev *pdev = container_of(dev, struct pci_dev, dev);
1138 struct sdhci_pci_chip *chip;
1139 struct sdhci_pci_slot *slot;
1140 int i, ret;
1141
1142 chip = pci_get_drvdata(pdev);
1143 if (!chip)
1144 return 0;
1145
1146 if (chip->fixes && chip->fixes->resume) {
1147 ret = chip->fixes->resume(chip);
1148 if (ret)
1149 return ret;
1150 }
1151
1152 for (i = 0; i < chip->num_slots; i++) {
1153 slot = chip->slots[i];
1154 if (!slot)
1155 continue;
1156
1157 ret = sdhci_runtime_resume_host(slot->host);
1158 if (ret)
1159 return ret;
1160 }
1161
1162 return 0;
1163}
1164
1165static int sdhci_pci_runtime_idle(struct device *dev)
1166{
1167 return 0;
1168}
1169
1170#else
1171
1172#define sdhci_pci_runtime_suspend NULL
1173#define sdhci_pci_runtime_resume NULL
1174#define sdhci_pci_runtime_idle NULL
1175
1176#endif
1177
1178static const struct dev_pm_ops sdhci_pci_pm_ops = {
Manuel Lauss29495aa2011-11-03 11:09:45 +01001179 .suspend = sdhci_pci_suspend,
1180 .resume = sdhci_pci_resume,
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001181 .runtime_suspend = sdhci_pci_runtime_suspend,
1182 .runtime_resume = sdhci_pci_runtime_resume,
1183 .runtime_idle = sdhci_pci_runtime_idle,
1184};
1185
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001186/*****************************************************************************\
1187 * *
1188 * Device probing/removal *
1189 * *
1190\*****************************************************************************/
1191
1192static struct sdhci_pci_slot * __devinit sdhci_pci_probe_slot(
Adrian Hunter52c506f2011-12-27 15:48:43 +02001193 struct pci_dev *pdev, struct sdhci_pci_chip *chip, int first_bar,
1194 int slotno)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001195{
1196 struct sdhci_pci_slot *slot;
1197 struct sdhci_host *host;
Adrian Hunter52c506f2011-12-27 15:48:43 +02001198 int ret, bar = first_bar + slotno;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001199
1200 if (!(pci_resource_flags(pdev, bar) & IORESOURCE_MEM)) {
1201 dev_err(&pdev->dev, "BAR %d is not iomem. Aborting.\n", bar);
1202 return ERR_PTR(-ENODEV);
1203 }
1204
1205 if (pci_resource_len(pdev, bar) != 0x100) {
1206 dev_err(&pdev->dev, "Invalid iomem size. You may "
1207 "experience problems.\n");
1208 }
1209
1210 if ((pdev->class & 0x0000FF) == PCI_SDHCI_IFVENDOR) {
1211 dev_err(&pdev->dev, "Vendor specific interface. Aborting.\n");
1212 return ERR_PTR(-ENODEV);
1213 }
1214
1215 if ((pdev->class & 0x0000FF) > PCI_SDHCI_IFVENDOR) {
1216 dev_err(&pdev->dev, "Unknown interface. Aborting.\n");
1217 return ERR_PTR(-ENODEV);
1218 }
1219
1220 host = sdhci_alloc_host(&pdev->dev, sizeof(struct sdhci_pci_slot));
1221 if (IS_ERR(host)) {
Dan Carpenterc60a32c2009-04-10 23:31:10 +02001222 dev_err(&pdev->dev, "cannot allocate host\n");
Julia Lawalldc0fd7b2010-05-26 14:42:11 -07001223 return ERR_CAST(host);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001224 }
1225
1226 slot = sdhci_priv(host);
1227
1228 slot->chip = chip;
1229 slot->host = host;
1230 slot->pci_bar = bar;
Adrian Hunter0f201652011-08-29 16:42:13 +03001231 slot->rst_n_gpio = -EINVAL;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001232
Adrian Hunter52c506f2011-12-27 15:48:43 +02001233 /* Retrieve platform data if there is any */
1234 if (*sdhci_pci_get_data)
1235 slot->data = sdhci_pci_get_data(pdev, slotno);
1236
1237 if (slot->data) {
1238 if (slot->data->setup) {
1239 ret = slot->data->setup(slot->data);
1240 if (ret) {
1241 dev_err(&pdev->dev, "platform setup failed\n");
1242 goto free;
1243 }
1244 }
1245 }
1246
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001247 host->hw_name = "PCI";
1248 host->ops = &sdhci_pci_ops;
1249 host->quirks = chip->quirks;
1250
1251 host->irq = pdev->irq;
1252
1253 ret = pci_request_region(pdev, bar, mmc_hostname(host->mmc));
1254 if (ret) {
1255 dev_err(&pdev->dev, "cannot request region\n");
Adrian Hunter52c506f2011-12-27 15:48:43 +02001256 goto cleanup;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001257 }
1258
Arjan van de Ven092f82e2008-09-28 16:15:56 -07001259 host->ioaddr = pci_ioremap_bar(pdev, bar);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001260 if (!host->ioaddr) {
1261 dev_err(&pdev->dev, "failed to remap registers\n");
Chris Ball9fdcdbb2011-03-29 00:46:12 -04001262 ret = -ENOMEM;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001263 goto release;
1264 }
1265
Pierre Ossman44894282008-04-04 19:36:59 +02001266 if (chip->fixes && chip->fixes->probe_slot) {
1267 ret = chip->fixes->probe_slot(slot);
1268 if (ret)
1269 goto unmap;
1270 }
1271
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08001272 host->mmc->pm_caps = MMC_PM_KEEP_POWER | MMC_PM_WAKE_SDIO_IRQ;
1273
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001274 ret = sdhci_add_host(host);
1275 if (ret)
Pierre Ossman44894282008-04-04 19:36:59 +02001276 goto remove;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001277
1278 return slot;
1279
Pierre Ossman44894282008-04-04 19:36:59 +02001280remove:
1281 if (chip->fixes && chip->fixes->remove_slot)
Pierre Ossman1e728592008-04-16 19:13:13 +02001282 chip->fixes->remove_slot(slot, 0);
Pierre Ossman44894282008-04-04 19:36:59 +02001283
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001284unmap:
1285 iounmap(host->ioaddr);
1286
1287release:
1288 pci_release_region(pdev, bar);
Dan Carpenterc60a32c2009-04-10 23:31:10 +02001289
Adrian Hunter52c506f2011-12-27 15:48:43 +02001290cleanup:
1291 if (slot->data && slot->data->cleanup)
1292 slot->data->cleanup(slot->data);
1293
Dan Carpenterc60a32c2009-04-10 23:31:10 +02001294free:
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001295 sdhci_free_host(host);
1296
1297 return ERR_PTR(ret);
1298}
1299
1300static void sdhci_pci_remove_slot(struct sdhci_pci_slot *slot)
1301{
Pierre Ossman1e728592008-04-16 19:13:13 +02001302 int dead;
1303 u32 scratch;
1304
1305 dead = 0;
1306 scratch = readl(slot->host->ioaddr + SDHCI_INT_STATUS);
1307 if (scratch == (u32)-1)
1308 dead = 1;
1309
1310 sdhci_remove_host(slot->host, dead);
Pierre Ossman44894282008-04-04 19:36:59 +02001311
1312 if (slot->chip->fixes && slot->chip->fixes->remove_slot)
Pierre Ossman1e728592008-04-16 19:13:13 +02001313 slot->chip->fixes->remove_slot(slot, dead);
Pierre Ossman44894282008-04-04 19:36:59 +02001314
Adrian Hunter52c506f2011-12-27 15:48:43 +02001315 if (slot->data && slot->data->cleanup)
1316 slot->data->cleanup(slot->data);
1317
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001318 pci_release_region(slot->chip->pdev, slot->pci_bar);
Pierre Ossman44894282008-04-04 19:36:59 +02001319
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001320 sdhci_free_host(slot->host);
1321}
1322
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001323static void __devinit sdhci_pci_runtime_pm_allow(struct device *dev)
1324{
1325 pm_runtime_put_noidle(dev);
1326 pm_runtime_allow(dev);
1327 pm_runtime_set_autosuspend_delay(dev, 50);
1328 pm_runtime_use_autosuspend(dev);
1329 pm_suspend_ignore_children(dev, 1);
1330}
1331
1332static void __devexit sdhci_pci_runtime_pm_forbid(struct device *dev)
1333{
1334 pm_runtime_forbid(dev);
1335 pm_runtime_get_noresume(dev);
1336}
1337
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001338static int __devinit sdhci_pci_probe(struct pci_dev *pdev,
1339 const struct pci_device_id *ent)
1340{
1341 struct sdhci_pci_chip *chip;
1342 struct sdhci_pci_slot *slot;
1343
Sergei Shtylyovcf5e23e2011-03-17 16:46:17 -04001344 u8 slots, first_bar;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001345 int ret, i;
1346
1347 BUG_ON(pdev == NULL);
1348 BUG_ON(ent == NULL);
1349
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001350 dev_info(&pdev->dev, "SDHCI controller found [%04x:%04x] (rev %x)\n",
Sergei Shtylyovcf5e23e2011-03-17 16:46:17 -04001351 (int)pdev->vendor, (int)pdev->device, (int)pdev->revision);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001352
1353 ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &slots);
1354 if (ret)
1355 return ret;
1356
1357 slots = PCI_SLOT_INFO_SLOTS(slots) + 1;
1358 dev_dbg(&pdev->dev, "found %d slot(s)\n", slots);
1359 if (slots == 0)
1360 return -ENODEV;
1361
1362 BUG_ON(slots > MAX_SLOTS);
1363
1364 ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &first_bar);
1365 if (ret)
1366 return ret;
1367
1368 first_bar &= PCI_SLOT_INFO_FIRST_BAR_MASK;
1369
1370 if (first_bar > 5) {
1371 dev_err(&pdev->dev, "Invalid first BAR. Aborting.\n");
1372 return -ENODEV;
1373 }
1374
1375 ret = pci_enable_device(pdev);
1376 if (ret)
1377 return ret;
1378
1379 chip = kzalloc(sizeof(struct sdhci_pci_chip), GFP_KERNEL);
1380 if (!chip) {
1381 ret = -ENOMEM;
1382 goto err;
1383 }
1384
1385 chip->pdev = pdev;
Ameya Palandeb177bc92011-04-05 21:13:13 +03001386 chip->fixes = (const struct sdhci_pci_fixes *)ent->driver_data;
Adrian Hunterc43fd772011-10-17 10:52:44 +03001387 if (chip->fixes) {
Pierre Ossman22606402008-03-23 19:33:23 +01001388 chip->quirks = chip->fixes->quirks;
Adrian Hunterc43fd772011-10-17 10:52:44 +03001389 chip->allow_runtime_pm = chip->fixes->allow_runtime_pm;
1390 }
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001391 chip->num_slots = slots;
1392
1393 pci_set_drvdata(pdev, chip);
1394
Pierre Ossman22606402008-03-23 19:33:23 +01001395 if (chip->fixes && chip->fixes->probe) {
1396 ret = chip->fixes->probe(chip);
1397 if (ret)
1398 goto free;
1399 }
1400
Alan Cox225d85f2010-10-04 15:24:21 +01001401 slots = chip->num_slots; /* Quirk may have changed this */
1402
Ameya Palandeb177bc92011-04-05 21:13:13 +03001403 for (i = 0; i < slots; i++) {
Adrian Hunter52c506f2011-12-27 15:48:43 +02001404 slot = sdhci_pci_probe_slot(pdev, chip, first_bar, i);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001405 if (IS_ERR(slot)) {
Ameya Palandeb177bc92011-04-05 21:13:13 +03001406 for (i--; i >= 0; i--)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001407 sdhci_pci_remove_slot(chip->slots[i]);
1408 ret = PTR_ERR(slot);
1409 goto free;
1410 }
1411
1412 chip->slots[i] = slot;
1413 }
1414
Adrian Hunterc43fd772011-10-17 10:52:44 +03001415 if (chip->allow_runtime_pm)
1416 sdhci_pci_runtime_pm_allow(&pdev->dev);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001417
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001418 return 0;
1419
1420free:
1421 pci_set_drvdata(pdev, NULL);
1422 kfree(chip);
1423
1424err:
1425 pci_disable_device(pdev);
1426 return ret;
1427}
1428
1429static void __devexit sdhci_pci_remove(struct pci_dev *pdev)
1430{
1431 int i;
1432 struct sdhci_pci_chip *chip;
1433
1434 chip = pci_get_drvdata(pdev);
1435
1436 if (chip) {
Adrian Hunterc43fd772011-10-17 10:52:44 +03001437 if (chip->allow_runtime_pm)
1438 sdhci_pci_runtime_pm_forbid(&pdev->dev);
1439
Ameya Palandeb177bc92011-04-05 21:13:13 +03001440 for (i = 0; i < chip->num_slots; i++)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001441 sdhci_pci_remove_slot(chip->slots[i]);
1442
1443 pci_set_drvdata(pdev, NULL);
1444 kfree(chip);
1445 }
1446
1447 pci_disable_device(pdev);
1448}
1449
1450static struct pci_driver sdhci_driver = {
Ameya Palandeb177bc92011-04-05 21:13:13 +03001451 .name = "sdhci-pci",
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001452 .id_table = pci_ids,
Ameya Palandeb177bc92011-04-05 21:13:13 +03001453 .probe = sdhci_pci_probe,
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001454 .remove = __devexit_p(sdhci_pci_remove),
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001455 .driver = {
1456 .pm = &sdhci_pci_pm_ops
1457 },
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001458};
1459
1460/*****************************************************************************\
1461 * *
1462 * Driver init/exit *
1463 * *
1464\*****************************************************************************/
1465
1466static int __init sdhci_drv_init(void)
1467{
1468 return pci_register_driver(&sdhci_driver);
1469}
1470
1471static void __exit sdhci_drv_exit(void)
1472{
1473 pci_unregister_driver(&sdhci_driver);
1474}
1475
1476module_init(sdhci_drv_init);
1477module_exit(sdhci_drv_exit);
1478
Pierre Ossman32710e82009-04-08 20:14:54 +02001479MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001480MODULE_DESCRIPTION("Secure Digital Host Controller Interface PCI driver");
1481MODULE_LICENSE("GPL");