mac.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. /*
  2. * Intel Wireless UWB Link 1480
  3. * MAC Firmware upload implementation
  4. *
  5. * Copyright (C) 2005-2006 Intel Corporation
  6. * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
  7. *
  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.
  11. *
  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.
  16. *
  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.
  21. *
  22. *
  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.
  28. */
  29. #include <linux/delay.h>
  30. #include <linux/firmware.h>
  31. #include <linux/slab.h>
  32. #include <linux/uwb.h>
  33. #include "i1480-dfu.h"
  34. /*
  35. * Descriptor for a continuous segment of MAC fw data
  36. */
  37. struct fw_hdr {
  38. unsigned long address;
  39. size_t length;
  40. const u32 *bin;
  41. struct fw_hdr *next;
  42. };
  43. /* Free a chain of firmware headers */
  44. static
  45. void fw_hdrs_free(struct fw_hdr *hdr)
  46. {
  47. struct fw_hdr *next;
  48. while (hdr) {
  49. next = hdr->next;
  50. kfree(hdr);
  51. hdr = next;
  52. }
  53. }
  54. /* Fill a firmware header descriptor from a memory buffer */
  55. static
  56. int fw_hdr_load(struct i1480 *i1480, struct fw_hdr *hdr, unsigned hdr_cnt,
  57. const char *_data, const u32 *data_itr, const u32 *data_top)
  58. {
  59. size_t hdr_offset = (const char *) data_itr - _data;
  60. size_t remaining_size = (void *) data_top - (void *) data_itr;
  61. if (data_itr + 2 > data_top) {
  62. dev_err(i1480->dev, "fw hdr #%u/%zu: EOF reached in header at "
  63. "offset %zu, limit %zu\n",
  64. hdr_cnt, hdr_offset,
  65. (const char *) data_itr + 2 - _data,
  66. (const char *) data_top - _data);
  67. return -EINVAL;
  68. }
  69. hdr->next = NULL;
  70. hdr->address = le32_to_cpu(*data_itr++);
  71. hdr->length = le32_to_cpu(*data_itr++);
  72. hdr->bin = data_itr;
  73. if (hdr->length > remaining_size) {
  74. dev_err(i1480->dev, "fw hdr #%u/%zu: EOF reached in data; "
  75. "chunk too long (%zu bytes), only %zu left\n",
  76. hdr_cnt, hdr_offset, hdr->length, remaining_size);
  77. return -EINVAL;
  78. }
  79. return 0;
  80. }
  81. /**
  82. * Get a buffer where the firmware is supposed to be and create a
  83. * chain of headers linking them together.
  84. *
  85. * @phdr: where to place the pointer to the first header (headers link
  86. * to the next via the @hdr->next ptr); need to free the whole
  87. * chain when done.
  88. *
  89. * @_data: Pointer to the data buffer.
  90. *
  91. * @_data_size: Size of the data buffer (bytes); data size has to be a
  92. * multiple of 4. Function will fail if not.
  93. *
  94. * Goes over the whole binary blob; reads the first chunk and creates
  95. * a fw hdr from it (which points to where the data is in @_data and
  96. * the length of the chunk); then goes on to the next chunk until
  97. * done. Each header is linked to the next.
  98. */
  99. static
  100. int fw_hdrs_load(struct i1480 *i1480, struct fw_hdr **phdr,
  101. const char *_data, size_t data_size)
  102. {
  103. int result;
  104. unsigned hdr_cnt = 0;
  105. u32 *data = (u32 *) _data, *data_itr, *data_top;
  106. struct fw_hdr *hdr, **prev_hdr = phdr;
  107. result = -EINVAL;
  108. /* Check size is ok and pointer is aligned */
  109. if (data_size % sizeof(u32) != 0)
  110. goto error;
  111. if ((unsigned long) _data % sizeof(u16) != 0)
  112. goto error;
  113. *phdr = NULL;
  114. data_itr = data;
  115. data_top = (u32 *) (_data + data_size);
  116. while (data_itr < data_top) {
  117. result = -ENOMEM;
  118. hdr = kmalloc(sizeof(*hdr), GFP_KERNEL);
  119. if (hdr == NULL) {
  120. dev_err(i1480->dev, "Cannot allocate fw header "
  121. "for chunk #%u\n", hdr_cnt);
  122. goto error_alloc;
  123. }
  124. result = fw_hdr_load(i1480, hdr, hdr_cnt,
  125. _data, data_itr, data_top);
  126. if (result < 0)
  127. goto error_load;
  128. data_itr += 2 + hdr->length;
  129. *prev_hdr = hdr;
  130. prev_hdr = &hdr->next;
  131. hdr_cnt++;
  132. };
  133. *prev_hdr = NULL;
  134. return 0;
  135. error_load:
  136. kfree(hdr);
  137. error_alloc:
  138. fw_hdrs_free(*phdr);
  139. error:
  140. return result;
  141. }
  142. /**
  143. * Compares a chunk of fw with one in the devices's memory
  144. *
  145. * @i1480: Device instance
  146. * @hdr: Pointer to the firmware chunk
  147. * @returns: 0 if equal, < 0 errno on error. If > 0, it is the offset
  148. * where the difference was found (plus one).
  149. *
  150. * Kind of dirty and simplistic, but does the trick in both the PCI
  151. * and USB version. We do a quick[er] memcmp(), and if it fails, we do
  152. * a byte-by-byte to find the offset.
  153. */
  154. static
  155. ssize_t i1480_fw_cmp(struct i1480 *i1480, struct fw_hdr *hdr)
  156. {
  157. ssize_t result = 0;
  158. u32 src_itr = 0, cnt;
  159. size_t size = hdr->length*sizeof(hdr->bin[0]);
  160. size_t chunk_size;
  161. u8 *bin = (u8 *) hdr->bin;
  162. while (size > 0) {
  163. chunk_size = size < i1480->buf_size ? size : i1480->buf_size;
  164. result = i1480->read(i1480, hdr->address + src_itr, chunk_size);
  165. if (result < 0) {
  166. dev_err(i1480->dev, "error reading for verification: "
  167. "%zd\n", result);
  168. goto error;
  169. }
  170. if (memcmp(i1480->cmd_buf, bin + src_itr, result)) {
  171. u8 *buf = i1480->cmd_buf;
  172. for (cnt = 0; cnt < result; cnt++)
  173. if (bin[src_itr + cnt] != buf[cnt]) {
  174. dev_err(i1480->dev, "byte failed at "
  175. "src_itr %u cnt %u [0x%02x "
  176. "vs 0x%02x]\n", src_itr, cnt,
  177. bin[src_itr + cnt], buf[cnt]);
  178. result = src_itr + cnt + 1;
  179. goto cmp_failed;
  180. }
  181. }
  182. src_itr += result;
  183. size -= result;
  184. }
  185. result = 0;
  186. error:
  187. cmp_failed:
  188. return result;
  189. }
  190. /**
  191. * Writes firmware headers to the device.
  192. *
  193. * @prd: PRD instance
  194. * @hdr: Processed firmware
  195. * @returns: 0 if ok, < 0 errno on error.
  196. */
  197. static
  198. int mac_fw_hdrs_push(struct i1480 *i1480, struct fw_hdr *hdr,
  199. const char *fw_name, const char *fw_tag)
  200. {
  201. struct device *dev = i1480->dev;
  202. ssize_t result = 0;
  203. struct fw_hdr *hdr_itr;
  204. int verif_retry_count;
  205. /* Now, header by header, push them to the hw */
  206. for (hdr_itr = hdr; hdr_itr != NULL; hdr_itr = hdr_itr->next) {
  207. verif_retry_count = 0;
  208. retry:
  209. dev_dbg(dev, "fw chunk (%zu @ 0x%08lx)\n",
  210. hdr_itr->length * sizeof(hdr_itr->bin[0]),
  211. hdr_itr->address);
  212. result = i1480->write(i1480, hdr_itr->address, hdr_itr->bin,
  213. hdr_itr->length*sizeof(hdr_itr->bin[0]));
  214. if (result < 0) {
  215. dev_err(dev, "%s fw '%s': write failed (%zuB @ 0x%lx):"
  216. " %zd\n", fw_tag, fw_name,
  217. hdr_itr->length * sizeof(hdr_itr->bin[0]),
  218. hdr_itr->address, result);
  219. break;
  220. }
  221. result = i1480_fw_cmp(i1480, hdr_itr);
  222. if (result < 0) {
  223. dev_err(dev, "%s fw '%s': verification read "
  224. "failed (%zuB @ 0x%lx): %zd\n",
  225. fw_tag, fw_name,
  226. hdr_itr->length * sizeof(hdr_itr->bin[0]),
  227. hdr_itr->address, result);
  228. break;
  229. }
  230. if (result > 0) { /* Offset where it failed + 1 */
  231. result--;
  232. dev_err(dev, "%s fw '%s': WARNING: verification "
  233. "failed at 0x%lx: retrying\n",
  234. fw_tag, fw_name, hdr_itr->address + result);
  235. if (++verif_retry_count < 3)
  236. goto retry; /* write this block again! */
  237. dev_err(dev, "%s fw '%s': verification failed at 0x%lx: "
  238. "tried %d times\n", fw_tag, fw_name,
  239. hdr_itr->address + result, verif_retry_count);
  240. result = -EINVAL;
  241. break;
  242. }
  243. }
  244. return result;
  245. }
  246. /** Puts the device in firmware upload mode.*/
  247. static
  248. int mac_fw_upload_enable(struct i1480 *i1480)
  249. {
  250. int result;
  251. u32 reg = 0x800000c0;
  252. u32 *buffer = (u32 *)i1480->cmd_buf;
  253. if (i1480->hw_rev > 1)
  254. reg = 0x8000d0d4;
  255. result = i1480->read(i1480, reg, sizeof(u32));
  256. if (result < 0)
  257. goto error_cmd;
  258. *buffer &= ~i1480_FW_UPLOAD_MODE_MASK;
  259. result = i1480->write(i1480, reg, buffer, sizeof(u32));
  260. if (result < 0)
  261. goto error_cmd;
  262. return 0;
  263. error_cmd:
  264. dev_err(i1480->dev, "can't enable fw upload mode: %d\n", result);
  265. return result;
  266. }
  267. /** Gets the device out of firmware upload mode. */
  268. static
  269. int mac_fw_upload_disable(struct i1480 *i1480)
  270. {
  271. int result;
  272. u32 reg = 0x800000c0;
  273. u32 *buffer = (u32 *)i1480->cmd_buf;
  274. if (i1480->hw_rev > 1)
  275. reg = 0x8000d0d4;
  276. result = i1480->read(i1480, reg, sizeof(u32));
  277. if (result < 0)
  278. goto error_cmd;
  279. *buffer |= i1480_FW_UPLOAD_MODE_MASK;
  280. result = i1480->write(i1480, reg, buffer, sizeof(u32));
  281. if (result < 0)
  282. goto error_cmd;
  283. return 0;
  284. error_cmd:
  285. dev_err(i1480->dev, "can't disable fw upload mode: %d\n", result);
  286. return result;
  287. }
  288. /**
  289. * Generic function for uploading a MAC firmware.
  290. *
  291. * @i1480: Device instance
  292. * @fw_name: Name of firmware file to upload.
  293. * @fw_tag: Name of the firmware type (for messages)
  294. * [eg: MAC, PRE]
  295. * @do_wait: Wait for device to emit initialization done message (0
  296. * for PRE fws, 1 for MAC fws).
  297. * @returns: 0 if ok, < 0 errno on error.
  298. */
  299. static
  300. int __mac_fw_upload(struct i1480 *i1480, const char *fw_name,
  301. const char *fw_tag)
  302. {
  303. int result;
  304. const struct firmware *fw;
  305. struct fw_hdr *fw_hdrs;
  306. result = request_firmware(&fw, fw_name, i1480->dev);
  307. if (result < 0) /* Up to caller to complain on -ENOENT */
  308. goto out;
  309. result = fw_hdrs_load(i1480, &fw_hdrs, fw->data, fw->size);
  310. if (result < 0) {
  311. dev_err(i1480->dev, "%s fw '%s': failed to parse firmware "
  312. "file: %d\n", fw_tag, fw_name, result);
  313. goto out_release;
  314. }
  315. result = mac_fw_upload_enable(i1480);
  316. if (result < 0)
  317. goto out_hdrs_release;
  318. result = mac_fw_hdrs_push(i1480, fw_hdrs, fw_name, fw_tag);
  319. mac_fw_upload_disable(i1480);
  320. out_hdrs_release:
  321. if (result >= 0)
  322. dev_info(i1480->dev, "%s fw '%s': uploaded\n", fw_tag, fw_name);
  323. else
  324. dev_err(i1480->dev, "%s fw '%s': failed to upload (%d), "
  325. "power cycle device\n", fw_tag, fw_name, result);
  326. fw_hdrs_free(fw_hdrs);
  327. out_release:
  328. release_firmware(fw);
  329. out:
  330. return result;
  331. }
  332. /**
  333. * Upload a pre-PHY firmware
  334. *
  335. */
  336. int i1480_pre_fw_upload(struct i1480 *i1480)
  337. {
  338. int result;
  339. result = __mac_fw_upload(i1480, i1480->pre_fw_name, "PRE");
  340. if (result == 0)
  341. msleep(400);
  342. return result;
  343. }
  344. /**
  345. * Reset a the MAC and PHY
  346. *
  347. * @i1480: Device's instance
  348. * @returns: 0 if ok, < 0 errno code on error
  349. *
  350. * We put the command on kmalloc'ed memory as some arches cannot do
  351. * USB from the stack. The reply event is copied from an stage buffer,
  352. * so it can be in the stack. See WUSB1.0[8.6.2.4] for more details.
  353. *
  354. * We issue the reset to make sure the UWB controller reinits the PHY;
  355. * this way we can now if the PHY init went ok.
  356. */
  357. static
  358. int i1480_cmd_reset(struct i1480 *i1480)
  359. {
  360. int result;
  361. struct uwb_rccb *cmd = (void *) i1480->cmd_buf;
  362. struct i1480_evt_reset {
  363. struct uwb_rceb rceb;
  364. u8 bResultCode;
  365. } __attribute__((packed)) *reply = (void *) i1480->evt_buf;
  366. result = -ENOMEM;
  367. cmd->bCommandType = UWB_RC_CET_GENERAL;
  368. cmd->wCommand = cpu_to_le16(UWB_RC_CMD_RESET);
  369. reply->rceb.bEventType = UWB_RC_CET_GENERAL;
  370. reply->rceb.wEvent = UWB_RC_CMD_RESET;
  371. result = i1480_cmd(i1480, "RESET", sizeof(*cmd), sizeof(*reply));
  372. if (result < 0)
  373. goto out;
  374. if (reply->bResultCode != UWB_RC_RES_SUCCESS) {
  375. dev_err(i1480->dev, "RESET: command execution failed: %u\n",
  376. reply->bResultCode);
  377. result = -EIO;
  378. }
  379. out:
  380. return result;
  381. }
  382. /* Wait for the MAC FW to start running */
  383. static
  384. int i1480_fw_is_running_q(struct i1480 *i1480)
  385. {
  386. int cnt = 0;
  387. int result;
  388. u32 *val = (u32 *) i1480->cmd_buf;
  389. for (cnt = 0; cnt < 10; cnt++) {
  390. msleep(100);
  391. result = i1480->read(i1480, 0x80080000, 4);
  392. if (result < 0) {
  393. dev_err(i1480->dev, "Can't read 0x8008000: %d\n", result);
  394. goto out;
  395. }
  396. if (*val == 0x55555555UL) /* fw running? cool */
  397. goto out;
  398. }
  399. dev_err(i1480->dev, "Timed out waiting for fw to start\n");
  400. result = -ETIMEDOUT;
  401. out:
  402. return result;
  403. }
  404. /**
  405. * Upload MAC firmware, wait for it to start
  406. *
  407. * @i1480: Device instance
  408. * @fw_name: Name of the file that contains the firmware
  409. *
  410. * This has to be called after the pre fw has been uploaded (if
  411. * there is any).
  412. */
  413. int i1480_mac_fw_upload(struct i1480 *i1480)
  414. {
  415. int result = 0, deprecated_name = 0;
  416. struct i1480_rceb *rcebe = (void *) i1480->evt_buf;
  417. result = __mac_fw_upload(i1480, i1480->mac_fw_name, "MAC");
  418. if (result == -ENOENT) {
  419. result = __mac_fw_upload(i1480, i1480->mac_fw_name_deprecate,
  420. "MAC");
  421. deprecated_name = 1;
  422. }
  423. if (result < 0)
  424. return result;
  425. if (deprecated_name == 1)
  426. dev_warn(i1480->dev,
  427. "WARNING: firmware file name %s is deprecated, "
  428. "please rename to %s\n",
  429. i1480->mac_fw_name_deprecate, i1480->mac_fw_name);
  430. result = i1480_fw_is_running_q(i1480);
  431. if (result < 0)
  432. goto error_fw_not_running;
  433. result = i1480->rc_setup ? i1480->rc_setup(i1480) : 0;
  434. if (result < 0) {
  435. dev_err(i1480->dev, "Cannot setup after MAC fw upload: %d\n",
  436. result);
  437. goto error_setup;
  438. }
  439. result = i1480->wait_init_done(i1480); /* wait init'on */
  440. if (result < 0) {
  441. dev_err(i1480->dev, "MAC fw '%s': Initialization timed out "
  442. "(%d)\n", i1480->mac_fw_name, result);
  443. goto error_init_timeout;
  444. }
  445. /* verify we got the right initialization done event */
  446. if (i1480->evt_result != sizeof(*rcebe)) {
  447. dev_err(i1480->dev, "MAC fw '%s': initialization event returns "
  448. "wrong size (%zu bytes vs %zu needed)\n",
  449. i1480->mac_fw_name, i1480->evt_result, sizeof(*rcebe));
  450. goto error_size;
  451. }
  452. result = -EIO;
  453. if (i1480_rceb_check(i1480, &rcebe->rceb, NULL, 0, i1480_CET_VS1,
  454. i1480_EVT_RM_INIT_DONE) < 0) {
  455. dev_err(i1480->dev, "wrong initialization event 0x%02x/%04x/%02x "
  456. "received; expected 0x%02x/%04x/00\n",
  457. rcebe->rceb.bEventType, le16_to_cpu(rcebe->rceb.wEvent),
  458. rcebe->rceb.bEventContext, i1480_CET_VS1,
  459. i1480_EVT_RM_INIT_DONE);
  460. goto error_init_timeout;
  461. }
  462. result = i1480_cmd_reset(i1480);
  463. if (result < 0)
  464. dev_err(i1480->dev, "MAC fw '%s': MBOA reset failed (%d)\n",
  465. i1480->mac_fw_name, result);
  466. error_fw_not_running:
  467. error_init_timeout:
  468. error_size:
  469. error_setup:
  470. return result;
  471. }