]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - board/enbw/enbw_cmc/enbw_cmc.c
Merge branch 'master' of git://git.denx.de/u-boot-mips
[karo-tx-uboot.git] / board / enbw / enbw_cmc / enbw_cmc.c
1 /*
2  * (C) Copyright 2011
3  * Heiko Schocher, DENX Software Engineering, hs@denx.de.
4  *
5  * Based on:
6  * Copyright (C) 2010 Texas Instruments Incorporated - http://www.ti.com/
7  *
8  * Based on da830evm.c. Original Copyrights follow:
9  *
10  * Copyright (C) 2009 Nick Thompson, GE Fanuc, Ltd. <nick.thompson@gefanuc.com>
11  * Copyright (C) 2007 Sergey Kubushyn <ksi@koi8.net>
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26  */
27
28 #include <common.h>
29 #include <command.h>
30 #include <environment.h>
31 #include <hwconfig.h>
32 #include <i2c.h>
33 #include <malloc.h>
34 #include <miiphy.h>
35 #include <mmc.h>
36 #include <net.h>
37 #include <netdev.h>
38 #include <spi.h>
39 #include <linux/ctype.h>
40 #include <asm/gpio.h>
41 #include <asm/io.h>
42 #include <asm/arch/da850_lowlevel.h>
43 #include <asm/arch/davinci_misc.h>
44 #include <asm/arch/emif_defs.h>
45 #include <asm/arch/emac_defs.h>
46 #include <asm/arch/gpio.h>
47 #include <asm/arch/pinmux_defs.h>
48 #include <asm/arch/hardware.h>
49 #include <asm/arch/sdmmc_defs.h>
50 #include <asm/arch/timer_defs.h>
51
52 DECLARE_GLOBAL_DATA_PTR;
53
54 const struct lpsc_resource lpsc[] = {
55         { DAVINCI_LPSC_AEMIF },
56         { DAVINCI_LPSC_SPI1 },
57         { DAVINCI_LPSC_ARM_RAM_ROM },
58         { DAVINCI_LPSC_UART0 },
59         { DAVINCI_LPSC_EMAC },
60         { DAVINCI_LPSC_UART0 },
61         { DAVINCI_LPSC_GPIO },
62         { DAVINCI_LPSC_DDR_EMIF },
63         { DAVINCI_LPSC_UART1 },
64         { DAVINCI_LPSC_UART2 },
65         { DAVINCI_LPSC_MMC_SD1 },
66         { DAVINCI_LPSC_USB20 },
67         { DAVINCI_LPSC_USB11 },
68 };
69
70 const int lpsc_size = ARRAY_SIZE(lpsc);
71
72 static const struct pinmux_config enbw_pins[] = {
73         { pinmux(0), 8, 0 },
74         { pinmux(0), 8, 1 },
75         { pinmux(0), 8, 2 },
76         { pinmux(0), 8, 3 },
77         { pinmux(0), 8, 4 },
78         { pinmux(0), 8, 5 },
79         { pinmux(1), 4, 0 },
80         { pinmux(1), 8, 1 },
81         { pinmux(1), 8, 2 },
82         { pinmux(1), 8, 3 },
83         { pinmux(1), 8, 4 },
84         { pinmux(1), 8, 5 },
85         { pinmux(1), 8, 6 },
86         { pinmux(1), 4, 7 },
87         { pinmux(2), 8, 0 },
88         { pinmux(5), 1, 0 },
89         { pinmux(5), 1, 3 },
90         { pinmux(5), 1, 7 },
91         { pinmux(5), 1, 5 },
92         { pinmux(5), 1, 4 },
93         { pinmux(5), 1, 3 },
94         { pinmux(5), 1, 2 },
95         { pinmux(5), 1, 1 },
96         { pinmux(5), 1, 0 },
97         { pinmux(6), 8, 0 },
98         { pinmux(6), 8, 1 },
99         { pinmux(6), 8, 2 },
100         { pinmux(6), 8, 3 },
101         { pinmux(6), 8, 4 },
102         { pinmux(6), 8, 5 },
103         { pinmux(6), 1, 7 },
104         { pinmux(7), 8, 2 },
105         { pinmux(7), 1, 3 },
106         { pinmux(7), 8, 6 },
107         { pinmux(7), 1, 7 },
108         { pinmux(13), 8, 2 },
109         { pinmux(13), 8, 3 },
110         { pinmux(13), 8, 4 },
111         { pinmux(13), 8, 5 },
112         { pinmux(13), 8, 6 },
113         { pinmux(13), 8, 7 },
114         { pinmux(14), 8, 0 },
115         { pinmux(14), 8, 1 },
116         { pinmux(16), 8, 1 },
117         { pinmux(16), 8, 2 },
118         { pinmux(16), 8, 3 },
119         { pinmux(16), 8, 4 },
120         { pinmux(16), 8, 5 },
121         { pinmux(16), 8, 6 },
122         { pinmux(16), 8, 7 },
123         { pinmux(17), 1, 0 },
124         { pinmux(17), 1, 1 },
125         { pinmux(17), 1, 2 },
126         { pinmux(17), 8, 3 },
127         { pinmux(17), 8, 4 },
128         { pinmux(17), 8, 5 },
129         { pinmux(17), 8, 6 },
130         { pinmux(17), 8, 7 },
131         { pinmux(18), 8, 0 },
132         { pinmux(18), 8, 1 },
133         { pinmux(18), 2, 2 },
134         { pinmux(18), 2, 3 },
135         { pinmux(18), 2, 4 },
136         { pinmux(18), 8, 6 },
137         { pinmux(18), 8, 7 },
138         { pinmux(19), 8, 0 },
139         { pinmux(19), 2, 1 },
140         { pinmux(19), 2, 2 },
141         { pinmux(19), 2, 3 },
142         { pinmux(19), 2, 4 },
143         { pinmux(19), 8, 5 },
144         { pinmux(19), 8, 6 },
145 };
146
147 const struct pinmux_resource pinmuxes[] = {
148         PINMUX_ITEM(emac_pins_mii),
149         PINMUX_ITEM(emac_pins_mdio),
150         PINMUX_ITEM(i2c0_pins),
151         PINMUX_ITEM(emifa_pins_cs2),
152         PINMUX_ITEM(emifa_pins_cs3),
153         PINMUX_ITEM(emifa_pins_cs4),
154         PINMUX_ITEM(emifa_pins_nand),
155         PINMUX_ITEM(emifa_pins_nor),
156         PINMUX_ITEM(spi1_pins_base),
157         PINMUX_ITEM(spi1_pins_scs0),
158         PINMUX_ITEM(uart1_pins_txrx),
159         PINMUX_ITEM(uart2_pins_txrx),
160         PINMUX_ITEM(uart2_pins_rtscts),
161         PINMUX_ITEM(enbw_pins),
162 };
163
164 const int pinmuxes_size = ARRAY_SIZE(pinmuxes);
165
166 struct gpio_config {
167         char name[GPIO_NAME_SIZE];
168         unsigned char bank;
169         unsigned char gpio;
170         unsigned char out;
171         unsigned char value;
172 };
173
174 static const struct gpio_config enbw_gpio_config_hut[] = {
175         { "RS485 enable",       8, 11, 1, 0 },
176         { "RS485 iso",          8, 10, 1, 1 },
177         { "W2HUT RS485 Rx ena", 8,  9, 1, 0 },
178         { "W2HUT RS485 iso",    8,  8, 1, 1 },
179 };
180
181 static const struct gpio_config enbw_gpio_config_w[] = {
182         { "RS485 enable",       8, 11, 1, 0 },
183         { "RS485 iso",          8, 10, 1, 0 },
184         { "W2HUT RS485 Rx ena", 8,  9, 1, 0 },
185         { "W2HUT RS485 iso",    8,  8, 1, 0 },
186 };
187
188 static const struct gpio_config enbw_gpio_config[] = {
189         { "LAN reset",          7, 15, 1, 1 },
190         { "ena 11V PLC",        7, 14, 1, 0 },
191         { "ena 1.5V PLC",       7, 13, 1, 0 },
192         { "disable VBUS",       7, 12, 1, 1 },
193         { "PLC reset",          6, 13, 1, 0 },
194         { "LCM RS",             6, 12, 1, 0 },
195         { "LCM R/W",            6, 11, 1, 0 },
196         { "PLC pairing",        6, 10, 1, 1 },
197         { "PLC MDIO CLK",       6,  9, 1, 0 },
198         { "HK218",              6,  8, 1, 0 },
199         { "HK218 Rx",           6,  1, 1, 1 },
200         { "TPM reset",          6,  0, 1, 0 },
201         { "Board-Type",         3,  9, 0, 0 },
202         { "HW-ID0",             2,  7, 0, 0 },
203         { "HW-ID1",             2,  6, 0, 0 },
204         { "HW-ID2",             2,  3, 0, 0 },
205         { "PV-IF RxD ena",      0, 15, 1, 1 },
206         { "LED1",               1, 15, 1, 1 },
207         { "LED2",               0,  1, 1, 1 },
208         { "LED3",               0,  2, 1, 1 },
209         { "LED4",               0,  3, 1, 1 },
210         { "LED5",               0,  4, 1, 1 },
211         { "LED6",               0,  5, 1, 0 },
212         { "LED7",               0,  6, 1, 0 },
213         { "LED8",               0, 14, 1, 0 },
214         { "USER1",              0, 12, 0, 0 },
215         { "USER2",              0, 13, 0, 0 },
216 };
217
218 #define PHY_POWER       0x0800
219
220 static void enbw_cmc_switch(int port, int on)
221 {
222         const char      *devname;
223         unsigned char phyaddr = 3;
224         unsigned char   reg = 0;
225         unsigned short  data;
226
227         if (port == 1)
228                 phyaddr = 2;
229
230         devname = miiphy_get_current_dev();
231         if (!devname) {
232                 printf("Error: no mii device\n");
233                 return;
234         }
235         if (miiphy_read(devname, phyaddr, reg, &data) != 0) {
236                 printf("Error reading from the PHY addr=%02x reg=%02x\n",
237                         phyaddr, reg);
238                 return;
239         }
240
241         if (on)
242                 data &= ~PHY_POWER;
243         else
244                 data |= PHY_POWER;
245
246         if (miiphy_write(devname, phyaddr, reg, data) != 0) {
247                 printf("Error writing to the PHY addr=%02x reg=%02x\n",
248                         phyaddr, reg);
249                 return;
250         }
251 }
252
253 static int enbw_cmc_init_gpio(const struct gpio_config *conf, int sz)
254 {
255         int i, ret;
256
257         for (i = 0; i < sz; i++) {
258                 int gpio = conf[i].bank * 16 +
259                         conf[i].gpio;
260
261                 ret = gpio_request(gpio, conf[i].name);
262                 if (ret) {
263                         printf("%s: Could not get %s gpio\n", __func__,
264                                 conf[i].name);
265                         return ret;
266                 }
267
268                 if (conf[i].out)
269                         gpio_direction_output(gpio,
270                                 conf[i].value);
271                 else
272                         gpio_direction_input(gpio);
273         }
274
275         return 0;
276 }
277
278 int board_init(void)
279 {
280         int board_type, hw_id;
281
282 #ifndef CONFIG_USE_IRQ
283         irq_init();
284 #endif
285         /* address of boot parameters, not used as booting with DTT */
286         gd->bd->bi_boot_params = 0;
287
288         enbw_cmc_init_gpio(enbw_gpio_config, ARRAY_SIZE(enbw_gpio_config));
289
290         /* detect HW version */
291         board_type = gpio_get_value(CONFIG_ENBW_CMC_BOARD_TYPE);
292         hw_id = gpio_get_value(CONFIG_ENBW_CMC_HW_ID_BIT0) +
293                 (gpio_get_value(CONFIG_ENBW_CMC_HW_ID_BIT1) << 1) +
294                 (gpio_get_value(CONFIG_ENBW_CMC_HW_ID_BIT2) << 2);
295         printf("BOARD: CMC-%s hw id: %d\n", (board_type ? "w2" : "hut"),
296                 hw_id);
297         if (board_type)
298                 enbw_cmc_init_gpio(enbw_gpio_config_w,
299                         ARRAY_SIZE(enbw_gpio_config_w));
300         else
301                 enbw_cmc_init_gpio(enbw_gpio_config_hut,
302                         ARRAY_SIZE(enbw_gpio_config_hut));
303
304         /* setup the SUSPSRC for ARM to control emulation suspend */
305         clrbits_le32(&davinci_syscfg_regs->suspsrc,
306                 (DAVINCI_SYSCFG_SUSPSRC_EMAC | DAVINCI_SYSCFG_SUSPSRC_I2C |
307                 DAVINCI_SYSCFG_SUSPSRC_SPI1 | DAVINCI_SYSCFG_SUSPSRC_TIMER0 |
308                 DAVINCI_SYSCFG_SUSPSRC_UART2));
309
310         return 0;
311 }
312
313 #ifdef CONFIG_DRIVER_TI_EMAC
314
315 #define KSZ_CMD_READ    0x03
316 #define KSZ_CMD_WRITE   0x02
317 #define KSZ_ID          0x95
318
319 static int enbw_cmc_switch_read(struct spi_slave *spi, u8 reg, u8 *val)
320 {
321         unsigned long flags = SPI_XFER_BEGIN;
322         int ret;
323         int cmd_len;
324         u8 cmd[2];
325
326         cmd[0] = KSZ_CMD_READ;
327         cmd[1] = reg;
328         cmd_len = 2;
329
330         ret = spi_xfer(spi, cmd_len * 8, cmd, NULL, flags);
331         if (ret) {
332                 debug("Failed to send command (%zu bytes): %d\n",
333                                 cmd_len, ret);
334                 return -EINVAL;
335         }
336         flags |= SPI_XFER_END;
337         *val = 0;
338         cmd_len = 1;
339         ret = spi_xfer(spi, cmd_len * 8, NULL, val, flags);
340         if (ret) {
341                 debug("Failed to read (%zu bytes): %d\n",
342                                 cmd_len, ret);
343                 return -EINVAL;
344         }
345
346         return 0;
347 }
348
349 static int enbw_cmc_switch_read_ident(struct spi_slave *spi)
350 {
351         int ret;
352         u8 val;
353
354         ret = enbw_cmc_switch_read(spi, 0, &val);
355         if (ret) {
356                 debug("Failed to read\n");
357                 return -EINVAL;
358         }
359
360         if (val != KSZ_ID)
361                 return -EINVAL;
362
363         return 0;
364 }
365
366 static int enbw_cmc_switch_write(struct spi_slave *spi, unsigned long reg,
367                 unsigned long val)
368 {
369         unsigned long flags = SPI_XFER_BEGIN;
370         int ret;
371         int cmd_len;
372         u8 cmd[3];
373
374         cmd[0] = KSZ_CMD_WRITE;
375         cmd[1] = reg;
376         cmd[2] = val;
377         cmd_len = 3;
378         flags |= SPI_XFER_END;
379
380         ret = spi_xfer(spi, cmd_len * 8, cmd, NULL, flags);
381         if (ret) {
382                 debug("Failed to send command (%zu bytes): %d\n",
383                                 cmd_len, ret);
384                 return -EINVAL;
385         }
386
387         udelay(1000);
388         ret = enbw_cmc_switch_read(spi, reg, &cmd[0]);
389         if (ret) {
390                 debug("Failed to read\n");
391                 return -EINVAL;
392         }
393         if (val != cmd[0])
394                 debug("warning: reg: %lx va: %x soll: %lx\n",
395                         reg, cmd[0], val);
396
397         return 0;
398 }
399
400 static int enbw_cmc_eof(unsigned char *ptr)
401 {
402         if (*ptr == 0xff)
403                 return 1;
404
405         return 0;
406 }
407
408 static char *enbw_cmc_getnewline(char *ptr)
409 {
410         while (*ptr != 0x0a) {
411                 ptr++;
412                 if (enbw_cmc_eof((unsigned char *)ptr))
413                         return NULL;
414         }
415
416         ptr++;
417         return ptr;
418 }
419
420 static char *enbw_cmc_getvalue(char *ptr, int *value)
421 {
422         int     end = 0;
423
424         *value = -EINVAL;
425
426         if (!isxdigit(*ptr))
427                 end = 1;
428
429         while (end) {
430                 if ((*ptr == '#') || (*ptr == ';')) {
431                         ptr = enbw_cmc_getnewline(ptr);
432                         return ptr;
433                 }
434                 if (ptr != NULL) {
435                         if (isxdigit(*ptr)) {
436                                 end = 0;
437                         } else if (*ptr == 0x0a) {
438                                 ptr++;
439                                 return ptr;
440                         } else {
441                                 ptr++;
442                                 if (enbw_cmc_eof((unsigned char *)ptr))
443                                         return NULL;
444                         }
445                 } else {
446                         return NULL;
447                 }
448         }
449         *value = (int)simple_strtoul((const char *)ptr, &ptr, 16);
450         ptr++;
451         return ptr;
452 }
453
454 static struct spi_slave *enbw_cmc_init_spi(void)
455 {
456         struct spi_slave *spi;
457         int ret;
458
459         spi = spi_setup_slave(0, 0, 1000000, 0);
460         if (!spi) {
461                 printf("Failed to set up slave\n");
462                 return NULL;
463         }
464
465         ret = spi_claim_bus(spi);
466         if (ret) {
467                 debug("Failed to claim SPI bus: %d\n", ret);
468                 goto err_claim_bus;
469         }
470
471         ret = enbw_cmc_switch_read_ident(spi);
472         if (ret)
473                 goto err_read;
474
475         return spi;
476 err_read:
477         spi_release_bus(spi);
478 err_claim_bus:
479         spi_free_slave(spi);
480         return NULL;
481 }
482
483 static int enbw_cmc_config_switch(unsigned long addr)
484 {
485         struct spi_slave *spi;
486         char *ptr = (char *)addr;
487         int value, reg;
488         int ret = 0;
489
490         debug("configure switch with file on addr: 0x%lx\n", addr);
491
492         spi = enbw_cmc_init_spi();
493         if (!spi)
494                 return -EINVAL;
495
496         while (ptr != NULL) {
497                 ptr = enbw_cmc_getvalue(ptr, &reg);
498                 if (ptr != NULL) {
499                         ptr = enbw_cmc_getvalue(ptr, &value);
500                         if ((ptr != NULL) && (value >= 0))
501                                 if (enbw_cmc_switch_write(spi, reg, value)) {
502                                         /* error writing to switch */
503                                         ptr = NULL;
504                                         ret = -EINVAL;
505                                 }
506                 }
507         }
508
509         spi_release_bus(spi);
510         spi_free_slave(spi);
511         return ret;
512 }
513
514 static int do_switch(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
515 {
516         unsigned long addr;
517
518         if (argc < 2)
519                 return cmd_usage(cmdtp);
520
521         addr = simple_strtoul(argv[1], NULL, 16);
522         enbw_cmc_config_switch(addr);
523
524         return 0;
525 }
526
527 U_BOOT_CMD(switch, 3, 1, do_switch,
528         "switch addr",
529         "[addr]"
530 );
531
532 /*
533  * Initializes on-board ethernet controllers.
534  */
535 int board_eth_init(bd_t *bis)
536 {
537         struct spi_slave *spi;
538         const char *s;
539         size_t len = 0;
540         int config = 1;
541
542         davinci_emac_mii_mode_sel(0);
543
544         /* send a config file to the switch */
545         s = hwconfig_subarg("switch", "config", &len);
546         if (len) {
547                 unsigned long addr = simple_strtoul(s, NULL, 16);
548
549                 config = enbw_cmc_config_switch(addr);
550         }
551
552         if (config) {
553                 /*
554                  * no valid config file -> do we have some args in
555                  * hwconfig ?
556                  */
557                 if ((hwconfig_subarg("switch", "lan", &len)) ||
558                     (hwconfig_subarg("switch", "lmn", &len))) {
559                         /* If so start switch */
560                         spi = enbw_cmc_init_spi();
561                         if (spi) {
562                                 if (enbw_cmc_switch_write(spi, 1, 0))
563                                         config = 0;
564                                 udelay(10000);
565                                 if (enbw_cmc_switch_write(spi, 1, 1))
566                                         config = 0;
567                                 spi_release_bus(spi);
568                                 spi_free_slave(spi);
569                         }
570                 } else {
571                         config = 0;
572                 }
573         }
574         if (!davinci_emac_initialize()) {
575                 printf("Error: Ethernet init failed!\n");
576                 return -1;
577         }
578
579         if (config) {
580                 if (hwconfig_subarg_cmp("switch", "lan", "on"))
581                         /* Switch port lan on */
582                         enbw_cmc_switch(1, 1);
583                 else
584                         enbw_cmc_switch(1, 0);
585
586                 if (hwconfig_subarg_cmp("switch", "lmn", "on"))
587                         /* Switch port pwl on */
588                         enbw_cmc_switch(2, 1);
589                 else
590                         enbw_cmc_switch(2, 0);
591         }
592
593         return 0;
594 }
595 #endif /* CONFIG_DRIVER_TI_EMAC */
596
597 #ifdef CONFIG_PREBOOT
598 static uchar kbd_magic_prefix[]         = "key_magic_";
599 static uchar kbd_command_prefix[]       = "key_cmd_";
600
601 struct kbd_data_t {
602         char s1;
603 };
604
605 struct kbd_data_t *get_keys(struct kbd_data_t *kbd_data)
606 {
607         /* read SW1 + SW2 */
608         kbd_data->s1 = gpio_get_value(12) +
609                 (gpio_get_value(13) << 1);
610         return kbd_data;
611 }
612
613 static int compare_magic(const struct kbd_data_t *kbd_data, char *str)
614 {
615         char s1 = str[0];
616
617         if (s1 >= '0' && s1 <= '9')
618                 s1 -= '0';
619         else if (s1 >= 'a' && s1 <= 'f')
620                 s1 = s1 - 'a' + 10;
621         else if (s1 >= 'A' && s1 <= 'F')
622                 s1 = s1 - 'A' + 10;
623         else
624                 return -1;
625
626         if (s1 != kbd_data->s1)
627                 return -1;
628
629         return 0;
630 }
631
632 static char *key_match(const struct kbd_data_t *kbd_data)
633 {
634         char magic[sizeof(kbd_magic_prefix) + 1];
635         char *suffix;
636         char *kbd_magic_keys;
637
638         /*
639          * The following string defines the characters that can be appended
640          * to "key_magic" to form the names of environment variables that
641          * hold "magic" key codes, i. e. such key codes that can cause
642          * pre-boot actions. If the string is empty (""), then only
643          * "key_magic" is checked (old behaviour); the string "125" causes
644          * checks for "key_magic1", "key_magic2" and "key_magic5", etc.
645          */
646         kbd_magic_keys = getenv("magic_keys");
647         if (kbd_magic_keys == NULL)
648                 kbd_magic_keys = "";
649
650         /*
651          * loop over all magic keys;
652          * use '\0' suffix in case of empty string
653          */
654         for (suffix = kbd_magic_keys; *suffix ||
655                 suffix == kbd_magic_keys; ++suffix) {
656                 sprintf(magic, "%s%c", kbd_magic_prefix, *suffix);
657
658                 if (compare_magic(kbd_data, getenv(magic)) == 0) {
659                         char cmd_name[sizeof(kbd_command_prefix) + 1];
660                         char *cmd;
661
662                         sprintf(cmd_name, "%s%c", kbd_command_prefix, *suffix);
663                         cmd = getenv(cmd_name);
664
665                         return cmd;
666                 }
667         }
668
669         return NULL;
670 }
671 #endif /* CONFIG_PREBOOT */
672
673 int misc_init_r(void)
674 {
675         char *s, buf[32];
676 #ifdef CONFIG_PREBOOT
677         struct kbd_data_t kbd_data;
678         /* Decode keys */
679         char *str = strdup(key_match(get_keys(&kbd_data)));
680         /* Set or delete definition */
681         setenv("preboot", str);
682         free(str);
683 #endif /* CONFIG_PREBOOT */
684
685         /* count all restarts, and save this in an environment var */
686         s = getenv("restartcount");
687
688         if (s)
689                 sprintf(buf, "%ld", simple_strtoul(s, NULL, 10) + 1);
690         else
691                 strcpy(buf, "1");
692
693         setenv("restartcount", buf);
694         saveenv();
695
696 #ifdef CONFIG_HW_WATCHDOG
697         davinci_hw_watchdog_enable();
698 #endif
699
700         return 0;
701 }
702
703 struct cmc_led {
704         char name[20];
705         unsigned char bank;
706         unsigned char gpio;
707 };
708
709 struct cmc_led led_table[] = {
710         {"led1", 1, 15},
711         {"led2", 0, 1},
712         {"led3", 0, 2},
713         {"led4", 0, 3},
714         {"led5", 0, 4},
715         {"led6", 0, 5},
716         {"led7", 0, 6},
717         {"led8", 0, 14},
718 };
719
720 static int cmc_get_led_state(struct cmc_led *led)
721 {
722         int value;
723         int gpio = led->bank * 16 + led->gpio;
724
725         value = gpio_get_value(gpio);
726
727         return value;
728 }
729
730 static int cmc_set_led_state(struct cmc_led *led, int state)
731 {
732         int gpio = led->bank * 16 + led->gpio;
733
734         gpio_set_value(gpio, state);
735         return 0;
736 }
737
738 static int do_led(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
739 {
740         struct cmc_led *led;
741         int found = 0;
742         int i = 0;
743         int only_print = 0;
744         int len = ARRAY_SIZE(led_table);
745
746         if (argc < 2)
747                 return cmd_usage(cmdtp);
748
749         if (argc < 3)
750                 only_print = 1;
751
752         led = led_table;
753         while ((!found) && (i < len)) {
754                 if (strcmp(argv[1], led->name) == 0) {
755                         found = 1;
756                 } else {
757                         led++;
758                         i++;
759                 }
760         }
761         if (!found)
762                 return cmd_usage(cmdtp);
763
764         if (only_print) {
765                 if (cmc_get_led_state(led))
766                         printf("on\n");
767                 else
768                         printf("off\n");
769
770                 return 0;
771         }
772         if (strcmp(argv[2], "on") == 0)
773                 cmc_set_led_state(led, 1);
774         else
775                 cmc_set_led_state(led, 0);
776
777         return 0;
778 }
779
780 U_BOOT_CMD(led, 3, 1, do_led,
781         "switch on/off board led",
782         "[name] [on/off]"
783 );
784
785 #ifdef CONFIG_HW_WATCHDOG
786 void hw_watchdog_reset(void)
787 {
788         davinci_hw_watchdog_reset();
789 }
790 #endif
791
792 #if defined(CONFIG_POST)
793 void arch_memory_failure_handle(void)
794 {
795         struct davinci_gpio *gpio = davinci_gpio_bank01;
796         int state = 1;
797
798         /*
799          * if memor< failure blink with the LED 1,2 and 3
800          * as we running from flash, we cannot use the gpio
801          * api here, so access the gpio pin direct through
802          * the gpio register.
803          */
804         while (1) {
805                 if (state) {
806                         clrbits_le32(&gpio->out_data, 0x80000006);
807                         state = 0;
808                 } else {
809                         setbits_le32(&gpio->out_data, 0x80000006);
810                         state = 1;
811                 }
812                 udelay(500);
813         }
814 }
815 #endif
816
817 ulong post_word_load(void)
818 {
819         struct davinci_rtc *reg =
820                 (struct davinci_rtc *)CONFIG_SYS_POST_WORD_ADDR;
821
822         return in_be32(&reg->scratch2);
823 }
824
825 void post_word_store(ulong value)
826 {
827         struct davinci_rtc *reg =
828                 (struct davinci_rtc *)CONFIG_SYS_POST_WORD_ADDR;
829
830         /*
831          * write RTC kick register to enable write
832          * for RTC Scratch registers. Cratch0 and 1 are
833          * used for bootcount values.
834          */
835         writel(RTC_KICK0R_WE, &reg->kick0r);
836         writel(RTC_KICK1R_WE, &reg->kick1r);
837         out_be32(&reg->scratch2, value);
838 }
839
840 void board_gpio_init(void)
841 {
842         struct davinci_gpio *gpio = davinci_gpio_bank01;
843
844         /*
845          * set LED (gpio Interface not usable here)
846          * set LED pins to output and state 0
847          */
848         clrbits_le32(&gpio->dir, 0x8000407e);
849         clrbits_le32(&gpio->out_data, 0x8000407e);
850         /* set LED 1 - 5 to state on */
851         setbits_le32(&gpio->out_data, 0x8000001e);
852
853         /*
854          * set some gpio pins to low, this is needed early,
855          * so we have no gpio Interface here
856          * gpios:
857          * 8[8]  Mode PV select  low
858          * 8[9]  Debug Rx Enable low
859          * 8[10] Mode Select PV  low
860          * 8[11] Counter Interface RS485 Rx-Enable low
861          */
862         gpio = davinci_gpio_bank8;
863         clrbits_le32(&gpio->dir, 0x00000f00);
864         clrbits_le32(&gpio->out_data, 0x0f00);
865 }
866
867 int board_late_init(void)
868 {
869         cmc_set_led_state(&led_table[4], 0);
870
871         return 0;
872 }
873
874 void show_boot_progress(int val)
875 {
876         switch (val) {
877         case 1:
878                 cmc_set_led_state(&led_table[4], 1);
879                 break;
880         case 4:
881                 cmc_set_led_state(&led_table[4], 0);
882                 break;
883         case 15:
884                 cmc_set_led_state(&led_table[4], 1);
885                 break;
886         }
887 }
888
889 #ifdef CONFIG_DAVINCI_MMC
890 static struct davinci_mmc mmc_sd1 = {
891         .reg_base       = (struct davinci_mmc_regs *)DAVINCI_MMC_SD1_BASE,
892         .input_clk      = 228000000,
893         .host_caps      = MMC_MODE_4BIT,
894         .voltages       = MMC_VDD_32_33 | MMC_VDD_33_34,
895         .version        = MMC_CTLR_VERSION_2,
896 };
897
898 int board_mmc_init(bd_t *bis)
899 {
900         mmc_sd1.input_clk = clk_get(DAVINCI_MMC_CLKID);
901         /* Add slot-0 to mmc subsystem */
902         return davinci_mmc_init(bis, &mmc_sd1);
903 }
904 #endif