blob: 41c26b3d931530e990c139d8eddc00cf931e6fb8 [file] [log] [blame]
Stephen Warren3917c262016-03-18 21:41:38 -06001/*
2 * (C) Copyright 2016 Stephen Warren <swarren@wwwdotorg.org>
3 *
4 * Derived from pl01x code:
5 *
6 * (C) Copyright 2000
7 * Rob Taylor, Flying Pig Systems. robt@flyingpig.com.
8 *
9 * (C) Copyright 2004
10 * ARM Ltd.
11 * Philippe Robin, <philippe.robin@arm.com>
12 *
13 * SPDX-License-Identifier: GPL-2.0+
14 */
15
16/* Simple U-Boot driver for the BCM283x mini UART */
17
18#include <common.h>
19#include <dm.h>
20#include <errno.h>
21#include <watchdog.h>
22#include <asm/io.h>
23#include <serial.h>
24#include <dm/platform_data/serial_bcm283x_mu.h>
25#include <linux/compiler.h>
26#include <fdtdec.h>
27
Fabian Vogt9f755f52016-09-26 14:26:44 +020028DECLARE_GLOBAL_DATA_PTR;
29
Stephen Warren3917c262016-03-18 21:41:38 -060030struct bcm283x_mu_regs {
31 u32 io;
32 u32 iir;
33 u32 ier;
34 u32 lcr;
35 u32 mcr;
36 u32 lsr;
37 u32 msr;
38 u32 scratch;
39 u32 cntl;
40 u32 stat;
41 u32 baud;
42};
43
44#define BCM283X_MU_LCR_DATA_SIZE_8 3
45
46#define BCM283X_MU_LSR_TX_IDLE BIT(6)
47/* This actually means not full, but is named not empty in the docs */
48#define BCM283X_MU_LSR_TX_EMPTY BIT(5)
49#define BCM283X_MU_LSR_RX_READY BIT(0)
50
51struct bcm283x_mu_priv {
52 struct bcm283x_mu_regs *regs;
53};
54
55static int bcm283x_mu_serial_setbrg(struct udevice *dev, int baudrate)
56{
57 struct bcm283x_mu_serial_platdata *plat = dev_get_platdata(dev);
58 struct bcm283x_mu_priv *priv = dev_get_priv(dev);
59 struct bcm283x_mu_regs *regs = priv->regs;
60 u32 divider;
61
Fabian Vogtcb97ad42016-09-26 14:26:49 +020062 if (plat->disabled || plat->skip_init)
Stephen Warren3917c262016-03-18 21:41:38 -060063 return 0;
64
65 divider = plat->clock / (baudrate * 8);
66
67 writel(BCM283X_MU_LCR_DATA_SIZE_8, &regs->lcr);
68 writel(divider - 1, &regs->baud);
69
70 return 0;
71}
72
73static int bcm283x_mu_serial_probe(struct udevice *dev)
74{
75 struct bcm283x_mu_serial_platdata *plat = dev_get_platdata(dev);
76 struct bcm283x_mu_priv *priv = dev_get_priv(dev);
77
Alexander Graf601147b2016-08-15 17:48:51 +020078 if (plat->disabled)
79 return -ENODEV;
80
Stephen Warren3917c262016-03-18 21:41:38 -060081 priv->regs = (struct bcm283x_mu_regs *)plat->base;
82
83 return 0;
84}
85
86static int bcm283x_mu_serial_getc(struct udevice *dev)
87{
Fabian Vogtcb97ad42016-09-26 14:26:49 +020088 struct bcm283x_mu_serial_platdata *plat = dev_get_platdata(dev);
Stephen Warren3917c262016-03-18 21:41:38 -060089 struct bcm283x_mu_priv *priv = dev_get_priv(dev);
90 struct bcm283x_mu_regs *regs = priv->regs;
91 u32 data;
92
Fabian Vogtcb97ad42016-09-26 14:26:49 +020093 if (plat->disabled)
94 return -EAGAIN;
95
Stephen Warren3917c262016-03-18 21:41:38 -060096 /* Wait until there is data in the FIFO */
97 if (!(readl(&regs->lsr) & BCM283X_MU_LSR_RX_READY))
98 return -EAGAIN;
99
100 data = readl(&regs->io);
101
102 return (int)data;
103}
104
105static int bcm283x_mu_serial_putc(struct udevice *dev, const char data)
106{
Fabian Vogtcb97ad42016-09-26 14:26:49 +0200107 struct bcm283x_mu_serial_platdata *plat = dev_get_platdata(dev);
Stephen Warren3917c262016-03-18 21:41:38 -0600108 struct bcm283x_mu_priv *priv = dev_get_priv(dev);
109 struct bcm283x_mu_regs *regs = priv->regs;
110
Fabian Vogtcb97ad42016-09-26 14:26:49 +0200111 if (plat->disabled)
112 return 0;
113
Stephen Warren3917c262016-03-18 21:41:38 -0600114 /* Wait until there is space in the FIFO */
115 if (!(readl(&regs->lsr) & BCM283X_MU_LSR_TX_EMPTY))
116 return -EAGAIN;
117
118 /* Send the character */
119 writel(data, &regs->io);
120
121 return 0;
122}
123
124static int bcm283x_mu_serial_pending(struct udevice *dev, bool input)
125{
Fabian Vogtcb97ad42016-09-26 14:26:49 +0200126 struct bcm283x_mu_serial_platdata *plat = dev_get_platdata(dev);
Stephen Warren3917c262016-03-18 21:41:38 -0600127 struct bcm283x_mu_priv *priv = dev_get_priv(dev);
128 struct bcm283x_mu_regs *regs = priv->regs;
Fabian Vogtcb97ad42016-09-26 14:26:49 +0200129 unsigned int lsr;
130
131 if (plat->disabled)
132 return 0;
133
134 lsr = readl(&regs->lsr);
Stephen Warren3917c262016-03-18 21:41:38 -0600135
136 if (input) {
137 WATCHDOG_RESET();
Stephen Warrene3a46e32016-04-13 22:29:52 -0600138 return (lsr & BCM283X_MU_LSR_RX_READY) ? 1 : 0;
Stephen Warren3917c262016-03-18 21:41:38 -0600139 } else {
Stephen Warrene3a46e32016-04-13 22:29:52 -0600140 return (lsr & BCM283X_MU_LSR_TX_IDLE) ? 0 : 1;
Stephen Warren3917c262016-03-18 21:41:38 -0600141 }
142}
143
144static const struct dm_serial_ops bcm283x_mu_serial_ops = {
145 .putc = bcm283x_mu_serial_putc,
146 .pending = bcm283x_mu_serial_pending,
147 .getc = bcm283x_mu_serial_getc,
148 .setbrg = bcm283x_mu_serial_setbrg,
149};
150
Fabian Vogt9f755f52016-09-26 14:26:44 +0200151#if CONFIG_IS_ENABLED(OF_CONTROL)
152static const struct udevice_id bcm283x_mu_serial_id[] = {
153 {.compatible = "brcm,bcm2835-aux-uart"},
154 {}
155};
156
157static int bcm283x_mu_serial_ofdata_to_platdata(struct udevice *dev)
158{
159 struct bcm283x_mu_serial_platdata *plat = dev_get_platdata(dev);
160 fdt_addr_t addr;
161
Simon Glassa821c4a2017-05-17 17:18:05 -0600162 addr = devfdt_get_addr(dev);
Fabian Vogt9f755f52016-09-26 14:26:44 +0200163 if (addr == FDT_ADDR_T_NONE)
164 return -EINVAL;
165
166 plat->base = addr;
Simon Glasse160f7d2017-01-17 16:52:55 -0700167 plat->clock = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev), "clock",
168 1);
169 plat->skip_init = fdtdec_get_bool(gd->fdt_blob, dev_of_offset(dev),
Fabian Vogt9f755f52016-09-26 14:26:44 +0200170 "skip-init");
171 plat->disabled = false;
172 return 0;
173}
174#endif
175
Stephen Warren3917c262016-03-18 21:41:38 -0600176U_BOOT_DRIVER(serial_bcm283x_mu) = {
177 .name = "serial_bcm283x_mu",
178 .id = UCLASS_SERIAL,
Fabian Vogt9f755f52016-09-26 14:26:44 +0200179 .of_match = of_match_ptr(bcm283x_mu_serial_id),
180 .ofdata_to_platdata = of_match_ptr(bcm283x_mu_serial_ofdata_to_platdata),
Stephen Warren3917c262016-03-18 21:41:38 -0600181 .platdata_auto_alloc_size = sizeof(struct bcm283x_mu_serial_platdata),
182 .probe = bcm283x_mu_serial_probe,
183 .ops = &bcm283x_mu_serial_ops,
184 .flags = DM_FLAG_PRE_RELOC,
185 .priv_auto_alloc_size = sizeof(struct bcm283x_mu_priv),
186};