ark1668e_devb_emmc.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  1. #include <common.h>
  2. #include <dwmmc.h>
  3. #include <malloc.h>
  4. #include <debug_uart.h>
  5. #include <asm-generic/gpio.h>
  6. #include <asm/arch/ark-common.h>
  7. #include <linux/usb/musb.h>
  8. DECLARE_GLOBAL_DATA_PTR;
  9. #define ARK1668_UPDATE_MAGIC "ada7f0c6-7c86-11e9-8f9e-2a86e4085a59"
  10. #define rSYS_SD_CLK_CFG *((volatile unsigned int *)(0xe4900058))
  11. #define rSYS_SD1_CLK_CFG *((volatile unsigned int *)(0xe490005c))
  12. #define rSYS_SOFT_RSTNA *((volatile unsigned int *)(0xe4900074))
  13. #define rSYS_SOFT_RSTNB *((volatile unsigned int *)(0xe4900078))
  14. #define rSYS_DDR_STATUS *((volatile unsigned int *)(0xe4900180))
  15. #define rSYS_DDR_IO_CFG *((volatile unsigned int *)(0xe490019C))
  16. #define rSYS_PAD_CTRL00 *((volatile unsigned int *)(0xe49001c0))
  17. #define rSYS_PAD_CTRL01 *((volatile unsigned int *)(0xe49001c4))
  18. #define rSYS_PAD_CTRL02 *((volatile unsigned int *)(0xe49001c8))
  19. #define rSYS_PAD_CTRL04 *((volatile unsigned int *)(0xe49001d0))
  20. #define rSYS_PAD_CTRL05 *((volatile unsigned int *)(0xe49001d4))
  21. #define rSYS_PAD_CTRL06 *((volatile unsigned int *)(0xe49001d8))
  22. #define rSYS_PAD_CTRL07 *((volatile unsigned int *)(0xe49001dc))
  23. #define rSYS_PAD_CTRL08 *((volatile unsigned int *)(0xe49001e0))
  24. #define rSYS_PAD_CTRL09 *((volatile unsigned int *)(0xe49001e4))
  25. #define rSYS_PAD_CTRL0A *((volatile unsigned int *)(0xe49001e8))
  26. #define rSYS_PAD_CTRL0B *((volatile unsigned int *)(0xe49001ec))
  27. #define rSYS_PAD_CTRL0C *((volatile unsigned int *)(0xe49001f0))
  28. #define rSYS_PAD_CTRL0D *((volatile unsigned int *)(0xe49001f4))
  29. #define rSYS_PAD_CTRL0E *((volatile unsigned int *)(0xe49001f8))
  30. #define rSYS_PAD_CTRL38 *((volatile unsigned int *)(0xe49001fc))
  31. #define rSYS_PAD_CTRL3E *((volatile unsigned int *)(0xe4900200))
  32. #define rSYS_PAD_CTRL0F *((volatile unsigned int *)(0xe4900204))
  33. #define rSYS_CPU_CTL *((volatile unsigned int *)(0xe4900208))
  34. #define rSYS_MFC_GMAC_CTL *((volatile unsigned int *)(0xe490020c))
  35. #define rSYS_DEVICE_CLK_CFG7 *((volatile unsigned int *)(0xe4900230))
  36. #define rSYS_DEVICE_CLK_CFG8 *((volatile unsigned int *)(0xe4900234))
  37. #define rSYS_PAD_CTL3C *((volatile unsigned int *)(0xe49000F0))
  38. #define rWDT_CR *((volatile unsigned int *)(0xe4b00000))
  39. #define MUSB_BASE 0xe0100000
  40. #define CHECKDATA_ERROR 2
  41. extern const struct musb_platform_ops ark_musb_ops;
  42. static struct musb_hdrc_config musb_config = {
  43. .multipoint = 1,
  44. .dyn_fifo = 0,
  45. .num_eps = 6,
  46. .ram_bits = 12
  47. };
  48. static struct musb_hdrc_platform_data musb_platform_data = {
  49. .mode = MUSB_HOST,
  50. .config = &musb_config,
  51. .power = 100,
  52. .platform_ops = &ark_musb_ops,
  53. };
  54. static void dwmci_select_pad(void)
  55. {
  56. unsigned int val;
  57. /* use sd/mmc 0 */
  58. val = rSYS_PAD_CTRL00;
  59. val &= ~((0x7<<18)|(0x7<<15)|(0x7<<12)|(0x7<<9)|(0x7<<6)|(0x7<<3)|(0x7<<0));
  60. val |= ((0x1<<18)|(0x1<<15)|(0x1<<12)|(0x1<<9)|(0x1<<6)|(0x1<<3)|(0x1<<0));
  61. rSYS_PAD_CTRL00 = val;
  62. val = rSYS_PAD_CTRL0E;
  63. val &= ~((0x7<<27)|(0x7<<24));
  64. val |= ((0x1<<27)|(0x1<<24));
  65. rSYS_PAD_CTRL0E = val;
  66. val = rSYS_PAD_CTRL38;
  67. val &= ~((0x7<<9)|(0x7<<6)|(0x7<<3)|(0x7<<0));
  68. val |= ((0x1<<9)|(0x1<<6)|(0x1<<3)|(0x1<<0));
  69. rSYS_PAD_CTRL38 = val;
  70. /* use sd/mmc 1 */
  71. val = rSYS_PAD_CTRL00;
  72. val &= ~((0x7<<27)|(0x7<<24)|(0x7<<21));
  73. val |= ((0x1<<27)|(0x1<<24)|(0x1<<21));
  74. rSYS_PAD_CTRL00 = val;
  75. val = rSYS_PAD_CTRL01;
  76. val &= ~((0x7<<9)|(0x7<<6)|(0x7<<3)|(0x7<<0));
  77. val |= ((0x1<<9)|(0x1<<6)|(0x1<<3)|(0x1<<0));
  78. rSYS_PAD_CTRL01 = val;
  79. /* use sdio wifi/mmc 2*/
  80. val = rSYS_PAD_CTRL01;
  81. val &= ~((0x7<<27)|(0x7<<24)|(0x7<<21)|(0x7<<18)|(0x7<<15)|(0x7<<12));
  82. val |= ((0x1<<27)|(0x1<<24)|(0x1<<21)|(0x1<<18)|(0x1<<15)|(0x1<<12));
  83. rSYS_PAD_CTRL01 = val;
  84. val = rSYS_PAD_CTRL02;
  85. val &= ~(0x7<<0);
  86. val |= (0x1<<0);
  87. rSYS_PAD_CTRL02 = val;
  88. }
  89. void dwmci_reset(void)
  90. {
  91. rSYS_SOFT_RSTNA &= ~((1<<29)|(1<<16));
  92. rSYS_SOFT_RSTNB &= ~(1<<3);
  93. udelay(100);
  94. rSYS_SOFT_RSTNA |= ((1<<29)|(1<<16));
  95. rSYS_SOFT_RSTNB |= (1<<3);
  96. }
  97. static void usb_controller_reset(void)
  98. {
  99. rSYS_PAD_CTRL0F &= ~0xfff;
  100. rSYS_SOFT_RSTNA &= ~(3 << 5);
  101. udelay(100);
  102. rSYS_SOFT_RSTNA |= 3 << 5;
  103. udelay(10);
  104. }
  105. #define ARK_MMC_CLK 24000000
  106. struct dwmci_host dwmcihost[2];
  107. static int ark_dwmci_init(char *name,u32 regbase, int bus_width, int index)
  108. {
  109. struct dwmci_host *host = NULL;
  110. host = &dwmcihost[index];
  111. memset(host, 0, sizeof(struct dwmci_host));
  112. dwmci_select_pad();
  113. //dwmci_reset();
  114. /* config clk in sample delay */
  115. //rSYS_SD_CLK_CFG &= ~(0x7f << 13);
  116. //rSYS_SD_CLK_CFG |= 52 << 13;
  117. //rSYS_SD1_CLK_CFG &= ~(0x7f << 13);
  118. //rSYS_SD1_CLK_CFG |= 52 << 13;
  119. host->name = name;
  120. host->ioaddr = (void *)regbase;
  121. host->buswidth = bus_width;
  122. host->dev_index = index;
  123. host->bus_hz = ARK_MMC_CLK;
  124. host->fifoth_val = 64;
  125. host->fifo_mode = 1;
  126. add_dwmci(host, host->bus_hz, 400000);
  127. return 0;
  128. }
  129. int board_mmc_init(bd_t *bis)
  130. {
  131. // ark_dwmci_init("ARK_MMC0", 0xec400000, 8, 0);
  132. //#ifndef CONFIG_SPL_BUILD
  133. ark_dwmci_init("ARK_MMC1", 0xec800000, 4, 0);
  134. //#endif
  135. // ark_dwmci_init("ARK_MMC2", 0xecc00000, 4, 2);
  136. ark_dwmci_init("ARK_MMC0", 0xec400000, 8, 1);
  137. return 0;
  138. }
  139. int dram_init(void)
  140. {
  141. gd->ram_size = get_ram_size((void *)CONFIG_SYS_SDRAM_BASE,
  142. CONFIG_SYS_SDRAM_SIZE);
  143. return 0;
  144. }
  145. int board_init(void)
  146. {
  147. unsigned int val;
  148. /* watchdog disable */
  149. rWDT_CR = 0;
  150. /* cpu1 disable */
  151. rSYS_CPU_CTL &= ~(1 << 7);
  152. /* cpu cnt clk enable */
  153. rSYS_CPU_CTL |= 1 << 23;
  154. /* nand pad enable */
  155. val = rSYS_PAD_CTRL08;
  156. val &= ~((0x7<<27) | (0x7<<24) | (0x7<<21) | (0x7<<18) | (0x7<<15) | (0x7<<12) | (0x7<<9) | (0x7<<6));
  157. val |= (0x1<<27) | (0x1<<24) | (0x1<<21) | (0x1<<18) | (0x1<<15) | (0x1<<12) | (0x1<<9) | (0x1<<6);
  158. rSYS_PAD_CTRL08 = val;
  159. val = rSYS_PAD_CTRL09;
  160. val &= ~((0x7<<15) | (0x7<<12) | (0x7<<9) | (0x7<<6) | (0x7<<3) | (0x7<<0));
  161. val |= (1<<15) | (1<<12) | (1<<9) | (1<<6) | (1<<3) | (1<<0);//enable nand cle, ale,ren,wen
  162. rSYS_PAD_CTRL09 = val;
  163. /* spi pad enable */
  164. val = rSYS_PAD_CTRL09;
  165. val &= ~((0x7<<27) | (0x7<<24));
  166. val |= (0x2<<27) | (0x2<<24);
  167. rSYS_PAD_CTRL09 = val;
  168. val = rSYS_PAD_CTRL0A;
  169. val &= ~((0x7<<3) | (0x7<<0));
  170. val |= (0x2<<0);
  171. rSYS_PAD_CTRL0A = val;
  172. /* gmac pad enable */
  173. rSYS_PAD_CTRL0C = (1 << 27) | (1 << 24) | (1 << 21) | (1 << 18) | (1 << 15) | (1 << 12) | (1 << 9) |
  174. (1 << 6) | (1 << 3) | (1 << 0);
  175. //rSYS_PAD_CTRL0D = (1 << 24) | (1 << 9) | (1 << 6) | (1 << 3) | (1 << 0);
  176. rSYS_PAD_CTRL0D = (1 << 24) | (1 << 21) | (1 << 18) | (1 << 15) | (1 << 12) | (1 << 9) |
  177. (1 << 6) | (1 << 3) | (1 << 0);
  178. /* gmac tx clk out */
  179. rSYS_PAD_CTRL0F |= (1 << 31);
  180. /* select rgmii interface */
  181. rSYS_MFC_GMAC_CTL &= ~(7 << 1);
  182. rSYS_MFC_GMAC_CTL |= (1 << 1);
  183. /* mac rx clk inv */
  184. rSYS_DEVICE_CLK_CFG7 |= (1 << 1);
  185. /* mac tx clk inv */
  186. rSYS_DEVICE_CLK_CFG8 |= (1 << 7);
  187. /* i2s0 sadata in */
  188. rSYS_PAD_CTRL0F &= ~(1 << 28);
  189. /* i2s1 sadata out */
  190. rSYS_PAD_CTRL0F |= (1 << 29);
  191. /* select pwm0 pad */
  192. rSYS_PAD_CTRL05 &= ~0x7;
  193. rSYS_PAD_CTRL05 |= 1;
  194. return 0;
  195. }
  196. #ifdef CONFIG_BOARD_EARLY_INIT_F
  197. int board_early_init_f(void)
  198. {
  199. #ifdef CONFIG_DEBUG_UART
  200. debug_uart_init();
  201. #endif
  202. return 0;
  203. }
  204. #endif
  205. static int get_data_from_media(char *file_name)
  206. {
  207. int ret = -1;
  208. char cmd[128] = { 0 };
  209. printf("file_name=%s\n", file_name);
  210. sprintf(cmd, "fatload %s %s %s %s",env_get("update_dev_type"),env_get("update_dev_part"),env_get("loadaddr"), file_name);
  211. printf("cmd=%s\n", cmd);
  212. ret = run_command(cmd, 0);
  213. return ret;
  214. }
  215. static int burn_data_2_emmc_partition(char *partition_name)
  216. {
  217. char cmd[128] = { 0 };
  218. int ret = -1;
  219. int file_size = 0;
  220. unsigned int i;
  221. unsigned int *srcdata = (unsigned int *)(env_get_hex("loadaddr", 0));
  222. unsigned int *dstdata = (unsigned int *)(env_get_hex("cmploadaddr", 0));
  223. sprintf(cmd, "emmc erase.part %s", partition_name);
  224. printf("cmd=%s\n", cmd);
  225. ret = run_command(cmd, 0);
  226. if (ret)
  227. return ret;
  228. file_size = env_get_ulong("filesize", 16, 0x2000);
  229. sprintf(cmd, "emmc write %s %s 0x%x", env_get("loadaddr"), partition_name, file_size);
  230. printf("cmd=%s\n", cmd);
  231. ret = run_command(cmd, 0);
  232. sprintf(cmd, "emmc read %s %s 0x%x",env_get("cmploadaddr"),partition_name, file_size);
  233. printf("cmd=%s\n", cmd);
  234. ret = run_command(cmd, 0);
  235. for(i = 0;i< file_size/4;i++)
  236. if(srcdata[i] != dstdata[i])
  237. {
  238. printf("check %s data error!!!\n",partition_name);
  239. return CHECKDATA_ERROR;
  240. }
  241. return ret;
  242. }
  243. static int ark_update_emmc_partition(char *partition_name, char *file_name)
  244. {
  245. int ret;
  246. ret = get_data_from_media(file_name);
  247. if (!ret) {
  248. return burn_data_2_emmc_partition(partition_name);
  249. }
  250. return ret;
  251. }
  252. static int ark_update_emmc_rootfs_from_media(char *partition_name)
  253. {
  254. char cmd[32];
  255. int ret;
  256. sprintf(cmd, "updaterootfs %s", partition_name);
  257. printf("cmd=%s\n", cmd);
  258. ret = run_command(cmd, 0);
  259. return ret;
  260. }
  261. static int do_update_from_media(void)
  262. {
  263. char cmd[32];
  264. unsigned int ret = 0;
  265. char *update_dev = NULL;
  266. char *curr_partition = NULL;
  267. char *update_fdt = NULL;
  268. unsigned char flag_partiton = 0 ;//0--A partiton,1---B partition
  269. sprintf(cmd, "mmc dev %s",env_get("emmc_dev_part"));
  270. printf("cmd=%s\n", cmd);
  271. run_command(cmd, 0);
  272. sprintf((char *)cmd,"disconfig 0");
  273. run_command(cmd, 0);
  274. update_fdt = env_get("emmcfdt");
  275. printf("update_fdt:%s\n", update_fdt);
  276. update_dev = env_get("update_dev_type");
  277. if (!strcmp(update_dev, "mmc"))
  278. {
  279. env_set("updata_status", "none");
  280. env_set("updata_from_part", "A");
  281. curr_partition = env_get("updata_from_part");
  282. if(!strcmp(curr_partition, "A"))
  283. flag_partiton = 0;
  284. else if(!strcmp(curr_partition, "B"))
  285. flag_partiton = 1;
  286. }else if(!strcmp(update_dev, "usb"))
  287. {
  288. env_set("updata_status", "usb");
  289. curr_partition = env_get("updata_from_part");
  290. if(!strcmp(curr_partition, "A"))
  291. flag_partiton = 1;
  292. else if(!strcmp(curr_partition, "B"))
  293. flag_partiton = 0;
  294. }
  295. printf("\r\n **** update from update ubootspl .....\r\n");
  296. ret = ark_update_emmc_partition("bootstrap", "ubootspl.bin");
  297. mdelay(30);
  298. if(!ret)
  299. {
  300. sprintf(cmd, "setenv bootstrapsize %s",env_get("filesize"));
  301. printf("cmd=%s\n", cmd);
  302. run_command(cmd, 0);
  303. mdelay(30);
  304. sprintf((char *)cmd,"disconfig 5");
  305. run_command(cmd, 0);
  306. }
  307. else if(ret == CHECKDATA_ERROR)
  308. goto bootoldsys;
  309. printf("\r\n **** update from update uboot .....\r\n");
  310. ret = ark_update_emmc_partition("bootloader", "u-boot.img");
  311. mdelay(30);
  312. if(!ret)
  313. {
  314. sprintf(cmd, "setenv bootloadersize %s",env_get("filesize"));
  315. printf("cmd=%s\n", cmd);
  316. run_command(cmd, 0);
  317. mdelay(30);
  318. sprintf((char *)cmd,"disconfig 10");
  319. run_command(cmd, 0);
  320. }
  321. else if(ret == CHECKDATA_ERROR)
  322. goto bootoldsys;
  323. printf("\r\n **** update from update fdt .....\r\n");
  324. if (flag_partiton == 0)
  325. ret = ark_update_emmc_partition("fdt", update_fdt);
  326. else
  327. ret = ark_update_emmc_partition("fdt_b", update_fdt);
  328. mdelay(30);
  329. if(!ret)
  330. {
  331. sprintf(cmd, "setenv fdtsize %s",env_get("filesize"));
  332. run_command(cmd, 0);
  333. printf("cmd=%s\n", cmd);
  334. mdelay(30);
  335. sprintf((char *)cmd,"disconfig 15");
  336. run_command(cmd, 0);
  337. }
  338. else if(ret == CHECKDATA_ERROR)
  339. goto bootoldsys;
  340. printf("\r\n **** update from update kernel .....\r\n");
  341. if (flag_partiton == 0)
  342. ret = ark_update_emmc_partition("kernel", "zImage");
  343. else
  344. ret = ark_update_emmc_partition("kernel_b", "zImage");
  345. mdelay(30);
  346. if(!ret)
  347. {
  348. sprintf(cmd, "setenv kernelsize %s",env_get("filesize"));
  349. printf("cmd=%s\n", cmd);
  350. run_command(cmd, 0);
  351. mdelay(30);
  352. sprintf((char *)cmd,"disconfig 25");
  353. run_command(cmd, 0);
  354. }
  355. else if(ret == CHECKDATA_ERROR)
  356. goto bootoldsys;
  357. printf("\r\n **** update from update bootanimation .....\r\n");
  358. ret = ark_update_emmc_partition("bootanimation", "bootanimation");
  359. mdelay(30);
  360. if(!ret)
  361. {
  362. sprintf(cmd, "setenv bootanimationsize %s",env_get("filesize"));
  363. run_command(cmd, 0);
  364. printf("cmd=%s\n", cmd);
  365. mdelay(30);
  366. sprintf((char *)cmd,"disconfig 30");
  367. run_command(cmd, 0);
  368. }
  369. else if(ret == CHECKDATA_ERROR)
  370. goto bootoldsys;
  371. printf("\r\n **** update from update reversingtrack .....\r\n");
  372. ret = ark_update_emmc_partition("reversingtrack", "reversingtrack");
  373. mdelay(30);
  374. if(!ret)
  375. {
  376. sprintf(cmd, "setenv reversingtracksize %s",env_get("filesize"));
  377. run_command(cmd, 0);
  378. printf("cmd=%s\n", cmd);
  379. mdelay(30);
  380. sprintf((char *)cmd,"disconfig 35");
  381. run_command(cmd, 0);
  382. }
  383. else if(ret == CHECKDATA_ERROR)
  384. goto bootoldsys;
  385. printf("\r\n **** update from update uboot back .....\r\n");
  386. ret = ark_update_emmc_partition("bootloader_bak", "u-boot.img");
  387. mdelay(30);
  388. if(!ret)
  389. {
  390. sprintf(cmd, "setenv bootloadersize %s",env_get("filesize"));
  391. run_command(cmd, 0);
  392. printf("cmd=%s\n", cmd);
  393. mdelay(30);
  394. sprintf((char *)cmd,"disconfig 40");
  395. run_command(cmd, 0);
  396. }
  397. else if(ret == CHECKDATA_ERROR)
  398. goto bootoldsys;
  399. printf("\r\n **** update from update rootfs .....\r\n");
  400. if (flag_partiton == 0)
  401. ret = ark_update_emmc_rootfs_from_media("rootfs");
  402. else
  403. ret = ark_update_emmc_rootfs_from_media("rootfs_b");
  404. if(!ret)
  405. {
  406. sprintf((char *)cmd,"disconfig 95");
  407. run_command(cmd, 0);
  408. }
  409. else if(ret == CHECKDATA_ERROR)
  410. goto bootoldsys;
  411. printf("\r\n **** set the env to partition .....\r\n");
  412. //mmc update set part A
  413. if(!strcmp(update_dev, "mmc"))
  414. {
  415. env_set("updata_from_part", "A");
  416. env_set("kernel_part", "kernel");
  417. env_set("fdt_part", "fdt");
  418. env_set("emmcroot", "/dev/mmcblk0p10 rw");
  419. }
  420. else if(!strcmp(update_dev, "usb"))
  421. {
  422. curr_partition = env_get("updata_from_part");
  423. if(!strcmp(curr_partition, "A"))
  424. {
  425. env_set("updata_from_part", "B");
  426. env_set("kernel_part", "kernel_b");
  427. env_set("fdt_part", "fdt_b");
  428. env_set("emmcroot", "/dev/mmcblk0p14 rw");
  429. }
  430. else if(!strcmp(curr_partition, "B"))
  431. {
  432. env_set("updata_from_part", "A");
  433. env_set("kernel_part", "kernel");
  434. env_set("fdt_part", "fdt");
  435. env_set("emmcroot", "/dev/mmcblk0p10 rw");
  436. }
  437. }
  438. mdelay(5);
  439. env_set("need_update", "no");
  440. mdelay(5);
  441. env_set("updata_status", "none");
  442. sprintf(cmd, "saveenv");
  443. printf("cmd=%s\n", cmd);
  444. run_command(cmd, 0);
  445. mdelay(30);
  446. sprintf((char *)cmd,"disconfig 100");
  447. run_command(cmd, 0);
  448. printf("\r\n **** update the device over! .....\r\n");
  449. return 0;
  450. bootoldsys:
  451. env_set("updata_status", "error");
  452. sprintf(cmd, "saveenv");
  453. printf("cmd=%s\n", cmd);
  454. run_command(cmd, 0);
  455. mdelay(20);
  456. sprintf((char *)cmd,"disconfig 100");
  457. run_command(cmd, 0);
  458. return 1;
  459. }
  460. int board_late_init(void)
  461. {
  462. char cmd[128];
  463. char *update_from_ota = NULL,*need_update = NULL;
  464. unsigned int loadaddr;
  465. int do_update = 0, update_from_mmc = 1;
  466. #ifdef CONFIG_USB_MUSB_HOST
  467. usb_controller_reset();
  468. musb_register(&musb_platform_data, NULL, (void *)MUSB_BASE);
  469. #endif
  470. update_from_ota = env_get("update_from_ota");
  471. printf("update_from_ota %s\n",update_from_ota);
  472. need_update = env_get("need_update");
  473. if (!strcmp(need_update, "yes")) {
  474. loadaddr = env_get_hex("loadaddr", 0);
  475. sprintf(cmd, "fatload %s %s %s update-magic", "mmc", env_get("sd_dev_part"), env_get("loadaddr"));
  476. printf("cmd %s\n",cmd);
  477. run_command(cmd, 0);
  478. if (loadaddr && !memcmp((void *)loadaddr, ARK1668_UPDATE_MAGIC, strlen(ARK1668_UPDATE_MAGIC))) {
  479. do_update = 1;
  480. goto update_done;
  481. } else {
  482. printf("Wrong update magic, do not update from mmc.\n");
  483. }
  484. #ifdef CONFIG_USB_MUSB_HOST
  485. run_command("usb start", 0);
  486. sprintf(cmd, "fatload %s %s %s update-magic", "usb", "0", env_get("loadaddr"));
  487. run_command(cmd, 0);
  488. if (loadaddr && !memcmp((void *)loadaddr, ARK1668_UPDATE_MAGIC, strlen(ARK1668_UPDATE_MAGIC))) {
  489. do_update = 1;
  490. update_from_mmc = 0;
  491. } else {
  492. printf("Wrong update magic, do not update from usb.\n");
  493. }
  494. #endif
  495. }
  496. else if(!strcmp(update_from_ota, "yes")){
  497. do_update = 0;
  498. sprintf(cmd, "update_from_emmc_ota");
  499. printf("cmd=%s\n", cmd);
  500. run_command(cmd, 0);
  501. }
  502. update_done:
  503. if (do_update) {
  504. run_command("emmc erase.part userdata", 0);
  505. env_set("need_update", "no");
  506. env_set("do_update", "yes");
  507. if (update_from_mmc) {
  508. printf("update form mmc...\n");
  509. env_set("update_dev_type", "mmc");
  510. env_set("update_dev_part", env_get("sd_dev_part"));
  511. } else {
  512. printf("update form usb...\n");
  513. env_set("update_dev_type", "usb");
  514. env_set("update_dev_part", "0");
  515. env_set("updata_status", "usb");
  516. }
  517. do_update_from_media();
  518. } else {
  519. env_set("do_update", "no");
  520. }
  521. return 0;
  522. }