3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * See file CREDITS for list of people who contributed to this
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.
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.
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,
34 #include <linux/types.h>
35 #include <linux/string.h> /* for strdup */
39 * Memory Controller Using
41 * CS0 - Flash memory (0x40000000)
42 * CS1 - SDRAM (0x00000000}
51 /* ------------------------------------------------------------------------- */
53 #define _not_used_ 0xffffffff
55 const uint sdram_table[]=
57 /* single read. (offset 0 in upm RAM) */
58 0x1f07fc04, 0xeeaefc04, 0x11adfc04, 0xefbbbc00,
61 /* MRS initialization (offset 5) */
63 0x1ff77c34, 0xefeabc34, 0x1fb57c35,
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_,
71 /* single write. (offset 18 in upm RAM) */
72 0x1f27fc04, 0xeeaebc00, 0x01b93c04, 0x1ff77c47,
73 _not_used_, _not_used_, _not_used_, _not_used_,
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_,
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_,
86 /* exception. (offset 3c in upm RAM) */
87 0x7ffffc07, _not_used_, _not_used_, _not_used_ };
89 /* ------------------------------------------------------------------------- */
92 * Check Board Identity:
97 puts ("Board: R360 MPI Board\n");
101 /* ------------------------------------------------------------------------- */
103 static long int dram_size (long int, long int *, long int);
105 /* ------------------------------------------------------------------------- */
107 long int initdram (int board_type)
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;
115 upmconfig (UPMA, (uint *) sdram_table,
116 sizeof (sdram_table) / sizeof (uint));
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.
124 memctl->memc_mptpr = CFG_MPTPR_2BK_8K;
126 memctl->memc_mar = 0x00000088;
129 * Map controller bank 1 to the SDRAM bank at
130 * preliminary address - these have to be modified after the
131 * SDRAM size has been determined.
133 memctl->memc_or1 = CFG_OR1_PRELIM;
134 memctl->memc_br1 = CFG_BR1_PRELIM;
136 memctl->memc_mamr = CFG_MAMR_8COL & (~(MAMR_PTAE)); /* no refresh yet */
140 /* perform SDRAM initializsation sequence */
142 memctl->memc_mcr = 0x80002105; /* SDRAM bank 0 */
144 memctl->memc_mcr = 0x80002230; /* SDRAM bank 0 - execute twice */
147 memctl->memc_mamr |= MAMR_PTAE; /* enable refresh */
152 * Check Bank 0 Memory Size for re-configuration
156 size8 = dram_size (CFG_MAMR_8COL, (ulong *) SDRAM_BASE1_PRELIM,
164 size9 = dram_size (CFG_MAMR_9COL, (ulong *) SDRAM_BASE1_PRELIM,
167 if (size8 < size9) { /* leave configuration at 9 columns */
169 /* debug ("SDRAM Bank 0 in 9 column mode: %ld MB\n", size >> 20); */
170 } else { /* back to 8 columns */
172 memctl->memc_mamr = CFG_MAMR_8COL;
174 /* debug ("SDRAM Bank 0 in 8 column mode: %ld MB\n", size >> 20); */
180 * Adjust refresh rate depending on SDRAM type, both banks
181 * For types > 128 MBit leave it at the current (fast) rate
183 if ((size_b0 < 0x02000000)) {
184 /* reduce to 15.6 us (62.4 us / quad) */
185 memctl->memc_mptpr = CFG_MPTPR_2BK_4K;
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;
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;
206 /* ------------------------------------------------------------------------- */
209 * Check memory range for valid RAM. A simple memory test determines
210 * the actually available RAM size between addresses `base' and
211 * `base + maxsize'. Some (not all) hardware errors are detected:
212 * - short between address lines
213 * - short between data lines
216 static long int dram_size (long int mamr_value, long int *base,
219 volatile immap_t *immap = (immap_t *) CFG_IMMR;
220 volatile memctl8xx_t *memctl = &immap->im_memctl;
221 volatile long int *addr;
223 ulong save[32]; /* to make test non-destructive */
226 memctl->memc_mamr = mamr_value;
228 for (cnt = maxsize / sizeof (long); cnt > 0; cnt >>= 1) {
229 addr = base + cnt; /* pointer arith! */
235 /* write 0 to base address */
240 /* check at base address */
241 if ((val = *addr) != 0) {
246 for (cnt = 1; cnt <= maxsize / sizeof (long); cnt <<= 1) {
247 addr = base + cnt; /* pointer arith! */
252 return (cnt * sizeof (long));
258 /* ------------------------------------------------------------------------- */
260 void r360_pwm_write (uchar reg, uchar val)
262 if (i2c_write (CFG_I2C_PWM_ADDR, reg, 1, &val, 1)) {
263 printf ("Can't write PWM register 0x%02X.\n", reg);
267 /* ------------------------------------------------------------------------- */
269 /*-----------------------------------------------------------------------
270 * Keyboard Controller
273 /* Number of bytes returned from Keyboard Controller */
274 #define KEYBD_KEY_MAX 20 /* maximum key number */
275 #define KEYBD_DATALEN ((KEYBD_KEY_MAX + 7) / 8) /* normal key scan data */
277 static uchar kbd_addr = CFG_I2C_KBD_ADDR;
279 static uchar *key_match (uchar *);
281 int misc_init_r (void)
283 uchar kbd_data[KEYBD_DATALEN];
284 uchar keybd_env[2 * KEYBD_DATALEN + 1];
288 i2c_init (CFG_I2C_SPEED, CFG_I2C_SLAVE);
290 i2c_read (kbd_addr, 0, 0, kbd_data, KEYBD_DATALEN);
292 for (i = 0; i < KEYBD_DATALEN; ++i) {
293 sprintf (keybd_env + i + i, "%02X", kbd_data[i]);
295 setenv ("keybd", keybd_env);
297 str = strdup (key_match (kbd_data)); /* decode keys */
299 #ifdef CONFIG_PREBOOT /* automatically configure "preboot" command on key match */
300 setenv ("preboot", str); /* set or delete definition */
301 #endif /* CONFIG_PREBOOT */
309 /*-----------------------------------------------------------------------
310 * Check if pressed key(s) match magic sequence,
311 * and return the command string associated with that key(s).
313 * If no key press was decoded, NULL is returned.
315 * Note: the first character of the argument will be overwritten with
316 * the "magic charcter code" of the decoded key(s), or '\0'.
319 * Note: the string points to static environment data and must be
320 * saved before you call any function that modifies the environment.
322 #ifdef CONFIG_PREBOOT
324 static uchar kbd_magic_prefix[] = "key_magic";
325 static uchar kbd_command_prefix[] = "key_cmd";
327 static uchar *key_match (uchar * kbd_data)
329 uchar compare[KEYBD_DATALEN];
330 uchar magic[sizeof (kbd_magic_prefix) + 1];
331 uchar cmd_name[sizeof (kbd_command_prefix) + 1];
333 uchar *str, *nxt, *suffix;
334 uchar *kbd_magic_keys;
339 * The following string defines the characters that can pe appended
340 * to "key_magic" to form the names of environment variables that
341 * hold "magic" key codes, i. e. such key codes that can cause
342 * pre-boot actions. If the string is empty (""), then only
343 * "key_magic" is checked (old behaviour); the string "125" causes
344 * checks for "key_magic1", "key_magic2" and "key_magic5", etc.
346 if ((kbd_magic_keys = getenv ("magic_keys")) == NULL)
349 /* loop over all magic keys;
350 * use '\0' suffix in case of empty string
352 for (suffix=kbd_magic_keys; *suffix || suffix==kbd_magic_keys; ++suffix) {
353 sprintf (magic, "%s%c", kbd_magic_prefix, *suffix);
355 printf ("### Check magic \"%s\"\n", magic);
358 memcpy(compare, kbd_data, KEYBD_DATALEN);
360 for (str = getenv(magic); str != NULL; str = (*nxt) ? nxt+1 : nxt) {
363 c = (uchar) simple_strtoul (str, (char **) (&nxt), 16);
365 if (str == nxt) /* invalid character */
368 if (c >= KEYBD_KEY_MAX) /* bad key number */
371 key_mask = 0x80 >> (c % 8);
373 if (!(compare[c / 8] & key_mask)) /* key not pressed */
376 compare[c / 8] &= ~key_mask;
379 for (i=0; i<KEYBD_DATALEN; i++)
380 if (compare[i]) /* key(s) not released */
383 sprintf (cmd_name, "%s%c", kbd_command_prefix, *suffix);
385 cmd = getenv (cmd_name);
387 printf ("### Set PREBOOT to $(%s): \"%s\"\n",
388 cmd_name, cmd ? cmd : "<<NULL>>");
396 printf ("### Delete PREBOOT\n");
401 #endif /* CONFIG_PREBOOT */
403 /* Read Keyboard status */
404 int do_kbd (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
406 uchar kbd_data[KEYBD_DATALEN];
407 uchar keybd_env[2 * KEYBD_DATALEN + 1];
410 i2c_init (CFG_I2C_SPEED, CFG_I2C_SLAVE);
413 i2c_read (kbd_addr, 0, 0, kbd_data, KEYBD_DATALEN);
416 for (i = 0; i < KEYBD_DATALEN; ++i) {
417 sprintf (keybd_env + i + i, "%02X", kbd_data[i]);
418 printf (" %02x", kbd_data[i]);
421 setenv ("keybd", keybd_env);