From 5db58c8f6f1364dc632e44c9e578c46b041d18e3 Mon Sep 17 00:00:00 2001 From: Rob Herring Date: Thu, 14 Aug 2014 16:48:25 +0900 Subject: [PATCH] of: introduce common FDT machine related functions Introduce common of_flat_dt_match_machine and of_flat_dt_get_machine_name functions to unify architectures' handling of machine level model and compatible properties. Several architectures match the root compatible string with an arch specific list of machine descriptors duplicating the same search algorithm. Create a common implementation with a simple architecture specific hook to iterate over each machine's match table. Change-Id: I77acb5c560e2b08591c37b57d5d87023aa3fbe91 Signed-off-by: Rob Herring Acked-by: Grant Likely --- drivers/of/fdt.c | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++ include/linux/of_fdt.h | 5 +++++ 2 files changed, 65 insertions(+) diff --git a/drivers/of/fdt.c b/drivers/of/fdt.c index 860dddf..c8ca782 100644 --- a/drivers/of/fdt.c +++ b/drivers/of/fdt.c @@ -622,6 +622,66 @@ int __init of_scan_flat_dt_by_path(const char *path, return ret; } +char * __init of_flat_dt_get_machine_name(void) +{ + char *name; + unsigned long dt_root = of_get_flat_dt_root(); + + name = of_get_flat_dt_prop(dt_root, "model", NULL); + if (!name) + name = of_get_flat_dt_prop(dt_root, "compatible", NULL); + return name; +} + +/** + * of_flat_dt_match_machine - Iterate match tables to find matching machine. + * + * @default_match: A machine specific ptr to return in case of no match. + * @get_next_compat: callback function to return next compatible match table. + * + * Iterate through machine match tables to find the best match for the machine + * compatible string in the FDT. + */ +void * __init of_flat_dt_match_machine(void *default_match, + void * (*get_next_compat)(const char * const**)) +{ + void *data = NULL; + void *best_data = default_match; + const char *const *compat; + unsigned long dt_root; + unsigned int best_score = ~1, score = 0; + + dt_root = of_get_flat_dt_root(); + while ((data = get_next_compat(&compat))) { + score = of_flat_dt_match(dt_root, compat); + if (score > 0 && score < best_score) { + best_data = data; + best_score = score; + } + } + if (!best_data) { + const char *prop; + long size; + + pr_err("\n unrecognized device tree list:\n[ "); + + prop = of_get_flat_dt_prop(dt_root, "compatible", &size); + if (prop) { + while (size > 0) { + printk("'%s' ", prop); + size -= strlen(prop) + 1; + prop += strlen(prop) + 1; + } + } + printk("]\n\n"); + return NULL; + } + + pr_info("Machine model: %s\n", of_flat_dt_get_machine_name()); + + return best_data; +} + #ifdef CONFIG_BLK_DEV_INITRD /** * early_init_dt_check_for_initrd - Decode initrd location from flat tree diff --git a/include/linux/of_fdt.h b/include/linux/of_fdt.h index 19f26f8..1573cac 100644 --- a/include/linux/of_fdt.h +++ b/include/linux/of_fdt.h @@ -117,10 +117,15 @@ extern void early_init_dt_setup_initrd_arch(unsigned long start, extern int early_init_dt_scan_root(unsigned long node, const char *uname, int depth, void *data); +extern char *of_flat_dt_get_machine_name(void); +extern void *of_flat_dt_match_machine(void *default_match, + void * (*get_next_compat)(const char * const**)); + /* Other Prototypes */ extern void unflatten_device_tree(void); extern void early_init_devtree(void *); #else /* CONFIG_OF_FLATTREE */ +static inline const char *of_flat_dt_get_machine_name(void) { return NULL; } static inline void unflatten_device_tree(void) {} #endif /* CONFIG_OF_FLATTREE */ -- 2.7.4