1 // SPDX-License-Identifier: GPL-2.0
2 // LPC interface for ChromeOS Embedded Controller
3 //
4 // Copyright (C) 2012-2015 Google, Inc
5 //
6 // This driver uses the ChromeOS EC byte-level message-based protocol for
7 // communicating the keyboard state (which keys are pressed) from a keyboard EC
8 // to the AP over some bus (such as i2c, lpc, spi).  The EC does debouncing,
9 // but everything else (including deghosting) is done here.  The main
10 // motivation for this is to keep the EC firmware as simple as possible, since
11 // it cannot be easily upgraded and EC flash/IRAM space is relatively
12 // expensive.
13 
14 #include <linux/acpi.h>
15 #include <linux/dmi.h>
16 #include <linux/delay.h>
17 #include <linux/io.h>
18 #include <linux/interrupt.h>
19 #include <linux/kobject.h>
20 #include <linux/module.h>
21 #include <linux/platform_data/cros_ec_commands.h>
22 #include <linux/platform_data/cros_ec_proto.h>
23 #include <linux/platform_device.h>
24 #include <linux/printk.h>
25 #include <linux/reboot.h>
26 #include <linux/suspend.h>
27 
28 #include "cros_ec.h"
29 #include "cros_ec_lpc_mec.h"
30 
31 #define DRV_NAME "cros_ec_lpcs"
32 #define ACPI_DRV_NAME "GOOG0004"
33 #define FRMW_ACPI_DRV_NAME "FRMWC004"
34 
35 /* True if ACPI device is present */
36 static bool cros_ec_lpc_acpi_device_found;
37 
38 /*
39  * Indicates that lpc_driver_data.quirk_mmio_memory_base should
40  * be used as the base port for EC mapped memory.
41  */
42 #define CROS_EC_LPC_QUIRK_REMAP_MEMORY              BIT(0)
43 /*
44  * Indicates that lpc_driver_data.quirk_acpi_id should be used to find
45  * the ACPI device.
46  */
47 #define CROS_EC_LPC_QUIRK_ACPI_ID                   BIT(1)
48 /*
49  * Indicates that lpc_driver_data.quirk_aml_mutex_name should be used
50  * to find an AML mutex to protect access to Microchip EC.
51  */
52 #define CROS_EC_LPC_QUIRK_AML_MUTEX                 BIT(2)
53 
54 /**
55  * struct lpc_driver_data - driver data attached to a DMI device ID to indicate
56  *                          hardware quirks.
57  * @quirks: a bitfield composed of quirks from CROS_EC_LPC_QUIRK_*
58  * @quirk_mmio_memory_base: The first I/O port addressing EC mapped memory (used
59  *                          when quirk ...REMAP_MEMORY is set.)
60  * @quirk_acpi_id: An ACPI HID to be used to find the ACPI device.
61  * @quirk_aml_mutex_name: The name of an AML mutex to be used to protect access
62  *                        to Microchip EC.
63  */
64 struct lpc_driver_data {
65 	u32 quirks;
66 	u16 quirk_mmio_memory_base;
67 	const char *quirk_acpi_id;
68 	const char *quirk_aml_mutex_name;
69 };
70 
71 /**
72  * struct cros_ec_lpc - LPC device-specific data
73  * @mmio_memory_base: The first I/O port addressing EC mapped memory.
74  * @base: For EC supporting memory mapping, base address of the mapped region.
75  * @mem32: Information about the memory mapped register region, if present.
76  * @read: Copy length bytes from EC address offset into buffer dest.
77  *        Returns a negative error code on error, or the 8-bit checksum
78  *        of all bytes read.
79  * @write: Copy length bytes from buffer msg into EC address offset.
80  *         Returns a negative error code on error, or the 8-bit checksum
81  *         of all bytes written.
82  */
83 struct cros_ec_lpc {
84 	u16 mmio_memory_base;
85 	void __iomem *base;
86 	struct acpi_resource_fixed_memory32 mem32;
87 	int (*read)(struct cros_ec_lpc *ec_lpc, unsigned int offset,
88 		    unsigned int length, u8 *dest);
89 	int (*write)(struct cros_ec_lpc *ec_lpc, unsigned int offset,
90 		     unsigned int length, const u8 *msg);
91 };
92 
93 /*
94  * A generic instance of the read function of struct lpc_driver_ops, used for
95  * the LPC EC.
96  */
cros_ec_lpc_read_bytes(struct cros_ec_lpc * _,unsigned int offset,unsigned int length,u8 * dest)97 static int cros_ec_lpc_read_bytes(struct cros_ec_lpc *_, unsigned int offset, unsigned int length,
98 				  u8 *dest)
99 {
100 	u8 sum = 0;
101 	int i;
102 
103 	for (i = 0; i < length; ++i) {
104 		dest[i] = inb(offset + i);
105 		sum += dest[i];
106 	}
107 
108 	/* Return checksum of all bytes read */
109 	return sum;
110 }
111 
112 /*
113  * A generic instance of the write function of struct lpc_driver_ops, used for
114  * the LPC EC.
115  */
cros_ec_lpc_write_bytes(struct cros_ec_lpc * _,unsigned int offset,unsigned int length,const u8 * msg)116 static int cros_ec_lpc_write_bytes(struct cros_ec_lpc *_, unsigned int offset, unsigned int length,
117 				   const u8 *msg)
118 {
119 	u8 sum = 0;
120 	int i;
121 
122 	for (i = 0; i < length; ++i) {
123 		outb(msg[i], offset + i);
124 		sum += msg[i];
125 	}
126 
127 	/* Return checksum of all bytes written */
128 	return sum;
129 }
130 
131 /*
132  * An instance of the read function of struct lpc_driver_ops, used for the
133  * MEC variant of LPC EC.
134  */
cros_ec_lpc_mec_read_bytes(struct cros_ec_lpc * ec_lpc,unsigned int offset,unsigned int length,u8 * dest)135 static int cros_ec_lpc_mec_read_bytes(struct cros_ec_lpc *ec_lpc, unsigned int offset,
136 				      unsigned int length, u8 *dest)
137 {
138 	int in_range = cros_ec_lpc_mec_in_range(offset, length);
139 
140 	if (in_range < 0)
141 		return in_range;
142 
143 	return in_range ?
144 		cros_ec_lpc_io_bytes_mec(MEC_IO_READ,
145 					 offset - EC_HOST_CMD_REGION0,
146 					 length, dest) :
147 		cros_ec_lpc_read_bytes(ec_lpc, offset, length, dest);
148 }
149 
150 /*
151  * An instance of the write function of struct lpc_driver_ops, used for the
152  * MEC variant of LPC EC.
153  */
cros_ec_lpc_mec_write_bytes(struct cros_ec_lpc * ec_lpc,unsigned int offset,unsigned int length,const u8 * msg)154 static int cros_ec_lpc_mec_write_bytes(struct cros_ec_lpc *ec_lpc, unsigned int offset,
155 				       unsigned int length, const u8 *msg)
156 {
157 	int in_range = cros_ec_lpc_mec_in_range(offset, length);
158 
159 	if (in_range < 0)
160 		return in_range;
161 
162 	return in_range ?
163 		cros_ec_lpc_io_bytes_mec(MEC_IO_WRITE,
164 					 offset - EC_HOST_CMD_REGION0,
165 					 length, (u8 *)msg) :
166 		cros_ec_lpc_write_bytes(ec_lpc, offset, length, msg);
167 }
168 
cros_ec_lpc_direct_read(struct cros_ec_lpc * ec_lpc,unsigned int offset,unsigned int length,u8 * dest)169 static int cros_ec_lpc_direct_read(struct cros_ec_lpc *ec_lpc, unsigned int offset,
170 				   unsigned int length, u8 *dest)
171 {
172 	int sum = 0;
173 	int i;
174 
175 	if (offset < EC_HOST_CMD_REGION0 || offset > EC_LPC_ADDR_MEMMAP +
176 			EC_MEMMAP_SIZE) {
177 		return cros_ec_lpc_read_bytes(ec_lpc, offset, length, dest);
178 	}
179 
180 	for (i = 0; i < length; ++i) {
181 		dest[i] = readb(ec_lpc->base + offset - EC_HOST_CMD_REGION0 + i);
182 		sum += dest[i];
183 	}
184 
185 	/* Return checksum of all bytes read */
186 	return sum;
187 }
188 
cros_ec_lpc_direct_write(struct cros_ec_lpc * ec_lpc,unsigned int offset,unsigned int length,const u8 * msg)189 static int cros_ec_lpc_direct_write(struct cros_ec_lpc *ec_lpc, unsigned int offset,
190 				    unsigned int length, const u8 *msg)
191 {
192 	int sum = 0;
193 	int i;
194 
195 	if (offset < EC_HOST_CMD_REGION0 || offset > EC_LPC_ADDR_MEMMAP +
196 			EC_MEMMAP_SIZE) {
197 		return cros_ec_lpc_write_bytes(ec_lpc, offset, length, msg);
198 	}
199 
200 	for (i = 0; i < length; ++i) {
201 		writeb(msg[i], ec_lpc->base + offset - EC_HOST_CMD_REGION0 + i);
202 		sum += msg[i];
203 	}
204 
205 	/* Return checksum of all bytes written */
206 	return sum;
207 }
208 
ec_response_timed_out(struct cros_ec_lpc * ec_lpc)209 static int ec_response_timed_out(struct cros_ec_lpc *ec_lpc)
210 {
211 	unsigned long one_second = jiffies + HZ;
212 	u8 data;
213 	int ret;
214 
215 	usleep_range(200, 300);
216 	do {
217 		ret = ec_lpc->read(ec_lpc, EC_LPC_ADDR_HOST_CMD, 1, &data);
218 		if (ret < 0)
219 			return ret;
220 		if (!(data & EC_LPC_STATUS_BUSY_MASK))
221 			return 0;
222 		usleep_range(100, 200);
223 	} while (time_before(jiffies, one_second));
224 
225 	return 1;
226 }
227 
cros_ec_pkt_xfer_lpc(struct cros_ec_device * ec,struct cros_ec_command * msg)228 static int cros_ec_pkt_xfer_lpc(struct cros_ec_device *ec,
229 				struct cros_ec_command *msg)
230 {
231 	struct cros_ec_lpc *ec_lpc = ec->priv;
232 	struct ec_host_response response;
233 	u8 sum;
234 	int ret = 0;
235 	u8 *dout;
236 
237 	ret = cros_ec_prepare_tx(ec, msg);
238 	if (ret < 0)
239 		goto done;
240 
241 	/* Write buffer */
242 	ret = ec_lpc->write(ec_lpc, EC_LPC_ADDR_HOST_PACKET, ret, ec->dout);
243 	if (ret < 0)
244 		goto done;
245 
246 	/* Here we go */
247 	sum = EC_COMMAND_PROTOCOL_3;
248 	ret = ec_lpc->write(ec_lpc, EC_LPC_ADDR_HOST_CMD, 1, &sum);
249 	if (ret < 0)
250 		goto done;
251 
252 	ret = ec_response_timed_out(ec_lpc);
253 	if (ret < 0)
254 		goto done;
255 	if (ret) {
256 		dev_warn(ec->dev, "EC response timed out\n");
257 		ret = -EIO;
258 		goto done;
259 	}
260 
261 	/* Check result */
262 	ret = ec_lpc->read(ec_lpc, EC_LPC_ADDR_HOST_DATA, 1, &sum);
263 	if (ret < 0)
264 		goto done;
265 	msg->result = ret;
266 	ret = cros_ec_check_result(ec, msg);
267 	if (ret)
268 		goto done;
269 
270 	/* Read back response */
271 	dout = (u8 *)&response;
272 	ret = ec_lpc->read(ec_lpc, EC_LPC_ADDR_HOST_PACKET, sizeof(response),
273 				   dout);
274 	if (ret < 0)
275 		goto done;
276 	sum = ret;
277 
278 	msg->result = response.result;
279 
280 	if (response.data_len > msg->insize) {
281 		dev_err(ec->dev,
282 			"packet too long (%d bytes, expected %d)",
283 			response.data_len, msg->insize);
284 		ret = -EMSGSIZE;
285 		goto done;
286 	}
287 
288 	/* Read response and process checksum */
289 	ret = ec_lpc->read(ec_lpc, EC_LPC_ADDR_HOST_PACKET +
290 				   sizeof(response), response.data_len,
291 				   msg->data);
292 	if (ret < 0)
293 		goto done;
294 	sum += ret;
295 
296 	if (sum) {
297 		dev_err(ec->dev,
298 			"bad packet checksum %02x\n",
299 			response.checksum);
300 		ret = -EBADMSG;
301 		goto done;
302 	}
303 
304 	/* Return actual amount of data received */
305 	ret = response.data_len;
306 done:
307 	return ret;
308 }
309 
cros_ec_cmd_xfer_lpc(struct cros_ec_device * ec,struct cros_ec_command * msg)310 static int cros_ec_cmd_xfer_lpc(struct cros_ec_device *ec,
311 				struct cros_ec_command *msg)
312 {
313 	struct cros_ec_lpc *ec_lpc = ec->priv;
314 	struct ec_lpc_host_args args;
315 	u8 sum;
316 	int ret = 0;
317 
318 	if (msg->outsize > EC_PROTO2_MAX_PARAM_SIZE ||
319 	    msg->insize > EC_PROTO2_MAX_PARAM_SIZE) {
320 		dev_err(ec->dev,
321 			"invalid buffer sizes (out %d, in %d)\n",
322 			msg->outsize, msg->insize);
323 		return -EINVAL;
324 	}
325 
326 	/* Now actually send the command to the EC and get the result */
327 	args.flags = EC_HOST_ARGS_FLAG_FROM_HOST;
328 	args.command_version = msg->version;
329 	args.data_size = msg->outsize;
330 
331 	/* Initialize checksum */
332 	sum = msg->command + args.flags + args.command_version + args.data_size;
333 
334 	/* Copy data and update checksum */
335 	ret = ec_lpc->write(ec_lpc, EC_LPC_ADDR_HOST_PARAM, msg->outsize,
336 				    msg->data);
337 	if (ret < 0)
338 		goto done;
339 	sum += ret;
340 
341 	/* Finalize checksum and write args */
342 	args.checksum = sum;
343 	ret = ec_lpc->write(ec_lpc, EC_LPC_ADDR_HOST_ARGS, sizeof(args),
344 				    (u8 *)&args);
345 	if (ret < 0)
346 		goto done;
347 
348 	/* Here we go */
349 	sum = msg->command;
350 	ret = ec_lpc->write(ec_lpc, EC_LPC_ADDR_HOST_CMD, 1, &sum);
351 	if (ret < 0)
352 		goto done;
353 
354 	ret = ec_response_timed_out(ec_lpc);
355 	if (ret < 0)
356 		goto done;
357 	if (ret) {
358 		dev_warn(ec->dev, "EC response timed out\n");
359 		ret = -EIO;
360 		goto done;
361 	}
362 
363 	/* Check result */
364 	ret = ec_lpc->read(ec_lpc, EC_LPC_ADDR_HOST_DATA, 1, &sum);
365 	if (ret < 0)
366 		goto done;
367 	msg->result = ret;
368 	ret = cros_ec_check_result(ec, msg);
369 	if (ret)
370 		goto done;
371 
372 	/* Read back args */
373 	ret = ec_lpc->read(ec_lpc, EC_LPC_ADDR_HOST_ARGS, sizeof(args), (u8 *)&args);
374 	if (ret < 0)
375 		goto done;
376 
377 	if (args.data_size > msg->insize) {
378 		dev_err(ec->dev,
379 			"packet too long (%d bytes, expected %d)",
380 			args.data_size, msg->insize);
381 		ret = -ENOSPC;
382 		goto done;
383 	}
384 
385 	/* Start calculating response checksum */
386 	sum = msg->command + args.flags + args.command_version + args.data_size;
387 
388 	/* Read response and update checksum */
389 	ret = ec_lpc->read(ec_lpc, EC_LPC_ADDR_HOST_PARAM, args.data_size,
390 				   msg->data);
391 	if (ret < 0)
392 		goto done;
393 	sum += ret;
394 
395 	/* Verify checksum */
396 	if (args.checksum != sum) {
397 		dev_err(ec->dev,
398 			"bad packet checksum, expected %02x, got %02x\n",
399 			args.checksum, sum);
400 		ret = -EBADMSG;
401 		goto done;
402 	}
403 
404 	/* Return actual amount of data received */
405 	ret = args.data_size;
406 done:
407 	return ret;
408 }
409 
410 /* 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)411 static int cros_ec_lpc_readmem(struct cros_ec_device *ec, unsigned int offset,
412 			       unsigned int bytes, void *dest)
413 {
414 	struct cros_ec_lpc *ec_lpc = ec->priv;
415 	int i = offset;
416 	char *s = dest;
417 	int cnt = 0;
418 	int ret;
419 
420 	if (offset >= EC_MEMMAP_SIZE - bytes)
421 		return -EINVAL;
422 
423 	/* fixed length */
424 	if (bytes) {
425 		ret = ec_lpc->read(ec_lpc, ec_lpc->mmio_memory_base + offset, bytes, s);
426 		if (ret < 0)
427 			return ret;
428 		return bytes;
429 	}
430 
431 	/* string */
432 	for (; i < EC_MEMMAP_SIZE; i++, s++) {
433 		ret = ec_lpc->read(ec_lpc, ec_lpc->mmio_memory_base + i, 1, s);
434 		if (ret < 0)
435 			return ret;
436 		cnt++;
437 		if (!*s)
438 			break;
439 	}
440 
441 	return cnt;
442 }
443 
cros_ec_lpc_acpi_notify(acpi_handle device,u32 value,void * data)444 static void cros_ec_lpc_acpi_notify(acpi_handle device, u32 value, void *data)
445 {
446 	static const char *env[] = { "ERROR=PANIC", NULL };
447 	struct cros_ec_device *ec_dev = data;
448 	bool ec_has_more_events;
449 	int ret;
450 
451 	ec_dev->last_event_time = cros_ec_get_time_ns();
452 
453 	if (value == ACPI_NOTIFY_CROS_EC_PANIC) {
454 		dev_emerg(ec_dev->dev, "CrOS EC Panic Reported. Shutdown is imminent!");
455 		blocking_notifier_call_chain(&ec_dev->panic_notifier, 0, ec_dev);
456 		kobject_uevent_env(&ec_dev->dev->kobj, KOBJ_CHANGE, (char **)env);
457 		/* Begin orderly shutdown. EC will force reset after a short period. */
458 		hw_protection_shutdown("CrOS EC Panic", -1);
459 		/* Do not query for other events after a panic is reported */
460 		return;
461 	}
462 
463 	if (value == ACPI_NOTIFY_CROS_EC_MKBP && ec_dev->mkbp_event_supported)
464 		do {
465 			ret = cros_ec_get_next_event(ec_dev, NULL,
466 						     &ec_has_more_events);
467 			if (ret > 0)
468 				blocking_notifier_call_chain(
469 						&ec_dev->event_notifier, 0,
470 						ec_dev);
471 		} while (ec_has_more_events);
472 
473 	if (value == ACPI_NOTIFY_DEVICE_WAKE)
474 		pm_system_wakeup();
475 }
476 
cros_ec_lpc_parse_device(acpi_handle handle,u32 level,void * context,void ** retval)477 static acpi_status cros_ec_lpc_parse_device(acpi_handle handle, u32 level,
478 					    void *context, void **retval)
479 {
480 	*(struct acpi_device **)context = acpi_fetch_acpi_dev(handle);
481 	return AE_CTRL_TERMINATE;
482 }
483 
cros_ec_lpc_get_device(const char * id)484 static struct acpi_device *cros_ec_lpc_get_device(const char *id)
485 {
486 	struct acpi_device *adev = NULL;
487 	acpi_status status = acpi_get_devices(id, cros_ec_lpc_parse_device,
488 					      &adev, NULL);
489 	if (ACPI_FAILURE(status)) {
490 		pr_warn(DRV_NAME ": Looking for %s failed\n", id);
491 		return NULL;
492 	}
493 
494 	return adev;
495 }
496 
cros_ec_lpc_resources(struct acpi_resource * res,void * data)497 static acpi_status cros_ec_lpc_resources(struct acpi_resource *res, void *data)
498 {
499 	struct cros_ec_lpc *ec_lpc = data;
500 
501 	switch (res->type) {
502 	case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
503 		ec_lpc->mem32 = res->data.fixed_memory32;
504 		break;
505 	default:
506 		break;
507 	}
508 	return AE_OK;
509 }
510 
cros_ec_lpc_probe(struct platform_device * pdev)511 static int cros_ec_lpc_probe(struct platform_device *pdev)
512 {
513 	struct device *dev = &pdev->dev;
514 	struct acpi_device *adev;
515 	acpi_status status;
516 	struct cros_ec_device *ec_dev;
517 	struct cros_ec_lpc *ec_lpc;
518 	const struct lpc_driver_data *driver_data;
519 	u8 buf[2] = {};
520 	int irq, ret;
521 	u32 quirks;
522 
523 	ec_lpc = devm_kzalloc(dev, sizeof(*ec_lpc), GFP_KERNEL);
524 	if (!ec_lpc)
525 		return -ENOMEM;
526 
527 	ec_lpc->mmio_memory_base = EC_LPC_ADDR_MEMMAP;
528 
529 	driver_data = platform_get_drvdata(pdev);
530 	if (!driver_data)
531 		driver_data = acpi_device_get_match_data(dev);
532 
533 	if (driver_data) {
534 		quirks = driver_data->quirks;
535 
536 		if (quirks)
537 			dev_info(dev, "loaded with quirks %8.08x\n", quirks);
538 
539 		if (quirks & CROS_EC_LPC_QUIRK_REMAP_MEMORY)
540 			ec_lpc->mmio_memory_base = driver_data->quirk_mmio_memory_base;
541 
542 		if (quirks & CROS_EC_LPC_QUIRK_ACPI_ID) {
543 			adev = cros_ec_lpc_get_device(driver_data->quirk_acpi_id);
544 			if (!adev) {
545 				dev_err(dev, "failed to get ACPI device '%s'",
546 					driver_data->quirk_acpi_id);
547 				return -ENODEV;
548 			}
549 			ACPI_COMPANION_SET(dev, adev);
550 		}
551 
552 		if (quirks & CROS_EC_LPC_QUIRK_AML_MUTEX) {
553 			const char *name = driver_data->quirk_aml_mutex_name;
554 			ret = cros_ec_lpc_mec_acpi_mutex(ACPI_COMPANION(dev), name);
555 			if (ret) {
556 				dev_err(dev, "failed to get AML mutex '%s'", name);
557 				return ret;
558 			}
559 			dev_info(dev, "got AML mutex '%s'", name);
560 		}
561 	}
562 	adev = ACPI_COMPANION(dev);
563 	if (adev) {
564 		/*
565 		 * Retrieve the resource information in the CRS register, if available.
566 		 */
567 		status = acpi_walk_resources(adev->handle, METHOD_NAME__CRS,
568 					     cros_ec_lpc_resources, ec_lpc);
569 		if (ACPI_SUCCESS(status) && ec_lpc->mem32.address_length) {
570 			ec_lpc->base = devm_ioremap(dev,
571 						    ec_lpc->mem32.address,
572 						    ec_lpc->mem32.address_length);
573 			if (!ec_lpc->base)
574 				return -EINVAL;
575 
576 			ec_lpc->read = cros_ec_lpc_direct_read;
577 			ec_lpc->write = cros_ec_lpc_direct_write;
578 		}
579 	}
580 	if (!ec_lpc->read) {
581 		/*
582 		 * The Framework Laptop (and possibly other non-ChromeOS devices)
583 		 * only exposes the eight I/O ports that are required for the Microchip EC.
584 		 * Requesting a larger reservation will fail.
585 		 */
586 		if (!devm_request_region(dev, EC_HOST_CMD_REGION0,
587 					 EC_HOST_CMD_MEC_REGION_SIZE, dev_name(dev))) {
588 			dev_err(dev, "couldn't reserve MEC region\n");
589 			return -EBUSY;
590 		}
591 
592 		cros_ec_lpc_mec_init(EC_HOST_CMD_REGION0,
593 				     EC_LPC_ADDR_MEMMAP + EC_MEMMAP_SIZE);
594 
595 		/*
596 		 * Read the mapped ID twice, the first one is assuming the
597 		 * EC is a Microchip Embedded Controller (MEC) variant, if the
598 		 * protocol fails, fallback to the non MEC variant and try to
599 		 * read again the ID.
600 		 */
601 		ec_lpc->read = cros_ec_lpc_mec_read_bytes;
602 		ec_lpc->write = cros_ec_lpc_mec_write_bytes;
603 	}
604 	ret = ec_lpc->read(ec_lpc, EC_LPC_ADDR_MEMMAP + EC_MEMMAP_ID, 2, buf);
605 	if (ret < 0)
606 		return ret;
607 	if (buf[0] != 'E' || buf[1] != 'C') {
608 		if (!devm_request_region(dev, ec_lpc->mmio_memory_base, EC_MEMMAP_SIZE,
609 					 dev_name(dev))) {
610 			dev_err(dev, "couldn't reserve memmap region\n");
611 			return -EBUSY;
612 		}
613 
614 		/* Re-assign read/write operations for the non MEC variant */
615 		ec_lpc->read = cros_ec_lpc_read_bytes;
616 		ec_lpc->write = cros_ec_lpc_write_bytes;
617 		ret = ec_lpc->read(ec_lpc, ec_lpc->mmio_memory_base + EC_MEMMAP_ID, 2,
618 					   buf);
619 		if (ret < 0)
620 			return ret;
621 		if (buf[0] != 'E' || buf[1] != 'C') {
622 			dev_err(dev, "EC ID not detected\n");
623 			return -ENODEV;
624 		}
625 
626 		/* Reserve the remaining I/O ports required by the non-MEC protocol. */
627 		if (!devm_request_region(dev, EC_HOST_CMD_REGION0 + EC_HOST_CMD_MEC_REGION_SIZE,
628 					 EC_HOST_CMD_REGION_SIZE - EC_HOST_CMD_MEC_REGION_SIZE,
629 					 dev_name(dev))) {
630 			dev_err(dev, "couldn't reserve remainder of region0\n");
631 			return -EBUSY;
632 		}
633 		if (!devm_request_region(dev, EC_HOST_CMD_REGION1,
634 					 EC_HOST_CMD_REGION_SIZE, dev_name(dev))) {
635 			dev_err(dev, "couldn't reserve region1\n");
636 			return -EBUSY;
637 		}
638 	}
639 
640 	ec_dev = devm_kzalloc(dev, sizeof(*ec_dev), GFP_KERNEL);
641 	if (!ec_dev)
642 		return -ENOMEM;
643 
644 	platform_set_drvdata(pdev, ec_dev);
645 	ec_dev->dev = dev;
646 	ec_dev->phys_name = dev_name(dev);
647 	ec_dev->cmd_xfer = cros_ec_cmd_xfer_lpc;
648 	ec_dev->pkt_xfer = cros_ec_pkt_xfer_lpc;
649 	ec_dev->cmd_readmem = cros_ec_lpc_readmem;
650 	ec_dev->din_size = sizeof(struct ec_host_response) +
651 			   sizeof(struct ec_response_get_protocol_info);
652 	ec_dev->dout_size = sizeof(struct ec_host_request) + sizeof(struct ec_params_rwsig_action);
653 	ec_dev->priv = ec_lpc;
654 
655 	/*
656 	 * Some boards do not have an IRQ allotted for cros_ec_lpc,
657 	 * which makes ENXIO an expected (and safe) scenario.
658 	 */
659 	irq = platform_get_irq_optional(pdev, 0);
660 	if (irq > 0)
661 		ec_dev->irq = irq;
662 	else if (irq != -ENXIO) {
663 		dev_err(dev, "couldn't retrieve IRQ number (%d)\n", irq);
664 		return irq;
665 	}
666 
667 	ret = cros_ec_register(ec_dev);
668 	if (ret) {
669 		dev_err(dev, "couldn't register ec_dev (%d)\n", ret);
670 		return ret;
671 	}
672 
673 	/*
674 	 * Connect a notify handler to process MKBP messages if we have a
675 	 * companion ACPI device.
676 	 */
677 	if (adev) {
678 		status = acpi_install_notify_handler(adev->handle,
679 						     ACPI_ALL_NOTIFY,
680 						     cros_ec_lpc_acpi_notify,
681 						     ec_dev);
682 		if (ACPI_FAILURE(status))
683 			dev_warn(dev, "Failed to register notifier %08x\n",
684 				 status);
685 	}
686 
687 	return 0;
688 }
689 
cros_ec_lpc_remove(struct platform_device * pdev)690 static void cros_ec_lpc_remove(struct platform_device *pdev)
691 {
692 	struct cros_ec_device *ec_dev = platform_get_drvdata(pdev);
693 	struct acpi_device *adev;
694 
695 	adev = ACPI_COMPANION(&pdev->dev);
696 	if (adev)
697 		acpi_remove_notify_handler(adev->handle, ACPI_ALL_NOTIFY,
698 					   cros_ec_lpc_acpi_notify);
699 
700 	cros_ec_unregister(ec_dev);
701 }
702 
703 static const struct lpc_driver_data framework_laptop_npcx_lpc_driver_data __initconst = {
704 	.quirks = CROS_EC_LPC_QUIRK_REMAP_MEMORY,
705 	.quirk_mmio_memory_base = 0xE00,
706 };
707 
708 static const struct lpc_driver_data framework_laptop_mec_lpc_driver_data __initconst = {
709 	.quirks = CROS_EC_LPC_QUIRK_ACPI_ID|CROS_EC_LPC_QUIRK_AML_MUTEX,
710 	.quirk_acpi_id = "PNP0C09",
711 	.quirk_aml_mutex_name = "ECMT",
712 };
713 
714 static const struct acpi_device_id cros_ec_lpc_acpi_device_ids[] = {
715 	{ ACPI_DRV_NAME, 0 },
716 	{ FRMW_ACPI_DRV_NAME, (kernel_ulong_t)&framework_laptop_npcx_lpc_driver_data },
717 	{ }
718 };
719 MODULE_DEVICE_TABLE(acpi, cros_ec_lpc_acpi_device_ids);
720 
721 static const struct dmi_system_id cros_ec_lpc_dmi_table[] __initconst = {
722 	{
723 		/*
724 		 * Today all Chromebooks/boxes ship with Google_* as version and
725 		 * coreboot as bios vendor. No other systems with this
726 		 * combination are known to date.
727 		 */
728 		.matches = {
729 			DMI_MATCH(DMI_BIOS_VENDOR, "coreboot"),
730 			DMI_MATCH(DMI_BIOS_VERSION, "Google_"),
731 		},
732 	},
733 	{
734 		/*
735 		 * If the box is running custom coreboot firmware then the
736 		 * DMI BIOS version string will not be matched by "Google_",
737 		 * but the system vendor string will still be matched by
738 		 * "GOOGLE".
739 		 */
740 		.matches = {
741 			DMI_MATCH(DMI_BIOS_VENDOR, "coreboot"),
742 			DMI_MATCH(DMI_SYS_VENDOR, "GOOGLE"),
743 		},
744 	},
745 	{
746 		/* x86-link, the Chromebook Pixel. */
747 		.matches = {
748 			DMI_MATCH(DMI_SYS_VENDOR, "GOOGLE"),
749 			DMI_MATCH(DMI_PRODUCT_NAME, "Link"),
750 		},
751 	},
752 	{
753 		/* x86-samus, the Chromebook Pixel 2. */
754 		.matches = {
755 			DMI_MATCH(DMI_SYS_VENDOR, "GOOGLE"),
756 			DMI_MATCH(DMI_PRODUCT_NAME, "Samus"),
757 		},
758 	},
759 	{
760 		/* x86-peppy, the Acer C720 Chromebook. */
761 		.matches = {
762 			DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
763 			DMI_MATCH(DMI_PRODUCT_NAME, "Peppy"),
764 		},
765 	},
766 	{
767 		/* x86-glimmer, the Lenovo Thinkpad Yoga 11e. */
768 		.matches = {
769 			DMI_MATCH(DMI_SYS_VENDOR, "GOOGLE"),
770 			DMI_MATCH(DMI_PRODUCT_NAME, "Glimmer"),
771 		},
772 	},
773 	/* A small number of non-Chromebook/box machines also use the ChromeOS EC */
774 	{
775 		/* Framework Laptop (11th Gen Intel Core) */
776 		.matches = {
777 			DMI_MATCH(DMI_SYS_VENDOR, "Framework"),
778 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Laptop"),
779 		},
780 		.driver_data = (void *)&framework_laptop_mec_lpc_driver_data,
781 	},
782 	{
783 		/* Framework Laptop (12th Gen Intel Core) */
784 		.matches = {
785 			DMI_MATCH(DMI_SYS_VENDOR, "Framework"),
786 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Laptop (12th Gen Intel Core)"),
787 		},
788 		.driver_data = (void *)&framework_laptop_mec_lpc_driver_data,
789 	},
790 	{
791 		/* Framework Laptop (13th Gen Intel Core) */
792 		.matches = {
793 			DMI_MATCH(DMI_SYS_VENDOR, "Framework"),
794 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Laptop (13th Gen Intel Core)"),
795 		},
796 		.driver_data = (void *)&framework_laptop_mec_lpc_driver_data,
797 	},
798 	{
799 		/*
800 		 * All remaining Framework Laptop models (13 AMD Ryzen, 16 AMD
801 		 * Ryzen, Intel Core Ultra)
802 		 */
803 		.matches = {
804 			DMI_MATCH(DMI_SYS_VENDOR, "Framework"),
805 			DMI_MATCH(DMI_PRODUCT_FAMILY, "Laptop"),
806 		},
807 		.driver_data = (void *)&framework_laptop_npcx_lpc_driver_data,
808 	},
809 	{ /* sentinel */ }
810 };
811 MODULE_DEVICE_TABLE(dmi, cros_ec_lpc_dmi_table);
812 
813 #ifdef CONFIG_PM_SLEEP
cros_ec_lpc_prepare(struct device * dev)814 static int cros_ec_lpc_prepare(struct device *dev)
815 {
816 	struct cros_ec_device *ec_dev = dev_get_drvdata(dev);
817 	return cros_ec_suspend_prepare(ec_dev);
818 }
819 
cros_ec_lpc_complete(struct device * dev)820 static void cros_ec_lpc_complete(struct device *dev)
821 {
822 	struct cros_ec_device *ec_dev = dev_get_drvdata(dev);
823 	cros_ec_resume_complete(ec_dev);
824 }
825 
cros_ec_lpc_suspend_late(struct device * dev)826 static int cros_ec_lpc_suspend_late(struct device *dev)
827 {
828 	struct cros_ec_device *ec_dev = dev_get_drvdata(dev);
829 
830 	return cros_ec_suspend_late(ec_dev);
831 }
832 
cros_ec_lpc_resume_early(struct device * dev)833 static int cros_ec_lpc_resume_early(struct device *dev)
834 {
835 	struct cros_ec_device *ec_dev = dev_get_drvdata(dev);
836 
837 	return cros_ec_resume_early(ec_dev);
838 }
839 #endif
840 
841 static const struct dev_pm_ops cros_ec_lpc_pm_ops = {
842 #ifdef CONFIG_PM_SLEEP
843 	.prepare = cros_ec_lpc_prepare,
844 	.complete = cros_ec_lpc_complete,
845 #endif
846 	SET_LATE_SYSTEM_SLEEP_PM_OPS(cros_ec_lpc_suspend_late, cros_ec_lpc_resume_early)
847 };
848 
849 static struct platform_driver cros_ec_lpc_driver = {
850 	.driver = {
851 		.name = DRV_NAME,
852 		.acpi_match_table = cros_ec_lpc_acpi_device_ids,
853 		.pm = &cros_ec_lpc_pm_ops,
854 		/*
855 		 * ACPI child devices may probe before us, and they racily
856 		 * check our drvdata pointer. Force synchronous probe until
857 		 * those races are resolved.
858 		 */
859 		.probe_type = PROBE_FORCE_SYNCHRONOUS,
860 	},
861 	.probe = cros_ec_lpc_probe,
862 	.remove = cros_ec_lpc_remove,
863 };
864 
865 static struct platform_device cros_ec_lpc_device = {
866 	.name = DRV_NAME
867 };
868 
cros_ec_lpc_init(void)869 static int __init cros_ec_lpc_init(void)
870 {
871 	int ret;
872 	const struct dmi_system_id *dmi_match;
873 
874 	cros_ec_lpc_acpi_device_found = !!cros_ec_lpc_get_device(ACPI_DRV_NAME) ||
875 		!!cros_ec_lpc_get_device(FRMW_ACPI_DRV_NAME);
876 
877 	dmi_match = dmi_first_match(cros_ec_lpc_dmi_table);
878 
879 	if (!cros_ec_lpc_acpi_device_found && !dmi_match) {
880 		pr_err(DRV_NAME ": unsupported system.\n");
881 		return -ENODEV;
882 	}
883 
884 	/* Register the driver */
885 	ret = platform_driver_register(&cros_ec_lpc_driver);
886 	if (ret) {
887 		pr_err(DRV_NAME ": can't register driver: %d\n", ret);
888 		return ret;
889 	}
890 
891 	if (!cros_ec_lpc_acpi_device_found) {
892 		/* Pass the DMI match's driver data down to the platform device */
893 		platform_set_drvdata(&cros_ec_lpc_device, dmi_match->driver_data);
894 
895 		/* Register the device, and it'll get hooked up automatically */
896 		ret = platform_device_register(&cros_ec_lpc_device);
897 		if (ret) {
898 			pr_err(DRV_NAME ": can't register device: %d\n", ret);
899 			platform_driver_unregister(&cros_ec_lpc_driver);
900 		}
901 	}
902 
903 	return ret;
904 }
905 
cros_ec_lpc_exit(void)906 static void __exit cros_ec_lpc_exit(void)
907 {
908 	if (!cros_ec_lpc_acpi_device_found)
909 		platform_device_unregister(&cros_ec_lpc_device);
910 	platform_driver_unregister(&cros_ec_lpc_driver);
911 }
912 
913 module_init(cros_ec_lpc_init);
914 module_exit(cros_ec_lpc_exit);
915 
916 MODULE_LICENSE("GPL");
917 MODULE_DESCRIPTION("ChromeOS EC LPC driver");
918