blob: 34f94bb9c9a669ccb1a3686ffdb588d23563b563 [file] [log] [blame]
Jes Sorensen26f1fad2015-10-14 20:44:51 -04001/*
2 * RTL8XXXU mac80211 USB driver
3 *
Jes Sorenseneb188062016-04-14 16:37:14 -04004 * Copyright (c) 2014 - 2016 Jes Sorensen <Jes.Sorensen@redhat.com>
Jes Sorensen26f1fad2015-10-14 20:44:51 -04005 *
6 * Portions, notably calibration code:
7 * Copyright(c) 2007 - 2011 Realtek Corporation. All rights reserved.
8 *
9 * This driver was written as a replacement for the vendor provided
10 * rtl8723au driver. As the Realtek 8xxx chips are very similar in
11 * their programming interface, I have started adding support for
12 * additional 8xxx chips like the 8192cu, 8188cus, etc.
13 *
14 * This program is free software; you can redistribute it and/or modify it
15 * under the terms of version 2 of the GNU General Public License as
16 * published by the Free Software Foundation.
17 *
18 * This program is distributed in the hope that it will be useful, but WITHOUT
19 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
21 * more details.
22 */
23
24#include <linux/init.h>
25#include <linux/kernel.h>
26#include <linux/sched.h>
27#include <linux/errno.h>
28#include <linux/slab.h>
29#include <linux/module.h>
30#include <linux/spinlock.h>
31#include <linux/list.h>
32#include <linux/usb.h>
33#include <linux/netdevice.h>
34#include <linux/etherdevice.h>
35#include <linux/ethtool.h>
36#include <linux/wireless.h>
37#include <linux/firmware.h>
38#include <linux/moduleparam.h>
39#include <net/mac80211.h>
40#include "rtl8xxxu.h"
41#include "rtl8xxxu_regs.h"
42
43#define DRIVER_NAME "rtl8xxxu"
44
Jes Sorensen599119f2016-04-28 15:19:06 -040045int rtl8xxxu_debug = RTL8XXXU_DEBUG_EFUSE;
Jes Sorensen26f1fad2015-10-14 20:44:51 -040046static bool rtl8xxxu_ht40_2g;
47
48MODULE_AUTHOR("Jes Sorensen <Jes.Sorensen@redhat.com>");
49MODULE_DESCRIPTION("RTL8XXXu USB mac80211 Wireless LAN Driver");
50MODULE_LICENSE("GPL");
51MODULE_FIRMWARE("rtlwifi/rtl8723aufw_A.bin");
52MODULE_FIRMWARE("rtlwifi/rtl8723aufw_B.bin");
53MODULE_FIRMWARE("rtlwifi/rtl8723aufw_B_NoBT.bin");
54MODULE_FIRMWARE("rtlwifi/rtl8192cufw_A.bin");
55MODULE_FIRMWARE("rtlwifi/rtl8192cufw_B.bin");
56MODULE_FIRMWARE("rtlwifi/rtl8192cufw_TMSC.bin");
Jes Sorensenb001e082016-02-29 17:04:02 -050057MODULE_FIRMWARE("rtlwifi/rtl8192eu_nic.bin");
Jes Sorensen35a741f2016-02-29 17:04:10 -050058MODULE_FIRMWARE("rtlwifi/rtl8723bu_nic.bin");
59MODULE_FIRMWARE("rtlwifi/rtl8723bu_bt.bin");
Jes Sorensen26f1fad2015-10-14 20:44:51 -040060
61module_param_named(debug, rtl8xxxu_debug, int, 0600);
62MODULE_PARM_DESC(debug, "Set debug mask");
63module_param_named(ht40_2g, rtl8xxxu_ht40_2g, bool, 0600);
64MODULE_PARM_DESC(ht40_2g, "Enable HT40 support on the 2.4GHz band");
65
66#define USB_VENDOR_ID_REALTEK 0x0bda
67/* Minimum IEEE80211_MAX_FRAME_LEN */
68#define RTL_RX_BUFFER_SIZE IEEE80211_MAX_FRAME_LEN
69#define RTL8XXXU_RX_URBS 32
70#define RTL8XXXU_RX_URB_PENDING_WATER 8
71#define RTL8XXXU_TX_URBS 64
72#define RTL8XXXU_TX_URB_LOW_WATER 25
73#define RTL8XXXU_TX_URB_HIGH_WATER 32
74
75static int rtl8xxxu_submit_rx_urb(struct rtl8xxxu_priv *priv,
76 struct rtl8xxxu_rx_urb *rx_urb);
77
78static struct ieee80211_rate rtl8xxxu_rates[] = {
79 { .bitrate = 10, .hw_value = DESC_RATE_1M, .flags = 0 },
80 { .bitrate = 20, .hw_value = DESC_RATE_2M, .flags = 0 },
81 { .bitrate = 55, .hw_value = DESC_RATE_5_5M, .flags = 0 },
82 { .bitrate = 110, .hw_value = DESC_RATE_11M, .flags = 0 },
83 { .bitrate = 60, .hw_value = DESC_RATE_6M, .flags = 0 },
84 { .bitrate = 90, .hw_value = DESC_RATE_9M, .flags = 0 },
85 { .bitrate = 120, .hw_value = DESC_RATE_12M, .flags = 0 },
86 { .bitrate = 180, .hw_value = DESC_RATE_18M, .flags = 0 },
87 { .bitrate = 240, .hw_value = DESC_RATE_24M, .flags = 0 },
88 { .bitrate = 360, .hw_value = DESC_RATE_36M, .flags = 0 },
89 { .bitrate = 480, .hw_value = DESC_RATE_48M, .flags = 0 },
90 { .bitrate = 540, .hw_value = DESC_RATE_54M, .flags = 0 },
91};
92
93static struct ieee80211_channel rtl8xxxu_channels_2g[] = {
Johannes Berg57fbcce2016-04-12 15:56:15 +020094 { .band = NL80211_BAND_2GHZ, .center_freq = 2412,
Jes Sorensen26f1fad2015-10-14 20:44:51 -040095 .hw_value = 1, .max_power = 30 },
Johannes Berg57fbcce2016-04-12 15:56:15 +020096 { .band = NL80211_BAND_2GHZ, .center_freq = 2417,
Jes Sorensen26f1fad2015-10-14 20:44:51 -040097 .hw_value = 2, .max_power = 30 },
Johannes Berg57fbcce2016-04-12 15:56:15 +020098 { .band = NL80211_BAND_2GHZ, .center_freq = 2422,
Jes Sorensen26f1fad2015-10-14 20:44:51 -040099 .hw_value = 3, .max_power = 30 },
Johannes Berg57fbcce2016-04-12 15:56:15 +0200100 { .band = NL80211_BAND_2GHZ, .center_freq = 2427,
Jes Sorensen26f1fad2015-10-14 20:44:51 -0400101 .hw_value = 4, .max_power = 30 },
Johannes Berg57fbcce2016-04-12 15:56:15 +0200102 { .band = NL80211_BAND_2GHZ, .center_freq = 2432,
Jes Sorensen26f1fad2015-10-14 20:44:51 -0400103 .hw_value = 5, .max_power = 30 },
Johannes Berg57fbcce2016-04-12 15:56:15 +0200104 { .band = NL80211_BAND_2GHZ, .center_freq = 2437,
Jes Sorensen26f1fad2015-10-14 20:44:51 -0400105 .hw_value = 6, .max_power = 30 },
Johannes Berg57fbcce2016-04-12 15:56:15 +0200106 { .band = NL80211_BAND_2GHZ, .center_freq = 2442,
Jes Sorensen26f1fad2015-10-14 20:44:51 -0400107 .hw_value = 7, .max_power = 30 },
Johannes Berg57fbcce2016-04-12 15:56:15 +0200108 { .band = NL80211_BAND_2GHZ, .center_freq = 2447,
Jes Sorensen26f1fad2015-10-14 20:44:51 -0400109 .hw_value = 8, .max_power = 30 },
Johannes Berg57fbcce2016-04-12 15:56:15 +0200110 { .band = NL80211_BAND_2GHZ, .center_freq = 2452,
Jes Sorensen26f1fad2015-10-14 20:44:51 -0400111 .hw_value = 9, .max_power = 30 },
Johannes Berg57fbcce2016-04-12 15:56:15 +0200112 { .band = NL80211_BAND_2GHZ, .center_freq = 2457,
Jes Sorensen26f1fad2015-10-14 20:44:51 -0400113 .hw_value = 10, .max_power = 30 },
Johannes Berg57fbcce2016-04-12 15:56:15 +0200114 { .band = NL80211_BAND_2GHZ, .center_freq = 2462,
Jes Sorensen26f1fad2015-10-14 20:44:51 -0400115 .hw_value = 11, .max_power = 30 },
Johannes Berg57fbcce2016-04-12 15:56:15 +0200116 { .band = NL80211_BAND_2GHZ, .center_freq = 2467,
Jes Sorensen26f1fad2015-10-14 20:44:51 -0400117 .hw_value = 12, .max_power = 30 },
Johannes Berg57fbcce2016-04-12 15:56:15 +0200118 { .band = NL80211_BAND_2GHZ, .center_freq = 2472,
Jes Sorensen26f1fad2015-10-14 20:44:51 -0400119 .hw_value = 13, .max_power = 30 },
Johannes Berg57fbcce2016-04-12 15:56:15 +0200120 { .band = NL80211_BAND_2GHZ, .center_freq = 2484,
Jes Sorensen26f1fad2015-10-14 20:44:51 -0400121 .hw_value = 14, .max_power = 30 }
122};
123
124static struct ieee80211_supported_band rtl8xxxu_supported_band = {
125 .channels = rtl8xxxu_channels_2g,
126 .n_channels = ARRAY_SIZE(rtl8xxxu_channels_2g),
127 .bitrates = rtl8xxxu_rates,
128 .n_bitrates = ARRAY_SIZE(rtl8xxxu_rates),
129};
130
Jes Sorensen20e3b2e2016-04-28 15:19:08 -0400131struct rtl8xxxu_reg8val rtl8xxxu_gen1_mac_init_table[] = {
Jes Sorensen26f1fad2015-10-14 20:44:51 -0400132 {0x420, 0x80}, {0x423, 0x00}, {0x430, 0x00}, {0x431, 0x00},
133 {0x432, 0x00}, {0x433, 0x01}, {0x434, 0x04}, {0x435, 0x05},
134 {0x436, 0x06}, {0x437, 0x07}, {0x438, 0x00}, {0x439, 0x00},
135 {0x43a, 0x00}, {0x43b, 0x01}, {0x43c, 0x04}, {0x43d, 0x05},
136 {0x43e, 0x06}, {0x43f, 0x07}, {0x440, 0x5d}, {0x441, 0x01},
137 {0x442, 0x00}, {0x444, 0x15}, {0x445, 0xf0}, {0x446, 0x0f},
138 {0x447, 0x00}, {0x458, 0x41}, {0x459, 0xa8}, {0x45a, 0x72},
139 {0x45b, 0xb9}, {0x460, 0x66}, {0x461, 0x66}, {0x462, 0x08},
140 {0x463, 0x03}, {0x4c8, 0xff}, {0x4c9, 0x08}, {0x4cc, 0xff},
141 {0x4cd, 0xff}, {0x4ce, 0x01}, {0x500, 0x26}, {0x501, 0xa2},
142 {0x502, 0x2f}, {0x503, 0x00}, {0x504, 0x28}, {0x505, 0xa3},
143 {0x506, 0x5e}, {0x507, 0x00}, {0x508, 0x2b}, {0x509, 0xa4},
144 {0x50a, 0x5e}, {0x50b, 0x00}, {0x50c, 0x4f}, {0x50d, 0xa4},
145 {0x50e, 0x00}, {0x50f, 0x00}, {0x512, 0x1c}, {0x514, 0x0a},
146 {0x515, 0x10}, {0x516, 0x0a}, {0x517, 0x10}, {0x51a, 0x16},
147 {0x524, 0x0f}, {0x525, 0x4f}, {0x546, 0x40}, {0x547, 0x00},
148 {0x550, 0x10}, {0x551, 0x10}, {0x559, 0x02}, {0x55a, 0x02},
149 {0x55d, 0xff}, {0x605, 0x30}, {0x608, 0x0e}, {0x609, 0x2a},
150 {0x652, 0x20}, {0x63c, 0x0a}, {0x63d, 0x0a}, {0x63e, 0x0e},
151 {0x63f, 0x0e}, {0x66e, 0x05}, {0x700, 0x21}, {0x701, 0x43},
152 {0x702, 0x65}, {0x703, 0x87}, {0x708, 0x21}, {0x709, 0x43},
153 {0x70a, 0x65}, {0x70b, 0x87}, {0xffff, 0xff},
154};
155
156static struct rtl8xxxu_reg32val rtl8723a_phy_1t_init_table[] = {
157 {0x800, 0x80040000}, {0x804, 0x00000003},
158 {0x808, 0x0000fc00}, {0x80c, 0x0000000a},
159 {0x810, 0x10001331}, {0x814, 0x020c3d10},
160 {0x818, 0x02200385}, {0x81c, 0x00000000},
161 {0x820, 0x01000100}, {0x824, 0x00390004},
162 {0x828, 0x00000000}, {0x82c, 0x00000000},
163 {0x830, 0x00000000}, {0x834, 0x00000000},
164 {0x838, 0x00000000}, {0x83c, 0x00000000},
165 {0x840, 0x00010000}, {0x844, 0x00000000},
166 {0x848, 0x00000000}, {0x84c, 0x00000000},
167 {0x850, 0x00000000}, {0x854, 0x00000000},
168 {0x858, 0x569a569a}, {0x85c, 0x001b25a4},
169 {0x860, 0x66f60110}, {0x864, 0x061f0130},
170 {0x868, 0x00000000}, {0x86c, 0x32323200},
171 {0x870, 0x07000760}, {0x874, 0x22004000},
172 {0x878, 0x00000808}, {0x87c, 0x00000000},
173 {0x880, 0xc0083070}, {0x884, 0x000004d5},
174 {0x888, 0x00000000}, {0x88c, 0xccc000c0},
175 {0x890, 0x00000800}, {0x894, 0xfffffffe},
176 {0x898, 0x40302010}, {0x89c, 0x00706050},
177 {0x900, 0x00000000}, {0x904, 0x00000023},
178 {0x908, 0x00000000}, {0x90c, 0x81121111},
179 {0xa00, 0x00d047c8}, {0xa04, 0x80ff000c},
180 {0xa08, 0x8c838300}, {0xa0c, 0x2e68120f},
181 {0xa10, 0x9500bb78}, {0xa14, 0x11144028},
182 {0xa18, 0x00881117}, {0xa1c, 0x89140f00},
183 {0xa20, 0x1a1b0000}, {0xa24, 0x090e1317},
184 {0xa28, 0x00000204}, {0xa2c, 0x00d30000},
185 {0xa70, 0x101fbf00}, {0xa74, 0x00000007},
186 {0xa78, 0x00000900},
187 {0xc00, 0x48071d40}, {0xc04, 0x03a05611},
188 {0xc08, 0x000000e4}, {0xc0c, 0x6c6c6c6c},
189 {0xc10, 0x08800000}, {0xc14, 0x40000100},
190 {0xc18, 0x08800000}, {0xc1c, 0x40000100},
191 {0xc20, 0x00000000}, {0xc24, 0x00000000},
192 {0xc28, 0x00000000}, {0xc2c, 0x00000000},
193 {0xc30, 0x69e9ac44}, {0xc34, 0x469652af},
194 {0xc38, 0x49795994}, {0xc3c, 0x0a97971c},
195 {0xc40, 0x1f7c403f}, {0xc44, 0x000100b7},
196 {0xc48, 0xec020107}, {0xc4c, 0x007f037f},
197 {0xc50, 0x69543420}, {0xc54, 0x43bc0094},
198 {0xc58, 0x69543420}, {0xc5c, 0x433c0094},
199 {0xc60, 0x00000000}, {0xc64, 0x7112848b},
200 {0xc68, 0x47c00bff}, {0xc6c, 0x00000036},
201 {0xc70, 0x2c7f000d}, {0xc74, 0x018610db},
202 {0xc78, 0x0000001f}, {0xc7c, 0x00b91612},
203 {0xc80, 0x40000100}, {0xc84, 0x20f60000},
204 {0xc88, 0x40000100}, {0xc8c, 0x20200000},
205 {0xc90, 0x00121820}, {0xc94, 0x00000000},
206 {0xc98, 0x00121820}, {0xc9c, 0x00007f7f},
207 {0xca0, 0x00000000}, {0xca4, 0x00000080},
208 {0xca8, 0x00000000}, {0xcac, 0x00000000},
209 {0xcb0, 0x00000000}, {0xcb4, 0x00000000},
210 {0xcb8, 0x00000000}, {0xcbc, 0x28000000},
211 {0xcc0, 0x00000000}, {0xcc4, 0x00000000},
212 {0xcc8, 0x00000000}, {0xccc, 0x00000000},
213 {0xcd0, 0x00000000}, {0xcd4, 0x00000000},
214 {0xcd8, 0x64b22427}, {0xcdc, 0x00766932},
215 {0xce0, 0x00222222}, {0xce4, 0x00000000},
216 {0xce8, 0x37644302}, {0xcec, 0x2f97d40c},
217 {0xd00, 0x00080740}, {0xd04, 0x00020401},
218 {0xd08, 0x0000907f}, {0xd0c, 0x20010201},
219 {0xd10, 0xa0633333}, {0xd14, 0x3333bc43},
220 {0xd18, 0x7a8f5b6b}, {0xd2c, 0xcc979975},
221 {0xd30, 0x00000000}, {0xd34, 0x80608000},
222 {0xd38, 0x00000000}, {0xd3c, 0x00027293},
223 {0xd40, 0x00000000}, {0xd44, 0x00000000},
224 {0xd48, 0x00000000}, {0xd4c, 0x00000000},
225 {0xd50, 0x6437140a}, {0xd54, 0x00000000},
226 {0xd58, 0x00000000}, {0xd5c, 0x30032064},
227 {0xd60, 0x4653de68}, {0xd64, 0x04518a3c},
228 {0xd68, 0x00002101}, {0xd6c, 0x2a201c16},
229 {0xd70, 0x1812362e}, {0xd74, 0x322c2220},
230 {0xd78, 0x000e3c24}, {0xe00, 0x2a2a2a2a},
231 {0xe04, 0x2a2a2a2a}, {0xe08, 0x03902a2a},
232 {0xe10, 0x2a2a2a2a}, {0xe14, 0x2a2a2a2a},
233 {0xe18, 0x2a2a2a2a}, {0xe1c, 0x2a2a2a2a},
234 {0xe28, 0x00000000}, {0xe30, 0x1000dc1f},
235 {0xe34, 0x10008c1f}, {0xe38, 0x02140102},
236 {0xe3c, 0x681604c2}, {0xe40, 0x01007c00},
237 {0xe44, 0x01004800}, {0xe48, 0xfb000000},
238 {0xe4c, 0x000028d1}, {0xe50, 0x1000dc1f},
239 {0xe54, 0x10008c1f}, {0xe58, 0x02140102},
240 {0xe5c, 0x28160d05}, {0xe60, 0x00000008},
241 {0xe68, 0x001b25a4}, {0xe6c, 0x631b25a0},
242 {0xe70, 0x631b25a0}, {0xe74, 0x081b25a0},
243 {0xe78, 0x081b25a0}, {0xe7c, 0x081b25a0},
244 {0xe80, 0x081b25a0}, {0xe84, 0x631b25a0},
245 {0xe88, 0x081b25a0}, {0xe8c, 0x631b25a0},
246 {0xed0, 0x631b25a0}, {0xed4, 0x631b25a0},
247 {0xed8, 0x631b25a0}, {0xedc, 0x001b25a0},
248 {0xee0, 0x001b25a0}, {0xeec, 0x6b1b25a0},
249 {0xf14, 0x00000003}, {0xf4c, 0x00000000},
250 {0xf00, 0x00000300},
251 {0xffff, 0xffffffff},
252};
253
254static struct rtl8xxxu_reg32val rtl8192cu_phy_2t_init_table[] = {
255 {0x024, 0x0011800f}, {0x028, 0x00ffdb83},
256 {0x800, 0x80040002}, {0x804, 0x00000003},
257 {0x808, 0x0000fc00}, {0x80c, 0x0000000a},
258 {0x810, 0x10000330}, {0x814, 0x020c3d10},
259 {0x818, 0x02200385}, {0x81c, 0x00000000},
260 {0x820, 0x01000100}, {0x824, 0x00390004},
261 {0x828, 0x01000100}, {0x82c, 0x00390004},
262 {0x830, 0x27272727}, {0x834, 0x27272727},
263 {0x838, 0x27272727}, {0x83c, 0x27272727},
264 {0x840, 0x00010000}, {0x844, 0x00010000},
265 {0x848, 0x27272727}, {0x84c, 0x27272727},
266 {0x850, 0x00000000}, {0x854, 0x00000000},
267 {0x858, 0x569a569a}, {0x85c, 0x0c1b25a4},
268 {0x860, 0x66e60230}, {0x864, 0x061f0130},
269 {0x868, 0x27272727}, {0x86c, 0x2b2b2b27},
270 {0x870, 0x07000700}, {0x874, 0x22184000},
271 {0x878, 0x08080808}, {0x87c, 0x00000000},
272 {0x880, 0xc0083070}, {0x884, 0x000004d5},
273 {0x888, 0x00000000}, {0x88c, 0xcc0000c0},
274 {0x890, 0x00000800}, {0x894, 0xfffffffe},
275 {0x898, 0x40302010}, {0x89c, 0x00706050},
276 {0x900, 0x00000000}, {0x904, 0x00000023},
277 {0x908, 0x00000000}, {0x90c, 0x81121313},
278 {0xa00, 0x00d047c8}, {0xa04, 0x80ff000c},
279 {0xa08, 0x8c838300}, {0xa0c, 0x2e68120f},
280 {0xa10, 0x9500bb78}, {0xa14, 0x11144028},
281 {0xa18, 0x00881117}, {0xa1c, 0x89140f00},
282 {0xa20, 0x1a1b0000}, {0xa24, 0x090e1317},
283 {0xa28, 0x00000204}, {0xa2c, 0x00d30000},
284 {0xa70, 0x101fbf00}, {0xa74, 0x00000007},
285 {0xc00, 0x48071d40}, {0xc04, 0x03a05633},
286 {0xc08, 0x000000e4}, {0xc0c, 0x6c6c6c6c},
287 {0xc10, 0x08800000}, {0xc14, 0x40000100},
288 {0xc18, 0x08800000}, {0xc1c, 0x40000100},
289 {0xc20, 0x00000000}, {0xc24, 0x00000000},
290 {0xc28, 0x00000000}, {0xc2c, 0x00000000},
291 {0xc30, 0x69e9ac44}, {0xc34, 0x469652cf},
292 {0xc38, 0x49795994}, {0xc3c, 0x0a97971c},
293 {0xc40, 0x1f7c403f}, {0xc44, 0x000100b7},
294 {0xc48, 0xec020107}, {0xc4c, 0x007f037f},
295 {0xc50, 0x69543420}, {0xc54, 0x43bc0094},
296 {0xc58, 0x69543420}, {0xc5c, 0x433c0094},
297 {0xc60, 0x00000000}, {0xc64, 0x5116848b},
298 {0xc68, 0x47c00bff}, {0xc6c, 0x00000036},
299 {0xc70, 0x2c7f000d}, {0xc74, 0x2186115b},
300 {0xc78, 0x0000001f}, {0xc7c, 0x00b99612},
301 {0xc80, 0x40000100}, {0xc84, 0x20f60000},
302 {0xc88, 0x40000100}, {0xc8c, 0xa0e40000},
303 {0xc90, 0x00121820}, {0xc94, 0x00000000},
304 {0xc98, 0x00121820}, {0xc9c, 0x00007f7f},
305 {0xca0, 0x00000000}, {0xca4, 0x00000080},
306 {0xca8, 0x00000000}, {0xcac, 0x00000000},
307 {0xcb0, 0x00000000}, {0xcb4, 0x00000000},
308 {0xcb8, 0x00000000}, {0xcbc, 0x28000000},
309 {0xcc0, 0x00000000}, {0xcc4, 0x00000000},
310 {0xcc8, 0x00000000}, {0xccc, 0x00000000},
311 {0xcd0, 0x00000000}, {0xcd4, 0x00000000},
312 {0xcd8, 0x64b22427}, {0xcdc, 0x00766932},
313 {0xce0, 0x00222222}, {0xce4, 0x00000000},
314 {0xce8, 0x37644302}, {0xcec, 0x2f97d40c},
315 {0xd00, 0x00080740}, {0xd04, 0x00020403},
316 {0xd08, 0x0000907f}, {0xd0c, 0x20010201},
317 {0xd10, 0xa0633333}, {0xd14, 0x3333bc43},
318 {0xd18, 0x7a8f5b6b}, {0xd2c, 0xcc979975},
319 {0xd30, 0x00000000}, {0xd34, 0x80608000},
320 {0xd38, 0x00000000}, {0xd3c, 0x00027293},
321 {0xd40, 0x00000000}, {0xd44, 0x00000000},
322 {0xd48, 0x00000000}, {0xd4c, 0x00000000},
323 {0xd50, 0x6437140a}, {0xd54, 0x00000000},
324 {0xd58, 0x00000000}, {0xd5c, 0x30032064},
325 {0xd60, 0x4653de68}, {0xd64, 0x04518a3c},
326 {0xd68, 0x00002101}, {0xd6c, 0x2a201c16},
327 {0xd70, 0x1812362e}, {0xd74, 0x322c2220},
328 {0xd78, 0x000e3c24}, {0xe00, 0x2a2a2a2a},
329 {0xe04, 0x2a2a2a2a}, {0xe08, 0x03902a2a},
330 {0xe10, 0x2a2a2a2a}, {0xe14, 0x2a2a2a2a},
331 {0xe18, 0x2a2a2a2a}, {0xe1c, 0x2a2a2a2a},
332 {0xe28, 0x00000000}, {0xe30, 0x1000dc1f},
333 {0xe34, 0x10008c1f}, {0xe38, 0x02140102},
334 {0xe3c, 0x681604c2}, {0xe40, 0x01007c00},
335 {0xe44, 0x01004800}, {0xe48, 0xfb000000},
336 {0xe4c, 0x000028d1}, {0xe50, 0x1000dc1f},
337 {0xe54, 0x10008c1f}, {0xe58, 0x02140102},
338 {0xe5c, 0x28160d05}, {0xe60, 0x00000010},
339 {0xe68, 0x001b25a4}, {0xe6c, 0x63db25a4},
340 {0xe70, 0x63db25a4}, {0xe74, 0x0c1b25a4},
341 {0xe78, 0x0c1b25a4}, {0xe7c, 0x0c1b25a4},
342 {0xe80, 0x0c1b25a4}, {0xe84, 0x63db25a4},
343 {0xe88, 0x0c1b25a4}, {0xe8c, 0x63db25a4},
344 {0xed0, 0x63db25a4}, {0xed4, 0x63db25a4},
345 {0xed8, 0x63db25a4}, {0xedc, 0x001b25a4},
346 {0xee0, 0x001b25a4}, {0xeec, 0x6fdb25a4},
347 {0xf14, 0x00000003}, {0xf4c, 0x00000000},
348 {0xf00, 0x00000300},
349 {0xffff, 0xffffffff},
350};
351
352static struct rtl8xxxu_reg32val rtl8188ru_phy_1t_highpa_table[] = {
353 {0x024, 0x0011800f}, {0x028, 0x00ffdb83},
354 {0x040, 0x000c0004}, {0x800, 0x80040000},
355 {0x804, 0x00000001}, {0x808, 0x0000fc00},
356 {0x80c, 0x0000000a}, {0x810, 0x10005388},
357 {0x814, 0x020c3d10}, {0x818, 0x02200385},
358 {0x81c, 0x00000000}, {0x820, 0x01000100},
359 {0x824, 0x00390204}, {0x828, 0x00000000},
360 {0x82c, 0x00000000}, {0x830, 0x00000000},
361 {0x834, 0x00000000}, {0x838, 0x00000000},
362 {0x83c, 0x00000000}, {0x840, 0x00010000},
363 {0x844, 0x00000000}, {0x848, 0x00000000},
364 {0x84c, 0x00000000}, {0x850, 0x00000000},
365 {0x854, 0x00000000}, {0x858, 0x569a569a},
366 {0x85c, 0x001b25a4}, {0x860, 0x66e60230},
367 {0x864, 0x061f0130}, {0x868, 0x00000000},
368 {0x86c, 0x20202000}, {0x870, 0x03000300},
369 {0x874, 0x22004000}, {0x878, 0x00000808},
370 {0x87c, 0x00ffc3f1}, {0x880, 0xc0083070},
371 {0x884, 0x000004d5}, {0x888, 0x00000000},
372 {0x88c, 0xccc000c0}, {0x890, 0x00000800},
373 {0x894, 0xfffffffe}, {0x898, 0x40302010},
374 {0x89c, 0x00706050}, {0x900, 0x00000000},
375 {0x904, 0x00000023}, {0x908, 0x00000000},
376 {0x90c, 0x81121111}, {0xa00, 0x00d047c8},
377 {0xa04, 0x80ff000c}, {0xa08, 0x8c838300},
378 {0xa0c, 0x2e68120f}, {0xa10, 0x9500bb78},
379 {0xa14, 0x11144028}, {0xa18, 0x00881117},
380 {0xa1c, 0x89140f00}, {0xa20, 0x15160000},
381 {0xa24, 0x070b0f12}, {0xa28, 0x00000104},
382 {0xa2c, 0x00d30000}, {0xa70, 0x101fbf00},
383 {0xa74, 0x00000007}, {0xc00, 0x48071d40},
384 {0xc04, 0x03a05611}, {0xc08, 0x000000e4},
385 {0xc0c, 0x6c6c6c6c}, {0xc10, 0x08800000},
386 {0xc14, 0x40000100}, {0xc18, 0x08800000},
387 {0xc1c, 0x40000100}, {0xc20, 0x00000000},
388 {0xc24, 0x00000000}, {0xc28, 0x00000000},
389 {0xc2c, 0x00000000}, {0xc30, 0x69e9ac44},
390 {0xc34, 0x469652cf}, {0xc38, 0x49795994},
391 {0xc3c, 0x0a97971c}, {0xc40, 0x1f7c403f},
392 {0xc44, 0x000100b7}, {0xc48, 0xec020107},
393 {0xc4c, 0x007f037f}, {0xc50, 0x6954342e},
394 {0xc54, 0x43bc0094}, {0xc58, 0x6954342f},
395 {0xc5c, 0x433c0094}, {0xc60, 0x00000000},
396 {0xc64, 0x5116848b}, {0xc68, 0x47c00bff},
397 {0xc6c, 0x00000036}, {0xc70, 0x2c46000d},
398 {0xc74, 0x018610db}, {0xc78, 0x0000001f},
399 {0xc7c, 0x00b91612}, {0xc80, 0x24000090},
400 {0xc84, 0x20f60000}, {0xc88, 0x24000090},
401 {0xc8c, 0x20200000}, {0xc90, 0x00121820},
402 {0xc94, 0x00000000}, {0xc98, 0x00121820},
403 {0xc9c, 0x00007f7f}, {0xca0, 0x00000000},
404 {0xca4, 0x00000080}, {0xca8, 0x00000000},
405 {0xcac, 0x00000000}, {0xcb0, 0x00000000},
406 {0xcb4, 0x00000000}, {0xcb8, 0x00000000},
407 {0xcbc, 0x28000000}, {0xcc0, 0x00000000},
408 {0xcc4, 0x00000000}, {0xcc8, 0x00000000},
409 {0xccc, 0x00000000}, {0xcd0, 0x00000000},
410 {0xcd4, 0x00000000}, {0xcd8, 0x64b22427},
411 {0xcdc, 0x00766932}, {0xce0, 0x00222222},
412 {0xce4, 0x00000000}, {0xce8, 0x37644302},
413 {0xcec, 0x2f97d40c}, {0xd00, 0x00080740},
414 {0xd04, 0x00020401}, {0xd08, 0x0000907f},
415 {0xd0c, 0x20010201}, {0xd10, 0xa0633333},
416 {0xd14, 0x3333bc43}, {0xd18, 0x7a8f5b6b},
417 {0xd2c, 0xcc979975}, {0xd30, 0x00000000},
418 {0xd34, 0x80608000}, {0xd38, 0x00000000},
419 {0xd3c, 0x00027293}, {0xd40, 0x00000000},
420 {0xd44, 0x00000000}, {0xd48, 0x00000000},
421 {0xd4c, 0x00000000}, {0xd50, 0x6437140a},
422 {0xd54, 0x00000000}, {0xd58, 0x00000000},
423 {0xd5c, 0x30032064}, {0xd60, 0x4653de68},
424 {0xd64, 0x04518a3c}, {0xd68, 0x00002101},
425 {0xd6c, 0x2a201c16}, {0xd70, 0x1812362e},
426 {0xd74, 0x322c2220}, {0xd78, 0x000e3c24},
427 {0xe00, 0x24242424}, {0xe04, 0x24242424},
428 {0xe08, 0x03902024}, {0xe10, 0x24242424},
429 {0xe14, 0x24242424}, {0xe18, 0x24242424},
430 {0xe1c, 0x24242424}, {0xe28, 0x00000000},
431 {0xe30, 0x1000dc1f}, {0xe34, 0x10008c1f},
432 {0xe38, 0x02140102}, {0xe3c, 0x681604c2},
433 {0xe40, 0x01007c00}, {0xe44, 0x01004800},
434 {0xe48, 0xfb000000}, {0xe4c, 0x000028d1},
435 {0xe50, 0x1000dc1f}, {0xe54, 0x10008c1f},
436 {0xe58, 0x02140102}, {0xe5c, 0x28160d05},
437 {0xe60, 0x00000008}, {0xe68, 0x001b25a4},
438 {0xe6c, 0x631b25a0}, {0xe70, 0x631b25a0},
439 {0xe74, 0x081b25a0}, {0xe78, 0x081b25a0},
440 {0xe7c, 0x081b25a0}, {0xe80, 0x081b25a0},
441 {0xe84, 0x631b25a0}, {0xe88, 0x081b25a0},
442 {0xe8c, 0x631b25a0}, {0xed0, 0x631b25a0},
443 {0xed4, 0x631b25a0}, {0xed8, 0x631b25a0},
444 {0xedc, 0x001b25a0}, {0xee0, 0x001b25a0},
445 {0xeec, 0x6b1b25a0}, {0xee8, 0x31555448},
446 {0xf14, 0x00000003}, {0xf4c, 0x00000000},
447 {0xf00, 0x00000300},
448 {0xffff, 0xffffffff},
449};
450
451static struct rtl8xxxu_reg32val rtl8xxx_agc_standard_table[] = {
452 {0xc78, 0x7b000001}, {0xc78, 0x7b010001},
453 {0xc78, 0x7b020001}, {0xc78, 0x7b030001},
454 {0xc78, 0x7b040001}, {0xc78, 0x7b050001},
455 {0xc78, 0x7a060001}, {0xc78, 0x79070001},
456 {0xc78, 0x78080001}, {0xc78, 0x77090001},
457 {0xc78, 0x760a0001}, {0xc78, 0x750b0001},
458 {0xc78, 0x740c0001}, {0xc78, 0x730d0001},
459 {0xc78, 0x720e0001}, {0xc78, 0x710f0001},
460 {0xc78, 0x70100001}, {0xc78, 0x6f110001},
461 {0xc78, 0x6e120001}, {0xc78, 0x6d130001},
462 {0xc78, 0x6c140001}, {0xc78, 0x6b150001},
463 {0xc78, 0x6a160001}, {0xc78, 0x69170001},
464 {0xc78, 0x68180001}, {0xc78, 0x67190001},
465 {0xc78, 0x661a0001}, {0xc78, 0x651b0001},
466 {0xc78, 0x641c0001}, {0xc78, 0x631d0001},
467 {0xc78, 0x621e0001}, {0xc78, 0x611f0001},
468 {0xc78, 0x60200001}, {0xc78, 0x49210001},
469 {0xc78, 0x48220001}, {0xc78, 0x47230001},
470 {0xc78, 0x46240001}, {0xc78, 0x45250001},
471 {0xc78, 0x44260001}, {0xc78, 0x43270001},
472 {0xc78, 0x42280001}, {0xc78, 0x41290001},
473 {0xc78, 0x402a0001}, {0xc78, 0x262b0001},
474 {0xc78, 0x252c0001}, {0xc78, 0x242d0001},
475 {0xc78, 0x232e0001}, {0xc78, 0x222f0001},
476 {0xc78, 0x21300001}, {0xc78, 0x20310001},
477 {0xc78, 0x06320001}, {0xc78, 0x05330001},
478 {0xc78, 0x04340001}, {0xc78, 0x03350001},
479 {0xc78, 0x02360001}, {0xc78, 0x01370001},
480 {0xc78, 0x00380001}, {0xc78, 0x00390001},
481 {0xc78, 0x003a0001}, {0xc78, 0x003b0001},
482 {0xc78, 0x003c0001}, {0xc78, 0x003d0001},
483 {0xc78, 0x003e0001}, {0xc78, 0x003f0001},
484 {0xc78, 0x7b400001}, {0xc78, 0x7b410001},
485 {0xc78, 0x7b420001}, {0xc78, 0x7b430001},
486 {0xc78, 0x7b440001}, {0xc78, 0x7b450001},
487 {0xc78, 0x7a460001}, {0xc78, 0x79470001},
488 {0xc78, 0x78480001}, {0xc78, 0x77490001},
489 {0xc78, 0x764a0001}, {0xc78, 0x754b0001},
490 {0xc78, 0x744c0001}, {0xc78, 0x734d0001},
491 {0xc78, 0x724e0001}, {0xc78, 0x714f0001},
492 {0xc78, 0x70500001}, {0xc78, 0x6f510001},
493 {0xc78, 0x6e520001}, {0xc78, 0x6d530001},
494 {0xc78, 0x6c540001}, {0xc78, 0x6b550001},
495 {0xc78, 0x6a560001}, {0xc78, 0x69570001},
496 {0xc78, 0x68580001}, {0xc78, 0x67590001},
497 {0xc78, 0x665a0001}, {0xc78, 0x655b0001},
498 {0xc78, 0x645c0001}, {0xc78, 0x635d0001},
499 {0xc78, 0x625e0001}, {0xc78, 0x615f0001},
500 {0xc78, 0x60600001}, {0xc78, 0x49610001},
501 {0xc78, 0x48620001}, {0xc78, 0x47630001},
502 {0xc78, 0x46640001}, {0xc78, 0x45650001},
503 {0xc78, 0x44660001}, {0xc78, 0x43670001},
504 {0xc78, 0x42680001}, {0xc78, 0x41690001},
505 {0xc78, 0x406a0001}, {0xc78, 0x266b0001},
506 {0xc78, 0x256c0001}, {0xc78, 0x246d0001},
507 {0xc78, 0x236e0001}, {0xc78, 0x226f0001},
508 {0xc78, 0x21700001}, {0xc78, 0x20710001},
509 {0xc78, 0x06720001}, {0xc78, 0x05730001},
510 {0xc78, 0x04740001}, {0xc78, 0x03750001},
511 {0xc78, 0x02760001}, {0xc78, 0x01770001},
512 {0xc78, 0x00780001}, {0xc78, 0x00790001},
513 {0xc78, 0x007a0001}, {0xc78, 0x007b0001},
514 {0xc78, 0x007c0001}, {0xc78, 0x007d0001},
515 {0xc78, 0x007e0001}, {0xc78, 0x007f0001},
516 {0xc78, 0x3800001e}, {0xc78, 0x3801001e},
517 {0xc78, 0x3802001e}, {0xc78, 0x3803001e},
518 {0xc78, 0x3804001e}, {0xc78, 0x3805001e},
519 {0xc78, 0x3806001e}, {0xc78, 0x3807001e},
520 {0xc78, 0x3808001e}, {0xc78, 0x3c09001e},
521 {0xc78, 0x3e0a001e}, {0xc78, 0x400b001e},
522 {0xc78, 0x440c001e}, {0xc78, 0x480d001e},
523 {0xc78, 0x4c0e001e}, {0xc78, 0x500f001e},
524 {0xc78, 0x5210001e}, {0xc78, 0x5611001e},
525 {0xc78, 0x5a12001e}, {0xc78, 0x5e13001e},
526 {0xc78, 0x6014001e}, {0xc78, 0x6015001e},
527 {0xc78, 0x6016001e}, {0xc78, 0x6217001e},
528 {0xc78, 0x6218001e}, {0xc78, 0x6219001e},
529 {0xc78, 0x621a001e}, {0xc78, 0x621b001e},
530 {0xc78, 0x621c001e}, {0xc78, 0x621d001e},
531 {0xc78, 0x621e001e}, {0xc78, 0x621f001e},
532 {0xffff, 0xffffffff}
533};
534
535static struct rtl8xxxu_reg32val rtl8xxx_agc_highpa_table[] = {
536 {0xc78, 0x7b000001}, {0xc78, 0x7b010001},
537 {0xc78, 0x7b020001}, {0xc78, 0x7b030001},
538 {0xc78, 0x7b040001}, {0xc78, 0x7b050001},
539 {0xc78, 0x7b060001}, {0xc78, 0x7b070001},
540 {0xc78, 0x7b080001}, {0xc78, 0x7a090001},
541 {0xc78, 0x790a0001}, {0xc78, 0x780b0001},
542 {0xc78, 0x770c0001}, {0xc78, 0x760d0001},
543 {0xc78, 0x750e0001}, {0xc78, 0x740f0001},
544 {0xc78, 0x73100001}, {0xc78, 0x72110001},
545 {0xc78, 0x71120001}, {0xc78, 0x70130001},
546 {0xc78, 0x6f140001}, {0xc78, 0x6e150001},
547 {0xc78, 0x6d160001}, {0xc78, 0x6c170001},
548 {0xc78, 0x6b180001}, {0xc78, 0x6a190001},
549 {0xc78, 0x691a0001}, {0xc78, 0x681b0001},
550 {0xc78, 0x671c0001}, {0xc78, 0x661d0001},
551 {0xc78, 0x651e0001}, {0xc78, 0x641f0001},
552 {0xc78, 0x63200001}, {0xc78, 0x62210001},
553 {0xc78, 0x61220001}, {0xc78, 0x60230001},
554 {0xc78, 0x46240001}, {0xc78, 0x45250001},
555 {0xc78, 0x44260001}, {0xc78, 0x43270001},
556 {0xc78, 0x42280001}, {0xc78, 0x41290001},
557 {0xc78, 0x402a0001}, {0xc78, 0x262b0001},
558 {0xc78, 0x252c0001}, {0xc78, 0x242d0001},
559 {0xc78, 0x232e0001}, {0xc78, 0x222f0001},
560 {0xc78, 0x21300001}, {0xc78, 0x20310001},
561 {0xc78, 0x06320001}, {0xc78, 0x05330001},
562 {0xc78, 0x04340001}, {0xc78, 0x03350001},
563 {0xc78, 0x02360001}, {0xc78, 0x01370001},
564 {0xc78, 0x00380001}, {0xc78, 0x00390001},
565 {0xc78, 0x003a0001}, {0xc78, 0x003b0001},
566 {0xc78, 0x003c0001}, {0xc78, 0x003d0001},
567 {0xc78, 0x003e0001}, {0xc78, 0x003f0001},
568 {0xc78, 0x7b400001}, {0xc78, 0x7b410001},
569 {0xc78, 0x7b420001}, {0xc78, 0x7b430001},
570 {0xc78, 0x7b440001}, {0xc78, 0x7b450001},
571 {0xc78, 0x7b460001}, {0xc78, 0x7b470001},
572 {0xc78, 0x7b480001}, {0xc78, 0x7a490001},
573 {0xc78, 0x794a0001}, {0xc78, 0x784b0001},
574 {0xc78, 0x774c0001}, {0xc78, 0x764d0001},
575 {0xc78, 0x754e0001}, {0xc78, 0x744f0001},
576 {0xc78, 0x73500001}, {0xc78, 0x72510001},
577 {0xc78, 0x71520001}, {0xc78, 0x70530001},
578 {0xc78, 0x6f540001}, {0xc78, 0x6e550001},
579 {0xc78, 0x6d560001}, {0xc78, 0x6c570001},
580 {0xc78, 0x6b580001}, {0xc78, 0x6a590001},
581 {0xc78, 0x695a0001}, {0xc78, 0x685b0001},
582 {0xc78, 0x675c0001}, {0xc78, 0x665d0001},
583 {0xc78, 0x655e0001}, {0xc78, 0x645f0001},
584 {0xc78, 0x63600001}, {0xc78, 0x62610001},
585 {0xc78, 0x61620001}, {0xc78, 0x60630001},
586 {0xc78, 0x46640001}, {0xc78, 0x45650001},
587 {0xc78, 0x44660001}, {0xc78, 0x43670001},
588 {0xc78, 0x42680001}, {0xc78, 0x41690001},
589 {0xc78, 0x406a0001}, {0xc78, 0x266b0001},
590 {0xc78, 0x256c0001}, {0xc78, 0x246d0001},
591 {0xc78, 0x236e0001}, {0xc78, 0x226f0001},
592 {0xc78, 0x21700001}, {0xc78, 0x20710001},
593 {0xc78, 0x06720001}, {0xc78, 0x05730001},
594 {0xc78, 0x04740001}, {0xc78, 0x03750001},
595 {0xc78, 0x02760001}, {0xc78, 0x01770001},
596 {0xc78, 0x00780001}, {0xc78, 0x00790001},
597 {0xc78, 0x007a0001}, {0xc78, 0x007b0001},
598 {0xc78, 0x007c0001}, {0xc78, 0x007d0001},
599 {0xc78, 0x007e0001}, {0xc78, 0x007f0001},
600 {0xc78, 0x3800001e}, {0xc78, 0x3801001e},
601 {0xc78, 0x3802001e}, {0xc78, 0x3803001e},
602 {0xc78, 0x3804001e}, {0xc78, 0x3805001e},
603 {0xc78, 0x3806001e}, {0xc78, 0x3807001e},
604 {0xc78, 0x3808001e}, {0xc78, 0x3c09001e},
605 {0xc78, 0x3e0a001e}, {0xc78, 0x400b001e},
606 {0xc78, 0x440c001e}, {0xc78, 0x480d001e},
607 {0xc78, 0x4c0e001e}, {0xc78, 0x500f001e},
608 {0xc78, 0x5210001e}, {0xc78, 0x5611001e},
609 {0xc78, 0x5a12001e}, {0xc78, 0x5e13001e},
610 {0xc78, 0x6014001e}, {0xc78, 0x6015001e},
611 {0xc78, 0x6016001e}, {0xc78, 0x6217001e},
612 {0xc78, 0x6218001e}, {0xc78, 0x6219001e},
613 {0xc78, 0x621a001e}, {0xc78, 0x621b001e},
614 {0xc78, 0x621c001e}, {0xc78, 0x621d001e},
615 {0xc78, 0x621e001e}, {0xc78, 0x621f001e},
616 {0xffff, 0xffffffff}
617};
618
Jes Sorensen26f1fad2015-10-14 20:44:51 -0400619static struct rtl8xxxu_rfregs rtl8xxxu_rfregs[] = {
620 { /* RF_A */
621 .hssiparm1 = REG_FPGA0_XA_HSSI_PARM1,
622 .hssiparm2 = REG_FPGA0_XA_HSSI_PARM2,
623 .lssiparm = REG_FPGA0_XA_LSSI_PARM,
624 .hspiread = REG_HSPI_XA_READBACK,
625 .lssiread = REG_FPGA0_XA_LSSI_READBACK,
626 .rf_sw_ctrl = REG_FPGA0_XA_RF_SW_CTRL,
627 },
628 { /* RF_B */
629 .hssiparm1 = REG_FPGA0_XB_HSSI_PARM1,
630 .hssiparm2 = REG_FPGA0_XB_HSSI_PARM2,
631 .lssiparm = REG_FPGA0_XB_LSSI_PARM,
632 .hspiread = REG_HSPI_XB_READBACK,
633 .lssiread = REG_FPGA0_XB_LSSI_READBACK,
634 .rf_sw_ctrl = REG_FPGA0_XB_RF_SW_CTRL,
635 },
636};
637
Jes Sorensen599119f2016-04-28 15:19:06 -0400638const u32 rtl8xxxu_iqk_phy_iq_bb_reg[RTL8XXXU_BB_REGS] = {
Jes Sorensen26f1fad2015-10-14 20:44:51 -0400639 REG_OFDM0_XA_RX_IQ_IMBALANCE,
640 REG_OFDM0_XB_RX_IQ_IMBALANCE,
641 REG_OFDM0_ENERGY_CCA_THRES,
642 REG_OFDM0_AGCR_SSI_TABLE,
643 REG_OFDM0_XA_TX_IQ_IMBALANCE,
644 REG_OFDM0_XB_TX_IQ_IMBALANCE,
645 REG_OFDM0_XC_TX_AFE,
646 REG_OFDM0_XD_TX_AFE,
647 REG_OFDM0_RX_IQ_EXT_ANTA
648};
649
Jes Sorensen599119f2016-04-28 15:19:06 -0400650u8 rtl8xxxu_read8(struct rtl8xxxu_priv *priv, u16 addr)
Jes Sorensen26f1fad2015-10-14 20:44:51 -0400651{
652 struct usb_device *udev = priv->udev;
653 int len;
654 u8 data;
655
656 mutex_lock(&priv->usb_buf_mutex);
657 len = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
658 REALTEK_USB_CMD_REQ, REALTEK_USB_READ,
659 addr, 0, &priv->usb_buf.val8, sizeof(u8),
660 RTW_USB_CONTROL_MSG_TIMEOUT);
661 data = priv->usb_buf.val8;
662 mutex_unlock(&priv->usb_buf_mutex);
663
664 if (rtl8xxxu_debug & RTL8XXXU_DEBUG_REG_READ)
665 dev_info(&udev->dev, "%s(%04x) = 0x%02x, len %i\n",
666 __func__, addr, data, len);
667 return data;
668}
669
Jes Sorensen599119f2016-04-28 15:19:06 -0400670u16 rtl8xxxu_read16(struct rtl8xxxu_priv *priv, u16 addr)
Jes Sorensen26f1fad2015-10-14 20:44:51 -0400671{
672 struct usb_device *udev = priv->udev;
673 int len;
674 u16 data;
675
676 mutex_lock(&priv->usb_buf_mutex);
677 len = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
678 REALTEK_USB_CMD_REQ, REALTEK_USB_READ,
679 addr, 0, &priv->usb_buf.val16, sizeof(u16),
680 RTW_USB_CONTROL_MSG_TIMEOUT);
681 data = le16_to_cpu(priv->usb_buf.val16);
682 mutex_unlock(&priv->usb_buf_mutex);
683
684 if (rtl8xxxu_debug & RTL8XXXU_DEBUG_REG_READ)
685 dev_info(&udev->dev, "%s(%04x) = 0x%04x, len %i\n",
686 __func__, addr, data, len);
687 return data;
688}
689
Jes Sorensen599119f2016-04-28 15:19:06 -0400690u32 rtl8xxxu_read32(struct rtl8xxxu_priv *priv, u16 addr)
Jes Sorensen26f1fad2015-10-14 20:44:51 -0400691{
692 struct usb_device *udev = priv->udev;
693 int len;
694 u32 data;
695
696 mutex_lock(&priv->usb_buf_mutex);
697 len = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
698 REALTEK_USB_CMD_REQ, REALTEK_USB_READ,
699 addr, 0, &priv->usb_buf.val32, sizeof(u32),
700 RTW_USB_CONTROL_MSG_TIMEOUT);
701 data = le32_to_cpu(priv->usb_buf.val32);
702 mutex_unlock(&priv->usb_buf_mutex);
703
704 if (rtl8xxxu_debug & RTL8XXXU_DEBUG_REG_READ)
705 dev_info(&udev->dev, "%s(%04x) = 0x%08x, len %i\n",
706 __func__, addr, data, len);
707 return data;
708}
709
Jes Sorensen599119f2016-04-28 15:19:06 -0400710int rtl8xxxu_write8(struct rtl8xxxu_priv *priv, u16 addr, u8 val)
Jes Sorensen26f1fad2015-10-14 20:44:51 -0400711{
712 struct usb_device *udev = priv->udev;
713 int ret;
714
715 mutex_lock(&priv->usb_buf_mutex);
716 priv->usb_buf.val8 = val;
717 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
718 REALTEK_USB_CMD_REQ, REALTEK_USB_WRITE,
719 addr, 0, &priv->usb_buf.val8, sizeof(u8),
720 RTW_USB_CONTROL_MSG_TIMEOUT);
721
722 mutex_unlock(&priv->usb_buf_mutex);
723
724 if (rtl8xxxu_debug & RTL8XXXU_DEBUG_REG_WRITE)
725 dev_info(&udev->dev, "%s(%04x) = 0x%02x\n",
726 __func__, addr, val);
727 return ret;
728}
729
Jes Sorensen599119f2016-04-28 15:19:06 -0400730int rtl8xxxu_write16(struct rtl8xxxu_priv *priv, u16 addr, u16 val)
Jes Sorensen26f1fad2015-10-14 20:44:51 -0400731{
732 struct usb_device *udev = priv->udev;
733 int ret;
734
735 mutex_lock(&priv->usb_buf_mutex);
736 priv->usb_buf.val16 = cpu_to_le16(val);
737 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
738 REALTEK_USB_CMD_REQ, REALTEK_USB_WRITE,
739 addr, 0, &priv->usb_buf.val16, sizeof(u16),
740 RTW_USB_CONTROL_MSG_TIMEOUT);
741 mutex_unlock(&priv->usb_buf_mutex);
742
743 if (rtl8xxxu_debug & RTL8XXXU_DEBUG_REG_WRITE)
744 dev_info(&udev->dev, "%s(%04x) = 0x%04x\n",
745 __func__, addr, val);
746 return ret;
747}
748
Jes Sorensen599119f2016-04-28 15:19:06 -0400749int rtl8xxxu_write32(struct rtl8xxxu_priv *priv, u16 addr, u32 val)
Jes Sorensen26f1fad2015-10-14 20:44:51 -0400750{
751 struct usb_device *udev = priv->udev;
752 int ret;
753
754 mutex_lock(&priv->usb_buf_mutex);
755 priv->usb_buf.val32 = cpu_to_le32(val);
756 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
757 REALTEK_USB_CMD_REQ, REALTEK_USB_WRITE,
758 addr, 0, &priv->usb_buf.val32, sizeof(u32),
759 RTW_USB_CONTROL_MSG_TIMEOUT);
760 mutex_unlock(&priv->usb_buf_mutex);
761
762 if (rtl8xxxu_debug & RTL8XXXU_DEBUG_REG_WRITE)
763 dev_info(&udev->dev, "%s(%04x) = 0x%08x\n",
764 __func__, addr, val);
765 return ret;
766}
767
768static int
769rtl8xxxu_writeN(struct rtl8xxxu_priv *priv, u16 addr, u8 *buf, u16 len)
770{
771 struct usb_device *udev = priv->udev;
772 int blocksize = priv->fops->writeN_block_size;
773 int ret, i, count, remainder;
774
775 count = len / blocksize;
776 remainder = len % blocksize;
777
778 for (i = 0; i < count; i++) {
779 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
780 REALTEK_USB_CMD_REQ, REALTEK_USB_WRITE,
781 addr, 0, buf, blocksize,
782 RTW_USB_CONTROL_MSG_TIMEOUT);
783 if (ret != blocksize)
784 goto write_error;
785
786 addr += blocksize;
787 buf += blocksize;
788 }
789
790 if (remainder) {
791 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
792 REALTEK_USB_CMD_REQ, REALTEK_USB_WRITE,
793 addr, 0, buf, remainder,
794 RTW_USB_CONTROL_MSG_TIMEOUT);
795 if (ret != remainder)
796 goto write_error;
797 }
798
799 return len;
800
801write_error:
802 dev_info(&udev->dev,
803 "%s: Failed to write block at addr: %04x size: %04x\n",
804 __func__, addr, blocksize);
805 return -EAGAIN;
806}
807
Jes Sorensen599119f2016-04-28 15:19:06 -0400808u32 rtl8xxxu_read_rfreg(struct rtl8xxxu_priv *priv,
809 enum rtl8xxxu_rfpath path, u8 reg)
Jes Sorensen26f1fad2015-10-14 20:44:51 -0400810{
811 u32 hssia, val32, retval;
812
813 hssia = rtl8xxxu_read32(priv, REG_FPGA0_XA_HSSI_PARM2);
814 if (path != RF_A)
815 val32 = rtl8xxxu_read32(priv, rtl8xxxu_rfregs[path].hssiparm2);
816 else
817 val32 = hssia;
818
819 val32 &= ~FPGA0_HSSI_PARM2_ADDR_MASK;
820 val32 |= (reg << FPGA0_HSSI_PARM2_ADDR_SHIFT);
821 val32 |= FPGA0_HSSI_PARM2_EDGE_READ;
822 hssia &= ~FPGA0_HSSI_PARM2_EDGE_READ;
823 rtl8xxxu_write32(priv, REG_FPGA0_XA_HSSI_PARM2, hssia);
824
825 udelay(10);
826
827 rtl8xxxu_write32(priv, rtl8xxxu_rfregs[path].hssiparm2, val32);
828 udelay(100);
829
830 hssia |= FPGA0_HSSI_PARM2_EDGE_READ;
831 rtl8xxxu_write32(priv, REG_FPGA0_XA_HSSI_PARM2, hssia);
832 udelay(10);
833
834 val32 = rtl8xxxu_read32(priv, rtl8xxxu_rfregs[path].hssiparm1);
835 if (val32 & FPGA0_HSSI_PARM1_PI)
836 retval = rtl8xxxu_read32(priv, rtl8xxxu_rfregs[path].hspiread);
837 else
838 retval = rtl8xxxu_read32(priv, rtl8xxxu_rfregs[path].lssiread);
839
840 retval &= 0xfffff;
841
842 if (rtl8xxxu_debug & RTL8XXXU_DEBUG_RFREG_READ)
843 dev_info(&priv->udev->dev, "%s(%02x) = 0x%06x\n",
844 __func__, reg, retval);
845 return retval;
846}
847
Jes Sorensen22a31d42016-02-29 17:04:15 -0500848/*
849 * The RTL8723BU driver indicates that registers 0xb2 and 0xb6 can
850 * have write issues in high temperature conditions. We may have to
851 * retry writing them.
852 */
Jes Sorensen599119f2016-04-28 15:19:06 -0400853int rtl8xxxu_write_rfreg(struct rtl8xxxu_priv *priv,
854 enum rtl8xxxu_rfpath path, u8 reg, u32 data)
Jes Sorensen26f1fad2015-10-14 20:44:51 -0400855{
856 int ret, retval;
Jes Sorensen2949b9e2016-04-07 14:19:25 -0400857 u32 dataaddr, val32;
Jes Sorensen26f1fad2015-10-14 20:44:51 -0400858
859 if (rtl8xxxu_debug & RTL8XXXU_DEBUG_RFREG_WRITE)
860 dev_info(&priv->udev->dev, "%s(%02x) = 0x%06x\n",
861 __func__, reg, data);
862
863 data &= FPGA0_LSSI_PARM_DATA_MASK;
864 dataaddr = (reg << FPGA0_LSSI_PARM_ADDR_SHIFT) | data;
865
Jes Sorensen2949b9e2016-04-07 14:19:25 -0400866 if (priv->rtl_chip == RTL8192E) {
867 val32 = rtl8xxxu_read32(priv, REG_FPGA0_POWER_SAVE);
868 val32 &= ~0x20000;
869 rtl8xxxu_write32(priv, REG_FPGA0_POWER_SAVE, val32);
870 }
871
Jes Sorensen26f1fad2015-10-14 20:44:51 -0400872 /* Use XB for path B */
873 ret = rtl8xxxu_write32(priv, rtl8xxxu_rfregs[path].lssiparm, dataaddr);
874 if (ret != sizeof(dataaddr))
875 retval = -EIO;
876 else
877 retval = 0;
878
879 udelay(1);
880
Jes Sorensen2949b9e2016-04-07 14:19:25 -0400881 if (priv->rtl_chip == RTL8192E) {
882 val32 = rtl8xxxu_read32(priv, REG_FPGA0_POWER_SAVE);
883 val32 |= 0x20000;
884 rtl8xxxu_write32(priv, REG_FPGA0_POWER_SAVE, val32);
885 }
886
Jes Sorensen26f1fad2015-10-14 20:44:51 -0400887 return retval;
888}
889
Jes Sorensen6c46ca32016-04-28 15:19:07 -0400890int rtl8723a_h2c_cmd(struct rtl8xxxu_priv *priv, struct h2c_cmd *h2c, int len)
Jes Sorensen26f1fad2015-10-14 20:44:51 -0400891{
892 struct device *dev = &priv->udev->dev;
893 int mbox_nr, retry, retval = 0;
894 int mbox_reg, mbox_ext_reg;
895 u8 val8;
896
897 mutex_lock(&priv->h2c_mutex);
898
899 mbox_nr = priv->next_mbox;
900 mbox_reg = REG_HMBOX_0 + (mbox_nr * 4);
Jes Sorensened35d092016-02-29 17:04:19 -0500901 mbox_ext_reg = priv->fops->mbox_ext_reg +
902 (mbox_nr * priv->fops->mbox_ext_width);
Jes Sorensen26f1fad2015-10-14 20:44:51 -0400903
904 /*
905 * MBOX ready?
906 */
907 retry = 100;
908 do {
909 val8 = rtl8xxxu_read8(priv, REG_HMTFR);
910 if (!(val8 & BIT(mbox_nr)))
911 break;
912 } while (retry--);
913
914 if (!retry) {
Jes Sorensenc7a5a192016-02-29 17:04:30 -0500915 dev_info(dev, "%s: Mailbox busy\n", __func__);
Jes Sorensen26f1fad2015-10-14 20:44:51 -0400916 retval = -EBUSY;
917 goto error;
918 }
919
920 /*
921 * Need to swap as it's being swapped again by rtl8xxxu_write16/32()
922 */
Jes Sorensen8da91572016-02-29 17:04:29 -0500923 if (len > sizeof(u32)) {
Jes Sorensened35d092016-02-29 17:04:19 -0500924 if (priv->fops->mbox_ext_width == 4) {
925 rtl8xxxu_write32(priv, mbox_ext_reg,
926 le32_to_cpu(h2c->raw_wide.ext));
927 if (rtl8xxxu_debug & RTL8XXXU_DEBUG_H2C)
928 dev_info(dev, "H2C_EXT %08x\n",
929 le32_to_cpu(h2c->raw_wide.ext));
930 } else {
931 rtl8xxxu_write16(priv, mbox_ext_reg,
932 le16_to_cpu(h2c->raw.ext));
933 if (rtl8xxxu_debug & RTL8XXXU_DEBUG_H2C)
934 dev_info(dev, "H2C_EXT %04x\n",
935 le16_to_cpu(h2c->raw.ext));
936 }
Jes Sorensen26f1fad2015-10-14 20:44:51 -0400937 }
938 rtl8xxxu_write32(priv, mbox_reg, le32_to_cpu(h2c->raw.data));
939 if (rtl8xxxu_debug & RTL8XXXU_DEBUG_H2C)
940 dev_info(dev, "H2C %08x\n", le32_to_cpu(h2c->raw.data));
941
942 priv->next_mbox = (mbox_nr + 1) % H2C_MAX_MBOX;
943
944error:
945 mutex_unlock(&priv->h2c_mutex);
946 return retval;
947}
948
Jes Sorensen20e3b2e2016-04-28 15:19:08 -0400949void rtl8xxxu_gen1_enable_rf(struct rtl8xxxu_priv *priv)
Jes Sorensen26f1fad2015-10-14 20:44:51 -0400950{
951 u8 val8;
952 u32 val32;
953
954 val8 = rtl8xxxu_read8(priv, REG_SPS0_CTRL);
955 val8 |= BIT(0) | BIT(3);
956 rtl8xxxu_write8(priv, REG_SPS0_CTRL, val8);
957
958 val32 = rtl8xxxu_read32(priv, REG_FPGA0_XAB_RF_PARM);
959 val32 &= ~(BIT(4) | BIT(5));
960 val32 |= BIT(3);
961 if (priv->rf_paths == 2) {
962 val32 &= ~(BIT(20) | BIT(21));
963 val32 |= BIT(19);
964 }
965 rtl8xxxu_write32(priv, REG_FPGA0_XAB_RF_PARM, val32);
966
967 val32 = rtl8xxxu_read32(priv, REG_OFDM0_TRX_PATH_ENABLE);
968 val32 &= ~OFDM_RF_PATH_TX_MASK;
969 if (priv->tx_paths == 2)
970 val32 |= OFDM_RF_PATH_TX_A | OFDM_RF_PATH_TX_B;
Jes Sorensenba17d822016-03-31 17:08:39 -0400971 else if (priv->rtl_chip == RTL8192C || priv->rtl_chip == RTL8191C)
Jes Sorensen26f1fad2015-10-14 20:44:51 -0400972 val32 |= OFDM_RF_PATH_TX_B;
973 else
974 val32 |= OFDM_RF_PATH_TX_A;
975 rtl8xxxu_write32(priv, REG_OFDM0_TRX_PATH_ENABLE, val32);
976
977 val32 = rtl8xxxu_read32(priv, REG_FPGA0_RF_MODE);
978 val32 &= ~FPGA_RF_MODE_JAPAN;
979 rtl8xxxu_write32(priv, REG_FPGA0_RF_MODE, val32);
980
981 if (priv->rf_paths == 2)
982 rtl8xxxu_write32(priv, REG_RX_WAIT_CCA, 0x63db25a0);
983 else
984 rtl8xxxu_write32(priv, REG_RX_WAIT_CCA, 0x631b25a0);
985
986 rtl8xxxu_write_rfreg(priv, RF_A, RF6052_REG_AC, 0x32d95);
987 if (priv->rf_paths == 2)
988 rtl8xxxu_write_rfreg(priv, RF_B, RF6052_REG_AC, 0x32d95);
989
990 rtl8xxxu_write8(priv, REG_TXPAUSE, 0x00);
991}
992
Jes Sorensen20e3b2e2016-04-28 15:19:08 -0400993void rtl8xxxu_gen1_disable_rf(struct rtl8xxxu_priv *priv)
Jes Sorensen26f1fad2015-10-14 20:44:51 -0400994{
995 u8 sps0;
996 u32 val32;
997
Jes Sorensen26f1fad2015-10-14 20:44:51 -0400998 sps0 = rtl8xxxu_read8(priv, REG_SPS0_CTRL);
999
1000 /* RF RX code for preamble power saving */
1001 val32 = rtl8xxxu_read32(priv, REG_FPGA0_XAB_RF_PARM);
1002 val32 &= ~(BIT(3) | BIT(4) | BIT(5));
1003 if (priv->rf_paths == 2)
1004 val32 &= ~(BIT(19) | BIT(20) | BIT(21));
1005 rtl8xxxu_write32(priv, REG_FPGA0_XAB_RF_PARM, val32);
1006
1007 /* Disable TX for four paths */
1008 val32 = rtl8xxxu_read32(priv, REG_OFDM0_TRX_PATH_ENABLE);
1009 val32 &= ~OFDM_RF_PATH_TX_MASK;
1010 rtl8xxxu_write32(priv, REG_OFDM0_TRX_PATH_ENABLE, val32);
1011
1012 /* Enable power saving */
1013 val32 = rtl8xxxu_read32(priv, REG_FPGA0_RF_MODE);
1014 val32 |= FPGA_RF_MODE_JAPAN;
1015 rtl8xxxu_write32(priv, REG_FPGA0_RF_MODE, val32);
1016
1017 /* AFE control register to power down bits [30:22] */
1018 if (priv->rf_paths == 2)
1019 rtl8xxxu_write32(priv, REG_RX_WAIT_CCA, 0x00db25a0);
1020 else
1021 rtl8xxxu_write32(priv, REG_RX_WAIT_CCA, 0x001b25a0);
1022
1023 /* Power down RF module */
1024 rtl8xxxu_write_rfreg(priv, RF_A, RF6052_REG_AC, 0);
1025 if (priv->rf_paths == 2)
1026 rtl8xxxu_write_rfreg(priv, RF_B, RF6052_REG_AC, 0);
1027
1028 sps0 &= ~(BIT(0) | BIT(3));
1029 rtl8xxxu_write8(priv, REG_SPS0_CTRL, sps0);
1030}
1031
Jes Sorensen97db5a82016-04-28 15:19:10 -04001032static void rtl8xxxu_stop_tx_beacon(struct rtl8xxxu_priv *priv)
Jes Sorensen26f1fad2015-10-14 20:44:51 -04001033{
1034 u8 val8;
1035
1036 val8 = rtl8xxxu_read8(priv, REG_FWHW_TXQ_CTRL + 2);
1037 val8 &= ~BIT(6);
1038 rtl8xxxu_write8(priv, REG_FWHW_TXQ_CTRL + 2, val8);
1039
1040 rtl8xxxu_write8(priv, REG_TBTT_PROHIBIT + 1, 0x64);
1041 val8 = rtl8xxxu_read8(priv, REG_TBTT_PROHIBIT + 2);
1042 val8 &= ~BIT(0);
1043 rtl8xxxu_write8(priv, REG_TBTT_PROHIBIT + 2, val8);
1044}
1045
1046
1047/*
1048 * The rtl8723a has 3 channel groups for it's efuse settings. It only
1049 * supports the 2.4GHz band, so channels 1 - 14:
1050 * group 0: channels 1 - 3
1051 * group 1: channels 4 - 9
1052 * group 2: channels 10 - 14
1053 *
1054 * Note: We index from 0 in the code
1055 */
1056static int rtl8723a_channel_to_group(int channel)
1057{
1058 int group;
1059
1060 if (channel < 4)
1061 group = 0;
1062 else if (channel < 10)
1063 group = 1;
1064 else
1065 group = 2;
1066
1067 return group;
1068}
1069
Jes Sorensen9e247722016-04-07 14:19:23 -04001070/*
1071 * Valid for rtl8723bu and rtl8192eu
1072 */
Jes Sorensen599119f2016-04-28 15:19:06 -04001073int rtl8xxxu_gen2_channel_to_group(int channel)
Jes Sorensene796dab2016-02-29 17:05:19 -05001074{
1075 int group;
1076
1077 if (channel < 3)
1078 group = 0;
1079 else if (channel < 6)
1080 group = 1;
1081 else if (channel < 9)
1082 group = 2;
1083 else if (channel < 12)
1084 group = 3;
1085 else
1086 group = 4;
1087
1088 return group;
1089}
1090
Jes Sorensen20e3b2e2016-04-28 15:19:08 -04001091void rtl8xxxu_gen1_config_channel(struct ieee80211_hw *hw)
Jes Sorensen26f1fad2015-10-14 20:44:51 -04001092{
1093 struct rtl8xxxu_priv *priv = hw->priv;
1094 u32 val32, rsr;
1095 u8 val8, opmode;
1096 bool ht = true;
1097 int sec_ch_above, channel;
1098 int i;
1099
1100 opmode = rtl8xxxu_read8(priv, REG_BW_OPMODE);
1101 rsr = rtl8xxxu_read32(priv, REG_RESPONSE_RATE_SET);
1102 channel = hw->conf.chandef.chan->hw_value;
1103
1104 switch (hw->conf.chandef.width) {
1105 case NL80211_CHAN_WIDTH_20_NOHT:
1106 ht = false;
1107 case NL80211_CHAN_WIDTH_20:
1108 opmode |= BW_OPMODE_20MHZ;
1109 rtl8xxxu_write8(priv, REG_BW_OPMODE, opmode);
1110
1111 val32 = rtl8xxxu_read32(priv, REG_FPGA0_RF_MODE);
1112 val32 &= ~FPGA_RF_MODE;
1113 rtl8xxxu_write32(priv, REG_FPGA0_RF_MODE, val32);
1114
1115 val32 = rtl8xxxu_read32(priv, REG_FPGA1_RF_MODE);
1116 val32 &= ~FPGA_RF_MODE;
1117 rtl8xxxu_write32(priv, REG_FPGA1_RF_MODE, val32);
1118
1119 val32 = rtl8xxxu_read32(priv, REG_FPGA0_ANALOG2);
1120 val32 |= FPGA0_ANALOG2_20MHZ;
1121 rtl8xxxu_write32(priv, REG_FPGA0_ANALOG2, val32);
1122 break;
1123 case NL80211_CHAN_WIDTH_40:
1124 if (hw->conf.chandef.center_freq1 >
1125 hw->conf.chandef.chan->center_freq) {
1126 sec_ch_above = 1;
1127 channel += 2;
1128 } else {
1129 sec_ch_above = 0;
1130 channel -= 2;
1131 }
1132
1133 opmode &= ~BW_OPMODE_20MHZ;
1134 rtl8xxxu_write8(priv, REG_BW_OPMODE, opmode);
1135 rsr &= ~RSR_RSC_BANDWIDTH_40M;
1136 if (sec_ch_above)
1137 rsr |= RSR_RSC_UPPER_SUB_CHANNEL;
1138 else
1139 rsr |= RSR_RSC_LOWER_SUB_CHANNEL;
1140 rtl8xxxu_write32(priv, REG_RESPONSE_RATE_SET, rsr);
1141
1142 val32 = rtl8xxxu_read32(priv, REG_FPGA0_RF_MODE);
1143 val32 |= FPGA_RF_MODE;
1144 rtl8xxxu_write32(priv, REG_FPGA0_RF_MODE, val32);
1145
1146 val32 = rtl8xxxu_read32(priv, REG_FPGA1_RF_MODE);
1147 val32 |= FPGA_RF_MODE;
1148 rtl8xxxu_write32(priv, REG_FPGA1_RF_MODE, val32);
1149
1150 /*
1151 * Set Control channel to upper or lower. These settings
1152 * are required only for 40MHz
1153 */
1154 val32 = rtl8xxxu_read32(priv, REG_CCK0_SYSTEM);
1155 val32 &= ~CCK0_SIDEBAND;
1156 if (!sec_ch_above)
1157 val32 |= CCK0_SIDEBAND;
1158 rtl8xxxu_write32(priv, REG_CCK0_SYSTEM, val32);
1159
1160 val32 = rtl8xxxu_read32(priv, REG_OFDM1_LSTF);
1161 val32 &= ~OFDM_LSTF_PRIME_CH_MASK; /* 0xc00 */
1162 if (sec_ch_above)
1163 val32 |= OFDM_LSTF_PRIME_CH_LOW;
1164 else
1165 val32 |= OFDM_LSTF_PRIME_CH_HIGH;
1166 rtl8xxxu_write32(priv, REG_OFDM1_LSTF, val32);
1167
1168 val32 = rtl8xxxu_read32(priv, REG_FPGA0_ANALOG2);
1169 val32 &= ~FPGA0_ANALOG2_20MHZ;
1170 rtl8xxxu_write32(priv, REG_FPGA0_ANALOG2, val32);
1171
1172 val32 = rtl8xxxu_read32(priv, REG_FPGA0_POWER_SAVE);
1173 val32 &= ~(FPGA0_PS_LOWER_CHANNEL | FPGA0_PS_UPPER_CHANNEL);
1174 if (sec_ch_above)
1175 val32 |= FPGA0_PS_UPPER_CHANNEL;
1176 else
1177 val32 |= FPGA0_PS_LOWER_CHANNEL;
1178 rtl8xxxu_write32(priv, REG_FPGA0_POWER_SAVE, val32);
1179 break;
1180
1181 default:
1182 break;
1183 }
1184
1185 for (i = RF_A; i < priv->rf_paths; i++) {
1186 val32 = rtl8xxxu_read_rfreg(priv, i, RF6052_REG_MODE_AG);
1187 val32 &= ~MODE_AG_CHANNEL_MASK;
1188 val32 |= channel;
1189 rtl8xxxu_write_rfreg(priv, i, RF6052_REG_MODE_AG, val32);
1190 }
1191
1192 if (ht)
1193 val8 = 0x0e;
1194 else
1195 val8 = 0x0a;
1196
1197 rtl8xxxu_write8(priv, REG_SIFS_CCK + 1, val8);
1198 rtl8xxxu_write8(priv, REG_SIFS_OFDM + 1, val8);
1199
1200 rtl8xxxu_write16(priv, REG_R2T_SIFS, 0x0808);
1201 rtl8xxxu_write16(priv, REG_T2T_SIFS, 0x0a0a);
1202
1203 for (i = RF_A; i < priv->rf_paths; i++) {
1204 val32 = rtl8xxxu_read_rfreg(priv, i, RF6052_REG_MODE_AG);
1205 if (hw->conf.chandef.width == NL80211_CHAN_WIDTH_40)
1206 val32 &= ~MODE_AG_CHANNEL_20MHZ;
1207 else
1208 val32 |= MODE_AG_CHANNEL_20MHZ;
1209 rtl8xxxu_write_rfreg(priv, i, RF6052_REG_MODE_AG, val32);
1210 }
1211}
1212
Jes Sorensen599119f2016-04-28 15:19:06 -04001213void rtl8xxxu_gen2_config_channel(struct ieee80211_hw *hw)
Jes Sorensenc3f95062016-02-29 17:04:40 -05001214{
1215 struct rtl8xxxu_priv *priv = hw->priv;
1216 u32 val32, rsr;
Jes Sorensen368633c2016-02-29 17:04:42 -05001217 u8 val8, subchannel;
Jes Sorensenc3f95062016-02-29 17:04:40 -05001218 u16 rf_mode_bw;
1219 bool ht = true;
1220 int sec_ch_above, channel;
1221 int i;
1222
1223 rf_mode_bw = rtl8xxxu_read16(priv, REG_WMAC_TRXPTCL_CTL);
1224 rf_mode_bw &= ~WMAC_TRXPTCL_CTL_BW_MASK;
1225 rsr = rtl8xxxu_read32(priv, REG_RESPONSE_RATE_SET);
1226 channel = hw->conf.chandef.chan->hw_value;
1227
1228/* Hack */
1229 subchannel = 0;
1230
1231 switch (hw->conf.chandef.width) {
1232 case NL80211_CHAN_WIDTH_20_NOHT:
1233 ht = false;
1234 case NL80211_CHAN_WIDTH_20:
1235 rf_mode_bw |= WMAC_TRXPTCL_CTL_BW_20;
1236 subchannel = 0;
1237
1238 val32 = rtl8xxxu_read32(priv, REG_FPGA0_RF_MODE);
1239 val32 &= ~FPGA_RF_MODE;
1240 rtl8xxxu_write32(priv, REG_FPGA0_RF_MODE, val32);
1241
1242 val32 = rtl8xxxu_read32(priv, REG_FPGA1_RF_MODE);
1243 val32 &= ~FPGA_RF_MODE;
1244 rtl8xxxu_write32(priv, REG_FPGA1_RF_MODE, val32);
1245
1246 val32 = rtl8xxxu_read32(priv, REG_OFDM0_TX_PSDO_NOISE_WEIGHT);
1247 val32 &= ~(BIT(30) | BIT(31));
1248 rtl8xxxu_write32(priv, REG_OFDM0_TX_PSDO_NOISE_WEIGHT, val32);
1249
1250 break;
1251 case NL80211_CHAN_WIDTH_40:
1252 rf_mode_bw |= WMAC_TRXPTCL_CTL_BW_40;
1253
1254 if (hw->conf.chandef.center_freq1 >
1255 hw->conf.chandef.chan->center_freq) {
1256 sec_ch_above = 1;
1257 channel += 2;
1258 } else {
1259 sec_ch_above = 0;
1260 channel -= 2;
1261 }
1262
1263 val32 = rtl8xxxu_read32(priv, REG_FPGA0_RF_MODE);
1264 val32 |= FPGA_RF_MODE;
1265 rtl8xxxu_write32(priv, REG_FPGA0_RF_MODE, val32);
1266
1267 val32 = rtl8xxxu_read32(priv, REG_FPGA1_RF_MODE);
1268 val32 |= FPGA_RF_MODE;
1269 rtl8xxxu_write32(priv, REG_FPGA1_RF_MODE, val32);
1270
1271 /*
1272 * Set Control channel to upper or lower. These settings
1273 * are required only for 40MHz
1274 */
1275 val32 = rtl8xxxu_read32(priv, REG_CCK0_SYSTEM);
1276 val32 &= ~CCK0_SIDEBAND;
1277 if (!sec_ch_above)
1278 val32 |= CCK0_SIDEBAND;
1279 rtl8xxxu_write32(priv, REG_CCK0_SYSTEM, val32);
1280
1281 val32 = rtl8xxxu_read32(priv, REG_OFDM1_LSTF);
1282 val32 &= ~OFDM_LSTF_PRIME_CH_MASK; /* 0xc00 */
1283 if (sec_ch_above)
1284 val32 |= OFDM_LSTF_PRIME_CH_LOW;
1285 else
1286 val32 |= OFDM_LSTF_PRIME_CH_HIGH;
1287 rtl8xxxu_write32(priv, REG_OFDM1_LSTF, val32);
1288
1289 val32 = rtl8xxxu_read32(priv, REG_FPGA0_POWER_SAVE);
1290 val32 &= ~(FPGA0_PS_LOWER_CHANNEL | FPGA0_PS_UPPER_CHANNEL);
1291 if (sec_ch_above)
1292 val32 |= FPGA0_PS_UPPER_CHANNEL;
1293 else
1294 val32 |= FPGA0_PS_LOWER_CHANNEL;
1295 rtl8xxxu_write32(priv, REG_FPGA0_POWER_SAVE, val32);
1296 break;
1297 case NL80211_CHAN_WIDTH_80:
1298 rf_mode_bw |= WMAC_TRXPTCL_CTL_BW_80;
1299 break;
1300 default:
1301 break;
1302 }
1303
1304 for (i = RF_A; i < priv->rf_paths; i++) {
1305 val32 = rtl8xxxu_read_rfreg(priv, i, RF6052_REG_MODE_AG);
1306 val32 &= ~MODE_AG_CHANNEL_MASK;
1307 val32 |= channel;
1308 rtl8xxxu_write_rfreg(priv, i, RF6052_REG_MODE_AG, val32);
1309 }
1310
1311 rtl8xxxu_write16(priv, REG_WMAC_TRXPTCL_CTL, rf_mode_bw);
1312 rtl8xxxu_write8(priv, REG_DATA_SUBCHANNEL, subchannel);
1313
1314 if (ht)
1315 val8 = 0x0e;
1316 else
1317 val8 = 0x0a;
1318
1319 rtl8xxxu_write8(priv, REG_SIFS_CCK + 1, val8);
1320 rtl8xxxu_write8(priv, REG_SIFS_OFDM + 1, val8);
1321
1322 rtl8xxxu_write16(priv, REG_R2T_SIFS, 0x0808);
1323 rtl8xxxu_write16(priv, REG_T2T_SIFS, 0x0a0a);
1324
1325 for (i = RF_A; i < priv->rf_paths; i++) {
1326 val32 = rtl8xxxu_read_rfreg(priv, i, RF6052_REG_MODE_AG);
1327 val32 &= ~MODE_AG_BW_MASK;
1328 switch(hw->conf.chandef.width) {
1329 case NL80211_CHAN_WIDTH_80:
1330 val32 |= MODE_AG_BW_80MHZ_8723B;
1331 break;
1332 case NL80211_CHAN_WIDTH_40:
1333 val32 |= MODE_AG_BW_40MHZ_8723B;
1334 break;
1335 default:
1336 val32 |= MODE_AG_BW_20MHZ_8723B;
1337 break;
1338 }
1339 rtl8xxxu_write_rfreg(priv, i, RF6052_REG_MODE_AG, val32);
1340 }
1341}
1342
Jes Sorensen20e3b2e2016-04-28 15:19:08 -04001343void
Jes Sorensen42a3bc72016-04-18 11:49:31 -04001344rtl8xxxu_gen1_set_tx_power(struct rtl8xxxu_priv *priv, int channel, bool ht40)
Jes Sorensen26f1fad2015-10-14 20:44:51 -04001345{
Jes Sorensen2fc0b8e2016-04-14 16:37:16 -04001346 struct rtl8xxxu_power_base *power_base = priv->power_base;
Jes Sorensen26f1fad2015-10-14 20:44:51 -04001347 u8 cck[RTL8723A_MAX_RF_PATHS], ofdm[RTL8723A_MAX_RF_PATHS];
1348 u8 ofdmbase[RTL8723A_MAX_RF_PATHS], mcsbase[RTL8723A_MAX_RF_PATHS];
1349 u32 val32, ofdm_a, ofdm_b, mcs_a, mcs_b;
1350 u8 val8;
1351 int group, i;
1352
1353 group = rtl8723a_channel_to_group(channel);
1354
Jes Sorensen2fc0b8e2016-04-14 16:37:16 -04001355 cck[0] = priv->cck_tx_power_index_A[group] - 1;
1356 cck[1] = priv->cck_tx_power_index_B[group] - 1;
Jes Sorensen26f1fad2015-10-14 20:44:51 -04001357
Jes Sorensenb591e982016-04-14 16:37:09 -04001358 if (priv->hi_pa) {
1359 if (cck[0] > 0x20)
1360 cck[0] = 0x20;
1361 if (cck[1] > 0x20)
1362 cck[1] = 0x20;
1363 }
1364
Jes Sorensen26f1fad2015-10-14 20:44:51 -04001365 ofdm[0] = priv->ht40_1s_tx_power_index_A[group];
1366 ofdm[1] = priv->ht40_1s_tx_power_index_B[group];
Jes Sorensen2fc0b8e2016-04-14 16:37:16 -04001367 if (ofdm[0])
1368 ofdm[0] -= 1;
1369 if (ofdm[1])
1370 ofdm[1] -= 1;
Jes Sorensen26f1fad2015-10-14 20:44:51 -04001371
1372 ofdmbase[0] = ofdm[0] + priv->ofdm_tx_power_index_diff[group].a;
1373 ofdmbase[1] = ofdm[1] + priv->ofdm_tx_power_index_diff[group].b;
1374
1375 mcsbase[0] = ofdm[0];
1376 mcsbase[1] = ofdm[1];
1377 if (!ht40) {
1378 mcsbase[0] += priv->ht20_tx_power_index_diff[group].a;
1379 mcsbase[1] += priv->ht20_tx_power_index_diff[group].b;
1380 }
1381
1382 if (priv->tx_paths > 1) {
1383 if (ofdm[0] > priv->ht40_2s_tx_power_index_diff[group].a)
1384 ofdm[0] -= priv->ht40_2s_tx_power_index_diff[group].a;
1385 if (ofdm[1] > priv->ht40_2s_tx_power_index_diff[group].b)
1386 ofdm[1] -= priv->ht40_2s_tx_power_index_diff[group].b;
1387 }
1388
1389 if (rtl8xxxu_debug & RTL8XXXU_DEBUG_CHANNEL)
1390 dev_info(&priv->udev->dev,
1391 "%s: Setting TX power CCK A: %02x, "
1392 "CCK B: %02x, OFDM A: %02x, OFDM B: %02x\n",
1393 __func__, cck[0], cck[1], ofdm[0], ofdm[1]);
1394
1395 for (i = 0; i < RTL8723A_MAX_RF_PATHS; i++) {
1396 if (cck[i] > RF6052_MAX_TX_PWR)
1397 cck[i] = RF6052_MAX_TX_PWR;
1398 if (ofdm[i] > RF6052_MAX_TX_PWR)
1399 ofdm[i] = RF6052_MAX_TX_PWR;
1400 }
1401
1402 val32 = rtl8xxxu_read32(priv, REG_TX_AGC_A_CCK1_MCS32);
1403 val32 &= 0xffff00ff;
1404 val32 |= (cck[0] << 8);
1405 rtl8xxxu_write32(priv, REG_TX_AGC_A_CCK1_MCS32, val32);
1406
1407 val32 = rtl8xxxu_read32(priv, REG_TX_AGC_B_CCK11_A_CCK2_11);
1408 val32 &= 0xff;
1409 val32 |= ((cck[0] << 8) | (cck[0] << 16) | (cck[0] << 24));
1410 rtl8xxxu_write32(priv, REG_TX_AGC_B_CCK11_A_CCK2_11, val32);
1411
1412 val32 = rtl8xxxu_read32(priv, REG_TX_AGC_B_CCK11_A_CCK2_11);
1413 val32 &= 0xffffff00;
1414 val32 |= cck[1];
1415 rtl8xxxu_write32(priv, REG_TX_AGC_B_CCK11_A_CCK2_11, val32);
1416
1417 val32 = rtl8xxxu_read32(priv, REG_TX_AGC_B_CCK1_55_MCS32);
1418 val32 &= 0xff;
1419 val32 |= ((cck[1] << 8) | (cck[1] << 16) | (cck[1] << 24));
1420 rtl8xxxu_write32(priv, REG_TX_AGC_B_CCK1_55_MCS32, val32);
1421
1422 ofdm_a = ofdmbase[0] | ofdmbase[0] << 8 |
1423 ofdmbase[0] << 16 | ofdmbase[0] << 24;
1424 ofdm_b = ofdmbase[1] | ofdmbase[1] << 8 |
1425 ofdmbase[1] << 16 | ofdmbase[1] << 24;
Jes Sorensen26f1fad2015-10-14 20:44:51 -04001426
Jes Sorensen2fc0b8e2016-04-14 16:37:16 -04001427 rtl8xxxu_write32(priv, REG_TX_AGC_A_RATE18_06,
1428 ofdm_a + power_base->reg_0e00);
1429 rtl8xxxu_write32(priv, REG_TX_AGC_B_RATE18_06,
1430 ofdm_b + power_base->reg_0830);
1431
1432 rtl8xxxu_write32(priv, REG_TX_AGC_A_RATE54_24,
1433 ofdm_a + power_base->reg_0e04);
1434 rtl8xxxu_write32(priv, REG_TX_AGC_B_RATE54_24,
1435 ofdm_b + power_base->reg_0834);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04001436
1437 mcs_a = mcsbase[0] | mcsbase[0] << 8 |
1438 mcsbase[0] << 16 | mcsbase[0] << 24;
1439 mcs_b = mcsbase[1] | mcsbase[1] << 8 |
1440 mcsbase[1] << 16 | mcsbase[1] << 24;
1441
Jes Sorensen2fc0b8e2016-04-14 16:37:16 -04001442 rtl8xxxu_write32(priv, REG_TX_AGC_A_MCS03_MCS00,
1443 mcs_a + power_base->reg_0e10);
1444 rtl8xxxu_write32(priv, REG_TX_AGC_B_MCS03_MCS00,
1445 mcs_b + power_base->reg_083c);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04001446
Jes Sorensen2fc0b8e2016-04-14 16:37:16 -04001447 rtl8xxxu_write32(priv, REG_TX_AGC_A_MCS07_MCS04,
1448 mcs_a + power_base->reg_0e14);
1449 rtl8xxxu_write32(priv, REG_TX_AGC_B_MCS07_MCS04,
1450 mcs_b + power_base->reg_0848);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04001451
Jes Sorensen2fc0b8e2016-04-14 16:37:16 -04001452 rtl8xxxu_write32(priv, REG_TX_AGC_A_MCS11_MCS08,
1453 mcs_a + power_base->reg_0e18);
1454 rtl8xxxu_write32(priv, REG_TX_AGC_B_MCS11_MCS08,
1455 mcs_b + power_base->reg_084c);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04001456
Jes Sorensen2fc0b8e2016-04-14 16:37:16 -04001457 rtl8xxxu_write32(priv, REG_TX_AGC_A_MCS15_MCS12,
1458 mcs_a + power_base->reg_0e1c);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04001459 for (i = 0; i < 3; i++) {
1460 if (i != 2)
1461 val8 = (mcsbase[0] > 8) ? (mcsbase[0] - 8) : 0;
1462 else
1463 val8 = (mcsbase[0] > 6) ? (mcsbase[0] - 6) : 0;
1464 rtl8xxxu_write8(priv, REG_OFDM0_XC_TX_IQ_IMBALANCE + i, val8);
1465 }
Jes Sorensen2fc0b8e2016-04-14 16:37:16 -04001466 rtl8xxxu_write32(priv, REG_TX_AGC_B_MCS15_MCS12,
1467 mcs_b + power_base->reg_0868);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04001468 for (i = 0; i < 3; i++) {
1469 if (i != 2)
1470 val8 = (mcsbase[1] > 8) ? (mcsbase[1] - 8) : 0;
1471 else
1472 val8 = (mcsbase[1] > 6) ? (mcsbase[1] - 6) : 0;
1473 rtl8xxxu_write8(priv, REG_OFDM0_XD_TX_IQ_IMBALANCE + i, val8);
1474 }
1475}
1476
1477static void rtl8xxxu_set_linktype(struct rtl8xxxu_priv *priv,
1478 enum nl80211_iftype linktype)
1479{
Jes Sorensena26703f2016-02-03 13:39:56 -05001480 u8 val8;
Jes Sorensen26f1fad2015-10-14 20:44:51 -04001481
Jes Sorensena26703f2016-02-03 13:39:56 -05001482 val8 = rtl8xxxu_read8(priv, REG_MSR);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04001483 val8 &= ~MSR_LINKTYPE_MASK;
1484
1485 switch (linktype) {
1486 case NL80211_IFTYPE_UNSPECIFIED:
1487 val8 |= MSR_LINKTYPE_NONE;
1488 break;
1489 case NL80211_IFTYPE_ADHOC:
1490 val8 |= MSR_LINKTYPE_ADHOC;
1491 break;
1492 case NL80211_IFTYPE_STATION:
1493 val8 |= MSR_LINKTYPE_STATION;
1494 break;
1495 case NL80211_IFTYPE_AP:
1496 val8 |= MSR_LINKTYPE_AP;
1497 break;
1498 default:
1499 goto out;
1500 }
1501
1502 rtl8xxxu_write8(priv, REG_MSR, val8);
1503out:
1504 return;
1505}
1506
1507static void
1508rtl8xxxu_set_retry(struct rtl8xxxu_priv *priv, u16 short_retry, u16 long_retry)
1509{
1510 u16 val16;
1511
1512 val16 = ((short_retry << RETRY_LIMIT_SHORT_SHIFT) &
1513 RETRY_LIMIT_SHORT_MASK) |
1514 ((long_retry << RETRY_LIMIT_LONG_SHIFT) &
1515 RETRY_LIMIT_LONG_MASK);
1516
1517 rtl8xxxu_write16(priv, REG_RETRY_LIMIT, val16);
1518}
1519
1520static void
1521rtl8xxxu_set_spec_sifs(struct rtl8xxxu_priv *priv, u16 cck, u16 ofdm)
1522{
1523 u16 val16;
1524
1525 val16 = ((cck << SPEC_SIFS_CCK_SHIFT) & SPEC_SIFS_CCK_MASK) |
1526 ((ofdm << SPEC_SIFS_OFDM_SHIFT) & SPEC_SIFS_OFDM_MASK);
1527
1528 rtl8xxxu_write16(priv, REG_SPEC_SIFS, val16);
1529}
1530
1531static void rtl8xxxu_print_chipinfo(struct rtl8xxxu_priv *priv)
1532{
1533 struct device *dev = &priv->udev->dev;
1534 char *cut;
1535
1536 switch (priv->chip_cut) {
1537 case 0:
1538 cut = "A";
1539 break;
1540 case 1:
1541 cut = "B";
1542 break;
Jes Sorensen0e5d4352016-02-29 17:04:00 -05001543 case 2:
1544 cut = "C";
1545 break;
1546 case 3:
1547 cut = "D";
1548 break;
1549 case 4:
1550 cut = "E";
1551 break;
Jes Sorensen26f1fad2015-10-14 20:44:51 -04001552 default:
1553 cut = "unknown";
1554 }
1555
1556 dev_info(dev,
1557 "RTL%s rev %s (%s) %iT%iR, TX queues %i, WiFi=%i, BT=%i, GPS=%i, HI PA=%i\n",
Jes Sorensen0e5d4352016-02-29 17:04:00 -05001558 priv->chip_name, cut, priv->chip_vendor, priv->tx_paths,
1559 priv->rx_paths, priv->ep_tx_count, priv->has_wifi,
1560 priv->has_bluetooth, priv->has_gps, priv->hi_pa);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04001561
1562 dev_info(dev, "RTL%s MAC: %pM\n", priv->chip_name, priv->mac_addr);
1563}
1564
1565static int rtl8xxxu_identify_chip(struct rtl8xxxu_priv *priv)
1566{
1567 struct device *dev = &priv->udev->dev;
1568 u32 val32, bonding;
1569 u16 val16;
1570
1571 val32 = rtl8xxxu_read32(priv, REG_SYS_CFG);
1572 priv->chip_cut = (val32 & SYS_CFG_CHIP_VERSION_MASK) >>
1573 SYS_CFG_CHIP_VERSION_SHIFT;
1574 if (val32 & SYS_CFG_TRP_VAUX_EN) {
1575 dev_info(dev, "Unsupported test chip\n");
1576 return -ENOTSUPP;
1577 }
1578
1579 if (val32 & SYS_CFG_BT_FUNC) {
Jes Sorensen35a741f2016-02-29 17:04:10 -05001580 if (priv->chip_cut >= 3) {
1581 sprintf(priv->chip_name, "8723BU");
Jes Sorensenba17d822016-03-31 17:08:39 -04001582 priv->rtl_chip = RTL8723B;
Jes Sorensen35a741f2016-02-29 17:04:10 -05001583 } else {
1584 sprintf(priv->chip_name, "8723AU");
Jes Sorensen0e28b972016-02-29 17:04:13 -05001585 priv->usb_interrupts = 1;
Jes Sorensenba17d822016-03-31 17:08:39 -04001586 priv->rtl_chip = RTL8723A;
Jes Sorensen35a741f2016-02-29 17:04:10 -05001587 }
1588
Jes Sorensen26f1fad2015-10-14 20:44:51 -04001589 priv->rf_paths = 1;
1590 priv->rx_paths = 1;
1591 priv->tx_paths = 1;
Jes Sorensen26f1fad2015-10-14 20:44:51 -04001592
1593 val32 = rtl8xxxu_read32(priv, REG_MULTI_FUNC_CTRL);
1594 if (val32 & MULTI_WIFI_FUNC_EN)
1595 priv->has_wifi = 1;
1596 if (val32 & MULTI_BT_FUNC_EN)
1597 priv->has_bluetooth = 1;
1598 if (val32 & MULTI_GPS_FUNC_EN)
1599 priv->has_gps = 1;
Jakub Sitnicki38451992016-02-03 13:39:49 -05001600 priv->is_multi_func = 1;
Jes Sorensen26f1fad2015-10-14 20:44:51 -04001601 } else if (val32 & SYS_CFG_TYPE_ID) {
1602 bonding = rtl8xxxu_read32(priv, REG_HPON_FSM);
1603 bonding &= HPON_FSM_BONDING_MASK;
Jes Sorensen55c0b6a2016-04-14 14:58:50 -04001604 if (priv->fops->tx_desc_size ==
1605 sizeof(struct rtl8xxxu_txdesc40)) {
Jes Sorensen0e5d4352016-02-29 17:04:00 -05001606 if (bonding == HPON_FSM_BONDING_1T2R) {
1607 sprintf(priv->chip_name, "8191EU");
1608 priv->rf_paths = 2;
1609 priv->rx_paths = 2;
1610 priv->tx_paths = 1;
Jes Sorensenba17d822016-03-31 17:08:39 -04001611 priv->rtl_chip = RTL8191E;
Jes Sorensen0e5d4352016-02-29 17:04:00 -05001612 } else {
1613 sprintf(priv->chip_name, "8192EU");
1614 priv->rf_paths = 2;
1615 priv->rx_paths = 2;
1616 priv->tx_paths = 2;
Jes Sorensenba17d822016-03-31 17:08:39 -04001617 priv->rtl_chip = RTL8192E;
Jes Sorensen0e5d4352016-02-29 17:04:00 -05001618 }
1619 } else if (bonding == HPON_FSM_BONDING_1T2R) {
Jes Sorensen26f1fad2015-10-14 20:44:51 -04001620 sprintf(priv->chip_name, "8191CU");
1621 priv->rf_paths = 2;
1622 priv->rx_paths = 2;
1623 priv->tx_paths = 1;
Jes Sorensen0e28b972016-02-29 17:04:13 -05001624 priv->usb_interrupts = 1;
Jes Sorensenba17d822016-03-31 17:08:39 -04001625 priv->rtl_chip = RTL8191C;
Jes Sorensen26f1fad2015-10-14 20:44:51 -04001626 } else {
1627 sprintf(priv->chip_name, "8192CU");
1628 priv->rf_paths = 2;
1629 priv->rx_paths = 2;
1630 priv->tx_paths = 2;
Jes Sorensen0e28b972016-02-29 17:04:13 -05001631 priv->usb_interrupts = 1;
Jes Sorensenba17d822016-03-31 17:08:39 -04001632 priv->rtl_chip = RTL8192C;
Jes Sorensen26f1fad2015-10-14 20:44:51 -04001633 }
1634 priv->has_wifi = 1;
1635 } else {
1636 sprintf(priv->chip_name, "8188CU");
1637 priv->rf_paths = 1;
1638 priv->rx_paths = 1;
1639 priv->tx_paths = 1;
Jes Sorensenba17d822016-03-31 17:08:39 -04001640 priv->rtl_chip = RTL8188C;
Jes Sorensen0e28b972016-02-29 17:04:13 -05001641 priv->usb_interrupts = 1;
Jes Sorensen26f1fad2015-10-14 20:44:51 -04001642 priv->has_wifi = 1;
1643 }
1644
Jes Sorensenba17d822016-03-31 17:08:39 -04001645 switch (priv->rtl_chip) {
1646 case RTL8188E:
1647 case RTL8192E:
1648 case RTL8723B:
Jes Sorensen0e5d4352016-02-29 17:04:00 -05001649 switch (val32 & SYS_CFG_VENDOR_EXT_MASK) {
1650 case SYS_CFG_VENDOR_ID_TSMC:
1651 sprintf(priv->chip_vendor, "TSMC");
1652 break;
1653 case SYS_CFG_VENDOR_ID_SMIC:
1654 sprintf(priv->chip_vendor, "SMIC");
1655 priv->vendor_smic = 1;
1656 break;
1657 case SYS_CFG_VENDOR_ID_UMC:
1658 sprintf(priv->chip_vendor, "UMC");
1659 priv->vendor_umc = 1;
1660 break;
1661 default:
1662 sprintf(priv->chip_vendor, "unknown");
1663 }
1664 break;
1665 default:
1666 if (val32 & SYS_CFG_VENDOR_ID) {
1667 sprintf(priv->chip_vendor, "UMC");
1668 priv->vendor_umc = 1;
1669 } else {
1670 sprintf(priv->chip_vendor, "TSMC");
1671 }
1672 }
Jes Sorensen26f1fad2015-10-14 20:44:51 -04001673
1674 val32 = rtl8xxxu_read32(priv, REG_GPIO_OUTSTS);
1675 priv->rom_rev = (val32 & GPIO_RF_RL_ID) >> 28;
1676
1677 val16 = rtl8xxxu_read16(priv, REG_NORMAL_SIE_EP_TX);
1678 if (val16 & NORMAL_SIE_EP_TX_HIGH_MASK) {
1679 priv->ep_tx_high_queue = 1;
1680 priv->ep_tx_count++;
1681 }
1682
1683 if (val16 & NORMAL_SIE_EP_TX_NORMAL_MASK) {
1684 priv->ep_tx_normal_queue = 1;
1685 priv->ep_tx_count++;
1686 }
1687
1688 if (val16 & NORMAL_SIE_EP_TX_LOW_MASK) {
1689 priv->ep_tx_low_queue = 1;
1690 priv->ep_tx_count++;
1691 }
1692
1693 /*
1694 * Fallback for devices that do not provide REG_NORMAL_SIE_EP_TX
1695 */
1696 if (!priv->ep_tx_count) {
1697 switch (priv->nr_out_eps) {
Jes Sorensen35a741f2016-02-29 17:04:10 -05001698 case 4:
Jes Sorensen26f1fad2015-10-14 20:44:51 -04001699 case 3:
1700 priv->ep_tx_low_queue = 1;
1701 priv->ep_tx_count++;
1702 case 2:
1703 priv->ep_tx_normal_queue = 1;
1704 priv->ep_tx_count++;
1705 case 1:
1706 priv->ep_tx_high_queue = 1;
1707 priv->ep_tx_count++;
1708 break;
1709 default:
1710 dev_info(dev, "Unsupported USB TX end-points\n");
1711 return -ENOTSUPP;
1712 }
1713 }
1714
1715 return 0;
1716}
1717
Jes Sorensen26f1fad2015-10-14 20:44:51 -04001718static int
1719rtl8xxxu_read_efuse8(struct rtl8xxxu_priv *priv, u16 offset, u8 *data)
1720{
1721 int i;
1722 u8 val8;
1723 u32 val32;
1724
1725 /* Write Address */
1726 rtl8xxxu_write8(priv, REG_EFUSE_CTRL + 1, offset & 0xff);
1727 val8 = rtl8xxxu_read8(priv, REG_EFUSE_CTRL + 2);
1728 val8 &= 0xfc;
1729 val8 |= (offset >> 8) & 0x03;
1730 rtl8xxxu_write8(priv, REG_EFUSE_CTRL + 2, val8);
1731
1732 val8 = rtl8xxxu_read8(priv, REG_EFUSE_CTRL + 3);
1733 rtl8xxxu_write8(priv, REG_EFUSE_CTRL + 3, val8 & 0x7f);
1734
1735 /* Poll for data read */
1736 val32 = rtl8xxxu_read32(priv, REG_EFUSE_CTRL);
1737 for (i = 0; i < RTL8XXXU_MAX_REG_POLL; i++) {
1738 val32 = rtl8xxxu_read32(priv, REG_EFUSE_CTRL);
1739 if (val32 & BIT(31))
1740 break;
1741 }
1742
1743 if (i == RTL8XXXU_MAX_REG_POLL)
1744 return -EIO;
1745
1746 udelay(50);
1747 val32 = rtl8xxxu_read32(priv, REG_EFUSE_CTRL);
1748
1749 *data = val32 & 0xff;
1750 return 0;
1751}
1752
1753static int rtl8xxxu_read_efuse(struct rtl8xxxu_priv *priv)
1754{
1755 struct device *dev = &priv->udev->dev;
1756 int i, ret = 0;
1757 u8 val8, word_mask, header, extheader;
1758 u16 val16, efuse_addr, offset;
1759 u32 val32;
1760
1761 val16 = rtl8xxxu_read16(priv, REG_9346CR);
1762 if (val16 & EEPROM_ENABLE)
1763 priv->has_eeprom = 1;
1764 if (val16 & EEPROM_BOOT)
1765 priv->boot_eeprom = 1;
1766
Jakub Sitnicki38451992016-02-03 13:39:49 -05001767 if (priv->is_multi_func) {
1768 val32 = rtl8xxxu_read32(priv, REG_EFUSE_TEST);
1769 val32 = (val32 & ~EFUSE_SELECT_MASK) | EFUSE_WIFI_SELECT;
1770 rtl8xxxu_write32(priv, REG_EFUSE_TEST, val32);
1771 }
Jes Sorensen26f1fad2015-10-14 20:44:51 -04001772
1773 dev_dbg(dev, "Booting from %s\n",
1774 priv->boot_eeprom ? "EEPROM" : "EFUSE");
1775
1776 rtl8xxxu_write8(priv, REG_EFUSE_ACCESS, EFUSE_ACCESS_ENABLE);
1777
1778 /* 1.2V Power: From VDDON with Power Cut(0x0000[15]), default valid */
1779 val16 = rtl8xxxu_read16(priv, REG_SYS_ISO_CTRL);
1780 if (!(val16 & SYS_ISO_PWC_EV12V)) {
1781 val16 |= SYS_ISO_PWC_EV12V;
1782 rtl8xxxu_write16(priv, REG_SYS_ISO_CTRL, val16);
1783 }
1784 /* Reset: 0x0000[28], default valid */
1785 val16 = rtl8xxxu_read16(priv, REG_SYS_FUNC);
1786 if (!(val16 & SYS_FUNC_ELDR)) {
1787 val16 |= SYS_FUNC_ELDR;
1788 rtl8xxxu_write16(priv, REG_SYS_FUNC, val16);
1789 }
1790
1791 /*
1792 * Clock: Gated(0x0008[5]) 8M(0x0008[1]) clock from ANA, default valid
1793 */
1794 val16 = rtl8xxxu_read16(priv, REG_SYS_CLKR);
1795 if (!(val16 & SYS_CLK_LOADER_ENABLE) || !(val16 & SYS_CLK_ANA8M)) {
1796 val16 |= (SYS_CLK_LOADER_ENABLE | SYS_CLK_ANA8M);
1797 rtl8xxxu_write16(priv, REG_SYS_CLKR, val16);
1798 }
1799
1800 /* Default value is 0xff */
Jes Sorensen3307d842016-02-29 17:03:59 -05001801 memset(priv->efuse_wifi.raw, 0xff, EFUSE_MAP_LEN);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04001802
1803 efuse_addr = 0;
1804 while (efuse_addr < EFUSE_REAL_CONTENT_LEN_8723A) {
Jakub Sitnickif6c47702016-02-29 17:04:23 -05001805 u16 map_addr;
1806
Jes Sorensen26f1fad2015-10-14 20:44:51 -04001807 ret = rtl8xxxu_read_efuse8(priv, efuse_addr++, &header);
1808 if (ret || header == 0xff)
1809 goto exit;
1810
1811 if ((header & 0x1f) == 0x0f) { /* extended header */
1812 offset = (header & 0xe0) >> 5;
1813
1814 ret = rtl8xxxu_read_efuse8(priv, efuse_addr++,
1815 &extheader);
1816 if (ret)
1817 goto exit;
1818 /* All words disabled */
1819 if ((extheader & 0x0f) == 0x0f)
1820 continue;
1821
1822 offset |= ((extheader & 0xf0) >> 1);
1823 word_mask = extheader & 0x0f;
1824 } else {
1825 offset = (header >> 4) & 0x0f;
1826 word_mask = header & 0x0f;
1827 }
1828
Jakub Sitnickif6c47702016-02-29 17:04:23 -05001829 /* Get word enable value from PG header */
Jes Sorensen26f1fad2015-10-14 20:44:51 -04001830
Jakub Sitnickif6c47702016-02-29 17:04:23 -05001831 /* We have 8 bits to indicate validity */
1832 map_addr = offset * 8;
1833 if (map_addr >= EFUSE_MAP_LEN) {
1834 dev_warn(dev, "%s: Illegal map_addr (%04x), "
1835 "efuse corrupt!\n",
1836 __func__, map_addr);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04001837 ret = -EINVAL;
1838 goto exit;
1839 }
Jakub Sitnickif6c47702016-02-29 17:04:23 -05001840 for (i = 0; i < EFUSE_MAX_WORD_UNIT; i++) {
1841 /* Check word enable condition in the section */
Jakub Sitnicki32a39dd2016-02-29 17:04:24 -05001842 if (word_mask & BIT(i)) {
Jakub Sitnickif6c47702016-02-29 17:04:23 -05001843 map_addr += 2;
Jakub Sitnicki32a39dd2016-02-29 17:04:24 -05001844 continue;
1845 }
1846
1847 ret = rtl8xxxu_read_efuse8(priv, efuse_addr++, &val8);
1848 if (ret)
1849 goto exit;
1850 priv->efuse_wifi.raw[map_addr++] = val8;
1851
1852 ret = rtl8xxxu_read_efuse8(priv, efuse_addr++, &val8);
1853 if (ret)
1854 goto exit;
1855 priv->efuse_wifi.raw[map_addr++] = val8;
Jakub Sitnickif6c47702016-02-29 17:04:23 -05001856 }
Jes Sorensen26f1fad2015-10-14 20:44:51 -04001857 }
1858
1859exit:
1860 rtl8xxxu_write8(priv, REG_EFUSE_ACCESS, EFUSE_ACCESS_DISABLE);
1861
1862 return ret;
1863}
1864
Jes Sorensen599119f2016-04-28 15:19:06 -04001865void rtl8xxxu_reset_8051(struct rtl8xxxu_priv *priv)
Jes Sorensend48fe602016-02-03 13:39:44 -05001866{
1867 u8 val8;
1868 u16 sys_func;
1869
1870 val8 = rtl8xxxu_read8(priv, REG_RSV_CTRL + 1);
Jes Sorensen53b381c2016-02-03 13:39:57 -05001871 val8 &= ~BIT(0);
Jes Sorensend48fe602016-02-03 13:39:44 -05001872 rtl8xxxu_write8(priv, REG_RSV_CTRL + 1, val8);
Jes Sorensen7d4ccb82016-02-29 17:05:50 -05001873
Jes Sorensend48fe602016-02-03 13:39:44 -05001874 sys_func = rtl8xxxu_read16(priv, REG_SYS_FUNC);
1875 sys_func &= ~SYS_FUNC_CPU_ENABLE;
1876 rtl8xxxu_write16(priv, REG_SYS_FUNC, sys_func);
Jes Sorensen7d4ccb82016-02-29 17:05:50 -05001877
Jes Sorensend48fe602016-02-03 13:39:44 -05001878 val8 = rtl8xxxu_read8(priv, REG_RSV_CTRL + 1);
Jes Sorensen53b381c2016-02-03 13:39:57 -05001879 val8 |= BIT(0);
Jes Sorensend48fe602016-02-03 13:39:44 -05001880 rtl8xxxu_write8(priv, REG_RSV_CTRL + 1, val8);
Jes Sorensen7d4ccb82016-02-29 17:05:50 -05001881
1882 sys_func |= SYS_FUNC_CPU_ENABLE;
1883 rtl8xxxu_write16(priv, REG_SYS_FUNC, sys_func);
1884}
1885
Jes Sorensen26f1fad2015-10-14 20:44:51 -04001886static int rtl8xxxu_start_firmware(struct rtl8xxxu_priv *priv)
1887{
1888 struct device *dev = &priv->udev->dev;
1889 int ret = 0, i;
1890 u32 val32;
1891
1892 /* Poll checksum report */
1893 for (i = 0; i < RTL8XXXU_FIRMWARE_POLL_MAX; i++) {
1894 val32 = rtl8xxxu_read32(priv, REG_MCU_FW_DL);
1895 if (val32 & MCU_FW_DL_CSUM_REPORT)
1896 break;
1897 }
1898
1899 if (i == RTL8XXXU_FIRMWARE_POLL_MAX) {
1900 dev_warn(dev, "Firmware checksum poll timed out\n");
1901 ret = -EAGAIN;
1902 goto exit;
1903 }
1904
1905 val32 = rtl8xxxu_read32(priv, REG_MCU_FW_DL);
1906 val32 |= MCU_FW_DL_READY;
1907 val32 &= ~MCU_WINT_INIT_READY;
1908 rtl8xxxu_write32(priv, REG_MCU_FW_DL, val32);
1909
Jes Sorensend48fe602016-02-03 13:39:44 -05001910 /*
1911 * Reset the 8051 in order for the firmware to start running,
1912 * otherwise it won't come up on the 8192eu
1913 */
Jes Sorensen7d4ccb82016-02-29 17:05:50 -05001914 priv->fops->reset_8051(priv);
Jes Sorensend48fe602016-02-03 13:39:44 -05001915
Jes Sorensen26f1fad2015-10-14 20:44:51 -04001916 /* Wait for firmware to become ready */
1917 for (i = 0; i < RTL8XXXU_FIRMWARE_POLL_MAX; i++) {
1918 val32 = rtl8xxxu_read32(priv, REG_MCU_FW_DL);
1919 if (val32 & MCU_WINT_INIT_READY)
1920 break;
1921
1922 udelay(100);
1923 }
1924
1925 if (i == RTL8XXXU_FIRMWARE_POLL_MAX) {
1926 dev_warn(dev, "Firmware failed to start\n");
1927 ret = -EAGAIN;
1928 goto exit;
1929 }
1930
Jes Sorensen3a4be6a2016-02-29 17:04:58 -05001931 /*
1932 * Init H2C command
1933 */
Jes Sorensenba17d822016-03-31 17:08:39 -04001934 if (priv->rtl_chip == RTL8723B)
Jes Sorensen3a4be6a2016-02-29 17:04:58 -05001935 rtl8xxxu_write8(priv, REG_HMTFR, 0x0f);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04001936exit:
1937 return ret;
1938}
1939
1940static int rtl8xxxu_download_firmware(struct rtl8xxxu_priv *priv)
1941{
1942 int pages, remainder, i, ret;
Jes Sorensend48fe602016-02-03 13:39:44 -05001943 u8 val8;
Jes Sorensen26f1fad2015-10-14 20:44:51 -04001944 u16 val16;
1945 u32 val32;
1946 u8 *fwptr;
1947
1948 val8 = rtl8xxxu_read8(priv, REG_SYS_FUNC + 1);
1949 val8 |= 4;
1950 rtl8xxxu_write8(priv, REG_SYS_FUNC + 1, val8);
1951
1952 /* 8051 enable */
1953 val16 = rtl8xxxu_read16(priv, REG_SYS_FUNC);
Jes Sorensen43154f62016-02-03 13:39:35 -05001954 val16 |= SYS_FUNC_CPU_ENABLE;
1955 rtl8xxxu_write16(priv, REG_SYS_FUNC, val16);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04001956
Jes Sorensen216202a2016-02-03 13:39:37 -05001957 val8 = rtl8xxxu_read8(priv, REG_MCU_FW_DL);
1958 if (val8 & MCU_FW_RAM_SEL) {
1959 pr_info("do the RAM reset\n");
1960 rtl8xxxu_write8(priv, REG_MCU_FW_DL, 0x00);
Jes Sorensen7d4ccb82016-02-29 17:05:50 -05001961 priv->fops->reset_8051(priv);
Jes Sorensen216202a2016-02-03 13:39:37 -05001962 }
1963
Jes Sorensen26f1fad2015-10-14 20:44:51 -04001964 /* MCU firmware download enable */
1965 val8 = rtl8xxxu_read8(priv, REG_MCU_FW_DL);
Jes Sorensenef1c0492016-02-03 13:39:36 -05001966 val8 |= MCU_FW_DL_ENABLE;
1967 rtl8xxxu_write8(priv, REG_MCU_FW_DL, val8);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04001968
1969 /* 8051 reset */
1970 val32 = rtl8xxxu_read32(priv, REG_MCU_FW_DL);
Jes Sorensenef1c0492016-02-03 13:39:36 -05001971 val32 &= ~BIT(19);
1972 rtl8xxxu_write32(priv, REG_MCU_FW_DL, val32);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04001973
1974 /* Reset firmware download checksum */
1975 val8 = rtl8xxxu_read8(priv, REG_MCU_FW_DL);
Jes Sorensenef1c0492016-02-03 13:39:36 -05001976 val8 |= MCU_FW_DL_CSUM_REPORT;
1977 rtl8xxxu_write8(priv, REG_MCU_FW_DL, val8);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04001978
1979 pages = priv->fw_size / RTL_FW_PAGE_SIZE;
1980 remainder = priv->fw_size % RTL_FW_PAGE_SIZE;
1981
1982 fwptr = priv->fw_data->data;
1983
1984 for (i = 0; i < pages; i++) {
1985 val8 = rtl8xxxu_read8(priv, REG_MCU_FW_DL + 2) & 0xF8;
Jes Sorensenef1c0492016-02-03 13:39:36 -05001986 val8 |= i;
1987 rtl8xxxu_write8(priv, REG_MCU_FW_DL + 2, val8);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04001988
1989 ret = rtl8xxxu_writeN(priv, REG_FW_START_ADDRESS,
1990 fwptr, RTL_FW_PAGE_SIZE);
1991 if (ret != RTL_FW_PAGE_SIZE) {
1992 ret = -EAGAIN;
1993 goto fw_abort;
1994 }
1995
1996 fwptr += RTL_FW_PAGE_SIZE;
1997 }
1998
1999 if (remainder) {
2000 val8 = rtl8xxxu_read8(priv, REG_MCU_FW_DL + 2) & 0xF8;
Jes Sorensenef1c0492016-02-03 13:39:36 -05002001 val8 |= i;
2002 rtl8xxxu_write8(priv, REG_MCU_FW_DL + 2, val8);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04002003 ret = rtl8xxxu_writeN(priv, REG_FW_START_ADDRESS,
2004 fwptr, remainder);
2005 if (ret != remainder) {
2006 ret = -EAGAIN;
2007 goto fw_abort;
2008 }
2009 }
2010
2011 ret = 0;
2012fw_abort:
2013 /* MCU firmware download disable */
2014 val16 = rtl8xxxu_read16(priv, REG_MCU_FW_DL);
Jes Sorensenef1c0492016-02-03 13:39:36 -05002015 val16 &= ~MCU_FW_DL_ENABLE;
2016 rtl8xxxu_write16(priv, REG_MCU_FW_DL, val16);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04002017
2018 return ret;
2019}
2020
Jes Sorensen599119f2016-04-28 15:19:06 -04002021int rtl8xxxu_load_firmware(struct rtl8xxxu_priv *priv, char *fw_name)
Jes Sorensen26f1fad2015-10-14 20:44:51 -04002022{
2023 struct device *dev = &priv->udev->dev;
2024 const struct firmware *fw;
2025 int ret = 0;
2026 u16 signature;
2027
2028 dev_info(dev, "%s: Loading firmware %s\n", DRIVER_NAME, fw_name);
2029 if (request_firmware(&fw, fw_name, &priv->udev->dev)) {
2030 dev_warn(dev, "request_firmware(%s) failed\n", fw_name);
2031 ret = -EAGAIN;
2032 goto exit;
2033 }
2034 if (!fw) {
2035 dev_warn(dev, "Firmware data not available\n");
2036 ret = -EINVAL;
2037 goto exit;
2038 }
2039
2040 priv->fw_data = kmemdup(fw->data, fw->size, GFP_KERNEL);
Tobias Klauser98e27cb2016-02-03 13:39:43 -05002041 if (!priv->fw_data) {
2042 ret = -ENOMEM;
2043 goto exit;
2044 }
Jes Sorensen26f1fad2015-10-14 20:44:51 -04002045 priv->fw_size = fw->size - sizeof(struct rtl8xxxu_firmware_header);
2046
2047 signature = le16_to_cpu(priv->fw_data->signature);
2048 switch (signature & 0xfff0) {
Jes Sorensen0e5d4352016-02-29 17:04:00 -05002049 case 0x92e0:
Jes Sorensen26f1fad2015-10-14 20:44:51 -04002050 case 0x92c0:
2051 case 0x88c0:
Jes Sorensen35a741f2016-02-29 17:04:10 -05002052 case 0x5300:
Jes Sorensen26f1fad2015-10-14 20:44:51 -04002053 case 0x2300:
2054 break;
2055 default:
2056 ret = -EINVAL;
2057 dev_warn(dev, "%s: Invalid firmware signature: 0x%04x\n",
2058 __func__, signature);
2059 }
2060
2061 dev_info(dev, "Firmware revision %i.%i (signature 0x%04x)\n",
2062 le16_to_cpu(priv->fw_data->major_version),
2063 priv->fw_data->minor_version, signature);
2064
2065exit:
2066 release_firmware(fw);
2067 return ret;
2068}
2069
Jes Sorensen6c46ca32016-04-28 15:19:07 -04002070void rtl8xxxu_firmware_self_reset(struct rtl8xxxu_priv *priv)
Jes Sorensen26f1fad2015-10-14 20:44:51 -04002071{
2072 u16 val16;
2073 int i = 100;
2074
2075 /* Inform 8051 to perform reset */
2076 rtl8xxxu_write8(priv, REG_HMTFR + 3, 0x20);
2077
2078 for (i = 100; i > 0; i--) {
2079 val16 = rtl8xxxu_read16(priv, REG_SYS_FUNC);
2080
2081 if (!(val16 & SYS_FUNC_CPU_ENABLE)) {
2082 dev_dbg(&priv->udev->dev,
2083 "%s: Firmware self reset success!\n", __func__);
2084 break;
2085 }
2086 udelay(50);
2087 }
2088
2089 if (!i) {
2090 /* Force firmware reset */
2091 val16 = rtl8xxxu_read16(priv, REG_SYS_FUNC);
2092 val16 &= ~SYS_FUNC_CPU_ENABLE;
2093 rtl8xxxu_write16(priv, REG_SYS_FUNC, val16);
2094 }
2095}
2096
2097static int
Jes Sorensenc606e662016-04-07 14:19:16 -04002098rtl8xxxu_init_mac(struct rtl8xxxu_priv *priv)
Jes Sorensen26f1fad2015-10-14 20:44:51 -04002099{
Jes Sorensenc606e662016-04-07 14:19:16 -04002100 struct rtl8xxxu_reg8val *array = priv->fops->mactable;
Jes Sorensen26f1fad2015-10-14 20:44:51 -04002101 int i, ret;
2102 u16 reg;
2103 u8 val;
2104
2105 for (i = 0; ; i++) {
2106 reg = array[i].reg;
2107 val = array[i].val;
2108
2109 if (reg == 0xffff && val == 0xff)
2110 break;
2111
2112 ret = rtl8xxxu_write8(priv, reg, val);
2113 if (ret != 1) {
2114 dev_warn(&priv->udev->dev,
Jes Sorensenc606e662016-04-07 14:19:16 -04002115 "Failed to initialize MAC "
2116 "(reg: %04x, val %02x)\n", reg, val);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04002117 return -EAGAIN;
2118 }
2119 }
2120
Jes Sorensen8a594852016-04-07 14:19:26 -04002121 if (priv->rtl_chip != RTL8723B && priv->rtl_chip != RTL8192E)
Jes Sorensen8baf6702016-02-29 17:04:54 -05002122 rtl8xxxu_write8(priv, REG_MAX_AGGR_NUM, 0x0a);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04002123
2124 return 0;
2125}
2126
Jes Sorensen599119f2016-04-28 15:19:06 -04002127int rtl8xxxu_init_phy_regs(struct rtl8xxxu_priv *priv,
2128 struct rtl8xxxu_reg32val *array)
Jes Sorensen26f1fad2015-10-14 20:44:51 -04002129{
2130 int i, ret;
2131 u16 reg;
2132 u32 val;
2133
2134 for (i = 0; ; i++) {
2135 reg = array[i].reg;
2136 val = array[i].val;
2137
2138 if (reg == 0xffff && val == 0xffffffff)
2139 break;
2140
2141 ret = rtl8xxxu_write32(priv, reg, val);
2142 if (ret != sizeof(val)) {
2143 dev_warn(&priv->udev->dev,
2144 "Failed to initialize PHY\n");
2145 return -EAGAIN;
2146 }
2147 udelay(1);
2148 }
2149
2150 return 0;
2151}
2152
Jes Sorensen20e3b2e2016-04-28 15:19:08 -04002153void rtl8xxxu_gen1_init_phy_bb(struct rtl8xxxu_priv *priv)
Jes Sorensen26f1fad2015-10-14 20:44:51 -04002154{
Jes Sorensenb84cac12016-04-14 14:59:00 -04002155 u8 val8, ldoa15, ldov12d, lpldo, ldohci12;
Jes Sorensen04313eb2016-02-29 17:04:51 -05002156 u16 val16;
Jes Sorensen26f1fad2015-10-14 20:44:51 -04002157 u32 val32;
2158
Jes Sorensencb877252016-04-14 14:58:57 -04002159 val8 = rtl8xxxu_read8(priv, REG_AFE_PLL_CTRL);
2160 udelay(2);
2161 val8 |= AFE_PLL_320_ENABLE;
2162 rtl8xxxu_write8(priv, REG_AFE_PLL_CTRL, val8);
2163 udelay(2);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04002164
Jes Sorensencb877252016-04-14 14:58:57 -04002165 rtl8xxxu_write8(priv, REG_AFE_PLL_CTRL + 1, 0xff);
2166 udelay(2);
Jes Sorensen8baf6702016-02-29 17:04:54 -05002167
Jes Sorensencb877252016-04-14 14:58:57 -04002168 val16 = rtl8xxxu_read16(priv, REG_SYS_FUNC);
2169 val16 |= SYS_FUNC_BB_GLB_RSTN | SYS_FUNC_BBRSTB;
2170 rtl8xxxu_write16(priv, REG_SYS_FUNC, val16);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04002171
Jes Sorensencb877252016-04-14 14:58:57 -04002172 val32 = rtl8xxxu_read32(priv, REG_AFE_XTAL_CTRL);
2173 val32 &= ~AFE_XTAL_RF_GATE;
2174 if (priv->has_bluetooth)
2175 val32 &= ~AFE_XTAL_BT_GATE;
2176 rtl8xxxu_write32(priv, REG_AFE_XTAL_CTRL, val32);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04002177
2178 /* 6. 0x1f[7:0] = 0x07 */
2179 val8 = RF_ENABLE | RF_RSTB | RF_SDMRSTB;
2180 rtl8xxxu_write8(priv, REG_RF_CTRL, val8);
2181
Jes Sorensencb877252016-04-14 14:58:57 -04002182 if (priv->hi_pa)
Jes Sorensenabd71bd2016-04-07 14:19:22 -04002183 rtl8xxxu_init_phy_regs(priv, rtl8188ru_phy_1t_highpa_table);
2184 else if (priv->tx_paths == 2)
2185 rtl8xxxu_init_phy_regs(priv, rtl8192cu_phy_2t_init_table);
2186 else
Jes Sorensen26f1fad2015-10-14 20:44:51 -04002187 rtl8xxxu_init_phy_regs(priv, rtl8723a_phy_1t_init_table);
2188
Jes Sorensen78a84212016-04-14 16:37:10 -04002189 if (priv->rtl_chip == RTL8188R && priv->hi_pa &&
Jes Sorensen26f1fad2015-10-14 20:44:51 -04002190 priv->vendor_umc && priv->chip_cut == 1)
2191 rtl8xxxu_write8(priv, REG_OFDM0_AGC_PARM1 + 2, 0x50);
Jes Sorensenc82f8d12016-04-14 14:58:59 -04002192
2193 if (priv->hi_pa)
2194 rtl8xxxu_init_phy_regs(priv, rtl8xxx_agc_highpa_table);
2195 else
2196 rtl8xxxu_init_phy_regs(priv, rtl8xxx_agc_standard_table);
Jes Sorensenb84cac12016-04-14 14:59:00 -04002197
2198 ldoa15 = LDOA15_ENABLE | LDOA15_OBUF;
2199 ldov12d = LDOV12D_ENABLE | BIT(2) | (2 << LDOV12D_VADJ_SHIFT);
2200 ldohci12 = 0x57;
2201 lpldo = 1;
2202 val32 = (lpldo << 24) | (ldohci12 << 16) | (ldov12d << 8) | ldoa15;
2203 rtl8xxxu_write32(priv, REG_LDOA15_CTRL, val32);
Jes Sorensencb877252016-04-14 14:58:57 -04002204}
2205
Jes Sorensencb877252016-04-14 14:58:57 -04002206/*
2207 * Most of this is black magic retrieved from the old rtl8723au driver
2208 */
2209static int rtl8xxxu_init_phy_bb(struct rtl8xxxu_priv *priv)
2210{
Jes Sorensenb84cac12016-04-14 14:59:00 -04002211 u8 val8;
Jes Sorensencb877252016-04-14 14:58:57 -04002212 u32 val32;
2213
2214 priv->fops->init_phy_bb(priv);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04002215
2216 if (priv->tx_paths == 1 && priv->rx_paths == 2) {
2217 /*
2218 * For 1T2R boards, patch the registers.
2219 *
2220 * It looks like 8191/2 1T2R boards use path B for TX
2221 */
2222 val32 = rtl8xxxu_read32(priv, REG_FPGA0_TX_INFO);
2223 val32 &= ~(BIT(0) | BIT(1));
2224 val32 |= BIT(1);
2225 rtl8xxxu_write32(priv, REG_FPGA0_TX_INFO, val32);
2226
2227 val32 = rtl8xxxu_read32(priv, REG_FPGA1_TX_INFO);
2228 val32 &= ~0x300033;
2229 val32 |= 0x200022;
2230 rtl8xxxu_write32(priv, REG_FPGA1_TX_INFO, val32);
2231
2232 val32 = rtl8xxxu_read32(priv, REG_CCK0_AFE_SETTING);
Jes Sorensenbd8fe402016-04-14 14:58:56 -04002233 val32 &= ~CCK0_AFE_RX_MASK;
Jes Sorensen90683082016-04-14 14:58:55 -04002234 val32 &= 0x00ffffff;
Jes Sorensenbd8fe402016-04-14 14:58:56 -04002235 val32 |= 0x40000000;
2236 val32 |= CCK0_AFE_RX_ANT_B;
Jes Sorensen26f1fad2015-10-14 20:44:51 -04002237 rtl8xxxu_write32(priv, REG_CCK0_AFE_SETTING, val32);
2238
2239 val32 = rtl8xxxu_read32(priv, REG_OFDM0_TRX_PATH_ENABLE);
2240 val32 &= ~(OFDM_RF_PATH_RX_MASK | OFDM_RF_PATH_TX_MASK);
2241 val32 |= (OFDM_RF_PATH_RX_A | OFDM_RF_PATH_RX_B |
2242 OFDM_RF_PATH_TX_B);
2243 rtl8xxxu_write32(priv, REG_OFDM0_TRX_PATH_ENABLE, val32);
2244
2245 val32 = rtl8xxxu_read32(priv, REG_OFDM0_AGC_PARM1);
2246 val32 &= ~(BIT(4) | BIT(5));
2247 val32 |= BIT(4);
2248 rtl8xxxu_write32(priv, REG_OFDM0_AGC_PARM1, val32);
2249
2250 val32 = rtl8xxxu_read32(priv, REG_TX_CCK_RFON);
2251 val32 &= ~(BIT(27) | BIT(26));
2252 val32 |= BIT(27);
2253 rtl8xxxu_write32(priv, REG_TX_CCK_RFON, val32);
2254
2255 val32 = rtl8xxxu_read32(priv, REG_TX_CCK_BBON);
2256 val32 &= ~(BIT(27) | BIT(26));
2257 val32 |= BIT(27);
2258 rtl8xxxu_write32(priv, REG_TX_CCK_BBON, val32);
2259
2260 val32 = rtl8xxxu_read32(priv, REG_TX_OFDM_RFON);
2261 val32 &= ~(BIT(27) | BIT(26));
2262 val32 |= BIT(27);
2263 rtl8xxxu_write32(priv, REG_TX_OFDM_RFON, val32);
2264
2265 val32 = rtl8xxxu_read32(priv, REG_TX_OFDM_BBON);
2266 val32 &= ~(BIT(27) | BIT(26));
2267 val32 |= BIT(27);
2268 rtl8xxxu_write32(priv, REG_TX_OFDM_BBON, val32);
2269
2270 val32 = rtl8xxxu_read32(priv, REG_TX_TO_TX);
2271 val32 &= ~(BIT(27) | BIT(26));
2272 val32 |= BIT(27);
2273 rtl8xxxu_write32(priv, REG_TX_TO_TX, val32);
2274 }
2275
Jes Sorensen4ef22eb2016-02-29 17:04:55 -05002276 if (priv->has_xtalk) {
Jes Sorensen26f1fad2015-10-14 20:44:51 -04002277 val32 = rtl8xxxu_read32(priv, REG_MAC_PHY_CTRL);
2278
Jes Sorensen4ef22eb2016-02-29 17:04:55 -05002279 val8 = priv->xtalk;
Jes Sorensen26f1fad2015-10-14 20:44:51 -04002280 val32 &= 0xff000fff;
2281 val32 |= ((val8 | (val8 << 6)) << 12);
2282
2283 rtl8xxxu_write32(priv, REG_MAC_PHY_CTRL, val32);
2284 }
2285
Jes Sorensen8a594852016-04-07 14:19:26 -04002286 if (priv->rtl_chip == RTL8192E)
2287 rtl8xxxu_write32(priv, REG_AFE_XTAL_CTRL, 0x000f81fb);
2288
Jes Sorensen26f1fad2015-10-14 20:44:51 -04002289 return 0;
2290}
2291
2292static int rtl8xxxu_init_rf_regs(struct rtl8xxxu_priv *priv,
2293 struct rtl8xxxu_rfregval *array,
2294 enum rtl8xxxu_rfpath path)
2295{
2296 int i, ret;
2297 u8 reg;
2298 u32 val;
2299
2300 for (i = 0; ; i++) {
2301 reg = array[i].reg;
2302 val = array[i].val;
2303
2304 if (reg == 0xff && val == 0xffffffff)
2305 break;
2306
2307 switch (reg) {
2308 case 0xfe:
2309 msleep(50);
2310 continue;
2311 case 0xfd:
2312 mdelay(5);
2313 continue;
2314 case 0xfc:
2315 mdelay(1);
2316 continue;
2317 case 0xfb:
2318 udelay(50);
2319 continue;
2320 case 0xfa:
2321 udelay(5);
2322 continue;
2323 case 0xf9:
2324 udelay(1);
2325 continue;
2326 }
2327
Jes Sorensen26f1fad2015-10-14 20:44:51 -04002328 ret = rtl8xxxu_write_rfreg(priv, path, reg, val);
2329 if (ret) {
2330 dev_warn(&priv->udev->dev,
2331 "Failed to initialize RF\n");
2332 return -EAGAIN;
2333 }
2334 udelay(1);
2335 }
2336
2337 return 0;
2338}
2339
Jes Sorensen599119f2016-04-28 15:19:06 -04002340int rtl8xxxu_init_phy_rf(struct rtl8xxxu_priv *priv,
2341 struct rtl8xxxu_rfregval *table,
2342 enum rtl8xxxu_rfpath path)
Jes Sorensen26f1fad2015-10-14 20:44:51 -04002343{
2344 u32 val32;
2345 u16 val16, rfsi_rfenv;
2346 u16 reg_sw_ctrl, reg_int_oe, reg_hssi_parm2;
2347
2348 switch (path) {
2349 case RF_A:
2350 reg_sw_ctrl = REG_FPGA0_XA_RF_SW_CTRL;
2351 reg_int_oe = REG_FPGA0_XA_RF_INT_OE;
2352 reg_hssi_parm2 = REG_FPGA0_XA_HSSI_PARM2;
2353 break;
2354 case RF_B:
2355 reg_sw_ctrl = REG_FPGA0_XB_RF_SW_CTRL;
2356 reg_int_oe = REG_FPGA0_XB_RF_INT_OE;
2357 reg_hssi_parm2 = REG_FPGA0_XB_HSSI_PARM2;
2358 break;
2359 default:
2360 dev_err(&priv->udev->dev, "%s:Unsupported RF path %c\n",
2361 __func__, path + 'A');
2362 return -EINVAL;
2363 }
2364 /* For path B, use XB */
2365 rfsi_rfenv = rtl8xxxu_read16(priv, reg_sw_ctrl);
2366 rfsi_rfenv &= FPGA0_RF_RFENV;
2367
2368 /*
2369 * These two we might be able to optimize into one
2370 */
2371 val32 = rtl8xxxu_read32(priv, reg_int_oe);
2372 val32 |= BIT(20); /* 0x10 << 16 */
2373 rtl8xxxu_write32(priv, reg_int_oe, val32);
2374 udelay(1);
2375
2376 val32 = rtl8xxxu_read32(priv, reg_int_oe);
2377 val32 |= BIT(4);
2378 rtl8xxxu_write32(priv, reg_int_oe, val32);
2379 udelay(1);
2380
2381 /*
2382 * These two we might be able to optimize into one
2383 */
2384 val32 = rtl8xxxu_read32(priv, reg_hssi_parm2);
2385 val32 &= ~FPGA0_HSSI_3WIRE_ADDR_LEN;
2386 rtl8xxxu_write32(priv, reg_hssi_parm2, val32);
2387 udelay(1);
2388
2389 val32 = rtl8xxxu_read32(priv, reg_hssi_parm2);
2390 val32 &= ~FPGA0_HSSI_3WIRE_DATA_LEN;
2391 rtl8xxxu_write32(priv, reg_hssi_parm2, val32);
2392 udelay(1);
2393
2394 rtl8xxxu_init_rf_regs(priv, table, path);
2395
2396 /* For path B, use XB */
2397 val16 = rtl8xxxu_read16(priv, reg_sw_ctrl);
2398 val16 &= ~FPGA0_RF_RFENV;
2399 val16 |= rfsi_rfenv;
2400 rtl8xxxu_write16(priv, reg_sw_ctrl, val16);
2401
2402 return 0;
2403}
2404
2405static int rtl8xxxu_llt_write(struct rtl8xxxu_priv *priv, u8 address, u8 data)
2406{
2407 int ret = -EBUSY;
2408 int count = 0;
2409 u32 value;
2410
2411 value = LLT_OP_WRITE | address << 8 | data;
2412
2413 rtl8xxxu_write32(priv, REG_LLT_INIT, value);
2414
2415 do {
2416 value = rtl8xxxu_read32(priv, REG_LLT_INIT);
2417 if ((value & LLT_OP_MASK) == LLT_OP_INACTIVE) {
2418 ret = 0;
2419 break;
2420 }
2421 } while (count++ < 20);
2422
2423 return ret;
2424}
2425
Jes Sorensen20e3b2e2016-04-28 15:19:08 -04002426int rtl8xxxu_init_llt_table(struct rtl8xxxu_priv *priv, u8 last_tx_page)
Jes Sorensen26f1fad2015-10-14 20:44:51 -04002427{
2428 int ret;
2429 int i;
2430
2431 for (i = 0; i < last_tx_page; i++) {
2432 ret = rtl8xxxu_llt_write(priv, i, i + 1);
2433 if (ret)
2434 goto exit;
2435 }
2436
2437 ret = rtl8xxxu_llt_write(priv, last_tx_page, 0xff);
2438 if (ret)
2439 goto exit;
2440
2441 /* Mark remaining pages as a ring buffer */
2442 for (i = last_tx_page + 1; i < 0xff; i++) {
2443 ret = rtl8xxxu_llt_write(priv, i, (i + 1));
2444 if (ret)
2445 goto exit;
2446 }
2447
2448 /* Let last entry point to the start entry of ring buffer */
2449 ret = rtl8xxxu_llt_write(priv, 0xff, last_tx_page + 1);
2450 if (ret)
2451 goto exit;
2452
2453exit:
2454 return ret;
2455}
2456
Jes Sorensen599119f2016-04-28 15:19:06 -04002457int rtl8xxxu_auto_llt_table(struct rtl8xxxu_priv *priv, u8 last_tx_page)
Jes Sorensen74b99be2016-02-29 17:04:04 -05002458{
2459 u32 val32;
2460 int ret = 0;
2461 int i;
2462
2463 val32 = rtl8xxxu_read32(priv, REG_AUTO_LLT);
Jes Sorensen74b99be2016-02-29 17:04:04 -05002464 val32 |= AUTO_LLT_INIT_LLT;
2465 rtl8xxxu_write32(priv, REG_AUTO_LLT, val32);
2466
2467 for (i = 500; i; i--) {
2468 val32 = rtl8xxxu_read32(priv, REG_AUTO_LLT);
2469 if (!(val32 & AUTO_LLT_INIT_LLT))
2470 break;
2471 usleep_range(2, 4);
2472 }
2473
Jes Sorensen4de24812016-02-29 17:04:07 -05002474 if (!i) {
Jes Sorensen74b99be2016-02-29 17:04:04 -05002475 ret = -EBUSY;
2476 dev_warn(&priv->udev->dev, "LLT table init failed\n");
2477 }
Jes Sorensen74b99be2016-02-29 17:04:04 -05002478
2479 return ret;
2480}
2481
Jes Sorensen26f1fad2015-10-14 20:44:51 -04002482static int rtl8xxxu_init_queue_priority(struct rtl8xxxu_priv *priv)
2483{
2484 u16 val16, hi, lo;
2485 u16 hiq, mgq, bkq, beq, viq, voq;
2486 int hip, mgp, bkp, bep, vip, vop;
2487 int ret = 0;
2488
2489 switch (priv->ep_tx_count) {
2490 case 1:
2491 if (priv->ep_tx_high_queue) {
2492 hi = TRXDMA_QUEUE_HIGH;
2493 } else if (priv->ep_tx_low_queue) {
2494 hi = TRXDMA_QUEUE_LOW;
2495 } else if (priv->ep_tx_normal_queue) {
2496 hi = TRXDMA_QUEUE_NORMAL;
2497 } else {
2498 hi = 0;
2499 ret = -EINVAL;
2500 }
2501
2502 hiq = hi;
2503 mgq = hi;
2504 bkq = hi;
2505 beq = hi;
2506 viq = hi;
2507 voq = hi;
2508
2509 hip = 0;
2510 mgp = 0;
2511 bkp = 0;
2512 bep = 0;
2513 vip = 0;
2514 vop = 0;
2515 break;
2516 case 2:
2517 if (priv->ep_tx_high_queue && priv->ep_tx_low_queue) {
2518 hi = TRXDMA_QUEUE_HIGH;
2519 lo = TRXDMA_QUEUE_LOW;
2520 } else if (priv->ep_tx_normal_queue && priv->ep_tx_low_queue) {
2521 hi = TRXDMA_QUEUE_NORMAL;
2522 lo = TRXDMA_QUEUE_LOW;
2523 } else if (priv->ep_tx_high_queue && priv->ep_tx_normal_queue) {
2524 hi = TRXDMA_QUEUE_HIGH;
2525 lo = TRXDMA_QUEUE_NORMAL;
2526 } else {
2527 ret = -EINVAL;
2528 hi = 0;
2529 lo = 0;
2530 }
2531
2532 hiq = hi;
2533 mgq = hi;
2534 bkq = lo;
2535 beq = lo;
2536 viq = hi;
2537 voq = hi;
2538
2539 hip = 0;
2540 mgp = 0;
2541 bkp = 1;
2542 bep = 1;
2543 vip = 0;
2544 vop = 0;
2545 break;
2546 case 3:
2547 beq = TRXDMA_QUEUE_LOW;
2548 bkq = TRXDMA_QUEUE_LOW;
2549 viq = TRXDMA_QUEUE_NORMAL;
2550 voq = TRXDMA_QUEUE_HIGH;
2551 mgq = TRXDMA_QUEUE_HIGH;
2552 hiq = TRXDMA_QUEUE_HIGH;
2553
2554 hip = hiq ^ 3;
2555 mgp = mgq ^ 3;
2556 bkp = bkq ^ 3;
2557 bep = beq ^ 3;
2558 vip = viq ^ 3;
2559 vop = viq ^ 3;
2560 break;
2561 default:
2562 ret = -EINVAL;
2563 }
2564
2565 /*
2566 * None of the vendor drivers are configuring the beacon
2567 * queue here .... why?
2568 */
2569 if (!ret) {
2570 val16 = rtl8xxxu_read16(priv, REG_TRXDMA_CTRL);
2571 val16 &= 0x7;
2572 val16 |= (voq << TRXDMA_CTRL_VOQ_SHIFT) |
2573 (viq << TRXDMA_CTRL_VIQ_SHIFT) |
2574 (beq << TRXDMA_CTRL_BEQ_SHIFT) |
2575 (bkq << TRXDMA_CTRL_BKQ_SHIFT) |
2576 (mgq << TRXDMA_CTRL_MGQ_SHIFT) |
2577 (hiq << TRXDMA_CTRL_HIQ_SHIFT);
2578 rtl8xxxu_write16(priv, REG_TRXDMA_CTRL, val16);
2579
2580 priv->pipe_out[TXDESC_QUEUE_VO] =
2581 usb_sndbulkpipe(priv->udev, priv->out_ep[vop]);
2582 priv->pipe_out[TXDESC_QUEUE_VI] =
2583 usb_sndbulkpipe(priv->udev, priv->out_ep[vip]);
2584 priv->pipe_out[TXDESC_QUEUE_BE] =
2585 usb_sndbulkpipe(priv->udev, priv->out_ep[bep]);
2586 priv->pipe_out[TXDESC_QUEUE_BK] =
2587 usb_sndbulkpipe(priv->udev, priv->out_ep[bkp]);
2588 priv->pipe_out[TXDESC_QUEUE_BEACON] =
2589 usb_sndbulkpipe(priv->udev, priv->out_ep[0]);
2590 priv->pipe_out[TXDESC_QUEUE_MGNT] =
2591 usb_sndbulkpipe(priv->udev, priv->out_ep[mgp]);
2592 priv->pipe_out[TXDESC_QUEUE_HIGH] =
2593 usb_sndbulkpipe(priv->udev, priv->out_ep[hip]);
2594 priv->pipe_out[TXDESC_QUEUE_CMD] =
2595 usb_sndbulkpipe(priv->udev, priv->out_ep[0]);
2596 }
2597
2598 return ret;
2599}
2600
Jes Sorensen599119f2016-04-28 15:19:06 -04002601void rtl8xxxu_fill_iqk_matrix_a(struct rtl8xxxu_priv *priv, bool iqk_ok,
2602 int result[][8], int candidate, bool tx_only)
Jes Sorensen26f1fad2015-10-14 20:44:51 -04002603{
2604 u32 oldval, x, tx0_a, reg;
2605 int y, tx0_c;
2606 u32 val32;
2607
2608 if (!iqk_ok)
2609 return;
2610
2611 val32 = rtl8xxxu_read32(priv, REG_OFDM0_XA_TX_IQ_IMBALANCE);
2612 oldval = val32 >> 22;
2613
2614 x = result[candidate][0];
2615 if ((x & 0x00000200) != 0)
2616 x = x | 0xfffffc00;
2617 tx0_a = (x * oldval) >> 8;
2618
2619 val32 = rtl8xxxu_read32(priv, REG_OFDM0_XA_TX_IQ_IMBALANCE);
2620 val32 &= ~0x3ff;
2621 val32 |= tx0_a;
2622 rtl8xxxu_write32(priv, REG_OFDM0_XA_TX_IQ_IMBALANCE, val32);
2623
2624 val32 = rtl8xxxu_read32(priv, REG_OFDM0_ENERGY_CCA_THRES);
2625 val32 &= ~BIT(31);
2626 if ((x * oldval >> 7) & 0x1)
2627 val32 |= BIT(31);
2628 rtl8xxxu_write32(priv, REG_OFDM0_ENERGY_CCA_THRES, val32);
2629
2630 y = result[candidate][1];
2631 if ((y & 0x00000200) != 0)
2632 y = y | 0xfffffc00;
2633 tx0_c = (y * oldval) >> 8;
2634
2635 val32 = rtl8xxxu_read32(priv, REG_OFDM0_XC_TX_AFE);
2636 val32 &= ~0xf0000000;
2637 val32 |= (((tx0_c & 0x3c0) >> 6) << 28);
2638 rtl8xxxu_write32(priv, REG_OFDM0_XC_TX_AFE, val32);
2639
2640 val32 = rtl8xxxu_read32(priv, REG_OFDM0_XA_TX_IQ_IMBALANCE);
2641 val32 &= ~0x003f0000;
2642 val32 |= ((tx0_c & 0x3f) << 16);
2643 rtl8xxxu_write32(priv, REG_OFDM0_XA_TX_IQ_IMBALANCE, val32);
2644
2645 val32 = rtl8xxxu_read32(priv, REG_OFDM0_ENERGY_CCA_THRES);
2646 val32 &= ~BIT(29);
2647 if ((y * oldval >> 7) & 0x1)
2648 val32 |= BIT(29);
2649 rtl8xxxu_write32(priv, REG_OFDM0_ENERGY_CCA_THRES, val32);
2650
2651 if (tx_only) {
2652 dev_dbg(&priv->udev->dev, "%s: only TX\n", __func__);
2653 return;
2654 }
2655
2656 reg = result[candidate][2];
2657
2658 val32 = rtl8xxxu_read32(priv, REG_OFDM0_XA_RX_IQ_IMBALANCE);
2659 val32 &= ~0x3ff;
2660 val32 |= (reg & 0x3ff);
2661 rtl8xxxu_write32(priv, REG_OFDM0_XA_RX_IQ_IMBALANCE, val32);
2662
2663 reg = result[candidate][3] & 0x3F;
2664
2665 val32 = rtl8xxxu_read32(priv, REG_OFDM0_XA_RX_IQ_IMBALANCE);
2666 val32 &= ~0xfc00;
2667 val32 |= ((reg << 10) & 0xfc00);
2668 rtl8xxxu_write32(priv, REG_OFDM0_XA_RX_IQ_IMBALANCE, val32);
2669
2670 reg = (result[candidate][3] >> 6) & 0xF;
2671
2672 val32 = rtl8xxxu_read32(priv, REG_OFDM0_RX_IQ_EXT_ANTA);
2673 val32 &= ~0xf0000000;
2674 val32 |= (reg << 28);
2675 rtl8xxxu_write32(priv, REG_OFDM0_RX_IQ_EXT_ANTA, val32);
2676}
2677
Jes Sorensen599119f2016-04-28 15:19:06 -04002678void rtl8xxxu_fill_iqk_matrix_b(struct rtl8xxxu_priv *priv, bool iqk_ok,
2679 int result[][8], int candidate, bool tx_only)
Jes Sorensen26f1fad2015-10-14 20:44:51 -04002680{
2681 u32 oldval, x, tx1_a, reg;
2682 int y, tx1_c;
2683 u32 val32;
2684
2685 if (!iqk_ok)
2686 return;
2687
2688 val32 = rtl8xxxu_read32(priv, REG_OFDM0_XB_TX_IQ_IMBALANCE);
2689 oldval = val32 >> 22;
2690
2691 x = result[candidate][4];
2692 if ((x & 0x00000200) != 0)
2693 x = x | 0xfffffc00;
2694 tx1_a = (x * oldval) >> 8;
2695
2696 val32 = rtl8xxxu_read32(priv, REG_OFDM0_XB_TX_IQ_IMBALANCE);
2697 val32 &= ~0x3ff;
2698 val32 |= tx1_a;
2699 rtl8xxxu_write32(priv, REG_OFDM0_XB_TX_IQ_IMBALANCE, val32);
2700
2701 val32 = rtl8xxxu_read32(priv, REG_OFDM0_ENERGY_CCA_THRES);
2702 val32 &= ~BIT(27);
2703 if ((x * oldval >> 7) & 0x1)
2704 val32 |= BIT(27);
2705 rtl8xxxu_write32(priv, REG_OFDM0_ENERGY_CCA_THRES, val32);
2706
2707 y = result[candidate][5];
2708 if ((y & 0x00000200) != 0)
2709 y = y | 0xfffffc00;
2710 tx1_c = (y * oldval) >> 8;
2711
2712 val32 = rtl8xxxu_read32(priv, REG_OFDM0_XD_TX_AFE);
2713 val32 &= ~0xf0000000;
2714 val32 |= (((tx1_c & 0x3c0) >> 6) << 28);
2715 rtl8xxxu_write32(priv, REG_OFDM0_XD_TX_AFE, val32);
2716
2717 val32 = rtl8xxxu_read32(priv, REG_OFDM0_XB_TX_IQ_IMBALANCE);
2718 val32 &= ~0x003f0000;
2719 val32 |= ((tx1_c & 0x3f) << 16);
2720 rtl8xxxu_write32(priv, REG_OFDM0_XB_TX_IQ_IMBALANCE, val32);
2721
2722 val32 = rtl8xxxu_read32(priv, REG_OFDM0_ENERGY_CCA_THRES);
2723 val32 &= ~BIT(25);
2724 if ((y * oldval >> 7) & 0x1)
2725 val32 |= BIT(25);
2726 rtl8xxxu_write32(priv, REG_OFDM0_ENERGY_CCA_THRES, val32);
2727
2728 if (tx_only) {
2729 dev_dbg(&priv->udev->dev, "%s: only TX\n", __func__);
2730 return;
2731 }
2732
2733 reg = result[candidate][6];
2734
2735 val32 = rtl8xxxu_read32(priv, REG_OFDM0_XB_RX_IQ_IMBALANCE);
2736 val32 &= ~0x3ff;
2737 val32 |= (reg & 0x3ff);
2738 rtl8xxxu_write32(priv, REG_OFDM0_XB_RX_IQ_IMBALANCE, val32);
2739
2740 reg = result[candidate][7] & 0x3f;
2741
2742 val32 = rtl8xxxu_read32(priv, REG_OFDM0_XB_RX_IQ_IMBALANCE);
2743 val32 &= ~0xfc00;
2744 val32 |= ((reg << 10) & 0xfc00);
2745 rtl8xxxu_write32(priv, REG_OFDM0_XB_RX_IQ_IMBALANCE, val32);
2746
2747 reg = (result[candidate][7] >> 6) & 0xf;
2748
2749 val32 = rtl8xxxu_read32(priv, REG_OFDM0_AGCR_SSI_TABLE);
2750 val32 &= ~0x0000f000;
2751 val32 |= (reg << 12);
2752 rtl8xxxu_write32(priv, REG_OFDM0_AGCR_SSI_TABLE, val32);
2753}
2754
2755#define MAX_TOLERANCE 5
2756
2757static bool rtl8xxxu_simularity_compare(struct rtl8xxxu_priv *priv,
2758 int result[][8], int c1, int c2)
2759{
2760 u32 i, j, diff, simubitmap, bound = 0;
2761 int candidate[2] = {-1, -1}; /* for path A and path B */
2762 bool retval = true;
2763
2764 if (priv->tx_paths > 1)
2765 bound = 8;
2766 else
2767 bound = 4;
2768
2769 simubitmap = 0;
2770
2771 for (i = 0; i < bound; i++) {
2772 diff = (result[c1][i] > result[c2][i]) ?
2773 (result[c1][i] - result[c2][i]) :
2774 (result[c2][i] - result[c1][i]);
2775 if (diff > MAX_TOLERANCE) {
2776 if ((i == 2 || i == 6) && !simubitmap) {
2777 if (result[c1][i] + result[c1][i + 1] == 0)
2778 candidate[(i / 4)] = c2;
2779 else if (result[c2][i] + result[c2][i + 1] == 0)
2780 candidate[(i / 4)] = c1;
2781 else
2782 simubitmap = simubitmap | (1 << i);
2783 } else {
2784 simubitmap = simubitmap | (1 << i);
2785 }
2786 }
2787 }
2788
2789 if (simubitmap == 0) {
2790 for (i = 0; i < (bound / 4); i++) {
2791 if (candidate[i] >= 0) {
2792 for (j = i * 4; j < (i + 1) * 4 - 2; j++)
2793 result[3][j] = result[candidate[i]][j];
2794 retval = false;
2795 }
2796 }
2797 return retval;
2798 } else if (!(simubitmap & 0x0f)) {
2799 /* path A OK */
2800 for (i = 0; i < 4; i++)
2801 result[3][i] = result[c1][i];
2802 } else if (!(simubitmap & 0xf0) && priv->tx_paths > 1) {
2803 /* path B OK */
2804 for (i = 4; i < 8; i++)
2805 result[3][i] = result[c1][i];
2806 }
2807
2808 return false;
2809}
2810
Jes Sorensen599119f2016-04-28 15:19:06 -04002811bool rtl8xxxu_gen2_simularity_compare(struct rtl8xxxu_priv *priv,
2812 int result[][8], int c1, int c2)
Jes Sorensene1547c52016-02-29 17:04:35 -05002813{
2814 u32 i, j, diff, simubitmap, bound = 0;
2815 int candidate[2] = {-1, -1}; /* for path A and path B */
2816 int tmp1, tmp2;
2817 bool retval = true;
2818
2819 if (priv->tx_paths > 1)
2820 bound = 8;
2821 else
2822 bound = 4;
2823
2824 simubitmap = 0;
2825
2826 for (i = 0; i < bound; i++) {
2827 if (i & 1) {
2828 if ((result[c1][i] & 0x00000200))
2829 tmp1 = result[c1][i] | 0xfffffc00;
2830 else
2831 tmp1 = result[c1][i];
2832
2833 if ((result[c2][i]& 0x00000200))
2834 tmp2 = result[c2][i] | 0xfffffc00;
2835 else
2836 tmp2 = result[c2][i];
2837 } else {
2838 tmp1 = result[c1][i];
2839 tmp2 = result[c2][i];
2840 }
2841
2842 diff = (tmp1 > tmp2) ? (tmp1 - tmp2) : (tmp2 - tmp1);
2843
2844 if (diff > MAX_TOLERANCE) {
2845 if ((i == 2 || i == 6) && !simubitmap) {
2846 if (result[c1][i] + result[c1][i + 1] == 0)
2847 candidate[(i / 4)] = c2;
2848 else if (result[c2][i] + result[c2][i + 1] == 0)
2849 candidate[(i / 4)] = c1;
2850 else
2851 simubitmap = simubitmap | (1 << i);
2852 } else {
2853 simubitmap = simubitmap | (1 << i);
2854 }
2855 }
2856 }
2857
2858 if (simubitmap == 0) {
2859 for (i = 0; i < (bound / 4); i++) {
2860 if (candidate[i] >= 0) {
2861 for (j = i * 4; j < (i + 1) * 4 - 2; j++)
2862 result[3][j] = result[candidate[i]][j];
2863 retval = false;
2864 }
2865 }
2866 return retval;
2867 } else {
2868 if (!(simubitmap & 0x03)) {
2869 /* path A TX OK */
2870 for (i = 0; i < 2; i++)
2871 result[3][i] = result[c1][i];
2872 }
2873
2874 if (!(simubitmap & 0x0c)) {
2875 /* path A RX OK */
2876 for (i = 2; i < 4; i++)
2877 result[3][i] = result[c1][i];
2878 }
2879
2880 if (!(simubitmap & 0x30) && priv->tx_paths > 1) {
2881 /* path B RX OK */
2882 for (i = 4; i < 6; i++)
2883 result[3][i] = result[c1][i];
2884 }
2885
2886 if (!(simubitmap & 0x30) && priv->tx_paths > 1) {
2887 /* path B RX OK */
2888 for (i = 6; i < 8; i++)
2889 result[3][i] = result[c1][i];
2890 }
2891 }
2892
2893 return false;
2894}
2895
Jes Sorensen599119f2016-04-28 15:19:06 -04002896void
Jes Sorensen26f1fad2015-10-14 20:44:51 -04002897rtl8xxxu_save_mac_regs(struct rtl8xxxu_priv *priv, const u32 *reg, u32 *backup)
2898{
2899 int i;
2900
2901 for (i = 0; i < (RTL8XXXU_MAC_REGS - 1); i++)
2902 backup[i] = rtl8xxxu_read8(priv, reg[i]);
2903
2904 backup[i] = rtl8xxxu_read32(priv, reg[i]);
2905}
2906
Jes Sorensen599119f2016-04-28 15:19:06 -04002907void rtl8xxxu_restore_mac_regs(struct rtl8xxxu_priv *priv,
2908 const u32 *reg, u32 *backup)
Jes Sorensen26f1fad2015-10-14 20:44:51 -04002909{
2910 int i;
2911
2912 for (i = 0; i < (RTL8XXXU_MAC_REGS - 1); i++)
2913 rtl8xxxu_write8(priv, reg[i], backup[i]);
2914
2915 rtl8xxxu_write32(priv, reg[i], backup[i]);
2916}
2917
Jes Sorensen599119f2016-04-28 15:19:06 -04002918void rtl8xxxu_save_regs(struct rtl8xxxu_priv *priv, const u32 *regs,
2919 u32 *backup, int count)
Jes Sorensen26f1fad2015-10-14 20:44:51 -04002920{
2921 int i;
2922
2923 for (i = 0; i < count; i++)
2924 backup[i] = rtl8xxxu_read32(priv, regs[i]);
2925}
2926
Jes Sorensen599119f2016-04-28 15:19:06 -04002927void rtl8xxxu_restore_regs(struct rtl8xxxu_priv *priv, const u32 *regs,
2928 u32 *backup, int count)
Jes Sorensen26f1fad2015-10-14 20:44:51 -04002929{
2930 int i;
2931
2932 for (i = 0; i < count; i++)
2933 rtl8xxxu_write32(priv, regs[i], backup[i]);
2934}
2935
2936
Jes Sorensen599119f2016-04-28 15:19:06 -04002937void rtl8xxxu_path_adda_on(struct rtl8xxxu_priv *priv, const u32 *regs,
2938 bool path_a_on)
Jes Sorensen26f1fad2015-10-14 20:44:51 -04002939{
2940 u32 path_on;
2941 int i;
2942
Jes Sorensen26f1fad2015-10-14 20:44:51 -04002943 if (priv->tx_paths == 1) {
Jes Sorensen8634af52016-02-29 17:04:33 -05002944 path_on = priv->fops->adda_1t_path_on;
2945 rtl8xxxu_write32(priv, regs[0], priv->fops->adda_1t_init);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04002946 } else {
Jes Sorensen8634af52016-02-29 17:04:33 -05002947 path_on = path_a_on ? priv->fops->adda_2t_path_on_a :
2948 priv->fops->adda_2t_path_on_b;
2949
Jes Sorensen26f1fad2015-10-14 20:44:51 -04002950 rtl8xxxu_write32(priv, regs[0], path_on);
2951 }
2952
2953 for (i = 1 ; i < RTL8XXXU_ADDA_REGS ; i++)
2954 rtl8xxxu_write32(priv, regs[i], path_on);
2955}
2956
Jes Sorensen599119f2016-04-28 15:19:06 -04002957void rtl8xxxu_mac_calibration(struct rtl8xxxu_priv *priv,
2958 const u32 *regs, u32 *backup)
Jes Sorensen26f1fad2015-10-14 20:44:51 -04002959{
2960 int i = 0;
2961
2962 rtl8xxxu_write8(priv, regs[i], 0x3f);
2963
2964 for (i = 1 ; i < (RTL8XXXU_MAC_REGS - 1); i++)
2965 rtl8xxxu_write8(priv, regs[i], (u8)(backup[i] & ~BIT(3)));
2966
2967 rtl8xxxu_write8(priv, regs[i], (u8)(backup[i] & ~BIT(5)));
2968}
2969
2970static int rtl8xxxu_iqk_path_a(struct rtl8xxxu_priv *priv)
2971{
2972 u32 reg_eac, reg_e94, reg_e9c, reg_ea4, val32;
2973 int result = 0;
2974
2975 /* path-A IQK setting */
2976 rtl8xxxu_write32(priv, REG_TX_IQK_TONE_A, 0x10008c1f);
2977 rtl8xxxu_write32(priv, REG_RX_IQK_TONE_A, 0x10008c1f);
2978 rtl8xxxu_write32(priv, REG_TX_IQK_PI_A, 0x82140102);
2979
2980 val32 = (priv->rf_paths > 1) ? 0x28160202 :
2981 /*IS_81xxC_VENDOR_UMC_B_CUT(pHalData->VersionID)?0x28160202: */
2982 0x28160502;
2983 rtl8xxxu_write32(priv, REG_RX_IQK_PI_A, val32);
2984
2985 /* path-B IQK setting */
2986 if (priv->rf_paths > 1) {
2987 rtl8xxxu_write32(priv, REG_TX_IQK_TONE_B, 0x10008c22);
2988 rtl8xxxu_write32(priv, REG_RX_IQK_TONE_B, 0x10008c22);
2989 rtl8xxxu_write32(priv, REG_TX_IQK_PI_B, 0x82140102);
2990 rtl8xxxu_write32(priv, REG_RX_IQK_PI_B, 0x28160202);
2991 }
2992
2993 /* LO calibration setting */
2994 rtl8xxxu_write32(priv, REG_IQK_AGC_RSP, 0x001028d1);
2995
2996 /* One shot, path A LOK & IQK */
2997 rtl8xxxu_write32(priv, REG_IQK_AGC_PTS, 0xf9000000);
2998 rtl8xxxu_write32(priv, REG_IQK_AGC_PTS, 0xf8000000);
2999
3000 mdelay(1);
3001
3002 /* Check failed */
3003 reg_eac = rtl8xxxu_read32(priv, REG_RX_POWER_AFTER_IQK_A_2);
3004 reg_e94 = rtl8xxxu_read32(priv, REG_TX_POWER_BEFORE_IQK_A);
3005 reg_e9c = rtl8xxxu_read32(priv, REG_TX_POWER_AFTER_IQK_A);
3006 reg_ea4 = rtl8xxxu_read32(priv, REG_RX_POWER_BEFORE_IQK_A_2);
3007
3008 if (!(reg_eac & BIT(28)) &&
3009 ((reg_e94 & 0x03ff0000) != 0x01420000) &&
3010 ((reg_e9c & 0x03ff0000) != 0x00420000))
3011 result |= 0x01;
3012 else /* If TX not OK, ignore RX */
3013 goto out;
3014
3015 /* If TX is OK, check whether RX is OK */
3016 if (!(reg_eac & BIT(27)) &&
3017 ((reg_ea4 & 0x03ff0000) != 0x01320000) &&
3018 ((reg_eac & 0x03ff0000) != 0x00360000))
3019 result |= 0x02;
3020 else
3021 dev_warn(&priv->udev->dev, "%s: Path A RX IQK failed!\n",
3022 __func__);
3023out:
3024 return result;
3025}
3026
3027static int rtl8xxxu_iqk_path_b(struct rtl8xxxu_priv *priv)
3028{
3029 u32 reg_eac, reg_eb4, reg_ebc, reg_ec4, reg_ecc;
3030 int result = 0;
3031
3032 /* One shot, path B LOK & IQK */
3033 rtl8xxxu_write32(priv, REG_IQK_AGC_CONT, 0x00000002);
3034 rtl8xxxu_write32(priv, REG_IQK_AGC_CONT, 0x00000000);
3035
3036 mdelay(1);
3037
3038 /* Check failed */
3039 reg_eac = rtl8xxxu_read32(priv, REG_RX_POWER_AFTER_IQK_A_2);
3040 reg_eb4 = rtl8xxxu_read32(priv, REG_TX_POWER_BEFORE_IQK_B);
3041 reg_ebc = rtl8xxxu_read32(priv, REG_TX_POWER_AFTER_IQK_B);
3042 reg_ec4 = rtl8xxxu_read32(priv, REG_RX_POWER_BEFORE_IQK_B_2);
3043 reg_ecc = rtl8xxxu_read32(priv, REG_RX_POWER_AFTER_IQK_B_2);
3044
3045 if (!(reg_eac & BIT(31)) &&
3046 ((reg_eb4 & 0x03ff0000) != 0x01420000) &&
3047 ((reg_ebc & 0x03ff0000) != 0x00420000))
3048 result |= 0x01;
3049 else
3050 goto out;
3051
3052 if (!(reg_eac & BIT(30)) &&
3053 (((reg_ec4 & 0x03ff0000) >> 16) != 0x132) &&
3054 (((reg_ecc & 0x03ff0000) >> 16) != 0x36))
3055 result |= 0x02;
3056 else
3057 dev_warn(&priv->udev->dev, "%s: Path B RX IQK failed!\n",
3058 __func__);
3059out:
3060 return result;
3061}
3062
3063static void rtl8xxxu_phy_iqcalibrate(struct rtl8xxxu_priv *priv,
3064 int result[][8], int t)
3065{
3066 struct device *dev = &priv->udev->dev;
3067 u32 i, val32;
3068 int path_a_ok, path_b_ok;
3069 int retry = 2;
3070 const u32 adda_regs[RTL8XXXU_ADDA_REGS] = {
3071 REG_FPGA0_XCD_SWITCH_CTRL, REG_BLUETOOTH,
3072 REG_RX_WAIT_CCA, REG_TX_CCK_RFON,
3073 REG_TX_CCK_BBON, REG_TX_OFDM_RFON,
3074 REG_TX_OFDM_BBON, REG_TX_TO_RX,
3075 REG_TX_TO_TX, REG_RX_CCK,
3076 REG_RX_OFDM, REG_RX_WAIT_RIFS,
3077 REG_RX_TO_RX, REG_STANDBY,
3078 REG_SLEEP, REG_PMPD_ANAEN
3079 };
3080 const u32 iqk_mac_regs[RTL8XXXU_MAC_REGS] = {
3081 REG_TXPAUSE, REG_BEACON_CTRL,
3082 REG_BEACON_CTRL_1, REG_GPIO_MUXCFG
3083 };
3084 const u32 iqk_bb_regs[RTL8XXXU_BB_REGS] = {
3085 REG_OFDM0_TRX_PATH_ENABLE, REG_OFDM0_TR_MUX_PAR,
3086 REG_FPGA0_XCD_RF_SW_CTRL, REG_CONFIG_ANT_A, REG_CONFIG_ANT_B,
3087 REG_FPGA0_XAB_RF_SW_CTRL, REG_FPGA0_XA_RF_INT_OE,
3088 REG_FPGA0_XB_RF_INT_OE, REG_FPGA0_RF_MODE
3089 };
3090
3091 /*
3092 * Note: IQ calibration must be performed after loading
3093 * PHY_REG.txt , and radio_a, radio_b.txt
3094 */
3095
3096 if (t == 0) {
3097 /* Save ADDA parameters, turn Path A ADDA on */
3098 rtl8xxxu_save_regs(priv, adda_regs, priv->adda_backup,
3099 RTL8XXXU_ADDA_REGS);
3100 rtl8xxxu_save_mac_regs(priv, iqk_mac_regs, priv->mac_backup);
3101 rtl8xxxu_save_regs(priv, iqk_bb_regs,
3102 priv->bb_backup, RTL8XXXU_BB_REGS);
3103 }
3104
3105 rtl8xxxu_path_adda_on(priv, adda_regs, true);
3106
3107 if (t == 0) {
3108 val32 = rtl8xxxu_read32(priv, REG_FPGA0_XA_HSSI_PARM1);
3109 if (val32 & FPGA0_HSSI_PARM1_PI)
3110 priv->pi_enabled = 1;
3111 }
3112
3113 if (!priv->pi_enabled) {
3114 /* Switch BB to PI mode to do IQ Calibration. */
3115 rtl8xxxu_write32(priv, REG_FPGA0_XA_HSSI_PARM1, 0x01000100);
3116 rtl8xxxu_write32(priv, REG_FPGA0_XB_HSSI_PARM1, 0x01000100);
3117 }
3118
3119 val32 = rtl8xxxu_read32(priv, REG_FPGA0_RF_MODE);
3120 val32 &= ~FPGA_RF_MODE_CCK;
3121 rtl8xxxu_write32(priv, REG_FPGA0_RF_MODE, val32);
3122
3123 rtl8xxxu_write32(priv, REG_OFDM0_TRX_PATH_ENABLE, 0x03a05600);
3124 rtl8xxxu_write32(priv, REG_OFDM0_TR_MUX_PAR, 0x000800e4);
3125 rtl8xxxu_write32(priv, REG_FPGA0_XCD_RF_SW_CTRL, 0x22204000);
3126
Jes Sorensencabb5502016-04-14 16:37:17 -04003127 if (!priv->no_pape) {
3128 val32 = rtl8xxxu_read32(priv, REG_FPGA0_XAB_RF_SW_CTRL);
3129 val32 |= (FPGA0_RF_PAPE |
3130 (FPGA0_RF_PAPE << FPGA0_RF_BD_CTRL_SHIFT));
3131 rtl8xxxu_write32(priv, REG_FPGA0_XAB_RF_SW_CTRL, val32);
3132 }
Jes Sorensen26f1fad2015-10-14 20:44:51 -04003133
3134 val32 = rtl8xxxu_read32(priv, REG_FPGA0_XA_RF_INT_OE);
3135 val32 &= ~BIT(10);
3136 rtl8xxxu_write32(priv, REG_FPGA0_XA_RF_INT_OE, val32);
3137 val32 = rtl8xxxu_read32(priv, REG_FPGA0_XB_RF_INT_OE);
3138 val32 &= ~BIT(10);
3139 rtl8xxxu_write32(priv, REG_FPGA0_XB_RF_INT_OE, val32);
3140
3141 if (priv->tx_paths > 1) {
3142 rtl8xxxu_write32(priv, REG_FPGA0_XA_LSSI_PARM, 0x00010000);
3143 rtl8xxxu_write32(priv, REG_FPGA0_XB_LSSI_PARM, 0x00010000);
3144 }
3145
3146 /* MAC settings */
3147 rtl8xxxu_mac_calibration(priv, iqk_mac_regs, priv->mac_backup);
3148
3149 /* Page B init */
3150 rtl8xxxu_write32(priv, REG_CONFIG_ANT_A, 0x00080000);
3151
3152 if (priv->tx_paths > 1)
3153 rtl8xxxu_write32(priv, REG_CONFIG_ANT_B, 0x00080000);
3154
3155 /* IQ calibration setting */
3156 rtl8xxxu_write32(priv, REG_FPGA0_IQK, 0x80800000);
3157 rtl8xxxu_write32(priv, REG_TX_IQK, 0x01007c00);
3158 rtl8xxxu_write32(priv, REG_RX_IQK, 0x01004800);
3159
3160 for (i = 0; i < retry; i++) {
3161 path_a_ok = rtl8xxxu_iqk_path_a(priv);
3162 if (path_a_ok == 0x03) {
3163 val32 = rtl8xxxu_read32(priv,
3164 REG_TX_POWER_BEFORE_IQK_A);
3165 result[t][0] = (val32 >> 16) & 0x3ff;
3166 val32 = rtl8xxxu_read32(priv,
3167 REG_TX_POWER_AFTER_IQK_A);
3168 result[t][1] = (val32 >> 16) & 0x3ff;
3169 val32 = rtl8xxxu_read32(priv,
3170 REG_RX_POWER_BEFORE_IQK_A_2);
3171 result[t][2] = (val32 >> 16) & 0x3ff;
3172 val32 = rtl8xxxu_read32(priv,
3173 REG_RX_POWER_AFTER_IQK_A_2);
3174 result[t][3] = (val32 >> 16) & 0x3ff;
3175 break;
3176 } else if (i == (retry - 1) && path_a_ok == 0x01) {
3177 /* TX IQK OK */
3178 dev_dbg(dev, "%s: Path A IQK Only Tx Success!!\n",
3179 __func__);
3180
3181 val32 = rtl8xxxu_read32(priv,
3182 REG_TX_POWER_BEFORE_IQK_A);
3183 result[t][0] = (val32 >> 16) & 0x3ff;
3184 val32 = rtl8xxxu_read32(priv,
3185 REG_TX_POWER_AFTER_IQK_A);
3186 result[t][1] = (val32 >> 16) & 0x3ff;
3187 }
3188 }
3189
3190 if (!path_a_ok)
3191 dev_dbg(dev, "%s: Path A IQK failed!\n", __func__);
3192
3193 if (priv->tx_paths > 1) {
3194 /*
3195 * Path A into standby
3196 */
3197 rtl8xxxu_write32(priv, REG_FPGA0_IQK, 0x0);
3198 rtl8xxxu_write32(priv, REG_FPGA0_XA_LSSI_PARM, 0x00010000);
3199 rtl8xxxu_write32(priv, REG_FPGA0_IQK, 0x80800000);
3200
3201 /* Turn Path B ADDA on */
3202 rtl8xxxu_path_adda_on(priv, adda_regs, false);
3203
3204 for (i = 0; i < retry; i++) {
3205 path_b_ok = rtl8xxxu_iqk_path_b(priv);
3206 if (path_b_ok == 0x03) {
3207 val32 = rtl8xxxu_read32(priv, REG_TX_POWER_BEFORE_IQK_B);
3208 result[t][4] = (val32 >> 16) & 0x3ff;
3209 val32 = rtl8xxxu_read32(priv, REG_TX_POWER_AFTER_IQK_B);
3210 result[t][5] = (val32 >> 16) & 0x3ff;
3211 val32 = rtl8xxxu_read32(priv, REG_RX_POWER_BEFORE_IQK_B_2);
3212 result[t][6] = (val32 >> 16) & 0x3ff;
3213 val32 = rtl8xxxu_read32(priv, REG_RX_POWER_AFTER_IQK_B_2);
3214 result[t][7] = (val32 >> 16) & 0x3ff;
3215 break;
3216 } else if (i == (retry - 1) && path_b_ok == 0x01) {
3217 /* TX IQK OK */
3218 val32 = rtl8xxxu_read32(priv, REG_TX_POWER_BEFORE_IQK_B);
3219 result[t][4] = (val32 >> 16) & 0x3ff;
3220 val32 = rtl8xxxu_read32(priv, REG_TX_POWER_AFTER_IQK_B);
3221 result[t][5] = (val32 >> 16) & 0x3ff;
3222 }
3223 }
3224
3225 if (!path_b_ok)
3226 dev_dbg(dev, "%s: Path B IQK failed!\n", __func__);
3227 }
3228
3229 /* Back to BB mode, load original value */
3230 rtl8xxxu_write32(priv, REG_FPGA0_IQK, 0);
3231
3232 if (t) {
3233 if (!priv->pi_enabled) {
3234 /*
3235 * Switch back BB to SI mode after finishing
3236 * IQ Calibration
3237 */
3238 val32 = 0x01000000;
3239 rtl8xxxu_write32(priv, REG_FPGA0_XA_HSSI_PARM1, val32);
3240 rtl8xxxu_write32(priv, REG_FPGA0_XB_HSSI_PARM1, val32);
3241 }
3242
3243 /* Reload ADDA power saving parameters */
3244 rtl8xxxu_restore_regs(priv, adda_regs, priv->adda_backup,
3245 RTL8XXXU_ADDA_REGS);
3246
3247 /* Reload MAC parameters */
3248 rtl8xxxu_restore_mac_regs(priv, iqk_mac_regs, priv->mac_backup);
3249
3250 /* Reload BB parameters */
3251 rtl8xxxu_restore_regs(priv, iqk_bb_regs,
3252 priv->bb_backup, RTL8XXXU_BB_REGS);
3253
3254 /* Restore RX initial gain */
3255 rtl8xxxu_write32(priv, REG_FPGA0_XA_LSSI_PARM, 0x00032ed3);
3256
3257 if (priv->tx_paths > 1) {
3258 rtl8xxxu_write32(priv, REG_FPGA0_XB_LSSI_PARM,
3259 0x00032ed3);
3260 }
3261
3262 /* Load 0xe30 IQC default value */
3263 rtl8xxxu_write32(priv, REG_TX_IQK_TONE_A, 0x01008c00);
3264 rtl8xxxu_write32(priv, REG_RX_IQK_TONE_A, 0x01008c00);
3265 }
3266}
3267
Jes Sorensen6c46ca32016-04-28 15:19:07 -04003268void rtl8xxxu_prepare_calibrate(struct rtl8xxxu_priv *priv, u8 start)
Jes Sorensenc7a5a192016-02-29 17:04:30 -05003269{
3270 struct h2c_cmd h2c;
3271
3272 if (priv->fops->mbox_ext_width < 4)
3273 return;
3274
3275 memset(&h2c, 0, sizeof(struct h2c_cmd));
3276 h2c.bt_wlan_calibration.cmd = H2C_8723B_BT_WLAN_CALIBRATION;
3277 h2c.bt_wlan_calibration.data = start;
3278
3279 rtl8723a_h2c_cmd(priv, &h2c, sizeof(h2c.bt_wlan_calibration));
3280}
3281
Jes Sorensen20e3b2e2016-04-28 15:19:08 -04003282void rtl8xxxu_gen1_phy_iq_calibrate(struct rtl8xxxu_priv *priv)
Jes Sorensen26f1fad2015-10-14 20:44:51 -04003283{
3284 struct device *dev = &priv->udev->dev;
3285 int result[4][8]; /* last is final result */
3286 int i, candidate;
3287 bool path_a_ok, path_b_ok;
3288 u32 reg_e94, reg_e9c, reg_ea4, reg_eac;
3289 u32 reg_eb4, reg_ebc, reg_ec4, reg_ecc;
3290 s32 reg_tmp = 0;
3291 bool simu;
3292
Jes Sorensenc7a5a192016-02-29 17:04:30 -05003293 rtl8xxxu_prepare_calibrate(priv, 1);
3294
Jes Sorensen26f1fad2015-10-14 20:44:51 -04003295 memset(result, 0, sizeof(result));
3296 candidate = -1;
3297
3298 path_a_ok = false;
3299 path_b_ok = false;
3300
3301 rtl8xxxu_read32(priv, REG_FPGA0_RF_MODE);
3302
3303 for (i = 0; i < 3; i++) {
3304 rtl8xxxu_phy_iqcalibrate(priv, result, i);
3305
3306 if (i == 1) {
3307 simu = rtl8xxxu_simularity_compare(priv, result, 0, 1);
3308 if (simu) {
3309 candidate = 0;
3310 break;
3311 }
3312 }
3313
3314 if (i == 2) {
3315 simu = rtl8xxxu_simularity_compare(priv, result, 0, 2);
3316 if (simu) {
3317 candidate = 0;
3318 break;
3319 }
3320
3321 simu = rtl8xxxu_simularity_compare(priv, result, 1, 2);
3322 if (simu) {
3323 candidate = 1;
3324 } else {
3325 for (i = 0; i < 8; i++)
3326 reg_tmp += result[3][i];
3327
3328 if (reg_tmp)
3329 candidate = 3;
3330 else
3331 candidate = -1;
3332 }
3333 }
3334 }
3335
3336 for (i = 0; i < 4; i++) {
3337 reg_e94 = result[i][0];
3338 reg_e9c = result[i][1];
3339 reg_ea4 = result[i][2];
3340 reg_eac = result[i][3];
3341 reg_eb4 = result[i][4];
3342 reg_ebc = result[i][5];
3343 reg_ec4 = result[i][6];
3344 reg_ecc = result[i][7];
3345 }
3346
3347 if (candidate >= 0) {
3348 reg_e94 = result[candidate][0];
3349 priv->rege94 = reg_e94;
3350 reg_e9c = result[candidate][1];
3351 priv->rege9c = reg_e9c;
3352 reg_ea4 = result[candidate][2];
3353 reg_eac = result[candidate][3];
3354 reg_eb4 = result[candidate][4];
3355 priv->regeb4 = reg_eb4;
3356 reg_ebc = result[candidate][5];
3357 priv->regebc = reg_ebc;
3358 reg_ec4 = result[candidate][6];
3359 reg_ecc = result[candidate][7];
3360 dev_dbg(dev, "%s: candidate is %x\n", __func__, candidate);
3361 dev_dbg(dev,
3362 "%s: e94 =%x e9c=%x ea4=%x eac=%x eb4=%x ebc=%x ec4=%x "
3363 "ecc=%x\n ", __func__, reg_e94, reg_e9c,
3364 reg_ea4, reg_eac, reg_eb4, reg_ebc, reg_ec4, reg_ecc);
3365 path_a_ok = true;
3366 path_b_ok = true;
3367 } else {
3368 reg_e94 = reg_eb4 = priv->rege94 = priv->regeb4 = 0x100;
3369 reg_e9c = reg_ebc = priv->rege9c = priv->regebc = 0x0;
3370 }
3371
3372 if (reg_e94 && candidate >= 0)
3373 rtl8xxxu_fill_iqk_matrix_a(priv, path_a_ok, result,
3374 candidate, (reg_ea4 == 0));
3375
3376 if (priv->tx_paths > 1 && reg_eb4)
3377 rtl8xxxu_fill_iqk_matrix_b(priv, path_b_ok, result,
3378 candidate, (reg_ec4 == 0));
3379
Jes Sorensen04a74a92016-04-18 11:49:36 -04003380 rtl8xxxu_save_regs(priv, rtl8xxxu_iqk_phy_iq_bb_reg,
Jes Sorensen26f1fad2015-10-14 20:44:51 -04003381 priv->bb_recovery_backup, RTL8XXXU_BB_REGS);
Jes Sorensenc7a5a192016-02-29 17:04:30 -05003382
3383 rtl8xxxu_prepare_calibrate(priv, 0);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04003384}
3385
3386static void rtl8723a_phy_lc_calibrate(struct rtl8xxxu_priv *priv)
3387{
3388 u32 val32;
3389 u32 rf_amode, rf_bmode = 0, lstf;
3390
3391 /* Check continuous TX and Packet TX */
3392 lstf = rtl8xxxu_read32(priv, REG_OFDM1_LSTF);
3393
3394 if (lstf & OFDM_LSTF_MASK) {
3395 /* Disable all continuous TX */
3396 val32 = lstf & ~OFDM_LSTF_MASK;
3397 rtl8xxxu_write32(priv, REG_OFDM1_LSTF, val32);
3398
3399 /* Read original RF mode Path A */
3400 rf_amode = rtl8xxxu_read_rfreg(priv, RF_A, RF6052_REG_AC);
3401
3402 /* Set RF mode to standby Path A */
3403 rtl8xxxu_write_rfreg(priv, RF_A, RF6052_REG_AC,
3404 (rf_amode & 0x8ffff) | 0x10000);
3405
3406 /* Path-B */
3407 if (priv->tx_paths > 1) {
3408 rf_bmode = rtl8xxxu_read_rfreg(priv, RF_B,
3409 RF6052_REG_AC);
3410
3411 rtl8xxxu_write_rfreg(priv, RF_B, RF6052_REG_AC,
3412 (rf_bmode & 0x8ffff) | 0x10000);
3413 }
3414 } else {
3415 /* Deal with Packet TX case */
3416 /* block all queues */
3417 rtl8xxxu_write8(priv, REG_TXPAUSE, 0xff);
3418 }
3419
3420 /* Start LC calibration */
Jes Sorensen0d698de2016-02-29 17:04:36 -05003421 if (priv->fops->has_s0s1)
3422 rtl8xxxu_write_rfreg(priv, RF_A, RF6052_REG_S0S1, 0xdfbe0);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04003423 val32 = rtl8xxxu_read_rfreg(priv, RF_A, RF6052_REG_MODE_AG);
3424 val32 |= 0x08000;
3425 rtl8xxxu_write_rfreg(priv, RF_A, RF6052_REG_MODE_AG, val32);
3426
3427 msleep(100);
3428
Jes Sorensen0d698de2016-02-29 17:04:36 -05003429 if (priv->fops->has_s0s1)
3430 rtl8xxxu_write_rfreg(priv, RF_A, RF6052_REG_S0S1, 0xdffe0);
3431
Jes Sorensen26f1fad2015-10-14 20:44:51 -04003432 /* Restore original parameters */
3433 if (lstf & OFDM_LSTF_MASK) {
3434 /* Path-A */
3435 rtl8xxxu_write32(priv, REG_OFDM1_LSTF, lstf);
3436 rtl8xxxu_write_rfreg(priv, RF_A, RF6052_REG_AC, rf_amode);
3437
3438 /* Path-B */
3439 if (priv->tx_paths > 1)
3440 rtl8xxxu_write_rfreg(priv, RF_B, RF6052_REG_AC,
3441 rf_bmode);
3442 } else /* Deal with Packet TX case */
3443 rtl8xxxu_write8(priv, REG_TXPAUSE, 0x00);
3444}
3445
3446static int rtl8xxxu_set_mac(struct rtl8xxxu_priv *priv)
3447{
3448 int i;
3449 u16 reg;
3450
3451 reg = REG_MACID;
3452
3453 for (i = 0; i < ETH_ALEN; i++)
3454 rtl8xxxu_write8(priv, reg + i, priv->mac_addr[i]);
3455
3456 return 0;
3457}
3458
3459static int rtl8xxxu_set_bssid(struct rtl8xxxu_priv *priv, const u8 *bssid)
3460{
3461 int i;
3462 u16 reg;
3463
3464 dev_dbg(&priv->udev->dev, "%s: (%pM)\n", __func__, bssid);
3465
3466 reg = REG_BSSID;
3467
3468 for (i = 0; i < ETH_ALEN; i++)
3469 rtl8xxxu_write8(priv, reg + i, bssid[i]);
3470
3471 return 0;
3472}
3473
3474static void
3475rtl8xxxu_set_ampdu_factor(struct rtl8xxxu_priv *priv, u8 ampdu_factor)
3476{
3477 u8 vals[4] = { 0x41, 0xa8, 0x72, 0xb9 };
3478 u8 max_agg = 0xf;
3479 int i;
3480
3481 ampdu_factor = 1 << (ampdu_factor + 2);
3482 if (ampdu_factor > max_agg)
3483 ampdu_factor = max_agg;
3484
3485 for (i = 0; i < 4; i++) {
3486 if ((vals[i] & 0xf0) > (ampdu_factor << 4))
3487 vals[i] = (vals[i] & 0x0f) | (ampdu_factor << 4);
3488
3489 if ((vals[i] & 0x0f) > ampdu_factor)
3490 vals[i] = (vals[i] & 0xf0) | ampdu_factor;
3491
3492 rtl8xxxu_write8(priv, REG_AGGLEN_LMT + i, vals[i]);
3493 }
3494}
3495
3496static void rtl8xxxu_set_ampdu_min_space(struct rtl8xxxu_priv *priv, u8 density)
3497{
3498 u8 val8;
3499
3500 val8 = rtl8xxxu_read8(priv, REG_AMPDU_MIN_SPACE);
3501 val8 &= 0xf8;
3502 val8 |= density;
3503 rtl8xxxu_write8(priv, REG_AMPDU_MIN_SPACE, val8);
3504}
3505
3506static int rtl8xxxu_active_to_emu(struct rtl8xxxu_priv *priv)
3507{
3508 u8 val8;
Colin Ian King37ba4b62016-04-14 16:37:07 -04003509 int count, ret = 0;
Jes Sorensen26f1fad2015-10-14 20:44:51 -04003510
3511 /* Start of rtl8723AU_card_enable_flow */
3512 /* Act to Cardemu sequence*/
3513 /* Turn off RF */
3514 rtl8xxxu_write8(priv, REG_RF_CTRL, 0);
3515
3516 /* 0x004E[7] = 0, switch DPDT_SEL_P output from register 0x0065[2] */
3517 val8 = rtl8xxxu_read8(priv, REG_LEDCFG2);
3518 val8 &= ~LEDCFG2_DPDT_SELECT;
3519 rtl8xxxu_write8(priv, REG_LEDCFG2, val8);
3520
3521 /* 0x0005[1] = 1 turn off MAC by HW state machine*/
3522 val8 = rtl8xxxu_read8(priv, REG_APS_FSMCO + 1);
3523 val8 |= BIT(1);
3524 rtl8xxxu_write8(priv, REG_APS_FSMCO + 1, val8);
3525
3526 for (count = RTL8XXXU_MAX_REG_POLL; count; count--) {
3527 val8 = rtl8xxxu_read8(priv, REG_APS_FSMCO + 1);
3528 if ((val8 & BIT(1)) == 0)
3529 break;
3530 udelay(10);
3531 }
3532
3533 if (!count) {
3534 dev_warn(&priv->udev->dev, "%s: Disabling MAC timed out\n",
3535 __func__);
3536 ret = -EBUSY;
3537 goto exit;
3538 }
3539
3540 /* 0x0000[5] = 1 analog Ips to digital, 1:isolation */
3541 val8 = rtl8xxxu_read8(priv, REG_SYS_ISO_CTRL);
3542 val8 |= SYS_ISO_ANALOG_IPS;
3543 rtl8xxxu_write8(priv, REG_SYS_ISO_CTRL, val8);
3544
3545 /* 0x0020[0] = 0 disable LDOA12 MACRO block*/
3546 val8 = rtl8xxxu_read8(priv, REG_LDOA15_CTRL);
3547 val8 &= ~LDOA15_ENABLE;
3548 rtl8xxxu_write8(priv, REG_LDOA15_CTRL, val8);
3549
3550exit:
3551 return ret;
3552}
3553
Jes Sorensen6c46ca32016-04-28 15:19:07 -04003554int rtl8xxxu_active_to_lps(struct rtl8xxxu_priv *priv)
Jes Sorensen26f1fad2015-10-14 20:44:51 -04003555{
3556 u8 val8;
3557 u8 val32;
Colin Ian King37ba4b62016-04-14 16:37:07 -04003558 int count, ret = 0;
Jes Sorensen26f1fad2015-10-14 20:44:51 -04003559
3560 rtl8xxxu_write8(priv, REG_TXPAUSE, 0xff);
3561
3562 /*
3563 * Poll - wait for RX packet to complete
3564 */
3565 for (count = RTL8XXXU_MAX_REG_POLL; count; count--) {
3566 val32 = rtl8xxxu_read32(priv, 0x5f8);
3567 if (!val32)
3568 break;
3569 udelay(10);
3570 }
3571
3572 if (!count) {
3573 dev_warn(&priv->udev->dev,
3574 "%s: RX poll timed out (0x05f8)\n", __func__);
3575 ret = -EBUSY;
3576 goto exit;
3577 }
3578
3579 /* Disable CCK and OFDM, clock gated */
3580 val8 = rtl8xxxu_read8(priv, REG_SYS_FUNC);
3581 val8 &= ~SYS_FUNC_BBRSTB;
3582 rtl8xxxu_write8(priv, REG_SYS_FUNC, val8);
3583
3584 udelay(2);
3585
3586 /* Reset baseband */
3587 val8 = rtl8xxxu_read8(priv, REG_SYS_FUNC);
3588 val8 &= ~SYS_FUNC_BB_GLB_RSTN;
3589 rtl8xxxu_write8(priv, REG_SYS_FUNC, val8);
3590
3591 /* Reset MAC TRX */
3592 val8 = rtl8xxxu_read8(priv, REG_CR);
3593 val8 = CR_HCI_TXDMA_ENABLE | CR_HCI_RXDMA_ENABLE;
3594 rtl8xxxu_write8(priv, REG_CR, val8);
3595
3596 /* Reset MAC TRX */
3597 val8 = rtl8xxxu_read8(priv, REG_CR + 1);
3598 val8 &= ~BIT(1); /* CR_SECURITY_ENABLE */
3599 rtl8xxxu_write8(priv, REG_CR + 1, val8);
3600
3601 /* Respond TX OK to scheduler */
3602 val8 = rtl8xxxu_read8(priv, REG_DUAL_TSF_RST);
3603 val8 |= DUAL_TSF_TX_OK;
3604 rtl8xxxu_write8(priv, REG_DUAL_TSF_RST, val8);
3605
3606exit:
3607 return ret;
3608}
3609
Jes Sorensen6c46ca32016-04-28 15:19:07 -04003610void rtl8723a_disabled_to_emu(struct rtl8xxxu_priv *priv)
Jes Sorensen26f1fad2015-10-14 20:44:51 -04003611{
3612 u8 val8;
3613
3614 /* Clear suspend enable and power down enable*/
3615 val8 = rtl8xxxu_read8(priv, REG_APS_FSMCO + 1);
3616 val8 &= ~(BIT(3) | BIT(7));
3617 rtl8xxxu_write8(priv, REG_APS_FSMCO + 1, val8);
3618
3619 /* 0x48[16] = 0 to disable GPIO9 as EXT WAKEUP*/
3620 val8 = rtl8xxxu_read8(priv, REG_GPIO_INTM + 2);
3621 val8 &= ~BIT(0);
3622 rtl8xxxu_write8(priv, REG_GPIO_INTM + 2, val8);
3623
3624 /* 0x04[12:11] = 11 enable WL suspend*/
3625 val8 = rtl8xxxu_read8(priv, REG_APS_FSMCO + 1);
3626 val8 &= ~(BIT(3) | BIT(4));
3627 rtl8xxxu_write8(priv, REG_APS_FSMCO + 1, val8);
3628}
3629
Jes Sorensen26f1fad2015-10-14 20:44:51 -04003630static int rtl8xxxu_emu_to_disabled(struct rtl8xxxu_priv *priv)
3631{
3632 u8 val8;
3633
3634 /* 0x0007[7:0] = 0x20 SOP option to disable BG/MB */
3635 rtl8xxxu_write8(priv, REG_APS_FSMCO + 3, 0x20);
3636
3637 /* 0x04[12:11] = 01 enable WL suspend */
3638 val8 = rtl8xxxu_read8(priv, REG_APS_FSMCO + 1);
3639 val8 &= ~BIT(4);
3640 val8 |= BIT(3);
3641 rtl8xxxu_write8(priv, REG_APS_FSMCO + 1, val8);
3642
3643 val8 = rtl8xxxu_read8(priv, REG_APS_FSMCO + 1);
3644 val8 |= BIT(7);
3645 rtl8xxxu_write8(priv, REG_APS_FSMCO + 1, val8);
3646
3647 /* 0x48[16] = 1 to enable GPIO9 as EXT wakeup */
3648 val8 = rtl8xxxu_read8(priv, REG_GPIO_INTM + 2);
3649 val8 |= BIT(0);
3650 rtl8xxxu_write8(priv, REG_GPIO_INTM + 2, val8);
3651
3652 return 0;
3653}
3654
Jes Sorensen6c46ca32016-04-28 15:19:07 -04003655int rtl8xxxu_flush_fifo(struct rtl8xxxu_priv *priv)
Jes Sorensen430b4542016-02-29 17:05:48 -05003656{
Jes Sorensen145428e2016-02-29 17:05:49 -05003657 struct device *dev = &priv->udev->dev;
Jes Sorensen430b4542016-02-29 17:05:48 -05003658 u32 val32;
3659 int retry, retval;
3660
3661 rtl8xxxu_write8(priv, REG_TXPAUSE, 0xff);
3662
3663 val32 = rtl8xxxu_read32(priv, REG_RXPKT_NUM);
3664 val32 |= RXPKT_NUM_RW_RELEASE_EN;
3665 rtl8xxxu_write32(priv, REG_RXPKT_NUM, val32);
3666
3667 retry = 100;
3668 retval = -EBUSY;
3669
3670 do {
3671 val32 = rtl8xxxu_read32(priv, REG_RXPKT_NUM);
3672 if (val32 & RXPKT_NUM_RXDMA_IDLE) {
3673 retval = 0;
3674 break;
3675 }
3676 } while (retry--);
3677
3678 rtl8xxxu_write16(priv, REG_RQPN_NPQ, 0);
3679 rtl8xxxu_write32(priv, REG_RQPN, 0x80000000);
3680 mdelay(2);
Jes Sorensen145428e2016-02-29 17:05:49 -05003681
3682 if (!retry)
3683 dev_warn(dev, "Failed to flush FIFO\n");
Jes Sorensen430b4542016-02-29 17:05:48 -05003684
3685 return retval;
3686}
3687
Jes Sorensen20e3b2e2016-04-28 15:19:08 -04003688void rtl8xxxu_gen1_usb_quirks(struct rtl8xxxu_priv *priv)
Jes Sorensen747bf232016-04-14 14:59:04 -04003689{
3690 /* Fix USB interface interference issue */
3691 rtl8xxxu_write8(priv, 0xfe40, 0xe0);
3692 rtl8xxxu_write8(priv, 0xfe41, 0x8d);
3693 rtl8xxxu_write8(priv, 0xfe42, 0x80);
3694 /*
3695 * This sets TXDMA_OFFSET_DROP_DATA_EN (bit 9) as well as bits
3696 * 8 and 5, for which I have found no documentation.
3697 */
3698 rtl8xxxu_write32(priv, REG_TXDMA_OFFSET_CHK, 0xfd0320);
3699
3700 /*
3701 * Solve too many protocol error on USB bus.
3702 * Can't do this for 8188/8192 UMC A cut parts
3703 */
3704 if (!(!priv->chip_cut && priv->vendor_umc)) {
3705 rtl8xxxu_write8(priv, 0xfe40, 0xe6);
3706 rtl8xxxu_write8(priv, 0xfe41, 0x94);
3707 rtl8xxxu_write8(priv, 0xfe42, 0x80);
3708
3709 rtl8xxxu_write8(priv, 0xfe40, 0xe0);
3710 rtl8xxxu_write8(priv, 0xfe41, 0x19);
3711 rtl8xxxu_write8(priv, 0xfe42, 0x80);
3712
3713 rtl8xxxu_write8(priv, 0xfe40, 0xe5);
3714 rtl8xxxu_write8(priv, 0xfe41, 0x91);
3715 rtl8xxxu_write8(priv, 0xfe42, 0x80);
3716
3717 rtl8xxxu_write8(priv, 0xfe40, 0xe2);
3718 rtl8xxxu_write8(priv, 0xfe41, 0x81);
3719 rtl8xxxu_write8(priv, 0xfe42, 0x80);
3720 }
3721}
3722
Jes Sorensen599119f2016-04-28 15:19:06 -04003723void rtl8xxxu_gen2_usb_quirks(struct rtl8xxxu_priv *priv)
Jes Sorensen747bf232016-04-14 14:59:04 -04003724{
3725 u32 val32;
3726
3727 val32 = rtl8xxxu_read32(priv, REG_TXDMA_OFFSET_CHK);
3728 val32 |= TXDMA_OFFSET_DROP_DATA_EN;
3729 rtl8xxxu_write32(priv, REG_TXDMA_OFFSET_CHK, val32);
3730}
3731
Jes Sorensen599119f2016-04-28 15:19:06 -04003732void rtl8xxxu_power_off(struct rtl8xxxu_priv *priv)
Jes Sorensen26f1fad2015-10-14 20:44:51 -04003733{
3734 u8 val8;
3735 u16 val16;
3736 u32 val32;
3737
3738 /*
3739 * Workaround for 8188RU LNA power leakage problem.
3740 */
Jes Sorensen8d95c802016-04-14 16:37:11 -04003741 if (priv->rtl_chip == RTL8188R) {
Jes Sorensen26f1fad2015-10-14 20:44:51 -04003742 val32 = rtl8xxxu_read32(priv, REG_FPGA0_XCD_RF_PARM);
3743 val32 |= BIT(1);
3744 rtl8xxxu_write32(priv, REG_FPGA0_XCD_RF_PARM, val32);
3745 }
3746
Jes Sorensen430b4542016-02-29 17:05:48 -05003747 rtl8xxxu_flush_fifo(priv);
3748
Jes Sorensen26f1fad2015-10-14 20:44:51 -04003749 rtl8xxxu_active_to_lps(priv);
3750
3751 /* Turn off RF */
3752 rtl8xxxu_write8(priv, REG_RF_CTRL, 0x00);
3753
3754 /* Reset Firmware if running in RAM */
3755 if (rtl8xxxu_read8(priv, REG_MCU_FW_DL) & MCU_FW_RAM_SEL)
3756 rtl8xxxu_firmware_self_reset(priv);
3757
3758 /* Reset MCU */
3759 val16 = rtl8xxxu_read16(priv, REG_SYS_FUNC);
3760 val16 &= ~SYS_FUNC_CPU_ENABLE;
3761 rtl8xxxu_write16(priv, REG_SYS_FUNC, val16);
3762
3763 /* Reset MCU ready status */
3764 rtl8xxxu_write8(priv, REG_MCU_FW_DL, 0x00);
3765
3766 rtl8xxxu_active_to_emu(priv);
3767 rtl8xxxu_emu_to_disabled(priv);
3768
3769 /* Reset MCU IO Wrapper */
3770 val8 = rtl8xxxu_read8(priv, REG_RSV_CTRL + 1);
3771 val8 &= ~BIT(0);
3772 rtl8xxxu_write8(priv, REG_RSV_CTRL + 1, val8);
3773
3774 val8 = rtl8xxxu_read8(priv, REG_RSV_CTRL + 1);
3775 val8 |= BIT(0);
3776 rtl8xxxu_write8(priv, REG_RSV_CTRL + 1, val8);
3777
3778 /* RSV_CTRL 0x1C[7:0] = 0x0e lock ISO/CLK/Power control register */
3779 rtl8xxxu_write8(priv, REG_RSV_CTRL, 0x0e);
3780}
3781
Jes Sorensena3a5dac2016-02-29 17:05:16 -05003782#ifdef NEED_PS_TDMA
Jes Sorensen3ca7b322016-02-29 17:04:43 -05003783static void rtl8723bu_set_ps_tdma(struct rtl8xxxu_priv *priv,
3784 u8 arg1, u8 arg2, u8 arg3, u8 arg4, u8 arg5)
3785{
3786 struct h2c_cmd h2c;
3787
3788 memset(&h2c, 0, sizeof(struct h2c_cmd));
3789 h2c.b_type_dma.cmd = H2C_8723B_B_TYPE_TDMA;
3790 h2c.b_type_dma.data1 = arg1;
3791 h2c.b_type_dma.data2 = arg2;
3792 h2c.b_type_dma.data3 = arg3;
3793 h2c.b_type_dma.data4 = arg4;
3794 h2c.b_type_dma.data5 = arg5;
3795 rtl8723a_h2c_cmd(priv, &h2c, sizeof(h2c.b_type_dma));
3796}
Jes Sorensena3a5dac2016-02-29 17:05:16 -05003797#endif
Jes Sorensen3ca7b322016-02-29 17:04:43 -05003798
Jes Sorensen599119f2016-04-28 15:19:06 -04003799void rtl8xxxu_gen2_disable_rf(struct rtl8xxxu_priv *priv)
Jes Sorensenfc89a412016-02-29 17:05:46 -05003800{
3801 u32 val32;
3802
Jes Sorensenfc89a412016-02-29 17:05:46 -05003803 val32 = rtl8xxxu_read32(priv, REG_RX_WAIT_CCA);
3804 val32 &= ~(BIT(22) | BIT(23));
3805 rtl8xxxu_write32(priv, REG_RX_WAIT_CCA, val32);
3806}
3807
Jes Sorensen89c2a092016-04-14 14:58:44 -04003808static void rtl8xxxu_old_init_queue_reserved_page(struct rtl8xxxu_priv *priv)
3809{
3810 u8 val8;
3811 u32 val32;
3812
3813 if (priv->ep_tx_normal_queue)
3814 val8 = TX_PAGE_NUM_NORM_PQ;
3815 else
3816 val8 = 0;
3817
3818 rtl8xxxu_write8(priv, REG_RQPN_NPQ, val8);
3819
3820 val32 = (TX_PAGE_NUM_PUBQ << RQPN_PUB_PQ_SHIFT) | RQPN_LOAD;
3821
3822 if (priv->ep_tx_high_queue)
3823 val32 |= (TX_PAGE_NUM_HI_PQ << RQPN_HI_PQ_SHIFT);
3824 if (priv->ep_tx_low_queue)
3825 val32 |= (TX_PAGE_NUM_LO_PQ << RQPN_LO_PQ_SHIFT);
3826
3827 rtl8xxxu_write32(priv, REG_RQPN, val32);
3828}
3829
3830static void rtl8xxxu_init_queue_reserved_page(struct rtl8xxxu_priv *priv)
3831{
3832 struct rtl8xxxu_fileops *fops = priv->fops;
3833 u32 hq, lq, nq, eq, pubq;
3834 u32 val32;
3835
3836 hq = 0;
3837 lq = 0;
3838 nq = 0;
3839 eq = 0;
3840 pubq = 0;
3841
3842 if (priv->ep_tx_high_queue)
3843 hq = fops->page_num_hi;
3844 if (priv->ep_tx_low_queue)
3845 lq = fops->page_num_lo;
3846 if (priv->ep_tx_normal_queue)
3847 nq = fops->page_num_norm;
3848
3849 val32 = (nq << RQPN_NPQ_SHIFT) | (eq << RQPN_EPQ_SHIFT);
3850 rtl8xxxu_write32(priv, REG_RQPN_NPQ, val32);
3851
3852 pubq = fops->total_page_num - hq - lq - nq;
3853
3854 val32 = RQPN_LOAD;
3855 val32 |= (hq << RQPN_HI_PQ_SHIFT);
3856 val32 |= (lq << RQPN_LO_PQ_SHIFT);
3857 val32 |= (pubq << RQPN_PUB_PQ_SHIFT);
3858
3859 rtl8xxxu_write32(priv, REG_RQPN, val32);
3860}
3861
Jes Sorensen26f1fad2015-10-14 20:44:51 -04003862static int rtl8xxxu_init_device(struct ieee80211_hw *hw)
3863{
3864 struct rtl8xxxu_priv *priv = hw->priv;
3865 struct device *dev = &priv->udev->dev;
Jes Sorensen26f1fad2015-10-14 20:44:51 -04003866 bool macpower;
3867 int ret;
3868 u8 val8;
3869 u16 val16;
3870 u32 val32;
3871
3872 /* Check if MAC is already powered on */
3873 val8 = rtl8xxxu_read8(priv, REG_CR);
3874
3875 /*
3876 * Fix 92DU-VC S3 hang with the reason is that secondary mac is not
3877 * initialized. First MAC returns 0xea, second MAC returns 0x00
3878 */
3879 if (val8 == 0xea)
3880 macpower = false;
3881 else
3882 macpower = true;
3883
3884 ret = priv->fops->power_on(priv);
3885 if (ret < 0) {
3886 dev_warn(dev, "%s: Failed power on\n", __func__);
3887 goto exit;
3888 }
3889
Jes Sorensen26f1fad2015-10-14 20:44:51 -04003890 if (!macpower) {
Jes Sorensen89c2a092016-04-14 14:58:44 -04003891 if (priv->fops->total_page_num)
3892 rtl8xxxu_init_queue_reserved_page(priv);
Jes Sorensen59b24da2016-04-14 14:58:43 -04003893 else
Jes Sorensen89c2a092016-04-14 14:58:44 -04003894 rtl8xxxu_old_init_queue_reserved_page(priv);
Jes Sorensen07bb46b2016-02-29 17:04:05 -05003895 }
3896
Jes Sorensen59b24da2016-04-14 14:58:43 -04003897 ret = rtl8xxxu_init_queue_priority(priv);
3898 dev_dbg(dev, "%s: init_queue_priority %i\n", __func__, ret);
3899 if (ret)
3900 goto exit;
3901
3902 /*
3903 * Set RX page boundary
3904 */
Jes Sorensen24e8e7e2016-04-14 14:59:01 -04003905 rtl8xxxu_write16(priv, REG_TRXFF_BNDY + 2, priv->fops->trxff_boundary);
Jes Sorensen59b24da2016-04-14 14:58:43 -04003906
Jes Sorensena47b9d42016-02-29 17:04:06 -05003907 ret = rtl8xxxu_download_firmware(priv);
3908 dev_dbg(dev, "%s: download_fiwmare %i\n", __func__, ret);
3909 if (ret)
3910 goto exit;
3911 ret = rtl8xxxu_start_firmware(priv);
3912 dev_dbg(dev, "%s: start_fiwmare %i\n", __func__, ret);
3913 if (ret)
3914 goto exit;
3915
Jes Sorensenf0d9f5e2016-02-29 17:04:16 -05003916 if (priv->fops->phy_init_antenna_selection)
3917 priv->fops->phy_init_antenna_selection(priv);
3918
Jes Sorensenc606e662016-04-07 14:19:16 -04003919 ret = rtl8xxxu_init_mac(priv);
Jes Sorensenb7dd8ff2016-02-29 17:04:17 -05003920
Jes Sorensen26f1fad2015-10-14 20:44:51 -04003921 dev_dbg(dev, "%s: init_mac %i\n", __func__, ret);
3922 if (ret)
3923 goto exit;
3924
3925 ret = rtl8xxxu_init_phy_bb(priv);
3926 dev_dbg(dev, "%s: init_phy_bb %i\n", __func__, ret);
3927 if (ret)
3928 goto exit;
3929
Jes Sorensen4062b8f2016-04-14 16:37:08 -04003930 ret = priv->fops->init_phy_rf(priv);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04003931 if (ret)
3932 goto exit;
3933
Jes Sorensenc1578632016-04-14 14:58:42 -04003934 /* RFSW Control - clear bit 14 ?? */
Jes Sorensenb8169012016-04-14 14:58:47 -04003935 if (priv->rtl_chip != RTL8723B && priv->rtl_chip != RTL8192E)
Jes Sorensenc1578632016-04-14 14:58:42 -04003936 rtl8xxxu_write32(priv, REG_FPGA0_TX_INFO, 0x00000003);
Jes Sorensen31133da2016-04-14 14:59:05 -04003937
3938 val32 = FPGA0_RF_TRSW | FPGA0_RF_TRSWB | FPGA0_RF_ANTSW |
Jes Sorensencabb5502016-04-14 16:37:17 -04003939 FPGA0_RF_ANTSWB |
3940 ((FPGA0_RF_ANTSW | FPGA0_RF_ANTSWB) << FPGA0_RF_BD_CTRL_SHIFT);
3941 if (!priv->no_pape) {
3942 val32 |= (FPGA0_RF_PAPE |
3943 (FPGA0_RF_PAPE << FPGA0_RF_BD_CTRL_SHIFT));
3944 }
Jes Sorensenc1578632016-04-14 14:58:42 -04003945 rtl8xxxu_write32(priv, REG_FPGA0_XAB_RF_SW_CTRL, val32);
Jes Sorensencabb5502016-04-14 16:37:17 -04003946
Jes Sorensenc1578632016-04-14 14:58:42 -04003947 /* 0x860[6:5]= 00 - why? - this sets antenna B */
3948 if (priv->rtl_chip != RTL8192E)
3949 rtl8xxxu_write32(priv, REG_FPGA0_XA_RF_INT_OE, 0x66f60210);
3950
Jes Sorensenf2a41632016-02-29 17:05:09 -05003951 if (!macpower) {
Jes Sorensen1f1b20f2016-02-29 17:05:00 -05003952 /*
3953 * Set TX buffer boundary
3954 */
Jes Sorensen80805aa2016-04-07 14:19:18 -04003955 if (priv->rtl_chip == RTL8192E)
3956 val8 = TX_TOTAL_PAGE_NUM_8192E + 1;
3957 else
3958 val8 = TX_TOTAL_PAGE_NUM + 1;
Jes Sorensen1f1b20f2016-02-29 17:05:00 -05003959
Jes Sorensenba17d822016-03-31 17:08:39 -04003960 if (priv->rtl_chip == RTL8723B)
Jes Sorensen1f1b20f2016-02-29 17:05:00 -05003961 val8 -= 1;
3962
3963 rtl8xxxu_write8(priv, REG_TXPKTBUF_BCNQ_BDNY, val8);
3964 rtl8xxxu_write8(priv, REG_TXPKTBUF_MGQ_BDNY, val8);
3965 rtl8xxxu_write8(priv, REG_TXPKTBUF_WMAC_LBK_BF_HD, val8);
3966 rtl8xxxu_write8(priv, REG_TRXFF_BNDY, val8);
3967 rtl8xxxu_write8(priv, REG_TDECTRL + 1, val8);
3968 }
3969
Jes Sorensen26f1fad2015-10-14 20:44:51 -04003970 /*
Jes Sorensen9b323ee2016-04-14 14:59:03 -04003971 * The vendor drivers set PBP for all devices, except 8192e.
3972 * There is no explanation for this in any of the sources.
Jes Sorensen26f1fad2015-10-14 20:44:51 -04003973 */
Jes Sorensen9b323ee2016-04-14 14:59:03 -04003974 val8 = (priv->fops->pbp_rx << PBP_PAGE_SIZE_RX_SHIFT) |
3975 (priv->fops->pbp_tx << PBP_PAGE_SIZE_TX_SHIFT);
Jes Sorensen2e7c7b32016-04-14 14:58:46 -04003976 if (priv->rtl_chip != RTL8192E)
3977 rtl8xxxu_write8(priv, REG_PBP, val8);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04003978
Jes Sorensen59b24da2016-04-14 14:58:43 -04003979 dev_dbg(dev, "%s: macpower %i\n", __func__, macpower);
3980 if (!macpower) {
3981 ret = priv->fops->llt_init(priv, TX_TOTAL_PAGE_NUM);
3982 if (ret) {
3983 dev_warn(dev, "%s: LLT table init failed\n", __func__);
3984 goto exit;
3985 }
3986
3987 /*
Jes Sorensen0486e802016-04-14 14:58:45 -04003988 * Chip specific quirks
3989 */
Jes Sorensen747bf232016-04-14 14:59:04 -04003990 priv->fops->usb_quirks(priv);
Jes Sorensen0486e802016-04-14 14:58:45 -04003991
3992 /*
Jes Sorensen59b24da2016-04-14 14:58:43 -04003993 * Presumably this is for 8188EU as well
3994 * Enable TX report and TX report timer
3995 */
3996 if (priv->rtl_chip == RTL8723B) {
3997 val8 = rtl8xxxu_read8(priv, REG_TX_REPORT_CTRL);
3998 val8 |= TX_REPORT_CTRL_TIMER_ENABLE;
3999 rtl8xxxu_write8(priv, REG_TX_REPORT_CTRL, val8);
4000 /* Set MAX RPT MACID */
4001 rtl8xxxu_write8(priv, REG_TX_REPORT_CTRL + 1, 0x02);
4002 /* TX report Timer. Unit: 32us */
4003 rtl8xxxu_write16(priv, REG_TX_REPORT_TIME, 0xcdf0);
4004
4005 /* tmp ps ? */
4006 val8 = rtl8xxxu_read8(priv, 0xa3);
4007 val8 &= 0xf8;
4008 rtl8xxxu_write8(priv, 0xa3, val8);
4009 }
4010 }
4011
Jes Sorensen26f1fad2015-10-14 20:44:51 -04004012 /*
4013 * Unit in 8 bytes, not obvious what it is used for
4014 */
4015 rtl8xxxu_write8(priv, REG_RX_DRVINFO_SZ, 4);
4016
Jes Sorensen57e5e2e2016-04-07 14:19:27 -04004017 if (priv->rtl_chip == RTL8192E) {
4018 rtl8xxxu_write32(priv, REG_HIMR0, 0x00);
4019 rtl8xxxu_write32(priv, REG_HIMR1, 0x00);
4020 } else {
4021 /*
4022 * Enable all interrupts - not obvious USB needs to do this
4023 */
4024 rtl8xxxu_write32(priv, REG_HISR, 0xffffffff);
4025 rtl8xxxu_write32(priv, REG_HIMR, 0xffffffff);
4026 }
Jes Sorensen26f1fad2015-10-14 20:44:51 -04004027
4028 rtl8xxxu_set_mac(priv);
4029 rtl8xxxu_set_linktype(priv, NL80211_IFTYPE_STATION);
4030
4031 /*
4032 * Configure initial WMAC settings
4033 */
4034 val32 = RCR_ACCEPT_PHYS_MATCH | RCR_ACCEPT_MCAST | RCR_ACCEPT_BCAST |
Jes Sorensen26f1fad2015-10-14 20:44:51 -04004035 RCR_ACCEPT_MGMT_FRAME | RCR_HTC_LOC_CTRL |
4036 RCR_APPEND_PHYSTAT | RCR_APPEND_ICV | RCR_APPEND_MIC;
4037 rtl8xxxu_write32(priv, REG_RCR, val32);
4038
4039 /*
4040 * Accept all multicast
4041 */
4042 rtl8xxxu_write32(priv, REG_MAR, 0xffffffff);
4043 rtl8xxxu_write32(priv, REG_MAR + 4, 0xffffffff);
4044
4045 /*
4046 * Init adaptive controls
4047 */
4048 val32 = rtl8xxxu_read32(priv, REG_RESPONSE_RATE_SET);
4049 val32 &= ~RESPONSE_RATE_BITMAP_ALL;
4050 val32 |= RESPONSE_RATE_RRSR_CCK_ONLY_1M;
4051 rtl8xxxu_write32(priv, REG_RESPONSE_RATE_SET, val32);
4052
4053 /* CCK = 0x0a, OFDM = 0x10 */
4054 rtl8xxxu_set_spec_sifs(priv, 0x10, 0x10);
4055 rtl8xxxu_set_retry(priv, 0x30, 0x30);
4056 rtl8xxxu_set_spec_sifs(priv, 0x0a, 0x10);
4057
4058 /*
4059 * Init EDCA
4060 */
4061 rtl8xxxu_write16(priv, REG_MAC_SPEC_SIFS, 0x100a);
4062
4063 /* Set CCK SIFS */
4064 rtl8xxxu_write16(priv, REG_SIFS_CCK, 0x100a);
4065
4066 /* Set OFDM SIFS */
4067 rtl8xxxu_write16(priv, REG_SIFS_OFDM, 0x100a);
4068
4069 /* TXOP */
4070 rtl8xxxu_write32(priv, REG_EDCA_BE_PARAM, 0x005ea42b);
4071 rtl8xxxu_write32(priv, REG_EDCA_BK_PARAM, 0x0000a44f);
4072 rtl8xxxu_write32(priv, REG_EDCA_VI_PARAM, 0x005ea324);
4073 rtl8xxxu_write32(priv, REG_EDCA_VO_PARAM, 0x002fa226);
4074
4075 /* Set data auto rate fallback retry count */
4076 rtl8xxxu_write32(priv, REG_DARFRC, 0x00000000);
4077 rtl8xxxu_write32(priv, REG_DARFRC + 4, 0x10080404);
4078 rtl8xxxu_write32(priv, REG_RARFRC, 0x04030201);
4079 rtl8xxxu_write32(priv, REG_RARFRC + 4, 0x08070605);
4080
4081 val8 = rtl8xxxu_read8(priv, REG_FWHW_TXQ_CTRL);
4082 val8 |= FWHW_TXQ_CTRL_AMPDU_RETRY;
4083 rtl8xxxu_write8(priv, REG_FWHW_TXQ_CTRL, val8);
4084
4085 /* Set ACK timeout */
4086 rtl8xxxu_write8(priv, REG_ACKTO, 0x40);
4087
4088 /*
4089 * Initialize beacon parameters
4090 */
4091 val16 = BEACON_DISABLE_TSF_UPDATE | (BEACON_DISABLE_TSF_UPDATE << 8);
4092 rtl8xxxu_write16(priv, REG_BEACON_CTRL, val16);
4093 rtl8xxxu_write16(priv, REG_TBTT_PROHIBIT, 0x6404);
4094 rtl8xxxu_write8(priv, REG_DRIVER_EARLY_INT, DRIVER_EARLY_INT_TIME);
4095 rtl8xxxu_write8(priv, REG_BEACON_DMA_TIME, BEACON_DMA_ATIME_INT_TIME);
4096 rtl8xxxu_write16(priv, REG_BEACON_TCFG, 0x660F);
4097
4098 /*
Jes Sorensenc3690602016-02-29 17:05:03 -05004099 * Initialize burst parameters
4100 */
Jes Sorensenba17d822016-03-31 17:08:39 -04004101 if (priv->rtl_chip == RTL8723B) {
Jes Sorensenc3690602016-02-29 17:05:03 -05004102 /*
4103 * For USB high speed set 512B packets
4104 */
4105 val8 = rtl8xxxu_read8(priv, REG_RXDMA_PRO_8723B);
4106 val8 &= ~(BIT(4) | BIT(5));
4107 val8 |= BIT(4);
4108 val8 |= BIT(1) | BIT(2) | BIT(3);
4109 rtl8xxxu_write8(priv, REG_RXDMA_PRO_8723B, val8);
4110
4111 /*
4112 * For USB high speed set 512B packets
4113 */
4114 val8 = rtl8xxxu_read8(priv, REG_HT_SINGLE_AMPDU_8723B);
4115 val8 |= BIT(7);
4116 rtl8xxxu_write8(priv, REG_HT_SINGLE_AMPDU_8723B, val8);
4117
4118 rtl8xxxu_write16(priv, REG_MAX_AGGR_NUM, 0x0c14);
4119 rtl8xxxu_write8(priv, REG_AMPDU_MAX_TIME_8723B, 0x5e);
4120 rtl8xxxu_write32(priv, REG_AGGLEN_LMT, 0xffffffff);
4121 rtl8xxxu_write8(priv, REG_RX_PKT_LIMIT, 0x18);
4122 rtl8xxxu_write8(priv, REG_PIFS, 0x00);
4123 rtl8xxxu_write8(priv, REG_USTIME_TSF_8723B, 0x50);
4124 rtl8xxxu_write8(priv, REG_USTIME_EDCA, 0x50);
4125
4126 val8 = rtl8xxxu_read8(priv, REG_RSV_CTRL);
4127 val8 |= BIT(5) | BIT(6);
4128 rtl8xxxu_write8(priv, REG_RSV_CTRL, val8);
4129 }
4130
Jes Sorensen3e88ca42016-02-29 17:05:08 -05004131 if (priv->fops->init_aggregation)
4132 priv->fops->init_aggregation(priv);
4133
Jes Sorensenc3690602016-02-29 17:05:03 -05004134 /*
Jes Sorensen26f1fad2015-10-14 20:44:51 -04004135 * Enable CCK and OFDM block
4136 */
4137 val32 = rtl8xxxu_read32(priv, REG_FPGA0_RF_MODE);
4138 val32 |= (FPGA_RF_MODE_CCK | FPGA_RF_MODE_OFDM);
4139 rtl8xxxu_write32(priv, REG_FPGA0_RF_MODE, val32);
4140
4141 /*
4142 * Invalidate all CAM entries - bit 30 is undocumented
4143 */
4144 rtl8xxxu_write32(priv, REG_CAM_CMD, CAM_CMD_POLLING | BIT(30));
4145
4146 /*
4147 * Start out with default power levels for channel 6, 20MHz
4148 */
Jes Sorensene796dab2016-02-29 17:05:19 -05004149 priv->fops->set_tx_power(priv, 1, false);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04004150
4151 /* Let the 8051 take control of antenna setting */
Jes Sorensen5bdb6b02016-04-14 14:58:48 -04004152 if (priv->rtl_chip != RTL8192E) {
4153 val8 = rtl8xxxu_read8(priv, REG_LEDCFG2);
4154 val8 |= LEDCFG2_DPDT_SELECT;
4155 rtl8xxxu_write8(priv, REG_LEDCFG2, val8);
4156 }
Jes Sorensen26f1fad2015-10-14 20:44:51 -04004157
4158 rtl8xxxu_write8(priv, REG_HWSEQ_CTRL, 0xff);
4159
4160 /* Disable BAR - not sure if this has any effect on USB */
4161 rtl8xxxu_write32(priv, REG_BAR_MODE_CTRL, 0x0201ffff);
4162
4163 rtl8xxxu_write16(priv, REG_FAST_EDCA_CTRL, 0);
4164
Jes Sorensen9c79bf92016-02-29 17:05:10 -05004165 if (priv->fops->init_statistics)
4166 priv->fops->init_statistics(priv);
4167
Jes Sorensenb052b7f2016-04-07 14:19:30 -04004168 if (priv->rtl_chip == RTL8192E) {
4169 /*
4170 * 0x4c6[3] 1: RTS BW = Data BW
4171 * 0: RTS BW depends on CCA / secondary CCA result.
4172 */
4173 val8 = rtl8xxxu_read8(priv, REG_QUEUE_CTRL);
4174 val8 &= ~BIT(3);
4175 rtl8xxxu_write8(priv, REG_QUEUE_CTRL, val8);
4176 /*
4177 * Reset USB mode switch setting
4178 */
4179 rtl8xxxu_write8(priv, REG_ACLK_MON, 0x00);
4180 }
4181
Jes Sorensenfa0f2d42016-02-29 17:04:37 -05004182 rtl8723a_phy_lc_calibrate(priv);
4183
Jes Sorensene1547c52016-02-29 17:04:35 -05004184 priv->fops->phy_iq_calibrate(priv);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04004185
4186 /*
4187 * This should enable thermal meter
4188 */
Jes Sorensen55c0b6a2016-04-14 14:58:50 -04004189 if (priv->fops->tx_desc_size == sizeof(struct rtl8xxxu_txdesc40))
Jes Sorensen72143b92016-02-29 17:05:25 -05004190 rtl8xxxu_write_rfreg(priv,
4191 RF_A, RF6052_REG_T_METER_8723B, 0x37cf8);
4192 else
4193 rtl8xxxu_write_rfreg(priv, RF_A, RF6052_REG_T_METER, 0x60);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04004194
Jes Sorensen26f1fad2015-10-14 20:44:51 -04004195 /* Set NAV_UPPER to 30000us */
4196 val8 = ((30000 + NAV_UPPER_UNIT - 1) / NAV_UPPER_UNIT);
4197 rtl8xxxu_write8(priv, REG_NAV_UPPER, val8);
4198
Jes Sorensenba17d822016-03-31 17:08:39 -04004199 if (priv->rtl_chip == RTL8723A) {
Jes Sorensen4042e612016-02-03 13:40:01 -05004200 /*
4201 * 2011/03/09 MH debug only, UMC-B cut pass 2500 S5 test,
4202 * but we need to find root cause.
4203 * This is 8723au only.
4204 */
4205 val32 = rtl8xxxu_read32(priv, REG_FPGA0_RF_MODE);
4206 if ((val32 & 0xff000000) != 0x83000000) {
4207 val32 |= FPGA_RF_MODE_CCK;
4208 rtl8xxxu_write32(priv, REG_FPGA0_RF_MODE, val32);
4209 }
Jes Sorensen3021e512016-04-07 14:19:28 -04004210 } else if (priv->rtl_chip == RTL8192E) {
4211 rtl8xxxu_write8(priv, REG_USB_HRPWM, 0x00);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04004212 }
4213
4214 val32 = rtl8xxxu_read32(priv, REG_FWHW_TXQ_CTRL);
4215 val32 |= FWHW_TXQ_CTRL_XMIT_MGMT_ACK;
4216 /* ack for xmit mgmt frames. */
4217 rtl8xxxu_write32(priv, REG_FWHW_TXQ_CTRL, val32);
4218
Jes Sorensene1394fe2016-04-07 14:19:29 -04004219 if (priv->rtl_chip == RTL8192E) {
4220 /*
4221 * Fix LDPC rx hang issue.
4222 */
4223 val32 = rtl8xxxu_read32(priv, REG_AFE_MISC);
4224 rtl8xxxu_write8(priv, REG_8192E_LDOV12_CTRL, 0x75);
4225 val32 &= 0xfff00fff;
4226 val32 |= 0x0007e000;
Jes Sorensen46b37832016-04-14 14:59:06 -04004227 rtl8xxxu_write32(priv, REG_AFE_MISC, val32);
Jes Sorensene1394fe2016-04-07 14:19:29 -04004228 }
Jes Sorensen26f1fad2015-10-14 20:44:51 -04004229exit:
4230 return ret;
4231}
4232
Jes Sorensen26f1fad2015-10-14 20:44:51 -04004233static void rtl8xxxu_cam_write(struct rtl8xxxu_priv *priv,
4234 struct ieee80211_key_conf *key, const u8 *mac)
4235{
4236 u32 cmd, val32, addr, ctrl;
4237 int j, i, tmp_debug;
4238
4239 tmp_debug = rtl8xxxu_debug;
4240 if (rtl8xxxu_debug & RTL8XXXU_DEBUG_KEY)
4241 rtl8xxxu_debug |= RTL8XXXU_DEBUG_REG_WRITE;
4242
4243 /*
4244 * This is a bit of a hack - the lower bits of the cipher
4245 * suite selector happens to match the cipher index in the CAM
4246 */
4247 addr = key->keyidx << CAM_CMD_KEY_SHIFT;
4248 ctrl = (key->cipher & 0x0f) << 2 | key->keyidx | CAM_WRITE_VALID;
4249
4250 for (j = 5; j >= 0; j--) {
4251 switch (j) {
4252 case 0:
4253 val32 = ctrl | (mac[0] << 16) | (mac[1] << 24);
4254 break;
4255 case 1:
4256 val32 = mac[2] | (mac[3] << 8) |
4257 (mac[4] << 16) | (mac[5] << 24);
4258 break;
4259 default:
4260 i = (j - 2) << 2;
4261 val32 = key->key[i] | (key->key[i + 1] << 8) |
4262 key->key[i + 2] << 16 | key->key[i + 3] << 24;
4263 break;
4264 }
4265
4266 rtl8xxxu_write32(priv, REG_CAM_WRITE, val32);
4267 cmd = CAM_CMD_POLLING | CAM_CMD_WRITE | (addr + j);
4268 rtl8xxxu_write32(priv, REG_CAM_CMD, cmd);
4269 udelay(100);
4270 }
4271
4272 rtl8xxxu_debug = tmp_debug;
4273}
4274
4275static void rtl8xxxu_sw_scan_start(struct ieee80211_hw *hw,
Jes Sorensen56e43742016-02-03 13:39:50 -05004276 struct ieee80211_vif *vif, const u8 *mac)
Jes Sorensen26f1fad2015-10-14 20:44:51 -04004277{
4278 struct rtl8xxxu_priv *priv = hw->priv;
4279 u8 val8;
4280
4281 val8 = rtl8xxxu_read8(priv, REG_BEACON_CTRL);
4282 val8 |= BEACON_DISABLE_TSF_UPDATE;
4283 rtl8xxxu_write8(priv, REG_BEACON_CTRL, val8);
4284}
4285
4286static void rtl8xxxu_sw_scan_complete(struct ieee80211_hw *hw,
4287 struct ieee80211_vif *vif)
4288{
4289 struct rtl8xxxu_priv *priv = hw->priv;
4290 u8 val8;
4291
4292 val8 = rtl8xxxu_read8(priv, REG_BEACON_CTRL);
4293 val8 &= ~BEACON_DISABLE_TSF_UPDATE;
4294 rtl8xxxu_write8(priv, REG_BEACON_CTRL, val8);
4295}
4296
Jes Sorensen20e3b2e2016-04-28 15:19:08 -04004297void rtl8xxxu_update_rate_mask(struct rtl8xxxu_priv *priv, u32 ramask, int sgi)
Jes Sorensen26f1fad2015-10-14 20:44:51 -04004298{
4299 struct h2c_cmd h2c;
4300
Jes Sorensenf653e692016-02-29 17:05:38 -05004301 memset(&h2c, 0, sizeof(struct h2c_cmd));
4302
Jes Sorensen26f1fad2015-10-14 20:44:51 -04004303 h2c.ramask.cmd = H2C_SET_RATE_MASK;
4304 h2c.ramask.mask_lo = cpu_to_le16(ramask & 0xffff);
4305 h2c.ramask.mask_hi = cpu_to_le16(ramask >> 16);
4306
4307 h2c.ramask.arg = 0x80;
4308 if (sgi)
4309 h2c.ramask.arg |= 0x20;
4310
Jes Sorensen7ff8c1a2016-02-29 17:04:32 -05004311 dev_dbg(&priv->udev->dev, "%s: rate mask %08x, arg %02x, size %zi\n",
Jes Sorensen8da91572016-02-29 17:04:29 -05004312 __func__, ramask, h2c.ramask.arg, sizeof(h2c.ramask));
4313 rtl8723a_h2c_cmd(priv, &h2c, sizeof(h2c.ramask));
Jes Sorensen26f1fad2015-10-14 20:44:51 -04004314}
4315
Jes Sorensen599119f2016-04-28 15:19:06 -04004316void rtl8xxxu_gen2_update_rate_mask(struct rtl8xxxu_priv *priv,
4317 u32 ramask, int sgi)
Jes Sorensenf653e692016-02-29 17:05:38 -05004318{
4319 struct h2c_cmd h2c;
4320 u8 bw = 0;
4321
4322 memset(&h2c, 0, sizeof(struct h2c_cmd));
4323
4324 h2c.b_macid_cfg.cmd = H2C_8723B_MACID_CFG_RAID;
4325 h2c.b_macid_cfg.ramask0 = ramask & 0xff;
4326 h2c.b_macid_cfg.ramask1 = (ramask >> 8) & 0xff;
4327 h2c.b_macid_cfg.ramask2 = (ramask >> 16) & 0xff;
4328 h2c.b_macid_cfg.ramask3 = (ramask >> 24) & 0xff;
4329
4330 h2c.ramask.arg = 0x80;
4331 h2c.b_macid_cfg.data1 = 0;
4332 if (sgi)
4333 h2c.b_macid_cfg.data1 |= BIT(7);
4334
4335 h2c.b_macid_cfg.data2 = bw;
4336
4337 dev_dbg(&priv->udev->dev, "%s: rate mask %08x, arg %02x, size %zi\n",
4338 __func__, ramask, h2c.ramask.arg, sizeof(h2c.b_macid_cfg));
4339 rtl8723a_h2c_cmd(priv, &h2c, sizeof(h2c.b_macid_cfg));
4340}
4341
Jes Sorensen20e3b2e2016-04-28 15:19:08 -04004342void rtl8xxxu_gen1_report_connect(struct rtl8xxxu_priv *priv,
4343 u8 macid, bool connect)
Jes Sorensen7d794ea2016-02-29 17:05:39 -05004344{
4345 struct h2c_cmd h2c;
4346
4347 memset(&h2c, 0, sizeof(struct h2c_cmd));
4348
4349 h2c.joinbss.cmd = H2C_JOIN_BSS_REPORT;
4350
4351 if (connect)
4352 h2c.joinbss.data = H2C_JOIN_BSS_CONNECT;
4353 else
4354 h2c.joinbss.data = H2C_JOIN_BSS_DISCONNECT;
4355
4356 rtl8723a_h2c_cmd(priv, &h2c, sizeof(h2c.joinbss));
4357}
4358
Jes Sorensen599119f2016-04-28 15:19:06 -04004359void rtl8xxxu_gen2_report_connect(struct rtl8xxxu_priv *priv,
4360 u8 macid, bool connect)
Jes Sorensen7d794ea2016-02-29 17:05:39 -05004361{
4362 struct h2c_cmd h2c;
4363
4364 memset(&h2c, 0, sizeof(struct h2c_cmd));
4365
4366 h2c.media_status_rpt.cmd = H2C_8723B_MEDIA_STATUS_RPT;
4367 if (connect)
4368 h2c.media_status_rpt.parm |= BIT(0);
4369 else
4370 h2c.media_status_rpt.parm &= ~BIT(0);
4371
4372 rtl8723a_h2c_cmd(priv, &h2c, sizeof(h2c.media_status_rpt));
4373}
4374
Jes Sorensen26f1fad2015-10-14 20:44:51 -04004375static void rtl8xxxu_set_basic_rates(struct rtl8xxxu_priv *priv, u32 rate_cfg)
4376{
4377 u32 val32;
4378 u8 rate_idx = 0;
4379
4380 rate_cfg &= RESPONSE_RATE_BITMAP_ALL;
4381
4382 val32 = rtl8xxxu_read32(priv, REG_RESPONSE_RATE_SET);
4383 val32 &= ~RESPONSE_RATE_BITMAP_ALL;
4384 val32 |= rate_cfg;
4385 rtl8xxxu_write32(priv, REG_RESPONSE_RATE_SET, val32);
4386
4387 dev_dbg(&priv->udev->dev, "%s: rates %08x\n", __func__, rate_cfg);
4388
4389 while (rate_cfg) {
4390 rate_cfg = (rate_cfg >> 1);
4391 rate_idx++;
4392 }
4393 rtl8xxxu_write8(priv, REG_INIRTS_RATE_SEL, rate_idx);
4394}
4395
4396static void
4397rtl8xxxu_bss_info_changed(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
4398 struct ieee80211_bss_conf *bss_conf, u32 changed)
4399{
4400 struct rtl8xxxu_priv *priv = hw->priv;
4401 struct device *dev = &priv->udev->dev;
4402 struct ieee80211_sta *sta;
4403 u32 val32;
4404 u8 val8;
4405
4406 if (changed & BSS_CHANGED_ASSOC) {
Jes Sorensen26f1fad2015-10-14 20:44:51 -04004407 dev_dbg(dev, "Changed ASSOC: %i!\n", bss_conf->assoc);
4408
Jes Sorensen26f1fad2015-10-14 20:44:51 -04004409 rtl8xxxu_set_linktype(priv, vif->type);
4410
4411 if (bss_conf->assoc) {
4412 u32 ramask;
4413 int sgi = 0;
4414
4415 rcu_read_lock();
4416 sta = ieee80211_find_sta(vif, bss_conf->bssid);
4417 if (!sta) {
4418 dev_info(dev, "%s: ASSOC no sta found\n",
4419 __func__);
4420 rcu_read_unlock();
4421 goto error;
4422 }
4423
4424 if (sta->ht_cap.ht_supported)
4425 dev_info(dev, "%s: HT supported\n", __func__);
4426 if (sta->vht_cap.vht_supported)
4427 dev_info(dev, "%s: VHT supported\n", __func__);
4428
4429 /* TODO: Set bits 28-31 for rate adaptive id */
4430 ramask = (sta->supp_rates[0] & 0xfff) |
4431 sta->ht_cap.mcs.rx_mask[0] << 12 |
4432 sta->ht_cap.mcs.rx_mask[1] << 20;
4433 if (sta->ht_cap.cap &
4434 (IEEE80211_HT_CAP_SGI_40 | IEEE80211_HT_CAP_SGI_20))
4435 sgi = 1;
4436 rcu_read_unlock();
4437
Jes Sorensenf653e692016-02-29 17:05:38 -05004438 priv->fops->update_rate_mask(priv, ramask, sgi);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04004439
Jes Sorensen26f1fad2015-10-14 20:44:51 -04004440 rtl8xxxu_write8(priv, REG_BCN_MAX_ERR, 0xff);
4441
Jes Sorensen97db5a82016-04-28 15:19:10 -04004442 rtl8xxxu_stop_tx_beacon(priv);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04004443
4444 /* joinbss sequence */
4445 rtl8xxxu_write16(priv, REG_BCN_PSR_RPT,
4446 0xc000 | bss_conf->aid);
4447
Jes Sorensen7d794ea2016-02-29 17:05:39 -05004448 priv->fops->report_connect(priv, 0, true);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04004449 } else {
Jes Sorensen26f1fad2015-10-14 20:44:51 -04004450 val8 = rtl8xxxu_read8(priv, REG_BEACON_CTRL);
4451 val8 |= BEACON_DISABLE_TSF_UPDATE;
4452 rtl8xxxu_write8(priv, REG_BEACON_CTRL, val8);
4453
Jes Sorensen7d794ea2016-02-29 17:05:39 -05004454 priv->fops->report_connect(priv, 0, false);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04004455 }
Jes Sorensen26f1fad2015-10-14 20:44:51 -04004456 }
4457
4458 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
4459 dev_dbg(dev, "Changed ERP_PREAMBLE: Use short preamble %i\n",
4460 bss_conf->use_short_preamble);
4461 val32 = rtl8xxxu_read32(priv, REG_RESPONSE_RATE_SET);
4462 if (bss_conf->use_short_preamble)
4463 val32 |= RSR_ACK_SHORT_PREAMBLE;
4464 else
4465 val32 &= ~RSR_ACK_SHORT_PREAMBLE;
4466 rtl8xxxu_write32(priv, REG_RESPONSE_RATE_SET, val32);
4467 }
4468
4469 if (changed & BSS_CHANGED_ERP_SLOT) {
4470 dev_dbg(dev, "Changed ERP_SLOT: short_slot_time %i\n",
4471 bss_conf->use_short_slot);
4472
4473 if (bss_conf->use_short_slot)
4474 val8 = 9;
4475 else
4476 val8 = 20;
4477 rtl8xxxu_write8(priv, REG_SLOT, val8);
4478 }
4479
4480 if (changed & BSS_CHANGED_BSSID) {
4481 dev_dbg(dev, "Changed BSSID!\n");
4482 rtl8xxxu_set_bssid(priv, bss_conf->bssid);
4483 }
4484
4485 if (changed & BSS_CHANGED_BASIC_RATES) {
4486 dev_dbg(dev, "Changed BASIC_RATES!\n");
4487 rtl8xxxu_set_basic_rates(priv, bss_conf->basic_rates);
4488 }
4489error:
4490 return;
4491}
4492
4493static u32 rtl8xxxu_80211_to_rtl_queue(u32 queue)
4494{
4495 u32 rtlqueue;
4496
4497 switch (queue) {
4498 case IEEE80211_AC_VO:
4499 rtlqueue = TXDESC_QUEUE_VO;
4500 break;
4501 case IEEE80211_AC_VI:
4502 rtlqueue = TXDESC_QUEUE_VI;
4503 break;
4504 case IEEE80211_AC_BE:
4505 rtlqueue = TXDESC_QUEUE_BE;
4506 break;
4507 case IEEE80211_AC_BK:
4508 rtlqueue = TXDESC_QUEUE_BK;
4509 break;
4510 default:
4511 rtlqueue = TXDESC_QUEUE_BE;
4512 }
4513
4514 return rtlqueue;
4515}
4516
4517static u32 rtl8xxxu_queue_select(struct ieee80211_hw *hw, struct sk_buff *skb)
4518{
4519 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
4520 u32 queue;
4521
4522 if (ieee80211_is_mgmt(hdr->frame_control))
4523 queue = TXDESC_QUEUE_MGNT;
4524 else
4525 queue = rtl8xxxu_80211_to_rtl_queue(skb_get_queue_mapping(skb));
4526
4527 return queue;
4528}
4529
Jes Sorensen179e1742016-02-29 17:05:27 -05004530/*
4531 * Despite newer chips 8723b/8812/8821 having a larger TX descriptor
4532 * format. The descriptor checksum is still only calculated over the
4533 * initial 32 bytes of the descriptor!
4534 */
Jes Sorensendbb28962016-03-31 17:08:33 -04004535static void rtl8xxxu_calc_tx_desc_csum(struct rtl8xxxu_txdesc32 *tx_desc)
Jes Sorensen26f1fad2015-10-14 20:44:51 -04004536{
4537 __le16 *ptr = (__le16 *)tx_desc;
4538 u16 csum = 0;
4539 int i;
4540
4541 /*
4542 * Clear csum field before calculation, as the csum field is
4543 * in the middle of the struct.
4544 */
4545 tx_desc->csum = cpu_to_le16(0);
4546
Jes Sorensendbb28962016-03-31 17:08:33 -04004547 for (i = 0; i < (sizeof(struct rtl8xxxu_txdesc32) / sizeof(u16)); i++)
Jes Sorensen26f1fad2015-10-14 20:44:51 -04004548 csum = csum ^ le16_to_cpu(ptr[i]);
4549
4550 tx_desc->csum |= cpu_to_le16(csum);
4551}
4552
4553static void rtl8xxxu_free_tx_resources(struct rtl8xxxu_priv *priv)
4554{
4555 struct rtl8xxxu_tx_urb *tx_urb, *tmp;
4556 unsigned long flags;
4557
4558 spin_lock_irqsave(&priv->tx_urb_lock, flags);
4559 list_for_each_entry_safe(tx_urb, tmp, &priv->tx_urb_free_list, list) {
4560 list_del(&tx_urb->list);
4561 priv->tx_urb_free_count--;
4562 usb_free_urb(&tx_urb->urb);
4563 }
4564 spin_unlock_irqrestore(&priv->tx_urb_lock, flags);
4565}
4566
4567static struct rtl8xxxu_tx_urb *
4568rtl8xxxu_alloc_tx_urb(struct rtl8xxxu_priv *priv)
4569{
4570 struct rtl8xxxu_tx_urb *tx_urb;
4571 unsigned long flags;
4572
4573 spin_lock_irqsave(&priv->tx_urb_lock, flags);
4574 tx_urb = list_first_entry_or_null(&priv->tx_urb_free_list,
4575 struct rtl8xxxu_tx_urb, list);
4576 if (tx_urb) {
4577 list_del(&tx_urb->list);
4578 priv->tx_urb_free_count--;
4579 if (priv->tx_urb_free_count < RTL8XXXU_TX_URB_LOW_WATER &&
4580 !priv->tx_stopped) {
4581 priv->tx_stopped = true;
4582 ieee80211_stop_queues(priv->hw);
4583 }
4584 }
4585
4586 spin_unlock_irqrestore(&priv->tx_urb_lock, flags);
4587
4588 return tx_urb;
4589}
4590
4591static void rtl8xxxu_free_tx_urb(struct rtl8xxxu_priv *priv,
4592 struct rtl8xxxu_tx_urb *tx_urb)
4593{
4594 unsigned long flags;
4595
4596 INIT_LIST_HEAD(&tx_urb->list);
4597
4598 spin_lock_irqsave(&priv->tx_urb_lock, flags);
4599
4600 list_add(&tx_urb->list, &priv->tx_urb_free_list);
4601 priv->tx_urb_free_count++;
4602 if (priv->tx_urb_free_count > RTL8XXXU_TX_URB_HIGH_WATER &&
4603 priv->tx_stopped) {
4604 priv->tx_stopped = false;
4605 ieee80211_wake_queues(priv->hw);
4606 }
4607
4608 spin_unlock_irqrestore(&priv->tx_urb_lock, flags);
4609}
4610
4611static void rtl8xxxu_tx_complete(struct urb *urb)
4612{
4613 struct sk_buff *skb = (struct sk_buff *)urb->context;
4614 struct ieee80211_tx_info *tx_info;
4615 struct ieee80211_hw *hw;
Jes Sorensen179e1742016-02-29 17:05:27 -05004616 struct rtl8xxxu_priv *priv;
Jes Sorensen26f1fad2015-10-14 20:44:51 -04004617 struct rtl8xxxu_tx_urb *tx_urb =
4618 container_of(urb, struct rtl8xxxu_tx_urb, urb);
4619
4620 tx_info = IEEE80211_SKB_CB(skb);
4621 hw = tx_info->rate_driver_data[0];
Jes Sorensen179e1742016-02-29 17:05:27 -05004622 priv = hw->priv;
Jes Sorensen26f1fad2015-10-14 20:44:51 -04004623
Jes Sorensen179e1742016-02-29 17:05:27 -05004624 skb_pull(skb, priv->fops->tx_desc_size);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04004625
4626 ieee80211_tx_info_clear_status(tx_info);
4627 tx_info->status.rates[0].idx = -1;
4628 tx_info->status.rates[0].count = 0;
4629
4630 if (!urb->status)
4631 tx_info->flags |= IEEE80211_TX_STAT_ACK;
4632
4633 ieee80211_tx_status_irqsafe(hw, skb);
4634
Jes Sorensen179e1742016-02-29 17:05:27 -05004635 rtl8xxxu_free_tx_urb(priv, tx_urb);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04004636}
4637
4638static void rtl8xxxu_dump_action(struct device *dev,
4639 struct ieee80211_hdr *hdr)
4640{
4641 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)hdr;
4642 u16 cap, timeout;
4643
4644 if (!(rtl8xxxu_debug & RTL8XXXU_DEBUG_ACTION))
4645 return;
4646
4647 switch (mgmt->u.action.u.addba_resp.action_code) {
4648 case WLAN_ACTION_ADDBA_RESP:
4649 cap = le16_to_cpu(mgmt->u.action.u.addba_resp.capab);
4650 timeout = le16_to_cpu(mgmt->u.action.u.addba_resp.timeout);
4651 dev_info(dev, "WLAN_ACTION_ADDBA_RESP: "
4652 "timeout %i, tid %02x, buf_size %02x, policy %02x, "
4653 "status %02x\n",
4654 timeout,
4655 (cap & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2,
4656 (cap & IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK) >> 6,
4657 (cap >> 1) & 0x1,
4658 le16_to_cpu(mgmt->u.action.u.addba_resp.status));
4659 break;
4660 case WLAN_ACTION_ADDBA_REQ:
4661 cap = le16_to_cpu(mgmt->u.action.u.addba_req.capab);
4662 timeout = le16_to_cpu(mgmt->u.action.u.addba_req.timeout);
4663 dev_info(dev, "WLAN_ACTION_ADDBA_REQ: "
4664 "timeout %i, tid %02x, buf_size %02x, policy %02x\n",
4665 timeout,
4666 (cap & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2,
4667 (cap & IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK) >> 6,
4668 (cap >> 1) & 0x1);
4669 break;
4670 default:
4671 dev_info(dev, "action frame %02x\n",
4672 mgmt->u.action.u.addba_resp.action_code);
4673 break;
4674 }
4675}
4676
4677static void rtl8xxxu_tx(struct ieee80211_hw *hw,
4678 struct ieee80211_tx_control *control,
4679 struct sk_buff *skb)
4680{
4681 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
4682 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
4683 struct ieee80211_rate *tx_rate = ieee80211_get_tx_rate(hw, tx_info);
4684 struct rtl8xxxu_priv *priv = hw->priv;
Jes Sorensendbb28962016-03-31 17:08:33 -04004685 struct rtl8xxxu_txdesc32 *tx_desc;
4686 struct rtl8xxxu_txdesc40 *tx_desc40;
Jes Sorensen26f1fad2015-10-14 20:44:51 -04004687 struct rtl8xxxu_tx_urb *tx_urb;
4688 struct ieee80211_sta *sta = NULL;
4689 struct ieee80211_vif *vif = tx_info->control.vif;
4690 struct device *dev = &priv->udev->dev;
4691 u32 queue, rate;
4692 u16 pktlen = skb->len;
4693 u16 seq_number;
4694 u16 rate_flag = tx_info->control.rates[0].flags;
Jes Sorensen179e1742016-02-29 17:05:27 -05004695 int tx_desc_size = priv->fops->tx_desc_size;
Jes Sorensen26f1fad2015-10-14 20:44:51 -04004696 int ret;
Jes Sorensencc2646d2016-02-29 17:05:32 -05004697 bool usedesc40, ampdu_enable;
Jes Sorensen26f1fad2015-10-14 20:44:51 -04004698
Jes Sorensen179e1742016-02-29 17:05:27 -05004699 if (skb_headroom(skb) < tx_desc_size) {
Jes Sorensen26f1fad2015-10-14 20:44:51 -04004700 dev_warn(dev,
4701 "%s: Not enough headroom (%i) for tx descriptor\n",
4702 __func__, skb_headroom(skb));
4703 goto error;
4704 }
4705
Jes Sorensen179e1742016-02-29 17:05:27 -05004706 if (unlikely(skb->len > (65535 - tx_desc_size))) {
Jes Sorensen26f1fad2015-10-14 20:44:51 -04004707 dev_warn(dev, "%s: Trying to send over-sized skb (%i)\n",
4708 __func__, skb->len);
4709 goto error;
4710 }
4711
4712 tx_urb = rtl8xxxu_alloc_tx_urb(priv);
4713 if (!tx_urb) {
4714 dev_warn(dev, "%s: Unable to allocate tx urb\n", __func__);
4715 goto error;
4716 }
4717
4718 if (rtl8xxxu_debug & RTL8XXXU_DEBUG_TX)
4719 dev_info(dev, "%s: TX rate: %d (%d), pkt size %d\n",
4720 __func__, tx_rate->bitrate, tx_rate->hw_value, pktlen);
4721
4722 if (ieee80211_is_action(hdr->frame_control))
4723 rtl8xxxu_dump_action(dev, hdr);
4724
Jes Sorensencc2646d2016-02-29 17:05:32 -05004725 usedesc40 = (tx_desc_size == 40);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04004726 tx_info->rate_driver_data[0] = hw;
4727
4728 if (control && control->sta)
4729 sta = control->sta;
4730
Jes Sorensendbb28962016-03-31 17:08:33 -04004731 tx_desc = (struct rtl8xxxu_txdesc32 *)skb_push(skb, tx_desc_size);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04004732
Jes Sorensen179e1742016-02-29 17:05:27 -05004733 memset(tx_desc, 0, tx_desc_size);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04004734 tx_desc->pkt_size = cpu_to_le16(pktlen);
Jes Sorensen179e1742016-02-29 17:05:27 -05004735 tx_desc->pkt_offset = tx_desc_size;
Jes Sorensen26f1fad2015-10-14 20:44:51 -04004736
4737 tx_desc->txdw0 =
4738 TXDESC_OWN | TXDESC_FIRST_SEGMENT | TXDESC_LAST_SEGMENT;
4739 if (is_multicast_ether_addr(ieee80211_get_DA(hdr)) ||
4740 is_broadcast_ether_addr(ieee80211_get_DA(hdr)))
4741 tx_desc->txdw0 |= TXDESC_BROADMULTICAST;
4742
4743 queue = rtl8xxxu_queue_select(hw, skb);
4744 tx_desc->txdw1 = cpu_to_le32(queue << TXDESC_QUEUE_SHIFT);
4745
4746 if (tx_info->control.hw_key) {
4747 switch (tx_info->control.hw_key->cipher) {
4748 case WLAN_CIPHER_SUITE_WEP40:
4749 case WLAN_CIPHER_SUITE_WEP104:
4750 case WLAN_CIPHER_SUITE_TKIP:
4751 tx_desc->txdw1 |= cpu_to_le32(TXDESC_SEC_RC4);
4752 break;
4753 case WLAN_CIPHER_SUITE_CCMP:
4754 tx_desc->txdw1 |= cpu_to_le32(TXDESC_SEC_AES);
4755 break;
4756 default:
4757 break;
4758 }
4759 }
4760
Jes Sorensen26f1fad2015-10-14 20:44:51 -04004761 /* (tx_info->flags & IEEE80211_TX_CTL_AMPDU) && */
Jes Sorensena40ace42016-02-29 17:05:31 -05004762 ampdu_enable = false;
Jes Sorensen26f1fad2015-10-14 20:44:51 -04004763 if (ieee80211_is_data_qos(hdr->frame_control) && sta) {
4764 if (sta->ht_cap.ht_supported) {
4765 u32 ampdu, val32;
4766
4767 ampdu = (u32)sta->ht_cap.ampdu_density;
4768 val32 = ampdu << TXDESC_AMPDU_DENSITY_SHIFT;
4769 tx_desc->txdw2 |= cpu_to_le32(val32);
Jes Sorensence2d1db2016-02-29 17:05:30 -05004770
Jes Sorensena40ace42016-02-29 17:05:31 -05004771 ampdu_enable = true;
4772 }
4773 }
4774
Jes Sorensen4c683602016-02-29 17:05:35 -05004775 if (rate_flag & IEEE80211_TX_RC_MCS)
4776 rate = tx_info->control.rates[0].idx + DESC_RATE_MCS0;
4777 else
4778 rate = tx_rate->hw_value;
4779
Jes Sorensencc2646d2016-02-29 17:05:32 -05004780 seq_number = IEEE80211_SEQ_TO_SN(le16_to_cpu(hdr->seq_ctrl));
4781 if (!usedesc40) {
Jes Sorensen4c683602016-02-29 17:05:35 -05004782 tx_desc->txdw5 = cpu_to_le32(rate);
4783
4784 if (ieee80211_is_data(hdr->frame_control))
4785 tx_desc->txdw5 |= cpu_to_le32(0x0001ff00);
4786
Jes Sorensencc2646d2016-02-29 17:05:32 -05004787 tx_desc->txdw3 =
Jes Sorensen33f37242016-03-31 17:08:34 -04004788 cpu_to_le32((u32)seq_number << TXDESC32_SEQ_SHIFT);
Jes Sorensencc2646d2016-02-29 17:05:32 -05004789
Jes Sorensena40ace42016-02-29 17:05:31 -05004790 if (ampdu_enable)
Jes Sorensen33f37242016-03-31 17:08:34 -04004791 tx_desc->txdw1 |= cpu_to_le32(TXDESC32_AGG_ENABLE);
Jes Sorensena40ace42016-02-29 17:05:31 -05004792 else
Jes Sorensen33f37242016-03-31 17:08:34 -04004793 tx_desc->txdw1 |= cpu_to_le32(TXDESC32_AGG_BREAK);
Jes Sorensen4c683602016-02-29 17:05:35 -05004794
4795 if (ieee80211_is_mgmt(hdr->frame_control)) {
4796 tx_desc->txdw5 = cpu_to_le32(tx_rate->hw_value);
4797 tx_desc->txdw4 |=
Jes Sorensen33f37242016-03-31 17:08:34 -04004798 cpu_to_le32(TXDESC32_USE_DRIVER_RATE);
Jes Sorensen4c683602016-02-29 17:05:35 -05004799 tx_desc->txdw5 |=
Jes Sorensen33f37242016-03-31 17:08:34 -04004800 cpu_to_le32(6 << TXDESC32_RETRY_LIMIT_SHIFT);
Jes Sorensen4c683602016-02-29 17:05:35 -05004801 tx_desc->txdw5 |=
Jes Sorensen33f37242016-03-31 17:08:34 -04004802 cpu_to_le32(TXDESC32_RETRY_LIMIT_ENABLE);
Jes Sorensen4c683602016-02-29 17:05:35 -05004803 }
4804
4805 if (ieee80211_is_data_qos(hdr->frame_control))
Jes Sorensen33f37242016-03-31 17:08:34 -04004806 tx_desc->txdw4 |= cpu_to_le32(TXDESC32_QOS);
Jes Sorensen4c683602016-02-29 17:05:35 -05004807
4808 if (rate_flag & IEEE80211_TX_RC_USE_SHORT_PREAMBLE ||
4809 (sta && vif && vif->bss_conf.use_short_preamble))
Jes Sorensen33f37242016-03-31 17:08:34 -04004810 tx_desc->txdw4 |= cpu_to_le32(TXDESC32_SHORT_PREAMBLE);
Jes Sorensen4c683602016-02-29 17:05:35 -05004811
4812 if (rate_flag & IEEE80211_TX_RC_SHORT_GI ||
4813 (ieee80211_is_data_qos(hdr->frame_control) &&
4814 sta && sta->ht_cap.cap &
4815 (IEEE80211_HT_CAP_SGI_40 | IEEE80211_HT_CAP_SGI_20))) {
Jes Sorensen1df1de32016-03-31 17:08:36 -04004816 tx_desc->txdw5 |= cpu_to_le32(TXDESC32_SHORT_GI);
Jes Sorensen4c683602016-02-29 17:05:35 -05004817 }
4818
4819 if (rate_flag & IEEE80211_TX_RC_USE_RTS_CTS) {
4820 /*
4821 * Use RTS rate 24M - does the mac80211 tell
4822 * us which to use?
4823 */
4824 tx_desc->txdw4 |=
4825 cpu_to_le32(DESC_RATE_24M <<
Jes Sorensen33f37242016-03-31 17:08:34 -04004826 TXDESC32_RTS_RATE_SHIFT);
Jes Sorensen4c683602016-02-29 17:05:35 -05004827 tx_desc->txdw4 |=
Jes Sorensen33f37242016-03-31 17:08:34 -04004828 cpu_to_le32(TXDESC32_RTS_CTS_ENABLE);
4829 tx_desc->txdw4 |= cpu_to_le32(TXDESC32_HW_RTS_ENABLE);
Jes Sorensen4c683602016-02-29 17:05:35 -05004830 }
Jes Sorensena40ace42016-02-29 17:05:31 -05004831 } else {
Jes Sorensendbb28962016-03-31 17:08:33 -04004832 tx_desc40 = (struct rtl8xxxu_txdesc40 *)tx_desc;
Jes Sorensencc2646d2016-02-29 17:05:32 -05004833
Jes Sorensen4c683602016-02-29 17:05:35 -05004834 tx_desc40->txdw4 = cpu_to_le32(rate);
4835 if (ieee80211_is_data(hdr->frame_control)) {
4836 tx_desc->txdw4 |=
4837 cpu_to_le32(0x1f <<
Jes Sorensen33f37242016-03-31 17:08:34 -04004838 TXDESC40_DATA_RATE_FB_SHIFT);
Jes Sorensen4c683602016-02-29 17:05:35 -05004839 }
4840
Jes Sorensencc2646d2016-02-29 17:05:32 -05004841 tx_desc40->txdw9 =
Jes Sorensen33f37242016-03-31 17:08:34 -04004842 cpu_to_le32((u32)seq_number << TXDESC40_SEQ_SHIFT);
Jes Sorensencc2646d2016-02-29 17:05:32 -05004843
Jes Sorensena40ace42016-02-29 17:05:31 -05004844 if (ampdu_enable)
Jes Sorensen33f37242016-03-31 17:08:34 -04004845 tx_desc40->txdw2 |= cpu_to_le32(TXDESC40_AGG_ENABLE);
Jes Sorensena40ace42016-02-29 17:05:31 -05004846 else
Jes Sorensen33f37242016-03-31 17:08:34 -04004847 tx_desc40->txdw2 |= cpu_to_le32(TXDESC40_AGG_BREAK);
Jes Sorensen4c683602016-02-29 17:05:35 -05004848
4849 if (ieee80211_is_mgmt(hdr->frame_control)) {
4850 tx_desc40->txdw4 = cpu_to_le32(tx_rate->hw_value);
4851 tx_desc40->txdw3 |=
Jes Sorensen33f37242016-03-31 17:08:34 -04004852 cpu_to_le32(TXDESC40_USE_DRIVER_RATE);
Jes Sorensen4c683602016-02-29 17:05:35 -05004853 tx_desc40->txdw4 |=
Jes Sorensen33f37242016-03-31 17:08:34 -04004854 cpu_to_le32(6 << TXDESC40_RETRY_LIMIT_SHIFT);
Jes Sorensen4c683602016-02-29 17:05:35 -05004855 tx_desc40->txdw4 |=
Jes Sorensen33f37242016-03-31 17:08:34 -04004856 cpu_to_le32(TXDESC40_RETRY_LIMIT_ENABLE);
Jes Sorensen4c683602016-02-29 17:05:35 -05004857 }
4858
4859 if (rate_flag & IEEE80211_TX_RC_USE_SHORT_PREAMBLE ||
4860 (sta && vif && vif->bss_conf.use_short_preamble))
4861 tx_desc40->txdw5 |=
Jes Sorensen33f37242016-03-31 17:08:34 -04004862 cpu_to_le32(TXDESC40_SHORT_PREAMBLE);
Jes Sorensen4c683602016-02-29 17:05:35 -05004863
4864 if (rate_flag & IEEE80211_TX_RC_USE_RTS_CTS) {
4865 /*
4866 * Use RTS rate 24M - does the mac80211 tell
4867 * us which to use?
4868 */
4869 tx_desc->txdw4 |=
4870 cpu_to_le32(DESC_RATE_24M <<
Jes Sorensen33f37242016-03-31 17:08:34 -04004871 TXDESC40_RTS_RATE_SHIFT);
4872 tx_desc->txdw3 |= cpu_to_le32(TXDESC40_RTS_CTS_ENABLE);
4873 tx_desc->txdw3 |= cpu_to_le32(TXDESC40_HW_RTS_ENABLE);
Jes Sorensen4c683602016-02-29 17:05:35 -05004874 }
Jes Sorensen69794942016-02-29 17:05:43 -05004875 }
Jes Sorensen26f1fad2015-10-14 20:44:51 -04004876
Jes Sorensen26f1fad2015-10-14 20:44:51 -04004877 rtl8xxxu_calc_tx_desc_csum(tx_desc);
4878
4879 usb_fill_bulk_urb(&tx_urb->urb, priv->udev, priv->pipe_out[queue],
4880 skb->data, skb->len, rtl8xxxu_tx_complete, skb);
4881
4882 usb_anchor_urb(&tx_urb->urb, &priv->tx_anchor);
4883 ret = usb_submit_urb(&tx_urb->urb, GFP_ATOMIC);
4884 if (ret) {
4885 usb_unanchor_urb(&tx_urb->urb);
4886 rtl8xxxu_free_tx_urb(priv, tx_urb);
4887 goto error;
4888 }
4889 return;
4890error:
4891 dev_kfree_skb(skb);
4892}
4893
4894static void rtl8xxxu_rx_parse_phystats(struct rtl8xxxu_priv *priv,
4895 struct ieee80211_rx_status *rx_status,
Jes Sorensen87957082016-02-29 17:05:42 -05004896 struct rtl8723au_phy_stats *phy_stats,
4897 u32 rxmcs)
Jes Sorensen26f1fad2015-10-14 20:44:51 -04004898{
4899 if (phy_stats->sgi_en)
4900 rx_status->flag |= RX_FLAG_SHORT_GI;
4901
Jes Sorensen87957082016-02-29 17:05:42 -05004902 if (rxmcs < DESC_RATE_6M) {
Jes Sorensen26f1fad2015-10-14 20:44:51 -04004903 /*
4904 * Handle PHY stats for CCK rates
4905 */
4906 u8 cck_agc_rpt = phy_stats->cck_agc_rpt_ofdm_cfosho_a;
4907
4908 switch (cck_agc_rpt & 0xc0) {
4909 case 0xc0:
4910 rx_status->signal = -46 - (cck_agc_rpt & 0x3e);
4911 break;
4912 case 0x80:
4913 rx_status->signal = -26 - (cck_agc_rpt & 0x3e);
4914 break;
4915 case 0x40:
4916 rx_status->signal = -12 - (cck_agc_rpt & 0x3e);
4917 break;
4918 case 0x00:
4919 rx_status->signal = 16 - (cck_agc_rpt & 0x3e);
4920 break;
4921 }
4922 } else {
4923 rx_status->signal =
4924 (phy_stats->cck_sig_qual_ofdm_pwdb_all >> 1) - 110;
4925 }
4926}
4927
4928static void rtl8xxxu_free_rx_resources(struct rtl8xxxu_priv *priv)
4929{
4930 struct rtl8xxxu_rx_urb *rx_urb, *tmp;
4931 unsigned long flags;
4932
4933 spin_lock_irqsave(&priv->rx_urb_lock, flags);
4934
4935 list_for_each_entry_safe(rx_urb, tmp,
4936 &priv->rx_urb_pending_list, list) {
4937 list_del(&rx_urb->list);
4938 priv->rx_urb_pending_count--;
4939 usb_free_urb(&rx_urb->urb);
4940 }
4941
4942 spin_unlock_irqrestore(&priv->rx_urb_lock, flags);
4943}
4944
4945static void rtl8xxxu_queue_rx_urb(struct rtl8xxxu_priv *priv,
4946 struct rtl8xxxu_rx_urb *rx_urb)
4947{
4948 struct sk_buff *skb;
4949 unsigned long flags;
4950 int pending = 0;
4951
4952 spin_lock_irqsave(&priv->rx_urb_lock, flags);
4953
4954 if (!priv->shutdown) {
4955 list_add_tail(&rx_urb->list, &priv->rx_urb_pending_list);
4956 priv->rx_urb_pending_count++;
4957 pending = priv->rx_urb_pending_count;
4958 } else {
4959 skb = (struct sk_buff *)rx_urb->urb.context;
4960 dev_kfree_skb(skb);
4961 usb_free_urb(&rx_urb->urb);
4962 }
4963
4964 spin_unlock_irqrestore(&priv->rx_urb_lock, flags);
4965
4966 if (pending > RTL8XXXU_RX_URB_PENDING_WATER)
4967 schedule_work(&priv->rx_urb_wq);
4968}
4969
4970static void rtl8xxxu_rx_urb_work(struct work_struct *work)
4971{
4972 struct rtl8xxxu_priv *priv;
4973 struct rtl8xxxu_rx_urb *rx_urb, *tmp;
4974 struct list_head local;
4975 struct sk_buff *skb;
4976 unsigned long flags;
4977 int ret;
4978
4979 priv = container_of(work, struct rtl8xxxu_priv, rx_urb_wq);
4980 INIT_LIST_HEAD(&local);
4981
4982 spin_lock_irqsave(&priv->rx_urb_lock, flags);
4983
4984 list_splice_init(&priv->rx_urb_pending_list, &local);
4985 priv->rx_urb_pending_count = 0;
4986
4987 spin_unlock_irqrestore(&priv->rx_urb_lock, flags);
4988
4989 list_for_each_entry_safe(rx_urb, tmp, &local, list) {
4990 list_del_init(&rx_urb->list);
4991 ret = rtl8xxxu_submit_rx_urb(priv, rx_urb);
4992 /*
4993 * If out of memory or temporary error, put it back on the
4994 * queue and try again. Otherwise the device is dead/gone
4995 * and we should drop it.
4996 */
4997 switch (ret) {
4998 case 0:
4999 break;
5000 case -ENOMEM:
5001 case -EAGAIN:
5002 rtl8xxxu_queue_rx_urb(priv, rx_urb);
5003 break;
5004 default:
5005 pr_info("failed to requeue urb %i\n", ret);
5006 skb = (struct sk_buff *)rx_urb->urb.context;
5007 dev_kfree_skb(skb);
5008 usb_free_urb(&rx_urb->urb);
5009 }
5010 }
5011}
5012
Jes Sorensen20e3b2e2016-04-28 15:19:08 -04005013int rtl8xxxu_parse_rxdesc16(struct rtl8xxxu_priv *priv, struct sk_buff *skb,
5014 struct ieee80211_rx_status *rx_status)
Jes Sorensenb18cdfd2016-02-29 17:04:47 -05005015{
Jes Sorensena49c7ce2016-04-14 14:58:52 -04005016 struct rtl8xxxu_rxdesc16 *rx_desc =
5017 (struct rtl8xxxu_rxdesc16 *)skb->data;
Jes Sorensenb18cdfd2016-02-29 17:04:47 -05005018 struct rtl8723au_phy_stats *phy_stats;
Jes Sorensen2cb79eb2016-04-14 14:58:51 -04005019 __le32 *_rx_desc_le = (__le32 *)skb->data;
5020 u32 *_rx_desc = (u32 *)skb->data;
Jes Sorensenb18cdfd2016-02-29 17:04:47 -05005021 int drvinfo_sz, desc_shift;
Jes Sorensen2cb79eb2016-04-14 14:58:51 -04005022 int i;
5023
Jes Sorensena49c7ce2016-04-14 14:58:52 -04005024 for (i = 0; i < (sizeof(struct rtl8xxxu_rxdesc16) / sizeof(u32)); i++)
Jes Sorensen2cb79eb2016-04-14 14:58:51 -04005025 _rx_desc[i] = le32_to_cpu(_rx_desc_le[i]);
Jes Sorensenb18cdfd2016-02-29 17:04:47 -05005026
Jes Sorensena49c7ce2016-04-14 14:58:52 -04005027 skb_pull(skb, sizeof(struct rtl8xxxu_rxdesc16));
Jes Sorensenb18cdfd2016-02-29 17:04:47 -05005028
5029 phy_stats = (struct rtl8723au_phy_stats *)skb->data;
5030
5031 drvinfo_sz = rx_desc->drvinfo_sz * 8;
5032 desc_shift = rx_desc->shift;
5033 skb_pull(skb, drvinfo_sz + desc_shift);
5034
5035 if (rx_desc->phy_stats)
Jes Sorensen87957082016-02-29 17:05:42 -05005036 rtl8xxxu_rx_parse_phystats(priv, rx_status, phy_stats,
5037 rx_desc->rxmcs);
Jes Sorensenb18cdfd2016-02-29 17:04:47 -05005038
5039 rx_status->mactime = le32_to_cpu(rx_desc->tsfl);
5040 rx_status->flag |= RX_FLAG_MACTIME_START;
5041
5042 if (!rx_desc->swdec)
5043 rx_status->flag |= RX_FLAG_DECRYPTED;
5044 if (rx_desc->crc32)
5045 rx_status->flag |= RX_FLAG_FAILED_FCS_CRC;
5046 if (rx_desc->bw)
5047 rx_status->flag |= RX_FLAG_40MHZ;
5048
5049 if (rx_desc->rxht) {
5050 rx_status->flag |= RX_FLAG_HT;
5051 rx_status->rate_idx = rx_desc->rxmcs - DESC_RATE_MCS0;
5052 } else {
5053 rx_status->rate_idx = rx_desc->rxmcs;
5054 }
5055
5056 return RX_TYPE_DATA_PKT;
5057}
5058
Jes Sorensen599119f2016-04-28 15:19:06 -04005059int rtl8xxxu_parse_rxdesc24(struct rtl8xxxu_priv *priv, struct sk_buff *skb,
5060 struct ieee80211_rx_status *rx_status)
Jes Sorensenb18cdfd2016-02-29 17:04:47 -05005061{
Jes Sorensena49c7ce2016-04-14 14:58:52 -04005062 struct rtl8xxxu_rxdesc24 *rx_desc =
5063 (struct rtl8xxxu_rxdesc24 *)skb->data;
Jes Sorensenb18cdfd2016-02-29 17:04:47 -05005064 struct rtl8723au_phy_stats *phy_stats;
Jes Sorensen2cb79eb2016-04-14 14:58:51 -04005065 __le32 *_rx_desc_le = (__le32 *)skb->data;
5066 u32 *_rx_desc = (u32 *)skb->data;
Jes Sorensenb18cdfd2016-02-29 17:04:47 -05005067 int drvinfo_sz, desc_shift;
Jes Sorensen2cb79eb2016-04-14 14:58:51 -04005068 int i;
5069
Jes Sorensena49c7ce2016-04-14 14:58:52 -04005070 for (i = 0; i < (sizeof(struct rtl8xxxu_rxdesc24) / sizeof(u32)); i++)
Jes Sorensen2cb79eb2016-04-14 14:58:51 -04005071 _rx_desc[i] = le32_to_cpu(_rx_desc_le[i]);
Jes Sorensenb18cdfd2016-02-29 17:04:47 -05005072
Jes Sorensena49c7ce2016-04-14 14:58:52 -04005073 skb_pull(skb, sizeof(struct rtl8xxxu_rxdesc24));
Jes Sorensenb18cdfd2016-02-29 17:04:47 -05005074
5075 phy_stats = (struct rtl8723au_phy_stats *)skb->data;
5076
5077 drvinfo_sz = rx_desc->drvinfo_sz * 8;
5078 desc_shift = rx_desc->shift;
5079 skb_pull(skb, drvinfo_sz + desc_shift);
5080
Jes Sorensene975b872016-02-29 17:05:36 -05005081 if (rx_desc->rpt_sel) {
5082 struct device *dev = &priv->udev->dev;
5083 dev_dbg(dev, "%s: C2H packet\n", __func__);
5084 return RX_TYPE_C2H;
5085 }
5086
Jes Sorensen87957082016-02-29 17:05:42 -05005087 if (rx_desc->phy_stats)
5088 rtl8xxxu_rx_parse_phystats(priv, rx_status, phy_stats,
5089 rx_desc->rxmcs);
5090
Jes Sorensenb18cdfd2016-02-29 17:04:47 -05005091 rx_status->mactime = le32_to_cpu(rx_desc->tsfl);
5092 rx_status->flag |= RX_FLAG_MACTIME_START;
5093
5094 if (!rx_desc->swdec)
5095 rx_status->flag |= RX_FLAG_DECRYPTED;
5096 if (rx_desc->crc32)
5097 rx_status->flag |= RX_FLAG_FAILED_FCS_CRC;
5098 if (rx_desc->bw)
5099 rx_status->flag |= RX_FLAG_40MHZ;
5100
5101 if (rx_desc->rxmcs >= DESC_RATE_MCS0) {
5102 rx_status->flag |= RX_FLAG_HT;
5103 rx_status->rate_idx = rx_desc->rxmcs - DESC_RATE_MCS0;
5104 } else {
5105 rx_status->rate_idx = rx_desc->rxmcs;
5106 }
5107
Jes Sorensene975b872016-02-29 17:05:36 -05005108 return RX_TYPE_DATA_PKT;
Jes Sorensenb18cdfd2016-02-29 17:04:47 -05005109}
5110
Jes Sorensenb2b43b72016-02-29 17:04:48 -05005111static void rtl8723bu_handle_c2h(struct rtl8xxxu_priv *priv,
5112 struct sk_buff *skb)
5113{
5114 struct rtl8723bu_c2h *c2h = (struct rtl8723bu_c2h *)skb->data;
5115 struct device *dev = &priv->udev->dev;
5116 int len;
5117
5118 len = skb->len - 2;
5119
Jes Sorensen5e00d502016-02-29 17:05:28 -05005120 dev_dbg(dev, "C2H ID %02x seq %02x, len %02x source %02x\n",
5121 c2h->id, c2h->seq, len, c2h->bt_info.response_source);
Jes Sorensenb2b43b72016-02-29 17:04:48 -05005122
5123 switch(c2h->id) {
5124 case C2H_8723B_BT_INFO:
5125 if (c2h->bt_info.response_source >
5126 BT_INFO_SRC_8723B_BT_ACTIVE_SEND)
Jes Sorensen5e00d502016-02-29 17:05:28 -05005127 dev_dbg(dev, "C2H_BT_INFO WiFi only firmware\n");
Jes Sorensenb2b43b72016-02-29 17:04:48 -05005128 else
Jes Sorensen5e00d502016-02-29 17:05:28 -05005129 dev_dbg(dev, "C2H_BT_INFO BT/WiFi coexist firmware\n");
Jes Sorensenb2b43b72016-02-29 17:04:48 -05005130
5131 if (c2h->bt_info.bt_has_reset)
Jes Sorensen5e00d502016-02-29 17:05:28 -05005132 dev_dbg(dev, "BT has been reset\n");
Jes Sorensen394f1bd2016-02-29 17:04:49 -05005133 if (c2h->bt_info.tx_rx_mask)
Jes Sorensen5e00d502016-02-29 17:05:28 -05005134 dev_dbg(dev, "BT TRx mask\n");
Jes Sorensenb2b43b72016-02-29 17:04:48 -05005135
5136 break;
Jes Sorensen394f1bd2016-02-29 17:04:49 -05005137 case C2H_8723B_BT_MP_INFO:
Jes Sorensen5e00d502016-02-29 17:05:28 -05005138 dev_dbg(dev, "C2H_MP_INFO ext ID %02x, status %02x\n",
5139 c2h->bt_mp_info.ext_id, c2h->bt_mp_info.status);
Jes Sorensen394f1bd2016-02-29 17:04:49 -05005140 break;
Jes Sorensen55a18dd2016-02-29 17:05:41 -05005141 case C2H_8723B_RA_REPORT:
5142 dev_dbg(dev,
5143 "C2H RA RPT: rate %02x, unk %i, macid %02x, noise %i\n",
5144 c2h->ra_report.rate, c2h->ra_report.dummy0_0,
5145 c2h->ra_report.macid, c2h->ra_report.noisy_state);
5146 break;
Jes Sorensenb2b43b72016-02-29 17:04:48 -05005147 default:
Jes Sorensen739dc9f2016-02-29 17:05:40 -05005148 dev_info(dev, "Unhandled C2H event %02x seq %02x\n",
5149 c2h->id, c2h->seq);
5150 print_hex_dump(KERN_INFO, "C2H content: ", DUMP_PREFIX_NONE,
5151 16, 1, c2h->raw.payload, len, false);
Jes Sorensenb2b43b72016-02-29 17:04:48 -05005152 break;
5153 }
5154}
5155
Jes Sorensen26f1fad2015-10-14 20:44:51 -04005156static void rtl8xxxu_rx_complete(struct urb *urb)
5157{
5158 struct rtl8xxxu_rx_urb *rx_urb =
5159 container_of(urb, struct rtl8xxxu_rx_urb, urb);
5160 struct ieee80211_hw *hw = rx_urb->hw;
5161 struct rtl8xxxu_priv *priv = hw->priv;
5162 struct sk_buff *skb = (struct sk_buff *)urb->context;
Jes Sorensen26f1fad2015-10-14 20:44:51 -04005163 struct ieee80211_rx_status *rx_status = IEEE80211_SKB_RXCB(skb);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04005164 struct device *dev = &priv->udev->dev;
Jes Sorensen2cb79eb2016-04-14 14:58:51 -04005165 int rx_type;
Jes Sorensen26f1fad2015-10-14 20:44:51 -04005166
Jes Sorensen26f1fad2015-10-14 20:44:51 -04005167 skb_put(skb, urb->actual_length);
5168
5169 if (urb->status == 0) {
Jes Sorensen26f1fad2015-10-14 20:44:51 -04005170 memset(rx_status, 0, sizeof(struct ieee80211_rx_status));
5171
Jes Sorensenb18cdfd2016-02-29 17:04:47 -05005172 rx_type = priv->fops->parse_rx_desc(priv, skb, rx_status);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04005173
5174 rx_status->freq = hw->conf.chandef.chan->center_freq;
5175 rx_status->band = hw->conf.chandef.chan->band;
5176
Jes Sorensenb18cdfd2016-02-29 17:04:47 -05005177 if (rx_type == RX_TYPE_DATA_PKT)
5178 ieee80211_rx_irqsafe(hw, skb);
Jes Sorensenb2b43b72016-02-29 17:04:48 -05005179 else {
5180 rtl8723bu_handle_c2h(priv, skb);
Jes Sorensenb18cdfd2016-02-29 17:04:47 -05005181 dev_kfree_skb(skb);
Jes Sorensenb2b43b72016-02-29 17:04:48 -05005182 }
Jes Sorensen26f1fad2015-10-14 20:44:51 -04005183
Jes Sorensen26f1fad2015-10-14 20:44:51 -04005184 skb = NULL;
5185 rx_urb->urb.context = NULL;
5186 rtl8xxxu_queue_rx_urb(priv, rx_urb);
5187 } else {
5188 dev_dbg(dev, "%s: status %i\n", __func__, urb->status);
5189 goto cleanup;
5190 }
5191 return;
5192
5193cleanup:
5194 usb_free_urb(urb);
5195 dev_kfree_skb(skb);
5196 return;
5197}
5198
5199static int rtl8xxxu_submit_rx_urb(struct rtl8xxxu_priv *priv,
5200 struct rtl8xxxu_rx_urb *rx_urb)
5201{
5202 struct sk_buff *skb;
5203 int skb_size;
Jes Sorensena49c7ce2016-04-14 14:58:52 -04005204 int ret, rx_desc_sz;
Jes Sorensen26f1fad2015-10-14 20:44:51 -04005205
Jes Sorensena49c7ce2016-04-14 14:58:52 -04005206 rx_desc_sz = priv->fops->rx_desc_size;
5207 skb_size = rx_desc_sz + RTL_RX_BUFFER_SIZE;
Jes Sorensen26f1fad2015-10-14 20:44:51 -04005208 skb = __netdev_alloc_skb(NULL, skb_size, GFP_KERNEL);
5209 if (!skb)
5210 return -ENOMEM;
5211
Jes Sorensena49c7ce2016-04-14 14:58:52 -04005212 memset(skb->data, 0, rx_desc_sz);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04005213 usb_fill_bulk_urb(&rx_urb->urb, priv->udev, priv->pipe_in, skb->data,
5214 skb_size, rtl8xxxu_rx_complete, skb);
5215 usb_anchor_urb(&rx_urb->urb, &priv->rx_anchor);
5216 ret = usb_submit_urb(&rx_urb->urb, GFP_ATOMIC);
5217 if (ret)
5218 usb_unanchor_urb(&rx_urb->urb);
5219 return ret;
5220}
5221
5222static void rtl8xxxu_int_complete(struct urb *urb)
5223{
5224 struct rtl8xxxu_priv *priv = (struct rtl8xxxu_priv *)urb->context;
5225 struct device *dev = &priv->udev->dev;
5226 int ret;
5227
5228 dev_dbg(dev, "%s: status %i\n", __func__, urb->status);
5229 if (urb->status == 0) {
5230 usb_anchor_urb(urb, &priv->int_anchor);
5231 ret = usb_submit_urb(urb, GFP_ATOMIC);
5232 if (ret)
5233 usb_unanchor_urb(urb);
5234 } else {
5235 dev_info(dev, "%s: Error %i\n", __func__, urb->status);
5236 }
5237}
5238
5239
5240static int rtl8xxxu_submit_int_urb(struct ieee80211_hw *hw)
5241{
5242 struct rtl8xxxu_priv *priv = hw->priv;
5243 struct urb *urb;
5244 u32 val32;
5245 int ret;
5246
5247 urb = usb_alloc_urb(0, GFP_KERNEL);
5248 if (!urb)
5249 return -ENOMEM;
5250
5251 usb_fill_int_urb(urb, priv->udev, priv->pipe_interrupt,
5252 priv->int_buf, USB_INTR_CONTENT_LENGTH,
5253 rtl8xxxu_int_complete, priv, 1);
5254 usb_anchor_urb(urb, &priv->int_anchor);
5255 ret = usb_submit_urb(urb, GFP_KERNEL);
5256 if (ret) {
5257 usb_unanchor_urb(urb);
5258 goto error;
5259 }
5260
5261 val32 = rtl8xxxu_read32(priv, REG_USB_HIMR);
5262 val32 |= USB_HIMR_CPWM;
5263 rtl8xxxu_write32(priv, REG_USB_HIMR, val32);
5264
5265error:
5266 return ret;
5267}
5268
5269static int rtl8xxxu_add_interface(struct ieee80211_hw *hw,
5270 struct ieee80211_vif *vif)
5271{
5272 struct rtl8xxxu_priv *priv = hw->priv;
5273 int ret;
5274 u8 val8;
5275
5276 switch (vif->type) {
5277 case NL80211_IFTYPE_STATION:
Jes Sorensen97db5a82016-04-28 15:19:10 -04005278 rtl8xxxu_stop_tx_beacon(priv);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04005279
5280 val8 = rtl8xxxu_read8(priv, REG_BEACON_CTRL);
5281 val8 |= BEACON_ATIM | BEACON_FUNCTION_ENABLE |
5282 BEACON_DISABLE_TSF_UPDATE;
5283 rtl8xxxu_write8(priv, REG_BEACON_CTRL, val8);
5284 ret = 0;
5285 break;
5286 default:
5287 ret = -EOPNOTSUPP;
5288 }
5289
5290 rtl8xxxu_set_linktype(priv, vif->type);
5291
5292 return ret;
5293}
5294
5295static void rtl8xxxu_remove_interface(struct ieee80211_hw *hw,
5296 struct ieee80211_vif *vif)
5297{
5298 struct rtl8xxxu_priv *priv = hw->priv;
5299
5300 dev_dbg(&priv->udev->dev, "%s\n", __func__);
5301}
5302
5303static int rtl8xxxu_config(struct ieee80211_hw *hw, u32 changed)
5304{
5305 struct rtl8xxxu_priv *priv = hw->priv;
5306 struct device *dev = &priv->udev->dev;
5307 u16 val16;
5308 int ret = 0, channel;
5309 bool ht40;
5310
5311 if (rtl8xxxu_debug & RTL8XXXU_DEBUG_CHANNEL)
5312 dev_info(dev,
5313 "%s: channel: %i (changed %08x chandef.width %02x)\n",
5314 __func__, hw->conf.chandef.chan->hw_value,
5315 changed, hw->conf.chandef.width);
5316
5317 if (changed & IEEE80211_CONF_CHANGE_RETRY_LIMITS) {
5318 val16 = ((hw->conf.long_frame_max_tx_count <<
5319 RETRY_LIMIT_LONG_SHIFT) & RETRY_LIMIT_LONG_MASK) |
5320 ((hw->conf.short_frame_max_tx_count <<
5321 RETRY_LIMIT_SHORT_SHIFT) & RETRY_LIMIT_SHORT_MASK);
5322 rtl8xxxu_write16(priv, REG_RETRY_LIMIT, val16);
5323 }
5324
5325 if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
5326 switch (hw->conf.chandef.width) {
5327 case NL80211_CHAN_WIDTH_20_NOHT:
5328 case NL80211_CHAN_WIDTH_20:
5329 ht40 = false;
5330 break;
5331 case NL80211_CHAN_WIDTH_40:
5332 ht40 = true;
5333 break;
5334 default:
5335 ret = -ENOTSUPP;
5336 goto exit;
5337 }
5338
5339 channel = hw->conf.chandef.chan->hw_value;
5340
Jes Sorensene796dab2016-02-29 17:05:19 -05005341 priv->fops->set_tx_power(priv, channel, ht40);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04005342
Jes Sorensen1ea8e842016-02-29 17:05:04 -05005343 priv->fops->config_channel(hw);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04005344 }
5345
5346exit:
5347 return ret;
5348}
5349
5350static int rtl8xxxu_conf_tx(struct ieee80211_hw *hw,
5351 struct ieee80211_vif *vif, u16 queue,
5352 const struct ieee80211_tx_queue_params *param)
5353{
5354 struct rtl8xxxu_priv *priv = hw->priv;
5355 struct device *dev = &priv->udev->dev;
5356 u32 val32;
5357 u8 aifs, acm_ctrl, acm_bit;
5358
5359 aifs = param->aifs;
5360
5361 val32 = aifs |
5362 fls(param->cw_min) << EDCA_PARAM_ECW_MIN_SHIFT |
5363 fls(param->cw_max) << EDCA_PARAM_ECW_MAX_SHIFT |
5364 (u32)param->txop << EDCA_PARAM_TXOP_SHIFT;
5365
5366 acm_ctrl = rtl8xxxu_read8(priv, REG_ACM_HW_CTRL);
5367 dev_dbg(dev,
5368 "%s: IEEE80211 queue %02x val %08x, acm %i, acm_ctrl %02x\n",
5369 __func__, queue, val32, param->acm, acm_ctrl);
5370
5371 switch (queue) {
5372 case IEEE80211_AC_VO:
5373 acm_bit = ACM_HW_CTRL_VO;
5374 rtl8xxxu_write32(priv, REG_EDCA_VO_PARAM, val32);
5375 break;
5376 case IEEE80211_AC_VI:
5377 acm_bit = ACM_HW_CTRL_VI;
5378 rtl8xxxu_write32(priv, REG_EDCA_VI_PARAM, val32);
5379 break;
5380 case IEEE80211_AC_BE:
5381 acm_bit = ACM_HW_CTRL_BE;
5382 rtl8xxxu_write32(priv, REG_EDCA_BE_PARAM, val32);
5383 break;
5384 case IEEE80211_AC_BK:
5385 acm_bit = ACM_HW_CTRL_BK;
5386 rtl8xxxu_write32(priv, REG_EDCA_BK_PARAM, val32);
5387 break;
5388 default:
5389 acm_bit = 0;
5390 break;
5391 }
5392
5393 if (param->acm)
5394 acm_ctrl |= acm_bit;
5395 else
5396 acm_ctrl &= ~acm_bit;
5397 rtl8xxxu_write8(priv, REG_ACM_HW_CTRL, acm_ctrl);
5398
5399 return 0;
5400}
5401
5402static void rtl8xxxu_configure_filter(struct ieee80211_hw *hw,
5403 unsigned int changed_flags,
5404 unsigned int *total_flags, u64 multicast)
5405{
5406 struct rtl8xxxu_priv *priv = hw->priv;
Bruno Randolf3bed4bf2016-02-03 13:39:51 -05005407 u32 rcr = rtl8xxxu_read32(priv, REG_RCR);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04005408
5409 dev_dbg(&priv->udev->dev, "%s: changed_flags %08x, total_flags %08x\n",
5410 __func__, changed_flags, *total_flags);
5411
Bruno Randolf3bed4bf2016-02-03 13:39:51 -05005412 /*
5413 * FIF_ALLMULTI ignored as all multicast frames are accepted (REG_MAR)
5414 */
5415
5416 if (*total_flags & FIF_FCSFAIL)
5417 rcr |= RCR_ACCEPT_CRC32;
5418 else
5419 rcr &= ~RCR_ACCEPT_CRC32;
5420
5421 /*
5422 * FIF_PLCPFAIL not supported?
5423 */
5424
5425 if (*total_flags & FIF_BCN_PRBRESP_PROMISC)
5426 rcr &= ~RCR_CHECK_BSSID_BEACON;
5427 else
5428 rcr |= RCR_CHECK_BSSID_BEACON;
5429
5430 if (*total_flags & FIF_CONTROL)
5431 rcr |= RCR_ACCEPT_CTRL_FRAME;
5432 else
5433 rcr &= ~RCR_ACCEPT_CTRL_FRAME;
5434
5435 if (*total_flags & FIF_OTHER_BSS) {
5436 rcr |= RCR_ACCEPT_AP;
5437 rcr &= ~RCR_CHECK_BSSID_MATCH;
5438 } else {
5439 rcr &= ~RCR_ACCEPT_AP;
5440 rcr |= RCR_CHECK_BSSID_MATCH;
5441 }
5442
5443 if (*total_flags & FIF_PSPOLL)
5444 rcr |= RCR_ACCEPT_PM;
5445 else
5446 rcr &= ~RCR_ACCEPT_PM;
5447
5448 /*
5449 * FIF_PROBE_REQ ignored as probe requests always seem to be accepted
5450 */
5451
5452 rtl8xxxu_write32(priv, REG_RCR, rcr);
5453
Jes Sorensen755bda12016-02-03 13:39:54 -05005454 *total_flags &= (FIF_ALLMULTI | FIF_FCSFAIL | FIF_BCN_PRBRESP_PROMISC |
5455 FIF_CONTROL | FIF_OTHER_BSS | FIF_PSPOLL |
5456 FIF_PROBE_REQ);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04005457}
5458
5459static int rtl8xxxu_set_rts_threshold(struct ieee80211_hw *hw, u32 rts)
5460{
5461 if (rts > 2347)
5462 return -EINVAL;
5463
5464 return 0;
5465}
5466
5467static int rtl8xxxu_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
5468 struct ieee80211_vif *vif,
5469 struct ieee80211_sta *sta,
5470 struct ieee80211_key_conf *key)
5471{
5472 struct rtl8xxxu_priv *priv = hw->priv;
5473 struct device *dev = &priv->udev->dev;
5474 u8 mac_addr[ETH_ALEN];
5475 u8 val8;
5476 u16 val16;
5477 u32 val32;
5478 int retval = -EOPNOTSUPP;
5479
5480 dev_dbg(dev, "%s: cmd %02x, cipher %08x, index %i\n",
5481 __func__, cmd, key->cipher, key->keyidx);
5482
5483 if (vif->type != NL80211_IFTYPE_STATION)
5484 return -EOPNOTSUPP;
5485
5486 if (key->keyidx > 3)
5487 return -EOPNOTSUPP;
5488
5489 switch (key->cipher) {
5490 case WLAN_CIPHER_SUITE_WEP40:
5491 case WLAN_CIPHER_SUITE_WEP104:
5492
5493 break;
5494 case WLAN_CIPHER_SUITE_CCMP:
5495 key->flags |= IEEE80211_KEY_FLAG_SW_MGMT_TX;
5496 break;
5497 case WLAN_CIPHER_SUITE_TKIP:
5498 key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
5499 default:
5500 return -EOPNOTSUPP;
5501 }
5502
5503 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
5504 dev_dbg(dev, "%s: pairwise key\n", __func__);
5505 ether_addr_copy(mac_addr, sta->addr);
5506 } else {
5507 dev_dbg(dev, "%s: group key\n", __func__);
5508 eth_broadcast_addr(mac_addr);
5509 }
5510
5511 val16 = rtl8xxxu_read16(priv, REG_CR);
5512 val16 |= CR_SECURITY_ENABLE;
5513 rtl8xxxu_write16(priv, REG_CR, val16);
5514
5515 val8 = SEC_CFG_TX_SEC_ENABLE | SEC_CFG_TXBC_USE_DEFKEY |
5516 SEC_CFG_RX_SEC_ENABLE | SEC_CFG_RXBC_USE_DEFKEY;
5517 val8 |= SEC_CFG_TX_USE_DEFKEY | SEC_CFG_RX_USE_DEFKEY;
5518 rtl8xxxu_write8(priv, REG_SECURITY_CFG, val8);
5519
5520 switch (cmd) {
5521 case SET_KEY:
5522 key->hw_key_idx = key->keyidx;
5523 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
5524 rtl8xxxu_cam_write(priv, key, mac_addr);
5525 retval = 0;
5526 break;
5527 case DISABLE_KEY:
5528 rtl8xxxu_write32(priv, REG_CAM_WRITE, 0x00000000);
5529 val32 = CAM_CMD_POLLING | CAM_CMD_WRITE |
5530 key->keyidx << CAM_CMD_KEY_SHIFT;
5531 rtl8xxxu_write32(priv, REG_CAM_CMD, val32);
5532 retval = 0;
5533 break;
5534 default:
5535 dev_warn(dev, "%s: Unsupported command %02x\n", __func__, cmd);
5536 }
5537
5538 return retval;
5539}
5540
5541static int
5542rtl8xxxu_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
Sara Sharon50ea05e2015-12-30 16:06:04 +02005543 struct ieee80211_ampdu_params *params)
Jes Sorensen26f1fad2015-10-14 20:44:51 -04005544{
5545 struct rtl8xxxu_priv *priv = hw->priv;
5546 struct device *dev = &priv->udev->dev;
5547 u8 ampdu_factor, ampdu_density;
Sara Sharon50ea05e2015-12-30 16:06:04 +02005548 struct ieee80211_sta *sta = params->sta;
5549 enum ieee80211_ampdu_mlme_action action = params->action;
Jes Sorensen26f1fad2015-10-14 20:44:51 -04005550
5551 switch (action) {
5552 case IEEE80211_AMPDU_TX_START:
5553 dev_info(dev, "%s: IEEE80211_AMPDU_TX_START\n", __func__);
5554 ampdu_factor = sta->ht_cap.ampdu_factor;
5555 ampdu_density = sta->ht_cap.ampdu_density;
5556 rtl8xxxu_set_ampdu_factor(priv, ampdu_factor);
5557 rtl8xxxu_set_ampdu_min_space(priv, ampdu_density);
5558 dev_dbg(dev,
5559 "Changed HT: ampdu_factor %02x, ampdu_density %02x\n",
5560 ampdu_factor, ampdu_density);
5561 break;
5562 case IEEE80211_AMPDU_TX_STOP_FLUSH:
5563 dev_info(dev, "%s: IEEE80211_AMPDU_TX_STOP_FLUSH\n", __func__);
5564 rtl8xxxu_set_ampdu_factor(priv, 0);
5565 rtl8xxxu_set_ampdu_min_space(priv, 0);
5566 break;
5567 case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
5568 dev_info(dev, "%s: IEEE80211_AMPDU_TX_STOP_FLUSH_CONT\n",
5569 __func__);
5570 rtl8xxxu_set_ampdu_factor(priv, 0);
5571 rtl8xxxu_set_ampdu_min_space(priv, 0);
5572 break;
5573 case IEEE80211_AMPDU_RX_START:
5574 dev_info(dev, "%s: IEEE80211_AMPDU_RX_START\n", __func__);
5575 break;
5576 case IEEE80211_AMPDU_RX_STOP:
5577 dev_info(dev, "%s: IEEE80211_AMPDU_RX_STOP\n", __func__);
5578 break;
5579 default:
5580 break;
5581 }
5582 return 0;
5583}
5584
5585static int rtl8xxxu_start(struct ieee80211_hw *hw)
5586{
5587 struct rtl8xxxu_priv *priv = hw->priv;
5588 struct rtl8xxxu_rx_urb *rx_urb;
5589 struct rtl8xxxu_tx_urb *tx_urb;
5590 unsigned long flags;
5591 int ret, i;
5592
5593 ret = 0;
5594
5595 init_usb_anchor(&priv->rx_anchor);
5596 init_usb_anchor(&priv->tx_anchor);
5597 init_usb_anchor(&priv->int_anchor);
5598
Jes Sorensendb08de92016-02-29 17:05:17 -05005599 priv->fops->enable_rf(priv);
Jes Sorensen0e28b972016-02-29 17:04:13 -05005600 if (priv->usb_interrupts) {
5601 ret = rtl8xxxu_submit_int_urb(hw);
5602 if (ret)
5603 goto exit;
5604 }
Jes Sorensen26f1fad2015-10-14 20:44:51 -04005605
5606 for (i = 0; i < RTL8XXXU_TX_URBS; i++) {
5607 tx_urb = kmalloc(sizeof(struct rtl8xxxu_tx_urb), GFP_KERNEL);
5608 if (!tx_urb) {
5609 if (!i)
5610 ret = -ENOMEM;
5611
5612 goto error_out;
5613 }
5614 usb_init_urb(&tx_urb->urb);
5615 INIT_LIST_HEAD(&tx_urb->list);
5616 tx_urb->hw = hw;
5617 list_add(&tx_urb->list, &priv->tx_urb_free_list);
5618 priv->tx_urb_free_count++;
5619 }
5620
5621 priv->tx_stopped = false;
5622
5623 spin_lock_irqsave(&priv->rx_urb_lock, flags);
5624 priv->shutdown = false;
5625 spin_unlock_irqrestore(&priv->rx_urb_lock, flags);
5626
5627 for (i = 0; i < RTL8XXXU_RX_URBS; i++) {
5628 rx_urb = kmalloc(sizeof(struct rtl8xxxu_rx_urb), GFP_KERNEL);
5629 if (!rx_urb) {
5630 if (!i)
5631 ret = -ENOMEM;
5632
5633 goto error_out;
5634 }
5635 usb_init_urb(&rx_urb->urb);
5636 INIT_LIST_HEAD(&rx_urb->list);
5637 rx_urb->hw = hw;
5638
5639 ret = rtl8xxxu_submit_rx_urb(priv, rx_urb);
5640 }
5641exit:
5642 /*
Bruno Randolfc85ea112016-02-03 13:39:55 -05005643 * Accept all data and mgmt frames
Jes Sorensen26f1fad2015-10-14 20:44:51 -04005644 */
Bruno Randolfc85ea112016-02-03 13:39:55 -05005645 rtl8xxxu_write16(priv, REG_RXFLTMAP2, 0xffff);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04005646 rtl8xxxu_write16(priv, REG_RXFLTMAP0, 0xffff);
5647
5648 rtl8xxxu_write32(priv, REG_OFDM0_XA_AGC_CORE1, 0x6954341e);
5649
5650 return ret;
5651
5652error_out:
5653 rtl8xxxu_free_tx_resources(priv);
5654 /*
5655 * Disable all data and mgmt frames
5656 */
5657 rtl8xxxu_write16(priv, REG_RXFLTMAP2, 0x0000);
5658 rtl8xxxu_write16(priv, REG_RXFLTMAP0, 0x0000);
5659
5660 return ret;
5661}
5662
5663static void rtl8xxxu_stop(struct ieee80211_hw *hw)
5664{
5665 struct rtl8xxxu_priv *priv = hw->priv;
5666 unsigned long flags;
5667
5668 rtl8xxxu_write8(priv, REG_TXPAUSE, 0xff);
5669
5670 rtl8xxxu_write16(priv, REG_RXFLTMAP0, 0x0000);
5671 rtl8xxxu_write16(priv, REG_RXFLTMAP2, 0x0000);
5672
5673 spin_lock_irqsave(&priv->rx_urb_lock, flags);
5674 priv->shutdown = true;
5675 spin_unlock_irqrestore(&priv->rx_urb_lock, flags);
5676
5677 usb_kill_anchored_urbs(&priv->rx_anchor);
5678 usb_kill_anchored_urbs(&priv->tx_anchor);
Jes Sorensen0e28b972016-02-29 17:04:13 -05005679 if (priv->usb_interrupts)
5680 usb_kill_anchored_urbs(&priv->int_anchor);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04005681
Jes Sorensen265697e2016-04-14 16:37:20 -04005682 rtl8xxxu_write8(priv, REG_TXPAUSE, 0xff);
5683
Jes Sorensenfc89a412016-02-29 17:05:46 -05005684 priv->fops->disable_rf(priv);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04005685
5686 /*
5687 * Disable interrupts
5688 */
Jes Sorensen0e28b972016-02-29 17:04:13 -05005689 if (priv->usb_interrupts)
5690 rtl8xxxu_write32(priv, REG_USB_HIMR, 0);
Jes Sorensen26f1fad2015-10-14 20:44:51 -04005691
5692 rtl8xxxu_free_rx_resources(priv);
5693 rtl8xxxu_free_tx_resources(priv);
5694}
5695
5696static const struct ieee80211_ops rtl8xxxu_ops = {
5697 .tx = rtl8xxxu_tx,
5698 .add_interface = rtl8xxxu_add_interface,
5699 .remove_interface = rtl8xxxu_remove_interface,
5700 .config = rtl8xxxu_config,
5701 .conf_tx = rtl8xxxu_conf_tx,
5702 .bss_info_changed = rtl8xxxu_bss_info_changed,
5703 .configure_filter = rtl8xxxu_configure_filter,
5704 .set_rts_threshold = rtl8xxxu_set_rts_threshold,
5705 .start = rtl8xxxu_start,
5706 .stop = rtl8xxxu_stop,
5707 .sw_scan_start = rtl8xxxu_sw_scan_start,
5708 .sw_scan_complete = rtl8xxxu_sw_scan_complete,
5709 .set_key = rtl8xxxu_set_key,
5710 .ampdu_action = rtl8xxxu_ampdu_action,
5711};
5712
5713static int rtl8xxxu_parse_usb(struct rtl8xxxu_priv *priv,
5714 struct usb_interface *interface)
5715{
5716 struct usb_interface_descriptor *interface_desc;
5717 struct usb_host_interface *host_interface;
5718 struct usb_endpoint_descriptor *endpoint;
5719 struct device *dev = &priv->udev->dev;
5720 int i, j = 0, endpoints;
5721 u8 dir, xtype, num;
5722 int ret = 0;
5723
5724 host_interface = &interface->altsetting[0];
5725 interface_desc = &host_interface->desc;
5726 endpoints = interface_desc->bNumEndpoints;
5727
5728 for (i = 0; i < endpoints; i++) {
5729 endpoint = &host_interface->endpoint[i].desc;
5730
5731 dir = endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK;
5732 num = usb_endpoint_num(endpoint);
5733 xtype = usb_endpoint_type(endpoint);
5734 if (rtl8xxxu_debug & RTL8XXXU_DEBUG_USB)
5735 dev_dbg(dev,
5736 "%s: endpoint: dir %02x, # %02x, type %02x\n",
5737 __func__, dir, num, xtype);
5738 if (usb_endpoint_dir_in(endpoint) &&
5739 usb_endpoint_xfer_bulk(endpoint)) {
5740 if (rtl8xxxu_debug & RTL8XXXU_DEBUG_USB)
5741 dev_dbg(dev, "%s: in endpoint num %i\n",
5742 __func__, num);
5743
5744 if (priv->pipe_in) {
5745 dev_warn(dev,
5746 "%s: Too many IN pipes\n", __func__);
5747 ret = -EINVAL;
5748 goto exit;
5749 }
5750
5751 priv->pipe_in = usb_rcvbulkpipe(priv->udev, num);
5752 }
5753
5754 if (usb_endpoint_dir_in(endpoint) &&
5755 usb_endpoint_xfer_int(endpoint)) {
5756 if (rtl8xxxu_debug & RTL8XXXU_DEBUG_USB)
5757 dev_dbg(dev, "%s: interrupt endpoint num %i\n",
5758 __func__, num);
5759
5760 if (priv->pipe_interrupt) {
5761 dev_warn(dev, "%s: Too many INTERRUPT pipes\n",
5762 __func__);
5763 ret = -EINVAL;
5764 goto exit;
5765 }
5766
5767 priv->pipe_interrupt = usb_rcvintpipe(priv->udev, num);
5768 }
5769
5770 if (usb_endpoint_dir_out(endpoint) &&
5771 usb_endpoint_xfer_bulk(endpoint)) {
5772 if (rtl8xxxu_debug & RTL8XXXU_DEBUG_USB)
5773 dev_dbg(dev, "%s: out endpoint num %i\n",
5774 __func__, num);
5775 if (j >= RTL8XXXU_OUT_ENDPOINTS) {
5776 dev_warn(dev,
5777 "%s: Too many OUT pipes\n", __func__);
5778 ret = -EINVAL;
5779 goto exit;
5780 }
5781 priv->out_ep[j++] = num;
5782 }
5783 }
5784exit:
5785 priv->nr_out_eps = j;
5786 return ret;
5787}
5788
5789static int rtl8xxxu_probe(struct usb_interface *interface,
5790 const struct usb_device_id *id)
5791{
5792 struct rtl8xxxu_priv *priv;
5793 struct ieee80211_hw *hw;
5794 struct usb_device *udev;
5795 struct ieee80211_supported_band *sband;
5796 int ret = 0;
5797 int untested = 1;
5798
5799 udev = usb_get_dev(interface_to_usbdev(interface));
5800
5801 switch (id->idVendor) {
5802 case USB_VENDOR_ID_REALTEK:
5803 switch(id->idProduct) {
5804 case 0x1724:
5805 case 0x8176:
5806 case 0x8178:
5807 case 0x817f:
5808 untested = 0;
5809 break;
5810 }
5811 break;
5812 case 0x7392:
5813 if (id->idProduct == 0x7811)
5814 untested = 0;
5815 break;
Jes Sorensene1d70c92016-04-14 16:37:06 -04005816 case 0x050d:
5817 if (id->idProduct == 0x1004)
5818 untested = 0;
5819 break;
Jes Sorensen26f1fad2015-10-14 20:44:51 -04005820 default:
5821 break;
5822 }
5823
5824 if (untested) {
Jes Sorenseneaa4d142016-02-29 17:04:31 -05005825 rtl8xxxu_debug |= RTL8XXXU_DEBUG_EFUSE;
Jes Sorensen26f1fad2015-10-14 20:44:51 -04005826 dev_info(&udev->dev,
5827 "This Realtek USB WiFi dongle (0x%04x:0x%04x) is untested!\n",
5828 id->idVendor, id->idProduct);
5829 dev_info(&udev->dev,
5830 "Please report results to Jes.Sorensen@gmail.com\n");
5831 }
5832
5833 hw = ieee80211_alloc_hw(sizeof(struct rtl8xxxu_priv), &rtl8xxxu_ops);
5834 if (!hw) {
5835 ret = -ENOMEM;
5836 goto exit;
5837 }
5838
5839 priv = hw->priv;
5840 priv->hw = hw;
5841 priv->udev = udev;
5842 priv->fops = (struct rtl8xxxu_fileops *)id->driver_info;
5843 mutex_init(&priv->usb_buf_mutex);
5844 mutex_init(&priv->h2c_mutex);
5845 INIT_LIST_HEAD(&priv->tx_urb_free_list);
5846 spin_lock_init(&priv->tx_urb_lock);
5847 INIT_LIST_HEAD(&priv->rx_urb_pending_list);
5848 spin_lock_init(&priv->rx_urb_lock);
5849 INIT_WORK(&priv->rx_urb_wq, rtl8xxxu_rx_urb_work);
5850
5851 usb_set_intfdata(interface, hw);
5852
5853 ret = rtl8xxxu_parse_usb(priv, interface);
5854 if (ret)
5855 goto exit;
5856
5857 ret = rtl8xxxu_identify_chip(priv);
5858 if (ret) {
5859 dev_err(&udev->dev, "Fatal - failed to identify chip\n");
5860 goto exit;
5861 }
5862
5863 ret = rtl8xxxu_read_efuse(priv);
5864 if (ret) {
5865 dev_err(&udev->dev, "Fatal - failed to read EFuse\n");
5866 goto exit;
5867 }
5868
5869 ret = priv->fops->parse_efuse(priv);
5870 if (ret) {
5871 dev_err(&udev->dev, "Fatal - failed to parse EFuse\n");
5872 goto exit;
5873 }
5874
5875 rtl8xxxu_print_chipinfo(priv);
5876
5877 ret = priv->fops->load_firmware(priv);
5878 if (ret) {
5879 dev_err(&udev->dev, "Fatal - failed to load firmware\n");
5880 goto exit;
5881 }
5882
5883 ret = rtl8xxxu_init_device(hw);
5884
5885 hw->wiphy->max_scan_ssids = 1;
5886 hw->wiphy->max_scan_ie_len = IEEE80211_MAX_DATA_LEN;
5887 hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION);
5888 hw->queues = 4;
5889
5890 sband = &rtl8xxxu_supported_band;
5891 sband->ht_cap.ht_supported = true;
5892 sband->ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
5893 sband->ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_16;
5894 sband->ht_cap.cap = IEEE80211_HT_CAP_SGI_20 | IEEE80211_HT_CAP_SGI_40;
5895 memset(&sband->ht_cap.mcs, 0, sizeof(sband->ht_cap.mcs));
5896 sband->ht_cap.mcs.rx_mask[0] = 0xff;
5897 sband->ht_cap.mcs.rx_mask[4] = 0x01;
5898 if (priv->rf_paths > 1) {
5899 sband->ht_cap.mcs.rx_mask[1] = 0xff;
5900 sband->ht_cap.cap |= IEEE80211_HT_CAP_SGI_40;
5901 }
5902 sband->ht_cap.mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
5903 /*
5904 * Some APs will negotiate HT20_40 in a noisy environment leading
5905 * to miserable performance. Rather than defaulting to this, only
5906 * enable it if explicitly requested at module load time.
5907 */
5908 if (rtl8xxxu_ht40_2g) {
5909 dev_info(&udev->dev, "Enabling HT_20_40 on the 2.4GHz band\n");
5910 sband->ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
5911 }
Johannes Berg57fbcce2016-04-12 15:56:15 +02005912 hw->wiphy->bands[NL80211_BAND_2GHZ] = sband;
Jes Sorensen26f1fad2015-10-14 20:44:51 -04005913
5914 hw->wiphy->rts_threshold = 2347;
5915
5916 SET_IEEE80211_DEV(priv->hw, &interface->dev);
5917 SET_IEEE80211_PERM_ADDR(hw, priv->mac_addr);
5918
Jes Sorensen179e1742016-02-29 17:05:27 -05005919 hw->extra_tx_headroom = priv->fops->tx_desc_size;
Jes Sorensen26f1fad2015-10-14 20:44:51 -04005920 ieee80211_hw_set(hw, SIGNAL_DBM);
5921 /*
5922 * The firmware handles rate control
5923 */
5924 ieee80211_hw_set(hw, HAS_RATE_CONTROL);
5925 ieee80211_hw_set(hw, AMPDU_AGGREGATION);
5926
5927 ret = ieee80211_register_hw(priv->hw);
5928 if (ret) {
5929 dev_err(&udev->dev, "%s: Failed to register: %i\n",
5930 __func__, ret);
5931 goto exit;
5932 }
5933
5934exit:
5935 if (ret < 0)
5936 usb_put_dev(udev);
5937 return ret;
5938}
5939
5940static void rtl8xxxu_disconnect(struct usb_interface *interface)
5941{
5942 struct rtl8xxxu_priv *priv;
5943 struct ieee80211_hw *hw;
5944
5945 hw = usb_get_intfdata(interface);
5946 priv = hw->priv;
5947
Jes Sorensen8cae2f12016-04-14 16:37:13 -04005948 ieee80211_unregister_hw(hw);
5949
5950 priv->fops->power_off(priv);
5951
Jes Sorensen26f1fad2015-10-14 20:44:51 -04005952 usb_set_intfdata(interface, NULL);
5953
5954 dev_info(&priv->udev->dev, "disconnecting\n");
5955
Jes Sorensen26f1fad2015-10-14 20:44:51 -04005956 kfree(priv->fw_data);
5957 mutex_destroy(&priv->usb_buf_mutex);
5958 mutex_destroy(&priv->h2c_mutex);
5959
5960 usb_put_dev(priv->udev);
5961 ieee80211_free_hw(hw);
5962}
5963
Jes Sorensen26f1fad2015-10-14 20:44:51 -04005964static struct usb_device_id dev_table[] = {
5965{USB_DEVICE_AND_INTERFACE_INFO(USB_VENDOR_ID_REALTEK, 0x8724, 0xff, 0xff, 0xff),
5966 .driver_info = (unsigned long)&rtl8723au_fops},
5967{USB_DEVICE_AND_INTERFACE_INFO(USB_VENDOR_ID_REALTEK, 0x1724, 0xff, 0xff, 0xff),
5968 .driver_info = (unsigned long)&rtl8723au_fops},
5969{USB_DEVICE_AND_INTERFACE_INFO(USB_VENDOR_ID_REALTEK, 0x0724, 0xff, 0xff, 0xff),
5970 .driver_info = (unsigned long)&rtl8723au_fops},
Jes Sorensen3307d842016-02-29 17:03:59 -05005971{USB_DEVICE_AND_INTERFACE_INFO(USB_VENDOR_ID_REALTEK, 0x818b, 0xff, 0xff, 0xff),
5972 .driver_info = (unsigned long)&rtl8192eu_fops},
Jes Sorensen35a741f2016-02-29 17:04:10 -05005973{USB_DEVICE_AND_INTERFACE_INFO(USB_VENDOR_ID_REALTEK, 0xb720, 0xff, 0xff, 0xff),
5974 .driver_info = (unsigned long)&rtl8723bu_fops},
Kalle Valo033695b2015-10-23 20:27:58 +03005975#ifdef CONFIG_RTL8XXXU_UNTESTED
5976/* Still supported by rtlwifi */
Jes Sorensen26f1fad2015-10-14 20:44:51 -04005977{USB_DEVICE_AND_INTERFACE_INFO(USB_VENDOR_ID_REALTEK, 0x8176, 0xff, 0xff, 0xff),
5978 .driver_info = (unsigned long)&rtl8192cu_fops},
5979{USB_DEVICE_AND_INTERFACE_INFO(USB_VENDOR_ID_REALTEK, 0x8178, 0xff, 0xff, 0xff),
5980 .driver_info = (unsigned long)&rtl8192cu_fops},
5981{USB_DEVICE_AND_INTERFACE_INFO(USB_VENDOR_ID_REALTEK, 0x817f, 0xff, 0xff, 0xff),
5982 .driver_info = (unsigned long)&rtl8192cu_fops},
5983/* Tested by Larry Finger */
5984{USB_DEVICE_AND_INTERFACE_INFO(0x7392, 0x7811, 0xff, 0xff, 0xff),
5985 .driver_info = (unsigned long)&rtl8192cu_fops},
Jes Sorensene1d70c92016-04-14 16:37:06 -04005986/* Tested by Andrea Merello */
5987{USB_DEVICE_AND_INTERFACE_INFO(0x050d, 0x1004, 0xff, 0xff, 0xff),
5988 .driver_info = (unsigned long)&rtl8192cu_fops},
Jes Sorensen26f1fad2015-10-14 20:44:51 -04005989/* Currently untested 8188 series devices */
5990{USB_DEVICE_AND_INTERFACE_INFO(USB_VENDOR_ID_REALTEK, 0x8191, 0xff, 0xff, 0xff),
5991 .driver_info = (unsigned long)&rtl8192cu_fops},
5992{USB_DEVICE_AND_INTERFACE_INFO(USB_VENDOR_ID_REALTEK, 0x8170, 0xff, 0xff, 0xff),
5993 .driver_info = (unsigned long)&rtl8192cu_fops},
5994{USB_DEVICE_AND_INTERFACE_INFO(USB_VENDOR_ID_REALTEK, 0x8177, 0xff, 0xff, 0xff),
5995 .driver_info = (unsigned long)&rtl8192cu_fops},
5996{USB_DEVICE_AND_INTERFACE_INFO(USB_VENDOR_ID_REALTEK, 0x817a, 0xff, 0xff, 0xff),
5997 .driver_info = (unsigned long)&rtl8192cu_fops},
5998{USB_DEVICE_AND_INTERFACE_INFO(USB_VENDOR_ID_REALTEK, 0x817b, 0xff, 0xff, 0xff),
5999 .driver_info = (unsigned long)&rtl8192cu_fops},
6000{USB_DEVICE_AND_INTERFACE_INFO(USB_VENDOR_ID_REALTEK, 0x817d, 0xff, 0xff, 0xff),
6001 .driver_info = (unsigned long)&rtl8192cu_fops},
6002{USB_DEVICE_AND_INTERFACE_INFO(USB_VENDOR_ID_REALTEK, 0x817e, 0xff, 0xff, 0xff),
6003 .driver_info = (unsigned long)&rtl8192cu_fops},
6004{USB_DEVICE_AND_INTERFACE_INFO(USB_VENDOR_ID_REALTEK, 0x818a, 0xff, 0xff, 0xff),
6005 .driver_info = (unsigned long)&rtl8192cu_fops},
6006{USB_DEVICE_AND_INTERFACE_INFO(USB_VENDOR_ID_REALTEK, 0x317f, 0xff, 0xff, 0xff),
6007 .driver_info = (unsigned long)&rtl8192cu_fops},
6008{USB_DEVICE_AND_INTERFACE_INFO(0x1058, 0x0631, 0xff, 0xff, 0xff),
6009 .driver_info = (unsigned long)&rtl8192cu_fops},
6010{USB_DEVICE_AND_INTERFACE_INFO(0x04bb, 0x094c, 0xff, 0xff, 0xff),
6011 .driver_info = (unsigned long)&rtl8192cu_fops},
6012{USB_DEVICE_AND_INTERFACE_INFO(0x050d, 0x1102, 0xff, 0xff, 0xff),
6013 .driver_info = (unsigned long)&rtl8192cu_fops},
6014{USB_DEVICE_AND_INTERFACE_INFO(0x06f8, 0xe033, 0xff, 0xff, 0xff),
6015 .driver_info = (unsigned long)&rtl8192cu_fops},
6016{USB_DEVICE_AND_INTERFACE_INFO(0x07b8, 0x8189, 0xff, 0xff, 0xff),
6017 .driver_info = (unsigned long)&rtl8192cu_fops},
6018{USB_DEVICE_AND_INTERFACE_INFO(0x0846, 0x9041, 0xff, 0xff, 0xff),
6019 .driver_info = (unsigned long)&rtl8192cu_fops},
6020{USB_DEVICE_AND_INTERFACE_INFO(0x0b05, 0x17ba, 0xff, 0xff, 0xff),
6021 .driver_info = (unsigned long)&rtl8192cu_fops},
6022{USB_DEVICE_AND_INTERFACE_INFO(USB_VENDOR_ID_REALTEK, 0x1e1e, 0xff, 0xff, 0xff),
6023 .driver_info = (unsigned long)&rtl8192cu_fops},
6024{USB_DEVICE_AND_INTERFACE_INFO(USB_VENDOR_ID_REALTEK, 0x5088, 0xff, 0xff, 0xff),
6025 .driver_info = (unsigned long)&rtl8192cu_fops},
6026{USB_DEVICE_AND_INTERFACE_INFO(0x0df6, 0x0052, 0xff, 0xff, 0xff),
6027 .driver_info = (unsigned long)&rtl8192cu_fops},
6028{USB_DEVICE_AND_INTERFACE_INFO(0x0df6, 0x005c, 0xff, 0xff, 0xff),
6029 .driver_info = (unsigned long)&rtl8192cu_fops},
6030{USB_DEVICE_AND_INTERFACE_INFO(0x0eb0, 0x9071, 0xff, 0xff, 0xff),
6031 .driver_info = (unsigned long)&rtl8192cu_fops},
6032{USB_DEVICE_AND_INTERFACE_INFO(0x103c, 0x1629, 0xff, 0xff, 0xff),
6033 .driver_info = (unsigned long)&rtl8192cu_fops},
6034{USB_DEVICE_AND_INTERFACE_INFO(0x13d3, 0x3357, 0xff, 0xff, 0xff),
6035 .driver_info = (unsigned long)&rtl8192cu_fops},
6036{USB_DEVICE_AND_INTERFACE_INFO(0x2001, 0x3308, 0xff, 0xff, 0xff),
6037 .driver_info = (unsigned long)&rtl8192cu_fops},
6038{USB_DEVICE_AND_INTERFACE_INFO(0x2001, 0x330b, 0xff, 0xff, 0xff),
6039 .driver_info = (unsigned long)&rtl8192cu_fops},
6040{USB_DEVICE_AND_INTERFACE_INFO(0x2019, 0x4902, 0xff, 0xff, 0xff),
6041 .driver_info = (unsigned long)&rtl8192cu_fops},
6042{USB_DEVICE_AND_INTERFACE_INFO(0x2019, 0xab2a, 0xff, 0xff, 0xff),
6043 .driver_info = (unsigned long)&rtl8192cu_fops},
6044{USB_DEVICE_AND_INTERFACE_INFO(0x2019, 0xab2e, 0xff, 0xff, 0xff),
6045 .driver_info = (unsigned long)&rtl8192cu_fops},
6046{USB_DEVICE_AND_INTERFACE_INFO(0x2019, 0xed17, 0xff, 0xff, 0xff),
6047 .driver_info = (unsigned long)&rtl8192cu_fops},
6048{USB_DEVICE_AND_INTERFACE_INFO(0x20f4, 0x648b, 0xff, 0xff, 0xff),
6049 .driver_info = (unsigned long)&rtl8192cu_fops},
6050{USB_DEVICE_AND_INTERFACE_INFO(0x4855, 0x0090, 0xff, 0xff, 0xff),
6051 .driver_info = (unsigned long)&rtl8192cu_fops},
6052{USB_DEVICE_AND_INTERFACE_INFO(0x4856, 0x0091, 0xff, 0xff, 0xff),
6053 .driver_info = (unsigned long)&rtl8192cu_fops},
6054{USB_DEVICE_AND_INTERFACE_INFO(0xcdab, 0x8010, 0xff, 0xff, 0xff),
6055 .driver_info = (unsigned long)&rtl8192cu_fops},
Jes Sorensen26f1fad2015-10-14 20:44:51 -04006056{USB_DEVICE_AND_INTERFACE_INFO(0x04f2, 0xaff7, 0xff, 0xff, 0xff),
6057 .driver_info = (unsigned long)&rtl8192cu_fops},
6058{USB_DEVICE_AND_INTERFACE_INFO(0x04f2, 0xaff9, 0xff, 0xff, 0xff),
6059 .driver_info = (unsigned long)&rtl8192cu_fops},
6060{USB_DEVICE_AND_INTERFACE_INFO(0x04f2, 0xaffa, 0xff, 0xff, 0xff),
6061 .driver_info = (unsigned long)&rtl8192cu_fops},
6062{USB_DEVICE_AND_INTERFACE_INFO(0x04f2, 0xaff8, 0xff, 0xff, 0xff),
6063 .driver_info = (unsigned long)&rtl8192cu_fops},
6064{USB_DEVICE_AND_INTERFACE_INFO(0x04f2, 0xaffb, 0xff, 0xff, 0xff),
6065 .driver_info = (unsigned long)&rtl8192cu_fops},
6066{USB_DEVICE_AND_INTERFACE_INFO(0x04f2, 0xaffc, 0xff, 0xff, 0xff),
6067 .driver_info = (unsigned long)&rtl8192cu_fops},
6068{USB_DEVICE_AND_INTERFACE_INFO(0x2019, 0x1201, 0xff, 0xff, 0xff),
6069 .driver_info = (unsigned long)&rtl8192cu_fops},
6070/* Currently untested 8192 series devices */
6071{USB_DEVICE_AND_INTERFACE_INFO(0x04bb, 0x0950, 0xff, 0xff, 0xff),
6072 .driver_info = (unsigned long)&rtl8192cu_fops},
Jes Sorensen26f1fad2015-10-14 20:44:51 -04006073{USB_DEVICE_AND_INTERFACE_INFO(0x050d, 0x2102, 0xff, 0xff, 0xff),
6074 .driver_info = (unsigned long)&rtl8192cu_fops},
6075{USB_DEVICE_AND_INTERFACE_INFO(0x050d, 0x2103, 0xff, 0xff, 0xff),
6076 .driver_info = (unsigned long)&rtl8192cu_fops},
6077{USB_DEVICE_AND_INTERFACE_INFO(0x0586, 0x341f, 0xff, 0xff, 0xff),
6078 .driver_info = (unsigned long)&rtl8192cu_fops},
6079{USB_DEVICE_AND_INTERFACE_INFO(0x06f8, 0xe035, 0xff, 0xff, 0xff),
6080 .driver_info = (unsigned long)&rtl8192cu_fops},
6081{USB_DEVICE_AND_INTERFACE_INFO(0x0b05, 0x17ab, 0xff, 0xff, 0xff),
6082 .driver_info = (unsigned long)&rtl8192cu_fops},
6083{USB_DEVICE_AND_INTERFACE_INFO(0x0df6, 0x0061, 0xff, 0xff, 0xff),
6084 .driver_info = (unsigned long)&rtl8192cu_fops},
6085{USB_DEVICE_AND_INTERFACE_INFO(0x0df6, 0x0070, 0xff, 0xff, 0xff),
6086 .driver_info = (unsigned long)&rtl8192cu_fops},
6087{USB_DEVICE_AND_INTERFACE_INFO(0x0789, 0x016d, 0xff, 0xff, 0xff),
6088 .driver_info = (unsigned long)&rtl8192cu_fops},
6089{USB_DEVICE_AND_INTERFACE_INFO(0x07aa, 0x0056, 0xff, 0xff, 0xff),
6090 .driver_info = (unsigned long)&rtl8192cu_fops},
6091{USB_DEVICE_AND_INTERFACE_INFO(0x07b8, 0x8178, 0xff, 0xff, 0xff),
6092 .driver_info = (unsigned long)&rtl8192cu_fops},
6093{USB_DEVICE_AND_INTERFACE_INFO(0x0846, 0x9021, 0xff, 0xff, 0xff),
6094 .driver_info = (unsigned long)&rtl8192cu_fops},
6095{USB_DEVICE_AND_INTERFACE_INFO(0x0846, 0xf001, 0xff, 0xff, 0xff),
6096 .driver_info = (unsigned long)&rtl8192cu_fops},
6097{USB_DEVICE_AND_INTERFACE_INFO(USB_VENDOR_ID_REALTEK, 0x2e2e, 0xff, 0xff, 0xff),
6098 .driver_info = (unsigned long)&rtl8192cu_fops},
6099{USB_DEVICE_AND_INTERFACE_INFO(0x0e66, 0x0019, 0xff, 0xff, 0xff),
6100 .driver_info = (unsigned long)&rtl8192cu_fops},
6101{USB_DEVICE_AND_INTERFACE_INFO(0x0e66, 0x0020, 0xff, 0xff, 0xff),
6102 .driver_info = (unsigned long)&rtl8192cu_fops},
6103{USB_DEVICE_AND_INTERFACE_INFO(0x2001, 0x3307, 0xff, 0xff, 0xff),
6104 .driver_info = (unsigned long)&rtl8192cu_fops},
6105{USB_DEVICE_AND_INTERFACE_INFO(0x2001, 0x3309, 0xff, 0xff, 0xff),
6106 .driver_info = (unsigned long)&rtl8192cu_fops},
6107{USB_DEVICE_AND_INTERFACE_INFO(0x2001, 0x330a, 0xff, 0xff, 0xff),
6108 .driver_info = (unsigned long)&rtl8192cu_fops},
6109{USB_DEVICE_AND_INTERFACE_INFO(0x2019, 0xab2b, 0xff, 0xff, 0xff),
6110 .driver_info = (unsigned long)&rtl8192cu_fops},
6111{USB_DEVICE_AND_INTERFACE_INFO(0x20f4, 0x624d, 0xff, 0xff, 0xff),
6112 .driver_info = (unsigned long)&rtl8192cu_fops},
6113{USB_DEVICE_AND_INTERFACE_INFO(0x2357, 0x0100, 0xff, 0xff, 0xff),
6114 .driver_info = (unsigned long)&rtl8192cu_fops},
6115{USB_DEVICE_AND_INTERFACE_INFO(0x4855, 0x0091, 0xff, 0xff, 0xff),
6116 .driver_info = (unsigned long)&rtl8192cu_fops},
6117{USB_DEVICE_AND_INTERFACE_INFO(0x7392, 0x7822, 0xff, 0xff, 0xff),
6118 .driver_info = (unsigned long)&rtl8192cu_fops},
6119#endif
6120{ }
6121};
6122
6123static struct usb_driver rtl8xxxu_driver = {
6124 .name = DRIVER_NAME,
6125 .probe = rtl8xxxu_probe,
6126 .disconnect = rtl8xxxu_disconnect,
6127 .id_table = dev_table,
Jes Sorensen6a62f9d2016-04-14 16:37:18 -04006128 .no_dynamic_id = 1,
Jes Sorensen26f1fad2015-10-14 20:44:51 -04006129 .disable_hub_initiated_lpm = 1,
6130};
6131
6132static int __init rtl8xxxu_module_init(void)
6133{
6134 int res;
6135
6136 res = usb_register(&rtl8xxxu_driver);
6137 if (res < 0)
6138 pr_err(DRIVER_NAME ": usb_register() failed (%i)\n", res);
6139
6140 return res;
6141}
6142
6143static void __exit rtl8xxxu_module_exit(void)
6144{
6145 usb_deregister(&rtl8xxxu_driver);
6146}
6147
6148
6149MODULE_DEVICE_TABLE(usb, dev_table);
6150
6151module_init(rtl8xxxu_module_init);
6152module_exit(rtl8xxxu_module_exit);