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