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)
142 if (chip->pdev->revision == 0) {
143 chip->quirks |= SDHCI_QUIRK_32BIT_DMA_ADDR |
144 SDHCI_QUIRK_32BIT_DMA_SIZE |
145 SDHCI_QUIRK_32BIT_ADMA_SIZE |
146 SDHCI_QUIRK_RESET_AFTER_REQUEST |
147 SDHCI_QUIRK_BROKEN_SMALL_PIO;
151 * JMicron chips can have two interfaces to the same hardware
152 * in order to work around limitations in Microsoft's driver.
153 * We need to make sure we only bind to one of them.
155 * This code assumes two things:
157 * 1. The PCI code adds subfunctions in order.
159 * 2. The MMC interface has a lower subfunction number
160 * than the SD interface.
162 if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_SD) {
163 struct pci_dev *sd_dev;
166 while ((sd_dev = pci_get_device(PCI_VENDOR_ID_JMICRON,
167 PCI_DEVICE_ID_JMICRON_JMB38X_MMC, sd_dev)) != NULL) {
168 if ((PCI_SLOT(chip->pdev->devfn) ==
169 PCI_SLOT(sd_dev->devfn)) &&
170 (chip->pdev->bus == sd_dev->bus))
176 dev_info(&chip->pdev->dev, "Refusing to bind to "
177 "secondary interface.\n");
183 * JMicron chips need a bit of a nudge to enable the power
186 ret = jmicron_pmos(chip, 1);
188 dev_err(&chip->pdev->dev, "Failure enabling card power\n");
195 static void jmicron_enable_mmc(struct sdhci_host *host, int on)
199 scratch = readb(host->ioaddr + 0xC0);
206 writeb(scratch, host->ioaddr + 0xC0);
209 static int jmicron_probe_slot(struct sdhci_pci_slot *slot)
211 if (slot->chip->pdev->revision == 0) {
214 version = readl(slot->host->ioaddr + SDHCI_HOST_VERSION);
215 version = (version & SDHCI_VENDOR_VER_MASK) >>
216 SDHCI_VENDOR_VER_SHIFT;
219 * Older versions of the chip have lots of nasty glitches
220 * in the ADMA engine. It's best just to avoid it
224 slot->host->quirks |= SDHCI_QUIRK_BROKEN_ADMA;
228 * The secondary interface requires a bit set to get the
231 if (slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC)
232 jmicron_enable_mmc(slot->host, 1);
237 static void jmicron_remove_slot(struct sdhci_pci_slot *slot, int dead)
242 if (slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC)
243 jmicron_enable_mmc(slot->host, 0);
246 static int jmicron_suspend(struct sdhci_pci_chip *chip, pm_message_t state)
250 if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC) {
251 for (i = 0;i < chip->num_slots;i++)
252 jmicron_enable_mmc(chip->slots[i]->host, 0);
258 static int jmicron_resume(struct sdhci_pci_chip *chip)
262 if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC) {
263 for (i = 0;i < chip->num_slots;i++)
264 jmicron_enable_mmc(chip->slots[i]->host, 1);
267 ret = jmicron_pmos(chip, 1);
269 dev_err(&chip->pdev->dev, "Failure enabling card power\n");
276 static const struct sdhci_pci_fixes sdhci_jmicron = {
277 .probe = jmicron_probe,
279 .probe_slot = jmicron_probe_slot,
280 .remove_slot = jmicron_remove_slot,
282 .suspend = jmicron_suspend,
283 .resume = jmicron_resume,
286 static const struct pci_device_id pci_ids[] __devinitdata = {
288 .vendor = PCI_VENDOR_ID_RICOH,
289 .device = PCI_DEVICE_ID_RICOH_R5C822,
290 .subvendor = PCI_ANY_ID,
291 .subdevice = PCI_ANY_ID,
292 .driver_data = (kernel_ulong_t)&sdhci_ricoh,
296 .vendor = PCI_VENDOR_ID_ENE,
297 .device = PCI_DEVICE_ID_ENE_CB712_SD,
298 .subvendor = PCI_ANY_ID,
299 .subdevice = PCI_ANY_ID,
300 .driver_data = (kernel_ulong_t)&sdhci_ene_712,
304 .vendor = PCI_VENDOR_ID_ENE,
305 .device = PCI_DEVICE_ID_ENE_CB712_SD_2,
306 .subvendor = PCI_ANY_ID,
307 .subdevice = PCI_ANY_ID,
308 .driver_data = (kernel_ulong_t)&sdhci_ene_712,
312 .vendor = PCI_VENDOR_ID_ENE,
313 .device = PCI_DEVICE_ID_ENE_CB714_SD,
314 .subvendor = PCI_ANY_ID,
315 .subdevice = PCI_ANY_ID,
316 .driver_data = (kernel_ulong_t)&sdhci_ene_714,
320 .vendor = PCI_VENDOR_ID_ENE,
321 .device = PCI_DEVICE_ID_ENE_CB714_SD_2,
322 .subvendor = PCI_ANY_ID,
323 .subdevice = PCI_ANY_ID,
324 .driver_data = (kernel_ulong_t)&sdhci_ene_714,
328 .vendor = PCI_VENDOR_ID_MARVELL,
329 .device = PCI_DEVICE_ID_MARVELL_CAFE_SD,
330 .subvendor = PCI_ANY_ID,
331 .subdevice = PCI_ANY_ID,
332 .driver_data = (kernel_ulong_t)&sdhci_cafe,
336 .vendor = PCI_VENDOR_ID_JMICRON,
337 .device = PCI_DEVICE_ID_JMICRON_JMB38X_SD,
338 .subvendor = PCI_ANY_ID,
339 .subdevice = PCI_ANY_ID,
340 .driver_data = (kernel_ulong_t)&sdhci_jmicron,
344 .vendor = PCI_VENDOR_ID_JMICRON,
345 .device = PCI_DEVICE_ID_JMICRON_JMB38X_MMC,
346 .subvendor = PCI_ANY_ID,
347 .subdevice = PCI_ANY_ID,
348 .driver_data = (kernel_ulong_t)&sdhci_jmicron,
351 { /* Generic SD host controller */
352 PCI_DEVICE_CLASS((PCI_CLASS_SYSTEM_SDHCI << 8), 0xFFFF00)
355 { /* end: all zeroes */ },
358 MODULE_DEVICE_TABLE(pci, pci_ids);
360 /*****************************************************************************\
362 * SDHCI core callbacks *
364 \*****************************************************************************/
366 static int sdhci_pci_enable_dma(struct sdhci_host *host)
368 struct sdhci_pci_slot *slot;
369 struct pci_dev *pdev;
372 slot = sdhci_priv(host);
373 pdev = slot->chip->pdev;
375 if (((pdev->class & 0xFFFF00) == (PCI_CLASS_SYSTEM_SDHCI << 8)) &&
376 ((pdev->class & 0x0000FF) != PCI_SDHCI_IFDMA) &&
377 (host->flags & SDHCI_USE_DMA)) {
378 dev_warn(&pdev->dev, "Will use DMA mode even though HW "
379 "doesn't fully claim to support it.\n");
382 ret = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
386 pci_set_master(pdev);
391 static struct sdhci_ops sdhci_pci_ops = {
392 .enable_dma = sdhci_pci_enable_dma,
395 /*****************************************************************************\
399 \*****************************************************************************/
403 static int sdhci_pci_suspend (struct pci_dev *pdev, pm_message_t state)
405 struct sdhci_pci_chip *chip;
406 struct sdhci_pci_slot *slot;
409 chip = pci_get_drvdata(pdev);
413 for (i = 0;i < chip->num_slots;i++) {
414 slot = chip->slots[i];
418 ret = sdhci_suspend_host(slot->host, state);
422 sdhci_resume_host(chip->slots[i]->host);
427 if (chip->fixes && chip->fixes->suspend) {
428 ret = chip->fixes->suspend(chip, state);
430 for (i = chip->num_slots - 1;i >= 0;i--)
431 sdhci_resume_host(chip->slots[i]->host);
436 pci_save_state(pdev);
437 pci_enable_wake(pdev, pci_choose_state(pdev, state), 0);
438 pci_disable_device(pdev);
439 pci_set_power_state(pdev, pci_choose_state(pdev, state));
444 static int sdhci_pci_resume (struct pci_dev *pdev)
446 struct sdhci_pci_chip *chip;
447 struct sdhci_pci_slot *slot;
450 chip = pci_get_drvdata(pdev);
454 pci_set_power_state(pdev, PCI_D0);
455 pci_restore_state(pdev);
456 ret = pci_enable_device(pdev);
460 if (chip->fixes && chip->fixes->resume) {
461 ret = chip->fixes->resume(chip);
466 for (i = 0;i < chip->num_slots;i++) {
467 slot = chip->slots[i];
471 ret = sdhci_resume_host(slot->host);
479 #else /* CONFIG_PM */
481 #define sdhci_pci_suspend NULL
482 #define sdhci_pci_resume NULL
484 #endif /* CONFIG_PM */
486 /*****************************************************************************\
488 * Device probing/removal *
490 \*****************************************************************************/
492 static struct sdhci_pci_slot * __devinit sdhci_pci_probe_slot(
493 struct pci_dev *pdev, struct sdhci_pci_chip *chip, int bar)
495 struct sdhci_pci_slot *slot;
496 struct sdhci_host *host;
498 resource_size_t addr;
502 if (!(pci_resource_flags(pdev, bar) & IORESOURCE_MEM)) {
503 dev_err(&pdev->dev, "BAR %d is not iomem. Aborting.\n", bar);
504 return ERR_PTR(-ENODEV);
507 if (pci_resource_len(pdev, bar) != 0x100) {
508 dev_err(&pdev->dev, "Invalid iomem size. You may "
509 "experience problems.\n");
512 if ((pdev->class & 0x0000FF) == PCI_SDHCI_IFVENDOR) {
513 dev_err(&pdev->dev, "Vendor specific interface. Aborting.\n");
514 return ERR_PTR(-ENODEV);
517 if ((pdev->class & 0x0000FF) > PCI_SDHCI_IFVENDOR) {
518 dev_err(&pdev->dev, "Unknown interface. Aborting.\n");
519 return ERR_PTR(-ENODEV);
522 host = sdhci_alloc_host(&pdev->dev, sizeof(struct sdhci_pci_slot));
528 slot = sdhci_priv(host);
534 host->hw_name = "PCI";
535 host->ops = &sdhci_pci_ops;
536 host->quirks = chip->quirks;
538 host->irq = pdev->irq;
540 ret = pci_request_region(pdev, bar, mmc_hostname(host->mmc));
542 dev_err(&pdev->dev, "cannot request region\n");
546 addr = pci_resource_start(pdev, bar);
547 host->ioaddr = ioremap_nocache(addr, pci_resource_len(pdev, bar));
549 dev_err(&pdev->dev, "failed to remap registers\n");
553 if (chip->fixes && chip->fixes->probe_slot) {
554 ret = chip->fixes->probe_slot(slot);
559 ret = sdhci_add_host(host);
566 if (chip->fixes && chip->fixes->remove_slot)
567 chip->fixes->remove_slot(slot, 0);
570 iounmap(host->ioaddr);
573 pci_release_region(pdev, bar);
574 sdhci_free_host(host);
579 static void sdhci_pci_remove_slot(struct sdhci_pci_slot *slot)
585 scratch = readl(slot->host->ioaddr + SDHCI_INT_STATUS);
586 if (scratch == (u32)-1)
589 sdhci_remove_host(slot->host, dead);
591 if (slot->chip->fixes && slot->chip->fixes->remove_slot)
592 slot->chip->fixes->remove_slot(slot, dead);
594 pci_release_region(slot->chip->pdev, slot->pci_bar);
596 sdhci_free_host(slot->host);
599 static int __devinit sdhci_pci_probe(struct pci_dev *pdev,
600 const struct pci_device_id *ent)
602 struct sdhci_pci_chip *chip;
603 struct sdhci_pci_slot *slot;
605 u8 slots, rev, first_bar;
608 BUG_ON(pdev == NULL);
611 pci_read_config_byte(pdev, PCI_CLASS_REVISION, &rev);
613 dev_info(&pdev->dev, "SDHCI controller found [%04x:%04x] (rev %x)\n",
614 (int)pdev->vendor, (int)pdev->device, (int)rev);
616 ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &slots);
620 slots = PCI_SLOT_INFO_SLOTS(slots) + 1;
621 dev_dbg(&pdev->dev, "found %d slot(s)\n", slots);
625 BUG_ON(slots > MAX_SLOTS);
627 ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &first_bar);
631 first_bar &= PCI_SLOT_INFO_FIRST_BAR_MASK;
634 dev_err(&pdev->dev, "Invalid first BAR. Aborting.\n");
638 ret = pci_enable_device(pdev);
642 chip = kzalloc(sizeof(struct sdhci_pci_chip), GFP_KERNEL);
649 chip->fixes = (const struct sdhci_pci_fixes*)ent->driver_data;
651 chip->quirks = chip->fixes->quirks;
652 chip->num_slots = slots;
654 pci_set_drvdata(pdev, chip);
656 if (chip->fixes && chip->fixes->probe) {
657 ret = chip->fixes->probe(chip);
662 for (i = 0;i < slots;i++) {
663 slot = sdhci_pci_probe_slot(pdev, chip, first_bar + i);
666 sdhci_pci_remove_slot(chip->slots[i]);
671 chip->slots[i] = slot;
677 pci_set_drvdata(pdev, NULL);
681 pci_disable_device(pdev);
685 static void __devexit sdhci_pci_remove(struct pci_dev *pdev)
688 struct sdhci_pci_chip *chip;
690 chip = pci_get_drvdata(pdev);
693 for (i = 0;i < chip->num_slots; i++)
694 sdhci_pci_remove_slot(chip->slots[i]);
696 pci_set_drvdata(pdev, NULL);
700 pci_disable_device(pdev);
703 static struct pci_driver sdhci_driver = {
706 .probe = sdhci_pci_probe,
707 .remove = __devexit_p(sdhci_pci_remove),
708 .suspend = sdhci_pci_suspend,
709 .resume = sdhci_pci_resume,
712 /*****************************************************************************\
716 \*****************************************************************************/
718 static int __init sdhci_drv_init(void)
720 return pci_register_driver(&sdhci_driver);
723 static void __exit sdhci_drv_exit(void)
725 pci_unregister_driver(&sdhci_driver);
728 module_init(sdhci_drv_init);
729 module_exit(sdhci_drv_exit);
731 MODULE_AUTHOR("Pierre Ossman <drzeus@drzeus.cx>");
732 MODULE_DESCRIPTION("Secure Digital Host Controller Interface PCI driver");
733 MODULE_LICENSE("GPL");