1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * (C) Copyright 2013-2016 Freescale Semiconductor, Inc.
4 */
5
6 #include <dm.h>
7 #include <errno.h>
8 #include <watchdog.h>
9 #include <asm/global_data.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
35 DECLARE_GLOBAL_DATA_PTR;
36
_linflex_serial_setbrg(struct linflex_fsl * base,int baudrate)37 static 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
_linflex_serial_getc(struct linflex_fsl * base)52 static 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
_linflex_serial_putc(struct linflex_fsl * base,const char c)68 static int _linflex_serial_putc(struct linflex_fsl *base, const char c)
69 {
70 __raw_writeb(c, &base->bdrl);
71
72 if (!(__raw_readb(&base->uartsr) & UARTSR_DTF))
73 return -EAGAIN;
74
75 __raw_writeb((__raw_readb(&base->uartsr) | UARTSR_DTF), &base->uartsr);
76
77 return 0;
78 }
79
80 /*
81 * Initialise the serial port with the given baudrate. The settings
82 * are always 8 data bits, no parity, 1 stop bit, no start bits.
83 */
_linflex_serial_init(struct linflex_fsl * base)84 static int _linflex_serial_init(struct linflex_fsl *base)
85 {
86 volatile u32 ctrl;
87
88 /* set the Linflex in master mode amd activate by-pass filter */
89 ctrl = LINCR1_BF | LINCR1_MME;
90 __raw_writel(ctrl, &base->lincr1);
91
92 /* init mode */
93 ctrl |= LINCR1_INIT;
94 __raw_writel(ctrl, &base->lincr1);
95
96 /* waiting for init mode entry - TODO: add a timeout */
97 while ((__raw_readl(&base->linsr) & LINSR_LINS_MASK) !=
98 LINSR_LINS_INITMODE);
99
100 /* set UART bit to allow writing other bits */
101 __raw_writel(UARTCR_UART, &base->uartcr);
102
103 /* provide data bits, parity, stop bit, etc */
104 serial_setbrg();
105
106 /* 8 bit data, no parity, Tx and Rx enabled, UART mode */
107 __raw_writel(UARTCR_PC1 | UARTCR_RXEN | UARTCR_TXEN | UARTCR_PC0
108 | UARTCR_WL0 | UARTCR_UART, &base->uartcr);
109
110 ctrl = __raw_readl(&base->lincr1);
111 ctrl &= ~LINCR1_INIT;
112 __raw_writel(ctrl, &base->lincr1); /* end init mode */
113
114 return 0;
115 }
116
117 struct linflex_serial_plat {
118 struct linflex_fsl *base_addr;
119 u8 port_id; /* do we need this? */
120 };
121
122 struct linflex_serial_priv {
123 struct linflex_fsl *lfuart;
124 };
125
linflex_serial_setbrg(struct udevice * dev,int baudrate)126 int linflex_serial_setbrg(struct udevice *dev, int baudrate)
127 {
128 struct linflex_serial_priv *priv = dev_get_priv(dev);
129
130 _linflex_serial_setbrg(priv->lfuart, baudrate);
131
132 return 0;
133 }
134
linflex_serial_getc(struct udevice * dev)135 static int linflex_serial_getc(struct udevice *dev)
136 {
137 struct linflex_serial_priv *priv = dev_get_priv(dev);
138
139 return _linflex_serial_getc(priv->lfuart);
140 }
141
linflex_serial_putc(struct udevice * dev,const char ch)142 static int linflex_serial_putc(struct udevice *dev, const char ch)
143 {
144
145 struct linflex_serial_priv *priv = dev_get_priv(dev);
146
147 return _linflex_serial_putc(priv->lfuart, ch);
148 }
149
linflex_serial_pending(struct udevice * dev,bool input)150 static int linflex_serial_pending(struct udevice *dev, bool input)
151 {
152 struct linflex_serial_priv *priv = dev_get_priv(dev);
153 uint32_t uartsr = __raw_readl(&priv->lfuart->uartsr);
154
155 if (input)
156 return ((uartsr & UARTSR_DRF) && (uartsr & UARTSR_RMB)) ? 1 : 0;
157 else
158 return uartsr & UARTSR_DTF ? 0 : 1;
159 }
160
linflex_serial_init_internal(struct linflex_fsl * lfuart)161 static void linflex_serial_init_internal(struct linflex_fsl *lfuart)
162 {
163 _linflex_serial_init(lfuart);
164 _linflex_serial_setbrg(lfuart, CONFIG_BAUDRATE);
165 return;
166 }
167
linflex_serial_probe(struct udevice * dev)168 static int linflex_serial_probe(struct udevice *dev)
169 {
170 struct linflex_serial_plat *plat = dev_get_plat(dev);
171 struct linflex_serial_priv *priv = dev_get_priv(dev);
172
173 priv->lfuart = (struct linflex_fsl *)plat->base_addr;
174 linflex_serial_init_internal(priv->lfuart);
175
176 return 0;
177 }
178
179 static const struct dm_serial_ops linflex_serial_ops = {
180 .putc = linflex_serial_putc,
181 .pending = linflex_serial_pending,
182 .getc = linflex_serial_getc,
183 .setbrg = linflex_serial_setbrg,
184 };
185
186 U_BOOT_DRIVER(serial_linflex) = {
187 .name = "serial_linflex",
188 .id = UCLASS_SERIAL,
189 .probe = linflex_serial_probe,
190 .ops = &linflex_serial_ops,
191 .flags = DM_FLAG_PRE_RELOC,
192 .priv_auto = sizeof(struct linflex_serial_priv),
193 };
194
195 #ifdef CONFIG_DEBUG_UART_LINFLEXUART
196
197 #include <debug_uart.h>
198
_debug_uart_init(void)199 static inline void _debug_uart_init(void)
200 {
201 struct linflex_fsl *base = (struct linflex_fsl *)CONFIG_VAL(DEBUG_UART_BASE);
202
203 linflex_serial_init_internal(base);
204 }
205
_debug_uart_putc(int ch)206 static inline void _debug_uart_putc(int ch)
207 {
208 struct linflex_fsl *base = (struct linflex_fsl *)CONFIG_VAL(DEBUG_UART_BASE);
209
210 /* XXX: Is this OK? Should this use the non-DM version? */
211 _linflex_serial_putc(base, ch);
212 }
213
214 DEBUG_UART_FUNCS
215
216 #endif /* CONFIG_DEBUG_UART_LINFLEXUART */
217