2 * Probe for F81216A LPC to 4 UART
4 * Copyright (C) 2014-2016 Ricardo Ribalda, Qtechnology A/S
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.
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>
24 #define CHIP_ID_F81865 0x0407
25 #define CHIP_ID_F81866 0x1010
26 #define CHIP_ID_F81216AD 0x1602
27 #define CHIP_ID_F81216H 0x0501
28 #define CHIP_ID_F81216 0x0802
29 #define VENDOR_ID1 0x23
30 #define VENDOR_ID1_VAL 0x19
31 #define VENDOR_ID2 0x24
32 #define VENDOR_ID2_VAL 0x34
37 #define FINTEK_IRQ_MODE 0x70
38 #define IRQ_SHARE BIT(4)
39 #define IRQ_MODE_MASK (BIT(6) | BIT(5))
40 #define IRQ_LEVEL_LOW 0
41 #define IRQ_EDGE_HIGH BIT(5)
44 #define RTS_INVERT BIT(5)
45 #define RS485_URA BIT(4)
46 #define RXW4C_IRA BIT(3)
47 #define TXW4C_IRA BIT(2)
49 #define FIFO_CTRL 0xF6
50 #define FIFO_MODE_MASK (BIT(1) | BIT(0))
51 #define FIFO_MODE_128 (BIT(1) | BIT(0))
52 #define RXFTHR_MODE_MASK (BIT(5) | BIT(4))
53 #define RXFTHR_MODE_4X BIT(5)
55 #define F81216_LDN_LOW 0x0
56 #define F81216_LDN_HIGH 0x4
61 * The IRQ setting mode of F81866 is not the same with F81216 series.
62 * Level/Low: IRQ_MODE0:0, IRQ_MODE1:0
63 * Edge/High: IRQ_MODE0:1, IRQ_MODE1:0
65 #define F81866_IRQ_MODE 0xf0
66 #define F81866_IRQ_SHARE BIT(0)
67 #define F81866_IRQ_MODE0 BIT(1)
69 #define F81866_FIFO_CTRL FIFO_CTRL
70 #define F81866_IRQ_MODE1 BIT(3)
72 #define F81866_LDN_LOW 0x10
73 #define F81866_LDN_HIGH 0x16
82 static u8
sio_read_reg(struct fintek_8250
*pdata
, u8 reg
)
84 outb(reg
, pdata
->base_port
+ ADDR_PORT
);
85 return inb(pdata
->base_port
+ DATA_PORT
);
88 static void sio_write_reg(struct fintek_8250
*pdata
, u8 reg
, u8 data
)
90 outb(reg
, pdata
->base_port
+ ADDR_PORT
);
91 outb(data
, pdata
->base_port
+ DATA_PORT
);
94 static void sio_write_mask_reg(struct fintek_8250
*pdata
, u8 reg
, u8 mask
,
99 tmp
= (sio_read_reg(pdata
, reg
) & ~mask
) | (mask
& data
);
100 sio_write_reg(pdata
, reg
, tmp
);
103 static int fintek_8250_enter_key(u16 base_port
, u8 key
)
105 if (!request_muxed_region(base_port
, 2, "8250_fintek"))
108 outb(key
, base_port
+ ADDR_PORT
);
109 outb(key
, base_port
+ ADDR_PORT
);
113 static void fintek_8250_exit_key(u16 base_port
)
116 outb(EXIT_KEY
, base_port
+ ADDR_PORT
);
117 release_region(base_port
+ ADDR_PORT
, 2);
120 static int fintek_8250_check_id(struct fintek_8250
*pdata
)
124 if (sio_read_reg(pdata
, VENDOR_ID1
) != VENDOR_ID1_VAL
)
127 if (sio_read_reg(pdata
, VENDOR_ID2
) != VENDOR_ID2_VAL
)
130 chip
= sio_read_reg(pdata
, CHIP_ID1
);
131 chip
|= sio_read_reg(pdata
, CHIP_ID2
) << 8;
136 case CHIP_ID_F81216AD
:
137 case CHIP_ID_F81216H
:
148 static int fintek_8250_get_ldn_range(struct fintek_8250
*pdata
, int *min
,
151 switch (pdata
->pid
) {
154 *min
= F81866_LDN_LOW
;
155 *max
= F81866_LDN_HIGH
;
158 case CHIP_ID_F81216AD
:
159 case CHIP_ID_F81216H
:
161 *min
= F81216_LDN_LOW
;
162 *max
= F81216_LDN_HIGH
;
169 static int fintek_8250_rs485_config(struct uart_port
*port
,
170 struct serial_rs485
*rs485
)
173 struct fintek_8250
*pdata
= port
->private_data
;
178 if (rs485
->flags
& SER_RS485_ENABLED
)
179 memset(rs485
->padding
, 0, sizeof(rs485
->padding
));
181 memset(rs485
, 0, sizeof(*rs485
));
183 rs485
->flags
&= SER_RS485_ENABLED
| SER_RS485_RTS_ON_SEND
|
184 SER_RS485_RTS_AFTER_SEND
;
186 if (rs485
->delay_rts_before_send
) {
187 rs485
->delay_rts_before_send
= 1;
191 if (rs485
->delay_rts_after_send
) {
192 rs485
->delay_rts_after_send
= 1;
196 if ((!!(rs485
->flags
& SER_RS485_RTS_ON_SEND
)) ==
197 (!!(rs485
->flags
& SER_RS485_RTS_AFTER_SEND
)))
198 rs485
->flags
&= SER_RS485_ENABLED
;
202 if (rs485
->flags
& SER_RS485_RTS_ON_SEND
)
203 config
|= RTS_INVERT
;
205 if (fintek_8250_enter_key(pdata
->base_port
, pdata
->key
))
208 sio_write_reg(pdata
, LDN
, pdata
->index
);
209 sio_write_reg(pdata
, RS485
, config
);
210 fintek_8250_exit_key(pdata
->base_port
);
212 port
->rs485
= *rs485
;
217 static void fintek_8250_set_irq_mode(struct fintek_8250
*pdata
, bool is_level
)
219 sio_write_reg(pdata
, LDN
, pdata
->index
);
221 switch (pdata
->pid
) {
223 sio_write_mask_reg(pdata
, F81866_FIFO_CTRL
, F81866_IRQ_MODE1
,
227 sio_write_mask_reg(pdata
, F81866_IRQ_MODE
, F81866_IRQ_SHARE
,
229 sio_write_mask_reg(pdata
, F81866_IRQ_MODE
, F81866_IRQ_MODE0
,
230 is_level
? 0 : F81866_IRQ_MODE0
);
233 case CHIP_ID_F81216AD
:
234 case CHIP_ID_F81216H
:
236 sio_write_mask_reg(pdata
, FINTEK_IRQ_MODE
, IRQ_SHARE
,
238 sio_write_mask_reg(pdata
, FINTEK_IRQ_MODE
, IRQ_MODE_MASK
,
239 is_level
? IRQ_LEVEL_LOW
: IRQ_EDGE_HIGH
);
244 static void fintek_8250_set_max_fifo(struct fintek_8250
*pdata
)
246 switch (pdata
->pid
) {
247 case CHIP_ID_F81216H
: /* 128Bytes FIFO */
249 sio_write_mask_reg(pdata
, FIFO_CTRL
,
250 FIFO_MODE_MASK
| RXFTHR_MODE_MASK
,
251 FIFO_MODE_128
| RXFTHR_MODE_4X
);
254 default: /* Default 16Bytes FIFO */
259 static int probe_setup_port(struct fintek_8250
*pdata
, u16 io_address
,
262 static const u16 addr
[] = {0x4e, 0x2e};
263 static const u8 keys
[] = {0x77, 0xa0, 0x87, 0x67};
264 struct irq_data
*irq_data
;
265 bool level_mode
= false;
266 int i
, j
, k
, min
, max
;
268 for (i
= 0; i
< ARRAY_SIZE(addr
); i
++) {
269 for (j
= 0; j
< ARRAY_SIZE(keys
); j
++) {
270 pdata
->base_port
= addr
[i
];
271 pdata
->key
= keys
[j
];
273 if (fintek_8250_enter_key(addr
[i
], keys
[j
]))
275 if (fintek_8250_check_id(pdata
) ||
276 fintek_8250_get_ldn_range(pdata
, &min
, &max
)) {
277 fintek_8250_exit_key(addr
[i
]);
281 for (k
= min
; k
< max
; k
++) {
284 sio_write_reg(pdata
, LDN
, k
);
285 aux
= sio_read_reg(pdata
, IO_ADDR1
);
286 aux
|= sio_read_reg(pdata
, IO_ADDR2
) << 8;
287 if (aux
!= io_address
)
292 irq_data
= irq_get_irq_data(irq
);
295 irqd_is_level_type(irq_data
);
297 fintek_8250_set_irq_mode(pdata
, level_mode
);
298 fintek_8250_set_max_fifo(pdata
);
299 fintek_8250_exit_key(addr
[i
]);
304 fintek_8250_exit_key(addr
[i
]);
311 static void fintek_8250_set_rs485_handler(struct uart_8250_port
*uart
)
313 struct fintek_8250
*pdata
= uart
->port
.private_data
;
315 switch (pdata
->pid
) {
316 case CHIP_ID_F81216AD
:
317 case CHIP_ID_F81216H
:
320 uart
->port
.rs485_config
= fintek_8250_rs485_config
;
323 default: /* No RS485 Auto direction functional */
328 int fintek_8250_probe(struct uart_8250_port
*uart
)
330 struct fintek_8250
*pdata
;
331 struct fintek_8250 probe_data
;
333 if (probe_setup_port(&probe_data
, uart
->port
.iobase
, uart
->port
.irq
))
336 pdata
= devm_kzalloc(uart
->port
.dev
, sizeof(*pdata
), GFP_KERNEL
);
340 memcpy(pdata
, &probe_data
, sizeof(probe_data
));
341 uart
->port
.private_data
= pdata
;
342 fintek_8250_set_rs485_handler(uart
);