Masahiro Yamada | ca8b80e | 2019-07-02 22:03:16 +0900 | [diff] [blame] | 1 | /* |
Masahiro Yamada | ca971b5 | 2020-02-03 19:45:16 +0900 | [diff] [blame] | 2 | * Copyright (c) 2019-2020, Socionext Inc. All rights reserved. |
Masahiro Yamada | ca8b80e | 2019-07-02 22:03:16 +0900 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
Masahiro Yamada | ca971b5 | 2020-02-03 19:45:16 +0900 | [diff] [blame] | 7 | #include <assert.h> |
| 8 | |
Masahiro Yamada | ca8b80e | 2019-07-02 22:03:16 +0900 | [diff] [blame] | 9 | #include <drivers/console.h> |
| 10 | #include <errno.h> |
| 11 | #include <lib/mmio.h> |
| 12 | #include <plat/common/platform.h> |
| 13 | |
| 14 | #include "uniphier.h" |
| 15 | #include "uniphier_console.h" |
| 16 | |
Masahiro Yamada | ca8b80e | 2019-07-02 22:03:16 +0900 | [diff] [blame] | 17 | #define UNIPHIER_UART_OFFSET 0x100 |
Masahiro Yamada | ca971b5 | 2020-02-03 19:45:16 +0900 | [diff] [blame] | 18 | #define UNIPHIER_UART_NR_PORTS 4 |
Masahiro Yamada | ca8b80e | 2019-07-02 22:03:16 +0900 | [diff] [blame] | 19 | |
Masahiro Yamada | ca8b80e | 2019-07-02 22:03:16 +0900 | [diff] [blame] | 20 | /* These callbacks are implemented in assembly to use crash_console_helpers.S */ |
| 21 | int uniphier_console_putc(int character, struct console *console); |
| 22 | int uniphier_console_getc(struct console *console); |
Jimmy Brisson | 39f9eee | 2020-08-05 13:44:05 -0500 | [diff] [blame] | 23 | void uniphier_console_flush(struct console *console); |
Masahiro Yamada | ca8b80e | 2019-07-02 22:03:16 +0900 | [diff] [blame] | 24 | |
Andre Przywara | 3380631 | 2020-01-25 00:58:35 +0000 | [diff] [blame] | 25 | static console_t uniphier_console = { |
| 26 | .flags = CONSOLE_FLAG_BOOT | |
Masahiro Yamada | ca8b80e | 2019-07-02 22:03:16 +0900 | [diff] [blame] | 27 | #if DEBUG |
Andre Przywara | 3380631 | 2020-01-25 00:58:35 +0000 | [diff] [blame] | 28 | CONSOLE_FLAG_RUNTIME | |
Masahiro Yamada | ca8b80e | 2019-07-02 22:03:16 +0900 | [diff] [blame] | 29 | #endif |
Andre Przywara | 3380631 | 2020-01-25 00:58:35 +0000 | [diff] [blame] | 30 | CONSOLE_FLAG_CRASH | |
| 31 | CONSOLE_FLAG_TRANSLATE_CRLF, |
| 32 | .putc = uniphier_console_putc, |
Sandrine Bailleux | f57e203 | 2023-10-11 08:38:00 +0200 | [diff] [blame] | 33 | #if ENABLE_CONSOLE_GETC |
Andre Przywara | 3380631 | 2020-01-25 00:58:35 +0000 | [diff] [blame] | 34 | .getc = uniphier_console_getc, |
Sandrine Bailleux | f57e203 | 2023-10-11 08:38:00 +0200 | [diff] [blame] | 35 | #endif |
Andre Przywara | 3380631 | 2020-01-25 00:58:35 +0000 | [diff] [blame] | 36 | .flush = uniphier_console_flush, |
Masahiro Yamada | ca8b80e | 2019-07-02 22:03:16 +0900 | [diff] [blame] | 37 | }; |
| 38 | |
Masahiro Yamada | ca971b5 | 2020-02-03 19:45:16 +0900 | [diff] [blame] | 39 | static const uintptr_t uniphier_uart_base[] = { |
| 40 | [UNIPHIER_SOC_LD11] = 0x54006800, |
| 41 | [UNIPHIER_SOC_LD20] = 0x54006800, |
| 42 | [UNIPHIER_SOC_PXS3] = 0x54006800, |
| 43 | }; |
| 44 | |
Masahiro Yamada | ca8b80e | 2019-07-02 22:03:16 +0900 | [diff] [blame] | 45 | /* |
| 46 | * There are 4 UART ports available on this platform. By default, we want to |
| 47 | * use the same one as used in the previous firmware stage. |
| 48 | */ |
Masahiro Yamada | ca971b5 | 2020-02-03 19:45:16 +0900 | [diff] [blame] | 49 | static uintptr_t uniphier_console_get_base(unsigned int soc) |
Masahiro Yamada | ca8b80e | 2019-07-02 22:03:16 +0900 | [diff] [blame] | 50 | { |
Masahiro Yamada | ca971b5 | 2020-02-03 19:45:16 +0900 | [diff] [blame] | 51 | uintptr_t base, end; |
Masahiro Yamada | ca8b80e | 2019-07-02 22:03:16 +0900 | [diff] [blame] | 52 | uint32_t div; |
| 53 | |
Masahiro Yamada | ca971b5 | 2020-02-03 19:45:16 +0900 | [diff] [blame] | 54 | assert(soc < ARRAY_SIZE(uniphier_uart_base)); |
| 55 | base = uniphier_uart_base[soc]; |
| 56 | end = base + UNIPHIER_UART_OFFSET * UNIPHIER_UART_NR_PORTS; |
| 57 | |
| 58 | while (base < end) { |
Masahiro Yamada | ca8b80e | 2019-07-02 22:03:16 +0900 | [diff] [blame] | 59 | div = mmio_read_32(base + UNIPHIER_UART_DLR); |
| 60 | if (div) |
| 61 | return base; |
| 62 | base += UNIPHIER_UART_OFFSET; |
| 63 | } |
| 64 | |
| 65 | return 0; |
| 66 | } |
| 67 | |
| 68 | static void uniphier_console_init(uintptr_t base) |
| 69 | { |
| 70 | mmio_write_32(base + UNIPHIER_UART_FCR, UNIPHIER_UART_FCR_ENABLE_FIFO); |
| 71 | mmio_write_32(base + UNIPHIER_UART_LCR_MCR, |
| 72 | UNIPHIER_UART_LCR_WLEN8 << 8); |
| 73 | } |
| 74 | |
Masahiro Yamada | ca971b5 | 2020-02-03 19:45:16 +0900 | [diff] [blame] | 75 | void uniphier_console_setup(unsigned int soc) |
Masahiro Yamada | ca8b80e | 2019-07-02 22:03:16 +0900 | [diff] [blame] | 76 | { |
| 77 | uintptr_t base; |
| 78 | |
Masahiro Yamada | ca971b5 | 2020-02-03 19:45:16 +0900 | [diff] [blame] | 79 | base = uniphier_console_get_base(soc); |
Masahiro Yamada | ca8b80e | 2019-07-02 22:03:16 +0900 | [diff] [blame] | 80 | if (!base) |
| 81 | plat_error_handler(-EINVAL); |
| 82 | |
| 83 | uniphier_console.base = base; |
Andre Przywara | 3380631 | 2020-01-25 00:58:35 +0000 | [diff] [blame] | 84 | console_register(&uniphier_console); |
Masahiro Yamada | ca8b80e | 2019-07-02 22:03:16 +0900 | [diff] [blame] | 85 | |
| 86 | /* |
| 87 | * The hardware might be still printing characters queued up in the |
| 88 | * previous firmware stage. Make sure the transmitter is empty before |
| 89 | * any initialization. Otherwise, the console might get corrupted. |
| 90 | */ |
| 91 | console_flush(); |
| 92 | |
| 93 | uniphier_console_init(base); |
| 94 | } |