xref: /openbmc/linux/drivers/tty/serial/8250/8250_fintek.c (revision fd97e66c5529046e989a0879c3bb58fddb592c71)
128e3fb6cSRicardo Ribalda Delgado /*
228e3fb6cSRicardo Ribalda Delgado  *  Probe for F81216A LPC to 4 UART
328e3fb6cSRicardo Ribalda Delgado  *
4fa01e2caSRicardo Ribalda Delgado  *  Copyright (C) 2014-2016 Ricardo Ribalda, Qtechnology A/S
528e3fb6cSRicardo Ribalda Delgado  *
628e3fb6cSRicardo Ribalda Delgado  *
728e3fb6cSRicardo Ribalda Delgado  * This program is free software; you can redistribute it and/or modify
828e3fb6cSRicardo Ribalda Delgado  * it under the terms of the GNU General Public License as published by
928e3fb6cSRicardo Ribalda Delgado  * the Free Software Foundation; either version 2 of the License.
1028e3fb6cSRicardo Ribalda Delgado  */
1128e3fb6cSRicardo Ribalda Delgado #include <linux/module.h>
1228e3fb6cSRicardo Ribalda Delgado #include <linux/pci.h>
1328e3fb6cSRicardo Ribalda Delgado #include <linux/pnp.h>
1428e3fb6cSRicardo Ribalda Delgado #include <linux/kernel.h>
1528e3fb6cSRicardo Ribalda Delgado #include <linux/serial_core.h>
164da22f14SJi-Ze Hong (Peter Hong) #include <linux/irq.h>
1728e3fb6cSRicardo Ribalda Delgado #include  "8250.h"
1828e3fb6cSRicardo Ribalda Delgado 
19017bec38SRicardo Ribalda Delgado #define ADDR_PORT 0
20017bec38SRicardo Ribalda Delgado #define DATA_PORT 1
2128e3fb6cSRicardo Ribalda Delgado #define EXIT_KEY 0xAA
2228e3fb6cSRicardo Ribalda Delgado #define CHIP_ID1  0x20
2328e3fb6cSRicardo Ribalda Delgado #define CHIP_ID2  0x21
24de48b099SJi-Ze Hong (Peter Hong) #define CHIP_ID_F81865 0x0407
25da60d6afSJi-Ze Hong (Peter Hong) #define CHIP_ID_F81866 0x1010
26c2236facSJi-Ze Hong (Peter Hong) #define CHIP_ID_F81216AD 0x1602
27c2236facSJi-Ze Hong (Peter Hong) #define CHIP_ID_F81216H 0x0501
281e26c472SJi-Ze Hong (Peter Hong) #define CHIP_ID_F81216 0x0802
2928e3fb6cSRicardo Ribalda Delgado #define VENDOR_ID1 0x23
3028e3fb6cSRicardo Ribalda Delgado #define VENDOR_ID1_VAL 0x19
3128e3fb6cSRicardo Ribalda Delgado #define VENDOR_ID2 0x24
3228e3fb6cSRicardo Ribalda Delgado #define VENDOR_ID2_VAL 0x34
3329d58642SRicardo Ribalda Delgado #define IO_ADDR1 0x61
3429d58642SRicardo Ribalda Delgado #define IO_ADDR2 0x60
3528e3fb6cSRicardo Ribalda Delgado #define LDN 0x7
3628e3fb6cSRicardo Ribalda Delgado 
3787a713c8SArnd Bergmann #define FINTEK_IRQ_MODE	0x70
384da22f14SJi-Ze Hong (Peter Hong) #define IRQ_SHARE	BIT(4)
394da22f14SJi-Ze Hong (Peter Hong) #define IRQ_MODE_MASK	(BIT(6) | BIT(5))
404da22f14SJi-Ze Hong (Peter Hong) #define IRQ_LEVEL_LOW	0
414da22f14SJi-Ze Hong (Peter Hong) #define IRQ_EDGE_HIGH	BIT(5)
424da22f14SJi-Ze Hong (Peter Hong) 
4358178914SJi-Ze Hong (Peter Hong) /*
4458178914SJi-Ze Hong (Peter Hong)  * F81216H clock source register, the value and mask is the same with F81866,
4558178914SJi-Ze Hong (Peter Hong)  * but it's on F0h.
4658178914SJi-Ze Hong (Peter Hong)  *
4758178914SJi-Ze Hong (Peter Hong)  * Clock speeds for UART (register F0h)
4858178914SJi-Ze Hong (Peter Hong)  * 00: 1.8432MHz.
4958178914SJi-Ze Hong (Peter Hong)  * 01: 18.432MHz.
5058178914SJi-Ze Hong (Peter Hong)  * 10: 24MHz.
5158178914SJi-Ze Hong (Peter Hong)  * 11: 14.769MHz.
5258178914SJi-Ze Hong (Peter Hong)  */
5328e3fb6cSRicardo Ribalda Delgado #define RS485  0xF0
5428e3fb6cSRicardo Ribalda Delgado #define RTS_INVERT BIT(5)
5528e3fb6cSRicardo Ribalda Delgado #define RS485_URA BIT(4)
5628e3fb6cSRicardo Ribalda Delgado #define RXW4C_IRA BIT(3)
5728e3fb6cSRicardo Ribalda Delgado #define TXW4C_IRA BIT(2)
5828e3fb6cSRicardo Ribalda Delgado 
59c2236facSJi-Ze Hong (Peter Hong) #define FIFO_CTRL		0xF6
60c2236facSJi-Ze Hong (Peter Hong) #define FIFO_MODE_MASK		(BIT(1) | BIT(0))
61c2236facSJi-Ze Hong (Peter Hong) #define FIFO_MODE_128		(BIT(1) | BIT(0))
62c2236facSJi-Ze Hong (Peter Hong) #define RXFTHR_MODE_MASK	(BIT(5) | BIT(4))
63c2236facSJi-Ze Hong (Peter Hong) #define RXFTHR_MODE_4X		BIT(5)
64c2236facSJi-Ze Hong (Peter Hong) 
65da60d6afSJi-Ze Hong (Peter Hong) #define F81216_LDN_LOW	0x0
66da60d6afSJi-Ze Hong (Peter Hong) #define F81216_LDN_HIGH	0x4
67da60d6afSJi-Ze Hong (Peter Hong) 
68da60d6afSJi-Ze Hong (Peter Hong) /*
69da60d6afSJi-Ze Hong (Peter Hong)  * F81866 registers
70da60d6afSJi-Ze Hong (Peter Hong)  *
71da60d6afSJi-Ze Hong (Peter Hong)  * The IRQ setting mode of F81866 is not the same with F81216 series.
72da60d6afSJi-Ze Hong (Peter Hong)  *	Level/Low: IRQ_MODE0:0, IRQ_MODE1:0
73da60d6afSJi-Ze Hong (Peter Hong)  *	Edge/High: IRQ_MODE0:1, IRQ_MODE1:0
74fab8a02bSLukas Redlinger  *
75fab8a02bSLukas Redlinger  * Clock speeds for UART (register F2h)
76fab8a02bSLukas Redlinger  * 00: 1.8432MHz.
77fab8a02bSLukas Redlinger  * 01: 18.432MHz.
78fab8a02bSLukas Redlinger  * 10: 24MHz.
79fab8a02bSLukas Redlinger  * 11: 14.769MHz.
80da60d6afSJi-Ze Hong (Peter Hong)  */
81da60d6afSJi-Ze Hong (Peter Hong) #define F81866_IRQ_MODE		0xf0
82da60d6afSJi-Ze Hong (Peter Hong) #define F81866_IRQ_SHARE	BIT(0)
83da60d6afSJi-Ze Hong (Peter Hong) #define F81866_IRQ_MODE0	BIT(1)
84da60d6afSJi-Ze Hong (Peter Hong) 
85da60d6afSJi-Ze Hong (Peter Hong) #define F81866_FIFO_CTRL	FIFO_CTRL
86da60d6afSJi-Ze Hong (Peter Hong) #define F81866_IRQ_MODE1	BIT(3)
87da60d6afSJi-Ze Hong (Peter Hong) 
88da60d6afSJi-Ze Hong (Peter Hong) #define F81866_LDN_LOW		0x10
89da60d6afSJi-Ze Hong (Peter Hong) #define F81866_LDN_HIGH		0x16
90da60d6afSJi-Ze Hong (Peter Hong) 
91fab8a02bSLukas Redlinger #define F81866_UART_CLK 0xF2
92fab8a02bSLukas Redlinger #define F81866_UART_CLK_MASK (BIT(1) | BIT(0))
93fab8a02bSLukas Redlinger #define F81866_UART_CLK_1_8432MHZ 0
94fab8a02bSLukas Redlinger #define F81866_UART_CLK_14_769MHZ (BIT(1) | BIT(0))
95fab8a02bSLukas Redlinger #define F81866_UART_CLK_18_432MHZ BIT(0)
96fab8a02bSLukas Redlinger #define F81866_UART_CLK_24MHZ BIT(1)
97fab8a02bSLukas Redlinger 
9892a5f11aSRicardo Ribalda Delgado struct fintek_8250 {
99c2236facSJi-Ze Hong (Peter Hong) 	u16 pid;
100017bec38SRicardo Ribalda Delgado 	u16 base_port;
10192a5f11aSRicardo Ribalda Delgado 	u8 index;
102ce8c267eSRicardo Ribalda Delgado 	u8 key;
10392a5f11aSRicardo Ribalda Delgado };
10492a5f11aSRicardo Ribalda Delgado 
105f1232ac2SJi-Ze Hong (Peter Hong) static u8 sio_read_reg(struct fintek_8250 *pdata, u8 reg)
106f1232ac2SJi-Ze Hong (Peter Hong) {
107f1232ac2SJi-Ze Hong (Peter Hong) 	outb(reg, pdata->base_port + ADDR_PORT);
108f1232ac2SJi-Ze Hong (Peter Hong) 	return inb(pdata->base_port + DATA_PORT);
109f1232ac2SJi-Ze Hong (Peter Hong) }
110f1232ac2SJi-Ze Hong (Peter Hong) 
111f1232ac2SJi-Ze Hong (Peter Hong) static void sio_write_reg(struct fintek_8250 *pdata, u8 reg, u8 data)
112f1232ac2SJi-Ze Hong (Peter Hong) {
113f1232ac2SJi-Ze Hong (Peter Hong) 	outb(reg, pdata->base_port + ADDR_PORT);
114f1232ac2SJi-Ze Hong (Peter Hong) 	outb(data, pdata->base_port + DATA_PORT);
115f1232ac2SJi-Ze Hong (Peter Hong) }
116f1232ac2SJi-Ze Hong (Peter Hong) 
117f1232ac2SJi-Ze Hong (Peter Hong) static void sio_write_mask_reg(struct fintek_8250 *pdata, u8 reg, u8 mask,
118f1232ac2SJi-Ze Hong (Peter Hong) 			       u8 data)
119f1232ac2SJi-Ze Hong (Peter Hong) {
120f1232ac2SJi-Ze Hong (Peter Hong) 	u8 tmp;
121f1232ac2SJi-Ze Hong (Peter Hong) 
122f1232ac2SJi-Ze Hong (Peter Hong) 	tmp = (sio_read_reg(pdata, reg) & ~mask) | (mask & data);
123f1232ac2SJi-Ze Hong (Peter Hong) 	sio_write_reg(pdata, reg, tmp);
124f1232ac2SJi-Ze Hong (Peter Hong) }
125f1232ac2SJi-Ze Hong (Peter Hong) 
126ce8c267eSRicardo Ribalda Delgado static int fintek_8250_enter_key(u16 base_port, u8 key)
127017bec38SRicardo Ribalda Delgado {
128fa01e2caSRicardo Ribalda Delgado 	if (!request_muxed_region(base_port, 2, "8250_fintek"))
12928e3fb6cSRicardo Ribalda Delgado 		return -EBUSY;
13028e3fb6cSRicardo Ribalda Delgado 
131*fd97e66cSJi-Ze Hong (Peter Hong) 	/* Force to deactive all SuperIO in this base_port */
132*fd97e66cSJi-Ze Hong (Peter Hong) 	outb(EXIT_KEY, base_port + ADDR_PORT);
133*fd97e66cSJi-Ze Hong (Peter Hong) 
134ce8c267eSRicardo Ribalda Delgado 	outb(key, base_port + ADDR_PORT);
135ce8c267eSRicardo Ribalda Delgado 	outb(key, base_port + ADDR_PORT);
13628e3fb6cSRicardo Ribalda Delgado 	return 0;
13728e3fb6cSRicardo Ribalda Delgado }
13828e3fb6cSRicardo Ribalda Delgado 
139017bec38SRicardo Ribalda Delgado static void fintek_8250_exit_key(u16 base_port)
140017bec38SRicardo Ribalda Delgado {
14128e3fb6cSRicardo Ribalda Delgado 
142017bec38SRicardo Ribalda Delgado 	outb(EXIT_KEY, base_port + ADDR_PORT);
143017bec38SRicardo Ribalda Delgado 	release_region(base_port + ADDR_PORT, 2);
14428e3fb6cSRicardo Ribalda Delgado }
14528e3fb6cSRicardo Ribalda Delgado 
146f1232ac2SJi-Ze Hong (Peter Hong) static int fintek_8250_check_id(struct fintek_8250 *pdata)
14728e3fb6cSRicardo Ribalda Delgado {
148dae77f75SRicardo Ribalda Delgado 	u16 chip;
14928e3fb6cSRicardo Ribalda Delgado 
150f1232ac2SJi-Ze Hong (Peter Hong) 	if (sio_read_reg(pdata, VENDOR_ID1) != VENDOR_ID1_VAL)
15128e3fb6cSRicardo Ribalda Delgado 		return -ENODEV;
15228e3fb6cSRicardo Ribalda Delgado 
153f1232ac2SJi-Ze Hong (Peter Hong) 	if (sio_read_reg(pdata, VENDOR_ID2) != VENDOR_ID2_VAL)
15428e3fb6cSRicardo Ribalda Delgado 		return -ENODEV;
15528e3fb6cSRicardo Ribalda Delgado 
156f1232ac2SJi-Ze Hong (Peter Hong) 	chip = sio_read_reg(pdata, CHIP_ID1);
157f1232ac2SJi-Ze Hong (Peter Hong) 	chip |= sio_read_reg(pdata, CHIP_ID2) << 8;
158dae77f75SRicardo Ribalda Delgado 
1591e26c472SJi-Ze Hong (Peter Hong) 	switch (chip) {
160de48b099SJi-Ze Hong (Peter Hong) 	case CHIP_ID_F81865:
161da60d6afSJi-Ze Hong (Peter Hong) 	case CHIP_ID_F81866:
1621e26c472SJi-Ze Hong (Peter Hong) 	case CHIP_ID_F81216AD:
1631e26c472SJi-Ze Hong (Peter Hong) 	case CHIP_ID_F81216H:
1641e26c472SJi-Ze Hong (Peter Hong) 	case CHIP_ID_F81216:
1651e26c472SJi-Ze Hong (Peter Hong) 		break;
1661e26c472SJi-Ze Hong (Peter Hong) 	default:
167dae77f75SRicardo Ribalda Delgado 		return -ENODEV;
1681e26c472SJi-Ze Hong (Peter Hong) 	}
169dae77f75SRicardo Ribalda Delgado 
170c2236facSJi-Ze Hong (Peter Hong) 	pdata->pid = chip;
17128e3fb6cSRicardo Ribalda Delgado 	return 0;
17228e3fb6cSRicardo Ribalda Delgado }
17328e3fb6cSRicardo Ribalda Delgado 
174da60d6afSJi-Ze Hong (Peter Hong) static int fintek_8250_get_ldn_range(struct fintek_8250 *pdata, int *min,
175da60d6afSJi-Ze Hong (Peter Hong) 				     int *max)
176da60d6afSJi-Ze Hong (Peter Hong) {
177da60d6afSJi-Ze Hong (Peter Hong) 	switch (pdata->pid) {
178de48b099SJi-Ze Hong (Peter Hong) 	case CHIP_ID_F81865:
179da60d6afSJi-Ze Hong (Peter Hong) 	case CHIP_ID_F81866:
180da60d6afSJi-Ze Hong (Peter Hong) 		*min = F81866_LDN_LOW;
181da60d6afSJi-Ze Hong (Peter Hong) 		*max = F81866_LDN_HIGH;
182da60d6afSJi-Ze Hong (Peter Hong) 		return 0;
183da60d6afSJi-Ze Hong (Peter Hong) 
184da60d6afSJi-Ze Hong (Peter Hong) 	case CHIP_ID_F81216AD:
185da60d6afSJi-Ze Hong (Peter Hong) 	case CHIP_ID_F81216H:
186da60d6afSJi-Ze Hong (Peter Hong) 	case CHIP_ID_F81216:
187da60d6afSJi-Ze Hong (Peter Hong) 		*min = F81216_LDN_LOW;
188da60d6afSJi-Ze Hong (Peter Hong) 		*max = F81216_LDN_HIGH;
189da60d6afSJi-Ze Hong (Peter Hong) 		return 0;
190da60d6afSJi-Ze Hong (Peter Hong) 	}
191da60d6afSJi-Ze Hong (Peter Hong) 
192da60d6afSJi-Ze Hong (Peter Hong) 	return -ENODEV;
193da60d6afSJi-Ze Hong (Peter Hong) }
194da60d6afSJi-Ze Hong (Peter Hong) 
19541e69093SRicardo Ribalda Delgado static int fintek_8250_rs485_config(struct uart_port *port,
19628e3fb6cSRicardo Ribalda Delgado 			      struct serial_rs485 *rs485)
19728e3fb6cSRicardo Ribalda Delgado {
19828e3fb6cSRicardo Ribalda Delgado 	uint8_t config = 0;
19992a5f11aSRicardo Ribalda Delgado 	struct fintek_8250 *pdata = port->private_data;
20028e3fb6cSRicardo Ribalda Delgado 
20192a5f11aSRicardo Ribalda Delgado 	if (!pdata)
20228e3fb6cSRicardo Ribalda Delgado 		return -EINVAL;
20328e3fb6cSRicardo Ribalda Delgado 
20428e3fb6cSRicardo Ribalda Delgado 	if (rs485->flags & SER_RS485_ENABLED)
20528e3fb6cSRicardo Ribalda Delgado 		memset(rs485->padding, 0, sizeof(rs485->padding));
20628e3fb6cSRicardo Ribalda Delgado 	else
20728e3fb6cSRicardo Ribalda Delgado 		memset(rs485, 0, sizeof(*rs485));
20828e3fb6cSRicardo Ribalda Delgado 
20928e3fb6cSRicardo Ribalda Delgado 	rs485->flags &= SER_RS485_ENABLED | SER_RS485_RTS_ON_SEND |
21028e3fb6cSRicardo Ribalda Delgado 			SER_RS485_RTS_AFTER_SEND;
21128e3fb6cSRicardo 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 			(!!(rs485->flags & SER_RS485_RTS_AFTER_SEND)))
22428e3fb6cSRicardo Ribalda Delgado 		rs485->flags &= SER_RS485_ENABLED;
22528e3fb6cSRicardo Ribalda Delgado 	else
22628e3fb6cSRicardo Ribalda Delgado 		config |= RS485_URA;
22728e3fb6cSRicardo Ribalda Delgado 
22828e3fb6cSRicardo Ribalda Delgado 	if (rs485->flags & SER_RS485_RTS_ON_SEND)
22928e3fb6cSRicardo Ribalda Delgado 		config |= RTS_INVERT;
23028e3fb6cSRicardo Ribalda Delgado 
231ce8c267eSRicardo Ribalda Delgado 	if (fintek_8250_enter_key(pdata->base_port, pdata->key))
23228e3fb6cSRicardo Ribalda Delgado 		return -EBUSY;
23328e3fb6cSRicardo Ribalda Delgado 
234f1232ac2SJi-Ze Hong (Peter Hong) 	sio_write_reg(pdata, LDN, pdata->index);
235f1232ac2SJi-Ze Hong (Peter Hong) 	sio_write_reg(pdata, RS485, config);
236017bec38SRicardo Ribalda Delgado 	fintek_8250_exit_key(pdata->base_port);
23728e3fb6cSRicardo Ribalda Delgado 
23841e69093SRicardo Ribalda Delgado 	port->rs485 = *rs485;
23941e69093SRicardo Ribalda Delgado 
24028e3fb6cSRicardo Ribalda Delgado 	return 0;
24128e3fb6cSRicardo Ribalda Delgado }
24228e3fb6cSRicardo Ribalda Delgado 
24306e39572SJi-Ze Hong (Peter Hong) static void fintek_8250_set_irq_mode(struct fintek_8250 *pdata, bool is_level)
24406e39572SJi-Ze Hong (Peter Hong) {
24506e39572SJi-Ze Hong (Peter Hong) 	sio_write_reg(pdata, LDN, pdata->index);
246da60d6afSJi-Ze Hong (Peter Hong) 
247da60d6afSJi-Ze Hong (Peter Hong) 	switch (pdata->pid) {
248da60d6afSJi-Ze Hong (Peter Hong) 	case CHIP_ID_F81866:
249da60d6afSJi-Ze Hong (Peter Hong) 		sio_write_mask_reg(pdata, F81866_FIFO_CTRL, F81866_IRQ_MODE1,
250da60d6afSJi-Ze Hong (Peter Hong) 				   0);
251de48b099SJi-Ze Hong (Peter Hong) 		/* fall through */
252de48b099SJi-Ze Hong (Peter Hong) 	case CHIP_ID_F81865:
253da60d6afSJi-Ze Hong (Peter Hong) 		sio_write_mask_reg(pdata, F81866_IRQ_MODE, F81866_IRQ_SHARE,
254da60d6afSJi-Ze Hong (Peter Hong) 				   F81866_IRQ_SHARE);
255da60d6afSJi-Ze Hong (Peter Hong) 		sio_write_mask_reg(pdata, F81866_IRQ_MODE, F81866_IRQ_MODE0,
256da60d6afSJi-Ze Hong (Peter Hong) 				   is_level ? 0 : F81866_IRQ_MODE0);
257da60d6afSJi-Ze Hong (Peter Hong) 		break;
258da60d6afSJi-Ze Hong (Peter Hong) 
259da60d6afSJi-Ze Hong (Peter Hong) 	case CHIP_ID_F81216AD:
260da60d6afSJi-Ze Hong (Peter Hong) 	case CHIP_ID_F81216H:
261da60d6afSJi-Ze Hong (Peter Hong) 	case CHIP_ID_F81216:
262da60d6afSJi-Ze Hong (Peter Hong) 		sio_write_mask_reg(pdata, FINTEK_IRQ_MODE, IRQ_SHARE,
263da60d6afSJi-Ze Hong (Peter Hong) 				   IRQ_SHARE);
26406e39572SJi-Ze Hong (Peter Hong) 		sio_write_mask_reg(pdata, FINTEK_IRQ_MODE, IRQ_MODE_MASK,
26506e39572SJi-Ze Hong (Peter Hong) 				   is_level ? IRQ_LEVEL_LOW : IRQ_EDGE_HIGH);
266da60d6afSJi-Ze Hong (Peter Hong) 		break;
267da60d6afSJi-Ze Hong (Peter Hong) 	}
26806e39572SJi-Ze Hong (Peter Hong) }
26906e39572SJi-Ze Hong (Peter Hong) 
270c2236facSJi-Ze Hong (Peter Hong) static void fintek_8250_set_max_fifo(struct fintek_8250 *pdata)
271c2236facSJi-Ze Hong (Peter Hong) {
272c2236facSJi-Ze Hong (Peter Hong) 	switch (pdata->pid) {
273c2236facSJi-Ze Hong (Peter Hong) 	case CHIP_ID_F81216H: /* 128Bytes FIFO */
274da60d6afSJi-Ze Hong (Peter Hong) 	case CHIP_ID_F81866:
275c2236facSJi-Ze Hong (Peter Hong) 		sio_write_mask_reg(pdata, FIFO_CTRL,
276c2236facSJi-Ze Hong (Peter Hong) 				   FIFO_MODE_MASK | RXFTHR_MODE_MASK,
277c2236facSJi-Ze Hong (Peter Hong) 				   FIFO_MODE_128 | RXFTHR_MODE_4X);
278c2236facSJi-Ze Hong (Peter Hong) 		break;
279c2236facSJi-Ze Hong (Peter Hong) 
280c2236facSJi-Ze Hong (Peter Hong) 	default: /* Default 16Bytes FIFO */
281c2236facSJi-Ze Hong (Peter Hong) 		break;
282c2236facSJi-Ze Hong (Peter Hong) 	}
283c2236facSJi-Ze Hong (Peter Hong) }
284c2236facSJi-Ze Hong (Peter Hong) 
285fab8a02bSLukas Redlinger static void fintek_8250_goto_highspeed(struct uart_8250_port *uart,
286fab8a02bSLukas Redlinger 			      struct fintek_8250 *pdata)
287fab8a02bSLukas Redlinger {
288fab8a02bSLukas Redlinger 	sio_write_reg(pdata, LDN, pdata->index);
289fab8a02bSLukas Redlinger 
290fab8a02bSLukas Redlinger 	switch (pdata->pid) {
291fab8a02bSLukas Redlinger 	case CHIP_ID_F81866: /* set uart clock for high speed serial mode */
292fab8a02bSLukas Redlinger 		sio_write_mask_reg(pdata, F81866_UART_CLK,
293fab8a02bSLukas Redlinger 			F81866_UART_CLK_MASK,
294fab8a02bSLukas Redlinger 			F81866_UART_CLK_14_769MHZ);
295fab8a02bSLukas Redlinger 
296fab8a02bSLukas Redlinger 		uart->port.uartclk = 921600 * 16;
297fab8a02bSLukas Redlinger 		break;
298fab8a02bSLukas Redlinger 	default: /* leave clock speed untouched */
299fab8a02bSLukas Redlinger 		break;
300fab8a02bSLukas Redlinger 	}
301fab8a02bSLukas Redlinger }
302fab8a02bSLukas Redlinger 
303195638b6SJi-Ze Hong (Peter Hong) void fintek_8250_set_termios(struct uart_port *port, struct ktermios *termios,
304195638b6SJi-Ze Hong (Peter Hong) 			struct ktermios *old)
305195638b6SJi-Ze Hong (Peter Hong) {
306195638b6SJi-Ze Hong (Peter Hong) 	struct fintek_8250 *pdata = port->private_data;
307195638b6SJi-Ze Hong (Peter Hong) 	unsigned int baud = tty_termios_baud_rate(termios);
308195638b6SJi-Ze Hong (Peter Hong) 	int i;
30958178914SJi-Ze Hong (Peter Hong) 	u8 reg;
310195638b6SJi-Ze Hong (Peter Hong) 	static u32 baudrate_table[] = {115200, 921600, 1152000, 1500000};
311195638b6SJi-Ze Hong (Peter Hong) 	static u8 clock_table[] = { F81866_UART_CLK_1_8432MHZ,
312195638b6SJi-Ze Hong (Peter Hong) 			F81866_UART_CLK_14_769MHZ, F81866_UART_CLK_18_432MHZ,
313195638b6SJi-Ze Hong (Peter Hong) 			F81866_UART_CLK_24MHZ };
314195638b6SJi-Ze Hong (Peter Hong) 
31558178914SJi-Ze Hong (Peter Hong) 	switch (pdata->pid) {
31658178914SJi-Ze Hong (Peter Hong) 	case CHIP_ID_F81216H:
31758178914SJi-Ze Hong (Peter Hong) 		reg = RS485;
31858178914SJi-Ze Hong (Peter Hong) 		break;
31958178914SJi-Ze Hong (Peter Hong) 	case CHIP_ID_F81866:
32058178914SJi-Ze Hong (Peter Hong) 		reg = F81866_UART_CLK;
32158178914SJi-Ze Hong (Peter Hong) 		break;
32258178914SJi-Ze Hong (Peter Hong) 	default:
32358178914SJi-Ze Hong (Peter Hong) 		/* Don't change clocksource with unknown PID */
32458178914SJi-Ze Hong (Peter Hong) 		dev_warn(port->dev,
32558178914SJi-Ze Hong (Peter Hong) 			"%s: pid: %x Not support. use default set_termios.\n",
32658178914SJi-Ze Hong (Peter Hong) 			__func__, pdata->pid);
32758178914SJi-Ze Hong (Peter Hong) 		serial8250_do_set_termios(port, termios, old);
32858178914SJi-Ze Hong (Peter Hong) 		return;
32958178914SJi-Ze Hong (Peter Hong) 	}
33058178914SJi-Ze Hong (Peter Hong) 
331195638b6SJi-Ze Hong (Peter Hong) 	for (i = 0; i < ARRAY_SIZE(baudrate_table); ++i) {
332195638b6SJi-Ze Hong (Peter Hong) 		if (baud > baudrate_table[i] || baudrate_table[i] % baud != 0)
333195638b6SJi-Ze Hong (Peter Hong) 			continue;
334195638b6SJi-Ze Hong (Peter Hong) 
335195638b6SJi-Ze Hong (Peter Hong) 		if (port->uartclk == baudrate_table[i] * 16)
336195638b6SJi-Ze Hong (Peter Hong) 			break;
337195638b6SJi-Ze Hong (Peter Hong) 
338195638b6SJi-Ze Hong (Peter Hong) 		if (fintek_8250_enter_key(pdata->base_port, pdata->key))
339195638b6SJi-Ze Hong (Peter Hong) 			continue;
340195638b6SJi-Ze Hong (Peter Hong) 
341195638b6SJi-Ze Hong (Peter Hong) 		port->uartclk = baudrate_table[i] * 16;
342195638b6SJi-Ze Hong (Peter Hong) 
343195638b6SJi-Ze Hong (Peter Hong) 		sio_write_reg(pdata, LDN, pdata->index);
34458178914SJi-Ze Hong (Peter Hong) 		sio_write_mask_reg(pdata, reg, F81866_UART_CLK_MASK,
34558178914SJi-Ze Hong (Peter Hong) 				clock_table[i]);
346195638b6SJi-Ze Hong (Peter Hong) 
347195638b6SJi-Ze Hong (Peter Hong) 		fintek_8250_exit_key(pdata->base_port);
348195638b6SJi-Ze Hong (Peter Hong) 		break;
349195638b6SJi-Ze Hong (Peter Hong) 	}
350195638b6SJi-Ze Hong (Peter Hong) 
351195638b6SJi-Ze Hong (Peter Hong) 	if (i == ARRAY_SIZE(baudrate_table)) {
352195638b6SJi-Ze Hong (Peter Hong) 		baud = tty_termios_baud_rate(old);
353195638b6SJi-Ze Hong (Peter Hong) 		tty_termios_encode_baud_rate(termios, baud, baud);
354195638b6SJi-Ze Hong (Peter Hong) 	}
355195638b6SJi-Ze Hong (Peter Hong) 
356195638b6SJi-Ze Hong (Peter Hong) 	serial8250_do_set_termios(port, termios, old);
357195638b6SJi-Ze Hong (Peter Hong) }
358195638b6SJi-Ze Hong (Peter Hong) 
359195638b6SJi-Ze Hong (Peter Hong) static void fintek_8250_set_termios_handler(struct uart_8250_port *uart)
360195638b6SJi-Ze Hong (Peter Hong) {
361195638b6SJi-Ze Hong (Peter Hong) 	struct fintek_8250 *pdata = uart->port.private_data;
362195638b6SJi-Ze Hong (Peter Hong) 
363195638b6SJi-Ze Hong (Peter Hong) 	switch (pdata->pid) {
36458178914SJi-Ze Hong (Peter Hong) 	case CHIP_ID_F81216H:
365195638b6SJi-Ze Hong (Peter Hong) 	case CHIP_ID_F81866:
366195638b6SJi-Ze Hong (Peter Hong) 		uart->port.set_termios = fintek_8250_set_termios;
367195638b6SJi-Ze Hong (Peter Hong) 		break;
368195638b6SJi-Ze Hong (Peter Hong) 
369195638b6SJi-Ze Hong (Peter Hong) 	default:
370195638b6SJi-Ze Hong (Peter Hong) 		break;
371195638b6SJi-Ze Hong (Peter Hong) 	}
372195638b6SJi-Ze Hong (Peter Hong) }
373195638b6SJi-Ze Hong (Peter Hong) 
374fab8a02bSLukas Redlinger static int probe_setup_port(struct fintek_8250 *pdata,
375fab8a02bSLukas Redlinger 					struct uart_8250_port *uart)
376017bec38SRicardo Ribalda Delgado {
377017bec38SRicardo Ribalda Delgado 	static const u16 addr[] = {0x4e, 0x2e};
378ce8c267eSRicardo Ribalda Delgado 	static const u8 keys[] = {0x77, 0xa0, 0x87, 0x67};
37906e39572SJi-Ze Hong (Peter Hong) 	struct irq_data *irq_data;
38006e39572SJi-Ze Hong (Peter Hong) 	bool level_mode = false;
381da60d6afSJi-Ze Hong (Peter Hong) 	int i, j, k, min, max;
382017bec38SRicardo Ribalda Delgado 
383017bec38SRicardo Ribalda Delgado 	for (i = 0; i < ARRAY_SIZE(addr); i++) {
384ce8c267eSRicardo Ribalda Delgado 		for (j = 0; j < ARRAY_SIZE(keys); j++) {
385f1232ac2SJi-Ze Hong (Peter Hong) 			pdata->base_port = addr[i];
386f1232ac2SJi-Ze Hong (Peter Hong) 			pdata->key = keys[j];
387017bec38SRicardo Ribalda Delgado 
388ce8c267eSRicardo Ribalda Delgado 			if (fintek_8250_enter_key(addr[i], keys[j]))
389017bec38SRicardo Ribalda Delgado 				continue;
390da60d6afSJi-Ze Hong (Peter Hong) 			if (fintek_8250_check_id(pdata) ||
391da60d6afSJi-Ze Hong (Peter Hong) 			    fintek_8250_get_ldn_range(pdata, &min, &max)) {
392017bec38SRicardo Ribalda Delgado 				fintek_8250_exit_key(addr[i]);
39329d58642SRicardo Ribalda Delgado 				continue;
39429d58642SRicardo Ribalda Delgado 			}
39529d58642SRicardo Ribalda Delgado 
396da60d6afSJi-Ze Hong (Peter Hong) 			for (k = min; k < max; k++) {
39729d58642SRicardo Ribalda Delgado 				u16 aux;
39829d58642SRicardo Ribalda Delgado 
399f1232ac2SJi-Ze Hong (Peter Hong) 				sio_write_reg(pdata, LDN, k);
400f1232ac2SJi-Ze Hong (Peter Hong) 				aux = sio_read_reg(pdata, IO_ADDR1);
401f1232ac2SJi-Ze Hong (Peter Hong) 				aux |= sio_read_reg(pdata, IO_ADDR2) << 8;
402fab8a02bSLukas Redlinger 				if (aux != uart->port.iobase)
40329d58642SRicardo Ribalda Delgado 					continue;
40429d58642SRicardo Ribalda Delgado 
405fa01e2caSRicardo Ribalda Delgado 				pdata->index = k;
406fa01e2caSRicardo Ribalda Delgado 
407fab8a02bSLukas Redlinger 				irq_data = irq_get_irq_data(uart->port.irq);
40806e39572SJi-Ze Hong (Peter Hong) 				if (irq_data)
40906e39572SJi-Ze Hong (Peter Hong) 					level_mode =
41006e39572SJi-Ze Hong (Peter Hong) 						irqd_is_level_type(irq_data);
41106e39572SJi-Ze Hong (Peter Hong) 
41206e39572SJi-Ze Hong (Peter Hong) 				fintek_8250_set_irq_mode(pdata, level_mode);
413c2236facSJi-Ze Hong (Peter Hong) 				fintek_8250_set_max_fifo(pdata);
414fab8a02bSLukas Redlinger 				fintek_8250_goto_highspeed(uart, pdata);
415fab8a02bSLukas Redlinger 
41606e39572SJi-Ze Hong (Peter Hong) 				fintek_8250_exit_key(addr[i]);
41706e39572SJi-Ze Hong (Peter Hong) 
418fa01e2caSRicardo Ribalda Delgado 				return 0;
419017bec38SRicardo Ribalda Delgado 			}
420fa01e2caSRicardo Ribalda Delgado 
42129d58642SRicardo Ribalda Delgado 			fintek_8250_exit_key(addr[i]);
422ce8c267eSRicardo Ribalda Delgado 		}
423ce8c267eSRicardo Ribalda Delgado 	}
424017bec38SRicardo Ribalda Delgado 
425017bec38SRicardo Ribalda Delgado 	return -ENODEV;
426017bec38SRicardo Ribalda Delgado }
427017bec38SRicardo Ribalda Delgado 
4281e26c472SJi-Ze Hong (Peter Hong) static void fintek_8250_set_rs485_handler(struct uart_8250_port *uart)
4291e26c472SJi-Ze Hong (Peter Hong) {
4301e26c472SJi-Ze Hong (Peter Hong) 	struct fintek_8250 *pdata = uart->port.private_data;
4311e26c472SJi-Ze Hong (Peter Hong) 
4321e26c472SJi-Ze Hong (Peter Hong) 	switch (pdata->pid) {
4331e26c472SJi-Ze Hong (Peter Hong) 	case CHIP_ID_F81216AD:
4341e26c472SJi-Ze Hong (Peter Hong) 	case CHIP_ID_F81216H:
435da60d6afSJi-Ze Hong (Peter Hong) 	case CHIP_ID_F81866:
436de48b099SJi-Ze Hong (Peter Hong) 	case CHIP_ID_F81865:
4371e26c472SJi-Ze Hong (Peter Hong) 		uart->port.rs485_config = fintek_8250_rs485_config;
4381e26c472SJi-Ze Hong (Peter Hong) 		break;
4391e26c472SJi-Ze Hong (Peter Hong) 
4401e26c472SJi-Ze Hong (Peter Hong) 	default: /* No RS485 Auto direction functional */
4411e26c472SJi-Ze Hong (Peter Hong) 		break;
4421e26c472SJi-Ze Hong (Peter Hong) 	}
4431e26c472SJi-Ze Hong (Peter Hong) }
4441e26c472SJi-Ze Hong (Peter Hong) 
445fa01e2caSRicardo Ribalda Delgado int fintek_8250_probe(struct uart_8250_port *uart)
44628e3fb6cSRicardo Ribalda Delgado {
44792a5f11aSRicardo Ribalda Delgado 	struct fintek_8250 *pdata;
448fa01e2caSRicardo Ribalda Delgado 	struct fintek_8250 probe_data;
44928e3fb6cSRicardo Ribalda Delgado 
450fab8a02bSLukas Redlinger 	if (probe_setup_port(&probe_data, uart))
45128e3fb6cSRicardo Ribalda Delgado 		return -ENODEV;
45228e3fb6cSRicardo Ribalda Delgado 
453fa01e2caSRicardo Ribalda Delgado 	pdata = devm_kzalloc(uart->port.dev, sizeof(*pdata), GFP_KERNEL);
45492a5f11aSRicardo Ribalda Delgado 	if (!pdata)
45592a5f11aSRicardo Ribalda Delgado 		return -ENOMEM;
45692a5f11aSRicardo Ribalda Delgado 
457fa01e2caSRicardo Ribalda Delgado 	memcpy(pdata, &probe_data, sizeof(probe_data));
458fa01e2caSRicardo Ribalda Delgado 	uart->port.private_data = pdata;
4591e26c472SJi-Ze Hong (Peter Hong) 	fintek_8250_set_rs485_handler(uart);
460195638b6SJi-Ze Hong (Peter Hong) 	fintek_8250_set_termios_handler(uart);
46128e3fb6cSRicardo Ribalda Delgado 
46206e39572SJi-Ze Hong (Peter Hong) 	return 0;
46328e3fb6cSRicardo Ribalda Delgado }
464