Jason Li | adca83a | 2020-01-30 12:34:58 -0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * (C) Copyright 2020 Cortina-Access Ltd. |
| 4 | * Common UART Driver for Cortina Access CAxxxx line of SoCs |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #include <common.h> |
| 9 | #include <dm.h> |
| 10 | #include <errno.h> |
| 11 | #include <watchdog.h> |
| 12 | #include <asm/io.h> |
| 13 | #include <serial.h> |
| 14 | #include <linux/compiler.h> |
| 15 | |
| 16 | /* Register definitions */ |
| 17 | #define UCFG 0x00 /* UART config register */ |
| 18 | #define UFC 0x04 /* Flow Control */ |
| 19 | #define URX_SAMPLE 0x08 /* UART RX Sample register */ |
| 20 | #define URT_TUNE 0x0C /* Fine tune of UART clk */ |
| 21 | #define UTX_DATA 0x10 /* UART TX Character data */ |
| 22 | #define URX_DATA 0x14 /* UART RX Character data */ |
| 23 | #define UINFO 0x18 /* UART Info */ |
| 24 | #define UINT_EN0 0x1C /* UART Interrupt enable 0 */ |
| 25 | #define UINT_EN1 0x20 /* UART Interrupt enable 1 */ |
| 26 | #define UINT0 0x24 /* UART Interrupt 0 setting/clearing */ |
| 27 | #define UINT1 0x28 /* UART Interrupt 1 setting/clearing */ |
| 28 | #define UINT_STAT 0x2C /* UART Interrupt Status */ |
| 29 | |
| 30 | /* UART Control Register Bit Fields */ |
| 31 | #define UCFG_BAUD_COUNT_MASK 0xFFFFFF00 |
| 32 | #define UCFG_BAUD_COUNT(x) ((x << 8) & UCFG_BAUD_COUNT_MASK) |
| 33 | #define UCFG_EN BIT(7) |
| 34 | #define UCFG_RX_EN BIT(6) |
| 35 | #define UCFG_TX_EN BIT(5) |
| 36 | #define UCFG_PARITY_EN BIT(4) |
| 37 | #define UCFG_PARITY_SEL BIT(3) |
| 38 | #define UCFG_2STOP_BIT BIT(2) |
| 39 | #define UCFG_CNT1 BIT(1) |
| 40 | #define UCFG_CNT0 BIT(0) |
| 41 | #define UCFG_CHAR_5 0 |
| 42 | #define UCFG_CHAR_6 1 |
| 43 | #define UCFG_CHAR_7 2 |
| 44 | #define UCFG_CHAR_8 3 |
| 45 | |
| 46 | #define UINFO_TX_FIFO_EMPTY BIT(3) |
| 47 | #define UINFO_TX_FIFO_FULL BIT(2) |
| 48 | #define UINFO_RX_FIFO_EMPTY BIT(1) |
| 49 | #define UINFO_RX_FIFO_FULL BIT(0) |
| 50 | |
| 51 | #define UINT_RX_NON_EMPTY BIT(6) |
| 52 | #define UINT_TX_EMPTY BIT(5) |
| 53 | #define UINT_RX_UNDERRUN BIT(4) |
| 54 | #define UINT_RX_OVERRUN BIT(3) |
| 55 | #define UINT_RX_PARITY_ERR BIT(2) |
| 56 | #define UINT_RX_STOP_ERR BIT(1) |
| 57 | #define UINT_TX_OVERRUN BIT(0) |
| 58 | #define UINT_MASK_ALL 0x7F |
| 59 | |
| 60 | struct ca_uart_priv { |
| 61 | void __iomem *base; |
| 62 | }; |
| 63 | |
| 64 | int ca_serial_setbrg(struct udevice *dev, int baudrate) |
| 65 | { |
| 66 | struct ca_uart_priv *priv = dev_get_priv(dev); |
| 67 | unsigned int uart_ctrl, baud, sample; |
| 68 | |
| 69 | baud = CORTINA_UART_CLOCK / baudrate; |
| 70 | |
| 71 | uart_ctrl = readl(priv->base + UCFG); |
| 72 | uart_ctrl &= ~UCFG_BAUD_COUNT_MASK; |
| 73 | uart_ctrl |= UCFG_BAUD_COUNT(baud); |
| 74 | writel(uart_ctrl, priv->base + UCFG); |
| 75 | |
| 76 | sample = baud / 2; |
| 77 | sample = (sample < 7) ? 7 : sample; |
| 78 | writel(sample, priv->base + URX_SAMPLE); |
| 79 | |
| 80 | return 0; |
| 81 | } |
| 82 | |
| 83 | static int ca_serial_getc(struct udevice *dev) |
| 84 | { |
| 85 | struct ca_uart_priv *priv = dev_get_priv(dev); |
| 86 | int ch; |
| 87 | |
| 88 | ch = readl(priv->base + URX_DATA) & 0xFF; |
| 89 | |
| 90 | return (int)ch; |
| 91 | } |
| 92 | |
| 93 | static int ca_serial_putc(struct udevice *dev, const char ch) |
| 94 | { |
| 95 | struct ca_uart_priv *priv = dev_get_priv(dev); |
| 96 | unsigned int status; |
| 97 | |
| 98 | /* Retry if TX FIFO full */ |
| 99 | status = readl(priv->base + UINFO); |
| 100 | if (status & UINFO_TX_FIFO_FULL) |
| 101 | return -EAGAIN; |
| 102 | |
| 103 | writel(ch, priv->base + UTX_DATA); |
| 104 | |
| 105 | return 0; |
| 106 | } |
| 107 | |
| 108 | static int ca_serial_pending(struct udevice *dev, bool input) |
| 109 | { |
| 110 | struct ca_uart_priv *priv = dev_get_priv(dev); |
| 111 | unsigned int status; |
| 112 | |
| 113 | status = readl(priv->base + UINFO); |
| 114 | |
| 115 | if (input) |
| 116 | return (status & UINFO_RX_FIFO_EMPTY) ? 0 : 1; |
| 117 | else |
| 118 | return (status & UINFO_TX_FIFO_FULL) ? 1 : 0; |
| 119 | } |
| 120 | |
| 121 | static int ca_serial_probe(struct udevice *dev) |
| 122 | { |
| 123 | struct ca_uart_priv *priv = dev_get_priv(dev); |
| 124 | u32 uart_ctrl; |
| 125 | |
| 126 | /* Set data, parity and stop bits */ |
| 127 | uart_ctrl = UCFG_EN | UCFG_TX_EN | UCFG_RX_EN | UCFG_CHAR_8; |
| 128 | writel(uart_ctrl, priv->base + UCFG); |
| 129 | |
| 130 | return 0; |
| 131 | } |
| 132 | |
| 133 | static int ca_serial_ofdata_to_platdata(struct udevice *dev) |
| 134 | { |
| 135 | struct ca_uart_priv *priv = dev_get_priv(dev); |
| 136 | |
| 137 | priv->base = dev_remap_addr_index(dev, 0); |
| 138 | if (!priv->base) |
| 139 | return -ENOENT; |
| 140 | |
| 141 | return 0; |
| 142 | } |
| 143 | |
| 144 | static const struct dm_serial_ops ca_serial_ops = { |
| 145 | .putc = ca_serial_putc, |
| 146 | .pending = ca_serial_pending, |
| 147 | .getc = ca_serial_getc, |
| 148 | .setbrg = ca_serial_setbrg, |
| 149 | }; |
| 150 | |
| 151 | static const struct udevice_id ca_serial_ids[] = { |
| 152 | {.compatible = "cortina,ca-uart"}, |
| 153 | {} |
| 154 | }; |
| 155 | |
| 156 | U_BOOT_DRIVER(serial_cortina) = { |
| 157 | .name = "serial_cortina", |
| 158 | .id = UCLASS_SERIAL, |
| 159 | .of_match = ca_serial_ids, |
| 160 | .ofdata_to_platdata = ca_serial_ofdata_to_platdata, |
| 161 | .priv_auto_alloc_size = sizeof(struct ca_uart_priv), |
| 162 | .probe = ca_serial_probe, |
| 163 | .ops = &ca_serial_ops |
| 164 | }; |