c4edfa83e493062fcd65363e4733387562211ef5
[sfrench/cifs-2.6.git] / drivers / platform / chrome / cros_ec_lpc_mec.c
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  */
23
24 #include <linux/delay.h>
25 #include <linux/io.h>
26 #include <linux/mfd/cros_ec_commands.h>
27 #include <linux/mutex.h>
28 #include <linux/types.h>
29
30 #include "cros_ec_lpc_mec.h"
31
32 /*
33  * This mutex must be held while accessing the EMI unit. We can't rely on the
34  * EC mutex because memmap data may be accessed without it being held.
35  */
36 static struct mutex io_mutex;
37
38 /*
39  * cros_ec_lpc_mec_emi_write_address
40  *
41  * Initialize EMI read / write at a given address.
42  *
43  * @addr:        Starting read / write address
44  * @access_type: Type of access, typically 32-bit auto-increment
45  */
46 static void cros_ec_lpc_mec_emi_write_address(u16 addr,
47                         enum cros_ec_lpc_mec_emi_access_mode access_type)
48 {
49         /* Address relative to start of EMI range */
50         addr -= MEC_EMI_RANGE_START;
51         outb((addr & 0xfc) | access_type, MEC_EMI_EC_ADDRESS_B0);
52         outb((addr >> 8) & 0x7f, MEC_EMI_EC_ADDRESS_B1);
53 }
54
55 /*
56  * cros_ec_lpc_io_bytes_mec - Read / write bytes to MEC EMI port
57  *
58  * @io_type: MEC_IO_READ or MEC_IO_WRITE, depending on request
59  * @offset:  Base read / write address
60  * @length:  Number of bytes to read / write
61  * @buf:     Destination / source buffer
62  *
63  * @return 8-bit checksum of all bytes read / written
64  */
65 u8 cros_ec_lpc_io_bytes_mec(enum cros_ec_lpc_mec_io_type io_type,
66                             unsigned int offset, unsigned int length,
67                             u8 *buf)
68 {
69         int i = 0;
70         int io_addr;
71         u8 sum = 0;
72         enum cros_ec_lpc_mec_emi_access_mode access, new_access;
73
74         /*
75          * Long access cannot be used on misaligned data since reading B0 loads
76          * the data register and writing B3 flushes.
77          */
78         if (offset & 0x3 || length < 4)
79                 access = ACCESS_TYPE_BYTE;
80         else
81                 access = ACCESS_TYPE_LONG_AUTO_INCREMENT;
82
83         mutex_lock(&io_mutex);
84
85         /* Initialize I/O at desired address */
86         cros_ec_lpc_mec_emi_write_address(offset, access);
87
88         /* Skip bytes in case of misaligned offset */
89         io_addr = MEC_EMI_EC_DATA_B0 + (offset & 0x3);
90         while (i < length) {
91                 while (io_addr <= MEC_EMI_EC_DATA_B3) {
92                         if (io_type == MEC_IO_READ)
93                                 buf[i] = inb(io_addr++);
94                         else
95                                 outb(buf[i], io_addr++);
96
97                         sum += buf[i++];
98                         offset++;
99
100                         /* Extra bounds check in case of misaligned length */
101                         if (i == length)
102                                 goto done;
103                 }
104
105                 /*
106                  * Use long auto-increment access except for misaligned write,
107                  * since writing B3 triggers the flush.
108                  */
109                 if (length - i < 4 && io_type == MEC_IO_WRITE)
110                         new_access = ACCESS_TYPE_BYTE;
111                 else
112                         new_access = ACCESS_TYPE_LONG_AUTO_INCREMENT;
113
114                 if (new_access != access ||
115                     access != ACCESS_TYPE_LONG_AUTO_INCREMENT) {
116                         access = new_access;
117                         cros_ec_lpc_mec_emi_write_address(offset, access);
118                 }
119
120                 /* Access [B0, B3] on each loop pass */
121                 io_addr = MEC_EMI_EC_DATA_B0;
122         }
123
124 done:
125         mutex_unlock(&io_mutex);
126
127         return sum;
128 }
129 EXPORT_SYMBOL(cros_ec_lpc_io_bytes_mec);
130
131 void cros_ec_lpc_mec_init(void)
132 {
133         mutex_init(&io_mutex);
134 }
135 EXPORT_SYMBOL(cros_ec_lpc_mec_init);
136
137 void cros_ec_lpc_mec_destroy(void)
138 {
139         mutex_destroy(&io_mutex);
140 }
141 EXPORT_SYMBOL(cros_ec_lpc_mec_destroy);