earlycon.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. #ifdef CONFIG_FIX_EARLYCON_MEM
  23. #include <asm/fixmap.h>
  24. #endif
  25. #include <asm/serial.h>
  26. static struct console early_con = {
  27. .name = "uart", /* fixed up at earlycon registration */
  28. .flags = CON_PRINTBUFFER | CON_BOOT,
  29. .index = 0,
  30. };
  31. static struct earlycon_device early_console_dev = {
  32. .con = &early_con,
  33. };
  34. static void __iomem * __init earlycon_map(unsigned long paddr, size_t size)
  35. {
  36. void __iomem *base;
  37. #ifdef CONFIG_FIX_EARLYCON_MEM
  38. set_fixmap_io(FIX_EARLYCON_MEM_BASE, paddr & PAGE_MASK);
  39. base = (void __iomem *)__fix_to_virt(FIX_EARLYCON_MEM_BASE);
  40. base += paddr & ~PAGE_MASK;
  41. #else
  42. base = ioremap(paddr, size);
  43. #endif
  44. if (!base)
  45. pr_err("%s: Couldn't map 0x%llx\n", __func__,
  46. (unsigned long long)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. const char *s;
  54. size_t len;
  55. /* scan backwards from end of string for first non-numeral */
  56. for (s = name + strlen(name);
  57. s > name && s[-1] >= '0' && s[-1] <= '9';
  58. s--)
  59. ;
  60. if (*s)
  61. earlycon->index = simple_strtoul(s, NULL, 10);
  62. len = s - name;
  63. strlcpy(earlycon->name, name, min(len + 1, sizeof(earlycon->name)));
  64. earlycon->data = &early_console_dev;
  65. }
  66. static int __init parse_options(struct earlycon_device *device, char *options)
  67. {
  68. struct uart_port *port = &device->port;
  69. int length;
  70. unsigned long addr;
  71. if (uart_parse_earlycon(options, &port->iotype, &addr, &options))
  72. return -EINVAL;
  73. switch (port->iotype) {
  74. case UPIO_MEM:
  75. port->mapbase = addr;
  76. break;
  77. case UPIO_MEM16:
  78. port->regshift = 1;
  79. port->mapbase = addr;
  80. break;
  81. case UPIO_MEM32:
  82. case UPIO_MEM32BE:
  83. port->regshift = 2;
  84. port->mapbase = addr;
  85. break;
  86. case UPIO_PORT:
  87. port->iobase = addr;
  88. break;
  89. default:
  90. return -EINVAL;
  91. }
  92. if (options) {
  93. device->baud = simple_strtoul(options, NULL, 0);
  94. length = min(strcspn(options, " ") + 1,
  95. (size_t)(sizeof(device->options)));
  96. strlcpy(device->options, options, length);
  97. }
  98. if (port->iotype == UPIO_MEM || port->iotype == UPIO_MEM16 ||
  99. port->iotype == UPIO_MEM32 || port->iotype == UPIO_MEM32BE)
  100. pr_info("Early serial console at MMIO%s 0x%llx (options '%s')\n",
  101. (port->iotype == UPIO_MEM) ? "" :
  102. (port->iotype == UPIO_MEM16) ? "16" :
  103. (port->iotype == UPIO_MEM32) ? "32" : "32be",
  104. (unsigned long long)port->mapbase,
  105. device->options);
  106. else
  107. pr_info("Early serial console at I/O port 0x%lx (options '%s')\n",
  108. port->iobase,
  109. device->options);
  110. return 0;
  111. }
  112. static int __init register_earlycon(char *buf, const struct earlycon_id *match)
  113. {
  114. int err;
  115. struct uart_port *port = &early_console_dev.port;
  116. /* On parsing error, pass the options buf to the setup function */
  117. if (buf && !parse_options(&early_console_dev, buf))
  118. buf = NULL;
  119. spin_lock_init(&port->lock);
  120. port->uartclk = BASE_BAUD * 16;
  121. if (port->mapbase)
  122. port->membase = earlycon_map(port->mapbase, 64);
  123. earlycon_init(&early_console_dev, match->name);
  124. err = match->setup(&early_console_dev, buf);
  125. if (err < 0)
  126. return err;
  127. if (!early_console_dev.con->write)
  128. return -ENODEV;
  129. register_console(early_console_dev.con);
  130. return 0;
  131. }
  132. /**
  133. * setup_earlycon - match and register earlycon console
  134. * @buf: earlycon param string
  135. *
  136. * Registers the earlycon console matching the earlycon specified
  137. * in the param string @buf. Acceptable param strings are of the form
  138. * <name>,io|mmio|mmio32|mmio32be,<addr>,<options>
  139. * <name>,0x<addr>,<options>
  140. * <name>,<options>
  141. * <name>
  142. *
  143. * Only for the third form does the earlycon setup() method receive the
  144. * <options> string in the 'options' parameter; all other forms set
  145. * the parameter to NULL.
  146. *
  147. * Returns 0 if an attempt to register the earlycon was made,
  148. * otherwise negative error code
  149. */
  150. int __init setup_earlycon(char *buf)
  151. {
  152. const struct earlycon_id *match;
  153. if (!buf || !buf[0])
  154. return -EINVAL;
  155. if (early_con.flags & CON_ENABLED)
  156. return -EALREADY;
  157. for (match = __earlycon_table; match < __earlycon_table_end; 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. /* early_param wrapper for setup_earlycon() */
  172. static int __init param_setup_earlycon(char *buf)
  173. {
  174. int err;
  175. /*
  176. * Just 'earlycon' is a valid param for devicetree earlycons;
  177. * don't generate a warning from parse_early_params() in that case
  178. */
  179. if (!buf || !buf[0])
  180. return 0;
  181. err = setup_earlycon(buf);
  182. if (err == -ENOENT || err == -EALREADY)
  183. return 0;
  184. return err;
  185. }
  186. early_param("earlycon", param_setup_earlycon);
  187. #ifdef CONFIG_OF_EARLY_FLATTREE
  188. int __init of_setup_earlycon(const struct earlycon_id *match,
  189. unsigned long node,
  190. const char *options)
  191. {
  192. int err;
  193. struct uart_port *port = &early_console_dev.port;
  194. const __be32 *val;
  195. bool big_endian;
  196. u64 addr;
  197. spin_lock_init(&port->lock);
  198. port->iotype = UPIO_MEM;
  199. addr = of_flat_dt_translate_address(node);
  200. if (addr == OF_BAD_ADDR) {
  201. pr_warn("[%s] bad address\n", match->name);
  202. return -ENXIO;
  203. }
  204. port->mapbase = addr;
  205. port->uartclk = BASE_BAUD * 16;
  206. port->membase = earlycon_map(port->mapbase, SZ_4K);
  207. val = of_get_flat_dt_prop(node, "reg-offset", NULL);
  208. if (val)
  209. port->mapbase += be32_to_cpu(*val);
  210. val = of_get_flat_dt_prop(node, "reg-shift", NULL);
  211. if (val)
  212. port->regshift = be32_to_cpu(*val);
  213. big_endian = of_get_flat_dt_prop(node, "big-endian", NULL) != NULL ||
  214. (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN) &&
  215. of_get_flat_dt_prop(node, "native-endian", NULL) != NULL);
  216. val = of_get_flat_dt_prop(node, "reg-io-width", NULL);
  217. if (val) {
  218. switch (be32_to_cpu(*val)) {
  219. case 1:
  220. port->iotype = UPIO_MEM;
  221. break;
  222. case 2:
  223. port->iotype = UPIO_MEM16;
  224. break;
  225. case 4:
  226. port->iotype = (big_endian) ? UPIO_MEM32BE : UPIO_MEM32;
  227. break;
  228. default:
  229. pr_warn("[%s] unsupported reg-io-width\n", match->name);
  230. return -EINVAL;
  231. }
  232. }
  233. if (options) {
  234. strlcpy(early_console_dev.options, options,
  235. sizeof(early_console_dev.options));
  236. }
  237. earlycon_init(&early_console_dev, match->name);
  238. err = match->setup(&early_console_dev, options);
  239. if (err < 0)
  240. return err;
  241. if (!early_console_dev.con->write)
  242. return -ENODEV;
  243. register_console(early_console_dev.con);
  244. return 0;
  245. }
  246. #endif /* CONFIG_OF_EARLY_FLATTREE */