2 * Copyright (C) 2011 Vladimir Zapolskiy <vz@mleia.com>
4 * SPDX-License-Identifier: GPL-2.0+
8 #include <asm/arch/cpu.h>
9 #include <asm/arch/clk.h>
10 #include <asm/arch/uart.h>
13 #include <linux/compiler.h>
15 DECLARE_GLOBAL_DATA_PTR;
17 static struct hsuart_regs *hsuart = (struct hsuart_regs *)HS_UART_BASE;
19 static void lpc32xx_serial_setbrg(void)
23 /* UART rate = PERIPH_CLK / ((HSU_RATE + 1) x 14) */
24 div = (get_serial_clock() / 14 + gd->baudrate / 2) / gd->baudrate - 1;
28 writel(div, &hsuart->rate);
31 static int lpc32xx_serial_getc(void)
33 while (!(readl(&hsuart->level) & HSUART_LEVEL_RX))
36 return readl(&hsuart->rx) & HSUART_RX_DATA;
39 static void lpc32xx_serial_putc(const char c)
44 writel(c, &hsuart->tx);
46 /* Wait for character to be sent */
47 while (readl(&hsuart->level) & HSUART_LEVEL_TX)
51 static int lpc32xx_serial_tstc(void)
53 if (readl(&hsuart->level) & HSUART_LEVEL_RX)
59 static int lpc32xx_serial_init(void)
61 lpc32xx_serial_setbrg();
63 /* Disable hardware RTS and CTS flow control, set up RX and TX FIFO */
64 writel(HSUART_CTRL_TMO_16 | HSUART_CTRL_HSU_OFFSET(20) |
65 HSUART_CTRL_HSU_RX_TRIG_32 | HSUART_CTRL_HSU_TX_TRIG_0,
70 static struct serial_device lpc32xx_serial_drv = {
71 .name = "lpc32xx_serial",
72 .start = lpc32xx_serial_init,
74 .setbrg = lpc32xx_serial_setbrg,
75 .putc = lpc32xx_serial_putc,
76 .puts = default_serial_puts,
77 .getc = lpc32xx_serial_getc,
78 .tstc = lpc32xx_serial_tstc,
81 void lpc32xx_serial_initialize(void)
83 serial_register(&lpc32xx_serial_drv);
86 __weak struct serial_device *default_serial_console(void)
88 return &lpc32xx_serial_drv;