earlycon.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. case UPIO_MEM32BE:
  63. port->regshift = 2; /* fall-through */
  64. case UPIO_MEM:
  65. port->mapbase = addr;
  66. break;
  67. case UPIO_PORT:
  68. port->iobase = addr;
  69. break;
  70. default:
  71. return -EINVAL;
  72. }
  73. if (options) {
  74. device->baud = simple_strtoul(options, NULL, 0);
  75. length = min(strcspn(options, " ") + 1,
  76. (size_t)(sizeof(device->options)));
  77. strlcpy(device->options, options, length);
  78. }
  79. if (port->iotype == UPIO_MEM || port->iotype == UPIO_MEM32 ||
  80. port->iotype == UPIO_MEM32BE)
  81. pr_info("Early serial console at MMIO%s 0x%llx (options '%s')\n",
  82. (port->iotype == UPIO_MEM) ? "" :
  83. (port->iotype == UPIO_MEM32) ? "32" : "32be",
  84. (unsigned long long)port->mapbase,
  85. device->options);
  86. else
  87. pr_info("Early serial console at I/O port 0x%lx (options '%s')\n",
  88. port->iobase,
  89. device->options);
  90. return 0;
  91. }
  92. static int __init register_earlycon(char *buf, const struct earlycon_id *match)
  93. {
  94. int err;
  95. struct uart_port *port = &early_console_dev.port;
  96. /* On parsing error, pass the options buf to the setup function */
  97. if (buf && !parse_options(&early_console_dev, buf))
  98. buf = NULL;
  99. spin_lock_init(&port->lock);
  100. port->uartclk = BASE_BAUD * 16;
  101. if (port->mapbase)
  102. port->membase = earlycon_map(port->mapbase, 64);
  103. early_console_dev.con->data = &early_console_dev;
  104. err = match->setup(&early_console_dev, buf);
  105. if (err < 0)
  106. return err;
  107. if (!early_console_dev.con->write)
  108. return -ENODEV;
  109. register_console(early_console_dev.con);
  110. return 0;
  111. }
  112. /**
  113. * setup_earlycon - match and register earlycon console
  114. * @buf: earlycon param string
  115. *
  116. * Registers the earlycon console matching the earlycon specified
  117. * in the param string @buf. Acceptable param strings are of the form
  118. * <name>,io|mmio|mmio32|mmio32be,<addr>,<options>
  119. * <name>,0x<addr>,<options>
  120. * <name>,<options>
  121. * <name>
  122. *
  123. * Only for the third form does the earlycon setup() method receive the
  124. * <options> string in the 'options' parameter; all other forms set
  125. * the parameter to NULL.
  126. *
  127. * Returns 0 if an attempt to register the earlycon was made,
  128. * otherwise negative error code
  129. */
  130. int __init setup_earlycon(char *buf)
  131. {
  132. const struct earlycon_id *match;
  133. if (!buf || !buf[0])
  134. return -EINVAL;
  135. if (early_con.flags & CON_ENABLED)
  136. return -EALREADY;
  137. for (match = __earlycon_table; match->name[0]; match++) {
  138. size_t len = strlen(match->name);
  139. if (strncmp(buf, match->name, len))
  140. continue;
  141. if (buf[len]) {
  142. if (buf[len] != ',')
  143. continue;
  144. buf += len + 1;
  145. } else
  146. buf = NULL;
  147. return register_earlycon(buf, match);
  148. }
  149. return -ENOENT;
  150. }
  151. /* early_param wrapper for setup_earlycon() */
  152. static int __init param_setup_earlycon(char *buf)
  153. {
  154. int err;
  155. /*
  156. * Just 'earlycon' is a valid param for devicetree earlycons;
  157. * don't generate a warning from parse_early_params() in that case
  158. */
  159. if (!buf || !buf[0])
  160. return 0;
  161. err = setup_earlycon(buf);
  162. if (err == -ENOENT || err == -EALREADY)
  163. return 0;
  164. return err;
  165. }
  166. early_param("earlycon", param_setup_earlycon);
  167. int __init of_setup_earlycon(unsigned long addr,
  168. int (*setup)(struct earlycon_device *, const char *))
  169. {
  170. int err;
  171. struct uart_port *port = &early_console_dev.port;
  172. spin_lock_init(&port->lock);
  173. port->iotype = UPIO_MEM;
  174. port->mapbase = addr;
  175. port->uartclk = BASE_BAUD * 16;
  176. port->membase = earlycon_map(addr, SZ_4K);
  177. early_console_dev.con->data = &early_console_dev;
  178. err = setup(&early_console_dev, NULL);
  179. if (err < 0)
  180. return err;
  181. if (!early_console_dev.con->write)
  182. return -ENODEV;
  183. register_console(early_console_dev.con);
  184. return 0;
  185. }