]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - drivers/misc/cros_ec_lpc.c
b94501e474217f5444c2bf7da9aebf86066c4e0b
[karo-tx-uboot.git] / drivers / misc / cros_ec_lpc.c
1 /*
2  * Chromium OS cros_ec driver - LPC interface
3  *
4  * Copyright (c) 2012 The Chromium OS Authors.
5  *
6  * SPDX-License-Identifier:     GPL-2.0+
7  */
8
9 /*
10  * The Matrix Keyboard Protocol driver handles talking to the keyboard
11  * controller chip. Mostly this is for keyboard functions, but some other
12  * things have slipped in, so we provide generic services to talk to the
13  * KBC.
14  */
15
16 #include <common.h>
17 #include <dm.h>
18 #include <command.h>
19 #include <cros_ec.h>
20 #include <asm/io.h>
21
22 #ifdef DEBUG_TRACE
23 #define debug_trace(fmt, b...)  debug(fmt, ##b)
24 #else
25 #define debug_trace(fmt, b...)
26 #endif
27
28 static int wait_for_sync(struct cros_ec_dev *dev)
29 {
30         unsigned long start;
31
32         start = get_timer(0);
33         while (inb(EC_LPC_ADDR_HOST_CMD) & EC_LPC_STATUS_BUSY_MASK) {
34                 if (get_timer(start) > 1000) {
35                         debug("%s: Timeout waiting for CROS_EC sync\n",
36                               __func__);
37                         return -1;
38                 }
39         }
40
41         return 0;
42 }
43
44 #ifdef CONFIG_DM_CROS_EC
45 int cros_ec_lpc_command(struct udevice *udev, uint8_t cmd, int cmd_version,
46                      const uint8_t *dout, int dout_len,
47                      uint8_t **dinp, int din_len)
48 {
49         struct cros_ec_dev *dev = dev_get_uclass_priv(udev);
50 #else
51 int cros_ec_lpc_command(struct cros_ec_dev *dev, uint8_t cmd, int cmd_version,
52                      const uint8_t *dout, int dout_len,
53                      uint8_t **dinp, int din_len)
54 {
55 #endif
56         const int cmd_addr = EC_LPC_ADDR_HOST_CMD;
57         const int data_addr = EC_LPC_ADDR_HOST_DATA;
58         const int args_addr = EC_LPC_ADDR_HOST_ARGS;
59         const int param_addr = EC_LPC_ADDR_HOST_PARAM;
60
61         struct ec_lpc_host_args args;
62         uint8_t *d;
63         int csum;
64         int i;
65
66         if (dout_len > EC_PROTO2_MAX_PARAM_SIZE) {
67                 debug("%s: Cannot send %d bytes\n", __func__, dout_len);
68                 return -1;
69         }
70
71         /* Fill in args */
72         args.flags = EC_HOST_ARGS_FLAG_FROM_HOST;
73         args.command_version = cmd_version;
74         args.data_size = dout_len;
75
76         /* Calculate checksum */
77         csum = cmd + args.flags + args.command_version + args.data_size;
78         for (i = 0, d = (uint8_t *)dout; i < dout_len; i++, d++)
79                 csum += *d;
80
81         args.checksum = (uint8_t)csum;
82
83         if (wait_for_sync(dev)) {
84                 debug("%s: Timeout waiting ready\n", __func__);
85                 return -1;
86         }
87
88         /* Write args */
89         for (i = 0, d = (uint8_t *)&args; i < sizeof(args); i++, d++)
90                 outb(*d, args_addr + i);
91
92         /* Write data, if any */
93         debug_trace("cmd: %02x, ver: %02x", cmd, cmd_version);
94         for (i = 0, d = (uint8_t *)dout; i < dout_len; i++, d++) {
95                 outb(*d, param_addr + i);
96                 debug_trace("%02x ", *d);
97         }
98
99         outb(cmd, cmd_addr);
100         debug_trace("\n");
101
102         if (wait_for_sync(dev)) {
103                 debug("%s: Timeout waiting for response\n", __func__);
104                 return -1;
105         }
106
107         /* Check result */
108         i = inb(data_addr);
109         if (i) {
110                 debug("%s: CROS_EC result code %d\n", __func__, i);
111                 return -i;
112         }
113
114         /* Read back args */
115         for (i = 0, d = (uint8_t *)&args; i < sizeof(args); i++, d++)
116                 *d = inb(args_addr + i);
117
118         /*
119          * If EC didn't modify args flags, then somehow we sent a new-style
120          * command to an old EC, which means it would have read its params
121          * from the wrong place.
122          */
123         if (!(args.flags & EC_HOST_ARGS_FLAG_TO_HOST)) {
124                 debug("%s: CROS_EC protocol mismatch\n", __func__);
125                 return -EC_RES_INVALID_RESPONSE;
126         }
127
128         if (args.data_size > din_len) {
129                 debug("%s: CROS_EC returned too much data %d > %d\n",
130                       __func__, args.data_size, din_len);
131                 return -EC_RES_INVALID_RESPONSE;
132         }
133
134         /* Read data, if any */
135         for (i = 0, d = (uint8_t *)dev->din; i < args.data_size; i++, d++) {
136                 *d = inb(param_addr + i);
137                 debug_trace("%02x ", *d);
138         }
139         debug_trace("\n");
140
141         /* Verify checksum */
142         csum = cmd + args.flags + args.command_version + args.data_size;
143         for (i = 0, d = (uint8_t *)dev->din; i < args.data_size; i++, d++)
144                 csum += *d;
145
146         if (args.checksum != (uint8_t)csum) {
147                 debug("%s: CROS_EC response has invalid checksum\n", __func__);
148                 return -EC_RES_INVALID_CHECKSUM;
149         }
150         *dinp = dev->din;
151
152         /* Return actual amount of data received */
153         return args.data_size;
154 }
155
156 /**
157  * Initialize LPC protocol.
158  *
159  * @param dev           CROS_EC device
160  * @param blob          Device tree blob
161  * @return 0 if ok, -1 on error
162  */
163 int cros_ec_lpc_init(struct cros_ec_dev *dev, const void *blob)
164 {
165         int byte, i;
166
167         /* See if we can find an EC at the other end */
168         byte = 0xff;
169         byte &= inb(EC_LPC_ADDR_HOST_CMD);
170         byte &= inb(EC_LPC_ADDR_HOST_DATA);
171         for (i = 0; i < EC_PROTO2_MAX_PARAM_SIZE && (byte == 0xff); i++)
172                 byte &= inb(EC_LPC_ADDR_HOST_PARAM + i);
173         if (byte == 0xff) {
174                 debug("%s: CROS_EC device not found on LPC bus\n",
175                         __func__);
176                 return -1;
177         }
178
179         return 0;
180 }
181
182 /*
183  * Test if LPC command args are supported.
184  *
185  * The cheapest way to do this is by looking for the memory-mapped
186  * flag.  This is faster than sending a new-style 'hello' command and
187  * seeing whether the EC sets the EC_HOST_ARGS_FLAG_FROM_HOST flag
188  * in args when it responds.
189  */
190 #ifdef CONFIG_DM_CROS_EC
191 static int cros_ec_lpc_check_version(struct udevice *dev)
192 #else
193 int cros_ec_lpc_check_version(struct cros_ec_dev *dev)
194 #endif
195 {
196         if (inb(EC_LPC_ADDR_MEMMAP + EC_MEMMAP_ID) == 'E' &&
197                         inb(EC_LPC_ADDR_MEMMAP + EC_MEMMAP_ID + 1)
198                                 == 'C' &&
199                         (inb(EC_LPC_ADDR_MEMMAP +
200                                 EC_MEMMAP_HOST_CMD_FLAGS) &
201                                 EC_HOST_CMD_FLAG_LPC_ARGS_SUPPORTED)) {
202                 return 0;
203         }
204
205         printf("%s: ERROR: old EC interface not supported\n", __func__);
206         return -1;
207 }
208
209 #ifdef CONFIG_DM_CROS_EC
210 static int cros_ec_probe(struct udevice *dev)
211 {
212         return cros_ec_register(dev);
213 }
214
215 static struct dm_cros_ec_ops cros_ec_ops = {
216         .command = cros_ec_lpc_command,
217         .check_version = cros_ec_lpc_check_version,
218 };
219
220 static const struct udevice_id cros_ec_ids[] = {
221         { .compatible = "google,cros-ec" },
222         { }
223 };
224
225 U_BOOT_DRIVER(cros_ec_lpc) = {
226         .name           = "cros_ec",
227         .id             = UCLASS_CROS_EC,
228         .of_match       = cros_ec_ids,
229         .probe          = cros_ec_probe,
230         .ops            = &cros_ec_ops,
231 };
232 #endif