r360mpi.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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 2 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_or2 = CFG_OR2_PRELIM;
  108. memctl->memc_br2 = CFG_BR2_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 = 0x80004105; /* SDRAM bank 0 */
  113. udelay (200);
  114. memctl->memc_mcr = 0x80004230; /* 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_BASE2_PRELIM,
  124. SDRAM_MAX_SIZE);
  125. udelay (1000);
  126. /*
  127. * try 9 column mode
  128. */
  129. size9 = dram_size (CFG_MAMR_9COL, (ulong *) SDRAM_BASE2_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. #ifdef CONFIG_CAN_DRIVER
  161. /* Initialize OR3 / BR3 */
  162. memctl->memc_or3 = CFG_OR3_CAN; /* switch GPLB_5 to GPLA_5 */
  163. memctl->memc_br3 = CFG_BR3_CAN;
  164. /* Initialize MBMR */
  165. memctl->memc_mbmr = MAMR_GPL_B4DIS; /* GPL_B4 works as UPWAITB */
  166. /* Initialize UPMB for CAN: single read */
  167. memctl->memc_mdr = 0xFFFFC004;
  168. memctl->memc_mcr = 0x0100 | UPMB;
  169. memctl->memc_mdr = 0x0FFFD004;
  170. memctl->memc_mcr = 0x0101 | UPMB;
  171. memctl->memc_mdr = 0x0FFFC000;
  172. memctl->memc_mcr = 0x0102 | UPMB;
  173. memctl->memc_mdr = 0x3FFFC004;
  174. memctl->memc_mcr = 0x0103 | UPMB;
  175. memctl->memc_mdr = 0xFFFFDC05;
  176. memctl->memc_mcr = 0x0104 | UPMB;
  177. /* Initialize UPMB for CAN: single write */
  178. memctl->memc_mdr = 0xFFFCC004;
  179. memctl->memc_mcr = 0x0118 | UPMB;
  180. memctl->memc_mdr = 0xCFFCD004;
  181. memctl->memc_mcr = 0x0119 | UPMB;
  182. memctl->memc_mdr = 0x0FFCC000;
  183. memctl->memc_mcr = 0x011A | UPMB;
  184. memctl->memc_mdr = 0x7FFCC004;
  185. memctl->memc_mcr = 0x011B | UPMB;
  186. memctl->memc_mdr = 0xFFFDCC05;
  187. memctl->memc_mcr = 0x011C | UPMB;
  188. #endif
  189. return (size_b0);
  190. }
  191. /* ------------------------------------------------------------------------- */
  192. /*
  193. * Check memory range for valid RAM. A simple memory test determines
  194. * the actually available RAM size between addresses `base' and
  195. * `base + maxsize'. Some (not all) hardware errors are detected:
  196. * - short between address lines
  197. * - short between data lines
  198. */
  199. static long int dram_size (long int mamr_value,
  200. long int *base, long int maxsize)
  201. {
  202. volatile immap_t *immap = (immap_t *) CFG_IMMR;
  203. volatile memctl8xx_t *memctl = &immap->im_memctl;
  204. volatile long int *addr;
  205. ulong cnt, val;
  206. ulong save[32]; /* to make test non-destructive */
  207. unsigned char i = 0;
  208. memctl->memc_mamr = mamr_value;
  209. for (cnt = maxsize / sizeof (long); cnt > 0; cnt >>= 1) {
  210. addr = base + cnt; /* pointer arith! */
  211. save[i++] = *addr;
  212. *addr = ~cnt;
  213. }
  214. /* write 0 to base address */
  215. addr = base;
  216. save[i] = *addr;
  217. *addr = 0;
  218. /* check at base address */
  219. if ((val = *addr) != 0) {
  220. *addr = save[i];
  221. return (0);
  222. }
  223. for (cnt = 1; cnt <= maxsize / sizeof (long); cnt <<= 1) {
  224. addr = base + cnt; /* pointer arith! */
  225. val = *addr;
  226. *addr = save[--i];
  227. if (val != (~cnt)) {
  228. return (cnt * sizeof (long));
  229. }
  230. }
  231. return (maxsize);
  232. }
  233. /* ------------------------------------------------------------------------- */
  234. void r360_i2c_lcd_write (uchar data0, uchar data1)
  235. {
  236. if (i2c_write (CFG_I2C_LCD_ADDR, data0, 1, &data1, 1)) {
  237. printf("Can't write lcd data 0x%02X 0x%02X.\n", data0, data1);
  238. }
  239. }
  240. /* ------------------------------------------------------------------------- */
  241. /*-----------------------------------------------------------------------
  242. * Keyboard Controller
  243. */
  244. /* Number of bytes returned from Keyboard Controller */
  245. #define KEYBD_KEY_MAX 16 /* maximum key number */
  246. #define KEYBD_DATALEN ((KEYBD_KEY_MAX + 7) / 8) /* normal key scan data */
  247. static uchar *key_match (uchar *);
  248. int misc_init_r (void)
  249. {
  250. uchar kbd_data[KEYBD_DATALEN];
  251. uchar keybd_env[2 * KEYBD_DATALEN + 1];
  252. uchar *str;
  253. int i;
  254. i2c_init (CFG_I2C_SPEED, CFG_I2C_SLAVE);
  255. i2c_read (CFG_I2C_KEY_ADDR, 0, 0, kbd_data, KEYBD_DATALEN);
  256. for (i = 0; i < KEYBD_DATALEN; ++i) {
  257. sprintf (keybd_env + i + i, "%02X", kbd_data[i]);
  258. }
  259. setenv ("keybd", keybd_env);
  260. str = strdup (key_match (keybd_env)); /* decode keys */
  261. #ifdef CONFIG_PREBOOT /* automatically configure "preboot" command on key match */
  262. setenv ("preboot", str); /* set or delete definition */
  263. #endif /* CONFIG_PREBOOT */
  264. if (str != NULL) {
  265. free (str);
  266. }
  267. return (0);
  268. }
  269. /*-----------------------------------------------------------------------
  270. * Check if pressed key(s) match magic sequence,
  271. * and return the command string associated with that key(s).
  272. *
  273. * If no key press was decoded, NULL is returned.
  274. *
  275. * Note: the first character of the argument will be overwritten with
  276. * the "magic charcter code" of the decoded key(s), or '\0'.
  277. *
  278. *
  279. * Note: the string points to static environment data and must be
  280. * saved before you call any function that modifies the environment.
  281. */
  282. #ifdef CONFIG_PREBOOT
  283. static uchar kbd_magic_prefix[] = "key_magic";
  284. static uchar kbd_command_prefix[] = "key_cmd";
  285. static uchar *key_match (uchar * kbd_str)
  286. {
  287. uchar magic[sizeof (kbd_magic_prefix) + 1];
  288. uchar cmd_name[sizeof (kbd_command_prefix) + 1];
  289. uchar *str, *suffix;
  290. uchar *kbd_magic_keys;
  291. char *cmd;
  292. /*
  293. * The following string defines the characters that can pe appended
  294. * to "key_magic" to form the names of environment variables that
  295. * hold "magic" key codes, i. e. such key codes that can cause
  296. * pre-boot actions. If the string is empty (""), then only
  297. * "key_magic" is checked (old behaviour); the string "125" causes
  298. * checks for "key_magic1", "key_magic2" and "key_magic5", etc.
  299. */
  300. if ((kbd_magic_keys = getenv ("magic_keys")) != NULL) {
  301. /* loop over all magic keys;
  302. * use '\0' suffix in case of empty string
  303. */
  304. for (suffix = kbd_magic_keys;
  305. *suffix || suffix == kbd_magic_keys;
  306. ++suffix) {
  307. sprintf (magic, "%s%c", kbd_magic_prefix, *suffix);
  308. #if 0
  309. printf ("### Check magic \"%s\"\n", magic);
  310. #endif
  311. if ((str = getenv (magic)) != 0) {
  312. #if 0
  313. printf ("### Compare \"%s\" \"%s\"\n",
  314. kbd_str, str);
  315. #endif
  316. if (strcmp (kbd_str, str) == 0) {
  317. sprintf (cmd_name, "%s%c",
  318. kbd_command_prefix,
  319. *suffix);
  320. if ((cmd = getenv (cmd_name)) != 0) {
  321. #if 0
  322. printf ("### Set PREBOOT to $(%s): \"%s\"\n",
  323. cmd_name, cmd);
  324. #endif
  325. return (cmd);
  326. }
  327. }
  328. }
  329. }
  330. }
  331. #if 0
  332. printf ("### Delete PREBOOT\n");
  333. #endif
  334. *kbd_str = '\0';
  335. return (NULL);
  336. }
  337. #endif /* CONFIG_PREBOOT */
  338. /* Read Keyboard status */
  339. int do_kbd (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
  340. {
  341. uchar kbd_data[KEYBD_DATALEN];
  342. uchar keybd_env[2 * KEYBD_DATALEN + 1];
  343. int i;
  344. i2c_init (CFG_I2C_SPEED, CFG_I2C_SLAVE);
  345. /* Read keys */
  346. i2c_read (CFG_I2C_KEY_ADDR, 0, 0, kbd_data, KEYBD_DATALEN);
  347. puts ("Keys:");
  348. for (i = 0; i < KEYBD_DATALEN; ++i) {
  349. sprintf (keybd_env + i + i, "%02X", kbd_data[i]);
  350. printf (" %02x", kbd_data[i]);
  351. }
  352. putc ('\n');
  353. setenv ("keybd", keybd_env);
  354. return 0;
  355. }