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-1999.
0009  *  On-Line Applications Research Corporation (OAR).
0010  *
0011  *  Modified for GRLIB BSP.
0012  *  COPYRIGHT (c) 2011.
0013  *  Aeroflex Gaisler.
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 #include <bsp.h>
0038 #include <amba.h>
0039 #include <rtems/libio.h>
0040 #include <rtems/sysinit.h>
0041 #include <stdlib.h>
0042 #include <assert.h>
0043 #include <stdio.h>
0044 #include <grlib/apbuart.h>
0045 #include <grlib/ambapp.h>
0046 #include <grlib/io.h>
0047 
0048 int grlib_debug_uart_index __attribute__((weak)) = 0;
0049 apbuart *grlib_debug_uart = NULL;
0050 
0051 /* Before UART driver has registered (or when no UART is available), calls to
0052  * printk that gets to bsp_out_char() will be filling data into the
0053  * pre_printk_dbgbuf[] buffer, hopefully the buffer can help debugging the
0054  * early BSP boot.. At least the last printk() will be caught.
0055  *
0056 static char pre_printk_dbgbuf[32] = {0};
0057 static int pre_printk_pos = 0;
0058  */
0059 
0060 /* Initialize the BSP system debug console layer. It will scan AMBA Plu&Play
0061  * for a debug APBUART and enable RX/TX for that UART.
0062  */
0063 static void bsp_debug_uart_init(void)
0064 {
0065   int i;
0066   struct ambapp_dev *adev;
0067   struct ambapp_apb_info *apb;
0068 
0069   /* Update grlib_debug_uart_index to index used as debug console. Let user
0070    * select Debug console by setting grlib_debug_uart_index. If the BSP is to
0071    * provide the default UART (grlib_debug_uart_index==0):
0072    *   non-MP: APBUART[0] is debug console
0073    *   MP: LEON CPU index select UART
0074    */
0075   if (grlib_debug_uart_index == 0) {
0076 #if defined(RTEMS_MULTIPROCESSING)
0077     grlib_debug_uart_index = GRLIB_Cpu_Index;
0078 #else
0079     grlib_debug_uart_index = 0;
0080 #endif
0081   } else {
0082     grlib_debug_uart_index--; /* User selected dbg-console */
0083   }
0084 
0085   /* Find APBUART core for System Debug Console */
0086   i = grlib_debug_uart_index;
0087   adev = (void *)ambapp_for_each(ambapp_plb(), (OPTIONS_ALL|OPTIONS_APB_SLVS),
0088                                  VENDOR_GAISLER, GAISLER_APBUART,
0089                                  ambapp_find_by_idx, (void *)&i);
0090   if (adev) {
0091     uint32_t ctrl;
0092 
0093     /* Found a matching debug console, initialize debug uart if present
0094      * for printk
0095      */
0096     apb = (struct ambapp_apb_info *)adev->devinfo;
0097     grlib_debug_uart = (apbuart *)apb->start;
0098     ctrl = grlib_load_32(&grlib_debug_uart->ctrl);
0099     ctrl |= APBUART_CTRL_RE | APBUART_CTRL_TE;
0100     grlib_store_32(&grlib_debug_uart->ctrl, ctrl);
0101     grlib_store_32(&grlib_debug_uart->status, 0);
0102   }
0103 }
0104 
0105 RTEMS_SYSINIT_ITEM(
0106   bsp_debug_uart_init,
0107   RTEMS_SYSINIT_BSP_START,
0108   RTEMS_SYSINIT_ORDER_FOURTH
0109 );
0110 
0111 /* putchar/getchar for printk */
0112 static void bsp_out_char(char c)
0113 {
0114   if (grlib_debug_uart == NULL) {
0115     uint32_t ctrl;
0116 
0117     /* Try to assign standard UART address to debug driver to pass some tests */
0118     grlib_debug_uart = (apbuart *) 0x80000100;
0119     ctrl = grlib_load_32(&grlib_debug_uart->ctrl);
0120     ctrl |= APBUART_CTRL_RE | APBUART_CTRL_TE;
0121     grlib_store_32(&grlib_debug_uart->ctrl, ctrl);
0122     grlib_store_32(&grlib_debug_uart->status, 0);
0123     /* Local debug buffer when UART driver has not registered */
0124     /*
0125     pre_printk_dbgbuf[pre_printk_pos++] = c;
0126     pre_printk_pos = pre_printk_pos & (sizeof(pre_printk_dbgbuf)-1);
0127     return;
0128     */
0129   }
0130 
0131   apbuart_outbyte_polled(grlib_debug_uart, c);
0132   apbuart_outbyte_wait(grlib_debug_uart);
0133 }
0134 
0135 /*
0136  *  To support printk
0137  */
0138 
0139 #include <rtems/bspIo.h>
0140 
0141 BSP_output_char_function_type BSP_output_char = bsp_out_char;
0142 
0143 static int bsp_in_char(void)
0144 {
0145   int tmp;
0146 
0147   if (grlib_debug_uart == NULL)
0148     return EOF;
0149 
0150   while ((tmp = apbuart_inbyte_nonblocking(grlib_debug_uart)) < 0)
0151     ;
0152   return tmp;
0153 }
0154 
0155 BSP_polling_getchar_function_type BSP_poll_char = bsp_in_char;