tifm: replace per-adapter kthread with freezeable workqueue
[linux-2.6.git] / drivers / misc / tifm_7xx1.c
1 /*
2  *  tifm_7xx1.c - TI FlashMedia driver
3  *
4  *  Copyright (C) 2006 Alex Dubov <oakad@yahoo.com>
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  */
11
12 #include <linux/tifm.h>
13 #include <linux/dma-mapping.h>
14 #include <linux/freezer.h>
15
16 #define DRIVER_NAME "tifm_7xx1"
17 #define DRIVER_VERSION "0.8"
18
19 #define TIFM_IRQ_ENABLE           0x80000000
20 #define TIFM_IRQ_SOCKMASK(x)      (x)
21 #define TIFM_IRQ_CARDMASK(x)      ((x) << 8)
22 #define TIFM_IRQ_FIFOMASK(x)      ((x) << 16)
23 #define TIFM_IRQ_SETALL           0xffffffff
24
25 static void tifm_7xx1_eject(struct tifm_adapter *fm, struct tifm_dev *sock)
26 {
27         unsigned long flags;
28
29         spin_lock_irqsave(&fm->lock, flags);
30         fm->socket_change_set |= 1 << sock->socket_id;
31         tifm_queue_work(&fm->media_switcher);
32         spin_unlock_irqrestore(&fm->lock, flags);
33 }
34
35 static irqreturn_t tifm_7xx1_isr(int irq, void *dev_id)
36 {
37         struct tifm_adapter *fm = dev_id;
38         struct tifm_dev *sock;
39         unsigned int irq_status;
40         unsigned int cnt;
41
42         spin_lock(&fm->lock);
43         irq_status = readl(fm->addr + FM_INTERRUPT_STATUS);
44         if (irq_status == 0 || irq_status == (~0)) {
45                 spin_unlock(&fm->lock);
46                 return IRQ_NONE;
47         }
48
49         if (irq_status & TIFM_IRQ_ENABLE) {
50                 writel(TIFM_IRQ_ENABLE, fm->addr + FM_CLEAR_INTERRUPT_ENABLE);
51
52                 for (cnt = 0; cnt < fm->num_sockets; cnt++) {
53                         sock = fm->sockets[cnt];
54                         if (sock) {
55                                 if ((irq_status >> cnt) & TIFM_IRQ_FIFOMASK(1))
56                                         sock->data_event(sock);
57                                 if ((irq_status >> cnt) & TIFM_IRQ_CARDMASK(1))
58                                         sock->card_event(sock);
59                         }
60                 }
61
62                 fm->socket_change_set |= irq_status
63                                          & ((1 << fm->num_sockets) - 1);
64         }
65         writel(irq_status, fm->addr + FM_INTERRUPT_STATUS);
66
67         if (fm->finish_me)
68                 complete_all(fm->finish_me);
69         else if (!fm->socket_change_set)
70                 writel(TIFM_IRQ_ENABLE, fm->addr + FM_SET_INTERRUPT_ENABLE);
71         else
72                 tifm_queue_work(&fm->media_switcher);
73
74         spin_unlock(&fm->lock);
75         return IRQ_HANDLED;
76 }
77
78 static unsigned char tifm_7xx1_toggle_sock_power(char __iomem *sock_addr,
79                                                  int is_x2)
80 {
81         unsigned int s_state;
82         int cnt;
83
84         writel(0x0e00, sock_addr + SOCK_CONTROL);
85
86         for (cnt = 0; cnt < 100; cnt++) {
87                 if (!(TIFM_SOCK_STATE_POWERED
88                       & readl(sock_addr + SOCK_PRESENT_STATE)))
89                         break;
90                 msleep(10);
91         }
92
93         s_state = readl(sock_addr + SOCK_PRESENT_STATE);
94         if (!(TIFM_SOCK_STATE_OCCUPIED & s_state))
95                 return 0;
96
97         if (is_x2) {
98                 writel((s_state & 7) | 0x0c00, sock_addr + SOCK_CONTROL);
99         } else {
100                 // SmartMedia cards need extra 40 msec
101                 if (((readl(sock_addr + SOCK_PRESENT_STATE) >> 4) & 7) == 1)
102                         msleep(40);
103                 writel(readl(sock_addr + SOCK_CONTROL) | TIFM_CTRL_LED,
104                        sock_addr + SOCK_CONTROL);
105                 msleep(10);
106                 writel((s_state & 0x7) | 0x0c00 | TIFM_CTRL_LED,
107                         sock_addr + SOCK_CONTROL);
108         }
109
110         for (cnt = 0; cnt < 100; cnt++) {
111                 if ((TIFM_SOCK_STATE_POWERED
112                      & readl(sock_addr + SOCK_PRESENT_STATE)))
113                         break;
114                 msleep(10);
115         }
116
117         if (!is_x2)
118                 writel(readl(sock_addr + SOCK_CONTROL) & (~TIFM_CTRL_LED),
119                        sock_addr + SOCK_CONTROL);
120
121         return (readl(sock_addr + SOCK_PRESENT_STATE) >> 4) & 7;
122 }
123
124 inline static char __iomem *
125 tifm_7xx1_sock_addr(char __iomem *base_addr, unsigned int sock_num)
126 {
127         return base_addr + ((sock_num + 1) << 10);
128 }
129
130 static void tifm_7xx1_switch_media(struct work_struct *work)
131 {
132         struct tifm_adapter *fm = container_of(work, struct tifm_adapter,
133                                                media_switcher);
134         unsigned long flags;
135         unsigned char media_id;
136         char *card_name = "xx";
137         int cnt;
138         struct tifm_dev *sock;
139         unsigned int socket_change_set;
140
141         spin_lock_irqsave(&fm->lock, flags);
142         socket_change_set = fm->socket_change_set;
143         fm->socket_change_set = 0;
144
145         dev_dbg(fm->dev, "checking media set %x\n",
146                 socket_change_set);
147
148         if (!socket_change_set) {
149                 spin_unlock_irqrestore(&fm->lock, flags);
150                 return;
151         }
152
153                 for (cnt = 0; cnt < fm->num_sockets; cnt++) {
154                         if (!(socket_change_set & (1 << cnt)))
155                                 continue;
156                         sock = fm->sockets[cnt];
157                         if (sock) {
158                                 printk(KERN_INFO DRIVER_NAME
159                                        ": demand removing card from socket %d\n",
160                                        cnt);
161                                 fm->sockets[cnt] = NULL;
162                                 spin_unlock_irqrestore(&fm->lock, flags);
163                                 device_unregister(&sock->dev);
164                                 spin_lock_irqsave(&fm->lock, flags);
165                                 writel(0x0e00,
166                                        tifm_7xx1_sock_addr(fm->addr, cnt)
167                                        + SOCK_CONTROL);
168                         }
169
170                         spin_unlock_irqrestore(&fm->lock, flags);
171                         media_id = tifm_7xx1_toggle_sock_power(
172                                         tifm_7xx1_sock_addr(fm->addr, cnt),
173                                         fm->num_sockets == 2);
174                         if (media_id) {
175                                 sock = tifm_alloc_device(fm);
176                                 if (sock) {
177                                         sock->addr = tifm_7xx1_sock_addr(fm->addr,
178                                                                          cnt);
179                                         sock->type = media_id;
180                                         sock->socket_id = cnt;
181                                         switch (media_id) {
182                                         case 1:
183                                                 card_name = "xd";
184                                                 break;
185                                         case 2:
186                                                 card_name = "ms";
187                                                 break;
188                                         case 3:
189                                                 card_name = "sd";
190                                                 break;
191                                         default:
192                                                 tifm_free_device(&sock->dev);
193                                                 spin_lock_irqsave(&fm->lock, flags);
194                                                 continue;
195                                         }
196                                         snprintf(sock->dev.bus_id, BUS_ID_SIZE,
197                                                  "tifm_%s%u:%u", card_name,
198                                                  fm->id, cnt);
199                                         printk(KERN_INFO DRIVER_NAME
200                                                ": %s card detected in socket %d\n",
201                                                card_name, cnt);
202                                         if (!device_register(&sock->dev)) {
203                                                 spin_lock_irqsave(&fm->lock, flags);
204                                                 if (!fm->sockets[cnt]) {
205                                                         fm->sockets[cnt] = sock;
206                                                         sock = NULL;
207                                                 }
208                                                 spin_unlock_irqrestore(&fm->lock, flags);
209                                         }
210                                         if (sock)
211                                                 tifm_free_device(&sock->dev);
212                                 }
213                                 spin_lock_irqsave(&fm->lock, flags);
214                         }
215                 }
216
217         writel(TIFM_IRQ_FIFOMASK(socket_change_set)
218                | TIFM_IRQ_CARDMASK(socket_change_set),
219                fm->addr + FM_CLEAR_INTERRUPT_ENABLE);
220
221         writel(TIFM_IRQ_FIFOMASK(socket_change_set)
222                | TIFM_IRQ_CARDMASK(socket_change_set),
223                fm->addr + FM_SET_INTERRUPT_ENABLE);
224
225         writel(TIFM_IRQ_ENABLE, fm->addr + FM_SET_INTERRUPT_ENABLE);
226         spin_unlock_irqrestore(&fm->lock, flags);
227 }
228
229 #ifdef CONFIG_PM
230
231 static int tifm_7xx1_suspend(struct pci_dev *dev, pm_message_t state)
232 {
233         dev_dbg(&dev->dev, "suspending host\n");
234
235         pci_save_state(dev);
236         pci_enable_wake(dev, pci_choose_state(dev, state), 0);
237         pci_disable_device(dev);
238         pci_set_power_state(dev, pci_choose_state(dev, state));
239         return 0;
240 }
241
242 static int tifm_7xx1_resume(struct pci_dev *dev)
243 {
244         struct tifm_adapter *fm = pci_get_drvdata(dev);
245         int cnt, rc;
246         unsigned long flags;
247         unsigned char new_ids[fm->num_sockets];
248         DECLARE_COMPLETION_ONSTACK(finish_resume);
249
250         pci_set_power_state(dev, PCI_D0);
251         pci_restore_state(dev);
252         rc = pci_enable_device(dev);
253         if (rc)
254                 return rc;
255         pci_set_master(dev);
256
257         dev_dbg(&dev->dev, "resuming host\n");
258
259         for (cnt = 0; cnt < fm->num_sockets; cnt++)
260                 new_ids[cnt] = tifm_7xx1_toggle_sock_power(
261                                         tifm_7xx1_sock_addr(fm->addr, cnt),
262                                         fm->num_sockets == 2);
263         spin_lock_irqsave(&fm->lock, flags);
264         fm->socket_change_set = 0;
265         for (cnt = 0; cnt < fm->num_sockets; cnt++) {
266                 if (fm->sockets[cnt]) {
267                         if (fm->sockets[cnt]->type == new_ids[cnt])
268                                 fm->socket_change_set |= 1 << cnt;
269
270                         fm->sockets[cnt]->type = new_ids[cnt];
271                 }
272         }
273
274         writel(TIFM_IRQ_ENABLE | TIFM_IRQ_SOCKMASK((1 << fm->num_sockets) - 1),
275                fm->addr + FM_SET_INTERRUPT_ENABLE);
276         if (!fm->socket_change_set) {
277                 spin_unlock_irqrestore(&fm->lock, flags);
278                 return 0;
279         } else {
280                 fm->socket_change_set = 0;
281                 fm->finish_me = &finish_resume;
282                 spin_unlock_irqrestore(&fm->lock, flags);
283         }
284
285         wait_for_completion_timeout(&finish_resume, HZ);
286
287         spin_lock_irqsave(&fm->lock, flags);
288         fm->finish_me = NULL;
289         writel(TIFM_IRQ_FIFOMASK(fm->socket_change_set)
290                | TIFM_IRQ_CARDMASK(fm->socket_change_set),
291                fm->addr + FM_CLEAR_INTERRUPT_ENABLE);
292         writel(TIFM_IRQ_FIFOMASK(fm->socket_change_set)
293                | TIFM_IRQ_CARDMASK(fm->socket_change_set),
294                fm->addr + FM_SET_INTERRUPT_ENABLE);
295         writel(TIFM_IRQ_ENABLE,
296                fm->addr + FM_SET_INTERRUPT_ENABLE);
297         fm->socket_change_set = 0;
298
299         spin_unlock_irqrestore(&fm->lock, flags);
300         return 0;
301 }
302
303 #else
304
305 #define tifm_7xx1_suspend NULL
306 #define tifm_7xx1_resume NULL
307
308 #endif /* CONFIG_PM */
309
310 static int tifm_7xx1_probe(struct pci_dev *dev,
311                            const struct pci_device_id *dev_id)
312 {
313         struct tifm_adapter *fm;
314         int pci_dev_busy = 0;
315         int rc;
316
317         rc = pci_set_dma_mask(dev, DMA_32BIT_MASK);
318         if (rc)
319                 return rc;
320
321         rc = pci_enable_device(dev);
322         if (rc)
323                 return rc;
324
325         pci_set_master(dev);
326
327         rc = pci_request_regions(dev, DRIVER_NAME);
328         if (rc) {
329                 pci_dev_busy = 1;
330                 goto err_out;
331         }
332
333         pci_intx(dev, 1);
334
335         fm = tifm_alloc_adapter();
336         if (!fm) {
337                 rc = -ENOMEM;
338                 goto err_out_int;
339         }
340
341         fm->dev = &dev->dev;
342         fm->num_sockets = (dev->device == PCI_DEVICE_ID_TI_XX21_XX11_FM)
343                           ? 4 : 2;
344         fm->sockets = kzalloc(sizeof(struct tifm_dev*) * fm->num_sockets,
345                               GFP_KERNEL);
346         if (!fm->sockets)
347                 goto err_out_free;
348
349         INIT_WORK(&fm->media_switcher, tifm_7xx1_switch_media);
350         fm->eject = tifm_7xx1_eject;
351         pci_set_drvdata(dev, fm);
352
353         fm->addr = ioremap(pci_resource_start(dev, 0),
354                            pci_resource_len(dev, 0));
355         if (!fm->addr)
356                 goto err_out_free;
357
358         rc = request_irq(dev->irq, tifm_7xx1_isr, IRQF_SHARED, DRIVER_NAME, fm);
359         if (rc)
360                 goto err_out_unmap;
361
362         rc = tifm_add_adapter(fm);
363         if (rc)
364                 goto err_out_irq;
365
366         writel(TIFM_IRQ_SETALL, fm->addr + FM_CLEAR_INTERRUPT_ENABLE);
367         writel(TIFM_IRQ_ENABLE | TIFM_IRQ_SOCKMASK((1 << fm->num_sockets) - 1),
368                fm->addr + FM_SET_INTERRUPT_ENABLE);
369
370         return 0;
371
372 err_out_irq:
373         free_irq(dev->irq, fm);
374 err_out_unmap:
375         iounmap(fm->addr);
376 err_out_free:
377         pci_set_drvdata(dev, NULL);
378         tifm_free_adapter(fm);
379 err_out_int:
380         pci_intx(dev, 0);
381         pci_release_regions(dev);
382 err_out:
383         if (!pci_dev_busy)
384                 pci_disable_device(dev);
385         return rc;
386 }
387
388 static void tifm_7xx1_remove(struct pci_dev *dev)
389 {
390         struct tifm_adapter *fm = pci_get_drvdata(dev);
391         unsigned long flags;
392
393         writel(TIFM_IRQ_SETALL, fm->addr + FM_CLEAR_INTERRUPT_ENABLE);
394         mmiowb();
395         free_irq(dev->irq, fm);
396
397         spin_lock_irqsave(&fm->lock, flags);
398         fm->socket_change_set = (1 << fm->num_sockets) - 1;
399         spin_unlock_irqrestore(&fm->lock, flags);
400
401         tifm_remove_adapter(fm);
402
403         pci_set_drvdata(dev, NULL);
404
405         iounmap(fm->addr);
406         pci_intx(dev, 0);
407         pci_release_regions(dev);
408
409         pci_disable_device(dev);
410         tifm_free_adapter(fm);
411 }
412
413 static struct pci_device_id tifm_7xx1_pci_tbl [] = {
414         { PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_XX21_XX11_FM, PCI_ANY_ID,
415           PCI_ANY_ID, 0, 0, 0 }, /* xx21 - the one I have */
416         { PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_XX12_FM, PCI_ANY_ID,
417           PCI_ANY_ID, 0, 0, 0 },
418         { PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_XX20_FM, PCI_ANY_ID,
419           PCI_ANY_ID, 0, 0, 0 },
420         { }
421 };
422
423 static struct pci_driver tifm_7xx1_driver = {
424         .name = DRIVER_NAME,
425         .id_table = tifm_7xx1_pci_tbl,
426         .probe = tifm_7xx1_probe,
427         .remove = tifm_7xx1_remove,
428         .suspend = tifm_7xx1_suspend,
429         .resume = tifm_7xx1_resume,
430 };
431
432 static int __init tifm_7xx1_init(void)
433 {
434         return pci_register_driver(&tifm_7xx1_driver);
435 }
436
437 static void __exit tifm_7xx1_exit(void)
438 {
439         pci_unregister_driver(&tifm_7xx1_driver);
440 }
441
442 MODULE_AUTHOR("Alex Dubov");
443 MODULE_DESCRIPTION("TI FlashMedia host driver");
444 MODULE_LICENSE("GPL");
445 MODULE_DEVICE_TABLE(pci, tifm_7xx1_pci_tbl);
446 MODULE_VERSION(DRIVER_VERSION);
447
448 module_init(tifm_7xx1_init);
449 module_exit(tifm_7xx1_exit);