2 * Regular cardbus driver ("yenta_socket")
4 * (C) Copyright 1999, 2000 Linus Torvalds
7 * Aug 2002: Manfred Spraul <manfred@colorfullife.com>
8 * Dynamically adjust the size of the bridge resource
10 * May 2003: Dominik Brodowski <linux@brodo.de>
11 * Merge pci_socket.c and yenta.c into one file
13 #include <linux/init.h>
14 #include <linux/pci.h>
15 #include <linux/sched.h>
16 #include <linux/workqueue.h>
17 #include <linux/interrupt.h>
18 #include <linux/delay.h>
19 #include <linux/module.h>
21 #include <pcmcia/cs_types.h>
22 #include <pcmcia/ss.h>
23 #include <pcmcia/cs.h>
27 #include "yenta_socket.h"
30 static int disable_clkrun;
31 module_param(disable_clkrun, bool, 0444);
32 MODULE_PARM_DESC(disable_clkrun, "If PC card doesn't function properly, please try this option");
34 static int isa_probe = 1;
35 module_param(isa_probe, bool, 0444);
36 MODULE_PARM_DESC(isa_probe, "If set ISA interrupts are probed (default). Set to N to disable probing");
38 static int pwr_irqs_off;
39 module_param(pwr_irqs_off, bool, 0644);
40 MODULE_PARM_DESC(pwr_irqs_off, "Force IRQs off during power-on of slot. Use only when seeing IRQ storms!");
43 #define debug(x,args...) printk(KERN_DEBUG "%s: " x, __func__ , ##args)
45 #define debug(x,args...)
49 #define to_cycles(ns) ((ns)/120)
50 #define to_ns(cycles) ((cycles)*120)
52 static int yenta_probe_cb_irq(struct yenta_socket *socket);
55 static unsigned int override_bios;
56 module_param(override_bios, uint, 0000);
57 MODULE_PARM_DESC (override_bios, "yenta ignore bios resource allocation");
60 * Generate easy-to-use ways of reading a cardbus sockets
61 * regular memory space ("cb_xxx"), configuration space
62 * ("config_xxx") and compatibility space ("exca_xxxx")
64 static inline u32 cb_readl(struct yenta_socket *socket, unsigned reg)
66 u32 val = readl(socket->base + reg);
67 debug("%p %04x %08x\n", socket, reg, val);
71 static inline void cb_writel(struct yenta_socket *socket, unsigned reg, u32 val)
73 debug("%p %04x %08x\n", socket, reg, val);
74 writel(val, socket->base + reg);
75 readl(socket->base + reg); /* avoid problems with PCI write posting */
78 static inline u8 config_readb(struct yenta_socket *socket, unsigned offset)
81 pci_read_config_byte(socket->dev, offset, &val);
82 debug("%p %04x %02x\n", socket, offset, val);
86 static inline u16 config_readw(struct yenta_socket *socket, unsigned offset)
89 pci_read_config_word(socket->dev, offset, &val);
90 debug("%p %04x %04x\n", socket, offset, val);
94 static inline u32 config_readl(struct yenta_socket *socket, unsigned offset)
97 pci_read_config_dword(socket->dev, offset, &val);
98 debug("%p %04x %08x\n", socket, offset, val);
102 static inline void config_writeb(struct yenta_socket *socket, unsigned offset, u8 val)
104 debug("%p %04x %02x\n", socket, offset, val);
105 pci_write_config_byte(socket->dev, offset, val);
108 static inline void config_writew(struct yenta_socket *socket, unsigned offset, u16 val)
110 debug("%p %04x %04x\n", socket, offset, val);
111 pci_write_config_word(socket->dev, offset, val);
114 static inline void config_writel(struct yenta_socket *socket, unsigned offset, u32 val)
116 debug("%p %04x %08x\n", socket, offset, val);
117 pci_write_config_dword(socket->dev, offset, val);
120 static inline u8 exca_readb(struct yenta_socket *socket, unsigned reg)
122 u8 val = readb(socket->base + 0x800 + reg);
123 debug("%p %04x %02x\n", socket, reg, val);
127 static inline u8 exca_readw(struct yenta_socket *socket, unsigned reg)
130 val = readb(socket->base + 0x800 + reg);
131 val |= readb(socket->base + 0x800 + reg + 1) << 8;
132 debug("%p %04x %04x\n", socket, reg, val);
136 static inline void exca_writeb(struct yenta_socket *socket, unsigned reg, u8 val)
138 debug("%p %04x %02x\n", socket, reg, val);
139 writeb(val, socket->base + 0x800 + reg);
140 readb(socket->base + 0x800 + reg); /* PCI write posting... */
143 static void exca_writew(struct yenta_socket *socket, unsigned reg, u16 val)
145 debug("%p %04x %04x\n", socket, reg, val);
146 writeb(val, socket->base + 0x800 + reg);
147 writeb(val >> 8, socket->base + 0x800 + reg + 1);
149 /* PCI write posting... */
150 readb(socket->base + 0x800 + reg);
151 readb(socket->base + 0x800 + reg + 1);
155 * Ugh, mixed-mode cardbus and 16-bit pccard state: things depend
156 * on what kind of card is inserted..
158 static int yenta_get_status(struct pcmcia_socket *sock, unsigned int *value)
160 struct yenta_socket *socket = container_of(sock, struct yenta_socket, socket);
162 u32 state = cb_readl(socket, CB_SOCKET_STATE);
164 val = (state & CB_3VCARD) ? SS_3VCARD : 0;
165 val |= (state & CB_XVCARD) ? SS_XVCARD : 0;
166 val |= (state & (CB_5VCARD | CB_3VCARD | CB_XVCARD | CB_YVCARD)) ? 0 : SS_PENDING;
167 val |= (state & (CB_CDETECT1 | CB_CDETECT2)) ? SS_PENDING : 0;
170 if (state & CB_CBCARD) {
172 val |= (state & CB_CARDSTS) ? SS_STSCHG : 0;
173 val |= (state & (CB_CDETECT1 | CB_CDETECT2)) ? 0 : SS_DETECT;
174 val |= (state & CB_PWRCYCLE) ? SS_POWERON | SS_READY : 0;
175 } else if (state & CB_16BITCARD) {
176 u8 status = exca_readb(socket, I365_STATUS);
177 val |= ((status & I365_CS_DETECT) == I365_CS_DETECT) ? SS_DETECT : 0;
178 if (exca_readb(socket, I365_INTCTL) & I365_PC_IOCARD) {
179 val |= (status & I365_CS_STSCHG) ? 0 : SS_STSCHG;
181 val |= (status & I365_CS_BVD1) ? 0 : SS_BATDEAD;
182 val |= (status & I365_CS_BVD2) ? 0 : SS_BATWARN;
184 val |= (status & I365_CS_WRPROT) ? SS_WRPROT : 0;
185 val |= (status & I365_CS_READY) ? SS_READY : 0;
186 val |= (status & I365_CS_POWERON) ? SS_POWERON : 0;
193 static void yenta_get_power(struct yenta_socket *socket, socket_state_t *state)
195 if (!(cb_readl(socket, CB_SOCKET_STATE) & CB_CBCARD) &&
196 (socket->flags & YENTA_16BIT_POWER_EXCA)) {
199 reg = exca_readb(socket, I365_POWER);
200 vcc = reg & I365_VCC_MASK;
201 vpp = reg & I365_VPP1_MASK;
202 state->Vcc = state->Vpp = 0;
204 if (socket->flags & YENTA_16BIT_POWER_DF) {
205 if (vcc == I365_VCC_3V)
207 if (vcc == I365_VCC_5V)
209 if (vpp == I365_VPP1_5V)
210 state->Vpp = state->Vcc;
211 if (vpp == I365_VPP1_12V)
214 if (reg & I365_VCC_5V) {
216 if (vpp == I365_VPP1_5V)
218 if (vpp == I365_VPP1_12V)
225 control = cb_readl(socket, CB_SOCKET_CONTROL);
227 switch (control & CB_SC_VCC_MASK) {
228 case CB_SC_VCC_5V: state->Vcc = 50; break;
229 case CB_SC_VCC_3V: state->Vcc = 33; break;
230 default: state->Vcc = 0;
233 switch (control & CB_SC_VPP_MASK) {
234 case CB_SC_VPP_12V: state->Vpp = 120; break;
235 case CB_SC_VPP_5V: state->Vpp = 50; break;
236 case CB_SC_VPP_3V: state->Vpp = 33; break;
237 default: state->Vpp = 0;
242 static int yenta_get_socket(struct pcmcia_socket *sock, socket_state_t *state)
244 struct yenta_socket *socket = container_of(sock, struct yenta_socket, socket);
248 control = cb_readl(socket, CB_SOCKET_CONTROL);
250 yenta_get_power(socket, state);
251 state->io_irq = socket->io_irq;
253 if (cb_readl(socket, CB_SOCKET_STATE) & CB_CBCARD) {
254 u16 bridge = config_readw(socket, CB_BRIDGE_CONTROL);
255 if (bridge & CB_BRIDGE_CRST)
256 state->flags |= SS_RESET;
260 /* 16-bit card state.. */
261 reg = exca_readb(socket, I365_POWER);
262 state->flags = (reg & I365_PWR_AUTO) ? SS_PWR_AUTO : 0;
263 state->flags |= (reg & I365_PWR_OUT) ? SS_OUTPUT_ENA : 0;
265 reg = exca_readb(socket, I365_INTCTL);
266 state->flags |= (reg & I365_PC_RESET) ? 0 : SS_RESET;
267 state->flags |= (reg & I365_PC_IOCARD) ? SS_IOCARD : 0;
269 reg = exca_readb(socket, I365_CSCINT);
270 state->csc_mask = (reg & I365_CSC_DETECT) ? SS_DETECT : 0;
271 if (state->flags & SS_IOCARD) {
272 state->csc_mask |= (reg & I365_CSC_STSCHG) ? SS_STSCHG : 0;
274 state->csc_mask |= (reg & I365_CSC_BVD1) ? SS_BATDEAD : 0;
275 state->csc_mask |= (reg & I365_CSC_BVD2) ? SS_BATWARN : 0;
276 state->csc_mask |= (reg & I365_CSC_READY) ? SS_READY : 0;
282 static void yenta_set_power(struct yenta_socket *socket, socket_state_t *state)
284 /* some birdges require to use the ExCA registers to power 16bit cards */
285 if (!(cb_readl(socket, CB_SOCKET_STATE) & CB_CBCARD) &&
286 (socket->flags & YENTA_16BIT_POWER_EXCA)) {
288 reg = old = exca_readb(socket, I365_POWER);
289 reg &= ~(I365_VCC_MASK | I365_VPP1_MASK | I365_VPP2_MASK);
291 /* i82365SL-DF style */
292 if (socket->flags & YENTA_16BIT_POWER_DF) {
293 switch (state->Vcc) {
294 case 33: reg |= I365_VCC_3V; break;
295 case 50: reg |= I365_VCC_5V; break;
296 default: reg = 0; break;
298 switch (state->Vpp) {
300 case 50: reg |= I365_VPP1_5V; break;
301 case 120: reg |= I365_VPP1_12V; break;
304 /* i82365SL-B style */
305 switch (state->Vcc) {
306 case 50: reg |= I365_VCC_5V; break;
307 default: reg = 0; break;
309 switch (state->Vpp) {
310 case 50: reg |= I365_VPP1_5V | I365_VPP2_5V; break;
311 case 120: reg |= I365_VPP1_12V | I365_VPP2_12V; break;
316 exca_writeb(socket, I365_POWER, reg);
318 u32 reg = 0; /* CB_SC_STPCLK? */
319 switch (state->Vcc) {
320 case 33: reg = CB_SC_VCC_3V; break;
321 case 50: reg = CB_SC_VCC_5V; break;
322 default: reg = 0; break;
324 switch (state->Vpp) {
325 case 33: reg |= CB_SC_VPP_3V; break;
326 case 50: reg |= CB_SC_VPP_5V; break;
327 case 120: reg |= CB_SC_VPP_12V; break;
329 if (reg != cb_readl(socket, CB_SOCKET_CONTROL))
330 cb_writel(socket, CB_SOCKET_CONTROL, reg);
334 static int yenta_set_socket(struct pcmcia_socket *sock, socket_state_t *state)
336 struct yenta_socket *socket = container_of(sock, struct yenta_socket, socket);
339 yenta_set_power(socket, state);
340 socket->io_irq = state->io_irq;
341 bridge = config_readw(socket, CB_BRIDGE_CONTROL) & ~(CB_BRIDGE_CRST | CB_BRIDGE_INTR);
342 if (cb_readl(socket, CB_SOCKET_STATE) & CB_CBCARD) {
344 bridge |= (state->flags & SS_RESET) ? CB_BRIDGE_CRST : 0;
346 /* ISA interrupt control? */
347 intr = exca_readb(socket, I365_INTCTL);
348 intr = (intr & ~0xf);
349 if (!socket->cb_irq) {
350 intr |= state->io_irq;
351 bridge |= CB_BRIDGE_INTR;
353 exca_writeb(socket, I365_INTCTL, intr);
357 reg = exca_readb(socket, I365_INTCTL) & (I365_RING_ENA | I365_INTR_ENA);
358 reg |= (state->flags & SS_RESET) ? 0 : I365_PC_RESET;
359 reg |= (state->flags & SS_IOCARD) ? I365_PC_IOCARD : 0;
360 if (state->io_irq != socket->cb_irq) {
361 reg |= state->io_irq;
362 bridge |= CB_BRIDGE_INTR;
364 exca_writeb(socket, I365_INTCTL, reg);
366 reg = exca_readb(socket, I365_POWER) & (I365_VCC_MASK|I365_VPP1_MASK);
367 reg |= I365_PWR_NORESET;
368 if (state->flags & SS_PWR_AUTO) reg |= I365_PWR_AUTO;
369 if (state->flags & SS_OUTPUT_ENA) reg |= I365_PWR_OUT;
370 if (exca_readb(socket, I365_POWER) != reg)
371 exca_writeb(socket, I365_POWER, reg);
373 /* CSC interrupt: no ISA irq for CSC */
374 reg = I365_CSC_DETECT;
375 if (state->flags & SS_IOCARD) {
376 if (state->csc_mask & SS_STSCHG) reg |= I365_CSC_STSCHG;
378 if (state->csc_mask & SS_BATDEAD) reg |= I365_CSC_BVD1;
379 if (state->csc_mask & SS_BATWARN) reg |= I365_CSC_BVD2;
380 if (state->csc_mask & SS_READY) reg |= I365_CSC_READY;
382 exca_writeb(socket, I365_CSCINT, reg);
383 exca_readb(socket, I365_CSC);
385 sock->zoom_video(sock, state->flags & SS_ZVCARD);
387 config_writew(socket, CB_BRIDGE_CONTROL, bridge);
388 /* Socket event mask: get card insert/remove events.. */
389 cb_writel(socket, CB_SOCKET_EVENT, -1);
390 cb_writel(socket, CB_SOCKET_MASK, CB_CDMASK);
394 static int yenta_set_io_map(struct pcmcia_socket *sock, struct pccard_io_map *io)
396 struct yenta_socket *socket = container_of(sock, struct yenta_socket, socket);
398 unsigned char ioctl, addr, enable;
405 enable = I365_ENA_IO(map);
406 addr = exca_readb(socket, I365_ADDRWIN);
408 /* Disable the window before changing it.. */
411 exca_writeb(socket, I365_ADDRWIN, addr);
414 exca_writew(socket, I365_IO(map)+I365_W_START, io->start);
415 exca_writew(socket, I365_IO(map)+I365_W_STOP, io->stop);
417 ioctl = exca_readb(socket, I365_IOCTL) & ~I365_IOCTL_MASK(map);
418 if (io->flags & MAP_0WS) ioctl |= I365_IOCTL_0WS(map);
419 if (io->flags & MAP_16BIT) ioctl |= I365_IOCTL_16BIT(map);
420 if (io->flags & MAP_AUTOSZ) ioctl |= I365_IOCTL_IOCS16(map);
421 exca_writeb(socket, I365_IOCTL, ioctl);
423 if (io->flags & MAP_ACTIVE)
424 exca_writeb(socket, I365_ADDRWIN, addr | enable);
428 static int yenta_set_mem_map(struct pcmcia_socket *sock, struct pccard_mem_map *mem)
430 struct yenta_socket *socket = container_of(sock, struct yenta_socket, socket);
431 struct pci_bus_region region;
433 unsigned char addr, enable;
434 unsigned int start, stop, card_start;
437 pcibios_resource_to_bus(socket->dev, ®ion, mem->res);
440 start = region.start;
442 card_start = mem->card_start;
444 if (map > 4 || start > stop || ((start ^ stop) >> 24) ||
445 (card_start >> 26) || mem->speed > 1000)
448 enable = I365_ENA_MEM(map);
449 addr = exca_readb(socket, I365_ADDRWIN);
452 exca_writeb(socket, I365_ADDRWIN, addr);
455 exca_writeb(socket, CB_MEM_PAGE(map), start >> 24);
457 word = (start >> 12) & 0x0fff;
458 if (mem->flags & MAP_16BIT)
459 word |= I365_MEM_16BIT;
460 if (mem->flags & MAP_0WS)
461 word |= I365_MEM_0WS;
462 exca_writew(socket, I365_MEM(map) + I365_W_START, word);
464 word = (stop >> 12) & 0x0fff;
465 switch (to_cycles(mem->speed)) {
467 case 1: word |= I365_MEM_WS0; break;
468 case 2: word |= I365_MEM_WS1; break;
469 default: word |= I365_MEM_WS1 | I365_MEM_WS0; break;
471 exca_writew(socket, I365_MEM(map) + I365_W_STOP, word);
473 word = ((card_start - start) >> 12) & 0x3fff;
474 if (mem->flags & MAP_WRPROT)
475 word |= I365_MEM_WRPROT;
476 if (mem->flags & MAP_ATTRIB)
477 word |= I365_MEM_REG;
478 exca_writew(socket, I365_MEM(map) + I365_W_OFF, word);
480 if (mem->flags & MAP_ACTIVE)
481 exca_writeb(socket, I365_ADDRWIN, addr | enable);
487 static irqreturn_t yenta_interrupt(int irq, void *dev_id, struct pt_regs *regs)
490 struct yenta_socket *socket = (struct yenta_socket *) dev_id;
494 /* Clear interrupt status for the event */
495 cb_event = cb_readl(socket, CB_SOCKET_EVENT);
496 cb_writel(socket, CB_SOCKET_EVENT, cb_event);
498 csc = exca_readb(socket, I365_CSC);
500 events = (cb_event & (CB_CD1EVENT | CB_CD2EVENT)) ? SS_DETECT : 0 ;
501 events |= (csc & I365_CSC_DETECT) ? SS_DETECT : 0;
502 if (exca_readb(socket, I365_INTCTL) & I365_PC_IOCARD) {
503 events |= (csc & I365_CSC_STSCHG) ? SS_STSCHG : 0;
505 events |= (csc & I365_CSC_BVD1) ? SS_BATDEAD : 0;
506 events |= (csc & I365_CSC_BVD2) ? SS_BATWARN : 0;
507 events |= (csc & I365_CSC_READY) ? SS_READY : 0;
511 pcmcia_parse_events(&socket->socket, events);
519 static void yenta_interrupt_wrapper(unsigned long data)
521 struct yenta_socket *socket = (struct yenta_socket *) data;
523 yenta_interrupt(0, (void *)socket, NULL);
524 socket->poll_timer.expires = jiffies + HZ;
525 add_timer(&socket->poll_timer);
528 static void yenta_clear_maps(struct yenta_socket *socket)
531 struct resource res = { .start = 0, .end = 0x0fff };
532 pccard_io_map io = { 0, 0, 0, 0, 1 };
533 pccard_mem_map mem = { .res = &res, };
535 yenta_set_socket(&socket->socket, &dead_socket);
536 for (i = 0; i < 2; i++) {
538 yenta_set_io_map(&socket->socket, &io);
540 for (i = 0; i < 5; i++) {
542 yenta_set_mem_map(&socket->socket, &mem);
546 /* redoes voltage interrogation if required */
547 static void yenta_interrogate(struct yenta_socket *socket)
551 state = cb_readl(socket, CB_SOCKET_STATE);
552 if (!(state & (CB_5VCARD | CB_3VCARD | CB_XVCARD | CB_YVCARD)) ||
553 (state & (CB_CDETECT1 | CB_CDETECT2 | CB_NOTACARD | CB_BADVCCREQ)) ||
554 ((state & (CB_16BITCARD | CB_CBCARD)) == (CB_16BITCARD | CB_CBCARD)))
555 cb_writel(socket, CB_SOCKET_FORCE, CB_CVSTEST);
558 /* Called at resume and initialization events */
559 static int yenta_sock_init(struct pcmcia_socket *sock)
561 struct yenta_socket *socket = container_of(sock, struct yenta_socket, socket);
563 exca_writeb(socket, I365_GBLCTL, 0x00);
564 exca_writeb(socket, I365_GENCTL, 0x00);
566 /* Redo card voltage interrogation */
567 yenta_interrogate(socket);
569 yenta_clear_maps(socket);
571 if (socket->type && socket->type->sock_init)
572 socket->type->sock_init(socket);
574 /* Re-enable CSC interrupts */
575 cb_writel(socket, CB_SOCKET_MASK, CB_CDMASK);
580 static int yenta_sock_suspend(struct pcmcia_socket *sock)
582 struct yenta_socket *socket = container_of(sock, struct yenta_socket, socket);
584 /* Disable CSC interrupts */
585 cb_writel(socket, CB_SOCKET_MASK, 0x0);
591 * Use an adaptive allocation for the memory resource,
592 * sometimes the memory behind pci bridges is limited:
593 * 1/8 of the size of the io window of the parent.
594 * max 4 MB, min 16 kB. We try very hard to not get below
595 * the "ACC" values, though.
597 #define BRIDGE_MEM_MAX 4*1024*1024
598 #define BRIDGE_MEM_ACC 128*1024
599 #define BRIDGE_MEM_MIN 16*1024
601 #define BRIDGE_IO_MAX 512
602 #define BRIDGE_IO_ACC 256
603 #define BRIDGE_IO_MIN 32
605 #ifndef PCIBIOS_MIN_CARDBUS_IO
606 #define PCIBIOS_MIN_CARDBUS_IO PCIBIOS_MIN_IO
609 static int yenta_search_one_res(struct resource *root, struct resource *res,
612 u32 align, size, start, end;
614 if (res->flags & IORESOURCE_IO) {
616 size = BRIDGE_IO_MAX;
617 start = PCIBIOS_MIN_CARDBUS_IO;
620 unsigned long avail = root->end - root->start;
622 size = BRIDGE_MEM_MAX;
623 if (size > avail/8) {
625 /* round size down to next power of 2 */
627 while ((size /= 2) != 0)
634 start = PCIBIOS_MIN_MEM;
639 if (allocate_resource(root, res, size, start, end, align,
645 } while (size >= min);
651 static int yenta_search_res(struct yenta_socket *socket, struct resource *res,
655 for (i=0; i<PCI_BUS_NUM_RESOURCES; i++) {
656 struct resource * root = socket->dev->bus->resource[i];
660 if ((res->flags ^ root->flags) &
661 (IORESOURCE_IO | IORESOURCE_MEM | IORESOURCE_PREFETCH))
662 continue; /* Wrong type */
664 if (yenta_search_one_res(root, res, min))
670 static int yenta_allocate_res(struct yenta_socket *socket, int nr, unsigned type, int addr_start, int addr_end)
672 struct resource *root, *res;
673 struct pci_bus_region region;
676 res = socket->dev->resource + PCI_BRIDGE_RESOURCES + nr;
677 /* Already allocated? */
681 /* The granularity of the memory limit is 4kB, on IO it's 4 bytes */
683 if (type & IORESOURCE_IO)
686 res->name = socket->dev->subordinate->name;
689 region.start = config_readl(socket, addr_start) & mask;
690 region.end = config_readl(socket, addr_end) | ~mask;
691 if (region.start && region.end > region.start && !override_bios) {
692 pcibios_bus_to_resource(socket->dev, res, ®ion);
693 root = pci_find_parent_resource(socket->dev, res);
694 if (root && (request_resource(root, res) == 0))
696 printk(KERN_INFO "yenta %s: Preassigned resource %d busy or not available, reconfiguring...\n",
697 pci_name(socket->dev), nr);
700 if (type & IORESOURCE_IO) {
701 if ((yenta_search_res(socket, res, BRIDGE_IO_MAX)) ||
702 (yenta_search_res(socket, res, BRIDGE_IO_ACC)) ||
703 (yenta_search_res(socket, res, BRIDGE_IO_MIN)))
706 if (type & IORESOURCE_PREFETCH) {
707 if ((yenta_search_res(socket, res, BRIDGE_MEM_MAX)) ||
708 (yenta_search_res(socket, res, BRIDGE_MEM_ACC)) ||
709 (yenta_search_res(socket, res, BRIDGE_MEM_MIN)))
711 /* Approximating prefetchable by non-prefetchable */
712 res->flags = IORESOURCE_MEM;
714 if ((yenta_search_res(socket, res, BRIDGE_MEM_MAX)) ||
715 (yenta_search_res(socket, res, BRIDGE_MEM_ACC)) ||
716 (yenta_search_res(socket, res, BRIDGE_MEM_MIN)))
720 printk(KERN_INFO "yenta %s: no resource of type %x available, trying to continue...\n",
721 pci_name(socket->dev), type);
722 res->start = res->end = res->flags = 0;
727 * Allocate the bridge mappings for the device..
729 static void yenta_allocate_resources(struct yenta_socket *socket)
732 program += yenta_allocate_res(socket, 0, IORESOURCE_IO,
733 PCI_CB_IO_BASE_0, PCI_CB_IO_LIMIT_0);
734 program += yenta_allocate_res(socket, 1, IORESOURCE_IO,
735 PCI_CB_IO_BASE_1, PCI_CB_IO_LIMIT_1);
736 program += yenta_allocate_res(socket, 2, IORESOURCE_MEM|IORESOURCE_PREFETCH,
737 PCI_CB_MEMORY_BASE_0, PCI_CB_MEMORY_LIMIT_0);
738 program += yenta_allocate_res(socket, 3, IORESOURCE_MEM,
739 PCI_CB_MEMORY_BASE_1, PCI_CB_MEMORY_LIMIT_1);
741 pci_setup_cardbus(socket->dev->subordinate);
746 * Free the bridge mappings for the device..
748 static void yenta_free_resources(struct yenta_socket *socket)
752 struct resource *res;
753 res = socket->dev->resource + PCI_BRIDGE_RESOURCES + i;
754 if (res->start != 0 && res->end != 0)
755 release_resource(res);
756 res->start = res->end = res->flags = 0;
762 * Close it down - release our resources and go home..
764 static void yenta_close(struct pci_dev *dev)
766 struct yenta_socket *sock = pci_get_drvdata(dev);
768 /* we don't want a dying socket registered */
769 pcmcia_unregister_socket(&sock->socket);
771 /* Disable all events so we don't die in an IRQ storm */
772 cb_writel(sock, CB_SOCKET_MASK, 0x0);
773 exca_writeb(sock, I365_CSCINT, 0);
776 free_irq(sock->cb_irq, sock);
778 del_timer_sync(&sock->poll_timer);
782 yenta_free_resources(sock);
784 pci_release_regions(dev);
785 pci_disable_device(dev);
786 pci_set_drvdata(dev, NULL);
790 static struct pccard_operations yenta_socket_operations = {
791 .init = yenta_sock_init,
792 .suspend = yenta_sock_suspend,
793 .get_status = yenta_get_status,
794 .get_socket = yenta_get_socket,
795 .set_socket = yenta_set_socket,
796 .set_io_map = yenta_set_io_map,
797 .set_mem_map = yenta_set_mem_map,
807 CARDBUS_TYPE_DEFAULT = -1,
813 CARDBUS_TYPE_TOPIC95,
814 CARDBUS_TYPE_TOPIC97,
815 CARDBUS_TYPE_O2MICRO,
820 * Different cardbus controllers have slightly different
821 * initialization sequences etc details. List them here..
823 static struct cardbus_type cardbus_type[] = {
824 [CARDBUS_TYPE_TI] = {
825 .override = ti_override,
826 .save_state = ti_save_state,
827 .restore_state = ti_restore_state,
828 .sock_init = ti_init,
830 [CARDBUS_TYPE_TI113X] = {
831 .override = ti113x_override,
832 .save_state = ti_save_state,
833 .restore_state = ti_restore_state,
834 .sock_init = ti_init,
836 [CARDBUS_TYPE_TI12XX] = {
837 .override = ti12xx_override,
838 .save_state = ti_save_state,
839 .restore_state = ti_restore_state,
840 .sock_init = ti_init,
842 [CARDBUS_TYPE_TI1250] = {
843 .override = ti1250_override,
844 .save_state = ti_save_state,
845 .restore_state = ti_restore_state,
846 .sock_init = ti_init,
848 [CARDBUS_TYPE_RICOH] = {
849 .override = ricoh_override,
850 .save_state = ricoh_save_state,
851 .restore_state = ricoh_restore_state,
853 [CARDBUS_TYPE_TOPIC95] = {
854 .override = topic95_override,
856 [CARDBUS_TYPE_TOPIC97] = {
857 .override = topic97_override,
859 [CARDBUS_TYPE_O2MICRO] = {
860 .override = o2micro_override,
861 .restore_state = o2micro_restore_state,
863 [CARDBUS_TYPE_ENE] = {
864 .override = ene_override,
865 .save_state = ti_save_state,
866 .restore_state = ti_restore_state,
867 .sock_init = ti_init,
873 * Only probe "regular" interrupts, don't
874 * touch dangerous spots like the mouse irq,
875 * because there are mice that apparently
876 * get really confused if they get fondled
879 * Default to 11, 10, 9, 7, 6, 5, 4, 3.
881 static u32 isa_interrupts = 0x0ef8;
883 static unsigned int yenta_probe_irq(struct yenta_socket *socket, u32 isa_irq_mask)
890 * Probe for usable interrupts using the force
891 * register to generate bogus card status events.
893 cb_writel(socket, CB_SOCKET_EVENT, -1);
894 cb_writel(socket, CB_SOCKET_MASK, CB_CSTSMASK);
895 exca_writeb(socket, I365_CSCINT, 0);
896 val = probe_irq_on() & isa_irq_mask;
897 for (i = 1; i < 16; i++) {
898 if (!((val >> i) & 1))
900 exca_writeb(socket, I365_CSCINT, I365_CSC_STSCHG | (i << 4));
901 cb_writel(socket, CB_SOCKET_FORCE, CB_FCARDSTS);
903 cb_writel(socket, CB_SOCKET_EVENT, -1);
905 cb_writel(socket, CB_SOCKET_MASK, 0);
906 exca_writeb(socket, I365_CSCINT, 0);
908 mask = probe_irq_mask(val) & 0xffff;
914 /* interrupt handler, only used during probing */
915 static irqreturn_t yenta_probe_handler(int irq, void *dev_id, struct pt_regs *regs)
917 struct yenta_socket *socket = (struct yenta_socket *) dev_id;
921 /* Clear interrupt status for the event */
922 cb_event = cb_readl(socket, CB_SOCKET_EVENT);
923 cb_writel(socket, CB_SOCKET_EVENT, -1);
924 csc = exca_readb(socket, I365_CSC);
926 if (cb_event || csc) {
927 socket->probe_status = 1;
934 /* probes the PCI interrupt, use only on override functions */
935 static int yenta_probe_cb_irq(struct yenta_socket *socket)
940 socket->probe_status = 0;
942 if (request_irq(socket->cb_irq, yenta_probe_handler, SA_SHIRQ, "yenta", socket)) {
943 printk(KERN_WARNING "Yenta: request_irq() in yenta_probe_cb_irq() failed!\n");
947 /* generate interrupt, wait */
948 exca_writeb(socket, I365_CSCINT, I365_CSC_STSCHG);
949 cb_writel(socket, CB_SOCKET_EVENT, -1);
950 cb_writel(socket, CB_SOCKET_MASK, CB_CSTSMASK);
951 cb_writel(socket, CB_SOCKET_FORCE, CB_FCARDSTS);
955 /* disable interrupts */
956 cb_writel(socket, CB_SOCKET_MASK, 0);
957 exca_writeb(socket, I365_CSCINT, 0);
958 cb_writel(socket, CB_SOCKET_EVENT, -1);
959 exca_readb(socket, I365_CSC);
961 free_irq(socket->cb_irq, socket);
963 return (int) socket->probe_status;
969 * Set static data that doesn't need re-initializing..
971 static void yenta_get_socket_capabilities(struct yenta_socket *socket, u32 isa_irq_mask)
973 socket->socket.pci_irq = socket->cb_irq;
975 socket->socket.irq_mask = yenta_probe_irq(socket, isa_irq_mask);
977 socket->socket.irq_mask = 0;
979 printk(KERN_INFO "Yenta: ISA IRQ mask 0x%04x, PCI irq %d\n",
980 socket->socket.irq_mask, socket->cb_irq);
984 * Initialize the standard cardbus registers
986 static void yenta_config_init(struct yenta_socket *socket)
989 struct pci_dev *dev = socket->dev;
990 struct pci_bus_region region;
992 pcibios_resource_to_bus(socket->dev, ®ion, &dev->resource[0]);
994 config_writel(socket, CB_LEGACY_MODE_BASE, 0);
995 config_writel(socket, PCI_BASE_ADDRESS_0, region.start);
996 config_writew(socket, PCI_COMMAND,
1002 /* MAGIC NUMBERS! Fixme */
1003 config_writeb(socket, PCI_CACHE_LINE_SIZE, L1_CACHE_BYTES / 4);
1004 config_writeb(socket, PCI_LATENCY_TIMER, 168);
1005 config_writel(socket, PCI_PRIMARY_BUS,
1006 (176 << 24) | /* sec. latency timer */
1007 (dev->subordinate->subordinate << 16) | /* subordinate bus */
1008 (dev->subordinate->secondary << 8) | /* secondary bus */
1009 dev->subordinate->primary); /* primary bus */
1012 * Set up the bridging state:
1013 * - enable write posting.
1014 * - memory window 0 prefetchable, window 1 non-prefetchable
1015 * - PCI interrupts enabled if a PCI interrupt exists..
1017 bridge = config_readw(socket, CB_BRIDGE_CONTROL);
1018 bridge &= ~(CB_BRIDGE_CRST | CB_BRIDGE_PREFETCH1 | CB_BRIDGE_ISAEN | CB_BRIDGE_VGAEN);
1019 bridge |= CB_BRIDGE_PREFETCH0 | CB_BRIDGE_POSTEN;
1020 config_writew(socket, CB_BRIDGE_CONTROL, bridge);
1024 * Initialize a cardbus controller. Make sure we have a usable
1025 * interrupt, and that we can map the cardbus area. Fill in the
1026 * socket information structure..
1028 static int __devinit yenta_probe (struct pci_dev *dev, const struct pci_device_id *id)
1030 struct yenta_socket *socket;
1034 * If we failed to assign proper bus numbers for this cardbus
1035 * controller during PCI probe, its subordinate pci_bus is NULL.
1038 if (!dev->subordinate) {
1039 printk(KERN_ERR "Yenta: no bus associated with %s! "
1040 "(try 'pci=assign-busses')\n", pci_name(dev));
1044 socket = kmalloc(sizeof(struct yenta_socket), GFP_KERNEL);
1047 memset(socket, 0, sizeof(*socket));
1049 /* prepare pcmcia_socket */
1050 socket->socket.ops = ¥ta_socket_operations;
1051 socket->socket.resource_ops = &pccard_nonstatic_ops;
1052 socket->socket.dev.dev = &dev->dev;
1053 socket->socket.driver_data = socket;
1054 socket->socket.owner = THIS_MODULE;
1055 socket->socket.features = SS_CAP_PAGE_REGS | SS_CAP_PCCARD;
1056 socket->socket.map_size = 0x1000;
1057 socket->socket.cb_dev = dev;
1059 /* prepare struct yenta_socket */
1061 pci_set_drvdata(dev, socket);
1064 * Do some basic sanity checking..
1066 if (pci_enable_device(dev)) {
1071 ret = pci_request_regions(dev, "yenta_socket");
1075 if (!pci_resource_start(dev, 0)) {
1076 printk(KERN_ERR "No cardbus resource!\n");
1082 * Ok, start setup.. Map the cardbus registers,
1083 * and request the IRQ.
1085 socket->base = ioremap(pci_resource_start(dev, 0), 0x1000);
1086 if (!socket->base) {
1092 * report the subsystem vendor and device for help debugging
1095 printk(KERN_INFO "Yenta: CardBus bridge found at %s [%04x:%04x]\n",
1096 pci_name(dev), dev->subsystem_vendor, dev->subsystem_device);
1098 yenta_config_init(socket);
1100 /* Disable all events */
1101 cb_writel(socket, CB_SOCKET_MASK, 0x0);
1103 /* Set up the bridge regions.. */
1104 yenta_allocate_resources(socket);
1106 socket->cb_irq = dev->irq;
1108 /* Do we have special options for the device? */
1109 if (id->driver_data != CARDBUS_TYPE_DEFAULT &&
1110 id->driver_data < ARRAY_SIZE(cardbus_type)) {
1111 socket->type = &cardbus_type[id->driver_data];
1113 ret = socket->type->override(socket);
1118 /* We must finish initialization here */
1120 if (!socket->cb_irq || request_irq(socket->cb_irq, yenta_interrupt, SA_SHIRQ, "yenta", socket)) {
1121 /* No IRQ or request_irq failed. Poll */
1122 socket->cb_irq = 0; /* But zero is a valid IRQ number. */
1123 init_timer(&socket->poll_timer);
1124 socket->poll_timer.function = yenta_interrupt_wrapper;
1125 socket->poll_timer.data = (unsigned long)socket;
1126 socket->poll_timer.expires = jiffies + HZ;
1127 add_timer(&socket->poll_timer);
1128 printk(KERN_INFO "Yenta: no PCI IRQ, CardBus support disabled for this socket.\n"
1129 KERN_INFO "Yenta: check your BIOS CardBus, BIOS IRQ or ACPI settings.\n");
1131 socket->socket.features |= SS_CAP_CARDBUS;
1134 /* Figure out what the dang thing can do for the PCMCIA layer... */
1135 yenta_interrogate(socket);
1136 yenta_get_socket_capabilities(socket, isa_interrupts);
1137 printk(KERN_INFO "Socket status: %08x\n", cb_readl(socket, CB_SOCKET_STATE));
1139 /* Register it with the pcmcia layer.. */
1140 ret = pcmcia_register_socket(&socket->socket);
1145 iounmap(socket->base);
1147 pci_release_regions(dev);
1149 pci_disable_device(dev);
1157 static int yenta_dev_suspend (struct pci_dev *dev, pm_message_t state)
1159 struct yenta_socket *socket = pci_get_drvdata(dev);
1162 ret = pcmcia_socket_dev_suspend(&dev->dev, state);
1165 if (socket->type && socket->type->save_state)
1166 socket->type->save_state(socket);
1168 /* FIXME: pci_save_state needs to have a better interface */
1169 pci_save_state(dev);
1170 pci_read_config_dword(dev, 16*4, &socket->saved_state[0]);
1171 pci_read_config_dword(dev, 17*4, &socket->saved_state[1]);
1172 pci_disable_device(dev);
1175 * Some laptops (IBM T22) do not like us putting the Cardbus
1176 * bridge into D3. At a guess, some other laptop will
1177 * probably require this, so leave it commented out for now.
1179 /* pci_set_power_state(dev, 3); */
1186 static int yenta_dev_resume (struct pci_dev *dev)
1188 struct yenta_socket *socket = pci_get_drvdata(dev);
1191 pci_set_power_state(dev, 0);
1192 /* FIXME: pci_restore_state needs to have a better interface */
1193 pci_restore_state(dev);
1194 pci_write_config_dword(dev, 16*4, socket->saved_state[0]);
1195 pci_write_config_dword(dev, 17*4, socket->saved_state[1]);
1196 pci_enable_device(dev);
1197 pci_set_master(dev);
1199 if (socket->type && socket->type->restore_state)
1200 socket->type->restore_state(socket);
1203 return pcmcia_socket_dev_resume(&dev->dev);
1207 #define CB_ID(vend,dev,type) \
1211 .subvendor = PCI_ANY_ID, \
1212 .subdevice = PCI_ANY_ID, \
1213 .class = PCI_CLASS_BRIDGE_CARDBUS << 8, \
1215 .driver_data = CARDBUS_TYPE_##type, \
1218 static struct pci_device_id yenta_table [] = {
1219 CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_1031, TI),
1222 * TBD: Check if these TI variants can use more
1223 * advanced overrides instead. (I can't get the
1224 * data sheets for these devices. --rmk)
1226 CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_1210, TI),
1228 CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_1130, TI113X),
1229 CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_1131, TI113X),
1231 CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_1211, TI12XX),
1232 CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_1220, TI12XX),
1233 CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_1221, TI12XX),
1234 CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_1225, TI12XX),
1235 CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_1251A, TI12XX),
1236 CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_1251B, TI12XX),
1237 CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_1420, TI12XX),
1238 CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_1450, TI12XX),
1239 CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_1451A, TI12XX),
1240 CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_1510, TI12XX),
1241 CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_1520, TI12XX),
1242 CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_1620, TI12XX),
1243 CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_4410, TI12XX),
1244 CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_4450, TI12XX),
1245 CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_4451, TI12XX),
1246 CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_4510, TI12XX),
1247 CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_4520, TI12XX),
1249 CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_1250, TI1250),
1250 CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_1410, TI1250),
1252 CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_XX21_XX11, TI12XX),
1253 CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_X515, TI12XX),
1254 CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_X420, TI12XX),
1255 CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_X620, TI12XX),
1256 CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_7410, TI12XX),
1257 CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_7510, TI12XX),
1258 CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_7610, TI12XX),
1260 CB_ID(PCI_VENDOR_ID_ENE, PCI_DEVICE_ID_ENE_710, TI12XX),
1261 CB_ID(PCI_VENDOR_ID_ENE, PCI_DEVICE_ID_ENE_712, TI12XX),
1262 CB_ID(PCI_VENDOR_ID_ENE, PCI_DEVICE_ID_ENE_720, TI12XX),
1263 CB_ID(PCI_VENDOR_ID_ENE, PCI_DEVICE_ID_ENE_722, TI12XX),
1264 CB_ID(PCI_VENDOR_ID_ENE, PCI_DEVICE_ID_ENE_1211, ENE),
1265 CB_ID(PCI_VENDOR_ID_ENE, PCI_DEVICE_ID_ENE_1225, ENE),
1266 CB_ID(PCI_VENDOR_ID_ENE, PCI_DEVICE_ID_ENE_1410, ENE),
1267 CB_ID(PCI_VENDOR_ID_ENE, PCI_DEVICE_ID_ENE_1420, ENE),
1269 CB_ID(PCI_VENDOR_ID_RICOH, PCI_DEVICE_ID_RICOH_RL5C465, RICOH),
1270 CB_ID(PCI_VENDOR_ID_RICOH, PCI_DEVICE_ID_RICOH_RL5C466, RICOH),
1271 CB_ID(PCI_VENDOR_ID_RICOH, PCI_DEVICE_ID_RICOH_RL5C475, RICOH),
1272 CB_ID(PCI_VENDOR_ID_RICOH, PCI_DEVICE_ID_RICOH_RL5C476, RICOH),
1273 CB_ID(PCI_VENDOR_ID_RICOH, PCI_DEVICE_ID_RICOH_RL5C478, RICOH),
1275 CB_ID(PCI_VENDOR_ID_TOSHIBA, PCI_DEVICE_ID_TOSHIBA_TOPIC95, TOPIC95),
1276 CB_ID(PCI_VENDOR_ID_TOSHIBA, PCI_DEVICE_ID_TOSHIBA_TOPIC97, TOPIC97),
1277 CB_ID(PCI_VENDOR_ID_TOSHIBA, PCI_DEVICE_ID_TOSHIBA_TOPIC100, TOPIC97),
1279 CB_ID(PCI_VENDOR_ID_O2, PCI_ANY_ID, O2MICRO),
1281 /* match any cardbus bridge */
1282 CB_ID(PCI_ANY_ID, PCI_ANY_ID, DEFAULT),
1283 { /* all zeroes */ }
1285 MODULE_DEVICE_TABLE(pci, yenta_table);
1288 static struct pci_driver yenta_cardbus_driver = {
1289 .name = "yenta_cardbus",
1290 .id_table = yenta_table,
1291 .probe = yenta_probe,
1292 .remove = __devexit_p(yenta_close),
1293 .suspend = yenta_dev_suspend,
1294 .resume = yenta_dev_resume,
1298 static int __init yenta_socket_init(void)
1300 return pci_register_driver (¥ta_cardbus_driver);
1304 static void __exit yenta_socket_exit (void)
1306 pci_unregister_driver (¥ta_cardbus_driver);
1310 module_init(yenta_socket_init);
1311 module_exit(yenta_socket_exit);
1313 MODULE_LICENSE("GPL");