ffu.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907
  1. /*
  2. * * ffu.c
  3. *
  4. * Copyright 2007-2008 Pierre Ossman
  5. *
  6. * Modified by SanDisk Corp., Copyright (c) 2013 SanDisk Corp.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or (at
  11. * your option) any later version.
  12. *
  13. * This program includes bug.h, card.h, host.h, mmc.h, scatterlist.h,
  14. * slab.h, ffu.h & swap.h header files
  15. * The original, unmodified version of this program - the mmc_test.c
  16. * file - is obtained under the GPL v2.0 license that is available via
  17. * http://www.gnu.org/licenses/,
  18. * or http://www.opensource.org/licenses/gpl-2.0.php
  19. */
  20. #include <linux/bug.h>
  21. #include <linux/errno.h>
  22. #include <linux/mmc/ioctl.h>
  23. #include <linux/mmc/card.h>
  24. #include <linux/mmc/host.h>
  25. #include <linux/mmc/mmc.h>
  26. #include <linux/scatterlist.h>
  27. #include <linux/slab.h>
  28. #include <linux/swap.h>
  29. #include <linux/mmc/ffu.h>
  30. #include "../core/core.h"
  31. #include <asm/uaccess.h>
  32. #define FFU_BUS_FREQ 25000000
  33. /**
  34. * struct mmc_ffu_pages - pages allocated by 'alloc_pages()'.
  35. * <at> page: first page in the allocation
  36. * <at> order: order of the number of pages allocated
  37. */
  38. struct mmc_ffu_pages {
  39. struct page *page;
  40. unsigned int order;
  41. };
  42. /**
  43. * struct mmc_ffu_mem - allocated memory.
  44. * <at> arr: array of allocations
  45. * <at> cnt: number of allocations
  46. */
  47. struct mmc_ffu_mem {
  48. struct mmc_ffu_pages *arr;
  49. unsigned int cnt;
  50. };
  51. struct mmc_ffu_area {
  52. unsigned long max_sz;
  53. unsigned int max_tfr;
  54. unsigned int max_segs;
  55. unsigned int max_seg_sz;
  56. unsigned int blocks;
  57. unsigned int sg_len;
  58. struct mmc_ffu_mem *mem;
  59. struct scatterlist *sg;
  60. };
  61. /* This strcut is cloned from mmc/card/block.c without modification*/
  62. struct mmc_blk_ioc_data {
  63. struct mmc_ioc_cmd ic;
  64. unsigned char *buf;
  65. u64 buf_bytes;
  66. };
  67. /*
  68. * Turn the cache ON/OFF.
  69. * Turning the cache OFF shall trigger flushing of the data
  70. * to the non-volatile storage.
  71. * This function should be called with host claimed
  72. */
  73. int mmc_ffu_cache_ctrl(struct mmc_host *host, u8 enable)
  74. {
  75. struct mmc_card *card = host->card;
  76. unsigned int timeout;
  77. int err = 0;
  78. #ifdef CONFIG_MTK_EMMC_CACHE
  79. if (card->quirks & MMC_QUIRK_DISABLE_CACHE)
  80. return err;
  81. #endif
  82. if (card && mmc_card_mmc(card) &&
  83. (card->ext_csd.cache_size > 0)) {
  84. enable = !!enable;
  85. if (card->ext_csd.cache_ctrl ^ enable) {
  86. timeout = enable ? card->ext_csd.generic_cmd6_time : 0;
  87. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  88. EXT_CSD_CACHE_CTRL, enable, timeout);
  89. if (err)
  90. pr_err("%s: cache %s error %d\n",
  91. mmc_hostname(card->host),
  92. enable ? "on" : "off",
  93. err);
  94. else
  95. card->ext_csd.cache_ctrl = enable;
  96. }
  97. }
  98. return err;
  99. }
  100. /* This function is cloned from mmc_blk_ioctl_copy_from_user() and only change
  101. MMC_IOC_MAX_BYTES as MMC_FFU_IOC_MAX_BYTES */
  102. struct mmc_blk_ioc_data *mmc_ffu_ioctl_copy_from_user(
  103. struct mmc_ioc_cmd __user *user)
  104. {
  105. struct mmc_blk_ioc_data *idata;
  106. int err;
  107. idata = kzalloc(sizeof(*idata), GFP_KERNEL);
  108. if (!idata) {
  109. err = -ENOMEM;
  110. goto out;
  111. }
  112. if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
  113. err = -EFAULT;
  114. goto idata_err;
  115. }
  116. idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
  117. if (idata->buf_bytes > MMC_FFU_IOC_MAX_BYTES) {
  118. err = -EOVERFLOW;
  119. goto idata_err;
  120. }
  121. if (!idata->buf_bytes)
  122. return idata;
  123. idata->buf = kzalloc(idata->buf_bytes, GFP_KERNEL);
  124. if (!idata->buf) {
  125. err = -ENOMEM;
  126. goto idata_err;
  127. }
  128. if (copy_from_user(idata->buf, (void __user *)(unsigned long)
  129. idata->ic.data_ptr, idata->buf_bytes)) {
  130. err = -EFAULT;
  131. goto copy_err;
  132. }
  133. return idata;
  134. copy_err:
  135. kfree(idata->buf);
  136. idata_err:
  137. kfree(idata);
  138. out:
  139. return ERR_PTR(err);
  140. }
  141. static void mmc_ffu_prepare_mrq(struct mmc_card *card,
  142. struct mmc_request *mrq, struct scatterlist *sg, unsigned int sg_len,
  143. u32 arg, unsigned int blocks, unsigned int blksz, int write)
  144. {
  145. BUG_ON(!mrq || !mrq->cmd || !mrq->data || !mrq->stop);
  146. if (blocks > 1) {
  147. mrq->cmd->opcode = write ?
  148. MMC_WRITE_MULTIPLE_BLOCK : MMC_READ_MULTIPLE_BLOCK;
  149. } else {
  150. mrq->cmd->opcode = write ? MMC_WRITE_BLOCK :
  151. MMC_READ_SINGLE_BLOCK;
  152. }
  153. mrq->cmd->arg = arg;
  154. if (!mmc_card_blockaddr(card))
  155. mrq->cmd->arg <<= 9;
  156. mrq->cmd->flags = MMC_RSP_R1 | MMC_CMD_ADTC;
  157. if (blocks == 1) {
  158. mrq->stop = NULL;
  159. } else {
  160. mrq->stop->opcode = MMC_STOP_TRANSMISSION;
  161. mrq->stop->arg = 0;
  162. mrq->stop->flags = MMC_RSP_R1B | MMC_CMD_AC;
  163. }
  164. mrq->data->blksz = blksz;
  165. mrq->data->blocks = blocks;
  166. mrq->data->flags = write ? MMC_DATA_WRITE : MMC_DATA_READ;
  167. mrq->data->sg = sg;
  168. mrq->data->sg_len = sg_len;
  169. mmc_set_data_timeout(mrq->data, card);
  170. }
  171. /*
  172. * Checks that a normal transfer didn't have any errors
  173. */
  174. static int mmc_ffu_check_result(struct mmc_request *mrq)
  175. {
  176. BUG_ON(!mrq || !mrq->cmd || !mrq->data);
  177. if (mrq->cmd->error != 0)
  178. return -EINVAL;
  179. if (mrq->data->error != 0)
  180. return -EINVAL;
  181. if (mrq->stop != NULL && mrq->stop->error != 0)
  182. return -1;
  183. if (mrq->data->bytes_xfered != (mrq->data->blocks * mrq->data->blksz))
  184. return -EINVAL;
  185. return 0;
  186. }
  187. static int mmc_ffu_busy(struct mmc_command *cmd)
  188. {
  189. return !(cmd->resp[0] & R1_READY_FOR_DATA) ||
  190. (R1_CURRENT_STATE(cmd->resp[0]) == R1_STATE_PRG);
  191. }
  192. static int mmc_ffu_wait_busy(struct mmc_card *card)
  193. {
  194. int ret, busy = 0;
  195. struct mmc_command cmd = {0};
  196. memset(&cmd, 0, sizeof(struct mmc_command));
  197. cmd.opcode = MMC_SEND_STATUS;
  198. cmd.arg = card->rca << 16;
  199. cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
  200. do {
  201. ret = mmc_wait_for_cmd(card->host, &cmd, 0);
  202. if (ret)
  203. break;
  204. if (!busy && mmc_ffu_busy(&cmd)) {
  205. busy = 1;
  206. if (card->host->caps & MMC_CAP_WAIT_WHILE_BUSY) {
  207. pr_warn("%s: Warning: Host did not wait for busy state to end.\n",
  208. mmc_hostname(card->host));
  209. }
  210. }
  211. } while (mmc_ffu_busy(&cmd));
  212. return ret;
  213. }
  214. /*
  215. * transfer with certain parameters
  216. */
  217. static int mmc_ffu_simple_transfer(struct mmc_card *card,
  218. struct scatterlist *sg, unsigned int sg_len, u32 arg,
  219. unsigned int blocks, unsigned int blksz, int write)
  220. {
  221. struct mmc_request mrq = {0};
  222. struct mmc_command cmd = {0};
  223. struct mmc_command stop = {0};
  224. struct mmc_data data = {0};
  225. mrq.cmd = &cmd;
  226. mrq.data = &data;
  227. mrq.stop = &stop;
  228. mmc_ffu_prepare_mrq(card, &mrq, sg, sg_len, arg, blocks, blksz,
  229. write);
  230. mmc_wait_for_req(card->host, &mrq);
  231. mmc_ffu_wait_busy(card);
  232. return mmc_ffu_check_result(&mrq);
  233. }
  234. /*
  235. * Map memory into a scatterlist.
  236. */
  237. static int mmc_ffu_map_sg(struct mmc_ffu_mem *mem, unsigned long size,
  238. struct scatterlist *sglist, unsigned int max_segs,
  239. unsigned int max_seg_sz, unsigned int *sg_len,
  240. int min_sg_len)
  241. {
  242. struct scatterlist *sg = NULL;
  243. unsigned int i;
  244. unsigned long sz = size;
  245. sg_init_table(sglist, max_segs);
  246. if (min_sg_len > max_segs)
  247. min_sg_len = max_segs;
  248. *sg_len = 0;
  249. do {
  250. for (i = 0; i < mem->cnt; i++) {
  251. unsigned long len = PAGE_SIZE << mem->arr[i].order;
  252. if (min_sg_len && (size / min_sg_len < len))
  253. len = ALIGN(size / min_sg_len, CARD_BLOCK_SIZE);
  254. if (len > sz)
  255. len = sz;
  256. if (len > max_seg_sz)
  257. len = max_seg_sz;
  258. if (sg)
  259. sg = sg_next(sg);
  260. else
  261. sg = sglist;
  262. if (!sg)
  263. return -EINVAL;
  264. sg_set_page(sg, mem->arr[i].page, len, 0);
  265. sz -= len;
  266. *sg_len += 1;
  267. if (!sz)
  268. break;
  269. }
  270. } while (sz);
  271. if (sg)
  272. sg_mark_end(sg);
  273. return 0;
  274. }
  275. static void mmc_ffu_free_mem(struct mmc_ffu_mem *mem)
  276. {
  277. if (!mem)
  278. return;
  279. while (mem->cnt--)
  280. __free_pages(mem->arr[mem->cnt].page, mem->arr[mem->cnt].order);
  281. kfree(mem->arr);
  282. }
  283. /*
  284. * Cleanup struct mmc_ffu_area.
  285. */
  286. static int mmc_ffu_area_cleanup(struct mmc_ffu_area *area)
  287. {
  288. kfree(area->sg);
  289. mmc_ffu_free_mem(area->mem);
  290. return 0;
  291. }
  292. /*
  293. * Allocate a lot of memory, preferably max_sz but at least min_sz. In case
  294. * there isn't much memory do not exceed 1/16th total low mem pages. Also do
  295. * not exceed a maximum number of segments and try not to make segments much
  296. * bigger than maximum segment size.
  297. */
  298. static struct mmc_ffu_mem *mmc_ffu_alloc_mem(unsigned long min_sz,
  299. unsigned long max_sz, unsigned int max_segs, unsigned int max_seg_sz)
  300. {
  301. unsigned long max_page_cnt = DIV_ROUND_UP(max_sz, PAGE_SIZE);
  302. unsigned long min_page_cnt = DIV_ROUND_UP(min_sz, PAGE_SIZE);
  303. unsigned long max_seg_page_cnt = DIV_ROUND_UP(max_seg_sz, PAGE_SIZE);
  304. unsigned long page_cnt = 0;
  305. unsigned long limit = nr_free_buffer_pages() >> 4;
  306. struct mmc_ffu_mem *mem;
  307. if (max_page_cnt > limit)
  308. max_page_cnt = limit;
  309. if (min_page_cnt > max_page_cnt)
  310. min_page_cnt = max_page_cnt;
  311. if (max_seg_page_cnt > max_page_cnt)
  312. max_seg_page_cnt = max_page_cnt;
  313. if (max_segs > max_page_cnt)
  314. max_segs = max_page_cnt;
  315. mem = kzalloc(sizeof(struct mmc_ffu_mem), GFP_KERNEL);
  316. if (!mem)
  317. return NULL;
  318. mem->arr = kcalloc(max_segs, sizeof(struct mmc_ffu_pages), GFP_KERNEL);
  319. if (!mem->arr)
  320. goto out_free;
  321. while (max_page_cnt) {
  322. struct page *page;
  323. unsigned int order;
  324. gfp_t flags = GFP_KERNEL | GFP_DMA | __GFP_NOWARN |
  325. __GFP_NORETRY;
  326. order = get_order(max_seg_page_cnt << PAGE_SHIFT);
  327. while (1) {
  328. page = alloc_pages(flags, order);
  329. if (page || !order)
  330. break;
  331. order -= 1;
  332. }
  333. if (!page) {
  334. if (page_cnt < min_page_cnt)
  335. goto out_free;
  336. break;
  337. }
  338. mem->arr[mem->cnt].page = page;
  339. mem->arr[mem->cnt].order = order;
  340. mem->cnt += 1;
  341. if (max_page_cnt <= (1UL << order))
  342. break;
  343. max_page_cnt -= 1UL << order;
  344. page_cnt += 1UL << order;
  345. if (mem->cnt >= max_segs) {
  346. if (page_cnt < min_page_cnt)
  347. goto out_free;
  348. break;
  349. }
  350. }
  351. return mem;
  352. out_free:
  353. mmc_ffu_free_mem(mem);
  354. return NULL;
  355. }
  356. /*
  357. * Initialize an area for data transfers.
  358. * Copy the data to the allocated pages.
  359. */
  360. static int mmc_ffu_area_init(struct mmc_ffu_area *area, struct mmc_card *card,
  361. u8 *data, unsigned int size)
  362. {
  363. int ret, i, length;
  364. area->max_segs = card->host->max_segs;
  365. area->max_seg_sz = card->host->max_seg_size & ~(CARD_BLOCK_SIZE - 1);
  366. area->max_tfr = size;
  367. if (area->max_tfr >> 9 > card->host->max_blk_count)
  368. area->max_tfr = card->host->max_blk_count << 9;
  369. if (area->max_tfr > card->host->max_req_size)
  370. area->max_tfr = card->host->max_req_size;
  371. if (area->max_tfr / area->max_seg_sz > area->max_segs)
  372. area->max_tfr = area->max_segs * area->max_seg_sz;
  373. /*
  374. * Try to allocate enough memory for a max. sized transfer. Less is OK
  375. * because the same memory can be mapped into the scatterlist more than
  376. * once. Also, take into account the limits imposed on scatterlist
  377. * segments by the host driver.
  378. */
  379. area->mem = mmc_ffu_alloc_mem(1, area->max_tfr, area->max_segs,
  380. area->max_seg_sz);
  381. if (!area->mem)
  382. return -ENOMEM;
  383. /* copy data to page */
  384. length = 0;
  385. for (i = 0; i < area->mem->cnt; i++) {
  386. memcpy(page_address(area->mem->arr[i].page), data + length,
  387. min(size - length, area->max_seg_sz));
  388. length += area->max_seg_sz;
  389. }
  390. area->sg = kmalloc_array(area->max_segs, sizeof(struct scatterlist),
  391. GFP_KERNEL);
  392. if (!area->sg) {
  393. ret = -ENOMEM;
  394. goto out_free;
  395. }
  396. ret = mmc_ffu_map_sg(area->mem, size, area->sg,
  397. area->max_segs, area->max_seg_sz, &area->sg_len, 1);
  398. if (ret != 0)
  399. goto out_free;
  400. return 0;
  401. out_free:
  402. mmc_ffu_area_cleanup(area);
  403. return ret;
  404. }
  405. static int mmc_ffu_write(struct mmc_card *card, u8 *src, u32 arg,
  406. int size)
  407. {
  408. int rc;
  409. struct mmc_ffu_area mem;
  410. mem.sg = NULL;
  411. mem.mem = NULL;
  412. if (!src) {
  413. pr_err("FFU: %s: data buffer is NULL\n",
  414. mmc_hostname(card->host));
  415. return -EINVAL;
  416. }
  417. rc = mmc_ffu_area_init(&mem, card, src, size);
  418. if (rc != 0)
  419. goto exit;
  420. rc = mmc_ffu_simple_transfer(card, mem.sg, mem.sg_len, arg,
  421. size / CARD_BLOCK_SIZE, CARD_BLOCK_SIZE, 1);
  422. pr_err("FFU write result %d\n", rc);
  423. exit:
  424. mmc_ffu_area_cleanup(&mem);
  425. return rc;
  426. }
  427. static int mmc_ffu_restart(struct mmc_card *card)
  428. {
  429. struct mmc_host *host = card->host;
  430. int err = 0;
  431. card->host->ios.timing = MMC_TIMING_LEGACY;
  432. mmc_set_clock(card->host, 300000);
  433. mmc_set_bus_width(card->host, MMC_BUS_WIDTH_1);
  434. card->state |= MMC_STATE_FFUED;
  435. err = mmc_reinit_oldcard(host);
  436. pr_err("mmc_init_card ret %d\n", err);
  437. if (!err)
  438. card->state &= ~MMC_STATE_FFUED;
  439. return err;
  440. }
  441. /* Host set the EXT_CSD */
  442. static int mmc_host_set_ffu(struct mmc_card *card, u32 ffu_enable)
  443. {
  444. int err;
  445. /* check if card is eMMC 5.0 or higher */
  446. if (card->ext_csd.rev < 7)
  447. return -EINVAL;
  448. if (FFU_ENABLED(ffu_enable)) {
  449. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  450. EXT_CSD_FW_CONFIG, MMC_FFU_ENABLE,
  451. card->ext_csd.generic_cmd6_time);
  452. if (err) {
  453. pr_err("%s: switch to FFU failed with error %d\n",
  454. mmc_hostname(card->host), err);
  455. return err;
  456. }
  457. }
  458. return 0;
  459. }
  460. #define CID_MANFID_SANDISK 0x2
  461. #define CID_MANFID_TOSHIBA 0x11
  462. #define CID_MANFID_MICRON 0x13
  463. #define CID_MANFID_SAMSUNG 0x15
  464. #define CID_MANFID_SANDISK_NEW 0x45
  465. #define CID_MANFID_KSI 0x70
  466. #define CID_MANFID_HYNIX 0x90
  467. static int mmc_ffu_reduce_speed(struct mmc_card *card)
  468. {
  469. int err;
  470. u8 bus_width = EXT_CSD_BUS_WIDTH_4, hs_timing = 0;
  471. u32 clock;
  472. /* Reduce to safe and lower clock speed */
  473. if (card->host->ios.clock > FFU_BUS_FREQ)
  474. clock = FFU_BUS_FREQ;
  475. else
  476. clock = card->host->ios.clock;
  477. /* 1. Don't switch between DDR and SDR */
  478. /* 2. Some device does not allow FFU in 8 bit mode,
  479. so switch to 4bit mode
  480. */
  481. if (card->host->ios.timing == MMC_TIMING_MMC_HS400) {
  482. card->host->ios.timing = MMC_TIMING_MMC_DDR52;
  483. bus_width = EXT_CSD_DDR_BUS_WIDTH_4;
  484. hs_timing = 1;
  485. } else if (card->host->ios.timing == MMC_TIMING_MMC_HS200) {
  486. card->host->ios.timing = MMC_TIMING_MMC_HS;
  487. bus_width = EXT_CSD_BUS_WIDTH_4;
  488. hs_timing = 1;
  489. } else if (card->host->ios.timing == MMC_TIMING_MMC_DDR52) {
  490. bus_width = EXT_CSD_DDR_BUS_WIDTH_4;
  491. hs_timing = 1;
  492. } else if (card->host->ios.timing == MMC_TIMING_MMC_HS) {
  493. if (!(card->host->caps &
  494. (MMC_CAP_8_BIT_DATA | MMC_CAP_4_BIT_DATA))) {
  495. bus_width = EXT_CSD_BUS_WIDTH_1;
  496. } else {
  497. bus_width = EXT_CSD_BUS_WIDTH_4;
  498. }
  499. hs_timing = 1;
  500. } else if (card->host->ios.timing == MMC_TIMING_LEGACY) {
  501. if (!(card->host->caps &
  502. (MMC_CAP_8_BIT_DATA | MMC_CAP_4_BIT_DATA))) {
  503. bus_width = EXT_CSD_BUS_WIDTH_1;
  504. } else {
  505. bus_width = EXT_CSD_BUS_WIDTH_4;
  506. }
  507. hs_timing = 0;
  508. }
  509. if (hs_timing == 1) {
  510. pr_err("FFU switch to HS\n");
  511. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  512. EXT_CSD_HS_TIMING, hs_timing, 0);
  513. if (err) {
  514. pr_err("FFU: %s: error %d switch to high-speed\n",
  515. mmc_hostname(card->host), err);
  516. goto exit;
  517. }
  518. }
  519. mmc_set_clock(card->host, clock);
  520. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  521. EXT_CSD_BUS_WIDTH, bus_width,
  522. card->ext_csd.generic_cmd6_time);
  523. if (err) {
  524. pr_err("FFU: %s: error %d change bus width to 4 bit\n",
  525. mmc_hostname(card->host), err);
  526. goto exit;
  527. }
  528. mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4);
  529. exit:
  530. return err;
  531. }
  532. static int mmc_ffu_restore_speed(struct mmc_card *card)
  533. {
  534. int err;
  535. struct mmc_host *host = card->host;
  536. u8 card_type = card->ext_csd.raw_card_type;
  537. u8 bus_width = EXT_CSD_BUS_WIDTH_4;
  538. u8 timing = MMC_TIMING_MMC_HS, switch_timing = 1;
  539. u32 clock = FFU_BUS_FREQ;
  540. unsigned char *timing_str = NULL;
  541. if (((card_type & EXT_CSD_CARD_TYPE_HS400_1_2V) &&
  542. (host->caps2 & MMC_CAP2_HS400_1_2V))
  543. || ((card_type & EXT_CSD_CARD_TYPE_HS400_1_8V) &&
  544. (host->caps2 & MMC_CAP2_HS400_1_8V))) {
  545. bus_width = EXT_CSD_DDR_BUS_WIDTH_8;
  546. timing = MMC_TIMING_MMC_HS400;
  547. switch_timing = 3;
  548. timing_str = "HS400";
  549. /* Since MMC_HS400_MAX_DTR is defined as 400M,
  550. we shall assign clock as MMC_HS200_MAX_DTR */
  551. clock = MMC_HS200_MAX_DTR;
  552. } else if (((host->caps2 & MMC_CAP2_HS200_1_8V_SDR) &&
  553. (card_type & EXT_CSD_CARD_TYPE_HS200_1_8V))
  554. || ((host->caps2 & MMC_CAP2_HS200_1_2V_SDR) &&
  555. (card_type & EXT_CSD_CARD_TYPE_HS200_1_2V))) {
  556. if (host->caps & MMC_CAP_8_BIT_DATA)
  557. bus_width = EXT_CSD_BUS_WIDTH_8;
  558. else if (host->caps & MMC_CAP_4_BIT_DATA)
  559. bus_width = EXT_CSD_BUS_WIDTH_4;
  560. timing = MMC_TIMING_MMC_HS200;
  561. switch_timing = 2;
  562. timing_str = "HS200";
  563. clock = MMC_HS200_MAX_DTR;
  564. } else if (((host->caps & MMC_CAP_1_8V_DDR) &&
  565. (card_type & EXT_CSD_CARD_TYPE_DDR_1_8V))
  566. || ((host->caps & MMC_CAP_1_2V_DDR) &&
  567. (card_type & EXT_CSD_CARD_TYPE_DDR_1_2V))) {
  568. if (host->caps & MMC_CAP_8_BIT_DATA)
  569. bus_width = EXT_CSD_DDR_BUS_WIDTH_8;
  570. else if (host->caps & MMC_CAP_4_BIT_DATA)
  571. bus_width = EXT_CSD_DDR_BUS_WIDTH_4;
  572. timing = MMC_TIMING_UHS_DDR50;
  573. switch_timing = 1;
  574. timing_str = "DDR";
  575. clock = MMC_HIGH_DDR_MAX_DTR;
  576. } else if (host->caps & MMC_CAP_MMC_HIGHSPEED &&
  577. card_type & EXT_CSD_CARD_TYPE_HS_52) {
  578. if (host->caps & MMC_CAP_8_BIT_DATA)
  579. bus_width = EXT_CSD_BUS_WIDTH_8;
  580. else if (host->caps & MMC_CAP_4_BIT_DATA)
  581. bus_width = EXT_CSD_BUS_WIDTH_4;
  582. timing = MMC_TIMING_MMC_HS;
  583. switch_timing = 1;
  584. timing_str = "HS";
  585. clock = MMC_HIGH_52_MAX_DTR;
  586. } else if (card_type & EXT_CSD_CARD_TYPE_HS_26) {
  587. switch_timing = 0;
  588. clock = MMC_HIGH_26_MAX_DTR;
  589. }
  590. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  591. EXT_CSD_BUS_WIDTH, bus_width,
  592. card->ext_csd.generic_cmd6_time);
  593. if (err) {
  594. pr_err("FFU: %s: error %d switching bus_width to %d\n",
  595. mmc_hostname(card->host), err, bus_width);
  596. goto exit;
  597. }
  598. if (host->caps & MMC_CAP_8_BIT_DATA)
  599. mmc_set_bus_width(card->host, MMC_BUS_WIDTH_8);
  600. else if (host->caps & MMC_CAP_4_BIT_DATA)
  601. mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4);
  602. else
  603. mmc_set_bus_width(card->host, MMC_BUS_WIDTH_1);
  604. if (switch_timing) {
  605. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  606. EXT_CSD_HS_TIMING, switch_timing, 0);
  607. if (err) {
  608. pr_err("FFU: %s: error %d switch to %s\n",
  609. mmc_hostname(card->host), err, timing_str);
  610. goto exit;
  611. }
  612. mmc_set_timing(card->host, timing);
  613. }
  614. mmc_set_clock(card->host, clock);
  615. if (timing == MMC_TIMING_MMC_HS200 || timing == MMC_TIMING_MMC_HS400) {
  616. err = card->host->ops->execute_tuning(card->host,
  617. MMC_SEND_TUNING_BLOCK_HS200);
  618. }
  619. exit:
  620. return err;
  621. }
  622. int mmc_ffu_install(struct mmc_card *card, u8 *ext_csd)
  623. {
  624. int err;
  625. u32 ffu_data_len;
  626. u32 timeout;
  627. u8 set = 1;
  628. if (!FFU_FEATURES(ext_csd[EXT_CSD_FFU_FEATURES])) {
  629. /* host switch back to work in normal MMC Read/Write commands */
  630. if ((card->cid.manfid == CID_MANFID_HYNIX) &&
  631. (card->cid.prv == 0x03)) {
  632. set = 0;
  633. }
  634. pr_err("FFU exit FFU mode\n");
  635. err = mmc_switch(card, set,
  636. EXT_CSD_MODE_CONFIG, MMC_FFU_MODE_NORMAL,
  637. card->ext_csd.generic_cmd6_time);
  638. if (err) {
  639. pr_err("FFU: %s: error %d exit FFU mode\n",
  640. mmc_hostname(card->host), err);
  641. goto exit;
  642. }
  643. pr_err("FFU restart eMMC\n");
  644. err = mmc_ffu_restart(card);
  645. if (err) {
  646. pr_err("FFU: %s: error %d restart\n",
  647. mmc_hostname(card->host), err);
  648. goto exit;
  649. }
  650. }
  651. /* check mode operation */
  652. if (FFU_FEATURES(ext_csd[EXT_CSD_FFU_FEATURES])) {
  653. ffu_data_len = ext_csd[EXT_CSD_NUM_OF_FW_SEC_PROG] |
  654. ext_csd[EXT_CSD_NUM_OF_FW_SEC_PROG + 1] << 8 |
  655. ext_csd[EXT_CSD_NUM_OF_FW_SEC_PROG + 2] << 16 |
  656. ext_csd[EXT_CSD_NUM_OF_FW_SEC_PROG + 3] << 24;
  657. if (!ffu_data_len) {
  658. err = -EPERM;
  659. return err;
  660. }
  661. timeout = ext_csd[EXT_CSD_OPERATION_CODE_TIMEOUT];
  662. if (timeout == 0 || timeout > 0x17) {
  663. timeout = 0x17;
  664. pr_warn("FFU: %s: operation code timeout is out of range. Using maximum timeout.\n",
  665. mmc_hostname(card->host));
  666. }
  667. /* timeout is at millisecond resolution */
  668. timeout = (100 * (1 << timeout) / 1000) + 1;
  669. /* set ext_csd to install mode */
  670. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  671. EXT_CSD_MODE_OPERATION_CODES,
  672. MMC_FFU_INSTALL_SET, timeout);
  673. if (err) {
  674. pr_err("FFU: %s: error %d setting install mode\n",
  675. mmc_hostname(card->host), err);
  676. goto exit;
  677. }
  678. /* Switching back to original bus width and timing */
  679. mmc_ffu_restore_speed(card);
  680. }
  681. /* read ext_csd */
  682. err = mmc_send_ext_csd(card, ext_csd);
  683. if (err) {
  684. pr_err("FFU: %s: error %d sending ext_csd\n",
  685. mmc_hostname(card->host), err);
  686. goto exit;
  687. }
  688. /* return status */
  689. err = ext_csd[EXT_CSD_FFU_STATUS];
  690. if (!err) {
  691. pr_err("FFU: %s: succeed FFU\n",
  692. mmc_hostname(card->host));
  693. } else if (err) {
  694. pr_err("FFU: %s: error %d FFU install:\n",
  695. mmc_hostname(card->host), err);
  696. err = -EINVAL;
  697. goto exit;
  698. }
  699. exit:
  700. return err;
  701. }
  702. int mmc_ffu_download(struct mmc_card *card, struct mmc_command *cmd,
  703. u8 *data, int buf_bytes)
  704. {
  705. u8 ext_csd[CARD_BLOCK_SIZE];
  706. int err;
  707. /* Read the EXT_CSD */
  708. err = mmc_send_ext_csd(card, ext_csd);
  709. if (err) {
  710. pr_err("FFU: %s: error %d sending ext_csd\n",
  711. mmc_hostname(card->host), err);
  712. goto exit;
  713. }
  714. /* Check if FFU is supported by card */
  715. if (!FFU_SUPPORTED_MODE(ext_csd[EXT_CSD_SUPPORTED_MODE])) {
  716. err = -EINVAL;
  717. pr_err("FFU: %s: error %d FFU is not supported\n",
  718. mmc_hostname(card->host), err);
  719. goto exit;
  720. }
  721. pr_err("eMMC cache originally %s -> %s\n",
  722. ((card->ext_csd.cache_ctrl) ? "on" : "off"),
  723. ((card->ext_csd.cache_ctrl) ? "turn off" : "keep"));
  724. if (card->ext_csd.cache_ctrl) {
  725. mmc_flush_cache(card);
  726. mmc_ffu_cache_ctrl(card->host, 0);
  727. }
  728. mmc_ffu_reduce_speed(card);
  729. err = mmc_host_set_ffu(card, ext_csd[EXT_CSD_FW_CONFIG]);
  730. if (err) {
  731. pr_err("FFU: %s: error %d FFU is not supported\n",
  732. mmc_hostname(card->host), err);
  733. err = -EINVAL;
  734. goto exit;
  735. }
  736. /* set device to FFU mode */
  737. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_MODE_CONFIG,
  738. MMC_FFU_MODE_SET, card->ext_csd.generic_cmd6_time);
  739. if (err) {
  740. pr_err("FFU: %s: error %d FFU is not supported\n",
  741. mmc_hostname(card->host), err);
  742. err = -EINVAL;
  743. goto exit;
  744. }
  745. /* set CMD ARG */
  746. cmd->arg = ext_csd[EXT_CSD_FFU_ARG] |
  747. ext_csd[EXT_CSD_FFU_ARG + 1] << 8 |
  748. ext_csd[EXT_CSD_FFU_ARG + 2] << 16 |
  749. ext_csd[EXT_CSD_FFU_ARG + 3] << 24;
  750. /* If arg is zero, should be set to a special value for samsung eMMC
  751. */
  752. if (card->cid.manfid == CID_MANFID_SAMSUNG && cmd->arg == 0x0)
  753. cmd->arg = 0xc7810000;
  754. pr_err("FFU perform write\n");
  755. err = mmc_ffu_write(card, data, cmd->arg, buf_bytes);
  756. if (err && (FFU_FEATURES(ext_csd[EXT_CSD_FFU_FEATURES]))) {
  757. /* FIX ME, to set FFU_ABORT to MODE_OPERATION_CODES */
  758. ;
  759. } else {
  760. err = mmc_ffu_install(card, ext_csd);
  761. }
  762. exit:
  763. return err;
  764. }
  765. EXPORT_SYMBOL(mmc_ffu_download);