d6c28c23 |
1 | /****************************************************************************** |
2 | * |
3 | * Copyright(c) 2009-2013 Realtek Corporation. |
4 | * |
5 | * This program is free software; you can redistribute it and/or modify it |
6 | * under the terms of version 2 of the GNU General Public License as |
7 | * published by the Free Software Foundation. |
8 | * |
9 | * This program is distributed in the hope that it will be useful, but WITHOUT |
10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
12 | * more details. |
13 | * |
14 | * You should have received a copy of the GNU General Public License along with |
15 | * this program; if not, write to the Free Software Foundation, Inc., |
16 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA |
17 | * |
18 | * The full GNU General Public License is included in this distribution in the |
19 | * file called LICENSE. |
20 | * |
21 | * Contact Information: |
22 | * wlanfae <wlanfae@realtek.com> |
23 | * Realtek Corporation, No. 2, Innovation Road II, Hsinchu Science Park, |
24 | * Hsinchu 300, Taiwan. |
25 | * |
26 | * Larry Finger <Larry.Finger@lwfinger.net> |
27 | * |
28 | *****************************************************************************/ |
29 | |
30 | #include "fw.h" |
31 | #include "drv_types.h" |
32 | #include "usb_ops_linux.h" |
33 | #include "rtl8188e_spec.h" |
34 | #include "rtl8188e_hal.h" |
35 | |
36 | #include <linux/firmware.h> |
37 | #include <linux/kmemleak.h> |
38 | |
39 | static void _rtl88e_enable_fw_download(struct adapter *adapt, bool enable) |
40 | { |
41 | u8 tmp; |
42 | |
43 | if (enable) { |
44 | tmp = usb_read8(adapt, REG_MCUFWDL); |
45 | usb_write8(adapt, REG_MCUFWDL, tmp | 0x01); |
46 | |
47 | tmp = usb_read8(adapt, REG_MCUFWDL + 2); |
48 | usb_write8(adapt, REG_MCUFWDL + 2, tmp & 0xf7); |
49 | } else { |
50 | tmp = usb_read8(adapt, REG_MCUFWDL); |
51 | usb_write8(adapt, REG_MCUFWDL, tmp & 0xfe); |
52 | |
53 | usb_write8(adapt, REG_MCUFWDL + 1, 0x00); |
54 | } |
55 | } |
56 | |
57 | static void _rtl88e_fw_block_write(struct adapter *adapt, |
58 | const u8 *buffer, u32 size) |
59 | { |
60 | u32 blk_sz = sizeof(u32); |
61 | u8 *buf_ptr = (u8 *)buffer; |
62 | u32 *pu4BytePtr = (u32 *)buffer; |
63 | u32 i, offset, blk_cnt, remain; |
64 | |
65 | blk_cnt = size / blk_sz; |
66 | remain = size % blk_sz; |
67 | |
1c48deff |
68 | offset = FW_8192C_START_ADDRESS; |
d44f58f7 |
69 | |
d6c28c23 |
70 | for (i = 0; i < blk_cnt; i++) { |
1c48deff |
71 | usb_write32(adapt, offset, pu4BytePtr[i]); |
d44f58f7 |
72 | offset += blk_sz; |
d6c28c23 |
73 | } |
74 | |
75 | if (remain) { |
37d55790 |
76 | buf_ptr += blk_cnt * blk_sz; |
d6c28c23 |
77 | for (i = 0; i < remain; i++) { |
8107b147 |
78 | usb_write8(adapt, offset + i, buf_ptr[i]); |
d6c28c23 |
79 | } |
80 | } |
81 | } |
82 | |
83 | static void _rtl88e_fill_dummy(u8 *pfwbuf, u32 *pfwlen) |
84 | { |
85 | u32 fwlen = *pfwlen; |
7be921a2 |
86 | u8 remain = (u8)(fwlen % 4); |
d6c28c23 |
87 | |
88 | remain = (remain == 0) ? 0 : (4 - remain); |
89 | |
90 | while (remain > 0) { |
91 | pfwbuf[fwlen] = 0; |
92 | fwlen++; |
93 | remain--; |
94 | } |
95 | |
96 | *pfwlen = fwlen; |
97 | } |
98 | |
99 | static void _rtl88e_fw_page_write(struct adapter *adapt, |
100 | u32 page, const u8 *buffer, u32 size) |
101 | { |
102 | u8 value8; |
7be921a2 |
103 | u8 u8page = (u8)(page & 0x07); |
d6c28c23 |
104 | |
105 | value8 = (usb_read8(adapt, REG_MCUFWDL + 2) & 0xF8) | u8page; |
106 | |
107 | usb_write8(adapt, (REG_MCUFWDL + 2), value8); |
108 | _rtl88e_fw_block_write(adapt, buffer, size); |
109 | } |
110 | |
111 | static void _rtl88e_write_fw(struct adapter *adapt, u8 *buffer, u32 size) |
112 | { |
113 | u8 *buf_ptr = buffer; |
114 | u32 page_no, remain; |
115 | u32 page, offset; |
116 | |
117 | _rtl88e_fill_dummy(buf_ptr, &size); |
118 | |
119 | page_no = size / FW_8192C_PAGE_SIZE; |
120 | remain = size % FW_8192C_PAGE_SIZE; |
121 | |
122 | for (page = 0; page < page_no; page++) { |
123 | offset = page * FW_8192C_PAGE_SIZE; |
124 | _rtl88e_fw_page_write(adapt, page, (buf_ptr + offset), |
125 | FW_8192C_PAGE_SIZE); |
126 | } |
127 | |
128 | if (remain) { |
129 | offset = page_no * FW_8192C_PAGE_SIZE; |
130 | page = page_no; |
131 | _rtl88e_fw_page_write(adapt, page, (buf_ptr + offset), remain); |
132 | } |
133 | } |
134 | |
135 | static void rtl88e_firmware_selfreset(struct adapter *adapt) |
136 | { |
137 | u8 u1b_tmp; |
138 | |
139 | u1b_tmp = usb_read8(adapt, REG_SYS_FUNC_EN+1); |
140 | usb_write8(adapt, REG_SYS_FUNC_EN+1, (u1b_tmp & (~BIT(2)))); |
141 | usb_write8(adapt, REG_SYS_FUNC_EN+1, (u1b_tmp | BIT(2))); |
142 | } |
143 | |
144 | static int _rtl88e_fw_free_to_go(struct adapter *adapt) |
145 | { |
146 | int err = -EIO; |
147 | u32 counter = 0; |
148 | u32 value32; |
149 | |
150 | do { |
151 | value32 = usb_read32(adapt, REG_MCUFWDL); |
152 | if (value32 & FWDL_ChkSum_rpt) |
153 | break; |
154 | } while (counter++ < POLLING_READY_TIMEOUT_COUNT); |
155 | |
dacd2ece |
156 | if (counter >= POLLING_READY_TIMEOUT_COUNT) |
d6c28c23 |
157 | goto exit; |
d6c28c23 |
158 | |
159 | value32 = usb_read32(adapt, REG_MCUFWDL); |
160 | value32 |= MCUFWDL_RDY; |
161 | value32 &= ~WINTINI_RDY; |
162 | usb_write32(adapt, REG_MCUFWDL, value32); |
163 | |
164 | rtl88e_firmware_selfreset(adapt); |
165 | counter = 0; |
166 | |
167 | do { |
168 | value32 = usb_read32(adapt, REG_MCUFWDL); |
169 | if (value32 & WINTINI_RDY) { |
170 | err = 0; |
171 | goto exit; |
172 | } |
173 | |
174 | udelay(FW_8192C_POLLING_DELAY); |
175 | |
176 | } while (counter++ < POLLING_READY_TIMEOUT_COUNT); |
177 | |
178 | exit: |
179 | return err; |
180 | } |
181 | |
90d88de8 |
182 | int rtl88eu_download_fw(struct adapter *adapt) |
d6c28c23 |
183 | { |
184 | struct hal_data_8188e *rtlhal = GET_HAL_DATA(adapt); |
185 | struct dvobj_priv *dvobj = adapter_to_dvobj(adapt); |
186 | struct device *device = dvobj_to_dev(dvobj); |
187 | const struct firmware *fw; |
188 | const char fw_name[] = "rtlwifi/rtl8188eufw.bin"; |
189 | struct rtl92c_firmware_header *pfwheader = NULL; |
190 | u8 *pfwdata; |
191 | u32 fwsize; |
d6c28c23 |
192 | |
7be921a2 |
193 | if (request_firmware(&fw, fw_name, device)) { |
d6c28c23 |
194 | dev_err(device, "Firmware %s not available\n", fw_name); |
195 | return -ENOENT; |
196 | } |
197 | |
198 | if (fw->size > FW_8188E_SIZE) { |
7be921a2 |
199 | dev_err(device, "Firmware size exceed 0x%X. Check it.\n", |
d6c28c23 |
200 | FW_8188E_SIZE); |
201 | return -1; |
202 | } |
203 | |
204 | pfwdata = kzalloc(FW_8188E_SIZE, GFP_KERNEL); |
205 | if (!pfwdata) |
206 | return -ENOMEM; |
207 | |
208 | rtlhal->pfirmware = pfwdata; |
209 | memcpy(rtlhal->pfirmware, fw->data, fw->size); |
210 | rtlhal->fwsize = fw->size; |
211 | release_firmware(fw); |
212 | |
213 | fwsize = rtlhal->fwsize; |
214 | pfwheader = (struct rtl92c_firmware_header *)pfwdata; |
215 | |
216 | if (IS_FW_HEADER_EXIST(pfwheader)) { |
217 | pfwdata = pfwdata + 32; |
218 | fwsize = fwsize - 32; |
219 | } |
220 | |
221 | if (usb_read8(adapt, REG_MCUFWDL) & RAM_DL_SEL) { |
222 | usb_write8(adapt, REG_MCUFWDL, 0); |
223 | rtl88e_firmware_selfreset(adapt); |
224 | } |
225 | _rtl88e_enable_fw_download(adapt, true); |
226 | usb_write8(adapt, REG_MCUFWDL, usb_read8(adapt, REG_MCUFWDL) | FWDL_ChkSum_rpt); |
227 | _rtl88e_write_fw(adapt, pfwdata, fwsize); |
228 | _rtl88e_enable_fw_download(adapt, false); |
229 | |
f79dc167 |
230 | return _rtl88e_fw_free_to_go(adapt); |
d6c28c23 |
231 | } |