GNU Linux-libre 4.9.294-gnu1
[releases.git] / drivers / tty / serial / 8250 / 8250_fintek.c
1 /*
2  *  Probe for F81216A LPC to 4 UART
3  *
4  *  Copyright (C) 2014-2016 Ricardo Ribalda, Qtechnology A/S
5  *
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License.
10  */
11 #include <linux/module.h>
12 #include <linux/pci.h>
13 #include <linux/pnp.h>
14 #include <linux/kernel.h>
15 #include <linux/serial_core.h>
16 #include <linux/irq.h>
17 #include  "8250.h"
18
19 #define ADDR_PORT 0
20 #define DATA_PORT 1
21 #define EXIT_KEY 0xAA
22 #define CHIP_ID1  0x20
23 #define CHIP_ID2  0x21
24 #define CHIP_ID_0 0x1602
25 #define CHIP_ID_1 0x0501
26 #define VENDOR_ID1 0x23
27 #define VENDOR_ID1_VAL 0x19
28 #define VENDOR_ID2 0x24
29 #define VENDOR_ID2_VAL 0x34
30 #define IO_ADDR1 0x61
31 #define IO_ADDR2 0x60
32 #define LDN 0x7
33
34 #define FINTEK_IRQ_MODE 0x70
35 #define IRQ_SHARE       BIT(4)
36 #define IRQ_MODE_MASK   (BIT(6) | BIT(5))
37 #define IRQ_LEVEL_LOW   0
38 #define IRQ_EDGE_HIGH   BIT(5)
39
40 #define RS485  0xF0
41 #define RTS_INVERT BIT(5)
42 #define RS485_URA BIT(4)
43 #define RXW4C_IRA BIT(3)
44 #define TXW4C_IRA BIT(2)
45
46 struct fintek_8250 {
47         u16 base_port;
48         u8 index;
49         u8 key;
50 };
51
52 static int fintek_8250_enter_key(u16 base_port, u8 key)
53 {
54         if (!request_muxed_region(base_port, 2, "8250_fintek"))
55                 return -EBUSY;
56
57         /* Force to deactive all SuperIO in this base_port */
58         outb(EXIT_KEY, base_port + ADDR_PORT);
59
60         outb(key, base_port + ADDR_PORT);
61         outb(key, base_port + ADDR_PORT);
62         return 0;
63 }
64
65 static void fintek_8250_exit_key(u16 base_port)
66 {
67
68         outb(EXIT_KEY, base_port + ADDR_PORT);
69         release_region(base_port + ADDR_PORT, 2);
70 }
71
72 static int fintek_8250_check_id(u16 base_port)
73 {
74         u16 chip;
75
76         outb(VENDOR_ID1, base_port + ADDR_PORT);
77         if (inb(base_port + DATA_PORT) != VENDOR_ID1_VAL)
78                 return -ENODEV;
79
80         outb(VENDOR_ID2, base_port + ADDR_PORT);
81         if (inb(base_port + DATA_PORT) != VENDOR_ID2_VAL)
82                 return -ENODEV;
83
84         outb(CHIP_ID1, base_port + ADDR_PORT);
85         chip = inb(base_port + DATA_PORT);
86         outb(CHIP_ID2, base_port + ADDR_PORT);
87         chip |= inb(base_port + DATA_PORT) << 8;
88
89         if (chip != CHIP_ID_0 && chip != CHIP_ID_1)
90                 return -ENODEV;
91
92         return 0;
93 }
94
95 static int fintek_8250_rs485_config(struct uart_port *port,
96                               struct serial_rs485 *rs485)
97 {
98         uint8_t config = 0;
99         struct fintek_8250 *pdata = port->private_data;
100
101         if (!pdata)
102                 return -EINVAL;
103
104         if (rs485->flags & SER_RS485_ENABLED)
105                 memset(rs485->padding, 0, sizeof(rs485->padding));
106         else
107                 memset(rs485, 0, sizeof(*rs485));
108
109         rs485->flags &= SER_RS485_ENABLED | SER_RS485_RTS_ON_SEND |
110                         SER_RS485_RTS_AFTER_SEND;
111
112         if (rs485->delay_rts_before_send) {
113                 rs485->delay_rts_before_send = 1;
114                 config |= TXW4C_IRA;
115         }
116
117         if (rs485->delay_rts_after_send) {
118                 rs485->delay_rts_after_send = 1;
119                 config |= RXW4C_IRA;
120         }
121
122         if ((!!(rs485->flags & SER_RS485_RTS_ON_SEND)) ==
123                         (!!(rs485->flags & SER_RS485_RTS_AFTER_SEND)))
124                 rs485->flags &= ~SER_RS485_ENABLED;
125         else
126                 config |= RS485_URA;
127
128         if (rs485->flags & SER_RS485_RTS_ON_SEND)
129                 config |= RTS_INVERT;
130
131         if (fintek_8250_enter_key(pdata->base_port, pdata->key))
132                 return -EBUSY;
133
134         outb(LDN, pdata->base_port + ADDR_PORT);
135         outb(pdata->index, pdata->base_port + DATA_PORT);
136         outb(RS485, pdata->base_port + ADDR_PORT);
137         outb(config, pdata->base_port + DATA_PORT);
138         fintek_8250_exit_key(pdata->base_port);
139
140         port->rs485 = *rs485;
141
142         return 0;
143 }
144
145 static int find_base_port(struct fintek_8250 *pdata, u16 io_address)
146 {
147         static const u16 addr[] = {0x4e, 0x2e};
148         static const u8 keys[] = {0x77, 0xa0, 0x87, 0x67};
149         int i, j, k;
150
151         for (i = 0; i < ARRAY_SIZE(addr); i++) {
152                 for (j = 0; j < ARRAY_SIZE(keys); j++) {
153
154                         if (fintek_8250_enter_key(addr[i], keys[j]))
155                                 continue;
156                         if (fintek_8250_check_id(addr[i])) {
157                                 fintek_8250_exit_key(addr[i]);
158                                 continue;
159                         }
160
161                         for (k = 0; k < 4; k++) {
162                                 u16 aux;
163
164                                 outb(LDN, addr[i] + ADDR_PORT);
165                                 outb(k, addr[i] + DATA_PORT);
166
167                                 outb(IO_ADDR1, addr[i] + ADDR_PORT);
168                                 aux = inb(addr[i] + DATA_PORT);
169                                 outb(IO_ADDR2, addr[i] + ADDR_PORT);
170                                 aux |= inb(addr[i] + DATA_PORT) << 8;
171                                 if (aux != io_address)
172                                         continue;
173
174                                 fintek_8250_exit_key(addr[i]);
175                                 pdata->key = keys[j];
176                                 pdata->base_port = addr[i];
177                                 pdata->index = k;
178
179                                 return 0;
180                         }
181
182                         fintek_8250_exit_key(addr[i]);
183                 }
184         }
185
186         return -ENODEV;
187 }
188
189 static int fintek_8250_set_irq_mode(struct fintek_8250 *pdata, bool level_mode)
190 {
191         int status;
192         u8 tmp;
193
194         status = fintek_8250_enter_key(pdata->base_port, pdata->key);
195         if (status)
196                 return status;
197
198         outb(LDN, pdata->base_port + ADDR_PORT);
199         outb(pdata->index, pdata->base_port + DATA_PORT);
200
201         outb(FINTEK_IRQ_MODE, pdata->base_port + ADDR_PORT);
202         tmp = inb(pdata->base_port + DATA_PORT);
203
204         tmp &= ~IRQ_MODE_MASK;
205         tmp |= IRQ_SHARE;
206         if (!level_mode)
207                 tmp |= IRQ_EDGE_HIGH;
208
209         outb(tmp, pdata->base_port + DATA_PORT);
210         fintek_8250_exit_key(pdata->base_port);
211         return 0;
212 }
213
214 int fintek_8250_probe(struct uart_8250_port *uart)
215 {
216         struct fintek_8250 *pdata;
217         struct fintek_8250 probe_data;
218         struct irq_data *irq_data = irq_get_irq_data(uart->port.irq);
219         bool level_mode = irqd_is_level_type(irq_data);
220
221         if (find_base_port(&probe_data, uart->port.iobase))
222                 return -ENODEV;
223
224         pdata = devm_kzalloc(uart->port.dev, sizeof(*pdata), GFP_KERNEL);
225         if (!pdata)
226                 return -ENOMEM;
227
228         memcpy(pdata, &probe_data, sizeof(probe_data));
229         uart->port.rs485_config = fintek_8250_rs485_config;
230         uart->port.private_data = pdata;
231
232         return fintek_8250_set_irq_mode(pdata, level_mode);
233 }