blob: 67d5d4d6e5f0e35b627f5c1b8a1b2fb1e2f9aa7f [file] [log] [blame]
Heiko Schocher27df4082011-11-29 02:33:47 +00001/*
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>
Heiko Schocher804810a2012-05-14 20:24:14 +000038#include <spi.h>
39#include <linux/ctype.h>
Heiko Schocher27df4082011-11-29 02:33:47 +000040#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
52DECLARE_GLOBAL_DATA_PTR;
53
Sughosh Ganua2616972012-02-02 00:44:41 +000054const struct lpsc_resource lpsc[] = {
Heiko Schocher27df4082011-11-29 02:33:47 +000055 { 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
Sughosh Ganua2616972012-02-02 00:44:41 +000070const int lpsc_size = ARRAY_SIZE(lpsc);
71
Heiko Schocher27df4082011-11-29 02:33:47 +000072static 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 },
Heiko Schocher804810a2012-05-14 20:24:14 +000091 { 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 },
Heiko Schocher27df4082011-11-29 02:33:47 +000099 { pinmux(6), 8, 2 },
100 { pinmux(6), 8, 3 },
Heiko Schocher804810a2012-05-14 20:24:14 +0000101 { pinmux(6), 8, 4 },
Heiko Schocher27df4082011-11-29 02:33:47 +0000102 { pinmux(6), 8, 5 },
103 { pinmux(6), 1, 7 },
104 { pinmux(7), 8, 2 },
105 { pinmux(7), 1, 3 },
Heiko Schocher804810a2012-05-14 20:24:14 +0000106 { pinmux(7), 8, 6 },
Heiko Schocher27df4082011-11-29 02:33:47 +0000107 { 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
147const 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
164const int pinmuxes_size = ARRAY_SIZE(pinmuxes);
165
166struct 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
Heiko Schocher804810a2012-05-14 20:24:14 +0000174static 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
181static const struct gpio_config enbw_gpio_config_w[] = {
Heiko Schocher27df4082011-11-29 02:33:47 +0000182 { "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 },
Heiko Schocher804810a2012-05-14 20:24:14 +0000186};
187
188static const struct gpio_config enbw_gpio_config[] = {
Heiko Schocher27df4082011-11-29 02:33:47 +0000189 { "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 },
Heiko Schocher804810a2012-05-14 20:24:14 +0000193 { "PLC reset", 6, 13, 1, 0 },
Heiko Schocher27df4082011-11-29 02:33:47 +0000194 { "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 },
Heiko Schocher804810a2012-05-14 20:24:14 +0000200 { "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 },
Heiko Schocher27df4082011-11-29 02:33:47 +0000205 { "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
220static 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
Heiko Schocher804810a2012-05-14 20:24:14 +0000253static int enbw_cmc_init_gpio(const struct gpio_config *conf, int sz)
Heiko Schocher27df4082011-11-29 02:33:47 +0000254{
255 int i, ret;
256
Heiko Schocher804810a2012-05-14 20:24:14 +0000257 for (i = 0; i < sz; i++) {
258 int gpio = conf[i].bank * 16 +
259 conf[i].gpio;
Heiko Schocher27df4082011-11-29 02:33:47 +0000260
Heiko Schocher804810a2012-05-14 20:24:14 +0000261 ret = gpio_request(gpio, conf[i].name);
Heiko Schocher27df4082011-11-29 02:33:47 +0000262 if (ret) {
263 printf("%s: Could not get %s gpio\n", __func__,
Heiko Schocher804810a2012-05-14 20:24:14 +0000264 conf[i].name);
265 return ret;
Heiko Schocher27df4082011-11-29 02:33:47 +0000266 }
267
Heiko Schocher804810a2012-05-14 20:24:14 +0000268 if (conf[i].out)
Heiko Schocher27df4082011-11-29 02:33:47 +0000269 gpio_direction_output(gpio,
Heiko Schocher804810a2012-05-14 20:24:14 +0000270 conf[i].value);
Heiko Schocher27df4082011-11-29 02:33:47 +0000271 else
272 gpio_direction_input(gpio);
273 }
274
Heiko Schocher804810a2012-05-14 20:24:14 +0000275 return 0;
276}
277
278int 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
Heiko Schocher27df4082011-11-29 02:33:47 +0000304 /* 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
Heiko Schocher804810a2012-05-14 20:24:14 +0000314
315#define KSZ_CMD_READ 0x03
316#define KSZ_CMD_WRITE 0x02
317#define KSZ_ID 0x95
318
319static 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
349static 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
366static 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
400static int enbw_cmc_eof(unsigned char *ptr)
401{
402 if (*ptr == 0xff)
403 return 1;
404
405 return 0;
406}
407
408static 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
420static 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
454static 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
497err_read:
498 spi_release_bus(spi);
499err_claim_bus:
500 spi_free_slave(spi);
501 return -EINVAL;
502}
503
504static 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
517U_BOOT_CMD(switch, 3, 1, do_switch,
518 "switch addr",
519 "[addr]"
520);
521
Heiko Schocher27df4082011-11-29 02:33:47 +0000522/*
523 * Initializes on-board ethernet controllers.
524 */
525int board_eth_init(bd_t *bis)
526{
Heiko Schocher804810a2012-05-14 20:24:14 +0000527 const char *s;
528 size_t len;
529
Heiko Schocher27df4082011-11-29 02:33:47 +0000530 davinci_emac_mii_mode_sel(0);
Heiko Schocher804810a2012-05-14 20:24:14 +0000531
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 }
Heiko Schocher27df4082011-11-29 02:33:47 +0000539
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
562static uchar kbd_magic_prefix[] = "key_magic_";
563static uchar kbd_command_prefix[] = "key_cmd_";
564
565struct kbd_data_t {
566 char s1;
567};
568
569struct 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
577static 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
596static 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
637int 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
667struct cmc_led {
668 char name[20];
669 unsigned char bank;
670 unsigned char gpio;
671};
672
673struct 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
684static 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
694static 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
702static 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
744U_BOOT_CMD(led, 3, 1, do_led,
745 "switch on/off board led",
746 "[name] [on/off]"
747);
748
749#ifdef CONFIG_HW_WATCHDOG
750void hw_watchdog_reset(void)
751{
752 davinci_hw_watchdog_reset();
753}
754#endif
755
756#if defined(CONFIG_POST)
757void 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
Heiko Schocher804810a2012-05-14 20:24:14 +0000781ulong post_word_load(void)
782{
783 struct davinci_rtc *reg =
784 (struct davinci_rtc *)CONFIG_SYS_POST_WORD_ADDR;
785
786 return in_be32(&reg->scratch2);
787}
788
789void post_word_store(ulong value)
790{
791 struct davinci_rtc *reg =
792 (struct davinci_rtc *)CONFIG_SYS_POST_WORD_ADDR;
793
794 /*
795 * write RTC kick register to enable write
796 * for RTC Scratch registers. Cratch0 and 1 are
797 * used for bootcount values.
798 */
799 writel(RTC_KICK0R_WE, &reg->kick0r);
800 writel(RTC_KICK1R_WE, &reg->kick1r);
801 out_be32(&reg->scratch2, value);
802}
803
Heiko Schocher27df4082011-11-29 02:33:47 +0000804void board_gpio_init(void)
805{
806 struct davinci_gpio *gpio = davinci_gpio_bank01;
807
808 /*
Heiko Schocher27df4082011-11-29 02:33:47 +0000809 * set LED (gpio Interface not usable here)
810 * set LED pins to output and state 0
811 */
812 clrbits_le32(&gpio->dir, 0x8000407e);
813 clrbits_le32(&gpio->out_data, 0x8000407e);
814 /* set LED 1 - 5 to state on */
815 setbits_le32(&gpio->out_data, 0x8000001e);
Heiko Schocher804810a2012-05-14 20:24:14 +0000816
817 /*
818 * set some gpio pins to low, this is needed early,
819 * so we have no gpio Interface here
820 * gpios:
821 * 8[8] Mode PV select low
822 * 8[9] Debug Rx Enable low
823 * 8[10] Mode Select PV low
824 * 8[11] Counter Interface RS485 Rx-Enable low
825 */
826 gpio = davinci_gpio_bank8;
827 clrbits_le32(&gpio->dir, 0x00000f00);
828 clrbits_le32(&gpio->out_data, 0x0f00);
Heiko Schocher27df4082011-11-29 02:33:47 +0000829}
830
831int board_late_init(void)
832{
833 cmc_set_led_state(&led_table[4], 0);
834
835 return 0;
836}
837
838void show_boot_progress(int val)
839{
840 switch (val) {
841 case 1:
842 cmc_set_led_state(&led_table[4], 1);
843 break;
844 case 4:
845 cmc_set_led_state(&led_table[4], 0);
846 break;
847 case 15:
848 cmc_set_led_state(&led_table[4], 1);
849 break;
850 }
851}
852
853#ifdef CONFIG_DAVINCI_MMC
854static struct davinci_mmc mmc_sd1 = {
855 .reg_base = (struct davinci_mmc_regs *)DAVINCI_MMC_SD1_BASE,
856 .input_clk = 228000000,
857 .host_caps = MMC_MODE_4BIT,
858 .voltages = MMC_VDD_32_33 | MMC_VDD_33_34,
859 .version = MMC_CTLR_VERSION_2,
860};
861
862int board_mmc_init(bd_t *bis)
863{
864 mmc_sd1.input_clk = clk_get(DAVINCI_MMC_CLKID);
865 /* Add slot-0 to mmc subsystem */
866 return davinci_mmc_init(bis, &mmc_sd1);
867}
868#endif