pci.c 12.7 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/*
 * Copyright (C) 2004 Benjamin Herrenschmuidt (benh@kernel.crashing.org),
 *		      IBM Corp.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version
 * 2 of the License, or (at your option) any later version.
 */

#define DEBUG

#include <linux/kernel.h>
#include <linux/pci.h>
#include <linux/delay.h>
#include <linux/string.h>
#include <linux/init.h>
#include <linux/bootmem.h>

#include <asm/sections.h>
#include <asm/io.h>
#include <asm/prom.h>
#include <asm/pci-bridge.h>
#include <asm/machdep.h>
#include <asm/iommu.h>
26
#include <asm/ppc-pci.h>
L
Linus Torvalds 已提交
27

28 29
#include "maple.h"

L
Linus Torvalds 已提交
30 31 32 33 34 35 36 37 38 39 40
#ifdef DEBUG
#define DBG(x...) printk(x)
#else
#define DBG(x...)
#endif

static struct pci_controller *u3_agp, *u3_ht;

static int __init fixup_one_level_bus_range(struct device_node *node, int higher)
{
	for (; node != 0;node = node->sibling) {
41 42
		const int *bus_range;
		const unsigned int *class_code;
L
Linus Torvalds 已提交
43 44 45
		int len;

		/* For PCI<->PCI bridges or CardBus bridges, we go down */
46
		class_code = get_property(node, "class-code", NULL);
L
Linus Torvalds 已提交
47 48 49
		if (!class_code || ((*class_code >> 8) != PCI_CLASS_BRIDGE_PCI &&
			(*class_code >> 8) != PCI_CLASS_BRIDGE_CARDBUS))
			continue;
50
		bus_range = get_property(node, "bus-range", &len);
L
Linus Torvalds 已提交
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
		if (bus_range != NULL && len > 2 * sizeof(int)) {
			if (bus_range[1] > higher)
				higher = bus_range[1];
		}
		higher = fixup_one_level_bus_range(node->child, higher);
	}
	return higher;
}

/* This routine fixes the "bus-range" property of all bridges in the
 * system since they tend to have their "last" member wrong on macs
 *
 * Note that the bus numbers manipulated here are OF bus numbers, they
 * are not Linux bus numbers.
 */
static void __init fixup_bus_range(struct device_node *bridge)
{
68 69
	int *bus_range;
	struct property *prop;
L
Linus Torvalds 已提交
70 71 72
	int len;

	/* Lookup the "bus-range" property for the hose */
73 74
	prop = of_find_property(bridge, "bus-range", &len);
	if (prop == NULL  || prop->value == NULL || len < 2 * sizeof(int)) {
L
Linus Torvalds 已提交
75 76 77 78
		printk(KERN_WARNING "Can't get bus-range for %s\n",
			       bridge->full_name);
		return;
	}
79
	bus_range = (int *)prop->value;
L
Linus Torvalds 已提交
80 81 82 83
	bus_range[1] = fixup_one_level_bus_range(bridge->child, bus_range[1]);
}


84 85 86 87 88 89
static unsigned long u3_agp_cfa0(u8 devfn, u8 off)
{
	return (1 << (unsigned long)PCI_SLOT(devfn)) |
		((unsigned long)PCI_FUNC(devfn) << 8) |
		((unsigned long)off & 0xFCUL);
}
L
Linus Torvalds 已提交
90

91 92 93 94 95 96 97
static unsigned long u3_agp_cfa1(u8 bus, u8 devfn, u8 off)
{
	return ((unsigned long)bus << 16) |
		((unsigned long)devfn << 8) |
		((unsigned long)off & 0xFCUL) |
		1UL;
}
L
Linus Torvalds 已提交
98

A
Al Viro 已提交
99
static volatile void __iomem *u3_agp_cfg_access(struct pci_controller* hose,
L
Linus Torvalds 已提交
100 101 102 103 104 105
				       u8 bus, u8 dev_fn, u8 offset)
{
	unsigned int caddr;

	if (bus == hose->first_busno) {
		if (dev_fn < (11 << 3))
A
Al Viro 已提交
106
			return NULL;
107
		caddr = u3_agp_cfa0(dev_fn, offset);
L
Linus Torvalds 已提交
108
	} else
109
		caddr = u3_agp_cfa1(bus, dev_fn, offset);
L
Linus Torvalds 已提交
110 111 112 113 114 115 116

	/* Uninorth will return garbage if we don't read back the value ! */
	do {
		out_le32(hose->cfg_addr, caddr);
	} while (in_le32(hose->cfg_addr) != caddr);

	offset &= 0x07;
A
Al Viro 已提交
117
	return hose->cfg_data + offset;
L
Linus Torvalds 已提交
118 119 120 121 122 123
}

static int u3_agp_read_config(struct pci_bus *bus, unsigned int devfn,
			      int offset, int len, u32 *val)
{
	struct pci_controller *hose;
A
Al Viro 已提交
124
	volatile void __iomem *addr;
L
Linus Torvalds 已提交
125 126 127 128 129 130 131 132 133 134 135 136 137 138

	hose = pci_bus_to_host(bus);
	if (hose == NULL)
		return PCIBIOS_DEVICE_NOT_FOUND;

	addr = u3_agp_cfg_access(hose, bus->number, devfn, offset);
	if (!addr)
		return PCIBIOS_DEVICE_NOT_FOUND;
	/*
	 * Note: the caller has already checked that offset is
	 * suitably aligned and that len is 1, 2 or 4.
	 */
	switch (len) {
	case 1:
A
Al Viro 已提交
139
		*val = in_8(addr);
L
Linus Torvalds 已提交
140 141
		break;
	case 2:
A
Al Viro 已提交
142
		*val = in_le16(addr);
L
Linus Torvalds 已提交
143 144
		break;
	default:
A
Al Viro 已提交
145
		*val = in_le32(addr);
L
Linus Torvalds 已提交
146 147 148 149 150 151 152 153 154
		break;
	}
	return PCIBIOS_SUCCESSFUL;
}

static int u3_agp_write_config(struct pci_bus *bus, unsigned int devfn,
			       int offset, int len, u32 val)
{
	struct pci_controller *hose;
A
Al Viro 已提交
155
	volatile void __iomem *addr;
L
Linus Torvalds 已提交
156 157 158 159 160 161 162 163 164 165 166 167 168 169

	hose = pci_bus_to_host(bus);
	if (hose == NULL)
		return PCIBIOS_DEVICE_NOT_FOUND;

	addr = u3_agp_cfg_access(hose, bus->number, devfn, offset);
	if (!addr)
		return PCIBIOS_DEVICE_NOT_FOUND;
	/*
	 * Note: the caller has already checked that offset is
	 * suitably aligned and that len is 1, 2 or 4.
	 */
	switch (len) {
	case 1:
A
Al Viro 已提交
170 171
		out_8(addr, val);
		(void) in_8(addr);
L
Linus Torvalds 已提交
172 173
		break;
	case 2:
A
Al Viro 已提交
174 175
		out_le16(addr, val);
		(void) in_le16(addr);
L
Linus Torvalds 已提交
176 177
		break;
	default:
A
Al Viro 已提交
178 179
		out_le32(addr, val);
		(void) in_le32(addr);
L
Linus Torvalds 已提交
180 181 182 183 184 185 186 187 188 189 190
		break;
	}
	return PCIBIOS_SUCCESSFUL;
}

static struct pci_ops u3_agp_pci_ops =
{
	u3_agp_read_config,
	u3_agp_write_config
};

191 192 193 194
static unsigned long u3_ht_cfa0(u8 devfn, u8 off)
{
	return (devfn << 8) | off;
}
L
Linus Torvalds 已提交
195

196 197 198 199
static unsigned long u3_ht_cfa1(u8 bus, u8 devfn, u8 off)
{
	return u3_ht_cfa0(devfn, off) + (bus << 16) + 0x01000000UL;
}
L
Linus Torvalds 已提交
200

A
Al Viro 已提交
201
static volatile void __iomem *u3_ht_cfg_access(struct pci_controller* hose,
L
Linus Torvalds 已提交
202 203 204 205
				      u8 bus, u8 devfn, u8 offset)
{
	if (bus == hose->first_busno) {
		if (PCI_SLOT(devfn) == 0)
A
Al Viro 已提交
206 207
			return NULL;
		return hose->cfg_data + u3_ht_cfa0(devfn, offset);
L
Linus Torvalds 已提交
208
	} else
A
Al Viro 已提交
209
		return hose->cfg_data + u3_ht_cfa1(bus, devfn, offset);
L
Linus Torvalds 已提交
210 211 212 213 214 215
}

static int u3_ht_read_config(struct pci_bus *bus, unsigned int devfn,
			     int offset, int len, u32 *val)
{
	struct pci_controller *hose;
A
Al Viro 已提交
216
	volatile void __iomem *addr;
L
Linus Torvalds 已提交
217 218 219 220 221

	hose = pci_bus_to_host(bus);
	if (hose == NULL)
		return PCIBIOS_DEVICE_NOT_FOUND;

222 223 224
	if (offset > 0xff)
		return PCIBIOS_BAD_REGISTER_NUMBER;

L
Linus Torvalds 已提交
225 226 227 228 229 230 231 232 233 234
	addr = u3_ht_cfg_access(hose, bus->number, devfn, offset);
	if (!addr)
		return PCIBIOS_DEVICE_NOT_FOUND;

	/*
	 * Note: the caller has already checked that offset is
	 * suitably aligned and that len is 1, 2 or 4.
	 */
	switch (len) {
	case 1:
A
Al Viro 已提交
235
		*val = in_8(addr);
L
Linus Torvalds 已提交
236 237
		break;
	case 2:
A
Al Viro 已提交
238
		*val = in_le16(addr);
L
Linus Torvalds 已提交
239 240
		break;
	default:
A
Al Viro 已提交
241
		*val = in_le32(addr);
L
Linus Torvalds 已提交
242 243 244 245 246 247 248 249 250
		break;
	}
	return PCIBIOS_SUCCESSFUL;
}

static int u3_ht_write_config(struct pci_bus *bus, unsigned int devfn,
			      int offset, int len, u32 val)
{
	struct pci_controller *hose;
A
Al Viro 已提交
251
	volatile void __iomem *addr;
L
Linus Torvalds 已提交
252 253 254 255 256

	hose = pci_bus_to_host(bus);
	if (hose == NULL)
		return PCIBIOS_DEVICE_NOT_FOUND;

257 258 259
	if (offset > 0xff)
		return PCIBIOS_BAD_REGISTER_NUMBER;

L
Linus Torvalds 已提交
260 261 262 263 264 265 266 267 268
	addr = u3_ht_cfg_access(hose, bus->number, devfn, offset);
	if (!addr)
		return PCIBIOS_DEVICE_NOT_FOUND;
	/*
	 * Note: the caller has already checked that offset is
	 * suitably aligned and that len is 1, 2 or 4.
	 */
	switch (len) {
	case 1:
A
Al Viro 已提交
269 270
		out_8(addr, val);
		(void) in_8(addr);
L
Linus Torvalds 已提交
271 272
		break;
	case 2:
A
Al Viro 已提交
273 274
		out_le16(addr, val);
		(void) in_le16(addr);
L
Linus Torvalds 已提交
275 276
		break;
	default:
A
Al Viro 已提交
277 278
		out_le32(addr, val);
		(void) in_le32(addr);
L
Linus Torvalds 已提交
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
		break;
	}
	return PCIBIOS_SUCCESSFUL;
}

static struct pci_ops u3_ht_pci_ops =
{
	u3_ht_read_config,
	u3_ht_write_config
};

static void __init setup_u3_agp(struct pci_controller* hose)
{
	/* On G5, we move AGP up to high bus number so we don't need
	 * to reassign bus numbers for HT. If we ever have P2P bridges
294
	 * on AGP, we'll have to move pci_assign_all_buses to the
L
Linus Torvalds 已提交
295 296 297 298 299 300
	 * pci_controller structure so we enable it for AGP and not for
	 * HT childs.
	 * We hard code the address because of the different size of
	 * the reg address cell, we shall fix that by killing struct
	 * reg_property and using some accessor functions instead
	 */
A
Anton Blanchard 已提交
301
	hose->first_busno = 0xf0;
L
Linus Torvalds 已提交
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
	hose->last_busno = 0xff;
	hose->ops = &u3_agp_pci_ops;
	hose->cfg_addr = ioremap(0xf0000000 + 0x800000, 0x1000);
	hose->cfg_data = ioremap(0xf0000000 + 0xc00000, 0x1000);

	u3_agp = hose;
}

static void __init setup_u3_ht(struct pci_controller* hose)
{
	hose->ops = &u3_ht_pci_ops;

	/* We hard code the address because of the different size of
	 * the reg address cell, we shall fix that by killing struct
	 * reg_property and using some accessor functions instead
	 */
A
Al Viro 已提交
318
	hose->cfg_data = ioremap(0xf2000000, 0x02000000);
L
Linus Torvalds 已提交
319 320 321 322 323 324 325 326 327 328 329 330

	hose->first_busno = 0;
	hose->last_busno = 0xef;

	u3_ht = hose;
}

static int __init add_bridge(struct device_node *dev)
{
	int len;
	struct pci_controller *hose;
	char* disp_name;
331
	const int *bus_range;
L
Linus Torvalds 已提交
332 333 334 335
	int primary = 1;

	DBG("Adding PCI host bridge %s\n", dev->full_name);

336
	bus_range = get_property(dev, "bus-range", &len);
A
Anton Blanchard 已提交
337 338 339 340
	if (bus_range == NULL || len < 2 * sizeof(int)) {
		printk(KERN_WARNING "Can't get bus-range for %s, assume bus 0\n",
		dev->full_name);
	}
L
Linus Torvalds 已提交
341

342
	hose = pcibios_alloc_controller(dev);
L
Linus Torvalds 已提交
343 344
	if (hose == NULL)
		return -ENOMEM;
A
Anton Blanchard 已提交
345 346
	hose->first_busno = bus_range ? bus_range[0] : 0;
	hose->last_busno = bus_range ? bus_range[1] : 0xff;
L
Linus Torvalds 已提交
347 348

	disp_name = NULL;
A
Anton Blanchard 已提交
349 350 351 352 353 354 355 356 357 358 359 360 361 362
	if (device_is_compatible(dev, "u3-agp")) {
		setup_u3_agp(hose);
		disp_name = "U3-AGP";
		primary = 0;
	} else if (device_is_compatible(dev, "u3-ht")) {
		setup_u3_ht(hose);
		disp_name = "U3-HT";
		primary = 1;
	}
	printk(KERN_INFO "Found %s PCI host bridge. Firmware bus number: %d->%d\n",
		disp_name, hose->first_busno, hose->last_busno);

	/* Interpret the "ranges" property */
	/* This also maps the I/O region and sets isa_io/mem_base */
363
	pci_process_bridge_OF_ranges(hose, dev, primary);
L
Linus Torvalds 已提交
364 365
	pci_setup_phb_io(hose, primary);

A
Anton Blanchard 已提交
366 367
	/* Fixup "bus-range" OF property */
	fixup_bus_range(dev);
L
Linus Torvalds 已提交
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392

	return 0;
}


void __init maple_pcibios_fixup(void)
{
	struct pci_dev *dev = NULL;

	DBG(" -> maple_pcibios_fixup\n");

	for_each_pci_dev(dev)
		pci_read_irq_line(dev);

	DBG(" <- maple_pcibios_fixup\n");
}

static void __init maple_fixup_phb_resources(void)
{
	struct pci_controller *hose, *tmp;
	
	list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
		unsigned long offset = (unsigned long)hose->io_base_virt - pci_io_base;
		hose->io_resource.start += offset;
		hose->io_resource.end += offset;
393
		printk(KERN_INFO "PCI Host %d, io start: %llx; io end: %llx\n",
L
Linus Torvalds 已提交
394
		       hose->global_number,
395 396
		       (unsigned long long)hose->io_resource.start,
		       (unsigned long long)hose->io_resource.end);
L
Linus Torvalds 已提交
397 398 399 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 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
	}
}

void __init maple_pci_init(void)
{
	struct device_node *np, *root;
	struct device_node *ht = NULL;

	/* Probe root PCI hosts, that is on U3 the AGP host and the
	 * HyperTransport host. That one is actually "kept" around
	 * and actually added last as it's resource management relies
	 * on the AGP resources to have been setup first
	 */
	root = of_find_node_by_path("/");
	if (root == NULL) {
		printk(KERN_CRIT "maple_find_bridges: can't find root of device tree\n");
		return;
	}
	for (np = NULL; (np = of_get_next_child(root, np)) != NULL;) {
		if (np->name == NULL)
			continue;
		if (strcmp(np->name, "pci") == 0) {
			if (add_bridge(np) == 0)
				of_node_get(np);
		}
		if (strcmp(np->name, "ht") == 0) {
			of_node_get(np);
			ht = np;
		}
	}
	of_node_put(root);

	/* Now setup the HyperTransport host if we found any
	 */
	if (ht && add_bridge(ht) != 0)
		of_node_put(ht);

	/* Fixup the IO resources on our host bridges as the common code
	 * does it only for childs of the host bridges
	 */
	maple_fixup_phb_resources();

	/* Setup the linkage between OF nodes and PHBs */ 
	pci_devs_phb_init();

	/* Fixup the PCI<->OF mapping for U3 AGP due to bus renumbering. We
	 * assume there is no P2P bridge on the AGP bus, which should be a
	 * safe assumptions hopefully.
	 */
	if (u3_agp) {
		struct device_node *np = u3_agp->arch_data;
448
		PCI_DN(np)->busno = 0xf0;
L
Linus Torvalds 已提交
449
		for (np = np->child; np; np = np->sibling)
450
			PCI_DN(np)->busno = 0xf0;
L
Linus Torvalds 已提交
451 452
	}

453 454
	/* Tell pci.c to not change any resource allocations.  */
	pci_probe_only = 1;
L
Linus Torvalds 已提交
455 456 457 458 459
}

int maple_pci_get_legacy_ide_irq(struct pci_dev *pdev, int channel)
{
	struct device_node *np;
460 461
	unsigned int defirq = channel ? 15 : 14;
	unsigned int irq;
L
Linus Torvalds 已提交
462 463 464

	if (pdev->vendor != PCI_VENDOR_ID_AMD ||
	    pdev->device != PCI_DEVICE_ID_AMD_8111_IDE)
465
		return defirq;
L
Linus Torvalds 已提交
466 467 468

	np = pci_device_to_OF_node(pdev);
	if (np == NULL)
469 470 471 472 473 474 475 476
		return defirq;
	irq = irq_of_parse_and_map(np, channel & 0x1);
	if (irq == NO_IRQ) {
		printk("Failed to map onboard IDE interrupt for channel %d\n",
		       channel);
		return defirq;
	}
	return irq;
L
Linus Torvalds 已提交
477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521
}

/* XXX: To remove once all firmwares are ok */
static void fixup_maple_ide(struct pci_dev* dev)
{
#if 0 /* Enable this to enable IDE port 0 */
	{
		u8 v;

		pci_read_config_byte(dev, 0x40, &v);
		v |= 2;
		pci_write_config_byte(dev, 0x40, v);
	}
#endif
#if 0 /* fix bus master base */
	pci_write_config_dword(dev, 0x20, 0xcc01);
	printk("old ide resource: %lx -> %lx \n",
	       dev->resource[4].start, dev->resource[4].end);
	dev->resource[4].start = 0xcc00;
	dev->resource[4].end = 0xcc10;
#endif
#if 1 /* Enable this to fixup IDE sense/polarity of irqs in IO-APICs */
	{
		struct pci_dev *apicdev;
		u32 v;

		apicdev = pci_get_slot (dev->bus, PCI_DEVFN(5,0));
		if (apicdev == NULL)
			printk("IDE Fixup IRQ: Can't find IO-APIC !\n");
		else {
			pci_write_config_byte(apicdev, 0xf2, 0x10 + 2*14);
			pci_read_config_dword(apicdev, 0xf4, &v);
			v &= ~0x00000022;
			pci_write_config_dword(apicdev, 0xf4, v);
			pci_write_config_byte(apicdev, 0xf2, 0x10 + 2*15);
			pci_read_config_dword(apicdev, 0xf4, &v);
			v &= ~0x00000022;
			pci_write_config_dword(apicdev, 0xf4, v);
			pci_dev_put(apicdev);
		}
	}
#endif
}
DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8111_IDE,
			 fixup_maple_ide);