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/module.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
, cnt
;
47 irq_status
= readl(fm
->addr
+ FM_INTERRUPT_STATUS
);
48 if (irq_status
== 0 || irq_status
== (~0)) {
49 spin_unlock(&fm
->lock
);
53 if (irq_status
& TIFM_IRQ_ENABLE
) {
54 writel(TIFM_IRQ_ENABLE
, fm
->addr
+ FM_CLEAR_INTERRUPT_ENABLE
);
56 for (cnt
= 0; cnt
< fm
->num_sockets
; cnt
++) {
57 sock
= fm
->sockets
[cnt
];
59 if ((irq_status
>> cnt
) & TIFM_IRQ_FIFOMASK(1))
60 sock
->data_event(sock
);
61 if ((irq_status
>> cnt
) & TIFM_IRQ_CARDMASK(1))
62 sock
->card_event(sock
);
66 fm
->socket_change_set
|= irq_status
67 & ((1 << fm
->num_sockets
) - 1);
69 writel(irq_status
, fm
->addr
+ FM_INTERRUPT_STATUS
);
72 complete_all(fm
->finish_me
);
73 else if (!fm
->socket_change_set
)
74 writel(TIFM_IRQ_ENABLE
, fm
->addr
+ FM_SET_INTERRUPT_ENABLE
);
76 tifm_queue_work(&fm
->media_switcher
);
78 spin_unlock(&fm
->lock
);
82 static unsigned char tifm_7xx1_toggle_sock_power(char __iomem
*sock_addr
)
87 writel(0x0e00, sock_addr
+ SOCK_CONTROL
);
89 for (cnt
= 16; cnt
<= 256; cnt
<<= 1) {
90 if (!(TIFM_SOCK_STATE_POWERED
91 & readl(sock_addr
+ SOCK_PRESENT_STATE
)))
97 s_state
= readl(sock_addr
+ SOCK_PRESENT_STATE
);
98 if (!(TIFM_SOCK_STATE_OCCUPIED
& s_state
))
101 writel(readl(sock_addr
+ SOCK_CONTROL
) | TIFM_CTRL_LED
,
102 sock_addr
+ SOCK_CONTROL
);
104 /* xd needs some extra time before power on */
105 if (((readl(sock_addr
+ SOCK_PRESENT_STATE
) >> 4) & 7)
109 writel((s_state
& TIFM_CTRL_POWER_MASK
) | 0x0c00,
110 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 void tifm_7xx1_sock_power_off(char __iomem
*sock_addr
)
129 writel((~TIFM_CTRL_POWER_MASK
) & readl(sock_addr
+ SOCK_CONTROL
),
130 sock_addr
+ SOCK_CONTROL
);
133 inline static char __iomem
*
134 tifm_7xx1_sock_addr(char __iomem
*base_addr
, unsigned int sock_num
)
136 return base_addr
+ ((sock_num
+ 1) << 10);
139 static void tifm_7xx1_switch_media(struct work_struct
*work
)
141 struct tifm_adapter
*fm
= container_of(work
, struct tifm_adapter
,
143 struct tifm_dev
*sock
;
144 char __iomem
*sock_addr
;
146 unsigned char media_id
;
147 unsigned int socket_change_set
, cnt
;
149 spin_lock_irqsave(&fm
->lock
, flags
);
150 socket_change_set
= fm
->socket_change_set
;
151 fm
->socket_change_set
= 0;
153 dev_dbg(fm
->dev
.parent
, "checking media set %x\n",
156 if (!socket_change_set
) {
157 spin_unlock_irqrestore(&fm
->lock
, flags
);
161 for (cnt
= 0; cnt
< fm
->num_sockets
; cnt
++) {
162 if (!(socket_change_set
& (1 << cnt
)))
164 sock
= fm
->sockets
[cnt
];
167 "%s : demand removing card from socket %u:%u\n",
168 dev_name(&fm
->dev
), fm
->id
, cnt
);
169 fm
->sockets
[cnt
] = NULL
;
170 sock_addr
= sock
->addr
;
171 spin_unlock_irqrestore(&fm
->lock
, flags
);
172 device_unregister(&sock
->dev
);
173 spin_lock_irqsave(&fm
->lock
, flags
);
174 tifm_7xx1_sock_power_off(sock_addr
);
175 writel(0x0e00, sock_addr
+ SOCK_CONTROL
);
178 spin_unlock_irqrestore(&fm
->lock
, flags
);
180 media_id
= tifm_7xx1_toggle_sock_power(
181 tifm_7xx1_sock_addr(fm
->addr
, cnt
));
183 // tifm_alloc_device will check if media_id is valid
184 sock
= tifm_alloc_device(fm
, cnt
, media_id
);
186 sock
->addr
= tifm_7xx1_sock_addr(fm
->addr
, cnt
);
188 if (!device_register(&sock
->dev
)) {
189 spin_lock_irqsave(&fm
->lock
, flags
);
190 if (!fm
->sockets
[cnt
]) {
191 fm
->sockets
[cnt
] = sock
;
194 spin_unlock_irqrestore(&fm
->lock
, flags
);
197 tifm_free_device(&sock
->dev
);
199 spin_lock_irqsave(&fm
->lock
, flags
);
202 writel(TIFM_IRQ_FIFOMASK(socket_change_set
)
203 | TIFM_IRQ_CARDMASK(socket_change_set
),
204 fm
->addr
+ FM_CLEAR_INTERRUPT_ENABLE
);
206 writel(TIFM_IRQ_FIFOMASK(socket_change_set
)
207 | TIFM_IRQ_CARDMASK(socket_change_set
),
208 fm
->addr
+ FM_SET_INTERRUPT_ENABLE
);
210 writel(TIFM_IRQ_ENABLE
, fm
->addr
+ FM_SET_INTERRUPT_ENABLE
);
211 spin_unlock_irqrestore(&fm
->lock
, flags
);
216 static int tifm_7xx1_suspend(struct pci_dev
*dev
, pm_message_t state
)
218 struct tifm_adapter
*fm
= pci_get_drvdata(dev
);
221 dev_dbg(&dev
->dev
, "suspending host\n");
223 for (cnt
= 0; cnt
< fm
->num_sockets
; cnt
++) {
224 if (fm
->sockets
[cnt
])
225 tifm_7xx1_sock_power_off(fm
->sockets
[cnt
]->addr
);
229 pci_enable_wake(dev
, pci_choose_state(dev
, state
), 0);
230 pci_disable_device(dev
);
231 pci_set_power_state(dev
, pci_choose_state(dev
, state
));
235 static int tifm_7xx1_resume(struct pci_dev
*dev
)
237 struct tifm_adapter
*fm
= pci_get_drvdata(dev
);
239 unsigned long timeout
;
240 unsigned int good_sockets
= 0, bad_sockets
= 0;
242 /* Maximum number of entries is 4 */
243 unsigned char new_ids
[4];
244 DECLARE_COMPLETION_ONSTACK(finish_resume
);
246 if (WARN_ON(fm
->num_sockets
> ARRAY_SIZE(new_ids
)))
249 pci_set_power_state(dev
, PCI_D0
);
250 pci_restore_state(dev
);
251 rc
= pci_enable_device(dev
);
256 dev_dbg(&dev
->dev
, "resuming host\n");
258 for (rc
= 0; rc
< fm
->num_sockets
; rc
++)
259 new_ids
[rc
] = tifm_7xx1_toggle_sock_power(
260 tifm_7xx1_sock_addr(fm
->addr
, rc
));
261 spin_lock_irqsave(&fm
->lock
, flags
);
262 for (rc
= 0; rc
< fm
->num_sockets
; rc
++) {
263 if (fm
->sockets
[rc
]) {
264 if (fm
->sockets
[rc
]->type
== new_ids
[rc
])
265 good_sockets
|= 1 << rc
;
267 bad_sockets
|= 1 << rc
;
271 writel(TIFM_IRQ_ENABLE
| TIFM_IRQ_SOCKMASK((1 << fm
->num_sockets
) - 1),
272 fm
->addr
+ FM_SET_INTERRUPT_ENABLE
);
273 dev_dbg(&dev
->dev
, "change sets on resume: good %x, bad %x\n",
274 good_sockets
, bad_sockets
);
276 fm
->socket_change_set
= 0;
278 fm
->finish_me
= &finish_resume
;
279 spin_unlock_irqrestore(&fm
->lock
, flags
);
280 timeout
= wait_for_completion_timeout(&finish_resume
, HZ
);
281 dev_dbg(&dev
->dev
, "wait returned %lu\n", timeout
);
282 writel(TIFM_IRQ_FIFOMASK(good_sockets
)
283 | TIFM_IRQ_CARDMASK(good_sockets
),
284 fm
->addr
+ FM_CLEAR_INTERRUPT_ENABLE
);
285 writel(TIFM_IRQ_FIFOMASK(good_sockets
)
286 | TIFM_IRQ_CARDMASK(good_sockets
),
287 fm
->addr
+ FM_SET_INTERRUPT_ENABLE
);
288 spin_lock_irqsave(&fm
->lock
, flags
);
289 fm
->finish_me
= NULL
;
290 fm
->socket_change_set
^= good_sockets
& fm
->socket_change_set
;
293 fm
->socket_change_set
|= bad_sockets
;
294 if (fm
->socket_change_set
)
295 tifm_queue_work(&fm
->media_switcher
);
297 spin_unlock_irqrestore(&fm
->lock
, flags
);
298 writel(TIFM_IRQ_ENABLE
,
299 fm
->addr
+ FM_SET_INTERRUPT_ENABLE
);
306 #define tifm_7xx1_suspend NULL
307 #define tifm_7xx1_resume NULL
309 #endif /* CONFIG_PM */
311 static int tifm_7xx1_dummy_has_ms_pif(struct tifm_adapter
*fm
,
312 struct tifm_dev
*sock
)
317 static int tifm_7xx1_has_ms_pif(struct tifm_adapter
*fm
, struct tifm_dev
*sock
)
319 if (((fm
->num_sockets
== 4) && (sock
->socket_id
== 2))
320 || ((fm
->num_sockets
== 2) && (sock
->socket_id
== 0)))
326 static int tifm_7xx1_probe(struct pci_dev
*dev
,
327 const struct pci_device_id
*dev_id
)
329 struct tifm_adapter
*fm
;
330 int pci_dev_busy
= 0;
333 rc
= pci_set_dma_mask(dev
, DMA_BIT_MASK(32));
337 rc
= pci_enable_device(dev
);
343 rc
= pci_request_regions(dev
, DRIVER_NAME
);
351 fm
= tifm_alloc_adapter(dev
->device
== PCI_DEVICE_ID_TI_XX21_XX11_FM
358 INIT_WORK(&fm
->media_switcher
, tifm_7xx1_switch_media
);
359 fm
->eject
= tifm_7xx1_eject
;
360 fm
->has_ms_pif
= tifm_7xx1_has_ms_pif
;
361 pci_set_drvdata(dev
, fm
);
363 fm
->addr
= pci_ioremap_bar(dev
, 0);
369 rc
= request_irq(dev
->irq
, tifm_7xx1_isr
, IRQF_SHARED
, DRIVER_NAME
, fm
);
373 rc
= tifm_add_adapter(fm
);
377 writel(TIFM_IRQ_ENABLE
| TIFM_IRQ_SOCKMASK((1 << fm
->num_sockets
) - 1),
378 fm
->addr
+ FM_CLEAR_INTERRUPT_ENABLE
);
379 writel(TIFM_IRQ_ENABLE
| TIFM_IRQ_SOCKMASK((1 << fm
->num_sockets
) - 1),
380 fm
->addr
+ FM_SET_INTERRUPT_ENABLE
);
384 free_irq(dev
->irq
, fm
);
388 tifm_free_adapter(fm
);
391 pci_release_regions(dev
);
394 pci_disable_device(dev
);
398 static void tifm_7xx1_remove(struct pci_dev
*dev
)
400 struct tifm_adapter
*fm
= pci_get_drvdata(dev
);
403 fm
->eject
= tifm_7xx1_dummy_eject
;
404 fm
->has_ms_pif
= tifm_7xx1_dummy_has_ms_pif
;
405 writel(TIFM_IRQ_SETALL
, fm
->addr
+ FM_CLEAR_INTERRUPT_ENABLE
);
407 free_irq(dev
->irq
, fm
);
409 tifm_remove_adapter(fm
);
411 for (cnt
= 0; cnt
< fm
->num_sockets
; cnt
++)
412 tifm_7xx1_sock_power_off(tifm_7xx1_sock_addr(fm
->addr
, cnt
));
416 pci_release_regions(dev
);
418 pci_disable_device(dev
);
419 tifm_free_adapter(fm
);
422 static const struct pci_device_id tifm_7xx1_pci_tbl
[] = {
423 { PCI_VENDOR_ID_TI
, PCI_DEVICE_ID_TI_XX21_XX11_FM
, PCI_ANY_ID
,
424 PCI_ANY_ID
, 0, 0, 0 }, /* xx21 - the one I have */
425 { PCI_VENDOR_ID_TI
, PCI_DEVICE_ID_TI_XX12_FM
, PCI_ANY_ID
,
426 PCI_ANY_ID
, 0, 0, 0 },
427 { PCI_VENDOR_ID_TI
, PCI_DEVICE_ID_TI_XX20_FM
, PCI_ANY_ID
,
428 PCI_ANY_ID
, 0, 0, 0 },
432 static struct pci_driver tifm_7xx1_driver
= {
434 .id_table
= tifm_7xx1_pci_tbl
,
435 .probe
= tifm_7xx1_probe
,
436 .remove
= tifm_7xx1_remove
,
437 .suspend
= tifm_7xx1_suspend
,
438 .resume
= tifm_7xx1_resume
,
441 module_pci_driver(tifm_7xx1_driver
);
442 MODULE_AUTHOR("Alex Dubov");
443 MODULE_DESCRIPTION("TI FlashMedia host driver");
444 MODULE_LICENSE("GPL");
445 MODULE_DEVICE_TABLE(pci
, tifm_7xx1_pci_tbl
);
446 MODULE_VERSION(DRIVER_VERSION
);