2 * ricoh_mmc.c - Dummy driver to disable the Rioch MMC controller.
4 * Copyright (C) 2007 Philip Langdale, All Rights Reserved.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or (at
9 * your option) any later version.
13 * This is a conceptually ridiculous driver, but it is required by the way
14 * the Ricoh multi-function chips (R5CXXX) work. These chips implement
15 * the four main memory card controllers (SD, MMC, MS, xD) and one or both
16 * of cardbus or firewire. It happens that they implement SD and MMC
17 * support as separate controllers (and PCI functions). The linux SDHCI
18 * driver supports MMC cards but the chip detects MMC cards in hardware
19 * and directs them to the MMC controller - so the SDHCI driver never sees
20 * them. To get around this, we must disable the useless MMC controller.
21 * At that point, the SDHCI controller will start seeing them. As a bonus,
22 * a detection event occurs immediately, even if the MMC card is already
25 * It seems to be the case that the relevant PCI registers to deactivate the
26 * MMC controller live on PCI function 0, which might be the cardbus controller
27 * or the firewire controller, depending on the particular chip in question. As
28 * such, it makes what this driver has to do unavoidably ugly. Such is life.
31 #include <linux/pci.h>
33 #define DRIVER_NAME "ricoh-mmc"
35 static const struct pci_device_id pci_ids
[] __devinitdata
= {
37 .vendor
= PCI_VENDOR_ID_RICOH
,
38 .device
= PCI_DEVICE_ID_RICOH_R5C843
,
39 .subvendor
= PCI_ANY_ID
,
40 .subdevice
= PCI_ANY_ID
,
42 { /* end: all zeroes */ },
45 MODULE_DEVICE_TABLE(pci
, pci_ids
);
47 static int ricoh_mmc_disable(struct pci_dev
*fw_dev
)
53 if (fw_dev
->device
== PCI_DEVICE_ID_RICOH_RL5C476
) {
56 pci_read_config_byte(fw_dev
, 0xB7, &disable
);
58 printk(KERN_INFO DRIVER_NAME
59 ": Controller already disabled. " \
64 pci_read_config_byte(fw_dev
, 0x8E, &write_enable
);
65 pci_write_config_byte(fw_dev
, 0x8E, 0xAA);
66 pci_read_config_byte(fw_dev
, 0x8D, &write_target
);
67 pci_write_config_byte(fw_dev
, 0x8D, 0xB7);
68 pci_write_config_byte(fw_dev
, 0xB7, disable
| 0x02);
69 pci_write_config_byte(fw_dev
, 0x8E, write_enable
);
70 pci_write_config_byte(fw_dev
, 0x8D, write_target
);
74 pci_read_config_byte(fw_dev
, 0xCB, &disable
);
76 printk(KERN_INFO DRIVER_NAME
77 ": Controller already disabled. " \
82 pci_read_config_byte(fw_dev
, 0xCA, &write_enable
);
83 pci_write_config_byte(fw_dev
, 0xCA, 0x57);
84 pci_write_config_byte(fw_dev
, 0xCB, disable
| 0x02);
85 pci_write_config_byte(fw_dev
, 0xCA, write_enable
);
88 printk(KERN_INFO DRIVER_NAME
89 ": Controller is now disabled.\n");
94 static int ricoh_mmc_enable(struct pci_dev
*fw_dev
)
100 if (fw_dev
->device
== PCI_DEVICE_ID_RICOH_RL5C476
) {
103 pci_read_config_byte(fw_dev
, 0x8E, &write_enable
);
104 pci_write_config_byte(fw_dev
, 0x8E, 0xAA);
105 pci_read_config_byte(fw_dev
, 0x8D, &write_target
);
106 pci_write_config_byte(fw_dev
, 0x8D, 0xB7);
107 pci_read_config_byte(fw_dev
, 0xB7, &disable
);
108 pci_write_config_byte(fw_dev
, 0xB7, disable
& ~0x02);
109 pci_write_config_byte(fw_dev
, 0x8E, write_enable
);
110 pci_write_config_byte(fw_dev
, 0x8D, write_target
);
114 pci_read_config_byte(fw_dev
, 0xCA, &write_enable
);
115 pci_read_config_byte(fw_dev
, 0xCB, &disable
);
116 pci_write_config_byte(fw_dev
, 0xCA, 0x57);
117 pci_write_config_byte(fw_dev
, 0xCB, disable
& ~0x02);
118 pci_write_config_byte(fw_dev
, 0xCA, write_enable
);
121 printk(KERN_INFO DRIVER_NAME
122 ": Controller is now re-enabled.\n");
127 static int __devinit
ricoh_mmc_probe(struct pci_dev
*pdev
,
128 const struct pci_device_id
*ent
)
133 struct pci_dev
*fw_dev
= NULL
;
135 BUG_ON(pdev
== NULL
);
138 pci_read_config_byte(pdev
, PCI_CLASS_REVISION
, &rev
);
140 printk(KERN_INFO DRIVER_NAME
141 ": Ricoh MMC controller found at %s [%04x:%04x] (rev %x)\n",
142 pci_name(pdev
), (int)pdev
->vendor
, (int)pdev
->device
,
146 pci_get_device(PCI_VENDOR_ID_RICOH
,
147 PCI_DEVICE_ID_RICOH_RL5C476
, fw_dev
))) {
148 if (PCI_SLOT(pdev
->devfn
) == PCI_SLOT(fw_dev
->devfn
) &&
149 PCI_FUNC(fw_dev
->devfn
) == 0 &&
150 pdev
->bus
== fw_dev
->bus
) {
151 if (ricoh_mmc_disable(fw_dev
) != 0)
154 pci_set_drvdata(pdev
, fw_dev
);
164 (fw_dev
= pci_get_device(PCI_VENDOR_ID_RICOH
,
165 PCI_DEVICE_ID_RICOH_R5C832
, fw_dev
))) {
166 if (PCI_SLOT(pdev
->devfn
) == PCI_SLOT(fw_dev
->devfn
) &&
167 PCI_FUNC(fw_dev
->devfn
) == 0 &&
168 pdev
->bus
== fw_dev
->bus
) {
169 if (ricoh_mmc_disable(fw_dev
) != 0)
172 pci_set_drvdata(pdev
, fw_dev
);
179 printk(KERN_WARNING DRIVER_NAME
180 ": Main Ricoh function not found. Cannot disable controller.\n");
187 static void __devexit
ricoh_mmc_remove(struct pci_dev
*pdev
)
189 struct pci_dev
*fw_dev
= NULL
;
191 fw_dev
= pci_get_drvdata(pdev
);
192 BUG_ON(fw_dev
== NULL
);
194 ricoh_mmc_enable(fw_dev
);
196 pci_set_drvdata(pdev
, NULL
);
199 static int ricoh_mmc_suspend_late(struct pci_dev
*pdev
, pm_message_t state
)
201 struct pci_dev
*fw_dev
= NULL
;
203 fw_dev
= pci_get_drvdata(pdev
);
204 BUG_ON(fw_dev
== NULL
);
206 printk(KERN_INFO DRIVER_NAME
": Suspending.\n");
208 ricoh_mmc_enable(fw_dev
);
213 static int ricoh_mmc_resume_early(struct pci_dev
*pdev
)
215 struct pci_dev
*fw_dev
= NULL
;
217 fw_dev
= pci_get_drvdata(pdev
);
218 BUG_ON(fw_dev
== NULL
);
220 printk(KERN_INFO DRIVER_NAME
": Resuming.\n");
222 ricoh_mmc_disable(fw_dev
);
227 static struct pci_driver ricoh_mmc_driver
= {
230 .probe
= ricoh_mmc_probe
,
231 .remove
= __devexit_p(ricoh_mmc_remove
),
232 .suspend_late
= ricoh_mmc_suspend_late
,
233 .resume_early
= ricoh_mmc_resume_early
,
236 /*****************************************************************************\
240 \*****************************************************************************/
242 static int __init
ricoh_mmc_drv_init(void)
244 printk(KERN_INFO DRIVER_NAME
245 ": Ricoh MMC Controller disabling driver\n");
246 printk(KERN_INFO DRIVER_NAME
": Copyright(c) Philip Langdale\n");
248 return pci_register_driver(&ricoh_mmc_driver
);
251 static void __exit
ricoh_mmc_drv_exit(void)
253 pci_unregister_driver(&ricoh_mmc_driver
);
256 module_init(ricoh_mmc_drv_init
);
257 module_exit(ricoh_mmc_drv_exit
);
259 MODULE_AUTHOR("Philip Langdale <philipl@alumni.utexas.net>");
260 MODULE_DESCRIPTION("Ricoh MMC Controller disabling driver");
261 MODULE_LICENSE("GPL");