Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / net / wireless / realtek / rtw88 / util.c
blob2c515af214e76e5bfecaeb4515545515b048acbe
1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /* Copyright(c) 2018-2019 Realtek Corporation
3 */
5 #include "main.h"
6 #include "util.h"
7 #include "reg.h"
9 bool check_hw_ready(struct rtw_dev *rtwdev, u32 addr, u32 mask, u32 target)
11 u32 cnt;
13 for (cnt = 0; cnt < 1000; cnt++) {
14 if (rtw_read32_mask(rtwdev, addr, mask) == target)
15 return true;
17 udelay(10);
20 return false;
22 EXPORT_SYMBOL(check_hw_ready);
24 bool ltecoex_read_reg(struct rtw_dev *rtwdev, u16 offset, u32 *val)
26 struct rtw_chip_info *chip = rtwdev->chip;
27 const struct rtw_ltecoex_addr *ltecoex = chip->ltecoex_addr;
29 if (!check_hw_ready(rtwdev, ltecoex->ctrl, LTECOEX_READY, 1))
30 return false;
32 rtw_write32(rtwdev, ltecoex->ctrl, 0x800F0000 | offset);
33 *val = rtw_read32(rtwdev, ltecoex->rdata);
35 return true;
38 bool ltecoex_reg_write(struct rtw_dev *rtwdev, u16 offset, u32 value)
40 struct rtw_chip_info *chip = rtwdev->chip;
41 const struct rtw_ltecoex_addr *ltecoex = chip->ltecoex_addr;
43 if (!check_hw_ready(rtwdev, ltecoex->ctrl, LTECOEX_READY, 1))
44 return false;
46 rtw_write32(rtwdev, ltecoex->wdata, value);
47 rtw_write32(rtwdev, ltecoex->ctrl, 0xC00F0000 | offset);
49 return true;
52 void rtw_restore_reg(struct rtw_dev *rtwdev,
53 struct rtw_backup_info *bckp, u32 num)
55 u8 len;
56 u32 reg;
57 u32 val;
58 int i;
60 for (i = 0; i < num; i++, bckp++) {
61 len = bckp->len;
62 reg = bckp->reg;
63 val = bckp->val;
65 switch (len) {
66 case 1:
67 rtw_write8(rtwdev, reg, (u8)val);
68 break;
69 case 2:
70 rtw_write16(rtwdev, reg, (u16)val);
71 break;
72 case 4:
73 rtw_write32(rtwdev, reg, (u32)val);
74 break;
75 default:
76 break;
80 EXPORT_SYMBOL(rtw_restore_reg);
82 void rtw_desc_to_mcsrate(u16 rate, u8 *mcs, u8 *nss)
84 if (rate <= DESC_RATE54M)
85 return;
87 if (rate >= DESC_RATEVHT1SS_MCS0 &&
88 rate <= DESC_RATEVHT1SS_MCS9) {
89 *nss = 1;
90 *mcs = rate - DESC_RATEVHT1SS_MCS0;
91 } else if (rate >= DESC_RATEVHT2SS_MCS0 &&
92 rate <= DESC_RATEVHT2SS_MCS9) {
93 *nss = 2;
94 *mcs = rate - DESC_RATEVHT2SS_MCS0;
95 } else if (rate >= DESC_RATEVHT3SS_MCS0 &&
96 rate <= DESC_RATEVHT3SS_MCS9) {
97 *nss = 3;
98 *mcs = rate - DESC_RATEVHT3SS_MCS0;
99 } else if (rate >= DESC_RATEVHT4SS_MCS0 &&
100 rate <= DESC_RATEVHT4SS_MCS9) {
101 *nss = 4;
102 *mcs = rate - DESC_RATEVHT4SS_MCS0;
103 } else if (rate >= DESC_RATEMCS0 &&
104 rate <= DESC_RATEMCS15) {
105 *mcs = rate - DESC_RATEMCS0;