r360mpi.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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. phys_size_t initdram (int board_type)
  85. {
  86. volatile immap_t *immap = (immap_t *) CONFIG_SYS_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 = CONFIG_SYS_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 = CONFIG_SYS_OR2_PRELIM;
  107. memctl->memc_br2 = CONFIG_SYS_BR2_PRELIM;
  108. memctl->memc_mamr = CONFIG_SYS_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 (CONFIG_SYS_MAMR_8COL, (long *) SDRAM_BASE2_PRELIM,
  123. SDRAM_MAX_SIZE);
  124. udelay (1000);
  125. /*
  126. * try 9 column mode
  127. */
  128. size9 = dram_size (CONFIG_SYS_MAMR_9COL, (long *) 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 = CONFIG_SYS_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 = CONFIG_SYS_MPTPR_2BK_4K;
  147. udelay (1000);
  148. }
  149. /*
  150. * Final mapping
  151. */
  152. memctl->memc_or1 = ((-size_b0) & 0xFFFF0000) | CONFIG_SYS_OR_TIMING_SDRAM;
  153. memctl->memc_br1 = (CONFIG_SYS_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 CONFIG_SYS_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 = CONFIG_SYS_OR3_CAN; /* switch GPLB_5 to GPLA_5 */
  162. memctl->memc_br3 = CONFIG_SYS_BR3_CAN;
  163. /* Initialize MBMR */
  164. memctl->memc_mbmr = MBMR_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 *) CONFIG_SYS_IMMR;
  202. volatile memctl8xx_t *memctl = &immap->im_memctl;
  203. memctl->memc_mamr = mamr_value;
  204. return (get_ram_size(base, maxsize));
  205. }
  206. /* ------------------------------------------------------------------------- */
  207. void r360_i2c_lcd_write (uchar data0, uchar data1)
  208. {
  209. if (i2c_write (CONFIG_SYS_I2C_LCD_ADDR, data0, 1, &data1, 1)) {
  210. printf("Can't write lcd data 0x%02X 0x%02X.\n", data0, data1);
  211. }
  212. }
  213. /* ------------------------------------------------------------------------- */
  214. /*-----------------------------------------------------------------------
  215. * Keyboard Controller
  216. */
  217. /* Number of bytes returned from Keyboard Controller */
  218. #define KEYBD_KEY_MAX 16 /* maximum key number */
  219. #define KEYBD_DATALEN ((KEYBD_KEY_MAX + 7) / 8) /* normal key scan data */
  220. static uchar *key_match (uchar *);
  221. int misc_init_r (void)
  222. {
  223. char kbd_data[KEYBD_DATALEN];
  224. char keybd_env[2 * KEYBD_DATALEN + 1];
  225. char *str;
  226. int i;
  227. i2c_init (CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
  228. i2c_read (CONFIG_SYS_I2C_KEY_ADDR, 0, 0, (uchar *)kbd_data, KEYBD_DATALEN);
  229. for (i = 0; i < KEYBD_DATALEN; ++i) {
  230. sprintf (keybd_env + i + i, "%02X", kbd_data[i]);
  231. }
  232. setenv ("keybd", keybd_env);
  233. str = strdup ((char *)key_match ((uchar *)keybd_env)); /* decode keys */
  234. #ifdef CONFIG_PREBOOT /* automatically configure "preboot" command on key match */
  235. setenv ("preboot", str); /* set or delete definition */
  236. #endif /* CONFIG_PREBOOT */
  237. if (str != NULL) {
  238. free (str);
  239. }
  240. return (0);
  241. }
  242. /*-----------------------------------------------------------------------
  243. * Check if pressed key(s) match magic sequence,
  244. * and return the command string associated with that key(s).
  245. *
  246. * If no key press was decoded, NULL is returned.
  247. *
  248. * Note: the first character of the argument will be overwritten with
  249. * the "magic charcter code" of the decoded key(s), or '\0'.
  250. *
  251. *
  252. * Note: the string points to static environment data and must be
  253. * saved before you call any function that modifies the environment.
  254. */
  255. #ifdef CONFIG_PREBOOT
  256. static uchar kbd_magic_prefix[] = "key_magic";
  257. static uchar kbd_command_prefix[] = "key_cmd";
  258. static uchar *key_match (uchar * kbd_str)
  259. {
  260. uchar magic[sizeof (kbd_magic_prefix) + 1];
  261. uchar cmd_name[sizeof (kbd_command_prefix) + 1];
  262. uchar *str, *suffix;
  263. uchar *kbd_magic_keys;
  264. char *cmd;
  265. /*
  266. * The following string defines the characters that can pe appended
  267. * to "key_magic" to form the names of environment variables that
  268. * hold "magic" key codes, i. e. such key codes that can cause
  269. * pre-boot actions. If the string is empty (""), then only
  270. * "key_magic" is checked (old behaviour); the string "125" causes
  271. * checks for "key_magic1", "key_magic2" and "key_magic5", etc.
  272. */
  273. if ((kbd_magic_keys = (uchar *)getenv ("magic_keys")) != NULL) {
  274. /* loop over all magic keys;
  275. * use '\0' suffix in case of empty string
  276. */
  277. for (suffix = kbd_magic_keys;
  278. *suffix || suffix == kbd_magic_keys;
  279. ++suffix) {
  280. sprintf ((char *)magic, "%s%c", kbd_magic_prefix, *suffix);
  281. #if 0
  282. printf ("### Check magic \"%s\"\n", magic);
  283. #endif
  284. if ((str = (uchar *)getenv ((char *)magic)) != 0) {
  285. #if 0
  286. printf ("### Compare \"%s\" \"%s\"\n",
  287. kbd_str, str);
  288. #endif
  289. if (strcmp ((char *)kbd_str, (char *)str) == 0) {
  290. sprintf ((char *)cmd_name, "%s%c",
  291. kbd_command_prefix,
  292. *suffix);
  293. if ((cmd = getenv ((char *)cmd_name)) != 0) {
  294. #if 0
  295. printf ("### Set PREBOOT to $(%s): \"%s\"\n",
  296. cmd_name, cmd);
  297. #endif
  298. return ((uchar *)cmd);
  299. }
  300. }
  301. }
  302. }
  303. }
  304. #if 0
  305. printf ("### Delete PREBOOT\n");
  306. #endif
  307. *kbd_str = '\0';
  308. return (NULL);
  309. }
  310. #endif /* CONFIG_PREBOOT */
  311. /* Read Keyboard status */
  312. int do_kbd (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
  313. {
  314. uchar kbd_data[KEYBD_DATALEN];
  315. uchar keybd_env[2 * KEYBD_DATALEN + 1];
  316. int i;
  317. i2c_init (CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
  318. /* Read keys */
  319. i2c_read (CONFIG_SYS_I2C_KEY_ADDR, 0, 0, kbd_data, KEYBD_DATALEN);
  320. puts ("Keys:");
  321. for (i = 0; i < KEYBD_DATALEN; ++i) {
  322. sprintf ((char *)(keybd_env + i + i), "%02X", kbd_data[i]);
  323. printf (" %02x", kbd_data[i]);
  324. }
  325. putc ('\n');
  326. setenv ("keybd", (char *)keybd_env);
  327. return 0;
  328. }
  329. U_BOOT_CMD(
  330. kbd, 1, 1, do_kbd,
  331. "read keyboard status",
  332. ""
  333. );