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>
56 #ifndef RTWN_WITHOUT_UCODE
58 rtwn_fw_loadpage(struct rtwn_softc *sc, int page, const uint8_t *buf,
65 reg = rtwn_read_4(sc, R92C_MCUFWDL);
66 reg = RW(reg, R92C_MCUFWDL_PAGE, page);
67 rtwn_write_4(sc, R92C_MCUFWDL, reg);
70 off = R92C_FW_START_ADDR;
72 if (len > R92C_FW_MAX_BLOCK_SIZE)
73 mlen = R92C_FW_MAX_BLOCK_SIZE;
78 error = rtwn_fw_write_block(sc, buf, off, mlen);
87 RTWN_DPRINTF(sc, RTWN_DEBUG_FIRMWARE,
88 "%s: could not load firmware page %d (offset %u)\n",
96 rtwn_fw_checksum_report(struct rtwn_softc *sc)
100 for (ntries = 0; ntries < 25; ntries++) {
101 if (rtwn_read_4(sc, R92C_MCUFWDL) & R92C_MCUFWDL_CHKSUM_RPT)
103 rtwn_delay(sc, 10000);
106 RTWN_DPRINTF(sc, RTWN_DEBUG_FIRMWARE,
107 "timeout waiting for checksum report\n");
115 rtwn_load_firmware(struct rtwn_softc *sc)
117 const struct firmware *fw;
118 const struct r92c_fw_hdr *hdr;
123 /* Read firmware image from the filesystem. */
125 fw = firmware_get(sc->fwname);
128 device_printf(sc->sc_dev,
129 "failed loadfirmware of file %s\n", sc->fwname);
134 if (len < sizeof(*hdr) || len > sc->fwsize_limit) {
135 device_printf(sc->sc_dev, "wrong firmware size (%zu)\n", len);
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);
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);
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);
158 /* Enable firmware download. */
159 rtwn_fw_download_enable(sc, 1);
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;
167 /* Reset the FWDL checksum. */
168 rtwn_setbits_1(sc, R92C_MCUFWDL, 0, R92C_MCUFWDL_CHKSUM_RPT);
170 for (page = 0; page < maxpages; page++) {
171 error = rtwn_fw_loadpage(sc, page, curr_ptr,
175 curr_ptr += R92C_FW_PAGE_SIZE;
177 if (page != maxpages)
180 if (len % R92C_FW_PAGE_SIZE != 0) {
181 error = rtwn_fw_loadpage(sc, page, curr_ptr,
182 len % R92C_FW_PAGE_SIZE);
187 /* Wait for checksum report. */
188 error = rtwn_fw_checksum_report(sc);
193 device_printf(sc->sc_dev,
194 "%s: failed to upload firmware %s (error %d)\n",
195 __func__, sc->fwname, error);
199 /* Disable firmware download. */
200 rtwn_fw_download_enable(sc, 0);
202 rtwn_setbits_4(sc, R92C_MCUFWDL, R92C_MCUFWDL_WINTINI_RDY,
205 rtwn_fw_reset(sc, RTWN_FW_RESET_CHECKSUM);
207 /* Wait for firmware readiness. */
208 for (ntries = 0; ntries < 20; ntries++) {
209 if (rtwn_read_4(sc, R92C_MCUFWDL) & R92C_MCUFWDL_WINTINI_RDY)
211 rtwn_delay(sc, 10000);
214 device_printf(sc->sc_dev,
215 "timeout waiting for firmware readiness\n");
220 firmware_put(fw, FIRMWARE_UNLOAD);