blob: 9c7c6213a589894cfd7ebedc9ed4ca0bc53fb6cc [file] [log] [blame]
Vladimir Zapolskiy8597d072012-04-19 04:33:09 +00001/*
2 * Copyright (C) 2011 Vladimir Zapolskiy <vz@mleia.com>
3 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02004 * SPDX-License-Identifier: GPL-2.0+
Vladimir Zapolskiy8597d072012-04-19 04:33:09 +00005 */
6
7#include <common.h>
8#include <asm/arch/cpu.h>
9#include <asm/arch/clk.h>
10#include <asm/arch/uart.h>
11#include <asm/io.h>
Marek Vasuta1207aa2012-09-13 16:51:02 +020012#include <serial.h>
13#include <linux/compiler.h>
Vladimir Zapolskiy8597d072012-04-19 04:33:09 +000014
15DECLARE_GLOBAL_DATA_PTR;
16
17static struct hsuart_regs *hsuart = (struct hsuart_regs *)HS_UART_BASE;
18
Marek Vasuta1207aa2012-09-13 16:51:02 +020019static void lpc32xx_serial_setbrg(void)
Vladimir Zapolskiy8597d072012-04-19 04:33:09 +000020{
21 u32 div;
22
23 /* UART rate = PERIPH_CLK / ((HSU_RATE + 1) x 14) */
24 div = (get_serial_clock() / 14 + gd->baudrate / 2) / gd->baudrate - 1;
25 if (div > 255)
26 div = 255;
27
28 writel(div, &hsuart->rate);
29}
30
Marek Vasuta1207aa2012-09-13 16:51:02 +020031static int lpc32xx_serial_getc(void)
Vladimir Zapolskiy8597d072012-04-19 04:33:09 +000032{
33 while (!(readl(&hsuart->level) & HSUART_LEVEL_RX))
34 /* NOP */;
35
36 return readl(&hsuart->rx) & HSUART_RX_DATA;
37}
38
Marek Vasuta1207aa2012-09-13 16:51:02 +020039static void lpc32xx_serial_putc(const char c)
Vladimir Zapolskiy8597d072012-04-19 04:33:09 +000040{
41 writel(c, &hsuart->tx);
42
43 /* Wait for character to be sent */
44 while (readl(&hsuart->level) & HSUART_LEVEL_TX)
45 /* NOP */;
46}
47
Marek Vasuta1207aa2012-09-13 16:51:02 +020048static int lpc32xx_serial_tstc(void)
Vladimir Zapolskiy8597d072012-04-19 04:33:09 +000049{
50 if (readl(&hsuart->level) & HSUART_LEVEL_RX)
51 return 1;
52
53 return 0;
54}
55
Marek Vasuta1207aa2012-09-13 16:51:02 +020056static int lpc32xx_serial_init(void)
Vladimir Zapolskiy8597d072012-04-19 04:33:09 +000057{
Marek Vasuta1207aa2012-09-13 16:51:02 +020058 lpc32xx_serial_setbrg();
Vladimir Zapolskiy8597d072012-04-19 04:33:09 +000059
60 /* Disable hardware RTS and CTS flow control, set up RX and TX FIFO */
61 writel(HSUART_CTRL_TMO_16 | HSUART_CTRL_HSU_OFFSET(20) |
62 HSUART_CTRL_HSU_RX_TRIG_32 | HSUART_CTRL_HSU_TX_TRIG_0,
63 &hsuart->ctrl);
Marek Vasuta1207aa2012-09-13 16:51:02 +020064 return 0;
Vladimir Zapolskiy8597d072012-04-19 04:33:09 +000065}
66
Marek Vasuta1207aa2012-09-13 16:51:02 +020067static struct serial_device lpc32xx_serial_drv = {
68 .name = "lpc32xx_serial",
69 .start = lpc32xx_serial_init,
70 .stop = NULL,
71 .setbrg = lpc32xx_serial_setbrg,
72 .putc = lpc32xx_serial_putc,
Marek Vasutd9c64492012-10-06 14:07:02 +000073 .puts = default_serial_puts,
Marek Vasuta1207aa2012-09-13 16:51:02 +020074 .getc = lpc32xx_serial_getc,
75 .tstc = lpc32xx_serial_tstc,
76};
77
78void lpc32xx_serial_initialize(void)
Vladimir Zapolskiy8597d072012-04-19 04:33:09 +000079{
Marek Vasuta1207aa2012-09-13 16:51:02 +020080 serial_register(&lpc32xx_serial_drv);
81}
Vladimir Zapolskiy8597d072012-04-19 04:33:09 +000082
Marek Vasuta1207aa2012-09-13 16:51:02 +020083__weak struct serial_device *default_serial_console(void)
84{
85 return &lpc32xx_serial_drv;
86}