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.7"
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)
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);
206 new_sock->addr = tifm_7xx1_sock_addr(fm->addr,
208 new_sock->media_id = media_id;
209 new_sock->socket_id = cnt;
223 snprintf(new_sock->dev.bus_id, BUS_ID_SIZE,
224 "tifm_%s%u:%u", card_name, fm->id, cnt);
225 printk(KERN_INFO DRIVER_NAME
226 ": %s card detected in socket %d\n",
228 spin_lock_irqsave(&fm->lock, flags);
229 if (!fm->sockets[cnt]) {
230 fm->sockets[cnt] = new_sock;
233 spin_unlock_irqrestore(&fm->lock, flags);
234 if (!ok_to_register ||
235 device_register(&new_sock->dev)) {
236 spin_lock_irqsave(&fm->lock, flags);
237 fm->sockets[cnt] = NULL;
238 spin_unlock_irqrestore(&fm->lock,
240 tifm_free_device(&new_sock->dev);
244 writel((TIFM_IRQ_FIFOMASK | TIFM_IRQ_CARDMASK) << cnt,
245 fm->addr + FM_CLEAR_INTERRUPT_ENABLE);
246 writel((TIFM_IRQ_FIFOMASK | TIFM_IRQ_CARDMASK) << cnt,
247 fm->addr + FM_SET_INTERRUPT_ENABLE);
250 writel(TIFM_IRQ_ENABLE, fm->addr + FM_SET_INTERRUPT_ENABLE);
251 class_device_put(&fm->cdev);
254 static int tifm_7xx1_suspend(struct pci_dev *dev, pm_message_t state)
256 struct tifm_adapter *fm = pci_get_drvdata(dev);
259 spin_lock_irqsave(&fm->lock, flags);
260 fm->inhibit_new_cards = 1;
261 fm->remove_mask = 0xf;
263 writel(TIFM_IRQ_ENABLE, fm->addr + FM_CLEAR_INTERRUPT_ENABLE);
264 spin_unlock_irqrestore(&fm->lock, flags);
265 flush_workqueue(fm->wq);
267 tifm_7xx1_remove_media(&fm->media_remover);
269 pci_set_power_state(dev, PCI_D3hot);
270 pci_disable_device(dev);
275 static int tifm_7xx1_resume(struct pci_dev *dev)
277 struct tifm_adapter *fm = pci_get_drvdata(dev);
280 pci_restore_state(dev);
281 pci_enable_device(dev);
282 pci_set_power_state(dev, PCI_D0);
285 spin_lock_irqsave(&fm->lock, flags);
286 fm->inhibit_new_cards = 0;
287 writel(TIFM_IRQ_SETALL, fm->addr + FM_INTERRUPT_STATUS);
288 writel(TIFM_IRQ_SETALL, fm->addr + FM_CLEAR_INTERRUPT_ENABLE);
289 writel(TIFM_IRQ_ENABLE | TIFM_IRQ_SETALLSOCK,
290 fm->addr + FM_SET_INTERRUPT_ENABLE);
291 fm->insert_mask = 0xf;
292 spin_unlock_irqrestore(&fm->lock, flags);
296 static int tifm_7xx1_probe(struct pci_dev *dev,
297 const struct pci_device_id *dev_id)
299 struct tifm_adapter *fm;
300 int pci_dev_busy = 0;
303 rc = pci_set_dma_mask(dev, DMA_32BIT_MASK);
307 rc = pci_enable_device(dev);
313 rc = pci_request_regions(dev, DRIVER_NAME);
321 fm = tifm_alloc_adapter();
328 fm->max_sockets = (dev->device == 0x803B) ? 2 : 4;
329 fm->sockets = kzalloc(sizeof(struct tifm_dev*) * fm->max_sockets,
334 INIT_WORK(&fm->media_inserter, tifm_7xx1_insert_media);
335 INIT_WORK(&fm->media_remover, tifm_7xx1_remove_media);
336 fm->eject = tifm_7xx1_eject;
337 pci_set_drvdata(dev, fm);
339 fm->addr = ioremap(pci_resource_start(dev, 0),
340 pci_resource_len(dev, 0));
344 rc = request_irq(dev->irq, tifm_7xx1_isr, SA_SHIRQ, DRIVER_NAME, fm);
348 rc = tifm_add_adapter(fm);
352 writel(TIFM_IRQ_SETALL, fm->addr + FM_CLEAR_INTERRUPT_ENABLE);
353 writel(TIFM_IRQ_ENABLE | TIFM_IRQ_SETALLSOCK,
354 fm->addr + FM_SET_INTERRUPT_ENABLE);
356 fm->insert_mask = 0xf;
361 free_irq(dev->irq, fm);
365 pci_set_drvdata(dev, NULL);
366 tifm_free_adapter(fm);
369 pci_release_regions(dev);
372 pci_disable_device(dev);
376 static void tifm_7xx1_remove(struct pci_dev *dev)
378 struct tifm_adapter *fm = pci_get_drvdata(dev);
381 spin_lock_irqsave(&fm->lock, flags);
382 fm->inhibit_new_cards = 1;
383 fm->remove_mask = 0xf;
385 writel(TIFM_IRQ_ENABLE, fm->addr + FM_CLEAR_INTERRUPT_ENABLE);
386 spin_unlock_irqrestore(&fm->lock, flags);
388 flush_workqueue(fm->wq);
390 tifm_7xx1_remove_media(&fm->media_remover);
392 writel(TIFM_IRQ_SETALL, fm->addr + FM_CLEAR_INTERRUPT_ENABLE);
393 free_irq(dev->irq, fm);
395 tifm_remove_adapter(fm);
397 pci_set_drvdata(dev, NULL);
401 pci_release_regions(dev);
403 pci_disable_device(dev);
404 tifm_free_adapter(fm);
407 static struct pci_device_id tifm_7xx1_pci_tbl [] = {
408 { PCI_VENDOR_ID_TI, 0x8033, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
409 0 }, /* xx21 - the one I have */
410 { PCI_VENDOR_ID_TI, 0x803B, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
411 0 }, /* xx12 - should be also supported */
415 static struct pci_driver tifm_7xx1_driver = {
417 .id_table = tifm_7xx1_pci_tbl,
418 .probe = tifm_7xx1_probe,
419 .remove = tifm_7xx1_remove,
420 .suspend = tifm_7xx1_suspend,
421 .resume = tifm_7xx1_resume,
424 static int __init tifm_7xx1_init(void)
426 return pci_register_driver(&tifm_7xx1_driver);
429 static void __exit tifm_7xx1_exit(void)
431 pci_unregister_driver(&tifm_7xx1_driver);
434 MODULE_AUTHOR("Alex Dubov");
435 MODULE_DESCRIPTION("TI FlashMedia host driver");
436 MODULE_LICENSE("GPL");
437 MODULE_DEVICE_TABLE(pci, tifm_7xx1_pci_tbl);
438 MODULE_VERSION(DRIVER_VERSION);
440 module_init(tifm_7xx1_init);
441 module_exit(tifm_7xx1_exit);