2014-08-01 03:22:26 +08:00
|
|
|
/*
|
|
|
|
* Probe for F81216A LPC to 4 UART
|
|
|
|
*
|
2016-04-27 16:40:10 +08:00
|
|
|
* Copyright (C) 2014-2016 Ricardo Ribalda, Qtechnology A/S
|
2014-08-01 03:22:26 +08:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License.
|
|
|
|
*/
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/pci.h>
|
|
|
|
#include <linux/pnp.h>
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/serial_core.h>
|
2016-05-27 10:02:51 +08:00
|
|
|
#include <linux/irq.h>
|
2014-08-01 03:22:26 +08:00
|
|
|
#include "8250.h"
|
|
|
|
|
2015-06-16 16:59:37 +08:00
|
|
|
#define ADDR_PORT 0
|
|
|
|
#define DATA_PORT 1
|
2014-08-01 03:22:26 +08:00
|
|
|
#define EXIT_KEY 0xAA
|
|
|
|
#define CHIP_ID1 0x20
|
|
|
|
#define CHIP_ID2 0x21
|
2016-10-04 16:28:01 +08:00
|
|
|
#define CHIP_ID_F81216AD 0x1602
|
|
|
|
#define CHIP_ID_F81216H 0x0501
|
2014-08-01 03:22:26 +08:00
|
|
|
#define VENDOR_ID1 0x23
|
|
|
|
#define VENDOR_ID1_VAL 0x19
|
|
|
|
#define VENDOR_ID2 0x24
|
|
|
|
#define VENDOR_ID2_VAL 0x34
|
2015-06-16 16:59:40 +08:00
|
|
|
#define IO_ADDR1 0x61
|
|
|
|
#define IO_ADDR2 0x60
|
2014-08-01 03:22:26 +08:00
|
|
|
#define LDN 0x7
|
|
|
|
|
2016-08-11 05:54:13 +08:00
|
|
|
#define FINTEK_IRQ_MODE 0x70
|
2016-05-27 10:02:51 +08:00
|
|
|
#define IRQ_SHARE BIT(4)
|
|
|
|
#define IRQ_MODE_MASK (BIT(6) | BIT(5))
|
|
|
|
#define IRQ_LEVEL_LOW 0
|
|
|
|
#define IRQ_EDGE_HIGH BIT(5)
|
|
|
|
|
2014-08-01 03:22:26 +08:00
|
|
|
#define RS485 0xF0
|
|
|
|
#define RTS_INVERT BIT(5)
|
|
|
|
#define RS485_URA BIT(4)
|
|
|
|
#define RXW4C_IRA BIT(3)
|
|
|
|
#define TXW4C_IRA BIT(2)
|
|
|
|
|
2016-10-04 16:28:01 +08:00
|
|
|
#define FIFO_CTRL 0xF6
|
|
|
|
#define FIFO_MODE_MASK (BIT(1) | BIT(0))
|
|
|
|
#define FIFO_MODE_128 (BIT(1) | BIT(0))
|
|
|
|
#define RXFTHR_MODE_MASK (BIT(5) | BIT(4))
|
|
|
|
#define RXFTHR_MODE_4X BIT(5)
|
|
|
|
|
2015-06-16 16:59:36 +08:00
|
|
|
struct fintek_8250 {
|
2016-10-04 16:28:01 +08:00
|
|
|
u16 pid;
|
2015-06-16 16:59:37 +08:00
|
|
|
u16 base_port;
|
2015-06-16 16:59:36 +08:00
|
|
|
u8 index;
|
2015-06-16 16:59:39 +08:00
|
|
|
u8 key;
|
2015-06-16 16:59:36 +08:00
|
|
|
};
|
|
|
|
|
2016-10-04 16:27:59 +08:00
|
|
|
static u8 sio_read_reg(struct fintek_8250 *pdata, u8 reg)
|
|
|
|
{
|
|
|
|
outb(reg, pdata->base_port + ADDR_PORT);
|
|
|
|
return inb(pdata->base_port + DATA_PORT);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sio_write_reg(struct fintek_8250 *pdata, u8 reg, u8 data)
|
|
|
|
{
|
|
|
|
outb(reg, pdata->base_port + ADDR_PORT);
|
|
|
|
outb(data, pdata->base_port + DATA_PORT);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sio_write_mask_reg(struct fintek_8250 *pdata, u8 reg, u8 mask,
|
|
|
|
u8 data)
|
|
|
|
{
|
|
|
|
u8 tmp;
|
|
|
|
|
|
|
|
tmp = (sio_read_reg(pdata, reg) & ~mask) | (mask & data);
|
|
|
|
sio_write_reg(pdata, reg, tmp);
|
|
|
|
}
|
|
|
|
|
2015-06-16 16:59:39 +08:00
|
|
|
static int fintek_8250_enter_key(u16 base_port, u8 key)
|
2015-06-16 16:59:37 +08:00
|
|
|
{
|
2016-04-27 16:40:10 +08:00
|
|
|
if (!request_muxed_region(base_port, 2, "8250_fintek"))
|
2014-08-01 03:22:26 +08:00
|
|
|
return -EBUSY;
|
|
|
|
|
2015-06-16 16:59:39 +08:00
|
|
|
outb(key, base_port + ADDR_PORT);
|
|
|
|
outb(key, base_port + ADDR_PORT);
|
2014-08-01 03:22:26 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-06-16 16:59:37 +08:00
|
|
|
static void fintek_8250_exit_key(u16 base_port)
|
|
|
|
{
|
2014-08-01 03:22:26 +08:00
|
|
|
|
2015-06-16 16:59:37 +08:00
|
|
|
outb(EXIT_KEY, base_port + ADDR_PORT);
|
|
|
|
release_region(base_port + ADDR_PORT, 2);
|
2014-08-01 03:22:26 +08:00
|
|
|
}
|
|
|
|
|
2016-10-04 16:27:59 +08:00
|
|
|
static int fintek_8250_check_id(struct fintek_8250 *pdata)
|
2014-08-01 03:22:26 +08:00
|
|
|
{
|
2015-06-16 16:59:38 +08:00
|
|
|
u16 chip;
|
2014-08-01 03:22:26 +08:00
|
|
|
|
2016-10-04 16:27:59 +08:00
|
|
|
if (sio_read_reg(pdata, VENDOR_ID1) != VENDOR_ID1_VAL)
|
2014-08-01 03:22:26 +08:00
|
|
|
return -ENODEV;
|
|
|
|
|
2016-10-04 16:27:59 +08:00
|
|
|
if (sio_read_reg(pdata, VENDOR_ID2) != VENDOR_ID2_VAL)
|
2014-08-01 03:22:26 +08:00
|
|
|
return -ENODEV;
|
|
|
|
|
2016-10-04 16:27:59 +08:00
|
|
|
chip = sio_read_reg(pdata, CHIP_ID1);
|
|
|
|
chip |= sio_read_reg(pdata, CHIP_ID2) << 8;
|
2015-06-16 16:59:38 +08:00
|
|
|
|
2016-10-04 16:28:01 +08:00
|
|
|
if (chip != CHIP_ID_F81216AD && chip != CHIP_ID_F81216H)
|
2015-06-16 16:59:38 +08:00
|
|
|
return -ENODEV;
|
|
|
|
|
2016-10-04 16:28:01 +08:00
|
|
|
pdata->pid = chip;
|
2014-08-01 03:22:26 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-06 16:22:52 +08:00
|
|
|
static int fintek_8250_rs485_config(struct uart_port *port,
|
2014-08-01 03:22:26 +08:00
|
|
|
struct serial_rs485 *rs485)
|
|
|
|
{
|
|
|
|
uint8_t config = 0;
|
2015-06-16 16:59:36 +08:00
|
|
|
struct fintek_8250 *pdata = port->private_data;
|
2014-08-01 03:22:26 +08:00
|
|
|
|
2015-06-16 16:59:36 +08:00
|
|
|
if (!pdata)
|
2014-08-01 03:22:26 +08:00
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if (rs485->flags & SER_RS485_ENABLED)
|
|
|
|
memset(rs485->padding, 0, sizeof(rs485->padding));
|
|
|
|
else
|
|
|
|
memset(rs485, 0, sizeof(*rs485));
|
|
|
|
|
|
|
|
rs485->flags &= SER_RS485_ENABLED | SER_RS485_RTS_ON_SEND |
|
|
|
|
SER_RS485_RTS_AFTER_SEND;
|
|
|
|
|
|
|
|
if (rs485->delay_rts_before_send) {
|
|
|
|
rs485->delay_rts_before_send = 1;
|
|
|
|
config |= TXW4C_IRA;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rs485->delay_rts_after_send) {
|
|
|
|
rs485->delay_rts_after_send = 1;
|
|
|
|
config |= RXW4C_IRA;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((!!(rs485->flags & SER_RS485_RTS_ON_SEND)) ==
|
|
|
|
(!!(rs485->flags & SER_RS485_RTS_AFTER_SEND)))
|
|
|
|
rs485->flags &= SER_RS485_ENABLED;
|
|
|
|
else
|
|
|
|
config |= RS485_URA;
|
|
|
|
|
|
|
|
if (rs485->flags & SER_RS485_RTS_ON_SEND)
|
|
|
|
config |= RTS_INVERT;
|
|
|
|
|
2015-06-16 16:59:39 +08:00
|
|
|
if (fintek_8250_enter_key(pdata->base_port, pdata->key))
|
2014-08-01 03:22:26 +08:00
|
|
|
return -EBUSY;
|
|
|
|
|
2016-10-04 16:27:59 +08:00
|
|
|
sio_write_reg(pdata, LDN, pdata->index);
|
|
|
|
sio_write_reg(pdata, RS485, config);
|
2015-06-16 16:59:37 +08:00
|
|
|
fintek_8250_exit_key(pdata->base_port);
|
2014-08-01 03:22:26 +08:00
|
|
|
|
2014-11-06 16:22:52 +08:00
|
|
|
port->rs485 = *rs485;
|
|
|
|
|
2014-08-01 03:22:26 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-10-04 16:28:00 +08:00
|
|
|
static void fintek_8250_set_irq_mode(struct fintek_8250 *pdata, bool is_level)
|
|
|
|
{
|
|
|
|
sio_write_reg(pdata, LDN, pdata->index);
|
|
|
|
sio_write_mask_reg(pdata, FINTEK_IRQ_MODE, IRQ_SHARE, IRQ_SHARE);
|
|
|
|
sio_write_mask_reg(pdata, FINTEK_IRQ_MODE, IRQ_MODE_MASK,
|
|
|
|
is_level ? IRQ_LEVEL_LOW : IRQ_EDGE_HIGH);
|
|
|
|
}
|
|
|
|
|
2016-10-04 16:28:01 +08:00
|
|
|
static void fintek_8250_set_max_fifo(struct fintek_8250 *pdata)
|
|
|
|
{
|
|
|
|
switch (pdata->pid) {
|
|
|
|
case CHIP_ID_F81216H: /* 128Bytes FIFO */
|
|
|
|
sio_write_mask_reg(pdata, FIFO_CTRL,
|
|
|
|
FIFO_MODE_MASK | RXFTHR_MODE_MASK,
|
|
|
|
FIFO_MODE_128 | RXFTHR_MODE_4X);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default: /* Default 16Bytes FIFO */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-04 16:28:00 +08:00
|
|
|
static int probe_setup_port(struct fintek_8250 *pdata, u16 io_address,
|
|
|
|
unsigned int irq)
|
2015-06-16 16:59:37 +08:00
|
|
|
{
|
|
|
|
static const u16 addr[] = {0x4e, 0x2e};
|
2015-06-16 16:59:39 +08:00
|
|
|
static const u8 keys[] = {0x77, 0xa0, 0x87, 0x67};
|
2016-10-04 16:28:00 +08:00
|
|
|
struct irq_data *irq_data;
|
|
|
|
bool level_mode = false;
|
2015-06-16 16:59:40 +08:00
|
|
|
int i, j, k;
|
2015-06-16 16:59:37 +08:00
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE(addr); i++) {
|
2015-06-16 16:59:39 +08:00
|
|
|
for (j = 0; j < ARRAY_SIZE(keys); j++) {
|
2016-10-04 16:27:59 +08:00
|
|
|
pdata->base_port = addr[i];
|
|
|
|
pdata->key = keys[j];
|
2015-06-16 16:59:39 +08:00
|
|
|
|
|
|
|
if (fintek_8250_enter_key(addr[i], keys[j]))
|
|
|
|
continue;
|
2016-10-04 16:27:59 +08:00
|
|
|
if (fintek_8250_check_id(pdata)) {
|
2015-06-16 16:59:40 +08:00
|
|
|
fintek_8250_exit_key(addr[i]);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (k = 0; k < 4; k++) {
|
|
|
|
u16 aux;
|
|
|
|
|
2016-10-04 16:27:59 +08:00
|
|
|
sio_write_reg(pdata, LDN, k);
|
|
|
|
aux = sio_read_reg(pdata, IO_ADDR1);
|
|
|
|
aux |= sio_read_reg(pdata, IO_ADDR2) << 8;
|
2015-06-16 16:59:40 +08:00
|
|
|
if (aux != io_address)
|
|
|
|
continue;
|
|
|
|
|
2016-04-27 16:40:10 +08:00
|
|
|
pdata->index = k;
|
|
|
|
|
2016-10-04 16:28:00 +08:00
|
|
|
irq_data = irq_get_irq_data(irq);
|
|
|
|
if (irq_data)
|
|
|
|
level_mode =
|
|
|
|
irqd_is_level_type(irq_data);
|
|
|
|
|
|
|
|
fintek_8250_set_irq_mode(pdata, level_mode);
|
2016-10-04 16:28:01 +08:00
|
|
|
fintek_8250_set_max_fifo(pdata);
|
2016-10-04 16:28:00 +08:00
|
|
|
fintek_8250_exit_key(addr[i]);
|
|
|
|
|
2016-04-27 16:40:10 +08:00
|
|
|
return 0;
|
2015-06-16 16:59:39 +08:00
|
|
|
}
|
2016-04-27 16:40:10 +08:00
|
|
|
|
2015-06-16 16:59:40 +08:00
|
|
|
fintek_8250_exit_key(addr[i]);
|
2015-06-16 16:59:39 +08:00
|
|
|
}
|
2015-06-16 16:59:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
2016-04-27 16:40:10 +08:00
|
|
|
int fintek_8250_probe(struct uart_8250_port *uart)
|
2014-08-01 03:22:26 +08:00
|
|
|
{
|
2015-06-16 16:59:36 +08:00
|
|
|
struct fintek_8250 *pdata;
|
2016-04-27 16:40:10 +08:00
|
|
|
struct fintek_8250 probe_data;
|
2014-08-01 03:22:26 +08:00
|
|
|
|
2016-10-04 16:28:00 +08:00
|
|
|
if (probe_setup_port(&probe_data, uart->port.iobase, uart->port.irq))
|
2014-08-01 03:22:26 +08:00
|
|
|
return -ENODEV;
|
|
|
|
|
2016-04-27 16:40:10 +08:00
|
|
|
pdata = devm_kzalloc(uart->port.dev, sizeof(*pdata), GFP_KERNEL);
|
2015-06-16 16:59:36 +08:00
|
|
|
if (!pdata)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2016-04-27 16:40:10 +08:00
|
|
|
memcpy(pdata, &probe_data, sizeof(probe_data));
|
|
|
|
uart->port.rs485_config = fintek_8250_rs485_config;
|
|
|
|
uart->port.private_data = pdata;
|
2014-08-01 03:22:26 +08:00
|
|
|
|
2016-10-04 16:28:00 +08:00
|
|
|
return 0;
|
2014-08-01 03:22:26 +08:00
|
|
|
}
|