Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:23:50

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /*
0004  * Copyright (C) 2022 On-Line Applications Research Corporation (OAR)
0005  *
0006  * Redistribution and use in source and binary forms, with or without
0007  * modification, are permitted provided that the following conditions
0008  * are met:
0009  * 1. Redistributions of source code must retain the above copyright
0010  *    notice, this list of conditions and the following disclaimer.
0011  * 2. Redistributions in binary form must reproduce the above copyright
0012  *    notice, this list of conditions and the following disclaimer in the
0013  *    documentation and/or other materials provided with the distribution.
0014  *
0015  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0016  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0017  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0018  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0019  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0020  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0021  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0022  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0023  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0024  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0025  * POSSIBILITY OF SUCH DAMAGE.
0026  */
0027 
0028 #include <bspopts.h>
0029 #include <bsp/microblaze-fdt-support.h>
0030 #include <bsp/fdt.h>
0031 
0032 #include <libfdt.h>
0033 
0034 #ifdef BSP_START_COPY_FDT_FROM_U_BOOT
0035 /* use external dtb provided by u-boot */
0036 #include <sys/param.h>
0037 
0038 #ifndef BSP_FDT_BLOB_SIZE_MAX
0039 #define BSP_FDT_BLOB_SIZE_MAX 0
0040 #endif
0041 
0042 static RTEMS_ALIGNED(8) uint32_t
0043 system_dtb[BSP_FDT_BLOB_SIZE_MAX / sizeof(uint32_t)];
0044 
0045 void bsp_fdt_copy(const void *src)
0046 {
0047   const volatile uint32_t *s = (const uint32_t *) src;
0048   uint32_t *d = RTEMS_DECONST(uint32_t *, &system_dtb[0]);
0049 
0050   if (s != d) {
0051     size_t m = MIN(sizeof(system_dtb), fdt_totalsize(src));
0052     size_t aligned_size = roundup2(m, CPU_CACHE_LINE_BYTES);
0053     size_t n = (m + sizeof(*d) - 1) / sizeof(*d);
0054     size_t i;
0055 
0056     for (i = 0; i < n; ++i) {
0057       d[i] = s[i];
0058     }
0059 
0060     rtems_cache_flush_multiple_data_lines(d, aligned_size);
0061   }
0062 }
0063 #endif /* BSP_START_COPY_FDT_FROM_U_BOOT */
0064 
0065 #ifdef BSP_MICROBLAZE_FPGA_USE_FDT
0066 #ifndef BSP_START_COPY_FDT_FROM_U_BOOT
0067 /* use internal bsp dtb */
0068 #include BSP_MICROBLAZE_FPGA_DTB_HEADER_PATH
0069 #endif /* BSP_START_COPY_FDT_FROM_U_BOOT */
0070 
0071 const void *bsp_fdt_get(void)
0072 {
0073   return system_dtb;
0074 }
0075 
0076 uint32_t bsp_fdt_map_intr(const uint32_t *intr, size_t icells)
0077 {
0078   return intr[0];
0079 }
0080 #endif /* BSP_MICROBLAZE_FPGA_USE_FDT */
0081 
0082 uint32_t try_get_prop_from_device_tree(
0083   const char *compatible,
0084   const char *prop_name,
0085   uint32_t default_value
0086 )
0087 {
0088   uint32_t value = default_value;
0089 
0090 #ifdef BSP_MICROBLAZE_FPGA_USE_FDT
0091   const void *fdt = bsp_fdt_get();
0092   int node = fdt_node_offset_by_compatible( fdt, -1, compatible );
0093   if ( node < 0 ) {
0094     return default_value;
0095   }
0096 
0097   const uint32_t *prop = fdt_getprop( fdt, node, prop_name, NULL );
0098   if ( prop == NULL ) {
0099     return default_value;
0100   }
0101 
0102   value = fdt32_to_cpu( prop[0] );
0103 #endif /* BSP_MICROBLAZE_FPGA_USE_FDT */
0104 
0105    return value;
0106 }