of_device.c 12.7 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 9 10
#include <linux/errno.h>
#include <linux/of_device.h>
#include <linux/of_platform.h>
11

12 13 14 15 16 17 18 19 20 21
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)
{
22
	struct device *dev = bus_find_device(&of_platform_bus_type, NULL,
23 24 25 26 27 28 29 30 31
					     dp, node_match);

	if (dev)
		return to_of_device(dev);

	return NULL;
}
EXPORT_SYMBOL(of_find_device_by_node);

32
#ifdef CONFIG_PCI
S
Stephen Rothwell 已提交
33
struct bus_type ebus_bus_type;
34
EXPORT_SYMBOL(ebus_bus_type);
35 36 37
#endif

#ifdef CONFIG_SBUS
S
Stephen Rothwell 已提交
38
struct bus_type sbus_bus_type;
39
EXPORT_SYMBOL(sbus_bus_type);
40 41
#endif

S
Stephen Rothwell 已提交
42
struct bus_type of_platform_bus_type;
43
EXPORT_SYMBOL(of_platform_bus_type);
44

45
static inline u64 of_read_addr(const u32 *cell, int size)
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
{
	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);
71 72
	int		(*map)(u32 *addr, const u32 *range,
			       int na, int ns, int pna);
73
	unsigned int	(*get_flags)(const u32 *addr);
74 75 76 77 78 79 80 81 82 83 84 85
};

/*
 * Default translator (generic bus)
 */

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

86 87 88 89 90
/* 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)
91
{
92 93 94 95 96
	u64 a = of_read_addr(addr, na);
	u64 b = of_read_addr(base, na);

	if (a < b)
		return 1;
97

98 99 100
	b += of_read_addr(size, ns);
	if (a >= b)
		return 1;
101

102
	return 0;
103 104
}

105 106
static int of_bus_default_map(u32 *addr, const u32 *range,
			      int na, int ns, int pna)
107
{
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
	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);
129 130 131 132

	return 0;
}

133
static unsigned int of_bus_default_get_flags(const u32 *addr)
134 135 136 137 138 139 140 141 142 143
{
	return IORESOURCE_MEM;
}

/*
 * PCI bus specific translator
 */

static int of_bus_pci_match(struct device_node *np)
{
144 145 146 147 148 149 150 151 152 153 154 155 156 157
	if (!strcmp(np->type, "pci") || !strcmp(np->type, "pciex")) {
		/* 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;
158 159 160 161 162 163 164 165 166 167 168
}

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

169 170
static int of_bus_pci_map(u32 *addr, const u32 *range,
			  int na, int ns, int pna)
171
{
172 173
	u32 result[OF_MAX_ADDR_CELLS];
	int i;
174 175 176

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

179 180 181
	if (of_out_of_range(addr + 1, range + 1, range + na + pna,
			    na - 1, ns))
		return -EINVAL;
182

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

186 187 188 189 190 191 192 193 194
	/* 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;
195 196
}

197
static unsigned int of_bus_pci_get_flags(const u32 *addr)
198 199 200 201 202 203 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
{
	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;
}

233
static int of_bus_sbus_map(u32 *addr, const u32 *range, int na, int ns, int pna)
234 235 236 237
{
	return of_bus_default_map(addr, range, na, ns, pna);
}

238
static unsigned int of_bus_sbus_get_flags(const u32 *addr)
239 240 241 242 243 244 245 246 247 248 249 250 251 252 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 288 289 290 291 292 293 294
{
	return IORESOURCE_MEM;
}


/*
 * 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,
	},
	/* SBUS */
	{
		.name = "sbus",
		.addr_prop_name = "reg",
		.match = of_bus_sbus_match,
		.count_cells = of_bus_sbus_count_cells,
		.map = of_bus_sbus_map,
		.get_flags = of_bus_sbus_get_flags,
	},
	/* 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)
{
295
	const u32 *ranges;
296 297 298 299 300
	unsigned int rlen;
	int rone;

	ranges = of_get_property(parent, "ranges", &rlen);
	if (ranges == NULL || rlen == 0) {
301 302 303 304 305 306 307 308 309 310
		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;
311 312 313 314 315 316
	}

	/* Now walk through the ranges */
	rlen /= 4;
	rone = na + pna + ns;
	for (; rlen >= rone; rlen -= rone, ranges += rone) {
317 318
		if (!bus->map(addr, ranges, na, ns, pna))
			return 0;
319 320
	}

321
	return 1;
322 323
}

324 325
static int of_resource_verbose;

326 327 328 329 330 331 332
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;
333
	const void *preg;
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354

	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;

	/* Conver to num-entries.  */
	num_reg /= na + ns;

	for (index = 0; index < num_reg; index++) {
		struct resource *r = &op->resource[index];
		u32 addr[OF_MAX_ADDR_CELLS];
355
		const u32 *reg = (preg + (index * ((na + ns) * 4)));
356 357
		struct device_node *dp = op->node;
		struct device_node *pp = p_op->node;
358
		struct of_bus *pbus, *dbus;
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
		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);

		/* If the immediate parent has no ranges property to apply,
		 * just use a 1<->1 mapping.
		 */
		if (of_find_property(pp, "ranges", NULL) == NULL) {
			result = of_read_addr(addr, na);
			goto build_res;
		}

		dna = na;
		dns = ns;
379
		dbus = bus;
380 381 382 383 384 385 386 387 388 389 390 391

		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);

392
			if (build_one_resource(dp, dbus, pbus, addr,
393
					       dna, dns, pna))
394 395 396 397
				break;

			dna = pna;
			dns = pns;
398
			dbus = pbus;
399 400 401 402
		}

	build_res:
		memset(r, 0, sizeof(*r));
403 404 405 406 407 408

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

409
		if (result != OF_BAD_ADDR) {
410
			r->start = result & 0xffffffff;
411
			r->end = result + size - 1;
412
			r->flags = flags | ((result >> 32ULL) & 0xffUL);
413 414 415 416 417 418 419 420 421
		}
		r->name = op->node->name;
	}
}

static struct of_device * __init scan_one_device(struct device_node *dp,
						 struct device *parent)
{
	struct of_device *op = kzalloc(sizeof(*op), GFP_KERNEL);
422
	const struct linux_prom_irqs *intr;
423
	int len, i;
424 425 426 427 428 429 430 431 432 433 434 435

	if (!op)
		return NULL;

	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);

436 437 438 439 440 441
	intr = of_get_property(dp, "intr", &len);
	if (intr) {
		op->num_irqs = len / sizeof(struct linux_prom_irqs);
		for (i = 0; i < op->num_irqs; i++)
			op->irqs[i] = intr[i].pri;
	} else {
442 443
		const unsigned int *irq =
			of_get_property(dp, "interrupts", &len);
444 445 446 447 448 449 450 451 452 453 454 455 456

		if (irq) {
			op->num_irqs = len / sizeof(unsigned int);
			for (i = 0; i < op->num_irqs; i++)
				op->irqs[i] = irq[i];
		} else {
			op->num_irqs = 0;
		}
	}
	if (sparc_cpu_model == sun4d) {
		static int pil_to_sbus[] = {
			0, 0, 1, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 0,
		};
457
		struct device_node *io_unit, *sbi = dp->parent;
458
		const struct linux_prom_registers *regs;
459 460 461 462 463 464 465 466 467 468
		int board, slot;

		while (sbi) {
			if (!strcmp(sbi->name, "sbi"))
				break;

			sbi = sbi->parent;
		}
		if (!sbi)
			goto build_resources;
469 470

		regs = of_get_property(dp, "reg", NULL);
471 472 473
		if (!regs)
			goto build_resources;

474 475
		slot = regs->which_io;

476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
		/* If SBI's parent is not io-unit or the io-unit lacks
		 * a "board#" property, something is very wrong.
		 */
		if (!sbi->parent || strcmp(sbi->parent->name, "io-unit")) {
			printk("%s: Error, parent is not io-unit.\n",
			       sbi->full_name);
			goto build_resources;
		}
		io_unit = sbi->parent;
		board = of_getintprop_default(io_unit, "board#", -1);
		if (board == -1) {
			printk("%s: Error, lacks board# property.\n",
			       io_unit->full_name);
			goto build_resources;
		}

492 493 494 495 496 497 498 499 500 501 502 503
		for (i = 0; i < op->num_irqs; i++) {
			int this_irq = op->irqs[i];
			int sbusl = pil_to_sbus[this_irq];

			if (sbusl)
				this_irq = (((board + 1) << 5) +
					    (sbusl << 2) +
					    slot);

			op->irqs[i] = this_irq;
		}
	}
504

505
build_resources:
506 507 508
	build_device_resources(op, parent);

	op->dev.parent = parent;
509
	op->dev.bus = &of_platform_bus_type;
510 511 512
	if (!parent)
		strcpy(op->dev.bus_id, "root");
	else
513
		sprintf(op->dev.bus_id, "%08x", dp->node);
514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548

	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);
}

549 550
static int __init of_bus_driver_init(void)
{
551
	int err;
552

S
Stephen Rothwell 已提交
553
	err = of_bus_type_init(&of_platform_bus_type, "of");
554 555
#ifdef CONFIG_PCI
	if (!err)
S
Stephen Rothwell 已提交
556
		err = of_bus_type_init(&ebus_bus_type, "ebus");
557 558 559
#endif
#ifdef CONFIG_SBUS
	if (!err)
S
Stephen Rothwell 已提交
560
		err = of_bus_type_init(&sbus_bus_type, "sbus");
561
#endif
562 563 564 565 566

	if (!err)
		scan_of_devices();

	return err;
567 568 569 570
}

postcore_initcall(of_bus_driver_init);

571 572 573 574 575 576 577 578 579 580 581 582
static int __init of_debug(char *str)
{
	int val = 0;

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

__setup("of_debug=", of_debug);

583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604
int of_register_driver(struct of_platform_driver *drv, struct bus_type *bus)
{
	/* initialize common driver fields */
	drv->driver.name = drv->name;
	drv->driver.bus = bus;

	/* register with core */
	return driver_register(&drv->driver);
}

void of_unregister_driver(struct of_platform_driver *drv)
{
	driver_unregister(&drv->driver);
}

struct of_device* of_platform_device_create(struct device_node *np,
					    const char *bus_id,
					    struct device *parent,
					    struct bus_type *bus)
{
	struct of_device *dev;

605
	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625
	if (!dev)
		return NULL;

	dev->dev.parent = parent;
	dev->dev.bus = bus;
	dev->dev.release = of_release_dev;

	strlcpy(dev->dev.bus_id, bus_id, BUS_ID_SIZE);

	if (of_device_register(dev) != 0) {
		kfree(dev);
		return NULL;
	}

	return dev;
}

EXPORT_SYMBOL(of_register_driver);
EXPORT_SYMBOL(of_unregister_driver);
EXPORT_SYMBOL(of_platform_device_create);