1/*
2 * Copyright (c) 2010 Broadcom Corporation
3 *
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
11 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
13 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
14 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17#include <net/mac80211.h>
18
19#include "types.h"
20#include "d11.h"
21#include "rate.h"
22#include "phy/phy_hal.h"
23#include "channel.h"
24#include "main.h"
25#include "stf.h"
26
27#define MIN_SPATIAL_EXPANSION	0
28#define MAX_SPATIAL_EXPANSION	1
29
30#define BRCMS_STF_SS_STBC_RX(wlc) (BRCMS_ISNPHY(wlc->band) && \
31	NREV_GT(wlc->band->phyrev, 3) && NREV_LE(wlc->band->phyrev, 6))
32
33#define NSTS_1	1
34#define NSTS_2	2
35#define NSTS_3	3
36#define NSTS_4	4
37
38static const u8 txcore_default[5] = {
39	(0),			/* bitmap of the core enabled */
40	(0x01),			/* For Nsts = 1, enable core 1 */
41	(0x03),			/* For Nsts = 2, enable core 1 & 2 */
42	(0x07),			/* For Nsts = 3, enable core 1, 2 & 3 */
43	(0x0f)			/* For Nsts = 4, enable all cores */
44};
45
46static void brcms_c_stf_stbc_rx_ht_update(struct brcms_c_info *wlc, int val)
47{
48	/* MIMOPHYs rev3-6 cannot receive STBC with only one rx core active */
49	if (BRCMS_STF_SS_STBC_RX(wlc)) {
50		if ((wlc->stf->rxstreams == 1) && (val != HT_CAP_RX_STBC_NO))
51			return;
52	}
53
54	if (wlc->pub->up) {
55		brcms_c_update_beacon(wlc);
56		brcms_c_update_probe_resp(wlc, true);
57	}
58}
59
60/*
61 * every WLC_TEMPSENSE_PERIOD seconds temperature check to decide whether to
62 * turn on/off txchain.
63 */
64void brcms_c_tempsense_upd(struct brcms_c_info *wlc)
65{
66	struct brcms_phy_pub *pi = wlc->band->pi;
67	uint active_chains, txchain;
68
69	/* Check if the chip is too hot. Disable one Tx chain, if it is */
70	/* high 4 bits are for Rx chain, low 4 bits are  for Tx chain */
71	active_chains = wlc_phy_stf_chain_active_get(pi);
72	txchain = active_chains & 0xf;
73
74	if (wlc->stf->txchain == wlc->stf->hw_txchain) {
75		if (txchain && (txchain < wlc->stf->hw_txchain))
76			/* turn off 1 tx chain */
77			brcms_c_stf_txchain_set(wlc, txchain, true);
78	} else if (wlc->stf->txchain < wlc->stf->hw_txchain) {
79		if (txchain == wlc->stf->hw_txchain)
80			/* turn back on txchain */
81			brcms_c_stf_txchain_set(wlc, txchain, true);
82	}
83}
84
85void
86brcms_c_stf_ss_algo_channel_get(struct brcms_c_info *wlc, u16 *ss_algo_channel,
87			    u16 chanspec)
88{
89	struct tx_power power;
90	u8 siso_mcs_id, cdd_mcs_id, stbc_mcs_id;
91
92	/* Clear previous settings */
93	*ss_algo_channel = 0;
94
95	if (!wlc->pub->up) {
96		*ss_algo_channel = (u16) -1;
97		return;
98	}
99
100	wlc_phy_txpower_get_current(wlc->band->pi, &power,
101				    CHSPEC_CHANNEL(chanspec));
102
103	siso_mcs_id = (CHSPEC_IS40(chanspec)) ?
104	    WL_TX_POWER_MCS40_SISO_FIRST : WL_TX_POWER_MCS20_SISO_FIRST;
105	cdd_mcs_id = (CHSPEC_IS40(chanspec)) ?
106	    WL_TX_POWER_MCS40_CDD_FIRST : WL_TX_POWER_MCS20_CDD_FIRST;
107	stbc_mcs_id = (CHSPEC_IS40(chanspec)) ?
108	    WL_TX_POWER_MCS40_STBC_FIRST : WL_TX_POWER_MCS20_STBC_FIRST;
109
110	/* criteria to choose stf mode */
111
112	/*
113	 * the "+3dbm (12 0.25db units)" is to account for the fact that with
114	 * CDD, tx occurs on both chains
115	 */
116	if (power.target[siso_mcs_id] > (power.target[cdd_mcs_id] + 12))
117		setbit(ss_algo_channel, PHY_TXC1_MODE_SISO);
118	else
119		setbit(ss_algo_channel, PHY_TXC1_MODE_CDD);
120
121	/*
122	 * STBC is ORed into to algo channel as STBC requires per-packet SCB
123	 * capability check so cannot be default mode of operation. One of
124	 * SISO, CDD have to be set
125	 */
126	if (power.target[siso_mcs_id] <= (power.target[stbc_mcs_id] + 12))
127		setbit(ss_algo_channel, PHY_TXC1_MODE_STBC);
128}
129
130static bool brcms_c_stf_stbc_tx_set(struct brcms_c_info *wlc, s32 int_val)
131{
132	if ((int_val != AUTO) && (int_val != OFF) && (int_val != ON))
133		return false;
134
135	if ((int_val == ON) && (wlc->stf->txstreams == 1))
136		return false;
137
138	wlc->bandstate[BAND_2G_INDEX]->band_stf_stbc_tx = (s8) int_val;
139	wlc->bandstate[BAND_5G_INDEX]->band_stf_stbc_tx = (s8) int_val;
140
141	return true;
142}
143
144bool brcms_c_stf_stbc_rx_set(struct brcms_c_info *wlc, s32 int_val)
145{
146	if ((int_val != HT_CAP_RX_STBC_NO)
147	    && (int_val != HT_CAP_RX_STBC_ONE_STREAM))
148		return false;
149
150	if (BRCMS_STF_SS_STBC_RX(wlc)) {
151		if ((int_val != HT_CAP_RX_STBC_NO)
152		    && (wlc->stf->rxstreams == 1))
153			return false;
154	}
155
156	brcms_c_stf_stbc_rx_ht_update(wlc, int_val);
157	return true;
158}
159
160static int brcms_c_stf_txcore_set(struct brcms_c_info *wlc, u8 Nsts,
161				  u8 core_mask)
162{
163	BCMMSG(wlc->wiphy, "wl%d: Nsts %d core_mask %x\n",
164		 wlc->pub->unit, Nsts, core_mask);
165
166	if (hweight8(core_mask) > wlc->stf->txstreams)
167		core_mask = 0;
168
169	if ((hweight8(core_mask) == wlc->stf->txstreams) &&
170	    ((core_mask & ~wlc->stf->txchain)
171	     || !(core_mask & wlc->stf->txchain)))
172		core_mask = wlc->stf->txchain;
173
174	wlc->stf->txcore[Nsts] = core_mask;
175	/* Nsts = 1..4, txcore index = 1..4 */
176	if (Nsts == 1) {
177		/* Needs to update beacon and ucode generated response
178		 * frames when 1 stream core map changed
179		 */
180		wlc->stf->phytxant = core_mask << PHY_TXC_ANT_SHIFT;
181		brcms_b_txant_set(wlc->hw, wlc->stf->phytxant);
182		if (wlc->clk) {
183			brcms_c_suspend_mac_and_wait(wlc);
184			brcms_c_beacon_phytxctl_txant_upd(wlc, wlc->bcn_rspec);
185			brcms_c_enable_mac(wlc);
186		}
187	}
188
189	return 0;
190}
191
192static int brcms_c_stf_spatial_policy_set(struct brcms_c_info *wlc, int val)
193{
194	int i;
195	u8 core_mask = 0;
196
197	BCMMSG(wlc->wiphy, "wl%d: val %x\n", wlc->pub->unit, val);
198
199	wlc->stf->spatial_policy = (s8) val;
200	for (i = 1; i <= MAX_STREAMS_SUPPORTED; i++) {
201		core_mask = (val == MAX_SPATIAL_EXPANSION) ?
202		    wlc->stf->txchain : txcore_default[i];
203		brcms_c_stf_txcore_set(wlc, (u8) i, core_mask);
204	}
205	return 0;
206}
207
208/*
209 * Centralized txant update function. call it whenever wlc->stf->txant and/or
210 * wlc->stf->txchain change.
211 *
212 * Antennas are controlled by ucode indirectly, which drives PHY or GPIO to
213 * achieve various tx/rx antenna selection schemes
214 *
215 * legacy phy, bit 6 and bit 7 means antenna 0 and 1 respectively, bit6+bit7
216 * means auto(last rx).
217 * for NREV<3, bit 6 and bit 7 means antenna 0 and 1 respectively, bit6+bit7
218 * means last rx and do tx-antenna selection for SISO transmissions
219 * for NREV=3, bit 6 and bit _8_ means antenna 0 and 1 respectively, bit6+bit7
220 * means last rx and do tx-antenna selection for SISO transmissions
221 * for NREV>=7, bit 6 and bit 7 mean antenna 0 and 1 respectively, nit6+bit7
222 * means both cores active
223*/
224static void _brcms_c_stf_phy_txant_upd(struct brcms_c_info *wlc)
225{
226	s8 txant;
227
228	txant = (s8) wlc->stf->txant;
229	if (BRCMS_PHY_11N_CAP(wlc->band)) {
230		if (txant == ANT_TX_FORCE_0) {
231			wlc->stf->phytxant = PHY_TXC_ANT_0;
232		} else if (txant == ANT_TX_FORCE_1) {
233			wlc->stf->phytxant = PHY_TXC_ANT_1;
234
235			if (BRCMS_ISNPHY(wlc->band) &&
236			    NREV_GE(wlc->band->phyrev, 3)
237			    && NREV_LT(wlc->band->phyrev, 7))
238				wlc->stf->phytxant = PHY_TXC_ANT_2;
239		} else {
240			if (BRCMS_ISLCNPHY(wlc->band) ||
241			    BRCMS_ISSSLPNPHY(wlc->band))
242				wlc->stf->phytxant = PHY_TXC_LCNPHY_ANT_LAST;
243			else {
244				/* catch out of sync wlc->stf->txcore */
245				WARN_ON(wlc->stf->txchain <= 0);
246				wlc->stf->phytxant =
247				    wlc->stf->txchain << PHY_TXC_ANT_SHIFT;
248			}
249		}
250	} else {
251		if (txant == ANT_TX_FORCE_0)
252			wlc->stf->phytxant = PHY_TXC_OLD_ANT_0;
253		else if (txant == ANT_TX_FORCE_1)
254			wlc->stf->phytxant = PHY_TXC_OLD_ANT_1;
255		else
256			wlc->stf->phytxant = PHY_TXC_OLD_ANT_LAST;
257	}
258
259	brcms_b_txant_set(wlc->hw, wlc->stf->phytxant);
260}
261
262int brcms_c_stf_txchain_set(struct brcms_c_info *wlc, s32 int_val, bool force)
263{
264	u8 txchain = (u8) int_val;
265	u8 txstreams;
266	uint i;
267
268	if (wlc->stf->txchain == txchain)
269		return 0;
270
271	if ((txchain & ~wlc->stf->hw_txchain)
272	    || !(txchain & wlc->stf->hw_txchain))
273		return -EINVAL;
274
275	/*
276	 * if nrate override is configured to be non-SISO STF mode, reject
277	 * reducing txchain to 1
278	 */
279	txstreams = (u8) hweight8(txchain);
280	if (txstreams > MAX_STREAMS_SUPPORTED)
281		return -EINVAL;
282
283	wlc->stf->txchain = txchain;
284	wlc->stf->txstreams = txstreams;
285	brcms_c_stf_stbc_tx_set(wlc, wlc->band->band_stf_stbc_tx);
286	brcms_c_stf_ss_update(wlc, wlc->bandstate[BAND_2G_INDEX]);
287	brcms_c_stf_ss_update(wlc, wlc->bandstate[BAND_5G_INDEX]);
288	wlc->stf->txant =
289	    (wlc->stf->txstreams == 1) ? ANT_TX_FORCE_0 : ANT_TX_DEF;
290	_brcms_c_stf_phy_txant_upd(wlc);
291
292	wlc_phy_stf_chain_set(wlc->band->pi, wlc->stf->txchain,
293			      wlc->stf->rxchain);
294
295	for (i = 1; i <= MAX_STREAMS_SUPPORTED; i++)
296		brcms_c_stf_txcore_set(wlc, (u8) i, txcore_default[i]);
297
298	return 0;
299}
300
301/*
302 * update wlc->stf->ss_opmode which represents the operational stf_ss mode
303 * we're using
304 */
305int brcms_c_stf_ss_update(struct brcms_c_info *wlc, struct brcms_band *band)
306{
307	int ret_code = 0;
308	u8 prev_stf_ss;
309	u8 upd_stf_ss;
310
311	prev_stf_ss = wlc->stf->ss_opmode;
312
313	/*
314	 * NOTE: opmode can only be SISO or CDD as STBC is decided on a
315	 * per-packet basis
316	 */
317	if (BRCMS_STBC_CAP_PHY(wlc) &&
318	    wlc->stf->ss_algosel_auto
319	    && (wlc->stf->ss_algo_channel != (u16) -1)) {
320		upd_stf_ss = (wlc->stf->txstreams == 1 ||
321			      isset(&wlc->stf->ss_algo_channel,
322				    PHY_TXC1_MODE_SISO)) ?
323				    PHY_TXC1_MODE_SISO : PHY_TXC1_MODE_CDD;
324	} else {
325		if (wlc->band != band)
326			return ret_code;
327		upd_stf_ss = (wlc->stf->txstreams == 1) ?
328				PHY_TXC1_MODE_SISO : band->band_stf_ss_mode;
329	}
330	if (prev_stf_ss != upd_stf_ss) {
331		wlc->stf->ss_opmode = upd_stf_ss;
332		brcms_b_band_stf_ss_set(wlc->hw, upd_stf_ss);
333	}
334
335	return ret_code;
336}
337
338int brcms_c_stf_attach(struct brcms_c_info *wlc)
339{
340	wlc->bandstate[BAND_2G_INDEX]->band_stf_ss_mode = PHY_TXC1_MODE_SISO;
341	wlc->bandstate[BAND_5G_INDEX]->band_stf_ss_mode = PHY_TXC1_MODE_CDD;
342
343	if (BRCMS_ISNPHY(wlc->band) &&
344	    (wlc_phy_txpower_hw_ctrl_get(wlc->band->pi) != PHY_TPC_HW_ON))
345		wlc->bandstate[BAND_2G_INDEX]->band_stf_ss_mode =
346		    PHY_TXC1_MODE_CDD;
347	brcms_c_stf_ss_update(wlc, wlc->bandstate[BAND_2G_INDEX]);
348	brcms_c_stf_ss_update(wlc, wlc->bandstate[BAND_5G_INDEX]);
349
350	brcms_c_stf_stbc_rx_ht_update(wlc, HT_CAP_RX_STBC_NO);
351	wlc->bandstate[BAND_2G_INDEX]->band_stf_stbc_tx = OFF;
352	wlc->bandstate[BAND_5G_INDEX]->band_stf_stbc_tx = OFF;
353
354	if (BRCMS_STBC_CAP_PHY(wlc)) {
355		wlc->stf->ss_algosel_auto = true;
356		/* Init the default value */
357		wlc->stf->ss_algo_channel = (u16) -1;
358	}
359	return 0;
360}
361
362void brcms_c_stf_detach(struct brcms_c_info *wlc)
363{
364}
365
366void brcms_c_stf_phy_txant_upd(struct brcms_c_info *wlc)
367{
368	_brcms_c_stf_phy_txant_upd(wlc);
369}
370
371void brcms_c_stf_phy_chain_calc(struct brcms_c_info *wlc)
372{
373	/* get available rx/tx chains */
374	wlc->stf->hw_txchain = (u8) getintvar(wlc->hw->sih, BRCMS_SROM_TXCHAIN);
375	wlc->stf->hw_rxchain = (u8) getintvar(wlc->hw->sih, BRCMS_SROM_RXCHAIN);
376
377	/* these parameter are intended to be used for all PHY types */
378	if (wlc->stf->hw_txchain == 0 || wlc->stf->hw_txchain == 0xf) {
379		if (BRCMS_ISNPHY(wlc->band))
380			wlc->stf->hw_txchain = TXCHAIN_DEF_NPHY;
381		else
382			wlc->stf->hw_txchain = TXCHAIN_DEF;
383	}
384
385	wlc->stf->txchain = wlc->stf->hw_txchain;
386	wlc->stf->txstreams = (u8) hweight8(wlc->stf->hw_txchain);
387
388	if (wlc->stf->hw_rxchain == 0 || wlc->stf->hw_rxchain == 0xf) {
389		if (BRCMS_ISNPHY(wlc->band))
390			wlc->stf->hw_rxchain = RXCHAIN_DEF_NPHY;
391		else
392			wlc->stf->hw_rxchain = RXCHAIN_DEF;
393	}
394
395	wlc->stf->rxchain = wlc->stf->hw_rxchain;
396	wlc->stf->rxstreams = (u8) hweight8(wlc->stf->hw_rxchain);
397
398	/* initialize the txcore table */
399	memcpy(wlc->stf->txcore, txcore_default, sizeof(wlc->stf->txcore));
400
401	/* default spatial_policy */
402	wlc->stf->spatial_policy = MIN_SPATIAL_EXPANSION;
403	brcms_c_stf_spatial_policy_set(wlc, MIN_SPATIAL_EXPANSION);
404}
405
406static u16 _brcms_c_stf_phytxchain_sel(struct brcms_c_info *wlc,
407				       u32 rspec)
408{
409	u16 phytxant = wlc->stf->phytxant;
410
411	if (rspec_stf(rspec) != PHY_TXC1_MODE_SISO)
412		phytxant = wlc->stf->txchain << PHY_TXC_ANT_SHIFT;
413	else if (wlc->stf->txant == ANT_TX_DEF)
414		phytxant = wlc->stf->txchain << PHY_TXC_ANT_SHIFT;
415	phytxant &= PHY_TXC_ANT_MASK;
416	return phytxant;
417}
418
419u16 brcms_c_stf_phytxchain_sel(struct brcms_c_info *wlc, u32 rspec)
420{
421	return _brcms_c_stf_phytxchain_sel(wlc, rspec);
422}
423
424u16 brcms_c_stf_d11hdrs_phyctl_txant(struct brcms_c_info *wlc, u32 rspec)
425{
426	u16 phytxant = wlc->stf->phytxant;
427	u16 mask = PHY_TXC_ANT_MASK;
428
429	/* for non-siso rates or default setting, use the available chains */
430	if (BRCMS_ISNPHY(wlc->band)) {
431		phytxant = _brcms_c_stf_phytxchain_sel(wlc, rspec);
432		mask = PHY_TXC_HTANT_MASK;
433	}
434	phytxant |= phytxant & mask;
435	return phytxant;
436}
437