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 | |
| 17 | #include <common.h> |
| 18 | #include <dm.h> |
| 19 | #include <errno.h> |
| 20 | #include <watchdog.h> |
Alexander Graf | ff9fd76 | 2018-01-25 12:05:48 +0100 | [diff] [blame] | 21 | #include <asm/gpio.h> |
Stephen Warren | f84f9bf | 2016-03-18 21:41:38 -0600 | [diff] [blame] | 22 | #include <asm/io.h> |
| 23 | #include <serial.h> |
| 24 | #include <dm/platform_data/serial_bcm283x_mu.h> |
Alexander Graf | ff9fd76 | 2018-01-25 12:05:48 +0100 | [diff] [blame] | 25 | #include <dm/pinctrl.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 | { |
| 57 | struct bcm283x_mu_serial_platdata *plat = dev_get_platdata(dev); |
| 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 | |
| 77 | static int bcm283x_mu_serial_probe(struct udevice *dev) |
| 78 | { |
| 79 | struct bcm283x_mu_serial_platdata *plat = dev_get_platdata(dev); |
| 80 | struct bcm283x_mu_priv *priv = dev_get_priv(dev); |
| 81 | |
| 82 | priv->regs = (struct bcm283x_mu_regs *)plat->base; |
| 83 | |
| 84 | return 0; |
| 85 | } |
| 86 | |
| 87 | static int bcm283x_mu_serial_getc(struct udevice *dev) |
| 88 | { |
| 89 | struct bcm283x_mu_priv *priv = dev_get_priv(dev); |
| 90 | struct bcm283x_mu_regs *regs = priv->regs; |
| 91 | u32 data; |
| 92 | |
| 93 | /* Wait until there is data in the FIFO */ |
| 94 | if (!(readl(®s->lsr) & BCM283X_MU_LSR_RX_READY)) |
| 95 | return -EAGAIN; |
| 96 | |
| 97 | data = readl(®s->io); |
| 98 | |
| 99 | return (int)data; |
| 100 | } |
| 101 | |
| 102 | static int bcm283x_mu_serial_putc(struct udevice *dev, const char data) |
| 103 | { |
| 104 | struct bcm283x_mu_priv *priv = dev_get_priv(dev); |
| 105 | struct bcm283x_mu_regs *regs = priv->regs; |
| 106 | |
| 107 | /* Wait until there is space in the FIFO */ |
| 108 | if (!(readl(®s->lsr) & BCM283X_MU_LSR_TX_EMPTY)) |
| 109 | return -EAGAIN; |
| 110 | |
| 111 | /* Send the character */ |
| 112 | writel(data, ®s->io); |
| 113 | |
| 114 | return 0; |
| 115 | } |
| 116 | |
| 117 | static int bcm283x_mu_serial_pending(struct udevice *dev, bool input) |
| 118 | { |
| 119 | struct bcm283x_mu_priv *priv = dev_get_priv(dev); |
| 120 | struct bcm283x_mu_regs *regs = priv->regs; |
Fabian Vogt | 0f11661 | 2016-09-26 14:26:49 +0200 | [diff] [blame] | 121 | unsigned int lsr; |
| 122 | |
Fabian Vogt | 0f11661 | 2016-09-26 14:26:49 +0200 | [diff] [blame] | 123 | lsr = readl(®s->lsr); |
Stephen Warren | f84f9bf | 2016-03-18 21:41:38 -0600 | [diff] [blame] | 124 | |
| 125 | if (input) { |
| 126 | WATCHDOG_RESET(); |
Stephen Warren | 3a03698 | 2016-04-13 22:29:52 -0600 | [diff] [blame] | 127 | return (lsr & BCM283X_MU_LSR_RX_READY) ? 1 : 0; |
Stephen Warren | f84f9bf | 2016-03-18 21:41:38 -0600 | [diff] [blame] | 128 | } else { |
Stephen Warren | 3a03698 | 2016-04-13 22:29:52 -0600 | [diff] [blame] | 129 | return (lsr & BCM283X_MU_LSR_TX_IDLE) ? 0 : 1; |
Stephen Warren | f84f9bf | 2016-03-18 21:41:38 -0600 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | |
| 133 | static const struct dm_serial_ops bcm283x_mu_serial_ops = { |
| 134 | .putc = bcm283x_mu_serial_putc, |
| 135 | .pending = bcm283x_mu_serial_pending, |
| 136 | .getc = bcm283x_mu_serial_getc, |
| 137 | .setbrg = bcm283x_mu_serial_setbrg, |
| 138 | }; |
| 139 | |
Fabian Vogt | cfb76fc | 2016-09-26 14:26:44 +0200 | [diff] [blame] | 140 | #if CONFIG_IS_ENABLED(OF_CONTROL) |
| 141 | static const struct udevice_id bcm283x_mu_serial_id[] = { |
| 142 | {.compatible = "brcm,bcm2835-aux-uart"}, |
| 143 | {} |
| 144 | }; |
| 145 | |
Alexander Graf | ff9fd76 | 2018-01-25 12:05:48 +0100 | [diff] [blame] | 146 | /* |
| 147 | * Check if this serial device is muxed |
| 148 | * |
| 149 | * The serial device will only work properly if it has been muxed to the serial |
| 150 | * pins by firmware. Check whether that happened here. |
| 151 | * |
| 152 | * @return true if serial device is muxed, false if not |
| 153 | */ |
| 154 | static bool bcm283x_is_serial_muxed(void) |
| 155 | { |
| 156 | int serial_gpio = 15; |
| 157 | struct udevice *dev; |
| 158 | |
| 159 | if (uclass_first_device(UCLASS_PINCTRL, &dev) || !dev) |
| 160 | return false; |
| 161 | |
| 162 | if (pinctrl_get_gpio_mux(dev, 0, serial_gpio) != BCM2835_GPIO_ALT5) |
| 163 | return false; |
| 164 | |
| 165 | return true; |
| 166 | } |
| 167 | |
Fabian Vogt | cfb76fc | 2016-09-26 14:26:44 +0200 | [diff] [blame] | 168 | static int bcm283x_mu_serial_ofdata_to_platdata(struct udevice *dev) |
| 169 | { |
| 170 | struct bcm283x_mu_serial_platdata *plat = dev_get_platdata(dev); |
| 171 | fdt_addr_t addr; |
| 172 | |
Alexander Graf | ff9fd76 | 2018-01-25 12:05:48 +0100 | [diff] [blame] | 173 | /* Don't spawn the device if it's not muxed */ |
| 174 | if (!bcm283x_is_serial_muxed()) |
| 175 | return -ENODEV; |
| 176 | |
Simon Glass | ba1dea4 | 2017-05-17 17:18:05 -0600 | [diff] [blame] | 177 | addr = devfdt_get_addr(dev); |
Fabian Vogt | cfb76fc | 2016-09-26 14:26:44 +0200 | [diff] [blame] | 178 | if (addr == FDT_ADDR_T_NONE) |
| 179 | return -EINVAL; |
| 180 | |
| 181 | plat->base = addr; |
Alexander Graf | 35b31e0 | 2018-01-25 12:05:46 +0100 | [diff] [blame] | 182 | plat->clock = dev_read_u32_default(dev, "clock", 1); |
Alexander Graf | c3883e0 | 2018-01-25 12:05:47 +0100 | [diff] [blame] | 183 | |
| 184 | /* |
| 185 | * TODO: Reinitialization doesn't always work for now, just skip |
| 186 | * init always - we know we're already initialized |
| 187 | */ |
| 188 | plat->skip_init = true; |
Alexander Graf | 35b31e0 | 2018-01-25 12:05:46 +0100 | [diff] [blame] | 189 | |
Fabian Vogt | cfb76fc | 2016-09-26 14:26:44 +0200 | [diff] [blame] | 190 | return 0; |
| 191 | } |
| 192 | #endif |
| 193 | |
Stephen Warren | f84f9bf | 2016-03-18 21:41:38 -0600 | [diff] [blame] | 194 | U_BOOT_DRIVER(serial_bcm283x_mu) = { |
| 195 | .name = "serial_bcm283x_mu", |
| 196 | .id = UCLASS_SERIAL, |
Fabian Vogt | cfb76fc | 2016-09-26 14:26:44 +0200 | [diff] [blame] | 197 | .of_match = of_match_ptr(bcm283x_mu_serial_id), |
| 198 | .ofdata_to_platdata = of_match_ptr(bcm283x_mu_serial_ofdata_to_platdata), |
Stephen Warren | f84f9bf | 2016-03-18 21:41:38 -0600 | [diff] [blame] | 199 | .platdata_auto_alloc_size = sizeof(struct bcm283x_mu_serial_platdata), |
| 200 | .probe = bcm283x_mu_serial_probe, |
| 201 | .ops = &bcm283x_mu_serial_ops, |
Bin Meng | bdb33d8 | 2018-10-24 06:36:36 -0700 | [diff] [blame^] | 202 | #if !CONFIG_IS_ENABLED(OF_CONTROL) |
Stephen Warren | f84f9bf | 2016-03-18 21:41:38 -0600 | [diff] [blame] | 203 | .flags = DM_FLAG_PRE_RELOC, |
Bin Meng | bdb33d8 | 2018-10-24 06:36:36 -0700 | [diff] [blame^] | 204 | #endif |
Stephen Warren | f84f9bf | 2016-03-18 21:41:38 -0600 | [diff] [blame] | 205 | .priv_auto_alloc_size = sizeof(struct bcm283x_mu_priv), |
| 206 | }; |