blob: e5f4680c192d287e987dc4b719e91d7b93f5b4ab [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ärdd6670902021-03-18 15:49:27 +010029#include <timing_adapter.h>
30
Kristofer Jonssonf62c3d72021-01-21 17:39:03 +010031#include "uart.h"
32
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>
Kristofer Jonssonf62c3d72021-01-21 17:39:03 +010036
37using namespace EthosU;
38
39/****************************************************************************
40 * Defines
41 ****************************************************************************/
42
43#define ETHOSU_BASE_ADDRESS 0x48102000
Jonny Svärdd6670902021-03-18 15:49:27 +010044#define ETHOSU_IRQ 56
Kristofer Jonssonf62c3d72021-01-21 17:39:03 +010045
Jonny Svärdd6670902021-03-18 15:49:27 +010046#define ETHOSU0_TA0_BASE_ADDRESS 0x48103000
47#define ETHOSU0_TA1_BASE_ADDRESS 0x48103200
Kristofer Jonssonf62c3d72021-01-21 17:39:03 +010048
49/****************************************************************************
50 * Variables
51 ****************************************************************************/
52
53#if defined(ETHOSU_FAST_MEMORY_SIZE) && ETHOSU_FAST_MEMORY_SIZE > 0
54__attribute__((aligned(16), section(".bss.ethosu_scratch"))) uint8_t ethosu_scratch[ETHOSU_FAST_MEMORY_SIZE];
55#else
56#define ethosu_scratch 0
57#define ETHOSU_FAST_MEMORY_SIZE 0
58#endif
59
Jonny Svärdd6670902021-03-18 15:49:27 +010060static uintptr_t ethosu_ta_base_addrs[ETHOSU_NPU_COUNT][ETHOSU_NPU_TA_COUNT] = {
61 {ETHOSU0_TA0_BASE_ADDRESS, ETHOSU0_TA1_BASE_ADDRESS}};
62struct timing_adapter ethosu_ta[ETHOSU_NPU_COUNT][ETHOSU_NPU_TA_COUNT];
63
Kristofer Jonssonf62c3d72021-01-21 17:39:03 +010064/****************************************************************************
65 * Cache maintenance
66 ****************************************************************************/
67
68#if defined(CPU_CACHE_ENABLE) && defined(__DCACHE_PRESENT) && (__DCACHE_PRESENT == 1U)
69extern "C" {
70void ethosu_flush_dcache(uint32_t *p, size_t bytes) {
71 if (p)
72 SCB_CleanDCache_by_Addr(p, bytes);
73 else
74 SCB_CleanDCache();
75}
76
77void ethosu_invalidate_dcache(uint32_t *p, size_t bytes) {
78 if (p)
79 SCB_InvalidateDCache_by_Addr(p, bytes);
80 else
81 SCB_InvalidateDCache();
82}
83}
84#endif
85
86/****************************************************************************
87 * Init
88 ****************************************************************************/
89
90namespace {
91
Per Åstrand174e98d2021-02-09 17:48:53 +010092extern "C" {
93struct ExcContext {
94 uint32_t r0;
95 uint32_t r1;
96 uint32_t r2;
97 uint32_t r3;
98 uint32_t r12;
99 uint32_t lr;
100 uint32_t pc;
101 uint32_t xPsr;
102};
103
104void HardFault_Handler() {
105 int irq;
106 struct ExcContext *e;
107 uint32_t sp;
108
109 asm volatile("mrs %0, ipsr \n" // Read IPSR (Exceptio number)
110 "sub %0, #16 \n" // Get it into IRQn_Type range
111 "tst lr, #4 \n" // Select the stack which was in use
112 "ite eq \n"
113 "mrseq %1, msp \n"
114 "mrsne %1, psp \n"
115 "mov %2, sp \n"
116 : "=r"(irq), "=r"(e), "=r"(sp));
117
118 printf("Hard fault. irq=%d, pc=0x%08" PRIx32 ", lr=0x%08" PRIx32 ", xpsr=0x%08" PRIx32 ", sp=0x%08" PRIx32 "\n",
119 irq,
120 e->pc,
121 e->lr,
122 e->xPsr,
123 sp);
124 printf(
125 "%11s cfsr=0x%08" PRIx32 " bfar=0x%08" PRIx32 " mmfar=0x%08" PRIx32 "\n", "", SCB->CFSR, SCB->BFAR, SCB->MMFAR);
126 exit(1);
127}
128}
129
Kristofer Jonssonf62c3d72021-01-21 17:39:03 +0100130#ifdef ETHOSU
131void ethosuIrqHandler() {
132 ethosu_irq_handler();
133}
134#endif
135
136} // namespace
137
138namespace EthosU {
139
140void targetSetup() {
141 // Initialize UART driver
142 uart_init();
143
Jonny Svärdd6670902021-03-18 15:49:27 +0100144 // Initialize timing adapter(s)
145 for (int i = 0; i < ETHOSU_NPU_COUNT; i++) {
146 for (int j = 0; j < ETHOSU_NPU_TA_COUNT; j++) {
147 if (ta_init(&ethosu_ta[i][j], ethosu_ta_base_addrs[i][j])) {
148 printf("Failed to initialize timing-adapter %d for NPU %d\n", j, i);
149 }
150 }
151 }
152
Kristofer Jonssonf62c3d72021-01-21 17:39:03 +0100153#ifdef ETHOSU
154 // Initialize Ethos-U NPU driver
155 if (ethosu_init_v3(reinterpret_cast<void *>(ETHOSU_BASE_ADDRESS), ethosu_scratch, ETHOSU_FAST_MEMORY_SIZE, 1, 1)) {
156 printf("Failed to initialize NPU.\n");
157 return;
158 }
159
160 /* Assumes SCB->VTOR point to RW memory */
161 NVIC_SetVector(static_cast<IRQn_Type>(ETHOSU_IRQ), (uint32_t)&ethosuIrqHandler);
162 NVIC_EnableIRQ(static_cast<IRQn_Type>(ETHOSU_IRQ));
163#endif
164}
165
166} // namespace EthosU