1e3b3d0f5SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 228e3fb6cSRicardo Ribalda Delgado /* 328e3fb6cSRicardo Ribalda Delgado * Probe for F81216A LPC to 4 UART 428e3fb6cSRicardo Ribalda Delgado * 5fa01e2caSRicardo Ribalda Delgado * Copyright (C) 2014-2016 Ricardo Ribalda, Qtechnology A/S 628e3fb6cSRicardo Ribalda Delgado */ 728e3fb6cSRicardo Ribalda Delgado #include <linux/module.h> 828e3fb6cSRicardo Ribalda Delgado #include <linux/pci.h> 928e3fb6cSRicardo Ribalda Delgado #include <linux/pnp.h> 1028e3fb6cSRicardo Ribalda Delgado #include <linux/kernel.h> 1128e3fb6cSRicardo Ribalda Delgado #include <linux/serial_core.h> 124da22f14SJi-Ze Hong (Peter Hong) #include <linux/irq.h> 1328e3fb6cSRicardo Ribalda Delgado #include "8250.h" 1428e3fb6cSRicardo Ribalda Delgado 15017bec38SRicardo Ribalda Delgado #define ADDR_PORT 0 16017bec38SRicardo Ribalda Delgado #define DATA_PORT 1 1728e3fb6cSRicardo Ribalda Delgado #define EXIT_KEY 0xAA 1828e3fb6cSRicardo Ribalda Delgado #define CHIP_ID1 0x20 1928e3fb6cSRicardo Ribalda Delgado #define CHIP_ID2 0x21 20de48b099SJi-Ze Hong (Peter Hong) #define CHIP_ID_F81865 0x0407 21da60d6afSJi-Ze Hong (Peter Hong) #define CHIP_ID_F81866 0x1010 22c2236facSJi-Ze Hong (Peter Hong) #define CHIP_ID_F81216AD 0x1602 23c2236facSJi-Ze Hong (Peter Hong) #define CHIP_ID_F81216H 0x0501 241e26c472SJi-Ze Hong (Peter Hong) #define CHIP_ID_F81216 0x0802 2528e3fb6cSRicardo Ribalda Delgado #define VENDOR_ID1 0x23 2628e3fb6cSRicardo Ribalda Delgado #define VENDOR_ID1_VAL 0x19 2728e3fb6cSRicardo Ribalda Delgado #define VENDOR_ID2 0x24 2828e3fb6cSRicardo Ribalda Delgado #define VENDOR_ID2_VAL 0x34 2929d58642SRicardo Ribalda Delgado #define IO_ADDR1 0x61 3029d58642SRicardo Ribalda Delgado #define IO_ADDR2 0x60 3128e3fb6cSRicardo Ribalda Delgado #define LDN 0x7 3228e3fb6cSRicardo Ribalda Delgado 3387a713c8SArnd Bergmann #define FINTEK_IRQ_MODE 0x70 344da22f14SJi-Ze Hong (Peter Hong) #define IRQ_SHARE BIT(4) 354da22f14SJi-Ze Hong (Peter Hong) #define IRQ_MODE_MASK (BIT(6) | BIT(5)) 364da22f14SJi-Ze Hong (Peter Hong) #define IRQ_LEVEL_LOW 0 374da22f14SJi-Ze Hong (Peter Hong) #define IRQ_EDGE_HIGH BIT(5) 384da22f14SJi-Ze Hong (Peter Hong) 3958178914SJi-Ze Hong (Peter Hong) /* 4058178914SJi-Ze Hong (Peter Hong) * F81216H clock source register, the value and mask is the same with F81866, 4158178914SJi-Ze Hong (Peter Hong) * but it's on F0h. 4258178914SJi-Ze Hong (Peter Hong) * 4358178914SJi-Ze Hong (Peter Hong) * Clock speeds for UART (register F0h) 4458178914SJi-Ze Hong (Peter Hong) * 00: 1.8432MHz. 4558178914SJi-Ze Hong (Peter Hong) * 01: 18.432MHz. 4658178914SJi-Ze Hong (Peter Hong) * 10: 24MHz. 4758178914SJi-Ze Hong (Peter Hong) * 11: 14.769MHz. 4858178914SJi-Ze Hong (Peter Hong) */ 4928e3fb6cSRicardo Ribalda Delgado #define RS485 0xF0 5028e3fb6cSRicardo Ribalda Delgado #define RTS_INVERT BIT(5) 5128e3fb6cSRicardo Ribalda Delgado #define RS485_URA BIT(4) 5228e3fb6cSRicardo Ribalda Delgado #define RXW4C_IRA BIT(3) 5328e3fb6cSRicardo Ribalda Delgado #define TXW4C_IRA BIT(2) 5428e3fb6cSRicardo Ribalda Delgado 55c2236facSJi-Ze Hong (Peter Hong) #define FIFO_CTRL 0xF6 56c2236facSJi-Ze Hong (Peter Hong) #define FIFO_MODE_MASK (BIT(1) | BIT(0)) 57c2236facSJi-Ze Hong (Peter Hong) #define FIFO_MODE_128 (BIT(1) | BIT(0)) 58c2236facSJi-Ze Hong (Peter Hong) #define RXFTHR_MODE_MASK (BIT(5) | BIT(4)) 59c2236facSJi-Ze Hong (Peter Hong) #define RXFTHR_MODE_4X BIT(5) 60c2236facSJi-Ze Hong (Peter Hong) 61da60d6afSJi-Ze Hong (Peter Hong) #define F81216_LDN_LOW 0x0 62da60d6afSJi-Ze Hong (Peter Hong) #define F81216_LDN_HIGH 0x4 63da60d6afSJi-Ze Hong (Peter Hong) 64da60d6afSJi-Ze Hong (Peter Hong) /* 65da60d6afSJi-Ze Hong (Peter Hong) * F81866 registers 66da60d6afSJi-Ze Hong (Peter Hong) * 67da60d6afSJi-Ze Hong (Peter Hong) * The IRQ setting mode of F81866 is not the same with F81216 series. 68da60d6afSJi-Ze Hong (Peter Hong) * Level/Low: IRQ_MODE0:0, IRQ_MODE1:0 69da60d6afSJi-Ze Hong (Peter Hong) * Edge/High: IRQ_MODE0:1, IRQ_MODE1:0 70fab8a02bSLukas Redlinger * 71fab8a02bSLukas Redlinger * Clock speeds for UART (register F2h) 72fab8a02bSLukas Redlinger * 00: 1.8432MHz. 73fab8a02bSLukas Redlinger * 01: 18.432MHz. 74fab8a02bSLukas Redlinger * 10: 24MHz. 75fab8a02bSLukas Redlinger * 11: 14.769MHz. 76da60d6afSJi-Ze Hong (Peter Hong) */ 77da60d6afSJi-Ze Hong (Peter Hong) #define F81866_IRQ_MODE 0xf0 78da60d6afSJi-Ze Hong (Peter Hong) #define F81866_IRQ_SHARE BIT(0) 79da60d6afSJi-Ze Hong (Peter Hong) #define F81866_IRQ_MODE0 BIT(1) 80da60d6afSJi-Ze Hong (Peter Hong) 81da60d6afSJi-Ze Hong (Peter Hong) #define F81866_FIFO_CTRL FIFO_CTRL 82da60d6afSJi-Ze Hong (Peter Hong) #define F81866_IRQ_MODE1 BIT(3) 83da60d6afSJi-Ze Hong (Peter Hong) 84da60d6afSJi-Ze Hong (Peter Hong) #define F81866_LDN_LOW 0x10 85da60d6afSJi-Ze Hong (Peter Hong) #define F81866_LDN_HIGH 0x16 86da60d6afSJi-Ze Hong (Peter Hong) 87fab8a02bSLukas Redlinger #define F81866_UART_CLK 0xF2 88fab8a02bSLukas Redlinger #define F81866_UART_CLK_MASK (BIT(1) | BIT(0)) 89fab8a02bSLukas Redlinger #define F81866_UART_CLK_1_8432MHZ 0 90fab8a02bSLukas Redlinger #define F81866_UART_CLK_14_769MHZ (BIT(1) | BIT(0)) 91fab8a02bSLukas Redlinger #define F81866_UART_CLK_18_432MHZ BIT(0) 92fab8a02bSLukas Redlinger #define F81866_UART_CLK_24MHZ BIT(1) 93fab8a02bSLukas Redlinger 9492a5f11aSRicardo Ribalda Delgado struct fintek_8250 { 95c2236facSJi-Ze Hong (Peter Hong) u16 pid; 96017bec38SRicardo Ribalda Delgado u16 base_port; 9792a5f11aSRicardo Ribalda Delgado u8 index; 98ce8c267eSRicardo Ribalda Delgado u8 key; 9992a5f11aSRicardo Ribalda Delgado }; 10092a5f11aSRicardo Ribalda Delgado 101f1232ac2SJi-Ze Hong (Peter Hong) static u8 sio_read_reg(struct fintek_8250 *pdata, u8 reg) 102f1232ac2SJi-Ze Hong (Peter Hong) { 103f1232ac2SJi-Ze Hong (Peter Hong) outb(reg, pdata->base_port + ADDR_PORT); 104f1232ac2SJi-Ze Hong (Peter Hong) return inb(pdata->base_port + DATA_PORT); 105f1232ac2SJi-Ze Hong (Peter Hong) } 106f1232ac2SJi-Ze Hong (Peter Hong) 107f1232ac2SJi-Ze Hong (Peter Hong) static void sio_write_reg(struct fintek_8250 *pdata, u8 reg, u8 data) 108f1232ac2SJi-Ze Hong (Peter Hong) { 109f1232ac2SJi-Ze Hong (Peter Hong) outb(reg, pdata->base_port + ADDR_PORT); 110f1232ac2SJi-Ze Hong (Peter Hong) outb(data, pdata->base_port + DATA_PORT); 111f1232ac2SJi-Ze Hong (Peter Hong) } 112f1232ac2SJi-Ze Hong (Peter Hong) 113f1232ac2SJi-Ze Hong (Peter Hong) static void sio_write_mask_reg(struct fintek_8250 *pdata, u8 reg, u8 mask, 114f1232ac2SJi-Ze Hong (Peter Hong) u8 data) 115f1232ac2SJi-Ze Hong (Peter Hong) { 116f1232ac2SJi-Ze Hong (Peter Hong) u8 tmp; 117f1232ac2SJi-Ze Hong (Peter Hong) 118f1232ac2SJi-Ze Hong (Peter Hong) tmp = (sio_read_reg(pdata, reg) & ~mask) | (mask & data); 119f1232ac2SJi-Ze Hong (Peter Hong) sio_write_reg(pdata, reg, tmp); 120f1232ac2SJi-Ze Hong (Peter Hong) } 121f1232ac2SJi-Ze Hong (Peter Hong) 122ce8c267eSRicardo Ribalda Delgado static int fintek_8250_enter_key(u16 base_port, u8 key) 123017bec38SRicardo Ribalda Delgado { 124fa01e2caSRicardo Ribalda Delgado if (!request_muxed_region(base_port, 2, "8250_fintek")) 12528e3fb6cSRicardo Ribalda Delgado return -EBUSY; 12628e3fb6cSRicardo Ribalda Delgado 127fd97e66cSJi-Ze Hong (Peter Hong) /* Force to deactive all SuperIO in this base_port */ 128fd97e66cSJi-Ze Hong (Peter Hong) outb(EXIT_KEY, base_port + ADDR_PORT); 129fd97e66cSJi-Ze Hong (Peter Hong) 130ce8c267eSRicardo Ribalda Delgado outb(key, base_port + ADDR_PORT); 131ce8c267eSRicardo Ribalda Delgado outb(key, base_port + ADDR_PORT); 13228e3fb6cSRicardo Ribalda Delgado return 0; 13328e3fb6cSRicardo Ribalda Delgado } 13428e3fb6cSRicardo Ribalda Delgado 135017bec38SRicardo Ribalda Delgado static void fintek_8250_exit_key(u16 base_port) 136017bec38SRicardo Ribalda Delgado { 13728e3fb6cSRicardo Ribalda Delgado 138017bec38SRicardo Ribalda Delgado outb(EXIT_KEY, base_port + ADDR_PORT); 139017bec38SRicardo Ribalda Delgado release_region(base_port + ADDR_PORT, 2); 14028e3fb6cSRicardo Ribalda Delgado } 14128e3fb6cSRicardo Ribalda Delgado 142f1232ac2SJi-Ze Hong (Peter Hong) static int fintek_8250_check_id(struct fintek_8250 *pdata) 14328e3fb6cSRicardo Ribalda Delgado { 144dae77f75SRicardo Ribalda Delgado u16 chip; 14528e3fb6cSRicardo Ribalda Delgado 146f1232ac2SJi-Ze Hong (Peter Hong) if (sio_read_reg(pdata, VENDOR_ID1) != VENDOR_ID1_VAL) 14728e3fb6cSRicardo Ribalda Delgado return -ENODEV; 14828e3fb6cSRicardo Ribalda Delgado 149f1232ac2SJi-Ze Hong (Peter Hong) if (sio_read_reg(pdata, VENDOR_ID2) != VENDOR_ID2_VAL) 15028e3fb6cSRicardo Ribalda Delgado return -ENODEV; 15128e3fb6cSRicardo Ribalda Delgado 152f1232ac2SJi-Ze Hong (Peter Hong) chip = sio_read_reg(pdata, CHIP_ID1); 153f1232ac2SJi-Ze Hong (Peter Hong) chip |= sio_read_reg(pdata, CHIP_ID2) << 8; 154dae77f75SRicardo Ribalda Delgado 1551e26c472SJi-Ze Hong (Peter Hong) switch (chip) { 156de48b099SJi-Ze Hong (Peter Hong) case CHIP_ID_F81865: 157da60d6afSJi-Ze Hong (Peter Hong) case CHIP_ID_F81866: 1581e26c472SJi-Ze Hong (Peter Hong) case CHIP_ID_F81216AD: 1591e26c472SJi-Ze Hong (Peter Hong) case CHIP_ID_F81216H: 1601e26c472SJi-Ze Hong (Peter Hong) case CHIP_ID_F81216: 1611e26c472SJi-Ze Hong (Peter Hong) break; 1621e26c472SJi-Ze Hong (Peter Hong) default: 163dae77f75SRicardo Ribalda Delgado return -ENODEV; 1641e26c472SJi-Ze Hong (Peter Hong) } 165dae77f75SRicardo Ribalda Delgado 166c2236facSJi-Ze Hong (Peter Hong) pdata->pid = chip; 16728e3fb6cSRicardo Ribalda Delgado return 0; 16828e3fb6cSRicardo Ribalda Delgado } 16928e3fb6cSRicardo Ribalda Delgado 170da60d6afSJi-Ze Hong (Peter Hong) static int fintek_8250_get_ldn_range(struct fintek_8250 *pdata, int *min, 171da60d6afSJi-Ze Hong (Peter Hong) int *max) 172da60d6afSJi-Ze Hong (Peter Hong) { 173da60d6afSJi-Ze Hong (Peter Hong) switch (pdata->pid) { 174de48b099SJi-Ze Hong (Peter Hong) case CHIP_ID_F81865: 175da60d6afSJi-Ze Hong (Peter Hong) case CHIP_ID_F81866: 176da60d6afSJi-Ze Hong (Peter Hong) *min = F81866_LDN_LOW; 177da60d6afSJi-Ze Hong (Peter Hong) *max = F81866_LDN_HIGH; 178da60d6afSJi-Ze Hong (Peter Hong) return 0; 179da60d6afSJi-Ze Hong (Peter Hong) 180da60d6afSJi-Ze Hong (Peter Hong) case CHIP_ID_F81216AD: 181da60d6afSJi-Ze Hong (Peter Hong) case CHIP_ID_F81216H: 182da60d6afSJi-Ze Hong (Peter Hong) case CHIP_ID_F81216: 183da60d6afSJi-Ze Hong (Peter Hong) *min = F81216_LDN_LOW; 184da60d6afSJi-Ze Hong (Peter Hong) *max = F81216_LDN_HIGH; 185da60d6afSJi-Ze Hong (Peter Hong) return 0; 186da60d6afSJi-Ze Hong (Peter Hong) } 187da60d6afSJi-Ze Hong (Peter Hong) 188da60d6afSJi-Ze Hong (Peter Hong) return -ENODEV; 189da60d6afSJi-Ze Hong (Peter Hong) } 190da60d6afSJi-Ze Hong (Peter Hong) 19141e69093SRicardo Ribalda Delgado static int fintek_8250_rs485_config(struct uart_port *port, 19228e3fb6cSRicardo Ribalda Delgado struct serial_rs485 *rs485) 19328e3fb6cSRicardo Ribalda Delgado { 19428e3fb6cSRicardo Ribalda Delgado uint8_t config = 0; 19592a5f11aSRicardo Ribalda Delgado struct fintek_8250 *pdata = port->private_data; 19628e3fb6cSRicardo Ribalda Delgado 19792a5f11aSRicardo Ribalda Delgado if (!pdata) 19828e3fb6cSRicardo Ribalda Delgado return -EINVAL; 19928e3fb6cSRicardo Ribalda Delgado 2007ecc7701SRicardo Ribalda Delgado /* Hardware do not support same RTS level on send and receive */ 2017ecc7701SRicardo Ribalda Delgado if (!(rs485->flags & SER_RS485_RTS_ON_SEND) == 2027ecc7701SRicardo Ribalda Delgado !(rs485->flags & SER_RS485_RTS_AFTER_SEND)) 2037ecc7701SRicardo Ribalda Delgado return -EINVAL; 2047ecc7701SRicardo Ribalda Delgado 2057ecc7701SRicardo Ribalda Delgado if (rs485->flags & SER_RS485_ENABLED) { 20628e3fb6cSRicardo Ribalda Delgado memset(rs485->padding, 0, sizeof(rs485->padding)); 2077ecc7701SRicardo Ribalda Delgado config |= RS485_URA; 2087ecc7701SRicardo Ribalda Delgado } else { 20928e3fb6cSRicardo Ribalda Delgado memset(rs485, 0, sizeof(*rs485)); 2107ecc7701SRicardo Ribalda Delgado } 21128e3fb6cSRicardo Ribalda Delgado 21228e3fb6cSRicardo Ribalda Delgado rs485->flags &= SER_RS485_ENABLED | SER_RS485_RTS_ON_SEND | 21328e3fb6cSRicardo Ribalda Delgado SER_RS485_RTS_AFTER_SEND; 21428e3fb6cSRicardo Ribalda Delgado 21521c4e7f2SRicardo Ribalda Delgado /* Only the first port supports delays */ 21621c4e7f2SRicardo Ribalda Delgado if (pdata->index) { 21721c4e7f2SRicardo Ribalda Delgado rs485->delay_rts_before_send = 0; 21821c4e7f2SRicardo Ribalda Delgado rs485->delay_rts_after_send = 0; 21921c4e7f2SRicardo Ribalda Delgado } 22021c4e7f2SRicardo Ribalda Delgado 22128e3fb6cSRicardo Ribalda Delgado if (rs485->delay_rts_before_send) { 22228e3fb6cSRicardo Ribalda Delgado rs485->delay_rts_before_send = 1; 22328e3fb6cSRicardo Ribalda Delgado config |= TXW4C_IRA; 22428e3fb6cSRicardo Ribalda Delgado } 22528e3fb6cSRicardo Ribalda Delgado 22628e3fb6cSRicardo Ribalda Delgado if (rs485->delay_rts_after_send) { 22728e3fb6cSRicardo Ribalda Delgado rs485->delay_rts_after_send = 1; 22828e3fb6cSRicardo Ribalda Delgado config |= RXW4C_IRA; 22928e3fb6cSRicardo Ribalda Delgado } 23028e3fb6cSRicardo Ribalda Delgado 23128e3fb6cSRicardo Ribalda Delgado if (rs485->flags & SER_RS485_RTS_ON_SEND) 23228e3fb6cSRicardo Ribalda Delgado config |= RTS_INVERT; 23328e3fb6cSRicardo Ribalda Delgado 234ce8c267eSRicardo Ribalda Delgado if (fintek_8250_enter_key(pdata->base_port, pdata->key)) 23528e3fb6cSRicardo Ribalda Delgado return -EBUSY; 23628e3fb6cSRicardo Ribalda Delgado 237f1232ac2SJi-Ze Hong (Peter Hong) sio_write_reg(pdata, LDN, pdata->index); 238f1232ac2SJi-Ze Hong (Peter Hong) sio_write_reg(pdata, RS485, config); 239017bec38SRicardo Ribalda Delgado fintek_8250_exit_key(pdata->base_port); 24028e3fb6cSRicardo Ribalda Delgado 24141e69093SRicardo Ribalda Delgado port->rs485 = *rs485; 24241e69093SRicardo Ribalda Delgado 24328e3fb6cSRicardo Ribalda Delgado return 0; 24428e3fb6cSRicardo Ribalda Delgado } 24528e3fb6cSRicardo Ribalda Delgado 24606e39572SJi-Ze Hong (Peter Hong) static void fintek_8250_set_irq_mode(struct fintek_8250 *pdata, bool is_level) 24706e39572SJi-Ze Hong (Peter Hong) { 24806e39572SJi-Ze Hong (Peter Hong) sio_write_reg(pdata, LDN, pdata->index); 249da60d6afSJi-Ze Hong (Peter Hong) 250da60d6afSJi-Ze Hong (Peter Hong) switch (pdata->pid) { 251da60d6afSJi-Ze Hong (Peter Hong) case CHIP_ID_F81866: 252da60d6afSJi-Ze Hong (Peter Hong) sio_write_mask_reg(pdata, F81866_FIFO_CTRL, F81866_IRQ_MODE1, 253da60d6afSJi-Ze Hong (Peter Hong) 0); 254de48b099SJi-Ze Hong (Peter Hong) /* fall through */ 255de48b099SJi-Ze Hong (Peter Hong) case CHIP_ID_F81865: 256da60d6afSJi-Ze Hong (Peter Hong) sio_write_mask_reg(pdata, F81866_IRQ_MODE, F81866_IRQ_SHARE, 257da60d6afSJi-Ze Hong (Peter Hong) F81866_IRQ_SHARE); 258da60d6afSJi-Ze Hong (Peter Hong) sio_write_mask_reg(pdata, F81866_IRQ_MODE, F81866_IRQ_MODE0, 259da60d6afSJi-Ze Hong (Peter Hong) is_level ? 0 : F81866_IRQ_MODE0); 260da60d6afSJi-Ze Hong (Peter Hong) break; 261da60d6afSJi-Ze Hong (Peter Hong) 262da60d6afSJi-Ze Hong (Peter Hong) case CHIP_ID_F81216AD: 263da60d6afSJi-Ze Hong (Peter Hong) case CHIP_ID_F81216H: 264da60d6afSJi-Ze Hong (Peter Hong) case CHIP_ID_F81216: 265da60d6afSJi-Ze Hong (Peter Hong) sio_write_mask_reg(pdata, FINTEK_IRQ_MODE, IRQ_SHARE, 266da60d6afSJi-Ze Hong (Peter Hong) IRQ_SHARE); 26706e39572SJi-Ze Hong (Peter Hong) sio_write_mask_reg(pdata, FINTEK_IRQ_MODE, IRQ_MODE_MASK, 26806e39572SJi-Ze Hong (Peter Hong) is_level ? IRQ_LEVEL_LOW : IRQ_EDGE_HIGH); 269da60d6afSJi-Ze Hong (Peter Hong) break; 270da60d6afSJi-Ze Hong (Peter Hong) } 27106e39572SJi-Ze Hong (Peter Hong) } 27206e39572SJi-Ze Hong (Peter Hong) 273c2236facSJi-Ze Hong (Peter Hong) static void fintek_8250_set_max_fifo(struct fintek_8250 *pdata) 274c2236facSJi-Ze Hong (Peter Hong) { 275c2236facSJi-Ze Hong (Peter Hong) switch (pdata->pid) { 276c2236facSJi-Ze Hong (Peter Hong) case CHIP_ID_F81216H: /* 128Bytes FIFO */ 277da60d6afSJi-Ze Hong (Peter Hong) case CHIP_ID_F81866: 278c2236facSJi-Ze Hong (Peter Hong) sio_write_mask_reg(pdata, FIFO_CTRL, 279c2236facSJi-Ze Hong (Peter Hong) FIFO_MODE_MASK | RXFTHR_MODE_MASK, 280c2236facSJi-Ze Hong (Peter Hong) FIFO_MODE_128 | RXFTHR_MODE_4X); 281c2236facSJi-Ze Hong (Peter Hong) break; 282c2236facSJi-Ze Hong (Peter Hong) 283c2236facSJi-Ze Hong (Peter Hong) default: /* Default 16Bytes FIFO */ 284c2236facSJi-Ze Hong (Peter Hong) break; 285c2236facSJi-Ze Hong (Peter Hong) } 286c2236facSJi-Ze Hong (Peter Hong) } 287c2236facSJi-Ze Hong (Peter Hong) 288fab8a02bSLukas Redlinger static void fintek_8250_goto_highspeed(struct uart_8250_port *uart, 289fab8a02bSLukas Redlinger struct fintek_8250 *pdata) 290fab8a02bSLukas Redlinger { 291fab8a02bSLukas Redlinger sio_write_reg(pdata, LDN, pdata->index); 292fab8a02bSLukas Redlinger 293fab8a02bSLukas Redlinger switch (pdata->pid) { 294fab8a02bSLukas Redlinger case CHIP_ID_F81866: /* set uart clock for high speed serial mode */ 295fab8a02bSLukas Redlinger sio_write_mask_reg(pdata, F81866_UART_CLK, 296fab8a02bSLukas Redlinger F81866_UART_CLK_MASK, 297fab8a02bSLukas Redlinger F81866_UART_CLK_14_769MHZ); 298fab8a02bSLukas Redlinger 299fab8a02bSLukas Redlinger uart->port.uartclk = 921600 * 16; 300fab8a02bSLukas Redlinger break; 301fab8a02bSLukas Redlinger default: /* leave clock speed untouched */ 302fab8a02bSLukas Redlinger break; 303fab8a02bSLukas Redlinger } 304fab8a02bSLukas Redlinger } 305fab8a02bSLukas Redlinger 306*9828def3SYueHaibing static void fintek_8250_set_termios(struct uart_port *port, 307*9828def3SYueHaibing struct ktermios *termios, 308195638b6SJi-Ze Hong (Peter Hong) struct ktermios *old) 309195638b6SJi-Ze Hong (Peter Hong) { 310195638b6SJi-Ze Hong (Peter Hong) struct fintek_8250 *pdata = port->private_data; 311195638b6SJi-Ze Hong (Peter Hong) unsigned int baud = tty_termios_baud_rate(termios); 312195638b6SJi-Ze Hong (Peter Hong) int i; 31358178914SJi-Ze Hong (Peter Hong) u8 reg; 314195638b6SJi-Ze Hong (Peter Hong) static u32 baudrate_table[] = {115200, 921600, 1152000, 1500000}; 315195638b6SJi-Ze Hong (Peter Hong) static u8 clock_table[] = { F81866_UART_CLK_1_8432MHZ, 316195638b6SJi-Ze Hong (Peter Hong) F81866_UART_CLK_14_769MHZ, F81866_UART_CLK_18_432MHZ, 317195638b6SJi-Ze Hong (Peter Hong) F81866_UART_CLK_24MHZ }; 318195638b6SJi-Ze Hong (Peter Hong) 31907a708f0SJi-Ze Hong (Peter Hong) /* 32007a708f0SJi-Ze Hong (Peter Hong) * We'll use serial8250_do_set_termios() for baud = 0, otherwise It'll 32107a708f0SJi-Ze Hong (Peter Hong) * crash on baudrate_table[i] % baud with "division by zero". 32207a708f0SJi-Ze Hong (Peter Hong) */ 32307a708f0SJi-Ze Hong (Peter Hong) if (!baud) 32407a708f0SJi-Ze Hong (Peter Hong) goto exit; 32507a708f0SJi-Ze Hong (Peter Hong) 32658178914SJi-Ze Hong (Peter Hong) switch (pdata->pid) { 32758178914SJi-Ze Hong (Peter Hong) case CHIP_ID_F81216H: 32858178914SJi-Ze Hong (Peter Hong) reg = RS485; 32958178914SJi-Ze Hong (Peter Hong) break; 33058178914SJi-Ze Hong (Peter Hong) case CHIP_ID_F81866: 33158178914SJi-Ze Hong (Peter Hong) reg = F81866_UART_CLK; 33258178914SJi-Ze Hong (Peter Hong) break; 33358178914SJi-Ze Hong (Peter Hong) default: 33458178914SJi-Ze Hong (Peter Hong) /* Don't change clocksource with unknown PID */ 33558178914SJi-Ze Hong (Peter Hong) dev_warn(port->dev, 33658178914SJi-Ze Hong (Peter Hong) "%s: pid: %x Not support. use default set_termios.\n", 33758178914SJi-Ze Hong (Peter Hong) __func__, pdata->pid); 33807a708f0SJi-Ze Hong (Peter Hong) goto exit; 33958178914SJi-Ze Hong (Peter Hong) } 34058178914SJi-Ze Hong (Peter Hong) 341195638b6SJi-Ze Hong (Peter Hong) for (i = 0; i < ARRAY_SIZE(baudrate_table); ++i) { 342195638b6SJi-Ze Hong (Peter Hong) if (baud > baudrate_table[i] || baudrate_table[i] % baud != 0) 343195638b6SJi-Ze Hong (Peter Hong) continue; 344195638b6SJi-Ze Hong (Peter Hong) 345195638b6SJi-Ze Hong (Peter Hong) if (port->uartclk == baudrate_table[i] * 16) 346195638b6SJi-Ze Hong (Peter Hong) break; 347195638b6SJi-Ze Hong (Peter Hong) 348195638b6SJi-Ze Hong (Peter Hong) if (fintek_8250_enter_key(pdata->base_port, pdata->key)) 349195638b6SJi-Ze Hong (Peter Hong) continue; 350195638b6SJi-Ze Hong (Peter Hong) 351195638b6SJi-Ze Hong (Peter Hong) port->uartclk = baudrate_table[i] * 16; 352195638b6SJi-Ze Hong (Peter Hong) 353195638b6SJi-Ze Hong (Peter Hong) sio_write_reg(pdata, LDN, pdata->index); 35458178914SJi-Ze Hong (Peter Hong) sio_write_mask_reg(pdata, reg, F81866_UART_CLK_MASK, 35558178914SJi-Ze Hong (Peter Hong) clock_table[i]); 356195638b6SJi-Ze Hong (Peter Hong) 357195638b6SJi-Ze Hong (Peter Hong) fintek_8250_exit_key(pdata->base_port); 358195638b6SJi-Ze Hong (Peter Hong) break; 359195638b6SJi-Ze Hong (Peter Hong) } 360195638b6SJi-Ze Hong (Peter Hong) 361195638b6SJi-Ze Hong (Peter Hong) if (i == ARRAY_SIZE(baudrate_table)) { 362195638b6SJi-Ze Hong (Peter Hong) baud = tty_termios_baud_rate(old); 363195638b6SJi-Ze Hong (Peter Hong) tty_termios_encode_baud_rate(termios, baud, baud); 364195638b6SJi-Ze Hong (Peter Hong) } 365195638b6SJi-Ze Hong (Peter Hong) 36607a708f0SJi-Ze Hong (Peter Hong) exit: 367195638b6SJi-Ze Hong (Peter Hong) serial8250_do_set_termios(port, termios, old); 368195638b6SJi-Ze Hong (Peter Hong) } 369195638b6SJi-Ze Hong (Peter Hong) 370195638b6SJi-Ze Hong (Peter Hong) static void fintek_8250_set_termios_handler(struct uart_8250_port *uart) 371195638b6SJi-Ze Hong (Peter Hong) { 372195638b6SJi-Ze Hong (Peter Hong) struct fintek_8250 *pdata = uart->port.private_data; 373195638b6SJi-Ze Hong (Peter Hong) 374195638b6SJi-Ze Hong (Peter Hong) switch (pdata->pid) { 37558178914SJi-Ze Hong (Peter Hong) case CHIP_ID_F81216H: 376195638b6SJi-Ze Hong (Peter Hong) case CHIP_ID_F81866: 377195638b6SJi-Ze Hong (Peter Hong) uart->port.set_termios = fintek_8250_set_termios; 378195638b6SJi-Ze Hong (Peter Hong) break; 379195638b6SJi-Ze Hong (Peter Hong) 380195638b6SJi-Ze Hong (Peter Hong) default: 381195638b6SJi-Ze Hong (Peter Hong) break; 382195638b6SJi-Ze Hong (Peter Hong) } 383195638b6SJi-Ze Hong (Peter Hong) } 384195638b6SJi-Ze Hong (Peter Hong) 385fab8a02bSLukas Redlinger static int probe_setup_port(struct fintek_8250 *pdata, 386fab8a02bSLukas Redlinger struct uart_8250_port *uart) 387017bec38SRicardo Ribalda Delgado { 388017bec38SRicardo Ribalda Delgado static const u16 addr[] = {0x4e, 0x2e}; 389ce8c267eSRicardo Ribalda Delgado static const u8 keys[] = {0x77, 0xa0, 0x87, 0x67}; 39006e39572SJi-Ze Hong (Peter Hong) struct irq_data *irq_data; 39106e39572SJi-Ze Hong (Peter Hong) bool level_mode = false; 392da60d6afSJi-Ze Hong (Peter Hong) int i, j, k, min, max; 393017bec38SRicardo Ribalda Delgado 394017bec38SRicardo Ribalda Delgado for (i = 0; i < ARRAY_SIZE(addr); i++) { 395ce8c267eSRicardo Ribalda Delgado for (j = 0; j < ARRAY_SIZE(keys); j++) { 396f1232ac2SJi-Ze Hong (Peter Hong) pdata->base_port = addr[i]; 397f1232ac2SJi-Ze Hong (Peter Hong) pdata->key = keys[j]; 398017bec38SRicardo Ribalda Delgado 399ce8c267eSRicardo Ribalda Delgado if (fintek_8250_enter_key(addr[i], keys[j])) 400017bec38SRicardo Ribalda Delgado continue; 401da60d6afSJi-Ze Hong (Peter Hong) if (fintek_8250_check_id(pdata) || 402da60d6afSJi-Ze Hong (Peter Hong) fintek_8250_get_ldn_range(pdata, &min, &max)) { 403017bec38SRicardo Ribalda Delgado fintek_8250_exit_key(addr[i]); 40429d58642SRicardo Ribalda Delgado continue; 40529d58642SRicardo Ribalda Delgado } 40629d58642SRicardo Ribalda Delgado 407da60d6afSJi-Ze Hong (Peter Hong) for (k = min; k < max; k++) { 40829d58642SRicardo Ribalda Delgado u16 aux; 40929d58642SRicardo Ribalda Delgado 410f1232ac2SJi-Ze Hong (Peter Hong) sio_write_reg(pdata, LDN, k); 411f1232ac2SJi-Ze Hong (Peter Hong) aux = sio_read_reg(pdata, IO_ADDR1); 412f1232ac2SJi-Ze Hong (Peter Hong) aux |= sio_read_reg(pdata, IO_ADDR2) << 8; 413fab8a02bSLukas Redlinger if (aux != uart->port.iobase) 41429d58642SRicardo Ribalda Delgado continue; 41529d58642SRicardo Ribalda Delgado 416fa01e2caSRicardo Ribalda Delgado pdata->index = k; 417fa01e2caSRicardo Ribalda Delgado 418fab8a02bSLukas Redlinger irq_data = irq_get_irq_data(uart->port.irq); 41906e39572SJi-Ze Hong (Peter Hong) if (irq_data) 42006e39572SJi-Ze Hong (Peter Hong) level_mode = 42106e39572SJi-Ze Hong (Peter Hong) irqd_is_level_type(irq_data); 42206e39572SJi-Ze Hong (Peter Hong) 42306e39572SJi-Ze Hong (Peter Hong) fintek_8250_set_irq_mode(pdata, level_mode); 424c2236facSJi-Ze Hong (Peter Hong) fintek_8250_set_max_fifo(pdata); 425fab8a02bSLukas Redlinger fintek_8250_goto_highspeed(uart, pdata); 426fab8a02bSLukas Redlinger 42706e39572SJi-Ze Hong (Peter Hong) fintek_8250_exit_key(addr[i]); 42806e39572SJi-Ze Hong (Peter Hong) 429fa01e2caSRicardo Ribalda Delgado return 0; 430017bec38SRicardo Ribalda Delgado } 431fa01e2caSRicardo Ribalda Delgado 43229d58642SRicardo Ribalda Delgado fintek_8250_exit_key(addr[i]); 433ce8c267eSRicardo Ribalda Delgado } 434ce8c267eSRicardo Ribalda Delgado } 435017bec38SRicardo Ribalda Delgado 436017bec38SRicardo Ribalda Delgado return -ENODEV; 437017bec38SRicardo Ribalda Delgado } 438017bec38SRicardo Ribalda Delgado 4391e26c472SJi-Ze Hong (Peter Hong) static void fintek_8250_set_rs485_handler(struct uart_8250_port *uart) 4401e26c472SJi-Ze Hong (Peter Hong) { 4411e26c472SJi-Ze Hong (Peter Hong) struct fintek_8250 *pdata = uart->port.private_data; 4421e26c472SJi-Ze Hong (Peter Hong) 4431e26c472SJi-Ze Hong (Peter Hong) switch (pdata->pid) { 4441e26c472SJi-Ze Hong (Peter Hong) case CHIP_ID_F81216AD: 4451e26c472SJi-Ze Hong (Peter Hong) case CHIP_ID_F81216H: 446da60d6afSJi-Ze Hong (Peter Hong) case CHIP_ID_F81866: 447de48b099SJi-Ze Hong (Peter Hong) case CHIP_ID_F81865: 4481e26c472SJi-Ze Hong (Peter Hong) uart->port.rs485_config = fintek_8250_rs485_config; 4491e26c472SJi-Ze Hong (Peter Hong) break; 4501e26c472SJi-Ze Hong (Peter Hong) 4511e26c472SJi-Ze Hong (Peter Hong) default: /* No RS485 Auto direction functional */ 4521e26c472SJi-Ze Hong (Peter Hong) break; 4531e26c472SJi-Ze Hong (Peter Hong) } 4541e26c472SJi-Ze Hong (Peter Hong) } 4551e26c472SJi-Ze Hong (Peter Hong) 456fa01e2caSRicardo Ribalda Delgado int fintek_8250_probe(struct uart_8250_port *uart) 45728e3fb6cSRicardo Ribalda Delgado { 45892a5f11aSRicardo Ribalda Delgado struct fintek_8250 *pdata; 459fa01e2caSRicardo Ribalda Delgado struct fintek_8250 probe_data; 46028e3fb6cSRicardo Ribalda Delgado 461fab8a02bSLukas Redlinger if (probe_setup_port(&probe_data, uart)) 46228e3fb6cSRicardo Ribalda Delgado return -ENODEV; 46328e3fb6cSRicardo Ribalda Delgado 464fa01e2caSRicardo Ribalda Delgado pdata = devm_kzalloc(uart->port.dev, sizeof(*pdata), GFP_KERNEL); 46592a5f11aSRicardo Ribalda Delgado if (!pdata) 46692a5f11aSRicardo Ribalda Delgado return -ENOMEM; 46792a5f11aSRicardo Ribalda Delgado 468fa01e2caSRicardo Ribalda Delgado memcpy(pdata, &probe_data, sizeof(probe_data)); 469fa01e2caSRicardo Ribalda Delgado uart->port.private_data = pdata; 4701e26c472SJi-Ze Hong (Peter Hong) fintek_8250_set_rs485_handler(uart); 471195638b6SJi-Ze Hong (Peter Hong) fintek_8250_set_termios_handler(uart); 47228e3fb6cSRicardo Ribalda Delgado 47306e39572SJi-Ze Hong (Peter Hong) return 0; 47428e3fb6cSRicardo Ribalda Delgado } 475