1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#[macro_use]
mod register_generation;

use super::Register;
use bitfield::bitfield;
use jep106::JEP106Code;

pub trait DebugPort {
    fn version(&self) -> &'static str;
}

pub trait DPAccess<PORT, R>
where
    PORT: DebugPort,
    R: DPRegister<PORT>,
{
    type Error: std::fmt::Debug;
    fn read_dp_register(&mut self, port: &PORT) -> Result<R, Self::Error>;

    fn write_dp_register(&mut self, port: &PORT, register: R) -> Result<(), Self::Error>;
}

#[derive(Debug, PartialEq, Copy, Clone)]
pub enum DPBankSel {
    DontCare,
    Bank(u8),
}

pub trait DPRegister<PortType: DebugPort>: Register {
    const DP_BANK: DPBankSel;
}

/// Debug PortType V1
pub struct DPv1 {}

impl DebugPort for DPv1 {
    fn version(&self) -> &'static str {
        "DPv1"
    }
}

/// Debug PortType V2
pub struct DPv2 {}

impl DebugPort for DPv2 {
    fn version(&self) -> &'static str {
        "DPv2"
    }
}

bitfield! {
    #[derive(Clone)]
    pub struct Abort(u32);
    impl Debug;
    pub _, set_orunerrclr: 5;
    pub _, set_wderrclr: 4;
    pub _, set_stkerrclr: 3;
    pub _, set_stkcmpclr: 2;
    pub _, set_dapabort: 1;
}

impl Default for Abort {
    fn default() -> Self {
        Abort(0)
    }
}

impl From<u32> for Abort {
    fn from(raw: u32) -> Self {
        Abort(raw)
    }
}

impl From<Abort> for u32 {
    fn from(raw: Abort) -> Self {
        raw.0
    }
}

impl DPRegister<DPv1> for Abort {
    const DP_BANK: DPBankSel = DPBankSel::DontCare;
}

impl Register for Abort {
    const ADDRESS: u8 = 0x0;
    const NAME: &'static str = "ABORT";
}

bitfield! {
    #[derive(Clone)]
    pub struct Ctrl(u32);
    impl Debug;
    pub csyspwrupack, _: 31;
    pub csyspwrupreq, set_csyspwrupreq: 30;
    pub cdbgpwrupack, _: 29;
    pub cdbgpwrupreq, set_cdbgpwrupreq: 28;
    pub cdbgrstack, _: 27;
    pub c_dbg_rst_req, set_c_dbg_rst_req: 26;
    pub u16, trn_cnt, set_trn_cnt: 23, 12;
    pub u8, mask_lane, set_mask_lane: 11, 8;
    pub w_data_err, _ : 7;
    pub read_ok, _ : 6;
    pub sticky_err, _: 5;
    pub stick_cmp, _: 4;
    pub u8, trn_mode, _: 3, 2;
    pub sticky_orun, _: 1;
    pub orun_detect, set_orun_detect: 0;
}

impl Default for Ctrl {
    fn default() -> Self {
        Ctrl(0)
    }
}

impl From<u32> for Ctrl {
    fn from(raw: u32) -> Self {
        Ctrl(raw)
    }
}

impl From<Ctrl> for u32 {
    fn from(raw: Ctrl) -> Self {
        raw.0
    }
}

impl DPRegister<DPv1> for Ctrl {
    const DP_BANK: DPBankSel = DPBankSel::Bank(0);
}

impl Register for Ctrl {
    const ADDRESS: u8 = 0x4;
    const NAME: &'static str = "CTRL/STAT";
}

bitfield! {
    #[derive(Clone)]
    pub struct Select(u32);
    impl Debug;
    pub u8, ap_sel, set_ap_sel: 31, 24;
    pub u8, ap_bank_sel, set_ap_bank_sel: 7, 4;
    pub u8, dp_bank_sel, set_dp_bank_sel: 3, 0;
}

impl From<u32> for Select {
    fn from(raw: u32) -> Self {
        Select(raw)
    }
}

impl From<Select> for u32 {
    fn from(raw: Select) -> Self {
        raw.0
    }
}

impl DPRegister<DPv1> for Select {
    const DP_BANK: DPBankSel = DPBankSel::DontCare;
}

impl Register for Select {
    const ADDRESS: u8 = 0x8;
    const NAME: &'static str = "SELECT";
}

bitfield! {
    #[derive(Clone)]
    pub struct DPIDR(u32);
    impl Debug;
    pub u8, revision, _: 31, 28;
    pub u8, part_no, _: 27, 20;
    pub min, _: 16;
    pub u8, version, _: 15, 12;
    pub designer, _: 11, 1;
    pub u8, jep_cc, _: 11, 8;
    pub u8, jep_id, _: 7, 1;
}

impl From<u32> for DPIDR {
    fn from(raw: u32) -> Self {
        Self(raw)
    }
}

impl From<DPIDR> for u32 {
    fn from(raw: DPIDR) -> Self {
        raw.0
    }
}

impl DPRegister<DPv1> for DPIDR {
    const DP_BANK: DPBankSel = DPBankSel::DontCare;
}

impl Register for DPIDR {
    const ADDRESS: u8 = 0x0;
    const NAME: &'static str = "DPIDR";
}

bitfield! {
    #[derive(Clone)]
    pub struct TARGETID(u32);
    impl Debug;
    pub u8, trevision, _: 31, 28;
    pub u16, tpartno, _: 27, 12;
    pub u16, tdesigner, _: 11, 1;
}

impl From<u32> for TARGETID {
    fn from(raw: u32) -> Self {
        Self(raw)
    }
}

impl From<TARGETID> for u32 {
    fn from(raw: TARGETID) -> Self {
        raw.0
    }
}

impl DPRegister<DPv2> for TARGETID {
    const DP_BANK: DPBankSel = DPBankSel::Bank(2);
}

impl Register for TARGETID {
    const ADDRESS: u8 = 0x4;
    const NAME: &'static str = "TARGETID";
}

#[derive(Debug)]
pub struct DebugPortId {
    pub revision: u8,
    pub part_no: u8,
    pub version: DebugPortVersion,
    pub min_dp_support: MinDpSupport,
    pub designer: JEP106Code,
}

impl From<DPIDR> for DebugPortId {
    fn from(dpidr: DPIDR) -> DebugPortId {
        DebugPortId {
            revision: dpidr.revision(),
            part_no: dpidr.part_no(),
            version: dpidr.version().into(),
            min_dp_support: dpidr.min().into(),
            designer: JEP106Code::new(dpidr.jep_cc(), dpidr.jep_id()),
        }
    }
}

#[derive(Debug, PartialEq)]
pub enum MinDpSupport {
    NotImplemented,
    Implemented,
}

impl From<bool> for MinDpSupport {
    fn from(bit_set: bool) -> Self {
        if bit_set {
            MinDpSupport::Implemented
        } else {
            MinDpSupport::NotImplemented
        }
    }
}

#[derive(Debug, PartialEq)]
pub enum DebugPortVersion {
    DPv0,
    DPv1,
    DPv2,
    Unsupported,
}

impl From<u8> for DebugPortVersion {
    fn from(value: u8) -> Self {
        match value {
            0 => DebugPortVersion::DPv0,
            1 => DebugPortVersion::DPv1,
            2 => DebugPortVersion::DPv2,
            _ => DebugPortVersion::Unsupported,
        }
    }
}