Back to home page

LXR

 
 

    


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

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /**
0004  * @file
0005  *
0006  * @brief BSP specific initialization support routines
0007  *
0008  */
0009 
0010 /*
0011  * COPYRIGHT (c) 1989-2020.
0012  * On-Line Applications Research Corporation (OAR).
0013  * Cobham Gaisler AB.
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 <bsp/fdt.h>
0039 
0040 #include <rtems/sysinit.h>
0041 
0042 #include <libfdt.h>
0043 
0044 /*
0045  *  These are provided by the linkcmds for ALL of the BSPs which use this file.
0046  */
0047 extern char WorkAreaBase[];
0048 extern char RamEnd[];
0049 
0050 static Memory_Area _Memory_Areas[ 1 ];
0051 
0052 static const char memory_path[] = "/memory";
0053 
0054 static void* get_end_of_memory_from_fdt(void)
0055 {
0056   const void *fdt;
0057   const void *val;
0058   int node;
0059   int parent;
0060   int ac;
0061   int sc;
0062   int len;
0063   uintptr_t start;
0064   uintptr_t size;
0065 
0066   fdt = bsp_fdt_get();
0067 
0068   node = fdt_path_offset_namelen(
0069     fdt,
0070     memory_path,
0071     (int) sizeof(memory_path) - 1
0072   );
0073 
0074   if (node < 0) {
0075     return NULL;
0076   }
0077 
0078   parent = fdt_parent_offset(fdt, node);
0079   if (parent < 0) {
0080     return NULL;
0081   }
0082 
0083   ac = fdt_address_cells(fdt, parent);
0084   if (ac != 1 && ac != 2) {
0085     return NULL;
0086   }
0087 
0088   sc = fdt_size_cells(fdt, parent);
0089   if (sc != 1 && sc != 2) {
0090     return NULL;
0091   }
0092 
0093   if (sc > ac) {
0094     return NULL;
0095   }
0096 
0097   val = fdt_getprop(fdt, node, "reg", &len);
0098   if (len < sc + ac) {
0099     return NULL;
0100   }
0101 
0102   if (ac == 1) {
0103     start = fdt32_ld(&((fdt32_t *)val)[0]);
0104     size = fdt32_ld(&((fdt32_t *)val)[1]);
0105   }
0106 
0107   if (ac == 2) {
0108     start = fdt64_ld(&((fdt64_t *)val)[0]);
0109     if (sc == 1) {
0110       size = fdt32_ld(&((fdt32_t *)(val+8))[0]);
0111     } else {
0112       size = fdt64_ld(&((fdt64_t *)val)[1]);
0113     }
0114   }
0115 
0116   return (void*) (start + size);
0117 }
0118 
0119 static void bsp_memory_initialize( void )
0120 {
0121   void *end;
0122 
0123   /* get end of memory from the "/memory" node in the fdt */
0124   end = get_end_of_memory_from_fdt();
0125   if (end == NULL) {
0126     /* fall back to linker symbol if "/memory" node not found or invalid */
0127     end = RamEnd;
0128   }
0129   _Memory_Initialize( &_Memory_Areas[ 0 ], WorkAreaBase, end );
0130 }
0131 
0132 RTEMS_SYSINIT_ITEM(
0133   bsp_memory_initialize,
0134   RTEMS_SYSINIT_MEMORY,
0135   RTEMS_SYSINIT_ORDER_MIDDLE
0136 );
0137 
0138 static const Memory_Information _Memory_Information =
0139   MEMORY_INFORMATION_INITIALIZER( _Memory_Areas );
0140 
0141 const Memory_Information *_Memory_Get( void )
0142 {
0143   return &_Memory_Information;
0144 }