2018-05-06 21:58:06 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2013-05-27 22:55:45 +00:00
|
|
|
/*
|
2019-01-31 12:08:10 +00:00
|
|
|
* Copyright 2019 NXP
|
2013-05-27 22:55:45 +00:00
|
|
|
* Copyright 2013 Freescale Semiconductor, Inc.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
2021-12-14 18:36:40 +00:00
|
|
|
#include <clock_legacy.h>
|
2018-10-18 22:26:23 +00:00
|
|
|
#include <clk.h>
|
2016-01-14 03:39:04 +00:00
|
|
|
#include <dm.h>
|
2017-02-22 08:21:51 +00:00
|
|
|
#include <fsl_lpuart.h>
|
2020-05-10 17:40:05 +00:00
|
|
|
#include <log.h>
|
2013-05-27 22:55:45 +00:00
|
|
|
#include <watchdog.h>
|
2020-10-31 03:38:53 +00:00
|
|
|
#include <asm/global_data.h>
|
2013-05-27 22:55:45 +00:00
|
|
|
#include <asm/io.h>
|
|
|
|
#include <serial.h>
|
2020-02-03 14:36:16 +00:00
|
|
|
#include <dm/device_compat.h>
|
2020-05-10 17:40:13 +00:00
|
|
|
#include <linux/bitops.h>
|
2013-05-27 22:55:45 +00:00
|
|
|
#include <linux/compiler.h>
|
|
|
|
#include <asm/arch/imx-regs.h>
|
|
|
|
#include <asm/arch/clock.h>
|
|
|
|
|
2016-01-14 03:39:01 +00:00
|
|
|
#define US1_TDRE (1 << 7)
|
|
|
|
#define US1_RDRF (1 << 5)
|
|
|
|
#define US1_OR (1 << 3)
|
|
|
|
#define UC2_TE (1 << 3)
|
|
|
|
#define UC2_RE (1 << 2)
|
|
|
|
#define CFIFO_TXFLUSH (1 << 7)
|
|
|
|
#define CFIFO_RXFLUSH (1 << 6)
|
|
|
|
#define SFIFO_RXOF (1 << 2)
|
|
|
|
#define SFIFO_RXUF (1 << 0)
|
2013-05-27 22:55:45 +00:00
|
|
|
|
2014-09-05 05:52:47 +00:00
|
|
|
#define STAT_LBKDIF (1 << 31)
|
|
|
|
#define STAT_RXEDGIF (1 << 30)
|
|
|
|
#define STAT_TDRE (1 << 23)
|
|
|
|
#define STAT_RDRF (1 << 21)
|
|
|
|
#define STAT_IDLE (1 << 20)
|
|
|
|
#define STAT_OR (1 << 19)
|
|
|
|
#define STAT_NF (1 << 18)
|
|
|
|
#define STAT_FE (1 << 17)
|
|
|
|
#define STAT_PF (1 << 16)
|
|
|
|
#define STAT_MA1F (1 << 15)
|
|
|
|
#define STAT_MA2F (1 << 14)
|
|
|
|
#define STAT_FLAGS (STAT_LBKDIF | STAT_RXEDGIF | STAT_IDLE | STAT_OR | \
|
2016-01-14 03:39:01 +00:00
|
|
|
STAT_NF | STAT_FE | STAT_PF | STAT_MA1F | STAT_MA2F)
|
2014-09-05 05:52:47 +00:00
|
|
|
|
|
|
|
#define CTRL_TE (1 << 19)
|
|
|
|
#define CTRL_RE (1 << 18)
|
|
|
|
|
2018-10-18 12:28:32 +00:00
|
|
|
#define FIFO_RXFLUSH BIT(14)
|
|
|
|
#define FIFO_TXFLUSH BIT(15)
|
|
|
|
#define FIFO_TXSIZE_MASK 0x70
|
|
|
|
#define FIFO_TXSIZE_OFF 4
|
|
|
|
#define FIFO_RXSIZE_MASK 0x7
|
|
|
|
#define FIFO_RXSIZE_OFF 0
|
2014-09-05 05:52:47 +00:00
|
|
|
#define FIFO_TXFE 0x80
|
2020-01-10 14:51:43 +00:00
|
|
|
#if defined(CONFIG_ARCH_IMX8) || defined(CONFIG_ARCH_IMXRT)
|
2018-10-18 12:28:31 +00:00
|
|
|
#define FIFO_RXFE 0x08
|
|
|
|
#else
|
2014-09-05 05:52:47 +00:00
|
|
|
#define FIFO_RXFE 0x40
|
2018-10-18 12:28:31 +00:00
|
|
|
#endif
|
2014-09-05 05:52:47 +00:00
|
|
|
|
2018-10-18 12:28:32 +00:00
|
|
|
#define WATER_TXWATER_OFF 0
|
2014-09-05 05:52:47 +00:00
|
|
|
#define WATER_RXWATER_OFF 16
|
|
|
|
|
2013-05-27 22:55:45 +00:00
|
|
|
DECLARE_GLOBAL_DATA_PTR;
|
|
|
|
|
2017-02-22 08:21:51 +00:00
|
|
|
#define LPUART_FLAG_REGMAP_32BIT_REG BIT(0)
|
|
|
|
#define LPUART_FLAG_REGMAP_ENDIAN_BIG BIT(1)
|
|
|
|
|
2017-02-22 08:21:52 +00:00
|
|
|
enum lpuart_devtype {
|
|
|
|
DEV_VF610 = 1,
|
|
|
|
DEV_LS1021A,
|
2018-10-18 12:28:31 +00:00
|
|
|
DEV_MX7ULP,
|
2020-01-10 14:51:43 +00:00
|
|
|
DEV_IMX8,
|
|
|
|
DEV_IMXRT,
|
2017-02-22 08:21:52 +00:00
|
|
|
};
|
|
|
|
|
2020-12-03 23:55:23 +00:00
|
|
|
struct lpuart_serial_plat {
|
2017-02-22 08:21:51 +00:00
|
|
|
void *reg;
|
2017-02-22 08:21:52 +00:00
|
|
|
enum lpuart_devtype devtype;
|
2017-02-22 08:21:51 +00:00
|
|
|
ulong flags;
|
2016-01-14 03:39:04 +00:00
|
|
|
};
|
|
|
|
|
2017-02-22 08:21:51 +00:00
|
|
|
static void lpuart_read32(u32 flags, u32 *addr, u32 *val)
|
|
|
|
{
|
|
|
|
if (flags & LPUART_FLAG_REGMAP_32BIT_REG) {
|
|
|
|
if (flags & LPUART_FLAG_REGMAP_ENDIAN_BIG)
|
|
|
|
*(u32 *)val = in_be32(addr);
|
|
|
|
else
|
|
|
|
*(u32 *)val = in_le32(addr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void lpuart_write32(u32 flags, u32 *addr, u32 val)
|
|
|
|
{
|
|
|
|
if (flags & LPUART_FLAG_REGMAP_32BIT_REG) {
|
|
|
|
if (flags & LPUART_FLAG_REGMAP_ENDIAN_BIG)
|
|
|
|
out_be32(addr, val);
|
|
|
|
else
|
|
|
|
out_le32(addr, val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
u32 __weak get_lpuart_clk(void)
|
2013-05-27 22:55:45 +00:00
|
|
|
{
|
2021-12-14 18:36:40 +00:00
|
|
|
return get_board_sys_clk();
|
2017-02-22 08:21:51 +00:00
|
|
|
}
|
|
|
|
|
2019-07-11 03:33:34 +00:00
|
|
|
#if CONFIG_IS_ENABLED(CLK)
|
2018-10-18 22:26:23 +00:00
|
|
|
static int get_lpuart_clk_rate(struct udevice *dev, u32 *clk)
|
|
|
|
{
|
|
|
|
struct clk per_clk;
|
|
|
|
ulong rate;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = clk_get_by_name(dev, "per", &per_clk);
|
|
|
|
if (ret) {
|
|
|
|
dev_err(dev, "Failed to get per clk: %d\n", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
rate = clk_get_rate(&per_clk);
|
|
|
|
if ((long)rate <= 0) {
|
|
|
|
dev_err(dev, "Failed to get per clk rate: %ld\n", (long)rate);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
*clk = rate;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
static inline int get_lpuart_clk_rate(struct udevice *dev, u32 *clk)
|
|
|
|
{ return -ENOSYS; }
|
|
|
|
#endif
|
|
|
|
|
2017-02-22 08:21:51 +00:00
|
|
|
static bool is_lpuart32(struct udevice *dev)
|
|
|
|
{
|
2020-12-23 02:30:28 +00:00
|
|
|
struct lpuart_serial_plat *plat = dev_get_plat(dev);
|
2017-02-22 08:21:51 +00:00
|
|
|
|
|
|
|
return plat->flags & LPUART_FLAG_REGMAP_32BIT_REG;
|
|
|
|
}
|
|
|
|
|
2018-10-18 22:26:23 +00:00
|
|
|
static void _lpuart_serial_setbrg(struct udevice *dev,
|
2017-02-22 08:21:51 +00:00
|
|
|
int baudrate)
|
|
|
|
{
|
2020-12-03 23:55:23 +00:00
|
|
|
struct lpuart_serial_plat *plat = dev_get_plat(dev);
|
2017-02-22 08:21:51 +00:00
|
|
|
struct lpuart_fsl *base = plat->reg;
|
2018-10-18 22:26:23 +00:00
|
|
|
u32 clk;
|
2013-05-27 22:55:45 +00:00
|
|
|
u16 sbr;
|
2018-10-18 22:26:23 +00:00
|
|
|
int ret;
|
|
|
|
|
2019-07-11 03:33:34 +00:00
|
|
|
if (CONFIG_IS_ENABLED(CLK)) {
|
2018-10-18 22:26:23 +00:00
|
|
|
ret = get_lpuart_clk_rate(dev, &clk);
|
|
|
|
if (ret)
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
clk = get_lpuart_clk();
|
|
|
|
}
|
2013-05-27 22:55:45 +00:00
|
|
|
|
2016-01-14 03:39:03 +00:00
|
|
|
sbr = (u16)(clk / (16 * baudrate));
|
2013-05-27 22:55:45 +00:00
|
|
|
|
2016-01-14 03:39:01 +00:00
|
|
|
/* place adjustment later - n/32 BRFA */
|
2013-05-27 22:55:45 +00:00
|
|
|
__raw_writeb(sbr >> 8, &base->ubdh);
|
|
|
|
__raw_writeb(sbr & 0xff, &base->ubdl);
|
|
|
|
}
|
|
|
|
|
2020-12-03 23:55:23 +00:00
|
|
|
static int _lpuart_serial_getc(struct lpuart_serial_plat *plat)
|
2013-05-27 22:55:45 +00:00
|
|
|
{
|
2017-02-22 08:21:51 +00:00
|
|
|
struct lpuart_fsl *base = plat->reg;
|
2022-12-10 23:31:21 +00:00
|
|
|
if (!(__raw_readb(&base->us1) & (US1_RDRF | US1_OR)))
|
|
|
|
return -EAGAIN;
|
2013-05-27 22:55:45 +00:00
|
|
|
|
2014-08-19 15:54:27 +00:00
|
|
|
barrier();
|
2013-05-27 22:55:45 +00:00
|
|
|
|
|
|
|
return __raw_readb(&base->ud);
|
|
|
|
}
|
|
|
|
|
2022-12-10 23:31:21 +00:00
|
|
|
static int _lpuart_serial_putc(struct lpuart_serial_plat *plat,
|
2017-02-22 08:21:51 +00:00
|
|
|
const char c)
|
2013-05-27 22:55:45 +00:00
|
|
|
{
|
2017-02-22 08:21:51 +00:00
|
|
|
struct lpuart_fsl *base = plat->reg;
|
|
|
|
|
2022-12-10 23:31:21 +00:00
|
|
|
if (!(__raw_readb(&base->us1) & US1_TDRE))
|
|
|
|
return -EAGAIN;
|
2013-05-27 22:55:45 +00:00
|
|
|
|
|
|
|
__raw_writeb(c, &base->ud);
|
2022-12-10 23:31:21 +00:00
|
|
|
return 0;
|
2013-05-27 22:55:45 +00:00
|
|
|
}
|
|
|
|
|
2016-01-14 03:39:01 +00:00
|
|
|
/* Test whether a character is in the RX buffer */
|
2020-12-03 23:55:23 +00:00
|
|
|
static int _lpuart_serial_tstc(struct lpuart_serial_plat *plat)
|
2013-05-27 22:55:45 +00:00
|
|
|
{
|
2017-02-22 08:21:51 +00:00
|
|
|
struct lpuart_fsl *base = plat->reg;
|
|
|
|
|
2013-05-27 22:55:45 +00:00
|
|
|
if (__raw_readb(&base->urcfifo) == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialise the serial port with the given baudrate. The settings
|
|
|
|
* are always 8 data bits, no parity, 1 stop bit, no start bits.
|
|
|
|
*/
|
2018-10-18 22:26:23 +00:00
|
|
|
static int _lpuart_serial_init(struct udevice *dev)
|
2013-05-27 22:55:45 +00:00
|
|
|
{
|
2020-12-03 23:55:23 +00:00
|
|
|
struct lpuart_serial_plat *plat = dev_get_plat(dev);
|
2017-02-22 08:21:51 +00:00
|
|
|
struct lpuart_fsl *base = (struct lpuart_fsl *)plat->reg;
|
2013-05-27 22:55:45 +00:00
|
|
|
u8 ctrl;
|
|
|
|
|
|
|
|
ctrl = __raw_readb(&base->uc2);
|
|
|
|
ctrl &= ~UC2_RE;
|
|
|
|
ctrl &= ~UC2_TE;
|
|
|
|
__raw_writeb(ctrl, &base->uc2);
|
|
|
|
|
|
|
|
__raw_writeb(0, &base->umodem);
|
|
|
|
__raw_writeb(0, &base->uc1);
|
|
|
|
|
2014-08-19 15:54:28 +00:00
|
|
|
/* Disable FIFO and flush buffer */
|
|
|
|
__raw_writeb(0x0, &base->upfifo);
|
|
|
|
__raw_writeb(0x0, &base->utwfifo);
|
|
|
|
__raw_writeb(0x1, &base->urwfifo);
|
|
|
|
__raw_writeb(CFIFO_TXFLUSH | CFIFO_RXFLUSH, &base->ucfifo);
|
|
|
|
|
2013-05-27 22:55:45 +00:00
|
|
|
/* provide data bits, parity, stop bit, etc */
|
2018-10-18 22:26:23 +00:00
|
|
|
_lpuart_serial_setbrg(dev, gd->baudrate);
|
2013-05-27 22:55:45 +00:00
|
|
|
|
|
|
|
__raw_writeb(UC2_RE | UC2_TE, &base->uc2);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-10-18 22:26:23 +00:00
|
|
|
static void _lpuart32_serial_setbrg_7ulp(struct udevice *dev,
|
2017-02-22 08:21:52 +00:00
|
|
|
int baudrate)
|
|
|
|
{
|
2020-12-03 23:55:23 +00:00
|
|
|
struct lpuart_serial_plat *plat = dev_get_plat(dev);
|
2017-02-22 08:21:52 +00:00
|
|
|
struct lpuart_fsl_reg32 *base = plat->reg;
|
|
|
|
u32 sbr, osr, baud_diff, tmp_osr, tmp_sbr, tmp_diff, tmp;
|
2018-10-18 22:26:23 +00:00
|
|
|
u32 clk;
|
|
|
|
int ret;
|
|
|
|
|
2019-07-11 03:33:34 +00:00
|
|
|
if (CONFIG_IS_ENABLED(CLK)) {
|
2018-10-18 22:26:23 +00:00
|
|
|
ret = get_lpuart_clk_rate(dev, &clk);
|
|
|
|
if (ret)
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
clk = get_lpuart_clk();
|
|
|
|
}
|
2017-02-22 08:21:52 +00:00
|
|
|
|
|
|
|
baud_diff = baudrate;
|
|
|
|
osr = 0;
|
|
|
|
sbr = 0;
|
|
|
|
|
|
|
|
for (tmp_osr = 4; tmp_osr <= 32; tmp_osr++) {
|
|
|
|
tmp_sbr = (clk / (baudrate * tmp_osr));
|
|
|
|
|
|
|
|
if (tmp_sbr == 0)
|
|
|
|
tmp_sbr = 1;
|
|
|
|
|
|
|
|
/*calculate difference in actual buad w/ current values */
|
|
|
|
tmp_diff = (clk / (tmp_osr * tmp_sbr));
|
|
|
|
tmp_diff = tmp_diff - baudrate;
|
|
|
|
|
|
|
|
/* select best values between sbr and sbr+1 */
|
|
|
|
if (tmp_diff > (baudrate - (clk / (tmp_osr * (tmp_sbr + 1))))) {
|
|
|
|
tmp_diff = baudrate - (clk / (tmp_osr * (tmp_sbr + 1)));
|
|
|
|
tmp_sbr++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tmp_diff <= baud_diff) {
|
|
|
|
baud_diff = tmp_diff;
|
|
|
|
osr = tmp_osr;
|
|
|
|
sbr = tmp_sbr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* TODO: handle buadrate outside acceptable rate
|
|
|
|
* if (baudDiff > ((config->baudRate_Bps / 100) * 3))
|
|
|
|
* {
|
|
|
|
* Unacceptable baud rate difference of more than 3%
|
|
|
|
* return kStatus_LPUART_BaudrateNotSupport;
|
|
|
|
* }
|
|
|
|
*/
|
|
|
|
tmp = in_le32(&base->baud);
|
|
|
|
|
|
|
|
if ((osr > 3) && (osr < 8))
|
|
|
|
tmp |= LPUART_BAUD_BOTHEDGE_MASK;
|
|
|
|
|
|
|
|
tmp &= ~LPUART_BAUD_OSR_MASK;
|
|
|
|
tmp |= LPUART_BAUD_OSR(osr-1);
|
|
|
|
|
|
|
|
tmp &= ~LPUART_BAUD_SBR_MASK;
|
|
|
|
tmp |= LPUART_BAUD_SBR(sbr);
|
|
|
|
|
|
|
|
/* explicitly disable 10 bit mode & set 1 stop bit */
|
|
|
|
tmp &= ~(LPUART_BAUD_M10_MASK | LPUART_BAUD_SBNS_MASK);
|
|
|
|
|
|
|
|
out_le32(&base->baud, tmp);
|
|
|
|
}
|
|
|
|
|
2018-10-18 22:26:23 +00:00
|
|
|
static void _lpuart32_serial_setbrg(struct udevice *dev,
|
2017-02-22 08:21:51 +00:00
|
|
|
int baudrate)
|
2014-09-05 05:52:47 +00:00
|
|
|
{
|
2020-12-03 23:55:23 +00:00
|
|
|
struct lpuart_serial_plat *plat = dev_get_plat(dev);
|
2017-02-22 08:21:51 +00:00
|
|
|
struct lpuart_fsl_reg32 *base = plat->reg;
|
2018-10-18 22:26:23 +00:00
|
|
|
u32 clk;
|
2014-09-05 05:52:47 +00:00
|
|
|
u32 sbr;
|
2018-10-18 22:26:23 +00:00
|
|
|
int ret;
|
|
|
|
|
2019-07-11 03:33:34 +00:00
|
|
|
if (CONFIG_IS_ENABLED(CLK)) {
|
2018-10-18 22:26:23 +00:00
|
|
|
ret = get_lpuart_clk_rate(dev, &clk);
|
|
|
|
if (ret)
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
clk = get_lpuart_clk();
|
|
|
|
}
|
2014-09-05 05:52:47 +00:00
|
|
|
|
2016-01-14 03:39:03 +00:00
|
|
|
sbr = (clk / (16 * baudrate));
|
2014-09-05 05:52:47 +00:00
|
|
|
|
2016-01-14 03:39:01 +00:00
|
|
|
/* place adjustment later - n/32 BRFA */
|
2017-02-22 08:21:51 +00:00
|
|
|
lpuart_write32(plat->flags, &base->baud, sbr);
|
2014-09-05 05:52:47 +00:00
|
|
|
}
|
|
|
|
|
2020-12-03 23:55:23 +00:00
|
|
|
static int _lpuart32_serial_getc(struct lpuart_serial_plat *plat)
|
2014-09-05 05:52:47 +00:00
|
|
|
{
|
2017-02-22 08:21:51 +00:00
|
|
|
struct lpuart_fsl_reg32 *base = plat->reg;
|
2017-02-22 08:21:52 +00:00
|
|
|
u32 stat, val;
|
2014-09-05 05:52:47 +00:00
|
|
|
|
2017-02-22 08:21:51 +00:00
|
|
|
lpuart_read32(plat->flags, &base->stat, &stat);
|
2022-12-10 23:31:21 +00:00
|
|
|
if ((stat & STAT_RDRF) == 0) {
|
2017-02-22 08:21:51 +00:00
|
|
|
lpuart_write32(plat->flags, &base->stat, STAT_FLAGS);
|
2022-12-10 23:31:21 +00:00
|
|
|
return -EAGAIN;
|
2014-09-05 05:52:47 +00:00
|
|
|
}
|
|
|
|
|
2017-02-22 08:21:52 +00:00
|
|
|
lpuart_read32(plat->flags, &base->data, &val);
|
2017-02-22 08:21:51 +00:00
|
|
|
|
2018-01-10 06:27:14 +00:00
|
|
|
lpuart_read32(plat->flags, &base->stat, &stat);
|
|
|
|
if (stat & STAT_OR)
|
|
|
|
lpuart_write32(plat->flags, &base->stat, STAT_OR);
|
2017-02-22 08:21:52 +00:00
|
|
|
|
|
|
|
return val & 0x3ff;
|
2014-09-05 05:52:47 +00:00
|
|
|
}
|
|
|
|
|
2022-12-10 23:31:21 +00:00
|
|
|
static int _lpuart32_serial_putc(struct lpuart_serial_plat *plat,
|
2017-02-22 08:21:51 +00:00
|
|
|
const char c)
|
2014-09-05 05:52:47 +00:00
|
|
|
{
|
2017-02-22 08:21:51 +00:00
|
|
|
struct lpuart_fsl_reg32 *base = plat->reg;
|
|
|
|
u32 stat;
|
|
|
|
|
2022-12-10 23:31:21 +00:00
|
|
|
lpuart_read32(plat->flags, &base->stat, &stat);
|
|
|
|
if (!(stat & STAT_TDRE))
|
|
|
|
return -EAGAIN;
|
2014-09-05 05:52:47 +00:00
|
|
|
|
2017-02-22 08:21:51 +00:00
|
|
|
lpuart_write32(plat->flags, &base->data, c);
|
2022-12-10 23:31:21 +00:00
|
|
|
return 0;
|
2014-09-05 05:52:47 +00:00
|
|
|
}
|
|
|
|
|
2016-01-14 03:39:01 +00:00
|
|
|
/* Test whether a character is in the RX buffer */
|
2020-12-03 23:55:23 +00:00
|
|
|
static int _lpuart32_serial_tstc(struct lpuart_serial_plat *plat)
|
2014-09-05 05:52:47 +00:00
|
|
|
{
|
2017-02-22 08:21:51 +00:00
|
|
|
struct lpuart_fsl_reg32 *base = plat->reg;
|
|
|
|
u32 water;
|
|
|
|
|
|
|
|
lpuart_read32(plat->flags, &base->water, &water);
|
|
|
|
|
|
|
|
if ((water >> 24) == 0)
|
2014-09-05 05:52:47 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialise the serial port with the given baudrate. The settings
|
|
|
|
* are always 8 data bits, no parity, 1 stop bit, no start bits.
|
|
|
|
*/
|
2018-10-18 22:26:23 +00:00
|
|
|
static int _lpuart32_serial_init(struct udevice *dev)
|
2014-09-05 05:52:47 +00:00
|
|
|
{
|
2020-12-03 23:55:23 +00:00
|
|
|
struct lpuart_serial_plat *plat = dev_get_plat(dev);
|
2017-02-22 08:21:51 +00:00
|
|
|
struct lpuart_fsl_reg32 *base = (struct lpuart_fsl_reg32 *)plat->reg;
|
2018-10-18 12:28:32 +00:00
|
|
|
u32 val, tx_fifo_size;
|
2014-09-05 05:52:47 +00:00
|
|
|
|
2018-10-18 12:28:32 +00:00
|
|
|
lpuart_read32(plat->flags, &base->ctrl, &val);
|
|
|
|
val &= ~CTRL_RE;
|
|
|
|
val &= ~CTRL_TE;
|
|
|
|
lpuart_write32(plat->flags, &base->ctrl, val);
|
2014-09-05 05:52:47 +00:00
|
|
|
|
2017-02-22 08:21:51 +00:00
|
|
|
lpuart_write32(plat->flags, &base->modir, 0);
|
2018-10-18 12:28:32 +00:00
|
|
|
|
|
|
|
lpuart_read32(plat->flags, &base->fifo, &val);
|
|
|
|
tx_fifo_size = (val & FIFO_TXSIZE_MASK) >> FIFO_TXSIZE_OFF;
|
|
|
|
/* Set the TX water to half of FIFO size */
|
|
|
|
if (tx_fifo_size > 1)
|
|
|
|
tx_fifo_size = tx_fifo_size >> 1;
|
|
|
|
|
|
|
|
/* Set RX water to 0, to be triggered by any receive data */
|
|
|
|
lpuart_write32(plat->flags, &base->water,
|
|
|
|
(tx_fifo_size << WATER_TXWATER_OFF));
|
|
|
|
|
|
|
|
/* Enable TX and RX FIFO */
|
|
|
|
val |= (FIFO_TXFE | FIFO_RXFE | FIFO_TXFLUSH | FIFO_RXFLUSH);
|
|
|
|
lpuart_write32(plat->flags, &base->fifo, val);
|
2014-09-05 05:52:47 +00:00
|
|
|
|
2017-02-22 08:21:51 +00:00
|
|
|
lpuart_write32(plat->flags, &base->match, 0);
|
2014-09-05 05:52:47 +00:00
|
|
|
|
2020-01-10 14:51:43 +00:00
|
|
|
if (plat->devtype == DEV_MX7ULP || plat->devtype == DEV_IMX8 ||
|
|
|
|
plat->devtype == DEV_IMXRT) {
|
2018-10-18 22:26:23 +00:00
|
|
|
_lpuart32_serial_setbrg_7ulp(dev, gd->baudrate);
|
2017-02-22 08:21:52 +00:00
|
|
|
} else {
|
|
|
|
/* provide data bits, parity, stop bit, etc */
|
2018-10-18 22:26:23 +00:00
|
|
|
_lpuart32_serial_setbrg(dev, gd->baudrate);
|
2017-02-22 08:21:52 +00:00
|
|
|
}
|
2014-09-05 05:52:47 +00:00
|
|
|
|
2017-02-22 08:21:51 +00:00
|
|
|
lpuart_write32(plat->flags, &base->ctrl, CTRL_RE | CTRL_TE);
|
2014-09-05 05:52:47 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-02-22 08:21:51 +00:00
|
|
|
static int lpuart_serial_setbrg(struct udevice *dev, int baudrate)
|
2016-01-14 03:39:04 +00:00
|
|
|
{
|
2020-12-03 23:55:23 +00:00
|
|
|
struct lpuart_serial_plat *plat = dev_get_plat(dev);
|
2016-01-14 03:39:04 +00:00
|
|
|
|
2017-02-22 08:21:52 +00:00
|
|
|
if (is_lpuart32(dev)) {
|
2020-01-10 14:51:43 +00:00
|
|
|
if (plat->devtype == DEV_MX7ULP || plat->devtype == DEV_IMX8 ||
|
|
|
|
plat->devtype == DEV_IMXRT)
|
2018-10-18 22:26:23 +00:00
|
|
|
_lpuart32_serial_setbrg_7ulp(dev, baudrate);
|
2017-02-22 08:21:52 +00:00
|
|
|
else
|
2018-10-18 22:26:23 +00:00
|
|
|
_lpuart32_serial_setbrg(dev, baudrate);
|
2017-02-22 08:21:52 +00:00
|
|
|
} else {
|
2018-10-18 22:26:23 +00:00
|
|
|
_lpuart_serial_setbrg(dev, baudrate);
|
2017-02-22 08:21:52 +00:00
|
|
|
}
|
2016-01-14 03:39:04 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-02-22 08:21:51 +00:00
|
|
|
static int lpuart_serial_getc(struct udevice *dev)
|
2016-01-14 03:39:04 +00:00
|
|
|
{
|
2020-12-23 02:30:28 +00:00
|
|
|
struct lpuart_serial_plat *plat = dev_get_plat(dev);
|
2016-01-14 03:39:04 +00:00
|
|
|
|
2017-02-22 08:21:51 +00:00
|
|
|
if (is_lpuart32(dev))
|
|
|
|
return _lpuart32_serial_getc(plat);
|
|
|
|
|
|
|
|
return _lpuart_serial_getc(plat);
|
2016-01-14 03:39:04 +00:00
|
|
|
}
|
|
|
|
|
2017-02-22 08:21:51 +00:00
|
|
|
static int lpuart_serial_putc(struct udevice *dev, const char c)
|
2016-01-14 03:39:04 +00:00
|
|
|
{
|
2020-12-23 02:30:28 +00:00
|
|
|
struct lpuart_serial_plat *plat = dev_get_plat(dev);
|
2016-01-14 03:39:04 +00:00
|
|
|
|
2017-02-22 08:21:51 +00:00
|
|
|
if (is_lpuart32(dev))
|
2022-12-10 23:31:21 +00:00
|
|
|
return _lpuart32_serial_putc(plat, c);
|
2016-01-14 03:39:04 +00:00
|
|
|
|
2022-12-10 23:31:21 +00:00
|
|
|
return _lpuart_serial_putc(plat, c);
|
2016-01-14 03:39:04 +00:00
|
|
|
}
|
|
|
|
|
2017-02-22 08:21:51 +00:00
|
|
|
static int lpuart_serial_pending(struct udevice *dev, bool input)
|
2016-01-14 03:39:04 +00:00
|
|
|
{
|
2020-12-23 02:30:28 +00:00
|
|
|
struct lpuart_serial_plat *plat = dev_get_plat(dev);
|
2016-01-14 03:39:04 +00:00
|
|
|
struct lpuart_fsl *reg = plat->reg;
|
2017-02-22 08:21:51 +00:00
|
|
|
struct lpuart_fsl_reg32 *reg32 = plat->reg;
|
|
|
|
u32 stat;
|
|
|
|
|
|
|
|
if (is_lpuart32(dev)) {
|
|
|
|
if (input) {
|
|
|
|
return _lpuart32_serial_tstc(plat);
|
|
|
|
} else {
|
|
|
|
lpuart_read32(plat->flags, ®32->stat, &stat);
|
|
|
|
return stat & STAT_TDRE ? 0 : 1;
|
|
|
|
}
|
|
|
|
}
|
2016-01-14 03:39:04 +00:00
|
|
|
|
|
|
|
if (input)
|
2017-02-22 08:21:51 +00:00
|
|
|
return _lpuart_serial_tstc(plat);
|
2016-01-14 03:39:04 +00:00
|
|
|
else
|
2017-02-22 08:21:51 +00:00
|
|
|
return __raw_readb(®->us1) & US1_TDRE ? 0 : 1;
|
2016-01-14 03:39:04 +00:00
|
|
|
}
|
|
|
|
|
2017-02-22 08:21:51 +00:00
|
|
|
static int lpuart_serial_probe(struct udevice *dev)
|
2016-01-14 03:39:04 +00:00
|
|
|
{
|
2020-01-10 14:47:05 +00:00
|
|
|
#if CONFIG_IS_ENABLED(CLK)
|
|
|
|
struct clk per_clk;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = clk_get_by_name(dev, "per", &per_clk);
|
|
|
|
if (!ret) {
|
|
|
|
ret = clk_enable(&per_clk);
|
|
|
|
if (ret) {
|
|
|
|
dev_err(dev, "Failed to get per clk: %d\n", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
} else {
|
2020-01-31 13:39:47 +00:00
|
|
|
debug("%s: Failed to get per clk: %d\n", __func__, ret);
|
2020-01-10 14:47:05 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-02-22 08:21:51 +00:00
|
|
|
if (is_lpuart32(dev))
|
2018-10-18 22:26:23 +00:00
|
|
|
return _lpuart32_serial_init(dev);
|
2017-02-22 08:21:51 +00:00
|
|
|
else
|
2018-10-18 22:26:23 +00:00
|
|
|
return _lpuart_serial_init(dev);
|
2016-01-14 03:39:04 +00:00
|
|
|
}
|
2013-05-27 22:55:45 +00:00
|
|
|
|
2020-12-03 23:55:21 +00:00
|
|
|
static int lpuart_serial_of_to_plat(struct udevice *dev)
|
2016-01-14 03:39:04 +00:00
|
|
|
{
|
2020-12-23 02:30:28 +00:00
|
|
|
struct lpuart_serial_plat *plat = dev_get_plat(dev);
|
2017-02-22 08:21:52 +00:00
|
|
|
const void *blob = gd->fdt_blob;
|
2017-05-17 23:18:09 +00:00
|
|
|
int node = dev_of_offset(dev);
|
2016-01-14 03:39:04 +00:00
|
|
|
fdt_addr_t addr;
|
|
|
|
|
2020-07-17 05:36:48 +00:00
|
|
|
addr = dev_read_addr(dev);
|
2016-01-14 03:39:04 +00:00
|
|
|
if (addr == FDT_ADDR_T_NONE)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2017-02-22 08:21:51 +00:00
|
|
|
plat->reg = (void *)addr;
|
|
|
|
plat->flags = dev_get_driver_data(dev);
|
2016-01-14 03:39:04 +00:00
|
|
|
|
2019-01-31 12:08:10 +00:00
|
|
|
if (fdtdec_get_bool(blob, node, "little-endian"))
|
|
|
|
plat->flags &= ~LPUART_FLAG_REGMAP_ENDIAN_BIG;
|
|
|
|
|
2017-02-22 08:21:52 +00:00
|
|
|
if (!fdt_node_check_compatible(blob, node, "fsl,ls1021a-lpuart"))
|
|
|
|
plat->devtype = DEV_LS1021A;
|
|
|
|
else if (!fdt_node_check_compatible(blob, node, "fsl,imx7ulp-lpuart"))
|
|
|
|
plat->devtype = DEV_MX7ULP;
|
|
|
|
else if (!fdt_node_check_compatible(blob, node, "fsl,vf610-lpuart"))
|
|
|
|
plat->devtype = DEV_VF610;
|
2018-10-18 12:28:31 +00:00
|
|
|
else if (!fdt_node_check_compatible(blob, node, "fsl,imx8qm-lpuart"))
|
|
|
|
plat->devtype = DEV_IMX8;
|
2020-01-10 14:51:43 +00:00
|
|
|
else if (!fdt_node_check_compatible(blob, node, "fsl,imxrt-lpuart"))
|
|
|
|
plat->devtype = DEV_IMXRT;
|
2017-02-22 08:21:52 +00:00
|
|
|
|
2016-01-14 03:39:04 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct dm_serial_ops lpuart_serial_ops = {
|
|
|
|
.putc = lpuart_serial_putc,
|
|
|
|
.pending = lpuart_serial_pending,
|
|
|
|
.getc = lpuart_serial_getc,
|
|
|
|
.setbrg = lpuart_serial_setbrg,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct udevice_id lpuart_serial_ids[] = {
|
2017-02-22 08:21:51 +00:00
|
|
|
{ .compatible = "fsl,ls1021a-lpuart", .data =
|
|
|
|
LPUART_FLAG_REGMAP_32BIT_REG | LPUART_FLAG_REGMAP_ENDIAN_BIG },
|
2021-10-13 16:14:19 +00:00
|
|
|
{ .compatible = "fsl,ls1028a-lpuart",
|
|
|
|
.data = LPUART_FLAG_REGMAP_32BIT_REG },
|
2017-02-22 08:21:52 +00:00
|
|
|
{ .compatible = "fsl,imx7ulp-lpuart",
|
|
|
|
.data = LPUART_FLAG_REGMAP_32BIT_REG },
|
2017-02-22 08:21:51 +00:00
|
|
|
{ .compatible = "fsl,vf610-lpuart"},
|
2018-10-18 12:28:31 +00:00
|
|
|
{ .compatible = "fsl,imx8qm-lpuart",
|
|
|
|
.data = LPUART_FLAG_REGMAP_32BIT_REG },
|
2020-01-10 14:51:43 +00:00
|
|
|
{ .compatible = "fsl,imxrt-lpuart",
|
|
|
|
.data = LPUART_FLAG_REGMAP_32BIT_REG },
|
2016-01-14 03:39:04 +00:00
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
|
|
|
U_BOOT_DRIVER(serial_lpuart) = {
|
|
|
|
.name = "serial_lpuart",
|
|
|
|
.id = UCLASS_SERIAL,
|
|
|
|
.of_match = lpuart_serial_ids,
|
2020-12-03 23:55:21 +00:00
|
|
|
.of_to_plat = lpuart_serial_of_to_plat,
|
2020-12-03 23:55:23 +00:00
|
|
|
.plat_auto = sizeof(struct lpuart_serial_plat),
|
2016-01-14 03:39:04 +00:00
|
|
|
.probe = lpuart_serial_probe,
|
|
|
|
.ops = &lpuart_serial_ops,
|
|
|
|
};
|