ark1668e_devb_emmc.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  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 45000000
  106. #define ARK_MMC_NUM 3
  107. struct dwmci_host dwmcihost[ARK_MMC_NUM];
  108. static int ark_dwmci_init(char *name,u32 regbase, int bus_width, int index)
  109. {
  110. struct dwmci_host *host = NULL;
  111. host = &dwmcihost[index];
  112. memset(host, 0, sizeof(struct dwmci_host));
  113. dwmci_select_pad();
  114. //dwmci_reset();
  115. /* config clk in sample delay */
  116. //rSYS_SD_CLK_CFG &= ~(0x7f << 13);
  117. //rSYS_SD_CLK_CFG |= 52 << 13;
  118. //rSYS_SD1_CLK_CFG &= ~(0x7f << 13);
  119. //rSYS_SD1_CLK_CFG |= 52 << 13;
  120. /* mmc clk axipll(720M) / ((7 + 1) * 2) */
  121. rSYS_SD_CLK_CFG &= ~0xfff;
  122. rSYS_SD_CLK_CFG |= (1 << 8) | (1 << 7) | (1 << 5) | 7;
  123. rSYS_SD1_CLK_CFG &= ~0xfff;
  124. rSYS_SD1_CLK_CFG |= (1 << 8) | (1 << 7) | (1 << 5) | 7;
  125. host->name = name;
  126. host->ioaddr = (void *)regbase;
  127. host->buswidth = bus_width;
  128. host->dev_index = index;
  129. host->bus_hz = ARK_MMC_CLK;
  130. host->fifoth_val = 64;
  131. host->fifo_mode = 1;
  132. add_dwmci(host, host->bus_hz, 400000);
  133. return 0;
  134. }
  135. int board_mmc_init(bd_t *bis)
  136. {
  137. // ark_dwmci_init("ARK_MMC0", 0xec400000, 8, 0);
  138. //#ifndef CONFIG_SPL_BUILD
  139. ark_dwmci_init("ARK_MMC1", 0xec800000, 4, 0);
  140. //#endif
  141. // ark_dwmci_init("ARK_MMC2", 0xecc00000, 4, 2);
  142. ark_dwmci_init("ARK_MMC0", 0xec400000, 8, 1);
  143. return 0;
  144. }
  145. int dram_init(void)
  146. {
  147. gd->ram_size = get_ram_size((void *)CONFIG_SYS_SDRAM_BASE,
  148. CONFIG_SYS_SDRAM_SIZE);
  149. return 0;
  150. }
  151. int board_init(void)
  152. {
  153. unsigned int val;
  154. /* watchdog disable */
  155. rWDT_CR = 0;
  156. /* cpu1 disable */
  157. rSYS_CPU_CTL &= ~(1 << 7);
  158. /* cpu cnt clk enable */
  159. rSYS_CPU_CTL |= 1 << 23;
  160. /* nand pad enable */
  161. val = rSYS_PAD_CTRL08;
  162. val &= ~((0x7<<27) | (0x7<<24) | (0x7<<21) | (0x7<<18) | (0x7<<15) | (0x7<<12) | (0x7<<9) | (0x7<<6));
  163. val |= (0x1<<27) | (0x1<<24) | (0x1<<21) | (0x1<<18) | (0x1<<15) | (0x1<<12) | (0x1<<9) | (0x1<<6);
  164. rSYS_PAD_CTRL08 = val;
  165. val = rSYS_PAD_CTRL09;
  166. val &= ~((0x7<<15) | (0x7<<12) | (0x7<<9) | (0x7<<6) | (0x7<<3) | (0x7<<0));
  167. val |= (1<<15) | (1<<12) | (1<<9) | (1<<6) | (1<<3) | (1<<0);//enable nand cle, ale,ren,wen
  168. rSYS_PAD_CTRL09 = val;
  169. /* spi pad enable */
  170. val = rSYS_PAD_CTRL09;
  171. val &= ~((0x7<<27) | (0x7<<24));
  172. val |= (0x2<<27) | (0x2<<24);
  173. rSYS_PAD_CTRL09 = val;
  174. val = rSYS_PAD_CTRL0A;
  175. val &= ~((0x7<<3) | (0x7<<0));
  176. val |= (0x2<<0);
  177. rSYS_PAD_CTRL0A = val;
  178. /* gmac pad enable */
  179. rSYS_PAD_CTRL0C = (1 << 27) | (1 << 24) | (1 << 21) | (1 << 18) | (1 << 15) | (1 << 12) | (1 << 9) |
  180. (1 << 6) | (1 << 3) | (1 << 0);
  181. //rSYS_PAD_CTRL0D = (1 << 24) | (1 << 9) | (1 << 6) | (1 << 3) | (1 << 0);
  182. rSYS_PAD_CTRL0D = (1 << 24) | (1 << 21) | (1 << 18) | (1 << 15) | (1 << 12) | (1 << 9) |
  183. (1 << 6) | (1 << 3) | (1 << 0);
  184. /* gmac tx clk out */
  185. rSYS_PAD_CTRL0F |= (1 << 31);
  186. /* select rgmii interface */
  187. rSYS_MFC_GMAC_CTL &= ~(7 << 1);
  188. rSYS_MFC_GMAC_CTL |= (1 << 1);
  189. /* mac rx clk inv */
  190. rSYS_DEVICE_CLK_CFG7 |= (1 << 1);
  191. /* mac tx clk inv */
  192. rSYS_DEVICE_CLK_CFG8 |= (1 << 7);
  193. /* i2s0 sadata in */
  194. rSYS_PAD_CTRL0F &= ~(1 << 28);
  195. /* i2s1 sadata out */
  196. rSYS_PAD_CTRL0F |= (1 << 29);
  197. /* select pwm0 pad */
  198. rSYS_PAD_CTRL05 &= ~0x7;
  199. rSYS_PAD_CTRL05 |= 1;
  200. return 0;
  201. }
  202. #ifdef CONFIG_BOARD_EARLY_INIT_F
  203. int board_early_init_f(void)
  204. {
  205. #ifdef CONFIG_DEBUG_UART
  206. debug_uart_init();
  207. #endif
  208. return 0;
  209. }
  210. #endif
  211. static int get_data_from_media(char *file_name)
  212. {
  213. int ret = -1;
  214. char cmd[128] = { 0 };
  215. printf("file_name=%s\n", file_name);
  216. sprintf(cmd, "fatload %s %s %s %s",env_get("update_dev_type"),env_get("update_dev_part"),env_get("loadaddr"), file_name);
  217. printf("cmd=%s\n", cmd);
  218. ret = run_command(cmd, 0);
  219. return ret;
  220. }
  221. static int burn_data_2_emmc_partition(char *partition_name)
  222. {
  223. char cmd[128] = { 0 };
  224. int ret = -1;
  225. int file_size = 0;
  226. unsigned int i;
  227. unsigned int *srcdata = (unsigned int *)(env_get_hex("loadaddr", 0));
  228. unsigned int *dstdata = (unsigned int *)(env_get_hex("cmploadaddr", 0));
  229. sprintf(cmd, "emmc erase.part %s", partition_name);
  230. printf("cmd=%s\n", cmd);
  231. ret = run_command(cmd, 0);
  232. if (ret)
  233. return ret;
  234. file_size = env_get_ulong("filesize", 16, 0x2000);
  235. sprintf(cmd, "emmc write %s %s 0x%x", env_get("loadaddr"), partition_name, file_size);
  236. printf("cmd=%s\n", cmd);
  237. ret = run_command(cmd, 0);
  238. sprintf(cmd, "emmc read %s %s 0x%x",env_get("cmploadaddr"),partition_name, file_size);
  239. printf("cmd=%s\n", cmd);
  240. ret = run_command(cmd, 0);
  241. for(i = 0;i< file_size/4;i++)
  242. if(srcdata[i] != dstdata[i])
  243. {
  244. printf("check %s data error!!!\n",partition_name);
  245. return CHECKDATA_ERROR;
  246. }
  247. return ret;
  248. }
  249. static int ark_update_emmc_partition(char *partition_name, char *file_name)
  250. {
  251. int ret;
  252. ret = get_data_from_media(file_name);
  253. if (!ret) {
  254. return burn_data_2_emmc_partition(partition_name);
  255. }
  256. return ret;
  257. }
  258. static int ark_update_emmc_rootfs_from_media(char *partition_name)
  259. {
  260. char cmd[32];
  261. int ret;
  262. sprintf(cmd, "updaterootfs %s", partition_name);
  263. printf("cmd=%s\n", cmd);
  264. ret = run_command(cmd, 0);
  265. return ret;
  266. }
  267. static int do_update_from_media(void)
  268. {
  269. char cmd[32];
  270. unsigned int ret = 0;
  271. char *update_dev = NULL;
  272. char *curr_partition = NULL;
  273. char *update_fdt = NULL;
  274. unsigned char flag_partiton = 0 ;//0--A partiton,1---B partition
  275. sprintf(cmd, "mmc dev %s",env_get("emmc_dev_part"));
  276. printf("cmd=%s\n", cmd);
  277. run_command(cmd, 0);
  278. sprintf((char *)cmd,"disconfig 0");
  279. run_command(cmd, 0);
  280. update_fdt = env_get("emmcfdt");
  281. printf("update_fdt:%s\n", update_fdt);
  282. update_dev = env_get("update_dev_type");
  283. if (!strcmp(update_dev, "mmc"))
  284. {
  285. env_set("updata_status", "none");
  286. env_set("updata_from_part", "A");
  287. curr_partition = env_get("updata_from_part");
  288. if(!strcmp(curr_partition, "A"))
  289. flag_partiton = 0;
  290. else if(!strcmp(curr_partition, "B"))
  291. flag_partiton = 1;
  292. }else if(!strcmp(update_dev, "usb"))
  293. {
  294. env_set("updata_status", "usb");
  295. curr_partition = env_get("updata_from_part");
  296. if(!strcmp(curr_partition, "A"))
  297. flag_partiton = 1;
  298. else if(!strcmp(curr_partition, "B"))
  299. flag_partiton = 0;
  300. }
  301. printf("\r\n **** update from update ubootspl .....\r\n");
  302. ret = ark_update_emmc_partition("bootstrap", "ubootspl.bin");
  303. mdelay(30);
  304. if(!ret)
  305. {
  306. sprintf(cmd, "setenv bootstrapsize %s",env_get("filesize"));
  307. printf("cmd=%s\n", cmd);
  308. run_command(cmd, 0);
  309. mdelay(30);
  310. sprintf((char *)cmd,"disconfig 5");
  311. run_command(cmd, 0);
  312. }
  313. else if(ret == CHECKDATA_ERROR)
  314. goto bootoldsys;
  315. printf("\r\n **** update from update uboot .....\r\n");
  316. ret = ark_update_emmc_partition("bootloader", "u-boot.img");
  317. mdelay(30);
  318. if(!ret)
  319. {
  320. sprintf(cmd, "setenv bootloadersize %s",env_get("filesize"));
  321. printf("cmd=%s\n", cmd);
  322. run_command(cmd, 0);
  323. mdelay(30);
  324. sprintf((char *)cmd,"disconfig 10");
  325. run_command(cmd, 0);
  326. }
  327. else if(ret == CHECKDATA_ERROR)
  328. goto bootoldsys;
  329. printf("\r\n **** update from update fdt .....\r\n");
  330. if (flag_partiton == 0)
  331. ret = ark_update_emmc_partition("fdt", update_fdt);
  332. else
  333. ret = ark_update_emmc_partition("fdt_b", update_fdt);
  334. mdelay(30);
  335. if(!ret)
  336. {
  337. if(flag_partiton == 0)
  338. sprintf(cmd, "setenv fdtsize_a %s",env_get("filesize"));
  339. else
  340. sprintf(cmd, "setenv fdtsize_b %s",env_get("filesize"));
  341. run_command(cmd, 0);
  342. printf("cmd=%s\n", cmd);
  343. mdelay(30);
  344. sprintf((char *)cmd,"disconfig 15");
  345. run_command(cmd, 0);
  346. }
  347. else if(ret == CHECKDATA_ERROR)
  348. goto bootoldsys;
  349. printf("\r\n **** update from update kernel .....\r\n");
  350. if (flag_partiton == 0)
  351. ret = ark_update_emmc_partition("kernel", "zImage");
  352. else
  353. ret = ark_update_emmc_partition("kernel_b", "zImage");
  354. mdelay(30);
  355. if(!ret)
  356. {
  357. if(flag_partiton == 0)
  358. sprintf(cmd, "setenv kernelsize_a %s",env_get("filesize"));
  359. else
  360. sprintf(cmd, "setenv kernelsize_b %s",env_get("filesize"));
  361. printf("cmd=%s\n", cmd);
  362. run_command(cmd, 0);
  363. mdelay(30);
  364. sprintf((char *)cmd,"disconfig 25");
  365. run_command(cmd, 0);
  366. }
  367. else if(ret == CHECKDATA_ERROR)
  368. goto bootoldsys;
  369. printf("\r\n **** update from update bootanimation .....\r\n");
  370. ret = ark_update_emmc_partition("bootanimation", "bootanimation");
  371. mdelay(30);
  372. if(!ret)
  373. {
  374. sprintf(cmd, "setenv bootanimationsize %s",env_get("filesize"));
  375. run_command(cmd, 0);
  376. printf("cmd=%s\n", cmd);
  377. mdelay(30);
  378. sprintf((char *)cmd,"disconfig 30");
  379. run_command(cmd, 0);
  380. }
  381. else if(ret == CHECKDATA_ERROR)
  382. goto bootoldsys;
  383. printf("\r\n **** update from update reversingtrack .....\r\n");
  384. ret = ark_update_emmc_partition("reversingtrack", "reversingtrack");
  385. mdelay(30);
  386. if(!ret)
  387. {
  388. sprintf(cmd, "setenv reversingtracksize %s",env_get("filesize"));
  389. run_command(cmd, 0);
  390. printf("cmd=%s\n", cmd);
  391. mdelay(30);
  392. sprintf((char *)cmd,"disconfig 35");
  393. run_command(cmd, 0);
  394. }
  395. else if(ret == CHECKDATA_ERROR)
  396. goto bootoldsys;
  397. printf("\r\n **** update from update uboot back .....\r\n");
  398. ret = ark_update_emmc_partition("bootloader_bak", "u-boot.img");
  399. mdelay(30);
  400. if(!ret)
  401. {
  402. sprintf(cmd, "setenv bootloadersize %s",env_get("filesize"));
  403. run_command(cmd, 0);
  404. printf("cmd=%s\n", cmd);
  405. mdelay(30);
  406. sprintf((char *)cmd,"disconfig 40");
  407. run_command(cmd, 0);
  408. }
  409. else if(ret == CHECKDATA_ERROR)
  410. goto bootoldsys;
  411. printf("\r\n **** update from update rootfs .....\r\n");
  412. if (flag_partiton == 0)
  413. ret = ark_update_emmc_rootfs_from_media("rootfs");
  414. else
  415. ret = ark_update_emmc_rootfs_from_media("rootfs_b");
  416. if(!ret)
  417. {
  418. sprintf((char *)cmd,"disconfig 95");
  419. run_command(cmd, 0);
  420. }
  421. else if(ret == CHECKDATA_ERROR)
  422. goto bootoldsys;
  423. printf("\r\n **** set the env to partition .....\r\n");
  424. //mmc update set part A
  425. if(!strcmp(update_dev, "mmc"))
  426. {
  427. env_set("updata_from_part", "A");
  428. env_set("kernel_part", "kernel");
  429. env_set("fdt_part", "fdt");
  430. env_set("emmcroot", "/dev/mmcblk0p10 rw");
  431. sprintf(cmd, "setenv fdtsize %s",env_get("fdtsize_a"));
  432. run_command(cmd, 0);
  433. printf("cmd=%s\n", cmd);
  434. mdelay(30);
  435. sprintf(cmd, "setenv kernelsize %s",env_get("kernelsize_a"));
  436. run_command(cmd, 0);
  437. printf("cmd=%s\n", cmd);
  438. mdelay(30);
  439. }
  440. else if(!strcmp(update_dev, "usb"))
  441. {
  442. curr_partition = env_get("updata_from_part");
  443. if(!strcmp(curr_partition, "A"))
  444. {
  445. env_set("updata_from_part", "B");
  446. env_set("kernel_part", "kernel_b");
  447. env_set("fdt_part", "fdt_b");
  448. env_set("emmcroot", "/dev/mmcblk0p14 rw");
  449. sprintf(cmd, "setenv fdtsize %s",env_get("fdtsize_b"));
  450. run_command(cmd, 0);
  451. printf("cmd=%s\n", cmd);
  452. mdelay(30);
  453. sprintf(cmd, "setenv kernelsize %s",env_get("kernelsize_b"));
  454. run_command(cmd, 0);
  455. printf("cmd=%s\n", cmd);
  456. mdelay(30);
  457. }
  458. else if(!strcmp(curr_partition, "B"))
  459. {
  460. env_set("updata_from_part", "A");
  461. env_set("kernel_part", "kernel");
  462. env_set("fdt_part", "fdt");
  463. env_set("emmcroot", "/dev/mmcblk0p10 rw");
  464. sprintf(cmd, "setenv fdtsize %s",env_get("fdtsize_a"));
  465. run_command(cmd, 0);
  466. printf("cmd=%s\n", cmd);
  467. mdelay(30);
  468. sprintf(cmd, "setenv kernelsize %s",env_get("kernelsize_a"));
  469. run_command(cmd, 0);
  470. printf("cmd=%s\n", cmd);
  471. mdelay(30);
  472. }
  473. }
  474. mdelay(5);
  475. env_set("need_update", "no");
  476. mdelay(5);
  477. env_set("updata_status", "none");
  478. sprintf(cmd, "saveenv");
  479. printf("cmd=%s\n", cmd);
  480. run_command(cmd, 0);
  481. mdelay(30);
  482. sprintf((char *)cmd,"disconfig 100");
  483. run_command(cmd, 0);
  484. printf("\r\n **** update the device over! .....\r\n");
  485. return 0;
  486. bootoldsys:
  487. env_set("updata_status", "error");
  488. sprintf(cmd, "saveenv");
  489. printf("cmd=%s\n", cmd);
  490. run_command(cmd, 0);
  491. mdelay(20);
  492. sprintf((char *)cmd,"disconfig 100");
  493. run_command(cmd, 0);
  494. return 1;
  495. }
  496. int board_late_init(void)
  497. {
  498. char cmd[128];
  499. char *update_from_ota = NULL,*need_update = NULL;
  500. unsigned int loadaddr;
  501. int do_update = 0, update_from_mmc = 1;
  502. char *curr_partition = NULL;
  503. #ifdef CONFIG_USB_MUSB_HOST
  504. usb_controller_reset();
  505. musb_register(&musb_platform_data, NULL, (void *)MUSB_BASE);
  506. #endif
  507. update_from_ota = env_get("update_from_ota");
  508. printf("update_from_ota %s\n",update_from_ota);
  509. need_update = env_get("need_update");
  510. if (!strcmp(need_update, "yes")) {
  511. loadaddr = env_get_hex("loadaddr", 0);
  512. if (loadaddr)
  513. memset((void*)loadaddr, 0, strlen(ARK1668_UPDATE_MAGIC));
  514. sprintf(cmd, "fatload %s %s %s update-magic", "mmc", env_get("sd_dev_part"), env_get("loadaddr"));
  515. printf("cmd %s\n",cmd);
  516. run_command(cmd, 0);
  517. if (loadaddr && !memcmp((void *)loadaddr, ARK1668_UPDATE_MAGIC, strlen(ARK1668_UPDATE_MAGIC))) {
  518. do_update = 1;
  519. goto update_done;
  520. } else {
  521. printf("Wrong update magic, do not update from mmc.\n");
  522. }
  523. #ifdef CONFIG_USB_MUSB_HOST
  524. run_command("usb start", 0);
  525. sprintf(cmd, "fatload %s %s %s update-magic", "usb", "0", env_get("loadaddr"));
  526. run_command(cmd, 0);
  527. if (loadaddr && !memcmp((void *)loadaddr, ARK1668_UPDATE_MAGIC, strlen(ARK1668_UPDATE_MAGIC))) {
  528. do_update = 1;
  529. update_from_mmc = 0;
  530. curr_partition = env_get("updata_from_part");
  531. printf("++updata_from_part %s++\n", curr_partition);
  532. run_command("env default -f -a", 0);
  533. mdelay(500);
  534. if(!strcmp(curr_partition, "A"))
  535. env_set("updata_from_part", "A");
  536. else if(!strcmp(curr_partition, "B"))
  537. env_set("updata_from_part", "B");
  538. else
  539. env_set("updata_from_part", "A");
  540. printf("++++++++++++++%s++++++++++\n",curr_partition);
  541. } else {
  542. printf("Wrong update magic, do not update from usb.\n");
  543. }
  544. #endif
  545. }
  546. else if(!strcmp(update_from_ota, "yes")){
  547. do_update = 0;
  548. curr_partition = env_get("updata_from_part");
  549. printf("++updata_from_part %s++\n", curr_partition);
  550. run_command("env default -f -a", 0);
  551. mdelay(500);
  552. if(!strcmp(curr_partition, "A"))
  553. env_set("updata_from_part", "A");
  554. else if(!strcmp(curr_partition, "B"))
  555. env_set("updata_from_part", "B");
  556. else
  557. env_set("updata_from_part", "A");
  558. printf("++++++++++++++%s++++++++++\n",curr_partition);
  559. sprintf(cmd, "update_from_emmc_ota");
  560. printf("cmd=%s\n", cmd);
  561. run_command(cmd, 0);
  562. }
  563. update_done:
  564. if (do_update) {
  565. run_command("emmc erase.part userdata", 0);
  566. env_set("need_update", "no");
  567. env_set("do_update", "yes");
  568. if (update_from_mmc) {
  569. printf("update form mmc...\n");
  570. env_set("update_dev_type", "mmc");
  571. env_set("update_dev_part", env_get("sd_dev_part"));
  572. } else {
  573. printf("update form usb...\n");
  574. env_set("update_dev_type", "usb");
  575. env_set("update_dev_part", "0");
  576. env_set("updata_status", "usb");
  577. }
  578. do_update_from_media();
  579. } else {
  580. env_set("do_update", "no");
  581. }
  582. return 0;
  583. }