summaryrefslogtreecommitdiffhomepage
path: root/dev/modules/MBCI/MBCI.h
blob: ebc73f69fefdfa7e620005c70614f3a0f6c6ea21 (plain)
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
/* -------------------------------------------

	Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved.

------------------------------------------- */

#ifndef _INC_MODULE_MBCI_H_
#define _INC_MODULE_MBCI_H_

#include <hint/CompilerHint.h>
#include <NewKit/Defines.h>
#include <modules/ACPI/ACPI.h>

/// @file MBCI.h
/// @brief Mini Bus Controller Interface.

/**
- VCC (IN) (OUT for MCU)
- CLK (IN) (OUT for MCU)
- ACK (BI) (Contains an Acknowledge Packet Frame)
- D0- (IN) (Starts with the Host Interface Packet Frame)
- D1- (IN) (Starts with the Host Interface Packet Frame)
- D0+ (OUT) (Starts with the Host Interface Packet Frame)
- D1+ (OUT) (Starts with the Host Interface Packet Frame)
- GND (IN) (OUT for MCU)
 */

#define kMBCIZeroSz (8)
#define kMBCIESBSz	(64)

namespace Kernel
{
	struct IMBCIHost;

	enum
	{
		kMBCISpeedDeviceInvalid,
		kMBCILowSpeedDevice,
		kMBCIHighSpeedDevice,
		kMBCISpeedDeviceCount,
	};

	/// @brief MBCI Host header.
	struct PACKED IMBCIHost final
	{
		UInt32 Magic;
		UInt32 HostId;
		UInt16 VendorId;
		UInt16 DeviceId;
		UInt8  MemoryType;
		UInt16 HostType;
		UInt16 HostFlags;
		UInt8  Error;
		UInt32 MMIOTest;
		UInt16 State;
		UInt8  Status;
		UInt8  InterruptEnable;
		UInt64 BaseAddressRegister;
		UInt64 BaseAddressRegisterSize;
		UInt32 CommandIssue;
		UInt8  Esb[kMBCIESBSz]; // Extended Signature Block
		UInt8  Zero[kMBCIZeroSz];
	};

	/// @brief MBCI host flags.
	enum MBCIHostFlags
	{
		kMBCIHostFlagsSupportsNothing,		// Invalid MBCI device.
		kMBCIHostFlagsSupportsAPM,			// FW's Advanced Power Management.
		kMBCIHostFlagsSupportsDaisyChain,	// Is daisy chained.
		kMBCIHostFlagsSupportsHWInterrupts, // Has HW interrupts.
		kMBCIHostFlagsSupportsDMA,			// Has DMA.
		kMBCIHostFlagsExtended,				// Extended flags table.
	};

	/// @brief MBCI host kind.
	enum MBCIHostKind
	{
		kMBCIHostKindHardDisk,
		kMBCIHostKindOpticalDisk,
		kMBCIHostKindKeyboardLow,
		kMBCIHostKindMouseLow,
		kMBCIHostKindMouseHigh,
		kMBCIHostKindKeyboardHigh,
		kMBCIHostKindNetworkInterface,
		kMBCIHostKindDaisyChain,
		kMBCIHostKindStartExtended, // Extended vendor table limit.
	};

	enum MBCIHostState
	{
		kMBCIHostStateInvalid,
		kMBCIHostStateReset,
		kMBCIHostStateSuccess,
		kMBCIHostStateReady,
		kMBCIHostStateDmaStart,
		kMBCIHostStateDmaEnd,
		kMBCIHostStateFail,
		kMBCIHostStateCount,
	};

	/// @brief An AuthKey is a context used to tokenize data for an MBCI packet.
	typedef UInt32 MBCIAuthKeyType;

	/// @brief Read Auth key for MBCI host.
	/// @param host the mbci host to get the key on.
	/// @return the 24-bit key.
	inline MBCIAuthKeyType mbci_read_auth_key(_Input volatile struct IMBCIHost* host)
	{
		constexpr auto const kChallengeMBCI = 0xdeadbeef;

		host->MMIOTest = kChallengeMBCI;

		if (host->MMIOTest == kChallengeMBCI)
		{
			return (host->Esb[kMBCIESBSz - 1] << 16) | (host->Esb[kMBCIESBSz - 2] << 8) | (host->Esb[kMBCIESBSz - 3] & 0xFF);
		}

		return kChallengeMBCI;
	}

	inline BOOL mbci_test_mmio(_Input volatile struct IMBCIHost* host)
	{
		constexpr auto const kChallengeMBCI = 0xdeadbeef;

		host->MMIOTest = kChallengeMBCI;
		return host->MMIOTest == kChallengeMBCI;
	}
} // namespace Kernel

#endif // ifndef _INC_MODULE_MBCI_H_