Stephen Warren | 3917c26 | 2016-03-18 21:41:38 -0600 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2016 Stephen Warren <swarren@wwwdotorg.org> |
| 3 | * |
| 4 | * Derived from pl01x code: |
| 5 | * |
| 6 | * (C) Copyright 2000 |
| 7 | * Rob Taylor, Flying Pig Systems. robt@flyingpig.com. |
| 8 | * |
| 9 | * (C) Copyright 2004 |
| 10 | * ARM Ltd. |
| 11 | * Philippe Robin, <philippe.robin@arm.com> |
| 12 | * |
| 13 | * SPDX-License-Identifier: GPL-2.0+ |
| 14 | */ |
| 15 | |
| 16 | /* Simple U-Boot driver for the BCM283x mini UART */ |
| 17 | |
| 18 | #include <common.h> |
| 19 | #include <dm.h> |
| 20 | #include <errno.h> |
| 21 | #include <watchdog.h> |
| 22 | #include <asm/io.h> |
| 23 | #include <serial.h> |
| 24 | #include <dm/platform_data/serial_bcm283x_mu.h> |
| 25 | #include <linux/compiler.h> |
| 26 | #include <fdtdec.h> |
| 27 | |
Fabian Vogt | 9f755f5 | 2016-09-26 14:26:44 +0200 | [diff] [blame^] | 28 | DECLARE_GLOBAL_DATA_PTR; |
| 29 | |
Stephen Warren | 3917c26 | 2016-03-18 21:41:38 -0600 | [diff] [blame] | 30 | struct bcm283x_mu_regs { |
| 31 | u32 io; |
| 32 | u32 iir; |
| 33 | u32 ier; |
| 34 | u32 lcr; |
| 35 | u32 mcr; |
| 36 | u32 lsr; |
| 37 | u32 msr; |
| 38 | u32 scratch; |
| 39 | u32 cntl; |
| 40 | u32 stat; |
| 41 | u32 baud; |
| 42 | }; |
| 43 | |
| 44 | #define BCM283X_MU_LCR_DATA_SIZE_8 3 |
| 45 | |
| 46 | #define BCM283X_MU_LSR_TX_IDLE BIT(6) |
| 47 | /* This actually means not full, but is named not empty in the docs */ |
| 48 | #define BCM283X_MU_LSR_TX_EMPTY BIT(5) |
| 49 | #define BCM283X_MU_LSR_RX_READY BIT(0) |
| 50 | |
| 51 | struct bcm283x_mu_priv { |
| 52 | struct bcm283x_mu_regs *regs; |
| 53 | }; |
| 54 | |
| 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 | |
| 62 | if (plat->skip_init) |
| 63 | return 0; |
| 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 | |
| 70 | return 0; |
| 71 | } |
| 72 | |
| 73 | static int bcm283x_mu_serial_probe(struct udevice *dev) |
| 74 | { |
| 75 | struct bcm283x_mu_serial_platdata *plat = dev_get_platdata(dev); |
| 76 | struct bcm283x_mu_priv *priv = dev_get_priv(dev); |
| 77 | |
Alexander Graf | 601147b | 2016-08-15 17:48:51 +0200 | [diff] [blame] | 78 | if (plat->disabled) |
| 79 | return -ENODEV; |
| 80 | |
Stephen Warren | 3917c26 | 2016-03-18 21:41:38 -0600 | [diff] [blame] | 81 | priv->regs = (struct bcm283x_mu_regs *)plat->base; |
| 82 | |
| 83 | return 0; |
| 84 | } |
| 85 | |
| 86 | static int bcm283x_mu_serial_getc(struct udevice *dev) |
| 87 | { |
| 88 | struct bcm283x_mu_priv *priv = dev_get_priv(dev); |
| 89 | struct bcm283x_mu_regs *regs = priv->regs; |
| 90 | u32 data; |
| 91 | |
| 92 | /* Wait until there is data in the FIFO */ |
| 93 | if (!(readl(®s->lsr) & BCM283X_MU_LSR_RX_READY)) |
| 94 | return -EAGAIN; |
| 95 | |
| 96 | data = readl(®s->io); |
| 97 | |
| 98 | return (int)data; |
| 99 | } |
| 100 | |
| 101 | static int bcm283x_mu_serial_putc(struct udevice *dev, const char data) |
| 102 | { |
| 103 | struct bcm283x_mu_priv *priv = dev_get_priv(dev); |
| 104 | struct bcm283x_mu_regs *regs = priv->regs; |
| 105 | |
| 106 | /* Wait until there is space in the FIFO */ |
| 107 | if (!(readl(®s->lsr) & BCM283X_MU_LSR_TX_EMPTY)) |
| 108 | return -EAGAIN; |
| 109 | |
| 110 | /* Send the character */ |
| 111 | writel(data, ®s->io); |
| 112 | |
| 113 | return 0; |
| 114 | } |
| 115 | |
| 116 | static int bcm283x_mu_serial_pending(struct udevice *dev, bool input) |
| 117 | { |
| 118 | struct bcm283x_mu_priv *priv = dev_get_priv(dev); |
| 119 | struct bcm283x_mu_regs *regs = priv->regs; |
| 120 | unsigned int lsr = readl(®s->lsr); |
| 121 | |
| 122 | if (input) { |
| 123 | WATCHDOG_RESET(); |
Stephen Warren | e3a46e3 | 2016-04-13 22:29:52 -0600 | [diff] [blame] | 124 | return (lsr & BCM283X_MU_LSR_RX_READY) ? 1 : 0; |
Stephen Warren | 3917c26 | 2016-03-18 21:41:38 -0600 | [diff] [blame] | 125 | } else { |
Stephen Warren | e3a46e3 | 2016-04-13 22:29:52 -0600 | [diff] [blame] | 126 | return (lsr & BCM283X_MU_LSR_TX_IDLE) ? 0 : 1; |
Stephen Warren | 3917c26 | 2016-03-18 21:41:38 -0600 | [diff] [blame] | 127 | } |
| 128 | } |
| 129 | |
| 130 | static const struct dm_serial_ops bcm283x_mu_serial_ops = { |
| 131 | .putc = bcm283x_mu_serial_putc, |
| 132 | .pending = bcm283x_mu_serial_pending, |
| 133 | .getc = bcm283x_mu_serial_getc, |
| 134 | .setbrg = bcm283x_mu_serial_setbrg, |
| 135 | }; |
| 136 | |
Fabian Vogt | 9f755f5 | 2016-09-26 14:26:44 +0200 | [diff] [blame^] | 137 | #if CONFIG_IS_ENABLED(OF_CONTROL) |
| 138 | static const struct udevice_id bcm283x_mu_serial_id[] = { |
| 139 | {.compatible = "brcm,bcm2835-aux-uart"}, |
| 140 | {} |
| 141 | }; |
| 142 | |
| 143 | static int bcm283x_mu_serial_ofdata_to_platdata(struct udevice *dev) |
| 144 | { |
| 145 | struct bcm283x_mu_serial_platdata *plat = dev_get_platdata(dev); |
| 146 | fdt_addr_t addr; |
| 147 | |
| 148 | addr = dev_get_addr(dev); |
| 149 | if (addr == FDT_ADDR_T_NONE) |
| 150 | return -EINVAL; |
| 151 | |
| 152 | plat->base = addr; |
| 153 | plat->clock = fdtdec_get_int(gd->fdt_blob, dev->of_offset, "clock", 1); |
| 154 | plat->skip_init = fdtdec_get_bool(gd->fdt_blob, dev->of_offset, |
| 155 | "skip-init"); |
| 156 | plat->disabled = false; |
| 157 | return 0; |
| 158 | } |
| 159 | #endif |
| 160 | |
Stephen Warren | 3917c26 | 2016-03-18 21:41:38 -0600 | [diff] [blame] | 161 | U_BOOT_DRIVER(serial_bcm283x_mu) = { |
| 162 | .name = "serial_bcm283x_mu", |
| 163 | .id = UCLASS_SERIAL, |
Fabian Vogt | 9f755f5 | 2016-09-26 14:26:44 +0200 | [diff] [blame^] | 164 | .of_match = of_match_ptr(bcm283x_mu_serial_id), |
| 165 | .ofdata_to_platdata = of_match_ptr(bcm283x_mu_serial_ofdata_to_platdata), |
Stephen Warren | 3917c26 | 2016-03-18 21:41:38 -0600 | [diff] [blame] | 166 | .platdata_auto_alloc_size = sizeof(struct bcm283x_mu_serial_platdata), |
| 167 | .probe = bcm283x_mu_serial_probe, |
| 168 | .ops = &bcm283x_mu_serial_ops, |
| 169 | .flags = DM_FLAG_PRE_RELOC, |
| 170 | .priv_auto_alloc_size = sizeof(struct bcm283x_mu_priv), |
| 171 | }; |