xref: /aosp_15_r20/external/pigweed/targets/emcraft_sf2_som/boot.cc (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1 // Copyright 2022 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14 
15 #include "pw_boot/boot.h"
16 
17 #include <array>
18 
19 #include "FreeRTOS.h"
20 #include "config/sf2_mss_hal_conf.h"
21 #include "m2sxxx.h"
22 #include "pw_boot_cortex_m/boot.h"
23 #include "pw_malloc/malloc.h"
24 #include "pw_preprocessor/compiler.h"
25 #include "pw_string/util.h"
26 #include "pw_sys_io_emcraft_sf2/init.h"
27 #include "pw_system/init.h"
28 #include "system_m2sxxx.h"
29 #include "task.h"
30 
31 #include liberosoc_CONFIG_FILE
32 
33 extern "C" void Reset_Handler(void);
34 
35 // uwTick is an uint32_t incremented each Systick interrupt 1ms. uwTick is used
36 // to execute HAL_Delay function.
37 
pw_boot_PreStaticMemoryInit()38 extern "C" void pw_boot_PreStaticMemoryInit() {
39 #if SF2_MSS_NO_BOOTLOADER
40   SystemInit();
41   // Initialize DDR
42   // inclusive-language: disable
43   MDDR->core.ddrc.DYN_SOFT_RESET_CR = 0x0000;
44   MDDR->core.ddrc.DYN_REFRESH_1_CR = 0x27de;
45   MDDR->core.ddrc.DYN_REFRESH_2_CR = 0x030f;
46   MDDR->core.ddrc.DYN_POWERDOWN_CR = 0x0002;
47   MDDR->core.ddrc.DYN_DEBUG_CR = 0x0000;
48   MDDR->core.ddrc.MODE_CR = 0x00C1;
49   MDDR->core.ddrc.ADDR_MAP_BANK_CR = 0x099f;
50   MDDR->core.ddrc.ECC_DATA_MASK_CR = 0x0000;
51   MDDR->core.ddrc.ADDR_MAP_COL_1_CR = 0x3333;
52   MDDR->core.ddrc.ADDR_MAP_COL_2_CR = 0xffff;
53   MDDR->core.ddrc.ADDR_MAP_ROW_1_CR = 0x7777;
54   MDDR->core.ddrc.ADDR_MAP_ROW_2_CR = 0x0fff;
55   MDDR->core.ddrc.INIT_1_CR = 0x0001;
56   MDDR->core.ddrc.CKE_RSTN_CYCLES_CR[0] = 0x4242;
57   MDDR->core.ddrc.CKE_RSTN_CYCLES_CR[1] = 0x0008;
58   MDDR->core.ddrc.INIT_MR_CR = 0x0033;
59   MDDR->core.ddrc.INIT_EMR_CR = 0x0020;
60   MDDR->core.ddrc.INIT_EMR2_CR = 0x0000;
61   MDDR->core.ddrc.INIT_EMR3_CR = 0x0000;
62   MDDR->core.ddrc.DRAM_BANK_TIMING_PARAM_CR = 0x00c0;
63   MDDR->core.ddrc.DRAM_RD_WR_LATENCY_CR = 0x0023;
64   MDDR->core.ddrc.DRAM_RD_WR_PRE_CR = 0x0235;
65   MDDR->core.ddrc.DRAM_MR_TIMING_PARAM_CR = 0x0064;
66   MDDR->core.ddrc.DRAM_RAS_TIMING_CR = 0x0108;
67   MDDR->core.ddrc.DRAM_RD_WR_TRNARND_TIME_CR = 0x0178;
68   MDDR->core.ddrc.DRAM_T_PD_CR = 0x0033;
69   MDDR->core.ddrc.DRAM_BANK_ACT_TIMING_CR = 0x1947;
70   MDDR->core.ddrc.ODT_PARAM_1_CR = 0x0010;
71   MDDR->core.ddrc.ODT_PARAM_2_CR = 0x0000;
72   MDDR->core.ddrc.ADDR_MAP_COL_3_CR = 0x3300;
73   MDDR->core.ddrc.MODE_REG_RD_WR_CR = 0x0000;
74   MDDR->core.ddrc.MODE_REG_DATA_CR = 0x0000;
75   MDDR->core.ddrc.PWR_SAVE_1_CR = 0x0514;
76   MDDR->core.ddrc.PWR_SAVE_2_CR = 0x0000;
77   MDDR->core.ddrc.ZQ_LONG_TIME_CR = 0x0200;
78   MDDR->core.ddrc.ZQ_SHORT_TIME_CR = 0x0040;
79   MDDR->core.ddrc.ZQ_SHORT_INT_REFRESH_MARGIN_CR[0] = 0x0012;
80   MDDR->core.ddrc.ZQ_SHORT_INT_REFRESH_MARGIN_CR[1] = 0x0002;
81   MDDR->core.ddrc.PERF_PARAM_1_CR = 0x4000;
82   MDDR->core.ddrc.HPR_QUEUE_PARAM_CR[0] = 0x80f8;
83   MDDR->core.ddrc.HPR_QUEUE_PARAM_CR[1] = 0x0007;
84   MDDR->core.ddrc.LPR_QUEUE_PARAM_CR[0] = 0x80f8;
85   MDDR->core.ddrc.LPR_QUEUE_PARAM_CR[1] = 0x0007;
86   MDDR->core.ddrc.WR_QUEUE_PARAM_CR = 0x0200;
87   MDDR->core.ddrc.PERF_PARAM_2_CR = 0x0001;
88   MDDR->core.ddrc.PERF_PARAM_3_CR = 0x0000;
89   MDDR->core.ddrc.DFI_RDDATA_EN_CR = 0x0003;
90   MDDR->core.ddrc.DFI_MIN_CTRLUPD_TIMING_CR = 0x0003;
91   MDDR->core.ddrc.DFI_MAX_CTRLUPD_TIMING_CR = 0x0040;
92   MDDR->core.ddrc.DFI_WR_LVL_CONTROL_CR[0] = 0x0000;
93   MDDR->core.ddrc.DFI_WR_LVL_CONTROL_CR[1] = 0x0000;
94   MDDR->core.ddrc.DFI_RD_LVL_CONTROL_CR[0] = 0x0000;
95   MDDR->core.ddrc.DFI_RD_LVL_CONTROL_CR[1] = 0x0000;
96   MDDR->core.ddrc.DFI_CTRLUPD_TIME_INTERVAL_CR = 0x0309;
97   MDDR->core.ddrc.AXI_FABRIC_PRI_ID_CR = 0x0000;
98   MDDR->core.ddrc.ECC_INT_CLR_REG = 0x0000;
99 
100   MDDR->core.phy.LOOPBACK_TEST_CR = 0x0000;
101   MDDR->core.phy.CTRL_SLAVE_RATIO_CR = 0x0080;
102   MDDR->core.phy.DATA_SLICE_IN_USE_CR = 0x0003;
103   MDDR->core.phy.DQ_OFFSET_CR[0] = 0x00000000;
104   MDDR->core.phy.DQ_OFFSET_CR[2] = 0x0000;
105   MDDR->core.phy.DLL_LOCK_DIFF_CR = 0x000B;
106   MDDR->core.phy.FIFO_WE_SLAVE_RATIO_CR[0] = 0x0040;
107   MDDR->core.phy.FIFO_WE_SLAVE_RATIO_CR[1] = 0x0401;
108   MDDR->core.phy.FIFO_WE_SLAVE_RATIO_CR[2] = 0x4010;
109   MDDR->core.phy.FIFO_WE_SLAVE_RATIO_CR[3] = 0x0000;
110   MDDR->core.phy.LOCAL_ODT_CR = 0x0001;
111   MDDR->core.phy.RD_DQS_SLAVE_RATIO_CR[0] = 0x0040;
112   MDDR->core.phy.RD_DQS_SLAVE_RATIO_CR[1] = 0x0401;
113   MDDR->core.phy.RD_DQS_SLAVE_RATIO_CR[2] = 0x4010;
114   MDDR->core.phy.WR_DATA_SLAVE_RATIO_CR[0] = 0x0040;
115   MDDR->core.phy.WR_DATA_SLAVE_RATIO_CR[1] = 0x0401;
116   MDDR->core.phy.WR_DATA_SLAVE_RATIO_CR[2] = 0x4010;
117   MDDR->core.phy.WR_RD_RL_CR = 0x0021;
118   MDDR->core.phy.RDC_WE_TO_RE_DELAY_CR = 0x0003;
119   MDDR->core.phy.USE_FIXED_RE_CR = 0x0001;
120   MDDR->core.phy.USE_RANK0_DELAYS_CR = 0x0001;
121   MDDR->core.phy.CONFIG_CR = 0x0009;
122   MDDR->core.phy.DYN_RESET_CR = 0x01;
123   MDDR->core.ddrc.DYN_SOFT_RESET_CR = 0x01;
124   // inclusive-language: enable
125   // Wait for config
126   while ((MDDR->core.ddrc.DDRC_SR) == 0x0000) {
127   }
128 #endif
129 }
130 
pw_boot_PreStaticConstructorInit()131 extern "C" void pw_boot_PreStaticConstructorInit() {
132   // TODO(skeys) add "#if no_bootLoader" and the functions needed for init.
133 
134 #if PW_MALLOC_ACTIVE
135   pw_MallocInit(&pw_boot_heap_low_addr, &pw_boot_heap_high_addr);
136 #endif  // PW_MALLOC_ACTIVE
137   pw_sys_io_Init();
138 }
139 
140 // TODO(amontanez): pw_boot_PreMainInit() should get renamed to
141 // pw_boot_FinalizeBoot or similar when main() is removed.
pw_boot_PreMainInit()142 extern "C" void pw_boot_PreMainInit() {
143   pw::system::Init();
144   vTaskStartScheduler();
145   PW_UNREACHABLE;
146 }
147 
sf2_SocInit()148 extern "C" void sf2_SocInit() {
149 #if SF2_MSS_NO_BOOTLOADER
150   Reset_Handler();
151 #endif
152   pw_boot_Entry();
153 }
154 
155 // This `main()` stub prevents another main function from being linked since
156 // this target deliberately doesn't run `main()`.
main()157 int main() {}
158 
pw_boot_PostMain()159 extern "C" PW_NO_RETURN void pw_boot_PostMain() {
160   // In case main() returns, just sit here until the device is reset.
161   while (true) {
162   }
163   PW_UNREACHABLE;
164 }
165