1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2008 - 2015 Michal Simek <monstr@monstr.eu>
4 * Clean driver and add xilinx constant from header file
6 * (C) Copyright 2004 Atmark Techno, Inc.
7 * Yasushi SHOJI <yashi@atmark-techno.com>
13 #include <linux/bitops.h>
14 #include <linux/compiler.h>
17 #define SR_TX_FIFO_FULL BIT(3) /* transmit FIFO full */
18 #define SR_TX_FIFO_EMPTY BIT(2) /* transmit FIFO empty */
19 #define SR_RX_FIFO_VALID_DATA BIT(0) /* data in receive FIFO */
20 #define SR_RX_FIFO_FULL BIT(1) /* receive FIFO full */
22 #define ULITE_CONTROL_RST_TX 0x01
23 #define ULITE_CONTROL_RST_RX 0x02
25 static bool little_endian __section(".data");
34 struct uartlite_plat {
35 struct uartlite *regs;
38 static u32 uart_in32(void __iomem *addr)
46 static void uart_out32(void __iomem *addr, u32 val)
54 static int uartlite_serial_putc(struct udevice *dev, const char ch)
56 struct uartlite_plat *plat = dev_get_plat(dev);
57 struct uartlite *regs = plat->regs;
59 if (uart_in32(®s->status) & SR_TX_FIFO_FULL)
62 uart_out32(®s->tx_fifo, ch & 0xff);
67 static int uartlite_serial_getc(struct udevice *dev)
69 struct uartlite_plat *plat = dev_get_plat(dev);
70 struct uartlite *regs = plat->regs;
72 if (!(uart_in32(®s->status) & SR_RX_FIFO_VALID_DATA))
75 return uart_in32(®s->rx_fifo) & 0xff;
78 static int uartlite_serial_pending(struct udevice *dev, bool input)
80 struct uartlite_plat *plat = dev_get_plat(dev);
81 struct uartlite *regs = plat->regs;
84 return uart_in32(®s->status) & SR_RX_FIFO_VALID_DATA;
86 return !(uart_in32(®s->status) & SR_TX_FIFO_EMPTY);
89 static int uartlite_serial_probe(struct udevice *dev)
91 struct uartlite_plat *plat = dev_get_plat(dev);
92 struct uartlite *regs = plat->regs;
95 uart_out32(®s->control, 0);
96 uart_out32(®s->control, ULITE_CONTROL_RST_RX | ULITE_CONTROL_RST_TX);
97 ret = uart_in32(®s->status);
98 /* Endianness detection */
99 if ((ret & SR_TX_FIFO_EMPTY) != SR_TX_FIFO_EMPTY) {
100 little_endian = true;
101 uart_out32(®s->control, ULITE_CONTROL_RST_RX |
102 ULITE_CONTROL_RST_TX);
108 static int uartlite_serial_of_to_plat(struct udevice *dev)
110 struct uartlite_plat *plat = dev_get_plat(dev);
112 plat->regs = dev_read_addr_ptr(dev);
117 static const struct dm_serial_ops uartlite_serial_ops = {
118 .putc = uartlite_serial_putc,
119 .pending = uartlite_serial_pending,
120 .getc = uartlite_serial_getc,
123 static const struct udevice_id uartlite_serial_ids[] = {
124 { .compatible = "xlnx,opb-uartlite-1.00.b", },
125 { .compatible = "xlnx,xps-uartlite-1.00.a" },
129 U_BOOT_DRIVER(serial_uartlite) = {
130 .name = "serial_uartlite",
132 .of_match = uartlite_serial_ids,
133 .of_to_plat = uartlite_serial_of_to_plat,
134 .plat_auto = sizeof(struct uartlite_plat),
135 .probe = uartlite_serial_probe,
136 .ops = &uartlite_serial_ops,
139 #ifdef CONFIG_DEBUG_UART_UARTLITE
141 #include <debug_uart.h>
143 static inline void _debug_uart_init(void)
145 struct uartlite *regs = (struct uartlite *)CONFIG_VAL(DEBUG_UART_BASE);
148 uart_out32(®s->control, 0);
149 uart_out32(®s->control, ULITE_CONTROL_RST_RX | ULITE_CONTROL_RST_TX);
150 ret = uart_in32(®s->status);
151 /* Endianness detection */
152 if ((ret & SR_TX_FIFO_EMPTY) != SR_TX_FIFO_EMPTY) {
153 little_endian = true;
154 uart_out32(®s->control, ULITE_CONTROL_RST_RX |
155 ULITE_CONTROL_RST_TX);
159 static inline void _debug_uart_putc(int ch)
161 struct uartlite *regs = (struct uartlite *)CONFIG_VAL(DEBUG_UART_BASE);
163 while (uart_in32(®s->status) & SR_TX_FIFO_FULL)
166 uart_out32(®s->tx_fifo, ch & 0xff);