blob: 06f026a537b2218b7f97c8d5640c58c7572f3639 [file] [log] [blame]
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001/* linux/drivers/mmc/host/sdhci-pci.c - SDHCI on PCI bus interface
2 *
3 * Copyright (C) 2005-2008 Pierre Ossman, All Rights Reserved.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or (at
8 * your option) any later version.
9 *
10 * Thanks to the following companies for their support:
11 *
12 * - JMicron (hardware and technical support)
13 */
14
15#include <linux/delay.h>
16#include <linux/highmem.h>
Paul Gortmaker88b47672011-07-03 15:15:51 -040017#include <linux/module.h>
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010018#include <linux/pci.h>
19#include <linux/dma-mapping.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Maxim Levitskyccc92c22010-08-10 18:01:42 -070021#include <linux/device.h>
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010022#include <linux/mmc/host.h>
Ameya Palandeb177bc92011-04-05 21:13:13 +030023#include <linux/scatterlist.h>
24#include <linux/io.h>
Adrian Hunter0f201652011-08-29 16:42:13 +030025#include <linux/gpio.h>
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030026#include <linux/pm_runtime.h>
Adrian Hunter52c506f2011-12-27 15:48:43 +020027#include <linux/mmc/sdhci-pci-data.h>
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010028
29#include "sdhci.h"
30
31/*
Alexander Stein296e0b02012-03-14 08:38:58 +010032 * PCI device IDs
33 */
34#define PCI_DEVICE_ID_INTEL_PCH_SDIO0 0x8809
35#define PCI_DEVICE_ID_INTEL_PCH_SDIO1 0x880a
Adrian Hunter728ef3d2013-04-26 11:27:23 +030036#define PCI_DEVICE_ID_INTEL_BYT_EMMC 0x0f14
37#define PCI_DEVICE_ID_INTEL_BYT_SDIO 0x0f15
38#define PCI_DEVICE_ID_INTEL_BYT_SD 0x0f16
Adrian Hunter30d025c2013-06-20 12:57:59 +030039#define PCI_DEVICE_ID_INTEL_BYT_EMMC2 0x0f50
David Cohen8776a162013-10-01 13:18:15 -070040#define PCI_DEVICE_ID_INTEL_MRFL_MMC 0x1190
Alexander Stein296e0b02012-03-14 08:38:58 +010041
42/*
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010043 * PCI registers
44 */
45
46#define PCI_SDHCI_IFPIO 0x00
47#define PCI_SDHCI_IFDMA 0x01
48#define PCI_SDHCI_IFVENDOR 0x02
49
50#define PCI_SLOT_INFO 0x40 /* 8 bits */
51#define PCI_SLOT_INFO_SLOTS(x) ((x >> 4) & 7)
52#define PCI_SLOT_INFO_FIRST_BAR_MASK 0x07
53
54#define MAX_SLOTS 8
55
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010056struct sdhci_pci_chip;
Pierre Ossman44894282008-04-04 19:36:59 +020057struct sdhci_pci_slot;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010058
Pierre Ossman22606402008-03-23 19:33:23 +010059struct sdhci_pci_fixes {
60 unsigned int quirks;
Adrian Hunterf3c55a72012-02-07 14:48:55 +020061 unsigned int quirks2;
Adrian Hunterc43fd772011-10-17 10:52:44 +030062 bool allow_runtime_pm;
Pierre Ossman22606402008-03-23 19:33:23 +010063
Ameya Palandeb177bc92011-04-05 21:13:13 +030064 int (*probe) (struct sdhci_pci_chip *);
Pierre Ossman45211e22008-03-24 13:09:09 +010065
Ameya Palandeb177bc92011-04-05 21:13:13 +030066 int (*probe_slot) (struct sdhci_pci_slot *);
67 void (*remove_slot) (struct sdhci_pci_slot *, int);
Pierre Ossman44894282008-04-04 19:36:59 +020068
Manuel Lauss29495aa2011-11-03 11:09:45 +010069 int (*suspend) (struct sdhci_pci_chip *);
Ameya Palandeb177bc92011-04-05 21:13:13 +030070 int (*resume) (struct sdhci_pci_chip *);
Pierre Ossman22606402008-03-23 19:33:23 +010071};
72
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010073struct sdhci_pci_slot {
74 struct sdhci_pci_chip *chip;
75 struct sdhci_host *host;
Adrian Hunter52c506f2011-12-27 15:48:43 +020076 struct sdhci_pci_data *data;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010077
78 int pci_bar;
Adrian Hunter0f201652011-08-29 16:42:13 +030079 int rst_n_gpio;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030080 int cd_gpio;
81 int cd_irq;
Adrian Hunterc9faff62013-06-13 11:50:26 +030082
83 void (*hw_reset)(struct sdhci_host *host);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010084};
85
86struct sdhci_pci_chip {
87 struct pci_dev *pdev;
Pierre Ossman22606402008-03-23 19:33:23 +010088
89 unsigned int quirks;
Adrian Hunterf3c55a72012-02-07 14:48:55 +020090 unsigned int quirks2;
Adrian Hunterc43fd772011-10-17 10:52:44 +030091 bool allow_runtime_pm;
Pierre Ossman22606402008-03-23 19:33:23 +010092 const struct sdhci_pci_fixes *fixes;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010093
94 int num_slots; /* Slots on controller */
95 struct sdhci_pci_slot *slots[MAX_SLOTS]; /* Pointers to host slots */
96};
97
Pierre Ossman22606402008-03-23 19:33:23 +010098
99/*****************************************************************************\
100 * *
101 * Hardware specific quirk handling *
102 * *
103\*****************************************************************************/
104
105static int ricoh_probe(struct sdhci_pci_chip *chip)
106{
Chris Ballc99436f2009-09-22 16:45:22 -0700107 if (chip->pdev->subsystem_vendor == PCI_VENDOR_ID_SAMSUNG ||
108 chip->pdev->subsystem_vendor == PCI_VENDOR_ID_SONY)
Pierre Ossman22606402008-03-23 19:33:23 +0100109 chip->quirks |= SDHCI_QUIRK_NO_CARD_NO_RESET;
Maxim Levitskyccc92c22010-08-10 18:01:42 -0700110 return 0;
111}
Pierre Ossman22606402008-03-23 19:33:23 +0100112
Maxim Levitskyccc92c22010-08-10 18:01:42 -0700113static int ricoh_mmc_probe_slot(struct sdhci_pci_slot *slot)
114{
115 slot->host->caps =
116 ((0x21 << SDHCI_TIMEOUT_CLK_SHIFT)
117 & SDHCI_TIMEOUT_CLK_MASK) |
118
119 ((0x21 << SDHCI_CLOCK_BASE_SHIFT)
120 & SDHCI_CLOCK_BASE_MASK) |
121
122 SDHCI_TIMEOUT_CLK_UNIT |
123 SDHCI_CAN_VDD_330 |
Madhvapathi Sriram1a1f1f02012-10-15 04:47:30 +0000124 SDHCI_CAN_DO_HISPD |
Maxim Levitskyccc92c22010-08-10 18:01:42 -0700125 SDHCI_CAN_DO_SDMA;
126 return 0;
127}
128
129static int ricoh_mmc_resume(struct sdhci_pci_chip *chip)
130{
131 /* Apply a delay to allow controller to settle */
132 /* Otherwise it becomes confused if card state changed
133 during suspend */
134 msleep(500);
Pierre Ossman22606402008-03-23 19:33:23 +0100135 return 0;
136}
137
138static const struct sdhci_pci_fixes sdhci_ricoh = {
139 .probe = ricoh_probe,
Vasily Khoruzhick84938292010-03-05 13:43:46 -0800140 .quirks = SDHCI_QUIRK_32BIT_DMA_ADDR |
141 SDHCI_QUIRK_FORCE_DMA |
142 SDHCI_QUIRK_CLOCK_BEFORE_RESET,
Pierre Ossman22606402008-03-23 19:33:23 +0100143};
144
Maxim Levitskyccc92c22010-08-10 18:01:42 -0700145static const struct sdhci_pci_fixes sdhci_ricoh_mmc = {
146 .probe_slot = ricoh_mmc_probe_slot,
147 .resume = ricoh_mmc_resume,
148 .quirks = SDHCI_QUIRK_32BIT_DMA_ADDR |
149 SDHCI_QUIRK_CLOCK_BEFORE_RESET |
150 SDHCI_QUIRK_NO_CARD_NO_RESET |
151 SDHCI_QUIRK_MISSING_CAPS
152};
153
Pierre Ossman22606402008-03-23 19:33:23 +0100154static const struct sdhci_pci_fixes sdhci_ene_712 = {
155 .quirks = SDHCI_QUIRK_SINGLE_POWER_WRITE |
156 SDHCI_QUIRK_BROKEN_DMA,
157};
158
159static const struct sdhci_pci_fixes sdhci_ene_714 = {
160 .quirks = SDHCI_QUIRK_SINGLE_POWER_WRITE |
161 SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS |
162 SDHCI_QUIRK_BROKEN_DMA,
163};
164
165static const struct sdhci_pci_fixes sdhci_cafe = {
166 .quirks = SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER |
Andres Salomona0874892009-03-02 21:48:20 +0100167 SDHCI_QUIRK_NO_BUSY_IRQ |
Daniel Drake55fc05b2012-07-03 23:13:39 +0100168 SDHCI_QUIRK_BROKEN_CARD_DETECTION |
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200169 SDHCI_QUIRK_BROKEN_TIMEOUT_VAL,
Pierre Ossman22606402008-03-23 19:33:23 +0100170};
171
Major Lee68077b02011-06-29 14:23:46 +0300172static int mrst_hc_probe_slot(struct sdhci_pci_slot *slot)
173{
174 slot->host->mmc->caps |= MMC_CAP_8_BIT_DATA;
175 return 0;
176}
177
Alan Coxf9ee3ea2010-10-04 15:25:11 +0100178/*
179 * ADMA operation is disabled for Moorestown platform due to
180 * hardware bugs.
181 */
Jacob Pan35ac6f02010-11-09 13:57:29 +0000182static int mrst_hc_probe(struct sdhci_pci_chip *chip)
Alan Coxf9ee3ea2010-10-04 15:25:11 +0100183{
184 /*
Jacob Pan35ac6f02010-11-09 13:57:29 +0000185 * slots number is fixed here for MRST as SDIO3/5 are never used and
186 * have hardware bugs.
Alan Coxf9ee3ea2010-10-04 15:25:11 +0100187 */
188 chip->num_slots = 1;
189 return 0;
190}
191
Alexander Stein296e0b02012-03-14 08:38:58 +0100192static int pch_hc_probe_slot(struct sdhci_pci_slot *slot)
193{
194 slot->host->mmc->caps |= MMC_CAP_8_BIT_DATA;
195 return 0;
196}
197
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300198#ifdef CONFIG_PM_RUNTIME
199
Adrian Hunterc5e027a2011-12-27 15:48:44 +0200200static irqreturn_t sdhci_pci_sd_cd(int irq, void *dev_id)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300201{
202 struct sdhci_pci_slot *slot = dev_id;
203 struct sdhci_host *host = slot->host;
204
205 mmc_detect_change(host->mmc, msecs_to_jiffies(200));
206 return IRQ_HANDLED;
207}
208
Adrian Hunterc5e027a2011-12-27 15:48:44 +0200209static void sdhci_pci_add_own_cd(struct sdhci_pci_slot *slot)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300210{
Adrian Hunterc5e027a2011-12-27 15:48:44 +0200211 int err, irq, gpio = slot->cd_gpio;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300212
213 slot->cd_gpio = -EINVAL;
214 slot->cd_irq = -EINVAL;
215
Adrian Hunterc5e027a2011-12-27 15:48:44 +0200216 if (!gpio_is_valid(gpio))
217 return;
218
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300219 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
Adrian Hunterc5e027a2011-12-27 15:48:44 +0200231 err = request_irq(irq, sdhci_pci_sd_cd, IRQF_TRIGGER_RISING |
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300232 IRQF_TRIGGER_FALLING, "sd_cd", slot);
233 if (err)
234 goto out_free;
235
236 slot->cd_gpio = gpio;
237 slot->cd_irq = irq;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300238
Adrian Hunterc5e027a2011-12-27 15:48:44 +0200239 return;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300240
241out_free:
242 gpio_free(gpio);
243out:
244 dev_warn(&slot->chip->pdev->dev, "failed to setup card detect wake up\n");
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300245}
246
Adrian Hunterc5e027a2011-12-27 15:48:44 +0200247static void sdhci_pci_remove_own_cd(struct sdhci_pci_slot *slot)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300248{
249 if (slot->cd_irq >= 0)
250 free_irq(slot->cd_irq, slot);
Adrian Hunterc5e027a2011-12-27 15:48:44 +0200251 if (gpio_is_valid(slot->cd_gpio))
252 gpio_free(slot->cd_gpio);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300253}
254
255#else
256
Adrian Hunterc5e027a2011-12-27 15:48:44 +0200257static inline void sdhci_pci_add_own_cd(struct sdhci_pci_slot *slot)
258{
259}
260
261static inline void sdhci_pci_remove_own_cd(struct sdhci_pci_slot *slot)
262{
263}
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300264
265#endif
266
Adrian Hunter0d013bc2011-06-29 14:23:47 +0300267static int mfd_emmc_probe_slot(struct sdhci_pci_slot *slot)
268{
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300269 slot->host->mmc->caps |= MMC_CAP_8_BIT_DATA | MMC_CAP_NONREMOVABLE;
Adrian Hunterda721cf2012-02-07 14:48:53 +0200270 slot->host->mmc->caps2 |= MMC_CAP2_BOOTPART_NOACC |
271 MMC_CAP2_HC_ERASE_SZ;
Adrian Hunter0d013bc2011-06-29 14:23:47 +0300272 return 0;
273}
274
Adrian Hunter93933502011-12-27 15:48:47 +0200275static int mfd_sdio_probe_slot(struct sdhci_pci_slot *slot)
276{
Adrian Hunter012e4672012-01-30 14:27:18 +0200277 slot->host->mmc->caps |= MMC_CAP_POWER_OFF_CARD | MMC_CAP_NONREMOVABLE;
Adrian Hunter93933502011-12-27 15:48:47 +0200278 return 0;
279}
280
Alan Coxf9ee3ea2010-10-04 15:25:11 +0100281static const struct sdhci_pci_fixes sdhci_intel_mrst_hc0 = {
282 .quirks = SDHCI_QUIRK_BROKEN_ADMA | SDHCI_QUIRK_NO_HISPD_BIT,
Major Lee68077b02011-06-29 14:23:46 +0300283 .probe_slot = mrst_hc_probe_slot,
Alan Coxf9ee3ea2010-10-04 15:25:11 +0100284};
285
Jacob Pan35ac6f02010-11-09 13:57:29 +0000286static const struct sdhci_pci_fixes sdhci_intel_mrst_hc1_hc2 = {
Alan Coxf9ee3ea2010-10-04 15:25:11 +0100287 .quirks = SDHCI_QUIRK_BROKEN_ADMA | SDHCI_QUIRK_NO_HISPD_BIT,
Jacob Pan35ac6f02010-11-09 13:57:29 +0000288 .probe = mrst_hc_probe,
Alan Coxf9ee3ea2010-10-04 15:25:11 +0100289};
290
Xiaochen Shen29229052010-10-04 15:24:52 +0100291static const struct sdhci_pci_fixes sdhci_intel_mfd_sd = {
292 .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
Adrian Hunterc43fd772011-10-17 10:52:44 +0300293 .allow_runtime_pm = true,
Xiaochen Shen29229052010-10-04 15:24:52 +0100294};
295
Adrian Hunter0d013bc2011-06-29 14:23:47 +0300296static const struct sdhci_pci_fixes sdhci_intel_mfd_sdio = {
Xiaochen Shen29229052010-10-04 15:24:52 +0100297 .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
Adrian Hunterf3c55a72012-02-07 14:48:55 +0200298 .quirks2 = SDHCI_QUIRK2_HOST_OFF_CARD_ON,
Adrian Hunterc43fd772011-10-17 10:52:44 +0300299 .allow_runtime_pm = true,
Adrian Hunter93933502011-12-27 15:48:47 +0200300 .probe_slot = mfd_sdio_probe_slot,
Xiaochen Shen29229052010-10-04 15:24:52 +0100301};
302
Adrian Hunter0d013bc2011-06-29 14:23:47 +0300303static const struct sdhci_pci_fixes sdhci_intel_mfd_emmc = {
304 .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
Adrian Hunterc43fd772011-10-17 10:52:44 +0300305 .allow_runtime_pm = true,
Adrian Hunter0d013bc2011-06-29 14:23:47 +0300306 .probe_slot = mfd_emmc_probe_slot,
307};
308
Alexander Stein296e0b02012-03-14 08:38:58 +0100309static const struct sdhci_pci_fixes sdhci_intel_pch_sdio = {
310 .quirks = SDHCI_QUIRK_BROKEN_ADMA,
311 .probe_slot = pch_hc_probe_slot,
312};
313
Adrian Hunterc9faff62013-06-13 11:50:26 +0300314static void sdhci_pci_int_hw_reset(struct sdhci_host *host)
315{
316 u8 reg;
317
318 reg = sdhci_readb(host, SDHCI_POWER_CONTROL);
319 reg |= 0x10;
320 sdhci_writeb(host, reg, SDHCI_POWER_CONTROL);
321 /* For eMMC, minimum is 1us but give it 9us for good measure */
322 udelay(9);
323 reg &= ~0x10;
324 sdhci_writeb(host, reg, SDHCI_POWER_CONTROL);
325 /* For eMMC, minimum is 200us but give it 300us for good measure */
326 usleep_range(300, 1000);
327}
328
Adrian Hunter728ef3d2013-04-26 11:27:23 +0300329static int byt_emmc_probe_slot(struct sdhci_pci_slot *slot)
330{
Adrian Hunterc9faff62013-06-13 11:50:26 +0300331 slot->host->mmc->caps |= MMC_CAP_8_BIT_DATA | MMC_CAP_NONREMOVABLE |
332 MMC_CAP_HW_RESET;
Adrian Hunter728ef3d2013-04-26 11:27:23 +0300333 slot->host->mmc->caps2 |= MMC_CAP2_HC_ERASE_SZ;
Adrian Hunterc9faff62013-06-13 11:50:26 +0300334 slot->hw_reset = sdhci_pci_int_hw_reset;
Adrian Hunter728ef3d2013-04-26 11:27:23 +0300335 return 0;
336}
337
338static int byt_sdio_probe_slot(struct sdhci_pci_slot *slot)
339{
340 slot->host->mmc->caps |= MMC_CAP_POWER_OFF_CARD | MMC_CAP_NONREMOVABLE;
341 return 0;
342}
343
344static const struct sdhci_pci_fixes sdhci_intel_byt_emmc = {
345 .allow_runtime_pm = true,
346 .probe_slot = byt_emmc_probe_slot,
347};
348
349static const struct sdhci_pci_fixes sdhci_intel_byt_sdio = {
350 .quirks2 = SDHCI_QUIRK2_HOST_OFF_CARD_ON,
351 .allow_runtime_pm = true,
352 .probe_slot = byt_sdio_probe_slot,
353};
354
355static const struct sdhci_pci_fixes sdhci_intel_byt_sd = {
Adrian Hunter7396e312013-05-06 12:17:34 +0300356 .quirks2 = SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON,
357 .allow_runtime_pm = true,
Adrian Hunter728ef3d2013-04-26 11:27:23 +0300358};
359
David Cohen8776a162013-10-01 13:18:15 -0700360/* Define Host controllers for Intel Merrifield platform */
361#define INTEL_MRFL_EMMC_0 0
362#define INTEL_MRFL_EMMC_1 1
363
364static int intel_mrfl_mmc_probe_slot(struct sdhci_pci_slot *slot)
365{
366 if ((PCI_FUNC(slot->chip->pdev->devfn) != INTEL_MRFL_EMMC_0) &&
367 (PCI_FUNC(slot->chip->pdev->devfn) != INTEL_MRFL_EMMC_1))
368 /* SD support is not ready yet */
369 return -ENODEV;
370
371 slot->host->mmc->caps |= MMC_CAP_8_BIT_DATA | MMC_CAP_NONREMOVABLE |
372 MMC_CAP_1_8V_DDR;
373
374 return 0;
375}
376
377static const struct sdhci_pci_fixes sdhci_intel_mrfl_mmc = {
378 .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
379 .probe_slot = intel_mrfl_mmc_probe_slot,
380};
381
Jennifer Li26daa1e2010-11-17 23:01:59 -0500382/* O2Micro extra registers */
383#define O2_SD_LOCK_WP 0xD3
384#define O2_SD_MULTI_VCC3V 0xEE
385#define O2_SD_CLKREQ 0xEC
386#define O2_SD_CAPS 0xE0
387#define O2_SD_ADMA1 0xE2
388#define O2_SD_ADMA2 0xE7
389#define O2_SD_INF_MOD 0xF1
390
391static int o2_probe(struct sdhci_pci_chip *chip)
392{
393 int ret;
394 u8 scratch;
395
396 switch (chip->pdev->device) {
397 case PCI_DEVICE_ID_O2_8220:
398 case PCI_DEVICE_ID_O2_8221:
399 case PCI_DEVICE_ID_O2_8320:
400 case PCI_DEVICE_ID_O2_8321:
401 /* This extra setup is required due to broken ADMA. */
402 ret = pci_read_config_byte(chip->pdev, O2_SD_LOCK_WP, &scratch);
403 if (ret)
404 return ret;
405 scratch &= 0x7f;
406 pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch);
407
408 /* Set Multi 3 to VCC3V# */
409 pci_write_config_byte(chip->pdev, O2_SD_MULTI_VCC3V, 0x08);
410
411 /* Disable CLK_REQ# support after media DET */
412 ret = pci_read_config_byte(chip->pdev, O2_SD_CLKREQ, &scratch);
413 if (ret)
414 return ret;
415 scratch |= 0x20;
416 pci_write_config_byte(chip->pdev, O2_SD_CLKREQ, scratch);
417
418 /* Choose capabilities, enable SDMA. We have to write 0x01
419 * to the capabilities register first to unlock it.
420 */
421 ret = pci_read_config_byte(chip->pdev, O2_SD_CAPS, &scratch);
422 if (ret)
423 return ret;
424 scratch |= 0x01;
425 pci_write_config_byte(chip->pdev, O2_SD_CAPS, scratch);
426 pci_write_config_byte(chip->pdev, O2_SD_CAPS, 0x73);
427
428 /* Disable ADMA1/2 */
429 pci_write_config_byte(chip->pdev, O2_SD_ADMA1, 0x39);
430 pci_write_config_byte(chip->pdev, O2_SD_ADMA2, 0x08);
431
432 /* Disable the infinite transfer mode */
433 ret = pci_read_config_byte(chip->pdev, O2_SD_INF_MOD, &scratch);
434 if (ret)
435 return ret;
436 scratch |= 0x08;
437 pci_write_config_byte(chip->pdev, O2_SD_INF_MOD, scratch);
438
439 /* Lock WP */
440 ret = pci_read_config_byte(chip->pdev, O2_SD_LOCK_WP, &scratch);
441 if (ret)
442 return ret;
443 scratch |= 0x80;
444 pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch);
445 }
446
447 return 0;
448}
449
Pierre Ossman45211e22008-03-24 13:09:09 +0100450static int jmicron_pmos(struct sdhci_pci_chip *chip, int on)
451{
452 u8 scratch;
453 int ret;
454
455 ret = pci_read_config_byte(chip->pdev, 0xAE, &scratch);
456 if (ret)
457 return ret;
458
459 /*
460 * Turn PMOS on [bit 0], set over current detection to 2.4 V
461 * [bit 1:2] and enable over current debouncing [bit 6].
462 */
463 if (on)
464 scratch |= 0x47;
465 else
466 scratch &= ~0x47;
467
468 ret = pci_write_config_byte(chip->pdev, 0xAE, scratch);
469 if (ret)
470 return ret;
471
472 return 0;
473}
474
475static int jmicron_probe(struct sdhci_pci_chip *chip)
476{
477 int ret;
Takashi Iwai8f230f42010-12-08 10:04:30 +0100478 u16 mmcdev = 0;
Pierre Ossman45211e22008-03-24 13:09:09 +0100479
Pierre Ossman93fc48c2008-06-28 18:21:41 +0200480 if (chip->pdev->revision == 0) {
481 chip->quirks |= SDHCI_QUIRK_32BIT_DMA_ADDR |
482 SDHCI_QUIRK_32BIT_DMA_SIZE |
Pierre Ossman2134a922008-06-28 18:28:51 +0200483 SDHCI_QUIRK_32BIT_ADMA_SIZE |
Pierre Ossman4a3cba32008-07-29 00:11:16 +0200484 SDHCI_QUIRK_RESET_AFTER_REQUEST |
Pierre Ossman86a6a872009-02-02 21:13:49 +0100485 SDHCI_QUIRK_BROKEN_SMALL_PIO;
Pierre Ossman93fc48c2008-06-28 18:21:41 +0200486 }
487
Pierre Ossman45211e22008-03-24 13:09:09 +0100488 /*
Pierre Ossman44894282008-04-04 19:36:59 +0200489 * JMicron chips can have two interfaces to the same hardware
490 * in order to work around limitations in Microsoft's driver.
491 * We need to make sure we only bind to one of them.
492 *
493 * This code assumes two things:
494 *
495 * 1. The PCI code adds subfunctions in order.
496 *
497 * 2. The MMC interface has a lower subfunction number
498 * than the SD interface.
499 */
Takashi Iwai8f230f42010-12-08 10:04:30 +0100500 if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_SD)
501 mmcdev = PCI_DEVICE_ID_JMICRON_JMB38X_MMC;
502 else if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_SD)
503 mmcdev = PCI_DEVICE_ID_JMICRON_JMB388_ESD;
504
505 if (mmcdev) {
Pierre Ossman44894282008-04-04 19:36:59 +0200506 struct pci_dev *sd_dev;
507
508 sd_dev = NULL;
509 while ((sd_dev = pci_get_device(PCI_VENDOR_ID_JMICRON,
Takashi Iwai8f230f42010-12-08 10:04:30 +0100510 mmcdev, sd_dev)) != NULL) {
Pierre Ossman44894282008-04-04 19:36:59 +0200511 if ((PCI_SLOT(chip->pdev->devfn) ==
512 PCI_SLOT(sd_dev->devfn)) &&
513 (chip->pdev->bus == sd_dev->bus))
514 break;
515 }
516
517 if (sd_dev) {
518 pci_dev_put(sd_dev);
519 dev_info(&chip->pdev->dev, "Refusing to bind to "
520 "secondary interface.\n");
521 return -ENODEV;
522 }
523 }
524
525 /*
Pierre Ossman45211e22008-03-24 13:09:09 +0100526 * JMicron chips need a bit of a nudge to enable the power
527 * output pins.
528 */
529 ret = jmicron_pmos(chip, 1);
530 if (ret) {
531 dev_err(&chip->pdev->dev, "Failure enabling card power\n");
532 return ret;
533 }
534
Takashi Iwai82b0e232011-04-21 20:26:38 +0200535 /* quirk for unsable RO-detection on JM388 chips */
536 if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_SD ||
537 chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD)
538 chip->quirks |= SDHCI_QUIRK_UNSTABLE_RO_DETECT;
539
Pierre Ossman45211e22008-03-24 13:09:09 +0100540 return 0;
541}
542
Pierre Ossman44894282008-04-04 19:36:59 +0200543static void jmicron_enable_mmc(struct sdhci_host *host, int on)
544{
545 u8 scratch;
546
547 scratch = readb(host->ioaddr + 0xC0);
548
549 if (on)
550 scratch |= 0x01;
551 else
552 scratch &= ~0x01;
553
554 writeb(scratch, host->ioaddr + 0xC0);
555}
556
557static int jmicron_probe_slot(struct sdhci_pci_slot *slot)
558{
Pierre Ossman2134a922008-06-28 18:28:51 +0200559 if (slot->chip->pdev->revision == 0) {
560 u16 version;
561
562 version = readl(slot->host->ioaddr + SDHCI_HOST_VERSION);
563 version = (version & SDHCI_VENDOR_VER_MASK) >>
564 SDHCI_VENDOR_VER_SHIFT;
565
566 /*
567 * Older versions of the chip have lots of nasty glitches
568 * in the ADMA engine. It's best just to avoid it
569 * completely.
570 */
571 if (version < 0xAC)
572 slot->host->quirks |= SDHCI_QUIRK_BROKEN_ADMA;
573 }
574
Takashi Iwai8f230f42010-12-08 10:04:30 +0100575 /* JM388 MMC doesn't support 1.8V while SD supports it */
576 if (slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD) {
577 slot->host->ocr_avail_sd = MMC_VDD_32_33 | MMC_VDD_33_34 |
578 MMC_VDD_29_30 | MMC_VDD_30_31 |
579 MMC_VDD_165_195; /* allow 1.8V */
580 slot->host->ocr_avail_mmc = MMC_VDD_32_33 | MMC_VDD_33_34 |
581 MMC_VDD_29_30 | MMC_VDD_30_31; /* no 1.8V for MMC */
582 }
583
Pierre Ossman44894282008-04-04 19:36:59 +0200584 /*
585 * The secondary interface requires a bit set to get the
586 * interrupts.
587 */
Takashi Iwai8f230f42010-12-08 10:04:30 +0100588 if (slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC ||
589 slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD)
Pierre Ossman44894282008-04-04 19:36:59 +0200590 jmicron_enable_mmc(slot->host, 1);
591
Takashi Iwaid75c1082010-12-16 17:54:14 +0100592 slot->host->mmc->caps |= MMC_CAP_BUS_WIDTH_TEST;
593
Pierre Ossman44894282008-04-04 19:36:59 +0200594 return 0;
595}
596
Pierre Ossman1e728592008-04-16 19:13:13 +0200597static void jmicron_remove_slot(struct sdhci_pci_slot *slot, int dead)
Pierre Ossman44894282008-04-04 19:36:59 +0200598{
Pierre Ossman1e728592008-04-16 19:13:13 +0200599 if (dead)
600 return;
601
Takashi Iwai8f230f42010-12-08 10:04:30 +0100602 if (slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC ||
603 slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD)
Pierre Ossman44894282008-04-04 19:36:59 +0200604 jmicron_enable_mmc(slot->host, 0);
605}
606
Manuel Lauss29495aa2011-11-03 11:09:45 +0100607static int jmicron_suspend(struct sdhci_pci_chip *chip)
Pierre Ossman44894282008-04-04 19:36:59 +0200608{
609 int i;
610
Takashi Iwai8f230f42010-12-08 10:04:30 +0100611 if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC ||
612 chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD) {
Ameya Palandeb177bc92011-04-05 21:13:13 +0300613 for (i = 0; i < chip->num_slots; i++)
Pierre Ossman44894282008-04-04 19:36:59 +0200614 jmicron_enable_mmc(chip->slots[i]->host, 0);
615 }
616
617 return 0;
618}
619
Pierre Ossman45211e22008-03-24 13:09:09 +0100620static int jmicron_resume(struct sdhci_pci_chip *chip)
621{
Pierre Ossman44894282008-04-04 19:36:59 +0200622 int ret, i;
623
Takashi Iwai8f230f42010-12-08 10:04:30 +0100624 if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC ||
625 chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD) {
Ameya Palandeb177bc92011-04-05 21:13:13 +0300626 for (i = 0; i < chip->num_slots; i++)
Pierre Ossman44894282008-04-04 19:36:59 +0200627 jmicron_enable_mmc(chip->slots[i]->host, 1);
628 }
Pierre Ossman45211e22008-03-24 13:09:09 +0100629
630 ret = jmicron_pmos(chip, 1);
631 if (ret) {
632 dev_err(&chip->pdev->dev, "Failure enabling card power\n");
633 return ret;
634 }
635
636 return 0;
637}
638
Jennifer Li26daa1e2010-11-17 23:01:59 -0500639static const struct sdhci_pci_fixes sdhci_o2 = {
640 .probe = o2_probe,
641};
642
Pierre Ossman22606402008-03-23 19:33:23 +0100643static const struct sdhci_pci_fixes sdhci_jmicron = {
Pierre Ossman45211e22008-03-24 13:09:09 +0100644 .probe = jmicron_probe,
645
Pierre Ossman44894282008-04-04 19:36:59 +0200646 .probe_slot = jmicron_probe_slot,
647 .remove_slot = jmicron_remove_slot,
648
649 .suspend = jmicron_suspend,
Pierre Ossman45211e22008-03-24 13:09:09 +0100650 .resume = jmicron_resume,
Pierre Ossman22606402008-03-23 19:33:23 +0100651};
652
Nicolas Pitrea7a61862009-12-14 18:01:26 -0800653/* SysKonnect CardBus2SDIO extra registers */
654#define SYSKT_CTRL 0x200
655#define SYSKT_RDFIFO_STAT 0x204
656#define SYSKT_WRFIFO_STAT 0x208
657#define SYSKT_POWER_DATA 0x20c
658#define SYSKT_POWER_330 0xef
659#define SYSKT_POWER_300 0xf8
660#define SYSKT_POWER_184 0xcc
661#define SYSKT_POWER_CMD 0x20d
662#define SYSKT_POWER_START (1 << 7)
663#define SYSKT_POWER_STATUS 0x20e
664#define SYSKT_POWER_STATUS_OK (1 << 0)
665#define SYSKT_BOARD_REV 0x210
666#define SYSKT_CHIP_REV 0x211
667#define SYSKT_CONF_DATA 0x212
668#define SYSKT_CONF_DATA_1V8 (1 << 2)
669#define SYSKT_CONF_DATA_2V5 (1 << 1)
670#define SYSKT_CONF_DATA_3V3 (1 << 0)
671
672static int syskt_probe(struct sdhci_pci_chip *chip)
673{
674 if ((chip->pdev->class & 0x0000FF) == PCI_SDHCI_IFVENDOR) {
675 chip->pdev->class &= ~0x0000FF;
676 chip->pdev->class |= PCI_SDHCI_IFDMA;
677 }
678 return 0;
679}
680
681static int syskt_probe_slot(struct sdhci_pci_slot *slot)
682{
683 int tm, ps;
684
685 u8 board_rev = readb(slot->host->ioaddr + SYSKT_BOARD_REV);
686 u8 chip_rev = readb(slot->host->ioaddr + SYSKT_CHIP_REV);
687 dev_info(&slot->chip->pdev->dev, "SysKonnect CardBus2SDIO, "
688 "board rev %d.%d, chip rev %d.%d\n",
689 board_rev >> 4, board_rev & 0xf,
690 chip_rev >> 4, chip_rev & 0xf);
691 if (chip_rev >= 0x20)
692 slot->host->quirks |= SDHCI_QUIRK_FORCE_DMA;
693
694 writeb(SYSKT_POWER_330, slot->host->ioaddr + SYSKT_POWER_DATA);
695 writeb(SYSKT_POWER_START, slot->host->ioaddr + SYSKT_POWER_CMD);
696 udelay(50);
697 tm = 10; /* Wait max 1 ms */
698 do {
699 ps = readw(slot->host->ioaddr + SYSKT_POWER_STATUS);
700 if (ps & SYSKT_POWER_STATUS_OK)
701 break;
702 udelay(100);
703 } while (--tm);
704 if (!tm) {
705 dev_err(&slot->chip->pdev->dev,
706 "power regulator never stabilized");
707 writeb(0, slot->host->ioaddr + SYSKT_POWER_CMD);
708 return -ENODEV;
709 }
710
711 return 0;
712}
713
714static const struct sdhci_pci_fixes sdhci_syskt = {
715 .quirks = SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER,
716 .probe = syskt_probe,
717 .probe_slot = syskt_probe_slot,
718};
719
Harald Welte557b0692009-06-18 16:53:38 +0200720static int via_probe(struct sdhci_pci_chip *chip)
721{
722 if (chip->pdev->revision == 0x10)
723 chip->quirks |= SDHCI_QUIRK_DELAY_AFTER_POWER;
724
725 return 0;
726}
727
728static const struct sdhci_pci_fixes sdhci_via = {
729 .probe = via_probe,
730};
731
Bill Pemberton9647f842012-11-19 13:25:11 -0500732static const struct pci_device_id pci_ids[] = {
Pierre Ossman22606402008-03-23 19:33:23 +0100733 {
734 .vendor = PCI_VENDOR_ID_RICOH,
735 .device = PCI_DEVICE_ID_RICOH_R5C822,
736 .subvendor = PCI_ANY_ID,
737 .subdevice = PCI_ANY_ID,
738 .driver_data = (kernel_ulong_t)&sdhci_ricoh,
739 },
740
741 {
Maxim Levitskyccc92c22010-08-10 18:01:42 -0700742 .vendor = PCI_VENDOR_ID_RICOH,
743 .device = 0x843,
744 .subvendor = PCI_ANY_ID,
745 .subdevice = PCI_ANY_ID,
746 .driver_data = (kernel_ulong_t)&sdhci_ricoh_mmc,
747 },
748
749 {
Pablo Castillo568133e2010-08-10 18:02:01 -0700750 .vendor = PCI_VENDOR_ID_RICOH,
751 .device = 0xe822,
752 .subvendor = PCI_ANY_ID,
753 .subdevice = PCI_ANY_ID,
754 .driver_data = (kernel_ulong_t)&sdhci_ricoh_mmc,
755 },
756
757 {
Manoj Iyer5fd11c02011-02-11 16:25:31 -0600758 .vendor = PCI_VENDOR_ID_RICOH,
759 .device = 0xe823,
760 .subvendor = PCI_ANY_ID,
761 .subdevice = PCI_ANY_ID,
762 .driver_data = (kernel_ulong_t)&sdhci_ricoh_mmc,
763 },
764
765 {
Pierre Ossman22606402008-03-23 19:33:23 +0100766 .vendor = PCI_VENDOR_ID_ENE,
767 .device = PCI_DEVICE_ID_ENE_CB712_SD,
768 .subvendor = PCI_ANY_ID,
769 .subdevice = PCI_ANY_ID,
770 .driver_data = (kernel_ulong_t)&sdhci_ene_712,
771 },
772
773 {
774 .vendor = PCI_VENDOR_ID_ENE,
775 .device = PCI_DEVICE_ID_ENE_CB712_SD_2,
776 .subvendor = PCI_ANY_ID,
777 .subdevice = PCI_ANY_ID,
778 .driver_data = (kernel_ulong_t)&sdhci_ene_712,
779 },
780
781 {
782 .vendor = PCI_VENDOR_ID_ENE,
783 .device = PCI_DEVICE_ID_ENE_CB714_SD,
784 .subvendor = PCI_ANY_ID,
785 .subdevice = PCI_ANY_ID,
786 .driver_data = (kernel_ulong_t)&sdhci_ene_714,
787 },
788
789 {
790 .vendor = PCI_VENDOR_ID_ENE,
791 .device = PCI_DEVICE_ID_ENE_CB714_SD_2,
792 .subvendor = PCI_ANY_ID,
793 .subdevice = PCI_ANY_ID,
794 .driver_data = (kernel_ulong_t)&sdhci_ene_714,
795 },
796
797 {
798 .vendor = PCI_VENDOR_ID_MARVELL,
David Woodhouse8c5eb882008-09-03 09:45:57 +0100799 .device = PCI_DEVICE_ID_MARVELL_88ALP01_SD,
Pierre Ossman22606402008-03-23 19:33:23 +0100800 .subvendor = PCI_ANY_ID,
801 .subdevice = PCI_ANY_ID,
802 .driver_data = (kernel_ulong_t)&sdhci_cafe,
803 },
804
805 {
806 .vendor = PCI_VENDOR_ID_JMICRON,
807 .device = PCI_DEVICE_ID_JMICRON_JMB38X_SD,
808 .subvendor = PCI_ANY_ID,
809 .subdevice = PCI_ANY_ID,
810 .driver_data = (kernel_ulong_t)&sdhci_jmicron,
811 },
812
Pierre Ossman44894282008-04-04 19:36:59 +0200813 {
814 .vendor = PCI_VENDOR_ID_JMICRON,
815 .device = PCI_DEVICE_ID_JMICRON_JMB38X_MMC,
816 .subvendor = PCI_ANY_ID,
817 .subdevice = PCI_ANY_ID,
818 .driver_data = (kernel_ulong_t)&sdhci_jmicron,
819 },
820
Harald Welte557b0692009-06-18 16:53:38 +0200821 {
Takashi Iwai8f230f42010-12-08 10:04:30 +0100822 .vendor = PCI_VENDOR_ID_JMICRON,
823 .device = PCI_DEVICE_ID_JMICRON_JMB388_SD,
824 .subvendor = PCI_ANY_ID,
825 .subdevice = PCI_ANY_ID,
826 .driver_data = (kernel_ulong_t)&sdhci_jmicron,
827 },
828
829 {
830 .vendor = PCI_VENDOR_ID_JMICRON,
831 .device = PCI_DEVICE_ID_JMICRON_JMB388_ESD,
832 .subvendor = PCI_ANY_ID,
833 .subdevice = PCI_ANY_ID,
834 .driver_data = (kernel_ulong_t)&sdhci_jmicron,
835 },
836
837 {
Nicolas Pitrea7a61862009-12-14 18:01:26 -0800838 .vendor = PCI_VENDOR_ID_SYSKONNECT,
839 .device = 0x8000,
840 .subvendor = PCI_ANY_ID,
841 .subdevice = PCI_ANY_ID,
842 .driver_data = (kernel_ulong_t)&sdhci_syskt,
843 },
844
845 {
Harald Welte557b0692009-06-18 16:53:38 +0200846 .vendor = PCI_VENDOR_ID_VIA,
847 .device = 0x95d0,
848 .subvendor = PCI_ANY_ID,
849 .subdevice = PCI_ANY_ID,
850 .driver_data = (kernel_ulong_t)&sdhci_via,
851 },
852
Xiaochen Shen29229052010-10-04 15:24:52 +0100853 {
854 .vendor = PCI_VENDOR_ID_INTEL,
Alan Coxf9ee3ea2010-10-04 15:25:11 +0100855 .device = PCI_DEVICE_ID_INTEL_MRST_SD0,
856 .subvendor = PCI_ANY_ID,
857 .subdevice = PCI_ANY_ID,
858 .driver_data = (kernel_ulong_t)&sdhci_intel_mrst_hc0,
859 },
860
861 {
862 .vendor = PCI_VENDOR_ID_INTEL,
863 .device = PCI_DEVICE_ID_INTEL_MRST_SD1,
864 .subvendor = PCI_ANY_ID,
865 .subdevice = PCI_ANY_ID,
Jacob Pan35ac6f02010-11-09 13:57:29 +0000866 .driver_data = (kernel_ulong_t)&sdhci_intel_mrst_hc1_hc2,
867 },
868
869 {
870 .vendor = PCI_VENDOR_ID_INTEL,
871 .device = PCI_DEVICE_ID_INTEL_MRST_SD2,
872 .subvendor = PCI_ANY_ID,
873 .subdevice = PCI_ANY_ID,
874 .driver_data = (kernel_ulong_t)&sdhci_intel_mrst_hc1_hc2,
Alan Coxf9ee3ea2010-10-04 15:25:11 +0100875 },
876
877 {
878 .vendor = PCI_VENDOR_ID_INTEL,
Xiaochen Shen29229052010-10-04 15:24:52 +0100879 .device = PCI_DEVICE_ID_INTEL_MFD_SD,
880 .subvendor = PCI_ANY_ID,
881 .subdevice = PCI_ANY_ID,
882 .driver_data = (kernel_ulong_t)&sdhci_intel_mfd_sd,
883 },
884
885 {
886 .vendor = PCI_VENDOR_ID_INTEL,
887 .device = PCI_DEVICE_ID_INTEL_MFD_SDIO1,
888 .subvendor = PCI_ANY_ID,
889 .subdevice = PCI_ANY_ID,
Adrian Hunter0d013bc2011-06-29 14:23:47 +0300890 .driver_data = (kernel_ulong_t)&sdhci_intel_mfd_sdio,
Xiaochen Shen29229052010-10-04 15:24:52 +0100891 },
892
893 {
894 .vendor = PCI_VENDOR_ID_INTEL,
895 .device = PCI_DEVICE_ID_INTEL_MFD_SDIO2,
896 .subvendor = PCI_ANY_ID,
897 .subdevice = PCI_ANY_ID,
Adrian Hunter0d013bc2011-06-29 14:23:47 +0300898 .driver_data = (kernel_ulong_t)&sdhci_intel_mfd_sdio,
Xiaochen Shen29229052010-10-04 15:24:52 +0100899 },
900
901 {
902 .vendor = PCI_VENDOR_ID_INTEL,
903 .device = PCI_DEVICE_ID_INTEL_MFD_EMMC0,
904 .subvendor = PCI_ANY_ID,
905 .subdevice = PCI_ANY_ID,
Adrian Hunter0d013bc2011-06-29 14:23:47 +0300906 .driver_data = (kernel_ulong_t)&sdhci_intel_mfd_emmc,
Xiaochen Shen29229052010-10-04 15:24:52 +0100907 },
908
909 {
910 .vendor = PCI_VENDOR_ID_INTEL,
911 .device = PCI_DEVICE_ID_INTEL_MFD_EMMC1,
912 .subvendor = PCI_ANY_ID,
913 .subdevice = PCI_ANY_ID,
Adrian Hunter0d013bc2011-06-29 14:23:47 +0300914 .driver_data = (kernel_ulong_t)&sdhci_intel_mfd_emmc,
Xiaochen Shen29229052010-10-04 15:24:52 +0100915 },
916
Jennifer Li26daa1e2010-11-17 23:01:59 -0500917 {
Alexander Stein296e0b02012-03-14 08:38:58 +0100918 .vendor = PCI_VENDOR_ID_INTEL,
919 .device = PCI_DEVICE_ID_INTEL_PCH_SDIO0,
920 .subvendor = PCI_ANY_ID,
921 .subdevice = PCI_ANY_ID,
922 .driver_data = (kernel_ulong_t)&sdhci_intel_pch_sdio,
923 },
924
925 {
926 .vendor = PCI_VENDOR_ID_INTEL,
927 .device = PCI_DEVICE_ID_INTEL_PCH_SDIO1,
928 .subvendor = PCI_ANY_ID,
929 .subdevice = PCI_ANY_ID,
930 .driver_data = (kernel_ulong_t)&sdhci_intel_pch_sdio,
931 },
932
933 {
Adrian Hunter728ef3d2013-04-26 11:27:23 +0300934 .vendor = PCI_VENDOR_ID_INTEL,
935 .device = PCI_DEVICE_ID_INTEL_BYT_EMMC,
936 .subvendor = PCI_ANY_ID,
937 .subdevice = PCI_ANY_ID,
938 .driver_data = (kernel_ulong_t)&sdhci_intel_byt_emmc,
939 },
940
941 {
942 .vendor = PCI_VENDOR_ID_INTEL,
943 .device = PCI_DEVICE_ID_INTEL_BYT_SDIO,
944 .subvendor = PCI_ANY_ID,
945 .subdevice = PCI_ANY_ID,
946 .driver_data = (kernel_ulong_t)&sdhci_intel_byt_sdio,
947 },
948
949 {
950 .vendor = PCI_VENDOR_ID_INTEL,
951 .device = PCI_DEVICE_ID_INTEL_BYT_SD,
952 .subvendor = PCI_ANY_ID,
953 .subdevice = PCI_ANY_ID,
954 .driver_data = (kernel_ulong_t)&sdhci_intel_byt_sd,
955 },
956
957 {
Adrian Hunter30d025c2013-06-20 12:57:59 +0300958 .vendor = PCI_VENDOR_ID_INTEL,
959 .device = PCI_DEVICE_ID_INTEL_BYT_EMMC2,
960 .subvendor = PCI_ANY_ID,
961 .subdevice = PCI_ANY_ID,
962 .driver_data = (kernel_ulong_t)&sdhci_intel_byt_emmc,
963 },
964
965 {
David Cohen8776a162013-10-01 13:18:15 -0700966 .vendor = PCI_VENDOR_ID_INTEL,
967 .device = PCI_DEVICE_ID_INTEL_MRFL_MMC,
968 .subvendor = PCI_ANY_ID,
969 .subdevice = PCI_ANY_ID,
970 .driver_data = (kernel_ulong_t)&sdhci_intel_mrfl_mmc,
971 },
972 {
Jennifer Li26daa1e2010-11-17 23:01:59 -0500973 .vendor = PCI_VENDOR_ID_O2,
974 .device = PCI_DEVICE_ID_O2_8120,
975 .subvendor = PCI_ANY_ID,
976 .subdevice = PCI_ANY_ID,
977 .driver_data = (kernel_ulong_t)&sdhci_o2,
978 },
979
980 {
981 .vendor = PCI_VENDOR_ID_O2,
982 .device = PCI_DEVICE_ID_O2_8220,
983 .subvendor = PCI_ANY_ID,
984 .subdevice = PCI_ANY_ID,
985 .driver_data = (kernel_ulong_t)&sdhci_o2,
986 },
987
988 {
989 .vendor = PCI_VENDOR_ID_O2,
990 .device = PCI_DEVICE_ID_O2_8221,
991 .subvendor = PCI_ANY_ID,
992 .subdevice = PCI_ANY_ID,
993 .driver_data = (kernel_ulong_t)&sdhci_o2,
994 },
995
996 {
997 .vendor = PCI_VENDOR_ID_O2,
998 .device = PCI_DEVICE_ID_O2_8320,
999 .subvendor = PCI_ANY_ID,
1000 .subdevice = PCI_ANY_ID,
1001 .driver_data = (kernel_ulong_t)&sdhci_o2,
1002 },
1003
1004 {
1005 .vendor = PCI_VENDOR_ID_O2,
1006 .device = PCI_DEVICE_ID_O2_8321,
1007 .subvendor = PCI_ANY_ID,
1008 .subdevice = PCI_ANY_ID,
1009 .driver_data = (kernel_ulong_t)&sdhci_o2,
1010 },
1011
Pierre Ossman22606402008-03-23 19:33:23 +01001012 { /* Generic SD host controller */
1013 PCI_DEVICE_CLASS((PCI_CLASS_SYSTEM_SDHCI << 8), 0xFFFF00)
1014 },
1015
1016 { /* end: all zeroes */ },
1017};
1018
1019MODULE_DEVICE_TABLE(pci, pci_ids);
1020
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001021/*****************************************************************************\
1022 * *
1023 * SDHCI core callbacks *
1024 * *
1025\*****************************************************************************/
1026
1027static int sdhci_pci_enable_dma(struct sdhci_host *host)
1028{
1029 struct sdhci_pci_slot *slot;
1030 struct pci_dev *pdev;
1031 int ret;
1032
1033 slot = sdhci_priv(host);
1034 pdev = slot->chip->pdev;
1035
1036 if (((pdev->class & 0xFFFF00) == (PCI_CLASS_SYSTEM_SDHCI << 8)) &&
1037 ((pdev->class & 0x0000FF) != PCI_SDHCI_IFDMA) &&
Richard Röjforsa13abc72009-09-22 16:45:30 -07001038 (host->flags & SDHCI_USE_SDMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001039 dev_warn(&pdev->dev, "Will use DMA mode even though HW "
1040 "doesn't fully claim to support it.\n");
1041 }
1042
Yang Hongyang284901a2009-04-06 19:01:15 -07001043 ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001044 if (ret)
1045 return ret;
1046
1047 pci_set_master(pdev);
1048
1049 return 0;
1050}
1051
Sascha Hauer7bc088d2013-01-21 19:02:27 +08001052static int sdhci_pci_bus_width(struct sdhci_host *host, int width)
Major Lee68077b02011-06-29 14:23:46 +03001053{
1054 u8 ctrl;
1055
1056 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
1057
1058 switch (width) {
1059 case MMC_BUS_WIDTH_8:
1060 ctrl |= SDHCI_CTRL_8BITBUS;
1061 ctrl &= ~SDHCI_CTRL_4BITBUS;
1062 break;
1063 case MMC_BUS_WIDTH_4:
1064 ctrl |= SDHCI_CTRL_4BITBUS;
1065 ctrl &= ~SDHCI_CTRL_8BITBUS;
1066 break;
1067 default:
1068 ctrl &= ~(SDHCI_CTRL_8BITBUS | SDHCI_CTRL_4BITBUS);
1069 break;
1070 }
1071
1072 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1073
1074 return 0;
1075}
1076
Adrian Hunterc9faff62013-06-13 11:50:26 +03001077static void sdhci_pci_gpio_hw_reset(struct sdhci_host *host)
Adrian Hunter0f201652011-08-29 16:42:13 +03001078{
1079 struct sdhci_pci_slot *slot = sdhci_priv(host);
1080 int rst_n_gpio = slot->rst_n_gpio;
1081
1082 if (!gpio_is_valid(rst_n_gpio))
1083 return;
1084 gpio_set_value_cansleep(rst_n_gpio, 0);
1085 /* For eMMC, minimum is 1us but give it 10us for good measure */
1086 udelay(10);
1087 gpio_set_value_cansleep(rst_n_gpio, 1);
1088 /* For eMMC, minimum is 200us but give it 300us for good measure */
1089 usleep_range(300, 1000);
1090}
1091
Adrian Hunterc9faff62013-06-13 11:50:26 +03001092static void sdhci_pci_hw_reset(struct sdhci_host *host)
1093{
1094 struct sdhci_pci_slot *slot = sdhci_priv(host);
1095
1096 if (slot->hw_reset)
1097 slot->hw_reset(host);
1098}
1099
Lars-Peter Clausenc9155682013-03-13 19:26:05 +01001100static const struct sdhci_ops sdhci_pci_ops = {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001101 .enable_dma = sdhci_pci_enable_dma,
Sascha Hauer7bc088d2013-01-21 19:02:27 +08001102 .platform_bus_width = sdhci_pci_bus_width,
Adrian Hunter0f201652011-08-29 16:42:13 +03001103 .hw_reset = sdhci_pci_hw_reset,
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001104};
1105
1106/*****************************************************************************\
1107 * *
1108 * Suspend/resume *
1109 * *
1110\*****************************************************************************/
1111
1112#ifdef CONFIG_PM
1113
Manuel Lauss29495aa2011-11-03 11:09:45 +01001114static int sdhci_pci_suspend(struct device *dev)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001115{
Manuel Lauss29495aa2011-11-03 11:09:45 +01001116 struct pci_dev *pdev = to_pci_dev(dev);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001117 struct sdhci_pci_chip *chip;
1118 struct sdhci_pci_slot *slot;
Daniel Drake5f619702010-11-04 22:20:39 +00001119 mmc_pm_flag_t slot_pm_flags;
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08001120 mmc_pm_flag_t pm_flags = 0;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001121 int i, ret;
1122
1123 chip = pci_get_drvdata(pdev);
1124 if (!chip)
1125 return 0;
1126
Ameya Palandeb177bc92011-04-05 21:13:13 +03001127 for (i = 0; i < chip->num_slots; i++) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001128 slot = chip->slots[i];
1129 if (!slot)
1130 continue;
1131
Manuel Lauss29495aa2011-11-03 11:09:45 +01001132 ret = sdhci_suspend_host(slot->host);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001133
Axel Linb678b912011-12-03 15:28:05 +08001134 if (ret)
1135 goto err_pci_suspend;
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08001136
Daniel Drake5f619702010-11-04 22:20:39 +00001137 slot_pm_flags = slot->host->mmc->pm_flags;
1138 if (slot_pm_flags & MMC_PM_WAKE_SDIO_IRQ)
1139 sdhci_enable_irq_wakeups(slot->host);
1140
1141 pm_flags |= slot_pm_flags;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001142 }
1143
Pierre Ossman44894282008-04-04 19:36:59 +02001144 if (chip->fixes && chip->fixes->suspend) {
Manuel Lauss29495aa2011-11-03 11:09:45 +01001145 ret = chip->fixes->suspend(chip);
Axel Linb678b912011-12-03 15:28:05 +08001146 if (ret)
1147 goto err_pci_suspend;
Pierre Ossman44894282008-04-04 19:36:59 +02001148 }
1149
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001150 pci_save_state(pdev);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08001151 if (pm_flags & MMC_PM_KEEP_POWER) {
Daniel Drake5f619702010-11-04 22:20:39 +00001152 if (pm_flags & MMC_PM_WAKE_SDIO_IRQ) {
1153 pci_pme_active(pdev, true);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08001154 pci_enable_wake(pdev, PCI_D3hot, 1);
Daniel Drake5f619702010-11-04 22:20:39 +00001155 }
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08001156 pci_set_power_state(pdev, PCI_D3hot);
1157 } else {
Manuel Lauss29495aa2011-11-03 11:09:45 +01001158 pci_enable_wake(pdev, PCI_D3hot, 0);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08001159 pci_disable_device(pdev);
Manuel Lauss29495aa2011-11-03 11:09:45 +01001160 pci_set_power_state(pdev, PCI_D3hot);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08001161 }
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001162
1163 return 0;
Axel Linb678b912011-12-03 15:28:05 +08001164
1165err_pci_suspend:
1166 while (--i >= 0)
1167 sdhci_resume_host(chip->slots[i]->host);
1168 return ret;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001169}
1170
Manuel Lauss29495aa2011-11-03 11:09:45 +01001171static int sdhci_pci_resume(struct device *dev)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001172{
Manuel Lauss29495aa2011-11-03 11:09:45 +01001173 struct pci_dev *pdev = to_pci_dev(dev);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001174 struct sdhci_pci_chip *chip;
1175 struct sdhci_pci_slot *slot;
1176 int i, ret;
1177
1178 chip = pci_get_drvdata(pdev);
1179 if (!chip)
1180 return 0;
1181
1182 pci_set_power_state(pdev, PCI_D0);
1183 pci_restore_state(pdev);
1184 ret = pci_enable_device(pdev);
1185 if (ret)
1186 return ret;
1187
Pierre Ossman45211e22008-03-24 13:09:09 +01001188 if (chip->fixes && chip->fixes->resume) {
1189 ret = chip->fixes->resume(chip);
1190 if (ret)
1191 return ret;
1192 }
1193
Ameya Palandeb177bc92011-04-05 21:13:13 +03001194 for (i = 0; i < chip->num_slots; i++) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001195 slot = chip->slots[i];
1196 if (!slot)
1197 continue;
1198
1199 ret = sdhci_resume_host(slot->host);
1200 if (ret)
1201 return ret;
1202 }
1203
1204 return 0;
1205}
1206
1207#else /* CONFIG_PM */
1208
1209#define sdhci_pci_suspend NULL
1210#define sdhci_pci_resume NULL
1211
1212#endif /* CONFIG_PM */
1213
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001214#ifdef CONFIG_PM_RUNTIME
1215
1216static int sdhci_pci_runtime_suspend(struct device *dev)
1217{
1218 struct pci_dev *pdev = container_of(dev, struct pci_dev, dev);
1219 struct sdhci_pci_chip *chip;
1220 struct sdhci_pci_slot *slot;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001221 int i, ret;
1222
1223 chip = pci_get_drvdata(pdev);
1224 if (!chip)
1225 return 0;
1226
1227 for (i = 0; i < chip->num_slots; i++) {
1228 slot = chip->slots[i];
1229 if (!slot)
1230 continue;
1231
1232 ret = sdhci_runtime_suspend_host(slot->host);
1233
Axel Linb678b912011-12-03 15:28:05 +08001234 if (ret)
1235 goto err_pci_runtime_suspend;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001236 }
1237
1238 if (chip->fixes && chip->fixes->suspend) {
Manuel Lauss29495aa2011-11-03 11:09:45 +01001239 ret = chip->fixes->suspend(chip);
Axel Linb678b912011-12-03 15:28:05 +08001240 if (ret)
1241 goto err_pci_runtime_suspend;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001242 }
1243
1244 return 0;
Axel Linb678b912011-12-03 15:28:05 +08001245
1246err_pci_runtime_suspend:
1247 while (--i >= 0)
1248 sdhci_runtime_resume_host(chip->slots[i]->host);
1249 return ret;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001250}
1251
1252static int sdhci_pci_runtime_resume(struct device *dev)
1253{
1254 struct pci_dev *pdev = container_of(dev, struct pci_dev, dev);
1255 struct sdhci_pci_chip *chip;
1256 struct sdhci_pci_slot *slot;
1257 int i, ret;
1258
1259 chip = pci_get_drvdata(pdev);
1260 if (!chip)
1261 return 0;
1262
1263 if (chip->fixes && chip->fixes->resume) {
1264 ret = chip->fixes->resume(chip);
1265 if (ret)
1266 return ret;
1267 }
1268
1269 for (i = 0; i < chip->num_slots; i++) {
1270 slot = chip->slots[i];
1271 if (!slot)
1272 continue;
1273
1274 ret = sdhci_runtime_resume_host(slot->host);
1275 if (ret)
1276 return ret;
1277 }
1278
1279 return 0;
1280}
1281
1282static int sdhci_pci_runtime_idle(struct device *dev)
1283{
1284 return 0;
1285}
1286
1287#else
1288
1289#define sdhci_pci_runtime_suspend NULL
1290#define sdhci_pci_runtime_resume NULL
1291#define sdhci_pci_runtime_idle NULL
1292
1293#endif
1294
1295static const struct dev_pm_ops sdhci_pci_pm_ops = {
Manuel Lauss29495aa2011-11-03 11:09:45 +01001296 .suspend = sdhci_pci_suspend,
1297 .resume = sdhci_pci_resume,
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001298 .runtime_suspend = sdhci_pci_runtime_suspend,
1299 .runtime_resume = sdhci_pci_runtime_resume,
1300 .runtime_idle = sdhci_pci_runtime_idle,
1301};
1302
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001303/*****************************************************************************\
1304 * *
1305 * Device probing/removal *
1306 * *
1307\*****************************************************************************/
1308
Bill Pembertonc3be1ef2012-11-19 13:23:06 -05001309static struct sdhci_pci_slot *sdhci_pci_probe_slot(
Adrian Hunter52c506f2011-12-27 15:48:43 +02001310 struct pci_dev *pdev, struct sdhci_pci_chip *chip, int first_bar,
1311 int slotno)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001312{
1313 struct sdhci_pci_slot *slot;
1314 struct sdhci_host *host;
Adrian Hunter52c506f2011-12-27 15:48:43 +02001315 int ret, bar = first_bar + slotno;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001316
1317 if (!(pci_resource_flags(pdev, bar) & IORESOURCE_MEM)) {
1318 dev_err(&pdev->dev, "BAR %d is not iomem. Aborting.\n", bar);
1319 return ERR_PTR(-ENODEV);
1320 }
1321
Adrian Hunter90b3e6c2012-10-18 09:54:31 +03001322 if (pci_resource_len(pdev, bar) < 0x100) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001323 dev_err(&pdev->dev, "Invalid iomem size. You may "
1324 "experience problems.\n");
1325 }
1326
1327 if ((pdev->class & 0x0000FF) == PCI_SDHCI_IFVENDOR) {
1328 dev_err(&pdev->dev, "Vendor specific interface. Aborting.\n");
1329 return ERR_PTR(-ENODEV);
1330 }
1331
1332 if ((pdev->class & 0x0000FF) > PCI_SDHCI_IFVENDOR) {
1333 dev_err(&pdev->dev, "Unknown interface. Aborting.\n");
1334 return ERR_PTR(-ENODEV);
1335 }
1336
1337 host = sdhci_alloc_host(&pdev->dev, sizeof(struct sdhci_pci_slot));
1338 if (IS_ERR(host)) {
Dan Carpenterc60a32c2009-04-10 23:31:10 +02001339 dev_err(&pdev->dev, "cannot allocate host\n");
Julia Lawalldc0fd7b2010-05-26 14:42:11 -07001340 return ERR_CAST(host);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001341 }
1342
1343 slot = sdhci_priv(host);
1344
1345 slot->chip = chip;
1346 slot->host = host;
1347 slot->pci_bar = bar;
Adrian Hunter0f201652011-08-29 16:42:13 +03001348 slot->rst_n_gpio = -EINVAL;
Adrian Hunterc5e027a2011-12-27 15:48:44 +02001349 slot->cd_gpio = -EINVAL;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001350
Adrian Hunter52c506f2011-12-27 15:48:43 +02001351 /* Retrieve platform data if there is any */
1352 if (*sdhci_pci_get_data)
1353 slot->data = sdhci_pci_get_data(pdev, slotno);
1354
1355 if (slot->data) {
1356 if (slot->data->setup) {
1357 ret = slot->data->setup(slot->data);
1358 if (ret) {
1359 dev_err(&pdev->dev, "platform setup failed\n");
1360 goto free;
1361 }
1362 }
Adrian Hunterc5e027a2011-12-27 15:48:44 +02001363 slot->rst_n_gpio = slot->data->rst_n_gpio;
1364 slot->cd_gpio = slot->data->cd_gpio;
Adrian Hunter52c506f2011-12-27 15:48:43 +02001365 }
1366
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001367 host->hw_name = "PCI";
1368 host->ops = &sdhci_pci_ops;
1369 host->quirks = chip->quirks;
Adrian Hunterf3c55a72012-02-07 14:48:55 +02001370 host->quirks2 = chip->quirks2;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001371
1372 host->irq = pdev->irq;
1373
1374 ret = pci_request_region(pdev, bar, mmc_hostname(host->mmc));
1375 if (ret) {
1376 dev_err(&pdev->dev, "cannot request region\n");
Adrian Hunter52c506f2011-12-27 15:48:43 +02001377 goto cleanup;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001378 }
1379
Arjan van de Ven092f82e2008-09-28 16:15:56 -07001380 host->ioaddr = pci_ioremap_bar(pdev, bar);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001381 if (!host->ioaddr) {
1382 dev_err(&pdev->dev, "failed to remap registers\n");
Chris Ball9fdcdbb2011-03-29 00:46:12 -04001383 ret = -ENOMEM;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001384 goto release;
1385 }
1386
Pierre Ossman44894282008-04-04 19:36:59 +02001387 if (chip->fixes && chip->fixes->probe_slot) {
1388 ret = chip->fixes->probe_slot(slot);
1389 if (ret)
1390 goto unmap;
1391 }
1392
Adrian Hunterc5e027a2011-12-27 15:48:44 +02001393 if (gpio_is_valid(slot->rst_n_gpio)) {
1394 if (!gpio_request(slot->rst_n_gpio, "eMMC_reset")) {
1395 gpio_direction_output(slot->rst_n_gpio, 1);
1396 slot->host->mmc->caps |= MMC_CAP_HW_RESET;
Adrian Hunterc9faff62013-06-13 11:50:26 +03001397 slot->hw_reset = sdhci_pci_gpio_hw_reset;
Adrian Hunterc5e027a2011-12-27 15:48:44 +02001398 } else {
1399 dev_warn(&pdev->dev, "failed to request rst_n_gpio\n");
1400 slot->rst_n_gpio = -EINVAL;
1401 }
1402 }
1403
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08001404 host->mmc->pm_caps = MMC_PM_KEEP_POWER | MMC_PM_WAKE_SDIO_IRQ;
Aaron Lueed222a2013-03-05 11:24:52 +08001405 host->mmc->slotno = slotno;
Adrian Huntera08b17b2013-04-15 11:27:25 -04001406 host->mmc->caps2 |= MMC_CAP2_NO_PRESCAN_POWERUP;
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08001407
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001408 ret = sdhci_add_host(host);
1409 if (ret)
Pierre Ossman44894282008-04-04 19:36:59 +02001410 goto remove;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001411
Adrian Hunterc5e027a2011-12-27 15:48:44 +02001412 sdhci_pci_add_own_cd(slot);
1413
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001414 return slot;
1415
Pierre Ossman44894282008-04-04 19:36:59 +02001416remove:
Adrian Hunterc5e027a2011-12-27 15:48:44 +02001417 if (gpio_is_valid(slot->rst_n_gpio))
1418 gpio_free(slot->rst_n_gpio);
1419
Pierre Ossman44894282008-04-04 19:36:59 +02001420 if (chip->fixes && chip->fixes->remove_slot)
Pierre Ossman1e728592008-04-16 19:13:13 +02001421 chip->fixes->remove_slot(slot, 0);
Pierre Ossman44894282008-04-04 19:36:59 +02001422
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001423unmap:
1424 iounmap(host->ioaddr);
1425
1426release:
1427 pci_release_region(pdev, bar);
Dan Carpenterc60a32c2009-04-10 23:31:10 +02001428
Adrian Hunter52c506f2011-12-27 15:48:43 +02001429cleanup:
1430 if (slot->data && slot->data->cleanup)
1431 slot->data->cleanup(slot->data);
1432
Dan Carpenterc60a32c2009-04-10 23:31:10 +02001433free:
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001434 sdhci_free_host(host);
1435
1436 return ERR_PTR(ret);
1437}
1438
1439static void sdhci_pci_remove_slot(struct sdhci_pci_slot *slot)
1440{
Pierre Ossman1e728592008-04-16 19:13:13 +02001441 int dead;
1442 u32 scratch;
1443
Adrian Hunterc5e027a2011-12-27 15:48:44 +02001444 sdhci_pci_remove_own_cd(slot);
1445
Pierre Ossman1e728592008-04-16 19:13:13 +02001446 dead = 0;
1447 scratch = readl(slot->host->ioaddr + SDHCI_INT_STATUS);
1448 if (scratch == (u32)-1)
1449 dead = 1;
1450
1451 sdhci_remove_host(slot->host, dead);
Pierre Ossman44894282008-04-04 19:36:59 +02001452
Adrian Hunterc5e027a2011-12-27 15:48:44 +02001453 if (gpio_is_valid(slot->rst_n_gpio))
1454 gpio_free(slot->rst_n_gpio);
1455
Pierre Ossman44894282008-04-04 19:36:59 +02001456 if (slot->chip->fixes && slot->chip->fixes->remove_slot)
Pierre Ossman1e728592008-04-16 19:13:13 +02001457 slot->chip->fixes->remove_slot(slot, dead);
Pierre Ossman44894282008-04-04 19:36:59 +02001458
Adrian Hunter52c506f2011-12-27 15:48:43 +02001459 if (slot->data && slot->data->cleanup)
1460 slot->data->cleanup(slot->data);
1461
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001462 pci_release_region(slot->chip->pdev, slot->pci_bar);
Pierre Ossman44894282008-04-04 19:36:59 +02001463
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001464 sdhci_free_host(slot->host);
1465}
1466
Bill Pembertonc3be1ef2012-11-19 13:23:06 -05001467static void sdhci_pci_runtime_pm_allow(struct device *dev)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001468{
1469 pm_runtime_put_noidle(dev);
1470 pm_runtime_allow(dev);
1471 pm_runtime_set_autosuspend_delay(dev, 50);
1472 pm_runtime_use_autosuspend(dev);
1473 pm_suspend_ignore_children(dev, 1);
1474}
1475
Bill Pemberton6e0ee712012-11-19 13:26:03 -05001476static void sdhci_pci_runtime_pm_forbid(struct device *dev)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001477{
1478 pm_runtime_forbid(dev);
1479 pm_runtime_get_noresume(dev);
1480}
1481
Bill Pembertonc3be1ef2012-11-19 13:23:06 -05001482static int sdhci_pci_probe(struct pci_dev *pdev,
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001483 const struct pci_device_id *ent)
1484{
1485 struct sdhci_pci_chip *chip;
1486 struct sdhci_pci_slot *slot;
1487
Sergei Shtylyovcf5e23e2011-03-17 16:46:17 -04001488 u8 slots, first_bar;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001489 int ret, i;
1490
1491 BUG_ON(pdev == NULL);
1492 BUG_ON(ent == NULL);
1493
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001494 dev_info(&pdev->dev, "SDHCI controller found [%04x:%04x] (rev %x)\n",
Sergei Shtylyovcf5e23e2011-03-17 16:46:17 -04001495 (int)pdev->vendor, (int)pdev->device, (int)pdev->revision);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001496
1497 ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &slots);
1498 if (ret)
1499 return ret;
1500
1501 slots = PCI_SLOT_INFO_SLOTS(slots) + 1;
1502 dev_dbg(&pdev->dev, "found %d slot(s)\n", slots);
1503 if (slots == 0)
1504 return -ENODEV;
1505
1506 BUG_ON(slots > MAX_SLOTS);
1507
1508 ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &first_bar);
1509 if (ret)
1510 return ret;
1511
1512 first_bar &= PCI_SLOT_INFO_FIRST_BAR_MASK;
1513
1514 if (first_bar > 5) {
1515 dev_err(&pdev->dev, "Invalid first BAR. Aborting.\n");
1516 return -ENODEV;
1517 }
1518
1519 ret = pci_enable_device(pdev);
1520 if (ret)
1521 return ret;
1522
1523 chip = kzalloc(sizeof(struct sdhci_pci_chip), GFP_KERNEL);
1524 if (!chip) {
1525 ret = -ENOMEM;
1526 goto err;
1527 }
1528
1529 chip->pdev = pdev;
Ameya Palandeb177bc92011-04-05 21:13:13 +03001530 chip->fixes = (const struct sdhci_pci_fixes *)ent->driver_data;
Adrian Hunterc43fd772011-10-17 10:52:44 +03001531 if (chip->fixes) {
Pierre Ossman22606402008-03-23 19:33:23 +01001532 chip->quirks = chip->fixes->quirks;
Adrian Hunterf3c55a72012-02-07 14:48:55 +02001533 chip->quirks2 = chip->fixes->quirks2;
Adrian Hunterc43fd772011-10-17 10:52:44 +03001534 chip->allow_runtime_pm = chip->fixes->allow_runtime_pm;
1535 }
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001536 chip->num_slots = slots;
1537
1538 pci_set_drvdata(pdev, chip);
1539
Pierre Ossman22606402008-03-23 19:33:23 +01001540 if (chip->fixes && chip->fixes->probe) {
1541 ret = chip->fixes->probe(chip);
1542 if (ret)
1543 goto free;
1544 }
1545
Alan Cox225d85f2010-10-04 15:24:21 +01001546 slots = chip->num_slots; /* Quirk may have changed this */
1547
Ameya Palandeb177bc92011-04-05 21:13:13 +03001548 for (i = 0; i < slots; i++) {
Adrian Hunter52c506f2011-12-27 15:48:43 +02001549 slot = sdhci_pci_probe_slot(pdev, chip, first_bar, i);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001550 if (IS_ERR(slot)) {
Ameya Palandeb177bc92011-04-05 21:13:13 +03001551 for (i--; i >= 0; i--)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001552 sdhci_pci_remove_slot(chip->slots[i]);
1553 ret = PTR_ERR(slot);
1554 goto free;
1555 }
1556
1557 chip->slots[i] = slot;
1558 }
1559
Adrian Hunterc43fd772011-10-17 10:52:44 +03001560 if (chip->allow_runtime_pm)
1561 sdhci_pci_runtime_pm_allow(&pdev->dev);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001562
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001563 return 0;
1564
1565free:
1566 pci_set_drvdata(pdev, NULL);
1567 kfree(chip);
1568
1569err:
1570 pci_disable_device(pdev);
1571 return ret;
1572}
1573
Bill Pemberton6e0ee712012-11-19 13:26:03 -05001574static void sdhci_pci_remove(struct pci_dev *pdev)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001575{
1576 int i;
1577 struct sdhci_pci_chip *chip;
1578
1579 chip = pci_get_drvdata(pdev);
1580
1581 if (chip) {
Adrian Hunterc43fd772011-10-17 10:52:44 +03001582 if (chip->allow_runtime_pm)
1583 sdhci_pci_runtime_pm_forbid(&pdev->dev);
1584
Ameya Palandeb177bc92011-04-05 21:13:13 +03001585 for (i = 0; i < chip->num_slots; i++)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001586 sdhci_pci_remove_slot(chip->slots[i]);
1587
1588 pci_set_drvdata(pdev, NULL);
1589 kfree(chip);
1590 }
1591
1592 pci_disable_device(pdev);
1593}
1594
1595static struct pci_driver sdhci_driver = {
Ameya Palandeb177bc92011-04-05 21:13:13 +03001596 .name = "sdhci-pci",
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001597 .id_table = pci_ids,
Ameya Palandeb177bc92011-04-05 21:13:13 +03001598 .probe = sdhci_pci_probe,
Bill Pemberton0433c142012-11-19 13:20:26 -05001599 .remove = sdhci_pci_remove,
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001600 .driver = {
1601 .pm = &sdhci_pci_pm_ops
1602 },
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001603};
1604
Sachin Kamatacc69642012-08-27 11:57:02 +05301605module_pci_driver(sdhci_driver);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001606
Pierre Ossman32710e82009-04-08 20:14:54 +02001607MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001608MODULE_DESCRIPTION("Secure Digital Host Controller Interface PCI driver");
1609MODULE_LICENSE("GPL");