[PATCH] x86_64: CPU hotplug sibling map cleanup
[linux/fpc-iii.git] / drivers / pcmcia / yenta_socket.c
blobbee05362fd244dc46762a0979e3c009c62752a24
1 /*
2 * Regular cardbus driver ("yenta_socket")
4 * (C) Copyright 1999, 2000 Linus Torvalds
6 * Changelog:
7 * Aug 2002: Manfred Spraul <manfred@colorfullife.com>
8 * Dynamically adjust the size of the bridge resource
9 *
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/version.h>
22 #include <pcmcia/cs_types.h>
23 #include <pcmcia/ss.h>
24 #include <pcmcia/cs.h>
26 #include <asm/io.h>
28 #include "yenta_socket.h"
29 #include "i82365.h"
31 static int disable_clkrun;
32 module_param(disable_clkrun, bool, 0444);
33 MODULE_PARM_DESC(disable_clkrun, "If PC card doesn't function properly, please try this option");
35 static int isa_probe = 1;
36 module_param(isa_probe, bool, 0444);
37 MODULE_PARM_DESC(isa_probe, "If set ISA interrupts are probed (default). Set to N to disable probing");
39 static int pwr_irqs_off;
40 module_param(pwr_irqs_off, bool, 0644);
41 MODULE_PARM_DESC(pwr_irqs_off, "Force IRQs off during power-on of slot. Use only when seeing IRQ storms!");
43 #if 0
44 #define debug(x,args...) printk(KERN_DEBUG "%s: " x, __func__ , ##args)
45 #else
46 #define debug(x,args...)
47 #endif
49 /* Don't ask.. */
50 #define to_cycles(ns) ((ns)/120)
51 #define to_ns(cycles) ((cycles)*120)
53 static int yenta_probe_cb_irq(struct yenta_socket *socket);
56 static unsigned int override_bios;
57 module_param(override_bios, uint, 0000);
58 MODULE_PARM_DESC (override_bios, "yenta ignore bios resource allocation");
61 * Generate easy-to-use ways of reading a cardbus sockets
62 * regular memory space ("cb_xxx"), configuration space
63 * ("config_xxx") and compatibility space ("exca_xxxx")
65 static inline u32 cb_readl(struct yenta_socket *socket, unsigned reg)
67 u32 val = readl(socket->base + reg);
68 debug("%p %04x %08x\n", socket, reg, val);
69 return val;
72 static inline void cb_writel(struct yenta_socket *socket, unsigned reg, u32 val)
74 debug("%p %04x %08x\n", socket, reg, val);
75 writel(val, socket->base + reg);
78 static inline u8 config_readb(struct yenta_socket *socket, unsigned offset)
80 u8 val;
81 pci_read_config_byte(socket->dev, offset, &val);
82 debug("%p %04x %02x\n", socket, offset, val);
83 return val;
86 static inline u16 config_readw(struct yenta_socket *socket, unsigned offset)
88 u16 val;
89 pci_read_config_word(socket->dev, offset, &val);
90 debug("%p %04x %04x\n", socket, offset, val);
91 return val;
94 static inline u32 config_readl(struct yenta_socket *socket, unsigned offset)
96 u32 val;
97 pci_read_config_dword(socket->dev, offset, &val);
98 debug("%p %04x %08x\n", socket, offset, val);
99 return 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);
124 return val;
127 static inline u8 exca_readw(struct yenta_socket *socket, unsigned reg)
129 u16 val;
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);
133 return 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);
142 static void exca_writew(struct yenta_socket *socket, unsigned reg, u16 val)
144 debug("%p %04x %04x\n", socket, reg, val);
145 writeb(val, socket->base + 0x800 + reg);
146 writeb(val >> 8, socket->base + 0x800 + reg + 1);
150 * Ugh, mixed-mode cardbus and 16-bit pccard state: things depend
151 * on what kind of card is inserted..
153 static int yenta_get_status(struct pcmcia_socket *sock, unsigned int *value)
155 struct yenta_socket *socket = container_of(sock, struct yenta_socket, socket);
156 unsigned int val;
157 u32 state = cb_readl(socket, CB_SOCKET_STATE);
159 val = (state & CB_3VCARD) ? SS_3VCARD : 0;
160 val |= (state & CB_XVCARD) ? SS_XVCARD : 0;
161 val |= (state & (CB_5VCARD | CB_3VCARD | CB_XVCARD | CB_YVCARD)) ? 0 : SS_PENDING;
162 val |= (state & (CB_CDETECT1 | CB_CDETECT2)) ? SS_PENDING : 0;
165 if (state & CB_CBCARD) {
166 val |= SS_CARDBUS;
167 val |= (state & CB_CARDSTS) ? SS_STSCHG : 0;
168 val |= (state & (CB_CDETECT1 | CB_CDETECT2)) ? 0 : SS_DETECT;
169 val |= (state & CB_PWRCYCLE) ? SS_POWERON | SS_READY : 0;
170 } else if (state & CB_16BITCARD) {
171 u8 status = exca_readb(socket, I365_STATUS);
172 val |= ((status & I365_CS_DETECT) == I365_CS_DETECT) ? SS_DETECT : 0;
173 if (exca_readb(socket, I365_INTCTL) & I365_PC_IOCARD) {
174 val |= (status & I365_CS_STSCHG) ? 0 : SS_STSCHG;
175 } else {
176 val |= (status & I365_CS_BVD1) ? 0 : SS_BATDEAD;
177 val |= (status & I365_CS_BVD2) ? 0 : SS_BATWARN;
179 val |= (status & I365_CS_WRPROT) ? SS_WRPROT : 0;
180 val |= (status & I365_CS_READY) ? SS_READY : 0;
181 val |= (status & I365_CS_POWERON) ? SS_POWERON : 0;
184 *value = val;
185 return 0;
188 static int yenta_Vcc_power(u32 control)
190 switch (control & CB_SC_VCC_MASK) {
191 case CB_SC_VCC_5V: return 50;
192 case CB_SC_VCC_3V: return 33;
193 default: return 0;
197 static int yenta_Vpp_power(u32 control)
199 switch (control & CB_SC_VPP_MASK) {
200 case CB_SC_VPP_12V: return 120;
201 case CB_SC_VPP_5V: return 50;
202 case CB_SC_VPP_3V: return 33;
203 default: return 0;
207 static int yenta_get_socket(struct pcmcia_socket *sock, socket_state_t *state)
209 struct yenta_socket *socket = container_of(sock, struct yenta_socket, socket);
210 u8 reg;
211 u32 control;
213 control = cb_readl(socket, CB_SOCKET_CONTROL);
215 state->Vcc = yenta_Vcc_power(control);
216 state->Vpp = yenta_Vpp_power(control);
217 state->io_irq = socket->io_irq;
219 if (cb_readl(socket, CB_SOCKET_STATE) & CB_CBCARD) {
220 u16 bridge = config_readw(socket, CB_BRIDGE_CONTROL);
221 if (bridge & CB_BRIDGE_CRST)
222 state->flags |= SS_RESET;
223 return 0;
226 /* 16-bit card state.. */
227 reg = exca_readb(socket, I365_POWER);
228 state->flags = (reg & I365_PWR_AUTO) ? SS_PWR_AUTO : 0;
229 state->flags |= (reg & I365_PWR_OUT) ? SS_OUTPUT_ENA : 0;
231 reg = exca_readb(socket, I365_INTCTL);
232 state->flags |= (reg & I365_PC_RESET) ? 0 : SS_RESET;
233 state->flags |= (reg & I365_PC_IOCARD) ? SS_IOCARD : 0;
235 reg = exca_readb(socket, I365_CSCINT);
236 state->csc_mask = (reg & I365_CSC_DETECT) ? SS_DETECT : 0;
237 if (state->flags & SS_IOCARD) {
238 state->csc_mask |= (reg & I365_CSC_STSCHG) ? SS_STSCHG : 0;
239 } else {
240 state->csc_mask |= (reg & I365_CSC_BVD1) ? SS_BATDEAD : 0;
241 state->csc_mask |= (reg & I365_CSC_BVD2) ? SS_BATWARN : 0;
242 state->csc_mask |= (reg & I365_CSC_READY) ? SS_READY : 0;
245 return 0;
248 static void yenta_set_power(struct yenta_socket *socket, socket_state_t *state)
250 u32 reg = 0; /* CB_SC_STPCLK? */
251 switch (state->Vcc) {
252 case 33: reg = CB_SC_VCC_3V; break;
253 case 50: reg = CB_SC_VCC_5V; break;
254 default: reg = 0; break;
256 switch (state->Vpp) {
257 case 33: reg |= CB_SC_VPP_3V; break;
258 case 50: reg |= CB_SC_VPP_5V; break;
259 case 120: reg |= CB_SC_VPP_12V; break;
261 if (reg != cb_readl(socket, CB_SOCKET_CONTROL))
262 cb_writel(socket, CB_SOCKET_CONTROL, reg);
265 static int yenta_set_socket(struct pcmcia_socket *sock, socket_state_t *state)
267 struct yenta_socket *socket = container_of(sock, struct yenta_socket, socket);
268 u16 bridge;
270 yenta_set_power(socket, state);
271 socket->io_irq = state->io_irq;
272 bridge = config_readw(socket, CB_BRIDGE_CONTROL) & ~(CB_BRIDGE_CRST | CB_BRIDGE_INTR);
273 if (cb_readl(socket, CB_SOCKET_STATE) & CB_CBCARD) {
274 u8 intr;
275 bridge |= (state->flags & SS_RESET) ? CB_BRIDGE_CRST : 0;
277 /* ISA interrupt control? */
278 intr = exca_readb(socket, I365_INTCTL);
279 intr = (intr & ~0xf);
280 if (!socket->cb_irq) {
281 intr |= state->io_irq;
282 bridge |= CB_BRIDGE_INTR;
284 exca_writeb(socket, I365_INTCTL, intr);
285 } else {
286 u8 reg;
288 reg = exca_readb(socket, I365_INTCTL) & (I365_RING_ENA | I365_INTR_ENA);
289 reg |= (state->flags & SS_RESET) ? 0 : I365_PC_RESET;
290 reg |= (state->flags & SS_IOCARD) ? I365_PC_IOCARD : 0;
291 if (state->io_irq != socket->cb_irq) {
292 reg |= state->io_irq;
293 bridge |= CB_BRIDGE_INTR;
295 exca_writeb(socket, I365_INTCTL, reg);
297 reg = exca_readb(socket, I365_POWER) & (I365_VCC_MASK|I365_VPP1_MASK);
298 reg |= I365_PWR_NORESET;
299 if (state->flags & SS_PWR_AUTO) reg |= I365_PWR_AUTO;
300 if (state->flags & SS_OUTPUT_ENA) reg |= I365_PWR_OUT;
301 if (exca_readb(socket, I365_POWER) != reg)
302 exca_writeb(socket, I365_POWER, reg);
304 /* CSC interrupt: no ISA irq for CSC */
305 reg = I365_CSC_DETECT;
306 if (state->flags & SS_IOCARD) {
307 if (state->csc_mask & SS_STSCHG) reg |= I365_CSC_STSCHG;
308 } else {
309 if (state->csc_mask & SS_BATDEAD) reg |= I365_CSC_BVD1;
310 if (state->csc_mask & SS_BATWARN) reg |= I365_CSC_BVD2;
311 if (state->csc_mask & SS_READY) reg |= I365_CSC_READY;
313 exca_writeb(socket, I365_CSCINT, reg);
314 exca_readb(socket, I365_CSC);
315 if(sock->zoom_video)
316 sock->zoom_video(sock, state->flags & SS_ZVCARD);
318 config_writew(socket, CB_BRIDGE_CONTROL, bridge);
319 /* Socket event mask: get card insert/remove events.. */
320 cb_writel(socket, CB_SOCKET_EVENT, -1);
321 cb_writel(socket, CB_SOCKET_MASK, CB_CDMASK);
322 return 0;
325 static int yenta_set_io_map(struct pcmcia_socket *sock, struct pccard_io_map *io)
327 struct yenta_socket *socket = container_of(sock, struct yenta_socket, socket);
328 int map;
329 unsigned char ioctl, addr, enable;
331 map = io->map;
333 if (map > 1)
334 return -EINVAL;
336 enable = I365_ENA_IO(map);
337 addr = exca_readb(socket, I365_ADDRWIN);
339 /* Disable the window before changing it.. */
340 if (addr & enable) {
341 addr &= ~enable;
342 exca_writeb(socket, I365_ADDRWIN, addr);
345 exca_writew(socket, I365_IO(map)+I365_W_START, io->start);
346 exca_writew(socket, I365_IO(map)+I365_W_STOP, io->stop);
348 ioctl = exca_readb(socket, I365_IOCTL) & ~I365_IOCTL_MASK(map);
349 if (io->flags & MAP_0WS) ioctl |= I365_IOCTL_0WS(map);
350 if (io->flags & MAP_16BIT) ioctl |= I365_IOCTL_16BIT(map);
351 if (io->flags & MAP_AUTOSZ) ioctl |= I365_IOCTL_IOCS16(map);
352 exca_writeb(socket, I365_IOCTL, ioctl);
354 if (io->flags & MAP_ACTIVE)
355 exca_writeb(socket, I365_ADDRWIN, addr | enable);
356 return 0;
359 static int yenta_set_mem_map(struct pcmcia_socket *sock, struct pccard_mem_map *mem)
361 struct yenta_socket *socket = container_of(sock, struct yenta_socket, socket);
362 struct pci_bus_region region;
363 int map;
364 unsigned char addr, enable;
365 unsigned int start, stop, card_start;
366 unsigned short word;
368 pcibios_resource_to_bus(socket->dev, &region, mem->res);
370 map = mem->map;
371 start = region.start;
372 stop = region.end;
373 card_start = mem->card_start;
375 if (map > 4 || start > stop || ((start ^ stop) >> 24) ||
376 (card_start >> 26) || mem->speed > 1000)
377 return -EINVAL;
379 enable = I365_ENA_MEM(map);
380 addr = exca_readb(socket, I365_ADDRWIN);
381 if (addr & enable) {
382 addr &= ~enable;
383 exca_writeb(socket, I365_ADDRWIN, addr);
386 exca_writeb(socket, CB_MEM_PAGE(map), start >> 24);
388 word = (start >> 12) & 0x0fff;
389 if (mem->flags & MAP_16BIT)
390 word |= I365_MEM_16BIT;
391 if (mem->flags & MAP_0WS)
392 word |= I365_MEM_0WS;
393 exca_writew(socket, I365_MEM(map) + I365_W_START, word);
395 word = (stop >> 12) & 0x0fff;
396 switch (to_cycles(mem->speed)) {
397 case 0: break;
398 case 1: word |= I365_MEM_WS0; break;
399 case 2: word |= I365_MEM_WS1; break;
400 default: word |= I365_MEM_WS1 | I365_MEM_WS0; break;
402 exca_writew(socket, I365_MEM(map) + I365_W_STOP, word);
404 word = ((card_start - start) >> 12) & 0x3fff;
405 if (mem->flags & MAP_WRPROT)
406 word |= I365_MEM_WRPROT;
407 if (mem->flags & MAP_ATTRIB)
408 word |= I365_MEM_REG;
409 exca_writew(socket, I365_MEM(map) + I365_W_OFF, word);
411 if (mem->flags & MAP_ACTIVE)
412 exca_writeb(socket, I365_ADDRWIN, addr | enable);
413 return 0;
418 static irqreturn_t yenta_interrupt(int irq, void *dev_id, struct pt_regs *regs)
420 unsigned int events;
421 struct yenta_socket *socket = (struct yenta_socket *) dev_id;
422 u8 csc;
423 u32 cb_event;
425 /* Clear interrupt status for the event */
426 cb_event = cb_readl(socket, CB_SOCKET_EVENT);
427 cb_writel(socket, CB_SOCKET_EVENT, cb_event);
429 csc = exca_readb(socket, I365_CSC);
431 events = (cb_event & (CB_CD1EVENT | CB_CD2EVENT)) ? SS_DETECT : 0 ;
432 events |= (csc & I365_CSC_DETECT) ? SS_DETECT : 0;
433 if (exca_readb(socket, I365_INTCTL) & I365_PC_IOCARD) {
434 events |= (csc & I365_CSC_STSCHG) ? SS_STSCHG : 0;
435 } else {
436 events |= (csc & I365_CSC_BVD1) ? SS_BATDEAD : 0;
437 events |= (csc & I365_CSC_BVD2) ? SS_BATWARN : 0;
438 events |= (csc & I365_CSC_READY) ? SS_READY : 0;
441 if (events)
442 pcmcia_parse_events(&socket->socket, events);
444 if (cb_event || csc)
445 return IRQ_HANDLED;
447 return IRQ_NONE;
450 static void yenta_interrupt_wrapper(unsigned long data)
452 struct yenta_socket *socket = (struct yenta_socket *) data;
454 yenta_interrupt(0, (void *)socket, NULL);
455 socket->poll_timer.expires = jiffies + HZ;
456 add_timer(&socket->poll_timer);
459 static void yenta_clear_maps(struct yenta_socket *socket)
461 int i;
462 struct resource res = { .start = 0, .end = 0x0fff };
463 pccard_io_map io = { 0, 0, 0, 0, 1 };
464 pccard_mem_map mem = { .res = &res, };
466 yenta_set_socket(&socket->socket, &dead_socket);
467 for (i = 0; i < 2; i++) {
468 io.map = i;
469 yenta_set_io_map(&socket->socket, &io);
471 for (i = 0; i < 5; i++) {
472 mem.map = i;
473 yenta_set_mem_map(&socket->socket, &mem);
477 /* redoes voltage interrogation if required */
478 static void yenta_interrogate(struct yenta_socket *socket)
480 u32 state;
482 state = cb_readl(socket, CB_SOCKET_STATE);
483 if (!(state & (CB_5VCARD | CB_3VCARD | CB_XVCARD | CB_YVCARD)) ||
484 (state & (CB_CDETECT1 | CB_CDETECT2 | CB_NOTACARD | CB_BADVCCREQ)) ||
485 ((state & (CB_16BITCARD | CB_CBCARD)) == (CB_16BITCARD | CB_CBCARD)))
486 cb_writel(socket, CB_SOCKET_FORCE, CB_CVSTEST);
489 /* Called at resume and initialization events */
490 static int yenta_sock_init(struct pcmcia_socket *sock)
492 struct yenta_socket *socket = container_of(sock, struct yenta_socket, socket);
493 u16 bridge;
495 bridge = config_readw(socket, CB_BRIDGE_CONTROL) & ~CB_BRIDGE_INTR;
496 if (!socket->cb_irq)
497 bridge |= CB_BRIDGE_INTR;
498 config_writew(socket, CB_BRIDGE_CONTROL, bridge);
500 exca_writeb(socket, I365_GBLCTL, 0x00);
501 exca_writeb(socket, I365_GENCTL, 0x00);
503 /* Redo card voltage interrogation */
504 yenta_interrogate(socket);
506 yenta_clear_maps(socket);
508 if (socket->type && socket->type->sock_init)
509 socket->type->sock_init(socket);
511 /* Re-enable CSC interrupts */
512 cb_writel(socket, CB_SOCKET_MASK, CB_CDMASK);
514 return 0;
517 static int yenta_sock_suspend(struct pcmcia_socket *sock)
519 struct yenta_socket *socket = container_of(sock, struct yenta_socket, socket);
521 /* Disable CSC interrupts */
522 cb_writel(socket, CB_SOCKET_MASK, 0x0);
524 return 0;
528 * Use an adaptive allocation for the memory resource,
529 * sometimes the memory behind pci bridges is limited:
530 * 1/8 of the size of the io window of the parent.
531 * max 4 MB, min 16 kB.
533 #define BRIDGE_MEM_MAX 4*1024*1024
534 #define BRIDGE_MEM_MIN 16*1024
536 #define BRIDGE_IO_MAX 256
537 #define BRIDGE_IO_MIN 32
539 #ifndef PCIBIOS_MIN_CARDBUS_IO
540 #define PCIBIOS_MIN_CARDBUS_IO PCIBIOS_MIN_IO
541 #endif
543 static void yenta_allocate_res(struct yenta_socket *socket, int nr, unsigned type)
545 struct pci_bus *bus;
546 struct resource *root, *res;
547 u32 start, end;
548 u32 align, size, min;
549 unsigned offset;
550 unsigned mask;
552 /* The granularity of the memory limit is 4kB, on IO it's 4 bytes */
553 mask = ~0xfff;
554 if (type & IORESOURCE_IO)
555 mask = ~3;
557 offset = 0x1c + 8*nr;
558 bus = socket->dev->subordinate;
559 res = socket->dev->resource + PCI_BRIDGE_RESOURCES + nr;
560 res->name = bus->name;
561 res->flags = type;
562 res->start = 0;
563 res->end = 0;
564 root = pci_find_parent_resource(socket->dev, res);
566 if (!root)
567 return;
569 start = config_readl(socket, offset) & mask;
570 end = config_readl(socket, offset+4) | ~mask;
571 if (start && end > start && !override_bios) {
572 res->start = start;
573 res->end = end;
574 if (request_resource(root, res) == 0)
575 return;
576 printk(KERN_INFO "yenta %s: Preassigned resource %d busy, reconfiguring...\n",
577 pci_name(socket->dev), nr);
578 res->start = res->end = 0;
581 if (type & IORESOURCE_IO) {
582 align = 1024;
583 size = BRIDGE_IO_MAX;
584 min = BRIDGE_IO_MIN;
585 start = PCIBIOS_MIN_CARDBUS_IO;
586 end = ~0U;
587 } else {
588 unsigned long avail = root->end - root->start;
589 int i;
590 size = BRIDGE_MEM_MAX;
591 if (size > avail/8) {
592 size=(avail+1)/8;
593 /* round size down to next power of 2 */
594 i = 0;
595 while ((size /= 2) != 0)
596 i++;
597 size = 1 << i;
599 if (size < BRIDGE_MEM_MIN)
600 size = BRIDGE_MEM_MIN;
601 min = BRIDGE_MEM_MIN;
602 align = size;
603 start = PCIBIOS_MIN_MEM;
604 end = ~0U;
607 do {
608 if (allocate_resource(root, res, size, start, end, align, NULL, NULL)==0) {
609 config_writel(socket, offset, res->start);
610 config_writel(socket, offset+4, res->end);
611 return;
613 size = size/2;
614 align = size;
615 } while (size >= min);
616 printk(KERN_INFO "yenta %s: no resource of type %x available, trying to continue...\n",
617 pci_name(socket->dev), type);
618 res->start = res->end = 0;
622 * Allocate the bridge mappings for the device..
624 static void yenta_allocate_resources(struct yenta_socket *socket)
626 yenta_allocate_res(socket, 0, IORESOURCE_MEM|IORESOURCE_PREFETCH);
627 yenta_allocate_res(socket, 1, IORESOURCE_MEM);
628 yenta_allocate_res(socket, 2, IORESOURCE_IO);
629 yenta_allocate_res(socket, 3, IORESOURCE_IO); /* PCI isn't clever enough to use this one yet */
634 * Free the bridge mappings for the device..
636 static void yenta_free_resources(struct yenta_socket *socket)
638 int i;
639 for (i=0;i<4;i++) {
640 struct resource *res;
641 res = socket->dev->resource + PCI_BRIDGE_RESOURCES + i;
642 if (res->start != 0 && res->end != 0)
643 release_resource(res);
644 res->start = res->end = 0;
650 * Close it down - release our resources and go home..
652 static void yenta_close(struct pci_dev *dev)
654 struct yenta_socket *sock = pci_get_drvdata(dev);
656 /* we don't want a dying socket registered */
657 pcmcia_unregister_socket(&sock->socket);
659 /* Disable all events so we don't die in an IRQ storm */
660 cb_writel(sock, CB_SOCKET_MASK, 0x0);
661 exca_writeb(sock, I365_CSCINT, 0);
663 if (sock->cb_irq)
664 free_irq(sock->cb_irq, sock);
665 else
666 del_timer_sync(&sock->poll_timer);
668 if (sock->base)
669 iounmap(sock->base);
670 yenta_free_resources(sock);
672 pci_release_regions(dev);
673 pci_disable_device(dev);
674 pci_set_drvdata(dev, NULL);
678 static struct pccard_operations yenta_socket_operations = {
679 .init = yenta_sock_init,
680 .suspend = yenta_sock_suspend,
681 .get_status = yenta_get_status,
682 .get_socket = yenta_get_socket,
683 .set_socket = yenta_set_socket,
684 .set_io_map = yenta_set_io_map,
685 .set_mem_map = yenta_set_mem_map,
689 #include "ti113x.h"
690 #include "ricoh.h"
691 #include "topic.h"
692 #include "o2micro.h"
694 enum {
695 CARDBUS_TYPE_DEFAULT = -1,
696 CARDBUS_TYPE_TI,
697 CARDBUS_TYPE_TI113X,
698 CARDBUS_TYPE_TI12XX,
699 CARDBUS_TYPE_TI1250,
700 CARDBUS_TYPE_RICOH,
701 CARDBUS_TYPE_TOPIC97,
702 CARDBUS_TYPE_O2MICRO,
706 * Different cardbus controllers have slightly different
707 * initialization sequences etc details. List them here..
709 static struct cardbus_type cardbus_type[] = {
710 [CARDBUS_TYPE_TI] = {
711 .override = ti_override,
712 .save_state = ti_save_state,
713 .restore_state = ti_restore_state,
714 .sock_init = ti_init,
716 [CARDBUS_TYPE_TI113X] = {
717 .override = ti113x_override,
718 .save_state = ti_save_state,
719 .restore_state = ti_restore_state,
720 .sock_init = ti_init,
722 [CARDBUS_TYPE_TI12XX] = {
723 .override = ti12xx_override,
724 .save_state = ti_save_state,
725 .restore_state = ti_restore_state,
726 .sock_init = ti_init,
728 [CARDBUS_TYPE_TI1250] = {
729 .override = ti1250_override,
730 .save_state = ti_save_state,
731 .restore_state = ti_restore_state,
732 .sock_init = ti_init,
734 [CARDBUS_TYPE_RICOH] = {
735 .override = ricoh_override,
736 .save_state = ricoh_save_state,
737 .restore_state = ricoh_restore_state,
739 [CARDBUS_TYPE_TOPIC97] = {
740 .override = topic97_override,
742 [CARDBUS_TYPE_O2MICRO] = {
743 .override = o2micro_override,
744 .restore_state = o2micro_restore_state,
750 * Only probe "regular" interrupts, don't
751 * touch dangerous spots like the mouse irq,
752 * because there are mice that apparently
753 * get really confused if they get fondled
754 * too intimately.
756 * Default to 11, 10, 9, 7, 6, 5, 4, 3.
758 static u32 isa_interrupts = 0x0ef8;
760 static unsigned int yenta_probe_irq(struct yenta_socket *socket, u32 isa_irq_mask)
762 int i;
763 unsigned long val;
764 u16 bridge_ctrl;
765 u32 mask;
767 /* Set up ISA irq routing to probe the ISA irqs.. */
768 bridge_ctrl = config_readw(socket, CB_BRIDGE_CONTROL);
769 if (!(bridge_ctrl & CB_BRIDGE_INTR)) {
770 bridge_ctrl |= CB_BRIDGE_INTR;
771 config_writew(socket, CB_BRIDGE_CONTROL, bridge_ctrl);
775 * Probe for usable interrupts using the force
776 * register to generate bogus card status events.
778 cb_writel(socket, CB_SOCKET_EVENT, -1);
779 cb_writel(socket, CB_SOCKET_MASK, CB_CSTSMASK);
780 exca_writeb(socket, I365_CSCINT, 0);
781 val = probe_irq_on() & isa_irq_mask;
782 for (i = 1; i < 16; i++) {
783 if (!((val >> i) & 1))
784 continue;
785 exca_writeb(socket, I365_CSCINT, I365_CSC_STSCHG | (i << 4));
786 cb_writel(socket, CB_SOCKET_FORCE, CB_FCARDSTS);
787 udelay(100);
788 cb_writel(socket, CB_SOCKET_EVENT, -1);
790 cb_writel(socket, CB_SOCKET_MASK, 0);
791 exca_writeb(socket, I365_CSCINT, 0);
793 mask = probe_irq_mask(val) & 0xffff;
795 bridge_ctrl &= ~CB_BRIDGE_INTR;
796 config_writew(socket, CB_BRIDGE_CONTROL, bridge_ctrl);
798 return mask;
802 /* interrupt handler, only used during probing */
803 static irqreturn_t yenta_probe_handler(int irq, void *dev_id, struct pt_regs *regs)
805 struct yenta_socket *socket = (struct yenta_socket *) dev_id;
806 u8 csc;
807 u32 cb_event;
809 /* Clear interrupt status for the event */
810 cb_event = cb_readl(socket, CB_SOCKET_EVENT);
811 cb_writel(socket, CB_SOCKET_EVENT, -1);
812 csc = exca_readb(socket, I365_CSC);
814 if (cb_event || csc) {
815 socket->probe_status = 1;
816 return IRQ_HANDLED;
819 return IRQ_NONE;
822 /* probes the PCI interrupt, use only on override functions */
823 static int yenta_probe_cb_irq(struct yenta_socket *socket)
825 u16 bridge_ctrl;
827 if (!socket->cb_irq)
828 return -1;
830 socket->probe_status = 0;
832 /* disable ISA interrupts */
833 bridge_ctrl = config_readw(socket, CB_BRIDGE_CONTROL);
834 bridge_ctrl &= ~CB_BRIDGE_INTR;
835 config_writew(socket, CB_BRIDGE_CONTROL, bridge_ctrl);
837 if (request_irq(socket->cb_irq, yenta_probe_handler, SA_SHIRQ, "yenta", socket)) {
838 printk(KERN_WARNING "Yenta: request_irq() in yenta_probe_cb_irq() failed!\n");
839 return -1;
842 /* generate interrupt, wait */
843 exca_writeb(socket, I365_CSCINT, I365_CSC_STSCHG);
844 cb_writel(socket, CB_SOCKET_EVENT, -1);
845 cb_writel(socket, CB_SOCKET_MASK, CB_CSTSMASK);
846 cb_writel(socket, CB_SOCKET_FORCE, CB_FCARDSTS);
848 msleep(100);
850 /* disable interrupts */
851 cb_writel(socket, CB_SOCKET_MASK, 0);
852 exca_writeb(socket, I365_CSCINT, 0);
853 cb_writel(socket, CB_SOCKET_EVENT, -1);
854 exca_readb(socket, I365_CSC);
856 free_irq(socket->cb_irq, socket);
858 return (int) socket->probe_status;
864 * Set static data that doesn't need re-initializing..
866 static void yenta_get_socket_capabilities(struct yenta_socket *socket, u32 isa_irq_mask)
868 socket->socket.features |= SS_CAP_PAGE_REGS | SS_CAP_PCCARD | SS_CAP_CARDBUS;
869 socket->socket.map_size = 0x1000;
870 socket->socket.pci_irq = socket->cb_irq;
871 if (isa_probe)
872 socket->socket.irq_mask = yenta_probe_irq(socket, isa_irq_mask);
873 else
874 socket->socket.irq_mask = 0;
875 socket->socket.cb_dev = socket->dev;
877 printk(KERN_INFO "Yenta: ISA IRQ mask 0x%04x, PCI irq %d\n",
878 socket->socket.irq_mask, socket->cb_irq);
882 * Initialize the standard cardbus registers
884 static void yenta_config_init(struct yenta_socket *socket)
886 u16 bridge;
887 struct pci_dev *dev = socket->dev;
889 pci_set_power_state(socket->dev, 0);
891 config_writel(socket, CB_LEGACY_MODE_BASE, 0);
892 config_writel(socket, PCI_BASE_ADDRESS_0, dev->resource[0].start);
893 config_writew(socket, PCI_COMMAND,
894 PCI_COMMAND_IO |
895 PCI_COMMAND_MEMORY |
896 PCI_COMMAND_MASTER |
897 PCI_COMMAND_WAIT);
899 /* MAGIC NUMBERS! Fixme */
900 config_writeb(socket, PCI_CACHE_LINE_SIZE, L1_CACHE_BYTES / 4);
901 config_writeb(socket, PCI_LATENCY_TIMER, 168);
902 config_writel(socket, PCI_PRIMARY_BUS,
903 (176 << 24) | /* sec. latency timer */
904 (dev->subordinate->subordinate << 16) | /* subordinate bus */
905 (dev->subordinate->secondary << 8) | /* secondary bus */
906 dev->subordinate->primary); /* primary bus */
909 * Set up the bridging state:
910 * - enable write posting.
911 * - memory window 0 prefetchable, window 1 non-prefetchable
912 * - PCI interrupts enabled if a PCI interrupt exists..
914 bridge = config_readw(socket, CB_BRIDGE_CONTROL);
915 bridge &= ~(CB_BRIDGE_CRST | CB_BRIDGE_PREFETCH1 | CB_BRIDGE_INTR | CB_BRIDGE_ISAEN | CB_BRIDGE_VGAEN);
916 bridge |= CB_BRIDGE_PREFETCH0 | CB_BRIDGE_POSTEN | CB_BRIDGE_INTR;
917 config_writew(socket, CB_BRIDGE_CONTROL, bridge);
921 * Initialize a cardbus controller. Make sure we have a usable
922 * interrupt, and that we can map the cardbus area. Fill in the
923 * socket information structure..
925 static int __devinit yenta_probe (struct pci_dev *dev, const struct pci_device_id *id)
927 struct yenta_socket *socket;
928 int ret;
930 socket = kmalloc(sizeof(struct yenta_socket), GFP_KERNEL);
931 if (!socket)
932 return -ENOMEM;
933 memset(socket, 0, sizeof(*socket));
935 /* prepare pcmcia_socket */
936 socket->socket.ops = &yenta_socket_operations;
937 socket->socket.resource_ops = &pccard_nonstatic_ops;
938 socket->socket.dev.dev = &dev->dev;
939 socket->socket.driver_data = socket;
940 socket->socket.owner = THIS_MODULE;
942 /* prepare struct yenta_socket */
943 socket->dev = dev;
944 pci_set_drvdata(dev, socket);
947 * Do some basic sanity checking..
949 if (pci_enable_device(dev)) {
950 ret = -EBUSY;
951 goto free;
954 ret = pci_request_regions(dev, "yenta_socket");
955 if (ret)
956 goto disable;
958 if (!pci_resource_start(dev, 0)) {
959 printk(KERN_ERR "No cardbus resource!\n");
960 ret = -ENODEV;
961 goto release;
965 * Ok, start setup.. Map the cardbus registers,
966 * and request the IRQ.
968 socket->base = ioremap(pci_resource_start(dev, 0), 0x1000);
969 if (!socket->base) {
970 ret = -ENOMEM;
971 goto release;
975 * report the subsystem vendor and device for help debugging
976 * the irq stuff...
978 printk(KERN_INFO "Yenta: CardBus bridge found at %s [%04x:%04x]\n",
979 pci_name(dev), dev->subsystem_vendor, dev->subsystem_device);
981 yenta_config_init(socket);
983 /* Disable all events */
984 cb_writel(socket, CB_SOCKET_MASK, 0x0);
986 /* Set up the bridge regions.. */
987 yenta_allocate_resources(socket);
989 socket->cb_irq = dev->irq;
991 /* Do we have special options for the device? */
992 if (id->driver_data != CARDBUS_TYPE_DEFAULT &&
993 id->driver_data < ARRAY_SIZE(cardbus_type)) {
994 socket->type = &cardbus_type[id->driver_data];
996 ret = socket->type->override(socket);
997 if (ret < 0)
998 goto unmap;
1001 /* We must finish initialization here */
1003 if (!socket->cb_irq || request_irq(socket->cb_irq, yenta_interrupt, SA_SHIRQ, "yenta", socket)) {
1004 /* No IRQ or request_irq failed. Poll */
1005 socket->cb_irq = 0; /* But zero is a valid IRQ number. */
1006 init_timer(&socket->poll_timer);
1007 socket->poll_timer.function = yenta_interrupt_wrapper;
1008 socket->poll_timer.data = (unsigned long)socket;
1009 socket->poll_timer.expires = jiffies + HZ;
1010 add_timer(&socket->poll_timer);
1013 /* Figure out what the dang thing can do for the PCMCIA layer... */
1014 yenta_interrogate(socket);
1015 yenta_get_socket_capabilities(socket, isa_interrupts);
1016 printk(KERN_INFO "Socket status: %08x\n", cb_readl(socket, CB_SOCKET_STATE));
1018 /* Register it with the pcmcia layer.. */
1019 ret = pcmcia_register_socket(&socket->socket);
1020 if (ret == 0)
1021 goto out;
1023 unmap:
1024 iounmap(socket->base);
1025 release:
1026 pci_release_regions(dev);
1027 disable:
1028 pci_disable_device(dev);
1029 free:
1030 kfree(socket);
1031 out:
1032 return ret;
1036 static int yenta_dev_suspend (struct pci_dev *dev, pm_message_t state)
1038 struct yenta_socket *socket = pci_get_drvdata(dev);
1039 int ret;
1041 ret = pcmcia_socket_dev_suspend(&dev->dev, state);
1043 if (socket) {
1044 if (socket->type && socket->type->save_state)
1045 socket->type->save_state(socket);
1047 /* FIXME: pci_save_state needs to have a better interface */
1048 pci_save_state(dev);
1049 pci_read_config_dword(dev, 16*4, &socket->saved_state[0]);
1050 pci_read_config_dword(dev, 17*4, &socket->saved_state[1]);
1053 * Some laptops (IBM T22) do not like us putting the Cardbus
1054 * bridge into D3. At a guess, some other laptop will
1055 * probably require this, so leave it commented out for now.
1057 /* pci_set_power_state(dev, 3); */
1060 return ret;
1064 static int yenta_dev_resume (struct pci_dev *dev)
1066 struct yenta_socket *socket = pci_get_drvdata(dev);
1068 if (socket) {
1069 pci_set_power_state(dev, 0);
1070 /* FIXME: pci_restore_state needs to have a better interface */
1071 pci_restore_state(dev);
1072 pci_write_config_dword(dev, 16*4, socket->saved_state[0]);
1073 pci_write_config_dword(dev, 17*4, socket->saved_state[1]);
1075 if (socket->type && socket->type->restore_state)
1076 socket->type->restore_state(socket);
1079 return pcmcia_socket_dev_resume(&dev->dev);
1083 #define CB_ID(vend,dev,type) \
1085 .vendor = vend, \
1086 .device = dev, \
1087 .subvendor = PCI_ANY_ID, \
1088 .subdevice = PCI_ANY_ID, \
1089 .class = PCI_CLASS_BRIDGE_CARDBUS << 8, \
1090 .class_mask = ~0, \
1091 .driver_data = CARDBUS_TYPE_##type, \
1094 static struct pci_device_id yenta_table [] = {
1095 CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_1031, TI),
1098 * TBD: Check if these TI variants can use more
1099 * advanced overrides instead. (I can't get the
1100 * data sheets for these devices. --rmk)
1102 CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_1210, TI),
1104 CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_1130, TI113X),
1105 CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_1131, TI113X),
1107 CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_1211, TI12XX),
1108 CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_1220, TI12XX),
1109 CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_1221, TI12XX),
1110 CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_1225, TI12XX),
1111 CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_1251A, TI12XX),
1112 CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_1251B, TI12XX),
1113 CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_1420, TI12XX),
1114 CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_1450, TI12XX),
1115 CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_1451A, TI12XX),
1116 CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_1510, TI12XX),
1117 CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_1520, TI12XX),
1118 CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_1620, TI12XX),
1119 CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_4410, TI12XX),
1120 CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_4450, TI12XX),
1121 CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_4451, TI12XX),
1122 CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_4510, TI12XX),
1123 CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_4520, TI12XX),
1125 CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_1250, TI1250),
1126 CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_1410, TI1250),
1128 CB_ID(PCI_VENDOR_ID_ENE, PCI_DEVICE_ID_ENE_1211, TI12XX),
1129 CB_ID(PCI_VENDOR_ID_ENE, PCI_DEVICE_ID_ENE_1225, TI12XX),
1130 CB_ID(PCI_VENDOR_ID_ENE, PCI_DEVICE_ID_ENE_1410, TI1250),
1131 CB_ID(PCI_VENDOR_ID_ENE, PCI_DEVICE_ID_ENE_1420, TI12XX),
1133 CB_ID(PCI_VENDOR_ID_RICOH, PCI_DEVICE_ID_RICOH_RL5C465, RICOH),
1134 CB_ID(PCI_VENDOR_ID_RICOH, PCI_DEVICE_ID_RICOH_RL5C466, RICOH),
1135 CB_ID(PCI_VENDOR_ID_RICOH, PCI_DEVICE_ID_RICOH_RL5C475, RICOH),
1136 CB_ID(PCI_VENDOR_ID_RICOH, PCI_DEVICE_ID_RICOH_RL5C476, RICOH),
1137 CB_ID(PCI_VENDOR_ID_RICOH, PCI_DEVICE_ID_RICOH_RL5C478, RICOH),
1139 CB_ID(PCI_VENDOR_ID_TOSHIBA, PCI_DEVICE_ID_TOSHIBA_TOPIC97, TOPIC97),
1140 CB_ID(PCI_VENDOR_ID_TOSHIBA, PCI_DEVICE_ID_TOSHIBA_TOPIC100, TOPIC97),
1142 CB_ID(PCI_VENDOR_ID_O2, PCI_ANY_ID, O2MICRO),
1144 /* match any cardbus bridge */
1145 CB_ID(PCI_ANY_ID, PCI_ANY_ID, DEFAULT),
1146 { /* all zeroes */ }
1148 MODULE_DEVICE_TABLE(pci, yenta_table);
1151 static struct pci_driver yenta_cardbus_driver = {
1152 .name = "yenta_cardbus",
1153 .id_table = yenta_table,
1154 .probe = yenta_probe,
1155 .remove = __devexit_p(yenta_close),
1156 .suspend = yenta_dev_suspend,
1157 .resume = yenta_dev_resume,
1161 static int __init yenta_socket_init(void)
1163 return pci_register_driver (&yenta_cardbus_driver);
1167 static void __exit yenta_socket_exit (void)
1169 pci_unregister_driver (&yenta_cardbus_driver);
1173 module_init(yenta_socket_init);
1174 module_exit(yenta_socket_exit);
1176 MODULE_LICENSE("GPL");