]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - include/cros_ec.h
cros_ec: spi: Add support for EC protocol version 3
[karo-tx-uboot.git] / include / cros_ec.h
1 /*
2  * Chromium OS cros_ec driver
3  *
4  * Copyright (c) 2012 The Chromium OS Authors.
5  *
6  * SPDX-License-Identifier:     GPL-2.0+
7  */
8
9 #ifndef _CROS_EC_H
10 #define _CROS_EC_H
11
12 #include <linux/compiler.h>
13 #include <ec_commands.h>
14 #include <fdtdec.h>
15 #include <cros_ec_message.h>
16
17 /* Which interface is the device on? */
18 enum cros_ec_interface_t {
19         CROS_EC_IF_NONE,
20         CROS_EC_IF_SPI,
21         CROS_EC_IF_I2C,
22         CROS_EC_IF_LPC, /* Intel Low Pin Count interface */
23 };
24
25 /* Our configuration information */
26 struct cros_ec_dev {
27         enum cros_ec_interface_t interface;
28         struct spi_slave *spi;          /* Our SPI slave, if using SPI */
29         int node;                       /* Our node */
30         int parent_node;                /* Our parent node (interface) */
31         unsigned int cs;                /* Our chip select */
32         unsigned int addr;              /* Device address (for I2C) */
33         unsigned int bus_num;           /* Bus number (for I2C) */
34         unsigned int max_frequency;     /* Maximum interface frequency */
35         struct fdt_gpio_state ec_int;   /* GPIO used as EC interrupt line */
36         int protocol_version;           /* Protocol version to use */
37         int optimise_flash_write;       /* Don't write erased flash blocks */
38
39         /*
40          * These two buffers will always be dword-aligned and include enough
41          * space for up to 7 word-alignment bytes also, so we can ensure that
42          * the body of the message is always dword-aligned (64-bit).
43          *
44          * We use this alignment to keep ARM and x86 happy. Probably word
45          * alignment would be OK, there might be a small performance advantage
46          * to using dword.
47          */
48         uint8_t din[ALIGN(MSG_BYTES + sizeof(int64_t), sizeof(int64_t))]
49                 __aligned(sizeof(int64_t));
50         uint8_t dout[ALIGN(MSG_BYTES + sizeof(int64_t), sizeof(int64_t))]
51                 __aligned(sizeof(int64_t));
52 };
53
54 /*
55  * Hard-code the number of columns we happen to know we have right now.  It
56  * would be more correct to call cros_ec_info() at startup and determine the
57  * actual number of keyboard cols from there.
58  */
59 #define CROS_EC_KEYSCAN_COLS 13
60
61 /* Information returned by a key scan */
62 struct mbkp_keyscan {
63         uint8_t data[CROS_EC_KEYSCAN_COLS];
64 };
65
66 /* Holds information about the Chrome EC */
67 struct fdt_cros_ec {
68         struct fmap_entry flash;        /* Address and size of EC flash */
69         /*
70          * Byte value of erased flash, or -1 if not known. It is normally
71          * 0xff but some flash devices use 0 (e.g. STM32Lxxx)
72          */
73         int flash_erase_value;
74         struct fmap_entry region[EC_FLASH_REGION_COUNT];
75 };
76
77 /**
78  * Read the ID of the CROS-EC device
79  *
80  * The ID is a string identifying the CROS-EC device.
81  *
82  * @param dev           CROS-EC device
83  * @param id            Place to put the ID
84  * @param maxlen        Maximum length of the ID field
85  * @return 0 if ok, -1 on error
86  */
87 int cros_ec_read_id(struct cros_ec_dev *dev, char *id, int maxlen);
88
89 /**
90  * Read a keyboard scan from the CROS-EC device
91  *
92  * Send a message requesting a keyboard scan and return the result
93  *
94  * @param dev           CROS-EC device
95  * @param scan          Place to put the scan results
96  * @return 0 if ok, -1 on error
97  */
98 int cros_ec_scan_keyboard(struct cros_ec_dev *dev, struct mbkp_keyscan *scan);
99
100 /**
101  * Read which image is currently running on the CROS-EC device.
102  *
103  * @param dev           CROS-EC device
104  * @param image         Destination for image identifier
105  * @return 0 if ok, <0 on error
106  */
107 int cros_ec_read_current_image(struct cros_ec_dev *dev,
108                 enum ec_current_image *image);
109
110 /**
111  * Read the hash of the CROS-EC device firmware.
112  *
113  * @param dev           CROS-EC device
114  * @param hash          Destination for hash information
115  * @return 0 if ok, <0 on error
116  */
117 int cros_ec_read_hash(struct cros_ec_dev *dev,
118                 struct ec_response_vboot_hash *hash);
119
120 /**
121  * Send a reboot command to the CROS-EC device.
122  *
123  * Note that some reboot commands (such as EC_REBOOT_COLD) also reboot the AP.
124  *
125  * @param dev           CROS-EC device
126  * @param cmd           Reboot command
127  * @param flags         Flags for reboot command (EC_REBOOT_FLAG_*)
128  * @return 0 if ok, <0 on error
129  */
130 int cros_ec_reboot(struct cros_ec_dev *dev, enum ec_reboot_cmd cmd,
131                 uint8_t flags);
132
133 /**
134  * Check if the CROS-EC device has an interrupt pending.
135  *
136  * Read the status of the external interrupt connected to the CROS-EC device.
137  * If no external interrupt is configured, this always returns 1.
138  *
139  * @param dev           CROS-EC device
140  * @return 0 if no interrupt is pending
141  */
142 int cros_ec_interrupt_pending(struct cros_ec_dev *dev);
143
144 enum {
145         CROS_EC_OK,
146         CROS_EC_ERR = 1,
147         CROS_EC_ERR_FDT_DECODE,
148         CROS_EC_ERR_CHECK_VERSION,
149         CROS_EC_ERR_READ_ID,
150         CROS_EC_ERR_DEV_INIT,
151 };
152
153 /**
154  * Initialise the Chromium OS EC driver
155  *
156  * @param blob          Device tree blob containing setup information
157  * @param cros_ecp        Returns pointer to the cros_ec device, or NULL if none
158  * @return 0 if we got an cros_ec device and all is well (or no cros_ec is
159  *      expected), -ve if we should have an cros_ec device but failed to find
160  *      one, or init failed (-CROS_EC_ERR_...).
161  */
162 int cros_ec_init(const void *blob, struct cros_ec_dev **cros_ecp);
163
164 /**
165  * Read information about the keyboard matrix
166  *
167  * @param dev           CROS-EC device
168  * @param info          Place to put the info structure
169  */
170 int cros_ec_info(struct cros_ec_dev *dev,
171                 struct ec_response_mkbp_info *info);
172
173 /**
174  * Read the host event flags
175  *
176  * @param dev           CROS-EC device
177  * @param events_ptr    Destination for event flags.  Not changed on error.
178  * @return 0 if ok, <0 on error
179  */
180 int cros_ec_get_host_events(struct cros_ec_dev *dev, uint32_t *events_ptr);
181
182 /**
183  * Clear the specified host event flags
184  *
185  * @param dev           CROS-EC device
186  * @param events        Event flags to clear
187  * @return 0 if ok, <0 on error
188  */
189 int cros_ec_clear_host_events(struct cros_ec_dev *dev, uint32_t events);
190
191 /**
192  * Get/set flash protection
193  *
194  * @param dev           CROS-EC device
195  * @param set_mask      Mask of flags to set; if 0, just retrieves existing
196  *                      protection state without changing it.
197  * @param set_flags     New flag values; only bits in set_mask are applied;
198  *                      ignored if set_mask=0.
199  * @param prot          Destination for updated protection state from EC.
200  * @return 0 if ok, <0 on error
201  */
202 int cros_ec_flash_protect(struct cros_ec_dev *dev,
203                        uint32_t set_mask, uint32_t set_flags,
204                        struct ec_response_flash_protect *resp);
205
206
207 /**
208  * Run internal tests on the cros_ec interface.
209  *
210  * @param dev           CROS-EC device
211  * @return 0 if ok, <0 if the test failed
212  */
213 int cros_ec_test(struct cros_ec_dev *dev);
214
215 /**
216  * Update the EC RW copy.
217  *
218  * @param dev           CROS-EC device
219  * @param image         the content to write
220  * @param imafge_size   content length
221  * @return 0 if ok, <0 if the test failed
222  */
223 int cros_ec_flash_update_rw(struct cros_ec_dev *dev,
224                          const uint8_t  *image, int image_size);
225
226 /**
227  * Return a pointer to the board's CROS-EC device
228  *
229  * This should be implemented by board files.
230  *
231  * @return pointer to CROS-EC device, or NULL if none is available
232  */
233 struct cros_ec_dev *board_get_cros_ec_dev(void);
234
235
236 /* Internal interfaces */
237 int cros_ec_i2c_init(struct cros_ec_dev *dev, const void *blob);
238 int cros_ec_spi_init(struct cros_ec_dev *dev, const void *blob);
239 int cros_ec_lpc_init(struct cros_ec_dev *dev, const void *blob);
240
241 /**
242  * Read information from the fdt for the i2c cros_ec interface
243  *
244  * @param dev           CROS-EC device
245  * @param blob          Device tree blob
246  * @return 0 if ok, -1 if we failed to read all required information
247  */
248 int cros_ec_i2c_decode_fdt(struct cros_ec_dev *dev, const void *blob);
249
250 /**
251  * Read information from the fdt for the spi cros_ec interface
252  *
253  * @param dev           CROS-EC device
254  * @param blob          Device tree blob
255  * @return 0 if ok, -1 if we failed to read all required information
256  */
257 int cros_ec_spi_decode_fdt(struct cros_ec_dev *dev, const void *blob);
258
259 /**
260  * Check whether the LPC interface supports new-style commands.
261  *
262  * LPC has its own way of doing this, which involves checking LPC values
263  * visible to the host. Do this, and update dev->protocol_version accordingly.
264  *
265  * @param dev           CROS-EC device to check
266  */
267 int cros_ec_lpc_check_version(struct cros_ec_dev *dev);
268
269 /**
270  * Send a command to an I2C CROS-EC device and return the reply.
271  *
272  * This rather complicated function deals with sending both old-style and
273  * new-style commands. The old ones have just a command byte and arguments.
274  * The new ones have version, command, arg-len, [args], chksum so are 3 bytes
275  * longer.
276  *
277  * The device's internal input/output buffers are used.
278  *
279  * @param dev           CROS-EC device
280  * @param cmd           Command to send (EC_CMD_...)
281  * @param cmd_version   Version of command to send (EC_VER_...)
282  * @param dout          Output data (may be NULL If dout_len=0)
283  * @param dout_len      Size of output data in bytes
284  * @param dinp          Returns pointer to response data
285  * @param din_len       Maximum size of response in bytes
286  * @return number of bytes in response, or -1 on error
287  */
288 int cros_ec_i2c_command(struct cros_ec_dev *dev, uint8_t cmd, int cmd_version,
289                      const uint8_t *dout, int dout_len,
290                      uint8_t **dinp, int din_len);
291
292 /**
293  * Send a command to a LPC CROS-EC device and return the reply.
294  *
295  * The device's internal input/output buffers are used.
296  *
297  * @param dev           CROS-EC device
298  * @param cmd           Command to send (EC_CMD_...)
299  * @param cmd_version   Version of command to send (EC_VER_...)
300  * @param dout          Output data (may be NULL If dout_len=0)
301  * @param dout_len      Size of output data in bytes
302  * @param dinp          Returns pointer to response data
303  * @param din_len       Maximum size of response in bytes
304  * @return number of bytes in response, or -1 on error
305  */
306 int cros_ec_lpc_command(struct cros_ec_dev *dev, uint8_t cmd, int cmd_version,
307                      const uint8_t *dout, int dout_len,
308                      uint8_t **dinp, int din_len);
309
310 int cros_ec_spi_command(struct cros_ec_dev *dev, uint8_t cmd, int cmd_version,
311                      const uint8_t *dout, int dout_len,
312                      uint8_t **dinp, int din_len);
313
314 /**
315  * Send a packet to a CROS-EC device and return the response packet.
316  *
317  * Expects the request packet to be stored in dev->dout.  Stores the response
318  * packet in dev->din.
319  *
320  * @param dev           CROS-EC device
321  * @param out_bytes     Size of request packet to output
322  * @param in_bytes      Maximum size of response packet to receive
323  * @return number of bytes in response packet, or <0 on error
324  */
325 int cros_ec_spi_packet(struct cros_ec_dev *dev, int out_bytes, int in_bytes);
326
327 /**
328  * Dump a block of data for a command.
329  *
330  * @param name  Name for data (e.g. 'in', 'out')
331  * @param cmd   Command number associated with data, or -1 for none
332  * @param data  Data block to dump
333  * @param len   Length of data block to dump
334  */
335 void cros_ec_dump_data(const char *name, int cmd, const uint8_t *data, int len);
336
337 /**
338  * Calculate a simple 8-bit checksum of a data block
339  *
340  * @param data  Data block to checksum
341  * @param size  Size of data block in bytes
342  * @return checksum value (0 to 255)
343  */
344 int cros_ec_calc_checksum(const uint8_t *data, int size);
345
346 /**
347  * Decode a flash region parameter
348  *
349  * @param argc  Number of params remaining
350  * @param argv  List of remaining parameters
351  * @return flash region (EC_FLASH_REGION_...) or -1 on error
352  */
353 int cros_ec_decode_region(int argc, char * const argv[]);
354
355 int cros_ec_flash_erase(struct cros_ec_dev *dev, uint32_t offset,
356                 uint32_t size);
357
358 /**
359  * Read data from the flash
360  *
361  * Read an arbitrary amount of data from the EC flash, by repeatedly reading
362  * small blocks.
363  *
364  * The offset starts at 0. You can obtain the region information from
365  * cros_ec_flash_offset() to find out where to read for a particular region.
366  *
367  * @param dev           CROS-EC device
368  * @param data          Pointer to data buffer to read into
369  * @param offset        Offset within flash to read from
370  * @param size          Number of bytes to read
371  * @return 0 if ok, -1 on error
372  */
373 int cros_ec_flash_read(struct cros_ec_dev *dev, uint8_t *data, uint32_t offset,
374                     uint32_t size);
375
376 /**
377  * Write data to the flash
378  *
379  * Write an arbitrary amount of data to the EC flash, by repeatedly writing
380  * small blocks.
381  *
382  * The offset starts at 0. You can obtain the region information from
383  * cros_ec_flash_offset() to find out where to write for a particular region.
384  *
385  * Attempting to write to the region where the EC is currently running from
386  * will result in an error.
387  *
388  * @param dev           CROS-EC device
389  * @param data          Pointer to data buffer to write
390  * @param offset        Offset within flash to write to.
391  * @param size          Number of bytes to write
392  * @return 0 if ok, -1 on error
393  */
394 int cros_ec_flash_write(struct cros_ec_dev *dev, const uint8_t *data,
395                      uint32_t offset, uint32_t size);
396
397 /**
398  * Obtain position and size of a flash region
399  *
400  * @param dev           CROS-EC device
401  * @param region        Flash region to query
402  * @param offset        Returns offset of flash region in EC flash
403  * @param size          Returns size of flash region
404  * @return 0 if ok, -1 on error
405  */
406 int cros_ec_flash_offset(struct cros_ec_dev *dev, enum ec_flash_region region,
407                       uint32_t *offset, uint32_t *size);
408
409 /**
410  * Read/write VbNvContext from/to a CROS-EC device.
411  *
412  * @param dev           CROS-EC device
413  * @param block         Buffer of VbNvContext to be read/write
414  * @return 0 if ok, -1 on error
415  */
416 int cros_ec_read_vbnvcontext(struct cros_ec_dev *dev, uint8_t *block);
417 int cros_ec_write_vbnvcontext(struct cros_ec_dev *dev, const uint8_t *block);
418
419 /**
420  * Read the version information for the EC images
421  *
422  * @param dev           CROS-EC device
423  * @param versionp      This is set to point to the version information
424  * @return 0 if ok, -1 on error
425  */
426 int cros_ec_read_version(struct cros_ec_dev *dev,
427                        struct ec_response_get_version **versionp);
428
429 /**
430  * Read the build information for the EC
431  *
432  * @param dev           CROS-EC device
433  * @param versionp      This is set to point to the build string
434  * @return 0 if ok, -1 on error
435  */
436 int cros_ec_read_build_info(struct cros_ec_dev *dev, char **strp);
437
438 /**
439  * Switch on/off a LDO / FET.
440  *
441  * @param dev           CROS-EC device
442  * @param index         index of the LDO/FET to switch
443  * @param state         new state of the LDO/FET : EC_LDO_STATE_ON|OFF
444  * @return 0 if ok, -1 on error
445  */
446 int cros_ec_set_ldo(struct cros_ec_dev *dev, uint8_t index, uint8_t state);
447
448 /**
449  * Read back a LDO / FET current state.
450  *
451  * @param dev           CROS-EC device
452  * @param index         index of the LDO/FET to switch
453  * @param state         current state of the LDO/FET : EC_LDO_STATE_ON|OFF
454  * @return 0 if ok, -1 on error
455  */
456 int cros_ec_get_ldo(struct cros_ec_dev *dev, uint8_t index, uint8_t *state);
457
458 /**
459  * Initialize the Chrome OS EC at board initialization time.
460  *
461  * @return 0 if ok, -ve on error
462  */
463 int cros_ec_board_init(void);
464
465 /**
466  * Get access to the error reported when cros_ec_board_init() was called
467  *
468  * This permits delayed reporting of the EC error if it failed during
469  * early init.
470  *
471  * @return error (0 if there was no error, -ve if there was an error)
472  */
473 int cros_ec_get_error(void);
474
475 /**
476  * Returns information from the FDT about the Chrome EC flash
477  *
478  * @param blob          FDT blob to use
479  * @param config        Structure to use to return information
480  */
481 int cros_ec_decode_ec_flash(const void *blob, struct fdt_cros_ec *config);
482
483 #endif