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