GNU Linux-libre 5.10.215-gnu1
[releases.git] / drivers / tty / serial / 8250 / 8250_pxa.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  *  drivers/tty/serial/8250/8250_pxa.c -- driver for PXA on-board UARTS
4  *  Copyright:  (C) 2013 Sergei Ianovich <ynvich@gmail.com>
5  *
6  *  replaces drivers/serial/pxa.c by Nicolas Pitre
7  *  Created:    Feb 20, 2003
8  *  Copyright:  (C) 2003 Monta Vista Software, Inc.
9  *
10  *  Based on drivers/serial/8250.c by Russell King.
11  */
12
13 #include <linux/device.h>
14 #include <linux/init.h>
15 #include <linux/io.h>
16 #include <linux/module.h>
17 #include <linux/serial_8250.h>
18 #include <linux/serial_core.h>
19 #include <linux/serial_reg.h>
20 #include <linux/of.h>
21 #include <linux/of_platform.h>
22 #include <linux/platform_device.h>
23 #include <linux/slab.h>
24 #include <linux/clk.h>
25 #include <linux/pm_runtime.h>
26
27 #include "8250.h"
28
29 struct pxa8250_data {
30         int                     line;
31         struct clk              *clk;
32 };
33
34 static int __maybe_unused serial_pxa_suspend(struct device *dev)
35 {
36         struct pxa8250_data *data = dev_get_drvdata(dev);
37
38         serial8250_suspend_port(data->line);
39
40         return 0;
41 }
42
43 static int __maybe_unused serial_pxa_resume(struct device *dev)
44 {
45         struct pxa8250_data *data = dev_get_drvdata(dev);
46
47         serial8250_resume_port(data->line);
48
49         return 0;
50 }
51
52 static const struct dev_pm_ops serial_pxa_pm_ops = {
53         SET_SYSTEM_SLEEP_PM_OPS(serial_pxa_suspend, serial_pxa_resume)
54 };
55
56 static const struct of_device_id serial_pxa_dt_ids[] = {
57         { .compatible = "mrvl,pxa-uart", },
58         { .compatible = "mrvl,mmp-uart", },
59         {}
60 };
61 MODULE_DEVICE_TABLE(of, serial_pxa_dt_ids);
62
63 /* Uart divisor latch write */
64 static void serial_pxa_dl_write(struct uart_8250_port *up, int value)
65 {
66         unsigned int dll;
67
68         serial_out(up, UART_DLL, value & 0xff);
69         /*
70          * work around Erratum #74 according to Marvel(R) PXA270M Processor
71          * Specification Update (April 19, 2010)
72          */
73         dll = serial_in(up, UART_DLL);
74         WARN_ON(dll != (value & 0xff));
75
76         serial_out(up, UART_DLM, value >> 8 & 0xff);
77 }
78
79
80 static void serial_pxa_pm(struct uart_port *port, unsigned int state,
81               unsigned int oldstate)
82 {
83         struct pxa8250_data *data = port->private_data;
84
85         if (!state)
86                 clk_prepare_enable(data->clk);
87         else
88                 clk_disable_unprepare(data->clk);
89 }
90
91 static int serial_pxa_probe(struct platform_device *pdev)
92 {
93         struct uart_8250_port uart = {};
94         struct pxa8250_data *data;
95         struct resource *mmres;
96         int irq, ret;
97
98         irq = platform_get_irq(pdev, 0);
99         if (irq < 0)
100                 return irq;
101
102         mmres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
103         if (!mmres)
104                 return -ENODEV;
105
106         data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
107         if (!data)
108                 return -ENOMEM;
109
110         data->clk = devm_clk_get(&pdev->dev, NULL);
111         if (IS_ERR(data->clk))
112                 return PTR_ERR(data->clk);
113
114         ret = clk_prepare(data->clk);
115         if (ret)
116                 return ret;
117
118         ret = of_alias_get_id(pdev->dev.of_node, "serial");
119         if (ret >= 0)
120                 uart.port.line = ret;
121
122         uart.port.type = PORT_XSCALE;
123         uart.port.iotype = UPIO_MEM32;
124         uart.port.mapbase = mmres->start;
125         uart.port.regshift = 2;
126         uart.port.irq = irq;
127         uart.port.fifosize = 64;
128         uart.port.flags = UPF_IOREMAP | UPF_SKIP_TEST | UPF_FIXED_TYPE;
129         uart.port.dev = &pdev->dev;
130         uart.port.uartclk = clk_get_rate(data->clk);
131         uart.port.pm = serial_pxa_pm;
132         uart.port.private_data = data;
133         uart.dl_write = serial_pxa_dl_write;
134
135         ret = serial8250_register_8250_port(&uart);
136         if (ret < 0)
137                 goto err_clk;
138
139         data->line = ret;
140
141         platform_set_drvdata(pdev, data);
142
143         return 0;
144
145  err_clk:
146         clk_unprepare(data->clk);
147         return ret;
148 }
149
150 static int serial_pxa_remove(struct platform_device *pdev)
151 {
152         struct pxa8250_data *data = platform_get_drvdata(pdev);
153
154         serial8250_unregister_port(data->line);
155
156         clk_unprepare(data->clk);
157
158         return 0;
159 }
160
161 static struct platform_driver serial_pxa_driver = {
162         .probe          = serial_pxa_probe,
163         .remove         = serial_pxa_remove,
164
165         .driver         = {
166                 .name   = "pxa2xx-uart",
167                 .pm     = &serial_pxa_pm_ops,
168                 .of_match_table = serial_pxa_dt_ids,
169         },
170 };
171
172 module_platform_driver(serial_pxa_driver);
173
174 #ifdef CONFIG_SERIAL_8250_CONSOLE
175 static int __init early_serial_pxa_setup(struct earlycon_device *device,
176                                   const char *options)
177 {
178         struct uart_port *port = &device->port;
179
180         if (!(device->port.membase || device->port.iobase))
181                 return -ENODEV;
182
183         port->regshift = 2;
184         return early_serial8250_setup(device, NULL);
185 }
186 OF_EARLYCON_DECLARE(early_pxa, "mrvl,pxa-uart", early_serial_pxa_setup);
187 #endif
188
189 MODULE_AUTHOR("Sergei Ianovich");
190 MODULE_LICENSE("GPL");
191 MODULE_ALIAS("platform:pxa2xx-uart");