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> |
| 18 | #include <spi.h> |
| 19 | |
| 20 | /** |
| 21 | * Send a command to a LPC CROS_EC device and return the reply. |
| 22 | * |
| 23 | * The device's internal input/output buffers are used. |
| 24 | * |
| 25 | * @param dev CROS_EC device |
| 26 | * @param cmd Command to send (EC_CMD_...) |
| 27 | * @param cmd_version Version of command to send (EC_VER_...) |
| 28 | * @param dout Output data (may be NULL If dout_len=0) |
| 29 | * @param dout_len Size of output data in bytes |
| 30 | * @param dinp Returns pointer to response data. This will be |
| 31 | * untouched unless we return a value > 0. |
| 32 | * @param din_len Maximum size of response in bytes |
| 33 | * @return number of bytes in response, or -1 on error |
| 34 | */ |
| 35 | int cros_ec_spi_command(struct cros_ec_dev *dev, uint8_t cmd, int cmd_version, |
| 36 | const uint8_t *dout, int dout_len, |
| 37 | uint8_t **dinp, int din_len) |
| 38 | { |
| 39 | int in_bytes = din_len + 4; /* status, length, checksum, trailer */ |
| 40 | uint8_t *out; |
| 41 | uint8_t *p; |
| 42 | int csum, len; |
| 43 | int rv; |
| 44 | |
Randall Spangler | e8c1266 | 2014-02-27 13:26:08 -0700 | [diff] [blame^] | 45 | if (dev->protocol_version != 2) { |
| 46 | debug("%s: Unsupported EC protcol version %d\n", |
| 47 | __func__, dev->protocol_version); |
| 48 | return -1; |
| 49 | } |
| 50 | |
Hung-ying Tyan | f3424c5 | 2013-05-15 18:27:30 +0800 | [diff] [blame] | 51 | /* |
| 52 | * Sanity-check input size to make sure it plus transaction overhead |
| 53 | * fits in the internal device buffer. |
| 54 | */ |
| 55 | if (in_bytes > sizeof(dev->din)) { |
| 56 | debug("%s: Cannot receive %d bytes\n", __func__, din_len); |
| 57 | return -1; |
| 58 | } |
| 59 | |
| 60 | /* We represent message length as a byte */ |
| 61 | if (dout_len > 0xff) { |
| 62 | debug("%s: Cannot send %d bytes\n", __func__, dout_len); |
| 63 | return -1; |
| 64 | } |
| 65 | |
| 66 | /* |
| 67 | * Clear input buffer so we don't get false hits for MSG_HEADER |
| 68 | */ |
| 69 | memset(dev->din, '\0', in_bytes); |
| 70 | |
| 71 | if (spi_claim_bus(dev->spi)) { |
| 72 | debug("%s: Cannot claim SPI bus\n", __func__); |
| 73 | return -1; |
| 74 | } |
| 75 | |
| 76 | out = dev->dout; |
| 77 | out[0] = cmd_version; |
| 78 | out[1] = cmd; |
| 79 | out[2] = (uint8_t)dout_len; |
| 80 | memcpy(out + 3, dout, dout_len); |
| 81 | csum = cros_ec_calc_checksum(out, 3) |
| 82 | + cros_ec_calc_checksum(dout, dout_len); |
| 83 | out[3 + dout_len] = (uint8_t)csum; |
| 84 | |
| 85 | /* |
| 86 | * Send output data and receive input data starting such that the |
| 87 | * message body will be dword aligned. |
| 88 | */ |
| 89 | p = dev->din + sizeof(int64_t) - 2; |
| 90 | len = dout_len + 4; |
| 91 | cros_ec_dump_data("out", cmd, out, len); |
| 92 | rv = spi_xfer(dev->spi, max(len, in_bytes) * 8, out, p, |
| 93 | SPI_XFER_BEGIN | SPI_XFER_END); |
| 94 | |
| 95 | spi_release_bus(dev->spi); |
| 96 | |
| 97 | if (rv) { |
| 98 | debug("%s: Cannot complete SPI transfer\n", __func__); |
| 99 | return -1; |
| 100 | } |
| 101 | |
| 102 | len = min(p[1], din_len); |
| 103 | cros_ec_dump_data("in", -1, p, len + 3); |
| 104 | |
| 105 | /* Response code is first byte of message */ |
| 106 | if (p[0] != EC_RES_SUCCESS) { |
| 107 | printf("%s: Returned status %d\n", __func__, p[0]); |
| 108 | return -(int)(p[0]); |
| 109 | } |
| 110 | |
| 111 | /* Check checksum */ |
| 112 | csum = cros_ec_calc_checksum(p, len + 2); |
| 113 | if (csum != p[len + 2]) { |
| 114 | debug("%s: Invalid checksum rx %#02x, calced %#02x\n", __func__, |
| 115 | p[2 + len], csum); |
| 116 | return -1; |
| 117 | } |
| 118 | |
| 119 | /* Anything else is the response data */ |
| 120 | *dinp = p + 2; |
| 121 | |
| 122 | return len; |
| 123 | } |
| 124 | |
| 125 | int cros_ec_spi_decode_fdt(struct cros_ec_dev *dev, const void *blob) |
| 126 | { |
| 127 | /* Decode interface-specific FDT params */ |
| 128 | dev->max_frequency = fdtdec_get_int(blob, dev->node, |
| 129 | "spi-max-frequency", 500000); |
| 130 | dev->cs = fdtdec_get_int(blob, dev->node, "reg", 0); |
| 131 | |
| 132 | return 0; |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Initialize SPI protocol. |
| 137 | * |
| 138 | * @param dev CROS_EC device |
| 139 | * @param blob Device tree blob |
| 140 | * @return 0 if ok, -1 on error |
| 141 | */ |
| 142 | int cros_ec_spi_init(struct cros_ec_dev *dev, const void *blob) |
| 143 | { |
Simon Glass | 0efc024 | 2013-12-03 16:43:24 -0700 | [diff] [blame] | 144 | dev->spi = spi_setup_slave_fdt(blob, dev->parent_node, dev->node); |
Hung-ying Tyan | f3424c5 | 2013-05-15 18:27:30 +0800 | [diff] [blame] | 145 | if (!dev->spi) { |
| 146 | debug("%s: Could not setup SPI slave\n", __func__); |
| 147 | return -1; |
| 148 | } |
| 149 | |
| 150 | return 0; |
| 151 | } |