Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Stefan Roese | 6985d49 | 2016-05-17 16:36:00 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2016 Stefan Roese <sr@denx.de> |
Stefan Roese | 6985d49 | 2016-05-17 16:36:00 +0200 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
| 7 | #include <dm.h> |
| 8 | #include <serial.h> |
| 9 | #include <asm/io.h> |
| 10 | |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 11 | struct mvebu_plat { |
Stefan Roese | 6985d49 | 2016-05-17 16:36:00 +0200 | [diff] [blame] | 12 | void __iomem *base; |
| 13 | }; |
| 14 | |
| 15 | /* |
| 16 | * Register offset |
| 17 | */ |
| 18 | #define UART_RX_REG 0x00 |
| 19 | #define UART_TX_REG 0x04 |
| 20 | #define UART_CTRL_REG 0x08 |
| 21 | #define UART_STATUS_REG 0x0c |
| 22 | #define UART_BAUD_REG 0x10 |
| 23 | #define UART_POSSR_REG 0x14 |
| 24 | |
| 25 | #define UART_STATUS_RX_RDY 0x10 |
| 26 | #define UART_STATUS_TXFIFO_FULL 0x800 |
| 27 | |
| 28 | #define UART_CTRL_RXFIFO_RESET 0x4000 |
| 29 | #define UART_CTRL_TXFIFO_RESET 0x8000 |
| 30 | |
| 31 | #define CONFIG_UART_BASE_CLOCK 25804800 |
| 32 | |
| 33 | static int mvebu_serial_putc(struct udevice *dev, const char ch) |
| 34 | { |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 35 | struct mvebu_plat *plat = dev_get_plat(dev); |
Stefan Roese | 6985d49 | 2016-05-17 16:36:00 +0200 | [diff] [blame] | 36 | void __iomem *base = plat->base; |
| 37 | |
| 38 | while (readl(base + UART_STATUS_REG) & UART_STATUS_TXFIFO_FULL) |
| 39 | ; |
| 40 | |
| 41 | writel(ch, base + UART_TX_REG); |
| 42 | |
| 43 | return 0; |
| 44 | } |
| 45 | |
| 46 | static int mvebu_serial_getc(struct udevice *dev) |
| 47 | { |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 48 | struct mvebu_plat *plat = dev_get_plat(dev); |
Stefan Roese | 6985d49 | 2016-05-17 16:36:00 +0200 | [diff] [blame] | 49 | void __iomem *base = plat->base; |
| 50 | |
| 51 | while (!(readl(base + UART_STATUS_REG) & UART_STATUS_RX_RDY)) |
| 52 | ; |
| 53 | |
| 54 | return readl(base + UART_RX_REG) & 0xff; |
| 55 | } |
| 56 | |
| 57 | static int mvebu_serial_pending(struct udevice *dev, bool input) |
| 58 | { |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 59 | struct mvebu_plat *plat = dev_get_plat(dev); |
Stefan Roese | 6985d49 | 2016-05-17 16:36:00 +0200 | [diff] [blame] | 60 | void __iomem *base = plat->base; |
| 61 | |
| 62 | if (readl(base + UART_STATUS_REG) & UART_STATUS_RX_RDY) |
| 63 | return 1; |
| 64 | |
| 65 | return 0; |
| 66 | } |
| 67 | |
| 68 | static int mvebu_serial_setbrg(struct udevice *dev, int baudrate) |
| 69 | { |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 70 | struct mvebu_plat *plat = dev_get_plat(dev); |
Stefan Roese | 6985d49 | 2016-05-17 16:36:00 +0200 | [diff] [blame] | 71 | void __iomem *base = plat->base; |
| 72 | |
| 73 | /* |
| 74 | * Calculate divider |
| 75 | * baudrate = clock / 16 / divider |
| 76 | */ |
| 77 | writel(CONFIG_UART_BASE_CLOCK / baudrate / 16, base + UART_BAUD_REG); |
| 78 | |
| 79 | /* |
| 80 | * Set Programmable Oversampling Stack to 0, |
| 81 | * UART defaults to 16x scheme |
| 82 | */ |
| 83 | writel(0, base + UART_POSSR_REG); |
| 84 | |
| 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | static int mvebu_serial_probe(struct udevice *dev) |
| 89 | { |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 90 | struct mvebu_plat *plat = dev_get_plat(dev); |
Stefan Roese | 6985d49 | 2016-05-17 16:36:00 +0200 | [diff] [blame] | 91 | void __iomem *base = plat->base; |
| 92 | |
| 93 | /* reset FIFOs */ |
| 94 | writel(UART_CTRL_RXFIFO_RESET | UART_CTRL_TXFIFO_RESET, |
| 95 | base + UART_CTRL_REG); |
| 96 | |
| 97 | /* No Parity, 1 Stop */ |
| 98 | writel(0, base + UART_CTRL_REG); |
| 99 | |
| 100 | return 0; |
| 101 | } |
| 102 | |
Simon Glass | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 103 | static int mvebu_serial_of_to_plat(struct udevice *dev) |
Stefan Roese | 6985d49 | 2016-05-17 16:36:00 +0200 | [diff] [blame] | 104 | { |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 105 | struct mvebu_plat *plat = dev_get_plat(dev); |
Stefan Roese | 6985d49 | 2016-05-17 16:36:00 +0200 | [diff] [blame] | 106 | |
Masahiro Yamada | 702e57e | 2020-08-04 14:14:43 +0900 | [diff] [blame] | 107 | plat->base = dev_read_addr_ptr(dev); |
Stefan Roese | 6985d49 | 2016-05-17 16:36:00 +0200 | [diff] [blame] | 108 | |
| 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | static const struct dm_serial_ops mvebu_serial_ops = { |
| 113 | .putc = mvebu_serial_putc, |
| 114 | .pending = mvebu_serial_pending, |
| 115 | .getc = mvebu_serial_getc, |
| 116 | .setbrg = mvebu_serial_setbrg, |
| 117 | }; |
| 118 | |
| 119 | static const struct udevice_id mvebu_serial_ids[] = { |
| 120 | { .compatible = "marvell,armada-3700-uart" }, |
| 121 | { } |
| 122 | }; |
| 123 | |
| 124 | U_BOOT_DRIVER(serial_mvebu) = { |
| 125 | .name = "serial_mvebu", |
| 126 | .id = UCLASS_SERIAL, |
| 127 | .of_match = mvebu_serial_ids, |
Simon Glass | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 128 | .of_to_plat = mvebu_serial_of_to_plat, |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 129 | .plat_auto = sizeof(struct mvebu_plat), |
Stefan Roese | 6985d49 | 2016-05-17 16:36:00 +0200 | [diff] [blame] | 130 | .probe = mvebu_serial_probe, |
| 131 | .ops = &mvebu_serial_ops, |
Stefan Roese | 6985d49 | 2016-05-17 16:36:00 +0200 | [diff] [blame] | 132 | }; |
| 133 | |
| 134 | #ifdef CONFIG_DEBUG_MVEBU_A3700_UART |
| 135 | |
| 136 | #include <debug_uart.h> |
| 137 | |
| 138 | static inline void _debug_uart_init(void) |
| 139 | { |
| 140 | void __iomem *base = (void __iomem *)CONFIG_DEBUG_UART_BASE; |
| 141 | |
| 142 | /* reset FIFOs */ |
| 143 | writel(UART_CTRL_RXFIFO_RESET | UART_CTRL_TXFIFO_RESET, |
| 144 | base + UART_CTRL_REG); |
| 145 | |
| 146 | /* No Parity, 1 Stop */ |
| 147 | writel(0, base + UART_CTRL_REG); |
| 148 | |
| 149 | /* |
| 150 | * Calculate divider |
| 151 | * baudrate = clock / 16 / divider |
| 152 | */ |
| 153 | writel(CONFIG_UART_BASE_CLOCK / 115200 / 16, base + UART_BAUD_REG); |
| 154 | |
| 155 | /* |
| 156 | * Set Programmable Oversampling Stack to 0, |
| 157 | * UART defaults to 16x scheme |
| 158 | */ |
| 159 | writel(0, base + UART_POSSR_REG); |
| 160 | } |
| 161 | |
| 162 | static inline void _debug_uart_putc(int ch) |
| 163 | { |
| 164 | void __iomem *base = (void __iomem *)CONFIG_DEBUG_UART_BASE; |
| 165 | |
| 166 | while (readl(base + UART_STATUS_REG) & UART_STATUS_TXFIFO_FULL) |
| 167 | ; |
| 168 | |
| 169 | writel(ch, base + UART_TX_REG); |
| 170 | } |
| 171 | |
| 172 | DEBUG_UART_FUNCS |
| 173 | #endif |