1 /* 2 * Chromium OS cros_ec driver - SPI interface 3 * 4 * Copyright (c) 2012 The Chromium OS Authors. 5 * 6 * SPDX-License-Identifier: GPL-2.0+ 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 45 /* 46 * Sanity-check input size to make sure it plus transaction overhead 47 * fits in the internal device buffer. 48 */ 49 if (in_bytes > sizeof(dev->din)) { 50 debug("%s: Cannot receive %d bytes\n", __func__, din_len); 51 return -1; 52 } 53 54 /* We represent message length as a byte */ 55 if (dout_len > 0xff) { 56 debug("%s: Cannot send %d bytes\n", __func__, dout_len); 57 return -1; 58 } 59 60 /* 61 * Clear input buffer so we don't get false hits for MSG_HEADER 62 */ 63 memset(dev->din, '\0', in_bytes); 64 65 if (spi_claim_bus(dev->spi)) { 66 debug("%s: Cannot claim SPI bus\n", __func__); 67 return -1; 68 } 69 70 out = dev->dout; 71 out[0] = cmd_version; 72 out[1] = cmd; 73 out[2] = (uint8_t)dout_len; 74 memcpy(out + 3, dout, dout_len); 75 csum = cros_ec_calc_checksum(out, 3) 76 + cros_ec_calc_checksum(dout, dout_len); 77 out[3 + dout_len] = (uint8_t)csum; 78 79 /* 80 * Send output data and receive input data starting such that the 81 * message body will be dword aligned. 82 */ 83 p = dev->din + sizeof(int64_t) - 2; 84 len = dout_len + 4; 85 cros_ec_dump_data("out", cmd, out, len); 86 rv = spi_xfer(dev->spi, max(len, in_bytes) * 8, out, p, 87 SPI_XFER_BEGIN | SPI_XFER_END); 88 89 spi_release_bus(dev->spi); 90 91 if (rv) { 92 debug("%s: Cannot complete SPI transfer\n", __func__); 93 return -1; 94 } 95 96 len = min(p[1], din_len); 97 cros_ec_dump_data("in", -1, p, len + 3); 98 99 /* Response code is first byte of message */ 100 if (p[0] != EC_RES_SUCCESS) { 101 printf("%s: Returned status %d\n", __func__, p[0]); 102 return -(int)(p[0]); 103 } 104 105 /* Check checksum */ 106 csum = cros_ec_calc_checksum(p, len + 2); 107 if (csum != p[len + 2]) { 108 debug("%s: Invalid checksum rx %#02x, calced %#02x\n", __func__, 109 p[2 + len], csum); 110 return -1; 111 } 112 113 /* Anything else is the response data */ 114 *dinp = p + 2; 115 116 return len; 117 } 118 119 int cros_ec_spi_decode_fdt(struct cros_ec_dev *dev, const void *blob) 120 { 121 /* Decode interface-specific FDT params */ 122 dev->max_frequency = fdtdec_get_int(blob, dev->node, 123 "spi-max-frequency", 500000); 124 dev->cs = fdtdec_get_int(blob, dev->node, "reg", 0); 125 126 return 0; 127 } 128 129 /** 130 * Initialize SPI protocol. 131 * 132 * @param dev CROS_EC device 133 * @param blob Device tree blob 134 * @return 0 if ok, -1 on error 135 */ 136 int cros_ec_spi_init(struct cros_ec_dev *dev, const void *blob) 137 { 138 dev->spi = spi_setup_slave_fdt(blob, dev->parent_node, dev->node); 139 if (!dev->spi) { 140 debug("%s: Could not setup SPI slave\n", __func__); 141 return -1; 142 } 143 144 return 0; 145 } 146