uwb: i1480/GUWA100U: fix firmware download issues
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / uwb / i1480 / dfu / mac.c
blob8d069907a3b5ab29a974d40809632e28a00d9ca6
1 /*
2 * Intel Wireless UWB Link 1480
3 * MAC Firmware upload implementation
5 * Copyright (C) 2005-2006 Intel Corporation
6 * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License version
10 * 2 as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 * 02110-1301, USA.
23 * Implementation of the code for parsing the firmware file (extract
24 * the headers and binary code chunks) in the fw_*() functions. The
25 * code to upload pre and mac firmwares is the same, so it uses a
26 * common entry point in __mac_fw_upload(), which uses the i1480
27 * function pointers to push the firmware to the device.
29 #include <linux/delay.h>
30 #include <linux/firmware.h>
31 #include <linux/uwb.h>
32 #include "i1480-dfu.h"
34 #define D_LOCAL 0
35 #include <linux/uwb/debug.h>
38 * Descriptor for a continuous segment of MAC fw data
40 struct fw_hdr {
41 unsigned long address;
42 size_t length;
43 const u32 *bin;
44 struct fw_hdr *next;
48 /* Free a chain of firmware headers */
49 static
50 void fw_hdrs_free(struct fw_hdr *hdr)
52 struct fw_hdr *next;
54 while (hdr) {
55 next = hdr->next;
56 kfree(hdr);
57 hdr = next;
62 /* Fill a firmware header descriptor from a memory buffer */
63 static
64 int fw_hdr_load(struct i1480 *i1480, struct fw_hdr *hdr, unsigned hdr_cnt,
65 const char *_data, const u32 *data_itr, const u32 *data_top)
67 size_t hdr_offset = (const char *) data_itr - _data;
68 size_t remaining_size = (void *) data_top - (void *) data_itr;
69 if (data_itr + 2 > data_top) {
70 dev_err(i1480->dev, "fw hdr #%u/%zu: EOF reached in header at "
71 "offset %zu, limit %zu\n",
72 hdr_cnt, hdr_offset,
73 (const char *) data_itr + 2 - _data,
74 (const char *) data_top - _data);
75 return -EINVAL;
77 hdr->next = NULL;
78 hdr->address = le32_to_cpu(*data_itr++);
79 hdr->length = le32_to_cpu(*data_itr++);
80 hdr->bin = data_itr;
81 if (hdr->length > remaining_size) {
82 dev_err(i1480->dev, "fw hdr #%u/%zu: EOF reached in data; "
83 "chunk too long (%zu bytes), only %zu left\n",
84 hdr_cnt, hdr_offset, hdr->length, remaining_size);
85 return -EINVAL;
87 return 0;
91 /**
92 * Get a buffer where the firmware is supposed to be and create a
93 * chain of headers linking them together.
95 * @phdr: where to place the pointer to the first header (headers link
96 * to the next via the @hdr->next ptr); need to free the whole
97 * chain when done.
99 * @_data: Pointer to the data buffer.
101 * @_data_size: Size of the data buffer (bytes); data size has to be a
102 * multiple of 4. Function will fail if not.
104 * Goes over the whole binary blob; reads the first chunk and creates
105 * a fw hdr from it (which points to where the data is in @_data and
106 * the length of the chunk); then goes on to the next chunk until
107 * done. Each header is linked to the next.
109 static
110 int fw_hdrs_load(struct i1480 *i1480, struct fw_hdr **phdr,
111 const char *_data, size_t data_size)
113 int result;
114 unsigned hdr_cnt = 0;
115 u32 *data = (u32 *) _data, *data_itr, *data_top;
116 struct fw_hdr *hdr, **prev_hdr = phdr;
118 result = -EINVAL;
119 /* Check size is ok and pointer is aligned */
120 if (data_size % sizeof(u32) != 0)
121 goto error;
122 if ((unsigned long) _data % sizeof(u16) != 0)
123 goto error;
124 *phdr = NULL;
125 data_itr = data;
126 data_top = (u32 *) (_data + data_size);
127 while (data_itr < data_top) {
128 result = -ENOMEM;
129 hdr = kmalloc(sizeof(*hdr), GFP_KERNEL);
130 if (hdr == NULL) {
131 dev_err(i1480->dev, "Cannot allocate fw header "
132 "for chunk #%u\n", hdr_cnt);
133 goto error_alloc;
135 result = fw_hdr_load(i1480, hdr, hdr_cnt,
136 _data, data_itr, data_top);
137 if (result < 0)
138 goto error_load;
139 data_itr += 2 + hdr->length;
140 *prev_hdr = hdr;
141 prev_hdr = &hdr->next;
142 hdr_cnt++;
144 *prev_hdr = NULL;
145 return 0;
147 error_load:
148 kfree(hdr);
149 error_alloc:
150 fw_hdrs_free(*phdr);
151 error:
152 return result;
157 * Compares a chunk of fw with one in the devices's memory
159 * @i1480: Device instance
160 * @hdr: Pointer to the firmware chunk
161 * @returns: 0 if equal, < 0 errno on error. If > 0, it is the offset
162 * where the difference was found (plus one).
164 * Kind of dirty and simplistic, but does the trick in both the PCI
165 * and USB version. We do a quick[er] memcmp(), and if it fails, we do
166 * a byte-by-byte to find the offset.
168 static
169 ssize_t i1480_fw_cmp(struct i1480 *i1480, struct fw_hdr *hdr)
171 ssize_t result = 0;
172 u32 src_itr = 0, cnt;
173 size_t size = hdr->length*sizeof(hdr->bin[0]);
174 size_t chunk_size;
175 u8 *bin = (u8 *) hdr->bin;
177 while (size > 0) {
178 chunk_size = size < i1480->buf_size ? size : i1480->buf_size;
179 result = i1480->read(i1480, hdr->address + src_itr, chunk_size);
180 if (result < 0) {
181 dev_err(i1480->dev, "error reading for verification: "
182 "%zd\n", result);
183 goto error;
185 if (memcmp(i1480->cmd_buf, bin + src_itr, result)) {
186 u8 *buf = i1480->cmd_buf;
187 d_printf(2, i1480->dev,
188 "original data @ %p + %u, %zu bytes\n",
189 bin, src_itr, result);
190 d_dump(4, i1480->dev, bin + src_itr, result);
191 for (cnt = 0; cnt < result; cnt++)
192 if (bin[src_itr + cnt] != buf[cnt]) {
193 dev_err(i1480->dev, "byte failed at "
194 "src_itr %u cnt %u [0x%02x "
195 "vs 0x%02x]\n", src_itr, cnt,
196 bin[src_itr + cnt], buf[cnt]);
197 result = src_itr + cnt + 1;
198 goto cmp_failed;
201 src_itr += result;
202 size -= result;
204 result = 0;
205 error:
206 cmp_failed:
207 return result;
212 * Writes firmware headers to the device.
214 * @prd: PRD instance
215 * @hdr: Processed firmware
216 * @returns: 0 if ok, < 0 errno on error.
218 static
219 int mac_fw_hdrs_push(struct i1480 *i1480, struct fw_hdr *hdr,
220 const char *fw_name, const char *fw_tag)
222 struct device *dev = i1480->dev;
223 ssize_t result = 0;
224 struct fw_hdr *hdr_itr;
225 int verif_retry_count;
227 d_fnstart(3, dev, "(%p, %p)\n", i1480, hdr);
228 /* Now, header by header, push them to the hw */
229 for (hdr_itr = hdr; hdr_itr != NULL; hdr_itr = hdr_itr->next) {
230 verif_retry_count = 0;
231 retry:
232 dev_dbg(dev, "fw chunk (%zu @ 0x%08lx)\n",
233 hdr_itr->length * sizeof(hdr_itr->bin[0]),
234 hdr_itr->address);
235 result = i1480->write(i1480, hdr_itr->address, hdr_itr->bin,
236 hdr_itr->length*sizeof(hdr_itr->bin[0]));
237 if (result < 0) {
238 dev_err(dev, "%s fw '%s': write failed (%zuB @ 0x%lx):"
239 " %zd\n", fw_tag, fw_name,
240 hdr_itr->length * sizeof(hdr_itr->bin[0]),
241 hdr_itr->address, result);
242 break;
244 result = i1480_fw_cmp(i1480, hdr_itr);
245 if (result < 0) {
246 dev_err(dev, "%s fw '%s': verification read "
247 "failed (%zuB @ 0x%lx): %zd\n",
248 fw_tag, fw_name,
249 hdr_itr->length * sizeof(hdr_itr->bin[0]),
250 hdr_itr->address, result);
251 break;
253 if (result > 0) { /* Offset where it failed + 1 */
254 result--;
255 dev_err(dev, "%s fw '%s': WARNING: verification "
256 "failed at 0x%lx: retrying\n",
257 fw_tag, fw_name, hdr_itr->address + result);
258 if (++verif_retry_count < 3)
259 goto retry; /* write this block again! */
260 dev_err(dev, "%s fw '%s': verification failed at 0x%lx: "
261 "tried %d times\n", fw_tag, fw_name,
262 hdr_itr->address + result, verif_retry_count);
263 result = -EINVAL;
264 break;
267 d_fnend(3, dev, "(%zd)\n", result);
268 return result;
272 /** Puts the device in firmware upload mode.*/
273 static
274 int mac_fw_upload_enable(struct i1480 *i1480)
276 int result;
277 u32 reg = 0x800000c0;
278 u32 *buffer = (u32 *)i1480->cmd_buf;
280 if (i1480->hw_rev > 1)
281 reg = 0x8000d0d4;
282 result = i1480->read(i1480, reg, sizeof(u32));
283 if (result < 0)
284 goto error_cmd;
285 *buffer &= ~i1480_FW_UPLOAD_MODE_MASK;
286 result = i1480->write(i1480, reg, buffer, sizeof(u32));
287 if (result < 0)
288 goto error_cmd;
289 return 0;
290 error_cmd:
291 dev_err(i1480->dev, "can't enable fw upload mode: %d\n", result);
292 return result;
296 /** Gets the device out of firmware upload mode. */
297 static
298 int mac_fw_upload_disable(struct i1480 *i1480)
300 int result;
301 u32 reg = 0x800000c0;
302 u32 *buffer = (u32 *)i1480->cmd_buf;
304 if (i1480->hw_rev > 1)
305 reg = 0x8000d0d4;
306 result = i1480->read(i1480, reg, sizeof(u32));
307 if (result < 0)
308 goto error_cmd;
309 *buffer |= i1480_FW_UPLOAD_MODE_MASK;
310 result = i1480->write(i1480, reg, buffer, sizeof(u32));
311 if (result < 0)
312 goto error_cmd;
313 return 0;
314 error_cmd:
315 dev_err(i1480->dev, "can't disable fw upload mode: %d\n", result);
316 return result;
322 * Generic function for uploading a MAC firmware.
324 * @i1480: Device instance
325 * @fw_name: Name of firmware file to upload.
326 * @fw_tag: Name of the firmware type (for messages)
327 * [eg: MAC, PRE]
328 * @do_wait: Wait for device to emit initialization done message (0
329 * for PRE fws, 1 for MAC fws).
330 * @returns: 0 if ok, < 0 errno on error.
332 static
333 int __mac_fw_upload(struct i1480 *i1480, const char *fw_name,
334 const char *fw_tag)
336 int result;
337 const struct firmware *fw;
338 struct fw_hdr *fw_hdrs;
340 d_fnstart(3, i1480->dev, "(%p, %s, %s)\n", i1480, fw_name, fw_tag);
341 result = request_firmware(&fw, fw_name, i1480->dev);
342 if (result < 0) /* Up to caller to complain on -ENOENT */
343 goto out;
344 d_printf(3, i1480->dev, "%s fw '%s': uploading\n", fw_tag, fw_name);
345 result = fw_hdrs_load(i1480, &fw_hdrs, fw->data, fw->size);
346 if (result < 0) {
347 dev_err(i1480->dev, "%s fw '%s': failed to parse firmware "
348 "file: %d\n", fw_tag, fw_name, result);
349 goto out_release;
351 result = mac_fw_upload_enable(i1480);
352 if (result < 0)
353 goto out_hdrs_release;
354 result = mac_fw_hdrs_push(i1480, fw_hdrs, fw_name, fw_tag);
355 mac_fw_upload_disable(i1480);
356 out_hdrs_release:
357 if (result >= 0)
358 dev_info(i1480->dev, "%s fw '%s': uploaded\n", fw_tag, fw_name);
359 else
360 dev_err(i1480->dev, "%s fw '%s': failed to upload (%d), "
361 "power cycle device\n", fw_tag, fw_name, result);
362 fw_hdrs_free(fw_hdrs);
363 out_release:
364 release_firmware(fw);
365 out:
366 d_fnend(3, i1480->dev, "(%p, %s, %s) = %d\n", i1480, fw_name, fw_tag,
367 result);
368 return result;
373 * Upload a pre-PHY firmware
376 int i1480_pre_fw_upload(struct i1480 *i1480)
378 int result;
379 result = __mac_fw_upload(i1480, i1480->pre_fw_name, "PRE");
380 if (result == 0)
381 msleep(400);
382 return result;
387 * Reset a the MAC and PHY
389 * @i1480: Device's instance
390 * @returns: 0 if ok, < 0 errno code on error
392 * We put the command on kmalloc'ed memory as some arches cannot do
393 * USB from the stack. The reply event is copied from an stage buffer,
394 * so it can be in the stack. See WUSB1.0[8.6.2.4] for more details.
396 * We issue the reset to make sure the UWB controller reinits the PHY;
397 * this way we can now if the PHY init went ok.
399 static
400 int i1480_cmd_reset(struct i1480 *i1480)
402 int result;
403 struct uwb_rccb *cmd = (void *) i1480->cmd_buf;
404 struct i1480_evt_reset {
405 struct uwb_rceb rceb;
406 u8 bResultCode;
407 } __attribute__((packed)) *reply = (void *) i1480->evt_buf;
409 result = -ENOMEM;
410 cmd->bCommandType = UWB_RC_CET_GENERAL;
411 cmd->wCommand = cpu_to_le16(UWB_RC_CMD_RESET);
412 reply->rceb.bEventType = UWB_RC_CET_GENERAL;
413 reply->rceb.wEvent = UWB_RC_CMD_RESET;
414 result = i1480_cmd(i1480, "RESET", sizeof(*cmd), sizeof(*reply));
415 if (result < 0)
416 goto out;
417 if (reply->bResultCode != UWB_RC_RES_SUCCESS) {
418 dev_err(i1480->dev, "RESET: command execution failed: %u\n",
419 reply->bResultCode);
420 result = -EIO;
422 out:
423 return result;
428 /** Wait for the MAC FW to start running */
429 static
430 int i1480_fw_is_running_q(struct i1480 *i1480)
432 int cnt = 0;
433 int result;
434 u32 *val = (u32 *) i1480->cmd_buf;
436 d_fnstart(3, i1480->dev, "(i1480 %p)\n", i1480);
437 for (cnt = 0; cnt < 10; cnt++) {
438 msleep(100);
439 result = i1480->read(i1480, 0x80080000, 4);
440 if (result < 0) {
441 dev_err(i1480->dev, "Can't read 0x8008000: %d\n", result);
442 goto out;
444 if (*val == 0x55555555UL) /* fw running? cool */
445 goto out;
446 if (printk_ratelimit())
447 d_printf(5, i1480->dev, "read #%d: 0x%08x\n", cnt, *val);
449 dev_err(i1480->dev, "Timed out waiting for fw to start\n");
450 result = -ETIMEDOUT;
451 out:
452 d_fnend(3, i1480->dev, "(i1480 %p) = %d\n", i1480, result);
453 return result;
459 * Upload MAC firmware, wait for it to start
461 * @i1480: Device instance
462 * @fw_name: Name of the file that contains the firmware
464 * This has to be called after the pre fw has been uploaded (if
465 * there is any).
467 int i1480_mac_fw_upload(struct i1480 *i1480)
469 int result = 0, deprecated_name = 0;
470 struct i1480_rceb *rcebe = (void *) i1480->evt_buf;
472 d_fnstart(3, i1480->dev, "(%p)\n", i1480);
473 result = __mac_fw_upload(i1480, i1480->mac_fw_name, "MAC");
474 if (result == -ENOENT) {
475 result = __mac_fw_upload(i1480, i1480->mac_fw_name_deprecate,
476 "MAC");
477 deprecated_name = 1;
479 if (result < 0)
480 return result;
481 if (deprecated_name == 1)
482 dev_warn(i1480->dev,
483 "WARNING: firmware file name %s is deprecated, "
484 "please rename to %s\n",
485 i1480->mac_fw_name_deprecate, i1480->mac_fw_name);
486 result = i1480_fw_is_running_q(i1480);
487 if (result < 0)
488 goto error_fw_not_running;
489 result = i1480->rc_setup ? i1480->rc_setup(i1480) : 0;
490 if (result < 0) {
491 dev_err(i1480->dev, "Cannot setup after MAC fw upload: %d\n",
492 result);
493 goto error_setup;
495 result = i1480->wait_init_done(i1480); /* wait init'on */
496 if (result < 0) {
497 dev_err(i1480->dev, "MAC fw '%s': Initialization timed out "
498 "(%d)\n", i1480->mac_fw_name, result);
499 goto error_init_timeout;
501 /* verify we got the right initialization done event */
502 if (i1480->evt_result != sizeof(*rcebe)) {
503 dev_err(i1480->dev, "MAC fw '%s': initialization event returns "
504 "wrong size (%zu bytes vs %zu needed)\n",
505 i1480->mac_fw_name, i1480->evt_result, sizeof(*rcebe));
506 dump_bytes(i1480->dev, rcebe, min(i1480->evt_result, (ssize_t)32));
507 goto error_size;
509 result = -EIO;
510 if (i1480_rceb_check(i1480, &rcebe->rceb, NULL, 0, i1480_CET_VS1,
511 i1480_EVT_RM_INIT_DONE) < 0) {
512 dev_err(i1480->dev, "wrong initialization event 0x%02x/%04x/%02x "
513 "received; expected 0x%02x/%04x/00\n",
514 rcebe->rceb.bEventType, le16_to_cpu(rcebe->rceb.wEvent),
515 rcebe->rceb.bEventContext, i1480_CET_VS1,
516 i1480_EVT_RM_INIT_DONE);
517 goto error_init_timeout;
519 result = i1480_cmd_reset(i1480);
520 if (result < 0)
521 dev_err(i1480->dev, "MAC fw '%s': MBOA reset failed (%d)\n",
522 i1480->mac_fw_name, result);
523 error_fw_not_running:
524 error_init_timeout:
525 error_size:
526 error_setup:
527 d_fnend(3, i1480->dev, "(i1480 %p) = %d\n", i1480, result);
528 return result;