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