Simon Glass | a17d94f | 2013-02-25 14:08:39 -0800 | [diff] [blame] | 1 | /* |
| 2 | * ChromeOS EC multi-function device (SPI) |
| 3 | * |
| 4 | * Copyright (C) 2012 Google, Inc |
| 5 | * |
| 6 | * This software is licensed under the terms of the GNU General Public |
| 7 | * License version 2, as published by the Free Software Foundation, and |
| 8 | * may be copied, distributed, and modified under those terms. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | */ |
| 15 | |
| 16 | #include <linux/delay.h> |
| 17 | #include <linux/kernel.h> |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/mfd/cros_ec.h> |
| 20 | #include <linux/mfd/cros_ec_commands.h> |
Rhyland Klein | 01e73c8 | 2013-12-09 12:36:09 +0100 | [diff] [blame] | 21 | #include <linux/of.h> |
Simon Glass | a17d94f | 2013-02-25 14:08:39 -0800 | [diff] [blame] | 22 | #include <linux/platform_device.h> |
| 23 | #include <linux/slab.h> |
| 24 | #include <linux/spi/spi.h> |
| 25 | |
| 26 | |
| 27 | /* The header byte, which follows the preamble */ |
| 28 | #define EC_MSG_HEADER 0xec |
| 29 | |
| 30 | /* |
| 31 | * Number of EC preamble bytes we read at a time. Since it takes |
| 32 | * about 400-500us for the EC to respond there is not a lot of |
| 33 | * point in tuning this. If the EC could respond faster then |
| 34 | * we could increase this so that might expect the preamble and |
| 35 | * message to occur in a single transaction. However, the maximum |
| 36 | * SPI transfer size is 256 bytes, so at 5MHz we need a response |
| 37 | * time of perhaps <320us (200 bytes / 1600 bits). |
| 38 | */ |
| 39 | #define EC_MSG_PREAMBLE_COUNT 32 |
| 40 | |
| 41 | /* |
Doug Anderson | 9c0b54a | 2014-04-30 10:44:07 -0700 | [diff] [blame] | 42 | * Allow for a long time for the EC to respond. We support i2c |
| 43 | * tunneling and support fairly long messages for the tunnel (249 |
| 44 | * bytes long at the moment). If we're talking to a 100 kHz device |
| 45 | * on the other end and need to transfer ~256 bytes, then we need: |
| 46 | * 10 us/bit * ~10 bits/byte * ~256 bytes = ~25ms |
| 47 | * |
| 48 | * We'll wait 4 times that to handle clock stretching and other |
| 49 | * paranoia. |
| 50 | * |
| 51 | * It's pretty unlikely that we'll really see a 249 byte tunnel in |
| 52 | * anything other than testing. If this was more common we might |
| 53 | * consider having slow commands like this require a GET_STATUS |
| 54 | * wait loop. The 'flash write' command would be another candidate |
| 55 | * for this, clocking in at 2-3ms. |
| 56 | */ |
| 57 | #define EC_MSG_DEADLINE_MS 100 |
Simon Glass | a17d94f | 2013-02-25 14:08:39 -0800 | [diff] [blame] | 58 | |
| 59 | /* |
| 60 | * Time between raising the SPI chip select (for the end of a |
| 61 | * transaction) and dropping it again (for the next transaction). |
Derek Basehore | 49f91ac | 2013-11-18 11:30:48 +0100 | [diff] [blame] | 62 | * If we go too fast, the EC will miss the transaction. We know that we |
| 63 | * need at least 70 us with the 16 MHz STM32 EC, so go with 200 us to be |
| 64 | * safe. |
Simon Glass | a17d94f | 2013-02-25 14:08:39 -0800 | [diff] [blame] | 65 | */ |
Derek Basehore | 49f91ac | 2013-11-18 11:30:48 +0100 | [diff] [blame] | 66 | #define EC_SPI_RECOVERY_TIME_NS (200 * 1000) |
Simon Glass | a17d94f | 2013-02-25 14:08:39 -0800 | [diff] [blame] | 67 | |
| 68 | /** |
| 69 | * struct cros_ec_spi - information about a SPI-connected EC |
| 70 | * |
| 71 | * @spi: SPI device we are connected to |
| 72 | * @last_transfer_ns: time that we last finished a transfer, or 0 if there |
| 73 | * if no record |
Alexandru M Stan | ff4378f | 2015-06-09 13:04:49 +0200 | [diff] [blame] | 74 | * @start_of_msg_delay: used to set the delay_usecs on the spi_transfer that |
| 75 | * is sent when we want to turn on CS at the start of a transaction. |
Rhyland Klein | 01e73c8 | 2013-12-09 12:36:09 +0100 | [diff] [blame] | 76 | * @end_of_msg_delay: used to set the delay_usecs on the spi_transfer that |
| 77 | * is sent when we want to turn off CS at the end of a transaction. |
Simon Glass | a17d94f | 2013-02-25 14:08:39 -0800 | [diff] [blame] | 78 | */ |
| 79 | struct cros_ec_spi { |
| 80 | struct spi_device *spi; |
| 81 | s64 last_transfer_ns; |
Alexandru M Stan | ff4378f | 2015-06-09 13:04:49 +0200 | [diff] [blame] | 82 | unsigned int start_of_msg_delay; |
Rhyland Klein | 01e73c8 | 2013-12-09 12:36:09 +0100 | [diff] [blame] | 83 | unsigned int end_of_msg_delay; |
Simon Glass | a17d94f | 2013-02-25 14:08:39 -0800 | [diff] [blame] | 84 | }; |
| 85 | |
| 86 | static void debug_packet(struct device *dev, const char *name, u8 *ptr, |
Stephen Barber | d365407 | 2015-06-09 13:04:46 +0200 | [diff] [blame] | 87 | int len) |
Simon Glass | a17d94f | 2013-02-25 14:08:39 -0800 | [diff] [blame] | 88 | { |
| 89 | #ifdef DEBUG |
| 90 | int i; |
| 91 | |
| 92 | dev_dbg(dev, "%s: ", name); |
| 93 | for (i = 0; i < len; i++) |
Thierry Reding | 9e146f4 | 2013-11-18 11:30:49 +0100 | [diff] [blame] | 94 | pr_cont(" %02x", ptr[i]); |
| 95 | |
| 96 | pr_cont("\n"); |
Simon Glass | a17d94f | 2013-02-25 14:08:39 -0800 | [diff] [blame] | 97 | #endif |
| 98 | } |
| 99 | |
Stephen Barber | d365407 | 2015-06-09 13:04:46 +0200 | [diff] [blame] | 100 | static int terminate_request(struct cros_ec_device *ec_dev) |
| 101 | { |
| 102 | struct cros_ec_spi *ec_spi = ec_dev->priv; |
| 103 | struct spi_message msg; |
| 104 | struct spi_transfer trans; |
| 105 | int ret; |
| 106 | |
| 107 | /* |
| 108 | * Turn off CS, possibly adding a delay to ensure the rising edge |
| 109 | * doesn't come too soon after the end of the data. |
| 110 | */ |
| 111 | spi_message_init(&msg); |
| 112 | memset(&trans, 0, sizeof(trans)); |
| 113 | trans.delay_usecs = ec_spi->end_of_msg_delay; |
| 114 | spi_message_add_tail(&trans, &msg); |
| 115 | |
Nicolas Boichat | 6d6e44a | 2015-11-25 13:51:07 +0800 | [diff] [blame] | 116 | ret = spi_sync_locked(ec_spi->spi, &msg); |
Stephen Barber | d365407 | 2015-06-09 13:04:46 +0200 | [diff] [blame] | 117 | |
| 118 | /* Reset end-of-response timer */ |
| 119 | ec_spi->last_transfer_ns = ktime_get_ns(); |
| 120 | if (ret < 0) { |
| 121 | dev_err(ec_dev->dev, |
| 122 | "cs-deassert spi transfer failed: %d\n", |
| 123 | ret); |
| 124 | } |
| 125 | |
| 126 | return ret; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * receive_n_bytes - receive n bytes from the EC. |
| 131 | * |
| 132 | * Assumes buf is a pointer into the ec_dev->din buffer |
| 133 | */ |
| 134 | static int receive_n_bytes(struct cros_ec_device *ec_dev, u8 *buf, int n) |
| 135 | { |
| 136 | struct cros_ec_spi *ec_spi = ec_dev->priv; |
| 137 | struct spi_transfer trans; |
| 138 | struct spi_message msg; |
| 139 | int ret; |
| 140 | |
| 141 | BUG_ON(buf - ec_dev->din + n > ec_dev->din_size); |
| 142 | |
| 143 | memset(&trans, 0, sizeof(trans)); |
| 144 | trans.cs_change = 1; |
| 145 | trans.rx_buf = buf; |
| 146 | trans.len = n; |
| 147 | |
| 148 | spi_message_init(&msg); |
| 149 | spi_message_add_tail(&trans, &msg); |
Nicolas Boichat | 6d6e44a | 2015-11-25 13:51:07 +0800 | [diff] [blame] | 150 | ret = spi_sync_locked(ec_spi->spi, &msg); |
Stephen Barber | d365407 | 2015-06-09 13:04:46 +0200 | [diff] [blame] | 151 | if (ret < 0) |
| 152 | dev_err(ec_dev->dev, "spi transfer failed: %d\n", ret); |
| 153 | |
| 154 | return ret; |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * cros_ec_spi_receive_packet - Receive a packet from the EC. |
| 159 | * |
| 160 | * This function has two phases: reading the preamble bytes (since if we read |
| 161 | * data from the EC before it is ready to send, we just get preamble) and |
| 162 | * reading the actual message. |
| 163 | * |
| 164 | * The received data is placed into ec_dev->din. |
| 165 | * |
| 166 | * @ec_dev: ChromeOS EC device |
| 167 | * @need_len: Number of message bytes we need to read |
| 168 | */ |
| 169 | static int cros_ec_spi_receive_packet(struct cros_ec_device *ec_dev, |
| 170 | int need_len) |
| 171 | { |
| 172 | struct ec_host_response *response; |
| 173 | u8 *ptr, *end; |
| 174 | int ret; |
| 175 | unsigned long deadline; |
| 176 | int todo; |
| 177 | |
Lee Jones | 8827a64 | 2015-10-28 16:42:45 +0000 | [diff] [blame] | 178 | BUG_ON(ec_dev->din_size < EC_MSG_PREAMBLE_COUNT); |
Stephen Barber | d365407 | 2015-06-09 13:04:46 +0200 | [diff] [blame] | 179 | |
| 180 | /* Receive data until we see the header byte */ |
| 181 | deadline = jiffies + msecs_to_jiffies(EC_MSG_DEADLINE_MS); |
| 182 | while (true) { |
| 183 | unsigned long start_jiffies = jiffies; |
| 184 | |
| 185 | ret = receive_n_bytes(ec_dev, |
| 186 | ec_dev->din, |
| 187 | EC_MSG_PREAMBLE_COUNT); |
| 188 | if (ret < 0) |
| 189 | return ret; |
| 190 | |
| 191 | ptr = ec_dev->din; |
| 192 | for (end = ptr + EC_MSG_PREAMBLE_COUNT; ptr != end; ptr++) { |
| 193 | if (*ptr == EC_SPI_FRAME_START) { |
| 194 | dev_dbg(ec_dev->dev, "msg found at %zd\n", |
| 195 | ptr - ec_dev->din); |
| 196 | break; |
| 197 | } |
| 198 | } |
| 199 | if (ptr != end) |
| 200 | break; |
| 201 | |
| 202 | /* |
| 203 | * Use the time at the start of the loop as a timeout. This |
| 204 | * gives us one last shot at getting the transfer and is useful |
| 205 | * in case we got context switched out for a while. |
| 206 | */ |
| 207 | if (time_after(start_jiffies, deadline)) { |
| 208 | dev_warn(ec_dev->dev, "EC failed to respond in time\n"); |
| 209 | return -ETIMEDOUT; |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | /* |
| 214 | * ptr now points to the header byte. Copy any valid data to the |
| 215 | * start of our buffer |
| 216 | */ |
| 217 | todo = end - ++ptr; |
| 218 | BUG_ON(todo < 0 || todo > ec_dev->din_size); |
| 219 | todo = min(todo, need_len); |
| 220 | memmove(ec_dev->din, ptr, todo); |
| 221 | ptr = ec_dev->din + todo; |
| 222 | dev_dbg(ec_dev->dev, "need %d, got %d bytes from preamble\n", |
| 223 | need_len, todo); |
| 224 | need_len -= todo; |
| 225 | |
| 226 | /* If the entire response struct wasn't read, get the rest of it. */ |
| 227 | if (todo < sizeof(*response)) { |
| 228 | ret = receive_n_bytes(ec_dev, ptr, sizeof(*response) - todo); |
| 229 | if (ret < 0) |
| 230 | return -EBADMSG; |
| 231 | ptr += (sizeof(*response) - todo); |
| 232 | todo = sizeof(*response); |
| 233 | } |
| 234 | |
| 235 | response = (struct ec_host_response *)ec_dev->din; |
| 236 | |
| 237 | /* Abort if data_len is too large. */ |
| 238 | if (response->data_len > ec_dev->din_size) |
| 239 | return -EMSGSIZE; |
| 240 | |
| 241 | /* Receive data until we have it all */ |
| 242 | while (need_len > 0) { |
| 243 | /* |
| 244 | * We can't support transfers larger than the SPI FIFO size |
| 245 | * unless we have DMA. We don't have DMA on the ISP SPI ports |
| 246 | * for Exynos. We need a way of asking SPI driver for |
| 247 | * maximum-supported transfer size. |
| 248 | */ |
| 249 | todo = min(need_len, 256); |
| 250 | dev_dbg(ec_dev->dev, "loop, todo=%d, need_len=%d, ptr=%zd\n", |
| 251 | todo, need_len, ptr - ec_dev->din); |
| 252 | |
| 253 | ret = receive_n_bytes(ec_dev, ptr, todo); |
| 254 | if (ret < 0) |
| 255 | return ret; |
| 256 | |
| 257 | ptr += todo; |
| 258 | need_len -= todo; |
| 259 | } |
| 260 | |
| 261 | dev_dbg(ec_dev->dev, "loop done, ptr=%zd\n", ptr - ec_dev->din); |
| 262 | |
| 263 | return 0; |
| 264 | } |
| 265 | |
Simon Glass | a17d94f | 2013-02-25 14:08:39 -0800 | [diff] [blame] | 266 | /** |
| 267 | * cros_ec_spi_receive_response - Receive a response from the EC. |
| 268 | * |
| 269 | * This function has two phases: reading the preamble bytes (since if we read |
| 270 | * data from the EC before it is ready to send, we just get preamble) and |
| 271 | * reading the actual message. |
| 272 | * |
| 273 | * The received data is placed into ec_dev->din. |
| 274 | * |
| 275 | * @ec_dev: ChromeOS EC device |
| 276 | * @need_len: Number of message bytes we need to read |
| 277 | */ |
| 278 | static int cros_ec_spi_receive_response(struct cros_ec_device *ec_dev, |
| 279 | int need_len) |
| 280 | { |
Simon Glass | a17d94f | 2013-02-25 14:08:39 -0800 | [diff] [blame] | 281 | u8 *ptr, *end; |
| 282 | int ret; |
| 283 | unsigned long deadline; |
| 284 | int todo; |
| 285 | |
Lee Jones | 8827a64 | 2015-10-28 16:42:45 +0000 | [diff] [blame] | 286 | BUG_ON(ec_dev->din_size < EC_MSG_PREAMBLE_COUNT); |
Stephen Barber | d365407 | 2015-06-09 13:04:46 +0200 | [diff] [blame] | 287 | |
Simon Glass | a17d94f | 2013-02-25 14:08:39 -0800 | [diff] [blame] | 288 | /* Receive data until we see the header byte */ |
| 289 | deadline = jiffies + msecs_to_jiffies(EC_MSG_DEADLINE_MS); |
Doug Anderson | c9a81d6 | 2014-04-30 10:44:06 -0700 | [diff] [blame] | 290 | while (true) { |
| 291 | unsigned long start_jiffies = jiffies; |
| 292 | |
Stephen Barber | d365407 | 2015-06-09 13:04:46 +0200 | [diff] [blame] | 293 | ret = receive_n_bytes(ec_dev, |
| 294 | ec_dev->din, |
| 295 | EC_MSG_PREAMBLE_COUNT); |
| 296 | if (ret < 0) |
Simon Glass | a17d94f | 2013-02-25 14:08:39 -0800 | [diff] [blame] | 297 | return ret; |
Simon Glass | a17d94f | 2013-02-25 14:08:39 -0800 | [diff] [blame] | 298 | |
Stephen Barber | d365407 | 2015-06-09 13:04:46 +0200 | [diff] [blame] | 299 | ptr = ec_dev->din; |
Simon Glass | a17d94f | 2013-02-25 14:08:39 -0800 | [diff] [blame] | 300 | for (end = ptr + EC_MSG_PREAMBLE_COUNT; ptr != end; ptr++) { |
Stephen Barber | d365407 | 2015-06-09 13:04:46 +0200 | [diff] [blame] | 301 | if (*ptr == EC_SPI_FRAME_START) { |
Geert Uytterhoeven | c34924b | 2013-05-08 23:37:45 +0200 | [diff] [blame] | 302 | dev_dbg(ec_dev->dev, "msg found at %zd\n", |
Simon Glass | a17d94f | 2013-02-25 14:08:39 -0800 | [diff] [blame] | 303 | ptr - ec_dev->din); |
| 304 | break; |
| 305 | } |
| 306 | } |
Doug Anderson | c9a81d6 | 2014-04-30 10:44:06 -0700 | [diff] [blame] | 307 | if (ptr != end) |
| 308 | break; |
Simon Glass | a17d94f | 2013-02-25 14:08:39 -0800 | [diff] [blame] | 309 | |
Doug Anderson | c9a81d6 | 2014-04-30 10:44:06 -0700 | [diff] [blame] | 310 | /* |
| 311 | * Use the time at the start of the loop as a timeout. This |
| 312 | * gives us one last shot at getting the transfer and is useful |
| 313 | * in case we got context switched out for a while. |
| 314 | */ |
| 315 | if (time_after(start_jiffies, deadline)) { |
Simon Glass | a17d94f | 2013-02-25 14:08:39 -0800 | [diff] [blame] | 316 | dev_warn(ec_dev->dev, "EC failed to respond in time\n"); |
| 317 | return -ETIMEDOUT; |
| 318 | } |
Doug Anderson | c9a81d6 | 2014-04-30 10:44:06 -0700 | [diff] [blame] | 319 | } |
Simon Glass | a17d94f | 2013-02-25 14:08:39 -0800 | [diff] [blame] | 320 | |
| 321 | /* |
| 322 | * ptr now points to the header byte. Copy any valid data to the |
| 323 | * start of our buffer |
| 324 | */ |
| 325 | todo = end - ++ptr; |
| 326 | BUG_ON(todo < 0 || todo > ec_dev->din_size); |
| 327 | todo = min(todo, need_len); |
| 328 | memmove(ec_dev->din, ptr, todo); |
| 329 | ptr = ec_dev->din + todo; |
| 330 | dev_dbg(ec_dev->dev, "need %d, got %d bytes from preamble\n", |
| 331 | need_len, todo); |
| 332 | need_len -= todo; |
| 333 | |
| 334 | /* Receive data until we have it all */ |
| 335 | while (need_len > 0) { |
| 336 | /* |
| 337 | * We can't support transfers larger than the SPI FIFO size |
| 338 | * unless we have DMA. We don't have DMA on the ISP SPI ports |
| 339 | * for Exynos. We need a way of asking SPI driver for |
| 340 | * maximum-supported transfer size. |
| 341 | */ |
| 342 | todo = min(need_len, 256); |
Geert Uytterhoeven | c34924b | 2013-05-08 23:37:45 +0200 | [diff] [blame] | 343 | dev_dbg(ec_dev->dev, "loop, todo=%d, need_len=%d, ptr=%zd\n", |
Simon Glass | a17d94f | 2013-02-25 14:08:39 -0800 | [diff] [blame] | 344 | todo, need_len, ptr - ec_dev->din); |
| 345 | |
Stephen Barber | d365407 | 2015-06-09 13:04:46 +0200 | [diff] [blame] | 346 | ret = receive_n_bytes(ec_dev, ptr, todo); |
| 347 | if (ret < 0) |
Simon Glass | a17d94f | 2013-02-25 14:08:39 -0800 | [diff] [blame] | 348 | return ret; |
Simon Glass | a17d94f | 2013-02-25 14:08:39 -0800 | [diff] [blame] | 349 | |
| 350 | debug_packet(ec_dev->dev, "interim", ptr, todo); |
| 351 | ptr += todo; |
| 352 | need_len -= todo; |
| 353 | } |
| 354 | |
Geert Uytterhoeven | c34924b | 2013-05-08 23:37:45 +0200 | [diff] [blame] | 355 | dev_dbg(ec_dev->dev, "loop done, ptr=%zd\n", ptr - ec_dev->din); |
Simon Glass | a17d94f | 2013-02-25 14:08:39 -0800 | [diff] [blame] | 356 | |
| 357 | return 0; |
| 358 | } |
| 359 | |
| 360 | /** |
Stephen Barber | d365407 | 2015-06-09 13:04:46 +0200 | [diff] [blame] | 361 | * cros_ec_pkt_xfer_spi - Transfer a packet over SPI and receive the reply |
| 362 | * |
| 363 | * @ec_dev: ChromeOS EC device |
| 364 | * @ec_msg: Message to transfer |
| 365 | */ |
| 366 | static int cros_ec_pkt_xfer_spi(struct cros_ec_device *ec_dev, |
| 367 | struct cros_ec_command *ec_msg) |
| 368 | { |
Stephen Barber | d365407 | 2015-06-09 13:04:46 +0200 | [diff] [blame] | 369 | struct ec_host_response *response; |
| 370 | struct cros_ec_spi *ec_spi = ec_dev->priv; |
Alexandru M Stan | ff4378f | 2015-06-09 13:04:49 +0200 | [diff] [blame] | 371 | struct spi_transfer trans, trans_delay; |
Stephen Barber | d365407 | 2015-06-09 13:04:46 +0200 | [diff] [blame] | 372 | struct spi_message msg; |
| 373 | int i, len; |
| 374 | u8 *ptr; |
| 375 | u8 *rx_buf; |
| 376 | u8 sum; |
| 377 | int ret = 0, final_ret; |
| 378 | |
| 379 | len = cros_ec_prepare_tx(ec_dev, ec_msg); |
Stephen Barber | d365407 | 2015-06-09 13:04:46 +0200 | [diff] [blame] | 380 | dev_dbg(ec_dev->dev, "prepared, len=%d\n", len); |
| 381 | |
| 382 | /* If it's too soon to do another transaction, wait */ |
| 383 | if (ec_spi->last_transfer_ns) { |
| 384 | unsigned long delay; /* The delay completed so far */ |
| 385 | |
| 386 | delay = ktime_get_ns() - ec_spi->last_transfer_ns; |
| 387 | if (delay < EC_SPI_RECOVERY_TIME_NS) |
| 388 | ndelay(EC_SPI_RECOVERY_TIME_NS - delay); |
| 389 | } |
| 390 | |
| 391 | rx_buf = kzalloc(len, GFP_KERNEL); |
Nicolas Boichat | 6d6e44a | 2015-11-25 13:51:07 +0800 | [diff] [blame] | 392 | if (!rx_buf) |
| 393 | return -ENOMEM; |
| 394 | |
| 395 | spi_bus_lock(ec_spi->spi->master); |
Stephen Barber | d365407 | 2015-06-09 13:04:46 +0200 | [diff] [blame] | 396 | |
Alexandru M Stan | ff4378f | 2015-06-09 13:04:49 +0200 | [diff] [blame] | 397 | /* |
| 398 | * Leave a gap between CS assertion and clocking of data to allow the |
| 399 | * EC time to wakeup. |
| 400 | */ |
| 401 | spi_message_init(&msg); |
| 402 | if (ec_spi->start_of_msg_delay) { |
| 403 | memset(&trans_delay, 0, sizeof(trans_delay)); |
| 404 | trans_delay.delay_usecs = ec_spi->start_of_msg_delay; |
| 405 | spi_message_add_tail(&trans_delay, &msg); |
| 406 | } |
| 407 | |
Stephen Barber | d365407 | 2015-06-09 13:04:46 +0200 | [diff] [blame] | 408 | /* Transmit phase - send our message */ |
| 409 | memset(&trans, 0, sizeof(trans)); |
| 410 | trans.tx_buf = ec_dev->dout; |
| 411 | trans.rx_buf = rx_buf; |
| 412 | trans.len = len; |
| 413 | trans.cs_change = 1; |
Stephen Barber | d365407 | 2015-06-09 13:04:46 +0200 | [diff] [blame] | 414 | spi_message_add_tail(&trans, &msg); |
Nicolas Boichat | 6d6e44a | 2015-11-25 13:51:07 +0800 | [diff] [blame] | 415 | ret = spi_sync_locked(ec_spi->spi, &msg); |
Stephen Barber | d365407 | 2015-06-09 13:04:46 +0200 | [diff] [blame] | 416 | |
| 417 | /* Get the response */ |
| 418 | if (!ret) { |
| 419 | /* Verify that EC can process command */ |
| 420 | for (i = 0; i < len; i++) { |
| 421 | switch (rx_buf[i]) { |
| 422 | case EC_SPI_PAST_END: |
| 423 | case EC_SPI_RX_BAD_DATA: |
| 424 | case EC_SPI_NOT_READY: |
| 425 | ret = -EAGAIN; |
| 426 | ec_msg->result = EC_RES_IN_PROGRESS; |
| 427 | default: |
| 428 | break; |
| 429 | } |
| 430 | if (ret) |
| 431 | break; |
| 432 | } |
| 433 | if (!ret) |
| 434 | ret = cros_ec_spi_receive_packet(ec_dev, |
| 435 | ec_msg->insize + sizeof(*response)); |
| 436 | } else { |
| 437 | dev_err(ec_dev->dev, "spi transfer failed: %d\n", ret); |
| 438 | } |
| 439 | |
| 440 | final_ret = terminate_request(ec_dev); |
Nicolas Boichat | 6d6e44a | 2015-11-25 13:51:07 +0800 | [diff] [blame] | 441 | |
| 442 | spi_bus_unlock(ec_spi->spi->master); |
| 443 | |
Stephen Barber | d365407 | 2015-06-09 13:04:46 +0200 | [diff] [blame] | 444 | if (!ret) |
| 445 | ret = final_ret; |
| 446 | if (ret < 0) |
| 447 | goto exit; |
| 448 | |
| 449 | ptr = ec_dev->din; |
| 450 | |
| 451 | /* check response error code */ |
| 452 | response = (struct ec_host_response *)ptr; |
| 453 | ec_msg->result = response->result; |
| 454 | |
| 455 | ret = cros_ec_check_result(ec_dev, ec_msg); |
| 456 | if (ret) |
| 457 | goto exit; |
| 458 | |
| 459 | len = response->data_len; |
| 460 | sum = 0; |
| 461 | if (len > ec_msg->insize) { |
| 462 | dev_err(ec_dev->dev, "packet too long (%d bytes, expected %d)", |
| 463 | len, ec_msg->insize); |
| 464 | ret = -EMSGSIZE; |
| 465 | goto exit; |
| 466 | } |
| 467 | |
| 468 | for (i = 0; i < sizeof(*response); i++) |
| 469 | sum += ptr[i]; |
| 470 | |
| 471 | /* copy response packet payload and compute checksum */ |
| 472 | memcpy(ec_msg->data, ptr + sizeof(*response), len); |
| 473 | for (i = 0; i < len; i++) |
| 474 | sum += ec_msg->data[i]; |
| 475 | |
| 476 | if (sum) { |
| 477 | dev_err(ec_dev->dev, |
| 478 | "bad packet checksum, calculated %x\n", |
| 479 | sum); |
| 480 | ret = -EBADMSG; |
| 481 | goto exit; |
| 482 | } |
| 483 | |
| 484 | ret = len; |
| 485 | exit: |
| 486 | kfree(rx_buf); |
| 487 | if (ec_msg->command == EC_CMD_REBOOT_EC) |
| 488 | msleep(EC_REBOOT_DELAY_MS); |
| 489 | |
| 490 | return ret; |
| 491 | } |
| 492 | |
| 493 | /** |
Bill Richardson | 7e6cb5b | 2014-06-18 11:14:00 -0700 | [diff] [blame] | 494 | * cros_ec_cmd_xfer_spi - Transfer a message over SPI and receive the reply |
Simon Glass | a17d94f | 2013-02-25 14:08:39 -0800 | [diff] [blame] | 495 | * |
| 496 | * @ec_dev: ChromeOS EC device |
| 497 | * @ec_msg: Message to transfer |
| 498 | */ |
Bill Richardson | 7e6cb5b | 2014-06-18 11:14:00 -0700 | [diff] [blame] | 499 | static int cros_ec_cmd_xfer_spi(struct cros_ec_device *ec_dev, |
Bill Richardson | 5d4773e | 2014-06-18 11:14:02 -0700 | [diff] [blame] | 500 | struct cros_ec_command *ec_msg) |
Simon Glass | a17d94f | 2013-02-25 14:08:39 -0800 | [diff] [blame] | 501 | { |
| 502 | struct cros_ec_spi *ec_spi = ec_dev->priv; |
| 503 | struct spi_transfer trans; |
| 504 | struct spi_message msg; |
| 505 | int i, len; |
| 506 | u8 *ptr; |
Stephen Barber | d365407 | 2015-06-09 13:04:46 +0200 | [diff] [blame] | 507 | u8 *rx_buf; |
Simon Glass | a17d94f | 2013-02-25 14:08:39 -0800 | [diff] [blame] | 508 | int sum; |
| 509 | int ret = 0, final_ret; |
Simon Glass | a17d94f | 2013-02-25 14:08:39 -0800 | [diff] [blame] | 510 | |
| 511 | len = cros_ec_prepare_tx(ec_dev, ec_msg); |
| 512 | dev_dbg(ec_dev->dev, "prepared, len=%d\n", len); |
| 513 | |
| 514 | /* If it's too soon to do another transaction, wait */ |
| 515 | if (ec_spi->last_transfer_ns) { |
Simon Glass | a17d94f | 2013-02-25 14:08:39 -0800 | [diff] [blame] | 516 | unsigned long delay; /* The delay completed so far */ |
| 517 | |
Thomas Gleixner | 154adc1 | 2014-07-16 21:04:41 +0000 | [diff] [blame] | 518 | delay = ktime_get_ns() - ec_spi->last_transfer_ns; |
Simon Glass | a17d94f | 2013-02-25 14:08:39 -0800 | [diff] [blame] | 519 | if (delay < EC_SPI_RECOVERY_TIME_NS) |
David Hendricks | 1fe3686 | 2014-04-30 10:44:04 -0700 | [diff] [blame] | 520 | ndelay(EC_SPI_RECOVERY_TIME_NS - delay); |
Simon Glass | a17d94f | 2013-02-25 14:08:39 -0800 | [diff] [blame] | 521 | } |
| 522 | |
Stephen Barber | d365407 | 2015-06-09 13:04:46 +0200 | [diff] [blame] | 523 | rx_buf = kzalloc(len, GFP_KERNEL); |
Nicolas Boichat | 6d6e44a | 2015-11-25 13:51:07 +0800 | [diff] [blame] | 524 | if (!rx_buf) |
| 525 | return -ENOMEM; |
| 526 | |
| 527 | spi_bus_lock(ec_spi->spi->master); |
Stephen Barber | d365407 | 2015-06-09 13:04:46 +0200 | [diff] [blame] | 528 | |
Simon Glass | a17d94f | 2013-02-25 14:08:39 -0800 | [diff] [blame] | 529 | /* Transmit phase - send our message */ |
| 530 | debug_packet(ec_dev->dev, "out", ec_dev->dout, len); |
Thierry Reding | daf93d2 | 2013-12-09 12:36:10 +0100 | [diff] [blame] | 531 | memset(&trans, 0, sizeof(trans)); |
Simon Glass | a17d94f | 2013-02-25 14:08:39 -0800 | [diff] [blame] | 532 | trans.tx_buf = ec_dev->dout; |
Stephen Barber | d365407 | 2015-06-09 13:04:46 +0200 | [diff] [blame] | 533 | trans.rx_buf = rx_buf; |
Simon Glass | a17d94f | 2013-02-25 14:08:39 -0800 | [diff] [blame] | 534 | trans.len = len; |
| 535 | trans.cs_change = 1; |
| 536 | spi_message_init(&msg); |
| 537 | spi_message_add_tail(&trans, &msg); |
Nicolas Boichat | 6d6e44a | 2015-11-25 13:51:07 +0800 | [diff] [blame] | 538 | ret = spi_sync_locked(ec_spi->spi, &msg); |
Simon Glass | a17d94f | 2013-02-25 14:08:39 -0800 | [diff] [blame] | 539 | |
| 540 | /* Get the response */ |
| 541 | if (!ret) { |
Stephen Barber | d365407 | 2015-06-09 13:04:46 +0200 | [diff] [blame] | 542 | /* Verify that EC can process command */ |
| 543 | for (i = 0; i < len; i++) { |
| 544 | switch (rx_buf[i]) { |
| 545 | case EC_SPI_PAST_END: |
| 546 | case EC_SPI_RX_BAD_DATA: |
| 547 | case EC_SPI_NOT_READY: |
| 548 | ret = -EAGAIN; |
| 549 | ec_msg->result = EC_RES_IN_PROGRESS; |
| 550 | default: |
| 551 | break; |
| 552 | } |
| 553 | if (ret) |
| 554 | break; |
| 555 | } |
| 556 | if (!ret) |
| 557 | ret = cros_ec_spi_receive_response(ec_dev, |
| 558 | ec_msg->insize + EC_MSG_TX_PROTO_BYTES); |
Simon Glass | a17d94f | 2013-02-25 14:08:39 -0800 | [diff] [blame] | 559 | } else { |
| 560 | dev_err(ec_dev->dev, "spi transfer failed: %d\n", ret); |
| 561 | } |
| 562 | |
Stephen Barber | d365407 | 2015-06-09 13:04:46 +0200 | [diff] [blame] | 563 | final_ret = terminate_request(ec_dev); |
Nicolas Boichat | 6d6e44a | 2015-11-25 13:51:07 +0800 | [diff] [blame] | 564 | |
| 565 | spi_bus_unlock(ec_spi->spi->master); |
| 566 | |
Simon Glass | a17d94f | 2013-02-25 14:08:39 -0800 | [diff] [blame] | 567 | if (!ret) |
| 568 | ret = final_ret; |
Stephen Barber | d365407 | 2015-06-09 13:04:46 +0200 | [diff] [blame] | 569 | if (ret < 0) |
Doug Anderson | 362196e | 2014-04-30 10:44:05 -0700 | [diff] [blame] | 570 | goto exit; |
Simon Glass | a17d94f | 2013-02-25 14:08:39 -0800 | [diff] [blame] | 571 | |
Simon Glass | a17d94f | 2013-02-25 14:08:39 -0800 | [diff] [blame] | 572 | ptr = ec_dev->din; |
Bill Richardson | 6db07b6 | 2014-06-18 11:14:05 -0700 | [diff] [blame] | 573 | |
| 574 | /* check response error code */ |
| 575 | ec_msg->result = ptr[0]; |
| 576 | ret = cros_ec_check_result(ec_dev, ec_msg); |
| 577 | if (ret) |
Doug Anderson | 362196e | 2014-04-30 10:44:05 -0700 | [diff] [blame] | 578 | goto exit; |
Bill Richardson | 6db07b6 | 2014-06-18 11:14:05 -0700 | [diff] [blame] | 579 | |
Simon Glass | a17d94f | 2013-02-25 14:08:39 -0800 | [diff] [blame] | 580 | len = ptr[1]; |
| 581 | sum = ptr[0] + ptr[1]; |
Bill Richardson | 5d4773e | 2014-06-18 11:14:02 -0700 | [diff] [blame] | 582 | if (len > ec_msg->insize) { |
Simon Glass | a17d94f | 2013-02-25 14:08:39 -0800 | [diff] [blame] | 583 | dev_err(ec_dev->dev, "packet too long (%d bytes, expected %d)", |
Bill Richardson | 5d4773e | 2014-06-18 11:14:02 -0700 | [diff] [blame] | 584 | len, ec_msg->insize); |
Doug Anderson | 362196e | 2014-04-30 10:44:05 -0700 | [diff] [blame] | 585 | ret = -ENOSPC; |
| 586 | goto exit; |
Simon Glass | a17d94f | 2013-02-25 14:08:39 -0800 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | /* copy response packet payload and compute checksum */ |
| 590 | for (i = 0; i < len; i++) { |
| 591 | sum += ptr[i + 2]; |
Bill Richardson | 5d4773e | 2014-06-18 11:14:02 -0700 | [diff] [blame] | 592 | if (ec_msg->insize) |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 593 | ec_msg->data[i] = ptr[i + 2]; |
Simon Glass | a17d94f | 2013-02-25 14:08:39 -0800 | [diff] [blame] | 594 | } |
| 595 | sum &= 0xff; |
| 596 | |
| 597 | debug_packet(ec_dev->dev, "in", ptr, len + 3); |
| 598 | |
| 599 | if (sum != ptr[len + 2]) { |
| 600 | dev_err(ec_dev->dev, |
| 601 | "bad packet checksum, expected %02x, got %02x\n", |
| 602 | sum, ptr[len + 2]); |
Doug Anderson | 362196e | 2014-04-30 10:44:05 -0700 | [diff] [blame] | 603 | ret = -EBADMSG; |
| 604 | goto exit; |
Simon Glass | a17d94f | 2013-02-25 14:08:39 -0800 | [diff] [blame] | 605 | } |
| 606 | |
Bill Richardson | 12ebc8a | 2014-06-18 11:14:06 -0700 | [diff] [blame] | 607 | ret = len; |
Doug Anderson | 362196e | 2014-04-30 10:44:05 -0700 | [diff] [blame] | 608 | exit: |
Stephen Barber | d365407 | 2015-06-09 13:04:46 +0200 | [diff] [blame] | 609 | kfree(rx_buf); |
Doug Anderson | 659e142 | 2014-09-18 17:18:54 +0200 | [diff] [blame] | 610 | if (ec_msg->command == EC_CMD_REBOOT_EC) |
| 611 | msleep(EC_REBOOT_DELAY_MS); |
| 612 | |
Doug Anderson | 362196e | 2014-04-30 10:44:05 -0700 | [diff] [blame] | 613 | return ret; |
Simon Glass | a17d94f | 2013-02-25 14:08:39 -0800 | [diff] [blame] | 614 | } |
| 615 | |
Rhyland Klein | 01e73c8 | 2013-12-09 12:36:09 +0100 | [diff] [blame] | 616 | static void cros_ec_spi_dt_probe(struct cros_ec_spi *ec_spi, struct device *dev) |
| 617 | { |
| 618 | struct device_node *np = dev->of_node; |
| 619 | u32 val; |
| 620 | int ret; |
| 621 | |
Alexandru M Stan | ff4378f | 2015-06-09 13:04:49 +0200 | [diff] [blame] | 622 | ret = of_property_read_u32(np, "google,cros-ec-spi-pre-delay", &val); |
| 623 | if (!ret) |
| 624 | ec_spi->start_of_msg_delay = val; |
| 625 | |
Rhyland Klein | 01e73c8 | 2013-12-09 12:36:09 +0100 | [diff] [blame] | 626 | ret = of_property_read_u32(np, "google,cros-ec-spi-msg-delay", &val); |
| 627 | if (!ret) |
| 628 | ec_spi->end_of_msg_delay = val; |
| 629 | } |
| 630 | |
Thierry Reding | 9922b41 | 2013-11-25 13:22:30 +0100 | [diff] [blame] | 631 | static int cros_ec_spi_probe(struct spi_device *spi) |
Simon Glass | a17d94f | 2013-02-25 14:08:39 -0800 | [diff] [blame] | 632 | { |
| 633 | struct device *dev = &spi->dev; |
| 634 | struct cros_ec_device *ec_dev; |
| 635 | struct cros_ec_spi *ec_spi; |
| 636 | int err; |
| 637 | |
| 638 | spi->bits_per_word = 8; |
| 639 | spi->mode = SPI_MODE_0; |
| 640 | err = spi_setup(spi); |
| 641 | if (err < 0) |
| 642 | return err; |
| 643 | |
| 644 | ec_spi = devm_kzalloc(dev, sizeof(*ec_spi), GFP_KERNEL); |
| 645 | if (ec_spi == NULL) |
| 646 | return -ENOMEM; |
| 647 | ec_spi->spi = spi; |
| 648 | ec_dev = devm_kzalloc(dev, sizeof(*ec_dev), GFP_KERNEL); |
| 649 | if (!ec_dev) |
| 650 | return -ENOMEM; |
| 651 | |
Rhyland Klein | 01e73c8 | 2013-12-09 12:36:09 +0100 | [diff] [blame] | 652 | /* Check for any DT properties */ |
| 653 | cros_ec_spi_dt_probe(ec_spi, dev); |
| 654 | |
Simon Glass | a17d94f | 2013-02-25 14:08:39 -0800 | [diff] [blame] | 655 | spi_set_drvdata(spi, ec_dev); |
Simon Glass | a17d94f | 2013-02-25 14:08:39 -0800 | [diff] [blame] | 656 | ec_dev->dev = dev; |
| 657 | ec_dev->priv = ec_spi; |
| 658 | ec_dev->irq = spi->irq; |
Bill Richardson | 7e6cb5b | 2014-06-18 11:14:00 -0700 | [diff] [blame] | 659 | ec_dev->cmd_xfer = cros_ec_cmd_xfer_spi; |
Stephen Barber | d365407 | 2015-06-09 13:04:46 +0200 | [diff] [blame] | 660 | ec_dev->pkt_xfer = cros_ec_pkt_xfer_spi; |
Simon Glass | a17d94f | 2013-02-25 14:08:39 -0800 | [diff] [blame] | 661 | ec_dev->phys_name = dev_name(&ec_spi->spi->dev); |
Stephen Barber | 2c7589a | 2015-06-09 13:04:45 +0200 | [diff] [blame] | 662 | ec_dev->din_size = EC_MSG_PREAMBLE_COUNT + |
| 663 | sizeof(struct ec_host_response) + |
| 664 | sizeof(struct ec_response_get_protocol_info); |
| 665 | ec_dev->dout_size = sizeof(struct ec_host_request); |
Simon Glass | a17d94f | 2013-02-25 14:08:39 -0800 | [diff] [blame] | 666 | |
Stephen Barber | d365407 | 2015-06-09 13:04:46 +0200 | [diff] [blame] | 667 | |
Simon Glass | a17d94f | 2013-02-25 14:08:39 -0800 | [diff] [blame] | 668 | err = cros_ec_register(ec_dev); |
| 669 | if (err) { |
| 670 | dev_err(dev, "cannot register EC\n"); |
| 671 | return err; |
| 672 | } |
| 673 | |
Prathyush K | fb50497 | 2014-06-16 14:12:23 -0700 | [diff] [blame] | 674 | device_init_wakeup(&spi->dev, true); |
| 675 | |
Simon Glass | a17d94f | 2013-02-25 14:08:39 -0800 | [diff] [blame] | 676 | return 0; |
| 677 | } |
| 678 | |
Thierry Reding | 9922b41 | 2013-11-25 13:22:30 +0100 | [diff] [blame] | 679 | static int cros_ec_spi_remove(struct spi_device *spi) |
Simon Glass | a17d94f | 2013-02-25 14:08:39 -0800 | [diff] [blame] | 680 | { |
| 681 | struct cros_ec_device *ec_dev; |
| 682 | |
| 683 | ec_dev = spi_get_drvdata(spi); |
| 684 | cros_ec_remove(ec_dev); |
| 685 | |
| 686 | return 0; |
| 687 | } |
| 688 | |
| 689 | #ifdef CONFIG_PM_SLEEP |
| 690 | static int cros_ec_spi_suspend(struct device *dev) |
| 691 | { |
| 692 | struct cros_ec_device *ec_dev = dev_get_drvdata(dev); |
| 693 | |
| 694 | return cros_ec_suspend(ec_dev); |
| 695 | } |
| 696 | |
| 697 | static int cros_ec_spi_resume(struct device *dev) |
| 698 | { |
| 699 | struct cros_ec_device *ec_dev = dev_get_drvdata(dev); |
| 700 | |
| 701 | return cros_ec_resume(ec_dev); |
| 702 | } |
| 703 | #endif |
| 704 | |
| 705 | static SIMPLE_DEV_PM_OPS(cros_ec_spi_pm_ops, cros_ec_spi_suspend, |
| 706 | cros_ec_spi_resume); |
| 707 | |
Javier Martinez Canillas | a78ea19 | 2015-08-20 09:07:22 +0200 | [diff] [blame] | 708 | static const struct of_device_id cros_ec_spi_of_match[] = { |
| 709 | { .compatible = "google,cros-ec-spi", }, |
| 710 | { /* sentinel */ }, |
| 711 | }; |
| 712 | MODULE_DEVICE_TABLE(of, cros_ec_spi_of_match); |
| 713 | |
Simon Glass | a17d94f | 2013-02-25 14:08:39 -0800 | [diff] [blame] | 714 | static const struct spi_device_id cros_ec_spi_id[] = { |
| 715 | { "cros-ec-spi", 0 }, |
| 716 | { } |
| 717 | }; |
| 718 | MODULE_DEVICE_TABLE(spi, cros_ec_spi_id); |
| 719 | |
| 720 | static struct spi_driver cros_ec_driver_spi = { |
| 721 | .driver = { |
| 722 | .name = "cros-ec-spi", |
Javier Martinez Canillas | a78ea19 | 2015-08-20 09:07:22 +0200 | [diff] [blame] | 723 | .of_match_table = of_match_ptr(cros_ec_spi_of_match), |
Simon Glass | a17d94f | 2013-02-25 14:08:39 -0800 | [diff] [blame] | 724 | .pm = &cros_ec_spi_pm_ops, |
| 725 | }, |
Thierry Reding | 9922b41 | 2013-11-25 13:22:30 +0100 | [diff] [blame] | 726 | .probe = cros_ec_spi_probe, |
| 727 | .remove = cros_ec_spi_remove, |
Simon Glass | a17d94f | 2013-02-25 14:08:39 -0800 | [diff] [blame] | 728 | .id_table = cros_ec_spi_id, |
| 729 | }; |
| 730 | |
| 731 | module_spi_driver(cros_ec_driver_spi); |
| 732 | |
Thierry Reding | ea0f8b0 | 2013-12-09 11:05:38 +0100 | [diff] [blame] | 733 | MODULE_LICENSE("GPL v2"); |
Simon Glass | a17d94f | 2013-02-25 14:08:39 -0800 | [diff] [blame] | 734 | MODULE_DESCRIPTION("ChromeOS EC multi function device (SPI)"); |