uart.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * Copyright (c) 2020-2022, MediaTek Inc. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <lib/mmio.h>
  7. #include <uart.h>
  8. static struct mt_uart uart_save_addr[DRV_SUPPORT_UART_PORTS];
  9. static const uint32_t uart_base_addr[DRV_SUPPORT_UART_PORTS] = {
  10. UART0_BASE,
  11. UART1_BASE
  12. };
  13. void mt_uart_restore(void)
  14. {
  15. int uart_idx = UART_PORT0;
  16. struct mt_uart *uart;
  17. unsigned long base;
  18. /* Must NOT print any debug log before UART restore */
  19. for (uart_idx = UART_PORT0; uart_idx < HW_SUPPORT_UART_PORTS;
  20. uart_idx++) {
  21. uart = &uart_save_addr[uart_idx];
  22. base = uart->base;
  23. mmio_write_32(UART_LCR(base), UART_LCR_MODE_B);
  24. mmio_write_32(UART_EFR(base), uart->registers.efr);
  25. mmio_write_32(UART_LCR(base), uart->registers.lcr);
  26. mmio_write_32(UART_FCR(base), uart->registers.fcr);
  27. /* baudrate */
  28. mmio_write_32(UART_HIGHSPEED(base), uart->registers.highspeed);
  29. mmio_write_32(UART_FRACDIV_L(base), uart->registers.fracdiv_l);
  30. mmio_write_32(UART_FRACDIV_M(base), uart->registers.fracdiv_m);
  31. mmio_write_32(UART_LCR(base),
  32. uart->registers.lcr | UART_LCR_DLAB);
  33. mmio_write_32(UART_DLL(base), uart->registers.dll);
  34. mmio_write_32(UART_DLH(base), uart->registers.dlh);
  35. mmio_write_32(UART_LCR(base), uart->registers.lcr);
  36. mmio_write_32(UART_SAMPLE_COUNT(base),
  37. uart->registers.sample_count);
  38. mmio_write_32(UART_SAMPLE_POINT(base),
  39. uart->registers.sample_point);
  40. mmio_write_32(UART_GUARD(base), uart->registers.guard);
  41. /* flow control */
  42. mmio_write_32(UART_ESCAPE_EN(base), uart->registers.escape_en);
  43. mmio_write_32(UART_MCR(base), uart->registers.mcr);
  44. mmio_write_32(UART_IER(base), uart->registers.ier);
  45. mmio_write_32(UART_SCR(base), uart->registers.scr);
  46. }
  47. }
  48. void mt_uart_save(void)
  49. {
  50. int uart_idx = UART_PORT0;
  51. struct mt_uart *uart;
  52. unsigned long base;
  53. for (uart_idx = UART_PORT0; uart_idx < HW_SUPPORT_UART_PORTS;
  54. uart_idx++) {
  55. uart_save_addr[uart_idx].base = uart_base_addr[uart_idx];
  56. base = uart_base_addr[uart_idx];
  57. uart = &uart_save_addr[uart_idx];
  58. uart->registers.lcr = mmio_read_32(UART_LCR(base));
  59. mmio_write_32(UART_LCR(base), UART_LCR_MODE_B);
  60. uart->registers.efr = mmio_read_32(UART_EFR(base));
  61. mmio_write_32(UART_LCR(base), uart->registers.lcr);
  62. uart->registers.fcr = mmio_read_32(UART_FCR_RD(base));
  63. /* baudrate */
  64. uart->registers.highspeed = mmio_read_32(UART_HIGHSPEED(base));
  65. uart->registers.fracdiv_l = mmio_read_32(UART_FRACDIV_L(base));
  66. uart->registers.fracdiv_m = mmio_read_32(UART_FRACDIV_M(base));
  67. mmio_write_32(UART_LCR(base),
  68. uart->registers.lcr | UART_LCR_DLAB);
  69. uart->registers.dll = mmio_read_32(UART_DLL(base));
  70. uart->registers.dlh = mmio_read_32(UART_DLH(base));
  71. mmio_write_32(UART_LCR(base), uart->registers.lcr);
  72. uart->registers.sample_count = mmio_read_32(
  73. UART_SAMPLE_COUNT(base));
  74. uart->registers.sample_point = mmio_read_32(
  75. UART_SAMPLE_POINT(base));
  76. uart->registers.guard = mmio_read_32(UART_GUARD(base));
  77. /* flow control */
  78. uart->registers.escape_en = mmio_read_32(UART_ESCAPE_EN(base));
  79. uart->registers.mcr = mmio_read_32(UART_MCR(base));
  80. uart->registers.ier = mmio_read_32(UART_IER(base));
  81. uart->registers.scr = mmio_read_32(UART_SCR(base));
  82. }
  83. }
  84. void mt_console_uart_cg(int on)
  85. {
  86. if (on == 1) {
  87. mmio_write_32(UART_CLOCK_GATE_CLR, UART0_CLOCK_GATE_BIT);
  88. } else {
  89. mmio_write_32(UART_CLOCK_GATE_SET, UART0_CLOCK_GATE_BIT);
  90. }
  91. }
  92. uint32_t mt_console_uart_cg_status(void)
  93. {
  94. return mmio_read_32(UART_CLOCK_GATE_STA) & UART0_CLOCK_GATE_BIT;
  95. }