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 #include "eeprom.h"
32 
33 #define MT_CALIBRATE_INTERVAL		(4 * HZ)
34 
35 #define MT_FREQ_CAL_INIT_DELAY		(30 * HZ)
36 #define MT_FREQ_CAL_CHECK_INTERVAL	(10 * HZ)
37 #define MT_FREQ_CAL_ADJ_INTERVAL	(HZ / 2)
38 
39 #define MT_BBP_REG_VERSION		0x00
40 
41 #define MT_USB_AGGR_SIZE_LIMIT		21 /* * 1024B */
42 #define MT_USB_AGGR_TIMEOUT		0x80 /* * 33ns */
43 
44 struct mac_stats {
45 	u64 rx_stat[6];
46 	u64 tx_stat[6];
47 	u64 aggr_stat[2];
48 	u64 aggr_n[32];
49 	u64 zero_len_del[2];
50 };
51 
52 struct mt76x0_eeprom_params;
53 
54 #define MT_EE_TEMPERATURE_SLOPE		39
55 #define MT_FREQ_OFFSET_INVALID		-128
56 
57 /* addr req mask */
58 #define MT_VEND_TYPE_EEPROM	BIT(31)
59 #define MT_VEND_TYPE_CFG	BIT(30)
60 #define MT_VEND_TYPE_MASK	(MT_VEND_TYPE_EEPROM | MT_VEND_TYPE_CFG)
61 
62 #define MT_VEND_ADDR(type, n)	(MT_VEND_TYPE_##type | (n))
63 
64 enum mt_bw {
65 	MT_BW_20,
66 	MT_BW_40,
67 };
68 
69 /**
70  * struct mt76x0_dev - adapter structure
71  * @lock:		protects @wcid->tx_rate.
72  * @mutex:		ensures exclusive access from mac80211 callbacks.
73  * @reg_atomic_mutex:	ensures atomicity of indirect register accesses
74  *			(accesses to RF and BBP).
75  */
76 struct mt76x0_dev {
77 	struct mt76_dev mt76; /* must be first */
78 
79 	struct delayed_work cal_work;
80 	struct delayed_work mac_work;
81 
82 	struct mt76x0_caldata caldata;
83 
84 	struct mutex reg_atomic_mutex;
85 
86 	atomic_t avg_ampdu_len;
87 
88 	u8 agc_save;
89 
90 	bool no_2ghz;
91 
92 	struct mac_stats stats;
93 };
94 
95 static inline bool is_mt7610e(struct mt76x0_dev *dev)
96 {
97 	/* TODO */
98 	return false;
99 }
100 
101 void mt76x0_init_debugfs(struct mt76x0_dev *dev);
102 
103 /* Compatibility with mt76 */
104 #define mt76_rmw_field(_dev, _reg, _field, _val)	\
105 	mt76_rmw(_dev, _reg, _field, FIELD_PREP(_field, _val))
106 
107 /* Init */
108 struct mt76x0_dev *
109 mt76x0_alloc_device(struct device *pdev,
110 		    const struct mt76_driver_ops *drv_ops,
111 		    const struct ieee80211_ops *ops);
112 int mt76x0_init_hardware(struct mt76x0_dev *dev);
113 int mt76x0_register_device(struct mt76x0_dev *dev);
114 void mt76x0_chip_onoff(struct mt76x0_dev *dev, bool enable, bool reset);
115 
116 int mt76x0_mac_start(struct mt76x0_dev *dev);
117 void mt76x0_mac_stop(struct mt76x0_dev *dev);
118 
119 int mt76x0_config(struct ieee80211_hw *hw, u32 changed);
120 void mt76x0_bss_info_changed(struct ieee80211_hw *hw,
121 			     struct ieee80211_vif *vif,
122 			     struct ieee80211_bss_conf *info, u32 changed);
123 void mt76x0_sw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
124 		    const u8 *mac_addr);
125 void mt76x0_sw_scan_complete(struct ieee80211_hw *hw,
126 			     struct ieee80211_vif *vif);
127 int mt76x0_set_rts_threshold(struct ieee80211_hw *hw, u32 value);
128 
129 /* PHY */
130 void mt76x0_phy_init(struct mt76x0_dev *dev);
131 int mt76x0_wait_bbp_ready(struct mt76x0_dev *dev);
132 void mt76x0_agc_save(struct mt76x0_dev *dev);
133 void mt76x0_agc_restore(struct mt76x0_dev *dev);
134 int mt76x0_phy_set_channel(struct mt76x0_dev *dev,
135 			    struct cfg80211_chan_def *chandef);
136 void mt76x0_phy_recalibrate_after_assoc(struct mt76x0_dev *dev);
137 int mt76x0_phy_get_rssi(struct mt76x0_dev *dev, struct mt76x02_rxwi *rxwi);
138 void mt76x0_phy_set_txpower(struct mt76x0_dev *dev);
139 
140 /* MAC */
141 void mt76x0_mac_work(struct work_struct *work);
142 void mt76x0_mac_set_protection(struct mt76x0_dev *dev, bool legacy_prot,
143 				int ht_mode);
144 void mt76x0_mac_set_short_preamble(struct mt76x0_dev *dev, bool short_preamb);
145 void mt76x0_mac_config_tsf(struct mt76x0_dev *dev, bool enable, int interval);
146 void mt76x0_mac_set_ampdu_factor(struct mt76x0_dev *dev);
147 
148 /* TX */
149 void mt76x0_tx(struct ieee80211_hw *hw, struct ieee80211_tx_control *control,
150 		struct sk_buff *skb);
151 void mt76x0_queue_rx_skb(struct mt76_dev *mdev, enum mt76_rxq_id q,
152 			 struct sk_buff *skb);
153 
154 #endif
155