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/version.h>
22 #include <pcmcia/cs_types.h>
23 #include <pcmcia/ss.h>
24 #include <pcmcia/cs.h>
28 #include "yenta_socket.h"
33 #define DEBUG(x,args...) printk("%s: " x, __FUNCTION__, ##args)
35 #define DEBUG(x,args...)
39 #define to_cycles(ns) ((ns)/120)
40 #define to_ns(cycles) ((cycles)*120)
43 * Generate easy-to-use ways of reading a cardbus sockets
44 * regular memory space ("cb_xxx"), configuration space
45 * ("config_xxx") and compatibility space ("exca_xxxx")
47 static inline u32
cb_readl(struct yenta_socket
*socket
, unsigned reg
)
49 u32 val
= readl(socket
->base
+ reg
);
50 DEBUG("%p %04x %08x\n", socket
, reg
, val
);
54 static inline void cb_writel(struct yenta_socket
*socket
, unsigned reg
, u32 val
)
56 DEBUG("%p %04x %08x\n", socket
, reg
, val
);
57 writel(val
, socket
->base
+ reg
);
60 static inline u8
config_readb(struct yenta_socket
*socket
, unsigned offset
)
63 pci_read_config_byte(socket
->dev
, offset
, &val
);
64 DEBUG("%p %04x %02x\n", socket
, offset
, val
);
68 static inline u16
config_readw(struct yenta_socket
*socket
, unsigned offset
)
71 pci_read_config_word(socket
->dev
, offset
, &val
);
72 DEBUG("%p %04x %04x\n", socket
, offset
, val
);
76 static inline u32
config_readl(struct yenta_socket
*socket
, unsigned offset
)
79 pci_read_config_dword(socket
->dev
, offset
, &val
);
80 DEBUG("%p %04x %08x\n", socket
, offset
, val
);
84 static inline void config_writeb(struct yenta_socket
*socket
, unsigned offset
, u8 val
)
86 DEBUG("%p %04x %02x\n", socket
, offset
, val
);
87 pci_write_config_byte(socket
->dev
, offset
, val
);
90 static inline void config_writew(struct yenta_socket
*socket
, unsigned offset
, u16 val
)
92 DEBUG("%p %04x %04x\n", socket
, offset
, val
);
93 pci_write_config_word(socket
->dev
, offset
, val
);
96 static inline void config_writel(struct yenta_socket
*socket
, unsigned offset
, u32 val
)
98 DEBUG("%p %04x %08x\n", socket
, offset
, val
);
99 pci_write_config_dword(socket
->dev
, offset
, val
);
102 static inline u8
exca_readb(struct yenta_socket
*socket
, unsigned reg
)
104 u8 val
= readb(socket
->base
+ 0x800 + reg
);
105 DEBUG("%p %04x %02x\n", socket
, reg
, val
);
109 static inline u8
exca_readw(struct yenta_socket
*socket
, unsigned reg
)
112 val
= readb(socket
->base
+ 0x800 + reg
);
113 val
|= readb(socket
->base
+ 0x800 + reg
+ 1) << 8;
114 DEBUG("%p %04x %04x\n", socket
, reg
, val
);
118 static inline void exca_writeb(struct yenta_socket
*socket
, unsigned reg
, u8 val
)
120 DEBUG("%p %04x %02x\n", socket
, reg
, val
);
121 writeb(val
, socket
->base
+ 0x800 + reg
);
124 static void exca_writew(struct yenta_socket
*socket
, unsigned reg
, u16 val
)
126 DEBUG("%p %04x %04x\n", socket
, reg
, val
);
127 writeb(val
, socket
->base
+ 0x800 + reg
);
128 writeb(val
>> 8, socket
->base
+ 0x800 + reg
+ 1);
132 * Ugh, mixed-mode cardbus and 16-bit pccard state: things depend
133 * on what kind of card is inserted..
135 static int yenta_get_status(struct pcmcia_socket
*sock
, unsigned int *value
)
137 struct yenta_socket
*socket
= container_of(sock
, struct yenta_socket
, socket
);
139 u32 state
= cb_readl(socket
, CB_SOCKET_STATE
);
141 val
= (state
& CB_3VCARD
) ? SS_3VCARD
: 0;
142 val
|= (state
& CB_XVCARD
) ? SS_XVCARD
: 0;
143 val
|= (state
& (CB_CDETECT1
| CB_CDETECT2
| CB_5VCARD
| CB_3VCARD
144 | CB_XVCARD
| CB_YVCARD
)) ? 0 : SS_PENDING
;
146 if (state
& CB_CBCARD
) {
148 val
|= (state
& CB_CARDSTS
) ? SS_STSCHG
: 0;
149 val
|= (state
& (CB_CDETECT1
| CB_CDETECT2
)) ? 0 : SS_DETECT
;
150 val
|= (state
& CB_PWRCYCLE
) ? SS_POWERON
| SS_READY
: 0;
152 u8 status
= exca_readb(socket
, I365_STATUS
);
153 val
|= ((status
& I365_CS_DETECT
) == I365_CS_DETECT
) ? SS_DETECT
: 0;
154 if (exca_readb(socket
, I365_INTCTL
) & I365_PC_IOCARD
) {
155 val
|= (status
& I365_CS_STSCHG
) ? 0 : SS_STSCHG
;
157 val
|= (status
& I365_CS_BVD1
) ? 0 : SS_BATDEAD
;
158 val
|= (status
& I365_CS_BVD2
) ? 0 : SS_BATWARN
;
160 val
|= (status
& I365_CS_WRPROT
) ? SS_WRPROT
: 0;
161 val
|= (status
& I365_CS_READY
) ? SS_READY
: 0;
162 val
|= (status
& I365_CS_POWERON
) ? SS_POWERON
: 0;
169 static int yenta_Vcc_power(u32 control
)
171 switch (control
& CB_SC_VCC_MASK
) {
172 case CB_SC_VCC_5V
: return 50;
173 case CB_SC_VCC_3V
: return 33;
178 static int yenta_Vpp_power(u32 control
)
180 switch (control
& CB_SC_VPP_MASK
) {
181 case CB_SC_VPP_12V
: return 120;
182 case CB_SC_VPP_5V
: return 50;
183 case CB_SC_VPP_3V
: return 33;
188 static int yenta_get_socket(struct pcmcia_socket
*sock
, socket_state_t
*state
)
190 struct yenta_socket
*socket
= container_of(sock
, struct yenta_socket
, socket
);
194 control
= cb_readl(socket
, CB_SOCKET_CONTROL
);
196 state
->Vcc
= yenta_Vcc_power(control
);
197 state
->Vpp
= yenta_Vpp_power(control
);
198 state
->io_irq
= socket
->io_irq
;
200 if (cb_readl(socket
, CB_SOCKET_STATE
) & CB_CBCARD
) {
201 u16 bridge
= config_readw(socket
, CB_BRIDGE_CONTROL
);
202 if (bridge
& CB_BRIDGE_CRST
)
203 state
->flags
|= SS_RESET
;
207 /* 16-bit card state.. */
208 reg
= exca_readb(socket
, I365_POWER
);
209 state
->flags
= (reg
& I365_PWR_AUTO
) ? SS_PWR_AUTO
: 0;
210 state
->flags
|= (reg
& I365_PWR_OUT
) ? SS_OUTPUT_ENA
: 0;
212 reg
= exca_readb(socket
, I365_INTCTL
);
213 state
->flags
|= (reg
& I365_PC_RESET
) ? 0 : SS_RESET
;
214 state
->flags
|= (reg
& I365_PC_IOCARD
) ? SS_IOCARD
: 0;
216 reg
= exca_readb(socket
, I365_CSCINT
);
217 state
->csc_mask
= (reg
& I365_CSC_DETECT
) ? SS_DETECT
: 0;
218 if (state
->flags
& SS_IOCARD
) {
219 state
->csc_mask
|= (reg
& I365_CSC_STSCHG
) ? SS_STSCHG
: 0;
221 state
->csc_mask
|= (reg
& I365_CSC_BVD1
) ? SS_BATDEAD
: 0;
222 state
->csc_mask
|= (reg
& I365_CSC_BVD2
) ? SS_BATWARN
: 0;
223 state
->csc_mask
|= (reg
& I365_CSC_READY
) ? SS_READY
: 0;
229 static void yenta_set_power(struct yenta_socket
*socket
, socket_state_t
*state
)
231 u32 reg
= 0; /* CB_SC_STPCLK? */
232 switch (state
->Vcc
) {
233 case 33: reg
= CB_SC_VCC_3V
; break;
234 case 50: reg
= CB_SC_VCC_5V
; break;
235 default: reg
= 0; break;
237 switch (state
->Vpp
) {
238 case 33: reg
|= CB_SC_VPP_3V
; break;
239 case 50: reg
|= CB_SC_VPP_5V
; break;
240 case 120: reg
|= CB_SC_VPP_12V
; break;
242 if (reg
!= cb_readl(socket
, CB_SOCKET_CONTROL
))
243 cb_writel(socket
, CB_SOCKET_CONTROL
, reg
);
246 static int yenta_set_socket(struct pcmcia_socket
*sock
, socket_state_t
*state
)
248 struct yenta_socket
*socket
= container_of(sock
, struct yenta_socket
, socket
);
251 if (state
->flags
& SS_DEBOUNCED
) {
252 /* The insertion debounce period has ended. Clear any pending insertion events */
253 socket
->events
&= ~SS_DETECT
;
254 state
->flags
&= ~SS_DEBOUNCED
; /* SS_DEBOUNCED is oneshot */
256 yenta_set_power(socket
, state
);
257 socket
->io_irq
= state
->io_irq
;
258 bridge
= config_readw(socket
, CB_BRIDGE_CONTROL
) & ~(CB_BRIDGE_CRST
| CB_BRIDGE_INTR
);
259 if (cb_readl(socket
, CB_SOCKET_STATE
) & CB_CBCARD
) {
261 bridge
|= (state
->flags
& SS_RESET
) ? CB_BRIDGE_CRST
: 0;
263 /* ISA interrupt control? */
264 intr
= exca_readb(socket
, I365_INTCTL
);
265 intr
= (intr
& ~0xf);
266 if (!socket
->cb_irq
) {
267 intr
|= state
->io_irq
;
268 bridge
|= CB_BRIDGE_INTR
;
270 exca_writeb(socket
, I365_INTCTL
, intr
);
274 reg
= exca_readb(socket
, I365_INTCTL
) & (I365_RING_ENA
| I365_INTR_ENA
);
275 reg
|= (state
->flags
& SS_RESET
) ? 0 : I365_PC_RESET
;
276 reg
|= (state
->flags
& SS_IOCARD
) ? I365_PC_IOCARD
: 0;
277 if (state
->io_irq
!= socket
->cb_irq
) {
278 reg
|= state
->io_irq
;
279 bridge
|= CB_BRIDGE_INTR
;
281 exca_writeb(socket
, I365_INTCTL
, reg
);
283 reg
= exca_readb(socket
, I365_POWER
) & (I365_VCC_MASK
|I365_VPP1_MASK
);
284 reg
|= I365_PWR_NORESET
;
285 if (state
->flags
& SS_PWR_AUTO
) reg
|= I365_PWR_AUTO
;
286 if (state
->flags
& SS_OUTPUT_ENA
) reg
|= I365_PWR_OUT
;
287 if (exca_readb(socket
, I365_POWER
) != reg
)
288 exca_writeb(socket
, I365_POWER
, reg
);
290 /* CSC interrupt: no ISA irq for CSC */
291 reg
= I365_CSC_DETECT
;
292 if (state
->flags
& SS_IOCARD
) {
293 if (state
->csc_mask
& SS_STSCHG
) reg
|= I365_CSC_STSCHG
;
295 if (state
->csc_mask
& SS_BATDEAD
) reg
|= I365_CSC_BVD1
;
296 if (state
->csc_mask
& SS_BATWARN
) reg
|= I365_CSC_BVD2
;
297 if (state
->csc_mask
& SS_READY
) reg
|= I365_CSC_READY
;
299 exca_writeb(socket
, I365_CSCINT
, reg
);
300 exca_readb(socket
, I365_CSC
);
302 config_writew(socket
, CB_BRIDGE_CONTROL
, bridge
);
303 /* Socket event mask: get card insert/remove events.. */
304 cb_writel(socket
, CB_SOCKET_EVENT
, -1);
305 cb_writel(socket
, CB_SOCKET_MASK
, CB_CDMASK
);
309 static int yenta_set_io_map(struct pcmcia_socket
*sock
, struct pccard_io_map
*io
)
311 struct yenta_socket
*socket
= container_of(sock
, struct yenta_socket
, socket
);
313 unsigned char ioctl
, addr
, enable
;
320 enable
= I365_ENA_IO(map
);
321 addr
= exca_readb(socket
, I365_ADDRWIN
);
323 /* Disable the window before changing it.. */
326 exca_writeb(socket
, I365_ADDRWIN
, addr
);
329 exca_writew(socket
, I365_IO(map
)+I365_W_START
, io
->start
);
330 exca_writew(socket
, I365_IO(map
)+I365_W_STOP
, io
->stop
);
332 ioctl
= exca_readb(socket
, I365_IOCTL
) & ~I365_IOCTL_MASK(map
);
333 if (io
->flags
& MAP_0WS
) ioctl
|= I365_IOCTL_0WS(map
);
334 if (io
->flags
& MAP_16BIT
) ioctl
|= I365_IOCTL_16BIT(map
);
335 if (io
->flags
& MAP_AUTOSZ
) ioctl
|= I365_IOCTL_IOCS16(map
);
336 exca_writeb(socket
, I365_IOCTL
, ioctl
);
338 if (io
->flags
& MAP_ACTIVE
)
339 exca_writeb(socket
, I365_ADDRWIN
, addr
| enable
);
343 static int yenta_set_mem_map(struct pcmcia_socket
*sock
, struct pccard_mem_map
*mem
)
345 struct yenta_socket
*socket
= container_of(sock
, struct yenta_socket
, socket
);
347 unsigned char addr
, enable
;
348 unsigned int start
, stop
, card_start
;
352 start
= mem
->sys_start
;
353 stop
= mem
->sys_stop
;
354 card_start
= mem
->card_start
;
356 if (map
> 4 || start
> stop
|| ((start
^ stop
) >> 24) ||
357 (card_start
>> 26) || mem
->speed
> 1000)
360 enable
= I365_ENA_MEM(map
);
361 addr
= exca_readb(socket
, I365_ADDRWIN
);
364 exca_writeb(socket
, I365_ADDRWIN
, addr
);
367 exca_writeb(socket
, CB_MEM_PAGE(map
), start
>> 24);
369 word
= (start
>> 12) & 0x0fff;
370 if (mem
->flags
& MAP_16BIT
)
371 word
|= I365_MEM_16BIT
;
372 if (mem
->flags
& MAP_0WS
)
373 word
|= I365_MEM_0WS
;
374 exca_writew(socket
, I365_MEM(map
) + I365_W_START
, word
);
376 word
= (stop
>> 12) & 0x0fff;
377 switch (to_cycles(mem
->speed
)) {
379 case 1: word
|= I365_MEM_WS0
; break;
380 case 2: word
|= I365_MEM_WS1
; break;
381 default: word
|= I365_MEM_WS1
| I365_MEM_WS0
; break;
383 exca_writew(socket
, I365_MEM(map
) + I365_W_STOP
, word
);
385 word
= ((card_start
- start
) >> 12) & 0x3fff;
386 if (mem
->flags
& MAP_WRPROT
)
387 word
|= I365_MEM_WRPROT
;
388 if (mem
->flags
& MAP_ATTRIB
)
389 word
|= I365_MEM_REG
;
390 exca_writew(socket
, I365_MEM(map
) + I365_W_OFF
, word
);
392 if (mem
->flags
& MAP_ACTIVE
)
393 exca_writeb(socket
, I365_ADDRWIN
, addr
| enable
);
398 static unsigned int yenta_events(struct yenta_socket
*socket
)
404 /* Clear interrupt status for the event */
405 cb_event
= cb_readl(socket
, CB_SOCKET_EVENT
);
406 cb_writel(socket
, CB_SOCKET_EVENT
, cb_event
);
408 csc
= exca_readb(socket
, I365_CSC
);
410 events
= (cb_event
& (CB_CD1EVENT
| CB_CD2EVENT
)) ? SS_DETECT
: 0 ;
411 events
|= (csc
& I365_CSC_DETECT
) ? SS_DETECT
: 0;
412 if (exca_readb(socket
, I365_INTCTL
) & I365_PC_IOCARD
) {
413 events
|= (csc
& I365_CSC_STSCHG
) ? SS_STSCHG
: 0;
415 events
|= (csc
& I365_CSC_BVD1
) ? SS_BATDEAD
: 0;
416 events
|= (csc
& I365_CSC_BVD2
) ? SS_BATWARN
: 0;
417 events
|= (csc
& I365_CSC_READY
) ? SS_READY
: 0;
423 static void yenta_bh(void *data
)
425 struct yenta_socket
*socket
= data
;
428 spin_lock_irq(&socket
->event_lock
);
429 events
= socket
->events
;
431 spin_unlock_irq(&socket
->event_lock
);
433 socket
->handler(socket
->info
, events
);
436 static irqreturn_t
yenta_interrupt(int irq
, void *dev_id
, struct pt_regs
*regs
)
439 struct yenta_socket
*socket
= (struct yenta_socket
*) dev_id
;
441 events
= yenta_events(socket
);
443 spin_lock(&socket
->event_lock
);
444 socket
->events
|= events
;
445 spin_unlock(&socket
->event_lock
);
446 schedule_work(&socket
->tq_task
);
452 static void yenta_interrupt_wrapper(unsigned long data
)
454 struct yenta_socket
*socket
= (struct yenta_socket
*) data
;
456 yenta_interrupt(0, (void *)socket
, NULL
);
457 socket
->poll_timer
.expires
= jiffies
+ HZ
;
458 add_timer(&socket
->poll_timer
);
462 * Only probe "regular" interrupts, don't
463 * touch dangerous spots like the mouse irq,
464 * because there are mice that apparently
465 * get really confused if they get fondled
468 * Default to 11, 10, 9, 7, 6, 5, 4, 3.
470 static u32 isa_interrupts
= 0x0ef8;
472 static unsigned int yenta_probe_irq(struct yenta_socket
*socket
, u32 isa_irq_mask
)
479 /* Set up ISA irq routing to probe the ISA irqs.. */
480 bridge_ctrl
= config_readw(socket
, CB_BRIDGE_CONTROL
);
481 if (!(bridge_ctrl
& CB_BRIDGE_INTR
)) {
482 bridge_ctrl
|= CB_BRIDGE_INTR
;
483 config_writew(socket
, CB_BRIDGE_CONTROL
, bridge_ctrl
);
487 * Probe for usable interrupts using the force
488 * register to generate bogus card status events.
490 cb_writel(socket
, CB_SOCKET_EVENT
, -1);
491 cb_writel(socket
, CB_SOCKET_MASK
, CB_CSTSMASK
);
492 exca_writeb(socket
, I365_CSCINT
, 0);
493 val
= probe_irq_on() & isa_irq_mask
;
494 for (i
= 1; i
< 16; i
++) {
495 if (!((val
>> i
) & 1))
497 exca_writeb(socket
, I365_CSCINT
, I365_CSC_STSCHG
| (i
<< 4));
498 cb_writel(socket
, CB_SOCKET_FORCE
, CB_FCARDSTS
);
500 cb_writel(socket
, CB_SOCKET_EVENT
, -1);
502 cb_writel(socket
, CB_SOCKET_MASK
, 0);
503 exca_writeb(socket
, I365_CSCINT
, 0);
505 mask
= probe_irq_mask(val
) & 0xffff;
507 bridge_ctrl
&= ~CB_BRIDGE_INTR
;
508 config_writew(socket
, CB_BRIDGE_CONTROL
, bridge_ctrl
);
514 * Set static data that doesn't need re-initializing..
516 static void yenta_get_socket_capabilities(struct yenta_socket
*socket
, u32 isa_irq_mask
)
518 socket
->socket
.features
|= SS_CAP_PAGE_REGS
| SS_CAP_PCCARD
| SS_CAP_CARDBUS
;
519 socket
->socket
.map_size
= 0x1000;
520 socket
->socket
.pci_irq
= socket
->cb_irq
;
521 socket
->socket
.irq_mask
= yenta_probe_irq(socket
, isa_irq_mask
);
522 socket
->socket
.cb_dev
= socket
->dev
;
524 printk("Yenta IRQ list %04x, PCI irq%d\n", socket
->socket
.irq_mask
, socket
->cb_irq
);
528 static void yenta_clear_maps(struct yenta_socket
*socket
)
531 pccard_io_map io
= { 0, 0, 0, 0, 1 };
532 pccard_mem_map mem
= { 0, 0, 0, 0, 0, 0 };
534 mem
.sys_stop
= 0x0fff;
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
);
547 * Initialize the standard cardbus registers
549 static void yenta_config_init(struct yenta_socket
*socket
)
552 struct pci_dev
*dev
= socket
->dev
;
554 pci_set_power_state(socket
->dev
, 0);
556 config_writel(socket
, CB_LEGACY_MODE_BASE
, 0);
557 config_writel(socket
, PCI_BASE_ADDRESS_0
, dev
->resource
[0].start
);
558 config_writew(socket
, PCI_COMMAND
,
564 /* MAGIC NUMBERS! Fixme */
565 config_writeb(socket
, PCI_CACHE_LINE_SIZE
, L1_CACHE_BYTES
/ 4);
566 config_writeb(socket
, PCI_LATENCY_TIMER
, 168);
567 config_writel(socket
, PCI_PRIMARY_BUS
,
568 (176 << 24) | /* sec. latency timer */
569 (dev
->subordinate
->subordinate
<< 16) | /* subordinate bus */
570 (dev
->subordinate
->secondary
<< 8) | /* secondary bus */
571 dev
->subordinate
->primary
); /* primary bus */
574 * Set up the bridging state:
575 * - enable write posting.
576 * - memory window 0 prefetchable, window 1 non-prefetchable
577 * - PCI interrupts enabled if a PCI interrupt exists..
579 bridge
= config_readw(socket
, CB_BRIDGE_CONTROL
);
580 bridge
&= ~(CB_BRIDGE_CRST
| CB_BRIDGE_PREFETCH1
| CB_BRIDGE_INTR
| CB_BRIDGE_ISAEN
| CB_BRIDGE_VGAEN
);
581 bridge
|= CB_BRIDGE_PREFETCH0
| CB_BRIDGE_POSTEN
;
583 bridge
|= CB_BRIDGE_INTR
;
584 config_writew(socket
, CB_BRIDGE_CONTROL
, bridge
);
586 exca_writeb(socket
, I365_GBLCTL
, 0x00);
587 exca_writeb(socket
, I365_GENCTL
, 0x00);
589 /* Redo card voltage interrogation */
590 cb_writel(socket
, CB_SOCKET_FORCE
, CB_CVSTEST
);
593 /* Called at resume and initialization events */
594 static int yenta_init(struct pcmcia_socket
*sock
)
596 struct yenta_socket
*socket
= container_of(sock
, struct yenta_socket
, socket
);
597 yenta_config_init(socket
);
598 yenta_clear_maps(socket
);
600 /* Re-enable interrupts */
601 cb_writel(socket
, CB_SOCKET_MASK
, CB_CDMASK
);
605 static int yenta_suspend(struct pcmcia_socket
*sock
)
607 struct yenta_socket
*socket
= container_of(sock
, struct yenta_socket
, socket
);
609 yenta_set_socket(sock
, &dead_socket
);
611 /* Disable interrupts */
612 cb_writel(socket
, CB_SOCKET_MASK
, 0x0);
615 * This does not work currently. The controller
616 * loses too much information during D3 to come up
617 * cleanly. We should probably fix yenta_init()
618 * to update all the critical registers, notably
619 * the IO and MEM bridging region data.. That is
620 * something that pci_set_power_state() should
621 * probably know about bridges anyway.
623 pci_set_power_state(socket->dev, 3);
630 * Use an adaptive allocation for the memory resource,
631 * sometimes the memory behind pci bridges is limited:
632 * 1/8 of the size of the io window of the parent.
633 * max 4 MB, min 16 kB.
635 #define BRIDGE_MEM_MAX 4*1024*1024
636 #define BRIDGE_MEM_MIN 16*1024
638 #define BRIDGE_IO_MAX 256
639 #define BRIDGE_IO_MIN 32
641 static void yenta_allocate_res(struct yenta_socket
*socket
, int nr
, unsigned type
)
644 struct resource
*root
, *res
;
646 u32 align
, size
, min
;
650 /* The granularity of the memory limit is 4kB, on IO it's 4 bytes */
652 if (type
& IORESOURCE_IO
)
655 offset
= 0x1c + 8*nr
;
656 bus
= socket
->dev
->subordinate
;
657 res
= socket
->dev
->resource
+ PCI_BRIDGE_RESOURCES
+ nr
;
658 res
->name
= bus
->name
;
662 root
= pci_find_parent_resource(socket
->dev
, res
);
667 start
= config_readl(socket
, offset
) & mask
;
668 end
= config_readl(socket
, offset
+4) | ~mask
;
669 if (start
&& end
> start
) {
672 if (request_resource(root
, res
) == 0)
674 printk(KERN_INFO
"yenta %s: Preassigned resource %d busy, reconfiguring...\n",
675 socket
->dev
->slot_name
, nr
);
676 res
->start
= res
->end
= 0;
679 if (type
& IORESOURCE_IO
) {
681 size
= BRIDGE_IO_MAX
;
683 start
= PCIBIOS_MIN_IO
;
686 unsigned long avail
= root
->end
- root
->start
;
688 size
= BRIDGE_MEM_MAX
;
689 if (size
> avail
/8) {
691 /* round size down to next power of 2 */
693 while ((size
/= 2) != 0)
697 if (size
< BRIDGE_MEM_MIN
)
698 size
= BRIDGE_MEM_MIN
;
699 min
= BRIDGE_MEM_MIN
;
701 start
= PCIBIOS_MIN_MEM
;
706 if (allocate_resource(root
, res
, size
, start
, end
, align
, NULL
, NULL
)==0) {
707 config_writel(socket
, offset
, res
->start
);
708 config_writel(socket
, offset
+4, res
->end
);
713 } while (size
>= min
);
714 printk(KERN_INFO
"yenta %s: no resource of type %x available, trying to continue...\n",
715 socket
->dev
->slot_name
, type
);
716 res
->start
= res
->end
= 0;
720 * Allocate the bridge mappings for the device..
722 static void yenta_allocate_resources(struct yenta_socket
*socket
)
724 yenta_allocate_res(socket
, 0, IORESOURCE_MEM
|IORESOURCE_PREFETCH
);
725 yenta_allocate_res(socket
, 1, IORESOURCE_MEM
);
726 yenta_allocate_res(socket
, 2, IORESOURCE_IO
);
727 yenta_allocate_res(socket
, 3, IORESOURCE_IO
); /* PCI isn't clever enough to use this one yet */
732 * Free the bridge mappings for the device..
734 static void yenta_free_resources(struct yenta_socket
*socket
)
738 struct resource
*res
;
739 res
= socket
->dev
->resource
+ PCI_BRIDGE_RESOURCES
+ i
;
740 if (res
->start
!= 0 && res
->end
!= 0)
741 release_resource(res
);
742 res
->start
= res
->end
= 0;
748 * Close it down - release our resources and go home..
750 static void yenta_close(struct pci_dev
*dev
)
752 struct yenta_socket
*sock
= pci_get_drvdata(dev
);
754 /* we don't want a dying socket registered */
755 pcmcia_unregister_socket(&sock
->socket
);
757 /* Disable all events so we don't die in an IRQ storm */
758 cb_writel(sock
, CB_SOCKET_MASK
, 0x0);
759 exca_writeb(sock
, I365_CSCINT
, 0);
762 free_irq(sock
->cb_irq
, sock
);
764 del_timer_sync(&sock
->poll_timer
);
768 yenta_free_resources(sock
);
770 pci_set_drvdata(dev
, NULL
);
774 static int yenta_register_callback(struct pcmcia_socket
*sock
, void (*handler
)(void *, unsigned int), void * info
)
776 struct yenta_socket
*socket
= container_of(sock
, struct yenta_socket
, socket
);
778 socket
->handler
= handler
;
784 static struct pccard_operations yenta_socket_operations
= {
785 .owner
= THIS_MODULE
,
787 .suspend
= yenta_suspend
,
788 .register_callback
= yenta_register_callback
,
789 .get_status
= yenta_get_status
,
790 .get_socket
= yenta_get_socket
,
791 .set_socket
= yenta_set_socket
,
792 .set_io_map
= yenta_set_io_map
,
793 .set_mem_map
= yenta_set_mem_map
,
801 * Different cardbus controllers have slightly different
802 * initialization sequences etc details. List them here..
804 #define PD(x,y) PCI_VENDOR_ID_##x, PCI_DEVICE_ID_##x##_##y
805 struct cardbus_override_struct
{
806 unsigned short vendor
;
807 unsigned short device
;
808 int (*override
) (struct yenta_socket
*socket
);
809 } cardbus_override
[] = {
810 { PD(TI
,1130), &ti113x_override
},
811 { PD(TI
,1031), &ti_override
},
812 { PD(TI
,1131), &ti113x_override
},
813 { PD(TI
,1250), &ti1250_override
},
814 { PD(TI
,1220), &ti_override
},
815 { PD(TI
,1221), &ti_override
},
816 { PD(TI
,1210), &ti_override
},
817 { PD(TI
,1450), &ti_override
},
818 { PD(TI
,1225), &ti_override
},
819 { PD(TI
,1251A
), &ti_override
},
820 { PD(TI
,1211), &ti_override
},
821 { PD(TI
,1251B
), &ti_override
},
822 { PD(TI
,1410), ti1250_override
},
823 { PD(TI
,1420), &ti_override
},
824 { PD(TI
,4410), &ti_override
},
825 { PD(TI
,4451), &ti_override
},
827 { PD(RICOH
,RL5C465
), &ricoh_override
},
828 { PD(RICOH
,RL5C466
), &ricoh_override
},
829 { PD(RICOH
,RL5C475
), &ricoh_override
},
830 { PD(RICOH
,RL5C476
), &ricoh_override
},
831 { PD(RICOH
,RL5C478
), &ricoh_override
},
833 { }, /* all zeroes */
838 * Initialize a cardbus controller. Make sure we have a usable
839 * interrupt, and that we can map the cardbus area. Fill in the
840 * socket information structure..
842 static int __devinit
yenta_probe (struct pci_dev
*dev
, const struct pci_device_id
*id
)
844 struct yenta_socket
*socket
;
845 struct cardbus_override_struct
*d
;
847 socket
= kmalloc(sizeof(struct yenta_socket
), GFP_KERNEL
);
850 memset(socket
, 0, sizeof(*socket
));
852 /* prepare pcmcia_socket */
853 socket
->socket
.ss_entry
= ¥ta_socket_operations
;
854 socket
->socket
.dev
.dev
= &dev
->dev
;
855 socket
->socket
.driver_data
= socket
;
857 /* prepare struct yenta_socket */
859 pci_set_drvdata(dev
, socket
);
860 spin_lock_init(&socket
->event_lock
);
863 * Do some basic sanity checking..
865 if (pci_enable_device(dev
))
867 if (!pci_resource_start(dev
, 0)) {
868 printk("No cardbus resource!\n");
873 * Ok, start setup.. Map the cardbus registers,
874 * and request the IRQ.
876 socket
->base
= ioremap(pci_resource_start(dev
, 0), 0x1000);
880 yenta_config_init(socket
);
882 /* Disable all events */
883 cb_writel(socket
, CB_SOCKET_MASK
, 0x0);
885 /* Set up the bridge regions.. */
886 yenta_allocate_resources(socket
);
888 socket
->cb_irq
= dev
->irq
;
890 /* Do we have special options for the device? */
891 d
= cardbus_override
;
892 while (d
->override
) {
893 if ((dev
->vendor
== d
->vendor
) && (dev
->device
== d
->device
)) {
894 int retval
= d
->override(socket
);
901 /* We must finish initialization here */
903 INIT_WORK(&socket
->tq_task
, yenta_bh
, socket
);
905 if (!socket
->cb_irq
|| request_irq(socket
->cb_irq
, yenta_interrupt
, SA_SHIRQ
, socket
->dev
->dev
.name
, socket
)) {
906 /* No IRQ or request_irq failed. Poll */
907 socket
->cb_irq
= 0; /* But zero is a valid IRQ number. */
908 init_timer(&socket
->poll_timer
);
909 socket
->poll_timer
.function
= yenta_interrupt_wrapper
;
910 socket
->poll_timer
.data
= (unsigned long)socket
;
911 socket
->poll_timer
.expires
= jiffies
+ HZ
;
912 add_timer(&socket
->poll_timer
);
915 /* Figure out what the dang thing can do for the PCMCIA layer... */
916 yenta_get_socket_capabilities(socket
, isa_interrupts
);
917 printk("Socket status: %08x\n", cb_readl(socket
, CB_SOCKET_STATE
));
919 /* Register it with the pcmcia layer.. */
920 return pcmcia_register_socket(&socket
->socket
);
924 static int yenta_dev_suspend (struct pci_dev
*dev
, u32 state
)
926 return pcmcia_socket_dev_suspend(&dev
->dev
, state
, 0);
930 static int yenta_dev_resume (struct pci_dev
*dev
)
932 return pcmcia_socket_dev_resume(&dev
->dev
, RESUME_RESTORE_STATE
);
936 static struct pci_device_id yenta_table
[] __devinitdata
= { {
937 .class = PCI_CLASS_BRIDGE_CARDBUS
<< 8,
940 .vendor
= PCI_ANY_ID
,
941 .device
= PCI_ANY_ID
,
942 .subvendor
= PCI_ANY_ID
,
943 .subdevice
= PCI_ANY_ID
,
944 }, { /* all zeroes */ }
946 MODULE_DEVICE_TABLE(pci
, yenta_table
);
949 static struct pci_driver yenta_cardbus_driver
= {
950 .name
= "yenta_cardbus",
951 .id_table
= yenta_table
,
952 .probe
= yenta_probe
,
953 .remove
= __devexit_p(yenta_close
),
954 .suspend
= yenta_dev_suspend
,
955 .resume
= yenta_dev_resume
,
959 static int __init
yenta_socket_init(void)
961 return pci_register_driver (¥ta_cardbus_driver
);
965 static void __exit
yenta_socket_exit (void)
967 pci_unregister_driver (¥ta_cardbus_driver
);
971 module_init(yenta_socket_init
);
972 module_exit(yenta_socket_exit
);
974 MODULE_LICENSE("GPL");