| .. | .. |
|---|
| 1 | | -/* |
|---|
| 2 | | - * cros_ec_lpc_mec - LPC variant I/O for Microchip EC |
|---|
| 3 | | - * |
|---|
| 4 | | - * Copyright (C) 2016 Google, Inc |
|---|
| 5 | | - * |
|---|
| 6 | | - * This software is licensed under the terms of the GNU General Public |
|---|
| 7 | | - * License version 2, as published by the Free Software Foundation, and |
|---|
| 8 | | - * may be copied, distributed, and modified under those terms. |
|---|
| 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. |
|---|
| 14 | | - * |
|---|
| 15 | | - * This driver uses the Chrome OS EC byte-level message-based protocol for |
|---|
| 16 | | - * communicating the keyboard state (which keys are pressed) from a keyboard EC |
|---|
| 17 | | - * to the AP over some bus (such as i2c, lpc, spi). The EC does debouncing, |
|---|
| 18 | | - * but everything else (including deghosting) is done here. The main |
|---|
| 19 | | - * motivation for this is to keep the EC firmware as simple as possible, since |
|---|
| 20 | | - * it cannot be easily upgraded and EC flash/IRAM space is relatively |
|---|
| 21 | | - * expensive. |
|---|
| 22 | | - */ |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
|---|
| 2 | +// LPC variant I/O for Microchip EC |
|---|
| 3 | +// |
|---|
| 4 | +// Copyright (C) 2016 Google, Inc |
|---|
| 23 | 5 | |
|---|
| 24 | 6 | #include <linux/delay.h> |
|---|
| 25 | 7 | #include <linux/io.h> |
|---|
| 26 | | -#include <linux/mfd/cros_ec_commands.h> |
|---|
| 27 | | -#include <linux/mfd/cros_ec_lpc_mec.h> |
|---|
| 28 | 8 | #include <linux/mutex.h> |
|---|
| 29 | 9 | #include <linux/types.h> |
|---|
| 10 | + |
|---|
| 11 | +#include "cros_ec_lpc_mec.h" |
|---|
| 30 | 12 | |
|---|
| 31 | 13 | /* |
|---|
| 32 | 14 | * This mutex must be held while accessing the EMI unit. We can't rely on the |
|---|
| 33 | 15 | * EC mutex because memmap data may be accessed without it being held. |
|---|
| 34 | 16 | */ |
|---|
| 35 | 17 | static struct mutex io_mutex; |
|---|
| 18 | +static u16 mec_emi_base, mec_emi_end; |
|---|
| 36 | 19 | |
|---|
| 37 | | -/* |
|---|
| 38 | | - * cros_ec_lpc_mec_emi_write_address |
|---|
| 20 | +/** |
|---|
| 21 | + * cros_ec_lpc_mec_emi_write_address() - Initialize EMI at a given address. |
|---|
| 39 | 22 | * |
|---|
| 40 | | - * Initialize EMI read / write at a given address. |
|---|
| 41 | | - * |
|---|
| 42 | | - * @addr: Starting read / write address |
|---|
| 23 | + * @addr: Starting read / write address |
|---|
| 43 | 24 | * @access_type: Type of access, typically 32-bit auto-increment |
|---|
| 44 | 25 | */ |
|---|
| 45 | 26 | static void cros_ec_lpc_mec_emi_write_address(u16 addr, |
|---|
| 46 | 27 | enum cros_ec_lpc_mec_emi_access_mode access_type) |
|---|
| 47 | 28 | { |
|---|
| 48 | | - /* Address relative to start of EMI range */ |
|---|
| 49 | | - addr -= MEC_EMI_RANGE_START; |
|---|
| 50 | | - outb((addr & 0xfc) | access_type, MEC_EMI_EC_ADDRESS_B0); |
|---|
| 51 | | - outb((addr >> 8) & 0x7f, MEC_EMI_EC_ADDRESS_B1); |
|---|
| 29 | + outb((addr & 0xfc) | access_type, MEC_EMI_EC_ADDRESS_B0(mec_emi_base)); |
|---|
| 30 | + outb((addr >> 8) & 0x7f, MEC_EMI_EC_ADDRESS_B1(mec_emi_base)); |
|---|
| 52 | 31 | } |
|---|
| 53 | 32 | |
|---|
| 54 | | -/* |
|---|
| 55 | | - * cros_ec_lpc_io_bytes_mec - Read / write bytes to MEC EMI port |
|---|
| 33 | +/** |
|---|
| 34 | + * cros_ec_lpc_mec_in_range() - Determine if addresses are in MEC EMI range. |
|---|
| 35 | + * |
|---|
| 36 | + * @offset: Address offset |
|---|
| 37 | + * @length: Number of bytes to check |
|---|
| 38 | + * |
|---|
| 39 | + * Return: 1 if in range, 0 if not, and -EINVAL on failure |
|---|
| 40 | + * such as the mec range not being initialized |
|---|
| 41 | + */ |
|---|
| 42 | +int cros_ec_lpc_mec_in_range(unsigned int offset, unsigned int length) |
|---|
| 43 | +{ |
|---|
| 44 | + if (length == 0) |
|---|
| 45 | + return -EINVAL; |
|---|
| 46 | + |
|---|
| 47 | + if (WARN_ON(mec_emi_base == 0 || mec_emi_end == 0)) |
|---|
| 48 | + return -EINVAL; |
|---|
| 49 | + |
|---|
| 50 | + if (offset >= mec_emi_base && offset < mec_emi_end) { |
|---|
| 51 | + if (WARN_ON(offset + length - 1 >= mec_emi_end)) |
|---|
| 52 | + return -EINVAL; |
|---|
| 53 | + return 1; |
|---|
| 54 | + } |
|---|
| 55 | + |
|---|
| 56 | + if (WARN_ON(offset + length > mec_emi_base && offset < mec_emi_end)) |
|---|
| 57 | + return -EINVAL; |
|---|
| 58 | + |
|---|
| 59 | + return 0; |
|---|
| 60 | +} |
|---|
| 61 | + |
|---|
| 62 | +/** |
|---|
| 63 | + * cros_ec_lpc_io_bytes_mec() - Read / write bytes to MEC EMI port. |
|---|
| 56 | 64 | * |
|---|
| 57 | 65 | * @io_type: MEC_IO_READ or MEC_IO_WRITE, depending on request |
|---|
| 58 | 66 | * @offset: Base read / write address |
|---|
| 59 | 67 | * @length: Number of bytes to read / write |
|---|
| 60 | 68 | * @buf: Destination / source buffer |
|---|
| 61 | 69 | * |
|---|
| 62 | | - * @return 8-bit checksum of all bytes read / written |
|---|
| 70 | + * Return: 8-bit checksum of all bytes read / written |
|---|
| 63 | 71 | */ |
|---|
| 64 | 72 | u8 cros_ec_lpc_io_bytes_mec(enum cros_ec_lpc_mec_io_type io_type, |
|---|
| 65 | 73 | unsigned int offset, unsigned int length, |
|---|
| .. | .. |
|---|
| 69 | 77 | int io_addr; |
|---|
| 70 | 78 | u8 sum = 0; |
|---|
| 71 | 79 | enum cros_ec_lpc_mec_emi_access_mode access, new_access; |
|---|
| 80 | + |
|---|
| 81 | + /* Return checksum of 0 if window is not initialized */ |
|---|
| 82 | + WARN_ON(mec_emi_base == 0 || mec_emi_end == 0); |
|---|
| 83 | + if (mec_emi_base == 0 || mec_emi_end == 0) |
|---|
| 84 | + return 0; |
|---|
| 72 | 85 | |
|---|
| 73 | 86 | /* |
|---|
| 74 | 87 | * Long access cannot be used on misaligned data since reading B0 loads |
|---|
| .. | .. |
|---|
| 85 | 98 | cros_ec_lpc_mec_emi_write_address(offset, access); |
|---|
| 86 | 99 | |
|---|
| 87 | 100 | /* Skip bytes in case of misaligned offset */ |
|---|
| 88 | | - io_addr = MEC_EMI_EC_DATA_B0 + (offset & 0x3); |
|---|
| 101 | + io_addr = MEC_EMI_EC_DATA_B0(mec_emi_base) + (offset & 0x3); |
|---|
| 89 | 102 | while (i < length) { |
|---|
| 90 | | - while (io_addr <= MEC_EMI_EC_DATA_B3) { |
|---|
| 103 | + while (io_addr <= MEC_EMI_EC_DATA_B3(mec_emi_base)) { |
|---|
| 91 | 104 | if (io_type == MEC_IO_READ) |
|---|
| 92 | 105 | buf[i] = inb(io_addr++); |
|---|
| 93 | 106 | else |
|---|
| .. | .. |
|---|
| 117 | 130 | } |
|---|
| 118 | 131 | |
|---|
| 119 | 132 | /* Access [B0, B3] on each loop pass */ |
|---|
| 120 | | - io_addr = MEC_EMI_EC_DATA_B0; |
|---|
| 133 | + io_addr = MEC_EMI_EC_DATA_B0(mec_emi_base); |
|---|
| 121 | 134 | } |
|---|
| 122 | 135 | |
|---|
| 123 | 136 | done: |
|---|
| .. | .. |
|---|
| 127 | 140 | } |
|---|
| 128 | 141 | EXPORT_SYMBOL(cros_ec_lpc_io_bytes_mec); |
|---|
| 129 | 142 | |
|---|
| 130 | | -void cros_ec_lpc_mec_init(void) |
|---|
| 143 | +void cros_ec_lpc_mec_init(unsigned int base, unsigned int end) |
|---|
| 131 | 144 | { |
|---|
| 132 | 145 | mutex_init(&io_mutex); |
|---|
| 146 | + mec_emi_base = base; |
|---|
| 147 | + mec_emi_end = end; |
|---|
| 133 | 148 | } |
|---|
| 134 | 149 | EXPORT_SYMBOL(cros_ec_lpc_mec_init); |
|---|
| 135 | 150 | |
|---|