blob: 841c1fdef3a91ba114a3aac4efa96b6dbcb4489e [file] [log] [blame]
Venkatesh Yadav Abbarapuf80014d2020-11-27 02:58:24 -07001/*
Maheedhar Bollapalli1f83d612024-09-23 09:04:23 +00002 * Copyright (c) 2015-2022, Xilinx Inc.
3 * Copyright (c) 2022-2024, Advanced Micro Devices, Inc. All rights reserved.
Venkatesh Yadav Abbarapuf80014d2020-11-27 02:58:24 -07004 * Written by Michal Simek.
5 *
6 * SPDX-License-Identifier: BSD-3-Clause
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
10 *
11 * Redistributions of source code must retain the above copyright notice, this
12 * list of conditions and the following disclaimer.
13 *
14 * Redistributions in binary form must reproduce the above copyright notice,
15 * this list of conditions and the following disclaimer in the documentation
16 * and/or other materials provided with the distribution.
17 *
18 * Neither the name of ARM nor the names of its contributors may be used
19 * to endorse or promote products derived from this software without specific
20 * prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
26 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 */
34
35#include <errno.h>
36#include <stddef.h>
37#include <arch_helpers.h>
38#include <drivers/arm/dcc.h>
39#include <drivers/console.h>
40#include <drivers/delay_timer.h>
41#include <lib/mmio.h>
42
43/* DCC Status Bits */
44#define DCC_STATUS_RX BIT(30)
45#define DCC_STATUS_TX BIT(29)
46#define TIMEOUT_COUNT_US U(0x10624)
47
48struct dcc_console {
Maheedhar Bollapalli1f83d612024-09-23 09:04:23 +000049 console_t console;
Venkatesh Yadav Abbarapuf80014d2020-11-27 02:58:24 -070050};
51
52static inline uint32_t __dcc_getstatus(void)
53{
54 return read_mdccsr_el0();
55}
56
Sandrine Bailleuxf57e2032023-10-11 08:38:00 +020057#if ENABLE_CONSOLE_GETC
Venkatesh Yadav Abbarapuf80014d2020-11-27 02:58:24 -070058static inline char __dcc_getchar(void)
59{
60 char c;
61
62 c = read_dbgdtrrx_el0();
63
64 return c;
65}
Sandrine Bailleuxf57e2032023-10-11 08:38:00 +020066#endif
Venkatesh Yadav Abbarapuf80014d2020-11-27 02:58:24 -070067
68static inline void __dcc_putchar(char c)
69{
70 /*
71 * The typecast is to make absolutely certain that 'c' is
72 * zero-extended.
73 */
74 write_dbgdtrtx_el0((unsigned char)c);
75}
76
77static int32_t dcc_status_timeout(uint32_t mask)
78{
79 const unsigned int timeout_count = TIMEOUT_COUNT_US;
80 uint64_t timeout;
81 unsigned int status;
82
83 timeout = timeout_init_us(timeout_count);
84
85 do {
86 status = (__dcc_getstatus() & mask);
87 if (timeout_elapsed(timeout)) {
88 return -ETIMEDOUT;
89 }
90 } while ((status != 0U));
91
92 return 0;
93}
94
95static int32_t dcc_console_putc(int32_t ch, struct console *console)
96{
97 unsigned int status;
98
99 status = dcc_status_timeout(DCC_STATUS_TX);
100 if (status != 0U) {
101 return status;
102 }
103 __dcc_putchar(ch);
104
105 return ch;
106}
107
Sandrine Bailleuxf57e2032023-10-11 08:38:00 +0200108#if ENABLE_CONSOLE_GETC
Venkatesh Yadav Abbarapuf80014d2020-11-27 02:58:24 -0700109static int32_t dcc_console_getc(struct console *console)
110{
111 unsigned int status;
112
113 status = dcc_status_timeout(DCC_STATUS_RX);
114 if (status != 0U) {
115 return status;
116 }
117
118 return __dcc_getchar();
119}
Sandrine Bailleuxf57e2032023-10-11 08:38:00 +0200120#endif
Venkatesh Yadav Abbarapuf80014d2020-11-27 02:58:24 -0700121
Venkatesh Yadav Abbarapuf80014d2020-11-27 02:58:24 -0700122/**
123 * dcc_console_flush() - Function to force a write of all buffered data
124 * that hasn't been output.
125 * @console Console struct
126 *
127 */
128static void dcc_console_flush(struct console *console)
129{
130 unsigned int status;
131
132 status = dcc_status_timeout(DCC_STATUS_TX);
133 if (status != 0U) {
134 return;
135 }
136}
137
138static struct dcc_console dcc_console = {
139 .console = {
140 .flags = CONSOLE_FLAG_BOOT |
Michal Simek78ba23d2023-09-18 10:16:25 +0200141 CONSOLE_FLAG_RUNTIME |
142 CONSOLE_FLAG_CRASH,
Venkatesh Yadav Abbarapuf80014d2020-11-27 02:58:24 -0700143 .putc = dcc_console_putc,
Sandrine Bailleuxf57e2032023-10-11 08:38:00 +0200144#if ENABLE_CONSOLE_GETC
Venkatesh Yadav Abbarapuf80014d2020-11-27 02:58:24 -0700145 .getc = dcc_console_getc,
Sandrine Bailleuxf57e2032023-10-11 08:38:00 +0200146#endif
Venkatesh Yadav Abbarapuf80014d2020-11-27 02:58:24 -0700147 .flush = dcc_console_flush,
148 },
149};
150
Maheedhar Bollapalli1f83d612024-09-23 09:04:23 +0000151int console_dcc_register(console_t *console)
Venkatesh Yadav Abbarapuf80014d2020-11-27 02:58:24 -0700152{
Maheedhar Bollapalli1f83d612024-09-23 09:04:23 +0000153 memcpy(console, &dcc_console.console, sizeof(console_t));
154 return console_register(console);
Venkatesh Yadav Abbarapuf80014d2020-11-27 02:58:24 -0700155}
Prasad Kummarif006e952023-09-19 22:35:08 +0530156
Maheedhar Bollapalli1f83d612024-09-23 09:04:23 +0000157void console_dcc_unregister(console_t *console)
Prasad Kummarif006e952023-09-19 22:35:08 +0530158{
Maheedhar Bollapalli1f83d612024-09-23 09:04:23 +0000159 dcc_console_flush(console);
160 (void)console_unregister(console);
Prasad Kummarif006e952023-09-19 22:35:08 +0530161}