-
Notifications
You must be signed in to change notification settings - Fork 32
/
dmi.rs
559 lines (453 loc) · 16.8 KB
/
dmi.rs
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
use crate::{
commands::DmiOp,
device::WchLink,
error::{AbstractcsCmdErr, Error, Result},
regs::{Abstractcs, DMReg, Dmcontrol, Dmstatus},
};
use std::{thread, time::Duration};
// FPEC, OPTWRE to unlock,
pub const KEY1: u32 = 0x45670123;
pub const KEY2: u32 = 0xCDEF89AB;
/// RISC-V DMI
pub trait DebugModuleInterface {
fn dmi_nop(&mut self) -> Result<()>;
fn dmi_read(&mut self, reg: u8) -> Result<u32>;
fn dmi_write(&mut self, reg: u8, value: u32) -> Result<()>;
fn read_dmi_reg<R>(&mut self) -> Result<R>
where
R: DMReg,
{
let val = self.dmi_read(R::ADDR)?;
Ok(R::from(val))
}
fn write_dmi_reg<R>(&mut self, reg: R) -> Result<()>
where
R: DMReg,
{
self.dmi_write(R::ADDR, reg.into())?;
Ok(())
}
}
impl DebugModuleInterface for WchLink {
fn dmi_nop(&mut self) -> Result<()> {
self.send_command(DmiOp::nop())?;
Ok(())
}
fn dmi_read(&mut self, reg: u8) -> Result<u32> {
let mut n = 0;
loop {
let resp = self.send_command(DmiOp::read(reg))?;
if resp.op == 0x03 && resp.data == 0xffffffff && resp.addr == 0x7d {
// special code for NotAttached
return Err(Error::NotAttached);
}
if resp.is_success() {
return Ok(resp.data);
} else if n > 100 {
return Err(Error::Timeout);
} else if resp.is_busy() {
log::warn!("dmi_read: busy, retrying");
thread::sleep(Duration::from_millis(10));
n += 1;
} else {
return Err(Error::DmiFailed);
}
}
}
fn dmi_write(&mut self, reg: u8, value: u32) -> Result<()> {
self.send_command(DmiOp::write(reg, value))?;
Ok(())
}
}
pub enum HaltMode {
NoReset,
Reset,
Reboot,
Resume,
/// Halt then go to bootloader
Bootloader,
}
pub struct Algorigthm<'a, D: DebugModuleInterface> {
dmi: &'a mut D,
}
impl<'a, D: DebugModuleInterface> Algorigthm<'a, D> {
pub fn new(dmi: &'a mut D) -> Self {
Self { dmi }
}
fn clear_abstractcs_cmderr(&mut self) -> Result<()> {
let mut abstractcs = Abstractcs::from(0);
abstractcs.set_cmderr(0b111);
self.dmi.write_dmi_reg(abstractcs)?;
Ok(())
}
pub fn ensure_mcu_halt(&mut self) -> Result<()> {
let dmstatus = self.dmi.read_dmi_reg::<Dmstatus>()?;
if dmstatus.allhalted() && dmstatus.anyhalted() {
log::trace!("Already halted, nop");
} else {
loop {
// Initiate a halt request
self.dmi.dmi_write(0x10, 0x80000001)?;
let dmstatus = self.dmi.read_dmi_reg::<Dmstatus>()?;
if dmstatus.anyhalted() && dmstatus.allhalted() {
break;
} else {
log::warn!("Not halt, try send");
thread::sleep(Duration::from_millis(10));
}
}
}
// Clear the halt request bit.
self.dmi.dmi_write(0x10, 0x00000001)?;
Ok(())
}
pub fn reset_debug_module(&mut self) -> Result<()> {
self.dmi.dmi_write(0x10, 0x00000000)?;
self.dmi.dmi_write(0x10, 0x00000001)?;
let dmcontrol = self.dmi.read_dmi_reg::<Dmcontrol>()?;
if dmcontrol.dmactive() {
Ok(())
} else {
Err(Error::DmiFailed)
}
}
pub fn read_mem32(&mut self, addr: u32) -> Result<u32> {
self.dmi.dmi_write(0x20, 0x0002a303)?; // lw x6,0(x5)
self.dmi.dmi_write(0x21, 0x00100073)?; // ebreak
self.dmi.dmi_write(0x04, addr)?; // data0 <- address
self.clear_abstractcs_cmderr()?;
self.dmi.dmi_write(0x17, 0x00271005)?;
let abstractcs: Abstractcs = self.dmi.read_dmi_reg()?;
if abstractcs.busy() {
return Err(Error::AbstractCommandError(AbstractcsCmdErr::Busy)); //resue busy
}
if abstractcs.cmderr() != 0 {
AbstractcsCmdErr::try_from_cmderr(abstractcs.cmderr() as _)?;
}
self.dmi.dmi_write(0x17, 0x00221006)?; // data0 <- x6
if abstractcs.busy() {
return Err(Error::AbstractCommandError(AbstractcsCmdErr::Busy)); //resue busy
}
if abstractcs.cmderr() != 0 {
AbstractcsCmdErr::try_from_cmderr(abstractcs.cmderr() as _)?;
}
let data0 = self.dmi.dmi_read(0x04)?;
Ok(data0)
}
pub fn write_mem32(&mut self, addr: u32, data: u32) -> Result<()> {
// rasm2 -a riscv -d 23a07200
// sw t2, 0(t0)
self.dmi.dmi_write(0x20, 0x0072a023)?; // sw x7,0(x5)
self.dmi.dmi_write(0x21, 0x00100073)?; // ebreak
self.dmi.dmi_write(0x04, addr)?; // data0 <- address
self.clear_abstractcs_cmderr()?;
self.dmi.dmi_write(0x17, 0x00231005)?; // x5 <- data0
let abstractcs: Abstractcs = self.dmi.read_dmi_reg()?;
log::trace!("{:?}", abstractcs);
if abstractcs.busy() {
return Err(Error::AbstractCommandError(AbstractcsCmdErr::Busy)); //resue busy
}
if abstractcs.cmderr() != 0 {
AbstractcsCmdErr::try_from_cmderr(abstractcs.cmderr() as _)?;
}
self.dmi.dmi_write(0x04, data)?; // data0 <- data
self.clear_abstractcs_cmderr()?;
self.dmi.dmi_write(0x17, 0x00271007)?; // x7 <- data0
let abstractcs: Abstractcs = self.dmi.read_dmi_reg()?;
log::trace!("{:?}", abstractcs);
if abstractcs.busy() {
return Err(Error::AbstractCommandError(AbstractcsCmdErr::Busy)); //resue busy
}
if abstractcs.cmderr() != 0 {
AbstractcsCmdErr::try_from_cmderr(abstractcs.cmderr() as _)?;
}
Ok(())
}
pub fn write_mem8(&mut self, addr: u32, data: u8) -> Result<()> {
self.dmi.dmi_write(0x20, 0x00728023)?; // sb x7,0(x5)
self.dmi.dmi_write(0x21, 0x00100073)?; // ebreak
self.dmi.dmi_write(0x04, addr)?; // data0 <- address
self.clear_abstractcs_cmderr()?;
self.dmi.dmi_write(0x17, 0x00231005)?; // x5 <- data0
let abstractcs: Abstractcs = self.dmi.read_dmi_reg()?;
log::trace!("{:?}", abstractcs);
if abstractcs.busy() {
return Err(Error::AbstractCommandError(AbstractcsCmdErr::Busy)); //resue busy
}
if abstractcs.cmderr() != 0 {
AbstractcsCmdErr::try_from_cmderr(abstractcs.cmderr() as _)?;
}
self.dmi.dmi_write(0x04, data as u32)?; // data0 <- data
self.clear_abstractcs_cmderr()?;
self.dmi.dmi_write(0x17, 0x00271007)?; // x7 <- data0
let abstractcs: Abstractcs = self.dmi.read_dmi_reg()?;
log::trace!("{:?}", abstractcs);
if abstractcs.busy() {
return Err(Error::AbstractCommandError(AbstractcsCmdErr::Busy)); //resue busy
}
if abstractcs.cmderr() != 0 {
AbstractcsCmdErr::try_from_cmderr(abstractcs.cmderr() as _)?;
}
Ok(())
}
/// Read register value
/// CSR: 0x0000 - 0x0fff
/// GPR: 0x1000 - 0x101f
/// FPR: 0x1020 - 0x103f
// ref: QingKeV2 Microprocessor Debug Manual
pub fn read_reg(&mut self, regno: u16) -> Result<u32> {
self.clear_abstractcs_cmderr()?;
let reg = regno as u32;
self.dmi.dmi_write(0x04, 0x00000000)?; // Clear the Data0 register
self.dmi.dmi_write(0x17, 0x00220000 | (reg & 0xFFFF))?;
let abstractcs = self.dmi.read_dmi_reg::<Abstractcs>()?;
if abstractcs.busy() {
return Err(Error::AbstractCommandError(AbstractcsCmdErr::Busy)); // resue busy
}
if abstractcs.cmderr() != 0 {
AbstractcsCmdErr::try_from_cmderr(abstractcs.cmderr() as _)?;
}
let resp = self.dmi.dmi_read(0x04)?;
Ok(resp)
}
pub fn dump_pmp(&mut self) -> Result<()> {
let regs = [
("pmpcfg0", 0x3A0),
("pmpaddr0", 0x3B0),
("pmpaddr1", 0x3B1),
("pmpaddr2", 0x3B2),
("pmpaddr3", 0x3B3),
];
for (name, addr) in regs.iter() {
let val = self.read_reg(*addr)?;
log::info!("{}: 0x{:08x}", name, val);
}
Ok(())
}
pub fn modify_mem32<F>(&mut self, addr: u32, f: F) -> Result<()>
where
F: FnOnce(u32) -> u32,
{
let data = self.read_mem32(addr)?;
let data = f(data);
self.write_mem32(addr, data)?;
Ok(())
}
pub fn wait_mem32<F>(&mut self, addr: u32, until: F) -> Result<u32>
where
F: Fn(u32) -> bool,
{
loop {
let data = self.read_mem32(addr)?;
if until(data) {
return Ok(data);
}
thread::sleep(Duration::from_millis(1));
}
}
pub fn read_memory(&mut self, addr: u32, len: u32) -> Result<Vec<u8>> {
if len % 4 != 0 {
return Err(Error::Custom("len must be 4 bytes aligned".to_string()));
}
let mut ret = Vec::with_capacity(len as usize);
for i in 0..len / 4 {
let data = self.read_mem32(addr + i * 4)?;
ret.extend_from_slice(&data.to_le_bytes());
}
Ok(ret)
}
fn lock_flash(&mut self) -> Result<()> {
const FLASH_CTLR: u32 = 0x40022010;
self.modify_mem32(FLASH_CTLR, |r| r | 0x00008080)?;
Ok(())
}
/// unlock FLASH LOCK and FLOCK
fn unlock_flash(&mut self) -> Result<()> {
const FLASH_CTLR: u32 = 0x40022010;
const FLASH_KEYR: u32 = 0x40022004;
const FLASH_MODEKEYR: u32 = 0x40022024;
const KEY1: u32 = 0x45670123;
const KEY2: u32 = 0xCDEF89AB;
let flash_ctlr = self.read_mem32(FLASH_CTLR)?;
log::debug!("flash_ctlr: 0x{:08x}", flash_ctlr);
// Test LOCK, FLOCK bits
if flash_ctlr & 0x00008080 == 0 {
// already unlocked
return Ok(());
}
// unlock LOCK
self.write_mem32(FLASH_KEYR, KEY1)?;
self.write_mem32(FLASH_KEYR, KEY2)?;
// unlock FLOCK
self.write_mem32(FLASH_MODEKEYR, KEY1)?;
self.write_mem32(FLASH_MODEKEYR, KEY2)?;
let flash_ctlr = self.read_mem32(FLASH_CTLR)?;
log::debug!("flash_ctlr: 0x{:08x}", flash_ctlr);
Ok(())
}
/// Erase by 256 bytes page
/// address must be 256 bytes aligned
pub fn fast_erase(&mut self, address: u32) -> Result<()> {
// require unlock
self.unlock_flash()?;
const FLASH_STATR: u32 = 0x4002200C;
const BUSY_MASK: u32 = 0x00000001;
const START_MASK: u32 = 1 << 6;
// const EOP_MASK: u32 = 1 << 5;
const WPROTECT_ERR_MASK: u32 = 1 << 4;
const FLASH_ADDR: u32 = 0x40022014;
const FLASH_CTLR: u32 = 0x40022010;
const PAGE_ERASE_MASK: u32 = 1 << 17;
if address & 0xff != 0 {
return Err(Error::Custom(
"address must be 256 bytes aligned".to_string(),
));
}
let statr = self.read_mem32(FLASH_STATR)?;
// check if busy
if statr & BUSY_MASK != 0 {
return Err(Error::Custom("flash busy".to_string()));
}
self.modify_mem32(FLASH_CTLR, |r| r | PAGE_ERASE_MASK)?;
self.write_mem32(FLASH_ADDR, address)?;
self.modify_mem32(FLASH_CTLR, |r| r | START_MASK)?;
loop {
let statr = self.read_mem32(FLASH_STATR)?;
// check if busy
if statr & BUSY_MASK != 0 {
thread::sleep(Duration::from_millis(1));
} else {
if statr & WPROTECT_ERR_MASK != 0 {
return Err(Error::Custom("flash write protect error".to_string()));
}
self.write_mem32(FLASH_STATR, statr)?; // write 1 to clear EOP
break;
}
}
// read 1 word to verify
let word = self.read_mem32(address)?;
println!("=> {:08x}", word);
// end erase, disable page erase
self.modify_mem32(FLASH_CTLR, |r| r & (!PAGE_ERASE_MASK))?;
self.lock_flash()?;
Ok(())
}
pub fn fast_erase_32k(&mut self, address: u32) -> Result<()> {
// require unlock
self.unlock_flash()?;
const FLASH_STATR: u32 = 0x4002200C;
const BUSY_MASK: u32 = 0x00000001;
const START_MASK: u32 = 1 << 6;
const WPROTECT_ERR_MASK: u32 = 1 << 4;
const FLASH_ADDR: u32 = 0x40022014;
const FLASH_CTLR: u32 = 0x40022010;
const BLOCK_ERASE_32K_MASK: u32 = 1 << 18;
if address & 0x7fff != 0 {
return Err(Error::Custom(
"address must be 32k bytes aligned".to_string(),
));
}
let statr = self.read_mem32(FLASH_STATR)?;
// check if busy
if statr & BUSY_MASK != 0 {
return Err(Error::Custom("flash busy".to_string()));
}
self.modify_mem32(FLASH_CTLR, |r| r | BLOCK_ERASE_32K_MASK)?;
self.write_mem32(FLASH_ADDR, address)?;
self.modify_mem32(FLASH_CTLR, |r| r | START_MASK)?;
loop {
let statr = self.read_mem32(FLASH_STATR)?;
// check if busy
if statr & BUSY_MASK != 0 {
thread::sleep(Duration::from_millis(1));
} else {
if statr & WPROTECT_ERR_MASK != 0 {
return Err(Error::Custom("flash write protect error".to_string()));
}
self.write_mem32(FLASH_STATR, statr)?; // write 1 to clear EOP
break;
}
}
// read 1 word to verify
let word = self.read_mem32(address)?;
println!("=> {:08x}", word);
// end erase
// disable page erase
self.modify_mem32(FLASH_CTLR, |r| r & (!BLOCK_ERASE_32K_MASK))?;
self.lock_flash()?;
Ok(())
}
pub fn erase_all(&mut self) -> Result<()> {
const FLASH_STATR: u32 = 0x4002200C;
const BUSY_MASK: u32 = 0x00000001;
const FLASH_CTLR: u32 = 0x40022010;
const MASS_ERASE_MASK: u32 = 1 << 2; // MER
const START_MASK: u32 = 1 << 6;
self.unlock_flash()?;
self.modify_mem32(FLASH_CTLR, |r| r | MASS_ERASE_MASK)?;
self.modify_mem32(FLASH_CTLR, |r| r | START_MASK)?;
let statr = self.wait_mem32(FLASH_STATR, |r| r & BUSY_MASK == 0)?;
self.write_mem32(FLASH_STATR, statr)?; // write 1 to clear EOP
// clear MER
self.modify_mem32(FLASH_CTLR, |r| r & (!MASS_ERASE_MASK))?;
Ok(())
}
/// Program bytes.
///
/// # Arguments
///
/// * `address` - The start address of the flash page to program.
/// * `data` - The data to be written to the page.
///
/// The page must be erased first
pub fn program_page(&mut self, address: u32, data: &[u8]) -> Result<()> {
// require unlock
self.unlock_flash()?;
const FLASH_STATR: u32 = 0x4002200C;
const BUSY_MASK: u32 = 0x00000001;
const WRITE_BUSY_MASK: u32 = 1 << 1;
const WPROTECT_ERR_MASK: u32 = 1 << 4;
const FLASH_CTLR: u32 = 0x40022010;
const PAGE_START_MASK: u32 = 1 << 21; // start page program
const PAGE_PROG_MASK: u32 = 1 << 16; //
if address & 0xff != 0 {
return Err(Error::Custom(
"address must be 256 bytes aligned".to_string(),
));
}
// check if busy
let statr = self.read_mem32(FLASH_STATR)?;
if statr & BUSY_MASK != 0 {
return Err(Error::Custom("flash busy".to_string()));
}
//let ctlr = self.read_mem32(FLASH_CTLR)?;
//let ctlr = ctlr | PAGE_PROG_MASK;
//self.write_mem32(FLASH_CTLR, ctlr)?;
self.modify_mem32(FLASH_CTLR, |r| r | PAGE_PROG_MASK)?;
for (i, word) in data.chunks(4).enumerate() {
let word = u32::from_le_bytes(word.try_into().unwrap());
self.write_mem32(address + (i as u32 * 4), word)?;
// write busy wait
self.wait_mem32(FLASH_STATR, |r| r & WRITE_BUSY_MASK == 0)?;
}
// start fast page program
self.modify_mem32(FLASH_CTLR, |r| r | PAGE_START_MASK)?;
// busy wait
let statr = self.wait_mem32(FLASH_STATR, |r| r & BUSY_MASK == 0)?;
self.write_mem32(FLASH_STATR, statr)?; // write 1 to clear EOP
if statr & WPROTECT_ERR_MASK != 0 {
return Err(Error::Custom("flash write protect error".to_string()));
}
// verify
// read 1 word to verify
//let word = self.read_mem32(address)?;
//println!("=> {:08x}", word);
// end program, clear PAGE_PROG
//let ctlr = self.read_mem32(FLASH_CTLR)?;
//let ctlr = ctlr & (!PAGE_PROG_MASK); // disable page erase
//self.write_mem32(FLASH_CTLR, ctlr)?;
self.modify_mem32(FLASH_CTLR, |r| r & (!PAGE_PROG_MASK))?;
self.lock_flash()?;
Ok(())
}
}