blob: ba59943c808054f42cae61dce0af433438ac19cb [file] [log] [blame]
Ron Madrid5bb907a2009-01-22 15:05:24 -08001/*
2 * Copyright (C) Freescale Semiconductor, Inc. 2006-2007
3 * Copyright (C) Sheldon Instruments, Inc. 2008
4 *
5 * Author: Ron Madrid <info@sheldoninst.com>
6 *
7 * (C) Copyright 2006
8 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
9 *
10 * See file CREDITS for list of people who contributed to this
11 * project.
12 *
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License as
15 * published by the Free Software Foundation; either version 2 of
16 * the License, or (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS for A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
26 * MA 02111-1307 USA
27 */
28
29#include <common.h>
30#include <mpc83xx.h>
31#include <spd_sdram.h>
32#include <asm/bitops.h>
33#include <asm/io.h>
34#include <asm/processor.h>
35#include <asm/mmu.h>
36
37DECLARE_GLOBAL_DATA_PTR;
38
39static long fixed_sdram(void);
40
41#if defined(CONFIG_NAND_SPL)
42void si_wait_i2c(void)
43{
44 volatile immap_t *im = (immap_t *) CONFIG_SYS_IMMR;
45
46 while (!(__raw_readb(&im->i2c[0].sr) & 0x02))
47 ;
48
49 __raw_writeb(0x00, &im->i2c[0].sr);
50
51 sync();
52
53 return;
54}
55
56void si_read_i2c(u32 lbyte, int count, u8 *buffer)
57{
58 volatile immap_t *im = (immap_t *) CONFIG_SYS_IMMR;
59 u32 i;
60 u8 chip = 0x50 << 1; /* boot sequencer I2C */
61 u32 ubyte = (lbyte & 0xff00) >> 8;
62
63 lbyte &= 0xff;
64
65 /*
66 * Set up controller
67 */
68 __raw_writeb(0x3f, &im->i2c[0].fdr);
69 __raw_writeb(0x00, &im->i2c[0].adr);
70 __raw_writeb(0x00, &im->i2c[0].sr);
71 __raw_writeb(0x00, &im->i2c[0].dr);
72
73 while (__raw_readb(&im->i2c[0].sr) & 0x20)
74 ;
75
76 /*
77 * Writing address to device
78 */
79 __raw_writeb(0xb0, &im->i2c[0].cr);
80 sync();
81 __raw_writeb(chip, &im->i2c[0].dr);
82 si_wait_i2c();
83
84 __raw_writeb(0xb0, &im->i2c[0].cr);
85 sync();
86 __raw_writeb(ubyte, &im->i2c[0].dr);
87 si_wait_i2c();
88
89 __raw_writeb(lbyte, &im->i2c[0].dr);
90 si_wait_i2c();
91
92 __raw_writeb(0xb4, &im->i2c[0].cr);
93 sync();
94 __raw_writeb(chip + 1, &im->i2c[0].dr);
95 si_wait_i2c();
96
97 __raw_writeb(0xa0, &im->i2c[0].cr);
98 sync();
99
100 /*
101 * Dummy read
102 */
103 __raw_readb(&im->i2c[0].dr);
104
105 si_wait_i2c();
106
107 /*
108 * Read actual data
109 */
110 for (i = 0; i < count; i++)
111 {
112 if (i == (count - 2)) /* Reached next to last byte, No ACK */
113 __raw_writeb(0xa8, &im->i2c[0].cr);
114 if (i == (count - 1)) /* Reached last byte, STOP */
115 __raw_writeb(0x88, &im->i2c[0].cr);
116
117 /* Read byte of data */
118 buffer[i] = __raw_readb(&im->i2c[0].dr);
119
120 if (i == (count - 1))
121 break;
122 si_wait_i2c();
123 }
124
125 return;
126}
127#endif /* CONFIG_NAND_SPL */
128
129phys_size_t initdram(int board_type)
130{
131 volatile immap_t *im = (immap_t *) CONFIG_SYS_IMMR;
Becky Brucef51cdaf2010-06-17 11:37:20 -0500132 volatile fsl_lbc_t *lbc = &im->im_lbc;
Ron Madrid5bb907a2009-01-22 15:05:24 -0800133 u32 msize;
134
135 if ((__raw_readl(&im->sysconf.immrbar) & IMMRBAR_BASE_ADDR) != (u32) im)
136 return -1;
137
138 /* DDR SDRAM - Main SODIMM */
139 __raw_writel(CONFIG_SYS_DDR_BASE & LAWBAR_BAR, &im->sysconf.ddrlaw[0].bar);
140
141 msize = fixed_sdram();
142
143 /* Local Bus setup lbcr and mrtpr */
144 __raw_writel(CONFIG_SYS_LBC_LBCR, &lbc->lbcr);
145 __raw_writel(CONFIG_SYS_LBC_MRTPR, &lbc->mrtpr);
146 sync();
147
148 /* return total bus SDRAM size(bytes) -- DDR */
149 return (msize * 1024 * 1024);
150}
151
152/*************************************************************************
153 * fixed sdram init -- reads values from boot sequencer I2C
154 ************************************************************************/
155static long fixed_sdram(void)
156{
157 volatile immap_t *im = (immap_t *) CONFIG_SYS_IMMR;
158 u32 msizelog2, msize = 1;
159#if defined(CONFIG_NAND_SPL)
160 u32 i;
161 const u8 bytecount = 135;
162 u8 buffer[bytecount];
163 u32 addr, data;
164
165 si_read_i2c(0, bytecount, buffer);
166
167 for (i = 18; i < bytecount; i += 7){
168 addr = (u32)buffer[i];
169 addr <<= 8;
170 addr |= (u32)buffer[i + 1];
171 addr <<= 2;
172 data = (u32)buffer[i + 2];
173 data <<= 8;
174 data |= (u32)buffer[i + 3];
175 data <<= 8;
176 data |= (u32)buffer[i + 4];
177 data <<= 8;
178 data |= (u32)buffer[i + 5];
179
180 __raw_writel(data, (u32 *)(CONFIG_SYS_IMMR + addr));
181 }
182
183 sync();
184
185 /* enable DDR controller */
186 __raw_writel((__raw_readl(&im->ddr.sdram_cfg) | SDRAM_CFG_MEM_EN), &im->ddr.sdram_cfg);
187#endif /* (CONFIG_NAND_SPL) */
188
189 msizelog2 = ((__raw_readl(&im->sysconf.ddrlaw[0].ar) & LAWAR_SIZE) + 1);
190 msize <<= (msizelog2 - 20);
191
192 return msize;
193}