ath9k: gather entropy from raw ADC I/Q samples (use the least significant bit only)
[openwrt/svn-archive/archive.git] / package / kernel / mac80211 / patches / 550-ath9k_entropy_from_adc.patch
1 --- a/drivers/net/wireless/ath/ath9k/hw.h
2 +++ b/drivers/net/wireless/ath/ath9k/hw.h
3 @@ -676,6 +676,7 @@ struct ath_spec_scan {
4 * @config_pci_powersave:
5 * @calibrate: periodic calibration for NF, ANI, IQ, ADC gain, ADC-DC
6 *
7 + * @get_adc_entropy: get entropy from the raw ADC I/Q output
8 * @spectral_scan_config: set parameters for spectral scan and enable/disable it
9 * @spectral_scan_trigger: trigger a spectral scan run
10 * @spectral_scan_wait: wait for a spectral scan run to finish
11 @@ -698,6 +699,7 @@ struct ath_hw_ops {
12 struct ath_hw_antcomb_conf *antconf);
13 void (*antdiv_comb_conf_set)(struct ath_hw *ah,
14 struct ath_hw_antcomb_conf *antconf);
15 + void (*get_adc_entropy)(struct ath_hw *ah, u8 *buf, size_t len);
16 void (*spectral_scan_config)(struct ath_hw *ah,
17 struct ath_spec_scan *param);
18 void (*spectral_scan_trigger)(struct ath_hw *ah);
19 --- a/drivers/net/wireless/ath/ath9k/ar9003_phy.c
20 +++ b/drivers/net/wireless/ath/ath9k/ar9003_phy.c
21 @@ -1609,6 +1609,26 @@ static void ar9003_hw_spectral_scan_wait
22 }
23 }
24
25 +static void ar9003_hw_get_adc_entropy(struct ath_hw *ah, u8 *buf, size_t len)
26 +{
27 + int i, j;
28 +
29 + REG_RMW_FIELD(ah, AR_PHY_TEST, AR_PHY_TEST_BBB_OBS_SEL, 1);
30 + REG_CLR_BIT(ah, AR_PHY_TEST, AR_PHY_TEST_RX_OBS_SEL_BIT5);
31 + REG_RMW_FIELD(ah, AR_PHY_TEST_CTL_STATUS, AR_PHY_TEST_CTL_RX_OBS_SEL, 0);
32 +
33 + memset(buf, 0, len);
34 + for (i = 0; i < len; i++) {
35 + for (j = 0; j < 4; j++) {
36 + u32 regval = REG_READ(ah, AR_PHY_TST_ADC);
37 +
38 + buf[i] <<= 2;
39 + buf[i] |= (regval & 1) | ((regval & BIT(10)) >> 9);
40 + udelay(1);
41 + }
42 + }
43 +}
44 +
45 void ar9003_hw_attach_phy_ops(struct ath_hw *ah)
46 {
47 struct ath_hw_private_ops *priv_ops = ath9k_hw_private_ops(ah);
48 @@ -1639,6 +1659,7 @@ void ar9003_hw_attach_phy_ops(struct ath
49 priv_ops->set_radar_params = ar9003_hw_set_radar_params;
50 priv_ops->fast_chan_change = ar9003_hw_fast_chan_change;
51
52 + ops->get_adc_entropy = ar9003_hw_get_adc_entropy;
53 ops->antdiv_comb_conf_get = ar9003_hw_antdiv_comb_conf_get;
54 ops->antdiv_comb_conf_set = ar9003_hw_antdiv_comb_conf_set;
55 ops->spectral_scan_config = ar9003_hw_spectral_scan_config;
56 --- a/drivers/net/wireless/ath/ath9k/init.c
57 +++ b/drivers/net/wireless/ath/ath9k/init.c
58 @@ -794,7 +794,8 @@ static void ath9k_init_txpower_limits(st
59 if (ah->caps.hw_caps & ATH9K_HW_CAP_5GHZ)
60 ath9k_init_band_txpower(sc, IEEE80211_BAND_5GHZ);
61
62 - ah->curchan = curchan;
63 + if (curchan)
64 + ah->curchan = curchan;
65 }
66
67 void ath9k_reload_chainmask_settings(struct ath_softc *sc)
68 @@ -940,6 +941,18 @@ void ath9k_set_hw_capab(struct ath_softc
69 SET_IEEE80211_PERM_ADDR(hw, common->macaddr);
70 }
71
72 +static void ath_get_initial_entropy(struct ath_softc *sc)
73 +{
74 + struct ath_hw *ah = sc->sc_ah;
75 + char buf[256];
76 +
77 + /* reuse last channel initialized by the tx power test */
78 + ath9k_hw_reset(ah, ah->curchan, NULL, false);
79 +
80 + ath9k_hw_get_adc_entropy(ah, buf, sizeof(buf));
81 + add_device_randomness(buf, sizeof(buf));
82 +}
83 +
84 int ath9k_init_device(u16 devid, struct ath_softc *sc,
85 const struct ath_bus_ops *bus_ops)
86 {
87 @@ -985,6 +998,8 @@ int ath9k_init_device(u16 devid, struct
88 ARRAY_SIZE(ath9k_tpt_blink));
89 #endif
90
91 + ath_get_initial_entropy(sc);
92 +
93 /* Register with mac80211 */
94 error = ieee80211_register_hw(hw);
95 if (error)
96 --- a/drivers/net/wireless/ath/ath9k/hw-ops.h
97 +++ b/drivers/net/wireless/ath/ath9k/hw-ops.h
98 @@ -78,6 +78,12 @@ static inline void ath9k_hw_antdiv_comb_
99 ath9k_hw_ops(ah)->antdiv_comb_conf_set(ah, antconf);
100 }
101
102 +static inline void ath9k_hw_get_adc_entropy(struct ath_hw *ah,
103 + u8 *buf, size_t len)
104 +{
105 + ath9k_hw_ops(ah)->get_adc_entropy(ah, buf, len);
106 +}
107 +
108 #ifdef CPTCFG_ATH9K_BTCOEX_SUPPORT
109
110 static inline void ath9k_hw_set_bt_ant_diversity(struct ath_hw *ah, bool enable)
111 --- a/drivers/net/wireless/ath/ath9k/link.c
112 +++ b/drivers/net/wireless/ath/ath9k/link.c
113 @@ -342,6 +342,11 @@ void ath_ani_calibrate(unsigned long dat
114 unsigned int timestamp = jiffies_to_msecs(jiffies);
115 u32 cal_interval, short_cal_interval, long_cal_interval;
116 unsigned long flags;
117 + char buf[256];
118 +
119 + /* gather entropy */
120 + ath9k_hw_get_adc_entropy(ah, buf, sizeof(buf));
121 + add_device_randomness(buf, sizeof(buf));
122
123 if (ah->caldata && ah->caldata->nfcal_interference)
124 long_cal_interval = ATH_LONG_CALINTERVAL_INT;
125 --- a/drivers/net/wireless/ath/ath9k/ar5008_phy.c
126 +++ b/drivers/net/wireless/ath/ath9k/ar5008_phy.c
127 @@ -1299,9 +1299,30 @@ static void ar5008_hw_set_radar_conf(str
128 conf->radar_inband = 8;
129 }
130
131 +static void ar5008_hw_get_adc_entropy(struct ath_hw *ah, u8 *buf, size_t len)
132 +{
133 + int i, j;
134 +
135 + REG_RMW_FIELD(ah, AR_PHY_TEST, AR_PHY_TEST_BBB_OBS_SEL, 1);
136 + REG_CLR_BIT(ah, AR_PHY_TEST, AR_PHY_TEST_RX_OBS_SEL_BIT5);
137 + REG_RMW_FIELD(ah, AR_PHY_TEST2, AR_PHY_TEST2_RX_OBS_SEL, 0);
138 +
139 + memset(buf, 0, len);
140 + for (i = 0; i < len; i++) {
141 + for (j = 0; j < 4; j++) {
142 + u32 regval = REG_READ(ah, AR_PHY_TST_ADC);
143 +
144 + buf[i] <<= 2;
145 + buf[i] |= (regval & 1) | ((regval & BIT(9)) >> 8);
146 + udelay(1);
147 + }
148 + }
149 +}
150 +
151 int ar5008_hw_attach_phy_ops(struct ath_hw *ah)
152 {
153 struct ath_hw_private_ops *priv_ops = ath9k_hw_private_ops(ah);
154 + struct ath_hw_ops *ops = ath9k_hw_ops(ah);
155 static const u32 ar5416_cca_regs[6] = {
156 AR_PHY_CCA,
157 AR_PHY_CH1_CCA,
158 @@ -1316,6 +1337,8 @@ int ar5008_hw_attach_phy_ops(struct ath_
159 if (ret)
160 return ret;
161
162 + ops->get_adc_entropy = ar5008_hw_get_adc_entropy;
163 +
164 priv_ops->rf_set_freq = ar5008_hw_set_channel;
165 priv_ops->spur_mitigate_freq = ar5008_hw_spur_mitigate;
166
167 --- a/drivers/net/wireless/ath/ath9k/ar9002_phy.h
168 +++ b/drivers/net/wireless/ath/ath9k/ar9002_phy.h
169 @@ -20,6 +20,12 @@
170 #define PHY_AGC_CLR 0x10000000
171 #define RFSILENT_BB 0x00002000
172
173 +#define AR_PHY_TEST_BBB_OBS_SEL 0x780000
174 +#define AR_PHY_TEST_BBB_OBS_SEL_S 19
175 +
176 +#define AR_PHY_TEST_RX_OBS_SEL_BIT5_S 23
177 +#define AR_PHY_TEST_RX_OBS_SEL_BIT5 (1 << AR_PHY_TEST_RX_OBS_SEL_BIT5_S)
178 +
179 #define AR_PHY_TURBO 0x9804
180 #define AR_PHY_FC_TURBO_MODE 0x00000001
181 #define AR_PHY_FC_TURBO_SHORT 0x00000002
182 @@ -36,6 +42,9 @@
183
184 #define AR_PHY_TEST2 0x9808
185
186 +#define AR_PHY_TEST2_RX_OBS_SEL 0x3C00
187 +#define AR_PHY_TEST2_RX_OBS_SEL_S 10
188 +
189 #define AR_PHY_TIMING2 0x9810
190 #define AR_PHY_TIMING3 0x9814
191 #define AR_PHY_TIMING3_DSC_MAN 0xFFFE0000
192 @@ -390,6 +399,8 @@
193 #define AR_PHY_RFBUS_GRANT 0x9C20
194 #define AR_PHY_RFBUS_GRANT_EN 0x00000001
195
196 +#define AR_PHY_TST_ADC 0x9C24
197 +
198 #define AR_PHY_CHAN_INFO_GAIN_DIFF 0x9CF4
199 #define AR_PHY_CHAN_INFO_GAIN_DIFF_UPPER_LIMIT 320
200