xref: /openbmc/linux/drivers/net/wireless/broadcom/brcm80211/brcmfmac/of.c (revision 360823a09426347ea8f232b0b0b5156d0aed0302)
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 
brcmf_of_get_country_codes(struct device * dev,struct brcmf_mp_device * settings)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 		/* If no explicit country code map is specified, check whether
28 		 * the trivial map should be used.
29 		 */
30 		settings->trivial_ccode_map =
31 			of_property_read_bool(np, "brcm,ccode-map-trivial");
32 
33 		/* The property is optional, so return success if it doesn't
34 		 * exist. Otherwise propagate the error code.
35 		 */
36 		return (count == -EINVAL) ? 0 : count;
37 	}
38 
39 	cc = devm_kzalloc(dev, struct_size(cc, table, count), GFP_KERNEL);
40 	if (!cc)
41 		return -ENOMEM;
42 
43 	cc->table_size = count;
44 
45 	for (i = 0; i < count; i++) {
46 		const char *map;
47 
48 		cce = &cc->table[i];
49 
50 		if (of_property_read_string_index(np, "brcm,ccode-map",
51 						  i, &map))
52 			continue;
53 
54 		/* String format e.g. US-Q2-86 */
55 		if (sscanf(map, "%2c-%2c-%d", cce->iso3166, cce->cc,
56 			   &cce->rev) != 3)
57 			brcmf_err("failed to read country map %s\n", map);
58 		else
59 			brcmf_dbg(INFO, "%s-%s-%d\n", cce->iso3166, cce->cc,
60 				  cce->rev);
61 	}
62 
63 	settings->country_codes = cc;
64 
65 	return 0;
66 }
67 
brcmf_of_probe(struct device * dev,enum brcmf_bus_type bus_type,struct brcmf_mp_device * settings)68 void brcmf_of_probe(struct device *dev, enum brcmf_bus_type bus_type,
69 		    struct brcmf_mp_device *settings)
70 {
71 	struct brcmfmac_sdio_pd *sdio = &settings->bus.sdio;
72 	struct device_node *root, *np = dev->of_node;
73 	const char *prop;
74 	int irq;
75 	int err;
76 	u32 irqf;
77 	u32 val;
78 
79 	/* Apple ARM64 platforms have their own idea of board type, passed in
80 	 * via the device tree. They also have an antenna SKU parameter
81 	 */
82 	err = of_property_read_string(np, "brcm,board-type", &prop);
83 	if (!err)
84 		settings->board_type = prop;
85 
86 	if (!of_property_read_string(np, "apple,antenna-sku", &prop))
87 		settings->antenna_sku = prop;
88 
89 	/* The WLAN calibration blob is normally stored in SROM, but Apple
90 	 * ARM64 platforms pass it via the DT instead.
91 	 */
92 	prop = of_get_property(np, "brcm,cal-blob", &settings->cal_size);
93 	if (prop && settings->cal_size)
94 		settings->cal_blob = prop;
95 
96 	/* Set board-type to the first string of the machine compatible prop */
97 	root = of_find_node_by_path("/");
98 	if (root && err) {
99 		char *board_type = NULL;
100 		const char *tmp;
101 
102 		/* get rid of '/' in the compatible string to be able to find the FW */
103 		if (!of_property_read_string_index(root, "compatible", 0, &tmp))
104 			board_type = devm_kstrdup(dev, tmp, GFP_KERNEL);
105 
106 		if (!board_type) {
107 			of_node_put(root);
108 			return;
109 		}
110 		strreplace(board_type, '/', '-');
111 		settings->board_type = board_type;
112 	}
113 	of_node_put(root);
114 
115 	if (!np || !of_device_is_compatible(np, "brcm,bcm4329-fmac"))
116 		return;
117 
118 	err = brcmf_of_get_country_codes(dev, settings);
119 	if (err)
120 		brcmf_err("failed to get OF country code map (err=%d)\n", err);
121 
122 	of_get_mac_address(np, settings->mac);
123 
124 	if (bus_type != BRCMF_BUSTYPE_SDIO)
125 		return;
126 
127 	if (of_property_read_u32(np, "brcm,drive-strength", &val) == 0)
128 		sdio->drive_strength = val;
129 
130 	/* make sure there are interrupts defined in the node */
131 	if (!of_property_present(np, "interrupts"))
132 		return;
133 
134 	irq = irq_of_parse_and_map(np, 0);
135 	if (!irq) {
136 		brcmf_err("interrupt could not be mapped\n");
137 		return;
138 	}
139 	irqf = irqd_get_trigger_type(irq_get_irq_data(irq));
140 
141 	sdio->oob_irq_supported = true;
142 	sdio->oob_irq_nr = irq;
143 	sdio->oob_irq_flags = irqf;
144 }
145