early_printk.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * Earlyprintk support.
  3. *
  4. * Copyright (C) 2012 ARM Ltd.
  5. * Author: Catalin Marinas <catalin.marinas@arm.com>
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <linux/kernel.h>
  20. #include <linux/console.h>
  21. #include <linux/init.h>
  22. #include <linux/string.h>
  23. #include <linux/mm.h>
  24. #include <linux/io.h>
  25. #include <linux/amba/serial.h>
  26. static void __iomem *early_base;
  27. static void (*printch)(char ch);
  28. /*
  29. * PL011 single character TX.
  30. */
  31. static void pl011_printch(char ch)
  32. {
  33. while (readl_relaxed(early_base + UART01x_FR) & UART01x_FR_TXFF)
  34. ;
  35. writeb_relaxed(ch, early_base + UART01x_DR);
  36. while (readl_relaxed(early_base + UART01x_FR) & UART01x_FR_BUSY)
  37. ;
  38. }
  39. /*
  40. * Semihosting-based debug console
  41. */
  42. static void smh_printch(char ch)
  43. {
  44. asm volatile("mov x1, %0\n"
  45. "mov x0, #3\n"
  46. "hlt 0xf000\n"
  47. : : "r" (&ch) : "x0", "x1", "memory");
  48. }
  49. struct earlycon_match {
  50. const char *name;
  51. void (*printch)(char ch);
  52. };
  53. static const struct earlycon_match earlycon_match[] __initconst = {
  54. { .name = "pl011", .printch = pl011_printch, },
  55. { .name = "smh", .printch = smh_printch, },
  56. {}
  57. };
  58. static void early_write(struct console *con, const char *s, unsigned n)
  59. {
  60. while (n-- > 0) {
  61. if (*s == '\n')
  62. printch('\r');
  63. printch(*s);
  64. s++;
  65. }
  66. }
  67. static struct console early_console = {
  68. .name = "earlycon",
  69. .write = early_write,
  70. .flags = CON_PRINTBUFFER | CON_BOOT,
  71. .index = -1,
  72. };
  73. /*
  74. * Parse earlyprintk=... parameter in the format:
  75. *
  76. * <name>[,<addr>][,<options>]
  77. *
  78. * and register the early console. It is assumed that the UART has been
  79. * initialised by the bootloader already.
  80. */
  81. static int __init setup_early_printk(char *buf)
  82. {
  83. const struct earlycon_match *match = earlycon_match;
  84. phys_addr_t paddr = 0;
  85. if (!buf) {
  86. pr_warning("No earlyprintk arguments passed.\n");
  87. return 0;
  88. }
  89. while (match->name) {
  90. size_t len = strlen(match->name);
  91. if (!strncmp(buf, match->name, len)) {
  92. buf += len;
  93. break;
  94. }
  95. match++;
  96. }
  97. if (!match->name) {
  98. pr_warning("Unknown earlyprintk arguments: %s\n", buf);
  99. return 0;
  100. }
  101. /* I/O address */
  102. if (!strncmp(buf, ",0x", 3)) {
  103. char *e;
  104. paddr = simple_strtoul(buf + 1, &e, 16);
  105. buf = e;
  106. }
  107. /* no options parsing yet */
  108. if (paddr)
  109. early_base = early_io_map(paddr, EARLYCON_IOBASE);
  110. printch = match->printch;
  111. register_console(&early_console);
  112. return 0;
  113. }
  114. early_param("earlyprintk", setup_early_printk);