earlycon.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. 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. unsigned long 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. /* early_param wrapper for setup_earlycon() */
  173. static int __init param_setup_earlycon(char *buf)
  174. {
  175. int err;
  176. /*
  177. * Just 'earlycon' is a valid param for devicetree earlycons;
  178. * don't generate a warning from parse_early_params() in that case
  179. */
  180. if (!buf || !buf[0])
  181. return 0;
  182. err = setup_earlycon(buf);
  183. if (err == -ENOENT || err == -EALREADY)
  184. return 0;
  185. return err;
  186. }
  187. early_param("earlycon", param_setup_earlycon);
  188. #ifdef CONFIG_OF_EARLY_FLATTREE
  189. int __init of_setup_earlycon(const struct earlycon_id *match,
  190. unsigned long node,
  191. const char *options)
  192. {
  193. int err;
  194. struct uart_port *port = &early_console_dev.port;
  195. const __be32 *val;
  196. bool big_endian;
  197. u64 addr;
  198. spin_lock_init(&port->lock);
  199. port->iotype = UPIO_MEM;
  200. addr = of_flat_dt_translate_address(node);
  201. if (addr == OF_BAD_ADDR) {
  202. pr_warn("[%s] bad address\n", match->name);
  203. return -ENXIO;
  204. }
  205. port->mapbase = addr;
  206. port->uartclk = BASE_BAUD * 16;
  207. port->membase = earlycon_map(port->mapbase, SZ_4K);
  208. val = of_get_flat_dt_prop(node, "reg-offset", NULL);
  209. if (val)
  210. port->mapbase += be32_to_cpu(*val);
  211. val = of_get_flat_dt_prop(node, "reg-shift", NULL);
  212. if (val)
  213. port->regshift = be32_to_cpu(*val);
  214. big_endian = of_get_flat_dt_prop(node, "big-endian", NULL) != NULL ||
  215. (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN) &&
  216. of_get_flat_dt_prop(node, "native-endian", NULL) != NULL);
  217. val = of_get_flat_dt_prop(node, "reg-io-width", NULL);
  218. if (val) {
  219. switch (be32_to_cpu(*val)) {
  220. case 1:
  221. port->iotype = UPIO_MEM;
  222. break;
  223. case 2:
  224. port->iotype = UPIO_MEM16;
  225. break;
  226. case 4:
  227. port->iotype = (big_endian) ? UPIO_MEM32BE : UPIO_MEM32;
  228. break;
  229. default:
  230. pr_warn("[%s] unsupported reg-io-width\n", match->name);
  231. return -EINVAL;
  232. }
  233. }
  234. if (options) {
  235. strlcpy(early_console_dev.options, options,
  236. sizeof(early_console_dev.options));
  237. }
  238. earlycon_init(&early_console_dev, match->name);
  239. err = match->setup(&early_console_dev, options);
  240. if (err < 0)
  241. return err;
  242. if (!early_console_dev.con->write)
  243. return -ENODEV;
  244. register_console(early_console_dev.con);
  245. return 0;
  246. }
  247. #endif /* CONFIG_OF_EARLY_FLATTREE */