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 24c2236facSJi-Ze Hong (Peter Hong) #define CHIP_ID_F81216AD 0x1602 25c2236facSJi-Ze Hong (Peter Hong) #define CHIP_ID_F81216H 0x0501 26*1e26c472SJi-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) 4128e3fb6cSRicardo Ribalda Delgado #define RS485 0xF0 4228e3fb6cSRicardo Ribalda Delgado #define RTS_INVERT BIT(5) 4328e3fb6cSRicardo Ribalda Delgado #define RS485_URA BIT(4) 4428e3fb6cSRicardo Ribalda Delgado #define RXW4C_IRA BIT(3) 4528e3fb6cSRicardo Ribalda Delgado #define TXW4C_IRA BIT(2) 4628e3fb6cSRicardo Ribalda Delgado 47c2236facSJi-Ze Hong (Peter Hong) #define FIFO_CTRL 0xF6 48c2236facSJi-Ze Hong (Peter Hong) #define FIFO_MODE_MASK (BIT(1) | BIT(0)) 49c2236facSJi-Ze Hong (Peter Hong) #define FIFO_MODE_128 (BIT(1) | BIT(0)) 50c2236facSJi-Ze Hong (Peter Hong) #define RXFTHR_MODE_MASK (BIT(5) | BIT(4)) 51c2236facSJi-Ze Hong (Peter Hong) #define RXFTHR_MODE_4X BIT(5) 52c2236facSJi-Ze Hong (Peter Hong) 5392a5f11aSRicardo Ribalda Delgado struct fintek_8250 { 54c2236facSJi-Ze Hong (Peter Hong) u16 pid; 55017bec38SRicardo Ribalda Delgado u16 base_port; 5692a5f11aSRicardo Ribalda Delgado u8 index; 57ce8c267eSRicardo Ribalda Delgado u8 key; 5892a5f11aSRicardo Ribalda Delgado }; 5992a5f11aSRicardo Ribalda Delgado 60f1232ac2SJi-Ze Hong (Peter Hong) static u8 sio_read_reg(struct fintek_8250 *pdata, u8 reg) 61f1232ac2SJi-Ze Hong (Peter Hong) { 62f1232ac2SJi-Ze Hong (Peter Hong) outb(reg, pdata->base_port + ADDR_PORT); 63f1232ac2SJi-Ze Hong (Peter Hong) return inb(pdata->base_port + DATA_PORT); 64f1232ac2SJi-Ze Hong (Peter Hong) } 65f1232ac2SJi-Ze Hong (Peter Hong) 66f1232ac2SJi-Ze Hong (Peter Hong) static void sio_write_reg(struct fintek_8250 *pdata, u8 reg, u8 data) 67f1232ac2SJi-Ze Hong (Peter Hong) { 68f1232ac2SJi-Ze Hong (Peter Hong) outb(reg, pdata->base_port + ADDR_PORT); 69f1232ac2SJi-Ze Hong (Peter Hong) outb(data, pdata->base_port + DATA_PORT); 70f1232ac2SJi-Ze Hong (Peter Hong) } 71f1232ac2SJi-Ze Hong (Peter Hong) 72f1232ac2SJi-Ze Hong (Peter Hong) static void sio_write_mask_reg(struct fintek_8250 *pdata, u8 reg, u8 mask, 73f1232ac2SJi-Ze Hong (Peter Hong) u8 data) 74f1232ac2SJi-Ze Hong (Peter Hong) { 75f1232ac2SJi-Ze Hong (Peter Hong) u8 tmp; 76f1232ac2SJi-Ze Hong (Peter Hong) 77f1232ac2SJi-Ze Hong (Peter Hong) tmp = (sio_read_reg(pdata, reg) & ~mask) | (mask & data); 78f1232ac2SJi-Ze Hong (Peter Hong) sio_write_reg(pdata, reg, tmp); 79f1232ac2SJi-Ze Hong (Peter Hong) } 80f1232ac2SJi-Ze Hong (Peter Hong) 81ce8c267eSRicardo Ribalda Delgado static int fintek_8250_enter_key(u16 base_port, u8 key) 82017bec38SRicardo Ribalda Delgado { 83fa01e2caSRicardo Ribalda Delgado if (!request_muxed_region(base_port, 2, "8250_fintek")) 8428e3fb6cSRicardo Ribalda Delgado return -EBUSY; 8528e3fb6cSRicardo Ribalda Delgado 86ce8c267eSRicardo Ribalda Delgado outb(key, base_port + ADDR_PORT); 87ce8c267eSRicardo Ribalda Delgado outb(key, base_port + ADDR_PORT); 8828e3fb6cSRicardo Ribalda Delgado return 0; 8928e3fb6cSRicardo Ribalda Delgado } 9028e3fb6cSRicardo Ribalda Delgado 91017bec38SRicardo Ribalda Delgado static void fintek_8250_exit_key(u16 base_port) 92017bec38SRicardo Ribalda Delgado { 9328e3fb6cSRicardo Ribalda Delgado 94017bec38SRicardo Ribalda Delgado outb(EXIT_KEY, base_port + ADDR_PORT); 95017bec38SRicardo Ribalda Delgado release_region(base_port + ADDR_PORT, 2); 9628e3fb6cSRicardo Ribalda Delgado } 9728e3fb6cSRicardo Ribalda Delgado 98f1232ac2SJi-Ze Hong (Peter Hong) static int fintek_8250_check_id(struct fintek_8250 *pdata) 9928e3fb6cSRicardo Ribalda Delgado { 100dae77f75SRicardo Ribalda Delgado u16 chip; 10128e3fb6cSRicardo Ribalda Delgado 102f1232ac2SJi-Ze Hong (Peter Hong) if (sio_read_reg(pdata, VENDOR_ID1) != VENDOR_ID1_VAL) 10328e3fb6cSRicardo Ribalda Delgado return -ENODEV; 10428e3fb6cSRicardo Ribalda Delgado 105f1232ac2SJi-Ze Hong (Peter Hong) if (sio_read_reg(pdata, VENDOR_ID2) != VENDOR_ID2_VAL) 10628e3fb6cSRicardo Ribalda Delgado return -ENODEV; 10728e3fb6cSRicardo Ribalda Delgado 108f1232ac2SJi-Ze Hong (Peter Hong) chip = sio_read_reg(pdata, CHIP_ID1); 109f1232ac2SJi-Ze Hong (Peter Hong) chip |= sio_read_reg(pdata, CHIP_ID2) << 8; 110dae77f75SRicardo Ribalda Delgado 111*1e26c472SJi-Ze Hong (Peter Hong) switch (chip) { 112*1e26c472SJi-Ze Hong (Peter Hong) case CHIP_ID_F81216AD: 113*1e26c472SJi-Ze Hong (Peter Hong) case CHIP_ID_F81216H: 114*1e26c472SJi-Ze Hong (Peter Hong) case CHIP_ID_F81216: 115*1e26c472SJi-Ze Hong (Peter Hong) break; 116*1e26c472SJi-Ze Hong (Peter Hong) default: 117dae77f75SRicardo Ribalda Delgado return -ENODEV; 118*1e26c472SJi-Ze Hong (Peter Hong) } 119dae77f75SRicardo Ribalda Delgado 120c2236facSJi-Ze Hong (Peter Hong) pdata->pid = chip; 12128e3fb6cSRicardo Ribalda Delgado return 0; 12228e3fb6cSRicardo Ribalda Delgado } 12328e3fb6cSRicardo Ribalda Delgado 12441e69093SRicardo Ribalda Delgado static int fintek_8250_rs485_config(struct uart_port *port, 12528e3fb6cSRicardo Ribalda Delgado struct serial_rs485 *rs485) 12628e3fb6cSRicardo Ribalda Delgado { 12728e3fb6cSRicardo Ribalda Delgado uint8_t config = 0; 12892a5f11aSRicardo Ribalda Delgado struct fintek_8250 *pdata = port->private_data; 12928e3fb6cSRicardo Ribalda Delgado 13092a5f11aSRicardo Ribalda Delgado if (!pdata) 13128e3fb6cSRicardo Ribalda Delgado return -EINVAL; 13228e3fb6cSRicardo Ribalda Delgado 13328e3fb6cSRicardo Ribalda Delgado if (rs485->flags & SER_RS485_ENABLED) 13428e3fb6cSRicardo Ribalda Delgado memset(rs485->padding, 0, sizeof(rs485->padding)); 13528e3fb6cSRicardo Ribalda Delgado else 13628e3fb6cSRicardo Ribalda Delgado memset(rs485, 0, sizeof(*rs485)); 13728e3fb6cSRicardo Ribalda Delgado 13828e3fb6cSRicardo Ribalda Delgado rs485->flags &= SER_RS485_ENABLED | SER_RS485_RTS_ON_SEND | 13928e3fb6cSRicardo Ribalda Delgado SER_RS485_RTS_AFTER_SEND; 14028e3fb6cSRicardo Ribalda Delgado 14128e3fb6cSRicardo Ribalda Delgado if (rs485->delay_rts_before_send) { 14228e3fb6cSRicardo Ribalda Delgado rs485->delay_rts_before_send = 1; 14328e3fb6cSRicardo Ribalda Delgado config |= TXW4C_IRA; 14428e3fb6cSRicardo Ribalda Delgado } 14528e3fb6cSRicardo Ribalda Delgado 14628e3fb6cSRicardo Ribalda Delgado if (rs485->delay_rts_after_send) { 14728e3fb6cSRicardo Ribalda Delgado rs485->delay_rts_after_send = 1; 14828e3fb6cSRicardo Ribalda Delgado config |= RXW4C_IRA; 14928e3fb6cSRicardo Ribalda Delgado } 15028e3fb6cSRicardo Ribalda Delgado 15128e3fb6cSRicardo Ribalda Delgado if ((!!(rs485->flags & SER_RS485_RTS_ON_SEND)) == 15228e3fb6cSRicardo Ribalda Delgado (!!(rs485->flags & SER_RS485_RTS_AFTER_SEND))) 15328e3fb6cSRicardo Ribalda Delgado rs485->flags &= SER_RS485_ENABLED; 15428e3fb6cSRicardo Ribalda Delgado else 15528e3fb6cSRicardo Ribalda Delgado config |= RS485_URA; 15628e3fb6cSRicardo Ribalda Delgado 15728e3fb6cSRicardo Ribalda Delgado if (rs485->flags & SER_RS485_RTS_ON_SEND) 15828e3fb6cSRicardo Ribalda Delgado config |= RTS_INVERT; 15928e3fb6cSRicardo Ribalda Delgado 160ce8c267eSRicardo Ribalda Delgado if (fintek_8250_enter_key(pdata->base_port, pdata->key)) 16128e3fb6cSRicardo Ribalda Delgado return -EBUSY; 16228e3fb6cSRicardo Ribalda Delgado 163f1232ac2SJi-Ze Hong (Peter Hong) sio_write_reg(pdata, LDN, pdata->index); 164f1232ac2SJi-Ze Hong (Peter Hong) sio_write_reg(pdata, RS485, config); 165017bec38SRicardo Ribalda Delgado fintek_8250_exit_key(pdata->base_port); 16628e3fb6cSRicardo Ribalda Delgado 16741e69093SRicardo Ribalda Delgado port->rs485 = *rs485; 16841e69093SRicardo Ribalda Delgado 16928e3fb6cSRicardo Ribalda Delgado return 0; 17028e3fb6cSRicardo Ribalda Delgado } 17128e3fb6cSRicardo Ribalda Delgado 17206e39572SJi-Ze Hong (Peter Hong) static void fintek_8250_set_irq_mode(struct fintek_8250 *pdata, bool is_level) 17306e39572SJi-Ze Hong (Peter Hong) { 17406e39572SJi-Ze Hong (Peter Hong) sio_write_reg(pdata, LDN, pdata->index); 17506e39572SJi-Ze Hong (Peter Hong) sio_write_mask_reg(pdata, FINTEK_IRQ_MODE, IRQ_SHARE, IRQ_SHARE); 17606e39572SJi-Ze Hong (Peter Hong) sio_write_mask_reg(pdata, FINTEK_IRQ_MODE, IRQ_MODE_MASK, 17706e39572SJi-Ze Hong (Peter Hong) is_level ? IRQ_LEVEL_LOW : IRQ_EDGE_HIGH); 17806e39572SJi-Ze Hong (Peter Hong) } 17906e39572SJi-Ze Hong (Peter Hong) 180c2236facSJi-Ze Hong (Peter Hong) static void fintek_8250_set_max_fifo(struct fintek_8250 *pdata) 181c2236facSJi-Ze Hong (Peter Hong) { 182c2236facSJi-Ze Hong (Peter Hong) switch (pdata->pid) { 183c2236facSJi-Ze Hong (Peter Hong) case CHIP_ID_F81216H: /* 128Bytes FIFO */ 184c2236facSJi-Ze Hong (Peter Hong) sio_write_mask_reg(pdata, FIFO_CTRL, 185c2236facSJi-Ze Hong (Peter Hong) FIFO_MODE_MASK | RXFTHR_MODE_MASK, 186c2236facSJi-Ze Hong (Peter Hong) FIFO_MODE_128 | RXFTHR_MODE_4X); 187c2236facSJi-Ze Hong (Peter Hong) break; 188c2236facSJi-Ze Hong (Peter Hong) 189c2236facSJi-Ze Hong (Peter Hong) default: /* Default 16Bytes FIFO */ 190c2236facSJi-Ze Hong (Peter Hong) break; 191c2236facSJi-Ze Hong (Peter Hong) } 192c2236facSJi-Ze Hong (Peter Hong) } 193c2236facSJi-Ze Hong (Peter Hong) 19406e39572SJi-Ze Hong (Peter Hong) static int probe_setup_port(struct fintek_8250 *pdata, u16 io_address, 19506e39572SJi-Ze Hong (Peter Hong) unsigned int irq) 196017bec38SRicardo Ribalda Delgado { 197017bec38SRicardo Ribalda Delgado static const u16 addr[] = {0x4e, 0x2e}; 198ce8c267eSRicardo Ribalda Delgado static const u8 keys[] = {0x77, 0xa0, 0x87, 0x67}; 19906e39572SJi-Ze Hong (Peter Hong) struct irq_data *irq_data; 20006e39572SJi-Ze Hong (Peter Hong) bool level_mode = false; 20129d58642SRicardo Ribalda Delgado int i, j, k; 202017bec38SRicardo Ribalda Delgado 203017bec38SRicardo Ribalda Delgado for (i = 0; i < ARRAY_SIZE(addr); i++) { 204ce8c267eSRicardo Ribalda Delgado for (j = 0; j < ARRAY_SIZE(keys); j++) { 205f1232ac2SJi-Ze Hong (Peter Hong) pdata->base_port = addr[i]; 206f1232ac2SJi-Ze Hong (Peter Hong) pdata->key = keys[j]; 207017bec38SRicardo Ribalda Delgado 208ce8c267eSRicardo Ribalda Delgado if (fintek_8250_enter_key(addr[i], keys[j])) 209017bec38SRicardo Ribalda Delgado continue; 210f1232ac2SJi-Ze Hong (Peter Hong) if (fintek_8250_check_id(pdata)) { 211017bec38SRicardo Ribalda Delgado fintek_8250_exit_key(addr[i]); 21229d58642SRicardo Ribalda Delgado continue; 21329d58642SRicardo Ribalda Delgado } 21429d58642SRicardo Ribalda Delgado 21529d58642SRicardo Ribalda Delgado for (k = 0; k < 4; k++) { 21629d58642SRicardo Ribalda Delgado u16 aux; 21729d58642SRicardo Ribalda Delgado 218f1232ac2SJi-Ze Hong (Peter Hong) sio_write_reg(pdata, LDN, k); 219f1232ac2SJi-Ze Hong (Peter Hong) aux = sio_read_reg(pdata, IO_ADDR1); 220f1232ac2SJi-Ze Hong (Peter Hong) aux |= sio_read_reg(pdata, IO_ADDR2) << 8; 22129d58642SRicardo Ribalda Delgado if (aux != io_address) 22229d58642SRicardo Ribalda Delgado continue; 22329d58642SRicardo Ribalda Delgado 224fa01e2caSRicardo Ribalda Delgado pdata->index = k; 225fa01e2caSRicardo Ribalda Delgado 22606e39572SJi-Ze Hong (Peter Hong) irq_data = irq_get_irq_data(irq); 22706e39572SJi-Ze Hong (Peter Hong) if (irq_data) 22806e39572SJi-Ze Hong (Peter Hong) level_mode = 22906e39572SJi-Ze Hong (Peter Hong) irqd_is_level_type(irq_data); 23006e39572SJi-Ze Hong (Peter Hong) 23106e39572SJi-Ze Hong (Peter Hong) fintek_8250_set_irq_mode(pdata, level_mode); 232c2236facSJi-Ze Hong (Peter Hong) fintek_8250_set_max_fifo(pdata); 23306e39572SJi-Ze Hong (Peter Hong) fintek_8250_exit_key(addr[i]); 23406e39572SJi-Ze Hong (Peter Hong) 235fa01e2caSRicardo Ribalda Delgado return 0; 236017bec38SRicardo Ribalda Delgado } 237fa01e2caSRicardo Ribalda Delgado 23829d58642SRicardo Ribalda Delgado fintek_8250_exit_key(addr[i]); 239ce8c267eSRicardo Ribalda Delgado } 240ce8c267eSRicardo Ribalda Delgado } 241017bec38SRicardo Ribalda Delgado 242017bec38SRicardo Ribalda Delgado return -ENODEV; 243017bec38SRicardo Ribalda Delgado } 244017bec38SRicardo Ribalda Delgado 245*1e26c472SJi-Ze Hong (Peter Hong) static void fintek_8250_set_rs485_handler(struct uart_8250_port *uart) 246*1e26c472SJi-Ze Hong (Peter Hong) { 247*1e26c472SJi-Ze Hong (Peter Hong) struct fintek_8250 *pdata = uart->port.private_data; 248*1e26c472SJi-Ze Hong (Peter Hong) 249*1e26c472SJi-Ze Hong (Peter Hong) switch (pdata->pid) { 250*1e26c472SJi-Ze Hong (Peter Hong) case CHIP_ID_F81216AD: 251*1e26c472SJi-Ze Hong (Peter Hong) case CHIP_ID_F81216H: 252*1e26c472SJi-Ze Hong (Peter Hong) uart->port.rs485_config = fintek_8250_rs485_config; 253*1e26c472SJi-Ze Hong (Peter Hong) break; 254*1e26c472SJi-Ze Hong (Peter Hong) 255*1e26c472SJi-Ze Hong (Peter Hong) default: /* No RS485 Auto direction functional */ 256*1e26c472SJi-Ze Hong (Peter Hong) break; 257*1e26c472SJi-Ze Hong (Peter Hong) } 258*1e26c472SJi-Ze Hong (Peter Hong) } 259*1e26c472SJi-Ze Hong (Peter Hong) 260fa01e2caSRicardo Ribalda Delgado int fintek_8250_probe(struct uart_8250_port *uart) 26128e3fb6cSRicardo Ribalda Delgado { 26292a5f11aSRicardo Ribalda Delgado struct fintek_8250 *pdata; 263fa01e2caSRicardo Ribalda Delgado struct fintek_8250 probe_data; 26428e3fb6cSRicardo Ribalda Delgado 26506e39572SJi-Ze Hong (Peter Hong) if (probe_setup_port(&probe_data, uart->port.iobase, uart->port.irq)) 26628e3fb6cSRicardo Ribalda Delgado return -ENODEV; 26728e3fb6cSRicardo Ribalda Delgado 268fa01e2caSRicardo Ribalda Delgado pdata = devm_kzalloc(uart->port.dev, sizeof(*pdata), GFP_KERNEL); 26992a5f11aSRicardo Ribalda Delgado if (!pdata) 27092a5f11aSRicardo Ribalda Delgado return -ENOMEM; 27192a5f11aSRicardo Ribalda Delgado 272fa01e2caSRicardo Ribalda Delgado memcpy(pdata, &probe_data, sizeof(probe_data)); 273fa01e2caSRicardo Ribalda Delgado uart->port.private_data = pdata; 274*1e26c472SJi-Ze Hong (Peter Hong) fintek_8250_set_rs485_handler(uart); 27528e3fb6cSRicardo Ribalda Delgado 27606e39572SJi-Ze Hong (Peter Hong) return 0; 27728e3fb6cSRicardo Ribalda Delgado } 278