r360mpi.c 10 KB

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