blob: 1789a6651ec6f15c5c5d46171ff4b7433877d5d3 [file] [log] [blame]
Greg Kroah-Hartmane3b3d0f2017-11-06 18:11:51 +01001// SPDX-License-Identifier: GPL-2.0+
Wilson Ding30530792016-02-16 19:14:53 +01002/*
3* ***************************************************************************
Paul Gortmaker89ebc272016-03-13 19:48:52 -04004* Marvell Armada-3700 Serial Driver
5* Author: Wilson Ding <dingwei@marvell.com>
Wilson Ding30530792016-02-16 19:14:53 +01006* Copyright (C) 2015 Marvell International Ltd.
7* ***************************************************************************
Wilson Ding30530792016-02-16 19:14:53 +01008*/
9
10#include <linux/clk.h>
11#include <linux/console.h>
12#include <linux/delay.h>
13#include <linux/device.h>
14#include <linux/init.h>
15#include <linux/io.h>
16#include <linux/iopoll.h>
Wilson Ding30530792016-02-16 19:14:53 +010017#include <linux/of.h>
18#include <linux/of_address.h>
19#include <linux/of_device.h>
20#include <linux/of_irq.h>
21#include <linux/of_platform.h>
22#include <linux/platform_device.h>
23#include <linux/serial.h>
24#include <linux/serial_core.h>
25#include <linux/slab.h>
26#include <linux/tty.h>
27#include <linux/tty_flip.h>
28
29/* Register Map */
Miquel Raynal5218d762017-10-13 11:01:49 +020030#define UART_STD_RBR 0x00
Miquel Raynal53501e02017-10-13 11:01:56 +020031#define UART_EXT_RBR 0x18
Wilson Ding30530792016-02-16 19:14:53 +010032
Miquel Raynal5218d762017-10-13 11:01:49 +020033#define UART_STD_TSH 0x04
Miquel Raynal53501e02017-10-13 11:01:56 +020034#define UART_EXT_TSH 0x1C
Wilson Ding30530792016-02-16 19:14:53 +010035
Miquel Raynal5218d762017-10-13 11:01:49 +020036#define UART_STD_CTRL1 0x08
Miquel Raynal53501e02017-10-13 11:01:56 +020037#define UART_EXT_CTRL1 0x04
Wilson Ding30530792016-02-16 19:14:53 +010038#define CTRL_SOFT_RST BIT(31)
39#define CTRL_TXFIFO_RST BIT(15)
40#define CTRL_RXFIFO_RST BIT(14)
Wilson Ding30530792016-02-16 19:14:53 +010041#define CTRL_SND_BRK_SEQ BIT(11)
Wilson Ding30530792016-02-16 19:14:53 +010042#define CTRL_BRK_DET_INT BIT(3)
43#define CTRL_FRM_ERR_INT BIT(2)
44#define CTRL_PAR_ERR_INT BIT(1)
45#define CTRL_OVR_ERR_INT BIT(0)
Miquel Raynal5218d762017-10-13 11:01:49 +020046#define CTRL_BRK_INT (CTRL_BRK_DET_INT | CTRL_FRM_ERR_INT | \
47 CTRL_PAR_ERR_INT | CTRL_OVR_ERR_INT)
Wilson Ding30530792016-02-16 19:14:53 +010048
Miquel Raynal5218d762017-10-13 11:01:49 +020049#define UART_STD_CTRL2 UART_STD_CTRL1
Miquel Raynal53501e02017-10-13 11:01:56 +020050#define UART_EXT_CTRL2 0x20
Miquel Raynal5218d762017-10-13 11:01:49 +020051#define CTRL_STD_TX_RDY_INT BIT(5)
Miquel Raynal53501e02017-10-13 11:01:56 +020052#define CTRL_EXT_TX_RDY_INT BIT(6)
Miquel Raynal5218d762017-10-13 11:01:49 +020053#define CTRL_STD_RX_RDY_INT BIT(4)
Miquel Raynal53501e02017-10-13 11:01:56 +020054#define CTRL_EXT_RX_RDY_INT BIT(5)
Miquel Raynal5218d762017-10-13 11:01:49 +020055
56#define UART_STAT 0x0C
Wilson Ding30530792016-02-16 19:14:53 +010057#define STAT_TX_FIFO_EMP BIT(13)
Wilson Ding30530792016-02-16 19:14:53 +010058#define STAT_TX_FIFO_FUL BIT(11)
Wilson Ding30530792016-02-16 19:14:53 +010059#define STAT_TX_EMP BIT(6)
Miquel Raynal5218d762017-10-13 11:01:49 +020060#define STAT_STD_TX_RDY BIT(5)
Miquel Raynal53501e02017-10-13 11:01:56 +020061#define STAT_EXT_TX_RDY BIT(15)
Miquel Raynal5218d762017-10-13 11:01:49 +020062#define STAT_STD_RX_RDY BIT(4)
Miquel Raynal53501e02017-10-13 11:01:56 +020063#define STAT_EXT_RX_RDY BIT(14)
Wilson Ding30530792016-02-16 19:14:53 +010064#define STAT_BRK_DET BIT(3)
65#define STAT_FRM_ERR BIT(2)
66#define STAT_PAR_ERR BIT(1)
67#define STAT_OVR_ERR BIT(0)
Colin Ian King0ef5a6e2018-02-23 14:14:51 +000068#define STAT_BRK_ERR (STAT_BRK_DET | STAT_FRM_ERR \
Wilson Ding30530792016-02-16 19:14:53 +010069 | STAT_PAR_ERR | STAT_OVR_ERR)
70
71#define UART_BRDV 0x10
Allen Yan68a0db12017-10-13 11:01:51 +020072#define BRDV_BAUD_MASK 0x3FF
Wilson Ding30530792016-02-16 19:14:53 +010073
Miquel Raynal394e8352018-04-21 16:07:33 +020074#define UART_OSAMP 0x14
Miquel Raynal0e4cf692018-11-23 16:45:29 +010075#define OSAMP_DEFAULT_DIVISOR 16
Miquel Raynal35d7a582018-11-23 16:45:30 +010076#define OSAMP_DIVISORS_MASK 0x3F3F3F3F
Miquel Raynal394e8352018-04-21 16:07:33 +020077
Miquel Raynal3a75e912017-10-13 11:01:55 +020078#define MVEBU_NR_UARTS 2
Wilson Ding30530792016-02-16 19:14:53 +010079
80#define MVEBU_UART_TYPE "mvebu-uart"
Yehuda Yitschak02c33332017-10-13 11:01:47 +020081#define DRIVER_NAME "mvebu_serial"
Wilson Ding30530792016-02-16 19:14:53 +010082
Miquel Raynal95f78762017-10-13 11:01:54 +020083enum {
84 /* Either there is only one summed IRQ... */
85 UART_IRQ_SUM = 0,
86 /* ...or there are two separate IRQ for RX and TX */
87 UART_RX_IRQ = 0,
88 UART_TX_IRQ,
89 UART_IRQ_COUNT
90};
91
92/* Diverging register offsets */
Miquel Raynal5218d762017-10-13 11:01:49 +020093struct uart_regs_layout {
94 unsigned int rbr;
95 unsigned int tsh;
96 unsigned int ctrl;
97 unsigned int intr;
Wilson Ding30530792016-02-16 19:14:53 +010098};
99
Miquel Raynal5218d762017-10-13 11:01:49 +0200100/* Diverging flags */
101struct uart_flags {
102 unsigned int ctrl_tx_rdy_int;
103 unsigned int ctrl_rx_rdy_int;
104 unsigned int stat_tx_rdy;
105 unsigned int stat_rx_rdy;
106};
107
108/* Driver data, a structure for each UART port */
109struct mvebu_uart_driver_data {
110 bool is_ext;
111 struct uart_regs_layout regs;
112 struct uart_flags flags;
113};
114
Miquel Raynal394e8352018-04-21 16:07:33 +0200115/* Saved registers during suspend */
116struct mvebu_uart_pm_regs {
117 unsigned int rbr;
118 unsigned int tsh;
119 unsigned int ctrl;
120 unsigned int intr;
121 unsigned int stat;
122 unsigned int brdv;
123 unsigned int osamp;
124};
125
Miquel Raynal5218d762017-10-13 11:01:49 +0200126/* MVEBU UART driver structure */
127struct mvebu_uart {
128 struct uart_port *port;
129 struct clk *clk;
Miquel Raynal95f78762017-10-13 11:01:54 +0200130 int irq[UART_IRQ_COUNT];
131 unsigned char __iomem *nb;
Miquel Raynal5218d762017-10-13 11:01:49 +0200132 struct mvebu_uart_driver_data *data;
Miquel Raynal394e8352018-04-21 16:07:33 +0200133#if defined(CONFIG_PM)
134 struct mvebu_uart_pm_regs pm_regs;
135#endif /* CONFIG_PM */
Miquel Raynal5218d762017-10-13 11:01:49 +0200136};
137
138static struct mvebu_uart *to_mvuart(struct uart_port *port)
139{
140 return (struct mvebu_uart *)port->private_data;
141}
142
143#define IS_EXTENDED(port) (to_mvuart(port)->data->is_ext)
144
145#define UART_RBR(port) (to_mvuart(port)->data->regs.rbr)
146#define UART_TSH(port) (to_mvuart(port)->data->regs.tsh)
147#define UART_CTRL(port) (to_mvuart(port)->data->regs.ctrl)
148#define UART_INTR(port) (to_mvuart(port)->data->regs.intr)
149
150#define CTRL_TX_RDY_INT(port) (to_mvuart(port)->data->flags.ctrl_tx_rdy_int)
151#define CTRL_RX_RDY_INT(port) (to_mvuart(port)->data->flags.ctrl_rx_rdy_int)
152#define STAT_TX_RDY(port) (to_mvuart(port)->data->flags.stat_tx_rdy)
153#define STAT_RX_RDY(port) (to_mvuart(port)->data->flags.stat_rx_rdy)
154
155static struct uart_port mvebu_uart_ports[MVEBU_NR_UARTS];
156
Wilson Ding30530792016-02-16 19:14:53 +0100157/* Core UART Driver Operations */
158static unsigned int mvebu_uart_tx_empty(struct uart_port *port)
159{
160 unsigned long flags;
161 unsigned int st;
162
163 spin_lock_irqsave(&port->lock, flags);
164 st = readl(port->membase + UART_STAT);
165 spin_unlock_irqrestore(&port->lock, flags);
166
167 return (st & STAT_TX_FIFO_EMP) ? TIOCSER_TEMT : 0;
168}
169
170static unsigned int mvebu_uart_get_mctrl(struct uart_port *port)
171{
172 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
173}
174
175static void mvebu_uart_set_mctrl(struct uart_port *port,
176 unsigned int mctrl)
177{
178/*
179 * Even if we do not support configuring the modem control lines, this
180 * function must be proided to the serial core
181 */
182}
183
184static void mvebu_uart_stop_tx(struct uart_port *port)
185{
Miquel Raynal5218d762017-10-13 11:01:49 +0200186 unsigned int ctl = readl(port->membase + UART_INTR(port));
Wilson Ding30530792016-02-16 19:14:53 +0100187
Miquel Raynal5218d762017-10-13 11:01:49 +0200188 ctl &= ~CTRL_TX_RDY_INT(port);
189 writel(ctl, port->membase + UART_INTR(port));
Wilson Ding30530792016-02-16 19:14:53 +0100190}
191
192static void mvebu_uart_start_tx(struct uart_port *port)
193{
Allen Yan30434b02017-10-13 11:01:53 +0200194 unsigned int ctl;
195 struct circ_buf *xmit = &port->state->xmit;
Wilson Ding30530792016-02-16 19:14:53 +0100196
Allen Yan30434b02017-10-13 11:01:53 +0200197 if (IS_EXTENDED(port) && !uart_circ_empty(xmit)) {
198 writel(xmit->buf[xmit->tail], port->membase + UART_TSH(port));
199 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
200 port->icount.tx++;
201 }
202
203 ctl = readl(port->membase + UART_INTR(port));
Miquel Raynal5218d762017-10-13 11:01:49 +0200204 ctl |= CTRL_TX_RDY_INT(port);
205 writel(ctl, port->membase + UART_INTR(port));
Wilson Ding30530792016-02-16 19:14:53 +0100206}
207
208static void mvebu_uart_stop_rx(struct uart_port *port)
209{
Miquel Raynal5218d762017-10-13 11:01:49 +0200210 unsigned int ctl;
Wilson Ding30530792016-02-16 19:14:53 +0100211
Miquel Raynal5218d762017-10-13 11:01:49 +0200212 ctl = readl(port->membase + UART_CTRL(port));
213 ctl &= ~CTRL_BRK_INT;
214 writel(ctl, port->membase + UART_CTRL(port));
215
216 ctl = readl(port->membase + UART_INTR(port));
217 ctl &= ~CTRL_RX_RDY_INT(port);
218 writel(ctl, port->membase + UART_INTR(port));
Wilson Ding30530792016-02-16 19:14:53 +0100219}
220
221static void mvebu_uart_break_ctl(struct uart_port *port, int brk)
222{
223 unsigned int ctl;
224 unsigned long flags;
225
226 spin_lock_irqsave(&port->lock, flags);
Miquel Raynal5218d762017-10-13 11:01:49 +0200227 ctl = readl(port->membase + UART_CTRL(port));
Wilson Ding30530792016-02-16 19:14:53 +0100228 if (brk == -1)
229 ctl |= CTRL_SND_BRK_SEQ;
230 else
231 ctl &= ~CTRL_SND_BRK_SEQ;
Miquel Raynal5218d762017-10-13 11:01:49 +0200232 writel(ctl, port->membase + UART_CTRL(port));
Wilson Ding30530792016-02-16 19:14:53 +0100233 spin_unlock_irqrestore(&port->lock, flags);
234}
235
236static void mvebu_uart_rx_chars(struct uart_port *port, unsigned int status)
237{
238 struct tty_port *tport = &port->state->port;
239 unsigned char ch = 0;
240 char flag = 0;
241
242 do {
Miquel Raynal5218d762017-10-13 11:01:49 +0200243 if (status & STAT_RX_RDY(port)) {
244 ch = readl(port->membase + UART_RBR(port));
Wilson Ding30530792016-02-16 19:14:53 +0100245 ch &= 0xff;
246 flag = TTY_NORMAL;
247 port->icount.rx++;
248
249 if (status & STAT_PAR_ERR)
250 port->icount.parity++;
251 }
252
253 if (status & STAT_BRK_DET) {
254 port->icount.brk++;
255 status &= ~(STAT_FRM_ERR | STAT_PAR_ERR);
256 if (uart_handle_break(port))
257 goto ignore_char;
258 }
259
260 if (status & STAT_OVR_ERR)
261 port->icount.overrun++;
262
263 if (status & STAT_FRM_ERR)
264 port->icount.frame++;
265
266 if (uart_handle_sysrq_char(port, ch))
267 goto ignore_char;
268
269 if (status & port->ignore_status_mask & STAT_PAR_ERR)
Miquel Raynal5218d762017-10-13 11:01:49 +0200270 status &= ~STAT_RX_RDY(port);
Wilson Ding30530792016-02-16 19:14:53 +0100271
272 status &= port->read_status_mask;
273
274 if (status & STAT_PAR_ERR)
275 flag = TTY_PARITY;
276
277 status &= ~port->ignore_status_mask;
278
Miquel Raynal5218d762017-10-13 11:01:49 +0200279 if (status & STAT_RX_RDY(port))
Wilson Ding30530792016-02-16 19:14:53 +0100280 tty_insert_flip_char(tport, ch, flag);
281
282 if (status & STAT_BRK_DET)
283 tty_insert_flip_char(tport, 0, TTY_BREAK);
284
285 if (status & STAT_FRM_ERR)
286 tty_insert_flip_char(tport, 0, TTY_FRAME);
287
288 if (status & STAT_OVR_ERR)
289 tty_insert_flip_char(tport, 0, TTY_OVERRUN);
290
291ignore_char:
292 status = readl(port->membase + UART_STAT);
Miquel Raynal5218d762017-10-13 11:01:49 +0200293 } while (status & (STAT_RX_RDY(port) | STAT_BRK_DET));
Wilson Ding30530792016-02-16 19:14:53 +0100294
295 tty_flip_buffer_push(tport);
296}
297
298static void mvebu_uart_tx_chars(struct uart_port *port, unsigned int status)
299{
300 struct circ_buf *xmit = &port->state->xmit;
301 unsigned int count;
302 unsigned int st;
303
304 if (port->x_char) {
Miquel Raynal5218d762017-10-13 11:01:49 +0200305 writel(port->x_char, port->membase + UART_TSH(port));
Wilson Ding30530792016-02-16 19:14:53 +0100306 port->icount.tx++;
307 port->x_char = 0;
308 return;
309 }
310
311 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
312 mvebu_uart_stop_tx(port);
313 return;
314 }
315
316 for (count = 0; count < port->fifosize; count++) {
Miquel Raynal5218d762017-10-13 11:01:49 +0200317 writel(xmit->buf[xmit->tail], port->membase + UART_TSH(port));
Wilson Ding30530792016-02-16 19:14:53 +0100318 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
319 port->icount.tx++;
320
321 if (uart_circ_empty(xmit))
322 break;
323
324 st = readl(port->membase + UART_STAT);
325 if (st & STAT_TX_FIFO_FUL)
326 break;
327 }
328
329 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
330 uart_write_wakeup(port);
331
332 if (uart_circ_empty(xmit))
333 mvebu_uart_stop_tx(port);
334}
335
336static irqreturn_t mvebu_uart_isr(int irq, void *dev_id)
337{
338 struct uart_port *port = (struct uart_port *)dev_id;
339 unsigned int st = readl(port->membase + UART_STAT);
340
Miquel Raynal5218d762017-10-13 11:01:49 +0200341 if (st & (STAT_RX_RDY(port) | STAT_OVR_ERR | STAT_FRM_ERR |
Miquel Raynal95f78762017-10-13 11:01:54 +0200342 STAT_BRK_DET))
343 mvebu_uart_rx_chars(port, st);
344
345 if (st & STAT_TX_RDY(port))
346 mvebu_uart_tx_chars(port, st);
347
348 return IRQ_HANDLED;
349}
350
351static irqreturn_t mvebu_uart_rx_isr(int irq, void *dev_id)
352{
353 struct uart_port *port = (struct uart_port *)dev_id;
354 unsigned int st = readl(port->membase + UART_STAT);
355
356 if (st & (STAT_RX_RDY(port) | STAT_OVR_ERR | STAT_FRM_ERR |
Miquel Raynal5218d762017-10-13 11:01:49 +0200357 STAT_BRK_DET))
Wilson Ding30530792016-02-16 19:14:53 +0100358 mvebu_uart_rx_chars(port, st);
359
Miquel Raynal95f78762017-10-13 11:01:54 +0200360 return IRQ_HANDLED;
361}
362
363static irqreturn_t mvebu_uart_tx_isr(int irq, void *dev_id)
364{
365 struct uart_port *port = (struct uart_port *)dev_id;
366 unsigned int st = readl(port->membase + UART_STAT);
367
Miquel Raynal5218d762017-10-13 11:01:49 +0200368 if (st & STAT_TX_RDY(port))
Wilson Ding30530792016-02-16 19:14:53 +0100369 mvebu_uart_tx_chars(port, st);
370
371 return IRQ_HANDLED;
372}
373
374static int mvebu_uart_startup(struct uart_port *port)
375{
Miquel Raynal95f78762017-10-13 11:01:54 +0200376 struct mvebu_uart *mvuart = to_mvuart(port);
Miquel Raynal5218d762017-10-13 11:01:49 +0200377 unsigned int ctl;
Wilson Ding30530792016-02-16 19:14:53 +0100378 int ret;
379
380 writel(CTRL_TXFIFO_RST | CTRL_RXFIFO_RST,
Miquel Raynal5218d762017-10-13 11:01:49 +0200381 port->membase + UART_CTRL(port));
Wilson Ding30530792016-02-16 19:14:53 +0100382 udelay(1);
Allen Yan2ff23c42017-10-13 11:01:52 +0200383
384 /* Clear the error bits of state register before IRQ request */
385 ret = readl(port->membase + UART_STAT);
386 ret |= STAT_BRK_ERR;
387 writel(ret, port->membase + UART_STAT);
388
Miquel Raynal5218d762017-10-13 11:01:49 +0200389 writel(CTRL_BRK_INT, port->membase + UART_CTRL(port));
390
391 ctl = readl(port->membase + UART_INTR(port));
392 ctl |= CTRL_RX_RDY_INT(port);
393 writel(ctl, port->membase + UART_INTR(port));
Wilson Ding30530792016-02-16 19:14:53 +0100394
Miquel Raynal95f78762017-10-13 11:01:54 +0200395 if (!mvuart->irq[UART_TX_IRQ]) {
396 /* Old bindings with just one interrupt (UART0 only) */
397 ret = devm_request_irq(port->dev, mvuart->irq[UART_IRQ_SUM],
398 mvebu_uart_isr, port->irqflags,
399 dev_name(port->dev), port);
400 if (ret) {
401 dev_err(port->dev, "unable to request IRQ %d\n",
402 mvuart->irq[UART_IRQ_SUM]);
403 return ret;
404 }
405 } else {
406 /* New bindings with an IRQ for RX and TX (both UART) */
407 ret = devm_request_irq(port->dev, mvuart->irq[UART_RX_IRQ],
408 mvebu_uart_rx_isr, port->irqflags,
409 dev_name(port->dev), port);
410 if (ret) {
411 dev_err(port->dev, "unable to request IRQ %d\n",
412 mvuart->irq[UART_RX_IRQ]);
413 return ret;
414 }
415
416 ret = devm_request_irq(port->dev, mvuart->irq[UART_TX_IRQ],
417 mvebu_uart_tx_isr, port->irqflags,
418 dev_name(port->dev),
419 port);
420 if (ret) {
421 dev_err(port->dev, "unable to request IRQ %d\n",
422 mvuart->irq[UART_TX_IRQ]);
423 devm_free_irq(port->dev, mvuart->irq[UART_RX_IRQ],
424 port);
425 return ret;
426 }
Wilson Ding30530792016-02-16 19:14:53 +0100427 }
428
429 return 0;
430}
431
432static void mvebu_uart_shutdown(struct uart_port *port)
433{
Miquel Raynal95f78762017-10-13 11:01:54 +0200434 struct mvebu_uart *mvuart = to_mvuart(port);
435
Miquel Raynal5218d762017-10-13 11:01:49 +0200436 writel(0, port->membase + UART_INTR(port));
Thomas Petazzonic2c16592016-06-16 16:48:52 +0200437
Miquel Raynal95f78762017-10-13 11:01:54 +0200438 if (!mvuart->irq[UART_TX_IRQ]) {
439 devm_free_irq(port->dev, mvuart->irq[UART_IRQ_SUM], port);
440 } else {
441 devm_free_irq(port->dev, mvuart->irq[UART_RX_IRQ], port);
442 devm_free_irq(port->dev, mvuart->irq[UART_TX_IRQ], port);
443 }
Wilson Ding30530792016-02-16 19:14:53 +0100444}
445
Allen Yan68a0db12017-10-13 11:01:51 +0200446static int mvebu_uart_baud_rate_set(struct uart_port *port, unsigned int baud)
447{
Miquel Raynal0e4cf692018-11-23 16:45:29 +0100448 unsigned int d_divisor, m_divisor;
Miquel Raynal35d7a582018-11-23 16:45:30 +0100449 u32 brdv, osamp;
Allen Yan68a0db12017-10-13 11:01:51 +0200450
Pali Rohárecd6b012021-06-25 00:49:01 +0200451 if (!port->uartclk)
452 return -EOPNOTSUPP;
Allen Yan68a0db12017-10-13 11:01:51 +0200453
454 /*
Miquel Raynal0e4cf692018-11-23 16:45:29 +0100455 * The baudrate is derived from the UART clock thanks to two divisors:
456 * > D ("baud generator"): can divide the clock from 2 to 2^10 - 1.
457 * > M ("fractional divisor"): allows a better accuracy for
458 * baudrates higher than 230400.
459 *
460 * As the derivation of M is rather complicated, the code sticks to its
461 * default value (x16) when all the prescalers are zeroed, and only
462 * makes use of D to configure the desired baudrate.
Allen Yan68a0db12017-10-13 11:01:51 +0200463 */
Miquel Raynal0e4cf692018-11-23 16:45:29 +0100464 m_divisor = OSAMP_DEFAULT_DIVISOR;
Pali Rohár90782042021-06-25 00:49:00 +0200465 d_divisor = DIV_ROUND_CLOSEST(port->uartclk, baud * m_divisor);
Miquel Raynal0e4cf692018-11-23 16:45:29 +0100466
Allen Yan68a0db12017-10-13 11:01:51 +0200467 brdv = readl(port->membase + UART_BRDV);
468 brdv &= ~BRDV_BAUD_MASK;
Miquel Raynal0e4cf692018-11-23 16:45:29 +0100469 brdv |= d_divisor;
Allen Yan68a0db12017-10-13 11:01:51 +0200470 writel(brdv, port->membase + UART_BRDV);
471
Miquel Raynal35d7a582018-11-23 16:45:30 +0100472 osamp = readl(port->membase + UART_OSAMP);
473 osamp &= ~OSAMP_DIVISORS_MASK;
474 writel(osamp, port->membase + UART_OSAMP);
475
Allen Yan68a0db12017-10-13 11:01:51 +0200476 return 0;
477}
478
Wilson Ding30530792016-02-16 19:14:53 +0100479static void mvebu_uart_set_termios(struct uart_port *port,
480 struct ktermios *termios,
481 struct ktermios *old)
482{
483 unsigned long flags;
484 unsigned int baud;
485
486 spin_lock_irqsave(&port->lock, flags);
487
Miquel Raynal5218d762017-10-13 11:01:49 +0200488 port->read_status_mask = STAT_RX_RDY(port) | STAT_OVR_ERR |
489 STAT_TX_RDY(port) | STAT_TX_FIFO_FUL;
Wilson Ding30530792016-02-16 19:14:53 +0100490
491 if (termios->c_iflag & INPCK)
492 port->read_status_mask |= STAT_FRM_ERR | STAT_PAR_ERR;
493
494 port->ignore_status_mask = 0;
495 if (termios->c_iflag & IGNPAR)
496 port->ignore_status_mask |=
497 STAT_FRM_ERR | STAT_PAR_ERR | STAT_OVR_ERR;
498
499 if ((termios->c_cflag & CREAD) == 0)
Miquel Raynal5218d762017-10-13 11:01:49 +0200500 port->ignore_status_mask |= STAT_RX_RDY(port) | STAT_BRK_ERR;
Wilson Ding30530792016-02-16 19:14:53 +0100501
Allen Yan68a0db12017-10-13 11:01:51 +0200502 /*
503 * Maximum achievable frequency with simple baudrate divisor is 230400.
504 * Since the error per bit frame would be of more than 15%, achieving
505 * higher frequencies would require to implement the fractional divisor
506 * feature.
507 */
508 baud = uart_get_baud_rate(port, termios, old, 0, 230400);
509 if (mvebu_uart_baud_rate_set(port, baud)) {
510 /* No clock available, baudrate cannot be changed */
511 if (old)
512 baud = uart_get_baud_rate(port, old, NULL, 0, 230400);
513 } else {
514 tty_termios_encode_baud_rate(termios, baud, baud);
515 uart_update_timeout(port, termios->c_cflag, baud);
516 }
Wilson Ding30530792016-02-16 19:14:53 +0100517
Allen Yan68a0db12017-10-13 11:01:51 +0200518 /* Only the following flag changes are supported */
519 if (old) {
520 termios->c_iflag &= INPCK | IGNPAR;
521 termios->c_iflag |= old->c_iflag & ~(INPCK | IGNPAR);
522 termios->c_cflag &= CREAD | CBAUD;
523 termios->c_cflag |= old->c_cflag & ~(CREAD | CBAUD);
Jan Kiszkae0bf2d4982018-08-26 19:49:32 +0200524 termios->c_cflag |= CS8;
Allen Yan68a0db12017-10-13 11:01:51 +0200525 }
Wilson Ding30530792016-02-16 19:14:53 +0100526
527 spin_unlock_irqrestore(&port->lock, flags);
528}
529
530static const char *mvebu_uart_type(struct uart_port *port)
531{
532 return MVEBU_UART_TYPE;
533}
534
535static void mvebu_uart_release_port(struct uart_port *port)
536{
537 /* Nothing to do here */
538}
539
540static int mvebu_uart_request_port(struct uart_port *port)
541{
542 return 0;
543}
544
545#ifdef CONFIG_CONSOLE_POLL
546static int mvebu_uart_get_poll_char(struct uart_port *port)
547{
548 unsigned int st = readl(port->membase + UART_STAT);
549
Miquel Raynal5218d762017-10-13 11:01:49 +0200550 if (!(st & STAT_RX_RDY(port)))
Wilson Ding30530792016-02-16 19:14:53 +0100551 return NO_POLL_CHAR;
552
Miquel Raynal5218d762017-10-13 11:01:49 +0200553 return readl(port->membase + UART_RBR(port));
Wilson Ding30530792016-02-16 19:14:53 +0100554}
555
556static void mvebu_uart_put_poll_char(struct uart_port *port, unsigned char c)
557{
558 unsigned int st;
559
560 for (;;) {
561 st = readl(port->membase + UART_STAT);
562
563 if (!(st & STAT_TX_FIFO_FUL))
564 break;
565
566 udelay(1);
567 }
568
Miquel Raynal5218d762017-10-13 11:01:49 +0200569 writel(c, port->membase + UART_TSH(port));
Wilson Ding30530792016-02-16 19:14:53 +0100570}
571#endif
572
573static const struct uart_ops mvebu_uart_ops = {
574 .tx_empty = mvebu_uart_tx_empty,
575 .set_mctrl = mvebu_uart_set_mctrl,
576 .get_mctrl = mvebu_uart_get_mctrl,
577 .stop_tx = mvebu_uart_stop_tx,
578 .start_tx = mvebu_uart_start_tx,
579 .stop_rx = mvebu_uart_stop_rx,
580 .break_ctl = mvebu_uart_break_ctl,
581 .startup = mvebu_uart_startup,
582 .shutdown = mvebu_uart_shutdown,
583 .set_termios = mvebu_uart_set_termios,
584 .type = mvebu_uart_type,
585 .release_port = mvebu_uart_release_port,
586 .request_port = mvebu_uart_request_port,
587#ifdef CONFIG_CONSOLE_POLL
588 .poll_get_char = mvebu_uart_get_poll_char,
589 .poll_put_char = mvebu_uart_put_poll_char,
590#endif
591};
592
593/* Console Driver Operations */
594
595#ifdef CONFIG_SERIAL_MVEBU_CONSOLE
596/* Early Console */
597static void mvebu_uart_putc(struct uart_port *port, int c)
598{
599 unsigned int st;
600
601 for (;;) {
602 st = readl(port->membase + UART_STAT);
603 if (!(st & STAT_TX_FIFO_FUL))
604 break;
605 }
606
Miquel Raynal5218d762017-10-13 11:01:49 +0200607 /* At early stage, DT is not parsed yet, only use UART0 */
608 writel(c, port->membase + UART_STD_TSH);
Wilson Ding30530792016-02-16 19:14:53 +0100609
610 for (;;) {
611 st = readl(port->membase + UART_STAT);
612 if (st & STAT_TX_FIFO_EMP)
613 break;
614 }
615}
616
617static void mvebu_uart_putc_early_write(struct console *con,
618 const char *s,
Jinchao Wang5607fa62021-06-24 10:12:07 +0800619 unsigned int n)
Wilson Ding30530792016-02-16 19:14:53 +0100620{
621 struct earlycon_device *dev = con->data;
622
623 uart_console_write(&dev->port, s, n, mvebu_uart_putc);
624}
625
626static int __init
627mvebu_uart_early_console_setup(struct earlycon_device *device,
628 const char *opt)
629{
630 if (!device->port.membase)
631 return -ENODEV;
632
633 device->con->write = mvebu_uart_putc_early_write;
634
635 return 0;
636}
637
638EARLYCON_DECLARE(ar3700_uart, mvebu_uart_early_console_setup);
639OF_EARLYCON_DECLARE(ar3700_uart, "marvell,armada-3700-uart",
640 mvebu_uart_early_console_setup);
641
642static void wait_for_xmitr(struct uart_port *port)
643{
644 u32 val;
645
646 readl_poll_timeout_atomic(port->membase + UART_STAT, val,
Gabriel Matnic685af12018-03-22 19:15:12 +0000647 (val & STAT_TX_RDY(port)), 1, 10000);
Wilson Ding30530792016-02-16 19:14:53 +0100648}
649
Pali Rohár54ca9552020-12-23 20:19:31 +0100650static void wait_for_xmite(struct uart_port *port)
651{
652 u32 val;
653
654 readl_poll_timeout_atomic(port->membase + UART_STAT, val,
655 (val & STAT_TX_EMP), 1, 10000);
656}
657
Wilson Ding30530792016-02-16 19:14:53 +0100658static void mvebu_uart_console_putchar(struct uart_port *port, int ch)
659{
660 wait_for_xmitr(port);
Miquel Raynal5218d762017-10-13 11:01:49 +0200661 writel(ch, port->membase + UART_TSH(port));
Wilson Ding30530792016-02-16 19:14:53 +0100662}
663
664static void mvebu_uart_console_write(struct console *co, const char *s,
665 unsigned int count)
666{
667 struct uart_port *port = &mvebu_uart_ports[co->index];
668 unsigned long flags;
Miquel Raynal5218d762017-10-13 11:01:49 +0200669 unsigned int ier, intr, ctl;
Wilson Ding30530792016-02-16 19:14:53 +0100670 int locked = 1;
671
672 if (oops_in_progress)
673 locked = spin_trylock_irqsave(&port->lock, flags);
674 else
675 spin_lock_irqsave(&port->lock, flags);
676
Miquel Raynal5218d762017-10-13 11:01:49 +0200677 ier = readl(port->membase + UART_CTRL(port)) & CTRL_BRK_INT;
678 intr = readl(port->membase + UART_INTR(port)) &
679 (CTRL_RX_RDY_INT(port) | CTRL_TX_RDY_INT(port));
680 writel(0, port->membase + UART_CTRL(port));
681 writel(0, port->membase + UART_INTR(port));
Wilson Ding30530792016-02-16 19:14:53 +0100682
683 uart_console_write(port, s, count, mvebu_uart_console_putchar);
684
Pali Rohár54ca9552020-12-23 20:19:31 +0100685 wait_for_xmite(port);
Wilson Ding30530792016-02-16 19:14:53 +0100686
687 if (ier)
Miquel Raynal5218d762017-10-13 11:01:49 +0200688 writel(ier, port->membase + UART_CTRL(port));
689
690 if (intr) {
691 ctl = intr | readl(port->membase + UART_INTR(port));
692 writel(ctl, port->membase + UART_INTR(port));
693 }
Wilson Ding30530792016-02-16 19:14:53 +0100694
695 if (locked)
696 spin_unlock_irqrestore(&port->lock, flags);
697}
698
699static int mvebu_uart_console_setup(struct console *co, char *options)
700{
701 struct uart_port *port;
702 int baud = 9600;
703 int bits = 8;
704 int parity = 'n';
705 int flow = 'n';
706
707 if (co->index < 0 || co->index >= MVEBU_NR_UARTS)
708 return -EINVAL;
709
710 port = &mvebu_uart_ports[co->index];
711
712 if (!port->mapbase || !port->membase) {
713 pr_debug("console on ttyMV%i not present\n", co->index);
714 return -ENODEV;
715 }
716
717 if (options)
718 uart_parse_options(options, &baud, &parity, &bits, &flow);
719
720 return uart_set_options(port, co, baud, parity, bits, flow);
721}
722
723static struct uart_driver mvebu_uart_driver;
724
725static struct console mvebu_uart_console = {
726 .name = "ttyMV",
727 .write = mvebu_uart_console_write,
728 .device = uart_console_device,
729 .setup = mvebu_uart_console_setup,
730 .flags = CON_PRINTBUFFER,
731 .index = -1,
732 .data = &mvebu_uart_driver,
733};
734
735static int __init mvebu_uart_console_init(void)
736{
737 register_console(&mvebu_uart_console);
738 return 0;
739}
740
741console_initcall(mvebu_uart_console_init);
742
743
744#endif /* CONFIG_SERIAL_MVEBU_CONSOLE */
745
746static struct uart_driver mvebu_uart_driver = {
747 .owner = THIS_MODULE,
Yehuda Yitschak02c33332017-10-13 11:01:47 +0200748 .driver_name = DRIVER_NAME,
Wilson Ding30530792016-02-16 19:14:53 +0100749 .dev_name = "ttyMV",
750 .nr = MVEBU_NR_UARTS,
751#ifdef CONFIG_SERIAL_MVEBU_CONSOLE
752 .cons = &mvebu_uart_console,
753#endif
754};
755
Miquel Raynal394e8352018-04-21 16:07:33 +0200756#if defined(CONFIG_PM)
757static int mvebu_uart_suspend(struct device *dev)
758{
759 struct mvebu_uart *mvuart = dev_get_drvdata(dev);
760 struct uart_port *port = mvuart->port;
761
762 uart_suspend_port(&mvebu_uart_driver, port);
763
764 mvuart->pm_regs.rbr = readl(port->membase + UART_RBR(port));
765 mvuart->pm_regs.tsh = readl(port->membase + UART_TSH(port));
766 mvuart->pm_regs.ctrl = readl(port->membase + UART_CTRL(port));
767 mvuart->pm_regs.intr = readl(port->membase + UART_INTR(port));
768 mvuart->pm_regs.stat = readl(port->membase + UART_STAT);
769 mvuart->pm_regs.brdv = readl(port->membase + UART_BRDV);
770 mvuart->pm_regs.osamp = readl(port->membase + UART_OSAMP);
771
772 device_set_wakeup_enable(dev, true);
773
774 return 0;
775}
776
777static int mvebu_uart_resume(struct device *dev)
778{
779 struct mvebu_uart *mvuart = dev_get_drvdata(dev);
780 struct uart_port *port = mvuart->port;
781
782 writel(mvuart->pm_regs.rbr, port->membase + UART_RBR(port));
783 writel(mvuart->pm_regs.tsh, port->membase + UART_TSH(port));
784 writel(mvuart->pm_regs.ctrl, port->membase + UART_CTRL(port));
785 writel(mvuart->pm_regs.intr, port->membase + UART_INTR(port));
786 writel(mvuart->pm_regs.stat, port->membase + UART_STAT);
787 writel(mvuart->pm_regs.brdv, port->membase + UART_BRDV);
788 writel(mvuart->pm_regs.osamp, port->membase + UART_OSAMP);
789
790 uart_resume_port(&mvebu_uart_driver, port);
791
792 return 0;
793}
794
795static const struct dev_pm_ops mvebu_uart_pm_ops = {
796 .suspend = mvebu_uart_suspend,
797 .resume = mvebu_uart_resume,
798};
799#endif /* CONFIG_PM */
800
Miquel Raynal5218d762017-10-13 11:01:49 +0200801static const struct of_device_id mvebu_uart_of_match[];
802
Allen Yan94228f92017-10-13 11:01:48 +0200803/* Counter to keep track of each UART port id when not using CONFIG_OF */
804static int uart_num_counter;
805
Wilson Ding30530792016-02-16 19:14:53 +0100806static int mvebu_uart_probe(struct platform_device *pdev)
807{
808 struct resource *reg = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Miquel Raynal5218d762017-10-13 11:01:49 +0200809 const struct of_device_id *match = of_match_device(mvebu_uart_of_match,
810 &pdev->dev);
Wilson Ding30530792016-02-16 19:14:53 +0100811 struct uart_port *port;
Miquel Raynal5218d762017-10-13 11:01:49 +0200812 struct mvebu_uart *mvuart;
Qinglang Miao58e49342020-09-29 16:56:51 +0800813 int id, irq;
Wilson Ding30530792016-02-16 19:14:53 +0100814
Miquel Raynal95f78762017-10-13 11:01:54 +0200815 if (!reg) {
816 dev_err(&pdev->dev, "no registers defined\n");
Wilson Ding30530792016-02-16 19:14:53 +0100817 return -EINVAL;
818 }
819
Allen Yan94228f92017-10-13 11:01:48 +0200820 /* Assume that all UART ports have a DT alias or none has */
821 id = of_alias_get_id(pdev->dev.of_node, "serial");
822 if (!pdev->dev.of_node || id < 0)
823 pdev->id = uart_num_counter++;
824 else
825 pdev->id = id;
826
827 if (pdev->id >= MVEBU_NR_UARTS) {
828 dev_err(&pdev->dev, "cannot have more than %d UART ports\n",
829 MVEBU_NR_UARTS);
830 return -EINVAL;
831 }
832
833 port = &mvebu_uart_ports[pdev->id];
Wilson Ding30530792016-02-16 19:14:53 +0100834
835 spin_lock_init(&port->lock);
836
837 port->dev = &pdev->dev;
838 port->type = PORT_MVEBU;
839 port->ops = &mvebu_uart_ops;
840 port->regshift = 0;
841
842 port->fifosize = 32;
843 port->iotype = UPIO_MEM32;
844 port->flags = UPF_FIXED_PORT;
Allen Yan94228f92017-10-13 11:01:48 +0200845 port->line = pdev->id;
Wilson Ding30530792016-02-16 19:14:53 +0100846
Miquel Raynal95f78762017-10-13 11:01:54 +0200847 /*
848 * IRQ number is not stored in this structure because we may have two of
849 * them per port (RX and TX). Instead, use the driver UART structure
850 * array so called ->irq[].
851 */
852 port->irq = 0;
Wilson Ding30530792016-02-16 19:14:53 +0100853 port->irqflags = 0;
854 port->mapbase = reg->start;
855
856 port->membase = devm_ioremap_resource(&pdev->dev, reg);
857 if (IS_ERR(port->membase))
tangbin4a3e2082020-03-05 09:38:23 +0800858 return PTR_ERR(port->membase);
Wilson Ding30530792016-02-16 19:14:53 +0100859
Miquel Raynal5218d762017-10-13 11:01:49 +0200860 mvuart = devm_kzalloc(&pdev->dev, sizeof(struct mvebu_uart),
861 GFP_KERNEL);
862 if (!mvuart)
Wilson Ding30530792016-02-16 19:14:53 +0100863 return -ENOMEM;
864
Allen Yan68a0db12017-10-13 11:01:51 +0200865 /* Get controller data depending on the compatible string */
Miquel Raynal5218d762017-10-13 11:01:49 +0200866 mvuart->data = (struct mvebu_uart_driver_data *)match->data;
867 mvuart->port = port;
Wilson Ding30530792016-02-16 19:14:53 +0100868
Miquel Raynal5218d762017-10-13 11:01:49 +0200869 port->private_data = mvuart;
870 platform_set_drvdata(pdev, mvuart);
Wilson Ding30530792016-02-16 19:14:53 +0100871
Allen Yan68a0db12017-10-13 11:01:51 +0200872 /* Get fixed clock frequency */
873 mvuart->clk = devm_clk_get(&pdev->dev, NULL);
874 if (IS_ERR(mvuart->clk)) {
875 if (PTR_ERR(mvuart->clk) == -EPROBE_DEFER)
876 return PTR_ERR(mvuart->clk);
877
878 if (IS_EXTENDED(port)) {
879 dev_err(&pdev->dev, "unable to get UART clock\n");
880 return PTR_ERR(mvuart->clk);
881 }
882 } else {
883 if (!clk_prepare_enable(mvuart->clk))
884 port->uartclk = clk_get_rate(mvuart->clk);
885 }
886
Miquel Raynal95f78762017-10-13 11:01:54 +0200887 /* Manage interrupts */
Miquel Raynal95f78762017-10-13 11:01:54 +0200888 if (platform_irq_count(pdev) == 1) {
889 /* Old bindings: no name on the single unamed UART0 IRQ */
890 irq = platform_get_irq(pdev, 0);
Stephen Boyd1df21782019-07-30 11:15:44 -0700891 if (irq < 0)
Miquel Raynal95f78762017-10-13 11:01:54 +0200892 return irq;
Miquel Raynal95f78762017-10-13 11:01:54 +0200893
894 mvuart->irq[UART_IRQ_SUM] = irq;
895 } else {
896 /*
897 * New bindings: named interrupts (RX, TX) for both UARTS,
898 * only make use of uart-rx and uart-tx interrupts, do not use
899 * uart-sum of UART0 port.
900 */
901 irq = platform_get_irq_byname(pdev, "uart-rx");
Stephen Boyd1df21782019-07-30 11:15:44 -0700902 if (irq < 0)
Miquel Raynal95f78762017-10-13 11:01:54 +0200903 return irq;
Miquel Raynal95f78762017-10-13 11:01:54 +0200904
905 mvuart->irq[UART_RX_IRQ] = irq;
906
907 irq = platform_get_irq_byname(pdev, "uart-tx");
Stephen Boyd1df21782019-07-30 11:15:44 -0700908 if (irq < 0)
Miquel Raynal95f78762017-10-13 11:01:54 +0200909 return irq;
Miquel Raynal95f78762017-10-13 11:01:54 +0200910
911 mvuart->irq[UART_TX_IRQ] = irq;
912 }
913
Allen Yan9c3d3ee2017-10-13 11:01:50 +0200914 /* UART Soft Reset*/
915 writel(CTRL_SOFT_RST, port->membase + UART_CTRL(port));
916 udelay(1);
917 writel(0, port->membase + UART_CTRL(port));
918
Qinglang Miaob6353702020-09-21 21:11:05 +0800919 return uart_add_one_port(&mvebu_uart_driver, port);
Wilson Ding30530792016-02-16 19:14:53 +0100920}
921
Miquel Raynal5218d762017-10-13 11:01:49 +0200922static struct mvebu_uart_driver_data uart_std_driver_data = {
923 .is_ext = false,
924 .regs.rbr = UART_STD_RBR,
925 .regs.tsh = UART_STD_TSH,
926 .regs.ctrl = UART_STD_CTRL1,
927 .regs.intr = UART_STD_CTRL2,
928 .flags.ctrl_tx_rdy_int = CTRL_STD_TX_RDY_INT,
929 .flags.ctrl_rx_rdy_int = CTRL_STD_RX_RDY_INT,
930 .flags.stat_tx_rdy = STAT_STD_TX_RDY,
931 .flags.stat_rx_rdy = STAT_STD_RX_RDY,
932};
933
Miquel Raynal53501e02017-10-13 11:01:56 +0200934static struct mvebu_uart_driver_data uart_ext_driver_data = {
935 .is_ext = true,
936 .regs.rbr = UART_EXT_RBR,
937 .regs.tsh = UART_EXT_TSH,
938 .regs.ctrl = UART_EXT_CTRL1,
939 .regs.intr = UART_EXT_CTRL2,
940 .flags.ctrl_tx_rdy_int = CTRL_EXT_TX_RDY_INT,
941 .flags.ctrl_rx_rdy_int = CTRL_EXT_RX_RDY_INT,
942 .flags.stat_tx_rdy = STAT_EXT_TX_RDY,
943 .flags.stat_rx_rdy = STAT_EXT_RX_RDY,
944};
945
Wilson Ding30530792016-02-16 19:14:53 +0100946/* Match table for of_platform binding */
947static const struct of_device_id mvebu_uart_of_match[] = {
Miquel Raynal5218d762017-10-13 11:01:49 +0200948 {
949 .compatible = "marvell,armada-3700-uart",
950 .data = (void *)&uart_std_driver_data,
951 },
Miquel Raynal53501e02017-10-13 11:01:56 +0200952 {
953 .compatible = "marvell,armada-3700-uart-ext",
954 .data = (void *)&uart_ext_driver_data,
955 },
Wilson Ding30530792016-02-16 19:14:53 +0100956 {}
957};
Wilson Ding30530792016-02-16 19:14:53 +0100958
959static struct platform_driver mvebu_uart_platform_driver = {
960 .probe = mvebu_uart_probe,
Wilson Ding30530792016-02-16 19:14:53 +0100961 .driver = {
Wilson Ding30530792016-02-16 19:14:53 +0100962 .name = "mvebu-uart",
963 .of_match_table = of_match_ptr(mvebu_uart_of_match),
Paul Gortmaker89ebc272016-03-13 19:48:52 -0400964 .suppress_bind_attrs = true,
Miquel Raynal394e8352018-04-21 16:07:33 +0200965#if defined(CONFIG_PM)
966 .pm = &mvebu_uart_pm_ops,
967#endif /* CONFIG_PM */
Wilson Ding30530792016-02-16 19:14:53 +0100968 },
969};
970
971static int __init mvebu_uart_init(void)
972{
973 int ret;
974
975 ret = uart_register_driver(&mvebu_uart_driver);
976 if (ret)
977 return ret;
978
979 ret = platform_driver_register(&mvebu_uart_platform_driver);
980 if (ret)
981 uart_unregister_driver(&mvebu_uart_driver);
982
983 return ret;
984}
Wilson Ding30530792016-02-16 19:14:53 +0100985arch_initcall(mvebu_uart_init);