blob: 8f8db8339ceef0de90129bce005b2a262d341fe9 [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>
Kristofer Jonsson7b8fa5e2021-12-16 14:02:56 +010027#include <timing_adapter.h>
Kristofer Jonssonf62c3d72021-01-21 17:39:03 +010028#endif
29
Jonny Svärd991af2b2021-04-15 17:31:01 +020030#include "mpu.hpp"
Yulia Garboviche9cdc632021-11-23 20:00:04 +020031#include "uart_stdout.h"
Kristofer Jonssonf62c3d72021-01-21 17:39:03 +010032
Jonny Svärdd6670902021-03-18 15:49:27 +010033#include <inttypes.h>
Kristofer Jonssonf62c3d72021-01-21 17:39:03 +010034#include <stdio.h>
Per Åstrand174e98d2021-02-09 17:48:53 +010035#include <stdlib.h>
Jonny Svärd991af2b2021-04-15 17:31:01 +020036#include <vector>
Kristofer Jonssonf62c3d72021-01-21 17:39:03 +010037
38using namespace EthosU;
39
40/****************************************************************************
41 * Defines
42 ****************************************************************************/
43
44#define ETHOSU_BASE_ADDRESS 0x48102000
Jonny Svärdd6670902021-03-18 15:49:27 +010045#define ETHOSU_IRQ 56
Kristofer Jonssonf62c3d72021-01-21 17:39:03 +010046
Jonny Svärdd6670902021-03-18 15:49:27 +010047#define ETHOSU0_TA0_BASE_ADDRESS 0x48103000
48#define ETHOSU0_TA1_BASE_ADDRESS 0x48103200
Kristofer Jonssonf62c3d72021-01-21 17:39:03 +010049
50/****************************************************************************
51 * Variables
52 ****************************************************************************/
53
54#if defined(ETHOSU_FAST_MEMORY_SIZE) && ETHOSU_FAST_MEMORY_SIZE > 0
55__attribute__((aligned(16), section(".bss.ethosu_scratch"))) uint8_t ethosu_scratch[ETHOSU_FAST_MEMORY_SIZE];
56#else
57#define ethosu_scratch 0
58#define ETHOSU_FAST_MEMORY_SIZE 0
59#endif
60
Jonny Svärd991af2b2021-04-15 17:31:01 +020061#ifdef ETHOSU
Jonny Svärdc5941c42021-06-01 18:40:45 +020062struct ethosu_driver ethosu0_driver;
Jonny Svärd991af2b2021-04-15 17:31:01 +020063#endif
64
Nir Ekhauza58edd82021-10-04 12:21:17 +030065/****************************************************************************
66 * Timing Adapters
67 ****************************************************************************/
68
Kristofer Jonsson7b8fa5e2021-12-16 14:02:56 +010069#ifdef ETHOSU
70
Kristofer Jonsson64f37cd2021-10-19 10:54:47 +020071#ifndef ETHOSU_TA_MAXR_0
Nir Ekhauza58edd82021-10-04 12:21:17 +030072#define ETHOSU_TA_MAXR_0 0
73#endif
Kristofer Jonsson64f37cd2021-10-19 10:54:47 +020074
75#ifndef ETHOSU_TA_MAXW_0
Nir Ekhauza58edd82021-10-04 12:21:17 +030076#define ETHOSU_TA_MAXW_0 0
77#endif
Kristofer Jonsson64f37cd2021-10-19 10:54:47 +020078
79#ifndef ETHOSU_TA_MAXRW_0
Nir Ekhauza58edd82021-10-04 12:21:17 +030080#define ETHOSU_TA_MAXRW_0 0
81#endif
Kristofer Jonsson64f37cd2021-10-19 10:54:47 +020082
83#ifndef ETHOSU_TA_RLATENCY_0
Nir Ekhauza58edd82021-10-04 12:21:17 +030084#define ETHOSU_TA_RLATENCY_0 0
85#endif
Kristofer Jonsson64f37cd2021-10-19 10:54:47 +020086
87#ifndef ETHOSU_TA_WLATENCY_0
Nir Ekhauza58edd82021-10-04 12:21:17 +030088#define ETHOSU_TA_WLATENCY_0 0
89#endif
Kristofer Jonsson64f37cd2021-10-19 10:54:47 +020090
91#ifndef ETHOSU_TA_PULSE_ON_0
Nir Ekhauza58edd82021-10-04 12:21:17 +030092#define ETHOSU_TA_PULSE_ON_0 0
93#endif
Kristofer Jonsson64f37cd2021-10-19 10:54:47 +020094
95#ifndef ETHOSU_TA_PULSE_OFF_0
Nir Ekhauza58edd82021-10-04 12:21:17 +030096#define ETHOSU_TA_PULSE_OFF_0 0
97#endif
Kristofer Jonsson64f37cd2021-10-19 10:54:47 +020098
99#ifndef ETHOSU_TA_BWCAP_0
Nir Ekhauza58edd82021-10-04 12:21:17 +0300100#define ETHOSU_TA_BWCAP_0 0
101#endif
Kristofer Jonsson64f37cd2021-10-19 10:54:47 +0200102
103#ifndef ETHOSU_TA_PERFCTRL_0
Nir Ekhauza58edd82021-10-04 12:21:17 +0300104#define ETHOSU_TA_PERFCTRL_0 0
105#endif
Kristofer Jonsson64f37cd2021-10-19 10:54:47 +0200106
107#ifndef ETHOSU_TA_PERFCNT_0
Nir Ekhauza58edd82021-10-04 12:21:17 +0300108#define ETHOSU_TA_PERFCNT_0 0
109#endif
Kristofer Jonsson64f37cd2021-10-19 10:54:47 +0200110
111#ifndef ETHOSU_TA_MODE_0
112#define ETHOSU_TA_MODE_0 1
Nir Ekhauza58edd82021-10-04 12:21:17 +0300113#endif
Kristofer Jonsson64f37cd2021-10-19 10:54:47 +0200114
115#ifndef ETHOSU_TA_HISTBIN_0
Nir Ekhauza58edd82021-10-04 12:21:17 +0300116#define ETHOSU_TA_HISTBIN_0 0
117#endif
Kristofer Jonsson64f37cd2021-10-19 10:54:47 +0200118
119#ifndef ETHOSU_TA_HISTCNT_0
Nir Ekhauza58edd82021-10-04 12:21:17 +0300120#define ETHOSU_TA_HISTCNT_0 0
121#endif
122
Kristofer Jonsson64f37cd2021-10-19 10:54:47 +0200123#ifndef ETHOSU_TA_MAXR_1
Nir Ekhauza58edd82021-10-04 12:21:17 +0300124#define ETHOSU_TA_MAXR_1 0
125#endif
Kristofer Jonsson64f37cd2021-10-19 10:54:47 +0200126
127#ifndef ETHOSU_TA_MAXW_1
Nir Ekhauza58edd82021-10-04 12:21:17 +0300128#define ETHOSU_TA_MAXW_1 0
129#endif
Kristofer Jonsson64f37cd2021-10-19 10:54:47 +0200130
131#ifndef ETHOSU_TA_MAXRW_1
Nir Ekhauza58edd82021-10-04 12:21:17 +0300132#define ETHOSU_TA_MAXRW_1 0
133#endif
Kristofer Jonsson64f37cd2021-10-19 10:54:47 +0200134
135#ifndef ETHOSU_TA_RLATENCY_1
Nir Ekhauza58edd82021-10-04 12:21:17 +0300136#define ETHOSU_TA_RLATENCY_1 0
137#endif
Kristofer Jonsson64f37cd2021-10-19 10:54:47 +0200138
139#ifndef ETHOSU_TA_WLATENCY_1
Nir Ekhauza58edd82021-10-04 12:21:17 +0300140#define ETHOSU_TA_WLATENCY_1 0
141#endif
Kristofer Jonsson64f37cd2021-10-19 10:54:47 +0200142
143#ifndef ETHOSU_TA_PULSE_ON_1
Nir Ekhauza58edd82021-10-04 12:21:17 +0300144#define ETHOSU_TA_PULSE_ON_1 0
145#endif
Kristofer Jonsson64f37cd2021-10-19 10:54:47 +0200146
147#ifndef ETHOSU_TA_PULSE_OFF_1
Nir Ekhauza58edd82021-10-04 12:21:17 +0300148#define ETHOSU_TA_PULSE_OFF_1 0
149#endif
Kristofer Jonsson64f37cd2021-10-19 10:54:47 +0200150
151#ifndef ETHOSU_TA_BWCAP_1
Nir Ekhauza58edd82021-10-04 12:21:17 +0300152#define ETHOSU_TA_BWCAP_1 0
153#endif
Kristofer Jonsson64f37cd2021-10-19 10:54:47 +0200154
155#ifndef ETHOSU_TA_PERFCTRL_1
Nir Ekhauza58edd82021-10-04 12:21:17 +0300156#define ETHOSU_TA_PERFCTRL_1 0
157#endif
Kristofer Jonsson64f37cd2021-10-19 10:54:47 +0200158
159#ifndef ETHOSU_TA_PERFCNT_1
Nir Ekhauza58edd82021-10-04 12:21:17 +0300160#define ETHOSU_TA_PERFCNT_1 0
161#endif
Kristofer Jonsson64f37cd2021-10-19 10:54:47 +0200162
163#ifndef ETHOSU_TA_MODE_1
164#define ETHOSU_TA_MODE_1 1
Nir Ekhauza58edd82021-10-04 12:21:17 +0300165#endif
Kristofer Jonsson64f37cd2021-10-19 10:54:47 +0200166
167#ifndef ETHOSU_TA_HISTBIN_1
Nir Ekhauza58edd82021-10-04 12:21:17 +0300168#define ETHOSU_TA_HISTBIN_1 0
169#endif
Kristofer Jonsson64f37cd2021-10-19 10:54:47 +0200170
171#ifndef ETHOSU_TA_HISTCNT_1
Nir Ekhauza58edd82021-10-04 12:21:17 +0300172#define ETHOSU_TA_HISTCNT_1 0
173#endif
174
Jonny Svärdd6670902021-03-18 15:49:27 +0100175static uintptr_t ethosu_ta_base_addrs[ETHOSU_NPU_COUNT][ETHOSU_NPU_TA_COUNT] = {
176 {ETHOSU0_TA0_BASE_ADDRESS, ETHOSU0_TA1_BASE_ADDRESS}};
177struct timing_adapter ethosu_ta[ETHOSU_NPU_COUNT][ETHOSU_NPU_TA_COUNT];
Nir Ekhauza58edd82021-10-04 12:21:17 +0300178struct timing_adapter_settings ethosu_ta_settings[ETHOSU_NPU_TA_COUNT] = {{ETHOSU_TA_MAXR_0,
179 ETHOSU_TA_MAXW_0,
180 ETHOSU_TA_MAXRW_0,
181 ETHOSU_TA_RLATENCY_0,
182 ETHOSU_TA_WLATENCY_0,
183 ETHOSU_TA_PULSE_ON_0,
184 ETHOSU_TA_PULSE_OFF_0,
185 ETHOSU_TA_BWCAP_0,
186 ETHOSU_TA_PERFCTRL_0,
187 ETHOSU_TA_PERFCNT_0,
188 ETHOSU_TA_MODE_0,
Kristofer Jonsson29467e02021-11-26 16:10:43 +0100189 0, // Read only register
Nir Ekhauza58edd82021-10-04 12:21:17 +0300190 ETHOSU_TA_HISTBIN_0,
191 ETHOSU_TA_HISTCNT_0},
192 {ETHOSU_TA_MAXR_1,
193 ETHOSU_TA_MAXW_1,
194 ETHOSU_TA_MAXRW_1,
195 ETHOSU_TA_RLATENCY_1,
196 ETHOSU_TA_WLATENCY_1,
197 ETHOSU_TA_PULSE_ON_1,
198 ETHOSU_TA_PULSE_OFF_1,
199 ETHOSU_TA_BWCAP_1,
200 ETHOSU_TA_PERFCTRL_1,
201 ETHOSU_TA_PERFCNT_1,
202 ETHOSU_TA_MODE_1,
Kristofer Jonsson29467e02021-11-26 16:10:43 +0100203 0, // Read only register
Nir Ekhauza58edd82021-10-04 12:21:17 +0300204 ETHOSU_TA_HISTBIN_1,
205 ETHOSU_TA_HISTCNT_1}};
Kristofer Jonsson29467e02021-11-26 16:10:43 +0100206
Kristofer Jonsson7b8fa5e2021-12-16 14:02:56 +0100207#endif
208
Kristofer Jonssonf62c3d72021-01-21 17:39:03 +0100209/****************************************************************************
210 * Cache maintenance
211 ****************************************************************************/
212
213#if defined(CPU_CACHE_ENABLE) && defined(__DCACHE_PRESENT) && (__DCACHE_PRESENT == 1U)
214extern "C" {
215void ethosu_flush_dcache(uint32_t *p, size_t bytes) {
216 if (p)
217 SCB_CleanDCache_by_Addr(p, bytes);
218 else
219 SCB_CleanDCache();
220}
221
222void ethosu_invalidate_dcache(uint32_t *p, size_t bytes) {
223 if (p)
224 SCB_InvalidateDCache_by_Addr(p, bytes);
225 else
226 SCB_InvalidateDCache();
227}
228}
229#endif
230
231/****************************************************************************
232 * Init
233 ****************************************************************************/
234
235namespace {
236
Per Åstrand174e98d2021-02-09 17:48:53 +0100237extern "C" {
238struct ExcContext {
239 uint32_t r0;
240 uint32_t r1;
241 uint32_t r2;
242 uint32_t r3;
243 uint32_t r12;
244 uint32_t lr;
245 uint32_t pc;
246 uint32_t xPsr;
247};
248
249void HardFault_Handler() {
250 int irq;
251 struct ExcContext *e;
252 uint32_t sp;
253
254 asm volatile("mrs %0, ipsr \n" // Read IPSR (Exceptio number)
255 "sub %0, #16 \n" // Get it into IRQn_Type range
256 "tst lr, #4 \n" // Select the stack which was in use
257 "ite eq \n"
258 "mrseq %1, msp \n"
259 "mrsne %1, psp \n"
260 "mov %2, sp \n"
261 : "=r"(irq), "=r"(e), "=r"(sp));
262
263 printf("Hard fault. irq=%d, pc=0x%08" PRIx32 ", lr=0x%08" PRIx32 ", xpsr=0x%08" PRIx32 ", sp=0x%08" PRIx32 "\n",
264 irq,
265 e->pc,
266 e->lr,
267 e->xPsr,
268 sp);
269 printf(
270 "%11s cfsr=0x%08" PRIx32 " bfar=0x%08" PRIx32 " mmfar=0x%08" PRIx32 "\n", "", SCB->CFSR, SCB->BFAR, SCB->MMFAR);
271 exit(1);
272}
273}
274
Kristofer Jonssonf62c3d72021-01-21 17:39:03 +0100275#ifdef ETHOSU
276void ethosuIrqHandler() {
Jonny Svärdc5941c42021-06-01 18:40:45 +0200277 ethosu_irq_handler(&ethosu0_driver);
Kristofer Jonssonf62c3d72021-01-21 17:39:03 +0100278}
279#endif
280
281} // namespace
282
283namespace EthosU {
284
285void targetSetup() {
286 // Initialize UART driver
Yulia Garboviche9cdc632021-11-23 20:00:04 +0200287 UartStdOutInit();
Kristofer Jonssonf62c3d72021-01-21 17:39:03 +0100288
Kristofer Jonsson7b8fa5e2021-12-16 14:02:56 +0100289#ifdef ETHOSU
Jonny Svärdd6670902021-03-18 15:49:27 +0100290 // Initialize timing adapter(s)
291 for (int i = 0; i < ETHOSU_NPU_COUNT; i++) {
292 for (int j = 0; j < ETHOSU_NPU_TA_COUNT; j++) {
293 if (ta_init(&ethosu_ta[i][j], ethosu_ta_base_addrs[i][j])) {
294 printf("Failed to initialize timing-adapter %d for NPU %d\n", j, i);
Nir Ekhauza58edd82021-10-04 12:21:17 +0300295 } else {
296 // Set the updated configuration
297 ta_set_all(&ethosu_ta[i][j], &ethosu_ta_settings[j]);
Jonny Svärdd6670902021-03-18 15:49:27 +0100298 }
299 }
300 }
301
Kristofer Jonssonf62c3d72021-01-21 17:39:03 +0100302 // Initialize Ethos-U NPU driver
Jonny Svärdc5941c42021-06-01 18:40:45 +0200303 if (ethosu_init(&ethosu0_driver,
Anton Mobergebe9a152021-05-03 09:28:52 +0200304 reinterpret_cast<void *>(ETHOSU_BASE_ADDRESS),
305 ethosu_scratch,
306 ETHOSU_FAST_MEMORY_SIZE,
307 1,
308 1)) {
Kristofer Jonssonf62c3d72021-01-21 17:39:03 +0100309 printf("Failed to initialize NPU.\n");
310 return;
311 }
312
Jonny Svärd991af2b2021-04-15 17:31:01 +0200313 // Assumes SCB->VTOR point to RW memory
Kristofer Jonssonf62c3d72021-01-21 17:39:03 +0100314 NVIC_SetVector(static_cast<IRQn_Type>(ETHOSU_IRQ), (uint32_t)&ethosuIrqHandler);
315 NVIC_EnableIRQ(static_cast<IRQn_Type>(ETHOSU_IRQ));
316#endif
Jonny Svärd991af2b2021-04-15 17:31:01 +0200317
318 // MPU setup
319 const std::vector<ARM_MPU_Region_t> mpuConfig = {
320 {
Jonny Svärde3b6b962021-04-27 11:32:31 +0200321 // ITCM (NS)
Jonny Svärd991af2b2021-04-15 17:31:01 +0200322 ARM_MPU_RBAR(0x00000000, // Base
323 ARM_MPU_SH_NON, // Non-shareable
324 1, // Read-Only
325 1, // Non-Privileged
326 0), // eXecute Never disabled
327 ARM_MPU_RLAR(0x0007ffff, // Limit
328 Mpu::WTRA_index) // Attribute index - Write-Through, Read-allocate
329 },
330 {
Jonny Svärde3b6b962021-04-27 11:32:31 +0200331 // ITCM (S)
Jonny Svärd991af2b2021-04-15 17:31:01 +0200332 ARM_MPU_RBAR(0x10000000, // Base
333 ARM_MPU_SH_NON, // Non-shareable
334 1, // Read-Only
335 1, // Non-Privileged
336 0), // eXecute Never disabled
337 ARM_MPU_RLAR(0x1007ffff, // Limit
338 Mpu::WTRA_index) // Attribute index - Write-Through, Read-allocate
339 },
340 {
Jonny Svärde3b6b962021-04-27 11:32:31 +0200341 // FPGA DATA SRAM; BRAM (NS)
342 ARM_MPU_RBAR(0x01000000, // Base
343 ARM_MPU_SH_NON, // Non-shareable
344 0, // Read-Write
345 1, // Non-Privileged
346 0), // eXecute Never disabled
347 ARM_MPU_RLAR(0x011fffff, // Limit
348 Mpu::WBWARA_index) // Attribute index - Write-Back, Write-Allocate, Read-allocate
349 },
350 {
351 // FPGA DATA SRAM; BRAM (S)
Jonny Svärd991af2b2021-04-15 17:31:01 +0200352 ARM_MPU_RBAR(0x11000000, // Base
353 ARM_MPU_SH_NON, // Non-shareable
354 0, // Read-Write
355 1, // Non-Privileged
356 0), // eXecute Never disabled
357 ARM_MPU_RLAR(0x111fffff, // Limit
358 Mpu::WBWARA_index) // Attribute index - Write-Back, Write-Allocate, Read-allocate
359 },
360 {
Jonny Svärde3b6b962021-04-27 11:32:31 +0200361 // DTCM (NS)
362 ARM_MPU_RBAR(0x20000000, // Base
363 ARM_MPU_SH_NON, // Non-shareable
364 0, // Read-Write
365 1, // Non-Privileged
366 1), // eXecute Never enabled
367 ARM_MPU_RLAR(0x2007ffff, // Limit
368 Mpu::WBWARA_index) // Attribute index - Write-Back, Write-Allocate, Read-allocate
369 },
370 {
371 // DTCM (S)
Jonny Svärd991af2b2021-04-15 17:31:01 +0200372 ARM_MPU_RBAR(0x30000000, // Base
373 ARM_MPU_SH_NON, // Non-shareable
374 0, // Read-Write
375 1, // Non-Privileged
376 1), // eXecute Never enabled
377 ARM_MPU_RLAR(0x3007ffff, // Limit
378 Mpu::WBWARA_index) // Attribute index - Write-Back, Write-Allocate, Read-allocate
379 },
380 {
Jonny Svärde3b6b962021-04-27 11:32:31 +0200381 // SSE-300 internal SRAM (NS)
382 ARM_MPU_RBAR(0x21000000, // Base
383 ARM_MPU_SH_NON, // Non-shareable
384 0, // Read-Write
385 1, // Non-Privileged
386 1), // eXecute Never enabled
387 ARM_MPU_RLAR(0x213fffff, // Limit
388 Mpu::WBWARA_index) // Attribute index - Write-Back, Write-Allocate, Read-allocate
389 },
390 {
391 // SSE-300 internal SRAM (S)
Jonny Svärd991af2b2021-04-15 17:31:01 +0200392 ARM_MPU_RBAR(0x31000000, // Base
393 ARM_MPU_SH_NON, // Non-shareable
394 0, // Read-Write
395 1, // Non-Privileged
396 1), // eXecute Never enabled
397 ARM_MPU_RLAR(0x313fffff, // Limit
398 Mpu::WBWARA_index) // Attribute index - Write-Back, Write-Allocate, Read-allocate
399 },
400 {
Jonny Svärde3b6b962021-04-27 11:32:31 +0200401 // DDR (NS)
402 ARM_MPU_RBAR(0x60000000, // Base
403 ARM_MPU_SH_NON, // Non-shareable
404 0, // Read-Write
405 1, // Non-Privileged
406 1), // eXecute Never enabled
407 ARM_MPU_RLAR(0x6fffffff, // Limit
408 Mpu::WBWARA_index) // Attribute index - Write-Back, Write-Allocate, Read-allocate
409 },
410 {
411 // DDR (S)
Jonny Svärd991af2b2021-04-15 17:31:01 +0200412 ARM_MPU_RBAR(0x70000000, // Base
413 ARM_MPU_SH_NON, // Non-shareable
414 0, // Read-Write
415 1, // Non-Privileged
416 1), // eXecute Never enabled
417 ARM_MPU_RLAR(0x7fffffff, // Limit
418 Mpu::WBWARA_index) // Attribute index - Write-Back, Write-Allocate, Read-allocate
419 }};
420
421 // Setup MPU configuration
422 Mpu::loadAndEnableConfig(&mpuConfig[0], mpuConfig.size());
423
424#if defined(CPU_CACHE_ENABLE) && defined(__DCACHE_PRESENT) && (__DCACHE_PRESENT == 1U)
425 SCB_EnableICache();
426 SCB_EnableDCache();
427#endif
Kristofer Jonssonf62c3d72021-01-21 17:39:03 +0100428}
429
430} // namespace EthosU