summaryrefslogtreecommitdiff
path: root/drivers/tty/serial/8250/8250_men_mcb.c
blob: 308977807994c75bccf7910348255d0633f8e95f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <linux/device.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/mcb.h>
#include <linux/serial.h>
#include <linux/serial_core.h>
#include <linux/serial_8250.h>
#include <uapi/linux/serial_core.h>

struct serial_8250_men_mcb_data {
	struct uart_8250_port uart;
	int line;
};

/*
 * The Z125 16550-compatible UART has no fixed base clock assigned
 * So, depending on the board we're on, we need to adjust the
 * parameter in order to really set the correct baudrate, and
 * do so if possible without user interaction
 */
static u32 men_z125_lookup_uartclk(struct mcb_device *mdev)
{
	/* use default value if board is not available below */
	u32 clkval = 1041666;

	dev_info(&mdev->dev, "%s on board %s\n",
		dev_name(&mdev->dev),
		mdev->bus->name);
	if  (strncmp(mdev->bus->name, "F075", 4) == 0)
		clkval = 1041666;
	else if  (strncmp(mdev->bus->name, "F216", 4) == 0)
		clkval = 1843200;
	else if (strncmp(mdev->bus->name, "G215", 4) == 0)
		clkval = 1843200;
	else
		dev_info(&mdev->dev,
			 "board not detected, using default uartclk\n");

	clkval = clkval  << 4;

	return clkval;
}

static int serial_8250_men_mcb_probe(struct mcb_device *mdev,
				     const struct mcb_device_id *id)
{
	struct serial_8250_men_mcb_data *data;
	struct resource *mem;

	data = devm_kzalloc(&mdev->dev,
			    sizeof(struct serial_8250_men_mcb_data),
			    GFP_KERNEL);
	if (!data)
		return -ENOMEM;

	mcb_set_drvdata(mdev, data);
	data->uart.port.dev = mdev->dma_dev;
	spin_lock_init(&data->uart.port.lock);

	data->uart.port.type = PORT_16550;
	data->uart.port.flags = UPF_SKIP_TEST | UPF_SHARE_IRQ | UPF_FIXED_TYPE;
	data->uart.port.iotype = UPIO_MEM;
	data->uart.port.uartclk = men_z125_lookup_uartclk(mdev);
	data->uart.port.regshift = 0;
	data->uart.port.fifosize = 60;

	mem = mcb_get_resource(mdev, IORESOURCE_MEM);
	if (mem == NULL)
		return -ENXIO;

	data->uart.port.irq = mcb_get_irq(mdev);

	data->uart.port.membase = devm_ioremap_resource(&mdev->dev, mem);
	if (IS_ERR(data->uart.port.membase))
		return PTR_ERR_OR_ZERO(data->uart.port.membase);

	data->uart.port.mapbase = (unsigned long) mem->start;
	data->uart.port.iobase = data->uart.port.mapbase;

	/* ok, register the port */
	data->line = serial8250_register_8250_port(&data->uart);
	if (data->line < 0)
		return data->line;

	dev_info(&mdev->dev, "found 16Z125 UART: ttyS%d\n", data->line);

	return 0;
}

static void serial_8250_men_mcb_remove(struct mcb_device *mdev)
{
	struct serial_8250_men_mcb_data *data = mcb_get_drvdata(mdev);

	if (data)
		serial8250_unregister_port(data->line);
}

static const struct mcb_device_id serial_8250_men_mcb_ids[] = {
	{ .device = 0x7d },
	{ }
};
MODULE_DEVICE_TABLE(mcb, serial_8250_men_mcb_ids);

static struct mcb_driver mcb_driver = {
	.driver = {
		.name = "8250_men_mcb",
		.owner = THIS_MODULE,
	},
	.probe = serial_8250_men_mcb_probe,
	.remove = serial_8250_men_mcb_remove,
	.id_table = serial_8250_men_mcb_ids,
};
module_mcb_driver(mcb_driver);

MODULE_LICENSE("GPL v2");
MODULE_DESCRIPTION("MEN 16z125 8250 UART driver");
MODULE_AUTHOR("Michael Moese <michael.moese@men.de");
MODULE_ALIAS("mcb:16z125");