blob: 83dc96c449ca4949176eaa49bf9ca42e75e8907e [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Stoica Cosmin-Stefan0edfb4e2016-06-05 03:42:59 +03002/*
3 * (C) Copyright 2013-2016 Freescale Semiconductor, Inc.
Stoica Cosmin-Stefan0edfb4e2016-06-05 03:42:59 +03004 */
5
6#include <common.h>
7#include <dm.h>
8#include <errno.h>
9#include <watchdog.h>
10#include <asm/io.h>
11#include <serial.h>
12#include <linux/compiler.h>
13#include <asm/arch/imx-regs.h>
14#include <asm/arch/clock.h>
15
16#define US1_TDRE (1 << 7)
17#define US1_RDRF (1 << 5)
18#define UC2_TE (1 << 3)
19#define LINCR1_INIT (1 << 0)
20#define LINCR1_MME (1 << 4)
21#define LINCR1_BF (1 << 7)
22#define LINSR_LINS_INITMODE (0x00001000)
23#define LINSR_LINS_MASK (0x0000F000)
24#define UARTCR_UART (1 << 0)
25#define UARTCR_WL0 (1 << 1)
26#define UARTCR_PCE (1 << 2)
27#define UARTCR_PC0 (1 << 3)
28#define UARTCR_TXEN (1 << 4)
29#define UARTCR_RXEN (1 << 5)
30#define UARTCR_PC1 (1 << 6)
31#define UARTSR_DTF (1 << 1)
32#define UARTSR_DRF (1 << 2)
33#define UARTSR_RMB (1 << 9)
34
35DECLARE_GLOBAL_DATA_PTR;
36
Stoica Cosmin-Stefan0edfb4e2016-06-05 03:42:59 +030037static void _linflex_serial_setbrg(struct linflex_fsl *base, int baudrate)
38{
39 u32 clk = mxc_get_clock(MXC_UART_CLK);
40 u32 ibr, fbr;
41
42 if (!baudrate)
43 baudrate = CONFIG_BAUDRATE;
44
45 ibr = (u32) (clk / (16 * gd->baudrate));
46 fbr = (u32) (clk % (16 * gd->baudrate)) * 16;
47
48 __raw_writel(ibr, &base->linibrr);
49 __raw_writel(fbr, &base->linfbrr);
50}
51
52static int _linflex_serial_getc(struct linflex_fsl *base)
53{
54 char c;
55
56 if (!(__raw_readb(&base->uartsr) & UARTSR_DRF))
57 return -EAGAIN;
58
59 if (!(__raw_readl(&base->uartsr) & UARTSR_RMB))
60 return -EAGAIN;
61
62 c = __raw_readl(&base->bdrm);
63 __raw_writeb((__raw_readb(&base->uartsr) | (UARTSR_DRF | UARTSR_RMB)),
64 &base->uartsr);
65 return c;
66}
67
68static int _linflex_serial_putc(struct linflex_fsl *base, const char c)
69{
70 __raw_writeb(c, &base->bdrl);
71
72
73 if (!(__raw_readb(&base->uartsr) & UARTSR_DTF))
74 return -EAGAIN;
75
76 __raw_writeb((__raw_readb(&base->uartsr) | UARTSR_DTF), &base->uartsr);
77
78 return 0;
79}
80
81/*
82 * Initialise the serial port with the given baudrate. The settings
83 * are always 8 data bits, no parity, 1 stop bit, no start bits.
84 */
85static int _linflex_serial_init(struct linflex_fsl *base)
86{
87 volatile u32 ctrl;
88
89 /* set the Linflex in master mode amd activate by-pass filter */
90 ctrl = LINCR1_BF | LINCR1_MME;
91 __raw_writel(ctrl, &base->lincr1);
92
93 /* init mode */
94 ctrl |= LINCR1_INIT;
95 __raw_writel(ctrl, &base->lincr1);
96
97 /* waiting for init mode entry - TODO: add a timeout */
98 while ((__raw_readl(&base->linsr) & LINSR_LINS_MASK) !=
99 LINSR_LINS_INITMODE);
100
101 /* set UART bit to allow writing other bits */
102 __raw_writel(UARTCR_UART, &base->uartcr);
103
104 /* provide data bits, parity, stop bit, etc */
105 serial_setbrg();
106
107 /* 8 bit data, no parity, Tx and Rx enabled, UART mode */
108 __raw_writel(UARTCR_PC1 | UARTCR_RXEN | UARTCR_TXEN | UARTCR_PC0
109 | UARTCR_WL0 | UARTCR_UART, &base->uartcr);
110
111 ctrl = __raw_readl(&base->lincr1);
112 ctrl &= ~LINCR1_INIT;
113 __raw_writel(ctrl, &base->lincr1); /* end init mode */
114
115 return 0;
116}
117
118struct linflex_serial_platdata {
119 struct linflex_fsl *base_addr;
120 u8 port_id; /* do we need this? */
121};
122
123struct linflex_serial_priv {
124 struct linflex_fsl *lfuart;
125};
126
127int linflex_serial_setbrg(struct udevice *dev, int baudrate)
128{
129 struct linflex_serial_priv *priv = dev_get_priv(dev);
130
131 _linflex_serial_setbrg(priv->lfuart, baudrate);
132
133 return 0;
134}
135
136static int linflex_serial_getc(struct udevice *dev)
137{
138 struct linflex_serial_priv *priv = dev_get_priv(dev);
139
140 return _linflex_serial_getc(priv->lfuart);
141}
142
143static int linflex_serial_putc(struct udevice *dev, const char ch)
144{
145
146 struct linflex_serial_priv *priv = dev_get_priv(dev);
147
148 return _linflex_serial_putc(priv->lfuart, ch);
149}
150
151static int linflex_serial_pending(struct udevice *dev, bool input)
152{
153 struct linflex_serial_priv *priv = dev_get_priv(dev);
154 uint32_t uartsr = __raw_readl(&priv->lfuart->uartsr);
155
156 if (input)
157 return ((uartsr & UARTSR_DRF) && (uartsr & UARTSR_RMB)) ? 1 : 0;
158 else
159 return uartsr & UARTSR_DTF ? 0 : 1;
160}
161
162static void linflex_serial_init_internal(struct linflex_fsl *lfuart)
163{
164 _linflex_serial_init(lfuart);
165 _linflex_serial_setbrg(lfuart, CONFIG_BAUDRATE);
166 return;
167}
168
169static int linflex_serial_probe(struct udevice *dev)
170{
171 struct linflex_serial_platdata *plat = dev->platdata;
172 struct linflex_serial_priv *priv = dev_get_priv(dev);
173
174 priv->lfuart = (struct linflex_fsl *)plat->base_addr;
175 linflex_serial_init_internal(priv->lfuart);
176
177 return 0;
178}
179
180static const struct dm_serial_ops linflex_serial_ops = {
181 .putc = linflex_serial_putc,
182 .pending = linflex_serial_pending,
183 .getc = linflex_serial_getc,
184 .setbrg = linflex_serial_setbrg,
185};
186
187U_BOOT_DRIVER(serial_linflex) = {
188 .name = "serial_linflex",
189 .id = UCLASS_SERIAL,
190 .probe = linflex_serial_probe,
191 .ops = &linflex_serial_ops,
192 .flags = DM_FLAG_PRE_RELOC,
193 .priv_auto_alloc_size = sizeof(struct linflex_serial_priv),
194};
195
196#ifdef CONFIG_DEBUG_UART_LINFLEXUART
197
198#include <debug_uart.h>
199
200
201static inline void _debug_uart_init(void)
202{
203 struct linflex_fsl *base = (struct linflex_fsl *)CONFIG_DEBUG_UART_BASE;
204
205 linflex_serial_init_internal(base);
206}
207
208static inline void _debug_uart_putc(int ch)
209{
210 struct linflex_fsl *base = (struct linflex_fsl *)CONFIG_DEBUG_UART_BASE;
211
212 /* XXX: Is this OK? Should this use the non-DM version? */
213 _linflex_serial_putc(base, ch);
214}
215
216DEBUG_UART_FUNCS
217
218#endif /* CONFIG_DEBUG_UART_LINFLEXUART */