1 /*
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/gpio.h>
23 #include <asm/io.h>
24 #include <serial.h>
25 #include <dm/platform_data/serial_bcm283x_mu.h>
26 #include <dm/pinctrl.h>
27 #include <linux/compiler.h>
28 
29 struct 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 
50 struct bcm283x_mu_priv {
51 	struct bcm283x_mu_regs *regs;
52 };
53 
54 static int bcm283x_mu_serial_setbrg(struct udevice *dev, int baudrate)
55 {
56 	struct bcm283x_mu_serial_platdata *plat = dev_get_platdata(dev);
57 	struct bcm283x_mu_priv *priv = dev_get_priv(dev);
58 	struct bcm283x_mu_regs *regs = priv->regs;
59 	u32 divider;
60 
61 	if (plat->skip_init)
62 		return 0;
63 
64 	divider = plat->clock / (baudrate * 8);
65 
66 	writel(BCM283X_MU_LCR_DATA_SIZE_8, &regs->lcr);
67 	writel(divider - 1, &regs->baud);
68 
69 	return 0;
70 }
71 
72 static int bcm283x_mu_serial_probe(struct udevice *dev)
73 {
74 	struct bcm283x_mu_serial_platdata *plat = dev_get_platdata(dev);
75 	struct bcm283x_mu_priv *priv = dev_get_priv(dev);
76 
77 	priv->regs = (struct bcm283x_mu_regs *)plat->base;
78 
79 	return 0;
80 }
81 
82 static int bcm283x_mu_serial_getc(struct udevice *dev)
83 {
84 	struct bcm283x_mu_priv *priv = dev_get_priv(dev);
85 	struct bcm283x_mu_regs *regs = priv->regs;
86 	u32 data;
87 
88 	/* Wait until there is data in the FIFO */
89 	if (!(readl(&regs->lsr) & BCM283X_MU_LSR_RX_READY))
90 		return -EAGAIN;
91 
92 	data = readl(&regs->io);
93 
94 	return (int)data;
95 }
96 
97 static int bcm283x_mu_serial_putc(struct udevice *dev, const char data)
98 {
99 	struct bcm283x_mu_priv *priv = dev_get_priv(dev);
100 	struct bcm283x_mu_regs *regs = priv->regs;
101 
102 	/* Wait until there is space in the FIFO */
103 	if (!(readl(&regs->lsr) & BCM283X_MU_LSR_TX_EMPTY))
104 		return -EAGAIN;
105 
106 	/* Send the character */
107 	writel(data, &regs->io);
108 
109 	return 0;
110 }
111 
112 static int bcm283x_mu_serial_pending(struct udevice *dev, bool input)
113 {
114 	struct bcm283x_mu_priv *priv = dev_get_priv(dev);
115 	struct bcm283x_mu_regs *regs = priv->regs;
116 	unsigned int lsr;
117 
118 	lsr = readl(&regs->lsr);
119 
120 	if (input) {
121 		WATCHDOG_RESET();
122 		return (lsr & BCM283X_MU_LSR_RX_READY) ? 1 : 0;
123 	} else {
124 		return (lsr & BCM283X_MU_LSR_TX_IDLE) ? 0 : 1;
125 	}
126 }
127 
128 static const struct dm_serial_ops bcm283x_mu_serial_ops = {
129 	.putc = bcm283x_mu_serial_putc,
130 	.pending = bcm283x_mu_serial_pending,
131 	.getc = bcm283x_mu_serial_getc,
132 	.setbrg = bcm283x_mu_serial_setbrg,
133 };
134 
135 #if CONFIG_IS_ENABLED(OF_CONTROL)
136 static const struct udevice_id bcm283x_mu_serial_id[] = {
137 	{.compatible = "brcm,bcm2835-aux-uart"},
138 	{}
139 };
140 
141 /*
142  * Check if this serial device is muxed
143  *
144  * The serial device will only work properly if it has been muxed to the serial
145  * pins by firmware. Check whether that happened here.
146  *
147  * @return true if serial device is muxed, false if not
148  */
149 static bool bcm283x_is_serial_muxed(void)
150 {
151 	int serial_gpio = 15;
152 	struct udevice *dev;
153 
154 	if (uclass_first_device(UCLASS_PINCTRL, &dev) || !dev)
155 		return false;
156 
157 	if (pinctrl_get_gpio_mux(dev, 0, serial_gpio) != BCM2835_GPIO_ALT5)
158 		return false;
159 
160 	return true;
161 }
162 
163 static int bcm283x_mu_serial_ofdata_to_platdata(struct udevice *dev)
164 {
165 	struct bcm283x_mu_serial_platdata *plat = dev_get_platdata(dev);
166 	fdt_addr_t addr;
167 
168 	/* Don't spawn the device if it's not muxed */
169 	if (!bcm283x_is_serial_muxed())
170 		return -ENODEV;
171 
172 	addr = devfdt_get_addr(dev);
173 	if (addr == FDT_ADDR_T_NONE)
174 		return -EINVAL;
175 
176 	plat->base = addr;
177 	plat->clock = dev_read_u32_default(dev, "clock", 1);
178 
179 	/*
180 	 * TODO: Reinitialization doesn't always work for now, just skip
181 	 *       init always - we know we're already initialized
182 	 */
183 	plat->skip_init = true;
184 
185 	return 0;
186 }
187 #endif
188 
189 U_BOOT_DRIVER(serial_bcm283x_mu) = {
190 	.name = "serial_bcm283x_mu",
191 	.id = UCLASS_SERIAL,
192 	.of_match = of_match_ptr(bcm283x_mu_serial_id),
193 	.ofdata_to_platdata = of_match_ptr(bcm283x_mu_serial_ofdata_to_platdata),
194 	.platdata_auto_alloc_size = sizeof(struct bcm283x_mu_serial_platdata),
195 	.probe = bcm283x_mu_serial_probe,
196 	.ops = &bcm283x_mu_serial_ops,
197 	.flags = DM_FLAG_PRE_RELOC,
198 	.priv_auto_alloc_size = sizeof(struct bcm283x_mu_priv),
199 };
200