Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:00

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /*
0004  *  This file contains the TTY driver for the GRLIB APBUART
0005  *
0006  *  This driver uses the termios pseudo driver.
0007  *
0008  *  COPYRIGHT (c) 1989-1998.
0009  *  On-Line Applications Research Corporation (OAR).
0010  *
0011  *  Modified for GRLIB BSP.
0012  *  COPYRIGHT (c) 2004.
0013  *  Gaisler Research.
0014  *
0015  * Redistribution and use in source and binary forms, with or without
0016  * modification, are permitted provided that the following conditions
0017  * are met:
0018  * 1. Redistributions of source code must retain the above copyright
0019  *    notice, this list of conditions and the following disclaimer.
0020  * 2. Redistributions in binary form must reproduce the above copyright
0021  *    notice, this list of conditions and the following disclaimer in the
0022  *    documentation and/or other materials provided with the distribution.
0023  *
0024  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0025  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0026  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0027  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0028  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0029  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0030  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0031  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0032  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0033  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0034  * POSSIBILITY OF SUCH DAMAGE.
0035  */
0036 
0037 /* Define CONSOLE_USE_INTERRUPTS to enable APBUART interrupt handling instead
0038  * of polling mode.
0039  *
0040  * Note that it is not possible to use the interrupt mode of the driver
0041  * together with the "old" APBUART and -u to GRMON. However the new
0042  * APBUART core (from GRLIB 1.0.17-b2710) has the GRMON debug bit and can 
0043  * handle interrupts.
0044  *
0045  * NOTE: This can be defined in the griscv.cfg file.
0046  */
0047 
0048 #include <bsp.h>
0049 #include <bsp/fatal.h>
0050 #include <amba.h>
0051 #include <grlib/ambapp.h>
0052 #include <grlib/apbuart_termios.h>
0053 #include <rtems/console.h>
0054 #include <string.h>
0055 
0056 /* The GRLIB BSP UART driver can rely on the Driver Manager if the
0057  * DrvMgr is initialized during startup. Otherwise the classic driver
0058  * must be used.
0059  *
0060  * The DrvMgr APBUART driver is located in the shared/uart directory
0061  */
0062 #ifndef RTEMS_DRVMGR_STARTUP
0063 
0064 int syscon_uart_index __attribute__((weak)) = 0;
0065 
0066 /* body is in debugputs.c */
0067 static struct apbuart_context apbuarts[BSP_NUMBER_OF_TERMIOS_PORTS];
0068 static int uarts = 0;
0069 
0070 static rtems_termios_device_context *grlib_console_get_context(int index)
0071 {
0072   struct apbuart_context *uart = &apbuarts[index];
0073 
0074   rtems_termios_device_context_initialize(&uart->base, "APBUART");
0075 
0076   return &uart->base;
0077 }
0078 
0079 /* AMBA PP find routine. Extract AMBA PnP information into data structure. */
0080 static int find_matching_apbuart(struct ambapp_dev *dev, int index, void *arg)
0081 {
0082   struct ambapp_apb_info *apb = (struct ambapp_apb_info *)dev->devinfo;
0083 
0084   /* Extract needed information of one APBUART */
0085   apbuarts[uarts].regs = (apbuart *)apb->start;
0086   apbuarts[uarts].irq = apb->common.irq;
0087   /* Get APBUART core frequency, it is assumed that it is the same
0088    * as Bus frequency where the UART is situated
0089    */
0090   apbuarts[uarts].freq_hz = ambapp_freq_get(ambapp_plb(), dev);
0091   uarts++;
0092 
0093   if (uarts >= BSP_NUMBER_OF_TERMIOS_PORTS)
0094     return 1; /* Satisfied number of UARTs, stop search */
0095   else
0096     return 0; /* Continue searching for more UARTs */
0097 }
0098 
0099 /* Find all UARTs */
0100 static void grlib_console_scan_uarts(void)
0101 {
0102   memset(apbuarts, 0, sizeof(apbuarts));
0103 
0104   /* Find APBUART cores */
0105   ambapp_for_each(ambapp_plb(), (OPTIONS_ALL|OPTIONS_APB_SLVS), VENDOR_GAISLER,
0106                   GAISLER_APBUART, find_matching_apbuart, NULL);
0107 }
0108 
0109 rtems_device_driver console_initialize(
0110   rtems_device_major_number  major,
0111   rtems_device_minor_number  minor,
0112   void                      *arg
0113 )
0114 {
0115   const rtems_termios_device_handler *handler =
0116 #if CONSOLE_USE_INTERRUPTS
0117     &apbuart_handler_interrupt;
0118 #else
0119     &apbuart_handler_polled;
0120 #endif
0121   rtems_status_code status;
0122   int i;
0123   char console_name[16];
0124 
0125   rtems_termios_initialize();
0126 
0127   /* Find UARTs */
0128   grlib_console_scan_uarts();
0129 
0130   /* Update syscon_uart_index to index used as /dev/console
0131    * Let user select System console by setting syscon_uart_index. If the
0132    * BSP is to provide the default UART (syscon_uart_index==0):
0133    *   non-MP: APBUART[0] is system console
0134    *   MP: LEON CPU index select UART
0135    */
0136   if (syscon_uart_index == 0) {
0137 #if defined(RTEMS_MULTIPROCESSING)
0138     syscon_uart_index = GRLIB_Cpu_Index;
0139 #else
0140     syscon_uart_index = 0;
0141 #endif
0142   } else {
0143     syscon_uart_index = syscon_uart_index - 1; /* User selected sys-console */
0144   }
0145 
0146   /*  Register Device Names
0147    *
0148    *  0 /dev/console   - APBUART[USER-SELECTED, DEFAULT=APBUART[0]]
0149    *  1 /dev/console_a - APBUART[0] (by default not present because is console)
0150    *  2 /dev/console_b - APBUART[1]
0151    *  ...
0152    *
0153    * On a MP system one should not open UARTs that other OS instances use.
0154    */
0155   if (syscon_uart_index < uarts) {
0156     status = rtems_termios_device_install(
0157       CONSOLE_DEVICE_NAME,
0158       handler,
0159       NULL,
0160       grlib_console_get_context(syscon_uart_index)
0161     );
0162     if (status != RTEMS_SUCCESSFUL)
0163       bsp_fatal(LEON3_FATAL_CONSOLE_REGISTER_DEV);
0164   }
0165   strcpy(console_name,"/dev/console_a");
0166   for (i = 0; i < uarts; i++) {
0167     if (i == syscon_uart_index)
0168       continue; /* skip UART that is registered as /dev/console */
0169     console_name[13] = 'a' + i;
0170     rtems_termios_device_install(
0171       console_name,
0172       handler,
0173       NULL,
0174       grlib_console_get_context(i)
0175     );
0176   }
0177 
0178   return RTEMS_SUCCESSFUL;
0179 }
0180 
0181 #endif