Hung-ying Tyan | f3424c5 | 2013-05-15 18:27:30 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Chromium OS cros_ec driver - SPI interface |
| 3 | * |
| 4 | * Copyright (c) 2012 The Chromium OS Authors. |
Hung-ying Tyan | f3424c5 | 2013-05-15 18:27:30 +0800 | [diff] [blame] | 5 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 6 | * SPDX-License-Identifier: GPL-2.0+ |
Hung-ying Tyan | f3424c5 | 2013-05-15 18:27:30 +0800 | [diff] [blame] | 7 | */ |
| 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> |
| 17 | #include <cros_ec.h> |
Simon Glass | ea0ebc8 | 2014-10-13 23:42:16 -0600 | [diff] [blame^] | 18 | #include <dm.h> |
| 19 | #include <errno.h> |
Hung-ying Tyan | f3424c5 | 2013-05-15 18:27:30 +0800 | [diff] [blame] | 20 | #include <spi.h> |
| 21 | |
Simon Glass | ea0ebc8 | 2014-10-13 23:42:16 -0600 | [diff] [blame^] | 22 | DECLARE_GLOBAL_DATA_PTR; |
| 23 | |
| 24 | #ifdef CONFIG_DM_CROS_EC |
| 25 | int cros_ec_spi_packet(struct udevice *udev, int out_bytes, int in_bytes) |
| 26 | { |
| 27 | struct cros_ec_dev *dev = udev->uclass_priv; |
| 28 | #else |
Randall Spangler | a607028 | 2014-02-27 13:26:10 -0700 | [diff] [blame] | 29 | int cros_ec_spi_packet(struct cros_ec_dev *dev, int out_bytes, int in_bytes) |
| 30 | { |
Simon Glass | ea0ebc8 | 2014-10-13 23:42:16 -0600 | [diff] [blame^] | 31 | #endif |
| 32 | struct spi_slave *slave = dev_get_parentdata(dev->dev); |
Randall Spangler | a607028 | 2014-02-27 13:26:10 -0700 | [diff] [blame] | 33 | int rv; |
| 34 | |
| 35 | /* Do the transfer */ |
Simon Glass | ea0ebc8 | 2014-10-13 23:42:16 -0600 | [diff] [blame^] | 36 | if (spi_claim_bus(slave)) { |
Randall Spangler | a607028 | 2014-02-27 13:26:10 -0700 | [diff] [blame] | 37 | debug("%s: Cannot claim SPI bus\n", __func__); |
| 38 | return -1; |
| 39 | } |
| 40 | |
Simon Glass | ea0ebc8 | 2014-10-13 23:42:16 -0600 | [diff] [blame^] | 41 | rv = spi_xfer(slave, max(out_bytes, in_bytes) * 8, |
Randall Spangler | a607028 | 2014-02-27 13:26:10 -0700 | [diff] [blame] | 42 | dev->dout, dev->din, |
| 43 | SPI_XFER_BEGIN | SPI_XFER_END); |
| 44 | |
Simon Glass | ea0ebc8 | 2014-10-13 23:42:16 -0600 | [diff] [blame^] | 45 | spi_release_bus(slave); |
Randall Spangler | a607028 | 2014-02-27 13:26:10 -0700 | [diff] [blame] | 46 | |
| 47 | if (rv) { |
| 48 | debug("%s: Cannot complete SPI transfer\n", __func__); |
| 49 | return -1; |
| 50 | } |
| 51 | |
| 52 | return in_bytes; |
| 53 | } |
| 54 | |
Hung-ying Tyan | f3424c5 | 2013-05-15 18:27:30 +0800 | [diff] [blame] | 55 | /** |
| 56 | * Send a command to a LPC CROS_EC device and return the reply. |
| 57 | * |
| 58 | * The device's internal input/output buffers are used. |
| 59 | * |
| 60 | * @param dev CROS_EC device |
| 61 | * @param cmd Command to send (EC_CMD_...) |
| 62 | * @param cmd_version Version of command to send (EC_VER_...) |
| 63 | * @param dout Output data (may be NULL If dout_len=0) |
| 64 | * @param dout_len Size of output data in bytes |
| 65 | * @param dinp Returns pointer to response data. This will be |
| 66 | * untouched unless we return a value > 0. |
| 67 | * @param din_len Maximum size of response in bytes |
| 68 | * @return number of bytes in response, or -1 on error |
| 69 | */ |
Simon Glass | ea0ebc8 | 2014-10-13 23:42:16 -0600 | [diff] [blame^] | 70 | #ifdef CONFIG_DM_CROS_EC |
| 71 | int cros_ec_spi_command(struct udevice *udev, uint8_t cmd, int cmd_version, |
| 72 | const uint8_t *dout, int dout_len, |
| 73 | uint8_t **dinp, int din_len) |
| 74 | { |
| 75 | struct cros_ec_dev *dev = udev->uclass_priv; |
| 76 | #else |
Hung-ying Tyan | f3424c5 | 2013-05-15 18:27:30 +0800 | [diff] [blame] | 77 | int cros_ec_spi_command(struct cros_ec_dev *dev, uint8_t cmd, int cmd_version, |
| 78 | const uint8_t *dout, int dout_len, |
| 79 | uint8_t **dinp, int din_len) |
| 80 | { |
Simon Glass | ea0ebc8 | 2014-10-13 23:42:16 -0600 | [diff] [blame^] | 81 | #endif |
| 82 | struct spi_slave *slave = dev_get_parentdata(dev->dev); |
Hung-ying Tyan | f3424c5 | 2013-05-15 18:27:30 +0800 | [diff] [blame] | 83 | int in_bytes = din_len + 4; /* status, length, checksum, trailer */ |
| 84 | uint8_t *out; |
| 85 | uint8_t *p; |
| 86 | int csum, len; |
| 87 | int rv; |
| 88 | |
Randall Spangler | e8c1266 | 2014-02-27 13:26:08 -0700 | [diff] [blame] | 89 | if (dev->protocol_version != 2) { |
| 90 | debug("%s: Unsupported EC protcol version %d\n", |
| 91 | __func__, dev->protocol_version); |
| 92 | return -1; |
| 93 | } |
| 94 | |
Hung-ying Tyan | f3424c5 | 2013-05-15 18:27:30 +0800 | [diff] [blame] | 95 | /* |
| 96 | * Sanity-check input size to make sure it plus transaction overhead |
| 97 | * fits in the internal device buffer. |
| 98 | */ |
| 99 | if (in_bytes > sizeof(dev->din)) { |
| 100 | debug("%s: Cannot receive %d bytes\n", __func__, din_len); |
| 101 | return -1; |
| 102 | } |
| 103 | |
| 104 | /* We represent message length as a byte */ |
| 105 | if (dout_len > 0xff) { |
| 106 | debug("%s: Cannot send %d bytes\n", __func__, dout_len); |
| 107 | return -1; |
| 108 | } |
| 109 | |
| 110 | /* |
| 111 | * Clear input buffer so we don't get false hits for MSG_HEADER |
| 112 | */ |
| 113 | memset(dev->din, '\0', in_bytes); |
| 114 | |
Simon Glass | ea0ebc8 | 2014-10-13 23:42:16 -0600 | [diff] [blame^] | 115 | if (spi_claim_bus(slave)) { |
Hung-ying Tyan | f3424c5 | 2013-05-15 18:27:30 +0800 | [diff] [blame] | 116 | debug("%s: Cannot claim SPI bus\n", __func__); |
| 117 | return -1; |
| 118 | } |
| 119 | |
| 120 | out = dev->dout; |
Simon Glass | 2001b9a | 2014-07-07 10:16:37 -0600 | [diff] [blame] | 121 | out[0] = EC_CMD_VERSION0 + cmd_version; |
Hung-ying Tyan | f3424c5 | 2013-05-15 18:27:30 +0800 | [diff] [blame] | 122 | out[1] = cmd; |
| 123 | out[2] = (uint8_t)dout_len; |
| 124 | memcpy(out + 3, dout, dout_len); |
| 125 | csum = cros_ec_calc_checksum(out, 3) |
| 126 | + cros_ec_calc_checksum(dout, dout_len); |
| 127 | out[3 + dout_len] = (uint8_t)csum; |
| 128 | |
| 129 | /* |
| 130 | * Send output data and receive input data starting such that the |
| 131 | * message body will be dword aligned. |
| 132 | */ |
| 133 | p = dev->din + sizeof(int64_t) - 2; |
| 134 | len = dout_len + 4; |
| 135 | cros_ec_dump_data("out", cmd, out, len); |
Simon Glass | ea0ebc8 | 2014-10-13 23:42:16 -0600 | [diff] [blame^] | 136 | rv = spi_xfer(slave, max(len, in_bytes) * 8, out, p, |
Hung-ying Tyan | f3424c5 | 2013-05-15 18:27:30 +0800 | [diff] [blame] | 137 | SPI_XFER_BEGIN | SPI_XFER_END); |
| 138 | |
Simon Glass | ea0ebc8 | 2014-10-13 23:42:16 -0600 | [diff] [blame^] | 139 | spi_release_bus(slave); |
Hung-ying Tyan | f3424c5 | 2013-05-15 18:27:30 +0800 | [diff] [blame] | 140 | |
| 141 | if (rv) { |
| 142 | debug("%s: Cannot complete SPI transfer\n", __func__); |
| 143 | return -1; |
| 144 | } |
| 145 | |
| 146 | len = min(p[1], din_len); |
| 147 | cros_ec_dump_data("in", -1, p, len + 3); |
| 148 | |
| 149 | /* Response code is first byte of message */ |
| 150 | if (p[0] != EC_RES_SUCCESS) { |
| 151 | printf("%s: Returned status %d\n", __func__, p[0]); |
| 152 | return -(int)(p[0]); |
| 153 | } |
| 154 | |
| 155 | /* Check checksum */ |
| 156 | csum = cros_ec_calc_checksum(p, len + 2); |
| 157 | if (csum != p[len + 2]) { |
| 158 | debug("%s: Invalid checksum rx %#02x, calced %#02x\n", __func__, |
| 159 | p[2 + len], csum); |
| 160 | return -1; |
| 161 | } |
| 162 | |
| 163 | /* Anything else is the response data */ |
| 164 | *dinp = p + 2; |
| 165 | |
| 166 | return len; |
| 167 | } |
| 168 | |
Simon Glass | ea0ebc8 | 2014-10-13 23:42:16 -0600 | [diff] [blame^] | 169 | #ifndef CONFIG_DM_CROS_EC |
Hung-ying Tyan | f3424c5 | 2013-05-15 18:27:30 +0800 | [diff] [blame] | 170 | int cros_ec_spi_decode_fdt(struct cros_ec_dev *dev, const void *blob) |
| 171 | { |
| 172 | /* Decode interface-specific FDT params */ |
| 173 | dev->max_frequency = fdtdec_get_int(blob, dev->node, |
| 174 | "spi-max-frequency", 500000); |
| 175 | dev->cs = fdtdec_get_int(blob, dev->node, "reg", 0); |
| 176 | |
| 177 | return 0; |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Initialize SPI protocol. |
| 182 | * |
| 183 | * @param dev CROS_EC device |
| 184 | * @param blob Device tree blob |
| 185 | * @return 0 if ok, -1 on error |
| 186 | */ |
| 187 | int cros_ec_spi_init(struct cros_ec_dev *dev, const void *blob) |
| 188 | { |
Simon Glass | ea0ebc8 | 2014-10-13 23:42:16 -0600 | [diff] [blame^] | 189 | int ret; |
| 190 | |
| 191 | ret = spi_setup_slave_fdt(blob, dev->node, dev->parent_node, |
| 192 | &slave); |
| 193 | if (ret) { |
Hung-ying Tyan | f3424c5 | 2013-05-15 18:27:30 +0800 | [diff] [blame] | 194 | debug("%s: Could not setup SPI slave\n", __func__); |
Simon Glass | ea0ebc8 | 2014-10-13 23:42:16 -0600 | [diff] [blame^] | 195 | return ret; |
Hung-ying Tyan | f3424c5 | 2013-05-15 18:27:30 +0800 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | return 0; |
| 199 | } |
Simon Glass | ea0ebc8 | 2014-10-13 23:42:16 -0600 | [diff] [blame^] | 200 | #endif |
| 201 | |
| 202 | #ifdef CONFIG_DM_CROS_EC |
| 203 | int cros_ec_probe(struct udevice *dev) |
| 204 | { |
| 205 | struct spi_slave *slave = dev_get_parentdata(dev); |
| 206 | int ret; |
| 207 | |
| 208 | /* |
| 209 | * TODO(sjg@chromium.org) |
| 210 | * |
| 211 | * This is really horrible at present. It is an artifact of removing |
| 212 | * the child_pre_probe() method for SPI. Everything here could go in |
| 213 | * an automatic function, except that spi_get_bus_and_cs() wants to |
| 214 | * set it up manually and call device_probe_child(). |
| 215 | * |
| 216 | * The solution may be to re-enable the child_pre_probe() method for |
| 217 | * SPI and have it do nothing if the child is already passed in via |
| 218 | * device_probe_child(). |
| 219 | */ |
| 220 | slave->dev = dev; |
| 221 | ret = spi_ofdata_to_platdata(gd->fdt_blob, dev->of_offset, slave); |
| 222 | if (ret) |
| 223 | return ret; |
| 224 | return cros_ec_register(dev); |
| 225 | } |
| 226 | |
| 227 | struct dm_cros_ec_ops cros_ec_ops = { |
| 228 | .packet = cros_ec_spi_packet, |
| 229 | .command = cros_ec_spi_command, |
| 230 | }; |
| 231 | |
| 232 | static const struct udevice_id cros_ec_ids[] = { |
| 233 | { .compatible = "google,cros-ec" }, |
| 234 | { } |
| 235 | }; |
| 236 | |
| 237 | U_BOOT_DRIVER(cros_ec_spi) = { |
| 238 | .name = "cros_ec", |
| 239 | .id = UCLASS_CROS_EC, |
| 240 | .of_match = cros_ec_ids, |
| 241 | .probe = cros_ec_probe, |
| 242 | .ops = &cros_ec_ops, |
| 243 | }; |
| 244 | #endif |