cam_enc_4xx.c 23 KB

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