]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - board/enbw/enbw_cmc/enbw_cmc.c
Changes to move hawkboard to the new spl infrastructure
[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 <asm/gpio.h>
39 #include <asm/io.h>
40 #include <asm/arch/da850_lowlevel.h>
41 #include <asm/arch/davinci_misc.h>
42 #include <asm/arch/emif_defs.h>
43 #include <asm/arch/emac_defs.h>
44 #include <asm/arch/gpio.h>
45 #include <asm/arch/pinmux_defs.h>
46 #include <asm/arch/hardware.h>
47 #include <asm/arch/sdmmc_defs.h>
48 #include <asm/arch/timer_defs.h>
49
50 DECLARE_GLOBAL_DATA_PTR;
51
52 const struct lpsc_resource lpsc[] = {
53         { DAVINCI_LPSC_AEMIF },
54         { DAVINCI_LPSC_SPI1 },
55         { DAVINCI_LPSC_ARM_RAM_ROM },
56         { DAVINCI_LPSC_UART0 },
57         { DAVINCI_LPSC_EMAC },
58         { DAVINCI_LPSC_UART0 },
59         { DAVINCI_LPSC_GPIO },
60         { DAVINCI_LPSC_DDR_EMIF },
61         { DAVINCI_LPSC_UART1 },
62         { DAVINCI_LPSC_UART2 },
63         { DAVINCI_LPSC_MMC_SD1 },
64         { DAVINCI_LPSC_USB20 },
65         { DAVINCI_LPSC_USB11 },
66 };
67
68 const int lpsc_size = ARRAY_SIZE(lpsc);
69
70 static const struct pinmux_config enbw_pins[] = {
71         { pinmux(0), 8, 0 },
72         { pinmux(0), 8, 1 },
73         { pinmux(0), 8, 2 },
74         { pinmux(0), 8, 3 },
75         { pinmux(0), 8, 4 },
76         { pinmux(0), 8, 5 },
77         { pinmux(1), 4, 0 },
78         { pinmux(1), 8, 1 },
79         { pinmux(1), 8, 2 },
80         { pinmux(1), 8, 3 },
81         { pinmux(1), 8, 4 },
82         { pinmux(1), 8, 5 },
83         { pinmux(1), 8, 6 },
84         { pinmux(1), 4, 7 },
85         { pinmux(2), 8, 0 },
86         { pinmux(5), 1, 0 },
87         { pinmux(5), 1, 3 },
88         { pinmux(5), 1, 7 },
89         { pinmux(6), 1, 0 },
90         { pinmux(6), 1, 1 },
91         { pinmux(6), 8, 2 },
92         { pinmux(6), 8, 3 },
93         { pinmux(6), 1, 4 },
94         { pinmux(6), 8, 5 },
95         { pinmux(6), 1, 7 },
96         { pinmux(7), 8, 2 },
97         { pinmux(7), 1, 3 },
98         { pinmux(7), 1, 6 },
99         { pinmux(7), 1, 7 },
100         { pinmux(13), 8, 2 },
101         { pinmux(13), 8, 3 },
102         { pinmux(13), 8, 4 },
103         { pinmux(13), 8, 5 },
104         { pinmux(13), 8, 6 },
105         { pinmux(13), 8, 7 },
106         { pinmux(14), 8, 0 },
107         { pinmux(14), 8, 1 },
108         { pinmux(16), 8, 1 },
109         { pinmux(16), 8, 2 },
110         { pinmux(16), 8, 3 },
111         { pinmux(16), 8, 4 },
112         { pinmux(16), 8, 5 },
113         { pinmux(16), 8, 6 },
114         { pinmux(16), 8, 7 },
115         { pinmux(17), 1, 0 },
116         { pinmux(17), 1, 1 },
117         { pinmux(17), 1, 2 },
118         { pinmux(17), 8, 3 },
119         { pinmux(17), 8, 4 },
120         { pinmux(17), 8, 5 },
121         { pinmux(17), 8, 6 },
122         { pinmux(17), 8, 7 },
123         { pinmux(18), 8, 0 },
124         { pinmux(18), 8, 1 },
125         { pinmux(18), 2, 2 },
126         { pinmux(18), 2, 3 },
127         { pinmux(18), 2, 4 },
128         { pinmux(18), 8, 6 },
129         { pinmux(18), 8, 7 },
130         { pinmux(19), 8, 0 },
131         { pinmux(19), 2, 1 },
132         { pinmux(19), 2, 2 },
133         { pinmux(19), 2, 3 },
134         { pinmux(19), 2, 4 },
135         { pinmux(19), 8, 5 },
136         { pinmux(19), 8, 6 },
137 };
138
139 const struct pinmux_resource pinmuxes[] = {
140         PINMUX_ITEM(emac_pins_mii),
141         PINMUX_ITEM(emac_pins_mdio),
142         PINMUX_ITEM(i2c0_pins),
143         PINMUX_ITEM(emifa_pins_cs2),
144         PINMUX_ITEM(emifa_pins_cs3),
145         PINMUX_ITEM(emifa_pins_cs4),
146         PINMUX_ITEM(emifa_pins_nand),
147         PINMUX_ITEM(emifa_pins_nor),
148         PINMUX_ITEM(spi1_pins_base),
149         PINMUX_ITEM(spi1_pins_scs0),
150         PINMUX_ITEM(uart1_pins_txrx),
151         PINMUX_ITEM(uart2_pins_txrx),
152         PINMUX_ITEM(uart2_pins_rtscts),
153         PINMUX_ITEM(enbw_pins),
154 };
155
156 const int pinmuxes_size = ARRAY_SIZE(pinmuxes);
157
158 struct gpio_config {
159         char name[GPIO_NAME_SIZE];
160         unsigned char bank;
161         unsigned char gpio;
162         unsigned char out;
163         unsigned char value;
164 };
165
166 static const struct gpio_config enbw_gpio_config[] = {
167         { "RS485 enable",       8, 11, 1, 0 },
168         { "RS485 iso",          8, 10, 1, 0 },
169         { "W2HUT RS485 Rx ena", 8,  9, 1, 0 },
170         { "W2HUT RS485 iso",    8,  8, 1, 0 },
171         { "LAN reset",          7, 15, 1, 1 },
172         { "ena 11V PLC",        7, 14, 1, 0 },
173         { "ena 1.5V PLC",       7, 13, 1, 0 },
174         { "disable VBUS",       7, 12, 1, 1 },
175         { "PLC reset",          6, 13, 1, 1 },
176         { "LCM RS",             6, 12, 1, 0 },
177         { "LCM R/W",            6, 11, 1, 0 },
178         { "PLC pairing",        6, 10, 1, 1 },
179         { "PLC MDIO CLK",       6,  9, 1, 0 },
180         { "HK218",              6,  8, 1, 0 },
181         { "HK218 Rx",           6,  1, 1, 1 },
182         { "TPM reset",          6,  0, 1, 1 },
183         { "LCM E",              2,  2, 1, 1 },
184         { "PV-IF RxD ena",      0, 15, 1, 1 },
185         { "LED1",               1, 15, 1, 1 },
186         { "LED2",               0,  1, 1, 1 },
187         { "LED3",               0,  2, 1, 1 },
188         { "LED4",               0,  3, 1, 1 },
189         { "LED5",               0,  4, 1, 1 },
190         { "LED6",               0,  5, 1, 0 },
191         { "LED7",               0,  6, 1, 0 },
192         { "LED8",               0, 14, 1, 0 },
193         { "USER1",              0, 12, 0, 0 },
194         { "USER2",              0, 13, 0, 0 },
195 };
196
197 #define PHY_POWER       0x0800
198
199 static void enbw_cmc_switch(int port, int on)
200 {
201         const char      *devname;
202         unsigned char phyaddr = 3;
203         unsigned char   reg = 0;
204         unsigned short  data;
205
206         if (port == 1)
207                 phyaddr = 2;
208
209         devname = miiphy_get_current_dev();
210         if (!devname) {
211                 printf("Error: no mii device\n");
212                 return;
213         }
214         if (miiphy_read(devname, phyaddr, reg, &data) != 0) {
215                 printf("Error reading from the PHY addr=%02x reg=%02x\n",
216                         phyaddr, reg);
217                 return;
218         }
219
220         if (on)
221                 data &= ~PHY_POWER;
222         else
223                 data |= PHY_POWER;
224
225         if (miiphy_write(devname, phyaddr, reg, data) != 0) {
226                 printf("Error writing to the PHY addr=%02x reg=%02x\n",
227                         phyaddr, reg);
228                 return;
229         }
230 }
231
232 int board_init(void)
233 {
234         int i, ret;
235
236 #ifndef CONFIG_USE_IRQ
237         irq_init();
238 #endif
239         /* address of boot parameters, not used as booting with DTT */
240         gd->bd->bi_boot_params = 0;
241
242         for (i = 0; i < ARRAY_SIZE(enbw_gpio_config); i++) {
243                 int gpio = enbw_gpio_config[i].bank * 16 +
244                         enbw_gpio_config[i].gpio;
245
246                 ret = gpio_request(gpio, enbw_gpio_config[i].name);
247                 if (ret) {
248                         printf("%s: Could not get %s gpio\n", __func__,
249                                 enbw_gpio_config[i].name);
250                         return -1;
251                 }
252
253                 if (enbw_gpio_config[i].out)
254                         gpio_direction_output(gpio,
255                                 enbw_gpio_config[i].value);
256                 else
257                         gpio_direction_input(gpio);
258         }
259
260         /* setup the SUSPSRC for ARM to control emulation suspend */
261         clrbits_le32(&davinci_syscfg_regs->suspsrc,
262                 (DAVINCI_SYSCFG_SUSPSRC_EMAC | DAVINCI_SYSCFG_SUSPSRC_I2C |
263                 DAVINCI_SYSCFG_SUSPSRC_SPI1 | DAVINCI_SYSCFG_SUSPSRC_TIMER0 |
264                 DAVINCI_SYSCFG_SUSPSRC_UART2));
265
266         return 0;
267 }
268
269 #ifdef CONFIG_DRIVER_TI_EMAC
270 /*
271  * Initializes on-board ethernet controllers.
272  */
273 int board_eth_init(bd_t *bis)
274 {
275 #ifdef CONFIG_DRIVER_TI_EMAC
276         davinci_emac_mii_mode_sel(0);
277 #endif /* CONFIG_DRIVER_TI_EMAC */
278
279         if (!davinci_emac_initialize()) {
280                 printf("Error: Ethernet init failed!\n");
281                 return -1;
282         }
283
284         if (hwconfig_subarg_cmp("switch", "lan", "on"))
285                 /* Switch port lan on */
286                 enbw_cmc_switch(1, 1);
287         else
288                 enbw_cmc_switch(1, 0);
289
290         if (hwconfig_subarg_cmp("switch", "pwl", "on"))
291                 /* Switch port pwl on */
292                 enbw_cmc_switch(2, 1);
293         else
294                 enbw_cmc_switch(2, 0);
295
296         return 0;
297 }
298 #endif /* CONFIG_DRIVER_TI_EMAC */
299
300 #ifdef CONFIG_PREBOOT
301 static uchar kbd_magic_prefix[]         = "key_magic_";
302 static uchar kbd_command_prefix[]       = "key_cmd_";
303
304 struct kbd_data_t {
305         char s1;
306 };
307
308 struct kbd_data_t *get_keys(struct kbd_data_t *kbd_data)
309 {
310         /* read SW1 + SW2 */
311         kbd_data->s1 = gpio_get_value(12) +
312                 (gpio_get_value(13) << 1);
313         return kbd_data;
314 }
315
316 static int compare_magic(const struct kbd_data_t *kbd_data, char *str)
317 {
318         char s1 = str[0];
319
320         if (s1 >= '0' && s1 <= '9')
321                 s1 -= '0';
322         else if (s1 >= 'a' && s1 <= 'f')
323                 s1 = s1 - 'a' + 10;
324         else if (s1 >= 'A' && s1 <= 'F')
325                 s1 = s1 - 'A' + 10;
326         else
327                 return -1;
328
329         if (s1 != kbd_data->s1)
330                 return -1;
331
332         return 0;
333 }
334
335 static char *key_match(const struct kbd_data_t *kbd_data)
336 {
337         char magic[sizeof(kbd_magic_prefix) + 1];
338         char *suffix;
339         char *kbd_magic_keys;
340
341         /*
342          * The following string defines the characters that can be appended
343          * to "key_magic" to form the names of environment variables that
344          * hold "magic" key codes, i. e. such key codes that can cause
345          * pre-boot actions. If the string is empty (""), then only
346          * "key_magic" is checked (old behaviour); the string "125" causes
347          * checks for "key_magic1", "key_magic2" and "key_magic5", etc.
348          */
349         kbd_magic_keys = getenv("magic_keys");
350         if (kbd_magic_keys == NULL)
351                 kbd_magic_keys = "";
352
353         /*
354          * loop over all magic keys;
355          * use '\0' suffix in case of empty string
356          */
357         for (suffix = kbd_magic_keys; *suffix ||
358                 suffix == kbd_magic_keys; ++suffix) {
359                 sprintf(magic, "%s%c", kbd_magic_prefix, *suffix);
360
361                 if (compare_magic(kbd_data, getenv(magic)) == 0) {
362                         char cmd_name[sizeof(kbd_command_prefix) + 1];
363                         char *cmd;
364
365                         sprintf(cmd_name, "%s%c", kbd_command_prefix, *suffix);
366                         cmd = getenv(cmd_name);
367
368                         return cmd;
369                 }
370         }
371
372         return NULL;
373 }
374 #endif /* CONFIG_PREBOOT */
375
376 int misc_init_r(void)
377 {
378         char *s, buf[32];
379 #ifdef CONFIG_PREBOOT
380         struct kbd_data_t kbd_data;
381         /* Decode keys */
382         char *str = strdup(key_match(get_keys(&kbd_data)));
383         /* Set or delete definition */
384         setenv("preboot", str);
385         free(str);
386 #endif /* CONFIG_PREBOOT */
387
388         /* count all restarts, and save this in an environment var */
389         s = getenv("restartcount");
390
391         if (s)
392                 sprintf(buf, "%ld", simple_strtoul(s, NULL, 10) + 1);
393         else
394                 strcpy(buf, "1");
395
396         setenv("restartcount", buf);
397         saveenv();
398
399 #ifdef CONFIG_HW_WATCHDOG
400         davinci_hw_watchdog_enable();
401 #endif
402
403         return 0;
404 }
405
406 struct cmc_led {
407         char name[20];
408         unsigned char bank;
409         unsigned char gpio;
410 };
411
412 struct cmc_led led_table[] = {
413         {"led1", 1, 15},
414         {"led2", 0, 1},
415         {"led3", 0, 2},
416         {"led4", 0, 3},
417         {"led5", 0, 4},
418         {"led6", 0, 5},
419         {"led7", 0, 6},
420         {"led8", 0, 14},
421 };
422
423 static int cmc_get_led_state(struct cmc_led *led)
424 {
425         int value;
426         int gpio = led->bank * 16 + led->gpio;
427
428         value = gpio_get_value(gpio);
429
430         return value;
431 }
432
433 static int cmc_set_led_state(struct cmc_led *led, int state)
434 {
435         int gpio = led->bank * 16 + led->gpio;
436
437         gpio_set_value(gpio, state);
438         return 0;
439 }
440
441 static int do_led(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
442 {
443         struct cmc_led *led;
444         int found = 0;
445         int i = 0;
446         int only_print = 0;
447         int len = ARRAY_SIZE(led_table);
448
449         if (argc < 2)
450                 return cmd_usage(cmdtp);
451
452         if (argc < 3)
453                 only_print = 1;
454
455         led = led_table;
456         while ((!found) && (i < len)) {
457                 if (strcmp(argv[1], led->name) == 0) {
458                         found = 1;
459                 } else {
460                         led++;
461                         i++;
462                 }
463         }
464         if (!found)
465                 return cmd_usage(cmdtp);
466
467         if (only_print) {
468                 if (cmc_get_led_state(led))
469                         printf("on\n");
470                 else
471                         printf("off\n");
472
473                 return 0;
474         }
475         if (strcmp(argv[2], "on") == 0)
476                 cmc_set_led_state(led, 1);
477         else
478                 cmc_set_led_state(led, 0);
479
480         return 0;
481 }
482
483 U_BOOT_CMD(led, 3, 1, do_led,
484         "switch on/off board led",
485         "[name] [on/off]"
486 );
487
488 #ifdef CONFIG_HW_WATCHDOG
489 void hw_watchdog_reset(void)
490 {
491         davinci_hw_watchdog_reset();
492 }
493 #endif
494
495 #if defined(CONFIG_POST)
496 void arch_memory_failure_handle(void)
497 {
498         struct davinci_gpio *gpio = davinci_gpio_bank01;
499         int state = 1;
500
501         /*
502          * if memor< failure blink with the LED 1,2 and 3
503          * as we running from flash, we cannot use the gpio
504          * api here, so access the gpio pin direct through
505          * the gpio register.
506          */
507         while (1) {
508                 if (state) {
509                         clrbits_le32(&gpio->out_data, 0x80000006);
510                         state = 0;
511                 } else {
512                         setbits_le32(&gpio->out_data, 0x80000006);
513                         state = 1;
514                 }
515                 udelay(500);
516         }
517 }
518 #endif
519
520 #if defined(CONFIG_BOOTCOUNT_LIMIT)
521 void bootcount_store(ulong a)
522 {
523         struct davinci_rtc *reg =
524                 (struct davinci_rtc *)CONFIG_SYS_BOOTCOUNT_ADDR;
525
526         /*
527          * write RTC kick register to enable write
528          * for RTC Scratch registers. Cratch0 and 1 are
529          * used for bootcount values.
530          */
531         writel(RTC_KICK0R_WE, &reg->kick0r);
532         writel(RTC_KICK1R_WE, &reg->kick1r);
533         out_be32(&reg->scratch0, a);
534         out_be32(&reg->scratch1, BOOTCOUNT_MAGIC);
535 }
536
537 ulong bootcount_load(void)
538 {
539         struct davinci_rtc *reg =
540                 (struct davinci_rtc *)CONFIG_SYS_BOOTCOUNT_ADDR;
541
542         if (in_be32(&reg->scratch1) != BOOTCOUNT_MAGIC)
543                 return 0;
544         else
545                 return in_be32(&reg->scratch0);
546 }
547 #endif
548
549 void board_gpio_init(void)
550 {
551         struct davinci_gpio *gpio = davinci_gpio_bank01;
552
553         /*
554          * set LED (gpio Interface not usable here)
555          * set LED pins to output and state 0
556          */
557         clrbits_le32(&gpio->dir, 0x8000407e);
558         clrbits_le32(&gpio->out_data, 0x8000407e);
559         /* set LED 1 - 5 to state on */
560         setbits_le32(&gpio->out_data, 0x8000001e);
561 }
562
563 int board_late_init(void)
564 {
565         cmc_set_led_state(&led_table[4], 0);
566
567         return 0;
568 }
569
570 void show_boot_progress(int val)
571 {
572         switch (val) {
573         case 1:
574                 cmc_set_led_state(&led_table[4], 1);
575                 break;
576         case 4:
577                 cmc_set_led_state(&led_table[4], 0);
578                 break;
579         case 15:
580                 cmc_set_led_state(&led_table[4], 1);
581                 break;
582         }
583 }
584
585 #ifdef CONFIG_DAVINCI_MMC
586 static struct davinci_mmc mmc_sd1 = {
587         .reg_base       = (struct davinci_mmc_regs *)DAVINCI_MMC_SD1_BASE,
588         .input_clk      = 228000000,
589         .host_caps      = MMC_MODE_4BIT,
590         .voltages       = MMC_VDD_32_33 | MMC_VDD_33_34,
591         .version        = MMC_CTLR_VERSION_2,
592 };
593
594 int board_mmc_init(bd_t *bis)
595 {
596         mmc_sd1.input_clk = clk_get(DAVINCI_MMC_CLKID);
597         /* Add slot-0 to mmc subsystem */
598         return davinci_mmc_init(bis, &mmc_sd1);
599 }
600 #endif