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
->pdev
->subsystem_vendor
== PCI_VENDOR_ID_SONY
)
88 chip
->quirks
|= SDHCI_QUIRK_NO_CARD_NO_RESET
;
93 static const struct sdhci_pci_fixes sdhci_ricoh
= {
95 .quirks
= SDHCI_QUIRK_32BIT_DMA_ADDR
,
98 static const struct sdhci_pci_fixes sdhci_ene_712
= {
99 .quirks
= SDHCI_QUIRK_SINGLE_POWER_WRITE
|
100 SDHCI_QUIRK_BROKEN_DMA
,
103 static const struct sdhci_pci_fixes sdhci_ene_714
= {
104 .quirks
= SDHCI_QUIRK_SINGLE_POWER_WRITE
|
105 SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS
|
106 SDHCI_QUIRK_BROKEN_DMA
,
109 static const struct sdhci_pci_fixes sdhci_cafe
= {
110 .quirks
= SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER
|
111 SDHCI_QUIRK_NO_BUSY_IRQ
|
112 SDHCI_QUIRK_BROKEN_TIMEOUT_VAL
,
115 static int jmicron_pmos(struct sdhci_pci_chip
*chip
, int on
)
120 ret
= pci_read_config_byte(chip
->pdev
, 0xAE, &scratch
);
125 * Turn PMOS on [bit 0], set over current detection to 2.4 V
126 * [bit 1:2] and enable over current debouncing [bit 6].
133 ret
= pci_write_config_byte(chip
->pdev
, 0xAE, scratch
);
140 static int jmicron_probe(struct sdhci_pci_chip
*chip
)
144 if (chip
->pdev
->revision
== 0) {
145 chip
->quirks
|= SDHCI_QUIRK_32BIT_DMA_ADDR
|
146 SDHCI_QUIRK_32BIT_DMA_SIZE
|
147 SDHCI_QUIRK_32BIT_ADMA_SIZE
|
148 SDHCI_QUIRK_RESET_AFTER_REQUEST
|
149 SDHCI_QUIRK_BROKEN_SMALL_PIO
;
153 * JMicron chips can have two interfaces to the same hardware
154 * in order to work around limitations in Microsoft's driver.
155 * We need to make sure we only bind to one of them.
157 * This code assumes two things:
159 * 1. The PCI code adds subfunctions in order.
161 * 2. The MMC interface has a lower subfunction number
162 * than the SD interface.
164 if (chip
->pdev
->device
== PCI_DEVICE_ID_JMICRON_JMB38X_SD
) {
165 struct pci_dev
*sd_dev
;
168 while ((sd_dev
= pci_get_device(PCI_VENDOR_ID_JMICRON
,
169 PCI_DEVICE_ID_JMICRON_JMB38X_MMC
, sd_dev
)) != NULL
) {
170 if ((PCI_SLOT(chip
->pdev
->devfn
) ==
171 PCI_SLOT(sd_dev
->devfn
)) &&
172 (chip
->pdev
->bus
== sd_dev
->bus
))
178 dev_info(&chip
->pdev
->dev
, "Refusing to bind to "
179 "secondary interface.\n");
185 * JMicron chips need a bit of a nudge to enable the power
188 ret
= jmicron_pmos(chip
, 1);
190 dev_err(&chip
->pdev
->dev
, "Failure enabling card power\n");
197 static void jmicron_enable_mmc(struct sdhci_host
*host
, int on
)
201 scratch
= readb(host
->ioaddr
+ 0xC0);
208 writeb(scratch
, host
->ioaddr
+ 0xC0);
211 static int jmicron_probe_slot(struct sdhci_pci_slot
*slot
)
213 if (slot
->chip
->pdev
->revision
== 0) {
216 version
= readl(slot
->host
->ioaddr
+ SDHCI_HOST_VERSION
);
217 version
= (version
& SDHCI_VENDOR_VER_MASK
) >>
218 SDHCI_VENDOR_VER_SHIFT
;
221 * Older versions of the chip have lots of nasty glitches
222 * in the ADMA engine. It's best just to avoid it
226 slot
->host
->quirks
|= SDHCI_QUIRK_BROKEN_ADMA
;
230 * The secondary interface requires a bit set to get the
233 if (slot
->chip
->pdev
->device
== PCI_DEVICE_ID_JMICRON_JMB38X_MMC
)
234 jmicron_enable_mmc(slot
->host
, 1);
239 static void jmicron_remove_slot(struct sdhci_pci_slot
*slot
, int dead
)
244 if (slot
->chip
->pdev
->device
== PCI_DEVICE_ID_JMICRON_JMB38X_MMC
)
245 jmicron_enable_mmc(slot
->host
, 0);
248 static int jmicron_suspend(struct sdhci_pci_chip
*chip
, pm_message_t state
)
252 if (chip
->pdev
->device
== PCI_DEVICE_ID_JMICRON_JMB38X_MMC
) {
253 for (i
= 0;i
< chip
->num_slots
;i
++)
254 jmicron_enable_mmc(chip
->slots
[i
]->host
, 0);
260 static int jmicron_resume(struct sdhci_pci_chip
*chip
)
264 if (chip
->pdev
->device
== PCI_DEVICE_ID_JMICRON_JMB38X_MMC
) {
265 for (i
= 0;i
< chip
->num_slots
;i
++)
266 jmicron_enable_mmc(chip
->slots
[i
]->host
, 1);
269 ret
= jmicron_pmos(chip
, 1);
271 dev_err(&chip
->pdev
->dev
, "Failure enabling card power\n");
278 static const struct sdhci_pci_fixes sdhci_jmicron
= {
279 .probe
= jmicron_probe
,
281 .probe_slot
= jmicron_probe_slot
,
282 .remove_slot
= jmicron_remove_slot
,
284 .suspend
= jmicron_suspend
,
285 .resume
= jmicron_resume
,
288 static int via_probe(struct sdhci_pci_chip
*chip
)
290 if (chip
->pdev
->revision
== 0x10)
291 chip
->quirks
|= SDHCI_QUIRK_DELAY_AFTER_POWER
;
296 static const struct sdhci_pci_fixes sdhci_via
= {
300 static const struct pci_device_id pci_ids
[] __devinitdata
= {
302 .vendor
= PCI_VENDOR_ID_RICOH
,
303 .device
= PCI_DEVICE_ID_RICOH_R5C822
,
304 .subvendor
= PCI_ANY_ID
,
305 .subdevice
= PCI_ANY_ID
,
306 .driver_data
= (kernel_ulong_t
)&sdhci_ricoh
,
310 .vendor
= PCI_VENDOR_ID_ENE
,
311 .device
= PCI_DEVICE_ID_ENE_CB712_SD
,
312 .subvendor
= PCI_ANY_ID
,
313 .subdevice
= PCI_ANY_ID
,
314 .driver_data
= (kernel_ulong_t
)&sdhci_ene_712
,
318 .vendor
= PCI_VENDOR_ID_ENE
,
319 .device
= PCI_DEVICE_ID_ENE_CB712_SD_2
,
320 .subvendor
= PCI_ANY_ID
,
321 .subdevice
= PCI_ANY_ID
,
322 .driver_data
= (kernel_ulong_t
)&sdhci_ene_712
,
326 .vendor
= PCI_VENDOR_ID_ENE
,
327 .device
= PCI_DEVICE_ID_ENE_CB714_SD
,
328 .subvendor
= PCI_ANY_ID
,
329 .subdevice
= PCI_ANY_ID
,
330 .driver_data
= (kernel_ulong_t
)&sdhci_ene_714
,
334 .vendor
= PCI_VENDOR_ID_ENE
,
335 .device
= PCI_DEVICE_ID_ENE_CB714_SD_2
,
336 .subvendor
= PCI_ANY_ID
,
337 .subdevice
= PCI_ANY_ID
,
338 .driver_data
= (kernel_ulong_t
)&sdhci_ene_714
,
342 .vendor
= PCI_VENDOR_ID_MARVELL
,
343 .device
= PCI_DEVICE_ID_MARVELL_88ALP01_SD
,
344 .subvendor
= PCI_ANY_ID
,
345 .subdevice
= PCI_ANY_ID
,
346 .driver_data
= (kernel_ulong_t
)&sdhci_cafe
,
350 .vendor
= PCI_VENDOR_ID_JMICRON
,
351 .device
= PCI_DEVICE_ID_JMICRON_JMB38X_SD
,
352 .subvendor
= PCI_ANY_ID
,
353 .subdevice
= PCI_ANY_ID
,
354 .driver_data
= (kernel_ulong_t
)&sdhci_jmicron
,
358 .vendor
= PCI_VENDOR_ID_JMICRON
,
359 .device
= PCI_DEVICE_ID_JMICRON_JMB38X_MMC
,
360 .subvendor
= PCI_ANY_ID
,
361 .subdevice
= PCI_ANY_ID
,
362 .driver_data
= (kernel_ulong_t
)&sdhci_jmicron
,
366 .vendor
= PCI_VENDOR_ID_VIA
,
368 .subvendor
= PCI_ANY_ID
,
369 .subdevice
= PCI_ANY_ID
,
370 .driver_data
= (kernel_ulong_t
)&sdhci_via
,
373 { /* Generic SD host controller */
374 PCI_DEVICE_CLASS((PCI_CLASS_SYSTEM_SDHCI
<< 8), 0xFFFF00)
377 { /* end: all zeroes */ },
380 MODULE_DEVICE_TABLE(pci
, pci_ids
);
382 /*****************************************************************************\
384 * SDHCI core callbacks *
386 \*****************************************************************************/
388 static int sdhci_pci_enable_dma(struct sdhci_host
*host
)
390 struct sdhci_pci_slot
*slot
;
391 struct pci_dev
*pdev
;
394 slot
= sdhci_priv(host
);
395 pdev
= slot
->chip
->pdev
;
397 if (((pdev
->class & 0xFFFF00) == (PCI_CLASS_SYSTEM_SDHCI
<< 8)) &&
398 ((pdev
->class & 0x0000FF) != PCI_SDHCI_IFDMA
) &&
399 (host
->flags
& SDHCI_USE_SDMA
)) {
400 dev_warn(&pdev
->dev
, "Will use DMA mode even though HW "
401 "doesn't fully claim to support it.\n");
404 ret
= pci_set_dma_mask(pdev
, DMA_BIT_MASK(32));
408 pci_set_master(pdev
);
413 static struct sdhci_ops sdhci_pci_ops
= {
414 .enable_dma
= sdhci_pci_enable_dma
,
417 /*****************************************************************************\
421 \*****************************************************************************/
425 static int sdhci_pci_suspend (struct pci_dev
*pdev
, pm_message_t state
)
427 struct sdhci_pci_chip
*chip
;
428 struct sdhci_pci_slot
*slot
;
431 chip
= pci_get_drvdata(pdev
);
435 for (i
= 0;i
< chip
->num_slots
;i
++) {
436 slot
= chip
->slots
[i
];
440 ret
= sdhci_suspend_host(slot
->host
, state
);
444 sdhci_resume_host(chip
->slots
[i
]->host
);
449 if (chip
->fixes
&& chip
->fixes
->suspend
) {
450 ret
= chip
->fixes
->suspend(chip
, state
);
452 for (i
= chip
->num_slots
- 1;i
>= 0;i
--)
453 sdhci_resume_host(chip
->slots
[i
]->host
);
458 pci_save_state(pdev
);
459 pci_enable_wake(pdev
, pci_choose_state(pdev
, state
), 0);
460 pci_disable_device(pdev
);
461 pci_set_power_state(pdev
, pci_choose_state(pdev
, state
));
466 static int sdhci_pci_resume (struct pci_dev
*pdev
)
468 struct sdhci_pci_chip
*chip
;
469 struct sdhci_pci_slot
*slot
;
472 chip
= pci_get_drvdata(pdev
);
476 pci_set_power_state(pdev
, PCI_D0
);
477 pci_restore_state(pdev
);
478 ret
= pci_enable_device(pdev
);
482 if (chip
->fixes
&& chip
->fixes
->resume
) {
483 ret
= chip
->fixes
->resume(chip
);
488 for (i
= 0;i
< chip
->num_slots
;i
++) {
489 slot
= chip
->slots
[i
];
493 ret
= sdhci_resume_host(slot
->host
);
501 #else /* CONFIG_PM */
503 #define sdhci_pci_suspend NULL
504 #define sdhci_pci_resume NULL
506 #endif /* CONFIG_PM */
508 /*****************************************************************************\
510 * Device probing/removal *
512 \*****************************************************************************/
514 static struct sdhci_pci_slot
* __devinit
sdhci_pci_probe_slot(
515 struct pci_dev
*pdev
, struct sdhci_pci_chip
*chip
, int bar
)
517 struct sdhci_pci_slot
*slot
;
518 struct sdhci_host
*host
;
520 resource_size_t addr
;
524 if (!(pci_resource_flags(pdev
, bar
) & IORESOURCE_MEM
)) {
525 dev_err(&pdev
->dev
, "BAR %d is not iomem. Aborting.\n", bar
);
526 return ERR_PTR(-ENODEV
);
529 if (pci_resource_len(pdev
, bar
) != 0x100) {
530 dev_err(&pdev
->dev
, "Invalid iomem size. You may "
531 "experience problems.\n");
534 if ((pdev
->class & 0x0000FF) == PCI_SDHCI_IFVENDOR
) {
535 dev_err(&pdev
->dev
, "Vendor specific interface. Aborting.\n");
536 return ERR_PTR(-ENODEV
);
539 if ((pdev
->class & 0x0000FF) > PCI_SDHCI_IFVENDOR
) {
540 dev_err(&pdev
->dev
, "Unknown interface. Aborting.\n");
541 return ERR_PTR(-ENODEV
);
544 host
= sdhci_alloc_host(&pdev
->dev
, sizeof(struct sdhci_pci_slot
));
546 dev_err(&pdev
->dev
, "cannot allocate host\n");
547 return ERR_PTR(PTR_ERR(host
));
550 slot
= sdhci_priv(host
);
556 host
->hw_name
= "PCI";
557 host
->ops
= &sdhci_pci_ops
;
558 host
->quirks
= chip
->quirks
;
560 host
->irq
= pdev
->irq
;
562 ret
= pci_request_region(pdev
, bar
, mmc_hostname(host
->mmc
));
564 dev_err(&pdev
->dev
, "cannot request region\n");
568 addr
= pci_resource_start(pdev
, bar
);
569 host
->ioaddr
= pci_ioremap_bar(pdev
, bar
);
571 dev_err(&pdev
->dev
, "failed to remap registers\n");
575 if (chip
->fixes
&& chip
->fixes
->probe_slot
) {
576 ret
= chip
->fixes
->probe_slot(slot
);
581 ret
= sdhci_add_host(host
);
588 if (chip
->fixes
&& chip
->fixes
->remove_slot
)
589 chip
->fixes
->remove_slot(slot
, 0);
592 iounmap(host
->ioaddr
);
595 pci_release_region(pdev
, bar
);
598 sdhci_free_host(host
);
603 static void sdhci_pci_remove_slot(struct sdhci_pci_slot
*slot
)
609 scratch
= readl(slot
->host
->ioaddr
+ SDHCI_INT_STATUS
);
610 if (scratch
== (u32
)-1)
613 sdhci_remove_host(slot
->host
, dead
);
615 if (slot
->chip
->fixes
&& slot
->chip
->fixes
->remove_slot
)
616 slot
->chip
->fixes
->remove_slot(slot
, dead
);
618 pci_release_region(slot
->chip
->pdev
, slot
->pci_bar
);
620 sdhci_free_host(slot
->host
);
623 static int __devinit
sdhci_pci_probe(struct pci_dev
*pdev
,
624 const struct pci_device_id
*ent
)
626 struct sdhci_pci_chip
*chip
;
627 struct sdhci_pci_slot
*slot
;
629 u8 slots
, rev
, first_bar
;
632 BUG_ON(pdev
== NULL
);
635 pci_read_config_byte(pdev
, PCI_CLASS_REVISION
, &rev
);
637 dev_info(&pdev
->dev
, "SDHCI controller found [%04x:%04x] (rev %x)\n",
638 (int)pdev
->vendor
, (int)pdev
->device
, (int)rev
);
640 ret
= pci_read_config_byte(pdev
, PCI_SLOT_INFO
, &slots
);
644 slots
= PCI_SLOT_INFO_SLOTS(slots
) + 1;
645 dev_dbg(&pdev
->dev
, "found %d slot(s)\n", slots
);
649 BUG_ON(slots
> MAX_SLOTS
);
651 ret
= pci_read_config_byte(pdev
, PCI_SLOT_INFO
, &first_bar
);
655 first_bar
&= PCI_SLOT_INFO_FIRST_BAR_MASK
;
658 dev_err(&pdev
->dev
, "Invalid first BAR. Aborting.\n");
662 ret
= pci_enable_device(pdev
);
666 chip
= kzalloc(sizeof(struct sdhci_pci_chip
), GFP_KERNEL
);
673 chip
->fixes
= (const struct sdhci_pci_fixes
*)ent
->driver_data
;
675 chip
->quirks
= chip
->fixes
->quirks
;
676 chip
->num_slots
= slots
;
678 pci_set_drvdata(pdev
, chip
);
680 if (chip
->fixes
&& chip
->fixes
->probe
) {
681 ret
= chip
->fixes
->probe(chip
);
686 for (i
= 0;i
< slots
;i
++) {
687 slot
= sdhci_pci_probe_slot(pdev
, chip
, first_bar
+ i
);
690 sdhci_pci_remove_slot(chip
->slots
[i
]);
695 chip
->slots
[i
] = slot
;
701 pci_set_drvdata(pdev
, NULL
);
705 pci_disable_device(pdev
);
709 static void __devexit
sdhci_pci_remove(struct pci_dev
*pdev
)
712 struct sdhci_pci_chip
*chip
;
714 chip
= pci_get_drvdata(pdev
);
717 for (i
= 0;i
< chip
->num_slots
; i
++)
718 sdhci_pci_remove_slot(chip
->slots
[i
]);
720 pci_set_drvdata(pdev
, NULL
);
724 pci_disable_device(pdev
);
727 static struct pci_driver sdhci_driver
= {
730 .probe
= sdhci_pci_probe
,
731 .remove
= __devexit_p(sdhci_pci_remove
),
732 .suspend
= sdhci_pci_suspend
,
733 .resume
= sdhci_pci_resume
,
736 /*****************************************************************************\
740 \*****************************************************************************/
742 static int __init
sdhci_drv_init(void)
744 return pci_register_driver(&sdhci_driver
);
747 static void __exit
sdhci_drv_exit(void)
749 pci_unregister_driver(&sdhci_driver
);
752 module_init(sdhci_drv_init
);
753 module_exit(sdhci_drv_exit
);
755 MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
756 MODULE_DESCRIPTION("Secure Digital Host Controller Interface PCI driver");
757 MODULE_LICENSE("GPL");