Skip to content

Commit a4f740c

Browse files
committed
of/flattree: Add of_flat_dt_match() helper function
This patch adds of_flat_dt_match() which tests a node for compatibility with a list of values and converts the relevant powerpc platform code to use it. This approach simplifies the board support code a bit. Signed-off-by: Grant Likely <[email protected]> Reviewed-by: Stephen Neuendorffer <[email protected]>
1 parent 73930a8 commit a4f740c

File tree

11 files changed

+89
-78
lines changed

11 files changed

+89
-78
lines changed

arch/powerpc/platforms/40x/ppc40x_simple.c

+4-9
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ machine_device_initcall(ppc40x_simple, ppc40x_device_probe);
5050
* Again, if your board needs to do things differently then create a
5151
* board.c file for it rather than adding it to this list.
5252
*/
53-
static char *board[] __initdata = {
53+
static const char *board[] __initdata = {
5454
"amcc,acadia",
5555
"amcc,haleakala",
5656
"amcc,kilauea",
@@ -60,14 +60,9 @@ static char *board[] __initdata = {
6060

6161
static int __init ppc40x_probe(void)
6262
{
63-
unsigned long root = of_get_flat_dt_root();
64-
int i = 0;
65-
66-
for (i = 0; i < ARRAY_SIZE(board); i++) {
67-
if (of_flat_dt_is_compatible(root, board[i])) {
68-
ppc_pci_set_flags(PPC_PCI_REASSIGN_ALL_RSRC);
69-
return 1;
70-
}
63+
if (of_flat_dt_match(of_get_flat_dt_root(), board)) {
64+
ppc_pci_set_flags(PPC_PCI_REASSIGN_ALL_RSRC);
65+
return 1;
7166
}
7267

7368
return 0;

arch/powerpc/platforms/512x/mpc5121_generic.c

+2-11
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
/*
2727
* list of supported boards
2828
*/
29-
static char *board[] __initdata = {
29+
static const char *board[] __initdata = {
3030
"prt,prtlvt",
3131
NULL
3232
};
@@ -36,16 +36,7 @@ static char *board[] __initdata = {
3636
*/
3737
static int __init mpc5121_generic_probe(void)
3838
{
39-
unsigned long node = of_get_flat_dt_root();
40-
int i = 0;
41-
42-
while (board[i]) {
43-
if (of_flat_dt_is_compatible(node, board[i]))
44-
break;
45-
i++;
46-
}
47-
48-
return board[i] != NULL;
39+
return of_flat_dt_match(of_get_flat_dt_root(), board);
4940
}
5041

5142
define_machine(mpc5121_generic) {

arch/powerpc/platforms/52xx/lite5200.c

+7-9
Original file line numberDiff line numberDiff line change
@@ -172,20 +172,18 @@ static void __init lite5200_setup_arch(void)
172172
mpc52xx_setup_pci();
173173
}
174174

175+
static const char *board[] __initdata = {
176+
"fsl,lite5200",
177+
"fsl,lite5200b",
178+
NULL,
179+
};
180+
175181
/*
176182
* Called very early, MMU is off, device-tree isn't unflattened
177183
*/
178184
static int __init lite5200_probe(void)
179185
{
180-
unsigned long node = of_get_flat_dt_root();
181-
const char *model = of_get_flat_dt_prop(node, "model", NULL);
182-
183-
if (!of_flat_dt_is_compatible(node, "fsl,lite5200") &&
184-
!of_flat_dt_is_compatible(node, "fsl,lite5200b"))
185-
return 0;
186-
pr_debug("%s board found\n", model ? model : "unknown");
187-
188-
return 1;
186+
return of_flat_dt_match(of_get_flat_dt_root(), board);
189187
}
190188

191189
define_machine(lite5200) {

arch/powerpc/platforms/52xx/media5200.c

+2-11
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ static void __init media5200_setup_arch(void)
239239
}
240240

241241
/* list of the supported boards */
242-
static char *board[] __initdata = {
242+
static const char *board[] __initdata = {
243243
"fsl,media5200",
244244
NULL
245245
};
@@ -249,16 +249,7 @@ static char *board[] __initdata = {
249249
*/
250250
static int __init media5200_probe(void)
251251
{
252-
unsigned long node = of_get_flat_dt_root();
253-
int i = 0;
254-
255-
while (board[i]) {
256-
if (of_flat_dt_is_compatible(node, board[i]))
257-
break;
258-
i++;
259-
}
260-
261-
return (board[i] != NULL);
252+
return of_flat_dt_match(of_get_flat_dt_root(), board);
262253
}
263254

264255
define_machine(media5200_platform) {

arch/powerpc/platforms/52xx/mpc5200_simple.c

+2-11
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ static void __init mpc5200_simple_setup_arch(void)
4949
}
5050

5151
/* list of the supported boards */
52-
static char *board[] __initdata = {
52+
static const char *board[] __initdata = {
5353
"intercontrol,digsy-mtc",
5454
"manroland,mucmc52",
5555
"manroland,uc101",
@@ -66,16 +66,7 @@ static char *board[] __initdata = {
6666
*/
6767
static int __init mpc5200_simple_probe(void)
6868
{
69-
unsigned long node = of_get_flat_dt_root();
70-
int i = 0;
71-
72-
while (board[i]) {
73-
if (of_flat_dt_is_compatible(node, board[i]))
74-
break;
75-
i++;
76-
}
77-
78-
return (board[i] != NULL);
69+
return of_flat_dt_match(of_get_flat_dt_root(), board);
7970
}
8071

8172
define_machine(mpc5200_simple_platform) {

arch/powerpc/platforms/83xx/mpc830x_rdb.c

+8-5
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,19 @@ static void __init mpc830x_rdb_init_IRQ(void)
5757
ipic_set_default_priority();
5858
}
5959

60+
struct const char *board[] __initdata = {
61+
"MPC8308RDB",
62+
"fsl,mpc8308rdb",
63+
"denx,mpc8308_p1m",
64+
NULL
65+
}
66+
6067
/*
6168
* Called very early, MMU is off, device-tree isn't unflattened
6269
*/
6370
static int __init mpc830x_rdb_probe(void)
6471
{
65-
unsigned long root = of_get_flat_dt_root();
66-
67-
return of_flat_dt_is_compatible(root, "MPC8308RDB") ||
68-
of_flat_dt_is_compatible(root, "fsl,mpc8308rdb") ||
69-
of_flat_dt_is_compatible(root, "denx,mpc8308_p1m");
72+
return of_flat_dt_match(of_get_flat_dt_root(), board);
7073
}
7174

7275
static struct of_device_id __initdata of_bus_ids[] = {

arch/powerpc/platforms/83xx/mpc831x_rdb.c

+7-4
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,18 @@ static void __init mpc831x_rdb_init_IRQ(void)
6060
ipic_set_default_priority();
6161
}
6262

63+
struct const char *board[] __initdata = {
64+
"MPC8313ERDB",
65+
"fsl,mpc8315erdb",
66+
NULL
67+
}
68+
6369
/*
6470
* Called very early, MMU is off, device-tree isn't unflattened
6571
*/
6672
static int __init mpc831x_rdb_probe(void)
6773
{
68-
unsigned long root = of_get_flat_dt_root();
69-
70-
return of_flat_dt_is_compatible(root, "MPC8313ERDB") ||
71-
of_flat_dt_is_compatible(root, "fsl,mpc8315erdb");
74+
return of_flat_dt_match(of_get_flat_dt_root(), board);
7275
}
7376

7477
static struct of_device_id __initdata of_bus_ids[] = {

arch/powerpc/platforms/83xx/mpc837x_rdb.c

+9-6
Original file line numberDiff line numberDiff line change
@@ -101,17 +101,20 @@ static void __init mpc837x_rdb_init_IRQ(void)
101101
ipic_set_default_priority();
102102
}
103103

104+
static const char *board[] __initdata = {
105+
"fsl,mpc8377rdb",
106+
"fsl,mpc8378rdb",
107+
"fsl,mpc8379rdb",
108+
"fsl,mpc8377wlan",
109+
NULL
110+
};
111+
104112
/*
105113
* Called very early, MMU is off, device-tree isn't unflattened
106114
*/
107115
static int __init mpc837x_rdb_probe(void)
108116
{
109-
unsigned long root = of_get_flat_dt_root();
110-
111-
return of_flat_dt_is_compatible(root, "fsl,mpc8377rdb") ||
112-
of_flat_dt_is_compatible(root, "fsl,mpc8378rdb") ||
113-
of_flat_dt_is_compatible(root, "fsl,mpc8379rdb") ||
114-
of_flat_dt_is_compatible(root, "fsl,mpc8377wlan");
117+
return of_flat_dt_match(of_get_flat_dt_root(), board);
115118
}
116119

117120
define_machine(mpc837x_rdb) {

arch/powerpc/platforms/85xx/tqm85xx.c

+10-10
Original file line numberDiff line numberDiff line change
@@ -186,21 +186,21 @@ static int __init declare_of_platform_devices(void)
186186
}
187187
machine_device_initcall(tqm85xx, declare_of_platform_devices);
188188

189+
static const char *board[] __initdata = {
190+
"tqc,tqm8540",
191+
"tqc,tqm8541",
192+
"tqc,tqm8548",
193+
"tqc,tqm8555",
194+
"tqc,tqm8560",
195+
NULL
196+
};
197+
189198
/*
190199
* Called very early, device-tree isn't unflattened
191200
*/
192201
static int __init tqm85xx_probe(void)
193202
{
194-
unsigned long root = of_get_flat_dt_root();
195-
196-
if ((of_flat_dt_is_compatible(root, "tqc,tqm8540")) ||
197-
(of_flat_dt_is_compatible(root, "tqc,tqm8541")) ||
198-
(of_flat_dt_is_compatible(root, "tqc,tqm8548")) ||
199-
(of_flat_dt_is_compatible(root, "tqc,tqm8555")) ||
200-
(of_flat_dt_is_compatible(root, "tqc,tqm8560")))
201-
return 1;
202-
203-
return 0;
203+
return of_flat_dt_match(of_get_flat_dt_root(), board);
204204
}
205205

206206
define_machine(tqm85xx) {

drivers/of/fdt.c

+35-2
Original file line numberDiff line numberDiff line change
@@ -78,19 +78,23 @@ void *of_fdt_get_property(struct boot_param_header *blob,
7878
* @blob: A device tree blob
7979
* @node: node to test
8080
* @compat: compatible string to compare with compatible list.
81+
*
82+
* On match, returns a non-zero value with smaller values returned for more
83+
* specific compatible values.
8184
*/
8285
int of_fdt_is_compatible(struct boot_param_header *blob,
8386
unsigned long node, const char *compat)
8487
{
8588
const char *cp;
86-
unsigned long cplen, l;
89+
unsigned long cplen, l, score = 0;
8790

8891
cp = of_fdt_get_property(blob, node, "compatible", &cplen);
8992
if (cp == NULL)
9093
return 0;
9194
while (cplen > 0) {
95+
score++;
9296
if (of_compat_cmp(cp, compat, strlen(compat)) == 0)
93-
return 1;
97+
return score;
9498
l = strlen(cp) + 1;
9599
cp += l;
96100
cplen -= l;
@@ -99,6 +103,27 @@ int of_fdt_is_compatible(struct boot_param_header *blob,
99103
return 0;
100104
}
101105

106+
/**
107+
* of_fdt_match - Return true if node matches a list of compatible values
108+
*/
109+
int of_fdt_match(struct boot_param_header *blob, unsigned long node,
110+
const char **compat)
111+
{
112+
unsigned int tmp, score = 0;
113+
114+
if (!compat)
115+
return 0;
116+
117+
while (*compat) {
118+
tmp = of_fdt_is_compatible(blob, node, *compat);
119+
if (tmp && (score == 0 || (tmp < score)))
120+
score = tmp;
121+
compat++;
122+
}
123+
124+
return score;
125+
}
126+
102127
static void *unflatten_dt_alloc(unsigned long *mem, unsigned long size,
103128
unsigned long align)
104129
{
@@ -511,6 +536,14 @@ int __init of_flat_dt_is_compatible(unsigned long node, const char *compat)
511536
return of_fdt_is_compatible(initial_boot_params, node, compat);
512537
}
513538

539+
/**
540+
* of_flat_dt_match - Return true if node matches a list of compatible values
541+
*/
542+
int __init of_flat_dt_match(unsigned long node, const char **compat)
543+
{
544+
return of_fdt_match(initial_boot_params, node, compat);
545+
}
546+
514547
#ifdef CONFIG_BLK_DEV_INITRD
515548
/**
516549
* early_init_dt_check_for_initrd - Decode initrd location from flat tree

include/linux/of_fdt.h

+3
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ extern void *of_fdt_get_property(struct boot_param_header *blob,
6868
extern int of_fdt_is_compatible(struct boot_param_header *blob,
6969
unsigned long node,
7070
const char *compat);
71+
extern int of_fdt_match(struct boot_param_header *blob, unsigned long node,
72+
const char **compat);
7173
extern void of_fdt_unflatten_tree(unsigned long *blob,
7274
struct device_node **mynodes);
7375

@@ -84,6 +86,7 @@ extern int of_scan_flat_dt(int (*it)(unsigned long node, const char *uname,
8486
extern void *of_get_flat_dt_prop(unsigned long node, const char *name,
8587
unsigned long *size);
8688
extern int of_flat_dt_is_compatible(unsigned long node, const char *name);
89+
extern int of_flat_dt_match(unsigned long node, const char **matches);
8790
extern unsigned long of_get_flat_dt_root(void);
8891

8992
extern int early_init_dt_scan_chosen(unsigned long node, const char *uname,

0 commit comments

Comments
 (0)