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 | |
| 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 | |
| 53 | static int bcm283x_mu_serial_setbrg(struct udevice *dev, int baudrate) |
| 54 | { |
| 55 | struct bcm283x_mu_serial_platdata *plat = dev_get_platdata(dev); |
| 56 | struct bcm283x_mu_priv *priv = dev_get_priv(dev); |
| 57 | struct bcm283x_mu_regs *regs = priv->regs; |
| 58 | u32 divider; |
| 59 | |
| 60 | if (plat->skip_init) |
| 61 | return 0; |
| 62 | |
| 63 | divider = plat->clock / (baudrate * 8); |
| 64 | |
| 65 | writel(BCM283X_MU_LCR_DATA_SIZE_8, ®s->lcr); |
| 66 | writel(divider - 1, ®s->baud); |
| 67 | |
| 68 | return 0; |
| 69 | } |
| 70 | |
| 71 | static int bcm283x_mu_serial_probe(struct udevice *dev) |
| 72 | { |
| 73 | struct bcm283x_mu_serial_platdata *plat = dev_get_platdata(dev); |
| 74 | struct bcm283x_mu_priv *priv = dev_get_priv(dev); |
| 75 | |
Alexander Graf | 601147b | 2016-08-15 17:48:51 +0200 | [diff] [blame] | 76 | if (plat->disabled) |
| 77 | return -ENODEV; |
| 78 | |
Stephen Warren | 3917c26 | 2016-03-18 21:41:38 -0600 | [diff] [blame] | 79 | priv->regs = (struct bcm283x_mu_regs *)plat->base; |
| 80 | |
| 81 | return 0; |
| 82 | } |
| 83 | |
| 84 | static int bcm283x_mu_serial_getc(struct udevice *dev) |
| 85 | { |
| 86 | struct bcm283x_mu_priv *priv = dev_get_priv(dev); |
| 87 | struct bcm283x_mu_regs *regs = priv->regs; |
| 88 | u32 data; |
| 89 | |
| 90 | /* Wait until there is data in the FIFO */ |
| 91 | if (!(readl(®s->lsr) & BCM283X_MU_LSR_RX_READY)) |
| 92 | return -EAGAIN; |
| 93 | |
| 94 | data = readl(®s->io); |
| 95 | |
| 96 | return (int)data; |
| 97 | } |
| 98 | |
| 99 | static int bcm283x_mu_serial_putc(struct udevice *dev, const char data) |
| 100 | { |
| 101 | struct bcm283x_mu_priv *priv = dev_get_priv(dev); |
| 102 | struct bcm283x_mu_regs *regs = priv->regs; |
| 103 | |
| 104 | /* Wait until there is space in the FIFO */ |
| 105 | if (!(readl(®s->lsr) & BCM283X_MU_LSR_TX_EMPTY)) |
| 106 | return -EAGAIN; |
| 107 | |
| 108 | /* Send the character */ |
| 109 | writel(data, ®s->io); |
| 110 | |
| 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | static int bcm283x_mu_serial_pending(struct udevice *dev, bool input) |
| 115 | { |
| 116 | struct bcm283x_mu_priv *priv = dev_get_priv(dev); |
| 117 | struct bcm283x_mu_regs *regs = priv->regs; |
| 118 | unsigned int lsr = readl(®s->lsr); |
| 119 | |
| 120 | if (input) { |
| 121 | WATCHDOG_RESET(); |
Stephen Warren | e3a46e3 | 2016-04-13 22:29:52 -0600 | [diff] [blame] | 122 | return (lsr & BCM283X_MU_LSR_RX_READY) ? 1 : 0; |
Stephen Warren | 3917c26 | 2016-03-18 21:41:38 -0600 | [diff] [blame] | 123 | } else { |
Stephen Warren | e3a46e3 | 2016-04-13 22:29:52 -0600 | [diff] [blame] | 124 | return (lsr & BCM283X_MU_LSR_TX_IDLE) ? 0 : 1; |
Stephen Warren | 3917c26 | 2016-03-18 21:41:38 -0600 | [diff] [blame] | 125 | } |
| 126 | } |
| 127 | |
| 128 | static const struct dm_serial_ops bcm283x_mu_serial_ops = { |
| 129 | .putc = bcm283x_mu_serial_putc, |
| 130 | .pending = bcm283x_mu_serial_pending, |
| 131 | .getc = bcm283x_mu_serial_getc, |
| 132 | .setbrg = bcm283x_mu_serial_setbrg, |
| 133 | }; |
| 134 | |
| 135 | U_BOOT_DRIVER(serial_bcm283x_mu) = { |
| 136 | .name = "serial_bcm283x_mu", |
| 137 | .id = UCLASS_SERIAL, |
| 138 | .platdata_auto_alloc_size = sizeof(struct bcm283x_mu_serial_platdata), |
| 139 | .probe = bcm283x_mu_serial_probe, |
| 140 | .ops = &bcm283x_mu_serial_ops, |
| 141 | .flags = DM_FLAG_PRE_RELOC, |
| 142 | .priv_auto_alloc_size = sizeof(struct bcm283x_mu_priv), |
| 143 | }; |