Linux Audio

Check our new training course

Loading...
v3.5.6
 
  1/*
  2 * Procedures for creating, accessing and interpreting the device tree.
  3 *
  4 * Paul Mackerras	August 1996.
  5 * Copyright (C) 1996-2005 Paul Mackerras.
  6 * 
  7 *  Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
  8 *    {engebret|bergner}@us.ibm.com 
  9 *
 10 *  Adapted for sparc32 by David S. Miller davem@davemloft.net
 11 *
 12 *      This program is free software; you can redistribute it and/or
 13 *      modify it under the terms of the GNU General Public License
 14 *      as published by the Free Software Foundation; either version
 15 *      2 of the License, or (at your option) any later version.
 16 */
 17
 18#include <linux/kernel.h>
 19#include <linux/types.h>
 20#include <linux/string.h>
 21#include <linux/mm.h>
 22#include <linux/bootmem.h>
 23
 24#include <asm/prom.h>
 25#include <asm/oplib.h>
 26#include <asm/leon.h>
 27#include <asm/leon_amba.h>
 28
 29#include "prom.h"
 30
 31void * __init prom_early_alloc(unsigned long size)
 32{
 33	void *ret;
 34
 35	ret = __alloc_bootmem(size, SMP_CACHE_BYTES, 0UL);
 36	if (ret != NULL)
 37		memset(ret, 0, size);
 38
 39	prom_early_allocated += size;
 40
 41	return ret;
 42}
 43
 44/* The following routines deal with the black magic of fully naming a
 45 * node.
 46 *
 47 * Certain well known named nodes are just the simple name string.
 48 *
 49 * Actual devices have an address specifier appended to the base name
 50 * string, like this "foo@addr".  The "addr" can be in any number of
 51 * formats, and the platform plus the type of the node determine the
 52 * format and how it is constructed.
 53 *
 54 * For children of the ROOT node, the naming convention is fixed and
 55 * determined by whether this is a sun4u or sun4v system.
 56 *
 57 * For children of other nodes, it is bus type specific.  So
 58 * we walk up the tree until we discover a "device_type" property
 59 * we recognize and we go from there.
 60 */
 61static void __init sparc32_path_component(struct device_node *dp, char *tmp_buf)
 62{
 
 63	struct linux_prom_registers *regs;
 64	struct property *rprop;
 65
 66	rprop = of_find_property(dp, "reg", NULL);
 67	if (!rprop)
 68		return;
 69
 70	regs = rprop->value;
 71	sprintf(tmp_buf, "%s@%x,%x",
 72		dp->name,
 73		regs->which_io, regs->phys_addr);
 74}
 75
 76/* "name@slot,offset"  */
 77static void __init sbus_path_component(struct device_node *dp, char *tmp_buf)
 78{
 
 79	struct linux_prom_registers *regs;
 80	struct property *prop;
 81
 82	prop = of_find_property(dp, "reg", NULL);
 83	if (!prop)
 84		return;
 85
 86	regs = prop->value;
 87	sprintf(tmp_buf, "%s@%x,%x",
 88		dp->name,
 89		regs->which_io,
 90		regs->phys_addr);
 91}
 92
 93/* "name@devnum[,func]" */
 94static void __init pci_path_component(struct device_node *dp, char *tmp_buf)
 95{
 
 96	struct linux_prom_pci_registers *regs;
 97	struct property *prop;
 98	unsigned int devfn;
 99
100	prop = of_find_property(dp, "reg", NULL);
101	if (!prop)
102		return;
103
104	regs = prop->value;
105	devfn = (regs->phys_hi >> 8) & 0xff;
106	if (devfn & 0x07) {
107		sprintf(tmp_buf, "%s@%x,%x",
108			dp->name,
109			devfn >> 3,
110			devfn & 0x07);
111	} else {
112		sprintf(tmp_buf, "%s@%x",
113			dp->name,
114			devfn >> 3);
115	}
116}
117
118/* "name@addrhi,addrlo" */
119static void __init ebus_path_component(struct device_node *dp, char *tmp_buf)
120{
 
121	struct linux_prom_registers *regs;
122	struct property *prop;
123
124	prop = of_find_property(dp, "reg", NULL);
125	if (!prop)
126		return;
127
128	regs = prop->value;
129
130	sprintf(tmp_buf, "%s@%x,%x",
131		dp->name,
132		regs->which_io, regs->phys_addr);
133}
134
135/* "name:vendor:device@irq,addrlo" */
136static void __init ambapp_path_component(struct device_node *dp, char *tmp_buf)
137{
 
138	struct amba_prom_registers *regs;
139	unsigned int *intr, *device, *vendor, reg0;
 
140	struct property *prop;
141	int interrupt = 0;
142
143	/* In order to get a unique ID in the device tree (multiple AMBA devices
144	 * may have the same name) the node number is printed
145	 */
146	prop = of_find_property(dp, "reg", NULL);
147	if (!prop) {
148		reg0 = (unsigned int)dp->phandle;
149	} else {
150		regs = prop->value;
151		reg0 = regs->phys_addr;
152	}
153
154	/* Not all cores have Interrupt */
155	prop = of_find_property(dp, "interrupts", NULL);
156	if (!prop)
157		intr = &interrupt; /* IRQ0 does not exist */
158	else
159		intr = prop->value;
160
161	prop = of_find_property(dp, "vendor", NULL);
162	if (!prop)
163		return;
164	vendor = prop->value;
165	prop = of_find_property(dp, "device", NULL);
166	if (!prop)
167		return;
168	device = prop->value;
169
170	sprintf(tmp_buf, "%s:%d:%d@%x,%x",
171		dp->name, *vendor, *device,
172		*intr, reg0);
173}
174
175static void __init __build_path_component(struct device_node *dp, char *tmp_buf)
176{
177	struct device_node *parent = dp->parent;
178
179	if (parent != NULL) {
180		if (!strcmp(parent->type, "pci") ||
181		    !strcmp(parent->type, "pciex"))
182			return pci_path_component(dp, tmp_buf);
183		if (!strcmp(parent->type, "sbus"))
184			return sbus_path_component(dp, tmp_buf);
185		if (!strcmp(parent->type, "ebus"))
186			return ebus_path_component(dp, tmp_buf);
187		if (!strcmp(parent->type, "ambapp"))
188			return ambapp_path_component(dp, tmp_buf);
189
190		/* "isa" is handled with platform naming */
191	}
192
193	/* Use platform naming convention.  */
194	return sparc32_path_component(dp, tmp_buf);
195}
196
197char * __init build_path_component(struct device_node *dp)
198{
 
199	char tmp_buf[64], *n;
200
201	tmp_buf[0] = '\0';
202	__build_path_component(dp, tmp_buf);
203	if (tmp_buf[0] == '\0')
204		strcpy(tmp_buf, dp->name);
205
206	n = prom_early_alloc(strlen(tmp_buf) + 1);
207	strcpy(n, tmp_buf);
208
209	return n;
210}
211
212extern void restore_current(void);
213
214void __init of_console_init(void)
215{
216	char *msg = "OF stdout device is: %s\n";
217	struct device_node *dp;
218	unsigned long flags;
219	const char *type;
220	phandle node;
221	int skip, tmp, fd;
222
223	of_console_path = prom_early_alloc(256);
224
225	switch (prom_vers) {
226	case PROM_V0:
227		skip = 0;
228		switch (*romvec->pv_stdout) {
229		case PROMDEV_SCREEN:
230			type = "display";
231			break;
232
233		case PROMDEV_TTYB:
234			skip = 1;
235			/* FALLTHRU */
236
237		case PROMDEV_TTYA:
238			type = "serial";
239			break;
240
241		default:
242			prom_printf("Invalid PROM_V0 stdout value %u\n",
243				    *romvec->pv_stdout);
244			prom_halt();
245		}
246
247		tmp = skip;
248		for_each_node_by_type(dp, type) {
249			if (!tmp--)
250				break;
251		}
252		if (!dp) {
253			prom_printf("Cannot find PROM_V0 console node.\n");
254			prom_halt();
255		}
256		of_console_device = dp;
257
258		strcpy(of_console_path, dp->full_name);
259		if (!strcmp(type, "serial")) {
260			strcat(of_console_path,
261			       (skip ? ":b" : ":a"));
262		}
263		break;
264
265	default:
266	case PROM_V2:
267	case PROM_V3:
268		fd = *romvec->pv_v2bootargs.fd_stdout;
269
270		spin_lock_irqsave(&prom_lock, flags);
271		node = (*romvec->pv_v2devops.v2_inst2pkg)(fd);
272		restore_current();
273		spin_unlock_irqrestore(&prom_lock, flags);
274
275		if (!node) {
276			prom_printf("Cannot resolve stdout node from "
277				    "instance %08x.\n", fd);
278			prom_halt();
279		}
280		dp = of_find_node_by_phandle(node);
281		type = of_get_property(dp, "device_type", NULL);
282
283		if (!type) {
284			prom_printf("Console stdout lacks "
285				    "device_type property.\n");
286			prom_halt();
287		}
288
289		if (strcmp(type, "display") && strcmp(type, "serial")) {
 
290			prom_printf("Console device_type is neither display "
291				    "nor serial.\n");
292			prom_halt();
293		}
294
295		of_console_device = dp;
296
297		if (prom_vers == PROM_V2) {
298			strcpy(of_console_path, dp->full_name);
299			switch (*romvec->pv_stdout) {
300			case PROMDEV_TTYA:
301				strcat(of_console_path, ":a");
302				break;
303			case PROMDEV_TTYB:
304				strcat(of_console_path, ":b");
305				break;
306			}
307		} else {
308			const char *path;
309
310			dp = of_find_node_by_path("/");
311			path = of_get_property(dp, "stdout-path", NULL);
312			if (!path) {
313				prom_printf("No stdout-path in root node.\n");
314				prom_halt();
315			}
316			strcpy(of_console_path, path);
317		}
318		break;
319	}
320
321	of_console_options = strrchr(of_console_path, ':');
322	if (of_console_options) {
323		of_console_options++;
324		if (*of_console_options == '\0')
325			of_console_options = NULL;
326	}
327
328	printk(msg, of_console_path);
329}
330
331void __init of_fill_in_cpu_data(void)
332{
333}
334
335void __init irq_trans_init(struct device_node *dp)
336{
337}
v6.2
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 * Procedures for creating, accessing and interpreting the device tree.
  4 *
  5 * Paul Mackerras	August 1996.
  6 * Copyright (C) 1996-2005 Paul Mackerras.
  7 * 
  8 *  Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
  9 *    {engebret|bergner}@us.ibm.com 
 10 *
 11 *  Adapted for sparc32 by David S. Miller davem@davemloft.net
 
 
 
 
 
 12 */
 13
 14#include <linux/kernel.h>
 15#include <linux/types.h>
 16#include <linux/string.h>
 17#include <linux/mm.h>
 18#include <linux/memblock.h>
 19
 20#include <asm/prom.h>
 21#include <asm/oplib.h>
 22#include <asm/leon.h>
 23#include <asm/leon_amba.h>
 24
 25#include "prom.h"
 26
 27void * __init prom_early_alloc(unsigned long size)
 28{
 29	void *ret;
 30
 31	ret = memblock_alloc(size, SMP_CACHE_BYTES);
 32	if (!ret)
 33		panic("%s: Failed to allocate %lu bytes\n", __func__, size);
 34
 35	prom_early_allocated += size;
 36
 37	return ret;
 38}
 39
 40/* The following routines deal with the black magic of fully naming a
 41 * node.
 42 *
 43 * Certain well known named nodes are just the simple name string.
 44 *
 45 * Actual devices have an address specifier appended to the base name
 46 * string, like this "foo@addr".  The "addr" can be in any number of
 47 * formats, and the platform plus the type of the node determine the
 48 * format and how it is constructed.
 49 *
 50 * For children of the ROOT node, the naming convention is fixed and
 51 * determined by whether this is a sun4u or sun4v system.
 52 *
 53 * For children of other nodes, it is bus type specific.  So
 54 * we walk up the tree until we discover a "device_type" property
 55 * we recognize and we go from there.
 56 */
 57static void __init sparc32_path_component(struct device_node *dp, char *tmp_buf)
 58{
 59	const char *name = of_get_property(dp, "name", NULL);
 60	struct linux_prom_registers *regs;
 61	struct property *rprop;
 62
 63	rprop = of_find_property(dp, "reg", NULL);
 64	if (!rprop)
 65		return;
 66
 67	regs = rprop->value;
 68	sprintf(tmp_buf, "%s@%x,%x",
 69		name,
 70		regs->which_io, regs->phys_addr);
 71}
 72
 73/* "name@slot,offset"  */
 74static void __init sbus_path_component(struct device_node *dp, char *tmp_buf)
 75{
 76	const char *name = of_get_property(dp, "name", NULL);
 77	struct linux_prom_registers *regs;
 78	struct property *prop;
 79
 80	prop = of_find_property(dp, "reg", NULL);
 81	if (!prop)
 82		return;
 83
 84	regs = prop->value;
 85	sprintf(tmp_buf, "%s@%x,%x",
 86		name,
 87		regs->which_io,
 88		regs->phys_addr);
 89}
 90
 91/* "name@devnum[,func]" */
 92static void __init pci_path_component(struct device_node *dp, char *tmp_buf)
 93{
 94	const char *name = of_get_property(dp, "name", NULL);
 95	struct linux_prom_pci_registers *regs;
 96	struct property *prop;
 97	unsigned int devfn;
 98
 99	prop = of_find_property(dp, "reg", NULL);
100	if (!prop)
101		return;
102
103	regs = prop->value;
104	devfn = (regs->phys_hi >> 8) & 0xff;
105	if (devfn & 0x07) {
106		sprintf(tmp_buf, "%s@%x,%x",
107			name,
108			devfn >> 3,
109			devfn & 0x07);
110	} else {
111		sprintf(tmp_buf, "%s@%x",
112			name,
113			devfn >> 3);
114	}
115}
116
117/* "name@addrhi,addrlo" */
118static void __init ebus_path_component(struct device_node *dp, char *tmp_buf)
119{
120	const char *name = of_get_property(dp, "name", NULL);
121	struct linux_prom_registers *regs;
122	struct property *prop;
123
124	prop = of_find_property(dp, "reg", NULL);
125	if (!prop)
126		return;
127
128	regs = prop->value;
129
130	sprintf(tmp_buf, "%s@%x,%x",
131		name,
132		regs->which_io, regs->phys_addr);
133}
134
135/* "name@irq,addrlo" */
136static void __init ambapp_path_component(struct device_node *dp, char *tmp_buf)
137{
138	const char *name = of_get_property(dp, "name", NULL);
139	struct amba_prom_registers *regs;
140	unsigned int *intr;
141	unsigned int reg0;
142	struct property *prop;
143	int interrupt = 0;
144
145	/* In order to get a unique ID in the device tree (multiple AMBA devices
146	 * may have the same name) the node number is printed
147	 */
148	prop = of_find_property(dp, "reg", NULL);
149	if (!prop) {
150		reg0 = (unsigned int)dp->phandle;
151	} else {
152		regs = prop->value;
153		reg0 = regs->phys_addr;
154	}
155
156	/* Not all cores have Interrupt */
157	prop = of_find_property(dp, "interrupts", NULL);
158	if (!prop)
159		intr = &interrupt; /* IRQ0 does not exist */
160	else
161		intr = prop->value;
162
163	sprintf(tmp_buf, "%s@%x,%x", name, *intr, reg0);
 
 
 
 
 
 
 
 
 
 
 
164}
165
166static void __init __build_path_component(struct device_node *dp, char *tmp_buf)
167{
168	struct device_node *parent = dp->parent;
169
170	if (parent != NULL) {
171		if (of_node_is_type(parent, "pci") ||
172		    of_node_is_type(parent, "pciex"))
173			return pci_path_component(dp, tmp_buf);
174		if (of_node_is_type(parent, "sbus"))
175			return sbus_path_component(dp, tmp_buf);
176		if (of_node_is_type(parent, "ebus"))
177			return ebus_path_component(dp, tmp_buf);
178		if (of_node_is_type(parent, "ambapp"))
179			return ambapp_path_component(dp, tmp_buf);
180
181		/* "isa" is handled with platform naming */
182	}
183
184	/* Use platform naming convention.  */
185	return sparc32_path_component(dp, tmp_buf);
186}
187
188char * __init build_path_component(struct device_node *dp)
189{
190	const char *name = of_get_property(dp, "name", NULL);
191	char tmp_buf[64], *n;
192
193	tmp_buf[0] = '\0';
194	__build_path_component(dp, tmp_buf);
195	if (tmp_buf[0] == '\0')
196		strcpy(tmp_buf, name);
197
198	n = prom_early_alloc(strlen(tmp_buf) + 1);
199	strcpy(n, tmp_buf);
200
201	return n;
202}
203
204extern void restore_current(void);
205
206void __init of_console_init(void)
207{
208	char *msg = "OF stdout device is: %s\n";
209	struct device_node *dp;
210	unsigned long flags;
211	const char *type;
212	phandle node;
213	int skip, tmp, fd;
214
215	of_console_path = prom_early_alloc(256);
216
217	switch (prom_vers) {
218	case PROM_V0:
219		skip = 0;
220		switch (*romvec->pv_stdout) {
221		case PROMDEV_SCREEN:
222			type = "display";
223			break;
224
225		case PROMDEV_TTYB:
226			skip = 1;
227			fallthrough;
228
229		case PROMDEV_TTYA:
230			type = "serial";
231			break;
232
233		default:
234			prom_printf("Invalid PROM_V0 stdout value %u\n",
235				    *romvec->pv_stdout);
236			prom_halt();
237		}
238
239		tmp = skip;
240		for_each_node_by_type(dp, type) {
241			if (!tmp--)
242				break;
243		}
244		if (!dp) {
245			prom_printf("Cannot find PROM_V0 console node.\n");
246			prom_halt();
247		}
248		of_console_device = dp;
249
250		sprintf(of_console_path, "%pOF", dp);
251		if (!strcmp(type, "serial")) {
252			strcat(of_console_path,
253			       (skip ? ":b" : ":a"));
254		}
255		break;
256
257	default:
258	case PROM_V2:
259	case PROM_V3:
260		fd = *romvec->pv_v2bootargs.fd_stdout;
261
262		spin_lock_irqsave(&prom_lock, flags);
263		node = (*romvec->pv_v2devops.v2_inst2pkg)(fd);
264		restore_current();
265		spin_unlock_irqrestore(&prom_lock, flags);
266
267		if (!node) {
268			prom_printf("Cannot resolve stdout node from "
269				    "instance %08x.\n", fd);
270			prom_halt();
271		}
272		dp = of_find_node_by_phandle(node);
 
 
 
 
 
 
 
273
274		if (!of_node_is_type(dp, "display") &&
275		    !of_node_is_type(dp, "serial")) {
276			prom_printf("Console device_type is neither display "
277				    "nor serial.\n");
278			prom_halt();
279		}
280
281		of_console_device = dp;
282
283		if (prom_vers == PROM_V2) {
284			sprintf(of_console_path, "%pOF", dp);
285			switch (*romvec->pv_stdout) {
286			case PROMDEV_TTYA:
287				strcat(of_console_path, ":a");
288				break;
289			case PROMDEV_TTYB:
290				strcat(of_console_path, ":b");
291				break;
292			}
293		} else {
294			const char *path;
295
296			dp = of_find_node_by_path("/");
297			path = of_get_property(dp, "stdout-path", NULL);
298			if (!path) {
299				prom_printf("No stdout-path in root node.\n");
300				prom_halt();
301			}
302			strcpy(of_console_path, path);
303		}
304		break;
305	}
306
307	of_console_options = strrchr(of_console_path, ':');
308	if (of_console_options) {
309		of_console_options++;
310		if (*of_console_options == '\0')
311			of_console_options = NULL;
312	}
313
314	printk(msg, of_console_path);
315}
316
317void __init of_fill_in_cpu_data(void)
318{
319}
320
321void __init irq_trans_init(struct device_node *dp)
322{
323}