rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2015 Travis Geiselbrecht |
| 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 | #include <reg.h> |
| 24 | #include <trace.h> |
| 25 | #include <lib/cbuf.h> |
| 26 | #include <lk/init.h> |
| 27 | #include <kernel/thread.h> |
| 28 | #include <platform.h> |
| 29 | #include <platform/interrupts.h> |
| 30 | #include <sys/types.h> |
| 31 | #include <target/microblaze-config.h> |
| 32 | |
| 33 | #define LOCAL_TRACE 0 |
| 34 | |
| 35 | #define R_RX 0 |
| 36 | #define R_TX 1 |
| 37 | #define R_STATUS 2 |
| 38 | #define R_CTRL 3 |
| 39 | #define R_MAX 4 |
| 40 | |
| 41 | #define STATUS_RXVALID 0x01 |
| 42 | #define STATUS_RXFULL 0x02 |
| 43 | #define STATUS_TXEMPTY 0x04 |
| 44 | #define STATUS_TXFULL 0x08 |
| 45 | #define STATUS_IE 0x10 |
| 46 | #define STATUS_OVERRUN 0x20 |
| 47 | #define STATUS_FRAME 0x40 |
| 48 | #define STATUS_PARITY 0x80 |
| 49 | |
| 50 | #define CONTROL_RST_TX 0x01 |
| 51 | #define CONTROL_RST_RX 0x02 |
| 52 | #define CONTROL_IE 0x10 |
| 53 | |
| 54 | #define UART_REG(reg) (*REG32(UARTLITE_BASEADDR + (reg) * 4)) |
| 55 | |
| 56 | #define RXBUF_SIZE 128 |
| 57 | static cbuf_t uart_rx_buf; |
| 58 | |
| 59 | void uartlite_putc(char c) |
| 60 | { |
| 61 | while (UART_REG(R_STATUS) & STATUS_TXFULL) |
| 62 | ; |
| 63 | UART_REG(R_TX) = c; |
| 64 | } |
| 65 | |
| 66 | int uartlite_getc(bool wait) |
| 67 | { |
| 68 | #if 0 |
| 69 | char c; |
| 70 | if (cbuf_read_char(&uart_rx_buf, &c, wait) == 1) |
| 71 | return c; |
| 72 | #else |
| 73 | do { |
| 74 | if (UART_REG(R_STATUS) & STATUS_RXVALID) { |
| 75 | char c = UART_REG(R_RX); |
| 76 | return c; |
| 77 | } |
| 78 | } while (wait); |
| 79 | #endif |
| 80 | |
| 81 | return -1; |
| 82 | } |
| 83 | |
| 84 | enum handler_return uartlite_irq(void *arg) |
| 85 | { |
| 86 | bool resched = false; |
| 87 | |
| 88 | /* while receive fifo not empty, read a char */ |
| 89 | while (UART_REG(R_STATUS) & STATUS_RXVALID) { |
| 90 | char c = UART_REG(R_RX); |
| 91 | cbuf_write_char(&uart_rx_buf, c, false); |
| 92 | |
| 93 | resched = true; |
| 94 | } |
| 95 | |
| 96 | return resched ? INT_RESCHEDULE : INT_NO_RESCHEDULE; |
| 97 | } |
| 98 | |
| 99 | static void uartlite_init(uint level) |
| 100 | { |
| 101 | TRACE; |
| 102 | |
| 103 | //UART_REG(R_CTRL) = CONTROL_RST_TX | CONTROL_RST_RX; |
| 104 | // UART_REG(R_CTRL) |= CONTROL_IE; |
| 105 | |
| 106 | cbuf_initialize(&uart_rx_buf, RXBUF_SIZE); |
| 107 | |
| 108 | // register_int_handler(UARTLITE_IRQ, uartlite_irq, NULL); |
| 109 | // unmask_interrupt(UARTLITE_IRQ); |
| 110 | } |
| 111 | |
| 112 | LK_INIT_HOOK(uartlite, uartlite_init, LK_INIT_LEVEL_PLATFORM); |
| 113 | |