2018-05-06 21:58:06 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2016-03-19 03:41:38 +00:00
|
|
|
/*
|
|
|
|
* (C) Copyright 2016 Stephen Warren <swarren@wwwdotorg.org>
|
|
|
|
*
|
|
|
|
* Derived from pl01x code:
|
|
|
|
*
|
|
|
|
* (C) Copyright 2000
|
|
|
|
* Rob Taylor, Flying Pig Systems. robt@flyingpig.com.
|
|
|
|
*
|
|
|
|
* (C) Copyright 2004
|
|
|
|
* ARM Ltd.
|
|
|
|
* Philippe Robin, <philippe.robin@arm.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Simple U-Boot driver for the BCM283x mini UART */
|
|
|
|
|
|
|
|
#include <common.h>
|
|
|
|
#include <dm.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <watchdog.h>
|
2018-01-25 11:05:48 +00:00
|
|
|
#include <asm/gpio.h>
|
2016-03-19 03:41:38 +00:00
|
|
|
#include <asm/io.h>
|
|
|
|
#include <serial.h>
|
|
|
|
#include <dm/platform_data/serial_bcm283x_mu.h>
|
2018-01-25 11:05:48 +00:00
|
|
|
#include <dm/pinctrl.h>
|
2020-05-10 17:40:13 +00:00
|
|
|
#include <linux/bitops.h>
|
2016-03-19 03:41:38 +00:00
|
|
|
#include <linux/compiler.h>
|
2016-09-26 12:26:44 +00:00
|
|
|
|
2016-03-19 03:41:38 +00:00
|
|
|
struct bcm283x_mu_regs {
|
|
|
|
u32 io;
|
|
|
|
u32 iir;
|
|
|
|
u32 ier;
|
|
|
|
u32 lcr;
|
|
|
|
u32 mcr;
|
|
|
|
u32 lsr;
|
|
|
|
u32 msr;
|
|
|
|
u32 scratch;
|
|
|
|
u32 cntl;
|
|
|
|
u32 stat;
|
|
|
|
u32 baud;
|
|
|
|
};
|
|
|
|
|
|
|
|
#define BCM283X_MU_LCR_DATA_SIZE_8 3
|
|
|
|
|
|
|
|
#define BCM283X_MU_LSR_TX_IDLE BIT(6)
|
|
|
|
/* This actually means not full, but is named not empty in the docs */
|
|
|
|
#define BCM283X_MU_LSR_TX_EMPTY BIT(5)
|
|
|
|
#define BCM283X_MU_LSR_RX_READY BIT(0)
|
|
|
|
|
|
|
|
struct bcm283x_mu_priv {
|
|
|
|
struct bcm283x_mu_regs *regs;
|
|
|
|
};
|
|
|
|
|
2018-03-07 21:08:24 +00:00
|
|
|
static int bcm283x_mu_serial_getc(struct udevice *dev);
|
|
|
|
|
2016-03-19 03:41:38 +00:00
|
|
|
static int bcm283x_mu_serial_setbrg(struct udevice *dev, int baudrate)
|
|
|
|
{
|
2020-12-03 23:55:23 +00:00
|
|
|
struct bcm283x_mu_serial_plat *plat = dev_get_plat(dev);
|
2016-03-19 03:41:38 +00:00
|
|
|
struct bcm283x_mu_priv *priv = dev_get_priv(dev);
|
|
|
|
struct bcm283x_mu_regs *regs = priv->regs;
|
|
|
|
u32 divider;
|
|
|
|
|
2018-01-25 11:05:44 +00:00
|
|
|
if (plat->skip_init)
|
2018-03-07 21:08:24 +00:00
|
|
|
goto out;
|
2016-03-19 03:41:38 +00:00
|
|
|
|
|
|
|
divider = plat->clock / (baudrate * 8);
|
|
|
|
|
|
|
|
writel(BCM283X_MU_LCR_DATA_SIZE_8, ®s->lcr);
|
|
|
|
writel(divider - 1, ®s->baud);
|
|
|
|
|
2018-03-07 21:08:24 +00:00
|
|
|
out:
|
|
|
|
/* Flush the RX queue - all data in there is bogus */
|
|
|
|
while (bcm283x_mu_serial_getc(dev) != -EAGAIN) ;
|
|
|
|
|
2016-03-19 03:41:38 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int bcm283x_mu_serial_getc(struct udevice *dev)
|
|
|
|
{
|
|
|
|
struct bcm283x_mu_priv *priv = dev_get_priv(dev);
|
|
|
|
struct bcm283x_mu_regs *regs = priv->regs;
|
|
|
|
u32 data;
|
|
|
|
|
|
|
|
/* Wait until there is data in the FIFO */
|
|
|
|
if (!(readl(®s->lsr) & BCM283X_MU_LSR_RX_READY))
|
|
|
|
return -EAGAIN;
|
|
|
|
|
|
|
|
data = readl(®s->io);
|
|
|
|
|
|
|
|
return (int)data;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int bcm283x_mu_serial_putc(struct udevice *dev, const char data)
|
|
|
|
{
|
|
|
|
struct bcm283x_mu_priv *priv = dev_get_priv(dev);
|
|
|
|
struct bcm283x_mu_regs *regs = priv->regs;
|
|
|
|
|
|
|
|
/* Wait until there is space in the FIFO */
|
|
|
|
if (!(readl(®s->lsr) & BCM283X_MU_LSR_TX_EMPTY))
|
|
|
|
return -EAGAIN;
|
|
|
|
|
|
|
|
/* Send the character */
|
|
|
|
writel(data, ®s->io);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int bcm283x_mu_serial_pending(struct udevice *dev, bool input)
|
|
|
|
{
|
|
|
|
struct bcm283x_mu_priv *priv = dev_get_priv(dev);
|
|
|
|
struct bcm283x_mu_regs *regs = priv->regs;
|
2016-09-26 12:26:49 +00:00
|
|
|
unsigned int lsr;
|
|
|
|
|
|
|
|
lsr = readl(®s->lsr);
|
2016-03-19 03:41:38 +00:00
|
|
|
|
|
|
|
if (input) {
|
|
|
|
WATCHDOG_RESET();
|
2016-04-14 04:29:52 +00:00
|
|
|
return (lsr & BCM283X_MU_LSR_RX_READY) ? 1 : 0;
|
2016-03-19 03:41:38 +00:00
|
|
|
} else {
|
2016-04-14 04:29:52 +00:00
|
|
|
return (lsr & BCM283X_MU_LSR_TX_IDLE) ? 0 : 1;
|
2016-03-19 03:41:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct dm_serial_ops bcm283x_mu_serial_ops = {
|
|
|
|
.putc = bcm283x_mu_serial_putc,
|
|
|
|
.pending = bcm283x_mu_serial_pending,
|
|
|
|
.getc = bcm283x_mu_serial_getc,
|
|
|
|
.setbrg = bcm283x_mu_serial_setbrg,
|
|
|
|
};
|
|
|
|
|
2016-09-26 12:26:44 +00:00
|
|
|
#if CONFIG_IS_ENABLED(OF_CONTROL)
|
|
|
|
static const struct udevice_id bcm283x_mu_serial_id[] = {
|
|
|
|
{.compatible = "brcm,bcm2835-aux-uart"},
|
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
2018-01-25 11:05:48 +00:00
|
|
|
/*
|
|
|
|
* Check if this serial device is muxed
|
|
|
|
*
|
|
|
|
* The serial device will only work properly if it has been muxed to the serial
|
|
|
|
* pins by firmware. Check whether that happened here.
|
|
|
|
*
|
2022-01-19 17:05:50 +00:00
|
|
|
* Return: true if serial device is muxed, false if not
|
2018-01-25 11:05:48 +00:00
|
|
|
*/
|
|
|
|
static bool bcm283x_is_serial_muxed(void)
|
|
|
|
{
|
|
|
|
int serial_gpio = 15;
|
|
|
|
struct udevice *dev;
|
|
|
|
|
|
|
|
if (uclass_first_device(UCLASS_PINCTRL, &dev) || !dev)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (pinctrl_get_gpio_mux(dev, 0, serial_gpio) != BCM2835_GPIO_ALT5)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-03-23 03:15:53 +00:00
|
|
|
static int bcm283x_mu_serial_probe(struct udevice *dev)
|
2016-09-26 12:26:44 +00:00
|
|
|
{
|
2020-12-03 23:55:23 +00:00
|
|
|
struct bcm283x_mu_serial_plat *plat = dev_get_plat(dev);
|
2020-03-23 03:15:53 +00:00
|
|
|
struct bcm283x_mu_priv *priv = dev_get_priv(dev);
|
2016-09-26 12:26:44 +00:00
|
|
|
fdt_addr_t addr;
|
|
|
|
|
2018-01-25 11:05:48 +00:00
|
|
|
/* Don't spawn the device if it's not muxed */
|
|
|
|
if (!bcm283x_is_serial_muxed())
|
|
|
|
return -ENODEV;
|
|
|
|
|
2020-03-23 03:15:53 +00:00
|
|
|
/*
|
2020-12-03 23:55:21 +00:00
|
|
|
* Read the ofdata here rather than in an of_to_plat() method
|
2020-03-23 03:15:53 +00:00
|
|
|
* since we need the soc simple-bus to be probed so that the 'ranges'
|
|
|
|
* property is used.
|
|
|
|
*/
|
2020-07-17 05:36:48 +00:00
|
|
|
addr = dev_read_addr(dev);
|
2016-09-26 12:26:44 +00:00
|
|
|
if (addr == FDT_ADDR_T_NONE)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
plat->base = addr;
|
2018-01-25 11:05:46 +00:00
|
|
|
plat->clock = dev_read_u32_default(dev, "clock", 1);
|
2018-01-25 11:05:47 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* TODO: Reinitialization doesn't always work for now, just skip
|
|
|
|
* init always - we know we're already initialized
|
|
|
|
*/
|
|
|
|
plat->skip_init = true;
|
2018-01-25 11:05:46 +00:00
|
|
|
|
2020-03-23 03:15:53 +00:00
|
|
|
priv->regs = (struct bcm283x_mu_regs *)plat->base;
|
|
|
|
|
2016-09-26 12:26:44 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-03-19 03:41:38 +00:00
|
|
|
U_BOOT_DRIVER(serial_bcm283x_mu) = {
|
|
|
|
.name = "serial_bcm283x_mu",
|
|
|
|
.id = UCLASS_SERIAL,
|
2016-09-26 12:26:44 +00:00
|
|
|
.of_match = of_match_ptr(bcm283x_mu_serial_id),
|
2020-12-03 23:55:23 +00:00
|
|
|
.plat_auto = sizeof(struct bcm283x_mu_serial_plat),
|
2016-03-19 03:41:38 +00:00
|
|
|
.probe = bcm283x_mu_serial_probe,
|
|
|
|
.ops = &bcm283x_mu_serial_ops,
|
2019-11-08 13:49:48 +00:00
|
|
|
#if !CONFIG_IS_ENABLED(OF_CONTROL) || CONFIG_IS_ENABLED(OF_BOARD)
|
2016-03-19 03:41:38 +00:00
|
|
|
.flags = DM_FLAG_PRE_RELOC,
|
2018-10-24 13:36:36 +00:00
|
|
|
#endif
|
2020-12-03 23:55:17 +00:00
|
|
|
.priv_auto = sizeof(struct bcm283x_mu_priv),
|
2016-03-19 03:41:38 +00:00
|
|
|
};
|