blob: b61a13663a0a79babf86a0d86722b76d8c02ad40 [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
20#include <pcmcia/cs_types.h>
21#include <pcmcia/ss.h>
22#include <pcmcia/cs.h>
23
24#include <asm/system.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26#include "pd6729.h"
27#include "i82365.h"
28#include "cirrus.h"
29
30MODULE_LICENSE("GPL");
31MODULE_DESCRIPTION("Driver for the Cirrus PD6729 PCI-PCMCIA bridge");
32MODULE_AUTHOR("Jun Komuro <komurojun-mbn@nifty.com>");
33
34#define MAX_SOCKETS 2
35
36/*
37 * simple helper functions
38 * External clock time, in nanoseconds. 120 ns = 8.33 MHz
39 */
40#define to_cycles(ns) ((ns)/120)
41
42#ifndef NO_IRQ
43#define NO_IRQ ((unsigned int)(0))
44#endif
45
46/*
47 * PARAMETERS
48 * irq_mode=n
49 * Specifies the interrupt delivery mode. The default (1) is to use PCI
50 * interrupts; a value of 0 selects ISA interrupts. This must be set for
51 * correct operation of PCI card readers.
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 */
53
54static int irq_mode = 1; /* 0 = ISA interrupt, 1 = PCI interrupt */
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56module_param(irq_mode, int, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057MODULE_PARM_DESC(irq_mode,
58 "interrupt delivery mode. 0 = ISA, 1 = PCI. default is 1");
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60static DEFINE_SPINLOCK(port_lock);
61
62/* basic value read/write functions */
63
64static unsigned char indirect_read(struct pd6729_socket *socket,
65 unsigned short reg)
66{
67 unsigned long port;
68 unsigned char val;
69 unsigned long flags;
70
71 spin_lock_irqsave(&port_lock, flags);
72 reg += socket->number * 0x40;
73 port = socket->io_base;
74 outb(reg, port);
75 val = inb(port + 1);
76 spin_unlock_irqrestore(&port_lock, flags);
77
78 return val;
79}
80
81static unsigned short indirect_read16(struct pd6729_socket *socket,
82 unsigned short reg)
83{
84 unsigned long port;
85 unsigned short tmp;
86 unsigned long flags;
87
88 spin_lock_irqsave(&port_lock, flags);
89 reg = reg + socket->number * 0x40;
90 port = socket->io_base;
91 outb(reg, port);
92 tmp = inb(port + 1);
93 reg++;
94 outb(reg, port);
95 tmp = tmp | (inb(port + 1) << 8);
96 spin_unlock_irqrestore(&port_lock, flags);
97
98 return tmp;
99}
100
101static void indirect_write(struct pd6729_socket *socket, unsigned short reg,
102 unsigned char value)
103{
104 unsigned long port;
105 unsigned long flags;
106
107 spin_lock_irqsave(&port_lock, flags);
108 reg = reg + socket->number * 0x40;
109 port = socket->io_base;
110 outb(reg, port);
111 outb(value, port + 1);
112 spin_unlock_irqrestore(&port_lock, flags);
113}
114
115static void indirect_setbit(struct pd6729_socket *socket, unsigned short reg,
116 unsigned char mask)
117{
118 unsigned long port;
119 unsigned char val;
120 unsigned long flags;
121
122 spin_lock_irqsave(&port_lock, flags);
123 reg = reg + socket->number * 0x40;
124 port = socket->io_base;
125 outb(reg, port);
126 val = inb(port + 1);
127 val |= mask;
128 outb(reg, port);
129 outb(val, port + 1);
130 spin_unlock_irqrestore(&port_lock, flags);
131}
132
133static void indirect_resetbit(struct pd6729_socket *socket, unsigned short reg,
134 unsigned char mask)
135{
136 unsigned long port;
137 unsigned char val;
138 unsigned long flags;
139
140 spin_lock_irqsave(&port_lock, flags);
141 reg = reg + socket->number * 0x40;
142 port = socket->io_base;
143 outb(reg, port);
144 val = inb(port + 1);
145 val &= ~mask;
146 outb(reg, port);
147 outb(val, port + 1);
148 spin_unlock_irqrestore(&port_lock, flags);
149}
150
151static void indirect_write16(struct pd6729_socket *socket, unsigned short reg,
152 unsigned short value)
153{
154 unsigned long port;
155 unsigned char val;
156 unsigned long flags;
157
158 spin_lock_irqsave(&port_lock, flags);
159 reg = reg + socket->number * 0x40;
160 port = socket->io_base;
161
162 outb(reg, port);
163 val = value & 255;
164 outb(val, port + 1);
165
166 reg++;
167
168 outb(reg, port);
169 val = value >> 8;
170 outb(val, port + 1);
171 spin_unlock_irqrestore(&port_lock, flags);
172}
173
174/* Interrupt handler functionality */
175
David Howells7d12e782006-10-05 14:55:46 +0100176static irqreturn_t pd6729_interrupt(int irq, void *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{
178 struct pd6729_socket *socket = (struct pd6729_socket *)dev;
179 int i;
180 int loopcount = 0;
181 int handled = 0;
182 unsigned int events, active = 0;
183
184 while (1) {
185 loopcount++;
186 if (loopcount > 20) {
187 printk(KERN_ERR "pd6729: infinite eventloop "
188 "in interrupt\n");
189 break;
190 }
191
192 active = 0;
193
194 for (i = 0; i < MAX_SOCKETS; i++) {
195 unsigned int csc;
196
197 /* card status change register */
198 csc = indirect_read(&socket[i], I365_CSC);
199 if (csc == 0) /* no events on this socket */
200 continue;
201
202 handled = 1;
203 events = 0;
204
205 if (csc & I365_CSC_DETECT) {
206 events |= SS_DETECT;
Dominik Brodowskia7149f92009-10-24 18:07:16 +0200207 dev_vdbg(&socket[i].socket.dev,
208 "Card detected in socket %i!\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 }
210
211 if (indirect_read(&socket[i], I365_INTCTL)
212 & I365_PC_IOCARD) {
213 /* For IO/CARDS, bit 0 means "read the card" */
214 events |= (csc & I365_CSC_STSCHG)
215 ? SS_STSCHG : 0;
216 } else {
217 /* Check for battery/ready events */
218 events |= (csc & I365_CSC_BVD1)
219 ? SS_BATDEAD : 0;
220 events |= (csc & I365_CSC_BVD2)
221 ? SS_BATWARN : 0;
222 events |= (csc & I365_CSC_READY)
223 ? SS_READY : 0;
224 }
225
Komuro5cbb2b92010-03-20 06:39:19 +0900226 if (events)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 pcmcia_parse_events(&socket[i].socket, events);
Komuro5cbb2b92010-03-20 06:39:19 +0900228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 active |= events;
230 }
231
232 if (active == 0) /* no more events to handle */
233 break;
234 }
235 return IRQ_RETVAL(handled);
236}
237
238/* socket functions */
239
240static void pd6729_interrupt_wrapper(unsigned long data)
241{
242 struct pd6729_socket *socket = (struct pd6729_socket *) data;
243
David Howells7d12e782006-10-05 14:55:46 +0100244 pd6729_interrupt(0, (void *)socket);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 mod_timer(&socket->poll_timer, jiffies + HZ);
246}
247
248static int pd6729_get_status(struct pcmcia_socket *sock, u_int *value)
249{
250 struct pd6729_socket *socket
251 = container_of(sock, struct pd6729_socket, socket);
252 unsigned int status;
253 unsigned int data;
254 struct pd6729_socket *t;
255
256 /* Interface Status Register */
257 status = indirect_read(socket, I365_STATUS);
258 *value = 0;
259
Komuro5cbb2b92010-03-20 06:39:19 +0900260 if ((status & I365_CS_DETECT) == I365_CS_DETECT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 *value |= SS_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
263 /*
264 * IO cards have a different meaning of bits 0,1
265 * Also notice the inverse-logic on the bits
266 */
267 if (indirect_read(socket, I365_INTCTL) & I365_PC_IOCARD) {
268 /* IO card */
269 if (!(status & I365_CS_STSCHG))
270 *value |= SS_STSCHG;
271 } else {
272 /* non I/O card */
273 if (!(status & I365_CS_BVD1))
274 *value |= SS_BATDEAD;
275 if (!(status & I365_CS_BVD2))
276 *value |= SS_BATWARN;
277 }
278
279 if (status & I365_CS_WRPROT)
280 *value |= SS_WRPROT; /* card is write protected */
281
282 if (status & I365_CS_READY)
283 *value |= SS_READY; /* card is not busy */
284
285 if (status & I365_CS_POWERON)
286 *value |= SS_POWERON; /* power is applied to the card */
287
288 t = (socket->number) ? socket : socket + 1;
289 indirect_write(t, PD67_EXT_INDEX, PD67_EXTERN_DATA);
290 data = indirect_read16(t, PD67_EXT_DATA);
291 *value |= (data & PD67_EXD_VS1(socket->number)) ? 0 : SS_3VCARD;
292
293 return 0;
294}
295
296
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297static int pd6729_set_socket(struct pcmcia_socket *sock, socket_state_t *state)
298{
299 struct pd6729_socket *socket
300 = container_of(sock, struct pd6729_socket, socket);
301 unsigned char reg, data;
302
303 /* First, set the global controller options */
304 indirect_write(socket, I365_GBLCTL, 0x00);
305 indirect_write(socket, I365_GENCTL, 0x00);
306
307 /* Values for the IGENC register */
308 socket->card_irq = state->io_irq;
309
310 reg = 0;
Komuro5cbb2b92010-03-20 06:39:19 +0900311 /* The reset bit has "inverse" logic */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 if (!(state->flags & SS_RESET))
313 reg |= I365_PC_RESET;
314 if (state->flags & SS_IOCARD)
315 reg |= I365_PC_IOCARD;
316
317 /* IGENC, Interrupt and General Control Register */
318 indirect_write(socket, I365_INTCTL, reg);
319
320 /* Power registers */
321
322 reg = I365_PWR_NORESET; /* default: disable resetdrv on resume */
323
324 if (state->flags & SS_PWR_AUTO) {
Dominik Brodowskia7149f92009-10-24 18:07:16 +0200325 dev_dbg(&sock->dev, "Auto power\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 reg |= I365_PWR_AUTO; /* automatic power mngmnt */
327 }
328 if (state->flags & SS_OUTPUT_ENA) {
Dominik Brodowskia7149f92009-10-24 18:07:16 +0200329 dev_dbg(&sock->dev, "Power Enabled\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 reg |= I365_PWR_OUT; /* enable power */
331 }
332
333 switch (state->Vcc) {
334 case 0:
335 break;
336 case 33:
Dominik Brodowskia7149f92009-10-24 18:07:16 +0200337 dev_dbg(&sock->dev,
338 "setting voltage to Vcc to 3.3V on socket %i\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 socket->number);
340 reg |= I365_VCC_5V;
341 indirect_setbit(socket, PD67_MISC_CTL_1, PD67_MC1_VCC_3V);
342 break;
343 case 50:
Dominik Brodowskia7149f92009-10-24 18:07:16 +0200344 dev_dbg(&sock->dev,
345 "setting voltage to Vcc to 5V on socket %i\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 socket->number);
347 reg |= I365_VCC_5V;
348 indirect_resetbit(socket, PD67_MISC_CTL_1, PD67_MC1_VCC_3V);
349 break;
350 default:
Dominik Brodowskia7149f92009-10-24 18:07:16 +0200351 dev_dbg(&sock->dev,
352 "pd6729_set_socket called with invalid VCC power "
353 "value: %i\n", state->Vcc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 return -EINVAL;
355 }
356
357 switch (state->Vpp) {
358 case 0:
Dominik Brodowskia7149f92009-10-24 18:07:16 +0200359 dev_dbg(&sock->dev, "not setting Vpp on socket %i\n",
360 socket->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 break;
362 case 33:
363 case 50:
Dominik Brodowskia7149f92009-10-24 18:07:16 +0200364 dev_dbg(&sock->dev, "setting Vpp to Vcc for socket %i\n",
365 socket->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 reg |= I365_VPP1_5V;
367 break;
368 case 120:
Dominik Brodowskia7149f92009-10-24 18:07:16 +0200369 dev_dbg(&sock->dev, "setting Vpp to 12.0\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 reg |= I365_VPP1_12V;
371 break;
372 default:
Dominik Brodowskia7149f92009-10-24 18:07:16 +0200373 dev_dbg(&sock->dev, "pd6729: pd6729_set_socket called with "
374 "invalid VPP power value: %i\n", state->Vpp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 return -EINVAL;
376 }
377
378 /* only write if changed */
379 if (reg != indirect_read(socket, I365_POWER))
380 indirect_write(socket, I365_POWER, reg);
381
382 if (irq_mode == 1) {
Komuro5cbb2b92010-03-20 06:39:19 +0900383 /* all interrupts are to be done as PCI interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 data = PD67_EC1_INV_MGMT_IRQ | PD67_EC1_INV_CARD_IRQ;
385 } else
386 data = 0;
387
388 indirect_write(socket, PD67_EXT_INDEX, PD67_EXT_CTL_1);
389 indirect_write(socket, PD67_EXT_DATA, data);
390
391 /* Enable specific interrupt events */
392
393 reg = 0x00;
Komuro5cbb2b92010-03-20 06:39:19 +0900394 if (state->csc_mask & SS_DETECT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 reg |= I365_CSC_DETECT;
Komuro5cbb2b92010-03-20 06:39:19 +0900396
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 if (state->flags & SS_IOCARD) {
398 if (state->csc_mask & SS_STSCHG)
399 reg |= I365_CSC_STSCHG;
400 } else {
401 if (state->csc_mask & SS_BATDEAD)
402 reg |= I365_CSC_BVD1;
403 if (state->csc_mask & SS_BATWARN)
404 reg |= I365_CSC_BVD2;
405 if (state->csc_mask & SS_READY)
406 reg |= I365_CSC_READY;
407 }
408 if (irq_mode == 1)
409 reg |= 0x30; /* management IRQ: PCI INTA# = "irq 3" */
410 indirect_write(socket, I365_CSCINT, reg);
411
412 reg = indirect_read(socket, I365_INTCTL);
413 if (irq_mode == 1)
414 reg |= 0x03; /* card IRQ: PCI INTA# = "irq 3" */
415 else
416 reg |= socket->card_irq;
417 indirect_write(socket, I365_INTCTL, reg);
418
419 /* now clear the (probably bogus) pending stuff by doing a dummy read */
420 (void)indirect_read(socket, I365_CSC);
421
422 return 0;
423}
424
425static int pd6729_set_io_map(struct pcmcia_socket *sock,
426 struct pccard_io_map *io)
427{
428 struct pd6729_socket *socket
429 = container_of(sock, struct pd6729_socket, socket);
430 unsigned char map, ioctl;
431
432 map = io->map;
433
434 /* Check error conditions */
435 if (map > 1) {
Dominik Brodowskia7149f92009-10-24 18:07:16 +0200436 dev_dbg(&sock->dev, "pd6729_set_io_map with invalid map\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 return -EINVAL;
438 }
439
440 /* Turn off the window before changing anything */
441 if (indirect_read(socket, I365_ADDRWIN) & I365_ENA_IO(map))
442 indirect_resetbit(socket, I365_ADDRWIN, I365_ENA_IO(map));
443
Dominik Brodowskia7149f92009-10-24 18:07:16 +0200444 /* dev_dbg(&sock->dev, "set_io_map: Setting range to %x - %x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 io->start, io->stop);*/
446
447 /* write the new values */
448 indirect_write16(socket, I365_IO(map)+I365_W_START, io->start);
449 indirect_write16(socket, I365_IO(map)+I365_W_STOP, io->stop);
450
451 ioctl = indirect_read(socket, I365_IOCTL) & ~I365_IOCTL_MASK(map);
452
Komuro5cbb2b92010-03-20 06:39:19 +0900453 if (io->flags & MAP_0WS)
454 ioctl |= I365_IOCTL_0WS(map);
455 if (io->flags & MAP_16BIT)
456 ioctl |= I365_IOCTL_16BIT(map);
457 if (io->flags & MAP_AUTOSZ)
458 ioctl |= I365_IOCTL_IOCS16(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
460 indirect_write(socket, I365_IOCTL, ioctl);
461
462 /* Turn the window back on if needed */
463 if (io->flags & MAP_ACTIVE)
464 indirect_setbit(socket, I365_ADDRWIN, I365_ENA_IO(map));
465
466 return 0;
467}
468
469static int pd6729_set_mem_map(struct pcmcia_socket *sock,
470 struct pccard_mem_map *mem)
471{
472 struct pd6729_socket *socket
473 = container_of(sock, struct pd6729_socket, socket);
474 unsigned short base, i;
475 unsigned char map;
476
477 map = mem->map;
478 if (map > 4) {
Dominik Brodowskia7149f92009-10-24 18:07:16 +0200479 dev_warn(&sock->dev, "invalid map requested\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 return -EINVAL;
481 }
482
483 if ((mem->res->start > mem->res->end) || (mem->speed > 1000)) {
Dominik Brodowskia7149f92009-10-24 18:07:16 +0200484 dev_warn(&sock->dev, "invalid invalid address / speed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 return -EINVAL;
486 }
487
488 /* Turn off the window before changing anything */
489 if (indirect_read(socket, I365_ADDRWIN) & I365_ENA_MEM(map))
490 indirect_resetbit(socket, I365_ADDRWIN, I365_ENA_MEM(map));
491
492 /* write the start address */
493 base = I365_MEM(map);
494 i = (mem->res->start >> 12) & 0x0fff;
495 if (mem->flags & MAP_16BIT)
496 i |= I365_MEM_16BIT;
497 if (mem->flags & MAP_0WS)
498 i |= I365_MEM_0WS;
499 indirect_write16(socket, base + I365_W_START, i);
500
501 /* write the stop address */
502
Komuro5cbb2b92010-03-20 06:39:19 +0900503 i = (mem->res->end >> 12) & 0x0fff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 switch (to_cycles(mem->speed)) {
505 case 0:
506 break;
507 case 1:
508 i |= I365_MEM_WS0;
509 break;
510 case 2:
511 i |= I365_MEM_WS1;
512 break;
513 default:
514 i |= I365_MEM_WS1 | I365_MEM_WS0;
515 break;
516 }
517
518 indirect_write16(socket, base + I365_W_STOP, i);
519
520 /* Take care of high byte */
521 indirect_write(socket, PD67_EXT_INDEX, PD67_MEM_PAGE(map));
522 indirect_write(socket, PD67_EXT_DATA, mem->res->start >> 24);
523
524 /* card start */
525
526 i = ((mem->card_start - mem->res->start) >> 12) & 0x3fff;
527 if (mem->flags & MAP_WRPROT)
528 i |= I365_MEM_WRPROT;
529 if (mem->flags & MAP_ATTRIB) {
Dominik Brodowskia7149f92009-10-24 18:07:16 +0200530 /* dev_dbg(&sock->dev, "requesting attribute memory for "
531 "socket %i\n", socket->number);*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 i |= I365_MEM_REG;
533 } else {
Dominik Brodowskia7149f92009-10-24 18:07:16 +0200534 /* dev_dbg(&sock->dev, "requesting normal memory for "
535 "socket %i\n", socket->number);*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 }
537 indirect_write16(socket, base + I365_W_OFF, i);
538
539 /* Enable the window if necessary */
540 if (mem->flags & MAP_ACTIVE)
541 indirect_setbit(socket, I365_ADDRWIN, I365_ENA_MEM(map));
542
543 return 0;
544}
545
546static int pd6729_init(struct pcmcia_socket *sock)
547{
548 int i;
549 struct resource res = { .end = 0x0fff };
550 pccard_io_map io = { 0, 0, 0, 0, 1 };
551 pccard_mem_map mem = { .res = &res, };
552
553 pd6729_set_socket(sock, &dead_socket);
554 for (i = 0; i < 2; i++) {
555 io.map = i;
556 pd6729_set_io_map(sock, &io);
557 }
558 for (i = 0; i < 5; i++) {
559 mem.map = i;
560 pd6729_set_mem_map(sock, &mem);
561 }
562
563 return 0;
564}
565
566
567/* the pccard structure and its functions */
568static struct pccard_operations pd6729_operations = {
Komuro5cbb2b92010-03-20 06:39:19 +0900569 .init = pd6729_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 .get_status = pd6729_get_status,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 .set_socket = pd6729_set_socket,
572 .set_io_map = pd6729_set_io_map,
573 .set_mem_map = pd6729_set_mem_map,
574};
575
David Howells7d12e782006-10-05 14:55:46 +0100576static irqreturn_t pd6729_test(int irq, void *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577{
Dominik Brodowskia7149f92009-10-24 18:07:16 +0200578 pr_devel("-> hit on irq %d\n", irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 return IRQ_HANDLED;
580}
581
Komuro3e022d02006-11-04 10:00:47 +0900582static int pd6729_check_irq(int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583{
Komuro5cbb2b92010-03-20 06:39:19 +0900584 int ret;
585
586 ret = request_irq(irq, pd6729_test, IRQF_PROBE_SHARED, "x",
587 pd6729_test);
588 if (ret)
589 return -1;
590
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 free_irq(irq, pd6729_test);
592 return 0;
593}
594
Andrew Morton9781b8b2006-05-20 15:00:32 -0700595static u_int __devinit pd6729_isa_scan(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596{
597 u_int mask0, mask = 0;
598 int i;
599
600 if (irq_mode == 1) {
601 printk(KERN_INFO "pd6729: PCI card interrupts, "
Komuro5cbb2b92010-03-20 06:39:19 +0900602 "PCI status changes\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 return 0;
604 }
605
Komurof9097dc2010-02-28 15:39:33 +0900606 mask0 = PD67_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
608 /* just find interrupts that aren't in use */
609 for (i = 0; i < 16; i++)
Komuro3e022d02006-11-04 10:00:47 +0900610 if ((mask0 & (1 << i)) && (pd6729_check_irq(i) == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 mask |= (1 << i);
612
613 printk(KERN_INFO "pd6729: ISA irqs = ");
614 for (i = 0; i < 16; i++)
615 if (mask & (1<<i))
616 printk("%s%d", ((mask & ((1<<i)-1)) ? "," : ""), i);
617
Komuro5cbb2b92010-03-20 06:39:19 +0900618 if (mask == 0)
619 printk("none!");
620 else
621 printk(" polling status changes.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
623 return mask;
624}
625
626static int __devinit pd6729_pci_probe(struct pci_dev *dev,
627 const struct pci_device_id *id)
628{
629 int i, j, ret;
630 u_int mask;
631 char configbyte;
632 struct pd6729_socket *socket;
633
Dominik Brodowski8084b372005-12-11 21:18:26 +0100634 socket = kzalloc(sizeof(struct pd6729_socket) * MAX_SOCKETS,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 GFP_KERNEL);
Komuro5cbb2b92010-03-20 06:39:19 +0900636 if (!socket) {
637 dev_warn(&dev->dev, "failed to kzalloc socket.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 return -ENOMEM;
Komuro5cbb2b92010-03-20 06:39:19 +0900639 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
Komuro5cbb2b92010-03-20 06:39:19 +0900641 ret = pci_enable_device(dev);
642 if (ret) {
643 dev_warn(&dev->dev, "failed to enable pci_device.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 goto err_out_free_mem;
Komuro5cbb2b92010-03-20 06:39:19 +0900645 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
Komuro94efb722009-10-24 08:07:39 +0900647 if (!pci_resource_start(dev, 0)) {
Dominik Brodowskia7149f92009-10-24 18:07:16 +0200648 dev_warn(&dev->dev, "refusing to load the driver as the "
649 "io_base is NULL.\n");
Komuro94efb722009-10-24 08:07:39 +0900650 goto err_out_free_mem;
651 }
652
Dominik Brodowskia7149f92009-10-24 18:07:16 +0200653 dev_info(&dev->dev, "Cirrus PD6729 PCI to PCMCIA Bridge at 0x%llx "
654 "on irq %d\n",
Greg Kroah-Hartman490ab722006-06-12 15:17:34 -0700655 (unsigned long long)pci_resource_start(dev, 0), dev->irq);
Komuro5cbb2b92010-03-20 06:39:19 +0900656 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 * Since we have no memory BARs some firmware may not
658 * have had PCI_COMMAND_MEMORY enabled, yet the device needs it.
659 */
660 pci_read_config_byte(dev, PCI_COMMAND, &configbyte);
661 if (!(configbyte & PCI_COMMAND_MEMORY)) {
Dominik Brodowskia7149f92009-10-24 18:07:16 +0200662 dev_dbg(&dev->dev, "pd6729: Enabling PCI_COMMAND_MEMORY.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 configbyte |= PCI_COMMAND_MEMORY;
664 pci_write_config_byte(dev, PCI_COMMAND, configbyte);
665 }
666
667 ret = pci_request_regions(dev, "pd6729");
668 if (ret) {
Dominik Brodowskia7149f92009-10-24 18:07:16 +0200669 dev_warn(&dev->dev, "pci request region failed.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 goto err_out_disable;
671 }
672
673 if (dev->irq == NO_IRQ)
674 irq_mode = 0; /* fall back to ISA interrupt mode */
675
676 mask = pd6729_isa_scan();
677 if (irq_mode == 0 && mask == 0) {
Dominik Brodowskia7149f92009-10-24 18:07:16 +0200678 dev_warn(&dev->dev, "no ISA interrupt is available.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 goto err_out_free_res;
680 }
681
682 for (i = 0; i < MAX_SOCKETS; i++) {
683 socket[i].io_base = pci_resource_start(dev, 0);
Dominik Brodowskic35e66a2005-05-05 16:15:56 -0700684 socket[i].socket.features |= SS_CAP_PAGE_REGS | SS_CAP_PCCARD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 socket[i].socket.map_size = 0x1000;
686 socket[i].socket.irq_mask = mask;
687 socket[i].socket.pci_irq = dev->irq;
Dominik Brodowski7a96e872010-03-13 17:42:39 +0100688 socket[i].socket.cb_dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 socket[i].socket.owner = THIS_MODULE;
690
691 socket[i].number = i;
692
693 socket[i].socket.ops = &pd6729_operations;
694 socket[i].socket.resource_ops = &pccard_nonstatic_ops;
Greg Kroah-Hartman87373312006-09-12 17:00:10 +0200695 socket[i].socket.dev.parent = &dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 socket[i].socket.driver_data = &socket[i];
697 }
698
699 pci_set_drvdata(dev, socket);
700 if (irq_mode == 1) {
701 /* Register the interrupt handler */
Komuro5cbb2b92010-03-20 06:39:19 +0900702 ret = request_irq(dev->irq, pd6729_interrupt, IRQF_SHARED,
703 "pd6729", socket);
704 if (ret) {
Dominik Brodowskia7149f92009-10-24 18:07:16 +0200705 dev_err(&dev->dev, "Failed to register irq %d\n",
706 dev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 goto err_out_free_res;
708 }
709 } else {
710 /* poll Card status change */
711 init_timer(&socket->poll_timer);
712 socket->poll_timer.function = pd6729_interrupt_wrapper;
713 socket->poll_timer.data = (unsigned long)socket;
714 socket->poll_timer.expires = jiffies + HZ;
715 add_timer(&socket->poll_timer);
716 }
717
718 for (i = 0; i < MAX_SOCKETS; i++) {
719 ret = pcmcia_register_socket(&socket[i].socket);
720 if (ret) {
Dominik Brodowskia7149f92009-10-24 18:07:16 +0200721 dev_warn(&dev->dev, "pcmcia_register_socket failed.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 for (j = 0; j < i ; j++)
723 pcmcia_unregister_socket(&socket[j].socket);
724 goto err_out_free_res2;
725 }
726 }
727
728 return 0;
729
730 err_out_free_res2:
731 if (irq_mode == 1)
732 free_irq(dev->irq, socket);
733 else
734 del_timer_sync(&socket->poll_timer);
735 err_out_free_res:
736 pci_release_regions(dev);
737 err_out_disable:
738 pci_disable_device(dev);
739
740 err_out_free_mem:
741 kfree(socket);
742 return ret;
743}
744
745static void __devexit pd6729_pci_remove(struct pci_dev *dev)
746{
747 int i;
748 struct pd6729_socket *socket = pci_get_drvdata(dev);
749
750 for (i = 0; i < MAX_SOCKETS; i++) {
751 /* Turn off all interrupt sources */
752 indirect_write(&socket[i], I365_CSCINT, 0);
753 indirect_write(&socket[i], I365_INTCTL, 0);
754
755 pcmcia_unregister_socket(&socket[i].socket);
756 }
757
758 if (irq_mode == 1)
759 free_irq(dev->irq, socket);
760 else
761 del_timer_sync(&socket->poll_timer);
762 pci_release_regions(dev);
763 pci_disable_device(dev);
764
765 kfree(socket);
766}
767
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768static struct pci_device_id pd6729_pci_ids[] = {
769 {
770 .vendor = PCI_VENDOR_ID_CIRRUS,
771 .device = PCI_DEVICE_ID_CIRRUS_6729,
772 .subvendor = PCI_ANY_ID,
773 .subdevice = PCI_ANY_ID,
774 },
775 { }
776};
777MODULE_DEVICE_TABLE(pci, pd6729_pci_ids);
778
Sam Ravnborgba66ddf2008-05-01 04:34:51 -0700779static struct pci_driver pd6729_pci_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 .name = "pd6729",
781 .id_table = pd6729_pci_ids,
782 .probe = pd6729_pci_probe,
783 .remove = __devexit_p(pd6729_pci_remove),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784};
785
786static int pd6729_module_init(void)
787{
Sam Ravnborgba66ddf2008-05-01 04:34:51 -0700788 return pci_register_driver(&pd6729_pci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789}
790
791static void pd6729_module_exit(void)
792{
Sam Ravnborgba66ddf2008-05-01 04:34:51 -0700793 pci_unregister_driver(&pd6729_pci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794}
795
796module_init(pd6729_module_init);
797module_exit(pd6729_module_exit);