1 // SPDX-License-Identifier: ISC
2 /*
3  * Copyright (c) 2014 Broadcom Corporation
4  */
5 #include <linux/init.h>
6 #include <linux/of.h>
7 #include <linux/of_irq.h>
8 #include <linux/of_net.h>
9 
10 #include <defs.h>
11 #include "debug.h"
12 #include "core.h"
13 #include "common.h"
14 #include "of.h"
15 
16 static int brcmf_of_get_country_codes(struct device *dev,
17 				      struct brcmf_mp_device *settings)
18 {
19 	struct device_node *np = dev->of_node;
20 	struct brcmfmac_pd_cc_entry *cce;
21 	struct brcmfmac_pd_cc *cc;
22 	int count;
23 	int i;
24 
25 	count = of_property_count_strings(np, "brcm,ccode-map");
26 	if (count < 0) {
27 		/* The property is optional, so return success if it doesn't
28 		 * exist. Otherwise propagate the error code.
29 		 */
30 		return (count == -EINVAL) ? 0 : count;
31 	}
32 
33 	cc = devm_kzalloc(dev, struct_size(cc, table, count), GFP_KERNEL);
34 	if (!cc)
35 		return -ENOMEM;
36 
37 	cc->table_size = count;
38 
39 	for (i = 0; i < count; i++) {
40 		const char *map;
41 
42 		cce = &cc->table[i];
43 
44 		if (of_property_read_string_index(np, "brcm,ccode-map",
45 						  i, &map))
46 			continue;
47 
48 		/* String format e.g. US-Q2-86 */
49 		if (sscanf(map, "%2c-%2c-%d", cce->iso3166, cce->cc,
50 			   &cce->rev) != 3)
51 			brcmf_err("failed to read country map %s\n", map);
52 		else
53 			brcmf_dbg(INFO, "%s-%s-%d\n", cce->iso3166, cce->cc,
54 				  cce->rev);
55 	}
56 
57 	settings->country_codes = cc;
58 
59 	return 0;
60 }
61 
62 void brcmf_of_probe(struct device *dev, enum brcmf_bus_type bus_type,
63 		    struct brcmf_mp_device *settings)
64 {
65 	struct brcmfmac_sdio_pd *sdio = &settings->bus.sdio;
66 	struct device_node *root, *np = dev->of_node;
67 	int irq;
68 	int err;
69 	u32 irqf;
70 	u32 val;
71 
72 	/* Set board-type to the first string of the machine compatible prop */
73 	root = of_find_node_by_path("/");
74 	if (root) {
75 		int i;
76 		char *board_type;
77 		const char *tmp;
78 
79 		of_property_read_string_index(root, "compatible", 0, &tmp);
80 
81 		/* get rid of '/' in the compatible string to be able to find the FW */
82 		board_type = devm_kstrdup(dev, tmp, GFP_KERNEL);
83 		if (!board_type) {
84 			of_node_put(root);
85 			return;
86 		}
87 		for (i = 0; i < board_type[i]; i++) {
88 			if (board_type[i] == '/')
89 				board_type[i] = '-';
90 		}
91 		settings->board_type = board_type;
92 
93 		of_node_put(root);
94 	}
95 
96 	if (!np || !of_device_is_compatible(np, "brcm,bcm4329-fmac"))
97 		return;
98 
99 	err = brcmf_of_get_country_codes(dev, settings);
100 	if (err)
101 		brcmf_err("failed to get OF country code map (err=%d)\n", err);
102 
103 	of_get_mac_address(np, settings->mac);
104 
105 	if (bus_type != BRCMF_BUSTYPE_SDIO)
106 		return;
107 
108 	if (of_property_read_u32(np, "brcm,drive-strength", &val) == 0)
109 		sdio->drive_strength = val;
110 
111 	/* make sure there are interrupts defined in the node */
112 	if (!of_find_property(np, "interrupts", NULL))
113 		return;
114 
115 	irq = irq_of_parse_and_map(np, 0);
116 	if (!irq) {
117 		brcmf_err("interrupt could not be mapped\n");
118 		return;
119 	}
120 	irqf = irqd_get_trigger_type(irq_get_irq_data(irq));
121 
122 	sdio->oob_irq_supported = true;
123 	sdio->oob_irq_nr = irq;
124 	sdio->oob_irq_flags = irqf;
125 }
126