18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-or-later */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) 2016 Imagination Technologies
48c2ecf20Sopenharmony_ci * Author: Paul Burton <paul.burton@mips.com>
58c2ecf20Sopenharmony_ci */
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci#ifndef __MIPS_ASM_MACHINE_H__
88c2ecf20Sopenharmony_ci#define __MIPS_ASM_MACHINE_H__
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#include <linux/libfdt.h>
118c2ecf20Sopenharmony_ci#include <linux/of.h>
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_cistruct mips_machine {
148c2ecf20Sopenharmony_ci	const struct of_device_id *matches;
158c2ecf20Sopenharmony_ci	const void *fdt;
168c2ecf20Sopenharmony_ci	bool (*detect)(void);
178c2ecf20Sopenharmony_ci	const void *(*fixup_fdt)(const void *fdt, const void *match_data);
188c2ecf20Sopenharmony_ci	unsigned int (*measure_hpt_freq)(void);
198c2ecf20Sopenharmony_ci};
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ciextern long __mips_machines_start;
228c2ecf20Sopenharmony_ciextern long __mips_machines_end;
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci#define MIPS_MACHINE(name)						\
258c2ecf20Sopenharmony_ci	static const struct mips_machine __mips_mach_##name		\
268c2ecf20Sopenharmony_ci		__used __section(".mips.machines.init")
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci#define for_each_mips_machine(mach)					\
298c2ecf20Sopenharmony_ci	for ((mach) = (struct mips_machine *)&__mips_machines_start;	\
308c2ecf20Sopenharmony_ci	     (mach) < (struct mips_machine *)&__mips_machines_end;	\
318c2ecf20Sopenharmony_ci	     (mach)++)
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci/**
348c2ecf20Sopenharmony_ci * mips_machine_is_compatible() - check if a machine is compatible with an FDT
358c2ecf20Sopenharmony_ci * @mach: the machine struct to check
368c2ecf20Sopenharmony_ci * @fdt: the FDT to check for compatibility with
378c2ecf20Sopenharmony_ci *
388c2ecf20Sopenharmony_ci * Check whether the given machine @mach is compatible with the given flattened
398c2ecf20Sopenharmony_ci * device tree @fdt, based upon the compatibility property of the root node.
408c2ecf20Sopenharmony_ci *
418c2ecf20Sopenharmony_ci * Return: the device id matched if any, else NULL
428c2ecf20Sopenharmony_ci */
438c2ecf20Sopenharmony_cistatic inline const struct of_device_id *
448c2ecf20Sopenharmony_cimips_machine_is_compatible(const struct mips_machine *mach, const void *fdt)
458c2ecf20Sopenharmony_ci{
468c2ecf20Sopenharmony_ci	const struct of_device_id *match;
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci	if (!mach->matches)
498c2ecf20Sopenharmony_ci		return NULL;
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci	for (match = mach->matches; match->compatible[0]; match++) {
528c2ecf20Sopenharmony_ci		if (fdt_node_check_compatible(fdt, 0, match->compatible) == 0)
538c2ecf20Sopenharmony_ci			return match;
548c2ecf20Sopenharmony_ci	}
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci	return NULL;
578c2ecf20Sopenharmony_ci}
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci/**
608c2ecf20Sopenharmony_ci * struct mips_fdt_fixup - Describe a fixup to apply to an FDT
618c2ecf20Sopenharmony_ci * @apply: applies the fixup to @fdt, returns zero on success else -errno
628c2ecf20Sopenharmony_ci * @description: a short description of the fixup
638c2ecf20Sopenharmony_ci *
648c2ecf20Sopenharmony_ci * Describes a fixup applied to an FDT blob by the @apply function. The
658c2ecf20Sopenharmony_ci * @description field provides a short description of the fixup intended for
668c2ecf20Sopenharmony_ci * use in error messages if the @apply function returns non-zero.
678c2ecf20Sopenharmony_ci */
688c2ecf20Sopenharmony_cistruct mips_fdt_fixup {
698c2ecf20Sopenharmony_ci	int (*apply)(void *fdt);
708c2ecf20Sopenharmony_ci	const char *description;
718c2ecf20Sopenharmony_ci};
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci/**
748c2ecf20Sopenharmony_ci * apply_mips_fdt_fixups() - apply fixups to an FDT blob
758c2ecf20Sopenharmony_ci * @fdt_out: buffer in which to place the fixed-up FDT
768c2ecf20Sopenharmony_ci * @fdt_out_size: the size of the @fdt_out buffer
778c2ecf20Sopenharmony_ci * @fdt_in: the FDT blob
788c2ecf20Sopenharmony_ci * @fixups: pointer to an array of fixups to be applied
798c2ecf20Sopenharmony_ci *
808c2ecf20Sopenharmony_ci * Loop through the array of fixups pointed to by @fixups, calling the apply
818c2ecf20Sopenharmony_ci * function on each until either one returns an error or we reach the end of
828c2ecf20Sopenharmony_ci * the list as indicated by an entry with a NULL apply field.
838c2ecf20Sopenharmony_ci *
848c2ecf20Sopenharmony_ci * Return: zero on success, else -errno
858c2ecf20Sopenharmony_ci */
868c2ecf20Sopenharmony_ciextern int __init apply_mips_fdt_fixups(void *fdt_out, size_t fdt_out_size,
878c2ecf20Sopenharmony_ci					const void *fdt_in,
888c2ecf20Sopenharmony_ci					const struct mips_fdt_fixup *fixups);
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci#endif /* __MIPS_ASM_MACHINE_H__ */
91