1#include <linux/device.h>
2#include <linux/kernel.h>
3#include <linux/module.h>
4#include <linux/io.h>
5#include <linux/mcb.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>
10
11#define MEN_UART_ID_Z025 0x19
12#define MEN_UART_ID_Z057 0x39
13#define MEN_UART_ID_Z125 0x7d
14
15#define MEN_UART_MEM_SIZE 0x10
16
17struct serial_8250_men_mcb_data {
18 struct uart_8250_port uart;
19 int line;
20};
21
22
23
24
25
26
27
28static u32 men_lookup_uartclk(struct mcb_device *mdev)
29{
30
31 u32 clkval = 1041666;
32
33 dev_info(&mdev->dev, "%s on board %s\n",
34 dev_name(&mdev->dev),
35 mdev->bus->name);
36 if (strncmp(mdev->bus->name, "F075", 4) == 0)
37 clkval = 1041666;
38 else if (strncmp(mdev->bus->name, "F216", 4) == 0)
39 clkval = 1843200;
40 else if (strncmp(mdev->bus->name, "G215", 4) == 0)
41 clkval = 1843200;
42 else if (strncmp(mdev->bus->name, "F210", 4) == 0)
43 clkval = 115200;
44 else
45 dev_info(&mdev->dev,
46 "board not detected, using default uartclk\n");
47
48 clkval = clkval << 4;
49
50 return clkval;
51}
52
53static unsigned int get_num_ports(struct mcb_device *mdev,
54 void __iomem *membase)
55{
56 switch (mdev->id) {
57 case MEN_UART_ID_Z125:
58 return 1U;
59 case MEN_UART_ID_Z025:
60 return readb(membase) >> 4;
61 case MEN_UART_ID_Z057:
62 return 4U;
63 default:
64 dev_err(&mdev->dev, "no supported device!\n");
65 return -ENODEV;
66 }
67}
68
69static int serial_8250_men_mcb_probe(struct mcb_device *mdev,
70 const struct mcb_device_id *id)
71{
72 struct serial_8250_men_mcb_data *data;
73 struct resource *mem;
74 unsigned int num_ports;
75 unsigned int i;
76 void __iomem *membase;
77
78 mem = mcb_get_resource(mdev, IORESOURCE_MEM);
79 if (mem == NULL)
80 return -ENXIO;
81 membase = devm_ioremap_resource(&mdev->dev, mem);
82 if (IS_ERR(membase))
83 return PTR_ERR_OR_ZERO(membase);
84
85 num_ports = get_num_ports(mdev, membase);
86
87 dev_dbg(&mdev->dev, "found a 16z%03u with %u ports\n",
88 mdev->id, num_ports);
89
90 if (num_ports == 0 || num_ports > 4) {
91 dev_err(&mdev->dev, "unexpected number of ports: %u\n",
92 num_ports);
93 return -ENODEV;
94 }
95
96 data = devm_kcalloc(&mdev->dev, num_ports,
97 sizeof(struct serial_8250_men_mcb_data),
98 GFP_KERNEL);
99 if (!data)
100 return -ENOMEM;
101
102 mcb_set_drvdata(mdev, data);
103
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);
107
108 data[i].uart.port.type = PORT_16550;
109 data[i].uart.port.flags = UPF_SKIP_TEST | UPF_SHARE_IRQ
110 | UPF_FIXED_TYPE;
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;
120
121
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");
125 return data[i].line;
126 }
127 dev_info(&mdev->dev, "found MCB UART: ttyS%d\n", data[i].line);
128 }
129
130 return 0;
131}
132
133static void serial_8250_men_mcb_remove(struct mcb_device *mdev)
134{
135 unsigned int num_ports, i;
136 struct serial_8250_men_mcb_data *data = mcb_get_drvdata(mdev);
137
138 if (!data)
139 return;
140
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");
144 return;
145 }
146
147 for (i = 0; i < num_ports; i++)
148 serial8250_unregister_port(data[i].line);
149}
150
151static 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 },
155 { }
156};
157MODULE_DEVICE_TABLE(mcb, serial_8250_men_mcb_ids);
158
159static struct mcb_driver mcb_driver = {
160 .driver = {
161 .name = "8250_men_mcb",
162 .owner = THIS_MODULE,
163 },
164 .probe = serial_8250_men_mcb_probe,
165 .remove = serial_8250_men_mcb_remove,
166 .id_table = serial_8250_men_mcb_ids,
167};
168module_mcb_driver(mcb_driver);
169
170MODULE_LICENSE("GPL v2");
171MODULE_DESCRIPTION("MEN 8250 UART driver");
172MODULE_AUTHOR("Michael Moese <michael.moese@men.de");
173MODULE_ALIAS("mcb:16z125");
174MODULE_ALIAS("mcb:16z025");
175MODULE_ALIAS("mcb:16z057");
176