|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: BSD-3-Clause |
| 3 | + * |
| 4 | + * Copyright © 2023 Keith Packard |
| 5 | + * |
| 6 | + * Redistribution and use in source and binary forms, with or without |
| 7 | + * modification, are permitted provided that the following conditions |
| 8 | + * are met: |
| 9 | + * |
| 10 | + * 1. Redistributions of source code must retain the above copyright |
| 11 | + * notice, this list of conditions and the following disclaimer. |
| 12 | + * |
| 13 | + * 2. Redistributions in binary form must reproduce the above |
| 14 | + * copyright notice, this list of conditions and the following |
| 15 | + * disclaimer in the documentation and/or other materials provided |
| 16 | + * with the distribution. |
| 17 | + * |
| 18 | + * 3. Neither the name of the copyright holder nor the names of its |
| 19 | + * contributors may be used to endorse or promote products derived |
| 20 | + * from this software without specific prior written permission. |
| 21 | + * |
| 22 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 23 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 24 | + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 25 | + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 26 | + * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
| 27 | + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 28 | + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 29 | + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 30 | + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 31 | + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 32 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 33 | + * OF THE POSSIBILITY OF SUCH DAMAGE. |
| 34 | + */ |
| 35 | + |
| 36 | +#include "or1k_semihost.h" |
| 37 | + |
| 38 | +typedef volatile uint8_t vuint8_t; |
| 39 | + |
| 40 | +struct uart_16550 { |
| 41 | + vuint8_t data; |
| 42 | + vuint8_t ier; |
| 43 | + vuint8_t iir; |
| 44 | + vuint8_t lcr; |
| 45 | + |
| 46 | + vuint8_t mcr; |
| 47 | + vuint8_t lsr; |
| 48 | + vuint8_t msr; |
| 49 | + vuint8_t scr; |
| 50 | +}; |
| 51 | + |
| 52 | +/* openrisc virt serial port */ |
| 53 | +#define uart (*((struct uart_16550 *) 0x90000000)) |
| 54 | + |
| 55 | +#define UART_LSR_FIFOE 0x80 /* Fifo error */ |
| 56 | +#define UART_LSR_TEMT 0x40 /* Transmitter empty */ |
| 57 | +#define UART_LSR_THRE 0x20 /* Transmit-hold-register empty */ |
| 58 | +#define UART_LSR_BI 0x10 /* Break interrupt indicator */ |
| 59 | +#define UART_LSR_FE 0x08 /* Frame error indicator */ |
| 60 | +#define UART_LSR_PE 0x04 /* Parity error indicator */ |
| 61 | +#define UART_LSR_OE 0x02 /* Overrun error indicator */ |
| 62 | +#define UART_LSR_DR 0x01 /* Receiver data ready */ |
| 63 | + |
| 64 | +int |
| 65 | +or1k_putc(char c, FILE *file) |
| 66 | +{ |
| 67 | + (void) file; |
| 68 | + while ((uart.lsr & (UART_LSR_TEMT|UART_LSR_THRE)) != (UART_LSR_TEMT|UART_LSR_THRE)) |
| 69 | + ; |
| 70 | + uart.data = (uint8_t) c; |
| 71 | + return (unsigned char) c; |
| 72 | +} |
| 73 | + |
| 74 | +#ifdef TINY_STDIO |
| 75 | + |
| 76 | +static int |
| 77 | +or1k_getc(FILE *file) |
| 78 | +{ |
| 79 | + (void) file; |
| 80 | + return EOF; |
| 81 | +} |
| 82 | + |
| 83 | +static FILE __stdio = FDEV_SETUP_STREAM(or1k_putc, or1k_getc, NULL, _FDEV_SETUP_RW); |
| 84 | + |
| 85 | +#ifdef __strong_reference |
| 86 | +#define STDIO_ALIAS(x) __strong_reference(stdin, x); |
| 87 | +#else |
| 88 | +#define STDIO_ALIAS(x) FILE *const x = &__stdio; |
| 89 | +#endif |
| 90 | + |
| 91 | +FILE *const stdin = &__stdio; |
| 92 | +STDIO_ALIAS(stdout); |
| 93 | +STDIO_ALIAS(stderr); |
| 94 | + |
| 95 | +#endif |
0 commit comments