blob: e1ae855960b9c97259861fd594afc39e22cefa3b [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>
17#include <linux/pci.h>
18#include <linux/dma-mapping.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
Maxim Levitskyccc92c22010-08-10 18:01:42 -070020#include <linux/device.h>
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010021#include <linux/mmc/host.h>
Ameya Palandeb177bc92011-04-05 21:13:13 +030022#include <linux/scatterlist.h>
23#include <linux/io.h>
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010024
25#include "sdhci.h"
26
27/*
28 * PCI registers
29 */
30
31#define PCI_SDHCI_IFPIO 0x00
32#define PCI_SDHCI_IFDMA 0x01
33#define PCI_SDHCI_IFVENDOR 0x02
34
35#define PCI_SLOT_INFO 0x40 /* 8 bits */
36#define PCI_SLOT_INFO_SLOTS(x) ((x >> 4) & 7)
37#define PCI_SLOT_INFO_FIRST_BAR_MASK 0x07
38
39#define MAX_SLOTS 8
40
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010041struct sdhci_pci_chip;
Pierre Ossman44894282008-04-04 19:36:59 +020042struct sdhci_pci_slot;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010043
Pierre Ossman22606402008-03-23 19:33:23 +010044struct sdhci_pci_fixes {
45 unsigned int quirks;
46
Ameya Palandeb177bc92011-04-05 21:13:13 +030047 int (*probe) (struct sdhci_pci_chip *);
Pierre Ossman45211e22008-03-24 13:09:09 +010048
Ameya Palandeb177bc92011-04-05 21:13:13 +030049 int (*probe_slot) (struct sdhci_pci_slot *);
50 void (*remove_slot) (struct sdhci_pci_slot *, int);
Pierre Ossman44894282008-04-04 19:36:59 +020051
Ameya Palandeb177bc92011-04-05 21:13:13 +030052 int (*suspend) (struct sdhci_pci_chip *,
Pierre Ossman44894282008-04-04 19:36:59 +020053 pm_message_t);
Ameya Palandeb177bc92011-04-05 21:13:13 +030054 int (*resume) (struct sdhci_pci_chip *);
Pierre Ossman22606402008-03-23 19:33:23 +010055};
56
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010057struct sdhci_pci_slot {
58 struct sdhci_pci_chip *chip;
59 struct sdhci_host *host;
60
61 int pci_bar;
62};
63
64struct sdhci_pci_chip {
65 struct pci_dev *pdev;
Pierre Ossman22606402008-03-23 19:33:23 +010066
67 unsigned int quirks;
68 const struct sdhci_pci_fixes *fixes;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010069
70 int num_slots; /* Slots on controller */
71 struct sdhci_pci_slot *slots[MAX_SLOTS]; /* Pointers to host slots */
72};
73
Pierre Ossman22606402008-03-23 19:33:23 +010074
75/*****************************************************************************\
76 * *
77 * Hardware specific quirk handling *
78 * *
79\*****************************************************************************/
80
81static int ricoh_probe(struct sdhci_pci_chip *chip)
82{
Chris Ballc99436f2009-09-22 16:45:22 -070083 if (chip->pdev->subsystem_vendor == PCI_VENDOR_ID_SAMSUNG ||
84 chip->pdev->subsystem_vendor == PCI_VENDOR_ID_SONY)
Pierre Ossman22606402008-03-23 19:33:23 +010085 chip->quirks |= SDHCI_QUIRK_NO_CARD_NO_RESET;
Maxim Levitskyccc92c22010-08-10 18:01:42 -070086 return 0;
87}
Pierre Ossman22606402008-03-23 19:33:23 +010088
Maxim Levitskyccc92c22010-08-10 18:01:42 -070089static int ricoh_mmc_probe_slot(struct sdhci_pci_slot *slot)
90{
91 slot->host->caps =
92 ((0x21 << SDHCI_TIMEOUT_CLK_SHIFT)
93 & SDHCI_TIMEOUT_CLK_MASK) |
94
95 ((0x21 << SDHCI_CLOCK_BASE_SHIFT)
96 & SDHCI_CLOCK_BASE_MASK) |
97
98 SDHCI_TIMEOUT_CLK_UNIT |
99 SDHCI_CAN_VDD_330 |
100 SDHCI_CAN_DO_SDMA;
101 return 0;
102}
103
104static int ricoh_mmc_resume(struct sdhci_pci_chip *chip)
105{
106 /* Apply a delay to allow controller to settle */
107 /* Otherwise it becomes confused if card state changed
108 during suspend */
109 msleep(500);
Pierre Ossman22606402008-03-23 19:33:23 +0100110 return 0;
111}
112
113static const struct sdhci_pci_fixes sdhci_ricoh = {
114 .probe = ricoh_probe,
Vasily Khoruzhick84938292010-03-05 13:43:46 -0800115 .quirks = SDHCI_QUIRK_32BIT_DMA_ADDR |
116 SDHCI_QUIRK_FORCE_DMA |
117 SDHCI_QUIRK_CLOCK_BEFORE_RESET,
Pierre Ossman22606402008-03-23 19:33:23 +0100118};
119
Maxim Levitskyccc92c22010-08-10 18:01:42 -0700120static const struct sdhci_pci_fixes sdhci_ricoh_mmc = {
121 .probe_slot = ricoh_mmc_probe_slot,
122 .resume = ricoh_mmc_resume,
123 .quirks = SDHCI_QUIRK_32BIT_DMA_ADDR |
124 SDHCI_QUIRK_CLOCK_BEFORE_RESET |
125 SDHCI_QUIRK_NO_CARD_NO_RESET |
126 SDHCI_QUIRK_MISSING_CAPS
127};
128
Pierre Ossman22606402008-03-23 19:33:23 +0100129static const struct sdhci_pci_fixes sdhci_ene_712 = {
130 .quirks = SDHCI_QUIRK_SINGLE_POWER_WRITE |
131 SDHCI_QUIRK_BROKEN_DMA,
132};
133
134static const struct sdhci_pci_fixes sdhci_ene_714 = {
135 .quirks = SDHCI_QUIRK_SINGLE_POWER_WRITE |
136 SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS |
137 SDHCI_QUIRK_BROKEN_DMA,
138};
139
140static const struct sdhci_pci_fixes sdhci_cafe = {
141 .quirks = SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER |
Andres Salomona0874892009-03-02 21:48:20 +0100142 SDHCI_QUIRK_NO_BUSY_IRQ |
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200143 SDHCI_QUIRK_BROKEN_TIMEOUT_VAL,
Pierre Ossman22606402008-03-23 19:33:23 +0100144};
145
Major Lee68077b02011-06-29 14:23:46 +0300146static int mrst_hc_probe_slot(struct sdhci_pci_slot *slot)
147{
148 slot->host->mmc->caps |= MMC_CAP_8_BIT_DATA;
149 return 0;
150}
151
Alan Coxf9ee3ea2010-10-04 15:25:11 +0100152/*
153 * ADMA operation is disabled for Moorestown platform due to
154 * hardware bugs.
155 */
Jacob Pan35ac6f02010-11-09 13:57:29 +0000156static int mrst_hc_probe(struct sdhci_pci_chip *chip)
Alan Coxf9ee3ea2010-10-04 15:25:11 +0100157{
158 /*
Jacob Pan35ac6f02010-11-09 13:57:29 +0000159 * slots number is fixed here for MRST as SDIO3/5 are never used and
160 * have hardware bugs.
Alan Coxf9ee3ea2010-10-04 15:25:11 +0100161 */
162 chip->num_slots = 1;
163 return 0;
164}
165
166static const struct sdhci_pci_fixes sdhci_intel_mrst_hc0 = {
167 .quirks = SDHCI_QUIRK_BROKEN_ADMA | SDHCI_QUIRK_NO_HISPD_BIT,
Major Lee68077b02011-06-29 14:23:46 +0300168 .probe_slot = mrst_hc_probe_slot,
Alan Coxf9ee3ea2010-10-04 15:25:11 +0100169};
170
Jacob Pan35ac6f02010-11-09 13:57:29 +0000171static const struct sdhci_pci_fixes sdhci_intel_mrst_hc1_hc2 = {
Alan Coxf9ee3ea2010-10-04 15:25:11 +0100172 .quirks = SDHCI_QUIRK_BROKEN_ADMA | SDHCI_QUIRK_NO_HISPD_BIT,
Jacob Pan35ac6f02010-11-09 13:57:29 +0000173 .probe = mrst_hc_probe,
Alan Coxf9ee3ea2010-10-04 15:25:11 +0100174};
175
Xiaochen Shen29229052010-10-04 15:24:52 +0100176static const struct sdhci_pci_fixes sdhci_intel_mfd_sd = {
177 .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
178};
179
180static const struct sdhci_pci_fixes sdhci_intel_mfd_emmc_sdio = {
181 .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
182};
183
Jennifer Li26daa1e2010-11-17 23:01:59 -0500184/* O2Micro extra registers */
185#define O2_SD_LOCK_WP 0xD3
186#define O2_SD_MULTI_VCC3V 0xEE
187#define O2_SD_CLKREQ 0xEC
188#define O2_SD_CAPS 0xE0
189#define O2_SD_ADMA1 0xE2
190#define O2_SD_ADMA2 0xE7
191#define O2_SD_INF_MOD 0xF1
192
193static int o2_probe(struct sdhci_pci_chip *chip)
194{
195 int ret;
196 u8 scratch;
197
198 switch (chip->pdev->device) {
199 case PCI_DEVICE_ID_O2_8220:
200 case PCI_DEVICE_ID_O2_8221:
201 case PCI_DEVICE_ID_O2_8320:
202 case PCI_DEVICE_ID_O2_8321:
203 /* This extra setup is required due to broken ADMA. */
204 ret = pci_read_config_byte(chip->pdev, O2_SD_LOCK_WP, &scratch);
205 if (ret)
206 return ret;
207 scratch &= 0x7f;
208 pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch);
209
210 /* Set Multi 3 to VCC3V# */
211 pci_write_config_byte(chip->pdev, O2_SD_MULTI_VCC3V, 0x08);
212
213 /* Disable CLK_REQ# support after media DET */
214 ret = pci_read_config_byte(chip->pdev, O2_SD_CLKREQ, &scratch);
215 if (ret)
216 return ret;
217 scratch |= 0x20;
218 pci_write_config_byte(chip->pdev, O2_SD_CLKREQ, scratch);
219
220 /* Choose capabilities, enable SDMA. We have to write 0x01
221 * to the capabilities register first to unlock it.
222 */
223 ret = pci_read_config_byte(chip->pdev, O2_SD_CAPS, &scratch);
224 if (ret)
225 return ret;
226 scratch |= 0x01;
227 pci_write_config_byte(chip->pdev, O2_SD_CAPS, scratch);
228 pci_write_config_byte(chip->pdev, O2_SD_CAPS, 0x73);
229
230 /* Disable ADMA1/2 */
231 pci_write_config_byte(chip->pdev, O2_SD_ADMA1, 0x39);
232 pci_write_config_byte(chip->pdev, O2_SD_ADMA2, 0x08);
233
234 /* Disable the infinite transfer mode */
235 ret = pci_read_config_byte(chip->pdev, O2_SD_INF_MOD, &scratch);
236 if (ret)
237 return ret;
238 scratch |= 0x08;
239 pci_write_config_byte(chip->pdev, O2_SD_INF_MOD, scratch);
240
241 /* Lock WP */
242 ret = pci_read_config_byte(chip->pdev, O2_SD_LOCK_WP, &scratch);
243 if (ret)
244 return ret;
245 scratch |= 0x80;
246 pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch);
247 }
248
249 return 0;
250}
251
Pierre Ossman45211e22008-03-24 13:09:09 +0100252static int jmicron_pmos(struct sdhci_pci_chip *chip, int on)
253{
254 u8 scratch;
255 int ret;
256
257 ret = pci_read_config_byte(chip->pdev, 0xAE, &scratch);
258 if (ret)
259 return ret;
260
261 /*
262 * Turn PMOS on [bit 0], set over current detection to 2.4 V
263 * [bit 1:2] and enable over current debouncing [bit 6].
264 */
265 if (on)
266 scratch |= 0x47;
267 else
268 scratch &= ~0x47;
269
270 ret = pci_write_config_byte(chip->pdev, 0xAE, scratch);
271 if (ret)
272 return ret;
273
274 return 0;
275}
276
277static int jmicron_probe(struct sdhci_pci_chip *chip)
278{
279 int ret;
Takashi Iwai8f230f42010-12-08 10:04:30 +0100280 u16 mmcdev = 0;
Pierre Ossman45211e22008-03-24 13:09:09 +0100281
Pierre Ossman93fc48c2008-06-28 18:21:41 +0200282 if (chip->pdev->revision == 0) {
283 chip->quirks |= SDHCI_QUIRK_32BIT_DMA_ADDR |
284 SDHCI_QUIRK_32BIT_DMA_SIZE |
Pierre Ossman2134a922008-06-28 18:28:51 +0200285 SDHCI_QUIRK_32BIT_ADMA_SIZE |
Pierre Ossman4a3cba32008-07-29 00:11:16 +0200286 SDHCI_QUIRK_RESET_AFTER_REQUEST |
Pierre Ossman86a6a872009-02-02 21:13:49 +0100287 SDHCI_QUIRK_BROKEN_SMALL_PIO;
Pierre Ossman93fc48c2008-06-28 18:21:41 +0200288 }
289
Pierre Ossman45211e22008-03-24 13:09:09 +0100290 /*
Pierre Ossman44894282008-04-04 19:36:59 +0200291 * JMicron chips can have two interfaces to the same hardware
292 * in order to work around limitations in Microsoft's driver.
293 * We need to make sure we only bind to one of them.
294 *
295 * This code assumes two things:
296 *
297 * 1. The PCI code adds subfunctions in order.
298 *
299 * 2. The MMC interface has a lower subfunction number
300 * than the SD interface.
301 */
Takashi Iwai8f230f42010-12-08 10:04:30 +0100302 if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_SD)
303 mmcdev = PCI_DEVICE_ID_JMICRON_JMB38X_MMC;
304 else if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_SD)
305 mmcdev = PCI_DEVICE_ID_JMICRON_JMB388_ESD;
306
307 if (mmcdev) {
Pierre Ossman44894282008-04-04 19:36:59 +0200308 struct pci_dev *sd_dev;
309
310 sd_dev = NULL;
311 while ((sd_dev = pci_get_device(PCI_VENDOR_ID_JMICRON,
Takashi Iwai8f230f42010-12-08 10:04:30 +0100312 mmcdev, sd_dev)) != NULL) {
Pierre Ossman44894282008-04-04 19:36:59 +0200313 if ((PCI_SLOT(chip->pdev->devfn) ==
314 PCI_SLOT(sd_dev->devfn)) &&
315 (chip->pdev->bus == sd_dev->bus))
316 break;
317 }
318
319 if (sd_dev) {
320 pci_dev_put(sd_dev);
321 dev_info(&chip->pdev->dev, "Refusing to bind to "
322 "secondary interface.\n");
323 return -ENODEV;
324 }
325 }
326
327 /*
Pierre Ossman45211e22008-03-24 13:09:09 +0100328 * JMicron chips need a bit of a nudge to enable the power
329 * output pins.
330 */
331 ret = jmicron_pmos(chip, 1);
332 if (ret) {
333 dev_err(&chip->pdev->dev, "Failure enabling card power\n");
334 return ret;
335 }
336
Takashi Iwai82b0e232011-04-21 20:26:38 +0200337 /* quirk for unsable RO-detection on JM388 chips */
338 if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_SD ||
339 chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD)
340 chip->quirks |= SDHCI_QUIRK_UNSTABLE_RO_DETECT;
341
Pierre Ossman45211e22008-03-24 13:09:09 +0100342 return 0;
343}
344
Pierre Ossman44894282008-04-04 19:36:59 +0200345static void jmicron_enable_mmc(struct sdhci_host *host, int on)
346{
347 u8 scratch;
348
349 scratch = readb(host->ioaddr + 0xC0);
350
351 if (on)
352 scratch |= 0x01;
353 else
354 scratch &= ~0x01;
355
356 writeb(scratch, host->ioaddr + 0xC0);
357}
358
359static int jmicron_probe_slot(struct sdhci_pci_slot *slot)
360{
Pierre Ossman2134a922008-06-28 18:28:51 +0200361 if (slot->chip->pdev->revision == 0) {
362 u16 version;
363
364 version = readl(slot->host->ioaddr + SDHCI_HOST_VERSION);
365 version = (version & SDHCI_VENDOR_VER_MASK) >>
366 SDHCI_VENDOR_VER_SHIFT;
367
368 /*
369 * Older versions of the chip have lots of nasty glitches
370 * in the ADMA engine. It's best just to avoid it
371 * completely.
372 */
373 if (version < 0xAC)
374 slot->host->quirks |= SDHCI_QUIRK_BROKEN_ADMA;
375 }
376
Takashi Iwai8f230f42010-12-08 10:04:30 +0100377 /* JM388 MMC doesn't support 1.8V while SD supports it */
378 if (slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD) {
379 slot->host->ocr_avail_sd = MMC_VDD_32_33 | MMC_VDD_33_34 |
380 MMC_VDD_29_30 | MMC_VDD_30_31 |
381 MMC_VDD_165_195; /* allow 1.8V */
382 slot->host->ocr_avail_mmc = MMC_VDD_32_33 | MMC_VDD_33_34 |
383 MMC_VDD_29_30 | MMC_VDD_30_31; /* no 1.8V for MMC */
384 }
385
Pierre Ossman44894282008-04-04 19:36:59 +0200386 /*
387 * The secondary interface requires a bit set to get the
388 * interrupts.
389 */
Takashi Iwai8f230f42010-12-08 10:04:30 +0100390 if (slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC ||
391 slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD)
Pierre Ossman44894282008-04-04 19:36:59 +0200392 jmicron_enable_mmc(slot->host, 1);
393
Takashi Iwaid75c1082010-12-16 17:54:14 +0100394 slot->host->mmc->caps |= MMC_CAP_BUS_WIDTH_TEST;
395
Pierre Ossman44894282008-04-04 19:36:59 +0200396 return 0;
397}
398
Pierre Ossman1e728592008-04-16 19:13:13 +0200399static void jmicron_remove_slot(struct sdhci_pci_slot *slot, int dead)
Pierre Ossman44894282008-04-04 19:36:59 +0200400{
Pierre Ossman1e728592008-04-16 19:13:13 +0200401 if (dead)
402 return;
403
Takashi Iwai8f230f42010-12-08 10:04:30 +0100404 if (slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC ||
405 slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD)
Pierre Ossman44894282008-04-04 19:36:59 +0200406 jmicron_enable_mmc(slot->host, 0);
407}
408
409static int jmicron_suspend(struct sdhci_pci_chip *chip, pm_message_t state)
410{
411 int i;
412
Takashi Iwai8f230f42010-12-08 10:04:30 +0100413 if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC ||
414 chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD) {
Ameya Palandeb177bc92011-04-05 21:13:13 +0300415 for (i = 0; i < chip->num_slots; i++)
Pierre Ossman44894282008-04-04 19:36:59 +0200416 jmicron_enable_mmc(chip->slots[i]->host, 0);
417 }
418
419 return 0;
420}
421
Pierre Ossman45211e22008-03-24 13:09:09 +0100422static int jmicron_resume(struct sdhci_pci_chip *chip)
423{
Pierre Ossman44894282008-04-04 19:36:59 +0200424 int ret, i;
425
Takashi Iwai8f230f42010-12-08 10:04:30 +0100426 if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC ||
427 chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD) {
Ameya Palandeb177bc92011-04-05 21:13:13 +0300428 for (i = 0; i < chip->num_slots; i++)
Pierre Ossman44894282008-04-04 19:36:59 +0200429 jmicron_enable_mmc(chip->slots[i]->host, 1);
430 }
Pierre Ossman45211e22008-03-24 13:09:09 +0100431
432 ret = jmicron_pmos(chip, 1);
433 if (ret) {
434 dev_err(&chip->pdev->dev, "Failure enabling card power\n");
435 return ret;
436 }
437
438 return 0;
439}
440
Jennifer Li26daa1e2010-11-17 23:01:59 -0500441static const struct sdhci_pci_fixes sdhci_o2 = {
442 .probe = o2_probe,
443};
444
Pierre Ossman22606402008-03-23 19:33:23 +0100445static const struct sdhci_pci_fixes sdhci_jmicron = {
Pierre Ossman45211e22008-03-24 13:09:09 +0100446 .probe = jmicron_probe,
447
Pierre Ossman44894282008-04-04 19:36:59 +0200448 .probe_slot = jmicron_probe_slot,
449 .remove_slot = jmicron_remove_slot,
450
451 .suspend = jmicron_suspend,
Pierre Ossman45211e22008-03-24 13:09:09 +0100452 .resume = jmicron_resume,
Pierre Ossman22606402008-03-23 19:33:23 +0100453};
454
Nicolas Pitrea7a61862009-12-14 18:01:26 -0800455/* SysKonnect CardBus2SDIO extra registers */
456#define SYSKT_CTRL 0x200
457#define SYSKT_RDFIFO_STAT 0x204
458#define SYSKT_WRFIFO_STAT 0x208
459#define SYSKT_POWER_DATA 0x20c
460#define SYSKT_POWER_330 0xef
461#define SYSKT_POWER_300 0xf8
462#define SYSKT_POWER_184 0xcc
463#define SYSKT_POWER_CMD 0x20d
464#define SYSKT_POWER_START (1 << 7)
465#define SYSKT_POWER_STATUS 0x20e
466#define SYSKT_POWER_STATUS_OK (1 << 0)
467#define SYSKT_BOARD_REV 0x210
468#define SYSKT_CHIP_REV 0x211
469#define SYSKT_CONF_DATA 0x212
470#define SYSKT_CONF_DATA_1V8 (1 << 2)
471#define SYSKT_CONF_DATA_2V5 (1 << 1)
472#define SYSKT_CONF_DATA_3V3 (1 << 0)
473
474static int syskt_probe(struct sdhci_pci_chip *chip)
475{
476 if ((chip->pdev->class & 0x0000FF) == PCI_SDHCI_IFVENDOR) {
477 chip->pdev->class &= ~0x0000FF;
478 chip->pdev->class |= PCI_SDHCI_IFDMA;
479 }
480 return 0;
481}
482
483static int syskt_probe_slot(struct sdhci_pci_slot *slot)
484{
485 int tm, ps;
486
487 u8 board_rev = readb(slot->host->ioaddr + SYSKT_BOARD_REV);
488 u8 chip_rev = readb(slot->host->ioaddr + SYSKT_CHIP_REV);
489 dev_info(&slot->chip->pdev->dev, "SysKonnect CardBus2SDIO, "
490 "board rev %d.%d, chip rev %d.%d\n",
491 board_rev >> 4, board_rev & 0xf,
492 chip_rev >> 4, chip_rev & 0xf);
493 if (chip_rev >= 0x20)
494 slot->host->quirks |= SDHCI_QUIRK_FORCE_DMA;
495
496 writeb(SYSKT_POWER_330, slot->host->ioaddr + SYSKT_POWER_DATA);
497 writeb(SYSKT_POWER_START, slot->host->ioaddr + SYSKT_POWER_CMD);
498 udelay(50);
499 tm = 10; /* Wait max 1 ms */
500 do {
501 ps = readw(slot->host->ioaddr + SYSKT_POWER_STATUS);
502 if (ps & SYSKT_POWER_STATUS_OK)
503 break;
504 udelay(100);
505 } while (--tm);
506 if (!tm) {
507 dev_err(&slot->chip->pdev->dev,
508 "power regulator never stabilized");
509 writeb(0, slot->host->ioaddr + SYSKT_POWER_CMD);
510 return -ENODEV;
511 }
512
513 return 0;
514}
515
516static const struct sdhci_pci_fixes sdhci_syskt = {
517 .quirks = SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER,
518 .probe = syskt_probe,
519 .probe_slot = syskt_probe_slot,
520};
521
Harald Welte557b0692009-06-18 16:53:38 +0200522static int via_probe(struct sdhci_pci_chip *chip)
523{
524 if (chip->pdev->revision == 0x10)
525 chip->quirks |= SDHCI_QUIRK_DELAY_AFTER_POWER;
526
527 return 0;
528}
529
530static const struct sdhci_pci_fixes sdhci_via = {
531 .probe = via_probe,
532};
533
Pierre Ossman22606402008-03-23 19:33:23 +0100534static const struct pci_device_id pci_ids[] __devinitdata = {
535 {
536 .vendor = PCI_VENDOR_ID_RICOH,
537 .device = PCI_DEVICE_ID_RICOH_R5C822,
538 .subvendor = PCI_ANY_ID,
539 .subdevice = PCI_ANY_ID,
540 .driver_data = (kernel_ulong_t)&sdhci_ricoh,
541 },
542
543 {
Maxim Levitskyccc92c22010-08-10 18:01:42 -0700544 .vendor = PCI_VENDOR_ID_RICOH,
545 .device = 0x843,
546 .subvendor = PCI_ANY_ID,
547 .subdevice = PCI_ANY_ID,
548 .driver_data = (kernel_ulong_t)&sdhci_ricoh_mmc,
549 },
550
551 {
Pablo Castillo568133e2010-08-10 18:02:01 -0700552 .vendor = PCI_VENDOR_ID_RICOH,
553 .device = 0xe822,
554 .subvendor = PCI_ANY_ID,
555 .subdevice = PCI_ANY_ID,
556 .driver_data = (kernel_ulong_t)&sdhci_ricoh_mmc,
557 },
558
559 {
Manoj Iyer5fd11c02011-02-11 16:25:31 -0600560 .vendor = PCI_VENDOR_ID_RICOH,
561 .device = 0xe823,
562 .subvendor = PCI_ANY_ID,
563 .subdevice = PCI_ANY_ID,
564 .driver_data = (kernel_ulong_t)&sdhci_ricoh_mmc,
565 },
566
567 {
Pierre Ossman22606402008-03-23 19:33:23 +0100568 .vendor = PCI_VENDOR_ID_ENE,
569 .device = PCI_DEVICE_ID_ENE_CB712_SD,
570 .subvendor = PCI_ANY_ID,
571 .subdevice = PCI_ANY_ID,
572 .driver_data = (kernel_ulong_t)&sdhci_ene_712,
573 },
574
575 {
576 .vendor = PCI_VENDOR_ID_ENE,
577 .device = PCI_DEVICE_ID_ENE_CB712_SD_2,
578 .subvendor = PCI_ANY_ID,
579 .subdevice = PCI_ANY_ID,
580 .driver_data = (kernel_ulong_t)&sdhci_ene_712,
581 },
582
583 {
584 .vendor = PCI_VENDOR_ID_ENE,
585 .device = PCI_DEVICE_ID_ENE_CB714_SD,
586 .subvendor = PCI_ANY_ID,
587 .subdevice = PCI_ANY_ID,
588 .driver_data = (kernel_ulong_t)&sdhci_ene_714,
589 },
590
591 {
592 .vendor = PCI_VENDOR_ID_ENE,
593 .device = PCI_DEVICE_ID_ENE_CB714_SD_2,
594 .subvendor = PCI_ANY_ID,
595 .subdevice = PCI_ANY_ID,
596 .driver_data = (kernel_ulong_t)&sdhci_ene_714,
597 },
598
599 {
600 .vendor = PCI_VENDOR_ID_MARVELL,
David Woodhouse8c5eb882008-09-03 09:45:57 +0100601 .device = PCI_DEVICE_ID_MARVELL_88ALP01_SD,
Pierre Ossman22606402008-03-23 19:33:23 +0100602 .subvendor = PCI_ANY_ID,
603 .subdevice = PCI_ANY_ID,
604 .driver_data = (kernel_ulong_t)&sdhci_cafe,
605 },
606
607 {
608 .vendor = PCI_VENDOR_ID_JMICRON,
609 .device = PCI_DEVICE_ID_JMICRON_JMB38X_SD,
610 .subvendor = PCI_ANY_ID,
611 .subdevice = PCI_ANY_ID,
612 .driver_data = (kernel_ulong_t)&sdhci_jmicron,
613 },
614
Pierre Ossman44894282008-04-04 19:36:59 +0200615 {
616 .vendor = PCI_VENDOR_ID_JMICRON,
617 .device = PCI_DEVICE_ID_JMICRON_JMB38X_MMC,
618 .subvendor = PCI_ANY_ID,
619 .subdevice = PCI_ANY_ID,
620 .driver_data = (kernel_ulong_t)&sdhci_jmicron,
621 },
622
Harald Welte557b0692009-06-18 16:53:38 +0200623 {
Takashi Iwai8f230f42010-12-08 10:04:30 +0100624 .vendor = PCI_VENDOR_ID_JMICRON,
625 .device = PCI_DEVICE_ID_JMICRON_JMB388_SD,
626 .subvendor = PCI_ANY_ID,
627 .subdevice = PCI_ANY_ID,
628 .driver_data = (kernel_ulong_t)&sdhci_jmicron,
629 },
630
631 {
632 .vendor = PCI_VENDOR_ID_JMICRON,
633 .device = PCI_DEVICE_ID_JMICRON_JMB388_ESD,
634 .subvendor = PCI_ANY_ID,
635 .subdevice = PCI_ANY_ID,
636 .driver_data = (kernel_ulong_t)&sdhci_jmicron,
637 },
638
639 {
Nicolas Pitrea7a61862009-12-14 18:01:26 -0800640 .vendor = PCI_VENDOR_ID_SYSKONNECT,
641 .device = 0x8000,
642 .subvendor = PCI_ANY_ID,
643 .subdevice = PCI_ANY_ID,
644 .driver_data = (kernel_ulong_t)&sdhci_syskt,
645 },
646
647 {
Harald Welte557b0692009-06-18 16:53:38 +0200648 .vendor = PCI_VENDOR_ID_VIA,
649 .device = 0x95d0,
650 .subvendor = PCI_ANY_ID,
651 .subdevice = PCI_ANY_ID,
652 .driver_data = (kernel_ulong_t)&sdhci_via,
653 },
654
Xiaochen Shen29229052010-10-04 15:24:52 +0100655 {
656 .vendor = PCI_VENDOR_ID_INTEL,
Alan Coxf9ee3ea2010-10-04 15:25:11 +0100657 .device = PCI_DEVICE_ID_INTEL_MRST_SD0,
658 .subvendor = PCI_ANY_ID,
659 .subdevice = PCI_ANY_ID,
660 .driver_data = (kernel_ulong_t)&sdhci_intel_mrst_hc0,
661 },
662
663 {
664 .vendor = PCI_VENDOR_ID_INTEL,
665 .device = PCI_DEVICE_ID_INTEL_MRST_SD1,
666 .subvendor = PCI_ANY_ID,
667 .subdevice = PCI_ANY_ID,
Jacob Pan35ac6f02010-11-09 13:57:29 +0000668 .driver_data = (kernel_ulong_t)&sdhci_intel_mrst_hc1_hc2,
669 },
670
671 {
672 .vendor = PCI_VENDOR_ID_INTEL,
673 .device = PCI_DEVICE_ID_INTEL_MRST_SD2,
674 .subvendor = PCI_ANY_ID,
675 .subdevice = PCI_ANY_ID,
676 .driver_data = (kernel_ulong_t)&sdhci_intel_mrst_hc1_hc2,
Alan Coxf9ee3ea2010-10-04 15:25:11 +0100677 },
678
679 {
680 .vendor = PCI_VENDOR_ID_INTEL,
Xiaochen Shen29229052010-10-04 15:24:52 +0100681 .device = PCI_DEVICE_ID_INTEL_MFD_SD,
682 .subvendor = PCI_ANY_ID,
683 .subdevice = PCI_ANY_ID,
684 .driver_data = (kernel_ulong_t)&sdhci_intel_mfd_sd,
685 },
686
687 {
688 .vendor = PCI_VENDOR_ID_INTEL,
689 .device = PCI_DEVICE_ID_INTEL_MFD_SDIO1,
690 .subvendor = PCI_ANY_ID,
691 .subdevice = PCI_ANY_ID,
692 .driver_data = (kernel_ulong_t)&sdhci_intel_mfd_emmc_sdio,
693 },
694
695 {
696 .vendor = PCI_VENDOR_ID_INTEL,
697 .device = PCI_DEVICE_ID_INTEL_MFD_SDIO2,
698 .subvendor = PCI_ANY_ID,
699 .subdevice = PCI_ANY_ID,
700 .driver_data = (kernel_ulong_t)&sdhci_intel_mfd_emmc_sdio,
701 },
702
703 {
704 .vendor = PCI_VENDOR_ID_INTEL,
705 .device = PCI_DEVICE_ID_INTEL_MFD_EMMC0,
706 .subvendor = PCI_ANY_ID,
707 .subdevice = PCI_ANY_ID,
708 .driver_data = (kernel_ulong_t)&sdhci_intel_mfd_emmc_sdio,
709 },
710
711 {
712 .vendor = PCI_VENDOR_ID_INTEL,
713 .device = PCI_DEVICE_ID_INTEL_MFD_EMMC1,
714 .subvendor = PCI_ANY_ID,
715 .subdevice = PCI_ANY_ID,
716 .driver_data = (kernel_ulong_t)&sdhci_intel_mfd_emmc_sdio,
717 },
718
Jennifer Li26daa1e2010-11-17 23:01:59 -0500719 {
720 .vendor = PCI_VENDOR_ID_O2,
721 .device = PCI_DEVICE_ID_O2_8120,
722 .subvendor = PCI_ANY_ID,
723 .subdevice = PCI_ANY_ID,
724 .driver_data = (kernel_ulong_t)&sdhci_o2,
725 },
726
727 {
728 .vendor = PCI_VENDOR_ID_O2,
729 .device = PCI_DEVICE_ID_O2_8220,
730 .subvendor = PCI_ANY_ID,
731 .subdevice = PCI_ANY_ID,
732 .driver_data = (kernel_ulong_t)&sdhci_o2,
733 },
734
735 {
736 .vendor = PCI_VENDOR_ID_O2,
737 .device = PCI_DEVICE_ID_O2_8221,
738 .subvendor = PCI_ANY_ID,
739 .subdevice = PCI_ANY_ID,
740 .driver_data = (kernel_ulong_t)&sdhci_o2,
741 },
742
743 {
744 .vendor = PCI_VENDOR_ID_O2,
745 .device = PCI_DEVICE_ID_O2_8320,
746 .subvendor = PCI_ANY_ID,
747 .subdevice = PCI_ANY_ID,
748 .driver_data = (kernel_ulong_t)&sdhci_o2,
749 },
750
751 {
752 .vendor = PCI_VENDOR_ID_O2,
753 .device = PCI_DEVICE_ID_O2_8321,
754 .subvendor = PCI_ANY_ID,
755 .subdevice = PCI_ANY_ID,
756 .driver_data = (kernel_ulong_t)&sdhci_o2,
757 },
758
Pierre Ossman22606402008-03-23 19:33:23 +0100759 { /* Generic SD host controller */
760 PCI_DEVICE_CLASS((PCI_CLASS_SYSTEM_SDHCI << 8), 0xFFFF00)
761 },
762
763 { /* end: all zeroes */ },
764};
765
766MODULE_DEVICE_TABLE(pci, pci_ids);
767
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100768/*****************************************************************************\
769 * *
770 * SDHCI core callbacks *
771 * *
772\*****************************************************************************/
773
774static int sdhci_pci_enable_dma(struct sdhci_host *host)
775{
776 struct sdhci_pci_slot *slot;
777 struct pci_dev *pdev;
778 int ret;
779
780 slot = sdhci_priv(host);
781 pdev = slot->chip->pdev;
782
783 if (((pdev->class & 0xFFFF00) == (PCI_CLASS_SYSTEM_SDHCI << 8)) &&
784 ((pdev->class & 0x0000FF) != PCI_SDHCI_IFDMA) &&
Richard Röjforsa13abc72009-09-22 16:45:30 -0700785 (host->flags & SDHCI_USE_SDMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100786 dev_warn(&pdev->dev, "Will use DMA mode even though HW "
787 "doesn't fully claim to support it.\n");
788 }
789
Yang Hongyang284901a2009-04-06 19:01:15 -0700790 ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100791 if (ret)
792 return ret;
793
794 pci_set_master(pdev);
795
796 return 0;
797}
798
Major Lee68077b02011-06-29 14:23:46 +0300799static int sdhci_pci_8bit_width(struct sdhci_host *host, int width)
800{
801 u8 ctrl;
802
803 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
804
805 switch (width) {
806 case MMC_BUS_WIDTH_8:
807 ctrl |= SDHCI_CTRL_8BITBUS;
808 ctrl &= ~SDHCI_CTRL_4BITBUS;
809 break;
810 case MMC_BUS_WIDTH_4:
811 ctrl |= SDHCI_CTRL_4BITBUS;
812 ctrl &= ~SDHCI_CTRL_8BITBUS;
813 break;
814 default:
815 ctrl &= ~(SDHCI_CTRL_8BITBUS | SDHCI_CTRL_4BITBUS);
816 break;
817 }
818
819 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
820
821 return 0;
822}
823
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100824static struct sdhci_ops sdhci_pci_ops = {
825 .enable_dma = sdhci_pci_enable_dma,
Major Lee68077b02011-06-29 14:23:46 +0300826 .platform_8bit_width = sdhci_pci_8bit_width,
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100827};
828
829/*****************************************************************************\
830 * *
831 * Suspend/resume *
832 * *
833\*****************************************************************************/
834
835#ifdef CONFIG_PM
836
Ameya Palandeb177bc92011-04-05 21:13:13 +0300837static int sdhci_pci_suspend(struct pci_dev *pdev, pm_message_t state)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100838{
839 struct sdhci_pci_chip *chip;
840 struct sdhci_pci_slot *slot;
Daniel Drake5f619702010-11-04 22:20:39 +0000841 mmc_pm_flag_t slot_pm_flags;
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800842 mmc_pm_flag_t pm_flags = 0;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100843 int i, ret;
844
845 chip = pci_get_drvdata(pdev);
846 if (!chip)
847 return 0;
848
Ameya Palandeb177bc92011-04-05 21:13:13 +0300849 for (i = 0; i < chip->num_slots; i++) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100850 slot = chip->slots[i];
851 if (!slot)
852 continue;
853
854 ret = sdhci_suspend_host(slot->host, state);
855
856 if (ret) {
Ameya Palandeb177bc92011-04-05 21:13:13 +0300857 for (i--; i >= 0; i--)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100858 sdhci_resume_host(chip->slots[i]->host);
859 return ret;
860 }
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800861
Daniel Drake5f619702010-11-04 22:20:39 +0000862 slot_pm_flags = slot->host->mmc->pm_flags;
863 if (slot_pm_flags & MMC_PM_WAKE_SDIO_IRQ)
864 sdhci_enable_irq_wakeups(slot->host);
865
866 pm_flags |= slot_pm_flags;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100867 }
868
Pierre Ossman44894282008-04-04 19:36:59 +0200869 if (chip->fixes && chip->fixes->suspend) {
870 ret = chip->fixes->suspend(chip, state);
871 if (ret) {
Ameya Palandeb177bc92011-04-05 21:13:13 +0300872 for (i = chip->num_slots - 1; i >= 0; i--)
Pierre Ossman44894282008-04-04 19:36:59 +0200873 sdhci_resume_host(chip->slots[i]->host);
874 return ret;
875 }
876 }
877
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100878 pci_save_state(pdev);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800879 if (pm_flags & MMC_PM_KEEP_POWER) {
Daniel Drake5f619702010-11-04 22:20:39 +0000880 if (pm_flags & MMC_PM_WAKE_SDIO_IRQ) {
881 pci_pme_active(pdev, true);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800882 pci_enable_wake(pdev, PCI_D3hot, 1);
Daniel Drake5f619702010-11-04 22:20:39 +0000883 }
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800884 pci_set_power_state(pdev, PCI_D3hot);
885 } else {
886 pci_enable_wake(pdev, pci_choose_state(pdev, state), 0);
887 pci_disable_device(pdev);
888 pci_set_power_state(pdev, pci_choose_state(pdev, state));
889 }
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100890
891 return 0;
892}
893
Ameya Palandeb177bc92011-04-05 21:13:13 +0300894static int sdhci_pci_resume(struct pci_dev *pdev)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100895{
896 struct sdhci_pci_chip *chip;
897 struct sdhci_pci_slot *slot;
898 int i, ret;
899
900 chip = pci_get_drvdata(pdev);
901 if (!chip)
902 return 0;
903
904 pci_set_power_state(pdev, PCI_D0);
905 pci_restore_state(pdev);
906 ret = pci_enable_device(pdev);
907 if (ret)
908 return ret;
909
Pierre Ossman45211e22008-03-24 13:09:09 +0100910 if (chip->fixes && chip->fixes->resume) {
911 ret = chip->fixes->resume(chip);
912 if (ret)
913 return ret;
914 }
915
Ameya Palandeb177bc92011-04-05 21:13:13 +0300916 for (i = 0; i < chip->num_slots; i++) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100917 slot = chip->slots[i];
918 if (!slot)
919 continue;
920
921 ret = sdhci_resume_host(slot->host);
922 if (ret)
923 return ret;
924 }
925
926 return 0;
927}
928
929#else /* CONFIG_PM */
930
931#define sdhci_pci_suspend NULL
932#define sdhci_pci_resume NULL
933
934#endif /* CONFIG_PM */
935
936/*****************************************************************************\
937 * *
938 * Device probing/removal *
939 * *
940\*****************************************************************************/
941
942static struct sdhci_pci_slot * __devinit sdhci_pci_probe_slot(
943 struct pci_dev *pdev, struct sdhci_pci_chip *chip, int bar)
944{
945 struct sdhci_pci_slot *slot;
946 struct sdhci_host *host;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100947 int ret;
948
949 if (!(pci_resource_flags(pdev, bar) & IORESOURCE_MEM)) {
950 dev_err(&pdev->dev, "BAR %d is not iomem. Aborting.\n", bar);
951 return ERR_PTR(-ENODEV);
952 }
953
954 if (pci_resource_len(pdev, bar) != 0x100) {
955 dev_err(&pdev->dev, "Invalid iomem size. You may "
956 "experience problems.\n");
957 }
958
959 if ((pdev->class & 0x0000FF) == PCI_SDHCI_IFVENDOR) {
960 dev_err(&pdev->dev, "Vendor specific interface. Aborting.\n");
961 return ERR_PTR(-ENODEV);
962 }
963
964 if ((pdev->class & 0x0000FF) > PCI_SDHCI_IFVENDOR) {
965 dev_err(&pdev->dev, "Unknown interface. Aborting.\n");
966 return ERR_PTR(-ENODEV);
967 }
968
969 host = sdhci_alloc_host(&pdev->dev, sizeof(struct sdhci_pci_slot));
970 if (IS_ERR(host)) {
Dan Carpenterc60a32c2009-04-10 23:31:10 +0200971 dev_err(&pdev->dev, "cannot allocate host\n");
Julia Lawalldc0fd7b2010-05-26 14:42:11 -0700972 return ERR_CAST(host);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100973 }
974
975 slot = sdhci_priv(host);
976
977 slot->chip = chip;
978 slot->host = host;
979 slot->pci_bar = bar;
980
981 host->hw_name = "PCI";
982 host->ops = &sdhci_pci_ops;
983 host->quirks = chip->quirks;
984
985 host->irq = pdev->irq;
986
987 ret = pci_request_region(pdev, bar, mmc_hostname(host->mmc));
988 if (ret) {
989 dev_err(&pdev->dev, "cannot request region\n");
Dan Carpenterc60a32c2009-04-10 23:31:10 +0200990 goto free;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100991 }
992
Arjan van de Ven092f82e2008-09-28 16:15:56 -0700993 host->ioaddr = pci_ioremap_bar(pdev, bar);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100994 if (!host->ioaddr) {
995 dev_err(&pdev->dev, "failed to remap registers\n");
Chris Ball9fdcdbb2011-03-29 00:46:12 -0400996 ret = -ENOMEM;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100997 goto release;
998 }
999
Pierre Ossman44894282008-04-04 19:36:59 +02001000 if (chip->fixes && chip->fixes->probe_slot) {
1001 ret = chip->fixes->probe_slot(slot);
1002 if (ret)
1003 goto unmap;
1004 }
1005
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08001006 host->mmc->pm_caps = MMC_PM_KEEP_POWER | MMC_PM_WAKE_SDIO_IRQ;
1007
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001008 ret = sdhci_add_host(host);
1009 if (ret)
Pierre Ossman44894282008-04-04 19:36:59 +02001010 goto remove;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001011
1012 return slot;
1013
Pierre Ossman44894282008-04-04 19:36:59 +02001014remove:
1015 if (chip->fixes && chip->fixes->remove_slot)
Pierre Ossman1e728592008-04-16 19:13:13 +02001016 chip->fixes->remove_slot(slot, 0);
Pierre Ossman44894282008-04-04 19:36:59 +02001017
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001018unmap:
1019 iounmap(host->ioaddr);
1020
1021release:
1022 pci_release_region(pdev, bar);
Dan Carpenterc60a32c2009-04-10 23:31:10 +02001023
1024free:
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001025 sdhci_free_host(host);
1026
1027 return ERR_PTR(ret);
1028}
1029
1030static void sdhci_pci_remove_slot(struct sdhci_pci_slot *slot)
1031{
Pierre Ossman1e728592008-04-16 19:13:13 +02001032 int dead;
1033 u32 scratch;
1034
1035 dead = 0;
1036 scratch = readl(slot->host->ioaddr + SDHCI_INT_STATUS);
1037 if (scratch == (u32)-1)
1038 dead = 1;
1039
1040 sdhci_remove_host(slot->host, dead);
Pierre Ossman44894282008-04-04 19:36:59 +02001041
1042 if (slot->chip->fixes && slot->chip->fixes->remove_slot)
Pierre Ossman1e728592008-04-16 19:13:13 +02001043 slot->chip->fixes->remove_slot(slot, dead);
Pierre Ossman44894282008-04-04 19:36:59 +02001044
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001045 pci_release_region(slot->chip->pdev, slot->pci_bar);
Pierre Ossman44894282008-04-04 19:36:59 +02001046
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001047 sdhci_free_host(slot->host);
1048}
1049
1050static int __devinit sdhci_pci_probe(struct pci_dev *pdev,
1051 const struct pci_device_id *ent)
1052{
1053 struct sdhci_pci_chip *chip;
1054 struct sdhci_pci_slot *slot;
1055
Sergei Shtylyovcf5e23e2011-03-17 16:46:17 -04001056 u8 slots, first_bar;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001057 int ret, i;
1058
1059 BUG_ON(pdev == NULL);
1060 BUG_ON(ent == NULL);
1061
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001062 dev_info(&pdev->dev, "SDHCI controller found [%04x:%04x] (rev %x)\n",
Sergei Shtylyovcf5e23e2011-03-17 16:46:17 -04001063 (int)pdev->vendor, (int)pdev->device, (int)pdev->revision);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001064
1065 ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &slots);
1066 if (ret)
1067 return ret;
1068
1069 slots = PCI_SLOT_INFO_SLOTS(slots) + 1;
1070 dev_dbg(&pdev->dev, "found %d slot(s)\n", slots);
1071 if (slots == 0)
1072 return -ENODEV;
1073
1074 BUG_ON(slots > MAX_SLOTS);
1075
1076 ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &first_bar);
1077 if (ret)
1078 return ret;
1079
1080 first_bar &= PCI_SLOT_INFO_FIRST_BAR_MASK;
1081
1082 if (first_bar > 5) {
1083 dev_err(&pdev->dev, "Invalid first BAR. Aborting.\n");
1084 return -ENODEV;
1085 }
1086
1087 ret = pci_enable_device(pdev);
1088 if (ret)
1089 return ret;
1090
1091 chip = kzalloc(sizeof(struct sdhci_pci_chip), GFP_KERNEL);
1092 if (!chip) {
1093 ret = -ENOMEM;
1094 goto err;
1095 }
1096
1097 chip->pdev = pdev;
Ameya Palandeb177bc92011-04-05 21:13:13 +03001098 chip->fixes = (const struct sdhci_pci_fixes *)ent->driver_data;
Pierre Ossman22606402008-03-23 19:33:23 +01001099 if (chip->fixes)
1100 chip->quirks = chip->fixes->quirks;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001101 chip->num_slots = slots;
1102
1103 pci_set_drvdata(pdev, chip);
1104
Pierre Ossman22606402008-03-23 19:33:23 +01001105 if (chip->fixes && chip->fixes->probe) {
1106 ret = chip->fixes->probe(chip);
1107 if (ret)
1108 goto free;
1109 }
1110
Alan Cox225d85f2010-10-04 15:24:21 +01001111 slots = chip->num_slots; /* Quirk may have changed this */
1112
Ameya Palandeb177bc92011-04-05 21:13:13 +03001113 for (i = 0; i < slots; i++) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001114 slot = sdhci_pci_probe_slot(pdev, chip, first_bar + i);
1115 if (IS_ERR(slot)) {
Ameya Palandeb177bc92011-04-05 21:13:13 +03001116 for (i--; i >= 0; i--)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001117 sdhci_pci_remove_slot(chip->slots[i]);
1118 ret = PTR_ERR(slot);
1119 goto free;
1120 }
1121
1122 chip->slots[i] = slot;
1123 }
1124
1125 return 0;
1126
1127free:
1128 pci_set_drvdata(pdev, NULL);
1129 kfree(chip);
1130
1131err:
1132 pci_disable_device(pdev);
1133 return ret;
1134}
1135
1136static void __devexit sdhci_pci_remove(struct pci_dev *pdev)
1137{
1138 int i;
1139 struct sdhci_pci_chip *chip;
1140
1141 chip = pci_get_drvdata(pdev);
1142
1143 if (chip) {
Ameya Palandeb177bc92011-04-05 21:13:13 +03001144 for (i = 0; i < chip->num_slots; i++)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001145 sdhci_pci_remove_slot(chip->slots[i]);
1146
1147 pci_set_drvdata(pdev, NULL);
1148 kfree(chip);
1149 }
1150
1151 pci_disable_device(pdev);
1152}
1153
1154static struct pci_driver sdhci_driver = {
Ameya Palandeb177bc92011-04-05 21:13:13 +03001155 .name = "sdhci-pci",
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001156 .id_table = pci_ids,
Ameya Palandeb177bc92011-04-05 21:13:13 +03001157 .probe = sdhci_pci_probe,
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001158 .remove = __devexit_p(sdhci_pci_remove),
1159 .suspend = sdhci_pci_suspend,
1160 .resume = sdhci_pci_resume,
1161};
1162
1163/*****************************************************************************\
1164 * *
1165 * Driver init/exit *
1166 * *
1167\*****************************************************************************/
1168
1169static int __init sdhci_drv_init(void)
1170{
1171 return pci_register_driver(&sdhci_driver);
1172}
1173
1174static void __exit sdhci_drv_exit(void)
1175{
1176 pci_unregister_driver(&sdhci_driver);
1177}
1178
1179module_init(sdhci_drv_init);
1180module_exit(sdhci_drv_exit);
1181
Pierre Ossman32710e82009-04-08 20:14:54 +02001182MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001183MODULE_DESCRIPTION("Secure Digital Host Controller Interface PCI driver");
1184MODULE_LICENSE("GPL");