Lines Matching +full:sc9836 +full:- +full:uart

1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2012-2015 Spreadtrum Communications Inc.
10 #include <linux/dma-mapping.h>
11 #include <linux/dma/sprd-dma.h>
140 return readl_relaxed(port->membase + offset); in serial_in()
146 writel_relaxed(value, port->membase + offset); in serial_out()
180 if (sp->rx_dma.enable) in sprd_stop_rx()
181 dmaengine_terminate_all(sp->rx_dma.chn); in sprd_stop_rx()
209 struct circ_buf *xmit = &port->state->xmit; in sprd_stop_tx_dma()
213 dmaengine_pause(sp->tx_dma.chn); in sprd_stop_tx_dma()
215 dmaengine_tx_status(sp->tx_dma.chn, sp->tx_dma.cookie, &state); in sprd_stop_tx_dma()
217 trans_len = state.residue - sp->tx_dma.phys_addr; in sprd_stop_tx_dma()
218 xmit->tail = (xmit->tail + trans_len) & (UART_XMIT_SIZE - 1); in sprd_stop_tx_dma()
219 port->icount.tx += trans_len; in sprd_stop_tx_dma()
220 dma_unmap_single(port->dev, sp->tx_dma.phys_addr, in sprd_stop_tx_dma()
221 sp->tx_dma.trans_len, DMA_TO_DEVICE); in sprd_stop_tx_dma()
224 dmaengine_terminate_all(sp->tx_dma.chn); in sprd_stop_tx_dma()
225 sp->tx_dma.trans_len = 0; in sprd_stop_tx_dma()
232 struct circ_buf *xmit = &port->state->xmit; in sprd_tx_buf_remap()
234 sp->tx_dma.trans_len = in sprd_tx_buf_remap()
235 CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE); in sprd_tx_buf_remap()
237 sp->tx_dma.phys_addr = dma_map_single(port->dev, in sprd_tx_buf_remap()
238 (void *)&(xmit->buf[xmit->tail]), in sprd_tx_buf_remap()
239 sp->tx_dma.trans_len, in sprd_tx_buf_remap()
241 return dma_mapping_error(port->dev, sp->tx_dma.phys_addr); in sprd_tx_buf_remap()
249 struct circ_buf *xmit = &port->state->xmit; in sprd_complete_tx_dma()
252 spin_lock_irqsave(&port->lock, flags); in sprd_complete_tx_dma()
253 dma_unmap_single(port->dev, sp->tx_dma.phys_addr, in sprd_complete_tx_dma()
254 sp->tx_dma.trans_len, DMA_TO_DEVICE); in sprd_complete_tx_dma()
256 xmit->tail = (xmit->tail + sp->tx_dma.trans_len) & (UART_XMIT_SIZE - 1); in sprd_complete_tx_dma()
257 port->icount.tx += sp->tx_dma.trans_len; in sprd_complete_tx_dma()
264 sp->tx_dma.trans_len = 0; in sprd_complete_tx_dma()
266 spin_unlock_irqrestore(&port->lock, flags); in sprd_complete_tx_dma()
282 dma_des = dmaengine_prep_slave_single(ud->chn, ud->phys_addr, trans_len, in sprd_uart_dma_submit()
285 return -ENODEV; in sprd_uart_dma_submit()
287 dma_des->callback = callback; in sprd_uart_dma_submit()
288 dma_des->callback_param = port; in sprd_uart_dma_submit()
290 ud->cookie = dmaengine_submit(dma_des); in sprd_uart_dma_submit()
291 if (dma_submit_error(ud->cookie)) in sprd_uart_dma_submit()
292 return dma_submit_error(ud->cookie); in sprd_uart_dma_submit()
294 dma_async_issue_pending(ud->chn); in sprd_uart_dma_submit()
303 u32 burst = sp->tx_dma.trans_len > SPRD_TX_FIFO_FULL ? in sprd_tx_dma_config()
304 SPRD_TX_FIFO_FULL : sp->tx_dma.trans_len; in sprd_tx_dma_config()
307 .dst_addr = port->mapbase + SPRD_TXD, in sprd_tx_dma_config()
313 ret = dmaengine_slave_config(sp->tx_dma.chn, &cfg); in sprd_tx_dma_config()
317 return sprd_uart_dma_submit(port, &sp->tx_dma, sp->tx_dma.trans_len, in sprd_tx_dma_config()
325 struct circ_buf *xmit = &port->state->xmit; in sprd_start_tx_dma()
327 if (port->x_char) { in sprd_start_tx_dma()
328 serial_out(port, SPRD_TXD, port->x_char); in sprd_start_tx_dma()
329 port->icount.tx++; in sprd_start_tx_dma()
330 port->x_char = 0; in sprd_start_tx_dma()
339 if (sp->tx_dma.trans_len) in sprd_start_tx_dma()
343 sp->tx_dma.trans_len = 0; in sprd_start_tx_dma()
357 sp->rx_dma.virt = dma_alloc_coherent(sp->port.dev, SPRD_UART_RX_SIZE, in sprd_rx_alloc_buf()
358 &sp->rx_dma.phys_addr, GFP_KERNEL); in sprd_rx_alloc_buf()
359 if (!sp->rx_dma.virt) in sprd_rx_alloc_buf()
360 return -ENOMEM; in sprd_rx_alloc_buf()
367 if (sp->rx_dma.virt) in sprd_rx_free_buf()
368 dma_free_coherent(sp->port.dev, SPRD_UART_RX_SIZE, in sprd_rx_free_buf()
369 sp->rx_dma.virt, sp->rx_dma.phys_addr); in sprd_rx_free_buf()
378 .src_addr = port->mapbase + SPRD_RXD, in sprd_rx_dma_config()
384 return dmaengine_slave_config(sp->rx_dma.chn, &cfg); in sprd_rx_dma_config()
391 struct tty_port *tty = &port->state->port; in sprd_uart_dma_rx()
393 port->icount.rx += sp->rx_dma.trans_len; in sprd_uart_dma_rx()
394 tty_insert_flip_string(tty, sp->rx_buf_tail, sp->rx_dma.trans_len); in sprd_uart_dma_rx()
405 status = dmaengine_tx_status(sp->rx_dma.chn, in sprd_uart_dma_irq()
406 sp->rx_dma.cookie, &state); in sprd_uart_dma_irq()
410 if (!state.residue && sp->pos == sp->rx_dma.phys_addr) in sprd_uart_dma_irq()
414 sp->rx_dma.trans_len = SPRD_UART_RX_SIZE + in sprd_uart_dma_irq()
415 sp->rx_dma.phys_addr - sp->pos; in sprd_uart_dma_irq()
416 sp->pos = sp->rx_dma.phys_addr; in sprd_uart_dma_irq()
418 sp->rx_dma.trans_len = state.residue - sp->pos; in sprd_uart_dma_irq()
419 sp->pos = state.residue; in sprd_uart_dma_irq()
423 sp->rx_buf_tail += sp->rx_dma.trans_len; in sprd_uart_dma_irq()
435 spin_lock_irqsave(&port->lock, flags); in sprd_complete_rx_dma()
437 status = dmaengine_tx_status(sp->rx_dma.chn, in sprd_complete_rx_dma()
438 sp->rx_dma.cookie, &state); in sprd_complete_rx_dma()
441 spin_unlock_irqrestore(&port->lock, flags); in sprd_complete_rx_dma()
445 if (sp->pos != sp->rx_dma.phys_addr) { in sprd_complete_rx_dma()
446 sp->rx_dma.trans_len = SPRD_UART_RX_SIZE + in sprd_complete_rx_dma()
447 sp->rx_dma.phys_addr - sp->pos; in sprd_complete_rx_dma()
449 sp->rx_buf_tail += sp->rx_dma.trans_len; in sprd_complete_rx_dma()
455 spin_unlock_irqrestore(&port->lock, flags); in sprd_complete_rx_dma()
464 if (!sp->rx_dma.enable) in sprd_start_dma_rx()
467 sp->pos = sp->rx_dma.phys_addr; in sprd_start_dma_rx()
468 sp->rx_buf_tail = sp->rx_dma.virt; in sprd_start_dma_rx()
474 return sprd_uart_dma_submit(port, &sp->rx_dma, SPRD_UART_RX_SIZE, in sprd_start_dma_rx()
485 if (sp->rx_dma.enable) in sprd_release_dma()
486 dma_release_channel(sp->rx_dma.chn); in sprd_release_dma()
488 if (sp->tx_dma.enable) in sprd_release_dma()
489 dma_release_channel(sp->tx_dma.chn); in sprd_release_dma()
491 sp->tx_dma.enable = false; in sprd_release_dma()
492 sp->rx_dma.enable = false; in sprd_release_dma()
500 sp->tx_dma.enable = true; in sprd_request_dma()
501 sp->rx_dma.enable = true; in sprd_request_dma()
503 sp->tx_dma.chn = dma_request_chan(port->dev, "tx"); in sprd_request_dma()
504 if (IS_ERR(sp->tx_dma.chn)) { in sprd_request_dma()
505 dev_err(port->dev, "request TX DMA channel failed, ret = %ld\n", in sprd_request_dma()
506 PTR_ERR(sp->tx_dma.chn)); in sprd_request_dma()
507 sp->tx_dma.enable = false; in sprd_request_dma()
510 sp->rx_dma.chn = dma_request_chan(port->dev, "rx"); in sprd_request_dma()
511 if (IS_ERR(sp->rx_dma.chn)) { in sprd_request_dma()
512 dev_err(port->dev, "request RX DMA channel failed, ret = %ld\n", in sprd_request_dma()
513 PTR_ERR(sp->rx_dma.chn)); in sprd_request_dma()
514 sp->rx_dma.enable = false; in sprd_request_dma()
524 if (sp->tx_dma.enable) { in sprd_stop_tx()
545 if (sp->tx_dma.enable) { in sprd_start_tx()
572 port->icount.brk++; in handle_lsr_errors()
577 port->icount.parity++; in handle_lsr_errors()
579 port->icount.frame++; in handle_lsr_errors()
581 port->icount.overrun++; in handle_lsr_errors()
584 *lsr &= port->read_status_mask; in handle_lsr_errors()
599 struct tty_port *tty = &port->state->port; in sprd_rx()
602 if (sp->rx_dma.enable) { in sprd_rx()
608 max_count--) { in sprd_rx()
612 port->icount.rx++; in sprd_rx()
629 struct circ_buf *xmit = &port->state->xmit; in sprd_tx()
632 if (port->x_char) { in sprd_tx()
633 serial_out(port, SPRD_TXD, port->x_char); in sprd_tx()
634 port->icount.tx++; in sprd_tx()
635 port->x_char = 0; in sprd_tx()
646 serial_out(port, SPRD_TXD, xmit->buf[xmit->tail]); in sprd_tx()
647 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1); in sprd_tx()
648 port->icount.tx++; in sprd_tx()
651 } while (--count > 0); in sprd_tx()
666 spin_lock(&port->lock); in sprd_handle_irq()
671 spin_unlock(&port->lock); in sprd_handle_irq()
688 spin_unlock(&port->lock); in sprd_handle_irq()
699 if (!(sp->rx_dma.enable || sp->tx_dma.enable)) in sprd_uart_dma_startup()
704 sp->rx_dma.enable = false; in sprd_uart_dma_startup()
705 dma_release_channel(sp->rx_dma.chn); in sprd_uart_dma_startup()
706 dev_warn(port->dev, "fail to start RX dma mode\n"); in sprd_uart_dma_startup()
725 while (timeout-- && serial_in(port, SPRD_STS1) & SPRD_RX_FIFO_CNT_MASK) in sprd_startup()
730 while (timeout-- && serial_in(port, SPRD_STS1) & SPRD_TX_FIFO_CNT_MASK) in sprd_startup()
739 snprintf(sp->name, sizeof(sp->name), "sprd_serial%d", port->line); in sprd_startup()
743 ret = devm_request_irq(port->dev, port->irq, sprd_handle_irq, in sprd_startup()
744 IRQF_SHARED, sp->name, port); in sprd_startup()
746 dev_err(port->dev, "fail to request serial irq %d, ret=%d\n", in sprd_startup()
747 port->irq, ret); in sprd_startup()
755 spin_lock_irqsave(&port->lock, flags); in sprd_startup()
758 if (!sp->rx_dma.enable) in sprd_startup()
761 spin_unlock_irqrestore(&port->lock, flags); in sprd_startup()
771 devm_free_irq(port->dev, port->irq, port); in sprd_shutdown()
785 quot = port->uartclk / baud; in sprd_set_termios()
788 switch (termios->c_cflag & CSIZE) { in sprd_set_termios()
806 if (termios->c_cflag & CSTOPB) in sprd_set_termios()
813 termios->c_cflag &= ~CMSPAR; /* no support mark/space */ in sprd_set_termios()
814 if (termios->c_cflag & PARENB) { in sprd_set_termios()
816 if (termios->c_cflag & PARODD) in sprd_set_termios()
822 spin_lock_irqsave(&port->lock, flags); in sprd_set_termios()
824 /* update the per-port timeout */ in sprd_set_termios()
825 uart_update_timeout(port, termios->c_cflag, baud); in sprd_set_termios()
827 port->read_status_mask = SPRD_LSR_OE; in sprd_set_termios()
828 if (termios->c_iflag & INPCK) in sprd_set_termios()
829 port->read_status_mask |= SPRD_LSR_FE | SPRD_LSR_PE; in sprd_set_termios()
830 if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK)) in sprd_set_termios()
831 port->read_status_mask |= SPRD_LSR_BI; in sprd_set_termios()
834 port->ignore_status_mask = 0; in sprd_set_termios()
835 if (termios->c_iflag & IGNPAR) in sprd_set_termios()
836 port->ignore_status_mask |= SPRD_LSR_PE | SPRD_LSR_FE; in sprd_set_termios()
837 if (termios->c_iflag & IGNBRK) { in sprd_set_termios()
838 port->ignore_status_mask |= SPRD_LSR_BI; in sprd_set_termios()
843 if (termios->c_iflag & IGNPAR) in sprd_set_termios()
844 port->ignore_status_mask |= SPRD_LSR_OE; in sprd_set_termios()
850 if (termios->c_cflag & CRTSCTS) { in sprd_set_termios()
866 spin_unlock_irqrestore(&port->lock, flags); in sprd_set_termios()
891 port->type = PORT_SPRD; in sprd_config_port()
896 if (ser->type != PORT_SPRD) in sprd_verify_port()
897 return -EINVAL; in sprd_verify_port()
898 if (port->irq != ser->irq) in sprd_verify_port()
899 return -EINVAL; in sprd_verify_port()
900 if (port->iotype != ser->io_type) in sprd_verify_port()
901 return -EINVAL; in sprd_verify_port()
913 clk_prepare_enable(sup->clk); in sprd_pm()
916 clk_disable_unprepare(sup->clk); in sprd_pm()
924 if (port->state->pm_state != UART_PM_STATE_ON) { in sprd_poll_init()
926 port->state->pm_state = UART_PM_STATE_ON; in sprd_poll_init()
981 if (--tmout == 0) in wait_for_xmitr()
996 struct uart_port *port = &sprd_port[co->index]->port; in sprd_console_write()
1000 if (port->sysrq) in sprd_console_write()
1003 locked = spin_trylock_irqsave(&port->lock, flags); in sprd_console_write()
1005 spin_lock_irqsave(&port->lock, flags); in sprd_console_write()
1013 spin_unlock_irqrestore(&port->lock, flags); in sprd_console_write()
1024 if (co->index >= UART_NR_MAX || co->index < 0) in sprd_console_setup()
1025 co->index = 0; in sprd_console_setup()
1027 sprd_uart_port = sprd_port[co->index]; in sprd_console_setup()
1028 if (!sprd_uart_port || !sprd_uart_port->port.membase) { in sprd_console_setup()
1029 pr_info("serial port %d not yet initialized\n", co->index); in sprd_console_setup()
1030 return -ENODEV; in sprd_console_setup()
1036 return uart_set_options(&sprd_uart_port->port, co, baud, in sprd_console_setup()
1047 .index = -1,
1065 while (timeout-- && in sprd_putc()
1066 !(readl(port->membase + SPRD_LSR) & SPRD_LSR_TX_OVER)) in sprd_putc()
1069 writeb(c, port->membase + SPRD_TXD); in sprd_putc()
1074 struct earlycon_device *dev = con->data; in sprd_early_write()
1076 uart_console_write(&dev->port, s, n, sprd_putc); in sprd_early_write()
1082 if (!device->port.membase) in sprd_early_console_setup()
1083 return -ENODEV; in sprd_early_console_setup()
1085 device->con->write = sprd_early_write; in sprd_early_console_setup()
1088 OF_EARLYCON_DECLARE(sprd_serial, "sprd,sc9836-uart",
1110 uart_remove_one_port(&sprd_uart_driver, &sup->port); in sprd_remove()
1111 sprd_port[sup->port.line] = NULL; in sprd_remove()
1113 sprd_ports_num--; in sprd_remove()
1126 if ((cons && cons->index >= 0 && cons->index == uport->line) || in sprd_uart_is_console()
1127 of_console_check(uport->dev->of_node, SPRD_TTY_NAME, uport->line)) in sprd_uart_is_console()
1136 struct sprd_uart_port *u = sprd_port[uport->line]; in sprd_clk_init()
1138 clk_uart = devm_clk_get(uport->dev, "uart"); in sprd_clk_init()
1140 dev_warn(uport->dev, "uart%d can't get uart clock\n", in sprd_clk_init()
1141 uport->line); in sprd_clk_init()
1145 clk_parent = devm_clk_get(uport->dev, "source"); in sprd_clk_init()
1147 dev_warn(uport->dev, "uart%d can't get source clock\n", in sprd_clk_init()
1148 uport->line); in sprd_clk_init()
1153 uport->uartclk = SPRD_DEFAULT_SOURCE_CLK; in sprd_clk_init()
1155 uport->uartclk = clk_get_rate(clk_uart); in sprd_clk_init()
1157 u->clk = devm_clk_get(uport->dev, "enable"); in sprd_clk_init()
1158 if (IS_ERR(u->clk)) { in sprd_clk_init()
1159 if (PTR_ERR(u->clk) == -EPROBE_DEFER) in sprd_clk_init()
1160 return -EPROBE_DEFER; in sprd_clk_init()
1162 dev_warn(uport->dev, "uart%d can't get enable clock\n", in sprd_clk_init()
1163 uport->line); in sprd_clk_init()
1167 return PTR_ERR(u->clk); in sprd_clk_init()
1169 u->clk = NULL; in sprd_clk_init()
1183 index = of_alias_get_id(pdev->dev.of_node, "serial"); in sprd_probe()
1185 dev_err(&pdev->dev, "got a wrong serial alias id %d\n", index); in sprd_probe()
1186 return -EINVAL; in sprd_probe()
1189 sprd_port[index] = devm_kzalloc(&pdev->dev, sizeof(*sprd_port[index]), in sprd_probe()
1192 return -ENOMEM; in sprd_probe()
1194 up = &sprd_port[index]->port; in sprd_probe()
1195 up->dev = &pdev->dev; in sprd_probe()
1196 up->line = index; in sprd_probe()
1197 up->type = PORT_SPRD; in sprd_probe()
1198 up->iotype = UPIO_MEM; in sprd_probe()
1199 up->uartclk = SPRD_DEF_RATE; in sprd_probe()
1200 up->fifosize = SPRD_FIFO_SIZE; in sprd_probe()
1201 up->ops = &serial_sprd_ops; in sprd_probe()
1202 up->flags = UPF_BOOT_AUTOCONF; in sprd_probe()
1203 up->has_sysrq = IS_ENABLED(CONFIG_SERIAL_SPRD_CONSOLE); in sprd_probe()
1210 up->membase = devm_ioremap_resource(&pdev->dev, res); in sprd_probe()
1211 if (IS_ERR(up->membase)) in sprd_probe()
1212 return PTR_ERR(up->membase); in sprd_probe()
1214 up->mapbase = res->start; in sprd_probe()
1219 up->irq = irq; in sprd_probe()
1232 pr_err("Failed to register SPRD-UART driver\n"); in sprd_probe()
1252 uart_suspend_port(&sprd_uart_driver, &sup->port); in sprd_suspend()
1261 uart_resume_port(&sprd_uart_driver, &sup->port); in sprd_resume()
1270 {.compatible = "sprd,sc9836-uart",},