phy.c 30 KB

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