blob: febb5ceea2a53bcce4342a86e48407400de583f2 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Stephen Warrenf84f9bf2016-03-18 21:41:38 -06002/*
3 * (C) Copyright 2016 Stephen Warren <swarren@wwwdotorg.org>
4 *
5 * Derived from pl01x code:
6 *
7 * (C) Copyright 2000
8 * Rob Taylor, Flying Pig Systems. robt@flyingpig.com.
9 *
10 * (C) Copyright 2004
11 * ARM Ltd.
12 * Philippe Robin, <philippe.robin@arm.com>
Stephen Warrenf84f9bf2016-03-18 21:41:38 -060013 */
14
15/* Simple U-Boot driver for the BCM283x mini UART */
16
17#include <common.h>
18#include <dm.h>
19#include <errno.h>
20#include <watchdog.h>
Alexander Grafff9fd762018-01-25 12:05:48 +010021#include <asm/gpio.h>
Stephen Warrenf84f9bf2016-03-18 21:41:38 -060022#include <asm/io.h>
23#include <serial.h>
24#include <dm/platform_data/serial_bcm283x_mu.h>
Alexander Grafff9fd762018-01-25 12:05:48 +010025#include <dm/pinctrl.h>
Stephen Warrenf84f9bf2016-03-18 21:41:38 -060026#include <linux/compiler.h>
Fabian Vogtcfb76fc2016-09-26 14:26:44 +020027
Stephen Warrenf84f9bf2016-03-18 21:41:38 -060028struct bcm283x_mu_regs {
29 u32 io;
30 u32 iir;
31 u32 ier;
32 u32 lcr;
33 u32 mcr;
34 u32 lsr;
35 u32 msr;
36 u32 scratch;
37 u32 cntl;
38 u32 stat;
39 u32 baud;
40};
41
42#define BCM283X_MU_LCR_DATA_SIZE_8 3
43
44#define BCM283X_MU_LSR_TX_IDLE BIT(6)
45/* This actually means not full, but is named not empty in the docs */
46#define BCM283X_MU_LSR_TX_EMPTY BIT(5)
47#define BCM283X_MU_LSR_RX_READY BIT(0)
48
49struct bcm283x_mu_priv {
50 struct bcm283x_mu_regs *regs;
51};
52
Alexander Graf15b79ff2018-03-07 22:08:24 +010053static int bcm283x_mu_serial_getc(struct udevice *dev);
54
Stephen Warrenf84f9bf2016-03-18 21:41:38 -060055static 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
Alexander Graff7c26282018-01-25 12:05:44 +010062 if (plat->skip_init)
Alexander Graf15b79ff2018-03-07 22:08:24 +010063 goto out;
Stephen Warrenf84f9bf2016-03-18 21:41:38 -060064
65 divider = plat->clock / (baudrate * 8);
66
67 writel(BCM283X_MU_LCR_DATA_SIZE_8, &regs->lcr);
68 writel(divider - 1, &regs->baud);
69
Alexander Graf15b79ff2018-03-07 22:08:24 +010070out:
71 /* Flush the RX queue - all data in there is bogus */
72 while (bcm283x_mu_serial_getc(dev) != -EAGAIN) ;
73
Stephen Warrenf84f9bf2016-03-18 21:41:38 -060074 return 0;
75}
76
Stephen Warrenf84f9bf2016-03-18 21:41:38 -060077static int bcm283x_mu_serial_getc(struct udevice *dev)
78{
79 struct bcm283x_mu_priv *priv = dev_get_priv(dev);
80 struct bcm283x_mu_regs *regs = priv->regs;
81 u32 data;
82
83 /* Wait until there is data in the FIFO */
84 if (!(readl(&regs->lsr) & BCM283X_MU_LSR_RX_READY))
85 return -EAGAIN;
86
87 data = readl(&regs->io);
88
89 return (int)data;
90}
91
92static int bcm283x_mu_serial_putc(struct udevice *dev, const char data)
93{
94 struct bcm283x_mu_priv *priv = dev_get_priv(dev);
95 struct bcm283x_mu_regs *regs = priv->regs;
96
97 /* Wait until there is space in the FIFO */
98 if (!(readl(&regs->lsr) & BCM283X_MU_LSR_TX_EMPTY))
99 return -EAGAIN;
100
101 /* Send the character */
102 writel(data, &regs->io);
103
104 return 0;
105}
106
107static int bcm283x_mu_serial_pending(struct udevice *dev, bool input)
108{
109 struct bcm283x_mu_priv *priv = dev_get_priv(dev);
110 struct bcm283x_mu_regs *regs = priv->regs;
Fabian Vogt0f116612016-09-26 14:26:49 +0200111 unsigned int lsr;
112
Fabian Vogt0f116612016-09-26 14:26:49 +0200113 lsr = readl(&regs->lsr);
Stephen Warrenf84f9bf2016-03-18 21:41:38 -0600114
115 if (input) {
116 WATCHDOG_RESET();
Stephen Warren3a036982016-04-13 22:29:52 -0600117 return (lsr & BCM283X_MU_LSR_RX_READY) ? 1 : 0;
Stephen Warrenf84f9bf2016-03-18 21:41:38 -0600118 } else {
Stephen Warren3a036982016-04-13 22:29:52 -0600119 return (lsr & BCM283X_MU_LSR_TX_IDLE) ? 0 : 1;
Stephen Warrenf84f9bf2016-03-18 21:41:38 -0600120 }
121}
122
123static const struct dm_serial_ops bcm283x_mu_serial_ops = {
124 .putc = bcm283x_mu_serial_putc,
125 .pending = bcm283x_mu_serial_pending,
126 .getc = bcm283x_mu_serial_getc,
127 .setbrg = bcm283x_mu_serial_setbrg,
128};
129
Fabian Vogtcfb76fc2016-09-26 14:26:44 +0200130#if CONFIG_IS_ENABLED(OF_CONTROL)
131static const struct udevice_id bcm283x_mu_serial_id[] = {
132 {.compatible = "brcm,bcm2835-aux-uart"},
133 {}
134};
135
Alexander Grafff9fd762018-01-25 12:05:48 +0100136/*
137 * Check if this serial device is muxed
138 *
139 * The serial device will only work properly if it has been muxed to the serial
140 * pins by firmware. Check whether that happened here.
141 *
142 * @return true if serial device is muxed, false if not
143 */
144static bool bcm283x_is_serial_muxed(void)
145{
146 int serial_gpio = 15;
147 struct udevice *dev;
148
149 if (uclass_first_device(UCLASS_PINCTRL, &dev) || !dev)
150 return false;
151
152 if (pinctrl_get_gpio_mux(dev, 0, serial_gpio) != BCM2835_GPIO_ALT5)
153 return false;
154
155 return true;
156}
157
Simon Glass910c58f2020-03-22 21:15:53 -0600158static int bcm283x_mu_serial_probe(struct udevice *dev)
Fabian Vogtcfb76fc2016-09-26 14:26:44 +0200159{
160 struct bcm283x_mu_serial_platdata *plat = dev_get_platdata(dev);
Simon Glass910c58f2020-03-22 21:15:53 -0600161 struct bcm283x_mu_priv *priv = dev_get_priv(dev);
Fabian Vogtcfb76fc2016-09-26 14:26:44 +0200162 fdt_addr_t addr;
163
Alexander Grafff9fd762018-01-25 12:05:48 +0100164 /* Don't spawn the device if it's not muxed */
165 if (!bcm283x_is_serial_muxed())
166 return -ENODEV;
167
Simon Glass910c58f2020-03-22 21:15:53 -0600168 /*
169 * Read the ofdata here rather than in an ofdata_to_platdata() method
170 * since we need the soc simple-bus to be probed so that the 'ranges'
171 * property is used.
172 */
Simon Glassba1dea42017-05-17 17:18:05 -0600173 addr = devfdt_get_addr(dev);
Fabian Vogtcfb76fc2016-09-26 14:26:44 +0200174 if (addr == FDT_ADDR_T_NONE)
175 return -EINVAL;
176
177 plat->base = addr;
Alexander Graf35b31e02018-01-25 12:05:46 +0100178 plat->clock = dev_read_u32_default(dev, "clock", 1);
Alexander Grafc3883e02018-01-25 12:05:47 +0100179
180 /*
181 * TODO: Reinitialization doesn't always work for now, just skip
182 * init always - we know we're already initialized
183 */
184 plat->skip_init = true;
Alexander Graf35b31e02018-01-25 12:05:46 +0100185
Simon Glass910c58f2020-03-22 21:15:53 -0600186 priv->regs = (struct bcm283x_mu_regs *)plat->base;
187
Fabian Vogtcfb76fc2016-09-26 14:26:44 +0200188 return 0;
189}
190#endif
191
Stephen Warrenf84f9bf2016-03-18 21:41:38 -0600192U_BOOT_DRIVER(serial_bcm283x_mu) = {
193 .name = "serial_bcm283x_mu",
194 .id = UCLASS_SERIAL,
Fabian Vogtcfb76fc2016-09-26 14:26:44 +0200195 .of_match = of_match_ptr(bcm283x_mu_serial_id),
Stephen Warrenf84f9bf2016-03-18 21:41:38 -0600196 .platdata_auto_alloc_size = sizeof(struct bcm283x_mu_serial_platdata),
197 .probe = bcm283x_mu_serial_probe,
198 .ops = &bcm283x_mu_serial_ops,
Matthias Brugger501b6842019-11-08 14:49:48 +0100199#if !CONFIG_IS_ENABLED(OF_CONTROL) || CONFIG_IS_ENABLED(OF_BOARD)
Stephen Warrenf84f9bf2016-03-18 21:41:38 -0600200 .flags = DM_FLAG_PRE_RELOC,
Bin Mengbdb33d82018-10-24 06:36:36 -0700201#endif
Stephen Warrenf84f9bf2016-03-18 21:41:38 -0600202 .priv_auto_alloc_size = sizeof(struct bcm283x_mu_priv),
203};