earlycon.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. #include <linux/console.h>
  14. #include <linux/kernel.h>
  15. #include <linux/init.h>
  16. #include <linux/io.h>
  17. #include <linux/serial_core.h>
  18. #include <linux/sizes.h>
  19. #include <linux/mod_devicetable.h>
  20. #ifdef CONFIG_FIX_EARLYCON_MEM
  21. #include <asm/fixmap.h>
  22. #endif
  23. #include <asm/serial.h>
  24. static struct console early_con = {
  25. .name = "uart", /* 8250 console switch requires this name */
  26. .flags = CON_PRINTBUFFER | CON_BOOT,
  27. .index = -1,
  28. };
  29. static struct earlycon_device early_console_dev = {
  30. .con = &early_con,
  31. };
  32. static const struct of_device_id __earlycon_of_table_sentinel
  33. __used __section(__earlycon_of_table_end);
  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 int __init parse_options(struct earlycon_device *device,
  50. char *options)
  51. {
  52. struct uart_port *port = &device->port;
  53. int mmio, mmio32, length;
  54. unsigned long addr;
  55. if (!options)
  56. return -ENODEV;
  57. mmio = !strncmp(options, "mmio,", 5);
  58. mmio32 = !strncmp(options, "mmio32,", 7);
  59. if (mmio || mmio32) {
  60. port->iotype = (mmio ? UPIO_MEM : UPIO_MEM32);
  61. options += mmio ? 5 : 7;
  62. addr = simple_strtoul(options, NULL, 0);
  63. port->mapbase = addr;
  64. if (mmio32)
  65. port->regshift = 2;
  66. } else if (!strncmp(options, "io,", 3)) {
  67. port->iotype = UPIO_PORT;
  68. options += 3;
  69. addr = simple_strtoul(options, NULL, 0);
  70. port->iobase = addr;
  71. mmio = 0;
  72. } else if (!strncmp(options, "0x", 2)) {
  73. port->iotype = UPIO_MEM;
  74. addr = simple_strtoul(options, NULL, 0);
  75. port->mapbase = addr;
  76. } else {
  77. return -EINVAL;
  78. }
  79. port->uartclk = BASE_BAUD * 16;
  80. options = strchr(options, ',');
  81. if (options) {
  82. options++;
  83. device->baud = simple_strtoul(options, NULL, 0);
  84. length = min(strcspn(options, " ") + 1,
  85. (size_t)(sizeof(device->options)));
  86. strlcpy(device->options, options, length);
  87. }
  88. if (mmio || mmio32)
  89. pr_info("Early serial console at MMIO%s 0x%llx (options '%s')\n",
  90. mmio32 ? "32" : "",
  91. (unsigned long long)port->mapbase,
  92. device->options);
  93. else
  94. pr_info("Early serial console at I/O port 0x%lx (options '%s')\n",
  95. port->iobase,
  96. device->options);
  97. return 0;
  98. }
  99. int __init setup_earlycon(char *buf, const char *match,
  100. int (*setup)(struct earlycon_device *, const char *))
  101. {
  102. int err;
  103. size_t len;
  104. struct uart_port *port = &early_console_dev.port;
  105. if (!buf || !match || !setup)
  106. return 0;
  107. len = strlen(match);
  108. if (strncmp(buf, match, len))
  109. return 0;
  110. if (buf[len] && (buf[len] != ','))
  111. return 0;
  112. buf += len + 1;
  113. err = parse_options(&early_console_dev, buf);
  114. /* On parsing error, pass the options buf to the setup function */
  115. if (!err)
  116. buf = NULL;
  117. if (port->mapbase)
  118. port->membase = earlycon_map(port->mapbase, 64);
  119. early_console_dev.con->data = &early_console_dev;
  120. err = setup(&early_console_dev, buf);
  121. if (err < 0)
  122. return err;
  123. if (!early_console_dev.con->write)
  124. return -ENODEV;
  125. register_console(early_console_dev.con);
  126. return 0;
  127. }
  128. int __init of_setup_earlycon(unsigned long addr,
  129. int (*setup)(struct earlycon_device *, const char *))
  130. {
  131. int err;
  132. struct uart_port *port = &early_console_dev.port;
  133. port->iotype = UPIO_MEM;
  134. port->mapbase = addr;
  135. port->uartclk = BASE_BAUD * 16;
  136. port->membase = earlycon_map(addr, SZ_4K);
  137. early_console_dev.con->data = &early_console_dev;
  138. err = setup(&early_console_dev, NULL);
  139. if (err < 0)
  140. return err;
  141. if (!early_console_dev.con->write)
  142. return -ENODEV;
  143. register_console(early_console_dev.con);
  144. return 0;
  145. }