earlycon.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2014 Linaro Ltd.
  4. * Author: Rob Herring <[email protected]>
  5. *
  6. * Based on 8250 earlycon:
  7. * (c) Copyright 2004 Hewlett-Packard Development Company, L.P.
  8. * Bjorn Helgaas <[email protected]>
  9. */
  10. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  11. #include <linux/console.h>
  12. #include <linux/kernel.h>
  13. #include <linux/init.h>
  14. #include <linux/io.h>
  15. #include <linux/serial_core.h>
  16. #include <linux/sizes.h>
  17. #include <linux/of.h>
  18. #include <linux/of_fdt.h>
  19. #include <linux/acpi.h>
  20. #ifdef CONFIG_FIX_EARLYCON_MEM
  21. #include <asm/fixmap.h>
  22. #endif
  23. #include <asm/serial.h>
  24. static struct console early_con = {
  25. .name = "uart", /* fixed up at earlycon registration */
  26. .flags = CON_PRINTBUFFER | CON_BOOT,
  27. .index = 0,
  28. };
  29. static struct earlycon_device early_console_dev = {
  30. .con = &early_con,
  31. };
  32. static void __iomem * __init earlycon_map(resource_size_t paddr, size_t size)
  33. {
  34. void __iomem *base;
  35. #ifdef CONFIG_FIX_EARLYCON_MEM
  36. set_fixmap_io(FIX_EARLYCON_MEM_BASE, paddr & PAGE_MASK);
  37. base = (void __iomem *)__fix_to_virt(FIX_EARLYCON_MEM_BASE);
  38. base += paddr & ~PAGE_MASK;
  39. #else
  40. base = ioremap(paddr, size);
  41. #endif
  42. if (!base)
  43. pr_err("%s: Couldn't map %pa\n", __func__, &paddr);
  44. return base;
  45. }
  46. static void __init earlycon_init(struct earlycon_device *device,
  47. const char *name)
  48. {
  49. struct console *earlycon = device->con;
  50. const char *s;
  51. size_t len;
  52. /* scan backwards from end of string for first non-numeral */
  53. for (s = name + strlen(name);
  54. s > name && s[-1] >= '0' && s[-1] <= '9';
  55. s--)
  56. ;
  57. if (*s)
  58. earlycon->index = simple_strtoul(s, NULL, 10);
  59. len = s - name;
  60. strscpy(earlycon->name, name, min(len + 1, sizeof(earlycon->name)));
  61. earlycon->data = &early_console_dev;
  62. }
  63. static void __init earlycon_print_info(struct earlycon_device *device)
  64. {
  65. struct console *earlycon = device->con;
  66. struct uart_port *port = &device->port;
  67. if (port->iotype == UPIO_MEM || port->iotype == UPIO_MEM16 ||
  68. port->iotype == UPIO_MEM32 || port->iotype == UPIO_MEM32BE)
  69. pr_info("%s%d at MMIO%s %pa (options '%s')\n",
  70. earlycon->name, earlycon->index,
  71. (port->iotype == UPIO_MEM) ? "" :
  72. (port->iotype == UPIO_MEM16) ? "16" :
  73. (port->iotype == UPIO_MEM32) ? "32" : "32be",
  74. &port->mapbase, device->options);
  75. else
  76. pr_info("%s%d at I/O port 0x%lx (options '%s')\n",
  77. earlycon->name, earlycon->index,
  78. port->iobase, device->options);
  79. }
  80. static int __init parse_options(struct earlycon_device *device, char *options)
  81. {
  82. struct uart_port *port = &device->port;
  83. int length;
  84. resource_size_t addr;
  85. if (uart_parse_earlycon(options, &port->iotype, &addr, &options))
  86. return -EINVAL;
  87. switch (port->iotype) {
  88. case UPIO_MEM:
  89. port->mapbase = addr;
  90. break;
  91. case UPIO_MEM16:
  92. port->regshift = 1;
  93. port->mapbase = addr;
  94. break;
  95. case UPIO_MEM32:
  96. case UPIO_MEM32BE:
  97. port->regshift = 2;
  98. port->mapbase = addr;
  99. break;
  100. case UPIO_PORT:
  101. port->iobase = addr;
  102. break;
  103. default:
  104. return -EINVAL;
  105. }
  106. if (options) {
  107. device->baud = simple_strtoul(options, NULL, 0);
  108. length = min(strcspn(options, " ") + 1,
  109. (size_t)(sizeof(device->options)));
  110. strscpy(device->options, options, length);
  111. }
  112. return 0;
  113. }
  114. static int __init register_earlycon(char *buf, const struct earlycon_id *match)
  115. {
  116. int err;
  117. struct uart_port *port = &early_console_dev.port;
  118. /* On parsing error, pass the options buf to the setup function */
  119. if (buf && !parse_options(&early_console_dev, buf))
  120. buf = NULL;
  121. spin_lock_init(&port->lock);
  122. port->uartclk = BASE_BAUD * 16;
  123. if (port->mapbase)
  124. port->membase = earlycon_map(port->mapbase, 64);
  125. earlycon_init(&early_console_dev, match->name);
  126. err = match->setup(&early_console_dev, buf);
  127. earlycon_print_info(&early_console_dev);
  128. if (err < 0)
  129. return err;
  130. if (!early_console_dev.con->write)
  131. return -ENODEV;
  132. register_console(early_console_dev.con);
  133. return 0;
  134. }
  135. /**
  136. * setup_earlycon - match and register earlycon console
  137. * @buf: earlycon param string
  138. *
  139. * Registers the earlycon console matching the earlycon specified
  140. * in the param string @buf. Acceptable param strings are of the form
  141. * <name>,io|mmio|mmio32|mmio32be,<addr>,<options>
  142. * <name>,0x<addr>,<options>
  143. * <name>,<options>
  144. * <name>
  145. *
  146. * Only for the third form does the earlycon setup() method receive the
  147. * <options> string in the 'options' parameter; all other forms set
  148. * the parameter to NULL.
  149. *
  150. * Returns 0 if an attempt to register the earlycon was made,
  151. * otherwise negative error code
  152. */
  153. int __init setup_earlycon(char *buf)
  154. {
  155. const struct earlycon_id *match;
  156. bool empty_compatible = true;
  157. if (!buf || !buf[0])
  158. return -EINVAL;
  159. if (early_con.flags & CON_ENABLED)
  160. return -EALREADY;
  161. again:
  162. for (match = __earlycon_table; match < __earlycon_table_end; match++) {
  163. size_t len = strlen(match->name);
  164. if (strncmp(buf, match->name, len))
  165. continue;
  166. /* prefer entries with empty compatible */
  167. if (empty_compatible && *match->compatible)
  168. continue;
  169. if (buf[len]) {
  170. if (buf[len] != ',')
  171. continue;
  172. buf += len + 1;
  173. } else
  174. buf = NULL;
  175. return register_earlycon(buf, match);
  176. }
  177. if (empty_compatible) {
  178. empty_compatible = false;
  179. goto again;
  180. }
  181. return -ENOENT;
  182. }
  183. /*
  184. * This defers the initialization of the early console until after ACPI has
  185. * been initialized.
  186. */
  187. bool earlycon_acpi_spcr_enable __initdata;
  188. /* early_param wrapper for setup_earlycon() */
  189. static int __init param_setup_earlycon(char *buf)
  190. {
  191. int err;
  192. /* Just 'earlycon' is a valid param for devicetree and ACPI SPCR. */
  193. if (!buf || !buf[0]) {
  194. if (IS_ENABLED(CONFIG_ACPI_SPCR_TABLE)) {
  195. earlycon_acpi_spcr_enable = true;
  196. return 0;
  197. } else if (!buf) {
  198. return early_init_dt_scan_chosen_stdout();
  199. }
  200. }
  201. err = setup_earlycon(buf);
  202. if (err == -ENOENT || err == -EALREADY)
  203. return 0;
  204. return err;
  205. }
  206. early_param("earlycon", param_setup_earlycon);
  207. #ifdef CONFIG_OF_EARLY_FLATTREE
  208. int __init of_setup_earlycon(const struct earlycon_id *match,
  209. unsigned long node,
  210. const char *options)
  211. {
  212. int err;
  213. struct uart_port *port = &early_console_dev.port;
  214. const __be32 *val;
  215. bool big_endian;
  216. u64 addr;
  217. if (early_con.flags & CON_ENABLED)
  218. return -EALREADY;
  219. spin_lock_init(&port->lock);
  220. port->iotype = UPIO_MEM;
  221. addr = of_flat_dt_translate_address(node);
  222. if (addr == OF_BAD_ADDR) {
  223. pr_warn("[%s] bad address\n", match->name);
  224. return -ENXIO;
  225. }
  226. port->mapbase = addr;
  227. val = of_get_flat_dt_prop(node, "reg-offset", NULL);
  228. if (val)
  229. port->mapbase += be32_to_cpu(*val);
  230. port->membase = earlycon_map(port->mapbase, SZ_4K);
  231. val = of_get_flat_dt_prop(node, "reg-shift", NULL);
  232. if (val)
  233. port->regshift = be32_to_cpu(*val);
  234. big_endian = of_get_flat_dt_prop(node, "big-endian", NULL) != NULL ||
  235. (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN) &&
  236. of_get_flat_dt_prop(node, "native-endian", NULL) != NULL);
  237. val = of_get_flat_dt_prop(node, "reg-io-width", NULL);
  238. if (val) {
  239. switch (be32_to_cpu(*val)) {
  240. case 1:
  241. port->iotype = UPIO_MEM;
  242. break;
  243. case 2:
  244. port->iotype = UPIO_MEM16;
  245. break;
  246. case 4:
  247. port->iotype = (big_endian) ? UPIO_MEM32BE : UPIO_MEM32;
  248. break;
  249. default:
  250. pr_warn("[%s] unsupported reg-io-width\n", match->name);
  251. return -EINVAL;
  252. }
  253. }
  254. val = of_get_flat_dt_prop(node, "current-speed", NULL);
  255. if (val)
  256. early_console_dev.baud = be32_to_cpu(*val);
  257. val = of_get_flat_dt_prop(node, "clock-frequency", NULL);
  258. if (val)
  259. port->uartclk = be32_to_cpu(*val);
  260. if (options) {
  261. early_console_dev.baud = simple_strtoul(options, NULL, 0);
  262. strscpy(early_console_dev.options, options,
  263. sizeof(early_console_dev.options));
  264. }
  265. earlycon_init(&early_console_dev, match->name);
  266. err = match->setup(&early_console_dev, options);
  267. earlycon_print_info(&early_console_dev);
  268. if (err < 0)
  269. return err;
  270. if (!early_console_dev.con->write)
  271. return -ENODEV;
  272. register_console(early_console_dev.con);
  273. return 0;
  274. }
  275. #endif /* CONFIG_OF_EARLY_FLATTREE */