blob: 96c72e90b79c4c1ff26bcc0e754dbb1c34350017 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090012#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/pci.h>
14#include <linux/init.h>
15#include <linux/workqueue.h>
16#include <linux/interrupt.h>
17#include <linux/device.h>
Komuro5cbb2b92010-03-20 06:39:19 +090018#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <pcmcia/ss.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
22#include <asm/system.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24#include "pd6729.h"
25#include "i82365.h"
26#include "cirrus.h"
27
28MODULE_LICENSE("GPL");
29MODULE_DESCRIPTION("Driver for the Cirrus PD6729 PCI-PCMCIA bridge");
30MODULE_AUTHOR("Jun Komuro <komurojun-mbn@nifty.com>");
31
32#define MAX_SOCKETS 2
33
34/*
35 * simple helper functions
36 * External clock time, in nanoseconds. 120 ns = 8.33 MHz
37 */
38#define to_cycles(ns) ((ns)/120)
39
40#ifndef NO_IRQ
41#define NO_IRQ ((unsigned int)(0))
42#endif
43
44/*
45 * PARAMETERS
46 * irq_mode=n
47 * Specifies the interrupt delivery mode. The default (1) is to use PCI
48 * interrupts; a value of 0 selects ISA interrupts. This must be set for
49 * correct operation of PCI card readers.
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 */
51
52static int irq_mode = 1; /* 0 = ISA interrupt, 1 = PCI interrupt */
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54module_param(irq_mode, int, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055MODULE_PARM_DESC(irq_mode,
56 "interrupt delivery mode. 0 = ISA, 1 = PCI. default is 1");
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58static DEFINE_SPINLOCK(port_lock);
59
60/* basic value read/write functions */
61
62static unsigned char indirect_read(struct pd6729_socket *socket,
63 unsigned short reg)
64{
65 unsigned long port;
66 unsigned char val;
67 unsigned long flags;
68
69 spin_lock_irqsave(&port_lock, flags);
70 reg += socket->number * 0x40;
71 port = socket->io_base;
72 outb(reg, port);
73 val = inb(port + 1);
74 spin_unlock_irqrestore(&port_lock, flags);
75
76 return val;
77}
78
79static unsigned short indirect_read16(struct pd6729_socket *socket,
80 unsigned short reg)
81{
82 unsigned long port;
83 unsigned short tmp;
84 unsigned long flags;
85
86 spin_lock_irqsave(&port_lock, flags);
87 reg = reg + socket->number * 0x40;
88 port = socket->io_base;
89 outb(reg, port);
90 tmp = inb(port + 1);
91 reg++;
92 outb(reg, port);
93 tmp = tmp | (inb(port + 1) << 8);
94 spin_unlock_irqrestore(&port_lock, flags);
95
96 return tmp;
97}
98
99static void indirect_write(struct pd6729_socket *socket, unsigned short reg,
100 unsigned char value)
101{
102 unsigned long port;
103 unsigned long flags;
104
105 spin_lock_irqsave(&port_lock, flags);
106 reg = reg + socket->number * 0x40;
107 port = socket->io_base;
108 outb(reg, port);
109 outb(value, port + 1);
110 spin_unlock_irqrestore(&port_lock, flags);
111}
112
113static void indirect_setbit(struct pd6729_socket *socket, unsigned short reg,
114 unsigned char mask)
115{
116 unsigned long port;
117 unsigned char val;
118 unsigned long flags;
119
120 spin_lock_irqsave(&port_lock, flags);
121 reg = reg + socket->number * 0x40;
122 port = socket->io_base;
123 outb(reg, port);
124 val = inb(port + 1);
125 val |= mask;
126 outb(reg, port);
127 outb(val, port + 1);
128 spin_unlock_irqrestore(&port_lock, flags);
129}
130
131static void indirect_resetbit(struct pd6729_socket *socket, unsigned short reg,
132 unsigned char mask)
133{
134 unsigned long port;
135 unsigned char val;
136 unsigned long flags;
137
138 spin_lock_irqsave(&port_lock, flags);
139 reg = reg + socket->number * 0x40;
140 port = socket->io_base;
141 outb(reg, port);
142 val = inb(port + 1);
143 val &= ~mask;
144 outb(reg, port);
145 outb(val, port + 1);
146 spin_unlock_irqrestore(&port_lock, flags);
147}
148
149static void indirect_write16(struct pd6729_socket *socket, unsigned short reg,
150 unsigned short value)
151{
152 unsigned long port;
153 unsigned char val;
154 unsigned long flags;
155
156 spin_lock_irqsave(&port_lock, flags);
157 reg = reg + socket->number * 0x40;
158 port = socket->io_base;
159
160 outb(reg, port);
161 val = value & 255;
162 outb(val, port + 1);
163
164 reg++;
165
166 outb(reg, port);
167 val = value >> 8;
168 outb(val, port + 1);
169 spin_unlock_irqrestore(&port_lock, flags);
170}
171
172/* Interrupt handler functionality */
173
David Howells7d12e782006-10-05 14:55:46 +0100174static irqreturn_t pd6729_interrupt(int irq, void *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175{
176 struct pd6729_socket *socket = (struct pd6729_socket *)dev;
177 int i;
178 int loopcount = 0;
179 int handled = 0;
180 unsigned int events, active = 0;
181
182 while (1) {
183 loopcount++;
184 if (loopcount > 20) {
185 printk(KERN_ERR "pd6729: infinite eventloop "
186 "in interrupt\n");
187 break;
188 }
189
190 active = 0;
191
192 for (i = 0; i < MAX_SOCKETS; i++) {
193 unsigned int csc;
194
195 /* card status change register */
196 csc = indirect_read(&socket[i], I365_CSC);
197 if (csc == 0) /* no events on this socket */
198 continue;
199
200 handled = 1;
201 events = 0;
202
203 if (csc & I365_CSC_DETECT) {
204 events |= SS_DETECT;
Dominik Brodowskia7149f92009-10-24 18:07:16 +0200205 dev_vdbg(&socket[i].socket.dev,
206 "Card detected in socket %i!\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 }
208
209 if (indirect_read(&socket[i], I365_INTCTL)
210 & I365_PC_IOCARD) {
211 /* For IO/CARDS, bit 0 means "read the card" */
212 events |= (csc & I365_CSC_STSCHG)
213 ? SS_STSCHG : 0;
214 } else {
215 /* Check for battery/ready events */
216 events |= (csc & I365_CSC_BVD1)
217 ? SS_BATDEAD : 0;
218 events |= (csc & I365_CSC_BVD2)
219 ? SS_BATWARN : 0;
220 events |= (csc & I365_CSC_READY)
221 ? SS_READY : 0;
222 }
223
Komuro5cbb2b92010-03-20 06:39:19 +0900224 if (events)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 pcmcia_parse_events(&socket[i].socket, events);
Komuro5cbb2b92010-03-20 06:39:19 +0900226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 active |= events;
228 }
229
230 if (active == 0) /* no more events to handle */
231 break;
232 }
233 return IRQ_RETVAL(handled);
234}
235
236/* socket functions */
237
238static void pd6729_interrupt_wrapper(unsigned long data)
239{
240 struct pd6729_socket *socket = (struct pd6729_socket *) data;
241
David Howells7d12e782006-10-05 14:55:46 +0100242 pd6729_interrupt(0, (void *)socket);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 mod_timer(&socket->poll_timer, jiffies + HZ);
244}
245
246static int pd6729_get_status(struct pcmcia_socket *sock, u_int *value)
247{
248 struct pd6729_socket *socket
249 = container_of(sock, struct pd6729_socket, socket);
250 unsigned int status;
251 unsigned int data;
252 struct pd6729_socket *t;
253
254 /* Interface Status Register */
255 status = indirect_read(socket, I365_STATUS);
256 *value = 0;
257
Komuro5cbb2b92010-03-20 06:39:19 +0900258 if ((status & I365_CS_DETECT) == I365_CS_DETECT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 *value |= SS_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
261 /*
262 * IO cards have a different meaning of bits 0,1
263 * Also notice the inverse-logic on the bits
264 */
265 if (indirect_read(socket, I365_INTCTL) & I365_PC_IOCARD) {
266 /* IO card */
267 if (!(status & I365_CS_STSCHG))
268 *value |= SS_STSCHG;
269 } else {
270 /* non I/O card */
271 if (!(status & I365_CS_BVD1))
272 *value |= SS_BATDEAD;
273 if (!(status & I365_CS_BVD2))
274 *value |= SS_BATWARN;
275 }
276
277 if (status & I365_CS_WRPROT)
278 *value |= SS_WRPROT; /* card is write protected */
279
280 if (status & I365_CS_READY)
281 *value |= SS_READY; /* card is not busy */
282
283 if (status & I365_CS_POWERON)
284 *value |= SS_POWERON; /* power is applied to the card */
285
286 t = (socket->number) ? socket : socket + 1;
287 indirect_write(t, PD67_EXT_INDEX, PD67_EXTERN_DATA);
288 data = indirect_read16(t, PD67_EXT_DATA);
289 *value |= (data & PD67_EXD_VS1(socket->number)) ? 0 : SS_3VCARD;
290
291 return 0;
292}
293
294
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295static int pd6729_set_socket(struct pcmcia_socket *sock, socket_state_t *state)
296{
297 struct pd6729_socket *socket
298 = container_of(sock, struct pd6729_socket, socket);
299 unsigned char reg, data;
300
301 /* First, set the global controller options */
302 indirect_write(socket, I365_GBLCTL, 0x00);
303 indirect_write(socket, I365_GENCTL, 0x00);
304
305 /* Values for the IGENC register */
306 socket->card_irq = state->io_irq;
307
308 reg = 0;
Komuro5cbb2b92010-03-20 06:39:19 +0900309 /* The reset bit has "inverse" logic */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 if (!(state->flags & SS_RESET))
311 reg |= I365_PC_RESET;
312 if (state->flags & SS_IOCARD)
313 reg |= I365_PC_IOCARD;
314
315 /* IGENC, Interrupt and General Control Register */
316 indirect_write(socket, I365_INTCTL, reg);
317
318 /* Power registers */
319
320 reg = I365_PWR_NORESET; /* default: disable resetdrv on resume */
321
322 if (state->flags & SS_PWR_AUTO) {
Dominik Brodowskia7149f92009-10-24 18:07:16 +0200323 dev_dbg(&sock->dev, "Auto power\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 reg |= I365_PWR_AUTO; /* automatic power mngmnt */
325 }
326 if (state->flags & SS_OUTPUT_ENA) {
Dominik Brodowskia7149f92009-10-24 18:07:16 +0200327 dev_dbg(&sock->dev, "Power Enabled\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 reg |= I365_PWR_OUT; /* enable power */
329 }
330
331 switch (state->Vcc) {
332 case 0:
333 break;
334 case 33:
Dominik Brodowskia7149f92009-10-24 18:07:16 +0200335 dev_dbg(&sock->dev,
336 "setting voltage to Vcc to 3.3V on socket %i\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 socket->number);
338 reg |= I365_VCC_5V;
339 indirect_setbit(socket, PD67_MISC_CTL_1, PD67_MC1_VCC_3V);
340 break;
341 case 50:
Dominik Brodowskia7149f92009-10-24 18:07:16 +0200342 dev_dbg(&sock->dev,
343 "setting voltage to Vcc to 5V on socket %i\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 socket->number);
345 reg |= I365_VCC_5V;
346 indirect_resetbit(socket, PD67_MISC_CTL_1, PD67_MC1_VCC_3V);
347 break;
348 default:
Dominik Brodowskia7149f92009-10-24 18:07:16 +0200349 dev_dbg(&sock->dev,
350 "pd6729_set_socket called with invalid VCC power "
351 "value: %i\n", state->Vcc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 return -EINVAL;
353 }
354
355 switch (state->Vpp) {
356 case 0:
Dominik Brodowskia7149f92009-10-24 18:07:16 +0200357 dev_dbg(&sock->dev, "not setting Vpp on socket %i\n",
358 socket->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 break;
360 case 33:
361 case 50:
Dominik Brodowskia7149f92009-10-24 18:07:16 +0200362 dev_dbg(&sock->dev, "setting Vpp to Vcc for socket %i\n",
363 socket->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 reg |= I365_VPP1_5V;
365 break;
366 case 120:
Dominik Brodowskia7149f92009-10-24 18:07:16 +0200367 dev_dbg(&sock->dev, "setting Vpp to 12.0\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 reg |= I365_VPP1_12V;
369 break;
370 default:
Dominik Brodowskia7149f92009-10-24 18:07:16 +0200371 dev_dbg(&sock->dev, "pd6729: pd6729_set_socket called with "
372 "invalid VPP power value: %i\n", state->Vpp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 return -EINVAL;
374 }
375
376 /* only write if changed */
377 if (reg != indirect_read(socket, I365_POWER))
378 indirect_write(socket, I365_POWER, reg);
379
380 if (irq_mode == 1) {
Komuro5cbb2b92010-03-20 06:39:19 +0900381 /* all interrupts are to be done as PCI interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 data = PD67_EC1_INV_MGMT_IRQ | PD67_EC1_INV_CARD_IRQ;
383 } else
384 data = 0;
385
386 indirect_write(socket, PD67_EXT_INDEX, PD67_EXT_CTL_1);
387 indirect_write(socket, PD67_EXT_DATA, data);
388
389 /* Enable specific interrupt events */
390
391 reg = 0x00;
Komuro5cbb2b92010-03-20 06:39:19 +0900392 if (state->csc_mask & SS_DETECT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 reg |= I365_CSC_DETECT;
Komuro5cbb2b92010-03-20 06:39:19 +0900394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 if (state->flags & SS_IOCARD) {
396 if (state->csc_mask & SS_STSCHG)
397 reg |= I365_CSC_STSCHG;
398 } else {
399 if (state->csc_mask & SS_BATDEAD)
400 reg |= I365_CSC_BVD1;
401 if (state->csc_mask & SS_BATWARN)
402 reg |= I365_CSC_BVD2;
403 if (state->csc_mask & SS_READY)
404 reg |= I365_CSC_READY;
405 }
406 if (irq_mode == 1)
407 reg |= 0x30; /* management IRQ: PCI INTA# = "irq 3" */
408 indirect_write(socket, I365_CSCINT, reg);
409
410 reg = indirect_read(socket, I365_INTCTL);
411 if (irq_mode == 1)
412 reg |= 0x03; /* card IRQ: PCI INTA# = "irq 3" */
413 else
414 reg |= socket->card_irq;
415 indirect_write(socket, I365_INTCTL, reg);
416
417 /* now clear the (probably bogus) pending stuff by doing a dummy read */
418 (void)indirect_read(socket, I365_CSC);
419
420 return 0;
421}
422
423static int pd6729_set_io_map(struct pcmcia_socket *sock,
424 struct pccard_io_map *io)
425{
426 struct pd6729_socket *socket
427 = container_of(sock, struct pd6729_socket, socket);
428 unsigned char map, ioctl;
429
430 map = io->map;
431
432 /* Check error conditions */
433 if (map > 1) {
Dominik Brodowskia7149f92009-10-24 18:07:16 +0200434 dev_dbg(&sock->dev, "pd6729_set_io_map with invalid map\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 return -EINVAL;
436 }
437
438 /* Turn off the window before changing anything */
439 if (indirect_read(socket, I365_ADDRWIN) & I365_ENA_IO(map))
440 indirect_resetbit(socket, I365_ADDRWIN, I365_ENA_IO(map));
441
Dominik Brodowskia7149f92009-10-24 18:07:16 +0200442 /* dev_dbg(&sock->dev, "set_io_map: Setting range to %x - %x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 io->start, io->stop);*/
444
445 /* write the new values */
446 indirect_write16(socket, I365_IO(map)+I365_W_START, io->start);
447 indirect_write16(socket, I365_IO(map)+I365_W_STOP, io->stop);
448
449 ioctl = indirect_read(socket, I365_IOCTL) & ~I365_IOCTL_MASK(map);
450
Komuro5cbb2b92010-03-20 06:39:19 +0900451 if (io->flags & MAP_0WS)
452 ioctl |= I365_IOCTL_0WS(map);
453 if (io->flags & MAP_16BIT)
454 ioctl |= I365_IOCTL_16BIT(map);
455 if (io->flags & MAP_AUTOSZ)
456 ioctl |= I365_IOCTL_IOCS16(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
458 indirect_write(socket, I365_IOCTL, ioctl);
459
460 /* Turn the window back on if needed */
461 if (io->flags & MAP_ACTIVE)
462 indirect_setbit(socket, I365_ADDRWIN, I365_ENA_IO(map));
463
464 return 0;
465}
466
467static int pd6729_set_mem_map(struct pcmcia_socket *sock,
468 struct pccard_mem_map *mem)
469{
470 struct pd6729_socket *socket
471 = container_of(sock, struct pd6729_socket, socket);
472 unsigned short base, i;
473 unsigned char map;
474
475 map = mem->map;
476 if (map > 4) {
Dominik Brodowskia7149f92009-10-24 18:07:16 +0200477 dev_warn(&sock->dev, "invalid map requested\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 return -EINVAL;
479 }
480
481 if ((mem->res->start > mem->res->end) || (mem->speed > 1000)) {
Dominik Brodowskia7149f92009-10-24 18:07:16 +0200482 dev_warn(&sock->dev, "invalid invalid address / speed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 return -EINVAL;
484 }
485
486 /* Turn off the window before changing anything */
487 if (indirect_read(socket, I365_ADDRWIN) & I365_ENA_MEM(map))
488 indirect_resetbit(socket, I365_ADDRWIN, I365_ENA_MEM(map));
489
490 /* write the start address */
491 base = I365_MEM(map);
492 i = (mem->res->start >> 12) & 0x0fff;
493 if (mem->flags & MAP_16BIT)
494 i |= I365_MEM_16BIT;
495 if (mem->flags & MAP_0WS)
496 i |= I365_MEM_0WS;
497 indirect_write16(socket, base + I365_W_START, i);
498
499 /* write the stop address */
500
Komuro5cbb2b92010-03-20 06:39:19 +0900501 i = (mem->res->end >> 12) & 0x0fff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 switch (to_cycles(mem->speed)) {
503 case 0:
504 break;
505 case 1:
506 i |= I365_MEM_WS0;
507 break;
508 case 2:
509 i |= I365_MEM_WS1;
510 break;
511 default:
512 i |= I365_MEM_WS1 | I365_MEM_WS0;
513 break;
514 }
515
516 indirect_write16(socket, base + I365_W_STOP, i);
517
518 /* Take care of high byte */
519 indirect_write(socket, PD67_EXT_INDEX, PD67_MEM_PAGE(map));
520 indirect_write(socket, PD67_EXT_DATA, mem->res->start >> 24);
521
522 /* card start */
523
524 i = ((mem->card_start - mem->res->start) >> 12) & 0x3fff;
525 if (mem->flags & MAP_WRPROT)
526 i |= I365_MEM_WRPROT;
527 if (mem->flags & MAP_ATTRIB) {
Dominik Brodowskia7149f92009-10-24 18:07:16 +0200528 /* dev_dbg(&sock->dev, "requesting attribute memory for "
529 "socket %i\n", socket->number);*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 i |= I365_MEM_REG;
531 } else {
Dominik Brodowskia7149f92009-10-24 18:07:16 +0200532 /* dev_dbg(&sock->dev, "requesting normal memory for "
533 "socket %i\n", socket->number);*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 }
535 indirect_write16(socket, base + I365_W_OFF, i);
536
537 /* Enable the window if necessary */
538 if (mem->flags & MAP_ACTIVE)
539 indirect_setbit(socket, I365_ADDRWIN, I365_ENA_MEM(map));
540
541 return 0;
542}
543
544static int pd6729_init(struct pcmcia_socket *sock)
545{
546 int i;
547 struct resource res = { .end = 0x0fff };
548 pccard_io_map io = { 0, 0, 0, 0, 1 };
549 pccard_mem_map mem = { .res = &res, };
550
551 pd6729_set_socket(sock, &dead_socket);
552 for (i = 0; i < 2; i++) {
553 io.map = i;
554 pd6729_set_io_map(sock, &io);
555 }
556 for (i = 0; i < 5; i++) {
557 mem.map = i;
558 pd6729_set_mem_map(sock, &mem);
559 }
560
561 return 0;
562}
563
564
565/* the pccard structure and its functions */
566static struct pccard_operations pd6729_operations = {
Komuro5cbb2b92010-03-20 06:39:19 +0900567 .init = pd6729_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 .get_status = pd6729_get_status,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 .set_socket = pd6729_set_socket,
570 .set_io_map = pd6729_set_io_map,
571 .set_mem_map = pd6729_set_mem_map,
572};
573
David Howells7d12e782006-10-05 14:55:46 +0100574static irqreturn_t pd6729_test(int irq, void *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575{
Dominik Brodowskia7149f92009-10-24 18:07:16 +0200576 pr_devel("-> hit on irq %d\n", irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 return IRQ_HANDLED;
578}
579
Komuro3e022d02006-11-04 10:00:47 +0900580static int pd6729_check_irq(int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
Komuro5cbb2b92010-03-20 06:39:19 +0900582 int ret;
583
584 ret = request_irq(irq, pd6729_test, IRQF_PROBE_SHARED, "x",
585 pd6729_test);
586 if (ret)
587 return -1;
588
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 free_irq(irq, pd6729_test);
590 return 0;
591}
592
Andrew Morton9781b8b2006-05-20 15:00:32 -0700593static u_int __devinit pd6729_isa_scan(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594{
595 u_int mask0, mask = 0;
596 int i;
597
598 if (irq_mode == 1) {
599 printk(KERN_INFO "pd6729: PCI card interrupts, "
Komuro5cbb2b92010-03-20 06:39:19 +0900600 "PCI status changes\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 return 0;
602 }
603
Komurof9097dc2010-02-28 15:39:33 +0900604 mask0 = PD67_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
606 /* just find interrupts that aren't in use */
607 for (i = 0; i < 16; i++)
Komuro3e022d02006-11-04 10:00:47 +0900608 if ((mask0 & (1 << i)) && (pd6729_check_irq(i) == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 mask |= (1 << i);
610
611 printk(KERN_INFO "pd6729: ISA irqs = ");
612 for (i = 0; i < 16; i++)
613 if (mask & (1<<i))
614 printk("%s%d", ((mask & ((1<<i)-1)) ? "," : ""), i);
615
Komuro5cbb2b92010-03-20 06:39:19 +0900616 if (mask == 0)
617 printk("none!");
618 else
619 printk(" polling status changes.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
621 return mask;
622}
623
624static int __devinit pd6729_pci_probe(struct pci_dev *dev,
625 const struct pci_device_id *id)
626{
627 int i, j, ret;
628 u_int mask;
629 char configbyte;
630 struct pd6729_socket *socket;
631
Dominik Brodowski8084b372005-12-11 21:18:26 +0100632 socket = kzalloc(sizeof(struct pd6729_socket) * MAX_SOCKETS,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 GFP_KERNEL);
Komuro5cbb2b92010-03-20 06:39:19 +0900634 if (!socket) {
635 dev_warn(&dev->dev, "failed to kzalloc socket.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 return -ENOMEM;
Komuro5cbb2b92010-03-20 06:39:19 +0900637 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Komuro5cbb2b92010-03-20 06:39:19 +0900639 ret = pci_enable_device(dev);
640 if (ret) {
641 dev_warn(&dev->dev, "failed to enable pci_device.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 goto err_out_free_mem;
Komuro5cbb2b92010-03-20 06:39:19 +0900643 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
Komuro94efb722009-10-24 08:07:39 +0900645 if (!pci_resource_start(dev, 0)) {
Dominik Brodowskia7149f92009-10-24 18:07:16 +0200646 dev_warn(&dev->dev, "refusing to load the driver as the "
647 "io_base is NULL.\n");
Rahul Ruikar40d24ff2010-09-26 17:00:29 +0530648 goto err_out_disable;
Komuro94efb722009-10-24 08:07:39 +0900649 }
650
Dominik Brodowskia7149f92009-10-24 18:07:16 +0200651 dev_info(&dev->dev, "Cirrus PD6729 PCI to PCMCIA Bridge at 0x%llx "
652 "on irq %d\n",
Greg Kroah-Hartman490ab722006-06-12 15:17:34 -0700653 (unsigned long long)pci_resource_start(dev, 0), dev->irq);
Komuro5cbb2b92010-03-20 06:39:19 +0900654 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 * Since we have no memory BARs some firmware may not
656 * have had PCI_COMMAND_MEMORY enabled, yet the device needs it.
657 */
658 pci_read_config_byte(dev, PCI_COMMAND, &configbyte);
659 if (!(configbyte & PCI_COMMAND_MEMORY)) {
Dominik Brodowskia7149f92009-10-24 18:07:16 +0200660 dev_dbg(&dev->dev, "pd6729: Enabling PCI_COMMAND_MEMORY.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 configbyte |= PCI_COMMAND_MEMORY;
662 pci_write_config_byte(dev, PCI_COMMAND, configbyte);
663 }
664
665 ret = pci_request_regions(dev, "pd6729");
666 if (ret) {
Dominik Brodowskia7149f92009-10-24 18:07:16 +0200667 dev_warn(&dev->dev, "pci request region failed.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 goto err_out_disable;
669 }
670
671 if (dev->irq == NO_IRQ)
672 irq_mode = 0; /* fall back to ISA interrupt mode */
673
674 mask = pd6729_isa_scan();
675 if (irq_mode == 0 && mask == 0) {
Dominik Brodowskia7149f92009-10-24 18:07:16 +0200676 dev_warn(&dev->dev, "no ISA interrupt is available.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 goto err_out_free_res;
678 }
679
680 for (i = 0; i < MAX_SOCKETS; i++) {
681 socket[i].io_base = pci_resource_start(dev, 0);
Dominik Brodowskic35e66a2005-05-05 16:15:56 -0700682 socket[i].socket.features |= SS_CAP_PAGE_REGS | SS_CAP_PCCARD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 socket[i].socket.map_size = 0x1000;
684 socket[i].socket.irq_mask = mask;
685 socket[i].socket.pci_irq = dev->irq;
Dominik Brodowski7a96e872010-03-13 17:42:39 +0100686 socket[i].socket.cb_dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 socket[i].socket.owner = THIS_MODULE;
688
689 socket[i].number = i;
690
691 socket[i].socket.ops = &pd6729_operations;
692 socket[i].socket.resource_ops = &pccard_nonstatic_ops;
Greg Kroah-Hartman87373312006-09-12 17:00:10 +0200693 socket[i].socket.dev.parent = &dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 socket[i].socket.driver_data = &socket[i];
695 }
696
697 pci_set_drvdata(dev, socket);
698 if (irq_mode == 1) {
699 /* Register the interrupt handler */
Komuro5cbb2b92010-03-20 06:39:19 +0900700 ret = request_irq(dev->irq, pd6729_interrupt, IRQF_SHARED,
701 "pd6729", socket);
702 if (ret) {
Dominik Brodowskia7149f92009-10-24 18:07:16 +0200703 dev_err(&dev->dev, "Failed to register irq %d\n",
704 dev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 goto err_out_free_res;
706 }
707 } else {
708 /* poll Card status change */
709 init_timer(&socket->poll_timer);
710 socket->poll_timer.function = pd6729_interrupt_wrapper;
711 socket->poll_timer.data = (unsigned long)socket;
712 socket->poll_timer.expires = jiffies + HZ;
713 add_timer(&socket->poll_timer);
714 }
715
716 for (i = 0; i < MAX_SOCKETS; i++) {
717 ret = pcmcia_register_socket(&socket[i].socket);
718 if (ret) {
Dominik Brodowskia7149f92009-10-24 18:07:16 +0200719 dev_warn(&dev->dev, "pcmcia_register_socket failed.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 for (j = 0; j < i ; j++)
721 pcmcia_unregister_socket(&socket[j].socket);
722 goto err_out_free_res2;
723 }
724 }
725
726 return 0;
727
Komuro006839f2010-10-23 07:02:05 +0900728err_out_free_res2:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 if (irq_mode == 1)
730 free_irq(dev->irq, socket);
731 else
732 del_timer_sync(&socket->poll_timer);
Komuro006839f2010-10-23 07:02:05 +0900733err_out_free_res:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 pci_release_regions(dev);
Komuro006839f2010-10-23 07:02:05 +0900735err_out_disable:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 pci_disable_device(dev);
737
Komuro006839f2010-10-23 07:02:05 +0900738err_out_free_mem:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 kfree(socket);
740 return ret;
741}
742
743static void __devexit pd6729_pci_remove(struct pci_dev *dev)
744{
745 int i;
746 struct pd6729_socket *socket = pci_get_drvdata(dev);
747
748 for (i = 0; i < MAX_SOCKETS; i++) {
749 /* Turn off all interrupt sources */
750 indirect_write(&socket[i], I365_CSCINT, 0);
751 indirect_write(&socket[i], I365_INTCTL, 0);
752
753 pcmcia_unregister_socket(&socket[i].socket);
754 }
755
756 if (irq_mode == 1)
757 free_irq(dev->irq, socket);
758 else
759 del_timer_sync(&socket->poll_timer);
760 pci_release_regions(dev);
761 pci_disable_device(dev);
762
763 kfree(socket);
764}
765
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766static struct pci_device_id pd6729_pci_ids[] = {
767 {
768 .vendor = PCI_VENDOR_ID_CIRRUS,
769 .device = PCI_DEVICE_ID_CIRRUS_6729,
770 .subvendor = PCI_ANY_ID,
771 .subdevice = PCI_ANY_ID,
772 },
773 { }
774};
775MODULE_DEVICE_TABLE(pci, pd6729_pci_ids);
776
Sam Ravnborgba66ddf2008-05-01 04:34:51 -0700777static struct pci_driver pd6729_pci_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 .name = "pd6729",
779 .id_table = pd6729_pci_ids,
780 .probe = pd6729_pci_probe,
781 .remove = __devexit_p(pd6729_pci_remove),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782};
783
784static int pd6729_module_init(void)
785{
Sam Ravnborgba66ddf2008-05-01 04:34:51 -0700786 return pci_register_driver(&pd6729_pci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787}
788
789static void pd6729_module_exit(void)
790{
Sam Ravnborgba66ddf2008-05-01 04:34:51 -0700791 pci_unregister_driver(&pd6729_pci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792}
793
794module_init(pd6729_module_init);
795module_exit(pd6729_module_exit);