1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * ChromeOS Wilco Embedded Controller
4  *
5  * Copyright 2018 Google LLC
6  */
7 
8 #ifndef WILCO_EC_H
9 #define WILCO_EC_H
10 
11 #include <linux/device.h>
12 #include <linux/kernel.h>
13 
14 /* Message flags for using the mailbox() interface */
15 #define WILCO_EC_FLAG_NO_RESPONSE	BIT(0) /* EC does not respond */
16 #define WILCO_EC_FLAG_EXTENDED_DATA	BIT(1) /* EC returns 256 data bytes */
17 #define WILCO_EC_FLAG_RAW_REQUEST	BIT(2) /* Do not trim request data */
18 #define WILCO_EC_FLAG_RAW_RESPONSE	BIT(3) /* Do not trim response data */
19 #define WILCO_EC_FLAG_RAW		(WILCO_EC_FLAG_RAW_REQUEST | \
20 					 WILCO_EC_FLAG_RAW_RESPONSE)
21 
22 /* Normal commands have a maximum 32 bytes of data */
23 #define EC_MAILBOX_DATA_SIZE		32
24 /* Extended commands have 256 bytes of response data */
25 #define EC_MAILBOX_DATA_SIZE_EXTENDED	256
26 
27 /**
28  * struct wilco_ec_device - Wilco Embedded Controller handle.
29  * @dev: Device handle.
30  * @mailbox_lock: Mutex to ensure one mailbox command at a time.
31  * @io_command: I/O port for mailbox command.  Provided by ACPI.
32  * @io_data: I/O port for mailbox data.  Provided by ACPI.
33  * @io_packet: I/O port for mailbox packet data.  Provided by ACPI.
34  * @data_buffer: Buffer used for EC communication.  The same buffer
35  *               is used to hold the request and the response.
36  * @data_size: Size of the data buffer used for EC communication.
37  * @debugfs_pdev: The child platform_device used by the debugfs sub-driver.
38  * @rtc_pdev: The child platform_device used by the RTC sub-driver.
39  */
40 struct wilco_ec_device {
41 	struct device *dev;
42 	struct mutex mailbox_lock;
43 	struct resource *io_command;
44 	struct resource *io_data;
45 	struct resource *io_packet;
46 	void *data_buffer;
47 	size_t data_size;
48 	struct platform_device *debugfs_pdev;
49 	struct platform_device *rtc_pdev;
50 };
51 
52 /**
53  * struct wilco_ec_request - Mailbox request message format.
54  * @struct_version: Should be %EC_MAILBOX_PROTO_VERSION
55  * @checksum: Sum of all bytes must be 0.
56  * @mailbox_id: Mailbox identifier, specifies the command set.
57  * @mailbox_version: Mailbox interface version %EC_MAILBOX_VERSION
58  * @reserved: Set to zero.
59  * @data_size: Length of request, data + last 2 bytes of the header.
60  * @command: Mailbox command code, unique for each mailbox_id set.
61  * @reserved_raw: Set to zero for most commands, but is used by
62  *                some command types and for raw commands.
63  */
64 struct wilco_ec_request {
65 	u8 struct_version;
66 	u8 checksum;
67 	u16 mailbox_id;
68 	u8 mailbox_version;
69 	u8 reserved;
70 	u16 data_size;
71 	u8 command;
72 	u8 reserved_raw;
73 } __packed;
74 
75 /**
76  * struct wilco_ec_response - Mailbox response message format.
77  * @struct_version: Should be %EC_MAILBOX_PROTO_VERSION
78  * @checksum: Sum of all bytes must be 0.
79  * @result: Result code from the EC.  Non-zero indicates an error.
80  * @data_size: Length of the response data buffer.
81  * @reserved: Set to zero.
82  * @mbox0: EC returned data at offset 0 is unused (always 0) so this byte
83  *         is treated as part of the header instead of the data.
84  * @data: Response data buffer.  Max size is %EC_MAILBOX_DATA_SIZE_EXTENDED.
85  */
86 struct wilco_ec_response {
87 	u8 struct_version;
88 	u8 checksum;
89 	u16 result;
90 	u16 data_size;
91 	u8 reserved[2];
92 	u8 mbox0;
93 	u8 data[0];
94 } __packed;
95 
96 /**
97  * enum wilco_ec_msg_type - Message type to select a set of command codes.
98  * @WILCO_EC_MSG_LEGACY: Legacy EC messages for standard EC behavior.
99  * @WILCO_EC_MSG_PROPERTY: Get/Set/Sync EC controlled NVRAM property.
100  * @WILCO_EC_MSG_TELEMETRY_SHORT: 32 bytes of telemetry data provided by the EC.
101  * @WILCO_EC_MSG_TELEMETRY_LONG: 256 bytes of telemetry data provided by the EC.
102  */
103 enum wilco_ec_msg_type {
104 	WILCO_EC_MSG_LEGACY = 0x00f0,
105 	WILCO_EC_MSG_PROPERTY = 0x00f2,
106 	WILCO_EC_MSG_TELEMETRY_SHORT = 0x00f5,
107 	WILCO_EC_MSG_TELEMETRY_LONG = 0x00f6,
108 };
109 
110 /**
111  * struct wilco_ec_message - Request and response message.
112  * @type: Mailbox message type.
113  * @flags: Message flags, e.g. %WILCO_EC_FLAG_NO_RESPONSE.
114  * @command: Mailbox command code.
115  * @result: Result code from the EC.  Non-zero indicates an error.
116  * @request_size: Number of bytes to send to the EC.
117  * @request_data: Buffer containing the request data.
118  * @response_size: Number of bytes expected from the EC.
119  *                 This is 32 by default and 256 if the flag
120  *                 is set for %WILCO_EC_FLAG_EXTENDED_DATA
121  * @response_data: Buffer containing the response data, should be
122  *                 response_size bytes and allocated by caller.
123  */
124 struct wilco_ec_message {
125 	enum wilco_ec_msg_type type;
126 	u8 flags;
127 	u8 command;
128 	u8 result;
129 	size_t request_size;
130 	void *request_data;
131 	size_t response_size;
132 	void *response_data;
133 };
134 
135 /**
136  * wilco_ec_mailbox() - Send request to the EC and receive the response.
137  * @ec: Wilco EC device.
138  * @msg: Wilco EC message.
139  *
140  * Return: Number of bytes received or negative error code on failure.
141  */
142 int wilco_ec_mailbox(struct wilco_ec_device *ec, struct wilco_ec_message *msg);
143 
144 #endif /* WILCO_EC_H */
145