blob: 7c5de6d21d3418122c17d290ad5780383ee13fd5 [file] [log] [blame]
alexander3c798932021-03-26 21:42:19 +00001/*
Kshitij Sisodiaa1256e32022-02-23 14:40:45 +00002 * Copyright (c) 2022 Arm Limited. All rights reserved.
alexander3c798932021-03-26 21:42:19 +00003 * SPDX-License-Identifier: Apache-2.0
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
alexander3c798932021-03-26 21:42:19 +000017
Kshitij Sisodiaa1256e32022-02-23 14:40:45 +000018#include "platform_drivers.h"
alexander3c798932021-03-26 21:42:19 +000019
Kshitij Sisodiaa1256e32022-02-23 14:40:45 +000020#include "log_macros.h" /* Logging functions */
Kshitij Sisodiaacc6b852022-03-01 10:23:11 +000021#include "uart_stdout.h" /* stdout over UART. */
Kshitij Sisodiac22e80e2022-03-14 09:26:48 +000022#include "smm_mps3.h" /* Memory map for MPS3. */
Kshitij Sisodiad5679cc2022-03-03 16:30:07 +000023
Kshitij Sisodiaa1256e32022-02-23 14:40:45 +000024#include <string.h> /* For strncpy */
25
Isabella Gottardiee4920b2022-02-25 14:29:32 +000026#if defined(ARM_NPU)
27#include "ethosu_npu_init.h"
28
Kshitij Sisodiad5679cc2022-03-03 16:30:07 +000029#if defined(ETHOS_U_NPU_TIMING_ADAPTER_ENABLED)
Isabella Gottardiee4920b2022-02-25 14:29:32 +000030#include "ethosu_ta_init.h"
Kshitij Sisodiad5679cc2022-03-03 16:30:07 +000031#endif /* ETHOS_U_NPU_TIMING_ADAPTER_ENABLED */
Isabella Gottardiee4920b2022-02-25 14:29:32 +000032
Kshitij Sisodia8bc863d2022-03-24 17:53:34 +000033#if defined(ETHOS_U_BASE_ADDR)
34 #if (ETHOS_U_NPU_BASE != ETHOS_U_BASE_ADDR) && (SEC_ETHOS_U_NPU_BASE != ETHOS_U_BASE_ADDR)
35 #error "NPU component configured with incorrect NPU base address."
36 #endif /* (ETHOS_U_NPU_BASE != ETHOS_U_BASE_ADDR) && (SEC_ETHOS_U_NPU_BASE == ETHOS_U_BASE_ADDR) */
37#else
38 #error "ETHOS_U_BASE_ADDR should have been defined by the NPU component."
39#endif /* defined(ETHOS_U_BASE_ADDR) */
40
Isabella Gottardiee4920b2022-02-25 14:29:32 +000041#endif /* ARM_NPU */
42
Kshitij Sisodiaa1256e32022-02-23 14:40:45 +000043/**
44 * @brief Checks if the platform is valid by checking
45 * the CPU ID for the FPGA implementation against
46 * the register from the CPU core.
47 * @return 0 if successful, 1 otherwise
48 */
49static int verify_platform(void);
50
Kshitij Sisodia4cc40212022-04-08 09:54:53 +010051/** Platform name */
52static const char* s_platform_name = DESIGN_NAME;
53
Kshitij Sisodiaa1256e32022-02-23 14:40:45 +000054int platform_init(void)
55{
56 int err = 0;
57
58 SystemCoreClockUpdate(); /* From start up code */
59
60 /* UART init - will enable valid use of printf (stdout
61 * re-directed at this UART (UART0) */
62 UartStdOutInit();
63
64 if (0 != (err = verify_platform())) {
65 return err;
66 }
67
Kshitij Sisodiaff570342022-06-10 17:19:22 +010068#if defined(__ICACHE_PRESENT) && (__ICACHE_PRESENT == 1U)
69 info("Enabling I-cache.\n");
70 SCB_EnableICache();
71#endif /* __ICACHE_PRESENT */
72
73#if defined(__DCACHE_PRESENT) && (__DCACHE_PRESENT == 1U)
74 info("Enabling D-cache.\n");
75 SCB_EnableDCache();
76#endif /* __DCACHE_PRESENT */
77
Isabella Gottardiee4920b2022-02-25 14:29:32 +000078#if defined(ARM_NPU)
79
Kshitij Sisodiad5679cc2022-03-03 16:30:07 +000080#if defined(ETHOS_U_NPU_TIMING_ADAPTER_ENABLED)
Isabella Gottardiee4920b2022-02-25 14:29:32 +000081 /* If the platform has timing adapter blocks along with Ethos-U core
82 * block, initialise them here. */
Kshitij Sisodiac22e80e2022-03-14 09:26:48 +000083 if (0 != (err = arm_ethosu_timing_adapter_init())) {
Isabella Gottardiee4920b2022-02-25 14:29:32 +000084 return err;
85 }
Kshitij Sisodiad5679cc2022-03-03 16:30:07 +000086#endif /* ETHOS_U_NPU_TIMING_ADAPTER_ENABLED */
Isabella Gottardiee4920b2022-02-25 14:29:32 +000087
88 int state;
89
90 /* If Arm Ethos-U NPU is to be used, we initialise it here */
Kshitij Sisodiac22e80e2022-03-14 09:26:48 +000091 if (0 != (state = arm_ethosu_npu_init())) {
Isabella Gottardiee4920b2022-02-25 14:29:32 +000092 return state;
93 }
94
95#endif /* ARM_NPU */
96
97 /* Print target design info */
Kshitij Sisodia4cc40212022-04-08 09:54:53 +010098 info("Target system design: %s\n", s_platform_name);
Kshitij Sisodiaa1256e32022-02-23 14:40:45 +000099 return 0;
100}
101
102void platform_release(void)
103{
104 __disable_irq();
105}
106
Kshitij Sisodia4cc40212022-04-08 09:54:53 +0100107const char* platform_name(void)
Kshitij Sisodiaa1256e32022-02-23 14:40:45 +0000108{
Kshitij Sisodia4cc40212022-04-08 09:54:53 +0100109 return s_platform_name;
Kshitij Sisodiaa1256e32022-02-23 14:40:45 +0000110}
111
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100112#define CREATE_MASK(msb, lsb) (int)(((1U << ((msb) - (lsb) + 1)) - 1) << (lsb))
113#define MASK_BITS(arg, msb, lsb) (int)((arg) & CREATE_MASK(msb, lsb))
114#define EXTRACT_BITS(arg, msb, lsb) (int)(MASK_BITS(arg, msb, lsb) >> (lsb))
alexander3c798932021-03-26 21:42:19 +0000115
Kshitij Sisodiaa1256e32022-02-23 14:40:45 +0000116static int verify_platform(void)
alexander3c798932021-03-26 21:42:19 +0000117{
alexander3c798932021-03-26 21:42:19 +0000118 uint32_t id = 0;
119 uint32_t fpgaid = 0;
120 uint32_t apnote = 0;
121 uint32_t rev = 0;
122 uint32_t aid = 0;
123 uint32_t fpga_clk = 0;
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100124 const uint32_t ascii_A = (uint32_t)('A');
alexander3c798932021-03-26 21:42:19 +0000125
126 /* Initialise the LEDs as the switches are */
127 MPS3_FPGAIO->LED = MPS3_FPGAIO->SWITCHES & 0xFF;
alexander3c798932021-03-26 21:42:19 +0000128
Kshitij Sisodiac22e80e2022-03-14 09:26:48 +0000129 info("Processor internal clock: %" PRIu32 "Hz\n", get_mps3_core_clock());
alexander3c798932021-03-26 21:42:19 +0000130
alexander3c798932021-03-26 21:42:19 +0000131 /* Get revision information from various registers */
132 rev = MPS3_SCC->CFG_REG4;
133 fpgaid = MPS3_SCC->SCC_ID;
134 aid = MPS3_SCC->SCC_AID;
135 apnote = EXTRACT_BITS(fpgaid, 15, 4);
Kshitij Sisodiac22e80e2022-03-14 09:26:48 +0000136 fpga_clk = get_mps3_core_clock();
alexander3c798932021-03-26 21:42:19 +0000137
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100138 info("V2M-MPS3 revision %c\n\n", (char)(rev + ascii_A));
139 info("Application Note AN%" PRIx32 ", Revision %c\n", apnote,
140 (char)(EXTRACT_BITS(aid, 23, 20) + ascii_A));
alexander3c798932021-03-26 21:42:19 +0000141 info("MPS3 build %d\n", EXTRACT_BITS(aid, 31, 24));
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100142 info("MPS3 core clock has been set to: %" PRIu32 "Hz\n", fpga_clk);
alexander3c798932021-03-26 21:42:19 +0000143
144 /* Display CPU ID */
145 id = SCB->CPUID;
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100146 info("CPU ID: 0x%08" PRIx32 "\n", id);
alexander3c798932021-03-26 21:42:19 +0000147
148 if(EXTRACT_BITS(id, 15, 8) == 0xD2) {
Kshitij Sisodia8c61c0a2022-05-17 11:16:22 +0100149 if (EXTRACT_BITS(id, 7, 4) == 3) {
150 info ("CPU: Cortex-M85 r%dp%d\n\n",
151 EXTRACT_BITS(id, 23, 20),EXTRACT_BITS(id, 3, 0));
152 /* @TODO: Remove CPU_CORTEX_M55 from here once CMake min version is > 3.21.0 or when
153 * toolchains officially support Cortex-M85. Currently, for CMake versions older than
154 * this, Cortex-M85 is built using Cortex-M55 flags. */
155#if defined (CPU_CORTEX_M55) || defined (ARMv81MML_DSP_DP_MVE_FP) || defined(CPU_CORTEX_M85)
156 /* CPU ID should be "0x_41_0f_d2_30" for Cortex-M85 */
157 return 0;
158#endif /* (CPU_CORTEX_M55) || (ARMv81MML_DSP_DP_MVE_FP) || (CPU_CORTEX_M85) */
159 } else if (EXTRACT_BITS(id, 7, 4) == 2) {
alexander3c798932021-03-26 21:42:19 +0000160 info ("CPU: Cortex-M55 r%dp%d\n\n",
161 EXTRACT_BITS(id, 23, 20),EXTRACT_BITS(id, 3, 0));
162#if defined (CPU_CORTEX_M55)
163 /* CPU ID should be "0x_41_0f_d2_20" for Cortex-M55 */
164 return 0;
165#endif /* CPU_CORTEX_M55 */
166 } else if (EXTRACT_BITS(id, 7, 4) == 1) {
167 info ("CPU: Cortex-M33 r%dp%d\n\n",
168 EXTRACT_BITS(id, 23, 20),EXTRACT_BITS(id, 3, 0));
169#if defined (CPU_CORTEX_M33)
170 return 0;
171#endif /* CPU_CORTEX_M33 */
172 } else if (EXTRACT_BITS(id, 7, 4) == 0) {
173 info ("CPU: Cortex-M23 r%dp%d\n\n",
174 EXTRACT_BITS(id, 23, 20),EXTRACT_BITS(id, 3, 0));
175 } else {
176 info ("CPU: Cortex-M processor family");
177 }
178 } else if (EXTRACT_BITS(id, 15, 8) == 0xC6) {
179 info ("CPU: Cortex-M%d+ r%dp%d\n\n",
180 EXTRACT_BITS(id, 7, 4), EXTRACT_BITS(id, 23, 20),
181 EXTRACT_BITS(id, 3, 0));
182 } else {
183 info ("CPU: Cortex-M%d r%dp%d\n\n",
184 EXTRACT_BITS(id, 7, 4), EXTRACT_BITS(id, 23, 20),
185 EXTRACT_BITS(id, 3, 0));
186 }
alexander3c798932021-03-26 21:42:19 +0000187
188 /* If the CPU is anything other than M33 or M55, we return 1 */
189 printf_err("CPU mismatch!\n");
190 return 1;
191}