[U-Boot-Users] RFC: flat dev tree fixup code

Kumar Gala galak at kernel.crashing.org
Wed Oct 3 23:55:24 CEST 2007


I'm posting this for review on a way to do fixup's that don't require
OF_CPU or OF_SOC to be defined.

The idea is we match/find a node based on:
* just device_type
* device_type and compatiable
* device_type, compatiable, and unit name

- k

/*
 * Copyright 2007 Freescale Semiconductor.
 *
 * See file CREDITS for list of people who contributed to this
 * project.
 *
 * 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.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 * MA 02111-1307 USA
 */


#include <common.h>
#include <libfdt.h>

#if defined(CONFIG_OF_LIBFDT)

/*
 * "Setter" functions used to add/modify FDT entries.
 */
static int fdt_set_eth0(void *blob, int nodeoffset, const char *name, bd_t *bd)
{
	/* Fix it up if it exists, don't create it if it doesn't exist */
	if (fdt_get_property(blob, nodeoffset, name, 0)) {
		return fdt_setprop(blob, nodeoffset, name, bd->bi_enetaddr, 6);
	}
	return 0;
}
#ifdef CONFIG_HAS_ETH1
/* second onboard ethernet port */
static int fdt_set_eth1(void *blob, int nodeoffset, const char *name, bd_t *bd)
{
	/* Fix it up if it exists, don't create it if it doesn't exist */
	if (fdt_get_property(blob, nodeoffset, name, 0)) {
		return fdt_setprop(blob, nodeoffset, name, bd->bi_enet1addr, 6);
	}
	return 0;
}
#endif
#ifdef CONFIG_HAS_ETH2
/* third onboard ethernet port */
static int fdt_set_eth2(void *blob, int nodeoffset, const char *name, bd_t *bd)
{
	/* Fix it up if it exists, don't create it if it doesn't exist */
	if (fdt_get_property(blob, nodeoffset, name, 0)) {
		return fdt_setprop(blob, nodeoffset, name, bd->bi_enet2addr, 6);
	}
	return 0;
}
#endif
#ifdef CONFIG_HAS_ETH3
/* fourth onboard ethernet port */
static int fdt_set_eth3(void *blob, int nodeoffset, const char *name, bd_t *bd)
{
	/* Fix it up if it exists, don't create it if it doesn't exist */
	if (fdt_get_property(blob, nodeoffset, name, 0)) {
		return fdt_setprop(blob, nodeoffset, name, bd->bi_enet3addr, 6);
	}
	return 0;
}
#endif

static int fdt_set_busfreq(void *blob, int nodeoffset, const char *name, bd_t *bd)
{
	u32 tmp;
	/* Create or update the property */
	tmp = cpu_to_be32(bd->bi_busfreq);
	return fdt_setprop(blob, nodeoffset, name, &tmp, sizeof(tmp));
}

static int fdt_set_tbfreq(void *blob, int nodeoffset, const char *name, bd_t *bd)
{
	u32 tmp;
	/* Create or update the property */
	tmp = cpu_to_be32(bd->bi_busfreq / 8);
	return fdt_setprop(blob, nodeoffset, name, &tmp, sizeof(tmp));
}

/*
 * Fixups to the fdt.
 */

typedef int (*set_fn_t)(void *blob, int nodeoffset, const char *name, bd_t *bd);

struct fdt_fixup {
	const char *type;
	const char *compat;
	const char *prop;
	const char *name;
	set_fn_t set_fn;
};

static struct fdt_fixup fdt_fixes[] = {
	{	.type = "cpu",
		.prop = "timebase-frequency",
		.set_fn = fdt_set_tbfreq
	},
	{	.type = "cpu",
		.prop = "bus-frequency",
		.set_fn = fdt_set_busfreq
	},
	{	.type = "cpu",
		.prop = "clock-frequency",
		.set_fn = fdt_set_busfreq
	},
	{	.type = "soc",
		.prop = "bus-frequency",
		.set_fn = fdt_set_busfreq
	},
	{	.type = "serial",
		.compat = "ns16550",
		.prop = "clock-frequency",
		.set_fn = fdt_set_busfreq
	},
#ifdef CONFIG_TSEC1
	{	.type = "network",
		.compat = "gianfar",
		.name = "ethernet at 24000",
		.prop = "mac-address",
		.set_fn = fdt_set_eth0,
	},
	{	.type = "network",
		.compat = "gianfar",
		.name = "ethernet at 24000",
		.prop = "local-mac-address",
		.set_fn = fdt_set_eth0,
	},
#endif
#ifdef CONFIG_TSEC2
	{	.type = "network",
		.compat = "gianfar",
		.name = "ethernet at 25000",
		.prop = "mac-address",
		.set_fn = fdt_set_eth1,
	},
	{	.type = "network",
		.compat = "gianfar",
		.name = "ethernet at 25000",
		.prop = "local-mac-address",
		.set_fn = fdt_set_eth1,
	},
#endif
#ifdef CONFIG_TSEC3
	{	.type = "network",
		.compat = "gianfar",
		.name = "ethernet at 26000",
		.prop = "mac-address",
		.set_fn = fdt_set_eth2,
	},
	{	.type = "network",
		.compat = "gianfar",
		.name = "ethernet at 26000",
		.prop = "local-mac-address",
		.set_fn = fdt_set_eth2,
	},
#endif
#ifdef CONFIG_TSEC4
	{	.type = "network",
		.compat = "gianfar",
		.name = "ethernet at 27000",
		.prop = "mac-address",
		.set_fn = fdt_set_eth3,
	},
	{	.type = "network",
		.compat = "gianfar",
		.name = "ethernet at 27000",
		.prop = "local-mac-address",
		.set_fn = fdt_set_eth3,
	},
#endif
};

void fdt_fixups(struct fdt_fixup *tbl, int num, void *blob, bd_t *bd)
{
	int j, err, off;

	for (j = 0; j < num; j++) {
		off = 0;
		do {
			if (tbl[j].compat)
				off = fdt_find_compatible_node(blob, off,
					tbl[j].type, tbl[j].compat);
			else
				off = fdt_find_node_by_type(blob, off, tbl[j].type);

			if (off >= 0) {
				if (tbl[j].name) {
					int len;
					const char *name;

					name = fdt_get_name(blob, off, &len);
					if (strncmp(name, tbl[j].name, len) != 0)
						continue;
				}

				err = tbl[j].set_fn(blob, off, tbl[j].prop, bd);
				if (err < 0)
					debug("Problem setting %s = %s: %s\n",
					      tbl[j].node, tbl[j].prop,
					      fdt_strerror(err));
			}
		} while (off >= 0);
	}
}

void ft_cpu_setup(void *blob, bd_t *bd)
{
	int nodeoffset;
	int tmp[2];

	fdt_fixups(&fdt_fixes, sizeof(fdt_fixes) / sizeof(fdt_fixes[0]), blob, bd);

	/* update, or add and update /memory node */
	nodeoffset = fdt_find_node_by_path(blob, "/memory");
	if (nodeoffset < 0) {
		nodeoffset = fdt_add_subnode(blob, 0, "memory");
		if (nodeoffset < 0)
			debug("failed to add /memory node: %s\n",
			      fdt_strerror(nodeoffset));
	}
	if (nodeoffset >= 0) {
		fdt_setprop(blob, nodeoffset, "device_type",
			    "memory", sizeof("memory"));
		tmp[0] = cpu_to_be32(bd->bi_memstart);
		tmp[1] = cpu_to_be32(bd->bi_memsize);
		fdt_setprop(blob, nodeoffset, "reg", tmp, sizeof(tmp));
	}
}
#endif /* CONFIG_OF_LIBFDT */

#if defined(CONFIG_OF_BOARD_SETUP)
extern void pci_of_setup(void *blob, bd_t *bd);

void ft_board_setup(void *blob, bd_t *bd)
{
#ifdef CONFIG_PCI
	pci_of_setup(blob, bd);
#endif
        ft_cpu_setup(blob, bd);
}
#endif




More information about the U-Boot mailing list