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