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 	.read_message_in = cros_ucsi_read_message_in,
141 	.async_control = cros_ucsi_async_control,
142 	.sync_control = cros_ucsi_sync_control,
143 };
144 
cros_ucsi_work(struct work_struct * work)145 static void cros_ucsi_work(struct work_struct *work)
146 {
147 	struct cros_ucsi_data *udata = container_of(work, struct cros_ucsi_data, work);
148 	u32 cci;
149 
150 	if (cros_ucsi_read_cci(udata->ucsi, &cci))
151 		return;
152 
153 	ucsi_notify_common(udata->ucsi, cci);
154 }
155 
cros_ucsi_write_timeout(struct work_struct * work)156 static void cros_ucsi_write_timeout(struct work_struct *work)
157 {
158 	struct cros_ucsi_data *udata =
159 		container_of(work, struct cros_ucsi_data, write_tmo.work);
160 	u32 cci;
161 	u64 cmd;
162 
163 	if (cros_ucsi_read(udata->ucsi, UCSI_CCI, &cci, sizeof(cci))) {
164 		dev_err(udata->dev,
165 			"Reading CCI failed; no write timeout recovery possible.\n");
166 		return;
167 	}
168 
169 	if (cci & UCSI_CCI_BUSY) {
170 		udata->tmo_counter++;
171 
172 		if (udata->tmo_counter <= WRITE_TMO_CTR_MAX)
173 			schedule_delayed_work(&udata->write_tmo,
174 					      msecs_to_jiffies(WRITE_TMO_MS));
175 		else
176 			dev_err(udata->dev,
177 				"PPM unresponsive - too many write timeouts.\n");
178 
179 		return;
180 	}
181 
182 	/* No longer busy means we can reset our timeout counter. */
183 	udata->tmo_counter = 0;
184 
185 	/* Need to ack previous command which may have timed out. */
186 	if (cci & UCSI_CCI_COMMAND_COMPLETE) {
187 		cmd = UCSI_ACK_CC_CI | UCSI_ACK_COMMAND_COMPLETE;
188 		cros_ucsi_async_control(udata->ucsi, cmd);
189 
190 		/* Check again after a few seconds that the system has
191 		 * recovered to make sure our async write above was successful.
192 		 */
193 		schedule_delayed_work(&udata->write_tmo,
194 				      msecs_to_jiffies(WRITE_TMO_MS));
195 		return;
196 	}
197 
198 	/* We recovered from a previous timeout. Treat this as a recovery from
199 	 * suspend and call resume.
200 	 */
201 	ucsi_resume(udata->ucsi);
202 }
203 
cros_ucsi_event(struct notifier_block * nb,unsigned long host_event,void * _notify)204 static int cros_ucsi_event(struct notifier_block *nb,
205 			   unsigned long host_event, void *_notify)
206 {
207 	struct cros_ucsi_data *udata = container_of(nb, struct cros_ucsi_data, nb);
208 
209 	if (host_event & PD_EVENT_INIT) {
210 		/* Late init event received from ChromeOS EC. Treat this as a
211 		 * system resume to re-enable communication with the PPM.
212 		 */
213 		dev_dbg(udata->dev, "Late PD init received\n");
214 		ucsi_resume(udata->ucsi);
215 	}
216 
217 	if (host_event & PD_EVENT_PPM) {
218 		dev_dbg(udata->dev, "UCSI notification received\n");
219 		flush_work(&udata->work);
220 		schedule_work(&udata->work);
221 	}
222 
223 	return NOTIFY_OK;
224 }
225 
cros_ucsi_destroy(struct cros_ucsi_data * udata)226 static void cros_ucsi_destroy(struct cros_ucsi_data *udata)
227 {
228 	cros_usbpd_unregister_notify(&udata->nb);
229 	cancel_delayed_work_sync(&udata->write_tmo);
230 	cancel_work_sync(&udata->work);
231 	ucsi_destroy(udata->ucsi);
232 }
233 
cros_ucsi_probe(struct platform_device * pdev)234 static int cros_ucsi_probe(struct platform_device *pdev)
235 {
236 	struct device *dev = &pdev->dev;
237 	struct cros_ec_dev *ec_data = dev_get_drvdata(dev->parent);
238 	struct cros_ucsi_data *udata;
239 	int ret;
240 
241 	udata = devm_kzalloc(dev, sizeof(*udata), GFP_KERNEL);
242 	if (!udata)
243 		return -ENOMEM;
244 
245 	udata->dev = dev;
246 
247 	udata->ec = ec_data->ec_dev;
248 	if (!udata->ec)
249 		return dev_err_probe(dev, -ENODEV, "couldn't find parent EC device\n");
250 
251 	platform_set_drvdata(pdev, udata);
252 
253 	INIT_WORK(&udata->work, cros_ucsi_work);
254 	INIT_DELAYED_WORK(&udata->write_tmo, cros_ucsi_write_timeout);
255 	init_completion(&udata->complete);
256 
257 	udata->ucsi = ucsi_create(dev, &cros_ucsi_ops);
258 	if (IS_ERR(udata->ucsi))
259 		return dev_err_probe(dev, PTR_ERR(udata->ucsi), "failed to allocate UCSI instance\n");
260 
261 	ucsi_set_drvdata(udata->ucsi, udata);
262 
263 	udata->nb.notifier_call = cros_ucsi_event;
264 	ret = cros_usbpd_register_notify(&udata->nb);
265 	if (ret) {
266 		dev_err_probe(dev, ret, "failed to register notifier\n");
267 		ucsi_destroy(udata->ucsi);
268 		return ret;
269 	}
270 
271 	ret = ucsi_register(udata->ucsi);
272 	if (ret) {
273 		dev_err_probe(dev, ret, "failed to register UCSI\n");
274 		cros_ucsi_destroy(udata);
275 		return ret;
276 	}
277 
278 	return 0;
279 }
280 
cros_ucsi_remove(struct platform_device * dev)281 static void cros_ucsi_remove(struct platform_device *dev)
282 {
283 	struct cros_ucsi_data *udata = platform_get_drvdata(dev);
284 
285 	ucsi_unregister(udata->ucsi);
286 	cros_ucsi_destroy(udata);
287 }
288 
cros_ucsi_suspend(struct device * dev)289 static int __maybe_unused cros_ucsi_suspend(struct device *dev)
290 {
291 	struct cros_ucsi_data *udata = dev_get_drvdata(dev);
292 
293 	cancel_delayed_work_sync(&udata->write_tmo);
294 	cancel_work_sync(&udata->work);
295 
296 	return 0;
297 }
298 
cros_ucsi_complete(struct device * dev)299 static void __maybe_unused cros_ucsi_complete(struct device *dev)
300 {
301 	struct cros_ucsi_data *udata = dev_get_drvdata(dev);
302 
303 	ucsi_resume(udata->ucsi);
304 }
305 
306 /*
307  * UCSI protocol is also used on ChromeOS platforms which reply on
308  * cros_ec_lpc.c driver for communication with embedded controller (EC).
309  * On such platforms communication with the EC is not available until
310  * the .complete() callback of the cros_ec_lpc driver is executed.
311  * For this reason we delay ucsi_resume() until the .complete() stage
312  * otherwise UCSI SET_NOTIFICATION_ENABLE command will fail and we won't
313  * receive any UCSI notifications from the EC where PPM is implemented.
314  */
315 static const struct dev_pm_ops cros_ucsi_pm_ops = {
316 #ifdef CONFIG_PM_SLEEP
317 	.suspend = cros_ucsi_suspend,
318 	.complete = cros_ucsi_complete,
319 #endif
320 };
321 
322 static const struct platform_device_id cros_ucsi_id[] = {
323 	{ KBUILD_MODNAME, 0 },
324 	{}
325 };
326 MODULE_DEVICE_TABLE(platform, cros_ucsi_id);
327 
328 static struct platform_driver cros_ucsi_driver = {
329 	.driver = {
330 		.name = KBUILD_MODNAME,
331 		.pm = &cros_ucsi_pm_ops,
332 	},
333 	.id_table = cros_ucsi_id,
334 	.probe = cros_ucsi_probe,
335 	.remove = cros_ucsi_remove,
336 };
337 
338 module_platform_driver(cros_ucsi_driver);
339 
340 MODULE_LICENSE("GPL");
341 MODULE_DESCRIPTION("UCSI driver for ChromeOS EC");
342