GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / mmc / host / sdhci-pci.c
blob580330e78c70527241b69a852899bf2d21728772
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>
19 #include <linux/slab.h>
20 #include <linux/device.h>
22 #include <linux/mmc/host.h>
24 #include <asm/scatterlist.h>
25 #include <asm/io.h>
27 #include "sdhci.h"
30 * PCI registers
33 #define PCI_SDHCI_IFPIO 0x00
34 #define PCI_SDHCI_IFDMA 0x01
35 #define PCI_SDHCI_IFVENDOR 0x02
37 #define PCI_SLOT_INFO 0x40 /* 8 bits */
38 #define PCI_SLOT_INFO_SLOTS(x) ((x >> 4) & 7)
39 #define PCI_SLOT_INFO_FIRST_BAR_MASK 0x07
41 #define MAX_SLOTS 8
43 struct sdhci_pci_chip;
44 struct sdhci_pci_slot;
46 struct sdhci_pci_fixes {
47 unsigned int quirks;
49 int (*probe)(struct sdhci_pci_chip*);
51 int (*probe_slot)(struct sdhci_pci_slot*);
52 void (*remove_slot)(struct sdhci_pci_slot*, int);
54 int (*suspend)(struct sdhci_pci_chip*,
55 pm_message_t);
56 int (*resume)(struct sdhci_pci_chip*);
59 struct sdhci_pci_slot {
60 struct sdhci_pci_chip *chip;
61 struct sdhci_host *host;
63 int pci_bar;
66 struct sdhci_pci_chip {
67 struct pci_dev *pdev;
69 unsigned int quirks;
70 const struct sdhci_pci_fixes *fixes;
72 int num_slots; /* Slots on controller */
73 struct sdhci_pci_slot *slots[MAX_SLOTS]; /* Pointers to host slots */
77 /*****************************************************************************\
78 * *
79 * Hardware specific quirk handling *
80 * *
81 \*****************************************************************************/
83 static int ricoh_probe(struct sdhci_pci_chip *chip)
85 if (chip->pdev->subsystem_vendor == PCI_VENDOR_ID_SAMSUNG ||
86 chip->pdev->subsystem_vendor == PCI_VENDOR_ID_SONY)
87 chip->quirks |= SDHCI_QUIRK_NO_CARD_NO_RESET;
88 return 0;
91 static int ricoh_mmc_probe_slot(struct sdhci_pci_slot *slot)
93 slot->host->caps =
94 ((0x21 << SDHCI_TIMEOUT_CLK_SHIFT)
95 & SDHCI_TIMEOUT_CLK_MASK) |
97 ((0x21 << SDHCI_CLOCK_BASE_SHIFT)
98 & SDHCI_CLOCK_BASE_MASK) |
100 SDHCI_TIMEOUT_CLK_UNIT |
101 SDHCI_CAN_VDD_330 |
102 SDHCI_CAN_DO_SDMA;
103 return 0;
106 static int ricoh_mmc_resume(struct sdhci_pci_chip *chip)
108 /* Apply a delay to allow controller to settle */
109 /* Otherwise it becomes confused if card state changed
110 during suspend */
111 msleep(500);
112 return 0;
115 static const struct sdhci_pci_fixes sdhci_ricoh = {
116 .probe = ricoh_probe,
117 .quirks = SDHCI_QUIRK_32BIT_DMA_ADDR |
118 SDHCI_QUIRK_FORCE_DMA |
119 SDHCI_QUIRK_CLOCK_BEFORE_RESET,
122 static const struct sdhci_pci_fixes sdhci_ricoh_mmc = {
123 .probe_slot = ricoh_mmc_probe_slot,
124 .resume = ricoh_mmc_resume,
125 .quirks = SDHCI_QUIRK_32BIT_DMA_ADDR |
126 SDHCI_QUIRK_CLOCK_BEFORE_RESET |
127 SDHCI_QUIRK_NO_CARD_NO_RESET |
128 SDHCI_QUIRK_MISSING_CAPS
131 static const struct sdhci_pci_fixes sdhci_ene_712 = {
132 .quirks = SDHCI_QUIRK_SINGLE_POWER_WRITE |
133 SDHCI_QUIRK_BROKEN_DMA,
136 static const struct sdhci_pci_fixes sdhci_ene_714 = {
137 .quirks = SDHCI_QUIRK_SINGLE_POWER_WRITE |
138 SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS |
139 SDHCI_QUIRK_BROKEN_DMA,
142 static const struct sdhci_pci_fixes sdhci_cafe = {
143 .quirks = SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER |
144 SDHCI_QUIRK_NO_BUSY_IRQ |
145 SDHCI_QUIRK_BROKEN_TIMEOUT_VAL,
148 static int jmicron_pmos(struct sdhci_pci_chip *chip, int on)
150 u8 scratch;
151 int ret;
153 ret = pci_read_config_byte(chip->pdev, 0xAE, &scratch);
154 if (ret)
155 return ret;
158 * Turn PMOS on [bit 0], set over current detection to 2.4 V
159 * [bit 1:2] and enable over current debouncing [bit 6].
161 if (on)
162 scratch |= 0x47;
163 else
164 scratch &= ~0x47;
166 ret = pci_write_config_byte(chip->pdev, 0xAE, scratch);
167 if (ret)
168 return ret;
170 return 0;
173 static int jmicron_probe(struct sdhci_pci_chip *chip)
175 int ret;
177 if (chip->pdev->revision == 0) {
178 chip->quirks |= SDHCI_QUIRK_32BIT_DMA_ADDR |
179 SDHCI_QUIRK_32BIT_DMA_SIZE |
180 SDHCI_QUIRK_32BIT_ADMA_SIZE |
181 SDHCI_QUIRK_RESET_AFTER_REQUEST |
182 SDHCI_QUIRK_BROKEN_SMALL_PIO;
185 if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_SD) {
186 struct pci_dev *sd_dev;
188 sd_dev = NULL;
189 while ((sd_dev = pci_get_device(PCI_VENDOR_ID_JMICRON,
190 PCI_DEVICE_ID_JMICRON_JMB38X_MMC, sd_dev)) != NULL) {
191 if ((PCI_SLOT(chip->pdev->devfn) ==
192 PCI_SLOT(sd_dev->devfn)) &&
193 (chip->pdev->bus == sd_dev->bus))
194 break;
197 if (sd_dev) {
198 pci_dev_put(sd_dev);
199 dev_info(&chip->pdev->dev, "Refusing to bind to "
200 "secondary interface.\n");
201 return -ENODEV;
206 * JMicron chips need a bit of a nudge to enable the power
207 * output pins.
209 ret = jmicron_pmos(chip, 1);
210 if (ret) {
211 dev_err(&chip->pdev->dev, "Failure enabling card power\n");
212 return ret;
215 return 0;
218 static void jmicron_enable_mmc(struct sdhci_host *host, int on)
220 u8 scratch;
222 scratch = readb(host->ioaddr + 0xC0);
224 if (on)
225 scratch |= 0x01;
226 else
227 scratch &= ~0x01;
229 writeb(scratch, host->ioaddr + 0xC0);
232 static int jmicron_probe_slot(struct sdhci_pci_slot *slot)
234 if (slot->chip->pdev->revision == 0) {
235 u16 version;
237 version = readl(slot->host->ioaddr + SDHCI_HOST_VERSION);
238 version = (version & SDHCI_VENDOR_VER_MASK) >>
239 SDHCI_VENDOR_VER_SHIFT;
242 * Older versions of the chip have lots of nasty glitches
243 * in the ADMA engine. It's best just to avoid it
244 * completely.
246 if (version < 0xAC)
247 slot->host->quirks |= SDHCI_QUIRK_BROKEN_ADMA;
251 * The secondary interface requires a bit set to get the
252 * interrupts.
254 if (slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC)
255 jmicron_enable_mmc(slot->host, 1);
257 return 0;
260 static void jmicron_remove_slot(struct sdhci_pci_slot *slot, int dead)
262 if (dead)
263 return;
265 if (slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC)
266 jmicron_enable_mmc(slot->host, 0);
269 static int jmicron_suspend(struct sdhci_pci_chip *chip, pm_message_t state)
271 int i;
273 if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC) {
274 for (i = 0;i < chip->num_slots;i++)
275 jmicron_enable_mmc(chip->slots[i]->host, 0);
278 return 0;
281 static int jmicron_resume(struct sdhci_pci_chip *chip)
283 int ret, i;
285 if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC) {
286 for (i = 0;i < chip->num_slots;i++)
287 jmicron_enable_mmc(chip->slots[i]->host, 1);
290 ret = jmicron_pmos(chip, 1);
291 if (ret) {
292 dev_err(&chip->pdev->dev, "Failure enabling card power\n");
293 return ret;
296 return 0;
299 static const struct sdhci_pci_fixes sdhci_jmicron = {
300 .probe = jmicron_probe,
302 .probe_slot = jmicron_probe_slot,
303 .remove_slot = jmicron_remove_slot,
305 .suspend = jmicron_suspend,
306 .resume = jmicron_resume,
309 /* SysKonnect CardBus2SDIO extra registers */
310 #define SYSKT_CTRL 0x200
311 #define SYSKT_RDFIFO_STAT 0x204
312 #define SYSKT_WRFIFO_STAT 0x208
313 #define SYSKT_POWER_DATA 0x20c
314 #define SYSKT_POWER_330 0xef
315 #define SYSKT_POWER_300 0xf8
316 #define SYSKT_POWER_184 0xcc
317 #define SYSKT_POWER_CMD 0x20d
318 #define SYSKT_POWER_START (1 << 7)
319 #define SYSKT_POWER_STATUS 0x20e
320 #define SYSKT_POWER_STATUS_OK (1 << 0)
321 #define SYSKT_BOARD_REV 0x210
322 #define SYSKT_CHIP_REV 0x211
323 #define SYSKT_CONF_DATA 0x212
324 #define SYSKT_CONF_DATA_1V8 (1 << 2)
325 #define SYSKT_CONF_DATA_2V5 (1 << 1)
326 #define SYSKT_CONF_DATA_3V3 (1 << 0)
328 static int syskt_probe(struct sdhci_pci_chip *chip)
330 if ((chip->pdev->class & 0x0000FF) == PCI_SDHCI_IFVENDOR) {
331 chip->pdev->class &= ~0x0000FF;
332 chip->pdev->class |= PCI_SDHCI_IFDMA;
334 return 0;
337 static int syskt_probe_slot(struct sdhci_pci_slot *slot)
339 int tm, ps;
341 u8 board_rev = readb(slot->host->ioaddr + SYSKT_BOARD_REV);
342 u8 chip_rev = readb(slot->host->ioaddr + SYSKT_CHIP_REV);
343 dev_info(&slot->chip->pdev->dev, "SysKonnect CardBus2SDIO, "
344 "board rev %d.%d, chip rev %d.%d\n",
345 board_rev >> 4, board_rev & 0xf,
346 chip_rev >> 4, chip_rev & 0xf);
347 if (chip_rev >= 0x20)
348 slot->host->quirks |= SDHCI_QUIRK_FORCE_DMA;
350 writeb(SYSKT_POWER_330, slot->host->ioaddr + SYSKT_POWER_DATA);
351 writeb(SYSKT_POWER_START, slot->host->ioaddr + SYSKT_POWER_CMD);
352 udelay(50);
353 tm = 10; /* Wait max 1 ms */
354 do {
355 ps = readw(slot->host->ioaddr + SYSKT_POWER_STATUS);
356 if (ps & SYSKT_POWER_STATUS_OK)
357 break;
358 udelay(100);
359 } while (--tm);
360 if (!tm) {
361 dev_err(&slot->chip->pdev->dev,
362 "power regulator never stabilized");
363 writeb(0, slot->host->ioaddr + SYSKT_POWER_CMD);
364 return -ENODEV;
367 return 0;
370 static const struct sdhci_pci_fixes sdhci_syskt = {
371 .quirks = SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER,
372 .probe = syskt_probe,
373 .probe_slot = syskt_probe_slot,
376 static int via_probe(struct sdhci_pci_chip *chip)
378 if (chip->pdev->revision == 0x10)
379 chip->quirks |= SDHCI_QUIRK_DELAY_AFTER_POWER;
381 return 0;
384 static const struct sdhci_pci_fixes sdhci_via = {
385 .probe = via_probe,
388 static const struct pci_device_id pci_ids[] __devinitdata = {
390 .vendor = PCI_VENDOR_ID_RICOH,
391 .device = PCI_DEVICE_ID_RICOH_R5C822,
392 .subvendor = PCI_ANY_ID,
393 .subdevice = PCI_ANY_ID,
394 .driver_data = (kernel_ulong_t)&sdhci_ricoh,
398 .vendor = PCI_VENDOR_ID_RICOH,
399 .device = 0x843,
400 .subvendor = PCI_ANY_ID,
401 .subdevice = PCI_ANY_ID,
402 .driver_data = (kernel_ulong_t)&sdhci_ricoh_mmc,
406 .vendor = PCI_VENDOR_ID_RICOH,
407 .device = 0xe822,
408 .subvendor = PCI_ANY_ID,
409 .subdevice = PCI_ANY_ID,
410 .driver_data = (kernel_ulong_t)&sdhci_ricoh_mmc,
414 .vendor = PCI_VENDOR_ID_ENE,
415 .device = PCI_DEVICE_ID_ENE_CB712_SD,
416 .subvendor = PCI_ANY_ID,
417 .subdevice = PCI_ANY_ID,
418 .driver_data = (kernel_ulong_t)&sdhci_ene_712,
422 .vendor = PCI_VENDOR_ID_ENE,
423 .device = PCI_DEVICE_ID_ENE_CB712_SD_2,
424 .subvendor = PCI_ANY_ID,
425 .subdevice = PCI_ANY_ID,
426 .driver_data = (kernel_ulong_t)&sdhci_ene_712,
430 .vendor = PCI_VENDOR_ID_ENE,
431 .device = PCI_DEVICE_ID_ENE_CB714_SD,
432 .subvendor = PCI_ANY_ID,
433 .subdevice = PCI_ANY_ID,
434 .driver_data = (kernel_ulong_t)&sdhci_ene_714,
438 .vendor = PCI_VENDOR_ID_ENE,
439 .device = PCI_DEVICE_ID_ENE_CB714_SD_2,
440 .subvendor = PCI_ANY_ID,
441 .subdevice = PCI_ANY_ID,
442 .driver_data = (kernel_ulong_t)&sdhci_ene_714,
446 .vendor = PCI_VENDOR_ID_MARVELL,
447 .device = PCI_DEVICE_ID_MARVELL_88ALP01_SD,
448 .subvendor = PCI_ANY_ID,
449 .subdevice = PCI_ANY_ID,
450 .driver_data = (kernel_ulong_t)&sdhci_cafe,
454 .vendor = PCI_VENDOR_ID_JMICRON,
455 .device = PCI_DEVICE_ID_JMICRON_JMB38X_SD,
456 .subvendor = PCI_ANY_ID,
457 .subdevice = PCI_ANY_ID,
458 .driver_data = (kernel_ulong_t)&sdhci_jmicron,
462 .vendor = PCI_VENDOR_ID_JMICRON,
463 .device = PCI_DEVICE_ID_JMICRON_JMB38X_MMC,
464 .subvendor = PCI_ANY_ID,
465 .subdevice = PCI_ANY_ID,
466 .driver_data = (kernel_ulong_t)&sdhci_jmicron,
470 .vendor = PCI_VENDOR_ID_SYSKONNECT,
471 .device = 0x8000,
472 .subvendor = PCI_ANY_ID,
473 .subdevice = PCI_ANY_ID,
474 .driver_data = (kernel_ulong_t)&sdhci_syskt,
478 .vendor = PCI_VENDOR_ID_VIA,
479 .device = 0x95d0,
480 .subvendor = PCI_ANY_ID,
481 .subdevice = PCI_ANY_ID,
482 .driver_data = (kernel_ulong_t)&sdhci_via,
485 { /* Generic SD host controller */
486 PCI_DEVICE_CLASS((PCI_CLASS_SYSTEM_SDHCI << 8), 0xFFFF00)
489 { /* end: all zeroes */ },
492 MODULE_DEVICE_TABLE(pci, pci_ids);
494 /*****************************************************************************\
496 * SDHCI core callbacks *
498 \*****************************************************************************/
500 static int sdhci_pci_enable_dma(struct sdhci_host *host)
502 struct sdhci_pci_slot *slot;
503 struct pci_dev *pdev;
504 int ret;
506 slot = sdhci_priv(host);
507 pdev = slot->chip->pdev;
509 if (((pdev->class & 0xFFFF00) == (PCI_CLASS_SYSTEM_SDHCI << 8)) &&
510 ((pdev->class & 0x0000FF) != PCI_SDHCI_IFDMA) &&
511 (host->flags & SDHCI_USE_SDMA)) {
512 dev_warn(&pdev->dev, "Will use DMA mode even though HW "
513 "doesn't fully claim to support it.\n");
516 ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
517 if (ret)
518 return ret;
520 pci_set_master(pdev);
522 return 0;
525 static struct sdhci_ops sdhci_pci_ops = {
526 .enable_dma = sdhci_pci_enable_dma,
529 /*****************************************************************************\
531 * Suspend/resume *
533 \*****************************************************************************/
535 #ifdef CONFIG_PM
537 static int sdhci_pci_suspend (struct pci_dev *pdev, pm_message_t state)
539 struct sdhci_pci_chip *chip;
540 struct sdhci_pci_slot *slot;
541 mmc_pm_flag_t pm_flags = 0;
542 int i, ret;
544 chip = pci_get_drvdata(pdev);
545 if (!chip)
546 return 0;
548 for (i = 0;i < chip->num_slots;i++) {
549 slot = chip->slots[i];
550 if (!slot)
551 continue;
553 ret = sdhci_suspend_host(slot->host, state);
555 if (ret) {
556 for (i--;i >= 0;i--)
557 sdhci_resume_host(chip->slots[i]->host);
558 return ret;
561 pm_flags |= slot->host->mmc->pm_flags;
564 if (chip->fixes && chip->fixes->suspend) {
565 ret = chip->fixes->suspend(chip, state);
566 if (ret) {
567 for (i = chip->num_slots - 1;i >= 0;i--)
568 sdhci_resume_host(chip->slots[i]->host);
569 return ret;
573 pci_save_state(pdev);
574 if (pm_flags & MMC_PM_KEEP_POWER) {
575 if (pm_flags & MMC_PM_WAKE_SDIO_IRQ)
576 pci_enable_wake(pdev, PCI_D3hot, 1);
577 pci_set_power_state(pdev, PCI_D3hot);
578 } else {
579 pci_enable_wake(pdev, pci_choose_state(pdev, state), 0);
580 pci_disable_device(pdev);
581 pci_set_power_state(pdev, pci_choose_state(pdev, state));
584 return 0;
587 static int sdhci_pci_resume (struct pci_dev *pdev)
589 struct sdhci_pci_chip *chip;
590 struct sdhci_pci_slot *slot;
591 int i, ret;
593 chip = pci_get_drvdata(pdev);
594 if (!chip)
595 return 0;
597 pci_set_power_state(pdev, PCI_D0);
598 pci_restore_state(pdev);
599 ret = pci_enable_device(pdev);
600 if (ret)
601 return ret;
603 if (chip->fixes && chip->fixes->resume) {
604 ret = chip->fixes->resume(chip);
605 if (ret)
606 return ret;
609 for (i = 0;i < chip->num_slots;i++) {
610 slot = chip->slots[i];
611 if (!slot)
612 continue;
614 ret = sdhci_resume_host(slot->host);
615 if (ret)
616 return ret;
619 return 0;
622 #else /* CONFIG_PM */
624 #define sdhci_pci_suspend NULL
625 #define sdhci_pci_resume NULL
627 #endif /* CONFIG_PM */
629 /*****************************************************************************\
631 * Device probing/removal *
633 \*****************************************************************************/
635 static struct sdhci_pci_slot * __devinit sdhci_pci_probe_slot(
636 struct pci_dev *pdev, struct sdhci_pci_chip *chip, int bar)
638 struct sdhci_pci_slot *slot;
639 struct sdhci_host *host;
641 resource_size_t addr;
643 int ret;
645 if (!(pci_resource_flags(pdev, bar) & IORESOURCE_MEM)) {
646 dev_err(&pdev->dev, "BAR %d is not iomem. Aborting.\n", bar);
647 return ERR_PTR(-ENODEV);
650 if (pci_resource_len(pdev, bar) != 0x100) {
651 dev_err(&pdev->dev, "Invalid iomem size. You may "
652 "experience problems.\n");
655 if ((pdev->class & 0x0000FF) == PCI_SDHCI_IFVENDOR) {
656 dev_err(&pdev->dev, "Vendor specific interface. Aborting.\n");
657 return ERR_PTR(-ENODEV);
660 if ((pdev->class & 0x0000FF) > PCI_SDHCI_IFVENDOR) {
661 dev_err(&pdev->dev, "Unknown interface. Aborting.\n");
662 return ERR_PTR(-ENODEV);
665 host = sdhci_alloc_host(&pdev->dev, sizeof(struct sdhci_pci_slot));
666 if (IS_ERR(host)) {
667 dev_err(&pdev->dev, "cannot allocate host\n");
668 return ERR_CAST(host);
671 slot = sdhci_priv(host);
673 slot->chip = chip;
674 slot->host = host;
675 slot->pci_bar = bar;
677 host->hw_name = "PCI";
678 host->ops = &sdhci_pci_ops;
679 host->quirks = chip->quirks;
681 host->irq = pdev->irq;
683 ret = pci_request_region(pdev, bar, mmc_hostname(host->mmc));
684 if (ret) {
685 dev_err(&pdev->dev, "cannot request region\n");
686 goto free;
689 addr = pci_resource_start(pdev, bar);
690 host->ioaddr = pci_ioremap_bar(pdev, bar);
691 if (!host->ioaddr) {
692 dev_err(&pdev->dev, "failed to remap registers\n");
693 goto release;
696 if (chip->fixes && chip->fixes->probe_slot) {
697 ret = chip->fixes->probe_slot(slot);
698 if (ret)
699 goto unmap;
702 host->mmc->pm_caps = MMC_PM_KEEP_POWER | MMC_PM_WAKE_SDIO_IRQ;
704 ret = sdhci_add_host(host);
705 if (ret)
706 goto remove;
708 return slot;
710 remove:
711 if (chip->fixes && chip->fixes->remove_slot)
712 chip->fixes->remove_slot(slot, 0);
714 unmap:
715 iounmap(host->ioaddr);
717 release:
718 pci_release_region(pdev, bar);
720 free:
721 sdhci_free_host(host);
723 return ERR_PTR(ret);
726 static void sdhci_pci_remove_slot(struct sdhci_pci_slot *slot)
728 int dead;
729 u32 scratch;
731 dead = 0;
732 scratch = readl(slot->host->ioaddr + SDHCI_INT_STATUS);
733 if (scratch == (u32)-1)
734 dead = 1;
736 sdhci_remove_host(slot->host, dead);
738 if (slot->chip->fixes && slot->chip->fixes->remove_slot)
739 slot->chip->fixes->remove_slot(slot, dead);
741 pci_release_region(slot->chip->pdev, slot->pci_bar);
743 sdhci_free_host(slot->host);
746 static int __devinit sdhci_pci_probe(struct pci_dev *pdev,
747 const struct pci_device_id *ent)
749 struct sdhci_pci_chip *chip;
750 struct sdhci_pci_slot *slot;
752 u8 slots, rev, first_bar;
753 int ret, i;
755 BUG_ON(pdev == NULL);
756 BUG_ON(ent == NULL);
758 pci_read_config_byte(pdev, PCI_CLASS_REVISION, &rev);
760 dev_info(&pdev->dev, "SDHCI controller found [%04x:%04x] (rev %x)\n",
761 (int)pdev->vendor, (int)pdev->device, (int)rev);
763 ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &slots);
764 if (ret)
765 return ret;
767 slots = PCI_SLOT_INFO_SLOTS(slots) + 1;
768 dev_dbg(&pdev->dev, "found %d slot(s)\n", slots);
769 if (slots == 0)
770 return -ENODEV;
772 BUG_ON(slots > MAX_SLOTS);
774 ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &first_bar);
775 if (ret)
776 return ret;
778 first_bar &= PCI_SLOT_INFO_FIRST_BAR_MASK;
780 if (first_bar > 5) {
781 dev_err(&pdev->dev, "Invalid first BAR. Aborting.\n");
782 return -ENODEV;
785 ret = pci_enable_device(pdev);
786 if (ret)
787 return ret;
789 chip = kzalloc(sizeof(struct sdhci_pci_chip), GFP_KERNEL);
790 if (!chip) {
791 ret = -ENOMEM;
792 goto err;
795 chip->pdev = pdev;
796 chip->fixes = (const struct sdhci_pci_fixes*)ent->driver_data;
797 if (chip->fixes)
798 chip->quirks = chip->fixes->quirks;
799 chip->num_slots = slots;
801 pci_set_drvdata(pdev, chip);
803 if (chip->fixes && chip->fixes->probe) {
804 ret = chip->fixes->probe(chip);
805 if (ret)
806 goto free;
809 for (i = 0;i < slots;i++) {
810 slot = sdhci_pci_probe_slot(pdev, chip, first_bar + i);
811 if (IS_ERR(slot)) {
812 for (i--;i >= 0;i--)
813 sdhci_pci_remove_slot(chip->slots[i]);
814 ret = PTR_ERR(slot);
815 goto free;
818 chip->slots[i] = slot;
821 return 0;
823 free:
824 pci_set_drvdata(pdev, NULL);
825 kfree(chip);
827 err:
828 pci_disable_device(pdev);
829 return ret;
832 static void __devexit sdhci_pci_remove(struct pci_dev *pdev)
834 int i;
835 struct sdhci_pci_chip *chip;
837 chip = pci_get_drvdata(pdev);
839 if (chip) {
840 for (i = 0;i < chip->num_slots; i++)
841 sdhci_pci_remove_slot(chip->slots[i]);
843 pci_set_drvdata(pdev, NULL);
844 kfree(chip);
847 pci_disable_device(pdev);
850 static struct pci_driver sdhci_driver = {
851 .name = "sdhci-pci",
852 .id_table = pci_ids,
853 .probe = sdhci_pci_probe,
854 .remove = __devexit_p(sdhci_pci_remove),
855 .suspend = sdhci_pci_suspend,
856 .resume = sdhci_pci_resume,
859 /*****************************************************************************\
861 * Driver init/exit *
863 \*****************************************************************************/
865 static int __init sdhci_drv_init(void)
867 return pci_register_driver(&sdhci_driver);
870 static void __exit sdhci_drv_exit(void)
872 pci_unregister_driver(&sdhci_driver);
875 module_init(sdhci_drv_init);
876 module_exit(sdhci_drv_exit);
878 MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
879 MODULE_DESCRIPTION("Secure Digital Host Controller Interface PCI driver");
880 MODULE_LICENSE("GPL");