18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Serial port driver for NXP LPC18xx/43xx UART 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (C) 2015 Joachim Eastwood <manabian@gmail.com> 68c2ecf20Sopenharmony_ci * 78c2ecf20Sopenharmony_ci * Based on 8250_mtk.c: 88c2ecf20Sopenharmony_ci * Copyright (c) 2014 MundoReader S.L. 98c2ecf20Sopenharmony_ci * Matthias Brugger <matthias.bgg@gmail.com> 108c2ecf20Sopenharmony_ci */ 118c2ecf20Sopenharmony_ci 128c2ecf20Sopenharmony_ci#include <linux/clk.h> 138c2ecf20Sopenharmony_ci#include <linux/io.h> 148c2ecf20Sopenharmony_ci#include <linux/module.h> 158c2ecf20Sopenharmony_ci#include <linux/of.h> 168c2ecf20Sopenharmony_ci#include <linux/platform_device.h> 178c2ecf20Sopenharmony_ci 188c2ecf20Sopenharmony_ci#include "8250.h" 198c2ecf20Sopenharmony_ci 208c2ecf20Sopenharmony_ci/* Additional LPC18xx/43xx 8250 registers and bits */ 218c2ecf20Sopenharmony_ci#define LPC18XX_UART_RS485CTRL (0x04c / sizeof(u32)) 228c2ecf20Sopenharmony_ci#define LPC18XX_UART_RS485CTRL_NMMEN BIT(0) 238c2ecf20Sopenharmony_ci#define LPC18XX_UART_RS485CTRL_DCTRL BIT(4) 248c2ecf20Sopenharmony_ci#define LPC18XX_UART_RS485CTRL_OINV BIT(5) 258c2ecf20Sopenharmony_ci#define LPC18XX_UART_RS485DLY (0x054 / sizeof(u32)) 268c2ecf20Sopenharmony_ci#define LPC18XX_UART_RS485DLY_MAX 255 278c2ecf20Sopenharmony_ci 288c2ecf20Sopenharmony_cistruct lpc18xx_uart_data { 298c2ecf20Sopenharmony_ci struct uart_8250_dma dma; 308c2ecf20Sopenharmony_ci struct clk *clk_uart; 318c2ecf20Sopenharmony_ci struct clk *clk_reg; 328c2ecf20Sopenharmony_ci int line; 338c2ecf20Sopenharmony_ci}; 348c2ecf20Sopenharmony_ci 358c2ecf20Sopenharmony_cistatic int lpc18xx_rs485_config(struct uart_port *port, 368c2ecf20Sopenharmony_ci struct serial_rs485 *rs485) 378c2ecf20Sopenharmony_ci{ 388c2ecf20Sopenharmony_ci struct uart_8250_port *up = up_to_u8250p(port); 398c2ecf20Sopenharmony_ci u32 rs485_ctrl_reg = 0; 408c2ecf20Sopenharmony_ci u32 rs485_dly_reg = 0; 418c2ecf20Sopenharmony_ci unsigned baud_clk; 428c2ecf20Sopenharmony_ci 438c2ecf20Sopenharmony_ci if (rs485->flags & SER_RS485_ENABLED) 448c2ecf20Sopenharmony_ci memset(rs485->padding, 0, sizeof(rs485->padding)); 458c2ecf20Sopenharmony_ci else 468c2ecf20Sopenharmony_ci memset(rs485, 0, sizeof(*rs485)); 478c2ecf20Sopenharmony_ci 488c2ecf20Sopenharmony_ci rs485->flags &= SER_RS485_ENABLED | SER_RS485_RTS_ON_SEND | 498c2ecf20Sopenharmony_ci SER_RS485_RTS_AFTER_SEND; 508c2ecf20Sopenharmony_ci 518c2ecf20Sopenharmony_ci if (rs485->flags & SER_RS485_ENABLED) { 528c2ecf20Sopenharmony_ci rs485_ctrl_reg |= LPC18XX_UART_RS485CTRL_NMMEN | 538c2ecf20Sopenharmony_ci LPC18XX_UART_RS485CTRL_DCTRL; 548c2ecf20Sopenharmony_ci 558c2ecf20Sopenharmony_ci if (rs485->flags & SER_RS485_RTS_ON_SEND) { 568c2ecf20Sopenharmony_ci rs485_ctrl_reg |= LPC18XX_UART_RS485CTRL_OINV; 578c2ecf20Sopenharmony_ci rs485->flags &= ~SER_RS485_RTS_AFTER_SEND; 588c2ecf20Sopenharmony_ci } else { 598c2ecf20Sopenharmony_ci rs485->flags |= SER_RS485_RTS_AFTER_SEND; 608c2ecf20Sopenharmony_ci } 618c2ecf20Sopenharmony_ci } 628c2ecf20Sopenharmony_ci 638c2ecf20Sopenharmony_ci if (rs485->delay_rts_after_send) { 648c2ecf20Sopenharmony_ci baud_clk = port->uartclk / up->dl_read(up); 658c2ecf20Sopenharmony_ci rs485_dly_reg = DIV_ROUND_UP(rs485->delay_rts_after_send 668c2ecf20Sopenharmony_ci * baud_clk, MSEC_PER_SEC); 678c2ecf20Sopenharmony_ci 688c2ecf20Sopenharmony_ci if (rs485_dly_reg > LPC18XX_UART_RS485DLY_MAX) 698c2ecf20Sopenharmony_ci rs485_dly_reg = LPC18XX_UART_RS485DLY_MAX; 708c2ecf20Sopenharmony_ci 718c2ecf20Sopenharmony_ci /* Calculate the resulting delay in ms */ 728c2ecf20Sopenharmony_ci rs485->delay_rts_after_send = (rs485_dly_reg * MSEC_PER_SEC) 738c2ecf20Sopenharmony_ci / baud_clk; 748c2ecf20Sopenharmony_ci } 758c2ecf20Sopenharmony_ci 768c2ecf20Sopenharmony_ci /* Delay RTS before send not supported */ 778c2ecf20Sopenharmony_ci rs485->delay_rts_before_send = 0; 788c2ecf20Sopenharmony_ci 798c2ecf20Sopenharmony_ci serial_out(up, LPC18XX_UART_RS485CTRL, rs485_ctrl_reg); 808c2ecf20Sopenharmony_ci serial_out(up, LPC18XX_UART_RS485DLY, rs485_dly_reg); 818c2ecf20Sopenharmony_ci 828c2ecf20Sopenharmony_ci port->rs485 = *rs485; 838c2ecf20Sopenharmony_ci 848c2ecf20Sopenharmony_ci return 0; 858c2ecf20Sopenharmony_ci} 868c2ecf20Sopenharmony_ci 878c2ecf20Sopenharmony_cistatic void lpc18xx_uart_serial_out(struct uart_port *p, int offset, int value) 888c2ecf20Sopenharmony_ci{ 898c2ecf20Sopenharmony_ci /* 908c2ecf20Sopenharmony_ci * For DMA mode one must ensure that the UART_FCR_DMA_SELECT 918c2ecf20Sopenharmony_ci * bit is set when FIFO is enabled. Even if DMA is not used 928c2ecf20Sopenharmony_ci * setting this bit doesn't seem to affect anything. 938c2ecf20Sopenharmony_ci */ 948c2ecf20Sopenharmony_ci if (offset == UART_FCR && (value & UART_FCR_ENABLE_FIFO)) 958c2ecf20Sopenharmony_ci value |= UART_FCR_DMA_SELECT; 968c2ecf20Sopenharmony_ci 978c2ecf20Sopenharmony_ci offset = offset << p->regshift; 988c2ecf20Sopenharmony_ci writel(value, p->membase + offset); 998c2ecf20Sopenharmony_ci} 1008c2ecf20Sopenharmony_ci 1018c2ecf20Sopenharmony_cistatic int lpc18xx_serial_probe(struct platform_device *pdev) 1028c2ecf20Sopenharmony_ci{ 1038c2ecf20Sopenharmony_ci struct lpc18xx_uart_data *data; 1048c2ecf20Sopenharmony_ci struct uart_8250_port uart; 1058c2ecf20Sopenharmony_ci struct resource *res; 1068c2ecf20Sopenharmony_ci int irq, ret; 1078c2ecf20Sopenharmony_ci 1088c2ecf20Sopenharmony_ci irq = platform_get_irq(pdev, 0); 1098c2ecf20Sopenharmony_ci if (irq < 0) 1108c2ecf20Sopenharmony_ci return irq; 1118c2ecf20Sopenharmony_ci 1128c2ecf20Sopenharmony_ci res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 1138c2ecf20Sopenharmony_ci if (!res) { 1148c2ecf20Sopenharmony_ci dev_err(&pdev->dev, "memory resource not found"); 1158c2ecf20Sopenharmony_ci return -EINVAL; 1168c2ecf20Sopenharmony_ci } 1178c2ecf20Sopenharmony_ci 1188c2ecf20Sopenharmony_ci memset(&uart, 0, sizeof(uart)); 1198c2ecf20Sopenharmony_ci 1208c2ecf20Sopenharmony_ci uart.port.membase = devm_ioremap(&pdev->dev, res->start, 1218c2ecf20Sopenharmony_ci resource_size(res)); 1228c2ecf20Sopenharmony_ci if (!uart.port.membase) 1238c2ecf20Sopenharmony_ci return -ENOMEM; 1248c2ecf20Sopenharmony_ci 1258c2ecf20Sopenharmony_ci data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL); 1268c2ecf20Sopenharmony_ci if (!data) 1278c2ecf20Sopenharmony_ci return -ENOMEM; 1288c2ecf20Sopenharmony_ci 1298c2ecf20Sopenharmony_ci data->clk_uart = devm_clk_get(&pdev->dev, "uartclk"); 1308c2ecf20Sopenharmony_ci if (IS_ERR(data->clk_uart)) { 1318c2ecf20Sopenharmony_ci dev_err(&pdev->dev, "uart clock not found\n"); 1328c2ecf20Sopenharmony_ci return PTR_ERR(data->clk_uart); 1338c2ecf20Sopenharmony_ci } 1348c2ecf20Sopenharmony_ci 1358c2ecf20Sopenharmony_ci data->clk_reg = devm_clk_get(&pdev->dev, "reg"); 1368c2ecf20Sopenharmony_ci if (IS_ERR(data->clk_reg)) { 1378c2ecf20Sopenharmony_ci dev_err(&pdev->dev, "reg clock not found\n"); 1388c2ecf20Sopenharmony_ci return PTR_ERR(data->clk_reg); 1398c2ecf20Sopenharmony_ci } 1408c2ecf20Sopenharmony_ci 1418c2ecf20Sopenharmony_ci ret = clk_prepare_enable(data->clk_reg); 1428c2ecf20Sopenharmony_ci if (ret) { 1438c2ecf20Sopenharmony_ci dev_err(&pdev->dev, "unable to enable reg clock\n"); 1448c2ecf20Sopenharmony_ci return ret; 1458c2ecf20Sopenharmony_ci } 1468c2ecf20Sopenharmony_ci 1478c2ecf20Sopenharmony_ci ret = clk_prepare_enable(data->clk_uart); 1488c2ecf20Sopenharmony_ci if (ret) { 1498c2ecf20Sopenharmony_ci dev_err(&pdev->dev, "unable to enable uart clock\n"); 1508c2ecf20Sopenharmony_ci goto dis_clk_reg; 1518c2ecf20Sopenharmony_ci } 1528c2ecf20Sopenharmony_ci 1538c2ecf20Sopenharmony_ci ret = of_alias_get_id(pdev->dev.of_node, "serial"); 1548c2ecf20Sopenharmony_ci if (ret >= 0) 1558c2ecf20Sopenharmony_ci uart.port.line = ret; 1568c2ecf20Sopenharmony_ci 1578c2ecf20Sopenharmony_ci data->dma.rx_param = data; 1588c2ecf20Sopenharmony_ci data->dma.tx_param = data; 1598c2ecf20Sopenharmony_ci 1608c2ecf20Sopenharmony_ci spin_lock_init(&uart.port.lock); 1618c2ecf20Sopenharmony_ci uart.port.dev = &pdev->dev; 1628c2ecf20Sopenharmony_ci uart.port.irq = irq; 1638c2ecf20Sopenharmony_ci uart.port.iotype = UPIO_MEM32; 1648c2ecf20Sopenharmony_ci uart.port.mapbase = res->start; 1658c2ecf20Sopenharmony_ci uart.port.regshift = 2; 1668c2ecf20Sopenharmony_ci uart.port.type = PORT_16550A; 1678c2ecf20Sopenharmony_ci uart.port.flags = UPF_FIXED_PORT | UPF_FIXED_TYPE | UPF_SKIP_TEST; 1688c2ecf20Sopenharmony_ci uart.port.uartclk = clk_get_rate(data->clk_uart); 1698c2ecf20Sopenharmony_ci uart.port.private_data = data; 1708c2ecf20Sopenharmony_ci uart.port.rs485_config = lpc18xx_rs485_config; 1718c2ecf20Sopenharmony_ci uart.port.serial_out = lpc18xx_uart_serial_out; 1728c2ecf20Sopenharmony_ci 1738c2ecf20Sopenharmony_ci uart.dma = &data->dma; 1748c2ecf20Sopenharmony_ci uart.dma->rxconf.src_maxburst = 1; 1758c2ecf20Sopenharmony_ci uart.dma->txconf.dst_maxburst = 1; 1768c2ecf20Sopenharmony_ci 1778c2ecf20Sopenharmony_ci ret = serial8250_register_8250_port(&uart); 1788c2ecf20Sopenharmony_ci if (ret < 0) { 1798c2ecf20Sopenharmony_ci dev_err(&pdev->dev, "unable to register 8250 port\n"); 1808c2ecf20Sopenharmony_ci goto dis_uart_clk; 1818c2ecf20Sopenharmony_ci } 1828c2ecf20Sopenharmony_ci 1838c2ecf20Sopenharmony_ci data->line = ret; 1848c2ecf20Sopenharmony_ci platform_set_drvdata(pdev, data); 1858c2ecf20Sopenharmony_ci 1868c2ecf20Sopenharmony_ci return 0; 1878c2ecf20Sopenharmony_ci 1888c2ecf20Sopenharmony_cidis_uart_clk: 1898c2ecf20Sopenharmony_ci clk_disable_unprepare(data->clk_uart); 1908c2ecf20Sopenharmony_cidis_clk_reg: 1918c2ecf20Sopenharmony_ci clk_disable_unprepare(data->clk_reg); 1928c2ecf20Sopenharmony_ci return ret; 1938c2ecf20Sopenharmony_ci} 1948c2ecf20Sopenharmony_ci 1958c2ecf20Sopenharmony_cistatic int lpc18xx_serial_remove(struct platform_device *pdev) 1968c2ecf20Sopenharmony_ci{ 1978c2ecf20Sopenharmony_ci struct lpc18xx_uart_data *data = platform_get_drvdata(pdev); 1988c2ecf20Sopenharmony_ci 1998c2ecf20Sopenharmony_ci serial8250_unregister_port(data->line); 2008c2ecf20Sopenharmony_ci clk_disable_unprepare(data->clk_uart); 2018c2ecf20Sopenharmony_ci clk_disable_unprepare(data->clk_reg); 2028c2ecf20Sopenharmony_ci 2038c2ecf20Sopenharmony_ci return 0; 2048c2ecf20Sopenharmony_ci} 2058c2ecf20Sopenharmony_ci 2068c2ecf20Sopenharmony_cistatic const struct of_device_id lpc18xx_serial_match[] = { 2078c2ecf20Sopenharmony_ci { .compatible = "nxp,lpc1850-uart" }, 2088c2ecf20Sopenharmony_ci { }, 2098c2ecf20Sopenharmony_ci}; 2108c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, lpc18xx_serial_match); 2118c2ecf20Sopenharmony_ci 2128c2ecf20Sopenharmony_cistatic struct platform_driver lpc18xx_serial_driver = { 2138c2ecf20Sopenharmony_ci .probe = lpc18xx_serial_probe, 2148c2ecf20Sopenharmony_ci .remove = lpc18xx_serial_remove, 2158c2ecf20Sopenharmony_ci .driver = { 2168c2ecf20Sopenharmony_ci .name = "lpc18xx-uart", 2178c2ecf20Sopenharmony_ci .of_match_table = lpc18xx_serial_match, 2188c2ecf20Sopenharmony_ci }, 2198c2ecf20Sopenharmony_ci}; 2208c2ecf20Sopenharmony_cimodule_platform_driver(lpc18xx_serial_driver); 2218c2ecf20Sopenharmony_ci 2228c2ecf20Sopenharmony_ciMODULE_AUTHOR("Joachim Eastwood <manabian@gmail.com>"); 2238c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Serial port driver NXP LPC18xx/43xx devices"); 2248c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2"); 225