1 /*
2 * Copyright (c) 2015 Google Inc. All rights reserved
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files
6 * (the "Software"), to deal in the Software without restriction,
7 * including without limitation the rights to use, copy, modify, merge,
8 * publish, distribute, sublicense, and/or sell copies of the Software,
9 * and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23
24 #include <kernel/thread.h>
25 #include <kernel/vm.h>
26 #include <lib/device_tree/libfdt_helpers.h>
27 #include <lk/reg.h>
28 #include <lk/types.h>
29 #include <platform/debug.h>
30
31 #include "debug.h"
32
33 #if GENERIC_ARM64_DEBUG_SMC_DEBUG_PUTC
34 #include "smc.h"
35 #endif
36
37 #if GENERIC_ARM64_DEBUG_UART
38 enum uart_type {
39 UART_NONE,
40 UART_PL011,
41 UART_8250,
42 };
43
44 static uint8_t* uart_base;
45 static enum uart_type uart_type;
46
map_uart(paddr_t reg_paddr,enum uart_type new_uart_type)47 static void map_uart(paddr_t reg_paddr, enum uart_type new_uart_type) {
48 int ret;
49 void* page_vaddr;
50
51 ASSERT(!uart_base);
52
53 paddr_t reg_pbase = round_down(reg_paddr, PAGE_SIZE);
54 ret = vmm_alloc_physical(
55 vmm_get_kernel_aspace(), "uart", PAGE_SIZE, &page_vaddr, 0,
56 reg_pbase, 0,
57 ARCH_MMU_FLAG_PERM_NO_EXECUTE | ARCH_MMU_FLAG_UNCACHED_DEVICE);
58 if (ret) {
59 return;
60 }
61 uart_type = new_uart_type;
62 uart_base = (uint8_t*)page_vaddr + (reg_paddr - reg_pbase);
63 }
64
generic_arm64_setup_uart(void * fdt)65 void generic_arm64_setup_uart(void* fdt) {
66 enum uart_type uart_type;
67 int fdt_chosen_offset = fdt_path_offset(fdt, "/chosen");
68 int fdt_stdout_path_len;
69 const char* fdt_stdout_path = fdt_getprop(
70 fdt, fdt_chosen_offset, "stdout-path", &fdt_stdout_path_len);
71 if (!fdt_stdout_path) {
72 return;
73 }
74 int fdt_stdout_offset = fdt_path_offset_namelen(fdt, fdt_stdout_path,
75 fdt_stdout_path_len - 1);
76
77 if (!fdt_node_check_compatible(fdt, fdt_stdout_offset, "arm,pl011")) {
78 uart_type = UART_PL011;
79 } else if (!fdt_node_check_compatible(fdt, fdt_stdout_offset, "ns8250") ||
80 !fdt_node_check_compatible(fdt, fdt_stdout_offset, "ns16550a")) {
81 uart_type = UART_8250;
82 } else {
83 return;
84 }
85
86 paddr_t uart_reg;
87 if (fdt_helper_get_reg(fdt, fdt_stdout_offset, 0, &uart_reg, NULL)) {
88 return;
89 }
90
91 map_uart(uart_reg, uart_type);
92 }
93 #endif
94
platform_dputc(char c)95 void platform_dputc(char c) {
96 #if GENERIC_ARM64_DEBUG_SMC_DEBUG_PUTC
97 generic_arm64_smc(SMC_FC_DEBUG_PUTC, (unsigned long)c, 0, 0);
98 #elif GENERIC_ARM64_DEBUG_UART
99 if (!uart_base) {
100 return;
101 }
102 if (uart_type == UART_8250) {
103 while (!(readb(uart_base + 5) & (1U << 5))) {
104 }
105 writeb(c, uart_base + 0);
106 } else if (uart_type == UART_PL011) {
107 while ((*REG16(uart_base + 0x018) & (1U << 5))) {
108 }
109 *REG16(uart_base + 0x000) = c;
110 }
111 #else
112 #error Unsupported GENERIC_ARM64_DEBUG mode
113 #endif
114 }
115
platform_dgetc(char * c,bool wait)116 int platform_dgetc(char* c, bool wait) {
117 int ret = -1;
118
119 while (wait)
120 thread_sleep(100);
121
122 return ret;
123 }
124