yenta_socket.c 39.9 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8
/*
 * Regular cardbus driver ("yenta_socket")
 *
 * (C) Copyright 1999, 2000 Linus Torvalds
 *
 * Changelog:
 * Aug 2002: Manfred Spraul <manfred@colorfullife.com>
 * 	Dynamically adjust the size of the bridge resource
D
Dominik Brodowski 已提交
9
 *
L
Linus Torvalds 已提交
10 11 12 13 14 15 16 17 18
 * May 2003: Dominik Brodowski <linux@brodo.de>
 * 	Merge pci_socket.c and yenta.c into one file
 */
#include <linux/init.h>
#include <linux/pci.h>
#include <linux/workqueue.h>
#include <linux/interrupt.h>
#include <linux/delay.h>
#include <linux/module.h>
D
Dominik Brodowski 已提交
19
#include <linux/io.h>
L
Linus Torvalds 已提交
20 21 22 23 24 25 26 27 28 29 30 31

#include <pcmcia/cs_types.h>
#include <pcmcia/ss.h>
#include <pcmcia/cs.h>

#include "yenta_socket.h"
#include "i82365.h"

static int disable_clkrun;
module_param(disable_clkrun, bool, 0444);
MODULE_PARM_DESC(disable_clkrun, "If PC card doesn't function properly, please try this option");

32 33 34 35 36 37 38 39
static int isa_probe = 1;
module_param(isa_probe, bool, 0444);
MODULE_PARM_DESC(isa_probe, "If set ISA interrupts are probed (default). Set to N to disable probing");

static int pwr_irqs_off;
module_param(pwr_irqs_off, bool, 0644);
MODULE_PARM_DESC(pwr_irqs_off, "Force IRQs off during power-on of slot. Use only when seeing IRQ storms!");

40 41 42 43 44
static char o2_speedup[] = "default";
module_param_string(o2_speedup, o2_speedup, sizeof(o2_speedup), 0444);
MODULE_PARM_DESC(o2_speedup, "Use prefetch/burst for O2-bridges: 'on', 'off' "
	"or 'default' (uses recommended behaviour for the detected bridge)");

45 46 47 48 49 50 51 52 53 54 55 56
/*
 * Only probe "regular" interrupts, don't
 * touch dangerous spots like the mouse irq,
 * because there are mice that apparently
 * get really confused if they get fondled
 * too intimately.
 *
 * Default to 11, 10, 9, 7, 6, 5, 4, 3.
 */
static u32 isa_interrupts = 0x0ef8;


57
#define debug(x, s, args...) dev_dbg(&s->dev->dev, x, ##args)
L
Linus Torvalds 已提交
58 59 60 61 62

/* Don't ask.. */
#define to_cycles(ns)	((ns)/120)
#define to_ns(cycles)	((cycles)*120)

R
Randy Dunlap 已提交
63
/*
64 65 66 67
 * yenta PCI irq probing.
 * currently only used in the TI/EnE initialization code
 */
#ifdef CONFIG_YENTA_TI
L
Linus Torvalds 已提交
68
static int yenta_probe_cb_irq(struct yenta_socket *socket);
69 70
static unsigned int yenta_probe_irq(struct yenta_socket *socket,
				u32 isa_irq_mask);
71
#endif
L
Linus Torvalds 已提交
72 73 74 75


static unsigned int override_bios;
module_param(override_bios, uint, 0000);
D
Dominik Brodowski 已提交
76
MODULE_PARM_DESC(override_bios, "yenta ignore bios resource allocation");
L
Linus Torvalds 已提交
77 78 79 80 81 82 83 84 85

/*
 * Generate easy-to-use ways of reading a cardbus sockets
 * regular memory space ("cb_xxx"), configuration space
 * ("config_xxx") and compatibility space ("exca_xxxx")
 */
static inline u32 cb_readl(struct yenta_socket *socket, unsigned reg)
{
	u32 val = readl(socket->base + reg);
86
	debug("%04x %08x\n", socket, reg, val);
L
Linus Torvalds 已提交
87 88 89 90 91
	return val;
}

static inline void cb_writel(struct yenta_socket *socket, unsigned reg, u32 val)
{
92
	debug("%04x %08x\n", socket, reg, val);
L
Linus Torvalds 已提交
93
	writel(val, socket->base + reg);
94
	readl(socket->base + reg); /* avoid problems with PCI write posting */
L
Linus Torvalds 已提交
95 96 97 98 99 100
}

static inline u8 config_readb(struct yenta_socket *socket, unsigned offset)
{
	u8 val;
	pci_read_config_byte(socket->dev, offset, &val);
101
	debug("%04x %02x\n", socket, offset, val);
L
Linus Torvalds 已提交
102 103 104 105 106 107 108
	return val;
}

static inline u16 config_readw(struct yenta_socket *socket, unsigned offset)
{
	u16 val;
	pci_read_config_word(socket->dev, offset, &val);
109
	debug("%04x %04x\n", socket, offset, val);
L
Linus Torvalds 已提交
110 111 112 113 114 115 116
	return val;
}

static inline u32 config_readl(struct yenta_socket *socket, unsigned offset)
{
	u32 val;
	pci_read_config_dword(socket->dev, offset, &val);
117
	debug("%04x %08x\n", socket, offset, val);
L
Linus Torvalds 已提交
118 119 120 121 122
	return val;
}

static inline void config_writeb(struct yenta_socket *socket, unsigned offset, u8 val)
{
123
	debug("%04x %02x\n", socket, offset, val);
L
Linus Torvalds 已提交
124 125 126 127 128
	pci_write_config_byte(socket->dev, offset, val);
}

static inline void config_writew(struct yenta_socket *socket, unsigned offset, u16 val)
{
129
	debug("%04x %04x\n", socket, offset, val);
L
Linus Torvalds 已提交
130 131 132 133 134
	pci_write_config_word(socket->dev, offset, val);
}

static inline void config_writel(struct yenta_socket *socket, unsigned offset, u32 val)
{
135
	debug("%04x %08x\n", socket, offset, val);
L
Linus Torvalds 已提交
136 137 138 139 140 141
	pci_write_config_dword(socket->dev, offset, val);
}

static inline u8 exca_readb(struct yenta_socket *socket, unsigned reg)
{
	u8 val = readb(socket->base + 0x800 + reg);
142
	debug("%04x %02x\n", socket, reg, val);
L
Linus Torvalds 已提交
143 144 145 146 147 148 149 150
	return val;
}

static inline u8 exca_readw(struct yenta_socket *socket, unsigned reg)
{
	u16 val;
	val = readb(socket->base + 0x800 + reg);
	val |= readb(socket->base + 0x800 + reg + 1) << 8;
151
	debug("%04x %04x\n", socket, reg, val);
L
Linus Torvalds 已提交
152 153 154 155 156
	return val;
}

static inline void exca_writeb(struct yenta_socket *socket, unsigned reg, u8 val)
{
157
	debug("%04x %02x\n", socket, reg, val);
L
Linus Torvalds 已提交
158
	writeb(val, socket->base + 0x800 + reg);
159
	readb(socket->base + 0x800 + reg); /* PCI write posting... */
L
Linus Torvalds 已提交
160 161 162 163
}

static void exca_writew(struct yenta_socket *socket, unsigned reg, u16 val)
{
164
	debug("%04x %04x\n", socket, reg, val);
L
Linus Torvalds 已提交
165 166
	writeb(val, socket->base + 0x800 + reg);
	writeb(val >> 8, socket->base + 0x800 + reg + 1);
167 168 169 170

	/* PCI write posting... */
	readb(socket->base + 0x800 + reg);
	readb(socket->base + 0x800 + reg + 1);
L
Linus Torvalds 已提交
171 172
}

173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
static ssize_t show_yenta_registers(struct device *yentadev, struct device_attribute *attr, char *buf)
{
	struct pci_dev *dev = to_pci_dev(yentadev);
	struct yenta_socket *socket = pci_get_drvdata(dev);
	int offset = 0, i;

	offset = snprintf(buf, PAGE_SIZE, "CB registers:");
	for (i = 0; i < 0x24; i += 4) {
		unsigned val;
		if (!(i & 15))
			offset += snprintf(buf + offset, PAGE_SIZE - offset, "\n%02x:", i);
		val = cb_readl(socket, i);
		offset += snprintf(buf + offset, PAGE_SIZE - offset, " %08x", val);
	}

	offset += snprintf(buf + offset, PAGE_SIZE - offset, "\n\nExCA registers:");
	for (i = 0; i < 0x45; i++) {
		unsigned char val;
		if (!(i & 7)) {
			if (i & 8) {
				memcpy(buf + offset, " -", 2);
				offset += 2;
			} else
				offset += snprintf(buf + offset, PAGE_SIZE - offset, "\n%02x:", i);
		}
		val = exca_readb(socket, i);
		offset += snprintf(buf + offset, PAGE_SIZE - offset, " %02x", val);
	}
	buf[offset++] = '\n';
	return offset;
}

static DEVICE_ATTR(yenta_registers, S_IRUSR, show_yenta_registers, NULL);

L
Linus Torvalds 已提交
207 208 209 210 211 212 213 214 215 216 217 218
/*
 * Ugh, mixed-mode cardbus and 16-bit pccard state: things depend
 * on what kind of card is inserted..
 */
static int yenta_get_status(struct pcmcia_socket *sock, unsigned int *value)
{
	struct yenta_socket *socket = container_of(sock, struct yenta_socket, socket);
	unsigned int val;
	u32 state = cb_readl(socket, CB_SOCKET_STATE);

	val  = (state & CB_3VCARD) ? SS_3VCARD : 0;
	val |= (state & CB_XVCARD) ? SS_XVCARD : 0;
219 220 221
	val |= (state & (CB_5VCARD | CB_3VCARD | CB_XVCARD | CB_YVCARD)) ? 0 : SS_PENDING;
	val |= (state & (CB_CDETECT1 | CB_CDETECT2)) ? SS_PENDING : 0;

L
Linus Torvalds 已提交
222 223

	if (state & CB_CBCARD) {
224
		val |= SS_CARDBUS;
L
Linus Torvalds 已提交
225 226 227
		val |= (state & CB_CARDSTS) ? SS_STSCHG : 0;
		val |= (state & (CB_CDETECT1 | CB_CDETECT2)) ? 0 : SS_DETECT;
		val |= (state & CB_PWRCYCLE) ? SS_POWERON | SS_READY : 0;
228
	} else if (state & CB_16BITCARD) {
L
Linus Torvalds 已提交
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
		u8 status = exca_readb(socket, I365_STATUS);
		val |= ((status & I365_CS_DETECT) == I365_CS_DETECT) ? SS_DETECT : 0;
		if (exca_readb(socket, I365_INTCTL) & I365_PC_IOCARD) {
			val |= (status & I365_CS_STSCHG) ? 0 : SS_STSCHG;
		} else {
			val |= (status & I365_CS_BVD1) ? 0 : SS_BATDEAD;
			val |= (status & I365_CS_BVD2) ? 0 : SS_BATWARN;
		}
		val |= (status & I365_CS_WRPROT) ? SS_WRPROT : 0;
		val |= (status & I365_CS_READY) ? SS_READY : 0;
		val |= (status & I365_CS_POWERON) ? SS_POWERON : 0;
	}

	*value = val;
	return 0;
}

static void yenta_set_power(struct yenta_socket *socket, socket_state_t *state)
{
248 249 250 251 252 253 254 255 256 257
	/* some birdges require to use the ExCA registers to power 16bit cards */
	if (!(cb_readl(socket, CB_SOCKET_STATE) & CB_CBCARD) &&
	    (socket->flags & YENTA_16BIT_POWER_EXCA)) {
		u8 reg, old;
		reg = old = exca_readb(socket, I365_POWER);
		reg &= ~(I365_VCC_MASK | I365_VPP1_MASK | I365_VPP2_MASK);

		/* i82365SL-DF style */
		if (socket->flags & YENTA_16BIT_POWER_DF) {
			switch (state->Vcc) {
D
Dominik Brodowski 已提交
258 259 260 261 262 263 264 265 266
			case 33:
				reg |= I365_VCC_3V;
				break;
			case 50:
				reg |= I365_VCC_5V;
				break;
			default:
				reg = 0;
				break;
267 268 269
			}
			switch (state->Vpp) {
			case 33:
D
Dominik Brodowski 已提交
270 271 272 273 274 275
			case 50:
				reg |= I365_VPP1_5V;
				break;
			case 120:
				reg |= I365_VPP1_12V;
				break;
276 277 278 279
			}
		} else {
			/* i82365SL-B style */
			switch (state->Vcc) {
D
Dominik Brodowski 已提交
280 281 282 283 284 285
			case 50:
				reg |= I365_VCC_5V;
				break;
			default:
				reg = 0;
				break;
286 287
			}
			switch (state->Vpp) {
D
Dominik Brodowski 已提交
288 289 290 291 292 293
			case 50:
				reg |= I365_VPP1_5V | I365_VPP2_5V;
				break;
			case 120:
				reg |= I365_VPP1_12V | I365_VPP2_12V;
				break;
294 295 296 297 298 299 300 301
			}
		}

		if (reg != old)
			exca_writeb(socket, I365_POWER, reg);
	} else {
		u32 reg = 0;	/* CB_SC_STPCLK? */
		switch (state->Vcc) {
D
Dominik Brodowski 已提交
302 303 304 305 306 307 308 309 310
		case 33:
			reg = CB_SC_VCC_3V;
			break;
		case 50:
			reg = CB_SC_VCC_5V;
			break;
		default:
			reg = 0;
			break;
311 312
		}
		switch (state->Vpp) {
D
Dominik Brodowski 已提交
313 314 315 316 317 318 319 320 321
		case 33:
			reg |= CB_SC_VPP_3V;
			break;
		case 50:
			reg |= CB_SC_VPP_5V;
			break;
		case 120:
			reg |= CB_SC_VPP_12V;
			break;
322 323 324
		}
		if (reg != cb_readl(socket, CB_SOCKET_CONTROL))
			cb_writel(socket, CB_SOCKET_CONTROL, reg);
L
Linus Torvalds 已提交
325 326 327 328 329 330 331 332
	}
}

static int yenta_set_socket(struct pcmcia_socket *sock, socket_state_t *state)
{
	struct yenta_socket *socket = container_of(sock, struct yenta_socket, socket);
	u16 bridge;

333 334 335 336
	/* if powering down: do it immediately */
	if (state->Vcc == 0)
		yenta_set_power(socket, state);

L
Linus Torvalds 已提交
337 338 339 340 341 342 343 344 345
	socket->io_irq = state->io_irq;
	bridge = config_readw(socket, CB_BRIDGE_CONTROL) & ~(CB_BRIDGE_CRST | CB_BRIDGE_INTR);
	if (cb_readl(socket, CB_SOCKET_STATE) & CB_CBCARD) {
		u8 intr;
		bridge |= (state->flags & SS_RESET) ? CB_BRIDGE_CRST : 0;

		/* ISA interrupt control? */
		intr = exca_readb(socket, I365_INTCTL);
		intr = (intr & ~0xf);
346 347
		if (!socket->dev->irq) {
			intr |= socket->cb_irq ? socket->cb_irq : state->io_irq;
L
Linus Torvalds 已提交
348 349 350 351 352 353 354 355 356
			bridge |= CB_BRIDGE_INTR;
		}
		exca_writeb(socket, I365_INTCTL, intr);
	}  else {
		u8 reg;

		reg = exca_readb(socket, I365_INTCTL) & (I365_RING_ENA | I365_INTR_ENA);
		reg |= (state->flags & SS_RESET) ? 0 : I365_PC_RESET;
		reg |= (state->flags & SS_IOCARD) ? I365_PC_IOCARD : 0;
357
		if (state->io_irq != socket->dev->irq) {
L
Linus Torvalds 已提交
358 359 360 361 362 363 364
			reg |= state->io_irq;
			bridge |= CB_BRIDGE_INTR;
		}
		exca_writeb(socket, I365_INTCTL, reg);

		reg = exca_readb(socket, I365_POWER) & (I365_VCC_MASK|I365_VPP1_MASK);
		reg |= I365_PWR_NORESET;
D
Dominik Brodowski 已提交
365 366 367 368
		if (state->flags & SS_PWR_AUTO)
			reg |= I365_PWR_AUTO;
		if (state->flags & SS_OUTPUT_ENA)
			reg |= I365_PWR_OUT;
L
Linus Torvalds 已提交
369 370 371 372
		if (exca_readb(socket, I365_POWER) != reg)
			exca_writeb(socket, I365_POWER, reg);

		/* CSC interrupt: no ISA irq for CSC */
373 374 375
		reg = exca_readb(socket, I365_CSCINT);
		reg &= I365_CSC_IRQ_MASK;
		reg |= I365_CSC_DETECT;
L
Linus Torvalds 已提交
376
		if (state->flags & SS_IOCARD) {
D
Dominik Brodowski 已提交
377 378
			if (state->csc_mask & SS_STSCHG)
				reg |= I365_CSC_STSCHG;
L
Linus Torvalds 已提交
379
		} else {
D
Dominik Brodowski 已提交
380 381 382 383 384 385
			if (state->csc_mask & SS_BATDEAD)
				reg |= I365_CSC_BVD1;
			if (state->csc_mask & SS_BATWARN)
				reg |= I365_CSC_BVD2;
			if (state->csc_mask & SS_READY)
				reg |= I365_CSC_READY;
L
Linus Torvalds 已提交
386 387 388
		}
		exca_writeb(socket, I365_CSCINT, reg);
		exca_readb(socket, I365_CSC);
D
Dominik Brodowski 已提交
389
		if (sock->zoom_video)
L
Linus Torvalds 已提交
390 391 392 393 394 395
			sock->zoom_video(sock, state->flags & SS_ZVCARD);
	}
	config_writew(socket, CB_BRIDGE_CONTROL, bridge);
	/* Socket event mask: get card insert/remove events.. */
	cb_writel(socket, CB_SOCKET_EVENT, -1);
	cb_writel(socket, CB_SOCKET_MASK, CB_CDMASK);
396 397 398 399

	/* if powering up: do it as the last step when the socket is configured */
	if (state->Vcc != 0)
		yenta_set_power(socket, state);
L
Linus Torvalds 已提交
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
	return 0;
}

static int yenta_set_io_map(struct pcmcia_socket *sock, struct pccard_io_map *io)
{
	struct yenta_socket *socket = container_of(sock, struct yenta_socket, socket);
	int map;
	unsigned char ioctl, addr, enable;

	map = io->map;

	if (map > 1)
		return -EINVAL;

	enable = I365_ENA_IO(map);
	addr = exca_readb(socket, I365_ADDRWIN);

	/* Disable the window before changing it.. */
	if (addr & enable) {
		addr &= ~enable;
		exca_writeb(socket, I365_ADDRWIN, addr);
	}

	exca_writew(socket, I365_IO(map)+I365_W_START, io->start);
	exca_writew(socket, I365_IO(map)+I365_W_STOP, io->stop);

	ioctl = exca_readb(socket, I365_IOCTL) & ~I365_IOCTL_MASK(map);
D
Dominik Brodowski 已提交
427 428 429 430 431 432
	if (io->flags & MAP_0WS)
		ioctl |= I365_IOCTL_0WS(map);
	if (io->flags & MAP_16BIT)
		ioctl |= I365_IOCTL_16BIT(map);
	if (io->flags & MAP_AUTOSZ)
		ioctl |= I365_IOCTL_IOCS16(map);
L
Linus Torvalds 已提交
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
	exca_writeb(socket, I365_IOCTL, ioctl);

	if (io->flags & MAP_ACTIVE)
		exca_writeb(socket, I365_ADDRWIN, addr | enable);
	return 0;
}

static int yenta_set_mem_map(struct pcmcia_socket *sock, struct pccard_mem_map *mem)
{
	struct yenta_socket *socket = container_of(sock, struct yenta_socket, socket);
	struct pci_bus_region region;
	int map;
	unsigned char addr, enable;
	unsigned int start, stop, card_start;
	unsigned short word;

	pcibios_resource_to_bus(socket->dev, &region, mem->res);

	map = mem->map;
	start = region.start;
	stop = region.end;
	card_start = mem->card_start;

	if (map > 4 || start > stop || ((start ^ stop) >> 24) ||
	    (card_start >> 26) || mem->speed > 1000)
		return -EINVAL;

	enable = I365_ENA_MEM(map);
	addr = exca_readb(socket, I365_ADDRWIN);
	if (addr & enable) {
		addr &= ~enable;
		exca_writeb(socket, I365_ADDRWIN, addr);
	}

	exca_writeb(socket, CB_MEM_PAGE(map), start >> 24);

	word = (start >> 12) & 0x0fff;
	if (mem->flags & MAP_16BIT)
		word |= I365_MEM_16BIT;
	if (mem->flags & MAP_0WS)
		word |= I365_MEM_0WS;
	exca_writew(socket, I365_MEM(map) + I365_W_START, word);

	word = (stop >> 12) & 0x0fff;
	switch (to_cycles(mem->speed)) {
D
Dominik Brodowski 已提交
478 479 480 481 482 483 484 485 486 487 488
	case 0:
		break;
	case 1:
		word |= I365_MEM_WS0;
		break;
	case 2:
		word |= I365_MEM_WS1;
		break;
	default:
		word |= I365_MEM_WS1 | I365_MEM_WS0;
		break;
L
Linus Torvalds 已提交
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
	}
	exca_writew(socket, I365_MEM(map) + I365_W_STOP, word);

	word = ((card_start - start) >> 12) & 0x3fff;
	if (mem->flags & MAP_WRPROT)
		word |= I365_MEM_WRPROT;
	if (mem->flags & MAP_ATTRIB)
		word |= I365_MEM_REG;
	exca_writew(socket, I365_MEM(map) + I365_W_OFF, word);

	if (mem->flags & MAP_ACTIVE)
		exca_writeb(socket, I365_ADDRWIN, addr | enable);
	return 0;
}


505

506
static irqreturn_t yenta_interrupt(int irq, void *dev_id)
L
Linus Torvalds 已提交
507
{
508 509
	unsigned int events;
	struct yenta_socket *socket = (struct yenta_socket *) dev_id;
L
Linus Torvalds 已提交
510 511 512 513 514 515 516 517 518
	u8 csc;
	u32 cb_event;

	/* Clear interrupt status for the event */
	cb_event = cb_readl(socket, CB_SOCKET_EVENT);
	cb_writel(socket, CB_SOCKET_EVENT, cb_event);

	csc = exca_readb(socket, I365_CSC);

519 520 521
	if (!(cb_event || csc))
		return IRQ_NONE;

L
Linus Torvalds 已提交
522 523 524 525 526 527 528 529 530 531
	events = (cb_event & (CB_CD1EVENT | CB_CD2EVENT)) ? SS_DETECT : 0 ;
	events |= (csc & I365_CSC_DETECT) ? SS_DETECT : 0;
	if (exca_readb(socket, I365_INTCTL) & I365_PC_IOCARD) {
		events |= (csc & I365_CSC_STSCHG) ? SS_STSCHG : 0;
	} else {
		events |= (csc & I365_CSC_BVD1) ? SS_BATDEAD : 0;
		events |= (csc & I365_CSC_BVD2) ? SS_BATWARN : 0;
		events |= (csc & I365_CSC_READY) ? SS_READY : 0;
	}

532
	if (events)
L
Linus Torvalds 已提交
533
		pcmcia_parse_events(&socket->socket, events);
534

535
	return IRQ_HANDLED;
L
Linus Torvalds 已提交
536 537 538 539 540 541
}

static void yenta_interrupt_wrapper(unsigned long data)
{
	struct yenta_socket *socket = (struct yenta_socket *) data;

542
	yenta_interrupt(0, (void *)socket);
L
Linus Torvalds 已提交
543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564
	socket->poll_timer.expires = jiffies + HZ;
	add_timer(&socket->poll_timer);
}

static void yenta_clear_maps(struct yenta_socket *socket)
{
	int i;
	struct resource res = { .start = 0, .end = 0x0fff };
	pccard_io_map io = { 0, 0, 0, 0, 1 };
	pccard_mem_map mem = { .res = &res, };

	yenta_set_socket(&socket->socket, &dead_socket);
	for (i = 0; i < 2; i++) {
		io.map = i;
		yenta_set_io_map(&socket->socket, &io);
	}
	for (i = 0; i < 5; i++) {
		mem.map = i;
		yenta_set_mem_map(&socket->socket, &mem);
	}
}

565 566 567 568 569 570 571 572 573 574 575 576
/* redoes voltage interrogation if required */
static void yenta_interrogate(struct yenta_socket *socket)
{
	u32 state;

	state = cb_readl(socket, CB_SOCKET_STATE);
	if (!(state & (CB_5VCARD | CB_3VCARD | CB_XVCARD | CB_YVCARD)) ||
	    (state & (CB_CDETECT1 | CB_CDETECT2 | CB_NOTACARD | CB_BADVCCREQ)) ||
	    ((state & (CB_16BITCARD | CB_CBCARD)) == (CB_16BITCARD | CB_CBCARD)))
		cb_writel(socket, CB_SOCKET_FORCE, CB_CVSTEST);
}

L
Linus Torvalds 已提交
577 578 579 580 581 582 583 584 585
/* Called at resume and initialization events */
static int yenta_sock_init(struct pcmcia_socket *sock)
{
	struct yenta_socket *socket = container_of(sock, struct yenta_socket, socket);

	exca_writeb(socket, I365_GBLCTL, 0x00);
	exca_writeb(socket, I365_GENCTL, 0x00);

	/* Redo card voltage interrogation */
586
	yenta_interrogate(socket);
L
Linus Torvalds 已提交
587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612

	yenta_clear_maps(socket);

	if (socket->type && socket->type->sock_init)
		socket->type->sock_init(socket);

	/* Re-enable CSC interrupts */
	cb_writel(socket, CB_SOCKET_MASK, CB_CDMASK);

	return 0;
}

static int yenta_sock_suspend(struct pcmcia_socket *sock)
{
	struct yenta_socket *socket = container_of(sock, struct yenta_socket, socket);

	/* Disable CSC interrupts */
	cb_writel(socket, CB_SOCKET_MASK, 0x0);

	return 0;
}

/*
 * Use an adaptive allocation for the memory resource,
 * sometimes the memory behind pci bridges is limited:
 * 1/8 of the size of the io window of the parent.
613 614
 * max 4 MB, min 16 kB. We try very hard to not get below
 * the "ACC" values, though.
L
Linus Torvalds 已提交
615
 */
D
Dominik Brodowski 已提交
616 617 618
#define BRIDGE_MEM_MAX (4*1024*1024)
#define BRIDGE_MEM_ACC (128*1024)
#define BRIDGE_MEM_MIN (16*1024)
L
Linus Torvalds 已提交
619

620 621
#define BRIDGE_IO_MAX 512
#define BRIDGE_IO_ACC 256
L
Linus Torvalds 已提交
622 623 624 625 626 627
#define BRIDGE_IO_MIN 32

#ifndef PCIBIOS_MIN_CARDBUS_IO
#define PCIBIOS_MIN_CARDBUS_IO PCIBIOS_MIN_IO
#endif

628 629 630 631 632 633 634 635 636 637 638 639 640 641 642
static int yenta_search_one_res(struct resource *root, struct resource *res,
				u32 min)
{
	u32 align, size, start, end;

	if (res->flags & IORESOURCE_IO) {
		align = 1024;
		size = BRIDGE_IO_MAX;
		start = PCIBIOS_MIN_CARDBUS_IO;
		end = ~0U;
	} else {
		unsigned long avail = root->end - root->start;
		int i;
		size = BRIDGE_MEM_MAX;
		if (size > avail/8) {
D
Dominik Brodowski 已提交
643
			size = (avail+1)/8;
644 645 646 647 648 649 650 651 652 653 654 655 656 657 658
			/* round size down to next power of 2 */
			i = 0;
			while ((size /= 2) != 0)
				i++;
			size = 1 << i;
		}
		if (size < min)
			size = min;
		align = size;
		start = PCIBIOS_MIN_MEM;
		end = ~0U;
	}

	do {
		if (allocate_resource(root, res, size, start, end, align,
D
Dominik Brodowski 已提交
659
				      NULL, NULL) == 0) {
660 661 662 663 664 665 666 667 668 669 670 671 672
			return 1;
		}
		size = size/2;
		align = size;
	} while (size >= min);

	return 0;
}


static int yenta_search_res(struct yenta_socket *socket, struct resource *res,
			    u32 min)
{
673
	struct resource *root;
674
	int i;
675 676

	pci_bus_for_each_resource(socket->dev->bus, root, i) {
677 678 679 680 681 682 683 684 685 686 687 688 689
		if (!root)
			continue;

		if ((res->flags ^ root->flags) &
		    (IORESOURCE_IO | IORESOURCE_MEM | IORESOURCE_PREFETCH))
			continue; /* Wrong type */

		if (yenta_search_one_res(root, res, min))
			return 1;
	}
	return 0;
}

690
static int yenta_allocate_res(struct yenta_socket *socket, int nr, unsigned type, int addr_start, int addr_end)
L
Linus Torvalds 已提交
691
{
M
Matthew Wilcox 已提交
692 693
	struct pci_dev *dev = socket->dev;
	struct resource *res;
694
	struct pci_bus_region region;
L
Linus Torvalds 已提交
695 696
	unsigned mask;

M
Matthew Wilcox 已提交
697
	res = dev->resource + PCI_BRIDGE_RESOURCES + nr;
I
Ivan Kokshaysky 已提交
698 699
	/* Already allocated? */
	if (res->parent)
700
		return 0;
I
Ivan Kokshaysky 已提交
701

L
Linus Torvalds 已提交
702 703 704 705 706
	/* The granularity of the memory limit is 4kB, on IO it's 4 bytes */
	mask = ~0xfff;
	if (type & IORESOURCE_IO)
		mask = ~3;

M
Matthew Wilcox 已提交
707
	res->name = dev->subordinate->name;
L
Linus Torvalds 已提交
708 709
	res->flags = type;

710 711 712
	region.start = config_readl(socket, addr_start) & mask;
	region.end = config_readl(socket, addr_end) | ~mask;
	if (region.start && region.end > region.start && !override_bios) {
M
Matthew Wilcox 已提交
713 714
		pcibios_bus_to_resource(dev, res, &region);
		if (pci_claim_resource(dev, PCI_BRIDGE_RESOURCES + nr) == 0)
715
			return 0;
M
Matthew Wilcox 已提交
716
		dev_printk(KERN_INFO, &dev->dev,
717 718 719
			   "Preassigned resource %d busy or not available, "
			   "reconfiguring...\n",
			   nr);
L
Linus Torvalds 已提交
720 721 722
	}

	if (type & IORESOURCE_IO) {
723 724
		if ((yenta_search_res(socket, res, BRIDGE_IO_MAX)) ||
		    (yenta_search_res(socket, res, BRIDGE_IO_ACC)) ||
725 726
		    (yenta_search_res(socket, res, BRIDGE_IO_MIN)))
			return 1;
L
Linus Torvalds 已提交
727
	} else {
728 729 730
		if (type & IORESOURCE_PREFETCH) {
			if ((yenta_search_res(socket, res, BRIDGE_MEM_MAX)) ||
			    (yenta_search_res(socket, res, BRIDGE_MEM_ACC)) ||
731 732
			    (yenta_search_res(socket, res, BRIDGE_MEM_MIN)))
				return 1;
733 734
			/* Approximating prefetchable by non-prefetchable */
			res->flags = IORESOURCE_MEM;
L
Linus Torvalds 已提交
735
		}
736 737
		if ((yenta_search_res(socket, res, BRIDGE_MEM_MAX)) ||
		    (yenta_search_res(socket, res, BRIDGE_MEM_ACC)) ||
738 739
		    (yenta_search_res(socket, res, BRIDGE_MEM_MIN)))
			return 1;
740 741
	}

M
Matthew Wilcox 已提交
742
	dev_printk(KERN_INFO, &dev->dev,
743 744
		   "no resource of type %x available, trying to continue...\n",
		   type);
745
	res->start = res->end = res->flags = 0;
746
	return 0;
L
Linus Torvalds 已提交
747 748 749 750 751 752 753
}

/*
 * Allocate the bridge mappings for the device..
 */
static void yenta_allocate_resources(struct yenta_socket *socket)
{
754 755
	int program = 0;
	program += yenta_allocate_res(socket, 0, IORESOURCE_IO,
756
			   PCI_CB_IO_BASE_0, PCI_CB_IO_LIMIT_0);
757
	program += yenta_allocate_res(socket, 1, IORESOURCE_IO,
758
			   PCI_CB_IO_BASE_1, PCI_CB_IO_LIMIT_1);
759
	program += yenta_allocate_res(socket, 2, IORESOURCE_MEM|IORESOURCE_PREFETCH,
760
			   PCI_CB_MEMORY_BASE_0, PCI_CB_MEMORY_LIMIT_0);
761
	program += yenta_allocate_res(socket, 3, IORESOURCE_MEM,
762
			   PCI_CB_MEMORY_BASE_1, PCI_CB_MEMORY_LIMIT_1);
763 764
	if (program)
		pci_setup_cardbus(socket->dev->subordinate);
L
Linus Torvalds 已提交
765 766 767 768 769 770 771 772 773
}


/*
 * Free the bridge mappings for the device..
 */
static void yenta_free_resources(struct yenta_socket *socket)
{
	int i;
D
Dominik Brodowski 已提交
774
	for (i = 0; i < 4; i++) {
L
Linus Torvalds 已提交
775 776 777 778
		struct resource *res;
		res = socket->dev->resource + PCI_BRIDGE_RESOURCES + i;
		if (res->start != 0 && res->end != 0)
			release_resource(res);
779
		res->start = res->end = res->flags = 0;
L
Linus Torvalds 已提交
780 781 782 783 784 785 786
	}
}


/*
 * Close it down - release our resources and go home..
 */
787
static void __devexit yenta_close(struct pci_dev *dev)
L
Linus Torvalds 已提交
788 789 790
{
	struct yenta_socket *sock = pci_get_drvdata(dev);

791 792 793
	/* Remove the register attributes */
	device_remove_file(&dev->dev, &dev_attr_yenta_registers);

L
Linus Torvalds 已提交
794 795
	/* we don't want a dying socket registered */
	pcmcia_unregister_socket(&sock->socket);
D
Dominik Brodowski 已提交
796

L
Linus Torvalds 已提交
797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825
	/* Disable all events so we don't die in an IRQ storm */
	cb_writel(sock, CB_SOCKET_MASK, 0x0);
	exca_writeb(sock, I365_CSCINT, 0);

	if (sock->cb_irq)
		free_irq(sock->cb_irq, sock);
	else
		del_timer_sync(&sock->poll_timer);

	if (sock->base)
		iounmap(sock->base);
	yenta_free_resources(sock);

	pci_release_regions(dev);
	pci_disable_device(dev);
	pci_set_drvdata(dev, NULL);
}


static struct pccard_operations yenta_socket_operations = {
	.init			= yenta_sock_init,
	.suspend		= yenta_sock_suspend,
	.get_status		= yenta_get_status,
	.set_socket		= yenta_set_socket,
	.set_io_map		= yenta_set_io_map,
	.set_mem_map		= yenta_set_mem_map,
};


826
#ifdef CONFIG_YENTA_TI
L
Linus Torvalds 已提交
827
#include "ti113x.h"
828 829
#endif
#ifdef CONFIG_YENTA_RICOH
L
Linus Torvalds 已提交
830
#include "ricoh.h"
831 832
#endif
#ifdef CONFIG_YENTA_TOSHIBA
L
Linus Torvalds 已提交
833
#include "topic.h"
834 835
#endif
#ifdef CONFIG_YENTA_O2
L
Linus Torvalds 已提交
836
#include "o2micro.h"
837
#endif
L
Linus Torvalds 已提交
838 839 840 841 842 843 844 845

enum {
	CARDBUS_TYPE_DEFAULT = -1,
	CARDBUS_TYPE_TI,
	CARDBUS_TYPE_TI113X,
	CARDBUS_TYPE_TI12XX,
	CARDBUS_TYPE_TI1250,
	CARDBUS_TYPE_RICOH,
846
	CARDBUS_TYPE_TOPIC95,
L
Linus Torvalds 已提交
847 848
	CARDBUS_TYPE_TOPIC97,
	CARDBUS_TYPE_O2MICRO,
849
	CARDBUS_TYPE_ENE,
L
Linus Torvalds 已提交
850 851 852 853 854 855 856
};

/*
 * Different cardbus controllers have slightly different
 * initialization sequences etc details. List them here..
 */
static struct cardbus_type cardbus_type[] = {
857
#ifdef CONFIG_YENTA_TI
L
Linus Torvalds 已提交
858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881
	[CARDBUS_TYPE_TI]	= {
		.override	= ti_override,
		.save_state	= ti_save_state,
		.restore_state	= ti_restore_state,
		.sock_init	= ti_init,
	},
	[CARDBUS_TYPE_TI113X]	= {
		.override	= ti113x_override,
		.save_state	= ti_save_state,
		.restore_state	= ti_restore_state,
		.sock_init	= ti_init,
	},
	[CARDBUS_TYPE_TI12XX]	= {
		.override	= ti12xx_override,
		.save_state	= ti_save_state,
		.restore_state	= ti_restore_state,
		.sock_init	= ti_init,
	},
	[CARDBUS_TYPE_TI1250]	= {
		.override	= ti1250_override,
		.save_state	= ti_save_state,
		.restore_state	= ti_restore_state,
		.sock_init	= ti_init,
	},
882 883
#endif
#ifdef CONFIG_YENTA_RICOH
L
Linus Torvalds 已提交
884 885 886 887 888
	[CARDBUS_TYPE_RICOH]	= {
		.override	= ricoh_override,
		.save_state	= ricoh_save_state,
		.restore_state	= ricoh_restore_state,
	},
889 890
#endif
#ifdef CONFIG_YENTA_TOSHIBA
891 892 893
	[CARDBUS_TYPE_TOPIC95]	= {
		.override	= topic95_override,
	},
L
Linus Torvalds 已提交
894 895 896
	[CARDBUS_TYPE_TOPIC97]	= {
		.override	= topic97_override,
	},
897 898
#endif
#ifdef CONFIG_YENTA_O2
L
Linus Torvalds 已提交
899 900 901 902
	[CARDBUS_TYPE_O2MICRO]	= {
		.override	= o2micro_override,
		.restore_state	= o2micro_restore_state,
	},
903 904
#endif
#ifdef CONFIG_YENTA_TI
905 906 907 908 909 910
	[CARDBUS_TYPE_ENE]	= {
		.override	= ene_override,
		.save_state	= ti_save_state,
		.restore_state	= ti_restore_state,
		.sock_init	= ti_init,
	},
911
#endif
L
Linus Torvalds 已提交
912 913 914 915 916 917 918 919
};


static unsigned int yenta_probe_irq(struct yenta_socket *socket, u32 isa_irq_mask)
{
	int i;
	unsigned long val;
	u32 mask;
920
	u8 reg;
L
Linus Torvalds 已提交
921 922 923 924 925 926 927

	/*
	 * Probe for usable interrupts using the force
	 * register to generate bogus card status events.
	 */
	cb_writel(socket, CB_SOCKET_EVENT, -1);
	cb_writel(socket, CB_SOCKET_MASK, CB_CSTSMASK);
928
	reg = exca_readb(socket, I365_CSCINT);
L
Linus Torvalds 已提交
929 930 931 932 933 934 935 936 937 938 939
	exca_writeb(socket, I365_CSCINT, 0);
	val = probe_irq_on() & isa_irq_mask;
	for (i = 1; i < 16; i++) {
		if (!((val >> i) & 1))
			continue;
		exca_writeb(socket, I365_CSCINT, I365_CSC_STSCHG | (i << 4));
		cb_writel(socket, CB_SOCKET_FORCE, CB_FCARDSTS);
		udelay(100);
		cb_writel(socket, CB_SOCKET_EVENT, -1);
	}
	cb_writel(socket, CB_SOCKET_MASK, 0);
940
	exca_writeb(socket, I365_CSCINT, reg);
L
Linus Torvalds 已提交
941 942 943 944 945 946 947

	mask = probe_irq_mask(val) & 0xffff;

	return mask;
}


R
Randy Dunlap 已提交
948
/*
949 950 951 952 953
 * yenta PCI irq probing.
 * currently only used in the TI/EnE initialization code
 */
#ifdef CONFIG_YENTA_TI

L
Linus Torvalds 已提交
954
/* interrupt handler, only used during probing */
955
static irqreturn_t yenta_probe_handler(int irq, void *dev_id)
L
Linus Torvalds 已提交
956 957 958
{
	struct yenta_socket *socket = (struct yenta_socket *) dev_id;
	u8 csc;
D
Dominik Brodowski 已提交
959
	u32 cb_event;
L
Linus Torvalds 已提交
960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976

	/* Clear interrupt status for the event */
	cb_event = cb_readl(socket, CB_SOCKET_EVENT);
	cb_writel(socket, CB_SOCKET_EVENT, -1);
	csc = exca_readb(socket, I365_CSC);

	if (cb_event || csc) {
		socket->probe_status = 1;
		return IRQ_HANDLED;
	}

	return IRQ_NONE;
}

/* probes the PCI interrupt, use only on override functions */
static int yenta_probe_cb_irq(struct yenta_socket *socket)
{
977 978
	u8 reg;

L
Linus Torvalds 已提交
979 980 981 982 983
	if (!socket->cb_irq)
		return -1;

	socket->probe_status = 0;

984
	if (request_irq(socket->cb_irq, yenta_probe_handler, IRQF_SHARED, "yenta", socket)) {
985 986
		dev_printk(KERN_WARNING, &socket->dev->dev,
			   "request_irq() in yenta_probe_cb_irq() failed!\n");
L
Linus Torvalds 已提交
987 988 989 990
		return -1;
	}

	/* generate interrupt, wait */
991 992
	reg = exca_readb(socket, I365_CSCINT);
	exca_writeb(socket, I365_CSCINT, reg | I365_CSC_STSCHG);
L
Linus Torvalds 已提交
993 994 995
	cb_writel(socket, CB_SOCKET_EVENT, -1);
	cb_writel(socket, CB_SOCKET_MASK, CB_CSTSMASK);
	cb_writel(socket, CB_SOCKET_FORCE, CB_FCARDSTS);
996

L
Linus Torvalds 已提交
997 998 999 1000
	msleep(100);

	/* disable interrupts */
	cb_writel(socket, CB_SOCKET_MASK, 0);
1001
	exca_writeb(socket, I365_CSCINT, reg);
L
Linus Torvalds 已提交
1002 1003 1004 1005 1006 1007 1008 1009
	cb_writel(socket, CB_SOCKET_EVENT, -1);
	exca_readb(socket, I365_CSC);

	free_irq(socket->cb_irq, socket);

	return (int) socket->probe_status;
}

1010
#endif /* CONFIG_YENTA_TI */
L
Linus Torvalds 已提交
1011 1012 1013 1014 1015 1016 1017 1018


/*
 * Set static data that doesn't need re-initializing..
 */
static void yenta_get_socket_capabilities(struct yenta_socket *socket, u32 isa_irq_mask)
{
	socket->socket.pci_irq = socket->cb_irq;
1019 1020 1021 1022
	if (isa_probe)
		socket->socket.irq_mask = yenta_probe_irq(socket, isa_irq_mask);
	else
		socket->socket.irq_mask = 0;
L
Linus Torvalds 已提交
1023

1024 1025 1026
	dev_printk(KERN_INFO, &socket->dev->dev,
		   "ISA IRQ mask 0x%04x, PCI irq %d\n",
		   socket->socket.irq_mask, socket->cb_irq);
L
Linus Torvalds 已提交
1027 1028 1029 1030 1031 1032 1033 1034 1035
}

/*
 * Initialize the standard cardbus registers
 */
static void yenta_config_init(struct yenta_socket *socket)
{
	u16 bridge;
	struct pci_dev *dev = socket->dev;
D
Dominik Brodowski 已提交
1036
	struct pci_bus_region region;
L
Linus Torvalds 已提交
1037

D
Dominik Brodowski 已提交
1038
	pcibios_resource_to_bus(socket->dev, &region, &dev->resource[0]);
L
Linus Torvalds 已提交
1039 1040

	config_writel(socket, CB_LEGACY_MODE_BASE, 0);
D
Dominik Brodowski 已提交
1041
	config_writel(socket, PCI_BASE_ADDRESS_0, region.start);
L
Linus Torvalds 已提交
1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063
	config_writew(socket, PCI_COMMAND,
			PCI_COMMAND_IO |
			PCI_COMMAND_MEMORY |
			PCI_COMMAND_MASTER |
			PCI_COMMAND_WAIT);

	/* MAGIC NUMBERS! Fixme */
	config_writeb(socket, PCI_CACHE_LINE_SIZE, L1_CACHE_BYTES / 4);
	config_writeb(socket, PCI_LATENCY_TIMER, 168);
	config_writel(socket, PCI_PRIMARY_BUS,
		(176 << 24) |			   /* sec. latency timer */
		(dev->subordinate->subordinate << 16) | /* subordinate bus */
		(dev->subordinate->secondary << 8) |  /* secondary bus */
		dev->subordinate->primary);		   /* primary bus */

	/*
	 * Set up the bridging state:
	 *  - enable write posting.
	 *  - memory window 0 prefetchable, window 1 non-prefetchable
	 *  - PCI interrupts enabled if a PCI interrupt exists..
	 */
	bridge = config_readw(socket, CB_BRIDGE_CONTROL);
1064 1065
	bridge &= ~(CB_BRIDGE_CRST | CB_BRIDGE_PREFETCH1 | CB_BRIDGE_ISAEN | CB_BRIDGE_VGAEN);
	bridge |= CB_BRIDGE_PREFETCH0 | CB_BRIDGE_POSTEN;
L
Linus Torvalds 已提交
1066 1067 1068
	config_writew(socket, CB_BRIDGE_CONTROL, bridge);
}

1069
/**
1070
 * yenta_fixup_parent_bridge - Fix subordinate bus# of the parent bridge
1071 1072 1073 1074 1075 1076
 * @cardbus_bridge: The PCI bus which the CardBus bridge bridges to
 *
 * Checks if devices on the bus which the CardBus bridge bridges to would be
 * invisible during PCI scans because of a misconfigured subordinate number
 * of the parent brige - some BIOSes seem to be too lazy to set it right.
 * Does the fixup carefully by checking how far it can go without conflicts.
R
Randy Dunlap 已提交
1077
 * See http\://bugzilla.kernel.org/show_bug.cgi?id=2944 for more information.
1078 1079 1080 1081 1082
 */
static void yenta_fixup_parent_bridge(struct pci_bus *cardbus_bridge)
{
	struct list_head *tmp;
	unsigned char upper_limit;
D
Dominik Brodowski 已提交
1083
	/*
1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101
	 * We only check and fix the parent bridge: All systems which need
	 * this fixup that have been reviewed are laptops and the only bridge
	 * which needed fixing was the parent bridge of the CardBus bridge:
	 */
	struct pci_bus *bridge_to_fix = cardbus_bridge->parent;

	/* Check bus numbers are already set up correctly: */
	if (bridge_to_fix->subordinate >= cardbus_bridge->subordinate)
		return; /* The subordinate number is ok, nothing to do */

	if (!bridge_to_fix->parent)
		return; /* Root bridges are ok */

	/* stay within the limits of the bus range of the parent: */
	upper_limit = bridge_to_fix->parent->subordinate;

	/* check the bus ranges of all silbling bridges to prevent overlap */
	list_for_each(tmp, &bridge_to_fix->parent->children) {
D
Dominik Brodowski 已提交
1102
		struct pci_bus *silbling = pci_bus_b(tmp);
1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115
		/*
		 * If the silbling has a higher secondary bus number
		 * and it's secondary is equal or smaller than our
		 * current upper limit, set the new upper limit to
		 * the bus number below the silbling's range:
		 */
		if (silbling->secondary > bridge_to_fix->subordinate
		    && silbling->secondary <= upper_limit)
			upper_limit = silbling->secondary - 1;
	}

	/* Show that the wanted subordinate number is not possible: */
	if (cardbus_bridge->subordinate > upper_limit)
1116 1117 1118
		dev_printk(KERN_WARNING, &cardbus_bridge->dev,
			   "Upper limit for fixing this "
			   "bridge's parent bridge: #%02x\n", upper_limit);
1119 1120 1121 1122 1123 1124 1125 1126

	/* If we have room to increase the bridge's subordinate number, */
	if (bridge_to_fix->subordinate < upper_limit) {

		/* use the highest number of the hidden bus, within limits */
		unsigned char subordinate_to_assign =
			min(cardbus_bridge->subordinate, upper_limit);

1127 1128 1129 1130 1131
		dev_printk(KERN_INFO, &bridge_to_fix->dev,
			   "Raising subordinate bus# of parent "
			   "bus (#%02x) from #%02x to #%02x\n",
			   bridge_to_fix->number,
			   bridge_to_fix->subordinate, subordinate_to_assign);
1132 1133 1134 1135 1136 1137 1138 1139 1140 1141

		/* Save the new subordinate in the bus struct of the bridge */
		bridge_to_fix->subordinate = subordinate_to_assign;

		/* and update the PCI config space with the new subordinate */
		pci_write_config_byte(bridge_to_fix->self,
			PCI_SUBORDINATE_BUS, bridge_to_fix->subordinate);
	}
}

L
Linus Torvalds 已提交
1142 1143 1144 1145 1146
/*
 * Initialize a cardbus controller. Make sure we have a usable
 * interrupt, and that we can map the cardbus area. Fill in the
 * socket information structure..
 */
D
Dominik Brodowski 已提交
1147
static int __devinit yenta_probe(struct pci_dev *dev, const struct pci_device_id *id)
L
Linus Torvalds 已提交
1148 1149 1150
{
	struct yenta_socket *socket;
	int ret;
I
Ivan Kokshaysky 已提交
1151 1152 1153 1154 1155 1156 1157

	/*
	 * If we failed to assign proper bus numbers for this cardbus
	 * controller during PCI probe, its subordinate pci_bus is NULL.
	 * Bail out if so.
	 */
	if (!dev->subordinate) {
1158 1159
		dev_printk(KERN_ERR, &dev->dev, "no bus associated! "
			   "(try 'pci=assign-busses')\n");
I
Ivan Kokshaysky 已提交
1160 1161 1162
		return -ENODEV;
	}

1163
	socket = kzalloc(sizeof(struct yenta_socket), GFP_KERNEL);
L
Linus Torvalds 已提交
1164 1165 1166 1167 1168 1169
	if (!socket)
		return -ENOMEM;

	/* prepare pcmcia_socket */
	socket->socket.ops = &yenta_socket_operations;
	socket->socket.resource_ops = &pccard_nonstatic_ops;
1170
	socket->socket.dev.parent = &dev->dev;
L
Linus Torvalds 已提交
1171 1172
	socket->socket.driver_data = socket;
	socket->socket.owner = THIS_MODULE;
1173 1174 1175
	socket->socket.features = SS_CAP_PAGE_REGS | SS_CAP_PCCARD;
	socket->socket.map_size = 0x1000;
	socket->socket.cb_dev = dev;
L
Linus Torvalds 已提交
1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193

	/* prepare struct yenta_socket */
	socket->dev = dev;
	pci_set_drvdata(dev, socket);

	/*
	 * Do some basic sanity checking..
	 */
	if (pci_enable_device(dev)) {
		ret = -EBUSY;
		goto free;
	}

	ret = pci_request_regions(dev, "yenta_socket");
	if (ret)
		goto disable;

	if (!pci_resource_start(dev, 0)) {
1194
		dev_printk(KERN_ERR, &dev->dev, "No cardbus resource!\n");
L
Linus Torvalds 已提交
1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212
		ret = -ENODEV;
		goto release;
	}

	/*
	 * Ok, start setup.. Map the cardbus registers,
	 * and request the IRQ.
	 */
	socket->base = ioremap(pci_resource_start(dev, 0), 0x1000);
	if (!socket->base) {
		ret = -ENOMEM;
		goto release;
	}

	/*
	 * report the subsystem vendor and device for help debugging
	 * the irq stuff...
	 */
1213 1214
	dev_printk(KERN_INFO, &dev->dev, "CardBus bridge found [%04x:%04x]\n",
		   dev->subsystem_vendor, dev->subsystem_device);
L
Linus Torvalds 已提交
1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237

	yenta_config_init(socket);

	/* Disable all events */
	cb_writel(socket, CB_SOCKET_MASK, 0x0);

	/* Set up the bridge regions.. */
	yenta_allocate_resources(socket);

	socket->cb_irq = dev->irq;

	/* Do we have special options for the device? */
	if (id->driver_data != CARDBUS_TYPE_DEFAULT &&
	    id->driver_data < ARRAY_SIZE(cardbus_type)) {
		socket->type = &cardbus_type[id->driver_data];

		ret = socket->type->override(socket);
		if (ret < 0)
			goto unmap;
	}

	/* We must finish initialization here */

1238
	if (!socket->cb_irq || request_irq(socket->cb_irq, yenta_interrupt, IRQF_SHARED, "yenta", socket)) {
L
Linus Torvalds 已提交
1239 1240 1241 1242 1243 1244 1245
		/* No IRQ or request_irq failed. Poll */
		socket->cb_irq = 0; /* But zero is a valid IRQ number. */
		init_timer(&socket->poll_timer);
		socket->poll_timer.function = yenta_interrupt_wrapper;
		socket->poll_timer.data = (unsigned long)socket;
		socket->poll_timer.expires = jiffies + HZ;
		add_timer(&socket->poll_timer);
1246 1247 1248 1249 1250 1251
		dev_printk(KERN_INFO, &dev->dev,
			   "no PCI IRQ, CardBus support disabled for this "
			   "socket.\n");
		dev_printk(KERN_INFO, &dev->dev,
			   "check your BIOS CardBus, BIOS IRQ or ACPI "
			   "settings.\n");
1252 1253
	} else {
		socket->socket.features |= SS_CAP_CARDBUS;
L
Linus Torvalds 已提交
1254 1255 1256
	}

	/* Figure out what the dang thing can do for the PCMCIA layer... */
1257
	yenta_interrogate(socket);
L
Linus Torvalds 已提交
1258
	yenta_get_socket_capabilities(socket, isa_interrupts);
1259 1260
	dev_printk(KERN_INFO, &dev->dev,
		   "Socket status: %08x\n", cb_readl(socket, CB_SOCKET_STATE));
L
Linus Torvalds 已提交
1261

1262 1263
	yenta_fixup_parent_bridge(dev->subordinate);

L
Linus Torvalds 已提交
1264 1265
	/* Register it with the pcmcia layer.. */
	ret = pcmcia_register_socket(&socket->socket);
1266 1267
	if (ret == 0) {
		/* Add the yenta register attributes */
1268 1269 1270 1271 1272 1273
		ret = device_create_file(&dev->dev, &dev_attr_yenta_registers);
		if (ret == 0)
			goto out;

		/* error path... */
		pcmcia_unregister_socket(&socket->socket);
1274
	}
L
Linus Torvalds 已提交
1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287

 unmap:
	iounmap(socket->base);
 release:
	pci_release_regions(dev);
 disable:
	pci_disable_device(dev);
 free:
	kfree(socket);
 out:
	return ret;
}

1288
#ifdef CONFIG_PM
1289
static int yenta_dev_suspend_noirq(struct device *dev)
L
Linus Torvalds 已提交
1290
{
1291 1292
	struct pci_dev *pdev = to_pci_dev(dev);
	struct yenta_socket *socket = pci_get_drvdata(pdev);
L
Linus Torvalds 已提交
1293 1294
	int ret;

1295
	ret = pcmcia_socket_dev_suspend(dev);
L
Linus Torvalds 已提交
1296

1297 1298
	if (!socket)
		return ret;
L
Linus Torvalds 已提交
1299

1300 1301
	if (socket->type && socket->type->save_state)
		socket->type->save_state(socket);
L
Linus Torvalds 已提交
1302

1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313
	pci_save_state(pdev);
	pci_read_config_dword(pdev, 16*4, &socket->saved_state[0]);
	pci_read_config_dword(pdev, 17*4, &socket->saved_state[1]);
	pci_disable_device(pdev);

	/*
	 * Some laptops (IBM T22) do not like us putting the Cardbus
	 * bridge into D3.  At a guess, some other laptop will
	 * probably require this, so leave it commented out for now.
	 */
	/* pci_set_power_state(dev, 3); */
L
Linus Torvalds 已提交
1314 1315 1316 1317

	return ret;
}

1318
static int yenta_dev_resume_noirq(struct device *dev)
L
Linus Torvalds 已提交
1319
{
1320 1321 1322
	struct pci_dev *pdev = to_pci_dev(dev);
	struct yenta_socket *socket = pci_get_drvdata(pdev);
	int ret;
L
Linus Torvalds 已提交
1323

1324 1325
	if (!socket)
		return 0;
1326

1327 1328
	pci_write_config_dword(pdev, 16*4, socket->saved_state[0]);
	pci_write_config_dword(pdev, 17*4, socket->saved_state[1]);
1329

1330 1331 1332
	ret = pci_enable_device(pdev);
	if (ret)
		return ret;
1333

1334
	pci_set_master(pdev);
L
Linus Torvalds 已提交
1335

1336 1337
	if (socket->type && socket->type->restore_state)
		socket->type->restore_state(socket);
L
Linus Torvalds 已提交
1338

1339 1340 1341 1342 1343 1344 1345 1346
	pcmcia_socket_dev_early_resume(dev);
	return 0;
}

static int yenta_dev_resume(struct device *dev)
{
	pcmcia_socket_dev_late_resume(dev);
	return 0;
L
Linus Torvalds 已提交
1347
}
1348

1349
static const struct dev_pm_ops yenta_pm_ops = {
1350 1351
	.suspend_noirq = yenta_dev_suspend_noirq,
	.resume_noirq = yenta_dev_resume_noirq,
1352
	.resume = yenta_dev_resume,
1353 1354
	.freeze_noirq = yenta_dev_suspend_noirq,
	.thaw_noirq = yenta_dev_resume_noirq,
1355
	.thaw = yenta_dev_resume,
1356 1357
	.poweroff_noirq = yenta_dev_suspend_noirq,
	.restore_noirq = yenta_dev_resume_noirq,
1358
	.restore = yenta_dev_resume,
1359 1360 1361 1362 1363
};

#define YENTA_PM_OPS	(&yenta_pm_ops)
#else
#define YENTA_PM_OPS	NULL
1364
#endif
L
Linus Torvalds 已提交
1365

D
Dominik Brodowski 已提交
1366
#define CB_ID(vend, dev, type)				\
L
Linus Torvalds 已提交
1367 1368 1369 1370 1371 1372 1373 1374 1375 1376
	{						\
		.vendor		= vend,			\
		.device		= dev,			\
		.subvendor	= PCI_ANY_ID,		\
		.subdevice	= PCI_ANY_ID,		\
		.class		= PCI_CLASS_BRIDGE_CARDBUS << 8, \
		.class_mask	= ~0,			\
		.driver_data	= CARDBUS_TYPE_##type,	\
	}

D
Dominik Brodowski 已提交
1377
static struct pci_device_id yenta_table[] = {
L
Linus Torvalds 已提交
1378 1379 1380 1381 1382 1383 1384
	CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_1031, TI),

	/*
	 * TBD: Check if these TI variants can use more
	 * advanced overrides instead.  (I can't get the
	 * data sheets for these devices. --rmk)
	 */
1385
#ifdef CONFIG_YENTA_TI
L
Linus Torvalds 已提交
1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411
	CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_1210, TI),

	CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_1130, TI113X),
	CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_1131, TI113X),

	CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_1211, TI12XX),
	CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_1220, TI12XX),
	CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_1221, TI12XX),
	CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_1225, TI12XX),
	CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_1251A, TI12XX),
	CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_1251B, TI12XX),
	CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_1420, TI12XX),
	CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_1450, TI12XX),
	CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_1451A, TI12XX),
	CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_1510, TI12XX),
	CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_1520, TI12XX),
	CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_1620, TI12XX),
	CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_4410, TI12XX),
	CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_4450, TI12XX),
	CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_4451, TI12XX),
	CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_4510, TI12XX),
	CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_4520, TI12XX),

	CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_1250, TI1250),
	CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_1410, TI1250),

1412 1413
	CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_XX21_XX11, TI12XX),
	CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_X515, TI12XX),
1414
	CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_XX12, TI12XX),
1415 1416 1417 1418 1419 1420
	CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_X420, TI12XX),
	CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_X620, TI12XX),
	CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_7410, TI12XX),
	CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_7510, TI12XX),
	CB_ID(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_7610, TI12XX),

1421 1422 1423 1424
	CB_ID(PCI_VENDOR_ID_ENE, PCI_DEVICE_ID_ENE_710, ENE),
	CB_ID(PCI_VENDOR_ID_ENE, PCI_DEVICE_ID_ENE_712, ENE),
	CB_ID(PCI_VENDOR_ID_ENE, PCI_DEVICE_ID_ENE_720, ENE),
	CB_ID(PCI_VENDOR_ID_ENE, PCI_DEVICE_ID_ENE_722, ENE),
1425 1426 1427 1428
	CB_ID(PCI_VENDOR_ID_ENE, PCI_DEVICE_ID_ENE_1211, ENE),
	CB_ID(PCI_VENDOR_ID_ENE, PCI_DEVICE_ID_ENE_1225, ENE),
	CB_ID(PCI_VENDOR_ID_ENE, PCI_DEVICE_ID_ENE_1410, ENE),
	CB_ID(PCI_VENDOR_ID_ENE, PCI_DEVICE_ID_ENE_1420, ENE),
1429
#endif /* CONFIG_YENTA_TI */
L
Linus Torvalds 已提交
1430

1431
#ifdef CONFIG_YENTA_RICOH
L
Linus Torvalds 已提交
1432 1433 1434 1435 1436
	CB_ID(PCI_VENDOR_ID_RICOH, PCI_DEVICE_ID_RICOH_RL5C465, RICOH),
	CB_ID(PCI_VENDOR_ID_RICOH, PCI_DEVICE_ID_RICOH_RL5C466, RICOH),
	CB_ID(PCI_VENDOR_ID_RICOH, PCI_DEVICE_ID_RICOH_RL5C475, RICOH),
	CB_ID(PCI_VENDOR_ID_RICOH, PCI_DEVICE_ID_RICOH_RL5C476, RICOH),
	CB_ID(PCI_VENDOR_ID_RICOH, PCI_DEVICE_ID_RICOH_RL5C478, RICOH),
1437
#endif
L
Linus Torvalds 已提交
1438

1439
#ifdef CONFIG_YENTA_TOSHIBA
1440
	CB_ID(PCI_VENDOR_ID_TOSHIBA, PCI_DEVICE_ID_TOSHIBA_TOPIC95, TOPIC95),
L
Linus Torvalds 已提交
1441 1442
	CB_ID(PCI_VENDOR_ID_TOSHIBA, PCI_DEVICE_ID_TOSHIBA_TOPIC97, TOPIC97),
	CB_ID(PCI_VENDOR_ID_TOSHIBA, PCI_DEVICE_ID_TOSHIBA_TOPIC100, TOPIC97),
1443
#endif
L
Linus Torvalds 已提交
1444

1445
#ifdef CONFIG_YENTA_O2
L
Linus Torvalds 已提交
1446
	CB_ID(PCI_VENDOR_ID_O2, PCI_ANY_ID, O2MICRO),
1447
#endif
L
Linus Torvalds 已提交
1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460

	/* match any cardbus bridge */
	CB_ID(PCI_ANY_ID, PCI_ANY_ID, DEFAULT),
	{ /* all zeroes */ }
};
MODULE_DEVICE_TABLE(pci, yenta_table);


static struct pci_driver yenta_cardbus_driver = {
	.name		= "yenta_cardbus",
	.id_table	= yenta_table,
	.probe		= yenta_probe,
	.remove		= __devexit_p(yenta_close),
1461
	.driver.pm	= YENTA_PM_OPS,
L
Linus Torvalds 已提交
1462 1463 1464 1465 1466
};


static int __init yenta_socket_init(void)
{
D
Dominik Brodowski 已提交
1467
	return pci_register_driver(&yenta_cardbus_driver);
L
Linus Torvalds 已提交
1468 1469 1470
}


D
Dominik Brodowski 已提交
1471
static void __exit yenta_socket_exit(void)
L
Linus Torvalds 已提交
1472
{
D
Dominik Brodowski 已提交
1473
	pci_unregister_driver(&yenta_cardbus_driver);
L
Linus Torvalds 已提交
1474 1475 1476 1477 1478 1479 1480
}


module_init(yenta_socket_init);
module_exit(yenta_socket_exit);

MODULE_LICENSE("GPL");