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 = {
96 static const struct sdhci_pci_fixes sdhci_ene_712 = {
97 .quirks = SDHCI_QUIRK_SINGLE_POWER_WRITE |
98 SDHCI_QUIRK_BROKEN_DMA,
101 static const struct sdhci_pci_fixes sdhci_ene_714 = {
102 .quirks = SDHCI_QUIRK_SINGLE_POWER_WRITE |
103 SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS |
104 SDHCI_QUIRK_BROKEN_DMA,
107 static const struct sdhci_pci_fixes sdhci_cafe = {
108 .quirks = SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER |
109 SDHCI_QUIRK_BROKEN_TIMEOUT_VAL,
112 static int jmicron_pmos(struct sdhci_pci_chip *chip, int on)
117 ret = pci_read_config_byte(chip->pdev, 0xAE, &scratch);
122 * Turn PMOS on [bit 0], set over current detection to 2.4 V
123 * [bit 1:2] and enable over current debouncing [bit 6].
130 ret = pci_write_config_byte(chip->pdev, 0xAE, scratch);
137 static int jmicron_probe(struct sdhci_pci_chip *chip)
142 * JMicron chips can have two interfaces to the same hardware
143 * in order to work around limitations in Microsoft's driver.
144 * We need to make sure we only bind to one of them.
146 * This code assumes two things:
148 * 1. The PCI code adds subfunctions in order.
150 * 2. The MMC interface has a lower subfunction number
151 * than the SD interface.
153 if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_SD) {
154 struct pci_dev *sd_dev;
157 while ((sd_dev = pci_get_device(PCI_VENDOR_ID_JMICRON,
158 PCI_DEVICE_ID_JMICRON_JMB38X_MMC, sd_dev)) != NULL) {
159 if ((PCI_SLOT(chip->pdev->devfn) ==
160 PCI_SLOT(sd_dev->devfn)) &&
161 (chip->pdev->bus == sd_dev->bus))
167 dev_info(&chip->pdev->dev, "Refusing to bind to "
168 "secondary interface.\n");
174 * JMicron chips need a bit of a nudge to enable the power
177 ret = jmicron_pmos(chip, 1);
179 dev_err(&chip->pdev->dev, "Failure enabling card power\n");
186 static void jmicron_enable_mmc(struct sdhci_host *host, int on)
190 scratch = readb(host->ioaddr + 0xC0);
197 writeb(scratch, host->ioaddr + 0xC0);
200 static int jmicron_probe_slot(struct sdhci_pci_slot *slot)
203 * The secondary interface requires a bit set to get the
206 if (slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC)
207 jmicron_enable_mmc(slot->host, 1);
212 static void jmicron_remove_slot(struct sdhci_pci_slot *slot, int dead)
217 if (slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC)
218 jmicron_enable_mmc(slot->host, 0);
221 static int jmicron_suspend(struct sdhci_pci_chip *chip, pm_message_t state)
225 if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC) {
226 for (i = 0;i < chip->num_slots;i++)
227 jmicron_enable_mmc(chip->slots[i]->host, 0);
233 static int jmicron_resume(struct sdhci_pci_chip *chip)
237 if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC) {
238 for (i = 0;i < chip->num_slots;i++)
239 jmicron_enable_mmc(chip->slots[i]->host, 1);
242 ret = jmicron_pmos(chip, 1);
244 dev_err(&chip->pdev->dev, "Failure enabling card power\n");
251 static const struct sdhci_pci_fixes sdhci_jmicron = {
252 .quirks = SDHCI_QUIRK_32BIT_DMA_ADDR |
253 SDHCI_QUIRK_32BIT_DMA_SIZE |
254 SDHCI_QUIRK_RESET_AFTER_REQUEST,
256 .probe = jmicron_probe,
258 .probe_slot = jmicron_probe_slot,
259 .remove_slot = jmicron_remove_slot,
261 .suspend = jmicron_suspend,
262 .resume = jmicron_resume,
265 static const struct pci_device_id pci_ids[] __devinitdata = {
267 .vendor = PCI_VENDOR_ID_RICOH,
268 .device = PCI_DEVICE_ID_RICOH_R5C822,
269 .subvendor = PCI_ANY_ID,
270 .subdevice = PCI_ANY_ID,
271 .driver_data = (kernel_ulong_t)&sdhci_ricoh,
275 .vendor = PCI_VENDOR_ID_ENE,
276 .device = PCI_DEVICE_ID_ENE_CB712_SD,
277 .subvendor = PCI_ANY_ID,
278 .subdevice = PCI_ANY_ID,
279 .driver_data = (kernel_ulong_t)&sdhci_ene_712,
283 .vendor = PCI_VENDOR_ID_ENE,
284 .device = PCI_DEVICE_ID_ENE_CB712_SD_2,
285 .subvendor = PCI_ANY_ID,
286 .subdevice = PCI_ANY_ID,
287 .driver_data = (kernel_ulong_t)&sdhci_ene_712,
291 .vendor = PCI_VENDOR_ID_ENE,
292 .device = PCI_DEVICE_ID_ENE_CB714_SD,
293 .subvendor = PCI_ANY_ID,
294 .subdevice = PCI_ANY_ID,
295 .driver_data = (kernel_ulong_t)&sdhci_ene_714,
299 .vendor = PCI_VENDOR_ID_ENE,
300 .device = PCI_DEVICE_ID_ENE_CB714_SD_2,
301 .subvendor = PCI_ANY_ID,
302 .subdevice = PCI_ANY_ID,
303 .driver_data = (kernel_ulong_t)&sdhci_ene_714,
307 .vendor = PCI_VENDOR_ID_MARVELL,
308 .device = PCI_DEVICE_ID_MARVELL_CAFE_SD,
309 .subvendor = PCI_ANY_ID,
310 .subdevice = PCI_ANY_ID,
311 .driver_data = (kernel_ulong_t)&sdhci_cafe,
315 .vendor = PCI_VENDOR_ID_JMICRON,
316 .device = PCI_DEVICE_ID_JMICRON_JMB38X_SD,
317 .subvendor = PCI_ANY_ID,
318 .subdevice = PCI_ANY_ID,
319 .driver_data = (kernel_ulong_t)&sdhci_jmicron,
323 .vendor = PCI_VENDOR_ID_JMICRON,
324 .device = PCI_DEVICE_ID_JMICRON_JMB38X_MMC,
325 .subvendor = PCI_ANY_ID,
326 .subdevice = PCI_ANY_ID,
327 .driver_data = (kernel_ulong_t)&sdhci_jmicron,
330 { /* Generic SD host controller */
331 PCI_DEVICE_CLASS((PCI_CLASS_SYSTEM_SDHCI << 8), 0xFFFF00)
334 { /* end: all zeroes */ },
337 MODULE_DEVICE_TABLE(pci, pci_ids);
339 /*****************************************************************************\
341 * SDHCI core callbacks *
343 \*****************************************************************************/
345 static int sdhci_pci_enable_dma(struct sdhci_host *host)
347 struct sdhci_pci_slot *slot;
348 struct pci_dev *pdev;
351 slot = sdhci_priv(host);
352 pdev = slot->chip->pdev;
354 if (((pdev->class & 0xFFFF00) == (PCI_CLASS_SYSTEM_SDHCI << 8)) &&
355 ((pdev->class & 0x0000FF) != PCI_SDHCI_IFDMA) &&
356 (host->flags & SDHCI_USE_DMA)) {
357 dev_warn(&pdev->dev, "Will use DMA mode even though HW "
358 "doesn't fully claim to support it.\n");
361 ret = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
365 pci_set_master(pdev);
370 static struct sdhci_ops sdhci_pci_ops = {
371 .enable_dma = sdhci_pci_enable_dma,
374 /*****************************************************************************\
378 \*****************************************************************************/
382 static int sdhci_pci_suspend (struct pci_dev *pdev, pm_message_t state)
384 struct sdhci_pci_chip *chip;
385 struct sdhci_pci_slot *slot;
388 chip = pci_get_drvdata(pdev);
392 for (i = 0;i < chip->num_slots;i++) {
393 slot = chip->slots[i];
397 ret = sdhci_suspend_host(slot->host, state);
401 sdhci_resume_host(chip->slots[i]->host);
406 if (chip->fixes && chip->fixes->suspend) {
407 ret = chip->fixes->suspend(chip, state);
409 for (i = chip->num_slots - 1;i >= 0;i--)
410 sdhci_resume_host(chip->slots[i]->host);
415 pci_save_state(pdev);
416 pci_enable_wake(pdev, pci_choose_state(pdev, state), 0);
417 pci_disable_device(pdev);
418 pci_set_power_state(pdev, pci_choose_state(pdev, state));
423 static int sdhci_pci_resume (struct pci_dev *pdev)
425 struct sdhci_pci_chip *chip;
426 struct sdhci_pci_slot *slot;
429 chip = pci_get_drvdata(pdev);
433 pci_set_power_state(pdev, PCI_D0);
434 pci_restore_state(pdev);
435 ret = pci_enable_device(pdev);
439 if (chip->fixes && chip->fixes->resume) {
440 ret = chip->fixes->resume(chip);
445 for (i = 0;i < chip->num_slots;i++) {
446 slot = chip->slots[i];
450 ret = sdhci_resume_host(slot->host);
458 #else /* CONFIG_PM */
460 #define sdhci_pci_suspend NULL
461 #define sdhci_pci_resume NULL
463 #endif /* CONFIG_PM */
465 /*****************************************************************************\
467 * Device probing/removal *
469 \*****************************************************************************/
471 static struct sdhci_pci_slot * __devinit sdhci_pci_probe_slot(
472 struct pci_dev *pdev, struct sdhci_pci_chip *chip, int bar)
474 struct sdhci_pci_slot *slot;
475 struct sdhci_host *host;
477 resource_size_t addr;
481 if (!(pci_resource_flags(pdev, bar) & IORESOURCE_MEM)) {
482 dev_err(&pdev->dev, "BAR %d is not iomem. Aborting.\n", bar);
483 return ERR_PTR(-ENODEV);
486 if (pci_resource_len(pdev, bar) != 0x100) {
487 dev_err(&pdev->dev, "Invalid iomem size. You may "
488 "experience problems.\n");
491 if ((pdev->class & 0x0000FF) == PCI_SDHCI_IFVENDOR) {
492 dev_err(&pdev->dev, "Vendor specific interface. Aborting.\n");
493 return ERR_PTR(-ENODEV);
496 if ((pdev->class & 0x0000FF) > PCI_SDHCI_IFVENDOR) {
497 dev_err(&pdev->dev, "Unknown interface. Aborting.\n");
498 return ERR_PTR(-ENODEV);
501 host = sdhci_alloc_host(&pdev->dev, sizeof(struct sdhci_pci_slot));
507 slot = sdhci_priv(host);
513 host->hw_name = "PCI";
514 host->ops = &sdhci_pci_ops;
515 host->quirks = chip->quirks;
517 host->irq = pdev->irq;
519 ret = pci_request_region(pdev, bar, mmc_hostname(host->mmc));
521 dev_err(&pdev->dev, "cannot request region\n");
525 addr = pci_resource_start(pdev, bar);
526 host->ioaddr = ioremap_nocache(addr, pci_resource_len(pdev, bar));
528 dev_err(&pdev->dev, "failed to remap registers\n");
532 if (chip->fixes && chip->fixes->probe_slot) {
533 ret = chip->fixes->probe_slot(slot);
538 ret = sdhci_add_host(host);
545 if (chip->fixes && chip->fixes->remove_slot)
546 chip->fixes->remove_slot(slot, 0);
549 iounmap(host->ioaddr);
552 pci_release_region(pdev, bar);
553 sdhci_free_host(host);
558 static void sdhci_pci_remove_slot(struct sdhci_pci_slot *slot)
564 scratch = readl(slot->host->ioaddr + SDHCI_INT_STATUS);
565 if (scratch == (u32)-1)
568 sdhci_remove_host(slot->host, dead);
570 if (slot->chip->fixes && slot->chip->fixes->remove_slot)
571 slot->chip->fixes->remove_slot(slot, dead);
573 pci_release_region(slot->chip->pdev, slot->pci_bar);
575 sdhci_free_host(slot->host);
578 static int __devinit sdhci_pci_probe(struct pci_dev *pdev,
579 const struct pci_device_id *ent)
581 struct sdhci_pci_chip *chip;
582 struct sdhci_pci_slot *slot;
584 u8 slots, rev, first_bar;
587 BUG_ON(pdev == NULL);
590 pci_read_config_byte(pdev, PCI_CLASS_REVISION, &rev);
592 dev_info(&pdev->dev, "SDHCI controller found [%04x:%04x] (rev %x)\n",
593 (int)pdev->vendor, (int)pdev->device, (int)rev);
595 ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &slots);
599 slots = PCI_SLOT_INFO_SLOTS(slots) + 1;
600 dev_dbg(&pdev->dev, "found %d slot(s)\n", slots);
604 BUG_ON(slots > MAX_SLOTS);
606 ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &first_bar);
610 first_bar &= PCI_SLOT_INFO_FIRST_BAR_MASK;
613 dev_err(&pdev->dev, "Invalid first BAR. Aborting.\n");
617 ret = pci_enable_device(pdev);
621 chip = kzalloc(sizeof(struct sdhci_pci_chip), GFP_KERNEL);
628 chip->fixes = (const struct sdhci_pci_fixes*)ent->driver_data;
630 chip->quirks = chip->fixes->quirks;
631 chip->num_slots = slots;
633 pci_set_drvdata(pdev, chip);
635 if (chip->fixes && chip->fixes->probe) {
636 ret = chip->fixes->probe(chip);
641 for (i = 0;i < slots;i++) {
642 slot = sdhci_pci_probe_slot(pdev, chip, first_bar + i);
645 sdhci_pci_remove_slot(chip->slots[i]);
650 chip->slots[i] = slot;
656 pci_set_drvdata(pdev, NULL);
660 pci_disable_device(pdev);
664 static void __devexit sdhci_pci_remove(struct pci_dev *pdev)
667 struct sdhci_pci_chip *chip;
669 chip = pci_get_drvdata(pdev);
672 for (i = 0;i < chip->num_slots; i++)
673 sdhci_pci_remove_slot(chip->slots[i]);
675 pci_set_drvdata(pdev, NULL);
679 pci_disable_device(pdev);
682 static struct pci_driver sdhci_driver = {
685 .probe = sdhci_pci_probe,
686 .remove = __devexit_p(sdhci_pci_remove),
687 .suspend = sdhci_pci_suspend,
688 .resume = sdhci_pci_resume,
691 /*****************************************************************************\
695 \*****************************************************************************/
697 static int __init sdhci_drv_init(void)
699 return pci_register_driver(&sdhci_driver);
702 static void __exit sdhci_drv_exit(void)
704 pci_unregister_driver(&sdhci_driver);
707 module_init(sdhci_drv_init);
708 module_exit(sdhci_drv_exit);
710 MODULE_AUTHOR("Pierre Ossman <drzeus@drzeus.cx>");
711 MODULE_DESCRIPTION("Secure Digital Host Controller Interface PCI driver");
712 MODULE_LICENSE("GPL");