blob: 73b388b604dd82864fbbe5703fc5bc557713d123 [file] [log] [blame]
alexander3c798932021-03-26 21:42:19 +00001/*
Richard Burtonbcec6752023-11-03 16:21:58 +00002 * SPDX-FileCopyrightText: Copyright 2022-2023 Arm Limited and/or its affiliates <open-source-office@arm.com>
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));
Richard Burtonbcec6752023-11-03 16:21:58 +0000152#if defined (ARMv81MML_DSP_DP_MVE_FP) || defined (CPU_CORTEX_M85)
Kshitij Sisodia8c61c0a2022-05-17 11:16:22 +0100153 /* CPU ID should be "0x_41_0f_d2_30" for Cortex-M85 */
154 return 0;
Richard Burtonbcec6752023-11-03 16:21:58 +0000155#endif /* (ARMv81MML_DSP_DP_MVE_FP) || (CPU_CORTEX_M85) */
Kshitij Sisodia8c61c0a2022-05-17 11:16:22 +0100156 } else if (EXTRACT_BITS(id, 7, 4) == 2) {
alexander3c798932021-03-26 21:42:19 +0000157 info ("CPU: Cortex-M55 r%dp%d\n\n",
158 EXTRACT_BITS(id, 23, 20),EXTRACT_BITS(id, 3, 0));
159#if defined (CPU_CORTEX_M55)
160 /* CPU ID should be "0x_41_0f_d2_20" for Cortex-M55 */
161 return 0;
162#endif /* CPU_CORTEX_M55 */
163 } else if (EXTRACT_BITS(id, 7, 4) == 1) {
164 info ("CPU: Cortex-M33 r%dp%d\n\n",
165 EXTRACT_BITS(id, 23, 20),EXTRACT_BITS(id, 3, 0));
166#if defined (CPU_CORTEX_M33)
167 return 0;
168#endif /* CPU_CORTEX_M33 */
169 } else if (EXTRACT_BITS(id, 7, 4) == 0) {
170 info ("CPU: Cortex-M23 r%dp%d\n\n",
171 EXTRACT_BITS(id, 23, 20),EXTRACT_BITS(id, 3, 0));
172 } else {
173 info ("CPU: Cortex-M processor family");
174 }
175 } else if (EXTRACT_BITS(id, 15, 8) == 0xC6) {
176 info ("CPU: Cortex-M%d+ r%dp%d\n\n",
177 EXTRACT_BITS(id, 7, 4), EXTRACT_BITS(id, 23, 20),
178 EXTRACT_BITS(id, 3, 0));
179 } else {
180 info ("CPU: Cortex-M%d r%dp%d\n\n",
181 EXTRACT_BITS(id, 7, 4), EXTRACT_BITS(id, 23, 20),
182 EXTRACT_BITS(id, 3, 0));
183 }
alexander3c798932021-03-26 21:42:19 +0000184
Richard Burtonbcec6752023-11-03 16:21:58 +0000185 /* If the CPU is anything other than M33, M55 or M85, we return 1 */
alexander3c798932021-03-26 21:42:19 +0000186 printf_err("CPU mismatch!\n");
187 return 1;
188}