File indexing completed on 2025-05-11 08:22:48
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026 #include <libchip/serial.h>
0027 #include <libchip/ns16550.h>
0028
0029 #include <rtems/bspIo.h>
0030
0031 #include <bsp.h>
0032 #include <bsp/irq.h>
0033 #include <bsp/uart-output-char.h>
0034
0035 #define CONSOLE_UART_THR (*(volatile unsigned int *)BSP_CONSOLE_UART_BASE)
0036 #define CONSOLE_UART_RHR (*(volatile unsigned int *)BSP_CONSOLE_UART_BASE)
0037 #define CONSOLE_UART_LSR (*(volatile unsigned int *)(BSP_CONSOLE_UART_BASE+0x14))
0038 #define CONSOLE_SYSC (*(volatile uint32_t *) (BSP_CONSOLE_UART_BASE + 0x54))
0039 #define CONSOLE_SYSS (*(volatile uint32_t *) (BSP_CONSOLE_UART_BASE + 0x58))
0040
0041 #define TX_FIFO_E (1<<5)
0042 #define RX_FIFO_E (1<<0)
0043
0044 static uint8_t beagle_uart_get_register(uintptr_t addr, uint8_t i)
0045 {
0046 uint8_t v;
0047 volatile uint32_t *reg_r = (volatile uint32_t *) addr + i;
0048
0049 if(reg_r == (uint32_t*) BSP_CONSOLE_UART_BASE ) {
0050
0051 if(!(CONSOLE_UART_LSR & 0x01)) {
0052 return 0;
0053 }
0054 }
0055
0056 v = (uint8_t) *reg_r;
0057
0058 return v;
0059 }
0060
0061 static void beagle_uart_set_register(uintptr_t addr, uint8_t i, uint8_t val)
0062 {
0063 volatile uint32_t *reg = (volatile uint32_t *) addr;
0064
0065 reg [i] = val;
0066 }
0067
0068 console_tbl Console_Configuration_Ports [] = {
0069 {
0070 .sDeviceName = "/dev/ttyS0",
0071 .deviceType = SERIAL_NS16550,
0072 #if CONSOLE_POLLED
0073 .pDeviceFns = &ns16550_fns_polled,
0074 #else
0075 .pDeviceFns = &ns16550_fns,
0076 #endif
0077 .ulMargin = 16,
0078 .ulHysteresis = 8,
0079 .pDeviceParams = (void *) CONSOLE_BAUD,
0080 .ulCtrlPort1 = BSP_CONSOLE_UART_BASE,
0081 .ulDataPort = BSP_CONSOLE_UART_BASE,
0082 .ulIntVector = BSP_CONSOLE_UART_IRQ,
0083 .getRegister = beagle_uart_get_register,
0084 .setRegister = beagle_uart_set_register,
0085 .ulClock = UART_CLOCK,
0086 },
0087 };
0088
0089 unsigned long Console_Configuration_Count = 1;
0090
0091 static int init_needed = 1;
0092
0093 static void beagle_console_init(void)
0094 {
0095 if(init_needed) {
0096 const uint32_t div = UART_CLOCK / 16 / CONSOLE_BAUD;
0097 CONSOLE_SYSC = 2;
0098 while ((CONSOLE_SYSS & 1) == 0)
0099 ;
0100 if ((CONSOLE_LSR & (CONSOLE_LSR_THRE | CONSOLE_LSR_TEMT)) == CONSOLE_LSR_THRE) {
0101 CONSOLE_LCR = 0x83;
0102 CONSOLE_DLL = div;
0103 CONSOLE_DLM = (div >> 8) & 0xff;
0104 CONSOLE_LCR = 0x03;
0105 CONSOLE_ACR = 0x00;
0106 }
0107
0108 while ((CONSOLE_LSR & CONSOLE_LSR_TEMT) == 0)
0109 ;
0110
0111 CONSOLE_LCR = 0x80 | 0x03;
0112 CONSOLE_DLL = 0x00;
0113 CONSOLE_DLM = 0x00;
0114 CONSOLE_LCR = 0x03;
0115 CONSOLE_MCR = 0x03;
0116 CONSOLE_FCR = 0x07;
0117 CONSOLE_LCR = 0x83;
0118 CONSOLE_DLL = div;
0119 CONSOLE_DLM = (div >> 8) & 0xff;
0120 CONSOLE_LCR = 0x03;
0121 CONSOLE_ACR = 0x00;
0122 init_needed = 0;
0123 }
0124 }
0125
0126 #define CONSOLE_THR8 (*(volatile uint8_t *) (BSP_CONSOLE_UART_BASE + 0x00))
0127
0128 static void uart_write_polled( char c )
0129 {
0130 if(init_needed) beagle_console_init();
0131
0132 while( ( CONSOLE_LSR & TX_FIFO_E ) == 0 )
0133 ;
0134 CONSOLE_THR8 = c;
0135 }
0136
0137 static void _BSP_put_char( char c ) {
0138 uart_write_polled( c );
0139 }
0140
0141 static int _BSP_get_char(void)
0142 {
0143 if ((CONSOLE_LSR & CONSOLE_LSR_RDR) != 0) {
0144 return CONSOLE_RBR;
0145 } else {
0146 return -1;
0147 }
0148 }
0149
0150 BSP_output_char_function_type BSP_output_char = _BSP_put_char;
0151
0152 BSP_polling_getchar_function_type BSP_poll_char = _BSP_get_char;