karo: tx28: enable activity led only on stk5-v3 baseboard
[karo-tx-uboot.git] / board / karo / tx28 / tx28.c
1 /*
2  * Copyright (C) 2011-2013 Lothar WaƟmann <LW@KARO-electronics.de>
3  * based on: board/freescale/mx28_evk.c (C) 2010 Freescale Semiconductor, Inc.
4  *
5  * See file CREDITS for list of people who contributed to this
6  * project.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * version 2 as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  */
18
19 #include <common.h>
20 #include <errno.h>
21 #include <libfdt.h>
22 #include <fdt_support.h>
23 #include <lcd.h>
24 #include <netdev.h>
25 #include <mmc.h>
26 #include <mxcfb.h>
27 #include <video_fb.h>
28 #include <linux/list.h>
29 #include <linux/fb.h>
30 #include <asm/io.h>
31 #include <asm/gpio.h>
32 #include <asm/arch/iomux-mx28.h>
33 #include <asm/arch/clock.h>
34 #include <asm/arch/imx-regs.h>
35 #include <asm/arch/sys_proto.h>
36
37 #include "../common/karo.h"
38
39 DECLARE_GLOBAL_DATA_PTR;
40
41 #define MXS_GPIO_NR(p, o)      (((p) << 5) | (o))
42
43 #define TX28_LCD_PWR_GPIO       MX28_PAD_LCD_ENABLE__GPIO_1_31
44 #define TX28_LCD_RST_GPIO       MX28_PAD_LCD_RESET__GPIO_3_30
45 #define TX28_LCD_BACKLIGHT_GPIO MX28_PAD_PWM0__GPIO_3_16
46
47 #define TX28_USBH_VBUSEN_GPIO   MX28_PAD_SPDIF__GPIO_3_27
48 #define TX28_USBH_OC_GPIO       MX28_PAD_JTAG_RTCK__GPIO_4_20
49 #define TX28_USBOTG_VBUSEN_GPIO MX28_PAD_GPMI_CE2N__GPIO_0_18
50 #define TX28_USBOTG_OC_GPIO     MX28_PAD_GPMI_CE3N__GPIO_0_19
51 #define TX28_USBOTG_ID_GPIO     MX28_PAD_PWM2__GPIO_3_18
52
53 #define TX28_LED_GPIO           MX28_PAD_ENET0_RXD3__GPIO_4_10
54
55 #define STK5_CAN_XCVR_GPIO      MX28_PAD_LCD_D00__GPIO_1_0
56
57 #define ENET_PAD_CTRL           (MXS_PAD_3V3 | MXS_PAD_4MA | MXS_PAD_PULLUP)
58 #define GPIO_PAD_CTRL           (MXS_PAD_3V3 | MXS_PAD_4MA | MXS_PAD_PULLUP)
59 #define I2C_PAD_CTRL            (MXS_PAD_3V3 | MXS_PAD_12MA | MXS_PAD_PULLUP)
60
61 #ifndef CONFIG_CONS_INDEX
62 struct serial_device *default_serial_console(void)
63 {
64         return NULL;
65 }
66 #endif
67
68 static const struct gpio tx28_gpios[] = {
69         { TX28_USBH_VBUSEN_GPIO, GPIOFLAG_OUTPUT_INIT_LOW, "USBH VBUSEN", },
70         { TX28_USBH_OC_GPIO, GPIOFLAG_INPUT, "USBH OC", },
71         { TX28_USBOTG_VBUSEN_GPIO, GPIOFLAG_OUTPUT_INIT_LOW, "USBOTG VBUSEN", },
72         { TX28_USBOTG_OC_GPIO, GPIOFLAG_INPUT, "USBOTG OC", },
73         { TX28_USBOTG_ID_GPIO, GPIOFLAG_INPUT, "USBOTG ID", },
74 };
75
76 static const iomux_cfg_t tx28_pads[] = {
77         /* I2C bus for internal DS1339, PCA9554 and on DIMM pins 40/41 */
78         MX28_PAD_I2C0_SCL__I2C0_SCL | I2C_PAD_CTRL,
79         MX28_PAD_I2C0_SDA__I2C0_SDA | I2C_PAD_CTRL,
80
81         /* USBH VBUSEN, OC */
82         MX28_PAD_SPDIF__GPIO_3_27,
83         MX28_PAD_JTAG_RTCK__GPIO_4_20,
84
85         /* USBOTG VBUSEN, OC, ID */
86         MX28_PAD_GPMI_CE2N__GPIO_0_18,
87         MX28_PAD_GPMI_CE3N__GPIO_0_19,
88         MX28_PAD_PWM2__GPIO_3_18,
89 };
90
91 /*
92  * Functions
93  */
94
95 /* provide at least _some_ sort of randomness */
96 #define MAX_LOOPS       100
97
98 static u32 random __attribute__((section("data")));
99
100 static inline void random_init(void)
101 {
102         struct mxs_digctl_regs *digctl_regs = (void *)MXS_DIGCTL_BASE;
103         u32 seed = 0;
104         int i;
105
106         for (i = 0; i < MAX_LOOPS; i++) {
107                 u32 hclk = readl(&digctl_regs->hw_digctl_hclkcount);
108                 u32 entropy = readl(&digctl_regs->hw_digctl_entropy);
109                 u32 usec = readl(&digctl_regs->hw_digctl_microseconds);
110
111                 seed = get_timer(hclk ^ entropy ^ usec ^ random ^ seed);
112                 srand(seed);
113                 random = rand();
114         }
115 }
116
117 #define RTC_PERSISTENT0_CLK32_MASK      (RTC_PERSISTENT0_CLOCKSOURCE |  \
118                                         RTC_PERSISTENT0_XTAL32KHZ_PWRUP)
119 static u32 boot_cause __attribute__((section("data")));
120
121 int board_early_init_f(void)
122 {
123         struct mxs_rtc_regs *rtc_regs = (void *)MXS_RTC_BASE;
124         u32 rtc_stat;
125         int timeout = 5000;
126
127         random_init();
128
129         /* IO0 clock at 480MHz */
130         mxs_set_ioclk(MXC_IOCLK0, 480000);
131         /* IO1 clock at 480MHz */
132         mxs_set_ioclk(MXC_IOCLK1, 480000);
133
134         /* SSP0 clock at 96MHz */
135         mxs_set_sspclk(MXC_SSPCLK0, 96000, 0);
136         /* SSP2 clock at 96MHz */
137         mxs_set_sspclk(MXC_SSPCLK2, 96000, 0);
138
139         gpio_request_array(tx28_gpios, ARRAY_SIZE(tx28_gpios));
140         mxs_iomux_setup_multiple_pads(tx28_pads, ARRAY_SIZE(tx28_pads));
141
142         while ((rtc_stat = readl(&rtc_regs->hw_rtc_stat)) &
143                 RTC_STAT_STALE_REGS_PERSISTENT0) {
144                 if (timeout-- < 0)
145                         return 1;
146                 udelay(1);
147         }
148         boot_cause = readl(&rtc_regs->hw_rtc_persistent0);
149         if ((boot_cause & RTC_PERSISTENT0_CLK32_MASK) !=
150                 RTC_PERSISTENT0_CLK32_MASK) {
151                 if (boot_cause & RTC_PERSISTENT0_CLOCKSOURCE)
152                         goto rtc_err;
153                 writel(RTC_PERSISTENT0_CLK32_MASK,
154                         &rtc_regs->hw_rtc_persistent0_set);
155         }
156         return 0;
157
158 rtc_err:
159         serial_puts("Inconsistent value in RTC_PERSISTENT0 register; power-on-reset required\n");
160         return 1;
161 }
162
163 int board_init(void)
164 {
165         if (ctrlc())
166                 printf("CTRL-C detected; safeboot enabled\n");
167
168         /* Address of boot parameters */
169 #ifdef CONFIG_OF_LIBFDT
170         gd->bd->bi_arch_number = -1;
171 #endif
172         gd->bd->bi_boot_params = PHYS_SDRAM_1 + 0x1000;
173         return 0;
174 }
175
176 int dram_init(void)
177 {
178         return mxs_dram_init();
179 }
180
181 #ifdef  CONFIG_CMD_MMC
182 static int tx28_mmc_wp(int dev_no)
183 {
184         return 0;
185 }
186
187 int board_mmc_init(bd_t *bis)
188 {
189         return mxsmmc_initialize(bis, 0, tx28_mmc_wp, NULL);
190 }
191 #endif /* CONFIG_CMD_MMC */
192
193 #ifdef CONFIG_FEC_MXC
194 #ifdef CONFIG_GET_FEC_MAC_ADDR_FROM_IIM
195
196 #ifndef CONFIG_TX28_S
197 #define FEC_MAX_IDX                     1
198 #else
199 #define FEC_MAX_IDX                     0
200 #endif
201 #ifndef ETH_ALEN
202 #define ETH_ALEN                        6
203 #endif
204
205 static int fec_get_mac_addr(int index)
206 {
207         int timeout = 1000;
208         struct mxs_ocotp_regs *ocotp_regs =
209                 (struct mxs_ocotp_regs *)MXS_OCOTP_BASE;
210         u32 *cust = &ocotp_regs->hw_ocotp_cust0;
211         u8 mac[ETH_ALEN];
212         char env_name[] = "eth.addr";
213         u32 val = 0;
214         int i;
215
216         if (index < 0 || index > FEC_MAX_IDX)
217                 return -EINVAL;
218
219         /* set this bit to open the OTP banks for reading */
220         writel(OCOTP_CTRL_RD_BANK_OPEN,
221                 &ocotp_regs->hw_ocotp_ctrl_set);
222
223         /* wait until OTP contents are readable */
224         while (OCOTP_CTRL_BUSY & readl(&ocotp_regs->hw_ocotp_ctrl)) {
225                 if (timeout-- < 0)
226                         return -ETIMEDOUT;
227                 udelay(100);
228         }
229
230         for (i = 0; i < sizeof(mac); i++) {
231                 int shift = 24 - i % 4 * 8;
232
233                 if (i % 4 == 0)
234                         val = readl(&cust[index * 8 + i]);
235                 mac[i] = val >> shift;
236         }
237         if (!is_valid_ethaddr(mac)) {
238                 if (index == 0)
239                         printf("No valid MAC address programmed\n");
240                 return 0;
241         }
242
243         if (index == 0) {
244                 printf("MAC addr from fuse: %pM\n", mac);
245                 snprintf(env_name, sizeof(env_name), "ethaddr");
246         } else {
247                 snprintf(env_name, sizeof(env_name), "eth%daddr", index);
248         }
249         eth_setenv_enetaddr(env_name, mac);
250         return 0;
251 }
252
253 static inline int tx28_fec1_enabled(void)
254 {
255         const char *status;
256         int off;
257
258         if (!gd->fdt_blob)
259                 return 0;
260
261         off = fdt_path_offset(gd->fdt_blob, "ethernet1");
262         if (off < 0)
263                 return 0;
264
265         status = fdt_getprop(gd->fdt_blob, off, "status", NULL);
266         return status && (strcmp(status, "okay") == 0);
267 }
268
269 static void tx28_init_mac(void)
270 {
271         int ret;
272
273         ret = fec_get_mac_addr(0);
274         if (ret < 0) {
275                 printf("Failed to read FEC0 MAC address from OCOTP\n");
276                 return;
277         }
278 #ifdef CONFIG_TX28_S
279         if (tx28_fec1_enabled()) {
280                 ret = fec_get_mac_addr(1);
281                 if (ret < 0) {
282                         printf("Failed to read FEC1 MAC address from OCOTP\n");
283                         return;
284                 }
285         }
286 #endif
287 }
288 #else
289 static inline void tx28_init_mac(void)
290 {
291 }
292 #endif /* CONFIG_GET_FEC_MAC_ADDR_FROM_IIM */
293
294 static const iomux_cfg_t tx28_fec_pads[] = {
295         MX28_PAD_ENET0_RX_EN__ENET0_RX_EN | ENET_PAD_CTRL,
296         MX28_PAD_ENET0_RXD0__ENET0_RXD0 | ENET_PAD_CTRL,
297         MX28_PAD_ENET0_RXD1__ENET0_RXD1 | ENET_PAD_CTRL,
298 };
299
300 int board_eth_init(bd_t *bis)
301 {
302         int ret;
303
304         /* Reset the external phy */
305         gpio_direction_output(MX28_PAD_ENET0_RX_CLK__GPIO_4_13, 0);
306
307         /* Power on the external phy */
308         gpio_direction_output(MX28_PAD_PWM4__GPIO_3_29, 1);
309
310         /* Pull strap pins to high */
311         gpio_direction_output(MX28_PAD_ENET0_RX_EN__GPIO_4_2, 1);
312         gpio_direction_output(MX28_PAD_ENET0_RXD0__GPIO_4_3, 1);
313         gpio_direction_output(MX28_PAD_ENET0_RXD1__GPIO_4_4, 1);
314         gpio_direction_input(MX28_PAD_ENET0_TX_CLK__GPIO_4_5);
315
316         udelay(25000);
317         gpio_set_value(MX28_PAD_ENET0_RX_CLK__GPIO_4_13, 1);
318         udelay(100);
319
320         mxs_iomux_setup_multiple_pads(tx28_fec_pads, ARRAY_SIZE(tx28_fec_pads));
321
322         ret = cpu_eth_init(bis);
323         if (ret) {
324                 printf("cpu_eth_init() failed: %d\n", ret);
325                 return ret;
326         }
327
328 #ifndef CONFIG_TX28_S
329         if (getenv("ethaddr")) {
330                 ret = fecmxc_initialize_multi(bis, 0, 0, MXS_ENET0_BASE);
331                 if (ret) {
332                         printf("FEC MXS: Unable to init FEC0\n");
333                         return ret;
334                 }
335         }
336
337         if (getenv("eth1addr")) {
338                 ret = fecmxc_initialize_multi(bis, 1, 1, MXS_ENET1_BASE);
339                 if (ret) {
340                         printf("FEC MXS: Unable to init FEC1\n");
341                         return ret;
342                 }
343         }
344 #else
345         if (getenv("ethaddr")) {
346                 ret = fecmxc_initialize(bis);
347                 if (ret) {
348                         printf("FEC MXS: Unable to init FEC\n");
349                         return ret;
350                 }
351         }
352 #endif
353         return 0;
354 }
355 #else
356 static inline void tx28_init_mac(void)
357 {
358 }
359 #endif /* CONFIG_FEC_MXC */
360
361 enum {
362         LED_STATE_INIT = -1,
363         LED_STATE_OFF,
364         LED_STATE_ON,
365         LED_STATE_DISABLED,
366 };
367
368 static int led_state = LED_STATE_DISABLED;
369
370 void show_activity(int arg)
371 {
372         static ulong last;
373
374         if (led_state == LED_STATE_DISABLED) {
375                 return;
376         } else if (led_state == LED_STATE_INIT) {
377                 last = get_timer(0);
378                 gpio_set_value(TX28_LED_GPIO, 1);
379                 led_state = LED_STATE_ON;
380         } else {
381                 if (get_timer(last) > CONFIG_SYS_HZ) {
382                         last = get_timer(0);
383                         if (led_state == LED_STATE_ON) {
384                                 gpio_set_value(TX28_LED_GPIO, 0);
385                                 led_state = LED_STATE_OFF;
386                         } else {
387                                 gpio_set_value(TX28_LED_GPIO, 1);
388                                 led_state = LED_STATE_ON;
389                         }
390                 }
391         }
392 }
393
394 static const iomux_cfg_t stk5_pads[] = {
395         /* SW controlled LED on STK5 baseboard */
396         MX28_PAD_ENET0_RXD3__GPIO_4_10 | GPIO_PAD_CTRL,
397 };
398
399 static const struct gpio stk5_gpios[] = {
400 };
401
402 #ifdef CONFIG_LCD
403 vidinfo_t panel_info = {
404         /* set to max. size supported by SoC */
405         .vl_col = 1600,
406         .vl_row = 1200,
407
408         .vl_bpix = LCD_COLOR32,    /* Bits per pixel, 0: 1bpp, 1: 2bpp, 2: 4bpp, 3: 8bpp ... */
409 };
410
411 static struct fb_videomode tx28_fb_modes[] = {
412         {
413                 /* Standard VGA timing */
414                 .name           = "VGA",
415                 .refresh        = 60,
416                 .xres           = 640,
417                 .yres           = 480,
418                 .pixclock       = KHZ2PICOS(25175),
419                 .left_margin    = 48,
420                 .hsync_len      = 96,
421                 .right_margin   = 16,
422                 .upper_margin   = 31,
423                 .vsync_len      = 2,
424                 .lower_margin   = 12,
425                 .vmode          = FB_VMODE_NONINTERLACED,
426         },
427         {
428                 /* Emerging ETV570 640 x 480 display. Syncs low active,
429                  * DE high active, 115.2 mm x 86.4 mm display area
430                  * VGA compatible timing
431                  */
432                 .name           = "ETV570",
433                 .refresh        = 60,
434                 .xres           = 640,
435                 .yres           = 480,
436                 .pixclock       = KHZ2PICOS(25175),
437                 .left_margin    = 114,
438                 .hsync_len      = 30,
439                 .right_margin   = 16,
440                 .upper_margin   = 32,
441                 .vsync_len      = 3,
442                 .lower_margin   = 10,
443                 .vmode          = FB_VMODE_NONINTERLACED,
444         },
445         {
446                 /* Emerging ET0350G0DH6 320 x 240 display.
447                  * 70.08 mm x 52.56 mm display area.
448                  */
449                 .name           = "ET0350",
450                 .refresh        = 60,
451                 .xres           = 320,
452                 .yres           = 240,
453                 .pixclock       = KHZ2PICOS(6500),
454                 .left_margin    = 68 - 34,
455                 .hsync_len      = 34,
456                 .right_margin   = 20,
457                 .upper_margin   = 18 - 3,
458                 .vsync_len      = 3,
459                 .lower_margin   = 4,
460                 .vmode          = FB_VMODE_NONINTERLACED,
461         },
462         {
463                 /* Emerging ET0430G0DH6 480 x 272 display.
464                  * 95.04 mm x 53.856 mm display area.
465                  */
466                 .name           = "ET0430",
467                 .refresh        = 60,
468                 .xres           = 480,
469                 .yres           = 272,
470                 .pixclock       = KHZ2PICOS(9000),
471                 .left_margin    = 2,
472                 .hsync_len      = 41,
473                 .right_margin   = 2,
474                 .upper_margin   = 2,
475                 .vsync_len      = 10,
476                 .lower_margin   = 2,
477                 .sync           = FB_SYNC_CLK_LAT_FALL,
478                 .vmode          = FB_VMODE_NONINTERLACED,
479         },
480         {
481                 /* Emerging ET0500G0DH6 800 x 480 display.
482                  * 109.6 mm x 66.4 mm display area.
483                  */
484                 .name           = "ET0500",
485                 .refresh        = 60,
486                 .xres           = 800,
487                 .yres           = 480,
488                 .pixclock       = KHZ2PICOS(33260),
489                 .left_margin    = 216 - 128,
490                 .hsync_len      = 128,
491                 .right_margin   = 1056 - 800 - 216,
492                 .upper_margin   = 35 - 2,
493                 .vsync_len      = 2,
494                 .lower_margin   = 525 - 480 - 35,
495                 .vmode          = FB_VMODE_NONINTERLACED,
496         },
497         {
498                 /* Emerging ETQ570G0DH6 320 x 240 display.
499                  * 115.2 mm x 86.4 mm display area.
500                  */
501                 .name           = "ETQ570",
502                 .refresh        = 60,
503                 .xres           = 320,
504                 .yres           = 240,
505                 .pixclock       = KHZ2PICOS(6400),
506                 .left_margin    = 38,
507                 .hsync_len      = 30,
508                 .right_margin   = 30,
509                 .upper_margin   = 16, /* 15 according to datasheet */
510                 .vsync_len      = 3, /* TVP -> 1>x>5 */
511                 .lower_margin   = 4, /* 4.5 according to datasheet */
512                 .vmode          = FB_VMODE_NONINTERLACED,
513         },
514         {
515                 /* Emerging ET0700G0DH6 800 x 480 display.
516                  * 152.4 mm x 91.44 mm display area.
517                  */
518                 .name           = "ET0700",
519                 .refresh        = 60,
520                 .xres           = 800,
521                 .yres           = 480,
522                 .pixclock       = KHZ2PICOS(33260),
523                 .left_margin    = 216 - 128,
524                 .hsync_len      = 128,
525                 .right_margin   = 1056 - 800 - 216,
526                 .upper_margin   = 35 - 2,
527                 .vsync_len      = 2,
528                 .lower_margin   = 525 - 480 - 35,
529                 .vmode          = FB_VMODE_NONINTERLACED,
530         },
531         {
532                 /* unnamed entry for assigning parameters parsed from 'video_mode' string */
533                 .vmode          = FB_VMODE_NONINTERLACED,
534         },
535 };
536
537 static int lcd_enabled = 1;
538 static int lcd_bl_polarity;
539
540 static int lcd_backlight_polarity(void)
541 {
542         return lcd_bl_polarity;
543 }
544
545 void lcd_enable(void)
546 {
547         /* HACK ALERT:
548          * global variable from common/lcd.c
549          * Set to 0 here to prevent messages from going to LCD
550          * rather than serial console
551          */
552         lcd_is_enabled = 0;
553
554         karo_load_splashimage(1);
555         if (lcd_enabled) {
556                 debug("Switching LCD on\n");
557                 gpio_set_value(TX28_LCD_PWR_GPIO, 1);
558                 udelay(100);
559                 gpio_set_value(TX28_LCD_RST_GPIO, 1);
560                 udelay(300000);
561                 gpio_set_value(TX28_LCD_BACKLIGHT_GPIO,
562                         lcd_backlight_polarity());
563         }
564 }
565
566 void lcd_disable(void)
567 {
568 }
569
570 void lcd_panel_disable(void)
571 {
572         if (lcd_enabled) {
573                 debug("Switching LCD off\n");
574                 gpio_set_value(TX28_LCD_BACKLIGHT_GPIO,
575                         !lcd_backlight_polarity());
576                 gpio_set_value(TX28_LCD_RST_GPIO, 0);
577                 gpio_set_value(TX28_LCD_PWR_GPIO, 0);
578         }
579 }
580
581 static const iomux_cfg_t stk5_lcd_pads[] = {
582         /* LCD RESET */
583         MX28_PAD_LCD_RESET__GPIO_3_30 | MXS_PAD_CTRL,
584         /* LCD POWER_ENABLE */
585         MX28_PAD_LCD_ENABLE__GPIO_1_31 | MXS_PAD_CTRL,
586         /* LCD Backlight (PWM) */
587         MX28_PAD_PWM0__GPIO_3_16 | MXS_PAD_CTRL,
588
589         /* Display */
590         MX28_PAD_LCD_D00__LCD_D0 | MXS_PAD_CTRL,
591         MX28_PAD_LCD_D01__LCD_D1 | MXS_PAD_CTRL,
592         MX28_PAD_LCD_D02__LCD_D2 | MXS_PAD_CTRL,
593         MX28_PAD_LCD_D03__LCD_D3 | MXS_PAD_CTRL,
594         MX28_PAD_LCD_D04__LCD_D4 | MXS_PAD_CTRL,
595         MX28_PAD_LCD_D05__LCD_D5 | MXS_PAD_CTRL,
596         MX28_PAD_LCD_D06__LCD_D6 | MXS_PAD_CTRL,
597         MX28_PAD_LCD_D07__LCD_D7 | MXS_PAD_CTRL,
598         MX28_PAD_LCD_D08__LCD_D8 | MXS_PAD_CTRL,
599         MX28_PAD_LCD_D09__LCD_D9 | MXS_PAD_CTRL,
600         MX28_PAD_LCD_D10__LCD_D10 | MXS_PAD_CTRL,
601         MX28_PAD_LCD_D11__LCD_D11 | MXS_PAD_CTRL,
602         MX28_PAD_LCD_D12__LCD_D12 | MXS_PAD_CTRL,
603         MX28_PAD_LCD_D13__LCD_D13 | MXS_PAD_CTRL,
604         MX28_PAD_LCD_D14__LCD_D14 | MXS_PAD_CTRL,
605         MX28_PAD_LCD_D15__LCD_D15 | MXS_PAD_CTRL,
606         MX28_PAD_LCD_D16__LCD_D16 | MXS_PAD_CTRL,
607         MX28_PAD_LCD_D17__LCD_D17 | MXS_PAD_CTRL,
608         MX28_PAD_LCD_D18__LCD_D18 | MXS_PAD_CTRL,
609         MX28_PAD_LCD_D19__LCD_D19 | MXS_PAD_CTRL,
610         MX28_PAD_LCD_D20__LCD_D20 | MXS_PAD_CTRL,
611         MX28_PAD_LCD_D21__LCD_D21 | MXS_PAD_CTRL,
612         MX28_PAD_LCD_D22__LCD_D22 | MXS_PAD_CTRL,
613         MX28_PAD_LCD_D23__LCD_D23 | MXS_PAD_CTRL,
614         MX28_PAD_LCD_RD_E__LCD_VSYNC | MXS_PAD_CTRL,
615         MX28_PAD_LCD_WR_RWN__LCD_HSYNC | MXS_PAD_CTRL,
616         MX28_PAD_LCD_RS__LCD_DOTCLK | MXS_PAD_CTRL,
617         MX28_PAD_LCD_CS__LCD_CS | MXS_PAD_CTRL,
618 };
619
620 static const struct gpio stk5_lcd_gpios[] = {
621         { TX28_LCD_RST_GPIO, GPIOFLAG_OUTPUT_INIT_LOW, "LCD RESET", },
622         { TX28_LCD_PWR_GPIO, GPIOFLAG_OUTPUT_INIT_LOW, "LCD POWER", },
623         { TX28_LCD_BACKLIGHT_GPIO, GPIOFLAG_OUTPUT_INIT_HIGH, "LCD BACKLIGHT", },
624 };
625
626 void lcd_ctrl_init(void *lcdbase)
627 {
628         int color_depth = 24;
629         const char *video_mode = karo_get_vmode(getenv("video_mode"));
630         const char *vm;
631         unsigned long val;
632         int refresh = 60;
633         struct fb_videomode *p = tx28_fb_modes;
634         struct fb_videomode fb_mode;
635         int xres_set = 0, yres_set = 0, bpp_set = 0, refresh_set = 0;
636
637         if (!lcd_enabled) {
638                 debug("LCD disabled\n");
639                 return;
640         }
641
642         if (had_ctrlc()) {
643                 debug("Disabling LCD\n");
644                 lcd_enabled = 0;
645                 setenv("splashimage", NULL);
646                 return;
647         }
648
649         karo_fdt_move_fdt();
650         lcd_bl_polarity = karo_fdt_get_backlight_polarity(working_fdt);
651
652         if (video_mode == NULL) {
653                 debug("Disabling LCD\n");
654                 lcd_enabled = 0;
655                 return;
656         }
657         vm = video_mode;
658         if (karo_fdt_get_fb_mode(working_fdt, video_mode, &fb_mode) == 0) {
659                 p = &fb_mode;
660                 debug("Using video mode from FDT\n");
661                 vm += strlen(vm);
662                 if (fb_mode.xres > panel_info.vl_col ||
663                         fb_mode.yres > panel_info.vl_row) {
664                         printf("video resolution from DT: %dx%d exceeds hardware limits: %dx%d\n",
665                                 fb_mode.xres, fb_mode.yres,
666                                 panel_info.vl_col, panel_info.vl_row);
667                         lcd_enabled = 0;
668                         return;
669                 }
670         }
671         if (p->name != NULL)
672                 debug("Trying compiled-in video modes\n");
673         while (p->name != NULL) {
674                 if (strcmp(p->name, vm) == 0) {
675                         debug("Using video mode: '%s'\n", p->name);
676                         vm += strlen(vm);
677                         break;
678                 }
679                 p++;
680         }
681         if (*vm != '\0')
682                 debug("Trying to decode video_mode: '%s'\n", vm);
683         while (*vm != '\0') {
684                 if (*vm >= '0' && *vm <= '9') {
685                         char *end;
686
687                         val = simple_strtoul(vm, &end, 0);
688                         if (end > vm) {
689                                 if (!xres_set) {
690                                         if (val > panel_info.vl_col)
691                                                 val = panel_info.vl_col;
692                                         p->xres = val;
693                                         panel_info.vl_col = val;
694                                         xres_set = 1;
695                                 } else if (!yres_set) {
696                                         if (val > panel_info.vl_row)
697                                                 val = panel_info.vl_row;
698                                         p->yres = val;
699                                         panel_info.vl_row = val;
700                                         yres_set = 1;
701                                 } else if (!bpp_set) {
702                                         switch (val) {
703                                         case 8:
704                                         case 16:
705                                         case 18:
706                                         case 24:
707                                                 color_depth = val;
708                                                 break;
709
710                                         default:
711                                                 printf("Invalid color depth: '%.*s' in video_mode; using default: '%u'\n",
712                                                         end - vm, vm, color_depth);
713                                         }
714                                         bpp_set = 1;
715                                 } else if (!refresh_set) {
716                                         refresh = val;
717                                         refresh_set = 1;
718                                 }
719                         }
720                         vm = end;
721                 }
722                 switch (*vm) {
723                 case '@':
724                         bpp_set = 1;
725                         /* fallthru */
726                 case '-':
727                         yres_set = 1;
728                         /* fallthru */
729                 case 'x':
730                         xres_set = 1;
731                         /* fallthru */
732                 case 'M':
733                 case 'R':
734                         vm++;
735                         break;
736
737                 default:
738                         if (*vm != '\0')
739                                 vm++;
740                 }
741         }
742         if (p->xres == 0 || p->yres == 0) {
743                 printf("Invalid video mode: %s\n", getenv("video_mode"));
744                 lcd_enabled = 0;
745                 printf("Supported video modes are:");
746                 for (p = &tx28_fb_modes[0]; p->name != NULL; p++) {
747                         printf(" %s", p->name);
748                 }
749                 printf("\n");
750                 return;
751         }
752         if (p->xres > panel_info.vl_col || p->yres > panel_info.vl_row) {
753                 printf("video resolution: %dx%d exceeds hardware limits: %dx%d\n",
754                         p->xres, p->yres, panel_info.vl_col, panel_info.vl_row);
755                 lcd_enabled = 0;
756                 return;
757         }
758         panel_info.vl_col = p->xres;
759         panel_info.vl_row = p->yres;
760
761         switch (color_depth) {
762         case 8:
763                 panel_info.vl_bpix = LCD_COLOR8;
764                 break;
765         case 16:
766                 panel_info.vl_bpix = LCD_COLOR16;
767                 break;
768         default:
769                 panel_info.vl_bpix = LCD_COLOR32;
770         }
771
772         p->pixclock = KHZ2PICOS(refresh *
773                 (p->xres + p->left_margin + p->right_margin + p->hsync_len) *
774                 (p->yres + p->upper_margin + p->lower_margin + p->vsync_len) /
775                                 1000);
776         debug("Pixel clock set to %lu.%03lu MHz\n",
777                 PICOS2KHZ(p->pixclock) / 1000, PICOS2KHZ(p->pixclock) % 1000);
778
779         if (p != &fb_mode) {
780                 int ret;
781
782                 debug("Creating new display-timing node from '%s'\n",
783                         video_mode);
784                 ret = karo_fdt_create_fb_mode(working_fdt, video_mode, p);
785                 if (ret)
786                         printf("Failed to create new display-timing node from '%s': %d\n",
787                                 video_mode, ret);
788         }
789
790         gpio_request_array(stk5_lcd_gpios, ARRAY_SIZE(stk5_lcd_gpios));
791         mxs_iomux_setup_multiple_pads(stk5_lcd_pads,
792                                 ARRAY_SIZE(stk5_lcd_pads));
793
794         debug("video format: %ux%u-%u@%u\n", p->xres, p->yres,
795                 color_depth, refresh);
796
797         if (karo_load_splashimage(0) == 0) {
798                 char vmode[128];
799
800                 /* setup env variable for mxsfb display driver */
801                 snprintf(vmode, sizeof(vmode),
802                         "x:%d,y:%d,le:%d,ri:%d,up:%d,lo:%d,hs:%d,vs:%d,sync:%d,pclk:%d,depth:%d",
803                         p->xres, p->yres, p->left_margin, p->right_margin,
804                         p->upper_margin, p->lower_margin, p->hsync_len,
805                         p->vsync_len, p->sync, p->pixclock, color_depth);
806                 setenv("videomode", vmode);
807
808                 debug("Initializing LCD controller\n");
809                 video_hw_init();
810                 setenv("videomode", NULL);
811         } else {
812                 debug("Skipping initialization of LCD controller\n");
813         }
814 }
815 #else
816 #define lcd_enabled 0
817 #endif /* CONFIG_LCD */
818
819 static void stk5_board_init(void)
820 {
821         gpio_request_array(stk5_gpios, ARRAY_SIZE(stk5_gpios));
822         mxs_iomux_setup_multiple_pads(stk5_pads, ARRAY_SIZE(stk5_pads));
823 }
824
825 static void stk5v3_board_init(void)
826 {
827         led_state = LED_STATE_INIT;
828         stk5_board_init();
829 }
830
831 static void stk5v5_board_init(void)
832 {
833         stk5_board_init();
834
835         /* init flexcan transceiver enable GPIO */
836         gpio_request_one(STK5_CAN_XCVR_GPIO, GPIOFLAG_OUTPUT_INIT_HIGH,
837                         "Flexcan Transceiver");
838         mxs_iomux_setup_pad(STK5_CAN_XCVR_GPIO);
839 }
840
841 int board_late_init(void)
842 {
843         int ret = 0;
844         const char *baseboard;
845
846         env_cleanup();
847
848         if (had_ctrlc())
849                 setenv_ulong("safeboot", 1);
850         else
851                 karo_fdt_move_fdt();
852
853         baseboard = getenv("baseboard");
854         if (!baseboard)
855                 goto exit;
856
857         printf("Baseboard: %s\n", baseboard);
858
859         if (strncmp(baseboard, "stk5", 4) == 0) {
860                 if ((strlen(baseboard) == 4) ||
861                         strcmp(baseboard, "stk5-v3") == 0) {
862                         stk5v3_board_init();
863                 } else if (strcmp(baseboard, "stk5-v5") == 0) {
864                         const char *otg_mode = getenv("otg_mode");
865
866                         if (otg_mode && strcmp(otg_mode, "host") == 0) {
867                                 printf("otg_mode='%s' is incompatible with baseboard %s; setting to 'none'\n",
868                                         otg_mode, baseboard);
869                                 setenv("otg_mode", "none");
870                         }
871                         stk5v5_board_init();
872                 } else {
873                         printf("WARNING: Unsupported STK5 board rev.: %s\n",
874                                 baseboard + 4);
875                 }
876         } else {
877                 printf("WARNING: Unsupported baseboard: '%s'\n",
878                         baseboard);
879                 if (!had_ctrlc())
880                         ret = -EINVAL;
881         }
882
883 exit:
884         tx28_init_mac();
885         clear_ctrlc();
886         return ret;
887 }
888
889 #define BOOT_CAUSE_MASK         (RTC_PERSISTENT0_EXTERNAL_RESET |       \
890                                 RTC_PERSISTENT0_ALARM_WAKE |            \
891                                 RTC_PERSISTENT0_THERMAL_RESET)
892
893 static void thermal_init(void)
894 {
895         struct mxs_power_regs *power_regs = (void *)MXS_POWER_BASE;
896         struct mxs_clkctrl_regs *clkctrl_regs = (void *)MXS_CLKCTRL_BASE;
897
898         writel(POWER_THERMAL_LOW_POWER | POWER_THERMAL_OFFSET_ADJ_ENABLE |
899                 POWER_THERMAL_OFFSET_ADJ_OFFSET(3),
900                 &power_regs->hw_power_thermal);
901
902         writel(CLKCTRL_RESET_EXTERNAL_RESET_ENABLE |
903                 CLKCTRL_RESET_THERMAL_RESET_ENABLE,
904                 &clkctrl_regs->hw_clkctrl_reset);
905 }
906
907 int checkboard(void)
908 {
909         struct mxs_power_regs *power_regs = (void *)MXS_POWER_BASE;
910         u32 pwr_sts = readl(&power_regs->hw_power_sts);
911         u32 pwrup_src = (pwr_sts >> 24) & 0x3f;
912         const char *dlm = "";
913
914         printf("Board: Ka-Ro TX28-4%sx%d\n", TX28_MOD_SUFFIX,
915                 CONFIG_SYS_SDRAM_SIZE / SZ_128M +
916                 CONFIG_SYS_NAND_BLOCKS / 2048 * 2);
917
918         printf("POWERUP Source: ");
919         if (pwrup_src & (3 << 0)) {
920                 printf("%sPSWITCH %s voltage", dlm,
921                         pwrup_src & (1 << 1) ? "HIGH" : "MID");
922                 dlm = " | ";
923         }
924         if (pwrup_src & (1 << 4)) {
925                 printf("%sRTC", dlm);
926                 dlm = " | ";
927         }
928         if (pwrup_src & (1 << 5)) {
929                 printf("%s5V", dlm);
930                 dlm = " | ";
931         }
932         printf("\n");
933
934         if (boot_cause & BOOT_CAUSE_MASK) {
935                 dlm="";
936                 printf("Last boot cause: ");
937                 if (boot_cause & RTC_PERSISTENT0_EXTERNAL_RESET) {
938                         printf("%sEXTERNAL", dlm);
939                         dlm = " | ";
940                 }
941                 if (boot_cause & RTC_PERSISTENT0_THERMAL_RESET) {
942                         printf("%sTHERMAL", dlm);
943                         dlm = " | ";
944                 }
945                 if (*dlm != '\0')
946                         printf(" RESET");
947                 if (boot_cause & RTC_PERSISTENT0_ALARM_WAKE) {
948                         printf("%sALARM WAKE", dlm);
949                         dlm = " | ";
950                 }
951                 printf("\n");
952         }
953
954         while (pwr_sts & POWER_STS_THERMAL_WARNING) {
955                 static int first = 1;
956
957                 if (first) {
958                         printf("CPU too hot to boot\n");
959                         first = 0;
960                 }
961                 if (tstc())
962                         break;
963                 pwr_sts = readl(&power_regs->hw_power_sts);
964         }
965
966         if (!(boot_cause & RTC_PERSISTENT0_THERMAL_RESET))
967                 thermal_init();
968
969         return 0;
970 }
971
972 #if defined(CONFIG_OF_BOARD_SETUP)
973 #ifdef CONFIG_FDT_FIXUP_PARTITIONS
974 #include <jffs2/jffs2.h>
975 #include <mtd_node.h>
976 static struct node_info tx28_nand_nodes[] = {
977         { "fsl,imx28-gpmi-nand", MTD_DEV_TYPE_NAND, },
978 };
979 #else
980 #define fdt_fixup_mtdparts(b,n,c) do { } while (0)
981 #endif
982
983 static const char *tx28_touchpanels[] = {
984         "ti,tsc2007",
985         "edt,edt-ft5x06",
986         "fsl,imx28-lradc",
987 };
988
989 int ft_board_setup(void *blob, bd_t *bd)
990 {
991         const char *baseboard = getenv("baseboard");
992         int stk5_v5 = baseboard != NULL && (strcmp(baseboard, "stk5-v5") == 0);
993         const char *video_mode = karo_get_vmode(getenv("video_mode"));
994         int ret;
995
996         ret = fdt_increase_size(blob, 4096);
997         if (ret) {
998                 printf("Failed to increase FDT size: %s\n", fdt_strerror(ret));
999                 return ret;
1000         }
1001 #ifdef CONFIG_TX28_S
1002         /* TX28-41xx (aka TX28S) has no external RTC
1003          * and no I2C GPIO extender
1004          */
1005         karo_fdt_remove_node(blob, "ds1339");
1006         karo_fdt_remove_node(blob, "gpio5");
1007 #endif
1008         if (stk5_v5)
1009                 karo_fdt_enable_node(blob, "stk5led", 0);
1010
1011         fdt_fixup_mtdparts(blob, tx28_nand_nodes, ARRAY_SIZE(tx28_nand_nodes));
1012
1013         karo_fdt_fixup_touchpanel(blob, tx28_touchpanels,
1014                                 ARRAY_SIZE(tx28_touchpanels));
1015         karo_fdt_fixup_usb_otg(blob, "usbotg", "fsl,usbphy", "vbus-supply");
1016         karo_fdt_fixup_flexcan(blob, stk5_v5);
1017         karo_fdt_update_fb_mode(blob, video_mode);
1018
1019         return 0;
1020 }
1021 #endif /* CONFIG_OF_BOARD_SETUP */