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 & TIFM_CTRL_POWER_MASK) | 0x0c00,
109 sock_addr + SOCK_CONTROL);
110 /* wait for power to stabilize */
112 for (cnt = 16; cnt <= 256; cnt <<= 1) {
113 if ((TIFM_SOCK_STATE_POWERED
114 & readl(sock_addr + SOCK_PRESENT_STATE)))
120 writel(readl(sock_addr + SOCK_CONTROL) & (~TIFM_CTRL_LED),
121 sock_addr + SOCK_CONTROL);
123 return (readl(sock_addr + SOCK_PRESENT_STATE) >> 4) & 7;
126 inline static void tifm_7xx1_sock_power_off(char __iomem *sock_addr)
128 writel((~TIFM_CTRL_POWER_MASK) & readl(sock_addr + SOCK_CONTROL),
129 sock_addr + SOCK_CONTROL);
132 inline static char __iomem *
133 tifm_7xx1_sock_addr(char __iomem *base_addr, unsigned int sock_num)
135 return base_addr + ((sock_num + 1) << 10);
138 static void tifm_7xx1_switch_media(struct work_struct *work)
140 struct tifm_adapter *fm = container_of(work, struct tifm_adapter,
142 struct tifm_dev *sock;
143 char __iomem *sock_addr;
145 unsigned char media_id;
146 unsigned int socket_change_set, cnt;
148 spin_lock_irqsave(&fm->lock, flags);
149 socket_change_set = fm->socket_change_set;
150 fm->socket_change_set = 0;
152 dev_dbg(fm->dev.parent, "checking media set %x\n",
155 if (!socket_change_set) {
156 spin_unlock_irqrestore(&fm->lock, flags);
160 for (cnt = 0; cnt < fm->num_sockets; cnt++) {
161 if (!(socket_change_set & (1 << cnt)))
163 sock = fm->sockets[cnt];
166 "%s : demand removing card from socket %u:%u\n",
167 dev_name(&fm->dev), fm->id, cnt);
168 fm->sockets[cnt] = NULL;
169 sock_addr = sock->addr;
170 spin_unlock_irqrestore(&fm->lock, flags);
171 device_unregister(&sock->dev);
172 spin_lock_irqsave(&fm->lock, flags);
173 tifm_7xx1_sock_power_off(sock_addr);
174 writel(0x0e00, sock_addr + SOCK_CONTROL);
177 spin_unlock_irqrestore(&fm->lock, flags);
179 media_id = tifm_7xx1_toggle_sock_power(
180 tifm_7xx1_sock_addr(fm->addr, cnt));
182 // tifm_alloc_device will check if media_id is valid
183 sock = tifm_alloc_device(fm, cnt, media_id);
185 sock->addr = tifm_7xx1_sock_addr(fm->addr, cnt);
187 if (!device_register(&sock->dev)) {
188 spin_lock_irqsave(&fm->lock, flags);
189 if (!fm->sockets[cnt]) {
190 fm->sockets[cnt] = sock;
193 spin_unlock_irqrestore(&fm->lock, flags);
196 tifm_free_device(&sock->dev);
198 spin_lock_irqsave(&fm->lock, flags);
201 writel(TIFM_IRQ_FIFOMASK(socket_change_set)
202 | TIFM_IRQ_CARDMASK(socket_change_set),
203 fm->addr + FM_CLEAR_INTERRUPT_ENABLE);
205 writel(TIFM_IRQ_FIFOMASK(socket_change_set)
206 | TIFM_IRQ_CARDMASK(socket_change_set),
207 fm->addr + FM_SET_INTERRUPT_ENABLE);
209 writel(TIFM_IRQ_ENABLE, fm->addr + FM_SET_INTERRUPT_ENABLE);
210 spin_unlock_irqrestore(&fm->lock, flags);
215 static int tifm_7xx1_suspend(struct pci_dev *dev, pm_message_t state)
217 struct tifm_adapter *fm = pci_get_drvdata(dev);
220 dev_dbg(&dev->dev, "suspending host\n");
222 for (cnt = 0; cnt < fm->num_sockets; cnt++) {
223 if (fm->sockets[cnt])
224 tifm_7xx1_sock_power_off(fm->sockets[cnt]->addr);
228 pci_enable_wake(dev, pci_choose_state(dev, state), 0);
229 pci_disable_device(dev);
230 pci_set_power_state(dev, pci_choose_state(dev, state));
234 static int tifm_7xx1_resume(struct pci_dev *dev)
236 struct tifm_adapter *fm = pci_get_drvdata(dev);
238 unsigned int good_sockets = 0, bad_sockets = 0;
240 unsigned char new_ids[fm->num_sockets];
241 DECLARE_COMPLETION_ONSTACK(finish_resume);
243 pci_set_power_state(dev, PCI_D0);
244 pci_restore_state(dev);
245 rc = pci_enable_device(dev);
250 dev_dbg(&dev->dev, "resuming host\n");
252 for (rc = 0; rc < fm->num_sockets; rc++)
253 new_ids[rc] = tifm_7xx1_toggle_sock_power(
254 tifm_7xx1_sock_addr(fm->addr, rc));
255 spin_lock_irqsave(&fm->lock, flags);
256 for (rc = 0; rc < fm->num_sockets; rc++) {
257 if (fm->sockets[rc]) {
258 if (fm->sockets[rc]->type == new_ids[rc])
259 good_sockets |= 1 << rc;
261 bad_sockets |= 1 << rc;
265 writel(TIFM_IRQ_ENABLE | TIFM_IRQ_SOCKMASK((1 << fm->num_sockets) - 1),
266 fm->addr + FM_SET_INTERRUPT_ENABLE);
267 dev_dbg(&dev->dev, "change sets on resume: good %x, bad %x\n",
268 good_sockets, bad_sockets);
270 fm->socket_change_set = 0;
272 fm->finish_me = &finish_resume;
273 spin_unlock_irqrestore(&fm->lock, flags);
274 rc = wait_for_completion_timeout(&finish_resume, HZ);
275 dev_dbg(&dev->dev, "wait returned %d\n", rc);
276 writel(TIFM_IRQ_FIFOMASK(good_sockets)
277 | TIFM_IRQ_CARDMASK(good_sockets),
278 fm->addr + FM_CLEAR_INTERRUPT_ENABLE);
279 writel(TIFM_IRQ_FIFOMASK(good_sockets)
280 | TIFM_IRQ_CARDMASK(good_sockets),
281 fm->addr + FM_SET_INTERRUPT_ENABLE);
282 spin_lock_irqsave(&fm->lock, flags);
283 fm->finish_me = NULL;
284 fm->socket_change_set ^= good_sockets & fm->socket_change_set;
287 fm->socket_change_set |= bad_sockets;
288 if (fm->socket_change_set)
289 tifm_queue_work(&fm->media_switcher);
291 spin_unlock_irqrestore(&fm->lock, flags);
292 writel(TIFM_IRQ_ENABLE,
293 fm->addr + FM_SET_INTERRUPT_ENABLE);
300 #define tifm_7xx1_suspend NULL
301 #define tifm_7xx1_resume NULL
303 #endif /* CONFIG_PM */
305 static int tifm_7xx1_dummy_has_ms_pif(struct tifm_adapter *fm,
306 struct tifm_dev *sock)
311 static int tifm_7xx1_has_ms_pif(struct tifm_adapter *fm, struct tifm_dev *sock)
313 if (((fm->num_sockets == 4) && (sock->socket_id == 2))
314 || ((fm->num_sockets == 2) && (sock->socket_id == 0)))
320 static int tifm_7xx1_probe(struct pci_dev *dev,
321 const struct pci_device_id *dev_id)
323 struct tifm_adapter *fm;
324 int pci_dev_busy = 0;
327 rc = pci_set_dma_mask(dev, DMA_BIT_MASK(32));
331 rc = pci_enable_device(dev);
337 rc = pci_request_regions(dev, DRIVER_NAME);
345 fm = tifm_alloc_adapter(dev->device == PCI_DEVICE_ID_TI_XX21_XX11_FM
352 INIT_WORK(&fm->media_switcher, tifm_7xx1_switch_media);
353 fm->eject = tifm_7xx1_eject;
354 fm->has_ms_pif = tifm_7xx1_has_ms_pif;
355 pci_set_drvdata(dev, fm);
357 fm->addr = pci_ioremap_bar(dev, 0);
361 rc = request_irq(dev->irq, tifm_7xx1_isr, IRQF_SHARED, DRIVER_NAME, fm);
365 rc = tifm_add_adapter(fm);
369 writel(TIFM_IRQ_ENABLE | TIFM_IRQ_SOCKMASK((1 << fm->num_sockets) - 1),
370 fm->addr + FM_CLEAR_INTERRUPT_ENABLE);
371 writel(TIFM_IRQ_ENABLE | TIFM_IRQ_SOCKMASK((1 << fm->num_sockets) - 1),
372 fm->addr + FM_SET_INTERRUPT_ENABLE);
376 free_irq(dev->irq, fm);
380 pci_set_drvdata(dev, NULL);
381 tifm_free_adapter(fm);
384 pci_release_regions(dev);
387 pci_disable_device(dev);
391 static void tifm_7xx1_remove(struct pci_dev *dev)
393 struct tifm_adapter *fm = pci_get_drvdata(dev);
396 fm->eject = tifm_7xx1_dummy_eject;
397 fm->has_ms_pif = tifm_7xx1_dummy_has_ms_pif;
398 writel(TIFM_IRQ_SETALL, fm->addr + FM_CLEAR_INTERRUPT_ENABLE);
400 free_irq(dev->irq, fm);
402 tifm_remove_adapter(fm);
404 for (cnt = 0; cnt < fm->num_sockets; cnt++)
405 tifm_7xx1_sock_power_off(tifm_7xx1_sock_addr(fm->addr, cnt));
407 pci_set_drvdata(dev, NULL);
411 pci_release_regions(dev);
413 pci_disable_device(dev);
414 tifm_free_adapter(fm);
417 static struct pci_device_id tifm_7xx1_pci_tbl [] = {
418 { PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_XX21_XX11_FM, PCI_ANY_ID,
419 PCI_ANY_ID, 0, 0, 0 }, /* xx21 - the one I have */
420 { PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_XX12_FM, PCI_ANY_ID,
421 PCI_ANY_ID, 0, 0, 0 },
422 { PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_XX20_FM, PCI_ANY_ID,
423 PCI_ANY_ID, 0, 0, 0 },
427 static struct pci_driver tifm_7xx1_driver = {
429 .id_table = tifm_7xx1_pci_tbl,
430 .probe = tifm_7xx1_probe,
431 .remove = tifm_7xx1_remove,
432 .suspend = tifm_7xx1_suspend,
433 .resume = tifm_7xx1_resume,
436 static int __init tifm_7xx1_init(void)
438 return pci_register_driver(&tifm_7xx1_driver);
441 static void __exit tifm_7xx1_exit(void)
443 pci_unregister_driver(&tifm_7xx1_driver);
446 MODULE_AUTHOR("Alex Dubov");
447 MODULE_DESCRIPTION("TI FlashMedia host driver");
448 MODULE_LICENSE("GPL");
449 MODULE_DEVICE_TABLE(pci, tifm_7xx1_pci_tbl);
450 MODULE_VERSION(DRIVER_VERSION);
452 module_init(tifm_7xx1_init);
453 module_exit(tifm_7xx1_exit);