mac.c 14 KB

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