3ecb74a0c8ea453d9c96032c4a8b9c1461bf18d2
[oweals/u-boot.git] / board / hmi1001 / hmi1001.c
1 /*
2  * (C) Copyright 2003-2004
3  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4  *
5  * (C) Copyright 2004
6  * Mark Jonas, Freescale Semiconductor, mark.jonas@motorola.com.
7  *
8  * (C) Copyright 2004
9  * Martin Krause, TQ-Systems GmbH, martin.krause@tqs.de
10  *
11  * See file CREDITS for list of people who contributed to this
12  * project.
13  *
14  * This program is free software; you can redistribute it and/or
15  * modify it under the terms of the GNU General Public License as
16  * published by the Free Software Foundation; either version 2 of
17  * the License, or (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, write to the Free Software
26  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
27  * MA 02111-1307 USA
28  */
29
30 #include <common.h>
31 #include <mpc5xxx.h>
32 #include <pci.h>
33 #include <malloc.h>
34
35 #ifndef CFG_RAMBOOT
36 static void sdram_start (int hi_addr)
37 {
38         long hi_addr_bit = hi_addr ? 0x01000000 : 0;
39
40         /* unlock mode register */
41         *(vu_long *)MPC5XXX_SDRAM_CTRL = SDRAM_CONTROL | 0x80000000 | hi_addr_bit;
42         __asm__ volatile ("sync");
43
44         /* precharge all banks */
45         *(vu_long *)MPC5XXX_SDRAM_CTRL = SDRAM_CONTROL | 0x80000002 | hi_addr_bit;
46         __asm__ volatile ("sync");
47
48 #if SDRAM_DDR
49         /* set mode register: extended mode */
50         *(vu_long *)MPC5XXX_SDRAM_MODE = SDRAM_EMODE;
51         __asm__ volatile ("sync");
52
53         /* set mode register: reset DLL */
54         *(vu_long *)MPC5XXX_SDRAM_MODE = SDRAM_MODE | 0x04000000;
55         __asm__ volatile ("sync");
56 #endif
57
58         /* precharge all banks */
59         *(vu_long *)MPC5XXX_SDRAM_CTRL = SDRAM_CONTROL | 0x80000002 | hi_addr_bit;
60         __asm__ volatile ("sync");
61
62         /* auto refresh */
63         *(vu_long *)MPC5XXX_SDRAM_CTRL = SDRAM_CONTROL | 0x80000004 | hi_addr_bit;
64         __asm__ volatile ("sync");
65
66         /* set mode register */
67         *(vu_long *)MPC5XXX_SDRAM_MODE = SDRAM_MODE;
68         __asm__ volatile ("sync");
69
70         /* normal operation */
71         *(vu_long *)MPC5XXX_SDRAM_CTRL = SDRAM_CONTROL | hi_addr_bit;
72         __asm__ volatile ("sync");
73 }
74 #endif
75
76 /*
77  * ATTENTION: Although partially referenced initdram does NOT make real use
78  *            use of CFG_SDRAM_BASE. The code does not work if CFG_SDRAM_BASE
79  *            is something else than 0x00000000.
80  */
81
82 long int initdram (int board_type)
83 {
84         ulong dramsize = 0;
85 #ifndef CFG_RAMBOOT
86         ulong test1, test2;
87
88         /* setup SDRAM chip selects */
89         *(vu_long *)MPC5XXX_SDRAM_CS0CFG = 0x0000001c; /* 512MB at 0x0 */
90         *(vu_long *)MPC5XXX_SDRAM_CS1CFG = 0x40000000; /* disabled */
91         __asm__ volatile ("sync");
92
93         /* setup config registers */
94         *(vu_long *)MPC5XXX_SDRAM_CONFIG1 = SDRAM_CONFIG1;
95         *(vu_long *)MPC5XXX_SDRAM_CONFIG2 = SDRAM_CONFIG2;
96         __asm__ volatile ("sync");
97
98 #if SDRAM_DDR
99         /* set tap delay */
100         *(vu_long *)MPC5XXX_CDM_PORCFG = SDRAM_TAPDELAY;
101         __asm__ volatile ("sync");
102 #endif
103
104         /* find RAM size using SDRAM CS0 only */
105         sdram_start(0);
106         test1 = get_ram_size((long *)CFG_SDRAM_BASE, 0x20000000);
107         sdram_start(1);
108         test2 = get_ram_size((long *)CFG_SDRAM_BASE, 0x20000000);
109         if (test1 > test2) {
110                 sdram_start(0);
111                 dramsize = test1;
112         } else {
113                 dramsize = test2;
114         }
115
116         /* memory smaller than 1MB is impossible */
117         if (dramsize < (1 << 20)) {
118                 dramsize = 0;
119         }
120
121         /* set SDRAM CS0 size according to the amount of RAM found */
122         if (dramsize > 0) {
123                 *(vu_long *)MPC5XXX_SDRAM_CS0CFG = 0x13 +
124                         __builtin_ffs(dramsize >> 20) - 1;
125         } else {
126                 *(vu_long *)MPC5XXX_SDRAM_CS0CFG = 0; /* disabled */
127         }
128
129         *(vu_long *)MPC5XXX_SDRAM_CS1CFG = dramsize; /* disabled */
130 #else /* CFG_RAMBOOT */
131
132         /* retrieve size of memory connected to SDRAM CS0 */
133         dramsize = *(vu_long *)MPC5XXX_SDRAM_CS0CFG & 0xFF;
134         if (dramsize >= 0x13) {
135                 dramsize = (1 << (dramsize - 0x13)) << 20;
136         } else {
137                 dramsize = 0;
138         }
139
140         /* retrieve size of memory connected to SDRAM CS1 */
141         dramsize2 = *(vu_long *)MPC5XXX_SDRAM_CS1CFG & 0xFF;
142         if (dramsize2 >= 0x13) {
143                 dramsize2 = (1 << (dramsize2 - 0x13)) << 20;
144         } else {
145                 dramsize2 = 0;
146         }
147
148 #endif /* CFG_RAMBOOT */
149
150         /*
151          * On MPC5200B we need to set the special configuration delay in the
152          * DDR controller. Please refer to Freescale's AN3221 "MPC5200B SDRAM
153          * Initialization and Configuration", 3.3.1 SDelay--MBAR + 0x0190:
154          *
155          * "The SDelay should be written to a value of 0x00000004. It is
156          * required to account for changes caused by normal wafer processing
157          * parameters."
158          */
159         svr = get_svr();
160         pvr = get_pvr();
161         if ((SVR_MJREV(svr) >= 2) &&
162             (PVR_MAJ(pvr) == 1) && (PVR_MIN(pvr) == 4)) {
163
164                 *(vu_long *)MPC5XXX_SDRAM_SDELAY = 0x04;
165                 __asm__ volatile ("sync");
166         }
167
168 /*      return dramsize + dramsize2; */
169         return dramsize;
170 }
171
172 int checkboard (void)
173 {
174         puts ("Board: HMI1001\n");
175         return 0;
176 }
177
178 #ifdef CONFIG_PREBOOT
179
180 static uchar kbd_magic_prefix[]         = "key_magic";
181 static uchar kbd_command_prefix[]       = "key_cmd";
182
183 #define S1_ROT  0xf0
184 #define S2_Q    0x40
185 #define S2_M    0x20
186
187 struct kbd_data_t {
188         char s1;
189         char s2;
190 };
191
192 struct kbd_data_t* get_keys (struct kbd_data_t *kbd_data)
193 {
194         kbd_data->s1 = *((volatile uchar*)(CFG_STATUS1_BASE));
195         kbd_data->s2 = *((volatile uchar*)(CFG_STATUS2_BASE));
196
197         return kbd_data;
198 }
199
200 static int compare_magic (const struct kbd_data_t *kbd_data, char *str)
201 {
202         char s1 = str[0];
203         char s2;
204
205         if (s1 >= '0' && s1 <= '9')
206                 s1 -= '0';
207         else if (s1 >= 'a' && s1 <= 'f')
208                 s1 = s1 - 'a' + 10;
209         else if (s1 >= 'A' && s1 <= 'F')
210                 s1 = s1 - 'A' + 10;
211         else
212                 return -1;
213
214         if (((S1_ROT & kbd_data->s1) >> 4) != s1)
215                 return -1;
216
217         s2 = (S2_Q | S2_M) & kbd_data->s2;
218
219         switch (str[1]) {
220         case 'q':
221         case 'Q':
222                 if (s2 == S2_Q)
223                         return -1;
224                 break;
225         case 'm':
226         case 'M':
227                 if (s2 == S2_M)
228                         return -1;
229                 break;
230         case '\0':
231                 if (s2 == (S2_Q | S2_M))
232                         return 0;
233         default:
234                 return -1;
235         }
236
237         if (str[2])
238                 return -1;
239
240         return 0;
241 }
242
243 static char *key_match (const struct kbd_data_t *kbd_data)
244 {
245         char magic[sizeof (kbd_magic_prefix) + 1];
246         char *suffix;
247         char *kbd_magic_keys;
248
249         /*
250          * The following string defines the characters that can be appended
251          * to "key_magic" to form the names of environment variables that
252          * hold "magic" key codes, i. e. such key codes that can cause
253          * pre-boot actions. If the string is empty (""), then only
254          * "key_magic" is checked (old behaviour); the string "125" causes
255          * checks for "key_magic1", "key_magic2" and "key_magic5", etc.
256          */
257         if ((kbd_magic_keys = getenv ("magic_keys")) == NULL)
258                 kbd_magic_keys = "";
259
260         /* loop over all magic keys;
261          * use '\0' suffix in case of empty string
262          */
263         for (suffix = kbd_magic_keys; *suffix ||
264                      suffix == kbd_magic_keys; ++suffix) {
265                 sprintf (magic, "%s%c", kbd_magic_prefix, *suffix);
266
267                 if (compare_magic(kbd_data, getenv(magic)) == 0) {
268                         char cmd_name[sizeof (kbd_command_prefix) + 1];
269                         char *cmd;
270
271                         sprintf (cmd_name, "%s%c", kbd_command_prefix, *suffix);
272                         cmd = getenv (cmd_name);
273
274                         return (cmd);
275                 }
276         }
277
278         return (NULL);
279 }
280
281 #endif /* CONFIG_PREBOOT */
282
283 int misc_init_r (void)
284 {
285 #ifdef CONFIG_PREBOOT
286         struct kbd_data_t kbd_data;
287         /* Decode keys */
288         char *str = strdup (key_match (get_keys (&kbd_data)));
289         /* Set or delete definition */
290         setenv ("preboot", str);
291         free (str);
292 #endif /* CONFIG_PREBOOT */
293
294         return 0;
295 }
296
297 int board_early_init_r (void)
298 {
299         *(vu_long *)MPC5XXX_BOOTCS_CFG &= ~0x1; /* clear RO */
300         *(vu_long *)MPC5XXX_BOOTCS_START =
301         *(vu_long *)MPC5XXX_CS0_START = START_REG(CFG_FLASH_BASE);
302         *(vu_long *)MPC5XXX_BOOTCS_STOP =
303         *(vu_long *)MPC5XXX_CS0_STOP = STOP_REG(CFG_FLASH_BASE, CFG_FLASH_SIZE);
304         return 0;
305 }
306 #ifdef  CONFIG_PCI
307 static struct pci_controller hose;
308
309 extern void pci_mpc5xxx_init(struct pci_controller *);
310
311 void pci_init_board(void)
312 {
313         pci_mpc5xxx_init(&hose);
314 }
315 #endif