1 /* 2 * Probe for F81216A LPC to 4 UART 3 * 4 * Copyright (C) 2014-2016 Ricardo Ribalda, Qtechnology A/S 5 * 6 * 7 * This program is free software; you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License as published by 9 * the Free Software Foundation; either version 2 of the License. 10 */ 11 #include <linux/module.h> 12 #include <linux/pci.h> 13 #include <linux/pnp.h> 14 #include <linux/kernel.h> 15 #include <linux/serial_core.h> 16 #include <linux/irq.h> 17 #include "8250.h" 18 19 #define ADDR_PORT 0 20 #define DATA_PORT 1 21 #define EXIT_KEY 0xAA 22 #define CHIP_ID1 0x20 23 #define CHIP_ID2 0x21 24 #define CHIP_ID_F81865 0x0407 25 #define CHIP_ID_F81866 0x1010 26 #define CHIP_ID_F81216AD 0x1602 27 #define CHIP_ID_F81216H 0x0501 28 #define CHIP_ID_F81216 0x0802 29 #define VENDOR_ID1 0x23 30 #define VENDOR_ID1_VAL 0x19 31 #define VENDOR_ID2 0x24 32 #define VENDOR_ID2_VAL 0x34 33 #define IO_ADDR1 0x61 34 #define IO_ADDR2 0x60 35 #define LDN 0x7 36 37 #define FINTEK_IRQ_MODE 0x70 38 #define IRQ_SHARE BIT(4) 39 #define IRQ_MODE_MASK (BIT(6) | BIT(5)) 40 #define IRQ_LEVEL_LOW 0 41 #define IRQ_EDGE_HIGH BIT(5) 42 43 #define RS485 0xF0 44 #define RTS_INVERT BIT(5) 45 #define RS485_URA BIT(4) 46 #define RXW4C_IRA BIT(3) 47 #define TXW4C_IRA BIT(2) 48 49 #define FIFO_CTRL 0xF6 50 #define FIFO_MODE_MASK (BIT(1) | BIT(0)) 51 #define FIFO_MODE_128 (BIT(1) | BIT(0)) 52 #define RXFTHR_MODE_MASK (BIT(5) | BIT(4)) 53 #define RXFTHR_MODE_4X BIT(5) 54 55 #define F81216_LDN_LOW 0x0 56 #define F81216_LDN_HIGH 0x4 57 58 /* 59 * F81866 registers 60 * 61 * The IRQ setting mode of F81866 is not the same with F81216 series. 62 * Level/Low: IRQ_MODE0:0, IRQ_MODE1:0 63 * Edge/High: IRQ_MODE0:1, IRQ_MODE1:0 64 * 65 * Clock speeds for UART (register F2h) 66 * 00: 1.8432MHz. 67 * 01: 18.432MHz. 68 * 10: 24MHz. 69 * 11: 14.769MHz. 70 */ 71 #define F81866_IRQ_MODE 0xf0 72 #define F81866_IRQ_SHARE BIT(0) 73 #define F81866_IRQ_MODE0 BIT(1) 74 75 #define F81866_FIFO_CTRL FIFO_CTRL 76 #define F81866_IRQ_MODE1 BIT(3) 77 78 #define F81866_LDN_LOW 0x10 79 #define F81866_LDN_HIGH 0x16 80 81 #define F81866_UART_CLK 0xF2 82 #define F81866_UART_CLK_MASK (BIT(1) | BIT(0)) 83 #define F81866_UART_CLK_1_8432MHZ 0 84 #define F81866_UART_CLK_14_769MHZ (BIT(1) | BIT(0)) 85 #define F81866_UART_CLK_18_432MHZ BIT(0) 86 #define F81866_UART_CLK_24MHZ BIT(1) 87 88 struct fintek_8250 { 89 u16 pid; 90 u16 base_port; 91 u8 index; 92 u8 key; 93 }; 94 95 static u8 sio_read_reg(struct fintek_8250 *pdata, u8 reg) 96 { 97 outb(reg, pdata->base_port + ADDR_PORT); 98 return inb(pdata->base_port + DATA_PORT); 99 } 100 101 static void sio_write_reg(struct fintek_8250 *pdata, u8 reg, u8 data) 102 { 103 outb(reg, pdata->base_port + ADDR_PORT); 104 outb(data, pdata->base_port + DATA_PORT); 105 } 106 107 static void sio_write_mask_reg(struct fintek_8250 *pdata, u8 reg, u8 mask, 108 u8 data) 109 { 110 u8 tmp; 111 112 tmp = (sio_read_reg(pdata, reg) & ~mask) | (mask & data); 113 sio_write_reg(pdata, reg, tmp); 114 } 115 116 static int fintek_8250_enter_key(u16 base_port, u8 key) 117 { 118 if (!request_muxed_region(base_port, 2, "8250_fintek")) 119 return -EBUSY; 120 121 outb(key, base_port + ADDR_PORT); 122 outb(key, base_port + ADDR_PORT); 123 return 0; 124 } 125 126 static void fintek_8250_exit_key(u16 base_port) 127 { 128 129 outb(EXIT_KEY, base_port + ADDR_PORT); 130 release_region(base_port + ADDR_PORT, 2); 131 } 132 133 static int fintek_8250_check_id(struct fintek_8250 *pdata) 134 { 135 u16 chip; 136 137 if (sio_read_reg(pdata, VENDOR_ID1) != VENDOR_ID1_VAL) 138 return -ENODEV; 139 140 if (sio_read_reg(pdata, VENDOR_ID2) != VENDOR_ID2_VAL) 141 return -ENODEV; 142 143 chip = sio_read_reg(pdata, CHIP_ID1); 144 chip |= sio_read_reg(pdata, CHIP_ID2) << 8; 145 146 switch (chip) { 147 case CHIP_ID_F81865: 148 case CHIP_ID_F81866: 149 case CHIP_ID_F81216AD: 150 case CHIP_ID_F81216H: 151 case CHIP_ID_F81216: 152 break; 153 default: 154 return -ENODEV; 155 } 156 157 pdata->pid = chip; 158 return 0; 159 } 160 161 static int fintek_8250_get_ldn_range(struct fintek_8250 *pdata, int *min, 162 int *max) 163 { 164 switch (pdata->pid) { 165 case CHIP_ID_F81865: 166 case CHIP_ID_F81866: 167 *min = F81866_LDN_LOW; 168 *max = F81866_LDN_HIGH; 169 return 0; 170 171 case CHIP_ID_F81216AD: 172 case CHIP_ID_F81216H: 173 case CHIP_ID_F81216: 174 *min = F81216_LDN_LOW; 175 *max = F81216_LDN_HIGH; 176 return 0; 177 } 178 179 return -ENODEV; 180 } 181 182 static int fintek_8250_rs485_config(struct uart_port *port, 183 struct serial_rs485 *rs485) 184 { 185 uint8_t config = 0; 186 struct fintek_8250 *pdata = port->private_data; 187 188 if (!pdata) 189 return -EINVAL; 190 191 if (rs485->flags & SER_RS485_ENABLED) 192 memset(rs485->padding, 0, sizeof(rs485->padding)); 193 else 194 memset(rs485, 0, sizeof(*rs485)); 195 196 rs485->flags &= SER_RS485_ENABLED | SER_RS485_RTS_ON_SEND | 197 SER_RS485_RTS_AFTER_SEND; 198 199 if (rs485->delay_rts_before_send) { 200 rs485->delay_rts_before_send = 1; 201 config |= TXW4C_IRA; 202 } 203 204 if (rs485->delay_rts_after_send) { 205 rs485->delay_rts_after_send = 1; 206 config |= RXW4C_IRA; 207 } 208 209 if ((!!(rs485->flags & SER_RS485_RTS_ON_SEND)) == 210 (!!(rs485->flags & SER_RS485_RTS_AFTER_SEND))) 211 rs485->flags &= SER_RS485_ENABLED; 212 else 213 config |= RS485_URA; 214 215 if (rs485->flags & SER_RS485_RTS_ON_SEND) 216 config |= RTS_INVERT; 217 218 if (fintek_8250_enter_key(pdata->base_port, pdata->key)) 219 return -EBUSY; 220 221 sio_write_reg(pdata, LDN, pdata->index); 222 sio_write_reg(pdata, RS485, config); 223 fintek_8250_exit_key(pdata->base_port); 224 225 port->rs485 = *rs485; 226 227 return 0; 228 } 229 230 static void fintek_8250_set_irq_mode(struct fintek_8250 *pdata, bool is_level) 231 { 232 sio_write_reg(pdata, LDN, pdata->index); 233 234 switch (pdata->pid) { 235 case CHIP_ID_F81866: 236 sio_write_mask_reg(pdata, F81866_FIFO_CTRL, F81866_IRQ_MODE1, 237 0); 238 /* fall through */ 239 case CHIP_ID_F81865: 240 sio_write_mask_reg(pdata, F81866_IRQ_MODE, F81866_IRQ_SHARE, 241 F81866_IRQ_SHARE); 242 sio_write_mask_reg(pdata, F81866_IRQ_MODE, F81866_IRQ_MODE0, 243 is_level ? 0 : F81866_IRQ_MODE0); 244 break; 245 246 case CHIP_ID_F81216AD: 247 case CHIP_ID_F81216H: 248 case CHIP_ID_F81216: 249 sio_write_mask_reg(pdata, FINTEK_IRQ_MODE, IRQ_SHARE, 250 IRQ_SHARE); 251 sio_write_mask_reg(pdata, FINTEK_IRQ_MODE, IRQ_MODE_MASK, 252 is_level ? IRQ_LEVEL_LOW : IRQ_EDGE_HIGH); 253 break; 254 } 255 } 256 257 static void fintek_8250_set_max_fifo(struct fintek_8250 *pdata) 258 { 259 switch (pdata->pid) { 260 case CHIP_ID_F81216H: /* 128Bytes FIFO */ 261 case CHIP_ID_F81866: 262 sio_write_mask_reg(pdata, FIFO_CTRL, 263 FIFO_MODE_MASK | RXFTHR_MODE_MASK, 264 FIFO_MODE_128 | RXFTHR_MODE_4X); 265 break; 266 267 default: /* Default 16Bytes FIFO */ 268 break; 269 } 270 } 271 272 static void fintek_8250_goto_highspeed(struct uart_8250_port *uart, 273 struct fintek_8250 *pdata) 274 { 275 sio_write_reg(pdata, LDN, pdata->index); 276 277 switch (pdata->pid) { 278 case CHIP_ID_F81866: /* set uart clock for high speed serial mode */ 279 sio_write_mask_reg(pdata, F81866_UART_CLK, 280 F81866_UART_CLK_MASK, 281 F81866_UART_CLK_14_769MHZ); 282 283 uart->port.uartclk = 921600 * 16; 284 break; 285 default: /* leave clock speed untouched */ 286 break; 287 } 288 } 289 290 static int probe_setup_port(struct fintek_8250 *pdata, 291 struct uart_8250_port *uart) 292 { 293 static const u16 addr[] = {0x4e, 0x2e}; 294 static const u8 keys[] = {0x77, 0xa0, 0x87, 0x67}; 295 struct irq_data *irq_data; 296 bool level_mode = false; 297 int i, j, k, min, max; 298 299 for (i = 0; i < ARRAY_SIZE(addr); i++) { 300 for (j = 0; j < ARRAY_SIZE(keys); j++) { 301 pdata->base_port = addr[i]; 302 pdata->key = keys[j]; 303 304 if (fintek_8250_enter_key(addr[i], keys[j])) 305 continue; 306 if (fintek_8250_check_id(pdata) || 307 fintek_8250_get_ldn_range(pdata, &min, &max)) { 308 fintek_8250_exit_key(addr[i]); 309 continue; 310 } 311 312 for (k = min; k < max; k++) { 313 u16 aux; 314 315 sio_write_reg(pdata, LDN, k); 316 aux = sio_read_reg(pdata, IO_ADDR1); 317 aux |= sio_read_reg(pdata, IO_ADDR2) << 8; 318 if (aux != uart->port.iobase) 319 continue; 320 321 pdata->index = k; 322 323 irq_data = irq_get_irq_data(uart->port.irq); 324 if (irq_data) 325 level_mode = 326 irqd_is_level_type(irq_data); 327 328 fintek_8250_set_irq_mode(pdata, level_mode); 329 fintek_8250_set_max_fifo(pdata); 330 fintek_8250_goto_highspeed(uart, pdata); 331 332 fintek_8250_exit_key(addr[i]); 333 334 return 0; 335 } 336 337 fintek_8250_exit_key(addr[i]); 338 } 339 } 340 341 return -ENODEV; 342 } 343 344 static void fintek_8250_set_rs485_handler(struct uart_8250_port *uart) 345 { 346 struct fintek_8250 *pdata = uart->port.private_data; 347 348 switch (pdata->pid) { 349 case CHIP_ID_F81216AD: 350 case CHIP_ID_F81216H: 351 case CHIP_ID_F81866: 352 case CHIP_ID_F81865: 353 uart->port.rs485_config = fintek_8250_rs485_config; 354 break; 355 356 default: /* No RS485 Auto direction functional */ 357 break; 358 } 359 } 360 361 int fintek_8250_probe(struct uart_8250_port *uart) 362 { 363 struct fintek_8250 *pdata; 364 struct fintek_8250 probe_data; 365 366 if (probe_setup_port(&probe_data, uart)) 367 return -ENODEV; 368 369 pdata = devm_kzalloc(uart->port.dev, sizeof(*pdata), GFP_KERNEL); 370 if (!pdata) 371 return -ENOMEM; 372 373 memcpy(pdata, &probe_data, sizeof(probe_data)); 374 uart->port.private_data = pdata; 375 fintek_8250_set_rs485_handler(uart); 376 377 return 0; 378 } 379