blob: d87b44e90266f526a51221c3e6b5d90ddff2d62b [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>
Alexander Graf9dfeffe2018-01-25 12:05:48 +010022#include <asm/gpio.h>
Stephen Warren3917c262016-03-18 21:41:38 -060023#include <asm/io.h>
24#include <serial.h>
25#include <dm/platform_data/serial_bcm283x_mu.h>
Alexander Graf9dfeffe2018-01-25 12:05:48 +010026#include <dm/pinctrl.h>
Stephen Warren3917c262016-03-18 21:41:38 -060027#include <linux/compiler.h>
Fabian Vogt9f755f52016-09-26 14:26:44 +020028
Stephen Warren3917c262016-03-18 21:41:38 -060029struct bcm283x_mu_regs {
30 u32 io;
31 u32 iir;
32 u32 ier;
33 u32 lcr;
34 u32 mcr;
35 u32 lsr;
36 u32 msr;
37 u32 scratch;
38 u32 cntl;
39 u32 stat;
40 u32 baud;
41};
42
43#define BCM283X_MU_LCR_DATA_SIZE_8 3
44
45#define BCM283X_MU_LSR_TX_IDLE BIT(6)
46/* This actually means not full, but is named not empty in the docs */
47#define BCM283X_MU_LSR_TX_EMPTY BIT(5)
48#define BCM283X_MU_LSR_RX_READY BIT(0)
49
50struct bcm283x_mu_priv {
51 struct bcm283x_mu_regs *regs;
52};
53
Alexander Graf293b9812018-03-07 22:08:24 +010054static int bcm283x_mu_serial_getc(struct udevice *dev);
55
Stephen Warren3917c262016-03-18 21:41:38 -060056static int bcm283x_mu_serial_setbrg(struct udevice *dev, int baudrate)
57{
58 struct bcm283x_mu_serial_platdata *plat = dev_get_platdata(dev);
59 struct bcm283x_mu_priv *priv = dev_get_priv(dev);
60 struct bcm283x_mu_regs *regs = priv->regs;
61 u32 divider;
62
Alexander Graffc8523a2018-01-25 12:05:44 +010063 if (plat->skip_init)
Alexander Graf293b9812018-03-07 22:08:24 +010064 goto out;
Stephen Warren3917c262016-03-18 21:41:38 -060065
66 divider = plat->clock / (baudrate * 8);
67
68 writel(BCM283X_MU_LCR_DATA_SIZE_8, &regs->lcr);
69 writel(divider - 1, &regs->baud);
70
Alexander Graf293b9812018-03-07 22:08:24 +010071out:
72 /* Flush the RX queue - all data in there is bogus */
73 while (bcm283x_mu_serial_getc(dev) != -EAGAIN) ;
74
Stephen Warren3917c262016-03-18 21:41:38 -060075 return 0;
76}
77
78static int bcm283x_mu_serial_probe(struct udevice *dev)
79{
80 struct bcm283x_mu_serial_platdata *plat = dev_get_platdata(dev);
81 struct bcm283x_mu_priv *priv = dev_get_priv(dev);
82
83 priv->regs = (struct bcm283x_mu_regs *)plat->base;
84
85 return 0;
86}
87
88static int bcm283x_mu_serial_getc(struct udevice *dev)
89{
90 struct bcm283x_mu_priv *priv = dev_get_priv(dev);
91 struct bcm283x_mu_regs *regs = priv->regs;
92 u32 data;
93
94 /* Wait until there is data in the FIFO */
95 if (!(readl(&regs->lsr) & BCM283X_MU_LSR_RX_READY))
96 return -EAGAIN;
97
98 data = readl(&regs->io);
99
100 return (int)data;
101}
102
103static int bcm283x_mu_serial_putc(struct udevice *dev, const char data)
104{
105 struct bcm283x_mu_priv *priv = dev_get_priv(dev);
106 struct bcm283x_mu_regs *regs = priv->regs;
107
108 /* Wait until there is space in the FIFO */
109 if (!(readl(&regs->lsr) & BCM283X_MU_LSR_TX_EMPTY))
110 return -EAGAIN;
111
112 /* Send the character */
113 writel(data, &regs->io);
114
115 return 0;
116}
117
118static int bcm283x_mu_serial_pending(struct udevice *dev, bool input)
119{
120 struct bcm283x_mu_priv *priv = dev_get_priv(dev);
121 struct bcm283x_mu_regs *regs = priv->regs;
Fabian Vogtcb97ad42016-09-26 14:26:49 +0200122 unsigned int lsr;
123
Fabian Vogtcb97ad42016-09-26 14:26:49 +0200124 lsr = readl(&regs->lsr);
Stephen Warren3917c262016-03-18 21:41:38 -0600125
126 if (input) {
127 WATCHDOG_RESET();
Stephen Warrene3a46e32016-04-13 22:29:52 -0600128 return (lsr & BCM283X_MU_LSR_RX_READY) ? 1 : 0;
Stephen Warren3917c262016-03-18 21:41:38 -0600129 } else {
Stephen Warrene3a46e32016-04-13 22:29:52 -0600130 return (lsr & BCM283X_MU_LSR_TX_IDLE) ? 0 : 1;
Stephen Warren3917c262016-03-18 21:41:38 -0600131 }
132}
133
134static const struct dm_serial_ops bcm283x_mu_serial_ops = {
135 .putc = bcm283x_mu_serial_putc,
136 .pending = bcm283x_mu_serial_pending,
137 .getc = bcm283x_mu_serial_getc,
138 .setbrg = bcm283x_mu_serial_setbrg,
139};
140
Fabian Vogt9f755f52016-09-26 14:26:44 +0200141#if CONFIG_IS_ENABLED(OF_CONTROL)
142static const struct udevice_id bcm283x_mu_serial_id[] = {
143 {.compatible = "brcm,bcm2835-aux-uart"},
144 {}
145};
146
Alexander Graf9dfeffe2018-01-25 12:05:48 +0100147/*
148 * Check if this serial device is muxed
149 *
150 * The serial device will only work properly if it has been muxed to the serial
151 * pins by firmware. Check whether that happened here.
152 *
153 * @return true if serial device is muxed, false if not
154 */
155static bool bcm283x_is_serial_muxed(void)
156{
157 int serial_gpio = 15;
158 struct udevice *dev;
159
160 if (uclass_first_device(UCLASS_PINCTRL, &dev) || !dev)
161 return false;
162
163 if (pinctrl_get_gpio_mux(dev, 0, serial_gpio) != BCM2835_GPIO_ALT5)
164 return false;
165
166 return true;
167}
168
Fabian Vogt9f755f52016-09-26 14:26:44 +0200169static int bcm283x_mu_serial_ofdata_to_platdata(struct udevice *dev)
170{
171 struct bcm283x_mu_serial_platdata *plat = dev_get_platdata(dev);
172 fdt_addr_t addr;
173
Alexander Graf9dfeffe2018-01-25 12:05:48 +0100174 /* Don't spawn the device if it's not muxed */
175 if (!bcm283x_is_serial_muxed())
176 return -ENODEV;
177
Simon Glassa821c4a2017-05-17 17:18:05 -0600178 addr = devfdt_get_addr(dev);
Fabian Vogt9f755f52016-09-26 14:26:44 +0200179 if (addr == FDT_ADDR_T_NONE)
180 return -EINVAL;
181
182 plat->base = addr;
Alexander Graf80d50012018-01-25 12:05:46 +0100183 plat->clock = dev_read_u32_default(dev, "clock", 1);
Alexander Grafbceab8d2018-01-25 12:05:47 +0100184
185 /*
186 * TODO: Reinitialization doesn't always work for now, just skip
187 * init always - we know we're already initialized
188 */
189 plat->skip_init = true;
Alexander Graf80d50012018-01-25 12:05:46 +0100190
Fabian Vogt9f755f52016-09-26 14:26:44 +0200191 return 0;
192}
193#endif
194
Stephen Warren3917c262016-03-18 21:41:38 -0600195U_BOOT_DRIVER(serial_bcm283x_mu) = {
196 .name = "serial_bcm283x_mu",
197 .id = UCLASS_SERIAL,
Fabian Vogt9f755f52016-09-26 14:26:44 +0200198 .of_match = of_match_ptr(bcm283x_mu_serial_id),
199 .ofdata_to_platdata = of_match_ptr(bcm283x_mu_serial_ofdata_to_platdata),
Stephen Warren3917c262016-03-18 21:41:38 -0600200 .platdata_auto_alloc_size = sizeof(struct bcm283x_mu_serial_platdata),
201 .probe = bcm283x_mu_serial_probe,
202 .ops = &bcm283x_mu_serial_ops,
203 .flags = DM_FLAG_PRE_RELOC,
204 .priv_auto_alloc_size = sizeof(struct bcm283x_mu_priv),
205};