Lines Matching +full:tx +full:- +full:fault +full:- +full:gpios

1 // SPDX-License-Identifier: GPL-2.0+
8 * Sorin-Andrei Pistirica <andrei.pistirica@microchip.com>
27 #include <asm/mach-pic32/pic32.h>
30 #define PIC32_DEV_NAME "pic32-uart"
44 /* struct pic32_sport - pic32 serial port descriptor
47 * @irq_fault: virtual fault interrupt number
48 * @irq_fault_name: irq fault name
51 * @irq_tx: virtual tx interrupt number
52 * @irq_tx_name: irq tx name
83 __raw_writel(val, sport->port.membase + reg); in pic32_uart_writel()
88 return __raw_readl(sport->port.membase + reg); in pic32_uart_readl()
132 /* wait for tx empty, otherwise chars will be lost or corrupted */ in pic32_wait_deplete_txbuf()
137 /* serial core request to check if uart tx buffer is empty */
167 if (!sport->cts_gpiod) in pic32_uart_get_mctrl()
169 else if (gpiod_get_value(sport->cts_gpiod)) in pic32_uart_get_mctrl()
181 /* stop tx and start tx are not called in pairs, therefore a flag indicates
182 * the status of irq to control the irq-depth.
186 if (en && !sport->enable_tx_irq) { in pic32_uart_irqtxen()
187 enable_irq(sport->irq_tx); in pic32_uart_irqtxen()
188 sport->enable_tx_irq = true; in pic32_uart_irqtxen()
189 } else if (!en && sport->enable_tx_irq) { in pic32_uart_irqtxen()
194 disable_irq_nosync(sport->irq_tx); in pic32_uart_irqtxen()
195 sport->enable_tx_irq = false; in pic32_uart_irqtxen()
199 /* serial core request to disable tx ASAP (used for flow control) */
210 /* wait for tx empty */ in pic32_uart_stop_tx()
218 /* serial core request to (re)enable tx */
234 disable_irq(sport->irq_rx); in pic32_uart_stop_rx()
247 spin_lock_irqsave(&port->lock, flags); in pic32_uart_break_ctl()
256 spin_unlock_irqrestore(&port->lock, flags); in pic32_uart_break_ctl()
262 return (port->type == PORT_PIC32) ? PIC32_DEV_NAME : NULL; in pic32_uart_type()
278 spin_lock(&port->lock); in pic32_uart_do_rx()
280 tty = &port->state->port; in pic32_uart_do_rx()
294 port->icount.overrun++; in pic32_uart_do_rx()
305 port->icount.rx++; in pic32_uart_do_rx()
314 port->icount.parity++; in pic32_uart_do_rx()
316 port->icount.frame++; in pic32_uart_do_rx()
319 sta_reg &= port->read_status_mask; in pic32_uart_do_rx()
330 if ((sta_reg & port->ignore_status_mask) == 0) in pic32_uart_do_rx()
333 } while (--max_count); in pic32_uart_do_rx()
335 spin_unlock(&port->lock); in pic32_uart_do_rx()
340 /* fill tx fifo with chars to send, stop when fifo is about to be full
346 struct circ_buf *xmit = &port->state->xmit; in pic32_uart_do_tx()
349 if (port->x_char) { in pic32_uart_do_tx()
350 pic32_uart_writel(sport, PIC32_UART_TX, port->x_char); in pic32_uart_do_tx()
351 port->icount.tx++; in pic32_uart_do_tx()
352 port->x_char = 0; in pic32_uart_do_tx()
364 /* keep stuffing chars into uart tx buffer in pic32_uart_do_tx()
375 unsigned int c = xmit->buf[xmit->tail]; in pic32_uart_do_tx()
379 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1); in pic32_uart_do_tx()
380 port->icount.tx++; in pic32_uart_do_tx()
383 if (--max_count == 0) in pic32_uart_do_tx()
409 /* TX interrupt handler */
415 spin_lock_irqsave(&port->lock, flags); in pic32_uart_tx_interrupt()
417 spin_unlock_irqrestore(&port->lock, flags); in pic32_uart_tx_interrupt()
422 /* FAULT interrupt handler */
429 /* enable rx & tx operation on uart */
440 /* disable rx & tx operation on uart */
445 /* wait for tx empty, otherwise chars will be lost or corrupted */ in pic32_uart_dsbl_and_mask()
458 u32 dflt_baud = (port->uartclk / PIC32_UART_DFLT_BRATE / 16) - 1; in pic32_uart_startup()
464 ret = clk_prepare_enable(sport->clk); in pic32_uart_startup()
488 sport->enable_tx_irq = false; in pic32_uart_startup()
490 sport->irq_fault_name = kasprintf(GFP_KERNEL, "%s%d-fault", in pic32_uart_startup()
492 sport->idx); in pic32_uart_startup()
493 if (!sport->irq_fault_name) { in pic32_uart_startup()
494 dev_err(port->dev, "%s: kasprintf err!", __func__); in pic32_uart_startup()
495 ret = -ENOMEM; in pic32_uart_startup()
498 irq_set_status_flags(sport->irq_fault, IRQ_NOAUTOEN); in pic32_uart_startup()
499 ret = request_irq(sport->irq_fault, pic32_uart_fault_interrupt, in pic32_uart_startup()
500 IRQF_NO_THREAD, sport->irq_fault_name, port); in pic32_uart_startup()
502 dev_err(port->dev, "%s: request irq(%d) err! ret:%d name:%s\n", in pic32_uart_startup()
503 __func__, sport->irq_fault, ret, in pic32_uart_startup()
508 sport->irq_rx_name = kasprintf(GFP_KERNEL, "%s%d-rx", in pic32_uart_startup()
510 sport->idx); in pic32_uart_startup()
511 if (!sport->irq_rx_name) { in pic32_uart_startup()
512 dev_err(port->dev, "%s: kasprintf err!", __func__); in pic32_uart_startup()
513 ret = -ENOMEM; in pic32_uart_startup()
516 irq_set_status_flags(sport->irq_rx, IRQ_NOAUTOEN); in pic32_uart_startup()
517 ret = request_irq(sport->irq_rx, pic32_uart_rx_interrupt, in pic32_uart_startup()
518 IRQF_NO_THREAD, sport->irq_rx_name, port); in pic32_uart_startup()
520 dev_err(port->dev, "%s: request irq(%d) err! ret:%d name:%s\n", in pic32_uart_startup()
521 __func__, sport->irq_rx, ret, in pic32_uart_startup()
526 sport->irq_tx_name = kasprintf(GFP_KERNEL, "%s%d-tx", in pic32_uart_startup()
528 sport->idx); in pic32_uart_startup()
529 if (!sport->irq_tx_name) { in pic32_uart_startup()
530 dev_err(port->dev, "%s: kasprintf err!", __func__); in pic32_uart_startup()
531 ret = -ENOMEM; in pic32_uart_startup()
534 irq_set_status_flags(sport->irq_tx, IRQ_NOAUTOEN); in pic32_uart_startup()
535 ret = request_irq(sport->irq_tx, pic32_uart_tx_interrupt, in pic32_uart_startup()
536 IRQF_NO_THREAD, sport->irq_tx_name, port); in pic32_uart_startup()
538 dev_err(port->dev, "%s: request irq(%d) err! ret:%d name:%s\n", in pic32_uart_startup()
539 __func__, sport->irq_tx, ret, in pic32_uart_startup()
559 enable_irq(sport->irq_rx); in pic32_uart_startup()
564 free_irq(sport->irq_tx, port); in pic32_uart_startup()
565 kfree(sport->irq_tx_name); in pic32_uart_startup()
567 free_irq(sport->irq_rx, port); in pic32_uart_startup()
568 kfree(sport->irq_rx_name); in pic32_uart_startup()
570 free_irq(sport->irq_fault, port); in pic32_uart_startup()
571 kfree(sport->irq_fault_name); in pic32_uart_startup()
573 clk_disable_unprepare(sport->clk); in pic32_uart_startup()
585 spin_lock_irqsave(&port->lock, flags); in pic32_uart_shutdown()
587 spin_unlock_irqrestore(&port->lock, flags); in pic32_uart_shutdown()
588 clk_disable_unprepare(sport->clk); in pic32_uart_shutdown()
591 free_irq(sport->irq_fault, port); in pic32_uart_shutdown()
592 kfree(sport->irq_fault_name); in pic32_uart_shutdown()
593 free_irq(sport->irq_tx, port); in pic32_uart_shutdown()
594 kfree(sport->irq_tx_name); in pic32_uart_shutdown()
595 free_irq(sport->irq_rx, port); in pic32_uart_shutdown()
596 kfree(sport->irq_rx_name); in pic32_uart_shutdown()
609 spin_lock_irqsave(&port->lock, flags); in pic32_uart_set_termios()
615 if (new->c_cflag & CSTOPB) in pic32_uart_set_termios()
623 if (new->c_cflag & PARENB) { in pic32_uart_set_termios()
624 if (new->c_cflag & PARODD) { in pic32_uart_set_termios()
641 if ((new->c_cflag & CRTSCTS) && sport->cts_gpiod) { in pic32_uart_set_termios()
659 /* Always 8-bit */ in pic32_uart_set_termios()
660 new->c_cflag |= CS8; in pic32_uart_set_termios()
663 new->c_cflag &= ~CMSPAR; in pic32_uart_set_termios()
666 baud = uart_get_baud_rate(port, new, old, 0, port->uartclk / 16); in pic32_uart_set_termios()
667 quot = uart_get_divisor(port, baud) - 1; in pic32_uart_set_termios()
669 uart_update_timeout(port, new->c_cflag, baud); in pic32_uart_set_termios()
677 spin_unlock_irqrestore(&port->lock, flags); in pic32_uart_set_termios()
683 struct platform_device *pdev = to_platform_device(port->dev); in pic32_uart_request_port()
688 return -EINVAL; in pic32_uart_request_port()
690 if (!request_mem_region(port->mapbase, resource_size(res_mem), in pic32_uart_request_port()
692 return -EBUSY; in pic32_uart_request_port()
694 port->membase = devm_ioremap(port->dev, port->mapbase, in pic32_uart_request_port()
696 if (!port->membase) { in pic32_uart_request_port()
697 dev_err(port->dev, "Unable to map registers\n"); in pic32_uart_request_port()
698 release_mem_region(port->mapbase, resource_size(res_mem)); in pic32_uart_request_port()
699 return -ENOMEM; in pic32_uart_request_port()
708 struct platform_device *pdev = to_platform_device(port->dev); in pic32_uart_release_port()
717 release_mem_region(port->mapbase, res_size); in pic32_uart_release_port()
720 /* serial core request to do any port required auto-configuration */
726 port->type = PORT_PIC32; in pic32_uart_config_port()
734 if (port->type != PORT_PIC32) in pic32_uart_verify_port()
735 return -EINVAL; in pic32_uart_verify_port()
736 if (port->irq != serinfo->irq) in pic32_uart_verify_port()
737 return -EINVAL; in pic32_uart_verify_port()
738 if (port->iotype != serinfo->io_type) in pic32_uart_verify_port()
739 return -EINVAL; in pic32_uart_verify_port()
740 if (port->mapbase != (unsigned long)serinfo->iomem_base) in pic32_uart_verify_port()
741 return -EINVAL; in pic32_uart_verify_port()
777 /* wait for tx empty */ in pic32_console_putchar()
787 struct pic32_sport *sport = pic32_sports[co->index]; in pic32_console_write()
790 uart_console_write(&sport->port, s, count, pic32_console_putchar); in pic32_console_write()
805 if (unlikely(co->index < 0 || co->index >= PIC32_MAX_UARTS)) in pic32_console_setup()
806 return -ENODEV; in pic32_console_setup()
808 sport = pic32_sports[co->index]; in pic32_console_setup()
810 return -ENODEV; in pic32_console_setup()
812 ret = clk_prepare_enable(sport->clk); in pic32_console_setup()
819 return uart_set_options(&sport->port, co, baud, parity, bits, flow); in pic32_console_setup()
829 .index = -1,
868 struct device *dev = &pdev->dev; in pic32_uart_probe()
869 struct device_node *np = dev->of_node; in pic32_uart_probe()
878 return -EINVAL; in pic32_uart_probe()
882 return -EINVAL; in pic32_uart_probe()
884 sport = devm_kzalloc(&pdev->dev, sizeof(*sport), GFP_KERNEL); in pic32_uart_probe()
886 return -ENOMEM; in pic32_uart_probe()
888 sport->idx = uart_idx; in pic32_uart_probe()
889 sport->irq_fault = irq_of_parse_and_map(np, 0); in pic32_uart_probe()
890 sport->irq_rx = irq_of_parse_and_map(np, 1); in pic32_uart_probe()
891 sport->irq_tx = irq_of_parse_and_map(np, 2); in pic32_uart_probe()
892 sport->clk = devm_clk_get(&pdev->dev, NULL); in pic32_uart_probe()
893 sport->dev = &pdev->dev; in pic32_uart_probe()
895 /* Hardware flow control: gpios in pic32_uart_probe()
898 sport->cts_gpiod = devm_gpiod_get_optional(dev, "cts", GPIOD_IN); in pic32_uart_probe()
899 if (IS_ERR(sport->cts_gpiod)) in pic32_uart_probe()
900 return dev_err_probe(dev, PTR_ERR(sport->cts_gpiod), "error requesting CTS GPIO\n"); in pic32_uart_probe()
901 gpiod_set_consumer_name(sport->cts_gpiod, "CTS"); in pic32_uart_probe()
904 port = &sport->port; in pic32_uart_probe()
905 port->iotype = UPIO_MEM; in pic32_uart_probe()
906 port->mapbase = res_mem->start; in pic32_uart_probe()
907 port->ops = &pic32_uart_ops; in pic32_uart_probe()
908 port->flags = UPF_BOOT_AUTOCONF; in pic32_uart_probe()
909 port->dev = &pdev->dev; in pic32_uart_probe()
910 port->fifosize = PIC32_UART_TX_FIFO_DEPTH; in pic32_uart_probe()
911 port->uartclk = clk_get_rate(sport->clk); in pic32_uart_probe()
912 port->line = uart_idx; in pic32_uart_probe()
916 port->membase = NULL; in pic32_uart_probe()
917 dev_err(port->dev, "%s: uart add port error!\n", __func__); in pic32_uart_probe()
926 clk_disable_unprepare(sport->clk); in pic32_uart_probe()
932 dev_info(&pdev->dev, "%s: uart(%d) driver initialized.\n", in pic32_uart_probe()
937 /* automatic unroll of sport and gpios */ in pic32_uart_probe()
947 clk_disable_unprepare(sport->clk); in pic32_uart_remove()
949 pic32_sports[sport->idx] = NULL; in pic32_uart_remove()
951 /* automatic unroll of sport and gpios */ in pic32_uart_remove()
956 { .compatible = "microchip,pic32mzda-uart" },
1002 MODULE_AUTHOR("Sorin-Andrei Pistirica <andrei.pistirica@microchip.com>");