cam_enc_4xx.c 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115
  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. for (i = 0; display->menulist[i]; i++) {
  466. sprintf(key, "%d", i + 1);
  467. if (display->id == MENU_NETWORK) {
  468. switch (i) {
  469. case 0:
  470. s = getenv("ipaddr");
  471. break;
  472. case 1:
  473. s = getenv("netmask");
  474. break;
  475. case 2:
  476. s = getenv("serverip");
  477. break;
  478. case 3:
  479. s = getenv("gatewayip");
  480. break;
  481. case 4:
  482. s = getenv("img_file");
  483. break;
  484. default:
  485. s = NULL;
  486. break;
  487. }
  488. if (s) {
  489. sprintf(temp[i], "%s: %s",
  490. display->menulist[i], s);
  491. ret = menu_item_add(m, key, temp[i]);
  492. } else {
  493. ret = menu_item_add(m, key,
  494. display->menulist[i]);
  495. }
  496. } else {
  497. ret = menu_item_add(m, key, display->menulist[i]);
  498. }
  499. if (ret != 1) {
  500. printf("failed to add item!");
  501. menu_destroy(m);
  502. return NULL;
  503. }
  504. }
  505. sprintf(key, "%d", 1);
  506. menu_default_set(m, key);
  507. if (menu_get_choice(m, &choice) != 1)
  508. debug("Problem picking a choice!\n");
  509. menu_destroy(m);
  510. return choice;
  511. }
  512. static int ait_menu_show(struct menu_display *display, int bootdelay)
  513. {
  514. int end = MENU_STAY;
  515. char *choice;
  516. if ((menu_start == 0) && (display->id == MENU_MAIN))
  517. display->timeout = bootdelay;
  518. else
  519. display->timeout = 0;
  520. while (end == MENU_STAY) {
  521. choice = menu_handle(display);
  522. if (choice)
  523. end = display->menu_evaluate(choice);
  524. if (end == display->id)
  525. end = MENU_STAY;
  526. if (display->id == MENU_MAIN) {
  527. if (menu_start == 0)
  528. end = MENU_EXIT_BOOTCMD;
  529. else
  530. display->timeout = 0;
  531. }
  532. }
  533. return end;
  534. }
  535. static int ait_writeublheader(void)
  536. {
  537. char s[20];
  538. unsigned long i;
  539. int ret;
  540. for (i = CONFIG_SYS_NAND_BLOCK_SIZE;
  541. i < CONFIG_SYS_NAND_U_BOOT_OFFS;
  542. i += CONFIG_SYS_NAND_BLOCK_SIZE) {
  543. sprintf(s, "%lx", i);
  544. ret = setenv("header_addr", s);
  545. if (ret == 0)
  546. ret = run_command("run img_writeheader", 0);
  547. if (ret != 0)
  548. break;
  549. }
  550. return ret;
  551. }
  552. static int ait_menu_install_images(void)
  553. {
  554. int ret = 0;
  555. int count = 0;
  556. char s[100];
  557. char *t;
  558. /*
  559. * possible image types:
  560. * FIT_SUBTYPE_UNKNOWN
  561. * FIT_SUBTYPE_UBL_HEADER
  562. * FIT_SUBTYPE_SPL_IMAGE
  563. * FIT_SUBTYPE_UBOOT_IMAGE
  564. * FIT_SUBTYPE_DF_ENV_IMAGE
  565. * FIT_SUBTYPE_RAMDISK_IMAGE
  566. *
  567. * use Envvariables:
  568. * img_addr_r: image start addr
  569. * header_addr: addr where to write to UBL header
  570. * img_writeheader: write ubl header to nand
  571. * img_writespl: write spl to nand
  572. * img_writeuboot: write uboot to nand
  573. * img_writedfenv: write default environment to ubi volume
  574. * img_volume: which ubi volume should be updated with img_writeramdisk
  575. * filesize: size of data for updating ubi volume
  576. * img_writeramdisk: write ramdisk to ubi volume
  577. */
  578. while (imgs[count].type != IH_TYPE_INVALID) {
  579. printf("Installing %s\n",
  580. genimg_get_type_name(imgs[count].type));
  581. sprintf(s, "%p", imgs[count].data);
  582. setenv("img_addr_r", s);
  583. sprintf(s, "%lx", (unsigned long)imgs[count].size);
  584. setenv("filesize", s);
  585. switch (imgs[count].subtype) {
  586. case FIT_SUBTYPE_DF_ENV_IMAGE:
  587. ret = run_command("run img_writedfenv", 0);
  588. break;
  589. case FIT_SUBTYPE_RAMDISK_IMAGE:
  590. t = getenv("img_volume");
  591. if (!t) {
  592. ret = setenv("img_volume", "rootfs1");
  593. } else {
  594. /* switch to other volume */
  595. if (strncmp(t, "rootfs1", 7) == 0)
  596. ret = setenv("img_volume", "rootfs2");
  597. else
  598. ret = setenv("img_volume", "rootfs1");
  599. }
  600. if (ret != 0)
  601. break;
  602. ret = run_command("run img_writeramdisk", 0);
  603. break;
  604. case FIT_SUBTYPE_SPL_IMAGE:
  605. ret = run_command("run img_writespl", 0);
  606. break;
  607. case FIT_SUBTYPE_UBL_HEADER:
  608. ret = ait_writeublheader();
  609. break;
  610. case FIT_SUBTYPE_UBOOT_IMAGE:
  611. ret = run_command("run img_writeuboot", 0);
  612. break;
  613. default:
  614. /* not supported type */
  615. break;
  616. }
  617. count++;
  618. }
  619. /* now save dvn_* and img_volume env vars to new values */
  620. if (ret == 0) {
  621. t = getenv("x_dvn_boot_vers");
  622. if (t)
  623. setenv("dvn_boot_vers", t);
  624. t = getenv("x_dvn_app_vers");
  625. if (t)
  626. setenv("dvn_boot_vers", t);
  627. setenv("x_dvn_boot_vers", NULL);
  628. setenv("x_dvn_app_vers", NULL);
  629. ret = run_command("run savenewvers", 0);
  630. }
  631. return ret;
  632. }
  633. static int ait_menu_evaluate_load(char *choice)
  634. {
  635. if (!choice)
  636. return -1;
  637. switch (choice[1]) {
  638. case '1':
  639. /* install image */
  640. ait_menu_install_images();
  641. break;
  642. case '2':
  643. /* cancel, back to main */
  644. setenv("x_dvn_boot_vers", NULL);
  645. setenv("x_dvn_app_vers", NULL);
  646. break;
  647. }
  648. return MENU_MAIN;
  649. }
  650. struct menu_display ait_load = {
  651. .title = "AIT load image",
  652. .timeout = 0,
  653. .id = MENU_LOAD,
  654. .menulist = menu_load,
  655. .menu_evaluate = ait_menu_evaluate_load,
  656. };
  657. static void ait_menu_read_env(char *name)
  658. {
  659. char output[CONFIG_SYS_CBSIZE];
  660. char cbuf[CONFIG_SYS_CBSIZE];
  661. int readret;
  662. int ret;
  663. sprintf(output, "%s old: %s value: ", name, getenv(name));
  664. memset(cbuf, 0, CONFIG_SYS_CBSIZE);
  665. readret = readline_into_buffer(output, cbuf, 0);
  666. if (readret >= 0) {
  667. ret = setenv(name, cbuf);
  668. if (ret) {
  669. printf("Error setting %s\n", name);
  670. return;
  671. }
  672. }
  673. return;
  674. }
  675. static int ait_menu_evaluate_network(char *choice)
  676. {
  677. if (!choice)
  678. return MENU_MAIN;
  679. switch (choice[1]) {
  680. case '1':
  681. ait_menu_read_env("ipaddr");
  682. break;
  683. case '2':
  684. ait_menu_read_env("netmask");
  685. break;
  686. case '3':
  687. ait_menu_read_env("serverip");
  688. break;
  689. case '4':
  690. ait_menu_read_env("gatewayip");
  691. break;
  692. case '5':
  693. ait_menu_read_env("img_file");
  694. break;
  695. case '6':
  696. return MENU_UPDATE;
  697. break;
  698. }
  699. return MENU_STAY;
  700. }
  701. struct menu_display ait_network = {
  702. .title = "AIT network settings",
  703. .timeout = 0,
  704. .id = MENU_NETWORK,
  705. .menulist = menu_network,
  706. .menu_evaluate = ait_menu_evaluate_network,
  707. };
  708. static int fit_get_subtype(const void *fit, int noffset, char **subtype)
  709. {
  710. int len;
  711. *subtype = (char *)fdt_getprop(fit, noffset, "subtype", &len);
  712. if (*subtype == NULL)
  713. return -1;
  714. return 0;
  715. }
  716. static int ait_subtype_nr(char *subtype)
  717. {
  718. int ret = FIT_SUBTYPE_UNKNOWN;
  719. if (!strncmp("ublheader", subtype, strlen("ublheader")))
  720. return FIT_SUBTYPE_UBL_HEADER;
  721. if (!strncmp("splimage", subtype, strlen("splimage")))
  722. return FIT_SUBTYPE_SPL_IMAGE;
  723. if (!strncmp("ubootimage", subtype, strlen("ubootimage")))
  724. return FIT_SUBTYPE_UBOOT_IMAGE;
  725. if (!strncmp("dfenvimage", subtype, strlen("dfenvimage")))
  726. return FIT_SUBTYPE_DF_ENV_IMAGE;
  727. return ret;
  728. }
  729. static int ait_menu_check_image(void)
  730. {
  731. char *s;
  732. unsigned long fit_addr;
  733. void *addr;
  734. int format;
  735. char *desc;
  736. char *subtype;
  737. int images_noffset;
  738. int noffset;
  739. int ndepth;
  740. int count = 0;
  741. int ret;
  742. int i;
  743. int found_uboot = -1;
  744. int found_ramdisk = -1;
  745. memset(imgs, 0, sizeof(imgs));
  746. s = getenv("fit_addr_r");
  747. fit_addr = s ? (unsigned long)simple_strtol(s, NULL, 16) : \
  748. CONFIG_BOARD_IMG_ADDR_R;
  749. addr = (void *)fit_addr;
  750. /* check if it is a FIT image */
  751. format = genimg_get_format(addr);
  752. if (format != IMAGE_FORMAT_FIT)
  753. return -EINVAL;
  754. if (!fit_check_format(addr))
  755. return -EINVAL;
  756. /* print the FIT description */
  757. ret = fit_get_desc(addr, 0, &desc);
  758. printf("FIT description: ");
  759. if (ret)
  760. printf("unavailable\n");
  761. else
  762. printf("%s\n", desc);
  763. /* find images */
  764. images_noffset = fdt_path_offset(addr, FIT_IMAGES_PATH);
  765. if (images_noffset < 0) {
  766. printf("Can't find images parent node '%s' (%s)\n",
  767. FIT_IMAGES_PATH, fdt_strerror(images_noffset));
  768. return -EINVAL;
  769. }
  770. /* Process its subnodes, print out component images details */
  771. for (ndepth = 0, count = 0,
  772. noffset = fdt_next_node(addr, images_noffset, &ndepth);
  773. (noffset >= 0) && (ndepth > 0);
  774. noffset = fdt_next_node(addr, noffset, &ndepth)) {
  775. if (ndepth == 1) {
  776. /*
  777. * Direct child node of the images parent node,
  778. * i.e. component image node.
  779. */
  780. printf("Image %u (%s)\n", count,
  781. fit_get_name(addr, noffset, NULL));
  782. fit_image_print(addr, noffset, "");
  783. fit_image_get_type(addr, noffset,
  784. &imgs[count].type);
  785. /* Mandatory properties */
  786. ret = fit_get_desc(addr, noffset, &desc);
  787. printf("Description: ");
  788. if (ret)
  789. printf("unavailable\n");
  790. else
  791. printf("%s\n", desc);
  792. ret = fit_get_subtype(addr, noffset, &subtype);
  793. printf("Subtype: ");
  794. if (ret) {
  795. printf("unavailable\n");
  796. } else {
  797. imgs[count].subtype = ait_subtype_nr(subtype);
  798. printf("%s %d\n", subtype,
  799. imgs[count].subtype);
  800. }
  801. sprintf(imgs[count].desc, "%s", desc);
  802. ret = fit_image_get_data(addr, noffset,
  803. &imgs[count].data,
  804. &imgs[count].size);
  805. printf("Data Size: ");
  806. if (ret)
  807. printf("unavailable\n");
  808. else
  809. genimg_print_size(imgs[count].size);
  810. printf("Data @ %p\n", imgs[count].data);
  811. count++;
  812. }
  813. }
  814. for (i = 0; i < count; i++) {
  815. if (imgs[i].subtype == FIT_SUBTYPE_UBOOT_IMAGE)
  816. found_uboot = i;
  817. if (imgs[i].type == IH_TYPE_RAMDISK) {
  818. found_ramdisk = i;
  819. imgs[i].subtype = FIT_SUBTYPE_RAMDISK_IMAGE;
  820. }
  821. }
  822. /* dvn_* env var update, if the FIT descriptors are different */
  823. if (found_uboot >= 0) {
  824. s = getenv("dvn_boot_vers");
  825. if (s) {
  826. ret = strcmp(s, imgs[found_uboot].desc);
  827. if (ret != 0) {
  828. setenv("x_dvn_boot_vers",
  829. imgs[found_uboot].desc);
  830. } else {
  831. found_uboot = -1;
  832. printf("no new uboot version\n");
  833. }
  834. } else {
  835. setenv("dvn_boot_vers", imgs[found_uboot].desc);
  836. }
  837. }
  838. if (found_ramdisk >= 0) {
  839. s = getenv("dvn_app_vers");
  840. if (s) {
  841. ret = strcmp(s, imgs[found_ramdisk].desc);
  842. if (ret != 0) {
  843. setenv("x_dvn_app_vers",
  844. imgs[found_ramdisk].desc);
  845. } else {
  846. found_ramdisk = -1;
  847. printf("no new ramdisk version\n");
  848. }
  849. } else {
  850. setenv("dvn_app_vers", imgs[found_ramdisk].desc);
  851. }
  852. }
  853. if ((found_uboot == -1) && (found_ramdisk == -1))
  854. return -EINVAL;
  855. return 0;
  856. }
  857. static int ait_menu_evaluate_update(char *choice)
  858. {
  859. int ret;
  860. if (!choice)
  861. return MENU_MAIN;
  862. switch (choice[1]) {
  863. case '1':
  864. return ait_menu_show(&ait_network, 0);
  865. break;
  866. case '2':
  867. /* load image */
  868. ret = run_command("run load_img", 0);
  869. printf("ret: %d\n", ret);
  870. if (ret)
  871. return MENU_UPDATE;
  872. ret = ait_menu_check_image();
  873. if (ret)
  874. return MENU_UPDATE;
  875. return ait_menu_show(&ait_load, 0);
  876. break;
  877. case '3':
  878. return MENU_MAIN;
  879. break;
  880. }
  881. return MENU_MAIN;
  882. }
  883. struct menu_display ait_update = {
  884. .title = "AIT Update Software",
  885. .timeout = 0,
  886. .id = MENU_UPDATE,
  887. .menulist = menu_update,
  888. .menu_evaluate = ait_menu_evaluate_update,
  889. };
  890. static int ait_menu_evaluate_main(char *choice)
  891. {
  892. if (!choice)
  893. return MENU_STAY;
  894. menu_start = 1;
  895. switch (choice[1]) {
  896. case '1':
  897. /* run bootcmd */
  898. return MENU_EXIT_BOOTCMD;
  899. break;
  900. case '2':
  901. return ait_menu_show(&ait_update, 0);
  902. break;
  903. case '3':
  904. /* reset to default settings */
  905. setenv("app_reset", "yes");
  906. return MENU_EXIT_BOOTCMD;
  907. break;
  908. case '4':
  909. /* u-boot shell */
  910. return MENU_EXIT;
  911. break;
  912. }
  913. return MENU_EXIT;
  914. }
  915. struct menu_display ait_main = {
  916. .title = "AIT Main",
  917. .timeout = CONFIG_BOOTDELAY,
  918. .id = MENU_MAIN,
  919. .menulist = menu_main,
  920. .menu_evaluate = ait_menu_evaluate_main,
  921. };
  922. int menu_show(int bootdelay)
  923. {
  924. int ret;
  925. run_command("run saveparms", 0);
  926. ret = ait_menu_show(&ait_main, bootdelay);
  927. run_command("run restoreparms", 0);
  928. if (ret == MENU_EXIT_BOOTCMD)
  929. return 0;
  930. return MENU_EXIT;
  931. }
  932. void menu_display_statusline(struct menu *m)
  933. {
  934. char *s1, *s2;
  935. s1 = getenv("x_dvn_boot_vers");
  936. if (!s1)
  937. s1 = getenv("dvn_boot_vers");
  938. s2 = getenv("x_dvn_app_vers");
  939. if (!s2)
  940. s2 = getenv("dvn_app_vers");
  941. printf("State: dvn_boot_vers: %s dvn_app_vers: %s\n", s1, s2);
  942. return;
  943. }
  944. #endif