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