phy.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253
  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. case MT_PHY_TYPE_CCK:
  635. reg = dev->rf_pa_mode[0];
  636. break;
  637. default:
  638. reg = dev->rf_pa_mode[1];
  639. break;
  640. }
  641. return decode_tb[(reg >> (tx_rate * 2)) & 0x3];
  642. }
  643. static struct mt7601u_tssi_params
  644. mt7601u_tssi_params_get(struct mt7601u_dev *dev)
  645. {
  646. static const u8 ofdm_pkt2rate[8] = { 6, 4, 2, 0, 7, 5, 3, 1 };
  647. static const int static_power[4] = { 0, -49152, -98304, 49152 };
  648. struct mt7601u_tssi_params p;
  649. u8 bbp_r47, pkt_type, tx_rate;
  650. struct power_per_rate *rate_table;
  651. bbp_r47 = mt7601u_bbp_rr(dev, 47);
  652. p.tssi0 = mt7601u_bbp_r47_get(dev, bbp_r47, BBP_R47_F_TSSI);
  653. dev->raw_temp = mt7601u_bbp_r47_get(dev, bbp_r47, BBP_R47_F_TEMP);
  654. pkt_type = mt7601u_bbp_r47_get(dev, bbp_r47, BBP_R47_F_PKT_T);
  655. p.trgt_power = mt7601u_current_tx_power(dev);
  656. switch (pkt_type & 0x03) {
  657. case MT_PHY_TYPE_CCK:
  658. tx_rate = (pkt_type >> 4) & 0x03;
  659. rate_table = dev->ee->power_rate_table.cck;
  660. break;
  661. case MT_PHY_TYPE_OFDM:
  662. tx_rate = ofdm_pkt2rate[(pkt_type >> 4) & 0x07];
  663. rate_table = dev->ee->power_rate_table.ofdm;
  664. break;
  665. default:
  666. tx_rate = mt7601u_bbp_r47_get(dev, bbp_r47, BBP_R47_F_TX_RATE);
  667. tx_rate &= 0x7f;
  668. rate_table = dev->ee->power_rate_table.ht;
  669. break;
  670. }
  671. if (dev->bw == MT_BW_20)
  672. p.trgt_power += rate_table[tx_rate / 2].bw20;
  673. else
  674. p.trgt_power += rate_table[tx_rate / 2].bw40;
  675. p.trgt_power <<= 12;
  676. dev_dbg(dev->dev, "tx_rate:%02hhx pwr:%08x\n", tx_rate, p.trgt_power);
  677. p.trgt_power += mt7601u_phy_rf_pa_mode_val(dev, pkt_type & 0x03,
  678. tx_rate);
  679. /* Channel 14, cck, bw20 */
  680. if ((pkt_type & 0x03) == MT_PHY_TYPE_CCK) {
  681. if (mt7601u_bbp_rr(dev, 4) & 0x20)
  682. p.trgt_power += mt7601u_bbp_rr(dev, 178) ? 18022 : 9830;
  683. else
  684. p.trgt_power += mt7601u_bbp_rr(dev, 178) ? 819 : 24576;
  685. }
  686. p.trgt_power += static_power[mt7601u_bbp_rr(dev, 1) & 0x03];
  687. p.trgt_power += dev->ee->tssi_data.tx0_delta_offset;
  688. dev_dbg(dev->dev,
  689. "tssi:%02hhx t_power:%08x temp:%02hhx pkt_type:%02hhx\n",
  690. p.tssi0, p.trgt_power, dev->raw_temp, pkt_type);
  691. return p;
  692. }
  693. static bool mt7601u_tssi_read_ready(struct mt7601u_dev *dev)
  694. {
  695. return !(mt7601u_bbp_rr(dev, 47) & 0x10);
  696. }
  697. static int mt7601u_tssi_cal(struct mt7601u_dev *dev)
  698. {
  699. struct mt7601u_tssi_params params;
  700. int curr_pwr, diff_pwr;
  701. char tssi_offset;
  702. s8 tssi_init;
  703. s16 tssi_m_dc, tssi_db;
  704. bool hvga;
  705. u32 val;
  706. if (!dev->ee->tssi_enabled)
  707. return 0;
  708. hvga = mt7601u_use_hvga(dev);
  709. if (!dev->tssi_read_trig)
  710. return mt7601u_mcu_tssi_read_kick(dev, hvga);
  711. if (!mt7601u_tssi_read_ready(dev))
  712. return 0;
  713. params = mt7601u_tssi_params_get(dev);
  714. tssi_init = (hvga ? dev->tssi_init_hvga : dev->tssi_init);
  715. tssi_m_dc = params.tssi0 - tssi_init;
  716. tssi_db = lin2dBd(tssi_m_dc);
  717. dev_dbg(dev->dev, "tssi dc:%04hx db:%04hx hvga:%d\n",
  718. tssi_m_dc, tssi_db, hvga);
  719. if (dev->chandef.chan->hw_value < 5)
  720. tssi_offset = dev->ee->tssi_data.offset[0];
  721. else if (dev->chandef.chan->hw_value < 9)
  722. tssi_offset = dev->ee->tssi_data.offset[1];
  723. else
  724. tssi_offset = dev->ee->tssi_data.offset[2];
  725. if (hvga)
  726. tssi_db -= dev->tssi_init_hvga_offset_db;
  727. curr_pwr = tssi_db * dev->ee->tssi_data.slope + (tssi_offset << 9);
  728. diff_pwr = params.trgt_power - curr_pwr;
  729. dev_dbg(dev->dev, "Power curr:%08x diff:%08x\n", curr_pwr, diff_pwr);
  730. if (params.tssi0 > 126 && diff_pwr > 0) {
  731. dev_err(dev->dev, "Error: TSSI upper saturation\n");
  732. diff_pwr = 0;
  733. }
  734. if (params.tssi0 - tssi_init < 1 && diff_pwr < 0) {
  735. dev_err(dev->dev, "Error: TSSI lower saturation\n");
  736. diff_pwr = 0;
  737. }
  738. if ((dev->prev_pwr_diff ^ diff_pwr) < 0 && abs(diff_pwr) < 4096 &&
  739. (abs(diff_pwr) > abs(dev->prev_pwr_diff) ||
  740. (diff_pwr > 0 && diff_pwr == -dev->prev_pwr_diff)))
  741. diff_pwr = 0;
  742. else
  743. dev->prev_pwr_diff = diff_pwr;
  744. diff_pwr += (diff_pwr > 0) ? 2048 : -2048;
  745. diff_pwr /= 4096;
  746. dev_dbg(dev->dev, "final diff: %08x\n", diff_pwr);
  747. val = mt7601u_rr(dev, MT_TX_ALC_CFG_1);
  748. curr_pwr = s6_to_int(FIELD_GET(MT_TX_ALC_CFG_1_TEMP_COMP, val));
  749. diff_pwr += curr_pwr;
  750. val = (val & ~MT_TX_ALC_CFG_1_TEMP_COMP) | int_to_s6(diff_pwr);
  751. mt7601u_wr(dev, MT_TX_ALC_CFG_1, val);
  752. return mt7601u_mcu_tssi_read_kick(dev, hvga);
  753. }
  754. static u8 mt7601u_agc_default(struct mt7601u_dev *dev)
  755. {
  756. return (dev->ee->lna_gain - 8) * 2 + 0x34;
  757. }
  758. static void mt7601u_agc_reset(struct mt7601u_dev *dev)
  759. {
  760. u8 agc = mt7601u_agc_default(dev);
  761. mt7601u_bbp_wr(dev, 66, agc);
  762. }
  763. void mt7601u_agc_save(struct mt7601u_dev *dev)
  764. {
  765. dev->agc_save = mt7601u_bbp_rr(dev, 66);
  766. }
  767. void mt7601u_agc_restore(struct mt7601u_dev *dev)
  768. {
  769. mt7601u_bbp_wr(dev, 66, dev->agc_save);
  770. }
  771. static void mt7601u_agc_tune(struct mt7601u_dev *dev)
  772. {
  773. u8 val = mt7601u_agc_default(dev);
  774. if (test_bit(MT7601U_STATE_SCANNING, &dev->state))
  775. return;
  776. /* Note: only in STA mode and not dozing; perhaps do this only if
  777. * there is enough rssi updates since last run?
  778. * Rssi updates are only on beacons and U2M so should work...
  779. */
  780. spin_lock_bh(&dev->con_mon_lock);
  781. if (dev->avg_rssi <= -70)
  782. val -= 0x20;
  783. else if (dev->avg_rssi <= -60)
  784. val -= 0x10;
  785. spin_unlock_bh(&dev->con_mon_lock);
  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. if (!info->assoc)
  875. cancel_delayed_work_sync(&dev->freq_cal.work);
  876. /* Start/stop collecting beacon data */
  877. spin_lock_bh(&dev->con_mon_lock);
  878. ether_addr_copy(dev->ap_bssid, info->bssid);
  879. dev->avg_rssi = 0;
  880. dev->bcn_freq_off = MT_FREQ_OFFSET_INVALID;
  881. spin_unlock_bh(&dev->con_mon_lock);
  882. dev->freq_cal.freq = dev->ee->rf_freq_off;
  883. dev->freq_cal.enabled = info->assoc;
  884. dev->freq_cal.adjusting = false;
  885. if (info->assoc)
  886. ieee80211_queue_delayed_work(dev->hw, &dev->freq_cal.work,
  887. MT_FREQ_CAL_INIT_DELAY);
  888. }
  889. static int mt7601u_init_cal(struct mt7601u_dev *dev)
  890. {
  891. u32 mac_ctrl;
  892. int ret;
  893. dev->raw_temp = mt7601u_read_bootup_temp(dev);
  894. dev->curr_temp = (dev->raw_temp - dev->ee->ref_temp) *
  895. MT_EE_TEMPERATURE_SLOPE;
  896. dev->dpd_temp = dev->curr_temp;
  897. mac_ctrl = mt7601u_rr(dev, MT_MAC_SYS_CTRL);
  898. ret = mt7601u_mcu_calibrate(dev, MCU_CAL_R, 0);
  899. if (ret)
  900. return ret;
  901. ret = mt7601u_rf_rr(dev, 0, 4);
  902. if (ret < 0)
  903. return ret;
  904. ret |= 0x80;
  905. ret = mt7601u_rf_wr(dev, 0, 4, ret);
  906. if (ret)
  907. return ret;
  908. msleep(2);
  909. ret = mt7601u_mcu_calibrate(dev, MCU_CAL_TXDCOC, 0);
  910. if (ret)
  911. return ret;
  912. mt7601u_rxdc_cal(dev);
  913. ret = mt7601u_set_bw_filter(dev, true);
  914. if (ret)
  915. return ret;
  916. ret = mt7601u_mcu_calibrate(dev, MCU_CAL_LOFT, 0);
  917. if (ret)
  918. return ret;
  919. ret = mt7601u_mcu_calibrate(dev, MCU_CAL_TXIQ, 0);
  920. if (ret)
  921. return ret;
  922. ret = mt7601u_mcu_calibrate(dev, MCU_CAL_RXIQ, 0);
  923. if (ret)
  924. return ret;
  925. ret = mt7601u_mcu_calibrate(dev, MCU_CAL_DPD, dev->dpd_temp);
  926. if (ret)
  927. return ret;
  928. mt7601u_rxdc_cal(dev);
  929. mt7601u_tssi_dc_gain_cal(dev);
  930. mt7601u_wr(dev, MT_MAC_SYS_CTRL, mac_ctrl);
  931. mt7601u_temp_comp(dev, true);
  932. return 0;
  933. }
  934. int mt7601u_bbp_set_bw(struct mt7601u_dev *dev, int bw)
  935. {
  936. u32 val, old;
  937. if (bw == dev->bw) {
  938. /* Vendor driver does the rmc even when no change is needed. */
  939. mt7601u_bbp_rmc(dev, 4, 0x18, bw == MT_BW_20 ? 0 : 0x10);
  940. return 0;
  941. }
  942. dev->bw = bw;
  943. /* Stop MAC for the time of bw change */
  944. old = mt7601u_rr(dev, MT_MAC_SYS_CTRL);
  945. val = old & ~(MT_MAC_SYS_CTRL_ENABLE_TX | MT_MAC_SYS_CTRL_ENABLE_RX);
  946. mt7601u_wr(dev, MT_MAC_SYS_CTRL, val);
  947. mt76_poll(dev, MT_MAC_STATUS, MT_MAC_STATUS_TX | MT_MAC_STATUS_RX,
  948. 0, 500000);
  949. mt7601u_bbp_rmc(dev, 4, 0x18, bw == MT_BW_20 ? 0 : 0x10);
  950. mt7601u_wr(dev, MT_MAC_SYS_CTRL, old);
  951. return mt7601u_load_bbp_temp_table_bw(dev);
  952. }
  953. /**
  954. * mt7601u_set_rx_path - set rx path in BBP
  955. * @dev: pointer to adapter structure
  956. * @path: rx path to set values are 0-based
  957. */
  958. void mt7601u_set_rx_path(struct mt7601u_dev *dev, u8 path)
  959. {
  960. mt7601u_bbp_rmw(dev, 3, 0x18, path << 3);
  961. }
  962. /**
  963. * mt7601u_set_tx_dac - set which tx DAC to use
  964. * @dev: pointer to adapter structure
  965. * @path: DAC index, values are 0-based
  966. */
  967. void mt7601u_set_tx_dac(struct mt7601u_dev *dev, u8 dac)
  968. {
  969. mt7601u_bbp_rmc(dev, 1, 0x18, dac << 3);
  970. }
  971. int mt7601u_phy_init(struct mt7601u_dev *dev)
  972. {
  973. int ret;
  974. dev->rf_pa_mode[0] = mt7601u_rr(dev, MT_RF_PA_MODE_CFG0);
  975. dev->rf_pa_mode[1] = mt7601u_rr(dev, MT_RF_PA_MODE_CFG1);
  976. ret = mt7601u_rf_wr(dev, 0, 12, dev->ee->rf_freq_off);
  977. if (ret)
  978. return ret;
  979. ret = mt7601u_write_reg_pairs(dev, 0, rf_central,
  980. ARRAY_SIZE(rf_central));
  981. if (ret)
  982. return ret;
  983. ret = mt7601u_write_reg_pairs(dev, 0, rf_channel,
  984. ARRAY_SIZE(rf_channel));
  985. if (ret)
  986. return ret;
  987. ret = mt7601u_write_reg_pairs(dev, 0, rf_vga, ARRAY_SIZE(rf_vga));
  988. if (ret)
  989. return ret;
  990. ret = mt7601u_init_cal(dev);
  991. if (ret)
  992. return ret;
  993. dev->prev_pwr_diff = 100;
  994. INIT_DELAYED_WORK(&dev->cal_work, mt7601u_phy_calibrate);
  995. INIT_DELAYED_WORK(&dev->freq_cal.work, mt7601u_phy_freq_cal);
  996. return 0;
  997. }