blob: b396705203fc07a8ace13203691678bdbb2038a8 [file] [log] [blame]
Simon Glassa17d94f2013-02-25 14:08:39 -08001/*
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 Klein01e73c82013-12-09 12:36:09 +010021#include <linux/of.h>
Simon Glassa17d94f2013-02-25 14:08:39 -080022#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 Anderson9c0b54a2014-04-30 10:44:07 -070042 * 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 Glassa17d94f2013-02-25 14:08:39 -080058
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 Basehore49f91ac2013-11-18 11:30:48 +010062 * 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 Glassa17d94f2013-02-25 14:08:39 -080065 */
Derek Basehore49f91ac2013-11-18 11:30:48 +010066#define EC_SPI_RECOVERY_TIME_NS (200 * 1000)
Simon Glassa17d94f2013-02-25 14:08:39 -080067
Doug Anderson659e1422014-09-18 17:18:54 +020068/*
69 * The EC is unresponsive for a time after a reboot command. Add a
70 * simple delay to make sure that the bus stays locked.
71 */
72#define EC_REBOOT_DELAY_MS 50
73
Simon Glassa17d94f2013-02-25 14:08:39 -080074/**
75 * struct cros_ec_spi - information about a SPI-connected EC
76 *
77 * @spi: SPI device we are connected to
78 * @last_transfer_ns: time that we last finished a transfer, or 0 if there
79 * if no record
Rhyland Klein01e73c82013-12-09 12:36:09 +010080 * @end_of_msg_delay: used to set the delay_usecs on the spi_transfer that
81 * is sent when we want to turn off CS at the end of a transaction.
Bill Richardson7e6cb5b2014-06-18 11:14:00 -070082 * @lock: mutex to ensure only one user of cros_ec_cmd_xfer_spi at a time
Simon Glassa17d94f2013-02-25 14:08:39 -080083 */
84struct cros_ec_spi {
85 struct spi_device *spi;
86 s64 last_transfer_ns;
Rhyland Klein01e73c82013-12-09 12:36:09 +010087 unsigned int end_of_msg_delay;
Doug Anderson362196e2014-04-30 10:44:05 -070088 struct mutex lock;
Simon Glassa17d94f2013-02-25 14:08:39 -080089};
90
91static void debug_packet(struct device *dev, const char *name, u8 *ptr,
92 int len)
93{
94#ifdef DEBUG
95 int i;
96
97 dev_dbg(dev, "%s: ", name);
98 for (i = 0; i < len; i++)
Thierry Reding9e146f42013-11-18 11:30:49 +010099 pr_cont(" %02x", ptr[i]);
100
101 pr_cont("\n");
Simon Glassa17d94f2013-02-25 14:08:39 -0800102#endif
103}
104
105/**
106 * cros_ec_spi_receive_response - Receive a response from the EC.
107 *
108 * This function has two phases: reading the preamble bytes (since if we read
109 * data from the EC before it is ready to send, we just get preamble) and
110 * reading the actual message.
111 *
112 * The received data is placed into ec_dev->din.
113 *
114 * @ec_dev: ChromeOS EC device
115 * @need_len: Number of message bytes we need to read
116 */
117static int cros_ec_spi_receive_response(struct cros_ec_device *ec_dev,
118 int need_len)
119{
120 struct cros_ec_spi *ec_spi = ec_dev->priv;
121 struct spi_transfer trans;
122 struct spi_message msg;
123 u8 *ptr, *end;
124 int ret;
125 unsigned long deadline;
126 int todo;
127
128 /* Receive data until we see the header byte */
129 deadline = jiffies + msecs_to_jiffies(EC_MSG_DEADLINE_MS);
Doug Andersonc9a81d62014-04-30 10:44:06 -0700130 while (true) {
131 unsigned long start_jiffies = jiffies;
132
Thierry Redingdaf93d22013-12-09 12:36:10 +0100133 memset(&trans, 0, sizeof(trans));
Simon Glassa17d94f2013-02-25 14:08:39 -0800134 trans.cs_change = 1;
135 trans.rx_buf = ptr = ec_dev->din;
136 trans.len = EC_MSG_PREAMBLE_COUNT;
137
138 spi_message_init(&msg);
139 spi_message_add_tail(&trans, &msg);
140 ret = spi_sync(ec_spi->spi, &msg);
141 if (ret < 0) {
142 dev_err(ec_dev->dev, "spi transfer failed: %d\n", ret);
143 return ret;
144 }
145
146 for (end = ptr + EC_MSG_PREAMBLE_COUNT; ptr != end; ptr++) {
147 if (*ptr == EC_MSG_HEADER) {
Geert Uytterhoevenc34924b2013-05-08 23:37:45 +0200148 dev_dbg(ec_dev->dev, "msg found at %zd\n",
Simon Glassa17d94f2013-02-25 14:08:39 -0800149 ptr - ec_dev->din);
150 break;
151 }
152 }
Doug Andersonc9a81d62014-04-30 10:44:06 -0700153 if (ptr != end)
154 break;
Simon Glassa17d94f2013-02-25 14:08:39 -0800155
Doug Andersonc9a81d62014-04-30 10:44:06 -0700156 /*
157 * Use the time at the start of the loop as a timeout. This
158 * gives us one last shot at getting the transfer and is useful
159 * in case we got context switched out for a while.
160 */
161 if (time_after(start_jiffies, deadline)) {
Simon Glassa17d94f2013-02-25 14:08:39 -0800162 dev_warn(ec_dev->dev, "EC failed to respond in time\n");
163 return -ETIMEDOUT;
164 }
Doug Andersonc9a81d62014-04-30 10:44:06 -0700165 }
Simon Glassa17d94f2013-02-25 14:08:39 -0800166
167 /*
168 * ptr now points to the header byte. Copy any valid data to the
169 * start of our buffer
170 */
171 todo = end - ++ptr;
172 BUG_ON(todo < 0 || todo > ec_dev->din_size);
173 todo = min(todo, need_len);
174 memmove(ec_dev->din, ptr, todo);
175 ptr = ec_dev->din + todo;
176 dev_dbg(ec_dev->dev, "need %d, got %d bytes from preamble\n",
177 need_len, todo);
178 need_len -= todo;
179
180 /* Receive data until we have it all */
181 while (need_len > 0) {
182 /*
183 * We can't support transfers larger than the SPI FIFO size
184 * unless we have DMA. We don't have DMA on the ISP SPI ports
185 * for Exynos. We need a way of asking SPI driver for
186 * maximum-supported transfer size.
187 */
188 todo = min(need_len, 256);
Geert Uytterhoevenc34924b2013-05-08 23:37:45 +0200189 dev_dbg(ec_dev->dev, "loop, todo=%d, need_len=%d, ptr=%zd\n",
Simon Glassa17d94f2013-02-25 14:08:39 -0800190 todo, need_len, ptr - ec_dev->din);
191
Thierry Redingdaf93d22013-12-09 12:36:10 +0100192 memset(&trans, 0, sizeof(trans));
Simon Glassa17d94f2013-02-25 14:08:39 -0800193 trans.cs_change = 1;
194 trans.rx_buf = ptr;
195 trans.len = todo;
196 spi_message_init(&msg);
197 spi_message_add_tail(&trans, &msg);
198
199 /* send command to EC and read answer */
200 BUG_ON((u8 *)trans.rx_buf - ec_dev->din + todo >
201 ec_dev->din_size);
202 ret = spi_sync(ec_spi->spi, &msg);
203 if (ret < 0) {
204 dev_err(ec_dev->dev, "spi transfer failed: %d\n", ret);
205 return ret;
206 }
207
208 debug_packet(ec_dev->dev, "interim", ptr, todo);
209 ptr += todo;
210 need_len -= todo;
211 }
212
Geert Uytterhoevenc34924b2013-05-08 23:37:45 +0200213 dev_dbg(ec_dev->dev, "loop done, ptr=%zd\n", ptr - ec_dev->din);
Simon Glassa17d94f2013-02-25 14:08:39 -0800214
215 return 0;
216}
217
218/**
Bill Richardson7e6cb5b2014-06-18 11:14:00 -0700219 * cros_ec_cmd_xfer_spi - Transfer a message over SPI and receive the reply
Simon Glassa17d94f2013-02-25 14:08:39 -0800220 *
221 * @ec_dev: ChromeOS EC device
222 * @ec_msg: Message to transfer
223 */
Bill Richardson7e6cb5b2014-06-18 11:14:00 -0700224static int cros_ec_cmd_xfer_spi(struct cros_ec_device *ec_dev,
Bill Richardson5d4773e2014-06-18 11:14:02 -0700225 struct cros_ec_command *ec_msg)
Simon Glassa17d94f2013-02-25 14:08:39 -0800226{
227 struct cros_ec_spi *ec_spi = ec_dev->priv;
228 struct spi_transfer trans;
229 struct spi_message msg;
230 int i, len;
231 u8 *ptr;
232 int sum;
233 int ret = 0, final_ret;
Simon Glassa17d94f2013-02-25 14:08:39 -0800234
Doug Anderson362196e2014-04-30 10:44:05 -0700235 /*
236 * We have the shared ec_dev buffer plus we do lots of separate spi_sync
237 * calls, so we need to make sure only one person is using this at a
238 * time.
239 */
240 mutex_lock(&ec_spi->lock);
241
Simon Glassa17d94f2013-02-25 14:08:39 -0800242 len = cros_ec_prepare_tx(ec_dev, ec_msg);
243 dev_dbg(ec_dev->dev, "prepared, len=%d\n", len);
244
245 /* If it's too soon to do another transaction, wait */
246 if (ec_spi->last_transfer_ns) {
Simon Glassa17d94f2013-02-25 14:08:39 -0800247 unsigned long delay; /* The delay completed so far */
248
Thomas Gleixner154adc12014-07-16 21:04:41 +0000249 delay = ktime_get_ns() - ec_spi->last_transfer_ns;
Simon Glassa17d94f2013-02-25 14:08:39 -0800250 if (delay < EC_SPI_RECOVERY_TIME_NS)
David Hendricks1fe36862014-04-30 10:44:04 -0700251 ndelay(EC_SPI_RECOVERY_TIME_NS - delay);
Simon Glassa17d94f2013-02-25 14:08:39 -0800252 }
253
254 /* Transmit phase - send our message */
255 debug_packet(ec_dev->dev, "out", ec_dev->dout, len);
Thierry Redingdaf93d22013-12-09 12:36:10 +0100256 memset(&trans, 0, sizeof(trans));
Simon Glassa17d94f2013-02-25 14:08:39 -0800257 trans.tx_buf = ec_dev->dout;
258 trans.len = len;
259 trans.cs_change = 1;
260 spi_message_init(&msg);
261 spi_message_add_tail(&trans, &msg);
262 ret = spi_sync(ec_spi->spi, &msg);
263
264 /* Get the response */
265 if (!ret) {
266 ret = cros_ec_spi_receive_response(ec_dev,
Bill Richardson5d4773e2014-06-18 11:14:02 -0700267 ec_msg->insize + EC_MSG_TX_PROTO_BYTES);
Simon Glassa17d94f2013-02-25 14:08:39 -0800268 } else {
269 dev_err(ec_dev->dev, "spi transfer failed: %d\n", ret);
270 }
271
Doug Anderson96758052014-06-13 11:13:32 -0700272 /*
273 * Turn off CS, possibly adding a delay to ensure the rising edge
274 * doesn't come too soon after the end of the data.
275 */
Simon Glassa17d94f2013-02-25 14:08:39 -0800276 spi_message_init(&msg);
Doug Anderson96758052014-06-13 11:13:32 -0700277 memset(&trans, 0, sizeof(trans));
278 trans.delay_usecs = ec_spi->end_of_msg_delay;
279 spi_message_add_tail(&trans, &msg);
Rhyland Klein01e73c82013-12-09 12:36:09 +0100280
Simon Glassa17d94f2013-02-25 14:08:39 -0800281 final_ret = spi_sync(ec_spi->spi, &msg);
Thomas Gleixner154adc12014-07-16 21:04:41 +0000282 ec_spi->last_transfer_ns = ktime_get_ns();
Simon Glassa17d94f2013-02-25 14:08:39 -0800283 if (!ret)
284 ret = final_ret;
285 if (ret < 0) {
286 dev_err(ec_dev->dev, "spi transfer failed: %d\n", ret);
Doug Anderson362196e2014-04-30 10:44:05 -0700287 goto exit;
Simon Glassa17d94f2013-02-25 14:08:39 -0800288 }
289
Simon Glassa17d94f2013-02-25 14:08:39 -0800290 ptr = ec_dev->din;
Bill Richardson6db07b62014-06-18 11:14:05 -0700291
292 /* check response error code */
293 ec_msg->result = ptr[0];
294 ret = cros_ec_check_result(ec_dev, ec_msg);
295 if (ret)
Doug Anderson362196e2014-04-30 10:44:05 -0700296 goto exit;
Bill Richardson6db07b62014-06-18 11:14:05 -0700297
Simon Glassa17d94f2013-02-25 14:08:39 -0800298 len = ptr[1];
299 sum = ptr[0] + ptr[1];
Bill Richardson5d4773e2014-06-18 11:14:02 -0700300 if (len > ec_msg->insize) {
Simon Glassa17d94f2013-02-25 14:08:39 -0800301 dev_err(ec_dev->dev, "packet too long (%d bytes, expected %d)",
Bill Richardson5d4773e2014-06-18 11:14:02 -0700302 len, ec_msg->insize);
Doug Anderson362196e2014-04-30 10:44:05 -0700303 ret = -ENOSPC;
304 goto exit;
Simon Glassa17d94f2013-02-25 14:08:39 -0800305 }
306
307 /* copy response packet payload and compute checksum */
308 for (i = 0; i < len; i++) {
309 sum += ptr[i + 2];
Bill Richardson5d4773e2014-06-18 11:14:02 -0700310 if (ec_msg->insize)
311 ec_msg->indata[i] = ptr[i + 2];
Simon Glassa17d94f2013-02-25 14:08:39 -0800312 }
313 sum &= 0xff;
314
315 debug_packet(ec_dev->dev, "in", ptr, len + 3);
316
317 if (sum != ptr[len + 2]) {
318 dev_err(ec_dev->dev,
319 "bad packet checksum, expected %02x, got %02x\n",
320 sum, ptr[len + 2]);
Doug Anderson362196e2014-04-30 10:44:05 -0700321 ret = -EBADMSG;
322 goto exit;
Simon Glassa17d94f2013-02-25 14:08:39 -0800323 }
324
Bill Richardson12ebc8a2014-06-18 11:14:06 -0700325 ret = len;
Doug Anderson362196e2014-04-30 10:44:05 -0700326exit:
Doug Anderson659e1422014-09-18 17:18:54 +0200327 if (ec_msg->command == EC_CMD_REBOOT_EC)
328 msleep(EC_REBOOT_DELAY_MS);
329
Doug Anderson362196e2014-04-30 10:44:05 -0700330 mutex_unlock(&ec_spi->lock);
331 return ret;
Simon Glassa17d94f2013-02-25 14:08:39 -0800332}
333
Rhyland Klein01e73c82013-12-09 12:36:09 +0100334static void cros_ec_spi_dt_probe(struct cros_ec_spi *ec_spi, struct device *dev)
335{
336 struct device_node *np = dev->of_node;
337 u32 val;
338 int ret;
339
340 ret = of_property_read_u32(np, "google,cros-ec-spi-msg-delay", &val);
341 if (!ret)
342 ec_spi->end_of_msg_delay = val;
343}
344
Thierry Reding9922b412013-11-25 13:22:30 +0100345static int cros_ec_spi_probe(struct spi_device *spi)
Simon Glassa17d94f2013-02-25 14:08:39 -0800346{
347 struct device *dev = &spi->dev;
348 struct cros_ec_device *ec_dev;
349 struct cros_ec_spi *ec_spi;
350 int err;
351
352 spi->bits_per_word = 8;
353 spi->mode = SPI_MODE_0;
354 err = spi_setup(spi);
355 if (err < 0)
356 return err;
357
358 ec_spi = devm_kzalloc(dev, sizeof(*ec_spi), GFP_KERNEL);
359 if (ec_spi == NULL)
360 return -ENOMEM;
361 ec_spi->spi = spi;
Doug Anderson362196e2014-04-30 10:44:05 -0700362 mutex_init(&ec_spi->lock);
Simon Glassa17d94f2013-02-25 14:08:39 -0800363 ec_dev = devm_kzalloc(dev, sizeof(*ec_dev), GFP_KERNEL);
364 if (!ec_dev)
365 return -ENOMEM;
366
Rhyland Klein01e73c82013-12-09 12:36:09 +0100367 /* Check for any DT properties */
368 cros_ec_spi_dt_probe(ec_spi, dev);
369
Simon Glassa17d94f2013-02-25 14:08:39 -0800370 spi_set_drvdata(spi, ec_dev);
Simon Glassa17d94f2013-02-25 14:08:39 -0800371 ec_dev->dev = dev;
372 ec_dev->priv = ec_spi;
373 ec_dev->irq = spi->irq;
Bill Richardson7e6cb5b2014-06-18 11:14:00 -0700374 ec_dev->cmd_xfer = cros_ec_cmd_xfer_spi;
Simon Glassa17d94f2013-02-25 14:08:39 -0800375 ec_dev->ec_name = ec_spi->spi->modalias;
376 ec_dev->phys_name = dev_name(&ec_spi->spi->dev);
377 ec_dev->parent = &ec_spi->spi->dev;
378 ec_dev->din_size = EC_MSG_BYTES + EC_MSG_PREAMBLE_COUNT;
379 ec_dev->dout_size = EC_MSG_BYTES;
380
381 err = cros_ec_register(ec_dev);
382 if (err) {
383 dev_err(dev, "cannot register EC\n");
384 return err;
385 }
386
Prathyush Kfb504972014-06-16 14:12:23 -0700387 device_init_wakeup(&spi->dev, true);
388
Simon Glassa17d94f2013-02-25 14:08:39 -0800389 return 0;
390}
391
Thierry Reding9922b412013-11-25 13:22:30 +0100392static int cros_ec_spi_remove(struct spi_device *spi)
Simon Glassa17d94f2013-02-25 14:08:39 -0800393{
394 struct cros_ec_device *ec_dev;
395
396 ec_dev = spi_get_drvdata(spi);
397 cros_ec_remove(ec_dev);
398
399 return 0;
400}
401
402#ifdef CONFIG_PM_SLEEP
403static int cros_ec_spi_suspend(struct device *dev)
404{
405 struct cros_ec_device *ec_dev = dev_get_drvdata(dev);
406
407 return cros_ec_suspend(ec_dev);
408}
409
410static int cros_ec_spi_resume(struct device *dev)
411{
412 struct cros_ec_device *ec_dev = dev_get_drvdata(dev);
413
414 return cros_ec_resume(ec_dev);
415}
416#endif
417
418static SIMPLE_DEV_PM_OPS(cros_ec_spi_pm_ops, cros_ec_spi_suspend,
419 cros_ec_spi_resume);
420
421static const struct spi_device_id cros_ec_spi_id[] = {
422 { "cros-ec-spi", 0 },
423 { }
424};
425MODULE_DEVICE_TABLE(spi, cros_ec_spi_id);
426
427static struct spi_driver cros_ec_driver_spi = {
428 .driver = {
429 .name = "cros-ec-spi",
430 .owner = THIS_MODULE,
431 .pm = &cros_ec_spi_pm_ops,
432 },
Thierry Reding9922b412013-11-25 13:22:30 +0100433 .probe = cros_ec_spi_probe,
434 .remove = cros_ec_spi_remove,
Simon Glassa17d94f2013-02-25 14:08:39 -0800435 .id_table = cros_ec_spi_id,
436};
437
438module_spi_driver(cros_ec_driver_spi);
439
Thierry Redingea0f8b02013-12-09 11:05:38 +0100440MODULE_LICENSE("GPL v2");
Simon Glassa17d94f2013-02-25 14:08:39 -0800441MODULE_DESCRIPTION("ChromeOS EC multi function device (SPI)");