early_console.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * Joshua Henderson <joshua.henderson@microchip.com>
  3. * Copyright (C) 2015 Microchip Technology Inc. All rights reserved.
  4. *
  5. * This program is free software; you can distribute it and/or modify it
  6. * under the terms of the GNU General Public License (Version 2) as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. * for more details.
  13. */
  14. #include <asm/mach-pic32/pic32.h>
  15. #include <asm/fw/fw.h>
  16. #include "pic32mzda.h"
  17. #include "early_pin.h"
  18. /* Default early console parameters */
  19. #define EARLY_CONSOLE_PORT 1
  20. #define EARLY_CONSOLE_BAUDRATE 115200
  21. #define UART_ENABLE BIT(15)
  22. #define UART_ENABLE_RX BIT(12)
  23. #define UART_ENABLE_TX BIT(10)
  24. #define UART_TX_FULL BIT(9)
  25. /* UART1(x == 0) - UART6(x == 5) */
  26. #define UART_BASE(x) ((x) * 0x0200)
  27. #define U_MODE(x) UART_BASE(x)
  28. #define U_STA(x) (UART_BASE(x) + 0x10)
  29. #define U_TXR(x) (UART_BASE(x) + 0x20)
  30. #define U_BRG(x) (UART_BASE(x) + 0x40)
  31. static void __iomem *uart_base;
  32. static char console_port = -1;
  33. static int __init configure_uart_pins(int port)
  34. {
  35. switch (port) {
  36. case 1:
  37. pic32_pps_input(IN_FUNC_U2RX, IN_RPB0);
  38. pic32_pps_output(OUT_FUNC_U2TX, OUT_RPG9);
  39. break;
  40. case 5:
  41. pic32_pps_input(IN_FUNC_U6RX, IN_RPD0);
  42. pic32_pps_output(OUT_FUNC_U6TX, OUT_RPB8);
  43. break;
  44. default:
  45. return -1;
  46. }
  47. return 0;
  48. }
  49. static void __init configure_uart(char port, int baud)
  50. {
  51. u32 pbclk;
  52. pbclk = pic32_get_pbclk(2);
  53. __raw_writel(0, uart_base + U_MODE(port));
  54. __raw_writel(((pbclk / baud) / 16) - 1, uart_base + U_BRG(port));
  55. __raw_writel(UART_ENABLE, uart_base + U_MODE(port));
  56. __raw_writel(UART_ENABLE_TX | UART_ENABLE_RX,
  57. uart_base + PIC32_SET(U_STA(port)));
  58. }
  59. static void __init setup_early_console(char port, int baud)
  60. {
  61. if (configure_uart_pins(port))
  62. return;
  63. console_port = port;
  64. configure_uart(console_port, baud);
  65. }
  66. static char * __init pic32_getcmdline(void)
  67. {
  68. /*
  69. * arch_mem_init() has not been called yet, so we don't have a real
  70. * command line setup if using CONFIG_CMDLINE_BOOL.
  71. */
  72. #ifdef CONFIG_CMDLINE_OVERRIDE
  73. return CONFIG_CMDLINE;
  74. #else
  75. return fw_getcmdline();
  76. #endif
  77. }
  78. static int __init get_port_from_cmdline(char *arch_cmdline)
  79. {
  80. char *s;
  81. int port = -1;
  82. if (!arch_cmdline || *arch_cmdline == '\0')
  83. goto _out;
  84. s = strstr(arch_cmdline, "earlyprintk=");
  85. if (s) {
  86. s = strstr(s, "ttyS");
  87. if (s)
  88. s += 4;
  89. else
  90. goto _out;
  91. port = (*s) - '0';
  92. }
  93. _out:
  94. return port;
  95. }
  96. static int __init get_baud_from_cmdline(char *arch_cmdline)
  97. {
  98. char *s;
  99. int baud = -1;
  100. if (!arch_cmdline || *arch_cmdline == '\0')
  101. goto _out;
  102. s = strstr(arch_cmdline, "earlyprintk=");
  103. if (s) {
  104. s = strstr(s, "ttyS");
  105. if (s)
  106. s += 6;
  107. else
  108. goto _out;
  109. baud = 0;
  110. while (*s >= '0' && *s <= '9')
  111. baud = baud * 10 + *s++ - '0';
  112. }
  113. _out:
  114. return baud;
  115. }
  116. void __init fw_init_early_console(char port)
  117. {
  118. char *arch_cmdline = pic32_getcmdline();
  119. int baud = -1;
  120. uart_base = ioremap_nocache(PIC32_BASE_UART, 0xc00);
  121. baud = get_baud_from_cmdline(arch_cmdline);
  122. if (port == -1)
  123. port = get_port_from_cmdline(arch_cmdline);
  124. if (port == -1)
  125. port = EARLY_CONSOLE_PORT;
  126. if (baud == -1)
  127. baud = EARLY_CONSOLE_BAUDRATE;
  128. setup_early_console(port, baud);
  129. }
  130. int prom_putchar(char c)
  131. {
  132. if (console_port >= 0) {
  133. while (__raw_readl(
  134. uart_base + U_STA(console_port)) & UART_TX_FULL)
  135. ;
  136. __raw_writel(c, uart_base + U_TXR(console_port));
  137. }
  138. return 1;
  139. }