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>
15 #define DRIVER_NAME "tifm_7xx1"
16 #define DRIVER_VERSION "0.6"
18 static void tifm_7xx1_eject(struct tifm_adapter *fm, struct tifm_dev *sock)
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);
33 spin_unlock_irqrestore(&fm->lock, flags);
36 static void tifm_7xx1_remove_media(void *adapter)
38 struct tifm_adapter *fm = adapter;
41 struct tifm_dev *sock;
43 if (!class_device_get(&fm->cdev))
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];
52 fm->remove_mask &= ~(1 << cnt);
54 writel(0x0e00, sock->addr + SOCK_CONTROL);
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);
61 spin_unlock_irqrestore(&fm->lock, flags);
62 device_unregister(&sock->dev);
63 spin_lock_irqsave(&fm->lock, flags);
66 spin_unlock_irqrestore(&fm->lock, flags);
67 class_device_put(&fm->cdev);
70 static irqreturn_t tifm_7xx1_isr(int irq, void *dev_id, struct pt_regs *regs)
72 struct tifm_adapter *fm = dev_id;
73 unsigned int irq_status;
74 unsigned int sock_irq_status, cnt;
77 irq_status = readl(fm->addr + FM_INTERRUPT_STATUS);
78 if (irq_status == 0 || irq_status == (~0)) {
79 spin_unlock(&fm->lock);
83 if (irq_status & TIFM_IRQ_ENABLE) {
84 writel(TIFM_IRQ_ENABLE, fm->addr + FM_CLEAR_INTERRUPT_ENABLE);
86 for (cnt = 0; cnt < fm->max_sockets; cnt++) {
87 sock_irq_status = (irq_status >> cnt) &
88 (TIFM_IRQ_FIFOMASK | TIFM_IRQ_CARDMASK);
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],
97 if (irq_status & (1 << cnt))
98 fm->remove_mask |= 1 << cnt;
100 if (irq_status & (1 << cnt))
101 fm->insert_mask |= 1 << cnt;
105 writel(irq_status, fm->addr + FM_INTERRUPT_STATUS);
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);
112 queue_work(fm->wq, &fm->media_remover);
113 queue_work(fm->wq, &fm->media_inserter);
117 spin_unlock(&fm->lock);
121 static tifm_media_id tifm_7xx1_toggle_sock_power(char *sock_addr, int is_x2)
123 unsigned int s_state;
126 writel(0x0e00, sock_addr + SOCK_CONTROL);
128 for (cnt = 0; cnt < 100; cnt++) {
129 if (!(TIFM_SOCK_STATE_POWERED &
130 readl(sock_addr + SOCK_PRESENT_STATE)))
135 s_state = readl(sock_addr + SOCK_PRESENT_STATE);
136 if (!(TIFM_SOCK_STATE_OCCUPIED & s_state))
140 writel((s_state & 7) | 0x0c00, sock_addr + SOCK_CONTROL);
142 // SmartMedia cards need extra 40 msec
143 if (((readl(sock_addr + SOCK_PRESENT_STATE) >> 4) & 7) == 1)
145 writel(readl(sock_addr + SOCK_CONTROL) | TIFM_CTRL_LED,
146 sock_addr + SOCK_CONTROL);
148 writel((s_state & 0x7) | 0x0c00 | TIFM_CTRL_LED,
149 sock_addr + SOCK_CONTROL);
152 for (cnt = 0; cnt < 100; cnt++) {
153 if ((TIFM_SOCK_STATE_POWERED &
154 readl(sock_addr + SOCK_PRESENT_STATE)))
160 writel(readl(sock_addr + SOCK_CONTROL) & (~TIFM_CTRL_LED),
161 sock_addr + SOCK_CONTROL);
163 return (readl(sock_addr + SOCK_PRESENT_STATE) >> 4) & 7;
166 inline static char *tifm_7xx1_sock_addr(char *base_addr, unsigned int sock_num)
168 return base_addr + ((sock_num + 1) << 10);
171 static void tifm_7xx1_insert_media(void *adapter)
173 struct tifm_adapter *fm = adapter;
175 tifm_media_id media_id;
176 char *card_name = "xx";
177 int cnt, ok_to_register;
178 unsigned int insert_mask;
179 struct tifm_dev *new_sock = 0;
181 if (!class_device_get(&fm->cdev))
183 spin_lock_irqsave(&fm->lock, flags);
184 insert_mask = fm->insert_mask;
186 if (fm->inhibit_new_cards) {
187 spin_unlock_irqrestore(&fm->lock, flags);
188 class_device_put(&fm->cdev);
191 spin_unlock_irqrestore(&fm->lock, flags);
193 for (cnt = 0; cnt < fm->max_sockets; cnt++) {
194 if (!(insert_mask & (1 << cnt)))
197 media_id = tifm_7xx1_toggle_sock_power(tifm_7xx1_sock_addr(fm->addr, cnt),
198 fm->max_sockets == 2);
201 new_sock = tifm_alloc_device(fm, cnt);
203 new_sock->addr = tifm_7xx1_sock_addr(fm->addr,
205 new_sock->media_id = media_id;
219 snprintf(new_sock->dev.bus_id, BUS_ID_SIZE,
220 "tifm_%s%u:%u", card_name, fm->id, cnt);
221 printk(KERN_INFO DRIVER_NAME
222 ": %s card detected in socket %d\n",
224 spin_lock_irqsave(&fm->lock, flags);
225 if (!fm->sockets[cnt]) {
226 fm->sockets[cnt] = new_sock;
229 spin_unlock_irqrestore(&fm->lock, flags);
230 if (!ok_to_register ||
231 device_register(&new_sock->dev)) {
232 spin_lock_irqsave(&fm->lock, flags);
233 fm->sockets[cnt] = 0;
234 spin_unlock_irqrestore(&fm->lock,
236 tifm_free_device(&new_sock->dev);
240 writel((TIFM_IRQ_FIFOMASK | TIFM_IRQ_CARDMASK) << cnt,
241 fm->addr + FM_CLEAR_INTERRUPT_ENABLE);
242 writel((TIFM_IRQ_FIFOMASK | TIFM_IRQ_CARDMASK) << cnt,
243 fm->addr + FM_SET_INTERRUPT_ENABLE);
246 writel(TIFM_IRQ_ENABLE, fm->addr + FM_SET_INTERRUPT_ENABLE);
247 class_device_put(&fm->cdev);
250 static int tifm_7xx1_suspend(struct pci_dev *dev, pm_message_t state)
252 struct tifm_adapter *fm = pci_get_drvdata(dev);
255 spin_lock_irqsave(&fm->lock, flags);
256 fm->inhibit_new_cards = 1;
257 fm->remove_mask = 0xf;
259 writel(TIFM_IRQ_ENABLE, fm->addr + FM_CLEAR_INTERRUPT_ENABLE);
260 spin_unlock_irqrestore(&fm->lock, flags);
261 flush_workqueue(fm->wq);
263 tifm_7xx1_remove_media(fm);
265 pci_set_power_state(dev, PCI_D3hot);
266 pci_disable_device(dev);
271 static int tifm_7xx1_resume(struct pci_dev *dev)
273 struct tifm_adapter *fm = pci_get_drvdata(dev);
276 pci_restore_state(dev);
277 pci_enable_device(dev);
278 pci_set_power_state(dev, PCI_D0);
281 spin_lock_irqsave(&fm->lock, flags);
282 fm->inhibit_new_cards = 0;
283 writel(TIFM_IRQ_SETALL, fm->addr + FM_INTERRUPT_STATUS);
284 writel(TIFM_IRQ_SETALL, fm->addr + FM_CLEAR_INTERRUPT_ENABLE);
285 writel(TIFM_IRQ_ENABLE | TIFM_IRQ_SETALLSOCK,
286 fm->addr + FM_SET_INTERRUPT_ENABLE);
287 fm->insert_mask = 0xf;
288 spin_unlock_irqrestore(&fm->lock, flags);
292 static int tifm_7xx1_probe(struct pci_dev *dev,
293 const struct pci_device_id *dev_id)
295 struct tifm_adapter *fm;
296 int pci_dev_busy = 0;
299 rc = pci_set_dma_mask(dev, DMA_32BIT_MASK);
303 rc = pci_enable_device(dev);
309 rc = pci_request_regions(dev, DRIVER_NAME);
317 fm = tifm_alloc_adapter();
324 fm->max_sockets = (dev->device == 0x803B) ? 2 : 4;
325 fm->sockets = kzalloc(sizeof(struct tifm_dev*) * fm->max_sockets,
330 INIT_WORK(&fm->media_inserter, tifm_7xx1_insert_media, fm);
331 INIT_WORK(&fm->media_remover, tifm_7xx1_remove_media, fm);
332 fm->eject = tifm_7xx1_eject;
333 pci_set_drvdata(dev, fm);
335 fm->addr = ioremap(pci_resource_start(dev, 0),
336 pci_resource_len(dev, 0));
340 rc = request_irq(dev->irq, tifm_7xx1_isr, SA_SHIRQ, DRIVER_NAME, fm);
344 rc = tifm_add_adapter(fm);
348 writel(TIFM_IRQ_SETALL, fm->addr + FM_CLEAR_INTERRUPT_ENABLE);
349 writel(TIFM_IRQ_ENABLE | TIFM_IRQ_SETALLSOCK,
350 fm->addr + FM_SET_INTERRUPT_ENABLE);
352 fm->insert_mask = 0xf;
357 free_irq(dev->irq, fm);
361 pci_set_drvdata(dev, NULL);
362 tifm_free_adapter(fm);
365 pci_release_regions(dev);
368 pci_disable_device(dev);
372 static void tifm_7xx1_remove(struct pci_dev *dev)
374 struct tifm_adapter *fm = pci_get_drvdata(dev);
377 spin_lock_irqsave(&fm->lock, flags);
378 fm->inhibit_new_cards = 1;
379 fm->remove_mask = 0xf;
381 writel(TIFM_IRQ_ENABLE, fm->addr + FM_CLEAR_INTERRUPT_ENABLE);
382 spin_unlock_irqrestore(&fm->lock, flags);
384 flush_workqueue(fm->wq);
386 tifm_7xx1_remove_media(fm);
388 writel(TIFM_IRQ_SETALL, fm->addr + FM_CLEAR_INTERRUPT_ENABLE);
389 free_irq(dev->irq, fm);
391 tifm_remove_adapter(fm);
393 pci_set_drvdata(dev, 0);
397 pci_release_regions(dev);
399 pci_disable_device(dev);
400 tifm_free_adapter(fm);
403 static struct pci_device_id tifm_7xx1_pci_tbl [] = {
404 { PCI_VENDOR_ID_TI, 0x8033, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
405 0 }, /* xx21 - the one I have */
406 { PCI_VENDOR_ID_TI, 0x803B, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
407 0 }, /* xx12 - should be also supported */
411 static struct pci_driver tifm_7xx1_driver = {
413 .id_table = tifm_7xx1_pci_tbl,
414 .probe = tifm_7xx1_probe,
415 .remove = tifm_7xx1_remove,
416 .suspend = tifm_7xx1_suspend,
417 .resume = tifm_7xx1_resume,
420 static int __init tifm_7xx1_init(void)
422 return pci_register_driver(&tifm_7xx1_driver);
425 static void __exit tifm_7xx1_exit(void)
427 pci_unregister_driver(&tifm_7xx1_driver);
430 MODULE_AUTHOR("Alex Dubov");
431 MODULE_DESCRIPTION("TI FlashMedia host driver");
432 MODULE_LICENSE("GPL");
433 MODULE_DEVICE_TABLE(pci, tifm_7xx1_pci_tbl);
434 MODULE_VERSION(DRIVER_VERSION);
436 module_init(tifm_7xx1_init);
437 module_exit(tifm_7xx1_exit);