1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * UCSI driver for ChromeOS EC
4 *
5 * Copyright 2024 Google LLC.
6 */
7
8 #include <linux/container_of.h>
9 #include <linux/dev_printk.h>
10 #include <linux/jiffies.h>
11 #include <linux/mod_devicetable.h>
12 #include <linux/module.h>
13 #include <linux/platform_data/cros_ec_commands.h>
14 #include <linux/platform_data/cros_usbpd_notify.h>
15 #include <linux/platform_data/cros_ec_proto.h>
16 #include <linux/platform_device.h>
17 #include <linux/slab.h>
18 #include <linux/wait.h>
19
20 #include "ucsi.h"
21
22 /*
23 * Maximum size in bytes of a UCSI message between AP and EC
24 */
25 #define MAX_EC_DATA_SIZE 256
26
27 /*
28 * Maximum time in milliseconds the cros_ec_ucsi driver
29 * will wait for a response to a command or and ack.
30 */
31 #define WRITE_TMO_MS 5000
32
33 /* Number of times to attempt recovery from a write timeout before giving up. */
34 #define WRITE_TMO_CTR_MAX 5
35
36 struct cros_ucsi_data {
37 struct device *dev;
38 struct ucsi *ucsi;
39
40 struct cros_ec_device *ec;
41 struct notifier_block nb;
42 struct work_struct work;
43 struct delayed_work write_tmo;
44 int tmo_counter;
45
46 struct completion complete;
47 unsigned long flags;
48 };
49
cros_ucsi_read(struct ucsi * ucsi,unsigned int offset,void * val,size_t val_len)50 static int cros_ucsi_read(struct ucsi *ucsi, unsigned int offset, void *val,
51 size_t val_len)
52 {
53 struct cros_ucsi_data *udata = ucsi_get_drvdata(ucsi);
54 struct ec_params_ucsi_ppm_get req = {
55 .offset = offset,
56 .size = val_len,
57 };
58 int ret;
59
60 if (val_len > MAX_EC_DATA_SIZE) {
61 dev_err(udata->dev, "Can't read %zu bytes. Too big.\n", val_len);
62 return -EINVAL;
63 }
64
65 ret = cros_ec_cmd(udata->ec, 0, EC_CMD_UCSI_PPM_GET,
66 &req, sizeof(req), val, val_len);
67 if (ret < 0) {
68 dev_warn(udata->dev, "Failed to send EC message UCSI_PPM_GET: error=%d\n", ret);
69 return ret;
70 }
71 return 0;
72 }
73
cros_ucsi_read_version(struct ucsi * ucsi,u16 * version)74 static int cros_ucsi_read_version(struct ucsi *ucsi, u16 *version)
75 {
76 return cros_ucsi_read(ucsi, UCSI_VERSION, version, sizeof(*version));
77 }
78
cros_ucsi_read_cci(struct ucsi * ucsi,u32 * cci)79 static int cros_ucsi_read_cci(struct ucsi *ucsi, u32 *cci)
80 {
81 return cros_ucsi_read(ucsi, UCSI_CCI, cci, sizeof(*cci));
82 }
83
cros_ucsi_read_message_in(struct ucsi * ucsi,void * val,size_t val_len)84 static int cros_ucsi_read_message_in(struct ucsi *ucsi, void *val,
85 size_t val_len)
86 {
87 return cros_ucsi_read(ucsi, UCSI_MESSAGE_IN, val, val_len);
88 }
89
cros_ucsi_async_control(struct ucsi * ucsi,u64 cmd)90 static int cros_ucsi_async_control(struct ucsi *ucsi, u64 cmd)
91 {
92 struct cros_ucsi_data *udata = ucsi_get_drvdata(ucsi);
93 u8 ec_buf[sizeof(struct ec_params_ucsi_ppm_set) + sizeof(cmd)];
94 struct ec_params_ucsi_ppm_set *req = (struct ec_params_ucsi_ppm_set *) ec_buf;
95 int ret;
96
97 req->offset = UCSI_CONTROL;
98 memcpy(req->data, &cmd, sizeof(cmd));
99 ret = cros_ec_cmd(udata->ec, 0, EC_CMD_UCSI_PPM_SET,
100 req, sizeof(ec_buf), NULL, 0);
101 if (ret < 0) {
102 dev_warn(udata->dev, "Failed to send EC message UCSI_PPM_SET: error=%d\n", ret);
103 return ret;
104 }
105 return 0;
106 }
107
cros_ucsi_sync_control(struct ucsi * ucsi,u64 cmd,u32 * cci,void * data,size_t size)108 static int cros_ucsi_sync_control(struct ucsi *ucsi, u64 cmd, u32 *cci,
109 void *data, size_t size)
110 {
111 struct cros_ucsi_data *udata = ucsi_get_drvdata(ucsi);
112 int ret;
113
114 ret = ucsi_sync_control_common(ucsi, cmd, cci, data, size);
115 switch (ret) {
116 case -EBUSY:
117 /* EC may return -EBUSY if CCI.busy is set.
118 * Convert this to a timeout.
119 */
120 case -ETIMEDOUT:
121 /* Schedule recovery attempt when we timeout
122 * or tried to send a command while still busy.
123 */
124 cancel_delayed_work_sync(&udata->write_tmo);
125 schedule_delayed_work(&udata->write_tmo,
126 msecs_to_jiffies(WRITE_TMO_MS));
127 break;
128 case 0:
129 /* Successful write. Cancel any pending recovery work. */
130 cancel_delayed_work_sync(&udata->write_tmo);
131 break;
132 }
133
134 return ret;
135 }
136
137 static const struct ucsi_operations cros_ucsi_ops = {
138 .read_version = cros_ucsi_read_version,
139 .read_cci = cros_ucsi_read_cci,
140 .poll_cci = cros_ucsi_read_cci,
141 .read_message_in = cros_ucsi_read_message_in,
142 .async_control = cros_ucsi_async_control,
143 .sync_control = cros_ucsi_sync_control,
144 };
145
cros_ucsi_work(struct work_struct * work)146 static void cros_ucsi_work(struct work_struct *work)
147 {
148 struct cros_ucsi_data *udata = container_of(work, struct cros_ucsi_data, work);
149 u32 cci;
150
151 if (cros_ucsi_read_cci(udata->ucsi, &cci))
152 return;
153
154 ucsi_notify_common(udata->ucsi, cci);
155 }
156
cros_ucsi_write_timeout(struct work_struct * work)157 static void cros_ucsi_write_timeout(struct work_struct *work)
158 {
159 struct cros_ucsi_data *udata =
160 container_of(work, struct cros_ucsi_data, write_tmo.work);
161 u32 cci;
162 u64 cmd;
163
164 if (cros_ucsi_read(udata->ucsi, UCSI_CCI, &cci, sizeof(cci))) {
165 dev_err(udata->dev,
166 "Reading CCI failed; no write timeout recovery possible.\n");
167 return;
168 }
169
170 if (cci & UCSI_CCI_BUSY) {
171 udata->tmo_counter++;
172
173 if (udata->tmo_counter <= WRITE_TMO_CTR_MAX)
174 schedule_delayed_work(&udata->write_tmo,
175 msecs_to_jiffies(WRITE_TMO_MS));
176 else
177 dev_err(udata->dev,
178 "PPM unresponsive - too many write timeouts.\n");
179
180 return;
181 }
182
183 /* No longer busy means we can reset our timeout counter. */
184 udata->tmo_counter = 0;
185
186 /* Need to ack previous command which may have timed out. */
187 if (cci & UCSI_CCI_COMMAND_COMPLETE) {
188 cmd = UCSI_ACK_CC_CI | UCSI_ACK_COMMAND_COMPLETE;
189 cros_ucsi_async_control(udata->ucsi, cmd);
190
191 /* Check again after a few seconds that the system has
192 * recovered to make sure our async write above was successful.
193 */
194 schedule_delayed_work(&udata->write_tmo,
195 msecs_to_jiffies(WRITE_TMO_MS));
196 return;
197 }
198
199 /* We recovered from a previous timeout. Treat this as a recovery from
200 * suspend and call resume.
201 */
202 ucsi_resume(udata->ucsi);
203 }
204
cros_ucsi_event(struct notifier_block * nb,unsigned long host_event,void * _notify)205 static int cros_ucsi_event(struct notifier_block *nb,
206 unsigned long host_event, void *_notify)
207 {
208 struct cros_ucsi_data *udata = container_of(nb, struct cros_ucsi_data, nb);
209
210 if (host_event & PD_EVENT_INIT) {
211 /* Late init event received from ChromeOS EC. Treat this as a
212 * system resume to re-enable communication with the PPM.
213 */
214 dev_dbg(udata->dev, "Late PD init received\n");
215 ucsi_resume(udata->ucsi);
216 }
217
218 if (host_event & PD_EVENT_PPM) {
219 dev_dbg(udata->dev, "UCSI notification received\n");
220 flush_work(&udata->work);
221 schedule_work(&udata->work);
222 }
223
224 return NOTIFY_OK;
225 }
226
cros_ucsi_destroy(struct cros_ucsi_data * udata)227 static void cros_ucsi_destroy(struct cros_ucsi_data *udata)
228 {
229 cros_usbpd_unregister_notify(&udata->nb);
230 cancel_delayed_work_sync(&udata->write_tmo);
231 cancel_work_sync(&udata->work);
232 ucsi_destroy(udata->ucsi);
233 }
234
cros_ucsi_probe(struct platform_device * pdev)235 static int cros_ucsi_probe(struct platform_device *pdev)
236 {
237 struct device *dev = &pdev->dev;
238 struct cros_ec_dev *ec_data = dev_get_drvdata(dev->parent);
239 struct cros_ucsi_data *udata;
240 int ret;
241
242 udata = devm_kzalloc(dev, sizeof(*udata), GFP_KERNEL);
243 if (!udata)
244 return -ENOMEM;
245
246 udata->dev = dev;
247
248 udata->ec = ec_data->ec_dev;
249 if (!udata->ec)
250 return dev_err_probe(dev, -ENODEV, "couldn't find parent EC device\n");
251
252 platform_set_drvdata(pdev, udata);
253
254 INIT_WORK(&udata->work, cros_ucsi_work);
255 INIT_DELAYED_WORK(&udata->write_tmo, cros_ucsi_write_timeout);
256 init_completion(&udata->complete);
257
258 udata->ucsi = ucsi_create(dev, &cros_ucsi_ops);
259 if (IS_ERR(udata->ucsi))
260 return dev_err_probe(dev, PTR_ERR(udata->ucsi), "failed to allocate UCSI instance\n");
261
262 ucsi_set_drvdata(udata->ucsi, udata);
263
264 udata->nb.notifier_call = cros_ucsi_event;
265 ret = cros_usbpd_register_notify(&udata->nb);
266 if (ret) {
267 dev_err_probe(dev, ret, "failed to register notifier\n");
268 ucsi_destroy(udata->ucsi);
269 return ret;
270 }
271
272 ret = ucsi_register(udata->ucsi);
273 if (ret) {
274 dev_err_probe(dev, ret, "failed to register UCSI\n");
275 cros_ucsi_destroy(udata);
276 return ret;
277 }
278
279 return 0;
280 }
281
cros_ucsi_remove(struct platform_device * dev)282 static void cros_ucsi_remove(struct platform_device *dev)
283 {
284 struct cros_ucsi_data *udata = platform_get_drvdata(dev);
285
286 ucsi_unregister(udata->ucsi);
287 cros_ucsi_destroy(udata);
288 }
289
cros_ucsi_suspend(struct device * dev)290 static int __maybe_unused cros_ucsi_suspend(struct device *dev)
291 {
292 struct cros_ucsi_data *udata = dev_get_drvdata(dev);
293
294 cancel_delayed_work_sync(&udata->write_tmo);
295 cancel_work_sync(&udata->work);
296
297 return 0;
298 }
299
cros_ucsi_complete(struct device * dev)300 static void __maybe_unused cros_ucsi_complete(struct device *dev)
301 {
302 struct cros_ucsi_data *udata = dev_get_drvdata(dev);
303
304 ucsi_resume(udata->ucsi);
305 }
306
307 /*
308 * UCSI protocol is also used on ChromeOS platforms which reply on
309 * cros_ec_lpc.c driver for communication with embedded controller (EC).
310 * On such platforms communication with the EC is not available until
311 * the .complete() callback of the cros_ec_lpc driver is executed.
312 * For this reason we delay ucsi_resume() until the .complete() stage
313 * otherwise UCSI SET_NOTIFICATION_ENABLE command will fail and we won't
314 * receive any UCSI notifications from the EC where PPM is implemented.
315 */
316 static const struct dev_pm_ops cros_ucsi_pm_ops = {
317 #ifdef CONFIG_PM_SLEEP
318 .suspend = cros_ucsi_suspend,
319 .complete = cros_ucsi_complete,
320 #endif
321 };
322
323 static const struct platform_device_id cros_ucsi_id[] = {
324 { KBUILD_MODNAME, 0 },
325 {}
326 };
327 MODULE_DEVICE_TABLE(platform, cros_ucsi_id);
328
329 static struct platform_driver cros_ucsi_driver = {
330 .driver = {
331 .name = KBUILD_MODNAME,
332 .pm = &cros_ucsi_pm_ops,
333 },
334 .id_table = cros_ucsi_id,
335 .probe = cros_ucsi_probe,
336 .remove = cros_ucsi_remove,
337 };
338
339 module_platform_driver(cros_ucsi_driver);
340
341 MODULE_LICENSE("GPL");
342 MODULE_DESCRIPTION("UCSI driver for ChromeOS EC");
343