prom.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Carsten Langgaard, carstenl@mips.com
  4. * Copyright (C) 1999,2000 MIPS Technologies, Inc. All rights reserved.
  5. *
  6. * Putting things on the screen/serial line using YAMONs facilities.
  7. */
  8. #include <linux/init.h>
  9. #include <linux/kernel.h>
  10. #include <linux/serial_reg.h>
  11. #include <linux/spinlock.h>
  12. #include <linux/export.h>
  13. #include <linux/string.h>
  14. #include <linux/io.h>
  15. #include <asm/bootinfo.h>
  16. #include <asm/setup.h>
  17. #include <asm/mach-ar7/ar7.h>
  18. #include <asm/mach-ar7/prom.h>
  19. #define MAX_ENTRY 80
  20. struct env_var {
  21. char *name;
  22. char *value;
  23. };
  24. static struct env_var adam2_env[MAX_ENTRY];
  25. char *prom_getenv(const char *name)
  26. {
  27. int i;
  28. for (i = 0; (i < MAX_ENTRY) && adam2_env[i].name; i++)
  29. if (!strcmp(name, adam2_env[i].name))
  30. return adam2_env[i].value;
  31. return NULL;
  32. }
  33. EXPORT_SYMBOL(prom_getenv);
  34. static void __init ar7_init_cmdline(int argc, char *argv[])
  35. {
  36. int i;
  37. for (i = 1; i < argc; i++) {
  38. strlcat(arcs_cmdline, argv[i], COMMAND_LINE_SIZE);
  39. if (i < (argc - 1))
  40. strlcat(arcs_cmdline, " ", COMMAND_LINE_SIZE);
  41. }
  42. }
  43. struct psbl_rec {
  44. u32 psbl_size;
  45. u32 env_base;
  46. u32 env_size;
  47. u32 ffs_base;
  48. u32 ffs_size;
  49. };
  50. static const char psp_env_version[] __initconst = "TIENV0.8";
  51. struct psp_env_chunk {
  52. u8 num;
  53. u8 ctrl;
  54. u16 csum;
  55. u8 len;
  56. char data[11];
  57. } __packed;
  58. struct psp_var_map_entry {
  59. u8 num;
  60. char *value;
  61. };
  62. static const struct psp_var_map_entry psp_var_map[] = {
  63. { 1, "cpufrequency" },
  64. { 2, "memsize" },
  65. { 3, "flashsize" },
  66. { 4, "modetty0" },
  67. { 5, "modetty1" },
  68. { 8, "maca" },
  69. { 9, "macb" },
  70. { 28, "sysfrequency" },
  71. { 38, "mipsfrequency" },
  72. };
  73. /*
  74. Well-known variable (num is looked up in table above for matching variable name)
  75. Example: cpufrequency=211968000
  76. +----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+---
  77. | 01 |CTRL|CHECKSUM | 01 | _2 | _1 | _1 | _9 | _6 | _8 | _0 | _0 | _0 | \0 | FF
  78. +----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+---
  79. Name=Value pair in a single chunk
  80. Example: NAME=VALUE
  81. +----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+---
  82. | 00 |CTRL|CHECKSUM | 01 | _N | _A | _M | _E | _0 | _V | _A | _L | _U | _E | \0
  83. +----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+---
  84. Name=Value pair in 2 chunks (len is the number of chunks)
  85. Example: bootloaderVersion=1.3.7.15
  86. +----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+---
  87. | 00 |CTRL|CHECKSUM | 02 | _b | _o | _o | _t | _l | _o | _a | _d | _e | _r | _V
  88. +----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+---
  89. | _e | _r | _s | _i | _o | _n | \0 | _1 | _. | _3 | _. | _7 | _. | _1 | _5 | \0
  90. +----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+---
  91. Data is padded with 0xFF
  92. */
  93. #define PSP_ENV_SIZE 4096
  94. static char psp_env_data[PSP_ENV_SIZE] = { 0, };
  95. static char * __init lookup_psp_var_map(u8 num)
  96. {
  97. int i;
  98. for (i = 0; i < ARRAY_SIZE(psp_var_map); i++)
  99. if (psp_var_map[i].num == num)
  100. return psp_var_map[i].value;
  101. return NULL;
  102. }
  103. static void __init add_adam2_var(char *name, char *value)
  104. {
  105. int i;
  106. for (i = 0; i < MAX_ENTRY; i++) {
  107. if (!adam2_env[i].name) {
  108. adam2_env[i].name = name;
  109. adam2_env[i].value = value;
  110. return;
  111. } else if (!strcmp(adam2_env[i].name, name)) {
  112. adam2_env[i].value = value;
  113. return;
  114. }
  115. }
  116. }
  117. static int __init parse_psp_env(void *psp_env_base)
  118. {
  119. int i, n;
  120. char *name, *value;
  121. struct psp_env_chunk *chunks = (struct psp_env_chunk *)psp_env_data;
  122. memcpy_fromio(chunks, psp_env_base, PSP_ENV_SIZE);
  123. i = 1;
  124. n = PSP_ENV_SIZE / sizeof(struct psp_env_chunk);
  125. while (i < n) {
  126. if ((chunks[i].num == 0xff) || ((i + chunks[i].len) > n))
  127. break;
  128. value = chunks[i].data;
  129. if (chunks[i].num) {
  130. name = lookup_psp_var_map(chunks[i].num);
  131. } else {
  132. name = value;
  133. value += strlen(name) + 1;
  134. }
  135. if (name)
  136. add_adam2_var(name, value);
  137. i += chunks[i].len;
  138. }
  139. return 0;
  140. }
  141. static void __init ar7_init_env(struct env_var *env)
  142. {
  143. int i;
  144. struct psbl_rec *psbl = (struct psbl_rec *)(KSEG1ADDR(0x14000300));
  145. void *psp_env = (void *)KSEG1ADDR(psbl->env_base);
  146. if (strcmp(psp_env, psp_env_version) == 0) {
  147. parse_psp_env(psp_env);
  148. } else {
  149. for (i = 0; i < MAX_ENTRY; i++, env++)
  150. if (env->name)
  151. add_adam2_var(env->name, env->value);
  152. }
  153. }
  154. static void __init console_config(void)
  155. {
  156. #ifdef CONFIG_SERIAL_8250_CONSOLE
  157. char console_string[40];
  158. int baud = 0;
  159. char parity = '\0', bits = '\0', flow = '\0';
  160. char *s, *p;
  161. if (strstr(arcs_cmdline, "console="))
  162. return;
  163. s = prom_getenv("modetty0");
  164. if (s) {
  165. baud = simple_strtoul(s, &p, 10);
  166. s = p;
  167. if (*s == ',')
  168. s++;
  169. if (*s)
  170. parity = *s++;
  171. if (*s == ',')
  172. s++;
  173. if (*s)
  174. bits = *s++;
  175. if (*s == ',')
  176. s++;
  177. if (*s == 'h')
  178. flow = 'r';
  179. }
  180. if (baud == 0)
  181. baud = 38400;
  182. if (parity != 'n' && parity != 'o' && parity != 'e')
  183. parity = 'n';
  184. if (bits != '7' && bits != '8')
  185. bits = '8';
  186. if (flow == 'r')
  187. sprintf(console_string, " console=ttyS0,%d%c%c%c", baud,
  188. parity, bits, flow);
  189. else
  190. sprintf(console_string, " console=ttyS0,%d%c%c", baud, parity,
  191. bits);
  192. strlcat(arcs_cmdline, console_string, COMMAND_LINE_SIZE);
  193. #endif
  194. }
  195. void __init prom_init(void)
  196. {
  197. ar7_init_cmdline(fw_arg0, (char **)fw_arg1);
  198. ar7_init_env((struct env_var *)fw_arg2);
  199. console_config();
  200. }
  201. #define PORT(offset) (KSEG1ADDR(AR7_REGS_UART0 + (offset * 4)))
  202. static inline unsigned int serial_in(int offset)
  203. {
  204. return readl((void *)PORT(offset));
  205. }
  206. static inline void serial_out(int offset, int value)
  207. {
  208. writel(value, (void *)PORT(offset));
  209. }
  210. void prom_putchar(char c)
  211. {
  212. while ((serial_in(UART_LSR) & UART_LSR_TEMT) == 0)
  213. ;
  214. serial_out(UART_TX, c);
  215. }