cam_enc_4xx.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092
  1. /*
  2. * Copyright (C) 2009 Texas Instruments Incorporated
  3. *
  4. * Copyright (C) 2011
  5. * Heiko Schocher, DENX Software Engineering, hs@denx.de.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #include <common.h>
  22. #include <errno.h>
  23. #include <linux/mtd/nand.h>
  24. #include <nand.h>
  25. #include <miiphy.h>
  26. #include <netdev.h>
  27. #include <asm/io.h>
  28. #include <asm/arch/hardware.h>
  29. #include <asm/arch/nand_defs.h>
  30. #include <asm/arch/davinci_misc.h>
  31. #ifdef CONFIG_DAVINCI_MMC
  32. #include <mmc.h>
  33. #include <asm/arch/sdmmc_defs.h>
  34. #endif
  35. DECLARE_GLOBAL_DATA_PTR;
  36. #ifndef CONFIG_SPL_BUILD
  37. static struct davinci_timer *timer =
  38. (struct davinci_timer *)DAVINCI_TIMER3_BASE;
  39. static unsigned long get_timer_val(void)
  40. {
  41. unsigned long now = readl(&timer->tim34);
  42. return now;
  43. }
  44. static int timer_running(void)
  45. {
  46. return readl(&timer->tcr) &
  47. (DV_TIMER_TCR_ENAMODE_MASK << DV_TIMER_TCR_ENAMODE34_SHIFT);
  48. }
  49. static void stop_timer(void)
  50. {
  51. writel(0x0, &timer->tcr);
  52. return;
  53. }
  54. int checkboard(void)
  55. {
  56. printf("Board: AIT CAM ENC 4XX\n");
  57. return 0;
  58. }
  59. int board_init(void)
  60. {
  61. gd->bd->bi_boot_params = PHYS_SDRAM_1 + 0x100;
  62. return 0;
  63. }
  64. #ifdef CONFIG_DRIVER_TI_EMAC
  65. static int cam_enc_4xx_check_network(void)
  66. {
  67. char *s;
  68. s = getenv("ethaddr");
  69. if (!s)
  70. return -EINVAL;
  71. if (!is_valid_ether_addr((const u8 *)s))
  72. return -EINVAL;
  73. s = getenv("ipaddr");
  74. if (!s)
  75. return -EINVAL;
  76. s = getenv("netmask");
  77. if (!s)
  78. return -EINVAL;
  79. s = getenv("serverip");
  80. if (!s)
  81. return -EINVAL;
  82. s = getenv("gatewayip");
  83. if (!s)
  84. return -EINVAL;
  85. return 0;
  86. }
  87. int board_eth_init(bd_t *bis)
  88. {
  89. int ret;
  90. ret = cam_enc_4xx_check_network();
  91. if (ret)
  92. return ret;
  93. davinci_emac_initialize();
  94. return 0;
  95. }
  96. #endif
  97. #ifdef CONFIG_NAND_DAVINCI
  98. static int
  99. davinci_std_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
  100. uint8_t *buf, int page)
  101. {
  102. struct nand_chip *this = mtd->priv;
  103. int i, eccsize = chip->ecc.size;
  104. int eccbytes = chip->ecc.bytes;
  105. int eccsteps = chip->ecc.steps;
  106. uint8_t *p = buf;
  107. uint8_t *oob = chip->oob_poi;
  108. chip->cmdfunc(mtd, NAND_CMD_READOOB, 0x0, page & this->pagemask);
  109. chip->read_buf(mtd, oob, mtd->oobsize);
  110. chip->cmdfunc(mtd, NAND_CMD_READ0, 0x0, page & this->pagemask);
  111. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  112. int stat;
  113. chip->ecc.hwctl(mtd, NAND_ECC_READ);
  114. chip->read_buf(mtd, p, eccsize);
  115. chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
  116. if (chip->ecc.prepad)
  117. oob += chip->ecc.prepad;
  118. stat = chip->ecc.correct(mtd, p, oob, NULL);
  119. if (stat == -1)
  120. mtd->ecc_stats.failed++;
  121. else
  122. mtd->ecc_stats.corrected += stat;
  123. oob += eccbytes;
  124. if (chip->ecc.postpad)
  125. oob += chip->ecc.postpad;
  126. }
  127. /* Calculate remaining oob bytes */
  128. i = mtd->oobsize - (oob - chip->oob_poi);
  129. if (i)
  130. chip->read_buf(mtd, oob, i);
  131. return 0;
  132. }
  133. static void davinci_std_write_page_syndrome(struct mtd_info *mtd,
  134. struct nand_chip *chip, const uint8_t *buf)
  135. {
  136. unsigned char davinci_ecc_buf[NAND_MAX_OOBSIZE];
  137. struct nand_chip *this = mtd->priv;
  138. int i, eccsize = chip->ecc.size;
  139. int eccbytes = chip->ecc.bytes;
  140. int eccsteps = chip->ecc.steps;
  141. int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
  142. int offset = 0;
  143. const uint8_t *p = buf;
  144. uint8_t *oob = chip->oob_poi;
  145. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  146. chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
  147. chip->write_buf(mtd, p, eccsize);
  148. /* Calculate ECC without prepad */
  149. chip->ecc.calculate(mtd, p, oob + chip->ecc.prepad);
  150. if (chip->ecc.prepad) {
  151. offset = (chip->ecc.steps - eccsteps) * chunk;
  152. memcpy(&davinci_ecc_buf[offset], oob, chip->ecc.prepad);
  153. oob += chip->ecc.prepad;
  154. }
  155. offset = ((chip->ecc.steps - eccsteps) * chunk) +
  156. chip->ecc.prepad;
  157. memcpy(&davinci_ecc_buf[offset], oob, eccbytes);
  158. oob += eccbytes;
  159. if (chip->ecc.postpad) {
  160. offset = ((chip->ecc.steps - eccsteps) * chunk) +
  161. chip->ecc.prepad + eccbytes;
  162. memcpy(&davinci_ecc_buf[offset], oob,
  163. chip->ecc.postpad);
  164. oob += chip->ecc.postpad;
  165. }
  166. }
  167. /*
  168. * Write the sparebytes into the page once
  169. * all eccsteps have been covered
  170. */
  171. for (i = 0; i < mtd->oobsize; i++)
  172. writeb(davinci_ecc_buf[i], this->IO_ADDR_W);
  173. /* Calculate remaining oob bytes */
  174. i = mtd->oobsize - (oob - chip->oob_poi);
  175. if (i)
  176. chip->write_buf(mtd, oob, i);
  177. }
  178. static int davinci_std_write_oob_syndrome(struct mtd_info *mtd,
  179. struct nand_chip *chip, int page)
  180. {
  181. int pos, status = 0;
  182. const uint8_t *bufpoi = chip->oob_poi;
  183. pos = mtd->writesize;
  184. chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
  185. chip->write_buf(mtd, bufpoi, mtd->oobsize);
  186. chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
  187. status = chip->waitfunc(mtd, chip);
  188. return status & NAND_STATUS_FAIL ? -1 : 0;
  189. }
  190. static int davinci_std_read_oob_syndrome(struct mtd_info *mtd,
  191. struct nand_chip *chip, int page, int sndcmd)
  192. {
  193. struct nand_chip *this = mtd->priv;
  194. uint8_t *buf = chip->oob_poi;
  195. uint8_t *bufpoi = buf;
  196. chip->cmdfunc(mtd, NAND_CMD_READOOB, 0x0, page & this->pagemask);
  197. chip->read_buf(mtd, bufpoi, mtd->oobsize);
  198. return 1;
  199. }
  200. static void nand_dm365evm_select_chip(struct mtd_info *mtd, int chip)
  201. {
  202. struct nand_chip *this = mtd->priv;
  203. unsigned long wbase = (unsigned long) this->IO_ADDR_W;
  204. unsigned long rbase = (unsigned long) this->IO_ADDR_R;
  205. if (chip == 1) {
  206. __set_bit(14, &wbase);
  207. __set_bit(14, &rbase);
  208. } else {
  209. __clear_bit(14, &wbase);
  210. __clear_bit(14, &rbase);
  211. }
  212. this->IO_ADDR_W = (void *)wbase;
  213. this->IO_ADDR_R = (void *)rbase;
  214. }
  215. int board_nand_init(struct nand_chip *nand)
  216. {
  217. davinci_nand_init(nand);
  218. nand->select_chip = nand_dm365evm_select_chip;
  219. return 0;
  220. }
  221. struct nand_ecc_ctrl org_ecc;
  222. static int notsaved = 1;
  223. static int nand_switch_hw_func(int mode)
  224. {
  225. struct nand_chip *nand;
  226. struct mtd_info *mtd;
  227. if (nand_curr_device < 0 ||
  228. nand_curr_device >= CONFIG_SYS_MAX_NAND_DEVICE ||
  229. !nand_info[nand_curr_device].name) {
  230. printf("Error: Can't switch hw functions," \
  231. " no devices available\n");
  232. return -1;
  233. }
  234. mtd = &nand_info[nand_curr_device];
  235. nand = mtd->priv;
  236. if (mode == 0) {
  237. if (notsaved == 0) {
  238. printf("switching to uboot hw functions.\n");
  239. memcpy(&nand->ecc, &org_ecc,
  240. sizeof(struct nand_ecc_ctrl));
  241. }
  242. } else {
  243. /* RBL */
  244. printf("switching to RBL hw functions.\n");
  245. if (notsaved == 1) {
  246. memcpy(&org_ecc, &nand->ecc,
  247. sizeof(struct nand_ecc_ctrl));
  248. notsaved = 0;
  249. }
  250. nand->ecc.mode = NAND_ECC_HW_SYNDROME;
  251. nand->ecc.prepad = 6;
  252. nand->ecc.read_page = davinci_std_read_page_syndrome;
  253. nand->ecc.write_page = davinci_std_write_page_syndrome;
  254. nand->ecc.read_oob = davinci_std_read_oob_syndrome;
  255. nand->ecc.write_oob = davinci_std_write_oob_syndrome;
  256. }
  257. return mode;
  258. }
  259. static int hwmode;
  260. static int do_switch_ecc(cmd_tbl_t *cmdtp, int flag, int argc,
  261. char *const argv[])
  262. {
  263. if (argc != 2)
  264. goto usage;
  265. if (strncmp(argv[1], "rbl", 2) == 0)
  266. hwmode = nand_switch_hw_func(1);
  267. else if (strncmp(argv[1], "uboot", 2) == 0)
  268. hwmode = nand_switch_hw_func(0);
  269. else
  270. goto usage;
  271. return 0;
  272. usage:
  273. printf("Usage: nandrbl %s\n", cmdtp->usage);
  274. return 1;
  275. }
  276. U_BOOT_CMD(
  277. nandrbl, 2, 1, do_switch_ecc,
  278. "switch between rbl/uboot NAND ECC calculation algorithm",
  279. "[rbl/uboot] - Switch between rbl/uboot NAND ECC algorithm"
  280. );
  281. #endif /* #ifdef CONFIG_NAND_DAVINCI */
  282. #ifdef CONFIG_DAVINCI_MMC
  283. static struct davinci_mmc mmc_sd0 = {
  284. .reg_base = (struct davinci_mmc_regs *)DAVINCI_MMC_SD0_BASE,
  285. .input_clk = 121500000,
  286. .host_caps = MMC_MODE_4BIT,
  287. .voltages = MMC_VDD_32_33 | MMC_VDD_33_34,
  288. .version = MMC_CTLR_VERSION_2,
  289. };
  290. int board_mmc_init(bd_t *bis)
  291. {
  292. int err;
  293. /* Add slot-0 to mmc subsystem */
  294. err = davinci_mmc_init(bis, &mmc_sd0);
  295. return err;
  296. }
  297. #endif
  298. int board_late_init(void)
  299. {
  300. struct davinci_gpio *gpio = davinci_gpio_bank45;
  301. /* 24MHz InputClock / 15 prediv -> 1.6 MHz timer running */
  302. while ((get_timer_val() < CONFIG_AIT_TIMER_TIMEOUT) &&
  303. timer_running())
  304. ;
  305. /* 1 sec reached -> stop timer, clear all LED */
  306. stop_timer();
  307. clrbits_le32(&gpio->out_data, CONFIG_CAM_ENC_LED_MASK);
  308. return 0;
  309. }
  310. void reset_phy(void)
  311. {
  312. char *name = "GENERIC @ 0x00";
  313. /* reset the phy */
  314. miiphy_reset(name, 0x0);
  315. }
  316. #else /* #ifndef CONFIG_SPL_BUILD */
  317. static void cam_enc_4xx_set_all_led(void)
  318. {
  319. struct davinci_gpio *gpio = davinci_gpio_bank45;
  320. setbits_le32(&gpio->out_data, CONFIG_CAM_ENC_LED_MASK);
  321. }
  322. /*
  323. * TIMER 0 is used for tick
  324. */
  325. static struct davinci_timer *timer =
  326. (struct davinci_timer *)DAVINCI_TIMER3_BASE;
  327. #define TIMER_LOAD_VAL 0xffffffff
  328. #define TIM_CLK_DIV 16
  329. static int cam_enc_4xx_timer_init(void)
  330. {
  331. /* We are using timer34 in unchained 32-bit mode, full speed */
  332. writel(0x0, &timer->tcr);
  333. writel(0x0, &timer->tgcr);
  334. writel(0x06 | ((TIM_CLK_DIV - 1) << 8), &timer->tgcr);
  335. writel(0x0, &timer->tim34);
  336. writel(TIMER_LOAD_VAL, &timer->prd34);
  337. writel(2 << 22, &timer->tcr);
  338. return 0;
  339. }
  340. void board_gpio_init(void)
  341. {
  342. struct davinci_gpio *gpio;
  343. cam_enc_4xx_set_all_led();
  344. cam_enc_4xx_timer_init();
  345. gpio = davinci_gpio_bank01;
  346. clrbits_le32(&gpio->dir, ~0xfdfffffe);
  347. /* clear LED D14 = GPIO25 */
  348. clrbits_le32(&gpio->out_data, 0x02000000);
  349. gpio = davinci_gpio_bank23;
  350. clrbits_le32(&gpio->dir, ~0x5ff0afef);
  351. /* set GPIO61 to 1 -> intern UART0 as Console */
  352. setbits_le32(&gpio->out_data, 0x20000000);
  353. /*
  354. * PHY out of reset GIO 50 = 1
  355. * NAND WP off GIO 51 = 1
  356. */
  357. setbits_le32(&gpio->out_data, 0x000c0004);
  358. gpio = davinci_gpio_bank45;
  359. clrbits_le32(&gpio->dir, ~(0xdb2fffff) | CONFIG_CAM_ENC_LED_MASK);
  360. /*
  361. * clear LED:
  362. * D17 = GPIO86
  363. * D11 = GPIO87
  364. * GPIO88
  365. * GPIO89
  366. * D13 = GPIO90
  367. * GPIO91
  368. */
  369. clrbits_le32(&gpio->out_data, CONFIG_CAM_ENC_LED_MASK);
  370. gpio = davinci_gpio_bank67;
  371. clrbits_le32(&gpio->dir, ~0x000007ff);
  372. }
  373. /*
  374. * functions for the post memory test.
  375. */
  376. int arch_memory_test_prepare(u32 *vstart, u32 *size, phys_addr_t *phys_offset)
  377. {
  378. *vstart = CONFIG_SYS_SDRAM_BASE;
  379. *size = PHYS_SDRAM_1_SIZE;
  380. *phys_offset = 0;
  381. return 0;
  382. }
  383. void arch_memory_failure_handle(void)
  384. {
  385. cam_enc_4xx_set_all_led();
  386. puts("mem failure\n");
  387. while (1)
  388. ;
  389. }
  390. #endif
  391. #if defined(CONFIG_MENU)
  392. #include "menu.h"
  393. #define MENU_EXIT -1
  394. #define MENU_EXIT_BOOTCMD -2
  395. #define MENU_STAY 0
  396. #define MENU_MAIN 1
  397. #define MENU_UPDATE 2
  398. #define MENU_NETWORK 3
  399. #define MENU_LOAD 4
  400. static int menu_start;
  401. #define FIT_SUBTYPE_UNKNOWN 0
  402. #define FIT_SUBTYPE_UBL_HEADER 1
  403. #define FIT_SUBTYPE_SPL_IMAGE 2
  404. #define FIT_SUBTYPE_UBOOT_IMAGE 3
  405. #define FIT_SUBTYPE_DF_ENV_IMAGE 4
  406. #define FIT_SUBTYPE_RAMDISK_IMAGE 5
  407. struct fit_images_info {
  408. u_int8_t type;
  409. int subtype;
  410. char desc[200];
  411. const void *data;
  412. size_t size;
  413. };
  414. static struct fit_images_info imgs[10];
  415. struct menu_display {
  416. char title[50];
  417. int timeout; /* in sec */
  418. int id; /* MENU_* */
  419. char **menulist;
  420. int (*menu_evaluate)(char *choice);
  421. };
  422. char *menu_main[] = {
  423. "(1) Boot",
  424. "(2) Update Software",
  425. "(3) Reset to default setting and boot",
  426. "(4) Enter U-Boot console",
  427. NULL
  428. };
  429. char *menu_update[] = {
  430. "(1) Network settings",
  431. "(2) load image",
  432. "(3) back to main",
  433. NULL
  434. };
  435. char *menu_load[] = {
  436. "(1) install image",
  437. "(2) cancel",
  438. NULL
  439. };
  440. char *menu_network[] = {
  441. "(1) ipaddr ",
  442. "(2) netmask ",
  443. "(3) serverip ",
  444. "(4) gatewayip",
  445. "(5) tftp image name",
  446. "(6) back to update software",
  447. NULL
  448. };
  449. static void ait_menu_print(void *data)
  450. {
  451. printf("%s\n", (char *)data);
  452. return;
  453. }
  454. static char *menu_handle(struct menu_display *display)
  455. {
  456. struct menu *m;
  457. int i;
  458. char *choice = NULL;
  459. char key[2];
  460. int ret;
  461. char *s;
  462. char temp[6][200];
  463. m = menu_create(display->title, display->timeout, 1, ait_menu_print);
  464. for (i = 0; display->menulist[i]; i++) {
  465. sprintf(key, "%d", i + 1);
  466. if (display->id == MENU_NETWORK) {
  467. switch (i) {
  468. case 0:
  469. s = getenv("ipaddr");
  470. break;
  471. case 1:
  472. s = getenv("netmask");
  473. break;
  474. case 2:
  475. s = getenv("serverip");
  476. break;
  477. case 3:
  478. s = getenv("gatewayip");
  479. break;
  480. case 4:
  481. s = getenv("img_file");
  482. break;
  483. default:
  484. s = NULL;
  485. break;
  486. }
  487. if (s) {
  488. sprintf(temp[i], "%s: %s",
  489. display->menulist[i], s);
  490. ret = menu_item_add(m, key, temp[i]);
  491. } else {
  492. ret = menu_item_add(m, key,
  493. display->menulist[i]);
  494. }
  495. } else {
  496. ret = menu_item_add(m, key, display->menulist[i]);
  497. }
  498. if (ret != 1) {
  499. printf("failed to add item!");
  500. menu_destroy(m);
  501. return NULL;
  502. }
  503. }
  504. sprintf(key, "%d", 1);
  505. menu_default_set(m, key);
  506. if (menu_get_choice(m, (void **)&choice) != 1)
  507. debug("Problem picking a choice!\n");
  508. menu_destroy(m);
  509. return choice;
  510. }
  511. static int ait_menu_show(struct menu_display *display, int bootdelay)
  512. {
  513. int end = MENU_STAY;
  514. char *choice;
  515. if ((menu_start == 0) && (display->id == MENU_MAIN))
  516. display->timeout = bootdelay;
  517. else
  518. display->timeout = 0;
  519. while (end == MENU_STAY) {
  520. choice = menu_handle(display);
  521. if (choice)
  522. end = display->menu_evaluate(choice);
  523. if (end == display->id)
  524. end = MENU_STAY;
  525. if (display->id == MENU_MAIN) {
  526. if (menu_start == 0)
  527. end = MENU_EXIT_BOOTCMD;
  528. else
  529. display->timeout = 0;
  530. }
  531. }
  532. return end;
  533. }
  534. static int ait_writeublheader(void)
  535. {
  536. char s[20];
  537. unsigned long i;
  538. int ret;
  539. for (i = CONFIG_SYS_NAND_BLOCK_SIZE;
  540. i < CONFIG_SYS_NAND_U_BOOT_OFFS;
  541. i += CONFIG_SYS_NAND_BLOCK_SIZE) {
  542. sprintf(s, "%lx", i);
  543. ret = setenv("header_addr", s);
  544. if (ret == 0)
  545. ret = run_command2("run img_writeheader", 0);
  546. if (ret != 0)
  547. break;
  548. }
  549. return ret;
  550. }
  551. static int ait_menu_install_images(void)
  552. {
  553. int ret = 0;
  554. int count = 0;
  555. char s[100];
  556. char *t;
  557. /*
  558. * possible image types:
  559. * FIT_SUBTYPE_UNKNOWN
  560. * FIT_SUBTYPE_UBL_HEADER
  561. * FIT_SUBTYPE_SPL_IMAGE
  562. * FIT_SUBTYPE_UBOOT_IMAGE
  563. * FIT_SUBTYPE_DF_ENV_IMAGE
  564. * FIT_SUBTYPE_RAMDISK_IMAGE
  565. *
  566. * use Envvariables:
  567. * img_addr_r: image start addr
  568. * header_addr: addr where to write to UBL header
  569. * img_writeheader: write ubl header to nand
  570. * img_writespl: write spl to nand
  571. * img_writeuboot: write uboot to nand
  572. * img_writedfenv: write default environment to ubi volume
  573. * img_volume: which ubi volume should be updated with img_writeramdisk
  574. * filesize: size of data for updating ubi volume
  575. * img_writeramdisk: write ramdisk to ubi volume
  576. */
  577. while (imgs[count].type != IH_TYPE_INVALID) {
  578. printf("Installing %s\n",
  579. genimg_get_type_name(imgs[count].type));
  580. sprintf(s, "%p", imgs[count].data);
  581. setenv("img_addr_r", s);
  582. sprintf(s, "%lx", (unsigned long)imgs[count].size);
  583. setenv("filesize", s);
  584. switch (imgs[count].subtype) {
  585. case FIT_SUBTYPE_DF_ENV_IMAGE:
  586. ret = run_command2("run img_writedfenv", 0);
  587. break;
  588. case FIT_SUBTYPE_RAMDISK_IMAGE:
  589. t = getenv("img_volume");
  590. if (!t) {
  591. ret = setenv("img_volume", "rootfs1");
  592. } else {
  593. /* switch to other volume */
  594. if (strncmp(t, "rootfs1", 7) == 0)
  595. ret = setenv("img_volume", "rootfs2");
  596. else
  597. ret = setenv("img_volume", "rootfs1");
  598. }
  599. if (ret != 0)
  600. break;
  601. ret = run_command2("run img_writeramdisk", 0);
  602. break;
  603. case FIT_SUBTYPE_SPL_IMAGE:
  604. ret = run_command2("run img_writespl", 0);
  605. break;
  606. case FIT_SUBTYPE_UBL_HEADER:
  607. ret = ait_writeublheader();
  608. break;
  609. case FIT_SUBTYPE_UBOOT_IMAGE:
  610. ret = run_command2("run img_writeuboot", 0);
  611. break;
  612. default:
  613. /* not supported type */
  614. break;
  615. }
  616. count++;
  617. }
  618. /* now save dvn_* and img_volume env vars to new values */
  619. if (ret == 0)
  620. ret = run_command2("run savenewvers", 0);
  621. return ret;
  622. }
  623. static int ait_menu_evaluate_load(char *choice)
  624. {
  625. if (!choice)
  626. return -1;
  627. switch (choice[1]) {
  628. case '1':
  629. /* install image */
  630. ait_menu_install_images();
  631. break;
  632. case '2':
  633. /* cancel, back to main */
  634. break;
  635. }
  636. return MENU_MAIN;
  637. }
  638. struct menu_display ait_load = {
  639. .title = "AIT load image",
  640. .timeout = 0,
  641. .id = MENU_LOAD,
  642. .menulist = menu_load,
  643. .menu_evaluate = ait_menu_evaluate_load,
  644. };
  645. static void ait_menu_read_env(char *name)
  646. {
  647. char output[CONFIG_SYS_CBSIZE];
  648. char cbuf[CONFIG_SYS_CBSIZE];
  649. int readret;
  650. int ret;
  651. sprintf(output, "%s old: %s value: ", name, getenv(name));
  652. memset(cbuf, 0, CONFIG_SYS_CBSIZE);
  653. readret = readline_into_buffer(output, cbuf, 0);
  654. if (readret >= 0) {
  655. ret = setenv(name, cbuf);
  656. if (ret) {
  657. printf("Error setting %s\n", name);
  658. return;
  659. }
  660. }
  661. return;
  662. }
  663. static int ait_menu_evaluate_network(char *choice)
  664. {
  665. if (!choice)
  666. return MENU_MAIN;
  667. switch (choice[1]) {
  668. case '1':
  669. ait_menu_read_env("ipaddr");
  670. break;
  671. case '2':
  672. ait_menu_read_env("netmask");
  673. break;
  674. case '3':
  675. ait_menu_read_env("serverip");
  676. break;
  677. case '4':
  678. ait_menu_read_env("gatewayip");
  679. break;
  680. case '5':
  681. ait_menu_read_env("img_file");
  682. break;
  683. case '6':
  684. return MENU_UPDATE;
  685. break;
  686. }
  687. return MENU_STAY;
  688. }
  689. struct menu_display ait_network = {
  690. .title = "AIT network settings",
  691. .timeout = 0,
  692. .id = MENU_NETWORK,
  693. .menulist = menu_network,
  694. .menu_evaluate = ait_menu_evaluate_network,
  695. };
  696. static int fit_get_subtype(const void *fit, int noffset, char **subtype)
  697. {
  698. int len;
  699. *subtype = (char *)fdt_getprop(fit, noffset, "subtype", &len);
  700. if (*subtype == NULL)
  701. return -1;
  702. return 0;
  703. }
  704. static int ait_subtype_nr(char *subtype)
  705. {
  706. int ret = FIT_SUBTYPE_UNKNOWN;
  707. if (!strncmp("ublheader", subtype, strlen("ublheader")))
  708. return FIT_SUBTYPE_UBL_HEADER;
  709. if (!strncmp("splimage", subtype, strlen("splimage")))
  710. return FIT_SUBTYPE_SPL_IMAGE;
  711. if (!strncmp("ubootimage", subtype, strlen("ubootimage")))
  712. return FIT_SUBTYPE_UBOOT_IMAGE;
  713. if (!strncmp("dfenvimage", subtype, strlen("dfenvimage")))
  714. return FIT_SUBTYPE_DF_ENV_IMAGE;
  715. return ret;
  716. }
  717. static int ait_menu_check_image(void)
  718. {
  719. char *s;
  720. unsigned long fit_addr;
  721. void *addr;
  722. int format;
  723. char *desc;
  724. char *subtype;
  725. int images_noffset;
  726. int noffset;
  727. int ndepth;
  728. int count = 0;
  729. int ret;
  730. int i;
  731. int found_uboot = -1;
  732. int found_ramdisk = -1;
  733. memset(imgs, 0, sizeof(imgs));
  734. s = getenv("fit_addr_r");
  735. fit_addr = s ? (unsigned long)simple_strtol(s, NULL, 16) : \
  736. CONFIG_BOARD_IMG_ADDR_R;
  737. addr = (void *)fit_addr;
  738. /* check if it is a FIT image */
  739. format = genimg_get_format(addr);
  740. if (format != IMAGE_FORMAT_FIT)
  741. return -EINVAL;
  742. if (!fit_check_format(addr))
  743. return -EINVAL;
  744. /* print the FIT description */
  745. ret = fit_get_desc(addr, 0, &desc);
  746. printf("FIT description: ");
  747. if (ret)
  748. printf("unavailable\n");
  749. else
  750. printf("%s\n", desc);
  751. /* find images */
  752. images_noffset = fdt_path_offset(addr, FIT_IMAGES_PATH);
  753. if (images_noffset < 0) {
  754. printf("Can't find images parent node '%s' (%s)\n",
  755. FIT_IMAGES_PATH, fdt_strerror(images_noffset));
  756. return -EINVAL;
  757. }
  758. /* Process its subnodes, print out component images details */
  759. for (ndepth = 0, count = 0,
  760. noffset = fdt_next_node(addr, images_noffset, &ndepth);
  761. (noffset >= 0) && (ndepth > 0);
  762. noffset = fdt_next_node(addr, noffset, &ndepth)) {
  763. if (ndepth == 1) {
  764. /*
  765. * Direct child node of the images parent node,
  766. * i.e. component image node.
  767. */
  768. printf("Image %u (%s)\n", count,
  769. fit_get_name(addr, noffset, NULL));
  770. fit_image_print(addr, noffset, "");
  771. fit_image_get_type(addr, noffset,
  772. &imgs[count].type);
  773. /* Mandatory properties */
  774. ret = fit_get_desc(addr, noffset, &desc);
  775. printf("Description: ");
  776. if (ret)
  777. printf("unavailable\n");
  778. else
  779. printf("%s\n", desc);
  780. ret = fit_get_subtype(addr, noffset, &subtype);
  781. printf("Subtype: ");
  782. if (ret) {
  783. printf("unavailable\n");
  784. } else {
  785. imgs[count].subtype = ait_subtype_nr(subtype);
  786. printf("%s %d\n", subtype,
  787. imgs[count].subtype);
  788. }
  789. sprintf(imgs[count].desc, "%s", desc);
  790. ret = fit_image_get_data(addr, noffset,
  791. &imgs[count].data,
  792. &imgs[count].size);
  793. printf("Data Size: ");
  794. if (ret)
  795. printf("unavailable\n");
  796. else
  797. genimg_print_size(imgs[count].size);
  798. printf("Data @ %p\n", imgs[count].data);
  799. count++;
  800. }
  801. }
  802. for (i = 0; i < count; i++) {
  803. if (imgs[i].subtype == FIT_SUBTYPE_UBOOT_IMAGE)
  804. found_uboot = i;
  805. if (imgs[i].type == IH_TYPE_RAMDISK) {
  806. found_ramdisk = i;
  807. imgs[i].subtype = FIT_SUBTYPE_RAMDISK_IMAGE;
  808. }
  809. }
  810. /* dvn_* env var update, if the FIT descriptors are different */
  811. if (found_uboot >= 0) {
  812. s = getenv("dvn_boot_vers");
  813. if (s) {
  814. ret = strcmp(s, imgs[found_uboot].desc);
  815. if (ret != 0) {
  816. setenv("dvn_boot_vers",
  817. imgs[found_uboot].desc);
  818. } else {
  819. found_uboot = -1;
  820. printf("no new uboot version\n");
  821. }
  822. } else {
  823. setenv("dvn_boot_vers", imgs[found_uboot].desc);
  824. }
  825. }
  826. if (found_ramdisk >= 0) {
  827. s = getenv("dvn_app_vers");
  828. if (s) {
  829. ret = strcmp(s, imgs[found_ramdisk].desc);
  830. if (ret != 0) {
  831. setenv("dvn_app_vers",
  832. imgs[found_ramdisk].desc);
  833. } else {
  834. found_ramdisk = -1;
  835. printf("no new ramdisk version\n");
  836. }
  837. } else {
  838. setenv("dvn_app_vers", imgs[found_ramdisk].desc);
  839. }
  840. }
  841. if ((found_uboot == -1) && (found_ramdisk == -1))
  842. return -EINVAL;
  843. return 0;
  844. }
  845. static int ait_menu_evaluate_update(char *choice)
  846. {
  847. int ret;
  848. if (!choice)
  849. return MENU_MAIN;
  850. switch (choice[1]) {
  851. case '1':
  852. return ait_menu_show(&ait_network, 0);
  853. break;
  854. case '2':
  855. /* load image */
  856. ret = run_command2("run load_img", 0);
  857. printf("ret: %d\n", ret);
  858. if (ret)
  859. return MENU_UPDATE;
  860. ret = ait_menu_check_image();
  861. if (ret)
  862. return MENU_UPDATE;
  863. return ait_menu_show(&ait_load, 0);
  864. break;
  865. case '3':
  866. return MENU_MAIN;
  867. break;
  868. }
  869. return MENU_MAIN;
  870. }
  871. struct menu_display ait_update = {
  872. .title = "AIT Update Software",
  873. .timeout = 0,
  874. .id = MENU_UPDATE,
  875. .menulist = menu_update,
  876. .menu_evaluate = ait_menu_evaluate_update,
  877. };
  878. static int ait_menu_evaluate_main(char *choice)
  879. {
  880. if (!choice)
  881. return MENU_STAY;
  882. menu_start = 1;
  883. switch (choice[1]) {
  884. case '1':
  885. /* run bootcmd */
  886. return MENU_EXIT_BOOTCMD;
  887. break;
  888. case '2':
  889. return ait_menu_show(&ait_update, 0);
  890. break;
  891. case '3':
  892. /* reset to default settings */
  893. setenv("app_reset", "yes");
  894. return MENU_EXIT_BOOTCMD;
  895. break;
  896. case '4':
  897. /* u-boot shell */
  898. return MENU_EXIT;
  899. break;
  900. }
  901. return MENU_EXIT;
  902. }
  903. struct menu_display ait_main = {
  904. .title = "AIT Main",
  905. .timeout = CONFIG_BOOTDELAY,
  906. .id = MENU_MAIN,
  907. .menulist = menu_main,
  908. .menu_evaluate = ait_menu_evaluate_main,
  909. };
  910. int menu_show(int bootdelay)
  911. {
  912. int ret;
  913. run_command2("run saveparms", 0);
  914. ret = ait_menu_show(&ait_main, bootdelay);
  915. run_command2("run restoreparms", 0);
  916. if (ret == MENU_EXIT_BOOTCMD)
  917. return 0;
  918. return MENU_EXIT;
  919. }
  920. void menu_display_statusline(struct menu *m)
  921. {
  922. printf("State: dvn_boot_vers: %s dvn_app_vers: %s\n",
  923. getenv("dvn_boot_vers"), getenv("dvn_app_vers"));
  924. return;
  925. }
  926. #endif