blob: 7d31c6d4a145b4f3fa104fed3a1d408155e641cb [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>
Simon Glassd96c2602019-12-28 10:44:58 -07007#include <clock_legacy.h>
Vladimir Zapolskiyf21069e2015-12-19 23:29:24 +02008#include <dm.h>
Marek Vasute503f902012-09-13 16:51:02 +02009#include <serial.h>
Vladimir Zapolskiyf21069e2015-12-19 23:29:24 +020010#include <dm/platform_data/lpc32xx_hsuart.h>
11
12#include <asm/arch/uart.h>
Marek Vasute503f902012-09-13 16:51:02 +020013#include <linux/compiler.h>
Vladimir Zapolskiycc35fdb2012-04-19 04:33:09 +000014
Vladimir Zapolskiyf21069e2015-12-19 23:29:24 +020015struct lpc32xx_hsuart_priv {
16 struct hsuart_regs *hsuart;
17};
Vladimir Zapolskiycc35fdb2012-04-19 04:33:09 +000018
Vladimir Zapolskiyf21069e2015-12-19 23:29:24 +020019static int lpc32xx_serial_setbrg(struct udevice *dev, int baudrate)
Vladimir Zapolskiycc35fdb2012-04-19 04:33:09 +000020{
Vladimir Zapolskiyf21069e2015-12-19 23:29:24 +020021 struct lpc32xx_hsuart_priv *priv = dev_get_priv(dev);
22 struct hsuart_regs *hsuart = priv->hsuart;
Vladimir Zapolskiycc35fdb2012-04-19 04:33:09 +000023 u32 div;
24
25 /* UART rate = PERIPH_CLK / ((HSU_RATE + 1) x 14) */
Vladimir Zapolskiyf21069e2015-12-19 23:29:24 +020026 div = (get_serial_clock() / 14 + baudrate / 2) / baudrate - 1;
Vladimir Zapolskiycc35fdb2012-04-19 04:33:09 +000027 if (div > 255)
28 div = 255;
29
30 writel(div, &hsuart->rate);
Vladimir Zapolskiyf21069e2015-12-19 23:29:24 +020031
32 return 0;
Vladimir Zapolskiycc35fdb2012-04-19 04:33:09 +000033}
34
Vladimir Zapolskiyf21069e2015-12-19 23:29:24 +020035static int lpc32xx_serial_getc(struct udevice *dev)
Vladimir Zapolskiycc35fdb2012-04-19 04:33:09 +000036{
Vladimir Zapolskiyf21069e2015-12-19 23:29:24 +020037 struct lpc32xx_hsuart_priv *priv = dev_get_priv(dev);
38 struct hsuart_regs *hsuart = priv->hsuart;
39
40 if (!(readl(&hsuart->level) & HSUART_LEVEL_RX))
41 return -EAGAIN;
Vladimir Zapolskiycc35fdb2012-04-19 04:33:09 +000042
43 return readl(&hsuart->rx) & HSUART_RX_DATA;
44}
45
Vladimir Zapolskiyf21069e2015-12-19 23:29:24 +020046static int lpc32xx_serial_putc(struct udevice *dev, const char c)
Vladimir Zapolskiycc35fdb2012-04-19 04:33:09 +000047{
Vladimir Zapolskiyf21069e2015-12-19 23:29:24 +020048 struct lpc32xx_hsuart_priv *priv = dev_get_priv(dev);
49 struct hsuart_regs *hsuart = priv->hsuart;
50
51 /* Wait for empty FIFO */
52 if (readl(&hsuart->level) & HSUART_LEVEL_TX)
53 return -EAGAIN;
Vladimir Zapolskiy5deccaf2013-11-30 16:47:01 +020054
Vladimir Zapolskiycc35fdb2012-04-19 04:33:09 +000055 writel(c, &hsuart->tx);
56
Vladimir Zapolskiycc35fdb2012-04-19 04:33:09 +000057 return 0;
58}
59
Vladimir Zapolskiyf21069e2015-12-19 23:29:24 +020060static int lpc32xx_serial_pending(struct udevice *dev, bool input)
Vladimir Zapolskiycc35fdb2012-04-19 04:33:09 +000061{
Vladimir Zapolskiyf21069e2015-12-19 23:29:24 +020062 struct lpc32xx_hsuart_priv *priv = dev_get_priv(dev);
63 struct hsuart_regs *hsuart = priv->hsuart;
Vladimir Zapolskiycc35fdb2012-04-19 04:33:09 +000064
Vladimir Zapolskiyf21069e2015-12-19 23:29:24 +020065 if (input) {
66 if (readl(&hsuart->level) & HSUART_LEVEL_RX)
67 return 1;
68 } else {
69 if (readl(&hsuart->level) & HSUART_LEVEL_TX)
70 return 1;
71 }
72
73 return 0;
74}
75
76static int lpc32xx_serial_init(struct hsuart_regs *hsuart)
77{
Vladimir Zapolskiycc35fdb2012-04-19 04:33:09 +000078 /* Disable hardware RTS and CTS flow control, set up RX and TX FIFO */
79 writel(HSUART_CTRL_TMO_16 | HSUART_CTRL_HSU_OFFSET(20) |
80 HSUART_CTRL_HSU_RX_TRIG_32 | HSUART_CTRL_HSU_TX_TRIG_0,
81 &hsuart->ctrl);
Vladimir Zapolskiyf21069e2015-12-19 23:29:24 +020082
Marek Vasute503f902012-09-13 16:51:02 +020083 return 0;
Vladimir Zapolskiycc35fdb2012-04-19 04:33:09 +000084}
85
Vladimir Zapolskiyf21069e2015-12-19 23:29:24 +020086static int lpc32xx_hsuart_probe(struct udevice *dev)
87{
88 struct lpc32xx_hsuart_platdata *platdata = dev_get_platdata(dev);
89 struct lpc32xx_hsuart_priv *priv = dev_get_priv(dev);
90
91 priv->hsuart = (struct hsuart_regs *)platdata->base;
92
93 lpc32xx_serial_init(priv->hsuart);
94
95 return 0;
96}
97
98static const struct dm_serial_ops lpc32xx_hsuart_ops = {
Marek Vasute503f902012-09-13 16:51:02 +020099 .setbrg = lpc32xx_serial_setbrg,
Marek Vasute503f902012-09-13 16:51:02 +0200100 .getc = lpc32xx_serial_getc,
Vladimir Zapolskiyf21069e2015-12-19 23:29:24 +0200101 .putc = lpc32xx_serial_putc,
102 .pending = lpc32xx_serial_pending,
Marek Vasute503f902012-09-13 16:51:02 +0200103};
104
Vladimir Zapolskiyf21069e2015-12-19 23:29:24 +0200105U_BOOT_DRIVER(lpc32xx_hsuart) = {
106 .name = "lpc32xx_hsuart",
107 .id = UCLASS_SERIAL,
108 .probe = lpc32xx_hsuart_probe,
109 .ops = &lpc32xx_hsuart_ops,
110 .priv_auto_alloc_size = sizeof(struct lpc32xx_hsuart_priv),
111 .flags = DM_FLAG_PRE_RELOC,
112};