blob: 1f87f0cb28ad74cb13b5b8089b9f2e62e7ca3131 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Stephen Warren3917c262016-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 Warren3917c262016-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 Graf9dfeffe2018-01-25 12:05:48 +010021#include <asm/gpio.h>
Stephen Warren3917c262016-03-18 21:41:38 -060022#include <asm/io.h>
23#include <serial.h>
24#include <dm/platform_data/serial_bcm283x_mu.h>
Alexander Graf9dfeffe2018-01-25 12:05:48 +010025#include <dm/pinctrl.h>
Stephen Warren3917c262016-03-18 21:41:38 -060026#include <linux/compiler.h>
Fabian Vogt9f755f52016-09-26 14:26:44 +020027
Stephen Warren3917c262016-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 Graf293b9812018-03-07 22:08:24 +010053static int bcm283x_mu_serial_getc(struct udevice *dev);
54
Stephen Warren3917c262016-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 Graffc8523a2018-01-25 12:05:44 +010062 if (plat->skip_init)
Alexander Graf293b9812018-03-07 22:08:24 +010063 goto out;
Stephen Warren3917c262016-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 Graf293b9812018-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 Warren3917c262016-03-18 21:41:38 -060074 return 0;
75}
76
77static int bcm283x_mu_serial_probe(struct udevice *dev)
78{
79 struct bcm283x_mu_serial_platdata *plat = dev_get_platdata(dev);
80 struct bcm283x_mu_priv *priv = dev_get_priv(dev);
81
82 priv->regs = (struct bcm283x_mu_regs *)plat->base;
83
84 return 0;
85}
86
87static int bcm283x_mu_serial_getc(struct udevice *dev)
88{
89 struct bcm283x_mu_priv *priv = dev_get_priv(dev);
90 struct bcm283x_mu_regs *regs = priv->regs;
91 u32 data;
92
93 /* Wait until there is data in the FIFO */
94 if (!(readl(&regs->lsr) & BCM283X_MU_LSR_RX_READY))
95 return -EAGAIN;
96
97 data = readl(&regs->io);
98
99 return (int)data;
100}
101
102static int bcm283x_mu_serial_putc(struct udevice *dev, const char data)
103{
104 struct bcm283x_mu_priv *priv = dev_get_priv(dev);
105 struct bcm283x_mu_regs *regs = priv->regs;
106
107 /* Wait until there is space in the FIFO */
108 if (!(readl(&regs->lsr) & BCM283X_MU_LSR_TX_EMPTY))
109 return -EAGAIN;
110
111 /* Send the character */
112 writel(data, &regs->io);
113
114 return 0;
115}
116
117static int bcm283x_mu_serial_pending(struct udevice *dev, bool input)
118{
119 struct bcm283x_mu_priv *priv = dev_get_priv(dev);
120 struct bcm283x_mu_regs *regs = priv->regs;
Fabian Vogtcb97ad42016-09-26 14:26:49 +0200121 unsigned int lsr;
122
Fabian Vogtcb97ad42016-09-26 14:26:49 +0200123 lsr = readl(&regs->lsr);
Stephen Warren3917c262016-03-18 21:41:38 -0600124
125 if (input) {
126 WATCHDOG_RESET();
Stephen Warrene3a46e32016-04-13 22:29:52 -0600127 return (lsr & BCM283X_MU_LSR_RX_READY) ? 1 : 0;
Stephen Warren3917c262016-03-18 21:41:38 -0600128 } else {
Stephen Warrene3a46e32016-04-13 22:29:52 -0600129 return (lsr & BCM283X_MU_LSR_TX_IDLE) ? 0 : 1;
Stephen Warren3917c262016-03-18 21:41:38 -0600130 }
131}
132
133static const struct dm_serial_ops bcm283x_mu_serial_ops = {
134 .putc = bcm283x_mu_serial_putc,
135 .pending = bcm283x_mu_serial_pending,
136 .getc = bcm283x_mu_serial_getc,
137 .setbrg = bcm283x_mu_serial_setbrg,
138};
139
Fabian Vogt9f755f52016-09-26 14:26:44 +0200140#if CONFIG_IS_ENABLED(OF_CONTROL)
141static const struct udevice_id bcm283x_mu_serial_id[] = {
142 {.compatible = "brcm,bcm2835-aux-uart"},
143 {}
144};
145
Alexander Graf9dfeffe2018-01-25 12:05:48 +0100146/*
147 * Check if this serial device is muxed
148 *
149 * The serial device will only work properly if it has been muxed to the serial
150 * pins by firmware. Check whether that happened here.
151 *
152 * @return true if serial device is muxed, false if not
153 */
154static bool bcm283x_is_serial_muxed(void)
155{
156 int serial_gpio = 15;
157 struct udevice *dev;
158
159 if (uclass_first_device(UCLASS_PINCTRL, &dev) || !dev)
160 return false;
161
162 if (pinctrl_get_gpio_mux(dev, 0, serial_gpio) != BCM2835_GPIO_ALT5)
163 return false;
164
165 return true;
166}
167
Fabian Vogt9f755f52016-09-26 14:26:44 +0200168static int bcm283x_mu_serial_ofdata_to_platdata(struct udevice *dev)
169{
170 struct bcm283x_mu_serial_platdata *plat = dev_get_platdata(dev);
171 fdt_addr_t addr;
172
Alexander Graf9dfeffe2018-01-25 12:05:48 +0100173 /* Don't spawn the device if it's not muxed */
174 if (!bcm283x_is_serial_muxed())
175 return -ENODEV;
176
Simon Glassa821c4a2017-05-17 17:18:05 -0600177 addr = devfdt_get_addr(dev);
Fabian Vogt9f755f52016-09-26 14:26:44 +0200178 if (addr == FDT_ADDR_T_NONE)
179 return -EINVAL;
180
181 plat->base = addr;
Alexander Graf80d50012018-01-25 12:05:46 +0100182 plat->clock = dev_read_u32_default(dev, "clock", 1);
Alexander Grafbceab8d2018-01-25 12:05:47 +0100183
184 /*
185 * TODO: Reinitialization doesn't always work for now, just skip
186 * init always - we know we're already initialized
187 */
188 plat->skip_init = true;
Alexander Graf80d50012018-01-25 12:05:46 +0100189
Fabian Vogt9f755f52016-09-26 14:26:44 +0200190 return 0;
191}
192#endif
193
Stephen Warren3917c262016-03-18 21:41:38 -0600194U_BOOT_DRIVER(serial_bcm283x_mu) = {
195 .name = "serial_bcm283x_mu",
196 .id = UCLASS_SERIAL,
Fabian Vogt9f755f52016-09-26 14:26:44 +0200197 .of_match = of_match_ptr(bcm283x_mu_serial_id),
198 .ofdata_to_platdata = of_match_ptr(bcm283x_mu_serial_ofdata_to_platdata),
Stephen Warren3917c262016-03-18 21:41:38 -0600199 .platdata_auto_alloc_size = sizeof(struct bcm283x_mu_serial_platdata),
200 .probe = bcm283x_mu_serial_probe,
201 .ops = &bcm283x_mu_serial_ops,
202 .flags = DM_FLAG_PRE_RELOC,
203 .priv_auto_alloc_size = sizeof(struct bcm283x_mu_priv),
204};