interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / aienux / disk / disk_sdhci.om

926 lines · 19818 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note aienux/disk/disk_sdhci.om: Pure-Omega Freestanding Polled PIO SDHCI Driver + Software Controller Model.
3note Converted from upstream aienux native/disk/disk_sdhci.{h,c} and native/disk/qemu/{payload.c,sdhci_payload.c,start.S}.
4note Compose with: si/directives/om2.om aienux/disk/disk.om aienux/disk/disk_file.om aienux/disk/disk_sdhci.om <test.om>
5
6note SDHCI error codes matching disk_sdhci.h
7const SDHCI_OK int 0
8const SDHCI_EARG int -20
9const SDHCI_EVER int -21
10const SDHCI_ERESET int -22
11const SDHCI_ENOCARD int -23
12const SDHCI_EVOLT int -24
13const SDHCI_ECLOCK int -25
14const SDHCI_ECARD int -26
15const SDHCI_ETIMEOUT int -27
16const SDHCI_ECSD int -28
17const SDHCI_ECMD int -29
18const SDHCI_EDATA int -30
19const SDHCI_ESTATUS int -31
20const SDHCI_ESTATE int -32
21
22note SDHCI register offsets
23const SDHCI_R_BLOCK uint 0x04
24const SDHCI_R_ARG uint 0x08
25const SDHCI_R_CMD uint 0x0c
26const SDHCI_R_RESP0 uint 0x10
27const SDHCI_R_RESP1 uint 0x14
28const SDHCI_R_RESP2 uint 0x18
29const SDHCI_R_RESP3 uint 0x1c
30const SDHCI_R_BUF uint 0x20
31const SDHCI_R_PRESENT uint 0x24
32const SDHCI_R_HOST uint 0x28
33const SDHCI_R_CLOCK uint 0x2c
34const SDHCI_R_INT uint 0x30
35const SDHCI_R_INT_EN uint 0x34
36const SDHCI_R_SIG_EN uint 0x38
37const SDHCI_R_CAP uint 0x40
38const SDHCI_R_VERSION uint 0xfc
39
40note QEMU virt PCIe ECAM / MMIO constants (from qemu/sdhci_payload.c, payload.c)
41const SDHCI_QEMU_UART ulong 0x09000000
42const SDHCI_QEMU_ECAM ulong 0x3f000000
43const SDHCI_QEMU_MMIO ulong 0x10000000
44const SDHCI_PCI_CLASS_SDHCI uint 0x0805
45const SDHCI_PCI_CLASS_NVME uint 0x010802
46
47note PRESENT_STATE bits
48const SD_PS_CMD_INHIBIT uint 0x00000001
49const SD_PS_DATA_INHIBIT uint 0x00000002
50const SD_PS_CARD_PRESENT uint 0x00010000
51
52note INT_STATUS bits
53const SD_INT_CMD_DONE uint 0x00000001
54const SD_INT_XFER_DONE uint 0x00000002
55const SD_INT_BUF_WR uint 0x00000010
56const SD_INT_BUF_RD uint 0x00000020
57const SD_INT_ERROR uint 0x00008000
58const SD_INT_ERR_MASK uint 0xffff8000
59
60note COMMAND / TRANSFER_MODE bits
61const SD_CMD_RESP_NONE uint 0x00
62const SD_CMD_RESP_LONG uint 0x01
63const SD_CMD_RESP_SHORT uint 0x02
64const SD_CMD_RESP_BUSY uint 0x03
65const SD_CMD_CRC uint 0x08
66const SD_CMD_INDEX uint 0x10
67const SD_CMD_DATA uint 0x20
68
69const SD_TM_BLK_CNT_EN uint 0x02
70const SD_TM_AUTO_CMD12 uint 0x04
71const SD_TM_READ uint 0x10
72const SD_TM_MULTI uint 0x20
73
74note CLOCK / HOST bits
75const SD_CLK_INT_EN uint 0x0001
76const SD_CLK_INT_STABLE uint 0x0002
77const SD_CLK_CARD_EN uint 0x0004
78const SD_RST_ALL uint 0x01000000
79const SD_RST_CMD uint 0x02000000
80const SD_RST_DATA uint 0x04000000
81const SD_HOST_4BIT uint 0x02
82const SD_PWR_330 uint 0x0e00
83const SD_PWR_ON uint 0x0100
84
85note Response kinds
86const SD_R_NONE uint 0
87const SD_R1 uint 1
88const SD_R1B uint 2
89const SD_R2 uint 3
90const SD_R3 uint 4
91const SD_R6 uint 5
92const SD_R7 uint 6
93
94const SD_R1_ERRORS uint 0xfdf98008
95const SD_R1_READY_FOR_DATA uint 0x00000100
96const SD_STATE_TRAN uint 4
97
98note Driver state globals
99note sd_u32: 0=ready, 1=spec, 2=base_hz, 3=card_hz, 4=rca, 5=high_cap, 6=max_blocks, 7=timeout_ms, 8=bus_4bit, 9=last_int
100global sd_u32 uint 10
101note sd_i32: 0=last_cmd, 1=last_err
102global sd_i32 int 2
103global sd_blocks ulong 1
104global sd_cid uint 4
105global sd_csd uint 4
106global sd_resp uint 4
107
108note Software SDHCI controller + 64-block (32 KiB) SDHC card model for host behavioral verification
109const SD_SIM_BLOCKS ulong 64
110global ssim_regs uint 64
111global ssim_mem uchar 32768
112note ssim_st: 0=active, 1=cmd55_next, 2=acmd41_polls, 3=card_present, 4=xfer_dir (1=rd,2=wr), 5=xfer_lba, 6=xfer_rem_words, 7=xfer_word_off, 8=inject_err, 9=inject_r1_err, 10=bad_cmd8, 11=spec_ver, 12=has_3v3, 13=base_mhz, 14=csd_csize
113global ssim_st uint 15
114
115fn sdhci_sim_init num_blocks ulong to void
116for i ulong 64
117put ssim_regs i 0
118end
119for i ulong 32768
120put ssim_mem i 0
121end
122for i ulong 15
123put ssim_st i 0
124end
125put ssim_st 0 1
126put ssim_st 3 1
127put ssim_st 11 2
128put ssim_st 12 1
129put ssim_st 13 50
130csize = uint 0
131if gt num_blocks 1024
132csize = uint sub div num_blocks 1024 1
133end
134put ssim_st 14 csize
135note Version register at 0xfc: spec=2 (3.00) in bits 23:16
136put ssim_regs 63 shl uint 2 16
137note Capabilities at 0x40: bit 24 = 3.3V, bits 15:8 = base_mhz (50)
138put ssim_regs 16 or shl uint 1 24 shl uint 50 8
139note Present state at 0x24: card present
140put ssim_regs 9 SD_PS_CARD_PRESENT
141end
142
143fn sdhci_sim_set_spec spec_ver uint to void
144put ssim_st 11 spec_ver
145put ssim_regs 63 shl spec_ver 16
146end
147
148fn sdhci_sim_set_volt has_3v3 uint to void
149put ssim_st 12 has_3v3
150cap = shl get ssim_st 13 8
151if ne has_3v3 0
152cap = or cap shl uint 1 24
153end
154put ssim_regs 16 cap
155end
156
157fn sdhci_sim_set_card_present pres uint to void
158put ssim_st 3 pres
159if ne pres 0
160put ssim_regs 9 SD_PS_CARD_PRESENT
161else
162put ssim_regs 9 0
163end
164end
165
166fn sdhci_sim_inject_err err_bits uint to void
167put ssim_st 8 err_bits
168end
169
170fn sdhci_sim_inject_r1 r1_bits uint to void
171put ssim_st 9 r1_bits
172end
173
174fn sdhci_sim_set_bad_cmd8 v uint to void
175put ssim_st 10 v
176end
177
178fn ssim_exec_cmd cmd_word uint to void
179idx = and shr cmd_word 24 0x3f
180arg = get ssim_regs 2
181is_app = get ssim_st 1
182put ssim_st 1 0
183inj = get ssim_st 8
184if ne inj 0
185put ssim_st 8 0
186put ssim_regs 12 or SD_INT_ERROR inj
187ret
188end
189r1_base = or shl SD_STATE_TRAN 9 SD_R1_READY_FOR_DATA
190r1_err = get ssim_st 9
191if ne r1_err 0
192put ssim_st 9 0
193r1_base = or r1_base r1_err
194end
195put ssim_regs 4 0
196put ssim_regs 5 0
197put ssim_regs 6 0
198put ssim_regs 7 0
199if eq idx 0
200put ssim_regs 12 SD_INT_CMD_DONE
201ret
202end
203if eq idx 8
204if ne get ssim_st 10 0
205put ssim_regs 4 0x000
206else
207put ssim_regs 4 arg
208end
209put ssim_regs 12 SD_INT_CMD_DONE
210ret
211end
212if eq idx 55
213put ssim_st 1 1
214put ssim_regs 4 r1_base
215put ssim_regs 12 SD_INT_CMD_DONE
216ret
217end
218if and ne is_app 0 eq idx 41
219polls = add get ssim_st 2 1
220put ssim_st 2 polls
221if ge polls 2
222note Ready + CCS (SDHC)
223put ssim_regs 4 0xc0ff8000
224else
225put ssim_regs 4 0x00ff8000
226end
227put ssim_regs 12 SD_INT_CMD_DONE
228ret
229end
230if eq idx 2
231put ssim_regs 4 0x12345678
232put ssim_regs 5 0x9abcdef0
233put ssim_regs 6 0x11223344
234put ssim_regs 7 0x55667788
235put ssim_regs 12 SD_INT_CMD_DONE
236ret
237end
238if eq idx 3
239note RCA = 0x1234
240put ssim_regs 4 shl uint 0x1234 16
241put ssim_regs 12 SD_INT_CMD_DONE
242ret
243end
244if eq idx 9
245note CSD v2.0 (structure=1 at CSD bits 127:126 -> reg bits 119:118 -> resp[3] bits 23:22)
246note C_SIZE at CSD bits 69:48 -> reg bits 61:40 -> resp[1] bits 29:8
247put ssim_regs 7 shl uint 1 22
248put ssim_regs 5 shl and get ssim_st 14 0x3fffff 8
249put ssim_regs 12 SD_INT_CMD_DONE
250ret
251end
252if eq idx 7
253put ssim_regs 4 r1_base
254put ssim_regs 12 or SD_INT_CMD_DONE SD_INT_XFER_DONE
255ret
256end
257if and ne is_app 0 eq idx 6
258put ssim_regs 4 r1_base
259put ssim_regs 12 SD_INT_CMD_DONE
260ret
261end
262if or eq idx 13 eq idx 16
263put ssim_regs 4 r1_base
264put ssim_regs 12 SD_INT_CMD_DONE
265ret
266end
267if or eq idx 17 eq idx 18
268nblks = shr get ssim_regs 1 16
269if eq nblks 0
270nblks = uint 1
271end
272put ssim_st 4 1
273put ssim_st 5 arg
274put ssim_st 6 mul nblks 128
275put ssim_st 7 0
276put ssim_regs 4 r1_base
277put ssim_regs 12 or SD_INT_CMD_DONE SD_INT_BUF_RD
278ret
279end
280if or eq idx 24 eq idx 25
281nblks = shr get ssim_regs 1 16
282if eq nblks 0
283nblks = uint 1
284end
285put ssim_st 4 2
286put ssim_st 5 arg
287put ssim_st 6 mul nblks 128
288put ssim_st 7 0
289put ssim_regs 4 r1_base
290put ssim_regs 12 or SD_INT_CMD_DONE SD_INT_BUF_WR
291ret
292end
293put ssim_regs 4 r1_base
294put ssim_regs 12 SD_INT_CMD_DONE
295end
296
297fn sdhci_r32 off uint to uint
298if eq off SDHCI_R_BUF
299if eq get ssim_st 4 1
300lba = ulong get ssim_st 5
301woff = ulong get ssim_st 7
302boff = add mul rem lba SD_SIM_BLOCKS 512 mul woff 4
303v = uint 0
304if lt add boff 3 32768
305b0 = uint get ssim_mem boff
306b1 = shl uint get ssim_mem add boff 1 8
307b2 = shl uint get ssim_mem add boff 2 16
308b3 = shl uint get ssim_mem add boff 3 24
309v = or or b0 b1 or b2 b3
310end
311woff = add woff 1
312put ssim_st 7 uint woff
313remw = get ssim_st 6
314if gt remw 0
315remw = sub remw 1
316put ssim_st 6 remw
317end
318if eq remw 0
319put ssim_st 4 0
320put ssim_regs 12 or get ssim_regs 12 SD_INT_XFER_DONE
321else
322if eq rem woff 128 0
323put ssim_regs 12 or get ssim_regs 12 SD_INT_BUF_RD
324end
325end
326ret v
327end
328ret 0
329end
330ridx = ulong shr off 2
331if lt ridx 64
332ret get ssim_regs ridx
333end
334ret 0
335end
336
337fn sdhci_w32 off uint v uint to void
338if eq off SDHCI_R_BUF
339if eq get ssim_st 4 2
340lba = ulong get ssim_st 5
341woff = ulong get ssim_st 7
342boff = add mul rem lba SD_SIM_BLOCKS 512 mul woff 4
343if lt add boff 3 32768
344put ssim_mem boff uchar and v 0xff
345put ssim_mem add boff 1 uchar and shr v 8 0xff
346put ssim_mem add boff 2 uchar and shr v 16 0xff
347put ssim_mem add boff 3 uchar and shr v 24 0xff
348end
349woff = add woff 1
350put ssim_st 7 uint woff
351remw = get ssim_st 6
352if gt remw 0
353remw = sub remw 1
354put ssim_st 6 remw
355end
356if eq remw 0
357put ssim_st 4 0
358put ssim_regs 12 or get ssim_regs 12 SD_INT_XFER_DONE
359else
360if eq rem woff 128 0
361put ssim_regs 12 or get ssim_regs 12 SD_INT_BUF_WR
362end
363end
364end
365ret
366end
367if eq off SDHCI_R_INT
368cur = get ssim_regs 12
369put ssim_regs 12 and cur xor v 0xffffffff
370ret
371end
372if eq off SDHCI_R_CLOCK
373nv = and v xor or SD_RST_ALL or SD_RST_CMD SD_RST_DATA 0xffffffff
374if ne and nv SD_CLK_INT_EN 0
375nv = or nv SD_CLK_INT_STABLE
376end
377put ssim_regs 11 nv
378ret
379end
380if eq off SDHCI_R_CMD
381put ssim_regs 3 v
382ssim_exec_cmd v
383ret
384end
385ridx = ulong shr off 2
386if lt ridx 64
387put ssim_regs ridx v
388end
389end
390
391note ---- SDHCI Core Driver Logic (disk_sdhci.c) ----
392
393fn sd_fail cmd int err int to int
394put sd_i32 0 cmd
395put sd_i32 1 err
396ret err
397end
398
399fn sd_wait_reg off uint mask uint want uint ms uint to int
400polls = add mul ms 100 1
401i = uint 0
402while lt i polls bound 200000
403if eq and sdhci_r32 off mask want
404ret 0
405end
406i = add i 1
407end
408ret -1
409end
410
411fn sd_line_reset bits uint to void
412v = sdhci_r32 SDHCI_R_CLOCK
413all_rst = or SD_RST_ALL or SD_RST_CMD SD_RST_DATA
414if ne and v all_rst 0
415w1 = sd_wait_reg SDHCI_R_CLOCK all_rst 0 100
416end
417v = and sdhci_r32 SDHCI_R_CLOCK 0x00ffffff
418sdhci_w32 SDHCI_R_CLOCK or v bits
419w2 = sd_wait_reg SDHCI_R_CLOCK bits 0 100
420end
421
422fn sd_wait_int mask uint ms uint to int
423polls = add mul ms 100 1
424i = uint 0
425while lt i polls bound 200000
426s = sdhci_r32 SDHCI_R_INT
427put sd_u32 9 s
428if ne and s or SD_INT_ERROR SD_INT_ERR_MASK 0
429ret -1
430end
431if ne and s mask 0
432sdhci_w32 SDHCI_R_INT and s mask
433ret 0
434end
435i = add i 1
436end
437put sd_u32 9 sdhci_r32 SDHCI_R_INT
438ret -2
439end
440
441fn sd_cmd_flags kind uint to uint
442if or eq kind SD_R1 or eq kind SD_R6 eq kind SD_R7
443ret or SD_CMD_RESP_SHORT or SD_CMD_CRC SD_CMD_INDEX
444end
445if eq kind SD_R1B
446ret or SD_CMD_RESP_BUSY or SD_CMD_CRC SD_CMD_INDEX
447end
448if eq kind SD_R2
449ret or SD_CMD_RESP_LONG SD_CMD_CRC
450end
451if eq kind SD_R3
452ret SD_CMD_RESP_SHORT
453end
454ret SD_CMD_RESP_NONE
455end
456
457fn sd_command idx uint arg uint kind uint tmode uint nblocks uint to int
458ms = get sd_u32 7
459inhibit = SD_PS_CMD_INHIBIT
460if or ne nblocks 0 eq kind SD_R1B
461inhibit = or inhibit SD_PS_DATA_INHIBIT
462end
463if ne sd_wait_reg SDHCI_R_PRESENT inhibit 0 ms 0
464ret sd_fail int idx SDHCI_ETIMEOUT
465end
466sdhci_w32 SDHCI_R_INT 0xffffffff
467if ne nblocks 0
468sdhci_w32 SDHCI_R_BLOCK or 512 shl nblocks 16
469end
470sdhci_w32 SDHCI_R_ARG arg
471f = sd_cmd_flags kind
472if ne nblocks 0
473f = or f SD_CMD_DATA
474end
475sdhci_w32 SDHCI_R_CMD or shl or shl idx 8 f 16 tmode
476w = sd_wait_int SD_INT_CMD_DONE ms
477if ne w 0
478sd_line_reset or SD_RST_CMD SD_RST_DATA
479if eq w -2
480ret sd_fail int idx SDHCI_ETIMEOUT
481end
482ret sd_fail int idx SDHCI_ECMD
483end
484put sd_resp 0 sdhci_r32 SDHCI_R_RESP0
485if eq kind SD_R2
486put sd_resp 1 sdhci_r32 SDHCI_R_RESP1
487put sd_resp 2 sdhci_r32 SDHCI_R_RESP2
488put sd_resp 3 sdhci_r32 SDHCI_R_RESP3
489end
490if eq kind SD_R1B
491if ne sd_wait_int SD_INT_XFER_DONE ms 0
492sd_line_reset or SD_RST_CMD SD_RST_DATA
493ret sd_fail int idx SDHCI_ETIMEOUT
494end
495end
496if or eq kind SD_R1 eq kind SD_R1B
497if ne and get sd_resp 0 SD_R1_ERRORS 0
498ret sd_fail int idx SDHCI_ESTATUS
499end
500end
501ret SDHCI_OK
502end
503
504fn sd_app_cmd idx uint arg uint kind uint to int
505rc = sd_command 55 shl get sd_u32 4 16 SD_R1 0 0
506if ne rc SDHCI_OK
507ret rc
508end
509ret sd_command idx arg kind 0 0
510end
511
512fn sd_set_clock hz uint to int
513base = get sd_u32 2
514spec = get sd_u32 1
515divf = uint 0
516actual = uint 0
517if ge spec 2
518n = uint 0
519if gt base hz
520denom = mul 2 hz
521n = div sub add base denom 1 denom
522end
523if gt n 0x3ff
524ret SDHCI_ECLOCK
525end
526if ne n 0
527actual = div base mul 2 n
528else
529actual = base
530end
531divf = or shl and n 0xff 8 shl and shr n 8 0x3 6
532else
533dv = uint 1
534while and lt dv 256 gt div base dv hz bound 16
535dv = mul dv 2
536end
537if gt div base dv hz
538ret SDHCI_ECLOCK
539end
540actual = div base dv
541divf = shl div dv 2 8
542end
543v = and sdhci_r32 SDHCI_R_CLOCK 0xffff0000
544sdhci_w32 SDHCI_R_CLOCK v
545sdhci_w32 SDHCI_R_CLOCK or or v divf SD_CLK_INT_EN
546if ne sd_wait_reg SDHCI_R_CLOCK SD_CLK_INT_STABLE SD_CLK_INT_STABLE 100 0
547ret SDHCI_ECLOCK
548end
549sdhci_w32 SDHCI_R_CLOCK or or or v divf SD_CLK_INT_EN SD_CLK_CARD_EN
550put sd_u32 3 actual
551ret SDHCI_OK
552end
553
554fn sd_csd_bits hi uint lo uint to uint
555rhi = sub hi 8
556rlo = sub lo 8
557v = uint 0
558b = add rhi 1
559while gt b rlo bound 128
560b = sub b 1
561word = get sd_csd ulong div b 32
562bit = and shr word rem b 32 1
563v = or shl v 1 bit
564end
565ret v
566end
567
568fn sd_card_status to int
569rc = sd_command 13 shl get sd_u32 4 16 SD_R1 0 0
570ret rc
571end
572
573fn sd_wait_tran to int
574ms = get sd_u32 7
575i = uint 0
576while le i ms bound 10000
577rc = sd_card_status
578if ne rc SDHCI_OK
579ret rc
580end
581st = get sd_resp 0
582if and ne and st SD_R1_READY_FOR_DATA 0 eq and shr st 9 0xf SD_STATE_TRAN
583ret SDHCI_OK
584end
585i = add i 1
586end
587ret sd_fail 13 SDHCI_ETIMEOUT
588end
589
590fn sdhci_init_card dev edit DiskDev base_hz uint timeout_ms uint bus_4bit uint max_blocks uint to int
591for i ulong 10
592put sd_u32 i 0
593end
594put sd_i32 0 -1
595put sd_i32 1 0
596put sd_blocks 0 0
597tms = timeout_ms
598if eq tms 0
599tms = uint 1000
600end
601mb = max_blocks
602if eq mb 0
603mb = uint 128
604end
605if gt mb 65535
606ret sd_fail -1 SDHCI_EARG
607end
608put sd_u32 7 tms
609put sd_u32 6 mb
610put sd_u32 8 bus_4bit
611spec = and shr sdhci_r32 SDHCI_R_VERSION 16 0xff
612put sd_u32 1 spec
613if lt spec 1
614ret sd_fail -1 SDHCI_EVER
615end
616sdhci_w32 SDHCI_R_CLOCK or and sdhci_r32 SDHCI_R_CLOCK 0x00ffffff SD_RST_ALL
617if ne sd_wait_reg SDHCI_R_CLOCK SD_RST_ALL 0 100 0
618ret sd_fail -1 SDHCI_ERESET
619end
620cap = sdhci_r32 SDHCI_R_CAP
621if eq and cap shl uint 1 24 0
622ret sd_fail -1 SDHCI_EVOLT
623end
624bhz = base_hz
625if eq bhz 0
626cmask = uint 0x3f
627if ge spec 2
628cmask = uint 0xff
629end
630bhz = mul and shr cap 8 cmask 1000000
631end
632put sd_u32 2 bhz
633if eq bhz 0
634ret sd_fail -1 SDHCI_ECLOCK
635end
636ret sdhci_init_post dev
637end
638
639fn sdhci_setup_power_clock to int
640host = and sdhci_r32 SDHCI_R_HOST 0xffff0000
641sdhci_w32 SDHCI_R_HOST or host SD_PWR_330
642sdhci_w32 SDHCI_R_HOST or or host SD_PWR_330 SD_PWR_ON
643if ne sd_wait_reg SDHCI_R_PRESENT SD_PS_CARD_PRESENT SD_PS_CARD_PRESENT 100 0
644ret sd_fail -1 SDHCI_ENOCARD
645end
646ien = or SD_INT_ERR_MASK SD_INT_BUF_RD
647ien = or ien SD_INT_BUF_WR
648ien = or ien or SD_INT_XFER_DONE SD_INT_CMD_DONE
649sdhci_w32 SDHCI_R_INT_EN ien
650sdhci_w32 SDHCI_R_SIG_EN 0
651clk = and sdhci_r32 SDHCI_R_CLOCK 0x0000ffff
652sdhci_w32 SDHCI_R_CLOCK or clk shl uint 0x0e 16
653if ne sd_set_clock 400000 SDHCI_OK
654ret sd_fail -1 SDHCI_ECLOCK
655end
656ret SDHCI_OK
657end
658
659fn sdhci_init_post dev edit DiskDev to int
660rc = sdhci_setup_power_clock
661if ne rc SDHCI_OK
662ret rc
663end
664rc = sd_command 0 0 SD_R_NONE 0 0
665if ne rc SDHCI_OK
666ret rc
667end
668rc = sd_command 8 0x1aa SD_R7 0 0
669if or ne rc SDHCI_OK ne and get sd_resp 0 0xfff 0x1aa
670ret sd_fail 8 SDHCI_ECARD
671end
672ret sdhci_init_identify dev
673end
674
675fn sdhci_init_identify dev edit DiskDev to int
676ready_ocr = false
677i = uint 0
678while le i 100 bound 128
679rc = sd_app_cmd 41 0x40ff8000 SD_R3
680if ne rc SDHCI_OK
681ret rc
682end
683if ne and get sd_resp 0 0x80000000 0
684ready_ocr = true
685break
686end
687i = add i 1
688end
689if not ready_ocr
690ret sd_fail 41 SDHCI_ETIMEOUT
691end
692put sd_u32 5 and shr get sd_resp 0 30 1
693rc = sd_command 2 0 SD_R2 0 0
694if ne rc SDHCI_OK
695ret rc
696end
697for k ulong 4
698put sd_cid k get sd_resp k
699end
700rc = sd_command 3 0 SD_R6 0 0
701if ne rc SDHCI_OK
702ret rc
703end
704put sd_u32 4 shr get sd_resp 0 16
705rc = sd_command 9 shl get sd_u32 4 16 SD_R2 0 0
706if ne rc SDHCI_OK
707ret rc
708end
709for k ulong 4
710put sd_csd k get sd_resp k
711end
712struct_v = sd_csd_bits 127 126
713blks = ulong 0
714if eq struct_v 1
715csize = ulong sd_csd_bits 69 48
716blks = mul add csize 1 1024
717else
718if and eq struct_v 0 eq get sd_u32 5 0
719csize0 = ulong sd_csd_bits 73 62
720cmult = sd_csd_bits 49 47
721rblen = sd_csd_bits 83 80
722nbytes = shl shl add csize0 1 add cmult 2 rblen
723blks = div nbytes 512
724else
725ret sd_fail 9 SDHCI_ECSD
726end
727end
728if eq blks 0
729ret sd_fail 9 SDHCI_ECSD
730end
731put sd_blocks 0 blks
732if ne sd_set_clock 25000000 SDHCI_OK
733ret sd_fail -1 SDHCI_ECLOCK
734end
735rc = sd_command 7 shl get sd_u32 4 16 SD_R1B 0 0
736if ne rc SDHCI_OK
737ret rc
738end
739if ne get sd_u32 8 0
740rc = sd_app_cmd 6 2 SD_R1
741if ne rc SDHCI_OK
742ret rc
743end
744sdhci_w32 SDHCI_R_HOST or sdhci_r32 SDHCI_R_HOST SD_HOST_4BIT
745end
746if eq get sd_u32 5 0
747rc = sd_command 16 512 SD_R1 0 0
748if ne rc SDHCI_OK
749ret rc
750end
751end
752put dev backend DISK_BACKEND_SDHCI
753put dev block_size 512
754put dev block_count blks
755put dev max_blocks get sd_u32 6
756put sd_u32 0 1
757ret SDHCI_OK
758end
759
760fn sd_read_block buf edit uchar * base ulong to void
761i = ulong 0
762while lt i 512 bound 128
763v = sdhci_r32 SDHCI_R_BUF
764p = add base i
765put buf p uchar and v 0xff
766put buf add p 1 uchar and shr v 8 0xff
767put buf add p 2 uchar and shr v 16 0xff
768put buf add p 3 uchar and shr v 24 0xff
769i = add i 4
770end
771end
772
773fn sd_write_block buf view uchar * base ulong to void
774i = ulong 0
775while lt i 512 bound 128
776p = add base i
777b0 = uint get buf p
778b1 = shl uint get buf add p 1 8
779b2 = shl uint get buf add p 2 16
780b3 = shl uint get buf add p 3 24
781sdhci_w32 SDHCI_R_BUF or or b0 b1 or b2 b3
782i = add i 4
783end
784end
785
786fn sdhci_read lba ulong cnt uint buf edit uchar * off ulong to int
787if or eq cnt 0 gt cnt get sd_u32 6
788ret DISK_EARG
789end
790if eq get sd_u32 0 0
791ret DISK_ESTATE
792end
793blks = get sd_blocks 0
794if or ge lba blks gt ulong cnt sub blks lba
795ret DISK_ERANGE
796end
797addr = lba
798if eq get sd_u32 5 0
799addr = mul lba 512
800end
801if gt addr 0xffffffff
802ret DISK_ERANGE
803end
804idx = uint 17
805tm = SD_TM_READ
806if gt cnt 1
807idx = uint 18
808tm = or SD_TM_READ or SD_TM_BLK_CNT_EN or SD_TM_MULTI SD_TM_AUTO_CMD12
809end
810rc = sd_command idx uint addr SD_R1 tm cnt
811b = uint 0
812while and eq rc SDHCI_OK lt b cnt bound 65536
813w = sd_wait_int SD_INT_BUF_RD get sd_u32 7
814if ne w 0
815if eq w -2
816rc = sd_fail int idx SDHCI_ETIMEOUT
817else
818rc = sd_fail int idx SDHCI_EDATA
819end
820break
821end
822sd_read_block buf add off mul ulong b 512
823b = add b 1
824end
825if eq rc SDHCI_OK
826w = sd_wait_int SD_INT_XFER_DONE get sd_u32 7
827if ne w 0
828if eq w -2
829rc = sd_fail int idx SDHCI_ETIMEOUT
830else
831rc = sd_fail int idx SDHCI_EDATA
832end
833end
834end
835if ne rc SDHCI_OK
836sd_line_reset or SD_RST_CMD SD_RST_DATA
837put sd_u32 0 0
838ret DISK_EIO
839end
840ret DISK_OK
841end
842
843fn sdhci_write lba ulong cnt uint buf view uchar * off ulong to int
844if or eq cnt 0 gt cnt get sd_u32 6
845ret DISK_EARG
846end
847if eq get sd_u32 0 0
848ret DISK_ESTATE
849end
850blks = get sd_blocks 0
851if or ge lba blks gt ulong cnt sub blks lba
852ret DISK_ERANGE
853end
854addr = lba
855if eq get sd_u32 5 0
856addr = mul lba 512
857end
858if gt addr 0xffffffff
859ret DISK_ERANGE
860end
861idx = uint 24
862tm = uint 0
863if gt cnt 1
864idx = uint 25
865tm = or SD_TM_BLK_CNT_EN or SD_TM_MULTI SD_TM_AUTO_CMD12
866end
867rc = sd_command idx uint addr SD_R1 tm cnt
868b = uint 0
869while and eq rc SDHCI_OK lt b cnt bound 65536
870w = sd_wait_int SD_INT_BUF_WR get sd_u32 7
871if ne w 0
872if eq w -2
873rc = sd_fail int idx SDHCI_ETIMEOUT
874else
875rc = sd_fail int idx SDHCI_EDATA
876end
877break
878end
879sd_write_block buf add off mul ulong b 512
880b = add b 1
881end
882if eq rc SDHCI_OK
883w = sd_wait_int SD_INT_XFER_DONE get sd_u32 7
884if ne w 0
885if eq w -2
886rc = sd_fail int idx SDHCI_ETIMEOUT
887else
888rc = sd_fail int idx SDHCI_EDATA
889end
890end
891end
892if eq rc SDHCI_OK
893rc = sd_wait_tran
894end
895if ne rc SDHCI_OK
896sd_line_reset or SD_RST_CMD SD_RST_DATA
897put sd_u32 0 0
898ret DISK_EIO
899end
900ret DISK_OK
901end
902
903fn sdhci_flush to int
904if eq get sd_u32 0 0
905ret DISK_ESTATE
906end
907rc = sd_card_status
908st = get sd_resp 0
909if or ne rc SDHCI_OK or eq and st SD_R1_READY_FOR_DATA 0 ne and shr st 9 0xf SD_STATE_TRAN
910put sd_u32 0 0
911ret DISK_EIO
912end
913ret DISK_OK
914end
915
916fn sdhci_is_ready to bool
917ret ne get sd_u32 0 0
918end
919
920fn sdhci_last_err to int
921ret get sd_i32 1
922end
923
924fn sdhci_card_hz to uint
925ret get sd_u32 3
926end