1515
1616#include "comm-host.h"
1717
18+ /* from comm-lpc.c */
1819int wait_for_ec (int status_addr , int timeout_usec );
1920
20- #define INITIAL_UDELAY 5 /* 5 us */
21- #define MAXIMUM_UDELAY 10000 /* 10 ms */
22-
2321typedef enum _ec_xfer_direction { EC_MEC_WRITE , EC_MEC_READ } ec_xfer_direction ;
2422
25- // As defined in MEC172x section 16.8.3
26- // https://ww1.microchip.com/downloads/en/DeviceDoc/MEC172x-Data-Sheet-DS00003583C.pdf
23+ /* As defined in MEC172x section 16.8.3
24+ * https://ww1.microchip.com/downloads/en/DeviceDoc/MEC172x-Data-Sheet-DS00003583C.pdf
25+ */
2726#define MEC_EC_BYTE_ACCESS 0x00
2827#define MEC_EC_LONG_ACCESS_AUTOINCREMENT 0x03
2928
3029#define MEC_EC_ADDRESS_REGISTER0 0x0802
3130#define MEC_EC_ADDRESS_REGISTER1 0x0803
3231#define MEC_EC_DATA_REGISTER0 0x0804
3332#define MEC_EC_DATA_REGISTER2 0x0806
33+ #define MEC_EC_MEMMAP_START 0x100
3434
3535static int ec_mec_xfer (ec_xfer_direction direction , uint16_t address ,
3636 char * data , uint16_t size )
3737{
38+ /*
39+ * There's a cleverer way to do this, but it's somewhat less clear what's happening.
40+ * I prefer clarity over cleverness. :)
41+ */
3842 int pos = 0 ;
3943 uint16_t temp [2 ];
4044 if (address % 4 > 0 ) {
@@ -47,13 +51,11 @@ static int ec_mec_xfer(ec_xfer_direction direction, uint16_t address,
4751 else if (direction == EC_MEC_READ )
4852 * storage = inb (MEC_EC_DATA_REGISTER0 + i );
4953 }
50- address = (address + 4 ) & 0xFFFC ; // Up to next multiple of 4
54+ address = (address + 4 ) & 0xFFFC ;
5155 }
5256
5357 if (size - pos >= 4 ) {
5458 outw ((address & 0xFFFC ) | MEC_EC_LONG_ACCESS_AUTOINCREMENT , MEC_EC_ADDRESS_REGISTER0 );
55- // Chunk writing for anything large, 4 bytes at a time
56- // Writing to 804, 806 automatically increments dest address
5759 while (size - pos >= 4 ) {
5860 if (direction == EC_MEC_WRITE ) {
5961 memcpy (temp , & data [pos ], sizeof (temp ));
@@ -71,7 +73,6 @@ static int ec_mec_xfer(ec_xfer_direction direction, uint16_t address,
7173 }
7274
7375 if (size - pos > 0 ) {
74- // Unaligned remaining data - R/W it by byte
7576 outw ((address & 0xFFFC ) | MEC_EC_BYTE_ACCESS , MEC_EC_ADDRESS_REGISTER0 );
7677 for (int i = 0 ; i < (size - pos ); ++ i ) {
7778 char * storage = & data [pos + i ];
@@ -180,8 +181,27 @@ static int ec_command_lpc_mec_3(int command, int version, const void *outdata,
180181
181182static int ec_readmem_lpc_mec (int offset , int bytes , void * dest )
182183{
183- ec_mec_xfer (EC_MEC_READ , (EC_LPC_ADDR_MEMMAP - EC_HOST_CMD_REGION0 ) | (offset & 0x7FFF ), dest , bytes );
184- return bytes ;
184+ int xfer_offset = MEC_EC_MEMMAP_START + offset ;
185+ int cnt = 0 ;
186+ char * s = dest ;
187+
188+ if (offset >= EC_MEMMAP_SIZE - bytes )
189+ return -1 ;
190+
191+ if (bytes ) {
192+ ec_mec_xfer (EC_MEC_READ , xfer_offset , dest , bytes );
193+ cnt = bytes ;
194+ } else {
195+ /* Somewhat brute-force to set up a bunch of
196+ * individual transfers, but clearer than copying the xfer code
197+ * to add a stop condition.
198+ */
199+ do {
200+ ec_mec_xfer (EC_MEC_READ , xfer_offset ++ , s ++ , 1 );
201+ cnt ++ ;
202+ } while (* s != '\0' && xfer_offset < EC_MEMMAP_SIZE );
203+ }
204+ return cnt ;
185205}
186206
187207int comm_init_lpc_mec (void )
0 commit comments