2 * tifm_7xx1.c - TI FlashMedia driver
4 * Copyright (C) 2006 Alex Dubov <oakad@yahoo.com>
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.
12 #include <linux/tifm.h>
13 #include <linux/dma-mapping.h>
14 #include <linux/freezer.h>
16 #define DRIVER_NAME "tifm_7xx1"
17 #define DRIVER_VERSION "0.8"
19 #define TIFM_IRQ_ENABLE 0x80000000
20 #define TIFM_IRQ_SOCKMASK(x) (x)
21 #define TIFM_IRQ_CARDMASK(x) ((x) << 8)
22 #define TIFM_IRQ_FIFOMASK(x) ((x) << 16)
23 #define TIFM_IRQ_SETALL 0xffffffff
25 static void tifm_7xx1_eject(struct tifm_adapter *fm, struct tifm_dev *sock)
29 spin_lock_irqsave(&fm->lock, flags);
30 fm->socket_change_set |= 1 << sock->socket_id;
31 wake_up_all(&fm->change_set_notify);
32 spin_unlock_irqrestore(&fm->lock, flags);
35 static irqreturn_t tifm_7xx1_isr(int irq, void *dev_id)
37 struct tifm_adapter *fm = dev_id;
38 struct tifm_dev *sock;
39 unsigned int irq_status;
43 irq_status = readl(fm->addr + FM_INTERRUPT_STATUS);
44 if (irq_status == 0 || irq_status == (~0)) {
45 spin_unlock(&fm->lock);
49 if (irq_status & TIFM_IRQ_ENABLE) {
50 writel(TIFM_IRQ_ENABLE, fm->addr + FM_CLEAR_INTERRUPT_ENABLE);
52 for (cnt = 0; cnt < fm->num_sockets; cnt++) {
53 sock = fm->sockets[cnt];
55 if ((irq_status >> cnt) & TIFM_IRQ_FIFOMASK(1))
56 sock->data_event(sock);
57 if ((irq_status >> cnt) & TIFM_IRQ_CARDMASK(1))
58 sock->card_event(sock);
62 fm->socket_change_set |= irq_status
63 & ((1 << fm->num_sockets) - 1);
65 writel(irq_status, fm->addr + FM_INTERRUPT_STATUS);
67 if (!fm->socket_change_set)
68 writel(TIFM_IRQ_ENABLE, fm->addr + FM_SET_INTERRUPT_ENABLE);
70 wake_up_all(&fm->change_set_notify);
72 spin_unlock(&fm->lock);
76 static unsigned char tifm_7xx1_toggle_sock_power(char __iomem *sock_addr,
82 writel(0x0e00, sock_addr + SOCK_CONTROL);
84 for (cnt = 0; cnt < 100; cnt++) {
85 if (!(TIFM_SOCK_STATE_POWERED
86 & readl(sock_addr + SOCK_PRESENT_STATE)))
91 s_state = readl(sock_addr + SOCK_PRESENT_STATE);
92 if (!(TIFM_SOCK_STATE_OCCUPIED & s_state))
96 writel((s_state & 7) | 0x0c00, sock_addr + SOCK_CONTROL);
98 // SmartMedia cards need extra 40 msec
99 if (((readl(sock_addr + SOCK_PRESENT_STATE) >> 4) & 7) == 1)
101 writel(readl(sock_addr + SOCK_CONTROL) | TIFM_CTRL_LED,
102 sock_addr + SOCK_CONTROL);
104 writel((s_state & 0x7) | 0x0c00 | TIFM_CTRL_LED,
105 sock_addr + SOCK_CONTROL);
108 for (cnt = 0; cnt < 100; cnt++) {
109 if ((TIFM_SOCK_STATE_POWERED
110 & readl(sock_addr + SOCK_PRESENT_STATE)))
116 writel(readl(sock_addr + SOCK_CONTROL) & (~TIFM_CTRL_LED),
117 sock_addr + SOCK_CONTROL);
119 return (readl(sock_addr + SOCK_PRESENT_STATE) >> 4) & 7;
122 inline static char __iomem *
123 tifm_7xx1_sock_addr(char __iomem *base_addr, unsigned int sock_num)
125 return base_addr + ((sock_num + 1) << 10);
128 static int tifm_7xx1_switch_media(void *data)
130 struct tifm_adapter *fm = data;
132 unsigned char media_id;
133 char *card_name = "xx";
135 struct tifm_dev *sock;
136 unsigned int socket_change_set;
139 rc = wait_event_interruptible(fm->change_set_notify,
140 fm->socket_change_set);
141 if (rc == -ERESTARTSYS)
144 spin_lock_irqsave(&fm->lock, flags);
145 socket_change_set = fm->socket_change_set;
146 fm->socket_change_set = 0;
148 dev_dbg(fm->dev, "checking media set %x\n",
151 if (kthread_should_stop())
152 socket_change_set = (1 << fm->num_sockets) - 1;
153 spin_unlock_irqrestore(&fm->lock, flags);
155 if (!socket_change_set)
158 spin_lock_irqsave(&fm->lock, flags);
159 for (cnt = 0; cnt < fm->num_sockets; cnt++) {
160 if (!(socket_change_set & (1 << cnt)))
162 sock = fm->sockets[cnt];
164 printk(KERN_INFO DRIVER_NAME
165 ": demand removing card from socket %d\n",
167 fm->sockets[cnt] = NULL;
168 spin_unlock_irqrestore(&fm->lock, flags);
169 device_unregister(&sock->dev);
170 spin_lock_irqsave(&fm->lock, flags);
172 tifm_7xx1_sock_addr(fm->addr, cnt)
175 if (kthread_should_stop())
178 spin_unlock_irqrestore(&fm->lock, flags);
179 media_id = tifm_7xx1_toggle_sock_power(
180 tifm_7xx1_sock_addr(fm->addr, cnt),
181 fm->num_sockets == 2);
183 sock = tifm_alloc_device(fm);
185 sock->addr = tifm_7xx1_sock_addr(fm->addr,
187 sock->type = media_id;
188 sock->socket_id = cnt;
200 tifm_free_device(&sock->dev);
201 spin_lock_irqsave(&fm->lock, flags);
204 snprintf(sock->dev.bus_id, BUS_ID_SIZE,
205 "tifm_%s%u:%u", card_name,
207 printk(KERN_INFO DRIVER_NAME
208 ": %s card detected in socket %d\n",
210 if (!device_register(&sock->dev)) {
211 spin_lock_irqsave(&fm->lock, flags);
212 if (!fm->sockets[cnt]) {
213 fm->sockets[cnt] = sock;
216 spin_unlock_irqrestore(&fm->lock, flags);
219 tifm_free_device(&sock->dev);
221 spin_lock_irqsave(&fm->lock, flags);
225 if (!kthread_should_stop()) {
226 writel(TIFM_IRQ_FIFOMASK(socket_change_set)
227 | TIFM_IRQ_CARDMASK(socket_change_set),
228 fm->addr + FM_CLEAR_INTERRUPT_ENABLE);
229 writel(TIFM_IRQ_FIFOMASK(socket_change_set)
230 | TIFM_IRQ_CARDMASK(socket_change_set),
231 fm->addr + FM_SET_INTERRUPT_ENABLE);
232 writel(TIFM_IRQ_ENABLE,
233 fm->addr + FM_SET_INTERRUPT_ENABLE);
234 spin_unlock_irqrestore(&fm->lock, flags);
236 for (cnt = 0; cnt < fm->num_sockets; cnt++) {
237 if (fm->sockets[cnt])
238 fm->socket_change_set |= 1 << cnt;
240 if (!fm->socket_change_set) {
241 spin_unlock_irqrestore(&fm->lock, flags);
244 spin_unlock_irqrestore(&fm->lock, flags);
253 static int tifm_7xx1_suspend(struct pci_dev *dev, pm_message_t state)
255 dev_dbg(&dev->dev, "suspending host\n");
258 pci_enable_wake(dev, pci_choose_state(dev, state), 0);
259 pci_disable_device(dev);
260 pci_set_power_state(dev, pci_choose_state(dev, state));
264 static int tifm_7xx1_resume(struct pci_dev *dev)
266 struct tifm_adapter *fm = pci_get_drvdata(dev);
269 unsigned char new_ids[fm->num_sockets];
271 pci_set_power_state(dev, PCI_D0);
272 pci_restore_state(dev);
273 rc = pci_enable_device(dev);
278 dev_dbg(&dev->dev, "resuming host\n");
280 for (cnt = 0; cnt < fm->num_sockets; cnt++)
281 new_ids[cnt] = tifm_7xx1_toggle_sock_power(
282 tifm_7xx1_sock_addr(fm->addr, cnt),
283 fm->num_sockets == 2);
284 spin_lock_irqsave(&fm->lock, flags);
285 fm->socket_change_set = 0;
286 for (cnt = 0; cnt < fm->num_sockets; cnt++) {
287 if (fm->sockets[cnt]) {
288 if (fm->sockets[cnt]->type == new_ids[cnt])
289 fm->socket_change_set |= 1 << cnt;
291 fm->sockets[cnt]->type = new_ids[cnt];
295 writel(TIFM_IRQ_ENABLE | TIFM_IRQ_SOCKMASK((1 << fm->num_sockets) - 1),
296 fm->addr + FM_SET_INTERRUPT_ENABLE);
297 if (!fm->socket_change_set) {
298 spin_unlock_irqrestore(&fm->lock, flags);
301 fm->socket_change_set = 0;
302 spin_unlock_irqrestore(&fm->lock, flags);
305 wait_event_timeout(fm->change_set_notify, fm->socket_change_set, HZ);
307 spin_lock_irqsave(&fm->lock, flags);
308 writel(TIFM_IRQ_FIFOMASK(fm->socket_change_set)
309 | TIFM_IRQ_CARDMASK(fm->socket_change_set),
310 fm->addr + FM_CLEAR_INTERRUPT_ENABLE);
311 writel(TIFM_IRQ_FIFOMASK(fm->socket_change_set)
312 | TIFM_IRQ_CARDMASK(fm->socket_change_set),
313 fm->addr + FM_SET_INTERRUPT_ENABLE);
314 writel(TIFM_IRQ_ENABLE,
315 fm->addr + FM_SET_INTERRUPT_ENABLE);
316 fm->socket_change_set = 0;
318 spin_unlock_irqrestore(&fm->lock, flags);
324 #define tifm_7xx1_suspend NULL
325 #define tifm_7xx1_resume NULL
327 #endif /* CONFIG_PM */
329 static int tifm_7xx1_probe(struct pci_dev *dev,
330 const struct pci_device_id *dev_id)
332 struct tifm_adapter *fm;
333 int pci_dev_busy = 0;
336 rc = pci_set_dma_mask(dev, DMA_32BIT_MASK);
340 rc = pci_enable_device(dev);
346 rc = pci_request_regions(dev, DRIVER_NAME);
354 fm = tifm_alloc_adapter();
361 fm->num_sockets = (dev->device == PCI_DEVICE_ID_TI_XX21_XX11_FM)
363 fm->sockets = kzalloc(sizeof(struct tifm_dev*) * fm->num_sockets,
368 fm->eject = tifm_7xx1_eject;
369 pci_set_drvdata(dev, fm);
371 fm->addr = ioremap(pci_resource_start(dev, 0),
372 pci_resource_len(dev, 0));
376 rc = request_irq(dev->irq, tifm_7xx1_isr, IRQF_SHARED, DRIVER_NAME, fm);
380 init_waitqueue_head(&fm->change_set_notify);
381 rc = tifm_add_adapter(fm, tifm_7xx1_switch_media);
385 writel(TIFM_IRQ_SETALL, fm->addr + FM_CLEAR_INTERRUPT_ENABLE);
386 writel(TIFM_IRQ_ENABLE | TIFM_IRQ_SOCKMASK((1 << fm->num_sockets) - 1),
387 fm->addr + FM_SET_INTERRUPT_ENABLE);
388 wake_up_process(fm->media_switcher);
392 free_irq(dev->irq, fm);
396 pci_set_drvdata(dev, NULL);
397 tifm_free_adapter(fm);
400 pci_release_regions(dev);
403 pci_disable_device(dev);
407 static void tifm_7xx1_remove(struct pci_dev *dev)
409 struct tifm_adapter *fm = pci_get_drvdata(dev);
412 writel(TIFM_IRQ_SETALL, fm->addr + FM_CLEAR_INTERRUPT_ENABLE);
414 free_irq(dev->irq, fm);
416 spin_lock_irqsave(&fm->lock, flags);
417 fm->socket_change_set = (1 << fm->num_sockets) - 1;
418 spin_unlock_irqrestore(&fm->lock, flags);
420 kthread_stop(fm->media_switcher);
422 tifm_remove_adapter(fm);
424 pci_set_drvdata(dev, NULL);
428 pci_release_regions(dev);
430 pci_disable_device(dev);
431 tifm_free_adapter(fm);
434 static struct pci_device_id tifm_7xx1_pci_tbl [] = {
435 { PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_XX21_XX11_FM, PCI_ANY_ID,
436 PCI_ANY_ID, 0, 0, 0 }, /* xx21 - the one I have */
437 { PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_XX12_FM, PCI_ANY_ID,
438 PCI_ANY_ID, 0, 0, 0 },
439 { PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_XX20_FM, PCI_ANY_ID,
440 PCI_ANY_ID, 0, 0, 0 },
444 static struct pci_driver tifm_7xx1_driver = {
446 .id_table = tifm_7xx1_pci_tbl,
447 .probe = tifm_7xx1_probe,
448 .remove = tifm_7xx1_remove,
449 .suspend = tifm_7xx1_suspend,
450 .resume = tifm_7xx1_resume,
453 static int __init tifm_7xx1_init(void)
455 return pci_register_driver(&tifm_7xx1_driver);
458 static void __exit tifm_7xx1_exit(void)
460 pci_unregister_driver(&tifm_7xx1_driver);
463 MODULE_AUTHOR("Alex Dubov");
464 MODULE_DESCRIPTION("TI FlashMedia host driver");
465 MODULE_LICENSE("GPL");
466 MODULE_DEVICE_TABLE(pci, tifm_7xx1_pci_tbl);
467 MODULE_VERSION(DRIVER_VERSION);
469 module_init(tifm_7xx1_init);
470 module_exit(tifm_7xx1_exit);