summaryrefslogtreecommitdiff
path: root/drivers/serial/serial_mxs.c
blob: 3659948b8728a84155a54f01d6c83f05fd6b728f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// SPDX-License-Identifier: GPL-2.0+
/*
 * Copyright (C) 2023 Marek Vasut <marex@denx.de>
 */
#include <common.h>
#include <dm.h>
#include <malloc.h>
#include <serial.h>
#include <wait_bit.h>

#define SET_REG					0x4
#define CLR_REG					0x8

#define AUART_CTRL0				0x00
#define AUART_CTRL1				0x10
#define AUART_CTRL2				0x20
#define AUART_LINECTRL				0x30
#define AUART_INTR				0x50
#define AUART_DATA				0x60
#define AUART_STAT				0x70

#define AUART_CTRL0_SFTRST			BIT(31)
#define AUART_CTRL0_CLKGATE			BIT(30)

#define AUART_CTRL2_UARTEN			BIT(0)

#define AUART_LINECTRL_BAUD_DIVINT(v)		(((v) & 0xffff) << 16)
#define AUART_LINECTRL_BAUD_DIVFRAC(v)		(((v) & 0x3f) << 8)
#define AUART_LINECTRL_WLEN(v)			((((v) - 5) & 0x3) << 5)

#define AUART_STAT_TXFE				BIT(27)
#define AUART_STAT_TXFF				BIT(25)
#define AUART_STAT_RXFE				BIT(24)

#define AUART_CLK				24000000

struct mxs_auart_uart_priv {
	void __iomem *base;
};

static int mxs_auart_uart_setbrg(struct udevice *dev, int baudrate)
{
	struct mxs_auart_uart_priv *priv = dev_get_priv(dev);
	u32 div;

	writel(AUART_CTRL0_CLKGATE, priv->base + AUART_CTRL0 + CLR_REG);
	writel(AUART_CTRL0_SFTRST, priv->base + AUART_CTRL0 + CLR_REG);

	writel(AUART_CTRL2_UARTEN, priv->base + AUART_CTRL2 + SET_REG);

	writel(0, priv->base + AUART_INTR);

	div = DIV_ROUND_CLOSEST(AUART_CLK * 32, baudrate);

	/* Disable FIFO, baudrate, 8N1. */
	writel(AUART_LINECTRL_BAUD_DIVFRAC(div & 0x3F) |
	       AUART_LINECTRL_BAUD_DIVINT(div >> 6) |
	       AUART_LINECTRL_WLEN(8),
	       priv->base + AUART_LINECTRL);

	return 0;
}

static int mxs_auart_uart_pending(struct udevice *dev, bool input)
{
	struct mxs_auart_uart_priv *priv = dev_get_priv(dev);
	u32 stat = readl(priv->base + AUART_STAT);

	if (input)
		return !(stat & AUART_STAT_RXFE);

	return !!(stat & AUART_STAT_TXFE);
}

static int mxs_auart_uart_putc(struct udevice *dev, const char ch)
{
	struct mxs_auart_uart_priv *priv = dev_get_priv(dev);
	u32 stat = readl(priv->base + AUART_STAT);

	if (stat & AUART_STAT_TXFF)
		return -EAGAIN;

	writel(ch, priv->base + AUART_DATA);

	return 0;
}

static int mxs_auart_uart_getc(struct udevice *dev)
{
	struct mxs_auart_uart_priv *priv = dev_get_priv(dev);

	if (!mxs_auart_uart_pending(dev, true))
		return -EAGAIN;

	return readl(priv->base + AUART_DATA) & 0xff;
}

static int mxs_auart_uart_probe(struct udevice *dev)
{
	struct mxs_auart_uart_priv *priv = dev_get_priv(dev);

	priv->base = dev_read_addr_ptr(dev);
	if (!priv->base)
		return -EINVAL;

	return mxs_auart_uart_setbrg(dev, CONFIG_BAUDRATE);
}

static const struct dm_serial_ops mxs_auart_uart_ops = {
	.putc		= mxs_auart_uart_putc,
	.pending	= mxs_auart_uart_pending,
	.getc		= mxs_auart_uart_getc,
	.setbrg		= mxs_auart_uart_setbrg,
};

static const struct udevice_id mxs_auart_uart_ids[] = {
	{ .compatible = "fsl,imx23-auart", },
	{ .compatible = "fsl,imx28-auart", },
	{ /* sentinel */ }
};

U_BOOT_DRIVER(mxs_auart_serial) = {
	.name		= "mxs-auart",
	.id		= UCLASS_SERIAL,
	.of_match	= mxs_auart_uart_ids,
	.probe		= mxs_auart_uart_probe,
	.ops		= &mxs_auart_uart_ops,
	.priv_auto	= sizeof(struct mxs_auart_uart_priv),
};