2 * Copyright (C) 2011-2015 Vladimir Zapolskiy <vz@mleia.com>
4 * SPDX-License-Identifier: GPL-2.0+
10 #include <dm/platform_data/lpc32xx_hsuart.h>
12 #include <asm/arch/uart.h>
13 #include <linux/compiler.h>
15 DECLARE_GLOBAL_DATA_PTR
;
17 struct lpc32xx_hsuart_priv
{
18 struct hsuart_regs
*hsuart
;
21 static int lpc32xx_serial_setbrg(struct udevice
*dev
, int baudrate
)
23 struct lpc32xx_hsuart_priv
*priv
= dev_get_priv(dev
);
24 struct hsuart_regs
*hsuart
= priv
->hsuart
;
27 /* UART rate = PERIPH_CLK / ((HSU_RATE + 1) x 14) */
28 div
= (get_serial_clock() / 14 + baudrate
/ 2) / baudrate
- 1;
32 writel(div
, &hsuart
->rate
);
37 static int lpc32xx_serial_getc(struct udevice
*dev
)
39 struct lpc32xx_hsuart_priv
*priv
= dev_get_priv(dev
);
40 struct hsuart_regs
*hsuart
= priv
->hsuart
;
42 if (!(readl(&hsuart
->level
) & HSUART_LEVEL_RX
))
45 return readl(&hsuart
->rx
) & HSUART_RX_DATA
;
48 static int lpc32xx_serial_putc(struct udevice
*dev
, const char c
)
50 struct lpc32xx_hsuart_priv
*priv
= dev_get_priv(dev
);
51 struct hsuart_regs
*hsuart
= priv
->hsuart
;
53 /* Wait for empty FIFO */
54 if (readl(&hsuart
->level
) & HSUART_LEVEL_TX
)
57 writel(c
, &hsuart
->tx
);
62 static int lpc32xx_serial_pending(struct udevice
*dev
, bool input
)
64 struct lpc32xx_hsuart_priv
*priv
= dev_get_priv(dev
);
65 struct hsuart_regs
*hsuart
= priv
->hsuart
;
68 if (readl(&hsuart
->level
) & HSUART_LEVEL_RX
)
71 if (readl(&hsuart
->level
) & HSUART_LEVEL_TX
)
78 static int lpc32xx_serial_init(struct hsuart_regs
*hsuart
)
80 /* Disable hardware RTS and CTS flow control, set up RX and TX FIFO */
81 writel(HSUART_CTRL_TMO_16
| HSUART_CTRL_HSU_OFFSET(20) |
82 HSUART_CTRL_HSU_RX_TRIG_32
| HSUART_CTRL_HSU_TX_TRIG_0
,
88 static int lpc32xx_hsuart_probe(struct udevice
*dev
)
90 struct lpc32xx_hsuart_platdata
*platdata
= dev_get_platdata(dev
);
91 struct lpc32xx_hsuart_priv
*priv
= dev_get_priv(dev
);
93 priv
->hsuart
= (struct hsuart_regs
*)platdata
->base
;
95 lpc32xx_serial_init(priv
->hsuart
);
100 static const struct dm_serial_ops lpc32xx_hsuart_ops
= {
101 .setbrg
= lpc32xx_serial_setbrg
,
102 .getc
= lpc32xx_serial_getc
,
103 .putc
= lpc32xx_serial_putc
,
104 .pending
= lpc32xx_serial_pending
,
107 U_BOOT_DRIVER(lpc32xx_hsuart
) = {
108 .name
= "lpc32xx_hsuart",
110 .probe
= lpc32xx_hsuart_probe
,
111 .ops
= &lpc32xx_hsuart_ops
,
112 .priv_auto_alloc_size
= sizeof(struct lpc32xx_hsuart_priv
),
113 .flags
= DM_FLAG_PRE_RELOC
,