earlycon.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2014 Linaro Ltd.
  4. * Author: Rob Herring <robh@kernel.org>
  5. *
  6. * Based on 8250 earlycon:
  7. * (c) Copyright 2004 Hewlett-Packard Development Company, L.P.
  8. * Bjorn Helgaas <bjorn.helgaas@hp.com>
  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. char *uartclk;
  108. device->baud = simple_strtoul(options, NULL, 0);
  109. uartclk = strchr(options, ',');
  110. if (uartclk && kstrtouint(uartclk + 1, 0, &port->uartclk) < 0)
  111. pr_warn("[%s] unsupported earlycon uart clkrate option\n",
  112. options);
  113. length = min(strcspn(options, " ") + 1,
  114. (size_t)(sizeof(device->options)));
  115. strscpy(device->options, options, length);
  116. }
  117. return 0;
  118. }
  119. static int __init register_earlycon(char *buf, const struct earlycon_id *match)
  120. {
  121. int err;
  122. struct uart_port *port = &early_console_dev.port;
  123. /* On parsing error, pass the options buf to the setup function */
  124. if (buf && !parse_options(&early_console_dev, buf))
  125. buf = NULL;
  126. spin_lock_init(&port->lock);
  127. if (!port->uartclk)
  128. port->uartclk = BASE_BAUD * 16;
  129. if (port->mapbase)
  130. port->membase = earlycon_map(port->mapbase, 64);
  131. earlycon_init(&early_console_dev, match->name);
  132. err = match->setup(&early_console_dev, buf);
  133. earlycon_print_info(&early_console_dev);
  134. if (err < 0)
  135. return err;
  136. if (!early_console_dev.con->write)
  137. return -ENODEV;
  138. register_console(early_console_dev.con);
  139. return 0;
  140. }
  141. /**
  142. * setup_earlycon - match and register earlycon console
  143. * @buf: earlycon param string
  144. *
  145. * Registers the earlycon console matching the earlycon specified
  146. * in the param string @buf. Acceptable param strings are of the form
  147. * <name>,io|mmio|mmio32|mmio32be,<addr>,<options>
  148. * <name>,0x<addr>,<options>
  149. * <name>,<options>
  150. * <name>
  151. *
  152. * Only for the third form does the earlycon setup() method receive the
  153. * <options> string in the 'options' parameter; all other forms set
  154. * the parameter to NULL.
  155. *
  156. * Returns 0 if an attempt to register the earlycon was made,
  157. * otherwise negative error code
  158. */
  159. int __init setup_earlycon(char *buf)
  160. {
  161. const struct earlycon_id *match;
  162. bool empty_compatible = true;
  163. if (!buf || !buf[0])
  164. return -EINVAL;
  165. if (console_is_registered(&early_con))
  166. return -EALREADY;
  167. again:
  168. for (match = __earlycon_table; match < __earlycon_table_end; match++) {
  169. size_t len = strlen(match->name);
  170. if (strncmp(buf, match->name, len))
  171. continue;
  172. /* prefer entries with empty compatible */
  173. if (empty_compatible && *match->compatible)
  174. continue;
  175. if (buf[len]) {
  176. if (buf[len] != ',')
  177. continue;
  178. buf += len + 1;
  179. } else
  180. buf = NULL;
  181. return register_earlycon(buf, match);
  182. }
  183. if (empty_compatible) {
  184. empty_compatible = false;
  185. goto again;
  186. }
  187. return -ENOENT;
  188. }
  189. /*
  190. * This defers the initialization of the early console until after ACPI has
  191. * been initialized.
  192. */
  193. bool earlycon_acpi_spcr_enable __initdata;
  194. /* early_param wrapper for setup_earlycon() */
  195. static int __init param_setup_earlycon(char *buf)
  196. {
  197. int err;
  198. /* Just 'earlycon' is a valid param for devicetree and ACPI SPCR. */
  199. if (!buf || !buf[0]) {
  200. if (IS_ENABLED(CONFIG_ACPI_SPCR_TABLE)) {
  201. earlycon_acpi_spcr_enable = true;
  202. return 0;
  203. } else if (!buf) {
  204. return early_init_dt_scan_chosen_stdout();
  205. }
  206. }
  207. err = setup_earlycon(buf);
  208. if (err == -ENOENT || err == -EALREADY)
  209. return 0;
  210. return err;
  211. }
  212. early_param("earlycon", param_setup_earlycon);
  213. /*
  214. * The `console` parameter is overloaded. It's handled here as an early param
  215. * and in `printk.c` as a late param. It's possible to specify an early
  216. * `bootconsole` using `earlycon=uartXXXX` (handled above), or via
  217. * the `console=uartXXX` alias. See the comment in `8250_early.c`.
  218. */
  219. static int __init param_setup_earlycon_console_alias(char *buf)
  220. {
  221. /*
  222. * A plain `console` parameter must not enable the SPCR `bootconsole`
  223. * like a plain `earlycon` does.
  224. *
  225. * A `console=` parameter that specifies an empty value is used to
  226. * disable the `console`, not the `earlycon` `bootconsole`. The
  227. * disabling of the `console` is handled by `printk.c`.
  228. */
  229. if (!buf || !buf[0])
  230. return 0;
  231. return param_setup_earlycon(buf);
  232. }
  233. early_param("console", param_setup_earlycon_console_alias);
  234. #ifdef CONFIG_OF_EARLY_FLATTREE
  235. int __init of_setup_earlycon(const struct earlycon_id *match,
  236. unsigned long node,
  237. const char *options)
  238. {
  239. int err;
  240. struct uart_port *port = &early_console_dev.port;
  241. const __be32 *val;
  242. bool big_endian;
  243. u64 addr;
  244. if (console_is_registered(&early_con))
  245. return -EALREADY;
  246. spin_lock_init(&port->lock);
  247. port->iotype = UPIO_MEM;
  248. addr = of_flat_dt_translate_address(node);
  249. if (addr == OF_BAD_ADDR) {
  250. pr_warn("[%s] bad address\n", match->name);
  251. return -ENXIO;
  252. }
  253. port->mapbase = addr;
  254. val = of_get_flat_dt_prop(node, "reg-offset", NULL);
  255. if (val)
  256. port->mapbase += be32_to_cpu(*val);
  257. port->membase = earlycon_map(port->mapbase, SZ_4K);
  258. val = of_get_flat_dt_prop(node, "reg-shift", NULL);
  259. if (val)
  260. port->regshift = be32_to_cpu(*val);
  261. big_endian = of_get_flat_dt_prop(node, "big-endian", NULL) != NULL ||
  262. (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN) &&
  263. of_get_flat_dt_prop(node, "native-endian", NULL) != NULL);
  264. val = of_get_flat_dt_prop(node, "reg-io-width", NULL);
  265. if (val) {
  266. switch (be32_to_cpu(*val)) {
  267. case 1:
  268. port->iotype = UPIO_MEM;
  269. break;
  270. case 2:
  271. port->iotype = UPIO_MEM16;
  272. break;
  273. case 4:
  274. port->iotype = (big_endian) ? UPIO_MEM32BE : UPIO_MEM32;
  275. break;
  276. default:
  277. pr_warn("[%s] unsupported reg-io-width\n", match->name);
  278. return -EINVAL;
  279. }
  280. }
  281. val = of_get_flat_dt_prop(node, "current-speed", NULL);
  282. if (val)
  283. early_console_dev.baud = be32_to_cpu(*val);
  284. val = of_get_flat_dt_prop(node, "clock-frequency", NULL);
  285. if (val)
  286. port->uartclk = be32_to_cpu(*val);
  287. if (options) {
  288. early_console_dev.baud = simple_strtoul(options, NULL, 0);
  289. strscpy(early_console_dev.options, options,
  290. sizeof(early_console_dev.options));
  291. }
  292. earlycon_init(&early_console_dev, match->name);
  293. err = match->setup(&early_console_dev, options);
  294. earlycon_print_info(&early_console_dev);
  295. if (err < 0)
  296. return err;
  297. if (!early_console_dev.con->write)
  298. return -ENODEV;
  299. register_console(early_console_dev.con);
  300. return 0;
  301. }
  302. #endif /* CONFIG_OF_EARLY_FLATTREE */