blob: 7fbf5d58bfc506f913b8bb4c3fbc5fff41290e59 [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;
Alex Dubov217334d2006-12-11 01:55:31 +110074 struct tifm_dev *sock;
Alex Dubov4020f2d2006-10-04 02:15:37 -070075 unsigned int irq_status;
76 unsigned int sock_irq_status, cnt;
77
78 spin_lock(&fm->lock);
79 irq_status = readl(fm->addr + FM_INTERRUPT_STATUS);
80 if (irq_status == 0 || irq_status == (~0)) {
81 spin_unlock(&fm->lock);
82 return IRQ_NONE;
83 }
84
85 if (irq_status & TIFM_IRQ_ENABLE) {
86 writel(TIFM_IRQ_ENABLE, fm->addr + FM_CLEAR_INTERRUPT_ENABLE);
87
88 for (cnt = 0; cnt < fm->max_sockets; cnt++) {
Alex Dubov217334d2006-12-11 01:55:31 +110089 sock = fm->sockets[cnt];
Alex Dubov4020f2d2006-10-04 02:15:37 -070090 sock_irq_status = (irq_status >> cnt) &
91 (TIFM_IRQ_FIFOMASK | TIFM_IRQ_CARDMASK);
92
Alex Dubov217334d2006-12-11 01:55:31 +110093 if (sock) {
94 if (sock_irq_status)
95 sock->signal_irq(sock, sock_irq_status);
Alex Dubov4020f2d2006-10-04 02:15:37 -070096
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
Al Viroe069d792006-10-09 20:29:43 +0100121static tifm_media_id tifm_7xx1_toggle_sock_power(char __iomem *sock_addr, int is_x2)
Alex Dubov4020f2d2006-10-04 02:15:37 -0700122{
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
Al Viroe069d792006-10-09 20:29:43 +0100166inline static char __iomem *
167tifm_7xx1_sock_addr(char __iomem *base_addr, unsigned int sock_num)
Alex Dubov4020f2d2006-10-04 02:15:37 -0700168{
169 return base_addr + ((sock_num + 1) << 10);
170}
171
David Howellsc4028952006-11-22 14:57:56 +0000172static void tifm_7xx1_insert_media(struct work_struct *work)
Alex Dubov4020f2d2006-10-04 02:15:37 -0700173{
David Howellsc4028952006-11-22 14:57:56 +0000174 struct tifm_adapter *fm =
175 container_of(work, struct tifm_adapter, media_inserter);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700176 unsigned long flags;
177 tifm_media_id media_id;
178 char *card_name = "xx";
179 int cnt, ok_to_register;
180 unsigned int insert_mask;
Al Viroe069d792006-10-09 20:29:43 +0100181 struct tifm_dev *new_sock = NULL;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700182
183 if (!class_device_get(&fm->cdev))
184 return;
185 spin_lock_irqsave(&fm->lock, flags);
186 insert_mask = fm->insert_mask;
187 fm->insert_mask = 0;
188 if (fm->inhibit_new_cards) {
189 spin_unlock_irqrestore(&fm->lock, flags);
190 class_device_put(&fm->cdev);
191 return;
192 }
193 spin_unlock_irqrestore(&fm->lock, flags);
194
195 for (cnt = 0; cnt < fm->max_sockets; cnt++) {
196 if (!(insert_mask & (1 << cnt)))
197 continue;
198
199 media_id = tifm_7xx1_toggle_sock_power(tifm_7xx1_sock_addr(fm->addr, cnt),
200 fm->max_sockets == 2);
201 if (media_id) {
202 ok_to_register = 0;
Alex Dubov8e02f852006-12-08 16:50:51 +1100203 new_sock = tifm_alloc_device(fm);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700204 if (new_sock) {
205 new_sock->addr = tifm_7xx1_sock_addr(fm->addr,
206 cnt);
207 new_sock->media_id = media_id;
Alex Dubov8e02f852006-12-08 16:50:51 +1100208 new_sock->socket_id = cnt;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700209 switch (media_id) {
210 case 1:
211 card_name = "xd";
212 break;
213 case 2:
214 card_name = "ms";
215 break;
216 case 3:
217 card_name = "sd";
218 break;
219 default:
220 break;
221 }
222 snprintf(new_sock->dev.bus_id, BUS_ID_SIZE,
223 "tifm_%s%u:%u", card_name, fm->id, cnt);
224 printk(KERN_INFO DRIVER_NAME
225 ": %s card detected in socket %d\n",
226 card_name, cnt);
227 spin_lock_irqsave(&fm->lock, flags);
228 if (!fm->sockets[cnt]) {
229 fm->sockets[cnt] = new_sock;
230 ok_to_register = 1;
231 }
232 spin_unlock_irqrestore(&fm->lock, flags);
233 if (!ok_to_register ||
234 device_register(&new_sock->dev)) {
235 spin_lock_irqsave(&fm->lock, flags);
Al Viroe069d792006-10-09 20:29:43 +0100236 fm->sockets[cnt] = NULL;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700237 spin_unlock_irqrestore(&fm->lock,
238 flags);
239 tifm_free_device(&new_sock->dev);
240 }
241 }
242 }
243 writel((TIFM_IRQ_FIFOMASK | TIFM_IRQ_CARDMASK) << cnt,
244 fm->addr + FM_CLEAR_INTERRUPT_ENABLE);
245 writel((TIFM_IRQ_FIFOMASK | TIFM_IRQ_CARDMASK) << cnt,
246 fm->addr + FM_SET_INTERRUPT_ENABLE);
247 }
248
249 writel(TIFM_IRQ_ENABLE, fm->addr + FM_SET_INTERRUPT_ENABLE);
250 class_device_put(&fm->cdev);
251}
252
253static int tifm_7xx1_suspend(struct pci_dev *dev, pm_message_t state)
254{
255 struct tifm_adapter *fm = pci_get_drvdata(dev);
256 unsigned long flags;
257
258 spin_lock_irqsave(&fm->lock, flags);
259 fm->inhibit_new_cards = 1;
260 fm->remove_mask = 0xf;
261 fm->insert_mask = 0;
262 writel(TIFM_IRQ_ENABLE, fm->addr + FM_CLEAR_INTERRUPT_ENABLE);
263 spin_unlock_irqrestore(&fm->lock, flags);
264 flush_workqueue(fm->wq);
265
David Howellsc4028952006-11-22 14:57:56 +0000266 tifm_7xx1_remove_media(&fm->media_remover);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700267
268 pci_set_power_state(dev, PCI_D3hot);
269 pci_disable_device(dev);
270 pci_save_state(dev);
271 return 0;
272}
273
274static int tifm_7xx1_resume(struct pci_dev *dev)
275{
276 struct tifm_adapter *fm = pci_get_drvdata(dev);
277 unsigned long flags;
278
279 pci_restore_state(dev);
280 pci_enable_device(dev);
281 pci_set_power_state(dev, PCI_D0);
282 pci_set_master(dev);
283
284 spin_lock_irqsave(&fm->lock, flags);
285 fm->inhibit_new_cards = 0;
286 writel(TIFM_IRQ_SETALL, fm->addr + FM_INTERRUPT_STATUS);
287 writel(TIFM_IRQ_SETALL, fm->addr + FM_CLEAR_INTERRUPT_ENABLE);
288 writel(TIFM_IRQ_ENABLE | TIFM_IRQ_SETALLSOCK,
289 fm->addr + FM_SET_INTERRUPT_ENABLE);
290 fm->insert_mask = 0xf;
291 spin_unlock_irqrestore(&fm->lock, flags);
292 return 0;
293}
294
295static int tifm_7xx1_probe(struct pci_dev *dev,
296 const struct pci_device_id *dev_id)
297{
298 struct tifm_adapter *fm;
299 int pci_dev_busy = 0;
300 int rc;
301
302 rc = pci_set_dma_mask(dev, DMA_32BIT_MASK);
303 if (rc)
304 return rc;
305
306 rc = pci_enable_device(dev);
307 if (rc)
308 return rc;
309
310 pci_set_master(dev);
311
312 rc = pci_request_regions(dev, DRIVER_NAME);
313 if (rc) {
314 pci_dev_busy = 1;
315 goto err_out;
316 }
317
318 pci_intx(dev, 1);
319
320 fm = tifm_alloc_adapter();
321 if (!fm) {
322 rc = -ENOMEM;
323 goto err_out_int;
324 }
325
326 fm->dev = &dev->dev;
327 fm->max_sockets = (dev->device == 0x803B) ? 2 : 4;
328 fm->sockets = kzalloc(sizeof(struct tifm_dev*) * fm->max_sockets,
329 GFP_KERNEL);
330 if (!fm->sockets)
331 goto err_out_free;
332
David Howellsc4028952006-11-22 14:57:56 +0000333 INIT_WORK(&fm->media_inserter, tifm_7xx1_insert_media);
334 INIT_WORK(&fm->media_remover, tifm_7xx1_remove_media);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700335 fm->eject = tifm_7xx1_eject;
336 pci_set_drvdata(dev, fm);
337
338 fm->addr = ioremap(pci_resource_start(dev, 0),
339 pci_resource_len(dev, 0));
340 if (!fm->addr)
341 goto err_out_free;
342
343 rc = request_irq(dev->irq, tifm_7xx1_isr, SA_SHIRQ, DRIVER_NAME, fm);
344 if (rc)
345 goto err_out_unmap;
346
347 rc = tifm_add_adapter(fm);
348 if (rc)
349 goto err_out_irq;
350
351 writel(TIFM_IRQ_SETALL, fm->addr + FM_CLEAR_INTERRUPT_ENABLE);
352 writel(TIFM_IRQ_ENABLE | TIFM_IRQ_SETALLSOCK,
353 fm->addr + FM_SET_INTERRUPT_ENABLE);
354
355 fm->insert_mask = 0xf;
356
357 return 0;
358
359err_out_irq:
360 free_irq(dev->irq, fm);
361err_out_unmap:
362 iounmap(fm->addr);
363err_out_free:
364 pci_set_drvdata(dev, NULL);
365 tifm_free_adapter(fm);
366err_out_int:
367 pci_intx(dev, 0);
368 pci_release_regions(dev);
369err_out:
370 if (!pci_dev_busy)
371 pci_disable_device(dev);
372 return rc;
373}
374
375static void tifm_7xx1_remove(struct pci_dev *dev)
376{
377 struct tifm_adapter *fm = pci_get_drvdata(dev);
378 unsigned long flags;
379
380 spin_lock_irqsave(&fm->lock, flags);
381 fm->inhibit_new_cards = 1;
382 fm->remove_mask = 0xf;
383 fm->insert_mask = 0;
384 writel(TIFM_IRQ_ENABLE, fm->addr + FM_CLEAR_INTERRUPT_ENABLE);
385 spin_unlock_irqrestore(&fm->lock, flags);
386
387 flush_workqueue(fm->wq);
388
David Howellsc4028952006-11-22 14:57:56 +0000389 tifm_7xx1_remove_media(&fm->media_remover);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700390
391 writel(TIFM_IRQ_SETALL, fm->addr + FM_CLEAR_INTERRUPT_ENABLE);
392 free_irq(dev->irq, fm);
393
394 tifm_remove_adapter(fm);
395
Al Viroe069d792006-10-09 20:29:43 +0100396 pci_set_drvdata(dev, NULL);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700397
398 iounmap(fm->addr);
399 pci_intx(dev, 0);
400 pci_release_regions(dev);
401
402 pci_disable_device(dev);
403 tifm_free_adapter(fm);
404}
405
406static struct pci_device_id tifm_7xx1_pci_tbl [] = {
407 { PCI_VENDOR_ID_TI, 0x8033, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
408 0 }, /* xx21 - the one I have */
409 { PCI_VENDOR_ID_TI, 0x803B, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
410 0 }, /* xx12 - should be also supported */
411 { }
412};
413
414static struct pci_driver tifm_7xx1_driver = {
415 .name = DRIVER_NAME,
416 .id_table = tifm_7xx1_pci_tbl,
417 .probe = tifm_7xx1_probe,
418 .remove = tifm_7xx1_remove,
419 .suspend = tifm_7xx1_suspend,
420 .resume = tifm_7xx1_resume,
421};
422
423static int __init tifm_7xx1_init(void)
424{
425 return pci_register_driver(&tifm_7xx1_driver);
426}
427
428static void __exit tifm_7xx1_exit(void)
429{
430 pci_unregister_driver(&tifm_7xx1_driver);
431}
432
433MODULE_AUTHOR("Alex Dubov");
434MODULE_DESCRIPTION("TI FlashMedia host driver");
435MODULE_LICENSE("GPL");
436MODULE_DEVICE_TABLE(pci, tifm_7xx1_pci_tbl);
437MODULE_VERSION(DRIVER_VERSION);
438
439module_init(tifm_7xx1_init);
440module_exit(tifm_7xx1_exit);