1 /*
2  * Copyright (C) 2016 Felix Fietkau <nbd@nbd.name>
3  * Copyright (C) 2018 Lorenzo Bianconi <lorenzo.bianconi83@gmail.com>
4  *
5  * Permission to use, copy, modify, and/or distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 #include "mt76x2.h"
19 
20 void mt76x2_mac_stop(struct mt76x02_dev *dev, bool force)
21 {
22 	bool stopped = false;
23 	u32 rts_cfg;
24 	int i;
25 
26 	mt76_clear(dev, MT_TXOP_CTRL_CFG, MT_TXOP_ED_CCA_EN);
27 	mt76_clear(dev, MT_TXOP_HLDR_ET, MT_TXOP_HLDR_TX40M_BLK_EN);
28 
29 	mt76_wr(dev, MT_MAC_SYS_CTRL, 0);
30 
31 	rts_cfg = mt76_rr(dev, MT_TX_RTS_CFG);
32 	mt76_wr(dev, MT_TX_RTS_CFG, rts_cfg & ~MT_TX_RTS_CFG_RETRY_LIMIT);
33 
34 	/* Wait for MAC to become idle */
35 	for (i = 0; i < 300; i++) {
36 		if ((mt76_rr(dev, MT_MAC_STATUS) &
37 		     (MT_MAC_STATUS_RX | MT_MAC_STATUS_TX)) ||
38 		    mt76_rr(dev, MT_BBP(IBI, 12))) {
39 			udelay(1);
40 			continue;
41 		}
42 
43 		stopped = true;
44 		break;
45 	}
46 
47 	if (force && !stopped) {
48 		mt76_set(dev, MT_BBP(CORE, 4), BIT(1));
49 		mt76_clear(dev, MT_BBP(CORE, 4), BIT(1));
50 
51 		mt76_set(dev, MT_BBP(CORE, 4), BIT(0));
52 		mt76_clear(dev, MT_BBP(CORE, 4), BIT(0));
53 	}
54 
55 	mt76_wr(dev, MT_TX_RTS_CFG, rts_cfg);
56 }
57 EXPORT_SYMBOL_GPL(mt76x2_mac_stop);
58