-
Notifications
You must be signed in to change notification settings - Fork 53
/
qemu-esp32.tar
25228 lines (21967 loc) · 660 KB
/
qemu-esp32.tar
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
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
hw/xtensa/esp32.c 0000777 0001750 0001750 00000320401 13426600005 012561 0 ustar olof olof /*
* Copyright (c) 2016, Max Filippov, Open Source and Linux Lab.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the Open Source and Linux Lab nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#if 0
ULP memory
0x50000000 8kb
esp_err_t ulp_run(uint32_t entry_point)
{
SET_PERI_REG_MASK(SARADC_SAR_START_FORCE_REG, SARADC_ULP_CP_FORCE_START_TOP_M);
SET_PERI_REG_BITS(SARADC_SAR_START_FORCE_REG, SARADC_PC_INIT_V, entry_point, SARADC_PC_INIT_S);
SET_PERI_REG_MASK(SARADC_SAR_START_FORCE_REG, SARADC_ULP_CP_START_TOP_M);
return ESP_OK;
}
#endif
#include "qemu/osdep.h"
#include "qapi/error.h"
#include "qemu-common.h"
#include "cpu.h"
#include "sysemu/sysemu.h"
#include "hw/boards.h"
#include "hw/loader.h"
#include "qemu/units.h"
#include "elf.h"
#include "exec/memory.h"
#include "exec/address-spaces.h"
#include "hw/char/serial.h"
#include "net/net.h"
#include "hw/sysbus.h"
#include "hw/block/flash.h"
#include "sysemu/block-backend.h"
//#include "sysemu/char.h"
#include "chardev/char.h"
#include "sysemu/device_tree.h"
#include "qemu/error-report.h"
#include "bootparam.h"
#include "qemu/timer.h"
#include "inttypes.h"
#include "hw/isa/isa.h"
//#include "hw/i2c/i2c_esp32.h"
#include <poll.h>
#include <error.h>
#include "esp32_sha.h"
// From Memorymapped.cpp
extern const unsigned char* get_flashMemory();
typedef struct Esp32 {
XtensaCPU *cpu[2];
unsigned int gpio_reg[600/4];
qemu_irq pro_to_app_yield_irq;
qemu_irq app_to_pro_yield_irq;
} Esp32;
XtensaCPU *APPcpu = NULL;
XtensaCPU *PROcpu = NULL;
//pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
//
QemuMutex mutex;
//pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
typedef struct connect_data {
int socket;
int uart_num;
} connect_data;
qemu_irq uart0_irq;
qemu_irq uart1_irq;
void *connection_handler(void *connect);
void *gdb_socket_thread(void *dummy);
#define DEBUG_LOG(...) fprintf(stdout, __VA_ARGS__)
#define DEFINE_BITS(prefix, reg, field, shift, len) \
prefix##_##reg##_##field##_SHIFT = shift, \
prefix##_##reg##_##field##_LEN = len, \
prefix##_##reg##_##field = ((~0U >> (32 - (len))) << (shift))
/* Serial */
enum {
ESP32_UART_FIFO,
ESP32_UART_INT_RAW,
ESP32_UART_INT_ST,
ESP32_UART_INT_ENA,
ESP32_UART_INT_CLR,
ESP32_UART_CLKDIV,
ESP32_UART_AUTOBAUD,
ESP32_UART_STATUS, // ESP32_UART_txfifo_cnt = 0x1c/4,
ESP32_UART_CONF0,
ESP32_UART_CONF1,
ESP32_UART_LOWPULSE,
ESP32_UART_HIGHPULSE,
ESP32_UART_RXD_CNT,
ESP32_UART_DATE = 0x78 / 4,
ESP32_UART_ID,
ESP32_UART_MAX,
};
#define ESP32_UART_BITS(reg, field, shift, len) \
DEFINE_BITS(ESP32_UART, reg, field, shift, len)
enum {
ESP32_UART_BITS(INT, RXFIFO_FULL, 0, 1),
ESP32_UART_BITS(INT, TXFIFO_EMPTY, 1, 1),
ESP32_UART_BITS(INT, RXFIFO_OVF, 4, 1),
};
enum {
ESP32_UART_BITS(CONF0, LOOPBACK, 14, 1),
ESP32_UART_BITS(CONF0, RXFIFO_RST, 17, 1),
};
enum {
ESP32_UART_BITS(CONF1, RXFIFO_FULL, 0, 7),
};
#define ESP32_UART_GET(s, _reg, _field) \
extract32(s->reg[ESP32_UART_##_reg], \
ESP32_UART_##_reg##_##_field##_SHIFT, \
ESP32_UART_##_reg##_##_field##_LEN)
#define ESP32_UART_FIFO_SIZE 0x80
#define ESP32_UART_FIFO_MASK 0x7f
#define MAX_GDB_BUFF 4096
typedef struct Esp32SerialState {
MemoryRegion iomem;
CharBackend chr;
ChardevFeature feature;
qemu_irq irq;
unsigned rx_first;
unsigned rx_last;
uint8_t rx[ESP32_UART_FIFO_SIZE];
uint32_t reg[ESP32_UART_MAX];
// These are used to interact with the socket thread
int uart_num;
int guard;
char gdb_serial_data[MAX_GDB_BUFF];
int gdb_serial_buff_rd;
int gdb_serial_buff_tx;
bool gdb_serial_connected;
} Esp32SerialState;
CharBackend* silly_serial=NULL;
static unsigned esp32_serial_rx_fifo_size(Esp32SerialState *s)
{
return (s->rx_last - s->rx_first) & ESP32_UART_FIFO_MASK;
}
static bool esp32_serial_can_receive(void *opaque)
{
Esp32SerialState *s = opaque;
//if (esp32_serial_rx_fifo_size(s) > (ESP32_UART_FIFO_SIZE - 1))
// fprintf(stderr,"FULL\n");
//fprintf(stderr,"FIFO SIZE %d\n",esp32_serial_rx_fifo_size(s));
return esp32_serial_rx_fifo_size(s) < (ESP32_UART_FIFO_SIZE - 1);
}
// b uart_rx_intr_handler_default
static void esp32_serial_irq_update(Esp32SerialState *s)
{
//qemu_mutex_lock_iothread();
//qemu_mutex_unlock_iothread();
s->reg[ESP32_UART_INT_ST] |= s->reg[ESP32_UART_INT_RAW];
//fprintf(stderr,"CHECKING IRQ\n");
if (s->uart_num==0 || s->uart_num==1 || (s->reg[ESP32_UART_INT_ST] & s->reg[ESP32_UART_INT_ENA])) {
//fprintf(stderr,"RAISING IRQ\n");
if (s->uart_num==0) {
qemu_irq_raise(uart0_irq);
}
else if (s->uart_num==1) {
qemu_irq_raise(uart1_irq);
}
else
{
qemu_irq_raise(s->irq);
}
} else {
if (s->uart_num==0) {
qemu_irq_lower(uart0_irq);
}
else if (s->uart_num==1) {
qemu_irq_lower(uart1_irq);
}
else
{
qemu_irq_lower(s->irq);
}
}
// Clear timeout
s->reg[ESP32_UART_INT_RAW] = s->reg[ESP32_UART_INT_RAW] & ~BIT(8);
}
static void esp32_serial_rx_irq_update(Esp32SerialState *s)
{
if (esp32_serial_rx_fifo_size(s) >= ESP32_UART_GET(s, CONF1, RXFIFO_FULL)) {
s->reg[ESP32_UART_INT_RAW] |= ESP32_UART_INT_RXFIFO_FULL;
} else {
s->reg[ESP32_UART_INT_RAW] &= ~ESP32_UART_INT_RXFIFO_FULL;
}
if (!esp32_serial_can_receive(s)) {
s->reg[ESP32_UART_INT_RAW] |= ESP32_UART_INT_RXFIFO_OVF;
} else {
s->reg[ESP32_UART_INT_RAW] &= ~ESP32_UART_INT_RXFIFO_OVF;
}
esp32_serial_irq_update(s);
}
static uint64_t esp32_serial_read(void *opaque, hwaddr addr,
unsigned size)
{
Esp32SerialState *s = opaque;
DEBUG_LOG("%s: +0x%02x: \n", __func__, (uint32_t)addr);
//if ((addr & 3) || size != 4) {
// return 0;
//}
switch (addr / 4) {
case ESP32_UART_INT_ST:
{
if (s->uart_num==0) {
qemu_irq_lower(uart0_irq);
}
else if (s->uart_num==1) {
qemu_irq_lower(uart1_irq);
}
else
{
qemu_irq_lower(s->irq);
}
return s->reg[ESP32_UART_INT_ST];
}
case ESP32_UART_STATUS:
DEBUG_LOG("%s: ESP32_UART_STATUS +0x%02x: \n", __func__, (uint32_t)addr);
// return 0;
// TODO!! TODO!! Make read interrupt work for UARTS!
return esp32_serial_rx_fifo_size(s);
case ESP32_UART_FIFO:
//fprintf(stderr,"siz %u\n",esp32_serial_rx_fifo_size(s));
if (esp32_serial_rx_fifo_size(s)) {
uint8_t r = s->rx[s->rx_first++];
//fprintf(stderr,"-->%c\n",(char)r);
s->rx_first &= ESP32_UART_FIFO_MASK;
esp32_serial_rx_irq_update(s);
return r;
} else {
//fprintf(stderr,"-->NULL\n");
return 0;
}
case ESP32_UART_CONF1:
DEBUG_LOG("%s: ESP32_UART_CONF1 +0x%02x: \n", __func__, (uint32_t)addr);
case ESP32_UART_INT_RAW:
//case ESP32_UART_INT_ST:
case ESP32_UART_INT_ENA:
case ESP32_UART_INT_CLR:
case ESP32_UART_CLKDIV:
case ESP32_UART_AUTOBAUD:
case ESP32_UART_CONF0:
case ESP32_UART_LOWPULSE:
case ESP32_UART_HIGHPULSE:
case ESP32_UART_RXD_CNT:
case ESP32_UART_DATE:
case ESP32_UART_ID:
return s->reg[addr / 4];
default:
printf("%s: unexpected read @0x%"HWADDR_PRIx"\n", __func__, addr);
//qemu_log("%s: unexpected read @0x%"HWADDR_PRIx"\n", __func__, addr);
break;
}
return 0;
}
static void esp32_serial_receive(void *opaque, const uint8_t *buf, int size)
{
Esp32SerialState *s = opaque;
unsigned i;
fprintf(stderr,"UART socket received %s,%d first%d last%d\n",buf,size,s->rx_first,s->rx_last);
for (i = 0; i < size && esp32_serial_can_receive(s); ++i) {
s->rx[s->rx_last++] = buf[i];
s->rx_last &= ESP32_UART_FIFO_MASK;
}
esp32_serial_rx_irq_update(s);
}
static void esp32_serial_ro(Esp32SerialState *s, hwaddr addr,
uint64_t val, unsigned size)
{
}
static void esp32_serial_tx(Esp32SerialState *s, hwaddr addr,
uint64_t val, unsigned size)
{
DEBUG_LOG("FIFO: %c \n",(char)val);
if (ESP32_UART_GET(s, CONF0, LOOPBACK)) {
if (esp32_serial_can_receive(s)) {
uint8_t buf[] = { (uint8_t)val };
fprintf(stderr,"loopback %c",(char)val);
esp32_serial_receive(s, buf, 1);
}
} else if (true /*s->chr*/) {
uint8_t buf[1] = { val };
qemu_chr_fe_write(&s->chr, buf, 1);
s->reg[ESP32_UART_INT_RAW] |= ESP32_UART_INT_TXFIFO_EMPTY;
esp32_serial_irq_update(s);
}
}
static void esp32_serial_int_ena(Esp32SerialState *s, hwaddr addr,
uint64_t val, unsigned size)
{
s->reg[ESP32_UART_INT_ENA] = val & 0x1ff;
esp32_serial_irq_update(s);
}
static void esp32_serial_int_clr(Esp32SerialState *s, hwaddr addr,
uint64_t val, unsigned size)
{
s->reg[ESP32_UART_INT_ST] &= ~val & 0x1ff;
esp32_serial_irq_update(s);
}
static void esp32_serial_set_conf0(Esp32SerialState *s, hwaddr addr,
uint64_t val, unsigned size)
{
s->reg[ESP32_UART_CONF0] = val & 0xffffff;
if (ESP32_UART_GET(s, CONF0, RXFIFO_RST)) {
s->rx_first = s->rx_last = 0;
esp32_serial_rx_irq_update(s);
}
}
#if 0
static void esp_serial_timeout_cb(void *opaque)
{
Esp32SerialState *s = opaque;
int64_t now=qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
DEBUG_LOG("%s: SERIAL_TIMEOUT +0x%PRIx6402x: \n", __func__, now);
if (now-s->start_timeout > 1000000) {
// 1 second timeout irq
DEBUG_LOG("%s: SERIAL_TIMEOUT +0x%PRIx6402x: \n", __func__, now);
s->reg[ESP32_UART_INT_RAW] |= BIT(8);
esp32_serial_rx_irq_update(s);
s->start_timeout=now;
}
//timer_mod_ns(s->timeout_timer,1000000000000);
}
static void esp32_serial_set_conf1(Esp32SerialState *s, hwaddr addr,
uint64_t val, unsigned size)
{
DEBUG_LOG("%s: +0x%02x: \n", __func__, (uint32_t)val);
s->reg[ESP32_UART_CONF1] = val & 0xffffff;
if (s->reg[ESP32_UART_CONF1] && BIT(31)==BIT(31)) {
DEBUG_LOG("%s: TIMEOUT +0x%02x: \n", __func__, (uint32_t)val);
s->start_timeout=qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
s->timeout_timer=timer_new_ns(QEMU_CLOCK_REALTIME, &esp_serial_timeout_cb, s);
timer_mod_ns(s->timeout_timer,1000000000);
}
//if (ESP32_UART_GET(s, CONF1, RXFIFO_RST)) {
// s->rx_first = s->rx_last = 0;
// esp32_serial_rx_irq_update(s);
//}
}
#endif
static void esp32_serial_write(void *opaque, hwaddr addr,
uint64_t val, unsigned size)
{
Esp32SerialState *s = opaque;
DEBUG_LOG("%s: +0x%02x: \n", __func__, (uint32_t)addr);
if (addr==0) {
fprintf(stderr,"%c",(char)val);
//fprintf(stderr,"%c",(char)val);
if (s->gdb_serial_connected) {
qemu_mutex_lock(&mutex);
int pos=s->gdb_serial_buff_tx%MAX_GDB_BUFF;
s->gdb_serial_data[pos]=(char)val;
//fprintf(stderr,"[%c,%c,%d]",(char)val,s->gdb_serial_data[pos],s->gdb_serial_buff_tx);
s->gdb_serial_buff_tx++;
qemu_mutex_unlock(&mutex);
}
}
static void (* const handler[])(Esp32SerialState *s, hwaddr addr,
uint64_t val, unsigned size) = {
[ESP32_UART_FIFO] = esp32_serial_tx,
[ESP32_UART_INT_RAW] = esp32_serial_ro,
[ESP32_UART_INT_ST] = esp32_serial_ro,
[ESP32_UART_INT_ENA] = esp32_serial_int_ena,
[ESP32_UART_INT_CLR] = esp32_serial_int_clr,
[ESP32_UART_STATUS] = esp32_serial_ro,
[ESP32_UART_CONF0] = esp32_serial_set_conf0,
//[ESP32_UART_CONF1] = esp32_serial_set_conf1,
[ESP32_UART_LOWPULSE] = esp32_serial_ro,
[ESP32_UART_HIGHPULSE] = esp32_serial_ro,
[ESP32_UART_RXD_CNT] = esp32_serial_ro,
};
if ((addr & 3) || size != 4 || addr / 4 >= ESP32_UART_MAX) {
return;
}
if (addr / 4 < ARRAY_SIZE(handler) && handler[addr / 4]) {
handler[addr / 4](s, addr, val, size);
} else {
s->reg[addr / 4] = val;
}
}
static void esp32_serial_reset(void *opaque)
{
Esp32SerialState *s = opaque;
memset(s->reg, 0, sizeof(s->reg));
s->reg[ESP32_UART_CLKDIV] = 0x2b6;
s->reg[ESP32_UART_AUTOBAUD] = 0x1000;
s->reg[ESP32_UART_CONF0] = 0x1c;
s->reg[ESP32_UART_CONF1] = 0x6060;
s->reg[ESP32_UART_LOWPULSE] = 0xfffff;
s->reg[ESP32_UART_HIGHPULSE] = 0xfffff;
s->reg[ESP32_UART_DATE] = 0x62000;
s->reg[ESP32_UART_ID] = 0x500;
esp32_serial_irq_update(s);
}
static const MemoryRegionOps esp32_serial_ops = {
.read = esp32_serial_read,
.write = esp32_serial_write,
.endianness = DEVICE_NATIVE_ENDIAN,
};
Esp32SerialState *gdb_serial[4]={0};
static void esp32_serial_event(void *opaque, int event)
{
Esp32SerialState *s = (Esp32SerialState *)opaque;
if (event == CHR_EVENT_BREAK) {
}
}
Esp32SerialState *esp32_serial_init(int uart_num,MemoryRegion *address_space,
hwaddr base, const char *name,
qemu_irq irq,
Chardev *chr_dev);
Esp32SerialState *esp32_serial_init(int uart_num,MemoryRegion *address_space,
hwaddr base, const char *name,
qemu_irq irq,
Chardev *chr_dev)
{
Esp32SerialState *s = g_malloc(sizeof(Esp32SerialState));
s->chr.chr=chr_dev;
s->irq = irq;
s->uart_num=uart_num;
//qemu_chr_add_handlers(s->chr, esp32_serial_can_receive,
// esp32_serial_receive, NULL, s);
memory_region_init_io(&s->iomem, NULL, &esp32_serial_ops, s,
name, 0x100);
memory_region_add_subregion(address_space, base, &s->iomem);
qemu_register_reset(esp32_serial_reset, s);
s->rx_last=s->rx_first=0;
s->guard=0xbeef;
qemu_chr_fe_set_handlers(&s->chr, esp32_serial_can_receive,
esp32_serial_receive, esp32_serial_event,
NULL,s, NULL, true);
if (uart_num==0) {
silly_serial=&s->chr;
}
return s;
}
//////// SPI //////////
/* SPI */
enum {
ESP32_SPI_FLASH_CMD, // 00
ESP32_SPI_FLASH_ADDR, // 04
ESP32_SPI_FLASH_CTRL, // 08
ESP32_SPI_FLASH_CTRL1, // 0C
ESP32_SPI_FLASH_STATUS, // 10
ESP32_SPI_FLASH_CTRL2, // 14
ESP32_SPI_FLASH_CLOCK, // 18
ESP32_SPI_FLASH_USER, // 1c
ESP32_SPI_FLASH_USER1, // 20
ESP32_SPI_FLASH_USER2,
ESP32_MOSI_DLEN,
ESP32_MISO_DLEN,
ESP32_SLV_WR_STATUS, // 30
ESP32_SPI_FLASH_PIN,
ESP32_SPI_FLASH_SLAVE,
ESP32_SPI_FLASH_SLAVE1,
ESP32_SPI_FLASH_SLAVE2, // 40
ESP32_SPI_FLASH_SLAVE3,
ESP32_SLV_WRBUF_DLEN,
ESP32_SLV_RDBUF_DLEN,
ESP32_CACHE_FCTRL, // 50
ESP32_CACHE_SCTRL,
ESP32_SRAM_CMD,
ESP32_SRAM_DRD_CMD,
sram_dwr_cmd, // 60
slv_rd_bit,
reserved_68,
reserved_6c,
reserved_70, // 70
reserved_74,
reserved_78,
reserved_7c,
//uint32_t data_buf[16], /*data buffer*/
data_w0, // 80
data_w1,
data_w2,
data_w3,
data_w4, // 90
data_w5,
data_w6,
data_w7,
data_w8, // a0
data_w9,
data_w10,
data_w11,
data_w12, // b0
data_w13,
data_w14,
data_w15,
tx_crc, // c0 /*For SPI1 the value of crc32 for 256 bits data.*/
reserved_c4,
reserved_c8,
reserved_cc,
reserved_d0, // d0
reserved_d4,
reserved_d8,
reserved_dc,
reserved_e0, // e0
reserved_e4,
reserved_e8,
reserved_ec,
SPI_EXT0_REG, // f0
SPI_EXT1_REG, // f4
SPI_EXT2_REG, // f8
SPI_EXT3_REG,
SPI_DMA_CONF, // 100
SPI_DMA_OUT_LINK,
SPI_DMA_IN_LINK,
SPI_DMA_STATUS,
SPI_DMA_INT_ENA, // 110
SPI_DMA_INT_RAW,
SPI_DMA_INT_ST,
SPI_DMA_INT_CLR, // 11c
SPI_SUC_EOF_DES_ADDR, // 120
SPI_INLINK_DSCR, // 12c
SPI_INLINK_DSCR_BF1, // 130
SPI_OUT_EOF_BFR_DES_ADDR,
SPI_OUT_EOF_DES_ADDR,
SPI_OUTLINK_DSCR,
SPI_OUTLINK_DSCR_BF0, // 140
SPI_OUTLINK_DSCR_BF1,
SPI_DMA_RSTATUS,
SPI_DMA_TSTATUS_REG, // 14c
SPI_DATE_REG=0x3fc/4, // 3fc
R_MAX = 0x100
};
// Redefing for SPI flash
#undef DEBUG_LOG
#define DEBUG_LOG(...) fprintf(stdout, __VA_ARGS__)
#define ESP32_SPI_FLASH_BITS(reg, field, shift, len) \
DEFINE_BITS(ESP32_SPI_FLASH, reg, field, shift, len)
#define ESP32_SPI_GET_VAL(v, _reg, _field) \
extract32(v, \
ESP32_SPI_FLASH_##_reg##_##_field##_SHIFT, \
ESP32_SPI_FLASH_##_reg##_##_field##_LEN)
#define ESP32_SPI_GET(s, _reg, _field) \
extract32(s->reg[ESP32_SPI_FLASH_##_reg], \
ESP32_SPI_FLASH_##_reg##_##_field##_SHIFT, \
ESP32_SPI_FLASH_##_reg##_##_field##_LEN)
#define ESP32_MAX_FLASH_SZ (1 << 24)
// https://github.com/OSLL/qemu-xtensa/pull/4/commits/13666dc20d630c5cc97c968740bd7639e9177221
//ESP8266_SPI_FLASH_BITS(CMD, USR, 18, 1),
//ESP8266_SPI_FLASH_BITS(CMD, CE, 22, 1), // Chip erase
//ESP8266_SPI_FLASH_BITS(CMD, BE, 23, 1), // BLock erase
//ESP8266_SPI_FLASH_BITS(CMD, SE, 24, 1), // Sector erase
//ESP8266_SPI_FLASH_BITS(CMD, PP, 25, 1), // WRITE FLASH
//ESP8266_SPI_FLASH_BITS(CMD, WRDI, 29, 1),
//ESP8266_SPI_FLASH_BITS(CMD, WREN, 30, 1),
//ESP8266_SPI_FLASH_BITS(CMD, READ, 31, 1),
enum {
ESP32_SPI_FLASH_BITS(CMD, USR, 18, 1),
ESP32_SPI_FLASH_BITS(CMD, WRDI, 29, 1),
ESP32_SPI_FLASH_BITS(CMD, WREN, 30, 1),
ESP32_SPI_FLASH_BITS(CMD, READ, 31, 1),
};
enum {
ESP32_SPI_FLASH_BITS(ADDR, OFFSET, 0, 24),
ESP32_SPI_FLASH_BITS(ADDR, LENGTH, 24, 8),
};
enum {
ESP32_SPI_FLASH_BITS(CTRL, ENABLE_AHB, 17, 1),
};
enum {
ESP32_SPI_FLASH_BITS(STATUS, BUSY, 0, 1),
ESP32_SPI_FLASH_BITS(STATUS, WRENABLE, 1, 1),
};
enum {
ESP32_SPI_FLASH_BITS(CLOCK, CLKCNT_L, 0, 6),
ESP32_SPI_FLASH_BITS(CLOCK, CLKCNT_H, 6, 6),
ESP32_SPI_FLASH_BITS(CLOCK, CLKCNT_N, 12, 6),
ESP32_SPI_FLASH_BITS(CLOCK, CLK_DIV_PRE, 18, 13),
ESP32_SPI_FLASH_BITS(CLOCK, CLK_EQU_SYSCLK, 31, 1),
};
enum {
ESP32_SPI_FLASH_BITS(USER, FLASH_MODE, 2, 1),
ESP32_SPI_FLASH_BITS(USER, COMMAND, 30, 1),
ESP32_SPI_FLASH_BITS(USER, ADDR, 31, 1),
};
enum {
ESP32_SPI_FLASH_BITS(USER2, COMMAND_VALUE, 0, 16),
ESP32_SPI_FLASH_BITS(USER2, COMMAND_BITLEN, 28, 4),
};
enum {
ESP32_SPI_FLASH_BITS(ADDR, ADDR_VALUE, 0, 31),
ESP32_SPI_FLASH_BITS(ADDR, ADDR_RESERVED, 24,8),
};
typedef struct Esp32SpiState {
int spiNum;
MemoryRegion iomem;
//MemoryRegion cache;
qemu_irq* irq;
void *flash_image;
int length;
int write_mode;
int wren;
uint32_t reg[R_MAX];
} Esp32SpiState;
static uint64_t esp32_spi_read(void *opaque, hwaddr addr, unsigned size)
{
Esp32SpiState *s = opaque;
DEBUG_LOG("%d %s: +0x%02x: ",s->spiNum, __func__, (uint32_t)addr);
if (addr / 4 >= R_MAX || addr % 4 || size != 4) {
DEBUG_LOG("SPI ADRESS ERROR 0\n");
return 0;
}
DEBUG_LOG("0x%08x\n", s->reg[addr / 4]);
if (s->spiNum!=0) {
if (addr==0x38) {
// SPI_TRANS_DONE
s->reg[addr / 4]=0xff;
}
}
return s->reg[addr / 4];
}
static void esp32_spi_cmd(Esp32SpiState *s, hwaddr addr,
uint64_t val, unsigned size)
{
//DEBUG_LOG("esp32_spi_cmd %08x\n",val);
//s->reg[addr / 4] = val;
// WRITE_PERI_REG(PERIPHS_SPI_FLASH_CMD, SPI_FLASH_PP);
// TODO, WRITE_PERI_REG(PERIPHS_SPI_FLASH_CMD, SPI_FLASH_PP);
// Results in 0 esp32_spi_write: +0x00 = 0x02000000
// This should be when data is written to the flash file
// Information is available in spi_flash_rom_patch.c
if (val & 0x1000000) {
DEBUG_LOG("esp32_spi_cmd_erase??? " PRIx64 "\n",val);
unsigned int write_addr=ESP32_SPI_GET(s, ADDR, OFFSET);
DEBUG_LOG("erase addr %" PRIx64 "\n",write_addr);
//write_addr=s->reg[ESP32_SPI_FLASH_ADDR] >> 8;
// Only allow spi0 to write to flash
if (s->spiNum!=0) {
return;
}
if (write_addr < 4 * 1024 * 1024) {
memset(s->flash_image + write_addr,
0xff,
0x1000); // (ESP32_SPI_GET(s, ADDR, LENGTH) + 3) & 0x3c
}
}
if (val & ESP32_SPI_FLASH_CMD_READ) {
if (ESP32_SPI_GET(s, USER, FLASH_MODE)) {
DEBUG_LOG("%s: READ FLASH 0x%02x@0x%08x\n",
__func__,
ESP32_SPI_GET(s, ADDR, LENGTH),
ESP32_SPI_GET(s, ADDR, OFFSET));
} else {
DEBUG_LOG("%s: READ ?????\n", __func__);
}
}
if (val & ESP32_SPI_FLASH_CMD_WRDI) {
DEBUG_LOG("status wrdi\n");
s->reg[ESP32_SPI_FLASH_STATUS] &= ~ESP32_SPI_FLASH_STATUS_WRENABLE;
}
if (val & ESP32_SPI_FLASH_CMD_WREN) {
DEBUG_LOG("status wren\n");
unsigned int write_addr=ESP32_SPI_GET(s, ADDR, OFFSET);
(void)write_addr;
DEBUG_LOG("Is this address " PRIx64 "\n",write_addr);
// Not sure this is a good idea??
// Where is length field? ESP32_MISO_DLEN
// REG_WRITE(SPI_MISO_DLEN_REG(1), ((ESP_ROM_SPIFLASH_BUFF_BYTE_READ_NUM << 3) - 1) << SPI_USR_MISO_DBITLEN_S);
// 0 esp32_spi_write: +0x2c = 0x000001ff 32 bytes
DEBUG_LOG("len %d\n",s->length);
s->wren=1;
//memcpy(s->flash_image + write_addr,
// &s->reg[data_w0], // ESP32_SPI_GET(s, ADDR, OFFSET)
// 4*8); // (ESP32_SPI_GET(s, ADDR, LENGTH) + 3) & 0x3c
s->reg[ESP32_SPI_FLASH_STATUS] |= ESP32_SPI_FLASH_STATUS_WRENABLE;
}
if (val & ESP32_SPI_FLASH_CMD_USR) {
DEBUG_LOG("%s: TX %04x[%d bits]\n",
__func__,
ESP32_SPI_GET(s, USER2, COMMAND_VALUE),
ESP32_SPI_GET(s, USER2, COMMAND_BITLEN));
int numBits=ESP32_SPI_GET(s, USER2, COMMAND_BITLEN);
int command=ESP32_SPI_GET(s, USER2, COMMAND_VALUE) & (( 1 << numBits) -1);
DEBUG_LOG("command %04x\n",command);
if (command==0x35) {
DEBUG_LOG("CMD 0x35 (RDSR2) read status register\n");
}
if (command==0x05) {
DEBUG_LOG("CMD 0x05 (RDSR) Read status register.\n");
if (s->wren==1) {
s->reg[data_w0]=0x02; // WRITE ENABLED
}
s->wren=1;
}
if (command==0x03 || command==0x3b || ESP32_SPI_GET(s, USER2, COMMAND_VALUE)==0xbb) {
DEBUG_LOG("SPI_READ 0x03. %08X\n",ESP32_SPI_GET(s, ADDR, OFFSET));
// TODO, ignore bit 0-7 !!!
s->wren=0;
// ESP32_SPI_GET(s, ADDR, OFFSET)
unsigned int silly= s->reg[ESP32_SPI_FLASH_ADDR] >> 8;
DEBUG_LOG("Silly %08X\n",silly);
/*
unsigned int *data1=(unsigned int *)s->flash_image +silly;
int q=0;
for(q=0;q<16;q++)
{
printf( "%08X", *data1);
data1++;
if (q%8==7) {
printf("\n");
}
}
*/
DEBUG_LOG("Enter crash %d %08X\n",(ESP32_SPI_GET(s, ADDR, LENGTH) + 3) & 0x3c,s->flash_image);
if ( s->flash_image && (silly + 4*16 < 4*1024*1024)) {
//uint32_t *dest_ptr=&(s->reg[data_w0]);
uint32_t *src_ptr=(uint32_t *)(s->flash_image + silly);
DEBUG_LOG("c0\n");
s->reg[data_w0]=*src_ptr++;
DEBUG_LOG("c1\n");
s->reg[data_w1]=*src_ptr++;
s->reg[data_w2]=*src_ptr++;
s->reg[data_w3]=*src_ptr++;
s->reg[data_w4]=*src_ptr++;
s->reg[data_w5]=*src_ptr++;
s->reg[data_w6]=*src_ptr++;
s->reg[data_w7]=*src_ptr++;
s->reg[data_w8]=*src_ptr++;
s->reg[data_w9]=*src_ptr++;
s->reg[data_w10]=*src_ptr++;
s->reg[data_w11]=*src_ptr++;
s->reg[data_w12]=*src_ptr++;
s->reg[data_w13]=*src_ptr++;
s->reg[data_w14]=*src_ptr++;
s->reg[data_w15]=*src_ptr++;
//DEBUG_LOG("here goes nothing\n");
memcpy(&(s->reg[data_w0]),
s->flash_image + silly, // ESP32_SPI_GET(s, ADDR, OFFSET)
4*16); // (ESP32_SPI_GET(s, ADDR, LENGTH) + 3) & 0x3c
}
//DEBUG_LOG("Leave crash\n");
/*
unsigned int *data=(unsigned int *)&s->reg[data_w0];
int j=0;
for(j=0;j<16;j++)
{
fprintf(stderr, "%08X", *data);
data++;
if (j%8==7) {
fprintf(stderr,"\n");
}
}
*/
}
if (command==0x02) {
DEBUG_LOG("SPI_WRITE 0x02. %08X\n",ESP32_SPI_GET(s, ADDR, OFFSET));
//memcpy(s->flash_image + silly,
// &s->reg[data_w0], // ESP32_SPI_GET(s, ADDR, OFFSET)
// 4*16); // (ESP32_SPI_GET(s, ADDR, LENGTH) + 3) & 0x3c
}
/*
if (command==0x02) {
DEBUG_LOG("SPI_WRITE 0x02. %08X\n",ESP32_SPI_GET(s, ADDR, OFFSET));
// TODO, ignore bit 0-7 !!!
unsigned int silly=ESP32_SPI_GET(s, ADDR, OFFSET) >> 8;
DEBUG_LOG("Silly %08X\n",silly);
memcpy(s->flash_image + silly,
&s->reg[data_w0], // ESP32_SPI_GET(s, ADDR, OFFSET)
4*16); // (ESP32_SPI_GET(s, ADDR, LENGTH) + 3) & 0x3c
}
*/
}
}
static void esp32_spi_write_address(Esp32SpiState *s, hwaddr addr,
uint64_t val, unsigned size)
{
s->reg[ESP32_SPI_FLASH_ADDR] = val;
DEBUG_LOG("val? %04X " ,val);
if ((val & 0xff000000) > 0 ) {
DEBUG_LOG("WRITE_____________________? %04X " ,val);
s->write_mode=1;
s->length=(val & 0xff000000) >> 24;
} else {
s->write_mode=0;
}
DEBUG_LOG("Address %s: TX %08x[%d reserved]\n",
__func__,
ESP32_SPI_GET(s, ADDR, ADDR_VALUE),
ESP32_SPI_GET(s, ADDR, ADDR_RESERVED));
}
static void esp32_spi_write_user2(Esp32SpiState *s, hwaddr addr,
uint64_t val, unsigned size)
{
s->reg[ESP32_SPI_FLASH_USER2] = val;
//DEBUG_LOG("equal? %04X , %04X" , SPI_EXT2_REG*4,0xf8);
int numBits=ESP32_SPI_GET(s, USER2, COMMAND_BITLEN);
int command=ESP32_SPI_GET(s, USER2, COMMAND_VALUE) & (( 1 << numBits) -1);
DEBUG_LOG("USER2 command %04x\n",command);
int length=s->reg[ESP32_MOSI_DLEN];
length= length & 0x3c;
DEBUG_LOG("USER2 length %04x\n",length);
unsigned int write_addr=s->reg[ESP32_SPI_FLASH_ADDR] & 0x0fffffff;
//(void)write_addr;
//write_addr=write_addr >> 8;
DEBUG_LOG("USER2 address %"PRIx64 "\n",write_addr);
//if (command==0x05) {
// s->wren=1;
//}
#if 0
if (command==0x05) {