prom.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /*
  2. * Carsten Langgaard, carstenl@mips.com
  3. * Copyright (C) 1999,2000 MIPS Technologies, Inc. All rights reserved.
  4. *
  5. * This program is free software; you can distribute it and/or modify it
  6. * under the terms of the GNU General Public License (Version 2) as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. * for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along
  15. * with this program; if not, write to the Free Software Foundation, Inc.,
  16. * 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
  17. *
  18. * Putting things on the screen/serial line using YAMONs facilities.
  19. */
  20. #include <linux/init.h>
  21. #include <linux/kernel.h>
  22. #include <linux/serial_reg.h>
  23. #include <linux/spinlock.h>
  24. #include <linux/module.h>
  25. #include <linux/string.h>
  26. #include <linux/io.h>
  27. #include <asm/bootinfo.h>
  28. #include <asm/mach-ar7/ar7.h>
  29. #include <asm/mach-ar7/prom.h>
  30. #define MAX_ENTRY 80
  31. struct env_var {
  32. char *name;
  33. char *value;
  34. };
  35. static struct env_var adam2_env[MAX_ENTRY];
  36. char *prom_getenv(const char *name)
  37. {
  38. int i;
  39. for (i = 0; (i < MAX_ENTRY) && adam2_env[i].name; i++)
  40. if (!strcmp(name, adam2_env[i].name))
  41. return adam2_env[i].value;
  42. return NULL;
  43. }
  44. EXPORT_SYMBOL(prom_getenv);
  45. char * __init prom_getcmdline(void)
  46. {
  47. return &(arcs_cmdline[0]);
  48. }
  49. static void __init ar7_init_cmdline(int argc, char *argv[])
  50. {
  51. char *cp;
  52. int actr;
  53. actr = 1; /* Always ignore argv[0] */
  54. cp = &(arcs_cmdline[0]);
  55. while (actr < argc) {
  56. strcpy(cp, argv[actr]);
  57. cp += strlen(argv[actr]);
  58. *cp++ = ' ';
  59. actr++;
  60. }
  61. if (cp != &(arcs_cmdline[0])) {
  62. /* get rid of trailing space */
  63. --cp;
  64. *cp = '\0';
  65. }
  66. }
  67. struct psbl_rec {
  68. u32 psbl_size;
  69. u32 env_base;
  70. u32 env_size;
  71. u32 ffs_base;
  72. u32 ffs_size;
  73. };
  74. static __initdata char psp_env_version[] = "TIENV0.8";
  75. struct psp_env_chunk {
  76. u8 num;
  77. u8 ctrl;
  78. u16 csum;
  79. u8 len;
  80. char data[11];
  81. } __attribute__ ((packed));
  82. struct psp_var_map_entry {
  83. u8 num;
  84. char *value;
  85. };
  86. static struct psp_var_map_entry psp_var_map[] = {
  87. { 1, "cpufrequency" },
  88. { 2, "memsize" },
  89. { 3, "flashsize" },
  90. { 4, "modetty0" },
  91. { 5, "modetty1" },
  92. { 8, "maca" },
  93. { 9, "macb" },
  94. { 28, "sysfrequency" },
  95. { 38, "mipsfrequency" },
  96. };
  97. /*
  98. Well-known variable (num is looked up in table above for matching variable name)
  99. Example: cpufrequency=211968000
  100. +----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+---
  101. | 01 |CTRL|CHECKSUM | 01 | _2 | _1 | _1 | _9 | _6 | _8 | _0 | _0 | _0 | \0 | FF
  102. +----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+---
  103. Name=Value pair in a single chunk
  104. Example: NAME=VALUE
  105. +----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+---
  106. | 00 |CTRL|CHECKSUM | 01 | _N | _A | _M | _E | _0 | _V | _A | _L | _U | _E | \0
  107. +----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+---
  108. Name=Value pair in 2 chunks (len is the number of chunks)
  109. Example: bootloaderVersion=1.3.7.15
  110. +----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+---
  111. | 00 |CTRL|CHECKSUM | 02 | _b | _o | _o | _t | _l | _o | _a | _d | _e | _r | _V
  112. +----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+---
  113. | _e | _r | _s | _i | _o | _n | \0 | _1 | _. | _3 | _. | _7 | _. | _1 | _5 | \0
  114. +----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+---
  115. Data is padded with 0xFF
  116. */
  117. #define PSP_ENV_SIZE 4096
  118. static char psp_env_data[PSP_ENV_SIZE] = { 0, };
  119. static char * __init lookup_psp_var_map(u8 num)
  120. {
  121. int i;
  122. for (i = 0; i < ARRAY_SIZE(psp_var_map); i++)
  123. if (psp_var_map[i].num == num)
  124. return psp_var_map[i].value;
  125. return NULL;
  126. }
  127. static void __init add_adam2_var(char *name, char *value)
  128. {
  129. int i;
  130. for (i = 0; i < MAX_ENTRY; i++) {
  131. if (!adam2_env[i].name) {
  132. adam2_env[i].name = name;
  133. adam2_env[i].value = value;
  134. return;
  135. } else if (!strcmp(adam2_env[i].name, name)) {
  136. adam2_env[i].value = value;
  137. return;
  138. }
  139. }
  140. }
  141. static int __init parse_psp_env(void *psp_env_base)
  142. {
  143. int i, n;
  144. char *name, *value;
  145. struct psp_env_chunk *chunks = (struct psp_env_chunk *)psp_env_data;
  146. memcpy_fromio(chunks, psp_env_base, PSP_ENV_SIZE);
  147. i = 1;
  148. n = PSP_ENV_SIZE / sizeof(struct psp_env_chunk);
  149. while (i < n) {
  150. if ((chunks[i].num == 0xff) || ((i + chunks[i].len) > n))
  151. break;
  152. value = chunks[i].data;
  153. if (chunks[i].num) {
  154. name = lookup_psp_var_map(chunks[i].num);
  155. } else {
  156. name = value;
  157. value += strlen(name) + 1;
  158. }
  159. if (name)
  160. add_adam2_var(name, value);
  161. i += chunks[i].len;
  162. }
  163. return 0;
  164. }
  165. static void __init ar7_init_env(struct env_var *env)
  166. {
  167. int i;
  168. struct psbl_rec *psbl = (struct psbl_rec *)(KSEG1ADDR(0x14000300));
  169. void *psp_env = (void *)KSEG1ADDR(psbl->env_base);
  170. if (strcmp(psp_env, psp_env_version) == 0) {
  171. parse_psp_env(psp_env);
  172. } else {
  173. for (i = 0; i < MAX_ENTRY; i++, env++)
  174. if (env->name)
  175. add_adam2_var(env->name, env->value);
  176. }
  177. }
  178. static void __init console_config(void)
  179. {
  180. #ifdef CONFIG_SERIAL_8250_CONSOLE
  181. char console_string[40];
  182. int baud = 0;
  183. char parity = '\0', bits = '\0', flow = '\0';
  184. char *s, *p;
  185. if (strstr(prom_getcmdline(), "console="))
  186. return;
  187. #ifdef CONFIG_KGDB
  188. if (!strstr(prom_getcmdline(), "nokgdb")) {
  189. strcat(prom_getcmdline(), " console=kgdb");
  190. kgdb_enabled = 1;
  191. return;
  192. }
  193. #endif
  194. s = prom_getenv("modetty0");
  195. if (s) {
  196. baud = simple_strtoul(s, &p, 10);
  197. s = p;
  198. if (*s == ',')
  199. s++;
  200. if (*s)
  201. parity = *s++;
  202. if (*s == ',')
  203. s++;
  204. if (*s)
  205. bits = *s++;
  206. if (*s == ',')
  207. s++;
  208. if (*s == 'h')
  209. flow = 'r';
  210. }
  211. if (baud == 0)
  212. baud = 38400;
  213. if (parity != 'n' && parity != 'o' && parity != 'e')
  214. parity = 'n';
  215. if (bits != '7' && bits != '8')
  216. bits = '8';
  217. if (flow == 'r')
  218. sprintf(console_string, " console=ttyS0,%d%c%c%c", baud,
  219. parity, bits, flow);
  220. else
  221. sprintf(console_string, " console=ttyS0,%d%c%c", baud, parity,
  222. bits);
  223. strcat(prom_getcmdline(), console_string);
  224. #endif
  225. }
  226. void __init prom_init(void)
  227. {
  228. ar7_init_cmdline(fw_arg0, (char **)fw_arg1);
  229. ar7_init_env((struct env_var *)fw_arg2);
  230. console_config();
  231. }
  232. #define PORT(offset) (KSEG1ADDR(AR7_REGS_UART0 + (offset * 4)))
  233. static inline unsigned int serial_in(int offset)
  234. {
  235. return readl((void *)PORT(offset));
  236. }
  237. static inline void serial_out(int offset, int value)
  238. {
  239. writel(value, (void *)PORT(offset));
  240. }
  241. char prom_getchar(void)
  242. {
  243. while (!(serial_in(UART_LSR) & UART_LSR_DR))
  244. ;
  245. return serial_in(UART_RX);
  246. }
  247. int prom_putchar(char c)
  248. {
  249. while ((serial_in(UART_LSR) & UART_LSR_TEMT) == 0)
  250. ;
  251. serial_out(UART_TX, c);
  252. return 1;
  253. }