earlycon.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /*
  2. * Copyright (C) 2014 Linaro Ltd.
  3. * Author: Rob Herring <[email protected]>
  4. *
  5. * Based on 8250 earlycon:
  6. * (c) Copyright 2004 Hewlett-Packard Development Company, L.P.
  7. * Bjorn Helgaas <[email protected]>
  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 **p_match;
  154. if (!buf || !buf[0])
  155. return -EINVAL;
  156. if (early_con.flags & CON_ENABLED)
  157. return -EALREADY;
  158. for (p_match = __earlycon_table; p_match < __earlycon_table_end;
  159. p_match++) {
  160. const struct earlycon_id *match = *p_match;
  161. size_t len = strlen(match->name);
  162. if (strncmp(buf, match->name, len))
  163. continue;
  164. if (buf[len]) {
  165. if (buf[len] != ',')
  166. continue;
  167. buf += len + 1;
  168. } else
  169. buf = NULL;
  170. return register_earlycon(buf, match);
  171. }
  172. return -ENOENT;
  173. }
  174. /*
  175. * When CONFIG_ACPI_SPCR_TABLE is defined, "earlycon" without parameters in
  176. * command line does not start DT earlycon immediately, instead it defers
  177. * starting it until DT/ACPI decision is made. At that time if ACPI is enabled
  178. * call parse_spcr(), else call early_init_dt_scan_chosen_stdout()
  179. */
  180. bool earlycon_init_is_deferred __initdata;
  181. /* early_param wrapper for setup_earlycon() */
  182. static int __init param_setup_earlycon(char *buf)
  183. {
  184. int err;
  185. /*
  186. * Just 'earlycon' is a valid param for devicetree earlycons;
  187. * don't generate a warning from parse_early_params() in that case
  188. */
  189. if (!buf || !buf[0]) {
  190. if (IS_ENABLED(CONFIG_ACPI_SPCR_TABLE)) {
  191. earlycon_init_is_deferred = true;
  192. return 0;
  193. } else {
  194. return early_init_dt_scan_chosen_stdout();
  195. }
  196. }
  197. err = setup_earlycon(buf);
  198. if (err == -ENOENT || err == -EALREADY)
  199. return 0;
  200. return err;
  201. }
  202. early_param("earlycon", param_setup_earlycon);
  203. #ifdef CONFIG_OF_EARLY_FLATTREE
  204. int __init of_setup_earlycon(const struct earlycon_id *match,
  205. unsigned long node,
  206. const char *options)
  207. {
  208. int err;
  209. struct uart_port *port = &early_console_dev.port;
  210. const __be32 *val;
  211. bool big_endian;
  212. u64 addr;
  213. spin_lock_init(&port->lock);
  214. port->iotype = UPIO_MEM;
  215. addr = of_flat_dt_translate_address(node);
  216. if (addr == OF_BAD_ADDR) {
  217. pr_warn("[%s] bad address\n", match->name);
  218. return -ENXIO;
  219. }
  220. port->mapbase = addr;
  221. port->uartclk = BASE_BAUD * 16;
  222. val = of_get_flat_dt_prop(node, "reg-offset", NULL);
  223. if (val)
  224. port->mapbase += be32_to_cpu(*val);
  225. port->membase = earlycon_map(port->mapbase, SZ_4K);
  226. val = of_get_flat_dt_prop(node, "reg-shift", NULL);
  227. if (val)
  228. port->regshift = be32_to_cpu(*val);
  229. big_endian = of_get_flat_dt_prop(node, "big-endian", NULL) != NULL ||
  230. (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN) &&
  231. of_get_flat_dt_prop(node, "native-endian", NULL) != NULL);
  232. val = of_get_flat_dt_prop(node, "reg-io-width", NULL);
  233. if (val) {
  234. switch (be32_to_cpu(*val)) {
  235. case 1:
  236. port->iotype = UPIO_MEM;
  237. break;
  238. case 2:
  239. port->iotype = UPIO_MEM16;
  240. break;
  241. case 4:
  242. port->iotype = (big_endian) ? UPIO_MEM32BE : UPIO_MEM32;
  243. break;
  244. default:
  245. pr_warn("[%s] unsupported reg-io-width\n", match->name);
  246. return -EINVAL;
  247. }
  248. }
  249. if (options) {
  250. strlcpy(early_console_dev.options, options,
  251. sizeof(early_console_dev.options));
  252. }
  253. earlycon_init(&early_console_dev, match->name);
  254. err = match->setup(&early_console_dev, options);
  255. if (err < 0)
  256. return err;
  257. if (!early_console_dev.con->write)
  258. return -ENODEV;
  259. register_console(early_console_dev.con);
  260. return 0;
  261. }
  262. #endif /* CONFIG_OF_EARLY_FLATTREE */