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