1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Freescale LINFlexD UART serial port driver
5 * Copyright 2012-2016 Freescale Semiconductor, Inc.
6 * Copyright 2017-2019 NXP
9 #include <linux/console.h>
11 #include <linux/irq.h>
12 #include <linux/module.h>
14 #include <linux/of_device.h>
15 #include <linux/serial_core.h>
16 #include <linux/slab.h>
17 #include <linux/tty_flip.h>
18 #include <linux/delay.h>
20 /* All registers are 32-bit width */
22 #define LINCR1 0x0000 /* LIN control register */
23 #define LINIER 0x0004 /* LIN interrupt enable register */
24 #define LINSR 0x0008 /* LIN status register */
25 #define LINESR 0x000C /* LIN error status register */
26 #define UARTCR 0x0010 /* UART mode control register */
27 #define UARTSR 0x0014 /* UART mode status register */
28 #define LINTCSR 0x0018 /* LIN timeout control status register */
29 #define LINOCR 0x001C /* LIN output compare register */
30 #define LINTOCR 0x0020 /* LIN timeout control register */
31 #define LINFBRR 0x0024 /* LIN fractional baud rate register */
32 #define LINIBRR 0x0028 /* LIN integer baud rate register */
33 #define LINCFR 0x002C /* LIN checksum field register */
34 #define LINCR2 0x0030 /* LIN control register 2 */
35 #define BIDR 0x0034 /* Buffer identifier register */
36 #define BDRL 0x0038 /* Buffer data register least significant */
37 #define BDRM 0x003C /* Buffer data register most significant */
38 #define IFER 0x0040 /* Identifier filter enable register */
39 #define IFMI 0x0044 /* Identifier filter match index */
40 #define IFMR 0x0048 /* Identifier filter mode register */
41 #define GCR 0x004C /* Global control register */
42 #define UARTPTO 0x0050 /* UART preset timeout register */
43 #define UARTCTO 0x0054 /* UART current timeout register */
46 * Register field definitions
49 #define LINFLEXD_LINCR1_INIT BIT(0)
50 #define LINFLEXD_LINCR1_MME BIT(4)
51 #define LINFLEXD_LINCR1_BF BIT(7)
53 #define LINFLEXD_LINSR_LINS_INITMODE BIT(12)
54 #define LINFLEXD_LINSR_LINS_MASK (0xF << 12)
56 #define LINFLEXD_LINIER_SZIE BIT(15)
57 #define LINFLEXD_LINIER_OCIE BIT(14)
58 #define LINFLEXD_LINIER_BEIE BIT(13)
59 #define LINFLEXD_LINIER_CEIE BIT(12)
60 #define LINFLEXD_LINIER_HEIE BIT(11)
61 #define LINFLEXD_LINIER_FEIE BIT(8)
62 #define LINFLEXD_LINIER_BOIE BIT(7)
63 #define LINFLEXD_LINIER_LSIE BIT(6)
64 #define LINFLEXD_LINIER_WUIE BIT(5)
65 #define LINFLEXD_LINIER_DBFIE BIT(4)
66 #define LINFLEXD_LINIER_DBEIETOIE BIT(3)
67 #define LINFLEXD_LINIER_DRIE BIT(2)
68 #define LINFLEXD_LINIER_DTIE BIT(1)
69 #define LINFLEXD_LINIER_HRIE BIT(0)
71 #define LINFLEXD_UARTCR_OSR_MASK (0xF << 24)
72 #define LINFLEXD_UARTCR_OSR(uartcr) (((uartcr) \
73 & LINFLEXD_UARTCR_OSR_MASK) >> 24)
75 #define LINFLEXD_UARTCR_ROSE BIT(23)
77 #define LINFLEXD_UARTCR_RFBM BIT(9)
78 #define LINFLEXD_UARTCR_TFBM BIT(8)
79 #define LINFLEXD_UARTCR_WL1 BIT(7)
80 #define LINFLEXD_UARTCR_PC1 BIT(6)
82 #define LINFLEXD_UARTCR_RXEN BIT(5)
83 #define LINFLEXD_UARTCR_TXEN BIT(4)
84 #define LINFLEXD_UARTCR_PC0 BIT(3)
86 #define LINFLEXD_UARTCR_PCE BIT(2)
87 #define LINFLEXD_UARTCR_WL0 BIT(1)
88 #define LINFLEXD_UARTCR_UART BIT(0)
90 #define LINFLEXD_UARTSR_SZF BIT(15)
91 #define LINFLEXD_UARTSR_OCF BIT(14)
92 #define LINFLEXD_UARTSR_PE3 BIT(13)
93 #define LINFLEXD_UARTSR_PE2 BIT(12)
94 #define LINFLEXD_UARTSR_PE1 BIT(11)
95 #define LINFLEXD_UARTSR_PE0 BIT(10)
96 #define LINFLEXD_UARTSR_RMB BIT(9)
97 #define LINFLEXD_UARTSR_FEF BIT(8)
98 #define LINFLEXD_UARTSR_BOF BIT(7)
99 #define LINFLEXD_UARTSR_RPS BIT(6)
100 #define LINFLEXD_UARTSR_WUF BIT(5)
101 #define LINFLEXD_UARTSR_4 BIT(4)
103 #define LINFLEXD_UARTSR_TO BIT(3)
105 #define LINFLEXD_UARTSR_DRFRFE BIT(2)
106 #define LINFLEXD_UARTSR_DTFTFF BIT(1)
107 #define LINFLEXD_UARTSR_NF BIT(0)
108 #define LINFLEXD_UARTSR_PE (LINFLEXD_UARTSR_PE0 |\
109 LINFLEXD_UARTSR_PE1 |\
110 LINFLEXD_UARTSR_PE2 |\
113 #define LINFLEX_LDIV_MULTIPLIER (16)
115 #define DRIVER_NAME "fsl-linflexuart"
116 #define DEV_NAME "ttyLF"
119 #define EARLYCON_BUFFER_INITIAL_CAP 8
121 #define PREINIT_DELAY 2000 /* us */
123 static const struct of_device_id linflex_dt_ids[] = {
125 .compatible = "fsl,s32v234-linflexuart",
129 MODULE_DEVICE_TABLE(of, linflex_dt_ids);
131 #ifdef CONFIG_SERIAL_FSL_LINFLEXUART_CONSOLE
132 static struct uart_port *earlycon_port;
133 static bool linflex_earlycon_same_instance;
134 static DEFINE_SPINLOCK(init_lock);
135 static bool during_init;
139 unsigned int len, cap;
143 static void linflex_stop_tx(struct uart_port *port)
147 ier = readl(port->membase + LINIER);
148 ier &= ~(LINFLEXD_LINIER_DTIE);
149 writel(ier, port->membase + LINIER);
152 static void linflex_stop_rx(struct uart_port *port)
156 ier = readl(port->membase + LINIER);
157 writel(ier & ~LINFLEXD_LINIER_DRIE, port->membase + LINIER);
160 static void linflex_put_char(struct uart_port *sport, unsigned char c)
162 unsigned long status;
164 writeb(c, sport->membase + BDRL);
166 /* Waiting for data transmission completed. */
167 while (((status = readl(sport->membase + UARTSR)) &
168 LINFLEXD_UARTSR_DTFTFF) !=
169 LINFLEXD_UARTSR_DTFTFF)
172 writel(status | LINFLEXD_UARTSR_DTFTFF, sport->membase + UARTSR);
175 static inline void linflex_transmit_buffer(struct uart_port *sport)
177 struct circ_buf *xmit = &sport->state->xmit;
179 while (!uart_circ_empty(xmit)) {
180 linflex_put_char(sport, xmit->buf[xmit->tail]);
181 uart_xmit_advance(sport, 1);
184 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
185 uart_write_wakeup(sport);
187 if (uart_circ_empty(xmit))
188 linflex_stop_tx(sport);
191 static void linflex_start_tx(struct uart_port *port)
195 linflex_transmit_buffer(port);
196 ier = readl(port->membase + LINIER);
197 writel(ier | LINFLEXD_LINIER_DTIE, port->membase + LINIER);
200 static irqreturn_t linflex_txint(int irq, void *dev_id)
202 struct uart_port *sport = dev_id;
203 struct circ_buf *xmit = &sport->state->xmit;
206 spin_lock_irqsave(&sport->lock, flags);
209 linflex_put_char(sport, sport->x_char);
213 if (uart_circ_empty(xmit) || uart_tx_stopped(sport)) {
214 linflex_stop_tx(sport);
218 linflex_transmit_buffer(sport);
220 spin_unlock_irqrestore(&sport->lock, flags);
224 static irqreturn_t linflex_rxint(int irq, void *dev_id)
226 struct uart_port *sport = dev_id;
228 struct tty_port *port = &sport->state->port;
229 unsigned long flags, status;
233 spin_lock_irqsave(&sport->lock, flags);
235 status = readl(sport->membase + UARTSR);
236 while (status & LINFLEXD_UARTSR_RMB) {
237 rx = readb(sport->membase + BDRM);
242 if (status & (LINFLEXD_UARTSR_BOF | LINFLEXD_UARTSR_FEF |
243 LINFLEXD_UARTSR_PE)) {
244 if (status & LINFLEXD_UARTSR_BOF)
245 sport->icount.overrun++;
246 if (status & LINFLEXD_UARTSR_FEF) {
251 sport->icount.frame++;
253 if (status & LINFLEXD_UARTSR_PE)
254 sport->icount.parity++;
257 writel(status, sport->membase + UARTSR);
258 status = readl(sport->membase + UARTSR);
261 uart_handle_break(sport);
263 if (uart_handle_sysrq_char(sport, (unsigned char)rx))
265 tty_insert_flip_char(port, rx, flg);
269 spin_unlock_irqrestore(&sport->lock, flags);
271 tty_flip_buffer_push(port);
276 static irqreturn_t linflex_int(int irq, void *dev_id)
278 struct uart_port *sport = dev_id;
279 unsigned long status;
281 status = readl(sport->membase + UARTSR);
283 if (status & LINFLEXD_UARTSR_DRFRFE)
284 linflex_rxint(irq, dev_id);
285 if (status & LINFLEXD_UARTSR_DTFTFF)
286 linflex_txint(irq, dev_id);
291 /* return TIOCSER_TEMT when transmitter is not busy */
292 static unsigned int linflex_tx_empty(struct uart_port *port)
294 unsigned long status;
296 status = readl(port->membase + UARTSR) & LINFLEXD_UARTSR_DTFTFF;
298 return status ? TIOCSER_TEMT : 0;
301 static unsigned int linflex_get_mctrl(struct uart_port *port)
306 static void linflex_set_mctrl(struct uart_port *port, unsigned int mctrl)
310 static void linflex_break_ctl(struct uart_port *port, int break_state)
314 static void linflex_setup_watermark(struct uart_port *sport)
316 unsigned long cr, ier, cr1;
318 /* Disable transmission/reception */
319 ier = readl(sport->membase + LINIER);
320 ier &= ~(LINFLEXD_LINIER_DRIE | LINFLEXD_LINIER_DTIE);
321 writel(ier, sport->membase + LINIER);
323 cr = readl(sport->membase + UARTCR);
324 cr &= ~(LINFLEXD_UARTCR_RXEN | LINFLEXD_UARTCR_TXEN);
325 writel(cr, sport->membase + UARTCR);
327 /* Enter initialization mode by setting INIT bit */
329 /* set the Linflex in master mode and activate by-pass filter */
330 cr1 = LINFLEXD_LINCR1_BF | LINFLEXD_LINCR1_MME
331 | LINFLEXD_LINCR1_INIT;
332 writel(cr1, sport->membase + LINCR1);
334 /* wait for init mode entry */
335 while ((readl(sport->membase + LINSR)
336 & LINFLEXD_LINSR_LINS_MASK)
337 != LINFLEXD_LINSR_LINS_INITMODE)
341 * UART = 0x1; - Linflex working in UART mode
342 * TXEN = 0x1; - Enable transmission of data now
343 * RXEn = 0x1; - Receiver enabled
344 * WL0 = 0x1; - 8 bit data
345 * PCE = 0x0; - No parity
348 /* set UART bit to allow writing other bits */
349 writel(LINFLEXD_UARTCR_UART, sport->membase + UARTCR);
351 cr = (LINFLEXD_UARTCR_RXEN | LINFLEXD_UARTCR_TXEN |
352 LINFLEXD_UARTCR_WL0 | LINFLEXD_UARTCR_UART);
354 writel(cr, sport->membase + UARTCR);
356 cr1 &= ~(LINFLEXD_LINCR1_INIT);
358 writel(cr1, sport->membase + LINCR1);
360 ier = readl(sport->membase + LINIER);
361 ier |= LINFLEXD_LINIER_DRIE;
362 ier |= LINFLEXD_LINIER_DTIE;
364 writel(ier, sport->membase + LINIER);
367 static int linflex_startup(struct uart_port *port)
372 spin_lock_irqsave(&port->lock, flags);
374 linflex_setup_watermark(port);
376 spin_unlock_irqrestore(&port->lock, flags);
378 ret = devm_request_irq(port->dev, port->irq, linflex_int, 0,
384 static void linflex_shutdown(struct uart_port *port)
389 spin_lock_irqsave(&port->lock, flags);
391 /* disable interrupts */
392 ier = readl(port->membase + LINIER);
393 ier &= ~(LINFLEXD_LINIER_DRIE | LINFLEXD_LINIER_DTIE);
394 writel(ier, port->membase + LINIER);
396 spin_unlock_irqrestore(&port->lock, flags);
398 devm_free_irq(port->dev, port->irq, port);
402 linflex_set_termios(struct uart_port *port, struct ktermios *termios,
403 const struct ktermios *old)
406 unsigned long cr, old_cr, cr1;
407 unsigned int old_csize = old ? old->c_cflag & CSIZE : CS8;
409 cr = readl(port->membase + UARTCR);
412 /* Enter initialization mode by setting INIT bit */
413 cr1 = readl(port->membase + LINCR1);
414 cr1 |= LINFLEXD_LINCR1_INIT;
415 writel(cr1, port->membase + LINCR1);
417 /* wait for init mode entry */
418 while ((readl(port->membase + LINSR)
419 & LINFLEXD_LINSR_LINS_MASK)
420 != LINFLEXD_LINSR_LINS_INITMODE)
424 * only support CS8 and CS7, and for CS7 must enable PE.
430 /* enter the UART into configuration mode */
432 while ((termios->c_cflag & CSIZE) != CS8 &&
433 (termios->c_cflag & CSIZE) != CS7) {
434 termios->c_cflag &= ~CSIZE;
435 termios->c_cflag |= old_csize;
439 if ((termios->c_cflag & CSIZE) == CS7) {
440 /* Word length: WL1WL0:00 */
441 cr = old_cr & ~LINFLEXD_UARTCR_WL1 & ~LINFLEXD_UARTCR_WL0;
444 if ((termios->c_cflag & CSIZE) == CS8) {
445 /* Word length: WL1WL0:01 */
446 cr = (old_cr | LINFLEXD_UARTCR_WL0) & ~LINFLEXD_UARTCR_WL1;
449 if (termios->c_cflag & CMSPAR) {
450 if ((termios->c_cflag & CSIZE) != CS8) {
451 termios->c_cflag &= ~CSIZE;
452 termios->c_cflag |= CS8;
454 /* has a space/sticky bit */
455 cr |= LINFLEXD_UARTCR_WL0;
458 if (termios->c_cflag & CSTOPB)
459 termios->c_cflag &= ~CSTOPB;
461 /* parity must be enabled when CS7 to match 8-bits format */
462 if ((termios->c_cflag & CSIZE) == CS7)
463 termios->c_cflag |= PARENB;
465 if ((termios->c_cflag & PARENB)) {
466 cr |= LINFLEXD_UARTCR_PCE;
467 if (termios->c_cflag & PARODD)
468 cr = (cr | LINFLEXD_UARTCR_PC0) &
469 (~LINFLEXD_UARTCR_PC1);
471 cr = cr & (~LINFLEXD_UARTCR_PC1 &
472 ~LINFLEXD_UARTCR_PC0);
474 cr &= ~LINFLEXD_UARTCR_PCE;
477 spin_lock_irqsave(&port->lock, flags);
479 port->read_status_mask = 0;
481 if (termios->c_iflag & INPCK)
482 port->read_status_mask |= (LINFLEXD_UARTSR_FEF |
483 LINFLEXD_UARTSR_PE0 |
484 LINFLEXD_UARTSR_PE1 |
485 LINFLEXD_UARTSR_PE2 |
486 LINFLEXD_UARTSR_PE3);
487 if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
488 port->read_status_mask |= LINFLEXD_UARTSR_FEF;
490 /* characters to ignore */
491 port->ignore_status_mask = 0;
492 if (termios->c_iflag & IGNPAR)
493 port->ignore_status_mask |= LINFLEXD_UARTSR_PE;
494 if (termios->c_iflag & IGNBRK) {
495 port->ignore_status_mask |= LINFLEXD_UARTSR_PE;
497 * if we're ignoring parity and break indicators,
498 * ignore overruns too (for real raw support).
500 if (termios->c_iflag & IGNPAR)
501 port->ignore_status_mask |= LINFLEXD_UARTSR_BOF;
504 writel(cr, port->membase + UARTCR);
506 cr1 &= ~(LINFLEXD_LINCR1_INIT);
508 writel(cr1, port->membase + LINCR1);
510 spin_unlock_irqrestore(&port->lock, flags);
513 static const char *linflex_type(struct uart_port *port)
515 return "FSL_LINFLEX";
518 static void linflex_release_port(struct uart_port *port)
523 static int linflex_request_port(struct uart_port *port)
528 /* configure/auto-configure the port */
529 static void linflex_config_port(struct uart_port *port, int flags)
531 if (flags & UART_CONFIG_TYPE)
532 port->type = PORT_LINFLEXUART;
535 static const struct uart_ops linflex_pops = {
536 .tx_empty = linflex_tx_empty,
537 .set_mctrl = linflex_set_mctrl,
538 .get_mctrl = linflex_get_mctrl,
539 .stop_tx = linflex_stop_tx,
540 .start_tx = linflex_start_tx,
541 .stop_rx = linflex_stop_rx,
542 .break_ctl = linflex_break_ctl,
543 .startup = linflex_startup,
544 .shutdown = linflex_shutdown,
545 .set_termios = linflex_set_termios,
546 .type = linflex_type,
547 .request_port = linflex_request_port,
548 .release_port = linflex_release_port,
549 .config_port = linflex_config_port,
552 static struct uart_port *linflex_ports[UART_NR];
554 #ifdef CONFIG_SERIAL_FSL_LINFLEXUART_CONSOLE
555 static void linflex_console_putchar(struct uart_port *port, unsigned char ch)
559 cr = readl(port->membase + UARTCR);
561 writeb(ch, port->membase + BDRL);
563 if (!(cr & LINFLEXD_UARTCR_TFBM))
564 while ((readl(port->membase + UARTSR) &
565 LINFLEXD_UARTSR_DTFTFF)
566 != LINFLEXD_UARTSR_DTFTFF)
569 while (readl(port->membase + UARTSR) &
570 LINFLEXD_UARTSR_DTFTFF)
573 if (!(cr & LINFLEXD_UARTCR_TFBM)) {
574 writel((readl(port->membase + UARTSR) |
575 LINFLEXD_UARTSR_DTFTFF),
576 port->membase + UARTSR);
580 static void linflex_earlycon_putchar(struct uart_port *port, unsigned char ch)
585 if (!linflex_earlycon_same_instance) {
586 linflex_console_putchar(port, ch);
590 spin_lock_irqsave(&init_lock, flags);
594 if (earlycon_buf.len >= 1 << CONFIG_LOG_BUF_SHIFT)
597 if (!earlycon_buf.cap) {
598 earlycon_buf.content = kmalloc(EARLYCON_BUFFER_INITIAL_CAP,
600 earlycon_buf.cap = earlycon_buf.content ?
601 EARLYCON_BUFFER_INITIAL_CAP : 0;
602 } else if (earlycon_buf.len == earlycon_buf.cap) {
603 ret = krealloc(earlycon_buf.content, earlycon_buf.cap << 1,
606 earlycon_buf.content = ret;
607 earlycon_buf.cap <<= 1;
611 if (earlycon_buf.len < earlycon_buf.cap)
612 earlycon_buf.content[earlycon_buf.len++] = ch;
617 linflex_console_putchar(port, ch);
619 spin_unlock_irqrestore(&init_lock, flags);
622 static void linflex_string_write(struct uart_port *sport, const char *s,
625 unsigned long cr, ier = 0;
627 ier = readl(sport->membase + LINIER);
628 linflex_stop_tx(sport);
630 cr = readl(sport->membase + UARTCR);
631 cr |= (LINFLEXD_UARTCR_TXEN);
632 writel(cr, sport->membase + UARTCR);
634 uart_console_write(sport, s, count, linflex_console_putchar);
636 writel(ier, sport->membase + LINIER);
640 linflex_console_write(struct console *co, const char *s, unsigned int count)
642 struct uart_port *sport = linflex_ports[co->index];
648 else if (oops_in_progress)
649 locked = spin_trylock_irqsave(&sport->lock, flags);
651 spin_lock_irqsave(&sport->lock, flags);
653 linflex_string_write(sport, s, count);
656 spin_unlock_irqrestore(&sport->lock, flags);
660 * if the port was already initialised (eg, by a boot loader),
661 * try to determine the current setup.
664 linflex_console_get_options(struct uart_port *sport, int *parity, int *bits)
668 cr = readl(sport->membase + UARTCR);
669 cr &= LINFLEXD_UARTCR_RXEN | LINFLEXD_UARTCR_TXEN;
674 /* ok, the port was enabled */
677 if (cr & LINFLEXD_UARTCR_PCE) {
678 if (cr & LINFLEXD_UARTCR_PC0)
684 if ((cr & LINFLEXD_UARTCR_WL0) && ((cr & LINFLEXD_UARTCR_WL1) == 0)) {
685 if (cr & LINFLEXD_UARTCR_PCE)
692 static int __init linflex_console_setup(struct console *co, char *options)
694 struct uart_port *sport;
703 * check whether an invalid uart number has been specified, and
704 * if so, search for the first available port that does have
707 if (co->index == -1 || co->index >= ARRAY_SIZE(linflex_ports))
710 sport = linflex_ports[co->index];
715 uart_parse_options(options, &baud, &parity, &bits, &flow);
717 linflex_console_get_options(sport, &parity, &bits);
719 if (earlycon_port && sport->mapbase == earlycon_port->mapbase) {
720 linflex_earlycon_same_instance = true;
722 spin_lock_irqsave(&init_lock, flags);
724 spin_unlock_irqrestore(&init_lock, flags);
726 /* Workaround for character loss or output of many invalid
727 * characters, when INIT mode is entered shortly after a
728 * character has just been printed.
730 udelay(PREINIT_DELAY);
733 linflex_setup_watermark(sport);
735 ret = uart_set_options(sport, co, baud, parity, bits, flow);
737 if (!linflex_earlycon_same_instance)
740 spin_lock_irqsave(&init_lock, flags);
742 /* Emptying buffer */
743 if (earlycon_buf.len) {
744 for (i = 0; i < earlycon_buf.len; i++)
745 linflex_console_putchar(earlycon_port,
746 earlycon_buf.content[i]);
748 kfree(earlycon_buf.content);
749 earlycon_buf.len = 0;
753 spin_unlock_irqrestore(&init_lock, flags);
759 static struct uart_driver linflex_reg;
760 static struct console linflex_console = {
762 .write = linflex_console_write,
763 .device = uart_console_device,
764 .setup = linflex_console_setup,
765 .flags = CON_PRINTBUFFER,
767 .data = &linflex_reg,
770 static void linflex_earlycon_write(struct console *con, const char *s,
773 struct earlycon_device *dev = con->data;
775 uart_console_write(&dev->port, s, n, linflex_earlycon_putchar);
778 static int __init linflex_early_console_setup(struct earlycon_device *device,
781 if (!device->port.membase)
784 device->con->write = linflex_earlycon_write;
785 earlycon_port = &device->port;
790 OF_EARLYCON_DECLARE(linflex, "fsl,s32v234-linflexuart",
791 linflex_early_console_setup);
793 #define LINFLEX_CONSOLE (&linflex_console)
795 #define LINFLEX_CONSOLE NULL
798 static struct uart_driver linflex_reg = {
799 .owner = THIS_MODULE,
800 .driver_name = DRIVER_NAME,
801 .dev_name = DEV_NAME,
802 .nr = ARRAY_SIZE(linflex_ports),
803 .cons = LINFLEX_CONSOLE,
806 static int linflex_probe(struct platform_device *pdev)
808 struct device_node *np = pdev->dev.of_node;
809 struct uart_port *sport;
810 struct resource *res;
813 sport = devm_kzalloc(&pdev->dev, sizeof(*sport), GFP_KERNEL);
817 ret = of_alias_get_id(np, "serial");
819 dev_err(&pdev->dev, "failed to get alias id, errno %d\n", ret);
822 if (ret >= UART_NR) {
823 dev_err(&pdev->dev, "driver limited to %d serial ports\n",
830 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
834 sport->mapbase = res->start;
835 sport->membase = devm_ioremap_resource(&pdev->dev, res);
836 if (IS_ERR(sport->membase))
837 return PTR_ERR(sport->membase);
839 sport->dev = &pdev->dev;
840 sport->type = PORT_LINFLEXUART;
841 sport->iotype = UPIO_MEM;
842 sport->irq = platform_get_irq(pdev, 0);
843 sport->ops = &linflex_pops;
844 sport->flags = UPF_BOOT_AUTOCONF;
845 sport->has_sysrq = IS_ENABLED(CONFIG_SERIAL_FSL_LINFLEXUART_CONSOLE);
847 linflex_ports[sport->line] = sport;
849 platform_set_drvdata(pdev, sport);
851 return uart_add_one_port(&linflex_reg, sport);
854 static int linflex_remove(struct platform_device *pdev)
856 struct uart_port *sport = platform_get_drvdata(pdev);
858 uart_remove_one_port(&linflex_reg, sport);
863 #ifdef CONFIG_PM_SLEEP
864 static int linflex_suspend(struct device *dev)
866 struct uart_port *sport = dev_get_drvdata(dev);
868 uart_suspend_port(&linflex_reg, sport);
873 static int linflex_resume(struct device *dev)
875 struct uart_port *sport = dev_get_drvdata(dev);
877 uart_resume_port(&linflex_reg, sport);
883 static SIMPLE_DEV_PM_OPS(linflex_pm_ops, linflex_suspend, linflex_resume);
885 static struct platform_driver linflex_driver = {
886 .probe = linflex_probe,
887 .remove = linflex_remove,
890 .of_match_table = linflex_dt_ids,
891 .pm = &linflex_pm_ops,
895 static int __init linflex_serial_init(void)
899 ret = uart_register_driver(&linflex_reg);
903 ret = platform_driver_register(&linflex_driver);
905 uart_unregister_driver(&linflex_reg);
910 static void __exit linflex_serial_exit(void)
912 platform_driver_unregister(&linflex_driver);
913 uart_unregister_driver(&linflex_reg);
916 module_init(linflex_serial_init);
917 module_exit(linflex_serial_exit);
919 MODULE_DESCRIPTION("Freescale LINFlexD serial port driver");
920 MODULE_LICENSE("GPL v2");