xref: /openbmc/linux/drivers/misc/cb710/core.c (revision 4800cd83)
1 /*
2  *  cb710/core.c
3  *
4  *  Copyright by Michał Mirosław, 2008-2009
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  */
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/pci.h>
13 #include <linux/spinlock.h>
14 #include <linux/idr.h>
15 #include <linux/cb710.h>
16 #include <linux/gfp.h>
17 
18 static DEFINE_IDA(cb710_ida);
19 static DEFINE_SPINLOCK(cb710_ida_lock);
20 
21 void cb710_pci_update_config_reg(struct pci_dev *pdev,
22 	int reg, uint32_t mask, uint32_t xor)
23 {
24 	u32 rval;
25 
26 	pci_read_config_dword(pdev, reg, &rval);
27 	rval = (rval & mask) ^ xor;
28 	pci_write_config_dword(pdev, reg, rval);
29 }
30 EXPORT_SYMBOL_GPL(cb710_pci_update_config_reg);
31 
32 /* Some magic writes based on Windows driver init code */
33 static int __devinit cb710_pci_configure(struct pci_dev *pdev)
34 {
35 	unsigned int devfn = PCI_DEVFN(PCI_SLOT(pdev->devfn), 0);
36 	struct pci_dev *pdev0 = pci_get_slot(pdev->bus, devfn);
37 	u32 val;
38 
39 	cb710_pci_update_config_reg(pdev, 0x48,
40 		~0x000000FF, 0x0000003F);
41 
42 	pci_read_config_dword(pdev, 0x48, &val);
43 	if (val & 0x80000000)
44 		return 0;
45 
46 	if (!pdev0)
47 		return -ENODEV;
48 
49 	if (pdev0->vendor == PCI_VENDOR_ID_ENE
50 	    && pdev0->device == PCI_DEVICE_ID_ENE_720) {
51 		cb710_pci_update_config_reg(pdev0, 0x8C,
52 			~0x00F00000, 0x00100000);
53 		cb710_pci_update_config_reg(pdev0, 0xB0,
54 			~0x08000000, 0x08000000);
55 	}
56 
57 	cb710_pci_update_config_reg(pdev0, 0x8C,
58 		~0x00000F00, 0x00000200);
59 	cb710_pci_update_config_reg(pdev0, 0x90,
60 		~0x00060000, 0x00040000);
61 
62 	pci_dev_put(pdev0);
63 
64 	return 0;
65 }
66 
67 static irqreturn_t cb710_irq_handler(int irq, void *data)
68 {
69 	struct cb710_chip *chip = data;
70 	struct cb710_slot *slot = &chip->slot[0];
71 	irqreturn_t handled = IRQ_NONE;
72 	unsigned nr;
73 
74 	spin_lock(&chip->irq_lock); /* incl. smp_rmb() */
75 
76 	for (nr = chip->slots; nr; ++slot, --nr) {
77 		cb710_irq_handler_t handler_func = slot->irq_handler;
78 		if (handler_func && handler_func(slot))
79 			handled = IRQ_HANDLED;
80 	}
81 
82 	spin_unlock(&chip->irq_lock);
83 
84 	return handled;
85 }
86 
87 static void cb710_release_slot(struct device *dev)
88 {
89 #ifdef CONFIG_CB710_DEBUG_ASSUMPTIONS
90 	struct cb710_slot *slot = cb710_pdev_to_slot(to_platform_device(dev));
91 	struct cb710_chip *chip = cb710_slot_to_chip(slot);
92 
93 	/* slot struct can be freed now */
94 	atomic_dec(&chip->slot_refs_count);
95 #endif
96 }
97 
98 static int __devinit cb710_register_slot(struct cb710_chip *chip,
99 	unsigned slot_mask, unsigned io_offset, const char *name)
100 {
101 	int nr = chip->slots;
102 	struct cb710_slot *slot = &chip->slot[nr];
103 	int err;
104 
105 	dev_dbg(cb710_chip_dev(chip),
106 		"register: %s.%d; slot %d; mask %d; IO offset: 0x%02X\n",
107 		name, chip->platform_id, nr, slot_mask, io_offset);
108 
109 	/* slot->irq_handler == NULL here; this needs to be
110 	 * seen before platform_device_register() */
111 	++chip->slots;
112 	smp_wmb();
113 
114 	slot->iobase = chip->iobase + io_offset;
115 	slot->pdev.name = name;
116 	slot->pdev.id = chip->platform_id;
117 	slot->pdev.dev.parent = &chip->pdev->dev;
118 	slot->pdev.dev.release = cb710_release_slot;
119 
120 	err = platform_device_register(&slot->pdev);
121 
122 #ifdef CONFIG_CB710_DEBUG_ASSUMPTIONS
123 	atomic_inc(&chip->slot_refs_count);
124 #endif
125 
126 	if (err) {
127 		/* device_initialize() called from platform_device_register()
128 		 * wants this on error path */
129 		platform_device_put(&slot->pdev);
130 
131 		/* slot->irq_handler == NULL here anyway, so no lock needed */
132 		--chip->slots;
133 		return err;
134 	}
135 
136 	chip->slot_mask |= slot_mask;
137 
138 	return 0;
139 }
140 
141 static void cb710_unregister_slot(struct cb710_chip *chip,
142 	unsigned slot_mask)
143 {
144 	int nr = chip->slots - 1;
145 
146 	if (!(chip->slot_mask & slot_mask))
147 		return;
148 
149 	platform_device_unregister(&chip->slot[nr].pdev);
150 
151 	/* complementary to spin_unlock() in cb710_set_irq_handler() */
152 	smp_rmb();
153 	BUG_ON(chip->slot[nr].irq_handler != NULL);
154 
155 	/* slot->irq_handler == NULL here, so no lock needed */
156 	--chip->slots;
157 	chip->slot_mask &= ~slot_mask;
158 }
159 
160 void cb710_set_irq_handler(struct cb710_slot *slot,
161 	cb710_irq_handler_t handler)
162 {
163 	struct cb710_chip *chip = cb710_slot_to_chip(slot);
164 	unsigned long flags;
165 
166 	spin_lock_irqsave(&chip->irq_lock, flags);
167 	slot->irq_handler = handler;
168 	spin_unlock_irqrestore(&chip->irq_lock, flags);
169 }
170 EXPORT_SYMBOL_GPL(cb710_set_irq_handler);
171 
172 #ifdef CONFIG_PM
173 
174 static int cb710_suspend(struct pci_dev *pdev, pm_message_t state)
175 {
176 	struct cb710_chip *chip = pci_get_drvdata(pdev);
177 
178 	free_irq(pdev->irq, chip);
179 	pci_save_state(pdev);
180 	pci_disable_device(pdev);
181 	if (state.event & PM_EVENT_SLEEP)
182 		pci_set_power_state(pdev, PCI_D3cold);
183 	return 0;
184 }
185 
186 static int cb710_resume(struct pci_dev *pdev)
187 {
188 	struct cb710_chip *chip = pci_get_drvdata(pdev);
189 	int err;
190 
191 	pci_set_power_state(pdev, PCI_D0);
192 	pci_restore_state(pdev);
193 	err = pcim_enable_device(pdev);
194 	if (err)
195 		return err;
196 
197 	return devm_request_irq(&pdev->dev, pdev->irq,
198 		cb710_irq_handler, IRQF_SHARED, KBUILD_MODNAME, chip);
199 }
200 
201 #endif /* CONFIG_PM */
202 
203 static int __devinit cb710_probe(struct pci_dev *pdev,
204 	const struct pci_device_id *ent)
205 {
206 	struct cb710_chip *chip;
207 	unsigned long flags;
208 	u32 val;
209 	int err;
210 	int n = 0;
211 
212 	err = cb710_pci_configure(pdev);
213 	if (err)
214 		return err;
215 
216 	/* this is actually magic... */
217 	pci_read_config_dword(pdev, 0x48, &val);
218 	if (!(val & 0x80000000)) {
219 		pci_write_config_dword(pdev, 0x48, val|0x71000000);
220 		pci_read_config_dword(pdev, 0x48, &val);
221 	}
222 
223 	dev_dbg(&pdev->dev, "PCI config[0x48] = 0x%08X\n", val);
224 	if (!(val & 0x70000000))
225 		return -ENODEV;
226 	val = (val >> 28) & 7;
227 	if (val & CB710_SLOT_MMC)
228 		++n;
229 	if (val & CB710_SLOT_MS)
230 		++n;
231 	if (val & CB710_SLOT_SM)
232 		++n;
233 
234 	chip = devm_kzalloc(&pdev->dev,
235 		sizeof(*chip) + n * sizeof(*chip->slot), GFP_KERNEL);
236 	if (!chip)
237 		return -ENOMEM;
238 
239 	err = pcim_enable_device(pdev);
240 	if (err)
241 		return err;
242 
243 	err = pcim_iomap_regions(pdev, 0x0001, KBUILD_MODNAME);
244 	if (err)
245 		return err;
246 
247 	chip->pdev = pdev;
248 	chip->iobase = pcim_iomap_table(pdev)[0];
249 
250 	pci_set_drvdata(pdev, chip);
251 
252 	err = devm_request_irq(&pdev->dev, pdev->irq,
253 		cb710_irq_handler, IRQF_SHARED, KBUILD_MODNAME, chip);
254 	if (err)
255 		return err;
256 
257 	do {
258 		if (!ida_pre_get(&cb710_ida, GFP_KERNEL))
259 			return -ENOMEM;
260 
261 		spin_lock_irqsave(&cb710_ida_lock, flags);
262 		err = ida_get_new(&cb710_ida, &chip->platform_id);
263 		spin_unlock_irqrestore(&cb710_ida_lock, flags);
264 
265 		if (err && err != -EAGAIN)
266 			return err;
267 	} while (err);
268 
269 
270 	dev_info(&pdev->dev, "id %d, IO 0x%p, IRQ %d\n",
271 		chip->platform_id, chip->iobase, pdev->irq);
272 
273 	if (val & CB710_SLOT_MMC) {	/* MMC/SD slot */
274 		err = cb710_register_slot(chip,
275 			CB710_SLOT_MMC, 0x00, "cb710-mmc");
276 		if (err)
277 			return err;
278 	}
279 
280 	if (val & CB710_SLOT_MS) {	/* MemoryStick slot */
281 		err = cb710_register_slot(chip,
282 			CB710_SLOT_MS, 0x40, "cb710-ms");
283 		if (err)
284 			goto unreg_mmc;
285 	}
286 
287 	if (val & CB710_SLOT_SM) {	/* SmartMedia slot */
288 		err = cb710_register_slot(chip,
289 			CB710_SLOT_SM, 0x60, "cb710-sm");
290 		if (err)
291 			goto unreg_ms;
292 	}
293 
294 	return 0;
295 unreg_ms:
296 	cb710_unregister_slot(chip, CB710_SLOT_MS);
297 unreg_mmc:
298 	cb710_unregister_slot(chip, CB710_SLOT_MMC);
299 
300 #ifdef CONFIG_CB710_DEBUG_ASSUMPTIONS
301 	BUG_ON(atomic_read(&chip->slot_refs_count) != 0);
302 #endif
303 	return err;
304 }
305 
306 static void __devexit cb710_remove_one(struct pci_dev *pdev)
307 {
308 	struct cb710_chip *chip = pci_get_drvdata(pdev);
309 	unsigned long flags;
310 
311 	cb710_unregister_slot(chip, CB710_SLOT_SM);
312 	cb710_unregister_slot(chip, CB710_SLOT_MS);
313 	cb710_unregister_slot(chip, CB710_SLOT_MMC);
314 #ifdef CONFIG_CB710_DEBUG_ASSUMPTIONS
315 	BUG_ON(atomic_read(&chip->slot_refs_count) != 0);
316 #endif
317 
318 	spin_lock_irqsave(&cb710_ida_lock, flags);
319 	ida_remove(&cb710_ida, chip->platform_id);
320 	spin_unlock_irqrestore(&cb710_ida_lock, flags);
321 }
322 
323 static const struct pci_device_id cb710_pci_tbl[] = {
324 	{ PCI_VENDOR_ID_ENE, PCI_DEVICE_ID_ENE_CB710_FLASH,
325 		PCI_ANY_ID, PCI_ANY_ID, },
326 	{ 0, }
327 };
328 
329 static struct pci_driver cb710_driver = {
330 	.name = KBUILD_MODNAME,
331 	.id_table = cb710_pci_tbl,
332 	.probe = cb710_probe,
333 	.remove = __devexit_p(cb710_remove_one),
334 #ifdef CONFIG_PM
335 	.suspend = cb710_suspend,
336 	.resume = cb710_resume,
337 #endif
338 };
339 
340 static int __init cb710_init_module(void)
341 {
342 	return pci_register_driver(&cb710_driver);
343 }
344 
345 static void __exit cb710_cleanup_module(void)
346 {
347 	pci_unregister_driver(&cb710_driver);
348 	ida_destroy(&cb710_ida);
349 }
350 
351 module_init(cb710_init_module);
352 module_exit(cb710_cleanup_module);
353 
354 MODULE_AUTHOR("Michał Mirosław <mirq-linux@rere.qmqm.pl>");
355 MODULE_DESCRIPTION("ENE CB710 memory card reader driver");
356 MODULE_LICENSE("GPL");
357 MODULE_DEVICE_TABLE(pci, cb710_pci_tbl);
358