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