Coverage Report

Created: 2026-03-11 06:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/u-boot/drivers/misc/cros_ec.c
Line
Count
Source
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
/*
9
 * This is the interface to the Chrome OS EC. It provides keyboard functions,
10
 * power control and battery management. Quite a few other functions are
11
 * provided to enable the EC software to be updated, talk to the EC's I2C bus
12
 * and store a small amount of data in a memory which persists while the EC
13
 * is not reset.
14
 */
15
16
#define LOG_CATEGORY UCLASS_CROS_EC
17
18
#include <command.h>
19
#include <dm.h>
20
#include <i2c.h>
21
#include <cros_ec.h>
22
#include <fdtdec.h>
23
#include <log.h>
24
#include <malloc.h>
25
#include <spi.h>
26
#include <time.h>
27
#include <linux/delay.h>
28
#include <linux/errno.h>
29
#include <asm/io.h>
30
#include <asm-generic/gpio.h>
31
#include <dm/device-internal.h>
32
#include <dm/of_extra.h>
33
#include <dm/uclass-internal.h>
34
35
#ifdef DEBUG_TRACE
36
#define debug_trace(fmt, b...)  debug(fmt, #b)
37
#else
38
#define debug_trace(fmt, b...)
39
#endif
40
41
enum {
42
  /* Timeout waiting for a flash erase command to complete */
43
  CROS_EC_CMD_TIMEOUT_MS  = 5000,
44
  /* Timeout waiting for a synchronous hash to be recomputed */
45
  CROS_EC_CMD_HASH_TIMEOUT_MS = 2000,
46
47
  /* Wait 10 ms between attempts to check if EC's hash is ready */
48
  CROS_EC_HASH_CHECK_DELAY_MS = 10,
49
50
};
51
52
0
#define INVALID_HCMD 0xFF
53
54
/*
55
 * Map UHEPI masks to non UHEPI commands in order to support old EC FW
56
 * which does not support UHEPI command.
57
 */
58
static const struct {
59
  u8 set_cmd;
60
  u8 clear_cmd;
61
  u8 get_cmd;
62
} event_map[] = {
63
  [EC_HOST_EVENT_MAIN] = {
64
    INVALID_HCMD, EC_CMD_HOST_EVENT_CLEAR,
65
    INVALID_HCMD,
66
  },
67
  [EC_HOST_EVENT_B] = {
68
    INVALID_HCMD, EC_CMD_HOST_EVENT_CLEAR_B,
69
    EC_CMD_HOST_EVENT_GET_B,
70
  },
71
  [EC_HOST_EVENT_SCI_MASK] = {
72
    EC_CMD_HOST_EVENT_SET_SCI_MASK, INVALID_HCMD,
73
    EC_CMD_HOST_EVENT_GET_SCI_MASK,
74
  },
75
  [EC_HOST_EVENT_SMI_MASK] = {
76
    EC_CMD_HOST_EVENT_SET_SMI_MASK, INVALID_HCMD,
77
    EC_CMD_HOST_EVENT_GET_SMI_MASK,
78
  },
79
  [EC_HOST_EVENT_ALWAYS_REPORT_MASK] = {
80
    INVALID_HCMD, INVALID_HCMD, INVALID_HCMD,
81
  },
82
  [EC_HOST_EVENT_ACTIVE_WAKE_MASK] = {
83
    EC_CMD_HOST_EVENT_SET_WAKE_MASK, INVALID_HCMD,
84
    EC_CMD_HOST_EVENT_GET_WAKE_MASK,
85
  },
86
  [EC_HOST_EVENT_LAZY_WAKE_MASK_S0IX] = {
87
    EC_CMD_HOST_EVENT_SET_WAKE_MASK, INVALID_HCMD,
88
    EC_CMD_HOST_EVENT_GET_WAKE_MASK,
89
  },
90
  [EC_HOST_EVENT_LAZY_WAKE_MASK_S3] = {
91
    EC_CMD_HOST_EVENT_SET_WAKE_MASK, INVALID_HCMD,
92
    EC_CMD_HOST_EVENT_GET_WAKE_MASK,
93
  },
94
  [EC_HOST_EVENT_LAZY_WAKE_MASK_S5] = {
95
    EC_CMD_HOST_EVENT_SET_WAKE_MASK, INVALID_HCMD,
96
    EC_CMD_HOST_EVENT_GET_WAKE_MASK,
97
  },
98
};
99
100
void cros_ec_dump_data(const char *name, int cmd, const uint8_t *data, int len)
101
0
{
102
#ifdef DEBUG
103
  int i;
104
105
  printf("%s: ", name);
106
  if (cmd != -1)
107
    printf("cmd=%#x: ", cmd);
108
  for (i = 0; i < len; i++)
109
    printf("%02x ", data[i]);
110
  printf("\n");
111
#endif
112
0
}
113
114
/*
115
 * Calculate a simple 8-bit checksum of a data block
116
 *
117
 * @param data  Data block to checksum
118
 * @param size  Size of data block in bytes
119
 * Return: checksum value (0 to 255)
120
 */
121
int cros_ec_calc_checksum(const uint8_t *data, int size)
122
0
{
123
0
  int csum, i;
124
125
0
  for (i = csum = 0; i < size; i++)
126
0
    csum += data[i];
127
0
  return csum & 0xff;
128
0
}
129
130
/**
131
 * Create a request packet for protocol version 3.
132
 *
133
 * The packet is stored in the device's internal output buffer.
134
 *
135
 * @param dev   CROS-EC device
136
 * @param cmd   Command to send (EC_CMD_...)
137
 * @param cmd_version Version of command to send (EC_VER_...)
138
 * @param dout          Output data (may be NULL If dout_len=0)
139
 * @param dout_len      Size of output data in bytes
140
 * Return: packet size in bytes, or <0 if error.
141
 */
142
static int create_proto3_request(struct cros_ec_dev *cdev,
143
         int cmd, int cmd_version,
144
         const void *dout, int dout_len)
145
0
{
146
0
  struct ec_host_request *rq = (struct ec_host_request *)cdev->dout;
147
0
  int out_bytes = dout_len + sizeof(*rq);
148
149
  /* Fail if output size is too big */
150
0
  if (out_bytes > (int)sizeof(cdev->dout)) {
151
0
    debug("%s: Cannot send %d bytes\n", __func__, dout_len);
152
0
    return -EC_RES_REQUEST_TRUNCATED;
153
0
  }
154
155
  /* Fill in request packet */
156
0
  rq->struct_version = EC_HOST_REQUEST_VERSION;
157
0
  rq->checksum = 0;
158
0
  rq->command = cmd;
159
0
  rq->command_version = cmd_version;
160
0
  rq->reserved = 0;
161
0
  rq->data_len = dout_len;
162
163
  /* Copy data after header */
164
0
  memcpy(rq + 1, dout, dout_len);
165
166
  /* Write checksum field so the entire packet sums to 0 */
167
0
  rq->checksum = (uint8_t)(-cros_ec_calc_checksum(cdev->dout, out_bytes));
168
169
0
  cros_ec_dump_data("out", cmd, cdev->dout, out_bytes);
170
171
  /* Return size of request packet */
172
0
  return out_bytes;
173
0
}
174
175
/**
176
 * Prepare the device to receive a protocol version 3 response.
177
 *
178
 * @param dev   CROS-EC device
179
 * @param din_len       Maximum size of response in bytes
180
 * Return: maximum expected number of bytes in response, or <0 if error.
181
 */
182
static int prepare_proto3_response_buffer(struct cros_ec_dev *cdev, int din_len)
183
0
{
184
0
  int in_bytes = din_len + sizeof(struct ec_host_response);
185
186
  /* Fail if input size is too big */
187
0
  if (in_bytes > (int)sizeof(cdev->din)) {
188
0
    debug("%s: Cannot receive %d bytes\n", __func__, din_len);
189
0
    return -EC_RES_RESPONSE_TOO_BIG;
190
0
  }
191
192
  /* Return expected size of response packet */
193
0
  return in_bytes;
194
0
}
195
196
/**
197
 * Handle a protocol version 3 response packet.
198
 *
199
 * The packet must already be stored in the device's internal input buffer.
200
 *
201
 * @param dev   CROS-EC device
202
 * @param dinp          Returns pointer to response data
203
 * @param din_len       Maximum size of response in bytes
204
 * Return: number of bytes of response data, or <0 if error. Note that error
205
 * codes can be from errno.h or -ve EC_RES_INVALID_CHECKSUM values (and they
206
 * overlap!)
207
 */
208
static int handle_proto3_response(struct cros_ec_dev *dev,
209
          uint8_t **dinp, int din_len)
210
0
{
211
0
  struct ec_host_response *rs = (struct ec_host_response *)dev->din;
212
0
  int in_bytes;
213
0
  int csum;
214
215
0
  cros_ec_dump_data("in-header", -1, dev->din, sizeof(*rs));
216
217
  /* Check input data */
218
0
  if (rs->struct_version != EC_HOST_RESPONSE_VERSION) {
219
0
    debug("%s: EC response version mismatch\n", __func__);
220
0
    return -EC_RES_INVALID_RESPONSE;
221
0
  }
222
223
0
  if (rs->reserved) {
224
0
    debug("%s: EC response reserved != 0\n", __func__);
225
0
    return -EC_RES_INVALID_RESPONSE;
226
0
  }
227
228
0
  if (rs->data_len > din_len) {
229
0
    debug("%s: EC returned too much data\n", __func__);
230
0
    return -EC_RES_RESPONSE_TOO_BIG;
231
0
  }
232
233
0
  cros_ec_dump_data("in-data", -1, dev->din + sizeof(*rs), rs->data_len);
234
235
  /* Update in_bytes to actual data size */
236
0
  in_bytes = sizeof(*rs) + rs->data_len;
237
238
  /* Verify checksum */
239
0
  csum = cros_ec_calc_checksum(dev->din, in_bytes);
240
0
  if (csum) {
241
0
    debug("%s: EC response checksum invalid: 0x%02x\n", __func__,
242
0
          csum);
243
0
    return -EC_RES_INVALID_CHECKSUM;
244
0
  }
245
246
  /* Return error result, if any */
247
0
  if (rs->result)
248
0
    return -(int)rs->result;
249
250
  /* If we're still here, set response data pointer and return length */
251
0
  *dinp = (uint8_t *)(rs + 1);
252
253
0
  return rs->data_len;
254
0
}
255
256
static int send_command_proto3(struct cros_ec_dev *cdev,
257
             int cmd, int cmd_version,
258
             const void *dout, int dout_len,
259
             uint8_t **dinp, int din_len)
260
0
{
261
0
  struct dm_cros_ec_ops *ops;
262
0
  int out_bytes, in_bytes;
263
0
  int rv;
264
265
  /* Create request packet */
266
0
  out_bytes = create_proto3_request(cdev, cmd, cmd_version,
267
0
            dout, dout_len);
268
0
  if (out_bytes < 0)
269
0
    return out_bytes;
270
271
  /* Prepare response buffer */
272
0
  in_bytes = prepare_proto3_response_buffer(cdev, din_len);
273
0
  if (in_bytes < 0)
274
0
    return in_bytes;
275
276
0
  ops = dm_cros_ec_get_ops(cdev->dev);
277
0
  rv = ops->packet ? ops->packet(cdev->dev, out_bytes, in_bytes) :
278
0
      -ENOSYS;
279
0
  if (rv < 0)
280
0
    return rv;
281
282
  /* Process the response */
283
0
  return handle_proto3_response(cdev, dinp, din_len);
284
0
}
285
286
static int send_command(struct cros_ec_dev *dev, uint cmd, int cmd_version,
287
      const void *dout, int dout_len,
288
      uint8_t **dinp, int din_len)
289
0
{
290
0
  struct dm_cros_ec_ops *ops;
291
0
  int ret = -1;
292
293
  /* Handle protocol version 3 support */
294
0
  if (dev->protocol_version == 3) {
295
0
    return send_command_proto3(dev, cmd, cmd_version,
296
0
             dout, dout_len, dinp, din_len);
297
0
  }
298
299
0
  ops = dm_cros_ec_get_ops(dev->dev);
300
0
  ret = ops->command(dev->dev, cmd, cmd_version,
301
0
         (const uint8_t *)dout, dout_len, dinp, din_len);
302
303
0
  return ret;
304
0
}
305
306
/**
307
 * Send a command to the CROS-EC device and return the reply.
308
 *
309
 * The device's internal input/output buffers are used.
310
 *
311
 * @param dev   CROS-EC device
312
 * @param cmd   Command to send (EC_CMD_...)
313
 * @param cmd_version Version of command to send (EC_VER_...)
314
 * @param dout          Output data (may be NULL If dout_len=0)
315
 * @param dout_len      Size of output data in bytes
316
 * @param dinp          Response data (may be NULL If din_len=0).
317
 *      If not NULL, it will be updated to point to the data
318
 *      and will always be double word aligned (64-bits)
319
 * @param din_len       Maximum size of response in bytes
320
 * Return: number of bytes in response, or -ve on error
321
 */
322
static int ec_command_inptr(struct udevice *dev, uint cmd,
323
          int cmd_version, const void *dout, int dout_len,
324
          uint8_t **dinp, int din_len)
325
0
{
326
0
  struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
327
0
  uint8_t *din = NULL;
328
0
  int len;
329
330
0
  len = send_command(cdev, cmd, cmd_version, dout, dout_len, &din,
331
0
         din_len);
332
333
  /* If the command doesn't complete, wait a while */
334
0
  if (len == -EC_RES_IN_PROGRESS) {
335
0
    struct ec_response_get_comms_status *resp = NULL;
336
0
    ulong start;
337
338
    /* Wait for command to complete */
339
0
    start = get_timer(0);
340
0
    do {
341
0
      int ret;
342
343
0
      mdelay(50); /* Insert some reasonable delay */
344
0
      ret = send_command(cdev, EC_CMD_GET_COMMS_STATUS, 0,
345
0
             NULL, 0,
346
0
             (uint8_t **)&resp, sizeof(*resp));
347
0
      if (ret < 0)
348
0
        return ret;
349
350
0
      if (get_timer(start) > CROS_EC_CMD_TIMEOUT_MS) {
351
0
        debug("%s: Command %#02x timeout\n",
352
0
              __func__, cmd);
353
0
        return -EC_RES_TIMEOUT;
354
0
      }
355
0
    } while (resp->flags & EC_COMMS_STATUS_PROCESSING);
356
357
    /* OK it completed, so read the status response */
358
    /* not sure why it was 0 for the last argument */
359
0
    len = send_command(cdev, EC_CMD_RESEND_RESPONSE, 0, NULL, 0,
360
0
           &din, din_len);
361
0
  }
362
363
0
  debug("%s: len=%d, din=%p\n", __func__, len, din);
364
0
  if (dinp) {
365
    /* If we have any data to return, it must be 64bit-aligned */
366
0
    assert(len <= 0 || !((uintptr_t)din & 7));
367
0
    *dinp = din;
368
0
  }
369
370
0
  return len;
371
0
}
372
373
/**
374
 * Send a command to the CROS-EC device and return the reply.
375
 *
376
 * The device's internal input/output buffers are used.
377
 *
378
 * @param dev   CROS-EC device
379
 * @param cmd   Command to send (EC_CMD_...)
380
 * @param cmd_version Version of command to send (EC_VER_...)
381
 * @param dout          Output data (may be NULL If dout_len=0)
382
 * @param dout_len      Size of output data in bytes
383
 * @param din           Response data (may be NULL If din_len=0).
384
 *      It not NULL, it is a place for ec_command() to copy the
385
 *      data to.
386
 * @param din_len       Maximum size of response in bytes
387
 * Return: number of bytes in response, or -ve on error
388
 */
389
static int ec_command(struct udevice *dev, uint cmd, int cmd_version,
390
          const void *dout, int dout_len,
391
          void *din, int din_len)
392
0
{
393
0
  uint8_t *in_buffer;
394
0
  int len;
395
396
0
  assert((din_len == 0) || din);
397
0
  len = ec_command_inptr(dev, cmd, cmd_version, dout, dout_len,
398
0
             &in_buffer, din_len);
399
0
  if (len > 0) {
400
    /*
401
     * If we were asked to put it somewhere, do so, otherwise just
402
     * disregard the result.
403
     */
404
0
    if (din && in_buffer) {
405
0
      assert(len <= din_len);
406
0
      if (len > din_len)
407
0
        return -ENOSPC;
408
0
      memmove(din, in_buffer, len);
409
0
    }
410
0
  }
411
0
  return len;
412
0
}
413
414
int cros_ec_scan_keyboard(struct udevice *dev, struct mbkp_keyscan *scan)
415
0
{
416
0
  if (ec_command(dev, EC_CMD_MKBP_STATE, 0, NULL, 0, scan,
417
0
           sizeof(scan->data)) != sizeof(scan->data))
418
0
    return -1;
419
420
0
  return 0;
421
0
}
422
423
int cros_ec_get_next_event(struct udevice *dev,
424
         struct ec_response_get_next_event *event)
425
0
{
426
0
  int ret;
427
428
0
  ret = ec_command(dev, EC_CMD_GET_NEXT_EVENT, 0, NULL, 0,
429
0
       event, sizeof(*event));
430
0
  if (ret < 0)
431
0
    return ret;
432
0
  else if (ret != sizeof(*event))
433
0
    return -EC_RES_INVALID_RESPONSE;
434
435
0
  return 0;
436
0
}
437
438
int cros_ec_read_id(struct udevice *dev, char *id, int maxlen)
439
0
{
440
0
  struct ec_response_get_version *r;
441
0
  int ret;
442
443
0
  ret = ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
444
0
             (uint8_t **)&r, sizeof(*r));
445
0
  if (ret != sizeof(*r)) {
446
0
    log_err("Got rc %d, expected %u\n", ret, (uint)sizeof(*r));
447
0
    return -1;
448
0
  }
449
450
0
  if (maxlen > (int)sizeof(r->version_string_ro))
451
0
    maxlen = sizeof(r->version_string_ro);
452
453
0
  switch (r->current_image) {
454
0
  case EC_IMAGE_RO:
455
0
    memcpy(id, r->version_string_ro, maxlen);
456
0
    break;
457
0
  case EC_IMAGE_RW:
458
0
    memcpy(id, r->version_string_rw, maxlen);
459
0
    break;
460
0
  default:
461
0
    log_err("Invalid EC image %d\n", r->current_image);
462
0
    return -1;
463
0
  }
464
465
0
  id[maxlen - 1] = '\0';
466
0
  return 0;
467
0
}
468
469
int cros_ec_read_version(struct udevice *dev,
470
       struct ec_response_get_version **versionp)
471
0
{
472
0
  if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
473
0
      (uint8_t **)versionp, sizeof(**versionp))
474
0
      != sizeof(**versionp))
475
0
    return -1;
476
477
0
  return 0;
478
0
}
479
480
int cros_ec_read_build_info(struct udevice *dev, char **strp)
481
0
{
482
0
  if (ec_command_inptr(dev, EC_CMD_GET_BUILD_INFO, 0, NULL, 0,
483
0
      (uint8_t **)strp, EC_PROTO2_MAX_PARAM_SIZE) < 0)
484
0
    return -1;
485
486
0
  return 0;
487
0
}
488
489
int cros_ec_read_current_image(struct udevice *dev,
490
             enum ec_current_image *image)
491
0
{
492
0
  struct ec_response_get_version *r;
493
494
0
  if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
495
0
      (uint8_t **)&r, sizeof(*r)) != sizeof(*r))
496
0
    return -1;
497
498
0
  *image = r->current_image;
499
0
  return 0;
500
0
}
501
502
static int cros_ec_wait_on_hash_done(struct udevice *dev,
503
             struct ec_params_vboot_hash *p,
504
             struct ec_response_vboot_hash *hash)
505
0
{
506
0
  ulong start;
507
508
0
  start = get_timer(0);
509
0
  while (hash->status == EC_VBOOT_HASH_STATUS_BUSY) {
510
0
    mdelay(CROS_EC_HASH_CHECK_DELAY_MS);
511
512
0
    p->cmd = EC_VBOOT_HASH_GET;
513
514
0
    if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, p, sizeof(*p), hash,
515
0
             sizeof(*hash)) < 0)
516
0
      return -1;
517
518
0
    if (get_timer(start) > CROS_EC_CMD_HASH_TIMEOUT_MS) {
519
0
      debug("%s: EC_VBOOT_HASH_GET timeout\n", __func__);
520
0
      return -EC_RES_TIMEOUT;
521
0
    }
522
0
  }
523
0
  return 0;
524
0
}
525
526
int cros_ec_read_hash(struct udevice *dev, uint hash_offset,
527
          struct ec_response_vboot_hash *hash)
528
0
{
529
0
  struct ec_params_vboot_hash p;
530
0
  int rv;
531
532
0
  p.cmd = EC_VBOOT_HASH_GET;
533
0
  p.offset = hash_offset;
534
0
  if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
535
0
           hash, sizeof(*hash)) < 0)
536
0
    return -1;
537
538
  /* If the EC is busy calculating the hash, fidget until it's done. */
539
0
  rv = cros_ec_wait_on_hash_done(dev, &p, hash);
540
0
  if (rv)
541
0
    return rv;
542
543
  /* If the hash is valid, we're done. Otherwise, we have to kick it off
544
   * again and wait for it to complete. Note that we explicitly assume
545
   * that hashing zero bytes is always wrong, even though that would
546
   * produce a valid hash value. */
547
0
  if (hash->status == EC_VBOOT_HASH_STATUS_DONE && hash->size)
548
0
    return 0;
549
550
0
  debug("%s: No valid hash (status=%d size=%d). Compute one...\n",
551
0
        __func__, hash->status, hash->size);
552
553
0
  p.cmd = EC_VBOOT_HASH_START;
554
0
  p.hash_type = EC_VBOOT_HASH_TYPE_SHA256;
555
0
  p.nonce_size = 0;
556
0
  p.offset = hash_offset;
557
558
0
  if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
559
0
           hash, sizeof(*hash)) < 0)
560
0
    return -1;
561
562
0
  rv = cros_ec_wait_on_hash_done(dev, &p, hash);
563
0
  if (rv)
564
0
    return rv;
565
0
  if (hash->status != EC_VBOOT_HASH_STATUS_DONE) {
566
0
    log_err("Hash did not complete, status=%d\n", hash->status);
567
0
    return -EIO;
568
0
  }
569
570
0
  debug("%s: hash done\n", __func__);
571
572
0
  return 0;
573
0
}
574
575
static int cros_ec_invalidate_hash(struct udevice *dev)
576
0
{
577
0
  struct ec_params_vboot_hash p;
578
0
  struct ec_response_vboot_hash *hash;
579
580
  /* We don't have an explict command for the EC to discard its current
581
   * hash value, so we'll just tell it to calculate one that we know is
582
   * wrong (we claim that hashing zero bytes is always invalid).
583
   */
584
0
  p.cmd = EC_VBOOT_HASH_RECALC;
585
0
  p.hash_type = EC_VBOOT_HASH_TYPE_SHA256;
586
0
  p.nonce_size = 0;
587
0
  p.offset = 0;
588
0
  p.size = 0;
589
590
0
  debug("%s:\n", __func__);
591
592
0
  if (ec_command_inptr(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
593
0
           (uint8_t **)&hash, sizeof(*hash)) < 0)
594
0
    return -1;
595
596
  /* No need to wait for it to finish */
597
0
  return 0;
598
0
}
599
600
int cros_ec_hello(struct udevice *dev, uint *handshakep)
601
0
{
602
0
  struct ec_params_hello req;
603
0
  struct ec_response_hello *resp;
604
605
0
  req.in_data = 0x12345678;
606
0
  if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req),
607
0
           (uint8_t **)&resp, sizeof(*resp)) < 0)
608
0
    return -EIO;
609
0
  if (resp->out_data != req.in_data + 0x01020304) {
610
0
    printf("Received invalid handshake %x\n", resp->out_data);
611
0
    if (handshakep)
612
0
      *handshakep = req.in_data;
613
0
    return -ENOTSYNC;
614
0
  }
615
616
0
  return 0;
617
0
}
618
619
int cros_ec_reboot(struct udevice *dev, enum ec_reboot_cmd cmd, uint8_t flags)
620
0
{
621
0
  struct ec_params_reboot_ec p;
622
623
0
  p.cmd = cmd;
624
0
  p.flags = flags;
625
626
0
  if (ec_command_inptr(dev, EC_CMD_REBOOT_EC, 0, &p, sizeof(p), NULL, 0)
627
0
      < 0)
628
0
    return -1;
629
630
0
  if (!(flags & EC_REBOOT_FLAG_ON_AP_SHUTDOWN)) {
631
0
    ulong start;
632
633
    /*
634
     * EC reboot will take place immediately so delay to allow it
635
     * to complete.  Note that some reboot types (EC_REBOOT_COLD)
636
     * will reboot the AP as well, in which case we won't actually
637
     * get to this point.
638
     */
639
0
    mdelay(50);
640
0
    start = get_timer(0);
641
0
    while (cros_ec_hello(dev, NULL)) {
642
0
      if (get_timer(start) > 3000) {
643
0
        log_err("EC did not return from reboot\n");
644
0
        return -ETIMEDOUT;
645
0
      }
646
0
      mdelay(5);
647
0
    }
648
0
  }
649
650
0
  return 0;
651
0
}
652
653
int cros_ec_interrupt_pending(struct udevice *dev)
654
0
{
655
0
  struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
656
657
  /* no interrupt support : always poll */
658
0
  if (!dm_gpio_is_valid(&cdev->ec_int))
659
0
    return -ENOENT;
660
661
0
  return dm_gpio_get_value(&cdev->ec_int);
662
0
}
663
664
int cros_ec_info(struct udevice *dev, struct ec_response_mkbp_info *info)
665
0
{
666
0
  if (ec_command(dev, EC_CMD_MKBP_INFO, 0, NULL, 0, info,
667
0
           sizeof(*info)) != sizeof(*info))
668
0
    return -1;
669
670
0
  return 0;
671
0
}
672
673
int cros_ec_get_event_mask(struct udevice *dev, uint type, uint32_t *mask)
674
0
{
675
0
  struct ec_response_host_event_mask rsp;
676
0
  int ret;
677
678
0
  ret = ec_command(dev, type, 0, NULL, 0, &rsp, sizeof(rsp));
679
0
  if (ret < 0)
680
0
    return ret;
681
0
  else if (ret != sizeof(rsp))
682
0
    return -EINVAL;
683
684
0
  *mask = rsp.mask;
685
686
0
  return 0;
687
0
}
688
689
int cros_ec_set_event_mask(struct udevice *dev, uint type, uint32_t mask)
690
0
{
691
0
  struct ec_params_host_event_mask req;
692
0
  int ret;
693
694
0
  req.mask = mask;
695
696
0
  ret = ec_command(dev, type, 0, &req, sizeof(req), NULL, 0);
697
0
  if (ret < 0)
698
0
    return ret;
699
700
0
  return 0;
701
0
}
702
703
int cros_ec_get_host_events(struct udevice *dev, uint32_t *events_ptr)
704
0
{
705
0
  struct ec_response_host_event_mask *resp;
706
707
  /*
708
   * Use the B copy of the event flags, because the main copy is already
709
   * used by ACPI/SMI.
710
   */
711
0
  if (ec_command_inptr(dev, EC_CMD_HOST_EVENT_GET_B, 0, NULL, 0,
712
0
           (uint8_t **)&resp, sizeof(*resp)) < (int)sizeof(*resp))
713
0
    return -1;
714
715
0
  if (resp->mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_INVALID))
716
0
    return -1;
717
718
0
  *events_ptr = resp->mask;
719
0
  return 0;
720
0
}
721
722
int cros_ec_clear_host_events(struct udevice *dev, uint32_t events)
723
0
{
724
0
  struct ec_params_host_event_mask params;
725
726
0
  params.mask = events;
727
728
  /*
729
   * Use the B copy of the event flags, so it affects the data returned
730
   * by cros_ec_get_host_events().
731
   */
732
0
  if (ec_command_inptr(dev, EC_CMD_HOST_EVENT_CLEAR_B, 0,
733
0
           &params, sizeof(params), NULL, 0) < 0)
734
0
    return -1;
735
736
0
  return 0;
737
0
}
738
739
int cros_ec_flash_protect(struct udevice *dev, uint32_t set_mask,
740
        uint32_t set_flags,
741
        struct ec_response_flash_protect *resp)
742
0
{
743
0
  struct ec_params_flash_protect params;
744
745
0
  params.mask = set_mask;
746
0
  params.flags = set_flags;
747
748
0
  if (ec_command(dev, EC_CMD_FLASH_PROTECT, EC_VER_FLASH_PROTECT,
749
0
           &params, sizeof(params),
750
0
           resp, sizeof(*resp)) != sizeof(*resp))
751
0
    return -1;
752
753
0
  return 0;
754
0
}
755
756
static int cros_ec_check_version(struct udevice *dev)
757
0
{
758
0
  struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
759
0
  struct ec_params_hello req;
760
761
0
  struct dm_cros_ec_ops *ops;
762
0
  int ret;
763
764
0
  ops = dm_cros_ec_get_ops(dev);
765
0
  if (ops->check_version) {
766
0
    ret = ops->check_version(dev);
767
0
    if (ret)
768
0
      return ret;
769
0
  }
770
771
  /*
772
   * TODO(sjg@chromium.org).
773
   * There is a strange oddity here with the EC. We could just ignore
774
   * the response, i.e. pass the last two parameters as NULL and 0.
775
   * In this case we won't read back very many bytes from the EC.
776
   * On the I2C bus the EC gets upset about this and will try to send
777
   * the bytes anyway. This means that we will have to wait for that
778
   * to complete before continuing with a new EC command.
779
   *
780
   * This problem is probably unique to the I2C bus.
781
   *
782
   * So for now, just read all the data anyway.
783
   */
784
785
  /* Try sending a version 3 packet */
786
0
  cdev->protocol_version = 3;
787
0
  req.in_data = 0;
788
0
  ret = cros_ec_hello(dev, NULL);
789
0
  if (!ret || ret == -ENOTSYNC)
790
0
    return 0;
791
792
  /* Try sending a version 2 packet */
793
0
  cdev->protocol_version = 2;
794
0
  ret = cros_ec_hello(dev, NULL);
795
0
  if (!ret || ret == -ENOTSYNC)
796
0
    return 0;
797
798
  /*
799
   * Fail if we're still here, since the EC doesn't understand any
800
   * protcol version we speak.  Version 1 interface without command
801
   * version is no longer supported, and we don't know about any new
802
   * protocol versions.
803
   */
804
0
  cdev->protocol_version = 0;
805
0
  printf("%s: ERROR: old EC interface not supported\n", __func__);
806
0
  return -1;
807
0
}
808
809
int cros_ec_test(struct udevice *dev)
810
0
{
811
0
  uint out_data;
812
0
  int ret;
813
814
0
  ret = cros_ec_hello(dev, &out_data);
815
0
  if (ret == -ENOTSYNC) {
816
0
    printf("Received invalid handshake %x\n", out_data);
817
0
    return ret;
818
0
  } else if (ret) {
819
0
    printf("ec_command_inptr() returned error\n");
820
0
    return ret;
821
0
  }
822
823
0
  return 0;
824
0
}
825
826
int cros_ec_flash_offset(struct udevice *dev, enum ec_flash_region region,
827
          uint32_t *offset, uint32_t *size)
828
0
{
829
0
  struct ec_params_flash_region_info p;
830
0
  struct ec_response_flash_region_info *r;
831
0
  int ret;
832
833
0
  p.region = region;
834
0
  ret = ec_command_inptr(dev, EC_CMD_FLASH_REGION_INFO,
835
0
       EC_VER_FLASH_REGION_INFO,
836
0
       &p, sizeof(p), (uint8_t **)&r, sizeof(*r));
837
0
  if (ret != sizeof(*r))
838
0
    return -1;
839
840
0
  if (offset)
841
0
    *offset = r->offset;
842
0
  if (size)
843
0
    *size = r->size;
844
845
0
  return 0;
846
0
}
847
848
int cros_ec_flash_erase(struct udevice *dev, uint32_t offset, uint32_t size)
849
0
{
850
0
  struct ec_params_flash_erase p;
851
852
0
  p.offset = offset;
853
0
  p.size = size;
854
0
  return ec_command_inptr(dev, EC_CMD_FLASH_ERASE, 0, &p, sizeof(p),
855
0
      NULL, 0);
856
0
}
857
858
/**
859
 * Write a single block to the flash
860
 *
861
 * Write a block of data to the EC flash. The size must not exceed the flash
862
 * write block size which you can obtain from cros_ec_flash_write_burst_size().
863
 *
864
 * The offset starts at 0. You can obtain the region information from
865
 * cros_ec_flash_offset() to find out where to write for a particular region.
866
 *
867
 * Attempting to write to the region where the EC is currently running from
868
 * will result in an error.
869
 *
870
 * @param dev   CROS-EC device
871
 * @param data    Pointer to data buffer to write
872
 * @param offset  Offset within flash to write to.
873
 * @param size    Number of bytes to write
874
 * Return: 0 if ok, -1 on error
875
 */
876
static int cros_ec_flash_write_block(struct udevice *dev, const uint8_t *data,
877
             uint32_t offset, uint32_t size)
878
0
{
879
0
  struct ec_params_flash_write *p;
880
0
  int ret;
881
882
0
  p = malloc(sizeof(*p) + size);
883
0
  if (!p)
884
0
    return -ENOMEM;
885
886
0
  p->offset = offset;
887
0
  p->size = size;
888
0
  assert(data && p->size <= EC_FLASH_WRITE_VER0_SIZE);
889
0
  memcpy(p + 1, data, p->size);
890
891
0
  ret = ec_command_inptr(dev, EC_CMD_FLASH_WRITE, 0,
892
0
        p, sizeof(*p) + size, NULL, 0) >= 0 ? 0 : -1;
893
894
0
  free(p);
895
896
0
  return ret;
897
0
}
898
899
/**
900
 * Return optimal flash write burst size
901
 */
902
static int cros_ec_flash_write_burst_size(struct udevice *dev)
903
0
{
904
0
  return EC_FLASH_WRITE_VER0_SIZE;
905
0
}
906
907
/**
908
 * Check if a block of data is erased (all 0xff)
909
 *
910
 * This function is useful when dealing with flash, for checking whether a
911
 * data block is erased and thus does not need to be programmed.
912
 *
913
 * @param data    Pointer to data to check (must be word-aligned)
914
 * @param size    Number of bytes to check (must be word-aligned)
915
 * Return: 0 if erased, non-zero if any word is not erased
916
 */
917
static int cros_ec_data_is_erased(const uint32_t *data, int size)
918
0
{
919
0
  assert(!(size & 3));
920
0
  size /= sizeof(uint32_t);
921
0
  for (; size > 0; size -= 4, data++)
922
0
    if (*data != -1U)
923
0
      return 0;
924
925
0
  return 1;
926
0
}
927
928
/**
929
 * Read back flash parameters
930
 *
931
 * This function reads back parameters of the flash as reported by the EC
932
 *
933
 * @param dev  Pointer to device
934
 * @param info Pointer to output flash info struct
935
 */
936
int cros_ec_read_flashinfo(struct udevice *dev,
937
         struct ec_response_flash_info *info)
938
0
{
939
0
  int ret;
940
941
0
  ret = ec_command(dev, EC_CMD_FLASH_INFO, 0,
942
0
       NULL, 0, info, sizeof(*info));
943
0
  if (ret < 0)
944
0
    return ret;
945
946
0
  return ret < sizeof(*info) ? -1 : 0;
947
0
}
948
949
int cros_ec_flash_write(struct udevice *dev, const uint8_t *data,
950
      uint32_t offset, uint32_t size)
951
0
{
952
0
  struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
953
0
  uint32_t burst = cros_ec_flash_write_burst_size(dev);
954
0
  uint32_t end, off;
955
0
  int ret;
956
957
0
  if (!burst)
958
0
    return -EINVAL;
959
960
  /*
961
   * TODO: round up to the nearest multiple of write size.  Can get away
962
   * without that on link right now because its write size is 4 bytes.
963
   */
964
0
  end = offset + size;
965
0
  for (off = offset; off < end; off += burst, data += burst) {
966
0
    uint32_t todo;
967
968
    /* If the data is empty, there is no point in programming it */
969
0
    todo = min(end - off, burst);
970
0
    if (cdev->optimise_flash_write &&
971
0
        cros_ec_data_is_erased((uint32_t *)data, todo))
972
0
      continue;
973
974
0
    ret = cros_ec_flash_write_block(dev, data, off, todo);
975
0
    if (ret)
976
0
      return ret;
977
0
  }
978
979
0
  return 0;
980
0
}
981
982
/**
983
 * Run verification on a slot
984
 *
985
 * @param me     CrosEc instance
986
 * @param region Region to run verification on
987
 * Return: 0 if success or not applicable. Non-zero if verification failed.
988
 */
989
int cros_ec_efs_verify(struct udevice *dev, enum ec_flash_region region)
990
0
{
991
0
  struct ec_params_efs_verify p;
992
0
  int rv;
993
994
0
  log_info("EFS: EC is verifying updated image...\n");
995
0
  p.region = region;
996
997
0
  rv = ec_command(dev, EC_CMD_EFS_VERIFY, 0, &p, sizeof(p), NULL, 0);
998
0
  if (rv >= 0) {
999
0
    log_info("EFS: Verification success\n");
1000
0
    return 0;
1001
0
  }
1002
0
  if (rv == -EC_RES_INVALID_COMMAND) {
1003
0
    log_info("EFS: EC doesn't support EFS_VERIFY command\n");
1004
0
    return 0;
1005
0
  }
1006
0
  log_info("EFS: Verification failed\n");
1007
1008
0
  return rv;
1009
0
}
1010
1011
/**
1012
 * Read a single block from the flash
1013
 *
1014
 * Read a block of data from the EC flash. The size must not exceed the flash
1015
 * write block size which you can obtain from cros_ec_flash_write_burst_size().
1016
 *
1017
 * The offset starts at 0. You can obtain the region information from
1018
 * cros_ec_flash_offset() to find out where to read for a particular region.
1019
 *
1020
 * @param dev   CROS-EC device
1021
 * @param data    Pointer to data buffer to read into
1022
 * @param offset  Offset within flash to read from
1023
 * @param size    Number of bytes to read
1024
 * Return: 0 if ok, -1 on error
1025
 */
1026
static int cros_ec_flash_read_block(struct udevice *dev, uint8_t *data,
1027
            uint32_t offset, uint32_t size)
1028
0
{
1029
0
  struct ec_params_flash_read p;
1030
1031
0
  p.offset = offset;
1032
0
  p.size = size;
1033
1034
0
  return ec_command(dev, EC_CMD_FLASH_READ, 0,
1035
0
        &p, sizeof(p), data, size) >= 0 ? 0 : -1;
1036
0
}
1037
1038
int cros_ec_flash_read(struct udevice *dev, uint8_t *data, uint32_t offset,
1039
           uint32_t size)
1040
0
{
1041
0
  uint32_t burst = cros_ec_flash_write_burst_size(dev);
1042
0
  uint32_t end, off;
1043
0
  int ret;
1044
1045
0
  end = offset + size;
1046
0
  for (off = offset; off < end; off += burst, data += burst) {
1047
0
    ret = cros_ec_flash_read_block(dev, data, off,
1048
0
              min(end - off, burst));
1049
0
    if (ret)
1050
0
      return ret;
1051
0
  }
1052
1053
0
  return 0;
1054
0
}
1055
1056
int cros_ec_flash_update_rw(struct udevice *dev, const uint8_t *image,
1057
          int image_size)
1058
0
{
1059
0
  uint32_t rw_offset, rw_size;
1060
0
  int ret;
1061
1062
0
  if (cros_ec_flash_offset(dev, EC_FLASH_REGION_ACTIVE, &rw_offset,
1063
0
    &rw_size))
1064
0
    return -1;
1065
0
  if (image_size > (int)rw_size)
1066
0
    return -1;
1067
1068
  /* Invalidate the existing hash, just in case the AP reboots
1069
   * unexpectedly during the update. If that happened, the EC RW firmware
1070
   * would be invalid, but the EC would still have the original hash.
1071
   */
1072
0
  ret = cros_ec_invalidate_hash(dev);
1073
0
  if (ret)
1074
0
    return ret;
1075
1076
  /*
1077
   * Erase the entire RW section, so that the EC doesn't see any garbage
1078
   * past the new image if it's smaller than the current image.
1079
   *
1080
   * TODO: could optimize this to erase just the current image, since
1081
   * presumably everything past that is 0xff's.  But would still need to
1082
   * round up to the nearest multiple of erase size.
1083
   */
1084
0
  ret = cros_ec_flash_erase(dev, rw_offset, rw_size);
1085
0
  if (ret)
1086
0
    return ret;
1087
1088
  /* Write the image */
1089
0
  ret = cros_ec_flash_write(dev, image, rw_offset, image_size);
1090
0
  if (ret)
1091
0
    return ret;
1092
1093
0
  return 0;
1094
0
}
1095
1096
int cros_ec_get_sku_id(struct udevice *dev)
1097
0
{
1098
0
  struct ec_sku_id_info *r;
1099
0
  int ret;
1100
1101
0
  ret = ec_command_inptr(dev, EC_CMD_GET_SKU_ID, 0, NULL, 0,
1102
0
             (uint8_t **)&r, sizeof(*r));
1103
0
  if (ret != sizeof(*r)) {
1104
0
    if (ret >= 0)
1105
0
      ret = -EIO;
1106
0
    return ret;
1107
0
  }
1108
1109
0
  return r->sku_id;
1110
0
}
1111
1112
int cros_ec_read_nvdata(struct udevice *dev, uint8_t *block, int size)
1113
0
{
1114
0
  struct ec_params_vbnvcontext p;
1115
0
  int len;
1116
1117
0
  if (size != EC_VBNV_BLOCK_SIZE && size != EC_VBNV_BLOCK_SIZE_V2)
1118
0
    return -EINVAL;
1119
1120
0
  p.op = EC_VBNV_CONTEXT_OP_READ;
1121
1122
0
  len = ec_command(dev, EC_CMD_VBNV_CONTEXT, EC_VER_VBNV_CONTEXT,
1123
0
       &p, sizeof(uint32_t) + size, block, size);
1124
0
  if (len != size) {
1125
0
    log_err("Expected %d bytes, got %d\n", size, len);
1126
0
    return -EIO;
1127
0
  }
1128
1129
0
  return 0;
1130
0
}
1131
1132
int cros_ec_write_nvdata(struct udevice *dev, const uint8_t *block, int size)
1133
0
{
1134
0
  struct ec_params_vbnvcontext p;
1135
0
  int len;
1136
1137
0
  if (size != EC_VBNV_BLOCK_SIZE && size != EC_VBNV_BLOCK_SIZE_V2)
1138
0
    return -EINVAL;
1139
0
  p.op = EC_VBNV_CONTEXT_OP_WRITE;
1140
0
  memcpy(p.block, block, size);
1141
1142
0
  len = ec_command_inptr(dev, EC_CMD_VBNV_CONTEXT, EC_VER_VBNV_CONTEXT,
1143
0
      &p, sizeof(uint32_t) + size, NULL, 0);
1144
0
  if (len < 0)
1145
0
    return -1;
1146
1147
0
  return 0;
1148
0
}
1149
1150
int cros_ec_battery_cutoff(struct udevice *dev, uint8_t flags)
1151
0
{
1152
0
  struct ec_params_battery_cutoff p;
1153
0
  int len;
1154
1155
0
  p.flags = flags;
1156
0
  len = ec_command(dev, EC_CMD_BATTERY_CUT_OFF, 1, &p, sizeof(p),
1157
0
       NULL, 0);
1158
1159
0
  if (len < 0)
1160
0
    return -1;
1161
0
  return 0;
1162
0
}
1163
1164
int cros_ec_set_pwm_duty(struct udevice *dev, uint8_t index, uint16_t duty)
1165
0
{
1166
0
  struct ec_params_pwm_set_duty p;
1167
0
  int ret;
1168
1169
0
  p.duty = duty;
1170
0
  p.pwm_type = EC_PWM_TYPE_GENERIC;
1171
0
  p.index = index;
1172
1173
0
  ret = ec_command(dev, EC_CMD_PWM_SET_DUTY, 0, &p, sizeof(p),
1174
0
       NULL, 0);
1175
0
  if (ret < 0)
1176
0
    return ret;
1177
1178
0
  return 0;
1179
0
}
1180
1181
int cros_ec_set_ldo(struct udevice *dev, uint8_t index, uint8_t state)
1182
0
{
1183
0
  struct ec_params_ldo_set params;
1184
1185
0
  params.index = index;
1186
0
  params.state = state;
1187
1188
0
  if (ec_command_inptr(dev, EC_CMD_LDO_SET, 0, &params, sizeof(params),
1189
0
           NULL, 0))
1190
0
    return -1;
1191
1192
0
  return 0;
1193
0
}
1194
1195
int cros_ec_get_ldo(struct udevice *dev, uint8_t index, uint8_t *state)
1196
0
{
1197
0
  struct ec_params_ldo_get params;
1198
0
  struct ec_response_ldo_get *resp;
1199
1200
0
  params.index = index;
1201
1202
0
  if (ec_command_inptr(dev, EC_CMD_LDO_GET, 0, &params, sizeof(params),
1203
0
           (uint8_t **)&resp, sizeof(*resp)) !=
1204
0
           sizeof(*resp))
1205
0
    return -1;
1206
1207
0
  *state = resp->state;
1208
1209
0
  return 0;
1210
0
}
1211
1212
int cros_ec_register(struct udevice *dev)
1213
0
{
1214
0
  struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
1215
0
  char id[MSG_BYTES];
1216
1217
0
  cdev->dev = dev;
1218
0
  gpio_request_by_name(dev, "ec-interrupt", 0, &cdev->ec_int,
1219
0
           GPIOD_IS_IN);
1220
0
  cdev->optimise_flash_write = dev_read_bool(dev, "optimise-flash-write");
1221
1222
0
  if (cros_ec_check_version(dev)) {
1223
0
    debug("%s: Could not detect CROS-EC version\n", __func__);
1224
0
    return -CROS_EC_ERR_CHECK_VERSION;
1225
0
  }
1226
1227
0
  if (cros_ec_read_id(dev, id, sizeof(id))) {
1228
0
    debug("%s: Could not read KBC ID\n", __func__);
1229
0
    return -CROS_EC_ERR_READ_ID;
1230
0
  }
1231
1232
  /* Remember this device for use by the cros_ec command */
1233
0
  debug("Google Chrome EC v%d CROS-EC driver ready, id '%s'\n",
1234
0
        cdev->protocol_version, id);
1235
1236
0
  return 0;
1237
0
}
1238
1239
int cros_ec_decode_ec_flash(struct udevice *dev, struct fdt_cros_ec *config)
1240
0
{
1241
0
  ofnode flash_node, node;
1242
1243
0
  flash_node = dev_read_subnode(dev, "flash");
1244
0
  if (!ofnode_valid(flash_node)) {
1245
0
    debug("Failed to find flash node\n");
1246
0
    return -1;
1247
0
  }
1248
1249
0
  if (ofnode_read_fmap_entry(flash_node,  &config->flash)) {
1250
0
    debug("Failed to decode flash node in chrome-ec\n");
1251
0
    return -1;
1252
0
  }
1253
1254
0
  config->flash_erase_value = ofnode_read_s32_default(flash_node,
1255
0
                  "erase-value", -1);
1256
0
  ofnode_for_each_subnode(node, flash_node) {
1257
0
    const char *name = ofnode_get_name(node);
1258
0
    enum ec_flash_region region;
1259
1260
0
    if (0 == strcmp(name, "ro")) {
1261
0
      region = EC_FLASH_REGION_RO;
1262
0
    } else if (0 == strcmp(name, "rw")) {
1263
0
      region = EC_FLASH_REGION_ACTIVE;
1264
0
    } else if (0 == strcmp(name, "wp-ro")) {
1265
0
      region = EC_FLASH_REGION_WP_RO;
1266
0
    } else {
1267
0
      debug("Unknown EC flash region name '%s'\n", name);
1268
0
      return -1;
1269
0
    }
1270
1271
0
    if (ofnode_read_fmap_entry(node, &config->region[region])) {
1272
0
      debug("Failed to decode flash region in chrome-ec'\n");
1273
0
      return -1;
1274
0
    }
1275
0
  }
1276
1277
0
  return 0;
1278
0
}
1279
1280
int cros_ec_i2c_tunnel(struct udevice *dev, int port, struct i2c_msg *in,
1281
           int nmsgs)
1282
0
{
1283
0
  union {
1284
0
    struct ec_params_i2c_passthru p;
1285
0
    uint8_t outbuf[EC_PROTO2_MAX_PARAM_SIZE];
1286
0
  } params;
1287
0
  union {
1288
0
    struct ec_response_i2c_passthru r;
1289
0
    uint8_t inbuf[EC_PROTO2_MAX_PARAM_SIZE];
1290
0
  } response;
1291
0
  struct ec_params_i2c_passthru *p = &params.p;
1292
0
  struct ec_response_i2c_passthru *r = &response.r;
1293
0
  struct ec_params_i2c_passthru_msg *msg;
1294
0
  uint8_t *pdata, *read_ptr = NULL;
1295
0
  int read_len;
1296
0
  int size;
1297
0
  int rv;
1298
0
  int i;
1299
1300
0
  p->port = port;
1301
1302
0
  p->num_msgs = nmsgs;
1303
0
  size = sizeof(*p) + p->num_msgs * sizeof(*msg);
1304
1305
  /* Create a message to write the register address and optional data */
1306
0
  pdata = (uint8_t *)p + size;
1307
1308
0
  read_len = 0;
1309
0
  for (i = 0, msg = p->msg; i < nmsgs; i++, msg++, in++) {
1310
0
    bool is_read = in->flags & I2C_M_RD;
1311
1312
0
    msg->addr_flags = in->addr;
1313
0
    msg->len = in->len;
1314
0
    if (is_read) {
1315
0
      msg->addr_flags |= EC_I2C_FLAG_READ;
1316
0
      read_len += in->len;
1317
0
      read_ptr = in->buf;
1318
0
      if (sizeof(*r) + read_len > sizeof(response)) {
1319
0
        puts("Read length too big for buffer\n");
1320
0
        return -1;
1321
0
      }
1322
0
    } else {
1323
0
      if (pdata - (uint8_t *)p + in->len > sizeof(params)) {
1324
0
        puts("Params too large for buffer\n");
1325
0
        return -1;
1326
0
      }
1327
0
      memcpy(pdata, in->buf, in->len);
1328
0
      pdata += in->len;
1329
0
    }
1330
0
  }
1331
1332
0
  rv = ec_command(dev, EC_CMD_I2C_PASSTHRU, 0, p, pdata - (uint8_t *)p,
1333
0
      r, sizeof(*r) + read_len);
1334
0
  if (rv < 0)
1335
0
    return rv;
1336
1337
  /* Parse response */
1338
0
  if (r->i2c_status & EC_I2C_STATUS_ERROR) {
1339
0
    printf("Transfer failed with status=0x%x\n", r->i2c_status);
1340
0
    return -1;
1341
0
  }
1342
1343
0
  if (rv < sizeof(*r) + read_len) {
1344
0
    puts("Truncated read response\n");
1345
0
    return -1;
1346
0
  }
1347
1348
  /* We only support a single read message for each transfer */
1349
0
  if (read_len)
1350
0
    memcpy(read_ptr, r->data, read_len);
1351
1352
0
  return 0;
1353
0
}
1354
1355
int cros_ec_get_features(struct udevice *dev, u64 *featuresp)
1356
0
{
1357
0
  struct ec_response_get_features r;
1358
0
  int rv;
1359
1360
0
  rv = ec_command(dev, EC_CMD_GET_FEATURES, 0, NULL, 0, &r, sizeof(r));
1361
0
  if (rv != sizeof(r))
1362
0
    return -EIO;
1363
0
  *featuresp = r.flags[0] | (u64)r.flags[1] << 32;
1364
1365
0
  return 0;
1366
0
}
1367
1368
int cros_ec_check_feature(struct udevice *dev, uint feature)
1369
0
{
1370
0
  struct ec_response_get_features r;
1371
0
  int rv;
1372
1373
0
  rv = ec_command(dev, EC_CMD_GET_FEATURES, 0, NULL, 0, &r, sizeof(r));
1374
0
  if (rv != sizeof(r))
1375
0
    return -EIO;
1376
1377
0
  if (feature >= 8 * sizeof(r.flags))
1378
0
    return -EINVAL;
1379
1380
0
  return r.flags[feature / 32] & EC_FEATURE_MASK_0(feature) ? true :
1381
0
     false;
1382
0
}
1383
1384
/*
1385
 * Query the EC for specified mask indicating enabled events.
1386
 * The EC maintains separate event masks for SMI, SCI and WAKE.
1387
 */
1388
static int cros_ec_uhepi_cmd(struct udevice *dev, uint mask, uint action,
1389
           uint64_t *value)
1390
0
{
1391
0
  int ret;
1392
0
  struct ec_params_host_event req;
1393
0
  struct ec_response_host_event rsp;
1394
1395
0
  req.action = action;
1396
0
  req.mask_type = mask;
1397
0
  if (action != EC_HOST_EVENT_GET)
1398
0
    req.value = *value;
1399
0
  else
1400
0
    *value = 0;
1401
0
  ret = ec_command(dev, EC_CMD_HOST_EVENT, 0, &req, sizeof(req), &rsp,
1402
0
       sizeof(rsp));
1403
1404
0
  if (action != EC_HOST_EVENT_GET)
1405
0
    return ret;
1406
0
  if (ret == 0)
1407
0
    *value = rsp.value;
1408
1409
0
  return ret;
1410
0
}
1411
1412
static int cros_ec_handle_non_uhepi_cmd(struct udevice *dev, uint hcmd,
1413
          uint action, uint64_t *value)
1414
0
{
1415
0
  int ret = -1;
1416
0
  struct ec_params_host_event_mask req;
1417
0
  struct ec_response_host_event_mask rsp;
1418
1419
0
  if (hcmd == INVALID_HCMD)
1420
0
    return ret;
1421
1422
0
  if (action != EC_HOST_EVENT_GET)
1423
0
    req.mask = (uint32_t)*value;
1424
0
  else
1425
0
    *value = 0;
1426
1427
0
  ret = ec_command(dev, hcmd, 0, &req, sizeof(req), &rsp, sizeof(rsp));
1428
0
  if (action != EC_HOST_EVENT_GET)
1429
0
    return ret;
1430
0
  if (ret == 0)
1431
0
    *value = rsp.mask;
1432
1433
0
  return ret;
1434
0
}
1435
1436
bool cros_ec_is_uhepi_supported(struct udevice *dev)
1437
0
{
1438
0
#define UHEPI_SUPPORTED 1
1439
0
#define UHEPI_NOT_SUPPORTED 2
1440
0
  static int uhepi_support;
1441
1442
0
  if (!uhepi_support) {
1443
0
    uhepi_support = cros_ec_check_feature(dev,
1444
0
      EC_FEATURE_UNIFIED_WAKE_MASKS) > 0 ? UHEPI_SUPPORTED :
1445
0
      UHEPI_NOT_SUPPORTED;
1446
0
    log_debug("Chrome EC: UHEPI %s\n",
1447
0
        uhepi_support == UHEPI_SUPPORTED ? "supported" :
1448
0
        "not supported");
1449
0
  }
1450
0
  return uhepi_support == UHEPI_SUPPORTED;
1451
0
}
1452
1453
static int cros_ec_get_mask(struct udevice *dev, uint type)
1454
{
1455
  u64 value = 0;
1456
1457
  if (cros_ec_is_uhepi_supported(dev)) {
1458
    cros_ec_uhepi_cmd(dev, type, EC_HOST_EVENT_GET, &value);
1459
  } else {
1460
    assert(type < ARRAY_SIZE(event_map));
1461
    cros_ec_handle_non_uhepi_cmd(dev, event_map[type].get_cmd,
1462
               EC_HOST_EVENT_GET, &value);
1463
  }
1464
  return value;
1465
}
1466
1467
static int cros_ec_clear_mask(struct udevice *dev, uint type, u64 mask)
1468
{
1469
  if (cros_ec_is_uhepi_supported(dev))
1470
    return cros_ec_uhepi_cmd(dev, type, EC_HOST_EVENT_CLEAR, &mask);
1471
1472
  assert(type < ARRAY_SIZE(event_map));
1473
1474
  return cros_ec_handle_non_uhepi_cmd(dev, event_map[type].clear_cmd,
1475
              EC_HOST_EVENT_CLEAR, &mask);
1476
}
1477
1478
uint64_t cros_ec_get_events_b(struct udevice *dev)
1479
0
{
1480
0
  return cros_ec_get_mask(dev, EC_HOST_EVENT_B);
1481
0
}
1482
1483
int cros_ec_clear_events_b(struct udevice *dev, uint64_t mask)
1484
0
{
1485
0
  log_debug("Chrome EC: clear events_b mask to 0x%016llx\n", mask);
1486
1487
0
  return cros_ec_clear_mask(dev, EC_HOST_EVENT_B, mask);
1488
0
}
1489
1490
int cros_ec_read_limit_power(struct udevice *dev, int *limit_powerp)
1491
0
{
1492
0
  struct ec_params_charge_state p;
1493
0
  struct ec_response_charge_state r;
1494
0
  int ret;
1495
1496
0
  p.cmd = CHARGE_STATE_CMD_GET_PARAM;
1497
0
  p.get_param.param = CS_PARAM_LIMIT_POWER;
1498
0
  ret = ec_command(dev, EC_CMD_CHARGE_STATE, 0, &p, sizeof(p),
1499
0
       &r, sizeof(r));
1500
1501
  /*
1502
   * If our EC doesn't support the LIMIT_POWER parameter, assume that
1503
   * LIMIT_POWER is not requested.
1504
   */
1505
0
  if (ret == -EC_RES_INVALID_PARAM || ret == -EC_RES_INVALID_COMMAND) {
1506
0
    log_warning("PARAM_LIMIT_POWER not supported by EC\n");
1507
0
    return -ENOSYS;
1508
0
  }
1509
1510
0
  if (ret != sizeof(r.get_param))
1511
0
    return -EINVAL;
1512
1513
0
  *limit_powerp = r.get_param.value;
1514
0
  return 0;
1515
0
}
1516
1517
int cros_ec_config_powerbtn(struct udevice *dev, uint32_t flags)
1518
0
{
1519
0
  struct ec_params_config_power_button params;
1520
0
  int ret;
1521
1522
0
  params.flags = flags;
1523
0
  ret = ec_command(dev, EC_CMD_CONFIG_POWER_BUTTON, 0,
1524
0
       &params, sizeof(params), NULL, 0);
1525
0
  if (ret < 0)
1526
0
    return ret;
1527
1528
0
  return 0;
1529
0
}
1530
1531
int cros_ec_get_lid_shutdown_mask(struct udevice *dev)
1532
0
{
1533
0
  u32 mask;
1534
0
  int ret;
1535
1536
0
  ret = cros_ec_get_event_mask(dev, EC_CMD_HOST_EVENT_GET_SMI_MASK,
1537
0
             &mask);
1538
0
  if (ret < 0)
1539
0
    return ret;
1540
1541
0
  return !!(mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_LID_CLOSED));
1542
0
}
1543
1544
int cros_ec_set_lid_shutdown_mask(struct udevice *dev, int enable)
1545
0
{
1546
0
  u32 mask;
1547
0
  int ret;
1548
1549
0
  ret = cros_ec_get_event_mask(dev, EC_CMD_HOST_EVENT_GET_SMI_MASK,
1550
0
             &mask);
1551
0
  if (ret < 0)
1552
0
    return ret;
1553
1554
  /* Set lid close event state in the EC SMI event mask */
1555
0
  if (enable)
1556
0
    mask |= EC_HOST_EVENT_MASK(EC_HOST_EVENT_LID_CLOSED);
1557
0
  else
1558
0
    mask &= ~EC_HOST_EVENT_MASK(EC_HOST_EVENT_LID_CLOSED);
1559
1560
0
  ret = cros_ec_set_event_mask(dev, EC_CMD_HOST_EVENT_SET_SMI_MASK, mask);
1561
0
  if (ret < 0)
1562
0
    return ret;
1563
1564
0
  printf("EC: %sabled lid close event\n", enable ? "en" : "dis");
1565
0
  return 0;
1566
0
}
1567
1568
int cros_ec_vstore_supported(struct udevice *dev)
1569
0
{
1570
0
  return cros_ec_check_feature(dev, EC_FEATURE_VSTORE);
1571
0
}
1572
1573
int cros_ec_vstore_info(struct udevice *dev, u32 *lockedp)
1574
0
{
1575
0
  struct ec_response_vstore_info *resp;
1576
1577
0
  if (ec_command_inptr(dev, EC_CMD_VSTORE_INFO, 0, NULL, 0,
1578
0
           (uint8_t **)&resp, sizeof(*resp)) != sizeof(*resp))
1579
0
    return -EIO;
1580
1581
0
  if (lockedp)
1582
0
    *lockedp = resp->slot_locked;
1583
1584
0
  return resp->slot_count;
1585
0
}
1586
1587
/*
1588
 * cros_ec_vstore_read - Read data from EC vstore slot
1589
 *
1590
 * @slot: vstore slot to read from
1591
 * @data: buffer to store read data, must be EC_VSTORE_SLOT_SIZE bytes
1592
 */
1593
int cros_ec_vstore_read(struct udevice *dev, int slot, uint8_t *data)
1594
0
{
1595
0
  struct ec_params_vstore_read req;
1596
0
  struct ec_response_vstore_read *resp;
1597
1598
0
  req.slot = slot;
1599
0
  if (ec_command_inptr(dev, EC_CMD_VSTORE_READ, 0, &req, sizeof(req),
1600
0
           (uint8_t **)&resp, sizeof(*resp)) != sizeof(*resp))
1601
0
    return -EIO;
1602
1603
0
  if (!data || req.slot >= EC_VSTORE_SLOT_MAX)
1604
0
    return -EINVAL;
1605
1606
0
  memcpy(data, resp->data, sizeof(resp->data));
1607
1608
0
  return 0;
1609
0
}
1610
1611
/*
1612
 * cros_ec_vstore_write - Save data into EC vstore slot
1613
 *
1614
 * @slot: vstore slot to write into
1615
 * @data: data to write
1616
 * @size: size of data in bytes
1617
 *
1618
 * Maximum size of data is EC_VSTORE_SLOT_SIZE.  It is the callers
1619
 * responsibility to check the number of implemented slots by
1620
 * querying the vstore info.
1621
 */
1622
int cros_ec_vstore_write(struct udevice *dev, int slot, const uint8_t *data,
1623
       size_t size)
1624
0
{
1625
0
  struct ec_params_vstore_write req;
1626
1627
0
  if (slot >= EC_VSTORE_SLOT_MAX || size > EC_VSTORE_SLOT_SIZE)
1628
0
    return -EINVAL;
1629
1630
0
  req.slot = slot;
1631
0
  memcpy(req.data, data, size);
1632
1633
0
  if (ec_command(dev, EC_CMD_VSTORE_WRITE, 0, &req, sizeof(req), NULL, 0))
1634
0
    return -EIO;
1635
1636
0
  return 0;
1637
0
}
1638
1639
int cros_ec_get_switches(struct udevice *dev)
1640
0
{
1641
0
  struct dm_cros_ec_ops *ops;
1642
0
  int ret;
1643
1644
0
  ops = dm_cros_ec_get_ops(dev);
1645
0
  if (!ops->get_switches)
1646
0
    return -ENOSYS;
1647
1648
0
  ret = ops->get_switches(dev);
1649
0
  if (ret < 0)
1650
0
    return log_msg_ret("get", ret);
1651
1652
0
  return ret;
1653
0
}
1654
1655
int cros_ec_read_batt_charge(struct udevice *dev, uint *chargep)
1656
0
{
1657
0
  struct ec_params_charge_state req;
1658
0
  struct ec_response_charge_state resp;
1659
0
  int ret;
1660
1661
0
  req.cmd = CHARGE_STATE_CMD_GET_STATE;
1662
0
  ret = ec_command(dev, EC_CMD_CHARGE_STATE, 0, &req, sizeof(req),
1663
0
       &resp, sizeof(resp));
1664
0
  if (ret)
1665
0
    return log_msg_ret("read", ret);
1666
1667
0
  *chargep = resp.get_state.batt_state_of_charge;
1668
1669
0
  return 0;
1670
0
}
1671
1672
UCLASS_DRIVER(cros_ec) = {
1673
  .id   = UCLASS_CROS_EC,
1674
  .name   = "cros-ec",
1675
  .per_device_auto  = sizeof(struct cros_ec_dev),
1676
#if CONFIG_IS_ENABLED(OF_REAL)
1677
  .post_bind  = dm_scan_fdt_dev,
1678
#endif
1679
  .flags    = DM_UC_FLAG_ALLOC_PRIV_DMA,
1680
};