1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /* Copyright(c) 2018-2019  Realtek Corporation
3  */
4 
5 #include "main.h"
6 #include "util.h"
7 #include "reg.h"
8 
9 bool check_hw_ready(struct rtw_dev *rtwdev, u32 addr, u32 mask, u32 target)
10 {
11 	u32 cnt;
12 
13 	for (cnt = 0; cnt < 1000; cnt++) {
14 		if (rtw_read32_mask(rtwdev, addr, mask) == target)
15 			return true;
16 
17 		udelay(10);
18 	}
19 
20 	return false;
21 }
22 
23 bool ltecoex_read_reg(struct rtw_dev *rtwdev, u16 offset, u32 *val)
24 {
25 	if (!check_hw_ready(rtwdev, LTECOEX_ACCESS_CTRL, LTECOEX_READY, 1))
26 		return false;
27 
28 	rtw_write32(rtwdev, LTECOEX_ACCESS_CTRL, 0x800F0000 | offset);
29 	*val = rtw_read32(rtwdev, LTECOEX_READ_DATA);
30 
31 	return true;
32 }
33 
34 bool ltecoex_reg_write(struct rtw_dev *rtwdev, u16 offset, u32 value)
35 {
36 	if (!check_hw_ready(rtwdev, LTECOEX_ACCESS_CTRL, LTECOEX_READY, 1))
37 		return false;
38 
39 	rtw_write32(rtwdev, LTECOEX_WRITE_DATA, value);
40 	rtw_write32(rtwdev, LTECOEX_ACCESS_CTRL, 0xC00F0000 | offset);
41 
42 	return true;
43 }
44 
45 void rtw_restore_reg(struct rtw_dev *rtwdev,
46 		     struct rtw_backup_info *bckp, u32 num)
47 {
48 	u8 len;
49 	u32 reg;
50 	u32 val;
51 	int i;
52 
53 	for (i = 0; i < num; i++, bckp++) {
54 		len = bckp->len;
55 		reg = bckp->reg;
56 		val = bckp->val;
57 
58 		switch (len) {
59 		case 1:
60 			rtw_write8(rtwdev, reg, (u8)val);
61 			break;
62 		case 2:
63 			rtw_write16(rtwdev, reg, (u16)val);
64 			break;
65 		case 4:
66 			rtw_write32(rtwdev, reg, (u32)val);
67 			break;
68 		default:
69 			break;
70 		}
71 	}
72 }
73 
74 void rtw_desc_to_mcsrate(u16 rate, u8 *mcs, u8 *nss)
75 {
76 	if (rate <= DESC_RATE54M)
77 		return;
78 
79 	if (rate >= DESC_RATEVHT1SS_MCS0 &&
80 	    rate <= DESC_RATEVHT1SS_MCS9) {
81 		*nss = 1;
82 		*mcs = rate - DESC_RATEVHT1SS_MCS0;
83 	} else if (rate >= DESC_RATEVHT2SS_MCS0 &&
84 		   rate <= DESC_RATEVHT2SS_MCS9) {
85 		*nss = 2;
86 		*mcs = rate - DESC_RATEVHT2SS_MCS0;
87 	} else if (rate >= DESC_RATEVHT3SS_MCS0 &&
88 		   rate <= DESC_RATEVHT3SS_MCS9) {
89 		*nss = 3;
90 		*mcs = rate - DESC_RATEVHT3SS_MCS0;
91 	} else if (rate >= DESC_RATEVHT4SS_MCS0 &&
92 		   rate <= DESC_RATEVHT4SS_MCS9) {
93 		*nss = 4;
94 		*mcs = rate - DESC_RATEVHT4SS_MCS0;
95 	} else if (rate >= DESC_RATEMCS0 &&
96 		   rate <= DESC_RATEMCS15) {
97 		*mcs = rate - DESC_RATEMCS0;
98 	}
99 }
100