of_device.c

来自「linux 内核源代码」· C语言 代码 · 共 897 行 · 第 1/2 页

C
897
字号
#include <linux/string.h>#include <linux/kernel.h>#include <linux/of.h>#include <linux/init.h>#include <linux/module.h>#include <linux/mod_devicetable.h>#include <linux/slab.h>#include <linux/errno.h>#include <linux/of_device.h>#include <linux/of_platform.h>void __iomem *of_ioremap(struct resource *res, unsigned long offset, unsigned long size, char *name){	unsigned long ret = res->start + offset;	struct resource *r;	if (res->flags & IORESOURCE_MEM)		r = request_mem_region(ret, size, name);	else		r = request_region(ret, size, name);	if (!r)		ret = 0;	return (void __iomem *) ret;}EXPORT_SYMBOL(of_ioremap);void of_iounmap(struct resource *res, void __iomem *base, unsigned long size){	if (res->flags & IORESOURCE_MEM)		release_mem_region((unsigned long) base, size);	else		release_region((unsigned long) base, size);}EXPORT_SYMBOL(of_iounmap);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){	struct device *dev = bus_find_device(&of_platform_bus_type, NULL,					     dp, node_match);	if (dev)		return to_of_device(dev);	return NULL;}EXPORT_SYMBOL(of_find_device_by_node);#ifdef CONFIG_PCIstruct bus_type isa_bus_type;EXPORT_SYMBOL(isa_bus_type);struct bus_type ebus_bus_type;EXPORT_SYMBOL(ebus_bus_type);#endif#ifdef CONFIG_SBUSstruct bus_type sbus_bus_type;EXPORT_SYMBOL(sbus_bus_type);#endifstruct bus_type of_platform_bus_type;EXPORT_SYMBOL(of_platform_bus_type);static inline u64 of_read_addr(const u32 *cell, int size){	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	4struct 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);	int		(*map)(u32 *addr, const u32 *range,			       int na, int ns, int pna);	unsigned int	(*get_flags)(const u32 *addr);};/* * Default translator (generic bus) */static void of_bus_default_count_cells(struct device_node *dev,				       int *addrc, int *sizec){	get_cells(dev, addrc, sizec);}/* 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){	u64 a = of_read_addr(addr, na);	u64 b = of_read_addr(base, na);	if (a < b)		return 1;	b += of_read_addr(size, ns);	if (a >= b)		return 1;	return 0;}static int of_bus_default_map(u32 *addr, const u32 *range,			      int na, int ns, int pna){	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);	return 0;}static unsigned int of_bus_default_get_flags(const u32 *addr){	return IORESOURCE_MEM;}/* * PCI bus specific translator */static int of_bus_pci_match(struct device_node *np){	if (!strcmp(np->type, "pci") || !strcmp(np->type, "pciex")) {		const char *model = of_get_property(np, "model", NULL);		if (model && !strcmp(model, "SUNW,simba"))			return 0;		/* 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;}static int of_bus_simba_match(struct device_node *np){	const char *model = of_get_property(np, "model", NULL);	if (model && !strcmp(model, "SUNW,simba"))		return 1;	/* 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;	}	return 0;}static int of_bus_simba_map(u32 *addr, const u32 *range,			    int na, int ns, int pna){	return 0;}static void of_bus_pci_count_cells(struct device_node *np,				   int *addrc, int *sizec){	if (addrc)		*addrc = 3;	if (sizec)		*sizec = 2;}static int of_bus_pci_map(u32 *addr, const u32 *range,			  int na, int ns, int pna){	u32 result[OF_MAX_ADDR_CELLS];	int i;	/* Check address type match */	if ((addr[0] ^ range[0]) & 0x03000000)		return -EINVAL;	if (of_out_of_range(addr + 1, range + 1, range + na + pna,			    na - 1, ns))		return -EINVAL;	/* Start with the parent range base.  */	memcpy(result, range + na, pna * 4);	/* 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;}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;	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;}/* * 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){	return !strcmp(np->name, "fhc") ||		!strcmp(np->name, "central");}#define of_bus_fhc_count_cells of_bus_sbus_count_cells/* * 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,	},	/* 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,	},	/* SBUS */	{		.name = "sbus",		.addr_prop_name = "reg",		.match = of_bus_sbus_match,		.count_cells = of_bus_sbus_count_cells,		.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,	},	/* 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){	const u32 *ranges;	unsigned int rlen;	int rone;	ranges = of_get_property(parent, "ranges", &rlen);	if (ranges == NULL || rlen == 0) {		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;	}	/* Now walk through the ranges */	rlen /= 4;	rone = na + pna + ns;	for (; rlen >= rone; rlen -= rone, ranges += rone) {		if (!bus->map(addr, ranges, na, ns, pna))			return 0;	}	/* 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;	return 1;}static int __init use_1to1_mapping(struct device_node *pp){	/* If this is on the PMU bus, don't try to translate it even	 * if a ranges property exists.	 */	if (!strcmp(pp->name, "pmu"))		return 1;	/* If we have a ranges property in the parent, use it.  */	if (of_find_property(pp, "ranges", NULL) != NULL)		return 0;	/* 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;	/* 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"))		return 0;	return 1;}static int of_resource_verbose;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;	const void *preg;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?