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