earlycon.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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, char *options)
  50. {
  51. struct uart_port *port = &device->port;
  52. int length;
  53. unsigned long addr;
  54. if (uart_parse_earlycon(options, &port->iotype, &addr, &options))
  55. return -EINVAL;
  56. switch (port->iotype) {
  57. case UPIO_MEM32:
  58. port->regshift = 2; /* fall-through */
  59. case UPIO_MEM:
  60. port->mapbase = addr;
  61. break;
  62. case UPIO_PORT:
  63. port->iobase = addr;
  64. break;
  65. default:
  66. return -EINVAL;
  67. }
  68. if (options) {
  69. device->baud = simple_strtoul(options, NULL, 0);
  70. length = min(strcspn(options, " ") + 1,
  71. (size_t)(sizeof(device->options)));
  72. strlcpy(device->options, options, length);
  73. }
  74. if (port->iotype == UPIO_MEM || port->iotype == UPIO_MEM32)
  75. pr_info("Early serial console at MMIO%s 0x%llx (options '%s')\n",
  76. (port->iotype == UPIO_MEM32) ? "32" : "",
  77. (unsigned long long)port->mapbase,
  78. device->options);
  79. else
  80. pr_info("Early serial console at I/O port 0x%lx (options '%s')\n",
  81. port->iobase,
  82. device->options);
  83. return 0;
  84. }
  85. int __init setup_earlycon(char *buf, const char *match,
  86. int (*setup)(struct earlycon_device *, const char *))
  87. {
  88. int err;
  89. size_t len;
  90. struct uart_port *port = &early_console_dev.port;
  91. if (!buf || !match || !setup)
  92. return 0;
  93. len = strlen(match);
  94. if (strncmp(buf, match, len))
  95. return 0;
  96. if (buf[len] && (buf[len] != ','))
  97. return 0;
  98. buf += len + 1;
  99. err = parse_options(&early_console_dev, buf);
  100. /* On parsing error, pass the options buf to the setup function */
  101. if (!err)
  102. buf = NULL;
  103. port->uartclk = BASE_BAUD * 16;
  104. if (port->mapbase)
  105. port->membase = earlycon_map(port->mapbase, 64);
  106. early_console_dev.con->data = &early_console_dev;
  107. err = setup(&early_console_dev, buf);
  108. if (err < 0)
  109. return err;
  110. if (!early_console_dev.con->write)
  111. return -ENODEV;
  112. register_console(early_console_dev.con);
  113. return 0;
  114. }
  115. int __init of_setup_earlycon(unsigned long addr,
  116. int (*setup)(struct earlycon_device *, const char *))
  117. {
  118. int err;
  119. struct uart_port *port = &early_console_dev.port;
  120. port->iotype = UPIO_MEM;
  121. port->mapbase = addr;
  122. port->uartclk = BASE_BAUD * 16;
  123. port->membase = earlycon_map(addr, SZ_4K);
  124. early_console_dev.con->data = &early_console_dev;
  125. err = setup(&early_console_dev, NULL);
  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. }