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;
43 struct sdhci_pci_fixes {
46 int (*probe)(struct sdhci_pci_chip*);
48 int (*resume)(struct sdhci_pci_chip*);
51 struct sdhci_pci_slot {
52 struct sdhci_pci_chip *chip;
53 struct sdhci_host *host;
58 struct sdhci_pci_chip {
62 const struct sdhci_pci_fixes *fixes;
64 int num_slots; /* Slots on controller */
65 struct sdhci_pci_slot *slots[MAX_SLOTS]; /* Pointers to host slots */
69 /*****************************************************************************\
71 * Hardware specific quirk handling *
73 \*****************************************************************************/
75 static int ricoh_probe(struct sdhci_pci_chip *chip)
77 if (chip->pdev->subsystem_vendor == PCI_VENDOR_ID_IBM)
78 chip->quirks |= SDHCI_QUIRK_CLOCK_BEFORE_RESET;
80 if (chip->pdev->subsystem_vendor == PCI_VENDOR_ID_SAMSUNG)
81 chip->quirks |= SDHCI_QUIRK_NO_CARD_NO_RESET;
86 static const struct sdhci_pci_fixes sdhci_ricoh = {
90 static const struct sdhci_pci_fixes sdhci_ene_712 = {
91 .quirks = SDHCI_QUIRK_SINGLE_POWER_WRITE |
92 SDHCI_QUIRK_BROKEN_DMA,
95 static const struct sdhci_pci_fixes sdhci_ene_714 = {
96 .quirks = SDHCI_QUIRK_SINGLE_POWER_WRITE |
97 SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS |
98 SDHCI_QUIRK_BROKEN_DMA,
101 static const struct sdhci_pci_fixes sdhci_cafe = {
102 .quirks = SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER |
103 SDHCI_QUIRK_BROKEN_TIMEOUT_VAL,
106 static int jmicron_pmos(struct sdhci_pci_chip *chip, int on)
111 ret = pci_read_config_byte(chip->pdev, 0xAE, &scratch);
116 * Turn PMOS on [bit 0], set over current detection to 2.4 V
117 * [bit 1:2] and enable over current debouncing [bit 6].
124 ret = pci_write_config_byte(chip->pdev, 0xAE, scratch);
131 static int jmicron_probe(struct sdhci_pci_chip *chip)
136 * JMicron chips need a bit of a nudge to enable the power
139 ret = jmicron_pmos(chip, 1);
141 dev_err(&chip->pdev->dev, "Failure enabling card power\n");
148 static int jmicron_resume(struct sdhci_pci_chip *chip)
152 ret = jmicron_pmos(chip, 1);
154 dev_err(&chip->pdev->dev, "Failure enabling card power\n");
161 static const struct sdhci_pci_fixes sdhci_jmicron = {
162 .quirks = SDHCI_QUIRK_32BIT_DMA_ADDR |
163 SDHCI_QUIRK_32BIT_DMA_SIZE |
164 SDHCI_QUIRK_RESET_AFTER_REQUEST,
166 .probe = jmicron_probe,
168 .resume = jmicron_resume,
171 static const struct pci_device_id pci_ids[] __devinitdata = {
173 .vendor = PCI_VENDOR_ID_RICOH,
174 .device = PCI_DEVICE_ID_RICOH_R5C822,
175 .subvendor = PCI_ANY_ID,
176 .subdevice = PCI_ANY_ID,
177 .driver_data = (kernel_ulong_t)&sdhci_ricoh,
181 .vendor = PCI_VENDOR_ID_ENE,
182 .device = PCI_DEVICE_ID_ENE_CB712_SD,
183 .subvendor = PCI_ANY_ID,
184 .subdevice = PCI_ANY_ID,
185 .driver_data = (kernel_ulong_t)&sdhci_ene_712,
189 .vendor = PCI_VENDOR_ID_ENE,
190 .device = PCI_DEVICE_ID_ENE_CB712_SD_2,
191 .subvendor = PCI_ANY_ID,
192 .subdevice = PCI_ANY_ID,
193 .driver_data = (kernel_ulong_t)&sdhci_ene_712,
197 .vendor = PCI_VENDOR_ID_ENE,
198 .device = PCI_DEVICE_ID_ENE_CB714_SD,
199 .subvendor = PCI_ANY_ID,
200 .subdevice = PCI_ANY_ID,
201 .driver_data = (kernel_ulong_t)&sdhci_ene_714,
205 .vendor = PCI_VENDOR_ID_ENE,
206 .device = PCI_DEVICE_ID_ENE_CB714_SD_2,
207 .subvendor = PCI_ANY_ID,
208 .subdevice = PCI_ANY_ID,
209 .driver_data = (kernel_ulong_t)&sdhci_ene_714,
213 .vendor = PCI_VENDOR_ID_MARVELL,
214 .device = PCI_DEVICE_ID_MARVELL_CAFE_SD,
215 .subvendor = PCI_ANY_ID,
216 .subdevice = PCI_ANY_ID,
217 .driver_data = (kernel_ulong_t)&sdhci_cafe,
221 .vendor = PCI_VENDOR_ID_JMICRON,
222 .device = PCI_DEVICE_ID_JMICRON_JMB38X_SD,
223 .subvendor = PCI_ANY_ID,
224 .subdevice = PCI_ANY_ID,
225 .driver_data = (kernel_ulong_t)&sdhci_jmicron,
228 { /* Generic SD host controller */
229 PCI_DEVICE_CLASS((PCI_CLASS_SYSTEM_SDHCI << 8), 0xFFFF00)
232 { /* end: all zeroes */ },
235 MODULE_DEVICE_TABLE(pci, pci_ids);
237 /*****************************************************************************\
239 * SDHCI core callbacks *
241 \*****************************************************************************/
243 static int sdhci_pci_enable_dma(struct sdhci_host *host)
245 struct sdhci_pci_slot *slot;
246 struct pci_dev *pdev;
249 slot = sdhci_priv(host);
250 pdev = slot->chip->pdev;
252 if (((pdev->class & 0xFFFF00) == (PCI_CLASS_SYSTEM_SDHCI << 8)) &&
253 ((pdev->class & 0x0000FF) != PCI_SDHCI_IFDMA) &&
254 (host->flags & SDHCI_USE_DMA)) {
255 dev_warn(&pdev->dev, "Will use DMA mode even though HW "
256 "doesn't fully claim to support it.\n");
259 ret = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
263 pci_set_master(pdev);
268 static struct sdhci_ops sdhci_pci_ops = {
269 .enable_dma = sdhci_pci_enable_dma,
272 /*****************************************************************************\
276 \*****************************************************************************/
280 static int sdhci_pci_suspend (struct pci_dev *pdev, pm_message_t state)
282 struct sdhci_pci_chip *chip;
283 struct sdhci_pci_slot *slot;
286 chip = pci_get_drvdata(pdev);
290 for (i = 0;i < chip->num_slots;i++) {
291 slot = chip->slots[i];
295 ret = sdhci_suspend_host(slot->host, state);
299 sdhci_resume_host(chip->slots[i]->host);
304 pci_save_state(pdev);
305 pci_enable_wake(pdev, pci_choose_state(pdev, state), 0);
306 pci_disable_device(pdev);
307 pci_set_power_state(pdev, pci_choose_state(pdev, state));
312 static int sdhci_pci_resume (struct pci_dev *pdev)
314 struct sdhci_pci_chip *chip;
315 struct sdhci_pci_slot *slot;
318 chip = pci_get_drvdata(pdev);
322 pci_set_power_state(pdev, PCI_D0);
323 pci_restore_state(pdev);
324 ret = pci_enable_device(pdev);
328 if (chip->fixes && chip->fixes->resume) {
329 ret = chip->fixes->resume(chip);
334 for (i = 0;i < chip->num_slots;i++) {
335 slot = chip->slots[i];
339 ret = sdhci_resume_host(slot->host);
347 #else /* CONFIG_PM */
349 #define sdhci_pci_suspend NULL
350 #define sdhci_pci_resume NULL
352 #endif /* CONFIG_PM */
354 /*****************************************************************************\
356 * Device probing/removal *
358 \*****************************************************************************/
360 static struct sdhci_pci_slot * __devinit sdhci_pci_probe_slot(
361 struct pci_dev *pdev, struct sdhci_pci_chip *chip, int bar)
363 struct sdhci_pci_slot *slot;
364 struct sdhci_host *host;
366 resource_size_t addr;
370 if (!(pci_resource_flags(pdev, bar) & IORESOURCE_MEM)) {
371 dev_err(&pdev->dev, "BAR %d is not iomem. Aborting.\n", bar);
372 return ERR_PTR(-ENODEV);
375 if (pci_resource_len(pdev, bar) != 0x100) {
376 dev_err(&pdev->dev, "Invalid iomem size. You may "
377 "experience problems.\n");
380 if ((pdev->class & 0x0000FF) == PCI_SDHCI_IFVENDOR) {
381 dev_err(&pdev->dev, "Vendor specific interface. Aborting.\n");
382 return ERR_PTR(-ENODEV);
385 if ((pdev->class & 0x0000FF) > PCI_SDHCI_IFVENDOR) {
386 dev_err(&pdev->dev, "Unknown interface. Aborting.\n");
387 return ERR_PTR(-ENODEV);
390 host = sdhci_alloc_host(&pdev->dev, sizeof(struct sdhci_pci_slot));
396 slot = sdhci_priv(host);
402 host->hw_name = "PCI";
403 host->ops = &sdhci_pci_ops;
404 host->quirks = chip->quirks;
406 host->irq = pdev->irq;
408 ret = pci_request_region(pdev, bar, mmc_hostname(host->mmc));
410 dev_err(&pdev->dev, "cannot request region\n");
414 addr = pci_resource_start(pdev, bar);
415 host->ioaddr = ioremap_nocache(addr, pci_resource_len(pdev, bar));
417 dev_err(&pdev->dev, "failed to remap registers\n");
421 ret = sdhci_add_host(host);
428 iounmap(host->ioaddr);
431 pci_release_region(pdev, bar);
432 sdhci_free_host(host);
437 static void sdhci_pci_remove_slot(struct sdhci_pci_slot *slot)
439 sdhci_remove_host(slot->host);
440 pci_release_region(slot->chip->pdev, slot->pci_bar);
441 sdhci_free_host(slot->host);
444 static int __devinit sdhci_pci_probe(struct pci_dev *pdev,
445 const struct pci_device_id *ent)
447 struct sdhci_pci_chip *chip;
448 struct sdhci_pci_slot *slot;
450 u8 slots, rev, first_bar;
453 BUG_ON(pdev == NULL);
456 pci_read_config_byte(pdev, PCI_CLASS_REVISION, &rev);
458 dev_info(&pdev->dev, "SDHCI controller found [%04x:%04x] (rev %x)\n",
459 (int)pdev->vendor, (int)pdev->device, (int)rev);
461 ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &slots);
465 slots = PCI_SLOT_INFO_SLOTS(slots) + 1;
466 dev_dbg(&pdev->dev, "found %d slot(s)\n", slots);
470 BUG_ON(slots > MAX_SLOTS);
472 ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &first_bar);
476 first_bar &= PCI_SLOT_INFO_FIRST_BAR_MASK;
479 dev_err(&pdev->dev, "Invalid first BAR. Aborting.\n");
483 ret = pci_enable_device(pdev);
487 chip = kzalloc(sizeof(struct sdhci_pci_chip), GFP_KERNEL);
494 chip->fixes = (const struct sdhci_pci_fixes*)ent->driver_data;
496 chip->quirks = chip->fixes->quirks;
497 chip->num_slots = slots;
499 pci_set_drvdata(pdev, chip);
501 if (chip->fixes && chip->fixes->probe) {
502 ret = chip->fixes->probe(chip);
507 for (i = 0;i < slots;i++) {
508 slot = sdhci_pci_probe_slot(pdev, chip, first_bar + i);
511 sdhci_pci_remove_slot(chip->slots[i]);
516 chip->slots[i] = slot;
522 pci_set_drvdata(pdev, NULL);
526 pci_disable_device(pdev);
530 static void __devexit sdhci_pci_remove(struct pci_dev *pdev)
533 struct sdhci_pci_chip *chip;
535 chip = pci_get_drvdata(pdev);
538 for (i = 0;i < chip->num_slots; i++)
539 sdhci_pci_remove_slot(chip->slots[i]);
541 pci_set_drvdata(pdev, NULL);
545 pci_disable_device(pdev);
548 static struct pci_driver sdhci_driver = {
551 .probe = sdhci_pci_probe,
552 .remove = __devexit_p(sdhci_pci_remove),
553 .suspend = sdhci_pci_suspend,
554 .resume = sdhci_pci_resume,
557 /*****************************************************************************\
561 \*****************************************************************************/
563 static int __init sdhci_drv_init(void)
565 return pci_register_driver(&sdhci_driver);
568 static void __exit sdhci_drv_exit(void)
570 pci_unregister_driver(&sdhci_driver);
573 module_init(sdhci_drv_init);
574 module_exit(sdhci_drv_exit);
576 MODULE_AUTHOR("Pierre Ossman <drzeus@drzeus.cx>");
577 MODULE_DESCRIPTION("Secure Digital Host Controller Interface PCI driver");
578 MODULE_LICENSE("GPL");