xref: /openbmc/linux/drivers/tty/serial/8250/8250_fintek.c (revision bec5b814d46c2a704c3c8148752e62a33e9fa6dc)
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
22423d9118SJi-Ze Hong (Peter Hong) #define CHIP_ID_F81966 0x0215
23c2236facSJi-Ze Hong (Peter Hong) #define CHIP_ID_F81216AD 0x1602
24c2236facSJi-Ze Hong (Peter Hong) #define CHIP_ID_F81216H 0x0501
251e26c472SJi-Ze Hong (Peter Hong) #define CHIP_ID_F81216 0x0802
2628e3fb6cSRicardo Ribalda Delgado #define VENDOR_ID1 0x23
2728e3fb6cSRicardo Ribalda Delgado #define VENDOR_ID1_VAL 0x19
2828e3fb6cSRicardo Ribalda Delgado #define VENDOR_ID2 0x24
2928e3fb6cSRicardo Ribalda Delgado #define VENDOR_ID2_VAL 0x34
3029d58642SRicardo Ribalda Delgado #define IO_ADDR1 0x61
3129d58642SRicardo Ribalda Delgado #define IO_ADDR2 0x60
3228e3fb6cSRicardo Ribalda Delgado #define LDN 0x7
3328e3fb6cSRicardo Ribalda Delgado 
3487a713c8SArnd Bergmann #define FINTEK_IRQ_MODE	0x70
354da22f14SJi-Ze Hong (Peter Hong) #define IRQ_SHARE	BIT(4)
364da22f14SJi-Ze Hong (Peter Hong) #define IRQ_MODE_MASK	(BIT(6) | BIT(5))
374da22f14SJi-Ze Hong (Peter Hong) #define IRQ_LEVEL_LOW	0
384da22f14SJi-Ze Hong (Peter Hong) #define IRQ_EDGE_HIGH	BIT(5)
394da22f14SJi-Ze Hong (Peter Hong) 
4058178914SJi-Ze Hong (Peter Hong) /*
4158178914SJi-Ze Hong (Peter Hong)  * F81216H clock source register, the value and mask is the same with F81866,
4258178914SJi-Ze Hong (Peter Hong)  * but it's on F0h.
4358178914SJi-Ze Hong (Peter Hong)  *
4458178914SJi-Ze Hong (Peter Hong)  * Clock speeds for UART (register F0h)
4558178914SJi-Ze Hong (Peter Hong)  * 00: 1.8432MHz.
4658178914SJi-Ze Hong (Peter Hong)  * 01: 18.432MHz.
4758178914SJi-Ze Hong (Peter Hong)  * 10: 24MHz.
4858178914SJi-Ze Hong (Peter Hong)  * 11: 14.769MHz.
4958178914SJi-Ze Hong (Peter Hong)  */
5028e3fb6cSRicardo Ribalda Delgado #define RS485  0xF0
5128e3fb6cSRicardo Ribalda Delgado #define RTS_INVERT BIT(5)
5228e3fb6cSRicardo Ribalda Delgado #define RS485_URA BIT(4)
5328e3fb6cSRicardo Ribalda Delgado #define RXW4C_IRA BIT(3)
5428e3fb6cSRicardo Ribalda Delgado #define TXW4C_IRA BIT(2)
5528e3fb6cSRicardo Ribalda Delgado 
56c2236facSJi-Ze Hong (Peter Hong) #define FIFO_CTRL		0xF6
57c2236facSJi-Ze Hong (Peter Hong) #define FIFO_MODE_MASK		(BIT(1) | BIT(0))
58c2236facSJi-Ze Hong (Peter Hong) #define FIFO_MODE_128		(BIT(1) | BIT(0))
59c2236facSJi-Ze Hong (Peter Hong) #define RXFTHR_MODE_MASK	(BIT(5) | BIT(4))
60c2236facSJi-Ze Hong (Peter Hong) #define RXFTHR_MODE_4X		BIT(5)
61c2236facSJi-Ze Hong (Peter Hong) 
62da60d6afSJi-Ze Hong (Peter Hong) #define F81216_LDN_LOW	0x0
63da60d6afSJi-Ze Hong (Peter Hong) #define F81216_LDN_HIGH	0x4
64da60d6afSJi-Ze Hong (Peter Hong) 
65da60d6afSJi-Ze Hong (Peter Hong) /*
66423d9118SJi-Ze Hong (Peter Hong)  * F81866/966 registers
67da60d6afSJi-Ze Hong (Peter Hong)  *
68423d9118SJi-Ze Hong (Peter Hong)  * The IRQ setting mode of F81866/966 is not the same with F81216 series.
69da60d6afSJi-Ze Hong (Peter Hong)  *	Level/Low: IRQ_MODE0:0, IRQ_MODE1:0
70da60d6afSJi-Ze Hong (Peter Hong)  *	Edge/High: IRQ_MODE0:1, IRQ_MODE1:0
71fab8a02bSLukas Redlinger  *
72fab8a02bSLukas Redlinger  * Clock speeds for UART (register F2h)
73fab8a02bSLukas Redlinger  * 00: 1.8432MHz.
74fab8a02bSLukas Redlinger  * 01: 18.432MHz.
75fab8a02bSLukas Redlinger  * 10: 24MHz.
76fab8a02bSLukas Redlinger  * 11: 14.769MHz.
77da60d6afSJi-Ze Hong (Peter Hong)  */
78da60d6afSJi-Ze Hong (Peter Hong) #define F81866_IRQ_MODE		0xf0
79da60d6afSJi-Ze Hong (Peter Hong) #define F81866_IRQ_SHARE	BIT(0)
80da60d6afSJi-Ze Hong (Peter Hong) #define F81866_IRQ_MODE0	BIT(1)
81da60d6afSJi-Ze Hong (Peter Hong) 
82da60d6afSJi-Ze Hong (Peter Hong) #define F81866_FIFO_CTRL	FIFO_CTRL
83da60d6afSJi-Ze Hong (Peter Hong) #define F81866_IRQ_MODE1	BIT(3)
84da60d6afSJi-Ze Hong (Peter Hong) 
85da60d6afSJi-Ze Hong (Peter Hong) #define F81866_LDN_LOW		0x10
86da60d6afSJi-Ze Hong (Peter Hong) #define F81866_LDN_HIGH		0x16
87da60d6afSJi-Ze Hong (Peter Hong) 
88fab8a02bSLukas Redlinger #define F81866_UART_CLK 0xF2
89fab8a02bSLukas Redlinger #define F81866_UART_CLK_MASK (BIT(1) | BIT(0))
90fab8a02bSLukas Redlinger #define F81866_UART_CLK_1_8432MHZ 0
91fab8a02bSLukas Redlinger #define F81866_UART_CLK_14_769MHZ (BIT(1) | BIT(0))
92fab8a02bSLukas Redlinger #define F81866_UART_CLK_18_432MHZ BIT(0)
93fab8a02bSLukas Redlinger #define F81866_UART_CLK_24MHZ BIT(1)
94fab8a02bSLukas Redlinger 
9592a5f11aSRicardo Ribalda Delgado struct fintek_8250 {
96c2236facSJi-Ze Hong (Peter Hong) 	u16 pid;
97017bec38SRicardo Ribalda Delgado 	u16 base_port;
9892a5f11aSRicardo Ribalda Delgado 	u8 index;
99ce8c267eSRicardo Ribalda Delgado 	u8 key;
10092a5f11aSRicardo Ribalda Delgado };
10192a5f11aSRicardo Ribalda Delgado 
102f1232ac2SJi-Ze Hong (Peter Hong) static u8 sio_read_reg(struct fintek_8250 *pdata, u8 reg)
103f1232ac2SJi-Ze Hong (Peter Hong) {
104f1232ac2SJi-Ze Hong (Peter Hong) 	outb(reg, pdata->base_port + ADDR_PORT);
105f1232ac2SJi-Ze Hong (Peter Hong) 	return inb(pdata->base_port + DATA_PORT);
106f1232ac2SJi-Ze Hong (Peter Hong) }
107f1232ac2SJi-Ze Hong (Peter Hong) 
108f1232ac2SJi-Ze Hong (Peter Hong) static void sio_write_reg(struct fintek_8250 *pdata, u8 reg, u8 data)
109f1232ac2SJi-Ze Hong (Peter Hong) {
110f1232ac2SJi-Ze Hong (Peter Hong) 	outb(reg, pdata->base_port + ADDR_PORT);
111f1232ac2SJi-Ze Hong (Peter Hong) 	outb(data, pdata->base_port + DATA_PORT);
112f1232ac2SJi-Ze Hong (Peter Hong) }
113f1232ac2SJi-Ze Hong (Peter Hong) 
114f1232ac2SJi-Ze Hong (Peter Hong) static void sio_write_mask_reg(struct fintek_8250 *pdata, u8 reg, u8 mask,
115f1232ac2SJi-Ze Hong (Peter Hong) 			       u8 data)
116f1232ac2SJi-Ze Hong (Peter Hong) {
117f1232ac2SJi-Ze Hong (Peter Hong) 	u8 tmp;
118f1232ac2SJi-Ze Hong (Peter Hong) 
119f1232ac2SJi-Ze Hong (Peter Hong) 	tmp = (sio_read_reg(pdata, reg) & ~mask) | (mask & data);
120f1232ac2SJi-Ze Hong (Peter Hong) 	sio_write_reg(pdata, reg, tmp);
121f1232ac2SJi-Ze Hong (Peter Hong) }
122f1232ac2SJi-Ze Hong (Peter Hong) 
123ce8c267eSRicardo Ribalda Delgado static int fintek_8250_enter_key(u16 base_port, u8 key)
124017bec38SRicardo Ribalda Delgado {
125fa01e2caSRicardo Ribalda Delgado 	if (!request_muxed_region(base_port, 2, "8250_fintek"))
12628e3fb6cSRicardo Ribalda Delgado 		return -EBUSY;
12728e3fb6cSRicardo Ribalda Delgado 
128fd97e66cSJi-Ze Hong (Peter Hong) 	/* Force to deactive all SuperIO in this base_port */
129fd97e66cSJi-Ze Hong (Peter Hong) 	outb(EXIT_KEY, base_port + ADDR_PORT);
130fd97e66cSJi-Ze Hong (Peter Hong) 
131ce8c267eSRicardo Ribalda Delgado 	outb(key, base_port + ADDR_PORT);
132ce8c267eSRicardo Ribalda Delgado 	outb(key, base_port + ADDR_PORT);
13328e3fb6cSRicardo Ribalda Delgado 	return 0;
13428e3fb6cSRicardo Ribalda Delgado }
13528e3fb6cSRicardo Ribalda Delgado 
136017bec38SRicardo Ribalda Delgado static void fintek_8250_exit_key(u16 base_port)
137017bec38SRicardo Ribalda Delgado {
13828e3fb6cSRicardo Ribalda Delgado 
139017bec38SRicardo Ribalda Delgado 	outb(EXIT_KEY, base_port + ADDR_PORT);
140017bec38SRicardo Ribalda Delgado 	release_region(base_port + ADDR_PORT, 2);
14128e3fb6cSRicardo Ribalda Delgado }
14228e3fb6cSRicardo Ribalda Delgado 
143f1232ac2SJi-Ze Hong (Peter Hong) static int fintek_8250_check_id(struct fintek_8250 *pdata)
14428e3fb6cSRicardo Ribalda Delgado {
145dae77f75SRicardo Ribalda Delgado 	u16 chip;
14628e3fb6cSRicardo Ribalda Delgado 
147f1232ac2SJi-Ze Hong (Peter Hong) 	if (sio_read_reg(pdata, VENDOR_ID1) != VENDOR_ID1_VAL)
14828e3fb6cSRicardo Ribalda Delgado 		return -ENODEV;
14928e3fb6cSRicardo Ribalda Delgado 
150f1232ac2SJi-Ze Hong (Peter Hong) 	if (sio_read_reg(pdata, VENDOR_ID2) != VENDOR_ID2_VAL)
15128e3fb6cSRicardo Ribalda Delgado 		return -ENODEV;
15228e3fb6cSRicardo Ribalda Delgado 
153f1232ac2SJi-Ze Hong (Peter Hong) 	chip = sio_read_reg(pdata, CHIP_ID1);
154f1232ac2SJi-Ze Hong (Peter Hong) 	chip |= sio_read_reg(pdata, CHIP_ID2) << 8;
155dae77f75SRicardo Ribalda Delgado 
1561e26c472SJi-Ze Hong (Peter Hong) 	switch (chip) {
157de48b099SJi-Ze Hong (Peter Hong) 	case CHIP_ID_F81865:
158da60d6afSJi-Ze Hong (Peter Hong) 	case CHIP_ID_F81866:
159423d9118SJi-Ze Hong (Peter Hong) 	case CHIP_ID_F81966:
1601e26c472SJi-Ze Hong (Peter Hong) 	case CHIP_ID_F81216AD:
1611e26c472SJi-Ze Hong (Peter Hong) 	case CHIP_ID_F81216H:
1621e26c472SJi-Ze Hong (Peter Hong) 	case CHIP_ID_F81216:
1631e26c472SJi-Ze Hong (Peter Hong) 		break;
1641e26c472SJi-Ze Hong (Peter Hong) 	default:
165dae77f75SRicardo Ribalda Delgado 		return -ENODEV;
1661e26c472SJi-Ze Hong (Peter Hong) 	}
167dae77f75SRicardo Ribalda Delgado 
168c2236facSJi-Ze Hong (Peter Hong) 	pdata->pid = chip;
16928e3fb6cSRicardo Ribalda Delgado 	return 0;
17028e3fb6cSRicardo Ribalda Delgado }
17128e3fb6cSRicardo Ribalda Delgado 
172da60d6afSJi-Ze Hong (Peter Hong) static int fintek_8250_get_ldn_range(struct fintek_8250 *pdata, int *min,
173da60d6afSJi-Ze Hong (Peter Hong) 				     int *max)
174da60d6afSJi-Ze Hong (Peter Hong) {
175da60d6afSJi-Ze Hong (Peter Hong) 	switch (pdata->pid) {
176423d9118SJi-Ze Hong (Peter Hong) 	case CHIP_ID_F81966:
177de48b099SJi-Ze Hong (Peter Hong) 	case CHIP_ID_F81865:
178da60d6afSJi-Ze Hong (Peter Hong) 	case CHIP_ID_F81866:
179da60d6afSJi-Ze Hong (Peter Hong) 		*min = F81866_LDN_LOW;
180da60d6afSJi-Ze Hong (Peter Hong) 		*max = F81866_LDN_HIGH;
181da60d6afSJi-Ze Hong (Peter Hong) 		return 0;
182da60d6afSJi-Ze Hong (Peter Hong) 
183da60d6afSJi-Ze Hong (Peter Hong) 	case CHIP_ID_F81216AD:
184da60d6afSJi-Ze Hong (Peter Hong) 	case CHIP_ID_F81216H:
185da60d6afSJi-Ze Hong (Peter Hong) 	case CHIP_ID_F81216:
186da60d6afSJi-Ze Hong (Peter Hong) 		*min = F81216_LDN_LOW;
187da60d6afSJi-Ze Hong (Peter Hong) 		*max = F81216_LDN_HIGH;
188da60d6afSJi-Ze Hong (Peter Hong) 		return 0;
189da60d6afSJi-Ze Hong (Peter Hong) 	}
190da60d6afSJi-Ze Hong (Peter Hong) 
191da60d6afSJi-Ze Hong (Peter Hong) 	return -ENODEV;
192da60d6afSJi-Ze Hong (Peter Hong) }
193da60d6afSJi-Ze Hong (Peter Hong) 
194ae50bb27SIlpo Järvinen static int fintek_8250_rs485_config(struct uart_port *port, struct ktermios *termios,
19528e3fb6cSRicardo Ribalda Delgado 			      struct serial_rs485 *rs485)
19628e3fb6cSRicardo Ribalda Delgado {
19728e3fb6cSRicardo Ribalda Delgado 	uint8_t config = 0;
19892a5f11aSRicardo Ribalda Delgado 	struct fintek_8250 *pdata = port->private_data;
19928e3fb6cSRicardo Ribalda Delgado 
20092a5f11aSRicardo Ribalda Delgado 	if (!pdata)
20128e3fb6cSRicardo Ribalda Delgado 		return -EINVAL;
20228e3fb6cSRicardo Ribalda Delgado 
203af017927SIlpo Järvinen 
204af017927SIlpo Järvinen 	if (rs485->flags & SER_RS485_ENABLED) {
2057ecc7701SRicardo Ribalda Delgado 		/* Hardware do not support same RTS level on send and receive */
2067ecc7701SRicardo Ribalda Delgado 		if (!(rs485->flags & SER_RS485_RTS_ON_SEND) ==
2077ecc7701SRicardo Ribalda Delgado 		    !(rs485->flags & SER_RS485_RTS_AFTER_SEND))
2087ecc7701SRicardo Ribalda Delgado 			return -EINVAL;
2097ecc7701SRicardo Ribalda Delgado 		config |= RS485_URA;
21021c4e7f2SRicardo Ribalda Delgado 	}
21121c4e7f2SRicardo Ribalda Delgado 
21228e3fb6cSRicardo Ribalda Delgado 	if (rs485->delay_rts_before_send) {
21328e3fb6cSRicardo Ribalda Delgado 		rs485->delay_rts_before_send = 1;
21428e3fb6cSRicardo Ribalda Delgado 		config |= TXW4C_IRA;
21528e3fb6cSRicardo Ribalda Delgado 	}
21628e3fb6cSRicardo Ribalda Delgado 
21728e3fb6cSRicardo Ribalda Delgado 	if (rs485->delay_rts_after_send) {
21828e3fb6cSRicardo Ribalda Delgado 		rs485->delay_rts_after_send = 1;
21928e3fb6cSRicardo Ribalda Delgado 		config |= RXW4C_IRA;
22028e3fb6cSRicardo Ribalda Delgado 	}
22128e3fb6cSRicardo Ribalda Delgado 
22228e3fb6cSRicardo Ribalda Delgado 	if (rs485->flags & SER_RS485_RTS_ON_SEND)
22328e3fb6cSRicardo Ribalda Delgado 		config |= RTS_INVERT;
22428e3fb6cSRicardo Ribalda Delgado 
225ce8c267eSRicardo Ribalda Delgado 	if (fintek_8250_enter_key(pdata->base_port, pdata->key))
22628e3fb6cSRicardo Ribalda Delgado 		return -EBUSY;
22728e3fb6cSRicardo Ribalda Delgado 
228f1232ac2SJi-Ze Hong (Peter Hong) 	sio_write_reg(pdata, LDN, pdata->index);
229f1232ac2SJi-Ze Hong (Peter Hong) 	sio_write_reg(pdata, RS485, config);
230017bec38SRicardo Ribalda Delgado 	fintek_8250_exit_key(pdata->base_port);
23128e3fb6cSRicardo Ribalda Delgado 
23228e3fb6cSRicardo Ribalda Delgado 	return 0;
23328e3fb6cSRicardo Ribalda Delgado }
23428e3fb6cSRicardo Ribalda Delgado 
23506e39572SJi-Ze Hong (Peter Hong) static void fintek_8250_set_irq_mode(struct fintek_8250 *pdata, bool is_level)
23606e39572SJi-Ze Hong (Peter Hong) {
23706e39572SJi-Ze Hong (Peter Hong) 	sio_write_reg(pdata, LDN, pdata->index);
238da60d6afSJi-Ze Hong (Peter Hong) 
239da60d6afSJi-Ze Hong (Peter Hong) 	switch (pdata->pid) {
240423d9118SJi-Ze Hong (Peter Hong) 	case CHIP_ID_F81966:
241da60d6afSJi-Ze Hong (Peter Hong) 	case CHIP_ID_F81866:
242da60d6afSJi-Ze Hong (Peter Hong) 		sio_write_mask_reg(pdata, F81866_FIFO_CTRL, F81866_IRQ_MODE1,
243da60d6afSJi-Ze Hong (Peter Hong) 				   0);
244df561f66SGustavo A. R. Silva 		fallthrough;
245de48b099SJi-Ze Hong (Peter Hong) 	case CHIP_ID_F81865:
246da60d6afSJi-Ze Hong (Peter Hong) 		sio_write_mask_reg(pdata, F81866_IRQ_MODE, F81866_IRQ_SHARE,
247da60d6afSJi-Ze Hong (Peter Hong) 				   F81866_IRQ_SHARE);
248da60d6afSJi-Ze Hong (Peter Hong) 		sio_write_mask_reg(pdata, F81866_IRQ_MODE, F81866_IRQ_MODE0,
249da60d6afSJi-Ze Hong (Peter Hong) 				   is_level ? 0 : F81866_IRQ_MODE0);
250da60d6afSJi-Ze Hong (Peter Hong) 		break;
251da60d6afSJi-Ze Hong (Peter Hong) 
252da60d6afSJi-Ze Hong (Peter Hong) 	case CHIP_ID_F81216AD:
253da60d6afSJi-Ze Hong (Peter Hong) 	case CHIP_ID_F81216H:
254da60d6afSJi-Ze Hong (Peter Hong) 	case CHIP_ID_F81216:
255da60d6afSJi-Ze Hong (Peter Hong) 		sio_write_mask_reg(pdata, FINTEK_IRQ_MODE, IRQ_SHARE,
256da60d6afSJi-Ze Hong (Peter Hong) 				   IRQ_SHARE);
25706e39572SJi-Ze Hong (Peter Hong) 		sio_write_mask_reg(pdata, FINTEK_IRQ_MODE, IRQ_MODE_MASK,
25806e39572SJi-Ze Hong (Peter Hong) 				   is_level ? IRQ_LEVEL_LOW : IRQ_EDGE_HIGH);
259da60d6afSJi-Ze Hong (Peter Hong) 		break;
260da60d6afSJi-Ze Hong (Peter Hong) 	}
26106e39572SJi-Ze Hong (Peter Hong) }
26206e39572SJi-Ze Hong (Peter Hong) 
263c2236facSJi-Ze Hong (Peter Hong) static void fintek_8250_set_max_fifo(struct fintek_8250 *pdata)
264c2236facSJi-Ze Hong (Peter Hong) {
265c2236facSJi-Ze Hong (Peter Hong) 	switch (pdata->pid) {
266c2236facSJi-Ze Hong (Peter Hong) 	case CHIP_ID_F81216H: /* 128Bytes FIFO */
267423d9118SJi-Ze Hong (Peter Hong) 	case CHIP_ID_F81966:
268da60d6afSJi-Ze Hong (Peter Hong) 	case CHIP_ID_F81866:
269c2236facSJi-Ze Hong (Peter Hong) 		sio_write_mask_reg(pdata, FIFO_CTRL,
270c2236facSJi-Ze Hong (Peter Hong) 				   FIFO_MODE_MASK | RXFTHR_MODE_MASK,
271c2236facSJi-Ze Hong (Peter Hong) 				   FIFO_MODE_128 | RXFTHR_MODE_4X);
272c2236facSJi-Ze Hong (Peter Hong) 		break;
273c2236facSJi-Ze Hong (Peter Hong) 
274c2236facSJi-Ze Hong (Peter Hong) 	default: /* Default 16Bytes FIFO */
275c2236facSJi-Ze Hong (Peter Hong) 		break;
276c2236facSJi-Ze Hong (Peter Hong) 	}
277c2236facSJi-Ze Hong (Peter Hong) }
278c2236facSJi-Ze Hong (Peter Hong) 
2799828def3SYueHaibing static void fintek_8250_set_termios(struct uart_port *port,
2809828def3SYueHaibing 				    struct ktermios *termios,
281*bec5b814SIlpo Järvinen 				    const struct ktermios *old)
282195638b6SJi-Ze Hong (Peter Hong) {
283195638b6SJi-Ze Hong (Peter Hong) 	struct fintek_8250 *pdata = port->private_data;
284195638b6SJi-Ze Hong (Peter Hong) 	unsigned int baud = tty_termios_baud_rate(termios);
285195638b6SJi-Ze Hong (Peter Hong) 	int i;
28658178914SJi-Ze Hong (Peter Hong) 	u8 reg;
287195638b6SJi-Ze Hong (Peter Hong) 	static u32 baudrate_table[] = {115200, 921600, 1152000, 1500000};
288195638b6SJi-Ze Hong (Peter Hong) 	static u8 clock_table[] = { F81866_UART_CLK_1_8432MHZ,
289195638b6SJi-Ze Hong (Peter Hong) 			F81866_UART_CLK_14_769MHZ, F81866_UART_CLK_18_432MHZ,
290195638b6SJi-Ze Hong (Peter Hong) 			F81866_UART_CLK_24MHZ };
291195638b6SJi-Ze Hong (Peter Hong) 
29207a708f0SJi-Ze Hong (Peter Hong) 	/*
29307a708f0SJi-Ze Hong (Peter Hong) 	 * We'll use serial8250_do_set_termios() for baud = 0, otherwise It'll
29407a708f0SJi-Ze Hong (Peter Hong) 	 * crash on baudrate_table[i] % baud with "division by zero".
29507a708f0SJi-Ze Hong (Peter Hong) 	 */
29607a708f0SJi-Ze Hong (Peter Hong) 	if (!baud)
29707a708f0SJi-Ze Hong (Peter Hong) 		goto exit;
29807a708f0SJi-Ze Hong (Peter Hong) 
29958178914SJi-Ze Hong (Peter Hong) 	switch (pdata->pid) {
30058178914SJi-Ze Hong (Peter Hong) 	case CHIP_ID_F81216H:
30158178914SJi-Ze Hong (Peter Hong) 		reg = RS485;
30258178914SJi-Ze Hong (Peter Hong) 		break;
303423d9118SJi-Ze Hong (Peter Hong) 	case CHIP_ID_F81966:
30458178914SJi-Ze Hong (Peter Hong) 	case CHIP_ID_F81866:
30558178914SJi-Ze Hong (Peter Hong) 		reg = F81866_UART_CLK;
30658178914SJi-Ze Hong (Peter Hong) 		break;
30758178914SJi-Ze Hong (Peter Hong) 	default:
30858178914SJi-Ze Hong (Peter Hong) 		/* Don't change clocksource with unknown PID */
30958178914SJi-Ze Hong (Peter Hong) 		dev_warn(port->dev,
31058178914SJi-Ze Hong (Peter Hong) 			"%s: pid: %x Not support. use default set_termios.\n",
31158178914SJi-Ze Hong (Peter Hong) 			__func__, pdata->pid);
31207a708f0SJi-Ze Hong (Peter Hong) 		goto exit;
31358178914SJi-Ze Hong (Peter Hong) 	}
31458178914SJi-Ze Hong (Peter Hong) 
315195638b6SJi-Ze Hong (Peter Hong) 	for (i = 0; i < ARRAY_SIZE(baudrate_table); ++i) {
316195638b6SJi-Ze Hong (Peter Hong) 		if (baud > baudrate_table[i] || baudrate_table[i] % baud != 0)
317195638b6SJi-Ze Hong (Peter Hong) 			continue;
318195638b6SJi-Ze Hong (Peter Hong) 
319195638b6SJi-Ze Hong (Peter Hong) 		if (port->uartclk == baudrate_table[i] * 16)
320195638b6SJi-Ze Hong (Peter Hong) 			break;
321195638b6SJi-Ze Hong (Peter Hong) 
322195638b6SJi-Ze Hong (Peter Hong) 		if (fintek_8250_enter_key(pdata->base_port, pdata->key))
323195638b6SJi-Ze Hong (Peter Hong) 			continue;
324195638b6SJi-Ze Hong (Peter Hong) 
325195638b6SJi-Ze Hong (Peter Hong) 		port->uartclk = baudrate_table[i] * 16;
326195638b6SJi-Ze Hong (Peter Hong) 
327195638b6SJi-Ze Hong (Peter Hong) 		sio_write_reg(pdata, LDN, pdata->index);
32858178914SJi-Ze Hong (Peter Hong) 		sio_write_mask_reg(pdata, reg, F81866_UART_CLK_MASK,
32958178914SJi-Ze Hong (Peter Hong) 				clock_table[i]);
330195638b6SJi-Ze Hong (Peter Hong) 
331195638b6SJi-Ze Hong (Peter Hong) 		fintek_8250_exit_key(pdata->base_port);
332195638b6SJi-Ze Hong (Peter Hong) 		break;
333195638b6SJi-Ze Hong (Peter Hong) 	}
334195638b6SJi-Ze Hong (Peter Hong) 
335195638b6SJi-Ze Hong (Peter Hong) 	if (i == ARRAY_SIZE(baudrate_table)) {
336195638b6SJi-Ze Hong (Peter Hong) 		baud = tty_termios_baud_rate(old);
337195638b6SJi-Ze Hong (Peter Hong) 		tty_termios_encode_baud_rate(termios, baud, baud);
338195638b6SJi-Ze Hong (Peter Hong) 	}
339195638b6SJi-Ze Hong (Peter Hong) 
34007a708f0SJi-Ze Hong (Peter Hong) exit:
341195638b6SJi-Ze Hong (Peter Hong) 	serial8250_do_set_termios(port, termios, old);
342195638b6SJi-Ze Hong (Peter Hong) }
343195638b6SJi-Ze Hong (Peter Hong) 
344195638b6SJi-Ze Hong (Peter Hong) static void fintek_8250_set_termios_handler(struct uart_8250_port *uart)
345195638b6SJi-Ze Hong (Peter Hong) {
346195638b6SJi-Ze Hong (Peter Hong) 	struct fintek_8250 *pdata = uart->port.private_data;
347195638b6SJi-Ze Hong (Peter Hong) 
348195638b6SJi-Ze Hong (Peter Hong) 	switch (pdata->pid) {
34958178914SJi-Ze Hong (Peter Hong) 	case CHIP_ID_F81216H:
350423d9118SJi-Ze Hong (Peter Hong) 	case CHIP_ID_F81966:
351195638b6SJi-Ze Hong (Peter Hong) 	case CHIP_ID_F81866:
352195638b6SJi-Ze Hong (Peter Hong) 		uart->port.set_termios = fintek_8250_set_termios;
353195638b6SJi-Ze Hong (Peter Hong) 		break;
354195638b6SJi-Ze Hong (Peter Hong) 
355195638b6SJi-Ze Hong (Peter Hong) 	default:
356195638b6SJi-Ze Hong (Peter Hong) 		break;
357195638b6SJi-Ze Hong (Peter Hong) 	}
358195638b6SJi-Ze Hong (Peter Hong) }
359195638b6SJi-Ze Hong (Peter Hong) 
360fab8a02bSLukas Redlinger static int probe_setup_port(struct fintek_8250 *pdata,
361fab8a02bSLukas Redlinger 					struct uart_8250_port *uart)
362017bec38SRicardo Ribalda Delgado {
363017bec38SRicardo Ribalda Delgado 	static const u16 addr[] = {0x4e, 0x2e};
364ce8c267eSRicardo Ribalda Delgado 	static const u8 keys[] = {0x77, 0xa0, 0x87, 0x67};
36506e39572SJi-Ze Hong (Peter Hong) 	struct irq_data *irq_data;
36606e39572SJi-Ze Hong (Peter Hong) 	bool level_mode = false;
367da60d6afSJi-Ze Hong (Peter Hong) 	int i, j, k, min, max;
368017bec38SRicardo Ribalda Delgado 
369017bec38SRicardo Ribalda Delgado 	for (i = 0; i < ARRAY_SIZE(addr); i++) {
370ce8c267eSRicardo Ribalda Delgado 		for (j = 0; j < ARRAY_SIZE(keys); j++) {
371f1232ac2SJi-Ze Hong (Peter Hong) 			pdata->base_port = addr[i];
372f1232ac2SJi-Ze Hong (Peter Hong) 			pdata->key = keys[j];
373017bec38SRicardo Ribalda Delgado 
374ce8c267eSRicardo Ribalda Delgado 			if (fintek_8250_enter_key(addr[i], keys[j]))
375017bec38SRicardo Ribalda Delgado 				continue;
376da60d6afSJi-Ze Hong (Peter Hong) 			if (fintek_8250_check_id(pdata) ||
377da60d6afSJi-Ze Hong (Peter Hong) 			    fintek_8250_get_ldn_range(pdata, &min, &max)) {
378017bec38SRicardo Ribalda Delgado 				fintek_8250_exit_key(addr[i]);
37929d58642SRicardo Ribalda Delgado 				continue;
38029d58642SRicardo Ribalda Delgado 			}
38129d58642SRicardo Ribalda Delgado 
382da60d6afSJi-Ze Hong (Peter Hong) 			for (k = min; k < max; k++) {
38329d58642SRicardo Ribalda Delgado 				u16 aux;
38429d58642SRicardo Ribalda Delgado 
385f1232ac2SJi-Ze Hong (Peter Hong) 				sio_write_reg(pdata, LDN, k);
386f1232ac2SJi-Ze Hong (Peter Hong) 				aux = sio_read_reg(pdata, IO_ADDR1);
387f1232ac2SJi-Ze Hong (Peter Hong) 				aux |= sio_read_reg(pdata, IO_ADDR2) << 8;
388fab8a02bSLukas Redlinger 				if (aux != uart->port.iobase)
38929d58642SRicardo Ribalda Delgado 					continue;
39029d58642SRicardo Ribalda Delgado 
391fa01e2caSRicardo Ribalda Delgado 				pdata->index = k;
392fa01e2caSRicardo Ribalda Delgado 
393fab8a02bSLukas Redlinger 				irq_data = irq_get_irq_data(uart->port.irq);
39406e39572SJi-Ze Hong (Peter Hong) 				if (irq_data)
39506e39572SJi-Ze Hong (Peter Hong) 					level_mode =
39606e39572SJi-Ze Hong (Peter Hong) 						irqd_is_level_type(irq_data);
39706e39572SJi-Ze Hong (Peter Hong) 
39806e39572SJi-Ze Hong (Peter Hong) 				fintek_8250_set_irq_mode(pdata, level_mode);
399c2236facSJi-Ze Hong (Peter Hong) 				fintek_8250_set_max_fifo(pdata);
400fab8a02bSLukas Redlinger 
40106e39572SJi-Ze Hong (Peter Hong) 				fintek_8250_exit_key(addr[i]);
40206e39572SJi-Ze Hong (Peter Hong) 
403fa01e2caSRicardo Ribalda Delgado 				return 0;
404017bec38SRicardo Ribalda Delgado 			}
405fa01e2caSRicardo Ribalda Delgado 
40629d58642SRicardo Ribalda Delgado 			fintek_8250_exit_key(addr[i]);
407ce8c267eSRicardo Ribalda Delgado 		}
408ce8c267eSRicardo Ribalda Delgado 	}
409017bec38SRicardo Ribalda Delgado 
410017bec38SRicardo Ribalda Delgado 	return -ENODEV;
411017bec38SRicardo Ribalda Delgado }
412017bec38SRicardo Ribalda Delgado 
41370780464SIlpo Järvinen /* Only the first port supports delays */
41470780464SIlpo Järvinen static const struct serial_rs485 fintek_8250_rs485_supported_port0 = {
41570780464SIlpo Järvinen 	.flags = SER_RS485_ENABLED | SER_RS485_RTS_ON_SEND | SER_RS485_RTS_AFTER_SEND,
41670780464SIlpo Järvinen 	.delay_rts_before_send = 1,
41770780464SIlpo Järvinen 	.delay_rts_after_send = 1,
41870780464SIlpo Järvinen };
41970780464SIlpo Järvinen 
42070780464SIlpo Järvinen static const struct serial_rs485 fintek_8250_rs485_supported = {
42170780464SIlpo Järvinen 	.flags = SER_RS485_ENABLED | SER_RS485_RTS_ON_SEND | SER_RS485_RTS_AFTER_SEND,
42270780464SIlpo Järvinen };
42370780464SIlpo Järvinen 
4241e26c472SJi-Ze Hong (Peter Hong) static void fintek_8250_set_rs485_handler(struct uart_8250_port *uart)
4251e26c472SJi-Ze Hong (Peter Hong) {
4261e26c472SJi-Ze Hong (Peter Hong) 	struct fintek_8250 *pdata = uart->port.private_data;
4271e26c472SJi-Ze Hong (Peter Hong) 
4281e26c472SJi-Ze Hong (Peter Hong) 	switch (pdata->pid) {
4291e26c472SJi-Ze Hong (Peter Hong) 	case CHIP_ID_F81216AD:
4301e26c472SJi-Ze Hong (Peter Hong) 	case CHIP_ID_F81216H:
431423d9118SJi-Ze Hong (Peter Hong) 	case CHIP_ID_F81966:
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;
43570780464SIlpo Järvinen 		if (!pdata->index)
4360139da50SIlpo Järvinen 			uart->port.rs485_supported = fintek_8250_rs485_supported_port0;
43770780464SIlpo Järvinen 		else
4380139da50SIlpo Järvinen 			uart->port.rs485_supported = fintek_8250_rs485_supported;
4391e26c472SJi-Ze Hong (Peter Hong) 		break;
4401e26c472SJi-Ze Hong (Peter Hong) 
4411e26c472SJi-Ze Hong (Peter Hong) 	default: /* No RS485 Auto direction functional */
4421e26c472SJi-Ze Hong (Peter Hong) 		break;
4431e26c472SJi-Ze Hong (Peter Hong) 	}
4441e26c472SJi-Ze Hong (Peter Hong) }
4451e26c472SJi-Ze Hong (Peter Hong) 
446fa01e2caSRicardo Ribalda Delgado int fintek_8250_probe(struct uart_8250_port *uart)
44728e3fb6cSRicardo Ribalda Delgado {
44892a5f11aSRicardo Ribalda Delgado 	struct fintek_8250 *pdata;
449fa01e2caSRicardo Ribalda Delgado 	struct fintek_8250 probe_data;
45028e3fb6cSRicardo Ribalda Delgado 
451fab8a02bSLukas Redlinger 	if (probe_setup_port(&probe_data, uart))
45228e3fb6cSRicardo Ribalda Delgado 		return -ENODEV;
45328e3fb6cSRicardo Ribalda Delgado 
454fa01e2caSRicardo Ribalda Delgado 	pdata = devm_kzalloc(uart->port.dev, sizeof(*pdata), GFP_KERNEL);
45592a5f11aSRicardo Ribalda Delgado 	if (!pdata)
45692a5f11aSRicardo Ribalda Delgado 		return -ENOMEM;
45792a5f11aSRicardo Ribalda Delgado 
458fa01e2caSRicardo Ribalda Delgado 	memcpy(pdata, &probe_data, sizeof(probe_data));
459fa01e2caSRicardo Ribalda Delgado 	uart->port.private_data = pdata;
4601e26c472SJi-Ze Hong (Peter Hong) 	fintek_8250_set_rs485_handler(uart);
461195638b6SJi-Ze Hong (Peter Hong) 	fintek_8250_set_termios_handler(uart);
46228e3fb6cSRicardo Ribalda Delgado 
46306e39572SJi-Ze Hong (Peter Hong) 	return 0;
46428e3fb6cSRicardo Ribalda Delgado }
465