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_RESET_AFTER_REQUEST;
149 * JMicron chips can have two interfaces to the same hardware
150 * in order to work around limitations in Microsoft's driver.
151 * We need to make sure we only bind to one of them.
153 * This code assumes two things:
155 * 1. The PCI code adds subfunctions in order.
157 * 2. The MMC interface has a lower subfunction number
158 * than the SD interface.
160 if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_SD) {
161 struct pci_dev *sd_dev;
164 while ((sd_dev = pci_get_device(PCI_VENDOR_ID_JMICRON,
165 PCI_DEVICE_ID_JMICRON_JMB38X_MMC, sd_dev)) != NULL) {
166 if ((PCI_SLOT(chip->pdev->devfn) ==
167 PCI_SLOT(sd_dev->devfn)) &&
168 (chip->pdev->bus == sd_dev->bus))
174 dev_info(&chip->pdev->dev, "Refusing to bind to "
175 "secondary interface.\n");
181 * JMicron chips need a bit of a nudge to enable the power
184 ret = jmicron_pmos(chip, 1);
186 dev_err(&chip->pdev->dev, "Failure enabling card power\n");
193 static void jmicron_enable_mmc(struct sdhci_host *host, int on)
197 scratch = readb(host->ioaddr + 0xC0);
204 writeb(scratch, host->ioaddr + 0xC0);
207 static int jmicron_probe_slot(struct sdhci_pci_slot *slot)
210 * The secondary interface requires a bit set to get the
213 if (slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC)
214 jmicron_enable_mmc(slot->host, 1);
219 static void jmicron_remove_slot(struct sdhci_pci_slot *slot, int dead)
224 if (slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC)
225 jmicron_enable_mmc(slot->host, 0);
228 static int jmicron_suspend(struct sdhci_pci_chip *chip, pm_message_t state)
232 if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC) {
233 for (i = 0;i < chip->num_slots;i++)
234 jmicron_enable_mmc(chip->slots[i]->host, 0);
240 static int jmicron_resume(struct sdhci_pci_chip *chip)
244 if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC) {
245 for (i = 0;i < chip->num_slots;i++)
246 jmicron_enable_mmc(chip->slots[i]->host, 1);
249 ret = jmicron_pmos(chip, 1);
251 dev_err(&chip->pdev->dev, "Failure enabling card power\n");
258 static const struct sdhci_pci_fixes sdhci_jmicron = {
259 .probe = jmicron_probe,
261 .probe_slot = jmicron_probe_slot,
262 .remove_slot = jmicron_remove_slot,
264 .suspend = jmicron_suspend,
265 .resume = jmicron_resume,
268 static const struct pci_device_id pci_ids[] __devinitdata = {
270 .vendor = PCI_VENDOR_ID_RICOH,
271 .device = PCI_DEVICE_ID_RICOH_R5C822,
272 .subvendor = PCI_ANY_ID,
273 .subdevice = PCI_ANY_ID,
274 .driver_data = (kernel_ulong_t)&sdhci_ricoh,
278 .vendor = PCI_VENDOR_ID_ENE,
279 .device = PCI_DEVICE_ID_ENE_CB712_SD,
280 .subvendor = PCI_ANY_ID,
281 .subdevice = PCI_ANY_ID,
282 .driver_data = (kernel_ulong_t)&sdhci_ene_712,
286 .vendor = PCI_VENDOR_ID_ENE,
287 .device = PCI_DEVICE_ID_ENE_CB712_SD_2,
288 .subvendor = PCI_ANY_ID,
289 .subdevice = PCI_ANY_ID,
290 .driver_data = (kernel_ulong_t)&sdhci_ene_712,
294 .vendor = PCI_VENDOR_ID_ENE,
295 .device = PCI_DEVICE_ID_ENE_CB714_SD,
296 .subvendor = PCI_ANY_ID,
297 .subdevice = PCI_ANY_ID,
298 .driver_data = (kernel_ulong_t)&sdhci_ene_714,
302 .vendor = PCI_VENDOR_ID_ENE,
303 .device = PCI_DEVICE_ID_ENE_CB714_SD_2,
304 .subvendor = PCI_ANY_ID,
305 .subdevice = PCI_ANY_ID,
306 .driver_data = (kernel_ulong_t)&sdhci_ene_714,
310 .vendor = PCI_VENDOR_ID_MARVELL,
311 .device = PCI_DEVICE_ID_MARVELL_CAFE_SD,
312 .subvendor = PCI_ANY_ID,
313 .subdevice = PCI_ANY_ID,
314 .driver_data = (kernel_ulong_t)&sdhci_cafe,
318 .vendor = PCI_VENDOR_ID_JMICRON,
319 .device = PCI_DEVICE_ID_JMICRON_JMB38X_SD,
320 .subvendor = PCI_ANY_ID,
321 .subdevice = PCI_ANY_ID,
322 .driver_data = (kernel_ulong_t)&sdhci_jmicron,
326 .vendor = PCI_VENDOR_ID_JMICRON,
327 .device = PCI_DEVICE_ID_JMICRON_JMB38X_MMC,
328 .subvendor = PCI_ANY_ID,
329 .subdevice = PCI_ANY_ID,
330 .driver_data = (kernel_ulong_t)&sdhci_jmicron,
333 { /* Generic SD host controller */
334 PCI_DEVICE_CLASS((PCI_CLASS_SYSTEM_SDHCI << 8), 0xFFFF00)
337 { /* end: all zeroes */ },
340 MODULE_DEVICE_TABLE(pci, pci_ids);
342 /*****************************************************************************\
344 * SDHCI core callbacks *
346 \*****************************************************************************/
348 static int sdhci_pci_enable_dma(struct sdhci_host *host)
350 struct sdhci_pci_slot *slot;
351 struct pci_dev *pdev;
354 slot = sdhci_priv(host);
355 pdev = slot->chip->pdev;
357 if (((pdev->class & 0xFFFF00) == (PCI_CLASS_SYSTEM_SDHCI << 8)) &&
358 ((pdev->class & 0x0000FF) != PCI_SDHCI_IFDMA) &&
359 (host->flags & SDHCI_USE_DMA)) {
360 dev_warn(&pdev->dev, "Will use DMA mode even though HW "
361 "doesn't fully claim to support it.\n");
364 ret = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
368 pci_set_master(pdev);
373 static struct sdhci_ops sdhci_pci_ops = {
374 .enable_dma = sdhci_pci_enable_dma,
377 /*****************************************************************************\
381 \*****************************************************************************/
385 static int sdhci_pci_suspend (struct pci_dev *pdev, pm_message_t state)
387 struct sdhci_pci_chip *chip;
388 struct sdhci_pci_slot *slot;
391 chip = pci_get_drvdata(pdev);
395 for (i = 0;i < chip->num_slots;i++) {
396 slot = chip->slots[i];
400 ret = sdhci_suspend_host(slot->host, state);
404 sdhci_resume_host(chip->slots[i]->host);
409 if (chip->fixes && chip->fixes->suspend) {
410 ret = chip->fixes->suspend(chip, state);
412 for (i = chip->num_slots - 1;i >= 0;i--)
413 sdhci_resume_host(chip->slots[i]->host);
418 pci_save_state(pdev);
419 pci_enable_wake(pdev, pci_choose_state(pdev, state), 0);
420 pci_disable_device(pdev);
421 pci_set_power_state(pdev, pci_choose_state(pdev, state));
426 static int sdhci_pci_resume (struct pci_dev *pdev)
428 struct sdhci_pci_chip *chip;
429 struct sdhci_pci_slot *slot;
432 chip = pci_get_drvdata(pdev);
436 pci_set_power_state(pdev, PCI_D0);
437 pci_restore_state(pdev);
438 ret = pci_enable_device(pdev);
442 if (chip->fixes && chip->fixes->resume) {
443 ret = chip->fixes->resume(chip);
448 for (i = 0;i < chip->num_slots;i++) {
449 slot = chip->slots[i];
453 ret = sdhci_resume_host(slot->host);
461 #else /* CONFIG_PM */
463 #define sdhci_pci_suspend NULL
464 #define sdhci_pci_resume NULL
466 #endif /* CONFIG_PM */
468 /*****************************************************************************\
470 * Device probing/removal *
472 \*****************************************************************************/
474 static struct sdhci_pci_slot * __devinit sdhci_pci_probe_slot(
475 struct pci_dev *pdev, struct sdhci_pci_chip *chip, int bar)
477 struct sdhci_pci_slot *slot;
478 struct sdhci_host *host;
480 resource_size_t addr;
484 if (!(pci_resource_flags(pdev, bar) & IORESOURCE_MEM)) {
485 dev_err(&pdev->dev, "BAR %d is not iomem. Aborting.\n", bar);
486 return ERR_PTR(-ENODEV);
489 if (pci_resource_len(pdev, bar) != 0x100) {
490 dev_err(&pdev->dev, "Invalid iomem size. You may "
491 "experience problems.\n");
494 if ((pdev->class & 0x0000FF) == PCI_SDHCI_IFVENDOR) {
495 dev_err(&pdev->dev, "Vendor specific interface. Aborting.\n");
496 return ERR_PTR(-ENODEV);
499 if ((pdev->class & 0x0000FF) > PCI_SDHCI_IFVENDOR) {
500 dev_err(&pdev->dev, "Unknown interface. Aborting.\n");
501 return ERR_PTR(-ENODEV);
504 host = sdhci_alloc_host(&pdev->dev, sizeof(struct sdhci_pci_slot));
510 slot = sdhci_priv(host);
516 host->hw_name = "PCI";
517 host->ops = &sdhci_pci_ops;
518 host->quirks = chip->quirks;
520 host->irq = pdev->irq;
522 ret = pci_request_region(pdev, bar, mmc_hostname(host->mmc));
524 dev_err(&pdev->dev, "cannot request region\n");
528 addr = pci_resource_start(pdev, bar);
529 host->ioaddr = ioremap_nocache(addr, pci_resource_len(pdev, bar));
531 dev_err(&pdev->dev, "failed to remap registers\n");
535 if (chip->fixes && chip->fixes->probe_slot) {
536 ret = chip->fixes->probe_slot(slot);
541 ret = sdhci_add_host(host);
548 if (chip->fixes && chip->fixes->remove_slot)
549 chip->fixes->remove_slot(slot, 0);
552 iounmap(host->ioaddr);
555 pci_release_region(pdev, bar);
556 sdhci_free_host(host);
561 static void sdhci_pci_remove_slot(struct sdhci_pci_slot *slot)
567 scratch = readl(slot->host->ioaddr + SDHCI_INT_STATUS);
568 if (scratch == (u32)-1)
571 sdhci_remove_host(slot->host, dead);
573 if (slot->chip->fixes && slot->chip->fixes->remove_slot)
574 slot->chip->fixes->remove_slot(slot, dead);
576 pci_release_region(slot->chip->pdev, slot->pci_bar);
578 sdhci_free_host(slot->host);
581 static int __devinit sdhci_pci_probe(struct pci_dev *pdev,
582 const struct pci_device_id *ent)
584 struct sdhci_pci_chip *chip;
585 struct sdhci_pci_slot *slot;
587 u8 slots, rev, first_bar;
590 BUG_ON(pdev == NULL);
593 pci_read_config_byte(pdev, PCI_CLASS_REVISION, &rev);
595 dev_info(&pdev->dev, "SDHCI controller found [%04x:%04x] (rev %x)\n",
596 (int)pdev->vendor, (int)pdev->device, (int)rev);
598 ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &slots);
602 slots = PCI_SLOT_INFO_SLOTS(slots) + 1;
603 dev_dbg(&pdev->dev, "found %d slot(s)\n", slots);
607 BUG_ON(slots > MAX_SLOTS);
609 ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &first_bar);
613 first_bar &= PCI_SLOT_INFO_FIRST_BAR_MASK;
616 dev_err(&pdev->dev, "Invalid first BAR. Aborting.\n");
620 ret = pci_enable_device(pdev);
624 chip = kzalloc(sizeof(struct sdhci_pci_chip), GFP_KERNEL);
631 chip->fixes = (const struct sdhci_pci_fixes*)ent->driver_data;
633 chip->quirks = chip->fixes->quirks;
634 chip->num_slots = slots;
636 pci_set_drvdata(pdev, chip);
638 if (chip->fixes && chip->fixes->probe) {
639 ret = chip->fixes->probe(chip);
644 for (i = 0;i < slots;i++) {
645 slot = sdhci_pci_probe_slot(pdev, chip, first_bar + i);
648 sdhci_pci_remove_slot(chip->slots[i]);
653 chip->slots[i] = slot;
659 pci_set_drvdata(pdev, NULL);
663 pci_disable_device(pdev);
667 static void __devexit sdhci_pci_remove(struct pci_dev *pdev)
670 struct sdhci_pci_chip *chip;
672 chip = pci_get_drvdata(pdev);
675 for (i = 0;i < chip->num_slots; i++)
676 sdhci_pci_remove_slot(chip->slots[i]);
678 pci_set_drvdata(pdev, NULL);
682 pci_disable_device(pdev);
685 static struct pci_driver sdhci_driver = {
688 .probe = sdhci_pci_probe,
689 .remove = __devexit_p(sdhci_pci_remove),
690 .suspend = sdhci_pci_suspend,
691 .resume = sdhci_pci_resume,
694 /*****************************************************************************\
698 \*****************************************************************************/
700 static int __init sdhci_drv_init(void)
702 return pci_register_driver(&sdhci_driver);
705 static void __exit sdhci_drv_exit(void)
707 pci_unregister_driver(&sdhci_driver);
710 module_init(sdhci_drv_init);
711 module_exit(sdhci_drv_exit);
713 MODULE_AUTHOR("Pierre Ossman <drzeus@drzeus.cx>");
714 MODULE_DESCRIPTION("Secure Digital Host Controller Interface PCI driver");
715 MODULE_LICENSE("GPL");