blob: d7d6bc8968d2422d8b2e9fb44a695b35b17927d4 [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
Alexander Stein296e0b02012-03-14 08:38:58 +010040
41/*
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010042 * PCI registers
43 */
44
45#define PCI_SDHCI_IFPIO 0x00
46#define PCI_SDHCI_IFDMA 0x01
47#define PCI_SDHCI_IFVENDOR 0x02
48
49#define PCI_SLOT_INFO 0x40 /* 8 bits */
50#define PCI_SLOT_INFO_SLOTS(x) ((x >> 4) & 7)
51#define PCI_SLOT_INFO_FIRST_BAR_MASK 0x07
52
53#define MAX_SLOTS 8
54
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010055struct sdhci_pci_chip;
Pierre Ossman44894282008-04-04 19:36:59 +020056struct sdhci_pci_slot;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010057
Pierre Ossman22606402008-03-23 19:33:23 +010058struct sdhci_pci_fixes {
59 unsigned int quirks;
Adrian Hunterf3c55a72012-02-07 14:48:55 +020060 unsigned int quirks2;
Adrian Hunterc43fd772011-10-17 10:52:44 +030061 bool allow_runtime_pm;
Pierre Ossman22606402008-03-23 19:33:23 +010062
Ameya Palandeb177bc92011-04-05 21:13:13 +030063 int (*probe) (struct sdhci_pci_chip *);
Pierre Ossman45211e22008-03-24 13:09:09 +010064
Ameya Palandeb177bc92011-04-05 21:13:13 +030065 int (*probe_slot) (struct sdhci_pci_slot *);
66 void (*remove_slot) (struct sdhci_pci_slot *, int);
Pierre Ossman44894282008-04-04 19:36:59 +020067
Manuel Lauss29495aa2011-11-03 11:09:45 +010068 int (*suspend) (struct sdhci_pci_chip *);
Ameya Palandeb177bc92011-04-05 21:13:13 +030069 int (*resume) (struct sdhci_pci_chip *);
Pierre Ossman22606402008-03-23 19:33:23 +010070};
71
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010072struct sdhci_pci_slot {
73 struct sdhci_pci_chip *chip;
74 struct sdhci_host *host;
Adrian Hunter52c506f2011-12-27 15:48:43 +020075 struct sdhci_pci_data *data;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010076
77 int pci_bar;
Adrian Hunter0f201652011-08-29 16:42:13 +030078 int rst_n_gpio;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030079 int cd_gpio;
80 int cd_irq;
Adrian Hunterc9faff62013-06-13 11:50:26 +030081
82 void (*hw_reset)(struct sdhci_host *host);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010083};
84
85struct sdhci_pci_chip {
86 struct pci_dev *pdev;
Pierre Ossman22606402008-03-23 19:33:23 +010087
88 unsigned int quirks;
Adrian Hunterf3c55a72012-02-07 14:48:55 +020089 unsigned int quirks2;
Adrian Hunterc43fd772011-10-17 10:52:44 +030090 bool allow_runtime_pm;
Pierre Ossman22606402008-03-23 19:33:23 +010091 const struct sdhci_pci_fixes *fixes;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010092
93 int num_slots; /* Slots on controller */
94 struct sdhci_pci_slot *slots[MAX_SLOTS]; /* Pointers to host slots */
95};
96
Pierre Ossman22606402008-03-23 19:33:23 +010097
98/*****************************************************************************\
99 * *
100 * Hardware specific quirk handling *
101 * *
102\*****************************************************************************/
103
104static int ricoh_probe(struct sdhci_pci_chip *chip)
105{
Chris Ballc99436f2009-09-22 16:45:22 -0700106 if (chip->pdev->subsystem_vendor == PCI_VENDOR_ID_SAMSUNG ||
107 chip->pdev->subsystem_vendor == PCI_VENDOR_ID_SONY)
Pierre Ossman22606402008-03-23 19:33:23 +0100108 chip->quirks |= SDHCI_QUIRK_NO_CARD_NO_RESET;
Maxim Levitskyccc92c22010-08-10 18:01:42 -0700109 return 0;
110}
Pierre Ossman22606402008-03-23 19:33:23 +0100111
Maxim Levitskyccc92c22010-08-10 18:01:42 -0700112static int ricoh_mmc_probe_slot(struct sdhci_pci_slot *slot)
113{
114 slot->host->caps =
115 ((0x21 << SDHCI_TIMEOUT_CLK_SHIFT)
116 & SDHCI_TIMEOUT_CLK_MASK) |
117
118 ((0x21 << SDHCI_CLOCK_BASE_SHIFT)
119 & SDHCI_CLOCK_BASE_MASK) |
120
121 SDHCI_TIMEOUT_CLK_UNIT |
122 SDHCI_CAN_VDD_330 |
Madhvapathi Sriram1a1f1f02012-10-15 04:47:30 +0000123 SDHCI_CAN_DO_HISPD |
Maxim Levitskyccc92c22010-08-10 18:01:42 -0700124 SDHCI_CAN_DO_SDMA;
125 return 0;
126}
127
128static int ricoh_mmc_resume(struct sdhci_pci_chip *chip)
129{
130 /* Apply a delay to allow controller to settle */
131 /* Otherwise it becomes confused if card state changed
132 during suspend */
133 msleep(500);
Pierre Ossman22606402008-03-23 19:33:23 +0100134 return 0;
135}
136
137static const struct sdhci_pci_fixes sdhci_ricoh = {
138 .probe = ricoh_probe,
Vasily Khoruzhick84938292010-03-05 13:43:46 -0800139 .quirks = SDHCI_QUIRK_32BIT_DMA_ADDR |
140 SDHCI_QUIRK_FORCE_DMA |
141 SDHCI_QUIRK_CLOCK_BEFORE_RESET,
Pierre Ossman22606402008-03-23 19:33:23 +0100142};
143
Maxim Levitskyccc92c22010-08-10 18:01:42 -0700144static const struct sdhci_pci_fixes sdhci_ricoh_mmc = {
145 .probe_slot = ricoh_mmc_probe_slot,
146 .resume = ricoh_mmc_resume,
147 .quirks = SDHCI_QUIRK_32BIT_DMA_ADDR |
148 SDHCI_QUIRK_CLOCK_BEFORE_RESET |
149 SDHCI_QUIRK_NO_CARD_NO_RESET |
150 SDHCI_QUIRK_MISSING_CAPS
151};
152
Pierre Ossman22606402008-03-23 19:33:23 +0100153static const struct sdhci_pci_fixes sdhci_ene_712 = {
154 .quirks = SDHCI_QUIRK_SINGLE_POWER_WRITE |
155 SDHCI_QUIRK_BROKEN_DMA,
156};
157
158static const struct sdhci_pci_fixes sdhci_ene_714 = {
159 .quirks = SDHCI_QUIRK_SINGLE_POWER_WRITE |
160 SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS |
161 SDHCI_QUIRK_BROKEN_DMA,
162};
163
164static const struct sdhci_pci_fixes sdhci_cafe = {
165 .quirks = SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER |
Andres Salomona0874892009-03-02 21:48:20 +0100166 SDHCI_QUIRK_NO_BUSY_IRQ |
Daniel Drake55fc05b2012-07-03 23:13:39 +0100167 SDHCI_QUIRK_BROKEN_CARD_DETECTION |
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200168 SDHCI_QUIRK_BROKEN_TIMEOUT_VAL,
Pierre Ossman22606402008-03-23 19:33:23 +0100169};
170
Major Lee68077b02011-06-29 14:23:46 +0300171static int mrst_hc_probe_slot(struct sdhci_pci_slot *slot)
172{
173 slot->host->mmc->caps |= MMC_CAP_8_BIT_DATA;
174 return 0;
175}
176
Alan Coxf9ee3ea2010-10-04 15:25:11 +0100177/*
178 * ADMA operation is disabled for Moorestown platform due to
179 * hardware bugs.
180 */
Jacob Pan35ac6f02010-11-09 13:57:29 +0000181static int mrst_hc_probe(struct sdhci_pci_chip *chip)
Alan Coxf9ee3ea2010-10-04 15:25:11 +0100182{
183 /*
Jacob Pan35ac6f02010-11-09 13:57:29 +0000184 * slots number is fixed here for MRST as SDIO3/5 are never used and
185 * have hardware bugs.
Alan Coxf9ee3ea2010-10-04 15:25:11 +0100186 */
187 chip->num_slots = 1;
188 return 0;
189}
190
Alexander Stein296e0b02012-03-14 08:38:58 +0100191static int pch_hc_probe_slot(struct sdhci_pci_slot *slot)
192{
193 slot->host->mmc->caps |= MMC_CAP_8_BIT_DATA;
194 return 0;
195}
196
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300197#ifdef CONFIG_PM_RUNTIME
198
Adrian Hunterc5e027a2011-12-27 15:48:44 +0200199static irqreturn_t sdhci_pci_sd_cd(int irq, void *dev_id)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300200{
201 struct sdhci_pci_slot *slot = dev_id;
202 struct sdhci_host *host = slot->host;
203
204 mmc_detect_change(host->mmc, msecs_to_jiffies(200));
205 return IRQ_HANDLED;
206}
207
Adrian Hunterc5e027a2011-12-27 15:48:44 +0200208static void sdhci_pci_add_own_cd(struct sdhci_pci_slot *slot)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300209{
Adrian Hunterc5e027a2011-12-27 15:48:44 +0200210 int err, irq, gpio = slot->cd_gpio;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300211
212 slot->cd_gpio = -EINVAL;
213 slot->cd_irq = -EINVAL;
214
Adrian Hunterc5e027a2011-12-27 15:48:44 +0200215 if (!gpio_is_valid(gpio))
216 return;
217
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300218 err = gpio_request(gpio, "sd_cd");
219 if (err < 0)
220 goto out;
221
222 err = gpio_direction_input(gpio);
223 if (err < 0)
224 goto out_free;
225
226 irq = gpio_to_irq(gpio);
227 if (irq < 0)
228 goto out_free;
229
Adrian Hunterc5e027a2011-12-27 15:48:44 +0200230 err = request_irq(irq, sdhci_pci_sd_cd, IRQF_TRIGGER_RISING |
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300231 IRQF_TRIGGER_FALLING, "sd_cd", slot);
232 if (err)
233 goto out_free;
234
235 slot->cd_gpio = gpio;
236 slot->cd_irq = irq;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300237
Adrian Hunterc5e027a2011-12-27 15:48:44 +0200238 return;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300239
240out_free:
241 gpio_free(gpio);
242out:
243 dev_warn(&slot->chip->pdev->dev, "failed to setup card detect wake up\n");
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300244}
245
Adrian Hunterc5e027a2011-12-27 15:48:44 +0200246static void sdhci_pci_remove_own_cd(struct sdhci_pci_slot *slot)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300247{
248 if (slot->cd_irq >= 0)
249 free_irq(slot->cd_irq, slot);
Adrian Hunterc5e027a2011-12-27 15:48:44 +0200250 if (gpio_is_valid(slot->cd_gpio))
251 gpio_free(slot->cd_gpio);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300252}
253
254#else
255
Adrian Hunterc5e027a2011-12-27 15:48:44 +0200256static inline void sdhci_pci_add_own_cd(struct sdhci_pci_slot *slot)
257{
258}
259
260static inline void sdhci_pci_remove_own_cd(struct sdhci_pci_slot *slot)
261{
262}
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300263
264#endif
265
Adrian Hunter0d013bc2011-06-29 14:23:47 +0300266static int mfd_emmc_probe_slot(struct sdhci_pci_slot *slot)
267{
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300268 slot->host->mmc->caps |= MMC_CAP_8_BIT_DATA | MMC_CAP_NONREMOVABLE;
Adrian Hunterda721cf2012-02-07 14:48:53 +0200269 slot->host->mmc->caps2 |= MMC_CAP2_BOOTPART_NOACC |
270 MMC_CAP2_HC_ERASE_SZ;
Adrian Hunter0d013bc2011-06-29 14:23:47 +0300271 return 0;
272}
273
Adrian Hunter93933502011-12-27 15:48:47 +0200274static int mfd_sdio_probe_slot(struct sdhci_pci_slot *slot)
275{
Adrian Hunter012e4672012-01-30 14:27:18 +0200276 slot->host->mmc->caps |= MMC_CAP_POWER_OFF_CARD | MMC_CAP_NONREMOVABLE;
Adrian Hunter93933502011-12-27 15:48:47 +0200277 return 0;
278}
279
Alan Coxf9ee3ea2010-10-04 15:25:11 +0100280static const struct sdhci_pci_fixes sdhci_intel_mrst_hc0 = {
281 .quirks = SDHCI_QUIRK_BROKEN_ADMA | SDHCI_QUIRK_NO_HISPD_BIT,
Major Lee68077b02011-06-29 14:23:46 +0300282 .probe_slot = mrst_hc_probe_slot,
Alan Coxf9ee3ea2010-10-04 15:25:11 +0100283};
284
Jacob Pan35ac6f02010-11-09 13:57:29 +0000285static const struct sdhci_pci_fixes sdhci_intel_mrst_hc1_hc2 = {
Alan Coxf9ee3ea2010-10-04 15:25:11 +0100286 .quirks = SDHCI_QUIRK_BROKEN_ADMA | SDHCI_QUIRK_NO_HISPD_BIT,
Jacob Pan35ac6f02010-11-09 13:57:29 +0000287 .probe = mrst_hc_probe,
Alan Coxf9ee3ea2010-10-04 15:25:11 +0100288};
289
Xiaochen Shen29229052010-10-04 15:24:52 +0100290static const struct sdhci_pci_fixes sdhci_intel_mfd_sd = {
291 .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
Adrian Hunterc43fd772011-10-17 10:52:44 +0300292 .allow_runtime_pm = true,
Xiaochen Shen29229052010-10-04 15:24:52 +0100293};
294
Adrian Hunter0d013bc2011-06-29 14:23:47 +0300295static const struct sdhci_pci_fixes sdhci_intel_mfd_sdio = {
Xiaochen Shen29229052010-10-04 15:24:52 +0100296 .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
Adrian Hunterf3c55a72012-02-07 14:48:55 +0200297 .quirks2 = SDHCI_QUIRK2_HOST_OFF_CARD_ON,
Adrian Hunterc43fd772011-10-17 10:52:44 +0300298 .allow_runtime_pm = true,
Adrian Hunter93933502011-12-27 15:48:47 +0200299 .probe_slot = mfd_sdio_probe_slot,
Xiaochen Shen29229052010-10-04 15:24:52 +0100300};
301
Adrian Hunter0d013bc2011-06-29 14:23:47 +0300302static const struct sdhci_pci_fixes sdhci_intel_mfd_emmc = {
303 .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
Adrian Hunterc43fd772011-10-17 10:52:44 +0300304 .allow_runtime_pm = true,
Adrian Hunter0d013bc2011-06-29 14:23:47 +0300305 .probe_slot = mfd_emmc_probe_slot,
306};
307
Alexander Stein296e0b02012-03-14 08:38:58 +0100308static const struct sdhci_pci_fixes sdhci_intel_pch_sdio = {
309 .quirks = SDHCI_QUIRK_BROKEN_ADMA,
310 .probe_slot = pch_hc_probe_slot,
311};
312
Adrian Hunterc9faff62013-06-13 11:50:26 +0300313static void sdhci_pci_int_hw_reset(struct sdhci_host *host)
314{
315 u8 reg;
316
317 reg = sdhci_readb(host, SDHCI_POWER_CONTROL);
318 reg |= 0x10;
319 sdhci_writeb(host, reg, SDHCI_POWER_CONTROL);
320 /* For eMMC, minimum is 1us but give it 9us for good measure */
321 udelay(9);
322 reg &= ~0x10;
323 sdhci_writeb(host, reg, SDHCI_POWER_CONTROL);
324 /* For eMMC, minimum is 200us but give it 300us for good measure */
325 usleep_range(300, 1000);
326}
327
Adrian Hunter728ef3d2013-04-26 11:27:23 +0300328static int byt_emmc_probe_slot(struct sdhci_pci_slot *slot)
329{
Adrian Hunterc9faff62013-06-13 11:50:26 +0300330 slot->host->mmc->caps |= MMC_CAP_8_BIT_DATA | MMC_CAP_NONREMOVABLE |
331 MMC_CAP_HW_RESET;
Adrian Hunter728ef3d2013-04-26 11:27:23 +0300332 slot->host->mmc->caps2 |= MMC_CAP2_HC_ERASE_SZ;
Adrian Hunterc9faff62013-06-13 11:50:26 +0300333 slot->hw_reset = sdhci_pci_int_hw_reset;
Adrian Hunter728ef3d2013-04-26 11:27:23 +0300334 return 0;
335}
336
337static int byt_sdio_probe_slot(struct sdhci_pci_slot *slot)
338{
339 slot->host->mmc->caps |= MMC_CAP_POWER_OFF_CARD | MMC_CAP_NONREMOVABLE;
340 return 0;
341}
342
343static const struct sdhci_pci_fixes sdhci_intel_byt_emmc = {
344 .allow_runtime_pm = true,
345 .probe_slot = byt_emmc_probe_slot,
346};
347
348static const struct sdhci_pci_fixes sdhci_intel_byt_sdio = {
349 .quirks2 = SDHCI_QUIRK2_HOST_OFF_CARD_ON,
350 .allow_runtime_pm = true,
351 .probe_slot = byt_sdio_probe_slot,
352};
353
354static const struct sdhci_pci_fixes sdhci_intel_byt_sd = {
Adrian Hunter7396e312013-05-06 12:17:34 +0300355 .quirks2 = SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON,
356 .allow_runtime_pm = true,
Adrian Hunter728ef3d2013-04-26 11:27:23 +0300357};
358
Jennifer Li26daa1e2010-11-17 23:01:59 -0500359/* O2Micro extra registers */
360#define O2_SD_LOCK_WP 0xD3
361#define O2_SD_MULTI_VCC3V 0xEE
362#define O2_SD_CLKREQ 0xEC
363#define O2_SD_CAPS 0xE0
364#define O2_SD_ADMA1 0xE2
365#define O2_SD_ADMA2 0xE7
366#define O2_SD_INF_MOD 0xF1
367
368static int o2_probe(struct sdhci_pci_chip *chip)
369{
370 int ret;
371 u8 scratch;
372
373 switch (chip->pdev->device) {
374 case PCI_DEVICE_ID_O2_8220:
375 case PCI_DEVICE_ID_O2_8221:
376 case PCI_DEVICE_ID_O2_8320:
377 case PCI_DEVICE_ID_O2_8321:
378 /* This extra setup is required due to broken ADMA. */
379 ret = pci_read_config_byte(chip->pdev, O2_SD_LOCK_WP, &scratch);
380 if (ret)
381 return ret;
382 scratch &= 0x7f;
383 pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch);
384
385 /* Set Multi 3 to VCC3V# */
386 pci_write_config_byte(chip->pdev, O2_SD_MULTI_VCC3V, 0x08);
387
388 /* Disable CLK_REQ# support after media DET */
389 ret = pci_read_config_byte(chip->pdev, O2_SD_CLKREQ, &scratch);
390 if (ret)
391 return ret;
392 scratch |= 0x20;
393 pci_write_config_byte(chip->pdev, O2_SD_CLKREQ, scratch);
394
395 /* Choose capabilities, enable SDMA. We have to write 0x01
396 * to the capabilities register first to unlock it.
397 */
398 ret = pci_read_config_byte(chip->pdev, O2_SD_CAPS, &scratch);
399 if (ret)
400 return ret;
401 scratch |= 0x01;
402 pci_write_config_byte(chip->pdev, O2_SD_CAPS, scratch);
403 pci_write_config_byte(chip->pdev, O2_SD_CAPS, 0x73);
404
405 /* Disable ADMA1/2 */
406 pci_write_config_byte(chip->pdev, O2_SD_ADMA1, 0x39);
407 pci_write_config_byte(chip->pdev, O2_SD_ADMA2, 0x08);
408
409 /* Disable the infinite transfer mode */
410 ret = pci_read_config_byte(chip->pdev, O2_SD_INF_MOD, &scratch);
411 if (ret)
412 return ret;
413 scratch |= 0x08;
414 pci_write_config_byte(chip->pdev, O2_SD_INF_MOD, scratch);
415
416 /* Lock WP */
417 ret = pci_read_config_byte(chip->pdev, O2_SD_LOCK_WP, &scratch);
418 if (ret)
419 return ret;
420 scratch |= 0x80;
421 pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch);
422 }
423
424 return 0;
425}
426
Pierre Ossman45211e22008-03-24 13:09:09 +0100427static int jmicron_pmos(struct sdhci_pci_chip *chip, int on)
428{
429 u8 scratch;
430 int ret;
431
432 ret = pci_read_config_byte(chip->pdev, 0xAE, &scratch);
433 if (ret)
434 return ret;
435
436 /*
437 * Turn PMOS on [bit 0], set over current detection to 2.4 V
438 * [bit 1:2] and enable over current debouncing [bit 6].
439 */
440 if (on)
441 scratch |= 0x47;
442 else
443 scratch &= ~0x47;
444
445 ret = pci_write_config_byte(chip->pdev, 0xAE, scratch);
446 if (ret)
447 return ret;
448
449 return 0;
450}
451
452static int jmicron_probe(struct sdhci_pci_chip *chip)
453{
454 int ret;
Takashi Iwai8f230f42010-12-08 10:04:30 +0100455 u16 mmcdev = 0;
Pierre Ossman45211e22008-03-24 13:09:09 +0100456
Pierre Ossman93fc48c2008-06-28 18:21:41 +0200457 if (chip->pdev->revision == 0) {
458 chip->quirks |= SDHCI_QUIRK_32BIT_DMA_ADDR |
459 SDHCI_QUIRK_32BIT_DMA_SIZE |
Pierre Ossman2134a922008-06-28 18:28:51 +0200460 SDHCI_QUIRK_32BIT_ADMA_SIZE |
Pierre Ossman4a3cba32008-07-29 00:11:16 +0200461 SDHCI_QUIRK_RESET_AFTER_REQUEST |
Pierre Ossman86a6a872009-02-02 21:13:49 +0100462 SDHCI_QUIRK_BROKEN_SMALL_PIO;
Pierre Ossman93fc48c2008-06-28 18:21:41 +0200463 }
464
Pierre Ossman45211e22008-03-24 13:09:09 +0100465 /*
Pierre Ossman44894282008-04-04 19:36:59 +0200466 * JMicron chips can have two interfaces to the same hardware
467 * in order to work around limitations in Microsoft's driver.
468 * We need to make sure we only bind to one of them.
469 *
470 * This code assumes two things:
471 *
472 * 1. The PCI code adds subfunctions in order.
473 *
474 * 2. The MMC interface has a lower subfunction number
475 * than the SD interface.
476 */
Takashi Iwai8f230f42010-12-08 10:04:30 +0100477 if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_SD)
478 mmcdev = PCI_DEVICE_ID_JMICRON_JMB38X_MMC;
479 else if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_SD)
480 mmcdev = PCI_DEVICE_ID_JMICRON_JMB388_ESD;
481
482 if (mmcdev) {
Pierre Ossman44894282008-04-04 19:36:59 +0200483 struct pci_dev *sd_dev;
484
485 sd_dev = NULL;
486 while ((sd_dev = pci_get_device(PCI_VENDOR_ID_JMICRON,
Takashi Iwai8f230f42010-12-08 10:04:30 +0100487 mmcdev, sd_dev)) != NULL) {
Pierre Ossman44894282008-04-04 19:36:59 +0200488 if ((PCI_SLOT(chip->pdev->devfn) ==
489 PCI_SLOT(sd_dev->devfn)) &&
490 (chip->pdev->bus == sd_dev->bus))
491 break;
492 }
493
494 if (sd_dev) {
495 pci_dev_put(sd_dev);
496 dev_info(&chip->pdev->dev, "Refusing to bind to "
497 "secondary interface.\n");
498 return -ENODEV;
499 }
500 }
501
502 /*
Pierre Ossman45211e22008-03-24 13:09:09 +0100503 * JMicron chips need a bit of a nudge to enable the power
504 * output pins.
505 */
506 ret = jmicron_pmos(chip, 1);
507 if (ret) {
508 dev_err(&chip->pdev->dev, "Failure enabling card power\n");
509 return ret;
510 }
511
Takashi Iwai82b0e232011-04-21 20:26:38 +0200512 /* quirk for unsable RO-detection on JM388 chips */
513 if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_SD ||
514 chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD)
515 chip->quirks |= SDHCI_QUIRK_UNSTABLE_RO_DETECT;
516
Pierre Ossman45211e22008-03-24 13:09:09 +0100517 return 0;
518}
519
Pierre Ossman44894282008-04-04 19:36:59 +0200520static void jmicron_enable_mmc(struct sdhci_host *host, int on)
521{
522 u8 scratch;
523
524 scratch = readb(host->ioaddr + 0xC0);
525
526 if (on)
527 scratch |= 0x01;
528 else
529 scratch &= ~0x01;
530
531 writeb(scratch, host->ioaddr + 0xC0);
532}
533
534static int jmicron_probe_slot(struct sdhci_pci_slot *slot)
535{
Pierre Ossman2134a922008-06-28 18:28:51 +0200536 if (slot->chip->pdev->revision == 0) {
537 u16 version;
538
539 version = readl(slot->host->ioaddr + SDHCI_HOST_VERSION);
540 version = (version & SDHCI_VENDOR_VER_MASK) >>
541 SDHCI_VENDOR_VER_SHIFT;
542
543 /*
544 * Older versions of the chip have lots of nasty glitches
545 * in the ADMA engine. It's best just to avoid it
546 * completely.
547 */
548 if (version < 0xAC)
549 slot->host->quirks |= SDHCI_QUIRK_BROKEN_ADMA;
550 }
551
Takashi Iwai8f230f42010-12-08 10:04:30 +0100552 /* JM388 MMC doesn't support 1.8V while SD supports it */
553 if (slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD) {
554 slot->host->ocr_avail_sd = MMC_VDD_32_33 | MMC_VDD_33_34 |
555 MMC_VDD_29_30 | MMC_VDD_30_31 |
556 MMC_VDD_165_195; /* allow 1.8V */
557 slot->host->ocr_avail_mmc = MMC_VDD_32_33 | MMC_VDD_33_34 |
558 MMC_VDD_29_30 | MMC_VDD_30_31; /* no 1.8V for MMC */
559 }
560
Pierre Ossman44894282008-04-04 19:36:59 +0200561 /*
562 * The secondary interface requires a bit set to get the
563 * interrupts.
564 */
Takashi Iwai8f230f42010-12-08 10:04:30 +0100565 if (slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC ||
566 slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD)
Pierre Ossman44894282008-04-04 19:36:59 +0200567 jmicron_enable_mmc(slot->host, 1);
568
Takashi Iwaid75c1082010-12-16 17:54:14 +0100569 slot->host->mmc->caps |= MMC_CAP_BUS_WIDTH_TEST;
570
Pierre Ossman44894282008-04-04 19:36:59 +0200571 return 0;
572}
573
Pierre Ossman1e728592008-04-16 19:13:13 +0200574static void jmicron_remove_slot(struct sdhci_pci_slot *slot, int dead)
Pierre Ossman44894282008-04-04 19:36:59 +0200575{
Pierre Ossman1e728592008-04-16 19:13:13 +0200576 if (dead)
577 return;
578
Takashi Iwai8f230f42010-12-08 10:04:30 +0100579 if (slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC ||
580 slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD)
Pierre Ossman44894282008-04-04 19:36:59 +0200581 jmicron_enable_mmc(slot->host, 0);
582}
583
Manuel Lauss29495aa2011-11-03 11:09:45 +0100584static int jmicron_suspend(struct sdhci_pci_chip *chip)
Pierre Ossman44894282008-04-04 19:36:59 +0200585{
586 int i;
587
Takashi Iwai8f230f42010-12-08 10:04:30 +0100588 if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC ||
589 chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD) {
Ameya Palandeb177bc92011-04-05 21:13:13 +0300590 for (i = 0; i < chip->num_slots; i++)
Pierre Ossman44894282008-04-04 19:36:59 +0200591 jmicron_enable_mmc(chip->slots[i]->host, 0);
592 }
593
594 return 0;
595}
596
Pierre Ossman45211e22008-03-24 13:09:09 +0100597static int jmicron_resume(struct sdhci_pci_chip *chip)
598{
Pierre Ossman44894282008-04-04 19:36:59 +0200599 int ret, i;
600
Takashi Iwai8f230f42010-12-08 10:04:30 +0100601 if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC ||
602 chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD) {
Ameya Palandeb177bc92011-04-05 21:13:13 +0300603 for (i = 0; i < chip->num_slots; i++)
Pierre Ossman44894282008-04-04 19:36:59 +0200604 jmicron_enable_mmc(chip->slots[i]->host, 1);
605 }
Pierre Ossman45211e22008-03-24 13:09:09 +0100606
607 ret = jmicron_pmos(chip, 1);
608 if (ret) {
609 dev_err(&chip->pdev->dev, "Failure enabling card power\n");
610 return ret;
611 }
612
613 return 0;
614}
615
Jennifer Li26daa1e2010-11-17 23:01:59 -0500616static const struct sdhci_pci_fixes sdhci_o2 = {
617 .probe = o2_probe,
618};
619
Pierre Ossman22606402008-03-23 19:33:23 +0100620static const struct sdhci_pci_fixes sdhci_jmicron = {
Pierre Ossman45211e22008-03-24 13:09:09 +0100621 .probe = jmicron_probe,
622
Pierre Ossman44894282008-04-04 19:36:59 +0200623 .probe_slot = jmicron_probe_slot,
624 .remove_slot = jmicron_remove_slot,
625
626 .suspend = jmicron_suspend,
Pierre Ossman45211e22008-03-24 13:09:09 +0100627 .resume = jmicron_resume,
Pierre Ossman22606402008-03-23 19:33:23 +0100628};
629
Nicolas Pitrea7a61862009-12-14 18:01:26 -0800630/* SysKonnect CardBus2SDIO extra registers */
631#define SYSKT_CTRL 0x200
632#define SYSKT_RDFIFO_STAT 0x204
633#define SYSKT_WRFIFO_STAT 0x208
634#define SYSKT_POWER_DATA 0x20c
635#define SYSKT_POWER_330 0xef
636#define SYSKT_POWER_300 0xf8
637#define SYSKT_POWER_184 0xcc
638#define SYSKT_POWER_CMD 0x20d
639#define SYSKT_POWER_START (1 << 7)
640#define SYSKT_POWER_STATUS 0x20e
641#define SYSKT_POWER_STATUS_OK (1 << 0)
642#define SYSKT_BOARD_REV 0x210
643#define SYSKT_CHIP_REV 0x211
644#define SYSKT_CONF_DATA 0x212
645#define SYSKT_CONF_DATA_1V8 (1 << 2)
646#define SYSKT_CONF_DATA_2V5 (1 << 1)
647#define SYSKT_CONF_DATA_3V3 (1 << 0)
648
649static int syskt_probe(struct sdhci_pci_chip *chip)
650{
651 if ((chip->pdev->class & 0x0000FF) == PCI_SDHCI_IFVENDOR) {
652 chip->pdev->class &= ~0x0000FF;
653 chip->pdev->class |= PCI_SDHCI_IFDMA;
654 }
655 return 0;
656}
657
658static int syskt_probe_slot(struct sdhci_pci_slot *slot)
659{
660 int tm, ps;
661
662 u8 board_rev = readb(slot->host->ioaddr + SYSKT_BOARD_REV);
663 u8 chip_rev = readb(slot->host->ioaddr + SYSKT_CHIP_REV);
664 dev_info(&slot->chip->pdev->dev, "SysKonnect CardBus2SDIO, "
665 "board rev %d.%d, chip rev %d.%d\n",
666 board_rev >> 4, board_rev & 0xf,
667 chip_rev >> 4, chip_rev & 0xf);
668 if (chip_rev >= 0x20)
669 slot->host->quirks |= SDHCI_QUIRK_FORCE_DMA;
670
671 writeb(SYSKT_POWER_330, slot->host->ioaddr + SYSKT_POWER_DATA);
672 writeb(SYSKT_POWER_START, slot->host->ioaddr + SYSKT_POWER_CMD);
673 udelay(50);
674 tm = 10; /* Wait max 1 ms */
675 do {
676 ps = readw(slot->host->ioaddr + SYSKT_POWER_STATUS);
677 if (ps & SYSKT_POWER_STATUS_OK)
678 break;
679 udelay(100);
680 } while (--tm);
681 if (!tm) {
682 dev_err(&slot->chip->pdev->dev,
683 "power regulator never stabilized");
684 writeb(0, slot->host->ioaddr + SYSKT_POWER_CMD);
685 return -ENODEV;
686 }
687
688 return 0;
689}
690
691static const struct sdhci_pci_fixes sdhci_syskt = {
692 .quirks = SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER,
693 .probe = syskt_probe,
694 .probe_slot = syskt_probe_slot,
695};
696
Harald Welte557b0692009-06-18 16:53:38 +0200697static int via_probe(struct sdhci_pci_chip *chip)
698{
699 if (chip->pdev->revision == 0x10)
700 chip->quirks |= SDHCI_QUIRK_DELAY_AFTER_POWER;
701
702 return 0;
703}
704
705static const struct sdhci_pci_fixes sdhci_via = {
706 .probe = via_probe,
707};
708
Bill Pemberton9647f842012-11-19 13:25:11 -0500709static const struct pci_device_id pci_ids[] = {
Pierre Ossman22606402008-03-23 19:33:23 +0100710 {
711 .vendor = PCI_VENDOR_ID_RICOH,
712 .device = PCI_DEVICE_ID_RICOH_R5C822,
713 .subvendor = PCI_ANY_ID,
714 .subdevice = PCI_ANY_ID,
715 .driver_data = (kernel_ulong_t)&sdhci_ricoh,
716 },
717
718 {
Maxim Levitskyccc92c22010-08-10 18:01:42 -0700719 .vendor = PCI_VENDOR_ID_RICOH,
720 .device = 0x843,
721 .subvendor = PCI_ANY_ID,
722 .subdevice = PCI_ANY_ID,
723 .driver_data = (kernel_ulong_t)&sdhci_ricoh_mmc,
724 },
725
726 {
Pablo Castillo568133e2010-08-10 18:02:01 -0700727 .vendor = PCI_VENDOR_ID_RICOH,
728 .device = 0xe822,
729 .subvendor = PCI_ANY_ID,
730 .subdevice = PCI_ANY_ID,
731 .driver_data = (kernel_ulong_t)&sdhci_ricoh_mmc,
732 },
733
734 {
Manoj Iyer5fd11c02011-02-11 16:25:31 -0600735 .vendor = PCI_VENDOR_ID_RICOH,
736 .device = 0xe823,
737 .subvendor = PCI_ANY_ID,
738 .subdevice = PCI_ANY_ID,
739 .driver_data = (kernel_ulong_t)&sdhci_ricoh_mmc,
740 },
741
742 {
Pierre Ossman22606402008-03-23 19:33:23 +0100743 .vendor = PCI_VENDOR_ID_ENE,
744 .device = PCI_DEVICE_ID_ENE_CB712_SD,
745 .subvendor = PCI_ANY_ID,
746 .subdevice = PCI_ANY_ID,
747 .driver_data = (kernel_ulong_t)&sdhci_ene_712,
748 },
749
750 {
751 .vendor = PCI_VENDOR_ID_ENE,
752 .device = PCI_DEVICE_ID_ENE_CB712_SD_2,
753 .subvendor = PCI_ANY_ID,
754 .subdevice = PCI_ANY_ID,
755 .driver_data = (kernel_ulong_t)&sdhci_ene_712,
756 },
757
758 {
759 .vendor = PCI_VENDOR_ID_ENE,
760 .device = PCI_DEVICE_ID_ENE_CB714_SD,
761 .subvendor = PCI_ANY_ID,
762 .subdevice = PCI_ANY_ID,
763 .driver_data = (kernel_ulong_t)&sdhci_ene_714,
764 },
765
766 {
767 .vendor = PCI_VENDOR_ID_ENE,
768 .device = PCI_DEVICE_ID_ENE_CB714_SD_2,
769 .subvendor = PCI_ANY_ID,
770 .subdevice = PCI_ANY_ID,
771 .driver_data = (kernel_ulong_t)&sdhci_ene_714,
772 },
773
774 {
775 .vendor = PCI_VENDOR_ID_MARVELL,
David Woodhouse8c5eb882008-09-03 09:45:57 +0100776 .device = PCI_DEVICE_ID_MARVELL_88ALP01_SD,
Pierre Ossman22606402008-03-23 19:33:23 +0100777 .subvendor = PCI_ANY_ID,
778 .subdevice = PCI_ANY_ID,
779 .driver_data = (kernel_ulong_t)&sdhci_cafe,
780 },
781
782 {
783 .vendor = PCI_VENDOR_ID_JMICRON,
784 .device = PCI_DEVICE_ID_JMICRON_JMB38X_SD,
785 .subvendor = PCI_ANY_ID,
786 .subdevice = PCI_ANY_ID,
787 .driver_data = (kernel_ulong_t)&sdhci_jmicron,
788 },
789
Pierre Ossman44894282008-04-04 19:36:59 +0200790 {
791 .vendor = PCI_VENDOR_ID_JMICRON,
792 .device = PCI_DEVICE_ID_JMICRON_JMB38X_MMC,
793 .subvendor = PCI_ANY_ID,
794 .subdevice = PCI_ANY_ID,
795 .driver_data = (kernel_ulong_t)&sdhci_jmicron,
796 },
797
Harald Welte557b0692009-06-18 16:53:38 +0200798 {
Takashi Iwai8f230f42010-12-08 10:04:30 +0100799 .vendor = PCI_VENDOR_ID_JMICRON,
800 .device = PCI_DEVICE_ID_JMICRON_JMB388_SD,
801 .subvendor = PCI_ANY_ID,
802 .subdevice = PCI_ANY_ID,
803 .driver_data = (kernel_ulong_t)&sdhci_jmicron,
804 },
805
806 {
807 .vendor = PCI_VENDOR_ID_JMICRON,
808 .device = PCI_DEVICE_ID_JMICRON_JMB388_ESD,
809 .subvendor = PCI_ANY_ID,
810 .subdevice = PCI_ANY_ID,
811 .driver_data = (kernel_ulong_t)&sdhci_jmicron,
812 },
813
814 {
Nicolas Pitrea7a61862009-12-14 18:01:26 -0800815 .vendor = PCI_VENDOR_ID_SYSKONNECT,
816 .device = 0x8000,
817 .subvendor = PCI_ANY_ID,
818 .subdevice = PCI_ANY_ID,
819 .driver_data = (kernel_ulong_t)&sdhci_syskt,
820 },
821
822 {
Harald Welte557b0692009-06-18 16:53:38 +0200823 .vendor = PCI_VENDOR_ID_VIA,
824 .device = 0x95d0,
825 .subvendor = PCI_ANY_ID,
826 .subdevice = PCI_ANY_ID,
827 .driver_data = (kernel_ulong_t)&sdhci_via,
828 },
829
Xiaochen Shen29229052010-10-04 15:24:52 +0100830 {
831 .vendor = PCI_VENDOR_ID_INTEL,
Alan Coxf9ee3ea2010-10-04 15:25:11 +0100832 .device = PCI_DEVICE_ID_INTEL_MRST_SD0,
833 .subvendor = PCI_ANY_ID,
834 .subdevice = PCI_ANY_ID,
835 .driver_data = (kernel_ulong_t)&sdhci_intel_mrst_hc0,
836 },
837
838 {
839 .vendor = PCI_VENDOR_ID_INTEL,
840 .device = PCI_DEVICE_ID_INTEL_MRST_SD1,
841 .subvendor = PCI_ANY_ID,
842 .subdevice = PCI_ANY_ID,
Jacob Pan35ac6f02010-11-09 13:57:29 +0000843 .driver_data = (kernel_ulong_t)&sdhci_intel_mrst_hc1_hc2,
844 },
845
846 {
847 .vendor = PCI_VENDOR_ID_INTEL,
848 .device = PCI_DEVICE_ID_INTEL_MRST_SD2,
849 .subvendor = PCI_ANY_ID,
850 .subdevice = PCI_ANY_ID,
851 .driver_data = (kernel_ulong_t)&sdhci_intel_mrst_hc1_hc2,
Alan Coxf9ee3ea2010-10-04 15:25:11 +0100852 },
853
854 {
855 .vendor = PCI_VENDOR_ID_INTEL,
Xiaochen Shen29229052010-10-04 15:24:52 +0100856 .device = PCI_DEVICE_ID_INTEL_MFD_SD,
857 .subvendor = PCI_ANY_ID,
858 .subdevice = PCI_ANY_ID,
859 .driver_data = (kernel_ulong_t)&sdhci_intel_mfd_sd,
860 },
861
862 {
863 .vendor = PCI_VENDOR_ID_INTEL,
864 .device = PCI_DEVICE_ID_INTEL_MFD_SDIO1,
865 .subvendor = PCI_ANY_ID,
866 .subdevice = PCI_ANY_ID,
Adrian Hunter0d013bc2011-06-29 14:23:47 +0300867 .driver_data = (kernel_ulong_t)&sdhci_intel_mfd_sdio,
Xiaochen Shen29229052010-10-04 15:24:52 +0100868 },
869
870 {
871 .vendor = PCI_VENDOR_ID_INTEL,
872 .device = PCI_DEVICE_ID_INTEL_MFD_SDIO2,
873 .subvendor = PCI_ANY_ID,
874 .subdevice = PCI_ANY_ID,
Adrian Hunter0d013bc2011-06-29 14:23:47 +0300875 .driver_data = (kernel_ulong_t)&sdhci_intel_mfd_sdio,
Xiaochen Shen29229052010-10-04 15:24:52 +0100876 },
877
878 {
879 .vendor = PCI_VENDOR_ID_INTEL,
880 .device = PCI_DEVICE_ID_INTEL_MFD_EMMC0,
881 .subvendor = PCI_ANY_ID,
882 .subdevice = PCI_ANY_ID,
Adrian Hunter0d013bc2011-06-29 14:23:47 +0300883 .driver_data = (kernel_ulong_t)&sdhci_intel_mfd_emmc,
Xiaochen Shen29229052010-10-04 15:24:52 +0100884 },
885
886 {
887 .vendor = PCI_VENDOR_ID_INTEL,
888 .device = PCI_DEVICE_ID_INTEL_MFD_EMMC1,
889 .subvendor = PCI_ANY_ID,
890 .subdevice = PCI_ANY_ID,
Adrian Hunter0d013bc2011-06-29 14:23:47 +0300891 .driver_data = (kernel_ulong_t)&sdhci_intel_mfd_emmc,
Xiaochen Shen29229052010-10-04 15:24:52 +0100892 },
893
Jennifer Li26daa1e2010-11-17 23:01:59 -0500894 {
Alexander Stein296e0b02012-03-14 08:38:58 +0100895 .vendor = PCI_VENDOR_ID_INTEL,
896 .device = PCI_DEVICE_ID_INTEL_PCH_SDIO0,
897 .subvendor = PCI_ANY_ID,
898 .subdevice = PCI_ANY_ID,
899 .driver_data = (kernel_ulong_t)&sdhci_intel_pch_sdio,
900 },
901
902 {
903 .vendor = PCI_VENDOR_ID_INTEL,
904 .device = PCI_DEVICE_ID_INTEL_PCH_SDIO1,
905 .subvendor = PCI_ANY_ID,
906 .subdevice = PCI_ANY_ID,
907 .driver_data = (kernel_ulong_t)&sdhci_intel_pch_sdio,
908 },
909
910 {
Adrian Hunter728ef3d2013-04-26 11:27:23 +0300911 .vendor = PCI_VENDOR_ID_INTEL,
912 .device = PCI_DEVICE_ID_INTEL_BYT_EMMC,
913 .subvendor = PCI_ANY_ID,
914 .subdevice = PCI_ANY_ID,
915 .driver_data = (kernel_ulong_t)&sdhci_intel_byt_emmc,
916 },
917
918 {
919 .vendor = PCI_VENDOR_ID_INTEL,
920 .device = PCI_DEVICE_ID_INTEL_BYT_SDIO,
921 .subvendor = PCI_ANY_ID,
922 .subdevice = PCI_ANY_ID,
923 .driver_data = (kernel_ulong_t)&sdhci_intel_byt_sdio,
924 },
925
926 {
927 .vendor = PCI_VENDOR_ID_INTEL,
928 .device = PCI_DEVICE_ID_INTEL_BYT_SD,
929 .subvendor = PCI_ANY_ID,
930 .subdevice = PCI_ANY_ID,
931 .driver_data = (kernel_ulong_t)&sdhci_intel_byt_sd,
932 },
933
934 {
Adrian Hunter30d025c2013-06-20 12:57:59 +0300935 .vendor = PCI_VENDOR_ID_INTEL,
936 .device = PCI_DEVICE_ID_INTEL_BYT_EMMC2,
937 .subvendor = PCI_ANY_ID,
938 .subdevice = PCI_ANY_ID,
939 .driver_data = (kernel_ulong_t)&sdhci_intel_byt_emmc,
940 },
941
942 {
Jennifer Li26daa1e2010-11-17 23:01:59 -0500943 .vendor = PCI_VENDOR_ID_O2,
944 .device = PCI_DEVICE_ID_O2_8120,
945 .subvendor = PCI_ANY_ID,
946 .subdevice = PCI_ANY_ID,
947 .driver_data = (kernel_ulong_t)&sdhci_o2,
948 },
949
950 {
951 .vendor = PCI_VENDOR_ID_O2,
952 .device = PCI_DEVICE_ID_O2_8220,
953 .subvendor = PCI_ANY_ID,
954 .subdevice = PCI_ANY_ID,
955 .driver_data = (kernel_ulong_t)&sdhci_o2,
956 },
957
958 {
959 .vendor = PCI_VENDOR_ID_O2,
960 .device = PCI_DEVICE_ID_O2_8221,
961 .subvendor = PCI_ANY_ID,
962 .subdevice = PCI_ANY_ID,
963 .driver_data = (kernel_ulong_t)&sdhci_o2,
964 },
965
966 {
967 .vendor = PCI_VENDOR_ID_O2,
968 .device = PCI_DEVICE_ID_O2_8320,
969 .subvendor = PCI_ANY_ID,
970 .subdevice = PCI_ANY_ID,
971 .driver_data = (kernel_ulong_t)&sdhci_o2,
972 },
973
974 {
975 .vendor = PCI_VENDOR_ID_O2,
976 .device = PCI_DEVICE_ID_O2_8321,
977 .subvendor = PCI_ANY_ID,
978 .subdevice = PCI_ANY_ID,
979 .driver_data = (kernel_ulong_t)&sdhci_o2,
980 },
981
Pierre Ossman22606402008-03-23 19:33:23 +0100982 { /* Generic SD host controller */
983 PCI_DEVICE_CLASS((PCI_CLASS_SYSTEM_SDHCI << 8), 0xFFFF00)
984 },
985
986 { /* end: all zeroes */ },
987};
988
989MODULE_DEVICE_TABLE(pci, pci_ids);
990
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100991/*****************************************************************************\
992 * *
993 * SDHCI core callbacks *
994 * *
995\*****************************************************************************/
996
997static int sdhci_pci_enable_dma(struct sdhci_host *host)
998{
999 struct sdhci_pci_slot *slot;
1000 struct pci_dev *pdev;
1001 int ret;
1002
1003 slot = sdhci_priv(host);
1004 pdev = slot->chip->pdev;
1005
1006 if (((pdev->class & 0xFFFF00) == (PCI_CLASS_SYSTEM_SDHCI << 8)) &&
1007 ((pdev->class & 0x0000FF) != PCI_SDHCI_IFDMA) &&
Richard Röjforsa13abc72009-09-22 16:45:30 -07001008 (host->flags & SDHCI_USE_SDMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001009 dev_warn(&pdev->dev, "Will use DMA mode even though HW "
1010 "doesn't fully claim to support it.\n");
1011 }
1012
Yang Hongyang284901a2009-04-06 19:01:15 -07001013 ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001014 if (ret)
1015 return ret;
1016
1017 pci_set_master(pdev);
1018
1019 return 0;
1020}
1021
Sascha Hauer7bc088d2013-01-21 19:02:27 +08001022static int sdhci_pci_bus_width(struct sdhci_host *host, int width)
Major Lee68077b02011-06-29 14:23:46 +03001023{
1024 u8 ctrl;
1025
1026 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
1027
1028 switch (width) {
1029 case MMC_BUS_WIDTH_8:
1030 ctrl |= SDHCI_CTRL_8BITBUS;
1031 ctrl &= ~SDHCI_CTRL_4BITBUS;
1032 break;
1033 case MMC_BUS_WIDTH_4:
1034 ctrl |= SDHCI_CTRL_4BITBUS;
1035 ctrl &= ~SDHCI_CTRL_8BITBUS;
1036 break;
1037 default:
1038 ctrl &= ~(SDHCI_CTRL_8BITBUS | SDHCI_CTRL_4BITBUS);
1039 break;
1040 }
1041
1042 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1043
1044 return 0;
1045}
1046
Adrian Hunterc9faff62013-06-13 11:50:26 +03001047static void sdhci_pci_gpio_hw_reset(struct sdhci_host *host)
Adrian Hunter0f201652011-08-29 16:42:13 +03001048{
1049 struct sdhci_pci_slot *slot = sdhci_priv(host);
1050 int rst_n_gpio = slot->rst_n_gpio;
1051
1052 if (!gpio_is_valid(rst_n_gpio))
1053 return;
1054 gpio_set_value_cansleep(rst_n_gpio, 0);
1055 /* For eMMC, minimum is 1us but give it 10us for good measure */
1056 udelay(10);
1057 gpio_set_value_cansleep(rst_n_gpio, 1);
1058 /* For eMMC, minimum is 200us but give it 300us for good measure */
1059 usleep_range(300, 1000);
1060}
1061
Adrian Hunterc9faff62013-06-13 11:50:26 +03001062static void sdhci_pci_hw_reset(struct sdhci_host *host)
1063{
1064 struct sdhci_pci_slot *slot = sdhci_priv(host);
1065
1066 if (slot->hw_reset)
1067 slot->hw_reset(host);
1068}
1069
Lars-Peter Clausenc9155682013-03-13 19:26:05 +01001070static const struct sdhci_ops sdhci_pci_ops = {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001071 .enable_dma = sdhci_pci_enable_dma,
Sascha Hauer7bc088d2013-01-21 19:02:27 +08001072 .platform_bus_width = sdhci_pci_bus_width,
Adrian Hunter0f201652011-08-29 16:42:13 +03001073 .hw_reset = sdhci_pci_hw_reset,
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001074};
1075
1076/*****************************************************************************\
1077 * *
1078 * Suspend/resume *
1079 * *
1080\*****************************************************************************/
1081
1082#ifdef CONFIG_PM
1083
Manuel Lauss29495aa2011-11-03 11:09:45 +01001084static int sdhci_pci_suspend(struct device *dev)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001085{
Manuel Lauss29495aa2011-11-03 11:09:45 +01001086 struct pci_dev *pdev = to_pci_dev(dev);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001087 struct sdhci_pci_chip *chip;
1088 struct sdhci_pci_slot *slot;
Daniel Drake5f619702010-11-04 22:20:39 +00001089 mmc_pm_flag_t slot_pm_flags;
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08001090 mmc_pm_flag_t pm_flags = 0;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001091 int i, ret;
1092
1093 chip = pci_get_drvdata(pdev);
1094 if (!chip)
1095 return 0;
1096
Ameya Palandeb177bc92011-04-05 21:13:13 +03001097 for (i = 0; i < chip->num_slots; i++) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001098 slot = chip->slots[i];
1099 if (!slot)
1100 continue;
1101
Manuel Lauss29495aa2011-11-03 11:09:45 +01001102 ret = sdhci_suspend_host(slot->host);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001103
Axel Linb678b912011-12-03 15:28:05 +08001104 if (ret)
1105 goto err_pci_suspend;
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08001106
Daniel Drake5f619702010-11-04 22:20:39 +00001107 slot_pm_flags = slot->host->mmc->pm_flags;
1108 if (slot_pm_flags & MMC_PM_WAKE_SDIO_IRQ)
1109 sdhci_enable_irq_wakeups(slot->host);
1110
1111 pm_flags |= slot_pm_flags;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001112 }
1113
Pierre Ossman44894282008-04-04 19:36:59 +02001114 if (chip->fixes && chip->fixes->suspend) {
Manuel Lauss29495aa2011-11-03 11:09:45 +01001115 ret = chip->fixes->suspend(chip);
Axel Linb678b912011-12-03 15:28:05 +08001116 if (ret)
1117 goto err_pci_suspend;
Pierre Ossman44894282008-04-04 19:36:59 +02001118 }
1119
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001120 pci_save_state(pdev);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08001121 if (pm_flags & MMC_PM_KEEP_POWER) {
Daniel Drake5f619702010-11-04 22:20:39 +00001122 if (pm_flags & MMC_PM_WAKE_SDIO_IRQ) {
1123 pci_pme_active(pdev, true);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08001124 pci_enable_wake(pdev, PCI_D3hot, 1);
Daniel Drake5f619702010-11-04 22:20:39 +00001125 }
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08001126 pci_set_power_state(pdev, PCI_D3hot);
1127 } else {
Manuel Lauss29495aa2011-11-03 11:09:45 +01001128 pci_enable_wake(pdev, PCI_D3hot, 0);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08001129 pci_disable_device(pdev);
Manuel Lauss29495aa2011-11-03 11:09:45 +01001130 pci_set_power_state(pdev, PCI_D3hot);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08001131 }
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001132
1133 return 0;
Axel Linb678b912011-12-03 15:28:05 +08001134
1135err_pci_suspend:
1136 while (--i >= 0)
1137 sdhci_resume_host(chip->slots[i]->host);
1138 return ret;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001139}
1140
Manuel Lauss29495aa2011-11-03 11:09:45 +01001141static int sdhci_pci_resume(struct device *dev)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001142{
Manuel Lauss29495aa2011-11-03 11:09:45 +01001143 struct pci_dev *pdev = to_pci_dev(dev);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001144 struct sdhci_pci_chip *chip;
1145 struct sdhci_pci_slot *slot;
1146 int i, ret;
1147
1148 chip = pci_get_drvdata(pdev);
1149 if (!chip)
1150 return 0;
1151
1152 pci_set_power_state(pdev, PCI_D0);
1153 pci_restore_state(pdev);
1154 ret = pci_enable_device(pdev);
1155 if (ret)
1156 return ret;
1157
Pierre Ossman45211e22008-03-24 13:09:09 +01001158 if (chip->fixes && chip->fixes->resume) {
1159 ret = chip->fixes->resume(chip);
1160 if (ret)
1161 return ret;
1162 }
1163
Ameya Palandeb177bc92011-04-05 21:13:13 +03001164 for (i = 0; i < chip->num_slots; i++) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001165 slot = chip->slots[i];
1166 if (!slot)
1167 continue;
1168
1169 ret = sdhci_resume_host(slot->host);
1170 if (ret)
1171 return ret;
1172 }
1173
1174 return 0;
1175}
1176
1177#else /* CONFIG_PM */
1178
1179#define sdhci_pci_suspend NULL
1180#define sdhci_pci_resume NULL
1181
1182#endif /* CONFIG_PM */
1183
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001184#ifdef CONFIG_PM_RUNTIME
1185
1186static int sdhci_pci_runtime_suspend(struct device *dev)
1187{
1188 struct pci_dev *pdev = container_of(dev, struct pci_dev, dev);
1189 struct sdhci_pci_chip *chip;
1190 struct sdhci_pci_slot *slot;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001191 int i, ret;
1192
1193 chip = pci_get_drvdata(pdev);
1194 if (!chip)
1195 return 0;
1196
1197 for (i = 0; i < chip->num_slots; i++) {
1198 slot = chip->slots[i];
1199 if (!slot)
1200 continue;
1201
1202 ret = sdhci_runtime_suspend_host(slot->host);
1203
Axel Linb678b912011-12-03 15:28:05 +08001204 if (ret)
1205 goto err_pci_runtime_suspend;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001206 }
1207
1208 if (chip->fixes && chip->fixes->suspend) {
Manuel Lauss29495aa2011-11-03 11:09:45 +01001209 ret = chip->fixes->suspend(chip);
Axel Linb678b912011-12-03 15:28:05 +08001210 if (ret)
1211 goto err_pci_runtime_suspend;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001212 }
1213
1214 return 0;
Axel Linb678b912011-12-03 15:28:05 +08001215
1216err_pci_runtime_suspend:
1217 while (--i >= 0)
1218 sdhci_runtime_resume_host(chip->slots[i]->host);
1219 return ret;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001220}
1221
1222static int sdhci_pci_runtime_resume(struct device *dev)
1223{
1224 struct pci_dev *pdev = container_of(dev, struct pci_dev, dev);
1225 struct sdhci_pci_chip *chip;
1226 struct sdhci_pci_slot *slot;
1227 int i, ret;
1228
1229 chip = pci_get_drvdata(pdev);
1230 if (!chip)
1231 return 0;
1232
1233 if (chip->fixes && chip->fixes->resume) {
1234 ret = chip->fixes->resume(chip);
1235 if (ret)
1236 return ret;
1237 }
1238
1239 for (i = 0; i < chip->num_slots; i++) {
1240 slot = chip->slots[i];
1241 if (!slot)
1242 continue;
1243
1244 ret = sdhci_runtime_resume_host(slot->host);
1245 if (ret)
1246 return ret;
1247 }
1248
1249 return 0;
1250}
1251
1252static int sdhci_pci_runtime_idle(struct device *dev)
1253{
1254 return 0;
1255}
1256
1257#else
1258
1259#define sdhci_pci_runtime_suspend NULL
1260#define sdhci_pci_runtime_resume NULL
1261#define sdhci_pci_runtime_idle NULL
1262
1263#endif
1264
1265static const struct dev_pm_ops sdhci_pci_pm_ops = {
Manuel Lauss29495aa2011-11-03 11:09:45 +01001266 .suspend = sdhci_pci_suspend,
1267 .resume = sdhci_pci_resume,
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001268 .runtime_suspend = sdhci_pci_runtime_suspend,
1269 .runtime_resume = sdhci_pci_runtime_resume,
1270 .runtime_idle = sdhci_pci_runtime_idle,
1271};
1272
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001273/*****************************************************************************\
1274 * *
1275 * Device probing/removal *
1276 * *
1277\*****************************************************************************/
1278
Bill Pembertonc3be1ef2012-11-19 13:23:06 -05001279static struct sdhci_pci_slot *sdhci_pci_probe_slot(
Adrian Hunter52c506f2011-12-27 15:48:43 +02001280 struct pci_dev *pdev, struct sdhci_pci_chip *chip, int first_bar,
1281 int slotno)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001282{
1283 struct sdhci_pci_slot *slot;
1284 struct sdhci_host *host;
Adrian Hunter52c506f2011-12-27 15:48:43 +02001285 int ret, bar = first_bar + slotno;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001286
1287 if (!(pci_resource_flags(pdev, bar) & IORESOURCE_MEM)) {
1288 dev_err(&pdev->dev, "BAR %d is not iomem. Aborting.\n", bar);
1289 return ERR_PTR(-ENODEV);
1290 }
1291
Adrian Hunter90b3e6c2012-10-18 09:54:31 +03001292 if (pci_resource_len(pdev, bar) < 0x100) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001293 dev_err(&pdev->dev, "Invalid iomem size. You may "
1294 "experience problems.\n");
1295 }
1296
1297 if ((pdev->class & 0x0000FF) == PCI_SDHCI_IFVENDOR) {
1298 dev_err(&pdev->dev, "Vendor specific interface. Aborting.\n");
1299 return ERR_PTR(-ENODEV);
1300 }
1301
1302 if ((pdev->class & 0x0000FF) > PCI_SDHCI_IFVENDOR) {
1303 dev_err(&pdev->dev, "Unknown interface. Aborting.\n");
1304 return ERR_PTR(-ENODEV);
1305 }
1306
1307 host = sdhci_alloc_host(&pdev->dev, sizeof(struct sdhci_pci_slot));
1308 if (IS_ERR(host)) {
Dan Carpenterc60a32c2009-04-10 23:31:10 +02001309 dev_err(&pdev->dev, "cannot allocate host\n");
Julia Lawalldc0fd7b2010-05-26 14:42:11 -07001310 return ERR_CAST(host);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001311 }
1312
1313 slot = sdhci_priv(host);
1314
1315 slot->chip = chip;
1316 slot->host = host;
1317 slot->pci_bar = bar;
Adrian Hunter0f201652011-08-29 16:42:13 +03001318 slot->rst_n_gpio = -EINVAL;
Adrian Hunterc5e027a2011-12-27 15:48:44 +02001319 slot->cd_gpio = -EINVAL;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001320
Adrian Hunter52c506f2011-12-27 15:48:43 +02001321 /* Retrieve platform data if there is any */
1322 if (*sdhci_pci_get_data)
1323 slot->data = sdhci_pci_get_data(pdev, slotno);
1324
1325 if (slot->data) {
1326 if (slot->data->setup) {
1327 ret = slot->data->setup(slot->data);
1328 if (ret) {
1329 dev_err(&pdev->dev, "platform setup failed\n");
1330 goto free;
1331 }
1332 }
Adrian Hunterc5e027a2011-12-27 15:48:44 +02001333 slot->rst_n_gpio = slot->data->rst_n_gpio;
1334 slot->cd_gpio = slot->data->cd_gpio;
Adrian Hunter52c506f2011-12-27 15:48:43 +02001335 }
1336
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001337 host->hw_name = "PCI";
1338 host->ops = &sdhci_pci_ops;
1339 host->quirks = chip->quirks;
Adrian Hunterf3c55a72012-02-07 14:48:55 +02001340 host->quirks2 = chip->quirks2;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001341
1342 host->irq = pdev->irq;
1343
1344 ret = pci_request_region(pdev, bar, mmc_hostname(host->mmc));
1345 if (ret) {
1346 dev_err(&pdev->dev, "cannot request region\n");
Adrian Hunter52c506f2011-12-27 15:48:43 +02001347 goto cleanup;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001348 }
1349
Arjan van de Ven092f82e2008-09-28 16:15:56 -07001350 host->ioaddr = pci_ioremap_bar(pdev, bar);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001351 if (!host->ioaddr) {
1352 dev_err(&pdev->dev, "failed to remap registers\n");
Chris Ball9fdcdbb2011-03-29 00:46:12 -04001353 ret = -ENOMEM;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001354 goto release;
1355 }
1356
Pierre Ossman44894282008-04-04 19:36:59 +02001357 if (chip->fixes && chip->fixes->probe_slot) {
1358 ret = chip->fixes->probe_slot(slot);
1359 if (ret)
1360 goto unmap;
1361 }
1362
Adrian Hunterc5e027a2011-12-27 15:48:44 +02001363 if (gpio_is_valid(slot->rst_n_gpio)) {
1364 if (!gpio_request(slot->rst_n_gpio, "eMMC_reset")) {
1365 gpio_direction_output(slot->rst_n_gpio, 1);
1366 slot->host->mmc->caps |= MMC_CAP_HW_RESET;
Adrian Hunterc9faff62013-06-13 11:50:26 +03001367 slot->hw_reset = sdhci_pci_gpio_hw_reset;
Adrian Hunterc5e027a2011-12-27 15:48:44 +02001368 } else {
1369 dev_warn(&pdev->dev, "failed to request rst_n_gpio\n");
1370 slot->rst_n_gpio = -EINVAL;
1371 }
1372 }
1373
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08001374 host->mmc->pm_caps = MMC_PM_KEEP_POWER | MMC_PM_WAKE_SDIO_IRQ;
Aaron Lueed222a2013-03-05 11:24:52 +08001375 host->mmc->slotno = slotno;
Adrian Huntera08b17b2013-04-15 11:27:25 -04001376 host->mmc->caps2 |= MMC_CAP2_NO_PRESCAN_POWERUP;
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08001377
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001378 ret = sdhci_add_host(host);
1379 if (ret)
Pierre Ossman44894282008-04-04 19:36:59 +02001380 goto remove;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001381
Adrian Hunterc5e027a2011-12-27 15:48:44 +02001382 sdhci_pci_add_own_cd(slot);
1383
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001384 return slot;
1385
Pierre Ossman44894282008-04-04 19:36:59 +02001386remove:
Adrian Hunterc5e027a2011-12-27 15:48:44 +02001387 if (gpio_is_valid(slot->rst_n_gpio))
1388 gpio_free(slot->rst_n_gpio);
1389
Pierre Ossman44894282008-04-04 19:36:59 +02001390 if (chip->fixes && chip->fixes->remove_slot)
Pierre Ossman1e728592008-04-16 19:13:13 +02001391 chip->fixes->remove_slot(slot, 0);
Pierre Ossman44894282008-04-04 19:36:59 +02001392
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001393unmap:
1394 iounmap(host->ioaddr);
1395
1396release:
1397 pci_release_region(pdev, bar);
Dan Carpenterc60a32c2009-04-10 23:31:10 +02001398
Adrian Hunter52c506f2011-12-27 15:48:43 +02001399cleanup:
1400 if (slot->data && slot->data->cleanup)
1401 slot->data->cleanup(slot->data);
1402
Dan Carpenterc60a32c2009-04-10 23:31:10 +02001403free:
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001404 sdhci_free_host(host);
1405
1406 return ERR_PTR(ret);
1407}
1408
1409static void sdhci_pci_remove_slot(struct sdhci_pci_slot *slot)
1410{
Pierre Ossman1e728592008-04-16 19:13:13 +02001411 int dead;
1412 u32 scratch;
1413
Adrian Hunterc5e027a2011-12-27 15:48:44 +02001414 sdhci_pci_remove_own_cd(slot);
1415
Pierre Ossman1e728592008-04-16 19:13:13 +02001416 dead = 0;
1417 scratch = readl(slot->host->ioaddr + SDHCI_INT_STATUS);
1418 if (scratch == (u32)-1)
1419 dead = 1;
1420
1421 sdhci_remove_host(slot->host, dead);
Pierre Ossman44894282008-04-04 19:36:59 +02001422
Adrian Hunterc5e027a2011-12-27 15:48:44 +02001423 if (gpio_is_valid(slot->rst_n_gpio))
1424 gpio_free(slot->rst_n_gpio);
1425
Pierre Ossman44894282008-04-04 19:36:59 +02001426 if (slot->chip->fixes && slot->chip->fixes->remove_slot)
Pierre Ossman1e728592008-04-16 19:13:13 +02001427 slot->chip->fixes->remove_slot(slot, dead);
Pierre Ossman44894282008-04-04 19:36:59 +02001428
Adrian Hunter52c506f2011-12-27 15:48:43 +02001429 if (slot->data && slot->data->cleanup)
1430 slot->data->cleanup(slot->data);
1431
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001432 pci_release_region(slot->chip->pdev, slot->pci_bar);
Pierre Ossman44894282008-04-04 19:36:59 +02001433
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001434 sdhci_free_host(slot->host);
1435}
1436
Bill Pembertonc3be1ef2012-11-19 13:23:06 -05001437static void sdhci_pci_runtime_pm_allow(struct device *dev)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001438{
1439 pm_runtime_put_noidle(dev);
1440 pm_runtime_allow(dev);
1441 pm_runtime_set_autosuspend_delay(dev, 50);
1442 pm_runtime_use_autosuspend(dev);
1443 pm_suspend_ignore_children(dev, 1);
1444}
1445
Bill Pemberton6e0ee712012-11-19 13:26:03 -05001446static void sdhci_pci_runtime_pm_forbid(struct device *dev)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001447{
1448 pm_runtime_forbid(dev);
1449 pm_runtime_get_noresume(dev);
1450}
1451
Bill Pembertonc3be1ef2012-11-19 13:23:06 -05001452static int sdhci_pci_probe(struct pci_dev *pdev,
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001453 const struct pci_device_id *ent)
1454{
1455 struct sdhci_pci_chip *chip;
1456 struct sdhci_pci_slot *slot;
1457
Sergei Shtylyovcf5e23e2011-03-17 16:46:17 -04001458 u8 slots, first_bar;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001459 int ret, i;
1460
1461 BUG_ON(pdev == NULL);
1462 BUG_ON(ent == NULL);
1463
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001464 dev_info(&pdev->dev, "SDHCI controller found [%04x:%04x] (rev %x)\n",
Sergei Shtylyovcf5e23e2011-03-17 16:46:17 -04001465 (int)pdev->vendor, (int)pdev->device, (int)pdev->revision);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001466
1467 ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &slots);
1468 if (ret)
1469 return ret;
1470
1471 slots = PCI_SLOT_INFO_SLOTS(slots) + 1;
1472 dev_dbg(&pdev->dev, "found %d slot(s)\n", slots);
1473 if (slots == 0)
1474 return -ENODEV;
1475
1476 BUG_ON(slots > MAX_SLOTS);
1477
1478 ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &first_bar);
1479 if (ret)
1480 return ret;
1481
1482 first_bar &= PCI_SLOT_INFO_FIRST_BAR_MASK;
1483
1484 if (first_bar > 5) {
1485 dev_err(&pdev->dev, "Invalid first BAR. Aborting.\n");
1486 return -ENODEV;
1487 }
1488
1489 ret = pci_enable_device(pdev);
1490 if (ret)
1491 return ret;
1492
1493 chip = kzalloc(sizeof(struct sdhci_pci_chip), GFP_KERNEL);
1494 if (!chip) {
1495 ret = -ENOMEM;
1496 goto err;
1497 }
1498
1499 chip->pdev = pdev;
Ameya Palandeb177bc92011-04-05 21:13:13 +03001500 chip->fixes = (const struct sdhci_pci_fixes *)ent->driver_data;
Adrian Hunterc43fd772011-10-17 10:52:44 +03001501 if (chip->fixes) {
Pierre Ossman22606402008-03-23 19:33:23 +01001502 chip->quirks = chip->fixes->quirks;
Adrian Hunterf3c55a72012-02-07 14:48:55 +02001503 chip->quirks2 = chip->fixes->quirks2;
Adrian Hunterc43fd772011-10-17 10:52:44 +03001504 chip->allow_runtime_pm = chip->fixes->allow_runtime_pm;
1505 }
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001506 chip->num_slots = slots;
1507
1508 pci_set_drvdata(pdev, chip);
1509
Pierre Ossman22606402008-03-23 19:33:23 +01001510 if (chip->fixes && chip->fixes->probe) {
1511 ret = chip->fixes->probe(chip);
1512 if (ret)
1513 goto free;
1514 }
1515
Alan Cox225d85f2010-10-04 15:24:21 +01001516 slots = chip->num_slots; /* Quirk may have changed this */
1517
Ameya Palandeb177bc92011-04-05 21:13:13 +03001518 for (i = 0; i < slots; i++) {
Adrian Hunter52c506f2011-12-27 15:48:43 +02001519 slot = sdhci_pci_probe_slot(pdev, chip, first_bar, i);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001520 if (IS_ERR(slot)) {
Ameya Palandeb177bc92011-04-05 21:13:13 +03001521 for (i--; i >= 0; i--)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001522 sdhci_pci_remove_slot(chip->slots[i]);
1523 ret = PTR_ERR(slot);
1524 goto free;
1525 }
1526
1527 chip->slots[i] = slot;
1528 }
1529
Adrian Hunterc43fd772011-10-17 10:52:44 +03001530 if (chip->allow_runtime_pm)
1531 sdhci_pci_runtime_pm_allow(&pdev->dev);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001532
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001533 return 0;
1534
1535free:
1536 pci_set_drvdata(pdev, NULL);
1537 kfree(chip);
1538
1539err:
1540 pci_disable_device(pdev);
1541 return ret;
1542}
1543
Bill Pemberton6e0ee712012-11-19 13:26:03 -05001544static void sdhci_pci_remove(struct pci_dev *pdev)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001545{
1546 int i;
1547 struct sdhci_pci_chip *chip;
1548
1549 chip = pci_get_drvdata(pdev);
1550
1551 if (chip) {
Adrian Hunterc43fd772011-10-17 10:52:44 +03001552 if (chip->allow_runtime_pm)
1553 sdhci_pci_runtime_pm_forbid(&pdev->dev);
1554
Ameya Palandeb177bc92011-04-05 21:13:13 +03001555 for (i = 0; i < chip->num_slots; i++)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001556 sdhci_pci_remove_slot(chip->slots[i]);
1557
1558 pci_set_drvdata(pdev, NULL);
1559 kfree(chip);
1560 }
1561
1562 pci_disable_device(pdev);
1563}
1564
1565static struct pci_driver sdhci_driver = {
Ameya Palandeb177bc92011-04-05 21:13:13 +03001566 .name = "sdhci-pci",
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001567 .id_table = pci_ids,
Ameya Palandeb177bc92011-04-05 21:13:13 +03001568 .probe = sdhci_pci_probe,
Bill Pemberton0433c142012-11-19 13:20:26 -05001569 .remove = sdhci_pci_remove,
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001570 .driver = {
1571 .pm = &sdhci_pci_pm_ops
1572 },
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001573};
1574
Sachin Kamatacc69642012-08-27 11:57:02 +05301575module_pci_driver(sdhci_driver);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001576
Pierre Ossman32710e82009-04-08 20:14:54 +02001577MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001578MODULE_DESCRIPTION("Secure Digital Host Controller Interface PCI driver");
1579MODULE_LICENSE("GPL");