of_device.c 18.3 KB
Newer Older
1 2
#include <linux/string.h>
#include <linux/kernel.h>
3
#include <linux/of.h>
4 5 6 7
#include <linux/init.h>
#include <linux/module.h>
#include <linux/mod_devicetable.h>
#include <linux/slab.h>
S
Stephen Rothwell 已提交
8
#include <linux/errno.h>
9
#include <linux/irq.h>
S
Stephen Rothwell 已提交
10 11
#include <linux/of_device.h>
#include <linux/of_platform.h>
12

13 14 15
void __iomem *of_ioremap(struct resource *res, unsigned long offset, unsigned long size, char *name)
{
	unsigned long ret = res->start + offset;
D
David S. Miller 已提交
16
	struct resource *r;
17

D
David S. Miller 已提交
18 19 20 21 22
	if (res->flags & IORESOURCE_MEM)
		r = request_mem_region(ret, size, name);
	else
		r = request_region(ret, size, name);
	if (!r)
23 24 25 26 27 28
		ret = 0;

	return (void __iomem *) ret;
}
EXPORT_SYMBOL(of_ioremap);

29
void of_iounmap(struct resource *res, void __iomem *base, unsigned long size)
30
{
31 32 33 34
	if (res->flags & IORESOURCE_MEM)
		release_mem_region((unsigned long) base, size);
	else
		release_region((unsigned long) base, size);
35 36 37
}
EXPORT_SYMBOL(of_iounmap);

38 39 40 41 42 43 44 45 46 47
static int node_match(struct device *dev, void *data)
{
	struct of_device *op = to_of_device(dev);
	struct device_node *dp = data;

	return (op->node == dp);
}

struct of_device *of_find_device_by_node(struct device_node *dp)
{
48
	struct device *dev = bus_find_device(&of_platform_bus_type, NULL,
49 50 51 52 53 54 55 56 57
					     dp, node_match);

	if (dev)
		return to_of_device(dev);

	return NULL;
}
EXPORT_SYMBOL(of_find_device_by_node);

58
#ifdef CONFIG_PCI
S
Stephen Rothwell 已提交
59
struct bus_type ebus_bus_type;
60
EXPORT_SYMBOL(ebus_bus_type);
61 62 63
#endif

#ifdef CONFIG_SBUS
S
Stephen Rothwell 已提交
64
struct bus_type sbus_bus_type;
65
EXPORT_SYMBOL(sbus_bus_type);
66 67
#endif

S
Stephen Rothwell 已提交
68
struct bus_type of_platform_bus_type;
69
EXPORT_SYMBOL(of_platform_bus_type);
70

71
static inline u64 of_read_addr(const u32 *cell, int size)
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
{
	u64 r = 0;
	while (size--)
		r = (r << 32) | *(cell++);
	return r;
}

static void __init get_cells(struct device_node *dp,
			     int *addrc, int *sizec)
{
	if (addrc)
		*addrc = of_n_addr_cells(dp);
	if (sizec)
		*sizec = of_n_size_cells(dp);
}

/* Max address size we deal with */
#define OF_MAX_ADDR_CELLS	4

struct of_bus {
	const char	*name;
	const char	*addr_prop_name;
	int		(*match)(struct device_node *parent);
	void		(*count_cells)(struct device_node *child,
				       int *addrc, int *sizec);
97 98
	int		(*map)(u32 *addr, const u32 *range,
			       int na, int ns, int pna);
99
	unsigned int	(*get_flags)(const u32 *addr);
100 101 102 103 104 105 106 107 108 109 110 111
};

/*
 * Default translator (generic bus)
 */

static void of_bus_default_count_cells(struct device_node *dev,
				       int *addrc, int *sizec)
{
	get_cells(dev, addrc, sizec);
}

112 113 114 115 116
/* Make sure the least significant 64-bits are in-range.  Even
 * for 3 or 4 cell values it is a good enough approximation.
 */
static int of_out_of_range(const u32 *addr, const u32 *base,
			   const u32 *size, int na, int ns)
117
{
118 119
	u64 a = of_read_addr(addr, na);
	u64 b = of_read_addr(base, na);
120

121 122
	if (a < b)
		return 1;
123

124 125 126 127 128
	b += of_read_addr(size, ns);
	if (a >= b)
		return 1;

	return 0;
129 130
}

131 132
static int of_bus_default_map(u32 *addr, const u32 *range,
			      int na, int ns, int pna)
133
{
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
	u32 result[OF_MAX_ADDR_CELLS];
	int i;

	if (ns > 2) {
		printk("of_device: Cannot handle size cells (%d) > 2.", ns);
		return -EINVAL;
	}

	if (of_out_of_range(addr, range, range + na + pna, na, ns))
		return -EINVAL;

	/* Start with the parent range base.  */
	memcpy(result, range + na, pna * 4);

	/* Add in the child address offset.  */
	for (i = 0; i < na; i++)
		result[pna - 1 - i] +=
			(addr[na - 1 - i] -
			 range[na - 1 - i]);

	memcpy(addr, result, pna * 4);
155 156 157 158

	return 0;
}

159
static unsigned int of_bus_default_get_flags(const u32 *addr)
160 161 162 163 164 165 166 167 168 169
{
	return IORESOURCE_MEM;
}

/*
 * PCI bus specific translator
 */

static int of_bus_pci_match(struct device_node *np)
{
170
	if (!strcmp(np->type, "pci") || !strcmp(np->type, "pciex")) {
171
		const char *model = of_get_property(np, "model", NULL);
172 173 174 175

		if (model && !strcmp(model, "SUNW,simba"))
			return 0;

176 177 178 179 180 181 182 183 184 185 186 187 188
		/* Do not do PCI specific frobbing if the
		 * PCI bridge lacks a ranges property.  We
		 * want to pass it through up to the next
		 * parent as-is, not with the PCI translate
		 * method which chops off the top address cell.
		 */
		if (!of_find_property(np, "ranges", NULL))
			return 0;

		return 1;
	}

	return 0;
189 190
}

191 192
static int of_bus_simba_match(struct device_node *np)
{
193
	const char *model = of_get_property(np, "model", NULL);
194 195 196

	if (model && !strcmp(model, "SUNW,simba"))
		return 1;
197 198 199 200 201 202 203 204 205

	/* Treat PCI busses lacking ranges property just like
	 * simba.
	 */
	if (!strcmp(np->type, "pci") || !strcmp(np->type, "pciex")) {
		if (!of_find_property(np, "ranges", NULL))
			return 1;
	}

206 207 208 209 210 211 212 213 214
	return 0;
}

static int of_bus_simba_map(u32 *addr, const u32 *range,
			    int na, int ns, int pna)
{
	return 0;
}

215 216 217 218 219 220 221 222 223
static void of_bus_pci_count_cells(struct device_node *np,
				   int *addrc, int *sizec)
{
	if (addrc)
		*addrc = 3;
	if (sizec)
		*sizec = 2;
}

224 225
static int of_bus_pci_map(u32 *addr, const u32 *range,
			  int na, int ns, int pna)
226
{
227 228
	u32 result[OF_MAX_ADDR_CELLS];
	int i;
229 230 231

	/* Check address type match */
	if ((addr[0] ^ range[0]) & 0x03000000)
232
		return -EINVAL;
233

234 235 236
	if (of_out_of_range(addr + 1, range + 1, range + na + pna,
			    na - 1, ns))
		return -EINVAL;
237

238 239
	/* Start with the parent range base.  */
	memcpy(result, range + na, pna * 4);
240

241 242 243 244 245 246 247 248 249
	/* Add in the child address offset, skipping high cell.  */
	for (i = 0; i < na - 1; i++)
		result[pna - 1 - i] +=
			(addr[na - 1 - i] -
			 range[na - 1 - i]);

	memcpy(addr, result, pna * 4);

	return 0;
250 251
}

252
static unsigned int of_bus_pci_get_flags(const u32 *addr)
253 254 255 256 257 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 283 284 285 286 287
{
	unsigned int flags = 0;
	u32 w = addr[0];

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

/*
 * SBUS bus specific translator
 */

static int of_bus_sbus_match(struct device_node *np)
{
	return !strcmp(np->name, "sbus") ||
		!strcmp(np->name, "sbi");
}

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

288 289 290 291 292 293 294 295 296 297
/*
 * FHC/Central bus specific translator.
 *
 * This is just needed to hard-code the address and size cell
 * counts.  'fhc' and 'central' nodes lack the #address-cells and
 * #size-cells properties, and if you walk to the root on such
 * Enterprise boxes all you'll get is a #size-cells of 2 which is
 * not what we want to use.
 */
static int of_bus_fhc_match(struct device_node *np)
298
{
299 300
	return !strcmp(np->name, "fhc") ||
		!strcmp(np->name, "central");
301 302
}

303
#define of_bus_fhc_count_cells of_bus_sbus_count_cells
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318

/*
 * Array of bus specific translators
 */

static struct of_bus of_busses[] = {
	/* PCI */
	{
		.name = "pci",
		.addr_prop_name = "assigned-addresses",
		.match = of_bus_pci_match,
		.count_cells = of_bus_pci_count_cells,
		.map = of_bus_pci_map,
		.get_flags = of_bus_pci_get_flags,
	},
319 320 321 322 323 324 325 326 327
	/* SIMBA */
	{
		.name = "simba",
		.addr_prop_name = "assigned-addresses",
		.match = of_bus_simba_match,
		.count_cells = of_bus_pci_count_cells,
		.map = of_bus_simba_map,
		.get_flags = of_bus_pci_get_flags,
	},
328 329 330 331 332 333
	/* SBUS */
	{
		.name = "sbus",
		.addr_prop_name = "reg",
		.match = of_bus_sbus_match,
		.count_cells = of_bus_sbus_count_cells,
334 335 336 337 338 339 340 341 342 343 344
		.map = of_bus_default_map,
		.get_flags = of_bus_default_get_flags,
	},
	/* FHC */
	{
		.name = "fhc",
		.addr_prop_name = "reg",
		.match = of_bus_fhc_match,
		.count_cells = of_bus_fhc_count_cells,
		.map = of_bus_default_map,
		.get_flags = of_bus_default_get_flags,
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
	},
	/* Default */
	{
		.name = "default",
		.addr_prop_name = "reg",
		.match = NULL,
		.count_cells = of_bus_default_count_cells,
		.map = of_bus_default_map,
		.get_flags = of_bus_default_get_flags,
	},
};

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 __init build_one_resource(struct device_node *parent,
				     struct of_bus *bus,
				     struct of_bus *pbus,
				     u32 *addr,
				     int na, int ns, int pna)
{
374
	const u32 *ranges;
375 376 377 378 379
	unsigned int rlen;
	int rone;

	ranges = of_get_property(parent, "ranges", &rlen);
	if (ranges == NULL || rlen == 0) {
380 381 382 383 384 385 386 387 388 389
		u32 result[OF_MAX_ADDR_CELLS];
		int i;

		memset(result, 0, pna * 4);
		for (i = 0; i < na; i++)
			result[pna - 1 - i] =
				addr[na - 1 - i];

		memcpy(addr, result, pna * 4);
		return 0;
390 391 392 393 394 395
	}

	/* Now walk through the ranges */
	rlen /= 4;
	rone = na + pna + ns;
	for (; rlen >= rone; rlen -= rone, ranges += rone) {
396 397
		if (!bus->map(addr, ranges, na, ns, pna))
			return 0;
398
	}
399

400 401 402 403 404 405 406
	/* When we miss an I/O space match on PCI, just pass it up
	 * to the next PCI bridge and/or controller.
	 */
	if (!strcmp(bus->name, "pci") &&
	    (addr[0] & 0x03000000) == 0x01000000)
		return 0;

407 408 409 410 411 412 413 414
	return 1;
}

static int __init use_1to1_mapping(struct device_node *pp)
{
	/* If we have a ranges property in the parent, use it.  */
	if (of_find_property(pp, "ranges", NULL) != NULL)
		return 0;
415

416 417 418 419 420 421
	/* If the parent is the dma node of an ISA bus, pass
	 * the translation up to the root.
	 */
	if (!strcmp(pp->name, "dma"))
		return 0;

422 423 424 425 426
	/* Similarly for all PCI bridges, if we get this far
	 * it lacks a ranges property, and this will include
	 * cases like Simba.
	 */
	if (!strcmp(pp->type, "pci") || !strcmp(pp->type, "pciex"))
427 428 429
		return 0;

	return 1;
430 431
}

432 433
static int of_resource_verbose;

434 435 436 437 438 439 440
static void __init build_device_resources(struct of_device *op,
					  struct device *parent)
{
	struct of_device *p_op;
	struct of_bus *bus;
	int na, ns;
	int index, num_reg;
441
	const void *preg;
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456

	if (!parent)
		return;

	p_op = to_of_device(parent);
	bus = of_match_bus(p_op->node);
	bus->count_cells(op->node, &na, &ns);

	preg = of_get_property(op->node, bus->addr_prop_name, &num_reg);
	if (!preg || num_reg == 0)
		return;

	/* Convert to num-cells.  */
	num_reg /= 4;

457
	/* Convert to num-entries.  */
458 459
	num_reg /= na + ns;

S
Simon Arlott 已提交
460
	/* Prevent overrunning the op->resources[] array.  */
461 462 463 464 465 466 467
	if (num_reg > PROMREG_MAX) {
		printk(KERN_WARNING "%s: Too many regs (%d), "
		       "limiting to %d.\n",
		       op->node->full_name, num_reg, PROMREG_MAX);
		num_reg = PROMREG_MAX;
	}

468 469 470
	for (index = 0; index < num_reg; index++) {
		struct resource *r = &op->resource[index];
		u32 addr[OF_MAX_ADDR_CELLS];
471
		const u32 *reg = (preg + (index * ((na + ns) * 4)));
472 473
		struct device_node *dp = op->node;
		struct device_node *pp = p_op->node;
474
		struct of_bus *pbus, *dbus;
475 476 477 478 479 480 481 482 483 484
		u64 size, result = OF_BAD_ADDR;
		unsigned long flags;
		int dna, dns;
		int pna, pns;

		size = of_read_addr(reg + na, ns);
		flags = bus->get_flags(reg);

		memcpy(addr, reg, na * 4);

485
		if (use_1to1_mapping(pp)) {
486 487 488 489 490 491
			result = of_read_addr(addr, na);
			goto build_res;
		}

		dna = na;
		dns = ns;
492
		dbus = bus;
493 494 495 496 497 498 499 500 501 502 503 504

		while (1) {
			dp = pp;
			pp = dp->parent;
			if (!pp) {
				result = of_read_addr(addr, dna);
				break;
			}

			pbus = of_match_bus(pp);
			pbus->count_cells(dp, &pna, &pns);

505
			if (build_one_resource(dp, dbus, pbus, addr,
506
					       dna, dns, pna))
507 508 509 510
				break;

			dna = pna;
			dns = pns;
511
			dbus = pbus;
512 513 514 515
		}

	build_res:
		memset(r, 0, sizeof(*r));
516 517 518 519 520 521

		if (of_resource_verbose)
			printk("%s reg[%d] -> %lx\n",
			       op->node->full_name, index,
			       result);

522
		if (result != OF_BAD_ADDR) {
523 524 525
			if (tlb_type == hypervisor)
				result &= 0x0fffffffffffffffUL;

526 527 528 529 530 531 532 533
			r->start = result;
			r->end = result + size - 1;
			r->flags = flags;
		}
		r->name = op->node->name;
	}
}

534 535
static struct device_node * __init
apply_interrupt_map(struct device_node *dp, struct device_node *pp,
536
		    const u32 *imap, int imlen, const u32 *imask,
537 538 539 540 541 542
		    unsigned int *irq_p)
{
	struct device_node *cp;
	unsigned int irq = *irq_p;
	struct of_bus *bus;
	phandle handle;
543
	const u32 *reg;
544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570
	int na, num_reg, i;

	bus = of_match_bus(pp);
	bus->count_cells(dp, &na, NULL);

	reg = of_get_property(dp, "reg", &num_reg);
	if (!reg || !num_reg)
		return NULL;

	imlen /= ((na + 3) * 4);
	handle = 0;
	for (i = 0; i < imlen; i++) {
		int j;

		for (j = 0; j < na; j++) {
			if ((reg[j] & imask[j]) != imap[j])
				goto next;
		}
		if (imap[na] == irq) {
			handle = imap[na + 1];
			irq = imap[na + 2];
			break;
		}

	next:
		imap += (na + 3);
	}
571 572 573 574 575 576 577 578 579 580 581 582 583 584
	if (i == imlen) {
		/* Psycho and Sabre PCI controllers can have 'interrupt-map'
		 * properties that do not include the on-board device
		 * interrupts.  Instead, the device's 'interrupts' property
		 * is already a fully specified INO value.
		 *
		 * Handle this by deciding that, if we didn't get a
		 * match in the parent's 'interrupt-map', and the
		 * parent is an IRQ translater, then use the parent as
		 * our IRQ controller.
		 */
		if (pp->irq_trans)
			return pp;

585
		return NULL;
586
	}
587 588 589 590 591 592 593 594 595 596 597

	*irq_p = irq;
	cp = of_find_node_by_phandle(handle);

	return cp;
}

static unsigned int __init pci_irq_swizzle(struct device_node *dp,
					   struct device_node *pp,
					   unsigned int irq)
{
598
	const struct linux_prom_pci_registers *regs;
599
	unsigned int bus, devfn, slot, ret;
600 601 602 603 604 605 606 607

	if (irq < 1 || irq > 4)
		return irq;

	regs = of_get_property(dp, "reg", NULL);
	if (!regs)
		return irq;

608
	bus = (regs->phys_hi >> 16) & 0xff;
609 610 611
	devfn = (regs->phys_hi >> 8) & 0xff;
	slot = (devfn >> 3) & 0x1f;

612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641
	if (pp->irq_trans) {
		/* Derived from Table 8-3, U2P User's Manual.  This branch
		 * is handling a PCI controller that lacks a proper set of
		 * interrupt-map and interrupt-map-mask properties.  The
		 * Ultra-E450 is one example.
		 *
		 * The bit layout is BSSLL, where:
		 * B: 0 on bus A, 1 on bus B
		 * D: 2-bit slot number, derived from PCI device number as
		 *    (dev - 1) for bus A, or (dev - 2) for bus B
		 * L: 2-bit line number
		 */
		if (bus & 0x80) {
			/* PBM-A */
			bus  = 0x00;
			slot = (slot - 1) << 2;
		} else {
			/* PBM-B */
			bus  = 0x10;
			slot = (slot - 2) << 2;
		}
		irq -= 1;

		ret = (bus | slot | irq);
	} else {
		/* Going through a PCI-PCI bridge that lacks a set of
		 * interrupt-map and interrupt-map-mask properties.
		 */
		ret = ((irq - 1 + (slot & 3)) & 3) + 1;
	}
642 643 644 645

	return ret;
}

646 647
static int of_irq_verbose;

648 649 650 651 652 653 654
static unsigned int __init build_one_device_irq(struct of_device *op,
						struct device *parent,
						unsigned int irq)
{
	struct device_node *dp = op->node;
	struct device_node *pp, *ip;
	unsigned int orig_irq = irq;
655
	int nid;
656 657 658 659 660 661 662

	if (irq == 0xffffffff)
		return irq;

	if (dp->irq_trans) {
		irq = dp->irq_trans->irq_build(dp, irq,
					       dp->irq_trans->data);
663 664 665 666 667

		if (of_irq_verbose)
			printk("%s: direct translate %x --> %x\n",
			       dp->full_name, orig_irq, irq);

668
		goto out;
669 670 671 672 673 674 675 676 677 678 679 680 681
	}

	/* Something more complicated.  Walk up to the root, applying
	 * interrupt-map or bus specific translations, until we hit
	 * an IRQ translator.
	 *
	 * If we hit a bus type or situation we cannot handle, we
	 * stop and assume that the original IRQ number was in a
	 * format which has special meaning to it's immediate parent.
	 */
	pp = dp->parent;
	ip = NULL;
	while (pp) {
682
		const void *imap, *imsk;
683 684 685 686 687 688 689 690 691 692 693
		int imlen;

		imap = of_get_property(pp, "interrupt-map", &imlen);
		imsk = of_get_property(pp, "interrupt-map-mask", NULL);
		if (imap && imsk) {
			struct device_node *iret;
			int this_orig_irq = irq;

			iret = apply_interrupt_map(dp, pp,
						   imap, imlen, imsk,
						   &irq);
694 695 696 697 698 699 700

			if (of_irq_verbose)
				printk("%s: Apply [%s:%x] imap --> [%s:%x]\n",
				       op->node->full_name,
				       pp->full_name, this_orig_irq,
				       (iret ? iret->full_name : "NULL"), irq);

701 702 703 704 705 706 707 708 709 710 711 712 713
			if (!iret)
				break;

			if (iret->irq_trans) {
				ip = iret;
				break;
			}
		} else {
			if (!strcmp(pp->type, "pci") ||
			    !strcmp(pp->type, "pciex")) {
				unsigned int this_orig_irq = irq;

				irq = pci_irq_swizzle(dp, pp, irq);
714 715 716 717 718 719 720
				if (of_irq_verbose)
					printk("%s: PCI swizzle [%s] "
					       "%x --> %x\n",
					       op->node->full_name,
					       pp->full_name, this_orig_irq,
					       irq);

721 722 723 724 725 726 727 728 729 730 731 732 733 734 735
			}

			if (pp->irq_trans) {
				ip = pp;
				break;
			}
		}
		dp = pp;
		pp = pp->parent;
	}
	if (!ip)
		return orig_irq;

	irq = ip->irq_trans->irq_build(op->node, irq,
				       ip->irq_trans->data);
736 737 738
	if (of_irq_verbose)
		printk("%s: Apply IRQ trans [%s] %x --> %x\n",
		       op->node->full_name, ip->full_name, orig_irq, irq);
739

740 741 742 743 744 745 746 747
out:
	nid = of_node_to_nid(dp);
	if (nid != -1) {
		cpumask_t numa_mask = node_to_cpumask(nid);

		irq_set_affinity(irq, numa_mask);
	}

748 749 750
	return irq;
}

751 752 753 754
static struct of_device * __init scan_one_device(struct device_node *dp,
						 struct device *parent)
{
	struct of_device *op = kzalloc(sizeof(*op), GFP_KERNEL);
755
	const unsigned int *irq;
756
	struct dev_archdata *sd;
757
	int len, i;
758 759 760 761

	if (!op)
		return NULL;

762 763 764 765
	sd = &op->dev.archdata;
	sd->prom_node = dp;
	sd->op = op;

766 767 768 769 770 771 772 773 774
	op->node = dp;

	op->clock_freq = of_getintprop_default(dp, "clock-frequency",
					       (25*1000*1000));
	op->portid = of_getintprop_default(dp, "upa-portid", -1);
	if (op->portid == -1)
		op->portid = of_getintprop_default(dp, "portid", -1);

	irq = of_get_property(dp, "interrupts", &len);
775 776 777 778 779 780
	if (irq) {
		memcpy(op->irqs, irq, len);
		op->num_irqs = len / 4;
	} else {
		op->num_irqs = 0;
	}
781

S
Simon Arlott 已提交
782
	/* Prevent overrunning the op->irqs[] array.  */
783 784 785 786 787 788 789
	if (op->num_irqs > PROMINTR_MAX) {
		printk(KERN_WARNING "%s: Too many irqs (%d), "
		       "limiting to %d.\n",
		       dp->full_name, op->num_irqs, PROMINTR_MAX);
		op->num_irqs = PROMINTR_MAX;
	}

790
	build_device_resources(op, parent);
791 792
	for (i = 0; i < op->num_irqs; i++)
		op->irqs[i] = build_one_device_irq(op, parent, op->irqs[i]);
793 794

	op->dev.parent = parent;
795
	op->dev.bus = &of_platform_bus_type;
796
	if (!parent)
797
		dev_set_name(&op->dev, "root");
798
	else
799
		dev_set_name(&op->dev, "%08x", dp->node);
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 826 827 828 829 830 831 832 833 834

	if (of_device_register(op)) {
		printk("%s: Could not register of device.\n",
		       dp->full_name);
		kfree(op);
		op = NULL;
	}

	return op;
}

static void __init scan_tree(struct device_node *dp, struct device *parent)
{
	while (dp) {
		struct of_device *op = scan_one_device(dp, parent);

		if (op)
			scan_tree(dp->child, &op->dev);

		dp = dp->sibling;
	}
}

static void __init scan_of_devices(void)
{
	struct device_node *root = of_find_node_by_path("/");
	struct of_device *parent;

	parent = scan_one_device(root, NULL);
	if (!parent)
		return;

	scan_tree(root->child, &parent->dev);
}

835 836
static int __init of_bus_driver_init(void)
{
837
	int err;
838

S
Stephen Rothwell 已提交
839
	err = of_bus_type_init(&of_platform_bus_type, "of");
840 841
#ifdef CONFIG_PCI
	if (!err)
S
Stephen Rothwell 已提交
842
		err = of_bus_type_init(&ebus_bus_type, "ebus");
843 844 845
#endif
#ifdef CONFIG_SBUS
	if (!err)
S
Stephen Rothwell 已提交
846
		err = of_bus_type_init(&sbus_bus_type, "sbus");
847
#endif
848 849 850 851 852

	if (!err)
		scan_of_devices();

	return err;
853 854 855 856
}

postcore_initcall(of_bus_driver_init);

857 858 859 860 861 862 863 864 865 866 867 868 869
static int __init of_debug(char *str)
{
	int val = 0;

	get_option(&str, &val);
	if (val & 1)
		of_resource_verbose = 1;
	if (val & 2)
		of_irq_verbose = 1;
	return 1;
}

__setup("of_debug=", of_debug);