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
rtwn_fw_loadpage(struct rtwn_softc * sc,int page,const uint8_t * buf,int len)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
rtwn_fw_checksum_report(struct rtwn_softc * sc)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
rtwn_load_firmware(struct rtwn_softc * sc)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 (%s) V%u.%u %02u-%02u %02u:%02u\n",
145 sc->fwname,
146 le16toh(hdr->version), le16toh(hdr->subversion),
147 hdr->month, hdr->date, hdr->hour, hdr->minute);
148 ptr += sizeof(*hdr);
149 len -= sizeof(*hdr);
150 }
151
152 if (rtwn_read_1(sc, R92C_MCUFWDL) & R92C_MCUFWDL_RAM_DL_SEL) {
153 rtwn_write_1(sc, R92C_MCUFWDL, 0);
154 rtwn_fw_reset(sc, RTWN_FW_RESET_DOWNLOAD);
155 }
156
157 /* Enable firmware download. */
158 rtwn_fw_download_enable(sc, 1);
159
160 error = 0; /* compiler warning */
161 for (ntries = 0; ntries < 3; ntries++) {
162 const u_char *curr_ptr = ptr;
163 const int maxpages = len / R92C_FW_PAGE_SIZE;
164 int page;
165
166 /* Reset the FWDL checksum. */
167 rtwn_setbits_1(sc, R92C_MCUFWDL, 0, R92C_MCUFWDL_CHKSUM_RPT);
168
169 for (page = 0; page < maxpages; page++) {
170 error = rtwn_fw_loadpage(sc, page, curr_ptr,
171 R92C_FW_PAGE_SIZE);
172 if (error != 0)
173 break;
174 curr_ptr += R92C_FW_PAGE_SIZE;
175 }
176 if (page != maxpages)
177 continue;
178
179 if (len % R92C_FW_PAGE_SIZE != 0) {
180 error = rtwn_fw_loadpage(sc, page, curr_ptr,
181 len % R92C_FW_PAGE_SIZE);
182 if (error != 0)
183 continue;
184 }
185
186 /* Wait for checksum report. */
187 error = rtwn_fw_checksum_report(sc);
188 if (error == 0)
189 break;
190 }
191 if (ntries == 3) {
192 device_printf(sc->sc_dev,
193 "%s: failed to upload firmware %s (error %d)\n",
194 __func__, sc->fwname, error);
195 goto fail;
196 }
197
198 /* Disable firmware download. */
199 rtwn_fw_download_enable(sc, 0);
200
201 rtwn_setbits_4(sc, R92C_MCUFWDL, R92C_MCUFWDL_WINTINI_RDY,
202 R92C_MCUFWDL_RDY);
203
204 rtwn_fw_reset(sc, RTWN_FW_RESET_CHECKSUM);
205
206 /* Wait for firmware readiness. */
207 for (ntries = 0; ntries < 20; ntries++) {
208 if (rtwn_read_4(sc, R92C_MCUFWDL) & R92C_MCUFWDL_WINTINI_RDY)
209 break;
210 rtwn_delay(sc, 10000);
211 }
212 if (ntries == 20) {
213 device_printf(sc->sc_dev,
214 "timeout waiting for firmware readiness\n");
215 error = ETIMEDOUT;
216 goto fail;
217 }
218 fail:
219 firmware_put(fw, FIRMWARE_UNLOAD);
220 return (error);
221 }
222 #endif
223