Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 /*
0003  *  drivers/tty/serial/8250/8250_pxa.c -- driver for PXA on-board UARTS
0004  *  Copyright:  (C) 2013 Sergei Ianovich <ynvich@gmail.com>
0005  *
0006  *  replaces drivers/serial/pxa.c by Nicolas Pitre
0007  *  Created:    Feb 20, 2003
0008  *  Copyright:  (C) 2003 Monta Vista Software, Inc.
0009  *
0010  *  Based on drivers/serial/8250.c by Russell King.
0011  */
0012 
0013 #include <linux/device.h>
0014 #include <linux/init.h>
0015 #include <linux/io.h>
0016 #include <linux/module.h>
0017 #include <linux/serial_8250.h>
0018 #include <linux/serial_core.h>
0019 #include <linux/serial_reg.h>
0020 #include <linux/of.h>
0021 #include <linux/of_platform.h>
0022 #include <linux/platform_device.h>
0023 #include <linux/slab.h>
0024 #include <linux/clk.h>
0025 
0026 #include "8250.h"
0027 
0028 struct pxa8250_data {
0029     int         line;
0030     struct clk      *clk;
0031 };
0032 
0033 static int __maybe_unused serial_pxa_suspend(struct device *dev)
0034 {
0035     struct pxa8250_data *data = dev_get_drvdata(dev);
0036 
0037     serial8250_suspend_port(data->line);
0038 
0039     return 0;
0040 }
0041 
0042 static int __maybe_unused serial_pxa_resume(struct device *dev)
0043 {
0044     struct pxa8250_data *data = dev_get_drvdata(dev);
0045 
0046     serial8250_resume_port(data->line);
0047 
0048     return 0;
0049 }
0050 
0051 static const struct dev_pm_ops serial_pxa_pm_ops = {
0052     SET_SYSTEM_SLEEP_PM_OPS(serial_pxa_suspend, serial_pxa_resume)
0053 };
0054 
0055 static const struct of_device_id serial_pxa_dt_ids[] = {
0056     { .compatible = "mrvl,pxa-uart", },
0057     { .compatible = "mrvl,mmp-uart", },
0058     {}
0059 };
0060 MODULE_DEVICE_TABLE(of, serial_pxa_dt_ids);
0061 
0062 /* Uart divisor latch write */
0063 static void serial_pxa_dl_write(struct uart_8250_port *up, int value)
0064 {
0065     unsigned int dll;
0066 
0067     serial_out(up, UART_DLL, value & 0xff);
0068     /*
0069      * work around Erratum #74 according to Marvel(R) PXA270M Processor
0070      * Specification Update (April 19, 2010)
0071      */
0072     dll = serial_in(up, UART_DLL);
0073     WARN_ON(dll != (value & 0xff));
0074 
0075     serial_out(up, UART_DLM, value >> 8 & 0xff);
0076 }
0077 
0078 
0079 static void serial_pxa_pm(struct uart_port *port, unsigned int state,
0080           unsigned int oldstate)
0081 {
0082     struct pxa8250_data *data = port->private_data;
0083 
0084     if (!state)
0085         clk_prepare_enable(data->clk);
0086     else
0087         clk_disable_unprepare(data->clk);
0088 }
0089 
0090 static int serial_pxa_probe(struct platform_device *pdev)
0091 {
0092     struct uart_8250_port uart = {};
0093     struct pxa8250_data *data;
0094     struct resource *mmres;
0095     int irq, ret;
0096 
0097     irq = platform_get_irq(pdev, 0);
0098     if (irq < 0)
0099         return irq;
0100 
0101     mmres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
0102     if (!mmres)
0103         return -ENODEV;
0104 
0105     data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
0106     if (!data)
0107         return -ENOMEM;
0108 
0109     data->clk = devm_clk_get(&pdev->dev, NULL);
0110     if (IS_ERR(data->clk))
0111         return PTR_ERR(data->clk);
0112 
0113     ret = clk_prepare(data->clk);
0114     if (ret)
0115         return ret;
0116 
0117     ret = of_alias_get_id(pdev->dev.of_node, "serial");
0118     if (ret >= 0)
0119         uart.port.line = ret;
0120 
0121     uart.port.type = PORT_XSCALE;
0122     uart.port.iotype = UPIO_MEM32;
0123     uart.port.mapbase = mmres->start;
0124     uart.port.regshift = 2;
0125     uart.port.irq = irq;
0126     uart.port.fifosize = 64;
0127     uart.port.flags = UPF_IOREMAP | UPF_SKIP_TEST | UPF_FIXED_TYPE;
0128     uart.port.dev = &pdev->dev;
0129     uart.port.uartclk = clk_get_rate(data->clk);
0130     uart.port.pm = serial_pxa_pm;
0131     uart.port.private_data = data;
0132     uart.dl_write = serial_pxa_dl_write;
0133 
0134     ret = serial8250_register_8250_port(&uart);
0135     if (ret < 0)
0136         goto err_clk;
0137 
0138     data->line = ret;
0139 
0140     platform_set_drvdata(pdev, data);
0141 
0142     return 0;
0143 
0144  err_clk:
0145     clk_unprepare(data->clk);
0146     return ret;
0147 }
0148 
0149 static int serial_pxa_remove(struct platform_device *pdev)
0150 {
0151     struct pxa8250_data *data = platform_get_drvdata(pdev);
0152 
0153     serial8250_unregister_port(data->line);
0154 
0155     clk_unprepare(data->clk);
0156 
0157     return 0;
0158 }
0159 
0160 static struct platform_driver serial_pxa_driver = {
0161     .probe          = serial_pxa_probe,
0162     .remove         = serial_pxa_remove,
0163 
0164     .driver     = {
0165         .name   = "pxa2xx-uart",
0166         .pm = &serial_pxa_pm_ops,
0167         .of_match_table = serial_pxa_dt_ids,
0168     },
0169 };
0170 
0171 module_platform_driver(serial_pxa_driver);
0172 
0173 #ifdef CONFIG_SERIAL_8250_CONSOLE
0174 static int __init early_serial_pxa_setup(struct earlycon_device *device,
0175                   const char *options)
0176 {
0177     struct uart_port *port = &device->port;
0178 
0179     if (!(device->port.membase || device->port.iobase))
0180         return -ENODEV;
0181 
0182     port->regshift = 2;
0183     return early_serial8250_setup(device, NULL);
0184 }
0185 OF_EARLYCON_DECLARE(early_pxa, "mrvl,pxa-uart", early_serial_pxa_setup);
0186 #endif
0187 
0188 MODULE_AUTHOR("Sergei Ianovich");
0189 MODULE_LICENSE("GPL");
0190 MODULE_ALIAS("platform:pxa2xx-uart");