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