1 // SPDX-License-Identifier: GPL-2.0
2 // LPC variant I/O for Microchip EC
4 // Copyright (C) 2016 Google, Inc
6 #include <linux/delay.h>
8 #include <linux/mutex.h>
9 #include <linux/types.h>
11 #include "cros_ec_lpc_mec.h"
13 #define ACPI_LOCK_DELAY_MS 500
16 * This mutex must be held while accessing the EMI unit. We can't rely on the
17 * EC mutex because memmap data may be accessed without it being held.
19 static DEFINE_MUTEX(io_mutex);
21 * An alternative mutex to be used when the ACPI AML code may also
22 * access memmap data. When set, this mutex is used in preference to
25 static acpi_handle aml_mutex;
27 static u16 mec_emi_base, mec_emi_end;
30 * cros_ec_lpc_mec_lock() - Acquire mutex for EMI
32 * @return: Negative error code, or zero for success
34 static int cros_ec_lpc_mec_lock(void)
39 mutex_lock(&io_mutex);
43 success = ACPI_SUCCESS(acpi_acquire_mutex(aml_mutex,
44 NULL, ACPI_LOCK_DELAY_MS));
52 * cros_ec_lpc_mec_unlock() - Release mutex for EMI
54 * @return: Negative error code, or zero for success
56 static int cros_ec_lpc_mec_unlock(void)
61 mutex_unlock(&io_mutex);
65 success = ACPI_SUCCESS(acpi_release_mutex(aml_mutex, NULL));
73 * cros_ec_lpc_mec_emi_write_address() - Initialize EMI at a given address.
75 * @addr: Starting read / write address
76 * @access_type: Type of access, typically 32-bit auto-increment
78 static void cros_ec_lpc_mec_emi_write_address(u16 addr,
79 enum cros_ec_lpc_mec_emi_access_mode access_type)
81 outb((addr & 0xfc) | access_type, MEC_EMI_EC_ADDRESS_B0(mec_emi_base));
82 outb((addr >> 8) & 0x7f, MEC_EMI_EC_ADDRESS_B1(mec_emi_base));
86 * cros_ec_lpc_mec_in_range() - Determine if addresses are in MEC EMI range.
88 * @offset: Address offset
89 * @length: Number of bytes to check
91 * Return: 1 if in range, 0 if not, and -EINVAL on failure
92 * such as the mec range not being initialized
94 int cros_ec_lpc_mec_in_range(unsigned int offset, unsigned int length)
96 if (WARN_ON(mec_emi_base == 0 || mec_emi_end == 0))
99 if (offset >= mec_emi_base && offset < mec_emi_end) {
100 if (WARN_ON(offset + length - 1 >= mec_emi_end))
105 if (WARN_ON(offset + length > mec_emi_base && offset < mec_emi_end))
112 * cros_ec_lpc_io_bytes_mec() - Read / write bytes to MEC EMI port.
114 * @io_type: MEC_IO_READ or MEC_IO_WRITE, depending on request
115 * @offset: Base read / write address
116 * @length: Number of bytes to read / write
117 * @buf: Destination / source buffer
119 * @return: A negative error code on error, or 8-bit checksum of all
120 * bytes read / written
122 int cros_ec_lpc_io_bytes_mec(enum cros_ec_lpc_mec_io_type io_type,
123 unsigned int offset, unsigned int length,
129 enum cros_ec_lpc_mec_emi_access_mode access, new_access;
135 /* Return checksum of 0 if window is not initialized */
136 WARN_ON(mec_emi_base == 0 || mec_emi_end == 0);
137 if (mec_emi_base == 0 || mec_emi_end == 0)
141 * Long access cannot be used on misaligned data since reading B0 loads
142 * the data register and writing B3 flushes.
144 if (offset & 0x3 || length < 4)
145 access = ACCESS_TYPE_BYTE;
147 access = ACCESS_TYPE_LONG_AUTO_INCREMENT;
149 ret = cros_ec_lpc_mec_lock();
153 /* Initialize I/O at desired address */
154 cros_ec_lpc_mec_emi_write_address(offset, access);
156 /* Skip bytes in case of misaligned offset */
157 io_addr = MEC_EMI_EC_DATA_B0(mec_emi_base) + (offset & 0x3);
159 while (io_addr <= MEC_EMI_EC_DATA_B3(mec_emi_base)) {
160 if (io_type == MEC_IO_READ)
161 buf[i] = inb(io_addr++);
163 outb(buf[i], io_addr++);
168 /* Extra bounds check in case of misaligned length */
174 * Use long auto-increment access except for misaligned write,
175 * since writing B3 triggers the flush.
177 if (length - i < 4 && io_type == MEC_IO_WRITE)
178 new_access = ACCESS_TYPE_BYTE;
180 new_access = ACCESS_TYPE_LONG_AUTO_INCREMENT;
182 if (new_access != access ||
183 access != ACCESS_TYPE_LONG_AUTO_INCREMENT) {
185 cros_ec_lpc_mec_emi_write_address(offset, access);
188 /* Access [B0, B3] on each loop pass */
189 io_addr = MEC_EMI_EC_DATA_B0(mec_emi_base);
193 ret = cros_ec_lpc_mec_unlock();
199 EXPORT_SYMBOL(cros_ec_lpc_io_bytes_mec);
201 void cros_ec_lpc_mec_init(unsigned int base, unsigned int end)
206 EXPORT_SYMBOL(cros_ec_lpc_mec_init);
208 int cros_ec_lpc_mec_acpi_mutex(struct acpi_device *adev, const char *pathname)
215 status = acpi_get_handle(adev->handle, pathname, &aml_mutex);
216 if (ACPI_FAILURE(status))
221 EXPORT_SYMBOL(cros_ec_lpc_mec_acpi_mutex);