]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - board/karo/tx6/tx6ul.c
52ac588db9d4813b91ecc1bd92dd635adabf8f2e
[karo-tx-uboot.git] / board / karo / tx6 / tx6ul.c
1 /*
2  * Copyright (C) 2015 Lothar Waßmann <LW@KARO-electronics.de>
3  *
4  * SPDX-License-Identifier:     GPL-2.0+
5  *
6  */
7 #include <common.h>
8 #include <errno.h>
9 #include <libfdt.h>
10 #include <fdt_support.h>
11 #include <lcd.h>
12 #include <netdev.h>
13 #include <mmc.h>
14 #include <fsl_esdhc.h>
15 #include <video_fb.h>
16 #include <ipu.h>
17 #include <mxcfb.h>
18 #include <i2c.h>
19 #include <linux/fb.h>
20 #include <asm/io.h>
21 #include <asm/gpio.h>
22 #include <asm/arch/mx6-pins.h>
23 #include <asm/arch/clock.h>
24 #include <asm/arch/hab.h>
25 #include <asm/arch/imx-regs.h>
26 #include <asm/arch/crm_regs.h>
27 #include <asm/arch/sys_proto.h>
28
29 #include "../common/karo.h"
30 #include "pmic.h"
31
32 #define __data __attribute__((section(".data")))
33
34 #define TX6UL_FEC_RST_GPIO              IMX_GPIO_NR(5, 6)
35 #define TX6UL_FEC_PWR_GPIO              IMX_GPIO_NR(5, 7)
36 #define TX6UL_FEC_INT_GPIO              IMX_GPIO_NR(5, 5)
37
38 #define TX6UL_FEC2_RST_GPIO             IMX_GPIO_NR(4, 28)
39 #define TX6UL_FEC2_INT_GPIO             IMX_GPIO_NR(4, 27)
40
41 #define TX6UL_LED_GPIO                  IMX_GPIO_NR(5, 9)
42
43 #define TX6UL_LCD_PWR_GPIO              IMX_GPIO_NR(5, 4)
44 #define TX6UL_LCD_RST_GPIO              IMX_GPIO_NR(3, 4)
45 #define TX6UL_LCD_BACKLIGHT_GPIO        IMX_GPIO_NR(4, 16)
46
47 #define TX6UL_I2C1_SCL_GPIO             CONFIG_SOFT_I2C_GPIO_SCL
48 #define TX6UL_I2C1_SDA_GPIO             CONFIG_SOFT_I2C_GPIO_SDA
49
50 #define TX6UL_SD1_CD_GPIO               IMX_GPIO_NR(4, 14)
51
52 #ifdef CONFIG_MX6_TEMPERATURE_MIN
53 #define TEMPERATURE_MIN                 CONFIG_MX6_TEMPERATURE_MIN
54 #else
55 #define TEMPERATURE_MIN                 (-40)
56 #endif
57 #ifdef CONFIG_MX6_TEMPERATURE_HOT
58 #define TEMPERATURE_HOT                 CONFIG_MX6_TEMPERATURE_HOT
59 #else
60 #define TEMPERATURE_HOT                 80
61 #endif
62
63 DECLARE_GLOBAL_DATA_PTR;
64
65 #define MUX_CFG_SION                    IOMUX_PAD(0, 0, IOMUX_CONFIG_SION, 0, 0, 0)
66
67 char __uboot_img_end[0] __attribute__((section(".__uboot_img_end")));
68 #ifdef CONFIG_SECURE_BOOT
69 char __csf_data[0] __attribute__((section(".__csf_data")));
70 #endif
71
72 static const iomux_v3_cfg_t const tx6ul_pads[] = {
73         /* UART pads */
74 #if CONFIG_MXC_UART_BASE == UART1_BASE
75         MX6_PAD_UART1_TX_DATA__UART1_DCE_TX,
76         MX6_PAD_UART1_RX_DATA__UART1_DCE_RX,
77         MX6_PAD_UART1_RTS_B__UART1_DCE_RTS,
78         MX6_PAD_UART1_CTS_B__UART1_DCE_CTS,
79 #endif
80 #if CONFIG_MXC_UART_BASE == UART2_BASE
81         MX6_PAD_UART2_TX_DATA__UART2_DCE_TX,
82         MX6_PAD_UART2_RX_DATA__UART2_DCE_RX,
83         MX6_PAD_UART3_RX_DATA__UART2_DCE_RTS,
84         MX6_PAD_UART3_TX_DATA__UART2_DCE_CTS,
85 #endif
86 #if CONFIG_MXC_UART_BASE == UART5_BASE
87         MX6_PAD_GPIO1_IO04__UART5_DCE_TX,
88         MX6_PAD_GPIO1_IO05__UART5_DCE_RX,
89         MX6_PAD_GPIO1_IO08__UART5_DCE_RTS,
90         MX6_PAD_GPIO1_IO09__UART5_DCE_CTS,
91 #endif
92         /* internal I2C */
93         MX6_PAD_SNVS_TAMPER1__GPIO5_IO01 | MUX_CFG_SION |
94                         MUX_PAD_CTRL(PAD_CTL_DSE_240ohm), /* I2C SCL */
95         MX6_PAD_SNVS_TAMPER0__GPIO5_IO00 | MUX_CFG_SION |
96                         MUX_PAD_CTRL(PAD_CTL_DSE_240ohm | PAD_CTL_HYS |
97                         PAD_CTL_ODE), /* I2C SDA */
98
99         /* FEC PHY GPIO functions */
100         MX6_PAD_SNVS_TAMPER7__GPIO5_IO07 | MUX_CFG_SION, /* PHY POWER */
101         MX6_PAD_SNVS_TAMPER6__GPIO5_IO06 | MUX_CFG_SION, /* PHY RESET */
102         MX6_PAD_SNVS_TAMPER5__GPIO5_IO05 | MUX_PAD_CTRL(PAD_CTL_PUS_22K_UP |
103                                                         PAD_CTL_DSE_40ohm), /* PHY INT */
104 };
105
106 #define TX6_ENET_PAD_CTRL       (PAD_CTL_SPEED_HIGH |   \
107                                 PAD_CTL_DSE_48ohm |     \
108                                 PAD_CTL_PUS_100K_UP |   \
109                                 PAD_CTL_SRE_FAST)
110 #define TX6_GPIO_OUT_PAD_CTRL   (PAD_CTL_SPEED_LOW |    \
111                                 PAD_CTL_DSE_60ohm |     \
112                                 PAD_CTL_SRE_SLOW)
113 #define TX6_GPIO_IN_PAD_CTRL    (PAD_CTL_SPEED_LOW |    \
114                                 PAD_CTL_PUS_47K_UP)
115
116 static const iomux_v3_cfg_t const tx6ul_enet1_pads[] = {
117         /* FEC functions */
118         MX6_PAD_GPIO1_IO07__ENET1_MDC | MUX_PAD_CTRL(PAD_CTL_DSE_48ohm |
119                                 PAD_CTL_SPEED_MED),
120         MX6_PAD_GPIO1_IO06__ENET1_MDIO | MUX_PAD_CTRL(PAD_CTL_PUS_100K_UP |
121                                 PAD_CTL_DSE_48ohm |
122                                 PAD_CTL_SPEED_MED),
123         MX6_PAD_ENET1_TX_CLK__ENET1_REF_CLK1 | MUX_CFG_SION |
124                                 MUX_PAD_CTRL(PAD_CTL_SPEED_MED |
125                                 PAD_CTL_DSE_40ohm |
126                                 PAD_CTL_SRE_FAST),
127         MX6_PAD_ENET1_RX_ER__ENET1_RX_ER | MUX_PAD_CTRL(TX6_ENET_PAD_CTRL),
128         MX6_PAD_ENET1_RX_EN__ENET1_RX_EN | MUX_PAD_CTRL(TX6_ENET_PAD_CTRL),
129         MX6_PAD_ENET1_RX_DATA1__ENET1_RDATA01 | MUX_PAD_CTRL(TX6_ENET_PAD_CTRL),
130         MX6_PAD_ENET1_RX_DATA0__ENET1_RDATA00 | MUX_PAD_CTRL(TX6_ENET_PAD_CTRL),
131         MX6_PAD_ENET1_TX_EN__ENET1_TX_EN | MUX_PAD_CTRL(TX6_ENET_PAD_CTRL),
132         MX6_PAD_ENET1_TX_DATA1__ENET1_TDATA01 | MUX_PAD_CTRL(TX6_ENET_PAD_CTRL),
133         MX6_PAD_ENET1_TX_DATA0__ENET1_TDATA00 | MUX_PAD_CTRL(TX6_ENET_PAD_CTRL),
134 };
135
136 static const iomux_v3_cfg_t const tx6ul_enet2_pads[] = {
137         MX6_PAD_ENET2_TX_CLK__ENET2_REF_CLK2 | MUX_CFG_SION |
138                                 MUX_PAD_CTRL(PAD_CTL_SPEED_HIGH |
139                                 PAD_CTL_DSE_48ohm |
140                                 PAD_CTL_SRE_FAST),
141         MX6_PAD_ENET2_RX_ER__ENET2_RX_ER | MUX_PAD_CTRL(TX6_ENET_PAD_CTRL),
142         MX6_PAD_ENET2_RX_EN__ENET2_RX_EN | MUX_PAD_CTRL(TX6_ENET_PAD_CTRL),
143         MX6_PAD_ENET2_RX_DATA1__ENET2_RDATA01 | MUX_PAD_CTRL(TX6_ENET_PAD_CTRL),
144         MX6_PAD_ENET2_RX_DATA0__ENET2_RDATA00 | MUX_PAD_CTRL(TX6_ENET_PAD_CTRL),
145         MX6_PAD_ENET2_TX_EN__ENET2_TX_EN | MUX_PAD_CTRL(TX6_ENET_PAD_CTRL),
146         MX6_PAD_ENET2_TX_DATA1__ENET2_TDATA01 | MUX_PAD_CTRL(TX6_ENET_PAD_CTRL),
147         MX6_PAD_ENET2_TX_DATA0__ENET2_TDATA00 | MUX_PAD_CTRL(TX6_ENET_PAD_CTRL),
148 };
149
150 #define TX6_I2C_PAD_CTRL        (PAD_CTL_PUS_22K_UP |   \
151                                 PAD_CTL_SPEED_MED |     \
152                                 PAD_CTL_DSE_34ohm |     \
153                                 PAD_CTL_SRE_FAST)
154
155 static const iomux_v3_cfg_t const tx6_i2c_gpio_pads[] = {
156         /* internal I2C */
157         MX6_PAD_SNVS_TAMPER1__GPIO5_IO01 | MUX_CFG_SION |
158                         MUX_PAD_CTRL(TX6_I2C_PAD_CTRL),
159         MX6_PAD_SNVS_TAMPER0__GPIO5_IO00 | MUX_CFG_SION |
160                         MUX_PAD_CTRL(TX6_I2C_PAD_CTRL),
161 };
162
163 static const struct gpio const tx6ul_gpios[] = {
164         /* These two entries are used to forcefully reinitialize the I2C bus */
165         { TX6UL_I2C1_SCL_GPIO, GPIOFLAG_INPUT, "I2C1 SCL", },
166         { TX6UL_I2C1_SDA_GPIO, GPIOFLAG_INPUT, "I2C1 SDA", },
167
168         { TX6UL_FEC_PWR_GPIO, GPIOFLAG_OUTPUT_INIT_HIGH, "FEC PHY PWR", },
169         { TX6UL_FEC_RST_GPIO, GPIOFLAG_OUTPUT_INIT_LOW, "FEC PHY RESET", },
170         { TX6UL_FEC_INT_GPIO, GPIOFLAG_INPUT, "FEC PHY INT", },
171 };
172
173 static const struct gpio const tx6ul_fec2_gpios[] = {
174         { TX6UL_FEC2_RST_GPIO, GPIOFLAG_OUTPUT_INIT_LOW, "FEC2 PHY RESET", },
175         { TX6UL_FEC2_INT_GPIO, GPIOFLAG_INPUT, "FEC2 PHY INT", },
176 };
177
178 #define GPIO_DR 0
179 #define GPIO_DIR 4
180 #define GPIO_PSR 8
181
182 /* run with default environment */
183 static void tx6_i2c_recover(void)
184 {
185         int i;
186         int bad = 0;
187 #define SCL_BIT         (1 << (TX6UL_I2C1_SCL_GPIO % 32))
188 #define SDA_BIT         (1 << (TX6UL_I2C1_SDA_GPIO % 32))
189 #define I2C_GPIO_BASE   (GPIO1_BASE_ADDR + TX6UL_I2C1_SCL_GPIO / 32 * 0x4000)
190
191         if ((readl(I2C_GPIO_BASE + GPIO_PSR) &
192                         (SCL_BIT | SDA_BIT)) == (SCL_BIT | SDA_BIT))
193                 return;
194
195         debug("Clearing I2C bus\n");
196         if (!(readl(I2C_GPIO_BASE + GPIO_PSR) & SCL_BIT)) {
197                 printf("I2C SCL stuck LOW\n");
198                 bad++;
199
200                 writel(readl(I2C_GPIO_BASE + GPIO_DR) | SCL_BIT,
201                         I2C_GPIO_BASE + GPIO_DR);
202                 writel(readl(I2C_GPIO_BASE + GPIO_DIR) | SCL_BIT,
203                         I2C_GPIO_BASE + GPIO_DIR);
204         }
205         if (!(readl(I2C_GPIO_BASE + GPIO_PSR) & SDA_BIT)) {
206                 printf("I2C SDA stuck LOW\n");
207                 bad++;
208
209                 writel(readl(I2C_GPIO_BASE + GPIO_DIR) & ~SDA_BIT,
210                         I2C_GPIO_BASE + GPIO_DIR);
211                 writel(readl(I2C_GPIO_BASE + GPIO_DR) | SCL_BIT,
212                         I2C_GPIO_BASE + GPIO_DR);
213                 writel(readl(I2C_GPIO_BASE + GPIO_DIR) | SCL_BIT,
214                         I2C_GPIO_BASE + GPIO_DIR);
215
216                 imx_iomux_v3_setup_multiple_pads(tx6_i2c_gpio_pads,
217                                                 ARRAY_SIZE(tx6_i2c_gpio_pads));
218                 udelay(10);
219
220                 for (i = 0; i < 18; i++) {
221                         u32 reg = readl(I2C_GPIO_BASE + GPIO_DR) ^ SCL_BIT;
222
223                         debug("%sing SCL\n", (reg & SCL_BIT) ? "Sett" : "Clear");
224                         writel(reg, I2C_GPIO_BASE + GPIO_DR);
225                         udelay(10);
226                         if (reg & SCL_BIT &&
227                                 readl(I2C_GPIO_BASE + GPIO_PSR) & SDA_BIT)
228                                 break;
229                 }
230         }
231         if (bad) {
232                 u32 reg = readl(I2C_GPIO_BASE + GPIO_PSR);
233
234                 if ((reg & (SCL_BIT | SDA_BIT)) == (SCL_BIT | SDA_BIT)) {
235                         printf("I2C bus recovery succeeded\n");
236                 } else {
237                         printf("I2C bus recovery FAILED: %08x:%08x\n", reg,
238                                 SCL_BIT | SDA_BIT);
239                 }
240         }
241 }
242
243 /* placed in section '.data' to prevent overwriting relocation info
244  * overlayed with bss
245  */
246 static u32 wrsr __data;
247
248 #define WRSR_POR                        (1 << 4)
249 #define WRSR_TOUT                       (1 << 1)
250 #define WRSR_SFTW                       (1 << 0)
251
252 static void print_reset_cause(void)
253 {
254         struct src *src_regs = (struct src *)SRC_BASE_ADDR;
255         void __iomem *wdt_base = (void __iomem *)WDOG1_BASE_ADDR;
256         u32 srsr;
257         char *dlm = "";
258
259         printf("Reset cause: ");
260
261         srsr = readl(&src_regs->srsr);
262         wrsr = readw(wdt_base + 4);
263
264         if (wrsr & WRSR_POR) {
265                 printf("%sPOR", dlm);
266                 dlm = " | ";
267         }
268         if (srsr & 0x00004) {
269                 printf("%sCSU", dlm);
270                 dlm = " | ";
271         }
272         if (srsr & 0x00008) {
273                 printf("%sIPP USER", dlm);
274                 dlm = " | ";
275         }
276         if (srsr & 0x00010) {
277                 if (wrsr & WRSR_SFTW) {
278                         printf("%sSOFT", dlm);
279                         dlm = " | ";
280                 }
281                 if (wrsr & WRSR_TOUT) {
282                         printf("%sWDOG", dlm);
283                         dlm = " | ";
284                 }
285         }
286         if (srsr & 0x00020) {
287                 printf("%sJTAG HIGH-Z", dlm);
288                 dlm = " | ";
289         }
290         if (srsr & 0x00040) {
291                 printf("%sJTAG SW", dlm);
292                 dlm = " | ";
293         }
294         if (srsr & 0x10000) {
295                 printf("%sWARM BOOT", dlm);
296                 dlm = " | ";
297         }
298         if (dlm[0] == '\0')
299                 printf("unknown");
300
301         printf("\n");
302 }
303
304 #ifdef CONFIG_IMX6_THERMAL
305 #include <thermal.h>
306 #include <imx_thermal.h>
307 #include <fuse.h>
308
309 static void print_temperature(void)
310 {
311         struct udevice *thermal_dev;
312         int cpu_tmp, minc, maxc, ret;
313         char const *grade_str;
314         static u32 __data thermal_calib;
315
316         puts("Temperature: ");
317         switch (get_cpu_temp_grade(&minc, &maxc)) {
318         case TEMP_AUTOMOTIVE:
319                 grade_str = "Automotive";
320                 break;
321         case TEMP_INDUSTRIAL:
322                 grade_str = "Industrial";
323                 break;
324         case TEMP_EXTCOMMERCIAL:
325                 grade_str = "Extended Commercial";
326                 break;
327         default:
328                 grade_str = "Commercial";
329         }
330         printf("%s grade (%dC to %dC)", grade_str, minc, maxc);
331         ret = uclass_get_device(UCLASS_THERMAL, 0, &thermal_dev);
332         if (ret == 0) {
333                 ret = thermal_get_temp(thermal_dev, &cpu_tmp);
334
335                 if (ret == 0)
336                         printf(" at %dC", cpu_tmp);
337                 else
338                         puts(" - failed to read sensor data");
339         } else {
340                 puts(" - no sensor device found");
341         }
342
343         if (fuse_read(1, 6, &thermal_calib) == 0) {
344                 printf(" - calibration data 0x%08x\n", thermal_calib);
345         } else {
346                 puts(" - Failed to read thermal calib fuse\n");
347         }
348 }
349 #else
350 static inline void print_temperature(void)
351 {
352 }
353 #endif
354
355 int checkboard(void)
356 {
357         u32 cpurev = get_cpu_rev();
358         char *cpu_str = "?";
359
360         if (is_cpu_type(MXC_CPU_MX6SL))
361                 cpu_str = "SL";
362         else if (is_cpu_type(MXC_CPU_MX6DL))
363                 cpu_str = "DL";
364         else if (is_cpu_type(MXC_CPU_MX6SOLO))
365                 cpu_str = "SOLO";
366         else if (is_cpu_type(MXC_CPU_MX6Q))
367                 cpu_str = "Q";
368         else if (is_cpu_type(MXC_CPU_MX6UL))
369                 cpu_str = "UL";
370
371         printf("CPU:   Freescale i.MX6%s rev%d.%d at %d MHz\n",
372                 cpu_str,
373                 (cpurev & 0x000F0) >> 4,
374                 (cpurev & 0x0000F) >> 0,
375                 mxc_get_clock(MXC_ARM_CLK) / 1000000);
376
377         print_temperature();
378         print_reset_cause();
379 #ifdef CONFIG_MX6_TEMPERATURE_HOT
380         check_cpu_temperature(1);
381 #endif
382         tx6_i2c_recover();
383         return 0;
384 }
385
386 /* serial port not initialized at this point */
387 int board_early_init_f(void)
388 {
389         return 0;
390 }
391
392 #ifndef CONFIG_MX6_TEMPERATURE_HOT
393 static bool tx6_temp_check_enabled = true;
394 #else
395 #define tx6_temp_check_enabled  0
396 #endif
397
398 static inline u8 tx6ul_mem_suffix(void)
399 {
400 #ifdef CONFIG_TX6_NAND
401         return '0';
402 #else
403         return '1';
404 #endif
405 }
406
407 /* PMIC settings */
408 #define VDD_RTC_VAL             rn5t_mV_to_regval_rtc(3000)
409 #define VDD_CORE_VAL            rn5t_mV_to_regval(1300)         /* DCDC1 */
410 #define VDD_CORE_VAL_LP         rn5t_mV_to_regval(900)
411 #define VDD_DDR_VAL             rn5t_mV_to_regval(1350)         /* DCDC3 */
412 #define VDD_DDR_VAL_LP          rn5t_mV_to_regval(1350)
413 #define VDD_HIGH_VAL            rn5t_mV_to_regval(3300)         /* DCDC4 */
414 #define VDD_HIGH_VAL_LP         rn5t_mV_to_regval(3300)
415 #define VDD_CSI_VAL             rn5t_mV_to_regval2(3300)        /* LDO4 */
416 #define VDD_CSI_VAL_LP          rn5t_mV_to_regval2(3300)
417
418 static struct pmic_regs rn5t567_regs[] = {
419         { RN5T567_NOETIMSET, NOETIMSET_DIS_OFF_NOE_TIM | 0x5, },
420         { RN5T567_DC2CTL, DC2_DC2DIS, },
421         { RN5T567_DC1DAC, VDD_CORE_VAL, },
422         { RN5T567_DC3DAC, VDD_DDR_VAL, },
423         { RN5T567_DC4DAC, VDD_HIGH_VAL, },
424         { RN5T567_DC1DAC_SLP, VDD_CORE_VAL_LP, },
425         { RN5T567_DC3DAC_SLP, VDD_DDR_VAL_LP, },
426         { RN5T567_DC4DAC_SLP, VDD_HIGH_VAL_LP, },
427         { RN5T567_LDOEN1, 0x01f, ~0x1f, },
428         { RN5T567_LDOEN2, 0x10, ~0x30, },
429         { RN5T567_LDODIS, 0x00, },
430         { RN5T567_LDO4DAC, VDD_CSI_VAL, },
431         { RN5T567_LDORTC1DAC, VDD_RTC_VAL, },
432         { RN5T567_LDORTC1_SLOT, 0x0f, ~0x3f, },
433 };
434
435 static int pmic_addr __maybe_unused = 0x33;
436
437 int board_init(void)
438 {
439         int ret;
440         u32 cpurev = get_cpu_rev();
441
442         debug("%s@%d: \n", __func__, __LINE__);
443
444         printf("Board: Ka-Ro TXUL-%c01%c\n",
445                 ((cpurev &0xff) > 0x10) ? '5' : '0',
446                 tx6ul_mem_suffix());
447
448         get_hab_status();
449
450         ret = gpio_request_array(tx6ul_gpios, ARRAY_SIZE(tx6ul_gpios));
451         if (ret < 0) {
452                 printf("Failed to request tx6ul_gpios: %d\n", ret);
453         }
454         imx_iomux_v3_setup_multiple_pads(tx6ul_pads, ARRAY_SIZE(tx6ul_pads));
455
456         /* Address of boot parameters */
457         gd->bd->bi_boot_params = PHYS_SDRAM_1 + 0x1000;
458         gd->bd->bi_arch_number = -1;
459
460         if (ctrlc() || (wrsr & WRSR_TOUT)) {
461                 if (wrsr & WRSR_TOUT)
462                         printf("WDOG RESET detected; Skipping PMIC setup\n");
463                 else
464                         printf("<CTRL-C> detected; safeboot enabled\n");
465 #ifndef CONFIG_MX6_TEMPERATURE_HOT
466                 tx6_temp_check_enabled = false;
467 #endif
468                 return 0;
469         }
470
471         ret = tx6_pmic_init(pmic_addr, rn5t567_regs, ARRAY_SIZE(rn5t567_regs));
472         if (ret) {
473                 printf("Failed to setup PMIC voltages: %d\n", ret);
474                 hang();
475         }
476         return 0;
477 }
478
479 int dram_init(void)
480 {
481         debug("%s@%d: \n", __func__, __LINE__);
482
483         /* dram_init must store complete ramsize in gd->ram_size */
484         gd->ram_size = get_ram_size((void *)CONFIG_SYS_SDRAM_BASE,
485                                 PHYS_SDRAM_1_SIZE * CONFIG_NR_DRAM_BANKS);
486         return 0;
487 }
488
489 void dram_init_banksize(void)
490 {
491         debug("%s@%d: \n", __func__, __LINE__);
492
493         gd->bd->bi_dram[0].start = PHYS_SDRAM_1;
494         gd->bd->bi_dram[0].size = get_ram_size((void *)PHYS_SDRAM_1,
495                         PHYS_SDRAM_1_SIZE);
496 #if CONFIG_NR_DRAM_BANKS > 1
497         gd->bd->bi_dram[1].start = PHYS_SDRAM_2;
498         gd->bd->bi_dram[1].size = get_ram_size((void *)PHYS_SDRAM_2,
499                         PHYS_SDRAM_2_SIZE);
500 #endif
501 }
502
503 #ifdef  CONFIG_FSL_ESDHC
504 #define TX6_SD_PAD_CTRL         (PAD_CTL_PUS_47K_UP |   \
505                                 PAD_CTL_SPEED_MED |     \
506                                 PAD_CTL_DSE_40ohm |     \
507                                 PAD_CTL_SRE_FAST)
508
509 static const iomux_v3_cfg_t mmc0_pads[] = {
510         MX6_PAD_SD1_CMD__USDHC1_CMD | MUX_PAD_CTRL(TX6_SD_PAD_CTRL),
511         MX6_PAD_SD1_CLK__USDHC1_CLK | MUX_PAD_CTRL(TX6_SD_PAD_CTRL),
512         MX6_PAD_SD1_DATA0__USDHC1_DATA0 | MUX_PAD_CTRL(TX6_SD_PAD_CTRL),
513         MX6_PAD_SD1_DATA1__USDHC1_DATA1 | MUX_PAD_CTRL(TX6_SD_PAD_CTRL),
514         MX6_PAD_SD1_DATA2__USDHC1_DATA2 | MUX_PAD_CTRL(TX6_SD_PAD_CTRL),
515         MX6_PAD_SD1_DATA3__USDHC1_DATA3 | MUX_PAD_CTRL(TX6_SD_PAD_CTRL),
516         /* SD1 CD */
517         MX6_PAD_NAND_CE1_B__GPIO4_IO14 | MUX_PAD_CTRL(TX6_SD_PAD_CTRL),
518 };
519
520 #ifdef CONFIG_TX6_EMMC
521 static const iomux_v3_cfg_t mmc1_pads[] = {
522         MX6_PAD_NAND_WE_B__USDHC2_CMD | MUX_PAD_CTRL(TX6_SD_PAD_CTRL),
523         MX6_PAD_NAND_RE_B__USDHC2_CLK | MUX_PAD_CTRL(TX6_SD_PAD_CTRL),
524         MX6_PAD_NAND_DATA00__USDHC2_DATA0 | MUX_PAD_CTRL(TX6_SD_PAD_CTRL),
525         MX6_PAD_NAND_DATA01__USDHC2_DATA1 | MUX_PAD_CTRL(TX6_SD_PAD_CTRL),
526         MX6_PAD_NAND_DATA02__USDHC2_DATA2 | MUX_PAD_CTRL(TX6_SD_PAD_CTRL),
527         MX6_PAD_NAND_DATA03__USDHC2_DATA3 | MUX_PAD_CTRL(TX6_SD_PAD_CTRL),
528         /* eMMC RESET */
529         MX6_PAD_NAND_ALE__USDHC2_RESET_B | MUX_PAD_CTRL(PAD_CTL_PUS_47K_UP |
530                                                 PAD_CTL_DSE_40ohm),
531 };
532 #endif
533
534 static struct tx6_esdhc_cfg {
535         const iomux_v3_cfg_t *pads;
536         int num_pads;
537         enum mxc_clock clkid;
538         struct fsl_esdhc_cfg cfg;
539         int cd_gpio;
540 } tx6ul_esdhc_cfg[] = {
541 #ifdef CONFIG_TX6_EMMC
542         {
543                 .pads = mmc1_pads,
544                 .num_pads = ARRAY_SIZE(mmc1_pads),
545                 .clkid = MXC_ESDHC2_CLK,
546                 .cfg = {
547                         .esdhc_base = (void __iomem *)USDHC2_BASE_ADDR,
548                         .max_bus_width = 4,
549                 },
550                 .cd_gpio = -EINVAL,
551         },
552 #endif
553         {
554                 .pads = mmc0_pads,
555                 .num_pads = ARRAY_SIZE(mmc0_pads),
556                 .clkid = MXC_ESDHC_CLK,
557                 .cfg = {
558                         .esdhc_base = (void __iomem *)USDHC1_BASE_ADDR,
559                         .max_bus_width = 4,
560                 },
561                 .cd_gpio = TX6UL_SD1_CD_GPIO,
562         },
563 };
564
565 static inline struct tx6_esdhc_cfg *to_tx6_esdhc_cfg(struct fsl_esdhc_cfg *cfg)
566 {
567         return container_of(cfg, struct tx6_esdhc_cfg, cfg);
568 }
569
570 int board_mmc_getcd(struct mmc *mmc)
571 {
572         struct tx6_esdhc_cfg *cfg = to_tx6_esdhc_cfg(mmc->priv);
573
574         if (cfg->cd_gpio < 0)
575                 return 1;
576
577         debug("SD card %d is %spresent (GPIO %d)\n",
578                 cfg - tx6ul_esdhc_cfg,
579                 gpio_get_value(cfg->cd_gpio) ? "NOT " : "",
580                 cfg->cd_gpio);
581         return !gpio_get_value(cfg->cd_gpio);
582 }
583
584 int board_mmc_init(bd_t *bis)
585 {
586         int i;
587
588         debug("%s@%d: \n", __func__, __LINE__);
589
590 #ifndef CONFIG_ENV_IS_IN_MMC
591         if (!(gd->flags & GD_FLG_ENV_READY)) {
592                 printf("deferred ...");
593                 return 0;
594         }
595 #endif
596         for (i = 0; i < ARRAY_SIZE(tx6ul_esdhc_cfg); i++) {
597                 struct mmc *mmc;
598                 struct tx6_esdhc_cfg *cfg = &tx6ul_esdhc_cfg[i];
599                 int ret;
600
601                 cfg->cfg.sdhc_clk = mxc_get_clock(cfg->clkid);
602                 imx_iomux_v3_setup_multiple_pads(cfg->pads, cfg->num_pads);
603
604                 if (cfg->cd_gpio >= 0) {
605                         ret = gpio_request_one(cfg->cd_gpio,
606                                         GPIOFLAG_INPUT, "MMC CD");
607                         if (ret) {
608                                 printf("Error %d requesting GPIO%d_%d\n",
609                                         ret, cfg->cd_gpio / 32, cfg->cd_gpio % 32);
610                                 continue;
611                         }
612                 }
613
614                 debug("%s: Initializing MMC slot %d\n", __func__, i);
615                 fsl_esdhc_initialize(bis, &cfg->cfg);
616
617                 mmc = find_mmc_device(i);
618                 if (mmc == NULL)
619                         continue;
620                 if (board_mmc_getcd(mmc))
621                         mmc_init(mmc);
622         }
623         return 0;
624 }
625 #endif /* CONFIG_FSL_ESDHC */
626
627 enum {
628         LED_STATE_INIT = -1,
629         LED_STATE_OFF,
630         LED_STATE_ON,
631         LED_STATE_ERR,
632 };
633
634 static inline int calc_blink_rate(void)
635 {
636         if (!tx6_temp_check_enabled)
637                 return CONFIG_SYS_HZ;
638
639         return CONFIG_SYS_HZ + CONFIG_SYS_HZ / 10 -
640                 (check_cpu_temperature(0) - TEMPERATURE_MIN) * CONFIG_SYS_HZ /
641                 (TEMPERATURE_HOT - TEMPERATURE_MIN);
642 }
643
644 void show_activity(int arg)
645 {
646         static int led_state = LED_STATE_INIT;
647         static int blink_rate;
648         static ulong last;
649         int ret;
650
651         switch (led_state) {
652         case LED_STATE_ERR:
653                 return;
654
655         case LED_STATE_INIT:
656                 last = get_timer(0);
657                 ret = gpio_set_value(TX6UL_LED_GPIO, 1);
658                 if (ret)
659                         led_state = LED_STATE_ERR;
660                 else
661                         led_state = LED_STATE_ON;
662                 blink_rate = calc_blink_rate();
663                 break;
664
665         case LED_STATE_ON:
666         case LED_STATE_OFF:
667                 if (get_timer(last) > blink_rate) {
668                         blink_rate = calc_blink_rate();
669                         last = get_timer_masked();
670                         if (led_state == LED_STATE_ON) {
671                                 gpio_set_value(TX6UL_LED_GPIO, 0);
672                         } else {
673                                 gpio_set_value(TX6UL_LED_GPIO, 1);
674                         }
675                         led_state = 1 - led_state;
676                 }
677                 break;
678         }
679 }
680
681 static const iomux_v3_cfg_t stk5_pads[] = {
682         /* SW controlled LED on STK5 baseboard */
683         MX6_PAD_SNVS_TAMPER9__GPIO5_IO09,
684
685         /* I2C bus on DIMM pins 40/41 */
686         MX6_PAD_GPIO1_IO01__I2C2_SDA | MUX_MODE_SION | MUX_PAD_CTRL(TX6_I2C_PAD_CTRL),
687         MX6_PAD_GPIO1_IO00__I2C2_SCL | MUX_MODE_SION | MUX_PAD_CTRL(TX6_I2C_PAD_CTRL),
688
689         /* TSC200x PEN IRQ */
690         MX6_PAD_JTAG_TMS__GPIO1_IO11 | MUX_PAD_CTRL(TX6_GPIO_IN_PAD_CTRL),
691
692         /* EDT-FT5x06 Polytouch panel */
693         MX6_PAD_SNVS_TAMPER2__GPIO5_IO02 | MUX_PAD_CTRL(TX6_GPIO_IN_PAD_CTRL), /* IRQ */
694         MX6_PAD_SNVS_TAMPER3__GPIO5_IO03 | MUX_PAD_CTRL(TX6_GPIO_OUT_PAD_CTRL), /* RESET */
695         MX6_PAD_SNVS_TAMPER8__GPIO5_IO08 | MUX_PAD_CTRL(TX6_GPIO_OUT_PAD_CTRL), /* WAKE */
696
697         /* USBH1 */
698         MX6_PAD_GPIO1_IO02__USB_OTG2_PWR | MUX_PAD_CTRL(TX6_GPIO_OUT_PAD_CTRL), /* VBUSEN */
699         MX6_PAD_GPIO1_IO03__USB_OTG2_OC | MUX_PAD_CTRL(TX6_GPIO_IN_PAD_CTRL), /* OC */
700
701         /* USBOTG */
702         MX6_PAD_UART3_CTS_B__GPIO1_IO26 | MUX_PAD_CTRL(TX6_GPIO_OUT_PAD_CTRL), /* VBUSEN */
703         MX6_PAD_UART3_RTS_B__GPIO1_IO27 | MUX_PAD_CTRL(TX6_GPIO_IN_PAD_CTRL), /* OC */
704 };
705
706 static const struct gpio stk5_gpios[] = {
707         { TX6UL_LED_GPIO, GPIOFLAG_OUTPUT_INIT_LOW, "HEARTBEAT LED", },
708
709         { IMX_GPIO_NR(1, 27), GPIOFLAG_INPUT, "USBOTG OC", },
710         { IMX_GPIO_NR(1, 26), GPIOFLAG_OUTPUT_INIT_LOW, "USBOTG VBUS enable", },
711 };
712
713 #ifdef CONFIG_LCD
714 vidinfo_t panel_info = {
715         /* set to max. size supported by SoC */
716         .vl_col = 4096,
717         .vl_row = 1024,
718
719         .vl_bpix = LCD_COLOR32,    /* Bits per pixel, 0: 1bpp, 1: 2bpp, 2: 4bpp, 3: 8bpp ... */
720 };
721
722 static struct fb_videomode tx6_fb_modes[] = {
723 #ifndef CONFIG_SYS_LVDS_IF
724         {
725                 /* Standard VGA timing */
726                 .name           = "VGA",
727                 .refresh        = 60,
728                 .xres           = 640,
729                 .yres           = 480,
730                 .pixclock       = KHZ2PICOS(25175),
731                 .left_margin    = 48,
732                 .hsync_len      = 96,
733                 .right_margin   = 16,
734                 .upper_margin   = 31,
735                 .vsync_len      = 2,
736                 .lower_margin   = 12,
737                 .sync           = FB_SYNC_CLK_LAT_FALL,
738         },
739         {
740                 /* Emerging ETV570 640 x 480 display. Syncs low active,
741                  * DE high active, 115.2 mm x 86.4 mm display area
742                  * VGA compatible timing
743                  */
744                 .name           = "ETV570",
745                 .refresh        = 60,
746                 .xres           = 640,
747                 .yres           = 480,
748                 .pixclock       = KHZ2PICOS(25175),
749                 .left_margin    = 114,
750                 .hsync_len      = 30,
751                 .right_margin   = 16,
752                 .upper_margin   = 32,
753                 .vsync_len      = 3,
754                 .lower_margin   = 10,
755                 .sync           = FB_SYNC_CLK_LAT_FALL,
756         },
757         {
758                 /* Emerging ET0350G0DH6 320 x 240 display.
759                  * 70.08 mm x 52.56 mm display area.
760                  */
761                 .name           = "ET0350",
762                 .refresh        = 60,
763                 .xres           = 320,
764                 .yres           = 240,
765                 .pixclock       = KHZ2PICOS(6500),
766                 .left_margin    = 68 - 34,
767                 .hsync_len      = 34,
768                 .right_margin   = 20,
769                 .upper_margin   = 18 - 3,
770                 .vsync_len      = 3,
771                 .lower_margin   = 4,
772                 .sync           = FB_SYNC_CLK_LAT_FALL,
773         },
774         {
775                 /* Emerging ET0430G0DH6 480 x 272 display.
776                  * 95.04 mm x 53.856 mm display area.
777                  */
778                 .name           = "ET0430",
779                 .refresh        = 60,
780                 .xres           = 480,
781                 .yres           = 272,
782                 .pixclock       = KHZ2PICOS(9000),
783                 .left_margin    = 2,
784                 .hsync_len      = 41,
785                 .right_margin   = 2,
786                 .upper_margin   = 2,
787                 .vsync_len      = 10,
788                 .lower_margin   = 2,
789         },
790         {
791                 /* Emerging ET0500G0DH6 800 x 480 display.
792                  * 109.6 mm x 66.4 mm display area.
793                  */
794                 .name           = "ET0500",
795                 .refresh        = 60,
796                 .xres           = 800,
797                 .yres           = 480,
798                 .pixclock       = KHZ2PICOS(33260),
799                 .left_margin    = 216 - 128,
800                 .hsync_len      = 128,
801                 .right_margin   = 1056 - 800 - 216,
802                 .upper_margin   = 35 - 2,
803                 .vsync_len      = 2,
804                 .lower_margin   = 525 - 480 - 35,
805                 .sync           = FB_SYNC_CLK_LAT_FALL,
806         },
807         {
808                 /* Emerging ETQ570G0DH6 320 x 240 display.
809                  * 115.2 mm x 86.4 mm display area.
810                  */
811                 .name           = "ETQ570",
812                 .refresh        = 60,
813                 .xres           = 320,
814                 .yres           = 240,
815                 .pixclock       = KHZ2PICOS(6400),
816                 .left_margin    = 38,
817                 .hsync_len      = 30,
818                 .right_margin   = 30,
819                 .upper_margin   = 16, /* 15 according to datasheet */
820                 .vsync_len      = 3, /* TVP -> 1>x>5 */
821                 .lower_margin   = 4, /* 4.5 according to datasheet */
822                 .sync           = FB_SYNC_CLK_LAT_FALL,
823         },
824         {
825                 /* Emerging ET0700G0DH6 800 x 480 display.
826                  * 152.4 mm x 91.44 mm display area.
827                  */
828                 .name           = "ET0700",
829                 .refresh        = 60,
830                 .xres           = 800,
831                 .yres           = 480,
832                 .pixclock       = KHZ2PICOS(33260),
833                 .left_margin    = 216 - 128,
834                 .hsync_len      = 128,
835                 .right_margin   = 1056 - 800 - 216,
836                 .upper_margin   = 35 - 2,
837                 .vsync_len      = 2,
838                 .lower_margin   = 525 - 480 - 35,
839                 .sync           = FB_SYNC_CLK_LAT_FALL,
840         },
841         {
842                 /* Emerging ET070001DM6 800 x 480 display.
843                  * 152.4 mm x 91.44 mm display area.
844                  */
845                 .name           = "ET070001DM6",
846                 .refresh        = 60,
847                 .xres           = 800,
848                 .yres           = 480,
849                 .pixclock       = KHZ2PICOS(33260),
850                 .left_margin    = 216 - 128,
851                 .hsync_len      = 128,
852                 .right_margin   = 1056 - 800 - 216,
853                 .upper_margin   = 35 - 2,
854                 .vsync_len      = 2,
855                 .lower_margin   = 525 - 480 - 35,
856                 .sync           = 0,
857         },
858 #else
859         {
860                 /* HannStar HSD100PXN1
861                  * 202.7m mm x 152.06 mm display area.
862                  */
863                 .name           = "HSD100PXN1",
864                 .refresh        = 60,
865                 .xres           = 1024,
866                 .yres           = 768,
867                 .pixclock       = KHZ2PICOS(65000),
868                 .left_margin    = 0,
869                 .hsync_len      = 0,
870                 .right_margin   = 320,
871                 .upper_margin   = 0,
872                 .vsync_len      = 0,
873                 .lower_margin   = 38,
874                 .sync           = FB_SYNC_CLK_LAT_FALL,
875         },
876 #endif
877         {
878                 /* unnamed entry for assigning parameters parsed from 'video_mode' string */
879                 .refresh        = 60,
880                 .left_margin    = 48,
881                 .hsync_len      = 96,
882                 .right_margin   = 16,
883                 .upper_margin   = 31,
884                 .vsync_len      = 2,
885                 .lower_margin   = 12,
886                 .sync           = FB_SYNC_CLK_LAT_FALL,
887         },
888 };
889
890 static int lcd_enabled = 1;
891 static int lcd_bl_polarity;
892
893 static int lcd_backlight_polarity(void)
894 {
895         return lcd_bl_polarity;
896 }
897
898 static const iomux_v3_cfg_t stk5_lcd_pads[] = {
899 #ifdef CONFIG_LCD
900         /* LCD RESET */
901         MX6_PAD_LCD_RESET__GPIO3_IO04 | MUX_PAD_CTRL(TX6_GPIO_OUT_PAD_CTRL),
902         /* LCD POWER_ENABLE */
903         MX6_PAD_SNVS_TAMPER4__GPIO5_IO04 | MUX_PAD_CTRL(TX6_GPIO_OUT_PAD_CTRL),
904         /* LCD Backlight (PWM) */
905         MX6_PAD_NAND_DQS__GPIO4_IO16 | MUX_PAD_CTRL(TX6_GPIO_OUT_PAD_CTRL),
906         /* Display */
907         MX6_PAD_LCD_DATA00__LCDIF_DATA00,
908         MX6_PAD_LCD_DATA01__LCDIF_DATA01,
909         MX6_PAD_LCD_DATA02__LCDIF_DATA02,
910         MX6_PAD_LCD_DATA03__LCDIF_DATA03,
911         MX6_PAD_LCD_DATA04__LCDIF_DATA04,
912         MX6_PAD_LCD_DATA05__LCDIF_DATA05,
913         MX6_PAD_LCD_DATA06__LCDIF_DATA06,
914         MX6_PAD_LCD_DATA07__LCDIF_DATA07,
915         MX6_PAD_LCD_DATA08__LCDIF_DATA08,
916         MX6_PAD_LCD_DATA09__LCDIF_DATA09,
917         MX6_PAD_LCD_DATA10__LCDIF_DATA10,
918         MX6_PAD_LCD_DATA11__LCDIF_DATA11,
919         MX6_PAD_LCD_DATA12__LCDIF_DATA12,
920         MX6_PAD_LCD_DATA13__LCDIF_DATA13,
921         MX6_PAD_LCD_DATA14__LCDIF_DATA14,
922         MX6_PAD_LCD_DATA15__LCDIF_DATA15,
923         MX6_PAD_LCD_DATA16__LCDIF_DATA16,
924         MX6_PAD_LCD_DATA17__LCDIF_DATA17,
925         MX6_PAD_LCD_DATA18__LCDIF_DATA18,
926         MX6_PAD_LCD_DATA19__LCDIF_DATA19,
927         MX6_PAD_LCD_DATA20__LCDIF_DATA20,
928         MX6_PAD_LCD_DATA21__LCDIF_DATA21,
929         MX6_PAD_LCD_DATA22__LCDIF_DATA22,
930         MX6_PAD_LCD_DATA23__LCDIF_DATA23,
931         MX6_PAD_LCD_HSYNC__LCDIF_HSYNC, /* HSYNC */
932         MX6_PAD_LCD_VSYNC__LCDIF_VSYNC, /* VSYNC */
933         MX6_PAD_LCD_ENABLE__LCDIF_ENABLE, /* OE_ACD */
934         MX6_PAD_LCD_CLK__LCDIF_CLK, /* LSCLK */
935 #endif
936 };
937
938 static const struct gpio stk5_lcd_gpios[] = {
939         { TX6UL_LCD_RST_GPIO, GPIOFLAG_OUTPUT_INIT_LOW, "LCD RESET", },
940         { TX6UL_LCD_PWR_GPIO, GPIOFLAG_OUTPUT_INIT_LOW, "LCD POWER", },
941         { TX6UL_LCD_BACKLIGHT_GPIO, GPIOFLAG_OUTPUT_INIT_HIGH, "LCD BACKLIGHT", },
942 };
943
944 /* run with valid env from NAND/eMMC */
945 void lcd_enable(void)
946 {
947         /* HACK ALERT:
948          * global variable from common/lcd.c
949          * Set to 0 here to prevent messages from going to LCD
950          * rather than serial console
951          */
952         lcd_is_enabled = 0;
953
954         if (lcd_enabled) {
955                 karo_load_splashimage(1);
956
957                 debug("Switching LCD on\n");
958                 gpio_set_value(TX6UL_LCD_PWR_GPIO, 1);
959                 udelay(100);
960                 gpio_set_value(TX6UL_LCD_RST_GPIO, 1);
961                 udelay(300000);
962                 gpio_set_value(TX6UL_LCD_BACKLIGHT_GPIO,
963                         lcd_backlight_polarity());
964         }
965 }
966
967 static void lcd_disable(void)
968 {
969         if (lcd_enabled) {
970                 printf("Disabling LCD\n");
971                 panel_info.vl_row = 0;
972                 lcd_enabled = 0;
973         }
974 }
975
976 void lcd_ctrl_init(void *lcdbase)
977 {
978         int color_depth = 24;
979         const char *video_mode = karo_get_vmode(getenv("video_mode"));
980         const char *vm;
981         unsigned long val;
982         int refresh = 60;
983         struct fb_videomode *p = &tx6_fb_modes[0];
984         struct fb_videomode fb_mode;
985         int xres_set = 0, yres_set = 0, bpp_set = 0, refresh_set = 0;
986
987         if (!lcd_enabled) {
988                 debug("LCD disabled\n");
989                 return;
990         }
991
992         if (had_ctrlc() || (wrsr & WRSR_TOUT)) {
993                 lcd_disable();
994                 setenv("splashimage", NULL);
995                 return;
996         }
997
998         karo_fdt_move_fdt();
999         lcd_bl_polarity = karo_fdt_get_backlight_polarity(working_fdt);
1000
1001         if (video_mode == NULL) {
1002                 lcd_disable();
1003                 return;
1004         }
1005         vm = video_mode;
1006         if (karo_fdt_get_fb_mode(working_fdt, video_mode, &fb_mode) == 0) {
1007                 p = &fb_mode;
1008                 debug("Using video mode from FDT\n");
1009                 vm += strlen(vm);
1010                 if (fb_mode.xres > panel_info.vl_col ||
1011                         fb_mode.yres > panel_info.vl_row) {
1012                         printf("video resolution from DT: %dx%d exceeds hardware limits: %dx%d\n",
1013                                 fb_mode.xres, fb_mode.yres,
1014                                 panel_info.vl_col, panel_info.vl_row);
1015                         lcd_enabled = 0;
1016                         return;
1017                 }
1018         }
1019         if (p->name != NULL)
1020                 debug("Trying compiled-in video modes\n");
1021         while (p->name != NULL) {
1022                 if (strcmp(p->name, vm) == 0) {
1023                         debug("Using video mode: '%s'\n", p->name);
1024                         vm += strlen(vm);
1025                         break;
1026                 }
1027                 p++;
1028         }
1029         if (*vm != '\0')
1030                 debug("Trying to decode video_mode: '%s'\n", vm);
1031         while (*vm != '\0') {
1032                 if (*vm >= '0' && *vm <= '9') {
1033                         char *end;
1034
1035                         val = simple_strtoul(vm, &end, 0);
1036                         if (end > vm) {
1037                                 if (!xres_set) {
1038                                         if (val > panel_info.vl_col)
1039                                                 val = panel_info.vl_col;
1040                                         p->xres = val;
1041                                         panel_info.vl_col = val;
1042                                         xres_set = 1;
1043                                 } else if (!yres_set) {
1044                                         if (val > panel_info.vl_row)
1045                                                 val = panel_info.vl_row;
1046                                         p->yres = val;
1047                                         panel_info.vl_row = val;
1048                                         yres_set = 1;
1049                                 } else if (!bpp_set) {
1050                                         switch (val) {
1051                                         case 8:
1052                                         case 16:
1053                                         case 18:
1054                                         case 24:
1055                                         case 32:
1056                                                 color_depth = val;
1057                                                 break;
1058
1059                                         default:
1060                                                 printf("Invalid color depth: '%.*s' in video_mode; using default: '%u'\n",
1061                                                         end - vm, vm, color_depth);
1062                                         }
1063                                         bpp_set = 1;
1064                                 } else if (!refresh_set) {
1065                                         refresh = val;
1066                                         refresh_set = 1;
1067                                 }
1068                         }
1069                         vm = end;
1070                 }
1071                 switch (*vm) {
1072                 case '@':
1073                         bpp_set = 1;
1074                         /* fallthru */
1075                 case '-':
1076                         yres_set = 1;
1077                         /* fallthru */
1078                 case 'x':
1079                         xres_set = 1;
1080                         /* fallthru */
1081                 case 'M':
1082                 case 'R':
1083                         vm++;
1084                         break;
1085
1086                 default:
1087                         if (*vm != '\0')
1088                                 vm++;
1089                 }
1090         }
1091         if (p->xres == 0 || p->yres == 0) {
1092                 printf("Invalid video mode: %s\n", getenv("video_mode"));
1093                 lcd_enabled = 0;
1094                 printf("Supported video modes are:");
1095                 for (p = &tx6_fb_modes[0]; p->name != NULL; p++) {
1096                         printf(" %s", p->name);
1097                 }
1098                 printf("\n");
1099                 return;
1100         }
1101         if (p->xres > panel_info.vl_col || p->yres > panel_info.vl_row) {
1102                 printf("video resolution: %dx%d exceeds hardware limits: %dx%d\n",
1103                         p->xres, p->yres, panel_info.vl_col, panel_info.vl_row);
1104                 lcd_enabled = 0;
1105                 return;
1106         }
1107         panel_info.vl_col = p->xres;
1108         panel_info.vl_row = p->yres;
1109
1110         switch (color_depth) {
1111         case 8:
1112                 panel_info.vl_bpix = LCD_COLOR8;
1113                 break;
1114         case 16:
1115                 panel_info.vl_bpix = LCD_COLOR16;
1116                 break;
1117         default:
1118                 panel_info.vl_bpix = LCD_COLOR32;
1119         }
1120
1121         p->pixclock = KHZ2PICOS(refresh *
1122                 (p->xres + p->left_margin + p->right_margin + p->hsync_len) *
1123                 (p->yres + p->upper_margin + p->lower_margin + p->vsync_len) /
1124                                 1000);
1125         debug("Pixel clock set to %lu.%03lu MHz\n",
1126                 PICOS2KHZ(p->pixclock) / 1000, PICOS2KHZ(p->pixclock) % 1000);
1127
1128         if (p != &fb_mode) {
1129                 int ret;
1130
1131                 debug("Creating new display-timing node from '%s'\n",
1132                         video_mode);
1133                 ret = karo_fdt_create_fb_mode(working_fdt, video_mode, p);
1134                 if (ret)
1135                         printf("Failed to create new display-timing node from '%s': %d\n",
1136                                 video_mode, ret);
1137         }
1138
1139         gpio_request_array(stk5_lcd_gpios, ARRAY_SIZE(stk5_lcd_gpios));
1140         imx_iomux_v3_setup_multiple_pads(stk5_lcd_pads,
1141                                         ARRAY_SIZE(stk5_lcd_pads));
1142
1143         debug("video format: %ux%u-%u@%u\n", p->xres, p->yres,
1144                 color_depth, refresh);
1145
1146         if (karo_load_splashimage(0) == 0) {
1147                 char vmode[128];
1148
1149                 /* setup env variable for mxsfb display driver */
1150                 snprintf(vmode, sizeof(vmode),
1151                         "x:%d,y:%d,le:%d,ri:%d,up:%d,lo:%d,hs:%d,vs:%d,sync:%d,pclk:%d,depth:%d",
1152                         p->xres, p->yres, p->left_margin, p->right_margin,
1153                         p->upper_margin, p->lower_margin, p->hsync_len,
1154                         p->vsync_len, p->sync, p->pixclock, color_depth);
1155                 setenv("videomode", vmode);
1156
1157                 debug("Initializing LCD controller\n");
1158                 lcdif_clk_enable();
1159                 video_hw_init();
1160                 setenv("videomode", NULL);
1161         } else {
1162                 debug("Skipping initialization of LCD controller\n");
1163         }
1164 }
1165 #else
1166 #define lcd_enabled 0
1167 #endif /* CONFIG_LCD */
1168
1169 #ifndef CONFIG_ENV_IS_IN_MMC
1170 static void tx6_mmc_init(void)
1171 {
1172         puts("MMC:   ");
1173         if (board_mmc_init(gd->bd) < 0)
1174                 cpu_mmc_init(gd->bd);
1175         print_mmc_devices(',');
1176 }
1177 #else
1178 static inline void tx6_mmc_init(void)
1179 {
1180 }
1181 #endif
1182
1183 static void stk5_board_init(void)
1184 {
1185         int ret;
1186
1187         ret = gpio_request_array(stk5_gpios, ARRAY_SIZE(stk5_gpios));
1188         if (ret < 0) {
1189                 printf("Failed to request stk5_gpios: %d\n", ret);
1190                 return;
1191         }
1192         imx_iomux_v3_setup_multiple_pads(stk5_pads, ARRAY_SIZE(stk5_pads));
1193         debug("%s@%d: \n", __func__, __LINE__);
1194 }
1195
1196 static void stk5v3_board_init(void)
1197 {
1198         debug("%s@%d: \n", __func__, __LINE__);
1199         stk5_board_init();
1200         debug("%s@%d: \n", __func__, __LINE__);
1201         tx6_mmc_init();
1202 }
1203
1204 static void stk5v5_board_init(void)
1205 {
1206         int ret;
1207
1208         stk5_board_init();
1209         tx6_mmc_init();
1210
1211         ret = gpio_request_one(IMX_GPIO_NR(3, 5), GPIOFLAG_OUTPUT_INIT_HIGH,
1212                         "Flexcan Transceiver");
1213         if (ret) {
1214                 printf("Failed to request Flexcan Transceiver GPIO: %d\n", ret);
1215                 return;
1216         }
1217
1218         imx_iomux_v3_setup_pad(MX6_PAD_LCD_DATA00__GPIO3_IO05 |
1219                         MUX_PAD_CTRL(TX6_GPIO_OUT_PAD_CTRL));
1220 }
1221
1222 static void tx6ul_set_cpu_clock(void)
1223 {
1224         unsigned long cpu_clk = getenv_ulong("cpu_clk", 10, 0);
1225
1226         if (cpu_clk == 0 || cpu_clk == mxc_get_clock(MXC_ARM_CLK) / 1000000)
1227                 return;
1228
1229         if (had_ctrlc() || (wrsr & WRSR_TOUT)) {
1230                 printf("%s detected; skipping cpu clock change\n",
1231                         (wrsr & WRSR_TOUT) ? "WDOG RESET" : "<CTRL-C>");
1232                 return;
1233         }
1234         if (mxc_set_clock(CONFIG_SYS_MX6_HCLK, cpu_clk, MXC_ARM_CLK) == 0) {
1235                 cpu_clk = mxc_get_clock(MXC_ARM_CLK);
1236                 printf("CPU clock set to %lu.%03lu MHz\n",
1237                         cpu_clk / 1000000, cpu_clk / 1000 % 1000);
1238         } else {
1239                 printf("Error: Failed to set CPU clock to %lu MHz\n", cpu_clk);
1240         }
1241 }
1242
1243 int board_late_init(void)
1244 {
1245         const char *baseboard;
1246
1247         debug("%s@%d: \n", __func__, __LINE__);
1248
1249         env_cleanup();
1250
1251         if (tx6_temp_check_enabled)
1252                 check_cpu_temperature(1);
1253
1254         tx6ul_set_cpu_clock();
1255
1256         if (had_ctrlc())
1257                 setenv_ulong("safeboot", 1);
1258         else if (wrsr & WRSR_TOUT)
1259                 setenv_ulong("wdreset", 1);
1260         else
1261                 karo_fdt_move_fdt();
1262
1263         baseboard = getenv("baseboard");
1264         if (!baseboard)
1265                 goto exit;
1266
1267         printf("Baseboard: %s\n", baseboard);
1268
1269         if (strncmp(baseboard, "stk5", 4) == 0) {
1270                 if ((strlen(baseboard) == 4) ||
1271                         strcmp(baseboard, "stk5-v3") == 0) {
1272                         stk5v3_board_init();
1273                 } else if (strcmp(baseboard, "stk5-v5") == 0) {
1274                         const char *otg_mode = getenv("otg_mode");
1275
1276                         if (otg_mode && strcmp(otg_mode, "host") == 0) {
1277                                 printf("otg_mode='%s' is incompatible with baseboard %s; setting to 'none'\n",
1278                                         otg_mode, baseboard);
1279                                 setenv("otg_mode", "none");
1280                         }
1281                         stk5v5_board_init();
1282                 } else {
1283                         printf("WARNING: Unsupported STK5 board rev.: %s\n",
1284                                 baseboard + 4);
1285                 }
1286         } else if (strncmp(baseboard, "ulmb-", 5) == 0) {
1287                         const char *otg_mode = getenv("otg_mode");
1288
1289                         if (otg_mode && strcmp(otg_mode, "host") == 0) {
1290                                 printf("otg_mode='%s' is incompatible with baseboard %s; setting to 'none'\n",
1291                                         otg_mode, baseboard);
1292                                 setenv("otg_mode", "none");
1293                         }
1294                         stk5_board_init();
1295         } else {
1296                 printf("WARNING: Unsupported baseboard: '%s'\n",
1297                         baseboard);
1298                 if (!had_ctrlc())
1299                         return -EINVAL;
1300         }
1301
1302 exit:
1303         debug("%s@%d: \n", __func__, __LINE__);
1304
1305         clear_ctrlc();
1306         return 0;
1307 }
1308
1309 #ifdef CONFIG_FEC_MXC
1310
1311 #ifndef ETH_ALEN
1312 #define ETH_ALEN 6
1313 #endif
1314
1315 static void tx6_init_mac(void)
1316 {
1317         u8 mac[ETH_ALEN];
1318         const char *baseboard = getenv("baseboard");
1319
1320         imx_get_mac_from_fuse(0, mac);
1321         if (!is_valid_ethaddr(mac)) {
1322                 printf("No valid MAC address programmed\n");
1323                 return;
1324         }
1325         printf("MAC addr from fuse: %pM\n", mac);
1326         if (!getenv("ethaddr"))
1327                 eth_setenv_enetaddr("ethaddr", mac);
1328
1329         if (!baseboard || strncmp(baseboard, "stk5", 4) == 0) {
1330                 setenv("eth1addr", NULL);
1331                 return;
1332         }
1333         if (getenv("eth1addr"))
1334                 return;
1335         imx_get_mac_from_fuse(1, mac);
1336         eth_setenv_enetaddr("eth1addr", mac);
1337 }
1338
1339 int board_eth_init(bd_t *bis)
1340 {
1341         int ret;
1342
1343         tx6_init_mac();
1344
1345         /* delay at least 21ms for the PHY internal POR signal to deassert */
1346         udelay(22000);
1347
1348         imx_iomux_v3_setup_multiple_pads(tx6ul_enet1_pads,
1349                                         ARRAY_SIZE(tx6ul_enet1_pads));
1350
1351         /* Deassert RESET to the external phys */
1352         gpio_set_value(TX6UL_FEC_RST_GPIO, 1);
1353
1354         if (getenv("ethaddr")) {
1355                 ret = fecmxc_initialize_multi(bis, 0, 0, ENET_BASE_ADDR);
1356                 if (ret) {
1357                         printf("failed to initialize FEC0: %d\n", ret);
1358                         return ret;
1359                 }
1360         }
1361         if (getenv("eth1addr")) {
1362                 ret = gpio_request_array(tx6ul_fec2_gpios,
1363                                         ARRAY_SIZE(tx6ul_fec2_gpios));
1364                 if (ret < 0) {
1365                         printf("Failed to request tx6ul_fec2_gpios: %d\n", ret);
1366                 }
1367                 imx_iomux_v3_setup_multiple_pads(tx6ul_enet2_pads,
1368                                                 ARRAY_SIZE(tx6ul_enet2_pads));
1369
1370                 writel(0x00100000, 0x020c80e4); /* assert ENET2_125M_EN */
1371
1372                 /* Minimum PHY reset duration */
1373                 udelay(100);
1374                 gpio_set_value(TX6UL_FEC2_RST_GPIO, 1);
1375                 /* Wait for PHY internal POR to finish */
1376                 udelay(22000);
1377
1378                 ret = fecmxc_initialize_multi(bis, 1, 2, ENET2_BASE_ADDR);
1379                 if (ret) {
1380                         printf("failed to initialize FEC1: %d\n", ret);
1381                         return ret;
1382                 }
1383         }
1384         return 0;
1385 }
1386 #endif /* CONFIG_FEC_MXC */
1387
1388 #ifdef CONFIG_SERIAL_TAG
1389 void get_board_serial(struct tag_serialnr *serialnr)
1390 {
1391         struct ocotp_regs *ocotp = (struct ocotp_regs *)OCOTP_BASE_ADDR;
1392         struct fuse_bank0_regs *fuse = (void *)ocotp->bank[0].fuse_regs;
1393
1394         serialnr->low = readl(&fuse->cfg0);
1395         serialnr->high = readl(&fuse->cfg1);
1396 }
1397 #endif
1398
1399 #if defined(CONFIG_OF_BOARD_SETUP)
1400 #ifdef CONFIG_FDT_FIXUP_PARTITIONS
1401 #include <jffs2/jffs2.h>
1402 #include <mtd_node.h>
1403 static struct node_info nodes[] = {
1404         { "fsl,imx6q-gpmi-nand", MTD_DEV_TYPE_NAND, },
1405 };
1406 #else
1407 #define fdt_fixup_mtdparts(b,n,c) do { } while (0)
1408 #endif
1409
1410 static const char *tx6_touchpanels[] = {
1411         "ti,tsc2007",
1412         "edt,edt-ft5x06",
1413         "eeti,egalax_ts",
1414 };
1415
1416 int ft_board_setup(void *blob, bd_t *bd)
1417 {
1418         const char *baseboard = getenv("baseboard");
1419         int stk5_v5 = baseboard != NULL && (strcmp(baseboard, "stk5-v5") == 0);
1420         const char *video_mode = karo_get_vmode(getenv("video_mode"));
1421         int ret;
1422
1423         ret = fdt_increase_size(blob, 4096);
1424         if (ret) {
1425                 printf("Failed to increase FDT size: %s\n", fdt_strerror(ret));
1426                 return ret;
1427         }
1428         if (stk5_v5)
1429                 karo_fdt_enable_node(blob, "stk5led", 0);
1430
1431         fdt_fixup_mtdparts(blob, nodes, ARRAY_SIZE(nodes));
1432
1433         karo_fdt_fixup_touchpanel(blob, tx6_touchpanels,
1434                                 ARRAY_SIZE(tx6_touchpanels));
1435         karo_fdt_fixup_usb_otg(blob, "usbotg", "fsl,usbphy", "vbus-supply");
1436         karo_fdt_fixup_flexcan(blob, stk5_v5);
1437
1438         karo_fdt_update_fb_mode(blob, video_mode);
1439
1440         return 0;
1441 }
1442 #endif /* CONFIG_OF_BOARD_SETUP */