11058ca94SEnric Balletbo i Serra // SPDX-License-Identifier: GPL-2.0
21058ca94SEnric Balletbo i Serra // LPC interface for ChromeOS Embedded Controller
31058ca94SEnric Balletbo i Serra //
41058ca94SEnric Balletbo i Serra // Copyright (C) 2012-2015 Google, Inc
51058ca94SEnric Balletbo i Serra //
61058ca94SEnric Balletbo i Serra // This driver uses the ChromeOS EC byte-level message-based protocol for
71058ca94SEnric Balletbo i Serra // communicating the keyboard state (which keys are pressed) from a keyboard EC
81058ca94SEnric Balletbo i Serra // to the AP over some bus (such as i2c, lpc, spi). The EC does debouncing,
91058ca94SEnric Balletbo i Serra // but everything else (including deghosting) is done here. The main
101058ca94SEnric Balletbo i Serra // motivation for this is to keep the EC firmware as simple as possible, since
111058ca94SEnric Balletbo i Serra // it cannot be easily upgraded and EC flash/IRAM space is relatively
121058ca94SEnric Balletbo i Serra // expensive.
13ec2f33abSBill Richardson
1412278dc7SGwendal Grignou #include <linux/acpi.h>
15ec2f33abSBill Richardson #include <linux/dmi.h>
16ec2f33abSBill Richardson #include <linux/delay.h>
1718d0dc24SJavier Martinez Canillas #include <linux/io.h>
18da1cf5a1SEnrico Granata #include <linux/interrupt.h>
192cbf475aSRob Barnes #include <linux/kobject.h>
20ec2f33abSBill Richardson #include <linux/module.h>
21840d9f13SEnric Balletbo i Serra #include <linux/platform_data/cros_ec_commands.h>
22840d9f13SEnric Balletbo i Serra #include <linux/platform_data/cros_ec_proto.h>
23ec2f33abSBill Richardson #include <linux/platform_device.h>
24ec2f33abSBill Richardson #include <linux/printk.h>
25957445d7SRob Barnes #include <linux/reboot.h>
26d6542b39SWenkai Du #include <linux/suspend.h>
27ec2f33abSBill Richardson
28034dbec1SEnric Balletbo i Serra #include "cros_ec.h"
294116fd25SEnric Balletbo i Serra #include "cros_ec_lpc_mec.h"
30cc8a4ea1SEnric Balletbo i Serra
31bce70fefSShawn Nematbakhsh #define DRV_NAME "cros_ec_lpcs"
3212278dc7SGwendal Grignou #define ACPI_DRV_NAME "GOOG0004"
33ec2f33abSBill Richardson
345f454bdfSEnric Balletbo i Serra /* True if ACPI device is present */
355f454bdfSEnric Balletbo i Serra static bool cros_ec_lpc_acpi_device_found;
365f454bdfSEnric Balletbo i Serra
3722c040faSEnric Balletbo i Serra /**
3822c040faSEnric Balletbo i Serra * struct lpc_driver_ops - LPC driver operations
3922c040faSEnric Balletbo i Serra * @read: Copy length bytes from EC address offset into buffer dest. Returns
4022c040faSEnric Balletbo i Serra * the 8-bit checksum of all bytes read.
4122c040faSEnric Balletbo i Serra * @write: Copy length bytes from buffer msg into EC address offset. Returns
4222c040faSEnric Balletbo i Serra * the 8-bit checksum of all bytes written.
4322c040faSEnric Balletbo i Serra */
4422c040faSEnric Balletbo i Serra struct lpc_driver_ops {
4522c040faSEnric Balletbo i Serra u8 (*read)(unsigned int offset, unsigned int length, u8 *dest);
4622c040faSEnric Balletbo i Serra u8 (*write)(unsigned int offset, unsigned int length, const u8 *msg);
4722c040faSEnric Balletbo i Serra };
4822c040faSEnric Balletbo i Serra
4922c040faSEnric Balletbo i Serra static struct lpc_driver_ops cros_ec_lpc_ops = { };
5022c040faSEnric Balletbo i Serra
5122c040faSEnric Balletbo i Serra /*
5222c040faSEnric Balletbo i Serra * A generic instance of the read function of struct lpc_driver_ops, used for
5322c040faSEnric Balletbo i Serra * the LPC EC.
5422c040faSEnric Balletbo i Serra */
cros_ec_lpc_read_bytes(unsigned int offset,unsigned int length,u8 * dest)5522c040faSEnric Balletbo i Serra static u8 cros_ec_lpc_read_bytes(unsigned int offset, unsigned int length,
5622c040faSEnric Balletbo i Serra u8 *dest)
574116fd25SEnric Balletbo i Serra {
584116fd25SEnric Balletbo i Serra int sum = 0;
594116fd25SEnric Balletbo i Serra int i;
604116fd25SEnric Balletbo i Serra
614116fd25SEnric Balletbo i Serra for (i = 0; i < length; ++i) {
624116fd25SEnric Balletbo i Serra dest[i] = inb(offset + i);
634116fd25SEnric Balletbo i Serra sum += dest[i];
644116fd25SEnric Balletbo i Serra }
654116fd25SEnric Balletbo i Serra
664116fd25SEnric Balletbo i Serra /* Return checksum of all bytes read */
674116fd25SEnric Balletbo i Serra return sum;
684116fd25SEnric Balletbo i Serra }
694116fd25SEnric Balletbo i Serra
7022c040faSEnric Balletbo i Serra /*
7122c040faSEnric Balletbo i Serra * A generic instance of the write function of struct lpc_driver_ops, used for
7222c040faSEnric Balletbo i Serra * the LPC EC.
7322c040faSEnric Balletbo i Serra */
cros_ec_lpc_write_bytes(unsigned int offset,unsigned int length,const u8 * msg)7422c040faSEnric Balletbo i Serra static u8 cros_ec_lpc_write_bytes(unsigned int offset, unsigned int length,
7522c040faSEnric Balletbo i Serra const u8 *msg)
764116fd25SEnric Balletbo i Serra {
774116fd25SEnric Balletbo i Serra int sum = 0;
784116fd25SEnric Balletbo i Serra int i;
794116fd25SEnric Balletbo i Serra
804116fd25SEnric Balletbo i Serra for (i = 0; i < length; ++i) {
814116fd25SEnric Balletbo i Serra outb(msg[i], offset + i);
824116fd25SEnric Balletbo i Serra sum += msg[i];
834116fd25SEnric Balletbo i Serra }
844116fd25SEnric Balletbo i Serra
854116fd25SEnric Balletbo i Serra /* Return checksum of all bytes written */
864116fd25SEnric Balletbo i Serra return sum;
874116fd25SEnric Balletbo i Serra }
884116fd25SEnric Balletbo i Serra
8922c040faSEnric Balletbo i Serra /*
9022c040faSEnric Balletbo i Serra * An instance of the read function of struct lpc_driver_ops, used for the
9122c040faSEnric Balletbo i Serra * MEC variant of LPC EC.
9222c040faSEnric Balletbo i Serra */
cros_ec_lpc_mec_read_bytes(unsigned int offset,unsigned int length,u8 * dest)9322c040faSEnric Balletbo i Serra static u8 cros_ec_lpc_mec_read_bytes(unsigned int offset, unsigned int length,
944116fd25SEnric Balletbo i Serra u8 *dest)
954116fd25SEnric Balletbo i Serra {
964116fd25SEnric Balletbo i Serra int in_range = cros_ec_lpc_mec_in_range(offset, length);
974116fd25SEnric Balletbo i Serra
984116fd25SEnric Balletbo i Serra if (in_range < 0)
994116fd25SEnric Balletbo i Serra return 0;
1004116fd25SEnric Balletbo i Serra
1014116fd25SEnric Balletbo i Serra return in_range ?
1024116fd25SEnric Balletbo i Serra cros_ec_lpc_io_bytes_mec(MEC_IO_READ,
1034116fd25SEnric Balletbo i Serra offset - EC_HOST_CMD_REGION0,
1044116fd25SEnric Balletbo i Serra length, dest) :
10522c040faSEnric Balletbo i Serra cros_ec_lpc_read_bytes(offset, length, dest);
1064116fd25SEnric Balletbo i Serra }
1074116fd25SEnric Balletbo i Serra
10822c040faSEnric Balletbo i Serra /*
10922c040faSEnric Balletbo i Serra * An instance of the write function of struct lpc_driver_ops, used for the
11022c040faSEnric Balletbo i Serra * MEC variant of LPC EC.
11122c040faSEnric Balletbo i Serra */
cros_ec_lpc_mec_write_bytes(unsigned int offset,unsigned int length,const u8 * msg)11222c040faSEnric Balletbo i Serra static u8 cros_ec_lpc_mec_write_bytes(unsigned int offset, unsigned int length,
11322c040faSEnric Balletbo i Serra const u8 *msg)
1144116fd25SEnric Balletbo i Serra {
1154116fd25SEnric Balletbo i Serra int in_range = cros_ec_lpc_mec_in_range(offset, length);
1164116fd25SEnric Balletbo i Serra
1174116fd25SEnric Balletbo i Serra if (in_range < 0)
1184116fd25SEnric Balletbo i Serra return 0;
1194116fd25SEnric Balletbo i Serra
1204116fd25SEnric Balletbo i Serra return in_range ?
1214116fd25SEnric Balletbo i Serra cros_ec_lpc_io_bytes_mec(MEC_IO_WRITE,
1224116fd25SEnric Balletbo i Serra offset - EC_HOST_CMD_REGION0,
12322c040faSEnric Balletbo i Serra length, (u8 *)msg) :
12422c040faSEnric Balletbo i Serra cros_ec_lpc_write_bytes(offset, length, msg);
1254116fd25SEnric Balletbo i Serra }
1264116fd25SEnric Balletbo i Serra
ec_response_timed_out(void)127ec2f33abSBill Richardson static int ec_response_timed_out(void)
128ec2f33abSBill Richardson {
129ec2f33abSBill Richardson unsigned long one_second = jiffies + HZ;
130bce70fefSShawn Nematbakhsh u8 data;
131ec2f33abSBill Richardson
132ec2f33abSBill Richardson usleep_range(200, 300);
133ec2f33abSBill Richardson do {
13422c040faSEnric Balletbo i Serra if (!(cros_ec_lpc_ops.read(EC_LPC_ADDR_HOST_CMD, 1, &data) &
135bce70fefSShawn Nematbakhsh EC_LPC_STATUS_BUSY_MASK))
136ec2f33abSBill Richardson return 0;
137ec2f33abSBill Richardson usleep_range(100, 200);
138ec2f33abSBill Richardson } while (time_before(jiffies, one_second));
139ec2f33abSBill Richardson
140ec2f33abSBill Richardson return 1;
141ec2f33abSBill Richardson }
142ec2f33abSBill Richardson
cros_ec_pkt_xfer_lpc(struct cros_ec_device * ec,struct cros_ec_command * msg)143d3654070SStephen Barber static int cros_ec_pkt_xfer_lpc(struct cros_ec_device *ec,
144d3654070SStephen Barber struct cros_ec_command *msg)
145d3654070SStephen Barber {
146d3654070SStephen Barber struct ec_host_response response;
147bce70fefSShawn Nematbakhsh u8 sum;
148d3654070SStephen Barber int ret = 0;
149d3654070SStephen Barber u8 *dout;
150d3654070SStephen Barber
151d3654070SStephen Barber ret = cros_ec_prepare_tx(ec, msg);
15271d3ae7fSTzung-Bi Shih if (ret < 0)
15371d3ae7fSTzung-Bi Shih goto done;
154d3654070SStephen Barber
155d3654070SStephen Barber /* Write buffer */
15622c040faSEnric Balletbo i Serra cros_ec_lpc_ops.write(EC_LPC_ADDR_HOST_PACKET, ret, ec->dout);
157d3654070SStephen Barber
158d3654070SStephen Barber /* Here we go */
159bce70fefSShawn Nematbakhsh sum = EC_COMMAND_PROTOCOL_3;
16022c040faSEnric Balletbo i Serra cros_ec_lpc_ops.write(EC_LPC_ADDR_HOST_CMD, 1, &sum);
161d3654070SStephen Barber
162d3654070SStephen Barber if (ec_response_timed_out()) {
163eb057514SColin Ian King dev_warn(ec->dev, "EC response timed out\n");
164d3654070SStephen Barber ret = -EIO;
165d3654070SStephen Barber goto done;
166d3654070SStephen Barber }
167d3654070SStephen Barber
168d3654070SStephen Barber /* Check result */
16922c040faSEnric Balletbo i Serra msg->result = cros_ec_lpc_ops.read(EC_LPC_ADDR_HOST_DATA, 1, &sum);
170d3654070SStephen Barber ret = cros_ec_check_result(ec, msg);
171d3654070SStephen Barber if (ret)
172d3654070SStephen Barber goto done;
173d3654070SStephen Barber
174d3654070SStephen Barber /* Read back response */
175d3654070SStephen Barber dout = (u8 *)&response;
17622c040faSEnric Balletbo i Serra sum = cros_ec_lpc_ops.read(EC_LPC_ADDR_HOST_PACKET, sizeof(response),
177bce70fefSShawn Nematbakhsh dout);
178d3654070SStephen Barber
179d3654070SStephen Barber msg->result = response.result;
180d3654070SStephen Barber
181d3654070SStephen Barber if (response.data_len > msg->insize) {
182d3654070SStephen Barber dev_err(ec->dev,
183d3654070SStephen Barber "packet too long (%d bytes, expected %d)",
184d3654070SStephen Barber response.data_len, msg->insize);
185d3654070SStephen Barber ret = -EMSGSIZE;
186d3654070SStephen Barber goto done;
187d3654070SStephen Barber }
188d3654070SStephen Barber
189d3654070SStephen Barber /* Read response and process checksum */
19022c040faSEnric Balletbo i Serra sum += cros_ec_lpc_ops.read(EC_LPC_ADDR_HOST_PACKET +
191bce70fefSShawn Nematbakhsh sizeof(response), response.data_len,
192bce70fefSShawn Nematbakhsh msg->data);
193d3654070SStephen Barber
194d3654070SStephen Barber if (sum) {
195d3654070SStephen Barber dev_err(ec->dev,
196d3654070SStephen Barber "bad packet checksum %02x\n",
197d3654070SStephen Barber response.checksum);
198d3654070SStephen Barber ret = -EBADMSG;
199d3654070SStephen Barber goto done;
200d3654070SStephen Barber }
201d3654070SStephen Barber
202d3654070SStephen Barber /* Return actual amount of data received */
203d3654070SStephen Barber ret = response.data_len;
204d3654070SStephen Barber done:
205d3654070SStephen Barber return ret;
206d3654070SStephen Barber }
207d3654070SStephen Barber
cros_ec_cmd_xfer_lpc(struct cros_ec_device * ec,struct cros_ec_command * msg)208ec2f33abSBill Richardson static int cros_ec_cmd_xfer_lpc(struct cros_ec_device *ec,
209ec2f33abSBill Richardson struct cros_ec_command *msg)
210ec2f33abSBill Richardson {
211ec2f33abSBill Richardson struct ec_lpc_host_args args;
212bce70fefSShawn Nematbakhsh u8 sum;
213ec2f33abSBill Richardson int ret = 0;
214ec2f33abSBill Richardson
215ec2f33abSBill Richardson if (msg->outsize > EC_PROTO2_MAX_PARAM_SIZE ||
216ec2f33abSBill Richardson msg->insize > EC_PROTO2_MAX_PARAM_SIZE) {
217ec2f33abSBill Richardson dev_err(ec->dev,
218ec2f33abSBill Richardson "invalid buffer sizes (out %d, in %d)\n",
219ec2f33abSBill Richardson msg->outsize, msg->insize);
220ec2f33abSBill Richardson return -EINVAL;
221ec2f33abSBill Richardson }
222ec2f33abSBill Richardson
223ec2f33abSBill Richardson /* Now actually send the command to the EC and get the result */
224ec2f33abSBill Richardson args.flags = EC_HOST_ARGS_FLAG_FROM_HOST;
225ec2f33abSBill Richardson args.command_version = msg->version;
226ec2f33abSBill Richardson args.data_size = msg->outsize;
227ec2f33abSBill Richardson
228ec2f33abSBill Richardson /* Initialize checksum */
229bce70fefSShawn Nematbakhsh sum = msg->command + args.flags + args.command_version + args.data_size;
230ec2f33abSBill Richardson
231ec2f33abSBill Richardson /* Copy data and update checksum */
23222c040faSEnric Balletbo i Serra sum += cros_ec_lpc_ops.write(EC_LPC_ADDR_HOST_PARAM, msg->outsize,
233bce70fefSShawn Nematbakhsh msg->data);
234ec2f33abSBill Richardson
235ec2f33abSBill Richardson /* Finalize checksum and write args */
236bce70fefSShawn Nematbakhsh args.checksum = sum;
23722c040faSEnric Balletbo i Serra cros_ec_lpc_ops.write(EC_LPC_ADDR_HOST_ARGS, sizeof(args),
238bce70fefSShawn Nematbakhsh (u8 *)&args);
239ec2f33abSBill Richardson
240ec2f33abSBill Richardson /* Here we go */
241bce70fefSShawn Nematbakhsh sum = msg->command;
24222c040faSEnric Balletbo i Serra cros_ec_lpc_ops.write(EC_LPC_ADDR_HOST_CMD, 1, &sum);
243ec2f33abSBill Richardson
244ec2f33abSBill Richardson if (ec_response_timed_out()) {
245eb057514SColin Ian King dev_warn(ec->dev, "EC response timed out\n");
246ec2f33abSBill Richardson ret = -EIO;
247ec2f33abSBill Richardson goto done;
248ec2f33abSBill Richardson }
249ec2f33abSBill Richardson
250ec2f33abSBill Richardson /* Check result */
25122c040faSEnric Balletbo i Serra msg->result = cros_ec_lpc_ops.read(EC_LPC_ADDR_HOST_DATA, 1, &sum);
252fbf40727SJavier Martinez Canillas ret = cros_ec_check_result(ec, msg);
253fbf40727SJavier Martinez Canillas if (ret)
254ec2f33abSBill Richardson goto done;
255ec2f33abSBill Richardson
256ec2f33abSBill Richardson /* Read back args */
25722c040faSEnric Balletbo i Serra cros_ec_lpc_ops.read(EC_LPC_ADDR_HOST_ARGS, sizeof(args), (u8 *)&args);
258ec2f33abSBill Richardson
259ec2f33abSBill Richardson if (args.data_size > msg->insize) {
260ec2f33abSBill Richardson dev_err(ec->dev,
261ec2f33abSBill Richardson "packet too long (%d bytes, expected %d)",
262ec2f33abSBill Richardson args.data_size, msg->insize);
263ec2f33abSBill Richardson ret = -ENOSPC;
264ec2f33abSBill Richardson goto done;
265ec2f33abSBill Richardson }
266ec2f33abSBill Richardson
267ec2f33abSBill Richardson /* Start calculating response checksum */
268bce70fefSShawn Nematbakhsh sum = msg->command + args.flags + args.command_version + args.data_size;
269ec2f33abSBill Richardson
270ec2f33abSBill Richardson /* Read response and update checksum */
27122c040faSEnric Balletbo i Serra sum += cros_ec_lpc_ops.read(EC_LPC_ADDR_HOST_PARAM, args.data_size,
272bce70fefSShawn Nematbakhsh msg->data);
273ec2f33abSBill Richardson
274ec2f33abSBill Richardson /* Verify checksum */
275bce70fefSShawn Nematbakhsh if (args.checksum != sum) {
276ec2f33abSBill Richardson dev_err(ec->dev,
277ec2f33abSBill Richardson "bad packet checksum, expected %02x, got %02x\n",
278bce70fefSShawn Nematbakhsh args.checksum, sum);
279ec2f33abSBill Richardson ret = -EBADMSG;
280ec2f33abSBill Richardson goto done;
281ec2f33abSBill Richardson }
282ec2f33abSBill Richardson
283ec2f33abSBill Richardson /* Return actual amount of data received */
284ec2f33abSBill Richardson ret = args.data_size;
285ec2f33abSBill Richardson done:
286ec2f33abSBill Richardson return ret;
287ec2f33abSBill Richardson }
288ec2f33abSBill Richardson
289ec2f33abSBill Richardson /* Returns num bytes read, or negative on error. Doesn't need locking. */
cros_ec_lpc_readmem(struct cros_ec_device * ec,unsigned int offset,unsigned int bytes,void * dest)290ec2f33abSBill Richardson static int cros_ec_lpc_readmem(struct cros_ec_device *ec, unsigned int offset,
291ec2f33abSBill Richardson unsigned int bytes, void *dest)
292ec2f33abSBill Richardson {
293ec2f33abSBill Richardson int i = offset;
294ec2f33abSBill Richardson char *s = dest;
295ec2f33abSBill Richardson int cnt = 0;
296ec2f33abSBill Richardson
297ec2f33abSBill Richardson if (offset >= EC_MEMMAP_SIZE - bytes)
298ec2f33abSBill Richardson return -EINVAL;
299ec2f33abSBill Richardson
300ec2f33abSBill Richardson /* fixed length */
301ec2f33abSBill Richardson if (bytes) {
30222c040faSEnric Balletbo i Serra cros_ec_lpc_ops.read(EC_LPC_ADDR_MEMMAP + offset, bytes, s);
303bce70fefSShawn Nematbakhsh return bytes;
304ec2f33abSBill Richardson }
305ec2f33abSBill Richardson
306ec2f33abSBill Richardson /* string */
307ec2f33abSBill Richardson for (; i < EC_MEMMAP_SIZE; i++, s++) {
30822c040faSEnric Balletbo i Serra cros_ec_lpc_ops.read(EC_LPC_ADDR_MEMMAP + i, 1, s);
309ec2f33abSBill Richardson cnt++;
310ec2f33abSBill Richardson if (!*s)
311ec2f33abSBill Richardson break;
312ec2f33abSBill Richardson }
313ec2f33abSBill Richardson
314ec2f33abSBill Richardson return cnt;
315ec2f33abSBill Richardson }
316ec2f33abSBill Richardson
cros_ec_lpc_acpi_notify(acpi_handle device,u32 value,void * data)317a6df7798SGwendal Grignou static void cros_ec_lpc_acpi_notify(acpi_handle device, u32 value, void *data)
318a6df7798SGwendal Grignou {
3192cbf475aSRob Barnes static const char *env[] = { "ERROR=PANIC", NULL };
320a6df7798SGwendal Grignou struct cros_ec_device *ec_dev = data;
3213300fdd6SEnrico Granata bool ec_has_more_events;
3223300fdd6SEnrico Granata int ret;
323a6df7798SGwendal Grignou
32405a3c420SGwendal Grignou ec_dev->last_event_time = cros_ec_get_time_ns();
32505a3c420SGwendal Grignou
326d90fa2c6SRob Barnes if (value == ACPI_NOTIFY_CROS_EC_PANIC) {
327d90fa2c6SRob Barnes dev_emerg(ec_dev->dev, "CrOS EC Panic Reported. Shutdown is imminent!");
328d90fa2c6SRob Barnes blocking_notifier_call_chain(&ec_dev->panic_notifier, 0, ec_dev);
3292cbf475aSRob Barnes kobject_uevent_env(&ec_dev->dev->kobj, KOBJ_CHANGE, (char **)env);
330f2d4dcedSRob Barnes /* Begin orderly shutdown. EC will force reset after a short period. */
331f2d4dcedSRob Barnes hw_protection_shutdown("CrOS EC Panic", -1);
332d90fa2c6SRob Barnes /* Do not query for other events after a panic is reported */
333d90fa2c6SRob Barnes return;
334d90fa2c6SRob Barnes }
335d90fa2c6SRob Barnes
3363300fdd6SEnrico Granata if (ec_dev->mkbp_event_supported)
3373300fdd6SEnrico Granata do {
3383300fdd6SEnrico Granata ret = cros_ec_get_next_event(ec_dev, NULL,
3393300fdd6SEnrico Granata &ec_has_more_events);
3403300fdd6SEnrico Granata if (ret > 0)
3413300fdd6SEnrico Granata blocking_notifier_call_chain(
3423300fdd6SEnrico Granata &ec_dev->event_notifier, 0,
343a6df7798SGwendal Grignou ec_dev);
3443300fdd6SEnrico Granata } while (ec_has_more_events);
345d6542b39SWenkai Du
346d6542b39SWenkai Du if (value == ACPI_NOTIFY_DEVICE_WAKE)
347d6542b39SWenkai Du pm_system_wakeup();
348a6df7798SGwendal Grignou }
349a6df7798SGwendal Grignou
cros_ec_lpc_probe(struct platform_device * pdev)350ec2f33abSBill Richardson static int cros_ec_lpc_probe(struct platform_device *pdev)
351ec2f33abSBill Richardson {
352ec2f33abSBill Richardson struct device *dev = &pdev->dev;
353a6df7798SGwendal Grignou struct acpi_device *adev;
354a6df7798SGwendal Grignou acpi_status status;
355ec2f33abSBill Richardson struct cros_ec_device *ec_dev;
3562ae3c610STom Rix u8 buf[2] = {};
357da1cf5a1SEnrico Granata int irq, ret;
358ec2f33abSBill Richardson
359c9bc1a0eSDustin L. Howett /*
360c9bc1a0eSDustin L. Howett * The Framework Laptop (and possibly other non-ChromeOS devices)
361c9bc1a0eSDustin L. Howett * only exposes the eight I/O ports that are required for the Microchip EC.
362c9bc1a0eSDustin L. Howett * Requesting a larger reservation will fail.
363c9bc1a0eSDustin L. Howett */
364c9bc1a0eSDustin L. Howett if (!devm_request_region(dev, EC_HOST_CMD_REGION0,
365c9bc1a0eSDustin L. Howett EC_HOST_CMD_MEC_REGION_SIZE, dev_name(dev))) {
366c9bc1a0eSDustin L. Howett dev_err(dev, "couldn't reserve MEC region\n");
367ec2f33abSBill Richardson return -EBUSY;
368ec2f33abSBill Richardson }
369ec2f33abSBill Richardson
370fdf84f9aSBrian Norris cros_ec_lpc_mec_init(EC_HOST_CMD_REGION0,
371fdf84f9aSBrian Norris EC_LPC_ADDR_MEMMAP + EC_MEMMAP_SIZE);
372fdf84f9aSBrian Norris
37322c040faSEnric Balletbo i Serra /*
37422c040faSEnric Balletbo i Serra * Read the mapped ID twice, the first one is assuming the
37522c040faSEnric Balletbo i Serra * EC is a Microchip Embedded Controller (MEC) variant, if the
37622c040faSEnric Balletbo i Serra * protocol fails, fallback to the non MEC variant and try to
37722c040faSEnric Balletbo i Serra * read again the ID.
37822c040faSEnric Balletbo i Serra */
37922c040faSEnric Balletbo i Serra cros_ec_lpc_ops.read = cros_ec_lpc_mec_read_bytes;
38022c040faSEnric Balletbo i Serra cros_ec_lpc_ops.write = cros_ec_lpc_mec_write_bytes;
38122c040faSEnric Balletbo i Serra cros_ec_lpc_ops.read(EC_LPC_ADDR_MEMMAP + EC_MEMMAP_ID, 2, buf);
38222c040faSEnric Balletbo i Serra if (buf[0] != 'E' || buf[1] != 'C') {
383c9bc1a0eSDustin L. Howett if (!devm_request_region(dev, EC_LPC_ADDR_MEMMAP, EC_MEMMAP_SIZE,
384c9bc1a0eSDustin L. Howett dev_name(dev))) {
385c9bc1a0eSDustin L. Howett dev_err(dev, "couldn't reserve memmap region\n");
386c9bc1a0eSDustin L. Howett return -EBUSY;
387c9bc1a0eSDustin L. Howett }
388c9bc1a0eSDustin L. Howett
38922c040faSEnric Balletbo i Serra /* Re-assign read/write operations for the non MEC variant */
39022c040faSEnric Balletbo i Serra cros_ec_lpc_ops.read = cros_ec_lpc_read_bytes;
39122c040faSEnric Balletbo i Serra cros_ec_lpc_ops.write = cros_ec_lpc_write_bytes;
39222c040faSEnric Balletbo i Serra cros_ec_lpc_ops.read(EC_LPC_ADDR_MEMMAP + EC_MEMMAP_ID, 2,
39322c040faSEnric Balletbo i Serra buf);
394bce70fefSShawn Nematbakhsh if (buf[0] != 'E' || buf[1] != 'C') {
395ec2f33abSBill Richardson dev_err(dev, "EC ID not detected\n");
396ec2f33abSBill Richardson return -ENODEV;
397ec2f33abSBill Richardson }
398ec2f33abSBill Richardson
399c9bc1a0eSDustin L. Howett /* Reserve the remaining I/O ports required by the non-MEC protocol. */
400c9bc1a0eSDustin L. Howett if (!devm_request_region(dev, EC_HOST_CMD_REGION0 + EC_HOST_CMD_MEC_REGION_SIZE,
401c9bc1a0eSDustin L. Howett EC_HOST_CMD_REGION_SIZE - EC_HOST_CMD_MEC_REGION_SIZE,
402c9bc1a0eSDustin L. Howett dev_name(dev))) {
403c9bc1a0eSDustin L. Howett dev_err(dev, "couldn't reserve remainder of region0\n");
404ec2f33abSBill Richardson return -EBUSY;
405ec2f33abSBill Richardson }
406ec2f33abSBill Richardson if (!devm_request_region(dev, EC_HOST_CMD_REGION1,
407ec2f33abSBill Richardson EC_HOST_CMD_REGION_SIZE, dev_name(dev))) {
408ec2f33abSBill Richardson dev_err(dev, "couldn't reserve region1\n");
409ec2f33abSBill Richardson return -EBUSY;
410ec2f33abSBill Richardson }
411c9bc1a0eSDustin L. Howett }
412ec2f33abSBill Richardson
413ec2f33abSBill Richardson ec_dev = devm_kzalloc(dev, sizeof(*ec_dev), GFP_KERNEL);
414ec2f33abSBill Richardson if (!ec_dev)
415ec2f33abSBill Richardson return -ENOMEM;
416ec2f33abSBill Richardson
417ec2f33abSBill Richardson platform_set_drvdata(pdev, ec_dev);
418ec2f33abSBill Richardson ec_dev->dev = dev;
419ec2f33abSBill Richardson ec_dev->phys_name = dev_name(dev);
420ec2f33abSBill Richardson ec_dev->cmd_xfer = cros_ec_cmd_xfer_lpc;
421d3654070SStephen Barber ec_dev->pkt_xfer = cros_ec_pkt_xfer_lpc;
422ec2f33abSBill Richardson ec_dev->cmd_readmem = cros_ec_lpc_readmem;
4232c7589afSStephen Barber ec_dev->din_size = sizeof(struct ec_host_response) +
4242c7589afSStephen Barber sizeof(struct ec_response_get_protocol_info);
4252c7589afSStephen Barber ec_dev->dout_size = sizeof(struct ec_host_request);
426ec2f33abSBill Richardson
427da1cf5a1SEnrico Granata /*
428da1cf5a1SEnrico Granata * Some boards do not have an IRQ allotted for cros_ec_lpc,
429da1cf5a1SEnrico Granata * which makes ENXIO an expected (and safe) scenario.
430da1cf5a1SEnrico Granata */
431a69b4eebSEnric Balletbo i Serra irq = platform_get_irq_optional(pdev, 0);
432da1cf5a1SEnrico Granata if (irq > 0)
433da1cf5a1SEnrico Granata ec_dev->irq = irq;
434da1cf5a1SEnrico Granata else if (irq != -ENXIO) {
435da1cf5a1SEnrico Granata dev_err(dev, "couldn't retrieve IRQ number (%d)\n", irq);
436da1cf5a1SEnrico Granata return irq;
437da1cf5a1SEnrico Granata }
438da1cf5a1SEnrico Granata
439ec2f33abSBill Richardson ret = cros_ec_register(ec_dev);
440ec2f33abSBill Richardson if (ret) {
441ec2f33abSBill Richardson dev_err(dev, "couldn't register ec_dev (%d)\n", ret);
442ec2f33abSBill Richardson return ret;
443ec2f33abSBill Richardson }
444ec2f33abSBill Richardson
445a6df7798SGwendal Grignou /*
446a6df7798SGwendal Grignou * Connect a notify handler to process MKBP messages if we have a
447a6df7798SGwendal Grignou * companion ACPI device.
448a6df7798SGwendal Grignou */
449a6df7798SGwendal Grignou adev = ACPI_COMPANION(dev);
450a6df7798SGwendal Grignou if (adev) {
451a6df7798SGwendal Grignou status = acpi_install_notify_handler(adev->handle,
452a6df7798SGwendal Grignou ACPI_ALL_NOTIFY,
453a6df7798SGwendal Grignou cros_ec_lpc_acpi_notify,
454a6df7798SGwendal Grignou ec_dev);
455a6df7798SGwendal Grignou if (ACPI_FAILURE(status))
456a6df7798SGwendal Grignou dev_warn(dev, "Failed to register notifier %08x\n",
457a6df7798SGwendal Grignou status);
458a6df7798SGwendal Grignou }
459a6df7798SGwendal Grignou
460ec2f33abSBill Richardson return 0;
461ec2f33abSBill Richardson }
462ec2f33abSBill Richardson
cros_ec_lpc_remove(struct platform_device * pdev)463ec2f33abSBill Richardson static int cros_ec_lpc_remove(struct platform_device *pdev)
464ec2f33abSBill Richardson {
4657aa703bbSEnric Balletbo i Serra struct cros_ec_device *ec_dev = platform_get_drvdata(pdev);
466a6df7798SGwendal Grignou struct acpi_device *adev;
467a6df7798SGwendal Grignou
468a6df7798SGwendal Grignou adev = ACPI_COMPANION(&pdev->dev);
469a6df7798SGwendal Grignou if (adev)
470a6df7798SGwendal Grignou acpi_remove_notify_handler(adev->handle, ACPI_ALL_NOTIFY,
471a6df7798SGwendal Grignou cros_ec_lpc_acpi_notify);
472ec2f33abSBill Richardson
473afb0a80eSUwe Kleine-König cros_ec_unregister(ec_dev);
474afb0a80eSUwe Kleine-König
475afb0a80eSUwe Kleine-König return 0;
476ec2f33abSBill Richardson }
477ec2f33abSBill Richardson
47812278dc7SGwendal Grignou static const struct acpi_device_id cros_ec_lpc_acpi_device_ids[] = {
47912278dc7SGwendal Grignou { ACPI_DRV_NAME, 0 },
48012278dc7SGwendal Grignou { }
48112278dc7SGwendal Grignou };
48212278dc7SGwendal Grignou MODULE_DEVICE_TABLE(acpi, cros_ec_lpc_acpi_device_ids);
48312278dc7SGwendal Grignou
4846faadbbbSChristoph Hellwig static const struct dmi_system_id cros_ec_lpc_dmi_table[] __initconst = {
485ec2f33abSBill Richardson {
486ec2f33abSBill Richardson /*
487ec2f33abSBill Richardson * Today all Chromebooks/boxes ship with Google_* as version and
488ec2f33abSBill Richardson * coreboot as bios vendor. No other systems with this
489ec2f33abSBill Richardson * combination are known to date.
490ec2f33abSBill Richardson */
491ec2f33abSBill Richardson .matches = {
492ec2f33abSBill Richardson DMI_MATCH(DMI_BIOS_VENDOR, "coreboot"),
493ec2f33abSBill Richardson DMI_MATCH(DMI_BIOS_VERSION, "Google_"),
494ec2f33abSBill Richardson },
495ec2f33abSBill Richardson },
496ec2f33abSBill Richardson {
497f56db262SSalvatore Bellizzi /*
498f56db262SSalvatore Bellizzi * If the box is running custom coreboot firmware then the
499f56db262SSalvatore Bellizzi * DMI BIOS version string will not be matched by "Google_",
500f56db262SSalvatore Bellizzi * but the system vendor string will still be matched by
501f56db262SSalvatore Bellizzi * "GOOGLE".
502f56db262SSalvatore Bellizzi */
503f56db262SSalvatore Bellizzi .matches = {
504f56db262SSalvatore Bellizzi DMI_MATCH(DMI_BIOS_VENDOR, "coreboot"),
505f56db262SSalvatore Bellizzi DMI_MATCH(DMI_SYS_VENDOR, "GOOGLE"),
506f56db262SSalvatore Bellizzi },
507f56db262SSalvatore Bellizzi },
508f56db262SSalvatore Bellizzi {
509ec2f33abSBill Richardson /* x86-link, the Chromebook Pixel. */
510ec2f33abSBill Richardson .matches = {
511ec2f33abSBill Richardson DMI_MATCH(DMI_SYS_VENDOR, "GOOGLE"),
512ec2f33abSBill Richardson DMI_MATCH(DMI_PRODUCT_NAME, "Link"),
513ec2f33abSBill Richardson },
514ec2f33abSBill Richardson },
515ec2f33abSBill Richardson {
51685bba84eSJavier Martinez Canillas /* x86-samus, the Chromebook Pixel 2. */
51785bba84eSJavier Martinez Canillas .matches = {
51885bba84eSJavier Martinez Canillas DMI_MATCH(DMI_SYS_VENDOR, "GOOGLE"),
51985bba84eSJavier Martinez Canillas DMI_MATCH(DMI_PRODUCT_NAME, "Samus"),
52085bba84eSJavier Martinez Canillas },
52185bba84eSJavier Martinez Canillas },
52285bba84eSJavier Martinez Canillas {
523ec2f33abSBill Richardson /* x86-peppy, the Acer C720 Chromebook. */
524ec2f33abSBill Richardson .matches = {
525ec2f33abSBill Richardson DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
526ec2f33abSBill Richardson DMI_MATCH(DMI_PRODUCT_NAME, "Peppy"),
527ec2f33abSBill Richardson },
528ec2f33abSBill Richardson },
529e6751917SThierry Escande {
530e6751917SThierry Escande /* x86-glimmer, the Lenovo Thinkpad Yoga 11e. */
531e6751917SThierry Escande .matches = {
532e6751917SThierry Escande DMI_MATCH(DMI_SYS_VENDOR, "GOOGLE"),
533e6751917SThierry Escande DMI_MATCH(DMI_PRODUCT_NAME, "Glimmer"),
534e6751917SThierry Escande },
535e6751917SThierry Escande },
5366a5d778eSDustin L. Howett /* A small number of non-Chromebook/box machines also use the ChromeOS EC */
5376a5d778eSDustin L. Howett {
5386a5d778eSDustin L. Howett /* the Framework Laptop */
5396a5d778eSDustin L. Howett .matches = {
5406a5d778eSDustin L. Howett DMI_MATCH(DMI_SYS_VENDOR, "Framework"),
5416a5d778eSDustin L. Howett DMI_MATCH(DMI_PRODUCT_NAME, "Laptop"),
5426a5d778eSDustin L. Howett },
5436a5d778eSDustin L. Howett },
544ec2f33abSBill Richardson { /* sentinel */ }
545ec2f33abSBill Richardson };
546ec2f33abSBill Richardson MODULE_DEVICE_TABLE(dmi, cros_ec_lpc_dmi_table);
547ec2f33abSBill Richardson
548450de8f4SArchana Patni #ifdef CONFIG_PM_SLEEP
cros_ec_lpc_prepare(struct device * dev)5494b9abbc1STim Van Patten static int cros_ec_lpc_prepare(struct device *dev)
550450de8f4SArchana Patni {
551450de8f4SArchana Patni struct cros_ec_device *ec_dev = dev_get_drvdata(dev);
552*5eafd56dSLalith Rajendran return cros_ec_suspend_prepare(ec_dev);
553450de8f4SArchana Patni }
554450de8f4SArchana Patni
cros_ec_lpc_complete(struct device * dev)5554b9abbc1STim Van Patten static void cros_ec_lpc_complete(struct device *dev)
556450de8f4SArchana Patni {
557450de8f4SArchana Patni struct cros_ec_device *ec_dev = dev_get_drvdata(dev);
558*5eafd56dSLalith Rajendran cros_ec_resume_complete(ec_dev);
559*5eafd56dSLalith Rajendran }
560*5eafd56dSLalith Rajendran
cros_ec_lpc_suspend_late(struct device * dev)561*5eafd56dSLalith Rajendran static int cros_ec_lpc_suspend_late(struct device *dev)
562*5eafd56dSLalith Rajendran {
563*5eafd56dSLalith Rajendran struct cros_ec_device *ec_dev = dev_get_drvdata(dev);
564*5eafd56dSLalith Rajendran
565*5eafd56dSLalith Rajendran return cros_ec_suspend_late(ec_dev);
566*5eafd56dSLalith Rajendran }
567*5eafd56dSLalith Rajendran
cros_ec_lpc_resume_early(struct device * dev)568*5eafd56dSLalith Rajendran static int cros_ec_lpc_resume_early(struct device *dev)
569*5eafd56dSLalith Rajendran {
570*5eafd56dSLalith Rajendran struct cros_ec_device *ec_dev = dev_get_drvdata(dev);
571*5eafd56dSLalith Rajendran
572*5eafd56dSLalith Rajendran return cros_ec_resume_early(ec_dev);
573450de8f4SArchana Patni }
574450de8f4SArchana Patni #endif
575450de8f4SArchana Patni
57681bc8c03SYueHaibing static const struct dev_pm_ops cros_ec_lpc_pm_ops = {
5774b9abbc1STim Van Patten #ifdef CONFIG_PM_SLEEP
5784b9abbc1STim Van Patten .prepare = cros_ec_lpc_prepare,
579*5eafd56dSLalith Rajendran .complete = cros_ec_lpc_complete,
5804b9abbc1STim Van Patten #endif
581*5eafd56dSLalith Rajendran SET_LATE_SYSTEM_SLEEP_PM_OPS(cros_ec_lpc_suspend_late, cros_ec_lpc_resume_early)
582450de8f4SArchana Patni };
583450de8f4SArchana Patni
584ec2f33abSBill Richardson static struct platform_driver cros_ec_lpc_driver = {
585ec2f33abSBill Richardson .driver = {
586ec2f33abSBill Richardson .name = DRV_NAME,
58712278dc7SGwendal Grignou .acpi_match_table = cros_ec_lpc_acpi_device_ids,
588450de8f4SArchana Patni .pm = &cros_ec_lpc_pm_ops,
589ca821c1fSBrian Norris /*
590ca821c1fSBrian Norris * ACPI child devices may probe before us, and they racily
591ca821c1fSBrian Norris * check our drvdata pointer. Force synchronous probe until
592ca821c1fSBrian Norris * those races are resolved.
593ca821c1fSBrian Norris */
594ca821c1fSBrian Norris .probe_type = PROBE_FORCE_SYNCHRONOUS,
595ec2f33abSBill Richardson },
596ec2f33abSBill Richardson .probe = cros_ec_lpc_probe,
597ec2f33abSBill Richardson .remove = cros_ec_lpc_remove,
598ec2f33abSBill Richardson };
599ec2f33abSBill Richardson
6005f454bdfSEnric Balletbo i Serra static struct platform_device cros_ec_lpc_device = {
6015f454bdfSEnric Balletbo i Serra .name = DRV_NAME
6025f454bdfSEnric Balletbo i Serra };
6035f454bdfSEnric Balletbo i Serra
cros_ec_lpc_parse_device(acpi_handle handle,u32 level,void * context,void ** retval)6045f454bdfSEnric Balletbo i Serra static acpi_status cros_ec_lpc_parse_device(acpi_handle handle, u32 level,
6055f454bdfSEnric Balletbo i Serra void *context, void **retval)
6065f454bdfSEnric Balletbo i Serra {
6075f454bdfSEnric Balletbo i Serra *(bool *)context = true;
6085f454bdfSEnric Balletbo i Serra return AE_CTRL_TERMINATE;
6095f454bdfSEnric Balletbo i Serra }
6105f454bdfSEnric Balletbo i Serra
cros_ec_lpc_init(void)611ec2f33abSBill Richardson static int __init cros_ec_lpc_init(void)
612ec2f33abSBill Richardson {
613ec2f33abSBill Richardson int ret;
6145f454bdfSEnric Balletbo i Serra acpi_status status;
615ec2f33abSBill Richardson
616b410b122SDmitry Torokhov status = acpi_get_devices(ACPI_DRV_NAME, cros_ec_lpc_parse_device,
617b410b122SDmitry Torokhov &cros_ec_lpc_acpi_device_found, NULL);
618b410b122SDmitry Torokhov if (ACPI_FAILURE(status))
619b410b122SDmitry Torokhov pr_warn(DRV_NAME ": Looking for %s failed\n", ACPI_DRV_NAME);
620b410b122SDmitry Torokhov
621b410b122SDmitry Torokhov if (!cros_ec_lpc_acpi_device_found &&
622b410b122SDmitry Torokhov !dmi_check_system(cros_ec_lpc_dmi_table)) {
623ec2f33abSBill Richardson pr_err(DRV_NAME ": unsupported system.\n");
624ec2f33abSBill Richardson return -ENODEV;
625ec2f33abSBill Richardson }
626ec2f33abSBill Richardson
627ec2f33abSBill Richardson /* Register the driver */
628ec2f33abSBill Richardson ret = platform_driver_register(&cros_ec_lpc_driver);
629ec2f33abSBill Richardson if (ret) {
630ec2f33abSBill Richardson pr_err(DRV_NAME ": can't register driver: %d\n", ret);
631ec2f33abSBill Richardson return ret;
632ec2f33abSBill Richardson }
633ec2f33abSBill Richardson
6345f454bdfSEnric Balletbo i Serra if (!cros_ec_lpc_acpi_device_found) {
6355f454bdfSEnric Balletbo i Serra /* Register the device, and it'll get hooked up automatically */
6365f454bdfSEnric Balletbo i Serra ret = platform_device_register(&cros_ec_lpc_device);
6375f454bdfSEnric Balletbo i Serra if (ret) {
6385f454bdfSEnric Balletbo i Serra pr_err(DRV_NAME ": can't register device: %d\n", ret);
6395f454bdfSEnric Balletbo i Serra platform_driver_unregister(&cros_ec_lpc_driver);
6405f454bdfSEnric Balletbo i Serra }
6415f454bdfSEnric Balletbo i Serra }
6425f454bdfSEnric Balletbo i Serra
6435f454bdfSEnric Balletbo i Serra return ret;
644ec2f33abSBill Richardson }
645ec2f33abSBill Richardson
cros_ec_lpc_exit(void)646ec2f33abSBill Richardson static void __exit cros_ec_lpc_exit(void)
647ec2f33abSBill Richardson {
6485f454bdfSEnric Balletbo i Serra if (!cros_ec_lpc_acpi_device_found)
6495f454bdfSEnric Balletbo i Serra platform_device_unregister(&cros_ec_lpc_device);
650ec2f33abSBill Richardson platform_driver_unregister(&cros_ec_lpc_driver);
651ec2f33abSBill Richardson }
652ec2f33abSBill Richardson
653ec2f33abSBill Richardson module_init(cros_ec_lpc_init);
654ec2f33abSBill Richardson module_exit(cros_ec_lpc_exit);
655ec2f33abSBill Richardson
656ec2f33abSBill Richardson MODULE_LICENSE("GPL");
657ec2f33abSBill Richardson MODULE_DESCRIPTION("ChromeOS EC LPC driver");
658