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) 4328e3fb6cSRicardo Ribalda Delgado #define RS485 0xF0 4428e3fb6cSRicardo Ribalda Delgado #define RTS_INVERT BIT(5) 4528e3fb6cSRicardo Ribalda Delgado #define RS485_URA BIT(4) 4628e3fb6cSRicardo Ribalda Delgado #define RXW4C_IRA BIT(3) 4728e3fb6cSRicardo Ribalda Delgado #define TXW4C_IRA BIT(2) 4828e3fb6cSRicardo Ribalda Delgado 49c2236facSJi-Ze Hong (Peter Hong) #define FIFO_CTRL 0xF6 50c2236facSJi-Ze Hong (Peter Hong) #define FIFO_MODE_MASK (BIT(1) | BIT(0)) 51c2236facSJi-Ze Hong (Peter Hong) #define FIFO_MODE_128 (BIT(1) | BIT(0)) 52c2236facSJi-Ze Hong (Peter Hong) #define RXFTHR_MODE_MASK (BIT(5) | BIT(4)) 53c2236facSJi-Ze Hong (Peter Hong) #define RXFTHR_MODE_4X BIT(5) 54c2236facSJi-Ze Hong (Peter Hong) 55da60d6afSJi-Ze Hong (Peter Hong) #define F81216_LDN_LOW 0x0 56da60d6afSJi-Ze Hong (Peter Hong) #define F81216_LDN_HIGH 0x4 57da60d6afSJi-Ze Hong (Peter Hong) 58da60d6afSJi-Ze Hong (Peter Hong) /* 59da60d6afSJi-Ze Hong (Peter Hong) * F81866 registers 60da60d6afSJi-Ze Hong (Peter Hong) * 61da60d6afSJi-Ze Hong (Peter Hong) * The IRQ setting mode of F81866 is not the same with F81216 series. 62da60d6afSJi-Ze Hong (Peter Hong) * Level/Low: IRQ_MODE0:0, IRQ_MODE1:0 63da60d6afSJi-Ze Hong (Peter Hong) * Edge/High: IRQ_MODE0:1, IRQ_MODE1:0 64fab8a02bSLukas Redlinger * 65fab8a02bSLukas Redlinger * Clock speeds for UART (register F2h) 66fab8a02bSLukas Redlinger * 00: 1.8432MHz. 67fab8a02bSLukas Redlinger * 01: 18.432MHz. 68fab8a02bSLukas Redlinger * 10: 24MHz. 69fab8a02bSLukas Redlinger * 11: 14.769MHz. 70da60d6afSJi-Ze Hong (Peter Hong) */ 71da60d6afSJi-Ze Hong (Peter Hong) #define F81866_IRQ_MODE 0xf0 72da60d6afSJi-Ze Hong (Peter Hong) #define F81866_IRQ_SHARE BIT(0) 73da60d6afSJi-Ze Hong (Peter Hong) #define F81866_IRQ_MODE0 BIT(1) 74da60d6afSJi-Ze Hong (Peter Hong) 75da60d6afSJi-Ze Hong (Peter Hong) #define F81866_FIFO_CTRL FIFO_CTRL 76da60d6afSJi-Ze Hong (Peter Hong) #define F81866_IRQ_MODE1 BIT(3) 77da60d6afSJi-Ze Hong (Peter Hong) 78da60d6afSJi-Ze Hong (Peter Hong) #define F81866_LDN_LOW 0x10 79da60d6afSJi-Ze Hong (Peter Hong) #define F81866_LDN_HIGH 0x16 80da60d6afSJi-Ze Hong (Peter Hong) 81fab8a02bSLukas Redlinger #define F81866_UART_CLK 0xF2 82fab8a02bSLukas Redlinger #define F81866_UART_CLK_MASK (BIT(1) | BIT(0)) 83fab8a02bSLukas Redlinger #define F81866_UART_CLK_1_8432MHZ 0 84fab8a02bSLukas Redlinger #define F81866_UART_CLK_14_769MHZ (BIT(1) | BIT(0)) 85fab8a02bSLukas Redlinger #define F81866_UART_CLK_18_432MHZ BIT(0) 86fab8a02bSLukas Redlinger #define F81866_UART_CLK_24MHZ BIT(1) 87fab8a02bSLukas Redlinger 8892a5f11aSRicardo Ribalda Delgado struct fintek_8250 { 89c2236facSJi-Ze Hong (Peter Hong) u16 pid; 90017bec38SRicardo Ribalda Delgado u16 base_port; 9192a5f11aSRicardo Ribalda Delgado u8 index; 92ce8c267eSRicardo Ribalda Delgado u8 key; 9392a5f11aSRicardo Ribalda Delgado }; 9492a5f11aSRicardo Ribalda Delgado 95f1232ac2SJi-Ze Hong (Peter Hong) static u8 sio_read_reg(struct fintek_8250 *pdata, u8 reg) 96f1232ac2SJi-Ze Hong (Peter Hong) { 97f1232ac2SJi-Ze Hong (Peter Hong) outb(reg, pdata->base_port + ADDR_PORT); 98f1232ac2SJi-Ze Hong (Peter Hong) return inb(pdata->base_port + DATA_PORT); 99f1232ac2SJi-Ze Hong (Peter Hong) } 100f1232ac2SJi-Ze Hong (Peter Hong) 101f1232ac2SJi-Ze Hong (Peter Hong) static void sio_write_reg(struct fintek_8250 *pdata, u8 reg, u8 data) 102f1232ac2SJi-Ze Hong (Peter Hong) { 103f1232ac2SJi-Ze Hong (Peter Hong) outb(reg, pdata->base_port + ADDR_PORT); 104f1232ac2SJi-Ze Hong (Peter Hong) outb(data, pdata->base_port + DATA_PORT); 105f1232ac2SJi-Ze Hong (Peter Hong) } 106f1232ac2SJi-Ze Hong (Peter Hong) 107f1232ac2SJi-Ze Hong (Peter Hong) static void sio_write_mask_reg(struct fintek_8250 *pdata, u8 reg, u8 mask, 108f1232ac2SJi-Ze Hong (Peter Hong) u8 data) 109f1232ac2SJi-Ze Hong (Peter Hong) { 110f1232ac2SJi-Ze Hong (Peter Hong) u8 tmp; 111f1232ac2SJi-Ze Hong (Peter Hong) 112f1232ac2SJi-Ze Hong (Peter Hong) tmp = (sio_read_reg(pdata, reg) & ~mask) | (mask & data); 113f1232ac2SJi-Ze Hong (Peter Hong) sio_write_reg(pdata, reg, tmp); 114f1232ac2SJi-Ze Hong (Peter Hong) } 115f1232ac2SJi-Ze Hong (Peter Hong) 116ce8c267eSRicardo Ribalda Delgado static int fintek_8250_enter_key(u16 base_port, u8 key) 117017bec38SRicardo Ribalda Delgado { 118fa01e2caSRicardo Ribalda Delgado if (!request_muxed_region(base_port, 2, "8250_fintek")) 11928e3fb6cSRicardo Ribalda Delgado return -EBUSY; 12028e3fb6cSRicardo Ribalda Delgado 121ce8c267eSRicardo Ribalda Delgado outb(key, base_port + ADDR_PORT); 122ce8c267eSRicardo Ribalda Delgado outb(key, base_port + ADDR_PORT); 12328e3fb6cSRicardo Ribalda Delgado return 0; 12428e3fb6cSRicardo Ribalda Delgado } 12528e3fb6cSRicardo Ribalda Delgado 126017bec38SRicardo Ribalda Delgado static void fintek_8250_exit_key(u16 base_port) 127017bec38SRicardo Ribalda Delgado { 12828e3fb6cSRicardo Ribalda Delgado 129017bec38SRicardo Ribalda Delgado outb(EXIT_KEY, base_port + ADDR_PORT); 130017bec38SRicardo Ribalda Delgado release_region(base_port + ADDR_PORT, 2); 13128e3fb6cSRicardo Ribalda Delgado } 13228e3fb6cSRicardo Ribalda Delgado 133f1232ac2SJi-Ze Hong (Peter Hong) static int fintek_8250_check_id(struct fintek_8250 *pdata) 13428e3fb6cSRicardo Ribalda Delgado { 135dae77f75SRicardo Ribalda Delgado u16 chip; 13628e3fb6cSRicardo Ribalda Delgado 137f1232ac2SJi-Ze Hong (Peter Hong) if (sio_read_reg(pdata, VENDOR_ID1) != VENDOR_ID1_VAL) 13828e3fb6cSRicardo Ribalda Delgado return -ENODEV; 13928e3fb6cSRicardo Ribalda Delgado 140f1232ac2SJi-Ze Hong (Peter Hong) if (sio_read_reg(pdata, VENDOR_ID2) != VENDOR_ID2_VAL) 14128e3fb6cSRicardo Ribalda Delgado return -ENODEV; 14228e3fb6cSRicardo Ribalda Delgado 143f1232ac2SJi-Ze Hong (Peter Hong) chip = sio_read_reg(pdata, CHIP_ID1); 144f1232ac2SJi-Ze Hong (Peter Hong) chip |= sio_read_reg(pdata, CHIP_ID2) << 8; 145dae77f75SRicardo Ribalda Delgado 1461e26c472SJi-Ze Hong (Peter Hong) switch (chip) { 147de48b099SJi-Ze Hong (Peter Hong) case CHIP_ID_F81865: 148da60d6afSJi-Ze Hong (Peter Hong) case CHIP_ID_F81866: 1491e26c472SJi-Ze Hong (Peter Hong) case CHIP_ID_F81216AD: 1501e26c472SJi-Ze Hong (Peter Hong) case CHIP_ID_F81216H: 1511e26c472SJi-Ze Hong (Peter Hong) case CHIP_ID_F81216: 1521e26c472SJi-Ze Hong (Peter Hong) break; 1531e26c472SJi-Ze Hong (Peter Hong) default: 154dae77f75SRicardo Ribalda Delgado return -ENODEV; 1551e26c472SJi-Ze Hong (Peter Hong) } 156dae77f75SRicardo Ribalda Delgado 157c2236facSJi-Ze Hong (Peter Hong) pdata->pid = chip; 15828e3fb6cSRicardo Ribalda Delgado return 0; 15928e3fb6cSRicardo Ribalda Delgado } 16028e3fb6cSRicardo Ribalda Delgado 161da60d6afSJi-Ze Hong (Peter Hong) static int fintek_8250_get_ldn_range(struct fintek_8250 *pdata, int *min, 162da60d6afSJi-Ze Hong (Peter Hong) int *max) 163da60d6afSJi-Ze Hong (Peter Hong) { 164da60d6afSJi-Ze Hong (Peter Hong) switch (pdata->pid) { 165de48b099SJi-Ze Hong (Peter Hong) case CHIP_ID_F81865: 166da60d6afSJi-Ze Hong (Peter Hong) case CHIP_ID_F81866: 167da60d6afSJi-Ze Hong (Peter Hong) *min = F81866_LDN_LOW; 168da60d6afSJi-Ze Hong (Peter Hong) *max = F81866_LDN_HIGH; 169da60d6afSJi-Ze Hong (Peter Hong) return 0; 170da60d6afSJi-Ze Hong (Peter Hong) 171da60d6afSJi-Ze Hong (Peter Hong) case CHIP_ID_F81216AD: 172da60d6afSJi-Ze Hong (Peter Hong) case CHIP_ID_F81216H: 173da60d6afSJi-Ze Hong (Peter Hong) case CHIP_ID_F81216: 174da60d6afSJi-Ze Hong (Peter Hong) *min = F81216_LDN_LOW; 175da60d6afSJi-Ze Hong (Peter Hong) *max = F81216_LDN_HIGH; 176da60d6afSJi-Ze Hong (Peter Hong) return 0; 177da60d6afSJi-Ze Hong (Peter Hong) } 178da60d6afSJi-Ze Hong (Peter Hong) 179da60d6afSJi-Ze Hong (Peter Hong) return -ENODEV; 180da60d6afSJi-Ze Hong (Peter Hong) } 181da60d6afSJi-Ze Hong (Peter Hong) 18241e69093SRicardo Ribalda Delgado static int fintek_8250_rs485_config(struct uart_port *port, 18328e3fb6cSRicardo Ribalda Delgado struct serial_rs485 *rs485) 18428e3fb6cSRicardo Ribalda Delgado { 18528e3fb6cSRicardo Ribalda Delgado uint8_t config = 0; 18692a5f11aSRicardo Ribalda Delgado struct fintek_8250 *pdata = port->private_data; 18728e3fb6cSRicardo Ribalda Delgado 18892a5f11aSRicardo Ribalda Delgado if (!pdata) 18928e3fb6cSRicardo Ribalda Delgado return -EINVAL; 19028e3fb6cSRicardo Ribalda Delgado 19128e3fb6cSRicardo Ribalda Delgado if (rs485->flags & SER_RS485_ENABLED) 19228e3fb6cSRicardo Ribalda Delgado memset(rs485->padding, 0, sizeof(rs485->padding)); 19328e3fb6cSRicardo Ribalda Delgado else 19428e3fb6cSRicardo Ribalda Delgado memset(rs485, 0, sizeof(*rs485)); 19528e3fb6cSRicardo Ribalda Delgado 19628e3fb6cSRicardo Ribalda Delgado rs485->flags &= SER_RS485_ENABLED | SER_RS485_RTS_ON_SEND | 19728e3fb6cSRicardo Ribalda Delgado SER_RS485_RTS_AFTER_SEND; 19828e3fb6cSRicardo Ribalda Delgado 19928e3fb6cSRicardo Ribalda Delgado if (rs485->delay_rts_before_send) { 20028e3fb6cSRicardo Ribalda Delgado rs485->delay_rts_before_send = 1; 20128e3fb6cSRicardo Ribalda Delgado config |= TXW4C_IRA; 20228e3fb6cSRicardo Ribalda Delgado } 20328e3fb6cSRicardo Ribalda Delgado 20428e3fb6cSRicardo Ribalda Delgado if (rs485->delay_rts_after_send) { 20528e3fb6cSRicardo Ribalda Delgado rs485->delay_rts_after_send = 1; 20628e3fb6cSRicardo Ribalda Delgado config |= RXW4C_IRA; 20728e3fb6cSRicardo Ribalda Delgado } 20828e3fb6cSRicardo Ribalda Delgado 20928e3fb6cSRicardo Ribalda Delgado if ((!!(rs485->flags & SER_RS485_RTS_ON_SEND)) == 21028e3fb6cSRicardo Ribalda Delgado (!!(rs485->flags & SER_RS485_RTS_AFTER_SEND))) 21128e3fb6cSRicardo Ribalda Delgado rs485->flags &= SER_RS485_ENABLED; 21228e3fb6cSRicardo Ribalda Delgado else 21328e3fb6cSRicardo Ribalda Delgado config |= RS485_URA; 21428e3fb6cSRicardo Ribalda Delgado 21528e3fb6cSRicardo Ribalda Delgado if (rs485->flags & SER_RS485_RTS_ON_SEND) 21628e3fb6cSRicardo Ribalda Delgado config |= RTS_INVERT; 21728e3fb6cSRicardo Ribalda Delgado 218ce8c267eSRicardo Ribalda Delgado if (fintek_8250_enter_key(pdata->base_port, pdata->key)) 21928e3fb6cSRicardo Ribalda Delgado return -EBUSY; 22028e3fb6cSRicardo Ribalda Delgado 221f1232ac2SJi-Ze Hong (Peter Hong) sio_write_reg(pdata, LDN, pdata->index); 222f1232ac2SJi-Ze Hong (Peter Hong) sio_write_reg(pdata, RS485, config); 223017bec38SRicardo Ribalda Delgado fintek_8250_exit_key(pdata->base_port); 22428e3fb6cSRicardo Ribalda Delgado 22541e69093SRicardo Ribalda Delgado port->rs485 = *rs485; 22641e69093SRicardo Ribalda Delgado 22728e3fb6cSRicardo Ribalda Delgado return 0; 22828e3fb6cSRicardo Ribalda Delgado } 22928e3fb6cSRicardo Ribalda Delgado 23006e39572SJi-Ze Hong (Peter Hong) static void fintek_8250_set_irq_mode(struct fintek_8250 *pdata, bool is_level) 23106e39572SJi-Ze Hong (Peter Hong) { 23206e39572SJi-Ze Hong (Peter Hong) sio_write_reg(pdata, LDN, pdata->index); 233da60d6afSJi-Ze Hong (Peter Hong) 234da60d6afSJi-Ze Hong (Peter Hong) switch (pdata->pid) { 235da60d6afSJi-Ze Hong (Peter Hong) case CHIP_ID_F81866: 236da60d6afSJi-Ze Hong (Peter Hong) sio_write_mask_reg(pdata, F81866_FIFO_CTRL, F81866_IRQ_MODE1, 237da60d6afSJi-Ze Hong (Peter Hong) 0); 238de48b099SJi-Ze Hong (Peter Hong) /* fall through */ 239de48b099SJi-Ze Hong (Peter Hong) case CHIP_ID_F81865: 240da60d6afSJi-Ze Hong (Peter Hong) sio_write_mask_reg(pdata, F81866_IRQ_MODE, F81866_IRQ_SHARE, 241da60d6afSJi-Ze Hong (Peter Hong) F81866_IRQ_SHARE); 242da60d6afSJi-Ze Hong (Peter Hong) sio_write_mask_reg(pdata, F81866_IRQ_MODE, F81866_IRQ_MODE0, 243da60d6afSJi-Ze Hong (Peter Hong) is_level ? 0 : F81866_IRQ_MODE0); 244da60d6afSJi-Ze Hong (Peter Hong) break; 245da60d6afSJi-Ze Hong (Peter Hong) 246da60d6afSJi-Ze Hong (Peter Hong) case CHIP_ID_F81216AD: 247da60d6afSJi-Ze Hong (Peter Hong) case CHIP_ID_F81216H: 248da60d6afSJi-Ze Hong (Peter Hong) case CHIP_ID_F81216: 249da60d6afSJi-Ze Hong (Peter Hong) sio_write_mask_reg(pdata, FINTEK_IRQ_MODE, IRQ_SHARE, 250da60d6afSJi-Ze Hong (Peter Hong) IRQ_SHARE); 25106e39572SJi-Ze Hong (Peter Hong) sio_write_mask_reg(pdata, FINTEK_IRQ_MODE, IRQ_MODE_MASK, 25206e39572SJi-Ze Hong (Peter Hong) is_level ? IRQ_LEVEL_LOW : IRQ_EDGE_HIGH); 253da60d6afSJi-Ze Hong (Peter Hong) break; 254da60d6afSJi-Ze Hong (Peter Hong) } 25506e39572SJi-Ze Hong (Peter Hong) } 25606e39572SJi-Ze Hong (Peter Hong) 257c2236facSJi-Ze Hong (Peter Hong) static void fintek_8250_set_max_fifo(struct fintek_8250 *pdata) 258c2236facSJi-Ze Hong (Peter Hong) { 259c2236facSJi-Ze Hong (Peter Hong) switch (pdata->pid) { 260c2236facSJi-Ze Hong (Peter Hong) case CHIP_ID_F81216H: /* 128Bytes FIFO */ 261da60d6afSJi-Ze Hong (Peter Hong) case CHIP_ID_F81866: 262c2236facSJi-Ze Hong (Peter Hong) sio_write_mask_reg(pdata, FIFO_CTRL, 263c2236facSJi-Ze Hong (Peter Hong) FIFO_MODE_MASK | RXFTHR_MODE_MASK, 264c2236facSJi-Ze Hong (Peter Hong) FIFO_MODE_128 | RXFTHR_MODE_4X); 265c2236facSJi-Ze Hong (Peter Hong) break; 266c2236facSJi-Ze Hong (Peter Hong) 267c2236facSJi-Ze Hong (Peter Hong) default: /* Default 16Bytes FIFO */ 268c2236facSJi-Ze Hong (Peter Hong) break; 269c2236facSJi-Ze Hong (Peter Hong) } 270c2236facSJi-Ze Hong (Peter Hong) } 271c2236facSJi-Ze Hong (Peter Hong) 272fab8a02bSLukas Redlinger static void fintek_8250_goto_highspeed(struct uart_8250_port *uart, 273fab8a02bSLukas Redlinger struct fintek_8250 *pdata) 274fab8a02bSLukas Redlinger { 275fab8a02bSLukas Redlinger sio_write_reg(pdata, LDN, pdata->index); 276fab8a02bSLukas Redlinger 277fab8a02bSLukas Redlinger switch (pdata->pid) { 278fab8a02bSLukas Redlinger case CHIP_ID_F81866: /* set uart clock for high speed serial mode */ 279fab8a02bSLukas Redlinger sio_write_mask_reg(pdata, F81866_UART_CLK, 280fab8a02bSLukas Redlinger F81866_UART_CLK_MASK, 281fab8a02bSLukas Redlinger F81866_UART_CLK_14_769MHZ); 282fab8a02bSLukas Redlinger 283fab8a02bSLukas Redlinger uart->port.uartclk = 921600 * 16; 284fab8a02bSLukas Redlinger break; 285fab8a02bSLukas Redlinger default: /* leave clock speed untouched */ 286fab8a02bSLukas Redlinger break; 287fab8a02bSLukas Redlinger } 288fab8a02bSLukas Redlinger } 289fab8a02bSLukas Redlinger 290*195638b6SJi-Ze Hong (Peter Hong) void fintek_8250_set_termios(struct uart_port *port, struct ktermios *termios, 291*195638b6SJi-Ze Hong (Peter Hong) struct ktermios *old) 292*195638b6SJi-Ze Hong (Peter Hong) { 293*195638b6SJi-Ze Hong (Peter Hong) struct fintek_8250 *pdata = port->private_data; 294*195638b6SJi-Ze Hong (Peter Hong) unsigned int baud = tty_termios_baud_rate(termios); 295*195638b6SJi-Ze Hong (Peter Hong) int i; 296*195638b6SJi-Ze Hong (Peter Hong) static u32 baudrate_table[] = {115200, 921600, 1152000, 1500000}; 297*195638b6SJi-Ze Hong (Peter Hong) static u8 clock_table[] = { F81866_UART_CLK_1_8432MHZ, 298*195638b6SJi-Ze Hong (Peter Hong) F81866_UART_CLK_14_769MHZ, F81866_UART_CLK_18_432MHZ, 299*195638b6SJi-Ze Hong (Peter Hong) F81866_UART_CLK_24MHZ }; 300*195638b6SJi-Ze Hong (Peter Hong) 301*195638b6SJi-Ze Hong (Peter Hong) for (i = 0; i < ARRAY_SIZE(baudrate_table); ++i) { 302*195638b6SJi-Ze Hong (Peter Hong) if (baud > baudrate_table[i] || baudrate_table[i] % baud != 0) 303*195638b6SJi-Ze Hong (Peter Hong) continue; 304*195638b6SJi-Ze Hong (Peter Hong) 305*195638b6SJi-Ze Hong (Peter Hong) if (port->uartclk == baudrate_table[i] * 16) 306*195638b6SJi-Ze Hong (Peter Hong) break; 307*195638b6SJi-Ze Hong (Peter Hong) 308*195638b6SJi-Ze Hong (Peter Hong) if (fintek_8250_enter_key(pdata->base_port, pdata->key)) 309*195638b6SJi-Ze Hong (Peter Hong) continue; 310*195638b6SJi-Ze Hong (Peter Hong) 311*195638b6SJi-Ze Hong (Peter Hong) port->uartclk = baudrate_table[i] * 16; 312*195638b6SJi-Ze Hong (Peter Hong) 313*195638b6SJi-Ze Hong (Peter Hong) sio_write_reg(pdata, LDN, pdata->index); 314*195638b6SJi-Ze Hong (Peter Hong) sio_write_mask_reg(pdata, F81866_UART_CLK, 315*195638b6SJi-Ze Hong (Peter Hong) F81866_UART_CLK_MASK, clock_table[i]); 316*195638b6SJi-Ze Hong (Peter Hong) 317*195638b6SJi-Ze Hong (Peter Hong) fintek_8250_exit_key(pdata->base_port); 318*195638b6SJi-Ze Hong (Peter Hong) break; 319*195638b6SJi-Ze Hong (Peter Hong) } 320*195638b6SJi-Ze Hong (Peter Hong) 321*195638b6SJi-Ze Hong (Peter Hong) if (i == ARRAY_SIZE(baudrate_table)) { 322*195638b6SJi-Ze Hong (Peter Hong) baud = tty_termios_baud_rate(old); 323*195638b6SJi-Ze Hong (Peter Hong) tty_termios_encode_baud_rate(termios, baud, baud); 324*195638b6SJi-Ze Hong (Peter Hong) } 325*195638b6SJi-Ze Hong (Peter Hong) 326*195638b6SJi-Ze Hong (Peter Hong) serial8250_do_set_termios(port, termios, old); 327*195638b6SJi-Ze Hong (Peter Hong) } 328*195638b6SJi-Ze Hong (Peter Hong) 329*195638b6SJi-Ze Hong (Peter Hong) static void fintek_8250_set_termios_handler(struct uart_8250_port *uart) 330*195638b6SJi-Ze Hong (Peter Hong) { 331*195638b6SJi-Ze Hong (Peter Hong) struct fintek_8250 *pdata = uart->port.private_data; 332*195638b6SJi-Ze Hong (Peter Hong) 333*195638b6SJi-Ze Hong (Peter Hong) switch (pdata->pid) { 334*195638b6SJi-Ze Hong (Peter Hong) case CHIP_ID_F81866: 335*195638b6SJi-Ze Hong (Peter Hong) uart->port.set_termios = fintek_8250_set_termios; 336*195638b6SJi-Ze Hong (Peter Hong) break; 337*195638b6SJi-Ze Hong (Peter Hong) 338*195638b6SJi-Ze Hong (Peter Hong) default: 339*195638b6SJi-Ze Hong (Peter Hong) break; 340*195638b6SJi-Ze Hong (Peter Hong) } 341*195638b6SJi-Ze Hong (Peter Hong) } 342*195638b6SJi-Ze Hong (Peter Hong) 343fab8a02bSLukas Redlinger static int probe_setup_port(struct fintek_8250 *pdata, 344fab8a02bSLukas Redlinger struct uart_8250_port *uart) 345017bec38SRicardo Ribalda Delgado { 346017bec38SRicardo Ribalda Delgado static const u16 addr[] = {0x4e, 0x2e}; 347ce8c267eSRicardo Ribalda Delgado static const u8 keys[] = {0x77, 0xa0, 0x87, 0x67}; 34806e39572SJi-Ze Hong (Peter Hong) struct irq_data *irq_data; 34906e39572SJi-Ze Hong (Peter Hong) bool level_mode = false; 350da60d6afSJi-Ze Hong (Peter Hong) int i, j, k, min, max; 351017bec38SRicardo Ribalda Delgado 352017bec38SRicardo Ribalda Delgado for (i = 0; i < ARRAY_SIZE(addr); i++) { 353ce8c267eSRicardo Ribalda Delgado for (j = 0; j < ARRAY_SIZE(keys); j++) { 354f1232ac2SJi-Ze Hong (Peter Hong) pdata->base_port = addr[i]; 355f1232ac2SJi-Ze Hong (Peter Hong) pdata->key = keys[j]; 356017bec38SRicardo Ribalda Delgado 357ce8c267eSRicardo Ribalda Delgado if (fintek_8250_enter_key(addr[i], keys[j])) 358017bec38SRicardo Ribalda Delgado continue; 359da60d6afSJi-Ze Hong (Peter Hong) if (fintek_8250_check_id(pdata) || 360da60d6afSJi-Ze Hong (Peter Hong) fintek_8250_get_ldn_range(pdata, &min, &max)) { 361017bec38SRicardo Ribalda Delgado fintek_8250_exit_key(addr[i]); 36229d58642SRicardo Ribalda Delgado continue; 36329d58642SRicardo Ribalda Delgado } 36429d58642SRicardo Ribalda Delgado 365da60d6afSJi-Ze Hong (Peter Hong) for (k = min; k < max; k++) { 36629d58642SRicardo Ribalda Delgado u16 aux; 36729d58642SRicardo Ribalda Delgado 368f1232ac2SJi-Ze Hong (Peter Hong) sio_write_reg(pdata, LDN, k); 369f1232ac2SJi-Ze Hong (Peter Hong) aux = sio_read_reg(pdata, IO_ADDR1); 370f1232ac2SJi-Ze Hong (Peter Hong) aux |= sio_read_reg(pdata, IO_ADDR2) << 8; 371fab8a02bSLukas Redlinger if (aux != uart->port.iobase) 37229d58642SRicardo Ribalda Delgado continue; 37329d58642SRicardo Ribalda Delgado 374fa01e2caSRicardo Ribalda Delgado pdata->index = k; 375fa01e2caSRicardo Ribalda Delgado 376fab8a02bSLukas Redlinger irq_data = irq_get_irq_data(uart->port.irq); 37706e39572SJi-Ze Hong (Peter Hong) if (irq_data) 37806e39572SJi-Ze Hong (Peter Hong) level_mode = 37906e39572SJi-Ze Hong (Peter Hong) irqd_is_level_type(irq_data); 38006e39572SJi-Ze Hong (Peter Hong) 38106e39572SJi-Ze Hong (Peter Hong) fintek_8250_set_irq_mode(pdata, level_mode); 382c2236facSJi-Ze Hong (Peter Hong) fintek_8250_set_max_fifo(pdata); 383fab8a02bSLukas Redlinger fintek_8250_goto_highspeed(uart, pdata); 384fab8a02bSLukas Redlinger 38506e39572SJi-Ze Hong (Peter Hong) fintek_8250_exit_key(addr[i]); 38606e39572SJi-Ze Hong (Peter Hong) 387fa01e2caSRicardo Ribalda Delgado return 0; 388017bec38SRicardo Ribalda Delgado } 389fa01e2caSRicardo Ribalda Delgado 39029d58642SRicardo Ribalda Delgado fintek_8250_exit_key(addr[i]); 391ce8c267eSRicardo Ribalda Delgado } 392ce8c267eSRicardo Ribalda Delgado } 393017bec38SRicardo Ribalda Delgado 394017bec38SRicardo Ribalda Delgado return -ENODEV; 395017bec38SRicardo Ribalda Delgado } 396017bec38SRicardo Ribalda Delgado 3971e26c472SJi-Ze Hong (Peter Hong) static void fintek_8250_set_rs485_handler(struct uart_8250_port *uart) 3981e26c472SJi-Ze Hong (Peter Hong) { 3991e26c472SJi-Ze Hong (Peter Hong) struct fintek_8250 *pdata = uart->port.private_data; 4001e26c472SJi-Ze Hong (Peter Hong) 4011e26c472SJi-Ze Hong (Peter Hong) switch (pdata->pid) { 4021e26c472SJi-Ze Hong (Peter Hong) case CHIP_ID_F81216AD: 4031e26c472SJi-Ze Hong (Peter Hong) case CHIP_ID_F81216H: 404da60d6afSJi-Ze Hong (Peter Hong) case CHIP_ID_F81866: 405de48b099SJi-Ze Hong (Peter Hong) case CHIP_ID_F81865: 4061e26c472SJi-Ze Hong (Peter Hong) uart->port.rs485_config = fintek_8250_rs485_config; 4071e26c472SJi-Ze Hong (Peter Hong) break; 4081e26c472SJi-Ze Hong (Peter Hong) 4091e26c472SJi-Ze Hong (Peter Hong) default: /* No RS485 Auto direction functional */ 4101e26c472SJi-Ze Hong (Peter Hong) break; 4111e26c472SJi-Ze Hong (Peter Hong) } 4121e26c472SJi-Ze Hong (Peter Hong) } 4131e26c472SJi-Ze Hong (Peter Hong) 414fa01e2caSRicardo Ribalda Delgado int fintek_8250_probe(struct uart_8250_port *uart) 41528e3fb6cSRicardo Ribalda Delgado { 41692a5f11aSRicardo Ribalda Delgado struct fintek_8250 *pdata; 417fa01e2caSRicardo Ribalda Delgado struct fintek_8250 probe_data; 41828e3fb6cSRicardo Ribalda Delgado 419fab8a02bSLukas Redlinger if (probe_setup_port(&probe_data, uart)) 42028e3fb6cSRicardo Ribalda Delgado return -ENODEV; 42128e3fb6cSRicardo Ribalda Delgado 422fa01e2caSRicardo Ribalda Delgado pdata = devm_kzalloc(uart->port.dev, sizeof(*pdata), GFP_KERNEL); 42392a5f11aSRicardo Ribalda Delgado if (!pdata) 42492a5f11aSRicardo Ribalda Delgado return -ENOMEM; 42592a5f11aSRicardo Ribalda Delgado 426fa01e2caSRicardo Ribalda Delgado memcpy(pdata, &probe_data, sizeof(probe_data)); 427fa01e2caSRicardo Ribalda Delgado uart->port.private_data = pdata; 4281e26c472SJi-Ze Hong (Peter Hong) fintek_8250_set_rs485_handler(uart); 429*195638b6SJi-Ze Hong (Peter Hong) fintek_8250_set_termios_handler(uart); 43028e3fb6cSRicardo Ribalda Delgado 43106e39572SJi-Ze Hong (Peter Hong) return 0; 43228e3fb6cSRicardo Ribalda Delgado } 433