Kristofer Jonsson | f62c3d7 | 2021-01-21 17:39:03 +0100 | [diff] [blame] | 1 | /* |
Anton Moberg | fa3e51b | 2021-03-31 11:05:02 +0200 | [diff] [blame] | 2 | * Copyright (c) 2020-2021 Arm Limited. All rights reserved. |
Kristofer Jonsson | f62c3d7 | 2021-01-21 17:39:03 +0100 | [diff] [blame] | 3 | * |
| 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ärd | d667090 | 2021-03-18 15:49:27 +0100 | [diff] [blame] | 29 | #include <timing_adapter.h> |
| 30 | |
Kristofer Jonsson | f62c3d7 | 2021-01-21 17:39:03 +0100 | [diff] [blame] | 31 | #include "uart.h" |
| 32 | |
Jonny Svärd | d667090 | 2021-03-18 15:49:27 +0100 | [diff] [blame] | 33 | #include <inttypes.h> |
Kristofer Jonsson | f62c3d7 | 2021-01-21 17:39:03 +0100 | [diff] [blame] | 34 | #include <stdio.h> |
Per Åstrand | 174e98d | 2021-02-09 17:48:53 +0100 | [diff] [blame] | 35 | #include <stdlib.h> |
Kristofer Jonsson | f62c3d7 | 2021-01-21 17:39:03 +0100 | [diff] [blame] | 36 | |
| 37 | using namespace EthosU; |
| 38 | |
| 39 | /**************************************************************************** |
| 40 | * Defines |
| 41 | ****************************************************************************/ |
| 42 | |
| 43 | #define ETHOSU_BASE_ADDRESS 0x48102000 |
Jonny Svärd | d667090 | 2021-03-18 15:49:27 +0100 | [diff] [blame] | 44 | #define ETHOSU_IRQ 56 |
Kristofer Jonsson | f62c3d7 | 2021-01-21 17:39:03 +0100 | [diff] [blame] | 45 | |
Jonny Svärd | d667090 | 2021-03-18 15:49:27 +0100 | [diff] [blame] | 46 | #define ETHOSU0_TA0_BASE_ADDRESS 0x48103000 |
| 47 | #define ETHOSU0_TA1_BASE_ADDRESS 0x48103200 |
Kristofer Jonsson | f62c3d7 | 2021-01-21 17:39:03 +0100 | [diff] [blame] | 48 | |
| 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ärd | d667090 | 2021-03-18 15:49:27 +0100 | [diff] [blame] | 60 | static uintptr_t ethosu_ta_base_addrs[ETHOSU_NPU_COUNT][ETHOSU_NPU_TA_COUNT] = { |
| 61 | {ETHOSU0_TA0_BASE_ADDRESS, ETHOSU0_TA1_BASE_ADDRESS}}; |
| 62 | struct timing_adapter ethosu_ta[ETHOSU_NPU_COUNT][ETHOSU_NPU_TA_COUNT]; |
| 63 | |
Kristofer Jonsson | f62c3d7 | 2021-01-21 17:39:03 +0100 | [diff] [blame] | 64 | /**************************************************************************** |
| 65 | * Cache maintenance |
| 66 | ****************************************************************************/ |
| 67 | |
| 68 | #if defined(CPU_CACHE_ENABLE) && defined(__DCACHE_PRESENT) && (__DCACHE_PRESENT == 1U) |
| 69 | extern "C" { |
| 70 | void 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 | |
| 77 | void 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 | |
| 90 | namespace { |
| 91 | |
Per Åstrand | 174e98d | 2021-02-09 17:48:53 +0100 | [diff] [blame] | 92 | extern "C" { |
| 93 | struct 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 | |
| 104 | void 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 Jonsson | f62c3d7 | 2021-01-21 17:39:03 +0100 | [diff] [blame] | 130 | #ifdef ETHOSU |
| 131 | void ethosuIrqHandler() { |
| 132 | ethosu_irq_handler(); |
| 133 | } |
| 134 | #endif |
| 135 | |
| 136 | } // namespace |
| 137 | |
| 138 | namespace EthosU { |
| 139 | |
| 140 | void targetSetup() { |
| 141 | // Initialize UART driver |
| 142 | uart_init(); |
| 143 | |
Jonny Svärd | d667090 | 2021-03-18 15:49:27 +0100 | [diff] [blame] | 144 | // 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(ðosu_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 Jonsson | f62c3d7 | 2021-01-21 17:39:03 +0100 | [diff] [blame] | 153 | #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)ðosuIrqHandler); |
| 162 | NVIC_EnableIRQ(static_cast<IRQn_Type>(ETHOSU_IRQ)); |
| 163 | #endif |
| 164 | } |
| 165 | |
| 166 | } // namespace EthosU |