1028fa281SKalle Valo // SPDX-License-Identifier: GPL-2.0-only
2028fa281SKalle Valo /*
3028fa281SKalle Valo  * RTL8XXXU mac80211 USB driver - 8710bu aka 8188gu specific subdriver
4028fa281SKalle Valo  *
5028fa281SKalle Valo  * Copyright (c) 2023 Bitterblue Smith <rtl8821cerfe2@gmail.com>
6028fa281SKalle Valo  *
7028fa281SKalle Valo  * Portions copied from existing rtl8xxxu code:
8028fa281SKalle Valo  * Copyright (c) 2014 - 2017 Jes Sorensen <Jes.Sorensen@gmail.com>
9028fa281SKalle Valo  *
10028fa281SKalle Valo  * Portions, notably calibration code:
11028fa281SKalle Valo  * Copyright(c) 2007 - 2011 Realtek Corporation. All rights reserved.
12028fa281SKalle Valo  */
13028fa281SKalle Valo 
14028fa281SKalle Valo #include "regs.h"
15*949f6f3aSPing-Ke Shih #include "rtl8xxxu.h"
16028fa281SKalle Valo 
17028fa281SKalle Valo static const struct rtl8xxxu_reg8val rtl8710b_mac_init_table[] = {
18028fa281SKalle Valo 	{0x421, 0x0F}, {0x428, 0x0A}, {0x429, 0x10}, {0x430, 0x00},
19028fa281SKalle Valo 	{0x431, 0x00}, {0x432, 0x00}, {0x433, 0x01}, {0x434, 0x04},
20028fa281SKalle Valo 	{0x435, 0x05}, {0x436, 0x07}, {0x437, 0x08}, {0x43C, 0x04},
21028fa281SKalle Valo 	{0x43D, 0x05}, {0x43E, 0x07}, {0x43F, 0x08}, {0x440, 0x5D},
22028fa281SKalle Valo 	{0x441, 0x01}, {0x442, 0x00}, {0x444, 0x10}, {0x445, 0x00},
23028fa281SKalle Valo 	{0x446, 0x00}, {0x447, 0x00}, {0x448, 0x00}, {0x449, 0xF0},
24028fa281SKalle Valo 	{0x44A, 0x0F}, {0x44B, 0x3E}, {0x44C, 0x10}, {0x44D, 0x00},
25028fa281SKalle Valo 	{0x44E, 0x00}, {0x44F, 0x00}, {0x450, 0x00}, {0x451, 0xF0},
26028fa281SKalle Valo 	{0x452, 0x0F}, {0x453, 0x00}, {0x456, 0x5E}, {0x460, 0x66},
27028fa281SKalle Valo 	{0x461, 0x66}, {0x4C8, 0xFF}, {0x4C9, 0x08}, {0x4CC, 0xFF},
28028fa281SKalle Valo 	{0x4CD, 0xFF}, {0x4CE, 0x01}, {0x500, 0x26}, {0x501, 0xA2},
29028fa281SKalle Valo 	{0x502, 0x2F}, {0x503, 0x00}, {0x504, 0x28}, {0x505, 0xA3},
30028fa281SKalle Valo 	{0x506, 0x5E}, {0x507, 0x00}, {0x508, 0x2B}, {0x509, 0xA4},
31028fa281SKalle Valo 	{0x50A, 0x5E}, {0x50B, 0x00}, {0x50C, 0x4F}, {0x50D, 0xA4},
32028fa281SKalle Valo 	{0x50E, 0x00}, {0x50F, 0x00}, {0x512, 0x1C}, {0x514, 0x0A},
33028fa281SKalle Valo 	{0x516, 0x0A}, {0x525, 0x4F}, {0x550, 0x10}, {0x551, 0x10},
34028fa281SKalle Valo 	{0x559, 0x02}, {0x55C, 0x28}, {0x55D, 0xFF}, {0x605, 0x30},
35028fa281SKalle Valo 	{0x608, 0x0E}, {0x609, 0x2A}, {0x620, 0xFF}, {0x621, 0xFF},
36028fa281SKalle Valo 	{0x622, 0xFF}, {0x623, 0xFF}, {0x624, 0xFF}, {0x625, 0xFF},
37028fa281SKalle Valo 	{0x626, 0xFF}, {0x627, 0xFF}, {0x638, 0x28}, {0x63C, 0x0A},
38028fa281SKalle Valo 	{0x63D, 0x0A}, {0x63E, 0x0C}, {0x63F, 0x0C}, {0x640, 0x40},
39028fa281SKalle Valo 	{0x642, 0x40}, {0x643, 0x00}, {0x652, 0xC8}, {0x66A, 0xB0},
40028fa281SKalle Valo 	{0x66E, 0x05}, {0x700, 0x21}, {0x701, 0x43}, {0x702, 0x65},
41028fa281SKalle Valo 	{0x703, 0x87}, {0x708, 0x21}, {0x709, 0x43}, {0x70A, 0x65},
42028fa281SKalle Valo 	{0x70B, 0x87},
43028fa281SKalle Valo 	{0xffff, 0xff},
44028fa281SKalle Valo };
45028fa281SKalle Valo 
46028fa281SKalle Valo /* If updating the phy init tables, also update rtl8710b_revise_cck_tx_psf(). */
47028fa281SKalle Valo static const struct rtl8xxxu_reg32val rtl8710bu_qfn48m_u_phy_init_table[] = {
48028fa281SKalle Valo 	{0x800, 0x80045700}, {0x804, 0x00000001},
49028fa281SKalle Valo 	{0x808, 0x00FC8000}, {0x80C, 0x0000000A},
50028fa281SKalle Valo 	{0x810, 0x10001331}, {0x814, 0x020C3D10},
51028fa281SKalle Valo 	{0x818, 0x00200385}, {0x81C, 0x00000000},
52028fa281SKalle Valo 	{0x820, 0x01000100}, {0x824, 0x00390204},
53028fa281SKalle Valo 	{0x828, 0x00000000}, {0x82C, 0x00000000},
54028fa281SKalle Valo 	{0x830, 0x00000000}, {0x834, 0x00000000},
55028fa281SKalle Valo 	{0x838, 0x00000000}, {0x83C, 0x00000000},
56028fa281SKalle Valo 	{0x840, 0x00010000}, {0x844, 0x00000000},
57028fa281SKalle Valo 	{0x848, 0x00000000}, {0x84C, 0x00000000},
58028fa281SKalle Valo 	{0x850, 0x00030000}, {0x854, 0x00000000},
59028fa281SKalle Valo 	{0x858, 0x7E1A569A}, {0x85C, 0x569A569A},
60028fa281SKalle Valo 	{0x860, 0x00000130}, {0x864, 0x20000000},
61028fa281SKalle Valo 	{0x868, 0x00000000}, {0x86C, 0x27272700},
62028fa281SKalle Valo 	{0x870, 0x00050000}, {0x874, 0x25005000},
63028fa281SKalle Valo 	{0x878, 0x00000808}, {0x87C, 0x004F0201},
64028fa281SKalle Valo 	{0x880, 0xB0000B1E}, {0x884, 0x00000007},
65028fa281SKalle Valo 	{0x888, 0x00000000}, {0x88C, 0xCCC400C0},
66028fa281SKalle Valo 	{0x890, 0x00000800}, {0x894, 0xFFFFFFFE},
67028fa281SKalle Valo 	{0x898, 0x40302010}, {0x89C, 0x00706050},
68028fa281SKalle Valo 	{0x900, 0x00000000}, {0x904, 0x00000023},
69028fa281SKalle Valo 	{0x908, 0x00000000}, {0x90C, 0x81121111},
70028fa281SKalle Valo 	{0x910, 0x00000402}, {0x914, 0x00000201},
71028fa281SKalle Valo 	{0x920, 0x18C6318C}, {0x924, 0x0000018C},
72028fa281SKalle Valo 	{0x948, 0x99000000}, {0x94C, 0x00000010},
73028fa281SKalle Valo 	{0x950, 0x00003000}, {0x954, 0x5A880000},
74028fa281SKalle Valo 	{0x958, 0x4BC6D87A}, {0x95C, 0x04EB9B79},
75028fa281SKalle Valo 	{0x96C, 0x00000003}, {0x970, 0x00000000},
76028fa281SKalle Valo 	{0x974, 0x00000000}, {0x978, 0x00000000},
77028fa281SKalle Valo 	{0x97C, 0x13000000}, {0x980, 0x00000000},
78028fa281SKalle Valo 	{0xA00, 0x00D046C8}, {0xA04, 0x80FF800C},
79028fa281SKalle Valo 	{0xA08, 0x84838300}, {0xA0C, 0x2E20100F},
80028fa281SKalle Valo 	{0xA10, 0x9500BB78}, {0xA14, 0x1114D028},
81028fa281SKalle Valo 	{0xA18, 0x00881117}, {0xA1C, 0x89140F00},
82028fa281SKalle Valo 	{0xA20, 0xE82C0001}, {0xA24, 0x64B80C1C},
83028fa281SKalle Valo 	{0xA28, 0x00008810}, {0xA2C, 0x00D30000},
84028fa281SKalle Valo 	{0xA70, 0x101FBF00}, {0xA74, 0x00000007},
85028fa281SKalle Valo 	{0xA78, 0x00000900}, {0xA7C, 0x225B0606},
86028fa281SKalle Valo 	{0xA80, 0x218075B1}, {0xA84, 0x00200000},
87028fa281SKalle Valo 	{0xA88, 0x040C0000}, {0xA8C, 0x12345678},
88028fa281SKalle Valo 	{0xA90, 0xABCDEF00}, {0xA94, 0x001B1B89},
89028fa281SKalle Valo 	{0xA98, 0x00000000}, {0xA9C, 0x80020000},
90028fa281SKalle Valo 	{0xAA0, 0x00000000}, {0xAA4, 0x0000000C},
91028fa281SKalle Valo 	{0xAA8, 0xCA110058}, {0xAAC, 0x01235667},
92028fa281SKalle Valo 	{0xAB0, 0x00000000}, {0xAB4, 0x20201402},
93028fa281SKalle Valo 	{0xB2C, 0x00000000}, {0xC00, 0x48071D40},
94028fa281SKalle Valo 	{0xC04, 0x03A05611}, {0xC08, 0x000000E4},
95028fa281SKalle Valo 	{0xC0C, 0x6C6C6C6C}, {0xC10, 0x18800000},
96028fa281SKalle Valo 	{0xC14, 0x40000100}, {0xC18, 0x08800000},
97028fa281SKalle Valo 	{0xC1C, 0x40000100}, {0xC20, 0x00000000},
98028fa281SKalle Valo 	{0xC24, 0x00000000}, {0xC28, 0x00000000},
99028fa281SKalle Valo 	{0xC2C, 0x00000000}, {0xC30, 0x69E9AC4A},
100028fa281SKalle Valo 	{0xC34, 0x31000040}, {0xC38, 0x21688080},
101028fa281SKalle Valo 	{0xC3C, 0x0000170C}, {0xC40, 0x1F78403F},
102028fa281SKalle Valo 	{0xC44, 0x00010036}, {0xC48, 0xEC020107},
103028fa281SKalle Valo 	{0xC4C, 0x007F037F}, {0xC50, 0x69553420},
104028fa281SKalle Valo 	{0xC54, 0x43BC0094}, {0xC58, 0x00013169},
105028fa281SKalle Valo 	{0xC5C, 0x00250492}, {0xC60, 0x00280A00},
106028fa281SKalle Valo 	{0xC64, 0x7112848B}, {0xC68, 0x47C074FF},
107028fa281SKalle Valo 	{0xC6C, 0x00000036}, {0xC70, 0x2C7F000D},
108028fa281SKalle Valo 	{0xC74, 0x020600DB}, {0xC78, 0x0000001F},
109028fa281SKalle Valo 	{0xC7C, 0x00B91612}, {0xC80, 0x390000E4},
110028fa281SKalle Valo 	{0xC84, 0x11F60000}, {0xC88, 0x1051B75F},
111028fa281SKalle Valo 	{0xC8C, 0x20200109}, {0xC90, 0x00091521},
112028fa281SKalle Valo 	{0xC94, 0x00000000}, {0xC98, 0x00121820},
113028fa281SKalle Valo 	{0xC9C, 0x00007F7F}, {0xCA0, 0x00011000},
114028fa281SKalle Valo 	{0xCA4, 0x800000A0}, {0xCA8, 0x84E6C606},
115028fa281SKalle Valo 	{0xCAC, 0x00000060}, {0xCB0, 0x00000000},
116028fa281SKalle Valo 	{0xCB4, 0x00000000}, {0xCB8, 0x00000000},
117028fa281SKalle Valo 	{0xCBC, 0x28000000}, {0xCC0, 0x1051B75F},
118028fa281SKalle Valo 	{0xCC4, 0x00000109}, {0xCC8, 0x000442D6},
119028fa281SKalle Valo 	{0xCCC, 0x00000000}, {0xCD0, 0x000001C8},
120028fa281SKalle Valo 	{0xCD4, 0x001C8000}, {0xCD8, 0x00000100},
121028fa281SKalle Valo 	{0xCDC, 0x40100000}, {0xCE0, 0x00222220},
122028fa281SKalle Valo 	{0xCE4, 0x10000000}, {0xCE8, 0x37644302},
123028fa281SKalle Valo 	{0xCEC, 0x2F97D40C}, {0xD00, 0x04030740},
124028fa281SKalle Valo 	{0xD04, 0x40020401}, {0xD08, 0x0000907F},
125028fa281SKalle Valo 	{0xD0C, 0x20010201}, {0xD10, 0xA0633333},
126028fa281SKalle Valo 	{0xD14, 0x3333BC53}, {0xD18, 0x7A8F5B6F},
127028fa281SKalle Valo 	{0xD2C, 0xCB979975}, {0xD30, 0x00000000},
128028fa281SKalle Valo 	{0xD34, 0x40608000}, {0xD38, 0x88000000},
129028fa281SKalle Valo 	{0xD3C, 0xC0127353}, {0xD40, 0x00000000},
130028fa281SKalle Valo 	{0xD44, 0x00000000}, {0xD48, 0x00000000},
131028fa281SKalle Valo 	{0xD4C, 0x00000000}, {0xD50, 0x00006528},
132028fa281SKalle Valo 	{0xD54, 0x00000000}, {0xD58, 0x00000282},
133028fa281SKalle Valo 	{0xD5C, 0x30032064}, {0xD60, 0x4653DE68},
134028fa281SKalle Valo 	{0xD64, 0x04518A3C}, {0xD68, 0x00002101},
135028fa281SKalle Valo 	{0xE00, 0x2D2D2D2D}, {0xE04, 0x2D2D2D2D},
136028fa281SKalle Valo 	{0xE08, 0x0390272D}, {0xE10, 0x2D2D2D2D},
137028fa281SKalle Valo 	{0xE14, 0x2D2D2D2D}, {0xE18, 0x2D2D2D2D},
138028fa281SKalle Valo 	{0xE1C, 0x2D2D2D2D}, {0xE28, 0x00000000},
139028fa281SKalle Valo 	{0xE30, 0x1000DC1F}, {0xE34, 0x10008C1F},
140028fa281SKalle Valo 	{0xE38, 0x02140102}, {0xE3C, 0x681604C2},
141028fa281SKalle Valo 	{0xE40, 0x01007C00}, {0xE44, 0x01004800},
142028fa281SKalle Valo 	{0xE48, 0xFB000000}, {0xE4C, 0x000028D1},
143028fa281SKalle Valo 	{0xE50, 0x1000DC1F}, {0xE54, 0x10008C1F},
144028fa281SKalle Valo 	{0xE58, 0x02140102}, {0xE5C, 0x28160D05},
145028fa281SKalle Valo 	{0xE60, 0x0000C008}, {0xE68, 0x001B25A4},
146028fa281SKalle Valo 	{0xE64, 0x281600A0}, {0xE6C, 0x01C00010},
147028fa281SKalle Valo 	{0xE70, 0x01C00010}, {0xE74, 0x02000010},
148028fa281SKalle Valo 	{0xE78, 0x02000010}, {0xE7C, 0x02000010},
149028fa281SKalle Valo 	{0xE80, 0x02000010}, {0xE84, 0x01C00010},
150028fa281SKalle Valo 	{0xE88, 0x02000010}, {0xE8C, 0x01C00010},
151028fa281SKalle Valo 	{0xED0, 0x01C00010}, {0xED4, 0x01C00010},
152028fa281SKalle Valo 	{0xED8, 0x01C00010}, {0xEDC, 0x00000010},
153028fa281SKalle Valo 	{0xEE0, 0x00000010}, {0xEEC, 0x03C00010},
154028fa281SKalle Valo 	{0xF14, 0x00000003}, {0xF00, 0x00100300},
155028fa281SKalle Valo 	{0xF08, 0x0000800B}, {0xF0C, 0x0000F007},
156028fa281SKalle Valo 	{0xF10, 0x0000A487}, {0xF1C, 0x80000064},
157028fa281SKalle Valo 	{0xF38, 0x00030155}, {0xF3C, 0x0000003A},
158028fa281SKalle Valo 	{0xF4C, 0x13000000}, {0xF50, 0x00000000},
159028fa281SKalle Valo 	{0xF18, 0x00000000},
160028fa281SKalle Valo 	{0xffff, 0xffffffff},
161028fa281SKalle Valo };
162028fa281SKalle Valo 
163028fa281SKalle Valo /* If updating the phy init tables, also update rtl8710b_revise_cck_tx_psf(). */
164028fa281SKalle Valo static const struct rtl8xxxu_reg32val rtl8710bu_qfn48m_s_phy_init_table[] = {
165028fa281SKalle Valo 	{0x800, 0x80045700}, {0x804, 0x00000001},
166028fa281SKalle Valo 	{0x808, 0x00FC8000}, {0x80C, 0x0000000A},
167028fa281SKalle Valo 	{0x810, 0x10001331}, {0x814, 0x020C3D10},
168028fa281SKalle Valo 	{0x818, 0x00200385}, {0x81C, 0x00000000},
169028fa281SKalle Valo 	{0x820, 0x01000100}, {0x824, 0x00390204},
170028fa281SKalle Valo 	{0x828, 0x00000000}, {0x82C, 0x00000000},
171028fa281SKalle Valo 	{0x830, 0x00000000}, {0x834, 0x00000000},
172028fa281SKalle Valo 	{0x838, 0x00000000}, {0x83C, 0x00000000},
173028fa281SKalle Valo 	{0x840, 0x00010000}, {0x844, 0x00000000},
174028fa281SKalle Valo 	{0x848, 0x00000000}, {0x84C, 0x00000000},
175028fa281SKalle Valo 	{0x850, 0x00030000}, {0x854, 0x00000000},
176028fa281SKalle Valo 	{0x858, 0x7E1A569A}, {0x85C, 0x569A569A},
177028fa281SKalle Valo 	{0x860, 0x00000130}, {0x864, 0x20000000},
178028fa281SKalle Valo 	{0x868, 0x00000000}, {0x86C, 0x27272700},
179028fa281SKalle Valo 	{0x870, 0x00050000}, {0x874, 0x25005000},
180028fa281SKalle Valo 	{0x878, 0x00000808}, {0x87C, 0x004F0201},
181028fa281SKalle Valo 	{0x880, 0xB0000B1E}, {0x884, 0x00000007},
182028fa281SKalle Valo 	{0x888, 0x00000000}, {0x88C, 0xCCC400C0},
183028fa281SKalle Valo 	{0x890, 0x00000800}, {0x894, 0xFFFFFFFE},
184028fa281SKalle Valo 	{0x898, 0x40302010}, {0x89C, 0x00706050},
185028fa281SKalle Valo 	{0x900, 0x00000000}, {0x904, 0x00000023},
186028fa281SKalle Valo 	{0x908, 0x00000000}, {0x90C, 0x81121111},
187028fa281SKalle Valo 	{0x910, 0x00000402}, {0x914, 0x00000201},
188028fa281SKalle Valo 	{0x920, 0x18C6318C}, {0x924, 0x0000018C},
189028fa281SKalle Valo 	{0x948, 0x99000000}, {0x94C, 0x00000010},
190028fa281SKalle Valo 	{0x950, 0x00003000}, {0x954, 0x5A880000},
191028fa281SKalle Valo 	{0x958, 0x4BC6D87A}, {0x95C, 0x04EB9B79},
192028fa281SKalle Valo 	{0x96C, 0x00000003}, {0x970, 0x00000000},
193028fa281SKalle Valo 	{0x974, 0x00000000}, {0x978, 0x00000000},
194028fa281SKalle Valo 	{0x97C, 0x13000000}, {0x980, 0x00000000},
195028fa281SKalle Valo 	{0xA00, 0x00D046C8}, {0xA04, 0x80FF800C},
196028fa281SKalle Valo 	{0xA08, 0x84838300}, {0xA0C, 0x2A20100F},
197028fa281SKalle Valo 	{0xA10, 0x9500BB78}, {0xA14, 0x1114D028},
198028fa281SKalle Valo 	{0xA18, 0x00881117}, {0xA1C, 0x89140F00},
199028fa281SKalle Valo 	{0xA20, 0xE82C0001}, {0xA24, 0x64B80C1C},
200028fa281SKalle Valo 	{0xA28, 0x00008810}, {0xA2C, 0x00D30000},
201028fa281SKalle Valo 	{0xA70, 0x101FBF00}, {0xA74, 0x00000007},
202028fa281SKalle Valo 	{0xA78, 0x00000900}, {0xA7C, 0x225B0606},
203028fa281SKalle Valo 	{0xA80, 0x218075B1}, {0xA84, 0x00200000},
204028fa281SKalle Valo 	{0xA88, 0x040C0000}, {0xA8C, 0x12345678},
205028fa281SKalle Valo 	{0xA90, 0xABCDEF00}, {0xA94, 0x001B1B89},
206028fa281SKalle Valo 	{0xA98, 0x00000000}, {0xA9C, 0x80020000},
207028fa281SKalle Valo 	{0xAA0, 0x00000000}, {0xAA4, 0x0000000C},
208028fa281SKalle Valo 	{0xAA8, 0xCA110058}, {0xAAC, 0x01235667},
209028fa281SKalle Valo 	{0xAB0, 0x00000000}, {0xAB4, 0x20201402},
210028fa281SKalle Valo 	{0xB2C, 0x00000000}, {0xC00, 0x48071D40},
211028fa281SKalle Valo 	{0xC04, 0x03A05611}, {0xC08, 0x000000E4},
212028fa281SKalle Valo 	{0xC0C, 0x6C6C6C6C}, {0xC10, 0x18800000},
213028fa281SKalle Valo 	{0xC14, 0x40000100}, {0xC18, 0x08800000},
214028fa281SKalle Valo 	{0xC1C, 0x40000100}, {0xC20, 0x00000000},
215028fa281SKalle Valo 	{0xC24, 0x00000000}, {0xC28, 0x00000000},
216028fa281SKalle Valo 	{0xC2C, 0x00000000}, {0xC30, 0x69E9AC4A},
217028fa281SKalle Valo 	{0xC34, 0x31000040}, {0xC38, 0x21688080},
218028fa281SKalle Valo 	{0xC3C, 0x0000170C}, {0xC40, 0x1F78403F},
219028fa281SKalle Valo 	{0xC44, 0x00010036}, {0xC48, 0xEC020107},
220028fa281SKalle Valo 	{0xC4C, 0x007F037F}, {0xC50, 0x69553420},
221028fa281SKalle Valo 	{0xC54, 0x43BC0094}, {0xC58, 0x00013169},
222028fa281SKalle Valo 	{0xC5C, 0x00250492}, {0xC60, 0x00280A00},
223028fa281SKalle Valo 	{0xC64, 0x7112848B}, {0xC68, 0x47C074FF},
224028fa281SKalle Valo 	{0xC6C, 0x00000036}, {0xC70, 0x2C7F000D},
225028fa281SKalle Valo 	{0xC74, 0x020600DB}, {0xC78, 0x0000001F},
226028fa281SKalle Valo 	{0xC7C, 0x00B91612}, {0xC80, 0x390000E4},
227028fa281SKalle Valo 	{0xC84, 0x11F60000}, {0xC88, 0x1051B75F},
228028fa281SKalle Valo 	{0xC8C, 0x20200109}, {0xC90, 0x00091521},
229028fa281SKalle Valo 	{0xC94, 0x00000000}, {0xC98, 0x00121820},
230028fa281SKalle Valo 	{0xC9C, 0x00007F7F}, {0xCA0, 0x00011000},
231028fa281SKalle Valo 	{0xCA4, 0x800000A0}, {0xCA8, 0x84E6C606},
232028fa281SKalle Valo 	{0xCAC, 0x00000060}, {0xCB0, 0x00000000},
233028fa281SKalle Valo 	{0xCB4, 0x00000000}, {0xCB8, 0x00000000},
234028fa281SKalle Valo 	{0xCBC, 0x28000000}, {0xCC0, 0x1051B75F},
235028fa281SKalle Valo 	{0xCC4, 0x00000109}, {0xCC8, 0x000442D6},
236028fa281SKalle Valo 	{0xCCC, 0x00000000}, {0xCD0, 0x000001C8},
237028fa281SKalle Valo 	{0xCD4, 0x001C8000}, {0xCD8, 0x00000100},
238028fa281SKalle Valo 	{0xCDC, 0x40100000}, {0xCE0, 0x00222220},
239028fa281SKalle Valo 	{0xCE4, 0x10000000}, {0xCE8, 0x37644302},
240028fa281SKalle Valo 	{0xCEC, 0x2F97D40C}, {0xD00, 0x04030740},
241028fa281SKalle Valo 	{0xD04, 0x40020401}, {0xD08, 0x0000907F},
242028fa281SKalle Valo 	{0xD0C, 0x20010201}, {0xD10, 0xA0633333},
243028fa281SKalle Valo 	{0xD14, 0x3333BC53}, {0xD18, 0x7A8F5B6F},
244028fa281SKalle Valo 	{0xD2C, 0xCB979975}, {0xD30, 0x00000000},
245028fa281SKalle Valo 	{0xD34, 0x40608000}, {0xD38, 0x88000000},
246028fa281SKalle Valo 	{0xD3C, 0xC0127353}, {0xD40, 0x00000000},
247028fa281SKalle Valo 	{0xD44, 0x00000000}, {0xD48, 0x00000000},
248028fa281SKalle Valo 	{0xD4C, 0x00000000}, {0xD50, 0x00006528},
249028fa281SKalle Valo 	{0xD54, 0x00000000}, {0xD58, 0x00000282},
250028fa281SKalle Valo 	{0xD5C, 0x30032064}, {0xD60, 0x4653DE68},
251028fa281SKalle Valo 	{0xD64, 0x04518A3C}, {0xD68, 0x00002101},
252028fa281SKalle Valo 	{0xE00, 0x2D2D2D2D}, {0xE04, 0x2D2D2D2D},
253028fa281SKalle Valo 	{0xE08, 0x0390272D}, {0xE10, 0x2D2D2D2D},
254028fa281SKalle Valo 	{0xE14, 0x2D2D2D2D}, {0xE18, 0x2D2D2D2D},
255028fa281SKalle Valo 	{0xE1C, 0x2D2D2D2D}, {0xE28, 0x00000000},
256028fa281SKalle Valo 	{0xE30, 0x1000DC1F}, {0xE34, 0x10008C1F},
257028fa281SKalle Valo 	{0xE38, 0x02140102}, {0xE3C, 0x681604C2},
258028fa281SKalle Valo 	{0xE40, 0x01007C00}, {0xE44, 0x01004800},
259028fa281SKalle Valo 	{0xE48, 0xFB000000}, {0xE4C, 0x000028D1},
260028fa281SKalle Valo 	{0xE50, 0x1000DC1F}, {0xE54, 0x10008C1F},
261028fa281SKalle Valo 	{0xE58, 0x02140102}, {0xE5C, 0x28160D05},
262028fa281SKalle Valo 	{0xE60, 0x0000C008}, {0xE68, 0x001B25A4},
263028fa281SKalle Valo 	{0xE64, 0x281600A0}, {0xE6C, 0x01C00010},
264028fa281SKalle Valo 	{0xE70, 0x01C00010}, {0xE74, 0x02000010},
265028fa281SKalle Valo 	{0xE78, 0x02000010}, {0xE7C, 0x02000010},
266028fa281SKalle Valo 	{0xE80, 0x02000010}, {0xE84, 0x01C00010},
267028fa281SKalle Valo 	{0xE88, 0x02000010}, {0xE8C, 0x01C00010},
268028fa281SKalle Valo 	{0xED0, 0x01C00010}, {0xED4, 0x01C00010},
269028fa281SKalle Valo 	{0xED8, 0x01C00010}, {0xEDC, 0x00000010},
270028fa281SKalle Valo 	{0xEE0, 0x00000010}, {0xEEC, 0x03C00010},
271028fa281SKalle Valo 	{0xF14, 0x00000003}, {0xF00, 0x00100300},
272028fa281SKalle Valo 	{0xF08, 0x0000800B}, {0xF0C, 0x0000F007},
273028fa281SKalle Valo 	{0xF10, 0x0000A487}, {0xF1C, 0x80000064},
274028fa281SKalle Valo 	{0xF38, 0x00030155}, {0xF3C, 0x0000003A},
275028fa281SKalle Valo 	{0xF4C, 0x13000000}, {0xF50, 0x00000000},
276028fa281SKalle Valo 	{0xF18, 0x00000000},
277028fa281SKalle Valo 	{0xffff, 0xffffffff},
278028fa281SKalle Valo };
279028fa281SKalle Valo 
280028fa281SKalle Valo static const struct rtl8xxxu_reg32val rtl8710b_agc_table[] = {
281028fa281SKalle Valo 	{0xC78, 0xFC000001}, {0xC78, 0xFB010001},
282028fa281SKalle Valo 	{0xC78, 0xFA020001}, {0xC78, 0xF9030001},
283028fa281SKalle Valo 	{0xC78, 0xF8040001}, {0xC78, 0xF7050001},
284028fa281SKalle Valo 	{0xC78, 0xF6060001}, {0xC78, 0xF5070001},
285028fa281SKalle Valo 	{0xC78, 0xF4080001}, {0xC78, 0xF3090001},
286028fa281SKalle Valo 	{0xC78, 0xF20A0001}, {0xC78, 0xF10B0001},
287028fa281SKalle Valo 	{0xC78, 0xF00C0001}, {0xC78, 0xEF0D0001},
288028fa281SKalle Valo 	{0xC78, 0xEE0E0001}, {0xC78, 0xED0F0001},
289028fa281SKalle Valo 	{0xC78, 0xEC100001}, {0xC78, 0xEB110001},
290028fa281SKalle Valo 	{0xC78, 0xEA120001}, {0xC78, 0xE9130001},
291028fa281SKalle Valo 	{0xC78, 0xE8140001}, {0xC78, 0xE7150001},
292028fa281SKalle Valo 	{0xC78, 0xE6160001}, {0xC78, 0xE5170001},
293028fa281SKalle Valo 	{0xC78, 0xE4180001}, {0xC78, 0xE3190001},
294028fa281SKalle Valo 	{0xC78, 0xE21A0001}, {0xC78, 0xE11B0001},
295028fa281SKalle Valo 	{0xC78, 0xE01C0001}, {0xC78, 0xC31D0001},
296028fa281SKalle Valo 	{0xC78, 0xC21E0001}, {0xC78, 0xC11F0001},
297028fa281SKalle Valo 	{0xC78, 0xC0200001}, {0xC78, 0xA3210001},
298028fa281SKalle Valo 	{0xC78, 0xA2220001}, {0xC78, 0xA1230001},
299028fa281SKalle Valo 	{0xC78, 0xA0240001}, {0xC78, 0x86250001},
300028fa281SKalle Valo 	{0xC78, 0x85260001}, {0xC78, 0x84270001},
301028fa281SKalle Valo 	{0xC78, 0x83280001}, {0xC78, 0x82290001},
302028fa281SKalle Valo 	{0xC78, 0x812A0001}, {0xC78, 0x802B0001},
303028fa281SKalle Valo 	{0xC78, 0x632C0001}, {0xC78, 0x622D0001},
304028fa281SKalle Valo 	{0xC78, 0x612E0001}, {0xC78, 0x602F0001},
305028fa281SKalle Valo 	{0xC78, 0x42300001}, {0xC78, 0x41310001},
306028fa281SKalle Valo 	{0xC78, 0x40320001}, {0xC78, 0x23330001},
307028fa281SKalle Valo 	{0xC78, 0x22340001}, {0xC78, 0x21350001},
308028fa281SKalle Valo 	{0xC78, 0x20360001}, {0xC78, 0x02370001},
309028fa281SKalle Valo 	{0xC78, 0x01380001}, {0xC78, 0x00390001},
310028fa281SKalle Valo 	{0xC78, 0x003A0001}, {0xC78, 0x003B0001},
311028fa281SKalle Valo 	{0xC78, 0x003C0001}, {0xC78, 0x003D0001},
312028fa281SKalle Valo 	{0xC78, 0x003E0001}, {0xC78, 0x003F0001},
313028fa281SKalle Valo 	{0xC78, 0xF7400001}, {0xC78, 0xF7410001},
314028fa281SKalle Valo 	{0xC78, 0xF7420001}, {0xC78, 0xF7430001},
315028fa281SKalle Valo 	{0xC78, 0xF7440001}, {0xC78, 0xF7450001},
316028fa281SKalle Valo 	{0xC78, 0xF7460001}, {0xC78, 0xF7470001},
317028fa281SKalle Valo 	{0xC78, 0xF7480001}, {0xC78, 0xF6490001},
318028fa281SKalle Valo 	{0xC78, 0xF34A0001}, {0xC78, 0xF24B0001},
319028fa281SKalle Valo 	{0xC78, 0xF14C0001}, {0xC78, 0xF04D0001},
320028fa281SKalle Valo 	{0xC78, 0xD14E0001}, {0xC78, 0xD04F0001},
321028fa281SKalle Valo 	{0xC78, 0xB5500001}, {0xC78, 0xB4510001},
322028fa281SKalle Valo 	{0xC78, 0xB3520001}, {0xC78, 0xB2530001},
323028fa281SKalle Valo 	{0xC78, 0xB1540001}, {0xC78, 0xB0550001},
324028fa281SKalle Valo 	{0xC78, 0xAF560001}, {0xC78, 0xAE570001},
325028fa281SKalle Valo 	{0xC78, 0xAD580001}, {0xC78, 0xAC590001},
326028fa281SKalle Valo 	{0xC78, 0xAB5A0001}, {0xC78, 0xAA5B0001},
327028fa281SKalle Valo 	{0xC78, 0xA95C0001}, {0xC78, 0xA85D0001},
328028fa281SKalle Valo 	{0xC78, 0xA75E0001}, {0xC78, 0xA65F0001},
329028fa281SKalle Valo 	{0xC78, 0xA5600001}, {0xC78, 0xA4610001},
330028fa281SKalle Valo 	{0xC78, 0xA3620001}, {0xC78, 0xA2630001},
331028fa281SKalle Valo 	{0xC78, 0xA1640001}, {0xC78, 0xA0650001},
332028fa281SKalle Valo 	{0xC78, 0x87660001}, {0xC78, 0x86670001},
333028fa281SKalle Valo 	{0xC78, 0x85680001}, {0xC78, 0x84690001},
334028fa281SKalle Valo 	{0xC78, 0x836A0001}, {0xC78, 0x826B0001},
335028fa281SKalle Valo 	{0xC78, 0x816C0001}, {0xC78, 0x806D0001},
336028fa281SKalle Valo 	{0xC78, 0x636E0001}, {0xC78, 0x626F0001},
337028fa281SKalle Valo 	{0xC78, 0x61700001}, {0xC78, 0x60710001},
338028fa281SKalle Valo 	{0xC78, 0x42720001}, {0xC78, 0x41730001},
339028fa281SKalle Valo 	{0xC78, 0x40740001}, {0xC78, 0x23750001},
340028fa281SKalle Valo 	{0xC78, 0x22760001}, {0xC78, 0x21770001},
341028fa281SKalle Valo 	{0xC78, 0x20780001}, {0xC78, 0x03790001},
342028fa281SKalle Valo 	{0xC78, 0x027A0001}, {0xC78, 0x017B0001},
343028fa281SKalle Valo 	{0xC78, 0x007C0001}, {0xC78, 0x007D0001},
344028fa281SKalle Valo 	{0xC78, 0x007E0001}, {0xC78, 0x007F0001},
345028fa281SKalle Valo 	{0xC50, 0x69553422}, {0xC50, 0x69553420},
346028fa281SKalle Valo 	{0xffff, 0xffffffff}
347028fa281SKalle Valo };
348028fa281SKalle Valo 
349028fa281SKalle Valo static const struct rtl8xxxu_rfregval rtl8710bu_qfn48m_u_radioa_init_table[] = {
350028fa281SKalle Valo 	{0x00, 0x00030000}, {0x08, 0x00008400},
351028fa281SKalle Valo 	{0x17, 0x00000000}, {0x18, 0x00000C01},
352028fa281SKalle Valo 	{0x19, 0x000739D2}, {0x1C, 0x00000C4C},
353028fa281SKalle Valo 	{0x1B, 0x00000C6C}, {0x1E, 0x00080009},
354028fa281SKalle Valo 	{0x1F, 0x00000880}, {0x2F, 0x0001A060},
355028fa281SKalle Valo 	{0x3F, 0x00015000}, {0x42, 0x000060C0},
356028fa281SKalle Valo 	{0x57, 0x000D0000}, {0x58, 0x000C0160},
357028fa281SKalle Valo 	{0x67, 0x00001552}, {0x83, 0x00000000},
358028fa281SKalle Valo 	{0xB0, 0x000FF9F0}, {0xB1, 0x00010018},
359028fa281SKalle Valo 	{0xB2, 0x00054C00}, {0xB4, 0x0004486B},
360028fa281SKalle Valo 	{0xB5, 0x0000112A}, {0xB6, 0x0000053E},
361028fa281SKalle Valo 	{0xB7, 0x00014408}, {0xB8, 0x00010200},
362028fa281SKalle Valo 	{0xB9, 0x00080801}, {0xBA, 0x00040001},
363028fa281SKalle Valo 	{0xBB, 0x00000400}, {0xBF, 0x000C0000},
364028fa281SKalle Valo 	{0xC2, 0x00002400}, {0xC3, 0x00000009},
365028fa281SKalle Valo 	{0xC4, 0x00040C91}, {0xC5, 0x00099999},
366028fa281SKalle Valo 	{0xC6, 0x000000A3}, {0xC7, 0x00088820},
367028fa281SKalle Valo 	{0xC8, 0x00076C06}, {0xC9, 0x00000000},
368028fa281SKalle Valo 	{0xCA, 0x00080000}, {0xDF, 0x00000180},
369028fa281SKalle Valo 	{0xEF, 0x000001A8}, {0x3D, 0x00000003},
370028fa281SKalle Valo 	{0x3D, 0x00080003}, {0x51, 0x000F1E69},
371028fa281SKalle Valo 	{0x52, 0x000FBF6C}, {0x53, 0x0000032F},
372028fa281SKalle Valo 	{0x54, 0x00055007}, {0x56, 0x000517F0},
373028fa281SKalle Valo 	{0x35, 0x000000F4}, {0x35, 0x00000179},
374028fa281SKalle Valo 	{0x35, 0x000002F4}, {0x36, 0x00000BF8},
375028fa281SKalle Valo 	{0x36, 0x00008BF8}, {0x36, 0x00010BF8},
376028fa281SKalle Valo 	{0x36, 0x00018BF8}, {0x18, 0x00000C01},
377028fa281SKalle Valo 	{0x5A, 0x00048000}, {0x5A, 0x00048000},
378028fa281SKalle Valo 	{0x34, 0x0000ADF5}, {0x34, 0x00009DF2},
379028fa281SKalle Valo 	{0x34, 0x00008DEF}, {0x34, 0x00007DEC},
380028fa281SKalle Valo 	{0x34, 0x00006DE9}, {0x34, 0x00005CEC},
381028fa281SKalle Valo 	{0x34, 0x00004CE9}, {0x34, 0x00003C6C},
382028fa281SKalle Valo 	{0x34, 0x00002C69}, {0x34, 0x0000106E},
383028fa281SKalle Valo 	{0x34, 0x0000006B}, {0x84, 0x00048000},
384028fa281SKalle Valo 	{0x87, 0x00000065}, {0x8E, 0x00065540},
385028fa281SKalle Valo 	{0xDF, 0x00000110}, {0x86, 0x0000002A},
386028fa281SKalle Valo 	{0x8F, 0x00088000}, {0x81, 0x0003FD80},
387028fa281SKalle Valo 	{0xEF, 0x00082000}, {0x3B, 0x000F0F00},
388028fa281SKalle Valo 	{0x3B, 0x000E0E00}, {0x3B, 0x000DFE00},
389028fa281SKalle Valo 	{0x3B, 0x000C0D00}, {0x3B, 0x000B0C00},
390028fa281SKalle Valo 	{0x3B, 0x000A0500}, {0x3B, 0x00090400},
391028fa281SKalle Valo 	{0x3B, 0x00080000}, {0x3B, 0x00070F00},
392028fa281SKalle Valo 	{0x3B, 0x00060E00}, {0x3B, 0x00050A00},
393028fa281SKalle Valo 	{0x3B, 0x00040D00}, {0x3B, 0x00030C00},
394028fa281SKalle Valo 	{0x3B, 0x00020500}, {0x3B, 0x00010400},
395028fa281SKalle Valo 	{0x3B, 0x00000000}, {0xEF, 0x00080000},
396028fa281SKalle Valo 	{0xEF, 0x00088000}, {0x3B, 0x00000170},
397028fa281SKalle Valo 	{0x3B, 0x000C0030}, {0xEF, 0x00080000},
398028fa281SKalle Valo 	{0xEF, 0x00080000}, {0x30, 0x00010000},
399028fa281SKalle Valo 	{0x31, 0x0000000F}, {0x32, 0x00047EFE},
400028fa281SKalle Valo 	{0xEF, 0x00000000}, {0x00, 0x00010159},
401028fa281SKalle Valo 	{0x18, 0x0000FC01}, {0xFE, 0x00000000},
402028fa281SKalle Valo 	{0x00, 0x00033D95},
403028fa281SKalle Valo 	{0xff, 0xffffffff}
404028fa281SKalle Valo };
405028fa281SKalle Valo 
406028fa281SKalle Valo static const struct rtl8xxxu_rfregval rtl8710bu_qfn48m_s_radioa_init_table[] = {
407028fa281SKalle Valo 	{0x00, 0x00030000}, {0x08, 0x00008400},
408028fa281SKalle Valo 	{0x17, 0x00000000}, {0x18, 0x00000C01},
409028fa281SKalle Valo 	{0x19, 0x000739D2}, {0x1C, 0x00000C4C},
410028fa281SKalle Valo 	{0x1B, 0x00000C6C}, {0x1E, 0x00080009},
411028fa281SKalle Valo 	{0x1F, 0x00000880}, {0x2F, 0x0001A060},
412028fa281SKalle Valo 	{0x3F, 0x00015000}, {0x42, 0x000060C0},
413028fa281SKalle Valo 	{0x57, 0x000D0000}, {0x58, 0x000C0160},
414028fa281SKalle Valo 	{0x67, 0x00001552}, {0x83, 0x00000000},
415028fa281SKalle Valo 	{0xB0, 0x000FF9F0}, {0xB1, 0x00010018},
416028fa281SKalle Valo 	{0xB2, 0x00054C00}, {0xB4, 0x0004486B},
417028fa281SKalle Valo 	{0xB5, 0x0000112A}, {0xB6, 0x0000053E},
418028fa281SKalle Valo 	{0xB7, 0x00014408}, {0xB8, 0x00010200},
419028fa281SKalle Valo 	{0xB9, 0x00080801}, {0xBA, 0x00040001},
420028fa281SKalle Valo 	{0xBB, 0x00000400}, {0xBF, 0x000C0000},
421028fa281SKalle Valo 	{0xC2, 0x00002400}, {0xC3, 0x00000009},
422028fa281SKalle Valo 	{0xC4, 0x00040C91}, {0xC5, 0x00099999},
423028fa281SKalle Valo 	{0xC6, 0x000000A3}, {0xC7, 0x00088820},
424028fa281SKalle Valo 	{0xC8, 0x00076C06}, {0xC9, 0x00000000},
425028fa281SKalle Valo 	{0xCA, 0x00080000}, {0xDF, 0x00000180},
426028fa281SKalle Valo 	{0xEF, 0x000001A8}, {0x3D, 0x00000003},
427028fa281SKalle Valo 	{0x3D, 0x00080003}, {0x51, 0x000F1E69},
428028fa281SKalle Valo 	{0x52, 0x000FBF6C}, {0x53, 0x0000032F},
429028fa281SKalle Valo 	{0x54, 0x00055007}, {0x56, 0x000517F0},
430028fa281SKalle Valo 	{0x35, 0x000000F4}, {0x35, 0x00000179},
431028fa281SKalle Valo 	{0x35, 0x000002F4}, {0x36, 0x00000BF8},
432028fa281SKalle Valo 	{0x36, 0x00008BF8}, {0x36, 0x00010BF8},
433028fa281SKalle Valo 	{0x36, 0x00018BF8}, {0x18, 0x00000C01},
434028fa281SKalle Valo 	{0x5A, 0x00048000}, {0x5A, 0x00048000},
435028fa281SKalle Valo 	{0x34, 0x0000ADF5}, {0x34, 0x00009DF2},
436028fa281SKalle Valo 	{0x34, 0x00008DEF}, {0x34, 0x00007DEC},
437028fa281SKalle Valo 	{0x34, 0x00006DE9}, {0x34, 0x00005CEC},
438028fa281SKalle Valo 	{0x34, 0x00004CE9}, {0x34, 0x00003C6C},
439028fa281SKalle Valo 	{0x34, 0x00002C69}, {0x34, 0x0000106E},
440028fa281SKalle Valo 	{0x34, 0x0000006B}, {0x84, 0x00048000},
441028fa281SKalle Valo 	{0x87, 0x00000065}, {0x8E, 0x00065540},
442028fa281SKalle Valo 	{0xDF, 0x00000110}, {0x86, 0x0000002A},
443028fa281SKalle Valo 	{0x8F, 0x00088000}, {0x81, 0x0003FD80},
444028fa281SKalle Valo 	{0xEF, 0x00082000}, {0x3B, 0x000F0F00},
445028fa281SKalle Valo 	{0x3B, 0x000E0E00}, {0x3B, 0x000DFE00},
446028fa281SKalle Valo 	{0x3B, 0x000C0D00}, {0x3B, 0x000B0C00},
447028fa281SKalle Valo 	{0x3B, 0x000A0500}, {0x3B, 0x00090400},
448028fa281SKalle Valo 	{0x3B, 0x00080000}, {0x3B, 0x00070F00},
449028fa281SKalle Valo 	{0x3B, 0x00060E00}, {0x3B, 0x00050A00},
450028fa281SKalle Valo 	{0x3B, 0x00040D00}, {0x3B, 0x00030C00},
451028fa281SKalle Valo 	{0x3B, 0x00020500}, {0x3B, 0x00010400},
452028fa281SKalle Valo 	{0x3B, 0x00000000}, {0xEF, 0x00080000},
453028fa281SKalle Valo 	{0xEF, 0x00088000}, {0x3B, 0x000000B0},
454028fa281SKalle Valo 	{0x3B, 0x000C0030}, {0xEF, 0x00080000},
455028fa281SKalle Valo 	{0xEF, 0x00080000}, {0x30, 0x00010000},
456028fa281SKalle Valo 	{0x31, 0x0000000F}, {0x32, 0x00047EFE},
457028fa281SKalle Valo 	{0xEF, 0x00000000}, {0x00, 0x00010159},
458028fa281SKalle Valo 	{0x18, 0x0000FC01}, {0xFE, 0x00000000},
459028fa281SKalle Valo 	{0x00, 0x00033D95},
460028fa281SKalle Valo 	{0xff, 0xffffffff}
461028fa281SKalle Valo };
462028fa281SKalle Valo 
rtl8710b_indirect_read32(struct rtl8xxxu_priv * priv,u32 addr)463028fa281SKalle Valo static u32 rtl8710b_indirect_read32(struct rtl8xxxu_priv *priv, u32 addr)
464028fa281SKalle Valo {
465028fa281SKalle Valo 	struct device *dev = &priv->udev->dev;
466028fa281SKalle Valo 	u32 val32, value = 0xffffffff;
467028fa281SKalle Valo 	u8 polling_count = 0xff;
468028fa281SKalle Valo 
469028fa281SKalle Valo 	if (!IS_ALIGNED(addr, 4)) {
470028fa281SKalle Valo 		dev_warn(dev, "%s: Aborting because 0x%x is not a multiple of 4.\n",
471028fa281SKalle Valo 			 __func__, addr);
472028fa281SKalle Valo 		return value;
473028fa281SKalle Valo 	}
474028fa281SKalle Valo 
475028fa281SKalle Valo 	mutex_lock(&priv->syson_indirect_access_mutex);
476028fa281SKalle Valo 
477028fa281SKalle Valo 	rtl8xxxu_write32(priv, REG_USB_HOST_INDIRECT_ADDR_8710B, addr);
478028fa281SKalle Valo 	rtl8xxxu_write32(priv, REG_EFUSE_INDIRECT_CTRL_8710B, NORMAL_REG_READ_OFFSET);
479028fa281SKalle Valo 
480028fa281SKalle Valo 	do
481028fa281SKalle Valo 		val32 = rtl8xxxu_read32(priv, REG_EFUSE_INDIRECT_CTRL_8710B);
482028fa281SKalle Valo 	while ((val32 & BIT(31)) && (--polling_count > 0));
483028fa281SKalle Valo 
484028fa281SKalle Valo 	if (polling_count == 0)
485028fa281SKalle Valo 		dev_warn(dev, "%s: Failed to read from 0x%x, 0x806c = 0x%x\n",
486028fa281SKalle Valo 			 __func__, addr, val32);
487028fa281SKalle Valo 	else
488028fa281SKalle Valo 		value = rtl8xxxu_read32(priv, REG_USB_HOST_INDIRECT_DATA_8710B);
489028fa281SKalle Valo 
490028fa281SKalle Valo 	mutex_unlock(&priv->syson_indirect_access_mutex);
491028fa281SKalle Valo 
492028fa281SKalle Valo 	if (rtl8xxxu_debug & RTL8XXXU_DEBUG_REG_READ)
493028fa281SKalle Valo 		dev_info(dev, "%s(%04x) = 0x%08x\n", __func__, addr, value);
494028fa281SKalle Valo 
495028fa281SKalle Valo 	return value;
496028fa281SKalle Valo }
497028fa281SKalle Valo 
rtl8710b_indirect_write32(struct rtl8xxxu_priv * priv,u32 addr,u32 val)498028fa281SKalle Valo static void rtl8710b_indirect_write32(struct rtl8xxxu_priv *priv, u32 addr, u32 val)
499028fa281SKalle Valo {
500028fa281SKalle Valo 	struct device *dev = &priv->udev->dev;
501028fa281SKalle Valo 	u8 polling_count = 0xff;
502028fa281SKalle Valo 	u32 val32;
503028fa281SKalle Valo 
504028fa281SKalle Valo 	if (!IS_ALIGNED(addr, 4)) {
505028fa281SKalle Valo 		dev_warn(dev, "%s: Aborting because 0x%x is not a multiple of 4.\n",
506028fa281SKalle Valo 			 __func__, addr);
507028fa281SKalle Valo 		return;
508028fa281SKalle Valo 	}
509028fa281SKalle Valo 
510028fa281SKalle Valo 	mutex_lock(&priv->syson_indirect_access_mutex);
511028fa281SKalle Valo 
512028fa281SKalle Valo 	rtl8xxxu_write32(priv, REG_USB_HOST_INDIRECT_ADDR_8710B, addr);
513028fa281SKalle Valo 	rtl8xxxu_write32(priv, REG_USB_HOST_INDIRECT_DATA_8710B, val);
514028fa281SKalle Valo 	rtl8xxxu_write32(priv, REG_EFUSE_INDIRECT_CTRL_8710B, NORMAL_REG_WRITE_OFFSET);
515028fa281SKalle Valo 
516028fa281SKalle Valo 	do
517028fa281SKalle Valo 		val32 = rtl8xxxu_read32(priv, REG_EFUSE_INDIRECT_CTRL_8710B);
518028fa281SKalle Valo 	while ((val32 & BIT(31)) && (--polling_count > 0));
519028fa281SKalle Valo 
520028fa281SKalle Valo 	if (polling_count == 0)
521028fa281SKalle Valo 		dev_warn(dev, "%s: Failed to write 0x%x to 0x%x, 0x806c = 0x%x\n",
522028fa281SKalle Valo 			 __func__, val, addr, val32);
523028fa281SKalle Valo 
524028fa281SKalle Valo 	mutex_unlock(&priv->syson_indirect_access_mutex);
525028fa281SKalle Valo 
526028fa281SKalle Valo 	if (rtl8xxxu_debug & RTL8XXXU_DEBUG_REG_WRITE)
527028fa281SKalle Valo 		dev_info(dev, "%s(%04x) = 0x%08x\n", __func__, addr, val);
528028fa281SKalle Valo }
529028fa281SKalle Valo 
rtl8710b_read_syson_reg(struct rtl8xxxu_priv * priv,u32 addr)530028fa281SKalle Valo static u32 rtl8710b_read_syson_reg(struct rtl8xxxu_priv *priv, u32 addr)
531028fa281SKalle Valo {
532028fa281SKalle Valo 	return rtl8710b_indirect_read32(priv, addr | SYSON_REG_BASE_ADDR_8710B);
533028fa281SKalle Valo }
534028fa281SKalle Valo 
rtl8710b_write_syson_reg(struct rtl8xxxu_priv * priv,u32 addr,u32 val)535028fa281SKalle Valo static void rtl8710b_write_syson_reg(struct rtl8xxxu_priv *priv, u32 addr, u32 val)
536028fa281SKalle Valo {
537028fa281SKalle Valo 	rtl8710b_indirect_write32(priv, addr | SYSON_REG_BASE_ADDR_8710B, val);
538028fa281SKalle Valo }
539028fa281SKalle Valo 
rtl8710b_read_efuse8(struct rtl8xxxu_priv * priv,u16 offset,u8 * data)540028fa281SKalle Valo static int rtl8710b_read_efuse8(struct rtl8xxxu_priv *priv, u16 offset, u8 *data)
541028fa281SKalle Valo {
542028fa281SKalle Valo 	u32 val32;
543028fa281SKalle Valo 	int i;
544028fa281SKalle Valo 
545028fa281SKalle Valo 	/* Write Address */
546028fa281SKalle Valo 	rtl8xxxu_write32(priv, REG_USB_HOST_INDIRECT_ADDR_8710B, offset);
547028fa281SKalle Valo 
548028fa281SKalle Valo 	rtl8xxxu_write32(priv, REG_EFUSE_INDIRECT_CTRL_8710B, EFUSE_READ_OFFSET);
549028fa281SKalle Valo 
550028fa281SKalle Valo 	/* Poll for data read */
551028fa281SKalle Valo 	val32 = rtl8xxxu_read32(priv, REG_EFUSE_INDIRECT_CTRL_8710B);
552028fa281SKalle Valo 	for (i = 0; i < RTL8XXXU_MAX_REG_POLL; i++) {
553028fa281SKalle Valo 		val32 = rtl8xxxu_read32(priv, REG_EFUSE_INDIRECT_CTRL_8710B);
554028fa281SKalle Valo 		if (!(val32 & BIT(31)))
555028fa281SKalle Valo 			break;
556028fa281SKalle Valo 	}
557028fa281SKalle Valo 
558028fa281SKalle Valo 	if (i == RTL8XXXU_MAX_REG_POLL)
559028fa281SKalle Valo 		return -EIO;
560028fa281SKalle Valo 
561028fa281SKalle Valo 	val32 = rtl8xxxu_read32(priv, REG_USB_HOST_INDIRECT_DATA_8710B);
562028fa281SKalle Valo 
563028fa281SKalle Valo 	*data = val32 & 0xff;
564028fa281SKalle Valo 	return 0;
565028fa281SKalle Valo }
566028fa281SKalle Valo 
567028fa281SKalle Valo #define EEPROM_PACKAGE_TYPE_8710B	0xF8
568028fa281SKalle Valo #define PACKAGE_QFN48M_U		0xee
569028fa281SKalle Valo #define PACKAGE_QFN48M_S		0xfe
570028fa281SKalle Valo 
rtl8710bu_identify_chip(struct rtl8xxxu_priv * priv)571028fa281SKalle Valo static int rtl8710bu_identify_chip(struct rtl8xxxu_priv *priv)
572028fa281SKalle Valo {
573028fa281SKalle Valo 	struct device *dev = &priv->udev->dev;
574028fa281SKalle Valo 	u32 cfg0, cfg2, vendor;
575028fa281SKalle Valo 	u8 package_type = 0x7; /* a nonsense value */
576028fa281SKalle Valo 
577028fa281SKalle Valo 	sprintf(priv->chip_name, "8710BU");
578028fa281SKalle Valo 	priv->rtl_chip = RTL8710B;
579028fa281SKalle Valo 	priv->rf_paths = 1;
580028fa281SKalle Valo 	priv->rx_paths = 1;
581028fa281SKalle Valo 	priv->tx_paths = 1;
582028fa281SKalle Valo 	priv->has_wifi = 1;
583028fa281SKalle Valo 
584028fa281SKalle Valo 	cfg0 = rtl8710b_read_syson_reg(priv, REG_SYS_SYSTEM_CFG0_8710B);
585028fa281SKalle Valo 	priv->chip_cut = cfg0 & 0xf;
586028fa281SKalle Valo 
587028fa281SKalle Valo 	if (cfg0 & BIT(16)) {
588028fa281SKalle Valo 		dev_info(dev, "%s: Unsupported test chip\n", __func__);
589028fa281SKalle Valo 		return -EOPNOTSUPP;
590028fa281SKalle Valo 	}
591028fa281SKalle Valo 
592028fa281SKalle Valo 	vendor = u32_get_bits(cfg0, 0xc0);
593028fa281SKalle Valo 
594028fa281SKalle Valo 	/* SMIC and TSMC are swapped compared to rtl8xxxu_identify_vendor_2bits */
595028fa281SKalle Valo 	switch (vendor) {
596028fa281SKalle Valo 	case 0:
597028fa281SKalle Valo 		sprintf(priv->chip_vendor, "SMIC");
598028fa281SKalle Valo 		priv->vendor_smic = 1;
599028fa281SKalle Valo 		break;
600028fa281SKalle Valo 	case 1:
601028fa281SKalle Valo 		sprintf(priv->chip_vendor, "TSMC");
602028fa281SKalle Valo 		break;
603028fa281SKalle Valo 	case 2:
604028fa281SKalle Valo 		sprintf(priv->chip_vendor, "UMC");
605028fa281SKalle Valo 		priv->vendor_umc = 1;
606028fa281SKalle Valo 		break;
607028fa281SKalle Valo 	default:
608028fa281SKalle Valo 		sprintf(priv->chip_vendor, "unknown");
609028fa281SKalle Valo 		break;
610028fa281SKalle Valo 	}
611028fa281SKalle Valo 
612028fa281SKalle Valo 	rtl8710b_read_efuse8(priv, EEPROM_PACKAGE_TYPE_8710B, &package_type);
613028fa281SKalle Valo 
614028fa281SKalle Valo 	if (package_type == 0xff) {
615028fa281SKalle Valo 		dev_warn(dev, "Package type is undefined. Assuming it based on the vendor.\n");
616028fa281SKalle Valo 
617028fa281SKalle Valo 		if (priv->vendor_umc) {
618028fa281SKalle Valo 			package_type = PACKAGE_QFN48M_U;
619028fa281SKalle Valo 		} else if (priv->vendor_smic) {
620028fa281SKalle Valo 			package_type = PACKAGE_QFN48M_S;
621028fa281SKalle Valo 		} else {
622028fa281SKalle Valo 			dev_warn(dev, "The vendor is neither UMC nor SMIC. Assuming the package type is QFN48M_U.\n");
623028fa281SKalle Valo 
624028fa281SKalle Valo 			/*
625028fa281SKalle Valo 			 * In this case the vendor driver doesn't set
626028fa281SKalle Valo 			 * the package type to anything, which is the
627028fa281SKalle Valo 			 * same as setting it to PACKAGE_DEFAULT (0).
628028fa281SKalle Valo 			 */
629028fa281SKalle Valo 			package_type = PACKAGE_QFN48M_U;
630028fa281SKalle Valo 		}
631028fa281SKalle Valo 	} else if (package_type != PACKAGE_QFN48M_S &&
632028fa281SKalle Valo 		   package_type != PACKAGE_QFN48M_U) {
633028fa281SKalle Valo 		dev_warn(dev, "Failed to read the package type. Assuming it's the default QFN48M_U.\n");
634028fa281SKalle Valo 
635028fa281SKalle Valo 		/*
636028fa281SKalle Valo 		 * In this case the vendor driver actually sets it to
637028fa281SKalle Valo 		 * PACKAGE_DEFAULT, but that selects the same values
638028fa281SKalle Valo 		 * from the init tables as PACKAGE_QFN48M_U.
639028fa281SKalle Valo 		 */
640028fa281SKalle Valo 		package_type = PACKAGE_QFN48M_U;
641028fa281SKalle Valo 	}
642028fa281SKalle Valo 
643028fa281SKalle Valo 	priv->package_type = package_type;
644028fa281SKalle Valo 
645028fa281SKalle Valo 	dev_dbg(dev, "Package type: 0x%x\n", package_type);
646028fa281SKalle Valo 
647028fa281SKalle Valo 	cfg2 = rtl8710b_read_syson_reg(priv, REG_SYS_SYSTEM_CFG2_8710B);
648028fa281SKalle Valo 	priv->rom_rev = cfg2 & 0xf;
649028fa281SKalle Valo 
650028fa281SKalle Valo 	return rtl8xxxu_config_endpoints_no_sie(priv);
651028fa281SKalle Valo }
652028fa281SKalle Valo 
rtl8710b_revise_cck_tx_psf(struct rtl8xxxu_priv * priv,u8 channel)653028fa281SKalle Valo static void rtl8710b_revise_cck_tx_psf(struct rtl8xxxu_priv *priv, u8 channel)
654028fa281SKalle Valo {
655028fa281SKalle Valo 	if (channel == 13) {
656028fa281SKalle Valo 		/* Normal values */
657028fa281SKalle Valo 		rtl8xxxu_write32(priv, REG_CCK0_TX_FILTER2, 0x64B80C1C);
658028fa281SKalle Valo 		rtl8xxxu_write32(priv, REG_CCK0_DEBUG_PORT, 0x00008810);
659028fa281SKalle Valo 		rtl8xxxu_write32(priv, REG_CCK0_TX_FILTER3, 0x01235667);
660028fa281SKalle Valo 		/* Special value for channel 13 */
661028fa281SKalle Valo 		rtl8xxxu_write32(priv, REG_CCK0_TX_FILTER1, 0xd1d80001);
662028fa281SKalle Valo 	} else if (channel == 14) {
663028fa281SKalle Valo 		/* Special values for channel 14 */
664028fa281SKalle Valo 		rtl8xxxu_write32(priv, REG_CCK0_TX_FILTER2, 0x0000B81C);
665028fa281SKalle Valo 		rtl8xxxu_write32(priv, REG_CCK0_DEBUG_PORT, 0x00000000);
666028fa281SKalle Valo 		rtl8xxxu_write32(priv, REG_CCK0_TX_FILTER3, 0x00003667);
667028fa281SKalle Valo 		/* Normal value */
668028fa281SKalle Valo 		rtl8xxxu_write32(priv, REG_CCK0_TX_FILTER1, 0xE82C0001);
669028fa281SKalle Valo 	} else {
670028fa281SKalle Valo 		/* Restore normal values from the phy init table */
671028fa281SKalle Valo 		rtl8xxxu_write32(priv, REG_CCK0_TX_FILTER2, 0x64B80C1C);
672028fa281SKalle Valo 		rtl8xxxu_write32(priv, REG_CCK0_DEBUG_PORT, 0x00008810);
673028fa281SKalle Valo 		rtl8xxxu_write32(priv, REG_CCK0_TX_FILTER3, 0x01235667);
674028fa281SKalle Valo 		rtl8xxxu_write32(priv, REG_CCK0_TX_FILTER1, 0xE82C0001);
675028fa281SKalle Valo 	}
676028fa281SKalle Valo }
677028fa281SKalle Valo 
rtl8710bu_config_channel(struct ieee80211_hw * hw)678028fa281SKalle Valo static void rtl8710bu_config_channel(struct ieee80211_hw *hw)
679028fa281SKalle Valo {
680028fa281SKalle Valo 	struct rtl8xxxu_priv *priv = hw->priv;
681028fa281SKalle Valo 	bool ht40 = conf_is_ht40(&hw->conf);
682028fa281SKalle Valo 	u8 channel, subchannel = 0;
683028fa281SKalle Valo 	bool sec_ch_above = 0;
684028fa281SKalle Valo 	u32 val32;
685028fa281SKalle Valo 	u16 val16;
686028fa281SKalle Valo 
687028fa281SKalle Valo 	channel = (u8)hw->conf.chandef.chan->hw_value;
688028fa281SKalle Valo 
689028fa281SKalle Valo 	if (conf_is_ht40_plus(&hw->conf)) {
690028fa281SKalle Valo 		sec_ch_above = 1;
691028fa281SKalle Valo 		channel += 2;
692028fa281SKalle Valo 		subchannel = 2;
693028fa281SKalle Valo 	} else if (conf_is_ht40_minus(&hw->conf)) {
694028fa281SKalle Valo 		sec_ch_above = 0;
695028fa281SKalle Valo 		channel -= 2;
696028fa281SKalle Valo 		subchannel = 1;
697028fa281SKalle Valo 	}
698028fa281SKalle Valo 
699028fa281SKalle Valo 	/* Set channel */
700028fa281SKalle Valo 	val32 = rtl8xxxu_read_rfreg(priv, RF_A, RF6052_REG_MODE_AG);
701028fa281SKalle Valo 	u32p_replace_bits(&val32, channel, MODE_AG_CHANNEL_MASK);
702028fa281SKalle Valo 	rtl8xxxu_write_rfreg(priv, RF_A, RF6052_REG_MODE_AG, val32);
703028fa281SKalle Valo 
704028fa281SKalle Valo 	rtl8710b_revise_cck_tx_psf(priv, channel);
705028fa281SKalle Valo 
706028fa281SKalle Valo 	/* Set bandwidth mode */
707028fa281SKalle Valo 	val16 = rtl8xxxu_read16(priv, REG_WMAC_TRXPTCL_CTL);
708028fa281SKalle Valo 	val16 &= ~WMAC_TRXPTCL_CTL_BW_MASK;
709028fa281SKalle Valo 	if (ht40)
710028fa281SKalle Valo 		val16 |= WMAC_TRXPTCL_CTL_BW_40;
711028fa281SKalle Valo 	rtl8xxxu_write16(priv, REG_WMAC_TRXPTCL_CTL, val16);
712028fa281SKalle Valo 
713028fa281SKalle Valo 	rtl8xxxu_write8(priv, REG_DATA_SUBCHANNEL, subchannel);
714028fa281SKalle Valo 
715028fa281SKalle Valo 	val32 = rtl8xxxu_read32(priv, REG_FPGA0_RF_MODE);
716028fa281SKalle Valo 	u32p_replace_bits(&val32, ht40, FPGA_RF_MODE);
717028fa281SKalle Valo 	rtl8xxxu_write32(priv, REG_FPGA0_RF_MODE, val32);
718028fa281SKalle Valo 
719028fa281SKalle Valo 	val32 = rtl8xxxu_read32(priv, REG_FPGA1_RF_MODE);
720028fa281SKalle Valo 	u32p_replace_bits(&val32, ht40, FPGA_RF_MODE);
721028fa281SKalle Valo 	rtl8xxxu_write32(priv, REG_FPGA1_RF_MODE, val32);
722028fa281SKalle Valo 
723028fa281SKalle Valo 	if (ht40) {
724028fa281SKalle Valo 		/* Set Control channel to upper or lower. */
725028fa281SKalle Valo 		val32 = rtl8xxxu_read32(priv, REG_CCK0_SYSTEM);
726028fa281SKalle Valo 		u32p_replace_bits(&val32, !sec_ch_above, CCK0_SIDEBAND);
727028fa281SKalle Valo 		rtl8xxxu_write32(priv, REG_CCK0_SYSTEM, val32);
728028fa281SKalle Valo 	}
729028fa281SKalle Valo 
730028fa281SKalle Valo 	/* RXADC CLK */
731028fa281SKalle Valo 	val32 = rtl8xxxu_read32(priv, REG_FPGA0_RF_MODE);
732028fa281SKalle Valo 	val32 |= GENMASK(10, 8);
733028fa281SKalle Valo 	rtl8xxxu_write32(priv, REG_FPGA0_RF_MODE, val32);
734028fa281SKalle Valo 
735028fa281SKalle Valo 	/* TXDAC CLK */
736028fa281SKalle Valo 	val32 = rtl8xxxu_read32(priv, REG_FPGA0_RF_MODE);
737028fa281SKalle Valo 	val32 |= BIT(14) | BIT(12);
738028fa281SKalle Valo 	val32 &= ~BIT(13);
739028fa281SKalle Valo 	rtl8xxxu_write32(priv, REG_FPGA0_RF_MODE, val32);
740028fa281SKalle Valo 
741028fa281SKalle Valo 	/* small BW */
742028fa281SKalle Valo 	val32 = rtl8xxxu_read32(priv, REG_OFDM0_TX_PSDO_NOISE_WEIGHT);
743028fa281SKalle Valo 	val32 &= ~GENMASK(31, 30);
744028fa281SKalle Valo 	rtl8xxxu_write32(priv, REG_OFDM0_TX_PSDO_NOISE_WEIGHT, val32);
745028fa281SKalle Valo 
746028fa281SKalle Valo 	/* adc buffer clk */
747028fa281SKalle Valo 	val32 = rtl8xxxu_read32(priv, REG_OFDM0_TX_PSDO_NOISE_WEIGHT);
748028fa281SKalle Valo 	val32 &= ~BIT(29);
749028fa281SKalle Valo 	val32 |= BIT(28);
750028fa281SKalle Valo 	rtl8xxxu_write32(priv, REG_OFDM0_TX_PSDO_NOISE_WEIGHT, val32);
751028fa281SKalle Valo 
752028fa281SKalle Valo 	/* adc buffer clk */
753028fa281SKalle Valo 	val32 = rtl8xxxu_read32(priv, REG_OFDM0_XA_RX_AFE);
754028fa281SKalle Valo 	val32 &= ~BIT(29);
755028fa281SKalle Valo 	val32 |= BIT(28);
756028fa281SKalle Valo 	rtl8xxxu_write32(priv, REG_OFDM0_XA_RX_AFE, val32);
757028fa281SKalle Valo 
758028fa281SKalle Valo 	val32 = rtl8xxxu_read32(priv, REG_FPGA0_XB_RF_INT_OE);
759028fa281SKalle Valo 	val32 &= ~BIT(30);
760028fa281SKalle Valo 	val32 |= BIT(29);
761028fa281SKalle Valo 	rtl8xxxu_write32(priv, REG_FPGA0_XB_RF_INT_OE, val32);
762028fa281SKalle Valo 
763028fa281SKalle Valo 	if (ht40) {
764028fa281SKalle Valo 		val32 = rtl8xxxu_read32(priv, REG_OFDM_RX_DFIR);
765028fa281SKalle Valo 		val32 &= ~BIT(19);
766028fa281SKalle Valo 		rtl8xxxu_write32(priv, REG_OFDM_RX_DFIR, val32);
767028fa281SKalle Valo 
768028fa281SKalle Valo 		val32 = rtl8xxxu_read32(priv, REG_OFDM_RX_DFIR);
769028fa281SKalle Valo 		val32 &= ~GENMASK(23, 20);
770028fa281SKalle Valo 		rtl8xxxu_write32(priv, REG_OFDM_RX_DFIR, val32);
771028fa281SKalle Valo 
772028fa281SKalle Valo 		val32 = rtl8xxxu_read32(priv, REG_OFDM_RX_DFIR);
773028fa281SKalle Valo 		val32 &= ~GENMASK(27, 24);
774028fa281SKalle Valo 		rtl8xxxu_write32(priv, REG_OFDM_RX_DFIR, val32);
775028fa281SKalle Valo 
776028fa281SKalle Valo 		/* RF TRX_BW */
777028fa281SKalle Valo 		val32 = rtl8xxxu_read_rfreg(priv, RF_A, RF6052_REG_MODE_AG);
778028fa281SKalle Valo 		val32 &= ~MODE_AG_BW_MASK;
779028fa281SKalle Valo 		val32 |= MODE_AG_BW_40MHZ_8723B;
780028fa281SKalle Valo 		rtl8xxxu_write_rfreg(priv, RF_A, RF6052_REG_MODE_AG, val32);
781028fa281SKalle Valo 	} else {
782028fa281SKalle Valo 		val32 = rtl8xxxu_read32(priv, REG_OFDM_RX_DFIR);
783028fa281SKalle Valo 		val32 |= BIT(19);
784028fa281SKalle Valo 		rtl8xxxu_write32(priv, REG_OFDM_RX_DFIR, val32);
785028fa281SKalle Valo 
786028fa281SKalle Valo 		val32 = rtl8xxxu_read32(priv, REG_OFDM_RX_DFIR);
787028fa281SKalle Valo 		val32 &= ~GENMASK(23, 20);
788028fa281SKalle Valo 		val32 |= BIT(23);
789028fa281SKalle Valo 		rtl8xxxu_write32(priv, REG_OFDM_RX_DFIR, val32);
790028fa281SKalle Valo 
791028fa281SKalle Valo 		val32 = rtl8xxxu_read32(priv, REG_OFDM_RX_DFIR);
792028fa281SKalle Valo 		val32 &= ~GENMASK(27, 24);
793028fa281SKalle Valo 		val32 |= BIT(27) | BIT(25);
794028fa281SKalle Valo 		rtl8xxxu_write32(priv, REG_OFDM_RX_DFIR, val32);
795028fa281SKalle Valo 
796028fa281SKalle Valo 		/* RF TRX_BW */
797028fa281SKalle Valo 		val32 = rtl8xxxu_read_rfreg(priv, RF_A, RF6052_REG_MODE_AG);
798028fa281SKalle Valo 		val32 &= ~MODE_AG_BW_MASK;
799028fa281SKalle Valo 		val32 |= MODE_AG_BW_20MHZ_8723B;
800028fa281SKalle Valo 		rtl8xxxu_write_rfreg(priv, RF_A, RF6052_REG_MODE_AG, val32);
801028fa281SKalle Valo 	}
802028fa281SKalle Valo }
803028fa281SKalle Valo 
rtl8710bu_init_aggregation(struct rtl8xxxu_priv * priv)804028fa281SKalle Valo static void rtl8710bu_init_aggregation(struct rtl8xxxu_priv *priv)
805028fa281SKalle Valo {
806028fa281SKalle Valo 	u32 agg_rx;
807028fa281SKalle Valo 	u8 agg_ctrl;
808028fa281SKalle Valo 
809028fa281SKalle Valo 	/* RX aggregation */
810028fa281SKalle Valo 	agg_ctrl = rtl8xxxu_read8(priv, REG_TRXDMA_CTRL);
811028fa281SKalle Valo 	agg_ctrl &= ~TRXDMA_CTRL_RXDMA_AGG_EN;
812028fa281SKalle Valo 
813028fa281SKalle Valo 	agg_rx = rtl8xxxu_read32(priv, REG_RXDMA_AGG_PG_TH);
814028fa281SKalle Valo 	agg_rx &= ~RXDMA_USB_AGG_ENABLE;
815028fa281SKalle Valo 	agg_rx &= ~0xFF0F; /* reset agg size and timeout */
816028fa281SKalle Valo 
817028fa281SKalle Valo 	rtl8xxxu_write8(priv, REG_TRXDMA_CTRL, agg_ctrl);
818028fa281SKalle Valo 	rtl8xxxu_write32(priv, REG_RXDMA_AGG_PG_TH, agg_rx);
819028fa281SKalle Valo }
820028fa281SKalle Valo 
rtl8710bu_init_statistics(struct rtl8xxxu_priv * priv)821028fa281SKalle Valo static void rtl8710bu_init_statistics(struct rtl8xxxu_priv *priv)
822028fa281SKalle Valo {
823028fa281SKalle Valo 	u32 val32;
824028fa281SKalle Valo 
825028fa281SKalle Valo 	/* Time duration for NHM unit: 4us, 0xc350=200ms */
826028fa281SKalle Valo 	rtl8xxxu_write16(priv, REG_NHM_TIMER_8723B + 2, 0xc350);
827028fa281SKalle Valo 	rtl8xxxu_write16(priv, REG_NHM_TH9_TH10_8723B + 2, 0xffff);
828028fa281SKalle Valo 	rtl8xxxu_write32(priv, REG_NHM_TH3_TO_TH0_8723B, 0xffffff50);
829028fa281SKalle Valo 	rtl8xxxu_write32(priv, REG_NHM_TH7_TO_TH4_8723B, 0xffffffff);
830028fa281SKalle Valo 
831028fa281SKalle Valo 	/* TH8 */
832028fa281SKalle Valo 	val32 = rtl8xxxu_read32(priv, REG_FPGA0_IQK);
833028fa281SKalle Valo 	val32 |= 0xff;
834028fa281SKalle Valo 	rtl8xxxu_write32(priv, REG_FPGA0_IQK, val32);
835028fa281SKalle Valo 
836028fa281SKalle Valo 	/* Enable CCK */
837028fa281SKalle Valo 	val32 = rtl8xxxu_read32(priv, REG_NHM_TH9_TH10_8723B);
838028fa281SKalle Valo 	val32 &= ~(BIT(8) | BIT(9) | BIT(10));
839028fa281SKalle Valo 	val32 |= BIT(8);
840028fa281SKalle Valo 	rtl8xxxu_write32(priv, REG_NHM_TH9_TH10_8723B, val32);
841028fa281SKalle Valo 
842028fa281SKalle Valo 	/* Max power amongst all RX antennas */
843028fa281SKalle Valo 	val32 = rtl8xxxu_read32(priv, REG_OFDM0_FA_RSTC);
844028fa281SKalle Valo 	val32 |= BIT(7);
845028fa281SKalle Valo 	rtl8xxxu_write32(priv, REG_OFDM0_FA_RSTC, val32);
846028fa281SKalle Valo }
847028fa281SKalle Valo 
rtl8710b_read_efuse(struct rtl8xxxu_priv * priv)848028fa281SKalle Valo static int rtl8710b_read_efuse(struct rtl8xxxu_priv *priv)
849028fa281SKalle Valo {
850028fa281SKalle Valo 	struct device *dev = &priv->udev->dev;
851028fa281SKalle Valo 	u8 val8, word_mask, header, extheader;
852028fa281SKalle Valo 	u16 efuse_addr, offset;
853028fa281SKalle Valo 	int i, ret = 0;
854028fa281SKalle Valo 	u32 val32;
855028fa281SKalle Valo 
856028fa281SKalle Valo 	val32 = rtl8710b_read_syson_reg(priv, REG_SYS_EEPROM_CTRL0_8710B);
857028fa281SKalle Valo 	priv->boot_eeprom = u32_get_bits(val32, EEPROM_BOOT);
858028fa281SKalle Valo 	priv->has_eeprom = u32_get_bits(val32, EEPROM_ENABLE);
859028fa281SKalle Valo 
860028fa281SKalle Valo 	/* Default value is 0xff */
861028fa281SKalle Valo 	memset(priv->efuse_wifi.raw, 0xff, EFUSE_MAP_LEN);
862028fa281SKalle Valo 
863028fa281SKalle Valo 	efuse_addr = 0;
864028fa281SKalle Valo 	while (efuse_addr < EFUSE_REAL_CONTENT_LEN_8723A) {
865028fa281SKalle Valo 		u16 map_addr;
866028fa281SKalle Valo 
867028fa281SKalle Valo 		ret = rtl8710b_read_efuse8(priv, efuse_addr++, &header);
868028fa281SKalle Valo 		if (ret || header == 0xff)
869028fa281SKalle Valo 			goto exit;
870028fa281SKalle Valo 
871028fa281SKalle Valo 		if ((header & 0x1f) == 0x0f) {	/* extended header */
872028fa281SKalle Valo 			offset = (header & 0xe0) >> 5;
873028fa281SKalle Valo 
874028fa281SKalle Valo 			ret = rtl8710b_read_efuse8(priv, efuse_addr++, &extheader);
875028fa281SKalle Valo 			if (ret)
876028fa281SKalle Valo 				goto exit;
877028fa281SKalle Valo 
878028fa281SKalle Valo 			/* All words disabled */
879028fa281SKalle Valo 			if ((extheader & 0x0f) == 0x0f)
880028fa281SKalle Valo 				continue;
881028fa281SKalle Valo 
882028fa281SKalle Valo 			offset |= ((extheader & 0xf0) >> 1);
883028fa281SKalle Valo 			word_mask = extheader & 0x0f;
884028fa281SKalle Valo 		} else {
885028fa281SKalle Valo 			offset = (header >> 4) & 0x0f;
886028fa281SKalle Valo 			word_mask = header & 0x0f;
887028fa281SKalle Valo 		}
888028fa281SKalle Valo 
889028fa281SKalle Valo 		/* Get word enable value from PG header */
890028fa281SKalle Valo 
891028fa281SKalle Valo 		/* We have 8 bits to indicate validity */
892028fa281SKalle Valo 		map_addr = offset * 8;
893028fa281SKalle Valo 		for (i = 0; i < EFUSE_MAX_WORD_UNIT; i++) {
894028fa281SKalle Valo 			/* Check word enable condition in the section */
895028fa281SKalle Valo 			if (word_mask & BIT(i)) {
896028fa281SKalle Valo 				map_addr += 2;
897028fa281SKalle Valo 				continue;
898028fa281SKalle Valo 			}
899028fa281SKalle Valo 
900028fa281SKalle Valo 			ret = rtl8710b_read_efuse8(priv, efuse_addr++, &val8);
901028fa281SKalle Valo 			if (ret)
902028fa281SKalle Valo 				goto exit;
903028fa281SKalle Valo 			if (map_addr >= EFUSE_MAP_LEN - 1) {
904028fa281SKalle Valo 				dev_warn(dev, "%s: Illegal map_addr (%04x), efuse corrupt!\n",
905028fa281SKalle Valo 					 __func__, map_addr);
906028fa281SKalle Valo 				ret = -EINVAL;
907028fa281SKalle Valo 				goto exit;
908028fa281SKalle Valo 			}
909028fa281SKalle Valo 			priv->efuse_wifi.raw[map_addr++] = val8;
910028fa281SKalle Valo 
911028fa281SKalle Valo 			ret = rtl8710b_read_efuse8(priv, efuse_addr++, &val8);
912028fa281SKalle Valo 			if (ret)
913028fa281SKalle Valo 				goto exit;
914028fa281SKalle Valo 			priv->efuse_wifi.raw[map_addr++] = val8;
915028fa281SKalle Valo 		}
916028fa281SKalle Valo 	}
917028fa281SKalle Valo 
918028fa281SKalle Valo exit:
919028fa281SKalle Valo 
920028fa281SKalle Valo 	return ret;
921028fa281SKalle Valo }
922028fa281SKalle Valo 
rtl8710bu_parse_efuse(struct rtl8xxxu_priv * priv)923028fa281SKalle Valo static int rtl8710bu_parse_efuse(struct rtl8xxxu_priv *priv)
924028fa281SKalle Valo {
925028fa281SKalle Valo 	struct rtl8710bu_efuse *efuse = &priv->efuse_wifi.efuse8710bu;
926028fa281SKalle Valo 
927028fa281SKalle Valo 	if (efuse->rtl_id != cpu_to_le16(0x8195))
928028fa281SKalle Valo 		return -EINVAL;
929028fa281SKalle Valo 
930028fa281SKalle Valo 	ether_addr_copy(priv->mac_addr, efuse->mac_addr);
931028fa281SKalle Valo 
932028fa281SKalle Valo 	memcpy(priv->cck_tx_power_index_A, efuse->tx_power_index_A.cck_base,
933028fa281SKalle Valo 	       sizeof(efuse->tx_power_index_A.cck_base));
934028fa281SKalle Valo 
935028fa281SKalle Valo 	memcpy(priv->ht40_1s_tx_power_index_A,
936028fa281SKalle Valo 	       efuse->tx_power_index_A.ht40_base,
937028fa281SKalle Valo 	       sizeof(efuse->tx_power_index_A.ht40_base));
938028fa281SKalle Valo 
939028fa281SKalle Valo 	priv->ofdm_tx_power_diff[0].a = efuse->tx_power_index_A.ht20_ofdm_1s_diff.a;
940028fa281SKalle Valo 	priv->ht20_tx_power_diff[0].a = efuse->tx_power_index_A.ht20_ofdm_1s_diff.b;
941028fa281SKalle Valo 
942028fa281SKalle Valo 	priv->default_crystal_cap = efuse->xtal_k & 0x3f;
943028fa281SKalle Valo 
944028fa281SKalle Valo 	return 0;
945028fa281SKalle Valo }
946028fa281SKalle Valo 
rtl8710bu_load_firmware(struct rtl8xxxu_priv * priv)947028fa281SKalle Valo static int rtl8710bu_load_firmware(struct rtl8xxxu_priv *priv)
948028fa281SKalle Valo {
949028fa281SKalle Valo 	if (priv->vendor_smic) {
950028fa281SKalle Valo 		return rtl8xxxu_load_firmware(priv, "rtlwifi/rtl8710bufw_SMIC.bin");
951028fa281SKalle Valo 	} else if (priv->vendor_umc) {
952028fa281SKalle Valo 		return rtl8xxxu_load_firmware(priv, "rtlwifi/rtl8710bufw_UMC.bin");
953028fa281SKalle Valo 	} else {
954028fa281SKalle Valo 		dev_err(&priv->udev->dev, "We have no suitable firmware for this chip.\n");
955028fa281SKalle Valo 		return -1;
956028fa281SKalle Valo 	}
957028fa281SKalle Valo }
958028fa281SKalle Valo 
rtl8710bu_init_phy_bb(struct rtl8xxxu_priv * priv)959028fa281SKalle Valo static void rtl8710bu_init_phy_bb(struct rtl8xxxu_priv *priv)
960028fa281SKalle Valo {
961028fa281SKalle Valo 	const struct rtl8xxxu_reg32val *phy_init_table;
962028fa281SKalle Valo 	u32 val32;
963028fa281SKalle Valo 
964028fa281SKalle Valo 	/* Enable BB and RF */
965028fa281SKalle Valo 	val32 = rtl8xxxu_read32(priv, REG_SYS_FUNC_8710B);
966028fa281SKalle Valo 	val32 |= GENMASK(17, 16) | GENMASK(26, 24);
967028fa281SKalle Valo 	rtl8xxxu_write32(priv, REG_SYS_FUNC_8710B, val32);
968028fa281SKalle Valo 
969028fa281SKalle Valo 	if (priv->package_type == PACKAGE_QFN48M_U)
970028fa281SKalle Valo 		phy_init_table = rtl8710bu_qfn48m_u_phy_init_table;
971028fa281SKalle Valo 	else
972028fa281SKalle Valo 		phy_init_table = rtl8710bu_qfn48m_s_phy_init_table;
973028fa281SKalle Valo 
974028fa281SKalle Valo 	rtl8xxxu_init_phy_regs(priv, phy_init_table);
975028fa281SKalle Valo 
976028fa281SKalle Valo 	rtl8xxxu_init_phy_regs(priv, rtl8710b_agc_table);
977028fa281SKalle Valo }
978028fa281SKalle Valo 
rtl8710bu_init_phy_rf(struct rtl8xxxu_priv * priv)979028fa281SKalle Valo static int rtl8710bu_init_phy_rf(struct rtl8xxxu_priv *priv)
980028fa281SKalle Valo {
981028fa281SKalle Valo 	const struct rtl8xxxu_rfregval *radioa_init_table;
982028fa281SKalle Valo 
983028fa281SKalle Valo 	if (priv->package_type == PACKAGE_QFN48M_U)
984028fa281SKalle Valo 		radioa_init_table = rtl8710bu_qfn48m_u_radioa_init_table;
985028fa281SKalle Valo 	else
986028fa281SKalle Valo 		radioa_init_table = rtl8710bu_qfn48m_s_radioa_init_table;
987028fa281SKalle Valo 
988028fa281SKalle Valo 	return rtl8xxxu_init_phy_rf(priv, radioa_init_table, RF_A);
989028fa281SKalle Valo }
990028fa281SKalle Valo 
rtl8710bu_iqk_path_a(struct rtl8xxxu_priv * priv,u32 * lok_result)991028fa281SKalle Valo static int rtl8710bu_iqk_path_a(struct rtl8xxxu_priv *priv, u32 *lok_result)
992028fa281SKalle Valo {
993028fa281SKalle Valo 	u32 reg_eac, reg_e94, reg_e9c, val32, path_sel_bb;
994028fa281SKalle Valo 	int result = 0;
995028fa281SKalle Valo 
996028fa281SKalle Valo 	path_sel_bb = rtl8xxxu_read32(priv, REG_S0S1_PATH_SWITCH);
997028fa281SKalle Valo 
998028fa281SKalle Valo 	rtl8xxxu_write32(priv, REG_S0S1_PATH_SWITCH, 0x99000000);
999028fa281SKalle Valo 
1000028fa281SKalle Valo 	/*
1001028fa281SKalle Valo 	 * Leave IQK mode
1002028fa281SKalle Valo 	 */
1003028fa281SKalle Valo 	val32 = rtl8xxxu_read32(priv, REG_FPGA0_IQK);
1004028fa281SKalle Valo 	u32p_replace_bits(&val32, 0, 0xffffff00);
1005028fa281SKalle Valo 	rtl8xxxu_write32(priv, REG_FPGA0_IQK, val32);
1006028fa281SKalle Valo 
1007028fa281SKalle Valo 	/*
1008028fa281SKalle Valo 	 * Enable path A PA in TX IQK mode
1009028fa281SKalle Valo 	 */
1010028fa281SKalle Valo 	val32 = rtl8xxxu_read_rfreg(priv, RF_A, RF6052_REG_WE_LUT);
1011028fa281SKalle Valo 	val32 |= 0x80000;
1012028fa281SKalle Valo 	rtl8xxxu_write_rfreg(priv, RF_A, RF6052_REG_WE_LUT, val32);
1013028fa281SKalle Valo 	rtl8xxxu_write_rfreg(priv, RF_A, RF6052_REG_RCK_OS, 0x20000);
1014028fa281SKalle Valo 	rtl8xxxu_write_rfreg(priv, RF_A, RF6052_REG_TXPA_G1, 0x0000f);
1015028fa281SKalle Valo 	rtl8xxxu_write_rfreg(priv, RF_A, RF6052_REG_TXPA_G2, 0x07ff7);
1016028fa281SKalle Valo 
1017028fa281SKalle Valo 	/* PA,PAD gain adjust */
1018028fa281SKalle Valo 	val32 = rtl8xxxu_read_rfreg(priv, RF_A, RF6052_REG_GAIN_CCA);
1019028fa281SKalle Valo 	val32 |= BIT(11);
1020028fa281SKalle Valo 	rtl8xxxu_write_rfreg(priv, RF_A, RF6052_REG_GAIN_CCA, val32);
1021028fa281SKalle Valo 	val32 = rtl8xxxu_read_rfreg(priv, RF_A, RF6052_REG_PAD_TXG);
1022028fa281SKalle Valo 	u32p_replace_bits(&val32, 0x1ed, 0x00fff);
1023028fa281SKalle Valo 	rtl8xxxu_write_rfreg(priv, RF_A, RF6052_REG_PAD_TXG, val32);
1024028fa281SKalle Valo 
1025028fa281SKalle Valo 	/* enter IQK mode */
1026028fa281SKalle Valo 	val32 = rtl8xxxu_read32(priv, REG_FPGA0_IQK);
1027028fa281SKalle Valo 	u32p_replace_bits(&val32, 0x808000, 0xffffff00);
1028028fa281SKalle Valo 	rtl8xxxu_write32(priv, REG_FPGA0_IQK, val32);
1029028fa281SKalle Valo 
1030028fa281SKalle Valo 	/* path-A IQK setting */
1031028fa281SKalle Valo 	rtl8xxxu_write32(priv, REG_TX_IQK_TONE_A, 0x18008c1c);
1032028fa281SKalle Valo 	rtl8xxxu_write32(priv, REG_RX_IQK_TONE_A, 0x38008c1c);
1033028fa281SKalle Valo 
1034028fa281SKalle Valo 	rtl8xxxu_write32(priv, REG_TX_IQK_PI_A, 0x821403ff);
1035028fa281SKalle Valo 	rtl8xxxu_write32(priv, REG_RX_IQK_PI_A, 0x28160c06);
1036028fa281SKalle Valo 
1037028fa281SKalle Valo 	/* LO calibration setting */
1038028fa281SKalle Valo 	rtl8xxxu_write32(priv, REG_IQK_AGC_RSP, 0x02002911);
1039028fa281SKalle Valo 
1040028fa281SKalle Valo 	/* One shot, path A LOK & IQK */
1041028fa281SKalle Valo 	rtl8xxxu_write32(priv, REG_IQK_AGC_PTS, 0xfa000000);
1042028fa281SKalle Valo 	rtl8xxxu_write32(priv, REG_IQK_AGC_PTS, 0xf8000000);
1043028fa281SKalle Valo 
1044028fa281SKalle Valo 	mdelay(10);
1045028fa281SKalle Valo 
1046028fa281SKalle Valo 	rtl8xxxu_write32(priv, REG_S0S1_PATH_SWITCH, path_sel_bb);
1047028fa281SKalle Valo 
1048028fa281SKalle Valo 	/*
1049028fa281SKalle Valo 	 * Leave IQK mode
1050028fa281SKalle Valo 	 */
1051028fa281SKalle Valo 	val32 = rtl8xxxu_read32(priv, REG_FPGA0_IQK);
1052028fa281SKalle Valo 	u32p_replace_bits(&val32, 0, 0xffffff00);
1053028fa281SKalle Valo 	rtl8xxxu_write32(priv, REG_FPGA0_IQK, val32);
1054028fa281SKalle Valo 
1055028fa281SKalle Valo 	val32 = rtl8xxxu_read_rfreg(priv, RF_A, RF6052_REG_GAIN_CCA);
1056028fa281SKalle Valo 	val32 &= ~BIT(11);
1057028fa281SKalle Valo 	rtl8xxxu_write_rfreg(priv, RF_A, RF6052_REG_GAIN_CCA, val32);
1058028fa281SKalle Valo 
1059028fa281SKalle Valo 	/* save LOK result */
1060028fa281SKalle Valo 	*lok_result = rtl8xxxu_read_rfreg(priv, RF_A, RF6052_REG_TXM_IDAC);
1061028fa281SKalle Valo 
1062028fa281SKalle Valo 	/* Check failed */
1063028fa281SKalle Valo 	reg_eac = rtl8xxxu_read32(priv, REG_RX_POWER_AFTER_IQK_A_2);
1064028fa281SKalle Valo 	reg_e94 = rtl8xxxu_read32(priv, REG_TX_POWER_BEFORE_IQK_A);
1065028fa281SKalle Valo 	reg_e9c = rtl8xxxu_read32(priv, REG_TX_POWER_AFTER_IQK_A);
1066028fa281SKalle Valo 
1067028fa281SKalle Valo 	if (!(reg_eac & BIT(28)) &&
1068028fa281SKalle Valo 	    ((reg_e94 & 0x03ff0000) != 0x01420000) &&
1069028fa281SKalle Valo 	    ((reg_e9c & 0x03ff0000) != 0x00420000))
1070028fa281SKalle Valo 		result |= 0x01;
1071028fa281SKalle Valo 
1072028fa281SKalle Valo 	return result;
1073028fa281SKalle Valo }
1074028fa281SKalle Valo 
rtl8710bu_rx_iqk_path_a(struct rtl8xxxu_priv * priv,u32 lok_result)1075028fa281SKalle Valo static int rtl8710bu_rx_iqk_path_a(struct rtl8xxxu_priv *priv, u32 lok_result)
1076028fa281SKalle Valo {
1077028fa281SKalle Valo 	u32 reg_ea4, reg_eac, reg_e94, reg_e9c, val32, path_sel_bb, tmp;
1078028fa281SKalle Valo 	int result = 0;
1079028fa281SKalle Valo 
1080028fa281SKalle Valo 	path_sel_bb = rtl8xxxu_read32(priv, REG_S0S1_PATH_SWITCH);
1081028fa281SKalle Valo 
1082028fa281SKalle Valo 	rtl8xxxu_write32(priv, REG_S0S1_PATH_SWITCH, 0x99000000);
1083028fa281SKalle Valo 
1084028fa281SKalle Valo 	/*
1085028fa281SKalle Valo 	 * Leave IQK mode
1086028fa281SKalle Valo 	 */
1087028fa281SKalle Valo 	val32 = rtl8xxxu_read32(priv, REG_FPGA0_IQK);
1088028fa281SKalle Valo 	u32p_replace_bits(&val32, 0, 0xffffff00);
1089028fa281SKalle Valo 	rtl8xxxu_write32(priv, REG_FPGA0_IQK, val32);
1090028fa281SKalle Valo 
1091028fa281SKalle Valo 	/* modify RXIQK mode table */
1092028fa281SKalle Valo 	val32 = rtl8xxxu_read_rfreg(priv, RF_A, RF6052_REG_WE_LUT);
1093028fa281SKalle Valo 	val32 |= 0x80000;
1094028fa281SKalle Valo 	rtl8xxxu_write_rfreg(priv, RF_A, RF6052_REG_WE_LUT, val32);
1095028fa281SKalle Valo 	rtl8xxxu_write_rfreg(priv, RF_A, RF6052_REG_RCK_OS, 0x30000);
1096028fa281SKalle Valo 	rtl8xxxu_write_rfreg(priv, RF_A, RF6052_REG_TXPA_G1, 0x0000f);
1097028fa281SKalle Valo 	rtl8xxxu_write_rfreg(priv, RF_A, RF6052_REG_TXPA_G2, 0xf1173);
1098028fa281SKalle Valo 
1099028fa281SKalle Valo 	/* PA,PAD gain adjust */
1100028fa281SKalle Valo 	val32 = rtl8xxxu_read_rfreg(priv, RF_A, RF6052_REG_GAIN_CCA);
1101028fa281SKalle Valo 	val32 |= BIT(11);
1102028fa281SKalle Valo 	rtl8xxxu_write_rfreg(priv, RF_A, RF6052_REG_GAIN_CCA, val32);
1103028fa281SKalle Valo 	val32 = rtl8xxxu_read_rfreg(priv, RF_A, RF6052_REG_PAD_TXG);
1104028fa281SKalle Valo 	u32p_replace_bits(&val32, 0xf, 0x003e0);
1105028fa281SKalle Valo 	rtl8xxxu_write_rfreg(priv, RF_A, RF6052_REG_PAD_TXG, val32);
1106028fa281SKalle Valo 
1107028fa281SKalle Valo 	/*
1108028fa281SKalle Valo 	 * Enter IQK mode
1109028fa281SKalle Valo 	 */
1110028fa281SKalle Valo 	val32 = rtl8xxxu_read32(priv, REG_FPGA0_IQK);
1111028fa281SKalle Valo 	u32p_replace_bits(&val32, 0x808000, 0xffffff00);
1112028fa281SKalle Valo 	rtl8xxxu_write32(priv, REG_FPGA0_IQK, val32);
1113028fa281SKalle Valo 
1114028fa281SKalle Valo 	/* path-A IQK setting */
1115028fa281SKalle Valo 	rtl8xxxu_write32(priv, REG_TX_IQK_TONE_A, 0x18008c1c);
1116028fa281SKalle Valo 	rtl8xxxu_write32(priv, REG_RX_IQK_TONE_A, 0x38008c1c);
1117028fa281SKalle Valo 
1118028fa281SKalle Valo 	rtl8xxxu_write32(priv, REG_TX_IQK_PI_A, 0x8216129f);
1119028fa281SKalle Valo 	rtl8xxxu_write32(priv, REG_RX_IQK_PI_A, 0x28160c00);
1120028fa281SKalle Valo 
1121028fa281SKalle Valo 	/*
1122028fa281SKalle Valo 	 * Tx IQK setting
1123028fa281SKalle Valo 	 */
1124028fa281SKalle Valo 	rtl8xxxu_write32(priv, REG_TX_IQK, 0x01007c00);
1125028fa281SKalle Valo 	rtl8xxxu_write32(priv, REG_RX_IQK, 0x01004800);
1126028fa281SKalle Valo 
1127028fa281SKalle Valo 	/* LO calibration setting */
1128028fa281SKalle Valo 	rtl8xxxu_write32(priv, REG_IQK_AGC_RSP, 0x0046a911);
1129028fa281SKalle Valo 
1130028fa281SKalle Valo 	/* One shot, path A LOK & IQK */
1131028fa281SKalle Valo 	rtl8xxxu_write32(priv, REG_IQK_AGC_PTS, 0xf9000000);
1132028fa281SKalle Valo 	rtl8xxxu_write32(priv, REG_IQK_AGC_PTS, 0xf8000000);
1133028fa281SKalle Valo 
1134028fa281SKalle Valo 	mdelay(10);
1135028fa281SKalle Valo 
1136028fa281SKalle Valo 	/* Check failed */
1137028fa281SKalle Valo 	reg_eac = rtl8xxxu_read32(priv, REG_RX_POWER_AFTER_IQK_A_2);
1138028fa281SKalle Valo 	reg_e94 = rtl8xxxu_read32(priv, REG_TX_POWER_BEFORE_IQK_A);
1139028fa281SKalle Valo 	reg_e9c = rtl8xxxu_read32(priv, REG_TX_POWER_AFTER_IQK_A);
1140028fa281SKalle Valo 
1141028fa281SKalle Valo 	if (!(reg_eac & BIT(28)) &&
1142028fa281SKalle Valo 	    ((reg_e94 & 0x03ff0000) != 0x01420000) &&
1143028fa281SKalle Valo 	    ((reg_e9c & 0x03ff0000) != 0x00420000)) {
1144028fa281SKalle Valo 		result |= 0x01;
1145028fa281SKalle Valo 	} else { /* If TX not OK, ignore RX */
1146028fa281SKalle Valo 
1147028fa281SKalle Valo 		/* reload RF path */
1148028fa281SKalle Valo 		rtl8xxxu_write32(priv, REG_S0S1_PATH_SWITCH, path_sel_bb);
1149028fa281SKalle Valo 
1150028fa281SKalle Valo 		/*
1151028fa281SKalle Valo 		 * Leave IQK mode
1152028fa281SKalle Valo 		 */
1153028fa281SKalle Valo 		val32 = rtl8xxxu_read32(priv, REG_FPGA0_IQK);
1154028fa281SKalle Valo 		u32p_replace_bits(&val32, 0, 0xffffff00);
1155028fa281SKalle Valo 		rtl8xxxu_write32(priv, REG_FPGA0_IQK, val32);
1156028fa281SKalle Valo 
1157028fa281SKalle Valo 		val32 = rtl8xxxu_read_rfreg(priv, RF_A, RF6052_REG_GAIN_CCA);
1158028fa281SKalle Valo 		val32 &= ~BIT(11);
1159028fa281SKalle Valo 		rtl8xxxu_write_rfreg(priv, RF_A, RF6052_REG_GAIN_CCA, val32);
1160028fa281SKalle Valo 
1161028fa281SKalle Valo 		return result;
1162028fa281SKalle Valo 	}
1163028fa281SKalle Valo 
1164028fa281SKalle Valo 	val32 = 0x80007c00 | (reg_e94 & 0x3ff0000) | ((reg_e9c & 0x3ff0000) >> 16);
1165028fa281SKalle Valo 	rtl8xxxu_write32(priv, REG_TX_IQK, val32);
1166028fa281SKalle Valo 
1167028fa281SKalle Valo 	/*
1168028fa281SKalle Valo 	 * Modify RX IQK mode table
1169028fa281SKalle Valo 	 */
1170028fa281SKalle Valo 	val32 = rtl8xxxu_read32(priv, REG_FPGA0_IQK);
1171028fa281SKalle Valo 	u32p_replace_bits(&val32, 0, 0xffffff00);
1172028fa281SKalle Valo 	rtl8xxxu_write32(priv, REG_FPGA0_IQK, val32);
1173028fa281SKalle Valo 
1174028fa281SKalle Valo 	val32 = rtl8xxxu_read_rfreg(priv, RF_A, RF6052_REG_WE_LUT);
1175028fa281SKalle Valo 	val32 |= 0x80000;
1176028fa281SKalle Valo 	rtl8xxxu_write_rfreg(priv, RF_A, RF6052_REG_WE_LUT, val32);
1177028fa281SKalle Valo 	rtl8xxxu_write_rfreg(priv, RF_A, RF6052_REG_RCK_OS, 0x30000);
1178028fa281SKalle Valo 	rtl8xxxu_write_rfreg(priv, RF_A, RF6052_REG_TXPA_G1, 0x0000f);
1179028fa281SKalle Valo 	rtl8xxxu_write_rfreg(priv, RF_A, RF6052_REG_TXPA_G2, 0xf7ff2);
1180028fa281SKalle Valo 
1181028fa281SKalle Valo 	/*
1182028fa281SKalle Valo 	 * PA, PAD setting
1183028fa281SKalle Valo 	 */
1184028fa281SKalle Valo 	val32 = rtl8xxxu_read_rfreg(priv, RF_A, RF6052_REG_GAIN_CCA);
1185028fa281SKalle Valo 	val32 |= BIT(11);
1186028fa281SKalle Valo 	rtl8xxxu_write_rfreg(priv, RF_A, RF6052_REG_GAIN_CCA, val32);
1187028fa281SKalle Valo 	val32 = rtl8xxxu_read_rfreg(priv, RF_A, RF6052_REG_PAD_TXG);
1188028fa281SKalle Valo 	u32p_replace_bits(&val32, 0x2a, 0x00fff);
1189028fa281SKalle Valo 	rtl8xxxu_write_rfreg(priv, RF_A, RF6052_REG_PAD_TXG, val32);
1190028fa281SKalle Valo 
1191028fa281SKalle Valo 	/*
1192028fa281SKalle Valo 	 * Enter IQK mode
1193028fa281SKalle Valo 	 */
1194028fa281SKalle Valo 	val32 = rtl8xxxu_read32(priv, REG_FPGA0_IQK);
1195028fa281SKalle Valo 	u32p_replace_bits(&val32, 0x808000, 0xffffff00);
1196028fa281SKalle Valo 	rtl8xxxu_write32(priv, REG_FPGA0_IQK, val32);
1197028fa281SKalle Valo 
1198028fa281SKalle Valo 	/*
1199028fa281SKalle Valo 	 * RX IQK setting
1200028fa281SKalle Valo 	 */
1201028fa281SKalle Valo 	rtl8xxxu_write32(priv, REG_RX_IQK, 0x01004800);
1202028fa281SKalle Valo 
1203028fa281SKalle Valo 	/* path-A IQK setting */
1204028fa281SKalle Valo 	rtl8xxxu_write32(priv, REG_TX_IQK_TONE_A, 0x38008c1c);
1205028fa281SKalle Valo 	rtl8xxxu_write32(priv, REG_RX_IQK_TONE_A, 0x18008c1c);
1206028fa281SKalle Valo 
1207028fa281SKalle Valo 	rtl8xxxu_write32(priv, REG_RX_IQK_PI_A, 0x2816169f);
1208028fa281SKalle Valo 
1209028fa281SKalle Valo 	/* LO calibration setting */
1210028fa281SKalle Valo 	rtl8xxxu_write32(priv, REG_IQK_AGC_RSP, 0x0046a911);
1211028fa281SKalle Valo 
1212028fa281SKalle Valo 	/* One shot, path A LOK & IQK */
1213028fa281SKalle Valo 	rtl8xxxu_write32(priv, REG_IQK_AGC_PTS, 0xf9000000);
1214028fa281SKalle Valo 	rtl8xxxu_write32(priv, REG_IQK_AGC_PTS, 0xf8000000);
1215028fa281SKalle Valo 
1216028fa281SKalle Valo 	mdelay(10);
1217028fa281SKalle Valo 
1218028fa281SKalle Valo 	/* reload RF path */
1219028fa281SKalle Valo 	rtl8xxxu_write32(priv, REG_S0S1_PATH_SWITCH, path_sel_bb);
1220028fa281SKalle Valo 
1221028fa281SKalle Valo 	/*
1222028fa281SKalle Valo 	 * Leave IQK mode
1223028fa281SKalle Valo 	 */
1224028fa281SKalle Valo 	val32 = rtl8xxxu_read32(priv, REG_FPGA0_IQK);
1225028fa281SKalle Valo 	u32p_replace_bits(&val32, 0, 0xffffff00);
1226028fa281SKalle Valo 	rtl8xxxu_write32(priv, REG_FPGA0_IQK, val32);
1227028fa281SKalle Valo 
1228028fa281SKalle Valo 	val32 = rtl8xxxu_read_rfreg(priv, RF_A, RF6052_REG_GAIN_CCA);
1229028fa281SKalle Valo 	val32 &= ~BIT(11);
1230028fa281SKalle Valo 	rtl8xxxu_write_rfreg(priv, RF_A, RF6052_REG_GAIN_CCA, val32);
1231028fa281SKalle Valo 
1232028fa281SKalle Valo 	/* reload LOK value */
1233028fa281SKalle Valo 	rtl8xxxu_write_rfreg(priv, RF_A, RF6052_REG_TXM_IDAC, lok_result);
1234028fa281SKalle Valo 
1235028fa281SKalle Valo 	/* Check failed */
1236028fa281SKalle Valo 	reg_eac = rtl8xxxu_read32(priv, REG_RX_POWER_AFTER_IQK_A_2);
1237028fa281SKalle Valo 	reg_ea4 = rtl8xxxu_read32(priv, REG_RX_POWER_BEFORE_IQK_A_2);
1238028fa281SKalle Valo 
1239028fa281SKalle Valo 	tmp = (reg_eac & 0x03ff0000) >> 16;
1240028fa281SKalle Valo 	if ((tmp & 0x200) > 0)
1241028fa281SKalle Valo 		tmp = 0x400 - tmp;
1242028fa281SKalle Valo 
1243028fa281SKalle Valo 	if (!(reg_eac & BIT(27)) &&
1244028fa281SKalle Valo 	    ((reg_ea4 & 0x03ff0000) != 0x01320000) &&
1245028fa281SKalle Valo 	    ((reg_eac & 0x03ff0000) != 0x00360000) &&
1246028fa281SKalle Valo 	    (((reg_ea4 & 0x03ff0000) >> 16) < 0x11a) &&
1247028fa281SKalle Valo 	    (((reg_ea4 & 0x03ff0000) >> 16) > 0xe6) &&
1248028fa281SKalle Valo 	    (tmp < 0x1a))
1249028fa281SKalle Valo 		result |= 0x02;
1250028fa281SKalle Valo 
1251028fa281SKalle Valo 	return result;
1252028fa281SKalle Valo }
1253028fa281SKalle Valo 
rtl8710bu_phy_iqcalibrate(struct rtl8xxxu_priv * priv,int result[][8],int t)1254028fa281SKalle Valo static void rtl8710bu_phy_iqcalibrate(struct rtl8xxxu_priv *priv,
1255028fa281SKalle Valo 				      int result[][8], int t)
1256028fa281SKalle Valo {
1257028fa281SKalle Valo 	struct device *dev = &priv->udev->dev;
1258028fa281SKalle Valo 	u32 i, val32, rx_initial_gain, lok_result;
1259028fa281SKalle Valo 	u32 path_sel_bb, path_sel_rf;
1260028fa281SKalle Valo 	int path_a_ok;
1261028fa281SKalle Valo 	int retry = 2;
1262028fa281SKalle Valo 	static const u32 adda_regs[RTL8XXXU_ADDA_REGS] = {
1263028fa281SKalle Valo 		REG_FPGA0_XCD_SWITCH_CTRL, REG_BLUETOOTH,
1264028fa281SKalle Valo 		REG_RX_WAIT_CCA, REG_TX_CCK_RFON,
1265028fa281SKalle Valo 		REG_TX_CCK_BBON, REG_TX_OFDM_RFON,
1266028fa281SKalle Valo 		REG_TX_OFDM_BBON, REG_TX_TO_RX,
1267028fa281SKalle Valo 		REG_TX_TO_TX, REG_RX_CCK,
1268028fa281SKalle Valo 		REG_RX_OFDM, REG_RX_WAIT_RIFS,
1269028fa281SKalle Valo 		REG_RX_TO_RX, REG_STANDBY,
1270028fa281SKalle Valo 		REG_SLEEP, REG_PMPD_ANAEN
1271028fa281SKalle Valo 	};
1272028fa281SKalle Valo 	static const u32 iqk_mac_regs[RTL8XXXU_MAC_REGS] = {
1273028fa281SKalle Valo 		REG_TXPAUSE, REG_BEACON_CTRL,
1274028fa281SKalle Valo 		REG_BEACON_CTRL_1, REG_GPIO_MUXCFG
1275028fa281SKalle Valo 	};
1276028fa281SKalle Valo 	static const u32 iqk_bb_regs[RTL8XXXU_BB_REGS] = {
1277028fa281SKalle Valo 		REG_OFDM0_TRX_PATH_ENABLE, REG_OFDM0_TR_MUX_PAR,
1278028fa281SKalle Valo 		REG_FPGA0_XCD_RF_SW_CTRL, REG_CONFIG_ANT_A, REG_CONFIG_ANT_B,
1279028fa281SKalle Valo 		REG_FPGA0_XAB_RF_SW_CTRL, REG_FPGA0_XA_RF_INT_OE,
1280028fa281SKalle Valo 		REG_FPGA0_XB_RF_INT_OE, REG_CCK0_AFE_SETTING
1281028fa281SKalle Valo 	};
1282028fa281SKalle Valo 
1283028fa281SKalle Valo 	/*
1284028fa281SKalle Valo 	 * Note: IQ calibration must be performed after loading
1285028fa281SKalle Valo 	 *       PHY_REG.txt , and radio_a, radio_b.txt
1286028fa281SKalle Valo 	 */
1287028fa281SKalle Valo 
1288028fa281SKalle Valo 	rx_initial_gain = rtl8xxxu_read32(priv, REG_OFDM0_XA_AGC_CORE1);
1289028fa281SKalle Valo 
1290028fa281SKalle Valo 	if (t == 0) {
1291028fa281SKalle Valo 		/* Save ADDA parameters, turn Path A ADDA on */
1292028fa281SKalle Valo 		rtl8xxxu_save_regs(priv, adda_regs, priv->adda_backup,
1293028fa281SKalle Valo 				   RTL8XXXU_ADDA_REGS);
1294028fa281SKalle Valo 		rtl8xxxu_save_mac_regs(priv, iqk_mac_regs, priv->mac_backup);
1295028fa281SKalle Valo 		rtl8xxxu_save_regs(priv, iqk_bb_regs,
1296028fa281SKalle Valo 				   priv->bb_backup, RTL8XXXU_BB_REGS);
1297028fa281SKalle Valo 	}
1298028fa281SKalle Valo 
1299028fa281SKalle Valo 	rtl8xxxu_path_adda_on(priv, adda_regs, true);
1300028fa281SKalle Valo 
1301028fa281SKalle Valo 	if (t == 0) {
1302028fa281SKalle Valo 		val32 = rtl8xxxu_read32(priv, REG_FPGA0_XA_HSSI_PARM1);
1303028fa281SKalle Valo 		priv->pi_enabled = u32_get_bits(val32, FPGA0_HSSI_PARM1_PI);
1304028fa281SKalle Valo 	}
1305028fa281SKalle Valo 
1306028fa281SKalle Valo 	if (!priv->pi_enabled) {
1307028fa281SKalle Valo 		/* Switch BB to PI mode to do IQ Calibration */
1308028fa281SKalle Valo 		rtl8xxxu_write32(priv, REG_FPGA0_XA_HSSI_PARM1, 0x01000100);
1309028fa281SKalle Valo 		rtl8xxxu_write32(priv, REG_FPGA0_XB_HSSI_PARM1, 0x01000100);
1310028fa281SKalle Valo 	}
1311028fa281SKalle Valo 
1312028fa281SKalle Valo 	/* MAC settings */
1313028fa281SKalle Valo 	val32 = rtl8xxxu_read32(priv, REG_TX_PTCL_CTRL);
1314028fa281SKalle Valo 	val32 |= 0x00ff0000;
1315028fa281SKalle Valo 	rtl8xxxu_write32(priv, REG_TX_PTCL_CTRL, val32);
1316028fa281SKalle Valo 
1317028fa281SKalle Valo 	/* save RF path */
1318028fa281SKalle Valo 	path_sel_bb = rtl8xxxu_read32(priv, REG_S0S1_PATH_SWITCH);
1319028fa281SKalle Valo 	path_sel_rf = rtl8xxxu_read_rfreg(priv, RF_A, RF6052_REG_S0S1);
1320028fa281SKalle Valo 
1321028fa281SKalle Valo 	/* BB setting */
1322028fa281SKalle Valo 	val32 = rtl8xxxu_read32(priv, REG_CCK0_AFE_SETTING);
1323028fa281SKalle Valo 	val32 |= 0x0f000000;
1324028fa281SKalle Valo 	rtl8xxxu_write32(priv, REG_CCK0_AFE_SETTING, val32);
1325028fa281SKalle Valo 	rtl8xxxu_write32(priv, REG_RX_WAIT_CCA, 0x03c00010);
1326028fa281SKalle Valo 	rtl8xxxu_write32(priv, REG_OFDM0_TRX_PATH_ENABLE, 0x03a05601);
1327028fa281SKalle Valo 	rtl8xxxu_write32(priv, REG_OFDM0_TR_MUX_PAR, 0x000800e4);
1328028fa281SKalle Valo 	rtl8xxxu_write32(priv, REG_FPGA0_XCD_RF_SW_CTRL, 0x25204000);
1329028fa281SKalle Valo 
1330028fa281SKalle Valo 	/* IQ calibration setting */
1331028fa281SKalle Valo 	val32 = rtl8xxxu_read32(priv, REG_FPGA0_IQK);
1332028fa281SKalle Valo 	u32p_replace_bits(&val32, 0x808000, 0xffffff00);
1333028fa281SKalle Valo 	rtl8xxxu_write32(priv, REG_FPGA0_IQK, val32);
1334028fa281SKalle Valo 	rtl8xxxu_write32(priv, REG_TX_IQK, 0x01007c00);
1335028fa281SKalle Valo 	rtl8xxxu_write32(priv, REG_RX_IQK, 0x01004800);
1336028fa281SKalle Valo 
1337028fa281SKalle Valo 	for (i = 0; i < retry; i++) {
1338028fa281SKalle Valo 		path_a_ok = rtl8710bu_iqk_path_a(priv, &lok_result);
1339028fa281SKalle Valo 
1340028fa281SKalle Valo 		if (path_a_ok == 0x01) {
1341028fa281SKalle Valo 			val32 = rtl8xxxu_read32(priv, REG_TX_POWER_BEFORE_IQK_A);
1342028fa281SKalle Valo 			result[t][0] = (val32 >> 16) & 0x3ff;
1343028fa281SKalle Valo 
1344028fa281SKalle Valo 			val32 = rtl8xxxu_read32(priv, REG_TX_POWER_AFTER_IQK_A);
1345028fa281SKalle Valo 			result[t][1] = (val32 >> 16) & 0x3ff;
1346028fa281SKalle Valo 			break;
1347028fa281SKalle Valo 		} else {
1348028fa281SKalle Valo 			result[t][0] = 0x100;
1349028fa281SKalle Valo 			result[t][1] = 0x0;
1350028fa281SKalle Valo 		}
1351028fa281SKalle Valo 	}
1352028fa281SKalle Valo 
1353028fa281SKalle Valo 	for (i = 0; i < retry; i++) {
1354028fa281SKalle Valo 		path_a_ok = rtl8710bu_rx_iqk_path_a(priv, lok_result);
1355028fa281SKalle Valo 
1356028fa281SKalle Valo 		if (path_a_ok == 0x03) {
1357028fa281SKalle Valo 			val32 = rtl8xxxu_read32(priv, REG_RX_POWER_BEFORE_IQK_A_2);
1358028fa281SKalle Valo 			result[t][2] = (val32 >> 16) & 0x3ff;
1359028fa281SKalle Valo 
1360028fa281SKalle Valo 			val32 = rtl8xxxu_read32(priv, REG_RX_POWER_AFTER_IQK_A_2);
1361028fa281SKalle Valo 			result[t][3] = (val32 >> 16) & 0x3ff;
1362028fa281SKalle Valo 			break;
1363028fa281SKalle Valo 		} else {
1364028fa281SKalle Valo 			result[t][2] = 0x100;
1365028fa281SKalle Valo 			result[t][3] = 0x0;
1366028fa281SKalle Valo 		}
1367028fa281SKalle Valo 	}
1368028fa281SKalle Valo 
1369028fa281SKalle Valo 	if (!path_a_ok)
1370028fa281SKalle Valo 		dev_warn(dev, "%s: Path A IQK failed!\n", __func__);
1371028fa281SKalle Valo 
1372028fa281SKalle Valo 	/* Back to BB mode, load original value */
1373028fa281SKalle Valo 	val32 = rtl8xxxu_read32(priv, REG_FPGA0_IQK);
1374028fa281SKalle Valo 	u32p_replace_bits(&val32, 0, 0xffffff00);
1375028fa281SKalle Valo 	rtl8xxxu_write32(priv, REG_FPGA0_IQK, val32);
1376028fa281SKalle Valo 
1377028fa281SKalle Valo 	if (t == 0)
1378028fa281SKalle Valo 		return;
1379028fa281SKalle Valo 
1380028fa281SKalle Valo 	/* Reload ADDA power saving parameters */
1381028fa281SKalle Valo 	rtl8xxxu_restore_regs(priv, adda_regs, priv->adda_backup, RTL8XXXU_ADDA_REGS);
1382028fa281SKalle Valo 
1383028fa281SKalle Valo 	/* Reload MAC parameters */
1384028fa281SKalle Valo 	rtl8xxxu_restore_mac_regs(priv, iqk_mac_regs, priv->mac_backup);
1385028fa281SKalle Valo 
1386028fa281SKalle Valo 	/* Reload BB parameters */
1387028fa281SKalle Valo 	rtl8xxxu_restore_regs(priv, iqk_bb_regs, priv->bb_backup, RTL8XXXU_BB_REGS);
1388028fa281SKalle Valo 
1389028fa281SKalle Valo 	/* Reload RF path */
1390028fa281SKalle Valo 	rtl8xxxu_write32(priv, REG_S0S1_PATH_SWITCH, path_sel_bb);
1391028fa281SKalle Valo 	rtl8xxxu_write_rfreg(priv, RF_A, RF6052_REG_S0S1, path_sel_rf);
1392028fa281SKalle Valo 
1393028fa281SKalle Valo 	/* Restore RX initial gain */
1394028fa281SKalle Valo 	val32 = rtl8xxxu_read32(priv, REG_OFDM0_XA_AGC_CORE1);
1395028fa281SKalle Valo 	u32p_replace_bits(&val32, 0x50, 0x000000ff);
1396028fa281SKalle Valo 	rtl8xxxu_write32(priv, REG_OFDM0_XA_AGC_CORE1, val32);
1397028fa281SKalle Valo 	val32 = rtl8xxxu_read32(priv, REG_OFDM0_XA_AGC_CORE1);
1398028fa281SKalle Valo 	u32p_replace_bits(&val32, rx_initial_gain & 0xff, 0x000000ff);
1399028fa281SKalle Valo 	rtl8xxxu_write32(priv, REG_OFDM0_XA_AGC_CORE1, val32);
1400028fa281SKalle Valo 
1401028fa281SKalle Valo 	/* Load 0xe30 IQC default value */
1402028fa281SKalle Valo 	rtl8xxxu_write32(priv, REG_TX_IQK_TONE_A, 0x01008c00);
1403028fa281SKalle Valo 	rtl8xxxu_write32(priv, REG_RX_IQK_TONE_A, 0x01008c00);
1404028fa281SKalle Valo }
1405028fa281SKalle Valo 
rtl8710bu_phy_iq_calibrate(struct rtl8xxxu_priv * priv)1406028fa281SKalle Valo static void rtl8710bu_phy_iq_calibrate(struct rtl8xxxu_priv *priv)
1407028fa281SKalle Valo {
1408028fa281SKalle Valo 	struct device *dev = &priv->udev->dev;
1409028fa281SKalle Valo 	int result[4][8]; /* last is final result */
1410028fa281SKalle Valo 	int i, candidate;
1411028fa281SKalle Valo 	bool path_a_ok;
1412028fa281SKalle Valo 	s32 reg_e94, reg_e9c, reg_ea4, reg_eac;
1413028fa281SKalle Valo 	s32 reg_tmp = 0;
1414028fa281SKalle Valo 	bool simu;
1415028fa281SKalle Valo 	u32 path_sel_bb;
1416028fa281SKalle Valo 
1417028fa281SKalle Valo 	/* Save RF path */
1418028fa281SKalle Valo 	path_sel_bb = rtl8xxxu_read32(priv, REG_S0S1_PATH_SWITCH);
1419028fa281SKalle Valo 
1420028fa281SKalle Valo 	memset(result, 0, sizeof(result));
1421028fa281SKalle Valo 	candidate = -1;
1422028fa281SKalle Valo 
1423028fa281SKalle Valo 	path_a_ok = false;
1424028fa281SKalle Valo 
1425028fa281SKalle Valo 	for (i = 0; i < 3; i++) {
1426028fa281SKalle Valo 		rtl8710bu_phy_iqcalibrate(priv, result, i);
1427028fa281SKalle Valo 
1428028fa281SKalle Valo 		if (i == 1) {
1429028fa281SKalle Valo 			simu = rtl8xxxu_gen2_simularity_compare(priv, result, 0, 1);
1430028fa281SKalle Valo 			if (simu) {
1431028fa281SKalle Valo 				candidate = 0;
1432028fa281SKalle Valo 				break;
1433028fa281SKalle Valo 			}
1434028fa281SKalle Valo 		}
1435028fa281SKalle Valo 
1436028fa281SKalle Valo 		if (i == 2) {
1437028fa281SKalle Valo 			simu = rtl8xxxu_gen2_simularity_compare(priv, result, 0, 2);
1438028fa281SKalle Valo 			if (simu) {
1439028fa281SKalle Valo 				candidate = 0;
1440028fa281SKalle Valo 				break;
1441028fa281SKalle Valo 			}
1442028fa281SKalle Valo 
1443028fa281SKalle Valo 			simu = rtl8xxxu_gen2_simularity_compare(priv, result, 1, 2);
1444028fa281SKalle Valo 			if (simu) {
1445028fa281SKalle Valo 				candidate = 1;
1446028fa281SKalle Valo 			} else {
1447028fa281SKalle Valo 				for (i = 0; i < 8; i++)
1448028fa281SKalle Valo 					reg_tmp += result[3][i];
1449028fa281SKalle Valo 
1450028fa281SKalle Valo 				if (reg_tmp)
1451028fa281SKalle Valo 					candidate = 3;
1452028fa281SKalle Valo 				else
1453028fa281SKalle Valo 					candidate = -1;
1454028fa281SKalle Valo 			}
1455028fa281SKalle Valo 		}
1456028fa281SKalle Valo 	}
1457028fa281SKalle Valo 
1458028fa281SKalle Valo 	if (candidate >= 0) {
1459028fa281SKalle Valo 		reg_e94 = result[candidate][0];
1460028fa281SKalle Valo 		reg_e9c = result[candidate][1];
1461028fa281SKalle Valo 		reg_ea4 = result[candidate][2];
1462028fa281SKalle Valo 		reg_eac = result[candidate][3];
1463028fa281SKalle Valo 
1464028fa281SKalle Valo 		dev_dbg(dev, "%s: candidate is %x\n", __func__, candidate);
1465028fa281SKalle Valo 		dev_dbg(dev, "%s: e94=%x e9c=%x ea4=%x eac=%x\n",
1466028fa281SKalle Valo 			__func__, reg_e94, reg_e9c, reg_ea4, reg_eac);
1467028fa281SKalle Valo 
1468028fa281SKalle Valo 		path_a_ok = true;
1469028fa281SKalle Valo 
1470028fa281SKalle Valo 		if (reg_e94)
1471028fa281SKalle Valo 			rtl8xxxu_fill_iqk_matrix_a(priv, path_a_ok, result,
1472028fa281SKalle Valo 						   candidate, (reg_ea4 == 0));
1473028fa281SKalle Valo 	}
1474028fa281SKalle Valo 
1475028fa281SKalle Valo 	rtl8xxxu_save_regs(priv, rtl8xxxu_iqk_phy_iq_bb_reg,
1476028fa281SKalle Valo 			   priv->bb_recovery_backup, RTL8XXXU_BB_REGS);
1477028fa281SKalle Valo 
1478028fa281SKalle Valo 	rtl8xxxu_write32(priv, REG_S0S1_PATH_SWITCH, path_sel_bb);
1479028fa281SKalle Valo }
1480028fa281SKalle Valo 
rtl8710b_emu_to_active(struct rtl8xxxu_priv * priv)1481028fa281SKalle Valo static int rtl8710b_emu_to_active(struct rtl8xxxu_priv *priv)
1482028fa281SKalle Valo {
1483028fa281SKalle Valo 	u8 val8;
1484028fa281SKalle Valo 	int count, ret = 0;
1485028fa281SKalle Valo 
1486028fa281SKalle Valo 	/* AFE power mode selection: 1: LDO mode, 0: Power-cut mode */
1487028fa281SKalle Valo 	val8 = rtl8xxxu_read8(priv, 0x5d);
1488028fa281SKalle Valo 	val8 &= ~BIT(0);
1489028fa281SKalle Valo 	rtl8xxxu_write8(priv, 0x5d, val8);
1490028fa281SKalle Valo 
1491028fa281SKalle Valo 	val8 = rtl8xxxu_read8(priv, REG_SYS_FUNC_8710B);
1492028fa281SKalle Valo 	val8 |= BIT(0);
1493028fa281SKalle Valo 	rtl8xxxu_write8(priv, REG_SYS_FUNC_8710B, val8);
1494028fa281SKalle Valo 
1495028fa281SKalle Valo 	rtl8xxxu_write8(priv, 0x56, 0x0e);
1496028fa281SKalle Valo 
1497028fa281SKalle Valo 	val8 = rtl8xxxu_read8(priv, 0x20);
1498028fa281SKalle Valo 	val8 |= BIT(0);
1499028fa281SKalle Valo 	rtl8xxxu_write8(priv, 0x20, val8);
1500028fa281SKalle Valo 
1501028fa281SKalle Valo 	for (count = RTL8XXXU_MAX_REG_POLL; count; count--) {
1502028fa281SKalle Valo 		val8 = rtl8xxxu_read8(priv, 0x20);
1503028fa281SKalle Valo 		if (!(val8 & BIT(0)))
1504028fa281SKalle Valo 			break;
1505028fa281SKalle Valo 
1506028fa281SKalle Valo 		udelay(10);
1507028fa281SKalle Valo 	}
1508028fa281SKalle Valo 
1509028fa281SKalle Valo 	if (!count)
1510028fa281SKalle Valo 		ret = -EBUSY;
1511028fa281SKalle Valo 
1512028fa281SKalle Valo 	return ret;
1513028fa281SKalle Valo }
1514028fa281SKalle Valo 
rtl8710bu_active_to_emu(struct rtl8xxxu_priv * priv)1515028fa281SKalle Valo static int rtl8710bu_active_to_emu(struct rtl8xxxu_priv *priv)
1516028fa281SKalle Valo {
1517028fa281SKalle Valo 	u8 val8;
1518028fa281SKalle Valo 	u32 val32;
1519028fa281SKalle Valo 	int count, ret = 0;
1520028fa281SKalle Valo 
1521028fa281SKalle Valo 	/* Turn off RF */
1522028fa281SKalle Valo 	val32 = rtl8xxxu_read32(priv, REG_SYS_FUNC_8710B);
1523028fa281SKalle Valo 	val32 &= ~GENMASK(26, 24);
1524028fa281SKalle Valo 	rtl8xxxu_write32(priv, REG_SYS_FUNC_8710B, val32);
1525028fa281SKalle Valo 
1526028fa281SKalle Valo 	/* BB reset */
1527028fa281SKalle Valo 	val32 = rtl8xxxu_read32(priv, REG_SYS_FUNC_8710B);
1528028fa281SKalle Valo 	val32 &= ~GENMASK(17, 16);
1529028fa281SKalle Valo 	rtl8xxxu_write32(priv, REG_SYS_FUNC_8710B, val32);
1530028fa281SKalle Valo 
1531028fa281SKalle Valo 	/* Turn off MAC by HW state machine */
1532028fa281SKalle Valo 	val8 = rtl8xxxu_read8(priv, 0x20);
1533028fa281SKalle Valo 	val8 |= BIT(1);
1534028fa281SKalle Valo 	rtl8xxxu_write8(priv, 0x20, val8);
1535028fa281SKalle Valo 
1536028fa281SKalle Valo 	for (count = RTL8XXXU_MAX_REG_POLL; count; count--) {
1537028fa281SKalle Valo 		val8 = rtl8xxxu_read8(priv, 0x20);
1538028fa281SKalle Valo 		if ((val8 & BIT(1)) == 0) {
1539028fa281SKalle Valo 			ret = 0;
1540028fa281SKalle Valo 			break;
1541028fa281SKalle Valo 		}
1542028fa281SKalle Valo 		udelay(10);
1543028fa281SKalle Valo 	}
1544028fa281SKalle Valo 
1545028fa281SKalle Valo 	if (!count)
1546028fa281SKalle Valo 		ret = -EBUSY;
1547028fa281SKalle Valo 
1548028fa281SKalle Valo 	return ret;
1549028fa281SKalle Valo }
1550028fa281SKalle Valo 
rtl8710bu_active_to_lps(struct rtl8xxxu_priv * priv)1551028fa281SKalle Valo static int rtl8710bu_active_to_lps(struct rtl8xxxu_priv *priv)
1552028fa281SKalle Valo {
1553028fa281SKalle Valo 	struct device *dev = &priv->udev->dev;
1554028fa281SKalle Valo 	u8 val8;
1555028fa281SKalle Valo 	u16 val16;
1556028fa281SKalle Valo 	u32 val32;
1557028fa281SKalle Valo 	int retry, retval;
1558028fa281SKalle Valo 
1559028fa281SKalle Valo 	/* Tx Pause */
1560028fa281SKalle Valo 	rtl8xxxu_write8(priv, REG_TXPAUSE, 0xff);
1561028fa281SKalle Valo 
1562028fa281SKalle Valo 	retry = 100;
1563028fa281SKalle Valo 	retval = -EBUSY;
1564028fa281SKalle Valo 	/*
1565028fa281SKalle Valo 	 * Poll 32 bit wide REG_SCH_TX_CMD for 0x00000000 to ensure no TX is pending.
1566028fa281SKalle Valo 	 */
1567028fa281SKalle Valo 	do {
1568028fa281SKalle Valo 		val32 = rtl8xxxu_read32(priv, REG_SCH_TX_CMD);
1569028fa281SKalle Valo 		if (!val32) {
1570028fa281SKalle Valo 			retval = 0;
1571028fa281SKalle Valo 			break;
1572028fa281SKalle Valo 		}
1573028fa281SKalle Valo 		udelay(10);
1574028fa281SKalle Valo 	} while (retry--);
1575028fa281SKalle Valo 
1576028fa281SKalle Valo 	if (!retry) {
1577028fa281SKalle Valo 		dev_warn(dev, "Failed to flush TX queue\n");
1578028fa281SKalle Valo 		retval = -EBUSY;
1579028fa281SKalle Valo 		return retval;
1580028fa281SKalle Valo 	}
1581028fa281SKalle Valo 
1582028fa281SKalle Valo 	/* Disable CCK and OFDM, clock gated */
1583028fa281SKalle Valo 	val8 = rtl8xxxu_read8(priv, REG_SYS_FUNC);
1584028fa281SKalle Valo 	val8 &= ~SYS_FUNC_BBRSTB;
1585028fa281SKalle Valo 	rtl8xxxu_write8(priv, REG_SYS_FUNC, val8);
1586028fa281SKalle Valo 
1587028fa281SKalle Valo 	udelay(2);
1588028fa281SKalle Valo 
1589028fa281SKalle Valo 	/* Whole BB is reset */
1590028fa281SKalle Valo 	val8 = rtl8xxxu_read8(priv, REG_SYS_FUNC);
1591028fa281SKalle Valo 	val8 &= ~SYS_FUNC_BB_GLB_RSTN;
1592028fa281SKalle Valo 	rtl8xxxu_write8(priv, REG_SYS_FUNC, val8);
1593028fa281SKalle Valo 
1594028fa281SKalle Valo 	/* Reset MAC TRX */
1595028fa281SKalle Valo 	val16 = rtl8xxxu_read16(priv, REG_CR);
1596028fa281SKalle Valo 	val16 &= 0xff00;
1597028fa281SKalle Valo 	val16 |= CR_HCI_RXDMA_ENABLE | CR_HCI_TXDMA_ENABLE;
1598028fa281SKalle Valo 	val16 &= ~CR_SECURITY_ENABLE;
1599028fa281SKalle Valo 	rtl8xxxu_write16(priv, REG_CR, val16);
1600028fa281SKalle Valo 
1601028fa281SKalle Valo 	/* Respond TxOK to scheduler */
1602028fa281SKalle Valo 	val8 = rtl8xxxu_read8(priv, REG_DUAL_TSF_RST);
1603028fa281SKalle Valo 	val8 |= DUAL_TSF_TX_OK;
1604028fa281SKalle Valo 	rtl8xxxu_write8(priv, REG_DUAL_TSF_RST, val8);
1605028fa281SKalle Valo 
1606028fa281SKalle Valo 	return retval;
1607028fa281SKalle Valo }
1608028fa281SKalle Valo 
rtl8710bu_power_on(struct rtl8xxxu_priv * priv)1609028fa281SKalle Valo static int rtl8710bu_power_on(struct rtl8xxxu_priv *priv)
1610028fa281SKalle Valo {
1611028fa281SKalle Valo 	u32 val32;
1612028fa281SKalle Valo 	u16 val16;
1613028fa281SKalle Valo 	u8 val8;
1614028fa281SKalle Valo 	int ret;
1615028fa281SKalle Valo 
1616028fa281SKalle Valo 	rtl8xxxu_write8(priv, REG_USB_ACCESS_TIMEOUT, 0x80);
1617028fa281SKalle Valo 
1618028fa281SKalle Valo 	val8 = rtl8xxxu_read8(priv, REG_SYS_ISO_CTRL);
1619028fa281SKalle Valo 	val8 &= ~BIT(5);
1620028fa281SKalle Valo 	rtl8xxxu_write8(priv, REG_SYS_ISO_CTRL, val8);
1621028fa281SKalle Valo 
1622028fa281SKalle Valo 	val8 = rtl8xxxu_read8(priv, REG_SYS_FUNC_8710B);
1623028fa281SKalle Valo 	val8 |= BIT(0);
1624028fa281SKalle Valo 	rtl8xxxu_write8(priv, REG_SYS_FUNC_8710B, val8);
1625028fa281SKalle Valo 
1626028fa281SKalle Valo 	val8 = rtl8xxxu_read8(priv, 0x20);
1627028fa281SKalle Valo 	val8 |= BIT(0);
1628028fa281SKalle Valo 	rtl8xxxu_write8(priv, 0x20, val8);
1629028fa281SKalle Valo 
1630028fa281SKalle Valo 	rtl8xxxu_write8(priv, REG_AFE_CTRL_8710B, 0);
1631028fa281SKalle Valo 
1632028fa281SKalle Valo 	val8 = rtl8xxxu_read8(priv, REG_WL_STATUS_8710B);
1633028fa281SKalle Valo 	val8 |= BIT(1);
1634028fa281SKalle Valo 	rtl8xxxu_write8(priv, REG_WL_STATUS_8710B, val8);
1635028fa281SKalle Valo 
1636028fa281SKalle Valo 	ret = rtl8710b_emu_to_active(priv);
1637028fa281SKalle Valo 	if (ret)
1638028fa281SKalle Valo 		return ret;
1639028fa281SKalle Valo 
1640028fa281SKalle Valo 	rtl8xxxu_write16(priv, REG_CR, 0);
1641028fa281SKalle Valo 
1642028fa281SKalle Valo 	val16 = rtl8xxxu_read16(priv, REG_CR);
1643028fa281SKalle Valo 
1644028fa281SKalle Valo 	val16 |= CR_HCI_TXDMA_ENABLE | CR_HCI_RXDMA_ENABLE |
1645028fa281SKalle Valo 		 CR_TXDMA_ENABLE | CR_RXDMA_ENABLE |
1646028fa281SKalle Valo 		 CR_PROTOCOL_ENABLE | CR_SCHEDULE_ENABLE |
1647028fa281SKalle Valo 		 CR_SECURITY_ENABLE | CR_CALTIMER_ENABLE;
1648028fa281SKalle Valo 	rtl8xxxu_write16(priv, REG_CR, val16);
1649028fa281SKalle Valo 
1650028fa281SKalle Valo 	/* Enable hardware sequence number. */
1651028fa281SKalle Valo 	val8 = rtl8xxxu_read8(priv, REG_HWSEQ_CTRL);
1652028fa281SKalle Valo 	val8 |= 0x7f;
1653028fa281SKalle Valo 	rtl8xxxu_write8(priv, REG_HWSEQ_CTRL, val8);
1654028fa281SKalle Valo 
1655028fa281SKalle Valo 	udelay(2);
1656028fa281SKalle Valo 
1657028fa281SKalle Valo 	/*
1658028fa281SKalle Valo 	 * Technically the rest was in the rtl8710bu_hal_init function,
1659028fa281SKalle Valo 	 * not the power_on function, but it's fine because we only
1660028fa281SKalle Valo 	 * call power_on from init_device.
1661028fa281SKalle Valo 	 */
1662028fa281SKalle Valo 
1663028fa281SKalle Valo 	val8 = rtl8xxxu_read8(priv, 0xfef9);
1664028fa281SKalle Valo 	val8 &= ~BIT(0);
1665028fa281SKalle Valo 	rtl8xxxu_write8(priv, 0xfef9, val8);
1666028fa281SKalle Valo 
1667028fa281SKalle Valo 	/* Clear the 0x40000138[5] to prevent CM4 Suspend */
1668028fa281SKalle Valo 	val32 = rtl8710b_read_syson_reg(priv, 0x138);
1669028fa281SKalle Valo 	val32 &= ~BIT(5);
1670028fa281SKalle Valo 	rtl8710b_write_syson_reg(priv, 0x138, val32);
1671028fa281SKalle Valo 
1672028fa281SKalle Valo 	return ret;
1673028fa281SKalle Valo }
1674028fa281SKalle Valo 
rtl8710bu_power_off(struct rtl8xxxu_priv * priv)1675028fa281SKalle Valo static void rtl8710bu_power_off(struct rtl8xxxu_priv *priv)
1676028fa281SKalle Valo {
1677028fa281SKalle Valo 	u32 val32;
1678028fa281SKalle Valo 	u8 val8;
1679028fa281SKalle Valo 
1680028fa281SKalle Valo 	rtl8xxxu_flush_fifo(priv);
1681028fa281SKalle Valo 
1682028fa281SKalle Valo 	rtl8xxxu_write32(priv, REG_HISR0_8710B, 0xffffffff);
1683028fa281SKalle Valo 	rtl8xxxu_write32(priv, REG_HIMR0_8710B, 0x0);
1684028fa281SKalle Valo 
1685028fa281SKalle Valo 	/* Set the 0x40000138[5] to allow CM4 Suspend */
1686028fa281SKalle Valo 	val32 = rtl8710b_read_syson_reg(priv, 0x138);
1687028fa281SKalle Valo 	val32 |= BIT(5);
1688028fa281SKalle Valo 	rtl8710b_write_syson_reg(priv, 0x138, val32);
1689028fa281SKalle Valo 
1690028fa281SKalle Valo 	/* Stop rx */
1691028fa281SKalle Valo 	rtl8xxxu_write8(priv, REG_CR, 0x00);
1692028fa281SKalle Valo 
1693028fa281SKalle Valo 	rtl8710bu_active_to_lps(priv);
1694028fa281SKalle Valo 
1695028fa281SKalle Valo 	/* Reset MCU ? */
1696028fa281SKalle Valo 	val8 = rtl8xxxu_read8(priv, REG_8051FW_CTRL_V1_8710B + 3);
1697028fa281SKalle Valo 	val8 &= ~BIT(0);
1698028fa281SKalle Valo 	rtl8xxxu_write8(priv, REG_8051FW_CTRL_V1_8710B + 3, val8);
1699028fa281SKalle Valo 
1700028fa281SKalle Valo 	/* Reset MCU ready status */
1701028fa281SKalle Valo 	rtl8xxxu_write8(priv, REG_8051FW_CTRL_V1_8710B, 0x00);
1702028fa281SKalle Valo 
1703028fa281SKalle Valo 	rtl8710bu_active_to_emu(priv);
1704028fa281SKalle Valo }
1705028fa281SKalle Valo 
rtl8710b_reset_8051(struct rtl8xxxu_priv * priv)1706028fa281SKalle Valo static void rtl8710b_reset_8051(struct rtl8xxxu_priv *priv)
1707028fa281SKalle Valo {
1708028fa281SKalle Valo 	u8 val8;
1709028fa281SKalle Valo 
1710028fa281SKalle Valo 	val8 = rtl8xxxu_read8(priv, REG_8051FW_CTRL_V1_8710B + 3);
1711028fa281SKalle Valo 	val8 &= ~BIT(0);
1712028fa281SKalle Valo 	rtl8xxxu_write8(priv, REG_8051FW_CTRL_V1_8710B + 3, val8);
1713028fa281SKalle Valo 
1714028fa281SKalle Valo 	udelay(50);
1715028fa281SKalle Valo 
1716028fa281SKalle Valo 	val8 = rtl8xxxu_read8(priv, REG_8051FW_CTRL_V1_8710B + 3);
1717028fa281SKalle Valo 	val8 |= BIT(0);
1718028fa281SKalle Valo 	rtl8xxxu_write8(priv, REG_8051FW_CTRL_V1_8710B + 3, val8);
1719028fa281SKalle Valo }
1720028fa281SKalle Valo 
rtl8710b_enable_rf(struct rtl8xxxu_priv * priv)1721028fa281SKalle Valo static void rtl8710b_enable_rf(struct rtl8xxxu_priv *priv)
1722028fa281SKalle Valo {
1723028fa281SKalle Valo 	u32 val32;
1724028fa281SKalle Valo 
1725028fa281SKalle Valo 	rtl8xxxu_write8(priv, REG_RF_CTRL, RF_ENABLE | RF_RSTB | RF_SDMRSTB);
1726028fa281SKalle Valo 
1727028fa281SKalle Valo 	val32 = rtl8xxxu_read32(priv, REG_OFDM0_TRX_PATH_ENABLE);
1728028fa281SKalle Valo 	val32 &= ~(OFDM_RF_PATH_RX_MASK | OFDM_RF_PATH_TX_MASK);
1729028fa281SKalle Valo 	val32 |= OFDM_RF_PATH_RX_A | OFDM_RF_PATH_TX_A;
1730028fa281SKalle Valo 	rtl8xxxu_write32(priv, REG_OFDM0_TRX_PATH_ENABLE, val32);
1731028fa281SKalle Valo 
1732028fa281SKalle Valo 	rtl8xxxu_write8(priv, REG_TXPAUSE, 0x00);
1733028fa281SKalle Valo }
1734028fa281SKalle Valo 
rtl8710b_disable_rf(struct rtl8xxxu_priv * priv)1735028fa281SKalle Valo static void rtl8710b_disable_rf(struct rtl8xxxu_priv *priv)
1736028fa281SKalle Valo {
1737028fa281SKalle Valo 	u32 val32;
1738028fa281SKalle Valo 
1739028fa281SKalle Valo 	val32 = rtl8xxxu_read32(priv, REG_OFDM0_TRX_PATH_ENABLE);
1740028fa281SKalle Valo 	val32 &= ~OFDM_RF_PATH_TX_MASK;
1741028fa281SKalle Valo 	rtl8xxxu_write32(priv, REG_OFDM0_TRX_PATH_ENABLE, val32);
1742028fa281SKalle Valo 
1743028fa281SKalle Valo 	/* Power down RF module */
1744028fa281SKalle Valo 	rtl8xxxu_write_rfreg(priv, RF_A, RF6052_REG_AC, 0);
1745028fa281SKalle Valo }
1746028fa281SKalle Valo 
rtl8710b_usb_quirks(struct rtl8xxxu_priv * priv)1747028fa281SKalle Valo static void rtl8710b_usb_quirks(struct rtl8xxxu_priv *priv)
1748028fa281SKalle Valo {
1749028fa281SKalle Valo 	u16 val16;
1750028fa281SKalle Valo 
1751028fa281SKalle Valo 	rtl8xxxu_gen2_usb_quirks(priv);
1752028fa281SKalle Valo 
1753028fa281SKalle Valo 	val16 = rtl8xxxu_read16(priv, REG_CR);
1754028fa281SKalle Valo 	val16 |= (CR_MAC_TX_ENABLE | CR_MAC_RX_ENABLE);
1755028fa281SKalle Valo 	rtl8xxxu_write16(priv, REG_CR, val16);
1756028fa281SKalle Valo }
1757028fa281SKalle Valo 
1758028fa281SKalle Valo #define XTAL1	GENMASK(29, 24)
1759028fa281SKalle Valo #define XTAL0	GENMASK(23, 18)
1760028fa281SKalle Valo 
rtl8710b_set_crystal_cap(struct rtl8xxxu_priv * priv,u8 crystal_cap)1761028fa281SKalle Valo static void rtl8710b_set_crystal_cap(struct rtl8xxxu_priv *priv, u8 crystal_cap)
1762028fa281SKalle Valo {
1763028fa281SKalle Valo 	struct rtl8xxxu_cfo_tracking *cfo = &priv->cfo_tracking;
1764028fa281SKalle Valo 	u32 val32;
1765028fa281SKalle Valo 
1766028fa281SKalle Valo 	if (crystal_cap == cfo->crystal_cap)
1767028fa281SKalle Valo 		return;
1768028fa281SKalle Valo 
1769028fa281SKalle Valo 	val32 = rtl8710b_read_syson_reg(priv, REG_SYS_XTAL_CTRL0_8710B);
1770028fa281SKalle Valo 
1771028fa281SKalle Valo 	dev_dbg(&priv->udev->dev,
1772028fa281SKalle Valo 		"%s: Adjusting crystal cap from 0x%x (actually 0x%x 0x%x) to 0x%x\n",
1773028fa281SKalle Valo 		__func__,
1774028fa281SKalle Valo 		cfo->crystal_cap,
1775028fa281SKalle Valo 		u32_get_bits(val32, XTAL1),
1776028fa281SKalle Valo 		u32_get_bits(val32, XTAL0),
1777028fa281SKalle Valo 		crystal_cap);
1778028fa281SKalle Valo 
1779028fa281SKalle Valo 	u32p_replace_bits(&val32, crystal_cap, XTAL1);
1780028fa281SKalle Valo 	u32p_replace_bits(&val32, crystal_cap, XTAL0);
1781028fa281SKalle Valo 	rtl8710b_write_syson_reg(priv, REG_SYS_XTAL_CTRL0_8710B, val32);
1782028fa281SKalle Valo 
1783028fa281SKalle Valo 	cfo->crystal_cap = crystal_cap;
1784028fa281SKalle Valo }
1785028fa281SKalle Valo 
rtl8710b_cck_rssi(struct rtl8xxxu_priv * priv,struct rtl8723au_phy_stats * phy_stats)1786028fa281SKalle Valo static s8 rtl8710b_cck_rssi(struct rtl8xxxu_priv *priv, struct rtl8723au_phy_stats *phy_stats)
1787028fa281SKalle Valo {
1788028fa281SKalle Valo 	struct jaguar2_phy_stats_type0 *phy_stats0 = (struct jaguar2_phy_stats_type0 *)phy_stats;
1789028fa281SKalle Valo 	u8 lna_idx = (phy_stats0->lna_h << 3) | phy_stats0->lna_l;
1790028fa281SKalle Valo 	u8 vga_idx = phy_stats0->vga;
1791028fa281SKalle Valo 	s8 rx_pwr_all = 0x00;
1792028fa281SKalle Valo 
1793028fa281SKalle Valo 	switch (lna_idx) {
1794028fa281SKalle Valo 	case 7:
1795028fa281SKalle Valo 		rx_pwr_all = -52 - (2 * vga_idx);
1796028fa281SKalle Valo 		break;
1797028fa281SKalle Valo 	case 6:
1798028fa281SKalle Valo 		rx_pwr_all = -42 - (2 * vga_idx);
1799028fa281SKalle Valo 		break;
1800028fa281SKalle Valo 	case 5:
1801028fa281SKalle Valo 		rx_pwr_all = -36 - (2 * vga_idx);
1802028fa281SKalle Valo 		break;
1803028fa281SKalle Valo 	case 3:
1804028fa281SKalle Valo 		rx_pwr_all = -12 - (2 * vga_idx);
1805028fa281SKalle Valo 		break;
1806028fa281SKalle Valo 	case 2:
1807028fa281SKalle Valo 		rx_pwr_all = 0 - (2 * vga_idx);
1808028fa281SKalle Valo 		break;
1809028fa281SKalle Valo 	default:
1810028fa281SKalle Valo 		rx_pwr_all = 0;
1811028fa281SKalle Valo 		break;
1812028fa281SKalle Valo 	}
1813028fa281SKalle Valo 
1814028fa281SKalle Valo 	return rx_pwr_all;
1815028fa281SKalle Valo }
1816028fa281SKalle Valo 
1817028fa281SKalle Valo struct rtl8xxxu_fileops rtl8710bu_fops = {
1818028fa281SKalle Valo 	.identify_chip = rtl8710bu_identify_chip,
1819028fa281SKalle Valo 	.parse_efuse = rtl8710bu_parse_efuse,
1820028fa281SKalle Valo 	.load_firmware = rtl8710bu_load_firmware,
1821028fa281SKalle Valo 	.power_on = rtl8710bu_power_on,
1822028fa281SKalle Valo 	.power_off = rtl8710bu_power_off,
1823028fa281SKalle Valo 	.read_efuse = rtl8710b_read_efuse,
1824028fa281SKalle Valo 	.reset_8051 = rtl8710b_reset_8051,
1825028fa281SKalle Valo 	.llt_init = rtl8xxxu_auto_llt_table,
1826028fa281SKalle Valo 	.init_phy_bb = rtl8710bu_init_phy_bb,
1827028fa281SKalle Valo 	.init_phy_rf = rtl8710bu_init_phy_rf,
1828028fa281SKalle Valo 	.phy_lc_calibrate = rtl8188f_phy_lc_calibrate,
1829028fa281SKalle Valo 	.phy_iq_calibrate = rtl8710bu_phy_iq_calibrate,
1830028fa281SKalle Valo 	.config_channel = rtl8710bu_config_channel,
1831028fa281SKalle Valo 	.parse_rx_desc = rtl8xxxu_parse_rxdesc24,
1832028fa281SKalle Valo 	.parse_phystats = jaguar2_rx_parse_phystats,
1833028fa281SKalle Valo 	.init_aggregation = rtl8710bu_init_aggregation,
1834028fa281SKalle Valo 	.init_statistics = rtl8710bu_init_statistics,
1835028fa281SKalle Valo 	.init_burst = rtl8xxxu_init_burst,
1836028fa281SKalle Valo 	.enable_rf = rtl8710b_enable_rf,
1837028fa281SKalle Valo 	.disable_rf = rtl8710b_disable_rf,
1838028fa281SKalle Valo 	.usb_quirks = rtl8710b_usb_quirks,
1839028fa281SKalle Valo 	.set_tx_power = rtl8188f_set_tx_power,
1840028fa281SKalle Valo 	.update_rate_mask = rtl8xxxu_gen2_update_rate_mask,
1841028fa281SKalle Valo 	.report_connect = rtl8xxxu_gen2_report_connect,
1842028fa281SKalle Valo 	.report_rssi = rtl8xxxu_gen2_report_rssi,
1843028fa281SKalle Valo 	.fill_txdesc = rtl8xxxu_fill_txdesc_v2,
1844028fa281SKalle Valo 	.set_crystal_cap = rtl8710b_set_crystal_cap,
1845028fa281SKalle Valo 	.cck_rssi = rtl8710b_cck_rssi,
1846028fa281SKalle Valo 	.writeN_block_size = 4,
1847028fa281SKalle Valo 	.rx_desc_size = sizeof(struct rtl8xxxu_rxdesc24),
1848028fa281SKalle Valo 	.tx_desc_size = sizeof(struct rtl8xxxu_txdesc40),
1849028fa281SKalle Valo 	.has_tx_report = 1,
1850028fa281SKalle Valo 	.gen2_thermal_meter = 1,
1851028fa281SKalle Valo 	.needs_full_init = 1,
1852028fa281SKalle Valo 	.init_reg_rxfltmap = 1,
1853028fa281SKalle Valo 	.init_reg_pkt_life_time = 1,
1854028fa281SKalle Valo 	.init_reg_hmtfr = 1,
1855028fa281SKalle Valo 	.ampdu_max_time = 0x5e,
1856028fa281SKalle Valo 	/*
1857028fa281SKalle Valo 	 * The RTL8710BU vendor driver uses 0x50 here and it works fine,
1858028fa281SKalle Valo 	 * but in rtl8xxxu 0x50 causes slow upload and random packet loss. Why?
1859028fa281SKalle Valo 	 */
1860028fa281SKalle Valo 	.ustime_tsf_edca = 0x28,
1861028fa281SKalle Valo 	.max_aggr_num = 0x0c14,
1862028fa281SKalle Valo 	.supports_ap = 1,
1863028fa281SKalle Valo 	.max_macid_num = 16,
1864028fa281SKalle Valo 	.max_sec_cam_num = 32,
1865028fa281SKalle Valo 	.adda_1t_init = 0x03c00016,
1866028fa281SKalle Valo 	.adda_1t_path_on = 0x03c00016,
1867028fa281SKalle Valo 	.trxff_boundary = 0x3f7f,
1868028fa281SKalle Valo 	.pbp_rx = PBP_PAGE_SIZE_256,
1869028fa281SKalle Valo 	.pbp_tx = PBP_PAGE_SIZE_256,
1870028fa281SKalle Valo 	.mactable = rtl8710b_mac_init_table,
1871028fa281SKalle Valo 	.total_page_num = TX_TOTAL_PAGE_NUM_8723B,
1872028fa281SKalle Valo 	.page_num_hi = TX_PAGE_NUM_HI_PQ_8723B,
1873028fa281SKalle Valo 	.page_num_lo = TX_PAGE_NUM_LO_PQ_8723B,
1874028fa281SKalle Valo 	.page_num_norm = TX_PAGE_NUM_NORM_PQ_8723B,
1875028fa281SKalle Valo };
1876