fsl_updater.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. /*
  2. * Freescale UUT driver
  3. *
  4. * Copyright 2008-2013 Freescale Semiconductor, Inc.
  5. * Copyright 2008-2009 Embedded Alley Solutions, Inc All Rights Reserved.
  6. */
  7. /*
  8. * The code contained herein is licensed under the GNU General Public
  9. * License. You may obtain a copy of the GNU General Public License
  10. * Version 2 or later at the following locations:
  11. *
  12. * http://www.opensource.org/licenses/gpl-license.html
  13. * http://www.gnu.org/copyleft/gpl.html
  14. */
  15. #include <common.h>
  16. #include <asm/arch/vf610_qspi.h>
  17. #define pr_debug printf
  18. #define pr_info printf
  19. #define UTP_FLAG_COMMAND 0x00000001
  20. #define UTP_FLAG_DATA 0x00000002
  21. #define UTP_FLAG_STATUS 0x00000004
  22. #define UTP_FLAG_REPORT_BUSY 0x10000000
  23. struct fsg_common;
  24. enum utp_media_type {
  25. NOMEDIA = 0,
  26. SD,
  27. NAND,
  28. NOR,
  29. SPI,
  30. QSPI,
  31. };
  32. struct utp_pipe_info {
  33. enum utp_media_type media_type;
  34. int media_id;
  35. unsigned long int index;
  36. size_t blksz;
  37. unsigned long int addr;
  38. } pinfo = {
  39. .media_type = NOMEDIA,
  40. .media_id = 0,
  41. .index = 0,
  42. .blksz = 0,
  43. };
  44. static u64 get_be64(u8 *buf)
  45. {
  46. return ((u64)get_unaligned_be32(buf) << 32) |
  47. get_unaligned_be32(buf + 4);
  48. }
  49. static int utp_init(struct fsg_dev *fsg)
  50. {
  51. /* the max message is 64KB */
  52. utp_context.buffer = vmalloc(0x10000);
  53. if (!utp_context.buffer) {
  54. printf("not enough memory!\n");
  55. return -EIO;
  56. }
  57. utp_context.utp_version = 0x1ull;
  58. fsg->utp = &utp_context;
  59. return 0;
  60. }
  61. static void utp_exit(struct fsg_dev *fsg)
  62. {
  63. vfree(utp_context.buffer);
  64. }
  65. static struct utp_user_data *utp_user_data_alloc(size_t blksz, size_t size)
  66. {
  67. struct utp_user_data *uud;
  68. size_t malloc_size = size + sizeof(*uud) + blksz;
  69. uud = malloc(malloc_size);
  70. if (!uud)
  71. return uud;
  72. memset(uud, 0, malloc_size);
  73. uud->data.size = size + sizeof(uud->data);
  74. return uud;
  75. }
  76. static void utp_user_data_free(struct utp_user_data *uud)
  77. {
  78. kfree(uud);
  79. }
  80. /* Will be called when the host wants to get the sense data */
  81. static int utp_get_sense(struct fsg_common *common)
  82. {
  83. struct fsg_dev *fsg;
  84. fsg = common->fsg;
  85. if (UTP_CTX(fsg)->processed == 0)
  86. return -1;
  87. UTP_CTX(fsg)->processed = 0;
  88. return 0;
  89. }
  90. static int utp_do_read(struct fsg_common *common, void *data, size_t size)
  91. {
  92. struct fsg_buffhd *bh;
  93. struct fsg_dev *fsg;
  94. int rc;
  95. u32 amount_left;
  96. unsigned int amount;
  97. /* Get the starting Logical Block Address and check that it's
  98. * not too big */
  99. fsg = common->fsg;
  100. amount_left = size;
  101. if (unlikely(amount_left == 0))
  102. return -EIO; /* No default reply*/
  103. pr_debug("%s: sending %d\n", __func__, size);
  104. for (;;) {
  105. /* Figure out how much we need to read:
  106. * Try to read the remaining amount.
  107. * But don't read more than the buffer size.
  108. * And don't try to read past the end of the file.
  109. * Finally, if we're not at a page boundary, don't read past
  110. * the next page.
  111. * If this means reading 0 then we were asked to read past
  112. * the end of file. */
  113. amount = min((unsigned int) amount_left, FSG_BUFLEN);
  114. /* Wait for the next buffer to become available */
  115. bh = common->next_buffhd_to_fill;
  116. while (bh->state != BUF_STATE_EMPTY) {
  117. rc = sleep_thread(common);
  118. if (rc)
  119. return rc;
  120. }
  121. /* If we were asked to read past the end of file,
  122. * end with an empty buffer. */
  123. if (amount == 0) {
  124. bh->inreq->length = 0;
  125. bh->state = BUF_STATE_FULL;
  126. break;
  127. }
  128. /* Perform the read */
  129. pr_info("Copied to %p, %d bytes started from %d\n",
  130. bh->buf, amount, size - amount_left);
  131. /* from upt buffer to file_storeage buffer */
  132. memcpy(bh->buf, data + size - amount_left, amount);
  133. amount_left -= amount;
  134. common->residue -= amount;
  135. bh->inreq->length = amount;
  136. bh->state = BUF_STATE_FULL;
  137. /* Send this buffer and go read some more */
  138. bh->inreq->zero = 0;
  139. /* USB Physical transfer: Data from device to host */
  140. start_transfer(fsg, fsg->bulk_in, bh->inreq,
  141. &bh->inreq_busy, &bh->state);
  142. common->next_buffhd_to_fill = bh->next;
  143. if (amount_left <= 0)
  144. break;
  145. }
  146. return size - amount_left;
  147. }
  148. static int utp_do_write(struct fsg_common *common, void *data, size_t size)
  149. {
  150. struct fsg_buffhd *bh;
  151. struct fsg_dev *fsg;
  152. int get_some_more;
  153. u32 amount_left_to_req, amount_left_to_write;
  154. unsigned int amount;
  155. int rc;
  156. loff_t offset;
  157. fsg = common->fsg;
  158. /* Carry out the file writes */
  159. get_some_more = 1;
  160. amount_left_to_req = amount_left_to_write = size;
  161. if (unlikely(amount_left_to_write == 0))
  162. return -EIO;
  163. offset = 0;
  164. while (amount_left_to_write > 0) {
  165. /* Queue a request for more data from the host */
  166. bh = common->next_buffhd_to_fill;
  167. if (bh->state == BUF_STATE_EMPTY && get_some_more) {
  168. /* Figure out how much we want to get:
  169. * Try to get the remaining amount.
  170. * But don't get more than the buffer size.
  171. * And don't try to go past the end of the file.
  172. * If we're not at a page boundary,
  173. * don't go past the next page.
  174. * If this means getting 0, then we were asked
  175. * to write past the end of file.
  176. * Finally, round down to a block boundary. */
  177. amount = min(amount_left_to_req, FSG_BUFLEN);
  178. if (amount == 0) {
  179. get_some_more = 0;
  180. /* cry now */
  181. continue;
  182. }
  183. /* Get the next buffer */
  184. amount_left_to_req -= amount;
  185. if (amount_left_to_req == 0)
  186. get_some_more = 0;
  187. /* amount is always divisible by 512, hence by
  188. * the bulk-out maxpacket size */
  189. bh->outreq->length = bh->bulk_out_intended_length =
  190. amount;
  191. bh->outreq->short_not_ok = 1;
  192. start_transfer(fsg, fsg->bulk_out, bh->outreq,
  193. &bh->outreq_busy, &bh->state);
  194. common->next_buffhd_to_fill = bh->next;
  195. continue;
  196. }
  197. /* Write the received data to the backing file */
  198. bh = common->next_buffhd_to_drain;
  199. if (bh->state == BUF_STATE_EMPTY && !get_some_more)
  200. break; /* We stopped early */
  201. if (bh->state == BUF_STATE_FULL) {
  202. common->next_buffhd_to_drain = bh->next;
  203. bh->state = BUF_STATE_EMPTY;
  204. /* Did something go wrong with the transfer? */
  205. if (bh->outreq->status != 0)
  206. /* cry again, COMMUNICATION_FAILURE */
  207. break;
  208. amount = bh->outreq->actual;
  209. /* Perform the write */
  210. memcpy(data + offset, bh->buf, amount);
  211. offset += amount;
  212. amount_left_to_write -= amount;
  213. common->residue -= amount;
  214. /* Did the host decide to stop early? */
  215. if (bh->outreq->actual != bh->outreq->length) {
  216. common->short_packet_received = 1;
  217. break;
  218. }
  219. continue;
  220. }
  221. /* Wait for something to happen */
  222. rc = sleep_thread(common);
  223. if (rc)
  224. return rc;
  225. }
  226. return -EIO;
  227. }
  228. static inline void utp_set_sense(struct fsg_common *common, u16 code, u64 reply)
  229. {
  230. struct fsg_dev *fsg;
  231. fsg = common->fsg;
  232. UTP_CTX(fsg)->processed = true;
  233. UTP_CTX(fsg)->sdinfo = reply & 0xFFFFFFFF;
  234. UTP_CTX(fsg)->sdinfo_h = (reply >> 32) & 0xFFFFFFFF;
  235. UTP_CTX(fsg)->sd = (UTP_SENSE_KEY << 16) | code;
  236. }
  237. static void utp_poll(struct fsg_common *common)
  238. {
  239. UTP_SS_PASS(common);
  240. return;
  241. }
  242. static int utp_exec(struct fsg_common *common,
  243. char *command,
  244. int cmdsize,
  245. unsigned long long payload)
  246. {
  247. struct fsg_dev *fsg = common->fsg;
  248. struct utp_user_data *uud = NULL, *uud2r;
  249. struct utp_context *ctx = UTP_CTX(fsg);
  250. uud2r = utp_user_data_alloc(0, cmdsize + 1);
  251. uud2r->data.flags = UTP_FLAG_COMMAND;
  252. uud2r->data.payload = payload;
  253. strncpy(uud2r->data.command, command, cmdsize);
  254. uud = utp_interpret_message(uud2r);
  255. utp_user_data_free(uud2r);
  256. if (command[0] == '!') /* there will be no response */
  257. return 0;
  258. #ifdef DEBUG
  259. pr_info("UUD:\n\tFlags = %02X\n", uud->data.flags);
  260. if (uud->data.flags & UTP_FLAG_DATA) {
  261. pr_info("\tbufsize = %d\n", uud->data.bufsize);
  262. print_hex_dump(KERN_DEBUG, "\t", DUMP_PREFIX_NONE,
  263. 16, 2, uud->data.data, uud->data.bufsize, true);
  264. }
  265. if (uud->data.flags & UTP_FLAG_REPORT_BUSY)
  266. pr_info("\tBUSY\n");
  267. #endif
  268. if (uud->data.flags & UTP_FLAG_DATA) {
  269. memcpy(ctx->buffer, uud->data.data, uud->data.bufsize);
  270. UTP_SS_SIZE(common, uud->data.bufsize);
  271. } else if (uud->data.flags & UTP_FLAG_REPORT_BUSY) {
  272. ctx->counter = 0xFFFF;
  273. UTP_SS_BUSY(common, ctx->counter);
  274. } else if (uud->data.flags & UTP_FLAG_STATUS) {
  275. printk(KERN_WARNING "%s: exit with status %d\n", __func__,
  276. uud->data.status);
  277. UTP_SS_EXIT(common, uud->data.status);
  278. } else {
  279. pr_debug("%s: pass\n", __func__);
  280. UTP_SS_PASS(common);
  281. }
  282. utp_user_data_free(uud);
  283. return 0;
  284. }
  285. static int utp_send_status(struct fsg_common *common)
  286. {
  287. struct fsg_buffhd *bh;
  288. struct fsg_dev *fsg = common->fsg;
  289. u8 status = USB_STATUS_PASS;
  290. struct bulk_cs_wrap *csw;
  291. int rc;
  292. /* Wait for the next buffer to become available */
  293. bh = common->next_buffhd_to_fill;
  294. while (bh->state != BUF_STATE_EMPTY) {
  295. rc = sleep_thread(common);
  296. if (rc)
  297. return rc;
  298. }
  299. if (common->phase_error) {
  300. DBG(fsg, "sending phase-error status\n");
  301. status = USB_STATUS_PHASE_ERROR;
  302. } else if ((UTP_CTX(fsg)->sd & 0xFFFF) != UTP_REPLY_PASS) {
  303. status = USB_STATUS_FAIL;
  304. }
  305. csw = bh->buf;
  306. /* Store and send the Bulk-only CSW */
  307. csw->Signature = __constant_cpu_to_le32(USB_BULK_CS_SIG);
  308. csw->Tag = common->tag;
  309. csw->Residue = cpu_to_le32(common->residue);
  310. csw->Status = status;
  311. bh->inreq->length = USB_BULK_CS_WRAP_LEN;
  312. bh->inreq->zero = 0;
  313. start_transfer(fsg, fsg->bulk_in, bh->inreq,
  314. &bh->inreq_busy, &bh->state);
  315. common->next_buffhd_to_fill = bh->next;
  316. return 0;
  317. }
  318. static int utp_handle_message(struct fsg_common *common,
  319. u8 *cdb_data,
  320. int default_reply)
  321. {
  322. struct utp_msg *m = (struct utp_msg *)cdb_data;
  323. struct fsg_dev *fsg = common->fsg;
  324. void *data = NULL;
  325. int r;
  326. struct utp_user_data *uud2r, *uud;
  327. unsigned long long param;
  328. unsigned long tag;
  329. if (m->f0 != 0xF0)
  330. return default_reply;
  331. tag = get_unaligned_be32((void *)&m->utp_msg_tag);
  332. param = get_be64((void *)&m->param);
  333. pr_debug("Type 0x%x, tag 0x%08lx, param %llx\n",
  334. m->utp_msg_type, tag, param);
  335. switch ((enum utp_msg_type)m->utp_msg_type) {
  336. case UTP_POLL:
  337. if (get_be64((void *)&m->param) == 1) {
  338. pr_debug("%s: version request\n", __func__);
  339. UTP_SS_EXIT(common, UTP_CTX(fsg)->utp_version);
  340. break;
  341. }
  342. utp_poll(common);
  343. break;
  344. case UTP_EXEC:
  345. pr_debug("%s: EXEC\n", __func__);
  346. data = kzalloc(common->data_size, GFP_KERNEL);
  347. /* copy data from usb buffer to utp buffer */
  348. utp_do_write(common, data, common->data_size);
  349. utp_exec(common, data, common->data_size, param);
  350. kfree(data);
  351. break;
  352. case UTP_GET: /* data from device to host */
  353. pr_debug("%s: GET, %d bytes\n", __func__, common->data_size);
  354. r = utp_do_read(common, UTP_CTX(fsg)->buffer, common->data_size);
  355. UTP_SS_PASS(common);
  356. break;
  357. case UTP_PUT: /* data from host to device */
  358. pr_debug("%s: PUT, %d bytes\n", __func__, common->data_size);
  359. uud2r = utp_user_data_alloc(pinfo.blksz, common->data_size);
  360. if (!uud2r) {
  361. printf("utp alloc fail\n");
  362. return -ENOMEM;
  363. }
  364. uud2r->data.bufsize = common->data_size;
  365. uud2r->data.flags = UTP_FLAG_DATA;
  366. utp_do_write(common, uud2r->data.data, common->data_size);
  367. /* don't know what will be written */
  368. utp_interpret_message(uud2r);
  369. utp_user_data_free(uud2r);
  370. /*
  371. * Return PASS or FAIL according to uuc's status
  372. * Please open it if need to check uuc's status
  373. * and use another version uuc
  374. */
  375. #if 0
  376. struct utp_user_data *uud = NULL;
  377. struct utp_context *ctx;
  378. WAIT_ACTIVITY(write);
  379. ctx = UTP_CTX(fsg);
  380. mutex_lock(&ctx->lock);
  381. if (!list_empty(&ctx->write))
  382. uud = list_first_entry(&ctx->write,
  383. struct utp_user_data, link);
  384. mutex_unlock(&ctx->lock);
  385. if (uud) {
  386. if (uud->data.flags & UTP_FLAG_STATUS) {
  387. printk(KERN_WARNING "%s: exit with status %d\n",
  388. __func__, uud->data.status);
  389. UTP_SS_EXIT(fsg, uud->data.status);
  390. } else {
  391. pr_debug("%s: pass\n", __func__);
  392. UTP_SS_PASS(fsg);
  393. }
  394. utp_user_data_free(uud);
  395. } else{
  396. UTP_SS_PASS(fsg);
  397. }
  398. #endif
  399. /* workaround, normally would SS_PASS */
  400. if (common->data_size == 65536)
  401. {
  402. pr_debug("%s: Sending UTP_SS_BUSY\n", __func__);
  403. UTP_SS_BUSY(common, UTP_CTX(fsg)->counter);
  404. }
  405. else
  406. {
  407. pr_debug("%s: Sending UTP_SS_PASS\n", __func__);
  408. UTP_SS_PASS(common);
  409. }
  410. break;
  411. }
  412. utp_send_status(common);
  413. return -1;
  414. }
  415. u32 utp_run(char *cmd)
  416. {
  417. return run_command(cmd, 0);
  418. }
  419. struct utp_user_data *utp_handle_command(char *cmd, u64 payload)
  420. {
  421. u32 flags, status;
  422. size_t size;
  423. struct utp_user_data *answer;
  424. unsigned long int dev;
  425. unsigned long int skip;
  426. unsigned long int addr;
  427. char argbuf[20];
  428. char *tmp;
  429. /* defaults */
  430. status = 0;
  431. flags = 0;
  432. size = 0;
  433. if (cmd[0] == '$') {
  434. status = utp_run(cmd + 2);
  435. if (status)
  436. flags = UTP_FLAG_STATUS;
  437. } else if (strncmp(cmd, "pipesd", 6) == 0) {
  438. pinfo.media_type = SD;
  439. pinfo.index = 0;
  440. pinfo.blksz = 512; /* TODO: find sd card block size */
  441. cmd += 7;
  442. tmp = strsep(&cmd, " ");
  443. printf(tmp);
  444. dev = simple_strtoul(tmp+4, NULL, 10);
  445. printf("dev: %lu\n", dev);
  446. pinfo.media_id = dev;
  447. sprintf(argbuf, "mmc dev %lu", dev);
  448. tmp = strsep(&cmd, " ");
  449. printf(tmp);
  450. skip = simple_strtoul(tmp+5, NULL, 10);
  451. pinfo.index += skip;
  452. utp_run(argbuf);
  453. } else if (strncmp(cmd, "pipeqspi", 8) == 0) {
  454. pinfo.media_type = QSPI;
  455. cmd += 9;
  456. tmp = strsep(&cmd, " ");
  457. if (strncmp(tmp, "addr=", 5) == 0) {
  458. addr = simple_strtoul(tmp+5, NULL, 16);
  459. printf("addr: 0x%x\n", addr);
  460. pinfo.addr = addr;
  461. } else {
  462. pinfo.addr = (pinfo.media_id ? QSPI1_FLASH_BASE_ADDR
  463. : QSPI0_FLASH_BASE_ADDR);
  464. }
  465. } else if (strncmp(cmd, "qspiinit", 8) == 0) {
  466. pinfo.media_type = QSPI;
  467. pinfo.index = 0;
  468. pinfo.blksz = 0x200;
  469. sprintf(argbuf, cmd);
  470. cmd += 7;
  471. tmp = strsep(&cmd, " ");
  472. printf(tmp);
  473. dev = simple_strtoul(tmp+4, NULL, 10);
  474. printf("dev: %lu\n", dev);
  475. pinfo.media_id = dev;
  476. printf("%s\n", cmd);
  477. utp_run(argbuf);
  478. }
  479. else if (strncmp (cmd, "ubootcmd", 8) == 0) {
  480. sprintf(argbuf, cmd + 8);
  481. utp_run(argbuf);
  482. }
  483. answer = utp_user_data_alloc(0, size);
  484. if (flags & UTP_FLAG_STATUS)
  485. answer->data.status = status;
  486. return answer;
  487. }
  488. int utp_pipe_sd(u8 *data, size_t bufsize)
  489. {
  490. pr_debug("got data chunk, size: %zx\n", bufsize);
  491. unsigned int blkcnt;
  492. char argbuf[100];
  493. blkcnt = (bufsize / pinfo.blksz) + (((bufsize % pinfo.blksz) != 0) ? 1 : 0);
  494. pr_debug("blkcnt: %x, index: %lx, bufsize: %zx\n", blkcnt, pinfo.index, bufsize);
  495. sprintf(argbuf, "mmc write %x %lx %x", data, pinfo.index, blkcnt);
  496. utp_run(argbuf);
  497. pr_debug("after write to sd\n");
  498. pinfo.index += blkcnt;
  499. return 0;
  500. }
  501. int utp_pipe_qspi(u8 *data, size_t bufsize)
  502. {
  503. pr_debug("got data chunk, size: %zx\n", bufsize);
  504. unsigned int blkcnt, dest, len;
  505. char argbuf[100];
  506. blkcnt = (bufsize / pinfo.blksz) + (((bufsize % pinfo.blksz) != 0) ? 1 : 0);
  507. dest = pinfo.addr + pinfo.blksz * pinfo.index;
  508. len = bufsize;
  509. pr_debug("dest: %x, data: %lx, len: %zx\n", dest, data, len);
  510. sprintf(argbuf, "qspiwrite %x %lx %x",dest, data, len);
  511. pr_debug("%s\n", argbuf);
  512. utp_run(argbuf);
  513. pr_debug("after write to qspi\n");
  514. pinfo.index += blkcnt;
  515. return 0;
  516. }
  517. struct utp_user_data *utp_interpret_message(struct utp_user_data *uud2r)
  518. {
  519. struct utp_user_data *answer;
  520. struct utp_message *msg = &uud2r->data;
  521. if (msg->flags & UTP_FLAG_COMMAND) {
  522. answer = utp_handle_command(msg->command, msg->payload);
  523. if (answer)
  524. return answer;
  525. } else if (msg->flags & UTP_FLAG_DATA) {
  526. switch(pinfo.media_type) {
  527. case SD:
  528. utp_pipe_sd(msg->data, msg->bufsize);
  529. break;
  530. case QSPI:
  531. utp_pipe_qspi(msg->data, msg->bufsize);
  532. default:
  533. break;
  534. }
  535. }
  536. return NULL;
  537. }