1/*
2 * Copyright (C) 2014 Felix Fietkau <nbd@openwrt.org>
3 * Copyright (C) 2015 Jakub Kicinski <kubakici@wp.pl>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2
7 * as published by the Free Software Foundation
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * GNU General Public License for more details.
13 */
14
15#include "mt7601u.h"
16
17int mt7601u_wait_asic_ready(struct mt7601u_dev *dev)
18{
19	int i = 100;
20	u32 val;
21
22	do {
23		if (test_bit(MT7601U_STATE_REMOVED, &dev->state))
24			return -EIO;
25
26		val = mt7601u_rr(dev, MT_MAC_CSR0);
27		if (val && ~val)
28			return 0;
29
30		udelay(10);
31	} while (i--);
32
33	return -EIO;
34}
35
36bool mt76_poll(struct mt7601u_dev *dev, u32 offset, u32 mask, u32 val,
37	       int timeout)
38{
39	u32 cur;
40
41	timeout /= 10;
42	do {
43		if (test_bit(MT7601U_STATE_REMOVED, &dev->state))
44			return false;
45
46		cur = mt7601u_rr(dev, offset) & mask;
47		if (cur == val)
48			return true;
49
50		udelay(10);
51	} while (timeout-- > 0);
52
53	dev_err(dev->dev, "Error: Time out with reg %08x\n", offset);
54
55	return false;
56}
57
58bool mt76_poll_msec(struct mt7601u_dev *dev, u32 offset, u32 mask, u32 val,
59		    int timeout)
60{
61	u32 cur;
62
63	timeout /= 10;
64	do {
65		if (test_bit(MT7601U_STATE_REMOVED, &dev->state))
66			return false;
67
68		cur = mt7601u_rr(dev, offset) & mask;
69		if (cur == val)
70			return true;
71
72		msleep(10);
73	} while (timeout-- > 0);
74
75	dev_err(dev->dev, "Error: Time out with reg %08x\n", offset);
76
77	return false;
78}
79