of_serial.c 5.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
/*
 *  Serial Port driver for Open Firmware platform devices
 *
 *    Copyright (C) 2006 Arnd Bergmann <arnd@arndb.de>, IBM Corp.
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  as published by the Free Software Foundation; either version
 *  2 of the License, or (at your option) any later version.
 *
 */
#include <linux/init.h>
#include <linux/module.h>
14
#include <linux/slab.h>
15 16
#include <linux/serial_core.h>
#include <linux/serial_8250.h>
G
Grant Likely 已提交
17
#include <linux/of_address.h>
18
#include <linux/of_irq.h>
19
#include <linux/of_platform.h>
20
#include <linux/nwpserial.h>
21

22 23 24 25 26
struct of_serial_info {
	int type;
	int line;
};

27 28 29
/*
 * Fill a struct uart_port for a given device node
 */
30
static int __devinit of_platform_serial_setup(struct platform_device *ofdev,
31 32 33
					int type, struct uart_port *port)
{
	struct resource resource;
34
	struct device_node *np = ofdev->dev.of_node;
35 36
	u32 clk, spd, prop;
	int ret;
37 38

	memset(port, 0, sizeof *port);
39
	if (of_property_read_u32(np, "clock-frequency", &clk)) {
40 41 42
		dev_warn(&ofdev->dev, "no clock-frequency property set\n");
		return -ENODEV;
	}
43 44 45
	/* If current-speed was set, then try not to change it. */
	if (of_property_read_u32(np, "current-speed", &spd) == 0)
		port->custom_divisor = clk / (16 * spd);
46 47 48 49 50 51 52 53 54

	ret = of_address_to_resource(np, 0, &resource);
	if (ret) {
		dev_warn(&ofdev->dev, "invalid address\n");
		return ret;
	}

	spin_lock_init(&port->lock);
	port->mapbase = resource.start;
55 56

	/* Check for shifted address mapping */
57 58
	if (of_property_read_u32(np, "reg-offset", &prop) == 0)
		port->mapbase += prop;
59 60

	/* Check for registers offset within the devices address range */
61 62
	if (of_property_read_u32(np, "reg-shift", &prop) == 0)
		port->regshift = prop;
63

64 65
	port->irq = irq_of_parse_and_map(np, 0);
	port->iotype = UPIO_MEM;
66 67
	if (of_property_read_u32(np, "reg-io-width", &prop) == 0) {
		switch (prop) {
68 69 70 71 72 73 74
		case 1:
			port->iotype = UPIO_MEM;
			break;
		case 4:
			port->iotype = UPIO_MEM32;
			break;
		default:
75 76
			dev_warn(&ofdev->dev, "unsupported reg-io-width (%d)\n",
				 prop);
77 78 79 80
			return -EINVAL;
		}
	}

81
	port->type = type;
82
	port->uartclk = clk;
83
	port->flags = UPF_SHARE_IRQ | UPF_BOOT_AUTOCONF | UPF_IOREMAP
84
		| UPF_FIXED_PORT | UPF_FIXED_TYPE;
85 86 87 88 89 90 91 92
	port->dev = &ofdev->dev;

	return 0;
}

/*
 * Try to register a serial port
 */
93
static struct of_device_id of_platform_serial_table[];
94
static int __devinit of_platform_serial_probe(struct platform_device *ofdev)
95
{
96
	const struct of_device_id *match;
97
	struct of_serial_info *info;
98 99 100 101
	struct uart_port port;
	int port_type;
	int ret;

102 103
	match = of_match_device(of_platform_serial_table, &ofdev->dev);
	if (!match)
104 105
		return -EINVAL;

106
	if (of_find_property(ofdev->dev.of_node, "used-by-rtas", NULL))
107 108
		return -EBUSY;

109 110 111 112
	info = kmalloc(sizeof(*info), GFP_KERNEL);
	if (info == NULL)
		return -ENOMEM;

113
	port_type = (unsigned long)match->data;
114 115 116 117 118
	ret = of_platform_serial_setup(ofdev, port_type, &port);
	if (ret)
		goto out;

	switch (port_type) {
119
#ifdef CONFIG_SERIAL_8250
120 121 122
	case PORT_8250 ... PORT_MAX_8250:
		ret = serial8250_register_port(&port);
		break;
123 124 125 126 127 128
#endif
#ifdef CONFIG_SERIAL_OF_PLATFORM_NWPSERIAL
	case PORT_NWPSERIAL:
		ret = nwpserial_register_port(&port);
		break;
#endif
129 130
	default:
		/* need to add code for these */
131 132
	case PORT_UNKNOWN:
		dev_info(&ofdev->dev, "Unknown serial port found, ignored\n");
133 134 135 136 137 138
		ret = -ENODEV;
		break;
	}
	if (ret < 0)
		goto out;

139 140
	info->type = port_type;
	info->line = ret;
141
	dev_set_drvdata(&ofdev->dev, info);
142 143
	return 0;
out:
144
	kfree(info);
145 146 147 148 149 150 151
	irq_dispose_mapping(port.irq);
	return ret;
}

/*
 * Release a line
 */
152
static int of_platform_serial_remove(struct platform_device *ofdev)
153
{
154
	struct of_serial_info *info = dev_get_drvdata(&ofdev->dev);
155
	switch (info->type) {
156
#ifdef CONFIG_SERIAL_8250
157 158 159
	case PORT_8250 ... PORT_MAX_8250:
		serial8250_unregister_port(info->line);
		break;
160 161 162 163 164 165
#endif
#ifdef CONFIG_SERIAL_OF_PLATFORM_NWPSERIAL
	case PORT_NWPSERIAL:
		nwpserial_unregister_port(info->line);
		break;
#endif
166 167 168 169 170
	default:
		/* need to add code for these */
		break;
	}
	kfree(info);
171 172 173 174 175 176 177
	return 0;
}

/*
 * A few common types, add more as needed.
 */
static struct of_device_id __devinitdata of_platform_serial_table[] = {
178 179 180 181 182 183
	{ .compatible = "ns8250",   .data = (void *)PORT_8250, },
	{ .compatible = "ns16450",  .data = (void *)PORT_16450, },
	{ .compatible = "ns16550a", .data = (void *)PORT_16550A, },
	{ .compatible = "ns16550",  .data = (void *)PORT_16550, },
	{ .compatible = "ns16750",  .data = (void *)PORT_16750, },
	{ .compatible = "ns16850",  .data = (void *)PORT_16850, },
184
	{ .compatible = "nvidia,tegra20-uart", .data = (void *)PORT_TEGRA, },
185
#ifdef CONFIG_SERIAL_OF_PLATFORM_NWPSERIAL
186 187
	{ .compatible = "ibm,qpace-nwp-serial",
		.data = (void *)PORT_NWPSERIAL, },
188
#endif
189
	{ .type = "serial",         .data = (void *)PORT_UNKNOWN, },
190 191 192
	{ /* end of list */ },
};

193
static struct platform_driver of_platform_serial_driver = {
194 195 196 197 198
	.driver = {
		.name = "of_serial",
		.owner = THIS_MODULE,
		.of_match_table = of_platform_serial_table,
	},
199 200 201 202
	.probe = of_platform_serial_probe,
	.remove = of_platform_serial_remove,
};

203
module_platform_driver(of_platform_serial_driver);
204 205 206 207

MODULE_AUTHOR("Arnd Bergmann <arnd@arndb.de>");
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Serial Port driver for Open Firmware platform devices");