summaryrefslogtreecommitdiff
path: root/drivers/staging/rt2860/common/rtmp_mcu.c
blob: 9f03901433bbddb05c756ca4a2db9666ca2c0b38 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*
 *************************************************************************
 * Ralink Tech Inc.
 * 5F., No.36, Taiyuan St., Jhubei City,
 * Hsinchu County 302,
 * Taiwan, R.O.C.
 *
 * (c) Copyright 2002-2007, Ralink Technology, Inc.
 *
 * This program is free software; you can redistribute it and/or modify  *
 * it under the terms of the GNU General Public License as published by  *
 * the Free Software Foundation; either version 2 of the License, or     *
 * (at your option) any later version.                                   *
 *                                                                       *
 * This program is distributed in the hope that it will be useful,       *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 * GNU General Public License for more details.                          *
 *                                                                       *
 * You should have received a copy of the GNU General Public License     *
 * along with this program; if not, write to the                         *
 * Free Software Foundation, Inc.,                                       *
 * 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 *                                                                       *
 *************************************************************************

	Module Name:
	rtmp_mcu.c

	Abstract:
	Miniport generic portion header file

	Revision History:
	Who         When          What
	--------    ----------    ----------------------------------------------
*/

#include	"../rt_config.h"

#if defined(RT2860) || defined(RT3090)
#include "firmware.h"
#include "../../rt3090/firmware.h"
#endif
#ifdef RT2870
#include "../../rt3070/firmware.h"
#include "firmware_3070.h"
#endif

#include <linux/bitrev.h>

#ifdef RTMP_MAC_USB
/* */
/* RT2870 Firmware Spec only used 1 oct for version expression */
/* */
#define FIRMWARE_MINOR_VERSION	7
#endif /* RTMP_MAC_USB // */

/* New 8k byte firmware size for RT3071/RT3072 */
#define FIRMWAREIMAGE_MAX_LENGTH	0x2000
#define FIRMWAREIMAGE_LENGTH			(sizeof (FirmwareImage) / sizeof(u8))
#define FIRMWARE_MAJOR_VERSION		0

#define FIRMWAREIMAGEV1_LENGTH		0x1000
#define FIRMWAREIMAGEV2_LENGTH		0x1000

#ifdef RTMP_MAC_PCI
#define FIRMWARE_MINOR_VERSION		2
#endif /* RTMP_MAC_PCI // */

/*
	========================================================================

	Routine Description:
		erase 8051 firmware image in MAC ASIC

	Arguments:
		Adapter						Pointer to our adapter

	IRQL = PASSIVE_LEVEL

	========================================================================
*/
int RtmpAsicEraseFirmware(struct rt_rtmp_adapter *pAd)
{
	unsigned long i;

	for (i = 0; i < MAX_FIRMWARE_IMAGE_SIZE; i += 4)
		RTMP_IO_WRITE32(pAd, FIRMWARE_IMAGE_BASE + i, 0);

	return 0;
}

/*
	========================================================================

	Routine Description:
		Load 8051 firmware file into MAC ASIC

	Arguments:
		Adapter						Pointer to our adapter

	Return Value:
		NDIS_STATUS_SUCCESS         firmware image load ok
		NDIS_STATUS_FAILURE         image not found

	IRQL = PASSIVE_LEVEL

	========================================================================
*/
int RtmpAsicLoadFirmware(struct rt_rtmp_adapter *pAd)
{

	int Status = NDIS_STATUS_SUCCESS;
	u8 *pFirmwareImage = NULL;
	unsigned long FileLength, Index;
	u32 MacReg = 0;
#ifdef RTMP_MAC_USB
	u32 Version = (pAd->MACVersion >> 16);
#endif

	/* New 8k byte firmware size for RT3071/RT3072 */
	{
#ifdef RTMP_MAC_PCI
		if (IS_RT3090(pAd) || IS_RT3390(pAd)) {
			pFirmwareImage = FirmwareImage_3090;
			FileLength = FIRMWAREIMAGE_MAX_LENGTH;
		} else {
			pFirmwareImage = FirmwareImage_2860;
			FileLength = FIRMWAREIMAGE_MAX_LENGTH;
		}
#endif /* RTMP_MAC_PCI // */
#ifdef RTMP_MAC_USB
		/* the firmware image consists of two parts */
		if ((Version != 0x2860) && (Version != 0x2872) && (Version != 0x3070)) {	/* use the second part */
			/*printk("KH:Use New Version,part2\n"); */
			pFirmwareImage =
			    (u8 *)&
			    FirmwareImage_3070[FIRMWAREIMAGEV1_LENGTH];
			FileLength = FIRMWAREIMAGEV2_LENGTH;
		} else {
			/*printk("KH:Use New Version,part1\n"); */
			if (Version == 0x3070)
				pFirmwareImage = FirmwareImage_3070;
			else
				pFirmwareImage = FirmwareImage_2870;
			FileLength = FIRMWAREIMAGEV1_LENGTH;
		}
#endif /* RTMP_MAC_USB // */
	}

	RTMP_WRITE_FIRMWARE(pAd, pFirmwareImage, FileLength);

	/* check if MCU is ready */
	Index = 0;
	do {
		RTMP_IO_READ32(pAd, PBF_SYS_CTRL, &MacReg);

		if (MacReg & 0x80)
			break;

		RTMPusecDelay(1000);
	} while (Index++ < 1000);

	if (Index > 1000) {
		DBGPRINT(RT_DEBUG_ERROR,
			 ("NICLoadFirmware: MCU is not ready\n"));
		Status = NDIS_STATUS_FAILURE;
	}

	DBGPRINT(RT_DEBUG_TRACE, ("<=== %s (status=%d)\n", __func__, Status));

	return Status;
}

int RtmpAsicSendCommandToMcu(struct rt_rtmp_adapter *pAd,
			     u8 Command,
			     u8 Token, u8 Arg0, u8 Arg1)
{
	HOST_CMD_CSR_STRUC H2MCmd;
	H2M_MAILBOX_STRUC H2MMailbox;
	unsigned long i = 0;

#ifdef PCIE_PS_SUPPORT
	/* 3090F power solution 3 has hw limitation that needs to ban all mcu command */
	/* when firmware is in radio state.  For other chip doesn't have this limitation. */
	if (((IS_RT3090(pAd) || IS_RT3572(pAd) || IS_RT3390(pAd))
	     && IS_VERSION_AFTER_F(pAd)) && IS_VERSION_AFTER_F(pAd)
	    && (pAd->StaCfg.PSControl.field.rt30xxPowerMode == 3)
	    && (pAd->StaCfg.PSControl.field.EnableNewPS == TRUE)) {
		RTMP_SEM_LOCK(&pAd->McuCmdLock);
		if ((pAd->brt30xxBanMcuCmd == TRUE)
		    && (Command != WAKE_MCU_CMD) && (Command != RFOFF_MCU_CMD)) {
			RTMP_SEM_UNLOCK(&pAd->McuCmdLock);
			DBGPRINT(RT_DEBUG_TRACE,
				 (" Ban Mcu Cmd %x in sleep mode\n", Command));
			return FALSE;
		} else if ((Command == SLEEP_MCU_CMD)
			   || (Command == RFOFF_MCU_CMD)) {
			pAd->brt30xxBanMcuCmd = TRUE;
		} else if (Command != WAKE_MCU_CMD) {
			pAd->brt30xxBanMcuCmd = FALSE;
		}

		RTMP_SEM_UNLOCK(&pAd->McuCmdLock);

	}
	if (((IS_RT3090(pAd) || IS_RT3572(pAd) || IS_RT3390(pAd))
	     && IS_VERSION_AFTER_F(pAd)) && IS_VERSION_AFTER_F(pAd)
	    && (pAd->StaCfg.PSControl.field.rt30xxPowerMode == 3)
	    && (pAd->StaCfg.PSControl.field.EnableNewPS == TRUE)
	    && (Command == WAKE_MCU_CMD)) {

		do {
			RTMP_IO_FORCE_READ32(pAd, H2M_MAILBOX_CSR,
					     &H2MMailbox.word);
			if (H2MMailbox.field.Owner == 0)
				break;

			RTMPusecDelay(2);
			DBGPRINT(RT_DEBUG_INFO,
				 ("AsicSendCommanToMcu::Mail box is busy\n"));
		} while (i++ < 100);

		if (i >= 100) {
			DBGPRINT_ERR(("H2M_MAILBOX still hold by MCU. command fail\n"));
			return FALSE;
		}

		H2MMailbox.field.Owner = 1;	/* pass ownership to MCU */
		H2MMailbox.field.CmdToken = Token;
		H2MMailbox.field.HighByte = Arg1;
		H2MMailbox.field.LowByte = Arg0;
		RTMP_IO_FORCE_WRITE32(pAd, H2M_MAILBOX_CSR, H2MMailbox.word);

		H2MCmd.word = 0;
		H2MCmd.field.HostCommand = Command;
		RTMP_IO_FORCE_WRITE32(pAd, HOST_CMD_CSR, H2MCmd.word);

	} else
#endif /* PCIE_PS_SUPPORT // */
	{
		do {
			RTMP_IO_READ32(pAd, H2M_MAILBOX_CSR, &H2MMailbox.word);
			if (H2MMailbox.field.Owner == 0)
				break;

			RTMPusecDelay(2);
		} while (i++ < 100);

		if (i > 100) {
#ifdef RTMP_MAC_PCI
#endif /* RTMP_MAC_PCI // */
			{
				DBGPRINT_ERR(("H2M_MAILBOX still hold by MCU. command fail\n"));
			}
			return FALSE;
		}
#ifdef RTMP_MAC_PCI
#endif /* RTMP_MAC_PCI // */

		H2MMailbox.field.Owner = 1;	/* pass ownership to MCU */
		H2MMailbox.field.CmdToken = Token;
		H2MMailbox.field.HighByte = Arg1;
		H2MMailbox.field.LowByte = Arg0;
		RTMP_IO_WRITE32(pAd, H2M_MAILBOX_CSR, H2MMailbox.word);

		H2MCmd.word = 0;
		H2MCmd.field.HostCommand = Command;
		RTMP_IO_WRITE32(pAd, HOST_CMD_CSR, H2MCmd.word);

		if (Command != 0x80) {
		}
	}
#ifdef PCIE_PS_SUPPORT
	/* 3090 MCU Wakeup command needs more time to be stable. */
	/* Before stable, don't issue other MCU command to prevent from firmware error. */
	if (((IS_RT3090(pAd) || IS_RT3572(pAd) || IS_RT3390(pAd))
	     && IS_VERSION_AFTER_F(pAd)) && IS_VERSION_AFTER_F(pAd)
	    && (pAd->StaCfg.PSControl.field.rt30xxPowerMode == 3)
	    && (pAd->StaCfg.PSControl.field.EnableNewPS == TRUE)
	    && (Command == WAKE_MCU_CMD)) {
		RTMPusecDelay(2000);
		/*Put this is after RF programming. */
		/*NdisAcquireSpinLock(&pAd->McuCmdLock); */
		/*pAd->brt30xxBanMcuCmd = FALSE; */
		/*NdisReleaseSpinLock(&pAd->McuCmdLock); */
	}
#endif /* PCIE_PS_SUPPORT // */

	return TRUE;
}