prom_parse.c 26.9 KB
Newer Older
1 2 3 4 5 6
#undef DEBUG

#include <linux/kernel.h>
#include <linux/string.h>
#include <linux/pci_regs.h>
#include <linux/module.h>
7
#include <linux/ioport.h>
8
#include <linux/etherdevice.h>
9
#include <asm/prom.h>
10
#include <asm/pci-bridge.h>
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26

#ifdef DEBUG
#define DBG(fmt...) do { printk(fmt); } while(0)
#else
#define DBG(fmt...) do { } while(0)
#endif

#ifdef CONFIG_PPC64
#define PRu64	"%lx"
#else
#define PRu64	"%llx"
#endif

/* Max address size we deal with */
#define OF_MAX_ADDR_CELLS	4
#define OF_CHECK_COUNTS(na, ns)	((na) > 0 && (na) <= OF_MAX_ADDR_CELLS && \
27
			(ns) > 0)
28

29 30 31 32 33 34
static struct of_bus *of_match_bus(struct device_node *np);
static int __of_address_to_resource(struct device_node *dev,
		const u32 *addrp, u64 size, unsigned int flags,
		struct resource *r);


35 36
/* Debug utility */
#ifdef DEBUG
37
static void of_dump_addr(const char *s, const u32 *addr, int na)
38 39 40 41 42 43 44
{
	printk("%s", s);
	while(na--)
		printk(" %08x", *(addr++));
	printk("\n");
}
#else
45
static void of_dump_addr(const char *s, const u32 *addr, int na) { }
46 47 48 49 50 51 52 53 54 55
#endif


/* Callbacks for bus specific translators */
struct of_bus {
	const char	*name;
	const char	*addresses;
	int		(*match)(struct device_node *parent);
	void		(*count_cells)(struct device_node *child,
				       int *addrc, int *sizec);
56 57
	u64		(*map)(u32 *addr, const u32 *range,
				int na, int ns, int pna);
58
	int		(*translate)(u32 *addr, u64 offset, int na);
59
	unsigned int	(*get_flags)(const u32 *addr);
60 61 62 63 64 65 66
};


/*
 * Default translator (generic bus)
 */

67 68
static void of_bus_default_count_cells(struct device_node *dev,
				       int *addrc, int *sizec)
69 70
{
	if (addrc)
71
		*addrc = of_n_addr_cells(dev);
72
	if (sizec)
73
		*sizec = of_n_size_cells(dev);
74 75
}

76 77
static u64 of_bus_default_map(u32 *addr, const u32 *range,
		int na, int ns, int pna)
78 79 80
{
	u64 cp, s, da;

81 82 83
	cp = of_read_number(range, na);
	s  = of_read_number(range + na + pna, ns);
	da = of_read_number(addr, na);
84 85 86 87 88 89 90 91 92

	DBG("OF: default map, cp="PRu64", s="PRu64", da="PRu64"\n",
	    cp, s, da);

	if (da < cp || da >= (cp + s))
		return OF_BAD_ADDR;
	return da - cp;
}

93
static int of_bus_default_translate(u32 *addr, u64 offset, int na)
94
{
95
	u64 a = of_read_number(addr, na);
96 97 98 99 100 101 102 103 104
	memset(addr, 0, na * 4);
	a += offset;
	if (na > 1)
		addr[na - 2] = a >> 32;
	addr[na - 1] = a & 0xffffffffu;

	return 0;
}

105
static unsigned int of_bus_default_get_flags(const u32 *addr)
106 107 108 109
{
	return IORESOURCE_MEM;
}

110

111
#ifdef CONFIG_PCI
112 113 114 115 116 117
/*
 * PCI bus specific translator
 */

static int of_bus_pci_match(struct device_node *np)
{
118 119
	/* "vci" is for the /chaos bridge on 1st-gen PCI powermacs */
	return !strcmp(np->type, "pci") || !strcmp(np->type, "vci");
120 121 122 123 124 125 126 127 128 129 130
}

static void of_bus_pci_count_cells(struct device_node *np,
				   int *addrc, int *sizec)
{
	if (addrc)
		*addrc = 3;
	if (sizec)
		*sizec = 2;
}

131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
static unsigned int of_bus_pci_get_flags(const u32 *addr)
{
	unsigned int flags = 0;
	u32 w = addr[0];

	switch((w >> 24) & 0x03) {
	case 0x01:
		flags |= IORESOURCE_IO;
		break;
	case 0x02: /* 32 bits */
	case 0x03: /* 64 bits */
		flags |= IORESOURCE_MEM;
		break;
	}
	if (w & 0x40000000)
		flags |= IORESOURCE_PREFETCH;
	return flags;
}

150
static u64 of_bus_pci_map(u32 *addr, const u32 *range, int na, int ns, int pna)
151 152
{
	u64 cp, s, da;
153 154 155 156
	unsigned int af, rf;

	af = of_bus_pci_get_flags(addr);
	rf = of_bus_pci_get_flags(range);
157 158

	/* Check address type match */
159
	if ((af ^ rf) & (IORESOURCE_MEM | IORESOURCE_IO))
160 161 162
		return OF_BAD_ADDR;

	/* Read address values, skipping high cell */
163 164 165
	cp = of_read_number(range + 1, na - 1);
	s  = of_read_number(range + na + pna, ns);
	da = of_read_number(addr + 1, na - 1);
166 167 168 169 170 171 172 173 174 175

	DBG("OF: PCI map, cp="PRu64", s="PRu64", da="PRu64"\n", cp, s, da);

	if (da < cp || da >= (cp + s))
		return OF_BAD_ADDR;
	return da - cp;
}

static int of_bus_pci_translate(u32 *addr, u64 offset, int na)
{
176 177 178
	return of_bus_default_translate(addr + 1, offset, na - 1);
}

179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
const u32 *of_get_pci_address(struct device_node *dev, int bar_no, u64 *size,
			unsigned int *flags)
{
	const u32 *prop;
	unsigned int psize;
	struct device_node *parent;
	struct of_bus *bus;
	int onesize, i, na, ns;

	/* Get parent & match bus type */
	parent = of_get_parent(dev);
	if (parent == NULL)
		return NULL;
	bus = of_match_bus(parent);
	if (strcmp(bus->name, "pci")) {
		of_node_put(parent);
		return NULL;
	}
	bus->count_cells(dev, &na, &ns);
	of_node_put(parent);
	if (!OF_CHECK_COUNTS(na, ns))
		return NULL;

	/* Get "reg" or "assigned-addresses" property */
203
	prop = of_get_property(dev, bus->addresses, &psize);
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
	if (prop == NULL)
		return NULL;
	psize /= 4;

	onesize = na + ns;
	for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++)
		if ((prop[0] & 0xff) == ((bar_no * 4) + PCI_BASE_ADDRESS_0)) {
			if (size)
				*size = of_read_number(prop + na, ns);
			if (flags)
				*flags = bus->get_flags(prop);
			return prop;
		}
	return NULL;
}
EXPORT_SYMBOL(of_get_pci_address);

int of_pci_address_to_resource(struct device_node *dev, int bar,
			       struct resource *r)
{
	const u32	*addrp;
	u64		size;
	unsigned int	flags;

	addrp = of_get_pci_address(dev, bar, &size, &flags);
	if (addrp == NULL)
		return -EINVAL;
	return __of_address_to_resource(dev, addrp, size, flags, r);
}
EXPORT_SYMBOL_GPL(of_pci_address_to_resource);

static u8 of_irq_pci_swizzle(u8 slot, u8 pin)
{
	return (((pin - 1) + slot) % 4) + 1;
}

int of_irq_map_pci(struct pci_dev *pdev, struct of_irq *out_irq)
{
	struct device_node *dn, *ppnode;
	struct pci_dev *ppdev;
	u32 lspec;
	u32 laddr[3];
	u8 pin;
	int rc;

	/* Check if we have a device node, if yes, fallback to standard OF
	 * parsing
	 */
	dn = pci_device_to_OF_node(pdev);
253 254 255 256 257
	if (dn) {
		rc = of_irq_map_one(dn, 0, out_irq);
		if (!rc)
			return rc;
	}
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282

	/* Ok, we don't, time to have fun. Let's start by building up an
	 * interrupt spec.  we assume #interrupt-cells is 1, which is standard
	 * for PCI. If you do different, then don't use that routine.
	 */
	rc = pci_read_config_byte(pdev, PCI_INTERRUPT_PIN, &pin);
	if (rc != 0)
		return rc;
	/* No pin, exit */
	if (pin == 0)
		return -ENODEV;

	/* Now we walk up the PCI tree */
	lspec = pin;
	for (;;) {
		/* Get the pci_dev of our parent */
		ppdev = pdev->bus->self;

		/* Ouch, it's a host bridge... */
		if (ppdev == NULL) {
#ifdef CONFIG_PPC64
			ppnode = pci_bus_to_OF_node(pdev->bus);
#else
			struct pci_controller *host;
			host = pci_bus_to_host(pdev->bus);
283
			ppnode = host ? host->dn : NULL;
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
#endif
			/* No node for host bridge ? give up */
			if (ppnode == NULL)
				return -EINVAL;
		} else
			/* We found a P2P bridge, check if it has a node */
			ppnode = pci_device_to_OF_node(ppdev);

		/* Ok, we have found a parent with a device-node, hand over to
		 * the OF parsing code.
		 * We build a unit address from the linux device to be used for
		 * resolution. Note that we use the linux bus number which may
		 * not match your firmware bus numbering.
		 * Fortunately, in most cases, interrupt-map-mask doesn't include
		 * the bus number as part of the matching.
		 * You should still be careful about that though if you intend
		 * to rely on this function (you ship  a firmware that doesn't
		 * create device nodes for all PCI devices).
		 */
		if (ppnode)
			break;

		/* We can only get here if we hit a P2P bridge with no node,
		 * let's do standard swizzling and try again
		 */
		lspec = of_irq_pci_swizzle(PCI_SLOT(pdev->devfn), lspec);
		pdev = ppdev;
	}

	laddr[0] = (pdev->bus->number << 16)
		| (pdev->devfn << 8);
	laddr[1]  = laddr[2] = 0;
	return of_irq_map_raw(ppnode, &lspec, 1, laddr, out_irq);
}
EXPORT_SYMBOL_GPL(of_irq_map_pci);
#endif /* CONFIG_PCI */

321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
/*
 * ISA bus specific translator
 */

static int of_bus_isa_match(struct device_node *np)
{
	return !strcmp(np->name, "isa");
}

static void of_bus_isa_count_cells(struct device_node *child,
				   int *addrc, int *sizec)
{
	if (addrc)
		*addrc = 2;
	if (sizec)
		*sizec = 1;
}

339
static u64 of_bus_isa_map(u32 *addr, const u32 *range, int na, int ns, int pna)
340 341 342 343 344 345 346 347
{
	u64 cp, s, da;

	/* Check address type match */
	if ((addr[0] ^ range[0]) & 0x00000001)
		return OF_BAD_ADDR;

	/* Read address values, skipping high cell */
348 349 350
	cp = of_read_number(range + 1, na - 1);
	s  = of_read_number(range + na + pna, ns);
	da = of_read_number(addr + 1, na - 1);
351 352 353 354 355 356 357 358 359 360

	DBG("OF: ISA map, cp="PRu64", s="PRu64", da="PRu64"\n", cp, s, da);

	if (da < cp || da >= (cp + s))
		return OF_BAD_ADDR;
	return da - cp;
}

static int of_bus_isa_translate(u32 *addr, u64 offset, int na)
{
361 362 363
	return of_bus_default_translate(addr + 1, offset, na - 1);
}

364
static unsigned int of_bus_isa_get_flags(const u32 *addr)
365 366 367 368 369 370 371 372 373
{
	unsigned int flags = 0;
	u32 w = addr[0];

	if (w & 1)
		flags |= IORESOURCE_IO;
	else
		flags |= IORESOURCE_MEM;
	return flags;
374 375
}

376

377 378 379 380 381
/*
 * Array of bus specific translators
 */

static struct of_bus of_busses[] = {
382
#ifdef CONFIG_PCI
383 384 385 386 387 388 389 390
	/* PCI */
	{
		.name = "pci",
		.addresses = "assigned-addresses",
		.match = of_bus_pci_match,
		.count_cells = of_bus_pci_count_cells,
		.map = of_bus_pci_map,
		.translate = of_bus_pci_translate,
391
		.get_flags = of_bus_pci_get_flags,
392
	},
393
#endif /* CONFIG_PCI */
394 395 396 397 398 399 400 401
	/* ISA */
	{
		.name = "isa",
		.addresses = "reg",
		.match = of_bus_isa_match,
		.count_cells = of_bus_isa_count_cells,
		.map = of_bus_isa_map,
		.translate = of_bus_isa_translate,
402
		.get_flags = of_bus_isa_get_flags,
403 404 405 406 407 408
	},
	/* Default */
	{
		.name = "default",
		.addresses = "reg",
		.match = NULL,
409 410 411 412
		.count_cells = of_bus_default_count_cells,
		.map = of_bus_default_map,
		.translate = of_bus_default_translate,
		.get_flags = of_bus_default_get_flags,
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
	},
};

static struct of_bus *of_match_bus(struct device_node *np)
{
	int i;

	for (i = 0; i < ARRAY_SIZE(of_busses); i ++)
		if (!of_busses[i].match || of_busses[i].match(np))
			return &of_busses[i];
	BUG();
	return NULL;
}

static int of_translate_one(struct device_node *parent, struct of_bus *bus,
			    struct of_bus *pbus, u32 *addr,
429
			    int na, int ns, int pna, const char *rprop)
430
{
431
	const u32 *ranges;
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
	unsigned int rlen;
	int rone;
	u64 offset = OF_BAD_ADDR;

	/* Normally, an absence of a "ranges" property means we are
	 * crossing a non-translatable boundary, and thus the addresses
	 * below the current not cannot be converted to CPU physical ones.
	 * Unfortunately, while this is very clear in the spec, it's not
	 * what Apple understood, and they do have things like /uni-n or
	 * /ht nodes with no "ranges" property and a lot of perfectly
	 * useable mapped devices below them. Thus we treat the absence of
	 * "ranges" as equivalent to an empty "ranges" property which means
	 * a 1:1 translation at that level. It's up to the caller not to try
	 * to translate addresses that aren't supposed to be translated in
	 * the first place. --BenH.
	 */
448
	ranges = of_get_property(parent, rprop, &rlen);
449
	if (ranges == NULL || rlen == 0) {
450
		offset = of_read_number(addr, na);
451 452
		memset(addr, 0, pna * 4);
		DBG("OF: no ranges, 1:1 translation\n");
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
		goto finish;
	}

	DBG("OF: walking ranges...\n");

	/* Now walk through the ranges */
	rlen /= 4;
	rone = na + pna + ns;
	for (; rlen >= rone; rlen -= rone, ranges += rone) {
		offset = bus->map(addr, ranges, na, ns, pna);
		if (offset != OF_BAD_ADDR)
			break;
	}
	if (offset == OF_BAD_ADDR) {
		DBG("OF: not found !\n");
		return 1;
	}
	memcpy(addr, ranges + na, 4 * pna);

 finish:
	of_dump_addr("OF: parent translation for:", addr, pna);
474
	DBG("OF: with offset: "PRu64"\n", offset);
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490

	/* Translate it into parent bus space */
	return pbus->translate(addr, offset, pna);
}


/*
 * Translate an address from the device-tree into a CPU physical address,
 * this walks up the tree and applies the various bus mappings on the
 * way.
 *
 * Note: We consider that crossing any level with #size-cells == 0 to mean
 * that translation is impossible (that is we are not dealing with a value
 * that can be mapped to a cpu physical address). This is not really specified
 * that way, but this is traditionally the way IBM at least do things
 */
491 492
u64 __of_translate_address(struct device_node *dev, const u32 *in_addr,
			   const char *rprop)
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 522 523 524 525 526 527 528 529 530 531 532 533
{
	struct device_node *parent = NULL;
	struct of_bus *bus, *pbus;
	u32 addr[OF_MAX_ADDR_CELLS];
	int na, ns, pna, pns;
	u64 result = OF_BAD_ADDR;

	DBG("OF: ** translation for device %s **\n", dev->full_name);

	/* Increase refcount at current level */
	of_node_get(dev);

	/* Get parent & match bus type */
	parent = of_get_parent(dev);
	if (parent == NULL)
		goto bail;
	bus = of_match_bus(parent);

	/* Cound address cells & copy address locally */
	bus->count_cells(dev, &na, &ns);
	if (!OF_CHECK_COUNTS(na, ns)) {
		printk(KERN_ERR "prom_parse: Bad cell count for %s\n",
		       dev->full_name);
		goto bail;
	}
	memcpy(addr, in_addr, na * 4);

	DBG("OF: bus is %s (na=%d, ns=%d) on %s\n",
	    bus->name, na, ns, parent->full_name);
	of_dump_addr("OF: translating address:", addr, na);

	/* Translate */
	for (;;) {
		/* Switch to parent bus */
		of_node_put(dev);
		dev = parent;
		parent = of_get_parent(dev);

		/* If root, we have finished */
		if (parent == NULL) {
			DBG("OF: reached root node\n");
534
			result = of_read_number(addr, na);
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
			break;
		}

		/* Get new parent bus and counts */
		pbus = of_match_bus(parent);
		pbus->count_cells(dev, &pna, &pns);
		if (!OF_CHECK_COUNTS(pna, pns)) {
			printk(KERN_ERR "prom_parse: Bad cell count for %s\n",
			       dev->full_name);
			break;
		}

		DBG("OF: parent bus is %s (na=%d, ns=%d) on %s\n",
		    pbus->name, pna, pns, parent->full_name);

		/* Apply bus translation */
551
		if (of_translate_one(dev, bus, pbus, addr, na, ns, pna, rprop))
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566
			break;

		/* Complete the move up one level */
		na = pna;
		ns = pns;
		bus = pbus;

		of_dump_addr("OF: one level translation:", addr, na);
	}
 bail:
	of_node_put(parent);
	of_node_put(dev);

	return result;
}
567 568 569 570 571

u64 of_translate_address(struct device_node *dev, const u32 *in_addr)
{
	return __of_translate_address(dev, in_addr, "ranges");
}
572 573
EXPORT_SYMBOL(of_translate_address);

574 575 576 577 578 579
u64 of_translate_dma_address(struct device_node *dev, const u32 *in_addr)
{
	return __of_translate_address(dev, in_addr, "dma-ranges");
}
EXPORT_SYMBOL(of_translate_dma_address);

580
const u32 *of_get_address(struct device_node *dev, int index, u64 *size,
581
		    unsigned int *flags)
582
{
583
	const u32 *prop;
584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599
	unsigned int psize;
	struct device_node *parent;
	struct of_bus *bus;
	int onesize, i, na, ns;

	/* Get parent & match bus type */
	parent = of_get_parent(dev);
	if (parent == NULL)
		return NULL;
	bus = of_match_bus(parent);
	bus->count_cells(dev, &na, &ns);
	of_node_put(parent);
	if (!OF_CHECK_COUNTS(na, ns))
		return NULL;

	/* Get "reg" or "assigned-addresses" property */
600
	prop = of_get_property(dev, bus->addresses, &psize);
601 602 603 604 605 606 607 608
	if (prop == NULL)
		return NULL;
	psize /= 4;

	onesize = na + ns;
	for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++)
		if (i == index) {
			if (size)
609
				*size = of_read_number(prop + na, ns);
610 611
			if (flags)
				*flags = bus->get_flags(prop);
612 613 614 615 616 617
			return prop;
		}
	return NULL;
}
EXPORT_SYMBOL(of_get_address);

618
static int __of_address_to_resource(struct device_node *dev, const u32 *addrp,
619 620 621 622 623 624 625 626 627 628 629 630
				    u64 size, unsigned int flags,
				    struct resource *r)
{
	u64 taddr;

	if ((flags & (IORESOURCE_IO | IORESOURCE_MEM)) == 0)
		return -EINVAL;
	taddr = of_translate_address(dev, addrp);
	if (taddr == OF_BAD_ADDR)
		return -EINVAL;
	memset(r, 0, sizeof(struct resource));
	if (flags & IORESOURCE_IO) {
631
		unsigned long port;
632
		port = pci_address_to_pio(taddr);
633
		if (port == (unsigned long)-1)
634 635 636 637 638 639 640 641 642 643 644 645 646 647 648
			return -EINVAL;
		r->start = port;
		r->end = port + size - 1;
	} else {
		r->start = taddr;
		r->end = taddr + size - 1;
	}
	r->flags = flags;
	r->name = dev->name;
	return 0;
}

int of_address_to_resource(struct device_node *dev, int index,
			   struct resource *r)
{
649
	const u32	*addrp;
650 651 652 653 654 655 656 657 658 659
	u64		size;
	unsigned int	flags;

	addrp = of_get_address(dev, index, &size, &flags);
	if (addrp == NULL)
		return -EINVAL;
	return __of_address_to_resource(dev, addrp, size, flags, r);
}
EXPORT_SYMBOL_GPL(of_address_to_resource);

660
void of_parse_dma_window(struct device_node *dn, const void *dma_window_prop,
661 662
		unsigned long *busno, unsigned long *phys, unsigned long *size)
{
663 664 665
	const u32 *dma_window;
	u32 cells;
	const unsigned char *prop;
666

667
	dma_window = dma_window_prop;
668 669 670 671

	/* busno is always one cell */
	*busno = *(dma_window++);

672
	prop = of_get_property(dn, "ibm,#dma-address-cells", NULL);
673
	if (!prop)
674
		prop = of_get_property(dn, "#address-cells", NULL);
675

676
	cells = prop ? *(u32 *)prop : of_n_addr_cells(dn);
677
	*phys = of_read_number(dma_window, cells);
678 679 680

	dma_window += cells;

681
	prop = of_get_property(dn, "ibm,#dma-size-cells", NULL);
682
	cells = prop ? *(u32 *)prop : of_n_size_cells(dn);
683 684 685 686 687 688 689 690 691 692 693 694 695
	*size = of_read_number(dma_window, cells);
}

/*
 * Interrupt remapper
 */

static unsigned int of_irq_workarounds;
static struct device_node *of_irq_dflt_pic;

static struct device_node *of_irq_find_parent(struct device_node *child)
{
	struct device_node *p;
696
	const phandle *parp;
697 698 699 700 701

	if (!of_node_get(child))
		return NULL;

	do {
702
		parp = of_get_property(child, "interrupt-parent", NULL);
703 704 705 706 707 708 709 710 711 712
		if (parp == NULL)
			p = of_get_parent(child);
		else {
			if (of_irq_workarounds & OF_IMAP_NO_PHANDLE)
				p = of_node_get(of_irq_dflt_pic);
			else
				p = of_find_node_by_phandle(*parp);
		}
		of_node_put(child);
		child = p;
713
	} while (p && of_get_property(p, "#interrupt-cells", NULL) == NULL);
714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736

	return p;
}

/* This doesn't need to be called if you don't have any special workaround
 * flags to pass
 */
void of_irq_map_init(unsigned int flags)
{
	of_irq_workarounds = flags;

	/* OldWorld, don't bother looking at other things */
	if (flags & OF_IMAP_OLDWORLD_MAC)
		return;

	/* If we don't have phandles, let's try to locate a default interrupt
	 * controller (happens when booting with BootX). We do a first match
	 * here, hopefully, that only ever happens on machines with one
	 * controller.
	 */
	if (flags & OF_IMAP_NO_PHANDLE) {
		struct device_node *np;

737
		for_each_node_with_property(np, "interrupt-controller") {
738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755
			/* Skip /chosen/interrupt-controller */
			if (strcmp(np->name, "chosen") == 0)
				continue;
			/* It seems like at least one person on this planet wants
			 * to use BootX on a machine with an AppleKiwi controller
			 * which happens to pretend to be an interrupt
			 * controller too.
			 */
			if (strcmp(np->name, "AppleKiwi") == 0)
				continue;
			/* I think we found one ! */
			of_irq_dflt_pic = np;
			break;
		}
	}

}

P
Paul Mackerras 已提交
756
int of_irq_map_raw(struct device_node *parent, const u32 *intspec, u32 ointsize,
757
		const u32 *addr, struct of_irq *out_irq)
758 759
{
	struct device_node *ipar, *tnode, *old = NULL, *newpar = NULL;
760
	const u32 *tmp, *imap, *imask;
761 762 763
	u32 intsize = 1, addrsize, newintsize = 0, newaddrsize = 0;
	int imaplen, match, i;

764 765 766
	DBG("of_irq_map_raw: par=%s,intspec=[0x%08x 0x%08x...],ointsize=%d\n",
	    parent->full_name, intspec[0], intspec[1], ointsize);

767 768 769 770 771 772 773
	ipar = of_node_get(parent);

	/* First get the #interrupt-cells property of the current cursor
	 * that tells us how to interpret the passed-in intspec. If there
	 * is none, we are nice and just walk up the tree
	 */
	do {
774
		tmp = of_get_property(ipar, "#interrupt-cells", NULL);
775 776 777 778 779 780 781 782 783 784 785 786 787 788 789
		if (tmp != NULL) {
			intsize = *tmp;
			break;
		}
		tnode = ipar;
		ipar = of_irq_find_parent(ipar);
		of_node_put(tnode);
	} while (ipar);
	if (ipar == NULL) {
		DBG(" -> no parent found !\n");
		goto fail;
	}

	DBG("of_irq_map_raw: ipar=%s, size=%d\n", ipar->full_name, intsize);

790 791 792
	if (ointsize != intsize)
		return -EINVAL;

793 794 795 796 797
	/* Look for this #address-cells. We have to implement the old linux
	 * trick of looking for the parent here as some device-trees rely on it
	 */
	old = of_node_get(ipar);
	do {
798
		tmp = of_get_property(old, "#address-cells", NULL);
799 800 801 802 803 804 805 806 807 808 809 810 811 812 813
		tnode = of_get_parent(old);
		of_node_put(old);
		old = tnode;
	} while(old && tmp == NULL);
	of_node_put(old);
	old = NULL;
	addrsize = (tmp == NULL) ? 2 : *tmp;

	DBG(" -> addrsize=%d\n", addrsize);

	/* Now start the actual "proper" walk of the interrupt tree */
	while (ipar != NULL) {
		/* Now check if cursor is an interrupt-controller and if it is
		 * then we are done
		 */
814 815
		if (of_get_property(ipar, "interrupt-controller", NULL) !=
				NULL) {
816 817 818 819 820 821 822 823 824 825
			DBG(" -> got it !\n");
			memcpy(out_irq->specifier, intspec,
			       intsize * sizeof(u32));
			out_irq->size = intsize;
			out_irq->controller = ipar;
			of_node_put(old);
			return 0;
		}

		/* Now look for an interrupt-map */
826
		imap = of_get_property(ipar, "interrupt-map", &imaplen);
827 828 829 830 831 832 833 834 835
		/* No interrupt map, check for an interrupt parent */
		if (imap == NULL) {
			DBG(" -> no map, getting parent\n");
			newpar = of_irq_find_parent(ipar);
			goto skiplevel;
		}
		imaplen /= sizeof(u32);

		/* Look for a mask */
836
		imask = of_get_property(ipar, "interrupt-map-mask", NULL);
837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882

		/* If we were passed no "reg" property and we attempt to parse
		 * an interrupt-map, then #address-cells must be 0.
		 * Fail if it's not.
		 */
		if (addr == NULL && addrsize != 0) {
			DBG(" -> no reg passed in when needed !\n");
			goto fail;
		}

		/* Parse interrupt-map */
		match = 0;
		while (imaplen > (addrsize + intsize + 1) && !match) {
			/* Compare specifiers */
			match = 1;
			for (i = 0; i < addrsize && match; ++i) {
				u32 mask = imask ? imask[i] : 0xffffffffu;
				match = ((addr[i] ^ imap[i]) & mask) == 0;
			}
			for (; i < (addrsize + intsize) && match; ++i) {
				u32 mask = imask ? imask[i] : 0xffffffffu;
				match =
				   ((intspec[i-addrsize] ^ imap[i]) & mask) == 0;
			}
			imap += addrsize + intsize;
			imaplen -= addrsize + intsize;

			DBG(" -> match=%d (imaplen=%d)\n", match, imaplen);

			/* Get the interrupt parent */
			if (of_irq_workarounds & OF_IMAP_NO_PHANDLE)
				newpar = of_node_get(of_irq_dflt_pic);
			else
				newpar = of_find_node_by_phandle((phandle)*imap);
			imap++;
			--imaplen;

			/* Check if not found */
			if (newpar == NULL) {
				DBG(" -> imap parent not found !\n");
				goto fail;
			}

			/* Get #interrupt-cells and #address-cells of new
			 * parent
			 */
883
			tmp = of_get_property(newpar, "#interrupt-cells", NULL);
884 885 886 887 888
			if (tmp == NULL) {
				DBG(" -> parent lacks #interrupt-cells !\n");
				goto fail;
			}
			newintsize = *tmp;
889
			tmp = of_get_property(newpar, "#address-cells", NULL);
890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933
			newaddrsize = (tmp == NULL) ? 0 : *tmp;

			DBG(" -> newintsize=%d, newaddrsize=%d\n",
			    newintsize, newaddrsize);

			/* Check for malformed properties */
			if (imaplen < (newaddrsize + newintsize))
				goto fail;

			imap += newaddrsize + newintsize;
			imaplen -= newaddrsize + newintsize;

			DBG(" -> imaplen=%d\n", imaplen);
		}
		if (!match)
			goto fail;

		of_node_put(old);
		old = of_node_get(newpar);
		addrsize = newaddrsize;
		intsize = newintsize;
		intspec = imap - intsize;
		addr = intspec - addrsize;

	skiplevel:
		/* Iterate again with new parent */
		DBG(" -> new parent: %s\n", newpar ? newpar->full_name : "<>");
		of_node_put(ipar);
		ipar = newpar;
		newpar = NULL;
	}
 fail:
	of_node_put(ipar);
	of_node_put(old);
	of_node_put(newpar);

	return -EINVAL;
}
EXPORT_SYMBOL_GPL(of_irq_map_raw);

#if defined(CONFIG_PPC_PMAC) && defined(CONFIG_PPC32)
static int of_irq_map_oldworld(struct device_node *device, int index,
			       struct of_irq *out_irq)
{
934
	const u32 *ints = NULL;
935 936 937 938
	int intlen;

	/*
	 * Old machines just have a list of interrupt numbers
939 940 941 942 943
	 * and no interrupt-controller nodes. We also have dodgy
	 * cases where the APPL,interrupts property is completely
	 * missing behind pci-pci bridges and we have to get it
	 * from the parent (the bridge itself, as apple just wired
	 * everything together on these)
944
	 */
945
	while (device) {
946
		ints = of_get_property(device, "AAPL,interrupts", &intlen);
947 948 949 950 951 952
		if (ints != NULL)
			break;
		device = device->parent;
		if (device && strcmp(device->type, "pci") != 0)
			break;
	}
953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976
	if (ints == NULL)
		return -EINVAL;
	intlen /= sizeof(u32);

	if (index >= intlen)
		return -EINVAL;

	out_irq->controller = NULL;
	out_irq->specifier[0] = ints[index];
	out_irq->size = 1;

	return 0;
}
#else /* defined(CONFIG_PPC_PMAC) && defined(CONFIG_PPC32) */
static int of_irq_map_oldworld(struct device_node *device, int index,
			       struct of_irq *out_irq)
{
	return -EINVAL;
}
#endif /* !(defined(CONFIG_PPC_PMAC) && defined(CONFIG_PPC32)) */

int of_irq_map_one(struct device_node *device, int index, struct of_irq *out_irq)
{
	struct device_node *p;
977 978
	const u32 *intspec, *tmp, *addr;
	u32 intsize, intlen;
979 980 981 982 983 984 985 986 987
	int res;

	DBG("of_irq_map_one: dev=%s, index=%d\n", device->full_name, index);

	/* OldWorld mac stuff is "special", handle out of line */
	if (of_irq_workarounds & OF_IMAP_OLDWORLD_MAC)
		return of_irq_map_oldworld(device, index, out_irq);

	/* Get the interrupts property */
988
	intspec = of_get_property(device, "interrupts", &intlen);
989 990 991 992 993
	if (intspec == NULL)
		return -EINVAL;
	intlen /= sizeof(u32);

	/* Get the reg property (if any) */
994
	addr = of_get_property(device, "reg", NULL);
995 996 997 998 999 1000 1001

	/* Look for the interrupt parent. */
	p = of_irq_find_parent(device);
	if (p == NULL)
		return -EINVAL;

	/* Get size of interrupt specifier */
1002
	tmp = of_get_property(p, "#interrupt-cells", NULL);
1003 1004 1005 1006 1007 1008
	if (tmp == NULL) {
		of_node_put(p);
		return -EINVAL;
	}
	intsize = *tmp;

1009 1010
	DBG(" intsize=%d intlen=%d\n", intsize, intlen);

1011
	/* Check index */
1012
	if ((index + 1) * intsize > intlen)
1013 1014 1015
		return -EINVAL;

	/* Get new specifier and map it */
1016 1017
	res = of_irq_map_raw(p, intspec + index * intsize, intsize,
			     addr, out_irq);
1018 1019 1020 1021
	of_node_put(p);
	return res;
}
EXPORT_SYMBOL_GPL(of_irq_map_one);
1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060

/**
 * Search the device tree for the best MAC address to use.  'mac-address' is
 * checked first, because that is supposed to contain to "most recent" MAC
 * address. If that isn't set, then 'local-mac-address' is checked next,
 * because that is the default address.  If that isn't set, then the obsolete
 * 'address' is checked, just in case we're using an old device tree.
 *
 * Note that the 'address' property is supposed to contain a virtual address of
 * the register set, but some DTS files have redefined that property to be the
 * MAC address.
 *
 * All-zero MAC addresses are rejected, because those could be properties that
 * exist in the device tree, but were not set by U-Boot.  For example, the
 * DTS could define 'mac-address' and 'local-mac-address', with zero MAC
 * addresses.  Some older U-Boots only initialized 'local-mac-address'.  In
 * this case, the real MAC is in 'local-mac-address', and 'mac-address' exists
 * but is all zeros.
*/
const void *of_get_mac_address(struct device_node *np)
{
	struct property *pp;

	pp = of_find_property(np, "mac-address", NULL);
	if (pp && (pp->length == 6) && is_valid_ether_addr(pp->value))
		return pp->value;

	pp = of_find_property(np, "local-mac-address", NULL);
	if (pp && (pp->length == 6) && is_valid_ether_addr(pp->value))
		return pp->value;

	pp = of_find_property(np, "address", NULL);
	if (pp && (pp->length == 6) && is_valid_ether_addr(pp->value))
		return pp->value;

	return NULL;
}
EXPORT_SYMBOL(of_get_mac_address);

1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074
int of_irq_to_resource(struct device_node *dev, int index, struct resource *r)
{
	int irq = irq_of_parse_and_map(dev, index);

	/* Only dereference the resource if both the
	 * resource and the irq are valid. */
	if (r && irq != NO_IRQ) {
		r->start = r->end = irq;
		r->flags = IORESOURCE_IRQ;
	}

	return irq;
}
EXPORT_SYMBOL_GPL(of_irq_to_resource);
1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085

void __iomem *of_iomap(struct device_node *np, int index)
{
	struct resource res;

	if (of_address_to_resource(np, index, &res))
		return NULL;

	return ioremap(res.start, 1 + res.end - res.start);
}
EXPORT_SYMBOL(of_iomap);