]> 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-microblaze
[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 int enbw_cmc_config_switch(unsigned long addr)
455 {
456         struct spi_slave *spi;
457         char *ptr = (char *)addr;
458         int value, reg;
459         int ret;
460         int bus, cs, max_hz, spi_mode;
461
462         debug("configure switch with file on addr: 0x%lx\n", addr);
463
464         bus = 0;
465         cs = 0;
466         max_hz = 1000000;
467         spi_mode = 0;
468
469         spi = spi_setup_slave(bus, cs, max_hz, spi_mode);
470         if (!spi) {
471                 printf("Failed to set up slave\n");
472                 return -EINVAL;
473         }
474
475         ret = spi_claim_bus(spi);
476         if (ret) {
477                 debug("Failed to claim SPI bus: %d\n", ret);
478                 goto err_claim_bus;
479         }
480
481         ret = enbw_cmc_switch_read_ident(spi);
482         if (ret)
483                 goto err_claim_bus;
484
485         ptr = (char *)addr;
486         while (ptr != NULL) {
487                 ptr = enbw_cmc_getvalue(ptr, &reg);
488                 if (ptr != NULL) {
489                         ptr = enbw_cmc_getvalue(ptr, &value);
490                         if ((ptr != NULL) && (value >= 0))
491                                 if (enbw_cmc_switch_write(spi, reg, value))
492                                         goto err_read;
493                 }
494         }
495         return 0;
496
497 err_read:
498         spi_release_bus(spi);
499 err_claim_bus:
500         spi_free_slave(spi);
501         return -EINVAL;
502 }
503
504 static int do_switch(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
505 {
506         unsigned long addr;
507
508         if (argc < 2)
509                 return cmd_usage(cmdtp);
510
511         addr = simple_strtoul(argv[1], NULL, 16);
512         enbw_cmc_config_switch(addr);
513
514         return 0;
515 }
516
517 U_BOOT_CMD(switch, 3, 1, do_switch,
518         "switch addr",
519         "[addr]"
520 );
521
522 /*
523  * Initializes on-board ethernet controllers.
524  */
525 int board_eth_init(bd_t *bis)
526 {
527         const char *s;
528         size_t len;
529
530         davinci_emac_mii_mode_sel(0);
531
532         /* send a config file to the switch */
533         s = hwconfig_subarg("switch", "config", &len);
534         if (len) {
535                 unsigned long addr = simple_strtoul(s, NULL, 16);
536
537                 enbw_cmc_config_switch(addr);
538         }
539
540         if (!davinci_emac_initialize()) {
541                 printf("Error: Ethernet init failed!\n");
542                 return -1;
543         }
544
545         if (hwconfig_subarg_cmp("switch", "lan", "on"))
546                 /* Switch port lan on */
547                 enbw_cmc_switch(1, 1);
548         else
549                 enbw_cmc_switch(1, 0);
550
551         if (hwconfig_subarg_cmp("switch", "pwl", "on"))
552                 /* Switch port pwl on */
553                 enbw_cmc_switch(2, 1);
554         else
555                 enbw_cmc_switch(2, 0);
556
557         return 0;
558 }
559 #endif /* CONFIG_DRIVER_TI_EMAC */
560
561 #ifdef CONFIG_PREBOOT
562 static uchar kbd_magic_prefix[]         = "key_magic_";
563 static uchar kbd_command_prefix[]       = "key_cmd_";
564
565 struct kbd_data_t {
566         char s1;
567 };
568
569 struct kbd_data_t *get_keys(struct kbd_data_t *kbd_data)
570 {
571         /* read SW1 + SW2 */
572         kbd_data->s1 = gpio_get_value(12) +
573                 (gpio_get_value(13) << 1);
574         return kbd_data;
575 }
576
577 static int compare_magic(const struct kbd_data_t *kbd_data, char *str)
578 {
579         char s1 = str[0];
580
581         if (s1 >= '0' && s1 <= '9')
582                 s1 -= '0';
583         else if (s1 >= 'a' && s1 <= 'f')
584                 s1 = s1 - 'a' + 10;
585         else if (s1 >= 'A' && s1 <= 'F')
586                 s1 = s1 - 'A' + 10;
587         else
588                 return -1;
589
590         if (s1 != kbd_data->s1)
591                 return -1;
592
593         return 0;
594 }
595
596 static char *key_match(const struct kbd_data_t *kbd_data)
597 {
598         char magic[sizeof(kbd_magic_prefix) + 1];
599         char *suffix;
600         char *kbd_magic_keys;
601
602         /*
603          * The following string defines the characters that can be appended
604          * to "key_magic" to form the names of environment variables that
605          * hold "magic" key codes, i. e. such key codes that can cause
606          * pre-boot actions. If the string is empty (""), then only
607          * "key_magic" is checked (old behaviour); the string "125" causes
608          * checks for "key_magic1", "key_magic2" and "key_magic5", etc.
609          */
610         kbd_magic_keys = getenv("magic_keys");
611         if (kbd_magic_keys == NULL)
612                 kbd_magic_keys = "";
613
614         /*
615          * loop over all magic keys;
616          * use '\0' suffix in case of empty string
617          */
618         for (suffix = kbd_magic_keys; *suffix ||
619                 suffix == kbd_magic_keys; ++suffix) {
620                 sprintf(magic, "%s%c", kbd_magic_prefix, *suffix);
621
622                 if (compare_magic(kbd_data, getenv(magic)) == 0) {
623                         char cmd_name[sizeof(kbd_command_prefix) + 1];
624                         char *cmd;
625
626                         sprintf(cmd_name, "%s%c", kbd_command_prefix, *suffix);
627                         cmd = getenv(cmd_name);
628
629                         return cmd;
630                 }
631         }
632
633         return NULL;
634 }
635 #endif /* CONFIG_PREBOOT */
636
637 int misc_init_r(void)
638 {
639         char *s, buf[32];
640 #ifdef CONFIG_PREBOOT
641         struct kbd_data_t kbd_data;
642         /* Decode keys */
643         char *str = strdup(key_match(get_keys(&kbd_data)));
644         /* Set or delete definition */
645         setenv("preboot", str);
646         free(str);
647 #endif /* CONFIG_PREBOOT */
648
649         /* count all restarts, and save this in an environment var */
650         s = getenv("restartcount");
651
652         if (s)
653                 sprintf(buf, "%ld", simple_strtoul(s, NULL, 10) + 1);
654         else
655                 strcpy(buf, "1");
656
657         setenv("restartcount", buf);
658         saveenv();
659
660 #ifdef CONFIG_HW_WATCHDOG
661         davinci_hw_watchdog_enable();
662 #endif
663
664         return 0;
665 }
666
667 struct cmc_led {
668         char name[20];
669         unsigned char bank;
670         unsigned char gpio;
671 };
672
673 struct cmc_led led_table[] = {
674         {"led1", 1, 15},
675         {"led2", 0, 1},
676         {"led3", 0, 2},
677         {"led4", 0, 3},
678         {"led5", 0, 4},
679         {"led6", 0, 5},
680         {"led7", 0, 6},
681         {"led8", 0, 14},
682 };
683
684 static int cmc_get_led_state(struct cmc_led *led)
685 {
686         int value;
687         int gpio = led->bank * 16 + led->gpio;
688
689         value = gpio_get_value(gpio);
690
691         return value;
692 }
693
694 static int cmc_set_led_state(struct cmc_led *led, int state)
695 {
696         int gpio = led->bank * 16 + led->gpio;
697
698         gpio_set_value(gpio, state);
699         return 0;
700 }
701
702 static int do_led(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
703 {
704         struct cmc_led *led;
705         int found = 0;
706         int i = 0;
707         int only_print = 0;
708         int len = ARRAY_SIZE(led_table);
709
710         if (argc < 2)
711                 return cmd_usage(cmdtp);
712
713         if (argc < 3)
714                 only_print = 1;
715
716         led = led_table;
717         while ((!found) && (i < len)) {
718                 if (strcmp(argv[1], led->name) == 0) {
719                         found = 1;
720                 } else {
721                         led++;
722                         i++;
723                 }
724         }
725         if (!found)
726                 return cmd_usage(cmdtp);
727
728         if (only_print) {
729                 if (cmc_get_led_state(led))
730                         printf("on\n");
731                 else
732                         printf("off\n");
733
734                 return 0;
735         }
736         if (strcmp(argv[2], "on") == 0)
737                 cmc_set_led_state(led, 1);
738         else
739                 cmc_set_led_state(led, 0);
740
741         return 0;
742 }
743
744 U_BOOT_CMD(led, 3, 1, do_led,
745         "switch on/off board led",
746         "[name] [on/off]"
747 );
748
749 #ifdef CONFIG_HW_WATCHDOG
750 void hw_watchdog_reset(void)
751 {
752         davinci_hw_watchdog_reset();
753 }
754 #endif
755
756 #if defined(CONFIG_POST)
757 void arch_memory_failure_handle(void)
758 {
759         struct davinci_gpio *gpio = davinci_gpio_bank01;
760         int state = 1;
761
762         /*
763          * if memor< failure blink with the LED 1,2 and 3
764          * as we running from flash, we cannot use the gpio
765          * api here, so access the gpio pin direct through
766          * the gpio register.
767          */
768         while (1) {
769                 if (state) {
770                         clrbits_le32(&gpio->out_data, 0x80000006);
771                         state = 0;
772                 } else {
773                         setbits_le32(&gpio->out_data, 0x80000006);
774                         state = 1;
775                 }
776                 udelay(500);
777         }
778 }
779 #endif
780
781 #if defined(CONFIG_BOOTCOUNT_LIMIT)
782 void bootcount_store(ulong a)
783 {
784         struct davinci_rtc *reg =
785                 (struct davinci_rtc *)CONFIG_SYS_BOOTCOUNT_ADDR;
786
787         /*
788          * write RTC kick register to enable write
789          * for RTC Scratch registers. Scratch0 and 1 are
790          * used for bootcount values.
791          */
792         writel(RTC_KICK0R_WE, &reg->kick0r);
793         writel(RTC_KICK1R_WE, &reg->kick1r);
794         out_be32(&reg->scratch0, a);
795         out_be32(&reg->scratch1, BOOTCOUNT_MAGIC);
796 }
797
798 ulong bootcount_load(void)
799 {
800         struct davinci_rtc *reg =
801                 (struct davinci_rtc *)CONFIG_SYS_BOOTCOUNT_ADDR;
802
803         if (in_be32(&reg->scratch1) != BOOTCOUNT_MAGIC)
804                 return 0;
805         else
806                 return in_be32(&reg->scratch0);
807 }
808 #endif
809
810 ulong post_word_load(void)
811 {
812         struct davinci_rtc *reg =
813                 (struct davinci_rtc *)CONFIG_SYS_POST_WORD_ADDR;
814
815         return in_be32(&reg->scratch2);
816 }
817
818 void post_word_store(ulong value)
819 {
820         struct davinci_rtc *reg =
821                 (struct davinci_rtc *)CONFIG_SYS_POST_WORD_ADDR;
822
823         /*
824          * write RTC kick register to enable write
825          * for RTC Scratch registers. Cratch0 and 1 are
826          * used for bootcount values.
827          */
828         writel(RTC_KICK0R_WE, &reg->kick0r);
829         writel(RTC_KICK1R_WE, &reg->kick1r);
830         out_be32(&reg->scratch2, value);
831 }
832
833 void board_gpio_init(void)
834 {
835         struct davinci_gpio *gpio = davinci_gpio_bank01;
836
837         /*
838          * set LED (gpio Interface not usable here)
839          * set LED pins to output and state 0
840          */
841         clrbits_le32(&gpio->dir, 0x8000407e);
842         clrbits_le32(&gpio->out_data, 0x8000407e);
843         /* set LED 1 - 5 to state on */
844         setbits_le32(&gpio->out_data, 0x8000001e);
845
846         /*
847          * set some gpio pins to low, this is needed early,
848          * so we have no gpio Interface here
849          * gpios:
850          * 8[8]  Mode PV select  low
851          * 8[9]  Debug Rx Enable low
852          * 8[10] Mode Select PV  low
853          * 8[11] Counter Interface RS485 Rx-Enable low
854          */
855         gpio = davinci_gpio_bank8;
856         clrbits_le32(&gpio->dir, 0x00000f00);
857         clrbits_le32(&gpio->out_data, 0x0f00);
858 }
859
860 int board_late_init(void)
861 {
862         cmc_set_led_state(&led_table[4], 0);
863
864         return 0;
865 }
866
867 void show_boot_progress(int val)
868 {
869         switch (val) {
870         case 1:
871                 cmc_set_led_state(&led_table[4], 1);
872                 break;
873         case 4:
874                 cmc_set_led_state(&led_table[4], 0);
875                 break;
876         case 15:
877                 cmc_set_led_state(&led_table[4], 1);
878                 break;
879         }
880 }
881
882 #ifdef CONFIG_DAVINCI_MMC
883 static struct davinci_mmc mmc_sd1 = {
884         .reg_base       = (struct davinci_mmc_regs *)DAVINCI_MMC_SD1_BASE,
885         .input_clk      = 228000000,
886         .host_caps      = MMC_MODE_4BIT,
887         .voltages       = MMC_VDD_32_33 | MMC_VDD_33_34,
888         .version        = MMC_CTLR_VERSION_2,
889 };
890
891 int board_mmc_init(bd_t *bis)
892 {
893         mmc_sd1.input_clk = clk_get(DAVINCI_MMC_CLKID);
894         /* Add slot-0 to mmc subsystem */
895         return davinci_mmc_init(bis, &mmc_sd1);
896 }
897 #endif