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