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 	const uint8_t hirq = priv->hirq;
381 	const uint8_t hrsl = priv->hrsl;
382 
383 	if (priv->last_xfer == NULL) {
384 		int ret;
385 
386 		priv->last_xfer = uhc_xfer_get_next(dev);
387 		if (priv->last_xfer == NULL) {
388 			LOG_DBG("Nothing to transfer");
389 			return 0;
390 		}
391 
392 		LOG_DBG("Next transfer %p", priv->last_xfer);
393 		ret = max3421e_peraddr(dev, priv->last_xfer->addr);
394 		if (ret) {
395 			return ret;
396 		}
397 	}
398 
399 	if (hirq & MAX3421E_FRAME) {
400 		if (priv->last_xfer->timeout) {
401 			priv->last_xfer->timeout--;
402 		} else {
403 			LOG_INF("Transfer timeout");
404 		}
405 	}
406 
407 	/*
408 	 * TODO: currently we only support control transfers and
409 	 * treat all others as bulk.
410 	 */
411 	if (USB_EP_GET_IDX(priv->last_xfer->ep) == 0) {
412 		return max3421e_xfer_control(dev, priv->last_xfer, hrsl);
413 	}
414 
415 	return max3421e_xfer_bulk(dev, priv->last_xfer, hrsl);
416 }
417 
max3421e_xfer_drop_active(const struct device * dev,int err)418 static void max3421e_xfer_drop_active(const struct device *dev, int err)
419 {
420 	struct max3421e_data *priv = uhc_get_private(dev);
421 
422 	if (priv->last_xfer) {
423 		uhc_xfer_return(dev, priv->last_xfer, err);
424 		priv->last_xfer = NULL;
425 	}
426 }
427 
max3421e_hrslt_success(const struct device * dev)428 static int max3421e_hrslt_success(const struct device *dev)
429 {
430 	struct max3421e_data *priv = uhc_get_private(dev);
431 	struct uhc_transfer *const xfer = priv->last_xfer;
432 	struct net_buf *buf = xfer->buf;
433 	bool finished = false;
434 	int err = 0;
435 	size_t len;
436 	uint8_t bc;
437 
438 	switch (MAX3421E_HXFR_TYPE(priv->hxfr)) {
439 	case MAX3421E_HXFR_TYPE_SETUP:
440 		if (xfer->buf != NULL) {
441 			xfer->stage = UHC_CONTROL_STAGE_DATA;
442 		} else {
443 			xfer->stage = UHC_CONTROL_STAGE_STATUS;
444 		}
445 		break;
446 	case MAX3421E_HXFR_TYPE_HSOUT:
447 		LOG_DBG("HSOUT");
448 		finished = true;
449 		break;
450 	case MAX3421E_HXFR_TYPE_HSIN:
451 		LOG_DBG("HSIN");
452 		finished = true;
453 		break;
454 	case MAX3421E_HXFR_TYPE_ISOOUT:
455 		LOG_ERR("ISO OUT is not implemented");
456 		k_panic();
457 		break;
458 	case MAX3421E_HXFR_TYPE_ISOIN:
459 		LOG_ERR("ISO IN is not implemented");
460 		k_panic();
461 		break;
462 	case MAX3421E_HXFR_TYPE_BULKOUT:
463 		if (buf->len == 0) {
464 			LOG_INF("hrslt bulk out %u", buf->len);
465 			if (xfer->ep == USB_CONTROL_EP_OUT) {
466 				xfer->stage = UHC_CONTROL_STAGE_STATUS;
467 			} else {
468 				finished = true;
469 			}
470 		}
471 		break;
472 	case MAX3421E_HXFR_TYPE_BULKIN:
473 		err = max3421e_read(dev, MAX3421E_REG_RCVBC, &bc, sizeof(bc));
474 		if (err) {
475 			break;
476 		}
477 
478 		if (bc > net_buf_tailroom(buf)) {
479 			LOG_WRN("%u received bytes will be dropped",
480 				bc - net_buf_tailroom(buf));
481 		}
482 
483 		len = MIN(net_buf_tailroom(buf), bc);
484 		err = max3421e_read(dev, MAX3421E_REG_RCVFIFO,
485 				    net_buf_add(buf, len), len);
486 		if (err) {
487 			break;
488 		}
489 
490 		LOG_INF("bc %u tr %u", bc, net_buf_tailroom(buf));
491 
492 		if (bc < MAX3421E_MAX_EP_SIZE || !net_buf_tailroom(buf)) {
493 			LOG_INF("hrslt bulk in %u, %u", bc, len);
494 			if (xfer->ep == USB_CONTROL_EP_IN) {
495 				xfer->stage = UHC_CONTROL_STAGE_STATUS;
496 			} else {
497 				finished = true;
498 			}
499 		}
500 		break;
501 	}
502 
503 	if (finished) {
504 		LOG_DBG("Transfer finished");
505 		uhc_xfer_return(dev, xfer, 0);
506 		priv->last_xfer = NULL;
507 	}
508 
509 	if (err) {
510 		max3421e_xfer_drop_active(dev, err);
511 	}
512 
513 	return err;
514 }
515 
max3421e_handle_hxfrdn(const struct device * dev)516 static int max3421e_handle_hxfrdn(const struct device *dev)
517 {
518 	struct max3421e_data *priv = uhc_get_private(dev);
519 	struct uhc_transfer *const xfer = priv->last_xfer;
520 	const uint8_t hrsl = priv->hrsl;
521 	int ret = 0;
522 
523 	if (xfer == NULL) {
524 		LOG_ERR("No transfers to handle");
525 		return -ENODATA;
526 	}
527 
528 	switch (MAX3421E_HRSLT(hrsl)) {
529 	case MAX3421E_HR_NAK:
530 		/*
531 		 * The transfer did not take place within
532 		 * the specified number of frames.
533 		 *
534 		 * TODO: Transfer cancel request (xfer->cancel)
535 		 * can be handled here as well.
536 		 */
537 		if (xfer->timeout == 0) {
538 			max3421e_xfer_drop_active(dev, -ETIMEDOUT);
539 		}
540 
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 			schedule = true;
686 		}
687 
688 		/* Frame Generator Interrupt */
689 		if (priv->hirq & MAX3421E_FRAME) {
690 			schedule = HRSLT_IS_BUSY(priv->hrsl) ? false : true;
691 		}
692 
693 		/* Shorten the if path a little */
694 		if (priv->hirq & ~(MAX3421E_FRAME | MAX3421E_HXFRDN)) {
695 			err = max3421e_handle_bus_irq(dev);
696 			if (unlikely(err)) {
697 				uhc_submit_event(dev, UHC_EVT_ERROR, err);
698 			}
699 		}
700 
701 		/* Clear interrupts and schedule new bus transfer */
702 		err = max3421e_clear_hirq(dev, priv->hirq);
703 		if (unlikely(err)) {
704 			uhc_submit_event(dev, UHC_EVT_ERROR, err);
705 		}
706 
707 		if (schedule) {
708 			err = max3421e_schedule_xfer(dev);
709 			if (unlikely(err)) {
710 				uhc_submit_event(dev, UHC_EVT_ERROR, err);
711 			}
712 		}
713 
714 		max3421e_unlock(dev);
715 	}
716 }
717 
max3421e_gpio_cb(const struct device * dev,struct gpio_callback * cb,uint32_t pins)718 static void max3421e_gpio_cb(const struct device *dev,
719 			     struct gpio_callback *cb,
720 			     uint32_t pins)
721 {
722 	struct max3421e_data *priv =
723 		CONTAINER_OF(cb, struct max3421e_data, gpio_cb);
724 
725 	k_sem_give(&priv->irq_sem);
726 }
727 
728 /* Enable SOF generator */
max3421e_sof_enable(const struct device * dev)729 static int max3421e_sof_enable(const struct device *dev)
730 {
731 	struct max3421e_data *priv = uhc_get_private(dev);
732 
733 	if (priv->mode & MAX3421E_SOFKAENAB) {
734 		return -EALREADY;
735 	}
736 
737 	priv->mode |= MAX3421E_SOFKAENAB;
738 
739 	return max3421e_write_byte(dev, MAX3421E_REG_MODE, priv->mode);
740 }
741 
742 /* Disable SOF generator and suspend bus */
max3421e_bus_suspend(const struct device * dev)743 static int max3421e_bus_suspend(const struct device *dev)
744 {
745 	struct max3421e_data *priv = uhc_get_private(dev);
746 
747 	if (!(priv->mode & MAX3421E_SOFKAENAB)) {
748 		return -EALREADY;
749 	}
750 
751 	priv->hien |= MAX3421E_SUSDN;
752 	priv->mode &= ~MAX3421E_SOFKAENAB;
753 
754 	uint8_t tmp[3] = {MAX3421E_SUSDN, priv->hien, priv->mode};
755 
756 	return max3421e_write(dev, MAX3421E_REG_HIRQ, tmp, sizeof(tmp));
757 }
758 
759 /* Signal bus reset, 50ms SE0 signal */
max3421e_bus_reset(const struct device * dev)760 static int max3421e_bus_reset(const struct device *dev)
761 {
762 	struct max3421e_data *priv = uhc_get_private(dev);
763 	int ret;
764 
765 	if (atomic_test_bit(&priv->state, MAX3421E_STATE_BUS_RESUME)) {
766 		return -EBUSY;
767 	}
768 
769 	ret = max3421e_write_byte(dev, MAX3421E_REG_HCTL, MAX3421E_BUSRST);
770 	atomic_set_bit(&priv->state, MAX3421E_STATE_BUS_RESET);
771 
772 	return ret;
773 }
774 
775 /* Signal bus resume event, 20ms K-state + low-speed EOP */
max3421e_bus_resume(const struct device * dev)776 static int max3421e_bus_resume(const struct device *dev)
777 {
778 	struct max3421e_data *priv = uhc_get_private(dev);
779 	int ret;
780 
781 	if (atomic_test_bit(&priv->state, MAX3421E_STATE_BUS_RESET)) {
782 		return -EBUSY;
783 	}
784 
785 	ret = max3421e_write_byte(dev, MAX3421E_REG_HCTL, MAX3421E_SIGRSM);
786 	atomic_set_bit(&priv->state, MAX3421E_STATE_BUS_RESUME);
787 
788 	return ret;
789 }
790 
max3421e_enqueue(const struct device * dev,struct uhc_transfer * const xfer)791 static int max3421e_enqueue(const struct device *dev,
792 			    struct uhc_transfer *const xfer)
793 {
794 	return uhc_xfer_append(dev, xfer);
795 }
796 
max3421e_dequeue(const struct device * dev,struct uhc_transfer * const xfer)797 static int max3421e_dequeue(const struct device *dev,
798 			    struct uhc_transfer *const xfer)
799 {
800 	/* TODO */
801 	return 0;
802 }
803 
max3421e_reset(const struct device * dev)804 static int max3421e_reset(const struct device *dev)
805 {
806 	const struct max3421e_config *config = dev->config;
807 	int ret;
808 
809 	if (config->dt_rst.port) {
810 		gpio_pin_set_dt(&config->dt_rst, 1);
811 		gpio_pin_set_dt(&config->dt_rst, 0);
812 	} else {
813 		LOG_DBG("Reset MAX3421E using CHIPRES");
814 		ret = max3421e_write_byte(dev, MAX3421E_REG_USBCTL, MAX3421E_CHIPRES);
815 		ret |= max3421e_write_byte(dev, MAX3421E_REG_USBCTL, 0);
816 
817 		if (ret) {
818 			return ret;
819 		}
820 	}
821 
822 	for (int i = 0; i < CONFIG_MAX3421E_OSC_WAIT_RETRIES; i++) {
823 		uint8_t usbirq;
824 
825 		ret = max3421e_read(dev, MAX3421E_REG_USBIRQ,
826 				    &usbirq, sizeof(usbirq));
827 
828 		LOG_DBG("USBIRQ 0x%02x", usbirq);
829 		if (usbirq & MAX3421E_OSCOKIRQ) {
830 			return 0;
831 		}
832 
833 		k_msleep(3);
834 	}
835 
836 	return -EIO;
837 }
838 
max3421e_pinctl_setup(const struct device * dev)839 static int max3421e_pinctl_setup(const struct device *dev)
840 {
841 	/* Full-Duplex SPI, INT pin edge active, GPX pin signals SOF */
842 	const uint8_t pinctl = MAX3421E_FDUPSPI | MAX3421E_GPXB | MAX3421E_GPXA;
843 	uint8_t tmp;
844 	int ret;
845 
846 	ret = max3421e_write_byte(dev, MAX3421E_REG_PINCTL, pinctl);
847 	if (unlikely(ret)) {
848 		return ret;
849 	}
850 
851 	ret = max3421e_read(dev, MAX3421E_REG_PINCTL, &tmp, sizeof(tmp));
852 	if (unlikely(ret)) {
853 		return ret;
854 	}
855 
856 	if (unlikely(tmp != pinctl)) {
857 		LOG_ERR("Failed to verify PINCTL register 0x%02x vs 0x%02x",
858 			pinctl, tmp);
859 		return -EIO;
860 	}
861 
862 	return 0;
863 }
864 
865 /*
866  * Cache MODE and HIEN register values to avoid having to read it
867  * before modifying register bits.
868  */
max3421e_mode_setup(const struct device * dev)869 static int max3421e_mode_setup(const struct device *dev)
870 {
871 	/*
872 	 * MODE register defaults:
873 	 *   host mode, connect internal D+ and D- pulldown resistors to ground
874 	 */
875 	const uint8_t mode = MAX3421E_DPPULLDN | MAX3421E_DMPULLDN |
876 			     MAX3421E_DELAYISO | MAX3421E_HOST;
877 	struct max3421e_data *priv = uhc_get_private(dev);
878 	uint8_t tmp;
879 	int ret;
880 
881 	ret = max3421e_write_byte(dev, MAX3421E_REG_MODE, mode);
882 	if (ret) {
883 		return ret;
884 	}
885 
886 	ret = max3421e_read(dev, MAX3421E_REG_MODE, &tmp, sizeof(tmp));
887 	if (ret) {
888 		return ret;
889 	}
890 
891 	if (tmp != mode) {
892 		LOG_ERR("Failed to verify MODE register 0x%02x vs 0x%02x",
893 			mode, tmp);
894 		return -EIO;
895 	}
896 
897 	priv->mode = mode;
898 
899 	return 0;
900 }
901 
max3421e_hien_setup(const struct device * dev)902 static int max3421e_hien_setup(const struct device *dev)
903 {
904 	/* Host interrupts enabled by default */
905 	const uint8_t hien = MAX3421E_HXFRDN | MAX3421E_FRAME |
906 			     MAX3421E_CONDET | MAX3421E_RWU |
907 			     MAX3421E_BUSEVENT;
908 	struct max3421e_data *priv = uhc_get_private(dev);
909 	uint8_t tmp;
910 	int ret;
911 
912 	ret = max3421e_write_byte(dev, MAX3421E_REG_HIEN, hien);
913 	if (ret) {
914 		return ret;
915 	}
916 
917 	ret = max3421e_read(dev, MAX3421E_REG_HIEN, &tmp, sizeof(tmp));
918 	if (ret) {
919 		return ret;
920 	}
921 
922 	if (tmp != hien) {
923 		LOG_ERR("Failed to verify HIEN register 0x%02x vs 0x%02x",
924 			hien, tmp);
925 		return -EIO;
926 	}
927 
928 	priv->hien = hien;
929 
930 	return 0;
931 }
932 
max3421e_enable_int_output(const struct device * dev)933 static int max3421e_enable_int_output(const struct device *dev)
934 {
935 	const uint8_t cpuctl = MAX3421E_IE;
936 	uint8_t tmp;
937 	int ret;
938 
939 	/* Enable MAX3421E INT output pin */
940 	ret = max3421e_write_byte(dev, MAX3421E_REG_CPUCTL, cpuctl);
941 	if (ret) {
942 		return ret;
943 	}
944 
945 	ret = max3421e_read(dev, MAX3421E_REG_CPUCTL, &tmp, sizeof(tmp));
946 	if (ret) {
947 		return ret;
948 	}
949 
950 	if (tmp != cpuctl) {
951 		LOG_ERR("Failed to verify CPUCTL register 0x%02x vs 0x%02x",
952 			cpuctl, tmp);
953 		return -EIO;
954 	}
955 
956 	return 0;
957 }
958 
uhc_max3421e_init(const struct device * dev)959 static int uhc_max3421e_init(const struct device *dev)
960 {
961 	struct max3421e_data *priv = uhc_get_private(dev);
962 	uint8_t rev;
963 	int ret;
964 
965 	ret = max3421e_pinctl_setup(dev);
966 	if (ret) {
967 		LOG_ERR("Failed to setup pinctl");
968 		return ret;
969 	}
970 
971 	ret = max3421e_read(dev, MAX3421E_REG_REVISION, &rev, sizeof(rev));
972 	if (ret) {
973 		LOG_ERR("Failed to read revision");
974 		return ret;
975 	}
976 
977 	ret = max3421e_reset(dev);
978 	if (ret) {
979 		LOG_ERR("Failed to reset MAX3421E");
980 		return ret;
981 	}
982 
983 	ret = max3421e_mode_setup(dev);
984 	if (ret) {
985 		LOG_ERR("Failed to setup controller mode");
986 		return ret;
987 	}
988 
989 	ret = max3421e_hien_setup(dev);
990 	if (ret) {
991 		LOG_ERR("Failed to setup interrupts");
992 		return ret;
993 	}
994 
995 	ret = max3421e_enable_int_output(dev);
996 	if (ret) {
997 		LOG_ERR("Failed to enable INT output");
998 		return ret;
999 	}
1000 
1001 	LOG_INF("REV 0x%x, MODE 0x%02x, HIEN 0x%02x",
1002 		rev, priv->mode, priv->hien);
1003 
1004 	priv->addr = 0;
1005 
1006 	/* Sample bus if device is already connected */
1007 	return max3421e_write_byte(dev, MAX3421E_REG_HCTL, MAX3421E_SAMPLEBUS);
1008 }
1009 
uhc_max3421e_enable(const struct device * dev)1010 static int uhc_max3421e_enable(const struct device *dev)
1011 {
1012 	/* TODO */
1013 	return 0;
1014 }
1015 
uhc_max3421e_disable(const struct device * dev)1016 static int uhc_max3421e_disable(const struct device *dev)
1017 {
1018 	/* TODO */
1019 	return 0;
1020 }
1021 
uhc_max3421e_shutdown(const struct device * dev)1022 static int uhc_max3421e_shutdown(const struct device *dev)
1023 {
1024 	/* TODO */
1025 	return 0;
1026 }
1027 
max3421e_driver_init(const struct device * dev)1028 static int max3421e_driver_init(const struct device *dev)
1029 {
1030 	const struct max3421e_config *config = dev->config;
1031 	struct uhc_data *data = dev->data;
1032 	struct max3421e_data *priv = data->priv;
1033 	int ret;
1034 
1035 	if (config->dt_rst.port) {
1036 		if (!gpio_is_ready_dt(&config->dt_rst)) {
1037 			LOG_ERR("GPIO device %s not ready",
1038 				config->dt_rst.port->name);
1039 			return -EIO;
1040 		}
1041 
1042 		ret = gpio_pin_configure_dt(&config->dt_rst,
1043 					    GPIO_OUTPUT_INACTIVE);
1044 		if (ret) {
1045 			LOG_ERR("Failed to configure GPIO pin %u",
1046 				config->dt_rst.pin);
1047 			return ret;
1048 		}
1049 	}
1050 
1051 	if (!spi_is_ready_dt(&config->dt_spi)) {
1052 		LOG_ERR("SPI device %s not ready", config->dt_spi.bus->name);
1053 		return -EIO;
1054 	}
1055 
1056 	if (!gpio_is_ready_dt(&config->dt_int)) {
1057 		LOG_ERR("GPIO device %s not ready", config->dt_int.port->name);
1058 		return -EIO;
1059 	}
1060 
1061 	ret = gpio_pin_configure_dt(&config->dt_int, GPIO_INPUT);
1062 	if (ret) {
1063 		LOG_ERR("Failed to configure GPIO pin %u", config->dt_int.pin);
1064 		return ret;
1065 	}
1066 
1067 	gpio_init_callback(&priv->gpio_cb, max3421e_gpio_cb,
1068 			   BIT(config->dt_int.pin));
1069 	ret = gpio_add_callback(config->dt_int.port, &priv->gpio_cb);
1070 	if (ret) {
1071 		return ret;
1072 	}
1073 
1074 	ret = gpio_pin_interrupt_configure_dt(&config->dt_int,
1075 					      GPIO_INT_EDGE_TO_ACTIVE);
1076 	if (ret) {
1077 		return ret;
1078 	}
1079 
1080 	k_mutex_init(&data->mutex);
1081 	k_thread_create(&drv_stack_data, drv_stack,
1082 			K_KERNEL_STACK_SIZEOF(drv_stack),
1083 			uhc_max3421e_thread,
1084 			(void *)dev, NULL, NULL,
1085 			K_PRIO_COOP(2), 0, K_NO_WAIT);
1086 	k_thread_name_set(&drv_stack_data, "uhc_max3421e");
1087 
1088 	LOG_DBG("MAX3421E CPU interface initialized");
1089 
1090 	return 0;
1091 }
1092 
1093 static const struct uhc_api max3421e_uhc_api = {
1094 	.lock = max3421e_lock,
1095 	.unlock = max3421e_unlock,
1096 	.init = uhc_max3421e_init,
1097 	.enable = uhc_max3421e_enable,
1098 	.disable = uhc_max3421e_disable,
1099 	.shutdown = uhc_max3421e_shutdown,
1100 
1101 	.bus_reset = max3421e_bus_reset,
1102 	.sof_enable  = max3421e_sof_enable,
1103 	.bus_suspend = max3421e_bus_suspend,
1104 	.bus_resume = max3421e_bus_resume,
1105 
1106 	.ep_enqueue = max3421e_enqueue,
1107 	.ep_dequeue = max3421e_dequeue,
1108 };
1109 
1110 static struct max3421e_data max3421e_data = {
1111 	.irq_sem = Z_SEM_INITIALIZER(max3421e_data.irq_sem, 0, 1),
1112 };
1113 
1114 static struct uhc_data max3421e_uhc_data = {
1115 	.priv = &max3421e_data,
1116 };
1117 
1118 static const struct max3421e_config max3421e_cfg = {
1119 	.dt_spi = SPI_DT_SPEC_INST_GET(0, SPI_WORD_SET(8) | SPI_TRANSFER_MSB, 0),
1120 	.dt_int = GPIO_DT_SPEC_INST_GET(0, int_gpios),
1121 	.dt_rst = GPIO_DT_SPEC_INST_GET_OR(0, reset_gpios, {0}),
1122 };
1123 
1124 DEVICE_DT_INST_DEFINE(0, max3421e_driver_init, NULL,
1125 		      &max3421e_uhc_data, &max3421e_cfg,
1126 		      POST_KERNEL, 99,
1127 		      &max3421e_uhc_api);
1128