blob: d9ad19f0931078fdff529356220107b3dff04870 [file] [log] [blame]
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001/*
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002 * Driver for Atmel AT91 / AT32 Serial ports
Andrew Victor1e6c9c22006-01-10 16:59:27 +00003 * Copyright (C) 2003 Rick Bronson
4 *
5 * Based on drivers/char/serial_sa1100.c, by Deep Blue Solutions Ltd.
6 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
7 *
Chip Coldwella6670612008-02-08 04:21:06 -08008 * DMA support added by Chip Coldwell.
9 *
Andrew Victor1e6c9c22006-01-10 16:59:27 +000010 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 *
24 */
Andrew Victor1e6c9c22006-01-10 16:59:27 +000025#include <linux/module.h>
26#include <linux/tty.h>
27#include <linux/ioport.h>
28#include <linux/slab.h>
29#include <linux/init.h>
30#include <linux/serial.h>
Andrew Victorafefc412006-06-19 19:53:19 +010031#include <linux/clk.h>
Andrew Victor1e6c9c22006-01-10 16:59:27 +000032#include <linux/console.h>
33#include <linux/sysrq.h>
34#include <linux/tty_flip.h>
Andrew Victorafefc412006-06-19 19:53:19 +010035#include <linux/platform_device.h>
Nicolas Ferre5fbe46b2011-10-12 18:07:00 +020036#include <linux/of.h>
37#include <linux/of_device.h>
Chip Coldwella6670612008-02-08 04:21:06 -080038#include <linux/dma-mapping.h>
Andrew Victor93a3ddc2007-02-08 11:31:22 +010039#include <linux/atmel_pdc.h>
Guennadi Liakhovetskifa3218d2008-01-29 15:43:13 +010040#include <linux/atmel_serial.h>
Claudio Scordinoe8faff72010-05-03 13:31:28 +010041#include <linux/uaccess.h>
Jean-Christophe PLAGNIOL-VILLARDbcd23602012-10-30 05:12:23 +080042#include <linux/platform_data/atmel.h>
Elen Song2e68c222013-07-22 16:30:30 +080043#include <linux/timer.h>
Andrew Victor1e6c9c22006-01-10 16:59:27 +000044
45#include <asm/io.h>
Peter Huewef7512e72010-06-29 19:35:39 +020046#include <asm/ioctls.h>
Andrew Victor1e6c9c22006-01-10 16:59:27 +000047
Haavard Skinnemoenacca9b82006-10-04 16:02:09 +020048#ifdef CONFIG_ARM
Russell Kinga09e64f2008-08-05 16:14:15 +010049#include <mach/cpu.h>
Russell King60e89722011-07-26 10:56:19 +010050#include <asm/gpio.h>
Haavard Skinnemoenacca9b82006-10-04 16:02:09 +020051#endif
Andrew Victor1e6c9c22006-01-10 16:59:27 +000052
Chip Coldwella6670612008-02-08 04:21:06 -080053#define PDC_BUFFER_SIZE 512
54/* Revisit: We should calculate this based on the actual port settings */
55#define PDC_RX_TIMEOUT (3 * 10) /* 3 bytes */
56
Haavard Skinnemoen749c4e62006-10-04 16:02:02 +020057#if defined(CONFIG_SERIAL_ATMEL_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
Andrew Victor1e6c9c22006-01-10 16:59:27 +000058#define SUPPORT_SYSRQ
59#endif
60
61#include <linux/serial_core.h>
62
Claudio Scordinoe8faff72010-05-03 13:31:28 +010063static void atmel_start_rx(struct uart_port *port);
64static void atmel_stop_rx(struct uart_port *port);
65
Haavard Skinnemoen749c4e62006-10-04 16:02:02 +020066#ifdef CONFIG_SERIAL_ATMEL_TTYAT
Andrew Victor1e6c9c22006-01-10 16:59:27 +000067
68/* Use device name ttyAT, major 204 and minor 154-169. This is necessary if we
69 * should coexist with the 8250 driver, such as if we have an external 16C550
70 * UART. */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +020071#define SERIAL_ATMEL_MAJOR 204
Andrew Victor1e6c9c22006-01-10 16:59:27 +000072#define MINOR_START 154
Haavard Skinnemoen7192f922006-10-04 16:02:05 +020073#define ATMEL_DEVICENAME "ttyAT"
Andrew Victor1e6c9c22006-01-10 16:59:27 +000074
75#else
76
77/* Use device name ttyS, major 4, minor 64-68. This is the usual serial port
78 * name, but it is legally reserved for the 8250 driver. */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +020079#define SERIAL_ATMEL_MAJOR TTY_MAJOR
Andrew Victor1e6c9c22006-01-10 16:59:27 +000080#define MINOR_START 64
Haavard Skinnemoen7192f922006-10-04 16:02:05 +020081#define ATMEL_DEVICENAME "ttyS"
Andrew Victor1e6c9c22006-01-10 16:59:27 +000082
83#endif
84
Haavard Skinnemoen7192f922006-10-04 16:02:05 +020085#define ATMEL_ISR_PASS_LIMIT 256
Andrew Victor1e6c9c22006-01-10 16:59:27 +000086
Remy Bohmerb843aa22008-02-08 04:21:01 -080087/* UART registers. CR is write-only, hence no GET macro */
Haavard Skinnemoen544fc722007-02-12 00:52:15 -080088#define UART_PUT_CR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_CR)
89#define UART_GET_MR(port) __raw_readl((port)->membase + ATMEL_US_MR)
90#define UART_PUT_MR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_MR)
91#define UART_PUT_IER(port,v) __raw_writel(v, (port)->membase + ATMEL_US_IER)
92#define UART_PUT_IDR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_IDR)
93#define UART_GET_IMR(port) __raw_readl((port)->membase + ATMEL_US_IMR)
94#define UART_GET_CSR(port) __raw_readl((port)->membase + ATMEL_US_CSR)
95#define UART_GET_CHAR(port) __raw_readl((port)->membase + ATMEL_US_RHR)
96#define UART_PUT_CHAR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_THR)
97#define UART_GET_BRGR(port) __raw_readl((port)->membase + ATMEL_US_BRGR)
98#define UART_PUT_BRGR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_BRGR)
99#define UART_PUT_RTOR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_RTOR)
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100100#define UART_PUT_TTGR(port, v) __raw_writel(v, (port)->membase + ATMEL_US_TTGR)
Elen Song055560b2013-07-22 16:30:29 +0800101#define UART_GET_IP_NAME(port) __raw_readl((port)->membase + ATMEL_US_NAME)
Nicolas Ferre731d9ca2013-10-17 17:37:12 +0200102#define UART_GET_IP_VERSION(port) __raw_readl((port)->membase + ATMEL_US_VERSION)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000103
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000104 /* PDC registers */
Haavard Skinnemoen544fc722007-02-12 00:52:15 -0800105#define UART_PUT_PTCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_PTCR)
106#define UART_GET_PTSR(port) __raw_readl((port)->membase + ATMEL_PDC_PTSR)
Andrew Victorafefc412006-06-19 19:53:19 +0100107
Haavard Skinnemoen544fc722007-02-12 00:52:15 -0800108#define UART_PUT_RPR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RPR)
109#define UART_GET_RPR(port) __raw_readl((port)->membase + ATMEL_PDC_RPR)
110#define UART_PUT_RCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RCR)
111#define UART_PUT_RNPR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RNPR)
112#define UART_PUT_RNCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RNCR)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000113
Haavard Skinnemoen544fc722007-02-12 00:52:15 -0800114#define UART_PUT_TPR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_TPR)
115#define UART_PUT_TCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_TCR)
Marc Pignat39d4c922008-04-02 13:04:42 -0700116#define UART_GET_TCR(port) __raw_readl((port)->membase + ATMEL_PDC_TCR)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000117
Haavard Skinnemoen71f2e2b2006-10-04 16:02:07 +0200118static int (*atmel_open_hook)(struct uart_port *);
119static void (*atmel_close_hook)(struct uart_port *);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000120
Chip Coldwella6670612008-02-08 04:21:06 -0800121struct atmel_dma_buffer {
122 unsigned char *buf;
123 dma_addr_t dma_addr;
124 unsigned int dma_size;
125 unsigned int ofs;
126};
127
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800128struct atmel_uart_char {
129 u16 status;
130 u16 ch;
131};
132
133#define ATMEL_SERIAL_RINGSIZE 1024
134
Andrew Victorafefc412006-06-19 19:53:19 +0100135/*
136 * We wrap our port structure around the generic uart_port.
137 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200138struct atmel_uart_port {
Andrew Victorafefc412006-06-19 19:53:19 +0100139 struct uart_port uart; /* uart */
140 struct clk *clk; /* uart clock */
Anti Sullinf05596d2008-09-22 13:57:54 -0700141 int may_wakeup; /* cached value of device_may_wakeup for times we need to disable it */
142 u32 backup_imr; /* IMR saved during suspend */
Haavard Skinnemoen9e6077b2007-07-15 23:40:36 -0700143 int break_active; /* break being received */
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800144
Elen Song34df42f2013-07-22 16:30:27 +0800145 bool use_dma_rx; /* enable DMA receiver */
Elen Song64e22eb2013-07-22 16:30:24 +0800146 bool use_pdc_rx; /* enable PDC receiver */
Chip Coldwella6670612008-02-08 04:21:06 -0800147 short pdc_rx_idx; /* current PDC RX buffer */
148 struct atmel_dma_buffer pdc_rx[2]; /* PDC receier */
149
Elen Song08f738b2013-07-22 16:30:26 +0800150 bool use_dma_tx; /* enable DMA transmitter */
Elen Song64e22eb2013-07-22 16:30:24 +0800151 bool use_pdc_tx; /* enable PDC transmitter */
Chip Coldwella6670612008-02-08 04:21:06 -0800152 struct atmel_dma_buffer pdc_tx; /* PDC transmitter */
153
Elen Song08f738b2013-07-22 16:30:26 +0800154 spinlock_t lock_tx; /* port lock */
Elen Song34df42f2013-07-22 16:30:27 +0800155 spinlock_t lock_rx; /* port lock */
Elen Song08f738b2013-07-22 16:30:26 +0800156 struct dma_chan *chan_tx;
Elen Song34df42f2013-07-22 16:30:27 +0800157 struct dma_chan *chan_rx;
Elen Song08f738b2013-07-22 16:30:26 +0800158 struct dma_async_tx_descriptor *desc_tx;
Elen Song34df42f2013-07-22 16:30:27 +0800159 struct dma_async_tx_descriptor *desc_rx;
Elen Song08f738b2013-07-22 16:30:26 +0800160 dma_cookie_t cookie_tx;
Elen Song34df42f2013-07-22 16:30:27 +0800161 dma_cookie_t cookie_rx;
Elen Song08f738b2013-07-22 16:30:26 +0800162 struct scatterlist sg_tx;
Elen Song34df42f2013-07-22 16:30:27 +0800163 struct scatterlist sg_rx;
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800164 struct tasklet_struct tasklet;
165 unsigned int irq_status;
166 unsigned int irq_status_prev;
167
168 struct circ_buf rx_ring;
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100169
170 struct serial_rs485 rs485; /* rs485 settings */
171 unsigned int tx_done_mask;
Elen Song055560b2013-07-22 16:30:29 +0800172 bool is_usart; /* usart or uart */
Elen Song2e68c222013-07-22 16:30:30 +0800173 struct timer_list uart_timer; /* uart timer */
Elen Songa930e522013-07-22 16:30:25 +0800174 int (*prepare_rx)(struct uart_port *port);
175 int (*prepare_tx)(struct uart_port *port);
176 void (*schedule_rx)(struct uart_port *port);
177 void (*schedule_tx)(struct uart_port *port);
178 void (*release_rx)(struct uart_port *port);
179 void (*release_tx)(struct uart_port *port);
Andrew Victorafefc412006-06-19 19:53:19 +0100180};
181
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200182static struct atmel_uart_port atmel_ports[ATMEL_MAX_UART];
Pawel Wieczorkiewicz503bded2013-02-20 17:26:20 +0100183static DECLARE_BITMAP(atmel_ports_in_use, ATMEL_MAX_UART);
Andrew Victorafefc412006-06-19 19:53:19 +0100184
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000185#ifdef SUPPORT_SYSRQ
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200186static struct console atmel_console;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000187#endif
188
Nicolas Ferre5fbe46b2011-10-12 18:07:00 +0200189#if defined(CONFIG_OF)
190static const struct of_device_id atmel_serial_dt_ids[] = {
191 { .compatible = "atmel,at91rm9200-usart" },
192 { .compatible = "atmel,at91sam9260-usart" },
193 { /* sentinel */ }
194};
195
196MODULE_DEVICE_TABLE(of, atmel_serial_dt_ids);
197#endif
198
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -0800199static inline struct atmel_uart_port *
200to_atmel_uart_port(struct uart_port *uart)
201{
202 return container_of(uart, struct atmel_uart_port, uart);
203}
204
Chip Coldwella6670612008-02-08 04:21:06 -0800205#ifdef CONFIG_SERIAL_ATMEL_PDC
Elen Song64e22eb2013-07-22 16:30:24 +0800206static bool atmel_use_pdc_rx(struct uart_port *port)
Chip Coldwella6670612008-02-08 04:21:06 -0800207{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -0800208 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Chip Coldwella6670612008-02-08 04:21:06 -0800209
Elen Song64e22eb2013-07-22 16:30:24 +0800210 return atmel_port->use_pdc_rx;
Chip Coldwella6670612008-02-08 04:21:06 -0800211}
212
Elen Song64e22eb2013-07-22 16:30:24 +0800213static bool atmel_use_pdc_tx(struct uart_port *port)
Chip Coldwella6670612008-02-08 04:21:06 -0800214{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -0800215 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Chip Coldwella6670612008-02-08 04:21:06 -0800216
Elen Song64e22eb2013-07-22 16:30:24 +0800217 return atmel_port->use_pdc_tx;
Chip Coldwella6670612008-02-08 04:21:06 -0800218}
219#else
Elen Song64e22eb2013-07-22 16:30:24 +0800220static bool atmel_use_pdc_rx(struct uart_port *port)
Chip Coldwella6670612008-02-08 04:21:06 -0800221{
222 return false;
223}
224
Elen Song64e22eb2013-07-22 16:30:24 +0800225static bool atmel_use_pdc_tx(struct uart_port *port)
Chip Coldwella6670612008-02-08 04:21:06 -0800226{
227 return false;
228}
229#endif
230
Elen Song08f738b2013-07-22 16:30:26 +0800231static bool atmel_use_dma_tx(struct uart_port *port)
232{
233 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
234
235 return atmel_port->use_dma_tx;
236}
237
Elen Song34df42f2013-07-22 16:30:27 +0800238static bool atmel_use_dma_rx(struct uart_port *port)
239{
240 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
241
242 return atmel_port->use_dma_rx;
243}
244
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100245/* Enable or disable the rs485 support */
246void atmel_config_rs485(struct uart_port *port, struct serial_rs485 *rs485conf)
247{
248 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
249 unsigned int mode;
Claudio Scordinodbf11152011-12-16 15:08:49 +0100250 unsigned long flags;
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100251
Claudio Scordinodbf11152011-12-16 15:08:49 +0100252 spin_lock_irqsave(&port->lock, flags);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100253
254 /* Disable interrupts */
255 UART_PUT_IDR(port, atmel_port->tx_done_mask);
256
257 mode = UART_GET_MR(port);
258
259 /* Resetting serial mode to RS232 (0x0) */
260 mode &= ~ATMEL_US_USMODE;
261
262 atmel_port->rs485 = *rs485conf;
263
264 if (rs485conf->flags & SER_RS485_ENABLED) {
265 dev_dbg(port->dev, "Setting UART to RS485\n");
266 atmel_port->tx_done_mask = ATMEL_US_TXEMPTY;
Claudio Scordino93f33502011-11-09 15:51:49 +0100267 if ((rs485conf->delay_rts_after_send) > 0)
Claudio Scordino1b633182010-07-20 15:26:47 -0700268 UART_PUT_TTGR(port, rs485conf->delay_rts_after_send);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100269 mode |= ATMEL_US_USMODE_RS485;
270 } else {
271 dev_dbg(port->dev, "Setting UART to RS232\n");
Elen Song64e22eb2013-07-22 16:30:24 +0800272 if (atmel_use_pdc_tx(port))
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100273 atmel_port->tx_done_mask = ATMEL_US_ENDTX |
274 ATMEL_US_TXBUFE;
275 else
276 atmel_port->tx_done_mask = ATMEL_US_TXRDY;
277 }
278 UART_PUT_MR(port, mode);
279
280 /* Enable interrupts */
281 UART_PUT_IER(port, atmel_port->tx_done_mask);
282
Claudio Scordinodbf11152011-12-16 15:08:49 +0100283 spin_unlock_irqrestore(&port->lock, flags);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100284
285}
286
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000287/*
288 * Return TIOCSER_TEMT when transmitter FIFO and Shift register is empty.
289 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200290static u_int atmel_tx_empty(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000291{
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200292 return (UART_GET_CSR(port) & ATMEL_US_TXEMPTY) ? TIOCSER_TEMT : 0;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000293}
294
295/*
296 * Set state of the modem control output lines
297 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200298static void atmel_set_mctrl(struct uart_port *port, u_int mctrl)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000299{
300 unsigned int control = 0;
Andrew Victorafefc412006-06-19 19:53:19 +0100301 unsigned int mode;
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100302 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000303
Andrew Victorc2f5ccf2006-12-04 09:37:20 +0100304#ifdef CONFIG_ARCH_AT91RM9200
Andrew Victor79da7a62006-11-30 11:58:14 +0100305 if (cpu_is_at91rm9200()) {
Andrew Victorafefc412006-06-19 19:53:19 +0100306 /*
Remy Bohmerb843aa22008-02-08 04:21:01 -0800307 * AT91RM9200 Errata #39: RTS0 is not internally connected
308 * to PA21. We need to drive the pin manually.
Andrew Victorafefc412006-06-19 19:53:19 +0100309 */
Andrew Victor72729912006-09-27 09:44:11 +0100310 if (port->mapbase == AT91RM9200_BASE_US0) {
Andrew Victorafefc412006-06-19 19:53:19 +0100311 if (mctrl & TIOCM_RTS)
Andrew Victor20e65272006-07-05 14:31:13 +0100312 at91_set_gpio_value(AT91_PIN_PA21, 0);
Andrew Victorafefc412006-06-19 19:53:19 +0100313 else
Andrew Victor20e65272006-07-05 14:31:13 +0100314 at91_set_gpio_value(AT91_PIN_PA21, 1);
Andrew Victorafefc412006-06-19 19:53:19 +0100315 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000316 }
Haavard Skinnemoenacca9b82006-10-04 16:02:09 +0200317#endif
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000318
319 if (mctrl & TIOCM_RTS)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200320 control |= ATMEL_US_RTSEN;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000321 else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200322 control |= ATMEL_US_RTSDIS;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000323
324 if (mctrl & TIOCM_DTR)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200325 control |= ATMEL_US_DTREN;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000326 else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200327 control |= ATMEL_US_DTRDIS;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000328
Andrew Victorafefc412006-06-19 19:53:19 +0100329 UART_PUT_CR(port, control);
330
331 /* Local loopback mode? */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200332 mode = UART_GET_MR(port) & ~ATMEL_US_CHMODE;
Andrew Victorafefc412006-06-19 19:53:19 +0100333 if (mctrl & TIOCM_LOOP)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200334 mode |= ATMEL_US_CHMODE_LOC_LOOP;
Andrew Victorafefc412006-06-19 19:53:19 +0100335 else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200336 mode |= ATMEL_US_CHMODE_NORMAL;
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100337
338 /* Resetting serial mode to RS232 (0x0) */
339 mode &= ~ATMEL_US_USMODE;
340
341 if (atmel_port->rs485.flags & SER_RS485_ENABLED) {
342 dev_dbg(port->dev, "Setting UART to RS485\n");
Claudio Scordino93f33502011-11-09 15:51:49 +0100343 if ((atmel_port->rs485.delay_rts_after_send) > 0)
Claudio Scordino1b633182010-07-20 15:26:47 -0700344 UART_PUT_TTGR(port,
345 atmel_port->rs485.delay_rts_after_send);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100346 mode |= ATMEL_US_USMODE_RS485;
347 } else {
348 dev_dbg(port->dev, "Setting UART to RS232\n");
349 }
Andrew Victorafefc412006-06-19 19:53:19 +0100350 UART_PUT_MR(port, mode);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000351}
352
353/*
354 * Get state of the modem control input lines
355 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200356static u_int atmel_get_mctrl(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000357{
358 unsigned int status, ret = 0;
359
360 status = UART_GET_CSR(port);
361
362 /*
363 * The control signals are active low.
364 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200365 if (!(status & ATMEL_US_DCD))
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000366 ret |= TIOCM_CD;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200367 if (!(status & ATMEL_US_CTS))
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000368 ret |= TIOCM_CTS;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200369 if (!(status & ATMEL_US_DSR))
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000370 ret |= TIOCM_DSR;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200371 if (!(status & ATMEL_US_RI))
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000372 ret |= TIOCM_RI;
373
374 return ret;
375}
376
377/*
378 * Stop transmitting.
379 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200380static void atmel_stop_tx(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000381{
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100382 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
383
Elen Song64e22eb2013-07-22 16:30:24 +0800384 if (atmel_use_pdc_tx(port)) {
Chip Coldwella6670612008-02-08 04:21:06 -0800385 /* disable PDC transmit */
386 UART_PUT_PTCR(port, ATMEL_PDC_TXTDIS);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100387 }
388 /* Disable interrupts */
389 UART_PUT_IDR(port, atmel_port->tx_done_mask);
390
Bernhard Roth83cac9f2011-08-24 09:48:23 +0200391 if ((atmel_port->rs485.flags & SER_RS485_ENABLED) &&
392 !(atmel_port->rs485.flags & SER_RS485_RX_DURING_TX))
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100393 atmel_start_rx(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000394}
395
396/*
397 * Start transmitting.
398 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200399static void atmel_start_tx(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000400{
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100401 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
402
Elen Song64e22eb2013-07-22 16:30:24 +0800403 if (atmel_use_pdc_tx(port)) {
Chip Coldwella6670612008-02-08 04:21:06 -0800404 if (UART_GET_PTSR(port) & ATMEL_PDC_TXTEN)
405 /* The transmitter is already running. Yes, we
406 really need this.*/
407 return;
408
Bernhard Roth83cac9f2011-08-24 09:48:23 +0200409 if ((atmel_port->rs485.flags & SER_RS485_ENABLED) &&
410 !(atmel_port->rs485.flags & SER_RS485_RX_DURING_TX))
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100411 atmel_stop_rx(port);
412
Chip Coldwella6670612008-02-08 04:21:06 -0800413 /* re-enable PDC transmit */
414 UART_PUT_PTCR(port, ATMEL_PDC_TXTEN);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100415 }
416 /* Enable interrupts */
417 UART_PUT_IER(port, atmel_port->tx_done_mask);
418}
419
420/*
421 * start receiving - port is in process of being opened.
422 */
423static void atmel_start_rx(struct uart_port *port)
424{
425 UART_PUT_CR(port, ATMEL_US_RSTSTA); /* reset status and receiver */
426
Siftar, Gabe57c36862012-03-29 15:40:05 +0200427 UART_PUT_CR(port, ATMEL_US_RXEN);
428
Elen Song64e22eb2013-07-22 16:30:24 +0800429 if (atmel_use_pdc_rx(port)) {
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100430 /* enable PDC controller */
431 UART_PUT_IER(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT |
432 port->read_status_mask);
433 UART_PUT_PTCR(port, ATMEL_PDC_RXTEN);
434 } else {
435 UART_PUT_IER(port, ATMEL_US_RXRDY);
436 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000437}
438
439/*
440 * Stop receiving - port is in process of being closed.
441 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200442static void atmel_stop_rx(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000443{
Siftar, Gabe57c36862012-03-29 15:40:05 +0200444 UART_PUT_CR(port, ATMEL_US_RXDIS);
445
Elen Song64e22eb2013-07-22 16:30:24 +0800446 if (atmel_use_pdc_rx(port)) {
Chip Coldwella6670612008-02-08 04:21:06 -0800447 /* disable PDC receive */
448 UART_PUT_PTCR(port, ATMEL_PDC_RXTDIS);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100449 UART_PUT_IDR(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT |
450 port->read_status_mask);
451 } else {
Chip Coldwella6670612008-02-08 04:21:06 -0800452 UART_PUT_IDR(port, ATMEL_US_RXRDY);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100453 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000454}
455
456/*
457 * Enable modem status interrupts
458 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200459static void atmel_enable_ms(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000460{
Remy Bohmerb843aa22008-02-08 04:21:01 -0800461 UART_PUT_IER(port, ATMEL_US_RIIC | ATMEL_US_DSRIC
462 | ATMEL_US_DCDIC | ATMEL_US_CTSIC);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000463}
464
465/*
466 * Control the transmission of a break signal
467 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200468static void atmel_break_ctl(struct uart_port *port, int break_state)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000469{
470 if (break_state != 0)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200471 UART_PUT_CR(port, ATMEL_US_STTBRK); /* start break */
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000472 else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200473 UART_PUT_CR(port, ATMEL_US_STPBRK); /* stop break */
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000474}
475
476/*
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800477 * Stores the incoming character in the ring buffer
478 */
479static void
480atmel_buffer_rx_char(struct uart_port *port, unsigned int status,
481 unsigned int ch)
482{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -0800483 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800484 struct circ_buf *ring = &atmel_port->rx_ring;
485 struct atmel_uart_char *c;
486
487 if (!CIRC_SPACE(ring->head, ring->tail, ATMEL_SERIAL_RINGSIZE))
488 /* Buffer overflow, ignore char */
489 return;
490
491 c = &((struct atmel_uart_char *)ring->buf)[ring->head];
492 c->status = status;
493 c->ch = ch;
494
495 /* Make sure the character is stored before we update head. */
496 smp_wmb();
497
498 ring->head = (ring->head + 1) & (ATMEL_SERIAL_RINGSIZE - 1);
499}
500
501/*
Chip Coldwella6670612008-02-08 04:21:06 -0800502 * Deal with parity, framing and overrun errors.
503 */
504static void atmel_pdc_rxerr(struct uart_port *port, unsigned int status)
505{
506 /* clear error */
507 UART_PUT_CR(port, ATMEL_US_RSTSTA);
508
509 if (status & ATMEL_US_RXBRK) {
510 /* ignore side-effect */
511 status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME);
512 port->icount.brk++;
513 }
514 if (status & ATMEL_US_PARE)
515 port->icount.parity++;
516 if (status & ATMEL_US_FRAME)
517 port->icount.frame++;
518 if (status & ATMEL_US_OVRE)
519 port->icount.overrun++;
520}
521
522/*
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000523 * Characters received (called from interrupt handler)
524 */
David Howells7d12e782006-10-05 14:55:46 +0100525static void atmel_rx_chars(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000526{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -0800527 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800528 unsigned int status, ch;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000529
Andrew Victorafefc412006-06-19 19:53:19 +0100530 status = UART_GET_CSR(port);
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200531 while (status & ATMEL_US_RXRDY) {
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000532 ch = UART_GET_CHAR(port);
533
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000534 /*
535 * note that the error handling code is
536 * out of the main execution path
537 */
Haavard Skinnemoen9e6077b2007-07-15 23:40:36 -0700538 if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME
539 | ATMEL_US_OVRE | ATMEL_US_RXBRK)
540 || atmel_port->break_active)) {
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800541
Remy Bohmerb843aa22008-02-08 04:21:01 -0800542 /* clear error */
543 UART_PUT_CR(port, ATMEL_US_RSTSTA);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800544
Haavard Skinnemoen9e6077b2007-07-15 23:40:36 -0700545 if (status & ATMEL_US_RXBRK
546 && !atmel_port->break_active) {
Haavard Skinnemoen9e6077b2007-07-15 23:40:36 -0700547 atmel_port->break_active = 1;
548 UART_PUT_IER(port, ATMEL_US_RXBRK);
Haavard Skinnemoen9e6077b2007-07-15 23:40:36 -0700549 } else {
550 /*
551 * This is either the end-of-break
552 * condition or we've received at
553 * least one character without RXBRK
554 * being set. In both cases, the next
555 * RXBRK will indicate start-of-break.
556 */
557 UART_PUT_IDR(port, ATMEL_US_RXBRK);
558 status &= ~ATMEL_US_RXBRK;
559 atmel_port->break_active = 0;
Andrew Victorafefc412006-06-19 19:53:19 +0100560 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000561 }
562
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800563 atmel_buffer_rx_char(port, status, ch);
Andrew Victorafefc412006-06-19 19:53:19 +0100564 status = UART_GET_CSR(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000565 }
566
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800567 tasklet_schedule(&atmel_port->tasklet);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000568}
569
570/*
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800571 * Transmit characters (called from tasklet with TXRDY interrupt
572 * disabled)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000573 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200574static void atmel_tx_chars(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000575{
Alan Coxebd2c8f2009-09-19 13:13:28 -0700576 struct circ_buf *xmit = &port->state->xmit;
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100577 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000578
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100579 if (port->x_char && UART_GET_CSR(port) & atmel_port->tx_done_mask) {
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000580 UART_PUT_CHAR(port, port->x_char);
581 port->icount.tx++;
582 port->x_char = 0;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000583 }
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800584 if (uart_circ_empty(xmit) || uart_tx_stopped(port))
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000585 return;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000586
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100587 while (UART_GET_CSR(port) & atmel_port->tx_done_mask) {
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000588 UART_PUT_CHAR(port, xmit->buf[xmit->tail]);
589 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
590 port->icount.tx++;
591 if (uart_circ_empty(xmit))
592 break;
593 }
594
595 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
596 uart_write_wakeup(port);
597
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800598 if (!uart_circ_empty(xmit))
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100599 /* Enable interrupts */
600 UART_PUT_IER(port, atmel_port->tx_done_mask);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000601}
602
Elen Song08f738b2013-07-22 16:30:26 +0800603static void atmel_complete_tx_dma(void *arg)
604{
605 struct atmel_uart_port *atmel_port = arg;
606 struct uart_port *port = &atmel_port->uart;
607 struct circ_buf *xmit = &port->state->xmit;
608 struct dma_chan *chan = atmel_port->chan_tx;
609 unsigned long flags;
610
611 spin_lock_irqsave(&port->lock, flags);
612
613 if (chan)
614 dmaengine_terminate_all(chan);
615 xmit->tail += sg_dma_len(&atmel_port->sg_tx);
616 xmit->tail &= UART_XMIT_SIZE - 1;
617
618 port->icount.tx += sg_dma_len(&atmel_port->sg_tx);
619
620 spin_lock_irq(&atmel_port->lock_tx);
621 async_tx_ack(atmel_port->desc_tx);
622 atmel_port->cookie_tx = -EINVAL;
623 atmel_port->desc_tx = NULL;
624 spin_unlock_irq(&atmel_port->lock_tx);
625
626 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
627 uart_write_wakeup(port);
628
629 /* Do we really need this? */
630 if (!uart_circ_empty(xmit))
631 tasklet_schedule(&atmel_port->tasklet);
632
633 spin_unlock_irqrestore(&port->lock, flags);
634}
635
636static void atmel_release_tx_dma(struct uart_port *port)
637{
638 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
639 struct dma_chan *chan = atmel_port->chan_tx;
640
641 if (chan) {
642 dmaengine_terminate_all(chan);
643 dma_release_channel(chan);
644 dma_unmap_sg(port->dev, &atmel_port->sg_tx, 1,
645 DMA_MEM_TO_DEV);
646 }
647
648 atmel_port->desc_tx = NULL;
649 atmel_port->chan_tx = NULL;
650 atmel_port->cookie_tx = -EINVAL;
651}
652
653/*
654 * Called from tasklet with TXRDY interrupt is disabled.
655 */
656static void atmel_tx_dma(struct uart_port *port)
657{
658 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
659 struct circ_buf *xmit = &port->state->xmit;
660 struct dma_chan *chan = atmel_port->chan_tx;
661 struct dma_async_tx_descriptor *desc;
662 struct scatterlist *sg = &atmel_port->sg_tx;
663
664 /* Make sure we have an idle channel */
665 if (atmel_port->desc_tx != NULL)
666 return;
667
668 if (!uart_circ_empty(xmit) && !uart_tx_stopped(port)) {
669 /*
670 * DMA is idle now.
671 * Port xmit buffer is already mapped,
672 * and it is one page... Just adjust
673 * offsets and lengths. Since it is a circular buffer,
674 * we have to transmit till the end, and then the rest.
675 * Take the port lock to get a
676 * consistent xmit buffer state.
677 */
678 sg->offset = xmit->tail & (UART_XMIT_SIZE - 1);
679 sg_dma_address(sg) = (sg_dma_address(sg) &
680 ~(UART_XMIT_SIZE - 1))
681 + sg->offset;
682 sg_dma_len(sg) = CIRC_CNT_TO_END(xmit->head,
683 xmit->tail,
684 UART_XMIT_SIZE);
685 BUG_ON(!sg_dma_len(sg));
686
687 desc = dmaengine_prep_slave_sg(chan,
688 sg,
689 1,
690 DMA_MEM_TO_DEV,
691 DMA_PREP_INTERRUPT |
692 DMA_CTRL_ACK);
693 if (!desc) {
694 dev_err(port->dev, "Failed to send via dma!\n");
695 return;
696 }
697
698 dma_sync_sg_for_device(port->dev, sg, 1, DMA_MEM_TO_DEV);
699
700 atmel_port->desc_tx = desc;
701 desc->callback = atmel_complete_tx_dma;
702 desc->callback_param = atmel_port;
703 atmel_port->cookie_tx = dmaengine_submit(desc);
704
705 } else {
706 if (atmel_port->rs485.flags & SER_RS485_ENABLED) {
707 /* DMA done, stop TX, start RX for RS485 */
708 atmel_start_rx(port);
709 }
710 }
711
712 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
713 uart_write_wakeup(port);
714}
715
716static int atmel_prepare_tx_dma(struct uart_port *port)
717{
718 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
719 dma_cap_mask_t mask;
720 struct dma_slave_config config;
721 int ret, nent;
722
723 dma_cap_zero(mask);
724 dma_cap_set(DMA_SLAVE, mask);
725
726 atmel_port->chan_tx = dma_request_slave_channel(port->dev, "tx");
727 if (atmel_port->chan_tx == NULL)
728 goto chan_err;
729 dev_info(port->dev, "using %s for tx DMA transfers\n",
730 dma_chan_name(atmel_port->chan_tx));
731
732 spin_lock_init(&atmel_port->lock_tx);
733 sg_init_table(&atmel_port->sg_tx, 1);
734 /* UART circular tx buffer is an aligned page. */
735 BUG_ON((int)port->state->xmit.buf & ~PAGE_MASK);
736 sg_set_page(&atmel_port->sg_tx,
737 virt_to_page(port->state->xmit.buf),
738 UART_XMIT_SIZE,
739 (int)port->state->xmit.buf & ~PAGE_MASK);
740 nent = dma_map_sg(port->dev,
741 &atmel_port->sg_tx,
742 1,
743 DMA_MEM_TO_DEV);
744
745 if (!nent) {
746 dev_dbg(port->dev, "need to release resource of dma\n");
747 goto chan_err;
748 } else {
749 dev_dbg(port->dev, "%s: mapped %d@%p to %x\n", __func__,
750 sg_dma_len(&atmel_port->sg_tx),
751 port->state->xmit.buf,
752 sg_dma_address(&atmel_port->sg_tx));
753 }
754
755 /* Configure the slave DMA */
756 memset(&config, 0, sizeof(config));
757 config.direction = DMA_MEM_TO_DEV;
758 config.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
759 config.dst_addr = port->mapbase + ATMEL_US_THR;
760
761 ret = dmaengine_device_control(atmel_port->chan_tx,
762 DMA_SLAVE_CONFIG,
763 (unsigned long)&config);
764 if (ret) {
765 dev_err(port->dev, "DMA tx slave configuration failed\n");
766 goto chan_err;
767 }
768
769 return 0;
770
771chan_err:
772 dev_err(port->dev, "TX channel not available, switch to pio\n");
773 atmel_port->use_dma_tx = 0;
774 if (atmel_port->chan_tx)
775 atmel_release_tx_dma(port);
776 return -EINVAL;
777}
778
Elen Song34df42f2013-07-22 16:30:27 +0800779static void atmel_flip_buffer_rx_dma(struct uart_port *port,
780 char *buf, size_t count)
781{
782 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
783 struct tty_port *tport = &port->state->port;
784
785 dma_sync_sg_for_cpu(port->dev,
786 &atmel_port->sg_rx,
787 1,
788 DMA_DEV_TO_MEM);
789
790 tty_insert_flip_string(tport, buf, count);
791
792 dma_sync_sg_for_device(port->dev,
793 &atmel_port->sg_rx,
794 1,
795 DMA_DEV_TO_MEM);
796 /*
797 * Drop the lock here since it might end up calling
798 * uart_start(), which takes the lock.
799 */
800 spin_unlock(&port->lock);
801 tty_flip_buffer_push(tport);
802 spin_lock(&port->lock);
803}
804
805static void atmel_complete_rx_dma(void *arg)
806{
807 struct uart_port *port = arg;
808 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
809
810 tasklet_schedule(&atmel_port->tasklet);
811}
812
813static void atmel_release_rx_dma(struct uart_port *port)
814{
815 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
816 struct dma_chan *chan = atmel_port->chan_rx;
817
818 if (chan) {
819 dmaengine_terminate_all(chan);
820 dma_release_channel(chan);
821 dma_unmap_sg(port->dev, &atmel_port->sg_rx, 1,
822 DMA_DEV_TO_MEM);
823 }
824
825 atmel_port->desc_rx = NULL;
826 atmel_port->chan_rx = NULL;
827 atmel_port->cookie_rx = -EINVAL;
828}
829
830static void atmel_rx_from_dma(struct uart_port *port)
831{
832 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
833 struct circ_buf *ring = &atmel_port->rx_ring;
834 struct dma_chan *chan = atmel_port->chan_rx;
835 struct dma_tx_state state;
836 enum dma_status dmastat;
837 size_t pending, count;
838
839
840 /* Reset the UART timeout early so that we don't miss one */
841 UART_PUT_CR(port, ATMEL_US_STTTO);
842 dmastat = dmaengine_tx_status(chan,
843 atmel_port->cookie_rx,
844 &state);
845 /* Restart a new tasklet if DMA status is error */
846 if (dmastat == DMA_ERROR) {
847 dev_dbg(port->dev, "Get residue error, restart tasklet\n");
848 UART_PUT_IER(port, ATMEL_US_TIMEOUT);
849 tasklet_schedule(&atmel_port->tasklet);
850 return;
851 }
852 /* current transfer size should no larger than dma buffer */
853 pending = sg_dma_len(&atmel_port->sg_rx) - state.residue;
854 BUG_ON(pending > sg_dma_len(&atmel_port->sg_rx));
855
856 /*
857 * This will take the chars we have so far,
858 * ring->head will record the transfer size, only new bytes come
859 * will insert into the framework.
860 */
861 if (pending > ring->head) {
862 count = pending - ring->head;
863
864 atmel_flip_buffer_rx_dma(port, ring->buf + ring->head, count);
865
866 ring->head += count;
867 if (ring->head == sg_dma_len(&atmel_port->sg_rx))
868 ring->head = 0;
869
870 port->icount.rx += count;
871 }
872
873 UART_PUT_IER(port, ATMEL_US_TIMEOUT);
874}
875
876static int atmel_prepare_rx_dma(struct uart_port *port)
877{
878 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
879 struct dma_async_tx_descriptor *desc;
880 dma_cap_mask_t mask;
881 struct dma_slave_config config;
882 struct circ_buf *ring;
883 int ret, nent;
884
885 ring = &atmel_port->rx_ring;
886
887 dma_cap_zero(mask);
888 dma_cap_set(DMA_CYCLIC, mask);
889
890 atmel_port->chan_rx = dma_request_slave_channel(port->dev, "rx");
891 if (atmel_port->chan_rx == NULL)
892 goto chan_err;
893 dev_info(port->dev, "using %s for rx DMA transfers\n",
894 dma_chan_name(atmel_port->chan_rx));
895
896 spin_lock_init(&atmel_port->lock_rx);
897 sg_init_table(&atmel_port->sg_rx, 1);
898 /* UART circular rx buffer is an aligned page. */
899 BUG_ON((int)port->state->xmit.buf & ~PAGE_MASK);
900 sg_set_page(&atmel_port->sg_rx,
901 virt_to_page(ring->buf),
902 ATMEL_SERIAL_RINGSIZE,
903 (int)ring->buf & ~PAGE_MASK);
904 nent = dma_map_sg(port->dev,
905 &atmel_port->sg_rx,
906 1,
907 DMA_DEV_TO_MEM);
908
909 if (!nent) {
910 dev_dbg(port->dev, "need to release resource of dma\n");
911 goto chan_err;
912 } else {
913 dev_dbg(port->dev, "%s: mapped %d@%p to %x\n", __func__,
914 sg_dma_len(&atmel_port->sg_rx),
915 ring->buf,
916 sg_dma_address(&atmel_port->sg_rx));
917 }
918
919 /* Configure the slave DMA */
920 memset(&config, 0, sizeof(config));
921 config.direction = DMA_DEV_TO_MEM;
922 config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
923 config.src_addr = port->mapbase + ATMEL_US_RHR;
924
925 ret = dmaengine_device_control(atmel_port->chan_rx,
926 DMA_SLAVE_CONFIG,
927 (unsigned long)&config);
928 if (ret) {
929 dev_err(port->dev, "DMA rx slave configuration failed\n");
930 goto chan_err;
931 }
932 /*
933 * Prepare a cyclic dma transfer, assign 2 descriptors,
934 * each one is half ring buffer size
935 */
936 desc = dmaengine_prep_dma_cyclic(atmel_port->chan_rx,
937 sg_dma_address(&atmel_port->sg_rx),
938 sg_dma_len(&atmel_port->sg_rx),
939 sg_dma_len(&atmel_port->sg_rx)/2,
940 DMA_DEV_TO_MEM,
941 DMA_PREP_INTERRUPT);
942 desc->callback = atmel_complete_rx_dma;
943 desc->callback_param = port;
944 atmel_port->desc_rx = desc;
945 atmel_port->cookie_rx = dmaengine_submit(desc);
946
947 return 0;
948
949chan_err:
950 dev_err(port->dev, "RX channel not available, switch to pio\n");
951 atmel_port->use_dma_rx = 0;
952 if (atmel_port->chan_rx)
953 atmel_release_rx_dma(port);
954 return -EINVAL;
955}
956
Elen Song2e68c222013-07-22 16:30:30 +0800957static void atmel_uart_timer_callback(unsigned long data)
958{
959 struct uart_port *port = (void *)data;
960 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
961
962 tasklet_schedule(&atmel_port->tasklet);
963 mod_timer(&atmel_port->uart_timer, jiffies + uart_poll_timeout(port));
964}
965
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000966/*
Remy Bohmerb843aa22008-02-08 04:21:01 -0800967 * receive interrupt handler.
968 */
969static void
970atmel_handle_receive(struct uart_port *port, unsigned int pending)
971{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -0800972 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Remy Bohmerb843aa22008-02-08 04:21:01 -0800973
Elen Song64e22eb2013-07-22 16:30:24 +0800974 if (atmel_use_pdc_rx(port)) {
Chip Coldwella6670612008-02-08 04:21:06 -0800975 /*
976 * PDC receive. Just schedule the tasklet and let it
977 * figure out the details.
978 *
979 * TODO: We're not handling error flags correctly at
980 * the moment.
981 */
982 if (pending & (ATMEL_US_ENDRX | ATMEL_US_TIMEOUT)) {
983 UART_PUT_IDR(port, (ATMEL_US_ENDRX
984 | ATMEL_US_TIMEOUT));
985 tasklet_schedule(&atmel_port->tasklet);
986 }
987
988 if (pending & (ATMEL_US_RXBRK | ATMEL_US_OVRE |
989 ATMEL_US_FRAME | ATMEL_US_PARE))
990 atmel_pdc_rxerr(port, pending);
991 }
992
Elen Song34df42f2013-07-22 16:30:27 +0800993 if (atmel_use_dma_rx(port)) {
994 if (pending & ATMEL_US_TIMEOUT) {
995 UART_PUT_IDR(port, ATMEL_US_TIMEOUT);
996 tasklet_schedule(&atmel_port->tasklet);
997 }
998 }
999
Remy Bohmerb843aa22008-02-08 04:21:01 -08001000 /* Interrupt receive */
1001 if (pending & ATMEL_US_RXRDY)
1002 atmel_rx_chars(port);
1003 else if (pending & ATMEL_US_RXBRK) {
1004 /*
1005 * End of break detected. If it came along with a
1006 * character, atmel_rx_chars will handle it.
1007 */
1008 UART_PUT_CR(port, ATMEL_US_RSTSTA);
1009 UART_PUT_IDR(port, ATMEL_US_RXBRK);
1010 atmel_port->break_active = 0;
1011 }
1012}
1013
1014/*
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001015 * transmit interrupt handler. (Transmit is IRQF_NODELAY safe)
Remy Bohmerb843aa22008-02-08 04:21:01 -08001016 */
1017static void
1018atmel_handle_transmit(struct uart_port *port, unsigned int pending)
1019{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001020 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001021
Claudio Scordinoe8faff72010-05-03 13:31:28 +01001022 if (pending & atmel_port->tx_done_mask) {
1023 /* Either PDC or interrupt transmission */
1024 UART_PUT_IDR(port, atmel_port->tx_done_mask);
1025 tasklet_schedule(&atmel_port->tasklet);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001026 }
Remy Bohmerb843aa22008-02-08 04:21:01 -08001027}
1028
1029/*
1030 * status flags interrupt handler.
1031 */
1032static void
1033atmel_handle_status(struct uart_port *port, unsigned int pending,
1034 unsigned int status)
1035{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001036 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001037
Remy Bohmerb843aa22008-02-08 04:21:01 -08001038 if (pending & (ATMEL_US_RIIC | ATMEL_US_DSRIC | ATMEL_US_DCDIC
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001039 | ATMEL_US_CTSIC)) {
1040 atmel_port->irq_status = status;
1041 tasklet_schedule(&atmel_port->tasklet);
1042 }
Remy Bohmerb843aa22008-02-08 04:21:01 -08001043}
1044
1045/*
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001046 * Interrupt handler
1047 */
David Howells7d12e782006-10-05 14:55:46 +01001048static irqreturn_t atmel_interrupt(int irq, void *dev_id)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001049{
1050 struct uart_port *port = dev_id;
1051 unsigned int status, pending, pass_counter = 0;
1052
Chip Coldwella6670612008-02-08 04:21:06 -08001053 do {
1054 status = UART_GET_CSR(port);
1055 pending = status & UART_GET_IMR(port);
1056 if (!pending)
1057 break;
1058
Remy Bohmerb843aa22008-02-08 04:21:01 -08001059 atmel_handle_receive(port, pending);
1060 atmel_handle_status(port, pending, status);
1061 atmel_handle_transmit(port, pending);
Chip Coldwella6670612008-02-08 04:21:06 -08001062 } while (pass_counter++ < ATMEL_ISR_PASS_LIMIT);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001063
Haavard Skinnemoen0400b692008-02-23 15:23:36 -08001064 return pass_counter ? IRQ_HANDLED : IRQ_NONE;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001065}
1066
Elen Songa930e522013-07-22 16:30:25 +08001067static void atmel_release_tx_pdc(struct uart_port *port)
1068{
1069 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1070 struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1071
1072 dma_unmap_single(port->dev,
1073 pdc->dma_addr,
1074 pdc->dma_size,
1075 DMA_TO_DEVICE);
1076}
1077
Chip Coldwella6670612008-02-08 04:21:06 -08001078/*
1079 * Called from tasklet with ENDTX and TXBUFE interrupts disabled.
1080 */
Elen Song64e22eb2013-07-22 16:30:24 +08001081static void atmel_tx_pdc(struct uart_port *port)
Chip Coldwella6670612008-02-08 04:21:06 -08001082{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001083 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Alan Coxebd2c8f2009-09-19 13:13:28 -07001084 struct circ_buf *xmit = &port->state->xmit;
Chip Coldwella6670612008-02-08 04:21:06 -08001085 struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1086 int count;
1087
Michael Trimarchiba0657f2008-04-02 13:04:41 -07001088 /* nothing left to transmit? */
1089 if (UART_GET_TCR(port))
1090 return;
1091
Chip Coldwella6670612008-02-08 04:21:06 -08001092 xmit->tail += pdc->ofs;
1093 xmit->tail &= UART_XMIT_SIZE - 1;
1094
1095 port->icount.tx += pdc->ofs;
1096 pdc->ofs = 0;
1097
Michael Trimarchiba0657f2008-04-02 13:04:41 -07001098 /* more to transmit - setup next transfer */
Chip Coldwella6670612008-02-08 04:21:06 -08001099
Michael Trimarchiba0657f2008-04-02 13:04:41 -07001100 /* disable PDC transmit */
1101 UART_PUT_PTCR(port, ATMEL_PDC_TXTDIS);
1102
Itai Levi1f140812009-01-15 13:50:43 -08001103 if (!uart_circ_empty(xmit) && !uart_tx_stopped(port)) {
Chip Coldwella6670612008-02-08 04:21:06 -08001104 dma_sync_single_for_device(port->dev,
1105 pdc->dma_addr,
1106 pdc->dma_size,
1107 DMA_TO_DEVICE);
1108
1109 count = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
1110 pdc->ofs = count;
1111
1112 UART_PUT_TPR(port, pdc->dma_addr + xmit->tail);
1113 UART_PUT_TCR(port, count);
Claudio Scordinoe8faff72010-05-03 13:31:28 +01001114 /* re-enable PDC transmit */
Chip Coldwella6670612008-02-08 04:21:06 -08001115 UART_PUT_PTCR(port, ATMEL_PDC_TXTEN);
Claudio Scordinoe8faff72010-05-03 13:31:28 +01001116 /* Enable interrupts */
1117 UART_PUT_IER(port, atmel_port->tx_done_mask);
1118 } else {
Bernhard Roth83cac9f2011-08-24 09:48:23 +02001119 if ((atmel_port->rs485.flags & SER_RS485_ENABLED) &&
1120 !(atmel_port->rs485.flags & SER_RS485_RX_DURING_TX)) {
Claudio Scordinoe8faff72010-05-03 13:31:28 +01001121 /* DMA done, stop TX, start RX for RS485 */
1122 atmel_start_rx(port);
1123 }
Chip Coldwella6670612008-02-08 04:21:06 -08001124 }
1125
1126 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1127 uart_write_wakeup(port);
1128}
1129
Elen Songa930e522013-07-22 16:30:25 +08001130static int atmel_prepare_tx_pdc(struct uart_port *port)
1131{
1132 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1133 struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1134 struct circ_buf *xmit = &port->state->xmit;
1135
1136 pdc->buf = xmit->buf;
1137 pdc->dma_addr = dma_map_single(port->dev,
1138 pdc->buf,
1139 UART_XMIT_SIZE,
1140 DMA_TO_DEVICE);
1141 pdc->dma_size = UART_XMIT_SIZE;
1142 pdc->ofs = 0;
1143
1144 return 0;
1145}
1146
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001147static void atmel_rx_from_ring(struct uart_port *port)
1148{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001149 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001150 struct circ_buf *ring = &atmel_port->rx_ring;
1151 unsigned int flg;
1152 unsigned int status;
1153
1154 while (ring->head != ring->tail) {
1155 struct atmel_uart_char c;
1156
1157 /* Make sure c is loaded after head. */
1158 smp_rmb();
1159
1160 c = ((struct atmel_uart_char *)ring->buf)[ring->tail];
1161
1162 ring->tail = (ring->tail + 1) & (ATMEL_SERIAL_RINGSIZE - 1);
1163
1164 port->icount.rx++;
1165 status = c.status;
1166 flg = TTY_NORMAL;
1167
1168 /*
1169 * note that the error handling code is
1170 * out of the main execution path
1171 */
1172 if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME
1173 | ATMEL_US_OVRE | ATMEL_US_RXBRK))) {
1174 if (status & ATMEL_US_RXBRK) {
1175 /* ignore side-effect */
1176 status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME);
1177
1178 port->icount.brk++;
1179 if (uart_handle_break(port))
1180 continue;
1181 }
1182 if (status & ATMEL_US_PARE)
1183 port->icount.parity++;
1184 if (status & ATMEL_US_FRAME)
1185 port->icount.frame++;
1186 if (status & ATMEL_US_OVRE)
1187 port->icount.overrun++;
1188
1189 status &= port->read_status_mask;
1190
1191 if (status & ATMEL_US_RXBRK)
1192 flg = TTY_BREAK;
1193 else if (status & ATMEL_US_PARE)
1194 flg = TTY_PARITY;
1195 else if (status & ATMEL_US_FRAME)
1196 flg = TTY_FRAME;
1197 }
1198
1199
1200 if (uart_handle_sysrq_char(port, c.ch))
1201 continue;
1202
1203 uart_insert_char(port, status, ATMEL_US_OVRE, c.ch, flg);
1204 }
1205
1206 /*
1207 * Drop the lock here since it might end up calling
1208 * uart_start(), which takes the lock.
1209 */
1210 spin_unlock(&port->lock);
Jiri Slaby2e124b42013-01-03 15:53:06 +01001211 tty_flip_buffer_push(&port->state->port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001212 spin_lock(&port->lock);
1213}
1214
Elen Songa930e522013-07-22 16:30:25 +08001215static void atmel_release_rx_pdc(struct uart_port *port)
1216{
1217 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1218 int i;
1219
1220 for (i = 0; i < 2; i++) {
1221 struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i];
1222
1223 dma_unmap_single(port->dev,
1224 pdc->dma_addr,
1225 pdc->dma_size,
1226 DMA_FROM_DEVICE);
1227 kfree(pdc->buf);
1228 }
1229}
1230
Elen Song64e22eb2013-07-22 16:30:24 +08001231static void atmel_rx_from_pdc(struct uart_port *port)
Chip Coldwella6670612008-02-08 04:21:06 -08001232{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001233 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Jiri Slaby05c7cd32013-01-03 15:53:04 +01001234 struct tty_port *tport = &port->state->port;
Chip Coldwella6670612008-02-08 04:21:06 -08001235 struct atmel_dma_buffer *pdc;
1236 int rx_idx = atmel_port->pdc_rx_idx;
1237 unsigned int head;
1238 unsigned int tail;
1239 unsigned int count;
1240
1241 do {
1242 /* Reset the UART timeout early so that we don't miss one */
1243 UART_PUT_CR(port, ATMEL_US_STTTO);
1244
1245 pdc = &atmel_port->pdc_rx[rx_idx];
1246 head = UART_GET_RPR(port) - pdc->dma_addr;
1247 tail = pdc->ofs;
1248
1249 /* If the PDC has switched buffers, RPR won't contain
1250 * any address within the current buffer. Since head
1251 * is unsigned, we just need a one-way comparison to
1252 * find out.
1253 *
1254 * In this case, we just need to consume the entire
1255 * buffer and resubmit it for DMA. This will clear the
1256 * ENDRX bit as well, so that we can safely re-enable
1257 * all interrupts below.
1258 */
1259 head = min(head, pdc->dma_size);
1260
1261 if (likely(head != tail)) {
1262 dma_sync_single_for_cpu(port->dev, pdc->dma_addr,
1263 pdc->dma_size, DMA_FROM_DEVICE);
1264
1265 /*
1266 * head will only wrap around when we recycle
1267 * the DMA buffer, and when that happens, we
1268 * explicitly set tail to 0. So head will
1269 * always be greater than tail.
1270 */
1271 count = head - tail;
1272
Jiri Slaby05c7cd32013-01-03 15:53:04 +01001273 tty_insert_flip_string(tport, pdc->buf + pdc->ofs,
1274 count);
Chip Coldwella6670612008-02-08 04:21:06 -08001275
1276 dma_sync_single_for_device(port->dev, pdc->dma_addr,
1277 pdc->dma_size, DMA_FROM_DEVICE);
1278
1279 port->icount.rx += count;
1280 pdc->ofs = head;
1281 }
1282
1283 /*
1284 * If the current buffer is full, we need to check if
1285 * the next one contains any additional data.
1286 */
1287 if (head >= pdc->dma_size) {
1288 pdc->ofs = 0;
1289 UART_PUT_RNPR(port, pdc->dma_addr);
1290 UART_PUT_RNCR(port, pdc->dma_size);
1291
1292 rx_idx = !rx_idx;
1293 atmel_port->pdc_rx_idx = rx_idx;
1294 }
1295 } while (head >= pdc->dma_size);
1296
1297 /*
1298 * Drop the lock here since it might end up calling
1299 * uart_start(), which takes the lock.
1300 */
1301 spin_unlock(&port->lock);
Jiri Slaby2e124b42013-01-03 15:53:06 +01001302 tty_flip_buffer_push(tport);
Chip Coldwella6670612008-02-08 04:21:06 -08001303 spin_lock(&port->lock);
1304
1305 UART_PUT_IER(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT);
1306}
1307
Elen Songa930e522013-07-22 16:30:25 +08001308static int atmel_prepare_rx_pdc(struct uart_port *port)
1309{
1310 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1311 int i;
1312
1313 for (i = 0; i < 2; i++) {
1314 struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i];
1315
1316 pdc->buf = kmalloc(PDC_BUFFER_SIZE, GFP_KERNEL);
1317 if (pdc->buf == NULL) {
1318 if (i != 0) {
1319 dma_unmap_single(port->dev,
1320 atmel_port->pdc_rx[0].dma_addr,
1321 PDC_BUFFER_SIZE,
1322 DMA_FROM_DEVICE);
1323 kfree(atmel_port->pdc_rx[0].buf);
1324 }
1325 atmel_port->use_pdc_rx = 0;
1326 return -ENOMEM;
1327 }
1328 pdc->dma_addr = dma_map_single(port->dev,
1329 pdc->buf,
1330 PDC_BUFFER_SIZE,
1331 DMA_FROM_DEVICE);
1332 pdc->dma_size = PDC_BUFFER_SIZE;
1333 pdc->ofs = 0;
1334 }
1335
1336 atmel_port->pdc_rx_idx = 0;
1337
1338 UART_PUT_RPR(port, atmel_port->pdc_rx[0].dma_addr);
1339 UART_PUT_RCR(port, PDC_BUFFER_SIZE);
1340
1341 UART_PUT_RNPR(port, atmel_port->pdc_rx[1].dma_addr);
1342 UART_PUT_RNCR(port, PDC_BUFFER_SIZE);
1343
1344 return 0;
1345}
1346
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001347/*
1348 * tasklet handling tty stuff outside the interrupt handler.
1349 */
1350static void atmel_tasklet_func(unsigned long data)
1351{
1352 struct uart_port *port = (struct uart_port *)data;
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001353 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001354 unsigned int status;
1355 unsigned int status_change;
1356
1357 /* The interrupt handler does not take the lock */
1358 spin_lock(&port->lock);
1359
Elen Songa930e522013-07-22 16:30:25 +08001360 atmel_port->schedule_tx(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001361
1362 status = atmel_port->irq_status;
1363 status_change = status ^ atmel_port->irq_status_prev;
1364
1365 if (status_change & (ATMEL_US_RI | ATMEL_US_DSR
1366 | ATMEL_US_DCD | ATMEL_US_CTS)) {
1367 /* TODO: All reads to CSR will clear these interrupts! */
1368 if (status_change & ATMEL_US_RI)
1369 port->icount.rng++;
1370 if (status_change & ATMEL_US_DSR)
1371 port->icount.dsr++;
1372 if (status_change & ATMEL_US_DCD)
1373 uart_handle_dcd_change(port, !(status & ATMEL_US_DCD));
1374 if (status_change & ATMEL_US_CTS)
1375 uart_handle_cts_change(port, !(status & ATMEL_US_CTS));
1376
Alan Coxbdc04e32009-09-19 13:13:31 -07001377 wake_up_interruptible(&port->state->port.delta_msr_wait);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001378
1379 atmel_port->irq_status_prev = status;
1380 }
1381
Elen Songa930e522013-07-22 16:30:25 +08001382 atmel_port->schedule_rx(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001383
1384 spin_unlock(&port->lock);
1385}
1386
Elen Song33d64c42013-07-22 16:30:28 +08001387static int atmel_init_property(struct atmel_uart_port *atmel_port,
1388 struct platform_device *pdev)
1389{
1390 struct device_node *np = pdev->dev.of_node;
Jingoo Han574de552013-07-30 17:06:57 +09001391 struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
Elen Song33d64c42013-07-22 16:30:28 +08001392
1393 if (np) {
1394 /* DMA/PDC usage specification */
1395 if (of_get_property(np, "atmel,use-dma-rx", NULL)) {
1396 if (of_get_property(np, "dmas", NULL)) {
1397 atmel_port->use_dma_rx = true;
1398 atmel_port->use_pdc_rx = false;
1399 } else {
1400 atmel_port->use_dma_rx = false;
1401 atmel_port->use_pdc_rx = true;
1402 }
1403 } else {
1404 atmel_port->use_dma_rx = false;
1405 atmel_port->use_pdc_rx = false;
1406 }
1407
1408 if (of_get_property(np, "atmel,use-dma-tx", NULL)) {
1409 if (of_get_property(np, "dmas", NULL)) {
1410 atmel_port->use_dma_tx = true;
1411 atmel_port->use_pdc_tx = false;
1412 } else {
1413 atmel_port->use_dma_tx = false;
1414 atmel_port->use_pdc_tx = true;
1415 }
1416 } else {
1417 atmel_port->use_dma_tx = false;
1418 atmel_port->use_pdc_tx = false;
1419 }
1420
1421 } else {
1422 atmel_port->use_pdc_rx = pdata->use_dma_rx;
1423 atmel_port->use_pdc_tx = pdata->use_dma_tx;
1424 atmel_port->use_dma_rx = false;
1425 atmel_port->use_dma_tx = false;
1426 }
1427
1428 return 0;
1429}
1430
1431static void atmel_init_rs485(struct atmel_uart_port *atmel_port,
1432 struct platform_device *pdev)
1433{
1434 struct device_node *np = pdev->dev.of_node;
Jingoo Han574de552013-07-30 17:06:57 +09001435 struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
Elen Song33d64c42013-07-22 16:30:28 +08001436
1437 if (np) {
1438 u32 rs485_delay[2];
1439 /* rs485 properties */
1440 if (of_property_read_u32_array(np, "rs485-rts-delay",
1441 rs485_delay, 2) == 0) {
1442 struct serial_rs485 *rs485conf = &atmel_port->rs485;
1443
1444 rs485conf->delay_rts_before_send = rs485_delay[0];
1445 rs485conf->delay_rts_after_send = rs485_delay[1];
1446 rs485conf->flags = 0;
1447
1448 if (of_get_property(np, "rs485-rx-during-tx", NULL))
1449 rs485conf->flags |= SER_RS485_RX_DURING_TX;
1450
1451 if (of_get_property(np, "linux,rs485-enabled-at-boot-time",
1452 NULL))
1453 rs485conf->flags |= SER_RS485_ENABLED;
1454 }
1455 } else {
1456 atmel_port->rs485 = pdata->rs485;
1457 }
1458
1459}
1460
Elen Songa930e522013-07-22 16:30:25 +08001461static void atmel_set_ops(struct uart_port *port)
1462{
1463 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1464
Elen Song34df42f2013-07-22 16:30:27 +08001465 if (atmel_use_dma_rx(port)) {
1466 atmel_port->prepare_rx = &atmel_prepare_rx_dma;
1467 atmel_port->schedule_rx = &atmel_rx_from_dma;
1468 atmel_port->release_rx = &atmel_release_rx_dma;
1469 } else if (atmel_use_pdc_rx(port)) {
Elen Songa930e522013-07-22 16:30:25 +08001470 atmel_port->prepare_rx = &atmel_prepare_rx_pdc;
1471 atmel_port->schedule_rx = &atmel_rx_from_pdc;
1472 atmel_port->release_rx = &atmel_release_rx_pdc;
1473 } else {
1474 atmel_port->prepare_rx = NULL;
1475 atmel_port->schedule_rx = &atmel_rx_from_ring;
1476 atmel_port->release_rx = NULL;
1477 }
1478
Elen Song08f738b2013-07-22 16:30:26 +08001479 if (atmel_use_dma_tx(port)) {
1480 atmel_port->prepare_tx = &atmel_prepare_tx_dma;
1481 atmel_port->schedule_tx = &atmel_tx_dma;
1482 atmel_port->release_tx = &atmel_release_tx_dma;
1483 } else if (atmel_use_pdc_tx(port)) {
Elen Songa930e522013-07-22 16:30:25 +08001484 atmel_port->prepare_tx = &atmel_prepare_tx_pdc;
1485 atmel_port->schedule_tx = &atmel_tx_pdc;
1486 atmel_port->release_tx = &atmel_release_tx_pdc;
1487 } else {
1488 atmel_port->prepare_tx = NULL;
1489 atmel_port->schedule_tx = &atmel_tx_chars;
1490 atmel_port->release_tx = NULL;
1491 }
1492}
1493
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001494/*
Elen Song055560b2013-07-22 16:30:29 +08001495 * Get ip name usart or uart
1496 */
Nicolas Ferre892db582013-10-17 17:37:11 +02001497static void atmel_get_ip_name(struct uart_port *port)
Elen Song055560b2013-07-22 16:30:29 +08001498{
1499 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1500 int name = UART_GET_IP_NAME(port);
Nicolas Ferre731d9ca2013-10-17 17:37:12 +02001501 u32 version;
Elen Song055560b2013-07-22 16:30:29 +08001502 int usart, uart;
1503 /* usart and uart ascii */
1504 usart = 0x55534152;
1505 uart = 0x44424755;
1506
1507 atmel_port->is_usart = false;
1508
1509 if (name == usart) {
1510 dev_dbg(port->dev, "This is usart\n");
1511 atmel_port->is_usart = true;
1512 } else if (name == uart) {
1513 dev_dbg(port->dev, "This is uart\n");
1514 atmel_port->is_usart = false;
1515 } else {
Nicolas Ferre731d9ca2013-10-17 17:37:12 +02001516 /* fallback for older SoCs: use version field */
1517 version = UART_GET_IP_VERSION(port);
1518 switch (version) {
1519 case 0x302:
1520 case 0x10213:
1521 dev_dbg(port->dev, "This version is usart\n");
1522 atmel_port->is_usart = true;
1523 break;
1524 case 0x203:
1525 case 0x10202:
1526 dev_dbg(port->dev, "This version is uart\n");
1527 atmel_port->is_usart = false;
1528 break;
1529 default:
1530 dev_err(port->dev, "Not supported ip name nor version, set to uart\n");
1531 }
Elen Song055560b2013-07-22 16:30:29 +08001532 }
Elen Song055560b2013-07-22 16:30:29 +08001533}
1534
1535/*
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001536 * Perform initialization and enable port for reception
1537 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001538static int atmel_startup(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001539{
Elen Song33d64c42013-07-22 16:30:28 +08001540 struct platform_device *pdev = to_platform_device(port->dev);
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001541 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Alan Coxebd2c8f2009-09-19 13:13:28 -07001542 struct tty_struct *tty = port->state->port.tty;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001543 int retval;
1544
1545 /*
1546 * Ensure that no interrupts are enabled otherwise when
1547 * request_irq() is called we could get stuck trying to
1548 * handle an unexpected interrupt
1549 */
1550 UART_PUT_IDR(port, -1);
1551
1552 /*
1553 * Allocate the IRQ
1554 */
Remy Bohmerb843aa22008-02-08 04:21:01 -08001555 retval = request_irq(port->irq, atmel_interrupt, IRQF_SHARED,
Haavard Skinnemoenae161062008-02-08 04:21:08 -08001556 tty ? tty->name : "atmel_serial", port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001557 if (retval) {
Richard Genoudddaa6032014-02-26 17:19:45 +01001558 dev_err(port->dev, "atmel_startup - Can't get irq\n");
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001559 return retval;
1560 }
1561
1562 /*
Chip Coldwella6670612008-02-08 04:21:06 -08001563 * Initialize DMA (if necessary)
1564 */
Elen Song33d64c42013-07-22 16:30:28 +08001565 atmel_init_property(atmel_port, pdev);
1566
Elen Songa930e522013-07-22 16:30:25 +08001567 if (atmel_port->prepare_rx) {
1568 retval = atmel_port->prepare_rx(port);
1569 if (retval < 0)
1570 atmel_set_ops(port);
Chip Coldwella6670612008-02-08 04:21:06 -08001571 }
1572
Elen Songa930e522013-07-22 16:30:25 +08001573 if (atmel_port->prepare_tx) {
1574 retval = atmel_port->prepare_tx(port);
1575 if (retval < 0)
1576 atmel_set_ops(port);
1577 }
Chip Coldwella6670612008-02-08 04:21:06 -08001578 /*
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001579 * If there is a specific "open" function (to register
1580 * control line interrupts)
1581 */
Haavard Skinnemoen71f2e2b2006-10-04 16:02:07 +02001582 if (atmel_open_hook) {
1583 retval = atmel_open_hook(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001584 if (retval) {
1585 free_irq(port->irq, port);
1586 return retval;
1587 }
1588 }
1589
Atsushi Nemoto27c0c8e2009-02-18 14:48:28 -08001590 /* Save current CSR for comparison in atmel_tasklet_func() */
1591 atmel_port->irq_status_prev = UART_GET_CSR(port);
1592 atmel_port->irq_status = atmel_port->irq_status_prev;
1593
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001594 /*
1595 * Finally, enable the serial port
1596 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001597 UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
Remy Bohmerb843aa22008-02-08 04:21:01 -08001598 /* enable xmit & rcvr */
1599 UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN);
Andrew Victorafefc412006-06-19 19:53:19 +01001600
Marek Roszko8bc661b2014-01-10 10:33:11 +01001601 setup_timer(&atmel_port->uart_timer,
1602 atmel_uart_timer_callback,
1603 (unsigned long)port);
1604
Elen Song64e22eb2013-07-22 16:30:24 +08001605 if (atmel_use_pdc_rx(port)) {
Chip Coldwella6670612008-02-08 04:21:06 -08001606 /* set UART timeout */
Elen Song2e68c222013-07-22 16:30:30 +08001607 if (!atmel_port->is_usart) {
Elen Song2e68c222013-07-22 16:30:30 +08001608 mod_timer(&atmel_port->uart_timer,
1609 jiffies + uart_poll_timeout(port));
1610 /* set USART timeout */
1611 } else {
1612 UART_PUT_RTOR(port, PDC_RX_TIMEOUT);
1613 UART_PUT_CR(port, ATMEL_US_STTTO);
Chip Coldwella6670612008-02-08 04:21:06 -08001614
Elen Song2e68c222013-07-22 16:30:30 +08001615 UART_PUT_IER(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT);
1616 }
Chip Coldwella6670612008-02-08 04:21:06 -08001617 /* enable PDC controller */
1618 UART_PUT_PTCR(port, ATMEL_PDC_RXTEN);
Elen Song34df42f2013-07-22 16:30:27 +08001619 } else if (atmel_use_dma_rx(port)) {
Elen Song2e68c222013-07-22 16:30:30 +08001620 /* set UART timeout */
1621 if (!atmel_port->is_usart) {
Elen Song2e68c222013-07-22 16:30:30 +08001622 mod_timer(&atmel_port->uart_timer,
1623 jiffies + uart_poll_timeout(port));
1624 /* set USART timeout */
1625 } else {
1626 UART_PUT_RTOR(port, PDC_RX_TIMEOUT);
1627 UART_PUT_CR(port, ATMEL_US_STTTO);
Elen Song34df42f2013-07-22 16:30:27 +08001628
Elen Song2e68c222013-07-22 16:30:30 +08001629 UART_PUT_IER(port, ATMEL_US_TIMEOUT);
1630 }
Chip Coldwella6670612008-02-08 04:21:06 -08001631 } else {
1632 /* enable receive only */
1633 UART_PUT_IER(port, ATMEL_US_RXRDY);
1634 }
Andrew Victorafefc412006-06-19 19:53:19 +01001635
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001636 return 0;
1637}
1638
1639/*
1640 * Disable the port
1641 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001642static void atmel_shutdown(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001643{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001644 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Marek Roszko0cc7c6c2014-01-07 11:45:06 +01001645
Chip Coldwella6670612008-02-08 04:21:06 -08001646 /*
Marek Roszko8bc661b2014-01-10 10:33:11 +01001647 * Prevent any tasklets being scheduled during
1648 * cleanup
1649 */
1650 del_timer_sync(&atmel_port->uart_timer);
1651
1652 /*
Marek Roszko0cc7c6c2014-01-07 11:45:06 +01001653 * Clear out any scheduled tasklets before
1654 * we destroy the buffers
1655 */
1656 tasklet_kill(&atmel_port->tasklet);
1657
1658 /*
1659 * Ensure everything is stopped and
1660 * disable all interrupts, port and break condition.
Chip Coldwella6670612008-02-08 04:21:06 -08001661 */
1662 atmel_stop_rx(port);
1663 atmel_stop_tx(port);
1664
Marek Roszko0cc7c6c2014-01-07 11:45:06 +01001665 UART_PUT_CR(port, ATMEL_US_RSTSTA);
1666 UART_PUT_IDR(port, -1);
1667
1668
Chip Coldwella6670612008-02-08 04:21:06 -08001669 /*
1670 * Shut-down the DMA.
1671 */
Elen Songa930e522013-07-22 16:30:25 +08001672 if (atmel_port->release_rx)
1673 atmel_port->release_rx(port);
1674 if (atmel_port->release_tx)
1675 atmel_port->release_tx(port);
Chip Coldwella6670612008-02-08 04:21:06 -08001676
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001677 /*
Mark Deneenbb7e73c2014-01-07 11:45:09 +01001678 * Reset ring buffer pointers
1679 */
1680 atmel_port->rx_ring.head = 0;
1681 atmel_port->rx_ring.tail = 0;
1682
1683 /*
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001684 * Free the interrupt
1685 */
1686 free_irq(port->irq, port);
1687
1688 /*
1689 * If there is a specific "close" function (to unregister
1690 * control line interrupts)
1691 */
Haavard Skinnemoen71f2e2b2006-10-04 16:02:07 +02001692 if (atmel_close_hook)
1693 atmel_close_hook(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001694}
1695
1696/*
Haavard Skinnemoen9afd5612008-07-16 21:52:46 +01001697 * Flush any TX data submitted for DMA. Called when the TX circular
1698 * buffer is reset.
1699 */
1700static void atmel_flush_buffer(struct uart_port *port)
1701{
1702 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1703
Elen Song64e22eb2013-07-22 16:30:24 +08001704 if (atmel_use_pdc_tx(port)) {
Haavard Skinnemoen9afd5612008-07-16 21:52:46 +01001705 UART_PUT_TCR(port, 0);
1706 atmel_port->pdc_tx.ofs = 0;
1707 }
1708}
1709
1710/*
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001711 * Power / Clock management.
1712 */
Remy Bohmerb843aa22008-02-08 04:21:01 -08001713static void atmel_serial_pm(struct uart_port *port, unsigned int state,
1714 unsigned int oldstate)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001715{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001716 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Andrew Victorafefc412006-06-19 19:53:19 +01001717
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001718 switch (state) {
Remy Bohmerb843aa22008-02-08 04:21:01 -08001719 case 0:
1720 /*
1721 * Enable the peripheral clock for this serial port.
1722 * This is called on uart_open() or a resume event.
1723 */
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02001724 clk_prepare_enable(atmel_port->clk);
Anti Sullinf05596d2008-09-22 13:57:54 -07001725
1726 /* re-enable interrupts if we disabled some on suspend */
1727 UART_PUT_IER(port, atmel_port->backup_imr);
Remy Bohmerb843aa22008-02-08 04:21:01 -08001728 break;
1729 case 3:
Anti Sullinf05596d2008-09-22 13:57:54 -07001730 /* Back up the interrupt mask and disable all interrupts */
1731 atmel_port->backup_imr = UART_GET_IMR(port);
1732 UART_PUT_IDR(port, -1);
1733
Remy Bohmerb843aa22008-02-08 04:21:01 -08001734 /*
1735 * Disable the peripheral clock for this serial port.
1736 * This is called on uart_close() or a suspend event.
1737 */
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02001738 clk_disable_unprepare(atmel_port->clk);
Remy Bohmerb843aa22008-02-08 04:21:01 -08001739 break;
1740 default:
Richard Genoudddaa6032014-02-26 17:19:45 +01001741 dev_err(port->dev, "atmel_serial: unknown pm %d\n", state);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001742 }
1743}
1744
1745/*
1746 * Change the port parameters
1747 */
Remy Bohmerb843aa22008-02-08 04:21:01 -08001748static void atmel_set_termios(struct uart_port *port, struct ktermios *termios,
1749 struct ktermios *old)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001750{
1751 unsigned long flags;
1752 unsigned int mode, imr, quot, baud;
Claudio Scordinoe8faff72010-05-03 13:31:28 +01001753 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001754
Andrew Victor03abeac2007-05-03 12:26:24 +01001755 /* Get current mode register */
Remy Bohmerb843aa22008-02-08 04:21:01 -08001756 mode = UART_GET_MR(port) & ~(ATMEL_US_USCLKS | ATMEL_US_CHRL
Peter Ma8e706c42009-03-31 10:31:02 -07001757 | ATMEL_US_NBSTOP | ATMEL_US_PAR
1758 | ATMEL_US_USMODE);
Andrew Victor03abeac2007-05-03 12:26:24 +01001759
Remy Bohmerb843aa22008-02-08 04:21:01 -08001760 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001761 quot = uart_get_divisor(port, baud);
1762
Remy Bohmerb843aa22008-02-08 04:21:01 -08001763 if (quot > 65535) { /* BRGR is 16-bit, so switch to slower clock */
Andrew Victor03abeac2007-05-03 12:26:24 +01001764 quot /= 8;
1765 mode |= ATMEL_US_USCLKS_MCK_DIV8;
1766 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001767
1768 /* byte size */
1769 switch (termios->c_cflag & CSIZE) {
1770 case CS5:
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001771 mode |= ATMEL_US_CHRL_5;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001772 break;
1773 case CS6:
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001774 mode |= ATMEL_US_CHRL_6;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001775 break;
1776 case CS7:
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001777 mode |= ATMEL_US_CHRL_7;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001778 break;
1779 default:
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001780 mode |= ATMEL_US_CHRL_8;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001781 break;
1782 }
1783
1784 /* stop bits */
1785 if (termios->c_cflag & CSTOPB)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001786 mode |= ATMEL_US_NBSTOP_2;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001787
1788 /* parity */
1789 if (termios->c_cflag & PARENB) {
Remy Bohmerb843aa22008-02-08 04:21:01 -08001790 /* Mark or Space parity */
1791 if (termios->c_cflag & CMSPAR) {
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001792 if (termios->c_cflag & PARODD)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001793 mode |= ATMEL_US_PAR_MARK;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001794 else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001795 mode |= ATMEL_US_PAR_SPACE;
Remy Bohmerb843aa22008-02-08 04:21:01 -08001796 } else if (termios->c_cflag & PARODD)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001797 mode |= ATMEL_US_PAR_ODD;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001798 else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001799 mode |= ATMEL_US_PAR_EVEN;
Remy Bohmerb843aa22008-02-08 04:21:01 -08001800 } else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001801 mode |= ATMEL_US_PAR_NONE;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001802
Peter Ma8e706c42009-03-31 10:31:02 -07001803 /* hardware handshake (RTS/CTS) */
1804 if (termios->c_cflag & CRTSCTS)
1805 mode |= ATMEL_US_USMODE_HWHS;
1806 else
1807 mode |= ATMEL_US_USMODE_NORMAL;
1808
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001809 spin_lock_irqsave(&port->lock, flags);
1810
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001811 port->read_status_mask = ATMEL_US_OVRE;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001812 if (termios->c_iflag & INPCK)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001813 port->read_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001814 if (termios->c_iflag & (BRKINT | PARMRK))
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001815 port->read_status_mask |= ATMEL_US_RXBRK;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001816
Elen Song64e22eb2013-07-22 16:30:24 +08001817 if (atmel_use_pdc_rx(port))
Chip Coldwella6670612008-02-08 04:21:06 -08001818 /* need to enable error interrupts */
1819 UART_PUT_IER(port, port->read_status_mask);
1820
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001821 /*
1822 * Characters to ignore
1823 */
1824 port->ignore_status_mask = 0;
1825 if (termios->c_iflag & IGNPAR)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001826 port->ignore_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001827 if (termios->c_iflag & IGNBRK) {
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001828 port->ignore_status_mask |= ATMEL_US_RXBRK;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001829 /*
1830 * If we're ignoring parity and break indicators,
1831 * ignore overruns too (for real raw support).
1832 */
1833 if (termios->c_iflag & IGNPAR)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001834 port->ignore_status_mask |= ATMEL_US_OVRE;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001835 }
Remy Bohmerb843aa22008-02-08 04:21:01 -08001836 /* TODO: Ignore all characters if CREAD is set.*/
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001837
1838 /* update the per-port timeout */
1839 uart_update_timeout(port, termios->c_cflag, baud);
1840
Haavard Skinnemoen0ccad872009-06-16 17:02:03 +01001841 /*
1842 * save/disable interrupts. The tty layer will ensure that the
1843 * transmitter is empty if requested by the caller, so there's
1844 * no need to wait for it here.
1845 */
Remy Bohmerb843aa22008-02-08 04:21:01 -08001846 imr = UART_GET_IMR(port);
1847 UART_PUT_IDR(port, -1);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001848
1849 /* disable receiver and transmitter */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001850 UART_PUT_CR(port, ATMEL_US_TXDIS | ATMEL_US_RXDIS);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001851
Claudio Scordinoe8faff72010-05-03 13:31:28 +01001852 /* Resetting serial mode to RS232 (0x0) */
1853 mode &= ~ATMEL_US_USMODE;
1854
1855 if (atmel_port->rs485.flags & SER_RS485_ENABLED) {
Claudio Scordino93f33502011-11-09 15:51:49 +01001856 if ((atmel_port->rs485.delay_rts_after_send) > 0)
Claudio Scordino1b633182010-07-20 15:26:47 -07001857 UART_PUT_TTGR(port,
1858 atmel_port->rs485.delay_rts_after_send);
Claudio Scordinoe8faff72010-05-03 13:31:28 +01001859 mode |= ATMEL_US_USMODE_RS485;
Claudio Scordinoe8faff72010-05-03 13:31:28 +01001860 }
1861
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001862 /* set the parity, stop bits and data size */
1863 UART_PUT_MR(port, mode);
1864
1865 /* set the baud rate */
1866 UART_PUT_BRGR(port, quot);
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001867 UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
1868 UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001869
1870 /* restore interrupts */
1871 UART_PUT_IER(port, imr);
1872
1873 /* CTS flow-control and modem-status interrupts */
1874 if (UART_ENABLE_MS(port, termios->c_cflag))
1875 port->ops->enable_ms(port);
1876
1877 spin_unlock_irqrestore(&port->lock, flags);
1878}
1879
Viktar Palstsiuk42bd7a42011-02-09 15:26:13 +01001880static void atmel_set_ldisc(struct uart_port *port, int new)
1881{
Jiri Slabyb54bf3b2011-11-09 21:33:44 +01001882 if (new == N_PPS) {
Viktar Palstsiuk42bd7a42011-02-09 15:26:13 +01001883 port->flags |= UPF_HARDPPS_CD;
1884 atmel_enable_ms(port);
1885 } else {
1886 port->flags &= ~UPF_HARDPPS_CD;
1887 }
1888}
1889
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001890/*
1891 * Return string describing the specified port
1892 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001893static const char *atmel_type(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001894{
Haavard Skinnemoen9ab4f882006-10-04 16:02:06 +02001895 return (port->type == PORT_ATMEL) ? "ATMEL_SERIAL" : NULL;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001896}
1897
1898/*
1899 * Release the memory region(s) being used by 'port'.
1900 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001901static void atmel_release_port(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001902{
Andrew Victorafefc412006-06-19 19:53:19 +01001903 struct platform_device *pdev = to_platform_device(port->dev);
1904 int size = pdev->resource[0].end - pdev->resource[0].start + 1;
1905
1906 release_mem_region(port->mapbase, size);
1907
1908 if (port->flags & UPF_IOREMAP) {
1909 iounmap(port->membase);
1910 port->membase = NULL;
1911 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001912}
1913
1914/*
1915 * Request the memory region(s) being used by 'port'.
1916 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001917static int atmel_request_port(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001918{
Andrew Victorafefc412006-06-19 19:53:19 +01001919 struct platform_device *pdev = to_platform_device(port->dev);
1920 int size = pdev->resource[0].end - pdev->resource[0].start + 1;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001921
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001922 if (!request_mem_region(port->mapbase, size, "atmel_serial"))
Andrew Victorafefc412006-06-19 19:53:19 +01001923 return -EBUSY;
1924
1925 if (port->flags & UPF_IOREMAP) {
1926 port->membase = ioremap(port->mapbase, size);
1927 if (port->membase == NULL) {
1928 release_mem_region(port->mapbase, size);
1929 return -ENOMEM;
1930 }
1931 }
1932
1933 return 0;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001934}
1935
1936/*
1937 * Configure/autoconfigure the port.
1938 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001939static void atmel_config_port(struct uart_port *port, int flags)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001940{
1941 if (flags & UART_CONFIG_TYPE) {
Haavard Skinnemoen9ab4f882006-10-04 16:02:06 +02001942 port->type = PORT_ATMEL;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001943 atmel_request_port(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001944 }
1945}
1946
1947/*
1948 * Verify the new serial_struct (for TIOCSSERIAL).
1949 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001950static int atmel_verify_port(struct uart_port *port, struct serial_struct *ser)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001951{
1952 int ret = 0;
Haavard Skinnemoen9ab4f882006-10-04 16:02:06 +02001953 if (ser->type != PORT_UNKNOWN && ser->type != PORT_ATMEL)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001954 ret = -EINVAL;
1955 if (port->irq != ser->irq)
1956 ret = -EINVAL;
1957 if (ser->io_type != SERIAL_IO_MEM)
1958 ret = -EINVAL;
1959 if (port->uartclk / 16 != ser->baud_base)
1960 ret = -EINVAL;
1961 if ((void *)port->mapbase != ser->iomem_base)
1962 ret = -EINVAL;
1963 if (port->iobase != ser->port)
1964 ret = -EINVAL;
1965 if (ser->hub6 != 0)
1966 ret = -EINVAL;
1967 return ret;
1968}
1969
Albin Tonnerre8fe2d542009-12-09 12:31:32 -08001970#ifdef CONFIG_CONSOLE_POLL
1971static int atmel_poll_get_char(struct uart_port *port)
1972{
1973 while (!(UART_GET_CSR(port) & ATMEL_US_RXRDY))
1974 cpu_relax();
1975
1976 return UART_GET_CHAR(port);
1977}
1978
1979static void atmel_poll_put_char(struct uart_port *port, unsigned char ch)
1980{
1981 while (!(UART_GET_CSR(port) & ATMEL_US_TXRDY))
1982 cpu_relax();
1983
1984 UART_PUT_CHAR(port, ch);
1985}
1986#endif
1987
Claudio Scordinoe8faff72010-05-03 13:31:28 +01001988static int
1989atmel_ioctl(struct uart_port *port, unsigned int cmd, unsigned long arg)
1990{
1991 struct serial_rs485 rs485conf;
1992
1993 switch (cmd) {
1994 case TIOCSRS485:
1995 if (copy_from_user(&rs485conf, (struct serial_rs485 *) arg,
1996 sizeof(rs485conf)))
1997 return -EFAULT;
1998
1999 atmel_config_rs485(port, &rs485conf);
2000 break;
2001
2002 case TIOCGRS485:
2003 if (copy_to_user((struct serial_rs485 *) arg,
2004 &(to_atmel_uart_port(port)->rs485),
2005 sizeof(rs485conf)))
2006 return -EFAULT;
2007 break;
2008
2009 default:
2010 return -ENOIOCTLCMD;
2011 }
2012 return 0;
2013}
2014
2015
2016
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002017static struct uart_ops atmel_pops = {
2018 .tx_empty = atmel_tx_empty,
2019 .set_mctrl = atmel_set_mctrl,
2020 .get_mctrl = atmel_get_mctrl,
2021 .stop_tx = atmel_stop_tx,
2022 .start_tx = atmel_start_tx,
2023 .stop_rx = atmel_stop_rx,
2024 .enable_ms = atmel_enable_ms,
2025 .break_ctl = atmel_break_ctl,
2026 .startup = atmel_startup,
2027 .shutdown = atmel_shutdown,
Haavard Skinnemoen9afd5612008-07-16 21:52:46 +01002028 .flush_buffer = atmel_flush_buffer,
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002029 .set_termios = atmel_set_termios,
Viktar Palstsiuk42bd7a42011-02-09 15:26:13 +01002030 .set_ldisc = atmel_set_ldisc,
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002031 .type = atmel_type,
2032 .release_port = atmel_release_port,
2033 .request_port = atmel_request_port,
2034 .config_port = atmel_config_port,
2035 .verify_port = atmel_verify_port,
2036 .pm = atmel_serial_pm,
Claudio Scordinoe8faff72010-05-03 13:31:28 +01002037 .ioctl = atmel_ioctl,
Albin Tonnerre8fe2d542009-12-09 12:31:32 -08002038#ifdef CONFIG_CONSOLE_POLL
2039 .poll_get_char = atmel_poll_get_char,
2040 .poll_put_char = atmel_poll_put_char,
2041#endif
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002042};
2043
Andrew Victorafefc412006-06-19 19:53:19 +01002044/*
2045 * Configure the port from the platform device resource info.
2046 */
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002047static int atmel_init_port(struct atmel_uart_port *atmel_port,
Remy Bohmerb843aa22008-02-08 04:21:01 -08002048 struct platform_device *pdev)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002049{
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002050 int ret;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002051 struct uart_port *port = &atmel_port->uart;
Jingoo Han574de552013-07-30 17:06:57 +09002052 struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002053
Elen Song33d64c42013-07-22 16:30:28 +08002054 if (!atmel_init_property(atmel_port, pdev))
2055 atmel_set_ops(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002056
Elen Song33d64c42013-07-22 16:30:28 +08002057 atmel_init_rs485(atmel_port, pdev);
Elen Songa930e522013-07-22 16:30:25 +08002058
Claudio Scordinoe8faff72010-05-03 13:31:28 +01002059 port->iotype = UPIO_MEM;
2060 port->flags = UPF_BOOT_AUTOCONF;
2061 port->ops = &atmel_pops;
2062 port->fifosize = 1;
Claudio Scordinoe8faff72010-05-03 13:31:28 +01002063 port->dev = &pdev->dev;
Andrew Victorafefc412006-06-19 19:53:19 +01002064 port->mapbase = pdev->resource[0].start;
2065 port->irq = pdev->resource[1].start;
2066
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08002067 tasklet_init(&atmel_port->tasklet, atmel_tasklet_func,
2068 (unsigned long)port);
2069
2070 memset(&atmel_port->rx_ring, 0, sizeof(atmel_port->rx_ring));
2071
Nicolas Ferre5fbe46b2011-10-12 18:07:00 +02002072 if (pdata && pdata->regs) {
Haavard Skinnemoen75d35212006-10-04 16:02:08 +02002073 /* Already mapped by setup code */
Nicolas Ferre1acfc7e2011-10-12 18:06:57 +02002074 port->membase = pdata->regs;
Nicolas Ferre588edbf2011-10-12 18:06:58 +02002075 } else {
Andrew Victorafefc412006-06-19 19:53:19 +01002076 port->flags |= UPF_IOREMAP;
2077 port->membase = NULL;
2078 }
2079
Remy Bohmerb843aa22008-02-08 04:21:01 -08002080 /* for console, the clock could already be configured */
2081 if (!atmel_port->clk) {
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002082 atmel_port->clk = clk_get(&pdev->dev, "usart");
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002083 if (IS_ERR(atmel_port->clk)) {
2084 ret = PTR_ERR(atmel_port->clk);
2085 atmel_port->clk = NULL;
2086 return ret;
2087 }
2088 ret = clk_prepare_enable(atmel_port->clk);
2089 if (ret) {
2090 clk_put(atmel_port->clk);
2091 atmel_port->clk = NULL;
2092 return ret;
2093 }
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002094 port->uartclk = clk_get_rate(atmel_port->clk);
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002095 clk_disable_unprepare(atmel_port->clk);
David Brownell06a7f052008-11-06 12:53:40 -08002096 /* only enable clock when USART is in use */
Andrew Victorafefc412006-06-19 19:53:19 +01002097 }
Chip Coldwella6670612008-02-08 04:21:06 -08002098
Claudio Scordinoe8faff72010-05-03 13:31:28 +01002099 /* Use TXEMPTY for interrupt when rs485 else TXRDY or ENDTX|TXBUFE */
2100 if (atmel_port->rs485.flags & SER_RS485_ENABLED)
2101 atmel_port->tx_done_mask = ATMEL_US_TXEMPTY;
Elen Song64e22eb2013-07-22 16:30:24 +08002102 else if (atmel_use_pdc_tx(port)) {
Chip Coldwella6670612008-02-08 04:21:06 -08002103 port->fifosize = PDC_BUFFER_SIZE;
Claudio Scordinoe8faff72010-05-03 13:31:28 +01002104 atmel_port->tx_done_mask = ATMEL_US_ENDTX | ATMEL_US_TXBUFE;
2105 } else {
2106 atmel_port->tx_done_mask = ATMEL_US_TXRDY;
2107 }
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002108
2109 return 0;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002110}
2111
Jean-Christophe PLAGNIOL-VILLARD69f6a272012-02-16 00:24:07 +08002112struct platform_device *atmel_default_console_device; /* the serial console device */
2113
Haavard Skinnemoen749c4e62006-10-04 16:02:02 +02002114#ifdef CONFIG_SERIAL_ATMEL_CONSOLE
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002115static void atmel_console_putchar(struct uart_port *port, int ch)
Russell Kingd3587882006-03-20 20:00:09 +00002116{
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002117 while (!(UART_GET_CSR(port) & ATMEL_US_TXRDY))
Haavard Skinnemoen829dd812008-02-08 04:21:02 -08002118 cpu_relax();
Russell Kingd3587882006-03-20 20:00:09 +00002119 UART_PUT_CHAR(port, ch);
2120}
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002121
2122/*
2123 * Interrupts are disabled on entering
2124 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002125static void atmel_console_write(struct console *co, const char *s, u_int count)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002126{
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002127 struct uart_port *port = &atmel_ports[co->index].uart;
Claudio Scordinoe8faff72010-05-03 13:31:28 +01002128 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Russell Kingd3587882006-03-20 20:00:09 +00002129 unsigned int status, imr;
Marc Pignat39d4c922008-04-02 13:04:42 -07002130 unsigned int pdc_tx;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002131
2132 /*
Remy Bohmerb843aa22008-02-08 04:21:01 -08002133 * First, save IMR and then disable interrupts
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002134 */
Remy Bohmerb843aa22008-02-08 04:21:01 -08002135 imr = UART_GET_IMR(port);
Claudio Scordinoe8faff72010-05-03 13:31:28 +01002136 UART_PUT_IDR(port, ATMEL_US_RXRDY | atmel_port->tx_done_mask);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002137
Marc Pignat39d4c922008-04-02 13:04:42 -07002138 /* Store PDC transmit status and disable it */
2139 pdc_tx = UART_GET_PTSR(port) & ATMEL_PDC_TXTEN;
2140 UART_PUT_PTCR(port, ATMEL_PDC_TXTDIS);
2141
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002142 uart_console_write(port, s, count, atmel_console_putchar);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002143
2144 /*
Remy Bohmerb843aa22008-02-08 04:21:01 -08002145 * Finally, wait for transmitter to become empty
2146 * and restore IMR
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002147 */
2148 do {
2149 status = UART_GET_CSR(port);
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002150 } while (!(status & ATMEL_US_TXRDY));
Marc Pignat39d4c922008-04-02 13:04:42 -07002151
2152 /* Restore PDC transmit status */
2153 if (pdc_tx)
2154 UART_PUT_PTCR(port, ATMEL_PDC_TXTEN);
2155
Remy Bohmerb843aa22008-02-08 04:21:01 -08002156 /* set interrupts back the way they were */
2157 UART_PUT_IER(port, imr);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002158}
2159
2160/*
Remy Bohmerb843aa22008-02-08 04:21:01 -08002161 * If the port was already initialised (eg, by a boot loader),
2162 * try to determine the current setup.
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002163 */
Remy Bohmerb843aa22008-02-08 04:21:01 -08002164static void __init atmel_console_get_options(struct uart_port *port, int *baud,
2165 int *parity, int *bits)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002166{
2167 unsigned int mr, quot;
2168
Haavard Skinnemoen1c0fd822008-02-08 04:21:03 -08002169 /*
2170 * If the baud rate generator isn't running, the port wasn't
2171 * initialized by the boot loader.
2172 */
Haavard Skinnemoen9c81c5c2008-06-05 22:46:39 -07002173 quot = UART_GET_BRGR(port) & ATMEL_US_CD;
Haavard Skinnemoen1c0fd822008-02-08 04:21:03 -08002174 if (!quot)
2175 return;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002176
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002177 mr = UART_GET_MR(port) & ATMEL_US_CHRL;
2178 if (mr == ATMEL_US_CHRL_8)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002179 *bits = 8;
2180 else
2181 *bits = 7;
2182
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002183 mr = UART_GET_MR(port) & ATMEL_US_PAR;
2184 if (mr == ATMEL_US_PAR_EVEN)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002185 *parity = 'e';
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002186 else if (mr == ATMEL_US_PAR_ODD)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002187 *parity = 'o';
2188
Haavard Skinnemoen4d5e3922006-10-04 16:02:11 +02002189 /*
2190 * The serial core only rounds down when matching this to a
2191 * supported baud rate. Make sure we don't end up slightly
2192 * lower than one of those, as it would make us fall through
2193 * to a much lower baud rate than we really want.
2194 */
Haavard Skinnemoen4d5e3922006-10-04 16:02:11 +02002195 *baud = port->uartclk / (16 * (quot - 1));
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002196}
2197
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002198static int __init atmel_console_setup(struct console *co, char *options)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002199{
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002200 int ret;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002201 struct uart_port *port = &atmel_ports[co->index].uart;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002202 int baud = 115200;
2203 int bits = 8;
2204 int parity = 'n';
2205 int flow = 'n';
2206
Remy Bohmerb843aa22008-02-08 04:21:01 -08002207 if (port->membase == NULL) {
2208 /* Port not initialized yet - delay setup */
Andrew Victorafefc412006-06-19 19:53:19 +01002209 return -ENODEV;
Remy Bohmerb843aa22008-02-08 04:21:01 -08002210 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002211
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002212 ret = clk_prepare_enable(atmel_ports[co->index].clk);
2213 if (ret)
2214 return ret;
David Brownell06a7f052008-11-06 12:53:40 -08002215
Remy Bohmerb843aa22008-02-08 04:21:01 -08002216 UART_PUT_IDR(port, -1);
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002217 UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
2218 UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002219
2220 if (options)
2221 uart_parse_options(options, &baud, &parity, &bits, &flow);
2222 else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002223 atmel_console_get_options(port, &baud, &parity, &bits);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002224
2225 return uart_set_options(port, co, baud, parity, bits, flow);
2226}
2227
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002228static struct uart_driver atmel_uart;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002229
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002230static struct console atmel_console = {
2231 .name = ATMEL_DEVICENAME,
2232 .write = atmel_console_write,
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002233 .device = uart_console_device,
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002234 .setup = atmel_console_setup,
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002235 .flags = CON_PRINTBUFFER,
2236 .index = -1,
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002237 .data = &atmel_uart,
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002238};
2239
David Brownell06a7f052008-11-06 12:53:40 -08002240#define ATMEL_CONSOLE_DEVICE (&atmel_console)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002241
Andrew Victorafefc412006-06-19 19:53:19 +01002242/*
2243 * Early console initialization (before VM subsystem initialized).
2244 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002245static int __init atmel_console_init(void)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002246{
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002247 int ret;
Haavard Skinnemoen73e27982006-10-04 16:02:04 +02002248 if (atmel_default_console_device) {
Voss, Nikolaus0d0a3cc2011-08-10 14:02:29 +02002249 struct atmel_uart_data *pdata =
Jingoo Han574de552013-07-30 17:06:57 +09002250 dev_get_platdata(&atmel_default_console_device->dev);
Linus Torvaldsefb8d212011-10-26 15:11:09 +02002251 int id = pdata->num;
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002252 struct atmel_uart_port *port = &atmel_ports[id];
Voss, Nikolaus0d0a3cc2011-08-10 14:02:29 +02002253
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002254 port->backup_imr = 0;
2255 port->uart.line = id;
2256
2257 add_preferred_console(ATMEL_DEVICENAME, id, NULL);
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002258 ret = atmel_init_port(port, atmel_default_console_device);
2259 if (ret)
2260 return ret;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002261 register_console(&atmel_console);
Andrew Victorafefc412006-06-19 19:53:19 +01002262 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002263
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002264 return 0;
2265}
Remy Bohmerb843aa22008-02-08 04:21:01 -08002266
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002267console_initcall(atmel_console_init);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002268
Andrew Victorafefc412006-06-19 19:53:19 +01002269/*
2270 * Late console initialization.
2271 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002272static int __init atmel_late_console_init(void)
Andrew Victorafefc412006-06-19 19:53:19 +01002273{
Remy Bohmerb843aa22008-02-08 04:21:01 -08002274 if (atmel_default_console_device
2275 && !(atmel_console.flags & CON_ENABLED))
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002276 register_console(&atmel_console);
Andrew Victorafefc412006-06-19 19:53:19 +01002277
2278 return 0;
2279}
Remy Bohmerb843aa22008-02-08 04:21:01 -08002280
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002281core_initcall(atmel_late_console_init);
Andrew Victorafefc412006-06-19 19:53:19 +01002282
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002283static inline bool atmel_is_console_port(struct uart_port *port)
2284{
2285 return port->cons && port->cons->index == port->line;
2286}
2287
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002288#else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002289#define ATMEL_CONSOLE_DEVICE NULL
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002290
2291static inline bool atmel_is_console_port(struct uart_port *port)
2292{
2293 return false;
2294}
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002295#endif
2296
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002297static struct uart_driver atmel_uart = {
Remy Bohmerb843aa22008-02-08 04:21:01 -08002298 .owner = THIS_MODULE,
2299 .driver_name = "atmel_serial",
2300 .dev_name = ATMEL_DEVICENAME,
2301 .major = SERIAL_ATMEL_MAJOR,
2302 .minor = MINOR_START,
2303 .nr = ATMEL_MAX_UART,
2304 .cons = ATMEL_CONSOLE_DEVICE,
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002305};
2306
Andrew Victorafefc412006-06-19 19:53:19 +01002307#ifdef CONFIG_PM
Haavard Skinnemoenf826caa2008-02-24 14:34:45 +01002308static bool atmel_serial_clk_will_stop(void)
2309{
2310#ifdef CONFIG_ARCH_AT91
2311 return at91_suspend_entering_slow_clock();
2312#else
2313 return false;
2314#endif
2315}
2316
Remy Bohmerb843aa22008-02-08 04:21:01 -08002317static int atmel_serial_suspend(struct platform_device *pdev,
2318 pm_message_t state)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002319{
Andrew Victorafefc412006-06-19 19:53:19 +01002320 struct uart_port *port = platform_get_drvdata(pdev);
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08002321 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002322
Haavard Skinnemoene1c609e2008-03-14 14:54:13 +01002323 if (atmel_is_console_port(port) && console_suspend_enabled) {
2324 /* Drain the TX shifter */
2325 while (!(UART_GET_CSR(port) & ATMEL_US_TXEMPTY))
2326 cpu_relax();
2327 }
2328
Anti Sullinf05596d2008-09-22 13:57:54 -07002329 /* we can not wake up if we're running on slow clock */
2330 atmel_port->may_wakeup = device_may_wakeup(&pdev->dev);
2331 if (atmel_serial_clk_will_stop())
2332 device_set_wakeup_enable(&pdev->dev, 0);
2333
2334 uart_suspend_port(&atmel_uart, port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002335
2336 return 0;
2337}
2338
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002339static int atmel_serial_resume(struct platform_device *pdev)
Andrew Victorafefc412006-06-19 19:53:19 +01002340{
2341 struct uart_port *port = platform_get_drvdata(pdev);
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08002342 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Andrew Victorafefc412006-06-19 19:53:19 +01002343
Anti Sullinf05596d2008-09-22 13:57:54 -07002344 uart_resume_port(&atmel_uart, port);
2345 device_set_wakeup_enable(&pdev->dev, atmel_port->may_wakeup);
Andrew Victorafefc412006-06-19 19:53:19 +01002346
2347 return 0;
2348}
2349#else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002350#define atmel_serial_suspend NULL
2351#define atmel_serial_resume NULL
Andrew Victorafefc412006-06-19 19:53:19 +01002352#endif
2353
Bill Pemberton9671f092012-11-19 13:21:50 -05002354static int atmel_serial_probe(struct platform_device *pdev)
Andrew Victorafefc412006-06-19 19:53:19 +01002355{
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002356 struct atmel_uart_port *port;
Nicolas Ferre5fbe46b2011-10-12 18:07:00 +02002357 struct device_node *np = pdev->dev.of_node;
Jingoo Han574de552013-07-30 17:06:57 +09002358 struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08002359 void *data;
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002360 int ret = -ENODEV;
Andrew Victorafefc412006-06-19 19:53:19 +01002361
Haavard Skinnemoen9d09daf2009-10-26 16:50:02 -07002362 BUILD_BUG_ON(ATMEL_SERIAL_RINGSIZE & (ATMEL_SERIAL_RINGSIZE - 1));
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08002363
Nicolas Ferre5fbe46b2011-10-12 18:07:00 +02002364 if (np)
2365 ret = of_alias_get_id(np, "serial");
2366 else
2367 if (pdata)
2368 ret = pdata->num;
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002369
2370 if (ret < 0)
Nicolas Ferre5fbe46b2011-10-12 18:07:00 +02002371 /* port id not found in platform data nor device-tree aliases:
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002372 * auto-enumerate it */
Pawel Wieczorkiewicz503bded2013-02-20 17:26:20 +01002373 ret = find_first_zero_bit(atmel_ports_in_use, ATMEL_MAX_UART);
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002374
Pawel Wieczorkiewicz503bded2013-02-20 17:26:20 +01002375 if (ret >= ATMEL_MAX_UART) {
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002376 ret = -ENODEV;
2377 goto err;
2378 }
2379
Pawel Wieczorkiewicz503bded2013-02-20 17:26:20 +01002380 if (test_and_set_bit(ret, atmel_ports_in_use)) {
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002381 /* port already in use */
2382 ret = -EBUSY;
2383 goto err;
2384 }
2385
2386 port = &atmel_ports[ret];
Anti Sullinf05596d2008-09-22 13:57:54 -07002387 port->backup_imr = 0;
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002388 port->uart.line = ret;
Anti Sullinf05596d2008-09-22 13:57:54 -07002389
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002390 ret = atmel_init_port(port, pdev);
2391 if (ret)
2392 goto err;
Andrew Victorafefc412006-06-19 19:53:19 +01002393
Elen Song64e22eb2013-07-22 16:30:24 +08002394 if (!atmel_use_pdc_rx(&port->uart)) {
Chip Coldwella6670612008-02-08 04:21:06 -08002395 ret = -ENOMEM;
Haavard Skinnemoen64334712008-02-08 04:21:07 -08002396 data = kmalloc(sizeof(struct atmel_uart_char)
2397 * ATMEL_SERIAL_RINGSIZE, GFP_KERNEL);
Chip Coldwella6670612008-02-08 04:21:06 -08002398 if (!data)
2399 goto err_alloc_ring;
2400 port->rx_ring.buf = data;
2401 }
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08002402
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002403 ret = uart_add_one_port(&atmel_uart, &port->uart);
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002404 if (ret)
2405 goto err_add_port;
2406
Albin Tonnerre8da14b52009-07-29 15:04:18 -07002407#ifdef CONFIG_SERIAL_ATMEL_CONSOLE
David Brownell06a7f052008-11-06 12:53:40 -08002408 if (atmel_is_console_port(&port->uart)
2409 && ATMEL_CONSOLE_DEVICE->flags & CON_ENABLED) {
2410 /*
2411 * The serial core enabled the clock for us, so undo
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002412 * the clk_prepare_enable() in atmel_console_setup()
David Brownell06a7f052008-11-06 12:53:40 -08002413 */
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002414 clk_disable_unprepare(port->clk);
David Brownell06a7f052008-11-06 12:53:40 -08002415 }
Albin Tonnerre8da14b52009-07-29 15:04:18 -07002416#endif
David Brownell06a7f052008-11-06 12:53:40 -08002417
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002418 device_init_wakeup(&pdev->dev, 1);
2419 platform_set_drvdata(pdev, port);
2420
Claudio Scordino5dfbd1d72011-01-13 15:45:39 -08002421 if (port->rs485.flags & SER_RS485_ENABLED) {
2422 UART_PUT_MR(&port->uart, ATMEL_US_USMODE_NORMAL);
2423 UART_PUT_CR(&port->uart, ATMEL_US_RTSEN);
2424 }
2425
Elen Song055560b2013-07-22 16:30:29 +08002426 /*
2427 * Get port name of usart or uart
2428 */
Nicolas Ferre892db582013-10-17 17:37:11 +02002429 atmel_get_ip_name(&port->uart);
Elen Song055560b2013-07-22 16:30:29 +08002430
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002431 return 0;
2432
2433err_add_port:
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08002434 kfree(port->rx_ring.buf);
2435 port->rx_ring.buf = NULL;
2436err_alloc_ring:
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002437 if (!atmel_is_console_port(&port->uart)) {
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002438 clk_put(port->clk);
2439 port->clk = NULL;
Andrew Victorafefc412006-06-19 19:53:19 +01002440 }
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002441err:
Andrew Victorafefc412006-06-19 19:53:19 +01002442 return ret;
2443}
2444
Bill Pembertonae8d8a12012-11-19 13:26:18 -05002445static int atmel_serial_remove(struct platform_device *pdev)
Andrew Victorafefc412006-06-19 19:53:19 +01002446{
2447 struct uart_port *port = platform_get_drvdata(pdev);
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08002448 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Andrew Victorafefc412006-06-19 19:53:19 +01002449 int ret = 0;
2450
Marek Roszkof50c995f2014-01-07 11:45:07 +01002451 tasklet_kill(&atmel_port->tasklet);
2452
Andrew Victorafefc412006-06-19 19:53:19 +01002453 device_init_wakeup(&pdev->dev, 0);
Andrew Victorafefc412006-06-19 19:53:19 +01002454
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002455 ret = uart_remove_one_port(&atmel_uart, port);
2456
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08002457 kfree(atmel_port->rx_ring.buf);
2458
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002459 /* "port" is allocated statically, so we shouldn't free it */
2460
Pawel Wieczorkiewicz503bded2013-02-20 17:26:20 +01002461 clear_bit(port->line, atmel_ports_in_use);
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002462
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002463 clk_put(atmel_port->clk);
Andrew Victorafefc412006-06-19 19:53:19 +01002464
2465 return ret;
2466}
2467
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002468static struct platform_driver atmel_serial_driver = {
2469 .probe = atmel_serial_probe,
Bill Pemberton2d47b712012-11-19 13:21:34 -05002470 .remove = atmel_serial_remove,
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002471 .suspend = atmel_serial_suspend,
2472 .resume = atmel_serial_resume,
Andrew Victorafefc412006-06-19 19:53:19 +01002473 .driver = {
Haavard Skinnemoen1e8ea802006-10-04 16:02:03 +02002474 .name = "atmel_usart",
Andrew Victorafefc412006-06-19 19:53:19 +01002475 .owner = THIS_MODULE,
Nicolas Ferre5fbe46b2011-10-12 18:07:00 +02002476 .of_match_table = of_match_ptr(atmel_serial_dt_ids),
Andrew Victorafefc412006-06-19 19:53:19 +01002477 },
2478};
2479
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002480static int __init atmel_serial_init(void)
Andrew Victorafefc412006-06-19 19:53:19 +01002481{
2482 int ret;
2483
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002484 ret = uart_register_driver(&atmel_uart);
Andrew Victorafefc412006-06-19 19:53:19 +01002485 if (ret)
2486 return ret;
2487
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002488 ret = platform_driver_register(&atmel_serial_driver);
Andrew Victorafefc412006-06-19 19:53:19 +01002489 if (ret)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002490 uart_unregister_driver(&atmel_uart);
Andrew Victorafefc412006-06-19 19:53:19 +01002491
2492 return ret;
2493}
2494
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002495static void __exit atmel_serial_exit(void)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002496{
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002497 platform_driver_unregister(&atmel_serial_driver);
2498 uart_unregister_driver(&atmel_uart);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002499}
2500
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002501module_init(atmel_serial_init);
2502module_exit(atmel_serial_exit);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002503
2504MODULE_AUTHOR("Rick Bronson");
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002505MODULE_DESCRIPTION("Atmel AT91 / AT32 serial port driver");
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002506MODULE_LICENSE("GPL");
Kay Sieverse169c132008-04-15 14:34:35 -07002507MODULE_ALIAS("platform:atmel_usart");