blob: e082fac6bc96b5569a9294afcffac44040d08fa0 [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
Alexander Stein296e0b02012-03-14 08:38:58 +010039
40/*
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010041 * PCI registers
42 */
43
44#define PCI_SDHCI_IFPIO 0x00
45#define PCI_SDHCI_IFDMA 0x01
46#define PCI_SDHCI_IFVENDOR 0x02
47
48#define PCI_SLOT_INFO 0x40 /* 8 bits */
49#define PCI_SLOT_INFO_SLOTS(x) ((x >> 4) & 7)
50#define PCI_SLOT_INFO_FIRST_BAR_MASK 0x07
51
52#define MAX_SLOTS 8
53
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010054struct sdhci_pci_chip;
Pierre Ossman44894282008-04-04 19:36:59 +020055struct sdhci_pci_slot;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010056
Pierre Ossman22606402008-03-23 19:33:23 +010057struct sdhci_pci_fixes {
58 unsigned int quirks;
Adrian Hunterf3c55a72012-02-07 14:48:55 +020059 unsigned int quirks2;
Adrian Hunterc43fd772011-10-17 10:52:44 +030060 bool allow_runtime_pm;
Pierre Ossman22606402008-03-23 19:33:23 +010061
Ameya Palandeb177bc92011-04-05 21:13:13 +030062 int (*probe) (struct sdhci_pci_chip *);
Pierre Ossman45211e22008-03-24 13:09:09 +010063
Ameya Palandeb177bc92011-04-05 21:13:13 +030064 int (*probe_slot) (struct sdhci_pci_slot *);
65 void (*remove_slot) (struct sdhci_pci_slot *, int);
Pierre Ossman44894282008-04-04 19:36:59 +020066
Manuel Lauss29495aa2011-11-03 11:09:45 +010067 int (*suspend) (struct sdhci_pci_chip *);
Ameya Palandeb177bc92011-04-05 21:13:13 +030068 int (*resume) (struct sdhci_pci_chip *);
Pierre Ossman22606402008-03-23 19:33:23 +010069};
70
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010071struct sdhci_pci_slot {
72 struct sdhci_pci_chip *chip;
73 struct sdhci_host *host;
Adrian Hunter52c506f2011-12-27 15:48:43 +020074 struct sdhci_pci_data *data;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010075
76 int pci_bar;
Adrian Hunter0f201652011-08-29 16:42:13 +030077 int rst_n_gpio;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030078 int cd_gpio;
79 int cd_irq;
Adrian Hunterc9faff62013-06-13 11:50:26 +030080
81 void (*hw_reset)(struct sdhci_host *host);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010082};
83
84struct sdhci_pci_chip {
85 struct pci_dev *pdev;
Pierre Ossman22606402008-03-23 19:33:23 +010086
87 unsigned int quirks;
Adrian Hunterf3c55a72012-02-07 14:48:55 +020088 unsigned int quirks2;
Adrian Hunterc43fd772011-10-17 10:52:44 +030089 bool allow_runtime_pm;
Pierre Ossman22606402008-03-23 19:33:23 +010090 const struct sdhci_pci_fixes *fixes;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010091
92 int num_slots; /* Slots on controller */
93 struct sdhci_pci_slot *slots[MAX_SLOTS]; /* Pointers to host slots */
94};
95
Pierre Ossman22606402008-03-23 19:33:23 +010096
97/*****************************************************************************\
98 * *
99 * Hardware specific quirk handling *
100 * *
101\*****************************************************************************/
102
103static int ricoh_probe(struct sdhci_pci_chip *chip)
104{
Chris Ballc99436f2009-09-22 16:45:22 -0700105 if (chip->pdev->subsystem_vendor == PCI_VENDOR_ID_SAMSUNG ||
106 chip->pdev->subsystem_vendor == PCI_VENDOR_ID_SONY)
Pierre Ossman22606402008-03-23 19:33:23 +0100107 chip->quirks |= SDHCI_QUIRK_NO_CARD_NO_RESET;
Maxim Levitskyccc92c22010-08-10 18:01:42 -0700108 return 0;
109}
Pierre Ossman22606402008-03-23 19:33:23 +0100110
Maxim Levitskyccc92c22010-08-10 18:01:42 -0700111static int ricoh_mmc_probe_slot(struct sdhci_pci_slot *slot)
112{
113 slot->host->caps =
114 ((0x21 << SDHCI_TIMEOUT_CLK_SHIFT)
115 & SDHCI_TIMEOUT_CLK_MASK) |
116
117 ((0x21 << SDHCI_CLOCK_BASE_SHIFT)
118 & SDHCI_CLOCK_BASE_MASK) |
119
120 SDHCI_TIMEOUT_CLK_UNIT |
121 SDHCI_CAN_VDD_330 |
Madhvapathi Sriram1a1f1f02012-10-15 04:47:30 +0000122 SDHCI_CAN_DO_HISPD |
Maxim Levitskyccc92c22010-08-10 18:01:42 -0700123 SDHCI_CAN_DO_SDMA;
124 return 0;
125}
126
127static int ricoh_mmc_resume(struct sdhci_pci_chip *chip)
128{
129 /* Apply a delay to allow controller to settle */
130 /* Otherwise it becomes confused if card state changed
131 during suspend */
132 msleep(500);
Pierre Ossman22606402008-03-23 19:33:23 +0100133 return 0;
134}
135
136static const struct sdhci_pci_fixes sdhci_ricoh = {
137 .probe = ricoh_probe,
Vasily Khoruzhick84938292010-03-05 13:43:46 -0800138 .quirks = SDHCI_QUIRK_32BIT_DMA_ADDR |
139 SDHCI_QUIRK_FORCE_DMA |
140 SDHCI_QUIRK_CLOCK_BEFORE_RESET,
Pierre Ossman22606402008-03-23 19:33:23 +0100141};
142
Maxim Levitskyccc92c22010-08-10 18:01:42 -0700143static const struct sdhci_pci_fixes sdhci_ricoh_mmc = {
144 .probe_slot = ricoh_mmc_probe_slot,
145 .resume = ricoh_mmc_resume,
146 .quirks = SDHCI_QUIRK_32BIT_DMA_ADDR |
147 SDHCI_QUIRK_CLOCK_BEFORE_RESET |
148 SDHCI_QUIRK_NO_CARD_NO_RESET |
149 SDHCI_QUIRK_MISSING_CAPS
150};
151
Pierre Ossman22606402008-03-23 19:33:23 +0100152static const struct sdhci_pci_fixes sdhci_ene_712 = {
153 .quirks = SDHCI_QUIRK_SINGLE_POWER_WRITE |
154 SDHCI_QUIRK_BROKEN_DMA,
155};
156
157static const struct sdhci_pci_fixes sdhci_ene_714 = {
158 .quirks = SDHCI_QUIRK_SINGLE_POWER_WRITE |
159 SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS |
160 SDHCI_QUIRK_BROKEN_DMA,
161};
162
163static const struct sdhci_pci_fixes sdhci_cafe = {
164 .quirks = SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER |
Andres Salomona0874892009-03-02 21:48:20 +0100165 SDHCI_QUIRK_NO_BUSY_IRQ |
Daniel Drake55fc05b2012-07-03 23:13:39 +0100166 SDHCI_QUIRK_BROKEN_CARD_DETECTION |
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200167 SDHCI_QUIRK_BROKEN_TIMEOUT_VAL,
Pierre Ossman22606402008-03-23 19:33:23 +0100168};
169
Major Lee68077b02011-06-29 14:23:46 +0300170static int mrst_hc_probe_slot(struct sdhci_pci_slot *slot)
171{
172 slot->host->mmc->caps |= MMC_CAP_8_BIT_DATA;
173 return 0;
174}
175
Alan Coxf9ee3ea2010-10-04 15:25:11 +0100176/*
177 * ADMA operation is disabled for Moorestown platform due to
178 * hardware bugs.
179 */
Jacob Pan35ac6f02010-11-09 13:57:29 +0000180static int mrst_hc_probe(struct sdhci_pci_chip *chip)
Alan Coxf9ee3ea2010-10-04 15:25:11 +0100181{
182 /*
Jacob Pan35ac6f02010-11-09 13:57:29 +0000183 * slots number is fixed here for MRST as SDIO3/5 are never used and
184 * have hardware bugs.
Alan Coxf9ee3ea2010-10-04 15:25:11 +0100185 */
186 chip->num_slots = 1;
187 return 0;
188}
189
Alexander Stein296e0b02012-03-14 08:38:58 +0100190static int pch_hc_probe_slot(struct sdhci_pci_slot *slot)
191{
192 slot->host->mmc->caps |= MMC_CAP_8_BIT_DATA;
193 return 0;
194}
195
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300196#ifdef CONFIG_PM_RUNTIME
197
Adrian Hunterc5e027a2011-12-27 15:48:44 +0200198static irqreturn_t sdhci_pci_sd_cd(int irq, void *dev_id)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300199{
200 struct sdhci_pci_slot *slot = dev_id;
201 struct sdhci_host *host = slot->host;
202
203 mmc_detect_change(host->mmc, msecs_to_jiffies(200));
204 return IRQ_HANDLED;
205}
206
Adrian Hunterc5e027a2011-12-27 15:48:44 +0200207static void sdhci_pci_add_own_cd(struct sdhci_pci_slot *slot)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300208{
Adrian Hunterc5e027a2011-12-27 15:48:44 +0200209 int err, irq, gpio = slot->cd_gpio;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300210
211 slot->cd_gpio = -EINVAL;
212 slot->cd_irq = -EINVAL;
213
Adrian Hunterc5e027a2011-12-27 15:48:44 +0200214 if (!gpio_is_valid(gpio))
215 return;
216
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300217 err = gpio_request(gpio, "sd_cd");
218 if (err < 0)
219 goto out;
220
221 err = gpio_direction_input(gpio);
222 if (err < 0)
223 goto out_free;
224
225 irq = gpio_to_irq(gpio);
226 if (irq < 0)
227 goto out_free;
228
Adrian Hunterc5e027a2011-12-27 15:48:44 +0200229 err = request_irq(irq, sdhci_pci_sd_cd, IRQF_TRIGGER_RISING |
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300230 IRQF_TRIGGER_FALLING, "sd_cd", slot);
231 if (err)
232 goto out_free;
233
234 slot->cd_gpio = gpio;
235 slot->cd_irq = irq;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300236
Adrian Hunterc5e027a2011-12-27 15:48:44 +0200237 return;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300238
239out_free:
240 gpio_free(gpio);
241out:
242 dev_warn(&slot->chip->pdev->dev, "failed to setup card detect wake up\n");
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300243}
244
Adrian Hunterc5e027a2011-12-27 15:48:44 +0200245static void sdhci_pci_remove_own_cd(struct sdhci_pci_slot *slot)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300246{
247 if (slot->cd_irq >= 0)
248 free_irq(slot->cd_irq, slot);
Adrian Hunterc5e027a2011-12-27 15:48:44 +0200249 if (gpio_is_valid(slot->cd_gpio))
250 gpio_free(slot->cd_gpio);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300251}
252
253#else
254
Adrian Hunterc5e027a2011-12-27 15:48:44 +0200255static inline void sdhci_pci_add_own_cd(struct sdhci_pci_slot *slot)
256{
257}
258
259static inline void sdhci_pci_remove_own_cd(struct sdhci_pci_slot *slot)
260{
261}
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300262
263#endif
264
Adrian Hunter0d013bc2011-06-29 14:23:47 +0300265static int mfd_emmc_probe_slot(struct sdhci_pci_slot *slot)
266{
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300267 slot->host->mmc->caps |= MMC_CAP_8_BIT_DATA | MMC_CAP_NONREMOVABLE;
Adrian Hunterda721cf2012-02-07 14:48:53 +0200268 slot->host->mmc->caps2 |= MMC_CAP2_BOOTPART_NOACC |
269 MMC_CAP2_HC_ERASE_SZ;
Adrian Hunter0d013bc2011-06-29 14:23:47 +0300270 return 0;
271}
272
Adrian Hunter93933502011-12-27 15:48:47 +0200273static int mfd_sdio_probe_slot(struct sdhci_pci_slot *slot)
274{
Adrian Hunter012e4672012-01-30 14:27:18 +0200275 slot->host->mmc->caps |= MMC_CAP_POWER_OFF_CARD | MMC_CAP_NONREMOVABLE;
Adrian Hunter93933502011-12-27 15:48:47 +0200276 return 0;
277}
278
Alan Coxf9ee3ea2010-10-04 15:25:11 +0100279static const struct sdhci_pci_fixes sdhci_intel_mrst_hc0 = {
280 .quirks = SDHCI_QUIRK_BROKEN_ADMA | SDHCI_QUIRK_NO_HISPD_BIT,
Major Lee68077b02011-06-29 14:23:46 +0300281 .probe_slot = mrst_hc_probe_slot,
Alan Coxf9ee3ea2010-10-04 15:25:11 +0100282};
283
Jacob Pan35ac6f02010-11-09 13:57:29 +0000284static const struct sdhci_pci_fixes sdhci_intel_mrst_hc1_hc2 = {
Alan Coxf9ee3ea2010-10-04 15:25:11 +0100285 .quirks = SDHCI_QUIRK_BROKEN_ADMA | SDHCI_QUIRK_NO_HISPD_BIT,
Jacob Pan35ac6f02010-11-09 13:57:29 +0000286 .probe = mrst_hc_probe,
Alan Coxf9ee3ea2010-10-04 15:25:11 +0100287};
288
Xiaochen Shen29229052010-10-04 15:24:52 +0100289static const struct sdhci_pci_fixes sdhci_intel_mfd_sd = {
290 .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
Adrian Hunterc43fd772011-10-17 10:52:44 +0300291 .allow_runtime_pm = true,
Xiaochen Shen29229052010-10-04 15:24:52 +0100292};
293
Adrian Hunter0d013bc2011-06-29 14:23:47 +0300294static const struct sdhci_pci_fixes sdhci_intel_mfd_sdio = {
Xiaochen Shen29229052010-10-04 15:24:52 +0100295 .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
Adrian Hunterf3c55a72012-02-07 14:48:55 +0200296 .quirks2 = SDHCI_QUIRK2_HOST_OFF_CARD_ON,
Adrian Hunterc43fd772011-10-17 10:52:44 +0300297 .allow_runtime_pm = true,
Adrian Hunter93933502011-12-27 15:48:47 +0200298 .probe_slot = mfd_sdio_probe_slot,
Xiaochen Shen29229052010-10-04 15:24:52 +0100299};
300
Adrian Hunter0d013bc2011-06-29 14:23:47 +0300301static const struct sdhci_pci_fixes sdhci_intel_mfd_emmc = {
302 .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
Adrian Hunterc43fd772011-10-17 10:52:44 +0300303 .allow_runtime_pm = true,
Adrian Hunter0d013bc2011-06-29 14:23:47 +0300304 .probe_slot = mfd_emmc_probe_slot,
305};
306
Alexander Stein296e0b02012-03-14 08:38:58 +0100307static const struct sdhci_pci_fixes sdhci_intel_pch_sdio = {
308 .quirks = SDHCI_QUIRK_BROKEN_ADMA,
309 .probe_slot = pch_hc_probe_slot,
310};
311
Adrian Hunterc9faff62013-06-13 11:50:26 +0300312static void sdhci_pci_int_hw_reset(struct sdhci_host *host)
313{
314 u8 reg;
315
316 reg = sdhci_readb(host, SDHCI_POWER_CONTROL);
317 reg |= 0x10;
318 sdhci_writeb(host, reg, SDHCI_POWER_CONTROL);
319 /* For eMMC, minimum is 1us but give it 9us for good measure */
320 udelay(9);
321 reg &= ~0x10;
322 sdhci_writeb(host, reg, SDHCI_POWER_CONTROL);
323 /* For eMMC, minimum is 200us but give it 300us for good measure */
324 usleep_range(300, 1000);
325}
326
Adrian Hunter728ef3d2013-04-26 11:27:23 +0300327static int byt_emmc_probe_slot(struct sdhci_pci_slot *slot)
328{
Adrian Hunterc9faff62013-06-13 11:50:26 +0300329 slot->host->mmc->caps |= MMC_CAP_8_BIT_DATA | MMC_CAP_NONREMOVABLE |
330 MMC_CAP_HW_RESET;
Adrian Hunter728ef3d2013-04-26 11:27:23 +0300331 slot->host->mmc->caps2 |= MMC_CAP2_HC_ERASE_SZ;
Adrian Hunterc9faff62013-06-13 11:50:26 +0300332 slot->hw_reset = sdhci_pci_int_hw_reset;
Adrian Hunter728ef3d2013-04-26 11:27:23 +0300333 return 0;
334}
335
336static int byt_sdio_probe_slot(struct sdhci_pci_slot *slot)
337{
338 slot->host->mmc->caps |= MMC_CAP_POWER_OFF_CARD | MMC_CAP_NONREMOVABLE;
339 return 0;
340}
341
342static const struct sdhci_pci_fixes sdhci_intel_byt_emmc = {
343 .allow_runtime_pm = true,
344 .probe_slot = byt_emmc_probe_slot,
345};
346
347static const struct sdhci_pci_fixes sdhci_intel_byt_sdio = {
348 .quirks2 = SDHCI_QUIRK2_HOST_OFF_CARD_ON,
349 .allow_runtime_pm = true,
350 .probe_slot = byt_sdio_probe_slot,
351};
352
353static const struct sdhci_pci_fixes sdhci_intel_byt_sd = {
Adrian Hunter7396e312013-05-06 12:17:34 +0300354 .quirks2 = SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON,
355 .allow_runtime_pm = true,
Adrian Hunter728ef3d2013-04-26 11:27:23 +0300356};
357
Jennifer Li26daa1e2010-11-17 23:01:59 -0500358/* O2Micro extra registers */
359#define O2_SD_LOCK_WP 0xD3
360#define O2_SD_MULTI_VCC3V 0xEE
361#define O2_SD_CLKREQ 0xEC
362#define O2_SD_CAPS 0xE0
363#define O2_SD_ADMA1 0xE2
364#define O2_SD_ADMA2 0xE7
365#define O2_SD_INF_MOD 0xF1
366
367static int o2_probe(struct sdhci_pci_chip *chip)
368{
369 int ret;
370 u8 scratch;
371
372 switch (chip->pdev->device) {
373 case PCI_DEVICE_ID_O2_8220:
374 case PCI_DEVICE_ID_O2_8221:
375 case PCI_DEVICE_ID_O2_8320:
376 case PCI_DEVICE_ID_O2_8321:
377 /* This extra setup is required due to broken ADMA. */
378 ret = pci_read_config_byte(chip->pdev, O2_SD_LOCK_WP, &scratch);
379 if (ret)
380 return ret;
381 scratch &= 0x7f;
382 pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch);
383
384 /* Set Multi 3 to VCC3V# */
385 pci_write_config_byte(chip->pdev, O2_SD_MULTI_VCC3V, 0x08);
386
387 /* Disable CLK_REQ# support after media DET */
388 ret = pci_read_config_byte(chip->pdev, O2_SD_CLKREQ, &scratch);
389 if (ret)
390 return ret;
391 scratch |= 0x20;
392 pci_write_config_byte(chip->pdev, O2_SD_CLKREQ, scratch);
393
394 /* Choose capabilities, enable SDMA. We have to write 0x01
395 * to the capabilities register first to unlock it.
396 */
397 ret = pci_read_config_byte(chip->pdev, O2_SD_CAPS, &scratch);
398 if (ret)
399 return ret;
400 scratch |= 0x01;
401 pci_write_config_byte(chip->pdev, O2_SD_CAPS, scratch);
402 pci_write_config_byte(chip->pdev, O2_SD_CAPS, 0x73);
403
404 /* Disable ADMA1/2 */
405 pci_write_config_byte(chip->pdev, O2_SD_ADMA1, 0x39);
406 pci_write_config_byte(chip->pdev, O2_SD_ADMA2, 0x08);
407
408 /* Disable the infinite transfer mode */
409 ret = pci_read_config_byte(chip->pdev, O2_SD_INF_MOD, &scratch);
410 if (ret)
411 return ret;
412 scratch |= 0x08;
413 pci_write_config_byte(chip->pdev, O2_SD_INF_MOD, scratch);
414
415 /* Lock WP */
416 ret = pci_read_config_byte(chip->pdev, O2_SD_LOCK_WP, &scratch);
417 if (ret)
418 return ret;
419 scratch |= 0x80;
420 pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch);
421 }
422
423 return 0;
424}
425
Pierre Ossman45211e22008-03-24 13:09:09 +0100426static int jmicron_pmos(struct sdhci_pci_chip *chip, int on)
427{
428 u8 scratch;
429 int ret;
430
431 ret = pci_read_config_byte(chip->pdev, 0xAE, &scratch);
432 if (ret)
433 return ret;
434
435 /*
436 * Turn PMOS on [bit 0], set over current detection to 2.4 V
437 * [bit 1:2] and enable over current debouncing [bit 6].
438 */
439 if (on)
440 scratch |= 0x47;
441 else
442 scratch &= ~0x47;
443
444 ret = pci_write_config_byte(chip->pdev, 0xAE, scratch);
445 if (ret)
446 return ret;
447
448 return 0;
449}
450
451static int jmicron_probe(struct sdhci_pci_chip *chip)
452{
453 int ret;
Takashi Iwai8f230f42010-12-08 10:04:30 +0100454 u16 mmcdev = 0;
Pierre Ossman45211e22008-03-24 13:09:09 +0100455
Pierre Ossman93fc48c2008-06-28 18:21:41 +0200456 if (chip->pdev->revision == 0) {
457 chip->quirks |= SDHCI_QUIRK_32BIT_DMA_ADDR |
458 SDHCI_QUIRK_32BIT_DMA_SIZE |
Pierre Ossman2134a922008-06-28 18:28:51 +0200459 SDHCI_QUIRK_32BIT_ADMA_SIZE |
Pierre Ossman4a3cba32008-07-29 00:11:16 +0200460 SDHCI_QUIRK_RESET_AFTER_REQUEST |
Pierre Ossman86a6a872009-02-02 21:13:49 +0100461 SDHCI_QUIRK_BROKEN_SMALL_PIO;
Pierre Ossman93fc48c2008-06-28 18:21:41 +0200462 }
463
Pierre Ossman45211e22008-03-24 13:09:09 +0100464 /*
Pierre Ossman44894282008-04-04 19:36:59 +0200465 * JMicron chips can have two interfaces to the same hardware
466 * in order to work around limitations in Microsoft's driver.
467 * We need to make sure we only bind to one of them.
468 *
469 * This code assumes two things:
470 *
471 * 1. The PCI code adds subfunctions in order.
472 *
473 * 2. The MMC interface has a lower subfunction number
474 * than the SD interface.
475 */
Takashi Iwai8f230f42010-12-08 10:04:30 +0100476 if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_SD)
477 mmcdev = PCI_DEVICE_ID_JMICRON_JMB38X_MMC;
478 else if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_SD)
479 mmcdev = PCI_DEVICE_ID_JMICRON_JMB388_ESD;
480
481 if (mmcdev) {
Pierre Ossman44894282008-04-04 19:36:59 +0200482 struct pci_dev *sd_dev;
483
484 sd_dev = NULL;
485 while ((sd_dev = pci_get_device(PCI_VENDOR_ID_JMICRON,
Takashi Iwai8f230f42010-12-08 10:04:30 +0100486 mmcdev, sd_dev)) != NULL) {
Pierre Ossman44894282008-04-04 19:36:59 +0200487 if ((PCI_SLOT(chip->pdev->devfn) ==
488 PCI_SLOT(sd_dev->devfn)) &&
489 (chip->pdev->bus == sd_dev->bus))
490 break;
491 }
492
493 if (sd_dev) {
494 pci_dev_put(sd_dev);
495 dev_info(&chip->pdev->dev, "Refusing to bind to "
496 "secondary interface.\n");
497 return -ENODEV;
498 }
499 }
500
501 /*
Pierre Ossman45211e22008-03-24 13:09:09 +0100502 * JMicron chips need a bit of a nudge to enable the power
503 * output pins.
504 */
505 ret = jmicron_pmos(chip, 1);
506 if (ret) {
507 dev_err(&chip->pdev->dev, "Failure enabling card power\n");
508 return ret;
509 }
510
Takashi Iwai82b0e232011-04-21 20:26:38 +0200511 /* quirk for unsable RO-detection on JM388 chips */
512 if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_SD ||
513 chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD)
514 chip->quirks |= SDHCI_QUIRK_UNSTABLE_RO_DETECT;
515
Pierre Ossman45211e22008-03-24 13:09:09 +0100516 return 0;
517}
518
Pierre Ossman44894282008-04-04 19:36:59 +0200519static void jmicron_enable_mmc(struct sdhci_host *host, int on)
520{
521 u8 scratch;
522
523 scratch = readb(host->ioaddr + 0xC0);
524
525 if (on)
526 scratch |= 0x01;
527 else
528 scratch &= ~0x01;
529
530 writeb(scratch, host->ioaddr + 0xC0);
531}
532
533static int jmicron_probe_slot(struct sdhci_pci_slot *slot)
534{
Pierre Ossman2134a922008-06-28 18:28:51 +0200535 if (slot->chip->pdev->revision == 0) {
536 u16 version;
537
538 version = readl(slot->host->ioaddr + SDHCI_HOST_VERSION);
539 version = (version & SDHCI_VENDOR_VER_MASK) >>
540 SDHCI_VENDOR_VER_SHIFT;
541
542 /*
543 * Older versions of the chip have lots of nasty glitches
544 * in the ADMA engine. It's best just to avoid it
545 * completely.
546 */
547 if (version < 0xAC)
548 slot->host->quirks |= SDHCI_QUIRK_BROKEN_ADMA;
549 }
550
Takashi Iwai8f230f42010-12-08 10:04:30 +0100551 /* JM388 MMC doesn't support 1.8V while SD supports it */
552 if (slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD) {
553 slot->host->ocr_avail_sd = MMC_VDD_32_33 | MMC_VDD_33_34 |
554 MMC_VDD_29_30 | MMC_VDD_30_31 |
555 MMC_VDD_165_195; /* allow 1.8V */
556 slot->host->ocr_avail_mmc = MMC_VDD_32_33 | MMC_VDD_33_34 |
557 MMC_VDD_29_30 | MMC_VDD_30_31; /* no 1.8V for MMC */
558 }
559
Pierre Ossman44894282008-04-04 19:36:59 +0200560 /*
561 * The secondary interface requires a bit set to get the
562 * interrupts.
563 */
Takashi Iwai8f230f42010-12-08 10:04:30 +0100564 if (slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC ||
565 slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD)
Pierre Ossman44894282008-04-04 19:36:59 +0200566 jmicron_enable_mmc(slot->host, 1);
567
Takashi Iwaid75c1082010-12-16 17:54:14 +0100568 slot->host->mmc->caps |= MMC_CAP_BUS_WIDTH_TEST;
569
Pierre Ossman44894282008-04-04 19:36:59 +0200570 return 0;
571}
572
Pierre Ossman1e728592008-04-16 19:13:13 +0200573static void jmicron_remove_slot(struct sdhci_pci_slot *slot, int dead)
Pierre Ossman44894282008-04-04 19:36:59 +0200574{
Pierre Ossman1e728592008-04-16 19:13:13 +0200575 if (dead)
576 return;
577
Takashi Iwai8f230f42010-12-08 10:04:30 +0100578 if (slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC ||
579 slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD)
Pierre Ossman44894282008-04-04 19:36:59 +0200580 jmicron_enable_mmc(slot->host, 0);
581}
582
Manuel Lauss29495aa2011-11-03 11:09:45 +0100583static int jmicron_suspend(struct sdhci_pci_chip *chip)
Pierre Ossman44894282008-04-04 19:36:59 +0200584{
585 int i;
586
Takashi Iwai8f230f42010-12-08 10:04:30 +0100587 if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC ||
588 chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD) {
Ameya Palandeb177bc92011-04-05 21:13:13 +0300589 for (i = 0; i < chip->num_slots; i++)
Pierre Ossman44894282008-04-04 19:36:59 +0200590 jmicron_enable_mmc(chip->slots[i]->host, 0);
591 }
592
593 return 0;
594}
595
Pierre Ossman45211e22008-03-24 13:09:09 +0100596static int jmicron_resume(struct sdhci_pci_chip *chip)
597{
Pierre Ossman44894282008-04-04 19:36:59 +0200598 int ret, i;
599
Takashi Iwai8f230f42010-12-08 10:04:30 +0100600 if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC ||
601 chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD) {
Ameya Palandeb177bc92011-04-05 21:13:13 +0300602 for (i = 0; i < chip->num_slots; i++)
Pierre Ossman44894282008-04-04 19:36:59 +0200603 jmicron_enable_mmc(chip->slots[i]->host, 1);
604 }
Pierre Ossman45211e22008-03-24 13:09:09 +0100605
606 ret = jmicron_pmos(chip, 1);
607 if (ret) {
608 dev_err(&chip->pdev->dev, "Failure enabling card power\n");
609 return ret;
610 }
611
612 return 0;
613}
614
Jennifer Li26daa1e2010-11-17 23:01:59 -0500615static const struct sdhci_pci_fixes sdhci_o2 = {
616 .probe = o2_probe,
617};
618
Pierre Ossman22606402008-03-23 19:33:23 +0100619static const struct sdhci_pci_fixes sdhci_jmicron = {
Pierre Ossman45211e22008-03-24 13:09:09 +0100620 .probe = jmicron_probe,
621
Pierre Ossman44894282008-04-04 19:36:59 +0200622 .probe_slot = jmicron_probe_slot,
623 .remove_slot = jmicron_remove_slot,
624
625 .suspend = jmicron_suspend,
Pierre Ossman45211e22008-03-24 13:09:09 +0100626 .resume = jmicron_resume,
Pierre Ossman22606402008-03-23 19:33:23 +0100627};
628
Nicolas Pitrea7a61862009-12-14 18:01:26 -0800629/* SysKonnect CardBus2SDIO extra registers */
630#define SYSKT_CTRL 0x200
631#define SYSKT_RDFIFO_STAT 0x204
632#define SYSKT_WRFIFO_STAT 0x208
633#define SYSKT_POWER_DATA 0x20c
634#define SYSKT_POWER_330 0xef
635#define SYSKT_POWER_300 0xf8
636#define SYSKT_POWER_184 0xcc
637#define SYSKT_POWER_CMD 0x20d
638#define SYSKT_POWER_START (1 << 7)
639#define SYSKT_POWER_STATUS 0x20e
640#define SYSKT_POWER_STATUS_OK (1 << 0)
641#define SYSKT_BOARD_REV 0x210
642#define SYSKT_CHIP_REV 0x211
643#define SYSKT_CONF_DATA 0x212
644#define SYSKT_CONF_DATA_1V8 (1 << 2)
645#define SYSKT_CONF_DATA_2V5 (1 << 1)
646#define SYSKT_CONF_DATA_3V3 (1 << 0)
647
648static int syskt_probe(struct sdhci_pci_chip *chip)
649{
650 if ((chip->pdev->class & 0x0000FF) == PCI_SDHCI_IFVENDOR) {
651 chip->pdev->class &= ~0x0000FF;
652 chip->pdev->class |= PCI_SDHCI_IFDMA;
653 }
654 return 0;
655}
656
657static int syskt_probe_slot(struct sdhci_pci_slot *slot)
658{
659 int tm, ps;
660
661 u8 board_rev = readb(slot->host->ioaddr + SYSKT_BOARD_REV);
662 u8 chip_rev = readb(slot->host->ioaddr + SYSKT_CHIP_REV);
663 dev_info(&slot->chip->pdev->dev, "SysKonnect CardBus2SDIO, "
664 "board rev %d.%d, chip rev %d.%d\n",
665 board_rev >> 4, board_rev & 0xf,
666 chip_rev >> 4, chip_rev & 0xf);
667 if (chip_rev >= 0x20)
668 slot->host->quirks |= SDHCI_QUIRK_FORCE_DMA;
669
670 writeb(SYSKT_POWER_330, slot->host->ioaddr + SYSKT_POWER_DATA);
671 writeb(SYSKT_POWER_START, slot->host->ioaddr + SYSKT_POWER_CMD);
672 udelay(50);
673 tm = 10; /* Wait max 1 ms */
674 do {
675 ps = readw(slot->host->ioaddr + SYSKT_POWER_STATUS);
676 if (ps & SYSKT_POWER_STATUS_OK)
677 break;
678 udelay(100);
679 } while (--tm);
680 if (!tm) {
681 dev_err(&slot->chip->pdev->dev,
682 "power regulator never stabilized");
683 writeb(0, slot->host->ioaddr + SYSKT_POWER_CMD);
684 return -ENODEV;
685 }
686
687 return 0;
688}
689
690static const struct sdhci_pci_fixes sdhci_syskt = {
691 .quirks = SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER,
692 .probe = syskt_probe,
693 .probe_slot = syskt_probe_slot,
694};
695
Harald Welte557b0692009-06-18 16:53:38 +0200696static int via_probe(struct sdhci_pci_chip *chip)
697{
698 if (chip->pdev->revision == 0x10)
699 chip->quirks |= SDHCI_QUIRK_DELAY_AFTER_POWER;
700
701 return 0;
702}
703
704static const struct sdhci_pci_fixes sdhci_via = {
705 .probe = via_probe,
706};
707
Bill Pemberton9647f842012-11-19 13:25:11 -0500708static const struct pci_device_id pci_ids[] = {
Pierre Ossman22606402008-03-23 19:33:23 +0100709 {
710 .vendor = PCI_VENDOR_ID_RICOH,
711 .device = PCI_DEVICE_ID_RICOH_R5C822,
712 .subvendor = PCI_ANY_ID,
713 .subdevice = PCI_ANY_ID,
714 .driver_data = (kernel_ulong_t)&sdhci_ricoh,
715 },
716
717 {
Maxim Levitskyccc92c22010-08-10 18:01:42 -0700718 .vendor = PCI_VENDOR_ID_RICOH,
719 .device = 0x843,
720 .subvendor = PCI_ANY_ID,
721 .subdevice = PCI_ANY_ID,
722 .driver_data = (kernel_ulong_t)&sdhci_ricoh_mmc,
723 },
724
725 {
Pablo Castillo568133e2010-08-10 18:02:01 -0700726 .vendor = PCI_VENDOR_ID_RICOH,
727 .device = 0xe822,
728 .subvendor = PCI_ANY_ID,
729 .subdevice = PCI_ANY_ID,
730 .driver_data = (kernel_ulong_t)&sdhci_ricoh_mmc,
731 },
732
733 {
Manoj Iyer5fd11c02011-02-11 16:25:31 -0600734 .vendor = PCI_VENDOR_ID_RICOH,
735 .device = 0xe823,
736 .subvendor = PCI_ANY_ID,
737 .subdevice = PCI_ANY_ID,
738 .driver_data = (kernel_ulong_t)&sdhci_ricoh_mmc,
739 },
740
741 {
Pierre Ossman22606402008-03-23 19:33:23 +0100742 .vendor = PCI_VENDOR_ID_ENE,
743 .device = PCI_DEVICE_ID_ENE_CB712_SD,
744 .subvendor = PCI_ANY_ID,
745 .subdevice = PCI_ANY_ID,
746 .driver_data = (kernel_ulong_t)&sdhci_ene_712,
747 },
748
749 {
750 .vendor = PCI_VENDOR_ID_ENE,
751 .device = PCI_DEVICE_ID_ENE_CB712_SD_2,
752 .subvendor = PCI_ANY_ID,
753 .subdevice = PCI_ANY_ID,
754 .driver_data = (kernel_ulong_t)&sdhci_ene_712,
755 },
756
757 {
758 .vendor = PCI_VENDOR_ID_ENE,
759 .device = PCI_DEVICE_ID_ENE_CB714_SD,
760 .subvendor = PCI_ANY_ID,
761 .subdevice = PCI_ANY_ID,
762 .driver_data = (kernel_ulong_t)&sdhci_ene_714,
763 },
764
765 {
766 .vendor = PCI_VENDOR_ID_ENE,
767 .device = PCI_DEVICE_ID_ENE_CB714_SD_2,
768 .subvendor = PCI_ANY_ID,
769 .subdevice = PCI_ANY_ID,
770 .driver_data = (kernel_ulong_t)&sdhci_ene_714,
771 },
772
773 {
774 .vendor = PCI_VENDOR_ID_MARVELL,
David Woodhouse8c5eb882008-09-03 09:45:57 +0100775 .device = PCI_DEVICE_ID_MARVELL_88ALP01_SD,
Pierre Ossman22606402008-03-23 19:33:23 +0100776 .subvendor = PCI_ANY_ID,
777 .subdevice = PCI_ANY_ID,
778 .driver_data = (kernel_ulong_t)&sdhci_cafe,
779 },
780
781 {
782 .vendor = PCI_VENDOR_ID_JMICRON,
783 .device = PCI_DEVICE_ID_JMICRON_JMB38X_SD,
784 .subvendor = PCI_ANY_ID,
785 .subdevice = PCI_ANY_ID,
786 .driver_data = (kernel_ulong_t)&sdhci_jmicron,
787 },
788
Pierre Ossman44894282008-04-04 19:36:59 +0200789 {
790 .vendor = PCI_VENDOR_ID_JMICRON,
791 .device = PCI_DEVICE_ID_JMICRON_JMB38X_MMC,
792 .subvendor = PCI_ANY_ID,
793 .subdevice = PCI_ANY_ID,
794 .driver_data = (kernel_ulong_t)&sdhci_jmicron,
795 },
796
Harald Welte557b0692009-06-18 16:53:38 +0200797 {
Takashi Iwai8f230f42010-12-08 10:04:30 +0100798 .vendor = PCI_VENDOR_ID_JMICRON,
799 .device = PCI_DEVICE_ID_JMICRON_JMB388_SD,
800 .subvendor = PCI_ANY_ID,
801 .subdevice = PCI_ANY_ID,
802 .driver_data = (kernel_ulong_t)&sdhci_jmicron,
803 },
804
805 {
806 .vendor = PCI_VENDOR_ID_JMICRON,
807 .device = PCI_DEVICE_ID_JMICRON_JMB388_ESD,
808 .subvendor = PCI_ANY_ID,
809 .subdevice = PCI_ANY_ID,
810 .driver_data = (kernel_ulong_t)&sdhci_jmicron,
811 },
812
813 {
Nicolas Pitrea7a61862009-12-14 18:01:26 -0800814 .vendor = PCI_VENDOR_ID_SYSKONNECT,
815 .device = 0x8000,
816 .subvendor = PCI_ANY_ID,
817 .subdevice = PCI_ANY_ID,
818 .driver_data = (kernel_ulong_t)&sdhci_syskt,
819 },
820
821 {
Harald Welte557b0692009-06-18 16:53:38 +0200822 .vendor = PCI_VENDOR_ID_VIA,
823 .device = 0x95d0,
824 .subvendor = PCI_ANY_ID,
825 .subdevice = PCI_ANY_ID,
826 .driver_data = (kernel_ulong_t)&sdhci_via,
827 },
828
Xiaochen Shen29229052010-10-04 15:24:52 +0100829 {
830 .vendor = PCI_VENDOR_ID_INTEL,
Alan Coxf9ee3ea2010-10-04 15:25:11 +0100831 .device = PCI_DEVICE_ID_INTEL_MRST_SD0,
832 .subvendor = PCI_ANY_ID,
833 .subdevice = PCI_ANY_ID,
834 .driver_data = (kernel_ulong_t)&sdhci_intel_mrst_hc0,
835 },
836
837 {
838 .vendor = PCI_VENDOR_ID_INTEL,
839 .device = PCI_DEVICE_ID_INTEL_MRST_SD1,
840 .subvendor = PCI_ANY_ID,
841 .subdevice = PCI_ANY_ID,
Jacob Pan35ac6f02010-11-09 13:57:29 +0000842 .driver_data = (kernel_ulong_t)&sdhci_intel_mrst_hc1_hc2,
843 },
844
845 {
846 .vendor = PCI_VENDOR_ID_INTEL,
847 .device = PCI_DEVICE_ID_INTEL_MRST_SD2,
848 .subvendor = PCI_ANY_ID,
849 .subdevice = PCI_ANY_ID,
850 .driver_data = (kernel_ulong_t)&sdhci_intel_mrst_hc1_hc2,
Alan Coxf9ee3ea2010-10-04 15:25:11 +0100851 },
852
853 {
854 .vendor = PCI_VENDOR_ID_INTEL,
Xiaochen Shen29229052010-10-04 15:24:52 +0100855 .device = PCI_DEVICE_ID_INTEL_MFD_SD,
856 .subvendor = PCI_ANY_ID,
857 .subdevice = PCI_ANY_ID,
858 .driver_data = (kernel_ulong_t)&sdhci_intel_mfd_sd,
859 },
860
861 {
862 .vendor = PCI_VENDOR_ID_INTEL,
863 .device = PCI_DEVICE_ID_INTEL_MFD_SDIO1,
864 .subvendor = PCI_ANY_ID,
865 .subdevice = PCI_ANY_ID,
Adrian Hunter0d013bc2011-06-29 14:23:47 +0300866 .driver_data = (kernel_ulong_t)&sdhci_intel_mfd_sdio,
Xiaochen Shen29229052010-10-04 15:24:52 +0100867 },
868
869 {
870 .vendor = PCI_VENDOR_ID_INTEL,
871 .device = PCI_DEVICE_ID_INTEL_MFD_SDIO2,
872 .subvendor = PCI_ANY_ID,
873 .subdevice = PCI_ANY_ID,
Adrian Hunter0d013bc2011-06-29 14:23:47 +0300874 .driver_data = (kernel_ulong_t)&sdhci_intel_mfd_sdio,
Xiaochen Shen29229052010-10-04 15:24:52 +0100875 },
876
877 {
878 .vendor = PCI_VENDOR_ID_INTEL,
879 .device = PCI_DEVICE_ID_INTEL_MFD_EMMC0,
880 .subvendor = PCI_ANY_ID,
881 .subdevice = PCI_ANY_ID,
Adrian Hunter0d013bc2011-06-29 14:23:47 +0300882 .driver_data = (kernel_ulong_t)&sdhci_intel_mfd_emmc,
Xiaochen Shen29229052010-10-04 15:24:52 +0100883 },
884
885 {
886 .vendor = PCI_VENDOR_ID_INTEL,
887 .device = PCI_DEVICE_ID_INTEL_MFD_EMMC1,
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_emmc,
Xiaochen Shen29229052010-10-04 15:24:52 +0100891 },
892
Jennifer Li26daa1e2010-11-17 23:01:59 -0500893 {
Alexander Stein296e0b02012-03-14 08:38:58 +0100894 .vendor = PCI_VENDOR_ID_INTEL,
895 .device = PCI_DEVICE_ID_INTEL_PCH_SDIO0,
896 .subvendor = PCI_ANY_ID,
897 .subdevice = PCI_ANY_ID,
898 .driver_data = (kernel_ulong_t)&sdhci_intel_pch_sdio,
899 },
900
901 {
902 .vendor = PCI_VENDOR_ID_INTEL,
903 .device = PCI_DEVICE_ID_INTEL_PCH_SDIO1,
904 .subvendor = PCI_ANY_ID,
905 .subdevice = PCI_ANY_ID,
906 .driver_data = (kernel_ulong_t)&sdhci_intel_pch_sdio,
907 },
908
909 {
Adrian Hunter728ef3d2013-04-26 11:27:23 +0300910 .vendor = PCI_VENDOR_ID_INTEL,
911 .device = PCI_DEVICE_ID_INTEL_BYT_EMMC,
912 .subvendor = PCI_ANY_ID,
913 .subdevice = PCI_ANY_ID,
914 .driver_data = (kernel_ulong_t)&sdhci_intel_byt_emmc,
915 },
916
917 {
918 .vendor = PCI_VENDOR_ID_INTEL,
919 .device = PCI_DEVICE_ID_INTEL_BYT_SDIO,
920 .subvendor = PCI_ANY_ID,
921 .subdevice = PCI_ANY_ID,
922 .driver_data = (kernel_ulong_t)&sdhci_intel_byt_sdio,
923 },
924
925 {
926 .vendor = PCI_VENDOR_ID_INTEL,
927 .device = PCI_DEVICE_ID_INTEL_BYT_SD,
928 .subvendor = PCI_ANY_ID,
929 .subdevice = PCI_ANY_ID,
930 .driver_data = (kernel_ulong_t)&sdhci_intel_byt_sd,
931 },
932
933 {
Jennifer Li26daa1e2010-11-17 23:01:59 -0500934 .vendor = PCI_VENDOR_ID_O2,
935 .device = PCI_DEVICE_ID_O2_8120,
936 .subvendor = PCI_ANY_ID,
937 .subdevice = PCI_ANY_ID,
938 .driver_data = (kernel_ulong_t)&sdhci_o2,
939 },
940
941 {
942 .vendor = PCI_VENDOR_ID_O2,
943 .device = PCI_DEVICE_ID_O2_8220,
944 .subvendor = PCI_ANY_ID,
945 .subdevice = PCI_ANY_ID,
946 .driver_data = (kernel_ulong_t)&sdhci_o2,
947 },
948
949 {
950 .vendor = PCI_VENDOR_ID_O2,
951 .device = PCI_DEVICE_ID_O2_8221,
952 .subvendor = PCI_ANY_ID,
953 .subdevice = PCI_ANY_ID,
954 .driver_data = (kernel_ulong_t)&sdhci_o2,
955 },
956
957 {
958 .vendor = PCI_VENDOR_ID_O2,
959 .device = PCI_DEVICE_ID_O2_8320,
960 .subvendor = PCI_ANY_ID,
961 .subdevice = PCI_ANY_ID,
962 .driver_data = (kernel_ulong_t)&sdhci_o2,
963 },
964
965 {
966 .vendor = PCI_VENDOR_ID_O2,
967 .device = PCI_DEVICE_ID_O2_8321,
968 .subvendor = PCI_ANY_ID,
969 .subdevice = PCI_ANY_ID,
970 .driver_data = (kernel_ulong_t)&sdhci_o2,
971 },
972
Pierre Ossman22606402008-03-23 19:33:23 +0100973 { /* Generic SD host controller */
974 PCI_DEVICE_CLASS((PCI_CLASS_SYSTEM_SDHCI << 8), 0xFFFF00)
975 },
976
977 { /* end: all zeroes */ },
978};
979
980MODULE_DEVICE_TABLE(pci, pci_ids);
981
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100982/*****************************************************************************\
983 * *
984 * SDHCI core callbacks *
985 * *
986\*****************************************************************************/
987
988static int sdhci_pci_enable_dma(struct sdhci_host *host)
989{
990 struct sdhci_pci_slot *slot;
991 struct pci_dev *pdev;
992 int ret;
993
994 slot = sdhci_priv(host);
995 pdev = slot->chip->pdev;
996
997 if (((pdev->class & 0xFFFF00) == (PCI_CLASS_SYSTEM_SDHCI << 8)) &&
998 ((pdev->class & 0x0000FF) != PCI_SDHCI_IFDMA) &&
Richard Röjforsa13abc72009-09-22 16:45:30 -0700999 (host->flags & SDHCI_USE_SDMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001000 dev_warn(&pdev->dev, "Will use DMA mode even though HW "
1001 "doesn't fully claim to support it.\n");
1002 }
1003
Yang Hongyang284901a2009-04-06 19:01:15 -07001004 ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001005 if (ret)
1006 return ret;
1007
1008 pci_set_master(pdev);
1009
1010 return 0;
1011}
1012
Sascha Hauer7bc088d2013-01-21 19:02:27 +08001013static int sdhci_pci_bus_width(struct sdhci_host *host, int width)
Major Lee68077b02011-06-29 14:23:46 +03001014{
1015 u8 ctrl;
1016
1017 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
1018
1019 switch (width) {
1020 case MMC_BUS_WIDTH_8:
1021 ctrl |= SDHCI_CTRL_8BITBUS;
1022 ctrl &= ~SDHCI_CTRL_4BITBUS;
1023 break;
1024 case MMC_BUS_WIDTH_4:
1025 ctrl |= SDHCI_CTRL_4BITBUS;
1026 ctrl &= ~SDHCI_CTRL_8BITBUS;
1027 break;
1028 default:
1029 ctrl &= ~(SDHCI_CTRL_8BITBUS | SDHCI_CTRL_4BITBUS);
1030 break;
1031 }
1032
1033 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1034
1035 return 0;
1036}
1037
Adrian Hunterc9faff62013-06-13 11:50:26 +03001038static void sdhci_pci_gpio_hw_reset(struct sdhci_host *host)
Adrian Hunter0f201652011-08-29 16:42:13 +03001039{
1040 struct sdhci_pci_slot *slot = sdhci_priv(host);
1041 int rst_n_gpio = slot->rst_n_gpio;
1042
1043 if (!gpio_is_valid(rst_n_gpio))
1044 return;
1045 gpio_set_value_cansleep(rst_n_gpio, 0);
1046 /* For eMMC, minimum is 1us but give it 10us for good measure */
1047 udelay(10);
1048 gpio_set_value_cansleep(rst_n_gpio, 1);
1049 /* For eMMC, minimum is 200us but give it 300us for good measure */
1050 usleep_range(300, 1000);
1051}
1052
Adrian Hunterc9faff62013-06-13 11:50:26 +03001053static void sdhci_pci_hw_reset(struct sdhci_host *host)
1054{
1055 struct sdhci_pci_slot *slot = sdhci_priv(host);
1056
1057 if (slot->hw_reset)
1058 slot->hw_reset(host);
1059}
1060
Lars-Peter Clausenc9155682013-03-13 19:26:05 +01001061static const struct sdhci_ops sdhci_pci_ops = {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001062 .enable_dma = sdhci_pci_enable_dma,
Sascha Hauer7bc088d2013-01-21 19:02:27 +08001063 .platform_bus_width = sdhci_pci_bus_width,
Adrian Hunter0f201652011-08-29 16:42:13 +03001064 .hw_reset = sdhci_pci_hw_reset,
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001065};
1066
1067/*****************************************************************************\
1068 * *
1069 * Suspend/resume *
1070 * *
1071\*****************************************************************************/
1072
1073#ifdef CONFIG_PM
1074
Manuel Lauss29495aa2011-11-03 11:09:45 +01001075static int sdhci_pci_suspend(struct device *dev)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001076{
Manuel Lauss29495aa2011-11-03 11:09:45 +01001077 struct pci_dev *pdev = to_pci_dev(dev);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001078 struct sdhci_pci_chip *chip;
1079 struct sdhci_pci_slot *slot;
Daniel Drake5f619702010-11-04 22:20:39 +00001080 mmc_pm_flag_t slot_pm_flags;
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08001081 mmc_pm_flag_t pm_flags = 0;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001082 int i, ret;
1083
1084 chip = pci_get_drvdata(pdev);
1085 if (!chip)
1086 return 0;
1087
Ameya Palandeb177bc92011-04-05 21:13:13 +03001088 for (i = 0; i < chip->num_slots; i++) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001089 slot = chip->slots[i];
1090 if (!slot)
1091 continue;
1092
Manuel Lauss29495aa2011-11-03 11:09:45 +01001093 ret = sdhci_suspend_host(slot->host);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001094
Axel Linb678b912011-12-03 15:28:05 +08001095 if (ret)
1096 goto err_pci_suspend;
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08001097
Daniel Drake5f619702010-11-04 22:20:39 +00001098 slot_pm_flags = slot->host->mmc->pm_flags;
1099 if (slot_pm_flags & MMC_PM_WAKE_SDIO_IRQ)
1100 sdhci_enable_irq_wakeups(slot->host);
1101
1102 pm_flags |= slot_pm_flags;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001103 }
1104
Pierre Ossman44894282008-04-04 19:36:59 +02001105 if (chip->fixes && chip->fixes->suspend) {
Manuel Lauss29495aa2011-11-03 11:09:45 +01001106 ret = chip->fixes->suspend(chip);
Axel Linb678b912011-12-03 15:28:05 +08001107 if (ret)
1108 goto err_pci_suspend;
Pierre Ossman44894282008-04-04 19:36:59 +02001109 }
1110
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001111 pci_save_state(pdev);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08001112 if (pm_flags & MMC_PM_KEEP_POWER) {
Daniel Drake5f619702010-11-04 22:20:39 +00001113 if (pm_flags & MMC_PM_WAKE_SDIO_IRQ) {
1114 pci_pme_active(pdev, true);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08001115 pci_enable_wake(pdev, PCI_D3hot, 1);
Daniel Drake5f619702010-11-04 22:20:39 +00001116 }
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08001117 pci_set_power_state(pdev, PCI_D3hot);
1118 } else {
Manuel Lauss29495aa2011-11-03 11:09:45 +01001119 pci_enable_wake(pdev, PCI_D3hot, 0);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08001120 pci_disable_device(pdev);
Manuel Lauss29495aa2011-11-03 11:09:45 +01001121 pci_set_power_state(pdev, PCI_D3hot);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08001122 }
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001123
1124 return 0;
Axel Linb678b912011-12-03 15:28:05 +08001125
1126err_pci_suspend:
1127 while (--i >= 0)
1128 sdhci_resume_host(chip->slots[i]->host);
1129 return ret;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001130}
1131
Manuel Lauss29495aa2011-11-03 11:09:45 +01001132static int sdhci_pci_resume(struct device *dev)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001133{
Manuel Lauss29495aa2011-11-03 11:09:45 +01001134 struct pci_dev *pdev = to_pci_dev(dev);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001135 struct sdhci_pci_chip *chip;
1136 struct sdhci_pci_slot *slot;
1137 int i, ret;
1138
1139 chip = pci_get_drvdata(pdev);
1140 if (!chip)
1141 return 0;
1142
1143 pci_set_power_state(pdev, PCI_D0);
1144 pci_restore_state(pdev);
1145 ret = pci_enable_device(pdev);
1146 if (ret)
1147 return ret;
1148
Pierre Ossman45211e22008-03-24 13:09:09 +01001149 if (chip->fixes && chip->fixes->resume) {
1150 ret = chip->fixes->resume(chip);
1151 if (ret)
1152 return ret;
1153 }
1154
Ameya Palandeb177bc92011-04-05 21:13:13 +03001155 for (i = 0; i < chip->num_slots; i++) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001156 slot = chip->slots[i];
1157 if (!slot)
1158 continue;
1159
1160 ret = sdhci_resume_host(slot->host);
1161 if (ret)
1162 return ret;
1163 }
1164
1165 return 0;
1166}
1167
1168#else /* CONFIG_PM */
1169
1170#define sdhci_pci_suspend NULL
1171#define sdhci_pci_resume NULL
1172
1173#endif /* CONFIG_PM */
1174
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001175#ifdef CONFIG_PM_RUNTIME
1176
1177static int sdhci_pci_runtime_suspend(struct device *dev)
1178{
1179 struct pci_dev *pdev = container_of(dev, struct pci_dev, dev);
1180 struct sdhci_pci_chip *chip;
1181 struct sdhci_pci_slot *slot;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001182 int i, ret;
1183
1184 chip = pci_get_drvdata(pdev);
1185 if (!chip)
1186 return 0;
1187
1188 for (i = 0; i < chip->num_slots; i++) {
1189 slot = chip->slots[i];
1190 if (!slot)
1191 continue;
1192
1193 ret = sdhci_runtime_suspend_host(slot->host);
1194
Axel Linb678b912011-12-03 15:28:05 +08001195 if (ret)
1196 goto err_pci_runtime_suspend;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001197 }
1198
1199 if (chip->fixes && chip->fixes->suspend) {
Manuel Lauss29495aa2011-11-03 11:09:45 +01001200 ret = chip->fixes->suspend(chip);
Axel Linb678b912011-12-03 15:28:05 +08001201 if (ret)
1202 goto err_pci_runtime_suspend;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001203 }
1204
1205 return 0;
Axel Linb678b912011-12-03 15:28:05 +08001206
1207err_pci_runtime_suspend:
1208 while (--i >= 0)
1209 sdhci_runtime_resume_host(chip->slots[i]->host);
1210 return ret;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001211}
1212
1213static int sdhci_pci_runtime_resume(struct device *dev)
1214{
1215 struct pci_dev *pdev = container_of(dev, struct pci_dev, dev);
1216 struct sdhci_pci_chip *chip;
1217 struct sdhci_pci_slot *slot;
1218 int i, ret;
1219
1220 chip = pci_get_drvdata(pdev);
1221 if (!chip)
1222 return 0;
1223
1224 if (chip->fixes && chip->fixes->resume) {
1225 ret = chip->fixes->resume(chip);
1226 if (ret)
1227 return ret;
1228 }
1229
1230 for (i = 0; i < chip->num_slots; i++) {
1231 slot = chip->slots[i];
1232 if (!slot)
1233 continue;
1234
1235 ret = sdhci_runtime_resume_host(slot->host);
1236 if (ret)
1237 return ret;
1238 }
1239
1240 return 0;
1241}
1242
1243static int sdhci_pci_runtime_idle(struct device *dev)
1244{
1245 return 0;
1246}
1247
1248#else
1249
1250#define sdhci_pci_runtime_suspend NULL
1251#define sdhci_pci_runtime_resume NULL
1252#define sdhci_pci_runtime_idle NULL
1253
1254#endif
1255
1256static const struct dev_pm_ops sdhci_pci_pm_ops = {
Manuel Lauss29495aa2011-11-03 11:09:45 +01001257 .suspend = sdhci_pci_suspend,
1258 .resume = sdhci_pci_resume,
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001259 .runtime_suspend = sdhci_pci_runtime_suspend,
1260 .runtime_resume = sdhci_pci_runtime_resume,
1261 .runtime_idle = sdhci_pci_runtime_idle,
1262};
1263
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001264/*****************************************************************************\
1265 * *
1266 * Device probing/removal *
1267 * *
1268\*****************************************************************************/
1269
Bill Pembertonc3be1ef2012-11-19 13:23:06 -05001270static struct sdhci_pci_slot *sdhci_pci_probe_slot(
Adrian Hunter52c506f2011-12-27 15:48:43 +02001271 struct pci_dev *pdev, struct sdhci_pci_chip *chip, int first_bar,
1272 int slotno)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001273{
1274 struct sdhci_pci_slot *slot;
1275 struct sdhci_host *host;
Adrian Hunter52c506f2011-12-27 15:48:43 +02001276 int ret, bar = first_bar + slotno;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001277
1278 if (!(pci_resource_flags(pdev, bar) & IORESOURCE_MEM)) {
1279 dev_err(&pdev->dev, "BAR %d is not iomem. Aborting.\n", bar);
1280 return ERR_PTR(-ENODEV);
1281 }
1282
Adrian Hunter90b3e6c2012-10-18 09:54:31 +03001283 if (pci_resource_len(pdev, bar) < 0x100) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001284 dev_err(&pdev->dev, "Invalid iomem size. You may "
1285 "experience problems.\n");
1286 }
1287
1288 if ((pdev->class & 0x0000FF) == PCI_SDHCI_IFVENDOR) {
1289 dev_err(&pdev->dev, "Vendor specific interface. Aborting.\n");
1290 return ERR_PTR(-ENODEV);
1291 }
1292
1293 if ((pdev->class & 0x0000FF) > PCI_SDHCI_IFVENDOR) {
1294 dev_err(&pdev->dev, "Unknown interface. Aborting.\n");
1295 return ERR_PTR(-ENODEV);
1296 }
1297
1298 host = sdhci_alloc_host(&pdev->dev, sizeof(struct sdhci_pci_slot));
1299 if (IS_ERR(host)) {
Dan Carpenterc60a32c2009-04-10 23:31:10 +02001300 dev_err(&pdev->dev, "cannot allocate host\n");
Julia Lawalldc0fd7b2010-05-26 14:42:11 -07001301 return ERR_CAST(host);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001302 }
1303
1304 slot = sdhci_priv(host);
1305
1306 slot->chip = chip;
1307 slot->host = host;
1308 slot->pci_bar = bar;
Adrian Hunter0f201652011-08-29 16:42:13 +03001309 slot->rst_n_gpio = -EINVAL;
Adrian Hunterc5e027a2011-12-27 15:48:44 +02001310 slot->cd_gpio = -EINVAL;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001311
Adrian Hunter52c506f2011-12-27 15:48:43 +02001312 /* Retrieve platform data if there is any */
1313 if (*sdhci_pci_get_data)
1314 slot->data = sdhci_pci_get_data(pdev, slotno);
1315
1316 if (slot->data) {
1317 if (slot->data->setup) {
1318 ret = slot->data->setup(slot->data);
1319 if (ret) {
1320 dev_err(&pdev->dev, "platform setup failed\n");
1321 goto free;
1322 }
1323 }
Adrian Hunterc5e027a2011-12-27 15:48:44 +02001324 slot->rst_n_gpio = slot->data->rst_n_gpio;
1325 slot->cd_gpio = slot->data->cd_gpio;
Adrian Hunter52c506f2011-12-27 15:48:43 +02001326 }
1327
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001328 host->hw_name = "PCI";
1329 host->ops = &sdhci_pci_ops;
1330 host->quirks = chip->quirks;
Adrian Hunterf3c55a72012-02-07 14:48:55 +02001331 host->quirks2 = chip->quirks2;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001332
1333 host->irq = pdev->irq;
1334
1335 ret = pci_request_region(pdev, bar, mmc_hostname(host->mmc));
1336 if (ret) {
1337 dev_err(&pdev->dev, "cannot request region\n");
Adrian Hunter52c506f2011-12-27 15:48:43 +02001338 goto cleanup;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001339 }
1340
Arjan van de Ven092f82e2008-09-28 16:15:56 -07001341 host->ioaddr = pci_ioremap_bar(pdev, bar);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001342 if (!host->ioaddr) {
1343 dev_err(&pdev->dev, "failed to remap registers\n");
Chris Ball9fdcdbb2011-03-29 00:46:12 -04001344 ret = -ENOMEM;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001345 goto release;
1346 }
1347
Pierre Ossman44894282008-04-04 19:36:59 +02001348 if (chip->fixes && chip->fixes->probe_slot) {
1349 ret = chip->fixes->probe_slot(slot);
1350 if (ret)
1351 goto unmap;
1352 }
1353
Adrian Hunterc5e027a2011-12-27 15:48:44 +02001354 if (gpio_is_valid(slot->rst_n_gpio)) {
1355 if (!gpio_request(slot->rst_n_gpio, "eMMC_reset")) {
1356 gpio_direction_output(slot->rst_n_gpio, 1);
1357 slot->host->mmc->caps |= MMC_CAP_HW_RESET;
Adrian Hunterc9faff62013-06-13 11:50:26 +03001358 slot->hw_reset = sdhci_pci_gpio_hw_reset;
Adrian Hunterc5e027a2011-12-27 15:48:44 +02001359 } else {
1360 dev_warn(&pdev->dev, "failed to request rst_n_gpio\n");
1361 slot->rst_n_gpio = -EINVAL;
1362 }
1363 }
1364
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08001365 host->mmc->pm_caps = MMC_PM_KEEP_POWER | MMC_PM_WAKE_SDIO_IRQ;
Aaron Lueed222a2013-03-05 11:24:52 +08001366 host->mmc->slotno = slotno;
Adrian Huntera08b17b2013-04-15 11:27:25 -04001367 host->mmc->caps2 |= MMC_CAP2_NO_PRESCAN_POWERUP;
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08001368
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001369 ret = sdhci_add_host(host);
1370 if (ret)
Pierre Ossman44894282008-04-04 19:36:59 +02001371 goto remove;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001372
Adrian Hunterc5e027a2011-12-27 15:48:44 +02001373 sdhci_pci_add_own_cd(slot);
1374
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001375 return slot;
1376
Pierre Ossman44894282008-04-04 19:36:59 +02001377remove:
Adrian Hunterc5e027a2011-12-27 15:48:44 +02001378 if (gpio_is_valid(slot->rst_n_gpio))
1379 gpio_free(slot->rst_n_gpio);
1380
Pierre Ossman44894282008-04-04 19:36:59 +02001381 if (chip->fixes && chip->fixes->remove_slot)
Pierre Ossman1e728592008-04-16 19:13:13 +02001382 chip->fixes->remove_slot(slot, 0);
Pierre Ossman44894282008-04-04 19:36:59 +02001383
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001384unmap:
1385 iounmap(host->ioaddr);
1386
1387release:
1388 pci_release_region(pdev, bar);
Dan Carpenterc60a32c2009-04-10 23:31:10 +02001389
Adrian Hunter52c506f2011-12-27 15:48:43 +02001390cleanup:
1391 if (slot->data && slot->data->cleanup)
1392 slot->data->cleanup(slot->data);
1393
Dan Carpenterc60a32c2009-04-10 23:31:10 +02001394free:
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001395 sdhci_free_host(host);
1396
1397 return ERR_PTR(ret);
1398}
1399
1400static void sdhci_pci_remove_slot(struct sdhci_pci_slot *slot)
1401{
Pierre Ossman1e728592008-04-16 19:13:13 +02001402 int dead;
1403 u32 scratch;
1404
Adrian Hunterc5e027a2011-12-27 15:48:44 +02001405 sdhci_pci_remove_own_cd(slot);
1406
Pierre Ossman1e728592008-04-16 19:13:13 +02001407 dead = 0;
1408 scratch = readl(slot->host->ioaddr + SDHCI_INT_STATUS);
1409 if (scratch == (u32)-1)
1410 dead = 1;
1411
1412 sdhci_remove_host(slot->host, dead);
Pierre Ossman44894282008-04-04 19:36:59 +02001413
Adrian Hunterc5e027a2011-12-27 15:48:44 +02001414 if (gpio_is_valid(slot->rst_n_gpio))
1415 gpio_free(slot->rst_n_gpio);
1416
Pierre Ossman44894282008-04-04 19:36:59 +02001417 if (slot->chip->fixes && slot->chip->fixes->remove_slot)
Pierre Ossman1e728592008-04-16 19:13:13 +02001418 slot->chip->fixes->remove_slot(slot, dead);
Pierre Ossman44894282008-04-04 19:36:59 +02001419
Adrian Hunter52c506f2011-12-27 15:48:43 +02001420 if (slot->data && slot->data->cleanup)
1421 slot->data->cleanup(slot->data);
1422
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001423 pci_release_region(slot->chip->pdev, slot->pci_bar);
Pierre Ossman44894282008-04-04 19:36:59 +02001424
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001425 sdhci_free_host(slot->host);
1426}
1427
Bill Pembertonc3be1ef2012-11-19 13:23:06 -05001428static void sdhci_pci_runtime_pm_allow(struct device *dev)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001429{
1430 pm_runtime_put_noidle(dev);
1431 pm_runtime_allow(dev);
1432 pm_runtime_set_autosuspend_delay(dev, 50);
1433 pm_runtime_use_autosuspend(dev);
1434 pm_suspend_ignore_children(dev, 1);
1435}
1436
Bill Pemberton6e0ee712012-11-19 13:26:03 -05001437static void sdhci_pci_runtime_pm_forbid(struct device *dev)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001438{
1439 pm_runtime_forbid(dev);
1440 pm_runtime_get_noresume(dev);
1441}
1442
Bill Pembertonc3be1ef2012-11-19 13:23:06 -05001443static int sdhci_pci_probe(struct pci_dev *pdev,
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001444 const struct pci_device_id *ent)
1445{
1446 struct sdhci_pci_chip *chip;
1447 struct sdhci_pci_slot *slot;
1448
Sergei Shtylyovcf5e23e2011-03-17 16:46:17 -04001449 u8 slots, first_bar;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001450 int ret, i;
1451
1452 BUG_ON(pdev == NULL);
1453 BUG_ON(ent == NULL);
1454
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001455 dev_info(&pdev->dev, "SDHCI controller found [%04x:%04x] (rev %x)\n",
Sergei Shtylyovcf5e23e2011-03-17 16:46:17 -04001456 (int)pdev->vendor, (int)pdev->device, (int)pdev->revision);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001457
1458 ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &slots);
1459 if (ret)
1460 return ret;
1461
1462 slots = PCI_SLOT_INFO_SLOTS(slots) + 1;
1463 dev_dbg(&pdev->dev, "found %d slot(s)\n", slots);
1464 if (slots == 0)
1465 return -ENODEV;
1466
1467 BUG_ON(slots > MAX_SLOTS);
1468
1469 ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &first_bar);
1470 if (ret)
1471 return ret;
1472
1473 first_bar &= PCI_SLOT_INFO_FIRST_BAR_MASK;
1474
1475 if (first_bar > 5) {
1476 dev_err(&pdev->dev, "Invalid first BAR. Aborting.\n");
1477 return -ENODEV;
1478 }
1479
1480 ret = pci_enable_device(pdev);
1481 if (ret)
1482 return ret;
1483
1484 chip = kzalloc(sizeof(struct sdhci_pci_chip), GFP_KERNEL);
1485 if (!chip) {
1486 ret = -ENOMEM;
1487 goto err;
1488 }
1489
1490 chip->pdev = pdev;
Ameya Palandeb177bc92011-04-05 21:13:13 +03001491 chip->fixes = (const struct sdhci_pci_fixes *)ent->driver_data;
Adrian Hunterc43fd772011-10-17 10:52:44 +03001492 if (chip->fixes) {
Pierre Ossman22606402008-03-23 19:33:23 +01001493 chip->quirks = chip->fixes->quirks;
Adrian Hunterf3c55a72012-02-07 14:48:55 +02001494 chip->quirks2 = chip->fixes->quirks2;
Adrian Hunterc43fd772011-10-17 10:52:44 +03001495 chip->allow_runtime_pm = chip->fixes->allow_runtime_pm;
1496 }
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001497 chip->num_slots = slots;
1498
1499 pci_set_drvdata(pdev, chip);
1500
Pierre Ossman22606402008-03-23 19:33:23 +01001501 if (chip->fixes && chip->fixes->probe) {
1502 ret = chip->fixes->probe(chip);
1503 if (ret)
1504 goto free;
1505 }
1506
Alan Cox225d85f2010-10-04 15:24:21 +01001507 slots = chip->num_slots; /* Quirk may have changed this */
1508
Ameya Palandeb177bc92011-04-05 21:13:13 +03001509 for (i = 0; i < slots; i++) {
Adrian Hunter52c506f2011-12-27 15:48:43 +02001510 slot = sdhci_pci_probe_slot(pdev, chip, first_bar, i);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001511 if (IS_ERR(slot)) {
Ameya Palandeb177bc92011-04-05 21:13:13 +03001512 for (i--; i >= 0; i--)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001513 sdhci_pci_remove_slot(chip->slots[i]);
1514 ret = PTR_ERR(slot);
1515 goto free;
1516 }
1517
1518 chip->slots[i] = slot;
1519 }
1520
Adrian Hunterc43fd772011-10-17 10:52:44 +03001521 if (chip->allow_runtime_pm)
1522 sdhci_pci_runtime_pm_allow(&pdev->dev);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001523
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001524 return 0;
1525
1526free:
1527 pci_set_drvdata(pdev, NULL);
1528 kfree(chip);
1529
1530err:
1531 pci_disable_device(pdev);
1532 return ret;
1533}
1534
Bill Pemberton6e0ee712012-11-19 13:26:03 -05001535static void sdhci_pci_remove(struct pci_dev *pdev)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001536{
1537 int i;
1538 struct sdhci_pci_chip *chip;
1539
1540 chip = pci_get_drvdata(pdev);
1541
1542 if (chip) {
Adrian Hunterc43fd772011-10-17 10:52:44 +03001543 if (chip->allow_runtime_pm)
1544 sdhci_pci_runtime_pm_forbid(&pdev->dev);
1545
Ameya Palandeb177bc92011-04-05 21:13:13 +03001546 for (i = 0; i < chip->num_slots; i++)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001547 sdhci_pci_remove_slot(chip->slots[i]);
1548
1549 pci_set_drvdata(pdev, NULL);
1550 kfree(chip);
1551 }
1552
1553 pci_disable_device(pdev);
1554}
1555
1556static struct pci_driver sdhci_driver = {
Ameya Palandeb177bc92011-04-05 21:13:13 +03001557 .name = "sdhci-pci",
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001558 .id_table = pci_ids,
Ameya Palandeb177bc92011-04-05 21:13:13 +03001559 .probe = sdhci_pci_probe,
Bill Pemberton0433c142012-11-19 13:20:26 -05001560 .remove = sdhci_pci_remove,
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001561 .driver = {
1562 .pm = &sdhci_pci_pm_ops
1563 },
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001564};
1565
Sachin Kamatacc69642012-08-27 11:57:02 +05301566module_pci_driver(sdhci_driver);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001567
Pierre Ossman32710e82009-04-08 20:14:54 +02001568MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001569MODULE_DESCRIPTION("Secure Digital Host Controller Interface PCI driver");
1570MODULE_LICENSE("GPL");