1 /*
2  * Copyright (c) 2022 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 /*
8  * MAX3421E USB Peripheral/Host Controller with SPI Interface.
9  * NOTE: Driver supports only host mode yet.
10  */
11 
12 #define DT_DRV_COMPAT maxim_max3421e_spi
13 
14 #include <string.h>
15 
16 #include <zephyr/kernel.h>
17 #include <zephyr/init.h>
18 #include <zephyr/sys/byteorder.h>
19 #include <zephyr/drivers/gpio.h>
20 #include <zephyr/drivers/spi.h>
21 #include <zephyr/drivers/usb/uhc.h>
22 
23 #include "uhc_common.h"
24 #include "uhc_max3421e.h"
25 
26 #include <zephyr/logging/log.h>
27 LOG_MODULE_REGISTER(max3421e, CONFIG_UHC_DRIVER_LOG_LEVEL);
28 
29 static K_KERNEL_STACK_DEFINE(drv_stack, CONFIG_MAX3421E_THREAD_STACK_SIZE);
30 static struct k_thread drv_stack_data;
31 
32 #define MAX3421E_STATE_BUS_RESET	0
33 #define MAX3421E_STATE_BUS_RESUME	1
34 
35 struct max3421e_data {
36 	struct gpio_callback gpio_cb;
37 	struct uhc_transfer *last_xfer;
38 	struct k_sem irq_sem;
39 	atomic_t state;
40 	uint16_t tog_in;
41 	uint16_t tog_out;
42 	uint8_t addr;
43 	uint8_t hirq;
44 	uint8_t hien;
45 	uint8_t mode;
46 	uint8_t hxfr;
47 	uint8_t hrsl;
48 };
49 
50 struct max3421e_config {
51 	struct spi_dt_spec dt_spi;
52 	struct gpio_dt_spec dt_int;
53 	struct gpio_dt_spec dt_rst;
54 };
55 
max3421e_read_hirq(const struct device * dev,const uint8_t reg,uint8_t * const data,const uint32_t count,bool update_hirq)56 static int max3421e_read_hirq(const struct device *dev,
57 			      const uint8_t reg,
58 			      uint8_t *const data,
59 			      const uint32_t count,
60 			      bool update_hirq)
61 {
62 	struct max3421e_data *priv = uhc_get_private(dev);
63 	const struct max3421e_config *config = dev->config;
64 	uint8_t cmd = MAX3421E_CMD_SPI_READ(reg);
65 	uint8_t hirq;
66 	int ret;
67 
68 	const struct spi_buf cmd_buf = {
69 		.buf = &cmd,
70 		.len = sizeof(cmd)
71 	};
72 	const struct spi_buf rx_buf[] = {
73 		{
74 			.buf = &hirq,
75 			.len = sizeof(hirq)
76 		},
77 		{
78 			.buf = data,
79 			.len = count
80 		}
81 	};
82 
83 	const struct spi_buf_set tx = {
84 		.buffers = &cmd_buf,
85 		.count = 1
86 	};
87 	const struct spi_buf_set rx = {
88 		.buffers = rx_buf,
89 		.count = ARRAY_SIZE(rx_buf)
90 	};
91 
92 	ret = spi_transceive_dt(&config->dt_spi, &tx, &rx);
93 	if (unlikely(update_hirq)) {
94 		priv->hirq = hirq;
95 	}
96 
97 	return ret;
98 }
99 
max3421e_read(const struct device * dev,const uint8_t reg,uint8_t * const data,const uint32_t count)100 static int max3421e_read(const struct device *dev,
101 			 const uint8_t reg,
102 			 uint8_t *const data,
103 			 const uint32_t count)
104 {
105 
106 	return max3421e_read_hirq(dev, reg, data, count, false);
107 }
108 
max3421e_write_byte(const struct device * dev,const uint8_t reg,const uint8_t val)109 static int max3421e_write_byte(const struct device *dev,
110 			       const uint8_t reg,
111 			       const uint8_t val)
112 {
113 	const struct max3421e_config *config = dev->config;
114 	uint8_t buf[2] = {MAX3421E_CMD_SPI_WRITE(reg), val};
115 
116 	const struct spi_buf cmd_buf = {
117 		.buf = &buf,
118 		.len = sizeof(buf)
119 	};
120 	const struct spi_buf_set tx = {
121 		.buffers = &cmd_buf,
122 		.count = 1
123 	};
124 
125 	return spi_write_dt(&config->dt_spi, &tx);
126 }
127 
max3421e_write(const struct device * dev,const uint8_t reg,uint8_t * const data,const size_t count)128 static int max3421e_write(const struct device *dev,
129 			  const uint8_t reg,
130 			  uint8_t *const data,
131 			  const size_t count)
132 {
133 	const struct max3421e_config *config = dev->config;
134 	uint8_t cmd = MAX3421E_CMD_SPI_WRITE(reg);
135 
136 	const struct spi_buf cmd_buf[] = {
137 		{
138 			.buf = &cmd,
139 			.len = sizeof(cmd),
140 		},
141 		{
142 			.buf = data,
143 			.len = count,
144 		},
145 	};
146 	const struct spi_buf_set tx = {
147 		.buffers = cmd_buf,
148 		.count = ARRAY_SIZE(cmd_buf),
149 	};
150 
151 	return spi_write_dt(&config->dt_spi, &tx);
152 }
153 
max3421e_lock(const struct device * dev)154 static int max3421e_lock(const struct device *dev)
155 {
156 	struct uhc_data *data = dev->data;
157 
158 	return k_mutex_lock(&data->mutex, K_FOREVER);
159 }
160 
max3421e_unlock(const struct device * dev)161 static int max3421e_unlock(const struct device *dev)
162 {
163 	struct uhc_data *data = dev->data;
164 
165 	return k_mutex_unlock(&data->mutex);
166 }
167 
168 /* Disable Host Interrupt */
max3421e_hien_disable(const struct device * dev,const uint8_t hint)169 static ALWAYS_INLINE int max3421e_hien_disable(const struct device *dev,
170 					       const uint8_t hint)
171 {
172 	struct max3421e_data *priv = uhc_get_private(dev);
173 
174 	priv->hien &= ~hint;
175 
176 	return max3421e_write_byte(dev, MAX3421E_REG_HIEN, priv->hien);
177 }
178 
179 /* Set peripheral (device) address to be used in next transfer */
max3421e_peraddr(const struct device * dev,const uint8_t addr)180 static ALWAYS_INLINE int max3421e_peraddr(const struct device *dev,
181 					  const uint8_t addr)
182 {
183 	struct max3421e_data *priv = uhc_get_private(dev);
184 	int ret = 0;
185 
186 	if (priv->addr != addr) {
187 		/*
188 		 * TODO: Consider how to force the update of toggle values
189 		 * for the next transfer. Necessary if we want to support
190 		 * multiple peripherals.
191 		 */
192 		ret = max3421e_write_byte(dev, MAX3421E_REG_PERADDR, addr);
193 		if (ret == 0) {
194 			priv->addr = addr;
195 		}
196 	}
197 
198 	return ret;
199 }
200 
201 /* Update driver's knowledge about DATA PID */
max3421e_tgl_update(const struct device * dev)202 static ALWAYS_INLINE void max3421e_tgl_update(const struct device *dev)
203 {
204 	struct max3421e_data *priv = uhc_get_private(dev);
205 	uint8_t ep_idx = MAX3421E_EP(priv->hxfr);
206 
207 	if (priv->hxfr & MAX3421E_OUTNIN) {
208 		if (priv->hrsl & MAX3421E_SNDTOGRD) {
209 			priv->tog_out |= BIT(ep_idx);
210 		} else {
211 			priv->tog_out &= ~BIT(ep_idx);
212 		}
213 	} else {
214 		if (priv->hrsl & MAX3421E_RCVTOGRD) {
215 			priv->tog_in |= BIT(ep_idx);
216 		} else {
217 			priv->tog_in &= ~BIT(ep_idx);
218 		}
219 	}
220 
221 	LOG_DBG("tog_in 0x%02x tog_out 0x%02x last-hxfr 0x%02x hrsl 0x%02x",
222 		priv->tog_in, priv->tog_out, priv->hxfr, priv->hrsl);
223 }
224 
225 /* Get DATA PID to be used for the next transfer */
max3421e_tgl_next(const struct device * dev,const uint8_t hxfr)226 static ALWAYS_INLINE uint8_t max3421e_tgl_next(const struct device *dev,
227 					       const uint8_t hxfr)
228 {
229 	struct max3421e_data *priv = uhc_get_private(dev);
230 	uint8_t ep_idx = MAX3421E_EP(hxfr);
231 	uint8_t hctl;
232 
233 	/* Force DATA1 PID for the data stage of control transfer */
234 	if (hxfr & MAX3421E_SETUP) {
235 		priv->tog_in |= BIT(0);
236 		priv->tog_out |= BIT(0);
237 	}
238 
239 	if (hxfr & MAX3421E_OUTNIN) {
240 		hctl = (priv->tog_out & BIT(ep_idx)) ? MAX3421E_SNDTOG1 :
241 						       MAX3421E_SNDTOG0;
242 	} else {
243 		hctl = (priv->tog_in & BIT(ep_idx)) ? MAX3421E_RCVTOG1 :
244 						      MAX3421E_RCVTOG0;
245 	}
246 
247 	return hctl;
248 }
249 
max3421e_hxfr_start(const struct device * dev,const uint8_t hxfr)250 static ALWAYS_INLINE int max3421e_hxfr_start(const struct device *dev,
251 					     const uint8_t hxfr)
252 {
253 	struct max3421e_data *priv = uhc_get_private(dev);
254 
255 	if (priv->hxfr != hxfr) {
256 		uint8_t reg[2] = {0, hxfr};
257 
258 		/* Update DATA PID if transfer parameter changes */
259 		max3421e_tgl_update(dev);
260 		reg[0] = max3421e_tgl_next(dev, hxfr);
261 		priv->hxfr = hxfr;
262 		LOG_DBG("hctl 0x%02x hxfr 0x%02x", reg[0], reg[1]);
263 
264 		return max3421e_write(dev, MAX3421E_REG_HCTL,
265 				      reg, sizeof(reg));
266 	}
267 
268 	return max3421e_write_byte(dev, MAX3421E_REG_HXFR, priv->hxfr);
269 }
270 
max3421e_xfer_data(const struct device * dev,struct net_buf * const buf,const uint8_t ep)271 static int max3421e_xfer_data(const struct device *dev,
272 			      struct net_buf *const buf,
273 			      const uint8_t ep)
274 {
275 	const uint8_t ep_idx = USB_EP_GET_IDX(ep);
276 	int ret;
277 
278 	if (USB_EP_DIR_IS_IN(ep)) {
279 		LOG_DBG("bulk in %p %u", buf, net_buf_tailroom(buf));
280 		ret = max3421e_hxfr_start(dev, MAX3421E_HXFR_BULKIN(ep_idx));
281 	} else {
282 		size_t len;
283 
284 		len = MIN(MAX3421E_MAX_EP_SIZE, buf->len);
285 		LOG_DBG("bulk out %p %u", buf, len);
286 
287 		ret = max3421e_write(dev, MAX3421E_REG_SNDFIFO, buf->data, len);
288 		if (ret) {
289 			return ret;
290 		}
291 
292 		ret = max3421e_write_byte(dev, MAX3421E_REG_SNDBC, len);
293 		if (ret) {
294 			return ret;
295 		}
296 
297 		/*
298 		 * FIXME: Pull should happen after device ACKs the data,
299 		 *        move to max3421e_hrslt_success().
300 		 */
301 		net_buf_pull(buf, len);
302 		ret = max3421e_hxfr_start(dev, MAX3421E_HXFR_BULKOUT(ep_idx));
303 	}
304 
305 	return ret;
306 }
307 
max3421e_xfer_control(const struct device * dev,struct uhc_transfer * const xfer,const uint8_t hrsl)308 static int max3421e_xfer_control(const struct device *dev,
309 				 struct uhc_transfer *const xfer,
310 				 const uint8_t hrsl)
311 {
312 	struct max3421e_data *priv = uhc_get_private(dev);
313 	struct net_buf *buf = xfer->buf;
314 	int ret;
315 
316 	/* Just restart if device NAKed packet */
317 	if (HRSLT_IS_NAK(hrsl)) {
318 		return max3421e_hxfr_start(dev, priv->hxfr);
319 	}
320 
321 
322 	if (xfer->stage == UHC_CONTROL_STAGE_SETUP) {
323 		LOG_DBG("Handle SETUP stage");
324 		ret = max3421e_write(dev, MAX3421E_REG_SUDFIFO,
325 				     xfer->setup_pkt, sizeof(xfer->setup_pkt));
326 		if (ret) {
327 			return ret;
328 		}
329 
330 		ret = max3421e_hxfr_start(dev, MAX3421E_HXFR_SETUP(0));
331 		if (ret) {
332 			return ret;
333 		}
334 
335 		return 0;
336 	}
337 
338 	if (buf != NULL && xfer->stage == UHC_CONTROL_STAGE_DATA) {
339 		LOG_DBG("Handle DATA stage");
340 		return max3421e_xfer_data(dev, buf, xfer->ep);
341 	}
342 
343 	if (xfer->stage == UHC_CONTROL_STAGE_STATUS) {
344 		LOG_DBG("Handle STATUS stage");
345 		if (USB_EP_DIR_IS_IN(xfer->ep)) {
346 			ret = max3421e_hxfr_start(dev, MAX3421E_HXFR_HSOUT(0));
347 		} else {
348 			ret = max3421e_hxfr_start(dev, MAX3421E_HXFR_HSIN(0));
349 		}
350 
351 		return ret;
352 	}
353 
354 	return -EINVAL;
355 }
356 
max3421e_xfer_bulk(const struct device * dev,struct uhc_transfer * const xfer,const uint8_t hrsl)357 static int max3421e_xfer_bulk(const struct device *dev,
358 			      struct uhc_transfer *const xfer,
359 			      const uint8_t hrsl)
360 {
361 	struct max3421e_data *priv = uhc_get_private(dev);
362 	struct net_buf *buf = xfer->buf;
363 
364 	/* Just restart if device NAKed packet */
365 	if (HRSLT_IS_NAK(hrsl)) {
366 		return max3421e_hxfr_start(dev, priv->hxfr);
367 	}
368 
369 	if (buf == NULL) {
370 		LOG_ERR("No buffer to handle");
371 		return -ENODATA;
372 	}
373 
374 	return max3421e_xfer_data(dev, buf, xfer->ep);
375 }
376 
max3421e_schedule_xfer(const struct device * dev)377 static int max3421e_schedule_xfer(const struct device *dev)
378 {
379 	struct max3421e_data *priv = uhc_get_private(dev);
380 	uint8_t hrsl = priv->hrsl;
381 
382 	if (priv->last_xfer == NULL) {
383 		int ret;
384 
385 		/* Do not restart last transfer */
386 		hrsl = 0;
387 
388 		priv->last_xfer = uhc_xfer_get_next(dev);
389 		if (priv->last_xfer == NULL) {
390 			LOG_DBG("Nothing to transfer");
391 			return 0;
392 		}
393 
394 		LOG_DBG("Next transfer %p", priv->last_xfer);
395 		ret = max3421e_peraddr(dev, priv->last_xfer->udev->addr);
396 		if (ret) {
397 			return ret;
398 		}
399 	}
400 
401 	/*
402 	 * TODO: currently we only support control transfers and
403 	 * treat all others as bulk.
404 	 */
405 	if (USB_EP_GET_IDX(priv->last_xfer->ep) == 0) {
406 		return max3421e_xfer_control(dev, priv->last_xfer, hrsl);
407 	}
408 
409 	return max3421e_xfer_bulk(dev, priv->last_xfer, hrsl);
410 }
411 
max3421e_xfer_drop_active(const struct device * dev,int err)412 static void max3421e_xfer_drop_active(const struct device *dev, int err)
413 {
414 	struct max3421e_data *priv = uhc_get_private(dev);
415 
416 	if (priv->last_xfer) {
417 		uhc_xfer_return(dev, priv->last_xfer, err);
418 		priv->last_xfer = NULL;
419 	}
420 }
421 
max3421e_xfer_cleanup_cancelled(const struct device * dev)422 static void max3421e_xfer_cleanup_cancelled(const struct device *dev)
423 {
424 	struct max3421e_data *priv = uhc_get_private(dev);
425 	struct uhc_data *data = dev->data;
426 	struct uhc_transfer *tmp;
427 
428 	if (priv->last_xfer != NULL && priv->last_xfer->err == -ECONNRESET) {
429 		max3421e_xfer_drop_active(dev, -ECONNRESET);
430 	}
431 
432 	SYS_DLIST_FOR_EACH_CONTAINER(&data->ctrl_xfers, tmp, node) {
433 		if (tmp->err == -ECONNRESET) {
434 			uhc_xfer_return(dev, tmp, -ECONNRESET);
435 		}
436 	}
437 }
438 
max3421e_hrslt_success(const struct device * dev)439 static int max3421e_hrslt_success(const struct device *dev)
440 {
441 	struct max3421e_data *priv = uhc_get_private(dev);
442 	struct uhc_transfer *const xfer = priv->last_xfer;
443 	struct net_buf *buf = xfer->buf;
444 	bool finished = false;
445 	int err = 0;
446 	size_t len;
447 	uint8_t bc;
448 
449 	switch (MAX3421E_HXFR_TYPE(priv->hxfr)) {
450 	case MAX3421E_HXFR_TYPE_SETUP:
451 		if (xfer->buf != NULL) {
452 			xfer->stage = UHC_CONTROL_STAGE_DATA;
453 		} else {
454 			xfer->stage = UHC_CONTROL_STAGE_STATUS;
455 		}
456 		break;
457 	case MAX3421E_HXFR_TYPE_HSOUT:
458 		LOG_DBG("HSOUT");
459 		finished = true;
460 		break;
461 	case MAX3421E_HXFR_TYPE_HSIN:
462 		LOG_DBG("HSIN");
463 		finished = true;
464 		break;
465 	case MAX3421E_HXFR_TYPE_ISOOUT:
466 		LOG_ERR("ISO OUT is not implemented");
467 		k_panic();
468 		break;
469 	case MAX3421E_HXFR_TYPE_ISOIN:
470 		LOG_ERR("ISO IN is not implemented");
471 		k_panic();
472 		break;
473 	case MAX3421E_HXFR_TYPE_BULKOUT:
474 		if (buf->len == 0) {
475 			LOG_INF("hrslt bulk out %u", buf->len);
476 			if (xfer->ep == USB_CONTROL_EP_OUT) {
477 				xfer->stage = UHC_CONTROL_STAGE_STATUS;
478 			} else {
479 				finished = true;
480 			}
481 		}
482 		break;
483 	case MAX3421E_HXFR_TYPE_BULKIN:
484 		err = max3421e_read(dev, MAX3421E_REG_RCVBC, &bc, sizeof(bc));
485 		if (err) {
486 			break;
487 		}
488 
489 		if (bc > net_buf_tailroom(buf)) {
490 			LOG_WRN("%u received bytes will be dropped",
491 				bc - net_buf_tailroom(buf));
492 		}
493 
494 		len = MIN(net_buf_tailroom(buf), bc);
495 		err = max3421e_read(dev, MAX3421E_REG_RCVFIFO,
496 				    net_buf_add(buf, len), len);
497 		if (err) {
498 			break;
499 		}
500 
501 		LOG_INF("bc %u tr %u", bc, net_buf_tailroom(buf));
502 
503 		if (bc < MAX3421E_MAX_EP_SIZE || !net_buf_tailroom(buf)) {
504 			LOG_INF("hrslt bulk in %u, %u", bc, len);
505 			if (xfer->ep == USB_CONTROL_EP_IN) {
506 				xfer->stage = UHC_CONTROL_STAGE_STATUS;
507 			} else {
508 				finished = true;
509 			}
510 		}
511 		break;
512 	}
513 
514 	if (finished) {
515 		LOG_DBG("Transfer finished");
516 		uhc_xfer_return(dev, xfer, 0);
517 		priv->last_xfer = NULL;
518 	}
519 
520 	if (err) {
521 		max3421e_xfer_drop_active(dev, err);
522 	}
523 
524 	return err;
525 }
526 
max3421e_handle_hxfrdn(const struct device * dev)527 static int max3421e_handle_hxfrdn(const struct device *dev)
528 {
529 	struct max3421e_data *priv = uhc_get_private(dev);
530 	struct uhc_transfer *const xfer = priv->last_xfer;
531 	const uint8_t hrsl = priv->hrsl;
532 	int ret = 0;
533 
534 	if (xfer == NULL) {
535 		LOG_ERR("No transfers to handle");
536 		return -ENODATA;
537 	}
538 
539 	switch (MAX3421E_HRSLT(hrsl)) {
540 	case MAX3421E_HR_NAK:
541 		break;
542 	case MAX3421E_HR_STALL:
543 		max3421e_xfer_drop_active(dev, -EPIPE);
544 		break;
545 	case MAX3421E_HR_TOGERR:
546 		LOG_WRN("Toggle error");
547 		break;
548 	case MAX3421E_HR_SUCCESS:
549 		ret = max3421e_hrslt_success(dev);
550 		break;
551 	default:
552 		/* TODO: Handle all reasonalbe result codes */
553 		max3421e_xfer_drop_active(dev, -EINVAL);
554 		ret = -EINVAL;
555 		break;
556 	}
557 
558 	return ret;
559 }
560 
max3421e_handle_condet(const struct device * dev)561 static void max3421e_handle_condet(const struct device *dev)
562 {
563 	struct max3421e_data *priv = uhc_get_private(dev);
564 	const uint8_t jk = priv->hrsl & MAX3421E_JKSTATUS_MASK;
565 	enum uhc_event_type type = UHC_EVT_ERROR;
566 
567 	/*
568 	 * JSTATUS:KSTATUS 0:0 - SE0
569 	 * JSTATUS:KSTATUS 0:1 - K   (Resume)
570 	 * JSTATUS:KSTATUS 1:0 - J   (Idle)
571 	 */
572 	if (jk == 0) {
573 		/* Device disconnected */
574 		type = UHC_EVT_DEV_REMOVED;
575 	}
576 
577 	if (jk == MAX3421E_JSTATUS) {
578 		/* Device connected */
579 		type = UHC_EVT_DEV_CONNECTED_FS;
580 	}
581 
582 	if (jk == MAX3421E_KSTATUS) {
583 		/* Device connected */
584 		type = UHC_EVT_DEV_CONNECTED_LS;
585 	}
586 
587 	uhc_submit_event(dev, type, 0);
588 }
589 
max3421e_bus_event(const struct device * dev)590 static void max3421e_bus_event(const struct device *dev)
591 {
592 	struct max3421e_data *priv = uhc_get_private(dev);
593 
594 	if (atomic_test_and_clear_bit(&priv->state,
595 				      MAX3421E_STATE_BUS_RESUME)) {
596 		/* Resume operation done event */
597 		uhc_submit_event(dev, UHC_EVT_RESUMED, 0);
598 	}
599 
600 	if (atomic_test_and_clear_bit(&priv->state,
601 				      MAX3421E_STATE_BUS_RESET)) {
602 		/* Reset operation done event */
603 		uhc_submit_event(dev, UHC_EVT_RESETED, 0);
604 	}
605 }
606 
max3421e_update_hrsl_hirq(const struct device * dev)607 static int max3421e_update_hrsl_hirq(const struct device *dev)
608 {
609 	struct max3421e_data *priv = uhc_get_private(dev);
610 	int err;
611 
612 	err = max3421e_read_hirq(dev, MAX3421E_REG_HRSL, &priv->hrsl, 1, true);
613 	/* Consider only enabled interrupts and RCVDAV bit (see RCVBC description) */
614 	priv->hirq &= priv->hien | MAX3421E_RCVDAV;
615 	LOG_DBG("HIRQ 0x%02x HRSLT %d", priv->hirq, MAX3421E_HRSLT(priv->hrsl));
616 
617 	return err;
618 }
619 
max3421e_clear_hirq(const struct device * dev,const uint8_t hirq)620 static int max3421e_clear_hirq(const struct device *dev, const uint8_t hirq)
621 {
622 	return max3421e_write_byte(dev, MAX3421E_REG_HIRQ, hirq);
623 }
624 
max3421e_handle_bus_irq(const struct device * dev)625 static int max3421e_handle_bus_irq(const struct device *dev)
626 {
627 	struct max3421e_data *priv = uhc_get_private(dev);
628 	const uint8_t hirq = priv->hirq;
629 	int ret = 0;
630 
631 	/* Suspend operation Done Interrupt (bus suspended) */
632 	if (hirq & MAX3421E_SUSDN) {
633 		ret = max3421e_hien_disable(dev, MAX3421E_SUSDN);
634 		uhc_submit_event(dev, UHC_EVT_SUSPENDED, 0);
635 	}
636 
637 	/* Peripheral Connect/Disconnect Interrupt */
638 	if (hirq & MAX3421E_CONDET) {
639 		max3421e_handle_condet(dev);
640 	}
641 
642 	/* Remote Wakeup Interrupt */
643 	if (hirq & MAX3421E_RWU) {
644 		uhc_submit_event(dev, UHC_EVT_RWUP, 0);
645 	}
646 
647 	/* Bus Reset or Bus Resume event */
648 	if (hirq & MAX3421E_BUSEVENT) {
649 		max3421e_bus_event(dev);
650 	}
651 
652 	return ret;
653 }
654 
uhc_max3421e_thread(void * p1,void * p2,void * p3)655 static void uhc_max3421e_thread(void *p1, void *p2, void *p3)
656 {
657 	ARG_UNUSED(p2);
658 	ARG_UNUSED(p3);
659 
660 	const struct device *dev = p1;
661 	struct max3421e_data *priv = uhc_get_private(dev);
662 
663 	LOG_DBG("MAX3421E thread started");
664 
665 	while (true) {
666 		bool schedule = false;
667 		int err;
668 
669 		k_sem_take(&priv->irq_sem, K_FOREVER);
670 
671 		max3421e_lock(dev);
672 
673 		/*
674 		 * Get HRSL and HIRQ values, do not perform any operation
675 		 * that changes the state of the bus yet.
676 		 */
677 		err = max3421e_update_hrsl_hirq(dev);
678 		if (unlikely(err)) {
679 			uhc_submit_event(dev, UHC_EVT_ERROR, err);
680 		}
681 
682 		/* Host Transfer Done Interrupt */
683 		if (priv->hirq & MAX3421E_HXFRDN) {
684 			err = max3421e_handle_hxfrdn(dev);
685 			if (unlikely(err)) {
686 				uhc_submit_event(dev, UHC_EVT_ERROR, err);
687 			}
688 		}
689 
690 		/* Frame Generator Interrupt */
691 		if (priv->hirq & MAX3421E_FRAME) {
692 			schedule = HRSLT_IS_BUSY(priv->hrsl) ? false : true;
693 		}
694 
695 		/* Shorten the if path a little */
696 		if (priv->hirq & ~(MAX3421E_FRAME | MAX3421E_HXFRDN)) {
697 			err = max3421e_handle_bus_irq(dev);
698 			if (unlikely(err)) {
699 				uhc_submit_event(dev, UHC_EVT_ERROR, err);
700 			}
701 		}
702 
703 		/* Clear interrupts and schedule new bus transfer */
704 		err = max3421e_clear_hirq(dev, priv->hirq);
705 		if (unlikely(err)) {
706 			uhc_submit_event(dev, UHC_EVT_ERROR, err);
707 		}
708 
709 		max3421e_xfer_cleanup_cancelled(dev);
710 
711 		if (schedule) {
712 			err = max3421e_schedule_xfer(dev);
713 			if (unlikely(err)) {
714 				uhc_submit_event(dev, UHC_EVT_ERROR, err);
715 			}
716 		}
717 
718 		max3421e_unlock(dev);
719 	}
720 }
721 
max3421e_gpio_cb(const struct device * dev,struct gpio_callback * cb,uint32_t pins)722 static void max3421e_gpio_cb(const struct device *dev,
723 			     struct gpio_callback *cb,
724 			     uint32_t pins)
725 {
726 	struct max3421e_data *priv =
727 		CONTAINER_OF(cb, struct max3421e_data, gpio_cb);
728 
729 	k_sem_give(&priv->irq_sem);
730 }
731 
732 /* Enable SOF generator */
max3421e_sof_enable(const struct device * dev)733 static int max3421e_sof_enable(const struct device *dev)
734 {
735 	struct max3421e_data *priv = uhc_get_private(dev);
736 
737 	if (priv->mode & MAX3421E_SOFKAENAB) {
738 		return -EALREADY;
739 	}
740 
741 	priv->mode |= MAX3421E_SOFKAENAB;
742 
743 	return max3421e_write_byte(dev, MAX3421E_REG_MODE, priv->mode);
744 }
745 
746 /* Disable SOF generator and suspend bus */
max3421e_bus_suspend(const struct device * dev)747 static int max3421e_bus_suspend(const struct device *dev)
748 {
749 	struct max3421e_data *priv = uhc_get_private(dev);
750 
751 	if (!(priv->mode & MAX3421E_SOFKAENAB)) {
752 		return -EALREADY;
753 	}
754 
755 	priv->hien |= MAX3421E_SUSDN;
756 	priv->mode &= ~MAX3421E_SOFKAENAB;
757 
758 	uint8_t tmp[3] = {MAX3421E_SUSDN, priv->hien, priv->mode};
759 
760 	return max3421e_write(dev, MAX3421E_REG_HIRQ, tmp, sizeof(tmp));
761 }
762 
763 /* Signal bus reset, 50ms SE0 signal */
max3421e_bus_reset(const struct device * dev)764 static int max3421e_bus_reset(const struct device *dev)
765 {
766 	struct max3421e_data *priv = uhc_get_private(dev);
767 	int ret;
768 
769 	if (atomic_test_bit(&priv->state, MAX3421E_STATE_BUS_RESUME)) {
770 		return -EBUSY;
771 	}
772 
773 	ret = max3421e_write_byte(dev, MAX3421E_REG_HCTL, MAX3421E_BUSRST);
774 	atomic_set_bit(&priv->state, MAX3421E_STATE_BUS_RESET);
775 
776 	return ret;
777 }
778 
779 /* Signal bus resume event, 20ms K-state + low-speed EOP */
max3421e_bus_resume(const struct device * dev)780 static int max3421e_bus_resume(const struct device *dev)
781 {
782 	struct max3421e_data *priv = uhc_get_private(dev);
783 	int ret;
784 
785 	if (atomic_test_bit(&priv->state, MAX3421E_STATE_BUS_RESET)) {
786 		return -EBUSY;
787 	}
788 
789 	ret = max3421e_write_byte(dev, MAX3421E_REG_HCTL, MAX3421E_SIGRSM);
790 	atomic_set_bit(&priv->state, MAX3421E_STATE_BUS_RESUME);
791 
792 	return ret;
793 }
794 
max3421e_enqueue(const struct device * dev,struct uhc_transfer * const xfer)795 static int max3421e_enqueue(const struct device *dev,
796 			    struct uhc_transfer *const xfer)
797 {
798 	return uhc_xfer_append(dev, xfer);
799 }
800 
max3421e_dequeue(const struct device * dev,struct uhc_transfer * const xfer)801 static int max3421e_dequeue(const struct device *dev,
802 			    struct uhc_transfer *const xfer)
803 {
804 	struct uhc_data *data = dev->data;
805 	struct uhc_transfer *tmp;
806 	unsigned int key;
807 
808 	key = irq_lock();
809 	SYS_DLIST_FOR_EACH_CONTAINER(&data->ctrl_xfers, tmp, node) {
810 		if (xfer == tmp) {
811 			tmp->err = -ECONNRESET;
812 		}
813 	}
814 
815 	irq_unlock(key);
816 
817 	return 0;
818 }
819 
max3421e_reset(const struct device * dev)820 static int max3421e_reset(const struct device *dev)
821 {
822 	const struct max3421e_config *config = dev->config;
823 	int ret;
824 
825 	if (config->dt_rst.port) {
826 		gpio_pin_set_dt(&config->dt_rst, 1);
827 		gpio_pin_set_dt(&config->dt_rst, 0);
828 	} else {
829 		LOG_DBG("Reset MAX3421E using CHIPRES");
830 		ret = max3421e_write_byte(dev, MAX3421E_REG_USBCTL, MAX3421E_CHIPRES);
831 		ret |= max3421e_write_byte(dev, MAX3421E_REG_USBCTL, 0);
832 
833 		if (ret) {
834 			return ret;
835 		}
836 	}
837 
838 	for (int i = 0; i < CONFIG_MAX3421E_OSC_WAIT_RETRIES; i++) {
839 		uint8_t usbirq;
840 
841 		ret = max3421e_read(dev, MAX3421E_REG_USBIRQ,
842 				    &usbirq, sizeof(usbirq));
843 
844 		LOG_DBG("USBIRQ 0x%02x", usbirq);
845 		if (usbirq & MAX3421E_OSCOKIRQ) {
846 			return 0;
847 		}
848 
849 		k_msleep(3);
850 	}
851 
852 	return -EIO;
853 }
854 
max3421e_pinctl_setup(const struct device * dev)855 static int max3421e_pinctl_setup(const struct device *dev)
856 {
857 	/* Full-Duplex SPI, INT pin edge active, GPX pin signals SOF */
858 	const uint8_t pinctl = MAX3421E_FDUPSPI | MAX3421E_GPXB | MAX3421E_GPXA;
859 	uint8_t tmp;
860 	int ret;
861 
862 	ret = max3421e_write_byte(dev, MAX3421E_REG_PINCTL, pinctl);
863 	if (unlikely(ret)) {
864 		return ret;
865 	}
866 
867 	ret = max3421e_read(dev, MAX3421E_REG_PINCTL, &tmp, sizeof(tmp));
868 	if (unlikely(ret)) {
869 		return ret;
870 	}
871 
872 	if (unlikely(tmp != pinctl)) {
873 		LOG_ERR("Failed to verify PINCTL register 0x%02x vs 0x%02x",
874 			pinctl, tmp);
875 		return -EIO;
876 	}
877 
878 	return 0;
879 }
880 
881 /*
882  * Cache MODE and HIEN register values to avoid having to read it
883  * before modifying register bits.
884  */
max3421e_mode_setup(const struct device * dev)885 static int max3421e_mode_setup(const struct device *dev)
886 {
887 	/*
888 	 * MODE register defaults:
889 	 *   host mode, connect internal D+ and D- pulldown resistors to ground
890 	 */
891 	const uint8_t mode = MAX3421E_DPPULLDN | MAX3421E_DMPULLDN |
892 			     MAX3421E_DELAYISO | MAX3421E_HOST;
893 	struct max3421e_data *priv = uhc_get_private(dev);
894 	uint8_t tmp;
895 	int ret;
896 
897 	ret = max3421e_write_byte(dev, MAX3421E_REG_MODE, mode);
898 	if (ret) {
899 		return ret;
900 	}
901 
902 	ret = max3421e_read(dev, MAX3421E_REG_MODE, &tmp, sizeof(tmp));
903 	if (ret) {
904 		return ret;
905 	}
906 
907 	if (tmp != mode) {
908 		LOG_ERR("Failed to verify MODE register 0x%02x vs 0x%02x",
909 			mode, tmp);
910 		return -EIO;
911 	}
912 
913 	priv->mode = mode;
914 
915 	return 0;
916 }
917 
max3421e_hien_setup(const struct device * dev)918 static int max3421e_hien_setup(const struct device *dev)
919 {
920 	/* Host interrupts enabled by default */
921 	const uint8_t hien = MAX3421E_HXFRDN | MAX3421E_FRAME |
922 			     MAX3421E_CONDET | MAX3421E_RWU |
923 			     MAX3421E_BUSEVENT;
924 	struct max3421e_data *priv = uhc_get_private(dev);
925 	uint8_t tmp;
926 	int ret;
927 
928 	ret = max3421e_write_byte(dev, MAX3421E_REG_HIEN, hien);
929 	if (ret) {
930 		return ret;
931 	}
932 
933 	ret = max3421e_read(dev, MAX3421E_REG_HIEN, &tmp, sizeof(tmp));
934 	if (ret) {
935 		return ret;
936 	}
937 
938 	if (tmp != hien) {
939 		LOG_ERR("Failed to verify HIEN register 0x%02x vs 0x%02x",
940 			hien, tmp);
941 		return -EIO;
942 	}
943 
944 	priv->hien = hien;
945 
946 	return 0;
947 }
948 
max3421e_enable_int_output(const struct device * dev)949 static int max3421e_enable_int_output(const struct device *dev)
950 {
951 	const uint8_t cpuctl = MAX3421E_IE;
952 	uint8_t tmp;
953 	int ret;
954 
955 	/* Enable MAX3421E INT output pin */
956 	ret = max3421e_write_byte(dev, MAX3421E_REG_CPUCTL, cpuctl);
957 	if (ret) {
958 		return ret;
959 	}
960 
961 	ret = max3421e_read(dev, MAX3421E_REG_CPUCTL, &tmp, sizeof(tmp));
962 	if (ret) {
963 		return ret;
964 	}
965 
966 	if (tmp != cpuctl) {
967 		LOG_ERR("Failed to verify CPUCTL register 0x%02x vs 0x%02x",
968 			cpuctl, tmp);
969 		return -EIO;
970 	}
971 
972 	return 0;
973 }
974 
uhc_max3421e_init(const struct device * dev)975 static int uhc_max3421e_init(const struct device *dev)
976 {
977 	struct max3421e_data *priv = uhc_get_private(dev);
978 	uint8_t rev;
979 	int ret;
980 
981 	ret = max3421e_pinctl_setup(dev);
982 	if (ret) {
983 		LOG_ERR("Failed to setup pinctl");
984 		return ret;
985 	}
986 
987 	ret = max3421e_read(dev, MAX3421E_REG_REVISION, &rev, sizeof(rev));
988 	if (ret) {
989 		LOG_ERR("Failed to read revision");
990 		return ret;
991 	}
992 
993 	ret = max3421e_reset(dev);
994 	if (ret) {
995 		LOG_ERR("Failed to reset MAX3421E");
996 		return ret;
997 	}
998 
999 	ret = max3421e_mode_setup(dev);
1000 	if (ret) {
1001 		LOG_ERR("Failed to setup controller mode");
1002 		return ret;
1003 	}
1004 
1005 	ret = max3421e_hien_setup(dev);
1006 	if (ret) {
1007 		LOG_ERR("Failed to setup interrupts");
1008 		return ret;
1009 	}
1010 
1011 	ret = max3421e_enable_int_output(dev);
1012 	if (ret) {
1013 		LOG_ERR("Failed to enable INT output");
1014 		return ret;
1015 	}
1016 
1017 	LOG_INF("REV 0x%x, MODE 0x%02x, HIEN 0x%02x",
1018 		rev, priv->mode, priv->hien);
1019 
1020 	priv->addr = 0;
1021 
1022 	/* Sample bus if device is already connected */
1023 	return max3421e_write_byte(dev, MAX3421E_REG_HCTL, MAX3421E_SAMPLEBUS);
1024 }
1025 
uhc_max3421e_enable(const struct device * dev)1026 static int uhc_max3421e_enable(const struct device *dev)
1027 {
1028 	/* TODO */
1029 	return 0;
1030 }
1031 
uhc_max3421e_disable(const struct device * dev)1032 static int uhc_max3421e_disable(const struct device *dev)
1033 {
1034 	/* TODO */
1035 	return 0;
1036 }
1037 
uhc_max3421e_shutdown(const struct device * dev)1038 static int uhc_max3421e_shutdown(const struct device *dev)
1039 {
1040 	/* TODO */
1041 	return 0;
1042 }
1043 
max3421e_driver_init(const struct device * dev)1044 static int max3421e_driver_init(const struct device *dev)
1045 {
1046 	const struct max3421e_config *config = dev->config;
1047 	struct uhc_data *data = dev->data;
1048 	struct max3421e_data *priv = data->priv;
1049 	int ret;
1050 
1051 	if (config->dt_rst.port) {
1052 		if (!gpio_is_ready_dt(&config->dt_rst)) {
1053 			LOG_ERR("GPIO device %s not ready",
1054 				config->dt_rst.port->name);
1055 			return -EIO;
1056 		}
1057 
1058 		ret = gpio_pin_configure_dt(&config->dt_rst,
1059 					    GPIO_OUTPUT_INACTIVE);
1060 		if (ret) {
1061 			LOG_ERR("Failed to configure GPIO pin %u",
1062 				config->dt_rst.pin);
1063 			return ret;
1064 		}
1065 	}
1066 
1067 	if (!spi_is_ready_dt(&config->dt_spi)) {
1068 		LOG_ERR("SPI device %s not ready", config->dt_spi.bus->name);
1069 		return -EIO;
1070 	}
1071 
1072 	if (!gpio_is_ready_dt(&config->dt_int)) {
1073 		LOG_ERR("GPIO device %s not ready", config->dt_int.port->name);
1074 		return -EIO;
1075 	}
1076 
1077 	ret = gpio_pin_configure_dt(&config->dt_int, GPIO_INPUT);
1078 	if (ret) {
1079 		LOG_ERR("Failed to configure GPIO pin %u", config->dt_int.pin);
1080 		return ret;
1081 	}
1082 
1083 	gpio_init_callback(&priv->gpio_cb, max3421e_gpio_cb,
1084 			   BIT(config->dt_int.pin));
1085 	ret = gpio_add_callback(config->dt_int.port, &priv->gpio_cb);
1086 	if (ret) {
1087 		return ret;
1088 	}
1089 
1090 	ret = gpio_pin_interrupt_configure_dt(&config->dt_int,
1091 					      GPIO_INT_EDGE_TO_ACTIVE);
1092 	if (ret) {
1093 		return ret;
1094 	}
1095 
1096 	k_mutex_init(&data->mutex);
1097 	k_thread_create(&drv_stack_data, drv_stack,
1098 			K_KERNEL_STACK_SIZEOF(drv_stack),
1099 			uhc_max3421e_thread,
1100 			(void *)dev, NULL, NULL,
1101 			K_PRIO_COOP(2), 0, K_NO_WAIT);
1102 	k_thread_name_set(&drv_stack_data, "uhc_max3421e");
1103 
1104 	LOG_DBG("MAX3421E CPU interface initialized");
1105 
1106 	return 0;
1107 }
1108 
1109 static const struct uhc_api max3421e_uhc_api = {
1110 	.lock = max3421e_lock,
1111 	.unlock = max3421e_unlock,
1112 	.init = uhc_max3421e_init,
1113 	.enable = uhc_max3421e_enable,
1114 	.disable = uhc_max3421e_disable,
1115 	.shutdown = uhc_max3421e_shutdown,
1116 
1117 	.bus_reset = max3421e_bus_reset,
1118 	.sof_enable  = max3421e_sof_enable,
1119 	.bus_suspend = max3421e_bus_suspend,
1120 	.bus_resume = max3421e_bus_resume,
1121 
1122 	.ep_enqueue = max3421e_enqueue,
1123 	.ep_dequeue = max3421e_dequeue,
1124 };
1125 
1126 static struct max3421e_data max3421e_data = {
1127 	.irq_sem = Z_SEM_INITIALIZER(max3421e_data.irq_sem, 0, 1),
1128 };
1129 
1130 static struct uhc_data max3421e_uhc_data = {
1131 	.priv = &max3421e_data,
1132 };
1133 
1134 static const struct max3421e_config max3421e_cfg = {
1135 	.dt_spi = SPI_DT_SPEC_INST_GET(0, SPI_WORD_SET(8) | SPI_TRANSFER_MSB, 0),
1136 	.dt_int = GPIO_DT_SPEC_INST_GET(0, int_gpios),
1137 	.dt_rst = GPIO_DT_SPEC_INST_GET_OR(0, reset_gpios, {0}),
1138 };
1139 
1140 DEVICE_DT_INST_DEFINE(0, max3421e_driver_init, NULL,
1141 		      &max3421e_uhc_data, &max3421e_cfg,
1142 		      POST_KERNEL, 99,
1143 		      &max3421e_uhc_api);
1144