blob: 1ba6c085419a243fb3afcbf59acf092a90559d05 [file] [log] [blame]
Alex Dubov4020f2d2006-10-04 02:15:37 -07001/*
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>
Andrew Mortonc4030692007-04-28 14:21:10 +020013#include <linux/dma-mapping.h>
Alex Dubov4020f2d2006-10-04 02:15:37 -070014
15#define DRIVER_NAME "tifm_7xx1"
Alex Dubov4552f0c2007-04-12 16:59:12 +100016#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
Alex Dubov4020f2d2006-10-04 02:15:37 -070023
Alex Dubov6113ed72007-04-12 16:59:17 +100024static void tifm_7xx1_dummy_eject(struct tifm_adapter *fm,
25 struct tifm_dev *sock)
26{
27}
28
Alex Dubov4020f2d2006-10-04 02:15:37 -070029static void tifm_7xx1_eject(struct tifm_adapter *fm, struct tifm_dev *sock)
30{
Alex Dubov4020f2d2006-10-04 02:15:37 -070031 unsigned long flags;
32
33 spin_lock_irqsave(&fm->lock, flags);
Alex Dubov7146f0d2006-12-18 14:20:06 +110034 fm->socket_change_set |= 1 << sock->socket_id;
Alex Dubov3540af82007-04-12 16:59:15 +100035 tifm_queue_work(&fm->media_switcher);
Alex Dubov4020f2d2006-10-04 02:15:37 -070036 spin_unlock_irqrestore(&fm->lock, flags);
37}
38
David Howells7d12e782006-10-05 14:55:46 +010039static irqreturn_t tifm_7xx1_isr(int irq, void *dev_id)
Alex Dubov4020f2d2006-10-04 02:15:37 -070040{
41 struct tifm_adapter *fm = dev_id;
Alex Dubov217334d2006-12-11 01:55:31 +110042 struct tifm_dev *sock;
Alex Dubov91f8d012007-04-12 17:05:26 +100043 unsigned int irq_status, cnt;
Alex Dubov4020f2d2006-10-04 02:15:37 -070044
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
Alex Dubov8b40ada2006-12-11 01:55:36 +110055 for (cnt = 0; cnt < fm->num_sockets; cnt++) {
Alex Dubov217334d2006-12-11 01:55:31 +110056 sock = fm->sockets[cnt];
Alex Dubov4552f0c2007-04-12 16:59:12 +100057 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 }
Alex Dubov4020f2d2006-10-04 02:15:37 -070063 }
Alex Dubov6412d922006-12-11 01:55:33 +110064
Alex Dubov8b40ada2006-12-11 01:55:36 +110065 fm->socket_change_set |= irq_status
66 & ((1 << fm->num_sockets) - 1);
Alex Dubov4020f2d2006-10-04 02:15:37 -070067 }
68 writel(irq_status, fm->addr + FM_INTERRUPT_STATUS);
69
Alex Dubov3540af82007-04-12 16:59:15 +100070 if (fm->finish_me)
71 complete_all(fm->finish_me);
72 else if (!fm->socket_change_set)
Alex Dubov7146f0d2006-12-18 14:20:06 +110073 writel(TIFM_IRQ_ENABLE, fm->addr + FM_SET_INTERRUPT_ENABLE);
74 else
Alex Dubov3540af82007-04-12 16:59:15 +100075 tifm_queue_work(&fm->media_switcher);
Alex Dubov4020f2d2006-10-04 02:15:37 -070076
77 spin_unlock(&fm->lock);
78 return IRQ_HANDLED;
79}
80
Alex Dubov342c0ec2007-04-12 16:59:16 +100081static unsigned char tifm_7xx1_toggle_sock_power(char __iomem *sock_addr)
Alex Dubov4020f2d2006-10-04 02:15:37 -070082{
83 unsigned int s_state;
84 int cnt;
85
86 writel(0x0e00, sock_addr + SOCK_CONTROL);
87
Alex Dubov342c0ec2007-04-12 16:59:16 +100088 for (cnt = 16; cnt <= 256; cnt <<= 1) {
Alex Dubov8b40ada2006-12-11 01:55:36 +110089 if (!(TIFM_SOCK_STATE_POWERED
90 & readl(sock_addr + SOCK_PRESENT_STATE)))
Alex Dubov4020f2d2006-10-04 02:15:37 -070091 break;
Alex Dubov342c0ec2007-04-12 16:59:16 +100092
93 msleep(cnt);
Alex Dubov4020f2d2006-10-04 02:15:37 -070094 }
95
96 s_state = readl(sock_addr + SOCK_PRESENT_STATE);
97 if (!(TIFM_SOCK_STATE_OCCUPIED & s_state))
Alex Dubove23f2b82007-04-12 16:59:14 +100098 return 0;
Alex Dubov4020f2d2006-10-04 02:15:37 -070099
Alex Dubov342c0ec2007-04-12 16:59:16 +1000100 writel(readl(sock_addr + SOCK_CONTROL) | TIFM_CTRL_LED,
101 sock_addr + SOCK_CONTROL);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700102
Alex Dubov342c0ec2007-04-12 16:59:16 +1000103 /* 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
108 writel((s_state & 7) | 0x0c00, sock_addr + SOCK_CONTROL);
109 /* wait for power to stabilize */
110 msleep(20);
111 for (cnt = 16; cnt <= 256; cnt <<= 1) {
Alex Dubov8b40ada2006-12-11 01:55:36 +1100112 if ((TIFM_SOCK_STATE_POWERED
113 & readl(sock_addr + SOCK_PRESENT_STATE)))
Alex Dubov4020f2d2006-10-04 02:15:37 -0700114 break;
Alex Dubov342c0ec2007-04-12 16:59:16 +1000115
116 msleep(cnt);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700117 }
118
Alex Dubov342c0ec2007-04-12 16:59:16 +1000119 writel(readl(sock_addr + SOCK_CONTROL) & (~TIFM_CTRL_LED),
120 sock_addr + SOCK_CONTROL);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700121
122 return (readl(sock_addr + SOCK_PRESENT_STATE) >> 4) & 7;
123}
124
Al Viroe069d792006-10-09 20:29:43 +0100125inline static char __iomem *
126tifm_7xx1_sock_addr(char __iomem *base_addr, unsigned int sock_num)
Alex Dubov4020f2d2006-10-04 02:15:37 -0700127{
128 return base_addr + ((sock_num + 1) << 10);
129}
130
Alex Dubov3540af82007-04-12 16:59:15 +1000131static void tifm_7xx1_switch_media(struct work_struct *work)
Alex Dubov4020f2d2006-10-04 02:15:37 -0700132{
Alex Dubov3540af82007-04-12 16:59:15 +1000133 struct tifm_adapter *fm = container_of(work, struct tifm_adapter,
134 media_switcher);
Alex Dubov91f8d012007-04-12 17:05:26 +1000135 struct tifm_dev *sock;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700136 unsigned long flags;
Alex Dubove23f2b82007-04-12 16:59:14 +1000137 unsigned char media_id;
Alex Dubov91f8d012007-04-12 17:05:26 +1000138 unsigned int socket_change_set, cnt;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700139
Alex Dubov3540af82007-04-12 16:59:15 +1000140 spin_lock_irqsave(&fm->lock, flags);
141 socket_change_set = fm->socket_change_set;
142 fm->socket_change_set = 0;
Alex Dubov7146f0d2006-12-18 14:20:06 +1100143
Alex Dubov6113ed72007-04-12 16:59:17 +1000144 dev_dbg(fm->cdev.dev, "checking media set %x\n",
Alex Dubov3540af82007-04-12 16:59:15 +1000145 socket_change_set);
Alex Dubov6412d922006-12-11 01:55:33 +1100146
Alex Dubov3540af82007-04-12 16:59:15 +1000147 if (!socket_change_set) {
Alex Dubov4020f2d2006-10-04 02:15:37 -0700148 spin_unlock_irqrestore(&fm->lock, flags);
Alex Dubov3540af82007-04-12 16:59:15 +1000149 return;
150 }
Alex Dubov4020f2d2006-10-04 02:15:37 -0700151
Alex Dubov2428a8f2007-04-12 16:59:18 +1000152 for (cnt = 0; cnt < fm->num_sockets; cnt++) {
153 if (!(socket_change_set & (1 << cnt)))
154 continue;
155 sock = fm->sockets[cnt];
156 if (sock) {
157 printk(KERN_INFO
158 "%s : demand removing card from socket %u:%u\n",
159 fm->cdev.class_id, fm->id, cnt);
160 fm->sockets[cnt] = NULL;
Alex Dubov6412d922006-12-11 01:55:33 +1100161 spin_unlock_irqrestore(&fm->lock, flags);
Alex Dubov2428a8f2007-04-12 16:59:18 +1000162 device_unregister(&sock->dev);
163 spin_lock_irqsave(&fm->lock, flags);
164 writel(0x0e00, tifm_7xx1_sock_addr(fm->addr, cnt)
165 + SOCK_CONTROL);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700166 }
Alex Dubov4020f2d2006-10-04 02:15:37 -0700167
Alex Dubov2428a8f2007-04-12 16:59:18 +1000168 spin_unlock_irqrestore(&fm->lock, flags);
169
170 media_id = tifm_7xx1_toggle_sock_power(
171 tifm_7xx1_sock_addr(fm->addr, cnt));
172
173 // tifm_alloc_device will check if media_id is valid
174 sock = tifm_alloc_device(fm, cnt, media_id);
175 if (sock) {
176 sock->addr = tifm_7xx1_sock_addr(fm->addr, cnt);
177
178 if (!device_register(&sock->dev)) {
179 spin_lock_irqsave(&fm->lock, flags);
180 if (!fm->sockets[cnt]) {
181 fm->sockets[cnt] = sock;
182 sock = NULL;
183 }
184 spin_unlock_irqrestore(&fm->lock, flags);
185 }
186 if (sock)
187 tifm_free_device(&sock->dev);
188 }
189 spin_lock_irqsave(&fm->lock, flags);
190 }
191
Alex Dubov3540af82007-04-12 16:59:15 +1000192 writel(TIFM_IRQ_FIFOMASK(socket_change_set)
193 | TIFM_IRQ_CARDMASK(socket_change_set),
194 fm->addr + FM_CLEAR_INTERRUPT_ENABLE);
195
196 writel(TIFM_IRQ_FIFOMASK(socket_change_set)
197 | TIFM_IRQ_CARDMASK(socket_change_set),
198 fm->addr + FM_SET_INTERRUPT_ENABLE);
199
200 writel(TIFM_IRQ_ENABLE, fm->addr + FM_SET_INTERRUPT_ENABLE);
201 spin_unlock_irqrestore(&fm->lock, flags);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700202}
203
Alex Dubov7146f0d2006-12-18 14:20:06 +1100204#ifdef CONFIG_PM
205
Alex Dubov4020f2d2006-10-04 02:15:37 -0700206static int tifm_7xx1_suspend(struct pci_dev *dev, pm_message_t state)
207{
Alex Dubov7146f0d2006-12-18 14:20:06 +1100208 dev_dbg(&dev->dev, "suspending host\n");
Alex Dubov4020f2d2006-10-04 02:15:37 -0700209
Alex Dubov7146f0d2006-12-18 14:20:06 +1100210 pci_save_state(dev);
211 pci_enable_wake(dev, pci_choose_state(dev, state), 0);
212 pci_disable_device(dev);
213 pci_set_power_state(dev, pci_choose_state(dev, state));
Alex Dubov4020f2d2006-10-04 02:15:37 -0700214 return 0;
215}
216
217static int tifm_7xx1_resume(struct pci_dev *dev)
218{
219 struct tifm_adapter *fm = pci_get_drvdata(dev);
Alex Dubov88de1b22007-04-12 16:59:19 +1000220 int rc;
221 unsigned int good_sockets = 0, bad_sockets = 0;
Alex Dubov4020f2d2006-10-04 02:15:37 -0700222 unsigned long flags;
Alex Dubove23f2b82007-04-12 16:59:14 +1000223 unsigned char new_ids[fm->num_sockets];
Alex Dubov3540af82007-04-12 16:59:15 +1000224 DECLARE_COMPLETION_ONSTACK(finish_resume);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700225
Alex Dubov7146f0d2006-12-18 14:20:06 +1100226 pci_set_power_state(dev, PCI_D0);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700227 pci_restore_state(dev);
Alex Dubov7146f0d2006-12-18 14:20:06 +1100228 rc = pci_enable_device(dev);
229 if (rc)
230 return rc;
231 pci_set_master(dev);
232
233 dev_dbg(&dev->dev, "resuming host\n");
234
Alex Dubov88de1b22007-04-12 16:59:19 +1000235 for (rc = 0; rc < fm->num_sockets; rc++)
236 new_ids[rc] = tifm_7xx1_toggle_sock_power(
237 tifm_7xx1_sock_addr(fm->addr, rc));
Alex Dubov7146f0d2006-12-18 14:20:06 +1100238 spin_lock_irqsave(&fm->lock, flags);
Alex Dubov88de1b22007-04-12 16:59:19 +1000239 for (rc = 0; rc < fm->num_sockets; rc++) {
240 if (fm->sockets[rc]) {
241 if (fm->sockets[rc]->type == new_ids[rc])
242 good_sockets |= 1 << rc;
243 else
244 bad_sockets |= 1 << rc;
Alex Dubov7146f0d2006-12-18 14:20:06 +1100245 }
246 }
247
248 writel(TIFM_IRQ_ENABLE | TIFM_IRQ_SOCKMASK((1 << fm->num_sockets) - 1),
249 fm->addr + FM_SET_INTERRUPT_ENABLE);
Alex Dubov88de1b22007-04-12 16:59:19 +1000250 dev_dbg(&dev->dev, "change sets on resume: good %x, bad %x\n",
251 good_sockets, bad_sockets);
252
253 fm->socket_change_set = 0;
254 if (good_sockets) {
Alex Dubov3540af82007-04-12 16:59:15 +1000255 fm->finish_me = &finish_resume;
Alex Dubov7146f0d2006-12-18 14:20:06 +1100256 spin_unlock_irqrestore(&fm->lock, flags);
Alex Dubov88de1b22007-04-12 16:59:19 +1000257 rc = wait_for_completion_timeout(&finish_resume, HZ);
258 dev_dbg(&dev->dev, "wait returned %d\n", rc);
259 writel(TIFM_IRQ_FIFOMASK(good_sockets)
260 | TIFM_IRQ_CARDMASK(good_sockets),
261 fm->addr + FM_CLEAR_INTERRUPT_ENABLE);
262 writel(TIFM_IRQ_FIFOMASK(good_sockets)
263 | TIFM_IRQ_CARDMASK(good_sockets),
264 fm->addr + FM_SET_INTERRUPT_ENABLE);
265 spin_lock_irqsave(&fm->lock, flags);
266 fm->finish_me = NULL;
267 fm->socket_change_set ^= good_sockets & fm->socket_change_set;
Alex Dubov7146f0d2006-12-18 14:20:06 +1100268 }
269
Alex Dubov88de1b22007-04-12 16:59:19 +1000270 fm->socket_change_set |= bad_sockets;
271 if (fm->socket_change_set)
272 tifm_queue_work(&fm->media_switcher);
Alex Dubov7146f0d2006-12-18 14:20:06 +1100273
Alex Dubov4020f2d2006-10-04 02:15:37 -0700274 spin_unlock_irqrestore(&fm->lock, flags);
Alex Dubov88de1b22007-04-12 16:59:19 +1000275 writel(TIFM_IRQ_ENABLE,
276 fm->addr + FM_SET_INTERRUPT_ENABLE);
277
Alex Dubov4020f2d2006-10-04 02:15:37 -0700278 return 0;
279}
280
Alex Dubov7146f0d2006-12-18 14:20:06 +1100281#else
282
283#define tifm_7xx1_suspend NULL
284#define tifm_7xx1_resume NULL
285
286#endif /* CONFIG_PM */
287
Alex Dubov4020f2d2006-10-04 02:15:37 -0700288static int tifm_7xx1_probe(struct pci_dev *dev,
Alex Dubov8b40ada2006-12-11 01:55:36 +1100289 const struct pci_device_id *dev_id)
Alex Dubov4020f2d2006-10-04 02:15:37 -0700290{
291 struct tifm_adapter *fm;
292 int pci_dev_busy = 0;
293 int rc;
294
295 rc = pci_set_dma_mask(dev, DMA_32BIT_MASK);
296 if (rc)
297 return rc;
298
299 rc = pci_enable_device(dev);
300 if (rc)
301 return rc;
302
303 pci_set_master(dev);
304
305 rc = pci_request_regions(dev, DRIVER_NAME);
306 if (rc) {
307 pci_dev_busy = 1;
308 goto err_out;
309 }
310
311 pci_intx(dev, 1);
312
Alex Dubov6113ed72007-04-12 16:59:17 +1000313 fm = tifm_alloc_adapter(dev->device == PCI_DEVICE_ID_TI_XX21_XX11_FM
314 ? 4 : 2, &dev->dev);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700315 if (!fm) {
316 rc = -ENOMEM;
317 goto err_out_int;
318 }
319
Alex Dubov3540af82007-04-12 16:59:15 +1000320 INIT_WORK(&fm->media_switcher, tifm_7xx1_switch_media);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700321 fm->eject = tifm_7xx1_eject;
322 pci_set_drvdata(dev, fm);
323
324 fm->addr = ioremap(pci_resource_start(dev, 0),
Alex Dubov8b40ada2006-12-11 01:55:36 +1100325 pci_resource_len(dev, 0));
Alex Dubov4020f2d2006-10-04 02:15:37 -0700326 if (!fm->addr)
327 goto err_out_free;
328
Alex Dubov6113ed72007-04-12 16:59:17 +1000329 rc = request_irq(dev->irq, tifm_7xx1_isr, SA_SHIRQ, DRIVER_NAME, fm);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700330 if (rc)
331 goto err_out_unmap;
332
Alex Dubov3540af82007-04-12 16:59:15 +1000333 rc = tifm_add_adapter(fm);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700334 if (rc)
335 goto err_out_irq;
336
Alex Dubov6412d922006-12-11 01:55:33 +1100337 writel(TIFM_IRQ_ENABLE | TIFM_IRQ_SOCKMASK((1 << fm->num_sockets) - 1),
Alex Dubov8b40ada2006-12-11 01:55:36 +1100338 fm->addr + FM_SET_INTERRUPT_ENABLE);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700339 return 0;
340
341err_out_irq:
342 free_irq(dev->irq, fm);
343err_out_unmap:
344 iounmap(fm->addr);
345err_out_free:
346 pci_set_drvdata(dev, NULL);
347 tifm_free_adapter(fm);
348err_out_int:
349 pci_intx(dev, 0);
350 pci_release_regions(dev);
351err_out:
352 if (!pci_dev_busy)
353 pci_disable_device(dev);
354 return rc;
355}
356
357static void tifm_7xx1_remove(struct pci_dev *dev)
358{
359 struct tifm_adapter *fm = pci_get_drvdata(dev);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700360
Alex Dubov6113ed72007-04-12 16:59:17 +1000361 fm->eject = tifm_7xx1_dummy_eject;
Alex Dubov7146f0d2006-12-18 14:20:06 +1100362 writel(TIFM_IRQ_SETALL, fm->addr + FM_CLEAR_INTERRUPT_ENABLE);
363 mmiowb();
364 free_irq(dev->irq, fm);
365
Alex Dubov4020f2d2006-10-04 02:15:37 -0700366 tifm_remove_adapter(fm);
367
Al Viroe069d792006-10-09 20:29:43 +0100368 pci_set_drvdata(dev, NULL);
Alex Dubov4020f2d2006-10-04 02:15:37 -0700369
370 iounmap(fm->addr);
371 pci_intx(dev, 0);
372 pci_release_regions(dev);
373
374 pci_disable_device(dev);
375 tifm_free_adapter(fm);
376}
377
378static struct pci_device_id tifm_7xx1_pci_tbl [] = {
Alex Dubovb5ad6762006-12-11 01:55:35 +1100379 { PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_XX21_XX11_FM, PCI_ANY_ID,
380 PCI_ANY_ID, 0, 0, 0 }, /* xx21 - the one I have */
381 { PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_XX12_FM, PCI_ANY_ID,
382 PCI_ANY_ID, 0, 0, 0 },
383 { PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_XX20_FM, PCI_ANY_ID,
384 PCI_ANY_ID, 0, 0, 0 },
Alex Dubov4020f2d2006-10-04 02:15:37 -0700385 { }
386};
387
388static struct pci_driver tifm_7xx1_driver = {
389 .name = DRIVER_NAME,
390 .id_table = tifm_7xx1_pci_tbl,
391 .probe = tifm_7xx1_probe,
392 .remove = tifm_7xx1_remove,
393 .suspend = tifm_7xx1_suspend,
394 .resume = tifm_7xx1_resume,
395};
396
397static int __init tifm_7xx1_init(void)
398{
399 return pci_register_driver(&tifm_7xx1_driver);
400}
401
402static void __exit tifm_7xx1_exit(void)
403{
404 pci_unregister_driver(&tifm_7xx1_driver);
405}
406
407MODULE_AUTHOR("Alex Dubov");
408MODULE_DESCRIPTION("TI FlashMedia host driver");
409MODULE_LICENSE("GPL");
410MODULE_DEVICE_TABLE(pci, tifm_7xx1_pci_tbl);
411MODULE_VERSION(DRIVER_VERSION);
412
413module_init(tifm_7xx1_init);
414module_exit(tifm_7xx1_exit);