blob: a4c16ee5c7188d24bce41f834851f61f5e56d9ea [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
23#include "pd6729.h"
24#include "i82365.h"
25#include "cirrus.h"
26
27MODULE_LICENSE("GPL");
28MODULE_DESCRIPTION("Driver for the Cirrus PD6729 PCI-PCMCIA bridge");
29MODULE_AUTHOR("Jun Komuro <komurojun-mbn@nifty.com>");
30
31#define MAX_SOCKETS 2
32
33/*
34 * simple helper functions
35 * External clock time, in nanoseconds. 120 ns = 8.33 MHz
36 */
37#define to_cycles(ns) ((ns)/120)
38
39#ifndef NO_IRQ
40#define NO_IRQ ((unsigned int)(0))
41#endif
42
43/*
44 * PARAMETERS
45 * irq_mode=n
46 * Specifies the interrupt delivery mode. The default (1) is to use PCI
47 * interrupts; a value of 0 selects ISA interrupts. This must be set for
48 * correct operation of PCI card readers.
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 */
50
51static int irq_mode = 1; /* 0 = ISA interrupt, 1 = PCI interrupt */
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53module_param(irq_mode, int, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054MODULE_PARM_DESC(irq_mode,
55 "interrupt delivery mode. 0 = ISA, 1 = PCI. default is 1");
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57static DEFINE_SPINLOCK(port_lock);
58
59/* basic value read/write functions */
60
61static unsigned char indirect_read(struct pd6729_socket *socket,
62 unsigned short reg)
63{
64 unsigned long port;
65 unsigned char val;
66 unsigned long flags;
67
68 spin_lock_irqsave(&port_lock, flags);
69 reg += socket->number * 0x40;
70 port = socket->io_base;
71 outb(reg, port);
72 val = inb(port + 1);
73 spin_unlock_irqrestore(&port_lock, flags);
74
75 return val;
76}
77
78static unsigned short indirect_read16(struct pd6729_socket *socket,
79 unsigned short reg)
80{
81 unsigned long port;
82 unsigned short tmp;
83 unsigned long flags;
84
85 spin_lock_irqsave(&port_lock, flags);
86 reg = reg + socket->number * 0x40;
87 port = socket->io_base;
88 outb(reg, port);
89 tmp = inb(port + 1);
90 reg++;
91 outb(reg, port);
92 tmp = tmp | (inb(port + 1) << 8);
93 spin_unlock_irqrestore(&port_lock, flags);
94
95 return tmp;
96}
97
98static void indirect_write(struct pd6729_socket *socket, unsigned short reg,
99 unsigned char value)
100{
101 unsigned long port;
102 unsigned long flags;
103
104 spin_lock_irqsave(&port_lock, flags);
105 reg = reg + socket->number * 0x40;
106 port = socket->io_base;
107 outb(reg, port);
108 outb(value, port + 1);
109 spin_unlock_irqrestore(&port_lock, flags);
110}
111
112static void indirect_setbit(struct pd6729_socket *socket, unsigned short reg,
113 unsigned char mask)
114{
115 unsigned long port;
116 unsigned char val;
117 unsigned long flags;
118
119 spin_lock_irqsave(&port_lock, flags);
120 reg = reg + socket->number * 0x40;
121 port = socket->io_base;
122 outb(reg, port);
123 val = inb(port + 1);
124 val |= mask;
125 outb(reg, port);
126 outb(val, port + 1);
127 spin_unlock_irqrestore(&port_lock, flags);
128}
129
130static void indirect_resetbit(struct pd6729_socket *socket, unsigned short reg,
131 unsigned char mask)
132{
133 unsigned long port;
134 unsigned char val;
135 unsigned long flags;
136
137 spin_lock_irqsave(&port_lock, flags);
138 reg = reg + socket->number * 0x40;
139 port = socket->io_base;
140 outb(reg, port);
141 val = inb(port + 1);
142 val &= ~mask;
143 outb(reg, port);
144 outb(val, port + 1);
145 spin_unlock_irqrestore(&port_lock, flags);
146}
147
148static void indirect_write16(struct pd6729_socket *socket, unsigned short reg,
149 unsigned short value)
150{
151 unsigned long port;
152 unsigned char val;
153 unsigned long flags;
154
155 spin_lock_irqsave(&port_lock, flags);
156 reg = reg + socket->number * 0x40;
157 port = socket->io_base;
158
159 outb(reg, port);
160 val = value & 255;
161 outb(val, port + 1);
162
163 reg++;
164
165 outb(reg, port);
166 val = value >> 8;
167 outb(val, port + 1);
168 spin_unlock_irqrestore(&port_lock, flags);
169}
170
171/* Interrupt handler functionality */
172
David Howells7d12e782006-10-05 14:55:46 +0100173static irqreturn_t pd6729_interrupt(int irq, void *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174{
175 struct pd6729_socket *socket = (struct pd6729_socket *)dev;
176 int i;
177 int loopcount = 0;
178 int handled = 0;
179 unsigned int events, active = 0;
180
181 while (1) {
182 loopcount++;
183 if (loopcount > 20) {
184 printk(KERN_ERR "pd6729: infinite eventloop "
185 "in interrupt\n");
186 break;
187 }
188
189 active = 0;
190
191 for (i = 0; i < MAX_SOCKETS; i++) {
192 unsigned int csc;
193
194 /* card status change register */
195 csc = indirect_read(&socket[i], I365_CSC);
196 if (csc == 0) /* no events on this socket */
197 continue;
198
199 handled = 1;
200 events = 0;
201
202 if (csc & I365_CSC_DETECT) {
203 events |= SS_DETECT;
Dominik Brodowskia7149f92009-10-24 18:07:16 +0200204 dev_vdbg(&socket[i].socket.dev,
205 "Card detected in socket %i!\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 }
207
208 if (indirect_read(&socket[i], I365_INTCTL)
209 & I365_PC_IOCARD) {
210 /* For IO/CARDS, bit 0 means "read the card" */
211 events |= (csc & I365_CSC_STSCHG)
212 ? SS_STSCHG : 0;
213 } else {
214 /* Check for battery/ready events */
215 events |= (csc & I365_CSC_BVD1)
216 ? SS_BATDEAD : 0;
217 events |= (csc & I365_CSC_BVD2)
218 ? SS_BATWARN : 0;
219 events |= (csc & I365_CSC_READY)
220 ? SS_READY : 0;
221 }
222
Komuro5cbb2b92010-03-20 06:39:19 +0900223 if (events)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 pcmcia_parse_events(&socket[i].socket, events);
Komuro5cbb2b92010-03-20 06:39:19 +0900225
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 active |= events;
227 }
228
229 if (active == 0) /* no more events to handle */
230 break;
231 }
232 return IRQ_RETVAL(handled);
233}
234
235/* socket functions */
236
237static void pd6729_interrupt_wrapper(unsigned long data)
238{
239 struct pd6729_socket *socket = (struct pd6729_socket *) data;
240
David Howells7d12e782006-10-05 14:55:46 +0100241 pd6729_interrupt(0, (void *)socket);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 mod_timer(&socket->poll_timer, jiffies + HZ);
243}
244
245static int pd6729_get_status(struct pcmcia_socket *sock, u_int *value)
246{
247 struct pd6729_socket *socket
248 = container_of(sock, struct pd6729_socket, socket);
249 unsigned int status;
250 unsigned int data;
251 struct pd6729_socket *t;
252
253 /* Interface Status Register */
254 status = indirect_read(socket, I365_STATUS);
255 *value = 0;
256
Komuro5cbb2b92010-03-20 06:39:19 +0900257 if ((status & I365_CS_DETECT) == I365_CS_DETECT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 *value |= SS_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
260 /*
261 * IO cards have a different meaning of bits 0,1
262 * Also notice the inverse-logic on the bits
263 */
264 if (indirect_read(socket, I365_INTCTL) & I365_PC_IOCARD) {
265 /* IO card */
266 if (!(status & I365_CS_STSCHG))
267 *value |= SS_STSCHG;
268 } else {
269 /* non I/O card */
270 if (!(status & I365_CS_BVD1))
271 *value |= SS_BATDEAD;
272 if (!(status & I365_CS_BVD2))
273 *value |= SS_BATWARN;
274 }
275
276 if (status & I365_CS_WRPROT)
277 *value |= SS_WRPROT; /* card is write protected */
278
279 if (status & I365_CS_READY)
280 *value |= SS_READY; /* card is not busy */
281
282 if (status & I365_CS_POWERON)
283 *value |= SS_POWERON; /* power is applied to the card */
284
285 t = (socket->number) ? socket : socket + 1;
286 indirect_write(t, PD67_EXT_INDEX, PD67_EXTERN_DATA);
287 data = indirect_read16(t, PD67_EXT_DATA);
288 *value |= (data & PD67_EXD_VS1(socket->number)) ? 0 : SS_3VCARD;
289
290 return 0;
291}
292
293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294static int pd6729_set_socket(struct pcmcia_socket *sock, socket_state_t *state)
295{
296 struct pd6729_socket *socket
297 = container_of(sock, struct pd6729_socket, socket);
298 unsigned char reg, data;
299
300 /* First, set the global controller options */
301 indirect_write(socket, I365_GBLCTL, 0x00);
302 indirect_write(socket, I365_GENCTL, 0x00);
303
304 /* Values for the IGENC register */
305 socket->card_irq = state->io_irq;
306
307 reg = 0;
Komuro5cbb2b92010-03-20 06:39:19 +0900308 /* The reset bit has "inverse" logic */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 if (!(state->flags & SS_RESET))
310 reg |= I365_PC_RESET;
311 if (state->flags & SS_IOCARD)
312 reg |= I365_PC_IOCARD;
313
314 /* IGENC, Interrupt and General Control Register */
315 indirect_write(socket, I365_INTCTL, reg);
316
317 /* Power registers */
318
319 reg = I365_PWR_NORESET; /* default: disable resetdrv on resume */
320
321 if (state->flags & SS_PWR_AUTO) {
Dominik Brodowskia7149f92009-10-24 18:07:16 +0200322 dev_dbg(&sock->dev, "Auto power\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 reg |= I365_PWR_AUTO; /* automatic power mngmnt */
324 }
325 if (state->flags & SS_OUTPUT_ENA) {
Dominik Brodowskia7149f92009-10-24 18:07:16 +0200326 dev_dbg(&sock->dev, "Power Enabled\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 reg |= I365_PWR_OUT; /* enable power */
328 }
329
330 switch (state->Vcc) {
331 case 0:
332 break;
333 case 33:
Dominik Brodowskia7149f92009-10-24 18:07:16 +0200334 dev_dbg(&sock->dev,
335 "setting voltage to Vcc to 3.3V on socket %i\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 socket->number);
337 reg |= I365_VCC_5V;
338 indirect_setbit(socket, PD67_MISC_CTL_1, PD67_MC1_VCC_3V);
339 break;
340 case 50:
Dominik Brodowskia7149f92009-10-24 18:07:16 +0200341 dev_dbg(&sock->dev,
342 "setting voltage to Vcc to 5V on socket %i\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 socket->number);
344 reg |= I365_VCC_5V;
345 indirect_resetbit(socket, PD67_MISC_CTL_1, PD67_MC1_VCC_3V);
346 break;
347 default:
Dominik Brodowskia7149f92009-10-24 18:07:16 +0200348 dev_dbg(&sock->dev,
349 "pd6729_set_socket called with invalid VCC power "
350 "value: %i\n", state->Vcc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 return -EINVAL;
352 }
353
354 switch (state->Vpp) {
355 case 0:
Dominik Brodowskia7149f92009-10-24 18:07:16 +0200356 dev_dbg(&sock->dev, "not setting Vpp on socket %i\n",
357 socket->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 break;
359 case 33:
360 case 50:
Dominik Brodowskia7149f92009-10-24 18:07:16 +0200361 dev_dbg(&sock->dev, "setting Vpp to Vcc for socket %i\n",
362 socket->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 reg |= I365_VPP1_5V;
364 break;
365 case 120:
Dominik Brodowskia7149f92009-10-24 18:07:16 +0200366 dev_dbg(&sock->dev, "setting Vpp to 12.0\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 reg |= I365_VPP1_12V;
368 break;
369 default:
Dominik Brodowskia7149f92009-10-24 18:07:16 +0200370 dev_dbg(&sock->dev, "pd6729: pd6729_set_socket called with "
371 "invalid VPP power value: %i\n", state->Vpp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 return -EINVAL;
373 }
374
375 /* only write if changed */
376 if (reg != indirect_read(socket, I365_POWER))
377 indirect_write(socket, I365_POWER, reg);
378
379 if (irq_mode == 1) {
Komuro5cbb2b92010-03-20 06:39:19 +0900380 /* all interrupts are to be done as PCI interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 data = PD67_EC1_INV_MGMT_IRQ | PD67_EC1_INV_CARD_IRQ;
382 } else
383 data = 0;
384
385 indirect_write(socket, PD67_EXT_INDEX, PD67_EXT_CTL_1);
386 indirect_write(socket, PD67_EXT_DATA, data);
387
388 /* Enable specific interrupt events */
389
390 reg = 0x00;
Komuro5cbb2b92010-03-20 06:39:19 +0900391 if (state->csc_mask & SS_DETECT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 reg |= I365_CSC_DETECT;
Komuro5cbb2b92010-03-20 06:39:19 +0900393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 if (state->flags & SS_IOCARD) {
395 if (state->csc_mask & SS_STSCHG)
396 reg |= I365_CSC_STSCHG;
397 } else {
398 if (state->csc_mask & SS_BATDEAD)
399 reg |= I365_CSC_BVD1;
400 if (state->csc_mask & SS_BATWARN)
401 reg |= I365_CSC_BVD2;
402 if (state->csc_mask & SS_READY)
403 reg |= I365_CSC_READY;
404 }
405 if (irq_mode == 1)
406 reg |= 0x30; /* management IRQ: PCI INTA# = "irq 3" */
407 indirect_write(socket, I365_CSCINT, reg);
408
409 reg = indirect_read(socket, I365_INTCTL);
410 if (irq_mode == 1)
411 reg |= 0x03; /* card IRQ: PCI INTA# = "irq 3" */
412 else
413 reg |= socket->card_irq;
414 indirect_write(socket, I365_INTCTL, reg);
415
416 /* now clear the (probably bogus) pending stuff by doing a dummy read */
417 (void)indirect_read(socket, I365_CSC);
418
419 return 0;
420}
421
422static int pd6729_set_io_map(struct pcmcia_socket *sock,
423 struct pccard_io_map *io)
424{
425 struct pd6729_socket *socket
426 = container_of(sock, struct pd6729_socket, socket);
427 unsigned char map, ioctl;
428
429 map = io->map;
430
431 /* Check error conditions */
432 if (map > 1) {
Dominik Brodowskia7149f92009-10-24 18:07:16 +0200433 dev_dbg(&sock->dev, "pd6729_set_io_map with invalid map\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 return -EINVAL;
435 }
436
437 /* Turn off the window before changing anything */
438 if (indirect_read(socket, I365_ADDRWIN) & I365_ENA_IO(map))
439 indirect_resetbit(socket, I365_ADDRWIN, I365_ENA_IO(map));
440
Dominik Brodowskia7149f92009-10-24 18:07:16 +0200441 /* dev_dbg(&sock->dev, "set_io_map: Setting range to %x - %x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 io->start, io->stop);*/
443
444 /* write the new values */
445 indirect_write16(socket, I365_IO(map)+I365_W_START, io->start);
446 indirect_write16(socket, I365_IO(map)+I365_W_STOP, io->stop);
447
448 ioctl = indirect_read(socket, I365_IOCTL) & ~I365_IOCTL_MASK(map);
449
Komuro5cbb2b92010-03-20 06:39:19 +0900450 if (io->flags & MAP_0WS)
451 ioctl |= I365_IOCTL_0WS(map);
452 if (io->flags & MAP_16BIT)
453 ioctl |= I365_IOCTL_16BIT(map);
454 if (io->flags & MAP_AUTOSZ)
455 ioctl |= I365_IOCTL_IOCS16(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
457 indirect_write(socket, I365_IOCTL, ioctl);
458
459 /* Turn the window back on if needed */
460 if (io->flags & MAP_ACTIVE)
461 indirect_setbit(socket, I365_ADDRWIN, I365_ENA_IO(map));
462
463 return 0;
464}
465
466static int pd6729_set_mem_map(struct pcmcia_socket *sock,
467 struct pccard_mem_map *mem)
468{
469 struct pd6729_socket *socket
470 = container_of(sock, struct pd6729_socket, socket);
471 unsigned short base, i;
472 unsigned char map;
473
474 map = mem->map;
475 if (map > 4) {
Dominik Brodowskia7149f92009-10-24 18:07:16 +0200476 dev_warn(&sock->dev, "invalid map requested\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 return -EINVAL;
478 }
479
480 if ((mem->res->start > mem->res->end) || (mem->speed > 1000)) {
Dominik Brodowskia7149f92009-10-24 18:07:16 +0200481 dev_warn(&sock->dev, "invalid invalid address / speed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 return -EINVAL;
483 }
484
485 /* Turn off the window before changing anything */
486 if (indirect_read(socket, I365_ADDRWIN) & I365_ENA_MEM(map))
487 indirect_resetbit(socket, I365_ADDRWIN, I365_ENA_MEM(map));
488
489 /* write the start address */
490 base = I365_MEM(map);
491 i = (mem->res->start >> 12) & 0x0fff;
492 if (mem->flags & MAP_16BIT)
493 i |= I365_MEM_16BIT;
494 if (mem->flags & MAP_0WS)
495 i |= I365_MEM_0WS;
496 indirect_write16(socket, base + I365_W_START, i);
497
498 /* write the stop address */
499
Komuro5cbb2b92010-03-20 06:39:19 +0900500 i = (mem->res->end >> 12) & 0x0fff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 switch (to_cycles(mem->speed)) {
502 case 0:
503 break;
504 case 1:
505 i |= I365_MEM_WS0;
506 break;
507 case 2:
508 i |= I365_MEM_WS1;
509 break;
510 default:
511 i |= I365_MEM_WS1 | I365_MEM_WS0;
512 break;
513 }
514
515 indirect_write16(socket, base + I365_W_STOP, i);
516
517 /* Take care of high byte */
518 indirect_write(socket, PD67_EXT_INDEX, PD67_MEM_PAGE(map));
519 indirect_write(socket, PD67_EXT_DATA, mem->res->start >> 24);
520
521 /* card start */
522
523 i = ((mem->card_start - mem->res->start) >> 12) & 0x3fff;
524 if (mem->flags & MAP_WRPROT)
525 i |= I365_MEM_WRPROT;
526 if (mem->flags & MAP_ATTRIB) {
Dominik Brodowskia7149f92009-10-24 18:07:16 +0200527 /* dev_dbg(&sock->dev, "requesting attribute memory for "
528 "socket %i\n", socket->number);*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 i |= I365_MEM_REG;
530 } else {
Dominik Brodowskia7149f92009-10-24 18:07:16 +0200531 /* dev_dbg(&sock->dev, "requesting normal memory for "
532 "socket %i\n", socket->number);*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 }
534 indirect_write16(socket, base + I365_W_OFF, i);
535
536 /* Enable the window if necessary */
537 if (mem->flags & MAP_ACTIVE)
538 indirect_setbit(socket, I365_ADDRWIN, I365_ENA_MEM(map));
539
540 return 0;
541}
542
543static int pd6729_init(struct pcmcia_socket *sock)
544{
545 int i;
546 struct resource res = { .end = 0x0fff };
547 pccard_io_map io = { 0, 0, 0, 0, 1 };
548 pccard_mem_map mem = { .res = &res, };
549
550 pd6729_set_socket(sock, &dead_socket);
551 for (i = 0; i < 2; i++) {
552 io.map = i;
553 pd6729_set_io_map(sock, &io);
554 }
555 for (i = 0; i < 5; i++) {
556 mem.map = i;
557 pd6729_set_mem_map(sock, &mem);
558 }
559
560 return 0;
561}
562
563
564/* the pccard structure and its functions */
565static struct pccard_operations pd6729_operations = {
Komuro5cbb2b92010-03-20 06:39:19 +0900566 .init = pd6729_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 .get_status = pd6729_get_status,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 .set_socket = pd6729_set_socket,
569 .set_io_map = pd6729_set_io_map,
570 .set_mem_map = pd6729_set_mem_map,
571};
572
David Howells7d12e782006-10-05 14:55:46 +0100573static irqreturn_t pd6729_test(int irq, void *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574{
Dominik Brodowskia7149f92009-10-24 18:07:16 +0200575 pr_devel("-> hit on irq %d\n", irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 return IRQ_HANDLED;
577}
578
Komuro3e022d02006-11-04 10:00:47 +0900579static int pd6729_check_irq(int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580{
Komuro5cbb2b92010-03-20 06:39:19 +0900581 int ret;
582
583 ret = request_irq(irq, pd6729_test, IRQF_PROBE_SHARED, "x",
584 pd6729_test);
585 if (ret)
586 return -1;
587
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 free_irq(irq, pd6729_test);
589 return 0;
590}
591
Bill Pemberton34cdf252012-11-19 13:23:12 -0500592static u_int 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, "
Komuro5cbb2b92010-03-20 06:39:19 +0900599 "PCI status changes\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 return 0;
601 }
602
Komurof9097dc2010-02-28 15:39:33 +0900603 mask0 = PD67_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
605 /* just find interrupts that aren't in use */
606 for (i = 0; i < 16; i++)
Komuro3e022d02006-11-04 10:00:47 +0900607 if ((mask0 & (1 << i)) && (pd6729_check_irq(i) == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 mask |= (1 << i);
609
610 printk(KERN_INFO "pd6729: ISA irqs = ");
611 for (i = 0; i < 16; i++)
612 if (mask & (1<<i))
613 printk("%s%d", ((mask & ((1<<i)-1)) ? "," : ""), i);
614
Komuro5cbb2b92010-03-20 06:39:19 +0900615 if (mask == 0)
616 printk("none!");
617 else
618 printk(" polling status changes.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
620 return mask;
621}
622
Bill Pemberton34cdf252012-11-19 13:23:12 -0500623static int pd6729_pci_probe(struct pci_dev *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 const struct pci_device_id *id)
625{
626 int i, j, ret;
627 u_int mask;
628 char configbyte;
629 struct pd6729_socket *socket;
630
Dominik Brodowski8084b372005-12-11 21:18:26 +0100631 socket = kzalloc(sizeof(struct pd6729_socket) * MAX_SOCKETS,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 GFP_KERNEL);
Komuro5cbb2b92010-03-20 06:39:19 +0900633 if (!socket) {
634 dev_warn(&dev->dev, "failed to kzalloc socket.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 return -ENOMEM;
Komuro5cbb2b92010-03-20 06:39:19 +0900636 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Komuro5cbb2b92010-03-20 06:39:19 +0900638 ret = pci_enable_device(dev);
639 if (ret) {
640 dev_warn(&dev->dev, "failed to enable pci_device.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 goto err_out_free_mem;
Komuro5cbb2b92010-03-20 06:39:19 +0900642 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
Komuro94efb722009-10-24 08:07:39 +0900644 if (!pci_resource_start(dev, 0)) {
Dominik Brodowskia7149f92009-10-24 18:07:16 +0200645 dev_warn(&dev->dev, "refusing to load the driver as the "
646 "io_base is NULL.\n");
Wei Yongjun214da672013-06-07 10:16:54 +0800647 ret = -ENOMEM;
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");
Wei Yongjun214da672013-06-07 10:16:54 +0800677 ret = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 goto err_out_free_res;
679 }
680
681 for (i = 0; i < MAX_SOCKETS; i++) {
682 socket[i].io_base = pci_resource_start(dev, 0);
Dominik Brodowskic35e66a2005-05-05 16:15:56 -0700683 socket[i].socket.features |= SS_CAP_PAGE_REGS | SS_CAP_PCCARD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 socket[i].socket.map_size = 0x1000;
685 socket[i].socket.irq_mask = mask;
686 socket[i].socket.pci_irq = dev->irq;
Dominik Brodowski7a96e872010-03-13 17:42:39 +0100687 socket[i].socket.cb_dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 socket[i].socket.owner = THIS_MODULE;
689
690 socket[i].number = i;
691
692 socket[i].socket.ops = &pd6729_operations;
693 socket[i].socket.resource_ops = &pccard_nonstatic_ops;
Greg Kroah-Hartman87373312006-09-12 17:00:10 +0200694 socket[i].socket.dev.parent = &dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 socket[i].socket.driver_data = &socket[i];
696 }
697
698 pci_set_drvdata(dev, socket);
699 if (irq_mode == 1) {
700 /* Register the interrupt handler */
Komuro5cbb2b92010-03-20 06:39:19 +0900701 ret = request_irq(dev->irq, pd6729_interrupt, IRQF_SHARED,
702 "pd6729", socket);
703 if (ret) {
Dominik Brodowskia7149f92009-10-24 18:07:16 +0200704 dev_err(&dev->dev, "Failed to register irq %d\n",
705 dev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 goto err_out_free_res;
707 }
708 } else {
709 /* poll Card status change */
710 init_timer(&socket->poll_timer);
711 socket->poll_timer.function = pd6729_interrupt_wrapper;
712 socket->poll_timer.data = (unsigned long)socket;
713 socket->poll_timer.expires = jiffies + HZ;
714 add_timer(&socket->poll_timer);
715 }
716
717 for (i = 0; i < MAX_SOCKETS; i++) {
718 ret = pcmcia_register_socket(&socket[i].socket);
719 if (ret) {
Dominik Brodowskia7149f92009-10-24 18:07:16 +0200720 dev_warn(&dev->dev, "pcmcia_register_socket failed.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 for (j = 0; j < i ; j++)
722 pcmcia_unregister_socket(&socket[j].socket);
723 goto err_out_free_res2;
724 }
725 }
726
727 return 0;
728
Komuro006839f2010-10-23 07:02:05 +0900729err_out_free_res2:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 if (irq_mode == 1)
731 free_irq(dev->irq, socket);
732 else
733 del_timer_sync(&socket->poll_timer);
Komuro006839f2010-10-23 07:02:05 +0900734err_out_free_res:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 pci_release_regions(dev);
Komuro006839f2010-10-23 07:02:05 +0900736err_out_disable:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 pci_disable_device(dev);
738
Komuro006839f2010-10-23 07:02:05 +0900739err_out_free_mem:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 kfree(socket);
741 return ret;
742}
743
Bill Pembertone765a022012-11-19 13:26:05 -0500744static void pd6729_pci_remove(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745{
746 int i;
747 struct pd6729_socket *socket = pci_get_drvdata(dev);
748
749 for (i = 0; i < MAX_SOCKETS; i++) {
750 /* Turn off all interrupt sources */
751 indirect_write(&socket[i], I365_CSCINT, 0);
752 indirect_write(&socket[i], I365_INTCTL, 0);
753
754 pcmcia_unregister_socket(&socket[i].socket);
755 }
756
757 if (irq_mode == 1)
758 free_irq(dev->irq, socket);
759 else
760 del_timer_sync(&socket->poll_timer);
761 pci_release_regions(dev);
762 pci_disable_device(dev);
763
764 kfree(socket);
765}
766
Axel Lin2b2c5d82011-12-27 16:17:46 +0800767static DEFINE_PCI_DEVICE_TABLE(pd6729_pci_ids) = {
768 { PCI_DEVICE(PCI_VENDOR_ID_CIRRUS, PCI_DEVICE_ID_CIRRUS_6729) },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 { }
770};
771MODULE_DEVICE_TABLE(pci, pd6729_pci_ids);
772
Sam Ravnborgba66ddf2008-05-01 04:34:51 -0700773static struct pci_driver pd6729_pci_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 .name = "pd6729",
775 .id_table = pd6729_pci_ids,
776 .probe = pd6729_pci_probe,
Bill Pemberton96364e32012-11-19 13:20:38 -0500777 .remove = pd6729_pci_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778};
779
780static int pd6729_module_init(void)
781{
Sam Ravnborgba66ddf2008-05-01 04:34:51 -0700782 return pci_register_driver(&pd6729_pci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783}
784
785static void pd6729_module_exit(void)
786{
Sam Ravnborgba66ddf2008-05-01 04:34:51 -0700787 pci_unregister_driver(&pd6729_pci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788}
789
790module_init(pd6729_module_init);
791module_exit(pd6729_module_exit);