r360mpi.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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. #include <common.h>
  24. #include <config.h>
  25. #include <mpc8xx.h>
  26. #include <i2c.h>
  27. #include <commproc.h>
  28. #include <command.h>
  29. #include <cmd_bsp.h>
  30. #include <malloc.h>
  31. #include <linux/types.h>
  32. #include <linux/string.h> /* for strdup */
  33. /*
  34. * Memory Controller Using
  35. *
  36. * CS0 - Flash memory (0x40000000)
  37. * CS1 - SDRAM (0x00000000}
  38. * CS2 -
  39. * CS3 -
  40. * CS4 -
  41. * CS5 -
  42. * CS6 - PCMCIA device
  43. * CS7 - PCMCIA device
  44. */
  45. /* ------------------------------------------------------------------------- */
  46. #define _not_used_ 0xffffffff
  47. const uint sdram_table[]=
  48. {
  49. /* single read. (offset 0 in upm RAM) */
  50. 0x1f07fc04, 0xeeaefc04, 0x11adfc04, 0xefbbbc00,
  51. 0x1ff77c47,
  52. /* MRS initialization (offset 5) */
  53. 0x1ff77c34, 0xefeabc34, 0x1fb57c35,
  54. /* burst read. (offset 8 in upm RAM) */
  55. 0x1f07fc04, 0xeeaefc04, 0x10adfc04, 0xf0affc00,
  56. 0xf0affc00, 0xf1affc00, 0xefbbbc00, 0x1ff77c47,
  57. _not_used_, _not_used_, _not_used_, _not_used_,
  58. _not_used_, _not_used_, _not_used_, _not_used_,
  59. /* single write. (offset 18 in upm RAM) */
  60. 0x1f27fc04, 0xeeaebc00, 0x01b93c04, 0x1ff77c47,
  61. _not_used_, _not_used_, _not_used_, _not_used_,
  62. /* burst write. (offset 20 in upm RAM) */
  63. 0x1f07fc04, 0xeeaebc00, 0x10ad7c00, 0xf0affc00,
  64. 0xf0affc00, 0xe1bbbc04, 0x1ff77c47, _not_used_,
  65. _not_used_, _not_used_, _not_used_, _not_used_,
  66. _not_used_, _not_used_, _not_used_, _not_used_,
  67. /* refresh. (offset 30 in upm RAM) */
  68. 0x1ff5fc84, 0xfffffc04, 0xfffffc04, 0xfffffc04,
  69. 0xfffffc84, 0xfffffc07, _not_used_, _not_used_,
  70. _not_used_, _not_used_, _not_used_, _not_used_,
  71. /* exception. (offset 3c in upm RAM) */
  72. 0x7ffffc07, _not_used_, _not_used_, _not_used_ };
  73. /* ------------------------------------------------------------------------- */
  74. /*
  75. * Check Board Identity:
  76. */
  77. int checkboard (void)
  78. {
  79. puts ("Board: R360 MPI Board\n");
  80. return 0;
  81. }
  82. /* ------------------------------------------------------------------------- */
  83. static long int dram_size (long int, long int *, long int);
  84. /* ------------------------------------------------------------------------- */
  85. long int initdram (int board_type)
  86. {
  87. volatile immap_t *immap = (immap_t *) CFG_IMMR;
  88. volatile memctl8xx_t *memctl = &immap->im_memctl;
  89. long int size8, size9;
  90. long int size_b0 = 0;
  91. unsigned long reg;
  92. upmconfig (UPMA, (uint *) sdram_table,
  93. sizeof (sdram_table) / sizeof (uint));
  94. /*
  95. * Preliminary prescaler for refresh (depends on number of
  96. * banks): This value is selected for four cycles every 62.4 us
  97. * with two SDRAM banks or four cycles every 31.2 us with one
  98. * bank. It will be adjusted after memory sizing.
  99. */
  100. memctl->memc_mptpr = CFG_MPTPR_2BK_8K;
  101. memctl->memc_mar = 0x00000088;
  102. /*
  103. * Map controller bank 1 to the SDRAM bank at
  104. * preliminary address - these have to be modified after the
  105. * SDRAM size has been determined.
  106. */
  107. memctl->memc_or1 = CFG_OR1_PRELIM;
  108. memctl->memc_br1 = CFG_BR1_PRELIM;
  109. memctl->memc_mamr = CFG_MAMR_8COL & (~(MAMR_PTAE)); /* no refresh yet */
  110. udelay (200);
  111. /* perform SDRAM initializsation sequence */
  112. memctl->memc_mcr = 0x80002105; /* SDRAM bank 0 */
  113. udelay (200);
  114. memctl->memc_mcr = 0x80002230; /* SDRAM bank 0 - execute twice */
  115. udelay (200);
  116. memctl->memc_mamr |= MAMR_PTAE; /* enable refresh */
  117. udelay (1000);
  118. /*
  119. * Check Bank 0 Memory Size for re-configuration
  120. *
  121. * try 8 column mode
  122. */
  123. size8 = dram_size (CFG_MAMR_8COL, (ulong *) SDRAM_BASE1_PRELIM,
  124. SDRAM_MAX_SIZE);
  125. udelay (1000);
  126. /*
  127. * try 9 column mode
  128. */
  129. size9 = dram_size (CFG_MAMR_9COL, (ulong *) SDRAM_BASE1_PRELIM,
  130. SDRAM_MAX_SIZE);
  131. if (size8 < size9) { /* leave configuration at 9 columns */
  132. size_b0 = size9;
  133. /* debug ("SDRAM Bank 0 in 9 column mode: %ld MB\n", size >> 20); */
  134. } else { /* back to 8 columns */
  135. size_b0 = size8;
  136. memctl->memc_mamr = CFG_MAMR_8COL;
  137. udelay (500);
  138. /* debug ("SDRAM Bank 0 in 8 column mode: %ld MB\n", size >> 20); */
  139. }
  140. udelay (1000);
  141. /*
  142. * Adjust refresh rate depending on SDRAM type, both banks
  143. * For types > 128 MBit leave it at the current (fast) rate
  144. */
  145. if ((size_b0 < 0x02000000)) {
  146. /* reduce to 15.6 us (62.4 us / quad) */
  147. memctl->memc_mptpr = CFG_MPTPR_2BK_4K;
  148. udelay (1000);
  149. }
  150. /*
  151. * Final mapping
  152. */
  153. memctl->memc_or1 = ((-size_b0) & 0xFFFF0000) | CFG_OR_TIMING_SDRAM;
  154. memctl->memc_br1 = (CFG_SDRAM_BASE & BR_BA_MSK) | BR_MS_UPMA | BR_V;
  155. /* adjust refresh rate depending on SDRAM type, one bank */
  156. reg = memctl->memc_mptpr;
  157. reg >>= 1; /* reduce to CFG_MPTPR_1BK_8K / _4K */
  158. memctl->memc_mptpr = reg;
  159. udelay (10000);
  160. return (size_b0);
  161. }
  162. /* ------------------------------------------------------------------------- */
  163. /*
  164. * Check memory range for valid RAM. A simple memory test determines
  165. * the actually available RAM size between addresses `base' and
  166. * `base + maxsize'. Some (not all) hardware errors are detected:
  167. * - short between address lines
  168. * - short between data lines
  169. */
  170. static long int dram_size (long int mamr_value, long int *base,
  171. long int maxsize)
  172. {
  173. volatile immap_t *immap = (immap_t *) CFG_IMMR;
  174. volatile memctl8xx_t *memctl = &immap->im_memctl;
  175. volatile long int *addr;
  176. ulong cnt, val;
  177. ulong save[32]; /* to make test non-destructive */
  178. unsigned char i = 0;
  179. memctl->memc_mamr = mamr_value;
  180. for (cnt = maxsize / sizeof (long); cnt > 0; cnt >>= 1) {
  181. addr = base + cnt; /* pointer arith! */
  182. save[i++] = *addr;
  183. *addr = ~cnt;
  184. }
  185. /* write 0 to base address */
  186. addr = base;
  187. save[i] = *addr;
  188. *addr = 0;
  189. /* check at base address */
  190. if ((val = *addr) != 0) {
  191. *addr = save[i];
  192. return (0);
  193. }
  194. for (cnt = 1; cnt <= maxsize / sizeof (long); cnt <<= 1) {
  195. addr = base + cnt; /* pointer arith! */
  196. val = *addr;
  197. *addr = save[--i];
  198. if (val != (~cnt)) {
  199. return (cnt * sizeof (long));
  200. }
  201. }
  202. return (maxsize);
  203. }
  204. /* ------------------------------------------------------------------------- */
  205. void r360_pwm_write (uchar reg, uchar val)
  206. {
  207. if (i2c_write (CFG_I2C_PWM_ADDR, reg, 1, &val, 1)) {
  208. printf ("Can't write PWM register 0x%02X.\n", reg);
  209. }
  210. }
  211. /* ------------------------------------------------------------------------- */
  212. /*-----------------------------------------------------------------------
  213. * Keyboard Controller
  214. */
  215. /* Number of bytes returned from Keyboard Controller */
  216. #define KEYBD_KEY_MAX 20 /* maximum key number */
  217. #define KEYBD_DATALEN ((KEYBD_KEY_MAX + 7) / 8) /* normal key scan data */
  218. static uchar kbd_addr = CFG_I2C_KBD_ADDR;
  219. static uchar *key_match (uchar *);
  220. int misc_init_r (void)
  221. {
  222. uchar kbd_data[KEYBD_DATALEN];
  223. uchar keybd_env[2 * KEYBD_DATALEN + 1];
  224. uchar *str;
  225. int i;
  226. i2c_init (CFG_I2C_SPEED, CFG_I2C_SLAVE);
  227. i2c_read (kbd_addr, 0, 0, kbd_data, KEYBD_DATALEN);
  228. for (i = 0; i < KEYBD_DATALEN; ++i) {
  229. sprintf (keybd_env + i + i, "%02X", kbd_data[i]);
  230. }
  231. setenv ("keybd", keybd_env);
  232. str = strdup (key_match (kbd_data)); /* decode keys */
  233. #ifdef CONFIG_PREBOOT /* automatically configure "preboot" command on key match */
  234. setenv ("preboot", str); /* set or delete definition */
  235. #endif /* CONFIG_PREBOOT */
  236. if (str != NULL) {
  237. free (str);
  238. }
  239. return (0);
  240. }
  241. /*-----------------------------------------------------------------------
  242. * Check if pressed key(s) match magic sequence,
  243. * and return the command string associated with that key(s).
  244. *
  245. * If no key press was decoded, NULL is returned.
  246. *
  247. * Note: the first character of the argument will be overwritten with
  248. * the "magic charcter code" of the decoded key(s), or '\0'.
  249. *
  250. *
  251. * Note: the string points to static environment data and must be
  252. * saved before you call any function that modifies the environment.
  253. */
  254. #ifdef CONFIG_PREBOOT
  255. static uchar kbd_magic_prefix[] = "key_magic";
  256. static uchar kbd_command_prefix[] = "key_cmd";
  257. static uchar *key_match (uchar * kbd_data)
  258. {
  259. uchar compare[KEYBD_DATALEN];
  260. uchar magic[sizeof (kbd_magic_prefix) + 1];
  261. uchar cmd_name[sizeof (kbd_command_prefix) + 1];
  262. uchar key_mask;
  263. uchar *str, *nxt, *suffix;
  264. uchar *kbd_magic_keys;
  265. char *cmd;
  266. int i;
  267. /*
  268. * The following string defines the characters that can pe appended
  269. * to "key_magic" to form the names of environment variables that
  270. * hold "magic" key codes, i. e. such key codes that can cause
  271. * pre-boot actions. If the string is empty (""), then only
  272. * "key_magic" is checked (old behaviour); the string "125" causes
  273. * checks for "key_magic1", "key_magic2" and "key_magic5", etc.
  274. */
  275. if ((kbd_magic_keys = getenv ("magic_keys")) == NULL)
  276. kbd_magic_keys = "";
  277. /* loop over all magic keys;
  278. * use '\0' suffix in case of empty string
  279. */
  280. for (suffix=kbd_magic_keys; *suffix || suffix==kbd_magic_keys; ++suffix) {
  281. sprintf (magic, "%s%c", kbd_magic_prefix, *suffix);
  282. #if 0
  283. printf ("### Check magic \"%s\"\n", magic);
  284. #endif
  285. memcpy(compare, kbd_data, KEYBD_DATALEN);
  286. for (str = getenv(magic); str != NULL; str = (*nxt) ? nxt+1 : nxt) {
  287. uchar c;
  288. c = (uchar) simple_strtoul (str, (char **) (&nxt), 16);
  289. if (str == nxt) /* invalid character */
  290. break;
  291. if (c >= KEYBD_KEY_MAX) /* bad key number */
  292. goto next_magic;
  293. key_mask = 0x80 >> (c % 8);
  294. if (!(compare[c / 8] & key_mask)) /* key not pressed */
  295. goto next_magic;
  296. compare[c / 8] &= ~key_mask;
  297. }
  298. for (i=0; i<KEYBD_DATALEN; i++)
  299. if (compare[i]) /* key(s) not released */
  300. goto next_magic;
  301. sprintf (cmd_name, "%s%c", kbd_command_prefix, *suffix);
  302. cmd = getenv (cmd_name);
  303. #if 0
  304. printf ("### Set PREBOOT to $(%s): \"%s\"\n",
  305. cmd_name, cmd ? cmd : "<<NULL>>");
  306. #endif
  307. *kbd_data = *suffix;
  308. return (cmd);
  309. next_magic:;
  310. }
  311. #if 0
  312. printf ("### Delete PREBOOT\n");
  313. #endif
  314. *kbd_data = '\0';
  315. return (NULL);
  316. }
  317. #endif /* CONFIG_PREBOOT */
  318. /* Read Keyboard status */
  319. int do_kbd (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
  320. {
  321. uchar kbd_data[KEYBD_DATALEN];
  322. uchar keybd_env[2 * KEYBD_DATALEN + 1];
  323. int i;
  324. i2c_init (CFG_I2C_SPEED, CFG_I2C_SLAVE);
  325. /* Read keys */
  326. i2c_read (kbd_addr, 0, 0, kbd_data, KEYBD_DATALEN);
  327. puts ("Keys:");
  328. for (i = 0; i < KEYBD_DATALEN; ++i) {
  329. sprintf (keybd_env + i + i, "%02X", kbd_data[i]);
  330. printf (" %02x", kbd_data[i]);
  331. }
  332. putc ('\n');
  333. setenv ("keybd", keybd_env);
  334. return 0;
  335. }