|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Nordic Semiconductor ASA. |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#ifndef CORESIGHT_ARM_H_ |
| 8 | +#define CORESIGHT_ARM_H_ |
| 9 | + |
| 10 | +#include <stdint.h> |
| 11 | +#include <stdbool.h> |
| 12 | +#include <errno.h> |
| 13 | +#include <zephyr/sys/sys_io.h> |
| 14 | + |
| 15 | +#ifdef __cplusplus |
| 16 | +extern "C" { |
| 17 | +#endif |
| 18 | + |
| 19 | +/** |
| 20 | + * @file |
| 21 | + * @brief Generic ARM CoreSight Hardware Abstraction Layer |
| 22 | + * |
| 23 | + * This HAL provides generic register definitions and utility functions for ARM CoreSight |
| 24 | + * peripherals. Platform-specific drivers should provide base addresses and use these |
| 25 | + * generic definitions for register access. |
| 26 | + */ |
| 27 | + |
| 28 | +/* Common CoreSight unlock key as defined by ARM architecture */ |
| 29 | +#define CORESIGHT_UNLOCK_KEY (0xC5ACCE55UL) |
| 30 | + |
| 31 | +/* CoreSight register offsets */ |
| 32 | + |
| 33 | +/* Common CoreSight peripheral register offsets (found at the end of all CoreSight peripherals) */ |
| 34 | +#define CORESIGHT_CLAIMSET_OFFSET (0xFA0UL) /* Claim Tag Set Register */ |
| 35 | +#define CORESIGHT_CLAIMCLR_OFFSET (0xFA4UL) /* Claim Tag Clear Register */ |
| 36 | +#define CORESIGHT_LAR_OFFSET (0xFB0UL) /* Lock Access Register */ |
| 37 | +#define CORESIGHT_LSR_OFFSET (0xFB4UL) /* Lock Status Register */ |
| 38 | + |
| 39 | +/* ATB Funnel register offsets */ |
| 40 | +#define ATBFUNNEL_CTRLREG_OFFSET (0x000UL) /* Control Register */ |
| 41 | + |
| 42 | +/* ATB Replicator register offsets */ |
| 43 | +#define ATBREPLICATOR_IDFILTER0_OFFSET (0x000UL) /* ID Filter Register 0 */ |
| 44 | +#define ATBREPLICATOR_IDFILTER1_OFFSET (0x004UL) /* ID Filter Register 1 */ |
| 45 | + |
| 46 | +/* ETR (Embedded Trace Router/TMC-ETR) register offsets */ |
| 47 | +#define ETR_RSZ_OFFSET (0x004UL) /* RAM Size Register */ |
| 48 | +#define ETR_RWP_OFFSET (0x018UL) /* RAM Write Pointer Register */ |
| 49 | +#define ETR_CTL_OFFSET (0x020UL) /* Control Register */ |
| 50 | +#define ETR_MODE_OFFSET (0x028UL) /* Mode Register */ |
| 51 | +#define ETR_DBALO_OFFSET (0x118UL) /* Data Buffer Address Low Register */ |
| 52 | +#define ETR_DBAHI_OFFSET (0x11CUL) /* Data Buffer Address High Register */ |
| 53 | +#define ETR_FFCR_OFFSET (0x304UL) /* Formatter and Flush Control Register */ |
| 54 | + |
| 55 | +/* STM (System Trace Macrocell) register offsets */ |
| 56 | +#define STM_STMHEER_OFFSET (0xD00UL) /* Hardware Event Enable Register */ |
| 57 | +#define STM_STMHEMCR_OFFSET (0xD64UL) /* Hardware Event Master Control Register */ |
| 58 | +#define STM_STMSPER_OFFSET (0xE00UL) /* Stimulus Port Enable Register */ |
| 59 | +#define STM_STMTCSR_OFFSET (0xE80UL) /* Trace Control and Status Register */ |
| 60 | +#define STM_STMTSFREQR_OFFSET (0xE8CUL) /* Timestamp Frequency Register */ |
| 61 | +#define STM_STMSYNCR_OFFSET (0xE90UL) /* Synchronization Control Register */ |
| 62 | +#define STM_STMAUXCR_OFFSET (0xE94UL) /* Auxiliary Control Register */ |
| 63 | + |
| 64 | +/* TPIU (Trace Port Interface Unit) register offsets */ |
| 65 | +#define TPIU_CSPSR_OFFSET (0x004UL) /* Current Parallel Port Size Register */ |
| 66 | +#define TPIU_FFCR_OFFSET (0x304UL) /* Formatter and Flush Control Register */ |
| 67 | +#define TPIU_FSCR_OFFSET (0x308UL) /* Formatter Synchronization Counter Register */ |
| 68 | + |
| 69 | +/* CTI (Cross Trigger Interface) register offsets */ |
| 70 | +#define CTI_CTICONTROL_OFFSET (0x000UL) /* CTI Control Register */ |
| 71 | +#define CTI_CTIOUTEN0_OFFSET (0x0A0UL) /* CTI Trigger Output Enable Register 0 */ |
| 72 | +#define CTI_CTIGATE_OFFSET (0x140UL) /* CTI Channel Gate Enable Register */ |
| 73 | + |
| 74 | +/* TSGEN (Timestamp Generator) register offsets */ |
| 75 | +#define TSGEN_CNTCR_OFFSET (0x000UL) /* Counter Control Register */ |
| 76 | +#define TSGEN_CNTFID0_OFFSET (0x020UL) /* Counter Frequency ID Register 0 */ |
| 77 | + |
| 78 | +/* Lock Status Register (LSR) bit fields */ |
| 79 | +#define CORESIGHT_LSR_LOCKED_Pos (1UL) |
| 80 | +#define CORESIGHT_LSR_LOCKED_Msk (0x1UL << CORESIGHT_LSR_LOCKED_Pos) |
| 81 | +#define CORESIGHT_LSR_PRESENT_Pos (0UL) |
| 82 | +#define CORESIGHT_LSR_PRESENT_Msk (0x1UL << CORESIGHT_LSR_PRESENT_Pos) |
| 83 | + |
| 84 | +/* STM Trace Control and Status Register (STMTCSR) bit fields */ |
| 85 | +#define STM_STMTCSR_EN_Pos (0UL) |
| 86 | +#define STM_STMTCSR_EN_Msk (0x1UL << STM_STMTCSR_EN_Pos) |
| 87 | +#define STM_STMTCSR_TSEN_Pos (1UL) |
| 88 | +#define STM_STMTCSR_TSEN_Msk (0x1UL << STM_STMTCSR_TSEN_Pos) |
| 89 | +#define STM_STMTCSR_TRACEID_Pos (16UL) |
| 90 | +#define STM_STMTCSR_TRACEID_Msk (0x7FUL << STM_STMTCSR_TRACEID_Pos) |
| 91 | + |
| 92 | +/* STM Hardware Event Master Control Register (STMHEMCR) bit fields */ |
| 93 | +#define STM_STMHEMCR_EN_Pos (0UL) |
| 94 | +#define STM_STMHEMCR_EN_Msk (0x1UL << STM_STMHEMCR_EN_Pos) |
| 95 | + |
| 96 | +/* STM Auxiliary Control Register (STMAUXCR) bit fields */ |
| 97 | +#define STM_STMAUXCR_FIFOAF_Pos (0UL) |
| 98 | +#define STM_STMAUXCR_FIFOAF_Msk (0x1UL << STM_STMAUXCR_FIFOAF_Pos) |
| 99 | + |
| 100 | +/* CTI Control Register (CTICONTROL) bit fields */ |
| 101 | +#define CTI_CTICONTROL_GLBEN_Pos (0UL) |
| 102 | +#define CTI_CTICONTROL_GLBEN_Msk (0x1UL << CTI_CTICONTROL_GLBEN_Pos) |
| 103 | + |
| 104 | +/* TPIU Formatter and Flush Control Register (FFCR) bit fields */ |
| 105 | +#define TPIU_FFCR_ENFCONT_Pos (1UL) |
| 106 | +#define TPIU_FFCR_ENFCONT_Msk (0x1UL << TPIU_FFCR_ENFCONT_Pos) |
| 107 | +#define TPIU_FFCR_FONFLIN_Pos (4UL) |
| 108 | +#define TPIU_FFCR_FONFLIN_Msk (0x1UL << TPIU_FFCR_FONFLIN_Pos) |
| 109 | +#define TPIU_FFCR_ENFTC_Pos (0UL) |
| 110 | +#define TPIU_FFCR_ENFTC_Msk (0x1UL << TPIU_FFCR_ENFTC_Pos) |
| 111 | + |
| 112 | +/* ETR Mode Register bit fields */ |
| 113 | +#define ETR_MODE_MODE_Pos (0UL) |
| 114 | +#define ETR_MODE_MODE_Msk (0x3UL << ETR_MODE_MODE_Pos) |
| 115 | +#define ETR_MODE_MODE_CIRCULARBUF (0UL) /* Circular Buffer mode */ |
| 116 | +#define ETR_MODE_MODE_SWFIFO1 (1UL) /* Software FIFO mode */ |
| 117 | +#define ETR_MODE_MODE_HWFIFO (2UL) /* Hardware FIFO mode */ |
| 118 | +#define ETR_MODE_MODE_SWFIFO2 (3UL) /* Software FIFO mode */ |
| 119 | + |
| 120 | +/* ETR Control Register bit fields */ |
| 121 | +#define ETR_CTL_TRACECAPTEN_Pos (0UL) |
| 122 | +#define ETR_CTL_TRACECAPTEN_Msk (0x1UL << ETR_CTL_TRACECAPTEN_Pos) |
| 123 | + |
| 124 | +/* ETR Formatter and Flush Control Register (FFCR) bit fields */ |
| 125 | +#define ETR_FFCR_ENFT_Pos (0UL) |
| 126 | +#define ETR_FFCR_ENFT_Msk (0x1UL << ETR_FFCR_ENFT_Pos) |
| 127 | +#define ETR_FFCR_ENTI_Pos (1UL) |
| 128 | +#define ETR_FFCR_ENTI_Msk (0x1UL << ETR_FFCR_ENTI_Pos) |
| 129 | + |
| 130 | +/* ATB Funnel Control Register bit fields */ |
| 131 | +#define ATBFUNNEL_CTRLREG_ENS0_Pos (0UL) |
| 132 | +#define ATBFUNNEL_CTRLREG_ENS0_Msk (0x1UL << ATBFUNNEL_CTRLREG_ENS0_Pos) |
| 133 | +#define ATBFUNNEL_CTRLREG_ENS1_Pos (1UL) |
| 134 | +#define ATBFUNNEL_CTRLREG_ENS1_Msk (0x1UL << ATBFUNNEL_CTRLREG_ENS1_Pos) |
| 135 | +#define ATBFUNNEL_CTRLREG_ENS2_Pos (2UL) |
| 136 | +#define ATBFUNNEL_CTRLREG_ENS2_Msk (0x1UL << ATBFUNNEL_CTRLREG_ENS2_Pos) |
| 137 | +#define ATBFUNNEL_CTRLREG_ENS3_Pos (3UL) |
| 138 | +#define ATBFUNNEL_CTRLREG_ENS3_Msk (0x1UL << ATBFUNNEL_CTRLREG_ENS3_Pos) |
| 139 | +#define ATBFUNNEL_CTRLREG_ENS4_Pos (4UL) |
| 140 | +#define ATBFUNNEL_CTRLREG_ENS4_Msk (0x1UL << ATBFUNNEL_CTRLREG_ENS4_Pos) |
| 141 | +#define ATBFUNNEL_CTRLREG_ENS5_Pos (5UL) |
| 142 | +#define ATBFUNNEL_CTRLREG_ENS5_Msk (0x1UL << ATBFUNNEL_CTRLREG_ENS5_Pos) |
| 143 | +#define ATBFUNNEL_CTRLREG_ENS6_Pos (6UL) |
| 144 | +#define ATBFUNNEL_CTRLREG_ENS6_Msk (0x1UL << ATBFUNNEL_CTRLREG_ENS6_Pos) |
| 145 | +#define ATBFUNNEL_CTRLREG_ENS7_Pos (7UL) |
| 146 | +#define ATBFUNNEL_CTRLREG_ENS7_Msk (0x1UL << ATBFUNNEL_CTRLREG_ENS7_Pos) |
| 147 | +#define ATBFUNNEL_CTRLREG_HT_Pos (8UL) |
| 148 | +#define ATBFUNNEL_CTRLREG_HT_Msk (0xFUL << ATBFUNNEL_CTRLREG_HT_Pos) |
| 149 | + |
| 150 | +/* TSGEN Counter Control Register bit fields */ |
| 151 | +#define TSGEN_CNTCR_EN_Pos (0UL) |
| 152 | +#define TSGEN_CNTCR_EN_Msk (0x1UL << TSGEN_CNTCR_EN_Pos) |
| 153 | + |
| 154 | +/** |
| 155 | + * @brief Check if a CoreSight peripheral is locked |
| 156 | + * |
| 157 | + * @param base_addr Base address of CoreSight peripheral |
| 158 | + * @return true if peripheral is locked, false otherwise |
| 159 | + */ |
| 160 | +static inline bool coresight_is_locked(mem_addr_t base_addr) |
| 161 | +{ |
| 162 | + uint32_t lsr = *(volatile uint32_t *)(base_addr + CORESIGHT_LSR_OFFSET); |
| 163 | + |
| 164 | + return (lsr & CORESIGHT_LSR_LOCKED_Msk) != 0; |
| 165 | +} |
| 166 | + |
| 167 | +/** |
| 168 | + * @brief Unlock a CoreSight peripheral |
| 169 | + * |
| 170 | + * @param base_addr Base address of CoreSight peripheral |
| 171 | + * @retval 0 on success |
| 172 | + * @retval -EIO if unlock operation failed |
| 173 | + */ |
| 174 | +static inline int coresight_unlock(mem_addr_t base_addr) |
| 175 | +{ |
| 176 | + *(volatile uint32_t *)(base_addr + CORESIGHT_LAR_OFFSET) = CORESIGHT_UNLOCK_KEY; |
| 177 | + |
| 178 | + if (coresight_is_locked(base_addr)) { |
| 179 | + return -EIO; |
| 180 | + } |
| 181 | + |
| 182 | + return 0; |
| 183 | +} |
| 184 | + |
| 185 | +/** |
| 186 | + * @brief Lock a CoreSight peripheral |
| 187 | + * |
| 188 | + * @param base_addr Base address of CoreSight peripheral |
| 189 | + * @retval 0 on success |
| 190 | + * @retval -EIO if lock operation failed |
| 191 | + */ |
| 192 | +static inline int coresight_lock(mem_addr_t base_addr) |
| 193 | +{ |
| 194 | + /* Write any value other than unlock key to Lock Access Register to lock */ |
| 195 | + *(volatile uint32_t *)(base_addr + CORESIGHT_LAR_OFFSET) = 0x00000000; |
| 196 | + |
| 197 | + if (!coresight_is_locked(base_addr)) { |
| 198 | + return -EIO; |
| 199 | + } |
| 200 | + |
| 201 | + return 0; |
| 202 | +} |
| 203 | + |
| 204 | +#ifdef __cplusplus |
| 205 | +} |
| 206 | +#endif |
| 207 | + |
| 208 | +#endif /* CORESIGHT_ARM_H_ */ |
0 commit comments