2 * board/karo/tx48/spl.c
3 * Copyright (C) 2012 Lothar Waßmann <LW@KARO-electronics.de>
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.
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.
21 #include <fdt_support.h>
25 #include <linux/mtd/nand.h>
27 #include <asm/cache.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>
39 #include <asm/arch/da8xx-fb.h>
43 DECLARE_GLOBAL_DATA_PTR;
45 #define TX48_LCD_BACKLIGHT_GPIO AM33XX_GPIO_NR(3, 14)
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)
57 * Configure the pin mux for the module
59 static inline void tx48_set_pin_mux(const struct pin_mux *pin_mux,
64 for (i = 0; i < num_pins; i++)
65 writel(pin_mux[i].val, CTRL_BASE + pin_mux[i].reg_offset);
68 static struct pin_mux tx48_pins[] = {
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 */
87 { OFFSET(i2c0_sda), MODE(0) | RXACTIVE | PULLUDEN | SLEWCTRL, }, /* I2C_DATA */
88 { OFFSET(i2c0_scl), MODE(0) | RXACTIVE | PULLUDEN | SLEWCTRL, }, /* I2C_SCLK */
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 */
105 #ifdef CONFIG_OMAP_HSMMC
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 */
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", },
122 void set_mux_conf_regs(void)
124 gpio_request_array(tx48_gpios, ARRAY_SIZE(tx48_gpios));
125 tx48_set_pin_mux(tx48_pins, ARRAY_SIZE(tx48_pins));
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 */
136 #ifdef CONFIG_SYS_NS16550_COM2
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 */
143 #ifdef CONFIG_SYS_NS16550_COM3
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 */
153 * early system init of muxing and clocks.
155 void set_uart_mux_conf(void)
157 tx48_set_pin_mux(tx48_uart_pins, ARRAY_SIZE(tx48_uart_pins));
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,
169 #define SDRAM_CLK CONFIG_SYS_DDR_CLK
171 #define ns_TO_ck(ns) (((ns) * SDRAM_CLK + 999) / 1000)
172 #define ck_TO_ns(ck) ((ck) * 1000 / SDRAM_CLK)
175 static inline unsigned ck_val_check(unsigned ck, unsigned offs, unsigned max,
179 printf("value %u for parameter %s is out of range (min: %u\n",
184 printf("value %u for parameter %s is out of range (max: %u\n",
190 #define CK_VAL(ck, offs, max) ck_val_check(ck, offs, max, #ck)
192 #define CK_VAL(ck, offs, max) ((ck) - (offs))
195 #define DDR3_NT5CB128 1
196 #define DDR3_H5TQ2G8 2
199 #define SDRAM_TYPE DDR3_NT5CB128
201 #define SDRAM_TYPE DDR3_H5TQ2G8
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)
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))
220 #define tRTP max(ns_TO_ck(8), 4)
221 #define tCKE max(ns_TO_ck(6), 3)
225 #define tRFC ns_TO_ck(160)
228 static inline int cwl(u32 sdram_clk)
230 if (sdram_clk <= 300)
232 else if (sdram_clk > 300 && sdram_clk <= 333)
234 else if (sdram_clk > 333 && sdram_clk <= 400)
236 else if (sdram_clk > 400 && sdram_clk <= 533)
238 else if (sdram_clk > 533 && sdram_clk <= 666)
240 else if (SDRAM_TYPE != DDR3_H5TQ2G8)
242 else if (sdram_clk > 666 && sdram_clk <= 800)
245 printf("SDRAM clock out of range\n");
248 #define CWL cwl(SDRAM_CLK)
250 static inline int cl(u32 sdram_clk)
252 if (sdram_clk <= 300)
254 else if (sdram_clk > 300 && sdram_clk <= 333)
256 else if (sdram_clk > 333 && sdram_clk <= 400)
258 else if (sdram_clk > 400 && sdram_clk <= 533)
260 else if (sdram_clk > 533 && sdram_clk <= 666)
261 return (SDRAM_TYPE == DDR3_H5TQ2G8) ? 10 : 9;
262 else if (SDRAM_TYPE != DDR3_H5TQ2G8)
264 else if (sdram_clk > 666 && sdram_clk <= 800)
267 printf("SDRAM clock out of range\n");
270 #define CL cl(SDRAM_CLK)
272 #define ROW_ADDR_BITS 14
273 #define SDRAM_PG_SIZE 1024
275 #error Unsupported SDRAM_TYPE specified
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 */ | \
295 #define SDREF_VAL ( \
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 */ | \
303 #define tFAW ns_TO_ck(45)
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))
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))
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))
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 */ | \
336 static struct ddr_data tx48_ddr3_data = {
338 .datardsratio0 = 0x04010040,
339 .datawdsratio0 = 0x0,
340 .datafwsratio0 = 0x0,
341 .datawrsratio0 = 0x04010040,
344 static struct cmd_control tx48_ddr3_cmd_ctrl_data = {
351 static void ddr3_calib_start(void)
353 static struct emif_reg_struct *emif_reg = (void *)EMIF4_0_CFG_BASE;
358 debug("Starting DDR3 calibration\n");
360 /* wait for DDR PHY ready */
361 while (!((emif_status = readl(&emif_reg->emif_status)) & (1 << 2))) {
362 if (loops++ > 100000)
366 debug("EMIF status: %08x after %u loops\n", emif_status, loops);
368 /* enable DDR3 write levelling */
370 writel(EMIF_REG_RDWRLVLFULL_START_MASK, &emif_reg->emif_rd_wr_lvl_ctl);
372 regval = readl(&emif_reg->emif_rd_wr_lvl_ctl);
373 if (!(regval & EMIF_REG_RDWRLVLFULL_START_MASK))
376 } while (loops++ < 100000);
377 if (regval & EMIF_REG_RDWRLVLFULL_START_MASK) {
378 printf("Full WRLVL timed out\n");
380 debug("Full Write Levelling done after %u us\n", loops);
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");
388 void sdram_init(void)
390 struct emif_regs r = {0};
392 debug("Initialising SDRAM timing for %u MHz DDR clock\n", SDRAM_CLK);
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;
402 config_ddr(SDRAM_CLK, NULL, &tx48_ddr3_data,
403 &tx48_ddr3_cmd_ctrl_data, &r, 0);
407 debug("%s: config_ddr done\n", __func__);
411 #ifdef CONFIG_HW_WATCHDOG
412 static inline void tx48_wdog_disable(void)
416 static inline void tx48_wdog_disable(void)
418 struct wd_timer *wdtimer = (struct wd_timer *)WDT_BASE;
420 /* WDT1 is already running when the bootloader gets control
421 * Disable it to avoid "random" resets
423 writel(0xAAAA, &wdtimer->wdtwspr);
424 while (readl(&wdtimer->wdtwwps) != 0x0)
426 writel(0x5555, &wdtimer->wdtwspr);
427 while (readl(&wdtimer->wdtwwps) != 0x0)
433 #define OSC (V_OSCK/1000000)
434 static const struct dpll_params tx48_ddr_params = {
435 266, OSC-1, 1, -1, -1, -1, -1,
438 const struct dpll_params *get_dpll_ddr_params(void)
440 return &tx48_ddr_params;
443 void am33xx_spl_board_init(void)
445 struct gpmc *gpmc_cfg = (struct gpmc *)GPMC_BASE;
447 enable_gpmc_cs_config(gpmc_nand_cfg, &gpmc_cfg->cs[0],
448 CONFIG_SYS_NAND_BASE, CONFIG_SYS_NAND_SIZE);