-
Notifications
You must be signed in to change notification settings - Fork 7
/
raspberrypi.nothing
1570 lines (1354 loc) · 58.7 KB
/
raspberrypi.nothing
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
;; -*- mode: scheme -*-
;; Copyright (C) 2012-2015 Tony Garnock-Jones <[email protected]>
;;
;; This file is part of pi-nothing.
;;
;; pi-nothing is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published
;; by the Free Software Foundation, either version 3 of the License,
;; or (at your option) any later version.
;;
;; pi-nothing 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.
;;
;; You should have received a copy of the GNU General Public License
;; along with pi-nothing. If not, see <http://www.gnu.org/licenses/>.
;;
;;---------------------------------------------------------------------------
;; NOTE: The procedure (font0-bitmap), containing font data, is NOT
;; covered by the GPL, and is instead covered by the Bitstream Vera
;; Fonts Copyright license, which is available in the file
;; font0license.txt.
;;---------------------------------------------------------------------------
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Interrupt table
;; TODO: Limitation on the linker means this routine has to be early
;; in the image, so that the offset to
;; sys:exception-handler-undefined-instruction etc isn't too large to
;; fit in an instruction. See the label-linker instance in
;; `indirect-immediate` in mach-arm7.rkt. A more flexible linker would
;; be able to put the needed value in the data quasisegment, like the
;; non-label branch does.
(define (configure-interrupt-table)
(let ((mutable vec 0))
(while (<u vec 8)
(! (<< vec 2) #xe59ff018) ;; LDR PC, [PC, #24]
(set! vec (+ vec 1))))
(! #x20 interrupt:reset)
(! #x24 sys:exception-handler-undefined-instruction)
(! sys:interrupt-vector-undefined-instruction interrupt:undefined-instruction)
(! #x28 sys:exception-handler-swi)
(! sys:interrupt-vector-swi interrupt:swi)
(! #x2c sys:exception-handler-prefetch-abort)
(! sys:interrupt-vector-prefetch-abort interrupt:prefetch-abort)
(! #x30 sys:exception-handler-data-abort)
(! sys:interrupt-vector-data-abort interrupt:data-abort)
(! #x34 interrupt:unused)
(! #x38 sys:exception-handler-irq)
(! sys:interrupt-vector-irq interrupt:irq)
(! #x3c sys:exception-handler-fiq)
(! sys:interrupt-vector-fiq interrupt:fiq))
(define (cpu-enable-interrupts)
(sys:set-cpsr (binand (sys:get-cpsr) #xffffff3f))) ;; clear I and F bits
(define (cpu-disable-interrupts)
(sys:set-cpsr (binor (sys:get-cpsr) #xc0))) ;; set I and F bits
(define (interrupt:reset)
(error %error-unexpected-interrupt:reset)
0)
(define (interrupt:undefined-instruction)
(error %error-unexpected-interrupt:undefined-instruction)
0) ;; either 0 or 4 depending on whether to skip the insn or retry it
(define (interrupt:swi)
(error %error-unexpected-interrupt:swi)
0) ;; will return immediately after the SWI
(define (interrupt:prefetch-abort)
(error %error-unexpected-interrupt:prefetch-abort)
0) ;; either 0 or 4 depending on whether to skip the insn or retry it
(define (interrupt:data-abort)
(error %error-unexpected-interrupt:data-abort)
0) ;; either 4 or 8 depending on whether to skip the insn or retry it
(define (interrupt:unused)
(error %error-unexpected-interrupt:unused)
0)
(define (irq-start-cycle-counter)
(data #"\0\0\0\0"))
(define (interrupt:irq dummy cycle-counter)
(! (irq-start-cycle-counter) cycle-counter)
(sys:set-spsr (binor (sys:get-spsr) #xc0)) ;; disable interrupts when we return from this handler.
;; ^ we do this because we don't know which interrupt caused us
;; here, so we can't clear the condition and will loop forever.
;; We leave the clearing of the condition to user code.
4) ;; not zero! otherwise we end up skipping an instruction
(define (interrupt:fiq)
(error %error-unexpected-interrupt:fiq)
4) ;; not zero! otherwise we end up skipping an instruction
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(const %error-division-by-zero #x00000002)
(const %error-no-error #x00000007)
(const %error-bad-mailbox-channel #x00000010)
(const %error-bad-mailbox-pointer #x00000011)
(const %error-unexpected-mailbox-message #x00000012)
(const %error-framebuffer-mode-set-failed #x00000020)
(const %error-unexpected-interrupt:reset #x00000030)
(const %error-unexpected-interrupt:undefined-instruction #x00000031)
(const %error-unexpected-interrupt:swi #x00000032)
(const %error-unexpected-interrupt:prefetch-abort #x00000033)
(const %error-unexpected-interrupt:data-abort #x00000034)
(const %error-unexpected-interrupt:unused #x00000035)
(const %error-unexpected-interrupt:irq #x00000036)
(const %error-unexpected-interrupt:fiq #x00000037)
(const %error-devicetree-bad-node-type #x00000040)
(const %error-dma-no-free-channels #x00000050)
(const %error-dma-channel-double-free #x00000051)
(define (error code)
(pulse-bits-forever code))
(define (error/argument code arg)
(while (= 0 0)
(pulse-bits code)
(pulse-bits arg)
(long-delay)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define (division-by-zero a b)
(error %error-division-by-zero))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define (delay-countdown v)
(let ((mutable n v))
(while (>u n 0)
(set! n (- n 1)))))
(define (short-delay)
(delay-countdown #x2000000))
(define (long-delay)
(delay-countdown #x6000000))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(const %gpio-base #x20200000)
(const %gpio-function-select-base #x0000) ;; GPIO pins 0-9; subsequent pins at adjacent regs
(const %gpio-pin-output-set-base #x001c) ;; GPIO pins 0-31; 32-53 in adjacent reg
(const %gpio-pin-output-clear-base #x0028) ;; GPIO pins 0-31; 32-53 in adjacent reg
(const %gpio-pull-up/down #x0094)
(const %gpio-pull-up/down-clock-base #x0098) ;; GPIO pins 0-31; 32-53 in adjacent reg
(const %gpio-pin-control-mask 7) ;; 3 bits
(const %gpio-pin-control-input 0) ;; pin is an input
(const %gpio-pin-control-output 1) ;; pin is an output
(const %gpio-pin-control-alternate-function-base 4) ;; add alt fun num to get funs 0-5
(const %gpio-pull-neither 0)
(const %gpio-pull-down 1)
(const %gpio-pull-up 2)
(define (gpio-function-select! pin function)
(let ((reg (+ %gpio-base %gpio-function-select-base (/ pin 10)))
(ofs (* (% pin 10) 3)))
(let ((old (? reg)))
(! reg (binor (binand old (binnot (<< %gpio-pin-control-mask ofs)))
(<< (binand function %gpio-pin-control-mask) ofs))))))
(define (gpio-set-pin! pin state)
(! (+ %gpio-base
(if (<> state 0) %gpio-pin-output-set-base %gpio-pin-output-clear-base)
(>>u pin 5)) ;; 32 pins per reg
(<< 1 (binand pin 31))))
;; pins0 and pins1 are bitmasks with a bit set for each pin to modify
;; e.g. pins0 should have (1 << 0) set to modify setting for pin 0
;; pins0 covers pins 0-31, and pins1 covers pins 32-53
;; value 0 = disable both pull-up and pull-down
;; value 1 = enable pull down
;; value 2 = enable pull up
(define (gpio-pull-control! pins0 pins1 value)
(! (+ %gpio-base %gpio-pull-up/down) value)
(delay-cycles 150)
(when (<> pins0 0) (! (+ %gpio-base %gpio-pull-up/down-clock-base 0) pins0))
(when (<> pins1 0) (! (+ %gpio-base %gpio-pull-up/down-clock-base 4) pins1))
(delay-cycles 150)
(! (+ %gpio-base %gpio-pull-up/down) 0)
(when (<> pins0 0) (! (+ %gpio-base %gpio-pull-up/down-clock-base 0) 0))
(when (<> pins1 0) (! (+ %gpio-base %gpio-pull-up/down-clock-base 4) 0)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define (enable-led)
(gpio-function-select! 16 %gpio-pin-control-output))
(define (set-led! state)
(gpio-set-pin! 16 (if (<> state 0) 0 1)))
(define (pulse-bits bitfield)
(if (<> bitfield 0)
(let ((bit (binand bitfield 1)))
(pulse-bits (>>u bitfield 1))
(set-led! 1)
(if (<> bit 0) (long-delay) (short-delay))
(set-led! 0)
(short-delay))
(begin (set-led! 0)
(long-delay))))
(define (pulse-bits-forever bitfield)
(while (= 0 0) (pulse-bits bitfield)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(const $alloc-pointer-addr #x00200000) ;; Heap starts at 2MB - i.e. above the stack
(define (init-alloc!)
(! $alloc-pointer-addr (+ $alloc-pointer-addr 16))) ;; can't use 4, is not 16-aligned
(define (*save-alloc-state*)
(? $alloc-pointer-addr))
(define (*restore-alloc-state* saved-state)
(! $alloc-pointer-addr saved-state))
(define (round-up-to-4 x) (binand #xfffffffc (+ x 3)))
(define (round-up-to-16 x) (binand #xfffffff0 (+ x 15)))
(define (alloc-align! bits)
(let ((mask (- (<< 1 bits) 1)))
(let ((aligned-pointer (binand (binnot mask) (+ (? $alloc-pointer-addr) mask))))
(! $alloc-pointer-addr aligned-pointer))))
(define (alloc! size)
;; always returns a 16-byte aligned pointer
(let ((rounded-size (round-up-to-16 size))
(result (? $alloc-pointer-addr)))
(! $alloc-pointer-addr (+ result rounded-size))
result))
(define (copy-bytes! target source count)
(when (>u count 0)
(!byte target (?byte source))
(copy-bytes! (+ target 1) (+ source 1) (- count 1))))
(define (maxu a b)
(if (>u a b) a b))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(const %mailbox-base #x2000b880)
(const %mailbox-reg-read #x00)
(const %mailbox-reg-peek #x10)
(const %mailbox-reg-sender #x14)
(const %mailbox-reg-status #x18)
(const %mailbox-reg-config #x1c)
(const %mailbox-reg-write #x20)
(const %channel-power-management 0)
(const %channel-framebuffer 1)
(const %channel-virtual-uart 2)
(const %channel-vchiq 3)
(const %channel-leds 4)
(const %channel-buttons 5)
(const %channel-touchscreen 6)
;; 7 unused?
(const %channel-properties-arm->vc 8)
(const %channel-properties-vc->arm 9)
(define (mailbox-barrier)
;; TODO: what kinds of memory barriers are needed here and in
;; mailbox-recv? See http://elinux.org/RPi_Framebuffer.
(sys:clean-and-invalidate-data-cache)
0)
(define (mailbox-status)
(mailbox-barrier)
(? (+ %mailbox-base %mailbox-reg-status)))
(define (mailbox-send! mailbox-channel message-pointer)
(when (>=u mailbox-channel 16) (error %error-bad-mailbox-channel))
(when (<> (binand message-pointer 15) 0) (error %error-bad-mailbox-pointer))
(while (<> (binand (mailbox-status) (<< 1 31)) 0))
(! (+ %mailbox-base %mailbox-reg-write)
(binor mailbox-channel message-pointer))
(mailbox-barrier))
(define (mailbox-recv mailbox-channel)
(when (>=u mailbox-channel 16) (error %error-bad-mailbox-channel))
(while (<> (binand (mailbox-status) (<< 1 30)) 0))
(mailbox-barrier)
(let ((pointer-and-channel (? (+ %mailbox-base %mailbox-reg-read))))
(when (<> (binand pointer-and-channel 15) mailbox-channel)
(error %error-unexpected-mailbox-message))
(binand pointer-and-channel #xfffffff0)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Mailbox property interface
;; https://github.com/raspberrypi/firmware/wiki/Mailbox-property-interface
;; Property buffer must be a multiple of 32 bits, i.e. a whole number of words.
;; Each tag needs to have enough internal sub-buffer space for its reply.
(struct property-buffer-header ((size word) ;; in bytes; includes this header
(opcode word)
;; Sequence of tags/values follows,
;; terminated by %property-tag-end and
;; padding to 4-byte alignment.
))
(const %property-buffer-op-request #x00000000)
(const %property-buffer-op-response-ok #x80000000)
(const %property-buffer-op-response-error #x80000001) ;; "error parsing request buffer"
(struct property-buffer-tag ((id word)
(buffer-size word) ;; in bytes; EXCLUDES this header
(value-length word) ;; MSB 0=req, 1=resp; 31 bits value length in bytes
;; Value buffer follows, buffer-size bytes.
))
(const %property-tag-end #x00000000)
(const %property-tag-get-arm-memory #x00010005)
(const %property-tag-get-vc-memory #x00010006)
(const %property-tag-get-clock-rate #x00030002)
(const %property-tag-get-physical-display-size #x00040003)
(const %property-tag-get-dma-channels #x00060001)
(define (mailbox-property-get tag request-buffer request-buffer-size response-buffer-size)
(let ((*saved* (*save-alloc-state*))
(request/response-buffer-size (maxu request-buffer-size response-buffer-size)))
(let ((buflen (round-up-to-4 (+ sizeof-property-buffer-header
sizeof-property-buffer-tag
request/response-buffer-size
4)))) ;; for end tag
(let ((buf (alloc! buflen)))
(let ((tag-start (+ buf sizeof-property-buffer-header))
(value-start (+ buf sizeof-property-buffer-header sizeof-property-buffer-tag)))
(! (+ buf property-buffer-header-size) buflen)
(! (+ buf property-buffer-header-opcode) %property-buffer-op-request)
(! (+ tag-start property-buffer-tag-id) tag)
(! (+ tag-start property-buffer-tag-buffer-size) request/response-buffer-size)
(! (+ tag-start property-buffer-tag-value-length) 0)
(copy-bytes! value-start request-buffer request-buffer-size)
(! (+ value-start request/response-buffer-size) %property-tag-end)
(mailbox-send! %channel-properties-arm->vc buf)
(let ((reply (mailbox-recv %channel-properties-arm->vc)))
;; The reply seems to be the same as the buffer pointer we gave.
(when (<> reply buf)
(puts (data #"(Weird, mailbox-property-get reply \0"))
(putx reply)
(puts (data #" isn't buf \0"))
(putx buf)
(puts (data #")\n\0"))))
(*restore-alloc-state* *saved*)
value-start))))) ;; N.B. only valid until the next `alloc!`!
(struct property-memory-info ((base word)
(size word))) ;; in bytes
(define (get-*-memory tag)
(? (+ (mailbox-property-get tag 0 0 sizeof-property-memory-info)
property-memory-info-size)))
(define (get-arm-memory) (get-*-memory %property-tag-get-arm-memory))
(define (get-vc-memory) (get-*-memory %property-tag-get-vc-memory))
(struct property-display-dimensions ((width word)
(height word)))
(define (get-physical-display-size)
(mailbox-property-get %property-tag-get-physical-display-size 0 0 sizeof-property-display-dimensions))
(define (get-dma-channel-availability-mask)
(binand (? (mailbox-property-get %property-tag-get-dma-channels 0 0 4))
#x0000ffff))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Clocks
(const %clock-RESERVED 0)
(const %clock-emmc 1)
(const %clock-uart 2)
(const %clock-arm 3)
(const %clock-core 4)
(const %clock-v3d 5)
(const %clock-h264 6)
(const %clock-isp 7)
(const %clock-sdram 8)
(const %clock-pixel 9)
(const %clock-pwm 10)
(define (get-clock-rate clock-id)
(let ((*saved* (*save-alloc-state*)))
(let ((clock-id-buffer (alloc! 4)))
(! clock-id-buffer clock-id)
(let ((reply-buffer (mailbox-property-get %property-tag-get-clock-rate clock-id-buffer 4 8)))
(*restore-alloc-state* *saved*)
(? (+ reply-buffer 4))))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(struct framebuffer-description ((width word)
(height word)
(virtual-width word)
(virtual-height word)
(pitch word) ;; set by GPU
(depth word) ;; bits per pixel
(x-offset word)
(y-offset word)
(pointer word) ;; set by GPU
(size word))) ;; set by GPU
(define (framebuffer-pointer fbinfo) (? (+ fbinfo framebuffer-description-pointer)))
(define (framebuffer-stride fbinfo) (? (+ fbinfo framebuffer-description-pitch)))
(define (framebuffer-pixel-width fbinfo) (>>u (? (+ fbinfo framebuffer-description-depth)) 3))
(define (pointer->gpu-pointer v)
(binor #x40000000 v))
(define (gpu-pointer->pointer v)
(binand #x3fffffff v))
(define (initialize-framebuffer)
(let ((dimensions (get-physical-display-size)))
(let ((W (? (+ dimensions property-display-dimensions-width)))
(H (? (+ dimensions property-display-dimensions-height)))
;; ^ Must read from `dimensions` before performing the next `alloc!`.
(fbinfo (alloc! sizeof-framebuffer-description))
;; (W 1920) (H 1080)
;; (W 1024) (H 768)
;; (W 640) (H 480)
;; (W 320) (H 240)
)
(! (+ fbinfo framebuffer-description-width) W)
(! (+ fbinfo framebuffer-description-height) H)
(! (+ fbinfo framebuffer-description-virtual-width) W)
(! (+ fbinfo framebuffer-description-virtual-height) H)
(! (+ fbinfo framebuffer-description-pitch) 0)
(! (+ fbinfo framebuffer-description-depth) 32)
(! (+ fbinfo framebuffer-description-x-offset) 0)
(! (+ fbinfo framebuffer-description-y-offset) 0)
(! (+ fbinfo framebuffer-description-pointer) 0)
(! (+ fbinfo framebuffer-description-size) 0)
(mailbox-send! %channel-framebuffer (pointer->gpu-pointer fbinfo))
(let ((result (mailbox-recv %channel-framebuffer)))
(when (<> result 0) (error/argument %error-framebuffer-mode-set-failed result))
(! (+ fbinfo framebuffer-description-pointer)
(gpu-pointer->pointer (? (+ fbinfo framebuffer-description-pointer))))
fbinfo))))
;; TODO: Use the mailbox property interface to set the cursor sprite?
;; https://github.com/raspberrypi/firmware/wiki/Mailbox-property-interface
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; DMA
;; Registers for talking to the DMA units
(struct dma ((control/status word)
(control-block-address word)
(transfer-information word)
(source-address word)
(destination-address word)
(transfer-len word)
(stride word)
(next-control-block word)
(debug word)))
;; Control blocks, basically a bunch of parameters in RAM. Pointers to
;; control blocks are sent to the DMA hardware via the "dma" struct
;; above.
(struct dma-control-block ((transfer-information word)
(source-address word)
(destination-address word)
(transfer-len word)
(stride word)
(next-control-block word)
(reserved0 word)
(reserved1 word)))
(const dma0 #x20007000) ;; DMA channel 0 registers. Subsequent
;; channels (for DMA0 through DMA14 (not 15))
;; start at intervals of #x100, i.e.
;; #x20007100 for DMA1, etc. DMA15 does exist,
;; but at #x20E05000. (See the BCM2835
;; peripherals PDF.)
(const dma-int-status #x20007fe0) ;; All DMA channels interrupt status
(const dma-enable #x20007ff0) ;; All DMA channels enable register
;; DMA control/status register
(const DMA_ACTIVE #x00000001)
(const DMA_END #x00000002)
(const DMA_INT #x00000004)
(const DMA_DREQ #x00000008)
(const DMA_PAUSED #x00000010)
(const DMA_DREQ_STOPS_DMA #x00000020)
(const DMA_WAITING_FOR_OUTSTANDING_WRITES #x00000040)
(const DMA_ERROR #x00000100)
(const DMA_PRIORITY_MASK #x000F0000) ;; priorities 0-15
(const DMA_PANIC_PRIORITY_MASK #x00F00000) ;; priorities 0-15
(const DMA_WAIT_FOR_OUTSTANDING_WRITES #x10000000)
(const DMA_DISDEBUG #x20000000)
(const DMA_ABORT #x40000000)
(const DMA_RESET #x80000000)
;; DMA transfer information
(const DMA_INTEN #x00000001)
(const DMA_TDMODE #x00000002) ;; 2D mode
;; #x00000004 ?
(const DMA_WAIT_RESP #x00000008)
(const DMA_DEST_INC #x00000010)
(const DMA_DEST_WIDTH #x00000020)
(const DMA_DEST_DREQ #x00000040)
(const DMA_DEST_IGNORE #x000080)
(const DMA_SRC_INC #x0000100)
(const DMA_SRC_WIDTH #x0000200)
(const DMA_SRC_DREQ #x0000400)
(const DMA_SRC_IGNORE #x0000800)
(const DMA_BURST_LENGTH_MASK #x0000F000) ;; burst length 1-16 (!!)
(const DMA_PERMAP_MASK #x001F0000) ;; peripheral mapping; 0=unpaced
(const DMA_WAITS_MASK #x03E00000) ;; extra wait cycles, 0-31
(const DMA_NO_WIDE_BURSTS #x04000000)
;; In 2D mode, split field specifies X and Y dimensions separately
(const DMA_XLENGTH #x0000FFFF)
(const DMA_YLENGTH #x3FFF0000)
(const DMA_YLENGTH_SHIFT 16)
;; In 2D mode, source and destination strides specified separately
(const DMA_S_STRIDE #x0000FFFF)
(const DMA_D_STRIDE #xFFFF0000)
(const DMA_D_STRIDE_SHIFT 16)
;; DMA debug fields
(const DMA_READ_LAST_NOT_SET_ERROR #x00000001)
(const DMA_FIFO_ERROR #x00000002)
(const DMA_READ_ERROR #x00000004)
(const DMA_OUTSTANDING_WRITES #x000000F0)
(const DMA_ID #x0000FF00)
(const DMA_STATE #x01FF0000)
(const DMA_VERSION #x0E000000)
(const DMA_LITE #x10000000)
(struct dma-state ((lite-channels word)
(free-full-channel-mask word)
(free-full-channel-count word)
(free-lite-channel-mask word)
(free-lite-channel-count word)))
(define (initialize-dma)
(let ((state (alloc! sizeof-dma-state))
(initial-availability (get-dma-channel-availability-mask)))
(! (+ state dma-state-lite-channels) 0)
(! (+ state dma-state-free-full-channel-mask) 0)
(! (+ state dma-state-free-full-channel-count) 0)
(! (+ state dma-state-free-lite-channel-mask) 0)
(! (+ state dma-state-free-lite-channel-count) 0)
(let ((mutable n 0))
(while (<u n 14) ;; TODO: handle channel 15 properly
(let ((mask (<< 1 n)))
(when (<> (binand initial-availability mask) 0)
(when (<> (binand (? (+ dma0 (* #x100 n) dma-debug)) DMA_LITE) 0)
(! (+ state dma-state-lite-channels)
(binor (? (+ state dma-state-lite-channels)) mask)))
(free-dma-channel state n)))
(set! n (+ n 1))))
(! dma-enable (binor (? dma-enable) initial-availability)) ;; actually enables the DMA channels
state))
(define (free-dma-channel dma channel)
(let ((mask (<< 1 channel)))
(let ((lite? (if (<> (binand (? (+ dma dma-state-lite-channels)) mask) 0) 1 0)))
(let ((free-mask-offset (if (<> lite? 0)
dma-state-free-lite-channel-mask
dma-state-free-full-channel-mask))
(free-count-offset (if (<> lite? 0)
dma-state-free-lite-channel-count
dma-state-free-full-channel-count)))
(let ((free (? (+ dma free-mask-offset))))
(when (<> (binand free mask) 0) (error %error-dma-channel-double-free))
(! (+ dma free-mask-offset) (binor free mask))
(! (+ dma free-count-offset) (+ (? (+ dma free-count-offset)) 1)))))))
(define (free-dma-channels dma channels count)
(when (>u count 0)
(free-dma-channel dma (? channels))
(free-dma-channels dma (+ channels 4) (- count 1))))
(define (allocate-dma-channel dma lite?)
(if (<> lite? 0)
(allocate-dma-channel1 dma
dma-state-free-lite-channel-mask
dma-state-free-lite-channel-count)
(allocate-dma-channel1 dma
dma-state-free-full-channel-mask
dma-state-free-full-channel-count)))
(define (allocate-dma-channel1 dma free-mask-offset free-count-offset)
(let ((old-count (? (+ dma free-count-offset))))
(when (= old-count 0) (error %error-dma-no-free-channels))
(! (+ dma free-count-offset) (- old-count 1)))
(allocate-dma-channel2 dma free-mask-offset (? (+ dma free-mask-offset)) 0))
(define (allocate-dma-channel2 dma free-mask-offset free n)
;; Caller has already ensured that *some* channel is free, hence no termination check here
(let ((mask (<< 1 n)))
(if (= (binand free mask) 0)
(allocate-dma-channel2 dma free-mask-offset free (+ n 1))
(begin
(! (+ dma free-mask-offset) (binand free (binnot mask)))
n))))
(define (allocate-dma-channels dma lite? channels count)
(when (>u count 0)
(! channels (allocate-dma-channel dma lite?))
(allocate-dma-channels dma lite? (+ channels 4) (- count 1))))
(define (start-dma channel control-block)
;; TODO: handle channel 15
(let ((base (+ dma0 (* #x100 channel))))
(sys:clean-data-cache)
(sys:data-sync-barrier)
(! (+ base dma-control-block-address) (pointer->gpu-pointer control-block))
(! (+ base dma-control/status) DMA_ACTIVE)))
(define (wait-for-dma-completion channel)
;; TODO: handle channel 15
(let ((base (+ dma0 (* #x100 channel))))
(sys:clean-data-cache)
(sys:data-sync-barrier)
(while (<> (binand (? (+ base dma-control/status)) DMA_ACTIVE) 0))
(! (+ base dma-control/status) (binor DMA_END DMA_INT)))) ;; clear flags
(define (fill-control-block control-block
mode
source
target
width ;; bytes
height
source-width ;; bytes
target-width) ;; bytes
(! (+ control-block dma-control-block-transfer-information) mode)
(! (+ control-block dma-control-block-source-address) (pointer->gpu-pointer source))
(! (+ control-block dma-control-block-destination-address) (pointer->gpu-pointer target))
(! (+ control-block dma-control-block-transfer-len)
(binor (binand (<< (- height 1) DMA_YLENGTH_SHIFT) DMA_YLENGTH)
(binand width DMA_XLENGTH)))
(! (+ control-block dma-control-block-stride)
(binor (binand (<< (- target-width width) DMA_D_STRIDE_SHIFT) DMA_D_STRIDE)
(binand (- source-width width) DMA_S_STRIDE)))
(! (+ control-block dma-control-block-next-control-block) 0)
(! (+ control-block dma-control-block-reserved0) 0)
(! (+ control-block dma-control-block-reserved1) 0))
(define (synchronous-2d-transfer dma
fill?
source
target
width ;; bytes
height
source-width ;; bytes
target-width) ;; bytes
(let ((*saved* (*save-alloc-state*)))
(alloc-align! 5) ;; 5-bit alignment = 32-byte boundary
(let ((slices (if (<u width 16)
1 ;; don't bother slicing narrow transfers
(? (+ dma dma-state-free-full-channel-count)))))
(let ((cbs (alloc! (* 8 4 slices)))
;; ^ make sure to allocate the control blocks first, since
;; they need the 32-byte alignment.
(channels (alloc! (* 4 slices)))
(slicewidth (<< (/ (>>u width 2) slices) 2))) ;; slices should be whole pixels/words wide
(allocate-dma-channels dma 0 channels slices)
;; (putxs channels slices)
;; (newline)
(let ((mutable n 0))
(while (<u n (- slices 1))
(fill-control-block (+ cbs (* 8 4 n))
(binor DMA_TDMODE
DMA_DEST_INC
DMA_DEST_WIDTH
(if (<> fill? 0) 0 DMA_SRC_INC)
DMA_SRC_WIDTH)
(if (<> fill? 0)
source
(+ source (* n slicewidth)))
(+ target (* n slicewidth))
slicewidth
height
(if (<> fill? 0) slicewidth source-width)
target-width)
(start-dma (? (+ channels (<< n 2))) (+ cbs (* 8 4 n)))
(set! n (+ n 1))))
(let ((n (- slices 1)))
(fill-control-block (+ cbs (* 8 4 n))
(binor DMA_TDMODE
DMA_DEST_INC
DMA_DEST_WIDTH
(if (<> fill? 0) 0 DMA_SRC_INC)
DMA_SRC_WIDTH)
(if (<> fill? 0)
source
(+ source (* n slicewidth)))
(+ target (* n slicewidth))
(- width (* n slicewidth))
height
(if (<> fill? 0) slicewidth source-width)
target-width)
(start-dma (? (+ channels (<< n 2))) (+ cbs (* 8 4 n))))
(let ((mutable n 0))
(while (<u n slices)
(wait-for-dma-completion (? (+ channels (<< n 2))))
(set! n (+ n 1))))
(free-dma-channels dma channels slices)))
(*restore-alloc-state* *saved*)))
(define (synchronous-2d-move dma
source
target
width ;; bytes
height
source-width ;; bytes
target-width) ;; bytes
(synchronous-2d-transfer dma
0
source
target
width
height
source-width
target-width))
(define (synchronous-2d-fill dma
value target width height target-stride)
(let ((*saved* (*save-alloc-state*)))
(let ((source-block (alloc! 16)))
(! source-block value)
(! (+ source-block 4) value)
(! (+ source-block 8) value)
(! (+ source-block 12) value)
(synchronous-2d-transfer dma
1
source-block
target
width
height
-1 ;; not used
target-stride))
(*restore-alloc-state* *saved*)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(struct console ((fbinfo word)
(dma word)
(x word)
(y word)
(width word)
(height word)))
(define (make-console fbinfo dma)
(let ((c (alloc! sizeof-console)))
(! (+ c console-fbinfo) fbinfo)
(! (+ c console-dma) dma)
(! (+ c console-x) 0)
(! (+ c console-y) 0)
(! (+ c console-width) (>>u (? (+ fbinfo framebuffer-description-width)) %char-width-shift))
(! (+ c console-height) (>>u (? (+ fbinfo framebuffer-description-height)) %char-height-shift))
c))
(define (console-putc con ch)
(putc ch)
(when (= ch 13)
(! (+ con console-x) 0)
(return 0))
(when (= ch 10)
(putc 13)
(! (+ con console-x) 0)
(! (+ con console-y) (+ (? (+ con console-y)) 1))
(scroll-if-required con)
(return 0))
(blit-char (? (+ con console-fbinfo))
(<< (? (+ con console-x)) %char-width-shift)
(<< (? (+ con console-y)) %char-height-shift)
ch
#x00ffffff
#x000000ff)
(! (+ con console-x) (+ (? (+ con console-x)) 1))
(when (= (? (+ con console-x)) (? (+ con console-width)))
(console-putc con 10)))
(define (scroll-if-required con)
(let ((height (? (+ con console-height))))
(when (= (? (+ con console-y)) height)
(let ((fbinfo (? (+ con console-fbinfo))))
(let ((target (framebuffer-pointer fbinfo))
(offset (* (framebuffer-stride fbinfo) (<< 1 %char-height-shift)))
(limit (* (framebuffer-stride fbinfo) (<< 1 %char-height-shift) (- height 1))))
(synchronous-2d-move (? (+ con console-dma))
(+ target offset)
target
(framebuffer-stride fbinfo)
(* (- height 1) (<< 1 %char-height-shift))
(framebuffer-stride fbinfo)
(framebuffer-stride fbinfo))
(synchronous-2d-fill (? (+ con console-dma))
0
(+ target limit)
(framebuffer-stride fbinfo)
(<< 1 %char-height-shift)
(framebuffer-stride fbinfo))
))
(! (+ con console-y) (- height 1)))))
(define (console-puts con p)
(let ((ch (?byte p)))
(when (<> ch 0)
(console-putc con ch)
(console-puts con (+ p 1)))))
(define (console-newline con)
(console-putc con 10))
(define (console-putn* con i)
(if (<> i 0)
(begin (console-putn* con (/ i 10))
(console-putc con (+ (% i 10) 48)))
0))
(define (console-putn con i)
(if (<> i 0)
(console-putn* con i)
(console-putc con 48)))
(define (console-putx con n)
(let ((mutable shift 28))
(while (>=s shift 0)
(let ((digit (binand 15 (>>u n shift))))
(console-putc con (+ digit (if (>=u digit 10)
55 ;; 65 - 10
48)))
(set! shift (- shift 4))))))
(define (console-putxdigit con n)
(let ((digit (binand 15 n)))
(console-putc con (+ digit (if (>=u digit 10) 55 48)))))
(define (console-putx-block con bs len)
(let ((mutable n 0))
(while (<u n len)
(let ((b (?byte (+ bs n))))
(console-putxdigit con (>>u b 4))
(console-putxdigit con b))
(set! n (+ n 1)))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(const ATAG_NONE #x00000000)
(const ATAG_CORE #x54410001)
(const ATAG_MEM #x54410002)
(const ATAG_VIDEOTEXT #x54410003)
(const ATAG_RAMDISK #x54410004)
(const ATAG_INITRD2 #x54420005) ;; yes, really, not #x54410005. Presumably that was INITRD1?
(const ATAG_SERIAL #x54410006)
(const ATAG_REVISION #x54410007)
(const ATAG_VIDEOLFB #x54410008)
(const ATAG_CMDLINE #x54410009)
(define (dump-atag-word con base field-number description-str)
(console-putc con 32)
(console-putc con 32)
(console-putx con (? (+ base (<< (+ field-number 2) 2))))
(console-putc con 32)
(console-puts con description-str)
(console-newline con))
(define (dump-atag-core con base)
(dump-atag-word con base 0 (data #"flags\0"))
(dump-atag-word con base 1 (data #"pagesize\0"))
(dump-atag-word con base 2 (data #"rootdev\0")))
(define (dump-atag-mem con base)
(dump-atag-word con base 0 (data #"size\0"))
(dump-atag-word con base 1 (data #"start\0")))
(define (dump-atag-cmdline con base)
(console-putc con 32)
(console-putc con 32)
(console-puts con (+ base 8))
(console-newline con))
(define (dump-atags con base)
(let ((size (? base))
(tag (? (+ base 4))))
(console-puts con (data #"ATAG #x\0"))
(console-putx con tag)
(console-puts con (data #" size \0"))
(console-putn con size)
(console-newline con)
(when (= tag ATAG_CORE) (dump-atag-core con base))
(when (= tag ATAG_MEM) (dump-atag-mem con base))
(when (= tag ATAG_CMDLINE) (dump-atag-cmdline con base))
(when (<> tag ATAG_NONE)
(dump-atags con (+ base (<< size 2))))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; While the RPi has an SP804, it's not quite AMBA-compatible, and the
;; BCM2835 peripheral documentation recommends to use the "System
;; Timer" instead. That's what this is.
;; - free-running counter runs at 1 MHz
;; - memory barriers as usual required whenever we're switching between peripherals
;; - apparently sys:data-mem-barrier ?
;; - compare0 and compare2 are used by the GPU.
;; - compare1 and compare3 are free for use by the CPU.
(struct system-timer ((control/status word)
(counter-low word)
(counter-high word)
(compare0 word)
(compare1 word)
(compare2 word)
(compare3 word)))
(const system-timer-base #x20003000)
(define (current-timer-value)
(sys:data-mem-barrier)
(? (+ system-timer-base system-timer-counter-low)))
;; Clear previous interrupt, if any
(define (clear-fired-alarm)
(sys:data-mem-barrier)
(! (+ system-timer-base system-timer-control/status) (<< 1 3))) ;; use timer ch 3
;; TODO: Use "Clocks" mailbox property interface to read the base
;; clock value dynamically. We're assuming it's 1 MHz here.
(define (set-alarm relative-microseconds)
(set-alarm-absolute (+ (current-timer-value) relative-microseconds)))
(define (set-alarm-absolute absolute-microseconds)
(clear-fired-alarm)
(! (+ system-timer-base system-timer-compare3) absolute-microseconds)
absolute-microseconds)
(define (check-alarm)
(if (<> (binand (? (+ system-timer-base system-timer-control/status)) (<< 1 3)) 0)
(begin (clear-fired-alarm) 1)
0))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Likewise, the RPi seems to have an unusual interrupt controller.
(struct interrupt-controller ((basic-pending word)
(pending1 word)
(pending2 word)
(fiq-control word)
(enable1 word)
(enable2 word)
(basic-enable word)
(disable1 word)
(disable2 word)
(basic-disable word)))
;; The BCM2835 peripheral docs give only some of these values; the
;; Xinu people at http://xinu.mscs.mu.edu/BCM2835_Interrupt_Controller
;; fill in some of the blanks.
(const IRQ_SYSTEM_TIMER_COMPARE0 0) ;; GPU ONLY
(const IRQ_SYSTEM_TIMER_COMPARE1 1) ;; OK for CPU
(const IRQ_SYSTEM_TIMER_COMPARE2 2) ;; GPU ONLY
(const IRQ_SYSTEM_TIMER_COMPARE3 3) ;; OK for CPU
(const IRQ_USB 9)
(const IRQ_PCM_AUDIO 55)
(const IRQ_UART 57)
(const IRQ_SDHC 62)
(const interrupt-controller-base #x2000b200)
(define (configure-interrupt-controller)
(! (+ interrupt-controller-base interrupt-controller-enable1)
(binor (<< 1 IRQ_SYSTEM_TIMER_COMPARE3)))
(! (+ interrupt-controller-base interrupt-controller-enable2)
(binor (<< 1 (- IRQ_UART 32)))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(struct boot-param-header ((magic word)
(totalsize word)
(off_dt_struct word)
(off_dt_strings word)
(off_mem_rsvmap word)