21 #include <linux/module.h>
26 #include <linux/tty.h>
28 #include <linux/serial_core.h>
29 #include <linux/serial.h>
37 #include <mach/platform.h>
38 #include <mach/hardware.h>
43 #define LPC32XX_HSUART_FIFO(x) ((x) + 0x00)
44 #define LPC32XX_HSUART_LEVEL(x) ((x) + 0x04)
45 #define LPC32XX_HSUART_IIR(x) ((x) + 0x08)
46 #define LPC32XX_HSUART_CTRL(x) ((x) + 0x0C)
47 #define LPC32XX_HSUART_RATE(x) ((x) + 0x10)
49 #define LPC32XX_HSU_BREAK_DATA (1 << 10)
50 #define LPC32XX_HSU_ERROR_DATA (1 << 9)
51 #define LPC32XX_HSU_RX_EMPTY (1 << 8)
53 #define LPC32XX_HSU_TX_LEV(n) (((n) >> 8) & 0xFF)
54 #define LPC32XX_HSU_RX_LEV(n) ((n) & 0xFF)
56 #define LPC32XX_HSU_TX_INT_SET (1 << 6)
57 #define LPC32XX_HSU_RX_OE_INT (1 << 5)
58 #define LPC32XX_HSU_BRK_INT (1 << 4)
59 #define LPC32XX_HSU_FE_INT (1 << 3)
60 #define LPC32XX_HSU_RX_TIMEOUT_INT (1 << 2)
61 #define LPC32XX_HSU_RX_TRIG_INT (1 << 1)
62 #define LPC32XX_HSU_TX_INT (1 << 0)
64 #define LPC32XX_HSU_HRTS_INV (1 << 21)
65 #define LPC32XX_HSU_HRTS_TRIG_8B (0x0 << 19)
66 #define LPC32XX_HSU_HRTS_TRIG_16B (0x1 << 19)
67 #define LPC32XX_HSU_HRTS_TRIG_32B (0x2 << 19)
68 #define LPC32XX_HSU_HRTS_TRIG_48B (0x3 << 19)
69 #define LPC32XX_HSU_HRTS_EN (1 << 18)
70 #define LPC32XX_HSU_TMO_DISABLED (0x0 << 16)
71 #define LPC32XX_HSU_TMO_INACT_4B (0x1 << 16)
72 #define LPC32XX_HSU_TMO_INACT_8B (0x2 << 16)
73 #define LPC32XX_HSU_TMO_INACT_16B (0x3 << 16)
74 #define LPC32XX_HSU_HCTS_INV (1 << 15)
75 #define LPC32XX_HSU_HCTS_EN (1 << 14)
76 #define LPC32XX_HSU_OFFSET(n) ((n) << 9)
77 #define LPC32XX_HSU_BREAK (1 << 8)
78 #define LPC32XX_HSU_ERR_INT_EN (1 << 7)
79 #define LPC32XX_HSU_RX_INT_EN (1 << 6)
80 #define LPC32XX_HSU_TX_INT_EN (1 << 5)
81 #define LPC32XX_HSU_RX_TL1B (0x0 << 2)
82 #define LPC32XX_HSU_RX_TL4B (0x1 << 2)
83 #define LPC32XX_HSU_RX_TL8B (0x2 << 2)
84 #define LPC32XX_HSU_RX_TL16B (0x3 << 2)
85 #define LPC32XX_HSU_RX_TL32B (0x4 << 2)
86 #define LPC32XX_HSU_RX_TL48B (0x5 << 2)
87 #define LPC32XX_HSU_TX_TLEMPTY (0x0 << 0)
88 #define LPC32XX_HSU_TX_TL0B (0x0 << 0)
89 #define LPC32XX_HSU_TX_TL4B (0x1 << 0)
90 #define LPC32XX_HSU_TX_TL8B (0x2 << 0)
91 #define LPC32XX_HSU_TX_TL16B (0x3 << 0)
93 #define MODNAME "lpc32xx_hsuart"
99 #define FIFO_READ_LIMIT 128
101 #define LPC32XX_TTY_NAME "ttyTX"
104 #ifdef CONFIG_SERIAL_HS_LPC32XX_CONSOLE
119 static void wait_for_xmit_ready(
struct uart_port *port)
121 unsigned int timeout = 10000;
133 static void lpc32xx_hsuart_console_putchar(
struct uart_port *port,
int ch)
135 wait_for_xmit_ready(port);
139 static void lpc32xx_hsuart_console_write(
struct console *co,
const char *
s,
151 locked = spin_trylock(&up->
port.lock);
153 spin_lock(&up->
port.lock);
156 wait_for_xmit_empty(&up->
port);
159 spin_unlock(&up->
port.lock);
163 static int __init lpc32xx_hsuart_console_setup(
struct console *co,
175 port = &lpc32xx_hs_ports[co->
index].port;
186 static struct console lpc32xx_hsuart_console = {
188 .write = lpc32xx_hsuart_console_write,
190 .setup = lpc32xx_hsuart_console_setup,
193 .data = &lpc32xx_hsuart_reg,
196 static int __init lpc32xx_hsuart_console_init(
void)
203 #define LPC32XX_HSUART_CONSOLE (&lpc32xx_hsuart_console)
205 #define LPC32XX_HSUART_CONSOLE NULL
215 static int uarts_registered;
217 static unsigned int __serial_get_clock_div(
unsigned long uartclk,
220 u32 div, goodrate, hsu_rate, l_hsu_rate, comprate;
224 div = uartclk /
rate;
225 goodrate = hsu_rate = (div / 14) - 1;
230 l_hsu_rate = hsu_rate + 3;
231 rate_diff = 0xFFFFFFFF;
233 while (hsu_rate < l_hsu_rate) {
234 comprate = uartclk / ((hsu_rate + 1) * 14);
235 if (
abs(comprate - rate) < rate_diff) {
237 rate_diff =
abs(comprate - rate);
248 static void __serial_uart_flush(
struct uart_port *port)
258 static void __serial_lpc32xx_rx(
struct uart_port *port)
287 tty_insert_flip_char(tty, (tmp & 0xFF), flag);
295 static void __serial_lpc32xx_tx(
struct uart_port *port)
338 spin_lock(&port->
lock);
347 uart_handle_break(port);
356 writel(LPC32XX_HSU_RX_OE_INT,
367 __serial_lpc32xx_rx(port);
375 __serial_lpc32xx_tx(port);
378 spin_unlock(&port->
lock);
385 static unsigned int serial_lpc32xx_tx_empty(
struct uart_port *port)
387 unsigned int ret = 0;
396 static void serial_lpc32xx_set_mctrl(
struct uart_port *port,
403 static unsigned int serial_lpc32xx_get_mctrl(
struct uart_port *port)
410 static void serial_lpc32xx_stop_tx(
struct uart_port *port)
420 static void serial_lpc32xx_start_tx(
struct uart_port *port)
424 __serial_lpc32xx_tx(port);
431 static void serial_lpc32xx_stop_rx(
struct uart_port *port)
439 writel((LPC32XX_HSU_BRK_INT | LPC32XX_HSU_RX_OE_INT |
444 static void serial_lpc32xx_enable_ms(
struct uart_port *port)
450 static void serial_lpc32xx_break_ctl(
struct uart_port *port,
458 if (break_state != 0)
463 spin_unlock_irqrestore(&port->
lock, flags);
483 WARN(1,
"lpc32xx_hs: Warning: Unknown port at %08x\n", mapbase);
496 static int serial_lpc32xx_startup(
struct uart_port *port)
504 __serial_uart_flush(port);
506 writel((LPC32XX_HSU_TX_INT | LPC32XX_HSU_FE_INT |
507 LPC32XX_HSU_BRK_INT | LPC32XX_HSU_RX_OE_INT),
520 lpc32xx_loopback_set(port->
mapbase, 0);
522 spin_unlock_irqrestore(&port->
lock, flags);
534 static void serial_lpc32xx_shutdown(
struct uart_port *port)
545 lpc32xx_loopback_set(port->
mapbase, 1);
547 spin_unlock_irqrestore(&port->
lock, flags);
553 static void serial_lpc32xx_set_termios(
struct uart_port *port,
558 unsigned int baud, quot;
570 quot = __serial_get_clock_div(port->
uartclk, baud);
586 spin_unlock_irqrestore(&port->
lock, flags);
593 static const char *serial_lpc32xx_type(
struct uart_port *port)
598 static void serial_lpc32xx_release_port(
struct uart_port *port)
610 static int serial_lpc32xx_request_port(
struct uart_port *port)
631 static void serial_lpc32xx_config_port(
struct uart_port *port,
int uflags)
635 ret = serial_lpc32xx_request_port(port);
641 __serial_uart_flush(port);
643 writel((LPC32XX_HSU_TX_INT | LPC32XX_HSU_FE_INT |
644 LPC32XX_HSU_BRK_INT | LPC32XX_HSU_RX_OE_INT),
656 static int serial_lpc32xx_verify_port(
struct uart_port *port,
667 static struct uart_ops serial_lpc32xx_pops = {
668 .tx_empty = serial_lpc32xx_tx_empty,
669 .set_mctrl = serial_lpc32xx_set_mctrl,
670 .get_mctrl = serial_lpc32xx_get_mctrl,
671 .stop_tx = serial_lpc32xx_stop_tx,
672 .start_tx = serial_lpc32xx_start_tx,
673 .stop_rx = serial_lpc32xx_stop_rx,
674 .enable_ms = serial_lpc32xx_enable_ms,
675 .break_ctl = serial_lpc32xx_break_ctl,
676 .startup = serial_lpc32xx_startup,
677 .shutdown = serial_lpc32xx_shutdown,
678 .set_termios = serial_lpc32xx_set_termios,
679 .type = serial_lpc32xx_type,
680 .release_port = serial_lpc32xx_release_port,
681 .request_port = serial_lpc32xx_request_port,
682 .config_port = serial_lpc32xx_config_port,
683 .verify_port = serial_lpc32xx_verify_port,
697 "Error: Number of possible ports exceeded (%d)!\n",
698 uarts_registered + 1);
707 "Error getting mem resource for HS UART port %d\n",
715 if (p->
port.irq < 0) {
716 dev_err(&pdev->
dev,
"Error getting irq for HS UART port %d\n",
723 p->
port.regshift = 2;
726 p->
port.ops = &serial_lpc32xx_pops;
727 p->
port.line = uarts_registered++;
731 lpc32xx_loopback_set(p->
port.mapbase, 1);
735 platform_set_drvdata(pdev, p);
773 #define serial_hs_lpc32xx_suspend NULL
774 #define serial_hs_lpc32xx_resume NULL
777 static const struct of_device_id serial_hs_lpc32xx_dt_ids[] = {
778 { .compatible =
"nxp,lpc3220-hsuart" },
785 .probe = serial_hs_lpc32xx_probe,
792 .of_match_table = serial_hs_lpc32xx_dt_ids,
796 static int __init lpc32xx_hsuart_init(
void)
811 static void __exit lpc32xx_hsuart_exit(
void)