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(struct work_struct *work)
38 struct tifm_adapter *fm =
39 container_of(work, struct tifm_adapter, media_remover);
42 struct tifm_dev *sock;
44 if (!class_device_get(&fm->cdev))
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];
52 fm->sockets[cnt] = NULL;
53 fm->remove_mask &= ~(1 << cnt);
55 writel(0x0e00, sock->addr + SOCK_CONTROL);
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);
62 spin_unlock_irqrestore(&fm->lock, flags);
63 device_unregister(&sock->dev);
64 spin_lock_irqsave(&fm->lock, flags);
67 spin_unlock_irqrestore(&fm->lock, flags);
68 class_device_put(&fm->cdev);
71 static irqreturn_t tifm_7xx1_isr(int irq, void *dev_id)
73 struct tifm_adapter *fm = dev_id;
74 unsigned int irq_status;
75 unsigned int sock_irq_status, cnt;
78 irq_status = readl(fm->addr + FM_INTERRUPT_STATUS);
79 if (irq_status == 0 || irq_status == (~0)) {
80 spin_unlock(&fm->lock);
84 if (irq_status & TIFM_IRQ_ENABLE) {
85 writel(TIFM_IRQ_ENABLE, fm->addr + FM_CLEAR_INTERRUPT_ENABLE);
87 for (cnt = 0; cnt < fm->max_sockets; cnt++) {
88 sock_irq_status = (irq_status >> cnt) &
89 (TIFM_IRQ_FIFOMASK | TIFM_IRQ_CARDMASK);
91 if (fm->sockets[cnt]) {
92 if (sock_irq_status &&
93 fm->sockets[cnt]->signal_irq)
94 sock_irq_status = fm->sockets[cnt]->
95 signal_irq(fm->sockets[cnt],
98 if (irq_status & (1 << cnt))
99 fm->remove_mask |= 1 << cnt;
101 if (irq_status & (1 << cnt))
102 fm->insert_mask |= 1 << cnt;
106 writel(irq_status, fm->addr + FM_INTERRUPT_STATUS);
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);
113 queue_work(fm->wq, &fm->media_remover);
114 queue_work(fm->wq, &fm->media_inserter);
118 spin_unlock(&fm->lock);
122 static tifm_media_id tifm_7xx1_toggle_sock_power(char __iomem *sock_addr, int is_x2)
124 unsigned int s_state;
127 writel(0x0e00, sock_addr + SOCK_CONTROL);
129 for (cnt = 0; cnt < 100; cnt++) {
130 if (!(TIFM_SOCK_STATE_POWERED &
131 readl(sock_addr + SOCK_PRESENT_STATE)))
136 s_state = readl(sock_addr + SOCK_PRESENT_STATE);
137 if (!(TIFM_SOCK_STATE_OCCUPIED & s_state))
141 writel((s_state & 7) | 0x0c00, sock_addr + SOCK_CONTROL);
143 // SmartMedia cards need extra 40 msec
144 if (((readl(sock_addr + SOCK_PRESENT_STATE) >> 4) & 7) == 1)
146 writel(readl(sock_addr + SOCK_CONTROL) | TIFM_CTRL_LED,
147 sock_addr + SOCK_CONTROL);
149 writel((s_state & 0x7) | 0x0c00 | TIFM_CTRL_LED,
150 sock_addr + SOCK_CONTROL);
153 for (cnt = 0; cnt < 100; cnt++) {
154 if ((TIFM_SOCK_STATE_POWERED &
155 readl(sock_addr + SOCK_PRESENT_STATE)))
161 writel(readl(sock_addr + SOCK_CONTROL) & (~TIFM_CTRL_LED),
162 sock_addr + SOCK_CONTROL);
164 return (readl(sock_addr + SOCK_PRESENT_STATE) >> 4) & 7;
167 inline static char __iomem *
168 tifm_7xx1_sock_addr(char __iomem *base_addr, unsigned int sock_num)
170 return base_addr + ((sock_num + 1) << 10);
173 static void tifm_7xx1_insert_media(struct work_struct *work)
175 struct tifm_adapter *fm =
176 container_of(work, struct tifm_adapter, media_inserter);
178 tifm_media_id media_id;
179 char *card_name = "xx";
180 int cnt, ok_to_register;
181 unsigned int insert_mask;
182 struct tifm_dev *new_sock = NULL;
184 if (!class_device_get(&fm->cdev))
186 spin_lock_irqsave(&fm->lock, flags);
187 insert_mask = fm->insert_mask;
189 if (fm->inhibit_new_cards) {
190 spin_unlock_irqrestore(&fm->lock, flags);
191 class_device_put(&fm->cdev);
194 spin_unlock_irqrestore(&fm->lock, flags);
196 for (cnt = 0; cnt < fm->max_sockets; cnt++) {
197 if (!(insert_mask & (1 << cnt)))
200 media_id = tifm_7xx1_toggle_sock_power(tifm_7xx1_sock_addr(fm->addr, cnt),
201 fm->max_sockets == 2);
204 new_sock = tifm_alloc_device(fm, cnt);
206 new_sock->addr = tifm_7xx1_sock_addr(fm->addr,
208 new_sock->media_id = media_id;
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",
227 spin_lock_irqsave(&fm->lock, flags);
228 if (!fm->sockets[cnt]) {
229 fm->sockets[cnt] = new_sock;
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);
236 fm->sockets[cnt] = NULL;
237 spin_unlock_irqrestore(&fm->lock,
239 tifm_free_device(&new_sock->dev);
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);
249 writel(TIFM_IRQ_ENABLE, fm->addr + FM_SET_INTERRUPT_ENABLE);
250 class_device_put(&fm->cdev);
253 static int tifm_7xx1_suspend(struct pci_dev *dev, pm_message_t state)
255 struct tifm_adapter *fm = pci_get_drvdata(dev);
258 spin_lock_irqsave(&fm->lock, flags);
259 fm->inhibit_new_cards = 1;
260 fm->remove_mask = 0xf;
262 writel(TIFM_IRQ_ENABLE, fm->addr + FM_CLEAR_INTERRUPT_ENABLE);
263 spin_unlock_irqrestore(&fm->lock, flags);
264 flush_workqueue(fm->wq);
266 tifm_7xx1_remove_media(&fm->media_remover);
268 pci_set_power_state(dev, PCI_D3hot);
269 pci_disable_device(dev);
274 static int tifm_7xx1_resume(struct pci_dev *dev)
276 struct tifm_adapter *fm = pci_get_drvdata(dev);
279 pci_restore_state(dev);
280 pci_enable_device(dev);
281 pci_set_power_state(dev, PCI_D0);
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);
295 static int tifm_7xx1_probe(struct pci_dev *dev,
296 const struct pci_device_id *dev_id)
298 struct tifm_adapter *fm;
299 int pci_dev_busy = 0;
302 rc = pci_set_dma_mask(dev, DMA_32BIT_MASK);
306 rc = pci_enable_device(dev);
312 rc = pci_request_regions(dev, DRIVER_NAME);
320 fm = tifm_alloc_adapter();
327 fm->max_sockets = (dev->device == 0x803B) ? 2 : 4;
328 fm->sockets = kzalloc(sizeof(struct tifm_dev*) * fm->max_sockets,
333 INIT_WORK(&fm->media_inserter, tifm_7xx1_insert_media);
334 INIT_WORK(&fm->media_remover, tifm_7xx1_remove_media);
335 fm->eject = tifm_7xx1_eject;
336 pci_set_drvdata(dev, fm);
338 fm->addr = ioremap(pci_resource_start(dev, 0),
339 pci_resource_len(dev, 0));
343 rc = request_irq(dev->irq, tifm_7xx1_isr, SA_SHIRQ, DRIVER_NAME, fm);
347 rc = tifm_add_adapter(fm);
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);
355 fm->insert_mask = 0xf;
360 free_irq(dev->irq, fm);
364 pci_set_drvdata(dev, NULL);
365 tifm_free_adapter(fm);
368 pci_release_regions(dev);
371 pci_disable_device(dev);
375 static void tifm_7xx1_remove(struct pci_dev *dev)
377 struct tifm_adapter *fm = pci_get_drvdata(dev);
380 spin_lock_irqsave(&fm->lock, flags);
381 fm->inhibit_new_cards = 1;
382 fm->remove_mask = 0xf;
384 writel(TIFM_IRQ_ENABLE, fm->addr + FM_CLEAR_INTERRUPT_ENABLE);
385 spin_unlock_irqrestore(&fm->lock, flags);
387 flush_workqueue(fm->wq);
389 tifm_7xx1_remove_media(&fm->media_remover);
391 writel(TIFM_IRQ_SETALL, fm->addr + FM_CLEAR_INTERRUPT_ENABLE);
392 free_irq(dev->irq, fm);
394 tifm_remove_adapter(fm);
396 pci_set_drvdata(dev, NULL);
400 pci_release_regions(dev);
402 pci_disable_device(dev);
403 tifm_free_adapter(fm);
406 static 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 */
414 static struct pci_driver tifm_7xx1_driver = {
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,
423 static int __init tifm_7xx1_init(void)
425 return pci_register_driver(&tifm_7xx1_driver);
428 static void __exit tifm_7xx1_exit(void)
430 pci_unregister_driver(&tifm_7xx1_driver);
433 MODULE_AUTHOR("Alex Dubov");
434 MODULE_DESCRIPTION("TI FlashMedia host driver");
435 MODULE_LICENSE("GPL");
436 MODULE_DEVICE_TABLE(pci, tifm_7xx1_pci_tbl);
437 MODULE_VERSION(DRIVER_VERSION);
439 module_init(tifm_7xx1_init);
440 module_exit(tifm_7xx1_exit);