blob: 07055afef858b1ee3c21f1199a3de412939e39f4 [file] [log] [blame]
Tomas Winklerab69a5a2009-10-17 09:09:34 +00001/*
2 * iwmc3200top - Intel Wireless MultiCom 3200 Top Driver
3 * drivers/misc/iwmc3200top/fw-download.c
4 *
5 * Copyright (C) 2009 Intel Corporation. All rights reserved.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License version
9 * 2 as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 * 02110-1301, USA.
20 *
21 *
22 * Author Name: Maxim Grabarnik <maxim.grabarnink@intel.com>
23 * -
24 *
25 */
26
27#include <linux/firmware.h>
28#include <linux/mmc/sdio_func.h>
29#include <asm/unaligned.h>
30
31#include "iwmc3200top.h"
32#include "log.h"
33#include "fw-msg.h"
34
35#define CHECKSUM_BYTES_NUM sizeof(u32)
36
37/**
38 init parser struct with file
39 */
40static int iwmct_fw_parser_init(struct iwmct_priv *priv, const u8 *file,
41 size_t file_size, size_t block_size)
42{
43 struct iwmct_parser *parser = &priv->parser;
44 struct iwmct_fw_hdr *fw_hdr = &parser->versions;
45
Tomas Winkler0df828f2009-12-16 04:26:25 +000046 LOG_TRACE(priv, FW_DOWNLOAD, "-->\n");
Tomas Winklerab69a5a2009-10-17 09:09:34 +000047
48 LOG_INFO(priv, FW_DOWNLOAD, "file_size=%zd\n", file_size);
49
50 parser->file = file;
51 parser->file_size = file_size;
52 parser->cur_pos = 0;
Tomas Winkler9fa4d672009-12-16 04:26:24 +000053 parser->entry_point = 0;
Tomas Winklerab69a5a2009-10-17 09:09:34 +000054 parser->buf = kzalloc(block_size, GFP_KERNEL);
55 if (!parser->buf) {
56 LOG_ERROR(priv, FW_DOWNLOAD, "kzalloc error\n");
57 return -ENOMEM;
58 }
59 parser->buf_size = block_size;
60
61 /* extract fw versions */
62 memcpy(fw_hdr, parser->file, sizeof(struct iwmct_fw_hdr));
63 LOG_INFO(priv, FW_DOWNLOAD, "fw versions are:\n"
64 "top %u.%u.%u gps %u.%u.%u bt %u.%u.%u tic %s\n",
65 fw_hdr->top_major, fw_hdr->top_minor, fw_hdr->top_revision,
66 fw_hdr->gps_major, fw_hdr->gps_minor, fw_hdr->gps_revision,
67 fw_hdr->bt_major, fw_hdr->bt_minor, fw_hdr->bt_revision,
68 fw_hdr->tic_name);
69
70 parser->cur_pos += sizeof(struct iwmct_fw_hdr);
71
Tomas Winkler0df828f2009-12-16 04:26:25 +000072 LOG_TRACE(priv, FW_DOWNLOAD, "<--\n");
Tomas Winklerab69a5a2009-10-17 09:09:34 +000073 return 0;
74}
75
76static bool iwmct_checksum(struct iwmct_priv *priv)
77{
78 struct iwmct_parser *parser = &priv->parser;
79 __le32 *file = (__le32 *)parser->file;
80 int i, pad, steps;
81 u32 accum = 0;
82 u32 checksum;
83 u32 mask = 0xffffffff;
84
85 pad = (parser->file_size - CHECKSUM_BYTES_NUM) % 4;
86 steps = (parser->file_size - CHECKSUM_BYTES_NUM) / 4;
87
88 LOG_INFO(priv, FW_DOWNLOAD, "pad=%d steps=%d\n", pad, steps);
89
90 for (i = 0; i < steps; i++)
91 accum += le32_to_cpu(file[i]);
92
93 if (pad) {
94 mask <<= 8 * (4 - pad);
95 accum += le32_to_cpu(file[steps]) & mask;
96 }
97
98 checksum = get_unaligned_le32((__le32 *)(parser->file +
99 parser->file_size - CHECKSUM_BYTES_NUM));
100
101 LOG_INFO(priv, FW_DOWNLOAD,
102 "compare checksum accum=0x%x to checksum=0x%x\n",
103 accum, checksum);
104
105 return checksum == accum;
106}
107
108static int iwmct_parse_next_section(struct iwmct_priv *priv, const u8 **p_sec,
109 size_t *sec_size, __le32 *sec_addr)
110{
111 struct iwmct_parser *parser = &priv->parser;
112 struct iwmct_dbg *dbg = &priv->dbg;
113 struct iwmct_fw_sec_hdr *sec_hdr;
114
Tomas Winkler0df828f2009-12-16 04:26:25 +0000115 LOG_TRACE(priv, FW_DOWNLOAD, "-->\n");
Tomas Winklerab69a5a2009-10-17 09:09:34 +0000116
117 while (parser->cur_pos + sizeof(struct iwmct_fw_sec_hdr)
118 <= parser->file_size) {
119
120 sec_hdr = (struct iwmct_fw_sec_hdr *)
121 (parser->file + parser->cur_pos);
122 parser->cur_pos += sizeof(struct iwmct_fw_sec_hdr);
123
124 LOG_INFO(priv, FW_DOWNLOAD,
125 "sec hdr: type=%s addr=0x%x size=%d\n",
126 sec_hdr->type, sec_hdr->target_addr,
127 sec_hdr->data_size);
128
129 if (strcmp(sec_hdr->type, "ENT") == 0)
130 parser->entry_point = le32_to_cpu(sec_hdr->target_addr);
131 else if (strcmp(sec_hdr->type, "LBL") == 0)
132 strcpy(dbg->label_fw, parser->file + parser->cur_pos);
133 else if (((strcmp(sec_hdr->type, "TOP") == 0) &&
134 (priv->barker & BARKER_DNLOAD_TOP_MSK)) ||
135 ((strcmp(sec_hdr->type, "GPS") == 0) &&
136 (priv->barker & BARKER_DNLOAD_GPS_MSK)) ||
137 ((strcmp(sec_hdr->type, "BTH") == 0) &&
138 (priv->barker & BARKER_DNLOAD_BT_MSK))) {
139 *sec_addr = sec_hdr->target_addr;
140 *sec_size = le32_to_cpu(sec_hdr->data_size);
141 *p_sec = parser->file + parser->cur_pos;
142 parser->cur_pos += le32_to_cpu(sec_hdr->data_size);
143 return 1;
144 } else if (strcmp(sec_hdr->type, "LOG") != 0)
145 LOG_WARNING(priv, FW_DOWNLOAD,
146 "skipping section type %s\n",
147 sec_hdr->type);
148
149 parser->cur_pos += le32_to_cpu(sec_hdr->data_size);
150 LOG_INFO(priv, FW_DOWNLOAD,
151 "finished with section cur_pos=%zd\n", parser->cur_pos);
152 }
153
Tomas Winkler0df828f2009-12-16 04:26:25 +0000154 LOG_TRACE(priv, INIT, "<--\n");
Tomas Winklerab69a5a2009-10-17 09:09:34 +0000155 return 0;
156}
157
158static int iwmct_download_section(struct iwmct_priv *priv, const u8 *p_sec,
159 size_t sec_size, __le32 addr)
160{
161 struct iwmct_parser *parser = &priv->parser;
162 struct iwmct_fw_load_hdr *hdr = (struct iwmct_fw_load_hdr *)parser->buf;
163 const u8 *cur_block = p_sec;
164 size_t sent = 0;
165 int cnt = 0;
166 int ret = 0;
167 u32 cmd = 0;
168
Tomas Winkler0df828f2009-12-16 04:26:25 +0000169 LOG_TRACE(priv, FW_DOWNLOAD, "-->\n");
Tomas Winklerab69a5a2009-10-17 09:09:34 +0000170 LOG_INFO(priv, FW_DOWNLOAD, "Download address 0x%x size 0x%zx\n",
171 addr, sec_size);
172
173 while (sent < sec_size) {
174 int i;
175 u32 chksm = 0;
176 u32 reset = atomic_read(&priv->reset);
177 /* actual FW data */
178 u32 data_size = min(parser->buf_size - sizeof(*hdr),
179 sec_size - sent);
180 /* Pad to block size */
181 u32 trans_size = (data_size + sizeof(*hdr) +
182 IWMC_SDIO_BLK_SIZE - 1) &
183 ~(IWMC_SDIO_BLK_SIZE - 1);
184 ++cnt;
185
186 /* in case of reset, interrupt FW DOWNLAOD */
187 if (reset) {
188 LOG_INFO(priv, FW_DOWNLOAD,
189 "Reset detected. Abort FW download!!!");
190 ret = -ECANCELED;
191 goto exit;
192 }
193
194 memset(parser->buf, 0, parser->buf_size);
195 cmd |= IWMC_OPCODE_WRITE << CMD_HDR_OPCODE_POS;
196 cmd |= IWMC_CMD_SIGNATURE << CMD_HDR_SIGNATURE_POS;
197 cmd |= (priv->dbg.direct ? 1 : 0) << CMD_HDR_DIRECT_ACCESS_POS;
198 cmd |= (priv->dbg.checksum ? 1 : 0) << CMD_HDR_USE_CHECKSUM_POS;
199 hdr->data_size = cpu_to_le32(data_size);
200 hdr->target_addr = addr;
201
202 /* checksum is allowed for sizes divisible by 4 */
203 if (data_size & 0x3)
204 cmd &= ~CMD_HDR_USE_CHECKSUM_MSK;
205
206 memcpy(hdr->data, cur_block, data_size);
207
208
209 if (cmd & CMD_HDR_USE_CHECKSUM_MSK) {
210
211 chksm = data_size + le32_to_cpu(addr) + cmd;
212 for (i = 0; i < data_size >> 2; i++)
213 chksm += ((u32 *)cur_block)[i];
214
215 hdr->block_chksm = cpu_to_le32(chksm);
216 LOG_INFO(priv, FW_DOWNLOAD, "Checksum = 0x%X\n",
217 hdr->block_chksm);
218 }
219
220 LOG_INFO(priv, FW_DOWNLOAD, "trans#%d, len=%d, sent=%zd, "
221 "sec_size=%zd, startAddress 0x%X\n",
222 cnt, trans_size, sent, sec_size, addr);
223
224 if (priv->dbg.dump)
225 LOG_HEXDUMP(FW_DOWNLOAD, parser->buf, trans_size);
226
227
228 hdr->cmd = cpu_to_le32(cmd);
229 /* send it down */
230 /* TODO: add more proper sending and error checking */
231 ret = iwmct_tx(priv, 0, parser->buf, trans_size);
232 if (ret != 0) {
233 LOG_INFO(priv, FW_DOWNLOAD,
234 "iwmct_tx returned %d\n", ret);
235 goto exit;
236 }
237
238 addr = cpu_to_le32(le32_to_cpu(addr) + data_size);
239 sent += data_size;
240 cur_block = p_sec + sent;
241
242 if (priv->dbg.blocks && (cnt + 1) >= priv->dbg.blocks) {
243 LOG_INFO(priv, FW_DOWNLOAD,
244 "Block number limit is reached [%d]\n",
245 priv->dbg.blocks);
246 break;
247 }
248 }
249
250 if (sent < sec_size)
251 ret = -EINVAL;
252exit:
Tomas Winkler0df828f2009-12-16 04:26:25 +0000253 LOG_TRACE(priv, FW_DOWNLOAD, "<--\n");
Tomas Winklerab69a5a2009-10-17 09:09:34 +0000254 return ret;
255}
256
257static int iwmct_kick_fw(struct iwmct_priv *priv, bool jump)
258{
259 struct iwmct_parser *parser = &priv->parser;
260 struct iwmct_fw_load_hdr *hdr = (struct iwmct_fw_load_hdr *)parser->buf;
261 int ret;
262 u32 cmd;
263
Tomas Winkler0df828f2009-12-16 04:26:25 +0000264 LOG_TRACE(priv, FW_DOWNLOAD, "-->\n");
Tomas Winklerab69a5a2009-10-17 09:09:34 +0000265
266 memset(parser->buf, 0, parser->buf_size);
267 cmd = IWMC_CMD_SIGNATURE << CMD_HDR_SIGNATURE_POS;
268 if (jump) {
269 cmd |= IWMC_OPCODE_JUMP << CMD_HDR_OPCODE_POS;
270 hdr->target_addr = cpu_to_le32(parser->entry_point);
271 LOG_INFO(priv, FW_DOWNLOAD, "jump address 0x%x\n",
272 parser->entry_point);
273 } else {
274 cmd |= IWMC_OPCODE_LAST_COMMAND << CMD_HDR_OPCODE_POS;
275 LOG_INFO(priv, FW_DOWNLOAD, "last command\n");
276 }
277
278 hdr->cmd = cpu_to_le32(cmd);
279
280 LOG_HEXDUMP(FW_DOWNLOAD, parser->buf, sizeof(*hdr));
281 /* send it down */
282 /* TODO: add more proper sending and error checking */
283 ret = iwmct_tx(priv, 0, parser->buf, IWMC_SDIO_BLK_SIZE);
284 if (ret)
285 LOG_INFO(priv, FW_DOWNLOAD, "iwmct_tx returned %d", ret);
286
Tomas Winkler0df828f2009-12-16 04:26:25 +0000287 LOG_TRACE(priv, FW_DOWNLOAD, "<--\n");
Tomas Winklerab69a5a2009-10-17 09:09:34 +0000288 return 0;
289}
290
291int iwmct_fw_load(struct iwmct_priv *priv)
292{
Tomas Winkler0e481742009-11-18 23:29:57 -0800293 const u8 *fw_name = FW_NAME(FW_API_VER);
294 const struct firmware *raw;
Tomas Winklerab69a5a2009-10-17 09:09:34 +0000295 const u8 *pdata;
Tomas Winkler0e481742009-11-18 23:29:57 -0800296 size_t len;
297 __le32 addr;
298 int ret;
Tomas Winklerab69a5a2009-10-17 09:09:34 +0000299
Tomas Winklerab69a5a2009-10-17 09:09:34 +0000300
Tomas Winkler0df828f2009-12-16 04:26:25 +0000301 LOG_INFO(priv, FW_DOWNLOAD, "barker download request 0x%x is:\n",
302 priv->barker);
303 LOG_INFO(priv, FW_DOWNLOAD, "******* Top FW %s requested ********\n",
304 (priv->barker & BARKER_DNLOAD_TOP_MSK) ? "was" : "not");
305 LOG_INFO(priv, FW_DOWNLOAD, "******* GPS FW %s requested ********\n",
306 (priv->barker & BARKER_DNLOAD_GPS_MSK) ? "was" : "not");
307 LOG_INFO(priv, FW_DOWNLOAD, "******* BT FW %s requested ********\n",
308 (priv->barker & BARKER_DNLOAD_BT_MSK) ? "was" : "not");
309
310
Tomas Winklerab69a5a2009-10-17 09:09:34 +0000311 /* get the firmware */
Tomas Winkler0e481742009-11-18 23:29:57 -0800312 ret = request_firmware(&raw, fw_name, &priv->func->dev);
Tomas Winklerab69a5a2009-10-17 09:09:34 +0000313 if (ret < 0) {
314 LOG_ERROR(priv, FW_DOWNLOAD, "%s request_firmware failed %d\n",
Tomas Winkler0e481742009-11-18 23:29:57 -0800315 fw_name, ret);
Tomas Winklerab69a5a2009-10-17 09:09:34 +0000316 goto exit;
317 }
318
319 if (raw->size < sizeof(struct iwmct_fw_sec_hdr)) {
320 LOG_ERROR(priv, FW_DOWNLOAD, "%s smaller then (%zd) (%zd)\n",
Tomas Winkler0e481742009-11-18 23:29:57 -0800321 fw_name, sizeof(struct iwmct_fw_sec_hdr), raw->size);
Tomas Winklerab69a5a2009-10-17 09:09:34 +0000322 goto exit;
323 }
324
Tomas Winkler0e481742009-11-18 23:29:57 -0800325 LOG_INFO(priv, FW_DOWNLOAD, "Read firmware '%s'\n", fw_name);
Tomas Winklerab69a5a2009-10-17 09:09:34 +0000326
Tomas Winkler9fa4d672009-12-16 04:26:24 +0000327 /* clear parser struct */
Tomas Winklerab69a5a2009-10-17 09:09:34 +0000328 ret = iwmct_fw_parser_init(priv, raw->data, raw->size, priv->trans_len);
329 if (ret < 0) {
330 LOG_ERROR(priv, FW_DOWNLOAD,
331 "iwmct_parser_init failed: Reason %d\n", ret);
332 goto exit;
333 }
334
Tomas Winklerab69a5a2009-10-17 09:09:34 +0000335 if (!iwmct_checksum(priv)) {
336 LOG_ERROR(priv, FW_DOWNLOAD, "checksum error\n");
337 ret = -EINVAL;
338 goto exit;
339 }
340
341 /* download firmware to device */
342 while (iwmct_parse_next_section(priv, &pdata, &len, &addr)) {
Tomas Winkler9fa4d672009-12-16 04:26:24 +0000343 ret = iwmct_download_section(priv, pdata, len, addr);
344 if (ret) {
Tomas Winklerab69a5a2009-10-17 09:09:34 +0000345 LOG_ERROR(priv, FW_DOWNLOAD,
Tomas Winkler0e481742009-11-18 23:29:57 -0800346 "%s download section failed\n", fw_name);
Tomas Winklerab69a5a2009-10-17 09:09:34 +0000347 goto exit;
348 }
349 }
350
Tomas Winkler9fa4d672009-12-16 04:26:24 +0000351 ret = iwmct_kick_fw(priv, !!(priv->barker & BARKER_DNLOAD_JUMP_MSK));
Tomas Winklerab69a5a2009-10-17 09:09:34 +0000352
353exit:
354 kfree(priv->parser.buf);
Tomas Winkler9fa4d672009-12-16 04:26:24 +0000355 release_firmware(raw);
Tomas Winklerab69a5a2009-10-17 09:09:34 +0000356 return ret;
357}