blob: d3e8ff46c2378caed205c192f534d01dcc0848b9 [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>
Alex Dubov7146f0d2006-12-18 14:20:06 +110014#include <linux/freezer.h>
Alex Dubov4020f2d2006-10-04 02:15:37 -070015
16#define DRIVER_NAME "tifm_7xx1"
Alex Dubov50743f42006-12-11 01:55:30 +110017#define DRIVER_VERSION "0.7"
Alex Dubov4020f2d2006-10-04 02:15:37 -070018
19static void tifm_7xx1_eject(struct tifm_adapter *fm, struct tifm_dev *sock)
20{
Alex Dubov4020f2d2006-10-04 02:15:37 -070021 unsigned long flags;
22
23 spin_lock_irqsave(&fm->lock, flags);
Alex Dubov7146f0d2006-12-18 14:20:06 +110024 fm->socket_change_set |= 1 << sock->socket_id;
25 wake_up_all(&fm->change_set_notify);
Alex Dubov4020f2d2006-10-04 02:15:37 -070026 spin_unlock_irqrestore(&fm->lock, flags);
27}
28
David Howells7d12e782006-10-05 14:55:46 +010029static irqreturn_t tifm_7xx1_isr(int irq, void *dev_id)
Alex Dubov4020f2d2006-10-04 02:15:37 -070030{
31 struct tifm_adapter *fm = dev_id;
Alex Dubov217334d2006-12-11 01:55:31 +110032 struct tifm_dev *sock;
Alex Dubov4020f2d2006-10-04 02:15:37 -070033 unsigned int irq_status;
34 unsigned int sock_irq_status, cnt;
35
36 spin_lock(&fm->lock);
37 irq_status = readl(fm->addr + FM_INTERRUPT_STATUS);
38 if (irq_status == 0 || irq_status == (~0)) {
39 spin_unlock(&fm->lock);
40 return IRQ_NONE;
41 }
42
43 if (irq_status & TIFM_IRQ_ENABLE) {
44 writel(TIFM_IRQ_ENABLE, fm->addr + FM_CLEAR_INTERRUPT_ENABLE);
45
Alex Dubov6412d922006-12-11 01:55:33 +110046 for (cnt = 0; cnt < fm->num_sockets; cnt++) {
Alex Dubov217334d2006-12-11 01:55:31 +110047 sock = fm->sockets[cnt];
Alex Dubov6412d922006-12-11 01:55:33 +110048 sock_irq_status = (irq_status >> cnt)
49 & (TIFM_IRQ_FIFOMASK(1)
50 | TIFM_IRQ_CARDMASK(1));
Alex Dubov4020f2d2006-10-04 02:15:37 -070051
Alex Dubov6412d922006-12-11 01:55:33 +110052 if (sock && sock_irq_status)
53 sock->signal_irq(sock, sock_irq_status);
Alex Dubov4020f2d2006-10-04 02:15:37 -070054 }
Alex Dubov6412d922006-12-11 01:55:33 +110055
56 fm->socket_change_set |= irq_status
57 & ((1 << fm->num_sockets) - 1);
Alex Dubov4020f2d2006-10-04 02:15:37 -070058 }
59 writel(irq_status, fm->addr + FM_INTERRUPT_STATUS);
60
Alex Dubov7146f0d2006-12-18 14:20:06 +110061 if (!fm->socket_change_set)
62 writel(TIFM_IRQ_ENABLE, fm->addr + FM_SET_INTERRUPT_ENABLE);
63 else
64 wake_up_all(&fm->change_set_notify);
Alex Dubov4020f2d2006-10-04 02:15:37 -070065
66 spin_unlock(&fm->lock);
67 return IRQ_HANDLED;
68}
69
Al Viroe069d792006-10-09 20:29:43 +010070static tifm_media_id tifm_7xx1_toggle_sock_power(char __iomem *sock_addr, int is_x2)
Alex Dubov4020f2d2006-10-04 02:15:37 -070071{
72 unsigned int s_state;
73 int cnt;
74
75 writel(0x0e00, sock_addr + SOCK_CONTROL);
76
77 for (cnt = 0; cnt < 100; cnt++) {
78 if (!(TIFM_SOCK_STATE_POWERED &
79 readl(sock_addr + SOCK_PRESENT_STATE)))
80 break;
81 msleep(10);
82 }
83
84 s_state = readl(sock_addr + SOCK_PRESENT_STATE);
85 if (!(TIFM_SOCK_STATE_OCCUPIED & s_state))
86 return FM_NULL;
87
88 if (is_x2) {
89 writel((s_state & 7) | 0x0c00, sock_addr + SOCK_CONTROL);
90 } else {
91 // SmartMedia cards need extra 40 msec
92 if (((readl(sock_addr + SOCK_PRESENT_STATE) >> 4) & 7) == 1)
93 msleep(40);
94 writel(readl(sock_addr + SOCK_CONTROL) | TIFM_CTRL_LED,
95 sock_addr + SOCK_CONTROL);
96 msleep(10);
97 writel((s_state & 0x7) | 0x0c00 | TIFM_CTRL_LED,
98 sock_addr + SOCK_CONTROL);
99 }
100
101 for (cnt = 0; cnt < 100; cnt++) {
102 if ((TIFM_SOCK_STATE_POWERED &
103 readl(sock_addr + SOCK_PRESENT_STATE)))
104 break;
105 msleep(10);
106 }
107
108 if (!is_x2)
109 writel(readl(sock_addr + SOCK_CONTROL) & (~TIFM_CTRL_LED),
110 sock_addr + SOCK_CONTROL);
111
112 return (readl(sock_addr + SOCK_PRESENT_STATE) >> 4) & 7;
113}
114
Al Viroe069d792006-10-09 20:29:43 +0100115inline static char __iomem *
116tifm_7xx1_sock_addr(char __iomem *base_addr, unsigned int sock_num)
Alex Dubov4020f2d2006-10-04 02:15:37 -0700117{
118 return base_addr + ((sock_num + 1) << 10);
119}
120
Alex Dubov7146f0d2006-12-18 14:20:06 +1100121static int tifm_7xx1_switch_media(void *data)
Alex Dubov4020f2d2006-10-04 02:15:37 -0700122{
Alex Dubov7146f0d2006-12-18 14:20:06 +1100123 struct tifm_adapter *fm = data;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700124 unsigned long flags;
125 tifm_media_id media_id;
126 char *card_name = "xx";
Alex Dubov7146f0d2006-12-18 14:20:06 +1100127 int cnt, rc;
Alex Dubov6412d922006-12-11 01:55:33 +1100128 struct tifm_dev *sock;
129 unsigned int socket_change_set;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700130
Alex Dubov6412d922006-12-11 01:55:33 +1100131 while (1) {
Alex Dubov7146f0d2006-12-18 14:20:06 +1100132 rc = wait_event_interruptible(fm->change_set_notify,
133 fm->socket_change_set);
134 if (rc == -ERESTARTSYS)
135 try_to_freeze();
136
Alex Dubov6412d922006-12-11 01:55:33 +1100137 spin_lock_irqsave(&fm->lock, flags);
138 socket_change_set = fm->socket_change_set;
139 fm->socket_change_set = 0;
140
141 dev_dbg(fm->dev, "checking media set %x\n",
142 socket_change_set);
143
Alex Dubov7146f0d2006-12-18 14:20:06 +1100144 if (kthread_should_stop())
Alex Dubov6412d922006-12-11 01:55:33 +1100145 socket_change_set = (1 << fm->num_sockets) - 1;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700146 spin_unlock_irqrestore(&fm->lock, flags);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700147
Alex Dubov6412d922006-12-11 01:55:33 +1100148 if (!socket_change_set)
Alex Dubov7146f0d2006-12-18 14:20:06 +1100149 continue;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700150
Alex Dubov6412d922006-12-11 01:55:33 +1100151 spin_lock_irqsave(&fm->lock, flags);
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) {
Alex Dubov4020f2d2006-10-04 02:15:37 -0700157 printk(KERN_INFO DRIVER_NAME
Alex Dubov6412d922006-12-11 01:55:33 +1100158 ": demand removing card from socket %d\n",
159 cnt);
160 fm->sockets[cnt] = NULL;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700161 spin_unlock_irqrestore(&fm->lock, flags);
Alex Dubov6412d922006-12-11 01:55:33 +1100162 device_unregister(&sock->dev);
163 spin_lock_irqsave(&fm->lock, flags);
164 writel(0x0e00,
165 tifm_7xx1_sock_addr(fm->addr, cnt)
166 + SOCK_CONTROL);
167 }
Alex Dubov7146f0d2006-12-18 14:20:06 +1100168 if (kthread_should_stop())
Alex Dubov6412d922006-12-11 01:55:33 +1100169 continue;
170
171 spin_unlock_irqrestore(&fm->lock, flags);
172 media_id = tifm_7xx1_toggle_sock_power(
173 tifm_7xx1_sock_addr(fm->addr, cnt),
174 fm->num_sockets == 2);
175 if (media_id) {
176 sock = tifm_alloc_device(fm);
177 if (sock) {
178 sock->addr = tifm_7xx1_sock_addr(fm->addr,
179 cnt);
180 sock->media_id = media_id;
181 sock->socket_id = cnt;
182 switch (media_id) {
183 case 1:
184 card_name = "xd";
185 break;
186 case 2:
187 card_name = "ms";
188 break;
189 case 3:
190 card_name = "sd";
191 break;
192 default:
193 tifm_free_device(&sock->dev);
194 spin_lock_irqsave(&fm->lock, flags);
195 continue;
196 }
197 snprintf(sock->dev.bus_id, BUS_ID_SIZE,
198 "tifm_%s%u:%u", card_name, fm->id, cnt);
199 printk(KERN_INFO DRIVER_NAME
200 ": %s card detected in socket %d\n",
201 card_name, cnt);
202 if (!device_register(&sock->dev)) {
203 spin_lock_irqsave(&fm->lock, flags);
204 if (!fm->sockets[cnt]) {
205 fm->sockets[cnt] = sock;
206 sock = NULL;
207 }
208 spin_unlock_irqrestore(&fm->lock, flags);
209 }
210 if (sock)
211 tifm_free_device(&sock->dev);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700212 }
Alex Dubov6412d922006-12-11 01:55:33 +1100213 spin_lock_irqsave(&fm->lock, flags);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700214 }
215 }
Alex Dubov4020f2d2006-10-04 02:15:37 -0700216
Alex Dubov7146f0d2006-12-18 14:20:06 +1100217 if (!kthread_should_stop()) {
Alex Dubov6412d922006-12-11 01:55:33 +1100218 writel(TIFM_IRQ_FIFOMASK(socket_change_set)
219 | TIFM_IRQ_CARDMASK(socket_change_set),
220 fm->addr + FM_CLEAR_INTERRUPT_ENABLE);
221 writel(TIFM_IRQ_FIFOMASK(socket_change_set)
222 | TIFM_IRQ_CARDMASK(socket_change_set),
223 fm->addr + FM_SET_INTERRUPT_ENABLE);
224 writel(TIFM_IRQ_ENABLE,
225 fm->addr + FM_SET_INTERRUPT_ENABLE);
226 spin_unlock_irqrestore(&fm->lock, flags);
Alex Dubov6412d922006-12-11 01:55:33 +1100227 } else {
228 for (cnt = 0; cnt < fm->num_sockets; cnt++) {
229 if (fm->sockets[cnt])
230 fm->socket_change_set |= 1 << cnt;
231 }
232 if (!fm->socket_change_set) {
233 spin_unlock_irqrestore(&fm->lock, flags);
Alex Dubov7146f0d2006-12-18 14:20:06 +1100234 return 0;
Alex Dubov6412d922006-12-11 01:55:33 +1100235 } else {
236 spin_unlock_irqrestore(&fm->lock, flags);
237 }
238 }
239 }
Alex Dubov7146f0d2006-12-18 14:20:06 +1100240 return 0;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700241}
242
Alex Dubov7146f0d2006-12-18 14:20:06 +1100243#ifdef CONFIG_PM
244
Alex Dubov4020f2d2006-10-04 02:15:37 -0700245static int tifm_7xx1_suspend(struct pci_dev *dev, pm_message_t state)
246{
Alex Dubov7146f0d2006-12-18 14:20:06 +1100247 dev_dbg(&dev->dev, "suspending host\n");
Alex Dubov4020f2d2006-10-04 02:15:37 -0700248
Alex Dubov7146f0d2006-12-18 14:20:06 +1100249 pci_save_state(dev);
250 pci_enable_wake(dev, pci_choose_state(dev, state), 0);
251 pci_disable_device(dev);
252 pci_set_power_state(dev, pci_choose_state(dev, state));
Alex Dubov4020f2d2006-10-04 02:15:37 -0700253 return 0;
254}
255
256static int tifm_7xx1_resume(struct pci_dev *dev)
257{
258 struct tifm_adapter *fm = pci_get_drvdata(dev);
Alex Dubov7146f0d2006-12-18 14:20:06 +1100259 int cnt, rc;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700260 unsigned long flags;
Alex Dubov7146f0d2006-12-18 14:20:06 +1100261 tifm_media_id new_ids[fm->num_sockets];
Alex Dubov4020f2d2006-10-04 02:15:37 -0700262
Alex Dubov7146f0d2006-12-18 14:20:06 +1100263 pci_set_power_state(dev, PCI_D0);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700264 pci_restore_state(dev);
Alex Dubov7146f0d2006-12-18 14:20:06 +1100265 rc = pci_enable_device(dev);
266 if (rc)
267 return rc;
268 pci_set_master(dev);
269
270 dev_dbg(&dev->dev, "resuming host\n");
271
272 for (cnt = 0; cnt < fm->num_sockets; cnt++)
273 new_ids[cnt] = tifm_7xx1_toggle_sock_power(
274 tifm_7xx1_sock_addr(fm->addr, cnt),
275 fm->num_sockets == 2);
276 spin_lock_irqsave(&fm->lock, flags);
277 fm->socket_change_set = 0;
278 for (cnt = 0; cnt < fm->num_sockets; cnt++) {
279 if (fm->sockets[cnt]) {
280 if (fm->sockets[cnt]->media_id == new_ids[cnt])
281 fm->socket_change_set |= 1 << cnt;
282
283 fm->sockets[cnt]->media_id = new_ids[cnt];
284 }
285 }
286
287 writel(TIFM_IRQ_ENABLE | TIFM_IRQ_SOCKMASK((1 << fm->num_sockets) - 1),
288 fm->addr + FM_SET_INTERRUPT_ENABLE);
289 if (!fm->socket_change_set) {
290 spin_unlock_irqrestore(&fm->lock, flags);
291 return 0;
292 } else {
293 fm->socket_change_set = 0;
294 spin_unlock_irqrestore(&fm->lock, flags);
295 }
296
297 wait_event_timeout(fm->change_set_notify, fm->socket_change_set, HZ);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700298
299 spin_lock_irqsave(&fm->lock, flags);
Alex Dubov7146f0d2006-12-18 14:20:06 +1100300 writel(TIFM_IRQ_FIFOMASK(fm->socket_change_set)
301 | TIFM_IRQ_CARDMASK(fm->socket_change_set),
302 fm->addr + FM_CLEAR_INTERRUPT_ENABLE);
303 writel(TIFM_IRQ_FIFOMASK(fm->socket_change_set)
304 | TIFM_IRQ_CARDMASK(fm->socket_change_set),
305 fm->addr + FM_SET_INTERRUPT_ENABLE);
306 writel(TIFM_IRQ_ENABLE,
307 fm->addr + FM_SET_INTERRUPT_ENABLE);
308 fm->socket_change_set = 0;
309
Alex Dubov4020f2d2006-10-04 02:15:37 -0700310 spin_unlock_irqrestore(&fm->lock, flags);
311 return 0;
312}
313
Alex Dubov7146f0d2006-12-18 14:20:06 +1100314#else
315
316#define tifm_7xx1_suspend NULL
317#define tifm_7xx1_resume NULL
318
319#endif /* CONFIG_PM */
320
Alex Dubov4020f2d2006-10-04 02:15:37 -0700321static int tifm_7xx1_probe(struct pci_dev *dev,
322 const struct pci_device_id *dev_id)
323{
324 struct tifm_adapter *fm;
325 int pci_dev_busy = 0;
326 int rc;
327
328 rc = pci_set_dma_mask(dev, DMA_32BIT_MASK);
329 if (rc)
330 return rc;
331
332 rc = pci_enable_device(dev);
333 if (rc)
334 return rc;
335
336 pci_set_master(dev);
337
338 rc = pci_request_regions(dev, DRIVER_NAME);
339 if (rc) {
340 pci_dev_busy = 1;
341 goto err_out;
342 }
343
344 pci_intx(dev, 1);
345
346 fm = tifm_alloc_adapter();
347 if (!fm) {
348 rc = -ENOMEM;
349 goto err_out_int;
350 }
351
352 fm->dev = &dev->dev;
Alex Dubov6412d922006-12-11 01:55:33 +1100353 fm->num_sockets = (dev->device == 0x803B) ? 2 : 4;
354 fm->sockets = kzalloc(sizeof(struct tifm_dev*) * fm->num_sockets,
Alex Dubov4020f2d2006-10-04 02:15:37 -0700355 GFP_KERNEL);
356 if (!fm->sockets)
357 goto err_out_free;
358
Alex Dubov4020f2d2006-10-04 02:15:37 -0700359 fm->eject = tifm_7xx1_eject;
360 pci_set_drvdata(dev, fm);
361
362 fm->addr = ioremap(pci_resource_start(dev, 0),
363 pci_resource_len(dev, 0));
364 if (!fm->addr)
365 goto err_out_free;
366
367 rc = request_irq(dev->irq, tifm_7xx1_isr, SA_SHIRQ, DRIVER_NAME, fm);
368 if (rc)
369 goto err_out_unmap;
370
Alex Dubov7146f0d2006-12-18 14:20:06 +1100371 init_waitqueue_head(&fm->change_set_notify);
372 rc = tifm_add_adapter(fm, tifm_7xx1_switch_media);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700373 if (rc)
374 goto err_out_irq;
375
376 writel(TIFM_IRQ_SETALL, fm->addr + FM_CLEAR_INTERRUPT_ENABLE);
Alex Dubov6412d922006-12-11 01:55:33 +1100377 writel(TIFM_IRQ_ENABLE | TIFM_IRQ_SOCKMASK((1 << fm->num_sockets) - 1),
Alex Dubov4020f2d2006-10-04 02:15:37 -0700378 fm->addr + FM_SET_INTERRUPT_ENABLE);
Alex Dubov7146f0d2006-12-18 14:20:06 +1100379 wake_up_process(fm->media_switcher);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700380 return 0;
381
382err_out_irq:
383 free_irq(dev->irq, fm);
384err_out_unmap:
385 iounmap(fm->addr);
386err_out_free:
387 pci_set_drvdata(dev, NULL);
388 tifm_free_adapter(fm);
389err_out_int:
390 pci_intx(dev, 0);
391 pci_release_regions(dev);
392err_out:
393 if (!pci_dev_busy)
394 pci_disable_device(dev);
395 return rc;
396}
397
398static void tifm_7xx1_remove(struct pci_dev *dev)
399{
400 struct tifm_adapter *fm = pci_get_drvdata(dev);
401 unsigned long flags;
402
Alex Dubov7146f0d2006-12-18 14:20:06 +1100403 writel(TIFM_IRQ_SETALL, fm->addr + FM_CLEAR_INTERRUPT_ENABLE);
404 mmiowb();
405 free_irq(dev->irq, fm);
406
Alex Dubov4020f2d2006-10-04 02:15:37 -0700407 spin_lock_irqsave(&fm->lock, flags);
Alex Dubov7146f0d2006-12-18 14:20:06 +1100408 fm->socket_change_set = (1 << fm->num_sockets) - 1;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700409 spin_unlock_irqrestore(&fm->lock, flags);
410
Alex Dubov7146f0d2006-12-18 14:20:06 +1100411 kthread_stop(fm->media_switcher);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700412
413 tifm_remove_adapter(fm);
414
Al Viroe069d792006-10-09 20:29:43 +0100415 pci_set_drvdata(dev, NULL);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700416
417 iounmap(fm->addr);
418 pci_intx(dev, 0);
419 pci_release_regions(dev);
420
421 pci_disable_device(dev);
422 tifm_free_adapter(fm);
423}
424
425static struct pci_device_id tifm_7xx1_pci_tbl [] = {
426 { PCI_VENDOR_ID_TI, 0x8033, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
427 0 }, /* xx21 - the one I have */
428 { PCI_VENDOR_ID_TI, 0x803B, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
429 0 }, /* xx12 - should be also supported */
430 { }
431};
432
433static struct pci_driver tifm_7xx1_driver = {
434 .name = DRIVER_NAME,
435 .id_table = tifm_7xx1_pci_tbl,
436 .probe = tifm_7xx1_probe,
437 .remove = tifm_7xx1_remove,
438 .suspend = tifm_7xx1_suspend,
439 .resume = tifm_7xx1_resume,
440};
441
442static int __init tifm_7xx1_init(void)
443{
444 return pci_register_driver(&tifm_7xx1_driver);
445}
446
447static void __exit tifm_7xx1_exit(void)
448{
449 pci_unregister_driver(&tifm_7xx1_driver);
450}
451
452MODULE_AUTHOR("Alex Dubov");
453MODULE_DESCRIPTION("TI FlashMedia host driver");
454MODULE_LICENSE("GPL");
455MODULE_DEVICE_TABLE(pci, tifm_7xx1_pci_tbl);
456MODULE_VERSION(DRIVER_VERSION);
457
458module_init(tifm_7xx1_init);
459module_exit(tifm_7xx1_exit);