Back to home page

LXR

 
 

    


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

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /**
0004  * @file
0005  *
0006  * @ingroup RTEMSBSPsAArch64RaspberryPi
0007  *
0008  * @brief Console Configuration
0009  */
0010 
0011 /*
0012  * Copyright (C) 2022 Mohd Noor Aman
0013  * Copyright (C) 2023 Utkarsh Verma
0014  * Copyright (C) 2024 Ning Yang
0015  *
0016  * Redistribution and use in source and binary forms, with or without
0017  * modification, are permitted provided that the following conditions
0018  * are met:
0019  * 1. Redistributions of source code must retain the above copyright
0020  *    notice, this list of conditions and the following disclaimer.
0021  * 2. Redistributions in binary form must reproduce the above copyright
0022  *    notice, this list of conditions and the following disclaimer in the
0023  *    documentation and/or other materials provided with the distribution.
0024  *
0025  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0026  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0027  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0028  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0029  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0030  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0031  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0032  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0033  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0034  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0035  * POSSIBILITY OF SUCH DAMAGE.
0036  */
0037 
0038 #include <rtems/bspIo.h>
0039 
0040 #include <bsp.h>
0041 #include <dev/serial/arm-pl011.h>
0042 #include <bsp/irq.h>
0043 #include <bsp/console.h>
0044 #include <bsp/fatal.h>
0045 #include <bsp/rpi-gpio.h>
0046 #include <bspopts.h>
0047 
0048 #include <rtems/console.h>
0049 #include <rtems/rtems/status.h>
0050 #include <rtems/termiosdevice.h>
0051 #include <stdint.h>
0052 
0053 #define CONSOLE_DEVICE_CONTEXT_NAME(port_no) uart##port_no##_context
0054 
0055 #define CONSOLE_DEVICE_CONTEXT( \
0056   port_no, _file_name, regs_base, _size, clock_freq, irq_no, \
0057   context_type, ... \
0058 ) \
0059 static context_type CONSOLE_DEVICE_CONTEXT_NAME(port_no) = { \
0060   .base   = RTEMS_TERMIOS_DEVICE_CONTEXT_INITIALIZER("UART" #port_no), \
0061   .regs = (volatile arm_pl011_uart *) regs_base, \
0062   .clock     = clock_freq, \
0063   .initial_baud = 115200, \
0064   .irq          = irq_no, \
0065 };
0066 
0067 #define CONSOLE_DEVICE( \
0068   port_no, file_name, _base, _size, _clock, _irq,_context_type, dev_handler, \
0069   write_char_func, rx_pin, tx_pin, gpio_func, ... \
0070 ) \
0071 [CONSOLE_DEVICE_PORT2ENUM(port_no)] = { \
0072   .file    = file_name, \
0073   .context = &CONSOLE_DEVICE_CONTEXT_NAME(port_no).base, \
0074   .gpio    = {.rx = rx_pin, .tx = tx_pin, .function = gpio_func}, \
0075   .handler = dev_handler, \
0076   .write_char_polled = write_char_func, \
0077 },
0078 
0079 typedef struct {
0080     const unsigned int rx;
0081     const unsigned int tx;
0082     const raspberrypi_gpio_function function;
0083 } raspberrypi_console_device_gpio_config;
0084 
0085 typedef struct {
0086     const char* file;
0087     rtems_termios_device_context* context;
0088     const raspberrypi_console_device_gpio_config gpio;
0089 
0090     const rtems_termios_device_handler* handler;
0091     void (*write_char_polled)(rtems_termios_device_context*, char);
0092 } raspberrypi_console_device;
0093 
0094 /* Initialize all console device contexts */
0095 CONSOLE_DEVICES(CONSOLE_DEVICE_CONTEXT)
0096 
0097 /* Initialize all device configurations */
0098 static const raspberrypi_console_device devices[CONSOLE_DEVICE_COUNT] = {
0099   CONSOLE_DEVICES(CONSOLE_DEVICE)
0100 };
0101 
0102 static rtems_status_code console_device_init_gpio(
0103   const raspberrypi_console_device_gpio_config *gpio
0104 )
0105 {
0106   rtems_status_code status = raspberrypi_gpio_set_function(
0107     gpio->rx,
0108     gpio->function
0109   );
0110   if (status != RTEMS_SUCCESSFUL)
0111     return status;
0112   
0113   status = raspberrypi_gpio_set_function(gpio->tx, gpio->function);
0114   if (status != RTEMS_SUCCESSFUL)
0115       return status;
0116 
0117   status = raspberrypi_gpio_set_pull(gpio->rx, GPIO_PULL_NONE);
0118   if (status != RTEMS_SUCCESSFUL)
0119       return status;
0120 
0121   status = raspberrypi_gpio_set_pull(gpio->tx, GPIO_PULL_NONE);
0122 
0123   return status;
0124 }
0125 
0126 static void output_char(const char ch) {
0127   const raspberrypi_console_device* device = &devices[BSP_CONSOLE_PORT];
0128   device->write_char_polled(device->context, ch);
0129 }
0130 
0131 static int poll_char(void) {
0132   const raspberrypi_console_device* device = &devices[BSP_CONSOLE_PORT];
0133   return device->handler->poll_read(device->context);
0134 }
0135 
0136 rtems_status_code raspberrypi_uart_init(
0137   raspberrypi_console_device_port uart_num
0138 )
0139 {
0140   const raspberrypi_console_device *device = &devices[uart_num];
0141 
0142   rtems_status_code status = console_device_init_gpio(&device->gpio);
0143   if (status != RTEMS_SUCCESSFUL) {
0144     return status;
0145   }
0146 
0147   status = rtems_termios_device_install(
0148     device->file, device->handler, NULL, device->context
0149   );
0150 
0151   return status;
0152 }
0153 
0154 rtems_device_driver console_initialize(
0155   rtems_device_major_number major,
0156   rtems_device_minor_number minor,
0157   void *arg
0158 )
0159 {
0160   const raspberrypi_console_device* device = &devices[BSP_CONSOLE_PORT];
0161   rtems_status_code status = raspberrypi_uart_init(BSP_CONSOLE_PORT);
0162   if (status != RTEMS_SUCCESSFUL) {
0163     bsp_fatal(BSP_FATAL_CONSOLE_INSTALL_0);
0164   }
0165 
0166   rtems_termios_initialize();
0167 
0168   if (link(device->file, CONSOLE_DEVICE_NAME) != 0) {
0169     bsp_fatal(BSP_FATAL_CONSOLE_INSTALL_1);
0170   }
0171 
0172   return RTEMS_SUCCESSFUL;
0173 }
0174 
0175 BSP_output_char_function_type BSP_output_char = output_char;
0176 BSP_polling_getchar_function_type BSP_poll_char = poll_char;