Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / tty / serial / lpc32xx_hs.c
blob1fa098d7aec4b230301c5df15b7cb239e27af9ea
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * High Speed Serial Ports on NXP LPC32xx SoC
5 * Authors: Kevin Wells <kevin.wells@nxp.com>
6 * Roland Stigge <stigge@antcom.de>
8 * Copyright (C) 2010 NXP Semiconductors
9 * Copyright (C) 2012 Roland Stigge
12 #include <linux/module.h>
13 #include <linux/ioport.h>
14 #include <linux/init.h>
15 #include <linux/console.h>
16 #include <linux/sysrq.h>
17 #include <linux/tty.h>
18 #include <linux/tty_flip.h>
19 #include <linux/serial_core.h>
20 #include <linux/serial.h>
21 #include <linux/platform_device.h>
22 #include <linux/delay.h>
23 #include <linux/nmi.h>
24 #include <linux/io.h>
25 #include <linux/irq.h>
26 #include <linux/of.h>
27 #include <linux/sizes.h>
28 #include <linux/soc/nxp/lpc32xx-misc.h>
31 * High Speed UART register offsets
33 #define LPC32XX_HSUART_FIFO(x) ((x) + 0x00)
34 #define LPC32XX_HSUART_LEVEL(x) ((x) + 0x04)
35 #define LPC32XX_HSUART_IIR(x) ((x) + 0x08)
36 #define LPC32XX_HSUART_CTRL(x) ((x) + 0x0C)
37 #define LPC32XX_HSUART_RATE(x) ((x) + 0x10)
39 #define LPC32XX_HSU_BREAK_DATA (1 << 10)
40 #define LPC32XX_HSU_ERROR_DATA (1 << 9)
41 #define LPC32XX_HSU_RX_EMPTY (1 << 8)
43 #define LPC32XX_HSU_TX_LEV(n) (((n) >> 8) & 0xFF)
44 #define LPC32XX_HSU_RX_LEV(n) ((n) & 0xFF)
46 #define LPC32XX_HSU_TX_INT_SET (1 << 6)
47 #define LPC32XX_HSU_RX_OE_INT (1 << 5)
48 #define LPC32XX_HSU_BRK_INT (1 << 4)
49 #define LPC32XX_HSU_FE_INT (1 << 3)
50 #define LPC32XX_HSU_RX_TIMEOUT_INT (1 << 2)
51 #define LPC32XX_HSU_RX_TRIG_INT (1 << 1)
52 #define LPC32XX_HSU_TX_INT (1 << 0)
54 #define LPC32XX_HSU_HRTS_INV (1 << 21)
55 #define LPC32XX_HSU_HRTS_TRIG_8B (0x0 << 19)
56 #define LPC32XX_HSU_HRTS_TRIG_16B (0x1 << 19)
57 #define LPC32XX_HSU_HRTS_TRIG_32B (0x2 << 19)
58 #define LPC32XX_HSU_HRTS_TRIG_48B (0x3 << 19)
59 #define LPC32XX_HSU_HRTS_EN (1 << 18)
60 #define LPC32XX_HSU_TMO_DISABLED (0x0 << 16)
61 #define LPC32XX_HSU_TMO_INACT_4B (0x1 << 16)
62 #define LPC32XX_HSU_TMO_INACT_8B (0x2 << 16)
63 #define LPC32XX_HSU_TMO_INACT_16B (0x3 << 16)
64 #define LPC32XX_HSU_HCTS_INV (1 << 15)
65 #define LPC32XX_HSU_HCTS_EN (1 << 14)
66 #define LPC32XX_HSU_OFFSET(n) ((n) << 9)
67 #define LPC32XX_HSU_BREAK (1 << 8)
68 #define LPC32XX_HSU_ERR_INT_EN (1 << 7)
69 #define LPC32XX_HSU_RX_INT_EN (1 << 6)
70 #define LPC32XX_HSU_TX_INT_EN (1 << 5)
71 #define LPC32XX_HSU_RX_TL1B (0x0 << 2)
72 #define LPC32XX_HSU_RX_TL4B (0x1 << 2)
73 #define LPC32XX_HSU_RX_TL8B (0x2 << 2)
74 #define LPC32XX_HSU_RX_TL16B (0x3 << 2)
75 #define LPC32XX_HSU_RX_TL32B (0x4 << 2)
76 #define LPC32XX_HSU_RX_TL48B (0x5 << 2)
77 #define LPC32XX_HSU_TX_TLEMPTY (0x0 << 0)
78 #define LPC32XX_HSU_TX_TL0B (0x0 << 0)
79 #define LPC32XX_HSU_TX_TL4B (0x1 << 0)
80 #define LPC32XX_HSU_TX_TL8B (0x2 << 0)
81 #define LPC32XX_HSU_TX_TL16B (0x3 << 0)
83 #define LPC32XX_MAIN_OSC_FREQ 13000000
85 #define MODNAME "lpc32xx_hsuart"
87 struct lpc32xx_hsuart_port {
88 struct uart_port port;
91 #define FIFO_READ_LIMIT 128
92 #define MAX_PORTS 3
93 #define LPC32XX_TTY_NAME "ttyTX"
94 static struct lpc32xx_hsuart_port lpc32xx_hs_ports[MAX_PORTS];
96 #ifdef CONFIG_SERIAL_HS_LPC32XX_CONSOLE
97 static void wait_for_xmit_empty(struct uart_port *port)
99 unsigned int timeout = 10000;
101 do {
102 if (LPC32XX_HSU_TX_LEV(readl(LPC32XX_HSUART_LEVEL(
103 port->membase))) == 0)
104 break;
105 if (--timeout == 0)
106 break;
107 udelay(1);
108 } while (1);
111 static void wait_for_xmit_ready(struct uart_port *port)
113 unsigned int timeout = 10000;
115 while (1) {
116 if (LPC32XX_HSU_TX_LEV(readl(LPC32XX_HSUART_LEVEL(
117 port->membase))) < 32)
118 break;
119 if (--timeout == 0)
120 break;
121 udelay(1);
125 static void lpc32xx_hsuart_console_putchar(struct uart_port *port, int ch)
127 wait_for_xmit_ready(port);
128 writel((u32)ch, LPC32XX_HSUART_FIFO(port->membase));
131 static void lpc32xx_hsuart_console_write(struct console *co, const char *s,
132 unsigned int count)
134 struct lpc32xx_hsuart_port *up = &lpc32xx_hs_ports[co->index];
135 unsigned long flags;
136 int locked = 1;
138 touch_nmi_watchdog();
139 local_irq_save(flags);
140 if (up->port.sysrq)
141 locked = 0;
142 else if (oops_in_progress)
143 locked = spin_trylock(&up->port.lock);
144 else
145 spin_lock(&up->port.lock);
147 uart_console_write(&up->port, s, count, lpc32xx_hsuart_console_putchar);
148 wait_for_xmit_empty(&up->port);
150 if (locked)
151 spin_unlock(&up->port.lock);
152 local_irq_restore(flags);
155 static int __init lpc32xx_hsuart_console_setup(struct console *co,
156 char *options)
158 struct uart_port *port;
159 int baud = 115200;
160 int bits = 8;
161 int parity = 'n';
162 int flow = 'n';
164 if (co->index >= MAX_PORTS)
165 co->index = 0;
167 port = &lpc32xx_hs_ports[co->index].port;
168 if (!port->membase)
169 return -ENODEV;
171 if (options)
172 uart_parse_options(options, &baud, &parity, &bits, &flow);
174 lpc32xx_loopback_set(port->mapbase, 0); /* get out of loopback mode */
176 return uart_set_options(port, co, baud, parity, bits, flow);
179 static struct uart_driver lpc32xx_hsuart_reg;
180 static struct console lpc32xx_hsuart_console = {
181 .name = LPC32XX_TTY_NAME,
182 .write = lpc32xx_hsuart_console_write,
183 .device = uart_console_device,
184 .setup = lpc32xx_hsuart_console_setup,
185 .flags = CON_PRINTBUFFER,
186 .index = -1,
187 .data = &lpc32xx_hsuart_reg,
190 static int __init lpc32xx_hsuart_console_init(void)
192 register_console(&lpc32xx_hsuart_console);
193 return 0;
195 console_initcall(lpc32xx_hsuart_console_init);
197 #define LPC32XX_HSUART_CONSOLE (&lpc32xx_hsuart_console)
198 #else
199 #define LPC32XX_HSUART_CONSOLE NULL
200 #endif
202 static struct uart_driver lpc32xx_hs_reg = {
203 .owner = THIS_MODULE,
204 .driver_name = MODNAME,
205 .dev_name = LPC32XX_TTY_NAME,
206 .nr = MAX_PORTS,
207 .cons = LPC32XX_HSUART_CONSOLE,
209 static int uarts_registered;
211 static unsigned int __serial_get_clock_div(unsigned long uartclk,
212 unsigned long rate)
214 u32 div, goodrate, hsu_rate, l_hsu_rate, comprate;
215 u32 rate_diff;
217 /* Find the closest divider to get the desired clock rate */
218 div = uartclk / rate;
219 goodrate = hsu_rate = (div / 14) - 1;
220 if (hsu_rate != 0)
221 hsu_rate--;
223 /* Tweak divider */
224 l_hsu_rate = hsu_rate + 3;
225 rate_diff = 0xFFFFFFFF;
227 while (hsu_rate < l_hsu_rate) {
228 comprate = uartclk / ((hsu_rate + 1) * 14);
229 if (abs(comprate - rate) < rate_diff) {
230 goodrate = hsu_rate;
231 rate_diff = abs(comprate - rate);
234 hsu_rate++;
236 if (hsu_rate > 0xFF)
237 hsu_rate = 0xFF;
239 return goodrate;
242 static void __serial_uart_flush(struct uart_port *port)
244 int cnt = 0;
246 while ((readl(LPC32XX_HSUART_LEVEL(port->membase)) > 0) &&
247 (cnt++ < FIFO_READ_LIMIT))
248 readl(LPC32XX_HSUART_FIFO(port->membase));
251 static void __serial_lpc32xx_rx(struct uart_port *port)
253 struct tty_port *tport = &port->state->port;
254 unsigned int tmp, flag;
256 /* Read data from FIFO and push into terminal */
257 tmp = readl(LPC32XX_HSUART_FIFO(port->membase));
258 while (!(tmp & LPC32XX_HSU_RX_EMPTY)) {
259 flag = TTY_NORMAL;
260 port->icount.rx++;
262 if (tmp & LPC32XX_HSU_ERROR_DATA) {
263 /* Framing error */
264 writel(LPC32XX_HSU_FE_INT,
265 LPC32XX_HSUART_IIR(port->membase));
266 port->icount.frame++;
267 flag = TTY_FRAME;
268 tty_insert_flip_char(tport, 0, TTY_FRAME);
271 tty_insert_flip_char(tport, (tmp & 0xFF), flag);
273 tmp = readl(LPC32XX_HSUART_FIFO(port->membase));
276 spin_unlock(&port->lock);
277 tty_flip_buffer_push(tport);
278 spin_lock(&port->lock);
281 static void __serial_lpc32xx_tx(struct uart_port *port)
283 struct circ_buf *xmit = &port->state->xmit;
284 unsigned int tmp;
286 if (port->x_char) {
287 writel((u32)port->x_char, LPC32XX_HSUART_FIFO(port->membase));
288 port->icount.tx++;
289 port->x_char = 0;
290 return;
293 if (uart_circ_empty(xmit) || uart_tx_stopped(port))
294 goto exit_tx;
296 /* Transfer data */
297 while (LPC32XX_HSU_TX_LEV(readl(
298 LPC32XX_HSUART_LEVEL(port->membase))) < 64) {
299 writel((u32) xmit->buf[xmit->tail],
300 LPC32XX_HSUART_FIFO(port->membase));
301 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
302 port->icount.tx++;
303 if (uart_circ_empty(xmit))
304 break;
307 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
308 uart_write_wakeup(port);
310 exit_tx:
311 if (uart_circ_empty(xmit)) {
312 tmp = readl(LPC32XX_HSUART_CTRL(port->membase));
313 tmp &= ~LPC32XX_HSU_TX_INT_EN;
314 writel(tmp, LPC32XX_HSUART_CTRL(port->membase));
318 static irqreturn_t serial_lpc32xx_interrupt(int irq, void *dev_id)
320 struct uart_port *port = dev_id;
321 struct tty_port *tport = &port->state->port;
322 u32 status;
324 spin_lock(&port->lock);
326 /* Read UART status and clear latched interrupts */
327 status = readl(LPC32XX_HSUART_IIR(port->membase));
329 if (status & LPC32XX_HSU_BRK_INT) {
330 /* Break received */
331 writel(LPC32XX_HSU_BRK_INT, LPC32XX_HSUART_IIR(port->membase));
332 port->icount.brk++;
333 uart_handle_break(port);
336 /* Framing error */
337 if (status & LPC32XX_HSU_FE_INT)
338 writel(LPC32XX_HSU_FE_INT, LPC32XX_HSUART_IIR(port->membase));
340 if (status & LPC32XX_HSU_RX_OE_INT) {
341 /* Receive FIFO overrun */
342 writel(LPC32XX_HSU_RX_OE_INT,
343 LPC32XX_HSUART_IIR(port->membase));
344 port->icount.overrun++;
345 tty_insert_flip_char(tport, 0, TTY_OVERRUN);
346 tty_schedule_flip(tport);
349 /* Data received? */
350 if (status & (LPC32XX_HSU_RX_TIMEOUT_INT | LPC32XX_HSU_RX_TRIG_INT))
351 __serial_lpc32xx_rx(port);
353 /* Transmit data request? */
354 if ((status & LPC32XX_HSU_TX_INT) && (!uart_tx_stopped(port))) {
355 writel(LPC32XX_HSU_TX_INT, LPC32XX_HSUART_IIR(port->membase));
356 __serial_lpc32xx_tx(port);
359 spin_unlock(&port->lock);
361 return IRQ_HANDLED;
364 /* port->lock is not held. */
365 static unsigned int serial_lpc32xx_tx_empty(struct uart_port *port)
367 unsigned int ret = 0;
369 if (LPC32XX_HSU_TX_LEV(readl(LPC32XX_HSUART_LEVEL(port->membase))) == 0)
370 ret = TIOCSER_TEMT;
372 return ret;
375 /* port->lock held by caller. */
376 static void serial_lpc32xx_set_mctrl(struct uart_port *port,
377 unsigned int mctrl)
379 /* No signals are supported on HS UARTs */
382 /* port->lock is held by caller and interrupts are disabled. */
383 static unsigned int serial_lpc32xx_get_mctrl(struct uart_port *port)
385 /* No signals are supported on HS UARTs */
386 return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
389 /* port->lock held by caller. */
390 static void serial_lpc32xx_stop_tx(struct uart_port *port)
392 u32 tmp;
394 tmp = readl(LPC32XX_HSUART_CTRL(port->membase));
395 tmp &= ~LPC32XX_HSU_TX_INT_EN;
396 writel(tmp, LPC32XX_HSUART_CTRL(port->membase));
399 /* port->lock held by caller. */
400 static void serial_lpc32xx_start_tx(struct uart_port *port)
402 u32 tmp;
404 __serial_lpc32xx_tx(port);
405 tmp = readl(LPC32XX_HSUART_CTRL(port->membase));
406 tmp |= LPC32XX_HSU_TX_INT_EN;
407 writel(tmp, LPC32XX_HSUART_CTRL(port->membase));
410 /* port->lock held by caller. */
411 static void serial_lpc32xx_stop_rx(struct uart_port *port)
413 u32 tmp;
415 tmp = readl(LPC32XX_HSUART_CTRL(port->membase));
416 tmp &= ~(LPC32XX_HSU_RX_INT_EN | LPC32XX_HSU_ERR_INT_EN);
417 writel(tmp, LPC32XX_HSUART_CTRL(port->membase));
419 writel((LPC32XX_HSU_BRK_INT | LPC32XX_HSU_RX_OE_INT |
420 LPC32XX_HSU_FE_INT), LPC32XX_HSUART_IIR(port->membase));
423 /* port->lock is not held. */
424 static void serial_lpc32xx_break_ctl(struct uart_port *port,
425 int break_state)
427 unsigned long flags;
428 u32 tmp;
430 spin_lock_irqsave(&port->lock, flags);
431 tmp = readl(LPC32XX_HSUART_CTRL(port->membase));
432 if (break_state != 0)
433 tmp |= LPC32XX_HSU_BREAK;
434 else
435 tmp &= ~LPC32XX_HSU_BREAK;
436 writel(tmp, LPC32XX_HSUART_CTRL(port->membase));
437 spin_unlock_irqrestore(&port->lock, flags);
440 /* port->lock is not held. */
441 static int serial_lpc32xx_startup(struct uart_port *port)
443 int retval;
444 unsigned long flags;
445 u32 tmp;
447 spin_lock_irqsave(&port->lock, flags);
449 __serial_uart_flush(port);
451 writel((LPC32XX_HSU_TX_INT | LPC32XX_HSU_FE_INT |
452 LPC32XX_HSU_BRK_INT | LPC32XX_HSU_RX_OE_INT),
453 LPC32XX_HSUART_IIR(port->membase));
455 writel(0xFF, LPC32XX_HSUART_RATE(port->membase));
458 * Set receiver timeout, HSU offset of 20, no break, no interrupts,
459 * and default FIFO trigger levels
461 tmp = LPC32XX_HSU_TX_TL8B | LPC32XX_HSU_RX_TL32B |
462 LPC32XX_HSU_OFFSET(20) | LPC32XX_HSU_TMO_INACT_4B;
463 writel(tmp, LPC32XX_HSUART_CTRL(port->membase));
465 lpc32xx_loopback_set(port->mapbase, 0); /* get out of loopback mode */
467 spin_unlock_irqrestore(&port->lock, flags);
469 retval = request_irq(port->irq, serial_lpc32xx_interrupt,
470 0, MODNAME, port);
471 if (!retval)
472 writel((tmp | LPC32XX_HSU_RX_INT_EN | LPC32XX_HSU_ERR_INT_EN),
473 LPC32XX_HSUART_CTRL(port->membase));
475 return retval;
478 /* port->lock is not held. */
479 static void serial_lpc32xx_shutdown(struct uart_port *port)
481 u32 tmp;
482 unsigned long flags;
484 spin_lock_irqsave(&port->lock, flags);
486 tmp = LPC32XX_HSU_TX_TL8B | LPC32XX_HSU_RX_TL32B |
487 LPC32XX_HSU_OFFSET(20) | LPC32XX_HSU_TMO_INACT_4B;
488 writel(tmp, LPC32XX_HSUART_CTRL(port->membase));
490 lpc32xx_loopback_set(port->mapbase, 1); /* go to loopback mode */
492 spin_unlock_irqrestore(&port->lock, flags);
494 free_irq(port->irq, port);
497 /* port->lock is not held. */
498 static void serial_lpc32xx_set_termios(struct uart_port *port,
499 struct ktermios *termios,
500 struct ktermios *old)
502 unsigned long flags;
503 unsigned int baud, quot;
504 u32 tmp;
506 /* Always 8-bit, no parity, 1 stop bit */
507 termios->c_cflag &= ~(CSIZE | CSTOPB | PARENB | PARODD);
508 termios->c_cflag |= CS8;
510 termios->c_cflag &= ~(HUPCL | CMSPAR | CLOCAL | CRTSCTS);
512 baud = uart_get_baud_rate(port, termios, old, 0,
513 port->uartclk / 14);
515 quot = __serial_get_clock_div(port->uartclk, baud);
517 spin_lock_irqsave(&port->lock, flags);
519 /* Ignore characters? */
520 tmp = readl(LPC32XX_HSUART_CTRL(port->membase));
521 if ((termios->c_cflag & CREAD) == 0)
522 tmp &= ~(LPC32XX_HSU_RX_INT_EN | LPC32XX_HSU_ERR_INT_EN);
523 else
524 tmp |= LPC32XX_HSU_RX_INT_EN | LPC32XX_HSU_ERR_INT_EN;
525 writel(tmp, LPC32XX_HSUART_CTRL(port->membase));
527 writel(quot, LPC32XX_HSUART_RATE(port->membase));
529 uart_update_timeout(port, termios->c_cflag, baud);
531 spin_unlock_irqrestore(&port->lock, flags);
533 /* Don't rewrite B0 */
534 if (tty_termios_baud_rate(termios))
535 tty_termios_encode_baud_rate(termios, baud, baud);
538 static const char *serial_lpc32xx_type(struct uart_port *port)
540 return MODNAME;
543 static void serial_lpc32xx_release_port(struct uart_port *port)
545 if ((port->iotype == UPIO_MEM32) && (port->mapbase)) {
546 if (port->flags & UPF_IOREMAP) {
547 iounmap(port->membase);
548 port->membase = NULL;
551 release_mem_region(port->mapbase, SZ_4K);
555 static int serial_lpc32xx_request_port(struct uart_port *port)
557 int ret = -ENODEV;
559 if ((port->iotype == UPIO_MEM32) && (port->mapbase)) {
560 ret = 0;
562 if (!request_mem_region(port->mapbase, SZ_4K, MODNAME))
563 ret = -EBUSY;
564 else if (port->flags & UPF_IOREMAP) {
565 port->membase = ioremap(port->mapbase, SZ_4K);
566 if (!port->membase) {
567 release_mem_region(port->mapbase, SZ_4K);
568 ret = -ENOMEM;
573 return ret;
576 static void serial_lpc32xx_config_port(struct uart_port *port, int uflags)
578 int ret;
580 ret = serial_lpc32xx_request_port(port);
581 if (ret < 0)
582 return;
583 port->type = PORT_UART00;
584 port->fifosize = 64;
586 __serial_uart_flush(port);
588 writel((LPC32XX_HSU_TX_INT | LPC32XX_HSU_FE_INT |
589 LPC32XX_HSU_BRK_INT | LPC32XX_HSU_RX_OE_INT),
590 LPC32XX_HSUART_IIR(port->membase));
592 writel(0xFF, LPC32XX_HSUART_RATE(port->membase));
594 /* Set receiver timeout, HSU offset of 20, no break, no interrupts,
595 and default FIFO trigger levels */
596 writel(LPC32XX_HSU_TX_TL8B | LPC32XX_HSU_RX_TL32B |
597 LPC32XX_HSU_OFFSET(20) | LPC32XX_HSU_TMO_INACT_4B,
598 LPC32XX_HSUART_CTRL(port->membase));
601 static int serial_lpc32xx_verify_port(struct uart_port *port,
602 struct serial_struct *ser)
604 int ret = 0;
606 if (ser->type != PORT_UART00)
607 ret = -EINVAL;
609 return ret;
612 static const struct uart_ops serial_lpc32xx_pops = {
613 .tx_empty = serial_lpc32xx_tx_empty,
614 .set_mctrl = serial_lpc32xx_set_mctrl,
615 .get_mctrl = serial_lpc32xx_get_mctrl,
616 .stop_tx = serial_lpc32xx_stop_tx,
617 .start_tx = serial_lpc32xx_start_tx,
618 .stop_rx = serial_lpc32xx_stop_rx,
619 .break_ctl = serial_lpc32xx_break_ctl,
620 .startup = serial_lpc32xx_startup,
621 .shutdown = serial_lpc32xx_shutdown,
622 .set_termios = serial_lpc32xx_set_termios,
623 .type = serial_lpc32xx_type,
624 .release_port = serial_lpc32xx_release_port,
625 .request_port = serial_lpc32xx_request_port,
626 .config_port = serial_lpc32xx_config_port,
627 .verify_port = serial_lpc32xx_verify_port,
631 * Register a set of serial devices attached to a platform device
633 static int serial_hs_lpc32xx_probe(struct platform_device *pdev)
635 struct lpc32xx_hsuart_port *p = &lpc32xx_hs_ports[uarts_registered];
636 int ret = 0;
637 struct resource *res;
639 if (uarts_registered >= MAX_PORTS) {
640 dev_err(&pdev->dev,
641 "Error: Number of possible ports exceeded (%d)!\n",
642 uarts_registered + 1);
643 return -ENXIO;
646 memset(p, 0, sizeof(*p));
648 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
649 if (!res) {
650 dev_err(&pdev->dev,
651 "Error getting mem resource for HS UART port %d\n",
652 uarts_registered);
653 return -ENXIO;
655 p->port.mapbase = res->start;
656 p->port.membase = NULL;
658 ret = platform_get_irq(pdev, 0);
659 if (ret < 0)
660 return ret;
661 p->port.irq = ret;
663 p->port.iotype = UPIO_MEM32;
664 p->port.uartclk = LPC32XX_MAIN_OSC_FREQ;
665 p->port.regshift = 2;
666 p->port.flags = UPF_BOOT_AUTOCONF | UPF_FIXED_PORT | UPF_IOREMAP;
667 p->port.dev = &pdev->dev;
668 p->port.ops = &serial_lpc32xx_pops;
669 p->port.line = uarts_registered++;
670 spin_lock_init(&p->port.lock);
672 /* send port to loopback mode by default */
673 lpc32xx_loopback_set(p->port.mapbase, 1);
675 ret = uart_add_one_port(&lpc32xx_hs_reg, &p->port);
677 platform_set_drvdata(pdev, p);
679 return ret;
683 * Remove serial ports registered against a platform device.
685 static int serial_hs_lpc32xx_remove(struct platform_device *pdev)
687 struct lpc32xx_hsuart_port *p = platform_get_drvdata(pdev);
689 uart_remove_one_port(&lpc32xx_hs_reg, &p->port);
691 return 0;
695 #ifdef CONFIG_PM
696 static int serial_hs_lpc32xx_suspend(struct platform_device *pdev,
697 pm_message_t state)
699 struct lpc32xx_hsuart_port *p = platform_get_drvdata(pdev);
701 uart_suspend_port(&lpc32xx_hs_reg, &p->port);
703 return 0;
706 static int serial_hs_lpc32xx_resume(struct platform_device *pdev)
708 struct lpc32xx_hsuart_port *p = platform_get_drvdata(pdev);
710 uart_resume_port(&lpc32xx_hs_reg, &p->port);
712 return 0;
714 #else
715 #define serial_hs_lpc32xx_suspend NULL
716 #define serial_hs_lpc32xx_resume NULL
717 #endif
719 static const struct of_device_id serial_hs_lpc32xx_dt_ids[] = {
720 { .compatible = "nxp,lpc3220-hsuart" },
721 { /* sentinel */ }
724 MODULE_DEVICE_TABLE(of, serial_hs_lpc32xx_dt_ids);
726 static struct platform_driver serial_hs_lpc32xx_driver = {
727 .probe = serial_hs_lpc32xx_probe,
728 .remove = serial_hs_lpc32xx_remove,
729 .suspend = serial_hs_lpc32xx_suspend,
730 .resume = serial_hs_lpc32xx_resume,
731 .driver = {
732 .name = MODNAME,
733 .of_match_table = serial_hs_lpc32xx_dt_ids,
737 static int __init lpc32xx_hsuart_init(void)
739 int ret;
741 ret = uart_register_driver(&lpc32xx_hs_reg);
742 if (ret)
743 return ret;
745 ret = platform_driver_register(&serial_hs_lpc32xx_driver);
746 if (ret)
747 uart_unregister_driver(&lpc32xx_hs_reg);
749 return ret;
752 static void __exit lpc32xx_hsuart_exit(void)
754 platform_driver_unregister(&serial_hs_lpc32xx_driver);
755 uart_unregister_driver(&lpc32xx_hs_reg);
758 module_init(lpc32xx_hsuart_init);
759 module_exit(lpc32xx_hsuart_exit);
761 MODULE_AUTHOR("Kevin Wells <kevin.wells@nxp.com>");
762 MODULE_AUTHOR("Roland Stigge <stigge@antcom.de>");
763 MODULE_DESCRIPTION("NXP LPC32XX High Speed UART driver");
764 MODULE_LICENSE("GPL");