xref: /freebsd/sys/dev/rtwn/if_rtwn_fw.c (revision b077aed33b7b6aefca7b17ddb250cf521f938613)
1 /*	$OpenBSD: if_urtwn.c,v 1.16 2011/02/10 17:26:40 jakemsr Exp $	*/
2 
3 /*-
4  * Copyright (c) 2010 Damien Bergamini <damien.bergamini@free.fr>
5  * Copyright (c) 2014 Kevin Lo <kevlo@FreeBSD.org>
6  * Copyright (c) 2015-2016 Andriy Voskoboinyk <avos@FreeBSD.org>
7  *
8  * Permission to use, copy, modify, and distribute this software for any
9  * purpose with or without fee is hereby granted, provided that the above
10  * copyright notice and this permission notice appear in all copies.
11  *
12  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19  */
20 
21 #include <sys/cdefs.h>
22 __FBSDID("$FreeBSD$");
23 
24 #include "opt_wlan.h"
25 
26 #include <sys/param.h>
27 #include <sys/lock.h>
28 #include <sys/mutex.h>
29 #include <sys/mbuf.h>
30 #include <sys/kernel.h>
31 #include <sys/socket.h>
32 #include <sys/systm.h>
33 #include <sys/malloc.h>
34 #include <sys/queue.h>
35 #include <sys/taskqueue.h>
36 #include <sys/bus.h>
37 #include <sys/endian.h>
38 #include <sys/linker.h>
39 #include <sys/firmware.h>
40 
41 #include <net/if.h>
42 #include <net/ethernet.h>
43 #include <net/if_media.h>
44 
45 #include <net80211/ieee80211_var.h>
46 #include <net80211/ieee80211_radiotap.h>
47 
48 #include <dev/rtwn/if_rtwnreg.h>
49 #include <dev/rtwn/if_rtwnvar.h>
50 
51 #include <dev/rtwn/if_rtwn_debug.h>
52 #include <dev/rtwn/if_rtwn_fw.h>
53 
54 #include <dev/rtwn/rtl8192c/r92c_reg.h>
55 
56 #ifndef RTWN_WITHOUT_UCODE
57 static int
58 rtwn_fw_loadpage(struct rtwn_softc *sc, int page, const uint8_t *buf,
59     int len)
60 {
61 	uint32_t reg;
62 	uint16_t off;
63 	int mlen, error;
64 
65 	reg = rtwn_read_4(sc, R92C_MCUFWDL);
66 	reg = RW(reg, R92C_MCUFWDL_PAGE, page);
67 	rtwn_write_4(sc, R92C_MCUFWDL, reg);
68 
69 	error = 0;
70 	off = R92C_FW_START_ADDR;
71 	while (len > 0) {
72 		if (len > R92C_FW_MAX_BLOCK_SIZE)
73 			mlen = R92C_FW_MAX_BLOCK_SIZE;
74 		else if (len > 4)
75 			mlen = 4;
76 		else
77 			mlen = 1;
78 		error = rtwn_fw_write_block(sc, buf, off, mlen);
79 		if (error != 0)
80 			break;
81 		off += mlen;
82 		buf += mlen;
83 		len -= mlen;
84 	}
85 
86 	if (error != 0) {
87 		RTWN_DPRINTF(sc, RTWN_DEBUG_FIRMWARE,
88 		    "%s: could not load firmware page %d (offset %u)\n",
89 		    __func__, page, off);
90 	}
91 
92 	return (error);
93 }
94 
95 static int
96 rtwn_fw_checksum_report(struct rtwn_softc *sc)
97 {
98 	int ntries;
99 
100 	for (ntries = 0; ntries < 25; ntries++) {
101 		if (rtwn_read_4(sc, R92C_MCUFWDL) & R92C_MCUFWDL_CHKSUM_RPT)
102 			break;
103 		rtwn_delay(sc, 10000);
104 	}
105 	if (ntries == 25) {
106 		RTWN_DPRINTF(sc, RTWN_DEBUG_FIRMWARE,
107 		    "timeout waiting for checksum report\n");
108 		return (ETIMEDOUT);
109 	}
110 
111 	return (0);
112 }
113 
114 int
115 rtwn_load_firmware(struct rtwn_softc *sc)
116 {
117 	const struct firmware *fw;
118 	const struct r92c_fw_hdr *hdr;
119 	const u_char *ptr;
120 	size_t len;
121 	int ntries, error;
122 
123 	/* Read firmware image from the filesystem. */
124 	RTWN_UNLOCK(sc);
125 	fw = firmware_get(sc->fwname);
126 	RTWN_LOCK(sc);
127 	if (fw == NULL) {
128 		device_printf(sc->sc_dev,
129 		    "failed loadfirmware of file %s\n", sc->fwname);
130 		return (ENOENT);
131 	}
132 
133 	len = fw->datasize;
134 	if (len < sizeof(*hdr) || len > sc->fwsize_limit) {
135 		device_printf(sc->sc_dev, "wrong firmware size (%zu)\n", len);
136 		error = EINVAL;
137 		goto fail;
138 	}
139 	ptr = fw->data;
140 	hdr = (const struct r92c_fw_hdr *)ptr;
141 	/* Check if there is a valid FW header and skip it. */
142 	if ((le16toh(hdr->signature) >> 4) == sc->fwsig) {
143 		sc->fwver = le16toh(hdr->version);
144 
145 		RTWN_DPRINTF(sc, RTWN_DEBUG_FIRMWARE,
146 		    "FW V%u.%u %02u-%02u %02u:%02u\n",
147 		    le16toh(hdr->version), le16toh(hdr->subversion),
148 		    hdr->month, hdr->date, hdr->hour, hdr->minute);
149 		ptr += sizeof(*hdr);
150 		len -= sizeof(*hdr);
151 	}
152 
153 	if (rtwn_read_1(sc, R92C_MCUFWDL) & R92C_MCUFWDL_RAM_DL_SEL) {
154 		rtwn_write_1(sc, R92C_MCUFWDL, 0);
155 		rtwn_fw_reset(sc, RTWN_FW_RESET_DOWNLOAD);
156 	}
157 
158 	/* Enable firmware download. */
159 	rtwn_fw_download_enable(sc, 1);
160 
161 	error = 0;	/* compiler warning */
162 	for (ntries = 0; ntries < 3; ntries++) {
163 		const u_char *curr_ptr = ptr;
164 		const int maxpages = len / R92C_FW_PAGE_SIZE;
165 		int page;
166 
167 		/* Reset the FWDL checksum. */
168 		rtwn_setbits_1(sc, R92C_MCUFWDL, 0, R92C_MCUFWDL_CHKSUM_RPT);
169 
170 		for (page = 0; page < maxpages; page++) {
171 			error = rtwn_fw_loadpage(sc, page, curr_ptr,
172 			    R92C_FW_PAGE_SIZE);
173 			if (error != 0)
174 				break;
175 			curr_ptr += R92C_FW_PAGE_SIZE;
176 		}
177 		if (page != maxpages)
178 			continue;
179 
180 		if (len % R92C_FW_PAGE_SIZE != 0) {
181 			error = rtwn_fw_loadpage(sc, page, curr_ptr,
182 			    len % R92C_FW_PAGE_SIZE);
183 			if (error != 0)
184 				continue;
185 		}
186 
187 		/* Wait for checksum report. */
188 		error = rtwn_fw_checksum_report(sc);
189 		if (error == 0)
190 			break;
191 	}
192 	if (ntries == 3) {
193 		device_printf(sc->sc_dev,
194 		    "%s: failed to upload firmware %s (error %d)\n",
195 		    __func__, sc->fwname, error);
196 		goto fail;
197 	}
198 
199 	/* Disable firmware download. */
200 	rtwn_fw_download_enable(sc, 0);
201 
202 	rtwn_setbits_4(sc, R92C_MCUFWDL, R92C_MCUFWDL_WINTINI_RDY,
203 	    R92C_MCUFWDL_RDY);
204 
205 	rtwn_fw_reset(sc, RTWN_FW_RESET_CHECKSUM);
206 
207 	/* Wait for firmware readiness. */
208 	for (ntries = 0; ntries < 20; ntries++) {
209 		if (rtwn_read_4(sc, R92C_MCUFWDL) & R92C_MCUFWDL_WINTINI_RDY)
210 			break;
211 		rtwn_delay(sc, 10000);
212 	}
213 	if (ntries == 20) {
214 		device_printf(sc->sc_dev,
215 		    "timeout waiting for firmware readiness\n");
216 		error = ETIMEDOUT;
217 		goto fail;
218 	}
219 fail:
220 	firmware_put(fw, FIRMWARE_UNLOAD);
221 	return (error);
222 }
223 #endif
224