blob: 1c39d3438f20c3a8cd8a6a7ca386e0c862e2cc5c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Driver for the Cirrus PD6729 PCI-PCMCIA bridge.
3 *
4 * Based on the i82092.c driver.
5 *
6 * This software may be used and distributed according to the terms of
7 * the GNU General Public License, incorporated herein by reference.
8 */
9
10#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/module.h>
12#include <linux/pci.h>
13#include <linux/init.h>
14#include <linux/workqueue.h>
15#include <linux/interrupt.h>
16#include <linux/device.h>
17
18#include <pcmcia/cs_types.h>
19#include <pcmcia/ss.h>
20#include <pcmcia/cs.h>
21
22#include <asm/system.h>
23#include <asm/io.h>
24
25#include "pd6729.h"
26#include "i82365.h"
27#include "cirrus.h"
28
29MODULE_LICENSE("GPL");
30MODULE_DESCRIPTION("Driver for the Cirrus PD6729 PCI-PCMCIA bridge");
31MODULE_AUTHOR("Jun Komuro <komurojun-mbn@nifty.com>");
32
33#define MAX_SOCKETS 2
34
35/*
36 * simple helper functions
37 * External clock time, in nanoseconds. 120 ns = 8.33 MHz
38 */
39#define to_cycles(ns) ((ns)/120)
40
41#ifndef NO_IRQ
42#define NO_IRQ ((unsigned int)(0))
43#endif
44
45/*
46 * PARAMETERS
47 * irq_mode=n
48 * Specifies the interrupt delivery mode. The default (1) is to use PCI
49 * interrupts; a value of 0 selects ISA interrupts. This must be set for
50 * correct operation of PCI card readers.
51 *
52 * irq_list=i,j,...
53 * This list limits the set of interrupts that can be used by PCMCIA
54 * cards.
55 * The default list is 3,4,5,7,9,10,11.
56 * (irq_list parameter is not used, if irq_mode = 1)
57 */
58
59static int irq_mode = 1; /* 0 = ISA interrupt, 1 = PCI interrupt */
60static int irq_list[16];
Al Viro64a6f952007-10-14 19:35:30 +010061static unsigned int irq_list_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63module_param(irq_mode, int, 0444);
64module_param_array(irq_list, int, &irq_list_count, 0444);
65MODULE_PARM_DESC(irq_mode,
66 "interrupt delivery mode. 0 = ISA, 1 = PCI. default is 1");
67MODULE_PARM_DESC(irq_list, "interrupts that can be used by PCMCIA cards");
68
69static DEFINE_SPINLOCK(port_lock);
70
71/* basic value read/write functions */
72
73static unsigned char indirect_read(struct pd6729_socket *socket,
74 unsigned short reg)
75{
76 unsigned long port;
77 unsigned char val;
78 unsigned long flags;
79
80 spin_lock_irqsave(&port_lock, flags);
81 reg += socket->number * 0x40;
82 port = socket->io_base;
83 outb(reg, port);
84 val = inb(port + 1);
85 spin_unlock_irqrestore(&port_lock, flags);
86
87 return val;
88}
89
90static unsigned short indirect_read16(struct pd6729_socket *socket,
91 unsigned short reg)
92{
93 unsigned long port;
94 unsigned short tmp;
95 unsigned long flags;
96
97 spin_lock_irqsave(&port_lock, flags);
98 reg = reg + socket->number * 0x40;
99 port = socket->io_base;
100 outb(reg, port);
101 tmp = inb(port + 1);
102 reg++;
103 outb(reg, port);
104 tmp = tmp | (inb(port + 1) << 8);
105 spin_unlock_irqrestore(&port_lock, flags);
106
107 return tmp;
108}
109
110static void indirect_write(struct pd6729_socket *socket, unsigned short reg,
111 unsigned char value)
112{
113 unsigned long port;
114 unsigned long flags;
115
116 spin_lock_irqsave(&port_lock, flags);
117 reg = reg + socket->number * 0x40;
118 port = socket->io_base;
119 outb(reg, port);
120 outb(value, port + 1);
121 spin_unlock_irqrestore(&port_lock, flags);
122}
123
124static void indirect_setbit(struct pd6729_socket *socket, unsigned short reg,
125 unsigned char mask)
126{
127 unsigned long port;
128 unsigned char val;
129 unsigned long flags;
130
131 spin_lock_irqsave(&port_lock, flags);
132 reg = reg + socket->number * 0x40;
133 port = socket->io_base;
134 outb(reg, port);
135 val = inb(port + 1);
136 val |= mask;
137 outb(reg, port);
138 outb(val, port + 1);
139 spin_unlock_irqrestore(&port_lock, flags);
140}
141
142static void indirect_resetbit(struct pd6729_socket *socket, unsigned short reg,
143 unsigned char mask)
144{
145 unsigned long port;
146 unsigned char val;
147 unsigned long flags;
148
149 spin_lock_irqsave(&port_lock, flags);
150 reg = reg + socket->number * 0x40;
151 port = socket->io_base;
152 outb(reg, port);
153 val = inb(port + 1);
154 val &= ~mask;
155 outb(reg, port);
156 outb(val, port + 1);
157 spin_unlock_irqrestore(&port_lock, flags);
158}
159
160static void indirect_write16(struct pd6729_socket *socket, unsigned short reg,
161 unsigned short value)
162{
163 unsigned long port;
164 unsigned char val;
165 unsigned long flags;
166
167 spin_lock_irqsave(&port_lock, flags);
168 reg = reg + socket->number * 0x40;
169 port = socket->io_base;
170
171 outb(reg, port);
172 val = value & 255;
173 outb(val, port + 1);
174
175 reg++;
176
177 outb(reg, port);
178 val = value >> 8;
179 outb(val, port + 1);
180 spin_unlock_irqrestore(&port_lock, flags);
181}
182
183/* Interrupt handler functionality */
184
David Howells7d12e782006-10-05 14:55:46 +0100185static irqreturn_t pd6729_interrupt(int irq, void *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186{
187 struct pd6729_socket *socket = (struct pd6729_socket *)dev;
188 int i;
189 int loopcount = 0;
190 int handled = 0;
191 unsigned int events, active = 0;
192
193 while (1) {
194 loopcount++;
195 if (loopcount > 20) {
196 printk(KERN_ERR "pd6729: infinite eventloop "
197 "in interrupt\n");
198 break;
199 }
200
201 active = 0;
202
203 for (i = 0; i < MAX_SOCKETS; i++) {
204 unsigned int csc;
205
206 /* card status change register */
207 csc = indirect_read(&socket[i], I365_CSC);
208 if (csc == 0) /* no events on this socket */
209 continue;
210
211 handled = 1;
212 events = 0;
213
214 if (csc & I365_CSC_DETECT) {
215 events |= SS_DETECT;
216 dprintk("Card detected in socket %i!\n", i);
217 }
218
219 if (indirect_read(&socket[i], I365_INTCTL)
220 & I365_PC_IOCARD) {
221 /* For IO/CARDS, bit 0 means "read the card" */
222 events |= (csc & I365_CSC_STSCHG)
223 ? SS_STSCHG : 0;
224 } else {
225 /* Check for battery/ready events */
226 events |= (csc & I365_CSC_BVD1)
227 ? SS_BATDEAD : 0;
228 events |= (csc & I365_CSC_BVD2)
229 ? SS_BATWARN : 0;
230 events |= (csc & I365_CSC_READY)
231 ? SS_READY : 0;
232 }
233
234 if (events) {
235 pcmcia_parse_events(&socket[i].socket, events);
236 }
237 active |= events;
238 }
239
240 if (active == 0) /* no more events to handle */
241 break;
242 }
243 return IRQ_RETVAL(handled);
244}
245
246/* socket functions */
247
248static void pd6729_interrupt_wrapper(unsigned long data)
249{
250 struct pd6729_socket *socket = (struct pd6729_socket *) data;
251
David Howells7d12e782006-10-05 14:55:46 +0100252 pd6729_interrupt(0, (void *)socket);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 mod_timer(&socket->poll_timer, jiffies + HZ);
254}
255
256static int pd6729_get_status(struct pcmcia_socket *sock, u_int *value)
257{
258 struct pd6729_socket *socket
259 = container_of(sock, struct pd6729_socket, socket);
260 unsigned int status;
261 unsigned int data;
262 struct pd6729_socket *t;
263
264 /* Interface Status Register */
265 status = indirect_read(socket, I365_STATUS);
266 *value = 0;
267
268 if ((status & I365_CS_DETECT) == I365_CS_DETECT) {
269 *value |= SS_DETECT;
270 }
271
272 /*
273 * IO cards have a different meaning of bits 0,1
274 * Also notice the inverse-logic on the bits
275 */
276 if (indirect_read(socket, I365_INTCTL) & I365_PC_IOCARD) {
277 /* IO card */
278 if (!(status & I365_CS_STSCHG))
279 *value |= SS_STSCHG;
280 } else {
281 /* non I/O card */
282 if (!(status & I365_CS_BVD1))
283 *value |= SS_BATDEAD;
284 if (!(status & I365_CS_BVD2))
285 *value |= SS_BATWARN;
286 }
287
288 if (status & I365_CS_WRPROT)
289 *value |= SS_WRPROT; /* card is write protected */
290
291 if (status & I365_CS_READY)
292 *value |= SS_READY; /* card is not busy */
293
294 if (status & I365_CS_POWERON)
295 *value |= SS_POWERON; /* power is applied to the card */
296
297 t = (socket->number) ? socket : socket + 1;
298 indirect_write(t, PD67_EXT_INDEX, PD67_EXTERN_DATA);
299 data = indirect_read16(t, PD67_EXT_DATA);
300 *value |= (data & PD67_EXD_VS1(socket->number)) ? 0 : SS_3VCARD;
301
302 return 0;
303}
304
305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306static int pd6729_set_socket(struct pcmcia_socket *sock, socket_state_t *state)
307{
308 struct pd6729_socket *socket
309 = container_of(sock, struct pd6729_socket, socket);
310 unsigned char reg, data;
311
312 /* First, set the global controller options */
313 indirect_write(socket, I365_GBLCTL, 0x00);
314 indirect_write(socket, I365_GENCTL, 0x00);
315
316 /* Values for the IGENC register */
317 socket->card_irq = state->io_irq;
318
319 reg = 0;
320 /* The reset bit has "inverse" logic */
321 if (!(state->flags & SS_RESET))
322 reg |= I365_PC_RESET;
323 if (state->flags & SS_IOCARD)
324 reg |= I365_PC_IOCARD;
325
326 /* IGENC, Interrupt and General Control Register */
327 indirect_write(socket, I365_INTCTL, reg);
328
329 /* Power registers */
330
331 reg = I365_PWR_NORESET; /* default: disable resetdrv on resume */
332
333 if (state->flags & SS_PWR_AUTO) {
334 dprintk("Auto power\n");
335 reg |= I365_PWR_AUTO; /* automatic power mngmnt */
336 }
337 if (state->flags & SS_OUTPUT_ENA) {
338 dprintk("Power Enabled\n");
339 reg |= I365_PWR_OUT; /* enable power */
340 }
341
342 switch (state->Vcc) {
343 case 0:
344 break;
345 case 33:
346 dprintk("setting voltage to Vcc to 3.3V on socket %i\n",
347 socket->number);
348 reg |= I365_VCC_5V;
349 indirect_setbit(socket, PD67_MISC_CTL_1, PD67_MC1_VCC_3V);
350 break;
351 case 50:
352 dprintk("setting voltage to Vcc to 5V on socket %i\n",
353 socket->number);
354 reg |= I365_VCC_5V;
355 indirect_resetbit(socket, PD67_MISC_CTL_1, PD67_MC1_VCC_3V);
356 break;
357 default:
358 dprintk("pd6729: pd6729_set_socket called with "
359 "invalid VCC power value: %i\n",
360 state->Vcc);
361 return -EINVAL;
362 }
363
364 switch (state->Vpp) {
365 case 0:
366 dprintk("not setting Vpp on socket %i\n", socket->number);
367 break;
368 case 33:
369 case 50:
370 dprintk("setting Vpp to Vcc for socket %i\n", socket->number);
371 reg |= I365_VPP1_5V;
372 break;
373 case 120:
374 dprintk("setting Vpp to 12.0\n");
375 reg |= I365_VPP1_12V;
376 break;
377 default:
378 dprintk("pd6729: pd6729_set_socket called with invalid VPP power value: %i\n",
379 state->Vpp);
380 return -EINVAL;
381 }
382
383 /* only write if changed */
384 if (reg != indirect_read(socket, I365_POWER))
385 indirect_write(socket, I365_POWER, reg);
386
387 if (irq_mode == 1) {
388 /* all interrupts are to be done as PCI interrupts */
389 data = PD67_EC1_INV_MGMT_IRQ | PD67_EC1_INV_CARD_IRQ;
390 } else
391 data = 0;
392
393 indirect_write(socket, PD67_EXT_INDEX, PD67_EXT_CTL_1);
394 indirect_write(socket, PD67_EXT_DATA, data);
395
396 /* Enable specific interrupt events */
397
398 reg = 0x00;
399 if (state->csc_mask & SS_DETECT) {
400 reg |= I365_CSC_DETECT;
401 }
402 if (state->flags & SS_IOCARD) {
403 if (state->csc_mask & SS_STSCHG)
404 reg |= I365_CSC_STSCHG;
405 } else {
406 if (state->csc_mask & SS_BATDEAD)
407 reg |= I365_CSC_BVD1;
408 if (state->csc_mask & SS_BATWARN)
409 reg |= I365_CSC_BVD2;
410 if (state->csc_mask & SS_READY)
411 reg |= I365_CSC_READY;
412 }
413 if (irq_mode == 1)
414 reg |= 0x30; /* management IRQ: PCI INTA# = "irq 3" */
415 indirect_write(socket, I365_CSCINT, reg);
416
417 reg = indirect_read(socket, I365_INTCTL);
418 if (irq_mode == 1)
419 reg |= 0x03; /* card IRQ: PCI INTA# = "irq 3" */
420 else
421 reg |= socket->card_irq;
422 indirect_write(socket, I365_INTCTL, reg);
423
424 /* now clear the (probably bogus) pending stuff by doing a dummy read */
425 (void)indirect_read(socket, I365_CSC);
426
427 return 0;
428}
429
430static int pd6729_set_io_map(struct pcmcia_socket *sock,
431 struct pccard_io_map *io)
432{
433 struct pd6729_socket *socket
434 = container_of(sock, struct pd6729_socket, socket);
435 unsigned char map, ioctl;
436
437 map = io->map;
438
439 /* Check error conditions */
440 if (map > 1) {
441 dprintk("pd6729_set_io_map with invalid map");
442 return -EINVAL;
443 }
444
445 /* Turn off the window before changing anything */
446 if (indirect_read(socket, I365_ADDRWIN) & I365_ENA_IO(map))
447 indirect_resetbit(socket, I365_ADDRWIN, I365_ENA_IO(map));
448
449 /* dprintk("set_io_map: Setting range to %x - %x\n",
450 io->start, io->stop);*/
451
452 /* write the new values */
453 indirect_write16(socket, I365_IO(map)+I365_W_START, io->start);
454 indirect_write16(socket, I365_IO(map)+I365_W_STOP, io->stop);
455
456 ioctl = indirect_read(socket, I365_IOCTL) & ~I365_IOCTL_MASK(map);
457
458 if (io->flags & MAP_0WS) ioctl |= I365_IOCTL_0WS(map);
459 if (io->flags & MAP_16BIT) ioctl |= I365_IOCTL_16BIT(map);
460 if (io->flags & MAP_AUTOSZ) ioctl |= I365_IOCTL_IOCS16(map);
461
462 indirect_write(socket, I365_IOCTL, ioctl);
463
464 /* Turn the window back on if needed */
465 if (io->flags & MAP_ACTIVE)
466 indirect_setbit(socket, I365_ADDRWIN, I365_ENA_IO(map));
467
468 return 0;
469}
470
471static int pd6729_set_mem_map(struct pcmcia_socket *sock,
472 struct pccard_mem_map *mem)
473{
474 struct pd6729_socket *socket
475 = container_of(sock, struct pd6729_socket, socket);
476 unsigned short base, i;
477 unsigned char map;
478
479 map = mem->map;
480 if (map > 4) {
481 printk("pd6729_set_mem_map: invalid map");
482 return -EINVAL;
483 }
484
485 if ((mem->res->start > mem->res->end) || (mem->speed > 1000)) {
486 printk("pd6729_set_mem_map: invalid address / speed");
487 return -EINVAL;
488 }
489
490 /* Turn off the window before changing anything */
491 if (indirect_read(socket, I365_ADDRWIN) & I365_ENA_MEM(map))
492 indirect_resetbit(socket, I365_ADDRWIN, I365_ENA_MEM(map));
493
494 /* write the start address */
495 base = I365_MEM(map);
496 i = (mem->res->start >> 12) & 0x0fff;
497 if (mem->flags & MAP_16BIT)
498 i |= I365_MEM_16BIT;
499 if (mem->flags & MAP_0WS)
500 i |= I365_MEM_0WS;
501 indirect_write16(socket, base + I365_W_START, i);
502
503 /* write the stop address */
504
505 i= (mem->res->end >> 12) & 0x0fff;
506 switch (to_cycles(mem->speed)) {
507 case 0:
508 break;
509 case 1:
510 i |= I365_MEM_WS0;
511 break;
512 case 2:
513 i |= I365_MEM_WS1;
514 break;
515 default:
516 i |= I365_MEM_WS1 | I365_MEM_WS0;
517 break;
518 }
519
520 indirect_write16(socket, base + I365_W_STOP, i);
521
522 /* Take care of high byte */
523 indirect_write(socket, PD67_EXT_INDEX, PD67_MEM_PAGE(map));
524 indirect_write(socket, PD67_EXT_DATA, mem->res->start >> 24);
525
526 /* card start */
527
528 i = ((mem->card_start - mem->res->start) >> 12) & 0x3fff;
529 if (mem->flags & MAP_WRPROT)
530 i |= I365_MEM_WRPROT;
531 if (mem->flags & MAP_ATTRIB) {
532 /* dprintk("requesting attribute memory for socket %i\n",
533 socket->number);*/
534 i |= I365_MEM_REG;
535 } else {
536 /* dprintk("requesting normal memory for socket %i\n",
537 socket->number);*/
538 }
539 indirect_write16(socket, base + I365_W_OFF, i);
540
541 /* Enable the window if necessary */
542 if (mem->flags & MAP_ACTIVE)
543 indirect_setbit(socket, I365_ADDRWIN, I365_ENA_MEM(map));
544
545 return 0;
546}
547
548static int pd6729_init(struct pcmcia_socket *sock)
549{
550 int i;
551 struct resource res = { .end = 0x0fff };
552 pccard_io_map io = { 0, 0, 0, 0, 1 };
553 pccard_mem_map mem = { .res = &res, };
554
555 pd6729_set_socket(sock, &dead_socket);
556 for (i = 0; i < 2; i++) {
557 io.map = i;
558 pd6729_set_io_map(sock, &io);
559 }
560 for (i = 0; i < 5; i++) {
561 mem.map = i;
562 pd6729_set_mem_map(sock, &mem);
563 }
564
565 return 0;
566}
567
568
569/* the pccard structure and its functions */
570static struct pccard_operations pd6729_operations = {
571 .init = pd6729_init,
572 .get_status = pd6729_get_status,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 .set_socket = pd6729_set_socket,
574 .set_io_map = pd6729_set_io_map,
575 .set_mem_map = pd6729_set_mem_map,
576};
577
David Howells7d12e782006-10-05 14:55:46 +0100578static irqreturn_t pd6729_test(int irq, void *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579{
580 dprintk("-> hit on irq %d\n", irq);
581 return IRQ_HANDLED;
582}
583
Komuro3e022d02006-11-04 10:00:47 +0900584static int pd6729_check_irq(int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585{
Komuro3e022d02006-11-04 10:00:47 +0900586 if (request_irq(irq, pd6729_test, IRQF_PROBE_SHARED, "x", pd6729_test)
587 != 0) return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 free_irq(irq, pd6729_test);
589 return 0;
590}
591
Andrew Morton9781b8b2006-05-20 15:00:32 -0700592static u_int __devinit pd6729_isa_scan(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593{
594 u_int mask0, mask = 0;
595 int i;
596
597 if (irq_mode == 1) {
598 printk(KERN_INFO "pd6729: PCI card interrupts, "
599 "PCI status changes\n");
600 return 0;
601 }
602
603 if (irq_list_count == 0)
604 mask0 = 0xffff;
605 else
606 for (i = mask0 = 0; i < irq_list_count; i++)
607 mask0 |= (1<<irq_list[i]);
608
609 mask0 &= PD67_MASK;
610
611 /* just find interrupts that aren't in use */
612 for (i = 0; i < 16; i++)
Komuro3e022d02006-11-04 10:00:47 +0900613 if ((mask0 & (1 << i)) && (pd6729_check_irq(i) == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 mask |= (1 << i);
615
616 printk(KERN_INFO "pd6729: ISA irqs = ");
617 for (i = 0; i < 16; i++)
618 if (mask & (1<<i))
619 printk("%s%d", ((mask & ((1<<i)-1)) ? "," : ""), i);
620
621 if (mask == 0) printk("none!");
622
623 printk(" polling status changes.\n");
624
625 return mask;
626}
627
628static int __devinit pd6729_pci_probe(struct pci_dev *dev,
629 const struct pci_device_id *id)
630{
631 int i, j, ret;
632 u_int mask;
633 char configbyte;
634 struct pd6729_socket *socket;
635
Dominik Brodowski8084b372005-12-11 21:18:26 +0100636 socket = kzalloc(sizeof(struct pd6729_socket) * MAX_SOCKETS,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 GFP_KERNEL);
638 if (!socket)
639 return -ENOMEM;
640
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 if ((ret = pci_enable_device(dev)))
642 goto err_out_free_mem;
643
644 printk(KERN_INFO "pd6729: Cirrus PD6729 PCI to PCMCIA Bridge "
Greg Kroah-Hartman490ab722006-06-12 15:17:34 -0700645 "at 0x%llx on irq %d\n",
646 (unsigned long long)pci_resource_start(dev, 0), dev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 /*
648 * Since we have no memory BARs some firmware may not
649 * have had PCI_COMMAND_MEMORY enabled, yet the device needs it.
650 */
651 pci_read_config_byte(dev, PCI_COMMAND, &configbyte);
652 if (!(configbyte & PCI_COMMAND_MEMORY)) {
653 printk(KERN_DEBUG "pd6729: Enabling PCI_COMMAND_MEMORY.\n");
654 configbyte |= PCI_COMMAND_MEMORY;
655 pci_write_config_byte(dev, PCI_COMMAND, configbyte);
656 }
657
658 ret = pci_request_regions(dev, "pd6729");
659 if (ret) {
660 printk(KERN_INFO "pd6729: pci request region failed.\n");
661 goto err_out_disable;
662 }
663
664 if (dev->irq == NO_IRQ)
665 irq_mode = 0; /* fall back to ISA interrupt mode */
666
667 mask = pd6729_isa_scan();
668 if (irq_mode == 0 && mask == 0) {
669 printk(KERN_INFO "pd6729: no ISA interrupt is available.\n");
670 goto err_out_free_res;
671 }
672
673 for (i = 0; i < MAX_SOCKETS; i++) {
674 socket[i].io_base = pci_resource_start(dev, 0);
Dominik Brodowskic35e66a2005-05-05 16:15:56 -0700675 socket[i].socket.features |= SS_CAP_PAGE_REGS | SS_CAP_PCCARD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 socket[i].socket.map_size = 0x1000;
677 socket[i].socket.irq_mask = mask;
678 socket[i].socket.pci_irq = dev->irq;
679 socket[i].socket.owner = THIS_MODULE;
680
681 socket[i].number = i;
682
683 socket[i].socket.ops = &pd6729_operations;
684 socket[i].socket.resource_ops = &pccard_nonstatic_ops;
Greg Kroah-Hartman87373312006-09-12 17:00:10 +0200685 socket[i].socket.dev.parent = &dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 socket[i].socket.driver_data = &socket[i];
687 }
688
689 pci_set_drvdata(dev, socket);
690 if (irq_mode == 1) {
691 /* Register the interrupt handler */
Thomas Gleixnerdace1452006-07-01 19:29:38 -0700692 if ((ret = request_irq(dev->irq, pd6729_interrupt, IRQF_SHARED,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 "pd6729", socket))) {
694 printk(KERN_ERR "pd6729: Failed to register irq %d, "
695 "aborting\n", dev->irq);
696 goto err_out_free_res;
697 }
698 } else {
699 /* poll Card status change */
700 init_timer(&socket->poll_timer);
701 socket->poll_timer.function = pd6729_interrupt_wrapper;
702 socket->poll_timer.data = (unsigned long)socket;
703 socket->poll_timer.expires = jiffies + HZ;
704 add_timer(&socket->poll_timer);
705 }
706
707 for (i = 0; i < MAX_SOCKETS; i++) {
708 ret = pcmcia_register_socket(&socket[i].socket);
709 if (ret) {
710 printk(KERN_INFO "pd6729: pcmcia_register_socket "
711 "failed.\n");
712 for (j = 0; j < i ; j++)
713 pcmcia_unregister_socket(&socket[j].socket);
714 goto err_out_free_res2;
715 }
716 }
717
718 return 0;
719
720 err_out_free_res2:
721 if (irq_mode == 1)
722 free_irq(dev->irq, socket);
723 else
724 del_timer_sync(&socket->poll_timer);
725 err_out_free_res:
726 pci_release_regions(dev);
727 err_out_disable:
728 pci_disable_device(dev);
729
730 err_out_free_mem:
731 kfree(socket);
732 return ret;
733}
734
735static void __devexit pd6729_pci_remove(struct pci_dev *dev)
736{
737 int i;
738 struct pd6729_socket *socket = pci_get_drvdata(dev);
739
740 for (i = 0; i < MAX_SOCKETS; i++) {
741 /* Turn off all interrupt sources */
742 indirect_write(&socket[i], I365_CSCINT, 0);
743 indirect_write(&socket[i], I365_INTCTL, 0);
744
745 pcmcia_unregister_socket(&socket[i].socket);
746 }
747
748 if (irq_mode == 1)
749 free_irq(dev->irq, socket);
750 else
751 del_timer_sync(&socket->poll_timer);
752 pci_release_regions(dev);
753 pci_disable_device(dev);
754
755 kfree(socket);
756}
757
Alexey Dobriyanf237de52006-10-20 14:44:13 -0700758#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759static int pd6729_socket_suspend(struct pci_dev *dev, pm_message_t state)
760{
Rafael J. Wysocki827b4642009-09-29 00:10:41 +0200761 return pcmcia_socket_dev_suspend(&dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762}
763
764static int pd6729_socket_resume(struct pci_dev *dev)
765{
766 return pcmcia_socket_dev_resume(&dev->dev);
767}
Alexey Dobriyanf237de52006-10-20 14:44:13 -0700768#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
770static struct pci_device_id pd6729_pci_ids[] = {
771 {
772 .vendor = PCI_VENDOR_ID_CIRRUS,
773 .device = PCI_DEVICE_ID_CIRRUS_6729,
774 .subvendor = PCI_ANY_ID,
775 .subdevice = PCI_ANY_ID,
776 },
777 { }
778};
779MODULE_DEVICE_TABLE(pci, pd6729_pci_ids);
780
Sam Ravnborgba66ddf2008-05-01 04:34:51 -0700781static struct pci_driver pd6729_pci_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 .name = "pd6729",
783 .id_table = pd6729_pci_ids,
784 .probe = pd6729_pci_probe,
785 .remove = __devexit_p(pd6729_pci_remove),
Alexey Dobriyanf237de52006-10-20 14:44:13 -0700786#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 .suspend = pd6729_socket_suspend,
788 .resume = pd6729_socket_resume,
Alexey Dobriyanf237de52006-10-20 14:44:13 -0700789#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790};
791
792static int pd6729_module_init(void)
793{
Sam Ravnborgba66ddf2008-05-01 04:34:51 -0700794 return pci_register_driver(&pd6729_pci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795}
796
797static void pd6729_module_exit(void)
798{
Sam Ravnborgba66ddf2008-05-01 04:34:51 -0700799 pci_unregister_driver(&pd6729_pci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800}
801
802module_init(pd6729_module_init);
803module_exit(pd6729_module_exit);