blob: 79926a4fa56519b864dbf6453b72930e9a323515 [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* ***************************************************************************
8* This program is free software: you can redistribute it and/or modify it
9* under the terms of the GNU General Public License as published by the Free
10* Software Foundation, either version 2 of the License, or any later version.
11*
12* This program is distributed in the hope that it will be useful,
13* but WITHOUT ANY WARRANTY; without even the implied warranty of
14* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15* GNU General Public License for more details.
16*
17* You should have received a copy of the GNU General Public License
18* along with this program. If not, see <http://www.gnu.org/licenses/>.
19* ***************************************************************************
20*/
21
22#include <linux/clk.h>
23#include <linux/console.h>
24#include <linux/delay.h>
25#include <linux/device.h>
26#include <linux/init.h>
27#include <linux/io.h>
28#include <linux/iopoll.h>
Wilson Ding30530792016-02-16 19:14:53 +010029#include <linux/of.h>
30#include <linux/of_address.h>
31#include <linux/of_device.h>
32#include <linux/of_irq.h>
33#include <linux/of_platform.h>
34#include <linux/platform_device.h>
35#include <linux/serial.h>
36#include <linux/serial_core.h>
37#include <linux/slab.h>
38#include <linux/tty.h>
39#include <linux/tty_flip.h>
40
41/* Register Map */
Miquel Raynal5218d762017-10-13 11:01:49 +020042#define UART_STD_RBR 0x00
Miquel Raynal53501e02017-10-13 11:01:56 +020043#define UART_EXT_RBR 0x18
Wilson Ding30530792016-02-16 19:14:53 +010044
Miquel Raynal5218d762017-10-13 11:01:49 +020045#define UART_STD_TSH 0x04
Miquel Raynal53501e02017-10-13 11:01:56 +020046#define UART_EXT_TSH 0x1C
Wilson Ding30530792016-02-16 19:14:53 +010047
Miquel Raynal5218d762017-10-13 11:01:49 +020048#define UART_STD_CTRL1 0x08
Miquel Raynal53501e02017-10-13 11:01:56 +020049#define UART_EXT_CTRL1 0x04
Wilson Ding30530792016-02-16 19:14:53 +010050#define CTRL_SOFT_RST BIT(31)
51#define CTRL_TXFIFO_RST BIT(15)
52#define CTRL_RXFIFO_RST BIT(14)
Wilson Ding30530792016-02-16 19:14:53 +010053#define CTRL_SND_BRK_SEQ BIT(11)
Wilson Ding30530792016-02-16 19:14:53 +010054#define CTRL_BRK_DET_INT BIT(3)
55#define CTRL_FRM_ERR_INT BIT(2)
56#define CTRL_PAR_ERR_INT BIT(1)
57#define CTRL_OVR_ERR_INT BIT(0)
Miquel Raynal5218d762017-10-13 11:01:49 +020058#define CTRL_BRK_INT (CTRL_BRK_DET_INT | CTRL_FRM_ERR_INT | \
59 CTRL_PAR_ERR_INT | CTRL_OVR_ERR_INT)
Wilson Ding30530792016-02-16 19:14:53 +010060
Miquel Raynal5218d762017-10-13 11:01:49 +020061#define UART_STD_CTRL2 UART_STD_CTRL1
Miquel Raynal53501e02017-10-13 11:01:56 +020062#define UART_EXT_CTRL2 0x20
Miquel Raynal5218d762017-10-13 11:01:49 +020063#define CTRL_STD_TX_RDY_INT BIT(5)
Miquel Raynal53501e02017-10-13 11:01:56 +020064#define CTRL_EXT_TX_RDY_INT BIT(6)
Miquel Raynal5218d762017-10-13 11:01:49 +020065#define CTRL_STD_RX_RDY_INT BIT(4)
Miquel Raynal53501e02017-10-13 11:01:56 +020066#define CTRL_EXT_RX_RDY_INT BIT(5)
Miquel Raynal5218d762017-10-13 11:01:49 +020067
68#define UART_STAT 0x0C
Wilson Ding30530792016-02-16 19:14:53 +010069#define STAT_TX_FIFO_EMP BIT(13)
Wilson Ding30530792016-02-16 19:14:53 +010070#define STAT_TX_FIFO_FUL BIT(11)
Wilson Ding30530792016-02-16 19:14:53 +010071#define STAT_TX_EMP BIT(6)
Miquel Raynal5218d762017-10-13 11:01:49 +020072#define STAT_STD_TX_RDY BIT(5)
Miquel Raynal53501e02017-10-13 11:01:56 +020073#define STAT_EXT_TX_RDY BIT(15)
Miquel Raynal5218d762017-10-13 11:01:49 +020074#define STAT_STD_RX_RDY BIT(4)
Miquel Raynal53501e02017-10-13 11:01:56 +020075#define STAT_EXT_RX_RDY BIT(14)
Wilson Ding30530792016-02-16 19:14:53 +010076#define STAT_BRK_DET BIT(3)
77#define STAT_FRM_ERR BIT(2)
78#define STAT_PAR_ERR BIT(1)
79#define STAT_OVR_ERR BIT(0)
80#define STAT_BRK_ERR (STAT_BRK_DET | STAT_FRM_ERR | STAT_FRM_ERR\
81 | STAT_PAR_ERR | STAT_OVR_ERR)
82
83#define UART_BRDV 0x10
Allen Yan68a0db12017-10-13 11:01:51 +020084#define BRDV_BAUD_MASK 0x3FF
Wilson Ding30530792016-02-16 19:14:53 +010085
Miquel Raynal3a75e912017-10-13 11:01:55 +020086#define MVEBU_NR_UARTS 2
Wilson Ding30530792016-02-16 19:14:53 +010087
88#define MVEBU_UART_TYPE "mvebu-uart"
Yehuda Yitschak02c33332017-10-13 11:01:47 +020089#define DRIVER_NAME "mvebu_serial"
Wilson Ding30530792016-02-16 19:14:53 +010090
Miquel Raynal95f78762017-10-13 11:01:54 +020091enum {
92 /* Either there is only one summed IRQ... */
93 UART_IRQ_SUM = 0,
94 /* ...or there are two separate IRQ for RX and TX */
95 UART_RX_IRQ = 0,
96 UART_TX_IRQ,
97 UART_IRQ_COUNT
98};
99
100/* Diverging register offsets */
Miquel Raynal5218d762017-10-13 11:01:49 +0200101struct uart_regs_layout {
102 unsigned int rbr;
103 unsigned int tsh;
104 unsigned int ctrl;
105 unsigned int intr;
Wilson Ding30530792016-02-16 19:14:53 +0100106};
107
Miquel Raynal5218d762017-10-13 11:01:49 +0200108/* Diverging flags */
109struct uart_flags {
110 unsigned int ctrl_tx_rdy_int;
111 unsigned int ctrl_rx_rdy_int;
112 unsigned int stat_tx_rdy;
113 unsigned int stat_rx_rdy;
114};
115
116/* Driver data, a structure for each UART port */
117struct mvebu_uart_driver_data {
118 bool is_ext;
119 struct uart_regs_layout regs;
120 struct uart_flags flags;
121};
122
123/* MVEBU UART driver structure */
124struct mvebu_uart {
125 struct uart_port *port;
126 struct clk *clk;
Miquel Raynal95f78762017-10-13 11:01:54 +0200127 int irq[UART_IRQ_COUNT];
128 unsigned char __iomem *nb;
Miquel Raynal5218d762017-10-13 11:01:49 +0200129 struct mvebu_uart_driver_data *data;
130};
131
132static struct mvebu_uart *to_mvuart(struct uart_port *port)
133{
134 return (struct mvebu_uart *)port->private_data;
135}
136
137#define IS_EXTENDED(port) (to_mvuart(port)->data->is_ext)
138
139#define UART_RBR(port) (to_mvuart(port)->data->regs.rbr)
140#define UART_TSH(port) (to_mvuart(port)->data->regs.tsh)
141#define UART_CTRL(port) (to_mvuart(port)->data->regs.ctrl)
142#define UART_INTR(port) (to_mvuart(port)->data->regs.intr)
143
144#define CTRL_TX_RDY_INT(port) (to_mvuart(port)->data->flags.ctrl_tx_rdy_int)
145#define CTRL_RX_RDY_INT(port) (to_mvuart(port)->data->flags.ctrl_rx_rdy_int)
146#define STAT_TX_RDY(port) (to_mvuart(port)->data->flags.stat_tx_rdy)
147#define STAT_RX_RDY(port) (to_mvuart(port)->data->flags.stat_rx_rdy)
148
149static struct uart_port mvebu_uart_ports[MVEBU_NR_UARTS];
150
Wilson Ding30530792016-02-16 19:14:53 +0100151/* Core UART Driver Operations */
152static unsigned int mvebu_uart_tx_empty(struct uart_port *port)
153{
154 unsigned long flags;
155 unsigned int st;
156
157 spin_lock_irqsave(&port->lock, flags);
158 st = readl(port->membase + UART_STAT);
159 spin_unlock_irqrestore(&port->lock, flags);
160
161 return (st & STAT_TX_FIFO_EMP) ? TIOCSER_TEMT : 0;
162}
163
164static unsigned int mvebu_uart_get_mctrl(struct uart_port *port)
165{
166 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
167}
168
169static void mvebu_uart_set_mctrl(struct uart_port *port,
170 unsigned int mctrl)
171{
172/*
173 * Even if we do not support configuring the modem control lines, this
174 * function must be proided to the serial core
175 */
176}
177
178static void mvebu_uart_stop_tx(struct uart_port *port)
179{
Miquel Raynal5218d762017-10-13 11:01:49 +0200180 unsigned int ctl = readl(port->membase + UART_INTR(port));
Wilson Ding30530792016-02-16 19:14:53 +0100181
Miquel Raynal5218d762017-10-13 11:01:49 +0200182 ctl &= ~CTRL_TX_RDY_INT(port);
183 writel(ctl, port->membase + UART_INTR(port));
Wilson Ding30530792016-02-16 19:14:53 +0100184}
185
186static void mvebu_uart_start_tx(struct uart_port *port)
187{
Allen Yan30434b02017-10-13 11:01:53 +0200188 unsigned int ctl;
189 struct circ_buf *xmit = &port->state->xmit;
Wilson Ding30530792016-02-16 19:14:53 +0100190
Allen Yan30434b02017-10-13 11:01:53 +0200191 if (IS_EXTENDED(port) && !uart_circ_empty(xmit)) {
192 writel(xmit->buf[xmit->tail], port->membase + UART_TSH(port));
193 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
194 port->icount.tx++;
195 }
196
197 ctl = readl(port->membase + UART_INTR(port));
Miquel Raynal5218d762017-10-13 11:01:49 +0200198 ctl |= CTRL_TX_RDY_INT(port);
199 writel(ctl, port->membase + UART_INTR(port));
Wilson Ding30530792016-02-16 19:14:53 +0100200}
201
202static void mvebu_uart_stop_rx(struct uart_port *port)
203{
Miquel Raynal5218d762017-10-13 11:01:49 +0200204 unsigned int ctl;
Wilson Ding30530792016-02-16 19:14:53 +0100205
Miquel Raynal5218d762017-10-13 11:01:49 +0200206 ctl = readl(port->membase + UART_CTRL(port));
207 ctl &= ~CTRL_BRK_INT;
208 writel(ctl, port->membase + UART_CTRL(port));
209
210 ctl = readl(port->membase + UART_INTR(port));
211 ctl &= ~CTRL_RX_RDY_INT(port);
212 writel(ctl, port->membase + UART_INTR(port));
Wilson Ding30530792016-02-16 19:14:53 +0100213}
214
215static void mvebu_uart_break_ctl(struct uart_port *port, int brk)
216{
217 unsigned int ctl;
218 unsigned long flags;
219
220 spin_lock_irqsave(&port->lock, flags);
Miquel Raynal5218d762017-10-13 11:01:49 +0200221 ctl = readl(port->membase + UART_CTRL(port));
Wilson Ding30530792016-02-16 19:14:53 +0100222 if (brk == -1)
223 ctl |= CTRL_SND_BRK_SEQ;
224 else
225 ctl &= ~CTRL_SND_BRK_SEQ;
Miquel Raynal5218d762017-10-13 11:01:49 +0200226 writel(ctl, port->membase + UART_CTRL(port));
Wilson Ding30530792016-02-16 19:14:53 +0100227 spin_unlock_irqrestore(&port->lock, flags);
228}
229
230static void mvebu_uart_rx_chars(struct uart_port *port, unsigned int status)
231{
232 struct tty_port *tport = &port->state->port;
233 unsigned char ch = 0;
234 char flag = 0;
235
236 do {
Miquel Raynal5218d762017-10-13 11:01:49 +0200237 if (status & STAT_RX_RDY(port)) {
238 ch = readl(port->membase + UART_RBR(port));
Wilson Ding30530792016-02-16 19:14:53 +0100239 ch &= 0xff;
240 flag = TTY_NORMAL;
241 port->icount.rx++;
242
243 if (status & STAT_PAR_ERR)
244 port->icount.parity++;
245 }
246
247 if (status & STAT_BRK_DET) {
248 port->icount.brk++;
249 status &= ~(STAT_FRM_ERR | STAT_PAR_ERR);
250 if (uart_handle_break(port))
251 goto ignore_char;
252 }
253
254 if (status & STAT_OVR_ERR)
255 port->icount.overrun++;
256
257 if (status & STAT_FRM_ERR)
258 port->icount.frame++;
259
260 if (uart_handle_sysrq_char(port, ch))
261 goto ignore_char;
262
263 if (status & port->ignore_status_mask & STAT_PAR_ERR)
Miquel Raynal5218d762017-10-13 11:01:49 +0200264 status &= ~STAT_RX_RDY(port);
Wilson Ding30530792016-02-16 19:14:53 +0100265
266 status &= port->read_status_mask;
267
268 if (status & STAT_PAR_ERR)
269 flag = TTY_PARITY;
270
271 status &= ~port->ignore_status_mask;
272
Miquel Raynal5218d762017-10-13 11:01:49 +0200273 if (status & STAT_RX_RDY(port))
Wilson Ding30530792016-02-16 19:14:53 +0100274 tty_insert_flip_char(tport, ch, flag);
275
276 if (status & STAT_BRK_DET)
277 tty_insert_flip_char(tport, 0, TTY_BREAK);
278
279 if (status & STAT_FRM_ERR)
280 tty_insert_flip_char(tport, 0, TTY_FRAME);
281
282 if (status & STAT_OVR_ERR)
283 tty_insert_flip_char(tport, 0, TTY_OVERRUN);
284
285ignore_char:
286 status = readl(port->membase + UART_STAT);
Miquel Raynal5218d762017-10-13 11:01:49 +0200287 } while (status & (STAT_RX_RDY(port) | STAT_BRK_DET));
Wilson Ding30530792016-02-16 19:14:53 +0100288
289 tty_flip_buffer_push(tport);
290}
291
292static void mvebu_uart_tx_chars(struct uart_port *port, unsigned int status)
293{
294 struct circ_buf *xmit = &port->state->xmit;
295 unsigned int count;
296 unsigned int st;
297
298 if (port->x_char) {
Miquel Raynal5218d762017-10-13 11:01:49 +0200299 writel(port->x_char, port->membase + UART_TSH(port));
Wilson Ding30530792016-02-16 19:14:53 +0100300 port->icount.tx++;
301 port->x_char = 0;
302 return;
303 }
304
305 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
306 mvebu_uart_stop_tx(port);
307 return;
308 }
309
310 for (count = 0; count < port->fifosize; count++) {
Miquel Raynal5218d762017-10-13 11:01:49 +0200311 writel(xmit->buf[xmit->tail], port->membase + UART_TSH(port));
Wilson Ding30530792016-02-16 19:14:53 +0100312 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
313 port->icount.tx++;
314
315 if (uart_circ_empty(xmit))
316 break;
317
318 st = readl(port->membase + UART_STAT);
319 if (st & STAT_TX_FIFO_FUL)
320 break;
321 }
322
323 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
324 uart_write_wakeup(port);
325
326 if (uart_circ_empty(xmit))
327 mvebu_uart_stop_tx(port);
328}
329
330static irqreturn_t mvebu_uart_isr(int irq, void *dev_id)
331{
332 struct uart_port *port = (struct uart_port *)dev_id;
333 unsigned int st = readl(port->membase + UART_STAT);
334
Miquel Raynal5218d762017-10-13 11:01:49 +0200335 if (st & (STAT_RX_RDY(port) | STAT_OVR_ERR | STAT_FRM_ERR |
Miquel Raynal95f78762017-10-13 11:01:54 +0200336 STAT_BRK_DET))
337 mvebu_uart_rx_chars(port, st);
338
339 if (st & STAT_TX_RDY(port))
340 mvebu_uart_tx_chars(port, st);
341
342 return IRQ_HANDLED;
343}
344
345static irqreturn_t mvebu_uart_rx_isr(int irq, void *dev_id)
346{
347 struct uart_port *port = (struct uart_port *)dev_id;
348 unsigned int st = readl(port->membase + UART_STAT);
349
350 if (st & (STAT_RX_RDY(port) | STAT_OVR_ERR | STAT_FRM_ERR |
Miquel Raynal5218d762017-10-13 11:01:49 +0200351 STAT_BRK_DET))
Wilson Ding30530792016-02-16 19:14:53 +0100352 mvebu_uart_rx_chars(port, st);
353
Miquel Raynal95f78762017-10-13 11:01:54 +0200354 return IRQ_HANDLED;
355}
356
357static irqreturn_t mvebu_uart_tx_isr(int irq, void *dev_id)
358{
359 struct uart_port *port = (struct uart_port *)dev_id;
360 unsigned int st = readl(port->membase + UART_STAT);
361
Miquel Raynal5218d762017-10-13 11:01:49 +0200362 if (st & STAT_TX_RDY(port))
Wilson Ding30530792016-02-16 19:14:53 +0100363 mvebu_uart_tx_chars(port, st);
364
365 return IRQ_HANDLED;
366}
367
368static int mvebu_uart_startup(struct uart_port *port)
369{
Miquel Raynal95f78762017-10-13 11:01:54 +0200370 struct mvebu_uart *mvuart = to_mvuart(port);
Miquel Raynal5218d762017-10-13 11:01:49 +0200371 unsigned int ctl;
Wilson Ding30530792016-02-16 19:14:53 +0100372 int ret;
373
374 writel(CTRL_TXFIFO_RST | CTRL_RXFIFO_RST,
Miquel Raynal5218d762017-10-13 11:01:49 +0200375 port->membase + UART_CTRL(port));
Wilson Ding30530792016-02-16 19:14:53 +0100376 udelay(1);
Allen Yan2ff23c42017-10-13 11:01:52 +0200377
378 /* Clear the error bits of state register before IRQ request */
379 ret = readl(port->membase + UART_STAT);
380 ret |= STAT_BRK_ERR;
381 writel(ret, port->membase + UART_STAT);
382
Miquel Raynal5218d762017-10-13 11:01:49 +0200383 writel(CTRL_BRK_INT, port->membase + UART_CTRL(port));
384
385 ctl = readl(port->membase + UART_INTR(port));
386 ctl |= CTRL_RX_RDY_INT(port);
387 writel(ctl, port->membase + UART_INTR(port));
Wilson Ding30530792016-02-16 19:14:53 +0100388
Miquel Raynal95f78762017-10-13 11:01:54 +0200389 if (!mvuart->irq[UART_TX_IRQ]) {
390 /* Old bindings with just one interrupt (UART0 only) */
391 ret = devm_request_irq(port->dev, mvuart->irq[UART_IRQ_SUM],
392 mvebu_uart_isr, port->irqflags,
393 dev_name(port->dev), port);
394 if (ret) {
395 dev_err(port->dev, "unable to request IRQ %d\n",
396 mvuart->irq[UART_IRQ_SUM]);
397 return ret;
398 }
399 } else {
400 /* New bindings with an IRQ for RX and TX (both UART) */
401 ret = devm_request_irq(port->dev, mvuart->irq[UART_RX_IRQ],
402 mvebu_uart_rx_isr, port->irqflags,
403 dev_name(port->dev), port);
404 if (ret) {
405 dev_err(port->dev, "unable to request IRQ %d\n",
406 mvuart->irq[UART_RX_IRQ]);
407 return ret;
408 }
409
410 ret = devm_request_irq(port->dev, mvuart->irq[UART_TX_IRQ],
411 mvebu_uart_tx_isr, port->irqflags,
412 dev_name(port->dev),
413 port);
414 if (ret) {
415 dev_err(port->dev, "unable to request IRQ %d\n",
416 mvuart->irq[UART_TX_IRQ]);
417 devm_free_irq(port->dev, mvuart->irq[UART_RX_IRQ],
418 port);
419 return ret;
420 }
Wilson Ding30530792016-02-16 19:14:53 +0100421 }
422
423 return 0;
424}
425
426static void mvebu_uart_shutdown(struct uart_port *port)
427{
Miquel Raynal95f78762017-10-13 11:01:54 +0200428 struct mvebu_uart *mvuart = to_mvuart(port);
429
Miquel Raynal5218d762017-10-13 11:01:49 +0200430 writel(0, port->membase + UART_INTR(port));
Thomas Petazzonic2c16592016-06-16 16:48:52 +0200431
Miquel Raynal95f78762017-10-13 11:01:54 +0200432 if (!mvuart->irq[UART_TX_IRQ]) {
433 devm_free_irq(port->dev, mvuart->irq[UART_IRQ_SUM], port);
434 } else {
435 devm_free_irq(port->dev, mvuart->irq[UART_RX_IRQ], port);
436 devm_free_irq(port->dev, mvuart->irq[UART_TX_IRQ], port);
437 }
Wilson Ding30530792016-02-16 19:14:53 +0100438}
439
Allen Yan68a0db12017-10-13 11:01:51 +0200440static int mvebu_uart_baud_rate_set(struct uart_port *port, unsigned int baud)
441{
442 struct mvebu_uart *mvuart = to_mvuart(port);
443 unsigned int baud_rate_div;
444 u32 brdv;
445
446 if (IS_ERR(mvuart->clk))
447 return -PTR_ERR(mvuart->clk);
448
449 /*
450 * The UART clock is divided by the value of the divisor to generate
451 * UCLK_OUT clock, which is 16 times faster than the baudrate.
452 * This prescaler can achieve all standard baudrates until 230400.
453 * Higher baudrates could be achieved for the extended UART by using the
454 * programmable oversampling stack (also called fractional divisor).
455 */
456 baud_rate_div = DIV_ROUND_UP(port->uartclk, baud * 16);
457 brdv = readl(port->membase + UART_BRDV);
458 brdv &= ~BRDV_BAUD_MASK;
459 brdv |= baud_rate_div;
460 writel(brdv, port->membase + UART_BRDV);
461
462 return 0;
463}
464
Wilson Ding30530792016-02-16 19:14:53 +0100465static void mvebu_uart_set_termios(struct uart_port *port,
466 struct ktermios *termios,
467 struct ktermios *old)
468{
469 unsigned long flags;
470 unsigned int baud;
471
472 spin_lock_irqsave(&port->lock, flags);
473
Miquel Raynal5218d762017-10-13 11:01:49 +0200474 port->read_status_mask = STAT_RX_RDY(port) | STAT_OVR_ERR |
475 STAT_TX_RDY(port) | STAT_TX_FIFO_FUL;
Wilson Ding30530792016-02-16 19:14:53 +0100476
477 if (termios->c_iflag & INPCK)
478 port->read_status_mask |= STAT_FRM_ERR | STAT_PAR_ERR;
479
480 port->ignore_status_mask = 0;
481 if (termios->c_iflag & IGNPAR)
482 port->ignore_status_mask |=
483 STAT_FRM_ERR | STAT_PAR_ERR | STAT_OVR_ERR;
484
485 if ((termios->c_cflag & CREAD) == 0)
Miquel Raynal5218d762017-10-13 11:01:49 +0200486 port->ignore_status_mask |= STAT_RX_RDY(port) | STAT_BRK_ERR;
Wilson Ding30530792016-02-16 19:14:53 +0100487
Allen Yan68a0db12017-10-13 11:01:51 +0200488 /*
489 * Maximum achievable frequency with simple baudrate divisor is 230400.
490 * Since the error per bit frame would be of more than 15%, achieving
491 * higher frequencies would require to implement the fractional divisor
492 * feature.
493 */
494 baud = uart_get_baud_rate(port, termios, old, 0, 230400);
495 if (mvebu_uart_baud_rate_set(port, baud)) {
496 /* No clock available, baudrate cannot be changed */
497 if (old)
498 baud = uart_get_baud_rate(port, old, NULL, 0, 230400);
499 } else {
500 tty_termios_encode_baud_rate(termios, baud, baud);
501 uart_update_timeout(port, termios->c_cflag, baud);
502 }
Wilson Ding30530792016-02-16 19:14:53 +0100503
Allen Yan68a0db12017-10-13 11:01:51 +0200504 /* Only the following flag changes are supported */
505 if (old) {
506 termios->c_iflag &= INPCK | IGNPAR;
507 termios->c_iflag |= old->c_iflag & ~(INPCK | IGNPAR);
508 termios->c_cflag &= CREAD | CBAUD;
509 termios->c_cflag |= old->c_cflag & ~(CREAD | CBAUD);
510 termios->c_lflag = old->c_lflag;
511 }
Wilson Ding30530792016-02-16 19:14:53 +0100512
513 spin_unlock_irqrestore(&port->lock, flags);
514}
515
516static const char *mvebu_uart_type(struct uart_port *port)
517{
518 return MVEBU_UART_TYPE;
519}
520
521static void mvebu_uart_release_port(struct uart_port *port)
522{
523 /* Nothing to do here */
524}
525
526static int mvebu_uart_request_port(struct uart_port *port)
527{
528 return 0;
529}
530
531#ifdef CONFIG_CONSOLE_POLL
532static int mvebu_uart_get_poll_char(struct uart_port *port)
533{
534 unsigned int st = readl(port->membase + UART_STAT);
535
Miquel Raynal5218d762017-10-13 11:01:49 +0200536 if (!(st & STAT_RX_RDY(port)))
Wilson Ding30530792016-02-16 19:14:53 +0100537 return NO_POLL_CHAR;
538
Miquel Raynal5218d762017-10-13 11:01:49 +0200539 return readl(port->membase + UART_RBR(port));
Wilson Ding30530792016-02-16 19:14:53 +0100540}
541
542static void mvebu_uart_put_poll_char(struct uart_port *port, unsigned char c)
543{
544 unsigned int st;
545
546 for (;;) {
547 st = readl(port->membase + UART_STAT);
548
549 if (!(st & STAT_TX_FIFO_FUL))
550 break;
551
552 udelay(1);
553 }
554
Miquel Raynal5218d762017-10-13 11:01:49 +0200555 writel(c, port->membase + UART_TSH(port));
Wilson Ding30530792016-02-16 19:14:53 +0100556}
557#endif
558
559static const struct uart_ops mvebu_uart_ops = {
560 .tx_empty = mvebu_uart_tx_empty,
561 .set_mctrl = mvebu_uart_set_mctrl,
562 .get_mctrl = mvebu_uart_get_mctrl,
563 .stop_tx = mvebu_uart_stop_tx,
564 .start_tx = mvebu_uart_start_tx,
565 .stop_rx = mvebu_uart_stop_rx,
566 .break_ctl = mvebu_uart_break_ctl,
567 .startup = mvebu_uart_startup,
568 .shutdown = mvebu_uart_shutdown,
569 .set_termios = mvebu_uart_set_termios,
570 .type = mvebu_uart_type,
571 .release_port = mvebu_uart_release_port,
572 .request_port = mvebu_uart_request_port,
573#ifdef CONFIG_CONSOLE_POLL
574 .poll_get_char = mvebu_uart_get_poll_char,
575 .poll_put_char = mvebu_uart_put_poll_char,
576#endif
577};
578
579/* Console Driver Operations */
580
581#ifdef CONFIG_SERIAL_MVEBU_CONSOLE
582/* Early Console */
583static void mvebu_uart_putc(struct uart_port *port, int c)
584{
585 unsigned int st;
586
587 for (;;) {
588 st = readl(port->membase + UART_STAT);
589 if (!(st & STAT_TX_FIFO_FUL))
590 break;
591 }
592
Miquel Raynal5218d762017-10-13 11:01:49 +0200593 /* At early stage, DT is not parsed yet, only use UART0 */
594 writel(c, port->membase + UART_STD_TSH);
Wilson Ding30530792016-02-16 19:14:53 +0100595
596 for (;;) {
597 st = readl(port->membase + UART_STAT);
598 if (st & STAT_TX_FIFO_EMP)
599 break;
600 }
601}
602
603static void mvebu_uart_putc_early_write(struct console *con,
604 const char *s,
605 unsigned n)
606{
607 struct earlycon_device *dev = con->data;
608
609 uart_console_write(&dev->port, s, n, mvebu_uart_putc);
610}
611
612static int __init
613mvebu_uart_early_console_setup(struct earlycon_device *device,
614 const char *opt)
615{
616 if (!device->port.membase)
617 return -ENODEV;
618
619 device->con->write = mvebu_uart_putc_early_write;
620
621 return 0;
622}
623
624EARLYCON_DECLARE(ar3700_uart, mvebu_uart_early_console_setup);
625OF_EARLYCON_DECLARE(ar3700_uart, "marvell,armada-3700-uart",
626 mvebu_uart_early_console_setup);
627
628static void wait_for_xmitr(struct uart_port *port)
629{
630 u32 val;
631
632 readl_poll_timeout_atomic(port->membase + UART_STAT, val,
633 (val & STAT_TX_EMP), 1, 10000);
634}
635
636static void mvebu_uart_console_putchar(struct uart_port *port, int ch)
637{
638 wait_for_xmitr(port);
Miquel Raynal5218d762017-10-13 11:01:49 +0200639 writel(ch, port->membase + UART_TSH(port));
Wilson Ding30530792016-02-16 19:14:53 +0100640}
641
642static void mvebu_uart_console_write(struct console *co, const char *s,
643 unsigned int count)
644{
645 struct uart_port *port = &mvebu_uart_ports[co->index];
646 unsigned long flags;
Miquel Raynal5218d762017-10-13 11:01:49 +0200647 unsigned int ier, intr, ctl;
Wilson Ding30530792016-02-16 19:14:53 +0100648 int locked = 1;
649
650 if (oops_in_progress)
651 locked = spin_trylock_irqsave(&port->lock, flags);
652 else
653 spin_lock_irqsave(&port->lock, flags);
654
Miquel Raynal5218d762017-10-13 11:01:49 +0200655 ier = readl(port->membase + UART_CTRL(port)) & CTRL_BRK_INT;
656 intr = readl(port->membase + UART_INTR(port)) &
657 (CTRL_RX_RDY_INT(port) | CTRL_TX_RDY_INT(port));
658 writel(0, port->membase + UART_CTRL(port));
659 writel(0, port->membase + UART_INTR(port));
Wilson Ding30530792016-02-16 19:14:53 +0100660
661 uart_console_write(port, s, count, mvebu_uart_console_putchar);
662
663 wait_for_xmitr(port);
664
665 if (ier)
Miquel Raynal5218d762017-10-13 11:01:49 +0200666 writel(ier, port->membase + UART_CTRL(port));
667
668 if (intr) {
669 ctl = intr | readl(port->membase + UART_INTR(port));
670 writel(ctl, port->membase + UART_INTR(port));
671 }
Wilson Ding30530792016-02-16 19:14:53 +0100672
673 if (locked)
674 spin_unlock_irqrestore(&port->lock, flags);
675}
676
677static int mvebu_uart_console_setup(struct console *co, char *options)
678{
679 struct uart_port *port;
680 int baud = 9600;
681 int bits = 8;
682 int parity = 'n';
683 int flow = 'n';
684
685 if (co->index < 0 || co->index >= MVEBU_NR_UARTS)
686 return -EINVAL;
687
688 port = &mvebu_uart_ports[co->index];
689
690 if (!port->mapbase || !port->membase) {
691 pr_debug("console on ttyMV%i not present\n", co->index);
692 return -ENODEV;
693 }
694
695 if (options)
696 uart_parse_options(options, &baud, &parity, &bits, &flow);
697
698 return uart_set_options(port, co, baud, parity, bits, flow);
699}
700
701static struct uart_driver mvebu_uart_driver;
702
703static struct console mvebu_uart_console = {
704 .name = "ttyMV",
705 .write = mvebu_uart_console_write,
706 .device = uart_console_device,
707 .setup = mvebu_uart_console_setup,
708 .flags = CON_PRINTBUFFER,
709 .index = -1,
710 .data = &mvebu_uart_driver,
711};
712
713static int __init mvebu_uart_console_init(void)
714{
715 register_console(&mvebu_uart_console);
716 return 0;
717}
718
719console_initcall(mvebu_uart_console_init);
720
721
722#endif /* CONFIG_SERIAL_MVEBU_CONSOLE */
723
724static struct uart_driver mvebu_uart_driver = {
725 .owner = THIS_MODULE,
Yehuda Yitschak02c33332017-10-13 11:01:47 +0200726 .driver_name = DRIVER_NAME,
Wilson Ding30530792016-02-16 19:14:53 +0100727 .dev_name = "ttyMV",
728 .nr = MVEBU_NR_UARTS,
729#ifdef CONFIG_SERIAL_MVEBU_CONSOLE
730 .cons = &mvebu_uart_console,
731#endif
732};
733
Miquel Raynal5218d762017-10-13 11:01:49 +0200734static const struct of_device_id mvebu_uart_of_match[];
735
Allen Yan94228f92017-10-13 11:01:48 +0200736/* Counter to keep track of each UART port id when not using CONFIG_OF */
737static int uart_num_counter;
738
Wilson Ding30530792016-02-16 19:14:53 +0100739static int mvebu_uart_probe(struct platform_device *pdev)
740{
741 struct resource *reg = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Miquel Raynal5218d762017-10-13 11:01:49 +0200742 const struct of_device_id *match = of_match_device(mvebu_uart_of_match,
743 &pdev->dev);
Wilson Ding30530792016-02-16 19:14:53 +0100744 struct uart_port *port;
Miquel Raynal5218d762017-10-13 11:01:49 +0200745 struct mvebu_uart *mvuart;
Miquel Raynal95f78762017-10-13 11:01:54 +0200746 int ret, id, irq;
Wilson Ding30530792016-02-16 19:14:53 +0100747
Miquel Raynal95f78762017-10-13 11:01:54 +0200748 if (!reg) {
749 dev_err(&pdev->dev, "no registers defined\n");
Wilson Ding30530792016-02-16 19:14:53 +0100750 return -EINVAL;
751 }
752
Allen Yan94228f92017-10-13 11:01:48 +0200753 /* Assume that all UART ports have a DT alias or none has */
754 id = of_alias_get_id(pdev->dev.of_node, "serial");
755 if (!pdev->dev.of_node || id < 0)
756 pdev->id = uart_num_counter++;
757 else
758 pdev->id = id;
759
760 if (pdev->id >= MVEBU_NR_UARTS) {
761 dev_err(&pdev->dev, "cannot have more than %d UART ports\n",
762 MVEBU_NR_UARTS);
763 return -EINVAL;
764 }
765
766 port = &mvebu_uart_ports[pdev->id];
Wilson Ding30530792016-02-16 19:14:53 +0100767
768 spin_lock_init(&port->lock);
769
770 port->dev = &pdev->dev;
771 port->type = PORT_MVEBU;
772 port->ops = &mvebu_uart_ops;
773 port->regshift = 0;
774
775 port->fifosize = 32;
776 port->iotype = UPIO_MEM32;
777 port->flags = UPF_FIXED_PORT;
Allen Yan94228f92017-10-13 11:01:48 +0200778 port->line = pdev->id;
Wilson Ding30530792016-02-16 19:14:53 +0100779
Miquel Raynal95f78762017-10-13 11:01:54 +0200780 /*
781 * IRQ number is not stored in this structure because we may have two of
782 * them per port (RX and TX). Instead, use the driver UART structure
783 * array so called ->irq[].
784 */
785 port->irq = 0;
Wilson Ding30530792016-02-16 19:14:53 +0100786 port->irqflags = 0;
787 port->mapbase = reg->start;
788
789 port->membase = devm_ioremap_resource(&pdev->dev, reg);
790 if (IS_ERR(port->membase))
791 return -PTR_ERR(port->membase);
792
Miquel Raynal5218d762017-10-13 11:01:49 +0200793 mvuart = devm_kzalloc(&pdev->dev, sizeof(struct mvebu_uart),
794 GFP_KERNEL);
795 if (!mvuart)
Wilson Ding30530792016-02-16 19:14:53 +0100796 return -ENOMEM;
797
Allen Yan68a0db12017-10-13 11:01:51 +0200798 /* Get controller data depending on the compatible string */
Miquel Raynal5218d762017-10-13 11:01:49 +0200799 mvuart->data = (struct mvebu_uart_driver_data *)match->data;
800 mvuart->port = port;
Wilson Ding30530792016-02-16 19:14:53 +0100801
Miquel Raynal5218d762017-10-13 11:01:49 +0200802 port->private_data = mvuart;
803 platform_set_drvdata(pdev, mvuart);
Wilson Ding30530792016-02-16 19:14:53 +0100804
Allen Yan68a0db12017-10-13 11:01:51 +0200805 /* Get fixed clock frequency */
806 mvuart->clk = devm_clk_get(&pdev->dev, NULL);
807 if (IS_ERR(mvuart->clk)) {
808 if (PTR_ERR(mvuart->clk) == -EPROBE_DEFER)
809 return PTR_ERR(mvuart->clk);
810
811 if (IS_EXTENDED(port)) {
812 dev_err(&pdev->dev, "unable to get UART clock\n");
813 return PTR_ERR(mvuart->clk);
814 }
815 } else {
816 if (!clk_prepare_enable(mvuart->clk))
817 port->uartclk = clk_get_rate(mvuart->clk);
818 }
819
Miquel Raynal95f78762017-10-13 11:01:54 +0200820 /* Manage interrupts */
Miquel Raynal95f78762017-10-13 11:01:54 +0200821 if (platform_irq_count(pdev) == 1) {
822 /* Old bindings: no name on the single unamed UART0 IRQ */
823 irq = platform_get_irq(pdev, 0);
824 if (irq < 0) {
825 dev_err(&pdev->dev, "unable to get UART IRQ\n");
826 return irq;
827 }
828
829 mvuart->irq[UART_IRQ_SUM] = irq;
830 } else {
831 /*
832 * New bindings: named interrupts (RX, TX) for both UARTS,
833 * only make use of uart-rx and uart-tx interrupts, do not use
834 * uart-sum of UART0 port.
835 */
836 irq = platform_get_irq_byname(pdev, "uart-rx");
837 if (irq < 0) {
838 dev_err(&pdev->dev, "unable to get 'uart-rx' IRQ\n");
839 return irq;
840 }
841
842 mvuart->irq[UART_RX_IRQ] = irq;
843
844 irq = platform_get_irq_byname(pdev, "uart-tx");
845 if (irq < 0) {
846 dev_err(&pdev->dev, "unable to get 'uart-tx' IRQ\n");
847 return irq;
848 }
849
850 mvuart->irq[UART_TX_IRQ] = irq;
851 }
852
Allen Yan9c3d3ee2017-10-13 11:01:50 +0200853 /* UART Soft Reset*/
854 writel(CTRL_SOFT_RST, port->membase + UART_CTRL(port));
855 udelay(1);
856 writel(0, port->membase + UART_CTRL(port));
857
Wilson Ding30530792016-02-16 19:14:53 +0100858 ret = uart_add_one_port(&mvebu_uart_driver, port);
859 if (ret)
860 return ret;
861 return 0;
862}
863
Miquel Raynal5218d762017-10-13 11:01:49 +0200864static struct mvebu_uart_driver_data uart_std_driver_data = {
865 .is_ext = false,
866 .regs.rbr = UART_STD_RBR,
867 .regs.tsh = UART_STD_TSH,
868 .regs.ctrl = UART_STD_CTRL1,
869 .regs.intr = UART_STD_CTRL2,
870 .flags.ctrl_tx_rdy_int = CTRL_STD_TX_RDY_INT,
871 .flags.ctrl_rx_rdy_int = CTRL_STD_RX_RDY_INT,
872 .flags.stat_tx_rdy = STAT_STD_TX_RDY,
873 .flags.stat_rx_rdy = STAT_STD_RX_RDY,
874};
875
Miquel Raynal53501e02017-10-13 11:01:56 +0200876static struct mvebu_uart_driver_data uart_ext_driver_data = {
877 .is_ext = true,
878 .regs.rbr = UART_EXT_RBR,
879 .regs.tsh = UART_EXT_TSH,
880 .regs.ctrl = UART_EXT_CTRL1,
881 .regs.intr = UART_EXT_CTRL2,
882 .flags.ctrl_tx_rdy_int = CTRL_EXT_TX_RDY_INT,
883 .flags.ctrl_rx_rdy_int = CTRL_EXT_RX_RDY_INT,
884 .flags.stat_tx_rdy = STAT_EXT_TX_RDY,
885 .flags.stat_rx_rdy = STAT_EXT_RX_RDY,
886};
887
Wilson Ding30530792016-02-16 19:14:53 +0100888/* Match table for of_platform binding */
889static const struct of_device_id mvebu_uart_of_match[] = {
Miquel Raynal5218d762017-10-13 11:01:49 +0200890 {
891 .compatible = "marvell,armada-3700-uart",
892 .data = (void *)&uart_std_driver_data,
893 },
Miquel Raynal53501e02017-10-13 11:01:56 +0200894 {
895 .compatible = "marvell,armada-3700-uart-ext",
896 .data = (void *)&uart_ext_driver_data,
897 },
Wilson Ding30530792016-02-16 19:14:53 +0100898 {}
899};
Wilson Ding30530792016-02-16 19:14:53 +0100900
901static struct platform_driver mvebu_uart_platform_driver = {
902 .probe = mvebu_uart_probe,
Wilson Ding30530792016-02-16 19:14:53 +0100903 .driver = {
Wilson Ding30530792016-02-16 19:14:53 +0100904 .name = "mvebu-uart",
905 .of_match_table = of_match_ptr(mvebu_uart_of_match),
Paul Gortmaker89ebc272016-03-13 19:48:52 -0400906 .suppress_bind_attrs = true,
Wilson Ding30530792016-02-16 19:14:53 +0100907 },
908};
909
910static int __init mvebu_uart_init(void)
911{
912 int ret;
913
914 ret = uart_register_driver(&mvebu_uart_driver);
915 if (ret)
916 return ret;
917
918 ret = platform_driver_register(&mvebu_uart_platform_driver);
919 if (ret)
920 uart_unregister_driver(&mvebu_uart_driver);
921
922 return ret;
923}
Wilson Ding30530792016-02-16 19:14:53 +0100924arch_initcall(mvebu_uart_init);