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"
14 * This mutex must be held while accessing the EMI unit. We can't rely on the
15 * EC mutex because memmap data may be accessed without it being held.
17 static struct mutex io_mutex
;
18 static u16 mec_emi_base
, mec_emi_end
;
21 * cros_ec_lpc_mec_emi_write_address() - Initialize EMI at a given address.
23 * @addr: Starting read / write address
24 * @access_type: Type of access, typically 32-bit auto-increment
26 static void cros_ec_lpc_mec_emi_write_address(u16 addr
,
27 enum cros_ec_lpc_mec_emi_access_mode access_type
)
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
));
34 * cros_ec_lpc_mec_in_range() - Determine if addresses are in MEC EMI range.
36 * @offset: Address offset
37 * @length: Number of bytes to check
39 * Return: 1 if in range, 0 if not, and -EINVAL on failure
40 * such as the mec range not being initialized
42 int cros_ec_lpc_mec_in_range(unsigned int offset
, unsigned int length
)
47 if (WARN_ON(mec_emi_base
== 0 || mec_emi_end
== 0))
50 if (offset
>= mec_emi_base
&& offset
< mec_emi_end
) {
51 if (WARN_ON(offset
+ length
- 1 >= mec_emi_end
))
56 if (WARN_ON(offset
+ length
> mec_emi_base
&& offset
< mec_emi_end
))
63 * cros_ec_lpc_io_bytes_mec() - Read / write bytes to MEC EMI port.
65 * @io_type: MEC_IO_READ or MEC_IO_WRITE, depending on request
66 * @offset: Base read / write address
67 * @length: Number of bytes to read / write
68 * @buf: Destination / source buffer
70 * Return: 8-bit checksum of all bytes read / written
72 u8
cros_ec_lpc_io_bytes_mec(enum cros_ec_lpc_mec_io_type io_type
,
73 unsigned int offset
, unsigned int length
,
79 enum cros_ec_lpc_mec_emi_access_mode access
, new_access
;
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)
87 * Long access cannot be used on misaligned data since reading B0 loads
88 * the data register and writing B3 flushes.
90 if (offset
& 0x3 || length
< 4)
91 access
= ACCESS_TYPE_BYTE
;
93 access
= ACCESS_TYPE_LONG_AUTO_INCREMENT
;
95 mutex_lock(&io_mutex
);
97 /* Initialize I/O at desired address */
98 cros_ec_lpc_mec_emi_write_address(offset
, access
);
100 /* Skip bytes in case of misaligned offset */
101 io_addr
= MEC_EMI_EC_DATA_B0(mec_emi_base
) + (offset
& 0x3);
103 while (io_addr
<= MEC_EMI_EC_DATA_B3(mec_emi_base
)) {
104 if (io_type
== MEC_IO_READ
)
105 buf
[i
] = inb(io_addr
++);
107 outb(buf
[i
], io_addr
++);
112 /* Extra bounds check in case of misaligned length */
118 * Use long auto-increment access except for misaligned write,
119 * since writing B3 triggers the flush.
121 if (length
- i
< 4 && io_type
== MEC_IO_WRITE
)
122 new_access
= ACCESS_TYPE_BYTE
;
124 new_access
= ACCESS_TYPE_LONG_AUTO_INCREMENT
;
126 if (new_access
!= access
||
127 access
!= ACCESS_TYPE_LONG_AUTO_INCREMENT
) {
129 cros_ec_lpc_mec_emi_write_address(offset
, access
);
132 /* Access [B0, B3] on each loop pass */
133 io_addr
= MEC_EMI_EC_DATA_B0(mec_emi_base
);
137 mutex_unlock(&io_mutex
);
141 EXPORT_SYMBOL(cros_ec_lpc_io_bytes_mec
);
143 void cros_ec_lpc_mec_init(unsigned int base
, unsigned int end
)
145 mutex_init(&io_mutex
);
149 EXPORT_SYMBOL(cros_ec_lpc_mec_init
);
151 void cros_ec_lpc_mec_destroy(void)
153 mutex_destroy(&io_mutex
);
155 EXPORT_SYMBOL(cros_ec_lpc_mec_destroy
);