xref: /openbmc/u-boot/drivers/serial/serial_linflexuart.c (revision 83d290c56fab2d38cd1ab4c4cc7099559c1d5046)
1*83d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+
21f68dbc8SStoica Cosmin-Stefan /*
31f68dbc8SStoica Cosmin-Stefan  * (C) Copyright 2013-2016 Freescale Semiconductor, Inc.
41f68dbc8SStoica Cosmin-Stefan  */
51f68dbc8SStoica Cosmin-Stefan 
61f68dbc8SStoica Cosmin-Stefan #include <common.h>
71f68dbc8SStoica Cosmin-Stefan #include <dm.h>
81f68dbc8SStoica Cosmin-Stefan #include <errno.h>
91f68dbc8SStoica Cosmin-Stefan #include <watchdog.h>
101f68dbc8SStoica Cosmin-Stefan #include <asm/io.h>
111f68dbc8SStoica Cosmin-Stefan #include <serial.h>
121f68dbc8SStoica Cosmin-Stefan #include <linux/compiler.h>
131f68dbc8SStoica Cosmin-Stefan #include <asm/arch/imx-regs.h>
141f68dbc8SStoica Cosmin-Stefan #include <asm/arch/clock.h>
151f68dbc8SStoica Cosmin-Stefan 
161f68dbc8SStoica Cosmin-Stefan #define US1_TDRE            (1 << 7)
171f68dbc8SStoica Cosmin-Stefan #define US1_RDRF            (1 << 5)
181f68dbc8SStoica Cosmin-Stefan #define UC2_TE              (1 << 3)
191f68dbc8SStoica Cosmin-Stefan #define LINCR1_INIT         (1 << 0)
201f68dbc8SStoica Cosmin-Stefan #define LINCR1_MME          (1 << 4)
211f68dbc8SStoica Cosmin-Stefan #define LINCR1_BF           (1 << 7)
221f68dbc8SStoica Cosmin-Stefan #define LINSR_LINS_INITMODE (0x00001000)
231f68dbc8SStoica Cosmin-Stefan #define LINSR_LINS_MASK     (0x0000F000)
241f68dbc8SStoica Cosmin-Stefan #define UARTCR_UART         (1 << 0)
251f68dbc8SStoica Cosmin-Stefan #define UARTCR_WL0          (1 << 1)
261f68dbc8SStoica Cosmin-Stefan #define UARTCR_PCE          (1 << 2)
271f68dbc8SStoica Cosmin-Stefan #define UARTCR_PC0          (1 << 3)
281f68dbc8SStoica Cosmin-Stefan #define UARTCR_TXEN         (1 << 4)
291f68dbc8SStoica Cosmin-Stefan #define UARTCR_RXEN         (1 << 5)
301f68dbc8SStoica Cosmin-Stefan #define UARTCR_PC1          (1 << 6)
311f68dbc8SStoica Cosmin-Stefan #define UARTSR_DTF          (1 << 1)
321f68dbc8SStoica Cosmin-Stefan #define UARTSR_DRF          (1 << 2)
331f68dbc8SStoica Cosmin-Stefan #define UARTSR_RMB          (1 << 9)
341f68dbc8SStoica Cosmin-Stefan 
351f68dbc8SStoica Cosmin-Stefan DECLARE_GLOBAL_DATA_PTR;
361f68dbc8SStoica Cosmin-Stefan 
_linflex_serial_setbrg(struct linflex_fsl * base,int baudrate)371f68dbc8SStoica Cosmin-Stefan static void _linflex_serial_setbrg(struct linflex_fsl *base, int baudrate)
381f68dbc8SStoica Cosmin-Stefan {
391f68dbc8SStoica Cosmin-Stefan 	u32 clk = mxc_get_clock(MXC_UART_CLK);
401f68dbc8SStoica Cosmin-Stefan 	u32 ibr, fbr;
411f68dbc8SStoica Cosmin-Stefan 
421f68dbc8SStoica Cosmin-Stefan 	if (!baudrate)
431f68dbc8SStoica Cosmin-Stefan 		baudrate = CONFIG_BAUDRATE;
441f68dbc8SStoica Cosmin-Stefan 
451f68dbc8SStoica Cosmin-Stefan 	ibr = (u32) (clk / (16 * gd->baudrate));
461f68dbc8SStoica Cosmin-Stefan 	fbr = (u32) (clk % (16 * gd->baudrate)) * 16;
471f68dbc8SStoica Cosmin-Stefan 
481f68dbc8SStoica Cosmin-Stefan 	__raw_writel(ibr, &base->linibrr);
491f68dbc8SStoica Cosmin-Stefan 	__raw_writel(fbr, &base->linfbrr);
501f68dbc8SStoica Cosmin-Stefan }
511f68dbc8SStoica Cosmin-Stefan 
_linflex_serial_getc(struct linflex_fsl * base)521f68dbc8SStoica Cosmin-Stefan static int _linflex_serial_getc(struct linflex_fsl *base)
531f68dbc8SStoica Cosmin-Stefan {
541f68dbc8SStoica Cosmin-Stefan 	char c;
551f68dbc8SStoica Cosmin-Stefan 
561f68dbc8SStoica Cosmin-Stefan 	if (!(__raw_readb(&base->uartsr) & UARTSR_DRF))
571f68dbc8SStoica Cosmin-Stefan 		return -EAGAIN;
581f68dbc8SStoica Cosmin-Stefan 
591f68dbc8SStoica Cosmin-Stefan 	if (!(__raw_readl(&base->uartsr) & UARTSR_RMB))
601f68dbc8SStoica Cosmin-Stefan 		return -EAGAIN;
611f68dbc8SStoica Cosmin-Stefan 
621f68dbc8SStoica Cosmin-Stefan 	c = __raw_readl(&base->bdrm);
631f68dbc8SStoica Cosmin-Stefan 	__raw_writeb((__raw_readb(&base->uartsr) | (UARTSR_DRF | UARTSR_RMB)),
641f68dbc8SStoica Cosmin-Stefan 		     &base->uartsr);
651f68dbc8SStoica Cosmin-Stefan 	return c;
661f68dbc8SStoica Cosmin-Stefan }
671f68dbc8SStoica Cosmin-Stefan 
_linflex_serial_putc(struct linflex_fsl * base,const char c)681f68dbc8SStoica Cosmin-Stefan static int _linflex_serial_putc(struct linflex_fsl *base, const char c)
691f68dbc8SStoica Cosmin-Stefan {
701f68dbc8SStoica Cosmin-Stefan 	__raw_writeb(c, &base->bdrl);
711f68dbc8SStoica Cosmin-Stefan 
721f68dbc8SStoica Cosmin-Stefan 
731f68dbc8SStoica Cosmin-Stefan 	if (!(__raw_readb(&base->uartsr) & UARTSR_DTF))
741f68dbc8SStoica Cosmin-Stefan 		return -EAGAIN;
751f68dbc8SStoica Cosmin-Stefan 
761f68dbc8SStoica Cosmin-Stefan 	__raw_writeb((__raw_readb(&base->uartsr) | UARTSR_DTF), &base->uartsr);
771f68dbc8SStoica Cosmin-Stefan 
781f68dbc8SStoica Cosmin-Stefan 	return 0;
791f68dbc8SStoica Cosmin-Stefan }
801f68dbc8SStoica Cosmin-Stefan 
811f68dbc8SStoica Cosmin-Stefan /*
821f68dbc8SStoica Cosmin-Stefan  * Initialise the serial port with the given baudrate. The settings
831f68dbc8SStoica Cosmin-Stefan  * are always 8 data bits, no parity, 1 stop bit, no start bits.
841f68dbc8SStoica Cosmin-Stefan  */
_linflex_serial_init(struct linflex_fsl * base)851f68dbc8SStoica Cosmin-Stefan static int _linflex_serial_init(struct linflex_fsl *base)
861f68dbc8SStoica Cosmin-Stefan {
871f68dbc8SStoica Cosmin-Stefan 	volatile u32 ctrl;
881f68dbc8SStoica Cosmin-Stefan 
891f68dbc8SStoica Cosmin-Stefan 	/* set the Linflex in master mode amd activate by-pass filter */
901f68dbc8SStoica Cosmin-Stefan 	ctrl = LINCR1_BF | LINCR1_MME;
911f68dbc8SStoica Cosmin-Stefan 	__raw_writel(ctrl, &base->lincr1);
921f68dbc8SStoica Cosmin-Stefan 
931f68dbc8SStoica Cosmin-Stefan 	/* init mode */
941f68dbc8SStoica Cosmin-Stefan 	ctrl |= LINCR1_INIT;
951f68dbc8SStoica Cosmin-Stefan 	__raw_writel(ctrl, &base->lincr1);
961f68dbc8SStoica Cosmin-Stefan 
971f68dbc8SStoica Cosmin-Stefan 	/* waiting for init mode entry - TODO: add a timeout */
981f68dbc8SStoica Cosmin-Stefan 	while ((__raw_readl(&base->linsr) & LINSR_LINS_MASK) !=
991f68dbc8SStoica Cosmin-Stefan 	       LINSR_LINS_INITMODE);
1001f68dbc8SStoica Cosmin-Stefan 
1011f68dbc8SStoica Cosmin-Stefan 	/* set UART bit to allow writing other bits */
1021f68dbc8SStoica Cosmin-Stefan 	__raw_writel(UARTCR_UART, &base->uartcr);
1031f68dbc8SStoica Cosmin-Stefan 
1041f68dbc8SStoica Cosmin-Stefan 	/* provide data bits, parity, stop bit, etc */
1051f68dbc8SStoica Cosmin-Stefan 	serial_setbrg();
1061f68dbc8SStoica Cosmin-Stefan 
1071f68dbc8SStoica Cosmin-Stefan 	/* 8 bit data, no parity, Tx and Rx enabled, UART mode */
1081f68dbc8SStoica Cosmin-Stefan 	__raw_writel(UARTCR_PC1 | UARTCR_RXEN | UARTCR_TXEN | UARTCR_PC0
1091f68dbc8SStoica Cosmin-Stefan 		     | UARTCR_WL0 | UARTCR_UART, &base->uartcr);
1101f68dbc8SStoica Cosmin-Stefan 
1111f68dbc8SStoica Cosmin-Stefan 	ctrl = __raw_readl(&base->lincr1);
1121f68dbc8SStoica Cosmin-Stefan 	ctrl &= ~LINCR1_INIT;
1131f68dbc8SStoica Cosmin-Stefan 	__raw_writel(ctrl, &base->lincr1);	/* end init mode */
1141f68dbc8SStoica Cosmin-Stefan 
1151f68dbc8SStoica Cosmin-Stefan 	return 0;
1161f68dbc8SStoica Cosmin-Stefan }
1171f68dbc8SStoica Cosmin-Stefan 
1181f68dbc8SStoica Cosmin-Stefan struct linflex_serial_platdata {
1191f68dbc8SStoica Cosmin-Stefan 	struct linflex_fsl *base_addr;
1201f68dbc8SStoica Cosmin-Stefan 	u8 port_id; /* do we need this? */
1211f68dbc8SStoica Cosmin-Stefan };
1221f68dbc8SStoica Cosmin-Stefan 
1231f68dbc8SStoica Cosmin-Stefan struct linflex_serial_priv {
1241f68dbc8SStoica Cosmin-Stefan 	struct linflex_fsl *lfuart;
1251f68dbc8SStoica Cosmin-Stefan };
1261f68dbc8SStoica Cosmin-Stefan 
linflex_serial_setbrg(struct udevice * dev,int baudrate)1271f68dbc8SStoica Cosmin-Stefan int linflex_serial_setbrg(struct udevice *dev, int baudrate)
1281f68dbc8SStoica Cosmin-Stefan {
1291f68dbc8SStoica Cosmin-Stefan 	struct linflex_serial_priv *priv = dev_get_priv(dev);
1301f68dbc8SStoica Cosmin-Stefan 
1311f68dbc8SStoica Cosmin-Stefan 	_linflex_serial_setbrg(priv->lfuart, baudrate);
1321f68dbc8SStoica Cosmin-Stefan 
1331f68dbc8SStoica Cosmin-Stefan 	return 0;
1341f68dbc8SStoica Cosmin-Stefan }
1351f68dbc8SStoica Cosmin-Stefan 
linflex_serial_getc(struct udevice * dev)1361f68dbc8SStoica Cosmin-Stefan static int linflex_serial_getc(struct udevice *dev)
1371f68dbc8SStoica Cosmin-Stefan {
1381f68dbc8SStoica Cosmin-Stefan 	struct linflex_serial_priv *priv = dev_get_priv(dev);
1391f68dbc8SStoica Cosmin-Stefan 
1401f68dbc8SStoica Cosmin-Stefan 	return _linflex_serial_getc(priv->lfuart);
1411f68dbc8SStoica Cosmin-Stefan }
1421f68dbc8SStoica Cosmin-Stefan 
linflex_serial_putc(struct udevice * dev,const char ch)1431f68dbc8SStoica Cosmin-Stefan static int linflex_serial_putc(struct udevice *dev, const char ch)
1441f68dbc8SStoica Cosmin-Stefan {
1451f68dbc8SStoica Cosmin-Stefan 
1461f68dbc8SStoica Cosmin-Stefan 	struct linflex_serial_priv *priv = dev_get_priv(dev);
1471f68dbc8SStoica Cosmin-Stefan 
1481f68dbc8SStoica Cosmin-Stefan 	return _linflex_serial_putc(priv->lfuart, ch);
1491f68dbc8SStoica Cosmin-Stefan }
1501f68dbc8SStoica Cosmin-Stefan 
linflex_serial_pending(struct udevice * dev,bool input)1511f68dbc8SStoica Cosmin-Stefan static int linflex_serial_pending(struct udevice *dev, bool input)
1521f68dbc8SStoica Cosmin-Stefan {
1531f68dbc8SStoica Cosmin-Stefan 	struct linflex_serial_priv *priv = dev_get_priv(dev);
1541f68dbc8SStoica Cosmin-Stefan 	uint32_t uartsr = __raw_readl(&priv->lfuart->uartsr);
1551f68dbc8SStoica Cosmin-Stefan 
1561f68dbc8SStoica Cosmin-Stefan 	if (input)
1571f68dbc8SStoica Cosmin-Stefan 		return ((uartsr & UARTSR_DRF) && (uartsr & UARTSR_RMB)) ? 1 : 0;
1581f68dbc8SStoica Cosmin-Stefan 	else
1591f68dbc8SStoica Cosmin-Stefan 		return uartsr & UARTSR_DTF ? 0 : 1;
1601f68dbc8SStoica Cosmin-Stefan }
1611f68dbc8SStoica Cosmin-Stefan 
linflex_serial_init_internal(struct linflex_fsl * lfuart)1621f68dbc8SStoica Cosmin-Stefan static void linflex_serial_init_internal(struct linflex_fsl *lfuart)
1631f68dbc8SStoica Cosmin-Stefan {
1641f68dbc8SStoica Cosmin-Stefan 	_linflex_serial_init(lfuart);
1651f68dbc8SStoica Cosmin-Stefan 	_linflex_serial_setbrg(lfuart, CONFIG_BAUDRATE);
1661f68dbc8SStoica Cosmin-Stefan 	return;
1671f68dbc8SStoica Cosmin-Stefan }
1681f68dbc8SStoica Cosmin-Stefan 
linflex_serial_probe(struct udevice * dev)1691f68dbc8SStoica Cosmin-Stefan static int linflex_serial_probe(struct udevice *dev)
1701f68dbc8SStoica Cosmin-Stefan {
1711f68dbc8SStoica Cosmin-Stefan 	struct linflex_serial_platdata *plat = dev->platdata;
1721f68dbc8SStoica Cosmin-Stefan 	struct linflex_serial_priv *priv = dev_get_priv(dev);
1731f68dbc8SStoica Cosmin-Stefan 
1741f68dbc8SStoica Cosmin-Stefan 	priv->lfuart = (struct linflex_fsl *)plat->base_addr;
1751f68dbc8SStoica Cosmin-Stefan 	linflex_serial_init_internal(priv->lfuart);
1761f68dbc8SStoica Cosmin-Stefan 
1771f68dbc8SStoica Cosmin-Stefan 	return 0;
1781f68dbc8SStoica Cosmin-Stefan }
1791f68dbc8SStoica Cosmin-Stefan 
1801f68dbc8SStoica Cosmin-Stefan static const struct dm_serial_ops linflex_serial_ops = {
1811f68dbc8SStoica Cosmin-Stefan 	.putc = linflex_serial_putc,
1821f68dbc8SStoica Cosmin-Stefan 	.pending = linflex_serial_pending,
1831f68dbc8SStoica Cosmin-Stefan 	.getc = linflex_serial_getc,
1841f68dbc8SStoica Cosmin-Stefan 	.setbrg = linflex_serial_setbrg,
1851f68dbc8SStoica Cosmin-Stefan };
1861f68dbc8SStoica Cosmin-Stefan 
1871f68dbc8SStoica Cosmin-Stefan U_BOOT_DRIVER(serial_linflex) = {
1881f68dbc8SStoica Cosmin-Stefan 	.name	= "serial_linflex",
1891f68dbc8SStoica Cosmin-Stefan 	.id	= UCLASS_SERIAL,
1901f68dbc8SStoica Cosmin-Stefan 	.probe = linflex_serial_probe,
1911f68dbc8SStoica Cosmin-Stefan 	.ops	= &linflex_serial_ops,
1921f68dbc8SStoica Cosmin-Stefan 	.flags = DM_FLAG_PRE_RELOC,
1931f68dbc8SStoica Cosmin-Stefan 	.priv_auto_alloc_size	= sizeof(struct linflex_serial_priv),
1941f68dbc8SStoica Cosmin-Stefan };
1951f68dbc8SStoica Cosmin-Stefan 
1961f68dbc8SStoica Cosmin-Stefan #ifdef CONFIG_DEBUG_UART_LINFLEXUART
1971f68dbc8SStoica Cosmin-Stefan 
1981f68dbc8SStoica Cosmin-Stefan #include <debug_uart.h>
1991f68dbc8SStoica Cosmin-Stefan 
2001f68dbc8SStoica Cosmin-Stefan 
_debug_uart_init(void)2011f68dbc8SStoica Cosmin-Stefan static inline void _debug_uart_init(void)
2021f68dbc8SStoica Cosmin-Stefan {
2031f68dbc8SStoica Cosmin-Stefan 	struct linflex_fsl *base = (struct linflex_fsl *)CONFIG_DEBUG_UART_BASE;
2041f68dbc8SStoica Cosmin-Stefan 
2051f68dbc8SStoica Cosmin-Stefan 	linflex_serial_init_internal(base);
2061f68dbc8SStoica Cosmin-Stefan }
2071f68dbc8SStoica Cosmin-Stefan 
_debug_uart_putc(int ch)2081f68dbc8SStoica Cosmin-Stefan static inline void _debug_uart_putc(int ch)
2091f68dbc8SStoica Cosmin-Stefan {
2101f68dbc8SStoica Cosmin-Stefan 	struct linflex_fsl *base = (struct linflex_fsl *)CONFIG_DEBUG_UART_BASE;
2111f68dbc8SStoica Cosmin-Stefan 
2121f68dbc8SStoica Cosmin-Stefan 	/* XXX: Is this OK? Should this use the non-DM version? */
2131f68dbc8SStoica Cosmin-Stefan 	_linflex_serial_putc(base, ch);
2141f68dbc8SStoica Cosmin-Stefan }
2151f68dbc8SStoica Cosmin-Stefan 
2161f68dbc8SStoica Cosmin-Stefan DEBUG_UART_FUNCS
2171f68dbc8SStoica Cosmin-Stefan 
2181f68dbc8SStoica Cosmin-Stefan #endif /* CONFIG_DEBUG_UART_LINFLEXUART */
219