1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2011-2015 Vladimir Zapolskiy <vz@mleia.com>
9 #include <dm/platform_data/lpc32xx_hsuart.h>
11 #include <asm/arch/uart.h>
12 #include <linux/compiler.h>
14 struct lpc32xx_hsuart_priv {
15 struct hsuart_regs *hsuart;
18 static int lpc32xx_serial_setbrg(struct udevice *dev, int baudrate)
20 struct lpc32xx_hsuart_priv *priv = dev_get_priv(dev);
21 struct hsuart_regs *hsuart = priv->hsuart;
24 /* UART rate = PERIPH_CLK / ((HSU_RATE + 1) x 14) */
25 div = (get_serial_clock() / 14 + baudrate / 2) / baudrate - 1;
29 writel(div, &hsuart->rate);
34 static int lpc32xx_serial_getc(struct udevice *dev)
36 struct lpc32xx_hsuart_priv *priv = dev_get_priv(dev);
37 struct hsuart_regs *hsuart = priv->hsuart;
39 if (!(readl(&hsuart->level) & HSUART_LEVEL_RX))
42 return readl(&hsuart->rx) & HSUART_RX_DATA;
45 static int lpc32xx_serial_putc(struct udevice *dev, const char c)
47 struct lpc32xx_hsuart_priv *priv = dev_get_priv(dev);
48 struct hsuart_regs *hsuart = priv->hsuart;
50 /* Wait for empty FIFO */
51 if (readl(&hsuart->level) & HSUART_LEVEL_TX)
54 writel(c, &hsuart->tx);
59 static int lpc32xx_serial_pending(struct udevice *dev, bool input)
61 struct lpc32xx_hsuart_priv *priv = dev_get_priv(dev);
62 struct hsuart_regs *hsuart = priv->hsuart;
65 if (readl(&hsuart->level) & HSUART_LEVEL_RX)
68 if (readl(&hsuart->level) & HSUART_LEVEL_TX)
75 static int lpc32xx_serial_init(struct hsuart_regs *hsuart)
77 /* Disable hardware RTS and CTS flow control, set up RX and TX FIFO */
78 writel(HSUART_CTRL_TMO_16 | HSUART_CTRL_HSU_OFFSET(20) |
79 HSUART_CTRL_HSU_RX_TRIG_32 | HSUART_CTRL_HSU_TX_TRIG_0,
85 static int lpc32xx_hsuart_probe(struct udevice *dev)
87 struct lpc32xx_hsuart_platdata *platdata = dev_get_platdata(dev);
88 struct lpc32xx_hsuart_priv *priv = dev_get_priv(dev);
90 priv->hsuart = (struct hsuart_regs *)platdata->base;
92 lpc32xx_serial_init(priv->hsuart);
97 static const struct dm_serial_ops lpc32xx_hsuart_ops = {
98 .setbrg = lpc32xx_serial_setbrg,
99 .getc = lpc32xx_serial_getc,
100 .putc = lpc32xx_serial_putc,
101 .pending = lpc32xx_serial_pending,
104 U_BOOT_DRIVER(lpc32xx_hsuart) = {
105 .name = "lpc32xx_hsuart",
107 .probe = lpc32xx_hsuart_probe,
108 .ops = &lpc32xx_hsuart_ops,
109 .priv_auto_alloc_size = sizeof(struct lpc32xx_hsuart_priv),
110 .flags = DM_FLAG_PRE_RELOC,