Hung-Te Lin | 0ee7062 | 2013-06-26 19:42:12 +0800 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright (C) 2013 Google Inc. All rights reserved. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; version 2 of the License |
| 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. |
Hung-Te Lin | 0ee7062 | 2013-06-26 19:42:12 +0800 | [diff] [blame] | 14 | */ |
| 15 | |
| 16 | #include <console/console.h> |
David Hendricks | 767d245 | 2014-09-27 20:04:49 -0700 | [diff] [blame] | 17 | #include <delay.h> |
Hung-Te Lin | 0ee7062 | 2013-06-26 19:42:12 +0800 | [diff] [blame] | 18 | #include "ec.h" |
| 19 | #include "ec_commands.h" |
Gabe Black | 967058f | 2014-03-21 21:32:12 -0700 | [diff] [blame] | 20 | #include <spi-generic.h> |
| 21 | #include <timer.h> |
| 22 | |
Julius Werner | 93b51e7 | 2016-10-03 12:52:55 -0700 | [diff] [blame] | 23 | /* This is assuming that this driver is not used on x86. If that changes, this |
| 24 | might need to become a CAR_GLOBAL or maybe even more complicated. */ |
| 25 | static struct stopwatch cs_cooldown_sw; |
| 26 | static const long cs_cooldown_us = 200; |
| 27 | |
Gabe Black | 967058f | 2014-03-21 21:32:12 -0700 | [diff] [blame] | 28 | static const uint8_t EcFramingByte = 0xec; |
Hung-Te Lin | 0ee7062 | 2013-06-26 19:42:12 +0800 | [diff] [blame] | 29 | |
Aaron Durbin | 8282727 | 2014-08-06 14:34:57 -0500 | [diff] [blame] | 30 | #define PROTO3_MAX_PACKET_SIZE 268 |
| 31 | |
| 32 | static uint8_t req_buf[PROTO3_MAX_PACKET_SIZE]; |
| 33 | static uint8_t resp_buf[PROTO3_MAX_PACKET_SIZE]; |
| 34 | |
| 35 | void *crosec_get_buffer(size_t size, int req) |
| 36 | { |
| 37 | if (size > PROTO3_MAX_PACKET_SIZE) { |
| 38 | printk(BIOS_DEBUG, "Proto v3 buffer request too large: %zu!\n", |
| 39 | size); |
| 40 | return NULL; |
| 41 | } |
| 42 | |
| 43 | if (req) |
| 44 | return req_buf; |
| 45 | else |
| 46 | return resp_buf; |
| 47 | } |
| 48 | |
| 49 | static int crosec_spi_io(size_t req_size, size_t resp_size, void *context) |
Hung-Te Lin | 0ee7062 | 2013-06-26 19:42:12 +0800 | [diff] [blame] | 50 | { |
| 51 | struct spi_slave *slave = (struct spi_slave *)context; |
Julius Werner | 93b51e7 | 2016-10-03 12:52:55 -0700 | [diff] [blame] | 52 | int ret = 0; |
Hung-Te Lin | 0ee7062 | 2013-06-26 19:42:12 +0800 | [diff] [blame] | 53 | |
Jonathan Neuschäfer | c966075 | 2017-09-19 15:19:54 +0200 | [diff] [blame] | 54 | /* Wait minimum delay between CS assertions. */ |
| 55 | stopwatch_wait_until_expired(&cs_cooldown_sw); |
| 56 | |
Hung-Te Lin | 0ee7062 | 2013-06-26 19:42:12 +0800 | [diff] [blame] | 57 | spi_claim_bus(slave); |
Hung-Te Lin | 0ee7062 | 2013-06-26 19:42:12 +0800 | [diff] [blame] | 58 | |
David Hendricks | 767d245 | 2014-09-27 20:04:49 -0700 | [diff] [blame] | 59 | /* Allow EC to ramp up clock after being awaken. |
| 60 | * See chrome-os-partner:32223 for more details. */ |
| 61 | udelay(CONFIG_EC_GOOGLE_CHROMEEC_SPI_WAKEUP_DELAY_US); |
| 62 | |
Aaron Durbin | 8282727 | 2014-08-06 14:34:57 -0500 | [diff] [blame] | 63 | if (spi_xfer(slave, req_buf, req_size, NULL, 0)) { |
Gabe Black | 967058f | 2014-03-21 21:32:12 -0700 | [diff] [blame] | 64 | printk(BIOS_ERR, "%s: Failed to send request.\n", __func__); |
Julius Werner | 93b51e7 | 2016-10-03 12:52:55 -0700 | [diff] [blame] | 65 | ret = -1; |
| 66 | goto out; |
Hung-Te Lin | 0ee7062 | 2013-06-26 19:42:12 +0800 | [diff] [blame] | 67 | } |
| 68 | |
Gabe Black | 967058f | 2014-03-21 21:32:12 -0700 | [diff] [blame] | 69 | uint8_t byte; |
Aaron Durbin | 4869111 | 2014-09-23 16:30:53 -0500 | [diff] [blame] | 70 | struct stopwatch sw; |
| 71 | // Wait 1s for a framing byte. |
| 72 | stopwatch_init_usecs_expire(&sw, USECS_PER_SEC); |
Gabe Black | 967058f | 2014-03-21 21:32:12 -0700 | [diff] [blame] | 73 | while (1) { |
| 74 | if (spi_xfer(slave, NULL, 0, &byte, sizeof(byte))) { |
| 75 | printk(BIOS_ERR, "%s: Failed to receive byte.\n", |
| 76 | __func__); |
Julius Werner | 93b51e7 | 2016-10-03 12:52:55 -0700 | [diff] [blame] | 77 | ret = -1; |
| 78 | goto out; |
Gabe Black | 967058f | 2014-03-21 21:32:12 -0700 | [diff] [blame] | 79 | } |
| 80 | if (byte == EcFramingByte) |
| 81 | break; |
| 82 | |
Aaron Durbin | 4869111 | 2014-09-23 16:30:53 -0500 | [diff] [blame] | 83 | if (stopwatch_expired(&sw)) { |
Gabe Black | 967058f | 2014-03-21 21:32:12 -0700 | [diff] [blame] | 84 | printk(BIOS_ERR, |
| 85 | "%s: Timeout waiting for framing byte.\n", |
| 86 | __func__); |
Julius Werner | 93b51e7 | 2016-10-03 12:52:55 -0700 | [diff] [blame] | 87 | ret = -1; |
| 88 | goto out; |
Gabe Black | 967058f | 2014-03-21 21:32:12 -0700 | [diff] [blame] | 89 | } |
| 90 | } |
| 91 | |
Aaron Durbin | 8282727 | 2014-08-06 14:34:57 -0500 | [diff] [blame] | 92 | if (spi_xfer(slave, NULL, 0, resp_buf, resp_size)) { |
Gabe Black | 967058f | 2014-03-21 21:32:12 -0700 | [diff] [blame] | 93 | printk(BIOS_ERR, "%s: Failed to receive response.\n", __func__); |
Julius Werner | 93b51e7 | 2016-10-03 12:52:55 -0700 | [diff] [blame] | 94 | ret = -1; |
Gabe Black | 967058f | 2014-03-21 21:32:12 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Julius Werner | 93b51e7 | 2016-10-03 12:52:55 -0700 | [diff] [blame] | 97 | out: |
Gabe Black | 967058f | 2014-03-21 21:32:12 -0700 | [diff] [blame] | 98 | spi_release_bus(slave); |
Julius Werner | 93b51e7 | 2016-10-03 12:52:55 -0700 | [diff] [blame] | 99 | stopwatch_init_usecs_expire(&cs_cooldown_sw, cs_cooldown_us); |
| 100 | return ret; |
Hung-Te Lin | 0ee7062 | 2013-06-26 19:42:12 +0800 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | int google_chromeec_command(struct chromeec_command *cec_command) |
| 104 | { |
Furquan Shaikh | 36b81af | 2016-12-01 01:02:44 -0800 | [diff] [blame] | 105 | static int done = 0; |
| 106 | static struct spi_slave slave; |
| 107 | |
| 108 | if (!done) { |
| 109 | if (spi_setup_slave(CONFIG_EC_GOOGLE_CHROMEEC_SPI_BUS, |
| 110 | CONFIG_EC_GOOGLE_CHROMEEC_SPI_CHIP, &slave)) |
| 111 | return -1; |
Julius Werner | 93b51e7 | 2016-10-03 12:52:55 -0700 | [diff] [blame] | 112 | stopwatch_init(&cs_cooldown_sw); |
Furquan Shaikh | 36b81af | 2016-12-01 01:02:44 -0800 | [diff] [blame] | 113 | done = 1; |
Julius Werner | 93b51e7 | 2016-10-03 12:52:55 -0700 | [diff] [blame] | 114 | } |
Furquan Shaikh | 36b81af | 2016-12-01 01:02:44 -0800 | [diff] [blame] | 115 | return crosec_command_proto(cec_command, crosec_spi_io, &slave); |
Hung-Te Lin | 0ee7062 | 2013-06-26 19:42:12 +0800 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | #ifndef __PRE_RAM__ |
| 119 | u8 google_chromeec_get_event(void) |
| 120 | { |
| 121 | printk(BIOS_ERR, "%s: Not supported.\n", __func__); |
| 122 | return 0; |
| 123 | } |
| 124 | #endif |