earlycon.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. struct uart_port *port = &device->port;
  51. const char *s;
  52. size_t len;
  53. /* scan backwards from end of string for first non-numeral */
  54. for (s = name + strlen(name);
  55. s > name && s[-1] >= '0' && s[-1] <= '9';
  56. s--)
  57. ;
  58. if (*s)
  59. earlycon->index = simple_strtoul(s, NULL, 10);
  60. len = s - name;
  61. strlcpy(earlycon->name, name, min(len + 1, sizeof(earlycon->name)));
  62. earlycon->data = &early_console_dev;
  63. if (port->iotype == UPIO_MEM || port->iotype == UPIO_MEM16 ||
  64. port->iotype == UPIO_MEM32 || port->iotype == UPIO_MEM32BE)
  65. pr_info("%s%d at MMIO%s %pa (options '%s')\n",
  66. earlycon->name, earlycon->index,
  67. (port->iotype == UPIO_MEM) ? "" :
  68. (port->iotype == UPIO_MEM16) ? "16" :
  69. (port->iotype == UPIO_MEM32) ? "32" : "32be",
  70. &port->mapbase, device->options);
  71. else
  72. pr_info("%s%d at I/O port 0x%lx (options '%s')\n",
  73. earlycon->name, earlycon->index,
  74. port->iobase, device->options);
  75. }
  76. static int __init parse_options(struct earlycon_device *device, char *options)
  77. {
  78. struct uart_port *port = &device->port;
  79. int length;
  80. resource_size_t addr;
  81. if (uart_parse_earlycon(options, &port->iotype, &addr, &options))
  82. return -EINVAL;
  83. switch (port->iotype) {
  84. case UPIO_MEM:
  85. port->mapbase = addr;
  86. break;
  87. case UPIO_MEM16:
  88. port->regshift = 1;
  89. port->mapbase = addr;
  90. break;
  91. case UPIO_MEM32:
  92. case UPIO_MEM32BE:
  93. port->regshift = 2;
  94. port->mapbase = addr;
  95. break;
  96. case UPIO_PORT:
  97. port->iobase = addr;
  98. break;
  99. default:
  100. return -EINVAL;
  101. }
  102. if (options) {
  103. device->baud = simple_strtoul(options, NULL, 0);
  104. length = min(strcspn(options, " ") + 1,
  105. (size_t)(sizeof(device->options)));
  106. strlcpy(device->options, options, length);
  107. }
  108. return 0;
  109. }
  110. static int __init register_earlycon(char *buf, const struct earlycon_id *match)
  111. {
  112. int err;
  113. struct uart_port *port = &early_console_dev.port;
  114. /* On parsing error, pass the options buf to the setup function */
  115. if (buf && !parse_options(&early_console_dev, buf))
  116. buf = NULL;
  117. spin_lock_init(&port->lock);
  118. port->uartclk = BASE_BAUD * 16;
  119. if (port->mapbase)
  120. port->membase = earlycon_map(port->mapbase, 64);
  121. earlycon_init(&early_console_dev, match->name);
  122. err = match->setup(&early_console_dev, buf);
  123. if (err < 0)
  124. return err;
  125. if (!early_console_dev.con->write)
  126. return -ENODEV;
  127. register_console(early_console_dev.con);
  128. return 0;
  129. }
  130. /**
  131. * setup_earlycon - match and register earlycon console
  132. * @buf: earlycon param string
  133. *
  134. * Registers the earlycon console matching the earlycon specified
  135. * in the param string @buf. Acceptable param strings are of the form
  136. * <name>,io|mmio|mmio32|mmio32be,<addr>,<options>
  137. * <name>,0x<addr>,<options>
  138. * <name>,<options>
  139. * <name>
  140. *
  141. * Only for the third form does the earlycon setup() method receive the
  142. * <options> string in the 'options' parameter; all other forms set
  143. * the parameter to NULL.
  144. *
  145. * Returns 0 if an attempt to register the earlycon was made,
  146. * otherwise negative error code
  147. */
  148. int __init setup_earlycon(char *buf)
  149. {
  150. const struct earlycon_id **p_match;
  151. if (!buf || !buf[0])
  152. return -EINVAL;
  153. if (early_con.flags & CON_ENABLED)
  154. return -EALREADY;
  155. for (p_match = __earlycon_table; p_match < __earlycon_table_end;
  156. p_match++) {
  157. const struct earlycon_id *match = *p_match;
  158. size_t len = strlen(match->name);
  159. if (strncmp(buf, match->name, len))
  160. continue;
  161. if (buf[len]) {
  162. if (buf[len] != ',')
  163. continue;
  164. buf += len + 1;
  165. } else
  166. buf = NULL;
  167. return register_earlycon(buf, match);
  168. }
  169. return -ENOENT;
  170. }
  171. /*
  172. * This defers the initialization of the early console until after ACPI has
  173. * been initialized.
  174. */
  175. bool earlycon_acpi_spcr_enable __initdata;
  176. /* early_param wrapper for setup_earlycon() */
  177. static int __init param_setup_earlycon(char *buf)
  178. {
  179. int err;
  180. /* Just 'earlycon' is a valid param for devicetree and ACPI SPCR. */
  181. if (!buf || !buf[0]) {
  182. if (IS_ENABLED(CONFIG_ACPI_SPCR_TABLE)) {
  183. earlycon_acpi_spcr_enable = true;
  184. return 0;
  185. } else if (!buf) {
  186. return early_init_dt_scan_chosen_stdout();
  187. }
  188. }
  189. err = setup_earlycon(buf);
  190. if (err == -ENOENT || err == -EALREADY)
  191. return 0;
  192. return err;
  193. }
  194. early_param("earlycon", param_setup_earlycon);
  195. #ifdef CONFIG_OF_EARLY_FLATTREE
  196. int __init of_setup_earlycon(const struct earlycon_id *match,
  197. unsigned long node,
  198. const char *options)
  199. {
  200. int err;
  201. struct uart_port *port = &early_console_dev.port;
  202. const __be32 *val;
  203. bool big_endian;
  204. u64 addr;
  205. spin_lock_init(&port->lock);
  206. port->iotype = UPIO_MEM;
  207. addr = of_flat_dt_translate_address(node);
  208. if (addr == OF_BAD_ADDR) {
  209. pr_warn("[%s] bad address\n", match->name);
  210. return -ENXIO;
  211. }
  212. port->mapbase = addr;
  213. val = of_get_flat_dt_prop(node, "reg-offset", NULL);
  214. if (val)
  215. port->mapbase += be32_to_cpu(*val);
  216. port->membase = earlycon_map(port->mapbase, SZ_4K);
  217. val = of_get_flat_dt_prop(node, "reg-shift", NULL);
  218. if (val)
  219. port->regshift = be32_to_cpu(*val);
  220. big_endian = of_get_flat_dt_prop(node, "big-endian", NULL) != NULL ||
  221. (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN) &&
  222. of_get_flat_dt_prop(node, "native-endian", NULL) != NULL);
  223. val = of_get_flat_dt_prop(node, "reg-io-width", NULL);
  224. if (val) {
  225. switch (be32_to_cpu(*val)) {
  226. case 1:
  227. port->iotype = UPIO_MEM;
  228. break;
  229. case 2:
  230. port->iotype = UPIO_MEM16;
  231. break;
  232. case 4:
  233. port->iotype = (big_endian) ? UPIO_MEM32BE : UPIO_MEM32;
  234. break;
  235. default:
  236. pr_warn("[%s] unsupported reg-io-width\n", match->name);
  237. return -EINVAL;
  238. }
  239. }
  240. val = of_get_flat_dt_prop(node, "current-speed", NULL);
  241. if (val)
  242. early_console_dev.baud = be32_to_cpu(*val);
  243. val = of_get_flat_dt_prop(node, "clock-frequency", NULL);
  244. if (val)
  245. port->uartclk = be32_to_cpu(*val);
  246. if (options) {
  247. early_console_dev.baud = simple_strtoul(options, NULL, 0);
  248. strlcpy(early_console_dev.options, options,
  249. sizeof(early_console_dev.options));
  250. }
  251. earlycon_init(&early_console_dev, match->name);
  252. err = match->setup(&early_console_dev, options);
  253. if (err < 0)
  254. return err;
  255. if (!early_console_dev.con->write)
  256. return -ENODEV;
  257. register_console(early_console_dev.con);
  258. return 0;
  259. }
  260. #endif /* CONFIG_OF_EARLY_FLATTREE */