1 /* $OpenBSD: if_urtwn.c,v 1.16 2011/02/10 17:26:40 jakemsr Exp $ */
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>
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.
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.
21 #include <sys/cdefs.h>
22 __FBSDID("$FreeBSD$");
26 #include <sys/param.h>
28 #include <sys/mutex.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>
37 #include <sys/endian.h>
38 #include <sys/linker.h>
39 #include <sys/firmware.h>
42 #include <net/ethernet.h>
43 #include <net/if_media.h>
45 #include <net80211/ieee80211_var.h>
46 #include <net80211/ieee80211_radiotap.h>
48 #include <dev/rtwn/if_rtwnreg.h>
49 #include <dev/rtwn/if_rtwnvar.h>
51 #include <dev/rtwn/if_rtwn_debug.h>
52 #include <dev/rtwn/if_rtwn_fw.h>
54 #include <dev/rtwn/rtl8192c/r92c_reg.h>
57 #ifndef RTWN_WITHOUT_UCODE
59 rtwn_fw_loadpage(struct rtwn_softc *sc, int page, const uint8_t *buf,
66 reg = rtwn_read_4(sc, R92C_MCUFWDL);
67 reg = RW(reg, R92C_MCUFWDL_PAGE, page);
68 rtwn_write_4(sc, R92C_MCUFWDL, reg);
71 off = R92C_FW_START_ADDR;
73 if (len > R92C_FW_MAX_BLOCK_SIZE)
74 mlen = R92C_FW_MAX_BLOCK_SIZE;
79 error = rtwn_fw_write_block(sc, buf, off, mlen);
88 RTWN_DPRINTF(sc, RTWN_DEBUG_FIRMWARE,
89 "%s: could not load firmware page %d (offset %u)\n",
97 rtwn_fw_checksum_report(struct rtwn_softc *sc)
101 for (ntries = 0; ntries < 25; ntries++) {
102 if (rtwn_read_4(sc, R92C_MCUFWDL) & R92C_MCUFWDL_CHKSUM_RPT)
104 rtwn_delay(sc, 10000);
107 RTWN_DPRINTF(sc, RTWN_DEBUG_FIRMWARE,
108 "timeout waiting for checksum report\n");
116 rtwn_load_firmware(struct rtwn_softc *sc)
118 const struct firmware *fw;
119 const struct r92c_fw_hdr *hdr;
124 /* Read firmware image from the filesystem. */
126 fw = firmware_get(sc->fwname);
129 device_printf(sc->sc_dev,
130 "failed loadfirmware of file %s\n", sc->fwname);
135 if (len < sizeof(*hdr) || len > sc->fwsize_limit) {
136 device_printf(sc->sc_dev, "wrong firmware size (%zu)\n", len);
141 hdr = (const struct r92c_fw_hdr *)ptr;
142 /* Check if there is a valid FW header and skip it. */
143 if ((le16toh(hdr->signature) >> 4) == sc->fwsig) {
144 sc->fwver = le16toh(hdr->version);
146 RTWN_DPRINTF(sc, RTWN_DEBUG_FIRMWARE,
147 "FW V%u.%u %02u-%02u %02u:%02u\n",
148 le16toh(hdr->version), le16toh(hdr->subversion),
149 hdr->month, hdr->date, hdr->hour, hdr->minute);
154 if (rtwn_read_1(sc, R92C_MCUFWDL) & R92C_MCUFWDL_RAM_DL_SEL) {
155 rtwn_write_1(sc, R92C_MCUFWDL, 0);
156 rtwn_fw_reset(sc, RTWN_FW_RESET_DOWNLOAD);
159 /* Enable firmware download. */
160 rtwn_fw_download_enable(sc, 1);
162 error = 0; /* compiler warning */
163 for (ntries = 0; ntries < 3; ntries++) {
164 const u_char *curr_ptr = ptr;
165 const int maxpages = len / R92C_FW_PAGE_SIZE;
168 /* Reset the FWDL checksum. */
169 rtwn_setbits_1(sc, R92C_MCUFWDL, 0, R92C_MCUFWDL_CHKSUM_RPT);
171 for (page = 0; page < maxpages; page++) {
172 error = rtwn_fw_loadpage(sc, page, curr_ptr,
176 curr_ptr += R92C_FW_PAGE_SIZE;
178 if (page != maxpages)
181 if (len % R92C_FW_PAGE_SIZE != 0) {
182 error = rtwn_fw_loadpage(sc, page, curr_ptr,
183 len % R92C_FW_PAGE_SIZE);
188 /* Wait for checksum report. */
189 error = rtwn_fw_checksum_report(sc);
194 device_printf(sc->sc_dev,
195 "%s: failed to upload firmware %s (error %d)\n",
196 __func__, sc->fwname, error);
200 /* Disable firmware download. */
201 rtwn_fw_download_enable(sc, 0);
203 rtwn_setbits_4(sc, R92C_MCUFWDL, R92C_MCUFWDL_WINTINI_RDY,
206 rtwn_fw_reset(sc, RTWN_FW_RESET_CHECKSUM);
208 /* Wait for firmware readiness. */
209 for (ntries = 0; ntries < 20; ntries++) {
210 if (rtwn_read_4(sc, R92C_MCUFWDL) & R92C_MCUFWDL_WINTINI_RDY)
212 rtwn_delay(sc, 10000);
215 device_printf(sc->sc_dev,
216 "timeout waiting for firmware readiness\n");
221 firmware_put(fw, FIRMWARE_UNLOAD);