Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Stephen Warren | f84f9bf | 2016-03-18 21:41:38 -0600 | [diff] [blame] | 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> |
Stephen Warren | f84f9bf | 2016-03-18 21:41:38 -0600 | [diff] [blame] | 13 | */ |
| 14 | |
| 15 | /* Simple U-Boot driver for the BCM283x mini UART */ |
| 16 | |
Stephen Warren | f84f9bf | 2016-03-18 21:41:38 -0600 | [diff] [blame] | 17 | #include <dm.h> |
| 18 | #include <errno.h> |
| 19 | #include <watchdog.h> |
Alexander Graf | ff9fd76 | 2018-01-25 12:05:48 +0100 | [diff] [blame] | 20 | #include <asm/gpio.h> |
Stephen Warren | f84f9bf | 2016-03-18 21:41:38 -0600 | [diff] [blame] | 21 | #include <asm/io.h> |
| 22 | #include <serial.h> |
| 23 | #include <dm/platform_data/serial_bcm283x_mu.h> |
Alexander Graf | ff9fd76 | 2018-01-25 12:05:48 +0100 | [diff] [blame] | 24 | #include <dm/pinctrl.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 25 | #include <linux/bitops.h> |
Stephen Warren | f84f9bf | 2016-03-18 21:41:38 -0600 | [diff] [blame] | 26 | #include <linux/compiler.h> |
Fabian Vogt | cfb76fc | 2016-09-26 14:26:44 +0200 | [diff] [blame] | 27 | |
Stephen Warren | f84f9bf | 2016-03-18 21:41:38 -0600 | [diff] [blame] | 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 | |
Alexander Graf | 15b79ff | 2018-03-07 22:08:24 +0100 | [diff] [blame] | 53 | static int bcm283x_mu_serial_getc(struct udevice *dev); |
| 54 | |
Stephen Warren | f84f9bf | 2016-03-18 21:41:38 -0600 | [diff] [blame] | 55 | static int bcm283x_mu_serial_setbrg(struct udevice *dev, int baudrate) |
| 56 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 57 | struct bcm283x_mu_serial_plat *plat = dev_get_plat(dev); |
Stephen Warren | f84f9bf | 2016-03-18 21:41:38 -0600 | [diff] [blame] | 58 | struct bcm283x_mu_priv *priv = dev_get_priv(dev); |
| 59 | struct bcm283x_mu_regs *regs = priv->regs; |
| 60 | u32 divider; |
| 61 | |
Alexander Graf | f7c2628 | 2018-01-25 12:05:44 +0100 | [diff] [blame] | 62 | if (plat->skip_init) |
Alexander Graf | 15b79ff | 2018-03-07 22:08:24 +0100 | [diff] [blame] | 63 | goto out; |
Stephen Warren | f84f9bf | 2016-03-18 21:41:38 -0600 | [diff] [blame] | 64 | |
| 65 | divider = plat->clock / (baudrate * 8); |
| 66 | |
| 67 | writel(BCM283X_MU_LCR_DATA_SIZE_8, ®s->lcr); |
| 68 | writel(divider - 1, ®s->baud); |
| 69 | |
Alexander Graf | 15b79ff | 2018-03-07 22:08:24 +0100 | [diff] [blame] | 70 | out: |
| 71 | /* Flush the RX queue - all data in there is bogus */ |
| 72 | while (bcm283x_mu_serial_getc(dev) != -EAGAIN) ; |
| 73 | |
Stephen Warren | f84f9bf | 2016-03-18 21:41:38 -0600 | [diff] [blame] | 74 | return 0; |
| 75 | } |
| 76 | |
Stephen Warren | f84f9bf | 2016-03-18 21:41:38 -0600 | [diff] [blame] | 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(®s->lsr) & BCM283X_MU_LSR_RX_READY)) |
| 85 | return -EAGAIN; |
| 86 | |
| 87 | data = readl(®s->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(®s->lsr) & BCM283X_MU_LSR_TX_EMPTY)) |
| 99 | return -EAGAIN; |
| 100 | |
| 101 | /* Send the character */ |
| 102 | writel(data, ®s->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; |
Fabian Vogt | 0f11661 | 2016-09-26 14:26:49 +0200 | [diff] [blame] | 111 | unsigned int lsr; |
| 112 | |
Fabian Vogt | 0f11661 | 2016-09-26 14:26:49 +0200 | [diff] [blame] | 113 | lsr = readl(®s->lsr); |
Stephen Warren | f84f9bf | 2016-03-18 21:41:38 -0600 | [diff] [blame] | 114 | |
| 115 | if (input) { |
Stefan Roese | 80877fa | 2022-09-02 14:10:46 +0200 | [diff] [blame] | 116 | schedule(); |
Stephen Warren | 3a03698 | 2016-04-13 22:29:52 -0600 | [diff] [blame] | 117 | return (lsr & BCM283X_MU_LSR_RX_READY) ? 1 : 0; |
Stephen Warren | f84f9bf | 2016-03-18 21:41:38 -0600 | [diff] [blame] | 118 | } else { |
Stephen Warren | 3a03698 | 2016-04-13 22:29:52 -0600 | [diff] [blame] | 119 | return (lsr & BCM283X_MU_LSR_TX_IDLE) ? 0 : 1; |
Stephen Warren | f84f9bf | 2016-03-18 21:41:38 -0600 | [diff] [blame] | 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 | |
Fabian Vogt | cfb76fc | 2016-09-26 14:26:44 +0200 | [diff] [blame] | 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 | |
Alexander Graf | ff9fd76 | 2018-01-25 12:05:48 +0100 | [diff] [blame] | 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 | * |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 142 | * Return: true if serial device is muxed, false if not |
Alexander Graf | ff9fd76 | 2018-01-25 12:05:48 +0100 | [diff] [blame] | 143 | */ |
| 144 | static bool bcm283x_is_serial_muxed(void) |
| 145 | { |
| 146 | int serial_gpio = 15; |
| 147 | struct udevice *dev; |
| 148 | |
Michal Suchanek | ac12a2f | 2022-10-12 21:57:59 +0200 | [diff] [blame] | 149 | if (uclass_first_device_err(UCLASS_PINCTRL, &dev)) |
Alexander Graf | ff9fd76 | 2018-01-25 12:05:48 +0100 | [diff] [blame] | 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 | |
Simon Glass | 910c58f | 2020-03-22 21:15:53 -0600 | [diff] [blame] | 158 | static int bcm283x_mu_serial_probe(struct udevice *dev) |
Fabian Vogt | cfb76fc | 2016-09-26 14:26:44 +0200 | [diff] [blame] | 159 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 160 | struct bcm283x_mu_serial_plat *plat = dev_get_plat(dev); |
Simon Glass | 910c58f | 2020-03-22 21:15:53 -0600 | [diff] [blame] | 161 | struct bcm283x_mu_priv *priv = dev_get_priv(dev); |
Fabian Vogt | cfb76fc | 2016-09-26 14:26:44 +0200 | [diff] [blame] | 162 | fdt_addr_t addr; |
| 163 | |
Alexander Graf | ff9fd76 | 2018-01-25 12:05:48 +0100 | [diff] [blame] | 164 | /* Don't spawn the device if it's not muxed */ |
| 165 | if (!bcm283x_is_serial_muxed()) |
| 166 | return -ENODEV; |
| 167 | |
Simon Glass | 910c58f | 2020-03-22 21:15:53 -0600 | [diff] [blame] | 168 | /* |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 169 | * Read the ofdata here rather than in an of_to_plat() method |
Simon Glass | 910c58f | 2020-03-22 21:15:53 -0600 | [diff] [blame] | 170 | * since we need the soc simple-bus to be probed so that the 'ranges' |
| 171 | * property is used. |
| 172 | */ |
Masahiro Yamada | a89b4de | 2020-07-17 14:36:48 +0900 | [diff] [blame] | 173 | addr = dev_read_addr(dev); |
Fabian Vogt | cfb76fc | 2016-09-26 14:26:44 +0200 | [diff] [blame] | 174 | if (addr == FDT_ADDR_T_NONE) |
| 175 | return -EINVAL; |
| 176 | |
| 177 | plat->base = addr; |
Alexander Graf | 35b31e0 | 2018-01-25 12:05:46 +0100 | [diff] [blame] | 178 | plat->clock = dev_read_u32_default(dev, "clock", 1); |
Alexander Graf | c3883e0 | 2018-01-25 12:05:47 +0100 | [diff] [blame] | 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; |
Alexander Graf | 35b31e0 | 2018-01-25 12:05:46 +0100 | [diff] [blame] | 185 | |
Simon Glass | 910c58f | 2020-03-22 21:15:53 -0600 | [diff] [blame] | 186 | priv->regs = (struct bcm283x_mu_regs *)plat->base; |
| 187 | |
Fabian Vogt | cfb76fc | 2016-09-26 14:26:44 +0200 | [diff] [blame] | 188 | return 0; |
| 189 | } |
| 190 | #endif |
| 191 | |
Stephen Warren | f84f9bf | 2016-03-18 21:41:38 -0600 | [diff] [blame] | 192 | U_BOOT_DRIVER(serial_bcm283x_mu) = { |
| 193 | .name = "serial_bcm283x_mu", |
| 194 | .id = UCLASS_SERIAL, |
Fabian Vogt | cfb76fc | 2016-09-26 14:26:44 +0200 | [diff] [blame] | 195 | .of_match = of_match_ptr(bcm283x_mu_serial_id), |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 196 | .plat_auto = sizeof(struct bcm283x_mu_serial_plat), |
Stephen Warren | f84f9bf | 2016-03-18 21:41:38 -0600 | [diff] [blame] | 197 | .probe = bcm283x_mu_serial_probe, |
| 198 | .ops = &bcm283x_mu_serial_ops, |
Simon Glass | 4096046 | 2023-02-05 17:54:42 -0700 | [diff] [blame] | 199 | #if !CONFIG_IS_ENABLED(OF_CONTROL) || IS_ENABLED(CONFIG_OF_BOARD) |
Stephen Warren | f84f9bf | 2016-03-18 21:41:38 -0600 | [diff] [blame] | 200 | .flags = DM_FLAG_PRE_RELOC, |
Bin Meng | bdb33d8 | 2018-10-24 06:36:36 -0700 | [diff] [blame] | 201 | #endif |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 202 | .priv_auto = sizeof(struct bcm283x_mu_priv), |
Stephen Warren | f84f9bf | 2016-03-18 21:41:38 -0600 | [diff] [blame] | 203 | }; |