1 /*
2 * Copyright (c) 2022 Antmicro <www.antmicro.com>
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #define DT_DRV_COMPAT quicklogic_usbserialport_s3b
8
9 #include <zephyr/kernel.h>
10 #include <zephyr/drivers/uart.h>
11 #include <eoss3_dev.h>
12
13 #include "uart_ql_usbserialport_s3b.h"
14
15 /*
16 * code is a modified version of usbserial driver from https://github.com/QuickLogic-Corp/qorc-sdk
17 * freertos_gateware/src/eoss3_hal_fpga_usbserial.c
18 * freertos_gateware/inc/eoss3_hal_fpga_usbserial.h
19 */
20
21 volatile struct fpga_usbserial_regs *usbserial_regs
22 = (struct fpga_usbserial_regs *)(FPGA_PERIPH_BASE);
23
usbserial_tx_fifo_status(void)24 static uint32_t usbserial_tx_fifo_status(void)
25 {
26 return usbserial_regs->m2u_fifo_flags;
27 }
28
usbserial_tx_fifo_full(void)29 static bool usbserial_tx_fifo_full(void)
30 {
31 return usbserial_tx_fifo_status() == USBSERIAL_TX_FIFO_FULL;
32 }
33
usbserial_rx_fifo_status(void)34 static uint32_t usbserial_rx_fifo_status(void)
35 {
36 return usbserial_regs->u2m_fifo_flags;
37 }
38
usbserial_rx_fifo_empty(void)39 static bool usbserial_rx_fifo_empty(void)
40 {
41 return usbserial_rx_fifo_status() == USBSERIAL_RX_FIFO_EMPTY;
42 }
43
44 /**
45 * @brief Output a character in polled mode.
46 *
47 * Writes data to tx register. Waits for space if transmitter is full.
48 *
49 * @param dev UART device struct
50 * @param c Character to send
51 */
uart_usbserial_poll_out(const struct device * dev,unsigned char c)52 static void uart_usbserial_poll_out(const struct device *dev, unsigned char c)
53 {
54 /* Wait for room in Tx FIFO */
55 while (usbserial_tx_fifo_full())
56 ;
57 usbserial_regs->wdata = c;
58 }
59
60 /**
61 * @brief Poll the device for input.
62 *
63 * @param dev UART device struct
64 * @param c Pointer to character
65 *
66 * @return 0 if a character arrived, -1 if the input buffer if empty.
67 */
uart_usbserial_poll_in(const struct device * dev,unsigned char * c)68 static int uart_usbserial_poll_in(const struct device *dev, unsigned char *c)
69 {
70 if (usbserial_rx_fifo_empty()) {
71 return -1;
72 }
73
74 *c = usbserial_regs->rdata;
75 return 0;
76 }
77
78 static const struct uart_driver_api uart_usbserial_driver_api = {
79 .poll_in = uart_usbserial_poll_in,
80 .poll_out = uart_usbserial_poll_out,
81 };
82
83 DEVICE_DT_INST_DEFINE(0, NULL, NULL, NULL, NULL, PRE_KERNEL_1,
84 CONFIG_SERIAL_INIT_PRIORITY,
85 (void *)&uart_usbserial_driver_api);
86