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.8"
18 #define TIFM_IRQ_ENABLE 0x80000000
19 #define TIFM_IRQ_SOCKMASK(x) (x)
20 #define TIFM_IRQ_CARDMASK(x) ((x) << 8)
21 #define TIFM_IRQ_FIFOMASK(x) ((x) << 16)
22 #define TIFM_IRQ_SETALL 0xffffffff
24 static void tifm_7xx1_dummy_eject(struct tifm_adapter *fm,
25 struct tifm_dev *sock)
29 static void tifm_7xx1_eject(struct tifm_adapter *fm, struct tifm_dev *sock)
33 spin_lock_irqsave(&fm->lock, flags);
34 fm->socket_change_set |= 1 << sock->socket_id;
35 tifm_queue_work(&fm->media_switcher);
36 spin_unlock_irqrestore(&fm->lock, flags);
39 static irqreturn_t tifm_7xx1_isr(int irq, void *dev_id)
41 struct tifm_adapter *fm = dev_id;
42 struct tifm_dev *sock;
43 unsigned int irq_status, cnt;
46 irq_status = readl(fm->addr + FM_INTERRUPT_STATUS);
47 if (irq_status == 0 || irq_status == (~0)) {
48 spin_unlock(&fm->lock);
52 if (irq_status & TIFM_IRQ_ENABLE) {
53 writel(TIFM_IRQ_ENABLE, fm->addr + FM_CLEAR_INTERRUPT_ENABLE);
55 for (cnt = 0; cnt < fm->num_sockets; cnt++) {
56 sock = fm->sockets[cnt];
58 if ((irq_status >> cnt) & TIFM_IRQ_FIFOMASK(1))
59 sock->data_event(sock);
60 if ((irq_status >> cnt) & TIFM_IRQ_CARDMASK(1))
61 sock->card_event(sock);
65 fm->socket_change_set |= irq_status
66 & ((1 << fm->num_sockets) - 1);
68 writel(irq_status, fm->addr + FM_INTERRUPT_STATUS);
71 complete_all(fm->finish_me);
72 else if (!fm->socket_change_set)
73 writel(TIFM_IRQ_ENABLE, fm->addr + FM_SET_INTERRUPT_ENABLE);
75 tifm_queue_work(&fm->media_switcher);
77 spin_unlock(&fm->lock);
81 static unsigned char tifm_7xx1_toggle_sock_power(char __iomem *sock_addr)
86 writel(0x0e00, sock_addr + SOCK_CONTROL);
88 for (cnt = 16; cnt <= 256; cnt <<= 1) {
89 if (!(TIFM_SOCK_STATE_POWERED
90 & readl(sock_addr + SOCK_PRESENT_STATE)))
96 s_state = readl(sock_addr + SOCK_PRESENT_STATE);
97 if (!(TIFM_SOCK_STATE_OCCUPIED & s_state))
100 writel(readl(sock_addr + SOCK_CONTROL) | TIFM_CTRL_LED,
101 sock_addr + SOCK_CONTROL);
103 /* xd needs some extra time before power on */
104 if (((readl(sock_addr + SOCK_PRESENT_STATE) >> 4) & 7)
108 writel((s_state & 7) | 0x0c00, sock_addr + SOCK_CONTROL);
109 /* wait for power to stabilize */
111 for (cnt = 16; cnt <= 256; cnt <<= 1) {
112 if ((TIFM_SOCK_STATE_POWERED
113 & readl(sock_addr + SOCK_PRESENT_STATE)))
119 writel(readl(sock_addr + SOCK_CONTROL) & (~TIFM_CTRL_LED),
120 sock_addr + SOCK_CONTROL);
122 return (readl(sock_addr + SOCK_PRESENT_STATE) >> 4) & 7;
125 inline static char __iomem *
126 tifm_7xx1_sock_addr(char __iomem *base_addr, unsigned int sock_num)
128 return base_addr + ((sock_num + 1) << 10);
131 static void tifm_7xx1_switch_media(struct work_struct *work)
133 struct tifm_adapter *fm = container_of(work, struct tifm_adapter,
135 struct tifm_dev *sock;
137 unsigned char media_id;
138 unsigned int socket_change_set, cnt;
140 spin_lock_irqsave(&fm->lock, flags);
141 socket_change_set = fm->socket_change_set;
142 fm->socket_change_set = 0;
144 dev_dbg(fm->cdev.dev, "checking media set %x\n",
147 if (!socket_change_set) {
148 spin_unlock_irqrestore(&fm->lock, flags);
152 for (cnt = 0; cnt < fm->num_sockets; cnt++) {
153 if (!(socket_change_set & (1 << cnt)))
155 sock = fm->sockets[cnt];
158 "%s : demand removing card from socket %u:%u\n",
159 fm->cdev.class_id, fm->id, cnt);
160 fm->sockets[cnt] = NULL;
161 spin_unlock_irqrestore(&fm->lock, flags);
162 device_unregister(&sock->dev);
163 spin_lock_irqsave(&fm->lock, flags);
164 writel(0x0e00, tifm_7xx1_sock_addr(fm->addr, cnt)
168 spin_unlock_irqrestore(&fm->lock, flags);
170 media_id = tifm_7xx1_toggle_sock_power(
171 tifm_7xx1_sock_addr(fm->addr, cnt));
173 // tifm_alloc_device will check if media_id is valid
174 sock = tifm_alloc_device(fm, cnt, media_id);
176 sock->addr = tifm_7xx1_sock_addr(fm->addr, cnt);
178 if (!device_register(&sock->dev)) {
179 spin_lock_irqsave(&fm->lock, flags);
180 if (!fm->sockets[cnt]) {
181 fm->sockets[cnt] = sock;
184 spin_unlock_irqrestore(&fm->lock, flags);
187 tifm_free_device(&sock->dev);
189 spin_lock_irqsave(&fm->lock, flags);
192 writel(TIFM_IRQ_FIFOMASK(socket_change_set)
193 | TIFM_IRQ_CARDMASK(socket_change_set),
194 fm->addr + FM_CLEAR_INTERRUPT_ENABLE);
196 writel(TIFM_IRQ_FIFOMASK(socket_change_set)
197 | TIFM_IRQ_CARDMASK(socket_change_set),
198 fm->addr + FM_SET_INTERRUPT_ENABLE);
200 writel(TIFM_IRQ_ENABLE, fm->addr + FM_SET_INTERRUPT_ENABLE);
201 spin_unlock_irqrestore(&fm->lock, flags);
206 static int tifm_7xx1_suspend(struct pci_dev *dev, pm_message_t state)
208 dev_dbg(&dev->dev, "suspending host\n");
211 pci_enable_wake(dev, pci_choose_state(dev, state), 0);
212 pci_disable_device(dev);
213 pci_set_power_state(dev, pci_choose_state(dev, state));
217 static int tifm_7xx1_resume(struct pci_dev *dev)
219 struct tifm_adapter *fm = pci_get_drvdata(dev);
221 unsigned int good_sockets = 0, bad_sockets = 0;
223 unsigned char new_ids[fm->num_sockets];
224 DECLARE_COMPLETION_ONSTACK(finish_resume);
226 pci_set_power_state(dev, PCI_D0);
227 pci_restore_state(dev);
228 rc = pci_enable_device(dev);
233 dev_dbg(&dev->dev, "resuming host\n");
235 for (rc = 0; rc < fm->num_sockets; rc++)
236 new_ids[rc] = tifm_7xx1_toggle_sock_power(
237 tifm_7xx1_sock_addr(fm->addr, rc));
238 spin_lock_irqsave(&fm->lock, flags);
239 for (rc = 0; rc < fm->num_sockets; rc++) {
240 if (fm->sockets[rc]) {
241 if (fm->sockets[rc]->type == new_ids[rc])
242 good_sockets |= 1 << rc;
244 bad_sockets |= 1 << rc;
248 writel(TIFM_IRQ_ENABLE | TIFM_IRQ_SOCKMASK((1 << fm->num_sockets) - 1),
249 fm->addr + FM_SET_INTERRUPT_ENABLE);
250 dev_dbg(&dev->dev, "change sets on resume: good %x, bad %x\n",
251 good_sockets, bad_sockets);
253 fm->socket_change_set = 0;
255 fm->finish_me = &finish_resume;
256 spin_unlock_irqrestore(&fm->lock, flags);
257 rc = wait_for_completion_timeout(&finish_resume, HZ);
258 dev_dbg(&dev->dev, "wait returned %d\n", rc);
259 writel(TIFM_IRQ_FIFOMASK(good_sockets)
260 | TIFM_IRQ_CARDMASK(good_sockets),
261 fm->addr + FM_CLEAR_INTERRUPT_ENABLE);
262 writel(TIFM_IRQ_FIFOMASK(good_sockets)
263 | TIFM_IRQ_CARDMASK(good_sockets),
264 fm->addr + FM_SET_INTERRUPT_ENABLE);
265 spin_lock_irqsave(&fm->lock, flags);
266 fm->finish_me = NULL;
267 fm->socket_change_set ^= good_sockets & fm->socket_change_set;
270 fm->socket_change_set |= bad_sockets;
271 if (fm->socket_change_set)
272 tifm_queue_work(&fm->media_switcher);
274 spin_unlock_irqrestore(&fm->lock, flags);
275 writel(TIFM_IRQ_ENABLE,
276 fm->addr + FM_SET_INTERRUPT_ENABLE);
283 #define tifm_7xx1_suspend NULL
284 #define tifm_7xx1_resume NULL
286 #endif /* CONFIG_PM */
288 static int tifm_7xx1_probe(struct pci_dev *dev,
289 const struct pci_device_id *dev_id)
291 struct tifm_adapter *fm;
292 int pci_dev_busy = 0;
295 rc = pci_set_dma_mask(dev, DMA_32BIT_MASK);
299 rc = pci_enable_device(dev);
305 rc = pci_request_regions(dev, DRIVER_NAME);
313 fm = tifm_alloc_adapter(dev->device == PCI_DEVICE_ID_TI_XX21_XX11_FM
320 INIT_WORK(&fm->media_switcher, tifm_7xx1_switch_media);
321 fm->eject = tifm_7xx1_eject;
322 pci_set_drvdata(dev, fm);
324 fm->addr = ioremap(pci_resource_start(dev, 0),
325 pci_resource_len(dev, 0));
329 rc = request_irq(dev->irq, tifm_7xx1_isr, SA_SHIRQ, DRIVER_NAME, fm);
333 rc = tifm_add_adapter(fm);
337 writel(TIFM_IRQ_ENABLE | TIFM_IRQ_SOCKMASK((1 << fm->num_sockets) - 1),
338 fm->addr + FM_SET_INTERRUPT_ENABLE);
342 free_irq(dev->irq, fm);
346 pci_set_drvdata(dev, NULL);
347 tifm_free_adapter(fm);
350 pci_release_regions(dev);
353 pci_disable_device(dev);
357 static void tifm_7xx1_remove(struct pci_dev *dev)
359 struct tifm_adapter *fm = pci_get_drvdata(dev);
361 fm->eject = tifm_7xx1_dummy_eject;
362 writel(TIFM_IRQ_SETALL, fm->addr + FM_CLEAR_INTERRUPT_ENABLE);
364 free_irq(dev->irq, fm);
366 tifm_remove_adapter(fm);
368 pci_set_drvdata(dev, NULL);
372 pci_release_regions(dev);
374 pci_disable_device(dev);
375 tifm_free_adapter(fm);
378 static struct pci_device_id tifm_7xx1_pci_tbl [] = {
379 { PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_XX21_XX11_FM, PCI_ANY_ID,
380 PCI_ANY_ID, 0, 0, 0 }, /* xx21 - the one I have */
381 { PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_XX12_FM, PCI_ANY_ID,
382 PCI_ANY_ID, 0, 0, 0 },
383 { PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_XX20_FM, PCI_ANY_ID,
384 PCI_ANY_ID, 0, 0, 0 },
388 static struct pci_driver tifm_7xx1_driver = {
390 .id_table = tifm_7xx1_pci_tbl,
391 .probe = tifm_7xx1_probe,
392 .remove = tifm_7xx1_remove,
393 .suspend = tifm_7xx1_suspend,
394 .resume = tifm_7xx1_resume,
397 static int __init tifm_7xx1_init(void)
399 return pci_register_driver(&tifm_7xx1_driver);
402 static void __exit tifm_7xx1_exit(void)
404 pci_unregister_driver(&tifm_7xx1_driver);
407 MODULE_AUTHOR("Alex Dubov");
408 MODULE_DESCRIPTION("TI FlashMedia host driver");
409 MODULE_LICENSE("GPL");
410 MODULE_DEVICE_TABLE(pci, tifm_7xx1_pci_tbl);
411 MODULE_VERSION(DRIVER_VERSION);
413 module_init(tifm_7xx1_init);
414 module_exit(tifm_7xx1_exit);