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) 19441e69093SRicardo Ribalda Delgado static int fintek_8250_rs485_config(struct uart_port *port, 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 203*af017927SIlpo Järvinen 204*af017927SIlpo 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; 20928e3fb6cSRicardo Ribalda Delgado memset(rs485->padding, 0, sizeof(rs485->padding)); 2107ecc7701SRicardo Ribalda Delgado config |= RS485_URA; 2117ecc7701SRicardo Ribalda Delgado } else { 21228e3fb6cSRicardo Ribalda Delgado memset(rs485, 0, sizeof(*rs485)); 2137ecc7701SRicardo Ribalda Delgado } 21428e3fb6cSRicardo Ribalda Delgado 21528e3fb6cSRicardo Ribalda Delgado rs485->flags &= SER_RS485_ENABLED | SER_RS485_RTS_ON_SEND | 21628e3fb6cSRicardo Ribalda Delgado SER_RS485_RTS_AFTER_SEND; 21728e3fb6cSRicardo Ribalda Delgado 21821c4e7f2SRicardo Ribalda Delgado /* Only the first port supports delays */ 21921c4e7f2SRicardo Ribalda Delgado if (pdata->index) { 22021c4e7f2SRicardo Ribalda Delgado rs485->delay_rts_before_send = 0; 22121c4e7f2SRicardo Ribalda Delgado rs485->delay_rts_after_send = 0; 22221c4e7f2SRicardo Ribalda Delgado } 22321c4e7f2SRicardo Ribalda Delgado 22428e3fb6cSRicardo Ribalda Delgado if (rs485->delay_rts_before_send) { 22528e3fb6cSRicardo Ribalda Delgado rs485->delay_rts_before_send = 1; 22628e3fb6cSRicardo Ribalda Delgado config |= TXW4C_IRA; 22728e3fb6cSRicardo Ribalda Delgado } 22828e3fb6cSRicardo Ribalda Delgado 22928e3fb6cSRicardo Ribalda Delgado if (rs485->delay_rts_after_send) { 23028e3fb6cSRicardo Ribalda Delgado rs485->delay_rts_after_send = 1; 23128e3fb6cSRicardo Ribalda Delgado config |= RXW4C_IRA; 23228e3fb6cSRicardo Ribalda Delgado } 23328e3fb6cSRicardo Ribalda Delgado 23428e3fb6cSRicardo Ribalda Delgado if (rs485->flags & SER_RS485_RTS_ON_SEND) 23528e3fb6cSRicardo Ribalda Delgado config |= RTS_INVERT; 23628e3fb6cSRicardo Ribalda Delgado 237ce8c267eSRicardo Ribalda Delgado if (fintek_8250_enter_key(pdata->base_port, pdata->key)) 23828e3fb6cSRicardo Ribalda Delgado return -EBUSY; 23928e3fb6cSRicardo Ribalda Delgado 240f1232ac2SJi-Ze Hong (Peter Hong) sio_write_reg(pdata, LDN, pdata->index); 241f1232ac2SJi-Ze Hong (Peter Hong) sio_write_reg(pdata, RS485, config); 242017bec38SRicardo Ribalda Delgado fintek_8250_exit_key(pdata->base_port); 24328e3fb6cSRicardo Ribalda Delgado 24441e69093SRicardo Ribalda Delgado port->rs485 = *rs485; 24541e69093SRicardo Ribalda Delgado 24628e3fb6cSRicardo Ribalda Delgado return 0; 24728e3fb6cSRicardo Ribalda Delgado } 24828e3fb6cSRicardo Ribalda Delgado 24906e39572SJi-Ze Hong (Peter Hong) static void fintek_8250_set_irq_mode(struct fintek_8250 *pdata, bool is_level) 25006e39572SJi-Ze Hong (Peter Hong) { 25106e39572SJi-Ze Hong (Peter Hong) sio_write_reg(pdata, LDN, pdata->index); 252da60d6afSJi-Ze Hong (Peter Hong) 253da60d6afSJi-Ze Hong (Peter Hong) switch (pdata->pid) { 254423d9118SJi-Ze Hong (Peter Hong) case CHIP_ID_F81966: 255da60d6afSJi-Ze Hong (Peter Hong) case CHIP_ID_F81866: 256da60d6afSJi-Ze Hong (Peter Hong) sio_write_mask_reg(pdata, F81866_FIFO_CTRL, F81866_IRQ_MODE1, 257da60d6afSJi-Ze Hong (Peter Hong) 0); 258df561f66SGustavo A. R. Silva fallthrough; 259de48b099SJi-Ze Hong (Peter Hong) case CHIP_ID_F81865: 260da60d6afSJi-Ze Hong (Peter Hong) sio_write_mask_reg(pdata, F81866_IRQ_MODE, F81866_IRQ_SHARE, 261da60d6afSJi-Ze Hong (Peter Hong) F81866_IRQ_SHARE); 262da60d6afSJi-Ze Hong (Peter Hong) sio_write_mask_reg(pdata, F81866_IRQ_MODE, F81866_IRQ_MODE0, 263da60d6afSJi-Ze Hong (Peter Hong) is_level ? 0 : F81866_IRQ_MODE0); 264da60d6afSJi-Ze Hong (Peter Hong) break; 265da60d6afSJi-Ze Hong (Peter Hong) 266da60d6afSJi-Ze Hong (Peter Hong) case CHIP_ID_F81216AD: 267da60d6afSJi-Ze Hong (Peter Hong) case CHIP_ID_F81216H: 268da60d6afSJi-Ze Hong (Peter Hong) case CHIP_ID_F81216: 269da60d6afSJi-Ze Hong (Peter Hong) sio_write_mask_reg(pdata, FINTEK_IRQ_MODE, IRQ_SHARE, 270da60d6afSJi-Ze Hong (Peter Hong) IRQ_SHARE); 27106e39572SJi-Ze Hong (Peter Hong) sio_write_mask_reg(pdata, FINTEK_IRQ_MODE, IRQ_MODE_MASK, 27206e39572SJi-Ze Hong (Peter Hong) is_level ? IRQ_LEVEL_LOW : IRQ_EDGE_HIGH); 273da60d6afSJi-Ze Hong (Peter Hong) break; 274da60d6afSJi-Ze Hong (Peter Hong) } 27506e39572SJi-Ze Hong (Peter Hong) } 27606e39572SJi-Ze Hong (Peter Hong) 277c2236facSJi-Ze Hong (Peter Hong) static void fintek_8250_set_max_fifo(struct fintek_8250 *pdata) 278c2236facSJi-Ze Hong (Peter Hong) { 279c2236facSJi-Ze Hong (Peter Hong) switch (pdata->pid) { 280c2236facSJi-Ze Hong (Peter Hong) case CHIP_ID_F81216H: /* 128Bytes FIFO */ 281423d9118SJi-Ze Hong (Peter Hong) case CHIP_ID_F81966: 282da60d6afSJi-Ze Hong (Peter Hong) case CHIP_ID_F81866: 283c2236facSJi-Ze Hong (Peter Hong) sio_write_mask_reg(pdata, FIFO_CTRL, 284c2236facSJi-Ze Hong (Peter Hong) FIFO_MODE_MASK | RXFTHR_MODE_MASK, 285c2236facSJi-Ze Hong (Peter Hong) FIFO_MODE_128 | RXFTHR_MODE_4X); 286c2236facSJi-Ze Hong (Peter Hong) break; 287c2236facSJi-Ze Hong (Peter Hong) 288c2236facSJi-Ze Hong (Peter Hong) default: /* Default 16Bytes FIFO */ 289c2236facSJi-Ze Hong (Peter Hong) break; 290c2236facSJi-Ze Hong (Peter Hong) } 291c2236facSJi-Ze Hong (Peter Hong) } 292c2236facSJi-Ze Hong (Peter Hong) 2939828def3SYueHaibing static void fintek_8250_set_termios(struct uart_port *port, 2949828def3SYueHaibing struct ktermios *termios, 295195638b6SJi-Ze Hong (Peter Hong) struct ktermios *old) 296195638b6SJi-Ze Hong (Peter Hong) { 297195638b6SJi-Ze Hong (Peter Hong) struct fintek_8250 *pdata = port->private_data; 298195638b6SJi-Ze Hong (Peter Hong) unsigned int baud = tty_termios_baud_rate(termios); 299195638b6SJi-Ze Hong (Peter Hong) int i; 30058178914SJi-Ze Hong (Peter Hong) u8 reg; 301195638b6SJi-Ze Hong (Peter Hong) static u32 baudrate_table[] = {115200, 921600, 1152000, 1500000}; 302195638b6SJi-Ze Hong (Peter Hong) static u8 clock_table[] = { F81866_UART_CLK_1_8432MHZ, 303195638b6SJi-Ze Hong (Peter Hong) F81866_UART_CLK_14_769MHZ, F81866_UART_CLK_18_432MHZ, 304195638b6SJi-Ze Hong (Peter Hong) F81866_UART_CLK_24MHZ }; 305195638b6SJi-Ze Hong (Peter Hong) 30607a708f0SJi-Ze Hong (Peter Hong) /* 30707a708f0SJi-Ze Hong (Peter Hong) * We'll use serial8250_do_set_termios() for baud = 0, otherwise It'll 30807a708f0SJi-Ze Hong (Peter Hong) * crash on baudrate_table[i] % baud with "division by zero". 30907a708f0SJi-Ze Hong (Peter Hong) */ 31007a708f0SJi-Ze Hong (Peter Hong) if (!baud) 31107a708f0SJi-Ze Hong (Peter Hong) goto exit; 31207a708f0SJi-Ze Hong (Peter Hong) 31358178914SJi-Ze Hong (Peter Hong) switch (pdata->pid) { 31458178914SJi-Ze Hong (Peter Hong) case CHIP_ID_F81216H: 31558178914SJi-Ze Hong (Peter Hong) reg = RS485; 31658178914SJi-Ze Hong (Peter Hong) break; 317423d9118SJi-Ze Hong (Peter Hong) case CHIP_ID_F81966: 31858178914SJi-Ze Hong (Peter Hong) case CHIP_ID_F81866: 31958178914SJi-Ze Hong (Peter Hong) reg = F81866_UART_CLK; 32058178914SJi-Ze Hong (Peter Hong) break; 32158178914SJi-Ze Hong (Peter Hong) default: 32258178914SJi-Ze Hong (Peter Hong) /* Don't change clocksource with unknown PID */ 32358178914SJi-Ze Hong (Peter Hong) dev_warn(port->dev, 32458178914SJi-Ze Hong (Peter Hong) "%s: pid: %x Not support. use default set_termios.\n", 32558178914SJi-Ze Hong (Peter Hong) __func__, pdata->pid); 32607a708f0SJi-Ze Hong (Peter Hong) goto exit; 32758178914SJi-Ze Hong (Peter Hong) } 32858178914SJi-Ze Hong (Peter Hong) 329195638b6SJi-Ze Hong (Peter Hong) for (i = 0; i < ARRAY_SIZE(baudrate_table); ++i) { 330195638b6SJi-Ze Hong (Peter Hong) if (baud > baudrate_table[i] || baudrate_table[i] % baud != 0) 331195638b6SJi-Ze Hong (Peter Hong) continue; 332195638b6SJi-Ze Hong (Peter Hong) 333195638b6SJi-Ze Hong (Peter Hong) if (port->uartclk == baudrate_table[i] * 16) 334195638b6SJi-Ze Hong (Peter Hong) break; 335195638b6SJi-Ze Hong (Peter Hong) 336195638b6SJi-Ze Hong (Peter Hong) if (fintek_8250_enter_key(pdata->base_port, pdata->key)) 337195638b6SJi-Ze Hong (Peter Hong) continue; 338195638b6SJi-Ze Hong (Peter Hong) 339195638b6SJi-Ze Hong (Peter Hong) port->uartclk = baudrate_table[i] * 16; 340195638b6SJi-Ze Hong (Peter Hong) 341195638b6SJi-Ze Hong (Peter Hong) sio_write_reg(pdata, LDN, pdata->index); 34258178914SJi-Ze Hong (Peter Hong) sio_write_mask_reg(pdata, reg, F81866_UART_CLK_MASK, 34358178914SJi-Ze Hong (Peter Hong) clock_table[i]); 344195638b6SJi-Ze Hong (Peter Hong) 345195638b6SJi-Ze Hong (Peter Hong) fintek_8250_exit_key(pdata->base_port); 346195638b6SJi-Ze Hong (Peter Hong) break; 347195638b6SJi-Ze Hong (Peter Hong) } 348195638b6SJi-Ze Hong (Peter Hong) 349195638b6SJi-Ze Hong (Peter Hong) if (i == ARRAY_SIZE(baudrate_table)) { 350195638b6SJi-Ze Hong (Peter Hong) baud = tty_termios_baud_rate(old); 351195638b6SJi-Ze Hong (Peter Hong) tty_termios_encode_baud_rate(termios, baud, baud); 352195638b6SJi-Ze Hong (Peter Hong) } 353195638b6SJi-Ze Hong (Peter Hong) 35407a708f0SJi-Ze Hong (Peter Hong) exit: 355195638b6SJi-Ze Hong (Peter Hong) serial8250_do_set_termios(port, termios, old); 356195638b6SJi-Ze Hong (Peter Hong) } 357195638b6SJi-Ze Hong (Peter Hong) 358195638b6SJi-Ze Hong (Peter Hong) static void fintek_8250_set_termios_handler(struct uart_8250_port *uart) 359195638b6SJi-Ze Hong (Peter Hong) { 360195638b6SJi-Ze Hong (Peter Hong) struct fintek_8250 *pdata = uart->port.private_data; 361195638b6SJi-Ze Hong (Peter Hong) 362195638b6SJi-Ze Hong (Peter Hong) switch (pdata->pid) { 36358178914SJi-Ze Hong (Peter Hong) case CHIP_ID_F81216H: 364423d9118SJi-Ze Hong (Peter Hong) case CHIP_ID_F81966: 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 41506e39572SJi-Ze Hong (Peter Hong) fintek_8250_exit_key(addr[i]); 41606e39572SJi-Ze Hong (Peter Hong) 417fa01e2caSRicardo Ribalda Delgado return 0; 418017bec38SRicardo Ribalda Delgado } 419fa01e2caSRicardo Ribalda Delgado 42029d58642SRicardo Ribalda Delgado fintek_8250_exit_key(addr[i]); 421ce8c267eSRicardo Ribalda Delgado } 422ce8c267eSRicardo Ribalda Delgado } 423017bec38SRicardo Ribalda Delgado 424017bec38SRicardo Ribalda Delgado return -ENODEV; 425017bec38SRicardo Ribalda Delgado } 426017bec38SRicardo Ribalda Delgado 4271e26c472SJi-Ze Hong (Peter Hong) static void fintek_8250_set_rs485_handler(struct uart_8250_port *uart) 4281e26c472SJi-Ze Hong (Peter Hong) { 4291e26c472SJi-Ze Hong (Peter Hong) struct fintek_8250 *pdata = uart->port.private_data; 4301e26c472SJi-Ze Hong (Peter Hong) 4311e26c472SJi-Ze Hong (Peter Hong) switch (pdata->pid) { 4321e26c472SJi-Ze Hong (Peter Hong) case CHIP_ID_F81216AD: 4331e26c472SJi-Ze Hong (Peter Hong) case CHIP_ID_F81216H: 434423d9118SJi-Ze Hong (Peter Hong) case CHIP_ID_F81966: 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