blob: f9bc9750d4cb2e617eab6fb4f4dd126d64c9e2b5 [file] [log] [blame]
Hung-ying Tyan78764a42013-05-15 18:27:29 +08001/*
2 * Chromium OS cros_ec driver - I2C interface
3 *
4 * Copyright (c) 2012 The Chromium OS Authors.
Hung-ying Tyan78764a42013-05-15 18:27:29 +08005 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02006 * SPDX-License-Identifier: GPL-2.0+
Hung-ying Tyan78764a42013-05-15 18:27:29 +08007 */
8
9/*
10 * The Matrix Keyboard Protocol driver handles talking to the keyboard
11 * controller chip. Mostly this is for keyboard functions, but some other
12 * things have slipped in, so we provide generic services to talk to the
13 * KBC.
14 */
15
16#include <common.h>
Simon Glass85df9582015-01-26 20:29:40 -070017#include <dm.h>
Hung-ying Tyan78764a42013-05-15 18:27:29 +080018#include <i2c.h>
19#include <cros_ec.h>
20
21#ifdef DEBUG_TRACE
22#define debug_trace(fmt, b...) debug(fmt, #b)
23#else
24#define debug_trace(fmt, b...)
25#endif
26
Simon Glass85df9582015-01-26 20:29:40 -070027static int cros_ec_i2c_command(struct udevice *udev, uint8_t cmd,
28 int cmd_version, const uint8_t *dout,
29 int dout_len, uint8_t **dinp, int din_len)
Hung-ying Tyan78764a42013-05-15 18:27:29 +080030{
Simon Glass85df9582015-01-26 20:29:40 -070031 struct cros_ec_dev *dev = udev->uclass_priv;
Hung-ying Tyan78764a42013-05-15 18:27:29 +080032 /* version8, cmd8, arglen8, out8[dout_len], csum8 */
33 int out_bytes = dout_len + 4;
34 /* response8, arglen8, in8[din_len], checksum8 */
35 int in_bytes = din_len + 3;
36 uint8_t *ptr;
37 /* Receive input data, so that args will be dword aligned */
38 uint8_t *in_ptr;
Randall Spanglere8c12662014-02-27 13:26:08 -070039 int len, csum, ret;
Hung-ying Tyan78764a42013-05-15 18:27:29 +080040
Hung-ying Tyan78764a42013-05-15 18:27:29 +080041 /*
42 * Sanity-check I/O sizes given transaction overhead in internal
43 * buffers.
44 */
45 if (out_bytes > sizeof(dev->dout)) {
46 debug("%s: Cannot send %d bytes\n", __func__, dout_len);
47 return -1;
48 }
49 if (in_bytes > sizeof(dev->din)) {
50 debug("%s: Cannot receive %d bytes\n", __func__, din_len);
51 return -1;
52 }
53 assert(dout_len >= 0);
54 assert(dinp);
55
56 /*
57 * Copy command and data into output buffer so we can do a single I2C
58 * burst transaction.
59 */
60 ptr = dev->dout;
61
62 /*
63 * in_ptr starts of pointing to a dword-aligned input data buffer.
64 * We decrement it back by the number of header bytes we expect to
65 * receive, so that the first parameter of the resulting input data
66 * will be dword aligned.
67 */
68 in_ptr = dev->din + sizeof(int64_t);
Randall Spanglere8c12662014-02-27 13:26:08 -070069
70 if (dev->protocol_version != 2) {
71 /* Something we don't support */
72 debug("%s: Protocol version %d unsupported\n",
73 __func__, dev->protocol_version);
74 return -1;
Hung-ying Tyan78764a42013-05-15 18:27:29 +080075 }
Randall Spanglere8c12662014-02-27 13:26:08 -070076
77 *ptr++ = EC_CMD_VERSION0 + cmd_version;
78 *ptr++ = cmd;
79 *ptr++ = dout_len;
80 in_ptr -= 2; /* Expect status, length bytes */
81
Hung-ying Tyan78764a42013-05-15 18:27:29 +080082 memcpy(ptr, dout, dout_len);
83 ptr += dout_len;
84
Randall Spanglere8c12662014-02-27 13:26:08 -070085 *ptr++ = (uint8_t)
86 cros_ec_calc_checksum(dev->dout, dout_len + 3);
Hung-ying Tyan78764a42013-05-15 18:27:29 +080087
Hung-ying Tyan78764a42013-05-15 18:27:29 +080088 /* Send output data */
89 cros_ec_dump_data("out", -1, dev->dout, out_bytes);
Simon Glass85df9582015-01-26 20:29:40 -070090 ret = dm_i2c_write(udev, 0, dev->dout, out_bytes);
Hung-ying Tyan78764a42013-05-15 18:27:29 +080091 if (ret) {
Simon Glass85df9582015-01-26 20:29:40 -070092 debug("%s: Cannot complete I2C write to %s\n", __func__,
93 udev->name);
Hung-ying Tyan78764a42013-05-15 18:27:29 +080094 ret = -1;
95 }
96
97 if (!ret) {
Simon Glass85df9582015-01-26 20:29:40 -070098 ret = dm_i2c_read(udev, 0, in_ptr, in_bytes);
Hung-ying Tyan78764a42013-05-15 18:27:29 +080099 if (ret) {
Simon Glass85df9582015-01-26 20:29:40 -0700100 debug("%s: Cannot complete I2C read from %s\n",
101 __func__, udev->name);
Hung-ying Tyan78764a42013-05-15 18:27:29 +0800102 ret = -1;
103 }
104 }
105
Hung-ying Tyan78764a42013-05-15 18:27:29 +0800106 if (*in_ptr != EC_RES_SUCCESS) {
107 debug("%s: Received bad result code %d\n", __func__, *in_ptr);
108 return -(int)*in_ptr;
109 }
110
Randall Spanglere8c12662014-02-27 13:26:08 -0700111 len = in_ptr[1];
112 if (len + 3 > sizeof(dev->din)) {
113 debug("%s: Received length %#02x too large\n",
114 __func__, len);
115 return -1;
Hung-ying Tyan78764a42013-05-15 18:27:29 +0800116 }
Randall Spanglere8c12662014-02-27 13:26:08 -0700117 csum = cros_ec_calc_checksum(in_ptr, 2 + len);
118 if (csum != in_ptr[2 + len]) {
119 debug("%s: Invalid checksum rx %#02x, calced %#02x\n",
120 __func__, in_ptr[2 + din_len], csum);
121 return -1;
122 }
123 din_len = min(din_len, len);
124 cros_ec_dump_data("in", -1, in_ptr, din_len + 3);
Hung-ying Tyan78764a42013-05-15 18:27:29 +0800125
126 /* Return pointer to dword-aligned input data, if any */
127 *dinp = dev->din + sizeof(int64_t);
128
129 return din_len;
130}
131
Simon Glass85df9582015-01-26 20:29:40 -0700132static int cros_ec_probe(struct udevice *dev)
Hung-ying Tyan78764a42013-05-15 18:27:29 +0800133{
Simon Glass85df9582015-01-26 20:29:40 -0700134 return cros_ec_register(dev);
Hung-ying Tyan78764a42013-05-15 18:27:29 +0800135}
136
Simon Glass85df9582015-01-26 20:29:40 -0700137static struct dm_cros_ec_ops cros_ec_ops = {
138 .command = cros_ec_i2c_command,
139};
Hung-ying Tyan78764a42013-05-15 18:27:29 +0800140
Simon Glass85df9582015-01-26 20:29:40 -0700141static const struct udevice_id cros_ec_ids[] = {
142 { .compatible = "google,cros-ec" },
143 { }
144};
145
146U_BOOT_DRIVER(cros_ec_i2c) = {
147 .name = "cros_ec",
148 .id = UCLASS_CROS_EC,
149 .of_match = cros_ec_ids,
150 .probe = cros_ec_probe,
151 .ops = &cros_ec_ops,
152};