Commit 6b7cb222 authored by Nick Crews's avatar Nick Crews Committed by Enric Balletbo i Serra

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: default avatarDuncan Laurie <dlaurie@google.com>
Signed-off-by: default avatarNick Crews <ncrews@chromium.org>
Signed-off-by: default avatarEnric Balletbo i Serra <enric.balletbo@collabora.com>
parent 67e9ac8c
...@@ -5,7 +5,6 @@ ...@@ -5,7 +5,6 @@
#include <linux/delay.h> #include <linux/delay.h>
#include <linux/io.h> #include <linux/io.h>
#include <linux/mfd/cros_ec_commands.h>
#include <linux/mutex.h> #include <linux/mutex.h>
#include <linux/types.h> #include <linux/types.h>
...@@ -16,6 +15,7 @@ ...@@ -16,6 +15,7 @@
* EC mutex because memmap data may be accessed without it being held. * EC mutex because memmap data may be accessed without it being held.
*/ */
static struct mutex io_mutex; static struct mutex io_mutex;
static u16 mec_emi_base, mec_emi_end;
/* /*
* cros_ec_lpc_mec_emi_write_address * cros_ec_lpc_mec_emi_write_address
...@@ -28,10 +28,37 @@ static struct mutex io_mutex; ...@@ -28,10 +28,37 @@ static struct mutex io_mutex;
static void cros_ec_lpc_mec_emi_write_address(u16 addr, static void cros_ec_lpc_mec_emi_write_address(u16 addr,
enum cros_ec_lpc_mec_emi_access_mode access_type) enum cros_ec_lpc_mec_emi_access_mode access_type)
{ {
/* Address relative to start of EMI range */ outb((addr & 0xfc) | access_type, MEC_EMI_EC_ADDRESS_B0(mec_emi_base));
addr -= MEC_EMI_RANGE_START; outb((addr >> 8) & 0x7f, MEC_EMI_EC_ADDRESS_B1(mec_emi_base));
outb((addr & 0xfc) | access_type, MEC_EMI_EC_ADDRESS_B0); }
outb((addr >> 8) & 0x7f, MEC_EMI_EC_ADDRESS_B1);
/**
* cros_ec_lpc_mec_in_range() - Determine if addresses are in MEC EMI range.
*
* @offset: Address offset
* @length: Number of bytes to check
*
* Return: 1 if in range, 0 if not, and -EINVAL on failure
* such as the mec range not being initialized
*/
int cros_ec_lpc_mec_in_range(unsigned int offset, unsigned int length)
{
if (length == 0)
return -EINVAL;
if (WARN_ON(mec_emi_base == 0 || mec_emi_end == 0))
return -EINVAL;
if (offset >= mec_emi_base && offset < mec_emi_end) {
if (WARN_ON(offset + length - 1 >= mec_emi_end))
return -EINVAL;
return 1;
}
if (WARN_ON(offset + length > mec_emi_base && offset < mec_emi_end))
return -EINVAL;
return 0;
} }
/* /*
...@@ -53,6 +80,11 @@ u8 cros_ec_lpc_io_bytes_mec(enum cros_ec_lpc_mec_io_type io_type, ...@@ -53,6 +80,11 @@ u8 cros_ec_lpc_io_bytes_mec(enum cros_ec_lpc_mec_io_type io_type,
u8 sum = 0; u8 sum = 0;
enum cros_ec_lpc_mec_emi_access_mode access, new_access; enum cros_ec_lpc_mec_emi_access_mode access, new_access;
/* Return checksum of 0 if window is not initialized */
WARN_ON(mec_emi_base == 0 || mec_emi_end == 0);
if (mec_emi_base == 0 || mec_emi_end == 0)
return 0;
/* /*
* Long access cannot be used on misaligned data since reading B0 loads * Long access cannot be used on misaligned data since reading B0 loads
* the data register and writing B3 flushes. * the data register and writing B3 flushes.
...@@ -68,9 +100,9 @@ u8 cros_ec_lpc_io_bytes_mec(enum cros_ec_lpc_mec_io_type io_type, ...@@ -68,9 +100,9 @@ u8 cros_ec_lpc_io_bytes_mec(enum cros_ec_lpc_mec_io_type io_type,
cros_ec_lpc_mec_emi_write_address(offset, access); cros_ec_lpc_mec_emi_write_address(offset, access);
/* Skip bytes in case of misaligned offset */ /* Skip bytes in case of misaligned offset */
io_addr = MEC_EMI_EC_DATA_B0 + (offset & 0x3); io_addr = MEC_EMI_EC_DATA_B0(mec_emi_base) + (offset & 0x3);
while (i < length) { while (i < length) {
while (io_addr <= MEC_EMI_EC_DATA_B3) { while (io_addr <= MEC_EMI_EC_DATA_B3(mec_emi_base)) {
if (io_type == MEC_IO_READ) if (io_type == MEC_IO_READ)
buf[i] = inb(io_addr++); buf[i] = inb(io_addr++);
else else
...@@ -100,7 +132,7 @@ u8 cros_ec_lpc_io_bytes_mec(enum cros_ec_lpc_mec_io_type io_type, ...@@ -100,7 +132,7 @@ u8 cros_ec_lpc_io_bytes_mec(enum cros_ec_lpc_mec_io_type io_type,
} }
/* Access [B0, B3] on each loop pass */ /* Access [B0, B3] on each loop pass */
io_addr = MEC_EMI_EC_DATA_B0; io_addr = MEC_EMI_EC_DATA_B0(mec_emi_base);
} }
done: done:
...@@ -110,9 +142,11 @@ u8 cros_ec_lpc_io_bytes_mec(enum cros_ec_lpc_mec_io_type io_type, ...@@ -110,9 +142,11 @@ u8 cros_ec_lpc_io_bytes_mec(enum cros_ec_lpc_mec_io_type io_type,
} }
EXPORT_SYMBOL(cros_ec_lpc_io_bytes_mec); EXPORT_SYMBOL(cros_ec_lpc_io_bytes_mec);
void cros_ec_lpc_mec_init(void) void cros_ec_lpc_mec_init(unsigned int base, unsigned int end)
{ {
mutex_init(&io_mutex); mutex_init(&io_mutex);
mec_emi_base = base;
mec_emi_end = end;
} }
EXPORT_SYMBOL(cros_ec_lpc_mec_init); EXPORT_SYMBOL(cros_ec_lpc_mec_init);
......
...@@ -8,8 +8,6 @@ ...@@ -8,8 +8,6 @@
#ifndef __CROS_EC_LPC_MEC_H #ifndef __CROS_EC_LPC_MEC_H
#define __CROS_EC_LPC_MEC_H #define __CROS_EC_LPC_MEC_H
#include <linux/mfd/cros_ec_commands.h>
enum cros_ec_lpc_mec_emi_access_mode { enum cros_ec_lpc_mec_emi_access_mode {
/* 8-bit access */ /* 8-bit access */
ACCESS_TYPE_BYTE = 0x0, ACCESS_TYPE_BYTE = 0x0,
...@@ -29,27 +27,23 @@ enum cros_ec_lpc_mec_io_type { ...@@ -29,27 +27,23 @@ enum cros_ec_lpc_mec_io_type {
MEC_IO_WRITE, MEC_IO_WRITE,
}; };
/* Access IO ranges 0x800 thru 0x9ff using EMI interface instead of LPC */
#define MEC_EMI_RANGE_START EC_HOST_CMD_REGION0
#define MEC_EMI_RANGE_END (EC_LPC_ADDR_MEMMAP + EC_MEMMAP_SIZE)
/* EMI registers are relative to base */ /* EMI registers are relative to base */
#define MEC_EMI_BASE 0x800 #define MEC_EMI_HOST_TO_EC(MEC_EMI_BASE) ((MEC_EMI_BASE) + 0)
#define MEC_EMI_HOST_TO_EC (MEC_EMI_BASE + 0) #define MEC_EMI_EC_TO_HOST(MEC_EMI_BASE) ((MEC_EMI_BASE) + 1)
#define MEC_EMI_EC_TO_HOST (MEC_EMI_BASE + 1) #define MEC_EMI_EC_ADDRESS_B0(MEC_EMI_BASE) ((MEC_EMI_BASE) + 2)
#define MEC_EMI_EC_ADDRESS_B0 (MEC_EMI_BASE + 2) #define MEC_EMI_EC_ADDRESS_B1(MEC_EMI_BASE) ((MEC_EMI_BASE) + 3)
#define MEC_EMI_EC_ADDRESS_B1 (MEC_EMI_BASE + 3) #define MEC_EMI_EC_DATA_B0(MEC_EMI_BASE) ((MEC_EMI_BASE) + 4)
#define MEC_EMI_EC_DATA_B0 (MEC_EMI_BASE + 4) #define MEC_EMI_EC_DATA_B1(MEC_EMI_BASE) ((MEC_EMI_BASE) + 5)
#define MEC_EMI_EC_DATA_B1 (MEC_EMI_BASE + 5) #define MEC_EMI_EC_DATA_B2(MEC_EMI_BASE) ((MEC_EMI_BASE) + 6)
#define MEC_EMI_EC_DATA_B2 (MEC_EMI_BASE + 6) #define MEC_EMI_EC_DATA_B3(MEC_EMI_BASE) ((MEC_EMI_BASE) + 7)
#define MEC_EMI_EC_DATA_B3 (MEC_EMI_BASE + 7)
/* /**
* cros_ec_lpc_mec_init * cros_ec_lpc_mec_init() - Initialize MEC I/O.
* *
* Initialize MEC I/O. * @base: MEC EMI Base address
* @end: MEC EMI End address
*/ */
void cros_ec_lpc_mec_init(void); void cros_ec_lpc_mec_init(unsigned int base, unsigned int end);
/* /*
* cros_ec_lpc_mec_destroy * cros_ec_lpc_mec_destroy
...@@ -58,6 +52,17 @@ void cros_ec_lpc_mec_init(void); ...@@ -58,6 +52,17 @@ void cros_ec_lpc_mec_init(void);
*/ */
void cros_ec_lpc_mec_destroy(void); void cros_ec_lpc_mec_destroy(void);
/**
* cros_ec_lpc_mec_in_range() - Determine if addresses are in MEC EMI range.
*
* @offset: Address offset
* @length: Number of bytes to check
*
* Return: 1 if in range, 0 if not, and -EINVAL on failure
* such as the mec range not being initialized
*/
int cros_ec_lpc_mec_in_range(unsigned int offset, unsigned int length);
/** /**
* cros_ec_lpc_io_bytes_mec - Read / write bytes to MEC EMI port * cros_ec_lpc_io_bytes_mec - Read / write bytes to MEC EMI port
* *
......
...@@ -41,51 +41,36 @@ static u8 lpc_write_bytes(unsigned int offset, unsigned int length, u8 *msg) ...@@ -41,51 +41,36 @@ static u8 lpc_write_bytes(unsigned int offset, unsigned int length, u8 *msg)
u8 cros_ec_lpc_read_bytes(unsigned int offset, unsigned int length, u8 *dest) u8 cros_ec_lpc_read_bytes(unsigned int offset, unsigned int length, u8 *dest)
{ {
if (length == 0) int in_range = cros_ec_lpc_mec_in_range(offset, length);
return 0;
/* Access desired range through EMI interface */
if (offset >= MEC_EMI_RANGE_START && offset <= MEC_EMI_RANGE_END) {
/* Ensure we don't straddle EMI region */
if (WARN_ON(offset + length - 1 > MEC_EMI_RANGE_END))
return 0;
return cros_ec_lpc_io_bytes_mec(MEC_IO_READ, offset, length, if (in_range < 0)
dest);
}
if (WARN_ON(offset + length > MEC_EMI_RANGE_START &&
offset < MEC_EMI_RANGE_START))
return 0; return 0;
return lpc_read_bytes(offset, length, dest); return in_range ?
cros_ec_lpc_io_bytes_mec(MEC_IO_READ,
offset - EC_HOST_CMD_REGION0,
length, dest) :
lpc_read_bytes(offset, length, dest);
} }
u8 cros_ec_lpc_write_bytes(unsigned int offset, unsigned int length, u8 *msg) u8 cros_ec_lpc_write_bytes(unsigned int offset, unsigned int length, u8 *msg)
{ {
if (length == 0) int in_range = cros_ec_lpc_mec_in_range(offset, length);
return 0;
/* Access desired range through EMI interface */
if (offset >= MEC_EMI_RANGE_START && offset <= MEC_EMI_RANGE_END) {
/* Ensure we don't straddle EMI region */
if (WARN_ON(offset + length - 1 > MEC_EMI_RANGE_END))
return 0;
return cros_ec_lpc_io_bytes_mec(MEC_IO_WRITE, offset, length, if (in_range < 0)
msg);
}
if (WARN_ON(offset + length > MEC_EMI_RANGE_START &&
offset < MEC_EMI_RANGE_START))
return 0; return 0;
return lpc_write_bytes(offset, length, msg); return in_range ?
cros_ec_lpc_io_bytes_mec(MEC_IO_WRITE,
offset - EC_HOST_CMD_REGION0,
length, msg) :
lpc_write_bytes(offset, length, msg);
} }
void cros_ec_lpc_reg_init(void) void cros_ec_lpc_reg_init(void)
{ {
cros_ec_lpc_mec_init(); cros_ec_lpc_mec_init(EC_HOST_CMD_REGION0,
EC_LPC_ADDR_MEMMAP + EC_MEMMAP_SIZE);
} }
void cros_ec_lpc_reg_destroy(void) void cros_ec_lpc_reg_destroy(void)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment