early_console.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 <asm/setup.h>
  17. #include "pic32mzda.h"
  18. #include "early_pin.h"
  19. /* Default early console parameters */
  20. #define EARLY_CONSOLE_PORT 1
  21. #define EARLY_CONSOLE_BAUDRATE 115200
  22. #define UART_ENABLE BIT(15)
  23. #define UART_ENABLE_RX BIT(12)
  24. #define UART_ENABLE_TX BIT(10)
  25. #define UART_TX_FULL BIT(9)
  26. /* UART1(x == 0) - UART6(x == 5) */
  27. #define UART_BASE(x) ((x) * 0x0200)
  28. #define U_MODE(x) UART_BASE(x)
  29. #define U_STA(x) (UART_BASE(x) + 0x10)
  30. #define U_TXR(x) (UART_BASE(x) + 0x20)
  31. #define U_BRG(x) (UART_BASE(x) + 0x40)
  32. static void __iomem *uart_base;
  33. static char console_port = -1;
  34. static int __init configure_uart_pins(int port)
  35. {
  36. switch (port) {
  37. case 1:
  38. pic32_pps_input(IN_FUNC_U2RX, IN_RPB0);
  39. pic32_pps_output(OUT_FUNC_U2TX, OUT_RPG9);
  40. break;
  41. case 5:
  42. pic32_pps_input(IN_FUNC_U6RX, IN_RPD0);
  43. pic32_pps_output(OUT_FUNC_U6TX, OUT_RPB8);
  44. break;
  45. default:
  46. return -1;
  47. }
  48. return 0;
  49. }
  50. static void __init configure_uart(char port, int baud)
  51. {
  52. u32 pbclk;
  53. pbclk = pic32_get_pbclk(2);
  54. __raw_writel(0, uart_base + U_MODE(port));
  55. __raw_writel(((pbclk / baud) / 16) - 1, uart_base + U_BRG(port));
  56. __raw_writel(UART_ENABLE, uart_base + U_MODE(port));
  57. __raw_writel(UART_ENABLE_TX | UART_ENABLE_RX,
  58. uart_base + PIC32_SET(U_STA(port)));
  59. }
  60. static void __init setup_early_console(char port, int baud)
  61. {
  62. if (configure_uart_pins(port))
  63. return;
  64. console_port = port;
  65. configure_uart(console_port, baud);
  66. }
  67. static char * __init pic32_getcmdline(void)
  68. {
  69. /*
  70. * arch_mem_init() has not been called yet, so we don't have a real
  71. * command line setup if using CONFIG_CMDLINE_BOOL.
  72. */
  73. #ifdef CONFIG_CMDLINE_OVERRIDE
  74. return CONFIG_CMDLINE;
  75. #else
  76. return fw_getcmdline();
  77. #endif
  78. }
  79. static int __init get_port_from_cmdline(char *arch_cmdline)
  80. {
  81. char *s;
  82. int port = -1;
  83. if (!arch_cmdline || *arch_cmdline == '\0')
  84. goto _out;
  85. s = strstr(arch_cmdline, "earlyprintk=");
  86. if (s) {
  87. s = strstr(s, "ttyS");
  88. if (s)
  89. s += 4;
  90. else
  91. goto _out;
  92. port = (*s) - '0';
  93. }
  94. _out:
  95. return port;
  96. }
  97. static int __init get_baud_from_cmdline(char *arch_cmdline)
  98. {
  99. char *s;
  100. int baud = -1;
  101. if (!arch_cmdline || *arch_cmdline == '\0')
  102. goto _out;
  103. s = strstr(arch_cmdline, "earlyprintk=");
  104. if (s) {
  105. s = strstr(s, "ttyS");
  106. if (s)
  107. s += 6;
  108. else
  109. goto _out;
  110. baud = 0;
  111. while (*s >= '0' && *s <= '9')
  112. baud = baud * 10 + *s++ - '0';
  113. }
  114. _out:
  115. return baud;
  116. }
  117. void __init fw_init_early_console(char port)
  118. {
  119. char *arch_cmdline = pic32_getcmdline();
  120. int baud = -1;
  121. uart_base = ioremap_nocache(PIC32_BASE_UART, 0xc00);
  122. baud = get_baud_from_cmdline(arch_cmdline);
  123. if (port == -1)
  124. port = get_port_from_cmdline(arch_cmdline);
  125. if (port == -1)
  126. port = EARLY_CONSOLE_PORT;
  127. if (baud == -1)
  128. baud = EARLY_CONSOLE_BAUDRATE;
  129. setup_early_console(port, baud);
  130. }
  131. void prom_putchar(char c)
  132. {
  133. if (console_port >= 0) {
  134. while (__raw_readl(
  135. uart_base + U_STA(console_port)) & UART_TX_FULL)
  136. ;
  137. __raw_writel(c, uart_base + U_TXR(console_port));
  138. }
  139. }