]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - board/karo/tx48/spl.c
karo: add workaround for Linux hang on i.MX6QP when tzasc1_ipg_clock is disabled
[karo-tx-uboot.git] / board / karo / tx48 / spl.c
1 /*
2  * board/karo/tx48/spl.c
3  * Copyright (C) 2012 Lothar Waßmann <LW@KARO-electronics.de>
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation version 2.
8  *
9  * This program is distributed "as is" WITHOUT ANY WARRANTY of any
10  * kind, whether express or implied; without even the implied warranty
11  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  */
14
15 #include <common.h>
16 #include <errno.h>
17 #include <miiphy.h>
18 #include <cpsw.h>
19 #include <serial.h>
20 #include <libfdt.h>
21 #include <fdt_support.h>
22 #include <nand.h>
23 #include <net.h>
24 #include <spl.h>
25 #include <linux/mtd/nand.h>
26 #include <asm/gpio.h>
27 #include <asm/cache.h>
28 #include <asm/io.h>
29 #include <asm/arch/cpu.h>
30 #include <asm/arch/hardware.h>
31 #include <asm/arch/mmc_host_def.h>
32 #include <asm/arch/mux.h>
33 #include <asm/arch/ddr_defs.h>
34 #include <asm/arch/sys_proto.h>
35 #include <asm/arch/clock.h>
36 #include <asm/arch/clocks_am33xx.h>
37 #include <asm/arch/mem.h>
38 #include <video_fb.h>
39 #include <asm/arch/da8xx-fb.h>
40
41 #include "flash.h"
42
43 DECLARE_GLOBAL_DATA_PTR;
44
45 #define TX48_LCD_BACKLIGHT_GPIO AM33XX_GPIO_NR(3, 14)
46
47 #define UART_RESETDONE          (1 << 0)
48 #define UART_IDLE_MODE(m)       (((m) << 3) & UART_IDLE_MODE_MASK)
49 #define UART_IDLE_MODE_MASK     (0x3 << 3)
50
51 struct pin_mux {
52         short reg_offset;
53         uint8_t val;
54 };
55
56 /*
57  * Configure the pin mux for the module
58  */
59 static inline void tx48_set_pin_mux(const struct pin_mux *pin_mux,
60                         int num_pins)
61 {
62         int i;
63
64         for (i = 0; i < num_pins; i++)
65                 writel(pin_mux[i].val, CTRL_BASE + pin_mux[i].reg_offset);
66 }
67
68 static struct pin_mux tx48_pins[] = {
69 #ifdef CONFIG_NAND
70         { OFFSET(gpmc_ad0), MODE(0) | PULLUP_EN | RXACTIVE, },  /* NAND AD0 */
71         { OFFSET(gpmc_ad1), MODE(0) | PULLUP_EN | RXACTIVE, },  /* NAND AD1 */
72         { OFFSET(gpmc_ad2), MODE(0) | PULLUP_EN | RXACTIVE, },  /* NAND AD2 */
73         { OFFSET(gpmc_ad3), MODE(0) | PULLUP_EN | RXACTIVE, },  /* NAND AD3 */
74         { OFFSET(gpmc_ad4), MODE(0) | PULLUP_EN | RXACTIVE, },  /* NAND AD4 */
75         { OFFSET(gpmc_ad5), MODE(0) | PULLUP_EN | RXACTIVE, },  /* NAND AD5 */
76         { OFFSET(gpmc_ad6), MODE(0) | PULLUP_EN | RXACTIVE, },  /* NAND AD6 */
77         { OFFSET(gpmc_ad7), MODE(0) | PULLUP_EN | RXACTIVE, },  /* NAND AD7 */
78         { OFFSET(gpmc_wait0), MODE(0) | RXACTIVE | PULLUP_EN, }, /* NAND WAIT */
79         { OFFSET(gpmc_wpn), MODE(7) | PULLUP_EN | RXACTIVE, },  /* NAND_WPN */
80         { OFFSET(gpmc_csn0), MODE(0) | PULLUDEN, },     /* NAND_CS0 */
81         { OFFSET(gpmc_advn_ale), MODE(0) | PULLUDEN, }, /* NAND_ADV_ALE */
82         { OFFSET(gpmc_oen_ren), MODE(0) | PULLUDEN, },  /* NAND_OE */
83         { OFFSET(gpmc_wen), MODE(0) | PULLUDEN, },      /* NAND_WEN */
84         { OFFSET(gpmc_be0n_cle), MODE(0) | PULLUDEN, }, /* NAND_BE_CLE */
85 #endif
86         /* I2C0 */
87         { OFFSET(i2c0_sda), MODE(0) | RXACTIVE | PULLUDEN | SLEWCTRL, }, /* I2C_DATA */
88         { OFFSET(i2c0_scl), MODE(0) | RXACTIVE | PULLUDEN | SLEWCTRL, }, /* I2C_SCLK */
89
90 #ifndef CONFIG_NO_ETH
91         /* RMII1 */
92         { OFFSET(mii1_crs), MODE(1) | RXACTIVE, },      /* RMII1_CRS */
93         { OFFSET(mii1_rxerr), MODE(1) | RXACTIVE | PULLUDEN, },  /* RMII1_RXERR */
94         { OFFSET(mii1_txen), MODE(1), },                     /* RMII1_TXEN */
95         { OFFSET(mii1_txd1), MODE(1), },                     /* RMII1_TXD1 */
96         { OFFSET(mii1_txd0), MODE(1), },                     /* RMII1_TXD0 */
97         { OFFSET(mii1_rxd1), MODE(1) | RXACTIVE | PULLUP_EN, }, /* RMII1_RXD1 */
98         { OFFSET(mii1_rxd0), MODE(1) | RXACTIVE | PULLUP_EN, }, /* RMII1_RXD0 */
99         { OFFSET(mdio_data), MODE(0) | RXACTIVE | PULLUP_EN, }, /* MDIO_DATA */
100         { OFFSET(mdio_clk), MODE(0) | PULLUP_EN, },     /* MDIO_CLK */
101         { OFFSET(rmii1_refclk), MODE(0) | RXACTIVE, },  /* RMII1_REFCLK */
102         { OFFSET(emu0), MODE(7) | RXACTIVE},         /* nINT */
103         { OFFSET(emu1), MODE(7), },                  /* nRST */
104 #endif
105 #ifdef CONFIG_OMAP_HSMMC
106         /* MMC1 */
107         { OFFSET(mii1_rxd2), MODE(4) | RXACTIVE | PULLUP_EN, }, /* MMC1_DAT3 */
108         { OFFSET(mii1_rxd3), MODE(4) | RXACTIVE | PULLUP_EN, }, /* MMC1_DAT2 */
109         { OFFSET(mii1_rxclk), MODE(4) | RXACTIVE | PULLUP_EN, }, /* MMC1_DAT1 */
110         { OFFSET(mii1_txclk), MODE(4) | RXACTIVE | PULLUP_EN, }, /* MMC1_DAT0 */
111         { OFFSET(gpmc_csn1), MODE(2) | RXACTIVE | PULLUP_EN, }, /* MMC1_CLK */
112         { OFFSET(gpmc_csn2), MODE(2) | RXACTIVE | PULLUP_EN, }, /* MMC1_CMD */
113         { OFFSET(mcasp0_fsx), MODE(4) | RXACTIVE, },    /* MMC1_CD */
114 #endif
115 };
116
117 static struct gpio tx48_gpios[] = {
118         /* configure this pin early to prevent flicker of the LCD */
119         { TX48_LCD_BACKLIGHT_GPIO, GPIOFLAG_OUTPUT_INIT_HIGH, "LCD BACKLIGHT", },
120 };
121
122 void set_mux_conf_regs(void)
123 {
124         gpio_request_array(tx48_gpios, ARRAY_SIZE(tx48_gpios));
125         tx48_set_pin_mux(tx48_pins, ARRAY_SIZE(tx48_pins));
126 }
127
128 static struct pin_mux tx48_uart_pins[] = {
129 #ifdef CONFIG_SYS_NS16550_COM1
130         /* UART0 for early boot messages */
131         { OFFSET(uart0_rxd), MODE(0) | PULLUP_EN | RXACTIVE, }, /* UART0_RXD */
132         { OFFSET(uart0_txd), MODE(0) | PULLUDEN, },             /* UART0_TXD */
133         { OFFSET(uart0_ctsn), MODE(0) | PULLUP_EN | RXACTIVE, },/* UART0_CTS */
134         { OFFSET(uart0_rtsn), MODE(0) | PULLUDEN, },            /* UART0_RTS */
135 #endif
136 #ifdef CONFIG_SYS_NS16550_COM2
137         /* UART1 */
138         { OFFSET(uart1_rxd), MODE(0) | PULLUP_EN | RXACTIVE, }, /* UART1_RXD */
139         { OFFSET(uart1_txd), MODE(0) | PULLUDEN, },             /* UART1_TXD */
140         { OFFSET(uart1_ctsn), MODE(0) | PULLUP_EN | RXACTIVE, },/* UART1_CTS */
141         { OFFSET(uart1_rtsn), MODE(0) | PULLUDEN, },            /* UART1_RTS */
142 #endif
143 #ifdef CONFIG_SYS_NS16550_COM3
144         /* UART5 */
145         { OFFSET(mii1_rxdv), MODE(3) | PULLUP_EN | RXACTIVE, }, /* UART5_RXD */
146         { OFFSET(mii1_col), MODE(3) | PULLUDEN, },              /* UART5_TXD */
147         { OFFSET(mmc0_dat1), MODE(2) | PULLUP_EN | RXACTIVE, }, /* UART5_CTS */
148         { OFFSET(mmc0_dat0), MODE(2) | PULLUDEN, },             /* UART5_RTS */
149 #endif
150 };
151
152 /*
153  * early system init of muxing and clocks.
154  */
155 void set_uart_mux_conf(void)
156 {
157         tx48_set_pin_mux(tx48_uart_pins, ARRAY_SIZE(tx48_uart_pins));
158 }
159
160 static const u32 gpmc_nand_cfg[GPMC_MAX_REG] = {
161         TX48_NAND_GPMC_CONFIG1,
162         TX48_NAND_GPMC_CONFIG2,
163         TX48_NAND_GPMC_CONFIG3,
164         TX48_NAND_GPMC_CONFIG4,
165         TX48_NAND_GPMC_CONFIG5,
166         TX48_NAND_GPMC_CONFIG6,
167 };
168
169 #define SDRAM_CLK               CONFIG_SYS_DDR_CLK
170
171 #define ns_TO_ck(ns)            (((ns) * SDRAM_CLK + 999) / 1000)
172 #define ck_TO_ns(ck)            ((ck) * 1000 / SDRAM_CLK)
173
174 #ifdef DEBUG
175 static inline unsigned ck_val_check(unsigned ck, unsigned offs, unsigned max,
176                         const char *name)
177 {
178         if (ck < offs) {
179                 printf("value %u for parameter %s is out of range (min: %u\n",
180                         ck, name, offs);
181                 hang();
182         }
183         if (ck > max) {
184                 printf("value %u for parameter %s is out of range (max: %u\n",
185                         ck, name, max);
186                 hang();
187         }
188         return ck - offs;
189 }
190 #define CK_VAL(ck, offs, max)   ck_val_check(ck, offs, max, #ck)
191 #else
192 #define CK_VAL(ck, offs, max)   ((ck) - (offs))
193 #endif
194
195 #define DDR3_NT5CB128           1
196 #define DDR3_H5TQ2G8            2
197
198 #if 1
199 #define SDRAM_TYPE DDR3_NT5CB128
200 #else
201 #define SDRAM_TYPE DDR3_H5TQ2G8
202 #endif
203
204 #ifndef SDRAM_TYPE
205 #error No SDRAM_TYPE specified
206 #elif (SDRAM_TYPE == DDR3_NT5CB128) || (SDRAM_TYPE == DDR3_H5TQ2G8)
207 #define tRP                     ns_TO_ck(14)
208 #define tRCD                    ns_TO_ck(14)
209 #define tWR                     ns_TO_ck(15)
210 #define tRAS                    ns_TO_ck(35)
211 #define tRC                     ns_TO_ck(49)
212 #define tRRD                    max(ns_TO_ck(8), 4)
213 #define tWTR                    max(ns_TO_ck(8), 4)
214
215 #define tXP                     max(ns_TO_ck(6), 3)
216 #define tXPR                    max(5, ns_TO_ck(ck_TO_ns(tRFC + 1) + 10))
217 #define tODT                    ns_TO_ck(9)
218 #define tXSNR                   max(5, ns_TO_ck(ck_TO_ns(tRFC + 1) + 10))
219 #define tXSRD                   512
220 #define tRTP                    max(ns_TO_ck(8), 4)
221 #define tCKE                    max(ns_TO_ck(6), 3)
222
223 #define tPDLL_UL                512
224 #define tZQCS                   64
225 #define tRFC                    ns_TO_ck(160)
226 #define tRAS_MAX                0xf
227
228 static inline int cwl(u32 sdram_clk)
229 {
230         if (sdram_clk <= 300)
231                 return 5;
232         else if (sdram_clk > 300 && sdram_clk <= 333)
233                 return 5;
234         else if (sdram_clk > 333 && sdram_clk <= 400)
235                 return 5;
236         else if (sdram_clk > 400 && sdram_clk <= 533)
237                 return 6;
238         else if (sdram_clk > 533 && sdram_clk <= 666)
239                 return 7;
240         else if (SDRAM_TYPE != DDR3_H5TQ2G8)
241                 ;
242         else if (sdram_clk > 666 && sdram_clk <= 800)
243                 return 8;
244
245         printf("SDRAM clock out of range\n");
246         hang();
247 }
248 #define CWL cwl(SDRAM_CLK)
249
250 static inline int cl(u32 sdram_clk)
251 {
252         if (sdram_clk <= 300)
253                 return 5;
254         else if (sdram_clk > 300 && sdram_clk <= 333)
255                 return 5;
256         else if (sdram_clk > 333 && sdram_clk <= 400)
257                 return 6;
258         else if (sdram_clk > 400 && sdram_clk <= 533)
259                 return 8;
260         else if (sdram_clk > 533 && sdram_clk <= 666)
261                 return (SDRAM_TYPE == DDR3_H5TQ2G8) ? 10 : 9;
262         else if (SDRAM_TYPE != DDR3_H5TQ2G8)
263                 ;
264         else if (sdram_clk > 666 && sdram_clk <= 800)
265                 return 11;
266
267         printf("SDRAM clock out of range\n");
268         hang();
269 }
270 #define CL cl(SDRAM_CLK)
271
272 #define ROW_ADDR_BITS           14
273 #define SDRAM_PG_SIZE           1024
274 #else
275 #error Unsupported SDRAM_TYPE specified
276 #endif
277
278 #define SDRAM_CONFIG_VAL        (                                       \
279                 (3 << 29) /* SDRAM type: 0: DDR1 1: LPDDR1 2: DDR2 3: DDR3 */ | \
280                 (0 << 27) /* IBANK pos */ |                             \
281                 (2 << 24) /* termination resistor value 0: disable 1: RZQ/4 2: RZQ/2 3: RZQ/6 4: RZQ/12 5: RZQ/8 */ | \
282                 (0 << 23) /* DDR2 differential DQS */ |                 \
283                 (1 << 21) /* dynamic ODT 0: off 1: RZQ/4 2: RZQ/2 */ |  \
284                 (0 << 20) /* DLL disable */ |                           \
285                 (1 << 18) /* drive strength 0: RZQ/6 1: RZQ/7 */ |      \
286                 ((CWL - 5) << 16) /* CWL 0: 5 ... 3: 8 */ |             \
287                 (1 << 14) /* SDRAM data bus width 0: 32 1: 16 */ |      \
288                 (((CL - 4) * 2) << 10) /* CAS latency 2: 5 4: 6 6: 8 ... 14: 11 (DDR3) */ | \
289                 ((ROW_ADDR_BITS - 9) << 7) /* # of row addr bits 0: 9 ... 7: 16 */ | \
290                 (3 << 4) /* # of SDRAM internal banks 0: 1 1: 2 2: 4 3: 8 */ | \
291                 (0 << 3) /* # of CS lines */ |                          \
292                 ((ffs(SDRAM_PG_SIZE / 256) - 1) << 0) /* page size 0: 256 1: 512 2: 1024 3:2048 */ | \
293                 0)
294
295 #define SDREF_VAL               (                                       \
296                 (0 << 31) /* */ |                                       \
297                 (1 << 29) /* self refresh temperature range 1: extended temp range */ | \
298                 (0 << 28) /* auto self refresh enable */ |              \
299                 (0 << 24) /* partial array self refresh */ |            \
300                 ((SDRAM_CLK * 7800 / 1000) << 0) /* refresh interval */ | \
301                 0)
302
303 #define tFAW            ns_TO_ck(45)
304
305 #define SDRAM_TIM1_VAL  ((CK_VAL(tRP, 1, 16) << 25) |   \
306                          (CK_VAL(tRCD, 1, 16) << 21) |  \
307                          (CK_VAL(tWR, 1, 16) << 17) |   \
308                          (CK_VAL(tRAS, 1, 32) << 12) |  \
309                          (CK_VAL(tRC, 1, 64) << 6) |    \
310                          (CK_VAL(tRRD, 1, 8) << 3) |    \
311                          (CK_VAL(tWTR, 1, 8) << 0))
312
313 #define SDRAM_TIM2_VAL  ((CK_VAL(max(tCKE, tXP), 1, 8) << 28) | \
314                          (CK_VAL(tODT, 0, 8) << 25) |           \
315                          (CK_VAL(tXSNR, 1, 128) << 16) |        \
316                          (CK_VAL(tXSRD, 1, 1024) << 6) |        \
317                          (CK_VAL(tRTP, 1, 8) << 3) |            \
318                          (CK_VAL(tCKE, 1, 8) << 0))
319
320 #define SDRAM_TIM3_VAL  ((CK_VAL(DIV_ROUND_UP(tPDLL_UL, 128), 0, 16) << 28) | \
321                          (CK_VAL(tZQCS, 1, 64) << 15) |                 \
322                          (CK_VAL(tRFC, 1, 1024) << 4) |                 \
323                          (CK_VAL(tRAS_MAX, 0, 16) << 0))
324
325 #define ZQ_CONFIG_VAL           (                                       \
326                 (1 << 31) /* ZQ calib for CS1 */ |                      \
327                 (0 << 30) /* ZQ calib for CS0 */ |                      \
328                 (0 << 29) /* dual calib */ |                            \
329                 (1 << 28) /* ZQ calib on SR/PWDN exit */ |              \
330                 (2 << 18) /* ZQCL intervals for ZQINIT */ |             \
331                 (4 << 16) /* ZQCS intervals for ZQCL */ |               \
332                 (80 << 0) /* refr periods between ZQCS commands */ |    \
333                 0)
334
335 #if 1
336 static struct ddr_data tx48_ddr3_data = {
337         /* reset defaults */
338         .datardsratio0 = 0x04010040,
339         .datawdsratio0 = 0x0,
340         .datafwsratio0 = 0x0,
341         .datawrsratio0 = 0x04010040,
342 };
343
344 static struct cmd_control tx48_ddr3_cmd_ctrl_data = {
345         /* reset defaults */
346         .cmd0csratio = 0x80,
347         .cmd1csratio = 0x80,
348         .cmd2csratio = 0x80,
349 };
350
351 static void ddr3_calib_start(void)
352 {
353         static struct emif_reg_struct *emif_reg = (void *)EMIF4_0_CFG_BASE;
354         int loops = 0;
355         u32 regval;
356         u32 emif_status;
357
358         debug("Starting DDR3 calibration\n");
359
360         /* wait for DDR PHY ready */
361         while (!((emif_status = readl(&emif_reg->emif_status)) & (1 << 2))) {
362                 if (loops++ > 100000)
363                         break;
364                 udelay(1);
365         }
366         debug("EMIF status: %08x after %u loops\n", emif_status, loops);
367
368         /* enable DDR3 write levelling */
369         loops = 0;
370         writel(EMIF_REG_RDWRLVLFULL_START_MASK, &emif_reg->emif_rd_wr_lvl_ctl);
371         do {
372                 regval = readl(&emif_reg->emif_rd_wr_lvl_ctl);
373                 if (!(regval & EMIF_REG_RDWRLVLFULL_START_MASK))
374                         break;
375                 udelay(1);
376         } while (loops++ < 100000);
377         if (regval & EMIF_REG_RDWRLVLFULL_START_MASK) {
378                 printf("Full WRLVL timed out\n");
379         } else {
380                 debug("Full Write Levelling done after %u us\n", loops);
381         }
382         writel(0, &emif_reg->emif_rd_wr_lvl_rmp_ctl);
383         writel(0, &emif_reg->emif_rd_wr_lvl_rmp_win);
384         writel(0x0f808080, &emif_reg->emif_rd_wr_lvl_ctl);
385         debug("DDR3 calibration done\n");
386 }
387
388 void sdram_init(void)
389 {
390         struct emif_regs r = {0};
391
392         debug("Initialising SDRAM timing for %u MHz DDR clock\n", SDRAM_CLK);
393
394         r.sdram_config = SDRAM_CONFIG_VAL;
395         r.ref_ctrl = SDREF_VAL;
396         r.sdram_tim1 = SDRAM_TIM1_VAL;
397         r.sdram_tim2 = SDRAM_TIM2_VAL;
398         r.sdram_tim3 = SDRAM_TIM3_VAL;
399         r.zq_config = ZQ_CONFIG_VAL;
400         r.emif_ddr_phy_ctlr_1 = 0x0000030b;
401
402         config_ddr(SDRAM_CLK, NULL, &tx48_ddr3_data,
403                 &tx48_ddr3_cmd_ctrl_data, &r, 0);
404
405         ddr3_calib_start();
406
407         debug("%s: config_ddr done\n", __func__);
408 }
409 #endif
410 #if 0
411 #ifdef CONFIG_HW_WATCHDOG
412 static inline void tx48_wdog_disable(void)
413 {
414 }
415 #else
416 static inline void tx48_wdog_disable(void)
417 {
418         struct wd_timer *wdtimer = (struct wd_timer *)WDT_BASE;
419
420         /* WDT1 is already running when the bootloader gets control
421          * Disable it to avoid "random" resets
422          */
423         writel(0xAAAA, &wdtimer->wdtwspr);
424         while (readl(&wdtimer->wdtwwps) != 0x0)
425                 ;
426         writel(0x5555, &wdtimer->wdtwspr);
427         while (readl(&wdtimer->wdtwwps) != 0x0)
428                 ;
429 }
430 #endif
431 #endif
432
433 #define OSC     (V_OSCK/1000000)
434 static const struct dpll_params tx48_ddr_params = {
435         266, OSC-1, 1, -1, -1, -1, -1,
436 };
437
438 const struct dpll_params *get_dpll_ddr_params(void)
439 {
440         return &tx48_ddr_params;
441 }
442
443 void am33xx_spl_board_init(void)
444 {
445         struct gpmc *gpmc_cfg = (struct gpmc *)GPMC_BASE;
446
447         enable_gpmc_cs_config(gpmc_nand_cfg, &gpmc_cfg->cs[0],
448                         CONFIG_SYS_NAND_BASE, CONFIG_SYS_NAND_SIZE);
449 }