1 /*
2  * Copyright (C) 2014 Felix Fietkau <nbd@openwrt.org>
3  * Copyright (C) 2015 Jakub Kicinski <kubakici@wp.pl>
4  * Copyright (C) 2018 Stanislaw Gruszka <stf_xl@wp.pl>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2
8  * as published by the Free Software Foundation
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  */
15 
16 #ifndef MT76X0U_H
17 #define MT76X0U_H
18 
19 #include <linux/bitfield.h>
20 #include <linux/kernel.h>
21 #include <linux/device.h>
22 #include <linux/mutex.h>
23 #include <linux/usb.h>
24 #include <linux/completion.h>
25 #include <net/mac80211.h>
26 #include <linux/debugfs.h>
27 
28 #include "../mt76.h"
29 #include "../mt76x02_regs.h"
30 #include "../mt76x02_mac.h"
31 
32 #define MT_CALIBRATE_INTERVAL		(4 * HZ)
33 
34 #define MT_FREQ_CAL_INIT_DELAY		(30 * HZ)
35 #define MT_FREQ_CAL_CHECK_INTERVAL	(10 * HZ)
36 #define MT_FREQ_CAL_ADJ_INTERVAL	(HZ / 2)
37 
38 #define MT_BBP_REG_VERSION		0x00
39 
40 #define MT_USB_AGGR_SIZE_LIMIT		21 /* * 1024B */
41 #define MT_USB_AGGR_TIMEOUT		0x80 /* * 33ns */
42 
43 struct mac_stats {
44 	u64 rx_stat[6];
45 	u64 tx_stat[6];
46 	u64 aggr_stat[2];
47 	u64 aggr_n[32];
48 	u64 zero_len_del[2];
49 };
50 
51 struct mt76x0_eeprom_params;
52 
53 #define MT_EE_TEMPERATURE_SLOPE		39
54 #define MT_FREQ_OFFSET_INVALID		-128
55 
56 /* addr req mask */
57 #define MT_VEND_TYPE_EEPROM	BIT(31)
58 #define MT_VEND_TYPE_CFG	BIT(30)
59 #define MT_VEND_TYPE_MASK	(MT_VEND_TYPE_EEPROM | MT_VEND_TYPE_CFG)
60 
61 #define MT_VEND_ADDR(type, n)	(MT_VEND_TYPE_##type | (n))
62 
63 enum mt_bw {
64 	MT_BW_20,
65 	MT_BW_40,
66 };
67 
68 /**
69  * struct mt76x0_dev - adapter structure
70  * @lock:		protects @wcid->tx_rate.
71  * @mac_lock:		locks out mac80211's tx status and rx paths.
72  * @con_mon_lock:	protects @ap_bssid, @bcn_*, @avg_rssi.
73  * @mutex:		ensures exclusive access from mac80211 callbacks.
74  * @reg_atomic_mutex:	ensures atomicity of indirect register accesses
75  *			(accesses to RF and BBP).
76  * @hw_atomic_mutex:	ensures exclusive access to HW during critical
77  *			operations (power management, channel switch).
78  */
79 struct mt76x0_dev {
80 	struct mt76_dev mt76; /* must be first */
81 
82 	u8 data[32];
83 
84 	struct delayed_work cal_work;
85 	struct delayed_work mac_work;
86 
87 	spinlock_t mac_lock;
88 
89 	const u16 *beacon_offsets;
90 
91 	struct mt76x0_eeprom_params *ee;
92 
93 	struct mutex reg_atomic_mutex;
94 	struct mutex hw_atomic_mutex;
95 
96 	u32 debugfs_reg;
97 
98 	atomic_t avg_ampdu_len;
99 
100 	/* Connection monitoring things */
101 	spinlock_t con_mon_lock;
102 	u8 ap_bssid[ETH_ALEN];
103 
104 	s8 bcn_freq_off;
105 	u8 bcn_phy_mode;
106 
107 	int avg_rssi; /* starts at 0 and converges */
108 
109 	u8 agc_save;
110 	u16 chainmask;
111 
112 	struct mac_stats stats;
113 };
114 
115 struct mt76x0_wcid {
116 	u8 idx;
117 	u8 hw_key_idx;
118 
119 	u16 tx_rate;
120 	bool tx_rate_set;
121 	u8 tx_rate_nss;
122 };
123 
124 extern const struct ieee80211_ops mt76x0_ops;
125 
126 static inline bool is_mt7610e(struct mt76x0_dev *dev)
127 {
128 	/* TODO */
129 	return false;
130 }
131 
132 void mt76x0_init_debugfs(struct mt76x0_dev *dev);
133 
134 /* Compatibility with mt76 */
135 #define mt76_rmw_field(_dev, _reg, _field, _val)	\
136 	mt76_rmw(_dev, _reg, _field, FIELD_PREP(_field, _val))
137 
138 int mt76x0_write_reg_pairs(struct mt76x0_dev *dev, u32 base,
139 			    const struct mt76_reg_pair *data, int len);
140 int mt76x0_read_reg_pairs(struct mt76x0_dev *dev, u32 base,
141 			  struct mt76_reg_pair *data, int len);
142 int mt76x0_burst_write_regs(struct mt76x0_dev *dev, u32 offset,
143 			     const u32 *data, int n);
144 void mt76x0_addr_wr(struct mt76x0_dev *dev, const u32 offset, const u8 *addr);
145 
146 /* Init */
147 struct mt76x0_dev *mt76x0_alloc_device(struct device *dev);
148 int mt76x0_init_hardware(struct mt76x0_dev *dev);
149 int mt76x0_register_device(struct mt76x0_dev *dev);
150 void mt76x0_cleanup(struct mt76x0_dev *dev);
151 void mt76x0_chip_onoff(struct mt76x0_dev *dev, bool enable, bool reset);
152 
153 int mt76x0_mac_start(struct mt76x0_dev *dev);
154 void mt76x0_mac_stop(struct mt76x0_dev *dev);
155 
156 /* PHY */
157 void mt76x0_phy_init(struct mt76x0_dev *dev);
158 int mt76x0_wait_bbp_ready(struct mt76x0_dev *dev);
159 void mt76x0_agc_save(struct mt76x0_dev *dev);
160 void mt76x0_agc_restore(struct mt76x0_dev *dev);
161 int mt76x0_phy_set_channel(struct mt76x0_dev *dev,
162 			    struct cfg80211_chan_def *chandef);
163 void mt76x0_phy_recalibrate_after_assoc(struct mt76x0_dev *dev);
164 int mt76x0_phy_get_rssi(struct mt76x0_dev *dev, struct mt76x02_rxwi *rxwi);
165 void mt76x0_phy_con_cal_onoff(struct mt76x0_dev *dev,
166 			       struct ieee80211_bss_conf *info);
167 
168 /* MAC */
169 void mt76x0_mac_work(struct work_struct *work);
170 void mt76x0_mac_set_protection(struct mt76x0_dev *dev, bool legacy_prot,
171 				int ht_mode);
172 void mt76x0_mac_set_short_preamble(struct mt76x0_dev *dev, bool short_preamb);
173 void mt76x0_mac_config_tsf(struct mt76x0_dev *dev, bool enable, int interval);
174 void mt76x0_mac_set_ampdu_factor(struct mt76x0_dev *dev);
175 
176 /* TX */
177 void mt76x0_tx(struct ieee80211_hw *hw, struct ieee80211_tx_control *control,
178 		struct sk_buff *skb);
179 
180 void mt76x0_queue_rx_skb(struct mt76_dev *mdev, enum mt76_rxq_id q,
181 			 struct sk_buff *skb);
182 
183 int mt76x0_tx_prepare_skb(struct mt76_dev *mdev, void *data,
184 			  struct sk_buff *skb, struct mt76_queue *q,
185 			  struct mt76_wcid *wcid, struct ieee80211_sta *sta,
186 			  u32 *tx_info);
187 #endif
188