xref: /haiku/src/system/boot/platform/riscv/htif.cpp (revision a182bd6eb5656b9fa14556fd0006bd514c6516fe)
1*a182bd6eSX512 /*
2*a182bd6eSX512  * Copyright 2021, Haiku, Inc.
3*a182bd6eSX512  * Distributed under the terms of the MIT License.
4*a182bd6eSX512  */
5*a182bd6eSX512 
6*a182bd6eSX512 
7*a182bd6eSX512 #include <Htif.h>
8*a182bd6eSX512 
9*a182bd6eSX512 
10*a182bd6eSX512 // This address is used by TinyEMU and it is not present in FDT.
11*a182bd6eSX512 HtifRegs* volatile gHtifRegs = (HtifRegs* volatile)0x40008000;
12*a182bd6eSX512 
13*a182bd6eSX512 
14*a182bd6eSX512 uint64_t
HtifCmd(uint32_t device,uint8_t cmd,uint32_t arg)15*a182bd6eSX512 HtifCmd(uint32_t device, uint8_t cmd, uint32_t arg)
16*a182bd6eSX512 {
17*a182bd6eSX512 	uint64_t htifTohost = ((uint64_t)device << 56)
18*a182bd6eSX512 		+ ((uint64_t)cmd << 48) + arg;
19*a182bd6eSX512 	gHtifRegs->toHostLo = htifTohost % ((uint64_t)1 << 32);
20*a182bd6eSX512 	gHtifRegs->toHostHi = htifTohost / ((uint64_t)1 << 32);
21*a182bd6eSX512 	return (uint64_t)gHtifRegs->fromHostLo
22*a182bd6eSX512 		+ ((uint64_t)gHtifRegs->fromHostHi << 32);
23*a182bd6eSX512 }
24*a182bd6eSX512 
25*a182bd6eSX512 
26*a182bd6eSX512 void
HtifShutdown()27*a182bd6eSX512 HtifShutdown()
28*a182bd6eSX512 {
29*a182bd6eSX512 	HtifCmd(0, 0, 1);
30*a182bd6eSX512 }
31*a182bd6eSX512 
32*a182bd6eSX512 
33*a182bd6eSX512 void
HtifOutChar(char ch)34*a182bd6eSX512 HtifOutChar(char ch)
35*a182bd6eSX512 {
36*a182bd6eSX512 	HtifCmd(1, 1, ch);
37*a182bd6eSX512 }
38*a182bd6eSX512 
39*a182bd6eSX512 
40*a182bd6eSX512 void
HtifOutString(const char * str)41*a182bd6eSX512 HtifOutString(const char* str)
42*a182bd6eSX512 {
43*a182bd6eSX512 	for (; *str != '\0'; str++) HtifOutChar(*str);
44*a182bd6eSX512 }
45*a182bd6eSX512 
46*a182bd6eSX512 
47*a182bd6eSX512 void
HtifOutString(const char * str,size_t len)48*a182bd6eSX512 HtifOutString(const char* str, size_t len)
49*a182bd6eSX512 {
50*a182bd6eSX512 	for (; len > 0; str++, len--) HtifOutChar(*str);
51*a182bd6eSX512 }
52