1 /* linux/drivers/mmc/host/sdhci-pci.c - SDHCI on PCI bus interface
3 * Copyright (C) 2005-2008 Pierre Ossman, All Rights Reserved.
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.
10 * Thanks to the following companies for their support:
12 * - JMicron (hardware and technical support)
15 #include <linux/delay.h>
16 #include <linux/highmem.h>
17 #include <linux/pci.h>
18 #include <linux/dma-mapping.h>
20 #include <linux/mmc/host.h>
22 #include <asm/scatterlist.h>
31 #define PCI_SDHCI_IFPIO 0x00
32 #define PCI_SDHCI_IFDMA 0x01
33 #define PCI_SDHCI_IFVENDOR 0x02
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
41 struct sdhci_pci_chip;
42 struct sdhci_pci_slot;
44 struct sdhci_pci_fixes {
47 int (*probe)(struct sdhci_pci_chip*);
49 int (*probe_slot)(struct sdhci_pci_slot*);
50 void (*remove_slot)(struct sdhci_pci_slot*, int);
52 int (*suspend)(struct sdhci_pci_chip*,
54 int (*resume)(struct sdhci_pci_chip*);
57 struct sdhci_pci_slot {
58 struct sdhci_pci_chip *chip;
59 struct sdhci_host *host;
64 struct sdhci_pci_chip {
68 const struct sdhci_pci_fixes *fixes;
70 int num_slots; /* Slots on controller */
71 struct sdhci_pci_slot *slots[MAX_SLOTS]; /* Pointers to host slots */
75 /*****************************************************************************\
77 * Hardware specific quirk handling *
79 \*****************************************************************************/
81 static int ricoh_probe(struct sdhci_pci_chip *chip)
83 if (chip->pdev->subsystem_vendor == PCI_VENDOR_ID_IBM)
84 chip->quirks |= SDHCI_QUIRK_CLOCK_BEFORE_RESET;
86 if (chip->pdev->subsystem_vendor == PCI_VENDOR_ID_SAMSUNG)
87 chip->quirks |= SDHCI_QUIRK_NO_CARD_NO_RESET;
92 static const struct sdhci_pci_fixes sdhci_ricoh = {
94 .quirks = SDHCI_QUIRK_32BIT_DMA_ADDR,
97 static const struct sdhci_pci_fixes sdhci_ene_712 = {
98 .quirks = SDHCI_QUIRK_SINGLE_POWER_WRITE |
99 SDHCI_QUIRK_BROKEN_DMA,
102 static const struct sdhci_pci_fixes sdhci_ene_714 = {
103 .quirks = SDHCI_QUIRK_SINGLE_POWER_WRITE |
104 SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS |
105 SDHCI_QUIRK_BROKEN_DMA,
108 static const struct sdhci_pci_fixes sdhci_cafe = {
109 .quirks = SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER |
110 SDHCI_QUIRK_BROKEN_TIMEOUT_VAL,
113 static int jmicron_pmos(struct sdhci_pci_chip *chip, int on)
118 ret = pci_read_config_byte(chip->pdev, 0xAE, &scratch);
123 * Turn PMOS on [bit 0], set over current detection to 2.4 V
124 * [bit 1:2] and enable over current debouncing [bit 6].
131 ret = pci_write_config_byte(chip->pdev, 0xAE, scratch);
138 static int jmicron_probe(struct sdhci_pci_chip *chip)
143 * JMicron chips can have two interfaces to the same hardware
144 * in order to work around limitations in Microsoft's driver.
145 * We need to make sure we only bind to one of them.
147 * This code assumes two things:
149 * 1. The PCI code adds subfunctions in order.
151 * 2. The MMC interface has a lower subfunction number
152 * than the SD interface.
154 if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_SD) {
155 struct pci_dev *sd_dev;
158 while ((sd_dev = pci_get_device(PCI_VENDOR_ID_JMICRON,
159 PCI_DEVICE_ID_JMICRON_JMB38X_MMC, sd_dev)) != NULL) {
160 if ((PCI_SLOT(chip->pdev->devfn) ==
161 PCI_SLOT(sd_dev->devfn)) &&
162 (chip->pdev->bus == sd_dev->bus))
168 dev_info(&chip->pdev->dev, "Refusing to bind to "
169 "secondary interface.\n");
175 * JMicron chips need a bit of a nudge to enable the power
178 ret = jmicron_pmos(chip, 1);
180 dev_err(&chip->pdev->dev, "Failure enabling card power\n");
187 static void jmicron_enable_mmc(struct sdhci_host *host, int on)
191 scratch = readb(host->ioaddr + 0xC0);
198 writeb(scratch, host->ioaddr + 0xC0);
201 static int jmicron_probe_slot(struct sdhci_pci_slot *slot)
204 * The secondary interface requires a bit set to get the
207 if (slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC)
208 jmicron_enable_mmc(slot->host, 1);
213 static void jmicron_remove_slot(struct sdhci_pci_slot *slot, int dead)
218 if (slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC)
219 jmicron_enable_mmc(slot->host, 0);
222 static int jmicron_suspend(struct sdhci_pci_chip *chip, pm_message_t state)
226 if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC) {
227 for (i = 0;i < chip->num_slots;i++)
228 jmicron_enable_mmc(chip->slots[i]->host, 0);
234 static int jmicron_resume(struct sdhci_pci_chip *chip)
238 if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC) {
239 for (i = 0;i < chip->num_slots;i++)
240 jmicron_enable_mmc(chip->slots[i]->host, 1);
243 ret = jmicron_pmos(chip, 1);
245 dev_err(&chip->pdev->dev, "Failure enabling card power\n");
252 static const struct sdhci_pci_fixes sdhci_jmicron = {
253 .quirks = SDHCI_QUIRK_32BIT_DMA_ADDR |
254 SDHCI_QUIRK_32BIT_DMA_SIZE |
255 SDHCI_QUIRK_RESET_AFTER_REQUEST,
257 .probe = jmicron_probe,
259 .probe_slot = jmicron_probe_slot,
260 .remove_slot = jmicron_remove_slot,
262 .suspend = jmicron_suspend,
263 .resume = jmicron_resume,
266 static const struct pci_device_id pci_ids[] __devinitdata = {
268 .vendor = PCI_VENDOR_ID_RICOH,
269 .device = PCI_DEVICE_ID_RICOH_R5C822,
270 .subvendor = PCI_ANY_ID,
271 .subdevice = PCI_ANY_ID,
272 .driver_data = (kernel_ulong_t)&sdhci_ricoh,
276 .vendor = PCI_VENDOR_ID_ENE,
277 .device = PCI_DEVICE_ID_ENE_CB712_SD,
278 .subvendor = PCI_ANY_ID,
279 .subdevice = PCI_ANY_ID,
280 .driver_data = (kernel_ulong_t)&sdhci_ene_712,
284 .vendor = PCI_VENDOR_ID_ENE,
285 .device = PCI_DEVICE_ID_ENE_CB712_SD_2,
286 .subvendor = PCI_ANY_ID,
287 .subdevice = PCI_ANY_ID,
288 .driver_data = (kernel_ulong_t)&sdhci_ene_712,
292 .vendor = PCI_VENDOR_ID_ENE,
293 .device = PCI_DEVICE_ID_ENE_CB714_SD,
294 .subvendor = PCI_ANY_ID,
295 .subdevice = PCI_ANY_ID,
296 .driver_data = (kernel_ulong_t)&sdhci_ene_714,
300 .vendor = PCI_VENDOR_ID_ENE,
301 .device = PCI_DEVICE_ID_ENE_CB714_SD_2,
302 .subvendor = PCI_ANY_ID,
303 .subdevice = PCI_ANY_ID,
304 .driver_data = (kernel_ulong_t)&sdhci_ene_714,
308 .vendor = PCI_VENDOR_ID_MARVELL,
309 .device = PCI_DEVICE_ID_MARVELL_CAFE_SD,
310 .subvendor = PCI_ANY_ID,
311 .subdevice = PCI_ANY_ID,
312 .driver_data = (kernel_ulong_t)&sdhci_cafe,
316 .vendor = PCI_VENDOR_ID_JMICRON,
317 .device = PCI_DEVICE_ID_JMICRON_JMB38X_SD,
318 .subvendor = PCI_ANY_ID,
319 .subdevice = PCI_ANY_ID,
320 .driver_data = (kernel_ulong_t)&sdhci_jmicron,
324 .vendor = PCI_VENDOR_ID_JMICRON,
325 .device = PCI_DEVICE_ID_JMICRON_JMB38X_MMC,
326 .subvendor = PCI_ANY_ID,
327 .subdevice = PCI_ANY_ID,
328 .driver_data = (kernel_ulong_t)&sdhci_jmicron,
331 { /* Generic SD host controller */
332 PCI_DEVICE_CLASS((PCI_CLASS_SYSTEM_SDHCI << 8), 0xFFFF00)
335 { /* end: all zeroes */ },
338 MODULE_DEVICE_TABLE(pci, pci_ids);
340 /*****************************************************************************\
342 * SDHCI core callbacks *
344 \*****************************************************************************/
346 static int sdhci_pci_enable_dma(struct sdhci_host *host)
348 struct sdhci_pci_slot *slot;
349 struct pci_dev *pdev;
352 slot = sdhci_priv(host);
353 pdev = slot->chip->pdev;
355 if (((pdev->class & 0xFFFF00) == (PCI_CLASS_SYSTEM_SDHCI << 8)) &&
356 ((pdev->class & 0x0000FF) != PCI_SDHCI_IFDMA) &&
357 (host->flags & SDHCI_USE_DMA)) {
358 dev_warn(&pdev->dev, "Will use DMA mode even though HW "
359 "doesn't fully claim to support it.\n");
362 ret = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
366 pci_set_master(pdev);
371 static struct sdhci_ops sdhci_pci_ops = {
372 .enable_dma = sdhci_pci_enable_dma,
375 /*****************************************************************************\
379 \*****************************************************************************/
383 static int sdhci_pci_suspend (struct pci_dev *pdev, pm_message_t state)
385 struct sdhci_pci_chip *chip;
386 struct sdhci_pci_slot *slot;
389 chip = pci_get_drvdata(pdev);
393 for (i = 0;i < chip->num_slots;i++) {
394 slot = chip->slots[i];
398 ret = sdhci_suspend_host(slot->host, state);
402 sdhci_resume_host(chip->slots[i]->host);
407 if (chip->fixes && chip->fixes->suspend) {
408 ret = chip->fixes->suspend(chip, state);
410 for (i = chip->num_slots - 1;i >= 0;i--)
411 sdhci_resume_host(chip->slots[i]->host);
416 pci_save_state(pdev);
417 pci_enable_wake(pdev, pci_choose_state(pdev, state), 0);
418 pci_disable_device(pdev);
419 pci_set_power_state(pdev, pci_choose_state(pdev, state));
424 static int sdhci_pci_resume (struct pci_dev *pdev)
426 struct sdhci_pci_chip *chip;
427 struct sdhci_pci_slot *slot;
430 chip = pci_get_drvdata(pdev);
434 pci_set_power_state(pdev, PCI_D0);
435 pci_restore_state(pdev);
436 ret = pci_enable_device(pdev);
440 if (chip->fixes && chip->fixes->resume) {
441 ret = chip->fixes->resume(chip);
446 for (i = 0;i < chip->num_slots;i++) {
447 slot = chip->slots[i];
451 ret = sdhci_resume_host(slot->host);
459 #else /* CONFIG_PM */
461 #define sdhci_pci_suspend NULL
462 #define sdhci_pci_resume NULL
464 #endif /* CONFIG_PM */
466 /*****************************************************************************\
468 * Device probing/removal *
470 \*****************************************************************************/
472 static struct sdhci_pci_slot * __devinit sdhci_pci_probe_slot(
473 struct pci_dev *pdev, struct sdhci_pci_chip *chip, int bar)
475 struct sdhci_pci_slot *slot;
476 struct sdhci_host *host;
478 resource_size_t addr;
482 if (!(pci_resource_flags(pdev, bar) & IORESOURCE_MEM)) {
483 dev_err(&pdev->dev, "BAR %d is not iomem. Aborting.\n", bar);
484 return ERR_PTR(-ENODEV);
487 if (pci_resource_len(pdev, bar) != 0x100) {
488 dev_err(&pdev->dev, "Invalid iomem size. You may "
489 "experience problems.\n");
492 if ((pdev->class & 0x0000FF) == PCI_SDHCI_IFVENDOR) {
493 dev_err(&pdev->dev, "Vendor specific interface. Aborting.\n");
494 return ERR_PTR(-ENODEV);
497 if ((pdev->class & 0x0000FF) > PCI_SDHCI_IFVENDOR) {
498 dev_err(&pdev->dev, "Unknown interface. Aborting.\n");
499 return ERR_PTR(-ENODEV);
502 host = sdhci_alloc_host(&pdev->dev, sizeof(struct sdhci_pci_slot));
508 slot = sdhci_priv(host);
514 host->hw_name = "PCI";
515 host->ops = &sdhci_pci_ops;
516 host->quirks = chip->quirks;
518 host->irq = pdev->irq;
520 ret = pci_request_region(pdev, bar, mmc_hostname(host->mmc));
522 dev_err(&pdev->dev, "cannot request region\n");
526 addr = pci_resource_start(pdev, bar);
527 host->ioaddr = ioremap_nocache(addr, pci_resource_len(pdev, bar));
529 dev_err(&pdev->dev, "failed to remap registers\n");
533 if (chip->fixes && chip->fixes->probe_slot) {
534 ret = chip->fixes->probe_slot(slot);
539 ret = sdhci_add_host(host);
546 if (chip->fixes && chip->fixes->remove_slot)
547 chip->fixes->remove_slot(slot, 0);
550 iounmap(host->ioaddr);
553 pci_release_region(pdev, bar);
554 sdhci_free_host(host);
559 static void sdhci_pci_remove_slot(struct sdhci_pci_slot *slot)
565 scratch = readl(slot->host->ioaddr + SDHCI_INT_STATUS);
566 if (scratch == (u32)-1)
569 sdhci_remove_host(slot->host, dead);
571 if (slot->chip->fixes && slot->chip->fixes->remove_slot)
572 slot->chip->fixes->remove_slot(slot, dead);
574 pci_release_region(slot->chip->pdev, slot->pci_bar);
576 sdhci_free_host(slot->host);
579 static int __devinit sdhci_pci_probe(struct pci_dev *pdev,
580 const struct pci_device_id *ent)
582 struct sdhci_pci_chip *chip;
583 struct sdhci_pci_slot *slot;
585 u8 slots, rev, first_bar;
588 BUG_ON(pdev == NULL);
591 pci_read_config_byte(pdev, PCI_CLASS_REVISION, &rev);
593 dev_info(&pdev->dev, "SDHCI controller found [%04x:%04x] (rev %x)\n",
594 (int)pdev->vendor, (int)pdev->device, (int)rev);
596 ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &slots);
600 slots = PCI_SLOT_INFO_SLOTS(slots) + 1;
601 dev_dbg(&pdev->dev, "found %d slot(s)\n", slots);
605 BUG_ON(slots > MAX_SLOTS);
607 ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &first_bar);
611 first_bar &= PCI_SLOT_INFO_FIRST_BAR_MASK;
614 dev_err(&pdev->dev, "Invalid first BAR. Aborting.\n");
618 ret = pci_enable_device(pdev);
622 chip = kzalloc(sizeof(struct sdhci_pci_chip), GFP_KERNEL);
629 chip->fixes = (const struct sdhci_pci_fixes*)ent->driver_data;
631 chip->quirks = chip->fixes->quirks;
632 chip->num_slots = slots;
634 pci_set_drvdata(pdev, chip);
636 if (chip->fixes && chip->fixes->probe) {
637 ret = chip->fixes->probe(chip);
642 for (i = 0;i < slots;i++) {
643 slot = sdhci_pci_probe_slot(pdev, chip, first_bar + i);
646 sdhci_pci_remove_slot(chip->slots[i]);
651 chip->slots[i] = slot;
657 pci_set_drvdata(pdev, NULL);
661 pci_disable_device(pdev);
665 static void __devexit sdhci_pci_remove(struct pci_dev *pdev)
668 struct sdhci_pci_chip *chip;
670 chip = pci_get_drvdata(pdev);
673 for (i = 0;i < chip->num_slots; i++)
674 sdhci_pci_remove_slot(chip->slots[i]);
676 pci_set_drvdata(pdev, NULL);
680 pci_disable_device(pdev);
683 static struct pci_driver sdhci_driver = {
686 .probe = sdhci_pci_probe,
687 .remove = __devexit_p(sdhci_pci_remove),
688 .suspend = sdhci_pci_suspend,
689 .resume = sdhci_pci_resume,
692 /*****************************************************************************\
696 \*****************************************************************************/
698 static int __init sdhci_drv_init(void)
700 return pci_register_driver(&sdhci_driver);
703 static void __exit sdhci_drv_exit(void)
705 pci_unregister_driver(&sdhci_driver);
708 module_init(sdhci_drv_init);
709 module_exit(sdhci_drv_exit);
711 MODULE_AUTHOR("Pierre Ossman <drzeus@drzeus.cx>");
712 MODULE_DESCRIPTION("Secure Digital Host Controller Interface PCI driver");
713 MODULE_LICENSE("GPL");