xref: /openbmc/linux/drivers/platform/chrome/cros_ec_lpc_mec.c (revision ac8f933664c3a0e2d42f6ee9a2a6d25f87cb23f6)
11058ca94SEnric Balletbo i Serra // SPDX-License-Identifier: GPL-2.0
21058ca94SEnric Balletbo i Serra // LPC variant I/O for Microchip EC
31058ca94SEnric Balletbo i Serra //
41058ca94SEnric Balletbo i Serra // Copyright (C) 2016 Google, Inc
58d4a3dc4SShawn Nematbakhsh 
68d4a3dc4SShawn Nematbakhsh #include <linux/delay.h>
78d4a3dc4SShawn Nematbakhsh #include <linux/io.h>
88d4a3dc4SShawn Nematbakhsh #include <linux/mutex.h>
98d4a3dc4SShawn Nematbakhsh #include <linux/types.h>
108d4a3dc4SShawn Nematbakhsh 
11cc8a4ea1SEnric Balletbo i Serra #include "cros_ec_lpc_mec.h"
12cc8a4ea1SEnric Balletbo i Serra 
13*0b722b81SBen Walsh #define ACPI_LOCK_DELAY_MS 500
14*0b722b81SBen Walsh 
158d4a3dc4SShawn Nematbakhsh /*
168d4a3dc4SShawn Nematbakhsh  * This mutex must be held while accessing the EMI unit. We can't rely on the
178d4a3dc4SShawn Nematbakhsh  * EC mutex because memmap data may be accessed without it being held.
188d4a3dc4SShawn Nematbakhsh  */
19d61b3f9bSYe Bin static DEFINE_MUTEX(io_mutex);
20*0b722b81SBen Walsh /*
21*0b722b81SBen Walsh  * An alternative mutex to be used when the ACPI AML code may also
22*0b722b81SBen Walsh  * access memmap data.  When set, this mutex is used in preference to
23*0b722b81SBen Walsh  * io_mutex.
24*0b722b81SBen Walsh  */
25*0b722b81SBen Walsh static acpi_handle aml_mutex;
26*0b722b81SBen Walsh 
276b7cb222SNick Crews static u16 mec_emi_base, mec_emi_end;
288d4a3dc4SShawn Nematbakhsh 
299eecd07bSEnric Balletbo i Serra /**
30*0b722b81SBen Walsh  * cros_ec_lpc_mec_lock() - Acquire mutex for EMI
31*0b722b81SBen Walsh  *
32*0b722b81SBen Walsh  * @return: Negative error code, or zero for success
33*0b722b81SBen Walsh  */
cros_ec_lpc_mec_lock(void)34*0b722b81SBen Walsh static int cros_ec_lpc_mec_lock(void)
35*0b722b81SBen Walsh {
36*0b722b81SBen Walsh 	bool success;
37*0b722b81SBen Walsh 
38*0b722b81SBen Walsh 	if (!aml_mutex) {
39*0b722b81SBen Walsh 		mutex_lock(&io_mutex);
40*0b722b81SBen Walsh 		return 0;
41*0b722b81SBen Walsh 	}
42*0b722b81SBen Walsh 
43*0b722b81SBen Walsh 	success = ACPI_SUCCESS(acpi_acquire_mutex(aml_mutex,
44*0b722b81SBen Walsh 						  NULL, ACPI_LOCK_DELAY_MS));
45*0b722b81SBen Walsh 	if (!success)
46*0b722b81SBen Walsh 		return -EBUSY;
47*0b722b81SBen Walsh 
48*0b722b81SBen Walsh 	return 0;
49*0b722b81SBen Walsh }
50*0b722b81SBen Walsh 
51*0b722b81SBen Walsh /**
52*0b722b81SBen Walsh  * cros_ec_lpc_mec_unlock() - Release mutex for EMI
53*0b722b81SBen Walsh  *
54*0b722b81SBen Walsh  * @return: Negative error code, or zero for success
55*0b722b81SBen Walsh  */
cros_ec_lpc_mec_unlock(void)56*0b722b81SBen Walsh static int cros_ec_lpc_mec_unlock(void)
57*0b722b81SBen Walsh {
58*0b722b81SBen Walsh 	bool success;
59*0b722b81SBen Walsh 
60*0b722b81SBen Walsh 	if (!aml_mutex) {
61*0b722b81SBen Walsh 		mutex_unlock(&io_mutex);
62*0b722b81SBen Walsh 		return 0;
63*0b722b81SBen Walsh 	}
64*0b722b81SBen Walsh 
65*0b722b81SBen Walsh 	success = ACPI_SUCCESS(acpi_release_mutex(aml_mutex, NULL));
66*0b722b81SBen Walsh 	if (!success)
67*0b722b81SBen Walsh 		return -EBUSY;
68*0b722b81SBen Walsh 
69*0b722b81SBen Walsh 	return 0;
70*0b722b81SBen Walsh }
71*0b722b81SBen Walsh 
72*0b722b81SBen Walsh /**
739eecd07bSEnric Balletbo i Serra  * cros_ec_lpc_mec_emi_write_address() - Initialize EMI at a given address.
748d4a3dc4SShawn Nematbakhsh  *
758d4a3dc4SShawn Nematbakhsh  * @addr: Starting read / write address
768d4a3dc4SShawn Nematbakhsh  * @access_type: Type of access, typically 32-bit auto-increment
778d4a3dc4SShawn Nematbakhsh  */
cros_ec_lpc_mec_emi_write_address(u16 addr,enum cros_ec_lpc_mec_emi_access_mode access_type)788d4a3dc4SShawn Nematbakhsh static void cros_ec_lpc_mec_emi_write_address(u16 addr,
798d4a3dc4SShawn Nematbakhsh 			enum cros_ec_lpc_mec_emi_access_mode access_type)
808d4a3dc4SShawn Nematbakhsh {
816b7cb222SNick Crews 	outb((addr & 0xfc) | access_type, MEC_EMI_EC_ADDRESS_B0(mec_emi_base));
826b7cb222SNick Crews 	outb((addr >> 8) & 0x7f, MEC_EMI_EC_ADDRESS_B1(mec_emi_base));
836b7cb222SNick Crews }
846b7cb222SNick Crews 
856b7cb222SNick Crews /**
866b7cb222SNick Crews  * cros_ec_lpc_mec_in_range() - Determine if addresses are in MEC EMI range.
876b7cb222SNick Crews  *
886b7cb222SNick Crews  * @offset: Address offset
896b7cb222SNick Crews  * @length: Number of bytes to check
906b7cb222SNick Crews  *
916b7cb222SNick Crews  * Return: 1 if in range, 0 if not, and -EINVAL on failure
926b7cb222SNick Crews  *         such as the mec range not being initialized
936b7cb222SNick Crews  */
cros_ec_lpc_mec_in_range(unsigned int offset,unsigned int length)946b7cb222SNick Crews int cros_ec_lpc_mec_in_range(unsigned int offset, unsigned int length)
956b7cb222SNick Crews {
966b7cb222SNick Crews 	if (length == 0)
976b7cb222SNick Crews 		return -EINVAL;
986b7cb222SNick Crews 
996b7cb222SNick Crews 	if (WARN_ON(mec_emi_base == 0 || mec_emi_end == 0))
1006b7cb222SNick Crews 		return -EINVAL;
1016b7cb222SNick Crews 
1026b7cb222SNick Crews 	if (offset >= mec_emi_base && offset < mec_emi_end) {
1036b7cb222SNick Crews 		if (WARN_ON(offset + length - 1 >= mec_emi_end))
1046b7cb222SNick Crews 			return -EINVAL;
1056b7cb222SNick Crews 		return 1;
1066b7cb222SNick Crews 	}
1076b7cb222SNick Crews 
1086b7cb222SNick Crews 	if (WARN_ON(offset + length > mec_emi_base && offset < mec_emi_end))
1096b7cb222SNick Crews 		return -EINVAL;
1106b7cb222SNick Crews 
1116b7cb222SNick Crews 	return 0;
1128d4a3dc4SShawn Nematbakhsh }
1138d4a3dc4SShawn Nematbakhsh 
1149eecd07bSEnric Balletbo i Serra /**
1159eecd07bSEnric Balletbo i Serra  * cros_ec_lpc_io_bytes_mec() - Read / write bytes to MEC EMI port.
1168d4a3dc4SShawn Nematbakhsh  *
1178d4a3dc4SShawn Nematbakhsh  * @io_type: MEC_IO_READ or MEC_IO_WRITE, depending on request
1188d4a3dc4SShawn Nematbakhsh  * @offset:  Base read / write address
1198d4a3dc4SShawn Nematbakhsh  * @length:  Number of bytes to read / write
1208d4a3dc4SShawn Nematbakhsh  * @buf:     Destination / source buffer
1218d4a3dc4SShawn Nematbakhsh  *
1229eecd07bSEnric Balletbo i Serra  * Return: 8-bit checksum of all bytes read / written
1238d4a3dc4SShawn Nematbakhsh  */
cros_ec_lpc_io_bytes_mec(enum cros_ec_lpc_mec_io_type io_type,unsigned int offset,unsigned int length,u8 * buf)1248d4a3dc4SShawn Nematbakhsh u8 cros_ec_lpc_io_bytes_mec(enum cros_ec_lpc_mec_io_type io_type,
1258d4a3dc4SShawn Nematbakhsh 			    unsigned int offset, unsigned int length,
1268d4a3dc4SShawn Nematbakhsh 			    u8 *buf)
1278d4a3dc4SShawn Nematbakhsh {
1288d4a3dc4SShawn Nematbakhsh 	int i = 0;
1298d4a3dc4SShawn Nematbakhsh 	int io_addr;
1308d4a3dc4SShawn Nematbakhsh 	u8 sum = 0;
1318d4a3dc4SShawn Nematbakhsh 	enum cros_ec_lpc_mec_emi_access_mode access, new_access;
132*0b722b81SBen Walsh 	int ret;
1338d4a3dc4SShawn Nematbakhsh 
1346b7cb222SNick Crews 	/* Return checksum of 0 if window is not initialized */
1356b7cb222SNick Crews 	WARN_ON(mec_emi_base == 0 || mec_emi_end == 0);
1366b7cb222SNick Crews 	if (mec_emi_base == 0 || mec_emi_end == 0)
1376b7cb222SNick Crews 		return 0;
1386b7cb222SNick Crews 
1398d4a3dc4SShawn Nematbakhsh 	/*
1408d4a3dc4SShawn Nematbakhsh 	 * Long access cannot be used on misaligned data since reading B0 loads
1418d4a3dc4SShawn Nematbakhsh 	 * the data register and writing B3 flushes.
1428d4a3dc4SShawn Nematbakhsh 	 */
1438d4a3dc4SShawn Nematbakhsh 	if (offset & 0x3 || length < 4)
1448d4a3dc4SShawn Nematbakhsh 		access = ACCESS_TYPE_BYTE;
1458d4a3dc4SShawn Nematbakhsh 	else
1468d4a3dc4SShawn Nematbakhsh 		access = ACCESS_TYPE_LONG_AUTO_INCREMENT;
1478d4a3dc4SShawn Nematbakhsh 
148*0b722b81SBen Walsh 	ret = cros_ec_lpc_mec_lock();
149*0b722b81SBen Walsh 	if (ret)
150*0b722b81SBen Walsh 		return ret;
1518d4a3dc4SShawn Nematbakhsh 
1528d4a3dc4SShawn Nematbakhsh 	/* Initialize I/O at desired address */
1538d4a3dc4SShawn Nematbakhsh 	cros_ec_lpc_mec_emi_write_address(offset, access);
1548d4a3dc4SShawn Nematbakhsh 
1558d4a3dc4SShawn Nematbakhsh 	/* Skip bytes in case of misaligned offset */
1566b7cb222SNick Crews 	io_addr = MEC_EMI_EC_DATA_B0(mec_emi_base) + (offset & 0x3);
1578d4a3dc4SShawn Nematbakhsh 	while (i < length) {
1586b7cb222SNick Crews 		while (io_addr <= MEC_EMI_EC_DATA_B3(mec_emi_base)) {
1598d4a3dc4SShawn Nematbakhsh 			if (io_type == MEC_IO_READ)
1608d4a3dc4SShawn Nematbakhsh 				buf[i] = inb(io_addr++);
1618d4a3dc4SShawn Nematbakhsh 			else
1628d4a3dc4SShawn Nematbakhsh 				outb(buf[i], io_addr++);
1638d4a3dc4SShawn Nematbakhsh 
1648d4a3dc4SShawn Nematbakhsh 			sum += buf[i++];
1658d4a3dc4SShawn Nematbakhsh 			offset++;
1668d4a3dc4SShawn Nematbakhsh 
1678d4a3dc4SShawn Nematbakhsh 			/* Extra bounds check in case of misaligned length */
1688d4a3dc4SShawn Nematbakhsh 			if (i == length)
1698d4a3dc4SShawn Nematbakhsh 				goto done;
1708d4a3dc4SShawn Nematbakhsh 		}
1718d4a3dc4SShawn Nematbakhsh 
1728d4a3dc4SShawn Nematbakhsh 		/*
1738d4a3dc4SShawn Nematbakhsh 		 * Use long auto-increment access except for misaligned write,
1748d4a3dc4SShawn Nematbakhsh 		 * since writing B3 triggers the flush.
1758d4a3dc4SShawn Nematbakhsh 		 */
1768d4a3dc4SShawn Nematbakhsh 		if (length - i < 4 && io_type == MEC_IO_WRITE)
1778d4a3dc4SShawn Nematbakhsh 			new_access = ACCESS_TYPE_BYTE;
1788d4a3dc4SShawn Nematbakhsh 		else
1798d4a3dc4SShawn Nematbakhsh 			new_access = ACCESS_TYPE_LONG_AUTO_INCREMENT;
1808d4a3dc4SShawn Nematbakhsh 
1818d4a3dc4SShawn Nematbakhsh 		if (new_access != access ||
1828d4a3dc4SShawn Nematbakhsh 		    access != ACCESS_TYPE_LONG_AUTO_INCREMENT) {
1838d4a3dc4SShawn Nematbakhsh 			access = new_access;
1848d4a3dc4SShawn Nematbakhsh 			cros_ec_lpc_mec_emi_write_address(offset, access);
1858d4a3dc4SShawn Nematbakhsh 		}
1868d4a3dc4SShawn Nematbakhsh 
1878d4a3dc4SShawn Nematbakhsh 		/* Access [B0, B3] on each loop pass */
1886b7cb222SNick Crews 		io_addr = MEC_EMI_EC_DATA_B0(mec_emi_base);
1898d4a3dc4SShawn Nematbakhsh 	}
1908d4a3dc4SShawn Nematbakhsh 
1918d4a3dc4SShawn Nematbakhsh done:
192*0b722b81SBen Walsh 	ret = cros_ec_lpc_mec_unlock();
193*0b722b81SBen Walsh 	if (ret)
194*0b722b81SBen Walsh 		return ret;
1958d4a3dc4SShawn Nematbakhsh 
1968d4a3dc4SShawn Nematbakhsh 	return sum;
1978d4a3dc4SShawn Nematbakhsh }
1988d4a3dc4SShawn Nematbakhsh EXPORT_SYMBOL(cros_ec_lpc_io_bytes_mec);
1998d4a3dc4SShawn Nematbakhsh 
cros_ec_lpc_mec_init(unsigned int base,unsigned int end)2006b7cb222SNick Crews void cros_ec_lpc_mec_init(unsigned int base, unsigned int end)
2018d4a3dc4SShawn Nematbakhsh {
2026b7cb222SNick Crews 	mec_emi_base = base;
2036b7cb222SNick Crews 	mec_emi_end = end;
2048d4a3dc4SShawn Nematbakhsh }
2058d4a3dc4SShawn Nematbakhsh EXPORT_SYMBOL(cros_ec_lpc_mec_init);
206*0b722b81SBen Walsh 
cros_ec_lpc_mec_acpi_mutex(struct acpi_device * adev,const char * pathname)207*0b722b81SBen Walsh int cros_ec_lpc_mec_acpi_mutex(struct acpi_device *adev, const char *pathname)
208*0b722b81SBen Walsh {
209*0b722b81SBen Walsh 	int status;
210*0b722b81SBen Walsh 
211*0b722b81SBen Walsh 	if (!adev)
212*0b722b81SBen Walsh 		return -ENOENT;
213*0b722b81SBen Walsh 
214*0b722b81SBen Walsh 	status = acpi_get_handle(adev->handle, pathname, &aml_mutex);
215*0b722b81SBen Walsh 	if (ACPI_FAILURE(status))
216*0b722b81SBen Walsh 		return -ENOENT;
217*0b722b81SBen Walsh 
218*0b722b81SBen Walsh 	return 0;
219*0b722b81SBen Walsh }
220*0b722b81SBen Walsh EXPORT_SYMBOL(cros_ec_lpc_mec_acpi_mutex);
221