blob: 4145eb83b9b6d708d57a07da1a0633829d3773f8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Regular cardbus driver ("yenta_socket")
3 *
4 * (C) Copyright 1999, 2000 Linus Torvalds
5 *
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
12 */
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>
20
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <pcmcia/cs_types.h>
22#include <pcmcia/ss.h>
23#include <pcmcia/cs.h>
24
25#include <asm/io.h>
26
27#include "yenta_socket.h"
28#include "i82365.h"
29
30static int disable_clkrun;
31module_param(disable_clkrun, bool, 0444);
32MODULE_PARM_DESC(disable_clkrun, "If PC card doesn't function properly, please try this option");
33
Daniel Ritzfa912bc2005-06-23 00:10:12 -070034static int isa_probe = 1;
35module_param(isa_probe, bool, 0444);
36MODULE_PARM_DESC(isa_probe, "If set ISA interrupts are probed (default). Set to N to disable probing");
37
38static int pwr_irqs_off;
39module_param(pwr_irqs_off, bool, 0644);
40MODULE_PARM_DESC(pwr_irqs_off, "Force IRQs off during power-on of slot. Use only when seeing IRQ storms!");
41
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#if 0
43#define debug(x,args...) printk(KERN_DEBUG "%s: " x, __func__ , ##args)
44#else
45#define debug(x,args...)
46#endif
47
48/* Don't ask.. */
49#define to_cycles(ns) ((ns)/120)
50#define to_ns(cycles) ((cycles)*120)
51
Daniel Ritz63e7ebd2005-11-03 21:12:14 +010052/**
53 * yenta PCI irq probing.
54 * currently only used in the TI/EnE initialization code
55 */
56#ifdef CONFIG_YENTA_TI
Linus Torvalds1da177e2005-04-16 15:20:36 -070057static int yenta_probe_cb_irq(struct yenta_socket *socket);
Daniel Ritz63e7ebd2005-11-03 21:12:14 +010058#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60
61static unsigned int override_bios;
62module_param(override_bios, uint, 0000);
63MODULE_PARM_DESC (override_bios, "yenta ignore bios resource allocation");
64
65/*
66 * Generate easy-to-use ways of reading a cardbus sockets
67 * regular memory space ("cb_xxx"), configuration space
68 * ("config_xxx") and compatibility space ("exca_xxxx")
69 */
70static inline u32 cb_readl(struct yenta_socket *socket, unsigned reg)
71{
72 u32 val = readl(socket->base + reg);
73 debug("%p %04x %08x\n", socket, reg, val);
74 return val;
75}
76
77static inline void cb_writel(struct yenta_socket *socket, unsigned reg, u32 val)
78{
79 debug("%p %04x %08x\n", socket, reg, val);
80 writel(val, socket->base + reg);
Daniel Ritzc8751e42005-09-09 13:03:25 -070081 readl(socket->base + reg); /* avoid problems with PCI write posting */
Linus Torvalds1da177e2005-04-16 15:20:36 -070082}
83
84static inline u8 config_readb(struct yenta_socket *socket, unsigned offset)
85{
86 u8 val;
87 pci_read_config_byte(socket->dev, offset, &val);
88 debug("%p %04x %02x\n", socket, offset, val);
89 return val;
90}
91
92static inline u16 config_readw(struct yenta_socket *socket, unsigned offset)
93{
94 u16 val;
95 pci_read_config_word(socket->dev, offset, &val);
96 debug("%p %04x %04x\n", socket, offset, val);
97 return val;
98}
99
100static inline u32 config_readl(struct yenta_socket *socket, unsigned offset)
101{
102 u32 val;
103 pci_read_config_dword(socket->dev, offset, &val);
104 debug("%p %04x %08x\n", socket, offset, val);
105 return val;
106}
107
108static inline void config_writeb(struct yenta_socket *socket, unsigned offset, u8 val)
109{
110 debug("%p %04x %02x\n", socket, offset, val);
111 pci_write_config_byte(socket->dev, offset, val);
112}
113
114static inline void config_writew(struct yenta_socket *socket, unsigned offset, u16 val)
115{
116 debug("%p %04x %04x\n", socket, offset, val);
117 pci_write_config_word(socket->dev, offset, val);
118}
119
120static inline void config_writel(struct yenta_socket *socket, unsigned offset, u32 val)
121{
122 debug("%p %04x %08x\n", socket, offset, val);
123 pci_write_config_dword(socket->dev, offset, val);
124}
125
126static inline u8 exca_readb(struct yenta_socket *socket, unsigned reg)
127{
128 u8 val = readb(socket->base + 0x800 + reg);
129 debug("%p %04x %02x\n", socket, reg, val);
130 return val;
131}
132
133static inline u8 exca_readw(struct yenta_socket *socket, unsigned reg)
134{
135 u16 val;
136 val = readb(socket->base + 0x800 + reg);
137 val |= readb(socket->base + 0x800 + reg + 1) << 8;
138 debug("%p %04x %04x\n", socket, reg, val);
139 return val;
140}
141
142static inline void exca_writeb(struct yenta_socket *socket, unsigned reg, u8 val)
143{
144 debug("%p %04x %02x\n", socket, reg, val);
145 writeb(val, socket->base + 0x800 + reg);
Daniel Ritzc8751e42005-09-09 13:03:25 -0700146 readb(socket->base + 0x800 + reg); /* PCI write posting... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147}
148
149static void exca_writew(struct yenta_socket *socket, unsigned reg, u16 val)
150{
151 debug("%p %04x %04x\n", socket, reg, val);
152 writeb(val, socket->base + 0x800 + reg);
153 writeb(val >> 8, socket->base + 0x800 + reg + 1);
Daniel Ritzc8751e42005-09-09 13:03:25 -0700154
155 /* PCI write posting... */
156 readb(socket->base + 0x800 + reg);
157 readb(socket->base + 0x800 + reg + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158}
159
Linus Torvalds030ee392005-10-28 13:55:08 -0700160static ssize_t show_yenta_registers(struct device *yentadev, struct device_attribute *attr, char *buf)
161{
162 struct pci_dev *dev = to_pci_dev(yentadev);
163 struct yenta_socket *socket = pci_get_drvdata(dev);
164 int offset = 0, i;
165
166 offset = snprintf(buf, PAGE_SIZE, "CB registers:");
167 for (i = 0; i < 0x24; i += 4) {
168 unsigned val;
169 if (!(i & 15))
170 offset += snprintf(buf + offset, PAGE_SIZE - offset, "\n%02x:", i);
171 val = cb_readl(socket, i);
172 offset += snprintf(buf + offset, PAGE_SIZE - offset, " %08x", val);
173 }
174
175 offset += snprintf(buf + offset, PAGE_SIZE - offset, "\n\nExCA registers:");
176 for (i = 0; i < 0x45; i++) {
177 unsigned char val;
178 if (!(i & 7)) {
179 if (i & 8) {
180 memcpy(buf + offset, " -", 2);
181 offset += 2;
182 } else
183 offset += snprintf(buf + offset, PAGE_SIZE - offset, "\n%02x:", i);
184 }
185 val = exca_readb(socket, i);
186 offset += snprintf(buf + offset, PAGE_SIZE - offset, " %02x", val);
187 }
188 buf[offset++] = '\n';
189 return offset;
190}
191
192static DEVICE_ATTR(yenta_registers, S_IRUSR, show_yenta_registers, NULL);
193
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194/*
195 * Ugh, mixed-mode cardbus and 16-bit pccard state: things depend
196 * on what kind of card is inserted..
197 */
198static int yenta_get_status(struct pcmcia_socket *sock, unsigned int *value)
199{
200 struct yenta_socket *socket = container_of(sock, struct yenta_socket, socket);
201 unsigned int val;
202 u32 state = cb_readl(socket, CB_SOCKET_STATE);
203
204 val = (state & CB_3VCARD) ? SS_3VCARD : 0;
205 val |= (state & CB_XVCARD) ? SS_XVCARD : 0;
Daniel Ritzfa912bc2005-06-23 00:10:12 -0700206 val |= (state & (CB_5VCARD | CB_3VCARD | CB_XVCARD | CB_YVCARD)) ? 0 : SS_PENDING;
207 val |= (state & (CB_CDETECT1 | CB_CDETECT2)) ? SS_PENDING : 0;
208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
210 if (state & CB_CBCARD) {
211 val |= SS_CARDBUS;
212 val |= (state & CB_CARDSTS) ? SS_STSCHG : 0;
213 val |= (state & (CB_CDETECT1 | CB_CDETECT2)) ? 0 : SS_DETECT;
214 val |= (state & CB_PWRCYCLE) ? SS_POWERON | SS_READY : 0;
Daniel Ritzfa912bc2005-06-23 00:10:12 -0700215 } else if (state & CB_16BITCARD) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 u8 status = exca_readb(socket, I365_STATUS);
217 val |= ((status & I365_CS_DETECT) == I365_CS_DETECT) ? SS_DETECT : 0;
218 if (exca_readb(socket, I365_INTCTL) & I365_PC_IOCARD) {
219 val |= (status & I365_CS_STSCHG) ? 0 : SS_STSCHG;
220 } else {
221 val |= (status & I365_CS_BVD1) ? 0 : SS_BATDEAD;
222 val |= (status & I365_CS_BVD2) ? 0 : SS_BATWARN;
223 }
224 val |= (status & I365_CS_WRPROT) ? SS_WRPROT : 0;
225 val |= (status & I365_CS_READY) ? SS_READY : 0;
226 val |= (status & I365_CS_POWERON) ? SS_POWERON : 0;
227 }
228
229 *value = val;
230 return 0;
231}
232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233static void yenta_set_power(struct yenta_socket *socket, socket_state_t *state)
234{
Daniel Ritzea2f1592005-09-06 15:16:50 -0700235 /* some birdges require to use the ExCA registers to power 16bit cards */
236 if (!(cb_readl(socket, CB_SOCKET_STATE) & CB_CBCARD) &&
237 (socket->flags & YENTA_16BIT_POWER_EXCA)) {
238 u8 reg, old;
239 reg = old = exca_readb(socket, I365_POWER);
240 reg &= ~(I365_VCC_MASK | I365_VPP1_MASK | I365_VPP2_MASK);
241
242 /* i82365SL-DF style */
243 if (socket->flags & YENTA_16BIT_POWER_DF) {
244 switch (state->Vcc) {
245 case 33: reg |= I365_VCC_3V; break;
246 case 50: reg |= I365_VCC_5V; break;
247 default: reg = 0; break;
248 }
249 switch (state->Vpp) {
250 case 33:
251 case 50: reg |= I365_VPP1_5V; break;
252 case 120: reg |= I365_VPP1_12V; break;
253 }
254 } else {
255 /* i82365SL-B style */
256 switch (state->Vcc) {
257 case 50: reg |= I365_VCC_5V; break;
258 default: reg = 0; break;
259 }
260 switch (state->Vpp) {
261 case 50: reg |= I365_VPP1_5V | I365_VPP2_5V; break;
262 case 120: reg |= I365_VPP1_12V | I365_VPP2_12V; break;
263 }
264 }
265
266 if (reg != old)
267 exca_writeb(socket, I365_POWER, reg);
268 } else {
269 u32 reg = 0; /* CB_SC_STPCLK? */
270 switch (state->Vcc) {
271 case 33: reg = CB_SC_VCC_3V; break;
272 case 50: reg = CB_SC_VCC_5V; break;
273 default: reg = 0; break;
274 }
275 switch (state->Vpp) {
276 case 33: reg |= CB_SC_VPP_3V; break;
277 case 50: reg |= CB_SC_VPP_5V; break;
278 case 120: reg |= CB_SC_VPP_12V; break;
279 }
280 if (reg != cb_readl(socket, CB_SOCKET_CONTROL))
281 cb_writel(socket, CB_SOCKET_CONTROL, reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283}
284
285static int yenta_set_socket(struct pcmcia_socket *sock, socket_state_t *state)
286{
287 struct yenta_socket *socket = container_of(sock, struct yenta_socket, socket);
288 u16 bridge;
289
290 yenta_set_power(socket, state);
291 socket->io_irq = state->io_irq;
292 bridge = config_readw(socket, CB_BRIDGE_CONTROL) & ~(CB_BRIDGE_CRST | CB_BRIDGE_INTR);
293 if (cb_readl(socket, CB_SOCKET_STATE) & CB_CBCARD) {
294 u8 intr;
295 bridge |= (state->flags & SS_RESET) ? CB_BRIDGE_CRST : 0;
296
297 /* ISA interrupt control? */
298 intr = exca_readb(socket, I365_INTCTL);
299 intr = (intr & ~0xf);
300 if (!socket->cb_irq) {
301 intr |= state->io_irq;
302 bridge |= CB_BRIDGE_INTR;
303 }
304 exca_writeb(socket, I365_INTCTL, intr);
305 } else {
306 u8 reg;
307
308 reg = exca_readb(socket, I365_INTCTL) & (I365_RING_ENA | I365_INTR_ENA);
309 reg |= (state->flags & SS_RESET) ? 0 : I365_PC_RESET;
310 reg |= (state->flags & SS_IOCARD) ? I365_PC_IOCARD : 0;
311 if (state->io_irq != socket->cb_irq) {
312 reg |= state->io_irq;
313 bridge |= CB_BRIDGE_INTR;
314 }
315 exca_writeb(socket, I365_INTCTL, reg);
316
317 reg = exca_readb(socket, I365_POWER) & (I365_VCC_MASK|I365_VPP1_MASK);
318 reg |= I365_PWR_NORESET;
319 if (state->flags & SS_PWR_AUTO) reg |= I365_PWR_AUTO;
320 if (state->flags & SS_OUTPUT_ENA) reg |= I365_PWR_OUT;
321 if (exca_readb(socket, I365_POWER) != reg)
322 exca_writeb(socket, I365_POWER, reg);
323
324 /* CSC interrupt: no ISA irq for CSC */
325 reg = I365_CSC_DETECT;
326 if (state->flags & SS_IOCARD) {
327 if (state->csc_mask & SS_STSCHG) reg |= I365_CSC_STSCHG;
328 } else {
329 if (state->csc_mask & SS_BATDEAD) reg |= I365_CSC_BVD1;
330 if (state->csc_mask & SS_BATWARN) reg |= I365_CSC_BVD2;
331 if (state->csc_mask & SS_READY) reg |= I365_CSC_READY;
332 }
333 exca_writeb(socket, I365_CSCINT, reg);
334 exca_readb(socket, I365_CSC);
335 if(sock->zoom_video)
336 sock->zoom_video(sock, state->flags & SS_ZVCARD);
337 }
338 config_writew(socket, CB_BRIDGE_CONTROL, bridge);
339 /* Socket event mask: get card insert/remove events.. */
340 cb_writel(socket, CB_SOCKET_EVENT, -1);
341 cb_writel(socket, CB_SOCKET_MASK, CB_CDMASK);
342 return 0;
343}
344
345static int yenta_set_io_map(struct pcmcia_socket *sock, struct pccard_io_map *io)
346{
347 struct yenta_socket *socket = container_of(sock, struct yenta_socket, socket);
348 int map;
349 unsigned char ioctl, addr, enable;
350
351 map = io->map;
352
353 if (map > 1)
354 return -EINVAL;
355
356 enable = I365_ENA_IO(map);
357 addr = exca_readb(socket, I365_ADDRWIN);
358
359 /* Disable the window before changing it.. */
360 if (addr & enable) {
361 addr &= ~enable;
362 exca_writeb(socket, I365_ADDRWIN, addr);
363 }
364
365 exca_writew(socket, I365_IO(map)+I365_W_START, io->start);
366 exca_writew(socket, I365_IO(map)+I365_W_STOP, io->stop);
367
368 ioctl = exca_readb(socket, I365_IOCTL) & ~I365_IOCTL_MASK(map);
369 if (io->flags & MAP_0WS) ioctl |= I365_IOCTL_0WS(map);
370 if (io->flags & MAP_16BIT) ioctl |= I365_IOCTL_16BIT(map);
371 if (io->flags & MAP_AUTOSZ) ioctl |= I365_IOCTL_IOCS16(map);
372 exca_writeb(socket, I365_IOCTL, ioctl);
373
374 if (io->flags & MAP_ACTIVE)
375 exca_writeb(socket, I365_ADDRWIN, addr | enable);
376 return 0;
377}
378
379static int yenta_set_mem_map(struct pcmcia_socket *sock, struct pccard_mem_map *mem)
380{
381 struct yenta_socket *socket = container_of(sock, struct yenta_socket, socket);
382 struct pci_bus_region region;
383 int map;
384 unsigned char addr, enable;
385 unsigned int start, stop, card_start;
386 unsigned short word;
387
388 pcibios_resource_to_bus(socket->dev, &region, mem->res);
389
390 map = mem->map;
391 start = region.start;
392 stop = region.end;
393 card_start = mem->card_start;
394
395 if (map > 4 || start > stop || ((start ^ stop) >> 24) ||
396 (card_start >> 26) || mem->speed > 1000)
397 return -EINVAL;
398
399 enable = I365_ENA_MEM(map);
400 addr = exca_readb(socket, I365_ADDRWIN);
401 if (addr & enable) {
402 addr &= ~enable;
403 exca_writeb(socket, I365_ADDRWIN, addr);
404 }
405
406 exca_writeb(socket, CB_MEM_PAGE(map), start >> 24);
407
408 word = (start >> 12) & 0x0fff;
409 if (mem->flags & MAP_16BIT)
410 word |= I365_MEM_16BIT;
411 if (mem->flags & MAP_0WS)
412 word |= I365_MEM_0WS;
413 exca_writew(socket, I365_MEM(map) + I365_W_START, word);
414
415 word = (stop >> 12) & 0x0fff;
416 switch (to_cycles(mem->speed)) {
417 case 0: break;
418 case 1: word |= I365_MEM_WS0; break;
419 case 2: word |= I365_MEM_WS1; break;
420 default: word |= I365_MEM_WS1 | I365_MEM_WS0; break;
421 }
422 exca_writew(socket, I365_MEM(map) + I365_W_STOP, word);
423
424 word = ((card_start - start) >> 12) & 0x3fff;
425 if (mem->flags & MAP_WRPROT)
426 word |= I365_MEM_WRPROT;
427 if (mem->flags & MAP_ATTRIB)
428 word |= I365_MEM_REG;
429 exca_writew(socket, I365_MEM(map) + I365_W_OFF, word);
430
431 if (mem->flags & MAP_ACTIVE)
432 exca_writeb(socket, I365_ADDRWIN, addr | enable);
433 return 0;
434}
435
436
Daniel Ritzfa912bc2005-06-23 00:10:12 -0700437
438static irqreturn_t yenta_interrupt(int irq, void *dev_id, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439{
Daniel Ritzfa912bc2005-06-23 00:10:12 -0700440 unsigned int events;
441 struct yenta_socket *socket = (struct yenta_socket *) dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 u8 csc;
443 u32 cb_event;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
445 /* Clear interrupt status for the event */
446 cb_event = cb_readl(socket, CB_SOCKET_EVENT);
447 cb_writel(socket, CB_SOCKET_EVENT, cb_event);
448
449 csc = exca_readb(socket, I365_CSC);
450
Daniel Ritze4115802005-09-20 14:17:37 -0700451 if (!(cb_event || csc))
452 return IRQ_NONE;
453
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 events = (cb_event & (CB_CD1EVENT | CB_CD2EVENT)) ? SS_DETECT : 0 ;
455 events |= (csc & I365_CSC_DETECT) ? SS_DETECT : 0;
456 if (exca_readb(socket, I365_INTCTL) & I365_PC_IOCARD) {
457 events |= (csc & I365_CSC_STSCHG) ? SS_STSCHG : 0;
458 } else {
459 events |= (csc & I365_CSC_BVD1) ? SS_BATDEAD : 0;
460 events |= (csc & I365_CSC_BVD2) ? SS_BATWARN : 0;
461 events |= (csc & I365_CSC_READY) ? SS_READY : 0;
462 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
Daniel Ritzfa912bc2005-06-23 00:10:12 -0700464 if (events)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 pcmcia_parse_events(&socket->socket, events);
Daniel Ritzfa912bc2005-06-23 00:10:12 -0700466
Daniel Ritze4115802005-09-20 14:17:37 -0700467 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468}
469
470static void yenta_interrupt_wrapper(unsigned long data)
471{
472 struct yenta_socket *socket = (struct yenta_socket *) data;
473
474 yenta_interrupt(0, (void *)socket, NULL);
475 socket->poll_timer.expires = jiffies + HZ;
476 add_timer(&socket->poll_timer);
477}
478
479static void yenta_clear_maps(struct yenta_socket *socket)
480{
481 int i;
482 struct resource res = { .start = 0, .end = 0x0fff };
483 pccard_io_map io = { 0, 0, 0, 0, 1 };
484 pccard_mem_map mem = { .res = &res, };
485
486 yenta_set_socket(&socket->socket, &dead_socket);
487 for (i = 0; i < 2; i++) {
488 io.map = i;
489 yenta_set_io_map(&socket->socket, &io);
490 }
491 for (i = 0; i < 5; i++) {
492 mem.map = i;
493 yenta_set_mem_map(&socket->socket, &mem);
494 }
495}
496
Daniel Ritzfa912bc2005-06-23 00:10:12 -0700497/* redoes voltage interrogation if required */
498static void yenta_interrogate(struct yenta_socket *socket)
499{
500 u32 state;
501
502 state = cb_readl(socket, CB_SOCKET_STATE);
503 if (!(state & (CB_5VCARD | CB_3VCARD | CB_XVCARD | CB_YVCARD)) ||
504 (state & (CB_CDETECT1 | CB_CDETECT2 | CB_NOTACARD | CB_BADVCCREQ)) ||
505 ((state & (CB_16BITCARD | CB_CBCARD)) == (CB_16BITCARD | CB_CBCARD)))
506 cb_writel(socket, CB_SOCKET_FORCE, CB_CVSTEST);
507}
508
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509/* Called at resume and initialization events */
510static int yenta_sock_init(struct pcmcia_socket *sock)
511{
512 struct yenta_socket *socket = container_of(sock, struct yenta_socket, socket);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
514 exca_writeb(socket, I365_GBLCTL, 0x00);
515 exca_writeb(socket, I365_GENCTL, 0x00);
516
517 /* Redo card voltage interrogation */
Daniel Ritzfa912bc2005-06-23 00:10:12 -0700518 yenta_interrogate(socket);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
520 yenta_clear_maps(socket);
521
522 if (socket->type && socket->type->sock_init)
523 socket->type->sock_init(socket);
524
525 /* Re-enable CSC interrupts */
526 cb_writel(socket, CB_SOCKET_MASK, CB_CDMASK);
527
528 return 0;
529}
530
531static int yenta_sock_suspend(struct pcmcia_socket *sock)
532{
533 struct yenta_socket *socket = container_of(sock, struct yenta_socket, socket);
534
535 /* Disable CSC interrupts */
536 cb_writel(socket, CB_SOCKET_MASK, 0x0);
537
538 return 0;
539}
540
541/*
542 * Use an adaptive allocation for the memory resource,
543 * sometimes the memory behind pci bridges is limited:
544 * 1/8 of the size of the io window of the parent.
Dominik Brodowskieb0a90b2005-07-12 13:58:17 -0700545 * max 4 MB, min 16 kB. We try very hard to not get below
546 * the "ACC" values, though.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 */
548#define BRIDGE_MEM_MAX 4*1024*1024
Dominik Brodowskieb0a90b2005-07-12 13:58:17 -0700549#define BRIDGE_MEM_ACC 128*1024
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550#define BRIDGE_MEM_MIN 16*1024
551
Dominik Brodowskieb0a90b2005-07-12 13:58:17 -0700552#define BRIDGE_IO_MAX 512
553#define BRIDGE_IO_ACC 256
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554#define BRIDGE_IO_MIN 32
555
556#ifndef PCIBIOS_MIN_CARDBUS_IO
557#define PCIBIOS_MIN_CARDBUS_IO PCIBIOS_MIN_IO
558#endif
559
Dominik Brodowskieb0a90b2005-07-12 13:58:17 -0700560static int yenta_search_one_res(struct resource *root, struct resource *res,
561 u32 min)
562{
563 u32 align, size, start, end;
564
565 if (res->flags & IORESOURCE_IO) {
566 align = 1024;
567 size = BRIDGE_IO_MAX;
568 start = PCIBIOS_MIN_CARDBUS_IO;
569 end = ~0U;
570 } else {
571 unsigned long avail = root->end - root->start;
572 int i;
573 size = BRIDGE_MEM_MAX;
574 if (size > avail/8) {
575 size=(avail+1)/8;
576 /* round size down to next power of 2 */
577 i = 0;
578 while ((size /= 2) != 0)
579 i++;
580 size = 1 << i;
581 }
582 if (size < min)
583 size = min;
584 align = size;
585 start = PCIBIOS_MIN_MEM;
586 end = ~0U;
587 }
588
589 do {
590 if (allocate_resource(root, res, size, start, end, align,
591 NULL, NULL)==0) {
592 return 1;
593 }
594 size = size/2;
595 align = size;
596 } while (size >= min);
597
598 return 0;
599}
600
601
602static int yenta_search_res(struct yenta_socket *socket, struct resource *res,
603 u32 min)
604{
605 int i;
606 for (i=0; i<PCI_BUS_NUM_RESOURCES; i++) {
607 struct resource * root = socket->dev->bus->resource[i];
608 if (!root)
609 continue;
610
611 if ((res->flags ^ root->flags) &
612 (IORESOURCE_IO | IORESOURCE_MEM | IORESOURCE_PREFETCH))
613 continue; /* Wrong type */
614
615 if (yenta_search_one_res(root, res, min))
616 return 1;
617 }
618 return 0;
619}
620
Dominik Brodowskib3743fa2005-09-09 13:03:23 -0700621static int yenta_allocate_res(struct yenta_socket *socket, int nr, unsigned type, int addr_start, int addr_end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 struct resource *root, *res;
Dominik Brodowski43c34732005-08-04 18:06:21 -0700624 struct pci_bus_region region;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 unsigned mask;
626
Ivan Kokshaysky79254072005-06-27 16:28:02 -0700627 res = socket->dev->resource + PCI_BRIDGE_RESOURCES + nr;
628 /* Already allocated? */
629 if (res->parent)
Dominik Brodowskib3743fa2005-09-09 13:03:23 -0700630 return 0;
Ivan Kokshaysky79254072005-06-27 16:28:02 -0700631
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 /* The granularity of the memory limit is 4kB, on IO it's 4 bytes */
633 mask = ~0xfff;
634 if (type & IORESOURCE_IO)
635 mask = ~3;
636
Dominik Brodowski43c34732005-08-04 18:06:21 -0700637 res->name = socket->dev->subordinate->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 res->flags = type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
Dominik Brodowski43c34732005-08-04 18:06:21 -0700640 region.start = config_readl(socket, addr_start) & mask;
641 region.end = config_readl(socket, addr_end) | ~mask;
642 if (region.start && region.end > region.start && !override_bios) {
643 pcibios_bus_to_resource(socket->dev, res, &region);
Dominik Brodowski862104e2005-07-12 13:58:15 -0700644 root = pci_find_parent_resource(socket->dev, res);
645 if (root && (request_resource(root, res) == 0))
Dominik Brodowskib3743fa2005-09-09 13:03:23 -0700646 return 0;
Dominik Brodowski862104e2005-07-12 13:58:15 -0700647 printk(KERN_INFO "yenta %s: Preassigned resource %d busy or not available, reconfiguring...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 pci_name(socket->dev), nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 }
650
651 if (type & IORESOURCE_IO) {
Dominik Brodowskieb0a90b2005-07-12 13:58:17 -0700652 if ((yenta_search_res(socket, res, BRIDGE_IO_MAX)) ||
653 (yenta_search_res(socket, res, BRIDGE_IO_ACC)) ||
Dominik Brodowskib3743fa2005-09-09 13:03:23 -0700654 (yenta_search_res(socket, res, BRIDGE_IO_MIN)))
655 return 1;
Dominik Brodowskieb0a90b2005-07-12 13:58:17 -0700656 } else {
657 if (type & IORESOURCE_PREFETCH) {
658 if ((yenta_search_res(socket, res, BRIDGE_MEM_MAX)) ||
659 (yenta_search_res(socket, res, BRIDGE_MEM_ACC)) ||
Dominik Brodowskib3743fa2005-09-09 13:03:23 -0700660 (yenta_search_res(socket, res, BRIDGE_MEM_MIN)))
661 return 1;
Dominik Brodowskieb0a90b2005-07-12 13:58:17 -0700662 /* Approximating prefetchable by non-prefetchable */
663 res->flags = IORESOURCE_MEM;
664 }
665 if ((yenta_search_res(socket, res, BRIDGE_MEM_MAX)) ||
666 (yenta_search_res(socket, res, BRIDGE_MEM_ACC)) ||
Dominik Brodowskib3743fa2005-09-09 13:03:23 -0700667 (yenta_search_res(socket, res, BRIDGE_MEM_MIN)))
668 return 1;
Dominik Brodowskieb0a90b2005-07-12 13:58:17 -0700669 }
670
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 printk(KERN_INFO "yenta %s: no resource of type %x available, trying to continue...\n",
Dominik Brodowskieb0a90b2005-07-12 13:58:17 -0700672 pci_name(socket->dev), type);
673 res->start = res->end = res->flags = 0;
Dominik Brodowskib3743fa2005-09-09 13:03:23 -0700674 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675}
676
677/*
678 * Allocate the bridge mappings for the device..
679 */
680static void yenta_allocate_resources(struct yenta_socket *socket)
681{
Dominik Brodowskib3743fa2005-09-09 13:03:23 -0700682 int program = 0;
683 program += yenta_allocate_res(socket, 0, IORESOURCE_IO,
Dominik Brodowski27879832005-07-12 13:58:16 -0700684 PCI_CB_IO_BASE_0, PCI_CB_IO_LIMIT_0);
Dominik Brodowskib3743fa2005-09-09 13:03:23 -0700685 program += yenta_allocate_res(socket, 1, IORESOURCE_IO,
Dominik Brodowski27879832005-07-12 13:58:16 -0700686 PCI_CB_IO_BASE_1, PCI_CB_IO_LIMIT_1);
Dominik Brodowskib3743fa2005-09-09 13:03:23 -0700687 program += yenta_allocate_res(socket, 2, IORESOURCE_MEM|IORESOURCE_PREFETCH,
Dominik Brodowski27879832005-07-12 13:58:16 -0700688 PCI_CB_MEMORY_BASE_0, PCI_CB_MEMORY_LIMIT_0);
Dominik Brodowskib3743fa2005-09-09 13:03:23 -0700689 program += yenta_allocate_res(socket, 3, IORESOURCE_MEM,
Dominik Brodowski27879832005-07-12 13:58:16 -0700690 PCI_CB_MEMORY_BASE_1, PCI_CB_MEMORY_LIMIT_1);
Dominik Brodowskib3743fa2005-09-09 13:03:23 -0700691 if (program)
692 pci_setup_cardbus(socket->dev->subordinate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693}
694
695
696/*
697 * Free the bridge mappings for the device..
698 */
699static void yenta_free_resources(struct yenta_socket *socket)
700{
701 int i;
702 for (i=0;i<4;i++) {
703 struct resource *res;
704 res = socket->dev->resource + PCI_BRIDGE_RESOURCES + i;
705 if (res->start != 0 && res->end != 0)
706 release_resource(res);
Dominik Brodowskib3743fa2005-09-09 13:03:23 -0700707 res->start = res->end = res->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 }
709}
710
711
712/*
713 * Close it down - release our resources and go home..
714 */
715static void yenta_close(struct pci_dev *dev)
716{
717 struct yenta_socket *sock = pci_get_drvdata(dev);
718
Linus Torvalds030ee392005-10-28 13:55:08 -0700719 /* Remove the register attributes */
720 device_remove_file(&dev->dev, &dev_attr_yenta_registers);
721
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 /* we don't want a dying socket registered */
723 pcmcia_unregister_socket(&sock->socket);
724
725 /* Disable all events so we don't die in an IRQ storm */
726 cb_writel(sock, CB_SOCKET_MASK, 0x0);
727 exca_writeb(sock, I365_CSCINT, 0);
728
729 if (sock->cb_irq)
730 free_irq(sock->cb_irq, sock);
731 else
732 del_timer_sync(&sock->poll_timer);
733
734 if (sock->base)
735 iounmap(sock->base);
736 yenta_free_resources(sock);
737
738 pci_release_regions(dev);
739 pci_disable_device(dev);
740 pci_set_drvdata(dev, NULL);
741}
742
743
744static struct pccard_operations yenta_socket_operations = {
745 .init = yenta_sock_init,
746 .suspend = yenta_sock_suspend,
747 .get_status = yenta_get_status,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 .set_socket = yenta_set_socket,
749 .set_io_map = yenta_set_io_map,
750 .set_mem_map = yenta_set_mem_map,
751};
752
753
Daniel Ritz63e7ebd2005-11-03 21:12:14 +0100754#ifdef CONFIG_YENTA_TI
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755#include "ti113x.h"
Daniel Ritz63e7ebd2005-11-03 21:12:14 +0100756#endif
757#ifdef CONFIG_YENTA_RICOH
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758#include "ricoh.h"
Daniel Ritz63e7ebd2005-11-03 21:12:14 +0100759#endif
760#ifdef CONFIG_YENTA_TOSHIBA
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761#include "topic.h"
Daniel Ritz63e7ebd2005-11-03 21:12:14 +0100762#endif
763#ifdef CONFIG_YENTA_O2
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764#include "o2micro.h"
Daniel Ritz63e7ebd2005-11-03 21:12:14 +0100765#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
767enum {
768 CARDBUS_TYPE_DEFAULT = -1,
769 CARDBUS_TYPE_TI,
770 CARDBUS_TYPE_TI113X,
771 CARDBUS_TYPE_TI12XX,
772 CARDBUS_TYPE_TI1250,
773 CARDBUS_TYPE_RICOH,
Daniel Ritzea2f1592005-09-06 15:16:50 -0700774 CARDBUS_TYPE_TOPIC95,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 CARDBUS_TYPE_TOPIC97,
776 CARDBUS_TYPE_O2MICRO,
Daniel Ritz8c3520d2005-08-21 22:29:26 -0700777 CARDBUS_TYPE_ENE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778};
779
780/*
781 * Different cardbus controllers have slightly different
782 * initialization sequences etc details. List them here..
783 */
784static struct cardbus_type cardbus_type[] = {
Daniel Ritz63e7ebd2005-11-03 21:12:14 +0100785#ifdef CONFIG_YENTA_TI
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 [CARDBUS_TYPE_TI] = {
787 .override = ti_override,
788 .save_state = ti_save_state,
789 .restore_state = ti_restore_state,
790 .sock_init = ti_init,
791 },
792 [CARDBUS_TYPE_TI113X] = {
793 .override = ti113x_override,
794 .save_state = ti_save_state,
795 .restore_state = ti_restore_state,
796 .sock_init = ti_init,
797 },
798 [CARDBUS_TYPE_TI12XX] = {
799 .override = ti12xx_override,
800 .save_state = ti_save_state,
801 .restore_state = ti_restore_state,
802 .sock_init = ti_init,
803 },
804 [CARDBUS_TYPE_TI1250] = {
805 .override = ti1250_override,
806 .save_state = ti_save_state,
807 .restore_state = ti_restore_state,
808 .sock_init = ti_init,
809 },
Daniel Ritz63e7ebd2005-11-03 21:12:14 +0100810#endif
811#ifdef CONFIG_YENTA_RICOH
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 [CARDBUS_TYPE_RICOH] = {
813 .override = ricoh_override,
814 .save_state = ricoh_save_state,
815 .restore_state = ricoh_restore_state,
816 },
Daniel Ritz63e7ebd2005-11-03 21:12:14 +0100817#endif
818#ifdef CONFIG_YENTA_TOSHIBA
Daniel Ritzea2f1592005-09-06 15:16:50 -0700819 [CARDBUS_TYPE_TOPIC95] = {
820 .override = topic95_override,
821 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 [CARDBUS_TYPE_TOPIC97] = {
823 .override = topic97_override,
824 },
Daniel Ritz63e7ebd2005-11-03 21:12:14 +0100825#endif
826#ifdef CONFIG_YENTA_O2
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 [CARDBUS_TYPE_O2MICRO] = {
828 .override = o2micro_override,
829 .restore_state = o2micro_restore_state,
830 },
Daniel Ritz63e7ebd2005-11-03 21:12:14 +0100831#endif
832#ifdef CONFIG_YENTA_TI
Daniel Ritz8c3520d2005-08-21 22:29:26 -0700833 [CARDBUS_TYPE_ENE] = {
834 .override = ene_override,
835 .save_state = ti_save_state,
836 .restore_state = ti_restore_state,
837 .sock_init = ti_init,
838 },
Daniel Ritz63e7ebd2005-11-03 21:12:14 +0100839#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840};
841
842
843/*
844 * Only probe "regular" interrupts, don't
845 * touch dangerous spots like the mouse irq,
846 * because there are mice that apparently
847 * get really confused if they get fondled
848 * too intimately.
849 *
850 * Default to 11, 10, 9, 7, 6, 5, 4, 3.
851 */
852static u32 isa_interrupts = 0x0ef8;
853
854static unsigned int yenta_probe_irq(struct yenta_socket *socket, u32 isa_irq_mask)
855{
856 int i;
857 unsigned long val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 u32 mask;
859
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 /*
861 * Probe for usable interrupts using the force
862 * register to generate bogus card status events.
863 */
864 cb_writel(socket, CB_SOCKET_EVENT, -1);
865 cb_writel(socket, CB_SOCKET_MASK, CB_CSTSMASK);
866 exca_writeb(socket, I365_CSCINT, 0);
867 val = probe_irq_on() & isa_irq_mask;
868 for (i = 1; i < 16; i++) {
869 if (!((val >> i) & 1))
870 continue;
871 exca_writeb(socket, I365_CSCINT, I365_CSC_STSCHG | (i << 4));
872 cb_writel(socket, CB_SOCKET_FORCE, CB_FCARDSTS);
873 udelay(100);
874 cb_writel(socket, CB_SOCKET_EVENT, -1);
875 }
876 cb_writel(socket, CB_SOCKET_MASK, 0);
877 exca_writeb(socket, I365_CSCINT, 0);
878
879 mask = probe_irq_mask(val) & 0xffff;
880
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 return mask;
882}
883
884
Daniel Ritz63e7ebd2005-11-03 21:12:14 +0100885/**
886 * yenta PCI irq probing.
887 * currently only used in the TI/EnE initialization code
888 */
889#ifdef CONFIG_YENTA_TI
890
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891/* interrupt handler, only used during probing */
892static irqreturn_t yenta_probe_handler(int irq, void *dev_id, struct pt_regs *regs)
893{
894 struct yenta_socket *socket = (struct yenta_socket *) dev_id;
895 u8 csc;
896 u32 cb_event;
897
898 /* Clear interrupt status for the event */
899 cb_event = cb_readl(socket, CB_SOCKET_EVENT);
900 cb_writel(socket, CB_SOCKET_EVENT, -1);
901 csc = exca_readb(socket, I365_CSC);
902
903 if (cb_event || csc) {
904 socket->probe_status = 1;
905 return IRQ_HANDLED;
906 }
907
908 return IRQ_NONE;
909}
910
911/* probes the PCI interrupt, use only on override functions */
912static int yenta_probe_cb_irq(struct yenta_socket *socket)
913{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 if (!socket->cb_irq)
915 return -1;
916
917 socket->probe_status = 0;
918
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 if (request_irq(socket->cb_irq, yenta_probe_handler, SA_SHIRQ, "yenta", socket)) {
920 printk(KERN_WARNING "Yenta: request_irq() in yenta_probe_cb_irq() failed!\n");
921 return -1;
922 }
923
924 /* generate interrupt, wait */
925 exca_writeb(socket, I365_CSCINT, I365_CSC_STSCHG);
926 cb_writel(socket, CB_SOCKET_EVENT, -1);
927 cb_writel(socket, CB_SOCKET_MASK, CB_CSTSMASK);
928 cb_writel(socket, CB_SOCKET_FORCE, CB_FCARDSTS);
Daniel Ritza413c092005-08-24 17:03:23 +0200929
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 msleep(100);
931
932 /* disable interrupts */
933 cb_writel(socket, CB_SOCKET_MASK, 0);
934 exca_writeb(socket, I365_CSCINT, 0);
935 cb_writel(socket, CB_SOCKET_EVENT, -1);
936 exca_readb(socket, I365_CSC);
937
938 free_irq(socket->cb_irq, socket);
939
940 return (int) socket->probe_status;
941}
942
Daniel Ritz63e7ebd2005-11-03 21:12:14 +0100943#endif /* CONFIG_YENTA_TI */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
945
946/*
947 * Set static data that doesn't need re-initializing..
948 */
949static void yenta_get_socket_capabilities(struct yenta_socket *socket, u32 isa_irq_mask)
950{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 socket->socket.pci_irq = socket->cb_irq;
Daniel Ritzfa912bc2005-06-23 00:10:12 -0700952 if (isa_probe)
953 socket->socket.irq_mask = yenta_probe_irq(socket, isa_irq_mask);
954 else
955 socket->socket.irq_mask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
957 printk(KERN_INFO "Yenta: ISA IRQ mask 0x%04x, PCI irq %d\n",
958 socket->socket.irq_mask, socket->cb_irq);
959}
960
961/*
962 * Initialize the standard cardbus registers
963 */
964static void yenta_config_init(struct yenta_socket *socket)
965{
966 u16 bridge;
967 struct pci_dev *dev = socket->dev;
Dominik Brodowski8e5d17e2005-09-07 16:00:28 -0700968 struct pci_bus_region region;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
Dominik Brodowski8e5d17e2005-09-07 16:00:28 -0700970 pcibios_resource_to_bus(socket->dev, &region, &dev->resource[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
972 config_writel(socket, CB_LEGACY_MODE_BASE, 0);
Dominik Brodowski8e5d17e2005-09-07 16:00:28 -0700973 config_writel(socket, PCI_BASE_ADDRESS_0, region.start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 config_writew(socket, PCI_COMMAND,
975 PCI_COMMAND_IO |
976 PCI_COMMAND_MEMORY |
977 PCI_COMMAND_MASTER |
978 PCI_COMMAND_WAIT);
979
980 /* MAGIC NUMBERS! Fixme */
981 config_writeb(socket, PCI_CACHE_LINE_SIZE, L1_CACHE_BYTES / 4);
982 config_writeb(socket, PCI_LATENCY_TIMER, 168);
983 config_writel(socket, PCI_PRIMARY_BUS,
984 (176 << 24) | /* sec. latency timer */
985 (dev->subordinate->subordinate << 16) | /* subordinate bus */
986 (dev->subordinate->secondary << 8) | /* secondary bus */
987 dev->subordinate->primary); /* primary bus */
988
989 /*
990 * Set up the bridging state:
991 * - enable write posting.
992 * - memory window 0 prefetchable, window 1 non-prefetchable
993 * - PCI interrupts enabled if a PCI interrupt exists..
994 */
995 bridge = config_readw(socket, CB_BRIDGE_CONTROL);
Daniel Ritza413c092005-08-24 17:03:23 +0200996 bridge &= ~(CB_BRIDGE_CRST | CB_BRIDGE_PREFETCH1 | CB_BRIDGE_ISAEN | CB_BRIDGE_VGAEN);
997 bridge |= CB_BRIDGE_PREFETCH0 | CB_BRIDGE_POSTEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 config_writew(socket, CB_BRIDGE_CONTROL, bridge);
999}
1000
1001/*
1002 * Initialize a cardbus controller. Make sure we have a usable
1003 * interrupt, and that we can map the cardbus area. Fill in the
1004 * socket information structure..
1005 */
1006static int __devinit yenta_probe (struct pci_dev *dev, const struct pci_device_id *id)
1007{
1008 struct yenta_socket *socket;
1009 int ret;
Ivan Kokshayskyc7fb0b32005-09-14 23:05:30 +04001010
1011 /*
1012 * If we failed to assign proper bus numbers for this cardbus
1013 * controller during PCI probe, its subordinate pci_bus is NULL.
1014 * Bail out if so.
1015 */
1016 if (!dev->subordinate) {
Linus Torvalds5a23f342005-09-14 13:05:17 -07001017 printk(KERN_ERR "Yenta: no bus associated with %s! "
1018 "(try 'pci=assign-busses')\n", pci_name(dev));
Ivan Kokshayskyc7fb0b32005-09-14 23:05:30 +04001019 return -ENODEV;
1020 }
1021
Dominik Brodowski8084b372005-12-11 21:18:26 +01001022 socket = kzalloc(sizeof(struct yenta_socket), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 if (!socket)
1024 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
1026 /* prepare pcmcia_socket */
1027 socket->socket.ops = &yenta_socket_operations;
1028 socket->socket.resource_ops = &pccard_nonstatic_ops;
1029 socket->socket.dev.dev = &dev->dev;
1030 socket->socket.driver_data = socket;
1031 socket->socket.owner = THIS_MODULE;
Russell King5bc6b682005-07-07 17:59:07 -07001032 socket->socket.features = SS_CAP_PAGE_REGS | SS_CAP_PCCARD;
1033 socket->socket.map_size = 0x1000;
1034 socket->socket.cb_dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
1036 /* prepare struct yenta_socket */
1037 socket->dev = dev;
1038 pci_set_drvdata(dev, socket);
1039
1040 /*
1041 * Do some basic sanity checking..
1042 */
1043 if (pci_enable_device(dev)) {
1044 ret = -EBUSY;
1045 goto free;
1046 }
1047
1048 ret = pci_request_regions(dev, "yenta_socket");
1049 if (ret)
1050 goto disable;
1051
1052 if (!pci_resource_start(dev, 0)) {
1053 printk(KERN_ERR "No cardbus resource!\n");
1054 ret = -ENODEV;
1055 goto release;
1056 }
1057
1058 /*
1059 * Ok, start setup.. Map the cardbus registers,
1060 * and request the IRQ.
1061 */
1062 socket->base = ioremap(pci_resource_start(dev, 0), 0x1000);
1063 if (!socket->base) {
1064 ret = -ENOMEM;
1065 goto release;
1066 }
1067
1068 /*
1069 * report the subsystem vendor and device for help debugging
1070 * the irq stuff...
1071 */
1072 printk(KERN_INFO "Yenta: CardBus bridge found at %s [%04x:%04x]\n",
1073 pci_name(dev), dev->subsystem_vendor, dev->subsystem_device);
1074
1075 yenta_config_init(socket);
1076
1077 /* Disable all events */
1078 cb_writel(socket, CB_SOCKET_MASK, 0x0);
1079
1080 /* Set up the bridge regions.. */
1081 yenta_allocate_resources(socket);
1082
1083 socket->cb_irq = dev->irq;
1084
1085 /* Do we have special options for the device? */
1086 if (id->driver_data != CARDBUS_TYPE_DEFAULT &&
1087 id->driver_data < ARRAY_SIZE(cardbus_type)) {
1088 socket->type = &cardbus_type[id->driver_data];
1089
1090 ret = socket->type->override(socket);
1091 if (ret < 0)
1092 goto unmap;
1093 }
1094
1095 /* We must finish initialization here */
1096
1097 if (!socket->cb_irq || request_irq(socket->cb_irq, yenta_interrupt, SA_SHIRQ, "yenta", socket)) {
1098 /* No IRQ or request_irq failed. Poll */
1099 socket->cb_irq = 0; /* But zero is a valid IRQ number. */
1100 init_timer(&socket->poll_timer);
1101 socket->poll_timer.function = yenta_interrupt_wrapper;
1102 socket->poll_timer.data = (unsigned long)socket;
1103 socket->poll_timer.expires = jiffies + HZ;
1104 add_timer(&socket->poll_timer);
Russell King5bc6b682005-07-07 17:59:07 -07001105 printk(KERN_INFO "Yenta: no PCI IRQ, CardBus support disabled for this socket.\n"
1106 KERN_INFO "Yenta: check your BIOS CardBus, BIOS IRQ or ACPI settings.\n");
1107 } else {
1108 socket->socket.features |= SS_CAP_CARDBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 }
1110
1111 /* Figure out what the dang thing can do for the PCMCIA layer... */
Daniel Ritzfa912bc2005-06-23 00:10:12 -07001112 yenta_interrogate(socket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 yenta_get_socket_capabilities(socket, isa_interrupts);
1114 printk(KERN_INFO "Socket status: %08x\n", cb_readl(socket, CB_SOCKET_STATE));
1115
1116 /* Register it with the pcmcia layer.. */
1117 ret = pcmcia_register_socket(&socket->socket);
Linus Torvalds030ee392005-10-28 13:55:08 -07001118 if (ret == 0) {
1119 /* Add the yenta register attributes */
1120 device_create_file(&dev->dev, &dev_attr_yenta_registers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 goto out;
Linus Torvalds030ee392005-10-28 13:55:08 -07001122 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
1124 unmap:
1125 iounmap(socket->base);
1126 release:
1127 pci_release_regions(dev);
1128 disable:
1129 pci_disable_device(dev);
1130 free:
1131 kfree(socket);
1132 out:
1133 return ret;
1134}
1135
1136
1137static int yenta_dev_suspend (struct pci_dev *dev, pm_message_t state)
1138{
1139 struct yenta_socket *socket = pci_get_drvdata(dev);
1140 int ret;
1141
1142 ret = pcmcia_socket_dev_suspend(&dev->dev, state);
1143
1144 if (socket) {
1145 if (socket->type && socket->type->save_state)
1146 socket->type->save_state(socket);
1147
1148 /* FIXME: pci_save_state needs to have a better interface */
1149 pci_save_state(dev);
1150 pci_read_config_dword(dev, 16*4, &socket->saved_state[0]);
1151 pci_read_config_dword(dev, 17*4, &socket->saved_state[1]);
David Shaohua Lid58da592005-03-18 16:43:54 -05001152 pci_disable_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153
1154 /*
1155 * Some laptops (IBM T22) do not like us putting the Cardbus
1156 * bridge into D3. At a guess, some other laptop will
1157 * probably require this, so leave it commented out for now.
1158 */
1159 /* pci_set_power_state(dev, 3); */
1160 }
1161
1162 return ret;
1163}
1164
1165
1166static int yenta_dev_resume (struct pci_dev *dev)
1167{
1168 struct yenta_socket *socket = pci_get_drvdata(dev);
1169
1170 if (socket) {
1171 pci_set_power_state(dev, 0);
1172 /* FIXME: pci_restore_state needs to have a better interface */
1173 pci_restore_state(dev);
1174 pci_write_config_dword(dev, 16*4, socket->saved_state[0]);
1175 pci_write_config_dword(dev, 17*4, socket->saved_state[1]);
David Shaohua Lid58da592005-03-18 16:43:54 -05001176 pci_enable_device(dev);
1177 pci_set_master(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 if (socket->type && socket->type->restore_state)
1180 socket->type->restore_state(socket);
1181 }
1182
1183 return pcmcia_socket_dev_resume(&dev->dev);
1184}
1185
1186
1187#define CB_ID(vend,dev,type) \
1188 { \
1189 .vendor = vend, \
1190 .device = dev, \
1191 .subvendor = PCI_ANY_ID, \
1192 .subdevice = PCI_ANY_ID, \
1193 .class = PCI_CLASS_BRIDGE_CARDBUS << 8, \
1194 .class_mask = ~0, \
1195 .driver_data = CARDBUS_TYPE_##type, \
1196 }
1197
1198static struct pci_device_id yenta_table [] = {
1199 CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_1031, TI),
1200
1201 /*
1202 * TBD: Check if these TI variants can use more
1203 * advanced overrides instead. (I can't get the
1204 * data sheets for these devices. --rmk)
1205 */
Daniel Ritz63e7ebd2005-11-03 21:12:14 +01001206#ifdef CONFIG_YENTA_TI
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_1210, TI),
1208
1209 CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_1130, TI113X),
1210 CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_1131, TI113X),
1211
1212 CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_1211, TI12XX),
1213 CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_1220, TI12XX),
1214 CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_1221, TI12XX),
1215 CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_1225, TI12XX),
1216 CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_1251A, TI12XX),
1217 CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_1251B, TI12XX),
1218 CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_1420, TI12XX),
1219 CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_1450, TI12XX),
1220 CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_1451A, TI12XX),
1221 CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_1510, TI12XX),
1222 CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_1520, TI12XX),
1223 CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_1620, TI12XX),
1224 CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_4410, TI12XX),
1225 CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_4450, TI12XX),
1226 CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_4451, TI12XX),
1227 CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_4510, TI12XX),
1228 CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_4520, TI12XX),
1229
1230 CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_1250, TI1250),
1231 CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_1410, TI1250),
1232
Daniel Ritz6c1a10d2005-09-20 14:12:17 -07001233 CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_XX21_XX11, TI12XX),
1234 CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_X515, TI12XX),
1235 CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_X420, TI12XX),
1236 CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_X620, TI12XX),
1237 CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_7410, TI12XX),
1238 CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_7510, TI12XX),
1239 CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_7610, TI12XX),
1240
Daniel Ritzf9cb8b72005-09-24 23:12:46 -07001241 CB_ID(PCI_VENDOR_ID_ENE, PCI_DEVICE_ID_ENE_710, TI12XX),
1242 CB_ID(PCI_VENDOR_ID_ENE, PCI_DEVICE_ID_ENE_712, TI12XX),
1243 CB_ID(PCI_VENDOR_ID_ENE, PCI_DEVICE_ID_ENE_720, TI12XX),
1244 CB_ID(PCI_VENDOR_ID_ENE, PCI_DEVICE_ID_ENE_722, TI12XX),
Daniel Ritz8c3520d2005-08-21 22:29:26 -07001245 CB_ID(PCI_VENDOR_ID_ENE, PCI_DEVICE_ID_ENE_1211, ENE),
1246 CB_ID(PCI_VENDOR_ID_ENE, PCI_DEVICE_ID_ENE_1225, ENE),
1247 CB_ID(PCI_VENDOR_ID_ENE, PCI_DEVICE_ID_ENE_1410, ENE),
1248 CB_ID(PCI_VENDOR_ID_ENE, PCI_DEVICE_ID_ENE_1420, ENE),
Daniel Ritz63e7ebd2005-11-03 21:12:14 +01001249#endif /* CONFIG_YENTA_TI */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
Daniel Ritz63e7ebd2005-11-03 21:12:14 +01001251#ifdef CONFIG_YENTA_RICOH
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 CB_ID(PCI_VENDOR_ID_RICOH, PCI_DEVICE_ID_RICOH_RL5C465, RICOH),
1253 CB_ID(PCI_VENDOR_ID_RICOH, PCI_DEVICE_ID_RICOH_RL5C466, RICOH),
1254 CB_ID(PCI_VENDOR_ID_RICOH, PCI_DEVICE_ID_RICOH_RL5C475, RICOH),
1255 CB_ID(PCI_VENDOR_ID_RICOH, PCI_DEVICE_ID_RICOH_RL5C476, RICOH),
1256 CB_ID(PCI_VENDOR_ID_RICOH, PCI_DEVICE_ID_RICOH_RL5C478, RICOH),
Daniel Ritz63e7ebd2005-11-03 21:12:14 +01001257#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
Daniel Ritz63e7ebd2005-11-03 21:12:14 +01001259#ifdef CONFIG_YENTA_TOSHIBA
Daniel Ritzea2f1592005-09-06 15:16:50 -07001260 CB_ID(PCI_VENDOR_ID_TOSHIBA, PCI_DEVICE_ID_TOSHIBA_TOPIC95, TOPIC95),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 CB_ID(PCI_VENDOR_ID_TOSHIBA, PCI_DEVICE_ID_TOSHIBA_TOPIC97, TOPIC97),
1262 CB_ID(PCI_VENDOR_ID_TOSHIBA, PCI_DEVICE_ID_TOSHIBA_TOPIC100, TOPIC97),
Daniel Ritz63e7ebd2005-11-03 21:12:14 +01001263#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
Daniel Ritz63e7ebd2005-11-03 21:12:14 +01001265#ifdef CONFIG_YENTA_O2
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 CB_ID(PCI_VENDOR_ID_O2, PCI_ANY_ID, O2MICRO),
Daniel Ritz63e7ebd2005-11-03 21:12:14 +01001267#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
1269 /* match any cardbus bridge */
1270 CB_ID(PCI_ANY_ID, PCI_ANY_ID, DEFAULT),
1271 { /* all zeroes */ }
1272};
1273MODULE_DEVICE_TABLE(pci, yenta_table);
1274
1275
1276static struct pci_driver yenta_cardbus_driver = {
1277 .name = "yenta_cardbus",
1278 .id_table = yenta_table,
1279 .probe = yenta_probe,
1280 .remove = __devexit_p(yenta_close),
1281 .suspend = yenta_dev_suspend,
1282 .resume = yenta_dev_resume,
1283};
1284
1285
1286static int __init yenta_socket_init(void)
1287{
1288 return pci_register_driver (&yenta_cardbus_driver);
1289}
1290
1291
1292static void __exit yenta_socket_exit (void)
1293{
1294 pci_unregister_driver (&yenta_cardbus_driver);
1295}
1296
1297
1298module_init(yenta_socket_init);
1299module_exit(yenta_socket_exit);
1300
1301MODULE_LICENSE("GPL");