1 /*
2  * Copyright (c) 2012-2018 LK Trusty Authors. 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 <arch/x86.h>
25 #include <arch/x86/mmu.h>
26 #include <err.h>
27 #include <kernel/mutex.h>
28 
29 #define UART_REGISTER_THR 0 /* WO Transmit Holding Register */
30 #define UART_REGISTER_LSR 5 /* R/W Line Status Register */
31 
32 typedef union {
33     struct {
34         uint8_t dr : 1;
35         uint8_t oe : 1;
36         uint8_t pe : 1;
37         uint8_t fe : 1;
38         uint8_t bi : 1;
39         uint8_t thre : 1;
40         uint8_t temt : 1;
41         uint8_t fifoe : 1;
42     } bits;
43     uint8_t data;
44 } uart_lsr_t;
45 
46 static spin_lock_t dputc_spin_lock = 0;
47 
serial_io_get_reg(uint64_t base_addr,uint32_t reg_id)48 static uint8_t serial_io_get_reg(uint64_t base_addr, uint32_t reg_id) {
49     return inp((uint16_t)base_addr + (uint16_t)reg_id);
50 }
51 
serial_io_set_reg(uint64_t base_addr,uint32_t reg_id,uint8_t val)52 static void serial_io_set_reg(uint64_t base_addr,
53                               uint32_t reg_id,
54                               uint8_t val) {
55     outp((uint16_t)base_addr + (uint16_t)reg_id, val);
56 }
57 
uart_putc(char c)58 static void uart_putc(char c) {
59     uart_lsr_t lsr;
60 
61     do {
62         lsr.data = serial_io_get_reg(TARGET_SERIAL_IO_BASE, UART_REGISTER_LSR);
63     } while (!lsr.bits.thre);
64 
65     serial_io_set_reg(TARGET_SERIAL_IO_BASE, UART_REGISTER_THR, c);
66 }
67 
platform_dputc(char c)68 void platform_dputc(char c) {
69     spin_lock_saved_state_t state;
70     spin_lock_save(&dputc_spin_lock, &state, SPIN_LOCK_FLAG_INTERRUPTS);
71     uart_putc(c);
72     spin_unlock_restore(&dputc_spin_lock, state, SPIN_LOCK_FLAG_INTERRUPTS);
73 }
74 
75 /* Do not accept any input */
platform_dgetc(char * c,bool wait)76 int platform_dgetc(char* c, bool wait) {
77     return ERR_NOT_SUPPORTED;
78 }
79