xref: /linux/arch/powerpc/include/asm/prom.h (revision d8678b58708d7e6bf947ebd03eaf44baf2adfad8)
19d24c888SGrant Likely #include <linux/of.h>	/* linux/of.h gets to determine #include ordering */
2b8b572e1SStephen Rothwell #ifndef _POWERPC_PROM_H
3b8b572e1SStephen Rothwell #define _POWERPC_PROM_H
4b8b572e1SStephen Rothwell #ifdef __KERNEL__
5b8b572e1SStephen Rothwell 
6b8b572e1SStephen Rothwell /*
7b8b572e1SStephen Rothwell  * Definitions for talking to the Open Firmware PROM on
8b8b572e1SStephen Rothwell  * Power Macintosh computers.
9b8b572e1SStephen Rothwell  *
10b8b572e1SStephen Rothwell  * Copyright (C) 1996-2005 Paul Mackerras.
11b8b572e1SStephen Rothwell  *
12b8b572e1SStephen Rothwell  * Updates for PPC64 by Peter Bergner & David Engebretsen, IBM Corp.
13b8b572e1SStephen Rothwell  *
14b8b572e1SStephen Rothwell  * This program is free software; you can redistribute it and/or
15b8b572e1SStephen Rothwell  * modify it under the terms of the GNU General Public License
16b8b572e1SStephen Rothwell  * as published by the Free Software Foundation; either version
17b8b572e1SStephen Rothwell  * 2 of the License, or (at your option) any later version.
18b8b572e1SStephen Rothwell  */
19b8b572e1SStephen Rothwell #include <linux/types.h>
20*d8678b58SGrant Likely #include <linux/of_fdt.h>
21b8b572e1SStephen Rothwell #include <linux/proc_fs.h>
22b8b572e1SStephen Rothwell #include <linux/platform_device.h>
23b8b572e1SStephen Rothwell #include <asm/irq.h>
24b8b572e1SStephen Rothwell #include <asm/atomic.h>
25b8b572e1SStephen Rothwell 
26b8b572e1SStephen Rothwell #define OF_ROOT_NODE_ADDR_CELLS_DEFAULT	1
27b8b572e1SStephen Rothwell #define OF_ROOT_NODE_SIZE_CELLS_DEFAULT	1
28b8b572e1SStephen Rothwell 
29b8b572e1SStephen Rothwell #define of_compat_cmp(s1, s2, l)	strcasecmp((s1), (s2))
30b8b572e1SStephen Rothwell #define of_prop_cmp(s1, s2)		strcmp((s1), (s2))
31b8b572e1SStephen Rothwell #define of_node_cmp(s1, s2)		strcasecmp((s1), (s2))
32b8b572e1SStephen Rothwell 
33b8b572e1SStephen Rothwell /*
34b8b572e1SStephen Rothwell  * This is what gets passed to the kernel by prom_init or kexec
35b8b572e1SStephen Rothwell  *
36b8b572e1SStephen Rothwell  * The dt struct contains the device tree structure, full pathes and
37b8b572e1SStephen Rothwell  * property contents. The dt strings contain a separate block with just
38b8b572e1SStephen Rothwell  * the strings for the property names, and is fully page aligned and
39b8b572e1SStephen Rothwell  * self contained in a page, so that it can be kept around by the kernel,
40b8b572e1SStephen Rothwell  * each property name appears only once in this page (cheap compression)
41b8b572e1SStephen Rothwell  *
42b8b572e1SStephen Rothwell  * the mem_rsvmap contains a map of reserved ranges of physical memory,
43b8b572e1SStephen Rothwell  * passing it here instead of in the device-tree itself greatly simplifies
44b8b572e1SStephen Rothwell  * the job of everybody. It's just a list of u64 pairs (base/size) that
45b8b572e1SStephen Rothwell  * ends when size is 0
46b8b572e1SStephen Rothwell  */
47b8b572e1SStephen Rothwell struct boot_param_header
48b8b572e1SStephen Rothwell {
49b8b572e1SStephen Rothwell 	u32	magic;			/* magic word OF_DT_HEADER */
50b8b572e1SStephen Rothwell 	u32	totalsize;		/* total size of DT block */
51b8b572e1SStephen Rothwell 	u32	off_dt_struct;		/* offset to structure */
52b8b572e1SStephen Rothwell 	u32	off_dt_strings;		/* offset to strings */
53b8b572e1SStephen Rothwell 	u32	off_mem_rsvmap;		/* offset to memory reserve map */
54b8b572e1SStephen Rothwell 	u32	version;		/* format version */
55b8b572e1SStephen Rothwell 	u32	last_comp_version;	/* last compatible version */
56b8b572e1SStephen Rothwell 	/* version 2 fields below */
57b8b572e1SStephen Rothwell 	u32	boot_cpuid_phys;	/* Physical CPU id we're booting on */
58b8b572e1SStephen Rothwell 	/* version 3 fields below */
59b8b572e1SStephen Rothwell 	u32	dt_strings_size;	/* size of the DT strings block */
60b8b572e1SStephen Rothwell 	/* version 17 fields below */
61b8b572e1SStephen Rothwell 	u32	dt_struct_size;		/* size of the DT structure block */
62b8b572e1SStephen Rothwell };
63b8b572e1SStephen Rothwell 
64b8b572e1SStephen Rothwell extern struct device_node *of_chosen;
65b8b572e1SStephen Rothwell 
66b8b572e1SStephen Rothwell static inline int of_node_check_flag(struct device_node *n, unsigned long flag)
67b8b572e1SStephen Rothwell {
68b8b572e1SStephen Rothwell 	return test_bit(flag, &n->_flags);
69b8b572e1SStephen Rothwell }
70b8b572e1SStephen Rothwell 
71b8b572e1SStephen Rothwell static inline void of_node_set_flag(struct device_node *n, unsigned long flag)
72b8b572e1SStephen Rothwell {
73b8b572e1SStephen Rothwell 	set_bit(flag, &n->_flags);
74b8b572e1SStephen Rothwell }
75b8b572e1SStephen Rothwell 
76b8b572e1SStephen Rothwell 
77b8b572e1SStephen Rothwell #define HAVE_ARCH_DEVTREE_FIXUPS
78b8b572e1SStephen Rothwell 
79b8b572e1SStephen Rothwell static inline void set_node_proc_entry(struct device_node *dn, struct proc_dir_entry *de)
80b8b572e1SStephen Rothwell {
81b8b572e1SStephen Rothwell 	dn->pde = de;
82b8b572e1SStephen Rothwell }
83b8b572e1SStephen Rothwell 
84b8b572e1SStephen Rothwell 
85b8b572e1SStephen Rothwell extern struct device_node *of_find_all_nodes(struct device_node *prev);
86b8b572e1SStephen Rothwell extern struct device_node *of_node_get(struct device_node *node);
87b8b572e1SStephen Rothwell extern void of_node_put(struct device_node *node);
88b8b572e1SStephen Rothwell 
89b8b572e1SStephen Rothwell /* For scanning the flat device-tree at boot time */
90b8b572e1SStephen Rothwell extern int __init of_scan_flat_dt(int (*it)(unsigned long node,
91b8b572e1SStephen Rothwell 					    const char *uname, int depth,
92b8b572e1SStephen Rothwell 					    void *data),
93b8b572e1SStephen Rothwell 				  void *data);
94b8b572e1SStephen Rothwell extern void* __init of_get_flat_dt_prop(unsigned long node, const char *name,
95b8b572e1SStephen Rothwell 					unsigned long *size);
96b8b572e1SStephen Rothwell extern int __init of_flat_dt_is_compatible(unsigned long node, const char *name);
97b8b572e1SStephen Rothwell extern unsigned long __init of_get_flat_dt_root(void);
98b8b572e1SStephen Rothwell 
99b8b572e1SStephen Rothwell /* For updating the device tree at runtime */
100b8b572e1SStephen Rothwell extern void of_attach_node(struct device_node *);
101b8b572e1SStephen Rothwell extern void of_detach_node(struct device_node *);
102b8b572e1SStephen Rothwell 
103b8b572e1SStephen Rothwell /* Other Prototypes */
104b8b572e1SStephen Rothwell extern void finish_device_tree(void);
105b8b572e1SStephen Rothwell extern void unflatten_device_tree(void);
106b8b572e1SStephen Rothwell extern void early_init_devtree(void *);
107b8b572e1SStephen Rothwell extern int machine_is_compatible(const char *compat);
108b8b572e1SStephen Rothwell extern void print_properties(struct device_node *node);
109b8b572e1SStephen Rothwell extern int prom_n_intr_cells(struct device_node* np);
110b8b572e1SStephen Rothwell extern void prom_get_irq_senses(unsigned char *senses, int off, int max);
111b8b572e1SStephen Rothwell extern int prom_add_property(struct device_node* np, struct property* prop);
112b8b572e1SStephen Rothwell extern int prom_remove_property(struct device_node *np, struct property *prop);
113b8b572e1SStephen Rothwell extern int prom_update_property(struct device_node *np,
114b8b572e1SStephen Rothwell 				struct property *newprop,
115b8b572e1SStephen Rothwell 				struct property *oldprop);
116b8b572e1SStephen Rothwell 
117b8b572e1SStephen Rothwell #ifdef CONFIG_PPC32
118b8b572e1SStephen Rothwell /*
119b8b572e1SStephen Rothwell  * PCI <-> OF matching functions
120b8b572e1SStephen Rothwell  * (XXX should these be here?)
121b8b572e1SStephen Rothwell  */
122b8b572e1SStephen Rothwell struct pci_bus;
123b8b572e1SStephen Rothwell struct pci_dev;
124b8b572e1SStephen Rothwell extern int pci_device_from_OF_node(struct device_node *node,
125b8b572e1SStephen Rothwell 				   u8* bus, u8* devfn);
126b8b572e1SStephen Rothwell extern struct device_node* pci_busdev_to_OF_node(struct pci_bus *, int);
127b8b572e1SStephen Rothwell extern struct device_node* pci_device_to_OF_node(struct pci_dev *);
128b8b572e1SStephen Rothwell extern void pci_create_OF_bus_map(void);
129b8b572e1SStephen Rothwell #endif
130b8b572e1SStephen Rothwell 
131b8b572e1SStephen Rothwell extern struct resource *request_OF_resource(struct device_node* node,
132b8b572e1SStephen Rothwell 				int index, const char* name_postfix);
133b8b572e1SStephen Rothwell extern int release_OF_resource(struct device_node* node, int index);
134b8b572e1SStephen Rothwell 
135b8b572e1SStephen Rothwell 
136b8b572e1SStephen Rothwell /*
137b8b572e1SStephen Rothwell  * OF address retreival & translation
138b8b572e1SStephen Rothwell  */
139b8b572e1SStephen Rothwell 
140b8b572e1SStephen Rothwell 
141b8b572e1SStephen Rothwell /* Helper to read a big number; size is in cells (not bytes) */
142b8b572e1SStephen Rothwell static inline u64 of_read_number(const u32 *cell, int size)
143b8b572e1SStephen Rothwell {
144b8b572e1SStephen Rothwell 	u64 r = 0;
145b8b572e1SStephen Rothwell 	while (size--)
146b8b572e1SStephen Rothwell 		r = (r << 32) | *(cell++);
147b8b572e1SStephen Rothwell 	return r;
148b8b572e1SStephen Rothwell }
149b8b572e1SStephen Rothwell 
150b8b572e1SStephen Rothwell /* Like of_read_number, but we want an unsigned long result */
151b8b572e1SStephen Rothwell #ifdef CONFIG_PPC32
152b8b572e1SStephen Rothwell static inline unsigned long of_read_ulong(const u32 *cell, int size)
153b8b572e1SStephen Rothwell {
154b8b572e1SStephen Rothwell 	return cell[size-1];
155b8b572e1SStephen Rothwell }
156b8b572e1SStephen Rothwell #else
157b8b572e1SStephen Rothwell #define of_read_ulong(cell, size)	of_read_number(cell, size)
158b8b572e1SStephen Rothwell #endif
159b8b572e1SStephen Rothwell 
160b8b572e1SStephen Rothwell /* Translate an OF address block into a CPU physical address
161b8b572e1SStephen Rothwell  */
162b8b572e1SStephen Rothwell extern u64 of_translate_address(struct device_node *np, const u32 *addr);
163b8b572e1SStephen Rothwell 
164b8b572e1SStephen Rothwell /* Translate a DMA address from device space to CPU space */
165b8b572e1SStephen Rothwell extern u64 of_translate_dma_address(struct device_node *dev,
166b8b572e1SStephen Rothwell 				    const u32 *in_addr);
167b8b572e1SStephen Rothwell 
168b8b572e1SStephen Rothwell /* Extract an address from a device, returns the region size and
169b8b572e1SStephen Rothwell  * the address space flags too. The PCI version uses a BAR number
170b8b572e1SStephen Rothwell  * instead of an absolute index
171b8b572e1SStephen Rothwell  */
172b8b572e1SStephen Rothwell extern const u32 *of_get_address(struct device_node *dev, int index,
173b8b572e1SStephen Rothwell 			   u64 *size, unsigned int *flags);
174b8b572e1SStephen Rothwell #ifdef CONFIG_PCI
175b8b572e1SStephen Rothwell extern const u32 *of_get_pci_address(struct device_node *dev, int bar_no,
176b8b572e1SStephen Rothwell 			       u64 *size, unsigned int *flags);
177b8b572e1SStephen Rothwell #else
178b8b572e1SStephen Rothwell static inline const u32 *of_get_pci_address(struct device_node *dev,
179b8b572e1SStephen Rothwell 		int bar_no, u64 *size, unsigned int *flags)
180b8b572e1SStephen Rothwell {
181b8b572e1SStephen Rothwell 	return NULL;
182b8b572e1SStephen Rothwell }
183b8b572e1SStephen Rothwell #endif /* CONFIG_PCI */
184b8b572e1SStephen Rothwell 
185b8b572e1SStephen Rothwell /* Get an address as a resource. Note that if your address is
186b8b572e1SStephen Rothwell  * a PIO address, the conversion will fail if the physical address
187b8b572e1SStephen Rothwell  * can't be internally converted to an IO token with
188b8b572e1SStephen Rothwell  * pci_address_to_pio(), that is because it's either called to early
189b8b572e1SStephen Rothwell  * or it can't be matched to any host bridge IO space
190b8b572e1SStephen Rothwell  */
191b8b572e1SStephen Rothwell extern int of_address_to_resource(struct device_node *dev, int index,
192b8b572e1SStephen Rothwell 				  struct resource *r);
193b8b572e1SStephen Rothwell #ifdef CONFIG_PCI
194b8b572e1SStephen Rothwell extern int of_pci_address_to_resource(struct device_node *dev, int bar,
195b8b572e1SStephen Rothwell 				      struct resource *r);
196b8b572e1SStephen Rothwell #else
197b8b572e1SStephen Rothwell static inline int of_pci_address_to_resource(struct device_node *dev, int bar,
198b8b572e1SStephen Rothwell 		struct resource *r)
199b8b572e1SStephen Rothwell {
200b8b572e1SStephen Rothwell 	return -ENOSYS;
201b8b572e1SStephen Rothwell }
202b8b572e1SStephen Rothwell #endif /* CONFIG_PCI */
203b8b572e1SStephen Rothwell 
204b8b572e1SStephen Rothwell /* Parse the ibm,dma-window property of an OF node into the busno, phys and
205b8b572e1SStephen Rothwell  * size parameters.
206b8b572e1SStephen Rothwell  */
207b8b572e1SStephen Rothwell void of_parse_dma_window(struct device_node *dn, const void *dma_window_prop,
208b8b572e1SStephen Rothwell 		unsigned long *busno, unsigned long *phys, unsigned long *size);
209b8b572e1SStephen Rothwell 
210b8b572e1SStephen Rothwell extern void kdump_move_device_tree(void);
211b8b572e1SStephen Rothwell 
212b8b572e1SStephen Rothwell /* CPU OF node matching */
213b8b572e1SStephen Rothwell struct device_node *of_get_cpu_node(int cpu, unsigned int *thread);
214b8b572e1SStephen Rothwell 
215e523f723SNathan Lynch /* cache lookup */
216e523f723SNathan Lynch struct device_node *of_find_next_cache_node(struct device_node *np);
217e523f723SNathan Lynch 
218b8b572e1SStephen Rothwell /* Get the MAC address */
219b8b572e1SStephen Rothwell extern const void *of_get_mac_address(struct device_node *np);
220b8b572e1SStephen Rothwell 
221b8b572e1SStephen Rothwell /*
222b8b572e1SStephen Rothwell  * OF interrupt mapping
223b8b572e1SStephen Rothwell  */
224b8b572e1SStephen Rothwell 
225b8b572e1SStephen Rothwell /* This structure is returned when an interrupt is mapped. The controller
226b8b572e1SStephen Rothwell  * field needs to be put() after use
227b8b572e1SStephen Rothwell  */
228b8b572e1SStephen Rothwell 
229b8b572e1SStephen Rothwell #define OF_MAX_IRQ_SPEC		 4 /* We handle specifiers of at most 4 cells */
230b8b572e1SStephen Rothwell 
231b8b572e1SStephen Rothwell struct of_irq {
232b8b572e1SStephen Rothwell 	struct device_node *controller;	/* Interrupt controller node */
233b8b572e1SStephen Rothwell 	u32 size;			/* Specifier size */
234b8b572e1SStephen Rothwell 	u32 specifier[OF_MAX_IRQ_SPEC];	/* Specifier copy */
235b8b572e1SStephen Rothwell };
236b8b572e1SStephen Rothwell 
237b8b572e1SStephen Rothwell /**
238b8b572e1SStephen Rothwell  * of_irq_map_init - Initialize the irq remapper
239b8b572e1SStephen Rothwell  * @flags:	flags defining workarounds to enable
240b8b572e1SStephen Rothwell  *
241b8b572e1SStephen Rothwell  * Some machines have bugs in the device-tree which require certain workarounds
242b8b572e1SStephen Rothwell  * to be applied. Call this before any interrupt mapping attempts to enable
243b8b572e1SStephen Rothwell  * those workarounds.
244b8b572e1SStephen Rothwell  */
245b8b572e1SStephen Rothwell #define OF_IMAP_OLDWORLD_MAC	0x00000001
246b8b572e1SStephen Rothwell #define OF_IMAP_NO_PHANDLE	0x00000002
247b8b572e1SStephen Rothwell 
248b8b572e1SStephen Rothwell extern void of_irq_map_init(unsigned int flags);
249b8b572e1SStephen Rothwell 
250b8b572e1SStephen Rothwell /**
251b8b572e1SStephen Rothwell  * of_irq_map_raw - Low level interrupt tree parsing
252b8b572e1SStephen Rothwell  * @parent:	the device interrupt parent
253b8b572e1SStephen Rothwell  * @intspec:	interrupt specifier ("interrupts" property of the device)
254b8b572e1SStephen Rothwell  * @ointsize:   size of the passed in interrupt specifier
255b8b572e1SStephen Rothwell  * @addr:	address specifier (start of "reg" property of the device)
256b8b572e1SStephen Rothwell  * @out_irq:	structure of_irq filled by this function
257b8b572e1SStephen Rothwell  *
258b8b572e1SStephen Rothwell  * Returns 0 on success and a negative number on error
259b8b572e1SStephen Rothwell  *
260b8b572e1SStephen Rothwell  * This function is a low-level interrupt tree walking function. It
261b8b572e1SStephen Rothwell  * can be used to do a partial walk with synthetized reg and interrupts
262b8b572e1SStephen Rothwell  * properties, for example when resolving PCI interrupts when no device
263b8b572e1SStephen Rothwell  * node exist for the parent.
264b8b572e1SStephen Rothwell  *
265b8b572e1SStephen Rothwell  */
266b8b572e1SStephen Rothwell 
267b8b572e1SStephen Rothwell extern int of_irq_map_raw(struct device_node *parent, const u32 *intspec,
268b8b572e1SStephen Rothwell 			  u32 ointsize, const u32 *addr,
269b8b572e1SStephen Rothwell 			  struct of_irq *out_irq);
270b8b572e1SStephen Rothwell 
271b8b572e1SStephen Rothwell 
272b8b572e1SStephen Rothwell /**
273b8b572e1SStephen Rothwell  * of_irq_map_one - Resolve an interrupt for a device
274b8b572e1SStephen Rothwell  * @device:	the device whose interrupt is to be resolved
275b8b572e1SStephen Rothwell  * @index:     	index of the interrupt to resolve
276b8b572e1SStephen Rothwell  * @out_irq:	structure of_irq filled by this function
277b8b572e1SStephen Rothwell  *
278b8b572e1SStephen Rothwell  * This function resolves an interrupt, walking the tree, for a given
279b8b572e1SStephen Rothwell  * device-tree node. It's the high level pendant to of_irq_map_raw().
280b8b572e1SStephen Rothwell  * It also implements the workarounds for OldWolrd Macs.
281b8b572e1SStephen Rothwell  */
282b8b572e1SStephen Rothwell extern int of_irq_map_one(struct device_node *device, int index,
283b8b572e1SStephen Rothwell 			  struct of_irq *out_irq);
284b8b572e1SStephen Rothwell 
285b8b572e1SStephen Rothwell /**
286b8b572e1SStephen Rothwell  * of_irq_map_pci - Resolve the interrupt for a PCI device
287b8b572e1SStephen Rothwell  * @pdev:	the device whose interrupt is to be resolved
288b8b572e1SStephen Rothwell  * @out_irq:	structure of_irq filled by this function
289b8b572e1SStephen Rothwell  *
290b8b572e1SStephen Rothwell  * This function resolves the PCI interrupt for a given PCI device. If a
291b8b572e1SStephen Rothwell  * device-node exists for a given pci_dev, it will use normal OF tree
292b8b572e1SStephen Rothwell  * walking. If not, it will implement standard swizzling and walk up the
293b8b572e1SStephen Rothwell  * PCI tree until an device-node is found, at which point it will finish
294b8b572e1SStephen Rothwell  * resolving using the OF tree walking.
295b8b572e1SStephen Rothwell  */
296b8b572e1SStephen Rothwell struct pci_dev;
297b8b572e1SStephen Rothwell extern int of_irq_map_pci(struct pci_dev *pdev, struct of_irq *out_irq);
298b8b572e1SStephen Rothwell 
299b8b572e1SStephen Rothwell extern int of_irq_to_resource(struct device_node *dev, int index,
300b8b572e1SStephen Rothwell 			struct resource *r);
301b8b572e1SStephen Rothwell 
302b8b572e1SStephen Rothwell /**
303b8b572e1SStephen Rothwell  * of_iomap - Maps the memory mapped IO for a given device_node
304b8b572e1SStephen Rothwell  * @device:	the device whose io range will be mapped
305b8b572e1SStephen Rothwell  * @index:	index of the io range
306b8b572e1SStephen Rothwell  *
307b8b572e1SStephen Rothwell  * Returns a pointer to the mapped memory
308b8b572e1SStephen Rothwell  */
309b8b572e1SStephen Rothwell extern void __iomem *of_iomap(struct device_node *device, int index);
310b8b572e1SStephen Rothwell 
311b8b572e1SStephen Rothwell #endif /* __KERNEL__ */
312b8b572e1SStephen Rothwell #endif /* _POWERPC_PROM_H */
313