blob: 8b0fd254b1b2aaf70244d9b10ca44c21a84e90f2 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Vladimir Zapolskiycc35fdb2012-04-19 04:33:09 +00002/*
Vladimir Zapolskiyf21069e2015-12-19 23:29:24 +02003 * Copyright (C) 2011-2015 Vladimir Zapolskiy <vz@mleia.com>
Vladimir Zapolskiycc35fdb2012-04-19 04:33:09 +00004 */
5
6#include <common.h>
Vladimir Zapolskiyf21069e2015-12-19 23:29:24 +02007#include <dm.h>
Marek Vasute503f902012-09-13 16:51:02 +02008#include <serial.h>
Vladimir Zapolskiyf21069e2015-12-19 23:29:24 +02009#include <dm/platform_data/lpc32xx_hsuart.h>
10
11#include <asm/arch/uart.h>
Marek Vasute503f902012-09-13 16:51:02 +020012#include <linux/compiler.h>
Vladimir Zapolskiycc35fdb2012-04-19 04:33:09 +000013
Vladimir Zapolskiyf21069e2015-12-19 23:29:24 +020014struct lpc32xx_hsuart_priv {
15 struct hsuart_regs *hsuart;
16};
Vladimir Zapolskiycc35fdb2012-04-19 04:33:09 +000017
Vladimir Zapolskiyf21069e2015-12-19 23:29:24 +020018static int lpc32xx_serial_setbrg(struct udevice *dev, int baudrate)
Vladimir Zapolskiycc35fdb2012-04-19 04:33:09 +000019{
Vladimir Zapolskiyf21069e2015-12-19 23:29:24 +020020 struct lpc32xx_hsuart_priv *priv = dev_get_priv(dev);
21 struct hsuart_regs *hsuart = priv->hsuart;
Vladimir Zapolskiycc35fdb2012-04-19 04:33:09 +000022 u32 div;
23
24 /* UART rate = PERIPH_CLK / ((HSU_RATE + 1) x 14) */
Vladimir Zapolskiyf21069e2015-12-19 23:29:24 +020025 div = (get_serial_clock() / 14 + baudrate / 2) / baudrate - 1;
Vladimir Zapolskiycc35fdb2012-04-19 04:33:09 +000026 if (div > 255)
27 div = 255;
28
29 writel(div, &hsuart->rate);
Vladimir Zapolskiyf21069e2015-12-19 23:29:24 +020030
31 return 0;
Vladimir Zapolskiycc35fdb2012-04-19 04:33:09 +000032}
33
Vladimir Zapolskiyf21069e2015-12-19 23:29:24 +020034static int lpc32xx_serial_getc(struct udevice *dev)
Vladimir Zapolskiycc35fdb2012-04-19 04:33:09 +000035{
Vladimir Zapolskiyf21069e2015-12-19 23:29:24 +020036 struct lpc32xx_hsuart_priv *priv = dev_get_priv(dev);
37 struct hsuart_regs *hsuart = priv->hsuart;
38
39 if (!(readl(&hsuart->level) & HSUART_LEVEL_RX))
40 return -EAGAIN;
Vladimir Zapolskiycc35fdb2012-04-19 04:33:09 +000041
42 return readl(&hsuart->rx) & HSUART_RX_DATA;
43}
44
Vladimir Zapolskiyf21069e2015-12-19 23:29:24 +020045static int lpc32xx_serial_putc(struct udevice *dev, const char c)
Vladimir Zapolskiycc35fdb2012-04-19 04:33:09 +000046{
Vladimir Zapolskiyf21069e2015-12-19 23:29:24 +020047 struct lpc32xx_hsuart_priv *priv = dev_get_priv(dev);
48 struct hsuart_regs *hsuart = priv->hsuart;
49
50 /* Wait for empty FIFO */
51 if (readl(&hsuart->level) & HSUART_LEVEL_TX)
52 return -EAGAIN;
Vladimir Zapolskiy5deccaf2013-11-30 16:47:01 +020053
Vladimir Zapolskiycc35fdb2012-04-19 04:33:09 +000054 writel(c, &hsuart->tx);
55
Vladimir Zapolskiycc35fdb2012-04-19 04:33:09 +000056 return 0;
57}
58
Vladimir Zapolskiyf21069e2015-12-19 23:29:24 +020059static int lpc32xx_serial_pending(struct udevice *dev, bool input)
Vladimir Zapolskiycc35fdb2012-04-19 04:33:09 +000060{
Vladimir Zapolskiyf21069e2015-12-19 23:29:24 +020061 struct lpc32xx_hsuart_priv *priv = dev_get_priv(dev);
62 struct hsuart_regs *hsuart = priv->hsuart;
Vladimir Zapolskiycc35fdb2012-04-19 04:33:09 +000063
Vladimir Zapolskiyf21069e2015-12-19 23:29:24 +020064 if (input) {
65 if (readl(&hsuart->level) & HSUART_LEVEL_RX)
66 return 1;
67 } else {
68 if (readl(&hsuart->level) & HSUART_LEVEL_TX)
69 return 1;
70 }
71
72 return 0;
73}
74
75static int lpc32xx_serial_init(struct hsuart_regs *hsuart)
76{
Vladimir Zapolskiycc35fdb2012-04-19 04:33:09 +000077 /* 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,
80 &hsuart->ctrl);
Vladimir Zapolskiyf21069e2015-12-19 23:29:24 +020081
Marek Vasute503f902012-09-13 16:51:02 +020082 return 0;
Vladimir Zapolskiycc35fdb2012-04-19 04:33:09 +000083}
84
Vladimir Zapolskiyf21069e2015-12-19 23:29:24 +020085static int lpc32xx_hsuart_probe(struct udevice *dev)
86{
87 struct lpc32xx_hsuart_platdata *platdata = dev_get_platdata(dev);
88 struct lpc32xx_hsuart_priv *priv = dev_get_priv(dev);
89
90 priv->hsuart = (struct hsuart_regs *)platdata->base;
91
92 lpc32xx_serial_init(priv->hsuart);
93
94 return 0;
95}
96
97static const struct dm_serial_ops lpc32xx_hsuart_ops = {
Marek Vasute503f902012-09-13 16:51:02 +020098 .setbrg = lpc32xx_serial_setbrg,
Marek Vasute503f902012-09-13 16:51:02 +020099 .getc = lpc32xx_serial_getc,
Vladimir Zapolskiyf21069e2015-12-19 23:29:24 +0200100 .putc = lpc32xx_serial_putc,
101 .pending = lpc32xx_serial_pending,
Marek Vasute503f902012-09-13 16:51:02 +0200102};
103
Vladimir Zapolskiyf21069e2015-12-19 23:29:24 +0200104U_BOOT_DRIVER(lpc32xx_hsuart) = {
105 .name = "lpc32xx_hsuart",
106 .id = UCLASS_SERIAL,
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,
111};