early_console.c 3.1 KB

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