blob: 5a1d40e0a6a744ad8079d846c9a7812112c272d3 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Stefan Roese6985d492016-05-17 16:36:00 +02002/*
3 * Copyright (C) 2016 Stefan Roese <sr@denx.de>
Stefan Roese6985d492016-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 Glass8a8d24b2020-12-03 16:55:23 -070011struct mvebu_plat {
Stefan Roese6985d492016-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
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
33static int mvebu_serial_putc(struct udevice *dev, const char ch)
34{
Simon Glass8a8d24b2020-12-03 16:55:23 -070035 struct mvebu_plat *plat = dev_get_plat(dev);
Stefan Roese6985d492016-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 Glass8a8d24b2020-12-03 16:55:23 -070048 struct mvebu_plat *plat = dev_get_plat(dev);
Stefan Roese6985d492016-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 Glass8a8d24b2020-12-03 16:55:23 -070059 struct mvebu_plat *plat = dev_get_plat(dev);
Stefan Roese6985d492016-05-17 16:36:00 +020060 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
68static int mvebu_serial_setbrg(struct udevice *dev, int baudrate)
69{
Simon Glass8a8d24b2020-12-03 16:55:23 -070070 struct mvebu_plat *plat = dev_get_plat(dev);
Stefan Roese6985d492016-05-17 16:36:00 +020071 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
88static int mvebu_serial_probe(struct udevice *dev)
89{
Simon Glass8a8d24b2020-12-03 16:55:23 -070090 struct mvebu_plat *plat = dev_get_plat(dev);
Stefan Roese6985d492016-05-17 16:36:00 +020091 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 Glassd1998a92020-12-03 16:55:21 -0700103static int mvebu_serial_of_to_plat(struct udevice *dev)
Stefan Roese6985d492016-05-17 16:36:00 +0200104{
Simon Glass8a8d24b2020-12-03 16:55:23 -0700105 struct mvebu_plat *plat = dev_get_plat(dev);
Stefan Roese6985d492016-05-17 16:36:00 +0200106
Masahiro Yamada702e57e2020-08-04 14:14:43 +0900107 plat->base = dev_read_addr_ptr(dev);
Stefan Roese6985d492016-05-17 16:36:00 +0200108
109 return 0;
110}
111
112static 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
119static const struct udevice_id mvebu_serial_ids[] = {
120 { .compatible = "marvell,armada-3700-uart" },
121 { }
122};
123
124U_BOOT_DRIVER(serial_mvebu) = {
125 .name = "serial_mvebu",
126 .id = UCLASS_SERIAL,
127 .of_match = mvebu_serial_ids,
Simon Glassd1998a92020-12-03 16:55:21 -0700128 .of_to_plat = mvebu_serial_of_to_plat,
Simon Glass8a8d24b2020-12-03 16:55:23 -0700129 .plat_auto = sizeof(struct mvebu_plat),
Stefan Roese6985d492016-05-17 16:36:00 +0200130 .probe = mvebu_serial_probe,
131 .ops = &mvebu_serial_ops,
Stefan Roese6985d492016-05-17 16:36:00 +0200132};
133
134#ifdef CONFIG_DEBUG_MVEBU_A3700_UART
135
136#include <debug_uart.h>
137
138static 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
162static 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
172DEBUG_UART_FUNCS
173#endif