blob: d1c3da27a6e460fcfed02faa0de61c340dc5d618 [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
Isabella Gottardiee4920b2022-02-25 14:29:32 +000068#if defined(ARM_NPU)
69
Kshitij Sisodiad5679cc2022-03-03 16:30:07 +000070#if defined(ETHOS_U_NPU_TIMING_ADAPTER_ENABLED)
Isabella Gottardiee4920b2022-02-25 14:29:32 +000071 /* If the platform has timing adapter blocks along with Ethos-U core
72 * block, initialise them here. */
Kshitij Sisodiac22e80e2022-03-14 09:26:48 +000073 if (0 != (err = arm_ethosu_timing_adapter_init())) {
Isabella Gottardiee4920b2022-02-25 14:29:32 +000074 return err;
75 }
Kshitij Sisodiad5679cc2022-03-03 16:30:07 +000076#endif /* ETHOS_U_NPU_TIMING_ADAPTER_ENABLED */
Isabella Gottardiee4920b2022-02-25 14:29:32 +000077
78 int state;
79
80 /* If Arm Ethos-U NPU is to be used, we initialise it here */
Kshitij Sisodiac22e80e2022-03-14 09:26:48 +000081 if (0 != (state = arm_ethosu_npu_init())) {
Isabella Gottardiee4920b2022-02-25 14:29:32 +000082 return state;
83 }
84
85#endif /* ARM_NPU */
86
87 /* Print target design info */
Kshitij Sisodia4cc40212022-04-08 09:54:53 +010088 info("Target system design: %s\n", s_platform_name);
Kshitij Sisodiaa1256e32022-02-23 14:40:45 +000089 return 0;
90}
91
92void platform_release(void)
93{
94 __disable_irq();
95}
96
Kshitij Sisodia4cc40212022-04-08 09:54:53 +010097const char* platform_name(void)
Kshitij Sisodiaa1256e32022-02-23 14:40:45 +000098{
Kshitij Sisodia4cc40212022-04-08 09:54:53 +010099 return s_platform_name;
Kshitij Sisodiaa1256e32022-02-23 14:40:45 +0000100}
101
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100102#define CREATE_MASK(msb, lsb) (int)(((1U << ((msb) - (lsb) + 1)) - 1) << (lsb))
103#define MASK_BITS(arg, msb, lsb) (int)((arg) & CREATE_MASK(msb, lsb))
104#define EXTRACT_BITS(arg, msb, lsb) (int)(MASK_BITS(arg, msb, lsb) >> (lsb))
alexander3c798932021-03-26 21:42:19 +0000105
Kshitij Sisodiaa1256e32022-02-23 14:40:45 +0000106static int verify_platform(void)
alexander3c798932021-03-26 21:42:19 +0000107{
alexander3c798932021-03-26 21:42:19 +0000108 uint32_t id = 0;
109 uint32_t fpgaid = 0;
110 uint32_t apnote = 0;
111 uint32_t rev = 0;
112 uint32_t aid = 0;
113 uint32_t fpga_clk = 0;
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100114 const uint32_t ascii_A = (uint32_t)('A');
alexander3c798932021-03-26 21:42:19 +0000115
116 /* Initialise the LEDs as the switches are */
117 MPS3_FPGAIO->LED = MPS3_FPGAIO->SWITCHES & 0xFF;
alexander3c798932021-03-26 21:42:19 +0000118
Kshitij Sisodiac22e80e2022-03-14 09:26:48 +0000119 info("Processor internal clock: %" PRIu32 "Hz\n", get_mps3_core_clock());
alexander3c798932021-03-26 21:42:19 +0000120
alexander3c798932021-03-26 21:42:19 +0000121 /* Get revision information from various registers */
122 rev = MPS3_SCC->CFG_REG4;
123 fpgaid = MPS3_SCC->SCC_ID;
124 aid = MPS3_SCC->SCC_AID;
125 apnote = EXTRACT_BITS(fpgaid, 15, 4);
Kshitij Sisodiac22e80e2022-03-14 09:26:48 +0000126 fpga_clk = get_mps3_core_clock();
alexander3c798932021-03-26 21:42:19 +0000127
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100128 info("V2M-MPS3 revision %c\n\n", (char)(rev + ascii_A));
129 info("Application Note AN%" PRIx32 ", Revision %c\n", apnote,
130 (char)(EXTRACT_BITS(aid, 23, 20) + ascii_A));
alexander3c798932021-03-26 21:42:19 +0000131 info("MPS3 build %d\n", EXTRACT_BITS(aid, 31, 24));
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100132 info("MPS3 core clock has been set to: %" PRIu32 "Hz\n", fpga_clk);
alexander3c798932021-03-26 21:42:19 +0000133
134 /* Display CPU ID */
135 id = SCB->CPUID;
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100136 info("CPU ID: 0x%08" PRIx32 "\n", id);
alexander3c798932021-03-26 21:42:19 +0000137
138 if(EXTRACT_BITS(id, 15, 8) == 0xD2) {
139 if (EXTRACT_BITS(id, 7, 4) == 2) {
140 info ("CPU: Cortex-M55 r%dp%d\n\n",
141 EXTRACT_BITS(id, 23, 20),EXTRACT_BITS(id, 3, 0));
142#if defined (CPU_CORTEX_M55)
143 /* CPU ID should be "0x_41_0f_d2_20" for Cortex-M55 */
144 return 0;
145#endif /* CPU_CORTEX_M55 */
146 } else if (EXTRACT_BITS(id, 7, 4) == 1) {
147 info ("CPU: Cortex-M33 r%dp%d\n\n",
148 EXTRACT_BITS(id, 23, 20),EXTRACT_BITS(id, 3, 0));
149#if defined (CPU_CORTEX_M33)
150 return 0;
151#endif /* CPU_CORTEX_M33 */
152 } else if (EXTRACT_BITS(id, 7, 4) == 0) {
153 info ("CPU: Cortex-M23 r%dp%d\n\n",
154 EXTRACT_BITS(id, 23, 20),EXTRACT_BITS(id, 3, 0));
155 } else {
156 info ("CPU: Cortex-M processor family");
157 }
158 } else if (EXTRACT_BITS(id, 15, 8) == 0xC6) {
159 info ("CPU: Cortex-M%d+ r%dp%d\n\n",
160 EXTRACT_BITS(id, 7, 4), EXTRACT_BITS(id, 23, 20),
161 EXTRACT_BITS(id, 3, 0));
162 } else {
163 info ("CPU: Cortex-M%d r%dp%d\n\n",
164 EXTRACT_BITS(id, 7, 4), EXTRACT_BITS(id, 23, 20),
165 EXTRACT_BITS(id, 3, 0));
166 }
alexander3c798932021-03-26 21:42:19 +0000167
168 /* If the CPU is anything other than M33 or M55, we return 1 */
169 printf_err("CPU mismatch!\n");
170 return 1;
171}