eeprom.c 7.2 KB

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