]>
Commit | Line | Data |
---|---|---|
4020f2d7 AD |
1 | /* |
2 | * tifm_7xx1.c - TI FlashMedia driver | |
3 | * | |
4 | * Copyright (C) 2006 Alex Dubov <oakad@yahoo.com> | |
5 | * | |
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. | |
9 | * | |
10 | */ | |
11 | ||
12 | #include <linux/tifm.h> | |
c4030698 | 13 | #include <linux/dma-mapping.h> |
4020f2d7 AD |
14 | |
15 | #define DRIVER_NAME "tifm_7xx1" | |
4552f0cb AD |
16 | #define DRIVER_VERSION "0.8" |
17 | ||
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 | |
4020f2d7 | 23 | |
6113ed73 AD |
24 | static void tifm_7xx1_dummy_eject(struct tifm_adapter *fm, |
25 | struct tifm_dev *sock) | |
26 | { | |
27 | } | |
28 | ||
4020f2d7 AD |
29 | static void tifm_7xx1_eject(struct tifm_adapter *fm, struct tifm_dev *sock) |
30 | { | |
4020f2d7 AD |
31 | unsigned long flags; |
32 | ||
33 | spin_lock_irqsave(&fm->lock, flags); | |
7146f0d3 | 34 | fm->socket_change_set |= 1 << sock->socket_id; |
3540af8f | 35 | tifm_queue_work(&fm->media_switcher); |
4020f2d7 AD |
36 | spin_unlock_irqrestore(&fm->lock, flags); |
37 | } | |
38 | ||
7d12e780 | 39 | static irqreturn_t tifm_7xx1_isr(int irq, void *dev_id) |
4020f2d7 AD |
40 | { |
41 | struct tifm_adapter *fm = dev_id; | |
217334d1 | 42 | struct tifm_dev *sock; |
91f8d011 | 43 | unsigned int irq_status, cnt; |
4020f2d7 AD |
44 | |
45 | spin_lock(&fm->lock); | |
46 | irq_status = readl(fm->addr + FM_INTERRUPT_STATUS); | |
47 | if (irq_status == 0 || irq_status == (~0)) { | |
48 | spin_unlock(&fm->lock); | |
49 | return IRQ_NONE; | |
50 | } | |
51 | ||
52 | if (irq_status & TIFM_IRQ_ENABLE) { | |
53 | writel(TIFM_IRQ_ENABLE, fm->addr + FM_CLEAR_INTERRUPT_ENABLE); | |
54 | ||
8b40adab | 55 | for (cnt = 0; cnt < fm->num_sockets; cnt++) { |
217334d1 | 56 | sock = fm->sockets[cnt]; |
4552f0cb AD |
57 | if (sock) { |
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); | |
62 | } | |
4020f2d7 | 63 | } |
6412d927 | 64 | |
8b40adab AD |
65 | fm->socket_change_set |= irq_status |
66 | & ((1 << fm->num_sockets) - 1); | |
4020f2d7 AD |
67 | } |
68 | writel(irq_status, fm->addr + FM_INTERRUPT_STATUS); | |
69 | ||
3540af8f AD |
70 | if (fm->finish_me) |
71 | complete_all(fm->finish_me); | |
72 | else if (!fm->socket_change_set) | |
7146f0d3 AD |
73 | writel(TIFM_IRQ_ENABLE, fm->addr + FM_SET_INTERRUPT_ENABLE); |
74 | else | |
3540af8f | 75 | tifm_queue_work(&fm->media_switcher); |
4020f2d7 AD |
76 | |
77 | spin_unlock(&fm->lock); | |
78 | return IRQ_HANDLED; | |
79 | } | |
80 | ||
342c0ec4 | 81 | static unsigned char tifm_7xx1_toggle_sock_power(char __iomem *sock_addr) |
4020f2d7 AD |
82 | { |
83 | unsigned int s_state; | |
84 | int cnt; | |
85 | ||
86 | writel(0x0e00, sock_addr + SOCK_CONTROL); | |
87 | ||
342c0ec4 | 88 | for (cnt = 16; cnt <= 256; cnt <<= 1) { |
8b40adab AD |
89 | if (!(TIFM_SOCK_STATE_POWERED |
90 | & readl(sock_addr + SOCK_PRESENT_STATE))) | |
4020f2d7 | 91 | break; |
342c0ec4 AD |
92 | |
93 | msleep(cnt); | |
4020f2d7 AD |
94 | } |
95 | ||
96 | s_state = readl(sock_addr + SOCK_PRESENT_STATE); | |
97 | if (!(TIFM_SOCK_STATE_OCCUPIED & s_state)) | |
e23f2b8a | 98 | return 0; |
4020f2d7 | 99 | |
342c0ec4 AD |
100 | writel(readl(sock_addr + SOCK_CONTROL) | TIFM_CTRL_LED, |
101 | sock_addr + SOCK_CONTROL); | |
4020f2d7 | 102 | |
342c0ec4 AD |
103 | /* xd needs some extra time before power on */ |
104 | if (((readl(sock_addr + SOCK_PRESENT_STATE) >> 4) & 7) | |
105 | == TIFM_TYPE_XD) | |
106 | msleep(40); | |
107 | ||
055b8224 AD |
108 | writel((s_state & TIFM_CTRL_POWER_MASK) | 0x0c00, |
109 | sock_addr + SOCK_CONTROL); | |
342c0ec4 AD |
110 | /* wait for power to stabilize */ |
111 | msleep(20); | |
112 | for (cnt = 16; cnt <= 256; cnt <<= 1) { | |
8b40adab AD |
113 | if ((TIFM_SOCK_STATE_POWERED |
114 | & readl(sock_addr + SOCK_PRESENT_STATE))) | |
4020f2d7 | 115 | break; |
342c0ec4 AD |
116 | |
117 | msleep(cnt); | |
4020f2d7 AD |
118 | } |
119 | ||
342c0ec4 AD |
120 | writel(readl(sock_addr + SOCK_CONTROL) & (~TIFM_CTRL_LED), |
121 | sock_addr + SOCK_CONTROL); | |
4020f2d7 AD |
122 | |
123 | return (readl(sock_addr + SOCK_PRESENT_STATE) >> 4) & 7; | |
124 | } | |
125 | ||
055b8224 AD |
126 | inline static void tifm_7xx1_sock_power_off(char __iomem *sock_addr) |
127 | { | |
128 | writel((~TIFM_CTRL_POWER_MASK) & readl(sock_addr + SOCK_CONTROL), | |
129 | sock_addr + SOCK_CONTROL); | |
130 | } | |
131 | ||
e069d79d AV |
132 | inline static char __iomem * |
133 | tifm_7xx1_sock_addr(char __iomem *base_addr, unsigned int sock_num) | |
4020f2d7 AD |
134 | { |
135 | return base_addr + ((sock_num + 1) << 10); | |
136 | } | |
137 | ||
3540af8f | 138 | static void tifm_7xx1_switch_media(struct work_struct *work) |
4020f2d7 | 139 | { |
3540af8f AD |
140 | struct tifm_adapter *fm = container_of(work, struct tifm_adapter, |
141 | media_switcher); | |
91f8d011 | 142 | struct tifm_dev *sock; |
055b8224 | 143 | char __iomem *sock_addr; |
4020f2d7 | 144 | unsigned long flags; |
e23f2b8a | 145 | unsigned char media_id; |
91f8d011 | 146 | unsigned int socket_change_set, cnt; |
4020f2d7 | 147 | |
3540af8f AD |
148 | spin_lock_irqsave(&fm->lock, flags); |
149 | socket_change_set = fm->socket_change_set; | |
150 | fm->socket_change_set = 0; | |
6412d927 | 151 | |
6113ed73 | 152 | dev_dbg(fm->cdev.dev, "checking media set %x\n", |
3540af8f | 153 | socket_change_set); |
6412d927 | 154 | |
3540af8f | 155 | if (!socket_change_set) { |
4020f2d7 | 156 | spin_unlock_irqrestore(&fm->lock, flags); |
3540af8f AD |
157 | return; |
158 | } | |
4020f2d7 | 159 | |
2428a8fe AD |
160 | for (cnt = 0; cnt < fm->num_sockets; cnt++) { |
161 | if (!(socket_change_set & (1 << cnt))) | |
162 | continue; | |
163 | sock = fm->sockets[cnt]; | |
164 | if (sock) { | |
165 | printk(KERN_INFO | |
166 | "%s : demand removing card from socket %u:%u\n", | |
167 | fm->cdev.class_id, fm->id, cnt); | |
168 | fm->sockets[cnt] = NULL; | |
055b8224 | 169 | sock_addr = sock->addr; |
6412d927 | 170 | spin_unlock_irqrestore(&fm->lock, flags); |
2428a8fe AD |
171 | device_unregister(&sock->dev); |
172 | spin_lock_irqsave(&fm->lock, flags); | |
055b8224 AD |
173 | tifm_7xx1_sock_power_off(sock_addr); |
174 | writel(0x0e00, sock_addr + SOCK_CONTROL); | |
2428a8fe AD |
175 | } |
176 | ||
177 | spin_unlock_irqrestore(&fm->lock, flags); | |
178 | ||
179 | media_id = tifm_7xx1_toggle_sock_power( | |
180 | tifm_7xx1_sock_addr(fm->addr, cnt)); | |
181 | ||
182 | // tifm_alloc_device will check if media_id is valid | |
183 | sock = tifm_alloc_device(fm, cnt, media_id); | |
184 | if (sock) { | |
185 | sock->addr = tifm_7xx1_sock_addr(fm->addr, cnt); | |
186 | ||
187 | if (!device_register(&sock->dev)) { | |
6412d927 | 188 | spin_lock_irqsave(&fm->lock, flags); |
2428a8fe AD |
189 | if (!fm->sockets[cnt]) { |
190 | fm->sockets[cnt] = sock; | |
191 | sock = NULL; | |
192 | } | |
193 | spin_unlock_irqrestore(&fm->lock, flags); | |
6412d927 | 194 | } |
2428a8fe AD |
195 | if (sock) |
196 | tifm_free_device(&sock->dev); | |
6412d927 | 197 | } |
2428a8fe AD |
198 | spin_lock_irqsave(&fm->lock, flags); |
199 | } | |
6412d927 | 200 | |
3540af8f AD |
201 | writel(TIFM_IRQ_FIFOMASK(socket_change_set) |
202 | | TIFM_IRQ_CARDMASK(socket_change_set), | |
203 | fm->addr + FM_CLEAR_INTERRUPT_ENABLE); | |
204 | ||
205 | writel(TIFM_IRQ_FIFOMASK(socket_change_set) | |
206 | | TIFM_IRQ_CARDMASK(socket_change_set), | |
207 | fm->addr + FM_SET_INTERRUPT_ENABLE); | |
208 | ||
209 | writel(TIFM_IRQ_ENABLE, fm->addr + FM_SET_INTERRUPT_ENABLE); | |
210 | spin_unlock_irqrestore(&fm->lock, flags); | |
4020f2d7 AD |
211 | } |
212 | ||
7146f0d3 AD |
213 | #ifdef CONFIG_PM |
214 | ||
4020f2d7 AD |
215 | static int tifm_7xx1_suspend(struct pci_dev *dev, pm_message_t state) |
216 | { | |
055b8224 AD |
217 | struct tifm_adapter *fm = pci_get_drvdata(dev); |
218 | int cnt; | |
219 | ||
7146f0d3 | 220 | dev_dbg(&dev->dev, "suspending host\n"); |
4020f2d7 | 221 | |
055b8224 AD |
222 | for (cnt = 0; cnt < fm->num_sockets; cnt++) { |
223 | if (fm->sockets[cnt]) | |
224 | tifm_7xx1_sock_power_off(fm->sockets[cnt]->addr); | |
225 | } | |
226 | ||
7146f0d3 AD |
227 | pci_save_state(dev); |
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)); | |
4020f2d7 AD |
231 | return 0; |
232 | } | |
233 | ||
234 | static int tifm_7xx1_resume(struct pci_dev *dev) | |
235 | { | |
236 | struct tifm_adapter *fm = pci_get_drvdata(dev); | |
88de1b2f AD |
237 | int rc; |
238 | unsigned int good_sockets = 0, bad_sockets = 0; | |
4020f2d7 | 239 | unsigned long flags; |
e23f2b8a | 240 | unsigned char new_ids[fm->num_sockets]; |
3540af8f | 241 | DECLARE_COMPLETION_ONSTACK(finish_resume); |
4020f2d7 | 242 | |
7146f0d3 | 243 | pci_set_power_state(dev, PCI_D0); |
4020f2d7 | 244 | pci_restore_state(dev); |
7146f0d3 AD |
245 | rc = pci_enable_device(dev); |
246 | if (rc) | |
247 | return rc; | |
248 | pci_set_master(dev); | |
249 | ||
250 | dev_dbg(&dev->dev, "resuming host\n"); | |
4020f2d7 | 251 | |
88de1b2f AD |
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)); | |
4020f2d7 | 255 | spin_lock_irqsave(&fm->lock, flags); |
88de1b2f AD |
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; | |
260 | else | |
261 | bad_sockets |= 1 << rc; | |
7146f0d3 AD |
262 | } |
263 | } | |
264 | ||
6412d927 | 265 | writel(TIFM_IRQ_ENABLE | TIFM_IRQ_SOCKMASK((1 << fm->num_sockets) - 1), |
7146f0d3 | 266 | fm->addr + FM_SET_INTERRUPT_ENABLE); |
88de1b2f AD |
267 | dev_dbg(&dev->dev, "change sets on resume: good %x, bad %x\n", |
268 | good_sockets, bad_sockets); | |
269 | ||
270 | fm->socket_change_set = 0; | |
271 | if (good_sockets) { | |
3540af8f | 272 | fm->finish_me = &finish_resume; |
7146f0d3 | 273 | spin_unlock_irqrestore(&fm->lock, flags); |
88de1b2f AD |
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; | |
7146f0d3 AD |
285 | } |
286 | ||
88de1b2f AD |
287 | fm->socket_change_set |= bad_sockets; |
288 | if (fm->socket_change_set) | |
289 | tifm_queue_work(&fm->media_switcher); | |
7146f0d3 | 290 | |
88de1b2f | 291 | spin_unlock_irqrestore(&fm->lock, flags); |
7146f0d3 AD |
292 | writel(TIFM_IRQ_ENABLE, |
293 | fm->addr + FM_SET_INTERRUPT_ENABLE); | |
7146f0d3 | 294 | |
4020f2d7 AD |
295 | return 0; |
296 | } | |
297 | ||
7146f0d3 AD |
298 | #else |
299 | ||
300 | #define tifm_7xx1_suspend NULL | |
301 | #define tifm_7xx1_resume NULL | |
302 | ||
303 | #endif /* CONFIG_PM */ | |
304 | ||
4020f2d7 | 305 | static int tifm_7xx1_probe(struct pci_dev *dev, |
8b40adab | 306 | const struct pci_device_id *dev_id) |
4020f2d7 AD |
307 | { |
308 | struct tifm_adapter *fm; | |
309 | int pci_dev_busy = 0; | |
310 | int rc; | |
311 | ||
312 | rc = pci_set_dma_mask(dev, DMA_32BIT_MASK); | |
313 | if (rc) | |
314 | return rc; | |
315 | ||
316 | rc = pci_enable_device(dev); | |
317 | if (rc) | |
318 | return rc; | |
319 | ||
320 | pci_set_master(dev); | |
321 | ||
322 | rc = pci_request_regions(dev, DRIVER_NAME); | |
323 | if (rc) { | |
324 | pci_dev_busy = 1; | |
325 | goto err_out; | |
326 | } | |
327 | ||
328 | pci_intx(dev, 1); | |
329 | ||
6113ed73 AD |
330 | fm = tifm_alloc_adapter(dev->device == PCI_DEVICE_ID_TI_XX21_XX11_FM |
331 | ? 4 : 2, &dev->dev); | |
4020f2d7 AD |
332 | if (!fm) { |
333 | rc = -ENOMEM; | |
334 | goto err_out_int; | |
335 | } | |
336 | ||
3540af8f | 337 | INIT_WORK(&fm->media_switcher, tifm_7xx1_switch_media); |
4020f2d7 AD |
338 | fm->eject = tifm_7xx1_eject; |
339 | pci_set_drvdata(dev, fm); | |
340 | ||
341 | fm->addr = ioremap(pci_resource_start(dev, 0), | |
8b40adab | 342 | pci_resource_len(dev, 0)); |
4020f2d7 AD |
343 | if (!fm->addr) |
344 | goto err_out_free; | |
345 | ||
bc913b18 | 346 | rc = request_irq(dev->irq, tifm_7xx1_isr, IRQF_SHARED, DRIVER_NAME, fm); |
4020f2d7 AD |
347 | if (rc) |
348 | goto err_out_unmap; | |
349 | ||
3540af8f | 350 | rc = tifm_add_adapter(fm); |
4020f2d7 AD |
351 | if (rc) |
352 | goto err_out_irq; | |
353 | ||
6412d927 | 354 | writel(TIFM_IRQ_ENABLE | TIFM_IRQ_SOCKMASK((1 << fm->num_sockets) - 1), |
8b40adab | 355 | fm->addr + FM_SET_INTERRUPT_ENABLE); |
4020f2d7 AD |
356 | return 0; |
357 | ||
358 | err_out_irq: | |
359 | free_irq(dev->irq, fm); | |
360 | err_out_unmap: | |
361 | iounmap(fm->addr); | |
362 | err_out_free: | |
363 | pci_set_drvdata(dev, NULL); | |
364 | tifm_free_adapter(fm); | |
365 | err_out_int: | |
366 | pci_intx(dev, 0); | |
367 | pci_release_regions(dev); | |
368 | err_out: | |
369 | if (!pci_dev_busy) | |
370 | pci_disable_device(dev); | |
371 | return rc; | |
372 | } | |
373 | ||
374 | static void tifm_7xx1_remove(struct pci_dev *dev) | |
375 | { | |
376 | struct tifm_adapter *fm = pci_get_drvdata(dev); | |
055b8224 | 377 | int cnt; |
4020f2d7 | 378 | |
6113ed73 | 379 | fm->eject = tifm_7xx1_dummy_eject; |
7146f0d3 AD |
380 | writel(TIFM_IRQ_SETALL, fm->addr + FM_CLEAR_INTERRUPT_ENABLE); |
381 | mmiowb(); | |
382 | free_irq(dev->irq, fm); | |
383 | ||
4020f2d7 AD |
384 | tifm_remove_adapter(fm); |
385 | ||
055b8224 AD |
386 | for (cnt = 0; cnt < fm->num_sockets; cnt++) |
387 | tifm_7xx1_sock_power_off(tifm_7xx1_sock_addr(fm->addr, cnt)); | |
388 | ||
e069d79d | 389 | pci_set_drvdata(dev, NULL); |
4020f2d7 AD |
390 | |
391 | iounmap(fm->addr); | |
392 | pci_intx(dev, 0); | |
393 | pci_release_regions(dev); | |
394 | ||
395 | pci_disable_device(dev); | |
396 | tifm_free_adapter(fm); | |
397 | } | |
398 | ||
399 | static struct pci_device_id tifm_7xx1_pci_tbl [] = { | |
b5ad6761 AD |
400 | { PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_XX21_XX11_FM, PCI_ANY_ID, |
401 | PCI_ANY_ID, 0, 0, 0 }, /* xx21 - the one I have */ | |
402 | { PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_XX12_FM, PCI_ANY_ID, | |
403 | PCI_ANY_ID, 0, 0, 0 }, | |
404 | { PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_XX20_FM, PCI_ANY_ID, | |
405 | PCI_ANY_ID, 0, 0, 0 }, | |
4020f2d7 AD |
406 | { } |
407 | }; | |
408 | ||
409 | static struct pci_driver tifm_7xx1_driver = { | |
410 | .name = DRIVER_NAME, | |
411 | .id_table = tifm_7xx1_pci_tbl, | |
412 | .probe = tifm_7xx1_probe, | |
413 | .remove = tifm_7xx1_remove, | |
414 | .suspend = tifm_7xx1_suspend, | |
415 | .resume = tifm_7xx1_resume, | |
416 | }; | |
417 | ||
418 | static int __init tifm_7xx1_init(void) | |
419 | { | |
420 | return pci_register_driver(&tifm_7xx1_driver); | |
421 | } | |
422 | ||
423 | static void __exit tifm_7xx1_exit(void) | |
424 | { | |
425 | pci_unregister_driver(&tifm_7xx1_driver); | |
426 | } | |
427 | ||
428 | MODULE_AUTHOR("Alex Dubov"); | |
429 | MODULE_DESCRIPTION("TI FlashMedia host driver"); | |
430 | MODULE_LICENSE("GPL"); | |
431 | MODULE_DEVICE_TABLE(pci, tifm_7xx1_pci_tbl); | |
432 | MODULE_VERSION(DRIVER_VERSION); | |
433 | ||
434 | module_init(tifm_7xx1_init); | |
435 | module_exit(tifm_7xx1_exit); |