Prepare v2024.10
[platform/kernel/u-boot.git] / drivers / serial / serial_bcm283x_mu.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
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>
13  */
14
15 /* Simple U-Boot driver for the BCM283x mini UART */
16
17 #include <dm.h>
18 #include <errno.h>
19 #include <watchdog.h>
20 #include <asm/gpio.h>
21 #include <asm/io.h>
22 #include <serial.h>
23 #include <dm/platform_data/serial_bcm283x_mu.h>
24 #include <dm/pinctrl.h>
25 #include <linux/bitops.h>
26 #include <linux/compiler.h>
27
28 struct 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
49 struct bcm283x_mu_priv {
50         struct bcm283x_mu_regs *regs;
51 };
52
53 static int bcm283x_mu_serial_getc(struct udevice *dev);
54
55 static int bcm283x_mu_serial_setbrg(struct udevice *dev, int baudrate)
56 {
57         struct bcm283x_mu_serial_plat *plat = dev_get_plat(dev);
58         struct bcm283x_mu_priv *priv = dev_get_priv(dev);
59         struct bcm283x_mu_regs *regs = priv->regs;
60         u32 divider;
61
62         if (plat->skip_init)
63                 goto out;
64
65         divider = plat->clock / (baudrate * 8);
66
67         writel(BCM283X_MU_LCR_DATA_SIZE_8, &regs->lcr);
68         writel(divider - 1, &regs->baud);
69
70 out:
71         /* Flush the RX queue - all data in there is bogus */
72         while (bcm283x_mu_serial_getc(dev) != -EAGAIN) ;
73
74         return 0;
75 }
76
77 static 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
92 static 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
107 static 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;
111         unsigned int lsr;
112
113         lsr = readl(&regs->lsr);
114
115         if (input) {
116                 schedule();
117                 return (lsr & BCM283X_MU_LSR_RX_READY) ? 1 : 0;
118         } else {
119                 return (lsr & BCM283X_MU_LSR_TX_IDLE) ? 0 : 1;
120         }
121 }
122
123 static 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
130 #if CONFIG_IS_ENABLED(OF_CONTROL)
131 static const struct udevice_id bcm283x_mu_serial_id[] = {
132         {.compatible = "brcm,bcm2835-aux-uart"},
133         {}
134 };
135
136 /*
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  */
144 static bool bcm283x_is_serial_muxed(void)
145 {
146         int serial_gpio = 15;
147         struct udevice *dev;
148
149         if (uclass_first_device_err(UCLASS_PINCTRL, &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
158 static int bcm283x_mu_serial_probe(struct udevice *dev)
159 {
160         struct bcm283x_mu_serial_plat *plat = dev_get_plat(dev);
161         struct bcm283x_mu_priv *priv = dev_get_priv(dev);
162         fdt_addr_t addr;
163
164         /* Don't spawn the device if it's not muxed */
165         if (!bcm283x_is_serial_muxed())
166                 return -ENODEV;
167
168         /*
169          * Read the ofdata here rather than in an of_to_plat() method
170          * since we need the soc simple-bus to be probed so that the 'ranges'
171          * property is used.
172          */
173         addr = dev_read_addr(dev);
174         if (addr == FDT_ADDR_T_NONE)
175                 return -EINVAL;
176
177         plat->base = addr;
178         plat->clock = dev_read_u32_default(dev, "clock", 1);
179
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;
185
186         priv->regs = (struct bcm283x_mu_regs *)plat->base;
187
188         return 0;
189 }
190 #endif
191
192 U_BOOT_DRIVER(serial_bcm283x_mu) = {
193         .name = "serial_bcm283x_mu",
194         .id = UCLASS_SERIAL,
195         .of_match = of_match_ptr(bcm283x_mu_serial_id),
196         .plat_auto      = sizeof(struct bcm283x_mu_serial_plat),
197         .probe = bcm283x_mu_serial_probe,
198         .ops = &bcm283x_mu_serial_ops,
199 #if !CONFIG_IS_ENABLED(OF_CONTROL) || IS_ENABLED(CONFIG_OF_BOARD)
200         .flags = DM_FLAG_PRE_RELOC,
201 #endif
202         .priv_auto      = sizeof(struct bcm283x_mu_priv),
203 };