blob: a44c12f88fb14dad2439ef5ace0b74120d971aac [file] [log] [blame]
Kristofer Jonssonf62c3d72021-01-21 17:39:03 +01001/*
Anton Mobergfa3e51b2021-03-31 11:05:02 +02002 * Copyright (c) 2020-2021 Arm Limited. All rights reserved.
Kristofer Jonssonf62c3d72021-01-21 17:39:03 +01003 *
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 * Licensed under the Apache License, Version 2.0 (the License); you may
7 * not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
14 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19/****************************************************************************
20 * Includes
21 ****************************************************************************/
22
23#include "target.hpp"
24
25#ifdef ETHOSU
26#include <ethosu_driver.h>
27#endif
28
Jonny Svärd991af2b2021-04-15 17:31:01 +020029#include "mpu.hpp"
Jonny Svärdd6670902021-03-18 15:49:27 +010030#include <timing_adapter.h>
31
Kristofer Jonssonf62c3d72021-01-21 17:39:03 +010032#include "uart.h"
33
Jonny Svärdd6670902021-03-18 15:49:27 +010034#include <inttypes.h>
Kristofer Jonssonf62c3d72021-01-21 17:39:03 +010035#include <stdio.h>
Per Åstrand174e98d2021-02-09 17:48:53 +010036#include <stdlib.h>
Jonny Svärd991af2b2021-04-15 17:31:01 +020037#include <vector>
Kristofer Jonssonf62c3d72021-01-21 17:39:03 +010038
39using namespace EthosU;
40
41/****************************************************************************
42 * Defines
43 ****************************************************************************/
44
45#define ETHOSU_BASE_ADDRESS 0x48102000
Jonny Svärdd6670902021-03-18 15:49:27 +010046#define ETHOSU_IRQ 56
Kristofer Jonssonf62c3d72021-01-21 17:39:03 +010047
Jonny Svärdd6670902021-03-18 15:49:27 +010048#define ETHOSU0_TA0_BASE_ADDRESS 0x48103000
49#define ETHOSU0_TA1_BASE_ADDRESS 0x48103200
Kristofer Jonssonf62c3d72021-01-21 17:39:03 +010050
51/****************************************************************************
52 * Variables
53 ****************************************************************************/
54
55#if defined(ETHOSU_FAST_MEMORY_SIZE) && ETHOSU_FAST_MEMORY_SIZE > 0
56__attribute__((aligned(16), section(".bss.ethosu_scratch"))) uint8_t ethosu_scratch[ETHOSU_FAST_MEMORY_SIZE];
57#else
58#define ethosu_scratch 0
59#define ETHOSU_FAST_MEMORY_SIZE 0
60#endif
61
Jonny Svärd991af2b2021-04-15 17:31:01 +020062#ifdef ETHOSU
63struct ethosu_driver *ethosu0_driver = &ethosu_drv;
64#endif
65
Jonny Svärdd6670902021-03-18 15:49:27 +010066static uintptr_t ethosu_ta_base_addrs[ETHOSU_NPU_COUNT][ETHOSU_NPU_TA_COUNT] = {
67 {ETHOSU0_TA0_BASE_ADDRESS, ETHOSU0_TA1_BASE_ADDRESS}};
68struct timing_adapter ethosu_ta[ETHOSU_NPU_COUNT][ETHOSU_NPU_TA_COUNT];
69
Kristofer Jonssonf62c3d72021-01-21 17:39:03 +010070/****************************************************************************
71 * Cache maintenance
72 ****************************************************************************/
73
74#if defined(CPU_CACHE_ENABLE) && defined(__DCACHE_PRESENT) && (__DCACHE_PRESENT == 1U)
75extern "C" {
76void ethosu_flush_dcache(uint32_t *p, size_t bytes) {
77 if (p)
78 SCB_CleanDCache_by_Addr(p, bytes);
79 else
80 SCB_CleanDCache();
81}
82
83void ethosu_invalidate_dcache(uint32_t *p, size_t bytes) {
84 if (p)
85 SCB_InvalidateDCache_by_Addr(p, bytes);
86 else
87 SCB_InvalidateDCache();
88}
89}
90#endif
91
92/****************************************************************************
93 * Init
94 ****************************************************************************/
95
96namespace {
97
Per Åstrand174e98d2021-02-09 17:48:53 +010098extern "C" {
99struct ExcContext {
100 uint32_t r0;
101 uint32_t r1;
102 uint32_t r2;
103 uint32_t r3;
104 uint32_t r12;
105 uint32_t lr;
106 uint32_t pc;
107 uint32_t xPsr;
108};
109
110void HardFault_Handler() {
111 int irq;
112 struct ExcContext *e;
113 uint32_t sp;
114
115 asm volatile("mrs %0, ipsr \n" // Read IPSR (Exceptio number)
116 "sub %0, #16 \n" // Get it into IRQn_Type range
117 "tst lr, #4 \n" // Select the stack which was in use
118 "ite eq \n"
119 "mrseq %1, msp \n"
120 "mrsne %1, psp \n"
121 "mov %2, sp \n"
122 : "=r"(irq), "=r"(e), "=r"(sp));
123
124 printf("Hard fault. irq=%d, pc=0x%08" PRIx32 ", lr=0x%08" PRIx32 ", xpsr=0x%08" PRIx32 ", sp=0x%08" PRIx32 "\n",
125 irq,
126 e->pc,
127 e->lr,
128 e->xPsr,
129 sp);
130 printf(
131 "%11s cfsr=0x%08" PRIx32 " bfar=0x%08" PRIx32 " mmfar=0x%08" PRIx32 "\n", "", SCB->CFSR, SCB->BFAR, SCB->MMFAR);
132 exit(1);
133}
134}
135
Kristofer Jonssonf62c3d72021-01-21 17:39:03 +0100136#ifdef ETHOSU
137void ethosuIrqHandler() {
138 ethosu_irq_handler();
139}
140#endif
141
142} // namespace
143
144namespace EthosU {
145
146void targetSetup() {
147 // Initialize UART driver
148 uart_init();
149
Jonny Svärdd6670902021-03-18 15:49:27 +0100150 // Initialize timing adapter(s)
151 for (int i = 0; i < ETHOSU_NPU_COUNT; i++) {
152 for (int j = 0; j < ETHOSU_NPU_TA_COUNT; j++) {
153 if (ta_init(&ethosu_ta[i][j], ethosu_ta_base_addrs[i][j])) {
154 printf("Failed to initialize timing-adapter %d for NPU %d\n", j, i);
155 }
156 }
157 }
158
Kristofer Jonssonf62c3d72021-01-21 17:39:03 +0100159#ifdef ETHOSU
160 // Initialize Ethos-U NPU driver
Jonny Svärd991af2b2021-04-15 17:31:01 +0200161 if (ethosu_init_v4(ethosu0_driver,
162 reinterpret_cast<void *>(ETHOSU_BASE_ADDRESS),
163 ethosu_scratch,
164 ETHOSU_FAST_MEMORY_SIZE,
165 1,
166 1)) {
Kristofer Jonssonf62c3d72021-01-21 17:39:03 +0100167 printf("Failed to initialize NPU.\n");
168 return;
169 }
170
Jonny Svärd991af2b2021-04-15 17:31:01 +0200171 // Assumes SCB->VTOR point to RW memory
Kristofer Jonssonf62c3d72021-01-21 17:39:03 +0100172 NVIC_SetVector(static_cast<IRQn_Type>(ETHOSU_IRQ), (uint32_t)&ethosuIrqHandler);
173 NVIC_EnableIRQ(static_cast<IRQn_Type>(ETHOSU_IRQ));
174#endif
Jonny Svärd991af2b2021-04-15 17:31:01 +0200175
176 // MPU setup
177 const std::vector<ARM_MPU_Region_t> mpuConfig = {
178 {
179 // ITCM
180 ARM_MPU_RBAR(0x00000000, // Base
181 ARM_MPU_SH_NON, // Non-shareable
182 1, // Read-Only
183 1, // Non-Privileged
184 0), // eXecute Never disabled
185 ARM_MPU_RLAR(0x0007ffff, // Limit
186 Mpu::WTRA_index) // Attribute index - Write-Through, Read-allocate
187 },
188 {
189 // ITCM
190 ARM_MPU_RBAR(0x10000000, // Base
191 ARM_MPU_SH_NON, // Non-shareable
192 1, // Read-Only
193 1, // Non-Privileged
194 0), // eXecute Never disabled
195 ARM_MPU_RLAR(0x1007ffff, // Limit
196 Mpu::WTRA_index) // Attribute index - Write-Through, Read-allocate
197 },
198 {
199 // FPGA DATA SRAM; BRAM
200 ARM_MPU_RBAR(0x11000000, // Base
201 ARM_MPU_SH_NON, // Non-shareable
202 0, // Read-Write
203 1, // Non-Privileged
204 0), // eXecute Never disabled
205 ARM_MPU_RLAR(0x111fffff, // Limit
206 Mpu::WBWARA_index) // Attribute index - Write-Back, Write-Allocate, Read-allocate
207 },
208 {
209 // DTCM
210 ARM_MPU_RBAR(0x30000000, // Base
211 ARM_MPU_SH_NON, // Non-shareable
212 0, // Read-Write
213 1, // Non-Privileged
214 1), // eXecute Never enabled
215 ARM_MPU_RLAR(0x3007ffff, // Limit
216 Mpu::WBWARA_index) // Attribute index - Write-Back, Write-Allocate, Read-allocate
217 },
218 {
219 // SSE-300 internal SRAM
220 ARM_MPU_RBAR(0x31000000, // Base
221 ARM_MPU_SH_NON, // Non-shareable
222 0, // Read-Write
223 1, // Non-Privileged
224 1), // eXecute Never enabled
225 ARM_MPU_RLAR(0x313fffff, // Limit
226 Mpu::WBWARA_index) // Attribute index - Write-Back, Write-Allocate, Read-allocate
227 },
228 {
229 ARM_MPU_RBAR(0x70000000, // Base
230 ARM_MPU_SH_NON, // Non-shareable
231 0, // Read-Write
232 1, // Non-Privileged
233 1), // eXecute Never enabled
234 ARM_MPU_RLAR(0x7fffffff, // Limit
235 Mpu::WBWARA_index) // Attribute index - Write-Back, Write-Allocate, Read-allocate
236 }};
237
238 // Setup MPU configuration
239 Mpu::loadAndEnableConfig(&mpuConfig[0], mpuConfig.size());
240
241#if defined(CPU_CACHE_ENABLE) && defined(__DCACHE_PRESENT) && (__DCACHE_PRESENT == 1U)
242 SCB_EnableICache();
243 SCB_EnableDCache();
244#endif
Kristofer Jonssonf62c3d72021-01-21 17:39:03 +0100245}
246
247} // namespace EthosU