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