blob: 17ccdf2e6d889c0326490a85eb6f73125b72cf5b [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
51int platform_init(void)
52{
53 int err = 0;
54
55 SystemCoreClockUpdate(); /* From start up code */
56
57 /* UART init - will enable valid use of printf (stdout
58 * re-directed at this UART (UART0) */
59 UartStdOutInit();
60
61 if (0 != (err = verify_platform())) {
62 return err;
63 }
64
Isabella Gottardiee4920b2022-02-25 14:29:32 +000065#if defined(ARM_NPU)
66
Kshitij Sisodiad5679cc2022-03-03 16:30:07 +000067#if defined(ETHOS_U_NPU_TIMING_ADAPTER_ENABLED)
Isabella Gottardiee4920b2022-02-25 14:29:32 +000068 /* If the platform has timing adapter blocks along with Ethos-U core
69 * block, initialise them here. */
Kshitij Sisodiac22e80e2022-03-14 09:26:48 +000070 if (0 != (err = arm_ethosu_timing_adapter_init())) {
Isabella Gottardiee4920b2022-02-25 14:29:32 +000071 return err;
72 }
Kshitij Sisodiad5679cc2022-03-03 16:30:07 +000073#endif /* ETHOS_U_NPU_TIMING_ADAPTER_ENABLED */
Isabella Gottardiee4920b2022-02-25 14:29:32 +000074
75 int state;
76
77 /* If Arm Ethos-U NPU is to be used, we initialise it here */
Kshitij Sisodiac22e80e2022-03-14 09:26:48 +000078 if (0 != (state = arm_ethosu_npu_init())) {
Isabella Gottardiee4920b2022-02-25 14:29:32 +000079 return state;
80 }
81
82#endif /* ARM_NPU */
83
84 /* Print target design info */
85 info("Target system design: %s\n", DESIGN_NAME);
86
Kshitij Sisodiaa1256e32022-02-23 14:40:45 +000087 return 0;
88}
89
90void platform_release(void)
91{
92 __disable_irq();
93}
94
95void platform_name(char* name, size_t size)
96{
97 strncpy(name, DESIGN_NAME, size);
98}
99
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100100#define CREATE_MASK(msb, lsb) (int)(((1U << ((msb) - (lsb) + 1)) - 1) << (lsb))
101#define MASK_BITS(arg, msb, lsb) (int)((arg) & CREATE_MASK(msb, lsb))
102#define EXTRACT_BITS(arg, msb, lsb) (int)(MASK_BITS(arg, msb, lsb) >> (lsb))
alexander3c798932021-03-26 21:42:19 +0000103
Kshitij Sisodiaa1256e32022-02-23 14:40:45 +0000104static int verify_platform(void)
alexander3c798932021-03-26 21:42:19 +0000105{
alexander3c798932021-03-26 21:42:19 +0000106 uint32_t id = 0;
107 uint32_t fpgaid = 0;
108 uint32_t apnote = 0;
109 uint32_t rev = 0;
110 uint32_t aid = 0;
111 uint32_t fpga_clk = 0;
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100112 const uint32_t ascii_A = (uint32_t)('A');
alexander3c798932021-03-26 21:42:19 +0000113
114 /* Initialise the LEDs as the switches are */
115 MPS3_FPGAIO->LED = MPS3_FPGAIO->SWITCHES & 0xFF;
alexander3c798932021-03-26 21:42:19 +0000116
Kshitij Sisodiac22e80e2022-03-14 09:26:48 +0000117 info("Processor internal clock: %" PRIu32 "Hz\n", get_mps3_core_clock());
alexander3c798932021-03-26 21:42:19 +0000118
alexander3c798932021-03-26 21:42:19 +0000119 /* Get revision information from various registers */
120 rev = MPS3_SCC->CFG_REG4;
121 fpgaid = MPS3_SCC->SCC_ID;
122 aid = MPS3_SCC->SCC_AID;
123 apnote = EXTRACT_BITS(fpgaid, 15, 4);
Kshitij Sisodiac22e80e2022-03-14 09:26:48 +0000124 fpga_clk = get_mps3_core_clock();
alexander3c798932021-03-26 21:42:19 +0000125
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100126 info("V2M-MPS3 revision %c\n\n", (char)(rev + ascii_A));
127 info("Application Note AN%" PRIx32 ", Revision %c\n", apnote,
128 (char)(EXTRACT_BITS(aid, 23, 20) + ascii_A));
alexander3c798932021-03-26 21:42:19 +0000129 info("MPS3 build %d\n", EXTRACT_BITS(aid, 31, 24));
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100130 info("MPS3 core clock has been set to: %" PRIu32 "Hz\n", fpga_clk);
alexander3c798932021-03-26 21:42:19 +0000131
132 /* Display CPU ID */
133 id = SCB->CPUID;
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100134 info("CPU ID: 0x%08" PRIx32 "\n", id);
alexander3c798932021-03-26 21:42:19 +0000135
136 if(EXTRACT_BITS(id, 15, 8) == 0xD2) {
137 if (EXTRACT_BITS(id, 7, 4) == 2) {
138 info ("CPU: Cortex-M55 r%dp%d\n\n",
139 EXTRACT_BITS(id, 23, 20),EXTRACT_BITS(id, 3, 0));
140#if defined (CPU_CORTEX_M55)
141 /* CPU ID should be "0x_41_0f_d2_20" for Cortex-M55 */
142 return 0;
143#endif /* CPU_CORTEX_M55 */
144 } else if (EXTRACT_BITS(id, 7, 4) == 1) {
145 info ("CPU: Cortex-M33 r%dp%d\n\n",
146 EXTRACT_BITS(id, 23, 20),EXTRACT_BITS(id, 3, 0));
147#if defined (CPU_CORTEX_M33)
148 return 0;
149#endif /* CPU_CORTEX_M33 */
150 } else if (EXTRACT_BITS(id, 7, 4) == 0) {
151 info ("CPU: Cortex-M23 r%dp%d\n\n",
152 EXTRACT_BITS(id, 23, 20),EXTRACT_BITS(id, 3, 0));
153 } else {
154 info ("CPU: Cortex-M processor family");
155 }
156 } else if (EXTRACT_BITS(id, 15, 8) == 0xC6) {
157 info ("CPU: Cortex-M%d+ r%dp%d\n\n",
158 EXTRACT_BITS(id, 7, 4), EXTRACT_BITS(id, 23, 20),
159 EXTRACT_BITS(id, 3, 0));
160 } else {
161 info ("CPU: Cortex-M%d r%dp%d\n\n",
162 EXTRACT_BITS(id, 7, 4), EXTRACT_BITS(id, 23, 20),
163 EXTRACT_BITS(id, 3, 0));
164 }
alexander3c798932021-03-26 21:42:19 +0000165
166 /* If the CPU is anything other than M33 or M55, we return 1 */
167 printf_err("CPU mismatch!\n");
168 return 1;
169}