early_printk.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. struct earlycon_match {
  40. const char *name;
  41. void (*printch)(char ch);
  42. };
  43. static const struct earlycon_match earlycon_match[] __initconst = {
  44. { .name = "pl011", .printch = pl011_printch, },
  45. {}
  46. };
  47. static void early_write(struct console *con, const char *s, unsigned n)
  48. {
  49. while (n-- > 0) {
  50. if (*s == '\n')
  51. printch('\r');
  52. printch(*s);
  53. s++;
  54. }
  55. }
  56. static struct console early_console = {
  57. .name = "earlycon",
  58. .write = early_write,
  59. .flags = CON_PRINTBUFFER | CON_BOOT,
  60. .index = -1,
  61. };
  62. /*
  63. * Parse earlyprintk=... parameter in the format:
  64. *
  65. * <name>[,<addr>][,<options>]
  66. *
  67. * and register the early console. It is assumed that the UART has been
  68. * initialised by the bootloader already.
  69. */
  70. static int __init setup_early_printk(char *buf)
  71. {
  72. const struct earlycon_match *match = earlycon_match;
  73. phys_addr_t paddr = 0;
  74. if (!buf) {
  75. pr_warning("No earlyprintk arguments passed.\n");
  76. return 0;
  77. }
  78. while (match->name) {
  79. size_t len = strlen(match->name);
  80. if (!strncmp(buf, match->name, len)) {
  81. buf += len;
  82. break;
  83. }
  84. match++;
  85. }
  86. if (!match->name) {
  87. pr_warning("Unknown earlyprintk arguments: %s\n", buf);
  88. return 0;
  89. }
  90. /* I/O address */
  91. if (!strncmp(buf, ",0x", 3)) {
  92. char *e;
  93. paddr = simple_strtoul(buf + 1, &e, 16);
  94. buf = e;
  95. }
  96. /* no options parsing yet */
  97. if (paddr)
  98. early_base = early_io_map(paddr, EARLYCON_IOBASE);
  99. printch = match->printch;
  100. register_console(&early_console);
  101. return 0;
  102. }
  103. early_param("earlyprintk", setup_early_printk);