phy.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * (c) Copyright 2002-2010, Ralink Technology, Inc.
  4. * Copyright (C) 2014 Felix Fietkau <nbd@openwrt.org>
  5. * Copyright (C) 2015 Jakub Kicinski <kubakici@wp.pl>
  6. */
  7. #include "mt7601u.h"
  8. #include "mcu.h"
  9. #include "eeprom.h"
  10. #include "trace.h"
  11. #include "initvals_phy.h"
  12. #include <linux/etherdevice.h>
  13. static void mt7601u_agc_reset(struct mt7601u_dev *dev);
  14. static int
  15. mt7601u_rf_wr(struct mt7601u_dev *dev, u8 bank, u8 offset, u8 value)
  16. {
  17. int ret = 0;
  18. if (WARN_ON(!test_bit(MT7601U_STATE_WLAN_RUNNING, &dev->state)) ||
  19. WARN_ON(offset > 63))
  20. return -EINVAL;
  21. if (test_bit(MT7601U_STATE_REMOVED, &dev->state))
  22. return 0;
  23. mutex_lock(&dev->reg_atomic_mutex);
  24. if (!mt76_poll(dev, MT_RF_CSR_CFG, MT_RF_CSR_CFG_KICK, 0, 100)) {
  25. ret = -ETIMEDOUT;
  26. goto out;
  27. }
  28. mt7601u_wr(dev, MT_RF_CSR_CFG,
  29. FIELD_PREP(MT_RF_CSR_CFG_DATA, value) |
  30. FIELD_PREP(MT_RF_CSR_CFG_REG_BANK, bank) |
  31. FIELD_PREP(MT_RF_CSR_CFG_REG_ID, offset) |
  32. MT_RF_CSR_CFG_WR |
  33. MT_RF_CSR_CFG_KICK);
  34. trace_rf_write(dev, bank, offset, value);
  35. out:
  36. mutex_unlock(&dev->reg_atomic_mutex);
  37. if (ret < 0)
  38. dev_err(dev->dev, "Error: RF write %02hhx:%02hhx failed:%d!!\n",
  39. bank, offset, ret);
  40. return ret;
  41. }
  42. static int
  43. mt7601u_rf_rr(struct mt7601u_dev *dev, u8 bank, u8 offset)
  44. {
  45. int ret = -ETIMEDOUT;
  46. u32 val;
  47. if (WARN_ON(!test_bit(MT7601U_STATE_WLAN_RUNNING, &dev->state)) ||
  48. WARN_ON(offset > 63))
  49. return -EINVAL;
  50. if (test_bit(MT7601U_STATE_REMOVED, &dev->state))
  51. return 0xff;
  52. mutex_lock(&dev->reg_atomic_mutex);
  53. if (!mt76_poll(dev, MT_RF_CSR_CFG, MT_RF_CSR_CFG_KICK, 0, 100))
  54. goto out;
  55. mt7601u_wr(dev, MT_RF_CSR_CFG,
  56. FIELD_PREP(MT_RF_CSR_CFG_REG_BANK, bank) |
  57. FIELD_PREP(MT_RF_CSR_CFG_REG_ID, offset) |
  58. MT_RF_CSR_CFG_KICK);
  59. if (!mt76_poll(dev, MT_RF_CSR_CFG, MT_RF_CSR_CFG_KICK, 0, 100))
  60. goto out;
  61. val = mt7601u_rr(dev, MT_RF_CSR_CFG);
  62. if (FIELD_GET(MT_RF_CSR_CFG_REG_ID, val) == offset &&
  63. FIELD_GET(MT_RF_CSR_CFG_REG_BANK, val) == bank) {
  64. ret = FIELD_GET(MT_RF_CSR_CFG_DATA, val);
  65. trace_rf_read(dev, bank, offset, ret);
  66. }
  67. out:
  68. mutex_unlock(&dev->reg_atomic_mutex);
  69. if (ret < 0)
  70. dev_err(dev->dev, "Error: RF read %02hhx:%02hhx failed:%d!!\n",
  71. bank, offset, ret);
  72. return ret;
  73. }
  74. static int
  75. mt7601u_rf_rmw(struct mt7601u_dev *dev, u8 bank, u8 offset, u8 mask, u8 val)
  76. {
  77. int ret;
  78. ret = mt7601u_rf_rr(dev, bank, offset);
  79. if (ret < 0)
  80. return ret;
  81. val |= ret & ~mask;
  82. ret = mt7601u_rf_wr(dev, bank, offset, val);
  83. if (ret)
  84. return ret;
  85. return val;
  86. }
  87. static int
  88. mt7601u_rf_set(struct mt7601u_dev *dev, u8 bank, u8 offset, u8 val)
  89. {
  90. return mt7601u_rf_rmw(dev, bank, offset, 0, val);
  91. }
  92. static int
  93. mt7601u_rf_clear(struct mt7601u_dev *dev, u8 bank, u8 offset, u8 mask)
  94. {
  95. return mt7601u_rf_rmw(dev, bank, offset, mask, 0);
  96. }
  97. static void mt7601u_bbp_wr(struct mt7601u_dev *dev, u8 offset, u8 val)
  98. {
  99. if (WARN_ON(!test_bit(MT7601U_STATE_WLAN_RUNNING, &dev->state)) ||
  100. test_bit(MT7601U_STATE_REMOVED, &dev->state))
  101. return;
  102. mutex_lock(&dev->reg_atomic_mutex);
  103. if (!mt76_poll(dev, MT_BBP_CSR_CFG, MT_BBP_CSR_CFG_BUSY, 0, 1000)) {
  104. dev_err(dev->dev, "Error: BBP write %02hhx failed!!\n", offset);
  105. goto out;
  106. }
  107. mt7601u_wr(dev, MT_BBP_CSR_CFG,
  108. FIELD_PREP(MT_BBP_CSR_CFG_VAL, val) |
  109. FIELD_PREP(MT_BBP_CSR_CFG_REG_NUM, offset) |
  110. MT_BBP_CSR_CFG_RW_MODE | MT_BBP_CSR_CFG_BUSY);
  111. trace_bbp_write(dev, offset, val);
  112. out:
  113. mutex_unlock(&dev->reg_atomic_mutex);
  114. }
  115. static int mt7601u_bbp_rr(struct mt7601u_dev *dev, u8 offset)
  116. {
  117. u32 val;
  118. int ret = -ETIMEDOUT;
  119. if (WARN_ON(!test_bit(MT7601U_STATE_WLAN_RUNNING, &dev->state)))
  120. return -EINVAL;
  121. if (test_bit(MT7601U_STATE_REMOVED, &dev->state))
  122. return 0xff;
  123. mutex_lock(&dev->reg_atomic_mutex);
  124. if (!mt76_poll(dev, MT_BBP_CSR_CFG, MT_BBP_CSR_CFG_BUSY, 0, 1000))
  125. goto out;
  126. mt7601u_wr(dev, MT_BBP_CSR_CFG,
  127. FIELD_PREP(MT_BBP_CSR_CFG_REG_NUM, offset) |
  128. MT_BBP_CSR_CFG_RW_MODE | MT_BBP_CSR_CFG_BUSY |
  129. MT_BBP_CSR_CFG_READ);
  130. if (!mt76_poll(dev, MT_BBP_CSR_CFG, MT_BBP_CSR_CFG_BUSY, 0, 1000))
  131. goto out;
  132. val = mt7601u_rr(dev, MT_BBP_CSR_CFG);
  133. if (FIELD_GET(MT_BBP_CSR_CFG_REG_NUM, val) == offset) {
  134. ret = FIELD_GET(MT_BBP_CSR_CFG_VAL, val);
  135. trace_bbp_read(dev, offset, ret);
  136. }
  137. out:
  138. mutex_unlock(&dev->reg_atomic_mutex);
  139. if (ret < 0)
  140. dev_err(dev->dev, "Error: BBP read %02hhx failed:%d!!\n",
  141. offset, ret);
  142. return ret;
  143. }
  144. static int mt7601u_bbp_rmw(struct mt7601u_dev *dev, u8 offset, u8 mask, u8 val)
  145. {
  146. int ret;
  147. ret = mt7601u_bbp_rr(dev, offset);
  148. if (ret < 0)
  149. return ret;
  150. val |= ret & ~mask;
  151. mt7601u_bbp_wr(dev, offset, val);
  152. return val;
  153. }
  154. static u8 mt7601u_bbp_rmc(struct mt7601u_dev *dev, u8 offset, u8 mask, u8 val)
  155. {
  156. int ret;
  157. ret = mt7601u_bbp_rr(dev, offset);
  158. if (ret < 0)
  159. return ret;
  160. val |= ret & ~mask;
  161. if (ret != val)
  162. mt7601u_bbp_wr(dev, offset, val);
  163. return val;
  164. }
  165. int mt7601u_wait_bbp_ready(struct mt7601u_dev *dev)
  166. {
  167. int i = 20;
  168. u8 val;
  169. do {
  170. val = mt7601u_bbp_rr(dev, MT_BBP_REG_VERSION);
  171. if (val && val != 0xff)
  172. break;
  173. } while (--i);
  174. if (!i) {
  175. dev_err(dev->dev, "Error: BBP is not ready\n");
  176. return -EIO;
  177. }
  178. return 0;
  179. }
  180. u32 mt7601u_bbp_set_ctrlch(struct mt7601u_dev *dev, bool below)
  181. {
  182. return mt7601u_bbp_rmc(dev, 3, 0x20, below ? 0x20 : 0);
  183. }
  184. int mt7601u_phy_get_rssi(struct mt7601u_dev *dev,
  185. struct mt7601u_rxwi *rxwi, u16 rate)
  186. {
  187. static const s8 lna[2][2][3] = {
  188. /* main LNA */ {
  189. /* bw20 */ { -2, 15, 33 },
  190. /* bw40 */ { 0, 16, 34 }
  191. },
  192. /* aux LNA */ {
  193. /* bw20 */ { -2, 15, 33 },
  194. /* bw40 */ { -2, 16, 34 }
  195. }
  196. };
  197. int bw = FIELD_GET(MT_RXWI_RATE_BW, rate);
  198. int aux_lna = FIELD_GET(MT_RXWI_ANT_AUX_LNA, rxwi->ant);
  199. int lna_id = FIELD_GET(MT_RXWI_GAIN_RSSI_LNA_ID, rxwi->gain);
  200. int val;
  201. if (lna_id) /* LNA id can be 0, 2, 3. */
  202. lna_id--;
  203. val = 8;
  204. val -= lna[aux_lna][bw][lna_id];
  205. val -= FIELD_GET(MT_RXWI_GAIN_RSSI_VAL, rxwi->gain);
  206. val -= dev->ee->lna_gain;
  207. val -= dev->ee->rssi_offset[0];
  208. return val;
  209. }
  210. static void mt7601u_vco_cal(struct mt7601u_dev *dev)
  211. {
  212. mt7601u_rf_wr(dev, 0, 4, 0x0a);
  213. mt7601u_rf_wr(dev, 0, 5, 0x20);
  214. mt7601u_rf_set(dev, 0, 4, BIT(7));
  215. msleep(2);
  216. }
  217. static int mt7601u_set_bw_filter(struct mt7601u_dev *dev, bool cal)
  218. {
  219. u32 filter = 0;
  220. int ret;
  221. if (!cal)
  222. filter |= 0x10000;
  223. if (dev->bw != MT_BW_20)
  224. filter |= 0x00100;
  225. /* TX */
  226. ret = mt7601u_mcu_calibrate(dev, MCU_CAL_BW, filter | 1);
  227. if (ret)
  228. return ret;
  229. /* RX */
  230. return mt7601u_mcu_calibrate(dev, MCU_CAL_BW, filter);
  231. }
  232. static int mt7601u_load_bbp_temp_table_bw(struct mt7601u_dev *dev)
  233. {
  234. const struct reg_table *t;
  235. if (WARN_ON(dev->temp_mode > MT_TEMP_MODE_LOW))
  236. return -EINVAL;
  237. t = &bbp_mode_table[dev->temp_mode][dev->bw];
  238. return mt7601u_write_reg_pairs(dev, MT_MCU_MEMMAP_BBP, t->regs, t->n);
  239. }
  240. static int mt7601u_bbp_temp(struct mt7601u_dev *dev, int mode, const char *name)
  241. {
  242. const struct reg_table *t;
  243. int ret;
  244. if (dev->temp_mode == mode)
  245. return 0;
  246. dev->temp_mode = mode;
  247. trace_temp_mode(dev, mode);
  248. t = bbp_mode_table[dev->temp_mode];
  249. ret = mt7601u_write_reg_pairs(dev, MT_MCU_MEMMAP_BBP,
  250. t[2].regs, t[2].n);
  251. if (ret)
  252. return ret;
  253. return mt7601u_write_reg_pairs(dev, MT_MCU_MEMMAP_BBP,
  254. t[dev->bw].regs, t[dev->bw].n);
  255. }
  256. static void mt7601u_apply_ch14_fixup(struct mt7601u_dev *dev, int hw_chan)
  257. {
  258. struct mt7601u_rate_power *t = &dev->ee->power_rate_table;
  259. if (hw_chan != 14 || dev->bw != MT_BW_20) {
  260. mt7601u_bbp_rmw(dev, 4, 0x20, 0);
  261. mt7601u_bbp_wr(dev, 178, 0xff);
  262. t->cck[0].bw20 = dev->ee->real_cck_bw20[0];
  263. t->cck[1].bw20 = dev->ee->real_cck_bw20[1];
  264. } else { /* Apply CH14 OBW fixup */
  265. mt7601u_bbp_wr(dev, 4, 0x60);
  266. mt7601u_bbp_wr(dev, 178, 0);
  267. /* Note: vendor code is buggy here for negative values */
  268. t->cck[0].bw20 = dev->ee->real_cck_bw20[0] - 2;
  269. t->cck[1].bw20 = dev->ee->real_cck_bw20[1] - 2;
  270. }
  271. }
  272. static int __mt7601u_phy_set_channel(struct mt7601u_dev *dev,
  273. struct cfg80211_chan_def *chandef)
  274. {
  275. #define FREQ_PLAN_REGS 4
  276. static const u8 freq_plan[14][FREQ_PLAN_REGS] = {
  277. { 0x99, 0x99, 0x09, 0x50 },
  278. { 0x46, 0x44, 0x0a, 0x50 },
  279. { 0xec, 0xee, 0x0a, 0x50 },
  280. { 0x99, 0x99, 0x0b, 0x50 },
  281. { 0x46, 0x44, 0x08, 0x51 },
  282. { 0xec, 0xee, 0x08, 0x51 },
  283. { 0x99, 0x99, 0x09, 0x51 },
  284. { 0x46, 0x44, 0x0a, 0x51 },
  285. { 0xec, 0xee, 0x0a, 0x51 },
  286. { 0x99, 0x99, 0x0b, 0x51 },
  287. { 0x46, 0x44, 0x08, 0x52 },
  288. { 0xec, 0xee, 0x08, 0x52 },
  289. { 0x99, 0x99, 0x09, 0x52 },
  290. { 0x33, 0x33, 0x0b, 0x52 },
  291. };
  292. struct mt76_reg_pair channel_freq_plan[FREQ_PLAN_REGS] = {
  293. { 17, 0 }, { 18, 0 }, { 19, 0 }, { 20, 0 },
  294. };
  295. struct mt76_reg_pair bbp_settings[3] = {
  296. { 62, 0x37 - dev->ee->lna_gain },
  297. { 63, 0x37 - dev->ee->lna_gain },
  298. { 64, 0x37 - dev->ee->lna_gain },
  299. };
  300. struct ieee80211_channel *chan = chandef->chan;
  301. enum nl80211_channel_type chan_type =
  302. cfg80211_get_chandef_type(chandef);
  303. struct mt7601u_rate_power *t = &dev->ee->power_rate_table;
  304. int chan_idx;
  305. bool chan_ext_below;
  306. u8 bw;
  307. int i, ret;
  308. bw = MT_BW_20;
  309. chan_ext_below = (chan_type == NL80211_CHAN_HT40MINUS);
  310. chan_idx = chan->hw_value - 1;
  311. if (chandef->width == NL80211_CHAN_WIDTH_40) {
  312. bw = MT_BW_40;
  313. if (chan_idx > 1 && chan_type == NL80211_CHAN_HT40MINUS)
  314. chan_idx -= 2;
  315. else if (chan_idx < 12 && chan_type == NL80211_CHAN_HT40PLUS)
  316. chan_idx += 2;
  317. else
  318. dev_err(dev->dev, "Error: invalid 40MHz channel!!\n");
  319. }
  320. if (bw != dev->bw || chan_ext_below != dev->chan_ext_below) {
  321. dev_dbg(dev->dev, "Info: switching HT mode bw:%d below:%d\n",
  322. bw, chan_ext_below);
  323. mt7601u_bbp_set_bw(dev, bw);
  324. mt7601u_bbp_set_ctrlch(dev, chan_ext_below);
  325. mt7601u_mac_set_ctrlch(dev, chan_ext_below);
  326. dev->chan_ext_below = chan_ext_below;
  327. }
  328. for (i = 0; i < FREQ_PLAN_REGS; i++)
  329. channel_freq_plan[i].value = freq_plan[chan_idx][i];
  330. ret = mt7601u_write_reg_pairs(dev, MT_MCU_MEMMAP_RF,
  331. channel_freq_plan, FREQ_PLAN_REGS);
  332. if (ret)
  333. return ret;
  334. mt7601u_rmw(dev, MT_TX_ALC_CFG_0, 0x3f3f,
  335. dev->ee->chan_pwr[chan_idx] & 0x3f);
  336. ret = mt7601u_write_reg_pairs(dev, MT_MCU_MEMMAP_BBP,
  337. bbp_settings, ARRAY_SIZE(bbp_settings));
  338. if (ret)
  339. return ret;
  340. mt7601u_vco_cal(dev);
  341. mt7601u_bbp_set_bw(dev, bw);
  342. ret = mt7601u_set_bw_filter(dev, false);
  343. if (ret)
  344. return ret;
  345. mt7601u_apply_ch14_fixup(dev, chan->hw_value);
  346. mt7601u_wr(dev, MT_TX_PWR_CFG_0, int_to_s6(t->ofdm[1].bw20) << 24 |
  347. int_to_s6(t->ofdm[0].bw20) << 16 |
  348. int_to_s6(t->cck[1].bw20) << 8 |
  349. int_to_s6(t->cck[0].bw20));
  350. if (test_bit(MT7601U_STATE_SCANNING, &dev->state))
  351. mt7601u_agc_reset(dev);
  352. dev->chandef = *chandef;
  353. return 0;
  354. }
  355. int mt7601u_phy_set_channel(struct mt7601u_dev *dev,
  356. struct cfg80211_chan_def *chandef)
  357. {
  358. int ret;
  359. cancel_delayed_work_sync(&dev->cal_work);
  360. cancel_delayed_work_sync(&dev->freq_cal.work);
  361. mutex_lock(&dev->hw_atomic_mutex);
  362. ret = __mt7601u_phy_set_channel(dev, chandef);
  363. mutex_unlock(&dev->hw_atomic_mutex);
  364. if (ret)
  365. return ret;
  366. if (test_bit(MT7601U_STATE_SCANNING, &dev->state))
  367. return 0;
  368. ieee80211_queue_delayed_work(dev->hw, &dev->cal_work,
  369. MT_CALIBRATE_INTERVAL);
  370. if (dev->freq_cal.enabled)
  371. ieee80211_queue_delayed_work(dev->hw, &dev->freq_cal.work,
  372. MT_FREQ_CAL_INIT_DELAY);
  373. return 0;
  374. }
  375. #define BBP_R47_FLAG GENMASK(2, 0)
  376. #define BBP_R47_F_TSSI 0
  377. #define BBP_R47_F_PKT_T 1
  378. #define BBP_R47_F_TX_RATE 2
  379. #define BBP_R47_F_TEMP 4
  380. /**
  381. * mt7601u_bbp_r47_get - read value through BBP R47/R49 pair
  382. * @dev: pointer to adapter structure
  383. * @reg: value of BBP R47 before the operation
  384. * @flag: one of the BBP_R47_F_* flags
  385. *
  386. * Convenience helper for reading values through BBP R47/R49 pair.
  387. * Takes old value of BBP R47 as @reg, because callers usually have it
  388. * cached already.
  389. *
  390. * Return: value of BBP R49.
  391. */
  392. static u8 mt7601u_bbp_r47_get(struct mt7601u_dev *dev, u8 reg, u8 flag)
  393. {
  394. flag |= reg & ~BBP_R47_FLAG;
  395. mt7601u_bbp_wr(dev, 47, flag);
  396. usleep_range(500, 700);
  397. return mt7601u_bbp_rr(dev, 49);
  398. }
  399. static s8 mt7601u_read_bootup_temp(struct mt7601u_dev *dev)
  400. {
  401. u8 bbp_val, temp;
  402. u32 rf_bp, rf_set;
  403. int i;
  404. rf_set = mt7601u_rr(dev, MT_RF_SETTING_0);
  405. rf_bp = mt7601u_rr(dev, MT_RF_BYPASS_0);
  406. mt7601u_wr(dev, MT_RF_BYPASS_0, 0);
  407. mt7601u_wr(dev, MT_RF_SETTING_0, 0x00000010);
  408. mt7601u_wr(dev, MT_RF_BYPASS_0, 0x00000010);
  409. bbp_val = mt7601u_bbp_rmw(dev, 47, 0, 0x10);
  410. mt7601u_bbp_wr(dev, 22, 0x40);
  411. for (i = 100; i && (bbp_val & 0x10); i--)
  412. bbp_val = mt7601u_bbp_rr(dev, 47);
  413. temp = mt7601u_bbp_r47_get(dev, bbp_val, BBP_R47_F_TEMP);
  414. mt7601u_bbp_wr(dev, 22, 0);
  415. bbp_val = mt7601u_bbp_rr(dev, 21);
  416. bbp_val |= 0x02;
  417. mt7601u_bbp_wr(dev, 21, bbp_val);
  418. bbp_val &= ~0x02;
  419. mt7601u_bbp_wr(dev, 21, bbp_val);
  420. mt7601u_wr(dev, MT_RF_BYPASS_0, 0);
  421. mt7601u_wr(dev, MT_RF_SETTING_0, rf_set);
  422. mt7601u_wr(dev, MT_RF_BYPASS_0, rf_bp);
  423. trace_read_temp(dev, temp);
  424. return temp;
  425. }
  426. static s8 mt7601u_read_temp(struct mt7601u_dev *dev)
  427. {
  428. int i;
  429. u8 val;
  430. s8 temp;
  431. val = mt7601u_bbp_rmw(dev, 47, 0x7f, 0x10);
  432. /* Note: this rarely succeeds, temp can change even if it fails. */
  433. for (i = 100; i && (val & 0x10); i--)
  434. val = mt7601u_bbp_rr(dev, 47);
  435. temp = mt7601u_bbp_r47_get(dev, val, BBP_R47_F_TEMP);
  436. trace_read_temp(dev, temp);
  437. return temp;
  438. }
  439. static void mt7601u_rxdc_cal(struct mt7601u_dev *dev)
  440. {
  441. static const struct mt76_reg_pair intro[] = {
  442. { 158, 0x8d }, { 159, 0xfc },
  443. { 158, 0x8c }, { 159, 0x4c },
  444. }, outro[] = {
  445. { 158, 0x8d }, { 159, 0xe0 },
  446. };
  447. u32 mac_ctrl;
  448. int i, ret;
  449. mac_ctrl = mt7601u_rr(dev, MT_MAC_SYS_CTRL);
  450. mt7601u_wr(dev, MT_MAC_SYS_CTRL, MT_MAC_SYS_CTRL_ENABLE_RX);
  451. ret = mt7601u_write_reg_pairs(dev, MT_MCU_MEMMAP_BBP,
  452. intro, ARRAY_SIZE(intro));
  453. if (ret)
  454. dev_err(dev->dev, "%s intro failed:%d\n", __func__, ret);
  455. for (i = 20; i; i--) {
  456. usleep_range(300, 500);
  457. mt7601u_bbp_wr(dev, 158, 0x8c);
  458. if (mt7601u_bbp_rr(dev, 159) == 0x0c)
  459. break;
  460. }
  461. if (!i)
  462. dev_err(dev->dev, "%s timed out\n", __func__);
  463. mt7601u_wr(dev, MT_MAC_SYS_CTRL, 0);
  464. ret = mt7601u_write_reg_pairs(dev, MT_MCU_MEMMAP_BBP,
  465. outro, ARRAY_SIZE(outro));
  466. if (ret)
  467. dev_err(dev->dev, "%s outro failed:%d\n", __func__, ret);
  468. mt7601u_wr(dev, MT_MAC_SYS_CTRL, mac_ctrl);
  469. }
  470. void mt7601u_phy_recalibrate_after_assoc(struct mt7601u_dev *dev)
  471. {
  472. if (test_bit(MT7601U_STATE_REMOVED, &dev->state))
  473. return;
  474. mt7601u_mcu_calibrate(dev, MCU_CAL_DPD, dev->curr_temp);
  475. mt7601u_rxdc_cal(dev);
  476. }
  477. /* Note: function copied from vendor driver */
  478. static s16 lin2dBd(u16 linear)
  479. {
  480. short exp = 0;
  481. unsigned int mantisa;
  482. int app, dBd;
  483. if (WARN_ON(!linear))
  484. return -10000;
  485. mantisa = linear;
  486. exp = fls(mantisa) - 16;
  487. if (exp > 0)
  488. mantisa >>= exp;
  489. else
  490. mantisa <<= abs(exp);
  491. if (mantisa <= 0xb800)
  492. app = (mantisa + (mantisa >> 3) + (mantisa >> 4) - 0x9600);
  493. else
  494. app = (mantisa - (mantisa >> 3) - (mantisa >> 6) - 0x5a00);
  495. if (app < 0)
  496. app = 0;
  497. dBd = ((15 + exp) << 15) + app;
  498. dBd = (dBd << 2) + (dBd << 1) + (dBd >> 6) + (dBd >> 7);
  499. dBd = (dBd >> 10);
  500. return dBd;
  501. }
  502. static void
  503. mt7601u_set_initial_tssi(struct mt7601u_dev *dev, s16 tssi_db, s16 tssi_hvga_db)
  504. {
  505. struct tssi_data *d = &dev->ee->tssi_data;
  506. int init_offset;
  507. init_offset = -((tssi_db * d->slope + d->offset[1]) / 4096) + 10;
  508. mt76_rmw(dev, MT_TX_ALC_CFG_1, MT_TX_ALC_CFG_1_TEMP_COMP,
  509. int_to_s6(init_offset) & MT_TX_ALC_CFG_1_TEMP_COMP);
  510. }
  511. static void mt7601u_tssi_dc_gain_cal(struct mt7601u_dev *dev)
  512. {
  513. u8 rf_vga, rf_mixer, bbp_r47;
  514. int i, j;
  515. s8 res[4];
  516. s16 tssi_init_db, tssi_init_hvga_db;
  517. mt7601u_wr(dev, MT_RF_SETTING_0, 0x00000030);
  518. mt7601u_wr(dev, MT_RF_BYPASS_0, 0x000c0030);
  519. mt7601u_wr(dev, MT_MAC_SYS_CTRL, 0);
  520. mt7601u_bbp_wr(dev, 58, 0);
  521. mt7601u_bbp_wr(dev, 241, 0x2);
  522. mt7601u_bbp_wr(dev, 23, 0x8);
  523. bbp_r47 = mt7601u_bbp_rr(dev, 47);
  524. /* Set VGA gain */
  525. rf_vga = mt7601u_rf_rr(dev, 5, 3);
  526. mt7601u_rf_wr(dev, 5, 3, 8);
  527. /* Mixer disable */
  528. rf_mixer = mt7601u_rf_rr(dev, 4, 39);
  529. mt7601u_rf_wr(dev, 4, 39, 0);
  530. for (i = 0; i < 4; i++) {
  531. mt7601u_rf_wr(dev, 4, 39, (i & 1) ? rf_mixer : 0);
  532. mt7601u_bbp_wr(dev, 23, (i < 2) ? 0x08 : 0x02);
  533. mt7601u_rf_wr(dev, 5, 3, (i < 2) ? 0x08 : 0x11);
  534. /* BBP TSSI initial and soft reset */
  535. mt7601u_bbp_wr(dev, 22, 0);
  536. mt7601u_bbp_wr(dev, 244, 0);
  537. mt7601u_bbp_wr(dev, 21, 1);
  538. udelay(1);
  539. mt7601u_bbp_wr(dev, 21, 0);
  540. /* TSSI measurement */
  541. mt7601u_bbp_wr(dev, 47, 0x50);
  542. mt7601u_bbp_wr(dev, (i & 1) ? 244 : 22, (i & 1) ? 0x31 : 0x40);
  543. for (j = 20; j; j--)
  544. if (!(mt7601u_bbp_rr(dev, 47) & 0x10))
  545. break;
  546. if (!j)
  547. dev_err(dev->dev, "%s timed out\n", __func__);
  548. /* TSSI read */
  549. mt7601u_bbp_wr(dev, 47, 0x40);
  550. res[i] = mt7601u_bbp_rr(dev, 49);
  551. }
  552. tssi_init_db = lin2dBd((short)res[1] - res[0]);
  553. tssi_init_hvga_db = lin2dBd(((short)res[3] - res[2]) * 4);
  554. dev->tssi_init = res[0];
  555. dev->tssi_init_hvga = res[2];
  556. dev->tssi_init_hvga_offset_db = tssi_init_hvga_db - tssi_init_db;
  557. dev_dbg(dev->dev,
  558. "TSSI_init:%hhx db:%hx hvga:%hhx hvga_db:%hx off_db:%hx\n",
  559. dev->tssi_init, tssi_init_db, dev->tssi_init_hvga,
  560. tssi_init_hvga_db, dev->tssi_init_hvga_offset_db);
  561. mt7601u_bbp_wr(dev, 22, 0);
  562. mt7601u_bbp_wr(dev, 244, 0);
  563. mt7601u_bbp_wr(dev, 21, 1);
  564. udelay(1);
  565. mt7601u_bbp_wr(dev, 21, 0);
  566. mt7601u_wr(dev, MT_RF_BYPASS_0, 0);
  567. mt7601u_wr(dev, MT_RF_SETTING_0, 0);
  568. mt7601u_rf_wr(dev, 5, 3, rf_vga);
  569. mt7601u_rf_wr(dev, 4, 39, rf_mixer);
  570. mt7601u_bbp_wr(dev, 47, bbp_r47);
  571. mt7601u_set_initial_tssi(dev, tssi_init_db, tssi_init_hvga_db);
  572. }
  573. static int mt7601u_temp_comp(struct mt7601u_dev *dev, bool on)
  574. {
  575. int ret, temp, hi_temp = 400, lo_temp = -200;
  576. temp = (dev->raw_temp - dev->ee->ref_temp) * MT_EE_TEMPERATURE_SLOPE;
  577. dev->curr_temp = temp;
  578. /* DPD Calibration */
  579. if (temp - dev->dpd_temp > 450 || temp - dev->dpd_temp < -450) {
  580. dev->dpd_temp = temp;
  581. ret = mt7601u_mcu_calibrate(dev, MCU_CAL_DPD, dev->dpd_temp);
  582. if (ret)
  583. return ret;
  584. mt7601u_vco_cal(dev);
  585. dev_dbg(dev->dev, "Recalibrate DPD\n");
  586. }
  587. /* PLL Lock Protect */
  588. if (temp < -50 && !dev->pll_lock_protect) { /* < 20C */
  589. dev->pll_lock_protect = true;
  590. mt7601u_rf_wr(dev, 4, 4, 6);
  591. mt7601u_rf_clear(dev, 4, 10, 0x30);
  592. dev_dbg(dev->dev, "PLL lock protect on - too cold\n");
  593. } else if (temp > 50 && dev->pll_lock_protect) { /* > 30C */
  594. dev->pll_lock_protect = false;
  595. mt7601u_rf_wr(dev, 4, 4, 0);
  596. mt7601u_rf_rmw(dev, 4, 10, 0x30, 0x10);
  597. dev_dbg(dev->dev, "PLL lock protect off\n");
  598. }
  599. if (on) {
  600. hi_temp -= 50;
  601. lo_temp -= 50;
  602. }
  603. /* BBP CR for H, L, N temperature */
  604. if (temp > hi_temp)
  605. return mt7601u_bbp_temp(dev, MT_TEMP_MODE_HIGH, "high");
  606. else if (temp > lo_temp)
  607. return mt7601u_bbp_temp(dev, MT_TEMP_MODE_NORMAL, "normal");
  608. else
  609. return mt7601u_bbp_temp(dev, MT_TEMP_MODE_LOW, "low");
  610. }
  611. /* Note: this is used only with TSSI, we can just use trgt_pwr from eeprom. */
  612. static int mt7601u_current_tx_power(struct mt7601u_dev *dev)
  613. {
  614. return dev->ee->chan_pwr[dev->chandef.chan->hw_value - 1];
  615. }
  616. static bool mt7601u_use_hvga(struct mt7601u_dev *dev)
  617. {
  618. return !(mt7601u_current_tx_power(dev) > 20);
  619. }
  620. static s16
  621. mt7601u_phy_rf_pa_mode_val(struct mt7601u_dev *dev, int phy_mode, int tx_rate)
  622. {
  623. static const s16 decode_tb[] = { 0, 8847, -5734, -5734 };
  624. u32 reg;
  625. switch (phy_mode) {
  626. case MT_PHY_TYPE_OFDM:
  627. tx_rate += 4;
  628. fallthrough;
  629. case MT_PHY_TYPE_CCK:
  630. reg = dev->rf_pa_mode[0];
  631. break;
  632. default:
  633. reg = dev->rf_pa_mode[1];
  634. break;
  635. }
  636. return decode_tb[(reg >> (tx_rate * 2)) & 0x3];
  637. }
  638. static struct mt7601u_tssi_params
  639. mt7601u_tssi_params_get(struct mt7601u_dev *dev)
  640. {
  641. static const u8 ofdm_pkt2rate[8] = { 6, 4, 2, 0, 7, 5, 3, 1 };
  642. static const int static_power[4] = { 0, -49152, -98304, 49152 };
  643. struct mt7601u_tssi_params p;
  644. u8 bbp_r47, pkt_type, tx_rate;
  645. struct power_per_rate *rate_table;
  646. bbp_r47 = mt7601u_bbp_rr(dev, 47);
  647. p.tssi0 = mt7601u_bbp_r47_get(dev, bbp_r47, BBP_R47_F_TSSI);
  648. dev->raw_temp = mt7601u_bbp_r47_get(dev, bbp_r47, BBP_R47_F_TEMP);
  649. pkt_type = mt7601u_bbp_r47_get(dev, bbp_r47, BBP_R47_F_PKT_T);
  650. p.trgt_power = mt7601u_current_tx_power(dev);
  651. switch (pkt_type & 0x03) {
  652. case MT_PHY_TYPE_CCK:
  653. tx_rate = (pkt_type >> 4) & 0x03;
  654. rate_table = dev->ee->power_rate_table.cck;
  655. break;
  656. case MT_PHY_TYPE_OFDM:
  657. tx_rate = ofdm_pkt2rate[(pkt_type >> 4) & 0x07];
  658. rate_table = dev->ee->power_rate_table.ofdm;
  659. break;
  660. default:
  661. tx_rate = mt7601u_bbp_r47_get(dev, bbp_r47, BBP_R47_F_TX_RATE);
  662. tx_rate &= 0x7f;
  663. rate_table = dev->ee->power_rate_table.ht;
  664. break;
  665. }
  666. if (dev->bw == MT_BW_20)
  667. p.trgt_power += rate_table[tx_rate / 2].bw20;
  668. else
  669. p.trgt_power += rate_table[tx_rate / 2].bw40;
  670. p.trgt_power <<= 12;
  671. dev_dbg(dev->dev, "tx_rate:%02hhx pwr:%08x\n", tx_rate, p.trgt_power);
  672. p.trgt_power += mt7601u_phy_rf_pa_mode_val(dev, pkt_type & 0x03,
  673. tx_rate);
  674. /* Channel 14, cck, bw20 */
  675. if ((pkt_type & 0x03) == MT_PHY_TYPE_CCK) {
  676. if (mt7601u_bbp_rr(dev, 4) & 0x20)
  677. p.trgt_power += mt7601u_bbp_rr(dev, 178) ? 18022 : 9830;
  678. else
  679. p.trgt_power += mt7601u_bbp_rr(dev, 178) ? 819 : 24576;
  680. }
  681. p.trgt_power += static_power[mt7601u_bbp_rr(dev, 1) & 0x03];
  682. p.trgt_power += dev->ee->tssi_data.tx0_delta_offset;
  683. dev_dbg(dev->dev,
  684. "tssi:%02hhx t_power:%08x temp:%02hhx pkt_type:%02hhx\n",
  685. p.tssi0, p.trgt_power, dev->raw_temp, pkt_type);
  686. return p;
  687. }
  688. static bool mt7601u_tssi_read_ready(struct mt7601u_dev *dev)
  689. {
  690. return !(mt7601u_bbp_rr(dev, 47) & 0x10);
  691. }
  692. static int mt7601u_tssi_cal(struct mt7601u_dev *dev)
  693. {
  694. struct mt7601u_tssi_params params;
  695. int curr_pwr, diff_pwr;
  696. char tssi_offset;
  697. s8 tssi_init;
  698. s16 tssi_m_dc, tssi_db;
  699. bool hvga;
  700. u32 val;
  701. if (!dev->ee->tssi_enabled)
  702. return 0;
  703. hvga = mt7601u_use_hvga(dev);
  704. if (!dev->tssi_read_trig)
  705. return mt7601u_mcu_tssi_read_kick(dev, hvga);
  706. if (!mt7601u_tssi_read_ready(dev))
  707. return 0;
  708. params = mt7601u_tssi_params_get(dev);
  709. tssi_init = (hvga ? dev->tssi_init_hvga : dev->tssi_init);
  710. tssi_m_dc = params.tssi0 - tssi_init;
  711. tssi_db = lin2dBd(tssi_m_dc);
  712. dev_dbg(dev->dev, "tssi dc:%04hx db:%04hx hvga:%d\n",
  713. tssi_m_dc, tssi_db, hvga);
  714. if (dev->chandef.chan->hw_value < 5)
  715. tssi_offset = dev->ee->tssi_data.offset[0];
  716. else if (dev->chandef.chan->hw_value < 9)
  717. tssi_offset = dev->ee->tssi_data.offset[1];
  718. else
  719. tssi_offset = dev->ee->tssi_data.offset[2];
  720. if (hvga)
  721. tssi_db -= dev->tssi_init_hvga_offset_db;
  722. curr_pwr = tssi_db * dev->ee->tssi_data.slope + (tssi_offset << 9);
  723. diff_pwr = params.trgt_power - curr_pwr;
  724. dev_dbg(dev->dev, "Power curr:%08x diff:%08x\n", curr_pwr, diff_pwr);
  725. if (params.tssi0 > 126 && diff_pwr > 0) {
  726. dev_err(dev->dev, "Error: TSSI upper saturation\n");
  727. diff_pwr = 0;
  728. }
  729. if (params.tssi0 - tssi_init < 1 && diff_pwr < 0) {
  730. dev_err(dev->dev, "Error: TSSI lower saturation\n");
  731. diff_pwr = 0;
  732. }
  733. if ((dev->prev_pwr_diff ^ diff_pwr) < 0 && abs(diff_pwr) < 4096 &&
  734. (abs(diff_pwr) > abs(dev->prev_pwr_diff) ||
  735. (diff_pwr > 0 && diff_pwr == -dev->prev_pwr_diff)))
  736. diff_pwr = 0;
  737. else
  738. dev->prev_pwr_diff = diff_pwr;
  739. diff_pwr += (diff_pwr > 0) ? 2048 : -2048;
  740. diff_pwr /= 4096;
  741. dev_dbg(dev->dev, "final diff: %08x\n", diff_pwr);
  742. val = mt7601u_rr(dev, MT_TX_ALC_CFG_1);
  743. curr_pwr = s6_to_int(FIELD_GET(MT_TX_ALC_CFG_1_TEMP_COMP, val));
  744. diff_pwr += curr_pwr;
  745. val = (val & ~MT_TX_ALC_CFG_1_TEMP_COMP) | int_to_s6(diff_pwr);
  746. mt7601u_wr(dev, MT_TX_ALC_CFG_1, val);
  747. return mt7601u_mcu_tssi_read_kick(dev, hvga);
  748. }
  749. static u8 mt7601u_agc_default(struct mt7601u_dev *dev)
  750. {
  751. return (dev->ee->lna_gain - 8) * 2 + 0x34;
  752. }
  753. static void mt7601u_agc_reset(struct mt7601u_dev *dev)
  754. {
  755. u8 agc = mt7601u_agc_default(dev);
  756. mt7601u_bbp_wr(dev, 66, agc);
  757. }
  758. void mt7601u_agc_save(struct mt7601u_dev *dev)
  759. {
  760. dev->agc_save = mt7601u_bbp_rr(dev, 66);
  761. }
  762. void mt7601u_agc_restore(struct mt7601u_dev *dev)
  763. {
  764. mt7601u_bbp_wr(dev, 66, dev->agc_save);
  765. }
  766. static void mt7601u_agc_tune(struct mt7601u_dev *dev)
  767. {
  768. u8 val = mt7601u_agc_default(dev);
  769. long avg_rssi;
  770. if (test_bit(MT7601U_STATE_SCANNING, &dev->state))
  771. return;
  772. /* Note: only in STA mode and not dozing; perhaps do this only if
  773. * there is enough rssi updates since last run?
  774. * Rssi updates are only on beacons and U2M so should work...
  775. */
  776. spin_lock_bh(&dev->con_mon_lock);
  777. avg_rssi = ewma_rssi_read(&dev->avg_rssi);
  778. spin_unlock_bh(&dev->con_mon_lock);
  779. if (avg_rssi == 0)
  780. return;
  781. avg_rssi = -avg_rssi;
  782. if (avg_rssi <= -70)
  783. val -= 0x20;
  784. else if (avg_rssi <= -60)
  785. val -= 0x10;
  786. if (val != mt7601u_bbp_rr(dev, 66))
  787. mt7601u_bbp_wr(dev, 66, val);
  788. /* TODO: also if lost a lot of beacons try resetting
  789. * (see RTMPSetAGCInitValue() call in mlme.c).
  790. */
  791. }
  792. static void mt7601u_phy_calibrate(struct work_struct *work)
  793. {
  794. struct mt7601u_dev *dev = container_of(work, struct mt7601u_dev,
  795. cal_work.work);
  796. mt7601u_agc_tune(dev);
  797. mt7601u_tssi_cal(dev);
  798. /* If TSSI calibration was run it already updated temperature. */
  799. if (!dev->ee->tssi_enabled)
  800. dev->raw_temp = mt7601u_read_temp(dev);
  801. mt7601u_temp_comp(dev, true); /* TODO: find right value for @on */
  802. ieee80211_queue_delayed_work(dev->hw, &dev->cal_work,
  803. MT_CALIBRATE_INTERVAL);
  804. }
  805. static unsigned long
  806. __mt7601u_phy_freq_cal(struct mt7601u_dev *dev, s8 last_offset, u8 phy_mode)
  807. {
  808. u8 activate_threshold, deactivate_threshold;
  809. trace_freq_cal_offset(dev, phy_mode, last_offset);
  810. /* No beacons received - reschedule soon */
  811. if (last_offset == MT_FREQ_OFFSET_INVALID)
  812. return MT_FREQ_CAL_ADJ_INTERVAL;
  813. switch (phy_mode) {
  814. case MT_PHY_TYPE_CCK:
  815. activate_threshold = 19;
  816. deactivate_threshold = 5;
  817. break;
  818. case MT_PHY_TYPE_OFDM:
  819. activate_threshold = 102;
  820. deactivate_threshold = 32;
  821. break;
  822. case MT_PHY_TYPE_HT:
  823. case MT_PHY_TYPE_HT_GF:
  824. activate_threshold = 82;
  825. deactivate_threshold = 20;
  826. break;
  827. default:
  828. WARN_ON(1);
  829. return MT_FREQ_CAL_CHECK_INTERVAL;
  830. }
  831. if (abs(last_offset) >= activate_threshold)
  832. dev->freq_cal.adjusting = true;
  833. else if (abs(last_offset) <= deactivate_threshold)
  834. dev->freq_cal.adjusting = false;
  835. if (!dev->freq_cal.adjusting)
  836. return MT_FREQ_CAL_CHECK_INTERVAL;
  837. if (last_offset > deactivate_threshold) {
  838. if (dev->freq_cal.freq > 0)
  839. dev->freq_cal.freq--;
  840. else
  841. dev->freq_cal.adjusting = false;
  842. } else if (last_offset < -deactivate_threshold) {
  843. if (dev->freq_cal.freq < 0xbf)
  844. dev->freq_cal.freq++;
  845. else
  846. dev->freq_cal.adjusting = false;
  847. }
  848. trace_freq_cal_adjust(dev, dev->freq_cal.freq);
  849. mt7601u_rf_wr(dev, 0, 12, dev->freq_cal.freq);
  850. mt7601u_vco_cal(dev);
  851. return dev->freq_cal.adjusting ? MT_FREQ_CAL_ADJ_INTERVAL :
  852. MT_FREQ_CAL_CHECK_INTERVAL;
  853. }
  854. static void mt7601u_phy_freq_cal(struct work_struct *work)
  855. {
  856. struct mt7601u_dev *dev = container_of(work, struct mt7601u_dev,
  857. freq_cal.work.work);
  858. s8 last_offset;
  859. u8 phy_mode;
  860. unsigned long delay;
  861. spin_lock_bh(&dev->con_mon_lock);
  862. last_offset = dev->bcn_freq_off;
  863. phy_mode = dev->bcn_phy_mode;
  864. spin_unlock_bh(&dev->con_mon_lock);
  865. delay = __mt7601u_phy_freq_cal(dev, last_offset, phy_mode);
  866. ieee80211_queue_delayed_work(dev->hw, &dev->freq_cal.work, delay);
  867. spin_lock_bh(&dev->con_mon_lock);
  868. dev->bcn_freq_off = MT_FREQ_OFFSET_INVALID;
  869. spin_unlock_bh(&dev->con_mon_lock);
  870. }
  871. void mt7601u_phy_con_cal_onoff(struct mt7601u_dev *dev,
  872. struct ieee80211_bss_conf *info)
  873. {
  874. struct ieee80211_vif *vif = container_of(info, struct ieee80211_vif,
  875. bss_conf);
  876. if (!vif->cfg.assoc)
  877. cancel_delayed_work_sync(&dev->freq_cal.work);
  878. /* Start/stop collecting beacon data */
  879. spin_lock_bh(&dev->con_mon_lock);
  880. ether_addr_copy(dev->ap_bssid, info->bssid);
  881. ewma_rssi_init(&dev->avg_rssi);
  882. dev->bcn_freq_off = MT_FREQ_OFFSET_INVALID;
  883. spin_unlock_bh(&dev->con_mon_lock);
  884. dev->freq_cal.freq = dev->ee->rf_freq_off;
  885. dev->freq_cal.enabled = vif->cfg.assoc;
  886. dev->freq_cal.adjusting = false;
  887. if (vif->cfg.assoc)
  888. ieee80211_queue_delayed_work(dev->hw, &dev->freq_cal.work,
  889. MT_FREQ_CAL_INIT_DELAY);
  890. }
  891. static int mt7601u_init_cal(struct mt7601u_dev *dev)
  892. {
  893. u32 mac_ctrl;
  894. int ret;
  895. dev->raw_temp = mt7601u_read_bootup_temp(dev);
  896. dev->curr_temp = (dev->raw_temp - dev->ee->ref_temp) *
  897. MT_EE_TEMPERATURE_SLOPE;
  898. dev->dpd_temp = dev->curr_temp;
  899. mac_ctrl = mt7601u_rr(dev, MT_MAC_SYS_CTRL);
  900. ret = mt7601u_mcu_calibrate(dev, MCU_CAL_R, 0);
  901. if (ret)
  902. return ret;
  903. ret = mt7601u_rf_rr(dev, 0, 4);
  904. if (ret < 0)
  905. return ret;
  906. ret |= 0x80;
  907. ret = mt7601u_rf_wr(dev, 0, 4, ret);
  908. if (ret)
  909. return ret;
  910. msleep(2);
  911. ret = mt7601u_mcu_calibrate(dev, MCU_CAL_TXDCOC, 0);
  912. if (ret)
  913. return ret;
  914. mt7601u_rxdc_cal(dev);
  915. ret = mt7601u_set_bw_filter(dev, true);
  916. if (ret)
  917. return ret;
  918. ret = mt7601u_mcu_calibrate(dev, MCU_CAL_LOFT, 0);
  919. if (ret)
  920. return ret;
  921. ret = mt7601u_mcu_calibrate(dev, MCU_CAL_TXIQ, 0);
  922. if (ret)
  923. return ret;
  924. ret = mt7601u_mcu_calibrate(dev, MCU_CAL_RXIQ, 0);
  925. if (ret)
  926. return ret;
  927. ret = mt7601u_mcu_calibrate(dev, MCU_CAL_DPD, dev->dpd_temp);
  928. if (ret)
  929. return ret;
  930. mt7601u_rxdc_cal(dev);
  931. mt7601u_tssi_dc_gain_cal(dev);
  932. mt7601u_wr(dev, MT_MAC_SYS_CTRL, mac_ctrl);
  933. mt7601u_temp_comp(dev, true);
  934. return 0;
  935. }
  936. int mt7601u_bbp_set_bw(struct mt7601u_dev *dev, int bw)
  937. {
  938. u32 val, old;
  939. if (bw == dev->bw) {
  940. /* Vendor driver does the rmc even when no change is needed. */
  941. mt7601u_bbp_rmc(dev, 4, 0x18, bw == MT_BW_20 ? 0 : 0x10);
  942. return 0;
  943. }
  944. dev->bw = bw;
  945. /* Stop MAC for the time of bw change */
  946. old = mt7601u_rr(dev, MT_MAC_SYS_CTRL);
  947. val = old & ~(MT_MAC_SYS_CTRL_ENABLE_TX | MT_MAC_SYS_CTRL_ENABLE_RX);
  948. mt7601u_wr(dev, MT_MAC_SYS_CTRL, val);
  949. mt76_poll(dev, MT_MAC_STATUS, MT_MAC_STATUS_TX | MT_MAC_STATUS_RX,
  950. 0, 500000);
  951. mt7601u_bbp_rmc(dev, 4, 0x18, bw == MT_BW_20 ? 0 : 0x10);
  952. mt7601u_wr(dev, MT_MAC_SYS_CTRL, old);
  953. return mt7601u_load_bbp_temp_table_bw(dev);
  954. }
  955. /**
  956. * mt7601u_set_rx_path - set rx path in BBP
  957. * @dev: pointer to adapter structure
  958. * @path: rx path to set values are 0-based
  959. */
  960. void mt7601u_set_rx_path(struct mt7601u_dev *dev, u8 path)
  961. {
  962. mt7601u_bbp_rmw(dev, 3, 0x18, path << 3);
  963. }
  964. /**
  965. * mt7601u_set_tx_dac - set which tx DAC to use
  966. * @dev: pointer to adapter structure
  967. * @dac: DAC index, values are 0-based
  968. */
  969. void mt7601u_set_tx_dac(struct mt7601u_dev *dev, u8 dac)
  970. {
  971. mt7601u_bbp_rmc(dev, 1, 0x18, dac << 3);
  972. }
  973. int mt7601u_phy_init(struct mt7601u_dev *dev)
  974. {
  975. int ret;
  976. dev->rf_pa_mode[0] = mt7601u_rr(dev, MT_RF_PA_MODE_CFG0);
  977. dev->rf_pa_mode[1] = mt7601u_rr(dev, MT_RF_PA_MODE_CFG1);
  978. ret = mt7601u_rf_wr(dev, 0, 12, dev->ee->rf_freq_off);
  979. if (ret)
  980. return ret;
  981. ret = mt7601u_write_reg_pairs(dev, 0, rf_central,
  982. ARRAY_SIZE(rf_central));
  983. if (ret)
  984. return ret;
  985. ret = mt7601u_write_reg_pairs(dev, 0, rf_channel,
  986. ARRAY_SIZE(rf_channel));
  987. if (ret)
  988. return ret;
  989. ret = mt7601u_write_reg_pairs(dev, 0, rf_vga, ARRAY_SIZE(rf_vga));
  990. if (ret)
  991. return ret;
  992. ret = mt7601u_init_cal(dev);
  993. if (ret)
  994. return ret;
  995. dev->prev_pwr_diff = 100;
  996. INIT_DELAYED_WORK(&dev->cal_work, mt7601u_phy_calibrate);
  997. INIT_DELAYED_WORK(&dev->freq_cal.work, mt7601u_phy_freq_cal);
  998. return 0;
  999. }