xref: /freebsd/sys/dev/rtwn/if_rtwn_fw.c (revision fe75646a0234a261c0013bf1840fdac4acaf0cec)
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 #include "opt_wlan.h"
23 
24 #include <sys/param.h>
25 #include <sys/lock.h>
26 #include <sys/mutex.h>
27 #include <sys/mbuf.h>
28 #include <sys/kernel.h>
29 #include <sys/socket.h>
30 #include <sys/systm.h>
31 #include <sys/malloc.h>
32 #include <sys/queue.h>
33 #include <sys/taskqueue.h>
34 #include <sys/bus.h>
35 #include <sys/endian.h>
36 #include <sys/linker.h>
37 #include <sys/firmware.h>
38 
39 #include <net/if.h>
40 #include <net/ethernet.h>
41 #include <net/if_media.h>
42 
43 #include <net80211/ieee80211_var.h>
44 #include <net80211/ieee80211_radiotap.h>
45 
46 #include <dev/rtwn/if_rtwnreg.h>
47 #include <dev/rtwn/if_rtwnvar.h>
48 
49 #include <dev/rtwn/if_rtwn_debug.h>
50 #include <dev/rtwn/if_rtwn_fw.h>
51 
52 #include <dev/rtwn/rtl8192c/r92c_reg.h>
53 
54 #ifndef RTWN_WITHOUT_UCODE
55 static int
56 rtwn_fw_loadpage(struct rtwn_softc *sc, int page, const uint8_t *buf,
57     int len)
58 {
59 	uint32_t reg;
60 	uint16_t off;
61 	int mlen, error;
62 
63 	reg = rtwn_read_4(sc, R92C_MCUFWDL);
64 	reg = RW(reg, R92C_MCUFWDL_PAGE, page);
65 	rtwn_write_4(sc, R92C_MCUFWDL, reg);
66 
67 	error = 0;
68 	off = R92C_FW_START_ADDR;
69 	while (len > 0) {
70 		if (len > R92C_FW_MAX_BLOCK_SIZE)
71 			mlen = R92C_FW_MAX_BLOCK_SIZE;
72 		else if (len > 4)
73 			mlen = 4;
74 		else
75 			mlen = 1;
76 		error = rtwn_fw_write_block(sc, buf, off, mlen);
77 		if (error != 0)
78 			break;
79 		off += mlen;
80 		buf += mlen;
81 		len -= mlen;
82 	}
83 
84 	if (error != 0) {
85 		RTWN_DPRINTF(sc, RTWN_DEBUG_FIRMWARE,
86 		    "%s: could not load firmware page %d (offset %u)\n",
87 		    __func__, page, off);
88 	}
89 
90 	return (error);
91 }
92 
93 static int
94 rtwn_fw_checksum_report(struct rtwn_softc *sc)
95 {
96 	int ntries;
97 
98 	for (ntries = 0; ntries < 25; ntries++) {
99 		if (rtwn_read_4(sc, R92C_MCUFWDL) & R92C_MCUFWDL_CHKSUM_RPT)
100 			break;
101 		rtwn_delay(sc, 10000);
102 	}
103 	if (ntries == 25) {
104 		RTWN_DPRINTF(sc, RTWN_DEBUG_FIRMWARE,
105 		    "timeout waiting for checksum report\n");
106 		return (ETIMEDOUT);
107 	}
108 
109 	return (0);
110 }
111 
112 int
113 rtwn_load_firmware(struct rtwn_softc *sc)
114 {
115 	const struct firmware *fw;
116 	const struct r92c_fw_hdr *hdr;
117 	const u_char *ptr;
118 	size_t len;
119 	int ntries, error;
120 
121 	/* Read firmware image from the filesystem. */
122 	RTWN_UNLOCK(sc);
123 	fw = firmware_get(sc->fwname);
124 	RTWN_LOCK(sc);
125 	if (fw == NULL) {
126 		device_printf(sc->sc_dev,
127 		    "failed loadfirmware of file %s\n", sc->fwname);
128 		return (ENOENT);
129 	}
130 
131 	len = fw->datasize;
132 	if (len < sizeof(*hdr) || len > sc->fwsize_limit) {
133 		device_printf(sc->sc_dev, "wrong firmware size (%zu)\n", len);
134 		error = EINVAL;
135 		goto fail;
136 	}
137 	ptr = fw->data;
138 	hdr = (const struct r92c_fw_hdr *)ptr;
139 	/* Check if there is a valid FW header and skip it. */
140 	if ((le16toh(hdr->signature) >> 4) == sc->fwsig) {
141 		sc->fwver = le16toh(hdr->version);
142 
143 		RTWN_DPRINTF(sc, RTWN_DEBUG_FIRMWARE,
144 		    "FW V%u.%u %02u-%02u %02u:%02u\n",
145 		    le16toh(hdr->version), le16toh(hdr->subversion),
146 		    hdr->month, hdr->date, hdr->hour, hdr->minute);
147 		ptr += sizeof(*hdr);
148 		len -= sizeof(*hdr);
149 	}
150 
151 	if (rtwn_read_1(sc, R92C_MCUFWDL) & R92C_MCUFWDL_RAM_DL_SEL) {
152 		rtwn_write_1(sc, R92C_MCUFWDL, 0);
153 		rtwn_fw_reset(sc, RTWN_FW_RESET_DOWNLOAD);
154 	}
155 
156 	/* Enable firmware download. */
157 	rtwn_fw_download_enable(sc, 1);
158 
159 	error = 0;	/* compiler warning */
160 	for (ntries = 0; ntries < 3; ntries++) {
161 		const u_char *curr_ptr = ptr;
162 		const int maxpages = len / R92C_FW_PAGE_SIZE;
163 		int page;
164 
165 		/* Reset the FWDL checksum. */
166 		rtwn_setbits_1(sc, R92C_MCUFWDL, 0, R92C_MCUFWDL_CHKSUM_RPT);
167 
168 		for (page = 0; page < maxpages; page++) {
169 			error = rtwn_fw_loadpage(sc, page, curr_ptr,
170 			    R92C_FW_PAGE_SIZE);
171 			if (error != 0)
172 				break;
173 			curr_ptr += R92C_FW_PAGE_SIZE;
174 		}
175 		if (page != maxpages)
176 			continue;
177 
178 		if (len % R92C_FW_PAGE_SIZE != 0) {
179 			error = rtwn_fw_loadpage(sc, page, curr_ptr,
180 			    len % R92C_FW_PAGE_SIZE);
181 			if (error != 0)
182 				continue;
183 		}
184 
185 		/* Wait for checksum report. */
186 		error = rtwn_fw_checksum_report(sc);
187 		if (error == 0)
188 			break;
189 	}
190 	if (ntries == 3) {
191 		device_printf(sc->sc_dev,
192 		    "%s: failed to upload firmware %s (error %d)\n",
193 		    __func__, sc->fwname, error);
194 		goto fail;
195 	}
196 
197 	/* Disable firmware download. */
198 	rtwn_fw_download_enable(sc, 0);
199 
200 	rtwn_setbits_4(sc, R92C_MCUFWDL, R92C_MCUFWDL_WINTINI_RDY,
201 	    R92C_MCUFWDL_RDY);
202 
203 	rtwn_fw_reset(sc, RTWN_FW_RESET_CHECKSUM);
204 
205 	/* Wait for firmware readiness. */
206 	for (ntries = 0; ntries < 20; ntries++) {
207 		if (rtwn_read_4(sc, R92C_MCUFWDL) & R92C_MCUFWDL_WINTINI_RDY)
208 			break;
209 		rtwn_delay(sc, 10000);
210 	}
211 	if (ntries == 20) {
212 		device_printf(sc->sc_dev,
213 		    "timeout waiting for firmware readiness\n");
214 		error = ETIMEDOUT;
215 		goto fail;
216 	}
217 fail:
218 	firmware_put(fw, FIRMWARE_UNLOAD);
219 	return (error);
220 }
221 #endif
222