1*94de0730Smatt /* $Id: emi_prep.c,v 1.3 2013/10/07 17:36:40 matt Exp $ */
2da8587eaSjkunz
3da8587eaSjkunz /*
4da8587eaSjkunz * Copyright (c) 2012 The NetBSD Foundation, Inc.
5da8587eaSjkunz * All rights reserved.
6da8587eaSjkunz *
7da8587eaSjkunz * This code is derived from software contributed to The NetBSD Foundation
8da8587eaSjkunz * by Petri Laakso.
9da8587eaSjkunz *
10da8587eaSjkunz * Redistribution and use in source and binary forms, with or without
11da8587eaSjkunz * modification, are permitted provided that the following conditions
12da8587eaSjkunz * are met:
13da8587eaSjkunz * 1. Redistributions of source code must retain the above copyright
14da8587eaSjkunz * notice, this list of conditions and the following disclaimer.
15da8587eaSjkunz * 2. Redistributions in binary form must reproduce the above copyright
16da8587eaSjkunz * notice, this list of conditions and the following disclaimer in the
17da8587eaSjkunz * documentation and/or other materials provided with the distribution.
18da8587eaSjkunz *
19da8587eaSjkunz * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20da8587eaSjkunz * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21da8587eaSjkunz * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22da8587eaSjkunz * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23da8587eaSjkunz * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24da8587eaSjkunz * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25da8587eaSjkunz * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26da8587eaSjkunz * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27da8587eaSjkunz * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28da8587eaSjkunz * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29da8587eaSjkunz * POSSIBILITY OF SUCH DAMAGE.
30da8587eaSjkunz */
31da8587eaSjkunz #include <sys/cdefs.h>
32*94de0730Smatt #include <sys/param.h>
33da8587eaSjkunz #include <sys/types.h>
34da8587eaSjkunz
35da8587eaSjkunz #include <arm/imx/imx23_emireg.h>
36da8587eaSjkunz
37da8587eaSjkunz #include "common.h"
38da8587eaSjkunz
395379dc38Sjkunz static void init_dram_registers(void);
405379dc38Sjkunz static uint32_t get_dram_int_status(void);
41da8587eaSjkunz
42*94de0730Smatt #define DRAM_REGS 41
43*94de0730Smatt
44*94de0730Smatt uint32_t dram_regs[DRAM_REGS] = {
45*94de0730Smatt 0x01010001, 0x00010100, 0x01000101, 0x00000001,
46*94de0730Smatt 0x00000101, 0x00000000, 0x00010000, 0x01000001,
47*94de0730Smatt 0x00000000, 0x00000001, 0x07000200, 0x00070202,
48*94de0730Smatt 0x02020000, 0x04040a01, 0x00000201, 0x02040000,
49*94de0730Smatt 0x02000000, 0x19000f08, 0x0d0d0000, 0x02021313,
50*94de0730Smatt 0x02061521, 0x0000000a, 0x00080008, 0x00200020,
51*94de0730Smatt 0x00200020, 0x00200020, 0x000003f7, 0x00000000,
52*94de0730Smatt 0x00000000, 0x00000020, 0x00000020, 0x00c80000,
53*94de0730Smatt 0x000a23cd, 0x000000c8, 0x00006665, 0x00000000,
54*94de0730Smatt 0x00000101, 0x00040001, 0x00000000, 0x00000000,
55*94de0730Smatt 0x00010000
56*94de0730Smatt };
57*94de0730Smatt
58da8587eaSjkunz /*
59*94de0730Smatt * Initialize DRAM memory.
60da8587eaSjkunz */
61da8587eaSjkunz int
emi_prep(void)62da8587eaSjkunz emi_prep(void)
63da8587eaSjkunz {
64*94de0730Smatt uint32_t tmp_r;
65*94de0730Smatt
66*94de0730Smatt REG_WR(HW_EMI_CTRL_BASE + HW_EMI_CTRL_CLR, HW_EMI_CTRL_SFTRST);
67*94de0730Smatt delay(10000);
68*94de0730Smatt
69*94de0730Smatt tmp_r = REG_RD(HW_DRAM_BASE + HW_DRAM_CTL08);
70*94de0730Smatt tmp_r &= ~(HW_DRAM_CTL08_START | HW_DRAM_CTL08_SREFRESH);
71*94de0730Smatt REG_WR(HW_DRAM_BASE + HW_DRAM_CTL08, tmp_r);
72da8587eaSjkunz
73da8587eaSjkunz init_dram_registers();
74da8587eaSjkunz
75*94de0730Smatt /* START */
76*94de0730Smatt tmp_r = REG_RD(HW_DRAM_BASE + HW_DRAM_CTL08);
77*94de0730Smatt tmp_r |= HW_DRAM_CTL08_START;
78*94de0730Smatt REG_WR(HW_DRAM_BASE + HW_DRAM_CTL08, tmp_r);
79*94de0730Smatt
80*94de0730Smatt delay(20000);
81da8587eaSjkunz
82da8587eaSjkunz /*
83*94de0730Smatt * Set memory power-down with memory
84*94de0730Smatt * clock gating mode (Mode 2).
85da8587eaSjkunz */
86*94de0730Smatt tmp_r = REG_RD(HW_DRAM_BASE + HW_DRAM_CTL16);
87*94de0730Smatt tmp_r |= (1 << 19);
88*94de0730Smatt REG_WR(HW_DRAM_BASE + HW_DRAM_CTL16, tmp_r);
89da8587eaSjkunz
90*94de0730Smatt tmp_r = REG_RD(HW_DRAM_BASE + HW_DRAM_CTL16);
91*94de0730Smatt tmp_r |= (1<<11);
92*94de0730Smatt REG_WR(HW_DRAM_BASE + HW_DRAM_CTL16, tmp_r);
93da8587eaSjkunz
94da8587eaSjkunz /* Wait until DRAM initialization is complete. */
95da8587eaSjkunz while(!(get_dram_int_status() & (1<<2)));
96da8587eaSjkunz
97*94de0730Smatt delay(20000);
98*94de0730Smatt
99*94de0730Smatt return 0;
100*94de0730Smatt }
101*94de0730Smatt /*
102*94de0730Smatt * Set DRAM register values.
103*94de0730Smatt */
104*94de0730Smatt static void
init_dram_registers(void)105*94de0730Smatt init_dram_registers(void)
106*94de0730Smatt {
107*94de0730Smatt volatile uint32_t *dram_r;
108*94de0730Smatt int i;
109*94de0730Smatt
110*94de0730Smatt dram_r = (uint32_t *)(HW_DRAM_BASE);
111*94de0730Smatt
112*94de0730Smatt for (i=0; i < DRAM_REGS; i++) {
113*94de0730Smatt /* Skip ctrl register 8, obsolete registers 27 and 28,
114*94de0730Smatt * read only register 35 */
115*94de0730Smatt if (i == 8 || i == 27 || i == 28 || i == 35)
116*94de0730Smatt continue;
117*94de0730Smatt *(dram_r + i) = dram_regs[i];
118da8587eaSjkunz }
119da8587eaSjkunz
120*94de0730Smatt /* Set tRAS lockout on. */
121*94de0730Smatt *(dram_r + 8) |= HW_DRAM_CTL08_TRAS_LOCKOUT;
122*94de0730Smatt
123*94de0730Smatt return;
124*94de0730Smatt }
125da8587eaSjkunz /*
126da8587eaSjkunz * Return DRAM controller interrupt status register.
127da8587eaSjkunz */
1285379dc38Sjkunz static uint32_t
get_dram_int_status(void)129da8587eaSjkunz get_dram_int_status(void)
130da8587eaSjkunz {
131da8587eaSjkunz uint32_t reg;
132da8587eaSjkunz
1335379dc38Sjkunz reg = REG_RD(HW_DRAM_BASE + HW_DRAM_CTL18);
134da8587eaSjkunz return __SHIFTOUT(reg, HW_DRAM_CTL18_INT_STATUS);
135da8587eaSjkunz }
136