• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3  * Chromium OS cros_ec driver
4  *
5  * Copyright (c) 2012 The Chromium OS Authors.
6  */
7 
8 #ifndef _CROS_EC_H
9 #define _CROS_EC_H
10 
11 #include <linux/compiler.h>
12 #include <ec_commands.h>
13 #include <cros_ec_message.h>
14 #include <asm/gpio.h>
15 #include <dm/of_extra.h>
16 
17 /* Our configuration information */
18 struct cros_ec_dev {
19 	struct udevice *dev;		/* Transport device */
20 	struct gpio_desc ec_int;	/* GPIO used as EC interrupt line */
21 	int protocol_version;           /* Protocol version to use */
22 	int optimise_flash_write;	/* Don't write erased flash blocks */
23 
24 	/*
25 	 * These two buffers will always be dword-aligned and include enough
26 	 * space for up to 7 word-alignment bytes also, so we can ensure that
27 	 * the body of the message is always dword-aligned (64-bit).
28 	 *
29 	 * We use this alignment to keep ARM and x86 happy. Probably word
30 	 * alignment would be OK, there might be a small performance advantage
31 	 * to using dword.
32 	 */
33 	uint8_t din[ALIGN(MSG_BYTES + sizeof(int64_t), sizeof(int64_t))]
34 		__aligned(sizeof(int64_t));
35 	uint8_t dout[ALIGN(MSG_BYTES + sizeof(int64_t), sizeof(int64_t))]
36 		__aligned(sizeof(int64_t));
37 };
38 
39 /*
40  * Hard-code the number of columns we happen to know we have right now.  It
41  * would be more correct to call cros_ec_info() at startup and determine the
42  * actual number of keyboard cols from there.
43  */
44 #define CROS_EC_KEYSCAN_COLS 13
45 
46 /* Information returned by a key scan */
47 struct mbkp_keyscan {
48 	uint8_t data[CROS_EC_KEYSCAN_COLS];
49 };
50 
51 /* Holds information about the Chrome EC */
52 struct fdt_cros_ec {
53 	struct fmap_entry flash;	/* Address and size of EC flash */
54 	/*
55 	 * Byte value of erased flash, or -1 if not known. It is normally
56 	 * 0xff but some flash devices use 0 (e.g. STM32Lxxx)
57 	 */
58 	int flash_erase_value;
59 	struct fmap_entry region[EC_FLASH_REGION_COUNT];
60 };
61 
62 /**
63  * Read the ID of the CROS-EC device
64  *
65  * The ID is a string identifying the CROS-EC device.
66  *
67  * @param dev		CROS-EC device
68  * @param id		Place to put the ID
69  * @param maxlen	Maximum length of the ID field
70  * @return 0 if ok, -1 on error
71  */
72 int cros_ec_read_id(struct cros_ec_dev *dev, char *id, int maxlen);
73 
74 /**
75  * Read a keyboard scan from the CROS-EC device
76  *
77  * Send a message requesting a keyboard scan and return the result
78  *
79  * @param dev		CROS-EC device
80  * @param scan		Place to put the scan results
81  * @return 0 if ok, -1 on error
82  */
83 int cros_ec_scan_keyboard(struct udevice *dev, struct mbkp_keyscan *scan);
84 
85 /**
86  * Read which image is currently running on the CROS-EC device.
87  *
88  * @param dev		CROS-EC device
89  * @param image		Destination for image identifier
90  * @return 0 if ok, <0 on error
91  */
92 int cros_ec_read_current_image(struct cros_ec_dev *dev,
93 		enum ec_current_image *image);
94 
95 /**
96  * Read the hash of the CROS-EC device firmware.
97  *
98  * @param dev		CROS-EC device
99  * @param hash		Destination for hash information
100  * @return 0 if ok, <0 on error
101  */
102 int cros_ec_read_hash(struct cros_ec_dev *dev,
103 		struct ec_response_vboot_hash *hash);
104 
105 /**
106  * Send a reboot command to the CROS-EC device.
107  *
108  * Note that some reboot commands (such as EC_REBOOT_COLD) also reboot the AP.
109  *
110  * @param dev		CROS-EC device
111  * @param cmd		Reboot command
112  * @param flags         Flags for reboot command (EC_REBOOT_FLAG_*)
113  * @return 0 if ok, <0 on error
114  */
115 int cros_ec_reboot(struct cros_ec_dev *dev, enum ec_reboot_cmd cmd,
116 		uint8_t flags);
117 
118 /**
119  * Check if the CROS-EC device has an interrupt pending.
120  *
121  * Read the status of the external interrupt connected to the CROS-EC device.
122  * If no external interrupt is configured, this always returns 1.
123  *
124  * @param dev		CROS-EC device
125  * @return 0 if no interrupt is pending
126  */
127 int cros_ec_interrupt_pending(struct udevice *dev);
128 
129 enum {
130 	CROS_EC_OK,
131 	CROS_EC_ERR = 1,
132 	CROS_EC_ERR_FDT_DECODE,
133 	CROS_EC_ERR_CHECK_VERSION,
134 	CROS_EC_ERR_READ_ID,
135 	CROS_EC_ERR_DEV_INIT,
136 };
137 
138 /**
139  * Initialise the Chromium OS EC driver
140  *
141  * @param blob		Device tree blob containing setup information
142  * @param cros_ecp        Returns pointer to the cros_ec device, or NULL if none
143  * @return 0 if we got an cros_ec device and all is well (or no cros_ec is
144  *	expected), -ve if we should have an cros_ec device but failed to find
145  *	one, or init failed (-CROS_EC_ERR_...).
146  */
147 int cros_ec_init(const void *blob, struct cros_ec_dev **cros_ecp);
148 
149 /**
150  * Read information about the keyboard matrix
151  *
152  * @param dev		CROS-EC device
153  * @param info		Place to put the info structure
154  */
155 int cros_ec_info(struct cros_ec_dev *dev,
156 		struct ec_response_mkbp_info *info);
157 
158 /**
159  * Read the host event flags
160  *
161  * @param dev		CROS-EC device
162  * @param events_ptr	Destination for event flags.  Not changed on error.
163  * @return 0 if ok, <0 on error
164  */
165 int cros_ec_get_host_events(struct cros_ec_dev *dev, uint32_t *events_ptr);
166 
167 /**
168  * Clear the specified host event flags
169  *
170  * @param dev		CROS-EC device
171  * @param events	Event flags to clear
172  * @return 0 if ok, <0 on error
173  */
174 int cros_ec_clear_host_events(struct cros_ec_dev *dev, uint32_t events);
175 
176 /**
177  * Get/set flash protection
178  *
179  * @param dev		CROS-EC device
180  * @param set_mask	Mask of flags to set; if 0, just retrieves existing
181  *                      protection state without changing it.
182  * @param set_flags	New flag values; only bits in set_mask are applied;
183  *                      ignored if set_mask=0.
184  * @param prot          Destination for updated protection state from EC.
185  * @return 0 if ok, <0 on error
186  */
187 int cros_ec_flash_protect(struct cros_ec_dev *dev,
188 		       uint32_t set_mask, uint32_t set_flags,
189 		       struct ec_response_flash_protect *resp);
190 
191 
192 /**
193  * Run internal tests on the cros_ec interface.
194  *
195  * @param dev		CROS-EC device
196  * @return 0 if ok, <0 if the test failed
197  */
198 int cros_ec_test(struct cros_ec_dev *dev);
199 
200 /**
201  * Update the EC RW copy.
202  *
203  * @param dev		CROS-EC device
204  * @param image		the content to write
205  * @param imafge_size	content length
206  * @return 0 if ok, <0 if the test failed
207  */
208 int cros_ec_flash_update_rw(struct cros_ec_dev *dev,
209 			 const uint8_t  *image, int image_size);
210 
211 /**
212  * Return a pointer to the board's CROS-EC device
213  *
214  * This should be implemented by board files.
215  *
216  * @return pointer to CROS-EC device, or NULL if none is available
217  */
218 struct cros_ec_dev *board_get_cros_ec_dev(void);
219 
220 struct dm_cros_ec_ops {
221 	int (*check_version)(struct udevice *dev);
222 	int (*command)(struct udevice *dev, uint8_t cmd, int cmd_version,
223 		       const uint8_t *dout, int dout_len,
224 		       uint8_t **dinp, int din_len);
225 	int (*packet)(struct udevice *dev, int out_bytes, int in_bytes);
226 };
227 
228 #define dm_cros_ec_get_ops(dev) \
229 		((struct dm_cros_ec_ops *)(dev)->driver->ops)
230 
231 int cros_ec_register(struct udevice *dev);
232 
233 /**
234  * Dump a block of data for a command.
235  *
236  * @param name	Name for data (e.g. 'in', 'out')
237  * @param cmd	Command number associated with data, or -1 for none
238  * @param data	Data block to dump
239  * @param len	Length of data block to dump
240  */
241 void cros_ec_dump_data(const char *name, int cmd, const uint8_t *data, int len);
242 
243 /**
244  * Calculate a simple 8-bit checksum of a data block
245  *
246  * @param data	Data block to checksum
247  * @param size	Size of data block in bytes
248  * @return checksum value (0 to 255)
249  */
250 int cros_ec_calc_checksum(const uint8_t *data, int size);
251 
252 int cros_ec_flash_erase(struct cros_ec_dev *dev, uint32_t offset,
253 		uint32_t size);
254 
255 /**
256  * Read data from the flash
257  *
258  * Read an arbitrary amount of data from the EC flash, by repeatedly reading
259  * small blocks.
260  *
261  * The offset starts at 0. You can obtain the region information from
262  * cros_ec_flash_offset() to find out where to read for a particular region.
263  *
264  * @param dev		CROS-EC device
265  * @param data		Pointer to data buffer to read into
266  * @param offset	Offset within flash to read from
267  * @param size		Number of bytes to read
268  * @return 0 if ok, -1 on error
269  */
270 int cros_ec_flash_read(struct cros_ec_dev *dev, uint8_t *data, uint32_t offset,
271 		    uint32_t size);
272 
273 /**
274  * Read back flash parameters
275  *
276  * This function reads back parameters of the flash as reported by the EC
277  *
278  * @param dev  Pointer to device
279  * @param info Pointer to output flash info struct
280  */
281 int cros_ec_read_flashinfo(struct cros_ec_dev *dev,
282 			  struct ec_response_flash_info *info);
283 
284 /**
285  * Write data to the flash
286  *
287  * Write an arbitrary amount of data to the EC flash, by repeatedly writing
288  * small blocks.
289  *
290  * The offset starts at 0. You can obtain the region information from
291  * cros_ec_flash_offset() to find out where to write for a particular region.
292  *
293  * Attempting to write to the region where the EC is currently running from
294  * will result in an error.
295  *
296  * @param dev		CROS-EC device
297  * @param data		Pointer to data buffer to write
298  * @param offset	Offset within flash to write to.
299  * @param size		Number of bytes to write
300  * @return 0 if ok, -1 on error
301  */
302 int cros_ec_flash_write(struct cros_ec_dev *dev, const uint8_t *data,
303 		     uint32_t offset, uint32_t size);
304 
305 /**
306  * Obtain position and size of a flash region
307  *
308  * @param dev		CROS-EC device
309  * @param region	Flash region to query
310  * @param offset	Returns offset of flash region in EC flash
311  * @param size		Returns size of flash region
312  * @return 0 if ok, -1 on error
313  */
314 int cros_ec_flash_offset(struct cros_ec_dev *dev, enum ec_flash_region region,
315 		      uint32_t *offset, uint32_t *size);
316 
317 /**
318  * Read/write VbNvContext from/to a CROS-EC device.
319  *
320  * @param dev		CROS-EC device
321  * @param block		Buffer of VbNvContext to be read/write
322  * @return 0 if ok, -1 on error
323  */
324 int cros_ec_read_vbnvcontext(struct cros_ec_dev *dev, uint8_t *block);
325 int cros_ec_write_vbnvcontext(struct cros_ec_dev *dev, const uint8_t *block);
326 
327 /**
328  * Read the version information for the EC images
329  *
330  * @param dev		CROS-EC device
331  * @param versionp	This is set to point to the version information
332  * @return 0 if ok, -1 on error
333  */
334 int cros_ec_read_version(struct cros_ec_dev *dev,
335 		       struct ec_response_get_version **versionp);
336 
337 /**
338  * Read the build information for the EC
339  *
340  * @param dev		CROS-EC device
341  * @param versionp	This is set to point to the build string
342  * @return 0 if ok, -1 on error
343  */
344 int cros_ec_read_build_info(struct cros_ec_dev *dev, char **strp);
345 
346 /**
347  * Switch on/off a LDO / FET.
348  *
349  * @param dev		CROS-EC device
350  * @param index		index of the LDO/FET to switch
351  * @param state		new state of the LDO/FET : EC_LDO_STATE_ON|OFF
352  * @return 0 if ok, -1 on error
353  */
354 int cros_ec_set_ldo(struct udevice *dev, uint8_t index, uint8_t state);
355 
356 /**
357  * Read back a LDO / FET current state.
358  *
359  * @param dev		CROS-EC device
360  * @param index		index of the LDO/FET to switch
361  * @param state		current state of the LDO/FET : EC_LDO_STATE_ON|OFF
362  * @return 0 if ok, -1 on error
363  */
364 int cros_ec_get_ldo(struct udevice *dev, uint8_t index, uint8_t *state);
365 
366 /**
367  * Get access to the error reported when cros_ec_board_init() was called
368  *
369  * This permits delayed reporting of the EC error if it failed during
370  * early init.
371  *
372  * @return error (0 if there was no error, -ve if there was an error)
373  */
374 int cros_ec_get_error(void);
375 
376 /**
377  * Returns information from the FDT about the Chrome EC flash
378  *
379  * @param dev		Device to read from
380  * @param config	Structure to use to return information
381  */
382 int cros_ec_decode_ec_flash(struct udevice *dev, struct fdt_cros_ec *config);
383 
384 /**
385  * Check the current keyboard state, in case recovery mode is requested.
386  * This function is for sandbox only.
387  *
388  * @param ec		CROS-EC device
389  */
390 void cros_ec_check_keyboard(struct cros_ec_dev *dev);
391 
392 struct i2c_msg;
393 /*
394  * Tunnel an I2C transfer to the EC
395  *
396  * @param dev		CROS-EC device
397  * @param port		The remote port on EC to use
398  * @param msg		List of messages to transfer
399  * @param nmsgs		Number of messages to transfer
400  */
401 int cros_ec_i2c_tunnel(struct udevice *dev, int port, struct i2c_msg *msg,
402 		       int nmsgs);
403 
404 #endif
405