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