earlycon.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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/mod_devicetable.h>
  21. #ifdef CONFIG_FIX_EARLYCON_MEM
  22. #include <asm/fixmap.h>
  23. #endif
  24. #include <asm/serial.h>
  25. static struct console early_con = {
  26. .name = "uart", /* 8250 console switch requires this name */
  27. .flags = CON_PRINTBUFFER | CON_BOOT,
  28. .index = -1,
  29. };
  30. static struct earlycon_device early_console_dev = {
  31. .con = &early_con,
  32. };
  33. extern struct earlycon_id __earlycon_table[];
  34. static const struct earlycon_id __earlycon_table_sentinel
  35. __used __section(__earlycon_table_end);
  36. static const struct of_device_id __earlycon_of_table_sentinel
  37. __used __section(__earlycon_of_table_end);
  38. static void __iomem * __init earlycon_map(unsigned long paddr, size_t size)
  39. {
  40. void __iomem *base;
  41. #ifdef CONFIG_FIX_EARLYCON_MEM
  42. set_fixmap_io(FIX_EARLYCON_MEM_BASE, paddr & PAGE_MASK);
  43. base = (void __iomem *)__fix_to_virt(FIX_EARLYCON_MEM_BASE);
  44. base += paddr & ~PAGE_MASK;
  45. #else
  46. base = ioremap(paddr, size);
  47. #endif
  48. if (!base)
  49. pr_err("%s: Couldn't map 0x%llx\n", __func__,
  50. (unsigned long long)paddr);
  51. return base;
  52. }
  53. static int __init parse_options(struct earlycon_device *device, char *options)
  54. {
  55. struct uart_port *port = &device->port;
  56. int length;
  57. unsigned long addr;
  58. if (uart_parse_earlycon(options, &port->iotype, &addr, &options))
  59. return -EINVAL;
  60. switch (port->iotype) {
  61. case UPIO_MEM32:
  62. port->regshift = 2; /* fall-through */
  63. case UPIO_MEM:
  64. port->mapbase = addr;
  65. break;
  66. case UPIO_PORT:
  67. port->iobase = addr;
  68. break;
  69. default:
  70. return -EINVAL;
  71. }
  72. if (options) {
  73. device->baud = simple_strtoul(options, NULL, 0);
  74. length = min(strcspn(options, " ") + 1,
  75. (size_t)(sizeof(device->options)));
  76. strlcpy(device->options, options, length);
  77. }
  78. if (port->iotype == UPIO_MEM || port->iotype == UPIO_MEM32)
  79. pr_info("Early serial console at MMIO%s 0x%llx (options '%s')\n",
  80. (port->iotype == UPIO_MEM32) ? "32" : "",
  81. (unsigned long long)port->mapbase,
  82. device->options);
  83. else
  84. pr_info("Early serial console at I/O port 0x%lx (options '%s')\n",
  85. port->iobase,
  86. device->options);
  87. return 0;
  88. }
  89. static int __init register_earlycon(char *buf, const struct earlycon_id *match)
  90. {
  91. int err;
  92. struct uart_port *port = &early_console_dev.port;
  93. /* On parsing error, pass the options buf to the setup function */
  94. if (buf && !parse_options(&early_console_dev, buf))
  95. buf = NULL;
  96. port->uartclk = BASE_BAUD * 16;
  97. if (port->mapbase)
  98. port->membase = earlycon_map(port->mapbase, 64);
  99. early_console_dev.con->data = &early_console_dev;
  100. err = match->setup(&early_console_dev, buf);
  101. if (err < 0)
  102. return err;
  103. if (!early_console_dev.con->write)
  104. return -ENODEV;
  105. register_console(early_console_dev.con);
  106. return 0;
  107. }
  108. /**
  109. * setup_earlycon - match and register earlycon console
  110. * @buf: earlycon param string
  111. *
  112. * Registers the earlycon console matching the earlycon specified
  113. * in the param string @buf. Acceptable param strings are of the form
  114. * <name>,io|mmio|mmio32,<addr>,<options>
  115. * <name>,0x<addr>,<options>
  116. * <name>,<options>
  117. * <name>
  118. *
  119. * Only for the third form does the earlycon setup() method receive the
  120. * <options> string in the 'options' parameter; all other forms set
  121. * the parameter to NULL.
  122. *
  123. * Returns 0 if an attempt to register the earlycon was made,
  124. * otherwise negative error code
  125. */
  126. int __init setup_earlycon(char *buf)
  127. {
  128. const struct earlycon_id *match;
  129. if (!buf || !buf[0])
  130. return -EINVAL;
  131. if (early_con.flags & CON_ENABLED)
  132. return -EALREADY;
  133. for (match = __earlycon_table; match->name[0]; match++) {
  134. size_t len = strlen(match->name);
  135. if (strncmp(buf, match->name, len))
  136. continue;
  137. if (buf[len]) {
  138. if (buf[len] != ',')
  139. continue;
  140. buf += len + 1;
  141. } else
  142. buf = NULL;
  143. return register_earlycon(buf, match);
  144. }
  145. return -ENOENT;
  146. }
  147. /* early_param wrapper for setup_earlycon() */
  148. static int __init param_setup_earlycon(char *buf)
  149. {
  150. int err;
  151. /*
  152. * Just 'earlycon' is a valid param for devicetree earlycons;
  153. * don't generate a warning from parse_early_params() in that case
  154. */
  155. if (!buf || !buf[0])
  156. return 0;
  157. err = setup_earlycon(buf);
  158. if (err == -ENOENT || err == -EALREADY)
  159. return 0;
  160. return err;
  161. }
  162. early_param("earlycon", param_setup_earlycon);
  163. int __init of_setup_earlycon(unsigned long addr,
  164. int (*setup)(struct earlycon_device *, const char *))
  165. {
  166. int err;
  167. struct uart_port *port = &early_console_dev.port;
  168. port->iotype = UPIO_MEM;
  169. port->mapbase = addr;
  170. port->uartclk = BASE_BAUD * 16;
  171. port->membase = earlycon_map(addr, SZ_4K);
  172. early_console_dev.con->data = &early_console_dev;
  173. err = setup(&early_console_dev, NULL);
  174. if (err < 0)
  175. return err;
  176. if (!early_console_dev.con->write)
  177. return -ENODEV;
  178. register_console(early_console_dev.con);
  179. return 0;
  180. }