blob: ec828e6784de71ea9dfb549b6f2d36fe0d4a5035 [file] [log] [blame]
wdenk507bbe32004-04-18 21:13:41 +00001/*
Michal Simek93768392015-12-01 14:24:20 +01002 * (C) Copyright 2008 - 2015 Michal Simek <monstr@monstr.eu>
Michal Simek53ea9812008-07-11 10:10:31 +02003 * Clean driver and add xilinx constant from header file
wdenk507bbe32004-04-18 21:13:41 +00004 *
Michal Simek53ea9812008-07-11 10:10:31 +02005 * (C) Copyright 2004 Atmark Techno, Inc.
wdenk507bbe32004-04-18 21:13:41 +00006 * Yasushi SHOJI <yashi@atmark-techno.com>
7 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02008 * SPDX-License-Identifier: GPL-2.0+
wdenk507bbe32004-04-18 21:13:41 +00009 */
10
11#include <config.h>
Michal Simek49a23e42011-09-25 21:03:08 +000012#include <common.h>
Michal Simek93768392015-12-01 14:24:20 +010013#include <dm.h>
Michal Simek53ea9812008-07-11 10:10:31 +020014#include <asm/io.h>
Michal Simek49a23e42011-09-25 21:03:08 +000015#include <linux/compiler.h>
16#include <serial.h>
wdenk507bbe32004-04-18 21:13:41 +000017
Michal Simek93768392015-12-01 14:24:20 +010018#define SR_TX_FIFO_FULL BIT(3) /* transmit FIFO full */
19#define SR_TX_FIFO_EMPTY BIT(2) /* transmit FIFO empty */
20#define SR_RX_FIFO_VALID_DATA BIT(0) /* data in receive FIFO */
21#define SR_RX_FIFO_FULL BIT(1) /* receive FIFO full */
wdenk507bbe32004-04-18 21:13:41 +000022
Michal Simek8c3bd6b2014-01-21 07:29:47 +010023#define ULITE_CONTROL_RST_TX 0x01
24#define ULITE_CONTROL_RST_RX 0x02
25
Michal Simek49a23e42011-09-25 21:03:08 +000026struct uartlite {
27 unsigned int rx_fifo;
28 unsigned int tx_fifo;
29 unsigned int status;
Michal Simek8c3bd6b2014-01-21 07:29:47 +010030 unsigned int control;
Michal Simek49a23e42011-09-25 21:03:08 +000031};
wdenk507bbe32004-04-18 21:13:41 +000032
Michal Simek93768392015-12-01 14:24:20 +010033struct uartlite_platdata {
34 struct uartlite *regs;
Michal Simek49a23e42011-09-25 21:03:08 +000035};
36
Michal Simek93768392015-12-01 14:24:20 +010037static int uartlite_serial_putc(struct udevice *dev, const char ch)
Michal Simek49a23e42011-09-25 21:03:08 +000038{
Michal Simek93768392015-12-01 14:24:20 +010039 struct uartlite_platdata *plat = dev_get_platdata(dev);
40 struct uartlite *regs = plat->regs;
Michal Simek49a23e42011-09-25 21:03:08 +000041
Michal Simek93768392015-12-01 14:24:20 +010042 if (in_be32(&regs->status) & SR_TX_FIFO_FULL)
43 return -EAGAIN;
Michal Simek49a23e42011-09-25 21:03:08 +000044
Michal Simek93768392015-12-01 14:24:20 +010045 out_be32(&regs->tx_fifo, ch & 0xff);
46
47 return 0;
Michal Simek49a23e42011-09-25 21:03:08 +000048}
49
Michal Simek93768392015-12-01 14:24:20 +010050static int uartlite_serial_getc(struct udevice *dev)
Michal Simek49a23e42011-09-25 21:03:08 +000051{
Michal Simek93768392015-12-01 14:24:20 +010052 struct uartlite_platdata *plat = dev_get_platdata(dev);
53 struct uartlite *regs = plat->regs;
Michal Simek49a23e42011-09-25 21:03:08 +000054
Michal Simek93768392015-12-01 14:24:20 +010055 if (!(in_be32(&regs->status) & SR_RX_FIFO_VALID_DATA))
56 return -EAGAIN;
Michal Simek49a23e42011-09-25 21:03:08 +000057
Michal Simek49a23e42011-09-25 21:03:08 +000058 return in_be32(&regs->rx_fifo) & 0xff;
59}
60
Michal Simek93768392015-12-01 14:24:20 +010061static int uartlite_serial_pending(struct udevice *dev, bool input)
Michal Simek49a23e42011-09-25 21:03:08 +000062{
Michal Simek93768392015-12-01 14:24:20 +010063 struct uartlite_platdata *plat = dev_get_platdata(dev);
64 struct uartlite *regs = plat->regs;
Michal Simek49a23e42011-09-25 21:03:08 +000065
Michal Simek93768392015-12-01 14:24:20 +010066 if (input)
67 return in_be32(&regs->status) & SR_RX_FIFO_VALID_DATA;
68
69 return !(in_be32(&regs->status) & SR_TX_FIFO_EMPTY);
Michal Simek49a23e42011-09-25 21:03:08 +000070}
71
Michal Simek93768392015-12-01 14:24:20 +010072static int uartlite_serial_probe(struct udevice *dev)
Michal Simek25239e12012-07-02 10:32:18 +020073{
Michal Simek93768392015-12-01 14:24:20 +010074 struct uartlite_platdata *plat = dev_get_platdata(dev);
75 struct uartlite *regs = plat->regs;
Michal Simek8c3bd6b2014-01-21 07:29:47 +010076
Michal Simek93768392015-12-01 14:24:20 +010077 out_be32(&regs->control, 0);
78 out_be32(&regs->control, ULITE_CONTROL_RST_RX | ULITE_CONTROL_RST_TX);
79 in_be32(&regs->control);
Michal Simek8c3bd6b2014-01-21 07:29:47 +010080
Michal Simek93768392015-12-01 14:24:20 +010081 return 0;
Michal Simek25239e12012-07-02 10:32:18 +020082}
83
Michal Simek93768392015-12-01 14:24:20 +010084static int uartlite_serial_ofdata_to_platdata(struct udevice *dev)
Michal Simek49a23e42011-09-25 21:03:08 +000085{
Michal Simek93768392015-12-01 14:24:20 +010086 struct uartlite_platdata *plat = dev_get_platdata(dev);
Michal Simek25239e12012-07-02 10:32:18 +020087
Simon Glassa821c4a2017-05-17 17:18:05 -060088 plat->regs = (struct uartlite *)devfdt_get_addr(dev);
Michal Simek93768392015-12-01 14:24:20 +010089
90 return 0;
Michal Simek49a23e42011-09-25 21:03:08 +000091}
Marek Vasut87d69222012-09-12 19:45:58 +020092
Michal Simek93768392015-12-01 14:24:20 +010093static const struct dm_serial_ops uartlite_serial_ops = {
94 .putc = uartlite_serial_putc,
95 .pending = uartlite_serial_pending,
96 .getc = uartlite_serial_getc,
97};
98
99static const struct udevice_id uartlite_serial_ids[] = {
100 { .compatible = "xlnx,opb-uartlite-1.00.b", },
101 { .compatible = "xlnx,xps-uartlite-1.00.a" },
102 { }
103};
104
105U_BOOT_DRIVER(serial_uartlite) = {
106 .name = "serial_uartlite",
107 .id = UCLASS_SERIAL,
108 .of_match = uartlite_serial_ids,
109 .ofdata_to_platdata = uartlite_serial_ofdata_to_platdata,
110 .platdata_auto_alloc_size = sizeof(struct uartlite_platdata),
111 .probe = uartlite_serial_probe,
112 .ops = &uartlite_serial_ops,
113 .flags = DM_FLAG_PRE_RELOC,
114};
Michal Simek4166ba32015-12-14 16:55:10 +0100115
116#ifdef CONFIG_DEBUG_UART_UARTLITE
117
118#include <debug_uart.h>
119
120static inline void _debug_uart_init(void)
121{
122 struct uartlite *regs = (struct uartlite *)CONFIG_DEBUG_UART_BASE;
123
124 out_be32(&regs->control, 0);
125 out_be32(&regs->control, ULITE_CONTROL_RST_RX | ULITE_CONTROL_RST_TX);
126 in_be32(&regs->control);
127}
128
129static inline void _debug_uart_putc(int ch)
130{
131 struct uartlite *regs = (struct uartlite *)CONFIG_DEBUG_UART_BASE;
132
133 while (in_be32(&regs->status) & SR_TX_FIFO_FULL)
134 ;
135
136 out_be32(&regs->tx_fifo, ch & 0xff);
137}
138
139DEBUG_UART_FUNCS
140#endif