cam_enc_4xx.c 23 KB

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