Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

platform/chrome: cros_ec: Remove cros_ec dependency in lpc_mec

In order to allow this code to be re-used, remove the dependency
on the rest of the cros_ec code from the cros_ec_lpc_mec functions.

Instead of using a hardcoded register base address of 0x800 have
this be passed in to cros_ec_lpc_mec_init(). The existing cros_ec
use case now passes in the 0x800 base address this way.

There are some error checks that happen in cros_ec_lpc_mec_in_range()
that probably shouldn't be there, as they are checking kernel-space
callers and not user-space input. However, we'll just do the refactor in
this patch, and in a future patch might remove this error checking and
fix all the instances of code that calls this.

There's a similar problem in cros_ec_lpc_read_bytes(), where we return a
checksum, but on error just return 0. This should probably be changed so
that it returns int, but we don't want to have to mess with all the
calling code for this fix. Maybe we'll come back through later and fix
this.

Signed-off-by: Duncan Laurie <dlaurie@google.com>
Signed-off-by: Nick Crews <ncrews@chromium.org>
Signed-off-by: Enric Balletbo i Serra <enric.balletbo@collabora.com>

authored by

Nick Crews and committed by
Enric Balletbo i Serra
6b7cb222 67e9ac8c

+85 -61
+43 -9
drivers/platform/chrome/cros_ec_lpc_mec.c
··· 5 5 6 6 #include <linux/delay.h> 7 7 #include <linux/io.h> 8 - #include <linux/mfd/cros_ec_commands.h> 9 8 #include <linux/mutex.h> 10 9 #include <linux/types.h> 11 10 ··· 15 16 * EC mutex because memmap data may be accessed without it being held. 16 17 */ 17 18 static struct mutex io_mutex; 19 + static u16 mec_emi_base, mec_emi_end; 18 20 19 21 /* 20 22 * cros_ec_lpc_mec_emi_write_address ··· 28 28 static void cros_ec_lpc_mec_emi_write_address(u16 addr, 29 29 enum cros_ec_lpc_mec_emi_access_mode access_type) 30 30 { 31 - /* Address relative to start of EMI range */ 32 - addr -= MEC_EMI_RANGE_START; 33 - outb((addr & 0xfc) | access_type, MEC_EMI_EC_ADDRESS_B0); 34 - outb((addr >> 8) & 0x7f, MEC_EMI_EC_ADDRESS_B1); 31 + outb((addr & 0xfc) | access_type, MEC_EMI_EC_ADDRESS_B0(mec_emi_base)); 32 + outb((addr >> 8) & 0x7f, MEC_EMI_EC_ADDRESS_B1(mec_emi_base)); 33 + } 34 + 35 + /** 36 + * cros_ec_lpc_mec_in_range() - Determine if addresses are in MEC EMI range. 37 + * 38 + * @offset: Address offset 39 + * @length: Number of bytes to check 40 + * 41 + * Return: 1 if in range, 0 if not, and -EINVAL on failure 42 + * such as the mec range not being initialized 43 + */ 44 + int cros_ec_lpc_mec_in_range(unsigned int offset, unsigned int length) 45 + { 46 + if (length == 0) 47 + return -EINVAL; 48 + 49 + if (WARN_ON(mec_emi_base == 0 || mec_emi_end == 0)) 50 + return -EINVAL; 51 + 52 + if (offset >= mec_emi_base && offset < mec_emi_end) { 53 + if (WARN_ON(offset + length - 1 >= mec_emi_end)) 54 + return -EINVAL; 55 + return 1; 56 + } 57 + 58 + if (WARN_ON(offset + length > mec_emi_base && offset < mec_emi_end)) 59 + return -EINVAL; 60 + 61 + return 0; 35 62 } 36 63 37 64 /* ··· 80 53 u8 sum = 0; 81 54 enum cros_ec_lpc_mec_emi_access_mode access, new_access; 82 55 56 + /* Return checksum of 0 if window is not initialized */ 57 + WARN_ON(mec_emi_base == 0 || mec_emi_end == 0); 58 + if (mec_emi_base == 0 || mec_emi_end == 0) 59 + return 0; 60 + 83 61 /* 84 62 * Long access cannot be used on misaligned data since reading B0 loads 85 63 * the data register and writing B3 flushes. ··· 100 68 cros_ec_lpc_mec_emi_write_address(offset, access); 101 69 102 70 /* Skip bytes in case of misaligned offset */ 103 - io_addr = MEC_EMI_EC_DATA_B0 + (offset & 0x3); 71 + io_addr = MEC_EMI_EC_DATA_B0(mec_emi_base) + (offset & 0x3); 104 72 while (i < length) { 105 - while (io_addr <= MEC_EMI_EC_DATA_B3) { 73 + while (io_addr <= MEC_EMI_EC_DATA_B3(mec_emi_base)) { 106 74 if (io_type == MEC_IO_READ) 107 75 buf[i] = inb(io_addr++); 108 76 else ··· 132 100 } 133 101 134 102 /* Access [B0, B3] on each loop pass */ 135 - io_addr = MEC_EMI_EC_DATA_B0; 103 + io_addr = MEC_EMI_EC_DATA_B0(mec_emi_base); 136 104 } 137 105 138 106 done: ··· 142 110 } 143 111 EXPORT_SYMBOL(cros_ec_lpc_io_bytes_mec); 144 112 145 - void cros_ec_lpc_mec_init(void) 113 + void cros_ec_lpc_mec_init(unsigned int base, unsigned int end) 146 114 { 147 115 mutex_init(&io_mutex); 116 + mec_emi_base = base; 117 + mec_emi_end = end; 148 118 } 149 119 EXPORT_SYMBOL(cros_ec_lpc_mec_init); 150 120
+24 -19
drivers/platform/chrome/cros_ec_lpc_mec.h
··· 8 8 #ifndef __CROS_EC_LPC_MEC_H 9 9 #define __CROS_EC_LPC_MEC_H 10 10 11 - #include <linux/mfd/cros_ec_commands.h> 12 - 13 11 enum cros_ec_lpc_mec_emi_access_mode { 14 12 /* 8-bit access */ 15 13 ACCESS_TYPE_BYTE = 0x0, ··· 27 29 MEC_IO_WRITE, 28 30 }; 29 31 30 - /* Access IO ranges 0x800 thru 0x9ff using EMI interface instead of LPC */ 31 - #define MEC_EMI_RANGE_START EC_HOST_CMD_REGION0 32 - #define MEC_EMI_RANGE_END (EC_LPC_ADDR_MEMMAP + EC_MEMMAP_SIZE) 33 - 34 32 /* EMI registers are relative to base */ 35 - #define MEC_EMI_BASE 0x800 36 - #define MEC_EMI_HOST_TO_EC (MEC_EMI_BASE + 0) 37 - #define MEC_EMI_EC_TO_HOST (MEC_EMI_BASE + 1) 38 - #define MEC_EMI_EC_ADDRESS_B0 (MEC_EMI_BASE + 2) 39 - #define MEC_EMI_EC_ADDRESS_B1 (MEC_EMI_BASE + 3) 40 - #define MEC_EMI_EC_DATA_B0 (MEC_EMI_BASE + 4) 41 - #define MEC_EMI_EC_DATA_B1 (MEC_EMI_BASE + 5) 42 - #define MEC_EMI_EC_DATA_B2 (MEC_EMI_BASE + 6) 43 - #define MEC_EMI_EC_DATA_B3 (MEC_EMI_BASE + 7) 33 + #define MEC_EMI_HOST_TO_EC(MEC_EMI_BASE) ((MEC_EMI_BASE) + 0) 34 + #define MEC_EMI_EC_TO_HOST(MEC_EMI_BASE) ((MEC_EMI_BASE) + 1) 35 + #define MEC_EMI_EC_ADDRESS_B0(MEC_EMI_BASE) ((MEC_EMI_BASE) + 2) 36 + #define MEC_EMI_EC_ADDRESS_B1(MEC_EMI_BASE) ((MEC_EMI_BASE) + 3) 37 + #define MEC_EMI_EC_DATA_B0(MEC_EMI_BASE) ((MEC_EMI_BASE) + 4) 38 + #define MEC_EMI_EC_DATA_B1(MEC_EMI_BASE) ((MEC_EMI_BASE) + 5) 39 + #define MEC_EMI_EC_DATA_B2(MEC_EMI_BASE) ((MEC_EMI_BASE) + 6) 40 + #define MEC_EMI_EC_DATA_B3(MEC_EMI_BASE) ((MEC_EMI_BASE) + 7) 44 41 45 - /* 46 - * cros_ec_lpc_mec_init 42 + /** 43 + * cros_ec_lpc_mec_init() - Initialize MEC I/O. 47 44 * 48 - * Initialize MEC I/O. 45 + * @base: MEC EMI Base address 46 + * @end: MEC EMI End address 49 47 */ 50 - void cros_ec_lpc_mec_init(void); 48 + void cros_ec_lpc_mec_init(unsigned int base, unsigned int end); 51 49 52 50 /* 53 51 * cros_ec_lpc_mec_destroy ··· 51 57 * Cleanup MEC I/O. 52 58 */ 53 59 void cros_ec_lpc_mec_destroy(void); 60 + 61 + /** 62 + * cros_ec_lpc_mec_in_range() - Determine if addresses are in MEC EMI range. 63 + * 64 + * @offset: Address offset 65 + * @length: Number of bytes to check 66 + * 67 + * Return: 1 if in range, 0 if not, and -EINVAL on failure 68 + * such as the mec range not being initialized 69 + */ 70 + int cros_ec_lpc_mec_in_range(unsigned int offset, unsigned int length); 54 71 55 72 /** 56 73 * cros_ec_lpc_io_bytes_mec - Read / write bytes to MEC EMI port
+18 -33
drivers/platform/chrome/cros_ec_lpc_reg.c
··· 41 41 42 42 u8 cros_ec_lpc_read_bytes(unsigned int offset, unsigned int length, u8 *dest) 43 43 { 44 - if (length == 0) 44 + int in_range = cros_ec_lpc_mec_in_range(offset, length); 45 + 46 + if (in_range < 0) 45 47 return 0; 46 48 47 - /* Access desired range through EMI interface */ 48 - if (offset >= MEC_EMI_RANGE_START && offset <= MEC_EMI_RANGE_END) { 49 - /* Ensure we don't straddle EMI region */ 50 - if (WARN_ON(offset + length - 1 > MEC_EMI_RANGE_END)) 51 - return 0; 52 - 53 - return cros_ec_lpc_io_bytes_mec(MEC_IO_READ, offset, length, 54 - dest); 55 - } 56 - 57 - if (WARN_ON(offset + length > MEC_EMI_RANGE_START && 58 - offset < MEC_EMI_RANGE_START)) 59 - return 0; 60 - 61 - return lpc_read_bytes(offset, length, dest); 49 + return in_range ? 50 + cros_ec_lpc_io_bytes_mec(MEC_IO_READ, 51 + offset - EC_HOST_CMD_REGION0, 52 + length, dest) : 53 + lpc_read_bytes(offset, length, dest); 62 54 } 63 55 64 56 u8 cros_ec_lpc_write_bytes(unsigned int offset, unsigned int length, u8 *msg) 65 57 { 66 - if (length == 0) 58 + int in_range = cros_ec_lpc_mec_in_range(offset, length); 59 + 60 + if (in_range < 0) 67 61 return 0; 68 62 69 - /* Access desired range through EMI interface */ 70 - if (offset >= MEC_EMI_RANGE_START && offset <= MEC_EMI_RANGE_END) { 71 - /* Ensure we don't straddle EMI region */ 72 - if (WARN_ON(offset + length - 1 > MEC_EMI_RANGE_END)) 73 - return 0; 74 - 75 - return cros_ec_lpc_io_bytes_mec(MEC_IO_WRITE, offset, length, 76 - msg); 77 - } 78 - 79 - if (WARN_ON(offset + length > MEC_EMI_RANGE_START && 80 - offset < MEC_EMI_RANGE_START)) 81 - return 0; 82 - 83 - return lpc_write_bytes(offset, length, msg); 63 + return in_range ? 64 + cros_ec_lpc_io_bytes_mec(MEC_IO_WRITE, 65 + offset - EC_HOST_CMD_REGION0, 66 + length, msg) : 67 + lpc_write_bytes(offset, length, msg); 84 68 } 85 69 86 70 void cros_ec_lpc_reg_init(void) 87 71 { 88 - cros_ec_lpc_mec_init(); 72 + cros_ec_lpc_mec_init(EC_HOST_CMD_REGION0, 73 + EC_LPC_ADDR_MEMMAP + EC_MEMMAP_SIZE); 89 74 } 90 75 91 76 void cros_ec_lpc_reg_destroy(void)