r360mpi.c 11 KB

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