9 #include <linux/module.h>
15 #include <linux/tty.h>
17 #include <linux/serial_core.h>
18 #include <linux/serial.h>
21 #include <linux/slab.h>
32 sirfsoc_uart_pio_rx_chars(
struct uart_port *
port,
unsigned int max_rx_count);
85 static inline unsigned int sirfsoc_uart_tx_empty(
struct uart_port *
port)
95 static unsigned int sirfsoc_uart_get_mctrl(
struct uart_port *port)
113 static void sirfsoc_uart_set_mctrl(
struct uart_port *port,
unsigned int mctrl)
118 unsigned int current_val;
126 static void sirfsoc_uart_stop_tx(
struct uart_port *port)
137 sirfsoc_uart_pio_tx_chars(sirfport, 1);
143 static void sirfsoc_uart_stop_rx(
struct uart_port *port)
151 static void sirfsoc_uart_disable_ms(
struct uart_port *port)
164 static void sirfsoc_uart_enable_ms(
struct uart_port *port)
181 static void sirfsoc_uart_break_ctl(
struct uart_port *port,
int break_state)
192 sirfsoc_uart_pio_rx_chars(
struct uart_port *port,
unsigned int max_rx_count)
194 unsigned int ch, rx_count = 0;
208 if (rx_count >= max_rx_count)
212 port->
icount.rx += rx_count;
224 unsigned int num_tx = 0;
241 unsigned long intr_status;
242 unsigned long cts_status;
253 if (uart_handle_break(port))
278 if (cts_status != 0) {
291 sirfsoc_uart_pio_tx_chars(sirfport,
296 sirfsoc_uart_stop_tx(port);
302 static void sirfsoc_uart_start_rx(
struct uart_port *port)
313 sirfsoc_calc_sample_div(
unsigned long baud_rate,
314 unsigned long ioclk_rate,
unsigned long *setted_baud)
316 unsigned long min_delta = ~0
UL;
317 unsigned short sample_div;
318 unsigned int regv = 0;
319 unsigned long ioclk_div;
320 unsigned long baud_tmp;
325 ioclk_div = (ioclk_rate / (baud_rate * (sample_div + 1))) - 1;
328 baud_tmp = ioclk_rate / ((ioclk_div + 1) * (sample_div + 1));
329 temp_delta = baud_tmp - baud_rate;
330 temp_delta = (temp_delta > 0) ? temp_delta : -temp_delta;
331 if (temp_delta < min_delta) {
333 regv = regv | ioclk_div;
336 min_delta = temp_delta;
337 *setted_baud = baud_tmp;
343 static void sirfsoc_uart_set_termios(
struct uart_port *port,
348 unsigned long ioclk_rate;
349 unsigned long config_reg = 0;
350 unsigned long baud_rate;
351 unsigned long setted_baud;
354 unsigned int clk_div_reg = 0;
355 unsigned long temp_reg_val;
356 unsigned long rx_time_out;
360 ioclk_rate = 150000000;
410 sirfsoc_uart_enable_ms(port);
413 sirfsoc_uart_disable_ms(port);
418 if (baud_rate == baudrate_to_regv[ic].baud_rate)
419 clk_div_reg = baudrate_to_regv[ic].
reg_val;
420 setted_baud = baud_rate;
423 clk_div_reg = sirfsoc_calc_sample_div(baud_rate, ioclk_rate,
432 rx_time_out = (rx_time_out > 0xFFFF) ? 0xFFFF : rx_time_out;
443 if (baud_rate < 1000000)
447 temp = port->
line == 1 ? 16 : 64;
453 sirfsoc_uart_start_rx(port);
455 spin_unlock_irqrestore(&port->
lock, flags);
458 static void startup_uart_controller(
struct uart_port *port)
460 unsigned long temp_regv;
473 temp = port->
line == 1 ? 16 : 64;
478 static int sirfsoc_uart_startup(
struct uart_port *port)
490 dev_err(port->
dev,
"UART%d request IRQ line (%d) failed.\n",
494 startup_uart_controller(port);
500 static void sirfsoc_uart_shutdown(
struct uart_port *port)
506 sirfsoc_uart_disable_ms(port);
511 static const char *sirfsoc_uart_type(
struct uart_port *port)
516 static int sirfsoc_uart_request_port(
struct uart_port *port)
521 return ret ? 0 : -
EBUSY;
524 static void sirfsoc_uart_release_port(
struct uart_port *port)
529 static void sirfsoc_uart_config_port(
struct uart_port *port,
int flags)
533 sirfsoc_uart_request_port(port);
537 static struct uart_ops sirfsoc_uart_ops = {
538 .tx_empty = sirfsoc_uart_tx_empty,
539 .get_mctrl = sirfsoc_uart_get_mctrl,
540 .set_mctrl = sirfsoc_uart_set_mctrl,
541 .stop_tx = sirfsoc_uart_stop_tx,
543 .stop_rx = sirfsoc_uart_stop_rx,
544 .enable_ms = sirfsoc_uart_enable_ms,
545 .break_ctl = sirfsoc_uart_break_ctl,
546 .startup = sirfsoc_uart_startup,
547 .shutdown = sirfsoc_uart_shutdown,
548 .set_termios = sirfsoc_uart_set_termios,
549 .type = sirfsoc_uart_type,
550 .release_port = sirfsoc_uart_release_port,
551 .request_port = sirfsoc_uart_request_port,
552 .config_port = sirfsoc_uart_config_port,
555 #ifdef CONFIG_SERIAL_SIRFSOC_CONSOLE
558 unsigned int baud = 115200;
559 unsigned int bits = 8;
560 unsigned int parity =
'n';
561 unsigned int flow =
'n';
576 static void sirfsoc_uart_console_putchar(
struct uart_port *port,
int ch)
584 static void sirfsoc_uart_console_write(
struct console *co,
const char *
s,
591 static struct console sirfsoc_uart_console = {
596 .write = sirfsoc_uart_console_write,
597 .setup = sirfsoc_uart_console_setup,
598 .data = &sirfsoc_uart_drv,
601 static int __init sirfsoc_uart_console_init(
void)
616 #ifdef CONFIG_SERIAL_SIRFSOC_CONSOLE
617 .cons = &sirfsoc_uart_console,
630 if (of_property_read_u32(pdev->
dev.of_node,
"cell-index", &pdev->
id)) {
632 "Unable to find cell-index in uart node.\n");
637 sirfport = &sirfsoc_uart_ports[pdev->
id];
638 port = &sirfport->
port;
645 if (of_property_read_u32(pdev->
dev.of_node,
649 "Unable to find fifosize in uart node.\n");
656 dev_err(&pdev->
dev,
"Insufficient resources.\n");
663 dev_err(&pdev->
dev,
"Cannot remap resource.\n");
669 dev_err(&pdev->
dev,
"Insufficient resources.\n");
676 sirfport->
p = pinctrl_get_select_default(&pdev->
dev);
677 ret = IS_ERR(sirfport->
p);
682 port->
ops = &sirfsoc_uart_ops;
685 platform_set_drvdata(pdev, sirfport);
688 dev_err(&pdev->
dev,
"Cannot add UART port(%d).\n", pdev->
id);
695 platform_set_drvdata(pdev,
NULL);
706 platform_set_drvdata(pdev,
NULL);
731 { .compatible =
"sirf,prima2-uart", },
739 .suspend = sirfsoc_uart_suspend,
740 .resume = sirfsoc_uart_resume,
744 .of_match_table = sirfsoc_uart_ids,
748 static int __init sirfsoc_uart_init(
void)
764 static void __exit sirfsoc_uart_exit(
void)