xref: /freebsd/sys/contrib/dev/broadcom/brcm80211/brcmfmac/of.c (revision b4c3e9b5b09c829b4135aff738bd2893ed052377)
1*b4c3e9b5SBjoern A. Zeeb // SPDX-License-Identifier: ISC
2*b4c3e9b5SBjoern A. Zeeb /*
3*b4c3e9b5SBjoern A. Zeeb  * Copyright (c) 2014 Broadcom Corporation
4*b4c3e9b5SBjoern A. Zeeb  */
5*b4c3e9b5SBjoern A. Zeeb #include <linux/init.h>
6*b4c3e9b5SBjoern A. Zeeb #include <linux/of.h>
7*b4c3e9b5SBjoern A. Zeeb #include <linux/of_irq.h>
8*b4c3e9b5SBjoern A. Zeeb #include <linux/of_net.h>
9*b4c3e9b5SBjoern A. Zeeb #include <linux/clk.h>
10*b4c3e9b5SBjoern A. Zeeb 
11*b4c3e9b5SBjoern A. Zeeb #include <defs.h>
12*b4c3e9b5SBjoern A. Zeeb #include "debug.h"
13*b4c3e9b5SBjoern A. Zeeb #include "core.h"
14*b4c3e9b5SBjoern A. Zeeb #include "common.h"
15*b4c3e9b5SBjoern A. Zeeb #include "of.h"
16*b4c3e9b5SBjoern A. Zeeb 
brcmf_of_get_country_codes(struct device * dev,struct brcmf_mp_device * settings)17*b4c3e9b5SBjoern A. Zeeb static int brcmf_of_get_country_codes(struct device *dev,
18*b4c3e9b5SBjoern A. Zeeb 				      struct brcmf_mp_device *settings)
19*b4c3e9b5SBjoern A. Zeeb {
20*b4c3e9b5SBjoern A. Zeeb 	struct device_node *np = dev->of_node;
21*b4c3e9b5SBjoern A. Zeeb 	struct brcmfmac_pd_cc_entry *cce;
22*b4c3e9b5SBjoern A. Zeeb 	struct brcmfmac_pd_cc *cc;
23*b4c3e9b5SBjoern A. Zeeb 	int count;
24*b4c3e9b5SBjoern A. Zeeb 	int i;
25*b4c3e9b5SBjoern A. Zeeb 
26*b4c3e9b5SBjoern A. Zeeb 	count = of_property_count_strings(np, "brcm,ccode-map");
27*b4c3e9b5SBjoern A. Zeeb 	if (count < 0) {
28*b4c3e9b5SBjoern A. Zeeb 		/* If no explicit country code map is specified, check whether
29*b4c3e9b5SBjoern A. Zeeb 		 * the trivial map should be used.
30*b4c3e9b5SBjoern A. Zeeb 		 */
31*b4c3e9b5SBjoern A. Zeeb 		settings->trivial_ccode_map =
32*b4c3e9b5SBjoern A. Zeeb 			of_property_read_bool(np, "brcm,ccode-map-trivial");
33*b4c3e9b5SBjoern A. Zeeb 
34*b4c3e9b5SBjoern A. Zeeb 		/* The property is optional, so return success if it doesn't
35*b4c3e9b5SBjoern A. Zeeb 		 * exist. Otherwise propagate the error code.
36*b4c3e9b5SBjoern A. Zeeb 		 */
37*b4c3e9b5SBjoern A. Zeeb 		return (count == -EINVAL) ? 0 : count;
38*b4c3e9b5SBjoern A. Zeeb 	}
39*b4c3e9b5SBjoern A. Zeeb 
40*b4c3e9b5SBjoern A. Zeeb 	cc = devm_kzalloc(dev, struct_size(cc, table, count), GFP_KERNEL);
41*b4c3e9b5SBjoern A. Zeeb 	if (!cc)
42*b4c3e9b5SBjoern A. Zeeb 		return -ENOMEM;
43*b4c3e9b5SBjoern A. Zeeb 
44*b4c3e9b5SBjoern A. Zeeb 	cc->table_size = count;
45*b4c3e9b5SBjoern A. Zeeb 
46*b4c3e9b5SBjoern A. Zeeb 	for (i = 0; i < count; i++) {
47*b4c3e9b5SBjoern A. Zeeb 		const char *map;
48*b4c3e9b5SBjoern A. Zeeb 
49*b4c3e9b5SBjoern A. Zeeb 		cce = &cc->table[i];
50*b4c3e9b5SBjoern A. Zeeb 
51*b4c3e9b5SBjoern A. Zeeb 		if (of_property_read_string_index(np, "brcm,ccode-map",
52*b4c3e9b5SBjoern A. Zeeb 						  i, &map))
53*b4c3e9b5SBjoern A. Zeeb 			continue;
54*b4c3e9b5SBjoern A. Zeeb 
55*b4c3e9b5SBjoern A. Zeeb 		/* String format e.g. US-Q2-86 */
56*b4c3e9b5SBjoern A. Zeeb 		if (sscanf(map, "%2c-%2c-%d", cce->iso3166, cce->cc,
57*b4c3e9b5SBjoern A. Zeeb 			   &cce->rev) != 3)
58*b4c3e9b5SBjoern A. Zeeb 			brcmf_err("failed to read country map %s\n", map);
59*b4c3e9b5SBjoern A. Zeeb 		else
60*b4c3e9b5SBjoern A. Zeeb 			brcmf_dbg(INFO, "%s-%s-%d\n", cce->iso3166, cce->cc,
61*b4c3e9b5SBjoern A. Zeeb 				  cce->rev);
62*b4c3e9b5SBjoern A. Zeeb 	}
63*b4c3e9b5SBjoern A. Zeeb 
64*b4c3e9b5SBjoern A. Zeeb 	settings->country_codes = cc;
65*b4c3e9b5SBjoern A. Zeeb 
66*b4c3e9b5SBjoern A. Zeeb 	return 0;
67*b4c3e9b5SBjoern A. Zeeb }
68*b4c3e9b5SBjoern A. Zeeb 
brcmf_of_probe(struct device * dev,enum brcmf_bus_type bus_type,struct brcmf_mp_device * settings)69*b4c3e9b5SBjoern A. Zeeb int brcmf_of_probe(struct device *dev, enum brcmf_bus_type bus_type,
70*b4c3e9b5SBjoern A. Zeeb 		   struct brcmf_mp_device *settings)
71*b4c3e9b5SBjoern A. Zeeb {
72*b4c3e9b5SBjoern A. Zeeb 	struct brcmfmac_sdio_pd *sdio = &settings->bus.sdio;
73*b4c3e9b5SBjoern A. Zeeb 	struct device_node *root, *np = dev->of_node;
74*b4c3e9b5SBjoern A. Zeeb 	struct of_phandle_args oirq;
75*b4c3e9b5SBjoern A. Zeeb 	struct clk *clk;
76*b4c3e9b5SBjoern A. Zeeb 	const char *prop;
77*b4c3e9b5SBjoern A. Zeeb 	int irq;
78*b4c3e9b5SBjoern A. Zeeb 	int err;
79*b4c3e9b5SBjoern A. Zeeb 	u32 irqf;
80*b4c3e9b5SBjoern A. Zeeb 	u32 val;
81*b4c3e9b5SBjoern A. Zeeb 
82*b4c3e9b5SBjoern A. Zeeb 	/* Apple ARM64 platforms have their own idea of board type, passed in
83*b4c3e9b5SBjoern A. Zeeb 	 * via the device tree. They also have an antenna SKU parameter
84*b4c3e9b5SBjoern A. Zeeb 	 */
85*b4c3e9b5SBjoern A. Zeeb 	err = of_property_read_string(np, "brcm,board-type", &prop);
86*b4c3e9b5SBjoern A. Zeeb 	if (!err)
87*b4c3e9b5SBjoern A. Zeeb 		settings->board_type = prop;
88*b4c3e9b5SBjoern A. Zeeb 
89*b4c3e9b5SBjoern A. Zeeb 	if (!of_property_read_string(np, "apple,antenna-sku", &prop))
90*b4c3e9b5SBjoern A. Zeeb 		settings->antenna_sku = prop;
91*b4c3e9b5SBjoern A. Zeeb 
92*b4c3e9b5SBjoern A. Zeeb 	/* The WLAN calibration blob is normally stored in SROM, but Apple
93*b4c3e9b5SBjoern A. Zeeb 	 * ARM64 platforms pass it via the DT instead.
94*b4c3e9b5SBjoern A. Zeeb 	 */
95*b4c3e9b5SBjoern A. Zeeb 	prop = of_get_property(np, "brcm,cal-blob", &settings->cal_size);
96*b4c3e9b5SBjoern A. Zeeb 	if (prop && settings->cal_size)
97*b4c3e9b5SBjoern A. Zeeb 		settings->cal_blob = prop;
98*b4c3e9b5SBjoern A. Zeeb 
99*b4c3e9b5SBjoern A. Zeeb 	/* Set board-type to the first string of the machine compatible prop */
100*b4c3e9b5SBjoern A. Zeeb 	root = of_find_node_by_path("/");
101*b4c3e9b5SBjoern A. Zeeb 	if (root && err) {
102*b4c3e9b5SBjoern A. Zeeb 		char *board_type = NULL;
103*b4c3e9b5SBjoern A. Zeeb 		const char *tmp;
104*b4c3e9b5SBjoern A. Zeeb 
105*b4c3e9b5SBjoern A. Zeeb 		/* get rid of '/' in the compatible string to be able to find the FW */
106*b4c3e9b5SBjoern A. Zeeb 		if (!of_property_read_string_index(root, "compatible", 0, &tmp))
107*b4c3e9b5SBjoern A. Zeeb 			board_type = devm_kstrdup(dev, tmp, GFP_KERNEL);
108*b4c3e9b5SBjoern A. Zeeb 
109*b4c3e9b5SBjoern A. Zeeb 		if (!board_type) {
110*b4c3e9b5SBjoern A. Zeeb 			of_node_put(root);
111*b4c3e9b5SBjoern A. Zeeb 			return 0;
112*b4c3e9b5SBjoern A. Zeeb 		}
113*b4c3e9b5SBjoern A. Zeeb 		strreplace(board_type, '/', '-');
114*b4c3e9b5SBjoern A. Zeeb 		settings->board_type = board_type;
115*b4c3e9b5SBjoern A. Zeeb 	}
116*b4c3e9b5SBjoern A. Zeeb 	of_node_put(root);
117*b4c3e9b5SBjoern A. Zeeb 
118*b4c3e9b5SBjoern A. Zeeb 	clk = devm_clk_get_optional_enabled_with_rate(dev, "lpo", 32768);
119*b4c3e9b5SBjoern A. Zeeb 	if (IS_ERR(clk))
120*b4c3e9b5SBjoern A. Zeeb 		return PTR_ERR(clk);
121*b4c3e9b5SBjoern A. Zeeb 
122*b4c3e9b5SBjoern A. Zeeb 	brcmf_dbg(INFO, "%s LPO clock\n", clk ? "enable" : "no");
123*b4c3e9b5SBjoern A. Zeeb 
124*b4c3e9b5SBjoern A. Zeeb 	if (!np || !of_device_is_compatible(np, "brcm,bcm4329-fmac"))
125*b4c3e9b5SBjoern A. Zeeb 		return 0;
126*b4c3e9b5SBjoern A. Zeeb 
127*b4c3e9b5SBjoern A. Zeeb 	err = brcmf_of_get_country_codes(dev, settings);
128*b4c3e9b5SBjoern A. Zeeb 	if (err)
129*b4c3e9b5SBjoern A. Zeeb 		brcmf_err("failed to get OF country code map (err=%d)\n", err);
130*b4c3e9b5SBjoern A. Zeeb 
131*b4c3e9b5SBjoern A. Zeeb 	of_get_mac_address(np, settings->mac);
132*b4c3e9b5SBjoern A. Zeeb 
133*b4c3e9b5SBjoern A. Zeeb 	if (bus_type != BRCMF_BUSTYPE_SDIO)
134*b4c3e9b5SBjoern A. Zeeb 		return 0;
135*b4c3e9b5SBjoern A. Zeeb 
136*b4c3e9b5SBjoern A. Zeeb 	if (of_property_read_u32(np, "brcm,drive-strength", &val) == 0)
137*b4c3e9b5SBjoern A. Zeeb 		sdio->drive_strength = val;
138*b4c3e9b5SBjoern A. Zeeb 
139*b4c3e9b5SBjoern A. Zeeb 	/* make sure there are interrupts defined in the node */
140*b4c3e9b5SBjoern A. Zeeb 	if (of_irq_parse_one(np, 0, &oirq))
141*b4c3e9b5SBjoern A. Zeeb 		return 0;
142*b4c3e9b5SBjoern A. Zeeb 
143*b4c3e9b5SBjoern A. Zeeb 	irq = irq_create_of_mapping(&oirq);
144*b4c3e9b5SBjoern A. Zeeb 	if (!irq) {
145*b4c3e9b5SBjoern A. Zeeb 		brcmf_err("interrupt could not be mapped\n");
146*b4c3e9b5SBjoern A. Zeeb 		return 0;
147*b4c3e9b5SBjoern A. Zeeb 	}
148*b4c3e9b5SBjoern A. Zeeb 	irqf = irq_get_trigger_type(irq);
149*b4c3e9b5SBjoern A. Zeeb 
150*b4c3e9b5SBjoern A. Zeeb 	sdio->oob_irq_supported = true;
151*b4c3e9b5SBjoern A. Zeeb 	sdio->oob_irq_nr = irq;
152*b4c3e9b5SBjoern A. Zeeb 	sdio->oob_irq_flags = irqf;
153*b4c3e9b5SBjoern A. Zeeb 
154*b4c3e9b5SBjoern A. Zeeb 	return 0;
155*b4c3e9b5SBjoern A. Zeeb }
156