blob: 9fda26e938c407d50d02cc76ce9ef5a92c48ff16 [file] [log] [blame]
Masahiro Yamadaca8b80e2019-07-02 22:03:16 +09001/*
Masahiro Yamadaca971b52020-02-03 19:45:16 +09002 * Copyright (c) 2019-2020, Socionext Inc. All rights reserved.
Masahiro Yamadaca8b80e2019-07-02 22:03:16 +09003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
Masahiro Yamadaca971b52020-02-03 19:45:16 +09007#include <assert.h>
8
Masahiro Yamadaca8b80e2019-07-02 22:03:16 +09009#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 Yamadaca8b80e2019-07-02 22:03:16 +090017#define UNIPHIER_UART_OFFSET 0x100
Masahiro Yamadaca971b52020-02-03 19:45:16 +090018#define UNIPHIER_UART_NR_PORTS 4
Masahiro Yamadaca8b80e2019-07-02 22:03:16 +090019
Masahiro Yamadaca8b80e2019-07-02 22:03:16 +090020/* These callbacks are implemented in assembly to use crash_console_helpers.S */
21int uniphier_console_putc(int character, struct console *console);
22int uniphier_console_getc(struct console *console);
Jimmy Brisson39f9eee2020-08-05 13:44:05 -050023void uniphier_console_flush(struct console *console);
Masahiro Yamadaca8b80e2019-07-02 22:03:16 +090024
Andre Przywara33806312020-01-25 00:58:35 +000025static console_t uniphier_console = {
26 .flags = CONSOLE_FLAG_BOOT |
Masahiro Yamadaca8b80e2019-07-02 22:03:16 +090027#if DEBUG
Andre Przywara33806312020-01-25 00:58:35 +000028 CONSOLE_FLAG_RUNTIME |
Masahiro Yamadaca8b80e2019-07-02 22:03:16 +090029#endif
Andre Przywara33806312020-01-25 00:58:35 +000030 CONSOLE_FLAG_CRASH |
31 CONSOLE_FLAG_TRANSLATE_CRLF,
32 .putc = uniphier_console_putc,
33 .getc = uniphier_console_getc,
34 .flush = uniphier_console_flush,
Masahiro Yamadaca8b80e2019-07-02 22:03:16 +090035};
36
Masahiro Yamadaca971b52020-02-03 19:45:16 +090037static const uintptr_t uniphier_uart_base[] = {
38 [UNIPHIER_SOC_LD11] = 0x54006800,
39 [UNIPHIER_SOC_LD20] = 0x54006800,
40 [UNIPHIER_SOC_PXS3] = 0x54006800,
41};
42
Masahiro Yamadaca8b80e2019-07-02 22:03:16 +090043/*
44 * There are 4 UART ports available on this platform. By default, we want to
45 * use the same one as used in the previous firmware stage.
46 */
Masahiro Yamadaca971b52020-02-03 19:45:16 +090047static uintptr_t uniphier_console_get_base(unsigned int soc)
Masahiro Yamadaca8b80e2019-07-02 22:03:16 +090048{
Masahiro Yamadaca971b52020-02-03 19:45:16 +090049 uintptr_t base, end;
Masahiro Yamadaca8b80e2019-07-02 22:03:16 +090050 uint32_t div;
51
Masahiro Yamadaca971b52020-02-03 19:45:16 +090052 assert(soc < ARRAY_SIZE(uniphier_uart_base));
53 base = uniphier_uart_base[soc];
54 end = base + UNIPHIER_UART_OFFSET * UNIPHIER_UART_NR_PORTS;
55
56 while (base < end) {
Masahiro Yamadaca8b80e2019-07-02 22:03:16 +090057 div = mmio_read_32(base + UNIPHIER_UART_DLR);
58 if (div)
59 return base;
60 base += UNIPHIER_UART_OFFSET;
61 }
62
63 return 0;
64}
65
66static void uniphier_console_init(uintptr_t base)
67{
68 mmio_write_32(base + UNIPHIER_UART_FCR, UNIPHIER_UART_FCR_ENABLE_FIFO);
69 mmio_write_32(base + UNIPHIER_UART_LCR_MCR,
70 UNIPHIER_UART_LCR_WLEN8 << 8);
71}
72
Masahiro Yamadaca971b52020-02-03 19:45:16 +090073void uniphier_console_setup(unsigned int soc)
Masahiro Yamadaca8b80e2019-07-02 22:03:16 +090074{
75 uintptr_t base;
76
Masahiro Yamadaca971b52020-02-03 19:45:16 +090077 base = uniphier_console_get_base(soc);
Masahiro Yamadaca8b80e2019-07-02 22:03:16 +090078 if (!base)
79 plat_error_handler(-EINVAL);
80
81 uniphier_console.base = base;
Andre Przywara33806312020-01-25 00:58:35 +000082 console_register(&uniphier_console);
Masahiro Yamadaca8b80e2019-07-02 22:03:16 +090083
84 /*
85 * The hardware might be still printing characters queued up in the
86 * previous firmware stage. Make sure the transmitter is empty before
87 * any initialization. Otherwise, the console might get corrupted.
88 */
89 console_flush();
90
91 uniphier_console_init(base);
92}