Lines Matching +full:port +full:-
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>
122 struct uart_port port; member
134 static int sprd_start_dma_rx(struct uart_port *port);
135 static int sprd_tx_dma_config(struct uart_port *port);
137 static inline unsigned int serial_in(struct uart_port *port, in serial_in() argument
140 return readl_relaxed(port->membase + offset); in serial_in()
143 static inline void serial_out(struct uart_port *port, unsigned int offset, in serial_out() argument
146 writel_relaxed(value, port->membase + offset); in serial_out()
149 static unsigned int sprd_tx_empty(struct uart_port *port) in sprd_tx_empty() argument
151 if (serial_in(port, SPRD_STS1) & SPRD_TX_FIFO_CNT_MASK) in sprd_tx_empty()
157 static unsigned int sprd_get_mctrl(struct uart_port *port) in sprd_get_mctrl() argument
162 static void sprd_set_mctrl(struct uart_port *port, unsigned int mctrl) in sprd_set_mctrl() argument
164 u32 val = serial_in(port, SPRD_CTL1); in sprd_set_mctrl()
171 serial_out(port, SPRD_CTL1, val); in sprd_set_mctrl()
174 static void sprd_stop_rx(struct uart_port *port) in sprd_stop_rx() argument
177 container_of(port, struct sprd_uart_port, port); in sprd_stop_rx()
180 if (sp->rx_dma.enable) in sprd_stop_rx()
181 dmaengine_terminate_all(sp->rx_dma.chn); in sprd_stop_rx()
183 iclr = serial_in(port, SPRD_ICLR); in sprd_stop_rx()
184 ien = serial_in(port, SPRD_IEN); in sprd_stop_rx()
189 serial_out(port, SPRD_IEN, ien); in sprd_stop_rx()
190 serial_out(port, SPRD_ICLR, iclr); in sprd_stop_rx()
193 static void sprd_uart_dma_enable(struct uart_port *port, bool enable) in sprd_uart_dma_enable() argument
195 u32 val = serial_in(port, SPRD_CTL1); in sprd_uart_dma_enable()
202 serial_out(port, SPRD_CTL1, val); in sprd_uart_dma_enable()
205 static void sprd_stop_tx_dma(struct uart_port *port) in sprd_stop_tx_dma() argument
208 container_of(port, struct sprd_uart_port, port); in sprd_stop_tx_dma()
212 dmaengine_pause(sp->tx_dma.chn); in sprd_stop_tx_dma()
214 dmaengine_tx_status(sp->tx_dma.chn, sp->tx_dma.cookie, &state); in sprd_stop_tx_dma()
216 trans_len = state.residue - sp->tx_dma.phys_addr; in sprd_stop_tx_dma()
217 uart_xmit_advance(port, trans_len); in sprd_stop_tx_dma()
218 dma_unmap_single(port->dev, sp->tx_dma.phys_addr, in sprd_stop_tx_dma()
219 sp->tx_dma.trans_len, DMA_TO_DEVICE); in sprd_stop_tx_dma()
222 dmaengine_terminate_all(sp->tx_dma.chn); in sprd_stop_tx_dma()
223 sp->tx_dma.trans_len = 0; in sprd_stop_tx_dma()
226 static int sprd_tx_buf_remap(struct uart_port *port) in sprd_tx_buf_remap() argument
229 container_of(port, struct sprd_uart_port, port); in sprd_tx_buf_remap()
230 struct tty_port *tport = &port->state->port; in sprd_tx_buf_remap()
233 sp->tx_dma.trans_len = kfifo_out_linear_ptr(&tport->xmit_fifo, &tail, in sprd_tx_buf_remap()
236 sp->tx_dma.phys_addr = dma_map_single(port->dev, tail, in sprd_tx_buf_remap()
237 sp->tx_dma.trans_len, in sprd_tx_buf_remap()
239 return dma_mapping_error(port->dev, sp->tx_dma.phys_addr); in sprd_tx_buf_remap()
244 struct uart_port *port = (struct uart_port *)data; in sprd_complete_tx_dma() local
246 container_of(port, struct sprd_uart_port, port); in sprd_complete_tx_dma()
247 struct tty_port *tport = &port->state->port; in sprd_complete_tx_dma()
250 uart_port_lock_irqsave(port, &flags); in sprd_complete_tx_dma()
251 dma_unmap_single(port->dev, sp->tx_dma.phys_addr, in sprd_complete_tx_dma()
252 sp->tx_dma.trans_len, DMA_TO_DEVICE); in sprd_complete_tx_dma()
254 uart_xmit_advance(port, sp->tx_dma.trans_len); in sprd_complete_tx_dma()
256 if (kfifo_len(&tport->xmit_fifo) < WAKEUP_CHARS) in sprd_complete_tx_dma()
257 uart_write_wakeup(port); in sprd_complete_tx_dma()
259 if (kfifo_is_empty(&tport->xmit_fifo) || sprd_tx_buf_remap(port) || in sprd_complete_tx_dma()
260 sprd_tx_dma_config(port)) in sprd_complete_tx_dma()
261 sp->tx_dma.trans_len = 0; in sprd_complete_tx_dma()
263 uart_port_unlock_irqrestore(port, flags); in sprd_complete_tx_dma()
266 static int sprd_uart_dma_submit(struct uart_port *port, in sprd_uart_dma_submit() argument
279 dma_des = dmaengine_prep_slave_single(ud->chn, ud->phys_addr, trans_len, in sprd_uart_dma_submit()
282 return -ENODEV; in sprd_uart_dma_submit()
284 dma_des->callback = callback; in sprd_uart_dma_submit()
285 dma_des->callback_param = port; in sprd_uart_dma_submit()
287 ud->cookie = dmaengine_submit(dma_des); in sprd_uart_dma_submit()
288 if (dma_submit_error(ud->cookie)) in sprd_uart_dma_submit()
289 return dma_submit_error(ud->cookie); in sprd_uart_dma_submit()
291 dma_async_issue_pending(ud->chn); in sprd_uart_dma_submit()
296 static int sprd_tx_dma_config(struct uart_port *port) in sprd_tx_dma_config() argument
299 container_of(port, struct sprd_uart_port, port); in sprd_tx_dma_config()
300 u32 burst = sp->tx_dma.trans_len > SPRD_TX_FIFO_FULL ? in sprd_tx_dma_config()
301 SPRD_TX_FIFO_FULL : sp->tx_dma.trans_len; in sprd_tx_dma_config()
304 .dst_addr = port->mapbase + SPRD_TXD, in sprd_tx_dma_config()
310 ret = dmaengine_slave_config(sp->tx_dma.chn, &cfg); in sprd_tx_dma_config()
314 return sprd_uart_dma_submit(port, &sp->tx_dma, sp->tx_dma.trans_len, in sprd_tx_dma_config()
318 static void sprd_start_tx_dma(struct uart_port *port) in sprd_start_tx_dma() argument
321 container_of(port, struct sprd_uart_port, port); in sprd_start_tx_dma()
322 struct tty_port *tport = &port->state->port; in sprd_start_tx_dma()
324 if (port->x_char) { in sprd_start_tx_dma()
325 serial_out(port, SPRD_TXD, port->x_char); in sprd_start_tx_dma()
326 port->icount.tx++; in sprd_start_tx_dma()
327 port->x_char = 0; in sprd_start_tx_dma()
331 if (kfifo_is_empty(&tport->xmit_fifo) || uart_tx_stopped(port)) { in sprd_start_tx_dma()
332 sprd_stop_tx_dma(port); in sprd_start_tx_dma()
336 if (sp->tx_dma.trans_len) in sprd_start_tx_dma()
339 if (sprd_tx_buf_remap(port) || sprd_tx_dma_config(port)) in sprd_start_tx_dma()
340 sp->tx_dma.trans_len = 0; in sprd_start_tx_dma()
343 static void sprd_rx_full_thld(struct uart_port *port, u32 thld) in sprd_rx_full_thld() argument
345 u32 val = serial_in(port, SPRD_CTL2); in sprd_rx_full_thld()
349 serial_out(port, SPRD_CTL2, val); in sprd_rx_full_thld()
354 sp->rx_dma.virt = dma_alloc_coherent(sp->port.dev, SPRD_UART_RX_SIZE, in sprd_rx_alloc_buf()
355 &sp->rx_dma.phys_addr, GFP_KERNEL); in sprd_rx_alloc_buf()
356 if (!sp->rx_dma.virt) in sprd_rx_alloc_buf()
357 return -ENOMEM; in sprd_rx_alloc_buf()
364 if (sp->rx_dma.virt) in sprd_rx_free_buf()
365 dma_free_coherent(sp->port.dev, SPRD_UART_RX_SIZE, in sprd_rx_free_buf()
366 sp->rx_dma.virt, sp->rx_dma.phys_addr); in sprd_rx_free_buf()
367 sp->rx_dma.virt = NULL; in sprd_rx_free_buf()
370 static int sprd_rx_dma_config(struct uart_port *port, u32 burst) in sprd_rx_dma_config() argument
373 container_of(port, struct sprd_uart_port, port); in sprd_rx_dma_config()
375 .src_addr = port->mapbase + SPRD_RXD, in sprd_rx_dma_config()
381 return dmaengine_slave_config(sp->rx_dma.chn, &cfg); in sprd_rx_dma_config()
384 static void sprd_uart_dma_rx(struct uart_port *port) in sprd_uart_dma_rx() argument
387 container_of(port, struct sprd_uart_port, port); in sprd_uart_dma_rx()
388 struct tty_port *tty = &port->state->port; in sprd_uart_dma_rx()
390 port->icount.rx += sp->rx_dma.trans_len; in sprd_uart_dma_rx()
391 tty_insert_flip_string(tty, sp->rx_buf_tail, sp->rx_dma.trans_len); in sprd_uart_dma_rx()
395 static void sprd_uart_dma_irq(struct uart_port *port) in sprd_uart_dma_irq() argument
398 container_of(port, struct sprd_uart_port, port); in sprd_uart_dma_irq()
402 status = dmaengine_tx_status(sp->rx_dma.chn, in sprd_uart_dma_irq()
403 sp->rx_dma.cookie, &state); in sprd_uart_dma_irq()
405 sprd_stop_rx(port); in sprd_uart_dma_irq()
407 if (!state.residue && sp->pos == sp->rx_dma.phys_addr) in sprd_uart_dma_irq()
411 sp->rx_dma.trans_len = SPRD_UART_RX_SIZE + in sprd_uart_dma_irq()
412 sp->rx_dma.phys_addr - sp->pos; in sprd_uart_dma_irq()
413 sp->pos = sp->rx_dma.phys_addr; in sprd_uart_dma_irq()
415 sp->rx_dma.trans_len = state.residue - sp->pos; in sprd_uart_dma_irq()
416 sp->pos = state.residue; in sprd_uart_dma_irq()
419 sprd_uart_dma_rx(port); in sprd_uart_dma_irq()
420 sp->rx_buf_tail += sp->rx_dma.trans_len; in sprd_uart_dma_irq()
425 struct uart_port *port = (struct uart_port *)data; in sprd_complete_rx_dma() local
427 container_of(port, struct sprd_uart_port, port); in sprd_complete_rx_dma()
432 uart_port_lock_irqsave(port, &flags); in sprd_complete_rx_dma()
434 status = dmaengine_tx_status(sp->rx_dma.chn, in sprd_complete_rx_dma()
435 sp->rx_dma.cookie, &state); in sprd_complete_rx_dma()
437 sprd_stop_rx(port); in sprd_complete_rx_dma()
438 uart_port_unlock_irqrestore(port, flags); in sprd_complete_rx_dma()
442 if (sp->pos != sp->rx_dma.phys_addr) { in sprd_complete_rx_dma()
443 sp->rx_dma.trans_len = SPRD_UART_RX_SIZE + in sprd_complete_rx_dma()
444 sp->rx_dma.phys_addr - sp->pos; in sprd_complete_rx_dma()
445 sprd_uart_dma_rx(port); in sprd_complete_rx_dma()
446 sp->rx_buf_tail += sp->rx_dma.trans_len; in sprd_complete_rx_dma()
449 if (sprd_start_dma_rx(port)) in sprd_complete_rx_dma()
450 sprd_stop_rx(port); in sprd_complete_rx_dma()
452 uart_port_unlock_irqrestore(port, flags); in sprd_complete_rx_dma()
455 static int sprd_start_dma_rx(struct uart_port *port) in sprd_start_dma_rx() argument
458 container_of(port, struct sprd_uart_port, port); in sprd_start_dma_rx()
461 if (!sp->rx_dma.enable) in sprd_start_dma_rx()
464 sp->pos = sp->rx_dma.phys_addr; in sprd_start_dma_rx()
465 sp->rx_buf_tail = sp->rx_dma.virt; in sprd_start_dma_rx()
466 sprd_rx_full_thld(port, SPRD_RX_FIFO_FULL); in sprd_start_dma_rx()
467 ret = sprd_rx_dma_config(port, SPRD_RX_DMA_STEP); in sprd_start_dma_rx()
471 return sprd_uart_dma_submit(port, &sp->rx_dma, SPRD_UART_RX_SIZE, in sprd_start_dma_rx()
475 static void sprd_release_dma(struct uart_port *port) in sprd_release_dma() argument
478 container_of(port, struct sprd_uart_port, port); in sprd_release_dma()
480 sprd_uart_dma_enable(port, false); in sprd_release_dma()
482 if (sp->rx_dma.enable) in sprd_release_dma()
483 dma_release_channel(sp->rx_dma.chn); in sprd_release_dma()
485 if (sp->tx_dma.enable) in sprd_release_dma()
486 dma_release_channel(sp->tx_dma.chn); in sprd_release_dma()
488 sp->tx_dma.enable = false; in sprd_release_dma()
489 sp->rx_dma.enable = false; in sprd_release_dma()
492 static void sprd_request_dma(struct uart_port *port) in sprd_request_dma() argument
495 container_of(port, struct sprd_uart_port, port); in sprd_request_dma()
497 sp->tx_dma.enable = true; in sprd_request_dma()
498 sp->rx_dma.enable = true; in sprd_request_dma()
500 sp->tx_dma.chn = dma_request_chan(port->dev, "tx"); in sprd_request_dma()
501 if (IS_ERR(sp->tx_dma.chn)) { in sprd_request_dma()
502 dev_err(port->dev, "request TX DMA channel failed, ret = %ld\n", in sprd_request_dma()
503 PTR_ERR(sp->tx_dma.chn)); in sprd_request_dma()
504 sp->tx_dma.enable = false; in sprd_request_dma()
507 sp->rx_dma.chn = dma_request_chan(port->dev, "rx"); in sprd_request_dma()
508 if (IS_ERR(sp->rx_dma.chn)) { in sprd_request_dma()
509 dev_err(port->dev, "request RX DMA channel failed, ret = %ld\n", in sprd_request_dma()
510 PTR_ERR(sp->rx_dma.chn)); in sprd_request_dma()
511 sp->rx_dma.enable = false; in sprd_request_dma()
515 static void sprd_stop_tx(struct uart_port *port) in sprd_stop_tx() argument
517 struct sprd_uart_port *sp = container_of(port, struct sprd_uart_port, in sprd_stop_tx()
518 port); in sprd_stop_tx()
521 if (sp->tx_dma.enable) { in sprd_stop_tx()
522 sprd_stop_tx_dma(port); in sprd_stop_tx()
526 iclr = serial_in(port, SPRD_ICLR); in sprd_stop_tx()
527 ien = serial_in(port, SPRD_IEN); in sprd_stop_tx()
532 serial_out(port, SPRD_IEN, ien); in sprd_stop_tx()
533 serial_out(port, SPRD_ICLR, iclr); in sprd_stop_tx()
536 static void sprd_start_tx(struct uart_port *port) in sprd_start_tx() argument
538 struct sprd_uart_port *sp = container_of(port, struct sprd_uart_port, in sprd_start_tx()
539 port); in sprd_start_tx()
542 if (sp->tx_dma.enable) { in sprd_start_tx()
543 sprd_start_tx_dma(port); in sprd_start_tx()
547 ien = serial_in(port, SPRD_IEN); in sprd_start_tx()
550 serial_out(port, SPRD_IEN, ien); in sprd_start_tx()
555 static void sprd_break_ctl(struct uart_port *port, int break_state) in sprd_break_ctl() argument
560 static int handle_lsr_errors(struct uart_port *port, in handle_lsr_errors() argument
569 port->icount.brk++; in handle_lsr_errors()
570 ret = uart_handle_break(port); in handle_lsr_errors()
574 port->icount.parity++; in handle_lsr_errors()
576 port->icount.frame++; in handle_lsr_errors()
578 port->icount.overrun++; in handle_lsr_errors()
581 *lsr &= port->read_status_mask; in handle_lsr_errors()
592 static inline void sprd_rx(struct uart_port *port) in sprd_rx() argument
594 struct sprd_uart_port *sp = container_of(port, struct sprd_uart_port, in sprd_rx()
595 port); in sprd_rx()
596 struct tty_port *tty = &port->state->port; in sprd_rx()
600 if (sp->rx_dma.enable) { in sprd_rx()
601 sprd_uart_dma_irq(port); in sprd_rx()
605 while ((serial_in(port, SPRD_STS1) & SPRD_RX_FIFO_CNT_MASK) && in sprd_rx()
606 max_count--) { in sprd_rx()
607 lsr = serial_in(port, SPRD_LSR); in sprd_rx()
608 ch = serial_in(port, SPRD_RXD); in sprd_rx()
610 port->icount.rx++; in sprd_rx()
614 if (handle_lsr_errors(port, &flag, &lsr)) in sprd_rx()
616 if (uart_handle_sysrq_char(port, ch)) in sprd_rx()
619 uart_insert_char(port, lsr, SPRD_LSR_OE, ch, flag); in sprd_rx()
625 static inline void sprd_tx(struct uart_port *port) in sprd_tx() argument
629 uart_port_tx_limited(port, ch, THLD_TX_EMPTY, in sprd_tx()
631 serial_out(port, SPRD_TXD, ch), in sprd_tx()
635 /* this handles the interrupt from one port */
638 struct uart_port *port = dev_id; in sprd_handle_irq() local
641 uart_port_lock(port); in sprd_handle_irq()
643 ims = serial_in(port, SPRD_IMSR); in sprd_handle_irq()
646 uart_port_unlock(port); in sprd_handle_irq()
651 serial_out(port, SPRD_ICLR, SPRD_ICLR_TIMEOUT); in sprd_handle_irq()
654 serial_out(port, SPRD_ICLR, SPRD_IMSR_BREAK_DETECT); in sprd_handle_irq()
658 sprd_rx(port); in sprd_handle_irq()
661 sprd_tx(port); in sprd_handle_irq()
663 uart_port_unlock(port); in sprd_handle_irq()
668 static void sprd_uart_dma_startup(struct uart_port *port, in sprd_uart_dma_startup() argument
673 sprd_request_dma(port); in sprd_uart_dma_startup()
674 if (!(sp->rx_dma.enable || sp->tx_dma.enable)) in sprd_uart_dma_startup()
677 ret = sprd_start_dma_rx(port); in sprd_uart_dma_startup()
679 sp->rx_dma.enable = false; in sprd_uart_dma_startup()
680 dma_release_channel(sp->rx_dma.chn); in sprd_uart_dma_startup()
681 dev_warn(port->dev, "fail to start RX dma mode\n"); in sprd_uart_dma_startup()
684 sprd_uart_dma_enable(port, true); in sprd_uart_dma_startup()
687 static int sprd_startup(struct uart_port *port) in sprd_startup() argument
695 serial_out(port, SPRD_CTL2, in sprd_startup()
700 while (timeout-- && serial_in(port, SPRD_STS1) & SPRD_RX_FIFO_CNT_MASK) in sprd_startup()
701 serial_in(port, SPRD_RXD); in sprd_startup()
705 while (timeout-- && serial_in(port, SPRD_STS1) & SPRD_TX_FIFO_CNT_MASK) in sprd_startup()
709 serial_out(port, SPRD_IEN, 0); in sprd_startup()
710 serial_out(port, SPRD_ICLR, ~0); in sprd_startup()
713 sp = container_of(port, struct sprd_uart_port, port); in sprd_startup()
714 snprintf(sp->name, sizeof(sp->name), "sprd_serial%d", port->line); in sprd_startup()
716 sprd_uart_dma_startup(port, sp); in sprd_startup()
718 ret = devm_request_irq(port->dev, port->irq, sprd_handle_irq, in sprd_startup()
719 IRQF_SHARED, sp->name, port); in sprd_startup()
721 dev_err(port->dev, "fail to request serial irq %d, ret=%d\n", in sprd_startup()
722 port->irq, ret); in sprd_startup()
725 fc = serial_in(port, SPRD_CTL1); in sprd_startup()
727 serial_out(port, SPRD_CTL1, fc); in sprd_startup()
730 uart_port_lock_irqsave(port, &flags); in sprd_startup()
731 ien = serial_in(port, SPRD_IEN); in sprd_startup()
733 if (!sp->rx_dma.enable) in sprd_startup()
735 serial_out(port, SPRD_IEN, ien); in sprd_startup()
736 uart_port_unlock_irqrestore(port, flags); in sprd_startup()
741 static void sprd_shutdown(struct uart_port *port) in sprd_shutdown() argument
743 sprd_release_dma(port); in sprd_shutdown()
744 serial_out(port, SPRD_IEN, 0); in sprd_shutdown()
745 serial_out(port, SPRD_ICLR, ~0); in sprd_shutdown()
746 devm_free_irq(port->dev, port->irq, port); in sprd_shutdown()
749 static void sprd_set_termios(struct uart_port *port, struct ktermios *termios, in sprd_set_termios() argument
757 baud = uart_get_baud_rate(port, termios, old, 0, SPRD_BAUD_IO_LIMIT); in sprd_set_termios()
759 quot = port->uartclk / baud; in sprd_set_termios()
762 switch (termios->c_cflag & CSIZE) { in sprd_set_termios()
780 if (termios->c_cflag & CSTOPB) in sprd_set_termios()
787 termios->c_cflag &= ~CMSPAR; /* no support mark/space */ in sprd_set_termios()
788 if (termios->c_cflag & PARENB) { in sprd_set_termios()
790 if (termios->c_cflag & PARODD) in sprd_set_termios()
796 uart_port_lock_irqsave(port, &flags); in sprd_set_termios()
798 /* update the per-port timeout */ in sprd_set_termios()
799 uart_update_timeout(port, termios->c_cflag, baud); in sprd_set_termios()
801 port->read_status_mask = SPRD_LSR_OE; in sprd_set_termios()
802 if (termios->c_iflag & INPCK) in sprd_set_termios()
803 port->read_status_mask |= SPRD_LSR_FE | SPRD_LSR_PE; in sprd_set_termios()
804 if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK)) in sprd_set_termios()
805 port->read_status_mask |= SPRD_LSR_BI; in sprd_set_termios()
808 port->ignore_status_mask = 0; in sprd_set_termios()
809 if (termios->c_iflag & IGNPAR) in sprd_set_termios()
810 port->ignore_status_mask |= SPRD_LSR_PE | SPRD_LSR_FE; in sprd_set_termios()
811 if (termios->c_iflag & IGNBRK) { in sprd_set_termios()
812 port->ignore_status_mask |= SPRD_LSR_BI; in sprd_set_termios()
817 if (termios->c_iflag & IGNPAR) in sprd_set_termios()
818 port->ignore_status_mask |= SPRD_LSR_OE; in sprd_set_termios()
822 fc = serial_in(port, SPRD_CTL1); in sprd_set_termios()
824 if (termios->c_cflag & CRTSCTS) { in sprd_set_termios()
831 serial_out(port, SPRD_CLKD0, quot & SPRD_CLKD0_MASK); in sprd_set_termios()
834 serial_out(port, SPRD_CLKD1, in sprd_set_termios()
836 serial_out(port, SPRD_LCR, lcr); in sprd_set_termios()
838 serial_out(port, SPRD_CTL1, fc); in sprd_set_termios()
840 uart_port_unlock_irqrestore(port, flags); in sprd_set_termios()
847 static const char *sprd_type(struct uart_port *port) in sprd_type() argument
852 static void sprd_release_port(struct uart_port *port) in sprd_release_port() argument
857 static int sprd_request_port(struct uart_port *port) in sprd_request_port() argument
862 static void sprd_config_port(struct uart_port *port, int flags) in sprd_config_port() argument
865 port->type = PORT_SPRD; in sprd_config_port()
868 static int sprd_verify_port(struct uart_port *port, struct serial_struct *ser) in sprd_verify_port() argument
870 if (ser->type != PORT_SPRD) in sprd_verify_port()
871 return -EINVAL; in sprd_verify_port()
872 if (port->irq != ser->irq) in sprd_verify_port()
873 return -EINVAL; in sprd_verify_port()
874 if (port->iotype != ser->io_type) in sprd_verify_port()
875 return -EINVAL; in sprd_verify_port()
879 static void sprd_pm(struct uart_port *port, unsigned int state, in sprd_pm() argument
883 container_of(port, struct sprd_uart_port, port); in sprd_pm()
887 clk_prepare_enable(sup->clk); in sprd_pm()
890 clk_disable_unprepare(sup->clk); in sprd_pm()
896 static int sprd_poll_init(struct uart_port *port) in sprd_poll_init() argument
898 if (port->state->pm_state != UART_PM_STATE_ON) { in sprd_poll_init()
899 sprd_pm(port, UART_PM_STATE_ON, 0); in sprd_poll_init()
900 port->state->pm_state = UART_PM_STATE_ON; in sprd_poll_init()
906 static int sprd_poll_get_char(struct uart_port *port) in sprd_poll_get_char() argument
908 while (!(serial_in(port, SPRD_STS1) & SPRD_RX_FIFO_CNT_MASK)) in sprd_poll_get_char()
911 return serial_in(port, SPRD_RXD); in sprd_poll_get_char()
914 static void sprd_poll_put_char(struct uart_port *port, unsigned char ch) in sprd_poll_put_char() argument
916 while (serial_in(port, SPRD_STS1) & SPRD_TX_FIFO_CNT_MASK) in sprd_poll_put_char()
919 serial_out(port, SPRD_TXD, ch); in sprd_poll_put_char()
948 static void wait_for_xmitr(struct uart_port *port) in wait_for_xmitr() argument
954 status = serial_in(port, SPRD_STS1); in wait_for_xmitr()
955 if (--tmout == 0) in wait_for_xmitr()
961 static void sprd_console_putchar(struct uart_port *port, unsigned char ch) in sprd_console_putchar() argument
963 wait_for_xmitr(port); in sprd_console_putchar()
964 serial_out(port, SPRD_TXD, ch); in sprd_console_putchar()
970 struct uart_port *port = &sprd_port[co->index]->port; in sprd_console_write() local
974 if (port->sysrq) in sprd_console_write()
977 locked = uart_port_trylock_irqsave(port, &flags); in sprd_console_write()
979 uart_port_lock_irqsave(port, &flags); in sprd_console_write()
981 uart_console_write(port, s, count, sprd_console_putchar); in sprd_console_write()
984 wait_for_xmitr(port); in sprd_console_write()
987 uart_port_unlock_irqrestore(port, flags); in sprd_console_write()
998 if (co->index >= UART_NR_MAX || co->index < 0) in sprd_console_setup()
999 co->index = 0; in sprd_console_setup()
1001 sprd_uart_port = sprd_port[co->index]; in sprd_console_setup()
1002 if (!sprd_uart_port || !sprd_uart_port->port.membase) { in sprd_console_setup()
1003 pr_info("serial port %d not yet initialized\n", co->index); in sprd_console_setup()
1004 return -ENODEV; in sprd_console_setup()
1010 return uart_set_options(&sprd_uart_port->port, co, baud, in sprd_console_setup()
1021 .index = -1,
1035 static void sprd_putc(struct uart_port *port, unsigned char c) in sprd_putc() argument
1039 while (timeout-- && in sprd_putc()
1040 !(readl(port->membase + SPRD_LSR) & SPRD_LSR_TX_OVER)) in sprd_putc()
1043 writeb(c, port->membase + SPRD_TXD); in sprd_putc()
1048 struct earlycon_device *dev = con->data; in sprd_early_write()
1050 uart_console_write(&dev->port, s, n, sprd_putc); in sprd_early_write()
1056 if (!device->port.membase) in sprd_early_console_setup()
1057 return -ENODEV; in sprd_early_console_setup()
1059 device->con->write = sprd_early_write; in sprd_early_console_setup()
1062 OF_EARLYCON_DECLARE(sprd_serial, "sprd,sc9836-uart",
1084 uart_remove_one_port(&sprd_uart_driver, &sup->port); in sprd_remove()
1085 sprd_port[sup->port.line] = NULL; in sprd_remove()
1087 sprd_ports_num--; in sprd_remove()
1098 if ((cons && cons->index >= 0 && cons->index == uport->line) || in sprd_uart_is_console()
1099 of_console_check(uport->dev->of_node, SPRD_TTY_NAME, uport->line)) in sprd_uart_is_console()
1108 struct sprd_uart_port *u = container_of(uport, struct sprd_uart_port, port); in sprd_clk_init()
1110 clk_uart = devm_clk_get(uport->dev, "uart"); in sprd_clk_init()
1112 dev_warn(uport->dev, "uart%d can't get uart clock\n", in sprd_clk_init()
1113 uport->line); in sprd_clk_init()
1117 clk_parent = devm_clk_get(uport->dev, "source"); in sprd_clk_init()
1119 dev_warn(uport->dev, "uart%d can't get source clock\n", in sprd_clk_init()
1120 uport->line); in sprd_clk_init()
1125 uport->uartclk = SPRD_DEFAULT_SOURCE_CLK; in sprd_clk_init()
1127 uport->uartclk = clk_get_rate(clk_uart); in sprd_clk_init()
1129 u->clk = devm_clk_get(uport->dev, "enable"); in sprd_clk_init()
1130 if (IS_ERR(u->clk)) { in sprd_clk_init()
1131 if (PTR_ERR(u->clk) == -EPROBE_DEFER) in sprd_clk_init()
1132 return -EPROBE_DEFER; in sprd_clk_init()
1134 dev_warn(uport->dev, "uart%d can't get enable clock\n", in sprd_clk_init()
1135 uport->line); in sprd_clk_init()
1139 return PTR_ERR(u->clk); in sprd_clk_init()
1141 u->clk = NULL; in sprd_clk_init()
1156 index = of_alias_get_id(pdev->dev.of_node, "serial"); in sprd_probe()
1158 dev_err(&pdev->dev, "got a wrong serial alias id %d\n", index); in sprd_probe()
1159 return -EINVAL; in sprd_probe()
1162 sport = devm_kzalloc(&pdev->dev, sizeof(*sport), GFP_KERNEL); in sprd_probe()
1164 return -ENOMEM; in sprd_probe()
1166 up = &sport->port; in sprd_probe()
1167 up->dev = &pdev->dev; in sprd_probe()
1168 up->line = index; in sprd_probe()
1169 up->type = PORT_SPRD; in sprd_probe()
1170 up->iotype = UPIO_MEM; in sprd_probe()
1171 up->uartclk = SPRD_DEF_RATE; in sprd_probe()
1172 up->fifosize = SPRD_FIFO_SIZE; in sprd_probe()
1173 up->ops = &serial_sprd_ops; in sprd_probe()
1174 up->flags = UPF_BOOT_AUTOCONF; in sprd_probe()
1175 up->has_sysrq = IS_ENABLED(CONFIG_SERIAL_SPRD_CONSOLE); in sprd_probe()
1181 up->membase = devm_platform_get_and_ioremap_resource(pdev, 0, &res); in sprd_probe()
1182 if (IS_ERR(up->membase)) in sprd_probe()
1183 return PTR_ERR(up->membase); in sprd_probe()
1185 up->mapbase = res->start; in sprd_probe()
1190 up->irq = irq; in sprd_probe()
1203 pr_err("Failed to register SPRD-UART driver\n"); in sprd_probe()
1221 if (--sprd_ports_num == 0) in sprd_probe()
1233 uart_suspend_port(&sprd_uart_driver, &sup->port); in sprd_suspend()
1242 uart_resume_port(&sprd_uart_driver, &sup->port); in sprd_resume()
1251 {.compatible = "sprd,sc9836-uart",},