blob: 7e0a3e9fee159112f5985215abbd7c0a544a2d83 [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 */
41#define UART_RBR 0x00
42#define RBR_BRK_DET BIT(15)
43#define RBR_FRM_ERR_DET BIT(14)
44#define RBR_PAR_ERR_DET BIT(13)
45#define RBR_OVR_ERR_DET BIT(12)
46
47#define UART_TSH 0x04
48
49#define UART_CTRL 0x08
50#define CTRL_SOFT_RST BIT(31)
51#define CTRL_TXFIFO_RST BIT(15)
52#define CTRL_RXFIFO_RST BIT(14)
53#define CTRL_ST_MIRR_EN BIT(13)
54#define CTRL_LPBK_EN BIT(12)
55#define CTRL_SND_BRK_SEQ BIT(11)
56#define CTRL_PAR_EN BIT(10)
57#define CTRL_TWO_STOP BIT(9)
58#define CTRL_TX_HFL_INT BIT(8)
59#define CTRL_RX_HFL_INT BIT(7)
60#define CTRL_TX_EMP_INT BIT(6)
61#define CTRL_TX_RDY_INT BIT(5)
62#define CTRL_RX_RDY_INT BIT(4)
63#define CTRL_BRK_DET_INT BIT(3)
64#define CTRL_FRM_ERR_INT BIT(2)
65#define CTRL_PAR_ERR_INT BIT(1)
66#define CTRL_OVR_ERR_INT BIT(0)
67#define CTRL_RX_INT (CTRL_RX_RDY_INT | CTRL_BRK_DET_INT |\
68 CTRL_FRM_ERR_INT | CTRL_PAR_ERR_INT | CTRL_OVR_ERR_INT)
69
70#define UART_STAT 0x0c
71#define STAT_TX_FIFO_EMP BIT(13)
72#define STAT_RX_FIFO_EMP BIT(12)
73#define STAT_TX_FIFO_FUL BIT(11)
74#define STAT_TX_FIFO_HFL BIT(10)
75#define STAT_RX_TOGL BIT(9)
76#define STAT_RX_FIFO_FUL BIT(8)
77#define STAT_RX_FIFO_HFL BIT(7)
78#define STAT_TX_EMP BIT(6)
79#define STAT_TX_RDY BIT(5)
80#define STAT_RX_RDY BIT(4)
81#define STAT_BRK_DET BIT(3)
82#define STAT_FRM_ERR BIT(2)
83#define STAT_PAR_ERR BIT(1)
84#define STAT_OVR_ERR BIT(0)
85#define STAT_BRK_ERR (STAT_BRK_DET | STAT_FRM_ERR | STAT_FRM_ERR\
86 | STAT_PAR_ERR | STAT_OVR_ERR)
87
88#define UART_BRDV 0x10
89
90#define MVEBU_NR_UARTS 1
91
92#define MVEBU_UART_TYPE "mvebu-uart"
Yehuda Yitschak02c33332017-10-13 11:01:47 +020093#define DRIVER_NAME "mvebu_serial"
Wilson Ding30530792016-02-16 19:14:53 +010094
95static struct uart_port mvebu_uart_ports[MVEBU_NR_UARTS];
96
97struct mvebu_uart_data {
98 struct uart_port *port;
99 struct clk *clk;
100};
101
102/* Core UART Driver Operations */
103static unsigned int mvebu_uart_tx_empty(struct uart_port *port)
104{
105 unsigned long flags;
106 unsigned int st;
107
108 spin_lock_irqsave(&port->lock, flags);
109 st = readl(port->membase + UART_STAT);
110 spin_unlock_irqrestore(&port->lock, flags);
111
112 return (st & STAT_TX_FIFO_EMP) ? TIOCSER_TEMT : 0;
113}
114
115static unsigned int mvebu_uart_get_mctrl(struct uart_port *port)
116{
117 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
118}
119
120static void mvebu_uart_set_mctrl(struct uart_port *port,
121 unsigned int mctrl)
122{
123/*
124 * Even if we do not support configuring the modem control lines, this
125 * function must be proided to the serial core
126 */
127}
128
129static void mvebu_uart_stop_tx(struct uart_port *port)
130{
131 unsigned int ctl = readl(port->membase + UART_CTRL);
132
133 ctl &= ~CTRL_TX_RDY_INT;
134 writel(ctl, port->membase + UART_CTRL);
135}
136
137static void mvebu_uart_start_tx(struct uart_port *port)
138{
139 unsigned int ctl = readl(port->membase + UART_CTRL);
140
141 ctl |= CTRL_TX_RDY_INT;
142 writel(ctl, port->membase + UART_CTRL);
143}
144
145static void mvebu_uart_stop_rx(struct uart_port *port)
146{
147 unsigned int ctl = readl(port->membase + UART_CTRL);
148
149 ctl &= ~CTRL_RX_INT;
150 writel(ctl, port->membase + UART_CTRL);
151}
152
153static void mvebu_uart_break_ctl(struct uart_port *port, int brk)
154{
155 unsigned int ctl;
156 unsigned long flags;
157
158 spin_lock_irqsave(&port->lock, flags);
159 ctl = readl(port->membase + UART_CTRL);
160 if (brk == -1)
161 ctl |= CTRL_SND_BRK_SEQ;
162 else
163 ctl &= ~CTRL_SND_BRK_SEQ;
164 writel(ctl, port->membase + UART_CTRL);
165 spin_unlock_irqrestore(&port->lock, flags);
166}
167
168static void mvebu_uart_rx_chars(struct uart_port *port, unsigned int status)
169{
170 struct tty_port *tport = &port->state->port;
171 unsigned char ch = 0;
172 char flag = 0;
173
174 do {
175 if (status & STAT_RX_RDY) {
176 ch = readl(port->membase + UART_RBR);
177 ch &= 0xff;
178 flag = TTY_NORMAL;
179 port->icount.rx++;
180
181 if (status & STAT_PAR_ERR)
182 port->icount.parity++;
183 }
184
185 if (status & STAT_BRK_DET) {
186 port->icount.brk++;
187 status &= ~(STAT_FRM_ERR | STAT_PAR_ERR);
188 if (uart_handle_break(port))
189 goto ignore_char;
190 }
191
192 if (status & STAT_OVR_ERR)
193 port->icount.overrun++;
194
195 if (status & STAT_FRM_ERR)
196 port->icount.frame++;
197
198 if (uart_handle_sysrq_char(port, ch))
199 goto ignore_char;
200
201 if (status & port->ignore_status_mask & STAT_PAR_ERR)
202 status &= ~STAT_RX_RDY;
203
204 status &= port->read_status_mask;
205
206 if (status & STAT_PAR_ERR)
207 flag = TTY_PARITY;
208
209 status &= ~port->ignore_status_mask;
210
211 if (status & STAT_RX_RDY)
212 tty_insert_flip_char(tport, ch, flag);
213
214 if (status & STAT_BRK_DET)
215 tty_insert_flip_char(tport, 0, TTY_BREAK);
216
217 if (status & STAT_FRM_ERR)
218 tty_insert_flip_char(tport, 0, TTY_FRAME);
219
220 if (status & STAT_OVR_ERR)
221 tty_insert_flip_char(tport, 0, TTY_OVERRUN);
222
223ignore_char:
224 status = readl(port->membase + UART_STAT);
225 } while (status & (STAT_RX_RDY | STAT_BRK_DET));
226
227 tty_flip_buffer_push(tport);
228}
229
230static void mvebu_uart_tx_chars(struct uart_port *port, unsigned int status)
231{
232 struct circ_buf *xmit = &port->state->xmit;
233 unsigned int count;
234 unsigned int st;
235
236 if (port->x_char) {
237 writel(port->x_char, port->membase + UART_TSH);
238 port->icount.tx++;
239 port->x_char = 0;
240 return;
241 }
242
243 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
244 mvebu_uart_stop_tx(port);
245 return;
246 }
247
248 for (count = 0; count < port->fifosize; count++) {
249 writel(xmit->buf[xmit->tail], port->membase + UART_TSH);
250 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
251 port->icount.tx++;
252
253 if (uart_circ_empty(xmit))
254 break;
255
256 st = readl(port->membase + UART_STAT);
257 if (st & STAT_TX_FIFO_FUL)
258 break;
259 }
260
261 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
262 uart_write_wakeup(port);
263
264 if (uart_circ_empty(xmit))
265 mvebu_uart_stop_tx(port);
266}
267
268static irqreturn_t mvebu_uart_isr(int irq, void *dev_id)
269{
270 struct uart_port *port = (struct uart_port *)dev_id;
271 unsigned int st = readl(port->membase + UART_STAT);
272
273 if (st & (STAT_RX_RDY | STAT_OVR_ERR | STAT_FRM_ERR | STAT_BRK_DET))
274 mvebu_uart_rx_chars(port, st);
275
276 if (st & STAT_TX_RDY)
277 mvebu_uart_tx_chars(port, st);
278
279 return IRQ_HANDLED;
280}
281
282static int mvebu_uart_startup(struct uart_port *port)
283{
284 int ret;
285
286 writel(CTRL_TXFIFO_RST | CTRL_RXFIFO_RST,
287 port->membase + UART_CTRL);
288 udelay(1);
289 writel(CTRL_RX_INT, port->membase + UART_CTRL);
290
Yehuda Yitschak02c33332017-10-13 11:01:47 +0200291 ret = request_irq(port->irq, mvebu_uart_isr, port->irqflags,
292 DRIVER_NAME, port);
Wilson Ding30530792016-02-16 19:14:53 +0100293 if (ret) {
294 dev_err(port->dev, "failed to request irq\n");
295 return ret;
296 }
297
298 return 0;
299}
300
301static void mvebu_uart_shutdown(struct uart_port *port)
302{
303 writel(0, port->membase + UART_CTRL);
Thomas Petazzonic2c16592016-06-16 16:48:52 +0200304
305 free_irq(port->irq, port);
Wilson Ding30530792016-02-16 19:14:53 +0100306}
307
308static void mvebu_uart_set_termios(struct uart_port *port,
309 struct ktermios *termios,
310 struct ktermios *old)
311{
312 unsigned long flags;
313 unsigned int baud;
314
315 spin_lock_irqsave(&port->lock, flags);
316
317 port->read_status_mask = STAT_RX_RDY | STAT_OVR_ERR |
318 STAT_TX_RDY | STAT_TX_FIFO_FUL;
319
320 if (termios->c_iflag & INPCK)
321 port->read_status_mask |= STAT_FRM_ERR | STAT_PAR_ERR;
322
323 port->ignore_status_mask = 0;
324 if (termios->c_iflag & IGNPAR)
325 port->ignore_status_mask |=
326 STAT_FRM_ERR | STAT_PAR_ERR | STAT_OVR_ERR;
327
328 if ((termios->c_cflag & CREAD) == 0)
329 port->ignore_status_mask |= STAT_RX_RDY | STAT_BRK_ERR;
330
331 if (old)
332 tty_termios_copy_hw(termios, old);
333
334 baud = uart_get_baud_rate(port, termios, old, 0, 460800);
335 uart_update_timeout(port, termios->c_cflag, baud);
336
337 spin_unlock_irqrestore(&port->lock, flags);
338}
339
340static const char *mvebu_uart_type(struct uart_port *port)
341{
342 return MVEBU_UART_TYPE;
343}
344
345static void mvebu_uart_release_port(struct uart_port *port)
346{
347 /* Nothing to do here */
348}
349
350static int mvebu_uart_request_port(struct uart_port *port)
351{
352 return 0;
353}
354
355#ifdef CONFIG_CONSOLE_POLL
356static int mvebu_uart_get_poll_char(struct uart_port *port)
357{
358 unsigned int st = readl(port->membase + UART_STAT);
359
360 if (!(st & STAT_RX_RDY))
361 return NO_POLL_CHAR;
362
363 return readl(port->membase + UART_RBR);
364}
365
366static void mvebu_uart_put_poll_char(struct uart_port *port, unsigned char c)
367{
368 unsigned int st;
369
370 for (;;) {
371 st = readl(port->membase + UART_STAT);
372
373 if (!(st & STAT_TX_FIFO_FUL))
374 break;
375
376 udelay(1);
377 }
378
379 writel(c, port->membase + UART_TSH);
380}
381#endif
382
383static const struct uart_ops mvebu_uart_ops = {
384 .tx_empty = mvebu_uart_tx_empty,
385 .set_mctrl = mvebu_uart_set_mctrl,
386 .get_mctrl = mvebu_uart_get_mctrl,
387 .stop_tx = mvebu_uart_stop_tx,
388 .start_tx = mvebu_uart_start_tx,
389 .stop_rx = mvebu_uart_stop_rx,
390 .break_ctl = mvebu_uart_break_ctl,
391 .startup = mvebu_uart_startup,
392 .shutdown = mvebu_uart_shutdown,
393 .set_termios = mvebu_uart_set_termios,
394 .type = mvebu_uart_type,
395 .release_port = mvebu_uart_release_port,
396 .request_port = mvebu_uart_request_port,
397#ifdef CONFIG_CONSOLE_POLL
398 .poll_get_char = mvebu_uart_get_poll_char,
399 .poll_put_char = mvebu_uart_put_poll_char,
400#endif
401};
402
403/* Console Driver Operations */
404
405#ifdef CONFIG_SERIAL_MVEBU_CONSOLE
406/* Early Console */
407static void mvebu_uart_putc(struct uart_port *port, int c)
408{
409 unsigned int st;
410
411 for (;;) {
412 st = readl(port->membase + UART_STAT);
413 if (!(st & STAT_TX_FIFO_FUL))
414 break;
415 }
416
417 writel(c, port->membase + UART_TSH);
418
419 for (;;) {
420 st = readl(port->membase + UART_STAT);
421 if (st & STAT_TX_FIFO_EMP)
422 break;
423 }
424}
425
426static void mvebu_uart_putc_early_write(struct console *con,
427 const char *s,
428 unsigned n)
429{
430 struct earlycon_device *dev = con->data;
431
432 uart_console_write(&dev->port, s, n, mvebu_uart_putc);
433}
434
435static int __init
436mvebu_uart_early_console_setup(struct earlycon_device *device,
437 const char *opt)
438{
439 if (!device->port.membase)
440 return -ENODEV;
441
442 device->con->write = mvebu_uart_putc_early_write;
443
444 return 0;
445}
446
447EARLYCON_DECLARE(ar3700_uart, mvebu_uart_early_console_setup);
448OF_EARLYCON_DECLARE(ar3700_uart, "marvell,armada-3700-uart",
449 mvebu_uart_early_console_setup);
450
451static void wait_for_xmitr(struct uart_port *port)
452{
453 u32 val;
454
455 readl_poll_timeout_atomic(port->membase + UART_STAT, val,
456 (val & STAT_TX_EMP), 1, 10000);
457}
458
459static void mvebu_uart_console_putchar(struct uart_port *port, int ch)
460{
461 wait_for_xmitr(port);
462 writel(ch, port->membase + UART_TSH);
463}
464
465static void mvebu_uart_console_write(struct console *co, const char *s,
466 unsigned int count)
467{
468 struct uart_port *port = &mvebu_uart_ports[co->index];
469 unsigned long flags;
470 unsigned int ier;
471 int locked = 1;
472
473 if (oops_in_progress)
474 locked = spin_trylock_irqsave(&port->lock, flags);
475 else
476 spin_lock_irqsave(&port->lock, flags);
477
478 ier = readl(port->membase + UART_CTRL) &
479 (CTRL_RX_INT | CTRL_TX_RDY_INT);
480 writel(0, port->membase + UART_CTRL);
481
482 uart_console_write(port, s, count, mvebu_uart_console_putchar);
483
484 wait_for_xmitr(port);
485
486 if (ier)
487 writel(ier, port->membase + UART_CTRL);
488
489 if (locked)
490 spin_unlock_irqrestore(&port->lock, flags);
491}
492
493static int mvebu_uart_console_setup(struct console *co, char *options)
494{
495 struct uart_port *port;
496 int baud = 9600;
497 int bits = 8;
498 int parity = 'n';
499 int flow = 'n';
500
501 if (co->index < 0 || co->index >= MVEBU_NR_UARTS)
502 return -EINVAL;
503
504 port = &mvebu_uart_ports[co->index];
505
506 if (!port->mapbase || !port->membase) {
507 pr_debug("console on ttyMV%i not present\n", co->index);
508 return -ENODEV;
509 }
510
511 if (options)
512 uart_parse_options(options, &baud, &parity, &bits, &flow);
513
514 return uart_set_options(port, co, baud, parity, bits, flow);
515}
516
517static struct uart_driver mvebu_uart_driver;
518
519static struct console mvebu_uart_console = {
520 .name = "ttyMV",
521 .write = mvebu_uart_console_write,
522 .device = uart_console_device,
523 .setup = mvebu_uart_console_setup,
524 .flags = CON_PRINTBUFFER,
525 .index = -1,
526 .data = &mvebu_uart_driver,
527};
528
529static int __init mvebu_uart_console_init(void)
530{
531 register_console(&mvebu_uart_console);
532 return 0;
533}
534
535console_initcall(mvebu_uart_console_init);
536
537
538#endif /* CONFIG_SERIAL_MVEBU_CONSOLE */
539
540static struct uart_driver mvebu_uart_driver = {
541 .owner = THIS_MODULE,
Yehuda Yitschak02c33332017-10-13 11:01:47 +0200542 .driver_name = DRIVER_NAME,
Wilson Ding30530792016-02-16 19:14:53 +0100543 .dev_name = "ttyMV",
544 .nr = MVEBU_NR_UARTS,
545#ifdef CONFIG_SERIAL_MVEBU_CONSOLE
546 .cons = &mvebu_uart_console,
547#endif
548};
549
550static int mvebu_uart_probe(struct platform_device *pdev)
551{
552 struct resource *reg = platform_get_resource(pdev, IORESOURCE_MEM, 0);
553 struct resource *irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
554 struct uart_port *port;
555 struct mvebu_uart_data *data;
556 int ret;
557
558 if (!reg || !irq) {
559 dev_err(&pdev->dev, "no registers/irq defined\n");
560 return -EINVAL;
561 }
562
563 port = &mvebu_uart_ports[0];
564
565 spin_lock_init(&port->lock);
566
567 port->dev = &pdev->dev;
568 port->type = PORT_MVEBU;
569 port->ops = &mvebu_uart_ops;
570 port->regshift = 0;
571
572 port->fifosize = 32;
573 port->iotype = UPIO_MEM32;
574 port->flags = UPF_FIXED_PORT;
575 port->line = 0; /* single port: force line number to 0 */
576
577 port->irq = irq->start;
578 port->irqflags = 0;
579 port->mapbase = reg->start;
580
581 port->membase = devm_ioremap_resource(&pdev->dev, reg);
582 if (IS_ERR(port->membase))
583 return -PTR_ERR(port->membase);
584
585 data = devm_kzalloc(&pdev->dev, sizeof(struct mvebu_uart_data),
586 GFP_KERNEL);
587 if (!data)
588 return -ENOMEM;
589
590 data->port = port;
591
592 port->private_data = data;
593 platform_set_drvdata(pdev, data);
594
595 ret = uart_add_one_port(&mvebu_uart_driver, port);
596 if (ret)
597 return ret;
598 return 0;
599}
600
Wilson Ding30530792016-02-16 19:14:53 +0100601/* Match table for of_platform binding */
602static const struct of_device_id mvebu_uart_of_match[] = {
603 { .compatible = "marvell,armada-3700-uart", },
604 {}
605};
Wilson Ding30530792016-02-16 19:14:53 +0100606
607static struct platform_driver mvebu_uart_platform_driver = {
608 .probe = mvebu_uart_probe,
Wilson Ding30530792016-02-16 19:14:53 +0100609 .driver = {
Wilson Ding30530792016-02-16 19:14:53 +0100610 .name = "mvebu-uart",
611 .of_match_table = of_match_ptr(mvebu_uart_of_match),
Paul Gortmaker89ebc272016-03-13 19:48:52 -0400612 .suppress_bind_attrs = true,
Wilson Ding30530792016-02-16 19:14:53 +0100613 },
614};
615
616static int __init mvebu_uart_init(void)
617{
618 int ret;
619
620 ret = uart_register_driver(&mvebu_uart_driver);
621 if (ret)
622 return ret;
623
624 ret = platform_driver_register(&mvebu_uart_platform_driver);
625 if (ret)
626 uart_unregister_driver(&mvebu_uart_driver);
627
628 return ret;
629}
Wilson Ding30530792016-02-16 19:14:53 +0100630arch_initcall(mvebu_uart_init);