Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

[PATCH] atmel_serial: Atmel RS485 support v2

53 views
Skip to first unread message

Claudio Scordino

unread,
Mar 29, 2010, 3:20:01 AM3/29/10
to
Hi all,

this is the new version of the patch to add RS485 support to the
atmel_serial driver. It's been changed according to Ryan Mallon's
comments.

This new patch has been tested again (with and without DMA) by Sebastian
Heutling (CC:-ed).

Many thanks to all,

Claudio


atmel_serial: Atmel RS485 support

Signed-off-by: Claudio Scordino <cla...@evidence.eu.com>
Signed-off-by: Michael Trimarchi <mic...@evidence.eu.com>
Signed-off-by: Rick Bronson <ri...@efn.org>
Signed-off-by: Sebastian Heutling <Sebastian...@who-ing.de>
---
arch/arm/include/asm/ioctls.h | 2 +
arch/arm/mach-at91/include/mach/board.h | 2 +
drivers/serial/atmel_serial.c | 244 ++++++++++++++++++++++++++-----
3 files changed, 212 insertions(+), 36 deletions(-)

diff --git a/arch/arm/include/asm/ioctls.h b/arch/arm/include/asm/ioctls.h
index a91d8a1..82f2177 100644
--- a/arch/arm/include/asm/ioctls.h
+++ b/arch/arm/include/asm/ioctls.h
@@ -70,6 +70,8 @@
#define TIOCGICOUNT 0x545D /* read serial port inline interrupt counts */
#define FIOQSIZE 0x545E

+#define TIOCSRS485 0x5461
+
/* Used for packet mode */
#define TIOCPKT_DATA 0
#define TIOCPKT_FLUSHREAD 1
diff --git a/arch/arm/mach-at91/include/mach/board.h b/arch/arm/mach-at91/include/mach/board.h
index ceaec6c..fd8a385 100644
--- a/arch/arm/mach-at91/include/mach/board.h
+++ b/arch/arm/mach-at91/include/mach/board.h
@@ -39,6 +39,7 @@
#include <linux/usb/atmel_usba_udc.h>
#include <linux/atmel-mci.h>
#include <sound/atmel-ac97c.h>
+#include <linux/serial.h>

/* USB Device */
struct at91_udc_data {
@@ -146,6 +147,7 @@ struct atmel_uart_data {
short use_dma_tx; /* use transmit DMA? */
short use_dma_rx; /* use receive DMA? */
void __iomem *regs; /* virtual base address, if any */
+ struct serial_rs485 rs485; /* rs485 settings */
};
extern void __init at91_add_device_serial(void);

diff --git a/drivers/serial/atmel_serial.c b/drivers/serial/atmel_serial.c
index 2c9bf9b..826a885 100644
--- a/drivers/serial/atmel_serial.c
+++ b/drivers/serial/atmel_serial.c
@@ -38,6 +38,7 @@
#include <linux/dma-mapping.h>
#include <linux/atmel_pdc.h>
#include <linux/atmel_serial.h>
+#include <linux/uaccess.h>

#include <asm/io.h>

@@ -59,6 +60,9 @@

#include <linux/serial_core.h>

+static void atmel_start_rx(struct uart_port *port);
+static void atmel_stop_rx(struct uart_port *port);
+
#ifdef CONFIG_SERIAL_ATMEL_TTYAT

/* Use device name ttyAT, major 204 and minor 154-169. This is necessary if we
@@ -93,6 +97,7 @@
#define UART_GET_BRGR(port) __raw_readl((port)->membase + ATMEL_US_BRGR)
#define UART_PUT_BRGR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_BRGR)
#define UART_PUT_RTOR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_RTOR)
+#define UART_PUT_TTGR(port, v) __raw_writel(v, (port)->membase + ATMEL_US_TTGR)

/* PDC registers */
#define UART_PUT_PTCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_PTCR)
@@ -147,6 +152,10 @@ struct atmel_uart_port {
unsigned int irq_status_prev;

struct circ_buf rx_ring;
+
+ struct serial_rs485 rs485; /* rs485 settings */
+
+ unsigned int tx_done_mask;
};

static struct atmel_uart_port atmel_ports[ATMEL_MAX_UART];
@@ -187,6 +196,81 @@ static bool atmel_use_dma_tx(struct uart_port *port)
}
#endif

+/* Enable or disable the rs485 support */
+void atmel_config_rs485(struct uart_port *port, struct serial_rs485 *rs485conf)
+{
+ struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
+ unsigned long flags;
+ unsigned int mode;
+
+ spin_lock_irqsave(&port->lock, flags);
+
+ mode = UART_GET_MR(port);
+
+ /* Resetting serial mode to RS232 (0x0) */
+ mode &= ~ATMEL_US_USMODE;
+
+ atmel_port->rs485 = *rs485conf;
+
+ if (rs485conf->flags & SER_RS485_ENABLED) {
+ dev_dbg(port->dev, "Setting UART to RS485\n");
+ atmel_port->tx_done_mask = ATMEL_US_TXEMPTY;
+ UART_PUT_TTGR(port, rs485conf->delay_rts_before_send);
+ mode |= ATMEL_US_USMODE_RS485;
+ } else {
+ dev_dbg(port->dev, "Setting UART to RS232\n");
+ if (atmel_use_dma_tx(port))
+ atmel_port->tx_done_mask = ATMEL_US_ENDTX | \
+ ATMEL_US_TXBUFE;
+ else
+ atmel_port->tx_done_mask = ATMEL_US_TXRDY;
+ }
+ UART_PUT_MR(port, mode);
+
+ spin_unlock_irqrestore(&port->lock, flags);
+}
+
+
+static ssize_t show_rs485(struct device *dev, struct device_attribute *attr, \
+ char *buf)
+{
+ struct platform_device *pdev = to_platform_device(dev);
+ struct uart_port *port = platform_get_drvdata(pdev);
+ unsigned int current_mode;
+
+ current_mode = UART_GET_MR(port) & ATMEL_US_USMODE;
+ return snprintf(buf, PAGE_SIZE, "%u\n", current_mode);
+}
+
+static ssize_t set_rs485(struct device *dev, struct device_attribute *attr, \
+ const char *buf, size_t len)
+{
+ struct platform_device *pdev = to_platform_device(dev);
+ struct uart_port *port = platform_get_drvdata(pdev);
+ struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
+ struct serial_rs485 rs485conf = atmel_port->rs485;
+ unsigned int value;
+
+ if (!buf)
+ return -EINVAL;
+
+ value = !!(simple_strtoul(buf, NULL, 0));
+
+ if (value) {
+ rs485conf.flags |= SER_RS485_ENABLED;
+ /* 0x4 is the normal reset value. */
+ rs485conf.delay_rts_before_send = 0x00000004;
+ } else {
+ rs485conf.flags &= ~(SER_RS485_ENABLED);
+ }
+
+ atmel_config_rs485(port, &rs485conf);
+
+ return strnlen(buf, PAGE_SIZE);
+}
+
+static DEVICE_ATTR(rs485, 0644, show_rs485, set_rs485);
+
/*
* Return TIOCSER_TEMT when transmitter FIFO and Shift register is empty.
*/
@@ -202,6 +286,7 @@ static void atmel_set_mctrl(struct uart_port *port, u_int mctrl)
{
unsigned int control = 0;
unsigned int mode;
+ struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);

#ifdef CONFIG_ARCH_AT91RM9200
if (cpu_is_at91rm9200()) {
@@ -236,6 +321,17 @@ static void atmel_set_mctrl(struct uart_port *port, u_int mctrl)
mode |= ATMEL_US_CHMODE_LOC_LOOP;
else
mode |= ATMEL_US_CHMODE_NORMAL;
+
+ /* Resetting serial mode to RS232 (0x0) */
+ mode &= ~ATMEL_US_USMODE;
+
+ if (atmel_port->rs485.flags & SER_RS485_ENABLED) {
+ dev_dbg(port->dev, "Keeping UART to RS485\n");
+ UART_PUT_TTGR(port, atmel_port->rs485.delay_rts_before_send);
+ mode |= ATMEL_US_USMODE_RS485;
+ } else {
+ dev_dbg(port->dev, "Keeping UART to RS232\n");
+ }
UART_PUT_MR(port, mode);
}

@@ -268,12 +364,17 @@ static u_int atmel_get_mctrl(struct uart_port *port)
*/
static void atmel_stop_tx(struct uart_port *port)
{
+ struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
+
if (atmel_use_dma_tx(port)) {
/* disable PDC transmit */
UART_PUT_PTCR(port, ATMEL_PDC_TXTDIS);
- UART_PUT_IDR(port, ATMEL_US_ENDTX | ATMEL_US_TXBUFE);
- } else
- UART_PUT_IDR(port, ATMEL_US_TXRDY);
+ }
+ /* Disable interrupts */
+ UART_PUT_IDR(port, atmel_port->tx_done_mask);
+
+ if (atmel_port->rs485.flags & SER_RS485_ENABLED)
+ atmel_start_rx(port);
}

/*
@@ -281,17 +382,39 @@ static void atmel_stop_tx(struct uart_port *port)
*/
static void atmel_start_tx(struct uart_port *port)
{
+ struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
+
if (atmel_use_dma_tx(port)) {
if (UART_GET_PTSR(port) & ATMEL_PDC_TXTEN)
/* The transmitter is already running. Yes, we
really need this.*/
return;

- UART_PUT_IER(port, ATMEL_US_ENDTX | ATMEL_US_TXBUFE);
+ if (atmel_port->rs485.flags & SER_RS485_ENABLED)
+ atmel_stop_rx(port);
+
/* re-enable PDC transmit */
UART_PUT_PTCR(port, ATMEL_PDC_TXTEN);
- } else
- UART_PUT_IER(port, ATMEL_US_TXRDY);
+ }
+ /* Enable interrupts */
+ UART_PUT_IER(port, atmel_port->tx_done_mask);
+}
+
+/*
+ * start receiving - port is in process of being opened.
+ */
+static void atmel_start_rx(struct uart_port *port)
+{
+ UART_PUT_CR(port, ATMEL_US_RSTSTA); /* reset status and receiver */
+
+ if (atmel_use_dma_rx(port)) {
+ /* enable PDC controller */
+ UART_PUT_IER(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT | \
+ port->read_status_mask);
+ UART_PUT_PTCR(port, ATMEL_PDC_RXTEN);
+ } else {
+ UART_PUT_IER(port, ATMEL_US_RXRDY);
+ }
}

/*
@@ -302,9 +425,11 @@ static void atmel_stop_rx(struct uart_port *port)
if (atmel_use_dma_rx(port)) {
/* disable PDC receive */
UART_PUT_PTCR(port, ATMEL_PDC_RXTDIS);
- UART_PUT_IDR(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT);
- } else
+ UART_PUT_IDR(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT | \
+ port->read_status_mask);
+ } else {
UART_PUT_IDR(port, ATMEL_US_RXRDY);
+ }
}

/*
@@ -428,8 +553,9 @@ static void atmel_rx_chars(struct uart_port *port)
static void atmel_tx_chars(struct uart_port *port)
{
struct circ_buf *xmit = &port->state->xmit;
+ struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);

- if (port->x_char && UART_GET_CSR(port) & ATMEL_US_TXRDY) {
+ if (port->x_char && UART_GET_CSR(port) & atmel_port->tx_done_mask) {
UART_PUT_CHAR(port, port->x_char);
port->icount.tx++;
port->x_char = 0;
@@ -437,7 +563,7 @@ static void atmel_tx_chars(struct uart_port *port)
if (uart_circ_empty(xmit) || uart_tx_stopped(port))
return;

- while (UART_GET_CSR(port) & ATMEL_US_TXRDY) {
+ while (UART_GET_CSR(port) & atmel_port->tx_done_mask) {
UART_PUT_CHAR(port, xmit->buf[xmit->tail]);
xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
port->icount.tx++;
@@ -449,7 +575,8 @@ static void atmel_tx_chars(struct uart_port *port)
uart_write_wakeup(port);

if (!uart_circ_empty(xmit))
- UART_PUT_IER(port, ATMEL_US_TXRDY);
+ /* Enable interrupts */
+ UART_PUT_IER(port, atmel_port->tx_done_mask);
}

/*
@@ -501,18 +628,10 @@ atmel_handle_transmit(struct uart_port *port, unsigned int pending)
{
struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);

- if (atmel_use_dma_tx(port)) {
- /* PDC transmit */
- if (pending & (ATMEL_US_ENDTX | ATMEL_US_TXBUFE)) {
- UART_PUT_IDR(port, ATMEL_US_ENDTX | ATMEL_US_TXBUFE);
- tasklet_schedule(&atmel_port->tasklet);
- }
- } else {
- /* Interrupt transmit */
- if (pending & ATMEL_US_TXRDY) {
- UART_PUT_IDR(port, ATMEL_US_TXRDY);
- tasklet_schedule(&atmel_port->tasklet);
- }
+ if (pending & atmel_port->tx_done_mask) {
+ /* Either PDC or interrupt transmission */
+ UART_PUT_IDR(port, atmel_port->tx_done_mask);
+ tasklet_schedule(&atmel_port->tasklet);
}
}

@@ -590,9 +709,15 @@ static void atmel_tx_dma(struct uart_port *port)

UART_PUT_TPR(port, pdc->dma_addr + xmit->tail);
UART_PUT_TCR(port, count);
- /* re-enable PDC transmit and interrupts */
+ /* re-enable PDC transmit */
UART_PUT_PTCR(port, ATMEL_PDC_TXTEN);
- UART_PUT_IER(port, ATMEL_US_ENDTX | ATMEL_US_TXBUFE);
+ /* Enable interrupts */
+ UART_PUT_IER(port, atmel_port->tx_done_mask);
+ } else {
+ if (atmel_port->rs485.flags & SER_RS485_ENABLED) {
+ /* DMA done, stop TX, start RX for RS485 */
+ atmel_start_rx(port);
+ }
}

if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
@@ -1017,6 +1142,7 @@ static void atmel_set_termios(struct uart_port *port, struct ktermios *termios,
{
unsigned long flags;
unsigned int mode, imr, quot, baud;
+ struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);

/* Get current mode register */
mode = UART_GET_MR(port) & ~(ATMEL_US_USCLKS | ATMEL_US_CHRL
@@ -1115,6 +1241,17 @@ static void atmel_set_termios(struct uart_port *port, struct ktermios *termios,
/* disable receiver and transmitter */
UART_PUT_CR(port, ATMEL_US_TXDIS | ATMEL_US_RXDIS);

+ /* Resetting serial mode to RS232 (0x0) */
+ mode &= ~ATMEL_US_USMODE;
+
+ if (atmel_port->rs485.flags & SER_RS485_ENABLED) {
+ dev_dbg(port->dev, "Keeping UART to RS485\n");
+ UART_PUT_TTGR(port, atmel_port->rs485.delay_rts_before_send);
+ mode |= ATMEL_US_USMODE_RS485;
+ } else {
+ dev_dbg(port->dev, "Keeping UART to RS232\n");
+ }
+
/* set the parity, stop bits and data size */
UART_PUT_MR(port, mode);

@@ -1231,6 +1368,30 @@ static void atmel_poll_put_char(struct uart_port *port, unsigned char ch)
}
#endif

+static int
+atmel_ioctl(struct uart_port *port, unsigned int cmd, unsigned long arg)
+{
+ struct serial_rs485 rs485conf;
+
+ switch (cmd) {
+ case TIOCSRS485:
+ if (copy_from_user(&rs485conf, (struct serial_rs485 *) arg, \
+ sizeof(rs485conf)))
+ return -EFAULT;
+
+ dev_dbg(port->dev, "enable e/o disable rs485\n");
+
+ atmel_config_rs485(port, &rs485conf);
+ break;
+
+ default:
+ return -ENOIOCTLCMD;
+ }
+ return 0;
+}
+
+
+
static struct uart_ops atmel_pops = {
.tx_empty = atmel_tx_empty,
.set_mctrl = atmel_set_mctrl,
@@ -1250,6 +1411,7 @@ static struct uart_ops atmel_pops = {
.config_port = atmel_config_port,
.verify_port = atmel_verify_port,
.pm = atmel_serial_pm,
+ .ioctl = atmel_ioctl,
#ifdef CONFIG_CONSOLE_POLL
.poll_get_char = atmel_poll_get_char,
.poll_put_char = atmel_poll_put_char,
@@ -1265,13 +1427,12 @@ static void __devinit atmel_init_port(struct atmel_uart_port *atmel_port,
struct uart_port *port = &atmel_port->uart;
struct atmel_uart_data *data = pdev->dev.platform_data;

- port->iotype = UPIO_MEM;
- port->flags = UPF_BOOT_AUTOCONF;
- port->ops = &atmel_pops;
- port->fifosize = 1;
- port->line = pdev->id;
- port->dev = &pdev->dev;
-
+ port->iotype = UPIO_MEM;
+ port->flags = UPF_BOOT_AUTOCONF;
+ port->ops = &atmel_pops;
+ port->fifosize = 1;
+ port->line = pdev->id;
+ port->dev = &pdev->dev;
port->mapbase = pdev->resource[0].start;
port->irq = pdev->resource[1].start;

@@ -1299,8 +1460,16 @@ static void __devinit atmel_init_port(struct atmel_uart_port *atmel_port,

atmel_port->use_dma_rx = data->use_dma_rx;
atmel_port->use_dma_tx = data->use_dma_tx;
- if (atmel_use_dma_tx(port))
+ atmel_port->rs485 = data->rs485;
+ /* Use TXEMPTY for interrupt when rs485 else TXRDY or ENDTX|TXBUFE */
+ if (atmel_port->rs485.flags & SER_RS485_ENABLED)
+ atmel_port->tx_done_mask = ATMEL_US_TXEMPTY;
+ else if (atmel_use_dma_tx(port)) {
port->fifosize = PDC_BUFFER_SIZE;
+ atmel_port->tx_done_mask = ATMEL_US_ENDTX | ATMEL_US_TXBUFE;
+ } else {
+ atmel_port->tx_done_mask = ATMEL_US_TXRDY;
+ }
}

/*
@@ -1334,6 +1503,7 @@ static void atmel_console_putchar(struct uart_port *port, int ch)
static void atmel_console_write(struct console *co, const char *s, u_int count)
{
struct uart_port *port = &atmel_ports[co->index].uart;
+ struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
unsigned int status, imr;
unsigned int pdc_tx;

@@ -1341,7 +1511,7 @@ static void atmel_console_write(struct console *co, const char *s, u_int count)
* First, save IMR and then disable interrupts
*/
imr = UART_GET_IMR(port);
- UART_PUT_IDR(port, ATMEL_US_RXRDY | ATMEL_US_TXRDY);
+ UART_PUT_IDR(port, ATMEL_US_RXRDY | atmel_port->tx_done_mask);

/* Store PDC transmit status and disable it */
pdc_tx = UART_GET_PTSR(port) & ATMEL_PDC_TXTEN;
@@ -1355,7 +1525,7 @@ static void atmel_console_write(struct console *co, const char *s, u_int count)
*/
do {
status = UART_GET_CSR(port);
- } while (!(status & ATMEL_US_TXRDY));
+ } while (!(status & atmel_port->tx_done_mask));

/* Restore PDC transmit status */
if (pdc_tx)
@@ -1587,7 +1757,7 @@ static int __devinit atmel_serial_probe(struct platform_device *pdev)
device_init_wakeup(&pdev->dev, 1);
platform_set_drvdata(pdev, port);

- return 0;
+ return device_create_file(&(pdev->dev), &dev_attr_rs485);

err_add_port:
kfree(port->rx_ring.buf);
@@ -1619,6 +1789,8 @@ static int __devexit atmel_serial_remove(struct platform_device *pdev)

clk_put(atmel_port->clk);

+ device_remove_file(&(pdev->dev), &dev_attr_rs485);
+
return ret;
}

--
1.6.0.4

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majo...@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

Ryan Mallon

unread,
Mar 29, 2010, 3:50:02 PM3/29/10
to
Claudio Scordino wrote:
> Hi all,
>
> this is the new version of the patch to add RS485 support to the
> atmel_serial driver. It's been changed according to Ryan Mallon's
> comments.
>
> This new patch has been tested again (with and without DMA) by Sebastian
> Heutling (CC:-ed).

A few more, mostly nitpicky, comments below.

Can you re-indent this structure so the rest of the members line up with
the new rs485 one.

Do we need all the whitespace here?

You only need to use backslashes on split lines in macro definitions.

> + else
> + atmel_port->tx_done_mask = ATMEL_US_TXRDY;
> + }
> + UART_PUT_MR(port, mode);
> +
> + spin_unlock_irqrestore(&port->lock, flags);
> +}
> +
> +
> +static ssize_t show_rs485(struct device *dev, struct device_attribute *attr, \
> + char *buf)
> +{

Same here, and a couple more places.

Don't need the parenthesis here.

"Setting" UART to RS485 probably makes more sense here (and couple of
other places).

You could remove this dev_dbg since amtel_config_rs485 already prints
debug information.

~Ryan

--
Bluewater Systems Ltd - ARM Technology Solution Centre

Ryan Mallon 5 Amuri Park, 404 Barbadoes St
ry...@bluewatersys.com PO Box 13 889, Christchurch 8013
http://www.bluewatersys.com New Zealand
Phone: +64 3 3779127 Freecall: Australia 1800 148 751
Fax: +64 3 3779135 USA 1800 261 2934

Claudio Scordino

unread,
Mar 30, 2010, 5:10:02 AM3/30/10
to
Ryan Mallon ha scritto:

> Claudio Scordino wrote:
>> Hi all,
>>
>> this is the new version of the patch to add RS485 support to the
>> atmel_serial driver. It's been changed according to Ryan Mallon's
>> comments.
>>
>> This new patch has been tested again (with and without DMA) by Sebastian
>> Heutling (CC:-ed).
>
> A few more, mostly nitpicky, comments below.

Hi Ryan,

many thanks for your feedback.

Please find attached a new version of the patch, modified according to
your comments.

Regards,

Claudio


atmel_serial: Atmel RS485 support

Signed-off-by: Claudio Scordino <cla...@evidence.eu.com>
Signed-off-by: Michael Trimarchi <mic...@evidence.eu.com>
Signed-off-by: Rick Bronson <ri...@efn.org>
Signed-off-by: Sebastian Heutling <Sebastian...@who-ing.de>
---
arch/arm/include/asm/ioctls.h | 2 +

arch/arm/mach-at91/include/mach/board.h | 8 +-
drivers/serial/atmel_serial.c | 241 ++++++++++++++++++++++++++-----
3 files changed, 212 insertions(+), 39 deletions(-)

diff --git a/arch/arm/include/asm/ioctls.h b/arch/arm/include/asm/ioctls.h
index a91d8a1..82f2177 100644
--- a/arch/arm/include/asm/ioctls.h
+++ b/arch/arm/include/asm/ioctls.h
@@ -70,6 +70,8 @@
#define TIOCGICOUNT 0x545D /* read serial port inline interrupt counts */
#define FIOQSIZE 0x545E

+#define TIOCSRS485 0x5461
+
/* Used for packet mode */
#define TIOCPKT_DATA 0
#define TIOCPKT_FLUSHREAD 1
diff --git a/arch/arm/mach-at91/include/mach/board.h b/arch/arm/mach-at91/include/mach/board.h

index ceaec6c..df2ed84 100644


--- a/arch/arm/mach-at91/include/mach/board.h
+++ b/arch/arm/mach-at91/include/mach/board.h
@@ -39,6 +39,7 @@
#include <linux/usb/atmel_usba_udc.h>
#include <linux/atmel-mci.h>
#include <sound/atmel-ac97c.h>
+#include <linux/serial.h>

/* USB Device */
struct at91_udc_data {

@@ -143,9 +144,10 @@ extern struct platform_device *atmel_default_console_device;
extern void __init __deprecated at91_init_serial(struct at91_uart_config *config);

struct atmel_uart_data {
- short use_dma_tx; /* use transmit DMA? */
- short use_dma_rx; /* use receive DMA? */
- void __iomem *regs; /* virtual base address, if any */
+ short use_dma_tx; /* use transmit DMA? */
+ short use_dma_rx; /* use receive DMA? */
+ void __iomem *regs; /* virt. base address, if any */


+ struct serial_rs485 rs485; /* rs485 settings */
};

extern void __init at91_add_device_serial(void);

diff --git a/drivers/serial/atmel_serial.c b/drivers/serial/atmel_serial.c

index 2c9bf9b..039cd65 100644


--- a/drivers/serial/atmel_serial.c
+++ b/drivers/serial/atmel_serial.c
@@ -38,6 +38,7 @@
#include <linux/dma-mapping.h>
#include <linux/atmel_pdc.h>
#include <linux/atmel_serial.h>
+#include <linux/uaccess.h>

#include <asm/io.h>

@@ -59,6 +60,9 @@

#include <linux/serial_core.h>

+static void atmel_start_rx(struct uart_port *port);
+static void atmel_stop_rx(struct uart_port *port);
+
#ifdef CONFIG_SERIAL_ATMEL_TTYAT

/* Use device name ttyAT, major 204 and minor 154-169. This is necessary if we
@@ -93,6 +97,7 @@
#define UART_GET_BRGR(port) __raw_readl((port)->membase + ATMEL_US_BRGR)
#define UART_PUT_BRGR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_BRGR)
#define UART_PUT_RTOR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_RTOR)
+#define UART_PUT_TTGR(port, v) __raw_writel(v, (port)->membase + ATMEL_US_TTGR)

/* PDC registers */
#define UART_PUT_PTCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_PTCR)

@@ -147,6 +152,9 @@ struct atmel_uart_port {


unsigned int irq_status_prev;

struct circ_buf rx_ring;
+
+ struct serial_rs485 rs485; /* rs485 settings */

+ unsigned int tx_done_mask;
};



static struct atmel_uart_port atmel_ports[ATMEL_MAX_UART];

@@ -187,6 +195,81 @@ static bool atmel_use_dma_tx(struct uart_port *port)

+ else
+ atmel_port->tx_done_mask = ATMEL_US_TXRDY;
+ }
+ UART_PUT_MR(port, mode);
+
+ spin_unlock_irqrestore(&port->lock, flags);
+}
+
+
+static ssize_t show_rs485(struct device *dev, struct device_attribute *attr,
+ char *buf)
+{

+ rs485conf.flags &= ~SER_RS485_ENABLED;


+ }
+
+ atmel_config_rs485(port, &rs485conf);
+
+ return strnlen(buf, PAGE_SIZE);
+}
+
+static DEVICE_ATTR(rs485, 0644, show_rs485, set_rs485);
+
/*
* Return TIOCSER_TEMT when transmitter FIFO and Shift register is empty.
*/

@@ -202,6 +285,7 @@ static void atmel_set_mctrl(struct uart_port *port, u_int mctrl)


{
unsigned int control = 0;
unsigned int mode;
+ struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);

#ifdef CONFIG_ARCH_AT91RM9200
if (cpu_is_at91rm9200()) {

@@ -236,6 +320,17 @@ static void atmel_set_mctrl(struct uart_port *port, u_int mctrl)


mode |= ATMEL_US_CHMODE_LOC_LOOP;
else
mode |= ATMEL_US_CHMODE_NORMAL;
+
+ /* Resetting serial mode to RS232 (0x0) */
+ mode &= ~ATMEL_US_USMODE;
+
+ if (atmel_port->rs485.flags & SER_RS485_ENABLED) {

+ dev_dbg(port->dev, "Setting UART to RS485\n");

+ UART_PUT_TTGR(port, atmel_port->rs485.delay_rts_before_send);
+ mode |= ATMEL_US_USMODE_RS485;
+ } else {

+ dev_dbg(port->dev, "Setting UART to RS232\n");
+ }

UART_PUT_MR(port, mode);
}

@@ -268,12 +363,17 @@ static u_int atmel_get_mctrl(struct uart_port *port)


*/
static void atmel_stop_tx(struct uart_port *port)
{
+ struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
+
if (atmel_use_dma_tx(port)) {
/* disable PDC transmit */
UART_PUT_PTCR(port, ATMEL_PDC_TXTDIS);
- UART_PUT_IDR(port, ATMEL_US_ENDTX | ATMEL_US_TXBUFE);
- } else
- UART_PUT_IDR(port, ATMEL_US_TXRDY);
+ }
+ /* Disable interrupts */
+ UART_PUT_IDR(port, atmel_port->tx_done_mask);
+
+ if (atmel_port->rs485.flags & SER_RS485_ENABLED)
+ atmel_start_rx(port);
}

/*

@@ -281,17 +381,39 @@ static void atmel_stop_tx(struct uart_port *port)

@@ -302,9 +424,11 @@ static void atmel_stop_rx(struct uart_port *port)


if (atmel_use_dma_rx(port)) {
/* disable PDC receive */
UART_PUT_PTCR(port, ATMEL_PDC_RXTDIS);
- UART_PUT_IDR(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT);
- } else
+ UART_PUT_IDR(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT |
+ port->read_status_mask);
+ } else {
UART_PUT_IDR(port, ATMEL_US_RXRDY);
+ }
}

/*

@@ -428,8 +552,9 @@ static void atmel_rx_chars(struct uart_port *port)


static void atmel_tx_chars(struct uart_port *port)
{
struct circ_buf *xmit = &port->state->xmit;
+ struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);

- if (port->x_char && UART_GET_CSR(port) & ATMEL_US_TXRDY) {
+ if (port->x_char && UART_GET_CSR(port) & atmel_port->tx_done_mask) {
UART_PUT_CHAR(port, port->x_char);
port->icount.tx++;
port->x_char = 0;

@@ -437,7 +562,7 @@ static void atmel_tx_chars(struct uart_port *port)


if (uart_circ_empty(xmit) || uart_tx_stopped(port))
return;

- while (UART_GET_CSR(port) & ATMEL_US_TXRDY) {
+ while (UART_GET_CSR(port) & atmel_port->tx_done_mask) {
UART_PUT_CHAR(port, xmit->buf[xmit->tail]);
xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
port->icount.tx++;

@@ -449,7 +574,8 @@ static void atmel_tx_chars(struct uart_port *port)


uart_write_wakeup(port);

if (!uart_circ_empty(xmit))
- UART_PUT_IER(port, ATMEL_US_TXRDY);
+ /* Enable interrupts */
+ UART_PUT_IER(port, atmel_port->tx_done_mask);
}

/*

@@ -501,18 +627,10 @@ atmel_handle_transmit(struct uart_port *port, unsigned int pending)


{
struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);

- if (atmel_use_dma_tx(port)) {
- /* PDC transmit */
- if (pending & (ATMEL_US_ENDTX | ATMEL_US_TXBUFE)) {
- UART_PUT_IDR(port, ATMEL_US_ENDTX | ATMEL_US_TXBUFE);
- tasklet_schedule(&atmel_port->tasklet);
- }
- } else {
- /* Interrupt transmit */
- if (pending & ATMEL_US_TXRDY) {
- UART_PUT_IDR(port, ATMEL_US_TXRDY);
- tasklet_schedule(&atmel_port->tasklet);
- }
+ if (pending & atmel_port->tx_done_mask) {
+ /* Either PDC or interrupt transmission */
+ UART_PUT_IDR(port, atmel_port->tx_done_mask);
+ tasklet_schedule(&atmel_port->tasklet);
}
}

@@ -590,9 +708,15 @@ static void atmel_tx_dma(struct uart_port *port)



UART_PUT_TPR(port, pdc->dma_addr + xmit->tail);
UART_PUT_TCR(port, count);
- /* re-enable PDC transmit and interrupts */
+ /* re-enable PDC transmit */
UART_PUT_PTCR(port, ATMEL_PDC_TXTEN);
- UART_PUT_IER(port, ATMEL_US_ENDTX | ATMEL_US_TXBUFE);
+ /* Enable interrupts */
+ UART_PUT_IER(port, atmel_port->tx_done_mask);
+ } else {
+ if (atmel_port->rs485.flags & SER_RS485_ENABLED) {
+ /* DMA done, stop TX, start RX for RS485 */
+ atmel_start_rx(port);
+ }
}

if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)

@@ -1017,6 +1141,7 @@ static void atmel_set_termios(struct uart_port *port, struct ktermios *termios,


{
unsigned long flags;
unsigned int mode, imr, quot, baud;
+ struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);

/* Get current mode register */
mode = UART_GET_MR(port) & ~(ATMEL_US_USCLKS | ATMEL_US_CHRL

@@ -1115,6 +1240,17 @@ static void atmel_set_termios(struct uart_port *port, struct ktermios *termios,


/* disable receiver and transmitter */
UART_PUT_CR(port, ATMEL_US_TXDIS | ATMEL_US_RXDIS);

+ /* Resetting serial mode to RS232 (0x0) */
+ mode &= ~ATMEL_US_USMODE;
+
+ if (atmel_port->rs485.flags & SER_RS485_ENABLED) {

+ dev_dbg(port->dev, "Setting UART to RS485\n");

+ UART_PUT_TTGR(port, atmel_port->rs485.delay_rts_before_send);
+ mode |= ATMEL_US_USMODE_RS485;
+ } else {

+ dev_dbg(port->dev, "Setting UART to RS232\n");
+ }

+
/* set the parity, stop bits and data size */
UART_PUT_MR(port, mode);

@@ -1231,6 +1367,28 @@ static void atmel_poll_put_char(struct uart_port *port, unsigned char ch)


}
#endif

+static int
+atmel_ioctl(struct uart_port *port, unsigned int cmd, unsigned long arg)
+{
+ struct serial_rs485 rs485conf;
+
+ switch (cmd) {
+ case TIOCSRS485:
+ if (copy_from_user(&rs485conf, (struct serial_rs485 *) arg,
+ sizeof(rs485conf)))
+ return -EFAULT;
+

+ atmel_config_rs485(port, &rs485conf);
+ break;
+
+ default:
+ return -ENOIOCTLCMD;
+ }
+ return 0;
+}
+
+
+
static struct uart_ops atmel_pops = {
.tx_empty = atmel_tx_empty,
.set_mctrl = atmel_set_mctrl,

@@ -1250,6 +1408,7 @@ static struct uart_ops atmel_pops = {


.config_port = atmel_config_port,
.verify_port = atmel_verify_port,
.pm = atmel_serial_pm,
+ .ioctl = atmel_ioctl,
#ifdef CONFIG_CONSOLE_POLL
.poll_get_char = atmel_poll_get_char,
.poll_put_char = atmel_poll_put_char,

@@ -1265,13 +1424,12 @@ static void __devinit atmel_init_port(struct atmel_uart_port *atmel_port,


struct uart_port *port = &atmel_port->uart;
struct atmel_uart_data *data = pdev->dev.platform_data;

- port->iotype = UPIO_MEM;
- port->flags = UPF_BOOT_AUTOCONF;
- port->ops = &atmel_pops;
- port->fifosize = 1;
- port->line = pdev->id;
- port->dev = &pdev->dev;
-
+ port->iotype = UPIO_MEM;
+ port->flags = UPF_BOOT_AUTOCONF;
+ port->ops = &atmel_pops;
+ port->fifosize = 1;
+ port->line = pdev->id;
+ port->dev = &pdev->dev;
port->mapbase = pdev->resource[0].start;
port->irq = pdev->resource[1].start;

@@ -1299,8 +1457,16 @@ static void __devinit atmel_init_port(struct atmel_uart_port *atmel_port,



atmel_port->use_dma_rx = data->use_dma_rx;
atmel_port->use_dma_tx = data->use_dma_tx;
- if (atmel_use_dma_tx(port))
+ atmel_port->rs485 = data->rs485;
+ /* Use TXEMPTY for interrupt when rs485 else TXRDY or ENDTX|TXBUFE */
+ if (atmel_port->rs485.flags & SER_RS485_ENABLED)
+ atmel_port->tx_done_mask = ATMEL_US_TXEMPTY;
+ else if (atmel_use_dma_tx(port)) {
port->fifosize = PDC_BUFFER_SIZE;
+ atmel_port->tx_done_mask = ATMEL_US_ENDTX | ATMEL_US_TXBUFE;
+ } else {
+ atmel_port->tx_done_mask = ATMEL_US_TXRDY;
+ }
}

/*

@@ -1334,6 +1500,7 @@ static void atmel_console_putchar(struct uart_port *port, int ch)


static void atmel_console_write(struct console *co, const char *s, u_int count)
{
struct uart_port *port = &atmel_ports[co->index].uart;
+ struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
unsigned int status, imr;
unsigned int pdc_tx;

@@ -1341,7 +1508,7 @@ static void atmel_console_write(struct console *co, const char *s, u_int count)


* First, save IMR and then disable interrupts
*/
imr = UART_GET_IMR(port);
- UART_PUT_IDR(port, ATMEL_US_RXRDY | ATMEL_US_TXRDY);
+ UART_PUT_IDR(port, ATMEL_US_RXRDY | atmel_port->tx_done_mask);

/* Store PDC transmit status and disable it */
pdc_tx = UART_GET_PTSR(port) & ATMEL_PDC_TXTEN;

@@ -1355,7 +1522,7 @@ static void atmel_console_write(struct console *co, const char *s, u_int count)


*/
do {
status = UART_GET_CSR(port);
- } while (!(status & ATMEL_US_TXRDY));
+ } while (!(status & atmel_port->tx_done_mask));

/* Restore PDC transmit status */
if (pdc_tx)

@@ -1587,7 +1754,7 @@ static int __devinit atmel_serial_probe(struct platform_device *pdev)


device_init_wakeup(&pdev->dev, 1);
platform_set_drvdata(pdev, port);

- return 0;
+ return device_create_file(&(pdev->dev), &dev_attr_rs485);

err_add_port:
kfree(port->rx_ring.buf);

@@ -1619,6 +1786,8 @@ static int __devexit atmel_serial_remove(struct platform_device *pdev)



clk_put(atmel_port->clk);

+ device_remove_file(&(pdev->dev), &dev_attr_rs485);
+
return ret;
}

--
1.6.0.4

Ryan Mallon

unread,
Mar 30, 2010, 3:40:02 PM3/30/10
to
Claudio Scordino wrote:
> Ryan Mallon ha scritto:
>> Claudio Scordino wrote:
>>> Hi all,
>>>
>>> this is the new version of the patch to add RS485 support to the
>>> atmel_serial driver. It's been changed according to Ryan Mallon's
>>> comments.
>>>
>>> This new patch has been tested again (with and without DMA) by Sebastian
>>> Heutling (CC:-ed).
>> A few more, mostly nitpicky, comments below.
>
> Hi Ryan,
>
> many thanks for your feedback.
>
> Please find attached a new version of the patch, modified according to
> your comments.

Looks good. You should add a Tested-by for Sebastian, and you can add:

Reviewed-by: Ryan Mallon <ry...@bluewatersys.com>

~Ryan


--

Bluewater Systems Ltd - ARM Technology Solution Centre

Ryan Mallon 5 Amuri Park, 404 Barbadoes St
ry...@bluewatersys.com PO Box 13 889, Christchurch 8013
http://www.bluewatersys.com New Zealand
Phone: +64 3 3779127 Freecall: Australia 1800 148 751
Fax: +64 3 3779135 USA 1800 261 2934

Claudio Scordino

unread,
Apr 8, 2010, 4:00:02 AM4/8/10
to
Ryan Mallon ha scritto:
> Claudio Scordino wrote:
>> Ryan Mallon ha scritto:
>>> Claudio Scordino wrote:
>>>> Hi all,
>>>>
>>>> this is the new version of the patch to add RS485 support to the
>>>> atmel_serial driver. It's been changed according to Ryan Mallon's
>>>> comments.
>>>>
>>>> This new patch has been tested again (with and without DMA) by Sebastian
>>>> Heutling (CC:-ed).
>>> A few more, mostly nitpicky, comments below.
>> Hi Ryan,
>>
>> many thanks for your feedback.
>>
>> Please find attached a new version of the patch, modified according to
>> your comments.
>
> Looks good. You should add a Tested-by for Sebastian, and you can add:
>
> Reviewed-by: Ryan Mallon <ry...@bluewatersys.com>
>

Here it is (I just added the Tested-by and Reviewed-by fields).

Best regards,

Claudio


atmel_serial: Atmel RS485 support

Signed-off-by: Claudio Scordino <cla...@evidence.eu.com>
Signed-off-by: Michael Trimarchi <mic...@evidence.eu.com>
Signed-off-by: Rick Bronson <ri...@efn.org>
Signed-off-by: Sebastian Heutling <Sebastian...@who-ing.de>

Tested-by: Sebastian Heutling <Sebastian...@who-ing.de>
Reviewed-by: Ryan Mallon <ry...@bluewatersys.com>

1.6.0.4

Russell King - ARM Linux

unread,
Apr 8, 2010, 5:10:03 AM4/8/10
to
On Thu, Apr 08, 2010 at 09:58:21AM +0200, Claudio Scordino wrote:
> diff --git a/arch/arm/include/asm/ioctls.h b/arch/arm/include/asm/ioctls.h
> index a91d8a1..82f2177 100644
> --- a/arch/arm/include/asm/ioctls.h
> +++ b/arch/arm/include/asm/ioctls.h
> @@ -70,6 +70,8 @@
> #define TIOCGICOUNT 0x545D /* read serial port inline interrupt counts */
> #define FIOQSIZE 0x545E
>
> +#define TIOCSRS485 0x5461

Why are you only supporting half the interface?

#define TIOCGRS485 0x542E
#define TIOCSRS485 0x542F

is what is in include/asm-generic/ioctls.h - and it would be a good idea
to use the same numbering as the generic file rather than creating yet
more divergence.

Alan Cox

unread,
Apr 8, 2010, 6:20:01 AM4/8/10
to
NAK - assorted problems, notably locking ones caused by the sysfs stuff
which should probably be dropped.

> +#define TIOCSRS485 0x5461

Please provide TIOCGRS485 as well

> +static ssize_t show_rs485(struct device *dev, struct device_attribute *attr,
> + char *buf)
> +{
> + struct platform_device *pdev = to_platform_device(dev);
> + struct uart_port *port = platform_get_drvdata(pdev);
> + unsigned int current_mode;
> +
> + current_mode = UART_GET_MR(port) & ATMEL_US_USMODE;
> + return snprintf(buf, PAGE_SIZE, "%u\n", current_mode);
> +}

You should have TIOCGRS485 for providing the info back as part of the API

> +
> +static ssize_t set_rs485(struct device *dev, struct device_attribute *attr,
> + const char *buf, size_t len)

Expain the locking on this could you - I don't see what protects against
parallel ioctl and sysfs stuff ?

> +static DEVICE_ATTR(rs485, 0644, show_rs485, set_rs485);

Why should this be public read ?

> + /* Resetting serial mode to RS232 (0x0) */
> + mode &= ~ATMEL_US_USMODE;
> +
> + if (atmel_port->rs485.flags & SER_RS485_ENABLED) {
> + dev_dbg(port->dev, "Setting UART to RS485\n");
> + UART_PUT_TTGR(port, atmel_port->rs485.delay_rts_before_send);
> + mode |= ATMEL_US_USMODE_RS485;
> + } else {
> + dev_dbg(port->dev, "Setting UART to RS232\n");

Locking versus sysfs ?

> + UART_PUT_IDR(port, atmel_port->tx_done_mask);
> +
> + if (atmel_port->rs485.flags & SER_RS485_ENABLED)
> + atmel_start_rx(port);
> }

Ditto

> - UART_PUT_IER(port, ATMEL_US_ENDTX | ATMEL_US_TXBUFE);
> + if (atmel_port->rs485.flags & SER_RS485_ENABLED)
> + atmel_stop_rx(port);
> +

Ditto


Given the locking mess you are going to create I would suggest dropping
the sysfs stuff.

Alan

Claudio Scordino

unread,
Apr 8, 2010, 9:20:02 AM4/8/10
to
Alan Cox ha scritto:


Thank you for the feedback.

Let me know if this new version solves the above problems.

Claudio


atmel_serial: Atmel RS485 support

Signed-off-by: Claudio Scordino <cla...@evidence.eu.com>
Signed-off-by: Michael Trimarchi <mic...@evidence.eu.com>
Signed-off-by: Rick Bronson <ri...@efn.org>
Signed-off-by: Sebastian Heutling <Sebastian...@who-ing.de>
Tested-by: Sebastian Heutling <Sebastian...@who-ing.de>
Reviewed-by: Ryan Mallon <ry...@bluewatersys.com>
---

arch/arm/include/asm/ioctls.h | 3 +
arch/arm/mach-at91/include/mach/board.h | 8 +-
drivers/serial/atmel_serial.c | 203 +++++++++++++++++++++++++------
3 files changed, 176 insertions(+), 38 deletions(-)

diff --git a/arch/arm/include/asm/ioctls.h b/arch/arm/include/asm/ioctls.h
index a91d8a1..7f0b6d1 100644
--- a/arch/arm/include/asm/ioctls.h
+++ b/arch/arm/include/asm/ioctls.h
@@ -53,6 +53,9 @@
#define TIOCGPTN _IOR('T',0x30, unsigned int) /* Get Pty Number (of pty-mux device) */
#define TIOCSPTLCK _IOW('T',0x31, int) /* Lock/unlock Pty */

+#define TIOCGRS485 0x542E
+#define TIOCSRS485 0x542F
+
#define FIONCLEX 0x5450 /* these numbers need to be adjusted. */
#define FIOCLEX 0x5451
#define FIOASYNC 0x5452


diff --git a/arch/arm/mach-at91/include/mach/board.h b/arch/arm/mach-at91/include/mach/board.h
index ceaec6c..df2ed84 100644
--- a/arch/arm/mach-at91/include/mach/board.h
+++ b/arch/arm/mach-at91/include/mach/board.h
@@ -39,6 +39,7 @@
#include <linux/usb/atmel_usba_udc.h>
#include <linux/atmel-mci.h>
#include <sound/atmel-ac97c.h>
+#include <linux/serial.h>

/* USB Device */
struct at91_udc_data {
@@ -143,9 +144,10 @@ extern struct platform_device *atmel_default_console_device;
extern void __init __deprecated at91_init_serial(struct at91_uart_config *config);

struct atmel_uart_data {
- short use_dma_tx; /* use transmit DMA? */
- short use_dma_rx; /* use receive DMA? */
- void __iomem *regs; /* virtual base address, if any */
+ short use_dma_tx; /* use transmit DMA? */
+ short use_dma_rx; /* use receive DMA? */
+ void __iomem *regs; /* virt. base address, if any */
+ struct serial_rs485 rs485; /* rs485 settings */
};
extern void __init at91_add_device_serial(void);

diff --git a/drivers/serial/atmel_serial.c b/drivers/serial/atmel_serial.c

index 2c9bf9b..4f4b0b9 100644

@@ -187,6 +195,40 @@ static bool atmel_use_dma_tx(struct uart_port *port)


}
#endif

+/* Enable or disable the rs485 support */
+void atmel_config_rs485(struct uart_port *port, struct serial_rs485 *rs485conf)
+{
+ struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
+ unsigned long flags;
+ unsigned int mode;
+
+ spin_lock_irqsave(&port->lock, flags);
+
+ mode = UART_GET_MR(port);
+

+ /* Resetting serial mode to RS232 (0x0) */
+ mode &= ~ATMEL_US_USMODE;
+

+ atmel_port->rs485 = *rs485conf;
+

+ if (rs485conf->flags & SER_RS485_ENABLED) {


+ dev_dbg(port->dev, "Setting UART to RS485\n");

+ atmel_port->tx_done_mask = ATMEL_US_TXEMPTY;
+ UART_PUT_TTGR(port, rs485conf->delay_rts_before_send);

+ mode |= ATMEL_US_USMODE_RS485;
+ } else {
+ dev_dbg(port->dev, "Setting UART to RS232\n");

+ if (atmel_use_dma_tx(port))
+ atmel_port->tx_done_mask = ATMEL_US_ENDTX |
+ ATMEL_US_TXBUFE;
+ else
+ atmel_port->tx_done_mask = ATMEL_US_TXRDY;
+ }
+ UART_PUT_MR(port, mode);
+
+ spin_unlock_irqrestore(&port->lock, flags);
+}
+

/*
* Return TIOCSER_TEMT when transmitter FIFO and Shift register is empty.
*/

@@ -202,6 +244,7 @@ static void atmel_set_mctrl(struct uart_port *port, u_int mctrl)


{
unsigned int control = 0;
unsigned int mode;
+ struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);

#ifdef CONFIG_ARCH_AT91RM9200
if (cpu_is_at91rm9200()) {

@@ -236,6 +279,17 @@ static void atmel_set_mctrl(struct uart_port *port, u_int mctrl)


mode |= ATMEL_US_CHMODE_LOC_LOOP;
else
mode |= ATMEL_US_CHMODE_NORMAL;
+

+ /* Resetting serial mode to RS232 (0x0) */
+ mode &= ~ATMEL_US_USMODE;
+
+ if (atmel_port->rs485.flags & SER_RS485_ENABLED) {
+ dev_dbg(port->dev, "Setting UART to RS485\n");
+ UART_PUT_TTGR(port, atmel_port->rs485.delay_rts_before_send);
+ mode |= ATMEL_US_USMODE_RS485;
+ } else {
+ dev_dbg(port->dev, "Setting UART to RS232\n");

+ }
UART_PUT_MR(port, mode);
}

@@ -268,12 +322,17 @@ static u_int atmel_get_mctrl(struct uart_port *port)


*/
static void atmel_stop_tx(struct uart_port *port)
{
+ struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
+
if (atmel_use_dma_tx(port)) {
/* disable PDC transmit */
UART_PUT_PTCR(port, ATMEL_PDC_TXTDIS);
- UART_PUT_IDR(port, ATMEL_US_ENDTX | ATMEL_US_TXBUFE);
- } else
- UART_PUT_IDR(port, ATMEL_US_TXRDY);
+ }
+ /* Disable interrupts */

+ UART_PUT_IDR(port, atmel_port->tx_done_mask);
+
+ if (atmel_port->rs485.flags & SER_RS485_ENABLED)
+ atmel_start_rx(port);
}

/*
@@ -281,17 +340,39 @@ static void atmel_stop_tx(struct uart_port *port)


*/
static void atmel_start_tx(struct uart_port *port)
{
+ struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
+
if (atmel_use_dma_tx(port)) {
if (UART_GET_PTSR(port) & ATMEL_PDC_TXTEN)
/* The transmitter is already running. Yes, we
really need this.*/
return;

- UART_PUT_IER(port, ATMEL_US_ENDTX | ATMEL_US_TXBUFE);
+ if (atmel_port->rs485.flags & SER_RS485_ENABLED)
+ atmel_stop_rx(port);
+

@@ -302,9 +383,11 @@ static void atmel_stop_rx(struct uart_port *port)


if (atmel_use_dma_rx(port)) {
/* disable PDC receive */
UART_PUT_PTCR(port, ATMEL_PDC_RXTDIS);
- UART_PUT_IDR(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT);
- } else
+ UART_PUT_IDR(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT |
+ port->read_status_mask);
+ } else {
UART_PUT_IDR(port, ATMEL_US_RXRDY);
+ }
}

/*

@@ -428,8 +511,9 @@ static void atmel_rx_chars(struct uart_port *port)


static void atmel_tx_chars(struct uart_port *port)
{
struct circ_buf *xmit = &port->state->xmit;
+ struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);

- if (port->x_char && UART_GET_CSR(port) & ATMEL_US_TXRDY) {
+ if (port->x_char && UART_GET_CSR(port) & atmel_port->tx_done_mask) {
UART_PUT_CHAR(port, port->x_char);
port->icount.tx++;
port->x_char = 0;

@@ -437,7 +521,7 @@ static void atmel_tx_chars(struct uart_port *port)


if (uart_circ_empty(xmit) || uart_tx_stopped(port))
return;

- while (UART_GET_CSR(port) & ATMEL_US_TXRDY) {
+ while (UART_GET_CSR(port) & atmel_port->tx_done_mask) {
UART_PUT_CHAR(port, xmit->buf[xmit->tail]);
xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
port->icount.tx++;

@@ -449,7 +533,8 @@ static void atmel_tx_chars(struct uart_port *port)


uart_write_wakeup(port);

if (!uart_circ_empty(xmit))
- UART_PUT_IER(port, ATMEL_US_TXRDY);
+ /* Enable interrupts */
+ UART_PUT_IER(port, atmel_port->tx_done_mask);
}

/*

@@ -501,18 +586,10 @@ atmel_handle_transmit(struct uart_port *port, unsigned int pending)


{
struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);

- if (atmel_use_dma_tx(port)) {
- /* PDC transmit */
- if (pending & (ATMEL_US_ENDTX | ATMEL_US_TXBUFE)) {
- UART_PUT_IDR(port, ATMEL_US_ENDTX | ATMEL_US_TXBUFE);
- tasklet_schedule(&atmel_port->tasklet);
- }
- } else {
- /* Interrupt transmit */
- if (pending & ATMEL_US_TXRDY) {
- UART_PUT_IDR(port, ATMEL_US_TXRDY);
- tasklet_schedule(&atmel_port->tasklet);
- }
+ if (pending & atmel_port->tx_done_mask) {
+ /* Either PDC or interrupt transmission */

+ UART_PUT_IDR(port, atmel_port->tx_done_mask);
+ tasklet_schedule(&atmel_port->tasklet);
}
}

@@ -590,9 +667,15 @@ static void atmel_tx_dma(struct uart_port *port)



UART_PUT_TPR(port, pdc->dma_addr + xmit->tail);
UART_PUT_TCR(port, count);
- /* re-enable PDC transmit and interrupts */
+ /* re-enable PDC transmit */
UART_PUT_PTCR(port, ATMEL_PDC_TXTEN);

- UART_PUT_IER(port, ATMEL_US_ENDTX | ATMEL_US_TXBUFE);


+ /* Enable interrupts */
+ UART_PUT_IER(port, atmel_port->tx_done_mask);
+ } else {

+ if (atmel_port->rs485.flags & SER_RS485_ENABLED) {

+ /* DMA done, stop TX, start RX for RS485 */
+ atmel_start_rx(port);
+ }
}

if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)

@@ -1017,6 +1100,7 @@ static void atmel_set_termios(struct uart_port *port, struct ktermios *termios,


{
unsigned long flags;
unsigned int mode, imr, quot, baud;
+ struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);

/* Get current mode register */
mode = UART_GET_MR(port) & ~(ATMEL_US_USCLKS | ATMEL_US_CHRL

@@ -1115,6 +1199,17 @@ static void atmel_set_termios(struct uart_port *port, struct ktermios *termios,


/* disable receiver and transmitter */
UART_PUT_CR(port, ATMEL_US_TXDIS | ATMEL_US_RXDIS);

+ /* Resetting serial mode to RS232 (0x0) */
+ mode &= ~ATMEL_US_USMODE;
+
+ if (atmel_port->rs485.flags & SER_RS485_ENABLED) {
+ dev_dbg(port->dev, "Setting UART to RS485\n");
+ UART_PUT_TTGR(port, atmel_port->rs485.delay_rts_before_send);
+ mode |= ATMEL_US_USMODE_RS485;
+ } else {
+ dev_dbg(port->dev, "Setting UART to RS232\n");

+ }
+
/* set the parity, stop bits and data size */
UART_PUT_MR(port, mode);

@@ -1231,6 +1326,35 @@ static void atmel_poll_put_char(struct uart_port *port, unsigned char ch)


}
#endif

+static int
+atmel_ioctl(struct uart_port *port, unsigned int cmd, unsigned long arg)
+{
+ struct serial_rs485 rs485conf;
+
+ switch (cmd) {
+ case TIOCSRS485:
+ if (copy_from_user(&rs485conf, (struct serial_rs485 *) arg,
+ sizeof(rs485conf)))
+ return -EFAULT;
+
+ atmel_config_rs485(port, &rs485conf);
+ break;
+

+ case TIOCGRS485:
+ if (copy_to_user((struct serial_rs485 *) arg,
+ &(to_atmel_uart_port(port)->rs485),


+ sizeof(rs485conf)))
+ return -EFAULT;

+ break;
+
+ default:
+ return -ENOIOCTLCMD;
+ }
+ return 0;
+}
+
+
+
static struct uart_ops atmel_pops = {
.tx_empty = atmel_tx_empty,
.set_mctrl = atmel_set_mctrl,

@@ -1250,6 +1374,7 @@ static struct uart_ops atmel_pops = {


.config_port = atmel_config_port,
.verify_port = atmel_verify_port,
.pm = atmel_serial_pm,
+ .ioctl = atmel_ioctl,
#ifdef CONFIG_CONSOLE_POLL
.poll_get_char = atmel_poll_get_char,
.poll_put_char = atmel_poll_put_char,

@@ -1265,13 +1390,12 @@ static void __devinit atmel_init_port(struct atmel_uart_port *atmel_port,


struct uart_port *port = &atmel_port->uart;
struct atmel_uart_data *data = pdev->dev.platform_data;

- port->iotype = UPIO_MEM;
- port->flags = UPF_BOOT_AUTOCONF;
- port->ops = &atmel_pops;
- port->fifosize = 1;
- port->line = pdev->id;
- port->dev = &pdev->dev;
-
+ port->iotype = UPIO_MEM;
+ port->flags = UPF_BOOT_AUTOCONF;
+ port->ops = &atmel_pops;
+ port->fifosize = 1;
+ port->line = pdev->id;
+ port->dev = &pdev->dev;
port->mapbase = pdev->resource[0].start;
port->irq = pdev->resource[1].start;

@@ -1299,8 +1423,16 @@ static void __devinit atmel_init_port(struct atmel_uart_port *atmel_port,



atmel_port->use_dma_rx = data->use_dma_rx;
atmel_port->use_dma_tx = data->use_dma_tx;
- if (atmel_use_dma_tx(port))
+ atmel_port->rs485 = data->rs485;
+ /* Use TXEMPTY for interrupt when rs485 else TXRDY or ENDTX|TXBUFE */

+ if (atmel_port->rs485.flags & SER_RS485_ENABLED)

+ atmel_port->tx_done_mask = ATMEL_US_TXEMPTY;
+ else if (atmel_use_dma_tx(port)) {
port->fifosize = PDC_BUFFER_SIZE;
+ atmel_port->tx_done_mask = ATMEL_US_ENDTX | ATMEL_US_TXBUFE;
+ } else {
+ atmel_port->tx_done_mask = ATMEL_US_TXRDY;
+ }
}

/*

@@ -1334,6 +1466,7 @@ static void atmel_console_putchar(struct uart_port *port, int ch)


static void atmel_console_write(struct console *co, const char *s, u_int count)
{
struct uart_port *port = &atmel_ports[co->index].uart;
+ struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
unsigned int status, imr;
unsigned int pdc_tx;

@@ -1341,7 +1474,7 @@ static void atmel_console_write(struct console *co, const char *s, u_int count)


* First, save IMR and then disable interrupts
*/
imr = UART_GET_IMR(port);
- UART_PUT_IDR(port, ATMEL_US_RXRDY | ATMEL_US_TXRDY);
+ UART_PUT_IDR(port, ATMEL_US_RXRDY | atmel_port->tx_done_mask);

/* Store PDC transmit status and disable it */
pdc_tx = UART_GET_PTSR(port) & ATMEL_PDC_TXTEN;

@@ -1355,7 +1488,7 @@ static void atmel_console_write(struct console *co, const char *s, u_int count)


*/
do {
status = UART_GET_CSR(port);
- } while (!(status & ATMEL_US_TXRDY));
+ } while (!(status & atmel_port->tx_done_mask));

/* Restore PDC transmit status */
if (pdc_tx)

--
1.6.0.4

Alan Cox

unread,
Apr 8, 2010, 9:50:02 AM4/8/10
to
> Thank you for the feedback.
>
> Let me know if this new version solves the above problems.

Looks good to me. Thanks for the quick clean up.

Alan

Philippe De Muyter

unread,
Apr 12, 2010, 5:50:01 AM4/12/10
to
On Wed, Feb 13, 2008 at 08:28:49AM +0000, Alan Cox wrote:
>
> > Thank you for the feedback.
> >
> > Let me know if this new version solves the above problems.
>
> Looks good to me. Thanks for the quick clean up.

Could someone add somewhere (Documentation, or usr/include/linux/serial.h)
a precise documentation of how this is to be used from user-space,
and implemented in other drivers ?

Philippe

Claudio Scordino

unread,
Apr 12, 2010, 12:00:02 PM4/12/10
to
Philippe De Muyter ha scritto:

> On Wed, Feb 13, 2008 at 08:28:49AM +0000, Alan Cox wrote:
>
>>> Thank you for the feedback.
>>>
>>> Let me know if this new version solves the above problems.
>>>
>> Looks good to me. Thanks for the quick clean up.
>>
>
> Could someone add somewhere (Documentation, or usr/include/linux/serial.h)
> a precise documentation of how this is to be used from user-space,
> and implemented in other drivers ?
>

I can add a short Documention/ file containing information about how to
use ioctls with this driver.

I'll add the document when I'll submit the final patch.

Regards,

Claudio

Nicolas Ferre

unread,
May 26, 2010, 9:30:03 AM5/26/10
to
Le 08/04/2010 15:16, Claudio Scordino :

[..]

> atmel_serial: Atmel RS485 support
>
> Signed-off-by: Claudio Scordino <cla...@evidence.eu.com>
> Signed-off-by: Michael Trimarchi <mic...@evidence.eu.com>
> Signed-off-by: Rick Bronson <ri...@efn.org>
> Signed-off-by: Sebastian Heutling <Sebastian...@who-ing.de>
> Tested-by: Sebastian Heutling <Sebastian...@who-ing.de>
> Reviewed-by: Ryan Mallon <ry...@bluewatersys.com>

Even though I personally did not test this patch I think it is time to
go forward with this very good work.

Considering the number of people involved in this testing and review I
think that we can submit this patch for mainline. Who will handle it?
- do we submit to ARM or serial subsystem ?

Best regards,
--
Nicolas Ferre

Claudio Scordino

unread,
May 27, 2010, 4:50:01 AM5/27/10
to
Nicolas Ferre ha scritto:

> Le 08/04/2010 15:16, Claudio Scordino :
>
> [..]
>
>
>> atmel_serial: Atmel RS485 support
>>
>> Signed-off-by: Claudio Scordino <cla...@evidence.eu.com>
>> Signed-off-by: Michael Trimarchi <mic...@evidence.eu.com>
>> Signed-off-by: Rick Bronson <ri...@efn.org>
>> Signed-off-by: Sebastian Heutling <Sebastian...@who-ing.de>
>> Tested-by: Sebastian Heutling <Sebastian...@who-ing.de>
>> Reviewed-by: Ryan Mallon <ry...@bluewatersys.com>
>>
>
> Even though I personally did not test this patch I think it is time to
> go forward with this very good work.
>
> Considering the number of people involved in this testing and review I
> think that we can submit this patch for mainline. Who will handle it?
> - do we submit to ARM or serial subsystem ?
>

Hi Nicolas,

the latest version of the patch has been already merged in the
latest Linus' git tree through Russell King's patchsystem (the maintainer of the
driver never answered to emails...).

Many thanks,

Claudio

Nicolas Ferre

unread,
May 27, 2010, 6:00:03 AM5/27/10
to
Le 27/05/2010 10:37, Claudio Scordino :

> Nicolas Ferre ha scritto:
>> Le 08/04/2010 15:16, Claudio Scordino :
>>
>> [..]
>>
>>
>>> atmel_serial: Atmel RS485 support
>>>
>>> Signed-off-by: Claudio Scordino <cla...@evidence.eu.com>
>>> Signed-off-by: Michael Trimarchi <mic...@evidence.eu.com>
>>> Signed-off-by: Rick Bronson <ri...@efn.org>
>>> Signed-off-by: Sebastian Heutling <Sebastian...@who-ing.de>
>>> Tested-by: Sebastian Heutling <Sebastian...@who-ing.de>
>>> Reviewed-by: Ryan Mallon <ry...@bluewatersys.com>
>>>
>>
>> Even though I personally did not test this patch I think it is time to
>> go forward with this very good work.
>>
>> Considering the number of people involved in this testing and review I
>> think that we can submit this patch for mainline. Who will handle it?
>> - do we submit to ARM or serial subsystem ?
>>
>
> Hi Nicolas,
>
> the latest version of the patch has been already merged in the
> latest Linus' git tree through Russell King's patchsystem

My fault: I use to check Russell patchsystem with "AT91" searching
string and not "atmel" ;-)

> (the maintainer of the driver never answered to emails...).

I was supposed to take over its maintainance one year ago but did not
find time to do it. My fault here also...

Anyway, it was the occasion to tell all of you that this RS485 support
is a nice job and that even if I do not answer, I try to track all
interesting activity in the background.

Best regards,
--
Nicolas Ferre

--

Wolfram Sang

unread,
May 27, 2010, 6:40:01 AM5/27/10
to
Hi Claudio,

kudos for getting a RS485-implementation into the kernel! Very useful.

> > the latest version of the patch has been already merged in the
> > latest Linus' git tree through Russell King's patchsystem

Nitpick: Wasn't there some documentation promised after the final patch ;)

Regards,

Wolfram

--
Pengutronix e.K. | Wolfram Sang |
Industrial Linux Solutions | http://www.pengutronix.de/ |

signature.asc

Haavard Skinnemoen

unread,
May 28, 2010, 5:50:01 AM5/28/10
to
Nicolas Ferre <nicola...@atmel.com> wrote:
> > (the maintainer of the driver never answered to emails...).
>
> I was supposed to take over its maintainance one year ago but did not
> find time to do it. My fault here also...

My fault too...I should really try harder to respond to e-mails. I was
planning to give it a spin and send it upstream, but I never found the
time.

Nicolas, is it ok if I send a patch to update MAINTAINERS? That way,
you'll receive these patches and questions about them by e-mail rather
than me.

Haavard

Nicolas Ferre

unread,
May 28, 2010, 9:00:02 AM5/28/10
to
I take over the maintenance of SPI, USART, Ethernet and USB gadget drivers.
Those drivers are found in Atmel microcontrollers, both AT32/AVR32 and
AT91/ARM.

Signed-off-by: Nicolas Ferre <nicola...@atmel.com>
---
Haavard,
I finally change the MAINTAINERS entries for all drivers that you were
taking care of. I must admit that I was a bit frighten at the first place but
anyway, since one year, I am getting used to it ;-)


MAINTAINERS | 10 +++++-----
1 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/MAINTAINERS b/MAINTAINERS
index 33047a6..d9ce9c6 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -1148,7 +1148,7 @@ F: drivers/mmc/host/atmel-mci.c
F: drivers/mmc/host/atmel-mci-regs.h

ATMEL AT91 / AT32 SERIAL DRIVER
-M: Haavard Skinnemoen <hskin...@atmel.com>
+M: Nicolas Ferre <nicola...@atmel.com>
S: Supported
F: drivers/serial/atmel_serial.c

@@ -1160,18 +1160,18 @@ F: drivers/video/atmel_lcdfb.c
F: include/video/atmel_lcdc.h

ATMEL MACB ETHERNET DRIVER
-M: Haavard Skinnemoen <hskin...@atmel.com>
+M: Nicolas Ferre <nicola...@atmel.com>
S: Supported
F: drivers/net/macb.*

ATMEL SPI DRIVER
-M: Haavard Skinnemoen <hskin...@atmel.com>
+M: Nicolas Ferre <nicola...@atmel.com>
S: Supported
F: drivers/spi/atmel_spi.*

ATMEL USBA UDC DRIVER
-M: Haavard Skinnemoen <hskin...@atmel.com>
-L: ker...@avr32linux.org
+M: Nicolas Ferre <nicola...@atmel.com>
+L: linux-ar...@lists.infradead.org (moderated for non-subscribers)
W: http://avr32linux.org/twiki/bin/view/Main/AtmelUsbDeviceDriver
S: Supported
F: drivers/usb/gadget/atmel_usba_udc.*
--
1.5.6.5

Haavard Skinnemoen

unread,
May 28, 2010, 9:40:02 AM5/28/10
to
Nicolas Ferre <nicola...@atmel.com> wrote:
> I take over the maintenance of SPI, USART, Ethernet and USB gadget drivers.
> Those drivers are found in Atmel microcontrollers, both AT32/AVR32 and
> AT91/ARM.
>
> Signed-off-by: Nicolas Ferre <nicola...@atmel.com>

Acked-by: Haavard Skinnemoen <haavard.s...@atmel.com>

> ---
> Haavard,
> I finally change the MAINTAINERS entries for all drivers that you were
> taking care of. I must admit that I was a bit frighten at the first place but
> anyway, since one year, I am getting used to it ;-)

Good to hear that you're getting the hang of it :-)

Haavard

Claudio Scordino

unread,
May 28, 2010, 11:10:01 AM5/28/10
to
Wolfram Sang ha scritto:

> Hi Claudio,
>
> kudos for getting a RS485-implementation into the kernel! Very useful.

Many thanks!

I hope other people will find it useful too.

>
>>> the latest version of the patch has been already merged in the
>>> latest Linus' git tree through Russell King's patchsystem
>
> Nitpick: Wasn't there some documentation promised after the final patch ;)

Here is the first attempt.

Best regards,

Claudio


Documentation about RS485

Signed-off-by: Claudio Scordino <cla...@evidence.eu.com>
---
Documentation/serial/00-INDEX | 2 +
Documentation/serial/serial-rs485.txt | 118 +++++++++++++++++++++++++++++++++
2 files changed, 120 insertions(+), 0 deletions(-)
create mode 100644 Documentation/serial/serial-rs485.txt

diff --git a/Documentation/serial/00-INDEX b/Documentation/serial/00-INDEX
index 07dcdb0..e09468a 100644
--- a/Documentation/serial/00-INDEX
+++ b/Documentation/serial/00-INDEX
@@ -14,6 +14,8 @@ riscom8.txt
- notes on using the RISCom/8 multi-port serial driver.
rocket.txt
- info on the Comtrol RocketPort multiport serial driver.
+serial-rs485.txt
+ - info about RS485 structures and support in the kernel.
specialix.txt
- info on hardware/driver for specialix IO8+ multiport serial card.
stallion.txt
diff --git a/Documentation/serial/serial-rs485.txt b/Documentation/serial/serial-rs485.txt
new file mode 100644
index 0000000..dac98c3
--- /dev/null
+++ b/Documentation/serial/serial-rs485.txt
@@ -0,0 +1,118 @@
+ RS485 SERIAL COMMUNICATIONS
+
+1. INTRODUCTION
+
+ EIA-485, also known as TIA/EIA-485 or RS-485, is a standard defining the
+ electrical characteristics of drivers and receivers for use in balanced
+ digital multipoint systems.
+ This standard is widely used for communications in industrial automation
+ because it can be used effectively over long distances and in electrically
+ noisy environments.
+ Even though the data is transmitted over a 2-wire twisted pair bus, all
+ EIA-485 transceivers interpret the voltage levels of the differential
+ signals with respect to a third common voltage. Without this common
+ reference, a set of transceivers may interpret the differential signals
+ incorrectly.
+ See [1] for more information.
+
+
+2. HARDWARE-RELATED CONSIDERATIONS
+
+ Some CPUs (e.g., Atmel AT91) contain a transceiver capable of working both
+ as RS232 and RS485. For these microcontrollers, the Linux driver should be
+ able of working in both modes, and proper ioctls (see later) should be made
+ available at user-level to allow switching from one mode to the other, and
+ viceversa.
+
+ On some other CPUs (e.g., Freescale imx25) the RS485 transceiver is not
+ integrated inside the microcontroller itself. Therefore, manufacturers who
+ use these microcontrollers to produce embedded boards need to connect an
+ external transceiver to some pin of the CPU.
+ On these architectures, therefore, no assumptions can be done at the
+ CPU-level about the presence of a RS485 transceiver, because the connection
+ (if any) is done outside the microcontroller. Moreover, even in case of
+ RS485 transceiver, the manufacturer is free to choose the CPU pin used for
+ the connection.
+
+
+3. DATA STRUCTURES ALREADY AVAILABLE IN THE KERNEL
+
+ The Linux kernel provides the serial_rs485 structure (inside
+ include/linux/serial.h) to handle RS485 communications. This data structure
+ is used to set and configure RS485 parameters in the platform data and in
+ ioctls.
+
+ Any driver for interfaces capable of working both as RS232 and RS485 should
+ provide at least the following ioctls:
+
+ - TIOCSRS485 (typically associated with number 0x542F). This ioctl is used
+ to enable/disable RS485 mode from user-space
+
+ - TIOCGRS485 (typically associated with number 0x542E). This ioctl is used
+ to get RS485 mode from kernel-space (i.e., driver) to user-space.
+
+ In other words, the serial driver should contain a code similar to the next
+ one:


+
+ static struct uart_ops atmel_pops = {

+ /* ... */
+ .ioctl = handle_ioctl,
+ };
+
+ static int
+ handle_ioctl(struct uart_port *port,
+ unsigned int cmd,
+ unsigned long arg)


+ {
+ struct serial_rs485 rs485conf;
+
+ switch (cmd) {
+ case TIOCSRS485:
+ if (copy_from_user(&rs485conf,

+ (struct serial_rs485 *) arg,


+ sizeof(rs485conf)))
+ return -EFAULT;
+

+ /* ... */


+ break;
+
+ case TIOCGRS485:
+ if (copy_to_user((struct serial_rs485 *) arg,

+ ...,


+ sizeof(rs485conf)))
+ return -EFAULT;

+ /* ... */
+ break;
+ /* ... */
+ }
+
+
+4. USAGE FROM USER-LEVEL
+
+ From user-level, RS485 configuration can be get/set using the previous
+ ioctls. For instance, to set RS485 you can use the following code:
+
+ #include <linux/serial.h>
+
+ /* Driver-specific ioctls: */
+ #define TIOCGRS485 0x542E
+ #define TIOCSRS485 0x542F
+
+ /* Open specific device: */
+ int fd = open ("/dev/mydevice", O_RDWR);

+ struct serial_rs485 rs485conf;
+

+ /* Set RS485 mode: */


+ rs485conf.flags |= SER_RS485_ENABLED;
+

+ /* Set delay: */
+ rs485conf.delay_rts_before_send = 0x00000004;
+ ioctl (fd, TIOCSRS485, &rs485conf);
+
+ close (fd);
+
+5. REFERENCES
+
+ [1] http://en.wikipedia.org/wiki/Rs485
+
+
--
1.6.0.4

Nicolas Ferre

unread,
Jun 11, 2010, 3:30:01 AM6/11/10
to
Le 28/05/2010 15:54, Nicolas Ferre :

> I take over the maintenance of SPI, USART, Ethernet and USB gadget drivers.
> Those drivers are found in Atmel microcontrollers, both AT32/AVR32 and
> AT91/ARM.
>
> Signed-off-by: Nicolas Ferre <nicola...@atmel.com>

Ping,

With added acked-by:
Acked-by: Haavard Skinnemoen <haavard.s...@atmel.com>


--
Nicolas Ferre

Claudio Scordino

unread,
Aug 11, 2010, 5:40:02 AM8/11/10
to
Hi all,

some time ago I've been asked (by both Wolfram and Philippe) to
provide some minimal documentation about the usage of the RS485
interface.

Here is the document (updated with the very last changes in the
interface).

Best regards,

Claudio


Documentation about RS485 serial communications.

Signed-off-by: Claudio Scordino <cla...@evidence.eu.com>
---
Documentation/serial/00-INDEX | 2 +

Documentation/serial/serial-rs485.txt | 123 +++++++++++++++++++++++++++++++++
2 files changed, 125 insertions(+), 0 deletions(-)
create mode 100644 Documentation/serial/serial-rs485.txt

diff --git a/Documentation/serial/00-INDEX b/Documentation/serial/00-INDEX
index 07dcdb0..e09468a 100644
--- a/Documentation/serial/00-INDEX
+++ b/Documentation/serial/00-INDEX
@@ -14,6 +14,8 @@ riscom8.txt
- notes on using the RISCom/8 multi-port serial driver.
rocket.txt
- info on the Comtrol RocketPort multiport serial driver.
+serial-rs485.txt
+ - info about RS485 structures and support in the kernel.
specialix.txt
- info on hardware/driver for specialix IO8+ multiport serial card.
stallion.txt
diff --git a/Documentation/serial/serial-rs485.txt b/Documentation/serial/serial-rs485.txt
new file mode 100644

index 0000000..f594831
--- /dev/null
+++ b/Documentation/serial/serial-rs485.txt
@@ -0,0 +1,123 @@

+ The Linux kernel provides the serial_rs485 structure (see [2]) to handle
+ RS485 communications. This data structure is used to set and configure RS485
+ parameters in the platform data and in ioctls.
+
+ Any driver for devices capable of working both as RS232 and RS485 should


+ provide at least the following ioctls:
+
+ - TIOCSRS485 (typically associated with number 0x542F). This ioctl is used
+ to enable/disable RS485 mode from user-space
+
+ - TIOCGRS485 (typically associated with number 0x542E). This ioctl is used
+ to get RS485 mode from kernel-space (i.e., driver) to user-space.
+
+ In other words, the serial driver should contain a code similar to the next
+ one:

+
+ static struct uart_ops atmel_pops = {

+ /* ... */
+ .ioctl = handle_ioctl,
+ };
+

+ static int handle_ioctl(struct uart_port *port,
+ unsigned int cmd,
+ unsigned long arg)


+ {
+ struct serial_rs485 rs485conf;
+
+ switch (cmd) {
+ case TIOCSRS485:
+ if (copy_from_user(&rs485conf,

+ (struct serial_rs485 *) arg,


+ sizeof(rs485conf)))
+ return -EFAULT;
+

+ /* ... */


+ break;
+
+ case TIOCGRS485:
+ if (copy_to_user((struct serial_rs485 *) arg,

+ ...,


+ sizeof(rs485conf)))
+ return -EFAULT;

+ /* ... */
+ break;
+

+ /* ... */
+ }
+ }
+
+
+4. USAGE FROM USER-LEVEL
+
+ From user-level, RS485 configuration can be get/set using the previous
+ ioctls. For instance, to set RS485 you can use the following code:
+
+ #include <linux/serial.h>
+
+ /* Driver-specific ioctls: */
+ #define TIOCGRS485 0x542E
+ #define TIOCSRS485 0x542F
+
+ /* Open specific device: */
+ int fd = open ("/dev/mydevice", O_RDWR);

+ struct serial_rs485 rs485conf;
+

+ /* Set RS485 mode: */

+ rs485conf.flags |= SER_RS485_ENABLED;
+

+ /* Set rts delay before send, if needed: */
+ rs485conf.flags |= SER_RS485_RTS_BEFORE_SEND;
+ rs485conf.delay_rts_before_send = ...;
+
+ /* Set rts delay after send, if needed: */
+ rs485conf.flags |= SER_RS485_RTS_AFTER_SEND;
+ rs485conf.delay_rts_after_send = ...;
+


+ ioctl (fd, TIOCSRS485, &rs485conf);
+
+ close (fd);
+
+5. REFERENCES
+
+ [1] http://en.wikipedia.org/wiki/Rs485

+ [2] include/linux/serial.h
--
1.6.0.4

Philippe De Muyter

unread,
Aug 11, 2010, 6:10:02 AM8/11/10
to
On Wed, Aug 11, 2010 at 11:26:23AM +0200, Claudio Scordino wrote:
> Hi all,
>
> some time ago I've been asked (by both Wolfram and Philippe) to
> provide some minimal documentation about the usage of the RS485
> interface.
>
> Here is the document (updated with the very last changes in the
> interface).

Thanks

Should those defines not come from a linux header file ?

> +
> + /* Open specific device: */
> + int fd = open ("/dev/mydevice", O_RDWR);
> + struct serial_rs485 rs485conf;
> +
> + /* Set RS485 mode: */
> + rs485conf.flags |= SER_RS485_ENABLED;
> +
> + /* Set rts delay before send, if needed: */
> + rs485conf.flags |= SER_RS485_RTS_BEFORE_SEND;
> + rs485conf.delay_rts_before_send = ...;
> +
> + /* Set rts delay after send, if needed: */
> + rs485conf.flags |= SER_RS485_RTS_AFTER_SEND;
> + rs485conf.delay_rts_after_send = ...;
> +
> + ioctl (fd, TIOCSRS485, &rs485conf);

I surmise that all the real work, the read() and write() system calls,
must come here, before the close() system call.

> +
> + close (fd);
> +
> +5. REFERENCES
> +
> + [1] http://en.wikipedia.org/wiki/Rs485
> + [2] include/linux/serial.h
> --
> 1.6.0.4
>

--
Philippe De Muyter phdm at macqel dot be Tel +32 27029044
Macq Electronique SA rue de l'Aeronef 2 B-1140 Bruxelles Fax +32 27029077

Randy Dunlap

unread,
Aug 11, 2010, 11:40:03 AM8/11/10
to
On Wed, 11 Aug 2010 11:26:23 +0200 Claudio Scordino wrote:

> Hi all,
>
> some time ago I've been asked (by both Wolfram and Philippe) to
> provide some minimal documentation about the usage of the RS485
> interface.
>
> Here is the document (updated with the very last changes in the
> interface).
>
> Best regards,
>
> Claudio
>
>
> Documentation about RS485 serial communications.
>
> Signed-off-by: Claudio Scordino <cla...@evidence.eu.com>
> ---
> Documentation/serial/00-INDEX | 2 +
> Documentation/serial/serial-rs485.txt | 123 +++++++++++++++++++++++++++++++++
> 2 files changed, 125 insertions(+), 0 deletions(-)
> create mode 100644 Documentation/serial/serial-rs485.txt

> diff --git a/Documentation/serial/serial-rs485.txt b/Documentation/serial/serial-rs485.txt
> new file mode 100644
> index 0000000..f594831
> --- /dev/null
> +++ b/Documentation/serial/serial-rs485.txt
> @@ -0,0 +1,123 @@
> + RS485 SERIAL COMMUNICATIONS
> +

...


> +
> +2. HARDWARE-RELATED CONSIDERATIONS
> +
> + Some CPUs (e.g., Atmel AT91) contain a transceiver capable of working both
> + as RS232 and RS485. For these microcontrollers, the Linux driver should be
> + able of working in both modes, and proper ioctls (see later) should be made

should be able to work in both modes
or
should be made capable of working in both modes

> + available at user-level to allow switching from one mode to the other, and
> + viceversa.

vice versa.

TIOC[SG]RS485 are #defined in <asm-generic/ioctls.h>, so
#include <asm-generic/ioctls.h>
here and/or below (in userspace program).

> + In other words, the serial driver should contain a code similar to the next

contain code similar to this:

> + one:
> +
> + static struct uart_ops atmel_pops = {
> + /* ... */
> + .ioctl = handle_ioctl,
> + };
> +
> + static int handle_ioctl(struct uart_port *port,
> + unsigned int cmd,
> + unsigned long arg)
> + {
> + struct serial_rs485 rs485conf;
> +
> + switch (cmd) {
> + case TIOCSRS485:

Coding style: we put switch and case at the same indent level.

...


> +
> +5. REFERENCES
> +
> + [1] http://en.wikipedia.org/wiki/Rs485
> + [2] include/linux/serial.h


Thanks for the addition.

---
~Randy
*** Remember to use Documentation/SubmitChecklist when testing your code ***

Claudio Scordino

unread,
Aug 11, 2010, 4:00:01 PM8/11/10
to
Hi Randy,

thank you for the feedback.

[...]

>
> TIOC[SG]RS485 are #defined in <asm-generic/ioctls.h>, so
> #include <asm-generic/ioctls.h>
> here and/or below (in userspace program).
>

I noticed that for some architectures (e.g., cris) these ioctls are defined also in arch/cris/include/asm/ioctls.h, and with different values with respect to the values defined on asm-generic/ioctls.h.

Therefore, I wasn't completely sure that the values defined in asm-generic are being used in every driver...

Best regards,

Claudio

Randy Dunlap

unread,
Aug 11, 2010, 4:30:02 PM8/11/10
to
----- Original Message -----
From: cla...@evidence.eu.com
To: randy....@oracle.com
Sent: Wednesday, August 11, 2010 12:59:08 PM GMT -08:00 US/Canada Pacific
Subject: Re: [PATCH] Documentation about RS485 serial communications

Hi Randy,

thank you for the feedback.

[...]

>
> TIOC[SG]RS485 are #defined in <asm-generic/ioctls.h>, so
> #include <asm-generic/ioctls.h>
> here and/or below (in userspace program).
>

I noticed that for some architectures (e.g., cris) these ioctls are defined also in arch/cris/include/asm/ioctls.h, and with different values with respect to the values defined on asm-generic/ioctls.h.

Therefore, I wasn't completely sure that the values defined in asm-generic are being used in every driver...

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Oh, OK, I see. Thanks for the info.

~Randy

Claudio Scordino

unread,
Aug 14, 2010, 9:00:01 AM8/14/10
to
Randy Dunlap ha scritto:

> On Wed, 11 Aug 2010 11:26:23 +0200 Claudio Scordino wrote:
>
>> Hi all,
>>
>> some time ago I've been asked (by both Wolfram and Philippe) to
>> provide some minimal documentation about the usage of the RS485
>> interface.
>>

[...]

>
> Thanks for the addition.
>

Hi all,

here is the document about RS485 with the requested additions.

If OK, somebody please provide for merging.

Best regards,

Claudio


Documentation about RS485 serial communications.

Signed-off-by: Claudio Scordino <cla...@evidence.eu.com>
---
Documentation/serial/00-INDEX | 2 +

Documentation/serial/serial-rs485.txt | 126 +++++++++++++++++++++++++++++++++
2 files changed, 128 insertions(+), 0 deletions(-)
create mode 100644 Documentation/serial/serial-rs485.txt

diff --git a/Documentation/serial/00-INDEX b/Documentation/serial/00-INDEX


index 07dcdb0..e09468a 100644
--- a/Documentation/serial/00-INDEX
+++ b/Documentation/serial/00-INDEX
@@ -14,6 +14,8 @@ riscom8.txt
- notes on using the RISCom/8 multi-port serial driver.
rocket.txt
- info on the Comtrol RocketPort multiport serial driver.
+serial-rs485.txt
+ - info about RS485 structures and support in the kernel.
specialix.txt
- info on hardware/driver for specialix IO8+ multiport serial card.
stallion.txt

diff --git a/Documentation/serial/serial-rs485.txt b/Documentation/serial/serial-rs485.txt
new file mode 100644

index 0000000..93b029e
--- /dev/null
+++ b/Documentation/serial/serial-rs485.txt
@@ -0,0 +1,126 @@


+ RS485 SERIAL COMMUNICATIONS
+

+1. INTRODUCTION
+
+ EIA-485, also known as TIA/EIA-485 or RS-485, is a standard defining the
+ electrical characteristics of drivers and receivers for use in balanced
+ digital multipoint systems.
+ This standard is widely used for communications in industrial automation
+ because it can be used effectively over long distances and in electrically
+ noisy environments.
+ Even though the data is transmitted over a 2-wire twisted pair bus, all
+ EIA-485 transceivers interpret the voltage levels of the differential
+ signals with respect to a third common voltage. Without this common
+ reference, a set of transceivers may interpret the differential signals
+ incorrectly.
+ See [1] for more information.
+

+
+2. HARDWARE-RELATED CONSIDERATIONS
+
+ Some CPUs (e.g., Atmel AT91) contain a transceiver capable of working both
+ as RS232 and RS485. For these microcontrollers, the Linux driver should be

+ made capable of working in both modes, and proper ioctls (see later) should
+ be made available at user-level to allow switching from one mode to the
+ other, and vice versa.

+


+ In other words, the serial driver should contain a code similar to the next

+ one:
+
+ static struct uart_ops atmel_pops = {
+ /* ... */
+ .ioctl = handle_ioctl,
+ };
+
+ static int handle_ioctl(struct uart_port *port,
+ unsigned int cmd,
+ unsigned long arg)
+ {
+ struct serial_rs485 rs485conf;
+
+ switch (cmd) {
+ case TIOCSRS485:

+ /* Open your specific device (e.g., /dev/mydevice): */


+ int fd = open ("/dev/mydevice", O_RDWR);

+ struct serial_rs485 rs485conf;
+

+ /* Set RS485 mode: */
+ rs485conf.flags |= SER_RS485_ENABLED;
+
+ /* Set rts delay before send, if needed: */
+ rs485conf.flags |= SER_RS485_RTS_BEFORE_SEND;
+ rs485conf.delay_rts_before_send = ...;
+
+ /* Set rts delay after send, if needed: */
+ rs485conf.flags |= SER_RS485_RTS_AFTER_SEND;
+ rs485conf.delay_rts_after_send = ...;
+
+ ioctl (fd, TIOCSRS485, &rs485conf);
+

+ /* Use read() and write() syscalls here... */
+
+ /* Close the device when finished: */
+ close (fd);


+
+5. REFERENCES
+
+ [1] http://en.wikipedia.org/wiki/Rs485
+ [2] include/linux/serial.h

--
1.6.0.4

Randy Dunlap

unread,
Aug 15, 2010, 6:10:02 PM8/15/10
to
On 08/14/10 05:50, Claudio Scordino wrote:
> Randy Dunlap ha scritto:
>> On Wed, 11 Aug 2010 11:26:23 +0200 Claudio Scordino wrote:
>>
>>> Hi all,
>>>
>>> some time ago I've been asked (by both Wolfram and Philippe) to
>>> provide some minimal documentation about the usage of the RS485
>>> interface.
>>>
>
> [...]
>
>>
>> Thanks for the addition.
>>
>
> Hi all,
>
> here is the document about RS485 with the requested additions.
>
> If OK, somebody please provide for merging.

I would expect GregKH to merge it.

Acked-by: Randy Dunlap <randy....@oracle.com>

Thanks.


--

~Randy
*** Remember to use Documentation/SubmitChecklist when testing your code ***

Russell King - ARM Linux

unread,
Aug 15, 2010, 6:30:02 PM8/15/10
to
On Sun, Aug 15, 2010 at 03:02:57PM -0700, Randy Dunlap wrote:
> On 08/14/10 05:50, Claudio Scordino wrote:
> > diff --git a/Documentation/serial/serial-rs485.txt b/Documentation/serial/serial-rs485.txt
> > new file mode 100644
> > index 0000000..93b029e
> > --- /dev/null
> > +++ b/Documentation/serial/serial-rs485.txt
> > @@ -0,0 +1,126 @@
> > + RS485 SERIAL COMMUNICATIONS
> > +
> > +1. INTRODUCTION
> > +
> > + EIA-485, also known as TIA/EIA-485 or RS-485, is a standard defining the
> > + electrical characteristics of drivers and receivers for use in balanced
> > + digital multipoint systems.
> > + This standard is widely used for communications in industrial automation
> > + because it can be used effectively over long distances and in electrically
> > + noisy environments.
> > + Even though the data is transmitted over a 2-wire twisted pair bus, all
> > + EIA-485 transceivers interpret the voltage levels of the differential
> > + signals with respect to a third common voltage. Without this common
> > + reference, a set of transceivers may interpret the differential signals
> > + incorrectly.
> > + See [1] for more information.

There are devices on the market which are fully isolating RS485
transceivers which just take the A/B connections, measuring the
differential voltage, and provide you with the TXD/RXD TTL signals
for your UART.

Also note that [1] appears a little confused about the number of pins
required for RS485 - it says two, and then lists three pins. I don't
think you can use this as being supportive of the requirement for three
connections - and as there are these fully isolating transceivers...

Example code really should do things right, such as check for error
conditions - otherwise people will copy'n'paste this and assume that
the ioctl never fails.

> > +
> > + /* Use read() and write() syscalls here... */
> > +
> > + /* Close the device when finished: */
> > + close (fd);
> > +
> > +5. REFERENCES
> > +
> > + [1] http://en.wikipedia.org/wiki/Rs485
> > + [2] include/linux/serial.h
--

Claudio Scordino

unread,
Oct 19, 2010, 8:40:01 AM10/19/10
to
Russell King - ARM Linux ha scritto:

Here is the new document containing the changed you've proposed.

If OK, somebody please provide for commit (the merge window is opening...).

Cheers,

Claudio


Documentation about RS485 serial communications

Signed-off-by: Claudio Scordino <cla...@evidence.eu.com>


---
Documentation/serial/00-INDEX | 2 +
Documentation/serial/serial-rs485.txt | 126 +++++++++++++++++++++++++++++++++
2 files changed, 128 insertions(+), 0 deletions(-)
create mode 100644 Documentation/serial/serial-rs485.txt

diff --git a/Documentation/serial/00-INDEX b/Documentation/serial/00-INDEX
index 07dcdb0..e09468a 100644
--- a/Documentation/serial/00-INDEX
+++ b/Documentation/serial/00-INDEX
@@ -14,6 +14,8 @@ riscom8.txt
- notes on using the RISCom/8 multi-port serial driver.
rocket.txt
- info on the Comtrol RocketPort multiport serial driver.
+serial-rs485.txt
+ - info about RS485 structures and support in the kernel.
specialix.txt
- info on hardware/driver for specialix IO8+ multiport serial card.
stallion.txt

diff --git a/Documentation/serial/serial-rs485.txt b/Documentation/serial/serial-rs485.txt
new file mode 100644

index 0000000..82ebc86


--- /dev/null
+++ b/Documentation/serial/serial-rs485.txt
@@ -0,0 +1,126 @@
+ RS485 SERIAL COMMUNICATIONS
+
+1. INTRODUCTION
+
+ EIA-485, also known as TIA/EIA-485 or RS-485, is a standard defining the
+ electrical characteristics of drivers and receivers for use in balanced
+ digital multipoint systems.
+ This standard is widely used for communications in industrial automation
+ because it can be used effectively over long distances and in electrically
+ noisy environments.
+

+2. HARDWARE-RELATED CONSIDERATIONS
+
+ Some CPUs (e.g., Atmel AT91) contain a transceiver capable of working both
+ as RS232 and RS485. For these microcontrollers, the Linux driver should be
+ made capable of working in both modes, and proper ioctls (see later) should
+ be made available at user-level to allow switching from one mode to the
+ other, and vice versa.
+
+ On some other CPUs (e.g., Freescale imx25) the RS485 transceiver is not
+ integrated inside the microcontroller itself. Therefore, manufacturers who
+ use these microcontrollers to produce embedded boards need to connect an
+ external transceiver to some pin of the CPU.
+ On these architectures, therefore, no assumptions can be done at the
+ CPU-level about the presence of a RS485 transceiver, because the connection
+ (if any) is done outside the microcontroller. Moreover, even in case of
+ RS485 transceiver, the manufacturer is free to choose the CPU pin used for
+ the connection.
+
+
+3. DATA STRUCTURES ALREADY AVAILABLE IN THE KERNEL
+

+ The Linux kernel provides the serial_rs485 structure (see [1]) to handle


+ RS485 communications. This data structure is used to set and configure RS485
+ parameters in the platform data and in ioctls.
+
+ Any driver for devices capable of working both as RS232 and RS485 should
+ provide at least the following ioctls:
+
+ - TIOCSRS485 (typically associated with number 0x542F). This ioctl is used
+ to enable/disable RS485 mode from user-space
+
+ - TIOCGRS485 (typically associated with number 0x542E). This ioctl is used
+ to get RS485 mode from kernel-space (i.e., driver) to user-space.
+
+ In other words, the serial driver should contain a code similar to the next
+ one:
+
+ static struct uart_ops atmel_pops = {
+ /* ... */
+ .ioctl = handle_ioctl,
+ };
+
+ static int handle_ioctl(struct uart_port *port,
+ unsigned int cmd,
+ unsigned long arg)
+ {

+ struct serial_rs485 rs485conf;
+

+ switch (cmd) {
+ case TIOCSRS485:
+ if (copy_from_user(&rs485conf,
+ (struct serial_rs485 *) arg,
+ sizeof(rs485conf)))
+ return -EFAULT;
+
+ /* ... */
+ break;
+
+ case TIOCGRS485:
+ if (copy_to_user((struct serial_rs485 *) arg,
+ ...,
+ sizeof(rs485conf)))
+ return -EFAULT;
+ /* ... */
+ break;
+
+ /* ... */
+ }
+ }
+
+

+4. USAGE FROM USER-LEVEL
+
+ From user-level, RS485 configuration can be get/set using the previous
+ ioctls. For instance, to set RS485 you can use the following code:
+
+ #include <linux/serial.h>
+
+ /* Driver-specific ioctls: */
+ #define TIOCGRS485 0x542E
+ #define TIOCSRS485 0x542F
+
+ /* Open your specific device (e.g., /dev/mydevice): */
+ int fd = open ("/dev/mydevice", O_RDWR);

+ if (fd < 0) {
+ /* Error handling. See errno. */
+ }
+


+ struct serial_rs485 rs485conf;
+
+ /* Set RS485 mode: */
+ rs485conf.flags |= SER_RS485_ENABLED;
+
+ /* Set rts delay before send, if needed: */
+ rs485conf.flags |= SER_RS485_RTS_BEFORE_SEND;
+ rs485conf.delay_rts_before_send = ...;
+
+ /* Set rts delay after send, if needed: */
+ rs485conf.flags |= SER_RS485_RTS_AFTER_SEND;
+ rs485conf.delay_rts_after_send = ...;
+

+ if (ioctl (fd, TIOCSRS485, &rs485conf) < 0) {
+ /* Error handling. See errno. */
+ }


+
+ /* Use read() and write() syscalls here... */
+
+ /* Close the device when finished: */

+ if (close (fd) < 0) {
+ /* Error handling. See errno. */
+ }
+
+5. REFERENCES
+
+ [1] include/linux/serial.h

Grant Edwards

unread,
Oct 19, 2010, 10:40:02 AM10/19/10
to
On 2010-10-19, Claudio Scordino <cla...@evidence.eu.com> wrote:

> +2. HARDWARE-RELATED CONSIDERATIONS
> +
> + Some CPUs (e.g., Atmel AT91) contain a transceiver capable of working both
> + as RS232 and RS485.

Pardon my curiosity, but I've looked at a few AT91 parts, and I've
never seen on with an internal transceiver -- nor does Google seem
able to find any.

Frankly, I'd be pretty surprised, since it would be very difficult
(fab-wise), to build a mircoprocessor with I/O cells capable of
withstanding the voltage levels specified in RS232.

Can you specify to which parts you're referring?

--
Grant Edwards grant.b.edwards Yow! I have the power to
at HALT PRODUCTION on all
gmail.com TEENAGE SEX COMEDIES!!

Alexander Stein

unread,
Oct 19, 2010, 12:00:02 PM10/19/10
to
On Tuesday 19 October 2010, 16:29:48 Grant Edwards wrote:
> On 2010-10-19, Claudio Scordino <cla...@evidence.eu.com> wrote:
> > +2. HARDWARE-RELATED CONSIDERATIONS
> > +
> > + Some CPUs (e.g., Atmel AT91) contain a transceiver capable of working
> > both + as RS232 and RS485.
>
> Pardon my curiosity, but I've looked at a few AT91 parts, and I've
> never seen on with an internal transceiver -- nor does Google seem
> able to find any.
>
> Frankly, I'd be pretty surprised, since it would be very difficult
> (fab-wise), to build a mircoprocessor with I/O cells capable of
> withstanding the voltage levels specified in RS232.
>
> Can you specify to which parts you're referring?

I doubt the AT91 do have an internal transceiver but it has a dedicated pin
feature (RTS or CTS or something) which can be used to switch between RS232
and RS485 by setting a bit in the USART provided the pin is connected
properly.

Regards,
Alexander

Alexander Stein

unread,
Oct 20, 2010, 4:10:03 AM10/20/10
to
On Tuesday 19 October 2010, 19:22:32 Grant Edwards wrote:
> Yes, the AT91 UARTs do have a built-in half-duplex mode that will
> automatically control line direction by toggling RTS.
>
> That can used to control external half-duplex hardware like an RS485
> transceiver or any RS232-connected half-duplex device like some
> modems.
>
> Though the Atmel docs refer to it as "RS485 mode" there is no RS485
> transceiver in the AT91 parts (AFAIK), and "RS485 mode" isn't specific
> to RS485 - it can be used with any external half-duplex hardware.

Out of curiosity, how can I add RS485 (or whatever) support for other serial
devices like e.g. i.mx serial? Is there some driver independent interface
which only gets filled in board configs like setting some GPIO pins?

Alan Cox

unread,
Oct 20, 2010, 3:00:02 PM10/20/10
to
On Wed, 20 Oct 2010 10:00:14 +0200
Alexander Stein <alexand...@systec-electronic.com> wrote:

> On Tuesday 19 October 2010, 19:22:32 Grant Edwards wrote:
> > Yes, the AT91 UARTs do have a built-in half-duplex mode that will
> > automatically control line direction by toggling RTS.
> >
> > That can used to control external half-duplex hardware like an RS485
> > transceiver or any RS232-connected half-duplex device like some
> > modems.
> >
> > Though the Atmel docs refer to it as "RS485 mode" there is no RS485
> > transceiver in the AT91 parts (AFAIK), and "RS485 mode" isn't specific
> > to RS485 - it can be used with any external half-duplex hardware.
>
> Out of curiosity, how can I add RS485 (or whatever) support for other serial
> devices like e.g. i.mx serial? Is there some driver independent interface
> which only gets filled in board configs like setting some GPIO pins?

Several devices support RS485 and the like

There are two interfaces

1. termiox which is used in the old Unix world for sync and
other stuff on serial world. Linux now supports a bit of this interface
and has the flags so we can grow it as needed. It covers one way flow
control

2. TIOCGSRS485/TIOCSRS486 plus struct serial_rs485 which is Linux
specific but also allows for pre/post send delays in hardware that some
chips can do.

And nothing stops you supporting both interfaces when it makes sense

Claudio Scordino

unread,
Oct 24, 2010, 7:40:02 AM10/24/10
to
Grant Edwards ha scritto:

> On 2010-10-19, Alexander Stein <alexand...@systec-electronic.com> wrote:
>> On Tuesday 19 October 2010, 16:29:48 Grant Edwards wrote:
>>> On 2010-10-19, Claudio Scordino <cla...@evidence.eu.com> wrote:
>>>> +2. HARDWARE-RELATED CONSIDERATIONS
>>>> +
>>>> + Some CPUs (e.g., Atmel AT91) contain a transceiver capable of working
>>>> both + as RS232 and RS485.
>>> Pardon my curiosity, but I've looked at a few AT91 parts, and I've
>>> never seen on with an internal transceiver -- nor does Google seem
>>> able to find any.
>>>
>>> Frankly, I'd be pretty surprised, since it would be very difficult
>>> (fab-wise), to build a mircoprocessor with I/O cells capable of
>>> withstanding the voltage levels specified in RS232.
>>>
>>> Can you specify to which parts you're referring?
>> I doubt the AT91 do have an internal transceiver but it has a
>> dedicated pin feature (RTS or CTS or something) which can be used to
>> switch between RS232 and RS485 by setting a bit in the USART provided
>> the pin is connected properly.
>
> Yes, the AT91 UARTs do have a built-in half-duplex mode that will
> automatically control line direction by toggling RTS.
>
> That can used to control external half-duplex hardware like an RS485
> transceiver or any RS232-connected half-duplex device like some
> modems.
>
> Though the Atmel docs refer to it as "RS485 mode" there is no RS485
> transceiver in the AT91 parts (AFAIK), and "RS485 mode" isn't specific
> to RS485 - it can be used with any external half-duplex hardware.

Hi all,

sorry for the misuse of the term "transceiver", and thank you
for your corrections.

Hopefully, this new version of the document is slightly better.

Many thanks,

Claudio


Documentation about RS485 serial communications

Signed-off-by: Claudio Scordino <cla...@evidence.eu.com>
---
Documentation/serial/00-INDEX | 2 +

Documentation/serial/serial-rs485.txt | 119 +++++++++++++++++++++++++++++++++
2 files changed, 121 insertions(+), 0 deletions(-)
create mode 100644 Documentation/serial/serial-rs485.txt

diff --git a/Documentation/serial/00-INDEX b/Documentation/serial/00-INDEX
index 07dcdb0..e09468a 100644
--- a/Documentation/serial/00-INDEX
+++ b/Documentation/serial/00-INDEX
@@ -14,6 +14,8 @@ riscom8.txt
- notes on using the RISCom/8 multi-port serial driver.
rocket.txt
- info on the Comtrol RocketPort multiport serial driver.
+serial-rs485.txt
+ - info about RS485 structures and support in the kernel.
specialix.txt
- info on hardware/driver for specialix IO8+ multiport serial card.
stallion.txt
diff --git a/Documentation/serial/serial-rs485.txt b/Documentation/serial/serial-rs485.txt
new file mode 100644

index 0000000..40f09c6
--- /dev/null
+++ b/Documentation/serial/serial-rs485.txt
@@ -0,0 +1,119 @@


+ RS485 SERIAL COMMUNICATIONS
+
+1. INTRODUCTION
+
+ EIA-485, also known as TIA/EIA-485 or RS-485, is a standard defining the
+ electrical characteristics of drivers and receivers for use in balanced
+ digital multipoint systems.
+ This standard is widely used for communications in industrial automation
+ because it can be used effectively over long distances and in electrically
+ noisy environments.
+

+2. HARDWARE-RELATED CONSIDERATIONS
+
+ Some CPUs (e.g., Atmel AT91) contain a built-in half-duplex mode capable of
+ automatically controlling line direction by toggling RTS. That can used to
+ control external half-duplex hardware like an RS485 transceiver or any
+ RS232-connected half-duplex device like some modems.
+
+ For these microcontrollers, the Linux driver should be made capable of
+ working in both modes, and proper ioctls (see later) should be made
+ available at user-level to allow switching from one mode to the other, and
+ vice versa.

1.6.0.4

Nicolas Ferre

unread,
Nov 10, 2010, 4:20:02 AM11/10/10
to
Le 24/10/2010 13:29, Claudio Scordino :

[..]

> Hi all,
>
> sorry for the misuse of the term "transceiver", and thank you
> for your corrections.
>
> Hopefully, this new version of the document is slightly better.

Hi all,

This documentation is floating around for some time now. Claudio has
enhanced it and it is maybe time to merge it in mainline...

So, can serial people include it in their tree (Greg?) or should it go
through another path?

Thanks to all of you.

Best regards,


--
Nicolas Ferre

Greg KH

unread,
Nov 10, 2010, 12:40:02 PM11/10/10
to
On Wed, Nov 10, 2010 at 10:17:03AM +0100, Nicolas Ferre wrote:
> Le 24/10/2010 13:29, Claudio Scordino :
>
> [..]
>
> > Hi all,
> >
> > sorry for the misuse of the term "transceiver", and thank you
> > for your corrections.
> >
> > Hopefully, this new version of the document is slightly better.
>
> Hi all,
>
> This documentation is floating around for some time now. Claudio has
> enhanced it and it is maybe time to merge it in mainline...
>
> So, can serial people include it in their tree (Greg?) or should it go
> through another path?

I can take it, if someone resends it with the accumulated acks that I
think it gathered over the review cycle.

thanks,

greg k-h

Claudio Scordino

unread,
Nov 11, 2010, 5:30:02 AM11/11/10
to
Greg KH ha scritto:

> On Wed, Nov 10, 2010 at 10:17:03AM +0100, Nicolas Ferre wrote:
>> Le 24/10/2010 13:29, Claudio Scordino :
>>
>> [..]
>>
>>> Hi all,
>>>
>>> sorry for the misuse of the term "transceiver", and thank you
>>> for your corrections.
>>>
>>> Hopefully, this new version of the document is slightly better.
>> Hi all,
>>
>> This documentation is floating around for some time now. Claudio has
>> enhanced it and it is maybe time to merge it in mainline...
>>
>> So, can serial people include it in their tree (Greg?) or should it go
>> through another path?
>
> I can take it, if someone resends it with the accumulated acks that I
> think it gathered over the review cycle.

Here it is.

Thanks,

Claudio


Documentation about RS485 serial communications

Signed-off-by: Claudio Scordino <cla...@evidence.eu.com>
Acked-by: Randy Dunlap <randy....@oracle.com>
Acked-by: Russell King <li...@arm.linux.org.uk>
Acked-by: Grant Edwards <grant.b...@gmail.com>

1.6.0.4

Pavel Machek

unread,
Nov 16, 2010, 9:40:01 AM11/16/10
to
Hi!

> Documentation about RS485 serial communications

I have seen hardware (kontron pmc-6l) that was capable of switching
between RS232, RS485 and one other standard by software.

Is such hw common? If so, should we have standard interface?

> + From user-level, RS485 configuration can be get/set using the previous
> + ioctls. For instance, to set RS485 you can use the following code:
> +
> + #include <linux/serial.h>
> +
> + /* Driver-specific ioctls: */
> + #define TIOCGRS485 0x542E
> + #define TIOCSRS485 0x542F
> +
> + /* Open your specific device (e.g., /dev/mydevice): */
> + int fd = open ("/dev/mydevice", O_RDWR);
> + if (fd < 0) {
> + /* Error handling. See errno. */
> + }
> +
> + struct serial_rs485 rs485conf;
> +
> + /* Set RS485 mode: */
> + rs485conf.flags |= SER_RS485_ENABLED;

IOW... is this supposed to switch electrical levels?

--
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html

Alexander Stein

unread,
Nov 16, 2010, 10:40:02 AM11/16/10
to
Hello,

On Tuesday 16 November 2010, 15:30:16 Pavel Machek wrote:
> > Documentation about RS485 serial communications
>
> I have seen hardware (kontron pmc-6l) that was capable of switching
> between RS232, RS485 and one other standard by software.

Maybe you mean RS422 here.

> Is such hw common? If so, should we have standard interface?

Especially in industrial system, you may have RS485 or RS422 additionally to
RS232. This also often means switching some GPIO to change the external signal
levels. Any the drivers must also be capable of turning on and off the
transmitter.

Best regards,
Alexander

Matt Schulte

unread,
Nov 16, 2010, 11:30:02 AM11/16/10
to
On Tue, Nov 16, 2010 at 8:30 AM, Pavel Machek <pa...@ucw.cz> wrote:
> Hi!
>
>> Documentation about RS485 serial communications
>
> I have seen hardware (kontron pmc-6l) that was capable of switching
> between RS232, RS485 and one other standard by software.
>
> Is such hw common? If so, should we have standard interface?

In my opinion this type of card is not that common. Generally
speaking the achievable baud rates for this type of multi-protocol
card are very limited because of limitations of the transceiver chips.
It seems that most of the time people would rather have a faster
serial port than one that does several different voltages.

Matt Schulte

Alan Cox

unread,
Nov 16, 2010, 12:30:01 PM11/16/10
to
On Tue, 16 Nov 2010 10:13:22 -0600
Matt Schulte <ma...@commtech-fastcom.com> wrote:

> On Tue, Nov 16, 2010 at 8:30 AM, Pavel Machek <pa...@ucw.cz> wrote:
> > Hi!
> >
> >> Documentation about RS485 serial communications
> >
> > I have seen hardware (kontron pmc-6l) that was capable of switching
> > between RS232, RS485 and one other standard by software.
> >
> > Is such hw common? If so, should we have standard interface?
>
> In my opinion this type of card is not that common. Generally
> speaking the achievable baud rates for this type of multi-protocol
> card are very limited because of limitations of the transceiver chips.
> It seems that most of the time people would rather have a faster
> serial port than one that does several different voltages

If there are two types in common use then thats enough to say we should
have a common interface IMHO

Grant Edwards

unread,
Nov 16, 2010, 1:10:02 PM11/16/10
to
On 2010-11-16, Alan Cox <al...@lxorguk.ukuu.org.uk> wrote:
> On Tue, 16 Nov 2010 10:13:22 -0600
> Matt Schulte <ma...@commtech-fastcom.com> wrote:
>
>> On Tue, Nov 16, 2010 at 8:30 AM, Pavel Machek <pa...@ucw.cz> wrote:
>> > Hi!
>> >
>> >> Documentation about RS485 serial communications
>> >
>> > I have seen hardware (kontron pmc-6l) that was capable of switching
>> > between RS232, RS485 and one other standard by software.
>> >
>> > Is such hw common? If so, should we have standard interface?
>>
>> In my opinion this type of card is not that common. Generally
>> speaking the achievable baud rates for this type of multi-protocol
>> card are very limited because of limitations of the transceiver chips.
>> It seems that most of the time people would rather have a faster
>> serial port than one that does several different voltages
>
> If there are two types in common use then thats enough to say we should
> have a common interface IMHO

Comtrol, Moxa, B&B, Sealevel, and others all sell PCI cards and
Ethernet attached serial ports that have selectable interfaces
(typically RS-232/422/485). Comtrol and Moxa have had drivers in the
kernel tree for ages, but they've always had to use custom ioctl calls
for things like configuring 232/485/422 mode and half/full-duplex
mode.

There are also tons of small Linux-based industrial server appliances
from Comtrol, Silex, Digi, Moxa, and others that have selectable
interface serial ports.

Having a standard API for things like interface mode, half-full
duplex, inter-character timeout, 9-bit mode, and so on would be life a
lot easier for those of us who maintain Linux serial drivers...

--
Grant Edwards grant.b.edwards Yow! I always have fun
at because I'm out of my
gmail.com mind!!!

Matt Schulte

unread,
Nov 16, 2010, 1:50:02 PM11/16/10
to
On Tue, Nov 16, 2010 at 10:28 AM, Grant Edwards
<grant.b...@gmail.com> wrote:

> On 2010-11-16, Matt Schulte <ma...@commtech-fastcom.com> wrote:
>> On Tue, Nov 16, 2010 at 8:30 AM, Pavel Machek <pa...@ucw.cz> wrote:
>>> Hi!
>>>
>>>> Documentation about RS485 serial communications
>>>
>>> I have seen hardware (kontron pmc-6l) that was capable of switching
>>> between RS232, RS485 and one other standard by software.
>>>
>>> Is such hw common? If so, should we have standard interface?
>>
>> In my opinion this type of card is not that common.  Generally
>> speaking the achievable baud rates for this type of multi-protocol
>> card are very limited because of limitations of the transceiver chips.
>
> I'm curious which selectable interface cards you're talking about that
> are slow?  The ones I'm familiar with generally support baud rates up
> to either 460K bps or 921K bps

>
>> It seems that most of the time people would rather have a faster
>> serial port than one that does several different voltages.
>
> Where did you find a selectable interface serial card that couldn't
> support high baud rates?

In my world 460kbps for an RS422 card is slow. RS422 cards generally
push multi megabit/s rates.

Alan Cox

unread,
Nov 16, 2010, 3:10:02 PM11/16/10
to
> Comtrol, Moxa, B&B, Sealevel, and others all sell PCI cards and
> Ethernet attached serial ports that have selectable interfaces
> (typically RS-232/422/485). Comtrol and Moxa have had drivers in the
> kernel tree for ages, but they've always had to use custom ioctl calls
> for things like configuring 232/485/422 mode and half/full-duplex
> mode.

Ok so what is needed that isn't covered by the current termiox and
TIOCG/SRS485 ioctls ?

> Having a standard API for things like interface mode, half-full
> duplex, inter-character timeout, 9-bit mode, and so on would be life a
> lot easier for those of us who maintain Linux serial drivers...

9bit mode is a real problem with the tty layer defined in terms of 8bit
streams but yes.

Pavel Machek

unread,
Nov 30, 2010, 2:20:03 PM11/30/10
to
On Tue 2010-11-16 20:04:26, Alan Cox wrote:
> > Comtrol, Moxa, B&B, Sealevel, and others all sell PCI cards and
> > Ethernet attached serial ports that have selectable interfaces
> > (typically RS-232/422/485). Comtrol and Moxa have had drivers in the
> > kernel tree for ages, but they've always had to use custom ioctl calls
> > for things like configuring 232/485/422 mode and half/full-duplex
> > mode.
>
> Ok so what is needed that isn't covered by the current termiox and
> TIOCG/SRS485 ioctls ?

IIRC selection of 232/485/422 mode was missing from proposed api.

> > Having a standard API for things like interface mode, half-full
> > duplex, inter-character timeout, 9-bit mode, and so on would be life a
> > lot easier for those of us who maintain Linux serial drivers...
>
> 9bit mode is a real problem with the tty layer defined in terms of 8bit
> streams but yes.

Well, if you wanted to have some fun... kontron card is able to
timestamp incoming characters, and saves state of the control signals
when character is received...
Pavel

0 new messages