serial: mvebu-uart: drop incorrect memset
[linux-2.6-microblaze.git] / drivers / tty / serial / mvebu-uart.c
1 /*
2 * ***************************************************************************
3 * Marvell Armada-3700 Serial Driver
4 * Author: Wilson Ding <dingwei@marvell.com>
5 * 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>
28 #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 */
41 #define UART_STD_RBR            0x00
42 #define UART_EXT_RBR            0x18
43
44 #define UART_STD_TSH            0x04
45 #define UART_EXT_TSH            0x1C
46
47 #define UART_STD_CTRL1          0x08
48 #define UART_EXT_CTRL1          0x04
49 #define  CTRL_SOFT_RST          BIT(31)
50 #define  CTRL_TXFIFO_RST        BIT(15)
51 #define  CTRL_RXFIFO_RST        BIT(14)
52 #define  CTRL_SND_BRK_SEQ       BIT(11)
53 #define  CTRL_BRK_DET_INT       BIT(3)
54 #define  CTRL_FRM_ERR_INT       BIT(2)
55 #define  CTRL_PAR_ERR_INT       BIT(1)
56 #define  CTRL_OVR_ERR_INT       BIT(0)
57 #define  CTRL_BRK_INT           (CTRL_BRK_DET_INT | CTRL_FRM_ERR_INT | \
58                                 CTRL_PAR_ERR_INT | CTRL_OVR_ERR_INT)
59
60 #define UART_STD_CTRL2          UART_STD_CTRL1
61 #define UART_EXT_CTRL2          0x20
62 #define  CTRL_STD_TX_RDY_INT    BIT(5)
63 #define  CTRL_EXT_TX_RDY_INT    BIT(6)
64 #define  CTRL_STD_RX_RDY_INT    BIT(4)
65 #define  CTRL_EXT_RX_RDY_INT    BIT(5)
66
67 #define UART_STAT               0x0C
68 #define  STAT_TX_FIFO_EMP       BIT(13)
69 #define  STAT_TX_FIFO_FUL       BIT(11)
70 #define  STAT_TX_EMP            BIT(6)
71 #define  STAT_STD_TX_RDY        BIT(5)
72 #define  STAT_EXT_TX_RDY        BIT(15)
73 #define  STAT_STD_RX_RDY        BIT(4)
74 #define  STAT_EXT_RX_RDY        BIT(14)
75 #define  STAT_BRK_DET           BIT(3)
76 #define  STAT_FRM_ERR           BIT(2)
77 #define  STAT_PAR_ERR           BIT(1)
78 #define  STAT_OVR_ERR           BIT(0)
79 #define  STAT_BRK_ERR           (STAT_BRK_DET | STAT_FRM_ERR | STAT_FRM_ERR\
80                                  | STAT_PAR_ERR | STAT_OVR_ERR)
81
82 #define UART_BRDV               0x10
83 #define  BRDV_BAUD_MASK         0x3FF
84
85 #define MVEBU_NR_UARTS          2
86
87 #define MVEBU_UART_TYPE         "mvebu-uart"
88 #define DRIVER_NAME             "mvebu_serial"
89
90 enum {
91         /* Either there is only one summed IRQ... */
92         UART_IRQ_SUM = 0,
93         /* ...or there are two separate IRQ for RX and TX */
94         UART_RX_IRQ = 0,
95         UART_TX_IRQ,
96         UART_IRQ_COUNT
97 };
98
99 /* Diverging register offsets */
100 struct uart_regs_layout {
101         unsigned int rbr;
102         unsigned int tsh;
103         unsigned int ctrl;
104         unsigned int intr;
105 };
106
107 /* Diverging flags */
108 struct uart_flags {
109         unsigned int ctrl_tx_rdy_int;
110         unsigned int ctrl_rx_rdy_int;
111         unsigned int stat_tx_rdy;
112         unsigned int stat_rx_rdy;
113 };
114
115 /* Driver data, a structure for each UART port */
116 struct mvebu_uart_driver_data {
117         bool is_ext;
118         struct uart_regs_layout regs;
119         struct uart_flags flags;
120 };
121
122 /* MVEBU UART driver structure */
123 struct mvebu_uart {
124         struct uart_port *port;
125         struct clk *clk;
126         int irq[UART_IRQ_COUNT];
127         unsigned char __iomem *nb;
128         struct mvebu_uart_driver_data *data;
129 };
130
131 static struct mvebu_uart *to_mvuart(struct uart_port *port)
132 {
133         return (struct mvebu_uart *)port->private_data;
134 }
135
136 #define IS_EXTENDED(port) (to_mvuart(port)->data->is_ext)
137
138 #define UART_RBR(port) (to_mvuart(port)->data->regs.rbr)
139 #define UART_TSH(port) (to_mvuart(port)->data->regs.tsh)
140 #define UART_CTRL(port) (to_mvuart(port)->data->regs.ctrl)
141 #define UART_INTR(port) (to_mvuart(port)->data->regs.intr)
142
143 #define CTRL_TX_RDY_INT(port) (to_mvuart(port)->data->flags.ctrl_tx_rdy_int)
144 #define CTRL_RX_RDY_INT(port) (to_mvuart(port)->data->flags.ctrl_rx_rdy_int)
145 #define STAT_TX_RDY(port) (to_mvuart(port)->data->flags.stat_tx_rdy)
146 #define STAT_RX_RDY(port) (to_mvuart(port)->data->flags.stat_rx_rdy)
147
148 static struct uart_port mvebu_uart_ports[MVEBU_NR_UARTS];
149
150 /* Core UART Driver Operations */
151 static unsigned int mvebu_uart_tx_empty(struct uart_port *port)
152 {
153         unsigned long flags;
154         unsigned int st;
155
156         spin_lock_irqsave(&port->lock, flags);
157         st = readl(port->membase + UART_STAT);
158         spin_unlock_irqrestore(&port->lock, flags);
159
160         return (st & STAT_TX_FIFO_EMP) ? TIOCSER_TEMT : 0;
161 }
162
163 static unsigned int mvebu_uart_get_mctrl(struct uart_port *port)
164 {
165         return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
166 }
167
168 static void mvebu_uart_set_mctrl(struct uart_port *port,
169                                  unsigned int mctrl)
170 {
171 /*
172  * Even if we do not support configuring the modem control lines, this
173  * function must be proided to the serial core
174  */
175 }
176
177 static void mvebu_uart_stop_tx(struct uart_port *port)
178 {
179         unsigned int ctl = readl(port->membase + UART_INTR(port));
180
181         ctl &= ~CTRL_TX_RDY_INT(port);
182         writel(ctl, port->membase + UART_INTR(port));
183 }
184
185 static void mvebu_uart_start_tx(struct uart_port *port)
186 {
187         unsigned int ctl;
188         struct circ_buf *xmit = &port->state->xmit;
189
190         if (IS_EXTENDED(port) && !uart_circ_empty(xmit)) {
191                 writel(xmit->buf[xmit->tail], port->membase + UART_TSH(port));
192                 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
193                 port->icount.tx++;
194         }
195
196         ctl = readl(port->membase + UART_INTR(port));
197         ctl |= CTRL_TX_RDY_INT(port);
198         writel(ctl, port->membase + UART_INTR(port));
199 }
200
201 static void mvebu_uart_stop_rx(struct uart_port *port)
202 {
203         unsigned int ctl;
204
205         ctl = readl(port->membase + UART_CTRL(port));
206         ctl &= ~CTRL_BRK_INT;
207         writel(ctl, port->membase + UART_CTRL(port));
208
209         ctl = readl(port->membase + UART_INTR(port));
210         ctl &= ~CTRL_RX_RDY_INT(port);
211         writel(ctl, port->membase + UART_INTR(port));
212 }
213
214 static void mvebu_uart_break_ctl(struct uart_port *port, int brk)
215 {
216         unsigned int ctl;
217         unsigned long flags;
218
219         spin_lock_irqsave(&port->lock, flags);
220         ctl = readl(port->membase + UART_CTRL(port));
221         if (brk == -1)
222                 ctl |= CTRL_SND_BRK_SEQ;
223         else
224                 ctl &= ~CTRL_SND_BRK_SEQ;
225         writel(ctl, port->membase + UART_CTRL(port));
226         spin_unlock_irqrestore(&port->lock, flags);
227 }
228
229 static void mvebu_uart_rx_chars(struct uart_port *port, unsigned int status)
230 {
231         struct tty_port *tport = &port->state->port;
232         unsigned char ch = 0;
233         char flag = 0;
234
235         do {
236                 if (status & STAT_RX_RDY(port)) {
237                         ch = readl(port->membase + UART_RBR(port));
238                         ch &= 0xff;
239                         flag = TTY_NORMAL;
240                         port->icount.rx++;
241
242                         if (status & STAT_PAR_ERR)
243                                 port->icount.parity++;
244                 }
245
246                 if (status & STAT_BRK_DET) {
247                         port->icount.brk++;
248                         status &= ~(STAT_FRM_ERR | STAT_PAR_ERR);
249                         if (uart_handle_break(port))
250                                 goto ignore_char;
251                 }
252
253                 if (status & STAT_OVR_ERR)
254                         port->icount.overrun++;
255
256                 if (status & STAT_FRM_ERR)
257                         port->icount.frame++;
258
259                 if (uart_handle_sysrq_char(port, ch))
260                         goto ignore_char;
261
262                 if (status & port->ignore_status_mask & STAT_PAR_ERR)
263                         status &= ~STAT_RX_RDY(port);
264
265                 status &= port->read_status_mask;
266
267                 if (status & STAT_PAR_ERR)
268                         flag = TTY_PARITY;
269
270                 status &= ~port->ignore_status_mask;
271
272                 if (status & STAT_RX_RDY(port))
273                         tty_insert_flip_char(tport, ch, flag);
274
275                 if (status & STAT_BRK_DET)
276                         tty_insert_flip_char(tport, 0, TTY_BREAK);
277
278                 if (status & STAT_FRM_ERR)
279                         tty_insert_flip_char(tport, 0, TTY_FRAME);
280
281                 if (status & STAT_OVR_ERR)
282                         tty_insert_flip_char(tport, 0, TTY_OVERRUN);
283
284 ignore_char:
285                 status = readl(port->membase + UART_STAT);
286         } while (status & (STAT_RX_RDY(port) | STAT_BRK_DET));
287
288         tty_flip_buffer_push(tport);
289 }
290
291 static void mvebu_uart_tx_chars(struct uart_port *port, unsigned int status)
292 {
293         struct circ_buf *xmit = &port->state->xmit;
294         unsigned int count;
295         unsigned int st;
296
297         if (port->x_char) {
298                 writel(port->x_char, port->membase + UART_TSH(port));
299                 port->icount.tx++;
300                 port->x_char = 0;
301                 return;
302         }
303
304         if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
305                 mvebu_uart_stop_tx(port);
306                 return;
307         }
308
309         for (count = 0; count < port->fifosize; count++) {
310                 writel(xmit->buf[xmit->tail], port->membase + UART_TSH(port));
311                 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
312                 port->icount.tx++;
313
314                 if (uart_circ_empty(xmit))
315                         break;
316
317                 st = readl(port->membase + UART_STAT);
318                 if (st & STAT_TX_FIFO_FUL)
319                         break;
320         }
321
322         if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
323                 uart_write_wakeup(port);
324
325         if (uart_circ_empty(xmit))
326                 mvebu_uart_stop_tx(port);
327 }
328
329 static irqreturn_t mvebu_uart_isr(int irq, void *dev_id)
330 {
331         struct uart_port *port = (struct uart_port *)dev_id;
332         unsigned int st = readl(port->membase + UART_STAT);
333
334         if (st & (STAT_RX_RDY(port) | STAT_OVR_ERR | STAT_FRM_ERR |
335                   STAT_BRK_DET))
336                 mvebu_uart_rx_chars(port, st);
337
338         if (st & STAT_TX_RDY(port))
339                 mvebu_uart_tx_chars(port, st);
340
341         return IRQ_HANDLED;
342 }
343
344 static irqreturn_t mvebu_uart_rx_isr(int irq, void *dev_id)
345 {
346         struct uart_port *port = (struct uart_port *)dev_id;
347         unsigned int st = readl(port->membase + UART_STAT);
348
349         if (st & (STAT_RX_RDY(port) | STAT_OVR_ERR | STAT_FRM_ERR |
350                         STAT_BRK_DET))
351                 mvebu_uart_rx_chars(port, st);
352
353         return IRQ_HANDLED;
354 }
355
356 static irqreturn_t mvebu_uart_tx_isr(int irq, void *dev_id)
357 {
358         struct uart_port *port = (struct uart_port *)dev_id;
359         unsigned int st = readl(port->membase + UART_STAT);
360
361         if (st & STAT_TX_RDY(port))
362                 mvebu_uart_tx_chars(port, st);
363
364         return IRQ_HANDLED;
365 }
366
367 static int mvebu_uart_startup(struct uart_port *port)
368 {
369         struct mvebu_uart *mvuart = to_mvuart(port);
370         unsigned int ctl;
371         int ret;
372
373         writel(CTRL_TXFIFO_RST | CTRL_RXFIFO_RST,
374                port->membase + UART_CTRL(port));
375         udelay(1);
376
377         /* Clear the error bits of state register before IRQ request */
378         ret = readl(port->membase + UART_STAT);
379         ret |= STAT_BRK_ERR;
380         writel(ret, port->membase + UART_STAT);
381
382         writel(CTRL_BRK_INT, port->membase + UART_CTRL(port));
383
384         ctl = readl(port->membase + UART_INTR(port));
385         ctl |= CTRL_RX_RDY_INT(port);
386         writel(ctl, port->membase + UART_INTR(port));
387
388         if (!mvuart->irq[UART_TX_IRQ]) {
389                 /* Old bindings with just one interrupt (UART0 only) */
390                 ret = devm_request_irq(port->dev, mvuart->irq[UART_IRQ_SUM],
391                                        mvebu_uart_isr, port->irqflags,
392                                        dev_name(port->dev), port);
393                 if (ret) {
394                         dev_err(port->dev, "unable to request IRQ %d\n",
395                                 mvuart->irq[UART_IRQ_SUM]);
396                         return ret;
397                 }
398         } else {
399                 /* New bindings with an IRQ for RX and TX (both UART) */
400                 ret = devm_request_irq(port->dev, mvuart->irq[UART_RX_IRQ],
401                                        mvebu_uart_rx_isr, port->irqflags,
402                                        dev_name(port->dev), port);
403                 if (ret) {
404                         dev_err(port->dev, "unable to request IRQ %d\n",
405                                 mvuart->irq[UART_RX_IRQ]);
406                         return ret;
407                 }
408
409                 ret = devm_request_irq(port->dev, mvuart->irq[UART_TX_IRQ],
410                                        mvebu_uart_tx_isr, port->irqflags,
411                                        dev_name(port->dev),
412                                        port);
413                 if (ret) {
414                         dev_err(port->dev, "unable to request IRQ %d\n",
415                                 mvuart->irq[UART_TX_IRQ]);
416                         devm_free_irq(port->dev, mvuart->irq[UART_RX_IRQ],
417                                       port);
418                         return ret;
419                 }
420         }
421
422         return 0;
423 }
424
425 static void mvebu_uart_shutdown(struct uart_port *port)
426 {
427         struct mvebu_uart *mvuart = to_mvuart(port);
428
429         writel(0, port->membase + UART_INTR(port));
430
431         if (!mvuart->irq[UART_TX_IRQ]) {
432                 devm_free_irq(port->dev, mvuart->irq[UART_IRQ_SUM], port);
433         } else {
434                 devm_free_irq(port->dev, mvuart->irq[UART_RX_IRQ], port);
435                 devm_free_irq(port->dev, mvuart->irq[UART_TX_IRQ], port);
436         }
437 }
438
439 static int mvebu_uart_baud_rate_set(struct uart_port *port, unsigned int baud)
440 {
441         struct mvebu_uart *mvuart = to_mvuart(port);
442         unsigned int baud_rate_div;
443         u32 brdv;
444
445         if (IS_ERR(mvuart->clk))
446                 return -PTR_ERR(mvuart->clk);
447
448         /*
449          * The UART clock is divided by the value of the divisor to generate
450          * UCLK_OUT clock, which is 16 times faster than the baudrate.
451          * This prescaler can achieve all standard baudrates until 230400.
452          * Higher baudrates could be achieved for the extended UART by using the
453          * programmable oversampling stack (also called fractional divisor).
454          */
455         baud_rate_div = DIV_ROUND_UP(port->uartclk, baud * 16);
456         brdv = readl(port->membase + UART_BRDV);
457         brdv &= ~BRDV_BAUD_MASK;
458         brdv |= baud_rate_div;
459         writel(brdv, port->membase + UART_BRDV);
460
461         return 0;
462 }
463
464 static void mvebu_uart_set_termios(struct uart_port *port,
465                                    struct ktermios *termios,
466                                    struct ktermios *old)
467 {
468         unsigned long flags;
469         unsigned int baud;
470
471         spin_lock_irqsave(&port->lock, flags);
472
473         port->read_status_mask = STAT_RX_RDY(port) | STAT_OVR_ERR |
474                 STAT_TX_RDY(port) | STAT_TX_FIFO_FUL;
475
476         if (termios->c_iflag & INPCK)
477                 port->read_status_mask |= STAT_FRM_ERR | STAT_PAR_ERR;
478
479         port->ignore_status_mask = 0;
480         if (termios->c_iflag & IGNPAR)
481                 port->ignore_status_mask |=
482                         STAT_FRM_ERR | STAT_PAR_ERR | STAT_OVR_ERR;
483
484         if ((termios->c_cflag & CREAD) == 0)
485                 port->ignore_status_mask |= STAT_RX_RDY(port) | STAT_BRK_ERR;
486
487         /*
488          * Maximum achievable frequency with simple baudrate divisor is 230400.
489          * Since the error per bit frame would be of more than 15%, achieving
490          * higher frequencies would require to implement the fractional divisor
491          * feature.
492          */
493         baud = uart_get_baud_rate(port, termios, old, 0, 230400);
494         if (mvebu_uart_baud_rate_set(port, baud)) {
495                 /* No clock available, baudrate cannot be changed */
496                 if (old)
497                         baud = uart_get_baud_rate(port, old, NULL, 0, 230400);
498         } else {
499                 tty_termios_encode_baud_rate(termios, baud, baud);
500                 uart_update_timeout(port, termios->c_cflag, baud);
501         }
502
503         /* Only the following flag changes are supported */
504         if (old) {
505                 termios->c_iflag &= INPCK | IGNPAR;
506                 termios->c_iflag |= old->c_iflag & ~(INPCK | IGNPAR);
507                 termios->c_cflag &= CREAD | CBAUD;
508                 termios->c_cflag |= old->c_cflag & ~(CREAD | CBAUD);
509                 termios->c_lflag = old->c_lflag;
510         }
511
512         spin_unlock_irqrestore(&port->lock, flags);
513 }
514
515 static const char *mvebu_uart_type(struct uart_port *port)
516 {
517         return MVEBU_UART_TYPE;
518 }
519
520 static void mvebu_uart_release_port(struct uart_port *port)
521 {
522         /* Nothing to do here */
523 }
524
525 static int mvebu_uart_request_port(struct uart_port *port)
526 {
527         return 0;
528 }
529
530 #ifdef CONFIG_CONSOLE_POLL
531 static int mvebu_uart_get_poll_char(struct uart_port *port)
532 {
533         unsigned int st = readl(port->membase + UART_STAT);
534
535         if (!(st & STAT_RX_RDY(port)))
536                 return NO_POLL_CHAR;
537
538         return readl(port->membase + UART_RBR(port));
539 }
540
541 static void mvebu_uart_put_poll_char(struct uart_port *port, unsigned char c)
542 {
543         unsigned int st;
544
545         for (;;) {
546                 st = readl(port->membase + UART_STAT);
547
548                 if (!(st & STAT_TX_FIFO_FUL))
549                         break;
550
551                 udelay(1);
552         }
553
554         writel(c, port->membase + UART_TSH(port));
555 }
556 #endif
557
558 static const struct uart_ops mvebu_uart_ops = {
559         .tx_empty       = mvebu_uart_tx_empty,
560         .set_mctrl      = mvebu_uart_set_mctrl,
561         .get_mctrl      = mvebu_uart_get_mctrl,
562         .stop_tx        = mvebu_uart_stop_tx,
563         .start_tx       = mvebu_uart_start_tx,
564         .stop_rx        = mvebu_uart_stop_rx,
565         .break_ctl      = mvebu_uart_break_ctl,
566         .startup        = mvebu_uart_startup,
567         .shutdown       = mvebu_uart_shutdown,
568         .set_termios    = mvebu_uart_set_termios,
569         .type           = mvebu_uart_type,
570         .release_port   = mvebu_uart_release_port,
571         .request_port   = mvebu_uart_request_port,
572 #ifdef CONFIG_CONSOLE_POLL
573         .poll_get_char  = mvebu_uart_get_poll_char,
574         .poll_put_char  = mvebu_uart_put_poll_char,
575 #endif
576 };
577
578 /* Console Driver Operations  */
579
580 #ifdef CONFIG_SERIAL_MVEBU_CONSOLE
581 /* Early Console */
582 static void mvebu_uart_putc(struct uart_port *port, int c)
583 {
584         unsigned int st;
585
586         for (;;) {
587                 st = readl(port->membase + UART_STAT);
588                 if (!(st & STAT_TX_FIFO_FUL))
589                         break;
590         }
591
592         /* At early stage, DT is not parsed yet, only use UART0 */
593         writel(c, port->membase + UART_STD_TSH);
594
595         for (;;) {
596                 st = readl(port->membase + UART_STAT);
597                 if (st & STAT_TX_FIFO_EMP)
598                         break;
599         }
600 }
601
602 static void mvebu_uart_putc_early_write(struct console *con,
603                                         const char *s,
604                                         unsigned n)
605 {
606         struct earlycon_device *dev = con->data;
607
608         uart_console_write(&dev->port, s, n, mvebu_uart_putc);
609 }
610
611 static int __init
612 mvebu_uart_early_console_setup(struct earlycon_device *device,
613                                const char *opt)
614 {
615         if (!device->port.membase)
616                 return -ENODEV;
617
618         device->con->write = mvebu_uart_putc_early_write;
619
620         return 0;
621 }
622
623 EARLYCON_DECLARE(ar3700_uart, mvebu_uart_early_console_setup);
624 OF_EARLYCON_DECLARE(ar3700_uart, "marvell,armada-3700-uart",
625                     mvebu_uart_early_console_setup);
626
627 static void wait_for_xmitr(struct uart_port *port)
628 {
629         u32 val;
630
631         readl_poll_timeout_atomic(port->membase + UART_STAT, val,
632                                   (val & STAT_TX_EMP), 1, 10000);
633 }
634
635 static void mvebu_uart_console_putchar(struct uart_port *port, int ch)
636 {
637         wait_for_xmitr(port);
638         writel(ch, port->membase + UART_TSH(port));
639 }
640
641 static void mvebu_uart_console_write(struct console *co, const char *s,
642                                      unsigned int count)
643 {
644         struct uart_port *port = &mvebu_uart_ports[co->index];
645         unsigned long flags;
646         unsigned int ier, intr, ctl;
647         int locked = 1;
648
649         if (oops_in_progress)
650                 locked = spin_trylock_irqsave(&port->lock, flags);
651         else
652                 spin_lock_irqsave(&port->lock, flags);
653
654         ier = readl(port->membase + UART_CTRL(port)) & CTRL_BRK_INT;
655         intr = readl(port->membase + UART_INTR(port)) &
656                 (CTRL_RX_RDY_INT(port) | CTRL_TX_RDY_INT(port));
657         writel(0, port->membase + UART_CTRL(port));
658         writel(0, port->membase + UART_INTR(port));
659
660         uart_console_write(port, s, count, mvebu_uart_console_putchar);
661
662         wait_for_xmitr(port);
663
664         if (ier)
665                 writel(ier, port->membase + UART_CTRL(port));
666
667         if (intr) {
668                 ctl = intr | readl(port->membase + UART_INTR(port));
669                 writel(ctl, port->membase + UART_INTR(port));
670         }
671
672         if (locked)
673                 spin_unlock_irqrestore(&port->lock, flags);
674 }
675
676 static int mvebu_uart_console_setup(struct console *co, char *options)
677 {
678         struct uart_port *port;
679         int baud = 9600;
680         int bits = 8;
681         int parity = 'n';
682         int flow = 'n';
683
684         if (co->index < 0 || co->index >= MVEBU_NR_UARTS)
685                 return -EINVAL;
686
687         port = &mvebu_uart_ports[co->index];
688
689         if (!port->mapbase || !port->membase) {
690                 pr_debug("console on ttyMV%i not present\n", co->index);
691                 return -ENODEV;
692         }
693
694         if (options)
695                 uart_parse_options(options, &baud, &parity, &bits, &flow);
696
697         return uart_set_options(port, co, baud, parity, bits, flow);
698 }
699
700 static struct uart_driver mvebu_uart_driver;
701
702 static struct console mvebu_uart_console = {
703         .name   = "ttyMV",
704         .write  = mvebu_uart_console_write,
705         .device = uart_console_device,
706         .setup  = mvebu_uart_console_setup,
707         .flags  = CON_PRINTBUFFER,
708         .index  = -1,
709         .data   = &mvebu_uart_driver,
710 };
711
712 static int __init mvebu_uart_console_init(void)
713 {
714         register_console(&mvebu_uart_console);
715         return 0;
716 }
717
718 console_initcall(mvebu_uart_console_init);
719
720
721 #endif /* CONFIG_SERIAL_MVEBU_CONSOLE */
722
723 static struct uart_driver mvebu_uart_driver = {
724         .owner                  = THIS_MODULE,
725         .driver_name            = DRIVER_NAME,
726         .dev_name               = "ttyMV",
727         .nr                     = MVEBU_NR_UARTS,
728 #ifdef CONFIG_SERIAL_MVEBU_CONSOLE
729         .cons                   = &mvebu_uart_console,
730 #endif
731 };
732
733 static const struct of_device_id mvebu_uart_of_match[];
734
735 /* Counter to keep track of each UART port id when not using CONFIG_OF */
736 static int uart_num_counter;
737
738 static int mvebu_uart_probe(struct platform_device *pdev)
739 {
740         struct resource *reg = platform_get_resource(pdev, IORESOURCE_MEM, 0);
741         const struct of_device_id *match = of_match_device(mvebu_uart_of_match,
742                                                            &pdev->dev);
743         struct uart_port *port;
744         struct mvebu_uart *mvuart;
745         int ret, id, irq;
746
747         if (!reg) {
748                 dev_err(&pdev->dev, "no registers defined\n");
749                 return -EINVAL;
750         }
751
752         /* Assume that all UART ports have a DT alias or none has */
753         id = of_alias_get_id(pdev->dev.of_node, "serial");
754         if (!pdev->dev.of_node || id < 0)
755                 pdev->id = uart_num_counter++;
756         else
757                 pdev->id = id;
758
759         if (pdev->id >= MVEBU_NR_UARTS) {
760                 dev_err(&pdev->dev, "cannot have more than %d UART ports\n",
761                         MVEBU_NR_UARTS);
762                 return -EINVAL;
763         }
764
765         port = &mvebu_uart_ports[pdev->id];
766
767         spin_lock_init(&port->lock);
768
769         port->dev        = &pdev->dev;
770         port->type       = PORT_MVEBU;
771         port->ops        = &mvebu_uart_ops;
772         port->regshift   = 0;
773
774         port->fifosize   = 32;
775         port->iotype     = UPIO_MEM32;
776         port->flags      = UPF_FIXED_PORT;
777         port->line       = pdev->id;
778
779         /*
780          * IRQ number is not stored in this structure because we may have two of
781          * them per port (RX and TX). Instead, use the driver UART structure
782          * array so called ->irq[].
783          */
784         port->irq        = 0;
785         port->irqflags   = 0;
786         port->mapbase    = reg->start;
787
788         port->membase = devm_ioremap_resource(&pdev->dev, reg);
789         if (IS_ERR(port->membase))
790                 return -PTR_ERR(port->membase);
791
792         mvuart = devm_kzalloc(&pdev->dev, sizeof(struct mvebu_uart),
793                               GFP_KERNEL);
794         if (!mvuart)
795                 return -ENOMEM;
796
797         /* Get controller data depending on the compatible string */
798         mvuart->data = (struct mvebu_uart_driver_data *)match->data;
799         mvuart->port = port;
800
801         port->private_data = mvuart;
802         platform_set_drvdata(pdev, mvuart);
803
804         /* Get fixed clock frequency */
805         mvuart->clk = devm_clk_get(&pdev->dev, NULL);
806         if (IS_ERR(mvuart->clk)) {
807                 if (PTR_ERR(mvuart->clk) == -EPROBE_DEFER)
808                         return PTR_ERR(mvuart->clk);
809
810                 if (IS_EXTENDED(port)) {
811                         dev_err(&pdev->dev, "unable to get UART clock\n");
812                         return PTR_ERR(mvuart->clk);
813                 }
814         } else {
815                 if (!clk_prepare_enable(mvuart->clk))
816                         port->uartclk = clk_get_rate(mvuart->clk);
817         }
818
819         /* Manage interrupts */
820         if (platform_irq_count(pdev) == 1) {
821                 /* Old bindings: no name on the single unamed UART0 IRQ */
822                 irq = platform_get_irq(pdev, 0);
823                 if (irq < 0) {
824                         dev_err(&pdev->dev, "unable to get UART IRQ\n");
825                         return irq;
826                 }
827
828                 mvuart->irq[UART_IRQ_SUM] = irq;
829         } else {
830                 /*
831                  * New bindings: named interrupts (RX, TX) for both UARTS,
832                  * only make use of uart-rx and uart-tx interrupts, do not use
833                  * uart-sum of UART0 port.
834                  */
835                 irq = platform_get_irq_byname(pdev, "uart-rx");
836                 if (irq < 0) {
837                         dev_err(&pdev->dev, "unable to get 'uart-rx' IRQ\n");
838                         return irq;
839                 }
840
841                 mvuart->irq[UART_RX_IRQ] = irq;
842
843                 irq = platform_get_irq_byname(pdev, "uart-tx");
844                 if (irq < 0) {
845                         dev_err(&pdev->dev, "unable to get 'uart-tx' IRQ\n");
846                         return irq;
847                 }
848
849                 mvuart->irq[UART_TX_IRQ] = irq;
850         }
851
852         /* UART Soft Reset*/
853         writel(CTRL_SOFT_RST, port->membase + UART_CTRL(port));
854         udelay(1);
855         writel(0, port->membase + UART_CTRL(port));
856
857         ret = uart_add_one_port(&mvebu_uart_driver, port);
858         if (ret)
859                 return ret;
860         return 0;
861 }
862
863 static struct mvebu_uart_driver_data uart_std_driver_data = {
864         .is_ext = false,
865         .regs.rbr = UART_STD_RBR,
866         .regs.tsh = UART_STD_TSH,
867         .regs.ctrl = UART_STD_CTRL1,
868         .regs.intr = UART_STD_CTRL2,
869         .flags.ctrl_tx_rdy_int = CTRL_STD_TX_RDY_INT,
870         .flags.ctrl_rx_rdy_int = CTRL_STD_RX_RDY_INT,
871         .flags.stat_tx_rdy = STAT_STD_TX_RDY,
872         .flags.stat_rx_rdy = STAT_STD_RX_RDY,
873 };
874
875 static struct mvebu_uart_driver_data uart_ext_driver_data = {
876         .is_ext = true,
877         .regs.rbr = UART_EXT_RBR,
878         .regs.tsh = UART_EXT_TSH,
879         .regs.ctrl = UART_EXT_CTRL1,
880         .regs.intr = UART_EXT_CTRL2,
881         .flags.ctrl_tx_rdy_int = CTRL_EXT_TX_RDY_INT,
882         .flags.ctrl_rx_rdy_int = CTRL_EXT_RX_RDY_INT,
883         .flags.stat_tx_rdy = STAT_EXT_TX_RDY,
884         .flags.stat_rx_rdy = STAT_EXT_RX_RDY,
885 };
886
887 /* Match table for of_platform binding */
888 static const struct of_device_id mvebu_uart_of_match[] = {
889         {
890                 .compatible = "marvell,armada-3700-uart",
891                 .data = (void *)&uart_std_driver_data,
892         },
893         {
894                 .compatible = "marvell,armada-3700-uart-ext",
895                 .data = (void *)&uart_ext_driver_data,
896         },
897         {}
898 };
899
900 static struct platform_driver mvebu_uart_platform_driver = {
901         .probe  = mvebu_uart_probe,
902         .driver = {
903                 .name  = "mvebu-uart",
904                 .of_match_table = of_match_ptr(mvebu_uart_of_match),
905                 .suppress_bind_attrs = true,
906         },
907 };
908
909 static int __init mvebu_uart_init(void)
910 {
911         int ret;
912
913         ret = uart_register_driver(&mvebu_uart_driver);
914         if (ret)
915                 return ret;
916
917         ret = platform_driver_register(&mvebu_uart_platform_driver);
918         if (ret)
919                 uart_unregister_driver(&mvebu_uart_driver);
920
921         return ret;
922 }
923 arch_initcall(mvebu_uart_init);