2 * Serial Port driver for Aspeed VUART device
4 * Copyright (C) 2016 Jeremy Kerr <jk@ozlabs.org>, IBM Corp.
5 * Copyright (C) 2006 Arnd Bergmann <arnd@arndb.de>, IBM Corp.
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
12 #include <linux/device.h>
13 #include <linux/module.h>
14 #include <linux/of_address.h>
15 #include <linux/of_irq.h>
16 #include <linux/of_platform.h>
17 #include <linux/clk.h>
21 #define ASPEED_VUART_GCRA 0x20
22 #define ASPEED_VUART_GCRA_VUART_EN BIT(0)
23 #define ASPEED_VUART_GCRA_DISABLE_HOST_TX_DISCARD BIT(5)
24 #define ASPEED_VUART_GCRB 0x24
25 #define ASPEED_VUART_GCRB_HOST_SIRQ_MASK GENMASK(7, 4)
26 #define ASPEED_VUART_GCRB_HOST_SIRQ_SHIFT 4
27 #define ASPEED_VUART_ADDRL 0x28
28 #define ASPEED_VUART_ADDRH 0x2c
38 * The VUART is basically two UART 'front ends' connected by their FIFO
39 * (no actual serial line in between). One is on the BMC side (management
40 * controller) and one is on the host CPU side.
42 * It allows the BMC to provide to the host a "UART" that pipes into
43 * the BMC itself and can then be turned by the BMC into a network console
44 * of some sort for example.
46 * This driver is for the BMC side. The sysfs files allow the BMC
47 * userspace which owns the system configuration policy, to specify
48 * at what IO port and interrupt number the host side will appear
49 * to the host on the Host <-> BMC LPC bus. It could be different on a
50 * different system (though most of them use 3f8/4).
53 static ssize_t lpc_address_show(struct device *dev,
54 struct device_attribute *attr, char *buf)
56 struct aspeed_vuart *vuart = dev_get_drvdata(dev);
59 addr = (readb(vuart->regs + ASPEED_VUART_ADDRH) << 8) |
60 (readb(vuart->regs + ASPEED_VUART_ADDRL));
62 return snprintf(buf, PAGE_SIZE - 1, "0x%x\n", addr);
65 static ssize_t lpc_address_store(struct device *dev,
66 struct device_attribute *attr,
67 const char *buf, size_t count)
69 struct aspeed_vuart *vuart = dev_get_drvdata(dev);
73 err = kstrtoul(buf, 0, &val);
77 writeb(val >> 8, vuart->regs + ASPEED_VUART_ADDRH);
78 writeb(val >> 0, vuart->regs + ASPEED_VUART_ADDRL);
83 static DEVICE_ATTR_RW(lpc_address);
85 static ssize_t sirq_show(struct device *dev,
86 struct device_attribute *attr, char *buf)
88 struct aspeed_vuart *vuart = dev_get_drvdata(dev);
91 reg = readb(vuart->regs + ASPEED_VUART_GCRB);
92 reg &= ASPEED_VUART_GCRB_HOST_SIRQ_MASK;
93 reg >>= ASPEED_VUART_GCRB_HOST_SIRQ_SHIFT;
95 return snprintf(buf, PAGE_SIZE - 1, "%u\n", reg);
98 static ssize_t sirq_store(struct device *dev, struct device_attribute *attr,
99 const char *buf, size_t count)
101 struct aspeed_vuart *vuart = dev_get_drvdata(dev);
106 err = kstrtoul(buf, 0, &val);
110 val <<= ASPEED_VUART_GCRB_HOST_SIRQ_SHIFT;
111 val &= ASPEED_VUART_GCRB_HOST_SIRQ_MASK;
113 reg = readb(vuart->regs + ASPEED_VUART_GCRB);
114 reg &= ~ASPEED_VUART_GCRB_HOST_SIRQ_MASK;
116 writeb(reg, vuart->regs + ASPEED_VUART_GCRB);
121 static DEVICE_ATTR_RW(sirq);
123 static struct attribute *aspeed_vuart_attrs[] = {
125 &dev_attr_lpc_address.attr,
129 static const struct attribute_group aspeed_vuart_attr_group = {
130 .attrs = aspeed_vuart_attrs,
133 static void aspeed_vuart_set_enabled(struct aspeed_vuart *vuart, bool enabled)
135 u8 reg = readb(vuart->regs + ASPEED_VUART_GCRA);
138 reg |= ASPEED_VUART_GCRA_VUART_EN;
140 reg &= ~ASPEED_VUART_GCRA_VUART_EN;
142 writeb(reg, vuart->regs + ASPEED_VUART_GCRA);
145 static void aspeed_vuart_set_host_tx_discard(struct aspeed_vuart *vuart,
150 reg = readb(vuart->regs + ASPEED_VUART_GCRA);
152 /* If the DISABLE_HOST_TX_DISCARD bit is set, discard is disabled */
154 reg |= ASPEED_VUART_GCRA_DISABLE_HOST_TX_DISCARD;
156 reg &= ~ASPEED_VUART_GCRA_DISABLE_HOST_TX_DISCARD;
158 writeb(reg, vuart->regs + ASPEED_VUART_GCRA);
161 static int aspeed_vuart_startup(struct uart_port *uart_port)
163 struct uart_8250_port *uart_8250_port = up_to_u8250p(uart_port);
164 struct aspeed_vuart *vuart = uart_8250_port->port.private_data;
167 rc = serial8250_do_startup(uart_port);
171 aspeed_vuart_set_host_tx_discard(vuart, false);
176 static void aspeed_vuart_shutdown(struct uart_port *uart_port)
178 struct uart_8250_port *uart_8250_port = up_to_u8250p(uart_port);
179 struct aspeed_vuart *vuart = uart_8250_port->port.private_data;
181 aspeed_vuart_set_host_tx_discard(vuart, true);
183 serial8250_do_shutdown(uart_port);
186 static int aspeed_vuart_probe(struct platform_device *pdev)
188 struct uart_8250_port port;
189 struct aspeed_vuart *vuart;
190 struct device_node *np;
191 struct resource *res;
195 np = pdev->dev.of_node;
197 vuart = devm_kzalloc(&pdev->dev, sizeof(*vuart), GFP_KERNEL);
201 vuart->dev = &pdev->dev;
203 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
204 vuart->regs = devm_ioremap_resource(&pdev->dev, res);
205 if (IS_ERR(vuart->regs))
206 return PTR_ERR(vuart->regs);
208 memset(&port, 0, sizeof(port));
209 port.port.private_data = vuart;
210 port.port.membase = vuart->regs;
211 port.port.mapbase = res->start;
212 port.port.mapsize = resource_size(res);
213 port.port.startup = aspeed_vuart_startup;
214 port.port.shutdown = aspeed_vuart_shutdown;
215 port.port.dev = &pdev->dev;
217 rc = sysfs_create_group(&vuart->dev->kobj, &aspeed_vuart_attr_group);
221 if (of_property_read_u32(np, "clock-frequency", &clk)) {
222 vuart->clk = devm_clk_get(&pdev->dev, NULL);
223 if (IS_ERR(vuart->clk)) {
225 "clk or clock-frequency not defined\n");
226 rc = PTR_ERR(vuart->clk);
227 goto err_sysfs_remove;
230 rc = clk_prepare_enable(vuart->clk);
232 goto err_sysfs_remove;
234 clk = clk_get_rate(vuart->clk);
237 /* If current-speed was set, then try not to change it. */
238 if (of_property_read_u32(np, "current-speed", &prop) == 0)
239 port.port.custom_divisor = clk / (16 * prop);
241 /* Check for shifted address mapping */
242 if (of_property_read_u32(np, "reg-offset", &prop) == 0)
243 port.port.mapbase += prop;
245 /* Check for registers offset within the devices address range */
246 if (of_property_read_u32(np, "reg-shift", &prop) == 0)
247 port.port.regshift = prop;
249 /* Check for fifo size */
250 if (of_property_read_u32(np, "fifo-size", &prop) == 0)
251 port.port.fifosize = prop;
253 /* Check for a fixed line number */
254 rc = of_alias_get_id(np, "serial");
258 port.port.irq = irq_of_parse_and_map(np, 0);
259 port.port.iotype = UPIO_MEM;
260 port.port.type = PORT_16550A;
261 port.port.uartclk = clk;
262 port.port.flags = UPF_SHARE_IRQ | UPF_BOOT_AUTOCONF
263 | UPF_FIXED_PORT | UPF_FIXED_TYPE | UPF_NO_THRE_TEST;
265 if (of_property_read_bool(np, "no-loopback-test"))
266 port.port.flags |= UPF_SKIP_TEST;
268 if (port.port.fifosize)
269 port.capabilities = UART_CAP_FIFO;
271 if (of_property_read_bool(np, "auto-flow-control"))
272 port.capabilities |= UART_CAP_AFE;
274 rc = serial8250_register_8250_port(&port);
276 goto err_clk_disable;
280 aspeed_vuart_set_enabled(vuart, true);
281 aspeed_vuart_set_host_tx_discard(vuart, true);
282 platform_set_drvdata(pdev, vuart);
287 clk_disable_unprepare(vuart->clk);
288 irq_dispose_mapping(port.port.irq);
290 sysfs_remove_group(&vuart->dev->kobj, &aspeed_vuart_attr_group);
294 static int aspeed_vuart_remove(struct platform_device *pdev)
296 struct aspeed_vuart *vuart = platform_get_drvdata(pdev);
298 aspeed_vuart_set_enabled(vuart, false);
299 serial8250_unregister_port(vuart->line);
300 sysfs_remove_group(&vuart->dev->kobj, &aspeed_vuart_attr_group);
301 clk_disable_unprepare(vuart->clk);
306 static const struct of_device_id aspeed_vuart_table[] = {
307 { .compatible = "aspeed,ast2400-vuart" },
308 { .compatible = "aspeed,ast2500-vuart" },
312 static struct platform_driver aspeed_vuart_driver = {
314 .name = "aspeed-vuart",
315 .of_match_table = aspeed_vuart_table,
317 .probe = aspeed_vuart_probe,
318 .remove = aspeed_vuart_remove,
321 module_platform_driver(aspeed_vuart_driver);
323 MODULE_AUTHOR("Jeremy Kerr <jk@ozlabs.org>");
324 MODULE_LICENSE("GPL");
325 MODULE_DESCRIPTION("Driver for Aspeed VUART device");