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
24*63ff22d7SFilip Brozovic #define CHIP_ID_F81216E 0x1617
25c2236facSJi-Ze Hong (Peter Hong) #define CHIP_ID_F81216H 0x0501
261e26c472SJi-Ze Hong (Peter Hong) #define CHIP_ID_F81216 0x0802
2728e3fb6cSRicardo Ribalda Delgado #define VENDOR_ID1 0x23
2828e3fb6cSRicardo Ribalda Delgado #define VENDOR_ID1_VAL 0x19
2928e3fb6cSRicardo Ribalda Delgado #define VENDOR_ID2 0x24
3028e3fb6cSRicardo Ribalda Delgado #define VENDOR_ID2_VAL 0x34
3129d58642SRicardo Ribalda Delgado #define IO_ADDR1 0x61
3229d58642SRicardo Ribalda Delgado #define IO_ADDR2 0x60
3328e3fb6cSRicardo Ribalda Delgado #define LDN 0x7
3428e3fb6cSRicardo Ribalda Delgado
3587a713c8SArnd Bergmann #define FINTEK_IRQ_MODE 0x70
364da22f14SJi-Ze Hong (Peter Hong) #define IRQ_SHARE BIT(4)
374da22f14SJi-Ze Hong (Peter Hong) #define IRQ_MODE_MASK (BIT(6) | BIT(5))
384da22f14SJi-Ze Hong (Peter Hong) #define IRQ_LEVEL_LOW 0
394da22f14SJi-Ze Hong (Peter Hong) #define IRQ_EDGE_HIGH BIT(5)
404da22f14SJi-Ze Hong (Peter Hong)
4158178914SJi-Ze Hong (Peter Hong) /*
4258178914SJi-Ze Hong (Peter Hong) * F81216H clock source register, the value and mask is the same with F81866,
4358178914SJi-Ze Hong (Peter Hong) * but it's on F0h.
4458178914SJi-Ze Hong (Peter Hong) *
4558178914SJi-Ze Hong (Peter Hong) * Clock speeds for UART (register F0h)
4658178914SJi-Ze Hong (Peter Hong) * 00: 1.8432MHz.
4758178914SJi-Ze Hong (Peter Hong) * 01: 18.432MHz.
4858178914SJi-Ze Hong (Peter Hong) * 10: 24MHz.
4958178914SJi-Ze Hong (Peter Hong) * 11: 14.769MHz.
5058178914SJi-Ze Hong (Peter Hong) */
5128e3fb6cSRicardo Ribalda Delgado #define RS485 0xF0
5228e3fb6cSRicardo Ribalda Delgado #define RTS_INVERT BIT(5)
5328e3fb6cSRicardo Ribalda Delgado #define RS485_URA BIT(4)
5428e3fb6cSRicardo Ribalda Delgado #define RXW4C_IRA BIT(3)
5528e3fb6cSRicardo Ribalda Delgado #define TXW4C_IRA BIT(2)
5628e3fb6cSRicardo Ribalda Delgado
57c2236facSJi-Ze Hong (Peter Hong) #define FIFO_CTRL 0xF6
58c2236facSJi-Ze Hong (Peter Hong) #define FIFO_MODE_MASK (BIT(1) | BIT(0))
59c2236facSJi-Ze Hong (Peter Hong) #define FIFO_MODE_128 (BIT(1) | BIT(0))
60c2236facSJi-Ze Hong (Peter Hong) #define RXFTHR_MODE_MASK (BIT(5) | BIT(4))
61c2236facSJi-Ze Hong (Peter Hong) #define RXFTHR_MODE_4X BIT(5)
62c2236facSJi-Ze Hong (Peter Hong)
63da60d6afSJi-Ze Hong (Peter Hong) #define F81216_LDN_LOW 0x0
64da60d6afSJi-Ze Hong (Peter Hong) #define F81216_LDN_HIGH 0x4
65da60d6afSJi-Ze Hong (Peter Hong)
66da60d6afSJi-Ze Hong (Peter Hong) /*
67423d9118SJi-Ze Hong (Peter Hong) * F81866/966 registers
68da60d6afSJi-Ze Hong (Peter Hong) *
69423d9118SJi-Ze Hong (Peter Hong) * The IRQ setting mode of F81866/966 is not the same with F81216 series.
70da60d6afSJi-Ze Hong (Peter Hong) * Level/Low: IRQ_MODE0:0, IRQ_MODE1:0
71da60d6afSJi-Ze Hong (Peter Hong) * Edge/High: IRQ_MODE0:1, IRQ_MODE1:0
72fab8a02bSLukas Redlinger *
73fab8a02bSLukas Redlinger * Clock speeds for UART (register F2h)
74fab8a02bSLukas Redlinger * 00: 1.8432MHz.
75fab8a02bSLukas Redlinger * 01: 18.432MHz.
76fab8a02bSLukas Redlinger * 10: 24MHz.
77fab8a02bSLukas Redlinger * 11: 14.769MHz.
78da60d6afSJi-Ze Hong (Peter Hong) */
79da60d6afSJi-Ze Hong (Peter Hong) #define F81866_IRQ_MODE 0xf0
80da60d6afSJi-Ze Hong (Peter Hong) #define F81866_IRQ_SHARE BIT(0)
81da60d6afSJi-Ze Hong (Peter Hong) #define F81866_IRQ_MODE0 BIT(1)
82da60d6afSJi-Ze Hong (Peter Hong)
83da60d6afSJi-Ze Hong (Peter Hong) #define F81866_FIFO_CTRL FIFO_CTRL
84da60d6afSJi-Ze Hong (Peter Hong) #define F81866_IRQ_MODE1 BIT(3)
85da60d6afSJi-Ze Hong (Peter Hong)
86da60d6afSJi-Ze Hong (Peter Hong) #define F81866_LDN_LOW 0x10
87da60d6afSJi-Ze Hong (Peter Hong) #define F81866_LDN_HIGH 0x16
88da60d6afSJi-Ze Hong (Peter Hong)
89fab8a02bSLukas Redlinger #define F81866_UART_CLK 0xF2
90fab8a02bSLukas Redlinger #define F81866_UART_CLK_MASK (BIT(1) | BIT(0))
91fab8a02bSLukas Redlinger #define F81866_UART_CLK_1_8432MHZ 0
92fab8a02bSLukas Redlinger #define F81866_UART_CLK_14_769MHZ (BIT(1) | BIT(0))
93fab8a02bSLukas Redlinger #define F81866_UART_CLK_18_432MHZ BIT(0)
94fab8a02bSLukas Redlinger #define F81866_UART_CLK_24MHZ BIT(1)
95fab8a02bSLukas Redlinger
9692a5f11aSRicardo Ribalda Delgado struct fintek_8250 {
97c2236facSJi-Ze Hong (Peter Hong) u16 pid;
98017bec38SRicardo Ribalda Delgado u16 base_port;
9992a5f11aSRicardo Ribalda Delgado u8 index;
100ce8c267eSRicardo Ribalda Delgado u8 key;
10192a5f11aSRicardo Ribalda Delgado };
10292a5f11aSRicardo Ribalda Delgado
sio_read_reg(struct fintek_8250 * pdata,u8 reg)103f1232ac2SJi-Ze Hong (Peter Hong) static u8 sio_read_reg(struct fintek_8250 *pdata, u8 reg)
104f1232ac2SJi-Ze Hong (Peter Hong) {
105f1232ac2SJi-Ze Hong (Peter Hong) outb(reg, pdata->base_port + ADDR_PORT);
106f1232ac2SJi-Ze Hong (Peter Hong) return inb(pdata->base_port + DATA_PORT);
107f1232ac2SJi-Ze Hong (Peter Hong) }
108f1232ac2SJi-Ze Hong (Peter Hong)
sio_write_reg(struct fintek_8250 * pdata,u8 reg,u8 data)109f1232ac2SJi-Ze Hong (Peter Hong) static void sio_write_reg(struct fintek_8250 *pdata, u8 reg, u8 data)
110f1232ac2SJi-Ze Hong (Peter Hong) {
111f1232ac2SJi-Ze Hong (Peter Hong) outb(reg, pdata->base_port + ADDR_PORT);
112f1232ac2SJi-Ze Hong (Peter Hong) outb(data, pdata->base_port + DATA_PORT);
113f1232ac2SJi-Ze Hong (Peter Hong) }
114f1232ac2SJi-Ze Hong (Peter Hong)
sio_write_mask_reg(struct fintek_8250 * pdata,u8 reg,u8 mask,u8 data)115f1232ac2SJi-Ze Hong (Peter Hong) static void sio_write_mask_reg(struct fintek_8250 *pdata, u8 reg, u8 mask,
116f1232ac2SJi-Ze Hong (Peter Hong) u8 data)
117f1232ac2SJi-Ze Hong (Peter Hong) {
118f1232ac2SJi-Ze Hong (Peter Hong) u8 tmp;
119f1232ac2SJi-Ze Hong (Peter Hong)
120f1232ac2SJi-Ze Hong (Peter Hong) tmp = (sio_read_reg(pdata, reg) & ~mask) | (mask & data);
121f1232ac2SJi-Ze Hong (Peter Hong) sio_write_reg(pdata, reg, tmp);
122f1232ac2SJi-Ze Hong (Peter Hong) }
123f1232ac2SJi-Ze Hong (Peter Hong)
fintek_8250_enter_key(u16 base_port,u8 key)124ce8c267eSRicardo Ribalda Delgado static int fintek_8250_enter_key(u16 base_port, u8 key)
125017bec38SRicardo Ribalda Delgado {
126fa01e2caSRicardo Ribalda Delgado if (!request_muxed_region(base_port, 2, "8250_fintek"))
12728e3fb6cSRicardo Ribalda Delgado return -EBUSY;
12828e3fb6cSRicardo Ribalda Delgado
129fd97e66cSJi-Ze Hong (Peter Hong) /* Force to deactive all SuperIO in this base_port */
130fd97e66cSJi-Ze Hong (Peter Hong) outb(EXIT_KEY, base_port + ADDR_PORT);
131fd97e66cSJi-Ze Hong (Peter Hong)
132ce8c267eSRicardo Ribalda Delgado outb(key, base_port + ADDR_PORT);
133ce8c267eSRicardo Ribalda Delgado outb(key, base_port + ADDR_PORT);
13428e3fb6cSRicardo Ribalda Delgado return 0;
13528e3fb6cSRicardo Ribalda Delgado }
13628e3fb6cSRicardo Ribalda Delgado
fintek_8250_exit_key(u16 base_port)137017bec38SRicardo Ribalda Delgado static void fintek_8250_exit_key(u16 base_port)
138017bec38SRicardo Ribalda Delgado {
13928e3fb6cSRicardo Ribalda Delgado
140017bec38SRicardo Ribalda Delgado outb(EXIT_KEY, base_port + ADDR_PORT);
141017bec38SRicardo Ribalda Delgado release_region(base_port + ADDR_PORT, 2);
14228e3fb6cSRicardo Ribalda Delgado }
14328e3fb6cSRicardo Ribalda Delgado
fintek_8250_check_id(struct fintek_8250 * pdata)144f1232ac2SJi-Ze Hong (Peter Hong) static int fintek_8250_check_id(struct fintek_8250 *pdata)
14528e3fb6cSRicardo Ribalda Delgado {
146dae77f75SRicardo Ribalda Delgado u16 chip;
14728e3fb6cSRicardo Ribalda Delgado
148f1232ac2SJi-Ze Hong (Peter Hong) if (sio_read_reg(pdata, VENDOR_ID1) != VENDOR_ID1_VAL)
14928e3fb6cSRicardo Ribalda Delgado return -ENODEV;
15028e3fb6cSRicardo Ribalda Delgado
151f1232ac2SJi-Ze Hong (Peter Hong) if (sio_read_reg(pdata, VENDOR_ID2) != VENDOR_ID2_VAL)
15228e3fb6cSRicardo Ribalda Delgado return -ENODEV;
15328e3fb6cSRicardo Ribalda Delgado
154f1232ac2SJi-Ze Hong (Peter Hong) chip = sio_read_reg(pdata, CHIP_ID1);
155f1232ac2SJi-Ze Hong (Peter Hong) chip |= sio_read_reg(pdata, CHIP_ID2) << 8;
156dae77f75SRicardo Ribalda Delgado
1571e26c472SJi-Ze Hong (Peter Hong) switch (chip) {
158de48b099SJi-Ze Hong (Peter Hong) case CHIP_ID_F81865:
159da60d6afSJi-Ze Hong (Peter Hong) case CHIP_ID_F81866:
160423d9118SJi-Ze Hong (Peter Hong) case CHIP_ID_F81966:
1611e26c472SJi-Ze Hong (Peter Hong) case CHIP_ID_F81216AD:
162*63ff22d7SFilip Brozovic case CHIP_ID_F81216E:
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
fintek_8250_get_ldn_range(struct fintek_8250 * pdata,int * min,int * max)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) {
178423d9118SJi-Ze Hong (Peter Hong) case CHIP_ID_F81966:
179de48b099SJi-Ze Hong (Peter Hong) case CHIP_ID_F81865:
180da60d6afSJi-Ze Hong (Peter Hong) case CHIP_ID_F81866:
181da60d6afSJi-Ze Hong (Peter Hong) *min = F81866_LDN_LOW;
182da60d6afSJi-Ze Hong (Peter Hong) *max = F81866_LDN_HIGH;
183da60d6afSJi-Ze Hong (Peter Hong) return 0;
184da60d6afSJi-Ze Hong (Peter Hong)
185da60d6afSJi-Ze Hong (Peter Hong) case CHIP_ID_F81216AD:
186*63ff22d7SFilip Brozovic case CHIP_ID_F81216E:
187da60d6afSJi-Ze Hong (Peter Hong) case CHIP_ID_F81216H:
188da60d6afSJi-Ze Hong (Peter Hong) case CHIP_ID_F81216:
189da60d6afSJi-Ze Hong (Peter Hong) *min = F81216_LDN_LOW;
190da60d6afSJi-Ze Hong (Peter Hong) *max = F81216_LDN_HIGH;
191da60d6afSJi-Ze Hong (Peter Hong) return 0;
192da60d6afSJi-Ze Hong (Peter Hong) }
193da60d6afSJi-Ze Hong (Peter Hong)
194da60d6afSJi-Ze Hong (Peter Hong) return -ENODEV;
195da60d6afSJi-Ze Hong (Peter Hong) }
196da60d6afSJi-Ze Hong (Peter Hong)
fintek_8250_rs485_config(struct uart_port * port,struct ktermios * termios,struct serial_rs485 * rs485)197ae50bb27SIlpo Järvinen static int fintek_8250_rs485_config(struct uart_port *port, struct ktermios *termios,
19828e3fb6cSRicardo Ribalda Delgado struct serial_rs485 *rs485)
19928e3fb6cSRicardo Ribalda Delgado {
20028e3fb6cSRicardo Ribalda Delgado uint8_t config = 0;
20192a5f11aSRicardo Ribalda Delgado struct fintek_8250 *pdata = port->private_data;
20228e3fb6cSRicardo Ribalda Delgado
20392a5f11aSRicardo Ribalda Delgado if (!pdata)
20428e3fb6cSRicardo Ribalda Delgado return -EINVAL;
20528e3fb6cSRicardo Ribalda Delgado
206af017927SIlpo Järvinen
207af017927SIlpo Järvinen if (rs485->flags & SER_RS485_ENABLED) {
2087ecc7701SRicardo Ribalda Delgado /* Hardware do not support same RTS level on send and receive */
2097ecc7701SRicardo Ribalda Delgado if (!(rs485->flags & SER_RS485_RTS_ON_SEND) ==
2107ecc7701SRicardo Ribalda Delgado !(rs485->flags & SER_RS485_RTS_AFTER_SEND))
2117ecc7701SRicardo Ribalda Delgado return -EINVAL;
2127ecc7701SRicardo Ribalda Delgado config |= RS485_URA;
21321c4e7f2SRicardo Ribalda Delgado }
21421c4e7f2SRicardo 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
23528e3fb6cSRicardo Ribalda Delgado return 0;
23628e3fb6cSRicardo Ribalda Delgado }
23728e3fb6cSRicardo Ribalda Delgado
fintek_8250_set_irq_mode(struct fintek_8250 * pdata,bool is_level)23806e39572SJi-Ze Hong (Peter Hong) static void fintek_8250_set_irq_mode(struct fintek_8250 *pdata, bool is_level)
23906e39572SJi-Ze Hong (Peter Hong) {
24006e39572SJi-Ze Hong (Peter Hong) sio_write_reg(pdata, LDN, pdata->index);
241da60d6afSJi-Ze Hong (Peter Hong)
242da60d6afSJi-Ze Hong (Peter Hong) switch (pdata->pid) {
243423d9118SJi-Ze Hong (Peter Hong) case CHIP_ID_F81966:
244da60d6afSJi-Ze Hong (Peter Hong) case CHIP_ID_F81866:
245da60d6afSJi-Ze Hong (Peter Hong) sio_write_mask_reg(pdata, F81866_FIFO_CTRL, F81866_IRQ_MODE1,
246da60d6afSJi-Ze Hong (Peter Hong) 0);
247df561f66SGustavo A. R. Silva fallthrough;
248de48b099SJi-Ze Hong (Peter Hong) case CHIP_ID_F81865:
249da60d6afSJi-Ze Hong (Peter Hong) sio_write_mask_reg(pdata, F81866_IRQ_MODE, F81866_IRQ_SHARE,
250da60d6afSJi-Ze Hong (Peter Hong) F81866_IRQ_SHARE);
251da60d6afSJi-Ze Hong (Peter Hong) sio_write_mask_reg(pdata, F81866_IRQ_MODE, F81866_IRQ_MODE0,
252da60d6afSJi-Ze Hong (Peter Hong) is_level ? 0 : F81866_IRQ_MODE0);
253da60d6afSJi-Ze Hong (Peter Hong) break;
254da60d6afSJi-Ze Hong (Peter Hong)
255da60d6afSJi-Ze Hong (Peter Hong) case CHIP_ID_F81216AD:
256*63ff22d7SFilip Brozovic case CHIP_ID_F81216E:
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)
fintek_8250_set_max_fifo(struct fintek_8250 * pdata)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) {
270*63ff22d7SFilip Brozovic case CHIP_ID_F81216E: /* 128Bytes FIFO */
271*63ff22d7SFilip Brozovic case CHIP_ID_F81216H:
272423d9118SJi-Ze Hong (Peter Hong) case CHIP_ID_F81966:
273da60d6afSJi-Ze Hong (Peter Hong) case CHIP_ID_F81866:
274c2236facSJi-Ze Hong (Peter Hong) sio_write_mask_reg(pdata, FIFO_CTRL,
275c2236facSJi-Ze Hong (Peter Hong) FIFO_MODE_MASK | RXFTHR_MODE_MASK,
276c2236facSJi-Ze Hong (Peter Hong) FIFO_MODE_128 | RXFTHR_MODE_4X);
277c2236facSJi-Ze Hong (Peter Hong) break;
278c2236facSJi-Ze Hong (Peter Hong)
279c2236facSJi-Ze Hong (Peter Hong) default: /* Default 16Bytes FIFO */
280c2236facSJi-Ze Hong (Peter Hong) break;
281c2236facSJi-Ze Hong (Peter Hong) }
282c2236facSJi-Ze Hong (Peter Hong) }
283c2236facSJi-Ze Hong (Peter Hong)
fintek_8250_set_termios(struct uart_port * port,struct ktermios * termios,const struct ktermios * old)2849828def3SYueHaibing static void fintek_8250_set_termios(struct uart_port *port,
2859828def3SYueHaibing struct ktermios *termios,
286bec5b814SIlpo Järvinen const struct ktermios *old)
287195638b6SJi-Ze Hong (Peter Hong) {
288195638b6SJi-Ze Hong (Peter Hong) struct fintek_8250 *pdata = port->private_data;
289195638b6SJi-Ze Hong (Peter Hong) unsigned int baud = tty_termios_baud_rate(termios);
290195638b6SJi-Ze Hong (Peter Hong) int i;
29158178914SJi-Ze Hong (Peter Hong) u8 reg;
292195638b6SJi-Ze Hong (Peter Hong) static u32 baudrate_table[] = {115200, 921600, 1152000, 1500000};
293195638b6SJi-Ze Hong (Peter Hong) static u8 clock_table[] = { F81866_UART_CLK_1_8432MHZ,
294195638b6SJi-Ze Hong (Peter Hong) F81866_UART_CLK_14_769MHZ, F81866_UART_CLK_18_432MHZ,
295195638b6SJi-Ze Hong (Peter Hong) F81866_UART_CLK_24MHZ };
296195638b6SJi-Ze Hong (Peter Hong)
29707a708f0SJi-Ze Hong (Peter Hong) /*
29807a708f0SJi-Ze Hong (Peter Hong) * We'll use serial8250_do_set_termios() for baud = 0, otherwise It'll
29907a708f0SJi-Ze Hong (Peter Hong) * crash on baudrate_table[i] % baud with "division by zero".
30007a708f0SJi-Ze Hong (Peter Hong) */
30107a708f0SJi-Ze Hong (Peter Hong) if (!baud)
30207a708f0SJi-Ze Hong (Peter Hong) goto exit;
30307a708f0SJi-Ze Hong (Peter Hong)
30458178914SJi-Ze Hong (Peter Hong) switch (pdata->pid) {
305*63ff22d7SFilip Brozovic case CHIP_ID_F81216E:
30658178914SJi-Ze Hong (Peter Hong) case CHIP_ID_F81216H:
30758178914SJi-Ze Hong (Peter Hong) reg = RS485;
30858178914SJi-Ze Hong (Peter Hong) break;
309423d9118SJi-Ze Hong (Peter Hong) case CHIP_ID_F81966:
31058178914SJi-Ze Hong (Peter Hong) case CHIP_ID_F81866:
31158178914SJi-Ze Hong (Peter Hong) reg = F81866_UART_CLK;
31258178914SJi-Ze Hong (Peter Hong) break;
31358178914SJi-Ze Hong (Peter Hong) default:
31458178914SJi-Ze Hong (Peter Hong) /* Don't change clocksource with unknown PID */
31558178914SJi-Ze Hong (Peter Hong) dev_warn(port->dev,
31658178914SJi-Ze Hong (Peter Hong) "%s: pid: %x Not support. use default set_termios.\n",
31758178914SJi-Ze Hong (Peter Hong) __func__, pdata->pid);
31807a708f0SJi-Ze Hong (Peter Hong) goto exit;
31958178914SJi-Ze Hong (Peter Hong) }
32058178914SJi-Ze Hong (Peter Hong)
321195638b6SJi-Ze Hong (Peter Hong) for (i = 0; i < ARRAY_SIZE(baudrate_table); ++i) {
322195638b6SJi-Ze Hong (Peter Hong) if (baud > baudrate_table[i] || baudrate_table[i] % baud != 0)
323195638b6SJi-Ze Hong (Peter Hong) continue;
324195638b6SJi-Ze Hong (Peter Hong)
325195638b6SJi-Ze Hong (Peter Hong) if (port->uartclk == baudrate_table[i] * 16)
326195638b6SJi-Ze Hong (Peter Hong) break;
327195638b6SJi-Ze Hong (Peter Hong)
328195638b6SJi-Ze Hong (Peter Hong) if (fintek_8250_enter_key(pdata->base_port, pdata->key))
329195638b6SJi-Ze Hong (Peter Hong) continue;
330195638b6SJi-Ze Hong (Peter Hong)
331195638b6SJi-Ze Hong (Peter Hong) port->uartclk = baudrate_table[i] * 16;
332195638b6SJi-Ze Hong (Peter Hong)
333195638b6SJi-Ze Hong (Peter Hong) sio_write_reg(pdata, LDN, pdata->index);
33458178914SJi-Ze Hong (Peter Hong) sio_write_mask_reg(pdata, reg, F81866_UART_CLK_MASK,
33558178914SJi-Ze Hong (Peter Hong) clock_table[i]);
336195638b6SJi-Ze Hong (Peter Hong)
337195638b6SJi-Ze Hong (Peter Hong) fintek_8250_exit_key(pdata->base_port);
338195638b6SJi-Ze Hong (Peter Hong) break;
339195638b6SJi-Ze Hong (Peter Hong) }
340195638b6SJi-Ze Hong (Peter Hong)
341195638b6SJi-Ze Hong (Peter Hong) if (i == ARRAY_SIZE(baudrate_table)) {
342195638b6SJi-Ze Hong (Peter Hong) baud = tty_termios_baud_rate(old);
343195638b6SJi-Ze Hong (Peter Hong) tty_termios_encode_baud_rate(termios, baud, baud);
344195638b6SJi-Ze Hong (Peter Hong) }
345195638b6SJi-Ze Hong (Peter Hong)
34607a708f0SJi-Ze Hong (Peter Hong) exit:
347195638b6SJi-Ze Hong (Peter Hong) serial8250_do_set_termios(port, termios, old);
348195638b6SJi-Ze Hong (Peter Hong) }
349195638b6SJi-Ze Hong (Peter Hong)
fintek_8250_set_termios_handler(struct uart_8250_port * uart)350195638b6SJi-Ze Hong (Peter Hong) static void fintek_8250_set_termios_handler(struct uart_8250_port *uart)
351195638b6SJi-Ze Hong (Peter Hong) {
352195638b6SJi-Ze Hong (Peter Hong) struct fintek_8250 *pdata = uart->port.private_data;
353195638b6SJi-Ze Hong (Peter Hong)
354195638b6SJi-Ze Hong (Peter Hong) switch (pdata->pid) {
355*63ff22d7SFilip Brozovic case CHIP_ID_F81216E:
35658178914SJi-Ze Hong (Peter Hong) case CHIP_ID_F81216H:
357423d9118SJi-Ze Hong (Peter Hong) case CHIP_ID_F81966:
358195638b6SJi-Ze Hong (Peter Hong) case CHIP_ID_F81866:
359195638b6SJi-Ze Hong (Peter Hong) uart->port.set_termios = fintek_8250_set_termios;
360195638b6SJi-Ze Hong (Peter Hong) break;
361195638b6SJi-Ze Hong (Peter Hong)
362195638b6SJi-Ze Hong (Peter Hong) default:
363195638b6SJi-Ze Hong (Peter Hong) break;
364195638b6SJi-Ze Hong (Peter Hong) }
365195638b6SJi-Ze Hong (Peter Hong) }
366195638b6SJi-Ze Hong (Peter Hong)
probe_setup_port(struct fintek_8250 * pdata,struct uart_8250_port * uart)367fab8a02bSLukas Redlinger static int probe_setup_port(struct fintek_8250 *pdata,
368fab8a02bSLukas Redlinger struct uart_8250_port *uart)
369017bec38SRicardo Ribalda Delgado {
370017bec38SRicardo Ribalda Delgado static const u16 addr[] = {0x4e, 0x2e};
371ce8c267eSRicardo Ribalda Delgado static const u8 keys[] = {0x77, 0xa0, 0x87, 0x67};
37206e39572SJi-Ze Hong (Peter Hong) struct irq_data *irq_data;
37306e39572SJi-Ze Hong (Peter Hong) bool level_mode = false;
374da60d6afSJi-Ze Hong (Peter Hong) int i, j, k, min, max;
375017bec38SRicardo Ribalda Delgado
376017bec38SRicardo Ribalda Delgado for (i = 0; i < ARRAY_SIZE(addr); i++) {
377ce8c267eSRicardo Ribalda Delgado for (j = 0; j < ARRAY_SIZE(keys); j++) {
378f1232ac2SJi-Ze Hong (Peter Hong) pdata->base_port = addr[i];
379f1232ac2SJi-Ze Hong (Peter Hong) pdata->key = keys[j];
380017bec38SRicardo Ribalda Delgado
381ce8c267eSRicardo Ribalda Delgado if (fintek_8250_enter_key(addr[i], keys[j]))
382017bec38SRicardo Ribalda Delgado continue;
383da60d6afSJi-Ze Hong (Peter Hong) if (fintek_8250_check_id(pdata) ||
384da60d6afSJi-Ze Hong (Peter Hong) fintek_8250_get_ldn_range(pdata, &min, &max)) {
385017bec38SRicardo Ribalda Delgado fintek_8250_exit_key(addr[i]);
38629d58642SRicardo Ribalda Delgado continue;
38729d58642SRicardo Ribalda Delgado }
38829d58642SRicardo Ribalda Delgado
389da60d6afSJi-Ze Hong (Peter Hong) for (k = min; k < max; k++) {
39029d58642SRicardo Ribalda Delgado u16 aux;
39129d58642SRicardo Ribalda Delgado
392f1232ac2SJi-Ze Hong (Peter Hong) sio_write_reg(pdata, LDN, k);
393f1232ac2SJi-Ze Hong (Peter Hong) aux = sio_read_reg(pdata, IO_ADDR1);
394f1232ac2SJi-Ze Hong (Peter Hong) aux |= sio_read_reg(pdata, IO_ADDR2) << 8;
395fab8a02bSLukas Redlinger if (aux != uart->port.iobase)
39629d58642SRicardo Ribalda Delgado continue;
39729d58642SRicardo Ribalda Delgado
398fa01e2caSRicardo Ribalda Delgado pdata->index = k;
399fa01e2caSRicardo Ribalda Delgado
400fab8a02bSLukas Redlinger irq_data = irq_get_irq_data(uart->port.irq);
40106e39572SJi-Ze Hong (Peter Hong) if (irq_data)
40206e39572SJi-Ze Hong (Peter Hong) level_mode =
40306e39572SJi-Ze Hong (Peter Hong) irqd_is_level_type(irq_data);
40406e39572SJi-Ze Hong (Peter Hong)
40506e39572SJi-Ze Hong (Peter Hong) fintek_8250_set_irq_mode(pdata, level_mode);
406c2236facSJi-Ze Hong (Peter Hong) fintek_8250_set_max_fifo(pdata);
407fab8a02bSLukas Redlinger
40806e39572SJi-Ze Hong (Peter Hong) fintek_8250_exit_key(addr[i]);
40906e39572SJi-Ze Hong (Peter Hong)
410fa01e2caSRicardo Ribalda Delgado return 0;
411017bec38SRicardo Ribalda Delgado }
412fa01e2caSRicardo Ribalda Delgado
41329d58642SRicardo Ribalda Delgado fintek_8250_exit_key(addr[i]);
414ce8c267eSRicardo Ribalda Delgado }
415ce8c267eSRicardo Ribalda Delgado }
416017bec38SRicardo Ribalda Delgado
417017bec38SRicardo Ribalda Delgado return -ENODEV;
418017bec38SRicardo Ribalda Delgado }
419017bec38SRicardo Ribalda Delgado
42070780464SIlpo Järvinen /* Only the first port supports delays */
42170780464SIlpo Järvinen static const struct serial_rs485 fintek_8250_rs485_supported_port0 = {
42270780464SIlpo Järvinen .flags = SER_RS485_ENABLED | SER_RS485_RTS_ON_SEND | SER_RS485_RTS_AFTER_SEND,
42370780464SIlpo Järvinen .delay_rts_before_send = 1,
42470780464SIlpo Järvinen .delay_rts_after_send = 1,
42570780464SIlpo Järvinen };
42670780464SIlpo Järvinen
42770780464SIlpo Järvinen static const struct serial_rs485 fintek_8250_rs485_supported = {
42870780464SIlpo Järvinen .flags = SER_RS485_ENABLED | SER_RS485_RTS_ON_SEND | SER_RS485_RTS_AFTER_SEND,
42970780464SIlpo Järvinen };
43070780464SIlpo Järvinen
fintek_8250_set_rs485_handler(struct uart_8250_port * uart)4311e26c472SJi-Ze Hong (Peter Hong) static void fintek_8250_set_rs485_handler(struct uart_8250_port *uart)
4321e26c472SJi-Ze Hong (Peter Hong) {
4331e26c472SJi-Ze Hong (Peter Hong) struct fintek_8250 *pdata = uart->port.private_data;
4341e26c472SJi-Ze Hong (Peter Hong)
4351e26c472SJi-Ze Hong (Peter Hong) switch (pdata->pid) {
4361e26c472SJi-Ze Hong (Peter Hong) case CHIP_ID_F81216AD:
4371e26c472SJi-Ze Hong (Peter Hong) case CHIP_ID_F81216H:
438423d9118SJi-Ze Hong (Peter Hong) case CHIP_ID_F81966:
439da60d6afSJi-Ze Hong (Peter Hong) case CHIP_ID_F81866:
440de48b099SJi-Ze Hong (Peter Hong) case CHIP_ID_F81865:
4411e26c472SJi-Ze Hong (Peter Hong) uart->port.rs485_config = fintek_8250_rs485_config;
44270780464SIlpo Järvinen if (!pdata->index)
4430139da50SIlpo Järvinen uart->port.rs485_supported = fintek_8250_rs485_supported_port0;
44470780464SIlpo Järvinen else
4450139da50SIlpo Järvinen uart->port.rs485_supported = fintek_8250_rs485_supported;
4461e26c472SJi-Ze Hong (Peter Hong) break;
4471e26c472SJi-Ze Hong (Peter Hong)
448*63ff22d7SFilip Brozovic case CHIP_ID_F81216E: /* F81216E does not support RS485 delays */
449*63ff22d7SFilip Brozovic uart->port.rs485_config = fintek_8250_rs485_config;
450*63ff22d7SFilip Brozovic uart->port.rs485_supported = fintek_8250_rs485_supported;
451*63ff22d7SFilip Brozovic break;
452*63ff22d7SFilip Brozovic
4531e26c472SJi-Ze Hong (Peter Hong) default: /* No RS485 Auto direction functional */
4541e26c472SJi-Ze Hong (Peter Hong) break;
4551e26c472SJi-Ze Hong (Peter Hong) }
4561e26c472SJi-Ze Hong (Peter Hong) }
4571e26c472SJi-Ze Hong (Peter Hong)
fintek_8250_probe(struct uart_8250_port * uart)458fa01e2caSRicardo Ribalda Delgado int fintek_8250_probe(struct uart_8250_port *uart)
45928e3fb6cSRicardo Ribalda Delgado {
46092a5f11aSRicardo Ribalda Delgado struct fintek_8250 *pdata;
461fa01e2caSRicardo Ribalda Delgado struct fintek_8250 probe_data;
46228e3fb6cSRicardo Ribalda Delgado
463fab8a02bSLukas Redlinger if (probe_setup_port(&probe_data, uart))
46428e3fb6cSRicardo Ribalda Delgado return -ENODEV;
46528e3fb6cSRicardo Ribalda Delgado
466fa01e2caSRicardo Ribalda Delgado pdata = devm_kzalloc(uart->port.dev, sizeof(*pdata), GFP_KERNEL);
46792a5f11aSRicardo Ribalda Delgado if (!pdata)
46892a5f11aSRicardo Ribalda Delgado return -ENOMEM;
46992a5f11aSRicardo Ribalda Delgado
470fa01e2caSRicardo Ribalda Delgado memcpy(pdata, &probe_data, sizeof(probe_data));
471fa01e2caSRicardo Ribalda Delgado uart->port.private_data = pdata;
4721e26c472SJi-Ze Hong (Peter Hong) fintek_8250_set_rs485_handler(uart);
473195638b6SJi-Ze Hong (Peter Hong) fintek_8250_set_termios_handler(uart);
47428e3fb6cSRicardo Ribalda Delgado
47506e39572SJi-Ze Hong (Peter Hong) return 0;
47628e3fb6cSRicardo Ribalda Delgado }
477