phy.c 31 KB

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