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_wr(dev, MT_MAC_SYS_CTRL, 0);
27 
28 	rts_cfg = mt76_rr(dev, MT_TX_RTS_CFG);
29 	mt76_wr(dev, MT_TX_RTS_CFG, rts_cfg & ~MT_TX_RTS_CFG_RETRY_LIMIT);
30 
31 	/* Wait for MAC to become idle */
32 	for (i = 0; i < 300; i++) {
33 		if ((mt76_rr(dev, MT_MAC_STATUS) &
34 		     (MT_MAC_STATUS_RX | MT_MAC_STATUS_TX)) ||
35 		    mt76_rr(dev, MT_BBP(IBI, 12))) {
36 			udelay(1);
37 			continue;
38 		}
39 
40 		stopped = true;
41 		break;
42 	}
43 
44 	if (force && !stopped) {
45 		mt76_set(dev, MT_BBP(CORE, 4), BIT(1));
46 		mt76_clear(dev, MT_BBP(CORE, 4), BIT(1));
47 
48 		mt76_set(dev, MT_BBP(CORE, 4), BIT(0));
49 		mt76_clear(dev, MT_BBP(CORE, 4), BIT(0));
50 	}
51 
52 	mt76_wr(dev, MT_TX_RTS_CFG, rts_cfg);
53 }
54 EXPORT_SYMBOL_GPL(mt76x2_mac_stop);
55