1
2
3
4
5
6
7
8#include <linux/device.h>
9#include <linux/io.h>
10#include <linux/module.h>
11#include <linux/serial_8250.h>
12#include <linux/serial_reg.h>
13#include <linux/platform_device.h>
14#include <linux/clk.h>
15#include <linux/slab.h>
16
17#include "8250.h"
18
19#define UART_DLL_EM 9
20#define UART_DLM_EM 10
21
22struct serial8250_em_priv {
23 struct clk *sclk;
24 int line;
25};
26
27static void serial8250_em_serial_out(struct uart_port *p, int offset, int value)
28{
29 switch (offset) {
30 case UART_TX:
31 writeb(value, p->membase);
32 break;
33 case UART_FCR:
34 case UART_LCR:
35 case UART_MCR:
36 case UART_SCR:
37 writel(value, p->membase + ((offset + 1) << 2));
38 break;
39 case UART_IER:
40 value &= 0x0f;
41
42 case UART_DLL_EM:
43 case UART_DLM_EM:
44 writel(value, p->membase + (offset << 2));
45 }
46}
47
48static unsigned int serial8250_em_serial_in(struct uart_port *p, int offset)
49{
50 switch (offset) {
51 case UART_RX:
52 return readb(p->membase);
53 case UART_MCR:
54 case UART_LSR:
55 case UART_MSR:
56 case UART_SCR:
57 return readl(p->membase + ((offset + 1) << 2));
58 case UART_IER:
59 case UART_IIR:
60 case UART_DLL_EM:
61 case UART_DLM_EM:
62 return readl(p->membase + (offset << 2));
63 }
64 return 0;
65}
66
67static int serial8250_em_serial_dl_read(struct uart_8250_port *up)
68{
69 return serial_in(up, UART_DLL_EM) | serial_in(up, UART_DLM_EM) << 8;
70}
71
72static void serial8250_em_serial_dl_write(struct uart_8250_port *up, int value)
73{
74 serial_out(up, UART_DLL_EM, value & 0xff);
75 serial_out(up, UART_DLM_EM, value >> 8 & 0xff);
76}
77
78static int serial8250_em_probe(struct platform_device *pdev)
79{
80 struct resource *regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
81 struct resource *irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
82 struct serial8250_em_priv *priv;
83 struct uart_8250_port up;
84 int ret;
85
86 if (!regs || !irq) {
87 dev_err(&pdev->dev, "missing registers or irq\n");
88 return -EINVAL;
89 }
90
91 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
92 if (!priv)
93 return -ENOMEM;
94
95 priv->sclk = devm_clk_get(&pdev->dev, "sclk");
96 if (IS_ERR(priv->sclk)) {
97 dev_err(&pdev->dev, "unable to get clock\n");
98 return PTR_ERR(priv->sclk);
99 }
100
101 memset(&up, 0, sizeof(up));
102 up.port.mapbase = regs->start;
103 up.port.irq = irq->start;
104 up.port.type = PORT_UNKNOWN;
105 up.port.flags = UPF_BOOT_AUTOCONF | UPF_FIXED_PORT | UPF_IOREMAP;
106 up.port.dev = &pdev->dev;
107 up.port.private_data = priv;
108
109 clk_prepare_enable(priv->sclk);
110 up.port.uartclk = clk_get_rate(priv->sclk);
111
112 up.port.iotype = UPIO_MEM32;
113 up.port.serial_in = serial8250_em_serial_in;
114 up.port.serial_out = serial8250_em_serial_out;
115 up.dl_read = serial8250_em_serial_dl_read;
116 up.dl_write = serial8250_em_serial_dl_write;
117
118 ret = serial8250_register_8250_port(&up);
119 if (ret < 0) {
120 dev_err(&pdev->dev, "unable to register 8250 port\n");
121 clk_disable_unprepare(priv->sclk);
122 return ret;
123 }
124
125 priv->line = ret;
126 platform_set_drvdata(pdev, priv);
127 return 0;
128}
129
130static int serial8250_em_remove(struct platform_device *pdev)
131{
132 struct serial8250_em_priv *priv = platform_get_drvdata(pdev);
133
134 serial8250_unregister_port(priv->line);
135 clk_disable_unprepare(priv->sclk);
136 return 0;
137}
138
139static const struct of_device_id serial8250_em_dt_ids[] = {
140 { .compatible = "renesas,em-uart", },
141 {},
142};
143MODULE_DEVICE_TABLE(of, serial8250_em_dt_ids);
144
145static struct platform_driver serial8250_em_platform_driver = {
146 .driver = {
147 .name = "serial8250-em",
148 .of_match_table = serial8250_em_dt_ids,
149 },
150 .probe = serial8250_em_probe,
151 .remove = serial8250_em_remove,
152};
153
154module_platform_driver(serial8250_em_platform_driver);
155
156MODULE_AUTHOR("Magnus Damm");
157MODULE_DESCRIPTION("Renesas Emma Mobile 8250 Driver");
158MODULE_LICENSE("GPL v2");
159