xref: /OK3568_Linux_fs/kernel/arch/mips/include/asm/machine.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0-or-later */
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Copyright (C) 2016 Imagination Technologies
4*4882a593Smuzhiyun  * Author: Paul Burton <paul.burton@mips.com>
5*4882a593Smuzhiyun  */
6*4882a593Smuzhiyun 
7*4882a593Smuzhiyun #ifndef __MIPS_ASM_MACHINE_H__
8*4882a593Smuzhiyun #define __MIPS_ASM_MACHINE_H__
9*4882a593Smuzhiyun 
10*4882a593Smuzhiyun #include <linux/libfdt.h>
11*4882a593Smuzhiyun #include <linux/of.h>
12*4882a593Smuzhiyun 
13*4882a593Smuzhiyun struct mips_machine {
14*4882a593Smuzhiyun 	const struct of_device_id *matches;
15*4882a593Smuzhiyun 	const void *fdt;
16*4882a593Smuzhiyun 	bool (*detect)(void);
17*4882a593Smuzhiyun 	const void *(*fixup_fdt)(const void *fdt, const void *match_data);
18*4882a593Smuzhiyun 	unsigned int (*measure_hpt_freq)(void);
19*4882a593Smuzhiyun };
20*4882a593Smuzhiyun 
21*4882a593Smuzhiyun extern long __mips_machines_start;
22*4882a593Smuzhiyun extern long __mips_machines_end;
23*4882a593Smuzhiyun 
24*4882a593Smuzhiyun #define MIPS_MACHINE(name)						\
25*4882a593Smuzhiyun 	static const struct mips_machine __mips_mach_##name		\
26*4882a593Smuzhiyun 		__used __section(".mips.machines.init")
27*4882a593Smuzhiyun 
28*4882a593Smuzhiyun #define for_each_mips_machine(mach)					\
29*4882a593Smuzhiyun 	for ((mach) = (struct mips_machine *)&__mips_machines_start;	\
30*4882a593Smuzhiyun 	     (mach) < (struct mips_machine *)&__mips_machines_end;	\
31*4882a593Smuzhiyun 	     (mach)++)
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun /**
34*4882a593Smuzhiyun  * mips_machine_is_compatible() - check if a machine is compatible with an FDT
35*4882a593Smuzhiyun  * @mach: the machine struct to check
36*4882a593Smuzhiyun  * @fdt: the FDT to check for compatibility with
37*4882a593Smuzhiyun  *
38*4882a593Smuzhiyun  * Check whether the given machine @mach is compatible with the given flattened
39*4882a593Smuzhiyun  * device tree @fdt, based upon the compatibility property of the root node.
40*4882a593Smuzhiyun  *
41*4882a593Smuzhiyun  * Return: the device id matched if any, else NULL
42*4882a593Smuzhiyun  */
43*4882a593Smuzhiyun static inline const struct of_device_id *
mips_machine_is_compatible(const struct mips_machine * mach,const void * fdt)44*4882a593Smuzhiyun mips_machine_is_compatible(const struct mips_machine *mach, const void *fdt)
45*4882a593Smuzhiyun {
46*4882a593Smuzhiyun 	const struct of_device_id *match;
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun 	if (!mach->matches)
49*4882a593Smuzhiyun 		return NULL;
50*4882a593Smuzhiyun 
51*4882a593Smuzhiyun 	for (match = mach->matches; match->compatible[0]; match++) {
52*4882a593Smuzhiyun 		if (fdt_node_check_compatible(fdt, 0, match->compatible) == 0)
53*4882a593Smuzhiyun 			return match;
54*4882a593Smuzhiyun 	}
55*4882a593Smuzhiyun 
56*4882a593Smuzhiyun 	return NULL;
57*4882a593Smuzhiyun }
58*4882a593Smuzhiyun 
59*4882a593Smuzhiyun /**
60*4882a593Smuzhiyun  * struct mips_fdt_fixup - Describe a fixup to apply to an FDT
61*4882a593Smuzhiyun  * @apply: applies the fixup to @fdt, returns zero on success else -errno
62*4882a593Smuzhiyun  * @description: a short description of the fixup
63*4882a593Smuzhiyun  *
64*4882a593Smuzhiyun  * Describes a fixup applied to an FDT blob by the @apply function. The
65*4882a593Smuzhiyun  * @description field provides a short description of the fixup intended for
66*4882a593Smuzhiyun  * use in error messages if the @apply function returns non-zero.
67*4882a593Smuzhiyun  */
68*4882a593Smuzhiyun struct mips_fdt_fixup {
69*4882a593Smuzhiyun 	int (*apply)(void *fdt);
70*4882a593Smuzhiyun 	const char *description;
71*4882a593Smuzhiyun };
72*4882a593Smuzhiyun 
73*4882a593Smuzhiyun /**
74*4882a593Smuzhiyun  * apply_mips_fdt_fixups() - apply fixups to an FDT blob
75*4882a593Smuzhiyun  * @fdt_out: buffer in which to place the fixed-up FDT
76*4882a593Smuzhiyun  * @fdt_out_size: the size of the @fdt_out buffer
77*4882a593Smuzhiyun  * @fdt_in: the FDT blob
78*4882a593Smuzhiyun  * @fixups: pointer to an array of fixups to be applied
79*4882a593Smuzhiyun  *
80*4882a593Smuzhiyun  * Loop through the array of fixups pointed to by @fixups, calling the apply
81*4882a593Smuzhiyun  * function on each until either one returns an error or we reach the end of
82*4882a593Smuzhiyun  * the list as indicated by an entry with a NULL apply field.
83*4882a593Smuzhiyun  *
84*4882a593Smuzhiyun  * Return: zero on success, else -errno
85*4882a593Smuzhiyun  */
86*4882a593Smuzhiyun extern int __init apply_mips_fdt_fixups(void *fdt_out, size_t fdt_out_size,
87*4882a593Smuzhiyun 					const void *fdt_in,
88*4882a593Smuzhiyun 					const struct mips_fdt_fixup *fixups);
89*4882a593Smuzhiyun 
90*4882a593Smuzhiyun #endif /* __MIPS_ASM_MACHINE_H__ */
91