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 RTEMSBSPsARMCycV
0007  */
0008 
0009 /*
0010  * Copyright (C) 2013, 2014 embedded brains GmbH & Co. KG
0011  *
0012  * Redistribution and use in source and binary forms, with or without
0013  * modification, are permitted provided that the following conditions
0014  * are met:
0015  * 1. Redistributions of source code must retain the above copyright
0016  *    notice, this list of conditions and the following disclaimer.
0017  * 2. Redistributions in binary form must reproduce the above copyright
0018  *    notice, this list of conditions and the following disclaimer in the
0019  *    documentation and/or other materials provided with the distribution.
0020  *
0021  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0022  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0023  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0024  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0025  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0026  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0027  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0028  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0029  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0030  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0031  * POSSIBILITY OF SUCH DAMAGE.
0032  */
0033 
0034 #include <libchip/ns16550.h>
0035 
0036 #include <rtems/bspIo.h>
0037 
0038 #include <bsp.h>
0039 #include <bsp/irq.h>
0040 #include <bsp/alt_clock_manager.h>
0041 #include <bsp/console-termios.h>
0042 #include <bsp/socal/alt_rstmgr.h>
0043 #include <bsp/socal/socal.h>
0044 #include <bsp/socal/alt_uart.h>
0045 #include <bsp/socal/hps.h>
0046 
0047 #ifdef BSP_USE_UART_INTERRUPTS
0048   #define DEVICE_FNS &ns16550_handler_interrupt
0049 #else
0050   #define DEVICE_FNS &ns16550_handler_polled
0051 #endif
0052 
0053 static uint8_t altera_cyclone_v_uart_get_register(uintptr_t addr, uint8_t i)
0054 {
0055   volatile uint32_t *reg = (volatile uint32_t *) addr;
0056 
0057   return (uint8_t) reg [i];
0058 }
0059 
0060 static void altera_cyclone_v_uart_set_register(uintptr_t addr, uint8_t i, uint8_t val)
0061 {
0062   volatile uint32_t *reg = (volatile uint32_t *) addr;
0063 
0064   reg [i] = val;
0065 }
0066 
0067 static bool altera_cyclone_v_uart_probe(
0068   rtems_termios_device_context *base,
0069   uint32_t uart_set_mask
0070 )
0071 {
0072   ns16550_context *ctx = (ns16550_context *) base;
0073   bool            ret           = true;
0074   uint32_t        ucr;
0075   ALT_STATUS_CODE sc;
0076   void*           location = (void *) ctx->port;
0077 
0078   /* The ALT_CLK_L4_SP is required for all SoCFPGA UARTs. 
0079    * Check that it's enabled. */
0080   if ( alt_clk_is_enabled(ALT_CLK_L4_SP) != ALT_E_TRUE ) {
0081     ret = false;
0082   }
0083 
0084   if ( ret ) {
0085     sc = alt_clk_freq_get(ALT_CLK_L4_SP, &ctx->clock);
0086     if ( sc != ALT_E_SUCCESS ) {
0087       ret = false;
0088     }
0089   }
0090 
0091   if ( ret ) {
0092     // Bring UART out of reset.
0093     alt_clrbits_word(ALT_RSTMGR_PERMODRST_ADDR, uart_set_mask);
0094 
0095     // Verify the UCR (UART Component Version)
0096     ucr = alt_read_word( ALT_UART_UCV_ADDR( location ) );
0097     if ( ucr != ALT_UART_UCV_UART_COMPONENT_VER_RESET ) {
0098       ret = false;
0099     }
0100   }
0101 
0102   if ( ret ) {
0103     // Write SRR::UR (Shadow Reset Register :: UART Reset)
0104     alt_write_word( ALT_UART_SRR_ADDR( location ), ALT_UART_SRR_UR_SET_MSK );
0105 
0106     // Read the MSR to work around case:119085.
0107     (void)alt_read_word( ALT_UART_MSR_ADDR( location ) );
0108 
0109     ret = ns16550_probe( base );
0110   }
0111 
0112   return ret;
0113 }
0114 
0115 #ifdef CYCLONE_V_CONFIG_CONSOLE
0116 static bool altera_cyclone_v_uart_probe_0(rtems_termios_device_context *base)
0117 {
0118   return altera_cyclone_v_uart_probe(base, ALT_RSTMGR_PERMODRST_UART0_SET_MSK);
0119 }
0120 
0121 static ns16550_context altera_cyclone_v_uart_context_0 = {
0122   .base = RTEMS_TERMIOS_DEVICE_CONTEXT_INITIALIZER("UART 0"),
0123   .get_reg = altera_cyclone_v_uart_get_register,
0124   .set_reg = altera_cyclone_v_uart_set_register,
0125   .port = (uintptr_t) ALT_UART0_ADDR,
0126   .irq = ALT_INT_INTERRUPT_UART0,
0127   .initial_baud = CYCLONE_V_UART_BAUD
0128 };
0129 #endif
0130 
0131 #ifdef CYCLONE_V_CONFIG_UART_1
0132 static bool altera_cyclone_v_uart_probe_1(rtems_termios_device_context *base)
0133 {
0134   return altera_cyclone_v_uart_probe(base, ALT_RSTMGR_PERMODRST_UART1_SET_MSK);
0135 }
0136 
0137 static ns16550_context altera_cyclone_v_uart_context_1 = {
0138   .base = RTEMS_TERMIOS_DEVICE_CONTEXT_INITIALIZER("UART 1"),
0139   .get_reg = altera_cyclone_v_uart_get_register,
0140   .set_reg = altera_cyclone_v_uart_set_register,
0141   .port = (uintptr_t) ALT_UART1_ADDR,
0142   .irq = ALT_INT_INTERRUPT_UART1,
0143   .initial_baud = CYCLONE_V_UART_BAUD
0144 };
0145 #endif
0146 
0147 const console_device console_device_table[] = {
0148   #ifdef CYCLONE_V_CONFIG_CONSOLE
0149     {
0150       .device_file = "/dev/ttyS0",
0151       .probe = altera_cyclone_v_uart_probe_0,
0152       .handler = DEVICE_FNS,
0153       .context = &altera_cyclone_v_uart_context_0.base
0154     },
0155   #endif
0156   #ifdef CYCLONE_V_CONFIG_UART_1
0157     {
0158       .device_file = "/dev/ttyS1",
0159       .probe = altera_cyclone_v_uart_probe_1,
0160       .handler = DEVICE_FNS,
0161       .context = &altera_cyclone_v_uart_context_1.base
0162     },
0163   #endif
0164 };
0165 
0166 const size_t console_device_count = RTEMS_ARRAY_SIZE(console_device_table);
0167 
0168 static void output_char(char c)
0169 {
0170   rtems_termios_device_context *ctx = console_device_table[0].context;
0171 
0172   ns16550_polled_putchar( ctx, c );
0173 }
0174 
0175 BSP_output_char_function_type BSP_output_char = output_char;
0176 
0177 BSP_polling_getchar_function_type BSP_poll_char = NULL;