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