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_32BIT_ADMA_SIZE
|
146 SDHCI_QUIRK_RESET_AFTER_REQUEST
;
150 * JMicron chips can have two interfaces to the same hardware
151 * in order to work around limitations in Microsoft's driver.
152 * We need to make sure we only bind to one of them.
154 * This code assumes two things:
156 * 1. The PCI code adds subfunctions in order.
158 * 2. The MMC interface has a lower subfunction number
159 * than the SD interface.
161 if (chip
->pdev
->device
== PCI_DEVICE_ID_JMICRON_JMB38X_SD
) {
162 struct pci_dev
*sd_dev
;
165 while ((sd_dev
= pci_get_device(PCI_VENDOR_ID_JMICRON
,
166 PCI_DEVICE_ID_JMICRON_JMB38X_MMC
, sd_dev
)) != NULL
) {
167 if ((PCI_SLOT(chip
->pdev
->devfn
) ==
168 PCI_SLOT(sd_dev
->devfn
)) &&
169 (chip
->pdev
->bus
== sd_dev
->bus
))
175 dev_info(&chip
->pdev
->dev
, "Refusing to bind to "
176 "secondary interface.\n");
182 * JMicron chips need a bit of a nudge to enable the power
185 ret
= jmicron_pmos(chip
, 1);
187 dev_err(&chip
->pdev
->dev
, "Failure enabling card power\n");
194 static void jmicron_enable_mmc(struct sdhci_host
*host
, int on
)
198 scratch
= readb(host
->ioaddr
+ 0xC0);
205 writeb(scratch
, host
->ioaddr
+ 0xC0);
208 static int jmicron_probe_slot(struct sdhci_pci_slot
*slot
)
210 if (slot
->chip
->pdev
->revision
== 0) {
213 version
= readl(slot
->host
->ioaddr
+ SDHCI_HOST_VERSION
);
214 version
= (version
& SDHCI_VENDOR_VER_MASK
) >>
215 SDHCI_VENDOR_VER_SHIFT
;
218 * Older versions of the chip have lots of nasty glitches
219 * in the ADMA engine. It's best just to avoid it
223 slot
->host
->quirks
|= SDHCI_QUIRK_BROKEN_ADMA
;
227 * The secondary interface requires a bit set to get the
230 if (slot
->chip
->pdev
->device
== PCI_DEVICE_ID_JMICRON_JMB38X_MMC
)
231 jmicron_enable_mmc(slot
->host
, 1);
236 static void jmicron_remove_slot(struct sdhci_pci_slot
*slot
, int dead
)
241 if (slot
->chip
->pdev
->device
== PCI_DEVICE_ID_JMICRON_JMB38X_MMC
)
242 jmicron_enable_mmc(slot
->host
, 0);
245 static int jmicron_suspend(struct sdhci_pci_chip
*chip
, pm_message_t state
)
249 if (chip
->pdev
->device
== PCI_DEVICE_ID_JMICRON_JMB38X_MMC
) {
250 for (i
= 0;i
< chip
->num_slots
;i
++)
251 jmicron_enable_mmc(chip
->slots
[i
]->host
, 0);
257 static int jmicron_resume(struct sdhci_pci_chip
*chip
)
261 if (chip
->pdev
->device
== PCI_DEVICE_ID_JMICRON_JMB38X_MMC
) {
262 for (i
= 0;i
< chip
->num_slots
;i
++)
263 jmicron_enable_mmc(chip
->slots
[i
]->host
, 1);
266 ret
= jmicron_pmos(chip
, 1);
268 dev_err(&chip
->pdev
->dev
, "Failure enabling card power\n");
275 static const struct sdhci_pci_fixes sdhci_jmicron
= {
276 .probe
= jmicron_probe
,
278 .probe_slot
= jmicron_probe_slot
,
279 .remove_slot
= jmicron_remove_slot
,
281 .suspend
= jmicron_suspend
,
282 .resume
= jmicron_resume
,
285 static const struct pci_device_id pci_ids
[] __devinitdata
= {
287 .vendor
= PCI_VENDOR_ID_RICOH
,
288 .device
= PCI_DEVICE_ID_RICOH_R5C822
,
289 .subvendor
= PCI_ANY_ID
,
290 .subdevice
= PCI_ANY_ID
,
291 .driver_data
= (kernel_ulong_t
)&sdhci_ricoh
,
295 .vendor
= PCI_VENDOR_ID_ENE
,
296 .device
= PCI_DEVICE_ID_ENE_CB712_SD
,
297 .subvendor
= PCI_ANY_ID
,
298 .subdevice
= PCI_ANY_ID
,
299 .driver_data
= (kernel_ulong_t
)&sdhci_ene_712
,
303 .vendor
= PCI_VENDOR_ID_ENE
,
304 .device
= PCI_DEVICE_ID_ENE_CB712_SD_2
,
305 .subvendor
= PCI_ANY_ID
,
306 .subdevice
= PCI_ANY_ID
,
307 .driver_data
= (kernel_ulong_t
)&sdhci_ene_712
,
311 .vendor
= PCI_VENDOR_ID_ENE
,
312 .device
= PCI_DEVICE_ID_ENE_CB714_SD
,
313 .subvendor
= PCI_ANY_ID
,
314 .subdevice
= PCI_ANY_ID
,
315 .driver_data
= (kernel_ulong_t
)&sdhci_ene_714
,
319 .vendor
= PCI_VENDOR_ID_ENE
,
320 .device
= PCI_DEVICE_ID_ENE_CB714_SD_2
,
321 .subvendor
= PCI_ANY_ID
,
322 .subdevice
= PCI_ANY_ID
,
323 .driver_data
= (kernel_ulong_t
)&sdhci_ene_714
,
327 .vendor
= PCI_VENDOR_ID_MARVELL
,
328 .device
= PCI_DEVICE_ID_MARVELL_CAFE_SD
,
329 .subvendor
= PCI_ANY_ID
,
330 .subdevice
= PCI_ANY_ID
,
331 .driver_data
= (kernel_ulong_t
)&sdhci_cafe
,
335 .vendor
= PCI_VENDOR_ID_JMICRON
,
336 .device
= PCI_DEVICE_ID_JMICRON_JMB38X_SD
,
337 .subvendor
= PCI_ANY_ID
,
338 .subdevice
= PCI_ANY_ID
,
339 .driver_data
= (kernel_ulong_t
)&sdhci_jmicron
,
343 .vendor
= PCI_VENDOR_ID_JMICRON
,
344 .device
= PCI_DEVICE_ID_JMICRON_JMB38X_MMC
,
345 .subvendor
= PCI_ANY_ID
,
346 .subdevice
= PCI_ANY_ID
,
347 .driver_data
= (kernel_ulong_t
)&sdhci_jmicron
,
350 { /* Generic SD host controller */
351 PCI_DEVICE_CLASS((PCI_CLASS_SYSTEM_SDHCI
<< 8), 0xFFFF00)
354 { /* end: all zeroes */ },
357 MODULE_DEVICE_TABLE(pci
, pci_ids
);
359 /*****************************************************************************\
361 * SDHCI core callbacks *
363 \*****************************************************************************/
365 static int sdhci_pci_enable_dma(struct sdhci_host
*host
)
367 struct sdhci_pci_slot
*slot
;
368 struct pci_dev
*pdev
;
371 slot
= sdhci_priv(host
);
372 pdev
= slot
->chip
->pdev
;
374 if (((pdev
->class & 0xFFFF00) == (PCI_CLASS_SYSTEM_SDHCI
<< 8)) &&
375 ((pdev
->class & 0x0000FF) != PCI_SDHCI_IFDMA
) &&
376 (host
->flags
& SDHCI_USE_DMA
)) {
377 dev_warn(&pdev
->dev
, "Will use DMA mode even though HW "
378 "doesn't fully claim to support it.\n");
381 ret
= pci_set_dma_mask(pdev
, DMA_32BIT_MASK
);
385 pci_set_master(pdev
);
390 static struct sdhci_ops sdhci_pci_ops
= {
391 .enable_dma
= sdhci_pci_enable_dma
,
394 /*****************************************************************************\
398 \*****************************************************************************/
402 static int sdhci_pci_suspend (struct pci_dev
*pdev
, pm_message_t state
)
404 struct sdhci_pci_chip
*chip
;
405 struct sdhci_pci_slot
*slot
;
408 chip
= pci_get_drvdata(pdev
);
412 for (i
= 0;i
< chip
->num_slots
;i
++) {
413 slot
= chip
->slots
[i
];
417 ret
= sdhci_suspend_host(slot
->host
, state
);
421 sdhci_resume_host(chip
->slots
[i
]->host
);
426 if (chip
->fixes
&& chip
->fixes
->suspend
) {
427 ret
= chip
->fixes
->suspend(chip
, state
);
429 for (i
= chip
->num_slots
- 1;i
>= 0;i
--)
430 sdhci_resume_host(chip
->slots
[i
]->host
);
435 pci_save_state(pdev
);
436 pci_enable_wake(pdev
, pci_choose_state(pdev
, state
), 0);
437 pci_disable_device(pdev
);
438 pci_set_power_state(pdev
, pci_choose_state(pdev
, state
));
443 static int sdhci_pci_resume (struct pci_dev
*pdev
)
445 struct sdhci_pci_chip
*chip
;
446 struct sdhci_pci_slot
*slot
;
449 chip
= pci_get_drvdata(pdev
);
453 pci_set_power_state(pdev
, PCI_D0
);
454 pci_restore_state(pdev
);
455 ret
= pci_enable_device(pdev
);
459 if (chip
->fixes
&& chip
->fixes
->resume
) {
460 ret
= chip
->fixes
->resume(chip
);
465 for (i
= 0;i
< chip
->num_slots
;i
++) {
466 slot
= chip
->slots
[i
];
470 ret
= sdhci_resume_host(slot
->host
);
478 #else /* CONFIG_PM */
480 #define sdhci_pci_suspend NULL
481 #define sdhci_pci_resume NULL
483 #endif /* CONFIG_PM */
485 /*****************************************************************************\
487 * Device probing/removal *
489 \*****************************************************************************/
491 static struct sdhci_pci_slot
* __devinit
sdhci_pci_probe_slot(
492 struct pci_dev
*pdev
, struct sdhci_pci_chip
*chip
, int bar
)
494 struct sdhci_pci_slot
*slot
;
495 struct sdhci_host
*host
;
497 resource_size_t addr
;
501 if (!(pci_resource_flags(pdev
, bar
) & IORESOURCE_MEM
)) {
502 dev_err(&pdev
->dev
, "BAR %d is not iomem. Aborting.\n", bar
);
503 return ERR_PTR(-ENODEV
);
506 if (pci_resource_len(pdev
, bar
) != 0x100) {
507 dev_err(&pdev
->dev
, "Invalid iomem size. You may "
508 "experience problems.\n");
511 if ((pdev
->class & 0x0000FF) == PCI_SDHCI_IFVENDOR
) {
512 dev_err(&pdev
->dev
, "Vendor specific interface. Aborting.\n");
513 return ERR_PTR(-ENODEV
);
516 if ((pdev
->class & 0x0000FF) > PCI_SDHCI_IFVENDOR
) {
517 dev_err(&pdev
->dev
, "Unknown interface. Aborting.\n");
518 return ERR_PTR(-ENODEV
);
521 host
= sdhci_alloc_host(&pdev
->dev
, sizeof(struct sdhci_pci_slot
));
527 slot
= sdhci_priv(host
);
533 host
->hw_name
= "PCI";
534 host
->ops
= &sdhci_pci_ops
;
535 host
->quirks
= chip
->quirks
;
537 host
->irq
= pdev
->irq
;
539 ret
= pci_request_region(pdev
, bar
, mmc_hostname(host
->mmc
));
541 dev_err(&pdev
->dev
, "cannot request region\n");
545 addr
= pci_resource_start(pdev
, bar
);
546 host
->ioaddr
= ioremap_nocache(addr
, pci_resource_len(pdev
, bar
));
548 dev_err(&pdev
->dev
, "failed to remap registers\n");
552 if (chip
->fixes
&& chip
->fixes
->probe_slot
) {
553 ret
= chip
->fixes
->probe_slot(slot
);
558 ret
= sdhci_add_host(host
);
565 if (chip
->fixes
&& chip
->fixes
->remove_slot
)
566 chip
->fixes
->remove_slot(slot
, 0);
569 iounmap(host
->ioaddr
);
572 pci_release_region(pdev
, bar
);
573 sdhci_free_host(host
);
578 static void sdhci_pci_remove_slot(struct sdhci_pci_slot
*slot
)
584 scratch
= readl(slot
->host
->ioaddr
+ SDHCI_INT_STATUS
);
585 if (scratch
== (u32
)-1)
588 sdhci_remove_host(slot
->host
, dead
);
590 if (slot
->chip
->fixes
&& slot
->chip
->fixes
->remove_slot
)
591 slot
->chip
->fixes
->remove_slot(slot
, dead
);
593 pci_release_region(slot
->chip
->pdev
, slot
->pci_bar
);
595 sdhci_free_host(slot
->host
);
598 static int __devinit
sdhci_pci_probe(struct pci_dev
*pdev
,
599 const struct pci_device_id
*ent
)
601 struct sdhci_pci_chip
*chip
;
602 struct sdhci_pci_slot
*slot
;
604 u8 slots
, rev
, first_bar
;
607 BUG_ON(pdev
== NULL
);
610 pci_read_config_byte(pdev
, PCI_CLASS_REVISION
, &rev
);
612 dev_info(&pdev
->dev
, "SDHCI controller found [%04x:%04x] (rev %x)\n",
613 (int)pdev
->vendor
, (int)pdev
->device
, (int)rev
);
615 ret
= pci_read_config_byte(pdev
, PCI_SLOT_INFO
, &slots
);
619 slots
= PCI_SLOT_INFO_SLOTS(slots
) + 1;
620 dev_dbg(&pdev
->dev
, "found %d slot(s)\n", slots
);
624 BUG_ON(slots
> MAX_SLOTS
);
626 ret
= pci_read_config_byte(pdev
, PCI_SLOT_INFO
, &first_bar
);
630 first_bar
&= PCI_SLOT_INFO_FIRST_BAR_MASK
;
633 dev_err(&pdev
->dev
, "Invalid first BAR. Aborting.\n");
637 ret
= pci_enable_device(pdev
);
641 chip
= kzalloc(sizeof(struct sdhci_pci_chip
), GFP_KERNEL
);
648 chip
->fixes
= (const struct sdhci_pci_fixes
*)ent
->driver_data
;
650 chip
->quirks
= chip
->fixes
->quirks
;
651 chip
->num_slots
= slots
;
653 pci_set_drvdata(pdev
, chip
);
655 if (chip
->fixes
&& chip
->fixes
->probe
) {
656 ret
= chip
->fixes
->probe(chip
);
661 for (i
= 0;i
< slots
;i
++) {
662 slot
= sdhci_pci_probe_slot(pdev
, chip
, first_bar
+ i
);
665 sdhci_pci_remove_slot(chip
->slots
[i
]);
670 chip
->slots
[i
] = slot
;
676 pci_set_drvdata(pdev
, NULL
);
680 pci_disable_device(pdev
);
684 static void __devexit
sdhci_pci_remove(struct pci_dev
*pdev
)
687 struct sdhci_pci_chip
*chip
;
689 chip
= pci_get_drvdata(pdev
);
692 for (i
= 0;i
< chip
->num_slots
; i
++)
693 sdhci_pci_remove_slot(chip
->slots
[i
]);
695 pci_set_drvdata(pdev
, NULL
);
699 pci_disable_device(pdev
);
702 static struct pci_driver sdhci_driver
= {
705 .probe
= sdhci_pci_probe
,
706 .remove
= __devexit_p(sdhci_pci_remove
),
707 .suspend
= sdhci_pci_suspend
,
708 .resume
= sdhci_pci_resume
,
711 /*****************************************************************************\
715 \*****************************************************************************/
717 static int __init
sdhci_drv_init(void)
719 return pci_register_driver(&sdhci_driver
);
722 static void __exit
sdhci_drv_exit(void)
724 pci_unregister_driver(&sdhci_driver
);
727 module_init(sdhci_drv_init
);
728 module_exit(sdhci_drv_exit
);
730 MODULE_AUTHOR("Pierre Ossman <drzeus@drzeus.cx>");
731 MODULE_DESCRIPTION("Secure Digital Host Controller Interface PCI driver");
732 MODULE_LICENSE("GPL");