xref: /openbmc/linux/drivers/tty/serial/8250/8250_em.c (revision b34e08d5)
1 /*
2  * Renesas Emma Mobile 8250 driver
3  *
4  *  Copyright (C) 2012 Magnus Damm
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  */
19 
20 #include <linux/device.h>
21 #include <linux/io.h>
22 #include <linux/module.h>
23 #include <linux/serial_8250.h>
24 #include <linux/serial_core.h>
25 #include <linux/serial_reg.h>
26 #include <linux/platform_device.h>
27 #include <linux/clk.h>
28 #include <linux/slab.h>
29 
30 #include "8250.h"
31 
32 #define UART_DLL_EM 9
33 #define UART_DLM_EM 10
34 
35 struct serial8250_em_priv {
36 	struct clk *sclk;
37 	int line;
38 };
39 
40 static void serial8250_em_serial_out(struct uart_port *p, int offset, int value)
41 {
42 	switch (offset) {
43 	case UART_TX: /* TX @ 0x00 */
44 		writeb(value, p->membase);
45 		break;
46 	case UART_FCR: /* FCR @ 0x0c (+1) */
47 	case UART_LCR: /* LCR @ 0x10 (+1) */
48 	case UART_MCR: /* MCR @ 0x14 (+1) */
49 	case UART_SCR: /* SCR @ 0x20 (+1) */
50 		writel(value, p->membase + ((offset + 1) << 2));
51 		break;
52 	case UART_IER: /* IER @ 0x04 */
53 		value &= 0x0f; /* only 4 valid bits - not Xscale */
54 		/* fall-through */
55 	case UART_DLL_EM: /* DLL @ 0x24 (+9) */
56 	case UART_DLM_EM: /* DLM @ 0x28 (+9) */
57 		writel(value, p->membase + (offset << 2));
58 	}
59 }
60 
61 static unsigned int serial8250_em_serial_in(struct uart_port *p, int offset)
62 {
63 	switch (offset) {
64 	case UART_RX: /* RX @ 0x00 */
65 		return readb(p->membase);
66 	case UART_MCR: /* MCR @ 0x14 (+1) */
67 	case UART_LSR: /* LSR @ 0x18 (+1) */
68 	case UART_MSR: /* MSR @ 0x1c (+1) */
69 	case UART_SCR: /* SCR @ 0x20 (+1) */
70 		return readl(p->membase + ((offset + 1) << 2));
71 	case UART_IER: /* IER @ 0x04 */
72 	case UART_IIR: /* IIR @ 0x08 */
73 	case UART_DLL_EM: /* DLL @ 0x24 (+9) */
74 	case UART_DLM_EM: /* DLM @ 0x28 (+9) */
75 		return readl(p->membase + (offset << 2));
76 	}
77 	return 0;
78 }
79 
80 static int serial8250_em_serial_dl_read(struct uart_8250_port *up)
81 {
82 	return serial_in(up, UART_DLL_EM) | serial_in(up, UART_DLM_EM) << 8;
83 }
84 
85 static void serial8250_em_serial_dl_write(struct uart_8250_port *up, int value)
86 {
87 	serial_out(up, UART_DLL_EM, value & 0xff);
88 	serial_out(up, UART_DLM_EM, value >> 8 & 0xff);
89 }
90 
91 static int serial8250_em_probe(struct platform_device *pdev)
92 {
93 	struct resource *regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
94 	struct resource *irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
95 	struct serial8250_em_priv *priv;
96 	struct uart_8250_port up;
97 	int ret;
98 
99 	if (!regs || !irq) {
100 		dev_err(&pdev->dev, "missing registers or irq\n");
101 		return -EINVAL;
102 	}
103 
104 	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
105 	if (!priv) {
106 		dev_err(&pdev->dev, "unable to allocate private data\n");
107 		return -ENOMEM;
108 	}
109 
110 	priv->sclk = devm_clk_get(&pdev->dev, "sclk");
111 	if (IS_ERR(priv->sclk)) {
112 		dev_err(&pdev->dev, "unable to get clock\n");
113 		return PTR_ERR(priv->sclk);
114 	}
115 
116 	memset(&up, 0, sizeof(up));
117 	up.port.mapbase = regs->start;
118 	up.port.irq = irq->start;
119 	up.port.type = PORT_UNKNOWN;
120 	up.port.flags = UPF_BOOT_AUTOCONF | UPF_FIXED_PORT | UPF_IOREMAP;
121 	up.port.dev = &pdev->dev;
122 	up.port.private_data = priv;
123 
124 	clk_prepare_enable(priv->sclk);
125 	up.port.uartclk = clk_get_rate(priv->sclk);
126 
127 	up.port.iotype = UPIO_MEM32;
128 	up.port.serial_in = serial8250_em_serial_in;
129 	up.port.serial_out = serial8250_em_serial_out;
130 	up.dl_read = serial8250_em_serial_dl_read;
131 	up.dl_write = serial8250_em_serial_dl_write;
132 
133 	ret = serial8250_register_8250_port(&up);
134 	if (ret < 0) {
135 		dev_err(&pdev->dev, "unable to register 8250 port\n");
136 		clk_disable_unprepare(priv->sclk);
137 		return ret;
138 	}
139 
140 	priv->line = ret;
141 	platform_set_drvdata(pdev, priv);
142 	return 0;
143 }
144 
145 static int serial8250_em_remove(struct platform_device *pdev)
146 {
147 	struct serial8250_em_priv *priv = platform_get_drvdata(pdev);
148 
149 	serial8250_unregister_port(priv->line);
150 	clk_disable_unprepare(priv->sclk);
151 	return 0;
152 }
153 
154 static const struct of_device_id serial8250_em_dt_ids[] = {
155 	{ .compatible = "renesas,em-uart", },
156 	{},
157 };
158 MODULE_DEVICE_TABLE(of, serial8250_em_dt_ids);
159 
160 static struct platform_driver serial8250_em_platform_driver = {
161 	.driver = {
162 		.name		= "serial8250-em",
163 		.of_match_table = serial8250_em_dt_ids,
164 		.owner		= THIS_MODULE,
165 	},
166 	.probe			= serial8250_em_probe,
167 	.remove			= serial8250_em_remove,
168 };
169 
170 module_platform_driver(serial8250_em_platform_driver);
171 
172 MODULE_AUTHOR("Magnus Damm");
173 MODULE_DESCRIPTION("Renesas Emma Mobile 8250 Driver");
174 MODULE_LICENSE("GPL v2");
175