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)
22 spin_lock_irqsave(&fm->lock, flags);
23 if (!fm->inhibit_new_cards) {
24 fm->remove_mask |= 1 << sock->socket_id;
25 queue_work(fm->wq, &fm->media_remover);
27 spin_unlock_irqrestore(&fm->lock, flags);
30 static void tifm_7xx1_remove_media(struct work_struct *work)
32 struct tifm_adapter *fm =
33 container_of(work, struct tifm_adapter, media_remover);
36 struct tifm_dev *sock;
38 if (!class_device_get(&fm->cdev))
40 spin_lock_irqsave(&fm->lock, flags);
41 for (cnt = 0; cnt < fm->max_sockets; cnt++) {
42 if (fm->sockets[cnt] && (fm->remove_mask & (1 << cnt))) {
43 printk(KERN_INFO DRIVER_NAME
44 ": demand removing card from socket %d\n", cnt);
45 sock = fm->sockets[cnt];
46 fm->sockets[cnt] = NULL;
47 fm->remove_mask &= ~(1 << cnt);
49 writel(0x0e00, sock->addr + SOCK_CONTROL);
51 writel((TIFM_IRQ_FIFOMASK | TIFM_IRQ_CARDMASK) << cnt,
52 fm->addr + FM_CLEAR_INTERRUPT_ENABLE);
53 writel((TIFM_IRQ_FIFOMASK | TIFM_IRQ_CARDMASK) << cnt,
54 fm->addr + FM_SET_INTERRUPT_ENABLE);
56 spin_unlock_irqrestore(&fm->lock, flags);
57 device_unregister(&sock->dev);
58 spin_lock_irqsave(&fm->lock, flags);
61 spin_unlock_irqrestore(&fm->lock, flags);
62 class_device_put(&fm->cdev);
65 static irqreturn_t tifm_7xx1_isr(int irq, void *dev_id)
67 struct tifm_adapter *fm = dev_id;
68 struct tifm_dev *sock;
69 unsigned int irq_status;
70 unsigned int sock_irq_status, cnt;
73 irq_status = readl(fm->addr + FM_INTERRUPT_STATUS);
74 if (irq_status == 0 || irq_status == (~0)) {
75 spin_unlock(&fm->lock);
79 if (irq_status & TIFM_IRQ_ENABLE) {
80 writel(TIFM_IRQ_ENABLE, fm->addr + FM_CLEAR_INTERRUPT_ENABLE);
82 for (cnt = 0; cnt < fm->max_sockets; cnt++) {
83 sock = fm->sockets[cnt];
84 sock_irq_status = (irq_status >> cnt) &
85 (TIFM_IRQ_FIFOMASK | TIFM_IRQ_CARDMASK);
89 sock->signal_irq(sock, sock_irq_status);
91 if (irq_status & (1 << cnt))
92 fm->remove_mask |= 1 << cnt;
94 if (irq_status & (1 << cnt))
95 fm->insert_mask |= 1 << cnt;
99 writel(irq_status, fm->addr + FM_INTERRUPT_STATUS);
101 if (!fm->inhibit_new_cards) {
102 if (!fm->remove_mask && !fm->insert_mask) {
103 writel(TIFM_IRQ_ENABLE,
104 fm->addr + FM_SET_INTERRUPT_ENABLE);
106 queue_work(fm->wq, &fm->media_remover);
107 queue_work(fm->wq, &fm->media_inserter);
111 spin_unlock(&fm->lock);
115 static tifm_media_id tifm_7xx1_toggle_sock_power(char __iomem *sock_addr, int is_x2)
117 unsigned int s_state;
120 writel(0x0e00, sock_addr + SOCK_CONTROL);
122 for (cnt = 0; cnt < 100; cnt++) {
123 if (!(TIFM_SOCK_STATE_POWERED &
124 readl(sock_addr + SOCK_PRESENT_STATE)))
129 s_state = readl(sock_addr + SOCK_PRESENT_STATE);
130 if (!(TIFM_SOCK_STATE_OCCUPIED & s_state))
134 writel((s_state & 7) | 0x0c00, sock_addr + SOCK_CONTROL);
136 // SmartMedia cards need extra 40 msec
137 if (((readl(sock_addr + SOCK_PRESENT_STATE) >> 4) & 7) == 1)
139 writel(readl(sock_addr + SOCK_CONTROL) | TIFM_CTRL_LED,
140 sock_addr + SOCK_CONTROL);
142 writel((s_state & 0x7) | 0x0c00 | TIFM_CTRL_LED,
143 sock_addr + SOCK_CONTROL);
146 for (cnt = 0; cnt < 100; cnt++) {
147 if ((TIFM_SOCK_STATE_POWERED &
148 readl(sock_addr + SOCK_PRESENT_STATE)))
154 writel(readl(sock_addr + SOCK_CONTROL) & (~TIFM_CTRL_LED),
155 sock_addr + SOCK_CONTROL);
157 return (readl(sock_addr + SOCK_PRESENT_STATE) >> 4) & 7;
160 inline static char __iomem *
161 tifm_7xx1_sock_addr(char __iomem *base_addr, unsigned int sock_num)
163 return base_addr + ((sock_num + 1) << 10);
166 static void tifm_7xx1_insert_media(struct work_struct *work)
168 struct tifm_adapter *fm =
169 container_of(work, struct tifm_adapter, media_inserter);
171 tifm_media_id media_id;
172 char *card_name = "xx";
173 int cnt, ok_to_register;
174 unsigned int insert_mask;
175 struct tifm_dev *new_sock = NULL;
177 if (!class_device_get(&fm->cdev))
179 spin_lock_irqsave(&fm->lock, flags);
180 insert_mask = fm->insert_mask;
182 if (fm->inhibit_new_cards) {
183 spin_unlock_irqrestore(&fm->lock, flags);
184 class_device_put(&fm->cdev);
187 spin_unlock_irqrestore(&fm->lock, flags);
189 for (cnt = 0; cnt < fm->max_sockets; cnt++) {
190 if (!(insert_mask & (1 << cnt)))
193 media_id = tifm_7xx1_toggle_sock_power(tifm_7xx1_sock_addr(fm->addr, cnt),
194 fm->max_sockets == 2);
197 new_sock = tifm_alloc_device(fm);
199 new_sock->addr = tifm_7xx1_sock_addr(fm->addr,
201 new_sock->media_id = media_id;
202 new_sock->socket_id = cnt;
216 snprintf(new_sock->dev.bus_id, BUS_ID_SIZE,
217 "tifm_%s%u:%u", card_name, fm->id, cnt);
218 printk(KERN_INFO DRIVER_NAME
219 ": %s card detected in socket %d\n",
221 spin_lock_irqsave(&fm->lock, flags);
222 if (!fm->sockets[cnt]) {
223 fm->sockets[cnt] = new_sock;
226 spin_unlock_irqrestore(&fm->lock, flags);
227 if (!ok_to_register ||
228 device_register(&new_sock->dev)) {
229 spin_lock_irqsave(&fm->lock, flags);
230 fm->sockets[cnt] = NULL;
231 spin_unlock_irqrestore(&fm->lock,
233 tifm_free_device(&new_sock->dev);
237 writel((TIFM_IRQ_FIFOMASK | TIFM_IRQ_CARDMASK) << cnt,
238 fm->addr + FM_CLEAR_INTERRUPT_ENABLE);
239 writel((TIFM_IRQ_FIFOMASK | TIFM_IRQ_CARDMASK) << cnt,
240 fm->addr + FM_SET_INTERRUPT_ENABLE);
243 writel(TIFM_IRQ_ENABLE, fm->addr + FM_SET_INTERRUPT_ENABLE);
244 class_device_put(&fm->cdev);
247 static int tifm_7xx1_suspend(struct pci_dev *dev, pm_message_t state)
249 struct tifm_adapter *fm = pci_get_drvdata(dev);
252 spin_lock_irqsave(&fm->lock, flags);
253 fm->inhibit_new_cards = 1;
254 fm->remove_mask = 0xf;
256 writel(TIFM_IRQ_ENABLE, fm->addr + FM_CLEAR_INTERRUPT_ENABLE);
257 spin_unlock_irqrestore(&fm->lock, flags);
258 flush_workqueue(fm->wq);
260 tifm_7xx1_remove_media(&fm->media_remover);
262 pci_set_power_state(dev, PCI_D3hot);
263 pci_disable_device(dev);
268 static int tifm_7xx1_resume(struct pci_dev *dev)
270 struct tifm_adapter *fm = pci_get_drvdata(dev);
273 pci_restore_state(dev);
274 pci_enable_device(dev);
275 pci_set_power_state(dev, PCI_D0);
278 spin_lock_irqsave(&fm->lock, flags);
279 fm->inhibit_new_cards = 0;
280 writel(TIFM_IRQ_SETALL, fm->addr + FM_INTERRUPT_STATUS);
281 writel(TIFM_IRQ_SETALL, fm->addr + FM_CLEAR_INTERRUPT_ENABLE);
282 writel(TIFM_IRQ_ENABLE | TIFM_IRQ_SETALLSOCK,
283 fm->addr + FM_SET_INTERRUPT_ENABLE);
284 fm->insert_mask = 0xf;
285 spin_unlock_irqrestore(&fm->lock, flags);
289 static int tifm_7xx1_probe(struct pci_dev *dev,
290 const struct pci_device_id *dev_id)
292 struct tifm_adapter *fm;
293 int pci_dev_busy = 0;
296 rc = pci_set_dma_mask(dev, DMA_32BIT_MASK);
300 rc = pci_enable_device(dev);
306 rc = pci_request_regions(dev, DRIVER_NAME);
314 fm = tifm_alloc_adapter();
321 fm->max_sockets = (dev->device == 0x803B) ? 2 : 4;
322 fm->sockets = kzalloc(sizeof(struct tifm_dev*) * fm->max_sockets,
327 INIT_WORK(&fm->media_inserter, tifm_7xx1_insert_media);
328 INIT_WORK(&fm->media_remover, tifm_7xx1_remove_media);
329 fm->eject = tifm_7xx1_eject;
330 pci_set_drvdata(dev, fm);
332 fm->addr = ioremap(pci_resource_start(dev, 0),
333 pci_resource_len(dev, 0));
337 rc = request_irq(dev->irq, tifm_7xx1_isr, SA_SHIRQ, DRIVER_NAME, fm);
341 rc = tifm_add_adapter(fm);
345 writel(TIFM_IRQ_SETALL, fm->addr + FM_CLEAR_INTERRUPT_ENABLE);
346 writel(TIFM_IRQ_ENABLE | TIFM_IRQ_SETALLSOCK,
347 fm->addr + FM_SET_INTERRUPT_ENABLE);
349 fm->insert_mask = 0xf;
354 free_irq(dev->irq, fm);
358 pci_set_drvdata(dev, NULL);
359 tifm_free_adapter(fm);
362 pci_release_regions(dev);
365 pci_disable_device(dev);
369 static void tifm_7xx1_remove(struct pci_dev *dev)
371 struct tifm_adapter *fm = pci_get_drvdata(dev);
374 spin_lock_irqsave(&fm->lock, flags);
375 fm->inhibit_new_cards = 1;
376 fm->remove_mask = 0xf;
378 writel(TIFM_IRQ_ENABLE, fm->addr + FM_CLEAR_INTERRUPT_ENABLE);
379 spin_unlock_irqrestore(&fm->lock, flags);
381 flush_workqueue(fm->wq);
383 tifm_7xx1_remove_media(&fm->media_remover);
385 writel(TIFM_IRQ_SETALL, fm->addr + FM_CLEAR_INTERRUPT_ENABLE);
386 free_irq(dev->irq, fm);
388 tifm_remove_adapter(fm);
390 pci_set_drvdata(dev, NULL);
394 pci_release_regions(dev);
396 pci_disable_device(dev);
397 tifm_free_adapter(fm);
400 static struct pci_device_id tifm_7xx1_pci_tbl [] = {
401 { PCI_VENDOR_ID_TI, 0x8033, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
402 0 }, /* xx21 - the one I have */
403 { PCI_VENDOR_ID_TI, 0x803B, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
404 0 }, /* xx12 - should be also supported */
408 static struct pci_driver tifm_7xx1_driver = {
410 .id_table = tifm_7xx1_pci_tbl,
411 .probe = tifm_7xx1_probe,
412 .remove = tifm_7xx1_remove,
413 .suspend = tifm_7xx1_suspend,
414 .resume = tifm_7xx1_resume,
417 static int __init tifm_7xx1_init(void)
419 return pci_register_driver(&tifm_7xx1_driver);
422 static void __exit tifm_7xx1_exit(void)
424 pci_unregister_driver(&tifm_7xx1_driver);
427 MODULE_AUTHOR("Alex Dubov");
428 MODULE_DESCRIPTION("TI FlashMedia host driver");
429 MODULE_LICENSE("GPL");
430 MODULE_DEVICE_TABLE(pci, tifm_7xx1_pci_tbl);
431 MODULE_VERSION(DRIVER_VERSION);
433 module_init(tifm_7xx1_init);
434 module_exit(tifm_7xx1_exit);