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