1 #if defined(CONFIG_SERIAL_EFM32_UART_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
5 #include <linux/kernel.h>
6 #include <linux/module.h>
11 #include <linux/serial_core.h>
13 #include <linux/slab.h>
20 #define DRIVER_NAME "efm32-uart"
21 #define DEV_NAME "ttyefm"
23 #define UARTn_CTRL 0x00
24 #define UARTn_CTRL_SYNC 0x0001
25 #define UARTn_CTRL_TXBIL 0x1000
27 #define UARTn_FRAME 0x04
28 #define UARTn_FRAME_DATABITS__MASK 0x000f
29 #define UARTn_FRAME_DATABITS(n) ((n) - 3)
30 #define UARTn_FRAME_PARITY_NONE 0x0000
31 #define UARTn_FRAME_PARITY_EVEN 0x0200
32 #define UARTn_FRAME_PARITY_ODD 0x0300
33 #define UARTn_FRAME_STOPBITS_HALF 0x0000
34 #define UARTn_FRAME_STOPBITS_ONE 0x1000
35 #define UARTn_FRAME_STOPBITS_TWO 0x3000
37 #define UARTn_CMD 0x0c
38 #define UARTn_CMD_RXEN 0x0001
39 #define UARTn_CMD_RXDIS 0x0002
40 #define UARTn_CMD_TXEN 0x0004
41 #define UARTn_CMD_TXDIS 0x0008
43 #define UARTn_STATUS 0x10
44 #define UARTn_STATUS_TXENS 0x0002
45 #define UARTn_STATUS_TXC 0x0020
46 #define UARTn_STATUS_TXBL 0x0040
47 #define UARTn_STATUS_RXDATAV 0x0080
49 #define UARTn_CLKDIV 0x14
51 #define UARTn_RXDATAX 0x18
52 #define UARTn_RXDATAX_RXDATA__MASK 0x01ff
53 #define UARTn_RXDATAX_PERR 0x4000
54 #define UARTn_RXDATAX_FERR 0x8000
60 #define SW_UARTn_RXDATAX_BERR 0x2000
62 #define UARTn_TXDATA 0x34
65 #define UARTn_IF_TXC 0x0001
66 #define UARTn_IF_TXBL 0x0002
67 #define UARTn_IF_RXDATAV 0x0004
68 #define UARTn_IF_RXOF 0x0010
70 #define UARTn_IFS 0x44
71 #define UARTn_IFC 0x48
72 #define UARTn_IEN 0x4c
74 #define UARTn_ROUTE 0x54
75 #define UARTn_ROUTE_LOCATION__MASK 0x0700
76 #define UARTn_ROUTE_LOCATION(n) (((n) << 8) & UARTn_ROUTE_LOCATION__MASK)
77 #define UARTn_ROUTE_RXPEN 0x0001
78 #define UARTn_ROUTE_TXPEN 0x0002
85 #define to_efm_port(_port) container_of(_port, struct efm32_uart_port, port)
86 #define efm_debug(efm_port, format, arg...) \
87 dev_dbg(efm_port->port.dev, format, ##arg)
92 writel_relaxed(value, efm_port->
port.membase + offset);
101 static unsigned int efm32_uart_tx_empty(
struct uart_port *
port)
112 static void efm32_uart_set_mctrl(
struct uart_port *port,
unsigned int mctrl)
117 static unsigned int efm32_uart_get_mctrl(
struct uart_port *port)
123 static void efm32_uart_stop_tx(
struct uart_port *port)
130 efm32_uart_write32(efm_port, ien,
UARTn_IEN);
142 efm32_uart_write32(efm_port, port->
x_char,
149 efm32_uart_write32(efm_port, xmit->
buf[xmit->
tail],
162 efm32_uart_stop_tx(port);
165 static void efm32_uart_start_tx(
struct uart_port *port)
170 efm32_uart_write32(efm_port,
172 ien = efm32_uart_read32(efm_port,
UARTn_IEN);
173 efm32_uart_write32(efm_port,
177 efm32_uart_tx_chars(efm_port);
180 static void efm32_uart_stop_rx(
struct uart_port *port)
187 static void efm32_uart_enable_ms(
struct uart_port *port)
192 static void efm32_uart_break_ctl(
struct uart_port *port,
int ctl)
221 if (uart_handle_break(port))
225 else if (rxdata & UARTn_RXDATAX_FERR)
232 else if (rxdata & UARTn_RXDATAX_PERR)
234 else if (rxdata & UARTn_RXDATAX_FERR)
237 rxdata & UARTn_RXDATAX_RXDATA__MASK))
241 tty_insert_flip_char(tty,
242 rxdata & UARTn_RXDATAX_RXDATA__MASK, flag);
249 u32 irqflag = efm32_uart_read32(efm_port,
UARTn_IF);
254 spin_lock(&port->
lock);
256 tty = tty_kref_get(port->
state->port.tty);
259 efm32_uart_write32(efm_port, UARTn_IF_RXDATAV,
UARTn_IFC);
260 efm32_uart_rx_chars(efm_port, tty);
266 efm32_uart_write32(efm_port, UARTn_IF_RXOF,
UARTn_IFC);
279 spin_unlock(&port->
lock);
284 static irqreturn_t efm32_uart_txirq(
int irq,
void *data)
287 u32 irqflag = efm32_uart_read32(efm_port,
UARTn_IF);
291 efm32_uart_write32(efm_port, UARTn_IF_TXC,
UARTn_IFC);
294 efm32_uart_tx_chars(efm_port);
300 static int efm32_uart_startup(
struct uart_port *port)
312 efm_debug(efm_port,
"failed to enable clk\n");
324 efm_debug(efm_port,
"failed to register rxirq\n");
325 goto err_request_irq_rx;
329 efm32_uart_write32(efm_port, 0,
UARTn_IEN);
334 efm_debug(efm_port,
"failed to register txirq\n");
340 efm32_uart_write32(efm_port,
341 UARTn_IF_RXDATAV | UARTn_IF_RXOF,
UARTn_IEN);
349 static void efm32_uart_shutdown(
struct uart_port *port)
353 efm32_uart_write32(efm_port, 0,
UARTn_IEN);
359 static void efm32_uart_set_termios(
struct uart_port *port,
375 switch (new->c_cflag &
CSIZE) {
390 if (new->c_cflag &
CSTOPB)
396 if (new->c_cflag &
PARENB) {
397 if (new->c_cflag &
PARODD)
412 efm32_uart_write32(efm_port,
416 if (new->c_iflag &
INPCK)
423 if (new->c_iflag &
IGNPAR)
426 if (new->c_iflag &
IGNBRK)
438 spin_unlock_irqrestore(&port->
lock, flags);
441 static const char *efm32_uart_type(
struct uart_port *port)
446 static void efm32_uart_release_port(
struct uart_port *port)
455 static int efm32_uart_request_port(
struct uart_port *port)
461 if (!efm_port->
port.membase) {
463 efm_debug(efm_port,
"failed to remap\n");
468 if (IS_ERR(efm_port->
clk)) {
469 ret = PTR_ERR(efm_port->
clk);
470 efm_debug(efm_port,
"failed to get clock\n");
486 static void efm32_uart_config_port(
struct uart_port *port,
int type)
489 !efm32_uart_request_port(port))
493 static int efm32_uart_verify_port(
struct uart_port *port,
504 static struct uart_ops efm32_uart_pops = {
505 .tx_empty = efm32_uart_tx_empty,
506 .set_mctrl = efm32_uart_set_mctrl,
507 .get_mctrl = efm32_uart_get_mctrl,
508 .stop_tx = efm32_uart_stop_tx,
509 .start_tx = efm32_uart_start_tx,
510 .stop_rx = efm32_uart_stop_rx,
511 .enable_ms = efm32_uart_enable_ms,
512 .break_ctl = efm32_uart_break_ctl,
513 .startup = efm32_uart_startup,
514 .shutdown = efm32_uart_shutdown,
515 .set_termios = efm32_uart_set_termios,
516 .type = efm32_uart_type,
517 .release_port = efm32_uart_release_port,
518 .request_port = efm32_uart_request_port,
519 .config_port = efm32_uart_config_port,
520 .verify_port = efm32_uart_verify_port,
525 #ifdef CONFIG_SERIAL_EFM32_UART_CONSOLE
526 static void efm32_uart_console_putchar(
struct uart_port *port,
int ch)
535 if (status & UARTn_STATUS_TXBL)
543 static void efm32_uart_console_write(
struct console *co,
const char *
s,
548 unsigned int timeout = 0x400;
554 efm32_uart_console_putchar);
565 if (!(status & UARTn_STATUS_TXENS))
569 static void efm32_uart_console_get_options(
struct efm32_uart_port *efm_port,
570 int *baud,
int *parity,
int *
bits)
587 16 * (4 + (clkdiv >> 6)));
600 efm_debug(efm_port,
"get_opts: options=%d%c%d\n",
601 *baud, *parity, *bits);
604 static int efm32_uart_console_setup(
struct console *co,
char *
options)
615 for (i = 0; i <
ARRAY_SIZE(efm32_uart_ports); ++
i) {
616 if (efm32_uart_ports[i]) {
617 pr_warn(
"efm32-console: fall back to console index %u (from %hhi)\n",
625 efm_port = efm32_uart_ports[co->
index];
634 "console: clk_prepare failed: %d\n", ret);
643 efm32_uart_console_get_options(efm_port,
644 &baud, &parity, &bits);
653 .write = efm32_uart_console_write,
655 .setup = efm32_uart_console_setup,
658 .data = &efm32_uart_reg,
662 #define efm32_uart_console (*(struct console *)NULL)
684 dev_err(&pdev->
dev,
"failed to get alias id: %d\n", ret);
699 efm_port = kzalloc(
sizeof(*efm_port),
GFP_KERNEL);
701 dev_dbg(&pdev->
dev,
"failed to allocate private data\n");
708 dev_dbg(&pdev->
dev,
"failed to determine base address\n");
712 if (resource_size(res) < 60) {
714 dev_dbg(&pdev->
dev,
"memory resource too small\n");
720 dev_dbg(&pdev->
dev,
"failed to get rx irq\n");
728 ret = efm_port->
port.irq + 1;
732 efm_port->
port.dev = &pdev->
dev;
736 efm_port->
port.fifosize = 2;
737 efm_port->
port.ops = &efm32_uart_pops;
740 ret = efm32_uart_probe_dt(pdev, efm_port);
743 efm_port->
port.line = pdev->
id;
745 if (efm_port->
port.line >= 0 &&
747 efm32_uart_ports[efm_port->
port.line] = efm_port;
751 dev_dbg(&pdev->
dev,
"failed to add port: %d\n", ret);
754 efm32_uart_ports[pdev->
id] =
NULL;
760 platform_set_drvdata(pdev, efm_port);
771 platform_set_drvdata(pdev,
NULL);
776 efm32_uart_ports[pdev->
id] =
NULL;
785 .compatible =
"efm32,uart",
793 .probe = efm32_uart_probe,
799 .of_match_table = efm32_uart_dt_ids,
803 static int __init efm32_uart_init(
void)
815 pr_info(
"EFM32 UART/USART driver\n");
821 static void __exit efm32_uart_exit(
void)