]> git.karo-electronics.de Git - karo-tx-uboot.git/blob - board/r360mpi/r360mpi.c
91e3234e29d21f3d4a1b747b9b41c5aaec9fe31c
[karo-tx-uboot.git] / board / r360mpi / r360mpi.c
1 /*
2  * (C) Copyright 2001
3  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4  *
5  * See file CREDITS for list of people who contributed to this
6  * project.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as
10  * published by the Free Software Foundation; either version 2 of
11  * the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21  * MA 02111-1307 USA
22  */
23
24 #include <common.h>
25 #include <config.h>
26 #include <mpc8xx.h>
27 #include <i2c.h>
28
29 #include <commproc.h>
30 #include <command.h>
31 #include <cmd_bsp.h>
32 #include <malloc.h>
33
34 #include <linux/types.h>
35 #include <linux/string.h>       /* for strdup */
36
37
38 /*
39  *  Memory Controller Using
40  *
41  *  CS0 - Flash memory            (0x40000000)
42  *  CS1 - SDRAM                   (0x00000000}
43  *  CS2 -
44  *  CS3 -
45  *  CS4 -
46  *  CS5 -
47  *  CS6 - PCMCIA device
48  *  CS7 - PCMCIA device
49  */
50
51 /* ------------------------------------------------------------------------- */
52
53 #define _not_used_      0xffffffff
54
55 const uint sdram_table[]=
56 {
57         /* single read. (offset 0 in upm RAM) */
58         0x1f07fc04, 0xeeaefc04, 0x11adfc04, 0xefbbbc00,
59         0x1ff77c47,
60
61         /* MRS initialization (offset 5) */
62
63         0x1ff77c34, 0xefeabc34, 0x1fb57c35,
64
65         /* burst read. (offset 8 in upm RAM) */
66         0x1f07fc04, 0xeeaefc04, 0x10adfc04, 0xf0affc00,
67         0xf0affc00, 0xf1affc00, 0xefbbbc00, 0x1ff77c47,
68         _not_used_, _not_used_, _not_used_, _not_used_,
69         _not_used_, _not_used_, _not_used_, _not_used_,
70
71         /* single write. (offset 18 in upm RAM) */
72         0x1f27fc04, 0xeeaebc00, 0x01b93c04, 0x1ff77c47,
73         _not_used_, _not_used_, _not_used_, _not_used_,
74
75         /* burst write. (offset 20 in upm RAM) */
76         0x1f07fc04, 0xeeaebc00, 0x10ad7c00, 0xf0affc00,
77         0xf0affc00, 0xe1bbbc04, 0x1ff77c47, _not_used_,
78         _not_used_, _not_used_, _not_used_, _not_used_,
79         _not_used_, _not_used_, _not_used_, _not_used_,
80
81         /* refresh. (offset 30 in upm RAM) */
82         0x1ff5fc84, 0xfffffc04, 0xfffffc04, 0xfffffc04,
83         0xfffffc84, 0xfffffc07, _not_used_, _not_used_,
84         _not_used_, _not_used_, _not_used_, _not_used_,
85
86         /* exception. (offset 3c in upm RAM) */
87         0x7ffffc07, _not_used_, _not_used_, _not_used_ };
88
89 /* ------------------------------------------------------------------------- */
90
91 /*
92  * Check Board Identity:
93  */
94
95 int checkboard (void)
96 {
97         puts ("Board: R360 MPI Board\n");
98         return 0;
99 }
100
101 /* ------------------------------------------------------------------------- */
102
103 static long int dram_size (long int, long int *, long int);
104
105 /* ------------------------------------------------------------------------- */
106
107 long int initdram (int board_type)
108 {
109         volatile immap_t *immap = (immap_t *) CFG_IMMR;
110         volatile memctl8xx_t *memctl = &immap->im_memctl;
111         long int size8, size9;
112         long int size_b0 = 0;
113         unsigned long reg;
114
115         upmconfig (UPMA, (uint *) sdram_table,
116                            sizeof (sdram_table) / sizeof (uint));
117
118         /*
119          * Preliminary prescaler for refresh (depends on number of
120          * banks): This value is selected for four cycles every 62.4 us
121          * with two SDRAM banks or four cycles every 31.2 us with one
122          * bank. It will be adjusted after memory sizing.
123          */
124         memctl->memc_mptpr = CFG_MPTPR_2BK_8K;
125
126         memctl->memc_mar = 0x00000088;
127
128         /*
129          * Map controller bank 2 to the SDRAM bank at
130          * preliminary address - these have to be modified after the
131          * SDRAM size has been determined.
132          */
133         memctl->memc_or2 = CFG_OR2_PRELIM;
134         memctl->memc_br2 = CFG_BR2_PRELIM;
135
136         memctl->memc_mamr = CFG_MAMR_8COL & (~(MAMR_PTAE));     /* no refresh yet */
137
138         udelay (200);
139
140         /* perform SDRAM initializsation sequence */
141
142         memctl->memc_mcr = 0x80004105;  /* SDRAM bank 0 */
143         udelay (200);
144         memctl->memc_mcr = 0x80004230;  /* SDRAM bank 0 - execute twice */
145         udelay (200);
146
147         memctl->memc_mamr |= MAMR_PTAE; /* enable refresh */
148
149         udelay (1000);
150
151         /*
152          * Check Bank 0 Memory Size for re-configuration
153          *
154          * try 8 column mode
155          */
156         size8 = dram_size (CFG_MAMR_8COL, (ulong *) SDRAM_BASE2_PRELIM,
157                                            SDRAM_MAX_SIZE);
158
159         udelay (1000);
160
161         /*
162          * try 9 column mode
163          */
164         size9 = dram_size (CFG_MAMR_9COL, (ulong *) SDRAM_BASE2_PRELIM,
165                                            SDRAM_MAX_SIZE);
166
167         if (size8 < size9) {            /* leave configuration at 9 columns */
168                 size_b0 = size9;
169 /*      debug ("SDRAM Bank 0 in 9 column mode: %ld MB\n", size >> 20);  */
170         } else {                        /* back to 8 columns            */
171                 size_b0 = size8;
172                 memctl->memc_mamr = CFG_MAMR_8COL;
173                 udelay (500);
174 /*      debug ("SDRAM Bank 0 in 8 column mode: %ld MB\n", size >> 20);  */
175         }
176
177         udelay (1000);
178
179         /*
180          * Adjust refresh rate depending on SDRAM type, both banks
181          * For types > 128 MBit leave it at the current (fast) rate
182          */
183         if ((size_b0 < 0x02000000)) {
184                 /* reduce to 15.6 us (62.4 us / quad) */
185                 memctl->memc_mptpr = CFG_MPTPR_2BK_4K;
186                 udelay (1000);
187         }
188
189         /*
190          * Final mapping
191          */
192
193         memctl->memc_or1 = ((-size_b0) & 0xFFFF0000) | CFG_OR_TIMING_SDRAM;
194         memctl->memc_br1 = (CFG_SDRAM_BASE & BR_BA_MSK) | BR_MS_UPMA | BR_V;
195
196         /* adjust refresh rate depending on SDRAM type, one bank */
197         reg = memctl->memc_mptpr;
198         reg >>= 1;              /* reduce to CFG_MPTPR_1BK_8K / _4K */
199         memctl->memc_mptpr = reg;
200
201         udelay (10000);
202
203 #ifdef CONFIG_CAN_DRIVER
204         /* Initialize OR3 / BR3 */
205         memctl->memc_or3 = CFG_OR3_CAN;         /* switch GPLB_5 to GPLA_5 */
206         memctl->memc_br3 = CFG_BR3_CAN;
207
208         /* Initialize MBMR */
209         memctl->memc_mbmr = MAMR_GPL_B4DIS;     /* GPL_B4 works as UPWAITB */
210
211         /* Initialize UPMB for CAN: single read */
212         memctl->memc_mdr = 0xFFFFC004;
213         memctl->memc_mcr = 0x0100 | UPMB;
214
215         memctl->memc_mdr = 0x0FFFD004;
216         memctl->memc_mcr = 0x0101 | UPMB;
217
218         memctl->memc_mdr = 0x0FFFC000;
219         memctl->memc_mcr = 0x0102 | UPMB;
220
221         memctl->memc_mdr = 0x3FFFC004;
222         memctl->memc_mcr = 0x0103 | UPMB;
223
224         memctl->memc_mdr = 0xFFFFDC05;
225         memctl->memc_mcr = 0x0104 | UPMB;
226
227         /* Initialize UPMB for CAN: single write */
228         memctl->memc_mdr = 0xFFFCC004;
229         memctl->memc_mcr = 0x0118 | UPMB;
230
231         memctl->memc_mdr = 0xCFFCD004;
232         memctl->memc_mcr = 0x0119 | UPMB;
233
234         memctl->memc_mdr = 0x0FFCC000;
235         memctl->memc_mcr = 0x011A | UPMB;
236
237         memctl->memc_mdr = 0x7FFCC004;
238         memctl->memc_mcr = 0x011B | UPMB;
239
240         memctl->memc_mdr = 0xFFFDCC05;
241         memctl->memc_mcr = 0x011C | UPMB;
242 #endif
243
244         return (size_b0);
245 }
246
247 /* ------------------------------------------------------------------------- */
248
249 /*
250  * Check memory range for valid RAM. A simple memory test determines
251  * the actually available RAM size between addresses `base' and
252  * `base + maxsize'. Some (not all) hardware errors are detected:
253  * - short between address lines
254  * - short between data lines
255  */
256
257 static long int dram_size (long int mamr_value,
258                            long int *base, long int maxsize)
259 {
260         volatile immap_t *immap = (immap_t *) CFG_IMMR;
261         volatile memctl8xx_t *memctl = &immap->im_memctl;
262         volatile long int *addr;
263         ulong cnt, val;
264         ulong save[32];                 /* to make test non-destructive */
265         unsigned char i = 0;
266
267         memctl->memc_mamr = mamr_value;
268
269         for (cnt = maxsize / sizeof (long); cnt > 0; cnt >>= 1) {
270                 addr = base + cnt;      /* pointer arith! */
271
272                 save[i++] = *addr;
273                 *addr = ~cnt;
274         }
275
276         /* write 0 to base address */
277         addr = base;
278         save[i] = *addr;
279         *addr = 0;
280
281         /* check at base address */
282         if ((val = *addr) != 0) {
283                 *addr = save[i];
284                 return (0);
285         }
286
287         for (cnt = 1; cnt <= maxsize / sizeof (long); cnt <<= 1) {
288                 addr = base + cnt;      /* pointer arith! */
289                 val = *addr;
290                 *addr = save[--i];
291
292                 if (val != (~cnt)) {
293                         return (cnt * sizeof (long));
294                 }
295         }
296         return (maxsize);
297 }
298
299 /* ------------------------------------------------------------------------- */
300
301 void r360_i2c_lcd_write (uchar data0, uchar data1)
302 {
303         if (i2c_write (CFG_I2C_LCD_ADDR, data0, 1, &data1, 1)) {
304                 printf("Can't write lcd data 0x%02X 0x%02X.\n", data0, data1);
305         }
306 }
307
308 /* ------------------------------------------------------------------------- */
309
310 /*-----------------------------------------------------------------------
311  * Keyboard Controller
312  */
313
314 /* Number of bytes returned from Keyboard Controller */
315 #define KEYBD_KEY_MAX   16                              /* maximum key number */
316 #define KEYBD_DATALEN   ((KEYBD_KEY_MAX + 7) / 8)       /* normal key scan data */
317
318 static uchar *key_match (uchar *);
319
320 int misc_init_r (void)
321 {
322         uchar kbd_data[KEYBD_DATALEN];
323         uchar keybd_env[2 * KEYBD_DATALEN + 1];
324         uchar *str;
325         int i;
326
327         i2c_init (CFG_I2C_SPEED, CFG_I2C_SLAVE);
328
329         i2c_read (CFG_I2C_KEY_ADDR, 0, 0, kbd_data, KEYBD_DATALEN);
330
331         for (i = 0; i < KEYBD_DATALEN; ++i) {
332                 sprintf (keybd_env + i + i, "%02X", kbd_data[i]);
333         }
334         setenv ("keybd", keybd_env);
335
336         str = strdup (key_match (keybd_env));   /* decode keys */
337
338 #ifdef CONFIG_PREBOOT   /* automatically configure "preboot" command on key match */
339         setenv ("preboot", str);        /* set or delete definition */
340 #endif /* CONFIG_PREBOOT */
341         if (str != NULL) {
342                 free (str);
343         }
344
345         return (0);
346 }
347
348 /*-----------------------------------------------------------------------
349  * Check if pressed key(s) match magic sequence,
350  * and return the command string associated with that key(s).
351  *
352  * If no key press was decoded, NULL is returned.
353  *
354  * Note: the first character of the argument will be overwritten with
355  * the "magic charcter code" of the decoded key(s), or '\0'.
356  *
357  *
358  * Note: the string points to static environment data and must be
359  * saved before you call any function that modifies the environment.
360  */
361 #ifdef CONFIG_PREBOOT
362
363 static uchar kbd_magic_prefix[] = "key_magic";
364 static uchar kbd_command_prefix[] = "key_cmd";
365
366 static uchar *key_match (uchar * kbd_str)
367 {
368         uchar magic[sizeof (kbd_magic_prefix) + 1];
369         uchar cmd_name[sizeof (kbd_command_prefix) + 1];
370         uchar *str, *suffix;
371         uchar *kbd_magic_keys;
372         char *cmd;
373
374         /*
375          * The following string defines the characters that can pe appended
376          * to "key_magic" to form the names of environment variables that
377          * hold "magic" key codes, i. e. such key codes that can cause
378          * pre-boot actions. If the string is empty (""), then only
379          * "key_magic" is checked (old behaviour); the string "125" causes
380          * checks for "key_magic1", "key_magic2" and "key_magic5", etc.
381          */
382         if ((kbd_magic_keys = getenv ("magic_keys")) != NULL) {
383                 /* loop over all magic keys;
384                  * use '\0' suffix in case of empty string
385                  */
386                 for (suffix = kbd_magic_keys;
387                      *suffix || suffix == kbd_magic_keys;
388                      ++suffix) {
389                         sprintf (magic, "%s%c", kbd_magic_prefix, *suffix);
390
391 #if 0
392                         printf ("### Check magic \"%s\"\n", magic);
393 #endif
394
395                         if ((str = getenv (magic)) != 0) {
396
397 #if 0
398                                 printf ("### Compare \"%s\" \"%s\"\n",
399                                         kbd_str, str);
400 #endif
401                                 if (strcmp (kbd_str, str) == 0) {
402                                         sprintf (cmd_name, "%s%c",
403                                                  kbd_command_prefix,
404                                                  *suffix);
405
406                                         if ((cmd = getenv (cmd_name)) != 0) {
407 #if 0
408                                                 printf ("### Set PREBOOT to $(%s): \"%s\"\n",
409                                                         cmd_name, cmd);
410 #endif
411                                                 return (cmd);
412                                         }
413                                 }
414                         }
415                 }
416         }
417 #if 0
418         printf ("### Delete PREBOOT\n");
419 #endif
420         *kbd_str = '\0';
421         return (NULL);
422 }
423 #endif  /* CONFIG_PREBOOT */
424
425 /* Read Keyboard status */
426 int do_kbd (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
427 {
428         uchar kbd_data[KEYBD_DATALEN];
429         uchar keybd_env[2 * KEYBD_DATALEN + 1];
430         int i;
431
432         i2c_init (CFG_I2C_SPEED, CFG_I2C_SLAVE);
433
434         /* Read keys */
435         i2c_read (CFG_I2C_KEY_ADDR, 0, 0, kbd_data, KEYBD_DATALEN);
436
437         puts ("Keys:");
438         for (i = 0; i < KEYBD_DATALEN; ++i) {
439                 sprintf (keybd_env + i + i, "%02X", kbd_data[i]);
440                 printf (" %02x", kbd_data[i]);
441         }
442         putc ('\n');
443         setenv ("keybd", keybd_env);
444         return 0;
445 }