Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Alexander Graf | a73b0ec | 2018-01-25 12:05:55 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2018 Alexander Graf <agraf@suse.de> |
Alexander Graf | a73b0ec | 2018-01-25 12:05:55 +0100 | [diff] [blame] | 4 | */ |
| 5 | |
Alexander Graf | a73b0ec | 2018-01-25 12:05:55 +0100 | [diff] [blame] | 6 | #include <dm.h> |
| 7 | #include <asm/gpio.h> |
| 8 | #include <dm/pinctrl.h> |
| 9 | #include <dm/platform_data/serial_pl01x.h> |
Alexander Graf | a5c3585 | 2018-03-07 22:08:25 +0100 | [diff] [blame] | 10 | #include <serial.h> |
Alexander Graf | a73b0ec | 2018-01-25 12:05:55 +0100 | [diff] [blame] | 11 | #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 Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 19 | * Return: true if serial device is muxed, false if not |
Alexander Graf | a73b0ec | 2018-01-25 12:05:55 +0100 | [diff] [blame] | 20 | */ |
| 21 | static bool bcm283x_is_serial_muxed(void) |
| 22 | { |
| 23 | int serial_gpio = 15; |
| 24 | struct udevice *dev; |
| 25 | |
Michal Suchanek | ac12a2f | 2022-10-12 21:57:59 +0200 | [diff] [blame] | 26 | if (uclass_first_device_err(UCLASS_PINCTRL, &dev)) |
Alexander Graf | a73b0ec | 2018-01-25 12:05:55 +0100 | [diff] [blame] | 27 | 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 Glass | 910c58f | 2020-03-22 21:15:53 -0600 | [diff] [blame] | 35 | static int bcm283x_pl011_serial_probe(struct udevice *dev) |
Alexander Graf | a73b0ec | 2018-01-25 12:05:55 +0100 | [diff] [blame] | 36 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 37 | struct pl01x_serial_plat *plat = dev_get_plat(dev); |
Alexander Graf | a73b0ec | 2018-01-25 12:05:55 +0100 | [diff] [blame] | 38 | 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 Glass | 910c58f | 2020-03-22 21:15:53 -0600 | [diff] [blame] | 44 | /* |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 45 | * Read the ofdata here rather than in an of_to_plat() method |
Simon Glass | 910c58f | 2020-03-22 21:15:53 -0600 | [diff] [blame] | 46 | * since we need the soc simple-bus to be probed so that the 'ranges' |
| 47 | * property is used. |
| 48 | */ |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 49 | ret = pl01x_serial_of_to_plat(dev); |
Alexander Graf | a73b0ec | 2018-01-25 12:05:55 +0100 | [diff] [blame] | 50 | 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 Glass | 910c58f | 2020-03-22 21:15:53 -0600 | [diff] [blame] | 59 | return pl01x_serial_probe(dev); |
Alexander Graf | a73b0ec | 2018-01-25 12:05:55 +0100 | [diff] [blame] | 60 | } |
| 61 | |
Alexander Graf | a5c3585 | 2018-03-07 22:08:25 +0100 | [diff] [blame] | 62 | static 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 | |
| 77 | static 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 Graf | a73b0ec | 2018-01-25 12:05:55 +0100 | [diff] [blame] | 84 | static const struct udevice_id bcm283x_pl011_serial_id[] = { |
| 85 | {.compatible = "brcm,bcm2835-pl011", .data = TYPE_PL011}, |
| 86 | {} |
| 87 | }; |
| 88 | |
| 89 | U_BOOT_DRIVER(bcm283x_pl011_uart) = { |
| 90 | .name = "bcm283x_pl011", |
| 91 | .id = UCLASS_SERIAL, |
| 92 | .of_match = of_match_ptr(bcm283x_pl011_serial_id), |
Simon Glass | 910c58f | 2020-03-22 21:15:53 -0600 | [diff] [blame] | 93 | .probe = bcm283x_pl011_serial_probe, |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 94 | .plat_auto = sizeof(struct pl01x_serial_plat), |
Alexander Graf | a5c3585 | 2018-03-07 22:08:25 +0100 | [diff] [blame] | 95 | .ops = &bcm283x_pl011_serial_ops, |
Simon Glass | 4096046 | 2023-02-05 17:54:42 -0700 | [diff] [blame] | 96 | #if !CONFIG_IS_ENABLED(OF_CONTROL) || IS_ENABLED(CONFIG_OF_BOARD) |
Alexander Graf | a73b0ec | 2018-01-25 12:05:55 +0100 | [diff] [blame] | 97 | .flags = DM_FLAG_PRE_RELOC, |
Bin Meng | bdb33d8 | 2018-10-24 06:36:36 -0700 | [diff] [blame] | 98 | #endif |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 99 | .priv_auto = sizeof(struct pl01x_priv), |
Alexander Graf | a73b0ec | 2018-01-25 12:05:55 +0100 | [diff] [blame] | 100 | }; |