xref: /openwifi/driver/sdr.h (revision 109b1cfd3a647e0550020bbd01dff1346ca9b8f4)
1 // Author: Xianjun Jiao, Michael Mehari, Wei Liu
2 // SPDX-FileCopyrightText: 2019 UGent
3 // SPDX-License-Identifier: AGPL-3.0-or-later
4 
5 #ifndef OPENWIFI_SDR
6 #define OPENWIFI_SDR
7 
8 #include "pre_def.h"
9 
10 // -------------------for leds--------------------------------
11 struct gpio_led_data { //please always align with the leds-gpio.c in linux kernel
12 	struct led_classdev cdev;
13 	struct gpio_desc *gpiod;
14 	u8 can_sleep;
15 	u8 blinking;
16 	gpio_blink_set_t platform_gpio_blink_set;
17 };
18 
19 struct gpio_leds_priv { //please always align with the leds-gpio.c in linux kernel
20 	int num_leds;
21 	struct gpio_led_data leds[];
22 };
23 
24 struct openwifi_rf_ops {
25 	char *name;
26 //	void (*init)(struct ieee80211_hw *);
27 //	void (*stop)(struct ieee80211_hw *);
28 	void (*set_chan)(struct ieee80211_hw *, struct ieee80211_conf *);
29 //	u8 (*calc_rssi)(u8 agc, u8 sq);
30 };
31 
32 struct openwifi_buffer_descriptor {
33 	// u32 num_dma_byte;
34     // u32 sn;
35     // u32 hw_queue_idx;
36     // u32 retry_limit;
37     // u32 need_ack;
38     struct sk_buff *skb_linked;
39     dma_addr_t dma_mapping_addr;
40     // u32 reserved;
41 } __packed;
42 
43 struct openwifi_ring {
44 	struct openwifi_buffer_descriptor *bds;
45     u32 bd_wr_idx;
46 	u32 bd_rd_idx;
47     u32 stop_flag; // track the stop/wake status between tx interrupt and openwifi_tx
48 	// u32 num_dma_symbol_request;
49 	// u32 reserved;
50 } __packed;
51 
52 struct openwifi_vif {
53 	struct ieee80211_hw *dev;
54 
55 	int idx; // this vif's idx on the dev
56 
57 	/* beaconing */
58 	struct delayed_work beacon_work;
59 	bool enable_beacon;
60 };
61 
62 union u32_byte4 {
63 	u32 a;
64 	u8 c[4];
65 };
66 union u16_byte2 {
67 	u16 a;
68 	u8 c[2];
69 };
70 
71 #define MAX_NUM_LED 4
72 #define OPENWIFI_LED_MAX_NAME_LEN 32
73 
74 // ------------ software reg definition ------------
75 #define MAX_NUM_DRV_REG            8
76 #define DRV_TX_REG_IDX_RATE        0
77 #define DRV_TX_REG_IDX_FREQ_BW_CFG 1
78 #define DRV_TX_REG_IDX_PRINT_CFG   (MAX_NUM_DRV_REG-1)
79 
80 #define DRV_RX_REG_IDX_FREQ_BW_CFG 1
81 #define DRV_RX_REG_IDX_EXTRA_FO    2
82 #define DRV_RX_REG_IDX_PRINT_CFG   (MAX_NUM_DRV_REG-1)
83 
84 #define DRV_XPU_REG_IDX_GIT_REV    (MAX_NUM_DRV_REG-1)
85 
86 // ------end of software reg definition ------------
87 
88 #define MAX_NUM_VIF 4
89 
90 #define LEN_PHY_HEADER 16
91 #define LEN_PHY_CRC 4
92 
93 #define RING_ROOM_THRESHOLD 4
94 #define NUM_TX_BD 64 // !!! should align to the fifo size in tx_bit_intf.v
95 
96 #ifdef USE_NEW_RX_INTERRUPT
97 #define NUM_RX_BD 8
98 #else
99 #define NUM_RX_BD 16
100 #endif
101 
102 #define TX_BD_BUF_SIZE (8192)
103 #define RX_BD_BUF_SIZE (8192)
104 
105 #define NUM_BIT_MAX_NUM_HW_QUEUE 2
106 #define MAX_NUM_HW_QUEUE 4 // number of queue in FPGA
107 #define MAX_NUM_SW_QUEUE 4 // number of queue in Linux, depends on the number we report by dev->queues in openwifi_dev_probe
108 #define NUM_BIT_MAX_PHY_TX_SN 10 // decrease 12 to 10 to reserve 2 bits storing related linux prio idx
109 #define MAX_PHY_TX_SN ((1<<NUM_BIT_MAX_PHY_TX_SN)-1)
110 
111 #define AD9361_RADIO_OFF_TX_ATT 89750 //please align with ad9361.c
112 #define AD9361_RADIO_ON_TX_ATT 000    //please align with rf_init.sh
113 
114 #define SDR_SUPPORTED_FILTERS	\
115 	(FIF_ALLMULTI |				\
116 	FIF_BCN_PRBRESP_PROMISC |	\
117 	FIF_CONTROL |				\
118 	FIF_OTHER_BSS |				\
119 	FIF_PSPOLL |				\
120 	FIF_PROBE_REQ)
121 
122 #define HIGH_PRIORITY_DISCARD_FLAG ((~0x040)<<16) // don't force drop OTHER_BSS by high priority discard
123 //#define HIGH_PRIORITY_DISCARD_FLAG ((~0x140)<<16) // don't force drop OTHER_BSS and PROB_REQ by high priority discard
124 
125 /* 5G chan 36 - chan 64*/
126 #define SDR_5GHZ_CH36_64	\
127 	REG_RULE(5150-10, 5350+10, 80, 0, 20, 0)
128 /* 5G chan 36 - chan 48*/
129 #define SDR_5GHZ_CH36_48	\
130 	REG_RULE(5150-10, 5270+10, 80, 0, 20, 0)
131 
132 /*
133  *Only these channels all allow active
134  *scan on all world regulatory domains
135  */
136 #define SDR_2GHZ_CH01_13	REG_RULE(2412-10, 2472+10, 40, 0, 20, NL80211_RRF_NO_CCK) // disable 11b
137 #define SDR_2GHZ_CH01_14	REG_RULE(2412-10, 2484+10, 40, 0, 20, NL80211_RRF_NO_CCK) // disable 11b
138 
139 // regulatory.h alpha2
140 //  *	00 - World regulatory domain
141 //  *	99 - built by driver but a specific alpha2 cannot be determined
142 //  *	98 - result of an intersection between two regulatory domains
143 //  *	97 - regulatory domain has not yet been configured
144 static const struct ieee80211_regdomain sdr_regd = { // for wiphy_apply_custom_regulatory
145 	.n_reg_rules = 2,
146 	.alpha2 = "99",
147 	.dfs_region = NL80211_DFS_ETSI,
148 	.reg_rules = {
149 		//SDR_2GHZ_CH01_13,
150 		//SDR_5GHZ_CH36_48, //Avoid radar!
151 		SDR_2GHZ_CH01_14,
152 		SDR_5GHZ_CH36_64,
153 		}
154 };
155 
156 #define CHAN2G(_channel, _freq, _flags) { \
157 	.band			= NL80211_BAND_2GHZ, \
158 	.hw_value		= (_channel), \
159 	.center_freq		= (_freq), \
160 	.flags			= (_flags), \
161 	.max_antenna_gain	= 0, \
162 	.max_power		= 0, \
163 }
164 
165 #define CHAN5G(_channel, _freq, _flags) { \
166 	.band			= NL80211_BAND_5GHZ, \
167 	.hw_value		= (_channel), \
168 	.center_freq		= (_freq), \
169 	.flags			= (_flags), \
170 	.max_antenna_gain	= 0, \
171 	.max_power		= 0, \
172 }
173 
174 static const struct ieee80211_rate openwifi_5GHz_rates[] = {
175 	{ .bitrate = 10,  .hw_value = 0,  .flags = 0},
176 	{ .bitrate = 20,  .hw_value = 1,  .flags = 0},
177 	{ .bitrate = 55,  .hw_value = 2,  .flags = 0},
178 	{ .bitrate = 110, .hw_value = 3,  .flags = 0},
179 	{ .bitrate = 60,  .hw_value = 4,  .flags = IEEE80211_RATE_MANDATORY_A},
180 	{ .bitrate = 90,  .hw_value = 5,  .flags = IEEE80211_RATE_MANDATORY_A},
181 	{ .bitrate = 120, .hw_value = 6,  .flags = IEEE80211_RATE_MANDATORY_A},
182 	{ .bitrate = 180, .hw_value = 7,  .flags = IEEE80211_RATE_MANDATORY_A},
183 	{ .bitrate = 240, .hw_value = 8,  .flags = IEEE80211_RATE_MANDATORY_A},
184 	{ .bitrate = 360, .hw_value = 9,  .flags = IEEE80211_RATE_MANDATORY_A},
185 	{ .bitrate = 480, .hw_value = 10, .flags = IEEE80211_RATE_MANDATORY_A},
186 	{ .bitrate = 540, .hw_value = 11, .flags = IEEE80211_RATE_MANDATORY_A},
187 };
188 
189 static const struct ieee80211_rate openwifi_2GHz_rates[] = {
190 	{ .bitrate = 10,  .hw_value = 0,  .flags = 0},
191 	{ .bitrate = 20,  .hw_value = 1,  .flags = 0},
192 	{ .bitrate = 55,  .hw_value = 2,  .flags = 0},
193 	{ .bitrate = 110, .hw_value = 3,  .flags = 0},
194 	{ .bitrate = 60,  .hw_value = 4,  .flags = IEEE80211_RATE_MANDATORY_G|IEEE80211_RATE_ERP_G},
195 	{ .bitrate = 90,  .hw_value = 5,  .flags = IEEE80211_RATE_MANDATORY_G|IEEE80211_RATE_ERP_G},
196 	{ .bitrate = 120, .hw_value = 6,  .flags = IEEE80211_RATE_MANDATORY_G|IEEE80211_RATE_ERP_G},
197 	{ .bitrate = 180, .hw_value = 7,  .flags = IEEE80211_RATE_MANDATORY_G|IEEE80211_RATE_ERP_G},
198 	{ .bitrate = 240, .hw_value = 8,  .flags = IEEE80211_RATE_MANDATORY_G|IEEE80211_RATE_ERP_G},
199 	{ .bitrate = 360, .hw_value = 9,  .flags = IEEE80211_RATE_MANDATORY_G|IEEE80211_RATE_ERP_G},
200 	{ .bitrate = 480, .hw_value = 10, .flags = IEEE80211_RATE_MANDATORY_G|IEEE80211_RATE_ERP_G},
201 	{ .bitrate = 540, .hw_value = 11, .flags = IEEE80211_RATE_MANDATORY_G|IEEE80211_RATE_ERP_G},
202 };
203 
204 static const struct ieee80211_channel openwifi_2GHz_channels[] = {
205 	CHAN2G(1, 2412, 0),
206 	CHAN2G(2, 2417, 0),
207 	CHAN2G(3, 2422, 0),
208 	CHAN2G(4, 2427, 0),
209 	CHAN2G(5, 2432, 0),
210 	CHAN2G(6, 2437, 0),
211 	CHAN2G(7, 2442, 0),
212 	CHAN2G(8, 2447, 0),
213 	CHAN2G(9, 2452, 0),
214 	CHAN2G(10, 2457, 0),
215 	CHAN2G(11, 2462, 0),
216 	CHAN2G(12, 2467, 0),
217 	CHAN2G(13, 2472, 0),
218 	CHAN2G(14, 2484, 0),
219 };
220 
221 static const struct ieee80211_channel openwifi_5GHz_channels[] = {
222 	CHAN5G(36, 5180, 0),
223 	CHAN5G(38, 5190, 0),
224 	CHAN5G(40, 5200, 0),
225 	CHAN5G(42, 5210, 0),
226 	CHAN5G(44, 5220, 0),
227 	CHAN5G(46, 5230, 0),
228 	CHAN5G(48, 5240, 0),
229 	CHAN5G(52, 5260, IEEE80211_CHAN_RADAR),
230 	CHAN5G(56, 5280, IEEE80211_CHAN_RADAR),
231 	CHAN5G(60, 5300, IEEE80211_CHAN_RADAR),
232 	CHAN5G(64, 5320, IEEE80211_CHAN_RADAR),
233 	// CHAN5G(100, 5500, 0),
234 	// CHAN5G(104, 5520, 0),
235 	// CHAN5G(108, 5540, 0),
236 	// CHAN5G(112, 5560, 0),
237 	// CHAN5G(116, 5580, 0),
238 	// CHAN5G(120, 5600, 0),
239 	// CHAN5G(124, 5620, 0),
240 	// CHAN5G(128, 5640, 0),
241 	// CHAN5G(132, 5660, 0),
242 	// CHAN5G(136, 5680, 0),
243 	// CHAN5G(140, 5700, 0),
244 	// CHAN5G(144, 5720, 0),
245 	// CHAN5G(149, 5745, 0),
246 	// CHAN5G(153, 5765, 0),
247 	// CHAN5G(157, 5785, 0),
248 	// CHAN5G(161, 5805, 0),
249 	// CHAN5G(165, 5825, 0),
250 	// CHAN5G(169, 5845, 0),
251 };
252 
253 static const struct ieee80211_iface_limit openwifi_if_limits[] = {
254 	{ .max = 2048,	.types = BIT(NL80211_IFTYPE_STATION) },
255 	{ .max = 4,	.types =
256 #ifdef CONFIG_MAC80211_MESH
257 				 BIT(NL80211_IFTYPE_MESH_POINT) |
258 #endif
259 				 BIT(NL80211_IFTYPE_AP) },
260 };
261 
262 static const struct ieee80211_iface_combination openwifi_if_comb = {
263 	.limits = openwifi_if_limits,
264 	.n_limits = ARRAY_SIZE(openwifi_if_limits),
265 	.max_interfaces = 2048,
266 	.num_different_channels = 1,
267 	.radar_detect_widths =	BIT(NL80211_CHAN_WIDTH_20_NOHT) |
268 					BIT(NL80211_CHAN_WIDTH_20) |
269 					BIT(NL80211_CHAN_WIDTH_40) |
270 					BIT(NL80211_CHAN_WIDTH_80),
271 };
272 
273 static const u8  wifi_rate_table_mapping[24] =     { 0,  0,  0,   0,  0,  0,   0,   0,  10,   8,   6,   4,  11,   9,   7,  5,   0,    1,   2,   3,   4,   5,   6,   7};
274 static const u16 wifi_rate_table[24] =             { 0,  0,  0,   0,  0,  0,   0,   0, 480, 240, 120,  60, 540, 360, 180, 90,  65,  130, 195, 260, 390, 520, 585, 650};
275 static const u16 wifi_rate_all[20] =               {10, 20, 55, 110, 60, 90, 120, 180, 240, 360, 480, 540,  65, 130, 195, 260, 390, 520, 585, 650};
276 static const u8  wifi_mcs_table_11b_force_up[16] = {11, 11, 11,  11, 11, 15,  10,  14,   9,  13,   8,  12,   0,   0,   0,  0};
277 static const u16 wifi_n_dbps_table[16] =           {24, 24, 24,  24, 24, 36,  48,  72,  96, 144, 192, 216,   0,   0,   0,  0};
278 static const u16 wifi_n_dbps_ht_table[16] =        {26, 26, 26,  26, 26, 52,  78, 104, 156, 208, 234, 260,   0,   0,   0,  0};
279 // static const u8 wifi_mcs_table[8] =             {6,9,12,18,24,36,48,54};
280 // static const u8 wifi_mcs_table_phy_tx[8]    =   {11,15,10,14,9,13,8,12};
281 
282 // ===== copy from adi-linux/drivers/iio/frequency/cf_axi_dds.c =====
283 struct cf_axi_dds_state {
284 	struct device			*dev_spi;
285 	struct clk			*clk;
286 	struct cf_axi_dds_chip_info	*chip_info;
287 	struct gpio_desc		*plddrbypass_gpio;
288 	struct gpio_desc		*interpolation_gpio;
289 
290 	bool				standalone;
291 	bool				dp_disable;
292 	bool				enable;
293 	bool				pl_dma_fifo_en;
294 	enum fifo_ctrl			gpio_dma_fifo_ctrl;
295 
296 	struct iio_info			iio_info;
297 	size_t				regs_size;
298 	void __iomem			*regs;
299 	void __iomem			*slave_regs;
300 	void __iomem			*master_regs;
301 	u64				dac_clk;
302 	unsigned int			ddr_dds_interp_en;
303 	unsigned int			cached_freq[16];
304 	unsigned int			version;
305 	unsigned int			have_slave_channels;
306 	unsigned int			interpolation_factor;
307 	struct notifier_block		clk_nb;
308 };
309 // ===== end of copy from adi-linux/drivers/iio/frequency/cf_axi_dds.c =====
310 
311 #define RX_DMA_CYCLIC_MODE
312 struct openwifi_priv {
313 	struct platform_device *pdev;
314 	struct ieee80211_vif *vif[MAX_NUM_VIF];
315 
316 	const struct openwifi_rf_ops *rf;
317 	enum openwifi_fpga_type fpga_type;
318 
319 	struct cf_axi_dds_state *dds_st;  //axi_ad9361 hdl ref design module, dac channel
320 	struct axiadc_state *adc_st;      //axi_ad9361 hdl ref design module, adc channel
321 	struct ad9361_rf_phy *ad9361_phy; //ad9361 chip
322 	struct ctrl_outs_control ctrl_out;
323 
324 	int rx_freq_offset_to_lo_MHz;
325 	int tx_freq_offset_to_lo_MHz;
326 	u32 rf_bw;
327 	u32 actual_rx_lo;
328 
329 	struct ieee80211_rate rates_2GHz[12];
330 	struct ieee80211_rate rates_5GHz[12];
331 	struct ieee80211_channel channels_2GHz[14];
332 	struct ieee80211_channel channels_5GHz[11];
333 	struct ieee80211_supported_band band_2GHz;
334 	struct ieee80211_supported_band band_5GHz;
335 	bool rfkill_off;
336 
337 	int rssi_correction; // dynamic RSSI correction according to current channel in _rf_set_channel()
338 
339 	enum rx_intf_mode rx_intf_cfg;
340 	enum tx_intf_mode tx_intf_cfg;
341 	enum openofdm_rx_mode openofdm_rx_cfg;
342 	enum openofdm_tx_mode openofdm_tx_cfg;
343 	enum xpu_mode xpu_cfg;
344 
345 	int irq_rx;
346 	int irq_tx;
347 
348 	// u32 call_counter;
349 	u8 *rx_cyclic_buf;
350 	dma_addr_t rx_cyclic_buf_dma_mapping_addr;
351 	struct dma_chan *rx_chan;
352 	struct dma_async_tx_descriptor *rxd;
353 	dma_cookie_t rx_cookie;
354 
355 	struct openwifi_ring tx_ring[MAX_NUM_SW_QUEUE];
356 	struct scatterlist tx_sg;
357 	struct dma_chan *tx_chan;
358 	struct dma_async_tx_descriptor *txd;
359 	dma_cookie_t tx_cookie;
360 	// struct completion tx_dma_complete;
361 	// bool openwifi_tx_first_time_run;
362 
363 	// int phy_tx_sn;
364 	u32 slice_idx;
365 	u32 dest_mac_addr_queue_map[MAX_NUM_HW_QUEUE];
366 	u8 mac_addr[ETH_ALEN];
367 	u16 seqno;
368 
369 	bool use_short_slot;
370 	u8 band;
371 	u16 channel;
372 
373 	u32 drv_rx_reg_val[MAX_NUM_DRV_REG];
374 	u32 drv_tx_reg_val[MAX_NUM_DRV_REG];
375 	u32 drv_xpu_reg_val[MAX_NUM_DRV_REG];
376 	// u8 num_led;
377 	// struct led_classdev *led[MAX_NUM_LED];//zc706 has 4 user leds. please find openwifi_dev_probe to see how we get them.
378 	// char led_name[MAX_NUM_LED][OPENWIFI_LED_MAX_NAME_LEN];
379 
380 	spinlock_t lock;
381 };
382 
383 #endif /* OPENWIFI_SDR */
384