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*);
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)
214 if (slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC)
215 jmicron_enable_mmc(slot->host, 0);
218 static int jmicron_suspend(struct sdhci_pci_chip *chip, pm_message_t state)
222 if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC) {
223 for (i = 0;i < chip->num_slots;i++)
224 jmicron_enable_mmc(chip->slots[i]->host, 0);
230 static int jmicron_resume(struct sdhci_pci_chip *chip)
234 if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC) {
235 for (i = 0;i < chip->num_slots;i++)
236 jmicron_enable_mmc(chip->slots[i]->host, 1);
239 ret = jmicron_pmos(chip, 1);
241 dev_err(&chip->pdev->dev, "Failure enabling card power\n");
248 static const struct sdhci_pci_fixes sdhci_jmicron = {
249 .quirks = SDHCI_QUIRK_32BIT_DMA_ADDR |
250 SDHCI_QUIRK_32BIT_DMA_SIZE |
251 SDHCI_QUIRK_RESET_AFTER_REQUEST,
253 .probe = jmicron_probe,
255 .probe_slot = jmicron_probe_slot,
256 .remove_slot = jmicron_remove_slot,
258 .suspend = jmicron_suspend,
259 .resume = jmicron_resume,
262 static const struct pci_device_id pci_ids[] __devinitdata = {
264 .vendor = PCI_VENDOR_ID_RICOH,
265 .device = PCI_DEVICE_ID_RICOH_R5C822,
266 .subvendor = PCI_ANY_ID,
267 .subdevice = PCI_ANY_ID,
268 .driver_data = (kernel_ulong_t)&sdhci_ricoh,
272 .vendor = PCI_VENDOR_ID_ENE,
273 .device = PCI_DEVICE_ID_ENE_CB712_SD,
274 .subvendor = PCI_ANY_ID,
275 .subdevice = PCI_ANY_ID,
276 .driver_data = (kernel_ulong_t)&sdhci_ene_712,
280 .vendor = PCI_VENDOR_ID_ENE,
281 .device = PCI_DEVICE_ID_ENE_CB712_SD_2,
282 .subvendor = PCI_ANY_ID,
283 .subdevice = PCI_ANY_ID,
284 .driver_data = (kernel_ulong_t)&sdhci_ene_712,
288 .vendor = PCI_VENDOR_ID_ENE,
289 .device = PCI_DEVICE_ID_ENE_CB714_SD,
290 .subvendor = PCI_ANY_ID,
291 .subdevice = PCI_ANY_ID,
292 .driver_data = (kernel_ulong_t)&sdhci_ene_714,
296 .vendor = PCI_VENDOR_ID_ENE,
297 .device = PCI_DEVICE_ID_ENE_CB714_SD_2,
298 .subvendor = PCI_ANY_ID,
299 .subdevice = PCI_ANY_ID,
300 .driver_data = (kernel_ulong_t)&sdhci_ene_714,
304 .vendor = PCI_VENDOR_ID_MARVELL,
305 .device = PCI_DEVICE_ID_MARVELL_CAFE_SD,
306 .subvendor = PCI_ANY_ID,
307 .subdevice = PCI_ANY_ID,
308 .driver_data = (kernel_ulong_t)&sdhci_cafe,
312 .vendor = PCI_VENDOR_ID_JMICRON,
313 .device = PCI_DEVICE_ID_JMICRON_JMB38X_SD,
314 .subvendor = PCI_ANY_ID,
315 .subdevice = PCI_ANY_ID,
316 .driver_data = (kernel_ulong_t)&sdhci_jmicron,
320 .vendor = PCI_VENDOR_ID_JMICRON,
321 .device = PCI_DEVICE_ID_JMICRON_JMB38X_MMC,
322 .subvendor = PCI_ANY_ID,
323 .subdevice = PCI_ANY_ID,
324 .driver_data = (kernel_ulong_t)&sdhci_jmicron,
327 { /* Generic SD host controller */
328 PCI_DEVICE_CLASS((PCI_CLASS_SYSTEM_SDHCI << 8), 0xFFFF00)
331 { /* end: all zeroes */ },
334 MODULE_DEVICE_TABLE(pci, pci_ids);
336 /*****************************************************************************\
338 * SDHCI core callbacks *
340 \*****************************************************************************/
342 static int sdhci_pci_enable_dma(struct sdhci_host *host)
344 struct sdhci_pci_slot *slot;
345 struct pci_dev *pdev;
348 slot = sdhci_priv(host);
349 pdev = slot->chip->pdev;
351 if (((pdev->class & 0xFFFF00) == (PCI_CLASS_SYSTEM_SDHCI << 8)) &&
352 ((pdev->class & 0x0000FF) != PCI_SDHCI_IFDMA) &&
353 (host->flags & SDHCI_USE_DMA)) {
354 dev_warn(&pdev->dev, "Will use DMA mode even though HW "
355 "doesn't fully claim to support it.\n");
358 ret = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
362 pci_set_master(pdev);
367 static struct sdhci_ops sdhci_pci_ops = {
368 .enable_dma = sdhci_pci_enable_dma,
371 /*****************************************************************************\
375 \*****************************************************************************/
379 static int sdhci_pci_suspend (struct pci_dev *pdev, pm_message_t state)
381 struct sdhci_pci_chip *chip;
382 struct sdhci_pci_slot *slot;
385 chip = pci_get_drvdata(pdev);
389 for (i = 0;i < chip->num_slots;i++) {
390 slot = chip->slots[i];
394 ret = sdhci_suspend_host(slot->host, state);
398 sdhci_resume_host(chip->slots[i]->host);
403 if (chip->fixes && chip->fixes->suspend) {
404 ret = chip->fixes->suspend(chip, state);
406 for (i = chip->num_slots - 1;i >= 0;i--)
407 sdhci_resume_host(chip->slots[i]->host);
412 pci_save_state(pdev);
413 pci_enable_wake(pdev, pci_choose_state(pdev, state), 0);
414 pci_disable_device(pdev);
415 pci_set_power_state(pdev, pci_choose_state(pdev, state));
420 static int sdhci_pci_resume (struct pci_dev *pdev)
422 struct sdhci_pci_chip *chip;
423 struct sdhci_pci_slot *slot;
426 chip = pci_get_drvdata(pdev);
430 pci_set_power_state(pdev, PCI_D0);
431 pci_restore_state(pdev);
432 ret = pci_enable_device(pdev);
436 if (chip->fixes && chip->fixes->resume) {
437 ret = chip->fixes->resume(chip);
442 for (i = 0;i < chip->num_slots;i++) {
443 slot = chip->slots[i];
447 ret = sdhci_resume_host(slot->host);
455 #else /* CONFIG_PM */
457 #define sdhci_pci_suspend NULL
458 #define sdhci_pci_resume NULL
460 #endif /* CONFIG_PM */
462 /*****************************************************************************\
464 * Device probing/removal *
466 \*****************************************************************************/
468 static struct sdhci_pci_slot * __devinit sdhci_pci_probe_slot(
469 struct pci_dev *pdev, struct sdhci_pci_chip *chip, int bar)
471 struct sdhci_pci_slot *slot;
472 struct sdhci_host *host;
474 resource_size_t addr;
478 if (!(pci_resource_flags(pdev, bar) & IORESOURCE_MEM)) {
479 dev_err(&pdev->dev, "BAR %d is not iomem. Aborting.\n", bar);
480 return ERR_PTR(-ENODEV);
483 if (pci_resource_len(pdev, bar) != 0x100) {
484 dev_err(&pdev->dev, "Invalid iomem size. You may "
485 "experience problems.\n");
488 if ((pdev->class & 0x0000FF) == PCI_SDHCI_IFVENDOR) {
489 dev_err(&pdev->dev, "Vendor specific interface. Aborting.\n");
490 return ERR_PTR(-ENODEV);
493 if ((pdev->class & 0x0000FF) > PCI_SDHCI_IFVENDOR) {
494 dev_err(&pdev->dev, "Unknown interface. Aborting.\n");
495 return ERR_PTR(-ENODEV);
498 host = sdhci_alloc_host(&pdev->dev, sizeof(struct sdhci_pci_slot));
504 slot = sdhci_priv(host);
510 host->hw_name = "PCI";
511 host->ops = &sdhci_pci_ops;
512 host->quirks = chip->quirks;
514 host->irq = pdev->irq;
516 ret = pci_request_region(pdev, bar, mmc_hostname(host->mmc));
518 dev_err(&pdev->dev, "cannot request region\n");
522 addr = pci_resource_start(pdev, bar);
523 host->ioaddr = ioremap_nocache(addr, pci_resource_len(pdev, bar));
525 dev_err(&pdev->dev, "failed to remap registers\n");
529 if (chip->fixes && chip->fixes->probe_slot) {
530 ret = chip->fixes->probe_slot(slot);
535 ret = sdhci_add_host(host);
542 if (chip->fixes && chip->fixes->remove_slot)
543 chip->fixes->remove_slot(slot);
546 iounmap(host->ioaddr);
549 pci_release_region(pdev, bar);
550 sdhci_free_host(host);
555 static void sdhci_pci_remove_slot(struct sdhci_pci_slot *slot)
557 sdhci_remove_host(slot->host);
559 if (slot->chip->fixes && slot->chip->fixes->remove_slot)
560 slot->chip->fixes->remove_slot(slot);
562 pci_release_region(slot->chip->pdev, slot->pci_bar);
564 sdhci_free_host(slot->host);
567 static int __devinit sdhci_pci_probe(struct pci_dev *pdev,
568 const struct pci_device_id *ent)
570 struct sdhci_pci_chip *chip;
571 struct sdhci_pci_slot *slot;
573 u8 slots, rev, first_bar;
576 BUG_ON(pdev == NULL);
579 pci_read_config_byte(pdev, PCI_CLASS_REVISION, &rev);
581 dev_info(&pdev->dev, "SDHCI controller found [%04x:%04x] (rev %x)\n",
582 (int)pdev->vendor, (int)pdev->device, (int)rev);
584 ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &slots);
588 slots = PCI_SLOT_INFO_SLOTS(slots) + 1;
589 dev_dbg(&pdev->dev, "found %d slot(s)\n", slots);
593 BUG_ON(slots > MAX_SLOTS);
595 ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &first_bar);
599 first_bar &= PCI_SLOT_INFO_FIRST_BAR_MASK;
602 dev_err(&pdev->dev, "Invalid first BAR. Aborting.\n");
606 ret = pci_enable_device(pdev);
610 chip = kzalloc(sizeof(struct sdhci_pci_chip), GFP_KERNEL);
617 chip->fixes = (const struct sdhci_pci_fixes*)ent->driver_data;
619 chip->quirks = chip->fixes->quirks;
620 chip->num_slots = slots;
622 pci_set_drvdata(pdev, chip);
624 if (chip->fixes && chip->fixes->probe) {
625 ret = chip->fixes->probe(chip);
630 for (i = 0;i < slots;i++) {
631 slot = sdhci_pci_probe_slot(pdev, chip, first_bar + i);
634 sdhci_pci_remove_slot(chip->slots[i]);
639 chip->slots[i] = slot;
645 pci_set_drvdata(pdev, NULL);
649 pci_disable_device(pdev);
653 static void __devexit sdhci_pci_remove(struct pci_dev *pdev)
656 struct sdhci_pci_chip *chip;
658 chip = pci_get_drvdata(pdev);
661 for (i = 0;i < chip->num_slots; i++)
662 sdhci_pci_remove_slot(chip->slots[i]);
664 pci_set_drvdata(pdev, NULL);
668 pci_disable_device(pdev);
671 static struct pci_driver sdhci_driver = {
674 .probe = sdhci_pci_probe,
675 .remove = __devexit_p(sdhci_pci_remove),
676 .suspend = sdhci_pci_suspend,
677 .resume = sdhci_pci_resume,
680 /*****************************************************************************\
684 \*****************************************************************************/
686 static int __init sdhci_drv_init(void)
688 return pci_register_driver(&sdhci_driver);
691 static void __exit sdhci_drv_exit(void)
693 pci_unregister_driver(&sdhci_driver);
696 module_init(sdhci_drv_init);
697 module_exit(sdhci_drv_exit);
699 MODULE_AUTHOR("Pierre Ossman <drzeus@drzeus.cx>");
700 MODULE_DESCRIPTION("Secure Digital Host Controller Interface PCI driver");
701 MODULE_LICENSE("GPL");