1 #include <linux/device.h>
2 #include <linux/kernel.h>
3 #include <linux/module.h>
6 #include <linux/serial.h>
7 #include <linux/serial_core.h>
8 #include <linux/serial_8250.h>
9 #include <uapi/linux/serial_core.h>
11 #define MEN_UART_ID_Z025 0x19
12 #define MEN_UART_ID_Z057 0x39
13 #define MEN_UART_ID_Z125 0x7d
15 #define MEN_UART_MEM_SIZE 0x10
17 struct serial_8250_men_mcb_data {
18 struct uart_8250_port uart;
23 * The Z125 16550-compatible UART has no fixed base clock assigned
24 * So, depending on the board we're on, we need to adjust the
25 * parameter in order to really set the correct baudrate, and
26 * do so if possible without user interaction
28 static u32 men_lookup_uartclk(struct mcb_device *mdev)
30 /* use default value if board is not available below */
33 dev_info(&mdev->dev, "%s on board %s\n",
36 if (strncmp(mdev->bus->name, "F075", 4) == 0)
38 else if (strncmp(mdev->bus->name, "F216", 4) == 0)
40 else if (strncmp(mdev->bus->name, "G215", 4) == 0)
42 else if (strncmp(mdev->bus->name, "F210", 4) == 0)
46 "board not detected, using default uartclk\n");
53 static unsigned int get_num_ports(struct mcb_device *mdev,
54 void __iomem *membase)
57 case MEN_UART_ID_Z125:
59 case MEN_UART_ID_Z025:
60 return readb(membase) >> 4;
61 case MEN_UART_ID_Z057:
64 dev_err(&mdev->dev, "no supported device!\n");
69 static int serial_8250_men_mcb_probe(struct mcb_device *mdev,
70 const struct mcb_device_id *id)
72 struct serial_8250_men_mcb_data *data;
76 void __iomem *membase;
78 mem = mcb_get_resource(mdev, IORESOURCE_MEM);
81 membase = devm_ioremap_resource(&mdev->dev, mem);
83 return PTR_ERR_OR_ZERO(membase);
85 num_ports = get_num_ports(mdev, membase);
87 dev_dbg(&mdev->dev, "found a 16z%03u with %u ports\n",
90 if (num_ports <= 0 || num_ports > 4) {
91 dev_err(&mdev->dev, "unexpected number of ports: %u\n",
96 data = devm_kcalloc(&mdev->dev, num_ports,
97 sizeof(struct serial_8250_men_mcb_data),
102 mcb_set_drvdata(mdev, data);
104 for (i = 0; i < num_ports; i++) {
105 data[i].uart.port.dev = mdev->dma_dev;
106 spin_lock_init(&data[i].uart.port.lock);
108 data[i].uart.port.type = PORT_16550;
109 data[i].uart.port.flags = UPF_SKIP_TEST | UPF_SHARE_IRQ
111 data[i].uart.port.iotype = UPIO_MEM;
112 data[i].uart.port.uartclk = men_lookup_uartclk(mdev);
113 data[i].uart.port.regshift = 0;
114 data[i].uart.port.irq = mcb_get_irq(mdev);
115 data[i].uart.port.membase = membase;
116 data[i].uart.port.fifosize = 60;
117 data[i].uart.port.mapbase = (unsigned long) mem->start
118 + i * MEN_UART_MEM_SIZE;
119 data[i].uart.port.iobase = data[i].uart.port.mapbase;
121 /* ok, register the port */
122 data[i].line = serial8250_register_8250_port(&data[i].uart);
123 if (data[i].line < 0) {
124 dev_err(&mdev->dev, "unable to register UART port\n");
127 dev_info(&mdev->dev, "found MCB UART: ttyS%d\n", data[i].line);
133 static void serial_8250_men_mcb_remove(struct mcb_device *mdev)
136 struct serial_8250_men_mcb_data *data = mcb_get_drvdata(mdev);
141 num_ports = get_num_ports(mdev, data[0].uart.port.membase);
142 if (num_ports < 0 || num_ports > 4) {
143 dev_err(&mdev->dev, "error retrieving number of ports!\n");
147 for (i = 0; i < num_ports; i++)
148 serial8250_unregister_port(data[i].line);
151 static const struct mcb_device_id serial_8250_men_mcb_ids[] = {
152 { .device = MEN_UART_ID_Z025 },
153 { .device = MEN_UART_ID_Z057 },
154 { .device = MEN_UART_ID_Z125 },
157 MODULE_DEVICE_TABLE(mcb, serial_8250_men_mcb_ids);
159 static struct mcb_driver mcb_driver = {
161 .name = "8250_men_mcb",
162 .owner = THIS_MODULE,
164 .probe = serial_8250_men_mcb_probe,
165 .remove = serial_8250_men_mcb_remove,
166 .id_table = serial_8250_men_mcb_ids,
168 module_mcb_driver(mcb_driver);
170 MODULE_LICENSE("GPL v2");
171 MODULE_DESCRIPTION("MEN 8250 UART driver");
172 MODULE_AUTHOR("Michael Moese <michael.moese@men.de");
173 MODULE_ALIAS("mcb:16z125");
174 MODULE_ALIAS("mcb:16z025");
175 MODULE_ALIAS("mcb:16z057");