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 static const struct pci_device_id pci_ids[] __devinitdata = {
43 .vendor = PCI_VENDOR_ID_RICOH,
44 .device = PCI_DEVICE_ID_RICOH_R5C822,
45 .subvendor = PCI_VENDOR_ID_IBM,
46 .subdevice = PCI_ANY_ID,
47 .driver_data = SDHCI_QUIRK_CLOCK_BEFORE_RESET,
51 .vendor = PCI_VENDOR_ID_RICOH,
52 .device = PCI_DEVICE_ID_RICOH_R5C822,
53 .subvendor = PCI_VENDOR_ID_SAMSUNG,
54 .subdevice = PCI_ANY_ID,
55 .driver_data = SDHCI_QUIRK_NO_CARD_NO_RESET,
59 .vendor = PCI_VENDOR_ID_ENE,
60 .device = PCI_DEVICE_ID_ENE_CB712_SD,
61 .subvendor = PCI_ANY_ID,
62 .subdevice = PCI_ANY_ID,
63 .driver_data = SDHCI_QUIRK_SINGLE_POWER_WRITE |
64 SDHCI_QUIRK_BROKEN_DMA,
68 .vendor = PCI_VENDOR_ID_ENE,
69 .device = PCI_DEVICE_ID_ENE_CB712_SD_2,
70 .subvendor = PCI_ANY_ID,
71 .subdevice = PCI_ANY_ID,
72 .driver_data = SDHCI_QUIRK_SINGLE_POWER_WRITE |
73 SDHCI_QUIRK_BROKEN_DMA,
77 .vendor = PCI_VENDOR_ID_ENE,
78 .device = PCI_DEVICE_ID_ENE_CB714_SD,
79 .subvendor = PCI_ANY_ID,
80 .subdevice = PCI_ANY_ID,
81 .driver_data = SDHCI_QUIRK_SINGLE_POWER_WRITE |
82 SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS |
83 SDHCI_QUIRK_BROKEN_DMA,
87 .vendor = PCI_VENDOR_ID_ENE,
88 .device = PCI_DEVICE_ID_ENE_CB714_SD_2,
89 .subvendor = PCI_ANY_ID,
90 .subdevice = PCI_ANY_ID,
91 .driver_data = SDHCI_QUIRK_SINGLE_POWER_WRITE |
92 SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS |
93 SDHCI_QUIRK_BROKEN_DMA,
97 .vendor = PCI_VENDOR_ID_MARVELL,
98 .device = PCI_DEVICE_ID_MARVELL_CAFE_SD,
99 .subvendor = PCI_ANY_ID,
100 .subdevice = PCI_ANY_ID,
101 .driver_data = SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER |
102 SDHCI_QUIRK_INCR_TIMEOUT_CONTROL,
106 .vendor = PCI_VENDOR_ID_JMICRON,
107 .device = PCI_DEVICE_ID_JMICRON_JMB38X_SD,
108 .subvendor = PCI_ANY_ID,
109 .subdevice = PCI_ANY_ID,
110 .driver_data = SDHCI_QUIRK_32BIT_DMA_ADDR |
111 SDHCI_QUIRK_32BIT_DMA_SIZE |
112 SDHCI_QUIRK_RESET_AFTER_REQUEST,
115 { /* Generic SD host controller */
116 PCI_DEVICE_CLASS((PCI_CLASS_SYSTEM_SDHCI << 8), 0xFFFF00)
119 { /* end: all zeroes */ },
122 MODULE_DEVICE_TABLE(pci, pci_ids);
124 struct sdhci_pci_chip;
126 struct sdhci_pci_slot {
127 struct sdhci_pci_chip *chip;
128 struct sdhci_host *host;
133 struct sdhci_pci_chip {
134 struct pci_dev *pdev;
137 int num_slots; /* Slots on controller */
138 struct sdhci_pci_slot *slots[MAX_SLOTS]; /* Pointers to host slots */
141 /*****************************************************************************\
143 * SDHCI core callbacks *
145 \*****************************************************************************/
147 static int sdhci_pci_enable_dma(struct sdhci_host *host)
149 struct sdhci_pci_slot *slot;
150 struct pci_dev *pdev;
153 slot = sdhci_priv(host);
154 pdev = slot->chip->pdev;
156 if (((pdev->class & 0xFFFF00) == (PCI_CLASS_SYSTEM_SDHCI << 8)) &&
157 ((pdev->class & 0x0000FF) != PCI_SDHCI_IFDMA) &&
158 (host->flags & SDHCI_USE_DMA)) {
159 dev_warn(&pdev->dev, "Will use DMA mode even though HW "
160 "doesn't fully claim to support it.\n");
163 ret = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
167 pci_set_master(pdev);
172 static struct sdhci_ops sdhci_pci_ops = {
173 .enable_dma = sdhci_pci_enable_dma,
176 /*****************************************************************************\
180 \*****************************************************************************/
184 static int sdhci_pci_suspend (struct pci_dev *pdev, pm_message_t state)
186 struct sdhci_pci_chip *chip;
187 struct sdhci_pci_slot *slot;
190 chip = pci_get_drvdata(pdev);
194 for (i = 0;i < chip->num_slots;i++) {
195 slot = chip->slots[i];
199 ret = sdhci_suspend_host(slot->host, state);
203 sdhci_resume_host(chip->slots[i]->host);
208 pci_save_state(pdev);
209 pci_enable_wake(pdev, pci_choose_state(pdev, state), 0);
210 pci_disable_device(pdev);
211 pci_set_power_state(pdev, pci_choose_state(pdev, state));
216 static int sdhci_pci_resume (struct pci_dev *pdev)
218 struct sdhci_pci_chip *chip;
219 struct sdhci_pci_slot *slot;
222 chip = pci_get_drvdata(pdev);
226 pci_set_power_state(pdev, PCI_D0);
227 pci_restore_state(pdev);
228 ret = pci_enable_device(pdev);
232 for (i = 0;i < chip->num_slots;i++) {
233 slot = chip->slots[i];
237 ret = sdhci_resume_host(slot->host);
245 #else /* CONFIG_PM */
247 #define sdhci_pci_suspend NULL
248 #define sdhci_pci_resume NULL
250 #endif /* CONFIG_PM */
252 /*****************************************************************************\
254 * Device probing/removal *
256 \*****************************************************************************/
258 static struct sdhci_pci_slot * __devinit sdhci_pci_probe_slot(
259 struct pci_dev *pdev, struct sdhci_pci_chip *chip, int bar)
261 struct sdhci_pci_slot *slot;
262 struct sdhci_host *host;
264 resource_size_t addr;
268 if (!(pci_resource_flags(pdev, bar) & IORESOURCE_MEM)) {
269 dev_err(&pdev->dev, "BAR %d is not iomem. Aborting.\n", bar);
270 return ERR_PTR(-ENODEV);
273 if (pci_resource_len(pdev, bar) != 0x100) {
274 dev_err(&pdev->dev, "Invalid iomem size. You may "
275 "experience problems.\n");
278 if ((pdev->class & 0x0000FF) == PCI_SDHCI_IFVENDOR) {
279 dev_err(&pdev->dev, "Vendor specific interface. Aborting.\n");
280 return ERR_PTR(-ENODEV);
283 if ((pdev->class & 0x0000FF) > PCI_SDHCI_IFVENDOR) {
284 dev_err(&pdev->dev, "Unknown interface. Aborting.\n");
285 return ERR_PTR(-ENODEV);
288 host = sdhci_alloc_host(&pdev->dev, sizeof(struct sdhci_pci_slot));
294 slot = sdhci_priv(host);
300 host->hw_name = "PCI";
301 host->ops = &sdhci_pci_ops;
302 host->quirks = chip->quirks;
304 host->irq = pdev->irq;
306 ret = pci_request_region(pdev, bar, mmc_hostname(host->mmc));
308 dev_err(&pdev->dev, "cannot request region\n");
312 addr = pci_resource_start(pdev, bar);
313 host->ioaddr = ioremap_nocache(addr, pci_resource_len(pdev, bar));
315 dev_err(&pdev->dev, "failed to remap registers\n");
319 ret = sdhci_add_host(host);
326 iounmap(host->ioaddr);
329 pci_release_region(pdev, bar);
330 sdhci_free_host(host);
335 static void sdhci_pci_remove_slot(struct sdhci_pci_slot *slot)
337 sdhci_remove_host(slot->host);
338 pci_release_region(slot->chip->pdev, slot->pci_bar);
339 sdhci_free_host(slot->host);
342 static int __devinit sdhci_pci_probe(struct pci_dev *pdev,
343 const struct pci_device_id *ent)
345 struct sdhci_pci_chip *chip;
346 struct sdhci_pci_slot *slot;
348 u8 slots, rev, first_bar;
351 BUG_ON(pdev == NULL);
354 pci_read_config_byte(pdev, PCI_CLASS_REVISION, &rev);
356 dev_info(&pdev->dev, "SDHCI controller found [%04x:%04x] (rev %x)\n",
357 (int)pdev->vendor, (int)pdev->device, (int)rev);
359 ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &slots);
363 slots = PCI_SLOT_INFO_SLOTS(slots) + 1;
364 dev_dbg(&pdev->dev, "found %d slot(s)\n", slots);
368 BUG_ON(slots > MAX_SLOTS);
370 ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &first_bar);
374 first_bar &= PCI_SLOT_INFO_FIRST_BAR_MASK;
377 dev_err(&pdev->dev, "Invalid first BAR. Aborting.\n");
381 ret = pci_enable_device(pdev);
385 chip = kzalloc(sizeof(struct sdhci_pci_chip), GFP_KERNEL);
392 chip->quirks = ent->driver_data;
393 chip->num_slots = slots;
395 pci_set_drvdata(pdev, chip);
397 for (i = 0;i < slots;i++) {
398 slot = sdhci_pci_probe_slot(pdev, chip, first_bar + i);
401 sdhci_pci_remove_slot(chip->slots[i]);
406 chip->slots[i] = slot;
412 pci_set_drvdata(pdev, NULL);
416 pci_disable_device(pdev);
420 static void __devexit sdhci_pci_remove(struct pci_dev *pdev)
423 struct sdhci_pci_chip *chip;
425 chip = pci_get_drvdata(pdev);
428 for (i = 0;i < chip->num_slots; i++)
429 sdhci_pci_remove_slot(chip->slots[i]);
431 pci_set_drvdata(pdev, NULL);
435 pci_disable_device(pdev);
438 static struct pci_driver sdhci_driver = {
441 .probe = sdhci_pci_probe,
442 .remove = __devexit_p(sdhci_pci_remove),
443 .suspend = sdhci_pci_suspend,
444 .resume = sdhci_pci_resume,
447 /*****************************************************************************\
451 \*****************************************************************************/
453 static int __init sdhci_drv_init(void)
455 return pci_register_driver(&sdhci_driver);
458 static void __exit sdhci_drv_exit(void)
460 pci_unregister_driver(&sdhci_driver);
463 module_init(sdhci_drv_init);
464 module_exit(sdhci_drv_exit);
466 MODULE_AUTHOR("Pierre Ossman <drzeus@drzeus.cx>");
467 MODULE_DESCRIPTION("Secure Digital Host Controller Interface PCI driver");
468 MODULE_LICENSE("GPL");