blob: d39a3c0494ead82c85cf194cf1816b345415fd63 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Vladimir Zapolskiy8597d072012-04-19 04:33:09 +00002/*
Vladimir Zapolskiyf20d4c02015-12-19 23:29:24 +02003 * Copyright (C) 2011-2015 Vladimir Zapolskiy <vz@mleia.com>
Vladimir Zapolskiy8597d072012-04-19 04:33:09 +00004 */
5
6#include <common.h>
Simon Glass85d65312019-12-28 10:44:58 -07007#include <clock_legacy.h>
Vladimir Zapolskiyf20d4c02015-12-19 23:29:24 +02008#include <dm.h>
Marek Vasuta1207aa2012-09-13 16:51:02 +02009#include <serial.h>
Vladimir Zapolskiyf20d4c02015-12-19 23:29:24 +020010#include <dm/platform_data/lpc32xx_hsuart.h>
11
12#include <asm/arch/uart.h>
Marek Vasuta1207aa2012-09-13 16:51:02 +020013#include <linux/compiler.h>
Vladimir Zapolskiy8597d072012-04-19 04:33:09 +000014
Vladimir Zapolskiyf20d4c02015-12-19 23:29:24 +020015struct lpc32xx_hsuart_priv {
16 struct hsuart_regs *hsuart;
17};
Vladimir Zapolskiy8597d072012-04-19 04:33:09 +000018
Vladimir Zapolskiyf20d4c02015-12-19 23:29:24 +020019static int lpc32xx_serial_setbrg(struct udevice *dev, int baudrate)
Vladimir Zapolskiy8597d072012-04-19 04:33:09 +000020{
Vladimir Zapolskiyf20d4c02015-12-19 23:29:24 +020021 struct lpc32xx_hsuart_priv *priv = dev_get_priv(dev);
22 struct hsuart_regs *hsuart = priv->hsuart;
Vladimir Zapolskiy8597d072012-04-19 04:33:09 +000023 u32 div;
24
25 /* UART rate = PERIPH_CLK / ((HSU_RATE + 1) x 14) */
Vladimir Zapolskiyf20d4c02015-12-19 23:29:24 +020026 div = (get_serial_clock() / 14 + baudrate / 2) / baudrate - 1;
Vladimir Zapolskiy8597d072012-04-19 04:33:09 +000027 if (div > 255)
28 div = 255;
29
30 writel(div, &hsuart->rate);
Vladimir Zapolskiyf20d4c02015-12-19 23:29:24 +020031
32 return 0;
Vladimir Zapolskiy8597d072012-04-19 04:33:09 +000033}
34
Vladimir Zapolskiyf20d4c02015-12-19 23:29:24 +020035static int lpc32xx_serial_getc(struct udevice *dev)
Vladimir Zapolskiy8597d072012-04-19 04:33:09 +000036{
Vladimir Zapolskiyf20d4c02015-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 Zapolskiy8597d072012-04-19 04:33:09 +000042
43 return readl(&hsuart->rx) & HSUART_RX_DATA;
44}
45
Vladimir Zapolskiyf20d4c02015-12-19 23:29:24 +020046static int lpc32xx_serial_putc(struct udevice *dev, const char c)
Vladimir Zapolskiy8597d072012-04-19 04:33:09 +000047{
Vladimir Zapolskiyf20d4c02015-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 Zapolskiyf1e43bd2013-11-30 16:47:01 +020054
Vladimir Zapolskiy8597d072012-04-19 04:33:09 +000055 writel(c, &hsuart->tx);
56
Vladimir Zapolskiyf20d4c02015-12-19 23:29:24 +020057 return 0;
Vladimir Zapolskiy8597d072012-04-19 04:33:09 +000058}
59
Vladimir Zapolskiyf20d4c02015-12-19 23:29:24 +020060static int lpc32xx_serial_pending(struct udevice *dev, bool input)
Vladimir Zapolskiy8597d072012-04-19 04:33:09 +000061{
Vladimir Zapolskiyf20d4c02015-12-19 23:29:24 +020062 struct lpc32xx_hsuart_priv *priv = dev_get_priv(dev);
63 struct hsuart_regs *hsuart = priv->hsuart;
64
65 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 }
Vladimir Zapolskiy8597d072012-04-19 04:33:09 +000072
73 return 0;
74}
75
Vladimir Zapolskiyf20d4c02015-12-19 23:29:24 +020076static int lpc32xx_serial_init(struct hsuart_regs *hsuart)
Vladimir Zapolskiy8597d072012-04-19 04:33:09 +000077{
Vladimir Zapolskiy8597d072012-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 Zapolskiyf20d4c02015-12-19 23:29:24 +020082
Marek Vasuta1207aa2012-09-13 16:51:02 +020083 return 0;
Vladimir Zapolskiy8597d072012-04-19 04:33:09 +000084}
85
Vladimir Zapolskiyf20d4c02015-12-19 23:29:24 +020086static int lpc32xx_hsuart_probe(struct udevice *dev)
87{
Simon Glassb75b15b2020-12-03 16:55:23 -070088 struct lpc32xx_hsuart_plat *plat = dev_get_plat(dev);
Vladimir Zapolskiyf20d4c02015-12-19 23:29:24 +020089 struct lpc32xx_hsuart_priv *priv = dev_get_priv(dev);
90
Simon Glass71fa5b42020-12-03 16:55:18 -070091 priv->hsuart = (struct hsuart_regs *)plat->base;
Vladimir Zapolskiyf20d4c02015-12-19 23:29:24 +020092
93 lpc32xx_serial_init(priv->hsuart);
94
95 return 0;
96}
97
98static const struct dm_serial_ops lpc32xx_hsuart_ops = {
Marek Vasuta1207aa2012-09-13 16:51:02 +020099 .setbrg = lpc32xx_serial_setbrg,
Marek Vasuta1207aa2012-09-13 16:51:02 +0200100 .getc = lpc32xx_serial_getc,
Vladimir Zapolskiyf20d4c02015-12-19 23:29:24 +0200101 .putc = lpc32xx_serial_putc,
102 .pending = lpc32xx_serial_pending,
Marek Vasuta1207aa2012-09-13 16:51:02 +0200103};
104
Vladimir Zapolskiyf20d4c02015-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,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700110 .priv_auto = sizeof(struct lpc32xx_hsuart_priv),
Vladimir Zapolskiyf20d4c02015-12-19 23:29:24 +0200111 .flags = DM_FLAG_PRE_RELOC,
112};