Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:23:51

0001 /**
0002  * @file
0003  *
0004  * @ingroup generic_or1k_uart
0005  *
0006  * @brief UART support.
0007  */
0008 
0009 /*
0010  * COPYRIGHT (c) 2014-2015 Hesham ALMatary <heshamelmatary@gmail.com>
0011  *
0012  * The license and distribution terms for this file may be
0013  * found in the file LICENSE in this distribution or at
0014  * http://www.rtems.org/license/LICENSE
0015  */
0016 
0017 #include <libchip/sersupp.h>
0018 #include <bsp/generic_or1k.h>
0019 #include <bsp.h>
0020 #include <bsp/irq.h>
0021 #include <bsp/uart.h>
0022 #include <rtems/score/isr.h>
0023 
0024 static void uart_initialize(int minor);
0025 static int  uart_first_open(int major, int minor, void *arg);
0026 static int  uart_last_close(int major, int minor, void *arg);
0027 static int  uart_read_polled(int minor);
0028 static ssize_t uart_write(int minor, const char *buf, size_t len);
0029 static void uart_write_polled(int minor, char c);
0030 static int  uart_set_attributes(int minor, const struct termios *t);
0031 
0032 #if 0
0033 /*
0034  *  These will be useful when the driver supports interrupt driven IO.
0035  */
0036 static rtems_vector_number uart_get_irq_number(const console_tbl *ct)
0037 {
0038   return ct->ulIntVector;
0039 }
0040 
0041 static uint32_t uart_get_baud(const console_tbl *ct)
0042 {
0043   return ct->ulClock;
0044 }
0045 #endif
0046 
0047 static void uart_set_baud(int baud)
0048 {
0049   uint16_t divisor = (OR1K_BSP_CLOCK_FREQ) / (16 * baud);
0050   OR1K_REG(OR1K_BSP_UART_REG_LINE_CTRL) =
0051     OR1K_BSP_UART_REG_LINE_CTRL_DLAB;
0052 
0053   OR1K_REG(OR1K_BSP_UART_REG_DEV_LATCH_LOW) = divisor & 0xff;
0054 
0055   OR1K_REG(OR1K_BSP_UART_REG_DEV_LATCH_HIGH) =
0056     (divisor >> 8);
0057 }
0058 
0059 static void uart_initialize(int minor)
0060 {
0061   /* Set baud rate */
0062   uart_set_baud(OR1K_UART_DEFAULT_BAUD);
0063 
0064   /* Set data pattern configuration */
0065   OR1K_REG(OR1K_BSP_UART_REG_LINE_CTRL) =
0066     OR1K_BSP_UART_REG_LINE_CTRL_WLEN8;
0067 
0068   /* Reset receiver and transmitter */
0069   OR1K_REG(OR1K_BSP_UART_REG_FIFO_CTRL) =
0070     OR1K_BSP_UART_REG_FIFO_CTRL_ENABLE_FIFO |
0071     OR1K_BSP_UART_REG_FIFO_CTRL_CLEAR_RCVR  |
0072     OR1K_BSP_UART_REG_FIFO_CTRL_TRIGGER_14;
0073 
0074   /* Disable all interrupts */
0075   OR1K_REG(OR1K_BSP_UART_REG_INT_ENABLE) = 0x00;
0076 
0077 }
0078 
0079 static int uart_first_open(int major, int minor, void *arg)
0080 {
0081   rtems_libio_open_close_args_t *oc = (rtems_libio_open_close_args_t *) arg;
0082   struct rtems_termios_tty *tty = (struct rtems_termios_tty *) oc->iop->data1;
0083   const console_tbl *ct = Console_Port_Tbl [minor];
0084   console_data *cd = &Console_Port_Data [minor];
0085 
0086   cd->termios_data = tty;
0087   rtems_termios_set_initial_baud(tty, ct->ulClock);
0088 
0089   return 0;
0090 }
0091 
0092 static int uart_last_close(int major, int minor, void *arg)
0093 {
0094   return 0;
0095 }
0096 
0097 static int uart_read_polled(int minor)
0098 {
0099   unsigned char lsr;
0100 
0101  /* Get a character when avaiable */
0102   do {
0103        lsr = OR1K_REG(OR1K_BSP_UART_REG_LINE_STATUS);
0104   } while ((lsr & OR1K_BSP_UART_REG_LINE_STATUS_DR)
0105            != OR1K_BSP_UART_REG_LINE_STATUS_DR);
0106 
0107   return OR1K_REG(OR1K_BSP_UART_REG_RX);
0108 }
0109 
0110 static void uart_write_polled(int minor, char c)
0111 {
0112   unsigned char lsr;
0113 
0114   /* Wait until there is no pending data in the transmitter FIFO (empty) */
0115   do {
0116       lsr = OR1K_REG(OR1K_BSP_UART_REG_LINE_STATUS);
0117   } while (!(lsr & OR1K_BSP_UART_REG_LINE_STATUS_THRE));
0118 
0119   OR1K_REG(OR1K_BSP_UART_REG_TX) = c;
0120 }
0121 
0122 static ssize_t uart_write(
0123   int minor,
0124   const char *s,
0125   size_t n
0126 )
0127 {
0128   ssize_t i = 0;
0129 
0130   for (i = 0; i < n; ++i){
0131     uart_write_polled(minor, s [i]);
0132   }
0133 
0134   return n;
0135 }
0136 
0137 static int uart_set_attributes(int minor, const struct termios *term)
0138 {
0139   return -1;
0140 }
0141 
0142 const console_fns generic_or1k_uart_fns = {
0143   .deviceProbe = libchip_serial_default_probe,
0144   .deviceFirstOpen = uart_first_open,
0145   .deviceLastClose = uart_last_close,
0146   .deviceRead = uart_read_polled,
0147   .deviceWrite = uart_write,
0148   .deviceInitialize = uart_initialize,
0149   .deviceWritePolled = uart_write_polled,
0150   .deviceSetAttributes = uart_set_attributes,
0151   .deviceOutputUsesInterrupts = false
0152 };