Venkatesh Yadav Abbarapu | f80014d | 2020-11-27 02:58:24 -0700 | [diff] [blame] | 1 | /* |
Maheedhar Bollapalli | 1f83d61 | 2024-09-23 09:04:23 +0000 | [diff] [blame] | 2 | * Copyright (c) 2015-2022, Xilinx Inc. |
| 3 | * Copyright (c) 2022-2024, Advanced Micro Devices, Inc. All rights reserved. |
Venkatesh Yadav Abbarapu | f80014d | 2020-11-27 02:58:24 -0700 | [diff] [blame] | 4 | * 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 | |
| 48 | struct dcc_console { |
Maheedhar Bollapalli | 1f83d61 | 2024-09-23 09:04:23 +0000 | [diff] [blame] | 49 | console_t console; |
Venkatesh Yadav Abbarapu | f80014d | 2020-11-27 02:58:24 -0700 | [diff] [blame] | 50 | }; |
| 51 | |
| 52 | static inline uint32_t __dcc_getstatus(void) |
| 53 | { |
| 54 | return read_mdccsr_el0(); |
| 55 | } |
| 56 | |
Sandrine Bailleux | f57e203 | 2023-10-11 08:38:00 +0200 | [diff] [blame] | 57 | #if ENABLE_CONSOLE_GETC |
Venkatesh Yadav Abbarapu | f80014d | 2020-11-27 02:58:24 -0700 | [diff] [blame] | 58 | static inline char __dcc_getchar(void) |
| 59 | { |
| 60 | char c; |
| 61 | |
| 62 | c = read_dbgdtrrx_el0(); |
| 63 | |
| 64 | return c; |
| 65 | } |
Sandrine Bailleux | f57e203 | 2023-10-11 08:38:00 +0200 | [diff] [blame] | 66 | #endif |
Venkatesh Yadav Abbarapu | f80014d | 2020-11-27 02:58:24 -0700 | [diff] [blame] | 67 | |
| 68 | static 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 | |
| 77 | static 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 | |
| 95 | static 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 Bailleux | f57e203 | 2023-10-11 08:38:00 +0200 | [diff] [blame] | 108 | #if ENABLE_CONSOLE_GETC |
Venkatesh Yadav Abbarapu | f80014d | 2020-11-27 02:58:24 -0700 | [diff] [blame] | 109 | static 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 Bailleux | f57e203 | 2023-10-11 08:38:00 +0200 | [diff] [blame] | 120 | #endif |
Venkatesh Yadav Abbarapu | f80014d | 2020-11-27 02:58:24 -0700 | [diff] [blame] | 121 | |
Venkatesh Yadav Abbarapu | f80014d | 2020-11-27 02:58:24 -0700 | [diff] [blame] | 122 | /** |
| 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 | */ |
| 128 | static 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 | |
| 138 | static struct dcc_console dcc_console = { |
| 139 | .console = { |
| 140 | .flags = CONSOLE_FLAG_BOOT | |
Michal Simek | 78ba23d | 2023-09-18 10:16:25 +0200 | [diff] [blame] | 141 | CONSOLE_FLAG_RUNTIME | |
| 142 | CONSOLE_FLAG_CRASH, |
Venkatesh Yadav Abbarapu | f80014d | 2020-11-27 02:58:24 -0700 | [diff] [blame] | 143 | .putc = dcc_console_putc, |
Sandrine Bailleux | f57e203 | 2023-10-11 08:38:00 +0200 | [diff] [blame] | 144 | #if ENABLE_CONSOLE_GETC |
Venkatesh Yadav Abbarapu | f80014d | 2020-11-27 02:58:24 -0700 | [diff] [blame] | 145 | .getc = dcc_console_getc, |
Sandrine Bailleux | f57e203 | 2023-10-11 08:38:00 +0200 | [diff] [blame] | 146 | #endif |
Venkatesh Yadav Abbarapu | f80014d | 2020-11-27 02:58:24 -0700 | [diff] [blame] | 147 | .flush = dcc_console_flush, |
| 148 | }, |
| 149 | }; |
| 150 | |
Maheedhar Bollapalli | 1f83d61 | 2024-09-23 09:04:23 +0000 | [diff] [blame] | 151 | int console_dcc_register(console_t *console) |
Venkatesh Yadav Abbarapu | f80014d | 2020-11-27 02:58:24 -0700 | [diff] [blame] | 152 | { |
Maheedhar Bollapalli | 1f83d61 | 2024-09-23 09:04:23 +0000 | [diff] [blame] | 153 | memcpy(console, &dcc_console.console, sizeof(console_t)); |
| 154 | return console_register(console); |
Venkatesh Yadav Abbarapu | f80014d | 2020-11-27 02:58:24 -0700 | [diff] [blame] | 155 | } |
Prasad Kummari | f006e95 | 2023-09-19 22:35:08 +0530 | [diff] [blame] | 156 | |
Maheedhar Bollapalli | 1f83d61 | 2024-09-23 09:04:23 +0000 | [diff] [blame] | 157 | void console_dcc_unregister(console_t *console) |
Prasad Kummari | f006e95 | 2023-09-19 22:35:08 +0530 | [diff] [blame] | 158 | { |
Maheedhar Bollapalli | 1f83d61 | 2024-09-23 09:04:23 +0000 | [diff] [blame] | 159 | dcc_console_flush(console); |
| 160 | (void)console_unregister(console); |
Prasad Kummari | f006e95 | 2023-09-19 22:35:08 +0530 | [diff] [blame] | 161 | } |