class: move driver core specific parts to a private structure
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / mmc / host / ricoh_mmc.c
bloba16d7609e4eede4fa88f4ecc6b3ac9dce699038a
1 /*
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 R5C832 works. This chip implements firewire
15 * and four different memory card controllers. Two of those controllers are
16 * an SDHCI controller and a proprietary MMC controller. The linux SDHCI
17 * driver supports MMC cards but the chip detects MMC cards in hardware
18 * and directs them to the MMC controller - so the SDHCI driver never sees
19 * them. To get around this, we must disable the useless MMC controller.
20 * At that point, the SDHCI controller will start seeing them. As a bonus,
21 * a detection event occurs immediately, even if the MMC card is already
22 * in the reader.
24 * The relevant registers live on the firewire function, so this is unavoidably
25 * ugly. Such is life.
28 #include <linux/pci.h>
30 #define DRIVER_NAME "ricoh-mmc"
32 static const struct pci_device_id pci_ids[] __devinitdata = {
34 .vendor = PCI_VENDOR_ID_RICOH,
35 .device = PCI_DEVICE_ID_RICOH_R5C843,
36 .subvendor = PCI_ANY_ID,
37 .subdevice = PCI_ANY_ID,
39 { /* end: all zeroes */ },
42 MODULE_DEVICE_TABLE(pci, pci_ids);
44 static int ricoh_mmc_disable(struct pci_dev *fw_dev)
46 u8 write_enable;
47 u8 write_target;
48 u8 disable;
50 if (fw_dev->device == PCI_DEVICE_ID_RICOH_RL5C476) {
51 /* via RL5C476 */
53 pci_read_config_byte(fw_dev, 0xB7, &disable);
54 if (disable & 0x02) {
55 printk(KERN_INFO DRIVER_NAME
56 ": Controller already disabled. " \
57 "Nothing to do.\n");
58 return -ENODEV;
61 pci_read_config_byte(fw_dev, 0x8E, &write_enable);
62 pci_write_config_byte(fw_dev, 0x8E, 0xAA);
63 pci_read_config_byte(fw_dev, 0x8D, &write_target);
64 pci_write_config_byte(fw_dev, 0x8D, 0xB7);
65 pci_write_config_byte(fw_dev, 0xB7, disable | 0x02);
66 pci_write_config_byte(fw_dev, 0x8E, write_enable);
67 pci_write_config_byte(fw_dev, 0x8D, write_target);
68 } else {
69 /* via R5C832 */
71 pci_read_config_byte(fw_dev, 0xCB, &disable);
72 if (disable & 0x02) {
73 printk(KERN_INFO DRIVER_NAME
74 ": Controller already disabled. " \
75 "Nothing to do.\n");
76 return -ENODEV;
79 pci_read_config_byte(fw_dev, 0xCA, &write_enable);
80 pci_write_config_byte(fw_dev, 0xCA, 0x57);
81 pci_write_config_byte(fw_dev, 0xCB, disable | 0x02);
82 pci_write_config_byte(fw_dev, 0xCA, write_enable);
85 printk(KERN_INFO DRIVER_NAME
86 ": Controller is now disabled.\n");
88 return 0;
91 static int ricoh_mmc_enable(struct pci_dev *fw_dev)
93 u8 write_enable;
94 u8 write_target;
95 u8 disable;
97 if (fw_dev->device == PCI_DEVICE_ID_RICOH_RL5C476) {
98 /* via RL5C476 */
100 pci_read_config_byte(fw_dev, 0x8E, &write_enable);
101 pci_write_config_byte(fw_dev, 0x8E, 0xAA);
102 pci_read_config_byte(fw_dev, 0x8D, &write_target);
103 pci_write_config_byte(fw_dev, 0x8D, 0xB7);
104 pci_read_config_byte(fw_dev, 0xB7, &disable);
105 pci_write_config_byte(fw_dev, 0xB7, disable & ~0x02);
106 pci_write_config_byte(fw_dev, 0x8E, write_enable);
107 pci_write_config_byte(fw_dev, 0x8D, write_target);
108 } else {
109 /* via R5C832 */
111 pci_read_config_byte(fw_dev, 0xCA, &write_enable);
112 pci_read_config_byte(fw_dev, 0xCB, &disable);
113 pci_write_config_byte(fw_dev, 0xCA, 0x57);
114 pci_write_config_byte(fw_dev, 0xCB, disable & ~0x02);
115 pci_write_config_byte(fw_dev, 0xCA, write_enable);
118 printk(KERN_INFO DRIVER_NAME
119 ": Controller is now re-enabled.\n");
121 return 0;
124 static int __devinit ricoh_mmc_probe(struct pci_dev *pdev,
125 const struct pci_device_id *ent)
127 u8 rev;
128 u8 ctrlfound = 0;
130 struct pci_dev *fw_dev = NULL;
132 BUG_ON(pdev == NULL);
133 BUG_ON(ent == NULL);
135 pci_read_config_byte(pdev, PCI_CLASS_REVISION, &rev);
137 printk(KERN_INFO DRIVER_NAME
138 ": Ricoh MMC controller found at %s [%04x:%04x] (rev %x)\n",
139 pci_name(pdev), (int)pdev->vendor, (int)pdev->device,
140 (int)rev);
142 while ((fw_dev =
143 pci_get_device(PCI_VENDOR_ID_RICOH,
144 PCI_DEVICE_ID_RICOH_RL5C476, fw_dev))) {
145 if (PCI_SLOT(pdev->devfn) == PCI_SLOT(fw_dev->devfn) &&
146 pdev->bus == fw_dev->bus) {
147 if (ricoh_mmc_disable(fw_dev) != 0)
148 return -ENODEV;
150 pci_set_drvdata(pdev, fw_dev);
152 ++ctrlfound;
153 break;
157 fw_dev = NULL;
159 while (!ctrlfound &&
160 (fw_dev = pci_get_device(PCI_VENDOR_ID_RICOH,
161 PCI_DEVICE_ID_RICOH_R5C832, fw_dev))) {
162 if (PCI_SLOT(pdev->devfn) == PCI_SLOT(fw_dev->devfn) &&
163 pdev->bus == fw_dev->bus) {
164 if (ricoh_mmc_disable(fw_dev) != 0)
165 return -ENODEV;
167 pci_set_drvdata(pdev, fw_dev);
169 ++ctrlfound;
173 if (!ctrlfound) {
174 printk(KERN_WARNING DRIVER_NAME
175 ": Main firewire function not found. Cannot disable controller.\n");
176 return -ENODEV;
179 return 0;
182 static void __devexit ricoh_mmc_remove(struct pci_dev *pdev)
184 struct pci_dev *fw_dev = NULL;
186 fw_dev = pci_get_drvdata(pdev);
187 BUG_ON(fw_dev == NULL);
189 ricoh_mmc_enable(fw_dev);
191 pci_set_drvdata(pdev, NULL);
194 static int ricoh_mmc_suspend(struct pci_dev *pdev, pm_message_t state)
196 struct pci_dev *fw_dev = NULL;
198 fw_dev = pci_get_drvdata(pdev);
199 BUG_ON(fw_dev == NULL);
201 printk(KERN_INFO DRIVER_NAME ": Suspending.\n");
203 ricoh_mmc_enable(fw_dev);
205 return 0;
208 static int ricoh_mmc_resume(struct pci_dev *pdev)
210 struct pci_dev *fw_dev = NULL;
212 fw_dev = pci_get_drvdata(pdev);
213 BUG_ON(fw_dev == NULL);
215 printk(KERN_INFO DRIVER_NAME ": Resuming.\n");
217 ricoh_mmc_disable(fw_dev);
219 return 0;
222 static struct pci_driver ricoh_mmc_driver = {
223 .name = DRIVER_NAME,
224 .id_table = pci_ids,
225 .probe = ricoh_mmc_probe,
226 .remove = __devexit_p(ricoh_mmc_remove),
227 .suspend = ricoh_mmc_suspend,
228 .resume = ricoh_mmc_resume,
231 /*****************************************************************************\
233 * Driver init/exit *
235 \*****************************************************************************/
237 static int __init ricoh_mmc_drv_init(void)
239 printk(KERN_INFO DRIVER_NAME
240 ": Ricoh MMC Controller disabling driver\n");
241 printk(KERN_INFO DRIVER_NAME ": Copyright(c) Philip Langdale\n");
243 return pci_register_driver(&ricoh_mmc_driver);
246 static void __exit ricoh_mmc_drv_exit(void)
248 pci_unregister_driver(&ricoh_mmc_driver);
251 module_init(ricoh_mmc_drv_init);
252 module_exit(ricoh_mmc_drv_exit);
254 MODULE_AUTHOR("Philip Langdale <philipl@alumni.utexas.net>");
255 MODULE_DESCRIPTION("Ricoh MMC Controller disabling driver");
256 MODULE_LICENSE("GPL");