-
Notifications
You must be signed in to change notification settings - Fork 54
/
tamarin_probe.c
475 lines (393 loc) · 14.5 KB
/
tamarin_probe.c
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
/*
* Licensed under GNU Public License v3
* Copyright (c) 2022 Thomas Roth <[email protected]>
* Based on Picoprobe by:
* Copyright (c) 2021 Raspberry Pi (Trading) Ltd.
*
*/
#include <pico/stdlib.h>
#include <stdio.h>
#include <string.h>
#include <hardware/clocks.h>
#include <hardware/gpio.h>
#include "tamarin_config.h"
#include "probe.pio.h"
#include "tusb.h"
#include "util.h"
// Disable all prints for now
#define serprint(...)
enum TAMARIN_CMDS {
TAMARIN_INVALID = 0, // Invalid command
TAMARIN_READ = 1,
TAMARIN_WRITE = 2,
TAMARIN_LINE_RESET = 3,
TAMARIN_SET_FREQ = 4
};
#define SWD_RSP_OK 0b001
#define SWD_RSP_WAIT 0b010
#define SWD_RSP_FAULT 0b100
static int gDPIDR = 0;
static int gOrigSEL = 0;
#define BITS_ALWYS 0x81
#define BITS_AP (1<<1)
#define BITS_DP (0<<1)
#define BITS_RD (1<<2)
#define BITS_WR (0<<2)
#define PARITY(i) (i<<5)
#define BITS_DP_IDCODE (0b00 << 3)
#define BITS_DP_ABORT (0b00 << 3)
#define BITS_DP_CTRL (0b01 << 3)
#define BITS_DP_RESEND (0b10 << 3)
#define BITS_DP_SELECT (0b10 << 3)
#define BITS_DP_RDBUFF (0b11 << 3)
#define BITS_AP_CSW (0b00 << 3)
#define BITS_AP_TAR (0b01 << 3)
#define BITS_AP_DRW (0b11 << 3)
#define BITS_DP_READ(addr) (((addr) == 0 || ((addr)>>3) == 3) ? PARITY(1) : PARITY(0)) | addr | BITS_RD | BITS_DP | BITS_ALWYS
#define BITS_DP_WRITE(addr) (((addr) == 0 || ((addr)>>3) == 3) ? PARITY(0) : PARITY(1)) | addr | BITS_WR | BITS_DP | BITS_ALWYS
#define BITS_AP_READ(addr) (((addr) == 0 || ((addr)>>3) == 3) ? PARITY(0) : PARITY(1)) | addr | BITS_RD | BITS_AP | BITS_ALWYS
#define BITS_AP_WRITE(addr) (((addr) == 0 || ((addr)>>3) == 3) ? PARITY(1) : PARITY(0)) | addr | BITS_WR | BITS_AP | BITS_ALWYS
#define SWD_DP_read_IDCODE(val) tamarin_tx_read_bare(BITS_DP_READ(BITS_DP_IDCODE),val)
#define SWD_DP_read_CTRL(val) tamarin_tx_read_bare(BITS_DP_READ(BITS_DP_CTRL),val)
#define SWD_DP_read_RESEND(val) tamarin_tx_read_bare(BITS_DP_READ(BITS_DP_RESEND),val)
#define SWD_DP_read_RDBUFF(val) tamarin_tx_read_bare(BITS_DP_READ(BITS_DP_RDBUFF),val)
#define SWD_DP_write_ABORT(val) tamarin_tx_write_bare(BITS_DP_WRITE(BITS_DP_ABORT),val)
#define SWD_DP_write_CTRL(val) tamarin_tx_write_bare(BITS_DP_WRITE(BITS_DP_CTRL),val)
#define SWD_DP_write_SELECT(val) tamarin_tx_write_bare(BITS_DP_WRITE(BITS_DP_SELECT),val)
#define SWD_AP_read_CSW(val) tamarin_tx_read_bare(BITS_AP_READ(BITS_AP_CSW),val)
#define SWD_AP_read_TAR(val) tamarin_tx_read_bare(BITS_AP_READ(BITS_AP_TAR),val)
#define SWD_AP_read_DRW(val) tamarin_tx_read_bare(BITS_AP_READ(BITS_AP_DRW),val)
#define SWD_AP_write_CSW(val) tamarin_tx_write_bare(BITS_AP_WRITE(BITS_AP_CSW),val)
#define SWD_AP_write_TAR(val) tamarin_tx_write_bare(BITS_AP_WRITE(BITS_AP_TAR),val)
#define SWD_AP_write_DRW(val) tamarin_tx_write_bare(BITS_AP_WRITE(BITS_AP_DRW),val)
#define SWD_DP_clear_error() SWD_DP_write_ABORT(0x1e)
// This struct is the direct struct that is sent to
// the probe.
struct __attribute__((__packed__)) tamarin_cmd_hdr {
// Currently unused
uint8_t id;
// One of TAMARIN_CMDS
uint8_t cmd;
// The full (incl. start/stop/parity) SWD command
// Unused for LINE_RESET and SET_FREQ.
uint8_t request;
// The data for writes (unused otherwise)
uint32_t data;
// Number of (8 bit) idle cycles to perform after this op
uint8_t idle_cycles;
};
// This is the struture returned by the probe for each command
struct __attribute__((__packed__)) tamarin_res_hdr {
// Unused
uint8_t id;
// The (3 bit) result: OK/WAIT/FAULT
uint8_t res;
// The data for reads (undefined otherwise)
uint32_t data;
};
#define PROBE_BUF_SIZE 8192
struct _probe {
// Data received from computer
struct tamarin_cmd_hdr probe_cmd;
// Data that will be sent back to the computer
struct tamarin_res_hdr probe_res;
// PIO offset
uint offset;
};
static struct _probe probe;
void probe_set_swclk_freq(uint freq_khz) {
tamarin_info("Setting SWD frequency to %d kHz\r\n", freq_khz);
uint clk_sys_freq_khz = clock_get_hz(clk_sys) / 1000;
// Worked out with saleae
uint32_t divider = clk_sys_freq_khz / freq_khz / 2;
pio_sm_set_clkdiv_int_frac(pio0, PROBE_SM, divider, 0);
}
static inline void probe_write_bits(uint bit_count, uint32_t data_byte) {
serprint(">> %X (%d)\r\n", data_byte, bit_count);
pio_sm_put_blocking(pio0, PROBE_SM, bit_count - 1);
pio_sm_put_blocking(pio0, PROBE_SM, data_byte);
pio_sm_get_blocking(pio0, PROBE_SM);
}
static inline uint32_t probe_read_bits(uint bit_count) {
pio_sm_put_blocking(pio0, PROBE_SM, bit_count - 1);
uint32_t data = pio_sm_get_blocking(pio0, PROBE_SM);
data = data >> (32-bit_count);
serprint("<< %X (%d)\r\n", data, bit_count);
return data;
}
static void probe_read_mode(void) {
pio_sm_exec(pio0, PROBE_SM, pio_encode_jmp(probe.offset + probe_offset_in_posedge));
while(pio0->dbg_padoe & (1 << PROBE_PIN_SWDIO));
}
static void probe_write_mode(void) {
pio_sm_exec(pio0, PROBE_SM, pio_encode_jmp(probe.offset + probe_offset_out_negedge));
while(!(pio0->dbg_padoe & (1 << PROBE_PIN_SWDIO)));
}
void tamarin_start_probe() {
// set to output
pio_sm_set_consecutive_pindirs(pio0, PROBE_SM, PROBE_PIN_OFFSET, 2, true);
// Enable SM
pio_sm_set_enabled(pio0, PROBE_SM, 1);
// Jump to write program
// probe_write_mode();
probe_read_mode();
// probe_enabled = true;
}
void tamarin_probe_init() {
// Funcsel pins
pio_gpio_init(pio0, PROBE_PIN_SWCLK);
pio_gpio_init(pio0, PROBE_PIN_SWDIO);
// Make sure SWDIO has a pullup on it. Idle state is high
gpio_pull_up(PROBE_PIN_SWDIO);
// Target reset pin: pull up, input to emulate open drain pin
gpio_pull_up(PROBE_PIN_RESET);
// gpio_init will leave the pin cleared and set as input
gpio_init(PROBE_PIN_RESET);
uint offset = pio_add_program(pio0, &probe_program);
probe.offset = offset;
pio_sm_config sm_config = probe_program_get_default_config(offset);
// Set SWCLK as a sideset pin
sm_config_set_sideset_pins(&sm_config, PROBE_PIN_SWCLK);
// Set SWDIO offset
sm_config_set_out_pins(&sm_config, PROBE_PIN_SWDIO, 1);
sm_config_set_set_pins(&sm_config, PROBE_PIN_SWDIO, 1);
sm_config_set_in_pins(&sm_config, PROBE_PIN_SWDIO);
// Set pins to input to ensure we don't pull the line low unnecessarily
pio_sm_set_consecutive_pindirs(pio0, PROBE_SM, PROBE_PIN_OFFSET, 2, false);
// shift output right, autopull off, autopull threshold
sm_config_set_out_shift(&sm_config, true, false, 0);
// shift input right as swd data is lsb first, autopush off
sm_config_set_in_shift(&sm_config, true, false, 0);
// Init SM with config
pio_sm_init(pio0, PROBE_SM, offset, &sm_config);
// Set up divisor
probe_set_swclk_freq(1000);
gDPIDR = 0;
gOrigSEL = -1;
tamarin_start_probe();
}
void tamarin_probe_deinit() {
gDPIDR = 0;
pio_sm_set_enabled(pio0, PROBE_SM, false);
pio_clear_instruction_memory(pio0);
gpio_disable_pulls(PROBE_PIN_SWDIO);
}
int __not_in_flash_func(tamarin_tx_read_bare)(uint8_t request, uint32_t *value);
void tamarin_line_reset() {
probe_write_mode();
probe_write_bits(32, 0xFFFFFFFF);
probe_write_bits(32, 0xFFFFFFFF);
probe_write_bits(16, 0xe79e);
probe_write_bits(32, 0xFFFFFFFF);
probe_write_bits(32, 0xFFFFFFFF);
// probe_write_bits(8, 0x0);
uint32_t foo;
tamarin_tx_read_bare(0xa5, &foo);
probe_read_mode();
}
int __not_in_flash_func(tamarin_tx_read_bare)(uint8_t request, uint32_t *value) {
// uint8_t data_reversed = reverse(request);
uint32_t result = request;
for(int i=0; i < 5; i++) {
serprint("Read loop");
probe_write_mode();
// Idle cycles just in case
probe_write_bits(32, 0x0);
// probe_write_bits(32, 0x0);
probe_write_bits(32, 0x0);
probe_write_bits(32, 0x0);
probe_write_bits(32, 0x0);
probe_write_bits(8, request);
// Read parity bit
probe_write_bits(1, 1);
probe_read_mode();
result = probe_read_bits(3);
if(result == 2) {
serprint("Read - Probe received wait. Try: %d\r\n", i);
// because we probably have overrun on we need to perform a
// data phase.
uint32_t read_data = probe_read_bits(32);
uint32_t result_parity = probe_read_bits(1);
continue;
}
if(result != 1) {
return result;
}
break;
}
uint32_t read_data = probe_read_bits(32);
uint32_t result_parity = probe_read_bits(1);
*value = read_data;
serprint("READ - Result: %d Data: %08X (%d)\r\n", result, read_data, result_parity);
return result;
}
int __not_in_flash_func(tamarin_tx_write_bare)(uint8_t request, uint32_t value) {
uint32_t value_parity = __builtin_parity(value);
uint32_t result = request;
for(int i=0; i < 5; i++) {
serprint("Write loop");
probe_write_mode();
// Idle cycles... These can be reduced
probe_write_bits(32, 0x0);
probe_write_bits(32, 0x0);
probe_write_bits(32, 0x0);
probe_write_bits(32, 0x0);
probe_write_bits(8, request);
// Read parity bit
probe_write_bits(1, 1);
probe_read_mode();
result = probe_read_bits(3);
if(result == 2) {
serprint("Write - Probe received wait. Try: %d\r\n", i);
// With overrun detection we still have to do the datapahse
probe_write_mode();
// turn
probe_write_bits(1, 0x1);
probe_write_bits(32, 0x0);
probe_write_bits(1, 0x1);
// uint32_t result_parity = probe_read_bits(1);
continue;
}
if(result != 1) {
return result;
}
break;
}
// Another turn!
probe_write_mode();
probe_write_bits(1, 1);
// Write the actual data
probe_write_bits(32, value);
// Write parity bit
probe_write_bits(1, value_parity);
probe_read_mode();
serprint("READ - Result: %d\r\n", result);
return result;
}
bool probe_enabled = false;
void probe_handle_pkt(void) {
struct tamarin_cmd_hdr *cmd = &probe.probe_cmd;
tamarin_debug("Processing packet: ID: %u Command: %u Request: 0x%02X Data: 0x%08X Idle: %d\r\n", cmd->id, cmd->cmd, cmd->request, cmd->data, cmd->idle_cycles);
int result = 0;
uint32_t data = 0;
switch(cmd->cmd) {
case TAMARIN_READ:
tamarin_debug("Executing read\r\n");
result = tamarin_tx_read_bare(cmd->request, &data);
tamarin_debug("Read: %d 0x%08X", result, data);
break;
case TAMARIN_WRITE:
tamarin_debug("Executing write\r\n");
if (cmd->request == BITS_DP_WRITE(BITS_DP_SELECT)){
gOrigSEL = cmd->data;
}
result = tamarin_tx_write_bare(cmd->request, cmd->data);
tamarin_debug("Wrrite: %d 0x%08X", result, cmd->data);
break;
case TAMARIN_LINE_RESET:
tamarin_debug("Executing line reset\r\n");
tamarin_line_reset();
break;
case TAMARIN_SET_FREQ:
tamarin_debug("Executing set frequency\r\n");
probe_set_swclk_freq(cmd->data);
break;
default:
tamarin_debug("UNKNOWN COMMAND!\r\n");
break;
}
// For now we just reply with a default command
struct tamarin_res_hdr res;
res.id = 33;
res.data = data;
res.res = result;
tud_vendor_write((char*)&res, sizeof(res));
}
int SWD_readmem(uint32_t addr, uint32_t *data){
int result = 0;
result = SWD_AP_write_TAR(addr);
if (result != SWD_RSP_OK) goto cleanup;
result = SWD_AP_read_DRW(data);
if (result != SWD_RSP_OK) goto cleanup;
result = SWD_DP_read_RDBUFF(data);
if (result != SWD_RSP_OK) goto cleanup;
cleanup:;
return result;
}
void handle_SPAM(void){
int result = 0;
uint32_t data = 0;
int hasdata = 0;
if (!gDPIDR){
probe_set_swclk_freq(1000);
tamarin_line_reset();
SWD_DP_clear_error();
result = SWD_DP_read_IDCODE(&data);
if (data != 0 && result == SWD_RSP_OK){
gDPIDR = data;
result = SWD_DP_write_CTRL(0x50000000);
}
}else{
uint32_t uart_ctrl_reg = 0;
if (gDPIDR == 0x5ba02477){
//s7002
uart_ctrl_reg = 0xc6e00004;
}else if (gDPIDR == 0x4ba02477){
//s8002
uart_ctrl_reg = 0xC83B401C;
}
if (uart_ctrl_reg){
result = SWD_DP_clear_error();
if (result != SWD_RSP_OK) goto cleanup;
if (gOrigSEL != 0x01000000){
result = SWD_DP_write_SELECT(0x01000000);
if (result != SWD_RSP_OK) goto cleanup;
if (gOrigSEL == -1) gOrigSEL = 0x01000000;
}
result = SWD_AP_write_CSW(0xA2000012);
if (result != SWD_RSP_OK) goto cleanup;
int cnt = 1;
uint32_t payload = 0;
while (cnt > 0){
result = SWD_readmem(uart_ctrl_reg + 0x00,&data);
if (result != SWD_RSP_OK) goto cleanup;
cnt = data & 0x7f;
if (!cnt) break;
hasdata = 1;
tud_cdc_n_write_char(ITF_DCSD, data >> 8);
cnt--;
result = SWD_readmem(uart_ctrl_reg + 0x0C,&payload);
if (result != SWD_RSP_OK) goto cleanup;
int sendSize = cnt > sizeof(payload) ? sizeof(payload) : cnt;
tud_cdc_n_write(ITF_DCSD, &payload, sendSize);
}
cleanup:;
if (result == SWD_RSP_FAULT){
data = 0;
result = SWD_DP_read_CTRL(&data);
if (result == 99) return;
}
if (gOrigSEL != 0x01000000){
result = SWD_RSP_WAIT;
for (int i=0; i<100 && result == SWD_RSP_WAIT; i++){
result = SWD_DP_write_SELECT(gOrigSEL);
}
}
}
}
if (hasdata) tud_cdc_n_write_flush(ITF_DCSD);
return;
}
// USB bits
void tamarin_probe_task(int doSPAM) {
if ( tud_vendor_available() ) {
char tmp_buf[64];
uint count = tud_vendor_read(tmp_buf, 64);
if (count == 0) {
return;
}
memcpy(&probe.probe_cmd, tmp_buf, sizeof(struct tamarin_cmd_hdr));
probe_handle_pkt();
}
if (doSPAM) handle_SPAM();
}