hc
2024-08-12 233ab1bd4c5697f5cdec94e60206e8c6ac609b4c
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
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
/*
 * Copyright (C) 2019 Spreadtrum Communications Inc.
 *
 * Authors    : xiaodong.bi
 *
 * This software is licensed under the terms of the GNU General Public
 * License version 2, as published by the Free Software Foundation, and
 * may be copied, distributed, and modified under those terms.
 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */
 
#include "wcn_swd_dp.h"
 
#ifdef CONFIG_WCN_PCIE
static void swd_ext_sel(bool enable)
{
   u32 *reg = (u32 *)(pcie_bar_vmem(4) + 0x1c);
   u32 *ahb_ctl = (u32 *)(pcie_bar_vmem(0) + 0x001303e8);/* dis sec */
 
   *ahb_ctl = 0x73e;
   if (enable)
       *reg |= (BIT(7) | BIT(6) | BIT(5));
   else
       *reg &= ~(BIT(7) | BIT(6) | BIT(5));
}
 
static void swclk_clr(void)
{
   u32 *reg = (u32 *)(pcie_bar_vmem(4) + 0x1c);
 
   *reg &= ~BIT(6);
}
 
static void swclk_set(void)
{
   u32 *reg = (u32 *)(pcie_bar_vmem(4) + 0x1c);
 
   *reg |= BIT(6);
}
 
static void sw_dio_out(u32 value)
{
   u32 *reg = (u32 *)(pcie_bar_vmem(4) + 0x1c);
 
   if (value & BIT(0))
       *reg |= BIT(5);
   else
       *reg &= ~BIT(5);
}
 
static u32 sw_dio_in(void)
{
   u32 *reg = (u32 *)(pcie_bar_vmem(4) + 0xc);
   u32 bit;
 
   bit = (*reg & BIT(7)) >> 7;
 
   return bit;
}
#elif defined CONFIG_WCN_USB
static void swd_ext_sel(bool enable)
{
}
 
static void swclk_clr(void)
{
}
 
static void swclk_set(void)
{
}
 
static void sw_dio_out(u32 value)
{
}
 
static u32 sw_dio_in(void)
{
   return 0;
}
#else
static void swd_ext_sel(bool enable)
{
   unsigned char reg;
 
   sdiohal_aon_readb(DAP_ADDR+0x0E, &reg);
   pr_info("reg value is:0x%x\n", reg);
 
   if (enable)
       reg |= (BIT(7) | BIT(6) | BIT(5));
   else
       reg &= ~(BIT(7) | BIT(6) | BIT(5));
   sdiohal_aon_writeb(DAP_ADDR+0x0E, reg);
   sdiohal_aon_readb(DAP_ADDR+0x0E, &reg);
   pr_info("reg value is:0x%x\n", reg);
}
 
static void swclk_clr(void)
{
   unsigned char reg;
 
   sdiohal_aon_readb(DAP_ADDR+0x0e, &reg);
   reg &= ~BIT(6);
   sdiohal_aon_writeb(DAP_ADDR+0x0E, reg);
}
 
static void swclk_set(void)
{
   unsigned char reg;
 
   sdiohal_aon_readb(DAP_ADDR+0x0E, &reg);
   reg |= BIT(6);
   sdiohal_aon_writeb(DAP_ADDR+0x0E, reg);
}
 
static void sw_dio_out(u32 value)
{
   unsigned char reg;
 
   sdiohal_aon_readb(DAP_ADDR+0x0E, &reg);
   if (value & BIT(0))
       reg |= BIT(5);
   else
       reg &= ~BIT(5);
   sdiohal_aon_writeb(DAP_ADDR+0x0E, reg);
}
 
static u32 sw_dio_in(void)
{
   unsigned char reg;
   u32 bit;
 
   sdiohal_aon_readb(DAP_ACK_ADDR, &reg);
   bit = (reg & BIT(7)) >> 7;
 
   return bit;
}
#endif
 
static void swd_clk_cycle(void)
{
   swclk_set();
   ndelay(100);
   swclk_clr();
   ndelay(100);
}
 
static void swd_write_bit(u32 bit)
{
   swclk_set();
   sw_dio_out(bit);
   ndelay(100);
   swclk_clr();
   ndelay(100);
}
 
static u32 swd_read_bit(void)
{
   u32 bit;
 
   swclk_set();
   ndelay(100);
   bit = sw_dio_in();
   swclk_clr();
   ndelay(100);
 
   return bit;
}
 
static void swd_insert_cycles(u32 n)
{
   u32 i;
 
   for (i = 0; i < n; i++)
       swd_clk_cycle();
}
 
u32 swd_transfer(u8 cmd, u32 *data)
{
   u32 ack = 0;
   u32 bit;
   u32 val;
   u32 parity;
   u32 n;
 
   parity = 0;
   /* Start Bit */
   swd_write_bit(1);
   bit = ((cmd >> 0) & BIT(0));
   /* APnDP Bit */
   swd_write_bit(bit);
   parity += bit;
   bit = ((cmd >> 1) & BIT(0));
   /* RnW Bit */
   swd_write_bit(bit);
   parity += bit;
   bit = ((cmd >> 2) & BIT(0));
   /* A2 Bit */
   swd_write_bit(bit);
   parity += bit;
   bit = ((cmd >> 3) & BIT(0));
   /* A3 Bit */
   swd_write_bit(bit);
   parity += bit;
   /* Parity Bit */
   swd_write_bit(parity & BIT(0));
   /* Stop Bit */
   swd_write_bit(0);
   /* Park Bit */
   swd_write_bit(1);
 
   /* Turnaround */
   swd_insert_cycles(1);
 
   /* Acknowledge response */
   bit = swd_read_bit();
   ack |= bit << 0;
   bit = swd_read_bit();
   ack |= bit << 1;
   bit = swd_read_bit();
   ack |= bit << 2;
 
   if (ack == DAP_TRANSFER_OK) {
       /* Data transfer */
       if (cmd & DAP_TRANSFER_RnW) {
           val = 0;
           parity = 0;
           /* Read DATA[0:31] */
           for (n = 0; n < 32; n++) {
               bit = swd_read_bit();
               parity += bit;
               val |= (bit << n);
           }
 
           /* Read Parity */
           bit = swd_read_bit();
           if ((parity ^ bit) & 1)
               ack = DAP_TRANSFER_ERROR;
 
           if (data)
               *data = val;
 
           /* Turnaround */
           swd_insert_cycles(1);
       } else {
           /* Turnaround */
           swd_insert_cycles(1);
           sw_dio_out(0);
 
           val = *data;
           parity = 0;
           /* Write WDATA[0:31] */
           for (n = 0; n < 32; n++) {
               bit = val & BIT(0);
               swd_write_bit(bit);
               parity += bit;
               val >>= 1;
           }
           /* Write Parity Bit */
           swd_write_bit(parity & BIT(0));
           /* Turnaround */
           swd_insert_cycles(1);
       }
 
       /* Idle cycles >= 8 */
       sw_dio_out(0);
       swd_insert_cycles(8);
 
       return ack;
   }
 
   if ((ack == DAP_TRANSFER_WAIT) || (ack == DAP_TRANSFER_FAULT)) {
       if ((cmd & DAP_TRANSFER_RnW) != 0)
           swd_insert_cycles(32+1);
       else {
           /* Turnaround */
           swd_insert_cycles(1);
           if ((cmd & DAP_TRANSFER_RnW) == 0)
               swd_insert_cycles(32 + 1);
       }
 
       /* Idle cycles >= 8 */
       sw_dio_out(0);
       swd_insert_cycles(8);
 
       return ack;
   }
 
   /* Turnaround */
   swd_insert_cycles(1);
   swd_insert_cycles(32+1);
 
   /* Idle cycles >= 8 */
   sw_dio_out(0);
   swd_insert_cycles(8);
 
   return ack;
}
 
static void swd_send_nbytes(u8 *buf, int nbytes)
{
   u8 i, j;
   u8 dat;
 
   for (i = 0; i < nbytes; i++) {
       dat = buf[i];
       for (j = 0; j < 8; j++) {
           if ((dat & 0x80) == 0x80)
               swd_write_bit(1);
           else
               swd_write_bit(0);
           dat <<= 1;
       }
   }
}
 
 
static u32 swd_dap_read(u8 reg, u32 *data)
{
   u32 ack;
 
   reg &= ~DAP_TRANSFER_APnDP;
   reg |= DAP_TRANSFER_RnW;
   ack = swd_transfer(reg, data);
 
   return ack;
}
 
static u32 swd_dap_write(u8 reg, u32 *data)
{
   u32 ack;
 
   reg &= ~(DAP_TRANSFER_APnDP | DAP_TRANSFER_RnW);
   ack = swd_transfer(reg, data);
 
   return ack;
}
 
static u32 swd_ap_read(u8 reg, u32 *data)
{
   u32 ack;
 
   reg |= DAP_TRANSFER_APnDP | DAP_TRANSFER_RnW;
   ack = swd_transfer(reg, data);
 
   return ack;
}
 
static u32 swd_ap_write(u32 reg, u32 *data)
{
   u32 ack;
 
   reg &= ~(DAP_TRANSFER_RnW);
   reg |= DAP_TRANSFER_APnDP;
   ack = swd_transfer(reg, data);
 
   return ack;
}
 
static u32 swd_memap_read(u32 addr, u32 *data)
{
   u8 reg;
   u32 ack;
 
   reg = DAP_TRANSFER_A2;
   ack = swd_ap_write(reg, &addr);
 
   reg = DAP_TRANSFER_A2 | DAP_TRANSFER_A3;
   ack = swd_ap_read(reg, data);
   ack = swd_ap_read(reg, data);
 
   return ack;
}
 
static u32 swd_memap_write(u32 addr, u32 *data)
{
   u8 reg;
   u32 ack;
 
   reg = DAP_TRANSFER_A2;
   ack = swd_ap_write(reg, &addr);
 
   reg = DAP_TRANSFER_A2 | DAP_TRANSFER_A3;
   ack = swd_ap_write(reg, data);
 
   return ack;
}
 
static void swd_read_arm_core(void)
{
   u32 value;
   u32 index;
   u32 addr;
   static const char *core_reg_name[19] = {
       "R0 ", "R1 ", "R2 ", "R3 ", "R4 ", "R5 ", "R6 ", "R7 ", "R8 ",
       "R9 ", "R10", "R11", "R12", "R13", "R14", "R15", "PSR", "MSP",
       "PSP",
   };
 
/* reg arm reg */
   for (index = 0; index < 19; index++) {
       addr = 0xe000edf4;
       swd_memap_write(addr, &index);
       addr = 0xe000edf8;
       swd_memap_read(addr, &value);
       pr_info("%s %s:0x%x\n", __func__, core_reg_name[index], value);
   }
}
 
/* MSB first */
u8 swd_wakeup_seq[16] = {
   0x49, 0xCF, 0x90, 0x46,
   0xA9, 0xB4, 0xA1, 0x61,
   0x97, 0xF5, 0xBB, 0xC7,
   0x45, 0x70, 0x3D, 0x98,
};
 
/* LSB first */
u8 swd_wakeup_seq2[16] = {
   0x19, 0xBC, 0x0E, 0xA2,
   0xE3, 0xDD, 0xAF, 0xE9,
   0x86, 0x85, 0x2D, 0x95,
   0x62, 0x09, 0xF3, 0x92,
};
 
u8 swd_to_ds_seq[2] = {
   0x3d, 0xc7,
};
 
static void swd_dormant_to_wake(void)
{
   u8  data;
 
   /* Send at least eight SWCLKTCK cycles with SWDIOTMS HIGH */
   sw_dio_out(1);
   swd_insert_cycles(8);
 
   /* 128 bit Selection Alert sequence */
   swd_send_nbytes(swd_wakeup_seq, 16);
 
   /* four SWCLKTCK cycles with SWDIOTMS LOW */
   sw_dio_out(0);
   swd_insert_cycles(4);
 
   /* Send the activation code */
   data = 0x58;
   swd_send_nbytes(&data, 1);
 
   swd_insert_cycles(1);
   sw_dio_out(0);
}
 
static void swd_wake_to_dormant(void)
{
 
   /* Send at least eight SWCLKTCK cycles with SWDIOTMS HIGH */
   sw_dio_out(1);
   swd_insert_cycles(8);
 
   /* 16-bit SWD-to-DS select sequence */
   swd_send_nbytes(swd_to_ds_seq, 2);
   swd_insert_cycles(1);
}
 
static void switch_jtag_to_swd(void)
{
 
   u8 data[2];
 
   /* Send at least 50 SWCLKTCK cycles with SWDIOTMS HIGH */
   sw_dio_out(1);
   swd_insert_cycles(50);
 
   /* send the 16-bit JTAG-to-SWD select sequence */
   data[0] = 0x79;
   data[1] = 0xe7;
   swd_send_nbytes(data, 2);
 
   /* Send at least 50 SWCLKTCK cycles with SWDIOTMS HIGH */
   swd_insert_cycles(50);
   sw_dio_out(0);
}
 
static void swd_line_reset(void)
{
   /* Complete SWD reset sequence
    *(50 cycles high followed by 2 or more idle cycles)
    */
   sw_dio_out(1);
   swd_insert_cycles(50);
   sw_dio_out(0);
   swd_insert_cycles(2);
}
 
static void swd_read_dpidr(void)
{
   u32 ack, data = 0;
 
   /* the dp idr is 0x0be12477*/
   ack = swd_dap_read(DP_IDCODE, &data);
 
   pr_info("%s idcode:0x%x\n", __func__, data);
}
 
static void swd_read_apidr(void)
{
   u32 ack, data = 0;
 
   data = (0xf << 4);
   ack = swd_dap_write(DP_SELECT, &data);
 
   /* the dp idr is 0x14770015*/
   ack = swd_ap_read(AP_IDCODE, &data);
   ack = swd_ap_read(AP_IDCODE, &data);
   pr_info("%s idcode:0x%x\n", __func__, data);
 
   data = (0x0 << 4);
   ack = swd_dap_write(DP_SELECT, &data);
}
 
 
u32 swd_sel_target(u8 cmd, u32 *data)
{
   u32 bit, parity;
   u32 n, val;
 
   sw_dio_out(1);
   swd_insert_cycles(50);
 
   sw_dio_out(0);
   swd_insert_cycles(2);
 
   parity = 0;
   /* Start Bit */
   swd_write_bit(1);
   bit = ((cmd >> 0) & BIT(0));
   /* APnDP Bit */
   swd_write_bit(bit);
   parity += bit;
   bit = ((cmd >> 1) & BIT(0));
   /* RnW Bit */
   swd_write_bit(bit);
   parity += bit;
   bit = ((cmd >> 2) & BIT(0));
   /* A2 Bit */
   swd_write_bit(bit);
   parity += bit;
   bit = ((cmd >> 3) & BIT(0));
   /* A3 Bit */
   swd_write_bit(bit);
   parity += bit;
   /* Parity Bit */
   swd_write_bit(parity & BIT(0));
   /* Stop Bit */
   swd_write_bit(0);
   /* Park Bit */
   swd_write_bit(1);
 
   /* Turnaround */
   swd_insert_cycles(5);
 
   val = *data;
   parity = 0;
   /* Write WDATA[0:31] */
   for (n = 0; n < 32; n++) {
       bit = (val & BIT(0));
       swd_write_bit(bit);
       parity += bit;
       val >>= 1;
   }
   /* Write Parity Bit */
   swd_write_bit(parity & BIT(0));
 
   sw_dio_out(0);
   swd_insert_cycles(3);
 
   return 0;
}
 
static void btwf_sys_dap_sel(void)
{
   u32 data = 0;
 
   swd_line_reset();
   data = TARGETSEL_CP;
   swd_sel_target(DP_TARGETSEL, &data);
   swd_read_dpidr();
}
 
static int swd_power_up(void)
{
   u32 data;
 
   pr_info("%s entry\n", __func__);
 
   data = 0x50000000;
   swd_dap_write(DP_CTRL_STAT, &data);
   swd_dap_read(DP_CTRL_STAT, &data);
 
   pr_info("%s read ctrl stat:0x%x\n", __func__, data);
 
   return 0;
}
 
static void swd_device_en(void)
{
   u32 data = 0;
 
   swd_ap_read(AP_CTRL, &data);
   swd_ap_read(AP_CTRL, &data);
   data = (data & 0xffffff88) |  BIT(1) | BIT(6);
   swd_ap_write(AP_STAT, &data);
}
 
/* Debug Exception and Monitor Control Register */
/* (0xe000edfC) = 0x010007f1 */
void swd_set_debug_mode(void)
{
   int ret;
   int addr;
   unsigned int reg_val;
 
   reg_val = 0x010007f1;
   addr = 0xe000edfC;
   ret = swd_memap_write(addr, &reg_val);
   if (ret < 0) {
       pr_info("%s  error:%d\n", __func__, ret);
       return;
   }
 
/*test */
   ret = swd_memap_read(addr, &reg_val);
   if (ret < 0) {
       pr_info("%s  error:%d\n", __func__, ret);
       return;
   }
   pr_info("%s arm debug reg value is 0x%x:\n", __func__, reg_val);
}
 
/*
 * Debug Halting Control status Register
 * (0xe000edf0) = 0xa05f0003
 */
void swd_hold_btwf_core(void)
{
   int ret;
   int addr;
   unsigned int reg_val;
 
   addr = 0xe000edf0;
   reg_val = 0xa05f0003;
 
   ret = swd_memap_write(addr, &reg_val);
   if (ret < 0) {
       pr_info("%s  error:%d\n", __func__, ret);
       return;
   }
/*test */
   ret = swd_memap_read(addr, &reg_val);
   if (ret < 0) {
       pr_info("%s  error:%d\n", __func__, ret);
       return;
   }
   pr_info("%s arm hold btwf reg value is 0x%x:\n", __func__, reg_val);
}
 
int swd_dump_arm_reg(void)
{
   pr_info("%s entry\n", __func__);
 
   swd_ext_sel(true);
   swd_line_reset();
 
   switch_jtag_to_swd();
   swd_dormant_to_wake();
   btwf_sys_dap_sel();
 
   swd_power_up();
   swd_read_apidr();
   swd_device_en();
   swd_hold_btwf_core();
   swd_set_debug_mode();
   swd_read_arm_core();
 
   /* release swd */
   swd_wake_to_dormant();
   swd_ext_sel(false);
 
   pr_info("%s end\n", __func__);
 
   return 0;
}