earlycon.c 7.5 KB

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