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