blob: 3d2fe73a442dc6074a34633555b12df46029b1e2 [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
Heiko Schocher62e34e32012-08-13 23:51:04 +0000454static struct spi_slave *enbw_cmc_init_spi(void)
Heiko Schocher804810a2012-05-14 20:24:14 +0000455{
456 struct spi_slave *spi;
Heiko Schocher804810a2012-05-14 20:24:14 +0000457 int ret;
Heiko Schocher804810a2012-05-14 20:24:14 +0000458
Heiko Schocher62e34e32012-08-13 23:51:04 +0000459 spi = spi_setup_slave(0, 0, 1000000, 0);
Heiko Schocher804810a2012-05-14 20:24:14 +0000460 if (!spi) {
461 printf("Failed to set up slave\n");
Heiko Schocher62e34e32012-08-13 23:51:04 +0000462 return NULL;
Heiko Schocher804810a2012-05-14 20:24:14 +0000463 }
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)
Heiko Schocher62e34e32012-08-13 23:51:04 +0000473 goto err_read;
Heiko Schocher804810a2012-05-14 20:24:14 +0000474
Heiko Schocher62e34e32012-08-13 23:51:04 +0000475 return spi;
476err_read:
477 spi_release_bus(spi);
478err_claim_bus:
479 spi_free_slave(spi);
480 return NULL;
481}
482
483static 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
Heiko Schocher804810a2012-05-14 20:24:14 +0000496 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))
Heiko Schocher62e34e32012-08-13 23:51:04 +0000501 if (enbw_cmc_switch_write(spi, reg, value)) {
502 /* error writing to switch */
503 ptr = NULL;
504 ret = -EINVAL;
505 }
Heiko Schocher804810a2012-05-14 20:24:14 +0000506 }
507 }
Heiko Schocher804810a2012-05-14 20:24:14 +0000508
Heiko Schocher804810a2012-05-14 20:24:14 +0000509 spi_release_bus(spi);
Heiko Schocher804810a2012-05-14 20:24:14 +0000510 spi_free_slave(spi);
Heiko Schocher62e34e32012-08-13 23:51:04 +0000511 return ret;
Heiko Schocher804810a2012-05-14 20:24:14 +0000512}
513
514static 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
527U_BOOT_CMD(switch, 3, 1, do_switch,
528 "switch addr",
529 "[addr]"
530);
531
Heiko Schocher27df4082011-11-29 02:33:47 +0000532/*
533 * Initializes on-board ethernet controllers.
534 */
535int board_eth_init(bd_t *bis)
536{
Heiko Schocher62e34e32012-08-13 23:51:04 +0000537 struct spi_slave *spi;
Heiko Schocher804810a2012-05-14 20:24:14 +0000538 const char *s;
Heiko Schocher62e34e32012-08-13 23:51:04 +0000539 size_t len = 0;
540 int config = 1;
Heiko Schocher804810a2012-05-14 20:24:14 +0000541
Heiko Schocher27df4082011-11-29 02:33:47 +0000542 davinci_emac_mii_mode_sel(0);
Heiko Schocher804810a2012-05-14 20:24:14 +0000543
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
Heiko Schocher62e34e32012-08-13 23:51:04 +0000549 config = enbw_cmc_config_switch(addr);
Heiko Schocher804810a2012-05-14 20:24:14 +0000550 }
Heiko Schocher27df4082011-11-29 02:33:47 +0000551
Heiko Schocher62e34e32012-08-13 23:51:04 +0000552 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 }
Heiko Schocher27df4082011-11-29 02:33:47 +0000574 if (!davinci_emac_initialize()) {
575 printf("Error: Ethernet init failed!\n");
576 return -1;
577 }
578
Heiko Schocher62e34e32012-08-13 23:51:04 +0000579 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);
Heiko Schocher27df4082011-11-29 02:33:47 +0000585
Heiko Schocher62e34e32012-08-13 23:51:04 +0000586 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 }
Heiko Schocher27df4082011-11-29 02:33:47 +0000592
593 return 0;
594}
595#endif /* CONFIG_DRIVER_TI_EMAC */
596
597#ifdef CONFIG_PREBOOT
598static uchar kbd_magic_prefix[] = "key_magic_";
599static uchar kbd_command_prefix[] = "key_cmd_";
600
601struct kbd_data_t {
602 char s1;
603};
604
605struct 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
613static 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
632static 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
673int 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
703struct cmc_led {
704 char name[20];
705 unsigned char bank;
706 unsigned char gpio;
707};
708
709struct 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
720static 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
730static 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
738static 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
780U_BOOT_CMD(led, 3, 1, do_led,
781 "switch on/off board led",
782 "[name] [on/off]"
783);
784
785#ifdef CONFIG_HW_WATCHDOG
786void hw_watchdog_reset(void)
787{
788 davinci_hw_watchdog_reset();
789}
790#endif
791
792#if defined(CONFIG_POST)
793void 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
Heiko Schocher804810a2012-05-14 20:24:14 +0000817ulong 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
825void 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
Heiko Schocher27df4082011-11-29 02:33:47 +0000840void board_gpio_init(void)
841{
842 struct davinci_gpio *gpio = davinci_gpio_bank01;
843
844 /*
Heiko Schocher27df4082011-11-29 02:33:47 +0000845 * 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);
Heiko Schocher804810a2012-05-14 20:24:14 +0000852
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);
Heiko Schocher27df4082011-11-29 02:33:47 +0000865}
866
867int board_late_init(void)
868{
869 cmc_set_led_state(&led_table[4], 0);
870
871 return 0;
872}
873
874void 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
890static 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
898int 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