blob: 2abc1c4658f3c5b5fe0c2c0a59bec993b90375b0 [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
Alexander Grafa73b0ec2018-01-25 12:05:55 +01006#include <dm.h>
7#include <asm/gpio.h>
8#include <dm/pinctrl.h>
9#include <dm/platform_data/serial_pl01x.h>
Alexander Grafa5c35852018-03-07 22:08:25 +010010#include <serial.h>
Alexander Grafa73b0ec2018-01-25 12:05:55 +010011#include "serial_pl01x_internal.h"
12
13/*
14 * Check if this serial device is muxed
15 *
16 * The serial device will only work properly if it has been muxed to the serial
17 * pins by firmware. Check whether that happened here.
18 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +010019 * Return: true if serial device is muxed, false if not
Alexander Grafa73b0ec2018-01-25 12:05:55 +010020 */
21static bool bcm283x_is_serial_muxed(void)
22{
23 int serial_gpio = 15;
24 struct udevice *dev;
25
Michal Suchanekac12a2f2022-10-12 21:57:59 +020026 if (uclass_first_device_err(UCLASS_PINCTRL, &dev))
Alexander Grafa73b0ec2018-01-25 12:05:55 +010027 return false;
28
29 if (pinctrl_get_gpio_mux(dev, 0, serial_gpio) != BCM2835_GPIO_ALT0)
30 return false;
31
32 return true;
33}
34
Simon Glass910c58f2020-03-22 21:15:53 -060035static int bcm283x_pl011_serial_probe(struct udevice *dev)
Alexander Grafa73b0ec2018-01-25 12:05:55 +010036{
Simon Glassb75b15b2020-12-03 16:55:23 -070037 struct pl01x_serial_plat *plat = dev_get_plat(dev);
Alexander Grafa73b0ec2018-01-25 12:05:55 +010038 int ret;
39
40 /* Don't spawn the device if it's not muxed */
41 if (!bcm283x_is_serial_muxed())
42 return -ENODEV;
43
Simon Glass910c58f2020-03-22 21:15:53 -060044 /*
Simon Glassaad29ae2020-12-03 16:55:21 -070045 * Read the ofdata here rather than in an of_to_plat() method
Simon Glass910c58f2020-03-22 21:15:53 -060046 * since we need the soc simple-bus to be probed so that the 'ranges'
47 * property is used.
48 */
Simon Glassaad29ae2020-12-03 16:55:21 -070049 ret = pl01x_serial_of_to_plat(dev);
Alexander Grafa73b0ec2018-01-25 12:05:55 +010050 if (ret)
51 return ret;
52
53 /*
54 * TODO: Reinitialization doesn't always work for now, just skip
55 * init always - we know we're already initialized
56 */
57 plat->skip_init = true;
58
Simon Glass910c58f2020-03-22 21:15:53 -060059 return pl01x_serial_probe(dev);
Alexander Grafa73b0ec2018-01-25 12:05:55 +010060}
61
Alexander Grafa5c35852018-03-07 22:08:25 +010062static int bcm283x_pl011_serial_setbrg(struct udevice *dev, int baudrate)
63{
64 int r;
65
66 r = pl01x_serial_setbrg(dev, baudrate);
67
68 /*
69 * We may have been muxed to a bogus line before. Drain the RX
70 * queue so we start at a clean slate.
71 */
72 while (pl01x_serial_getc(dev) != -EAGAIN) ;
73
74 return r;
75}
76
77static const struct dm_serial_ops bcm283x_pl011_serial_ops = {
78 .putc = pl01x_serial_putc,
79 .pending = pl01x_serial_pending,
80 .getc = pl01x_serial_getc,
81 .setbrg = bcm283x_pl011_serial_setbrg,
82};
83
Alexander Grafa73b0ec2018-01-25 12:05:55 +010084static const struct udevice_id bcm283x_pl011_serial_id[] = {
85 {.compatible = "brcm,bcm2835-pl011", .data = TYPE_PL011},
86 {}
87};
88
89U_BOOT_DRIVER(bcm283x_pl011_uart) = {
90 .name = "bcm283x_pl011",
91 .id = UCLASS_SERIAL,
92 .of_match = of_match_ptr(bcm283x_pl011_serial_id),
Simon Glass910c58f2020-03-22 21:15:53 -060093 .probe = bcm283x_pl011_serial_probe,
Simon Glassb75b15b2020-12-03 16:55:23 -070094 .plat_auto = sizeof(struct pl01x_serial_plat),
Alexander Grafa5c35852018-03-07 22:08:25 +010095 .ops = &bcm283x_pl011_serial_ops,
Simon Glass40960462023-02-05 17:54:42 -070096#if !CONFIG_IS_ENABLED(OF_CONTROL) || IS_ENABLED(CONFIG_OF_BOARD)
Alexander Grafa73b0ec2018-01-25 12:05:55 +010097 .flags = DM_FLAG_PRE_RELOC,
Bin Mengbdb33d82018-10-24 06:36:36 -070098#endif
Simon Glass8a2b47f2020-12-03 16:55:17 -070099 .priv_auto = sizeof(struct pl01x_priv),
Alexander Grafa73b0ec2018-01-25 12:05:55 +0100100};