1 // SPDX-License-Identifier: ISC
2 /*
3  * Copyright (C) 2016 Felix Fietkau <nbd@nbd.name>
4  */
5 #include <linux/of.h>
6 #include <linux/of_net.h>
7 #include <linux/mtd/mtd.h>
8 #include <linux/mtd/partitions.h>
9 #include <linux/etherdevice.h>
10 #include "mt76.h"
11 
mt76_get_of_eeprom(struct mt76_dev * dev,void * eep,int offset,int len)12 int mt76_get_of_eeprom(struct mt76_dev *dev, void *eep, int offset, int len)
13 {
14 #if defined(CONFIG_OF) && defined(CONFIG_MTD)
15 	struct device_node *np = dev->dev->of_node;
16 	struct mtd_info *mtd;
17 	const __be32 *list;
18 	const void *data;
19 	const char *part;
20 	phandle phandle;
21 	int size;
22 	size_t retlen;
23 	int ret;
24 
25 	if (!np)
26 		return -ENOENT;
27 
28 	data = of_get_property(np, "mediatek,eeprom-data", &size);
29 	if (data) {
30 		if (size > len)
31 			return -EINVAL;
32 
33 		memcpy(eep, data, size);
34 
35 		return 0;
36 	}
37 
38 	list = of_get_property(np, "mediatek,mtd-eeprom", &size);
39 	if (!list)
40 		return -ENOENT;
41 
42 	phandle = be32_to_cpup(list++);
43 	if (!phandle)
44 		return -ENOENT;
45 
46 	np = of_find_node_by_phandle(phandle);
47 	if (!np)
48 		return -EINVAL;
49 
50 	part = of_get_property(np, "label", NULL);
51 	if (!part)
52 		part = np->name;
53 
54 	mtd = get_mtd_device_nm(part);
55 	if (IS_ERR(mtd)) {
56 		ret =  PTR_ERR(mtd);
57 		goto out_put_node;
58 	}
59 
60 	if (size <= sizeof(*list)) {
61 		ret = -EINVAL;
62 		goto out_put_node;
63 	}
64 
65 	offset = be32_to_cpup(list);
66 	ret = mtd_read(mtd, offset, len, &retlen, eep);
67 	put_mtd_device(mtd);
68 	if (mtd_is_bitflip(ret))
69 		ret = 0;
70 	if (ret) {
71 		dev_err(dev->dev, "reading EEPROM from mtd %s failed: %i\n",
72 			part, ret);
73 		goto out_put_node;
74 	}
75 
76 	if (retlen < len) {
77 		ret = -EINVAL;
78 		goto out_put_node;
79 	}
80 
81 	if (of_property_read_bool(dev->dev->of_node, "big-endian")) {
82 		u8 *data = (u8 *)eep;
83 		int i;
84 
85 		/* convert eeprom data in Little Endian */
86 		for (i = 0; i < round_down(len, 2); i += 2)
87 			put_unaligned_le16(get_unaligned_be16(&data[i]),
88 					   &data[i]);
89 	}
90 
91 #ifdef CONFIG_NL80211_TESTMODE
92 	dev->test_mtd.name = devm_kstrdup(dev->dev, part, GFP_KERNEL);
93 	dev->test_mtd.offset = offset;
94 #endif
95 
96 out_put_node:
97 	of_node_put(np);
98 	return ret;
99 #else
100 	return -ENOENT;
101 #endif
102 }
103 EXPORT_SYMBOL_GPL(mt76_get_of_eeprom);
104 
105 void
mt76_eeprom_override(struct mt76_phy * phy)106 mt76_eeprom_override(struct mt76_phy *phy)
107 {
108 	struct mt76_dev *dev = phy->dev;
109 	struct device_node *np = dev->dev->of_node;
110 
111 	of_get_mac_address(np, phy->macaddr);
112 
113 	if (!is_valid_ether_addr(phy->macaddr)) {
114 		eth_random_addr(phy->macaddr);
115 		dev_info(dev->dev,
116 			 "Invalid MAC address, using random address %pM\n",
117 			 phy->macaddr);
118 	}
119 }
120 EXPORT_SYMBOL_GPL(mt76_eeprom_override);
121 
mt76_string_prop_find(struct property * prop,const char * str)122 static bool mt76_string_prop_find(struct property *prop, const char *str)
123 {
124 	const char *cp = NULL;
125 
126 	if (!prop || !str || !str[0])
127 		return false;
128 
129 	while ((cp = of_prop_next_string(prop, cp)) != NULL)
130 		if (!strcasecmp(cp, str))
131 			return true;
132 
133 	return false;
134 }
135 
136 static struct device_node *
mt76_find_power_limits_node(struct mt76_dev * dev)137 mt76_find_power_limits_node(struct mt76_dev *dev)
138 {
139 	struct device_node *np = dev->dev->of_node;
140 	const char *const region_names[] = {
141 		[NL80211_DFS_ETSI] = "etsi",
142 		[NL80211_DFS_FCC] = "fcc",
143 		[NL80211_DFS_JP] = "jp",
144 	};
145 	struct device_node *cur, *fallback = NULL;
146 	const char *region_name = NULL;
147 
148 	if (dev->region < ARRAY_SIZE(region_names))
149 		region_name = region_names[dev->region];
150 
151 	np = of_get_child_by_name(np, "power-limits");
152 	if (!np)
153 		return NULL;
154 
155 	for_each_child_of_node(np, cur) {
156 		struct property *country = of_find_property(cur, "country", NULL);
157 		struct property *regd = of_find_property(cur, "regdomain", NULL);
158 
159 		if (!country && !regd) {
160 			fallback = cur;
161 			continue;
162 		}
163 
164 		if (mt76_string_prop_find(country, dev->alpha2) ||
165 		    mt76_string_prop_find(regd, region_name)) {
166 			of_node_put(np);
167 			return cur;
168 		}
169 	}
170 
171 	of_node_put(np);
172 	return fallback;
173 }
174 
175 static const __be32 *
mt76_get_of_array(struct device_node * np,char * name,size_t * len,int min)176 mt76_get_of_array(struct device_node *np, char *name, size_t *len, int min)
177 {
178 	struct property *prop = of_find_property(np, name, NULL);
179 
180 	if (!prop || !prop->value || prop->length < min * 4)
181 		return NULL;
182 
183 	*len = prop->length;
184 
185 	return prop->value;
186 }
187 
188 static struct device_node *
mt76_find_channel_node(struct device_node * np,struct ieee80211_channel * chan)189 mt76_find_channel_node(struct device_node *np, struct ieee80211_channel *chan)
190 {
191 	struct device_node *cur;
192 	const __be32 *val;
193 	size_t len;
194 
195 	for_each_child_of_node(np, cur) {
196 		val = mt76_get_of_array(cur, "channels", &len, 2);
197 		if (!val)
198 			continue;
199 
200 		while (len >= 2 * sizeof(*val)) {
201 			if (chan->hw_value >= be32_to_cpu(val[0]) &&
202 			    chan->hw_value <= be32_to_cpu(val[1]))
203 				return cur;
204 
205 			val += 2;
206 			len -= 2 * sizeof(*val);
207 		}
208 	}
209 
210 	return NULL;
211 }
212 
213 static s8
mt76_get_txs_delta(struct device_node * np,u8 nss)214 mt76_get_txs_delta(struct device_node *np, u8 nss)
215 {
216 	const __be32 *val;
217 	size_t len;
218 
219 	val = mt76_get_of_array(np, "txs-delta", &len, nss);
220 	if (!val)
221 		return 0;
222 
223 	return be32_to_cpu(val[nss - 1]);
224 }
225 
226 static void
mt76_apply_array_limit(s8 * pwr,size_t pwr_len,const __be32 * data,s8 target_power,s8 nss_delta,s8 * max_power)227 mt76_apply_array_limit(s8 *pwr, size_t pwr_len, const __be32 *data,
228 		       s8 target_power, s8 nss_delta, s8 *max_power)
229 {
230 	int i;
231 
232 	if (!data)
233 		return;
234 
235 	for (i = 0; i < pwr_len; i++) {
236 		pwr[i] = min_t(s8, target_power,
237 			       be32_to_cpu(data[i]) + nss_delta);
238 		*max_power = max(*max_power, pwr[i]);
239 	}
240 }
241 
242 static void
mt76_apply_multi_array_limit(s8 * pwr,size_t pwr_len,s8 pwr_num,const __be32 * data,size_t len,s8 target_power,s8 nss_delta,s8 * max_power)243 mt76_apply_multi_array_limit(s8 *pwr, size_t pwr_len, s8 pwr_num,
244 			     const __be32 *data, size_t len, s8 target_power,
245 			     s8 nss_delta, s8 *max_power)
246 {
247 	int i, cur;
248 
249 	if (!data)
250 		return;
251 
252 	len /= 4;
253 	cur = be32_to_cpu(data[0]);
254 	for (i = 0; i < pwr_num; i++) {
255 		if (len < pwr_len + 1)
256 			break;
257 
258 		mt76_apply_array_limit(pwr + pwr_len * i, pwr_len, data + 1,
259 				       target_power, nss_delta, max_power);
260 		if (--cur > 0)
261 			continue;
262 
263 		data += pwr_len + 1;
264 		len -= pwr_len + 1;
265 		if (!len)
266 			break;
267 
268 		cur = be32_to_cpu(data[0]);
269 	}
270 }
271 
mt76_get_rate_power_limits(struct mt76_phy * phy,struct ieee80211_channel * chan,struct mt76_power_limits * dest,s8 target_power)272 s8 mt76_get_rate_power_limits(struct mt76_phy *phy,
273 			      struct ieee80211_channel *chan,
274 			      struct mt76_power_limits *dest,
275 			      s8 target_power)
276 {
277 	struct mt76_dev *dev = phy->dev;
278 	struct device_node *np;
279 	const __be32 *val;
280 	char name[16];
281 	u32 mcs_rates = dev->drv->mcs_rates;
282 	u32 ru_rates = ARRAY_SIZE(dest->ru[0]);
283 	char band;
284 	size_t len;
285 	s8 max_power = 0;
286 	s8 txs_delta;
287 
288 	if (!mcs_rates)
289 		mcs_rates = 10;
290 
291 	memset(dest, target_power, sizeof(*dest));
292 
293 	if (!IS_ENABLED(CONFIG_OF))
294 		return target_power;
295 
296 	np = mt76_find_power_limits_node(dev);
297 	if (!np)
298 		return target_power;
299 
300 	switch (chan->band) {
301 	case NL80211_BAND_2GHZ:
302 		band = '2';
303 		break;
304 	case NL80211_BAND_5GHZ:
305 		band = '5';
306 		break;
307 	case NL80211_BAND_6GHZ:
308 		band = '6';
309 		break;
310 	default:
311 		return target_power;
312 	}
313 
314 	snprintf(name, sizeof(name), "txpower-%cg", band);
315 	np = of_get_child_by_name(np, name);
316 	if (!np)
317 		return target_power;
318 
319 	np = mt76_find_channel_node(np, chan);
320 	if (!np)
321 		return target_power;
322 
323 	txs_delta = mt76_get_txs_delta(np, hweight8(phy->antenna_mask));
324 
325 	val = mt76_get_of_array(np, "rates-cck", &len, ARRAY_SIZE(dest->cck));
326 	mt76_apply_array_limit(dest->cck, ARRAY_SIZE(dest->cck), val,
327 			       target_power, txs_delta, &max_power);
328 
329 	val = mt76_get_of_array(np, "rates-ofdm",
330 				&len, ARRAY_SIZE(dest->ofdm));
331 	mt76_apply_array_limit(dest->ofdm, ARRAY_SIZE(dest->ofdm), val,
332 			       target_power, txs_delta, &max_power);
333 
334 	val = mt76_get_of_array(np, "rates-mcs", &len, mcs_rates + 1);
335 	mt76_apply_multi_array_limit(dest->mcs[0], ARRAY_SIZE(dest->mcs[0]),
336 				     ARRAY_SIZE(dest->mcs), val, len,
337 				     target_power, txs_delta, &max_power);
338 
339 	val = mt76_get_of_array(np, "rates-ru", &len, ru_rates + 1);
340 	mt76_apply_multi_array_limit(dest->ru[0], ARRAY_SIZE(dest->ru[0]),
341 				     ARRAY_SIZE(dest->ru), val, len,
342 				     target_power, txs_delta, &max_power);
343 
344 	return max_power;
345 }
346 EXPORT_SYMBOL_GPL(mt76_get_rate_power_limits);
347 
348 int
mt76_eeprom_init(struct mt76_dev * dev,int len)349 mt76_eeprom_init(struct mt76_dev *dev, int len)
350 {
351 	dev->eeprom.size = len;
352 	dev->eeprom.data = devm_kzalloc(dev->dev, len, GFP_KERNEL);
353 	if (!dev->eeprom.data)
354 		return -ENOMEM;
355 
356 	return !mt76_get_of_eeprom(dev, dev->eeprom.data, 0, len);
357 }
358 EXPORT_SYMBOL_GPL(mt76_eeprom_init);
359