blob: 9e7e479f80d486f23b0741fbc647d91fff0c08da [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Stefan Roese027276a2016-05-17 16:36:00 +02002/*
3 * Copyright (C) 2016 Stefan Roese <sr@denx.de>
Stefan Roese027276a2016-05-17 16:36:00 +02004 */
5
6#include <common.h>
7#include <dm.h>
8#include <serial.h>
9#include <asm/io.h>
Pali Rohárb76d08c2021-05-25 19:42:38 +020010#include <asm/arch/cpu.h>
Stefan Roese027276a2016-05-17 16:36:00 +020011
Simon Glassb75b15b2020-12-03 16:55:23 -070012struct mvebu_plat {
Stefan Roese027276a2016-05-17 16:36:00 +020013 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árabba9dd2021-01-14 15:46:35 +010027#define UART_STATUS_TX_EMPTY 0x40
Stefan Roese027276a2016-05-17 16:36:00 +020028#define UART_STATUS_TXFIFO_FULL 0x800
29
30#define UART_CTRL_RXFIFO_RESET 0x4000
31#define UART_CTRL_TXFIFO_RESET 0x8000
32
Stefan Roese027276a2016-05-17 16:36:00 +020033static int mvebu_serial_putc(struct udevice *dev, const char ch)
34{
Simon Glassb75b15b2020-12-03 16:55:23 -070035 struct mvebu_plat *plat = dev_get_plat(dev);
Stefan Roese027276a2016-05-17 16:36:00 +020036 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
46static int mvebu_serial_getc(struct udevice *dev)
47{
Simon Glassb75b15b2020-12-03 16:55:23 -070048 struct mvebu_plat *plat = dev_get_plat(dev);
Stefan Roese027276a2016-05-17 16:36:00 +020049 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
57static int mvebu_serial_pending(struct udevice *dev, bool input)
58{
Simon Glassb75b15b2020-12-03 16:55:23 -070059 struct mvebu_plat *plat = dev_get_plat(dev);
Stefan Roese027276a2016-05-17 16:36:00 +020060 void __iomem *base = plat->base;
61
Pali Rohárabba9dd2021-01-14 15:46:35 +010062 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 Roese027276a2016-05-17 16:36:00 +020069
70 return 0;
71}
72
73static int mvebu_serial_setbrg(struct udevice *dev, int baudrate)
74{
Simon Glassb75b15b2020-12-03 16:55:23 -070075 struct mvebu_plat *plat = dev_get_plat(dev);
Stefan Roese027276a2016-05-17 16:36:00 +020076 void __iomem *base = plat->base;
Pali Rohárb76d08c2021-05-25 19:42:38 +020077 u32 parent_rate, divider;
Stefan Roese027276a2016-05-17 16:36:00 +020078
79 /*
80 * Calculate divider
81 * baudrate = clock / 16 / divider
82 */
Pali Rohárb76d08c2021-05-25 19:42:38 +020083 parent_rate = get_ref_clk() * 1000000;
84 divider = DIV_ROUND_CLOSEST(parent_rate, baudrate * 16);
85 writel(divider, base + UART_BAUD_REG);
Stefan Roese027276a2016-05-17 16:36:00 +020086
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
96static int mvebu_serial_probe(struct udevice *dev)
97{
Simon Glassb75b15b2020-12-03 16:55:23 -070098 struct mvebu_plat *plat = dev_get_plat(dev);
Stefan Roese027276a2016-05-17 16:36:00 +020099 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 Glassaad29ae2020-12-03 16:55:21 -0700111static int mvebu_serial_of_to_plat(struct udevice *dev)
Stefan Roese027276a2016-05-17 16:36:00 +0200112{
Simon Glassb75b15b2020-12-03 16:55:23 -0700113 struct mvebu_plat *plat = dev_get_plat(dev);
Stefan Roese027276a2016-05-17 16:36:00 +0200114
Masahiro Yamada32822d02020-08-04 14:14:43 +0900115 plat->base = dev_read_addr_ptr(dev);
Stefan Roese027276a2016-05-17 16:36:00 +0200116
117 return 0;
118}
119
120static 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
127static const struct udevice_id mvebu_serial_ids[] = {
128 { .compatible = "marvell,armada-3700-uart" },
129 { }
130};
131
132U_BOOT_DRIVER(serial_mvebu) = {
133 .name = "serial_mvebu",
134 .id = UCLASS_SERIAL,
135 .of_match = mvebu_serial_ids,
Simon Glassaad29ae2020-12-03 16:55:21 -0700136 .of_to_plat = mvebu_serial_of_to_plat,
Simon Glassb75b15b2020-12-03 16:55:23 -0700137 .plat_auto = sizeof(struct mvebu_plat),
Stefan Roese027276a2016-05-17 16:36:00 +0200138 .probe = mvebu_serial_probe,
139 .ops = &mvebu_serial_ops,
Stefan Roese027276a2016-05-17 16:36:00 +0200140};
141
142#ifdef CONFIG_DEBUG_MVEBU_A3700_UART
143
144#include <debug_uart.h>
145
146static inline void _debug_uart_init(void)
147{
148 void __iomem *base = (void __iomem *)CONFIG_DEBUG_UART_BASE;
Pali Rohárb76d08c2021-05-25 19:42:38 +0200149 u32 baudrate, parent_rate, divider;
Stefan Roese027276a2016-05-17 16:36:00 +0200150
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árb76d08c2021-05-25 19:42:38 +0200162 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 Roese027276a2016-05-17 16:36:00 +0200166
167 /*
168 * Set Programmable Oversampling Stack to 0,
169 * UART defaults to 16x scheme
170 */
171 writel(0, base + UART_POSSR_REG);
172}
173
174static 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
184DEBUG_UART_FUNCS
185#endif