blob: e52248ec26895f9c72e363e5e80069a2d00f6026 [file] [log] [blame]
Wilson Ding30530792016-02-16 19:14:53 +01001/*
2* ***************************************************************************
Paul Gortmaker89ebc272016-03-13 19:48:52 -04003* Marvell Armada-3700 Serial Driver
4* Author: Wilson Ding <dingwei@marvell.com>
Wilson Ding30530792016-02-16 19:14:53 +01005* Copyright (C) 2015 Marvell International Ltd.
6* ***************************************************************************
7* This program is free software: you can redistribute it and/or modify it
8* under the terms of the GNU General Public License as published by the Free
9* Software Foundation, either version 2 of the License, or any later version.
10*
11* This program is distributed in the hope that it will be useful,
12* but WITHOUT ANY WARRANTY; without even the implied warranty of
13* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14* GNU General Public License for more details.
15*
16* You should have received a copy of the GNU General Public License
17* along with this program. If not, see <http://www.gnu.org/licenses/>.
18* ***************************************************************************
19*/
20
21#include <linux/clk.h>
22#include <linux/console.h>
23#include <linux/delay.h>
24#include <linux/device.h>
25#include <linux/init.h>
26#include <linux/io.h>
27#include <linux/iopoll.h>
Wilson Ding30530792016-02-16 19:14:53 +010028#include <linux/of.h>
29#include <linux/of_address.h>
30#include <linux/of_device.h>
31#include <linux/of_irq.h>
32#include <linux/of_platform.h>
33#include <linux/platform_device.h>
34#include <linux/serial.h>
35#include <linux/serial_core.h>
36#include <linux/slab.h>
37#include <linux/tty.h>
38#include <linux/tty_flip.h>
39
40/* Register Map */
Miquel Raynal5218d762017-10-13 11:01:49 +020041#define UART_STD_RBR 0x00
Wilson Ding30530792016-02-16 19:14:53 +010042
Miquel Raynal5218d762017-10-13 11:01:49 +020043#define UART_STD_TSH 0x04
Wilson Ding30530792016-02-16 19:14:53 +010044
Miquel Raynal5218d762017-10-13 11:01:49 +020045#define UART_STD_CTRL1 0x08
Wilson Ding30530792016-02-16 19:14:53 +010046#define CTRL_SOFT_RST BIT(31)
47#define CTRL_TXFIFO_RST BIT(15)
48#define CTRL_RXFIFO_RST BIT(14)
Wilson Ding30530792016-02-16 19:14:53 +010049#define CTRL_SND_BRK_SEQ BIT(11)
Wilson Ding30530792016-02-16 19:14:53 +010050#define CTRL_BRK_DET_INT BIT(3)
51#define CTRL_FRM_ERR_INT BIT(2)
52#define CTRL_PAR_ERR_INT BIT(1)
53#define CTRL_OVR_ERR_INT BIT(0)
Miquel Raynal5218d762017-10-13 11:01:49 +020054#define CTRL_BRK_INT (CTRL_BRK_DET_INT | CTRL_FRM_ERR_INT | \
55 CTRL_PAR_ERR_INT | CTRL_OVR_ERR_INT)
Wilson Ding30530792016-02-16 19:14:53 +010056
Miquel Raynal5218d762017-10-13 11:01:49 +020057#define UART_STD_CTRL2 UART_STD_CTRL1
58#define CTRL_STD_TX_RDY_INT BIT(5)
59#define CTRL_STD_RX_RDY_INT BIT(4)
60
61#define UART_STAT 0x0C
Wilson Ding30530792016-02-16 19:14:53 +010062#define STAT_TX_FIFO_EMP BIT(13)
Wilson Ding30530792016-02-16 19:14:53 +010063#define STAT_TX_FIFO_FUL BIT(11)
Wilson Ding30530792016-02-16 19:14:53 +010064#define STAT_TX_EMP BIT(6)
Miquel Raynal5218d762017-10-13 11:01:49 +020065#define STAT_STD_TX_RDY BIT(5)
66#define STAT_STD_RX_RDY BIT(4)
Wilson Ding30530792016-02-16 19:14:53 +010067#define STAT_BRK_DET BIT(3)
68#define STAT_FRM_ERR BIT(2)
69#define STAT_PAR_ERR BIT(1)
70#define STAT_OVR_ERR BIT(0)
71#define STAT_BRK_ERR (STAT_BRK_DET | STAT_FRM_ERR | STAT_FRM_ERR\
72 | STAT_PAR_ERR | STAT_OVR_ERR)
73
74#define UART_BRDV 0x10
Allen Yan68a0db12017-10-13 11:01:51 +020075#define BRDV_BAUD_MASK 0x3FF
Wilson Ding30530792016-02-16 19:14:53 +010076
77#define MVEBU_NR_UARTS 1
78
79#define MVEBU_UART_TYPE "mvebu-uart"
Yehuda Yitschak02c33332017-10-13 11:01:47 +020080#define DRIVER_NAME "mvebu_serial"
Wilson Ding30530792016-02-16 19:14:53 +010081
Miquel Raynal5218d762017-10-13 11:01:49 +020082/* Register offsets, different depending on the UART */
83struct uart_regs_layout {
84 unsigned int rbr;
85 unsigned int tsh;
86 unsigned int ctrl;
87 unsigned int intr;
Wilson Ding30530792016-02-16 19:14:53 +010088};
89
Miquel Raynal5218d762017-10-13 11:01:49 +020090/* Diverging flags */
91struct uart_flags {
92 unsigned int ctrl_tx_rdy_int;
93 unsigned int ctrl_rx_rdy_int;
94 unsigned int stat_tx_rdy;
95 unsigned int stat_rx_rdy;
96};
97
98/* Driver data, a structure for each UART port */
99struct mvebu_uart_driver_data {
100 bool is_ext;
101 struct uart_regs_layout regs;
102 struct uart_flags flags;
103};
104
105/* MVEBU UART driver structure */
106struct mvebu_uart {
107 struct uart_port *port;
108 struct clk *clk;
109 struct mvebu_uart_driver_data *data;
110};
111
112static struct mvebu_uart *to_mvuart(struct uart_port *port)
113{
114 return (struct mvebu_uart *)port->private_data;
115}
116
117#define IS_EXTENDED(port) (to_mvuart(port)->data->is_ext)
118
119#define UART_RBR(port) (to_mvuart(port)->data->regs.rbr)
120#define UART_TSH(port) (to_mvuart(port)->data->regs.tsh)
121#define UART_CTRL(port) (to_mvuart(port)->data->regs.ctrl)
122#define UART_INTR(port) (to_mvuart(port)->data->regs.intr)
123
124#define CTRL_TX_RDY_INT(port) (to_mvuart(port)->data->flags.ctrl_tx_rdy_int)
125#define CTRL_RX_RDY_INT(port) (to_mvuart(port)->data->flags.ctrl_rx_rdy_int)
126#define STAT_TX_RDY(port) (to_mvuart(port)->data->flags.stat_tx_rdy)
127#define STAT_RX_RDY(port) (to_mvuart(port)->data->flags.stat_rx_rdy)
128
129static struct uart_port mvebu_uart_ports[MVEBU_NR_UARTS];
130
Wilson Ding30530792016-02-16 19:14:53 +0100131/* Core UART Driver Operations */
132static unsigned int mvebu_uart_tx_empty(struct uart_port *port)
133{
134 unsigned long flags;
135 unsigned int st;
136
137 spin_lock_irqsave(&port->lock, flags);
138 st = readl(port->membase + UART_STAT);
139 spin_unlock_irqrestore(&port->lock, flags);
140
141 return (st & STAT_TX_FIFO_EMP) ? TIOCSER_TEMT : 0;
142}
143
144static unsigned int mvebu_uart_get_mctrl(struct uart_port *port)
145{
146 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
147}
148
149static void mvebu_uart_set_mctrl(struct uart_port *port,
150 unsigned int mctrl)
151{
152/*
153 * Even if we do not support configuring the modem control lines, this
154 * function must be proided to the serial core
155 */
156}
157
158static void mvebu_uart_stop_tx(struct uart_port *port)
159{
Miquel Raynal5218d762017-10-13 11:01:49 +0200160 unsigned int ctl = readl(port->membase + UART_INTR(port));
Wilson Ding30530792016-02-16 19:14:53 +0100161
Miquel Raynal5218d762017-10-13 11:01:49 +0200162 ctl &= ~CTRL_TX_RDY_INT(port);
163 writel(ctl, port->membase + UART_INTR(port));
Wilson Ding30530792016-02-16 19:14:53 +0100164}
165
166static void mvebu_uart_start_tx(struct uart_port *port)
167{
Allen Yan30434b02017-10-13 11:01:53 +0200168 unsigned int ctl;
169 struct circ_buf *xmit = &port->state->xmit;
Wilson Ding30530792016-02-16 19:14:53 +0100170
Allen Yan30434b02017-10-13 11:01:53 +0200171 if (IS_EXTENDED(port) && !uart_circ_empty(xmit)) {
172 writel(xmit->buf[xmit->tail], port->membase + UART_TSH(port));
173 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
174 port->icount.tx++;
175 }
176
177 ctl = readl(port->membase + UART_INTR(port));
Miquel Raynal5218d762017-10-13 11:01:49 +0200178 ctl |= CTRL_TX_RDY_INT(port);
179 writel(ctl, port->membase + UART_INTR(port));
Wilson Ding30530792016-02-16 19:14:53 +0100180}
181
182static void mvebu_uart_stop_rx(struct uart_port *port)
183{
Miquel Raynal5218d762017-10-13 11:01:49 +0200184 unsigned int ctl;
Wilson Ding30530792016-02-16 19:14:53 +0100185
Miquel Raynal5218d762017-10-13 11:01:49 +0200186 ctl = readl(port->membase + UART_CTRL(port));
187 ctl &= ~CTRL_BRK_INT;
188 writel(ctl, port->membase + UART_CTRL(port));
189
190 ctl = readl(port->membase + UART_INTR(port));
191 ctl &= ~CTRL_RX_RDY_INT(port);
192 writel(ctl, port->membase + UART_INTR(port));
Wilson Ding30530792016-02-16 19:14:53 +0100193}
194
195static void mvebu_uart_break_ctl(struct uart_port *port, int brk)
196{
197 unsigned int ctl;
198 unsigned long flags;
199
200 spin_lock_irqsave(&port->lock, flags);
Miquel Raynal5218d762017-10-13 11:01:49 +0200201 ctl = readl(port->membase + UART_CTRL(port));
Wilson Ding30530792016-02-16 19:14:53 +0100202 if (brk == -1)
203 ctl |= CTRL_SND_BRK_SEQ;
204 else
205 ctl &= ~CTRL_SND_BRK_SEQ;
Miquel Raynal5218d762017-10-13 11:01:49 +0200206 writel(ctl, port->membase + UART_CTRL(port));
Wilson Ding30530792016-02-16 19:14:53 +0100207 spin_unlock_irqrestore(&port->lock, flags);
208}
209
210static void mvebu_uart_rx_chars(struct uart_port *port, unsigned int status)
211{
212 struct tty_port *tport = &port->state->port;
213 unsigned char ch = 0;
214 char flag = 0;
215
216 do {
Miquel Raynal5218d762017-10-13 11:01:49 +0200217 if (status & STAT_RX_RDY(port)) {
218 ch = readl(port->membase + UART_RBR(port));
Wilson Ding30530792016-02-16 19:14:53 +0100219 ch &= 0xff;
220 flag = TTY_NORMAL;
221 port->icount.rx++;
222
223 if (status & STAT_PAR_ERR)
224 port->icount.parity++;
225 }
226
227 if (status & STAT_BRK_DET) {
228 port->icount.brk++;
229 status &= ~(STAT_FRM_ERR | STAT_PAR_ERR);
230 if (uart_handle_break(port))
231 goto ignore_char;
232 }
233
234 if (status & STAT_OVR_ERR)
235 port->icount.overrun++;
236
237 if (status & STAT_FRM_ERR)
238 port->icount.frame++;
239
240 if (uart_handle_sysrq_char(port, ch))
241 goto ignore_char;
242
243 if (status & port->ignore_status_mask & STAT_PAR_ERR)
Miquel Raynal5218d762017-10-13 11:01:49 +0200244 status &= ~STAT_RX_RDY(port);
Wilson Ding30530792016-02-16 19:14:53 +0100245
246 status &= port->read_status_mask;
247
248 if (status & STAT_PAR_ERR)
249 flag = TTY_PARITY;
250
251 status &= ~port->ignore_status_mask;
252
Miquel Raynal5218d762017-10-13 11:01:49 +0200253 if (status & STAT_RX_RDY(port))
Wilson Ding30530792016-02-16 19:14:53 +0100254 tty_insert_flip_char(tport, ch, flag);
255
256 if (status & STAT_BRK_DET)
257 tty_insert_flip_char(tport, 0, TTY_BREAK);
258
259 if (status & STAT_FRM_ERR)
260 tty_insert_flip_char(tport, 0, TTY_FRAME);
261
262 if (status & STAT_OVR_ERR)
263 tty_insert_flip_char(tport, 0, TTY_OVERRUN);
264
265ignore_char:
266 status = readl(port->membase + UART_STAT);
Miquel Raynal5218d762017-10-13 11:01:49 +0200267 } while (status & (STAT_RX_RDY(port) | STAT_BRK_DET));
Wilson Ding30530792016-02-16 19:14:53 +0100268
269 tty_flip_buffer_push(tport);
270}
271
272static void mvebu_uart_tx_chars(struct uart_port *port, unsigned int status)
273{
274 struct circ_buf *xmit = &port->state->xmit;
275 unsigned int count;
276 unsigned int st;
277
278 if (port->x_char) {
Miquel Raynal5218d762017-10-13 11:01:49 +0200279 writel(port->x_char, port->membase + UART_TSH(port));
Wilson Ding30530792016-02-16 19:14:53 +0100280 port->icount.tx++;
281 port->x_char = 0;
282 return;
283 }
284
285 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
286 mvebu_uart_stop_tx(port);
287 return;
288 }
289
290 for (count = 0; count < port->fifosize; count++) {
Miquel Raynal5218d762017-10-13 11:01:49 +0200291 writel(xmit->buf[xmit->tail], port->membase + UART_TSH(port));
Wilson Ding30530792016-02-16 19:14:53 +0100292 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
293 port->icount.tx++;
294
295 if (uart_circ_empty(xmit))
296 break;
297
298 st = readl(port->membase + UART_STAT);
299 if (st & STAT_TX_FIFO_FUL)
300 break;
301 }
302
303 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
304 uart_write_wakeup(port);
305
306 if (uart_circ_empty(xmit))
307 mvebu_uart_stop_tx(port);
308}
309
310static irqreturn_t mvebu_uart_isr(int irq, void *dev_id)
311{
312 struct uart_port *port = (struct uart_port *)dev_id;
313 unsigned int st = readl(port->membase + UART_STAT);
314
Miquel Raynal5218d762017-10-13 11:01:49 +0200315 if (st & (STAT_RX_RDY(port) | STAT_OVR_ERR | STAT_FRM_ERR |
316 STAT_BRK_DET))
Wilson Ding30530792016-02-16 19:14:53 +0100317 mvebu_uart_rx_chars(port, st);
318
Miquel Raynal5218d762017-10-13 11:01:49 +0200319 if (st & STAT_TX_RDY(port))
Wilson Ding30530792016-02-16 19:14:53 +0100320 mvebu_uart_tx_chars(port, st);
321
322 return IRQ_HANDLED;
323}
324
325static int mvebu_uart_startup(struct uart_port *port)
326{
Miquel Raynal5218d762017-10-13 11:01:49 +0200327 unsigned int ctl;
Wilson Ding30530792016-02-16 19:14:53 +0100328 int ret;
329
330 writel(CTRL_TXFIFO_RST | CTRL_RXFIFO_RST,
Miquel Raynal5218d762017-10-13 11:01:49 +0200331 port->membase + UART_CTRL(port));
Wilson Ding30530792016-02-16 19:14:53 +0100332 udelay(1);
Allen Yan2ff23c42017-10-13 11:01:52 +0200333
334 /* Clear the error bits of state register before IRQ request */
335 ret = readl(port->membase + UART_STAT);
336 ret |= STAT_BRK_ERR;
337 writel(ret, port->membase + UART_STAT);
338
Miquel Raynal5218d762017-10-13 11:01:49 +0200339 writel(CTRL_BRK_INT, port->membase + UART_CTRL(port));
340
341 ctl = readl(port->membase + UART_INTR(port));
342 ctl |= CTRL_RX_RDY_INT(port);
343 writel(ctl, port->membase + UART_INTR(port));
Wilson Ding30530792016-02-16 19:14:53 +0100344
Yehuda Yitschak02c33332017-10-13 11:01:47 +0200345 ret = request_irq(port->irq, mvebu_uart_isr, port->irqflags,
346 DRIVER_NAME, port);
Wilson Ding30530792016-02-16 19:14:53 +0100347 if (ret) {
348 dev_err(port->dev, "failed to request irq\n");
349 return ret;
350 }
351
352 return 0;
353}
354
355static void mvebu_uart_shutdown(struct uart_port *port)
356{
Miquel Raynal5218d762017-10-13 11:01:49 +0200357 writel(0, port->membase + UART_INTR(port));
Thomas Petazzonic2c16592016-06-16 16:48:52 +0200358
359 free_irq(port->irq, port);
Wilson Ding30530792016-02-16 19:14:53 +0100360}
361
Allen Yan68a0db12017-10-13 11:01:51 +0200362static int mvebu_uart_baud_rate_set(struct uart_port *port, unsigned int baud)
363{
364 struct mvebu_uart *mvuart = to_mvuart(port);
365 unsigned int baud_rate_div;
366 u32 brdv;
367
368 if (IS_ERR(mvuart->clk))
369 return -PTR_ERR(mvuart->clk);
370
371 /*
372 * The UART clock is divided by the value of the divisor to generate
373 * UCLK_OUT clock, which is 16 times faster than the baudrate.
374 * This prescaler can achieve all standard baudrates until 230400.
375 * Higher baudrates could be achieved for the extended UART by using the
376 * programmable oversampling stack (also called fractional divisor).
377 */
378 baud_rate_div = DIV_ROUND_UP(port->uartclk, baud * 16);
379 brdv = readl(port->membase + UART_BRDV);
380 brdv &= ~BRDV_BAUD_MASK;
381 brdv |= baud_rate_div;
382 writel(brdv, port->membase + UART_BRDV);
383
384 return 0;
385}
386
Wilson Ding30530792016-02-16 19:14:53 +0100387static void mvebu_uart_set_termios(struct uart_port *port,
388 struct ktermios *termios,
389 struct ktermios *old)
390{
391 unsigned long flags;
392 unsigned int baud;
393
394 spin_lock_irqsave(&port->lock, flags);
395
Miquel Raynal5218d762017-10-13 11:01:49 +0200396 port->read_status_mask = STAT_RX_RDY(port) | STAT_OVR_ERR |
397 STAT_TX_RDY(port) | STAT_TX_FIFO_FUL;
Wilson Ding30530792016-02-16 19:14:53 +0100398
399 if (termios->c_iflag & INPCK)
400 port->read_status_mask |= STAT_FRM_ERR | STAT_PAR_ERR;
401
402 port->ignore_status_mask = 0;
403 if (termios->c_iflag & IGNPAR)
404 port->ignore_status_mask |=
405 STAT_FRM_ERR | STAT_PAR_ERR | STAT_OVR_ERR;
406
407 if ((termios->c_cflag & CREAD) == 0)
Miquel Raynal5218d762017-10-13 11:01:49 +0200408 port->ignore_status_mask |= STAT_RX_RDY(port) | STAT_BRK_ERR;
Wilson Ding30530792016-02-16 19:14:53 +0100409
Allen Yan68a0db12017-10-13 11:01:51 +0200410 /*
411 * Maximum achievable frequency with simple baudrate divisor is 230400.
412 * Since the error per bit frame would be of more than 15%, achieving
413 * higher frequencies would require to implement the fractional divisor
414 * feature.
415 */
416 baud = uart_get_baud_rate(port, termios, old, 0, 230400);
417 if (mvebu_uart_baud_rate_set(port, baud)) {
418 /* No clock available, baudrate cannot be changed */
419 if (old)
420 baud = uart_get_baud_rate(port, old, NULL, 0, 230400);
421 } else {
422 tty_termios_encode_baud_rate(termios, baud, baud);
423 uart_update_timeout(port, termios->c_cflag, baud);
424 }
Wilson Ding30530792016-02-16 19:14:53 +0100425
Allen Yan68a0db12017-10-13 11:01:51 +0200426 /* Only the following flag changes are supported */
427 if (old) {
428 termios->c_iflag &= INPCK | IGNPAR;
429 termios->c_iflag |= old->c_iflag & ~(INPCK | IGNPAR);
430 termios->c_cflag &= CREAD | CBAUD;
431 termios->c_cflag |= old->c_cflag & ~(CREAD | CBAUD);
432 termios->c_lflag = old->c_lflag;
433 }
Wilson Ding30530792016-02-16 19:14:53 +0100434
435 spin_unlock_irqrestore(&port->lock, flags);
436}
437
438static const char *mvebu_uart_type(struct uart_port *port)
439{
440 return MVEBU_UART_TYPE;
441}
442
443static void mvebu_uart_release_port(struct uart_port *port)
444{
445 /* Nothing to do here */
446}
447
448static int mvebu_uart_request_port(struct uart_port *port)
449{
450 return 0;
451}
452
453#ifdef CONFIG_CONSOLE_POLL
454static int mvebu_uart_get_poll_char(struct uart_port *port)
455{
456 unsigned int st = readl(port->membase + UART_STAT);
457
Miquel Raynal5218d762017-10-13 11:01:49 +0200458 if (!(st & STAT_RX_RDY(port)))
Wilson Ding30530792016-02-16 19:14:53 +0100459 return NO_POLL_CHAR;
460
Miquel Raynal5218d762017-10-13 11:01:49 +0200461 return readl(port->membase + UART_RBR(port));
Wilson Ding30530792016-02-16 19:14:53 +0100462}
463
464static void mvebu_uart_put_poll_char(struct uart_port *port, unsigned char c)
465{
466 unsigned int st;
467
468 for (;;) {
469 st = readl(port->membase + UART_STAT);
470
471 if (!(st & STAT_TX_FIFO_FUL))
472 break;
473
474 udelay(1);
475 }
476
Miquel Raynal5218d762017-10-13 11:01:49 +0200477 writel(c, port->membase + UART_TSH(port));
Wilson Ding30530792016-02-16 19:14:53 +0100478}
479#endif
480
481static const struct uart_ops mvebu_uart_ops = {
482 .tx_empty = mvebu_uart_tx_empty,
483 .set_mctrl = mvebu_uart_set_mctrl,
484 .get_mctrl = mvebu_uart_get_mctrl,
485 .stop_tx = mvebu_uart_stop_tx,
486 .start_tx = mvebu_uart_start_tx,
487 .stop_rx = mvebu_uart_stop_rx,
488 .break_ctl = mvebu_uart_break_ctl,
489 .startup = mvebu_uart_startup,
490 .shutdown = mvebu_uart_shutdown,
491 .set_termios = mvebu_uart_set_termios,
492 .type = mvebu_uart_type,
493 .release_port = mvebu_uart_release_port,
494 .request_port = mvebu_uart_request_port,
495#ifdef CONFIG_CONSOLE_POLL
496 .poll_get_char = mvebu_uart_get_poll_char,
497 .poll_put_char = mvebu_uart_put_poll_char,
498#endif
499};
500
501/* Console Driver Operations */
502
503#ifdef CONFIG_SERIAL_MVEBU_CONSOLE
504/* Early Console */
505static void mvebu_uart_putc(struct uart_port *port, int c)
506{
507 unsigned int st;
508
509 for (;;) {
510 st = readl(port->membase + UART_STAT);
511 if (!(st & STAT_TX_FIFO_FUL))
512 break;
513 }
514
Miquel Raynal5218d762017-10-13 11:01:49 +0200515 /* At early stage, DT is not parsed yet, only use UART0 */
516 writel(c, port->membase + UART_STD_TSH);
Wilson Ding30530792016-02-16 19:14:53 +0100517
518 for (;;) {
519 st = readl(port->membase + UART_STAT);
520 if (st & STAT_TX_FIFO_EMP)
521 break;
522 }
523}
524
525static void mvebu_uart_putc_early_write(struct console *con,
526 const char *s,
527 unsigned n)
528{
529 struct earlycon_device *dev = con->data;
530
531 uart_console_write(&dev->port, s, n, mvebu_uart_putc);
532}
533
534static int __init
535mvebu_uart_early_console_setup(struct earlycon_device *device,
536 const char *opt)
537{
538 if (!device->port.membase)
539 return -ENODEV;
540
541 device->con->write = mvebu_uart_putc_early_write;
542
543 return 0;
544}
545
546EARLYCON_DECLARE(ar3700_uart, mvebu_uart_early_console_setup);
547OF_EARLYCON_DECLARE(ar3700_uart, "marvell,armada-3700-uart",
548 mvebu_uart_early_console_setup);
549
550static void wait_for_xmitr(struct uart_port *port)
551{
552 u32 val;
553
554 readl_poll_timeout_atomic(port->membase + UART_STAT, val,
555 (val & STAT_TX_EMP), 1, 10000);
556}
557
558static void mvebu_uart_console_putchar(struct uart_port *port, int ch)
559{
560 wait_for_xmitr(port);
Miquel Raynal5218d762017-10-13 11:01:49 +0200561 writel(ch, port->membase + UART_TSH(port));
Wilson Ding30530792016-02-16 19:14:53 +0100562}
563
564static void mvebu_uart_console_write(struct console *co, const char *s,
565 unsigned int count)
566{
567 struct uart_port *port = &mvebu_uart_ports[co->index];
568 unsigned long flags;
Miquel Raynal5218d762017-10-13 11:01:49 +0200569 unsigned int ier, intr, ctl;
Wilson Ding30530792016-02-16 19:14:53 +0100570 int locked = 1;
571
572 if (oops_in_progress)
573 locked = spin_trylock_irqsave(&port->lock, flags);
574 else
575 spin_lock_irqsave(&port->lock, flags);
576
Miquel Raynal5218d762017-10-13 11:01:49 +0200577 ier = readl(port->membase + UART_CTRL(port)) & CTRL_BRK_INT;
578 intr = readl(port->membase + UART_INTR(port)) &
579 (CTRL_RX_RDY_INT(port) | CTRL_TX_RDY_INT(port));
580 writel(0, port->membase + UART_CTRL(port));
581 writel(0, port->membase + UART_INTR(port));
Wilson Ding30530792016-02-16 19:14:53 +0100582
583 uart_console_write(port, s, count, mvebu_uart_console_putchar);
584
585 wait_for_xmitr(port);
586
587 if (ier)
Miquel Raynal5218d762017-10-13 11:01:49 +0200588 writel(ier, port->membase + UART_CTRL(port));
589
590 if (intr) {
591 ctl = intr | readl(port->membase + UART_INTR(port));
592 writel(ctl, port->membase + UART_INTR(port));
593 }
Wilson Ding30530792016-02-16 19:14:53 +0100594
595 if (locked)
596 spin_unlock_irqrestore(&port->lock, flags);
597}
598
599static int mvebu_uart_console_setup(struct console *co, char *options)
600{
601 struct uart_port *port;
602 int baud = 9600;
603 int bits = 8;
604 int parity = 'n';
605 int flow = 'n';
606
607 if (co->index < 0 || co->index >= MVEBU_NR_UARTS)
608 return -EINVAL;
609
610 port = &mvebu_uart_ports[co->index];
611
612 if (!port->mapbase || !port->membase) {
613 pr_debug("console on ttyMV%i not present\n", co->index);
614 return -ENODEV;
615 }
616
617 if (options)
618 uart_parse_options(options, &baud, &parity, &bits, &flow);
619
620 return uart_set_options(port, co, baud, parity, bits, flow);
621}
622
623static struct uart_driver mvebu_uart_driver;
624
625static struct console mvebu_uart_console = {
626 .name = "ttyMV",
627 .write = mvebu_uart_console_write,
628 .device = uart_console_device,
629 .setup = mvebu_uart_console_setup,
630 .flags = CON_PRINTBUFFER,
631 .index = -1,
632 .data = &mvebu_uart_driver,
633};
634
635static int __init mvebu_uart_console_init(void)
636{
637 register_console(&mvebu_uart_console);
638 return 0;
639}
640
641console_initcall(mvebu_uart_console_init);
642
643
644#endif /* CONFIG_SERIAL_MVEBU_CONSOLE */
645
646static struct uart_driver mvebu_uart_driver = {
647 .owner = THIS_MODULE,
Yehuda Yitschak02c33332017-10-13 11:01:47 +0200648 .driver_name = DRIVER_NAME,
Wilson Ding30530792016-02-16 19:14:53 +0100649 .dev_name = "ttyMV",
650 .nr = MVEBU_NR_UARTS,
651#ifdef CONFIG_SERIAL_MVEBU_CONSOLE
652 .cons = &mvebu_uart_console,
653#endif
654};
655
Miquel Raynal5218d762017-10-13 11:01:49 +0200656static const struct of_device_id mvebu_uart_of_match[];
657
Allen Yan94228f92017-10-13 11:01:48 +0200658/* Counter to keep track of each UART port id when not using CONFIG_OF */
659static int uart_num_counter;
660
Wilson Ding30530792016-02-16 19:14:53 +0100661static int mvebu_uart_probe(struct platform_device *pdev)
662{
663 struct resource *reg = platform_get_resource(pdev, IORESOURCE_MEM, 0);
664 struct resource *irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
Miquel Raynal5218d762017-10-13 11:01:49 +0200665 const struct of_device_id *match = of_match_device(mvebu_uart_of_match,
666 &pdev->dev);
Wilson Ding30530792016-02-16 19:14:53 +0100667 struct uart_port *port;
Miquel Raynal5218d762017-10-13 11:01:49 +0200668 struct mvebu_uart *mvuart;
Allen Yan94228f92017-10-13 11:01:48 +0200669 int ret, id;
Wilson Ding30530792016-02-16 19:14:53 +0100670
671 if (!reg || !irq) {
672 dev_err(&pdev->dev, "no registers/irq defined\n");
673 return -EINVAL;
674 }
675
Allen Yan94228f92017-10-13 11:01:48 +0200676 /* Assume that all UART ports have a DT alias or none has */
677 id = of_alias_get_id(pdev->dev.of_node, "serial");
678 if (!pdev->dev.of_node || id < 0)
679 pdev->id = uart_num_counter++;
680 else
681 pdev->id = id;
682
683 if (pdev->id >= MVEBU_NR_UARTS) {
684 dev_err(&pdev->dev, "cannot have more than %d UART ports\n",
685 MVEBU_NR_UARTS);
686 return -EINVAL;
687 }
688
689 port = &mvebu_uart_ports[pdev->id];
Wilson Ding30530792016-02-16 19:14:53 +0100690
691 spin_lock_init(&port->lock);
692
693 port->dev = &pdev->dev;
694 port->type = PORT_MVEBU;
695 port->ops = &mvebu_uart_ops;
696 port->regshift = 0;
697
698 port->fifosize = 32;
699 port->iotype = UPIO_MEM32;
700 port->flags = UPF_FIXED_PORT;
Allen Yan94228f92017-10-13 11:01:48 +0200701 port->line = pdev->id;
Wilson Ding30530792016-02-16 19:14:53 +0100702
703 port->irq = irq->start;
704 port->irqflags = 0;
705 port->mapbase = reg->start;
706
707 port->membase = devm_ioremap_resource(&pdev->dev, reg);
708 if (IS_ERR(port->membase))
709 return -PTR_ERR(port->membase);
710
Miquel Raynal5218d762017-10-13 11:01:49 +0200711 mvuart = devm_kzalloc(&pdev->dev, sizeof(struct mvebu_uart),
712 GFP_KERNEL);
713 if (!mvuart)
Wilson Ding30530792016-02-16 19:14:53 +0100714 return -ENOMEM;
715
Allen Yan68a0db12017-10-13 11:01:51 +0200716 /* Get controller data depending on the compatible string */
Miquel Raynal5218d762017-10-13 11:01:49 +0200717 mvuart->data = (struct mvebu_uart_driver_data *)match->data;
718 mvuart->port = port;
Wilson Ding30530792016-02-16 19:14:53 +0100719
Miquel Raynal5218d762017-10-13 11:01:49 +0200720 port->private_data = mvuart;
721 platform_set_drvdata(pdev, mvuart);
Wilson Ding30530792016-02-16 19:14:53 +0100722
Allen Yan68a0db12017-10-13 11:01:51 +0200723 /* Get fixed clock frequency */
724 mvuart->clk = devm_clk_get(&pdev->dev, NULL);
725 if (IS_ERR(mvuart->clk)) {
726 if (PTR_ERR(mvuart->clk) == -EPROBE_DEFER)
727 return PTR_ERR(mvuart->clk);
728
729 if (IS_EXTENDED(port)) {
730 dev_err(&pdev->dev, "unable to get UART clock\n");
731 return PTR_ERR(mvuart->clk);
732 }
733 } else {
734 if (!clk_prepare_enable(mvuart->clk))
735 port->uartclk = clk_get_rate(mvuart->clk);
736 }
737
Allen Yan9c3d3ee2017-10-13 11:01:50 +0200738 /* UART Soft Reset*/
739 writel(CTRL_SOFT_RST, port->membase + UART_CTRL(port));
740 udelay(1);
741 writel(0, port->membase + UART_CTRL(port));
742
Wilson Ding30530792016-02-16 19:14:53 +0100743 ret = uart_add_one_port(&mvebu_uart_driver, port);
744 if (ret)
745 return ret;
746 return 0;
747}
748
Miquel Raynal5218d762017-10-13 11:01:49 +0200749static struct mvebu_uart_driver_data uart_std_driver_data = {
750 .is_ext = false,
751 .regs.rbr = UART_STD_RBR,
752 .regs.tsh = UART_STD_TSH,
753 .regs.ctrl = UART_STD_CTRL1,
754 .regs.intr = UART_STD_CTRL2,
755 .flags.ctrl_tx_rdy_int = CTRL_STD_TX_RDY_INT,
756 .flags.ctrl_rx_rdy_int = CTRL_STD_RX_RDY_INT,
757 .flags.stat_tx_rdy = STAT_STD_TX_RDY,
758 .flags.stat_rx_rdy = STAT_STD_RX_RDY,
759};
760
Wilson Ding30530792016-02-16 19:14:53 +0100761/* Match table for of_platform binding */
762static const struct of_device_id mvebu_uart_of_match[] = {
Miquel Raynal5218d762017-10-13 11:01:49 +0200763 {
764 .compatible = "marvell,armada-3700-uart",
765 .data = (void *)&uart_std_driver_data,
766 },
Wilson Ding30530792016-02-16 19:14:53 +0100767 {}
768};
Wilson Ding30530792016-02-16 19:14:53 +0100769
770static struct platform_driver mvebu_uart_platform_driver = {
771 .probe = mvebu_uart_probe,
Wilson Ding30530792016-02-16 19:14:53 +0100772 .driver = {
Wilson Ding30530792016-02-16 19:14:53 +0100773 .name = "mvebu-uart",
774 .of_match_table = of_match_ptr(mvebu_uart_of_match),
Paul Gortmaker89ebc272016-03-13 19:48:52 -0400775 .suppress_bind_attrs = true,
Wilson Ding30530792016-02-16 19:14:53 +0100776 },
777};
778
779static int __init mvebu_uart_init(void)
780{
781 int ret;
782
783 ret = uart_register_driver(&mvebu_uart_driver);
784 if (ret)
785 return ret;
786
787 ret = platform_driver_register(&mvebu_uart_platform_driver);
788 if (ret)
789 uart_unregister_driver(&mvebu_uart_driver);
790
791 return ret;
792}
Wilson Ding30530792016-02-16 19:14:53 +0100793arch_initcall(mvebu_uart_init);