]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - board/karo/tx6/tx6qdl.c
karo: tx6: remove duplicate setup of LCD pads
[karo-tx-uboot.git] / board / karo / tx6 / tx6qdl.c
1 /*
2  * Copyright (C) 2012,2013 Lothar Waßmann <LW@KARO-electronics.de>
3  *
4  * See file CREDITS for list of people who contributed to this
5  * project.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * version 2 as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  */
17
18 #include <common.h>
19 #include <errno.h>
20 #include <libfdt.h>
21 #include <fdt_support.h>
22 #include <lcd.h>
23 #include <netdev.h>
24 #include <mmc.h>
25 #include <fsl_esdhc.h>
26 #include <video_fb.h>
27 #include <ipu.h>
28 #include <mxcfb.h>
29 #include <i2c.h>
30 #include <linux/fb.h>
31 #include <asm/io.h>
32 #include <asm/gpio.h>
33 #include <asm/arch/mx6-pins.h>
34 #include <asm/arch/clock.h>
35 #include <asm/arch/imx-regs.h>
36 #include <asm/arch/crm_regs.h>
37 #include <asm/arch/sys_proto.h>
38
39 #include "../common/karo.h"
40
41 #define TX6_FEC_RST_GPIO                IMX_GPIO_NR(7, 6)
42 #define TX6_FEC_PWR_GPIO                IMX_GPIO_NR(3, 20)
43 #define TX6_FEC_INT_GPIO                IMX_GPIO_NR(7, 1)
44 #define TX6_LED_GPIO                    IMX_GPIO_NR(2, 20)
45
46 #define TX6_LCD_PWR_GPIO                IMX_GPIO_NR(2, 31)
47 #define TX6_LCD_RST_GPIO                IMX_GPIO_NR(3, 29)
48 #define TX6_LCD_BACKLIGHT_GPIO          IMX_GPIO_NR(1, 1)
49
50 #define TX6_RESET_OUT_GPIO              IMX_GPIO_NR(7, 12)
51
52 #define TEMPERATURE_MIN                 -40
53 #define TEMPERATURE_HOT                 80
54 #define TEMPERATURE_MAX                 125
55
56 DECLARE_GLOBAL_DATA_PTR;
57
58 #define MUX_CFG_SION                    IOMUX_PAD(0, 0, IOMUX_CONFIG_SION, 0, 0, 0)
59
60 static const iomux_v3_cfg_t tx6qdl_pads[] = {
61         /* NAND flash pads */
62         MX6_PAD_NANDF_CLE__RAWNAND_CLE,
63         MX6_PAD_NANDF_ALE__RAWNAND_ALE,
64         MX6_PAD_NANDF_WP_B__RAWNAND_RESETN,
65         MX6_PAD_NANDF_RB0__RAWNAND_READY0,
66         MX6_PAD_NANDF_CS0__RAWNAND_CE0N,
67         MX6_PAD_SD4_CMD__RAWNAND_RDN,
68         MX6_PAD_SD4_CLK__RAWNAND_WRN,
69         MX6_PAD_NANDF_D0__RAWNAND_D0,
70         MX6_PAD_NANDF_D1__RAWNAND_D1,
71         MX6_PAD_NANDF_D2__RAWNAND_D2,
72         MX6_PAD_NANDF_D3__RAWNAND_D3,
73         MX6_PAD_NANDF_D4__RAWNAND_D4,
74         MX6_PAD_NANDF_D5__RAWNAND_D5,
75         MX6_PAD_NANDF_D6__RAWNAND_D6,
76         MX6_PAD_NANDF_D7__RAWNAND_D7,
77
78         /* RESET_OUT */
79         MX6_PAD_GPIO_17__GPIO_7_12,
80
81         /* UART pads */
82 #if CONFIG_MXC_UART_BASE == UART1_BASE
83         MX6_PAD_SD3_DAT7__UART1_TXD,
84         MX6_PAD_SD3_DAT6__UART1_RXD,
85         MX6_PAD_SD3_DAT1__UART1_RTS,
86         MX6_PAD_SD3_DAT0__UART1_CTS,
87 #endif
88 #if CONFIG_MXC_UART_BASE == UART2_BASE
89         MX6_PAD_SD4_DAT4__UART2_RXD,
90         MX6_PAD_SD4_DAT7__UART2_TXD,
91         MX6_PAD_SD4_DAT5__UART2_RTS,
92         MX6_PAD_SD4_DAT6__UART2_CTS,
93 #endif
94 #if CONFIG_MXC_UART_BASE == UART3_BASE
95         MX6_PAD_EIM_D24__UART3_TXD,
96         MX6_PAD_EIM_D25__UART3_RXD,
97         MX6_PAD_SD3_RST__UART3_RTS,
98         MX6_PAD_SD3_DAT3__UART3_CTS,
99 #endif
100         /* internal I2C */
101         MX6_PAD_EIM_D28__I2C1_SDA,
102         MX6_PAD_EIM_D21__I2C1_SCL,
103
104         /* FEC PHY GPIO functions */
105         MX6_PAD_EIM_D20__GPIO_3_20 | MUX_CFG_SION, /* PHY POWER */
106         MX6_PAD_SD3_DAT2__GPIO_7_6 | MUX_CFG_SION, /* PHY RESET */
107         MX6_PAD_SD3_DAT4__GPIO_7_1, /* PHY INT */
108 };
109
110 static const iomux_v3_cfg_t tx6qdl_fec_pads[] = {
111         /* FEC functions */
112         MX6_PAD_ENET_MDC__ENET_MDC,
113         MX6_PAD_ENET_MDIO__ENET_MDIO,
114         MX6_PAD_GPIO_16__ENET_ANATOP_ETHERNET_REF_OUT,
115         MX6_PAD_ENET_RX_ER__ENET_RX_ER,
116         MX6_PAD_ENET_CRS_DV__ENET_RX_EN,
117         MX6_PAD_ENET_RXD1__ENET_RDATA_1,
118         MX6_PAD_ENET_RXD0__ENET_RDATA_0,
119         MX6_PAD_ENET_TX_EN__ENET_TX_EN,
120         MX6_PAD_ENET_TXD1__ENET_TDATA_1,
121         MX6_PAD_ENET_TXD0__ENET_TDATA_0,
122 };
123
124 static const struct gpio tx6qdl_gpios[] = {
125         { TX6_RESET_OUT_GPIO, GPIOF_OUTPUT_INIT_HIGH, "#RESET_OUT", },
126         { TX6_FEC_PWR_GPIO, GPIOF_OUTPUT_INIT_HIGH, "FEC PHY PWR", },
127         { TX6_FEC_RST_GPIO, GPIOF_OUTPUT_INIT_LOW, "FEC PHY RESET", },
128         { TX6_FEC_INT_GPIO, GPIOF_INPUT, "FEC PHY INT", },
129 };
130
131 /*
132  * Functions
133  */
134 /* placed in section '.data' to prevent overwriting relocation info
135  * overlayed with bss
136  */
137 static u32 wrsr __attribute__((section(".data")));
138
139 #define WRSR_POR                        (1 << 4)
140 #define WRSR_TOUT                       (1 << 1)
141 #define WRSR_SFTW                       (1 << 0)
142
143 static void print_reset_cause(void)
144 {
145         struct src *src_regs = (struct src *)SRC_BASE_ADDR;
146         void __iomem *wdt_base = (void __iomem *)WDOG1_BASE_ADDR;
147         u32 srsr;
148         char *dlm = "";
149
150         printf("Reset cause: ");
151
152         srsr = readl(&src_regs->srsr);
153         wrsr = readw(wdt_base + 4);
154
155         if (wrsr & WRSR_POR) {
156                 printf("%sPOR", dlm);
157                 dlm = " | ";
158         }
159         if (srsr & 0x00004) {
160                 printf("%sCSU", dlm);
161                 dlm = " | ";
162         }
163         if (srsr & 0x00008) {
164                 printf("%sIPP USER", dlm);
165                 dlm = " | ";
166         }
167         if (srsr & 0x00010) {
168                 if (wrsr & WRSR_SFTW) {
169                         printf("%sSOFT", dlm);
170                         dlm = " | ";
171                 }
172                 if (wrsr & WRSR_TOUT) {
173                         printf("%sWDOG", dlm);
174                         dlm = " | ";
175                 }
176         }
177         if (srsr & 0x00020) {
178                 printf("%sJTAG HIGH-Z", dlm);
179                 dlm = " | ";
180         }
181         if (srsr & 0x00040) {
182                 printf("%sJTAG SW", dlm);
183                 dlm = " | ";
184         }
185         if (srsr & 0x10000) {
186                 printf("%sWARM BOOT", dlm);
187                 dlm = " | ";
188         }
189         if (dlm[0] == '\0')
190                 printf("unknown");
191
192         printf("\n");
193 }
194
195 int read_cpu_temperature(void);
196 int check_cpu_temperature(int boot);
197
198 static void tx6qdl_print_cpuinfo(void)
199 {
200         u32 cpurev = get_cpu_rev();
201         char *cpu_str = "?";
202
203         switch ((cpurev >> 12) & 0xff) {
204         case MXC_CPU_MX6SL:
205                 cpu_str = "SL";
206                 break;
207         case MXC_CPU_MX6DL:
208                 cpu_str = "DL";
209                 break;
210         case MXC_CPU_MX6SOLO:
211                 cpu_str = "SOLO";
212                 break;
213         case MXC_CPU_MX6Q:
214                 cpu_str = "Q";
215                 break;
216         }
217
218         printf("CPU:   Freescale i.MX6%s rev%d.%d at %d MHz\n",
219                 cpu_str,
220                 (cpurev & 0x000F0) >> 4,
221                 (cpurev & 0x0000F) >> 0,
222                 mxc_get_clock(MXC_ARM_CLK) / 1000000);
223
224         print_reset_cause();
225         check_cpu_temperature(1);
226 }
227
228 #define LTC3676_BUCK1           0x01
229 #define LTC3676_BUCK2           0x02
230 #define LTC3676_BUCK3           0x03
231 #define LTC3676_BUCK4           0x04
232 #define LTC3676_DVB1A           0x0A
233 #define LTC3676_DVB1B           0x0B
234 #define LTC3676_DVB2A           0x0C
235 #define LTC3676_DVB2B           0x0D
236 #define LTC3676_DVB3A           0x0E
237 #define LTC3676_DVB3B           0x0F
238 #define LTC3676_DVB4A           0x10
239 #define LTC3676_DVB4B           0x11
240 #define LTC3676_MSKPG           0x13
241 #define LTC3676_CLIRQ           0x1f
242
243 #define LTC3676_BUCK_DVDT_FAST  (1 << 0)
244 #define LTC3676_BUCK_KEEP_ALIVE (1 << 1)
245 #define LTC3676_BUCK_CLK_RATE_LOW (1 << 2)
246 #define LTC3676_BUCK_PHASE_SEL  (1 << 3)
247 #define LTC3676_BUCK_ENABLE_300 (1 << 4)
248 #define LTC3676_BUCK_PULSE_SKIP (0 << 5)
249 #define LTC3676_BUCK_BURST_MODE (1 << 5)
250 #define LTC3676_BUCK_CONTINUOUS (2 << 5)
251 #define LTC3676_BUCK_ENABLE     (1 << 7)
252
253 #define LTC3676_PGOOD_MASK      (1 << 5)
254
255 #define LTC3676_MSKPG_BUCK1     (1 << 0)
256 #define LTC3676_MSKPG_BUCK2     (1 << 1)
257 #define LTC3676_MSKPG_BUCK3     (1 << 2)
258 #define LTC3676_MSKPG_BUCK4     (1 << 3)
259 #define LTC3676_MSKPG_LDO2      (1 << 5)
260 #define LTC3676_MSKPG_LDO3      (1 << 6)
261 #define LTC3676_MSKPG_LDO4      (1 << 7)
262
263 #define VDD_IO_VAL              mV_to_regval(vout_to_vref(3300 * 10, 5))
264 #define VDD_IO_VAL_LP           mV_to_regval(vout_to_vref(3100 * 10, 5))
265 #define VDD_IO_VAL_2            mV_to_regval(vout_to_vref(3300 * 10, 5_2))
266 #define VDD_IO_VAL_2_LP         mV_to_regval(vout_to_vref(3100 * 10, 5_2))
267 #define VDD_SOC_VAL             mV_to_regval(vout_to_vref(1425 * 10, 6))
268 #define VDD_SOC_VAL_LP          mV_to_regval(vout_to_vref(900 * 10, 6))
269 #define VDD_DDR_VAL             mV_to_regval(vout_to_vref(1500 * 10, 7))
270 #define VDD_CORE_VAL            mV_to_regval(vout_to_vref(1425 * 10, 8))
271 #define VDD_CORE_VAL_LP         mV_to_regval(vout_to_vref(900 * 10, 8))
272
273 /* LDO1 */
274 #define R1_1                    470
275 #define R2_1                    150
276 /* LDO4 */
277 #define R1_4                    470
278 #define R2_4                    150
279 /* Buck1 */
280 #define R1_5                    390
281 #define R2_5                    110
282 #define R1_5_2                  470
283 #define R2_5_2                  150
284 /* Buck2 */
285 #define R1_6                    150
286 #define R2_6                    180
287 /* Buck3 */
288 #define R1_7                    150
289 #define R2_7                    140
290 /* Buck4 */
291 #define R1_8                    150
292 #define R2_8                    180
293
294 /* calculate voltages in 10mV */
295 #define R1(idx)                 R1_##idx
296 #define R2(idx)                 R2_##idx
297
298 #define vout_to_vref(vout, idx) ((vout) * R2(idx) / (R1(idx) + R2(idx)))
299 #define vref_to_vout(vref, idx) DIV_ROUND_UP((vref) * (R1(idx) + R2(idx)), R2(idx))
300
301 #define mV_to_regval(mV)        DIV_ROUND(((((mV) < 4125) ? 4125 : (mV)) - 4125), 125)
302 #define regval_to_mV(v)         (((v) * 125 + 4125))
303
304 static struct ltc3673_regs {
305         u8 addr;
306         u8 val;
307         u8 mask;
308 } ltc3676_regs[] = {
309         { LTC3676_MSKPG, ~LTC3676_MSKPG_BUCK1, },
310         { LTC3676_DVB2B, VDD_SOC_VAL | LTC3676_PGOOD_MASK, ~0x3f, },
311         { LTC3676_DVB3B, VDD_DDR_VAL, ~0x3f, },
312         { LTC3676_DVB4B, VDD_CORE_VAL | LTC3676_PGOOD_MASK, ~0x3f, },
313         { LTC3676_DVB2A, VDD_SOC_VAL, ~0x3f, },
314         { LTC3676_DVB3A, VDD_DDR_VAL, ~0x3f, },
315         { LTC3676_DVB4A, VDD_CORE_VAL, ~0x3f, },
316         { LTC3676_BUCK1, LTC3676_BUCK_BURST_MODE | LTC3676_BUCK_CLK_RATE_LOW, },
317         { LTC3676_BUCK2, LTC3676_BUCK_BURST_MODE, },
318         { LTC3676_BUCK3, LTC3676_BUCK_BURST_MODE, },
319         { LTC3676_BUCK4, LTC3676_BUCK_BURST_MODE, },
320         { LTC3676_CLIRQ, 0, }, /* clear interrupt status */
321 };
322
323 static struct ltc3673_regs ltc3676_regs_1[] = {
324         { LTC3676_DVB1B, VDD_IO_VAL_LP | LTC3676_PGOOD_MASK, ~0x3f, },
325         { LTC3676_DVB1A, VDD_IO_VAL, ~0x3f, },
326 };
327
328 static struct ltc3673_regs ltc3676_regs_2[] = {
329         { LTC3676_DVB1B, VDD_IO_VAL_2_LP | LTC3676_PGOOD_MASK, ~0x3f, },
330         { LTC3676_DVB1A, VDD_IO_VAL_2, ~0x3f, },
331 };
332
333 static int tx6_rev_2(void)
334 {
335         struct ocotp_regs *ocotp = (struct ocotp_regs *)OCOTP_BASE_ADDR;
336         struct fuse_bank5_regs *fuse = (void *)ocotp->bank[5].fuse_regs;
337         u32 pad_settings = readl(&fuse->pad_settings);
338
339         debug("Fuse pad_settings @ %p = %02x\n",
340                 &fuse->pad_settings, pad_settings);
341         return pad_settings & 1;
342 }
343
344 static int tx6_ltc3676_setup_regs(struct ltc3673_regs *r, size_t count)
345 {
346         int ret;
347         int i;
348
349         for (i = 0; i < count; i++, r++) {
350 #ifdef DEBUG
351                 unsigned char value;
352
353                 ret = i2c_read(CONFIG_SYS_I2C_SLAVE, r->addr, 1, &value, 1);
354                 if ((value & ~r->mask) != r->val) {
355                         printf("Changing PMIC reg %02x from %02x to %02x\n",
356                                 r->addr, value, r->val);
357                 }
358                 if (ret) {
359                         printf("%s: failed to read PMIC register %02x: %d\n",
360                                 __func__, r->addr, ret);
361                         return ret;
362                 }
363 #endif
364                 ret = i2c_write(CONFIG_SYS_I2C_SLAVE,
365                                 r->addr, 1, &r->val, 1);
366                 if (ret) {
367                         printf("%s: failed to write PMIC register %02x: %d\n",
368                                 __func__, r->addr, ret);
369                         return ret;
370                 }
371         }
372         return 0;
373 }
374
375 static int setup_pmic_voltages(void)
376 {
377         int ret;
378         unsigned char value;
379
380         ret = i2c_probe(CONFIG_SYS_I2C_SLAVE);
381         if (ret != 0) {
382                 printf("Failed to initialize I2C\n");
383                 return ret;
384         }
385
386         ret = i2c_read(CONFIG_SYS_I2C_SLAVE, 0x11, 1, &value, 1);
387         if (ret) {
388                 printf("%s: i2c_read error: %d\n", __func__, ret);
389                 return ret;
390         }
391
392         ret = tx6_ltc3676_setup_regs(ltc3676_regs, ARRAY_SIZE(ltc3676_regs));
393         if (ret)
394                 return ret;
395
396         printf("VDDCORE set to %umV\n",
397                 DIV_ROUND(vref_to_vout(regval_to_mV(VDD_CORE_VAL), 8), 10));
398         printf("VDDSOC  set to %umV\n",
399                 DIV_ROUND(vref_to_vout(regval_to_mV(VDD_SOC_VAL), 6), 10));
400
401         if (tx6_rev_2()) {
402                 ret = tx6_ltc3676_setup_regs(ltc3676_regs_2,
403                                 ARRAY_SIZE(ltc3676_regs_2));
404                 printf("VDDIO   set to %umV\n",
405                         DIV_ROUND(vref_to_vout(
406                                         regval_to_mV(VDD_IO_VAL_2), 5_2), 10));
407         } else {
408                 ret = tx6_ltc3676_setup_regs(ltc3676_regs_1,
409                                 ARRAY_SIZE(ltc3676_regs_1));
410         }
411         return ret;
412 }
413
414 int board_early_init_f(void)
415 {
416         gpio_request_array(tx6qdl_gpios, ARRAY_SIZE(tx6qdl_gpios));
417         imx_iomux_v3_setup_multiple_pads(tx6qdl_pads, ARRAY_SIZE(tx6qdl_pads));
418
419         return 0;
420 }
421
422 int board_init(void)
423 {
424         int ret;
425
426         /* Address of boot parameters */
427         gd->bd->bi_boot_params = PHYS_SDRAM_1 + 0x1000;
428         gd->bd->bi_arch_number = -1;
429
430         if (ctrlc()) {
431                 printf("CTRL-C detected; Skipping PMIC setup\n");
432                 return 1;
433         }
434
435         ret = setup_pmic_voltages();
436         if (ret) {
437                 printf("Failed to setup PMIC voltages\n");
438                 hang();
439         }
440         return 0;
441 }
442
443 int dram_init(void)
444 {
445         /* dram_init must store complete ramsize in gd->ram_size */
446         gd->ram_size = get_ram_size((void *)CONFIG_SYS_SDRAM_BASE,
447                                 PHYS_SDRAM_1_SIZE);
448         return 0;
449 }
450
451 void dram_init_banksize(void)
452 {
453         gd->bd->bi_dram[0].start = PHYS_SDRAM_1;
454         gd->bd->bi_dram[0].size = get_ram_size((void *)PHYS_SDRAM_1,
455                         PHYS_SDRAM_1_SIZE);
456 #if CONFIG_NR_DRAM_BANKS > 1
457         gd->bd->bi_dram[1].start = PHYS_SDRAM_2;
458         gd->bd->bi_dram[1].size = get_ram_size((void *)PHYS_SDRAM_2,
459                         PHYS_SDRAM_2_SIZE);
460 #endif
461 }
462
463 #ifdef  CONFIG_CMD_MMC
464 static const iomux_v3_cfg_t mmc0_pads[] = {
465         MX6_PAD_SD1_CMD__USDHC1_CMD,
466         MX6_PAD_SD1_CLK__USDHC1_CLK,
467         MX6_PAD_SD1_DAT0__USDHC1_DAT0,
468         MX6_PAD_SD1_DAT1__USDHC1_DAT1,
469         MX6_PAD_SD1_DAT2__USDHC1_DAT2,
470         MX6_PAD_SD1_DAT3__USDHC1_DAT3,
471         /* SD1 CD */
472         MX6_PAD_SD3_CMD__GPIO_7_2,
473 };
474
475 static const iomux_v3_cfg_t mmc1_pads[] = {
476         MX6_PAD_SD2_CMD__USDHC2_CMD,
477         MX6_PAD_SD2_CLK__USDHC2_CLK,
478         MX6_PAD_SD2_DAT0__USDHC2_DAT0,
479         MX6_PAD_SD2_DAT1__USDHC2_DAT1,
480         MX6_PAD_SD2_DAT2__USDHC2_DAT2,
481         MX6_PAD_SD2_DAT3__USDHC2_DAT3,
482         /* SD2 CD */
483         MX6_PAD_SD3_CLK__GPIO_7_3,
484 };
485
486 static struct tx6_esdhc_cfg {
487         const iomux_v3_cfg_t *pads;
488         int num_pads;
489         enum mxc_clock clkid;
490         struct fsl_esdhc_cfg cfg;
491         int cd_gpio;
492 } tx6qdl_esdhc_cfg[] = {
493         {
494                 .pads = mmc0_pads,
495                 .num_pads = ARRAY_SIZE(mmc0_pads),
496                 .clkid = MXC_ESDHC_CLK,
497                 .cfg = {
498                         .esdhc_base = (void __iomem *)USDHC1_BASE_ADDR,
499                         .max_bus_width = 4,
500                 },
501                 .cd_gpio = IMX_GPIO_NR(7, 2),
502         },
503         {
504                 .pads = mmc1_pads,
505                 .num_pads = ARRAY_SIZE(mmc1_pads),
506                 .clkid = MXC_ESDHC2_CLK,
507                 .cfg = {
508                         .esdhc_base = (void __iomem *)USDHC2_BASE_ADDR,
509                         .max_bus_width = 4,
510                 },
511                 .cd_gpio = IMX_GPIO_NR(7, 3),
512         },
513 };
514
515 static inline struct tx6_esdhc_cfg *to_tx6_esdhc_cfg(struct fsl_esdhc_cfg *cfg)
516 {
517         return container_of(cfg, struct tx6_esdhc_cfg, cfg);
518 }
519
520 int board_mmc_getcd(struct mmc *mmc)
521 {
522         struct tx6_esdhc_cfg *cfg = to_tx6_esdhc_cfg(mmc->priv);
523
524         if (cfg->cd_gpio < 0)
525                 return cfg->cd_gpio;
526
527         debug("SD card %d is %spresent\n",
528                 cfg - tx6qdl_esdhc_cfg,
529                 gpio_get_value(cfg->cd_gpio) ? "NOT " : "");
530         return !gpio_get_value(cfg->cd_gpio);
531 }
532
533 int board_mmc_init(bd_t *bis)
534 {
535         int i;
536
537         for (i = 0; i < ARRAY_SIZE(tx6qdl_esdhc_cfg); i++) {
538                 struct mmc *mmc;
539                 struct tx6_esdhc_cfg *cfg = &tx6qdl_esdhc_cfg[i];
540                 int ret;
541
542                 cfg->cfg.sdhc_clk = mxc_get_clock(cfg->clkid);
543                 imx_iomux_v3_setup_multiple_pads(cfg->pads, cfg->num_pads);
544
545                 ret = gpio_request_one(cfg->cd_gpio,
546                                 GPIOF_INPUT, "MMC CD");
547                 if (ret) {
548                         printf("Error %d requesting GPIO%d_%d\n",
549                                 ret, cfg->cd_gpio / 32, cfg->cd_gpio % 32);
550                         continue;
551                 }
552
553                 debug("%s: Initializing MMC slot %d\n", __func__, i);
554                 fsl_esdhc_initialize(bis, &cfg->cfg);
555
556                 mmc = find_mmc_device(i);
557                 if (mmc == NULL)
558                         continue;
559                 if (board_mmc_getcd(mmc) > 0)
560                         mmc_init(mmc);
561         }
562         return 0;
563 }
564 #endif /* CONFIG_CMD_MMC */
565
566 #ifdef CONFIG_FEC_MXC
567
568 #define FEC_PAD_CTL     (PAD_CTL_DVS | PAD_CTL_DSE_HIGH | \
569                         PAD_CTL_SRE_FAST)
570 #define FEC_PAD_CTL2    (PAD_CTL_DVS | PAD_CTL_SRE_FAST)
571 #define GPIO_PAD_CTL    (PAD_CTL_DVS | PAD_CTL_DSE_HIGH)
572
573 #ifndef ETH_ALEN
574 #define ETH_ALEN 6
575 #endif
576
577 int board_eth_init(bd_t *bis)
578 {
579         int ret;
580
581         /* delay at least 21ms for the PHY internal POR signal to deassert */
582         udelay(22000);
583
584         imx_iomux_v3_setup_multiple_pads(tx6qdl_fec_pads, ARRAY_SIZE(tx6qdl_fec_pads));
585
586         /* Deassert RESET to the external phy */
587         gpio_set_value(TX6_FEC_RST_GPIO, 1);
588
589         ret = cpu_eth_init(bis);
590         if (ret)
591                 printf("cpu_eth_init() failed: %d\n", ret);
592
593         return ret;
594 }
595 #endif /* CONFIG_FEC_MXC */
596
597 enum {
598         LED_STATE_INIT = -1,
599         LED_STATE_OFF,
600         LED_STATE_ON,
601 };
602
603 static inline int calc_blink_rate(int tmp)
604 {
605         return CONFIG_SYS_HZ + CONFIG_SYS_HZ / 10 -
606                 (tmp - TEMPERATURE_MIN) * CONFIG_SYS_HZ /
607                 (TEMPERATURE_HOT - TEMPERATURE_MIN);
608 }
609
610 void show_activity(int arg)
611 {
612         static int led_state = LED_STATE_INIT;
613         static int blink_rate;
614         static ulong last;
615
616         if (led_state == LED_STATE_INIT) {
617                 last = get_timer(0);
618                 gpio_set_value(TX6_LED_GPIO, 1);
619                 led_state = LED_STATE_ON;
620                 blink_rate = calc_blink_rate(check_cpu_temperature(0));
621         } else {
622                 if (get_timer(last) > blink_rate) {
623                         blink_rate = calc_blink_rate(check_cpu_temperature(0));
624                         last = get_timer_masked();
625                         if (led_state == LED_STATE_ON) {
626                                 gpio_set_value(TX6_LED_GPIO, 0);
627                         } else {
628                                 gpio_set_value(TX6_LED_GPIO, 1);
629                         }
630                         led_state = 1 - led_state;
631                 }
632         }
633 }
634
635 static const iomux_v3_cfg_t stk5_pads[] = {
636         /* SW controlled LED on STK5 baseboard */
637         MX6_PAD_EIM_A18__GPIO_2_20,
638
639         /* I2C bus on DIMM pins 40/41 */
640         MX6_PAD_GPIO_6__I2C3_SDA,
641         MX6_PAD_GPIO_3__I2C3_SCL,
642
643         /* TSC200x PEN IRQ */
644         MX6_PAD_EIM_D26__GPIO_3_26,
645
646         /* EDT-FT5x06 Polytouch panel */
647         MX6_PAD_NANDF_CS2__GPIO_6_15, /* IRQ */
648         MX6_PAD_EIM_A16__GPIO_2_22, /* RESET */
649         MX6_PAD_EIM_A17__GPIO_2_21, /* WAKE */
650
651         /* USBH1 */
652         MX6_PAD_EIM_D31__GPIO_3_31, /* VBUSEN */
653         MX6_PAD_EIM_D30__GPIO_3_30, /* OC */
654         /* USBOTG */
655         MX6_PAD_EIM_D23__GPIO_3_23, /* USBOTG ID */
656         MX6_PAD_GPIO_7__GPIO_1_7, /* VBUSEN */
657         MX6_PAD_GPIO_8__GPIO_1_8, /* OC */
658 };
659
660 static const struct gpio stk5_gpios[] = {
661         { TX6_LED_GPIO, GPIOF_OUTPUT_INIT_LOW, "HEARTBEAT LED", },
662
663         { IMX_GPIO_NR(3, 23), GPIOF_INPUT, "USBOTG ID", },
664         { IMX_GPIO_NR(1, 8), GPIOF_INPUT, "USBOTG OC", },
665         { IMX_GPIO_NR(1, 7), GPIOF_OUTPUT_INIT_LOW, "USBOTG VBUS enable", },
666         { IMX_GPIO_NR(3, 30), GPIOF_INPUT, "USBH1 OC", },
667         { IMX_GPIO_NR(3, 31), GPIOF_OUTPUT_INIT_LOW, "USBH1 VBUS enable", },
668 };
669
670 #ifdef CONFIG_LCD
671 static u16 tx6_cmap[256];
672 vidinfo_t panel_info = {
673         /* set to max. size supported by SoC */
674         .vl_col = 1920,
675         .vl_row = 1080,
676
677         .vl_bpix = LCD_COLOR24,    /* Bits per pixel, 0: 1bpp, 1: 2bpp, 2: 4bpp, 3: 8bpp ... */
678         .cmap = tx6_cmap,
679 };
680
681 static struct fb_videomode tx6_fb_modes[] = {
682 #ifndef CONFIG_SYS_LVDS_IF
683         {
684                 /* Standard VGA timing */
685                 .name           = "VGA",
686                 .refresh        = 60,
687                 .xres           = 640,
688                 .yres           = 480,
689                 .pixclock       = KHZ2PICOS(25175),
690                 .left_margin    = 48,
691                 .hsync_len      = 96,
692                 .right_margin   = 16,
693                 .upper_margin   = 31,
694                 .vsync_len      = 2,
695                 .lower_margin   = 12,
696                 .sync           = FB_SYNC_CLK_LAT_FALL,
697         },
698         {
699                 /* Emerging ETV570 640 x 480 display. Syncs low active,
700                  * DE high active, 115.2 mm x 86.4 mm display area
701                  * VGA compatible timing
702                  */
703                 .name           = "ETV570",
704                 .refresh        = 60,
705                 .xres           = 640,
706                 .yres           = 480,
707                 .pixclock       = KHZ2PICOS(25175),
708                 .left_margin    = 114,
709                 .hsync_len      = 30,
710                 .right_margin   = 16,
711                 .upper_margin   = 32,
712                 .vsync_len      = 3,
713                 .lower_margin   = 10,
714                 .sync           = FB_SYNC_CLK_LAT_FALL,
715         },
716         {
717                 /* Emerging ET0350G0DH6 320 x 240 display.
718                  * 70.08 mm x 52.56 mm display area.
719                  */
720                 .name           = "ET0350",
721                 .refresh        = 60,
722                 .xres           = 320,
723                 .yres           = 240,
724                 .pixclock       = KHZ2PICOS(6500),
725                 .left_margin    = 68 - 34,
726                 .hsync_len      = 34,
727                 .right_margin   = 20,
728                 .upper_margin   = 18 - 3,
729                 .vsync_len      = 3,
730                 .lower_margin   = 4,
731                 .sync           = FB_SYNC_CLK_LAT_FALL,
732         },
733         {
734                 /* Emerging ET0430G0DH6 480 x 272 display.
735                  * 95.04 mm x 53.856 mm display area.
736                  */
737                 .name           = "ET0430",
738                 .refresh        = 60,
739                 .xres           = 480,
740                 .yres           = 272,
741                 .pixclock       = KHZ2PICOS(9000),
742                 .left_margin    = 2,
743                 .hsync_len      = 41,
744                 .right_margin   = 2,
745                 .upper_margin   = 2,
746                 .vsync_len      = 10,
747                 .lower_margin   = 2,
748                 .sync           = FB_SYNC_CLK_LAT_FALL,
749         },
750         {
751                 /* Emerging ET0500G0DH6 800 x 480 display.
752                  * 109.6 mm x 66.4 mm display area.
753                  */
754                 .name           = "ET0500",
755                 .refresh        = 60,
756                 .xres           = 800,
757                 .yres           = 480,
758                 .pixclock       = KHZ2PICOS(33260),
759                 .left_margin    = 216 - 128,
760                 .hsync_len      = 128,
761                 .right_margin   = 1056 - 800 - 216,
762                 .upper_margin   = 35 - 2,
763                 .vsync_len      = 2,
764                 .lower_margin   = 525 - 480 - 35,
765                 .sync           = FB_SYNC_CLK_LAT_FALL,
766         },
767         {
768                 /* Emerging ETQ570G0DH6 320 x 240 display.
769                  * 115.2 mm x 86.4 mm display area.
770                  */
771                 .name           = "ETQ570",
772                 .refresh        = 60,
773                 .xres           = 320,
774                 .yres           = 240,
775                 .pixclock       = KHZ2PICOS(6400),
776                 .left_margin    = 38,
777                 .hsync_len      = 30,
778                 .right_margin   = 30,
779                 .upper_margin   = 16, /* 15 according to datasheet */
780                 .vsync_len      = 3, /* TVP -> 1>x>5 */
781                 .lower_margin   = 4, /* 4.5 according to datasheet */
782                 .sync           = FB_SYNC_CLK_LAT_FALL,
783         },
784         {
785                 /* Emerging ET0700G0DH6 800 x 480 display.
786                  * 152.4 mm x 91.44 mm display area.
787                  */
788                 .name           = "ET0700",
789                 .refresh        = 60,
790                 .xres           = 800,
791                 .yres           = 480,
792                 .pixclock       = KHZ2PICOS(33260),
793                 .left_margin    = 216 - 128,
794                 .hsync_len      = 128,
795                 .right_margin   = 1056 - 800 - 216,
796                 .upper_margin   = 35 - 2,
797                 .vsync_len      = 2,
798                 .lower_margin   = 525 - 480 - 35,
799                 .sync           = FB_SYNC_CLK_LAT_FALL,
800         },
801         {
802                 /* Emerging ET070001DM6 800 x 480 display.
803                  * 152.4 mm x 91.44 mm display area.
804                  */
805                 .name           = "ET070001DM6",
806                 .refresh        = 60,
807                 .xres           = 800,
808                 .yres           = 480,
809                 .pixclock       = KHZ2PICOS(33260),
810                 .left_margin    = 216 - 128,
811                 .hsync_len      = 128,
812                 .right_margin   = 1056 - 800 - 216,
813                 .upper_margin   = 35 - 2,
814                 .vsync_len      = 2,
815                 .lower_margin   = 525 - 480 - 35,
816                 .sync           = 0,
817         },
818 #else
819         {
820                 /* HannStar HSD100PXN1
821                  * 202.7m mm x 152.06 mm display area.
822                  */
823                 .name           = "HSD100PXN1",
824                 .refresh        = 60,
825                 .xres           = 1024,
826                 .yres           = 768,
827                 .pixclock       = KHZ2PICOS(65000),
828                 .left_margin    = 0,
829                 .hsync_len      = 0,
830                 .right_margin   = 320,
831                 .upper_margin   = 0,
832                 .vsync_len      = 0,
833                 .lower_margin   = 38,
834                 .sync           = FB_SYNC_CLK_LAT_FALL,
835         },
836 #endif
837         {
838                 /* unnamed entry for assigning parameters parsed from 'video_mode' string */
839                 .refresh        = 60,
840                 .left_margin    = 48,
841                 .hsync_len      = 96,
842                 .right_margin   = 16,
843                 .upper_margin   = 31,
844                 .vsync_len      = 2,
845                 .lower_margin   = 12,
846                 .sync           = FB_SYNC_CLK_LAT_FALL,
847         },
848 };
849
850 static int lcd_enabled = 1;
851 static int lcd_bl_polarity;
852
853 static int lcd_backlight_polarity(void)
854 {
855         return lcd_bl_polarity;
856 }
857
858 void lcd_enable(void)
859 {
860         /* HACK ALERT:
861          * global variable from common/lcd.c
862          * Set to 0 here to prevent messages from going to LCD
863          * rather than serial console
864          */
865         lcd_is_enabled = 0;
866
867         karo_load_splashimage(1);
868
869         if (lcd_enabled) {
870                 debug("Switching LCD on\n");
871                 gpio_set_value(TX6_LCD_PWR_GPIO, 1);
872                 udelay(100);
873                 gpio_set_value(TX6_LCD_RST_GPIO, 1);
874                 udelay(300000);
875                 gpio_set_value(TX6_LCD_BACKLIGHT_GPIO,
876                         lcd_backlight_polarity());
877         }
878 }
879
880 void lcd_disable(void)
881 {
882         if (lcd_enabled) {
883                 printf("Disabling LCD\n");
884                 ipuv3_fb_shutdown();
885         }
886 }
887
888 void lcd_panel_disable(void)
889 {
890         if (lcd_enabled) {
891                 debug("Switching LCD off\n");
892                 gpio_set_value(TX6_LCD_BACKLIGHT_GPIO,
893                         !lcd_backlight_polarity());
894                 gpio_set_value(TX6_LCD_RST_GPIO, 0);
895                 gpio_set_value(TX6_LCD_PWR_GPIO, 0);
896         }
897 }
898
899 static const iomux_v3_cfg_t stk5_lcd_pads[] = {
900         /* LCD RESET */
901         MX6_PAD_EIM_D29__GPIO_3_29,
902         /* LCD POWER_ENABLE */
903         MX6_PAD_EIM_EB3__GPIO_2_31,
904         /* LCD Backlight (PWM) */
905         MX6_PAD_GPIO_1__GPIO_1_1,
906
907 #ifndef CONFIG_SYS_LVDS_IF
908         /* Display */
909         MX6_PAD_DISP0_DAT0__IPU1_DISP0_DAT_0,
910         MX6_PAD_DISP0_DAT1__IPU1_DISP0_DAT_1,
911         MX6_PAD_DISP0_DAT2__IPU1_DISP0_DAT_2,
912         MX6_PAD_DISP0_DAT3__IPU1_DISP0_DAT_3,
913         MX6_PAD_DISP0_DAT4__IPU1_DISP0_DAT_4,
914         MX6_PAD_DISP0_DAT5__IPU1_DISP0_DAT_5,
915         MX6_PAD_DISP0_DAT6__IPU1_DISP0_DAT_6,
916         MX6_PAD_DISP0_DAT7__IPU1_DISP0_DAT_7,
917         MX6_PAD_DISP0_DAT8__IPU1_DISP0_DAT_8,
918         MX6_PAD_DISP0_DAT9__IPU1_DISP0_DAT_9,
919         MX6_PAD_DISP0_DAT10__IPU1_DISP0_DAT_10,
920         MX6_PAD_DISP0_DAT11__IPU1_DISP0_DAT_11,
921         MX6_PAD_DISP0_DAT12__IPU1_DISP0_DAT_12,
922         MX6_PAD_DISP0_DAT13__IPU1_DISP0_DAT_13,
923         MX6_PAD_DISP0_DAT14__IPU1_DISP0_DAT_14,
924         MX6_PAD_DISP0_DAT15__IPU1_DISP0_DAT_15,
925         MX6_PAD_DISP0_DAT16__IPU1_DISP0_DAT_16,
926         MX6_PAD_DISP0_DAT17__IPU1_DISP0_DAT_17,
927         MX6_PAD_DISP0_DAT18__IPU1_DISP0_DAT_18,
928         MX6_PAD_DISP0_DAT19__IPU1_DISP0_DAT_19,
929         MX6_PAD_DISP0_DAT20__IPU1_DISP0_DAT_20,
930         MX6_PAD_DISP0_DAT21__IPU1_DISP0_DAT_21,
931         MX6_PAD_DISP0_DAT22__IPU1_DISP0_DAT_22,
932         MX6_PAD_DISP0_DAT23__IPU1_DISP0_DAT_23,
933         MX6_PAD_DI0_PIN2__IPU1_DI0_PIN2, /* HSYNC */
934         MX6_PAD_DI0_PIN3__IPU1_DI0_PIN3, /* VSYNC */
935         MX6_PAD_DI0_PIN15__IPU1_DI0_PIN15, /* OE_ACD */
936         MX6_PAD_DI0_DISP_CLK__IPU1_DI0_DISP_CLK, /* LSCLK */
937 #endif
938 };
939
940 static const struct gpio stk5_lcd_gpios[] = {
941         { TX6_LCD_RST_GPIO, GPIOF_OUTPUT_INIT_LOW, "LCD RESET", },
942         { TX6_LCD_PWR_GPIO, GPIOF_OUTPUT_INIT_LOW, "LCD POWER", },
943         { TX6_LCD_BACKLIGHT_GPIO, GPIOF_OUTPUT_INIT_HIGH, "LCD BACKLIGHT", },
944 };
945
946 void lcd_ctrl_init(void *lcdbase)
947 {
948         int color_depth = 24;
949         const char *video_mode = karo_get_vmode(getenv("video_mode"));
950         const char *vm;
951         unsigned long val;
952         int refresh = 60;
953         struct fb_videomode *p = &tx6_fb_modes[0];
954         struct fb_videomode fb_mode;
955         int xres_set = 0, yres_set = 0, bpp_set = 0, refresh_set = 0;
956         int pix_fmt;
957         int lcd_bus_width;
958         unsigned long di_clk_rate = 65000000;
959
960         if (!lcd_enabled) {
961                 debug("LCD disabled\n");
962                 return;
963         }
964
965         if (had_ctrlc() || (wrsr & WRSR_TOUT)) {
966                 debug("Disabling LCD\n");
967                 lcd_enabled = 0;
968                 setenv("splashimage", NULL);
969                 return;
970         }
971
972         karo_fdt_move_fdt();
973         lcd_bl_polarity = karo_fdt_get_backlight_polarity(working_fdt);
974
975         if (video_mode == NULL) {
976                 debug("Disabling LCD\n");
977                 lcd_enabled = 0;
978                 return;
979         }
980         vm = video_mode;
981         if (karo_fdt_get_fb_mode(working_fdt, video_mode, &fb_mode) == 0) {
982                 p = &fb_mode;
983                 debug("Using video mode from FDT\n");
984                 vm += strlen(vm);
985                 if (fb_mode.xres > panel_info.vl_col ||
986                         fb_mode.yres > panel_info.vl_row) {
987                         printf("video resolution from DT: %dx%d exceeds hardware limits: %dx%d\n",
988                                 fb_mode.xres, fb_mode.yres,
989                                 panel_info.vl_col, panel_info.vl_row);
990                         lcd_enabled = 0;
991                         return;
992                 }
993         }
994         if (p->name != NULL)
995                 debug("Trying compiled-in video modes\n");
996         while (p->name != NULL) {
997                 if (strcmp(p->name, vm) == 0) {
998                         debug("Using video mode: '%s'\n", p->name);
999                         vm += strlen(vm);
1000                         break;
1001                 }
1002                 p++;
1003         }
1004         if (*vm != '\0')
1005                 debug("Trying to decode video_mode: '%s'\n", vm);
1006         while (*vm != '\0') {
1007                 if (*vm >= '0' && *vm <= '9') {
1008                         char *end;
1009
1010                         val = simple_strtoul(vm, &end, 0);
1011                         if (end > vm) {
1012                                 if (!xres_set) {
1013                                         if (val > panel_info.vl_col)
1014                                                 val = panel_info.vl_col;
1015                                         p->xres = val;
1016                                         panel_info.vl_col = val;
1017                                         xres_set = 1;
1018                                 } else if (!yres_set) {
1019                                         if (val > panel_info.vl_row)
1020                                                 val = panel_info.vl_row;
1021                                         p->yres = val;
1022                                         panel_info.vl_row = val;
1023                                         yres_set = 1;
1024                                 } else if (!bpp_set) {
1025                                         switch (val) {
1026                                         case 32:
1027                                         case 24:
1028                                                 if (is_lvds())
1029                                                         pix_fmt = IPU_PIX_FMT_LVDS888;
1030                                                 /* fallthru */
1031                                         case 16:
1032                                         case 8:
1033                                                 color_depth = val;
1034                                                 break;
1035
1036                                         case 18:
1037                                                 if (is_lvds()) {
1038                                                         color_depth = val;
1039                                                         break;
1040                                                 }
1041                                                 /* fallthru */
1042                                         default:
1043                                                 printf("Invalid color depth: '%.*s' in video_mode; using default: '%u'\n",
1044                                                         end - vm, vm, color_depth);
1045                                         }
1046                                         bpp_set = 1;
1047                                 } else if (!refresh_set) {
1048                                         refresh = val;
1049                                         refresh_set = 1;
1050                                 }
1051                         }
1052                         vm = end;
1053                 }
1054                 switch (*vm) {
1055                 case '@':
1056                         bpp_set = 1;
1057                         /* fallthru */
1058                 case '-':
1059                         yres_set = 1;
1060                         /* fallthru */
1061                 case 'x':
1062                         xres_set = 1;
1063                         /* fallthru */
1064                 case 'M':
1065                 case 'R':
1066                         vm++;
1067                         break;
1068
1069                 default:
1070                         if (*vm != '\0')
1071                                 vm++;
1072                 }
1073         }
1074         if (p->xres == 0 || p->yres == 0) {
1075                 printf("Invalid video mode: %s\n", getenv("video_mode"));
1076                 lcd_enabled = 0;
1077                 printf("Supported video modes are:");
1078                 for (p = &tx6_fb_modes[0]; p->name != NULL; p++) {
1079                         printf(" %s", p->name);
1080                 }
1081                 printf("\n");
1082                 return;
1083         }
1084         if (p->xres > panel_info.vl_col || p->yres > panel_info.vl_row) {
1085                 printf("video resolution: %dx%d exceeds hardware limits: %dx%d\n",
1086                         p->xres, p->yres, panel_info.vl_col, panel_info.vl_row);
1087                 lcd_enabled = 0;
1088                 return;
1089         }
1090         panel_info.vl_col = p->xres;
1091         panel_info.vl_row = p->yres;
1092
1093         switch (color_depth) {
1094         case 8:
1095                 panel_info.vl_bpix = LCD_COLOR8;
1096                 break;
1097         case 16:
1098                 panel_info.vl_bpix = LCD_COLOR16;
1099                 break;
1100         default:
1101                 panel_info.vl_bpix = LCD_COLOR24;
1102         }
1103
1104         p->pixclock = KHZ2PICOS(refresh *
1105                 (p->xres + p->left_margin + p->right_margin + p->hsync_len) *
1106                 (p->yres + p->upper_margin + p->lower_margin + p->vsync_len) /
1107                                 1000);
1108         debug("Pixel clock set to %lu.%03lu MHz\n",
1109                 PICOS2KHZ(p->pixclock) / 1000, PICOS2KHZ(p->pixclock) % 1000);
1110
1111         if (p != &fb_mode) {
1112                 int ret;
1113
1114                 debug("Creating new display-timing node from '%s'\n",
1115                         video_mode);
1116                 ret = karo_fdt_create_fb_mode(working_fdt, video_mode, p);
1117                 if (ret)
1118                         printf("Failed to create new display-timing node from '%s': %d\n",
1119                                 video_mode, ret);
1120         }
1121
1122         gpio_request_array(stk5_lcd_gpios, ARRAY_SIZE(stk5_lcd_gpios));
1123         imx_iomux_v3_setup_multiple_pads(stk5_lcd_pads,
1124                                         ARRAY_SIZE(stk5_lcd_pads));
1125
1126         lcd_bus_width = karo_fdt_get_lcd_bus_width(working_fdt, 24);
1127         switch (lcd_bus_width) {
1128         case 24:
1129                 pix_fmt = is_lvds() ? IPU_PIX_FMT_LVDS888 : IPU_PIX_FMT_RGB24;
1130                 break;
1131
1132         case 18:
1133                 pix_fmt = is_lvds() ? IPU_PIX_FMT_LVDS666 : IPU_PIX_FMT_RGB666;
1134                 break;
1135
1136         case 16:
1137                 if (!is_lvds()) {
1138                         pix_fmt = IPU_PIX_FMT_RGB565;
1139                         break;
1140                 }
1141                 /* fallthru */
1142         default:
1143                 lcd_enabled = 0;
1144                 printf("Invalid %s bus width: %d\n", is_lvds() ? "LVDS" : "LCD",
1145                         lcd_bus_width);
1146                 return;
1147         }
1148         if (is_lvds()) {
1149                 int lvds_mapping = karo_fdt_get_lvds_mapping(working_fdt, 0);
1150                 int lvds_chan_mask = karo_fdt_get_lvds_channels(working_fdt);
1151                 uint32_t gpr2;
1152
1153                 if (lvds_chan_mask == 0) {
1154                         printf("No LVDS channel active\n");
1155                         lcd_enabled = 0;
1156                         return;
1157                 }
1158
1159                 gpr2 = (lvds_mapping << 6) | (lvds_mapping << 8);
1160                 if (lcd_bus_width == 24)
1161                         gpr2 |= (1 << 5) | (1 << 7);
1162                 gpr2 |= (lvds_chan_mask & 1) ? 1 << 0 : 0;
1163                 gpr2 |= (lvds_chan_mask & 2) ? 3 << 2 : 0;
1164                 debug("writing %08x to GPR2[%08x]\n", gpr2, IOMUXC_BASE_ADDR + 8);
1165                 writel(gpr2, IOMUXC_BASE_ADDR + 8);
1166         }
1167         if (karo_load_splashimage(0) == 0) {
1168                 int ret;
1169
1170                 debug("Initializing LCD controller\n");
1171                 ret = ipuv3_fb_init(p, 0, pix_fmt, DI_PCLK_PLL3, di_clk_rate, -1);
1172                 if (ret) {
1173                         printf("Failed to initialize FB driver: %d\n", ret);
1174                         lcd_enabled = 0;
1175                 }
1176         } else {
1177                 debug("Skipping initialization of LCD controller\n");
1178         }
1179 }
1180 #else
1181 #define lcd_enabled 0
1182 #endif /* CONFIG_LCD */
1183
1184 static void stk5_board_init(void)
1185 {
1186         gpio_request_array(stk5_gpios, ARRAY_SIZE(stk5_gpios));
1187         imx_iomux_v3_setup_multiple_pads(stk5_pads, ARRAY_SIZE(stk5_pads));
1188 }
1189
1190 static void stk5v3_board_init(void)
1191 {
1192         stk5_board_init();
1193 }
1194
1195 static void stk5v5_board_init(void)
1196 {
1197         stk5_board_init();
1198
1199         gpio_request_one(IMX_GPIO_NR(4, 21), GPIOF_OUTPUT_INIT_HIGH,
1200                         "Flexcan Transceiver");
1201         imx_iomux_v3_setup_pad(MX6_PAD_DISP0_DAT0__GPIO_4_21);
1202 }
1203
1204 static void tx6qdl_set_cpu_clock(void)
1205 {
1206         unsigned long cpu_clk = getenv_ulong("cpu_clk", 10, 0);
1207
1208         if (had_ctrlc() || (wrsr & WRSR_TOUT))
1209                 return;
1210
1211         if (cpu_clk == 0 || cpu_clk == mxc_get_clock(MXC_ARM_CLK) / 1000000)
1212                 return;
1213
1214         if (mxc_set_clock(CONFIG_SYS_MX6_HCLK, cpu_clk, MXC_ARM_CLK) == 0) {
1215                 cpu_clk = mxc_get_clock(MXC_ARM_CLK);
1216                 printf("CPU clock set to %lu.%03lu MHz\n",
1217                         cpu_clk / 1000000, cpu_clk / 1000 % 1000);
1218         } else {
1219                 printf("Error: Failed to set CPU clock to %lu MHz\n", cpu_clk);
1220         }
1221 }
1222
1223 static void tx6_init_mac(void)
1224 {
1225         u8 mac[ETH_ALEN];
1226
1227         imx_get_mac_from_fuse(-1, mac);
1228         if (!is_valid_ether_addr(mac)) {
1229                 printf("No valid MAC address programmed\n");
1230                 return;
1231         }
1232
1233         printf("MAC addr from fuse: %pM\n", mac);
1234         eth_setenv_enetaddr("ethaddr", mac);
1235 }
1236
1237 int board_late_init(void)
1238 {
1239         int ret = 0;
1240         const char *baseboard;
1241
1242         tx6qdl_set_cpu_clock();
1243         karo_fdt_move_fdt();
1244
1245         baseboard = getenv("baseboard");
1246         if (!baseboard)
1247                 goto exit;
1248
1249         printf("Baseboard: %s\n", baseboard);
1250
1251         if (strncmp(baseboard, "stk5", 4) == 0) {
1252                 if ((strlen(baseboard) == 4) ||
1253                         strcmp(baseboard, "stk5-v3") == 0) {
1254                         stk5v3_board_init();
1255                 } else if (strcmp(baseboard, "stk5-v5") == 0) {
1256                         const char *otg_mode = getenv("otg_mode");
1257
1258                         if (otg_mode && strcmp(otg_mode, "host") == 0) {
1259                                 printf("otg_mode='%s' is incompatible with baseboard %s; setting to 'none'\n",
1260                                         otg_mode, baseboard);
1261                                 setenv("otg_mode", "none");
1262                         }
1263                         stk5v5_board_init();
1264                 } else {
1265                         printf("WARNING: Unsupported STK5 board rev.: %s\n",
1266                                 baseboard + 4);
1267                 }
1268         } else {
1269                 printf("WARNING: Unsupported baseboard: '%s'\n",
1270                         baseboard);
1271                 ret = -EINVAL;
1272         }
1273
1274 exit:
1275         tx6_init_mac();
1276
1277         gpio_set_value(TX6_RESET_OUT_GPIO, 1);
1278         clear_ctrlc();
1279         return ret;
1280 }
1281
1282 int checkboard(void)
1283 {
1284         u32 cpurev = get_cpu_rev();
1285         int cpu_variant = (cpurev >> 12) & 0xff;
1286
1287         tx6qdl_print_cpuinfo();
1288
1289         printf("Board: Ka-Ro TX6%c-%d%d1%d\n",
1290                 cpu_variant == MXC_CPU_MX6Q ? 'Q' : 'U',
1291                 cpu_variant == MXC_CPU_MX6Q ? 1 : 8,
1292                 is_lvds(), 1 - PHYS_SDRAM_1_WIDTH / 64);
1293
1294         return 0;
1295 }
1296
1297 #ifdef CONFIG_SERIAL_TAG
1298 void get_board_serial(struct tag_serialnr *serialnr)
1299 {
1300         struct ocotp_regs *ocotp = (struct ocotp_regs *)OCOTP_BASE_ADDR;
1301         struct fuse_bank0_regs *fuse = (void *)ocotp->bank[0].fuse_regs;
1302
1303         serialnr->low = readl(&fuse->cfg0);
1304         serialnr->high = readl(&fuse->cfg1);
1305 }
1306 #endif
1307
1308 #if defined(CONFIG_OF_BOARD_SETUP)
1309 #ifdef CONFIG_FDT_FIXUP_PARTITIONS
1310 #include <jffs2/jffs2.h>
1311 #include <mtd_node.h>
1312 static struct node_info nodes[] = {
1313         { "fsl,imx6q-gpmi-nand", MTD_DEV_TYPE_NAND, },
1314 };
1315 #else
1316 #define fdt_fixup_mtdparts(b,n,c) do { } while (0)
1317 #endif
1318
1319 static const char *tx6_touchpanels[] = {
1320         "ti,tsc2007",
1321         "edt,edt-ft5x06",
1322         "eeti,egalax_ts",
1323 };
1324
1325 void ft_board_setup(void *blob, bd_t *bd)
1326 {
1327         const char *baseboard = getenv("baseboard");
1328         int stk5_v5 = baseboard != NULL && (strcmp(baseboard, "stk5-v5") == 0);
1329         const char *video_mode = karo_get_vmode(getenv("video_mode"));
1330         int ret;
1331
1332         ret = fdt_increase_size(blob, 4096);
1333         if (ret)
1334                 printf("Failed to increase FDT size: %s\n", fdt_strerror(ret));
1335
1336         if (stk5_v5)
1337                 karo_fdt_enable_node(blob, "stk5led", 0);
1338
1339         fdt_fixup_mtdparts(blob, nodes, ARRAY_SIZE(nodes));
1340         fdt_fixup_ethernet(blob);
1341
1342         karo_fdt_fixup_touchpanel(blob, tx6_touchpanels,
1343                                 ARRAY_SIZE(tx6_touchpanels));
1344         karo_fdt_fixup_usb_otg(blob, "usbotg", "fsl,usbphy");
1345         karo_fdt_fixup_flexcan(blob, stk5_v5);
1346
1347         karo_fdt_update_fb_mode(blob, video_mode);
1348 }
1349 #endif /* CONFIG_OF_BOARD_SETUP */