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>
14 #include <linux/freezer.h>
16 #define DRIVER_NAME "tifm_7xx1"
17 #define DRIVER_VERSION "0.8"
19 #define TIFM_IRQ_ENABLE 0x80000000
20 #define TIFM_IRQ_SOCKMASK(x) (x)
21 #define TIFM_IRQ_CARDMASK(x) ((x) << 8)
22 #define TIFM_IRQ_FIFOMASK(x) ((x) << 16)
23 #define TIFM_IRQ_SETALL 0xffffffff
25 static void tifm_7xx1_dummy_eject(struct tifm_adapter *fm,
26 struct tifm_dev *sock)
30 static void tifm_7xx1_eject(struct tifm_adapter *fm, struct tifm_dev *sock)
34 spin_lock_irqsave(&fm->lock, flags);
35 fm->socket_change_set |= 1 << sock->socket_id;
36 tifm_queue_work(&fm->media_switcher);
37 spin_unlock_irqrestore(&fm->lock, flags);
40 static irqreturn_t tifm_7xx1_isr(int irq, void *dev_id)
42 struct tifm_adapter *fm = dev_id;
43 struct tifm_dev *sock;
44 unsigned int irq_status;
48 irq_status = readl(fm->addr + FM_INTERRUPT_STATUS);
49 if (irq_status == 0 || irq_status == (~0)) {
50 spin_unlock(&fm->lock);
54 if (irq_status & TIFM_IRQ_ENABLE) {
55 writel(TIFM_IRQ_ENABLE, fm->addr + FM_CLEAR_INTERRUPT_ENABLE);
57 for (cnt = 0; cnt < fm->num_sockets; cnt++) {
58 sock = fm->sockets[cnt];
60 if ((irq_status >> cnt) & TIFM_IRQ_FIFOMASK(1))
61 sock->data_event(sock);
62 if ((irq_status >> cnt) & TIFM_IRQ_CARDMASK(1))
63 sock->card_event(sock);
67 fm->socket_change_set |= irq_status
68 & ((1 << fm->num_sockets) - 1);
70 writel(irq_status, fm->addr + FM_INTERRUPT_STATUS);
73 complete_all(fm->finish_me);
74 else if (!fm->socket_change_set)
75 writel(TIFM_IRQ_ENABLE, fm->addr + FM_SET_INTERRUPT_ENABLE);
77 tifm_queue_work(&fm->media_switcher);
79 spin_unlock(&fm->lock);
83 static unsigned char tifm_7xx1_toggle_sock_power(char __iomem *sock_addr)
88 writel(0x0e00, sock_addr + SOCK_CONTROL);
90 for (cnt = 16; cnt <= 256; cnt <<= 1) {
91 if (!(TIFM_SOCK_STATE_POWERED
92 & readl(sock_addr + SOCK_PRESENT_STATE)))
98 s_state = readl(sock_addr + SOCK_PRESENT_STATE);
99 if (!(TIFM_SOCK_STATE_OCCUPIED & s_state))
102 writel(readl(sock_addr + SOCK_CONTROL) | TIFM_CTRL_LED,
103 sock_addr + SOCK_CONTROL);
105 /* xd needs some extra time before power on */
106 if (((readl(sock_addr + SOCK_PRESENT_STATE) >> 4) & 7)
110 writel((s_state & 7) | 0x0c00, sock_addr + SOCK_CONTROL);
111 /* wait for power to stabilize */
113 for (cnt = 16; cnt <= 256; cnt <<= 1) {
114 if ((TIFM_SOCK_STATE_POWERED
115 & readl(sock_addr + SOCK_PRESENT_STATE)))
121 writel(readl(sock_addr + SOCK_CONTROL) & (~TIFM_CTRL_LED),
122 sock_addr + SOCK_CONTROL);
124 return (readl(sock_addr + SOCK_PRESENT_STATE) >> 4) & 7;
127 inline static char __iomem *
128 tifm_7xx1_sock_addr(char __iomem *base_addr, unsigned int sock_num)
130 return base_addr + ((sock_num + 1) << 10);
133 static void tifm_7xx1_switch_media(struct work_struct *work)
135 struct tifm_adapter *fm = container_of(work, struct tifm_adapter,
138 unsigned char media_id;
140 struct tifm_dev *sock;
141 unsigned int socket_change_set;
143 spin_lock_irqsave(&fm->lock, flags);
144 socket_change_set = fm->socket_change_set;
145 fm->socket_change_set = 0;
147 dev_dbg(fm->cdev.dev, "checking media set %x\n",
150 if (!socket_change_set) {
151 spin_unlock_irqrestore(&fm->lock, flags);
155 for (cnt = 0; cnt < fm->num_sockets; cnt++) {
156 if (!(socket_change_set & (1 << cnt)))
158 sock = fm->sockets[cnt];
161 "%s : demand removing card from socket %u:%u\n",
162 fm->cdev.class_id, fm->id, cnt);
163 fm->sockets[cnt] = NULL;
164 spin_unlock_irqrestore(&fm->lock, flags);
165 device_unregister(&sock->dev);
166 spin_lock_irqsave(&fm->lock, flags);
167 writel(0x0e00, tifm_7xx1_sock_addr(fm->addr, cnt)
171 spin_unlock_irqrestore(&fm->lock, flags);
173 media_id = tifm_7xx1_toggle_sock_power(
174 tifm_7xx1_sock_addr(fm->addr, cnt));
176 // tifm_alloc_device will check if media_id is valid
177 sock = tifm_alloc_device(fm, cnt, media_id);
179 sock->addr = tifm_7xx1_sock_addr(fm->addr, cnt);
181 if (!device_register(&sock->dev)) {
182 spin_lock_irqsave(&fm->lock, flags);
183 if (!fm->sockets[cnt]) {
184 fm->sockets[cnt] = sock;
187 spin_unlock_irqrestore(&fm->lock, flags);
190 tifm_free_device(&sock->dev);
192 spin_lock_irqsave(&fm->lock, flags);
195 writel(TIFM_IRQ_FIFOMASK(socket_change_set)
196 | TIFM_IRQ_CARDMASK(socket_change_set),
197 fm->addr + FM_CLEAR_INTERRUPT_ENABLE);
199 writel(TIFM_IRQ_FIFOMASK(socket_change_set)
200 | TIFM_IRQ_CARDMASK(socket_change_set),
201 fm->addr + FM_SET_INTERRUPT_ENABLE);
203 writel(TIFM_IRQ_ENABLE, fm->addr + FM_SET_INTERRUPT_ENABLE);
204 spin_unlock_irqrestore(&fm->lock, flags);
209 static int tifm_7xx1_suspend(struct pci_dev *dev, pm_message_t state)
211 dev_dbg(&dev->dev, "suspending host\n");
214 pci_enable_wake(dev, pci_choose_state(dev, state), 0);
215 pci_disable_device(dev);
216 pci_set_power_state(dev, pci_choose_state(dev, state));
220 static int tifm_7xx1_resume(struct pci_dev *dev)
222 struct tifm_adapter *fm = pci_get_drvdata(dev);
225 unsigned char new_ids[fm->num_sockets];
226 DECLARE_COMPLETION_ONSTACK(finish_resume);
228 pci_set_power_state(dev, PCI_D0);
229 pci_restore_state(dev);
230 rc = pci_enable_device(dev);
235 dev_dbg(&dev->dev, "resuming host\n");
237 for (cnt = 0; cnt < fm->num_sockets; cnt++)
238 new_ids[cnt] = tifm_7xx1_toggle_sock_power(
239 tifm_7xx1_sock_addr(fm->addr, cnt));
240 spin_lock_irqsave(&fm->lock, flags);
241 fm->socket_change_set = 0;
242 for (cnt = 0; cnt < fm->num_sockets; cnt++) {
243 if (fm->sockets[cnt]) {
244 if (fm->sockets[cnt]->type == new_ids[cnt])
245 fm->socket_change_set |= 1 << cnt;
247 fm->sockets[cnt]->type = new_ids[cnt];
251 writel(TIFM_IRQ_ENABLE | TIFM_IRQ_SOCKMASK((1 << fm->num_sockets) - 1),
252 fm->addr + FM_SET_INTERRUPT_ENABLE);
253 if (!fm->socket_change_set) {
254 spin_unlock_irqrestore(&fm->lock, flags);
257 fm->socket_change_set = 0;
258 fm->finish_me = &finish_resume;
259 spin_unlock_irqrestore(&fm->lock, flags);
262 wait_for_completion_timeout(&finish_resume, HZ);
264 spin_lock_irqsave(&fm->lock, flags);
265 fm->finish_me = NULL;
266 writel(TIFM_IRQ_FIFOMASK(fm->socket_change_set)
267 | TIFM_IRQ_CARDMASK(fm->socket_change_set),
268 fm->addr + FM_CLEAR_INTERRUPT_ENABLE);
269 writel(TIFM_IRQ_FIFOMASK(fm->socket_change_set)
270 | TIFM_IRQ_CARDMASK(fm->socket_change_set),
271 fm->addr + FM_SET_INTERRUPT_ENABLE);
272 writel(TIFM_IRQ_ENABLE,
273 fm->addr + FM_SET_INTERRUPT_ENABLE);
274 fm->socket_change_set = 0;
276 spin_unlock_irqrestore(&fm->lock, flags);
282 #define tifm_7xx1_suspend NULL
283 #define tifm_7xx1_resume NULL
285 #endif /* CONFIG_PM */
287 static int tifm_7xx1_probe(struct pci_dev *dev,
288 const struct pci_device_id *dev_id)
290 struct tifm_adapter *fm;
291 int pci_dev_busy = 0;
294 rc = pci_set_dma_mask(dev, DMA_32BIT_MASK);
298 rc = pci_enable_device(dev);
304 rc = pci_request_regions(dev, DRIVER_NAME);
312 fm = tifm_alloc_adapter(dev->device == PCI_DEVICE_ID_TI_XX21_XX11_FM
319 INIT_WORK(&fm->media_switcher, tifm_7xx1_switch_media);
320 fm->eject = tifm_7xx1_eject;
321 pci_set_drvdata(dev, fm);
323 fm->addr = ioremap(pci_resource_start(dev, 0),
324 pci_resource_len(dev, 0));
328 rc = request_irq(dev->irq, tifm_7xx1_isr, SA_SHIRQ, DRIVER_NAME, fm);
332 rc = tifm_add_adapter(fm);
336 writel(TIFM_IRQ_ENABLE | TIFM_IRQ_SOCKMASK((1 << fm->num_sockets) - 1),
337 fm->addr + FM_SET_INTERRUPT_ENABLE);
341 free_irq(dev->irq, fm);
345 pci_set_drvdata(dev, NULL);
346 tifm_free_adapter(fm);
349 pci_release_regions(dev);
352 pci_disable_device(dev);
356 static void tifm_7xx1_remove(struct pci_dev *dev)
358 struct tifm_adapter *fm = pci_get_drvdata(dev);
360 fm->eject = tifm_7xx1_dummy_eject;
361 writel(TIFM_IRQ_SETALL, fm->addr + FM_CLEAR_INTERRUPT_ENABLE);
363 free_irq(dev->irq, fm);
365 tifm_remove_adapter(fm);
367 pci_set_drvdata(dev, NULL);
371 pci_release_regions(dev);
373 pci_disable_device(dev);
374 tifm_free_adapter(fm);
377 static struct pci_device_id tifm_7xx1_pci_tbl [] = {
378 { PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_XX21_XX11_FM, PCI_ANY_ID,
379 PCI_ANY_ID, 0, 0, 0 }, /* xx21 - the one I have */
380 { PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_XX12_FM, PCI_ANY_ID,
381 PCI_ANY_ID, 0, 0, 0 },
382 { PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_XX20_FM, PCI_ANY_ID,
383 PCI_ANY_ID, 0, 0, 0 },
387 static struct pci_driver tifm_7xx1_driver = {
389 .id_table = tifm_7xx1_pci_tbl,
390 .probe = tifm_7xx1_probe,
391 .remove = tifm_7xx1_remove,
392 .suspend = tifm_7xx1_suspend,
393 .resume = tifm_7xx1_resume,
396 static int __init tifm_7xx1_init(void)
398 return pci_register_driver(&tifm_7xx1_driver);
401 static void __exit tifm_7xx1_exit(void)
403 pci_unregister_driver(&tifm_7xx1_driver);
406 MODULE_AUTHOR("Alex Dubov");
407 MODULE_DESCRIPTION("TI FlashMedia host driver");
408 MODULE_LICENSE("GPL");
409 MODULE_DEVICE_TABLE(pci, tifm_7xx1_pci_tbl);
410 MODULE_VERSION(DRIVER_VERSION);
412 module_init(tifm_7xx1_init);
413 module_exit(tifm_7xx1_exit);