blob: 170e446a2f625c4af4bf29c05f87931182979a73 [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
75
Miquel Raynal3a75e912017-10-13 11:01:55 +020076#define MVEBU_NR_UARTS 2
Wilson Ding30530792016-02-16 19:14:53 +010077
78#define MVEBU_UART_TYPE "mvebu-uart"
Yehuda Yitschak02c33332017-10-13 11:01:47 +020079#define DRIVER_NAME "mvebu_serial"
Wilson Ding30530792016-02-16 19:14:53 +010080
Miquel Raynal95f78762017-10-13 11:01:54 +020081enum {
82 /* Either there is only one summed IRQ... */
83 UART_IRQ_SUM = 0,
84 /* ...or there are two separate IRQ for RX and TX */
85 UART_RX_IRQ = 0,
86 UART_TX_IRQ,
87 UART_IRQ_COUNT
88};
89
90/* Diverging register offsets */
Miquel Raynal5218d762017-10-13 11:01:49 +020091struct uart_regs_layout {
92 unsigned int rbr;
93 unsigned int tsh;
94 unsigned int ctrl;
95 unsigned int intr;
Wilson Ding30530792016-02-16 19:14:53 +010096};
97
Miquel Raynal5218d762017-10-13 11:01:49 +020098/* Diverging flags */
99struct uart_flags {
100 unsigned int ctrl_tx_rdy_int;
101 unsigned int ctrl_rx_rdy_int;
102 unsigned int stat_tx_rdy;
103 unsigned int stat_rx_rdy;
104};
105
106/* Driver data, a structure for each UART port */
107struct mvebu_uart_driver_data {
108 bool is_ext;
109 struct uart_regs_layout regs;
110 struct uart_flags flags;
111};
112
Miquel Raynal394e8352018-04-21 16:07:33 +0200113/* Saved registers during suspend */
114struct mvebu_uart_pm_regs {
115 unsigned int rbr;
116 unsigned int tsh;
117 unsigned int ctrl;
118 unsigned int intr;
119 unsigned int stat;
120 unsigned int brdv;
121 unsigned int osamp;
122};
123
Miquel Raynal5218d762017-10-13 11:01:49 +0200124/* MVEBU UART driver structure */
125struct mvebu_uart {
126 struct uart_port *port;
127 struct clk *clk;
Miquel Raynal95f78762017-10-13 11:01:54 +0200128 int irq[UART_IRQ_COUNT];
129 unsigned char __iomem *nb;
Miquel Raynal5218d762017-10-13 11:01:49 +0200130 struct mvebu_uart_driver_data *data;
Miquel Raynal394e8352018-04-21 16:07:33 +0200131#if defined(CONFIG_PM)
132 struct mvebu_uart_pm_regs pm_regs;
133#endif /* CONFIG_PM */
Miquel Raynal5218d762017-10-13 11:01:49 +0200134};
135
136static struct mvebu_uart *to_mvuart(struct uart_port *port)
137{
138 return (struct mvebu_uart *)port->private_data;
139}
140
141#define IS_EXTENDED(port) (to_mvuart(port)->data->is_ext)
142
143#define UART_RBR(port) (to_mvuart(port)->data->regs.rbr)
144#define UART_TSH(port) (to_mvuart(port)->data->regs.tsh)
145#define UART_CTRL(port) (to_mvuart(port)->data->regs.ctrl)
146#define UART_INTR(port) (to_mvuart(port)->data->regs.intr)
147
148#define CTRL_TX_RDY_INT(port) (to_mvuart(port)->data->flags.ctrl_tx_rdy_int)
149#define CTRL_RX_RDY_INT(port) (to_mvuart(port)->data->flags.ctrl_rx_rdy_int)
150#define STAT_TX_RDY(port) (to_mvuart(port)->data->flags.stat_tx_rdy)
151#define STAT_RX_RDY(port) (to_mvuart(port)->data->flags.stat_rx_rdy)
152
153static struct uart_port mvebu_uart_ports[MVEBU_NR_UARTS];
154
Wilson Ding30530792016-02-16 19:14:53 +0100155/* Core UART Driver Operations */
156static unsigned int mvebu_uart_tx_empty(struct uart_port *port)
157{
158 unsigned long flags;
159 unsigned int st;
160
161 spin_lock_irqsave(&port->lock, flags);
162 st = readl(port->membase + UART_STAT);
163 spin_unlock_irqrestore(&port->lock, flags);
164
165 return (st & STAT_TX_FIFO_EMP) ? TIOCSER_TEMT : 0;
166}
167
168static unsigned int mvebu_uart_get_mctrl(struct uart_port *port)
169{
170 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
171}
172
173static void mvebu_uart_set_mctrl(struct uart_port *port,
174 unsigned int mctrl)
175{
176/*
177 * Even if we do not support configuring the modem control lines, this
178 * function must be proided to the serial core
179 */
180}
181
182static void mvebu_uart_stop_tx(struct uart_port *port)
183{
Miquel Raynal5218d762017-10-13 11:01:49 +0200184 unsigned int ctl = readl(port->membase + UART_INTR(port));
Wilson Ding30530792016-02-16 19:14:53 +0100185
Miquel Raynal5218d762017-10-13 11:01:49 +0200186 ctl &= ~CTRL_TX_RDY_INT(port);
187 writel(ctl, port->membase + UART_INTR(port));
Wilson Ding30530792016-02-16 19:14:53 +0100188}
189
190static void mvebu_uart_start_tx(struct uart_port *port)
191{
Allen Yan30434b02017-10-13 11:01:53 +0200192 unsigned int ctl;
193 struct circ_buf *xmit = &port->state->xmit;
Wilson Ding30530792016-02-16 19:14:53 +0100194
Allen Yan30434b02017-10-13 11:01:53 +0200195 if (IS_EXTENDED(port) && !uart_circ_empty(xmit)) {
196 writel(xmit->buf[xmit->tail], port->membase + UART_TSH(port));
197 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
198 port->icount.tx++;
199 }
200
201 ctl = readl(port->membase + UART_INTR(port));
Miquel Raynal5218d762017-10-13 11:01:49 +0200202 ctl |= CTRL_TX_RDY_INT(port);
203 writel(ctl, port->membase + UART_INTR(port));
Wilson Ding30530792016-02-16 19:14:53 +0100204}
205
206static void mvebu_uart_stop_rx(struct uart_port *port)
207{
Miquel Raynal5218d762017-10-13 11:01:49 +0200208 unsigned int ctl;
Wilson Ding30530792016-02-16 19:14:53 +0100209
Miquel Raynal5218d762017-10-13 11:01:49 +0200210 ctl = readl(port->membase + UART_CTRL(port));
211 ctl &= ~CTRL_BRK_INT;
212 writel(ctl, port->membase + UART_CTRL(port));
213
214 ctl = readl(port->membase + UART_INTR(port));
215 ctl &= ~CTRL_RX_RDY_INT(port);
216 writel(ctl, port->membase + UART_INTR(port));
Wilson Ding30530792016-02-16 19:14:53 +0100217}
218
219static void mvebu_uart_break_ctl(struct uart_port *port, int brk)
220{
221 unsigned int ctl;
222 unsigned long flags;
223
224 spin_lock_irqsave(&port->lock, flags);
Miquel Raynal5218d762017-10-13 11:01:49 +0200225 ctl = readl(port->membase + UART_CTRL(port));
Wilson Ding30530792016-02-16 19:14:53 +0100226 if (brk == -1)
227 ctl |= CTRL_SND_BRK_SEQ;
228 else
229 ctl &= ~CTRL_SND_BRK_SEQ;
Miquel Raynal5218d762017-10-13 11:01:49 +0200230 writel(ctl, port->membase + UART_CTRL(port));
Wilson Ding30530792016-02-16 19:14:53 +0100231 spin_unlock_irqrestore(&port->lock, flags);
232}
233
234static void mvebu_uart_rx_chars(struct uart_port *port, unsigned int status)
235{
236 struct tty_port *tport = &port->state->port;
237 unsigned char ch = 0;
238 char flag = 0;
239
240 do {
Miquel Raynal5218d762017-10-13 11:01:49 +0200241 if (status & STAT_RX_RDY(port)) {
242 ch = readl(port->membase + UART_RBR(port));
Wilson Ding30530792016-02-16 19:14:53 +0100243 ch &= 0xff;
244 flag = TTY_NORMAL;
245 port->icount.rx++;
246
247 if (status & STAT_PAR_ERR)
248 port->icount.parity++;
249 }
250
251 if (status & STAT_BRK_DET) {
252 port->icount.brk++;
253 status &= ~(STAT_FRM_ERR | STAT_PAR_ERR);
254 if (uart_handle_break(port))
255 goto ignore_char;
256 }
257
258 if (status & STAT_OVR_ERR)
259 port->icount.overrun++;
260
261 if (status & STAT_FRM_ERR)
262 port->icount.frame++;
263
264 if (uart_handle_sysrq_char(port, ch))
265 goto ignore_char;
266
267 if (status & port->ignore_status_mask & STAT_PAR_ERR)
Miquel Raynal5218d762017-10-13 11:01:49 +0200268 status &= ~STAT_RX_RDY(port);
Wilson Ding30530792016-02-16 19:14:53 +0100269
270 status &= port->read_status_mask;
271
272 if (status & STAT_PAR_ERR)
273 flag = TTY_PARITY;
274
275 status &= ~port->ignore_status_mask;
276
Miquel Raynal5218d762017-10-13 11:01:49 +0200277 if (status & STAT_RX_RDY(port))
Wilson Ding30530792016-02-16 19:14:53 +0100278 tty_insert_flip_char(tport, ch, flag);
279
280 if (status & STAT_BRK_DET)
281 tty_insert_flip_char(tport, 0, TTY_BREAK);
282
283 if (status & STAT_FRM_ERR)
284 tty_insert_flip_char(tport, 0, TTY_FRAME);
285
286 if (status & STAT_OVR_ERR)
287 tty_insert_flip_char(tport, 0, TTY_OVERRUN);
288
289ignore_char:
290 status = readl(port->membase + UART_STAT);
Miquel Raynal5218d762017-10-13 11:01:49 +0200291 } while (status & (STAT_RX_RDY(port) | STAT_BRK_DET));
Wilson Ding30530792016-02-16 19:14:53 +0100292
293 tty_flip_buffer_push(tport);
294}
295
296static void mvebu_uart_tx_chars(struct uart_port *port, unsigned int status)
297{
298 struct circ_buf *xmit = &port->state->xmit;
299 unsigned int count;
300 unsigned int st;
301
302 if (port->x_char) {
Miquel Raynal5218d762017-10-13 11:01:49 +0200303 writel(port->x_char, port->membase + UART_TSH(port));
Wilson Ding30530792016-02-16 19:14:53 +0100304 port->icount.tx++;
305 port->x_char = 0;
306 return;
307 }
308
309 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
310 mvebu_uart_stop_tx(port);
311 return;
312 }
313
314 for (count = 0; count < port->fifosize; count++) {
Miquel Raynal5218d762017-10-13 11:01:49 +0200315 writel(xmit->buf[xmit->tail], port->membase + UART_TSH(port));
Wilson Ding30530792016-02-16 19:14:53 +0100316 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
317 port->icount.tx++;
318
319 if (uart_circ_empty(xmit))
320 break;
321
322 st = readl(port->membase + UART_STAT);
323 if (st & STAT_TX_FIFO_FUL)
324 break;
325 }
326
327 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
328 uart_write_wakeup(port);
329
330 if (uart_circ_empty(xmit))
331 mvebu_uart_stop_tx(port);
332}
333
334static irqreturn_t mvebu_uart_isr(int irq, void *dev_id)
335{
336 struct uart_port *port = (struct uart_port *)dev_id;
337 unsigned int st = readl(port->membase + UART_STAT);
338
Miquel Raynal5218d762017-10-13 11:01:49 +0200339 if (st & (STAT_RX_RDY(port) | STAT_OVR_ERR | STAT_FRM_ERR |
Miquel Raynal95f78762017-10-13 11:01:54 +0200340 STAT_BRK_DET))
341 mvebu_uart_rx_chars(port, st);
342
343 if (st & STAT_TX_RDY(port))
344 mvebu_uart_tx_chars(port, st);
345
346 return IRQ_HANDLED;
347}
348
349static irqreturn_t mvebu_uart_rx_isr(int irq, void *dev_id)
350{
351 struct uart_port *port = (struct uart_port *)dev_id;
352 unsigned int st = readl(port->membase + UART_STAT);
353
354 if (st & (STAT_RX_RDY(port) | STAT_OVR_ERR | STAT_FRM_ERR |
Miquel Raynal5218d762017-10-13 11:01:49 +0200355 STAT_BRK_DET))
Wilson Ding30530792016-02-16 19:14:53 +0100356 mvebu_uart_rx_chars(port, st);
357
Miquel Raynal95f78762017-10-13 11:01:54 +0200358 return IRQ_HANDLED;
359}
360
361static irqreturn_t mvebu_uart_tx_isr(int irq, void *dev_id)
362{
363 struct uart_port *port = (struct uart_port *)dev_id;
364 unsigned int st = readl(port->membase + UART_STAT);
365
Miquel Raynal5218d762017-10-13 11:01:49 +0200366 if (st & STAT_TX_RDY(port))
Wilson Ding30530792016-02-16 19:14:53 +0100367 mvebu_uart_tx_chars(port, st);
368
369 return IRQ_HANDLED;
370}
371
372static int mvebu_uart_startup(struct uart_port *port)
373{
Miquel Raynal95f78762017-10-13 11:01:54 +0200374 struct mvebu_uart *mvuart = to_mvuart(port);
Miquel Raynal5218d762017-10-13 11:01:49 +0200375 unsigned int ctl;
Wilson Ding30530792016-02-16 19:14:53 +0100376 int ret;
377
378 writel(CTRL_TXFIFO_RST | CTRL_RXFIFO_RST,
Miquel Raynal5218d762017-10-13 11:01:49 +0200379 port->membase + UART_CTRL(port));
Wilson Ding30530792016-02-16 19:14:53 +0100380 udelay(1);
Allen Yan2ff23c42017-10-13 11:01:52 +0200381
382 /* Clear the error bits of state register before IRQ request */
383 ret = readl(port->membase + UART_STAT);
384 ret |= STAT_BRK_ERR;
385 writel(ret, port->membase + UART_STAT);
386
Miquel Raynal5218d762017-10-13 11:01:49 +0200387 writel(CTRL_BRK_INT, port->membase + UART_CTRL(port));
388
389 ctl = readl(port->membase + UART_INTR(port));
390 ctl |= CTRL_RX_RDY_INT(port);
391 writel(ctl, port->membase + UART_INTR(port));
Wilson Ding30530792016-02-16 19:14:53 +0100392
Miquel Raynal95f78762017-10-13 11:01:54 +0200393 if (!mvuart->irq[UART_TX_IRQ]) {
394 /* Old bindings with just one interrupt (UART0 only) */
395 ret = devm_request_irq(port->dev, mvuart->irq[UART_IRQ_SUM],
396 mvebu_uart_isr, port->irqflags,
397 dev_name(port->dev), port);
398 if (ret) {
399 dev_err(port->dev, "unable to request IRQ %d\n",
400 mvuart->irq[UART_IRQ_SUM]);
401 return ret;
402 }
403 } else {
404 /* New bindings with an IRQ for RX and TX (both UART) */
405 ret = devm_request_irq(port->dev, mvuart->irq[UART_RX_IRQ],
406 mvebu_uart_rx_isr, port->irqflags,
407 dev_name(port->dev), port);
408 if (ret) {
409 dev_err(port->dev, "unable to request IRQ %d\n",
410 mvuart->irq[UART_RX_IRQ]);
411 return ret;
412 }
413
414 ret = devm_request_irq(port->dev, mvuart->irq[UART_TX_IRQ],
415 mvebu_uart_tx_isr, port->irqflags,
416 dev_name(port->dev),
417 port);
418 if (ret) {
419 dev_err(port->dev, "unable to request IRQ %d\n",
420 mvuart->irq[UART_TX_IRQ]);
421 devm_free_irq(port->dev, mvuart->irq[UART_RX_IRQ],
422 port);
423 return ret;
424 }
Wilson Ding30530792016-02-16 19:14:53 +0100425 }
426
427 return 0;
428}
429
430static void mvebu_uart_shutdown(struct uart_port *port)
431{
Miquel Raynal95f78762017-10-13 11:01:54 +0200432 struct mvebu_uart *mvuart = to_mvuart(port);
433
Miquel Raynal5218d762017-10-13 11:01:49 +0200434 writel(0, port->membase + UART_INTR(port));
Thomas Petazzonic2c16592016-06-16 16:48:52 +0200435
Miquel Raynal95f78762017-10-13 11:01:54 +0200436 if (!mvuart->irq[UART_TX_IRQ]) {
437 devm_free_irq(port->dev, mvuart->irq[UART_IRQ_SUM], port);
438 } else {
439 devm_free_irq(port->dev, mvuart->irq[UART_RX_IRQ], port);
440 devm_free_irq(port->dev, mvuart->irq[UART_TX_IRQ], port);
441 }
Wilson Ding30530792016-02-16 19:14:53 +0100442}
443
Allen Yan68a0db12017-10-13 11:01:51 +0200444static int mvebu_uart_baud_rate_set(struct uart_port *port, unsigned int baud)
445{
446 struct mvebu_uart *mvuart = to_mvuart(port);
447 unsigned int baud_rate_div;
448 u32 brdv;
449
450 if (IS_ERR(mvuart->clk))
451 return -PTR_ERR(mvuart->clk);
452
453 /*
454 * The UART clock is divided by the value of the divisor to generate
455 * UCLK_OUT clock, which is 16 times faster than the baudrate.
456 * This prescaler can achieve all standard baudrates until 230400.
457 * Higher baudrates could be achieved for the extended UART by using the
458 * programmable oversampling stack (also called fractional divisor).
459 */
460 baud_rate_div = DIV_ROUND_UP(port->uartclk, baud * 16);
461 brdv = readl(port->membase + UART_BRDV);
462 brdv &= ~BRDV_BAUD_MASK;
463 brdv |= baud_rate_div;
464 writel(brdv, port->membase + UART_BRDV);
465
466 return 0;
467}
468
Wilson Ding30530792016-02-16 19:14:53 +0100469static void mvebu_uart_set_termios(struct uart_port *port,
470 struct ktermios *termios,
471 struct ktermios *old)
472{
473 unsigned long flags;
474 unsigned int baud;
475
476 spin_lock_irqsave(&port->lock, flags);
477
Miquel Raynal5218d762017-10-13 11:01:49 +0200478 port->read_status_mask = STAT_RX_RDY(port) | STAT_OVR_ERR |
479 STAT_TX_RDY(port) | STAT_TX_FIFO_FUL;
Wilson Ding30530792016-02-16 19:14:53 +0100480
481 if (termios->c_iflag & INPCK)
482 port->read_status_mask |= STAT_FRM_ERR | STAT_PAR_ERR;
483
484 port->ignore_status_mask = 0;
485 if (termios->c_iflag & IGNPAR)
486 port->ignore_status_mask |=
487 STAT_FRM_ERR | STAT_PAR_ERR | STAT_OVR_ERR;
488
489 if ((termios->c_cflag & CREAD) == 0)
Miquel Raynal5218d762017-10-13 11:01:49 +0200490 port->ignore_status_mask |= STAT_RX_RDY(port) | STAT_BRK_ERR;
Wilson Ding30530792016-02-16 19:14:53 +0100491
Allen Yan68a0db12017-10-13 11:01:51 +0200492 /*
493 * Maximum achievable frequency with simple baudrate divisor is 230400.
494 * Since the error per bit frame would be of more than 15%, achieving
495 * higher frequencies would require to implement the fractional divisor
496 * feature.
497 */
498 baud = uart_get_baud_rate(port, termios, old, 0, 230400);
499 if (mvebu_uart_baud_rate_set(port, baud)) {
500 /* No clock available, baudrate cannot be changed */
501 if (old)
502 baud = uart_get_baud_rate(port, old, NULL, 0, 230400);
503 } else {
504 tty_termios_encode_baud_rate(termios, baud, baud);
505 uart_update_timeout(port, termios->c_cflag, baud);
506 }
Wilson Ding30530792016-02-16 19:14:53 +0100507
Allen Yan68a0db12017-10-13 11:01:51 +0200508 /* Only the following flag changes are supported */
509 if (old) {
510 termios->c_iflag &= INPCK | IGNPAR;
511 termios->c_iflag |= old->c_iflag & ~(INPCK | IGNPAR);
512 termios->c_cflag &= CREAD | CBAUD;
513 termios->c_cflag |= old->c_cflag & ~(CREAD | CBAUD);
Jan Kiszkae0bf2d4982018-08-26 19:49:32 +0200514 termios->c_cflag |= CS8;
Allen Yan68a0db12017-10-13 11:01:51 +0200515 }
Wilson Ding30530792016-02-16 19:14:53 +0100516
517 spin_unlock_irqrestore(&port->lock, flags);
518}
519
520static const char *mvebu_uart_type(struct uart_port *port)
521{
522 return MVEBU_UART_TYPE;
523}
524
525static void mvebu_uart_release_port(struct uart_port *port)
526{
527 /* Nothing to do here */
528}
529
530static int mvebu_uart_request_port(struct uart_port *port)
531{
532 return 0;
533}
534
535#ifdef CONFIG_CONSOLE_POLL
536static int mvebu_uart_get_poll_char(struct uart_port *port)
537{
538 unsigned int st = readl(port->membase + UART_STAT);
539
Miquel Raynal5218d762017-10-13 11:01:49 +0200540 if (!(st & STAT_RX_RDY(port)))
Wilson Ding30530792016-02-16 19:14:53 +0100541 return NO_POLL_CHAR;
542
Miquel Raynal5218d762017-10-13 11:01:49 +0200543 return readl(port->membase + UART_RBR(port));
Wilson Ding30530792016-02-16 19:14:53 +0100544}
545
546static void mvebu_uart_put_poll_char(struct uart_port *port, unsigned char c)
547{
548 unsigned int st;
549
550 for (;;) {
551 st = readl(port->membase + UART_STAT);
552
553 if (!(st & STAT_TX_FIFO_FUL))
554 break;
555
556 udelay(1);
557 }
558
Miquel Raynal5218d762017-10-13 11:01:49 +0200559 writel(c, port->membase + UART_TSH(port));
Wilson Ding30530792016-02-16 19:14:53 +0100560}
561#endif
562
563static const struct uart_ops mvebu_uart_ops = {
564 .tx_empty = mvebu_uart_tx_empty,
565 .set_mctrl = mvebu_uart_set_mctrl,
566 .get_mctrl = mvebu_uart_get_mctrl,
567 .stop_tx = mvebu_uart_stop_tx,
568 .start_tx = mvebu_uart_start_tx,
569 .stop_rx = mvebu_uart_stop_rx,
570 .break_ctl = mvebu_uart_break_ctl,
571 .startup = mvebu_uart_startup,
572 .shutdown = mvebu_uart_shutdown,
573 .set_termios = mvebu_uart_set_termios,
574 .type = mvebu_uart_type,
575 .release_port = mvebu_uart_release_port,
576 .request_port = mvebu_uart_request_port,
577#ifdef CONFIG_CONSOLE_POLL
578 .poll_get_char = mvebu_uart_get_poll_char,
579 .poll_put_char = mvebu_uart_put_poll_char,
580#endif
581};
582
583/* Console Driver Operations */
584
585#ifdef CONFIG_SERIAL_MVEBU_CONSOLE
586/* Early Console */
587static void mvebu_uart_putc(struct uart_port *port, int c)
588{
589 unsigned int st;
590
591 for (;;) {
592 st = readl(port->membase + UART_STAT);
593 if (!(st & STAT_TX_FIFO_FUL))
594 break;
595 }
596
Miquel Raynal5218d762017-10-13 11:01:49 +0200597 /* At early stage, DT is not parsed yet, only use UART0 */
598 writel(c, port->membase + UART_STD_TSH);
Wilson Ding30530792016-02-16 19:14:53 +0100599
600 for (;;) {
601 st = readl(port->membase + UART_STAT);
602 if (st & STAT_TX_FIFO_EMP)
603 break;
604 }
605}
606
607static void mvebu_uart_putc_early_write(struct console *con,
608 const char *s,
609 unsigned n)
610{
611 struct earlycon_device *dev = con->data;
612
613 uart_console_write(&dev->port, s, n, mvebu_uart_putc);
614}
615
616static int __init
617mvebu_uart_early_console_setup(struct earlycon_device *device,
618 const char *opt)
619{
620 if (!device->port.membase)
621 return -ENODEV;
622
623 device->con->write = mvebu_uart_putc_early_write;
624
625 return 0;
626}
627
628EARLYCON_DECLARE(ar3700_uart, mvebu_uart_early_console_setup);
629OF_EARLYCON_DECLARE(ar3700_uart, "marvell,armada-3700-uart",
630 mvebu_uart_early_console_setup);
631
632static void wait_for_xmitr(struct uart_port *port)
633{
634 u32 val;
635
636 readl_poll_timeout_atomic(port->membase + UART_STAT, val,
Gabriel Matnic685af12018-03-22 19:15:12 +0000637 (val & STAT_TX_RDY(port)), 1, 10000);
Wilson Ding30530792016-02-16 19:14:53 +0100638}
639
640static void mvebu_uart_console_putchar(struct uart_port *port, int ch)
641{
642 wait_for_xmitr(port);
Miquel Raynal5218d762017-10-13 11:01:49 +0200643 writel(ch, port->membase + UART_TSH(port));
Wilson Ding30530792016-02-16 19:14:53 +0100644}
645
646static void mvebu_uart_console_write(struct console *co, const char *s,
647 unsigned int count)
648{
649 struct uart_port *port = &mvebu_uart_ports[co->index];
650 unsigned long flags;
Miquel Raynal5218d762017-10-13 11:01:49 +0200651 unsigned int ier, intr, ctl;
Wilson Ding30530792016-02-16 19:14:53 +0100652 int locked = 1;
653
654 if (oops_in_progress)
655 locked = spin_trylock_irqsave(&port->lock, flags);
656 else
657 spin_lock_irqsave(&port->lock, flags);
658
Miquel Raynal5218d762017-10-13 11:01:49 +0200659 ier = readl(port->membase + UART_CTRL(port)) & CTRL_BRK_INT;
660 intr = readl(port->membase + UART_INTR(port)) &
661 (CTRL_RX_RDY_INT(port) | CTRL_TX_RDY_INT(port));
662 writel(0, port->membase + UART_CTRL(port));
663 writel(0, port->membase + UART_INTR(port));
Wilson Ding30530792016-02-16 19:14:53 +0100664
665 uart_console_write(port, s, count, mvebu_uart_console_putchar);
666
667 wait_for_xmitr(port);
668
669 if (ier)
Miquel Raynal5218d762017-10-13 11:01:49 +0200670 writel(ier, port->membase + UART_CTRL(port));
671
672 if (intr) {
673 ctl = intr | readl(port->membase + UART_INTR(port));
674 writel(ctl, port->membase + UART_INTR(port));
675 }
Wilson Ding30530792016-02-16 19:14:53 +0100676
677 if (locked)
678 spin_unlock_irqrestore(&port->lock, flags);
679}
680
681static int mvebu_uart_console_setup(struct console *co, char *options)
682{
683 struct uart_port *port;
684 int baud = 9600;
685 int bits = 8;
686 int parity = 'n';
687 int flow = 'n';
688
689 if (co->index < 0 || co->index >= MVEBU_NR_UARTS)
690 return -EINVAL;
691
692 port = &mvebu_uart_ports[co->index];
693
694 if (!port->mapbase || !port->membase) {
695 pr_debug("console on ttyMV%i not present\n", co->index);
696 return -ENODEV;
697 }
698
699 if (options)
700 uart_parse_options(options, &baud, &parity, &bits, &flow);
701
702 return uart_set_options(port, co, baud, parity, bits, flow);
703}
704
705static struct uart_driver mvebu_uart_driver;
706
707static struct console mvebu_uart_console = {
708 .name = "ttyMV",
709 .write = mvebu_uart_console_write,
710 .device = uart_console_device,
711 .setup = mvebu_uart_console_setup,
712 .flags = CON_PRINTBUFFER,
713 .index = -1,
714 .data = &mvebu_uart_driver,
715};
716
717static int __init mvebu_uart_console_init(void)
718{
719 register_console(&mvebu_uart_console);
720 return 0;
721}
722
723console_initcall(mvebu_uart_console_init);
724
725
726#endif /* CONFIG_SERIAL_MVEBU_CONSOLE */
727
728static struct uart_driver mvebu_uart_driver = {
729 .owner = THIS_MODULE,
Yehuda Yitschak02c33332017-10-13 11:01:47 +0200730 .driver_name = DRIVER_NAME,
Wilson Ding30530792016-02-16 19:14:53 +0100731 .dev_name = "ttyMV",
732 .nr = MVEBU_NR_UARTS,
733#ifdef CONFIG_SERIAL_MVEBU_CONSOLE
734 .cons = &mvebu_uart_console,
735#endif
736};
737
Miquel Raynal394e8352018-04-21 16:07:33 +0200738#if defined(CONFIG_PM)
739static int mvebu_uart_suspend(struct device *dev)
740{
741 struct mvebu_uart *mvuart = dev_get_drvdata(dev);
742 struct uart_port *port = mvuart->port;
743
744 uart_suspend_port(&mvebu_uart_driver, port);
745
746 mvuart->pm_regs.rbr = readl(port->membase + UART_RBR(port));
747 mvuart->pm_regs.tsh = readl(port->membase + UART_TSH(port));
748 mvuart->pm_regs.ctrl = readl(port->membase + UART_CTRL(port));
749 mvuart->pm_regs.intr = readl(port->membase + UART_INTR(port));
750 mvuart->pm_regs.stat = readl(port->membase + UART_STAT);
751 mvuart->pm_regs.brdv = readl(port->membase + UART_BRDV);
752 mvuart->pm_regs.osamp = readl(port->membase + UART_OSAMP);
753
754 device_set_wakeup_enable(dev, true);
755
756 return 0;
757}
758
759static int mvebu_uart_resume(struct device *dev)
760{
761 struct mvebu_uart *mvuart = dev_get_drvdata(dev);
762 struct uart_port *port = mvuart->port;
763
764 writel(mvuart->pm_regs.rbr, port->membase + UART_RBR(port));
765 writel(mvuart->pm_regs.tsh, port->membase + UART_TSH(port));
766 writel(mvuart->pm_regs.ctrl, port->membase + UART_CTRL(port));
767 writel(mvuart->pm_regs.intr, port->membase + UART_INTR(port));
768 writel(mvuart->pm_regs.stat, port->membase + UART_STAT);
769 writel(mvuart->pm_regs.brdv, port->membase + UART_BRDV);
770 writel(mvuart->pm_regs.osamp, port->membase + UART_OSAMP);
771
772 uart_resume_port(&mvebu_uart_driver, port);
773
774 return 0;
775}
776
777static const struct dev_pm_ops mvebu_uart_pm_ops = {
778 .suspend = mvebu_uart_suspend,
779 .resume = mvebu_uart_resume,
780};
781#endif /* CONFIG_PM */
782
Miquel Raynal5218d762017-10-13 11:01:49 +0200783static const struct of_device_id mvebu_uart_of_match[];
784
Allen Yan94228f92017-10-13 11:01:48 +0200785/* Counter to keep track of each UART port id when not using CONFIG_OF */
786static int uart_num_counter;
787
Wilson Ding30530792016-02-16 19:14:53 +0100788static int mvebu_uart_probe(struct platform_device *pdev)
789{
790 struct resource *reg = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Miquel Raynal5218d762017-10-13 11:01:49 +0200791 const struct of_device_id *match = of_match_device(mvebu_uart_of_match,
792 &pdev->dev);
Wilson Ding30530792016-02-16 19:14:53 +0100793 struct uart_port *port;
Miquel Raynal5218d762017-10-13 11:01:49 +0200794 struct mvebu_uart *mvuart;
Miquel Raynal95f78762017-10-13 11:01:54 +0200795 int ret, id, irq;
Wilson Ding30530792016-02-16 19:14:53 +0100796
Miquel Raynal95f78762017-10-13 11:01:54 +0200797 if (!reg) {
798 dev_err(&pdev->dev, "no registers defined\n");
Wilson Ding30530792016-02-16 19:14:53 +0100799 return -EINVAL;
800 }
801
Allen Yan94228f92017-10-13 11:01:48 +0200802 /* Assume that all UART ports have a DT alias or none has */
803 id = of_alias_get_id(pdev->dev.of_node, "serial");
804 if (!pdev->dev.of_node || id < 0)
805 pdev->id = uart_num_counter++;
806 else
807 pdev->id = id;
808
809 if (pdev->id >= MVEBU_NR_UARTS) {
810 dev_err(&pdev->dev, "cannot have more than %d UART ports\n",
811 MVEBU_NR_UARTS);
812 return -EINVAL;
813 }
814
815 port = &mvebu_uart_ports[pdev->id];
Wilson Ding30530792016-02-16 19:14:53 +0100816
817 spin_lock_init(&port->lock);
818
819 port->dev = &pdev->dev;
820 port->type = PORT_MVEBU;
821 port->ops = &mvebu_uart_ops;
822 port->regshift = 0;
823
824 port->fifosize = 32;
825 port->iotype = UPIO_MEM32;
826 port->flags = UPF_FIXED_PORT;
Allen Yan94228f92017-10-13 11:01:48 +0200827 port->line = pdev->id;
Wilson Ding30530792016-02-16 19:14:53 +0100828
Miquel Raynal95f78762017-10-13 11:01:54 +0200829 /*
830 * IRQ number is not stored in this structure because we may have two of
831 * them per port (RX and TX). Instead, use the driver UART structure
832 * array so called ->irq[].
833 */
834 port->irq = 0;
Wilson Ding30530792016-02-16 19:14:53 +0100835 port->irqflags = 0;
836 port->mapbase = reg->start;
837
838 port->membase = devm_ioremap_resource(&pdev->dev, reg);
839 if (IS_ERR(port->membase))
840 return -PTR_ERR(port->membase);
841
Miquel Raynal5218d762017-10-13 11:01:49 +0200842 mvuart = devm_kzalloc(&pdev->dev, sizeof(struct mvebu_uart),
843 GFP_KERNEL);
844 if (!mvuart)
Wilson Ding30530792016-02-16 19:14:53 +0100845 return -ENOMEM;
846
Allen Yan68a0db12017-10-13 11:01:51 +0200847 /* Get controller data depending on the compatible string */
Miquel Raynal5218d762017-10-13 11:01:49 +0200848 mvuart->data = (struct mvebu_uart_driver_data *)match->data;
849 mvuart->port = port;
Wilson Ding30530792016-02-16 19:14:53 +0100850
Miquel Raynal5218d762017-10-13 11:01:49 +0200851 port->private_data = mvuart;
852 platform_set_drvdata(pdev, mvuart);
Wilson Ding30530792016-02-16 19:14:53 +0100853
Allen Yan68a0db12017-10-13 11:01:51 +0200854 /* Get fixed clock frequency */
855 mvuart->clk = devm_clk_get(&pdev->dev, NULL);
856 if (IS_ERR(mvuart->clk)) {
857 if (PTR_ERR(mvuart->clk) == -EPROBE_DEFER)
858 return PTR_ERR(mvuart->clk);
859
860 if (IS_EXTENDED(port)) {
861 dev_err(&pdev->dev, "unable to get UART clock\n");
862 return PTR_ERR(mvuart->clk);
863 }
864 } else {
865 if (!clk_prepare_enable(mvuart->clk))
866 port->uartclk = clk_get_rate(mvuart->clk);
867 }
868
Miquel Raynal95f78762017-10-13 11:01:54 +0200869 /* Manage interrupts */
Miquel Raynal95f78762017-10-13 11:01:54 +0200870 if (platform_irq_count(pdev) == 1) {
871 /* Old bindings: no name on the single unamed UART0 IRQ */
872 irq = platform_get_irq(pdev, 0);
873 if (irq < 0) {
874 dev_err(&pdev->dev, "unable to get UART IRQ\n");
875 return irq;
876 }
877
878 mvuart->irq[UART_IRQ_SUM] = irq;
879 } else {
880 /*
881 * New bindings: named interrupts (RX, TX) for both UARTS,
882 * only make use of uart-rx and uart-tx interrupts, do not use
883 * uart-sum of UART0 port.
884 */
885 irq = platform_get_irq_byname(pdev, "uart-rx");
886 if (irq < 0) {
887 dev_err(&pdev->dev, "unable to get 'uart-rx' IRQ\n");
888 return irq;
889 }
890
891 mvuart->irq[UART_RX_IRQ] = irq;
892
893 irq = platform_get_irq_byname(pdev, "uart-tx");
894 if (irq < 0) {
895 dev_err(&pdev->dev, "unable to get 'uart-tx' IRQ\n");
896 return irq;
897 }
898
899 mvuart->irq[UART_TX_IRQ] = irq;
900 }
901
Allen Yan9c3d3ee2017-10-13 11:01:50 +0200902 /* UART Soft Reset*/
903 writel(CTRL_SOFT_RST, port->membase + UART_CTRL(port));
904 udelay(1);
905 writel(0, port->membase + UART_CTRL(port));
906
Wilson Ding30530792016-02-16 19:14:53 +0100907 ret = uart_add_one_port(&mvebu_uart_driver, port);
908 if (ret)
909 return ret;
910 return 0;
911}
912
Miquel Raynal5218d762017-10-13 11:01:49 +0200913static struct mvebu_uart_driver_data uart_std_driver_data = {
914 .is_ext = false,
915 .regs.rbr = UART_STD_RBR,
916 .regs.tsh = UART_STD_TSH,
917 .regs.ctrl = UART_STD_CTRL1,
918 .regs.intr = UART_STD_CTRL2,
919 .flags.ctrl_tx_rdy_int = CTRL_STD_TX_RDY_INT,
920 .flags.ctrl_rx_rdy_int = CTRL_STD_RX_RDY_INT,
921 .flags.stat_tx_rdy = STAT_STD_TX_RDY,
922 .flags.stat_rx_rdy = STAT_STD_RX_RDY,
923};
924
Miquel Raynal53501e02017-10-13 11:01:56 +0200925static struct mvebu_uart_driver_data uart_ext_driver_data = {
926 .is_ext = true,
927 .regs.rbr = UART_EXT_RBR,
928 .regs.tsh = UART_EXT_TSH,
929 .regs.ctrl = UART_EXT_CTRL1,
930 .regs.intr = UART_EXT_CTRL2,
931 .flags.ctrl_tx_rdy_int = CTRL_EXT_TX_RDY_INT,
932 .flags.ctrl_rx_rdy_int = CTRL_EXT_RX_RDY_INT,
933 .flags.stat_tx_rdy = STAT_EXT_TX_RDY,
934 .flags.stat_rx_rdy = STAT_EXT_RX_RDY,
935};
936
Wilson Ding30530792016-02-16 19:14:53 +0100937/* Match table for of_platform binding */
938static const struct of_device_id mvebu_uart_of_match[] = {
Miquel Raynal5218d762017-10-13 11:01:49 +0200939 {
940 .compatible = "marvell,armada-3700-uart",
941 .data = (void *)&uart_std_driver_data,
942 },
Miquel Raynal53501e02017-10-13 11:01:56 +0200943 {
944 .compatible = "marvell,armada-3700-uart-ext",
945 .data = (void *)&uart_ext_driver_data,
946 },
Wilson Ding30530792016-02-16 19:14:53 +0100947 {}
948};
Wilson Ding30530792016-02-16 19:14:53 +0100949
950static struct platform_driver mvebu_uart_platform_driver = {
951 .probe = mvebu_uart_probe,
Wilson Ding30530792016-02-16 19:14:53 +0100952 .driver = {
Wilson Ding30530792016-02-16 19:14:53 +0100953 .name = "mvebu-uart",
954 .of_match_table = of_match_ptr(mvebu_uart_of_match),
Paul Gortmaker89ebc272016-03-13 19:48:52 -0400955 .suppress_bind_attrs = true,
Miquel Raynal394e8352018-04-21 16:07:33 +0200956#if defined(CONFIG_PM)
957 .pm = &mvebu_uart_pm_ops,
958#endif /* CONFIG_PM */
Wilson Ding30530792016-02-16 19:14:53 +0100959 },
960};
961
962static int __init mvebu_uart_init(void)
963{
964 int ret;
965
966 ret = uart_register_driver(&mvebu_uart_driver);
967 if (ret)
968 return ret;
969
970 ret = platform_driver_register(&mvebu_uart_platform_driver);
971 if (ret)
972 uart_unregister_driver(&mvebu_uart_driver);
973
974 return ret;
975}
Wilson Ding30530792016-02-16 19:14:53 +0100976arch_initcall(mvebu_uart_init);