blob: 8f404879a56584774aa520fc9bb6ee9bf50f589f [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>
10
Simon Glassb75b15b2020-12-03 16:55:23 -070011struct mvebu_plat {
Stefan Roese027276a2016-05-17 16:36:00 +020012 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
Pali Rohárabba9dd2021-01-14 15:46:35 +010026#define UART_STATUS_TX_EMPTY 0x40
Stefan Roese027276a2016-05-17 16:36:00 +020027#define UART_STATUS_TXFIFO_FULL 0x800
28
29#define UART_CTRL_RXFIFO_RESET 0x4000
30#define UART_CTRL_TXFIFO_RESET 0x8000
31
32#define CONFIG_UART_BASE_CLOCK 25804800
33
34static int mvebu_serial_putc(struct udevice *dev, const char ch)
35{
Simon Glassb75b15b2020-12-03 16:55:23 -070036 struct mvebu_plat *plat = dev_get_plat(dev);
Stefan Roese027276a2016-05-17 16:36:00 +020037 void __iomem *base = plat->base;
38
39 while (readl(base + UART_STATUS_REG) & UART_STATUS_TXFIFO_FULL)
40 ;
41
42 writel(ch, base + UART_TX_REG);
43
44 return 0;
45}
46
47static int mvebu_serial_getc(struct udevice *dev)
48{
Simon Glassb75b15b2020-12-03 16:55:23 -070049 struct mvebu_plat *plat = dev_get_plat(dev);
Stefan Roese027276a2016-05-17 16:36:00 +020050 void __iomem *base = plat->base;
51
52 while (!(readl(base + UART_STATUS_REG) & UART_STATUS_RX_RDY))
53 ;
54
55 return readl(base + UART_RX_REG) & 0xff;
56}
57
58static int mvebu_serial_pending(struct udevice *dev, bool input)
59{
Simon Glassb75b15b2020-12-03 16:55:23 -070060 struct mvebu_plat *plat = dev_get_plat(dev);
Stefan Roese027276a2016-05-17 16:36:00 +020061 void __iomem *base = plat->base;
62
Pali Rohárabba9dd2021-01-14 15:46:35 +010063 if (input) {
64 if (readl(base + UART_STATUS_REG) & UART_STATUS_RX_RDY)
65 return 1;
66 } else {
67 if (!(readl(base + UART_STATUS_REG) & UART_STATUS_TX_EMPTY))
68 return 1;
69 }
Stefan Roese027276a2016-05-17 16:36:00 +020070
71 return 0;
72}
73
74static int mvebu_serial_setbrg(struct udevice *dev, int baudrate)
75{
Simon Glassb75b15b2020-12-03 16:55:23 -070076 struct mvebu_plat *plat = dev_get_plat(dev);
Stefan Roese027276a2016-05-17 16:36:00 +020077 void __iomem *base = plat->base;
78
79 /*
80 * Calculate divider
81 * baudrate = clock / 16 / divider
82 */
83 writel(CONFIG_UART_BASE_CLOCK / baudrate / 16, base + UART_BAUD_REG);
84
85 /*
86 * Set Programmable Oversampling Stack to 0,
87 * UART defaults to 16x scheme
88 */
89 writel(0, base + UART_POSSR_REG);
90
91 return 0;
92}
93
94static int mvebu_serial_probe(struct udevice *dev)
95{
Simon Glassb75b15b2020-12-03 16:55:23 -070096 struct mvebu_plat *plat = dev_get_plat(dev);
Stefan Roese027276a2016-05-17 16:36:00 +020097 void __iomem *base = plat->base;
98
99 /* reset FIFOs */
100 writel(UART_CTRL_RXFIFO_RESET | UART_CTRL_TXFIFO_RESET,
101 base + UART_CTRL_REG);
102
103 /* No Parity, 1 Stop */
104 writel(0, base + UART_CTRL_REG);
105
106 return 0;
107}
108
Simon Glassaad29ae2020-12-03 16:55:21 -0700109static int mvebu_serial_of_to_plat(struct udevice *dev)
Stefan Roese027276a2016-05-17 16:36:00 +0200110{
Simon Glassb75b15b2020-12-03 16:55:23 -0700111 struct mvebu_plat *plat = dev_get_plat(dev);
Stefan Roese027276a2016-05-17 16:36:00 +0200112
Masahiro Yamada32822d02020-08-04 14:14:43 +0900113 plat->base = dev_read_addr_ptr(dev);
Stefan Roese027276a2016-05-17 16:36:00 +0200114
115 return 0;
116}
117
118static const struct dm_serial_ops mvebu_serial_ops = {
119 .putc = mvebu_serial_putc,
120 .pending = mvebu_serial_pending,
121 .getc = mvebu_serial_getc,
122 .setbrg = mvebu_serial_setbrg,
123};
124
125static const struct udevice_id mvebu_serial_ids[] = {
126 { .compatible = "marvell,armada-3700-uart" },
127 { }
128};
129
130U_BOOT_DRIVER(serial_mvebu) = {
131 .name = "serial_mvebu",
132 .id = UCLASS_SERIAL,
133 .of_match = mvebu_serial_ids,
Simon Glassaad29ae2020-12-03 16:55:21 -0700134 .of_to_plat = mvebu_serial_of_to_plat,
Simon Glassb75b15b2020-12-03 16:55:23 -0700135 .plat_auto = sizeof(struct mvebu_plat),
Stefan Roese027276a2016-05-17 16:36:00 +0200136 .probe = mvebu_serial_probe,
137 .ops = &mvebu_serial_ops,
Stefan Roese027276a2016-05-17 16:36:00 +0200138};
139
140#ifdef CONFIG_DEBUG_MVEBU_A3700_UART
141
142#include <debug_uart.h>
143
144static inline void _debug_uart_init(void)
145{
146 void __iomem *base = (void __iomem *)CONFIG_DEBUG_UART_BASE;
147
148 /* reset FIFOs */
149 writel(UART_CTRL_RXFIFO_RESET | UART_CTRL_TXFIFO_RESET,
150 base + UART_CTRL_REG);
151
152 /* No Parity, 1 Stop */
153 writel(0, base + UART_CTRL_REG);
154
155 /*
156 * Calculate divider
157 * baudrate = clock / 16 / divider
158 */
159 writel(CONFIG_UART_BASE_CLOCK / 115200 / 16, base + UART_BAUD_REG);
160
161 /*
162 * Set Programmable Oversampling Stack to 0,
163 * UART defaults to 16x scheme
164 */
165 writel(0, base + UART_POSSR_REG);
166}
167
168static inline void _debug_uart_putc(int ch)
169{
170 void __iomem *base = (void __iomem *)CONFIG_DEBUG_UART_BASE;
171
172 while (readl(base + UART_STATUS_REG) & UART_STATUS_TXFIFO_FULL)
173 ;
174
175 writel(ch, base + UART_TX_REG);
176}
177
178DEBUG_UART_FUNCS
179#endif