Back to home page

LXR

 
 

    


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

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /**
0004  * @file
0005  *
0006  * @ingroup RTEMSBSPsSPARCLEON3
0007  *
0008  * @brief This source file contains the definition of ::BSP_output_char and
0009  *   ::BSP_poll_char.
0010  */
0011 
0012 /*
0013  *  COPYRIGHT (c) 1989-1999.
0014  *  On-Line Applications Research Corporation (OAR).
0015  *
0016  *  Modified for LEON3 BSP.
0017  *  COPYRIGHT (c) 2011.
0018  *  Aeroflex Gaisler.
0019  *
0020  * Redistribution and use in source and binary forms, with or without
0021  * modification, are permitted provided that the following conditions
0022  * are met:
0023  * 1. Redistributions of source code must retain the above copyright
0024  *    notice, this list of conditions and the following disclaimer.
0025  * 2. Redistributions in binary form must reproduce the above copyright
0026  *    notice, this list of conditions and the following disclaimer in the
0027  *    documentation and/or other materials provided with the distribution.
0028  *
0029  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0030  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0031  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0032  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0033  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0034  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0035  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0036  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0037  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0038  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0039  * POSSIBILITY OF SUCH DAMAGE.
0040  */
0041 
0042 #include <bsp.h>
0043 #include <bsp/leon3.h>
0044 #include <rtems/bspIo.h>
0045 #include <rtems/sysinit.h>
0046 #include <grlib/apbuart.h>
0047 #include <grlib/io.h>
0048 
0049 #if !defined(LEON3_APBUART_BASE)
0050 #include <grlib/ambapp.h>
0051 
0052 int leon3_debug_uart_index __attribute__((weak)) = 0;
0053 
0054 apbuart *leon3_debug_uart = NULL;
0055 #endif
0056 
0057 static void bsp_debug_uart_init(void);
0058 
0059 static void apbuart_enable_receive_and_transmit(apbuart *regs)
0060 {
0061   uint32_t ctrl;
0062 
0063   ctrl = grlib_load_32(&regs->ctrl);
0064   ctrl |= APBUART_CTRL_RE | APBUART_CTRL_TE;
0065   grlib_store_32(&regs->ctrl, ctrl);
0066   grlib_store_32(&regs->status, 0);
0067 }
0068 
0069 static void bsp_debug_uart_output_char(char c)
0070 {
0071   apbuart_outbyte_polled(leon3_debug_uart, c);
0072   apbuart_outbyte_wait(leon3_debug_uart);
0073 }
0074 
0075 static int bsp_debug_uart_poll_char(void)
0076 {
0077   return apbuart_inbyte_nonblocking(leon3_debug_uart);
0078 }
0079 
0080 static void bsp_debug_uart_pre_init_out(char c)
0081 {
0082   bsp_debug_uart_init();
0083   (*BSP_output_char)(c);
0084 }
0085 
0086 #if defined(LEON3_APBUART_BASE)
0087 
0088 static void bsp_debug_uart_init(void)
0089 {
0090   apbuart_enable_receive_and_transmit(leon3_debug_uart);
0091   BSP_poll_char = bsp_debug_uart_poll_char;
0092   BSP_output_char = bsp_debug_uart_output_char;
0093 }
0094 
0095 #else /* !LEON3_APBUART_BASE */
0096 
0097 static void bsp_debug_uart_discard(char c)
0098 {
0099   (void) c;
0100 }
0101 
0102 /* Initialize the BSP system debug console layer. It will scan AMBA Plu&Play
0103  * for a debug APBUART and enable RX/TX for that UART.
0104  */
0105 static void bsp_debug_uart_init(void)
0106 {
0107   int i;
0108   struct ambapp_dev *adev;
0109 
0110   if ( BSP_output_char != bsp_debug_uart_pre_init_out ) {
0111     return;
0112   }
0113 
0114   BSP_output_char = bsp_debug_uart_discard;
0115 
0116   /* Update leon3_debug_uart_index to index used as debug console. Let user
0117    * select Debug console by setting leon3_debug_uart_index. If the BSP is to
0118    * provide the default UART (leon3_debug_uart_index==0):
0119    *   non-MP: APBUART[0] is debug console
0120    *   MP: LEON CPU index select UART
0121    */
0122   if (leon3_debug_uart_index == 0) {
0123 #if defined(RTEMS_MULTIPROCESSING)
0124     leon3_debug_uart_index = LEON3_Cpu_Index;
0125 #else
0126     leon3_debug_uart_index = 0;
0127 #endif
0128   } else {
0129     leon3_debug_uart_index--; /* User selected dbg-console */
0130   }
0131 
0132   /* Find APBUART core for System Debug Console */
0133   i = leon3_debug_uart_index;
0134   adev = (void *)ambapp_for_each(ambapp_plb(), (OPTIONS_ALL|OPTIONS_APB_SLVS),
0135                                  VENDOR_GAISLER, GAISLER_APBUART,
0136                                  ambapp_find_by_idx, (void *)&i);
0137   if (adev != NULL) {
0138     struct ambapp_apb_info *apb;
0139 
0140     /*
0141      * Found a matching debug console, initialize debug UART if present for
0142      * printk().
0143      */
0144     apb = (struct ambapp_apb_info *)adev->devinfo;
0145     leon3_debug_uart = (apbuart *)apb->start;
0146     apbuart_enable_receive_and_transmit(leon3_debug_uart);
0147 
0148     BSP_poll_char = bsp_debug_uart_poll_char;
0149     BSP_output_char = bsp_debug_uart_output_char;
0150   }
0151 }
0152 
0153 #endif /* LEON3_APBUART_BASE */
0154 
0155 RTEMS_SYSINIT_ITEM(
0156   bsp_debug_uart_init,
0157   RTEMS_SYSINIT_BSP_START,
0158   RTEMS_SYSINIT_ORDER_FOURTH
0159 );
0160 
0161 BSP_output_char_function_type BSP_output_char = bsp_debug_uart_pre_init_out;
0162 
0163 BSP_polling_getchar_function_type BSP_poll_char;