xref: /openbmc/linux/drivers/tty/serial/8250/8250_fintek.c (revision 7ecc77011c6fb40d3eef7c6a0ebcec61e69b3ec3)
1e3b3d0f5SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
228e3fb6cSRicardo Ribalda Delgado /*
328e3fb6cSRicardo Ribalda Delgado  *  Probe for F81216A LPC to 4 UART
428e3fb6cSRicardo Ribalda Delgado  *
5fa01e2caSRicardo Ribalda Delgado  *  Copyright (C) 2014-2016 Ricardo Ribalda, Qtechnology A/S
628e3fb6cSRicardo Ribalda Delgado  */
728e3fb6cSRicardo Ribalda Delgado #include <linux/module.h>
828e3fb6cSRicardo Ribalda Delgado #include <linux/pci.h>
928e3fb6cSRicardo Ribalda Delgado #include <linux/pnp.h>
1028e3fb6cSRicardo Ribalda Delgado #include <linux/kernel.h>
1128e3fb6cSRicardo Ribalda Delgado #include <linux/serial_core.h>
124da22f14SJi-Ze Hong (Peter Hong) #include <linux/irq.h>
1328e3fb6cSRicardo Ribalda Delgado #include  "8250.h"
1428e3fb6cSRicardo Ribalda Delgado 
15017bec38SRicardo Ribalda Delgado #define ADDR_PORT 0
16017bec38SRicardo Ribalda Delgado #define DATA_PORT 1
1728e3fb6cSRicardo Ribalda Delgado #define EXIT_KEY 0xAA
1828e3fb6cSRicardo Ribalda Delgado #define CHIP_ID1  0x20
1928e3fb6cSRicardo Ribalda Delgado #define CHIP_ID2  0x21
20de48b099SJi-Ze Hong (Peter Hong) #define CHIP_ID_F81865 0x0407
21da60d6afSJi-Ze Hong (Peter Hong) #define CHIP_ID_F81866 0x1010
22c2236facSJi-Ze Hong (Peter Hong) #define CHIP_ID_F81216AD 0x1602
23c2236facSJi-Ze Hong (Peter Hong) #define CHIP_ID_F81216H 0x0501
241e26c472SJi-Ze Hong (Peter Hong) #define CHIP_ID_F81216 0x0802
2528e3fb6cSRicardo Ribalda Delgado #define VENDOR_ID1 0x23
2628e3fb6cSRicardo Ribalda Delgado #define VENDOR_ID1_VAL 0x19
2728e3fb6cSRicardo Ribalda Delgado #define VENDOR_ID2 0x24
2828e3fb6cSRicardo Ribalda Delgado #define VENDOR_ID2_VAL 0x34
2929d58642SRicardo Ribalda Delgado #define IO_ADDR1 0x61
3029d58642SRicardo Ribalda Delgado #define IO_ADDR2 0x60
3128e3fb6cSRicardo Ribalda Delgado #define LDN 0x7
3228e3fb6cSRicardo Ribalda Delgado 
3387a713c8SArnd Bergmann #define FINTEK_IRQ_MODE	0x70
344da22f14SJi-Ze Hong (Peter Hong) #define IRQ_SHARE	BIT(4)
354da22f14SJi-Ze Hong (Peter Hong) #define IRQ_MODE_MASK	(BIT(6) | BIT(5))
364da22f14SJi-Ze Hong (Peter Hong) #define IRQ_LEVEL_LOW	0
374da22f14SJi-Ze Hong (Peter Hong) #define IRQ_EDGE_HIGH	BIT(5)
384da22f14SJi-Ze Hong (Peter Hong) 
3958178914SJi-Ze Hong (Peter Hong) /*
4058178914SJi-Ze Hong (Peter Hong)  * F81216H clock source register, the value and mask is the same with F81866,
4158178914SJi-Ze Hong (Peter Hong)  * but it's on F0h.
4258178914SJi-Ze Hong (Peter Hong)  *
4358178914SJi-Ze Hong (Peter Hong)  * Clock speeds for UART (register F0h)
4458178914SJi-Ze Hong (Peter Hong)  * 00: 1.8432MHz.
4558178914SJi-Ze Hong (Peter Hong)  * 01: 18.432MHz.
4658178914SJi-Ze Hong (Peter Hong)  * 10: 24MHz.
4758178914SJi-Ze Hong (Peter Hong)  * 11: 14.769MHz.
4858178914SJi-Ze Hong (Peter Hong)  */
4928e3fb6cSRicardo Ribalda Delgado #define RS485  0xF0
5028e3fb6cSRicardo Ribalda Delgado #define RTS_INVERT BIT(5)
5128e3fb6cSRicardo Ribalda Delgado #define RS485_URA BIT(4)
5228e3fb6cSRicardo Ribalda Delgado #define RXW4C_IRA BIT(3)
5328e3fb6cSRicardo Ribalda Delgado #define TXW4C_IRA BIT(2)
5428e3fb6cSRicardo Ribalda Delgado 
55c2236facSJi-Ze Hong (Peter Hong) #define FIFO_CTRL		0xF6
56c2236facSJi-Ze Hong (Peter Hong) #define FIFO_MODE_MASK		(BIT(1) | BIT(0))
57c2236facSJi-Ze Hong (Peter Hong) #define FIFO_MODE_128		(BIT(1) | BIT(0))
58c2236facSJi-Ze Hong (Peter Hong) #define RXFTHR_MODE_MASK	(BIT(5) | BIT(4))
59c2236facSJi-Ze Hong (Peter Hong) #define RXFTHR_MODE_4X		BIT(5)
60c2236facSJi-Ze Hong (Peter Hong) 
61da60d6afSJi-Ze Hong (Peter Hong) #define F81216_LDN_LOW	0x0
62da60d6afSJi-Ze Hong (Peter Hong) #define F81216_LDN_HIGH	0x4
63da60d6afSJi-Ze Hong (Peter Hong) 
64da60d6afSJi-Ze Hong (Peter Hong) /*
65da60d6afSJi-Ze Hong (Peter Hong)  * F81866 registers
66da60d6afSJi-Ze Hong (Peter Hong)  *
67da60d6afSJi-Ze Hong (Peter Hong)  * The IRQ setting mode of F81866 is not the same with F81216 series.
68da60d6afSJi-Ze Hong (Peter Hong)  *	Level/Low: IRQ_MODE0:0, IRQ_MODE1:0
69da60d6afSJi-Ze Hong (Peter Hong)  *	Edge/High: IRQ_MODE0:1, IRQ_MODE1:0
70fab8a02bSLukas Redlinger  *
71fab8a02bSLukas Redlinger  * Clock speeds for UART (register F2h)
72fab8a02bSLukas Redlinger  * 00: 1.8432MHz.
73fab8a02bSLukas Redlinger  * 01: 18.432MHz.
74fab8a02bSLukas Redlinger  * 10: 24MHz.
75fab8a02bSLukas Redlinger  * 11: 14.769MHz.
76da60d6afSJi-Ze Hong (Peter Hong)  */
77da60d6afSJi-Ze Hong (Peter Hong) #define F81866_IRQ_MODE		0xf0
78da60d6afSJi-Ze Hong (Peter Hong) #define F81866_IRQ_SHARE	BIT(0)
79da60d6afSJi-Ze Hong (Peter Hong) #define F81866_IRQ_MODE0	BIT(1)
80da60d6afSJi-Ze Hong (Peter Hong) 
81da60d6afSJi-Ze Hong (Peter Hong) #define F81866_FIFO_CTRL	FIFO_CTRL
82da60d6afSJi-Ze Hong (Peter Hong) #define F81866_IRQ_MODE1	BIT(3)
83da60d6afSJi-Ze Hong (Peter Hong) 
84da60d6afSJi-Ze Hong (Peter Hong) #define F81866_LDN_LOW		0x10
85da60d6afSJi-Ze Hong (Peter Hong) #define F81866_LDN_HIGH		0x16
86da60d6afSJi-Ze Hong (Peter Hong) 
87fab8a02bSLukas Redlinger #define F81866_UART_CLK 0xF2
88fab8a02bSLukas Redlinger #define F81866_UART_CLK_MASK (BIT(1) | BIT(0))
89fab8a02bSLukas Redlinger #define F81866_UART_CLK_1_8432MHZ 0
90fab8a02bSLukas Redlinger #define F81866_UART_CLK_14_769MHZ (BIT(1) | BIT(0))
91fab8a02bSLukas Redlinger #define F81866_UART_CLK_18_432MHZ BIT(0)
92fab8a02bSLukas Redlinger #define F81866_UART_CLK_24MHZ BIT(1)
93fab8a02bSLukas Redlinger 
9492a5f11aSRicardo Ribalda Delgado struct fintek_8250 {
95c2236facSJi-Ze Hong (Peter Hong) 	u16 pid;
96017bec38SRicardo Ribalda Delgado 	u16 base_port;
9792a5f11aSRicardo Ribalda Delgado 	u8 index;
98ce8c267eSRicardo Ribalda Delgado 	u8 key;
9992a5f11aSRicardo Ribalda Delgado };
10092a5f11aSRicardo Ribalda Delgado 
101f1232ac2SJi-Ze Hong (Peter Hong) static u8 sio_read_reg(struct fintek_8250 *pdata, u8 reg)
102f1232ac2SJi-Ze Hong (Peter Hong) {
103f1232ac2SJi-Ze Hong (Peter Hong) 	outb(reg, pdata->base_port + ADDR_PORT);
104f1232ac2SJi-Ze Hong (Peter Hong) 	return inb(pdata->base_port + DATA_PORT);
105f1232ac2SJi-Ze Hong (Peter Hong) }
106f1232ac2SJi-Ze Hong (Peter Hong) 
107f1232ac2SJi-Ze Hong (Peter Hong) static void sio_write_reg(struct fintek_8250 *pdata, u8 reg, u8 data)
108f1232ac2SJi-Ze Hong (Peter Hong) {
109f1232ac2SJi-Ze Hong (Peter Hong) 	outb(reg, pdata->base_port + ADDR_PORT);
110f1232ac2SJi-Ze Hong (Peter Hong) 	outb(data, pdata->base_port + DATA_PORT);
111f1232ac2SJi-Ze Hong (Peter Hong) }
112f1232ac2SJi-Ze Hong (Peter Hong) 
113f1232ac2SJi-Ze Hong (Peter Hong) static void sio_write_mask_reg(struct fintek_8250 *pdata, u8 reg, u8 mask,
114f1232ac2SJi-Ze Hong (Peter Hong) 			       u8 data)
115f1232ac2SJi-Ze Hong (Peter Hong) {
116f1232ac2SJi-Ze Hong (Peter Hong) 	u8 tmp;
117f1232ac2SJi-Ze Hong (Peter Hong) 
118f1232ac2SJi-Ze Hong (Peter Hong) 	tmp = (sio_read_reg(pdata, reg) & ~mask) | (mask & data);
119f1232ac2SJi-Ze Hong (Peter Hong) 	sio_write_reg(pdata, reg, tmp);
120f1232ac2SJi-Ze Hong (Peter Hong) }
121f1232ac2SJi-Ze Hong (Peter Hong) 
122ce8c267eSRicardo Ribalda Delgado static int fintek_8250_enter_key(u16 base_port, u8 key)
123017bec38SRicardo Ribalda Delgado {
124fa01e2caSRicardo Ribalda Delgado 	if (!request_muxed_region(base_port, 2, "8250_fintek"))
12528e3fb6cSRicardo Ribalda Delgado 		return -EBUSY;
12628e3fb6cSRicardo Ribalda Delgado 
127fd97e66cSJi-Ze Hong (Peter Hong) 	/* Force to deactive all SuperIO in this base_port */
128fd97e66cSJi-Ze Hong (Peter Hong) 	outb(EXIT_KEY, base_port + ADDR_PORT);
129fd97e66cSJi-Ze Hong (Peter Hong) 
130ce8c267eSRicardo Ribalda Delgado 	outb(key, base_port + ADDR_PORT);
131ce8c267eSRicardo Ribalda Delgado 	outb(key, base_port + ADDR_PORT);
13228e3fb6cSRicardo Ribalda Delgado 	return 0;
13328e3fb6cSRicardo Ribalda Delgado }
13428e3fb6cSRicardo Ribalda Delgado 
135017bec38SRicardo Ribalda Delgado static void fintek_8250_exit_key(u16 base_port)
136017bec38SRicardo Ribalda Delgado {
13728e3fb6cSRicardo Ribalda Delgado 
138017bec38SRicardo Ribalda Delgado 	outb(EXIT_KEY, base_port + ADDR_PORT);
139017bec38SRicardo Ribalda Delgado 	release_region(base_port + ADDR_PORT, 2);
14028e3fb6cSRicardo Ribalda Delgado }
14128e3fb6cSRicardo Ribalda Delgado 
142f1232ac2SJi-Ze Hong (Peter Hong) static int fintek_8250_check_id(struct fintek_8250 *pdata)
14328e3fb6cSRicardo Ribalda Delgado {
144dae77f75SRicardo Ribalda Delgado 	u16 chip;
14528e3fb6cSRicardo Ribalda Delgado 
146f1232ac2SJi-Ze Hong (Peter Hong) 	if (sio_read_reg(pdata, VENDOR_ID1) != VENDOR_ID1_VAL)
14728e3fb6cSRicardo Ribalda Delgado 		return -ENODEV;
14828e3fb6cSRicardo Ribalda Delgado 
149f1232ac2SJi-Ze Hong (Peter Hong) 	if (sio_read_reg(pdata, VENDOR_ID2) != VENDOR_ID2_VAL)
15028e3fb6cSRicardo Ribalda Delgado 		return -ENODEV;
15128e3fb6cSRicardo Ribalda Delgado 
152f1232ac2SJi-Ze Hong (Peter Hong) 	chip = sio_read_reg(pdata, CHIP_ID1);
153f1232ac2SJi-Ze Hong (Peter Hong) 	chip |= sio_read_reg(pdata, CHIP_ID2) << 8;
154dae77f75SRicardo Ribalda Delgado 
1551e26c472SJi-Ze Hong (Peter Hong) 	switch (chip) {
156de48b099SJi-Ze Hong (Peter Hong) 	case CHIP_ID_F81865:
157da60d6afSJi-Ze Hong (Peter Hong) 	case CHIP_ID_F81866:
1581e26c472SJi-Ze Hong (Peter Hong) 	case CHIP_ID_F81216AD:
1591e26c472SJi-Ze Hong (Peter Hong) 	case CHIP_ID_F81216H:
1601e26c472SJi-Ze Hong (Peter Hong) 	case CHIP_ID_F81216:
1611e26c472SJi-Ze Hong (Peter Hong) 		break;
1621e26c472SJi-Ze Hong (Peter Hong) 	default:
163dae77f75SRicardo Ribalda Delgado 		return -ENODEV;
1641e26c472SJi-Ze Hong (Peter Hong) 	}
165dae77f75SRicardo Ribalda Delgado 
166c2236facSJi-Ze Hong (Peter Hong) 	pdata->pid = chip;
16728e3fb6cSRicardo Ribalda Delgado 	return 0;
16828e3fb6cSRicardo Ribalda Delgado }
16928e3fb6cSRicardo Ribalda Delgado 
170da60d6afSJi-Ze Hong (Peter Hong) static int fintek_8250_get_ldn_range(struct fintek_8250 *pdata, int *min,
171da60d6afSJi-Ze Hong (Peter Hong) 				     int *max)
172da60d6afSJi-Ze Hong (Peter Hong) {
173da60d6afSJi-Ze Hong (Peter Hong) 	switch (pdata->pid) {
174de48b099SJi-Ze Hong (Peter Hong) 	case CHIP_ID_F81865:
175da60d6afSJi-Ze Hong (Peter Hong) 	case CHIP_ID_F81866:
176da60d6afSJi-Ze Hong (Peter Hong) 		*min = F81866_LDN_LOW;
177da60d6afSJi-Ze Hong (Peter Hong) 		*max = F81866_LDN_HIGH;
178da60d6afSJi-Ze Hong (Peter Hong) 		return 0;
179da60d6afSJi-Ze Hong (Peter Hong) 
180da60d6afSJi-Ze Hong (Peter Hong) 	case CHIP_ID_F81216AD:
181da60d6afSJi-Ze Hong (Peter Hong) 	case CHIP_ID_F81216H:
182da60d6afSJi-Ze Hong (Peter Hong) 	case CHIP_ID_F81216:
183da60d6afSJi-Ze Hong (Peter Hong) 		*min = F81216_LDN_LOW;
184da60d6afSJi-Ze Hong (Peter Hong) 		*max = F81216_LDN_HIGH;
185da60d6afSJi-Ze Hong (Peter Hong) 		return 0;
186da60d6afSJi-Ze Hong (Peter Hong) 	}
187da60d6afSJi-Ze Hong (Peter Hong) 
188da60d6afSJi-Ze Hong (Peter Hong) 	return -ENODEV;
189da60d6afSJi-Ze Hong (Peter Hong) }
190da60d6afSJi-Ze Hong (Peter Hong) 
19141e69093SRicardo Ribalda Delgado static int fintek_8250_rs485_config(struct uart_port *port,
19228e3fb6cSRicardo Ribalda Delgado 			      struct serial_rs485 *rs485)
19328e3fb6cSRicardo Ribalda Delgado {
19428e3fb6cSRicardo Ribalda Delgado 	uint8_t config = 0;
19592a5f11aSRicardo Ribalda Delgado 	struct fintek_8250 *pdata = port->private_data;
19628e3fb6cSRicardo Ribalda Delgado 
19792a5f11aSRicardo Ribalda Delgado 	if (!pdata)
19828e3fb6cSRicardo Ribalda Delgado 		return -EINVAL;
19928e3fb6cSRicardo Ribalda Delgado 
200*7ecc7701SRicardo Ribalda Delgado 	/* Hardware do not support same RTS level on send and receive */
201*7ecc7701SRicardo Ribalda Delgado 	if (!(rs485->flags & SER_RS485_RTS_ON_SEND) ==
202*7ecc7701SRicardo Ribalda Delgado 			!(rs485->flags & SER_RS485_RTS_AFTER_SEND))
203*7ecc7701SRicardo Ribalda Delgado 		return -EINVAL;
204*7ecc7701SRicardo Ribalda Delgado 
205*7ecc7701SRicardo Ribalda Delgado 	if (rs485->flags & SER_RS485_ENABLED) {
20628e3fb6cSRicardo Ribalda Delgado 		memset(rs485->padding, 0, sizeof(rs485->padding));
207*7ecc7701SRicardo Ribalda Delgado 		config |= RS485_URA;
208*7ecc7701SRicardo Ribalda Delgado 	} else {
20928e3fb6cSRicardo Ribalda Delgado 		memset(rs485, 0, sizeof(*rs485));
210*7ecc7701SRicardo Ribalda Delgado 	}
21128e3fb6cSRicardo Ribalda Delgado 
21228e3fb6cSRicardo Ribalda Delgado 	rs485->flags &= SER_RS485_ENABLED | SER_RS485_RTS_ON_SEND |
21328e3fb6cSRicardo Ribalda Delgado 			SER_RS485_RTS_AFTER_SEND;
21428e3fb6cSRicardo Ribalda Delgado 
21528e3fb6cSRicardo Ribalda Delgado 	if (rs485->delay_rts_before_send) {
21628e3fb6cSRicardo Ribalda Delgado 		rs485->delay_rts_before_send = 1;
21728e3fb6cSRicardo Ribalda Delgado 		config |= TXW4C_IRA;
21828e3fb6cSRicardo Ribalda Delgado 	}
21928e3fb6cSRicardo Ribalda Delgado 
22028e3fb6cSRicardo Ribalda Delgado 	if (rs485->delay_rts_after_send) {
22128e3fb6cSRicardo Ribalda Delgado 		rs485->delay_rts_after_send = 1;
22228e3fb6cSRicardo Ribalda Delgado 		config |= RXW4C_IRA;
22328e3fb6cSRicardo Ribalda Delgado 	}
22428e3fb6cSRicardo Ribalda Delgado 
22528e3fb6cSRicardo Ribalda Delgado 	if (rs485->flags & SER_RS485_RTS_ON_SEND)
22628e3fb6cSRicardo Ribalda Delgado 		config |= RTS_INVERT;
22728e3fb6cSRicardo Ribalda Delgado 
228ce8c267eSRicardo Ribalda Delgado 	if (fintek_8250_enter_key(pdata->base_port, pdata->key))
22928e3fb6cSRicardo Ribalda Delgado 		return -EBUSY;
23028e3fb6cSRicardo Ribalda Delgado 
231f1232ac2SJi-Ze Hong (Peter Hong) 	sio_write_reg(pdata, LDN, pdata->index);
232f1232ac2SJi-Ze Hong (Peter Hong) 	sio_write_reg(pdata, RS485, config);
233017bec38SRicardo Ribalda Delgado 	fintek_8250_exit_key(pdata->base_port);
23428e3fb6cSRicardo Ribalda Delgado 
23541e69093SRicardo Ribalda Delgado 	port->rs485 = *rs485;
23641e69093SRicardo Ribalda Delgado 
23728e3fb6cSRicardo Ribalda Delgado 	return 0;
23828e3fb6cSRicardo Ribalda Delgado }
23928e3fb6cSRicardo Ribalda Delgado 
24006e39572SJi-Ze Hong (Peter Hong) static void fintek_8250_set_irq_mode(struct fintek_8250 *pdata, bool is_level)
24106e39572SJi-Ze Hong (Peter Hong) {
24206e39572SJi-Ze Hong (Peter Hong) 	sio_write_reg(pdata, LDN, pdata->index);
243da60d6afSJi-Ze Hong (Peter Hong) 
244da60d6afSJi-Ze Hong (Peter Hong) 	switch (pdata->pid) {
245da60d6afSJi-Ze Hong (Peter Hong) 	case CHIP_ID_F81866:
246da60d6afSJi-Ze Hong (Peter Hong) 		sio_write_mask_reg(pdata, F81866_FIFO_CTRL, F81866_IRQ_MODE1,
247da60d6afSJi-Ze Hong (Peter Hong) 				   0);
248de48b099SJi-Ze Hong (Peter Hong) 		/* fall through */
249de48b099SJi-Ze Hong (Peter Hong) 	case CHIP_ID_F81865:
250da60d6afSJi-Ze Hong (Peter Hong) 		sio_write_mask_reg(pdata, F81866_IRQ_MODE, F81866_IRQ_SHARE,
251da60d6afSJi-Ze Hong (Peter Hong) 				   F81866_IRQ_SHARE);
252da60d6afSJi-Ze Hong (Peter Hong) 		sio_write_mask_reg(pdata, F81866_IRQ_MODE, F81866_IRQ_MODE0,
253da60d6afSJi-Ze Hong (Peter Hong) 				   is_level ? 0 : F81866_IRQ_MODE0);
254da60d6afSJi-Ze Hong (Peter Hong) 		break;
255da60d6afSJi-Ze Hong (Peter Hong) 
256da60d6afSJi-Ze Hong (Peter Hong) 	case CHIP_ID_F81216AD:
257da60d6afSJi-Ze Hong (Peter Hong) 	case CHIP_ID_F81216H:
258da60d6afSJi-Ze Hong (Peter Hong) 	case CHIP_ID_F81216:
259da60d6afSJi-Ze Hong (Peter Hong) 		sio_write_mask_reg(pdata, FINTEK_IRQ_MODE, IRQ_SHARE,
260da60d6afSJi-Ze Hong (Peter Hong) 				   IRQ_SHARE);
26106e39572SJi-Ze Hong (Peter Hong) 		sio_write_mask_reg(pdata, FINTEK_IRQ_MODE, IRQ_MODE_MASK,
26206e39572SJi-Ze Hong (Peter Hong) 				   is_level ? IRQ_LEVEL_LOW : IRQ_EDGE_HIGH);
263da60d6afSJi-Ze Hong (Peter Hong) 		break;
264da60d6afSJi-Ze Hong (Peter Hong) 	}
26506e39572SJi-Ze Hong (Peter Hong) }
26606e39572SJi-Ze Hong (Peter Hong) 
267c2236facSJi-Ze Hong (Peter Hong) static void fintek_8250_set_max_fifo(struct fintek_8250 *pdata)
268c2236facSJi-Ze Hong (Peter Hong) {
269c2236facSJi-Ze Hong (Peter Hong) 	switch (pdata->pid) {
270c2236facSJi-Ze Hong (Peter Hong) 	case CHIP_ID_F81216H: /* 128Bytes FIFO */
271da60d6afSJi-Ze Hong (Peter Hong) 	case CHIP_ID_F81866:
272c2236facSJi-Ze Hong (Peter Hong) 		sio_write_mask_reg(pdata, FIFO_CTRL,
273c2236facSJi-Ze Hong (Peter Hong) 				   FIFO_MODE_MASK | RXFTHR_MODE_MASK,
274c2236facSJi-Ze Hong (Peter Hong) 				   FIFO_MODE_128 | RXFTHR_MODE_4X);
275c2236facSJi-Ze Hong (Peter Hong) 		break;
276c2236facSJi-Ze Hong (Peter Hong) 
277c2236facSJi-Ze Hong (Peter Hong) 	default: /* Default 16Bytes FIFO */
278c2236facSJi-Ze Hong (Peter Hong) 		break;
279c2236facSJi-Ze Hong (Peter Hong) 	}
280c2236facSJi-Ze Hong (Peter Hong) }
281c2236facSJi-Ze Hong (Peter Hong) 
282fab8a02bSLukas Redlinger static void fintek_8250_goto_highspeed(struct uart_8250_port *uart,
283fab8a02bSLukas Redlinger 			      struct fintek_8250 *pdata)
284fab8a02bSLukas Redlinger {
285fab8a02bSLukas Redlinger 	sio_write_reg(pdata, LDN, pdata->index);
286fab8a02bSLukas Redlinger 
287fab8a02bSLukas Redlinger 	switch (pdata->pid) {
288fab8a02bSLukas Redlinger 	case CHIP_ID_F81866: /* set uart clock for high speed serial mode */
289fab8a02bSLukas Redlinger 		sio_write_mask_reg(pdata, F81866_UART_CLK,
290fab8a02bSLukas Redlinger 			F81866_UART_CLK_MASK,
291fab8a02bSLukas Redlinger 			F81866_UART_CLK_14_769MHZ);
292fab8a02bSLukas Redlinger 
293fab8a02bSLukas Redlinger 		uart->port.uartclk = 921600 * 16;
294fab8a02bSLukas Redlinger 		break;
295fab8a02bSLukas Redlinger 	default: /* leave clock speed untouched */
296fab8a02bSLukas Redlinger 		break;
297fab8a02bSLukas Redlinger 	}
298fab8a02bSLukas Redlinger }
299fab8a02bSLukas Redlinger 
300195638b6SJi-Ze Hong (Peter Hong) void fintek_8250_set_termios(struct uart_port *port, struct ktermios *termios,
301195638b6SJi-Ze Hong (Peter Hong) 			struct ktermios *old)
302195638b6SJi-Ze Hong (Peter Hong) {
303195638b6SJi-Ze Hong (Peter Hong) 	struct fintek_8250 *pdata = port->private_data;
304195638b6SJi-Ze Hong (Peter Hong) 	unsigned int baud = tty_termios_baud_rate(termios);
305195638b6SJi-Ze Hong (Peter Hong) 	int i;
30658178914SJi-Ze Hong (Peter Hong) 	u8 reg;
307195638b6SJi-Ze Hong (Peter Hong) 	static u32 baudrate_table[] = {115200, 921600, 1152000, 1500000};
308195638b6SJi-Ze Hong (Peter Hong) 	static u8 clock_table[] = { F81866_UART_CLK_1_8432MHZ,
309195638b6SJi-Ze Hong (Peter Hong) 			F81866_UART_CLK_14_769MHZ, F81866_UART_CLK_18_432MHZ,
310195638b6SJi-Ze Hong (Peter Hong) 			F81866_UART_CLK_24MHZ };
311195638b6SJi-Ze Hong (Peter Hong) 
31258178914SJi-Ze Hong (Peter Hong) 	switch (pdata->pid) {
31358178914SJi-Ze Hong (Peter Hong) 	case CHIP_ID_F81216H:
31458178914SJi-Ze Hong (Peter Hong) 		reg = RS485;
31558178914SJi-Ze Hong (Peter Hong) 		break;
31658178914SJi-Ze Hong (Peter Hong) 	case CHIP_ID_F81866:
31758178914SJi-Ze Hong (Peter Hong) 		reg = F81866_UART_CLK;
31858178914SJi-Ze Hong (Peter Hong) 		break;
31958178914SJi-Ze Hong (Peter Hong) 	default:
32058178914SJi-Ze Hong (Peter Hong) 		/* Don't change clocksource with unknown PID */
32158178914SJi-Ze Hong (Peter Hong) 		dev_warn(port->dev,
32258178914SJi-Ze Hong (Peter Hong) 			"%s: pid: %x Not support. use default set_termios.\n",
32358178914SJi-Ze Hong (Peter Hong) 			__func__, pdata->pid);
32458178914SJi-Ze Hong (Peter Hong) 		serial8250_do_set_termios(port, termios, old);
32558178914SJi-Ze Hong (Peter Hong) 		return;
32658178914SJi-Ze Hong (Peter Hong) 	}
32758178914SJi-Ze Hong (Peter Hong) 
328195638b6SJi-Ze Hong (Peter Hong) 	for (i = 0; i < ARRAY_SIZE(baudrate_table); ++i) {
329195638b6SJi-Ze Hong (Peter Hong) 		if (baud > baudrate_table[i] || baudrate_table[i] % baud != 0)
330195638b6SJi-Ze Hong (Peter Hong) 			continue;
331195638b6SJi-Ze Hong (Peter Hong) 
332195638b6SJi-Ze Hong (Peter Hong) 		if (port->uartclk == baudrate_table[i] * 16)
333195638b6SJi-Ze Hong (Peter Hong) 			break;
334195638b6SJi-Ze Hong (Peter Hong) 
335195638b6SJi-Ze Hong (Peter Hong) 		if (fintek_8250_enter_key(pdata->base_port, pdata->key))
336195638b6SJi-Ze Hong (Peter Hong) 			continue;
337195638b6SJi-Ze Hong (Peter Hong) 
338195638b6SJi-Ze Hong (Peter Hong) 		port->uartclk = baudrate_table[i] * 16;
339195638b6SJi-Ze Hong (Peter Hong) 
340195638b6SJi-Ze Hong (Peter Hong) 		sio_write_reg(pdata, LDN, pdata->index);
34158178914SJi-Ze Hong (Peter Hong) 		sio_write_mask_reg(pdata, reg, F81866_UART_CLK_MASK,
34258178914SJi-Ze Hong (Peter Hong) 				clock_table[i]);
343195638b6SJi-Ze Hong (Peter Hong) 
344195638b6SJi-Ze Hong (Peter Hong) 		fintek_8250_exit_key(pdata->base_port);
345195638b6SJi-Ze Hong (Peter Hong) 		break;
346195638b6SJi-Ze Hong (Peter Hong) 	}
347195638b6SJi-Ze Hong (Peter Hong) 
348195638b6SJi-Ze Hong (Peter Hong) 	if (i == ARRAY_SIZE(baudrate_table)) {
349195638b6SJi-Ze Hong (Peter Hong) 		baud = tty_termios_baud_rate(old);
350195638b6SJi-Ze Hong (Peter Hong) 		tty_termios_encode_baud_rate(termios, baud, baud);
351195638b6SJi-Ze Hong (Peter Hong) 	}
352195638b6SJi-Ze Hong (Peter Hong) 
353195638b6SJi-Ze Hong (Peter Hong) 	serial8250_do_set_termios(port, termios, old);
354195638b6SJi-Ze Hong (Peter Hong) }
355195638b6SJi-Ze Hong (Peter Hong) 
356195638b6SJi-Ze Hong (Peter Hong) static void fintek_8250_set_termios_handler(struct uart_8250_port *uart)
357195638b6SJi-Ze Hong (Peter Hong) {
358195638b6SJi-Ze Hong (Peter Hong) 	struct fintek_8250 *pdata = uart->port.private_data;
359195638b6SJi-Ze Hong (Peter Hong) 
360195638b6SJi-Ze Hong (Peter Hong) 	switch (pdata->pid) {
36158178914SJi-Ze Hong (Peter Hong) 	case CHIP_ID_F81216H:
362195638b6SJi-Ze Hong (Peter Hong) 	case CHIP_ID_F81866:
363195638b6SJi-Ze Hong (Peter Hong) 		uart->port.set_termios = fintek_8250_set_termios;
364195638b6SJi-Ze Hong (Peter Hong) 		break;
365195638b6SJi-Ze Hong (Peter Hong) 
366195638b6SJi-Ze Hong (Peter Hong) 	default:
367195638b6SJi-Ze Hong (Peter Hong) 		break;
368195638b6SJi-Ze Hong (Peter Hong) 	}
369195638b6SJi-Ze Hong (Peter Hong) }
370195638b6SJi-Ze Hong (Peter Hong) 
371fab8a02bSLukas Redlinger static int probe_setup_port(struct fintek_8250 *pdata,
372fab8a02bSLukas Redlinger 					struct uart_8250_port *uart)
373017bec38SRicardo Ribalda Delgado {
374017bec38SRicardo Ribalda Delgado 	static const u16 addr[] = {0x4e, 0x2e};
375ce8c267eSRicardo Ribalda Delgado 	static const u8 keys[] = {0x77, 0xa0, 0x87, 0x67};
37606e39572SJi-Ze Hong (Peter Hong) 	struct irq_data *irq_data;
37706e39572SJi-Ze Hong (Peter Hong) 	bool level_mode = false;
378da60d6afSJi-Ze Hong (Peter Hong) 	int i, j, k, min, max;
379017bec38SRicardo Ribalda Delgado 
380017bec38SRicardo Ribalda Delgado 	for (i = 0; i < ARRAY_SIZE(addr); i++) {
381ce8c267eSRicardo Ribalda Delgado 		for (j = 0; j < ARRAY_SIZE(keys); j++) {
382f1232ac2SJi-Ze Hong (Peter Hong) 			pdata->base_port = addr[i];
383f1232ac2SJi-Ze Hong (Peter Hong) 			pdata->key = keys[j];
384017bec38SRicardo Ribalda Delgado 
385ce8c267eSRicardo Ribalda Delgado 			if (fintek_8250_enter_key(addr[i], keys[j]))
386017bec38SRicardo Ribalda Delgado 				continue;
387da60d6afSJi-Ze Hong (Peter Hong) 			if (fintek_8250_check_id(pdata) ||
388da60d6afSJi-Ze Hong (Peter Hong) 			    fintek_8250_get_ldn_range(pdata, &min, &max)) {
389017bec38SRicardo Ribalda Delgado 				fintek_8250_exit_key(addr[i]);
39029d58642SRicardo Ribalda Delgado 				continue;
39129d58642SRicardo Ribalda Delgado 			}
39229d58642SRicardo Ribalda Delgado 
393da60d6afSJi-Ze Hong (Peter Hong) 			for (k = min; k < max; k++) {
39429d58642SRicardo Ribalda Delgado 				u16 aux;
39529d58642SRicardo Ribalda Delgado 
396f1232ac2SJi-Ze Hong (Peter Hong) 				sio_write_reg(pdata, LDN, k);
397f1232ac2SJi-Ze Hong (Peter Hong) 				aux = sio_read_reg(pdata, IO_ADDR1);
398f1232ac2SJi-Ze Hong (Peter Hong) 				aux |= sio_read_reg(pdata, IO_ADDR2) << 8;
399fab8a02bSLukas Redlinger 				if (aux != uart->port.iobase)
40029d58642SRicardo Ribalda Delgado 					continue;
40129d58642SRicardo Ribalda Delgado 
402fa01e2caSRicardo Ribalda Delgado 				pdata->index = k;
403fa01e2caSRicardo Ribalda Delgado 
404fab8a02bSLukas Redlinger 				irq_data = irq_get_irq_data(uart->port.irq);
40506e39572SJi-Ze Hong (Peter Hong) 				if (irq_data)
40606e39572SJi-Ze Hong (Peter Hong) 					level_mode =
40706e39572SJi-Ze Hong (Peter Hong) 						irqd_is_level_type(irq_data);
40806e39572SJi-Ze Hong (Peter Hong) 
40906e39572SJi-Ze Hong (Peter Hong) 				fintek_8250_set_irq_mode(pdata, level_mode);
410c2236facSJi-Ze Hong (Peter Hong) 				fintek_8250_set_max_fifo(pdata);
411fab8a02bSLukas Redlinger 				fintek_8250_goto_highspeed(uart, pdata);
412fab8a02bSLukas Redlinger 
41306e39572SJi-Ze Hong (Peter Hong) 				fintek_8250_exit_key(addr[i]);
41406e39572SJi-Ze Hong (Peter Hong) 
415fa01e2caSRicardo Ribalda Delgado 				return 0;
416017bec38SRicardo Ribalda Delgado 			}
417fa01e2caSRicardo Ribalda Delgado 
41829d58642SRicardo Ribalda Delgado 			fintek_8250_exit_key(addr[i]);
419ce8c267eSRicardo Ribalda Delgado 		}
420ce8c267eSRicardo Ribalda Delgado 	}
421017bec38SRicardo Ribalda Delgado 
422017bec38SRicardo Ribalda Delgado 	return -ENODEV;
423017bec38SRicardo Ribalda Delgado }
424017bec38SRicardo Ribalda Delgado 
4251e26c472SJi-Ze Hong (Peter Hong) static void fintek_8250_set_rs485_handler(struct uart_8250_port *uart)
4261e26c472SJi-Ze Hong (Peter Hong) {
4271e26c472SJi-Ze Hong (Peter Hong) 	struct fintek_8250 *pdata = uart->port.private_data;
4281e26c472SJi-Ze Hong (Peter Hong) 
4291e26c472SJi-Ze Hong (Peter Hong) 	switch (pdata->pid) {
4301e26c472SJi-Ze Hong (Peter Hong) 	case CHIP_ID_F81216AD:
4311e26c472SJi-Ze Hong (Peter Hong) 	case CHIP_ID_F81216H:
432da60d6afSJi-Ze Hong (Peter Hong) 	case CHIP_ID_F81866:
433de48b099SJi-Ze Hong (Peter Hong) 	case CHIP_ID_F81865:
4341e26c472SJi-Ze Hong (Peter Hong) 		uart->port.rs485_config = fintek_8250_rs485_config;
4351e26c472SJi-Ze Hong (Peter Hong) 		break;
4361e26c472SJi-Ze Hong (Peter Hong) 
4371e26c472SJi-Ze Hong (Peter Hong) 	default: /* No RS485 Auto direction functional */
4381e26c472SJi-Ze Hong (Peter Hong) 		break;
4391e26c472SJi-Ze Hong (Peter Hong) 	}
4401e26c472SJi-Ze Hong (Peter Hong) }
4411e26c472SJi-Ze Hong (Peter Hong) 
442fa01e2caSRicardo Ribalda Delgado int fintek_8250_probe(struct uart_8250_port *uart)
44328e3fb6cSRicardo Ribalda Delgado {
44492a5f11aSRicardo Ribalda Delgado 	struct fintek_8250 *pdata;
445fa01e2caSRicardo Ribalda Delgado 	struct fintek_8250 probe_data;
44628e3fb6cSRicardo Ribalda Delgado 
447fab8a02bSLukas Redlinger 	if (probe_setup_port(&probe_data, uart))
44828e3fb6cSRicardo Ribalda Delgado 		return -ENODEV;
44928e3fb6cSRicardo Ribalda Delgado 
450fa01e2caSRicardo Ribalda Delgado 	pdata = devm_kzalloc(uart->port.dev, sizeof(*pdata), GFP_KERNEL);
45192a5f11aSRicardo Ribalda Delgado 	if (!pdata)
45292a5f11aSRicardo Ribalda Delgado 		return -ENOMEM;
45392a5f11aSRicardo Ribalda Delgado 
454fa01e2caSRicardo Ribalda Delgado 	memcpy(pdata, &probe_data, sizeof(probe_data));
455fa01e2caSRicardo Ribalda Delgado 	uart->port.private_data = pdata;
4561e26c472SJi-Ze Hong (Peter Hong) 	fintek_8250_set_rs485_handler(uart);
457195638b6SJi-Ze Hong (Peter Hong) 	fintek_8250_set_termios_handler(uart);
45828e3fb6cSRicardo Ribalda Delgado 
45906e39572SJi-Ze Hong (Peter Hong) 	return 0;
46028e3fb6cSRicardo Ribalda Delgado }
461