1
2
3
4
5
6
7
8
9
10
11#include <linux/module.h>
12#include <linux/pci.h>
13
14#include "8250.h"
15
16#define PCI_DEVICE_ID_MOXA_CP102E 0x1024
17#define PCI_DEVICE_ID_MOXA_CP102EL 0x1025
18#define PCI_DEVICE_ID_MOXA_CP104EL_A 0x1045
19#define PCI_DEVICE_ID_MOXA_CP114EL 0x1144
20#define PCI_DEVICE_ID_MOXA_CP116E_A_A 0x1160
21#define PCI_DEVICE_ID_MOXA_CP116E_A_B 0x1161
22#define PCI_DEVICE_ID_MOXA_CP118EL_A 0x1182
23#define PCI_DEVICE_ID_MOXA_CP118E_A_I 0x1183
24#define PCI_DEVICE_ID_MOXA_CP132EL 0x1322
25#define PCI_DEVICE_ID_MOXA_CP134EL_A 0x1342
26#define PCI_DEVICE_ID_MOXA_CP138E_A 0x1381
27#define PCI_DEVICE_ID_MOXA_CP168EL_A 0x1683
28
29#define MOXA_BASE_BAUD 921600
30#define MOXA_UART_OFFSET 0x200
31#define MOXA_BASE_BAR 1
32
33struct moxa8250_board {
34 unsigned int num_ports;
35 int line[0];
36};
37
38enum {
39 moxa8250_2p = 0,
40 moxa8250_4p,
41 moxa8250_8p
42};
43
44static struct moxa8250_board moxa8250_boards[] = {
45 [moxa8250_2p] = { .num_ports = 2},
46 [moxa8250_4p] = { .num_ports = 4},
47 [moxa8250_8p] = { .num_ports = 8},
48};
49
50static int moxa8250_probe(struct pci_dev *pdev, const struct pci_device_id *id)
51{
52 struct uart_8250_port uart;
53 struct moxa8250_board *brd;
54 void __iomem *ioaddr;
55 resource_size_t baseaddr;
56 unsigned int i, nr_ports;
57 unsigned int offset;
58 int ret;
59
60 brd = &moxa8250_boards[id->driver_data];
61 nr_ports = brd->num_ports;
62
63 ret = pcim_enable_device(pdev);
64 if (ret)
65 return ret;
66
67 brd = devm_kzalloc(&pdev->dev, sizeof(struct moxa8250_board) +
68 sizeof(unsigned int) * nr_ports, GFP_KERNEL);
69 if (!brd)
70 return -ENOMEM;
71 brd->num_ports = nr_ports;
72
73 memset(&uart, 0, sizeof(struct uart_8250_port));
74
75 uart.port.dev = &pdev->dev;
76 uart.port.irq = pdev->irq;
77 uart.port.uartclk = MOXA_BASE_BAUD * 16;
78 uart.port.flags = UPF_SKIP_TEST | UPF_BOOT_AUTOCONF | UPF_SHARE_IRQ;
79
80 baseaddr = pci_resource_start(pdev, MOXA_BASE_BAR);
81 ioaddr = pcim_iomap(pdev, MOXA_BASE_BAR, 0);
82 if (!ioaddr)
83 return -ENOMEM;
84
85 for (i = 0; i < nr_ports; i++) {
86
87
88
89
90
91 if (nr_ports == 4 && i == 3)
92 offset = 7 * MOXA_UART_OFFSET;
93 else
94 offset = i * MOXA_UART_OFFSET;
95
96 uart.port.iotype = UPIO_MEM;
97 uart.port.iobase = 0;
98 uart.port.mapbase = baseaddr + offset;
99 uart.port.membase = ioaddr + offset;
100 uart.port.regshift = 0;
101
102 dev_dbg(&pdev->dev, "Setup PCI port: port %lx, irq %d, type %d\n",
103 uart.port.iobase, uart.port.irq, uart.port.iotype);
104
105 brd->line[i] = serial8250_register_8250_port(&uart);
106 if (brd->line[i] < 0) {
107 dev_err(&pdev->dev,
108 "Couldn't register serial port %lx, irq %d, type %d, error %d\n",
109 uart.port.iobase, uart.port.irq,
110 uart.port.iotype, brd->line[i]);
111 break;
112 }
113 }
114
115 pci_set_drvdata(pdev, brd);
116 return 0;
117}
118
119static void moxa8250_remove(struct pci_dev *pdev)
120{
121 struct moxa8250_board *brd = pci_get_drvdata(pdev);
122 unsigned int i;
123
124 for (i = 0; i < brd->num_ports; i++)
125 serial8250_unregister_port(brd->line[i]);
126}
127
128#define MOXA_DEVICE(id, data) { PCI_VDEVICE(MOXA, id), (kernel_ulong_t)data }
129
130static const struct pci_device_id pci_ids[] = {
131 MOXA_DEVICE(PCI_DEVICE_ID_MOXA_CP102E, moxa8250_2p),
132 MOXA_DEVICE(PCI_DEVICE_ID_MOXA_CP102EL, moxa8250_2p),
133 MOXA_DEVICE(PCI_DEVICE_ID_MOXA_CP104EL_A, moxa8250_4p),
134 MOXA_DEVICE(PCI_DEVICE_ID_MOXA_CP114EL, moxa8250_4p),
135 MOXA_DEVICE(PCI_DEVICE_ID_MOXA_CP116E_A_A, moxa8250_8p),
136 MOXA_DEVICE(PCI_DEVICE_ID_MOXA_CP116E_A_B, moxa8250_8p),
137 MOXA_DEVICE(PCI_DEVICE_ID_MOXA_CP118EL_A, moxa8250_8p),
138 MOXA_DEVICE(PCI_DEVICE_ID_MOXA_CP118E_A_I, moxa8250_8p),
139 MOXA_DEVICE(PCI_DEVICE_ID_MOXA_CP132EL, moxa8250_2p),
140 MOXA_DEVICE(PCI_DEVICE_ID_MOXA_CP134EL_A, moxa8250_4p),
141 MOXA_DEVICE(PCI_DEVICE_ID_MOXA_CP138E_A, moxa8250_8p),
142 MOXA_DEVICE(PCI_DEVICE_ID_MOXA_CP168EL_A, moxa8250_8p),
143 {0}
144};
145MODULE_DEVICE_TABLE(pci, pci_ids);
146
147static struct pci_driver moxa8250_pci_driver = {
148 .name = "8250_moxa",
149 .id_table = pci_ids,
150 .probe = moxa8250_probe,
151 .remove = moxa8250_remove,
152};
153
154module_pci_driver(moxa8250_pci_driver);
155
156MODULE_AUTHOR("Mathieu OTHACEHE");
157MODULE_DESCRIPTION("MOXA SmartIO MUE driver");
158MODULE_LICENSE("GPL v2");
159