blob: 6d7a87cf652cd49cc26109a5f65d6015f33459a5 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Alexander Grafa73b0ec2018-01-25 12:05:55 +01002/*
3 * Copyright (c) 2018 Alexander Graf <agraf@suse.de>
Alexander Grafa73b0ec2018-01-25 12:05:55 +01004 */
5
6#include <common.h>
7#include <dm.h>
8#include <asm/gpio.h>
9#include <dm/pinctrl.h>
10#include <dm/platform_data/serial_pl01x.h>
Alexander Grafa5c35852018-03-07 22:08:25 +010011#include <serial.h>
Alexander Grafa73b0ec2018-01-25 12:05:55 +010012#include "serial_pl01x_internal.h"
13
14/*
15 * Check if this serial device is muxed
16 *
17 * The serial device will only work properly if it has been muxed to the serial
18 * pins by firmware. Check whether that happened here.
19 *
20 * @return true if serial device is muxed, false if not
21 */
22static bool bcm283x_is_serial_muxed(void)
23{
24 int serial_gpio = 15;
25 struct udevice *dev;
26
27 if (uclass_first_device(UCLASS_PINCTRL, &dev) || !dev)
28 return false;
29
30 if (pinctrl_get_gpio_mux(dev, 0, serial_gpio) != BCM2835_GPIO_ALT0)
31 return false;
32
33 return true;
34}
35
Simon Glass910c58f2020-03-22 21:15:53 -060036static int bcm283x_pl011_serial_probe(struct udevice *dev)
Alexander Grafa73b0ec2018-01-25 12:05:55 +010037{
Simon Glassfa20e932020-12-03 16:55:20 -070038 struct pl01x_serial_platdata *plat = dev_get_plat(dev);
Alexander Grafa73b0ec2018-01-25 12:05:55 +010039 int ret;
40
41 /* Don't spawn the device if it's not muxed */
42 if (!bcm283x_is_serial_muxed())
43 return -ENODEV;
44
Simon Glass910c58f2020-03-22 21:15:53 -060045 /*
46 * Read the ofdata here rather than in an ofdata_to_platdata() method
47 * since we need the soc simple-bus to be probed so that the 'ranges'
48 * property is used.
49 */
Alexander Grafa73b0ec2018-01-25 12:05:55 +010050 ret = pl01x_serial_ofdata_to_platdata(dev);
51 if (ret)
52 return ret;
53
54 /*
55 * TODO: Reinitialization doesn't always work for now, just skip
56 * init always - we know we're already initialized
57 */
58 plat->skip_init = true;
59
Simon Glass910c58f2020-03-22 21:15:53 -060060 return pl01x_serial_probe(dev);
Alexander Grafa73b0ec2018-01-25 12:05:55 +010061}
62
Alexander Grafa5c35852018-03-07 22:08:25 +010063static int bcm283x_pl011_serial_setbrg(struct udevice *dev, int baudrate)
64{
65 int r;
66
67 r = pl01x_serial_setbrg(dev, baudrate);
68
69 /*
70 * We may have been muxed to a bogus line before. Drain the RX
71 * queue so we start at a clean slate.
72 */
73 while (pl01x_serial_getc(dev) != -EAGAIN) ;
74
75 return r;
76}
77
78static const struct dm_serial_ops bcm283x_pl011_serial_ops = {
79 .putc = pl01x_serial_putc,
80 .pending = pl01x_serial_pending,
81 .getc = pl01x_serial_getc,
82 .setbrg = bcm283x_pl011_serial_setbrg,
83};
84
Alexander Grafa73b0ec2018-01-25 12:05:55 +010085static const struct udevice_id bcm283x_pl011_serial_id[] = {
86 {.compatible = "brcm,bcm2835-pl011", .data = TYPE_PL011},
87 {}
88};
89
90U_BOOT_DRIVER(bcm283x_pl011_uart) = {
91 .name = "bcm283x_pl011",
92 .id = UCLASS_SERIAL,
93 .of_match = of_match_ptr(bcm283x_pl011_serial_id),
Simon Glass910c58f2020-03-22 21:15:53 -060094 .probe = bcm283x_pl011_serial_probe,
Simon Glass71fa5b42020-12-03 16:55:18 -070095 .plat_auto = sizeof(struct pl01x_serial_platdata),
Alexander Grafa5c35852018-03-07 22:08:25 +010096 .ops = &bcm283x_pl011_serial_ops,
Matthias Brugger501b6842019-11-08 14:49:48 +010097#if !CONFIG_IS_ENABLED(OF_CONTROL) || CONFIG_IS_ENABLED(OF_BOARD)
Alexander Grafa73b0ec2018-01-25 12:05:55 +010098 .flags = DM_FLAG_PRE_RELOC,
Bin Mengbdb33d82018-10-24 06:36:36 -070099#endif
Simon Glass8a2b47f2020-12-03 16:55:17 -0700100 .priv_auto = sizeof(struct pl01x_priv),
Alexander Grafa73b0ec2018-01-25 12:05:55 +0100101};