Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:22:48

0001 /**
0002  * @file
0003  *
0004  * @ingroup arm_beagle
0005  *
0006  * @brief Console configuration.
0007  */
0008 
0009 /*
0010  * Copyright (c) 2012 Claas Ziemke. All rights reserved.
0011  *
0012  *  Claas Ziemke
0013  *  Kernerstrasse 11
0014  *  70182 Stuttgart
0015  *  Germany
0016  *  <claas.ziemke@gmx.net>
0017  *
0018  * The license and distribution terms for this file may be
0019  * found in the file LICENSE in this distribution or at
0020  * http://www.rtems.org/license/LICENSE.
0021  *
0022  * Modified by Ben Gras <beng@shrike-systems.com> to make
0023  * interrupt-driven uart i/o work for beagleboards; beaglebone support added.
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 /* reading RHR */ ) {
0050     /* check there should be anything in the RHR before accessing it */
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  /* option to facilitate running the tests */
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,  /* 48MHz base clock */
0086     },
0087 };
0088 
0089 unsigned long Console_Configuration_Count = 1;
0090 
0091 static int init_needed = 1; // don't rely on bss being 0
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;