blob: d196e77a93dc99e9d4ef05438fb0ac85290929f1 [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
22#include <linux/mmc/host.h>
23
24#include <asm/scatterlist.h>
25#include <asm/io.h>
26
27#include "sdhci.h"
28
29/*
30 * PCI registers
31 */
32
33#define PCI_SDHCI_IFPIO 0x00
34#define PCI_SDHCI_IFDMA 0x01
35#define PCI_SDHCI_IFVENDOR 0x02
36
37#define PCI_SLOT_INFO 0x40 /* 8 bits */
38#define PCI_SLOT_INFO_SLOTS(x) ((x >> 4) & 7)
39#define PCI_SLOT_INFO_FIRST_BAR_MASK 0x07
40
41#define MAX_SLOTS 8
42
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010043struct sdhci_pci_chip;
Pierre Ossman44894282008-04-04 19:36:59 +020044struct sdhci_pci_slot;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010045
Pierre Ossman22606402008-03-23 19:33:23 +010046struct sdhci_pci_fixes {
47 unsigned int quirks;
48
49 int (*probe)(struct sdhci_pci_chip*);
Pierre Ossman45211e22008-03-24 13:09:09 +010050
Pierre Ossman44894282008-04-04 19:36:59 +020051 int (*probe_slot)(struct sdhci_pci_slot*);
Pierre Ossman1e728592008-04-16 19:13:13 +020052 void (*remove_slot)(struct sdhci_pci_slot*, int);
Pierre Ossman44894282008-04-04 19:36:59 +020053
54 int (*suspend)(struct sdhci_pci_chip*,
55 pm_message_t);
Pierre Ossman45211e22008-03-24 13:09:09 +010056 int (*resume)(struct sdhci_pci_chip*);
Pierre Ossman22606402008-03-23 19:33:23 +010057};
58
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010059struct sdhci_pci_slot {
60 struct sdhci_pci_chip *chip;
61 struct sdhci_host *host;
62
63 int pci_bar;
64};
65
66struct sdhci_pci_chip {
67 struct pci_dev *pdev;
Pierre Ossman22606402008-03-23 19:33:23 +010068
69 unsigned int quirks;
70 const struct sdhci_pci_fixes *fixes;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010071
72 int num_slots; /* Slots on controller */
73 struct sdhci_pci_slot *slots[MAX_SLOTS]; /* Pointers to host slots */
74};
75
Pierre Ossman22606402008-03-23 19:33:23 +010076
77/*****************************************************************************\
78 * *
79 * Hardware specific quirk handling *
80 * *
81\*****************************************************************************/
82
83static int ricoh_probe(struct sdhci_pci_chip *chip)
84{
Chris Ballc99436f2009-09-22 16:45:22 -070085 if (chip->pdev->subsystem_vendor == PCI_VENDOR_ID_SAMSUNG ||
86 chip->pdev->subsystem_vendor == PCI_VENDOR_ID_SONY)
Pierre Ossman22606402008-03-23 19:33:23 +010087 chip->quirks |= SDHCI_QUIRK_NO_CARD_NO_RESET;
Maxim Levitskyccc92c22010-08-10 18:01:42 -070088 return 0;
89}
Pierre Ossman22606402008-03-23 19:33:23 +010090
Maxim Levitskyccc92c22010-08-10 18:01:42 -070091static int ricoh_mmc_probe_slot(struct sdhci_pci_slot *slot)
92{
93 slot->host->caps =
94 ((0x21 << SDHCI_TIMEOUT_CLK_SHIFT)
95 & SDHCI_TIMEOUT_CLK_MASK) |
96
97 ((0x21 << SDHCI_CLOCK_BASE_SHIFT)
98 & SDHCI_CLOCK_BASE_MASK) |
99
100 SDHCI_TIMEOUT_CLK_UNIT |
101 SDHCI_CAN_VDD_330 |
102 SDHCI_CAN_DO_SDMA;
103 return 0;
104}
105
106static int ricoh_mmc_resume(struct sdhci_pci_chip *chip)
107{
108 /* Apply a delay to allow controller to settle */
109 /* Otherwise it becomes confused if card state changed
110 during suspend */
111 msleep(500);
Pierre Ossman22606402008-03-23 19:33:23 +0100112 return 0;
113}
114
115static const struct sdhci_pci_fixes sdhci_ricoh = {
116 .probe = ricoh_probe,
Vasily Khoruzhick84938292010-03-05 13:43:46 -0800117 .quirks = SDHCI_QUIRK_32BIT_DMA_ADDR |
118 SDHCI_QUIRK_FORCE_DMA |
119 SDHCI_QUIRK_CLOCK_BEFORE_RESET,
Pierre Ossman22606402008-03-23 19:33:23 +0100120};
121
Maxim Levitskyccc92c22010-08-10 18:01:42 -0700122static const struct sdhci_pci_fixes sdhci_ricoh_mmc = {
123 .probe_slot = ricoh_mmc_probe_slot,
124 .resume = ricoh_mmc_resume,
125 .quirks = SDHCI_QUIRK_32BIT_DMA_ADDR |
126 SDHCI_QUIRK_CLOCK_BEFORE_RESET |
127 SDHCI_QUIRK_NO_CARD_NO_RESET |
128 SDHCI_QUIRK_MISSING_CAPS
129};
130
Pierre Ossman22606402008-03-23 19:33:23 +0100131static const struct sdhci_pci_fixes sdhci_ene_712 = {
132 .quirks = SDHCI_QUIRK_SINGLE_POWER_WRITE |
133 SDHCI_QUIRK_BROKEN_DMA,
134};
135
136static const struct sdhci_pci_fixes sdhci_ene_714 = {
137 .quirks = SDHCI_QUIRK_SINGLE_POWER_WRITE |
138 SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS |
139 SDHCI_QUIRK_BROKEN_DMA,
140};
141
142static const struct sdhci_pci_fixes sdhci_cafe = {
143 .quirks = SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER |
Andres Salomona0874892009-03-02 21:48:20 +0100144 SDHCI_QUIRK_NO_BUSY_IRQ |
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200145 SDHCI_QUIRK_BROKEN_TIMEOUT_VAL,
Pierre Ossman22606402008-03-23 19:33:23 +0100146};
147
Alan Coxf9ee3ea2010-10-04 15:25:11 +0100148/*
149 * ADMA operation is disabled for Moorestown platform due to
150 * hardware bugs.
151 */
152static int mrst_hc1_probe(struct sdhci_pci_chip *chip)
153{
154 /*
155 * slots number is fixed here for MRST as SDIO3 is never used and has
156 * hardware bugs.
157 */
158 chip->num_slots = 1;
159 return 0;
160}
161
162static const struct sdhci_pci_fixes sdhci_intel_mrst_hc0 = {
163 .quirks = SDHCI_QUIRK_BROKEN_ADMA | SDHCI_QUIRK_NO_HISPD_BIT,
164};
165
166static const struct sdhci_pci_fixes sdhci_intel_mrst_hc1 = {
167 .quirks = SDHCI_QUIRK_BROKEN_ADMA | SDHCI_QUIRK_NO_HISPD_BIT,
168 .probe = mrst_hc1_probe,
169};
170
Xiaochen Shen29229052010-10-04 15:24:52 +0100171static const struct sdhci_pci_fixes sdhci_intel_mfd_sd = {
172 .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
173};
174
175static const struct sdhci_pci_fixes sdhci_intel_mfd_emmc_sdio = {
176 .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
177};
178
Pierre Ossman45211e22008-03-24 13:09:09 +0100179static int jmicron_pmos(struct sdhci_pci_chip *chip, int on)
180{
181 u8 scratch;
182 int ret;
183
184 ret = pci_read_config_byte(chip->pdev, 0xAE, &scratch);
185 if (ret)
186 return ret;
187
188 /*
189 * Turn PMOS on [bit 0], set over current detection to 2.4 V
190 * [bit 1:2] and enable over current debouncing [bit 6].
191 */
192 if (on)
193 scratch |= 0x47;
194 else
195 scratch &= ~0x47;
196
197 ret = pci_write_config_byte(chip->pdev, 0xAE, scratch);
198 if (ret)
199 return ret;
200
201 return 0;
202}
203
204static int jmicron_probe(struct sdhci_pci_chip *chip)
205{
206 int ret;
207
Pierre Ossman93fc48c2008-06-28 18:21:41 +0200208 if (chip->pdev->revision == 0) {
209 chip->quirks |= SDHCI_QUIRK_32BIT_DMA_ADDR |
210 SDHCI_QUIRK_32BIT_DMA_SIZE |
Pierre Ossman2134a922008-06-28 18:28:51 +0200211 SDHCI_QUIRK_32BIT_ADMA_SIZE |
Pierre Ossman4a3cba32008-07-29 00:11:16 +0200212 SDHCI_QUIRK_RESET_AFTER_REQUEST |
Pierre Ossman86a6a872009-02-02 21:13:49 +0100213 SDHCI_QUIRK_BROKEN_SMALL_PIO;
Pierre Ossman93fc48c2008-06-28 18:21:41 +0200214 }
215
Pierre Ossman45211e22008-03-24 13:09:09 +0100216 /*
Pierre Ossman44894282008-04-04 19:36:59 +0200217 * JMicron chips can have two interfaces to the same hardware
218 * in order to work around limitations in Microsoft's driver.
219 * We need to make sure we only bind to one of them.
220 *
221 * This code assumes two things:
222 *
223 * 1. The PCI code adds subfunctions in order.
224 *
225 * 2. The MMC interface has a lower subfunction number
226 * than the SD interface.
227 */
228 if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_SD) {
229 struct pci_dev *sd_dev;
230
231 sd_dev = NULL;
232 while ((sd_dev = pci_get_device(PCI_VENDOR_ID_JMICRON,
233 PCI_DEVICE_ID_JMICRON_JMB38X_MMC, sd_dev)) != NULL) {
234 if ((PCI_SLOT(chip->pdev->devfn) ==
235 PCI_SLOT(sd_dev->devfn)) &&
236 (chip->pdev->bus == sd_dev->bus))
237 break;
238 }
239
240 if (sd_dev) {
241 pci_dev_put(sd_dev);
242 dev_info(&chip->pdev->dev, "Refusing to bind to "
243 "secondary interface.\n");
244 return -ENODEV;
245 }
246 }
247
248 /*
Pierre Ossman45211e22008-03-24 13:09:09 +0100249 * JMicron chips need a bit of a nudge to enable the power
250 * output pins.
251 */
252 ret = jmicron_pmos(chip, 1);
253 if (ret) {
254 dev_err(&chip->pdev->dev, "Failure enabling card power\n");
255 return ret;
256 }
257
258 return 0;
259}
260
Pierre Ossman44894282008-04-04 19:36:59 +0200261static void jmicron_enable_mmc(struct sdhci_host *host, int on)
262{
263 u8 scratch;
264
265 scratch = readb(host->ioaddr + 0xC0);
266
267 if (on)
268 scratch |= 0x01;
269 else
270 scratch &= ~0x01;
271
272 writeb(scratch, host->ioaddr + 0xC0);
273}
274
275static int jmicron_probe_slot(struct sdhci_pci_slot *slot)
276{
Pierre Ossman2134a922008-06-28 18:28:51 +0200277 if (slot->chip->pdev->revision == 0) {
278 u16 version;
279
280 version = readl(slot->host->ioaddr + SDHCI_HOST_VERSION);
281 version = (version & SDHCI_VENDOR_VER_MASK) >>
282 SDHCI_VENDOR_VER_SHIFT;
283
284 /*
285 * Older versions of the chip have lots of nasty glitches
286 * in the ADMA engine. It's best just to avoid it
287 * completely.
288 */
289 if (version < 0xAC)
290 slot->host->quirks |= SDHCI_QUIRK_BROKEN_ADMA;
291 }
292
Pierre Ossman44894282008-04-04 19:36:59 +0200293 /*
294 * The secondary interface requires a bit set to get the
295 * interrupts.
296 */
297 if (slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC)
298 jmicron_enable_mmc(slot->host, 1);
299
300 return 0;
301}
302
Pierre Ossman1e728592008-04-16 19:13:13 +0200303static void jmicron_remove_slot(struct sdhci_pci_slot *slot, int dead)
Pierre Ossman44894282008-04-04 19:36:59 +0200304{
Pierre Ossman1e728592008-04-16 19:13:13 +0200305 if (dead)
306 return;
307
Pierre Ossman44894282008-04-04 19:36:59 +0200308 if (slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC)
309 jmicron_enable_mmc(slot->host, 0);
310}
311
312static int jmicron_suspend(struct sdhci_pci_chip *chip, pm_message_t state)
313{
314 int i;
315
316 if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC) {
317 for (i = 0;i < chip->num_slots;i++)
318 jmicron_enable_mmc(chip->slots[i]->host, 0);
319 }
320
321 return 0;
322}
323
Pierre Ossman45211e22008-03-24 13:09:09 +0100324static int jmicron_resume(struct sdhci_pci_chip *chip)
325{
Pierre Ossman44894282008-04-04 19:36:59 +0200326 int ret, i;
327
328 if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC) {
329 for (i = 0;i < chip->num_slots;i++)
330 jmicron_enable_mmc(chip->slots[i]->host, 1);
331 }
Pierre Ossman45211e22008-03-24 13:09:09 +0100332
333 ret = jmicron_pmos(chip, 1);
334 if (ret) {
335 dev_err(&chip->pdev->dev, "Failure enabling card power\n");
336 return ret;
337 }
338
339 return 0;
340}
341
Pierre Ossman22606402008-03-23 19:33:23 +0100342static const struct sdhci_pci_fixes sdhci_jmicron = {
Pierre Ossman45211e22008-03-24 13:09:09 +0100343 .probe = jmicron_probe,
344
Pierre Ossman44894282008-04-04 19:36:59 +0200345 .probe_slot = jmicron_probe_slot,
346 .remove_slot = jmicron_remove_slot,
347
348 .suspend = jmicron_suspend,
Pierre Ossman45211e22008-03-24 13:09:09 +0100349 .resume = jmicron_resume,
Pierre Ossman22606402008-03-23 19:33:23 +0100350};
351
Nicolas Pitrea7a61862009-12-14 18:01:26 -0800352/* SysKonnect CardBus2SDIO extra registers */
353#define SYSKT_CTRL 0x200
354#define SYSKT_RDFIFO_STAT 0x204
355#define SYSKT_WRFIFO_STAT 0x208
356#define SYSKT_POWER_DATA 0x20c
357#define SYSKT_POWER_330 0xef
358#define SYSKT_POWER_300 0xf8
359#define SYSKT_POWER_184 0xcc
360#define SYSKT_POWER_CMD 0x20d
361#define SYSKT_POWER_START (1 << 7)
362#define SYSKT_POWER_STATUS 0x20e
363#define SYSKT_POWER_STATUS_OK (1 << 0)
364#define SYSKT_BOARD_REV 0x210
365#define SYSKT_CHIP_REV 0x211
366#define SYSKT_CONF_DATA 0x212
367#define SYSKT_CONF_DATA_1V8 (1 << 2)
368#define SYSKT_CONF_DATA_2V5 (1 << 1)
369#define SYSKT_CONF_DATA_3V3 (1 << 0)
370
371static int syskt_probe(struct sdhci_pci_chip *chip)
372{
373 if ((chip->pdev->class & 0x0000FF) == PCI_SDHCI_IFVENDOR) {
374 chip->pdev->class &= ~0x0000FF;
375 chip->pdev->class |= PCI_SDHCI_IFDMA;
376 }
377 return 0;
378}
379
380static int syskt_probe_slot(struct sdhci_pci_slot *slot)
381{
382 int tm, ps;
383
384 u8 board_rev = readb(slot->host->ioaddr + SYSKT_BOARD_REV);
385 u8 chip_rev = readb(slot->host->ioaddr + SYSKT_CHIP_REV);
386 dev_info(&slot->chip->pdev->dev, "SysKonnect CardBus2SDIO, "
387 "board rev %d.%d, chip rev %d.%d\n",
388 board_rev >> 4, board_rev & 0xf,
389 chip_rev >> 4, chip_rev & 0xf);
390 if (chip_rev >= 0x20)
391 slot->host->quirks |= SDHCI_QUIRK_FORCE_DMA;
392
393 writeb(SYSKT_POWER_330, slot->host->ioaddr + SYSKT_POWER_DATA);
394 writeb(SYSKT_POWER_START, slot->host->ioaddr + SYSKT_POWER_CMD);
395 udelay(50);
396 tm = 10; /* Wait max 1 ms */
397 do {
398 ps = readw(slot->host->ioaddr + SYSKT_POWER_STATUS);
399 if (ps & SYSKT_POWER_STATUS_OK)
400 break;
401 udelay(100);
402 } while (--tm);
403 if (!tm) {
404 dev_err(&slot->chip->pdev->dev,
405 "power regulator never stabilized");
406 writeb(0, slot->host->ioaddr + SYSKT_POWER_CMD);
407 return -ENODEV;
408 }
409
410 return 0;
411}
412
413static const struct sdhci_pci_fixes sdhci_syskt = {
414 .quirks = SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER,
415 .probe = syskt_probe,
416 .probe_slot = syskt_probe_slot,
417};
418
Harald Welte557b0692009-06-18 16:53:38 +0200419static int via_probe(struct sdhci_pci_chip *chip)
420{
421 if (chip->pdev->revision == 0x10)
422 chip->quirks |= SDHCI_QUIRK_DELAY_AFTER_POWER;
423
424 return 0;
425}
426
427static const struct sdhci_pci_fixes sdhci_via = {
428 .probe = via_probe,
429};
430
Pierre Ossman22606402008-03-23 19:33:23 +0100431static const struct pci_device_id pci_ids[] __devinitdata = {
432 {
433 .vendor = PCI_VENDOR_ID_RICOH,
434 .device = PCI_DEVICE_ID_RICOH_R5C822,
435 .subvendor = PCI_ANY_ID,
436 .subdevice = PCI_ANY_ID,
437 .driver_data = (kernel_ulong_t)&sdhci_ricoh,
438 },
439
440 {
Maxim Levitskyccc92c22010-08-10 18:01:42 -0700441 .vendor = PCI_VENDOR_ID_RICOH,
442 .device = 0x843,
443 .subvendor = PCI_ANY_ID,
444 .subdevice = PCI_ANY_ID,
445 .driver_data = (kernel_ulong_t)&sdhci_ricoh_mmc,
446 },
447
448 {
Pablo Castillo568133e2010-08-10 18:02:01 -0700449 .vendor = PCI_VENDOR_ID_RICOH,
450 .device = 0xe822,
451 .subvendor = PCI_ANY_ID,
452 .subdevice = PCI_ANY_ID,
453 .driver_data = (kernel_ulong_t)&sdhci_ricoh_mmc,
454 },
455
456 {
Pierre Ossman22606402008-03-23 19:33:23 +0100457 .vendor = PCI_VENDOR_ID_ENE,
458 .device = PCI_DEVICE_ID_ENE_CB712_SD,
459 .subvendor = PCI_ANY_ID,
460 .subdevice = PCI_ANY_ID,
461 .driver_data = (kernel_ulong_t)&sdhci_ene_712,
462 },
463
464 {
465 .vendor = PCI_VENDOR_ID_ENE,
466 .device = PCI_DEVICE_ID_ENE_CB712_SD_2,
467 .subvendor = PCI_ANY_ID,
468 .subdevice = PCI_ANY_ID,
469 .driver_data = (kernel_ulong_t)&sdhci_ene_712,
470 },
471
472 {
473 .vendor = PCI_VENDOR_ID_ENE,
474 .device = PCI_DEVICE_ID_ENE_CB714_SD,
475 .subvendor = PCI_ANY_ID,
476 .subdevice = PCI_ANY_ID,
477 .driver_data = (kernel_ulong_t)&sdhci_ene_714,
478 },
479
480 {
481 .vendor = PCI_VENDOR_ID_ENE,
482 .device = PCI_DEVICE_ID_ENE_CB714_SD_2,
483 .subvendor = PCI_ANY_ID,
484 .subdevice = PCI_ANY_ID,
485 .driver_data = (kernel_ulong_t)&sdhci_ene_714,
486 },
487
488 {
489 .vendor = PCI_VENDOR_ID_MARVELL,
David Woodhouse8c5eb882008-09-03 09:45:57 +0100490 .device = PCI_DEVICE_ID_MARVELL_88ALP01_SD,
Pierre Ossman22606402008-03-23 19:33:23 +0100491 .subvendor = PCI_ANY_ID,
492 .subdevice = PCI_ANY_ID,
493 .driver_data = (kernel_ulong_t)&sdhci_cafe,
494 },
495
496 {
497 .vendor = PCI_VENDOR_ID_JMICRON,
498 .device = PCI_DEVICE_ID_JMICRON_JMB38X_SD,
499 .subvendor = PCI_ANY_ID,
500 .subdevice = PCI_ANY_ID,
501 .driver_data = (kernel_ulong_t)&sdhci_jmicron,
502 },
503
Pierre Ossman44894282008-04-04 19:36:59 +0200504 {
505 .vendor = PCI_VENDOR_ID_JMICRON,
506 .device = PCI_DEVICE_ID_JMICRON_JMB38X_MMC,
507 .subvendor = PCI_ANY_ID,
508 .subdevice = PCI_ANY_ID,
509 .driver_data = (kernel_ulong_t)&sdhci_jmicron,
510 },
511
Harald Welte557b0692009-06-18 16:53:38 +0200512 {
Nicolas Pitrea7a61862009-12-14 18:01:26 -0800513 .vendor = PCI_VENDOR_ID_SYSKONNECT,
514 .device = 0x8000,
515 .subvendor = PCI_ANY_ID,
516 .subdevice = PCI_ANY_ID,
517 .driver_data = (kernel_ulong_t)&sdhci_syskt,
518 },
519
520 {
Harald Welte557b0692009-06-18 16:53:38 +0200521 .vendor = PCI_VENDOR_ID_VIA,
522 .device = 0x95d0,
523 .subvendor = PCI_ANY_ID,
524 .subdevice = PCI_ANY_ID,
525 .driver_data = (kernel_ulong_t)&sdhci_via,
526 },
527
Xiaochen Shen29229052010-10-04 15:24:52 +0100528 {
529 .vendor = PCI_VENDOR_ID_INTEL,
Alan Coxf9ee3ea2010-10-04 15:25:11 +0100530 .device = PCI_DEVICE_ID_INTEL_MRST_SD0,
531 .subvendor = PCI_ANY_ID,
532 .subdevice = PCI_ANY_ID,
533 .driver_data = (kernel_ulong_t)&sdhci_intel_mrst_hc0,
534 },
535
536 {
537 .vendor = PCI_VENDOR_ID_INTEL,
538 .device = PCI_DEVICE_ID_INTEL_MRST_SD1,
539 .subvendor = PCI_ANY_ID,
540 .subdevice = PCI_ANY_ID,
541 .driver_data = (kernel_ulong_t)&sdhci_intel_mrst_hc1,
542 },
543
544 {
545 .vendor = PCI_VENDOR_ID_INTEL,
Xiaochen Shen29229052010-10-04 15:24:52 +0100546 .device = PCI_DEVICE_ID_INTEL_MFD_SD,
547 .subvendor = PCI_ANY_ID,
548 .subdevice = PCI_ANY_ID,
549 .driver_data = (kernel_ulong_t)&sdhci_intel_mfd_sd,
550 },
551
552 {
553 .vendor = PCI_VENDOR_ID_INTEL,
554 .device = PCI_DEVICE_ID_INTEL_MFD_SDIO1,
555 .subvendor = PCI_ANY_ID,
556 .subdevice = PCI_ANY_ID,
557 .driver_data = (kernel_ulong_t)&sdhci_intel_mfd_emmc_sdio,
558 },
559
560 {
561 .vendor = PCI_VENDOR_ID_INTEL,
562 .device = PCI_DEVICE_ID_INTEL_MFD_SDIO2,
563 .subvendor = PCI_ANY_ID,
564 .subdevice = PCI_ANY_ID,
565 .driver_data = (kernel_ulong_t)&sdhci_intel_mfd_emmc_sdio,
566 },
567
568 {
569 .vendor = PCI_VENDOR_ID_INTEL,
570 .device = PCI_DEVICE_ID_INTEL_MFD_EMMC0,
571 .subvendor = PCI_ANY_ID,
572 .subdevice = PCI_ANY_ID,
573 .driver_data = (kernel_ulong_t)&sdhci_intel_mfd_emmc_sdio,
574 },
575
576 {
577 .vendor = PCI_VENDOR_ID_INTEL,
578 .device = PCI_DEVICE_ID_INTEL_MFD_EMMC1,
579 .subvendor = PCI_ANY_ID,
580 .subdevice = PCI_ANY_ID,
581 .driver_data = (kernel_ulong_t)&sdhci_intel_mfd_emmc_sdio,
582 },
583
Pierre Ossman22606402008-03-23 19:33:23 +0100584 { /* Generic SD host controller */
585 PCI_DEVICE_CLASS((PCI_CLASS_SYSTEM_SDHCI << 8), 0xFFFF00)
586 },
587
588 { /* end: all zeroes */ },
589};
590
591MODULE_DEVICE_TABLE(pci, pci_ids);
592
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100593/*****************************************************************************\
594 * *
595 * SDHCI core callbacks *
596 * *
597\*****************************************************************************/
598
599static int sdhci_pci_enable_dma(struct sdhci_host *host)
600{
601 struct sdhci_pci_slot *slot;
602 struct pci_dev *pdev;
603 int ret;
604
605 slot = sdhci_priv(host);
606 pdev = slot->chip->pdev;
607
608 if (((pdev->class & 0xFFFF00) == (PCI_CLASS_SYSTEM_SDHCI << 8)) &&
609 ((pdev->class & 0x0000FF) != PCI_SDHCI_IFDMA) &&
Richard Röjforsa13abc72009-09-22 16:45:30 -0700610 (host->flags & SDHCI_USE_SDMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100611 dev_warn(&pdev->dev, "Will use DMA mode even though HW "
612 "doesn't fully claim to support it.\n");
613 }
614
Yang Hongyang284901a2009-04-06 19:01:15 -0700615 ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100616 if (ret)
617 return ret;
618
619 pci_set_master(pdev);
620
621 return 0;
622}
623
624static struct sdhci_ops sdhci_pci_ops = {
625 .enable_dma = sdhci_pci_enable_dma,
626};
627
628/*****************************************************************************\
629 * *
630 * Suspend/resume *
631 * *
632\*****************************************************************************/
633
634#ifdef CONFIG_PM
635
636static int sdhci_pci_suspend (struct pci_dev *pdev, pm_message_t state)
637{
638 struct sdhci_pci_chip *chip;
639 struct sdhci_pci_slot *slot;
Daniel Drake5f619702010-11-04 22:20:39 +0000640 mmc_pm_flag_t slot_pm_flags;
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800641 mmc_pm_flag_t pm_flags = 0;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100642 int i, ret;
643
644 chip = pci_get_drvdata(pdev);
645 if (!chip)
646 return 0;
647
648 for (i = 0;i < chip->num_slots;i++) {
649 slot = chip->slots[i];
650 if (!slot)
651 continue;
652
653 ret = sdhci_suspend_host(slot->host, state);
654
655 if (ret) {
656 for (i--;i >= 0;i--)
657 sdhci_resume_host(chip->slots[i]->host);
658 return ret;
659 }
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800660
Daniel Drake5f619702010-11-04 22:20:39 +0000661 slot_pm_flags = slot->host->mmc->pm_flags;
662 if (slot_pm_flags & MMC_PM_WAKE_SDIO_IRQ)
663 sdhci_enable_irq_wakeups(slot->host);
664
665 pm_flags |= slot_pm_flags;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100666 }
667
Pierre Ossman44894282008-04-04 19:36:59 +0200668 if (chip->fixes && chip->fixes->suspend) {
669 ret = chip->fixes->suspend(chip, state);
670 if (ret) {
671 for (i = chip->num_slots - 1;i >= 0;i--)
672 sdhci_resume_host(chip->slots[i]->host);
673 return ret;
674 }
675 }
676
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100677 pci_save_state(pdev);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800678 if (pm_flags & MMC_PM_KEEP_POWER) {
Daniel Drake5f619702010-11-04 22:20:39 +0000679 if (pm_flags & MMC_PM_WAKE_SDIO_IRQ) {
680 pci_pme_active(pdev, true);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800681 pci_enable_wake(pdev, PCI_D3hot, 1);
Daniel Drake5f619702010-11-04 22:20:39 +0000682 }
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800683 pci_set_power_state(pdev, PCI_D3hot);
684 } else {
685 pci_enable_wake(pdev, pci_choose_state(pdev, state), 0);
686 pci_disable_device(pdev);
687 pci_set_power_state(pdev, pci_choose_state(pdev, state));
688 }
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100689
690 return 0;
691}
692
693static int sdhci_pci_resume (struct pci_dev *pdev)
694{
695 struct sdhci_pci_chip *chip;
696 struct sdhci_pci_slot *slot;
697 int i, ret;
698
699 chip = pci_get_drvdata(pdev);
700 if (!chip)
701 return 0;
702
703 pci_set_power_state(pdev, PCI_D0);
704 pci_restore_state(pdev);
705 ret = pci_enable_device(pdev);
706 if (ret)
707 return ret;
708
Pierre Ossman45211e22008-03-24 13:09:09 +0100709 if (chip->fixes && chip->fixes->resume) {
710 ret = chip->fixes->resume(chip);
711 if (ret)
712 return ret;
713 }
714
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100715 for (i = 0;i < chip->num_slots;i++) {
716 slot = chip->slots[i];
717 if (!slot)
718 continue;
719
720 ret = sdhci_resume_host(slot->host);
721 if (ret)
722 return ret;
723 }
724
725 return 0;
726}
727
728#else /* CONFIG_PM */
729
730#define sdhci_pci_suspend NULL
731#define sdhci_pci_resume NULL
732
733#endif /* CONFIG_PM */
734
735/*****************************************************************************\
736 * *
737 * Device probing/removal *
738 * *
739\*****************************************************************************/
740
741static struct sdhci_pci_slot * __devinit sdhci_pci_probe_slot(
742 struct pci_dev *pdev, struct sdhci_pci_chip *chip, int bar)
743{
744 struct sdhci_pci_slot *slot;
745 struct sdhci_host *host;
746
747 resource_size_t addr;
748
749 int ret;
750
751 if (!(pci_resource_flags(pdev, bar) & IORESOURCE_MEM)) {
752 dev_err(&pdev->dev, "BAR %d is not iomem. Aborting.\n", bar);
753 return ERR_PTR(-ENODEV);
754 }
755
756 if (pci_resource_len(pdev, bar) != 0x100) {
757 dev_err(&pdev->dev, "Invalid iomem size. You may "
758 "experience problems.\n");
759 }
760
761 if ((pdev->class & 0x0000FF) == PCI_SDHCI_IFVENDOR) {
762 dev_err(&pdev->dev, "Vendor specific interface. Aborting.\n");
763 return ERR_PTR(-ENODEV);
764 }
765
766 if ((pdev->class & 0x0000FF) > PCI_SDHCI_IFVENDOR) {
767 dev_err(&pdev->dev, "Unknown interface. Aborting.\n");
768 return ERR_PTR(-ENODEV);
769 }
770
771 host = sdhci_alloc_host(&pdev->dev, sizeof(struct sdhci_pci_slot));
772 if (IS_ERR(host)) {
Dan Carpenterc60a32c2009-04-10 23:31:10 +0200773 dev_err(&pdev->dev, "cannot allocate host\n");
Julia Lawalldc0fd7b2010-05-26 14:42:11 -0700774 return ERR_CAST(host);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100775 }
776
777 slot = sdhci_priv(host);
778
779 slot->chip = chip;
780 slot->host = host;
781 slot->pci_bar = bar;
782
783 host->hw_name = "PCI";
784 host->ops = &sdhci_pci_ops;
785 host->quirks = chip->quirks;
786
787 host->irq = pdev->irq;
788
789 ret = pci_request_region(pdev, bar, mmc_hostname(host->mmc));
790 if (ret) {
791 dev_err(&pdev->dev, "cannot request region\n");
Dan Carpenterc60a32c2009-04-10 23:31:10 +0200792 goto free;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100793 }
794
795 addr = pci_resource_start(pdev, bar);
Arjan van de Ven092f82e2008-09-28 16:15:56 -0700796 host->ioaddr = pci_ioremap_bar(pdev, bar);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100797 if (!host->ioaddr) {
798 dev_err(&pdev->dev, "failed to remap registers\n");
799 goto release;
800 }
801
Pierre Ossman44894282008-04-04 19:36:59 +0200802 if (chip->fixes && chip->fixes->probe_slot) {
803 ret = chip->fixes->probe_slot(slot);
804 if (ret)
805 goto unmap;
806 }
807
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800808 host->mmc->pm_caps = MMC_PM_KEEP_POWER | MMC_PM_WAKE_SDIO_IRQ;
809
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100810 ret = sdhci_add_host(host);
811 if (ret)
Pierre Ossman44894282008-04-04 19:36:59 +0200812 goto remove;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100813
814 return slot;
815
Pierre Ossman44894282008-04-04 19:36:59 +0200816remove:
817 if (chip->fixes && chip->fixes->remove_slot)
Pierre Ossman1e728592008-04-16 19:13:13 +0200818 chip->fixes->remove_slot(slot, 0);
Pierre Ossman44894282008-04-04 19:36:59 +0200819
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100820unmap:
821 iounmap(host->ioaddr);
822
823release:
824 pci_release_region(pdev, bar);
Dan Carpenterc60a32c2009-04-10 23:31:10 +0200825
826free:
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100827 sdhci_free_host(host);
828
829 return ERR_PTR(ret);
830}
831
832static void sdhci_pci_remove_slot(struct sdhci_pci_slot *slot)
833{
Pierre Ossman1e728592008-04-16 19:13:13 +0200834 int dead;
835 u32 scratch;
836
837 dead = 0;
838 scratch = readl(slot->host->ioaddr + SDHCI_INT_STATUS);
839 if (scratch == (u32)-1)
840 dead = 1;
841
842 sdhci_remove_host(slot->host, dead);
Pierre Ossman44894282008-04-04 19:36:59 +0200843
844 if (slot->chip->fixes && slot->chip->fixes->remove_slot)
Pierre Ossman1e728592008-04-16 19:13:13 +0200845 slot->chip->fixes->remove_slot(slot, dead);
Pierre Ossman44894282008-04-04 19:36:59 +0200846
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100847 pci_release_region(slot->chip->pdev, slot->pci_bar);
Pierre Ossman44894282008-04-04 19:36:59 +0200848
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100849 sdhci_free_host(slot->host);
850}
851
852static int __devinit sdhci_pci_probe(struct pci_dev *pdev,
853 const struct pci_device_id *ent)
854{
855 struct sdhci_pci_chip *chip;
856 struct sdhci_pci_slot *slot;
857
858 u8 slots, rev, first_bar;
859 int ret, i;
860
861 BUG_ON(pdev == NULL);
862 BUG_ON(ent == NULL);
863
864 pci_read_config_byte(pdev, PCI_CLASS_REVISION, &rev);
865
866 dev_info(&pdev->dev, "SDHCI controller found [%04x:%04x] (rev %x)\n",
867 (int)pdev->vendor, (int)pdev->device, (int)rev);
868
869 ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &slots);
870 if (ret)
871 return ret;
872
873 slots = PCI_SLOT_INFO_SLOTS(slots) + 1;
874 dev_dbg(&pdev->dev, "found %d slot(s)\n", slots);
875 if (slots == 0)
876 return -ENODEV;
877
878 BUG_ON(slots > MAX_SLOTS);
879
880 ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &first_bar);
881 if (ret)
882 return ret;
883
884 first_bar &= PCI_SLOT_INFO_FIRST_BAR_MASK;
885
886 if (first_bar > 5) {
887 dev_err(&pdev->dev, "Invalid first BAR. Aborting.\n");
888 return -ENODEV;
889 }
890
891 ret = pci_enable_device(pdev);
892 if (ret)
893 return ret;
894
895 chip = kzalloc(sizeof(struct sdhci_pci_chip), GFP_KERNEL);
896 if (!chip) {
897 ret = -ENOMEM;
898 goto err;
899 }
900
901 chip->pdev = pdev;
Pierre Ossman22606402008-03-23 19:33:23 +0100902 chip->fixes = (const struct sdhci_pci_fixes*)ent->driver_data;
903 if (chip->fixes)
904 chip->quirks = chip->fixes->quirks;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100905 chip->num_slots = slots;
906
907 pci_set_drvdata(pdev, chip);
908
Pierre Ossman22606402008-03-23 19:33:23 +0100909 if (chip->fixes && chip->fixes->probe) {
910 ret = chip->fixes->probe(chip);
911 if (ret)
912 goto free;
913 }
914
Alan Cox225d85f2010-10-04 15:24:21 +0100915 slots = chip->num_slots; /* Quirk may have changed this */
916
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100917 for (i = 0;i < slots;i++) {
918 slot = sdhci_pci_probe_slot(pdev, chip, first_bar + i);
919 if (IS_ERR(slot)) {
920 for (i--;i >= 0;i--)
921 sdhci_pci_remove_slot(chip->slots[i]);
922 ret = PTR_ERR(slot);
923 goto free;
924 }
925
926 chip->slots[i] = slot;
927 }
928
929 return 0;
930
931free:
932 pci_set_drvdata(pdev, NULL);
933 kfree(chip);
934
935err:
936 pci_disable_device(pdev);
937 return ret;
938}
939
940static void __devexit sdhci_pci_remove(struct pci_dev *pdev)
941{
942 int i;
943 struct sdhci_pci_chip *chip;
944
945 chip = pci_get_drvdata(pdev);
946
947 if (chip) {
948 for (i = 0;i < chip->num_slots; i++)
949 sdhci_pci_remove_slot(chip->slots[i]);
950
951 pci_set_drvdata(pdev, NULL);
952 kfree(chip);
953 }
954
955 pci_disable_device(pdev);
956}
957
958static struct pci_driver sdhci_driver = {
959 .name = "sdhci-pci",
960 .id_table = pci_ids,
961 .probe = sdhci_pci_probe,
962 .remove = __devexit_p(sdhci_pci_remove),
963 .suspend = sdhci_pci_suspend,
964 .resume = sdhci_pci_resume,
965};
966
967/*****************************************************************************\
968 * *
969 * Driver init/exit *
970 * *
971\*****************************************************************************/
972
973static int __init sdhci_drv_init(void)
974{
975 return pci_register_driver(&sdhci_driver);
976}
977
978static void __exit sdhci_drv_exit(void)
979{
980 pci_unregister_driver(&sdhci_driver);
981}
982
983module_init(sdhci_drv_init);
984module_exit(sdhci_drv_exit);
985
Pierre Ossman32710e82009-04-08 20:14:54 +0200986MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100987MODULE_DESCRIPTION("Secure Digital Host Controller Interface PCI driver");
988MODULE_LICENSE("GPL");