blob: 8dbc3e67dfa44031c44c86d8640ee84d2415b332 [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>
Andrew Victor1e6c9c22006-01-10 16:59:27 +000043
44#include <asm/io.h>
Peter Huewef7512e72010-06-29 19:35:39 +020045#include <asm/ioctls.h>
Andrew Victor1e6c9c22006-01-10 16:59:27 +000046
Haavard Skinnemoenacca9b82006-10-04 16:02:09 +020047#ifdef CONFIG_ARM
Russell Kinga09e64f2008-08-05 16:14:15 +010048#include <mach/cpu.h>
Russell King60e89722011-07-26 10:56:19 +010049#include <asm/gpio.h>
Haavard Skinnemoenacca9b82006-10-04 16:02:09 +020050#endif
Andrew Victor1e6c9c22006-01-10 16:59:27 +000051
Chip Coldwella6670612008-02-08 04:21:06 -080052#define PDC_BUFFER_SIZE 512
53/* Revisit: We should calculate this based on the actual port settings */
54#define PDC_RX_TIMEOUT (3 * 10) /* 3 bytes */
55
Haavard Skinnemoen749c4e62006-10-04 16:02:02 +020056#if defined(CONFIG_SERIAL_ATMEL_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
Andrew Victor1e6c9c22006-01-10 16:59:27 +000057#define SUPPORT_SYSRQ
58#endif
59
60#include <linux/serial_core.h>
61
Claudio Scordinoe8faff72010-05-03 13:31:28 +010062static void atmel_start_rx(struct uart_port *port);
63static void atmel_stop_rx(struct uart_port *port);
64
Haavard Skinnemoen749c4e62006-10-04 16:02:02 +020065#ifdef CONFIG_SERIAL_ATMEL_TTYAT
Andrew Victor1e6c9c22006-01-10 16:59:27 +000066
67/* Use device name ttyAT, major 204 and minor 154-169. This is necessary if we
68 * should coexist with the 8250 driver, such as if we have an external 16C550
69 * UART. */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +020070#define SERIAL_ATMEL_MAJOR 204
Andrew Victor1e6c9c22006-01-10 16:59:27 +000071#define MINOR_START 154
Haavard Skinnemoen7192f922006-10-04 16:02:05 +020072#define ATMEL_DEVICENAME "ttyAT"
Andrew Victor1e6c9c22006-01-10 16:59:27 +000073
74#else
75
76/* Use device name ttyS, major 4, minor 64-68. This is the usual serial port
77 * name, but it is legally reserved for the 8250 driver. */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +020078#define SERIAL_ATMEL_MAJOR TTY_MAJOR
Andrew Victor1e6c9c22006-01-10 16:59:27 +000079#define MINOR_START 64
Haavard Skinnemoen7192f922006-10-04 16:02:05 +020080#define ATMEL_DEVICENAME "ttyS"
Andrew Victor1e6c9c22006-01-10 16:59:27 +000081
82#endif
83
Haavard Skinnemoen7192f922006-10-04 16:02:05 +020084#define ATMEL_ISR_PASS_LIMIT 256
Andrew Victor1e6c9c22006-01-10 16:59:27 +000085
Remy Bohmerb843aa22008-02-08 04:21:01 -080086/* UART registers. CR is write-only, hence no GET macro */
Haavard Skinnemoen544fc722007-02-12 00:52:15 -080087#define UART_PUT_CR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_CR)
88#define UART_GET_MR(port) __raw_readl((port)->membase + ATMEL_US_MR)
89#define UART_PUT_MR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_MR)
90#define UART_PUT_IER(port,v) __raw_writel(v, (port)->membase + ATMEL_US_IER)
91#define UART_PUT_IDR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_IDR)
92#define UART_GET_IMR(port) __raw_readl((port)->membase + ATMEL_US_IMR)
93#define UART_GET_CSR(port) __raw_readl((port)->membase + ATMEL_US_CSR)
94#define UART_GET_CHAR(port) __raw_readl((port)->membase + ATMEL_US_RHR)
95#define UART_PUT_CHAR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_THR)
96#define UART_GET_BRGR(port) __raw_readl((port)->membase + ATMEL_US_BRGR)
97#define UART_PUT_BRGR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_BRGR)
98#define UART_PUT_RTOR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_RTOR)
Claudio Scordinoe8faff72010-05-03 13:31:28 +010099#define UART_PUT_TTGR(port, v) __raw_writel(v, (port)->membase + ATMEL_US_TTGR)
Elen Song055560b2013-07-22 16:30:29 +0800100#define UART_GET_IP_NAME(port) __raw_readl((port)->membase + ATMEL_US_NAME)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000101
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000102 /* PDC registers */
Haavard Skinnemoen544fc722007-02-12 00:52:15 -0800103#define UART_PUT_PTCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_PTCR)
104#define UART_GET_PTSR(port) __raw_readl((port)->membase + ATMEL_PDC_PTSR)
Andrew Victorafefc412006-06-19 19:53:19 +0100105
Haavard Skinnemoen544fc722007-02-12 00:52:15 -0800106#define UART_PUT_RPR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RPR)
107#define UART_GET_RPR(port) __raw_readl((port)->membase + ATMEL_PDC_RPR)
108#define UART_PUT_RCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RCR)
109#define UART_PUT_RNPR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RNPR)
110#define UART_PUT_RNCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RNCR)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000111
Haavard Skinnemoen544fc722007-02-12 00:52:15 -0800112#define UART_PUT_TPR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_TPR)
113#define UART_PUT_TCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_TCR)
Marc Pignat39d4c922008-04-02 13:04:42 -0700114#define UART_GET_TCR(port) __raw_readl((port)->membase + ATMEL_PDC_TCR)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000115
Haavard Skinnemoen71f2e2b2006-10-04 16:02:07 +0200116static int (*atmel_open_hook)(struct uart_port *);
117static void (*atmel_close_hook)(struct uart_port *);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000118
Chip Coldwella6670612008-02-08 04:21:06 -0800119struct atmel_dma_buffer {
120 unsigned char *buf;
121 dma_addr_t dma_addr;
122 unsigned int dma_size;
123 unsigned int ofs;
124};
125
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800126struct atmel_uart_char {
127 u16 status;
128 u16 ch;
129};
130
131#define ATMEL_SERIAL_RINGSIZE 1024
132
Andrew Victorafefc412006-06-19 19:53:19 +0100133/*
134 * We wrap our port structure around the generic uart_port.
135 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200136struct atmel_uart_port {
Andrew Victorafefc412006-06-19 19:53:19 +0100137 struct uart_port uart; /* uart */
138 struct clk *clk; /* uart clock */
Anti Sullinf05596d2008-09-22 13:57:54 -0700139 int may_wakeup; /* cached value of device_may_wakeup for times we need to disable it */
140 u32 backup_imr; /* IMR saved during suspend */
Haavard Skinnemoen9e6077b2007-07-15 23:40:36 -0700141 int break_active; /* break being received */
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800142
Elen Song34df42f2013-07-22 16:30:27 +0800143 bool use_dma_rx; /* enable DMA receiver */
Elen Song64e22eb2013-07-22 16:30:24 +0800144 bool use_pdc_rx; /* enable PDC receiver */
Chip Coldwella6670612008-02-08 04:21:06 -0800145 short pdc_rx_idx; /* current PDC RX buffer */
146 struct atmel_dma_buffer pdc_rx[2]; /* PDC receier */
147
Elen Song08f738b2013-07-22 16:30:26 +0800148 bool use_dma_tx; /* enable DMA transmitter */
Elen Song64e22eb2013-07-22 16:30:24 +0800149 bool use_pdc_tx; /* enable PDC transmitter */
Chip Coldwella6670612008-02-08 04:21:06 -0800150 struct atmel_dma_buffer pdc_tx; /* PDC transmitter */
151
Elen Song08f738b2013-07-22 16:30:26 +0800152 spinlock_t lock_tx; /* port lock */
Elen Song34df42f2013-07-22 16:30:27 +0800153 spinlock_t lock_rx; /* port lock */
Elen Song08f738b2013-07-22 16:30:26 +0800154 struct dma_chan *chan_tx;
Elen Song34df42f2013-07-22 16:30:27 +0800155 struct dma_chan *chan_rx;
Elen Song08f738b2013-07-22 16:30:26 +0800156 struct dma_async_tx_descriptor *desc_tx;
Elen Song34df42f2013-07-22 16:30:27 +0800157 struct dma_async_tx_descriptor *desc_rx;
Elen Song08f738b2013-07-22 16:30:26 +0800158 dma_cookie_t cookie_tx;
Elen Song34df42f2013-07-22 16:30:27 +0800159 dma_cookie_t cookie_rx;
Elen Song08f738b2013-07-22 16:30:26 +0800160 struct scatterlist sg_tx;
Elen Song34df42f2013-07-22 16:30:27 +0800161 struct scatterlist sg_rx;
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800162 struct tasklet_struct tasklet;
163 unsigned int irq_status;
164 unsigned int irq_status_prev;
165
166 struct circ_buf rx_ring;
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100167
168 struct serial_rs485 rs485; /* rs485 settings */
169 unsigned int tx_done_mask;
Elen Song055560b2013-07-22 16:30:29 +0800170 bool is_usart; /* usart or uart */
Elen Songa930e522013-07-22 16:30:25 +0800171 int (*prepare_rx)(struct uart_port *port);
172 int (*prepare_tx)(struct uart_port *port);
173 void (*schedule_rx)(struct uart_port *port);
174 void (*schedule_tx)(struct uart_port *port);
175 void (*release_rx)(struct uart_port *port);
176 void (*release_tx)(struct uart_port *port);
Andrew Victorafefc412006-06-19 19:53:19 +0100177};
178
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200179static struct atmel_uart_port atmel_ports[ATMEL_MAX_UART];
Pawel Wieczorkiewicz503bded2013-02-20 17:26:20 +0100180static DECLARE_BITMAP(atmel_ports_in_use, ATMEL_MAX_UART);
Andrew Victorafefc412006-06-19 19:53:19 +0100181
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000182#ifdef SUPPORT_SYSRQ
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200183static struct console atmel_console;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000184#endif
185
Nicolas Ferre5fbe46b2011-10-12 18:07:00 +0200186#if defined(CONFIG_OF)
187static const struct of_device_id atmel_serial_dt_ids[] = {
188 { .compatible = "atmel,at91rm9200-usart" },
189 { .compatible = "atmel,at91sam9260-usart" },
190 { /* sentinel */ }
191};
192
193MODULE_DEVICE_TABLE(of, atmel_serial_dt_ids);
194#endif
195
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -0800196static inline struct atmel_uart_port *
197to_atmel_uart_port(struct uart_port *uart)
198{
199 return container_of(uart, struct atmel_uart_port, uart);
200}
201
Chip Coldwella6670612008-02-08 04:21:06 -0800202#ifdef CONFIG_SERIAL_ATMEL_PDC
Elen Song64e22eb2013-07-22 16:30:24 +0800203static bool atmel_use_pdc_rx(struct uart_port *port)
Chip Coldwella6670612008-02-08 04:21:06 -0800204{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -0800205 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Chip Coldwella6670612008-02-08 04:21:06 -0800206
Elen Song64e22eb2013-07-22 16:30:24 +0800207 return atmel_port->use_pdc_rx;
Chip Coldwella6670612008-02-08 04:21:06 -0800208}
209
Elen Song64e22eb2013-07-22 16:30:24 +0800210static bool atmel_use_pdc_tx(struct uart_port *port)
Chip Coldwella6670612008-02-08 04:21:06 -0800211{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -0800212 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Chip Coldwella6670612008-02-08 04:21:06 -0800213
Elen Song64e22eb2013-07-22 16:30:24 +0800214 return atmel_port->use_pdc_tx;
Chip Coldwella6670612008-02-08 04:21:06 -0800215}
216#else
Elen Song64e22eb2013-07-22 16:30:24 +0800217static bool atmel_use_pdc_rx(struct uart_port *port)
Chip Coldwella6670612008-02-08 04:21:06 -0800218{
219 return false;
220}
221
Elen Song64e22eb2013-07-22 16:30:24 +0800222static bool atmel_use_pdc_tx(struct uart_port *port)
Chip Coldwella6670612008-02-08 04:21:06 -0800223{
224 return false;
225}
226#endif
227
Elen Song08f738b2013-07-22 16:30:26 +0800228static bool atmel_use_dma_tx(struct uart_port *port)
229{
230 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
231
232 return atmel_port->use_dma_tx;
233}
234
Elen Song34df42f2013-07-22 16:30:27 +0800235static bool atmel_use_dma_rx(struct uart_port *port)
236{
237 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
238
239 return atmel_port->use_dma_rx;
240}
241
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100242/* Enable or disable the rs485 support */
243void atmel_config_rs485(struct uart_port *port, struct serial_rs485 *rs485conf)
244{
245 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
246 unsigned int mode;
Claudio Scordinodbf11152011-12-16 15:08:49 +0100247 unsigned long flags;
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100248
Claudio Scordinodbf11152011-12-16 15:08:49 +0100249 spin_lock_irqsave(&port->lock, flags);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100250
251 /* Disable interrupts */
252 UART_PUT_IDR(port, atmel_port->tx_done_mask);
253
254 mode = UART_GET_MR(port);
255
256 /* Resetting serial mode to RS232 (0x0) */
257 mode &= ~ATMEL_US_USMODE;
258
259 atmel_port->rs485 = *rs485conf;
260
261 if (rs485conf->flags & SER_RS485_ENABLED) {
262 dev_dbg(port->dev, "Setting UART to RS485\n");
263 atmel_port->tx_done_mask = ATMEL_US_TXEMPTY;
Claudio Scordino93f33502011-11-09 15:51:49 +0100264 if ((rs485conf->delay_rts_after_send) > 0)
Claudio Scordino1b633182010-07-20 15:26:47 -0700265 UART_PUT_TTGR(port, rs485conf->delay_rts_after_send);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100266 mode |= ATMEL_US_USMODE_RS485;
267 } else {
268 dev_dbg(port->dev, "Setting UART to RS232\n");
Elen Song64e22eb2013-07-22 16:30:24 +0800269 if (atmel_use_pdc_tx(port))
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100270 atmel_port->tx_done_mask = ATMEL_US_ENDTX |
271 ATMEL_US_TXBUFE;
272 else
273 atmel_port->tx_done_mask = ATMEL_US_TXRDY;
274 }
275 UART_PUT_MR(port, mode);
276
277 /* Enable interrupts */
278 UART_PUT_IER(port, atmel_port->tx_done_mask);
279
Claudio Scordinodbf11152011-12-16 15:08:49 +0100280 spin_unlock_irqrestore(&port->lock, flags);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100281
282}
283
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000284/*
285 * Return TIOCSER_TEMT when transmitter FIFO and Shift register is empty.
286 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200287static u_int atmel_tx_empty(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000288{
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200289 return (UART_GET_CSR(port) & ATMEL_US_TXEMPTY) ? TIOCSER_TEMT : 0;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000290}
291
292/*
293 * Set state of the modem control output lines
294 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200295static void atmel_set_mctrl(struct uart_port *port, u_int mctrl)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000296{
297 unsigned int control = 0;
Andrew Victorafefc412006-06-19 19:53:19 +0100298 unsigned int mode;
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100299 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000300
Andrew Victorc2f5ccf2006-12-04 09:37:20 +0100301#ifdef CONFIG_ARCH_AT91RM9200
Andrew Victor79da7a62006-11-30 11:58:14 +0100302 if (cpu_is_at91rm9200()) {
Andrew Victorafefc412006-06-19 19:53:19 +0100303 /*
Remy Bohmerb843aa22008-02-08 04:21:01 -0800304 * AT91RM9200 Errata #39: RTS0 is not internally connected
305 * to PA21. We need to drive the pin manually.
Andrew Victorafefc412006-06-19 19:53:19 +0100306 */
Andrew Victor72729912006-09-27 09:44:11 +0100307 if (port->mapbase == AT91RM9200_BASE_US0) {
Andrew Victorafefc412006-06-19 19:53:19 +0100308 if (mctrl & TIOCM_RTS)
Andrew Victor20e65272006-07-05 14:31:13 +0100309 at91_set_gpio_value(AT91_PIN_PA21, 0);
Andrew Victorafefc412006-06-19 19:53:19 +0100310 else
Andrew Victor20e65272006-07-05 14:31:13 +0100311 at91_set_gpio_value(AT91_PIN_PA21, 1);
Andrew Victorafefc412006-06-19 19:53:19 +0100312 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000313 }
Haavard Skinnemoenacca9b82006-10-04 16:02:09 +0200314#endif
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000315
316 if (mctrl & TIOCM_RTS)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200317 control |= ATMEL_US_RTSEN;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000318 else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200319 control |= ATMEL_US_RTSDIS;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000320
321 if (mctrl & TIOCM_DTR)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200322 control |= ATMEL_US_DTREN;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000323 else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200324 control |= ATMEL_US_DTRDIS;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000325
Andrew Victorafefc412006-06-19 19:53:19 +0100326 UART_PUT_CR(port, control);
327
328 /* Local loopback mode? */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200329 mode = UART_GET_MR(port) & ~ATMEL_US_CHMODE;
Andrew Victorafefc412006-06-19 19:53:19 +0100330 if (mctrl & TIOCM_LOOP)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200331 mode |= ATMEL_US_CHMODE_LOC_LOOP;
Andrew Victorafefc412006-06-19 19:53:19 +0100332 else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200333 mode |= ATMEL_US_CHMODE_NORMAL;
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100334
335 /* Resetting serial mode to RS232 (0x0) */
336 mode &= ~ATMEL_US_USMODE;
337
338 if (atmel_port->rs485.flags & SER_RS485_ENABLED) {
339 dev_dbg(port->dev, "Setting UART to RS485\n");
Claudio Scordino93f33502011-11-09 15:51:49 +0100340 if ((atmel_port->rs485.delay_rts_after_send) > 0)
Claudio Scordino1b633182010-07-20 15:26:47 -0700341 UART_PUT_TTGR(port,
342 atmel_port->rs485.delay_rts_after_send);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100343 mode |= ATMEL_US_USMODE_RS485;
344 } else {
345 dev_dbg(port->dev, "Setting UART to RS232\n");
346 }
Andrew Victorafefc412006-06-19 19:53:19 +0100347 UART_PUT_MR(port, mode);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000348}
349
350/*
351 * Get state of the modem control input lines
352 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200353static u_int atmel_get_mctrl(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000354{
355 unsigned int status, ret = 0;
356
357 status = UART_GET_CSR(port);
358
359 /*
360 * The control signals are active low.
361 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200362 if (!(status & ATMEL_US_DCD))
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000363 ret |= TIOCM_CD;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200364 if (!(status & ATMEL_US_CTS))
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000365 ret |= TIOCM_CTS;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200366 if (!(status & ATMEL_US_DSR))
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000367 ret |= TIOCM_DSR;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200368 if (!(status & ATMEL_US_RI))
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000369 ret |= TIOCM_RI;
370
371 return ret;
372}
373
374/*
375 * Stop transmitting.
376 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200377static void atmel_stop_tx(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000378{
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100379 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
380
Elen Song64e22eb2013-07-22 16:30:24 +0800381 if (atmel_use_pdc_tx(port)) {
Chip Coldwella6670612008-02-08 04:21:06 -0800382 /* disable PDC transmit */
383 UART_PUT_PTCR(port, ATMEL_PDC_TXTDIS);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100384 }
385 /* Disable interrupts */
386 UART_PUT_IDR(port, atmel_port->tx_done_mask);
387
Bernhard Roth83cac9f2011-08-24 09:48:23 +0200388 if ((atmel_port->rs485.flags & SER_RS485_ENABLED) &&
389 !(atmel_port->rs485.flags & SER_RS485_RX_DURING_TX))
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100390 atmel_start_rx(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000391}
392
393/*
394 * Start transmitting.
395 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200396static void atmel_start_tx(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000397{
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100398 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
399
Elen Song64e22eb2013-07-22 16:30:24 +0800400 if (atmel_use_pdc_tx(port)) {
Chip Coldwella6670612008-02-08 04:21:06 -0800401 if (UART_GET_PTSR(port) & ATMEL_PDC_TXTEN)
402 /* The transmitter is already running. Yes, we
403 really need this.*/
404 return;
405
Bernhard Roth83cac9f2011-08-24 09:48:23 +0200406 if ((atmel_port->rs485.flags & SER_RS485_ENABLED) &&
407 !(atmel_port->rs485.flags & SER_RS485_RX_DURING_TX))
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100408 atmel_stop_rx(port);
409
Chip Coldwella6670612008-02-08 04:21:06 -0800410 /* re-enable PDC transmit */
411 UART_PUT_PTCR(port, ATMEL_PDC_TXTEN);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100412 }
413 /* Enable interrupts */
414 UART_PUT_IER(port, atmel_port->tx_done_mask);
415}
416
417/*
418 * start receiving - port is in process of being opened.
419 */
420static void atmel_start_rx(struct uart_port *port)
421{
422 UART_PUT_CR(port, ATMEL_US_RSTSTA); /* reset status and receiver */
423
Siftar, Gabe57c36862012-03-29 15:40:05 +0200424 UART_PUT_CR(port, ATMEL_US_RXEN);
425
Elen Song64e22eb2013-07-22 16:30:24 +0800426 if (atmel_use_pdc_rx(port)) {
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100427 /* enable PDC controller */
428 UART_PUT_IER(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT |
429 port->read_status_mask);
430 UART_PUT_PTCR(port, ATMEL_PDC_RXTEN);
431 } else {
432 UART_PUT_IER(port, ATMEL_US_RXRDY);
433 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000434}
435
436/*
437 * Stop receiving - port is in process of being closed.
438 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200439static void atmel_stop_rx(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000440{
Siftar, Gabe57c36862012-03-29 15:40:05 +0200441 UART_PUT_CR(port, ATMEL_US_RXDIS);
442
Elen Song64e22eb2013-07-22 16:30:24 +0800443 if (atmel_use_pdc_rx(port)) {
Chip Coldwella6670612008-02-08 04:21:06 -0800444 /* disable PDC receive */
445 UART_PUT_PTCR(port, ATMEL_PDC_RXTDIS);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100446 UART_PUT_IDR(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT |
447 port->read_status_mask);
448 } else {
Chip Coldwella6670612008-02-08 04:21:06 -0800449 UART_PUT_IDR(port, ATMEL_US_RXRDY);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100450 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000451}
452
453/*
454 * Enable modem status interrupts
455 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200456static void atmel_enable_ms(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000457{
Remy Bohmerb843aa22008-02-08 04:21:01 -0800458 UART_PUT_IER(port, ATMEL_US_RIIC | ATMEL_US_DSRIC
459 | ATMEL_US_DCDIC | ATMEL_US_CTSIC);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000460}
461
462/*
463 * Control the transmission of a break signal
464 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200465static void atmel_break_ctl(struct uart_port *port, int break_state)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000466{
467 if (break_state != 0)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200468 UART_PUT_CR(port, ATMEL_US_STTBRK); /* start break */
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000469 else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200470 UART_PUT_CR(port, ATMEL_US_STPBRK); /* stop break */
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000471}
472
473/*
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800474 * Stores the incoming character in the ring buffer
475 */
476static void
477atmel_buffer_rx_char(struct uart_port *port, unsigned int status,
478 unsigned int ch)
479{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -0800480 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800481 struct circ_buf *ring = &atmel_port->rx_ring;
482 struct atmel_uart_char *c;
483
484 if (!CIRC_SPACE(ring->head, ring->tail, ATMEL_SERIAL_RINGSIZE))
485 /* Buffer overflow, ignore char */
486 return;
487
488 c = &((struct atmel_uart_char *)ring->buf)[ring->head];
489 c->status = status;
490 c->ch = ch;
491
492 /* Make sure the character is stored before we update head. */
493 smp_wmb();
494
495 ring->head = (ring->head + 1) & (ATMEL_SERIAL_RINGSIZE - 1);
496}
497
498/*
Chip Coldwella6670612008-02-08 04:21:06 -0800499 * Deal with parity, framing and overrun errors.
500 */
501static void atmel_pdc_rxerr(struct uart_port *port, unsigned int status)
502{
503 /* clear error */
504 UART_PUT_CR(port, ATMEL_US_RSTSTA);
505
506 if (status & ATMEL_US_RXBRK) {
507 /* ignore side-effect */
508 status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME);
509 port->icount.brk++;
510 }
511 if (status & ATMEL_US_PARE)
512 port->icount.parity++;
513 if (status & ATMEL_US_FRAME)
514 port->icount.frame++;
515 if (status & ATMEL_US_OVRE)
516 port->icount.overrun++;
517}
518
519/*
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000520 * Characters received (called from interrupt handler)
521 */
David Howells7d12e782006-10-05 14:55:46 +0100522static void atmel_rx_chars(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000523{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -0800524 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800525 unsigned int status, ch;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000526
Andrew Victorafefc412006-06-19 19:53:19 +0100527 status = UART_GET_CSR(port);
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200528 while (status & ATMEL_US_RXRDY) {
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000529 ch = UART_GET_CHAR(port);
530
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000531 /*
532 * note that the error handling code is
533 * out of the main execution path
534 */
Haavard Skinnemoen9e6077b2007-07-15 23:40:36 -0700535 if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME
536 | ATMEL_US_OVRE | ATMEL_US_RXBRK)
537 || atmel_port->break_active)) {
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800538
Remy Bohmerb843aa22008-02-08 04:21:01 -0800539 /* clear error */
540 UART_PUT_CR(port, ATMEL_US_RSTSTA);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800541
Haavard Skinnemoen9e6077b2007-07-15 23:40:36 -0700542 if (status & ATMEL_US_RXBRK
543 && !atmel_port->break_active) {
Haavard Skinnemoen9e6077b2007-07-15 23:40:36 -0700544 atmel_port->break_active = 1;
545 UART_PUT_IER(port, ATMEL_US_RXBRK);
Haavard Skinnemoen9e6077b2007-07-15 23:40:36 -0700546 } else {
547 /*
548 * This is either the end-of-break
549 * condition or we've received at
550 * least one character without RXBRK
551 * being set. In both cases, the next
552 * RXBRK will indicate start-of-break.
553 */
554 UART_PUT_IDR(port, ATMEL_US_RXBRK);
555 status &= ~ATMEL_US_RXBRK;
556 atmel_port->break_active = 0;
Andrew Victorafefc412006-06-19 19:53:19 +0100557 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000558 }
559
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800560 atmel_buffer_rx_char(port, status, ch);
Andrew Victorafefc412006-06-19 19:53:19 +0100561 status = UART_GET_CSR(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000562 }
563
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800564 tasklet_schedule(&atmel_port->tasklet);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000565}
566
567/*
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800568 * Transmit characters (called from tasklet with TXRDY interrupt
569 * disabled)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000570 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200571static void atmel_tx_chars(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000572{
Alan Coxebd2c8f2009-09-19 13:13:28 -0700573 struct circ_buf *xmit = &port->state->xmit;
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100574 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000575
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100576 if (port->x_char && UART_GET_CSR(port) & atmel_port->tx_done_mask) {
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000577 UART_PUT_CHAR(port, port->x_char);
578 port->icount.tx++;
579 port->x_char = 0;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000580 }
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800581 if (uart_circ_empty(xmit) || uart_tx_stopped(port))
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000582 return;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000583
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100584 while (UART_GET_CSR(port) & atmel_port->tx_done_mask) {
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000585 UART_PUT_CHAR(port, xmit->buf[xmit->tail]);
586 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
587 port->icount.tx++;
588 if (uart_circ_empty(xmit))
589 break;
590 }
591
592 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
593 uart_write_wakeup(port);
594
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800595 if (!uart_circ_empty(xmit))
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100596 /* Enable interrupts */
597 UART_PUT_IER(port, atmel_port->tx_done_mask);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000598}
599
Elen Song08f738b2013-07-22 16:30:26 +0800600static void atmel_complete_tx_dma(void *arg)
601{
602 struct atmel_uart_port *atmel_port = arg;
603 struct uart_port *port = &atmel_port->uart;
604 struct circ_buf *xmit = &port->state->xmit;
605 struct dma_chan *chan = atmel_port->chan_tx;
606 unsigned long flags;
607
608 spin_lock_irqsave(&port->lock, flags);
609
610 if (chan)
611 dmaengine_terminate_all(chan);
612 xmit->tail += sg_dma_len(&atmel_port->sg_tx);
613 xmit->tail &= UART_XMIT_SIZE - 1;
614
615 port->icount.tx += sg_dma_len(&atmel_port->sg_tx);
616
617 spin_lock_irq(&atmel_port->lock_tx);
618 async_tx_ack(atmel_port->desc_tx);
619 atmel_port->cookie_tx = -EINVAL;
620 atmel_port->desc_tx = NULL;
621 spin_unlock_irq(&atmel_port->lock_tx);
622
623 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
624 uart_write_wakeup(port);
625
626 /* Do we really need this? */
627 if (!uart_circ_empty(xmit))
628 tasklet_schedule(&atmel_port->tasklet);
629
630 spin_unlock_irqrestore(&port->lock, flags);
631}
632
633static void atmel_release_tx_dma(struct uart_port *port)
634{
635 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
636 struct dma_chan *chan = atmel_port->chan_tx;
637
638 if (chan) {
639 dmaengine_terminate_all(chan);
640 dma_release_channel(chan);
641 dma_unmap_sg(port->dev, &atmel_port->sg_tx, 1,
642 DMA_MEM_TO_DEV);
643 }
644
645 atmel_port->desc_tx = NULL;
646 atmel_port->chan_tx = NULL;
647 atmel_port->cookie_tx = -EINVAL;
648}
649
650/*
651 * Called from tasklet with TXRDY interrupt is disabled.
652 */
653static void atmel_tx_dma(struct uart_port *port)
654{
655 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
656 struct circ_buf *xmit = &port->state->xmit;
657 struct dma_chan *chan = atmel_port->chan_tx;
658 struct dma_async_tx_descriptor *desc;
659 struct scatterlist *sg = &atmel_port->sg_tx;
660
661 /* Make sure we have an idle channel */
662 if (atmel_port->desc_tx != NULL)
663 return;
664
665 if (!uart_circ_empty(xmit) && !uart_tx_stopped(port)) {
666 /*
667 * DMA is idle now.
668 * Port xmit buffer is already mapped,
669 * and it is one page... Just adjust
670 * offsets and lengths. Since it is a circular buffer,
671 * we have to transmit till the end, and then the rest.
672 * Take the port lock to get a
673 * consistent xmit buffer state.
674 */
675 sg->offset = xmit->tail & (UART_XMIT_SIZE - 1);
676 sg_dma_address(sg) = (sg_dma_address(sg) &
677 ~(UART_XMIT_SIZE - 1))
678 + sg->offset;
679 sg_dma_len(sg) = CIRC_CNT_TO_END(xmit->head,
680 xmit->tail,
681 UART_XMIT_SIZE);
682 BUG_ON(!sg_dma_len(sg));
683
684 desc = dmaengine_prep_slave_sg(chan,
685 sg,
686 1,
687 DMA_MEM_TO_DEV,
688 DMA_PREP_INTERRUPT |
689 DMA_CTRL_ACK);
690 if (!desc) {
691 dev_err(port->dev, "Failed to send via dma!\n");
692 return;
693 }
694
695 dma_sync_sg_for_device(port->dev, sg, 1, DMA_MEM_TO_DEV);
696
697 atmel_port->desc_tx = desc;
698 desc->callback = atmel_complete_tx_dma;
699 desc->callback_param = atmel_port;
700 atmel_port->cookie_tx = dmaengine_submit(desc);
701
702 } else {
703 if (atmel_port->rs485.flags & SER_RS485_ENABLED) {
704 /* DMA done, stop TX, start RX for RS485 */
705 atmel_start_rx(port);
706 }
707 }
708
709 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
710 uart_write_wakeup(port);
711}
712
713static int atmel_prepare_tx_dma(struct uart_port *port)
714{
715 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
716 dma_cap_mask_t mask;
717 struct dma_slave_config config;
718 int ret, nent;
719
720 dma_cap_zero(mask);
721 dma_cap_set(DMA_SLAVE, mask);
722
723 atmel_port->chan_tx = dma_request_slave_channel(port->dev, "tx");
724 if (atmel_port->chan_tx == NULL)
725 goto chan_err;
726 dev_info(port->dev, "using %s for tx DMA transfers\n",
727 dma_chan_name(atmel_port->chan_tx));
728
729 spin_lock_init(&atmel_port->lock_tx);
730 sg_init_table(&atmel_port->sg_tx, 1);
731 /* UART circular tx buffer is an aligned page. */
732 BUG_ON((int)port->state->xmit.buf & ~PAGE_MASK);
733 sg_set_page(&atmel_port->sg_tx,
734 virt_to_page(port->state->xmit.buf),
735 UART_XMIT_SIZE,
736 (int)port->state->xmit.buf & ~PAGE_MASK);
737 nent = dma_map_sg(port->dev,
738 &atmel_port->sg_tx,
739 1,
740 DMA_MEM_TO_DEV);
741
742 if (!nent) {
743 dev_dbg(port->dev, "need to release resource of dma\n");
744 goto chan_err;
745 } else {
746 dev_dbg(port->dev, "%s: mapped %d@%p to %x\n", __func__,
747 sg_dma_len(&atmel_port->sg_tx),
748 port->state->xmit.buf,
749 sg_dma_address(&atmel_port->sg_tx));
750 }
751
752 /* Configure the slave DMA */
753 memset(&config, 0, sizeof(config));
754 config.direction = DMA_MEM_TO_DEV;
755 config.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
756 config.dst_addr = port->mapbase + ATMEL_US_THR;
757
758 ret = dmaengine_device_control(atmel_port->chan_tx,
759 DMA_SLAVE_CONFIG,
760 (unsigned long)&config);
761 if (ret) {
762 dev_err(port->dev, "DMA tx slave configuration failed\n");
763 goto chan_err;
764 }
765
766 return 0;
767
768chan_err:
769 dev_err(port->dev, "TX channel not available, switch to pio\n");
770 atmel_port->use_dma_tx = 0;
771 if (atmel_port->chan_tx)
772 atmel_release_tx_dma(port);
773 return -EINVAL;
774}
775
Elen Song34df42f2013-07-22 16:30:27 +0800776static void atmel_flip_buffer_rx_dma(struct uart_port *port,
777 char *buf, size_t count)
778{
779 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
780 struct tty_port *tport = &port->state->port;
781
782 dma_sync_sg_for_cpu(port->dev,
783 &atmel_port->sg_rx,
784 1,
785 DMA_DEV_TO_MEM);
786
787 tty_insert_flip_string(tport, buf, count);
788
789 dma_sync_sg_for_device(port->dev,
790 &atmel_port->sg_rx,
791 1,
792 DMA_DEV_TO_MEM);
793 /*
794 * Drop the lock here since it might end up calling
795 * uart_start(), which takes the lock.
796 */
797 spin_unlock(&port->lock);
798 tty_flip_buffer_push(tport);
799 spin_lock(&port->lock);
800}
801
802static void atmel_complete_rx_dma(void *arg)
803{
804 struct uart_port *port = arg;
805 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
806
807 tasklet_schedule(&atmel_port->tasklet);
808}
809
810static void atmel_release_rx_dma(struct uart_port *port)
811{
812 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
813 struct dma_chan *chan = atmel_port->chan_rx;
814
815 if (chan) {
816 dmaengine_terminate_all(chan);
817 dma_release_channel(chan);
818 dma_unmap_sg(port->dev, &atmel_port->sg_rx, 1,
819 DMA_DEV_TO_MEM);
820 }
821
822 atmel_port->desc_rx = NULL;
823 atmel_port->chan_rx = NULL;
824 atmel_port->cookie_rx = -EINVAL;
825}
826
827static void atmel_rx_from_dma(struct uart_port *port)
828{
829 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
830 struct circ_buf *ring = &atmel_port->rx_ring;
831 struct dma_chan *chan = atmel_port->chan_rx;
832 struct dma_tx_state state;
833 enum dma_status dmastat;
834 size_t pending, count;
835
836
837 /* Reset the UART timeout early so that we don't miss one */
838 UART_PUT_CR(port, ATMEL_US_STTTO);
839 dmastat = dmaengine_tx_status(chan,
840 atmel_port->cookie_rx,
841 &state);
842 /* Restart a new tasklet if DMA status is error */
843 if (dmastat == DMA_ERROR) {
844 dev_dbg(port->dev, "Get residue error, restart tasklet\n");
845 UART_PUT_IER(port, ATMEL_US_TIMEOUT);
846 tasklet_schedule(&atmel_port->tasklet);
847 return;
848 }
849 /* current transfer size should no larger than dma buffer */
850 pending = sg_dma_len(&atmel_port->sg_rx) - state.residue;
851 BUG_ON(pending > sg_dma_len(&atmel_port->sg_rx));
852
853 /*
854 * This will take the chars we have so far,
855 * ring->head will record the transfer size, only new bytes come
856 * will insert into the framework.
857 */
858 if (pending > ring->head) {
859 count = pending - ring->head;
860
861 atmel_flip_buffer_rx_dma(port, ring->buf + ring->head, count);
862
863 ring->head += count;
864 if (ring->head == sg_dma_len(&atmel_port->sg_rx))
865 ring->head = 0;
866
867 port->icount.rx += count;
868 }
869
870 UART_PUT_IER(port, ATMEL_US_TIMEOUT);
871}
872
873static int atmel_prepare_rx_dma(struct uart_port *port)
874{
875 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
876 struct dma_async_tx_descriptor *desc;
877 dma_cap_mask_t mask;
878 struct dma_slave_config config;
879 struct circ_buf *ring;
880 int ret, nent;
881
882 ring = &atmel_port->rx_ring;
883
884 dma_cap_zero(mask);
885 dma_cap_set(DMA_CYCLIC, mask);
886
887 atmel_port->chan_rx = dma_request_slave_channel(port->dev, "rx");
888 if (atmel_port->chan_rx == NULL)
889 goto chan_err;
890 dev_info(port->dev, "using %s for rx DMA transfers\n",
891 dma_chan_name(atmel_port->chan_rx));
892
893 spin_lock_init(&atmel_port->lock_rx);
894 sg_init_table(&atmel_port->sg_rx, 1);
895 /* UART circular rx buffer is an aligned page. */
896 BUG_ON((int)port->state->xmit.buf & ~PAGE_MASK);
897 sg_set_page(&atmel_port->sg_rx,
898 virt_to_page(ring->buf),
899 ATMEL_SERIAL_RINGSIZE,
900 (int)ring->buf & ~PAGE_MASK);
901 nent = dma_map_sg(port->dev,
902 &atmel_port->sg_rx,
903 1,
904 DMA_DEV_TO_MEM);
905
906 if (!nent) {
907 dev_dbg(port->dev, "need to release resource of dma\n");
908 goto chan_err;
909 } else {
910 dev_dbg(port->dev, "%s: mapped %d@%p to %x\n", __func__,
911 sg_dma_len(&atmel_port->sg_rx),
912 ring->buf,
913 sg_dma_address(&atmel_port->sg_rx));
914 }
915
916 /* Configure the slave DMA */
917 memset(&config, 0, sizeof(config));
918 config.direction = DMA_DEV_TO_MEM;
919 config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
920 config.src_addr = port->mapbase + ATMEL_US_RHR;
921
922 ret = dmaengine_device_control(atmel_port->chan_rx,
923 DMA_SLAVE_CONFIG,
924 (unsigned long)&config);
925 if (ret) {
926 dev_err(port->dev, "DMA rx slave configuration failed\n");
927 goto chan_err;
928 }
929 /*
930 * Prepare a cyclic dma transfer, assign 2 descriptors,
931 * each one is half ring buffer size
932 */
933 desc = dmaengine_prep_dma_cyclic(atmel_port->chan_rx,
934 sg_dma_address(&atmel_port->sg_rx),
935 sg_dma_len(&atmel_port->sg_rx),
936 sg_dma_len(&atmel_port->sg_rx)/2,
937 DMA_DEV_TO_MEM,
938 DMA_PREP_INTERRUPT);
939 desc->callback = atmel_complete_rx_dma;
940 desc->callback_param = port;
941 atmel_port->desc_rx = desc;
942 atmel_port->cookie_rx = dmaengine_submit(desc);
943
944 return 0;
945
946chan_err:
947 dev_err(port->dev, "RX channel not available, switch to pio\n");
948 atmel_port->use_dma_rx = 0;
949 if (atmel_port->chan_rx)
950 atmel_release_rx_dma(port);
951 return -EINVAL;
952}
953
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000954/*
Remy Bohmerb843aa22008-02-08 04:21:01 -0800955 * receive interrupt handler.
956 */
957static void
958atmel_handle_receive(struct uart_port *port, unsigned int pending)
959{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -0800960 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Remy Bohmerb843aa22008-02-08 04:21:01 -0800961
Elen Song64e22eb2013-07-22 16:30:24 +0800962 if (atmel_use_pdc_rx(port)) {
Chip Coldwella6670612008-02-08 04:21:06 -0800963 /*
964 * PDC receive. Just schedule the tasklet and let it
965 * figure out the details.
966 *
967 * TODO: We're not handling error flags correctly at
968 * the moment.
969 */
970 if (pending & (ATMEL_US_ENDRX | ATMEL_US_TIMEOUT)) {
971 UART_PUT_IDR(port, (ATMEL_US_ENDRX
972 | ATMEL_US_TIMEOUT));
973 tasklet_schedule(&atmel_port->tasklet);
974 }
975
976 if (pending & (ATMEL_US_RXBRK | ATMEL_US_OVRE |
977 ATMEL_US_FRAME | ATMEL_US_PARE))
978 atmel_pdc_rxerr(port, pending);
979 }
980
Elen Song34df42f2013-07-22 16:30:27 +0800981 if (atmel_use_dma_rx(port)) {
982 if (pending & ATMEL_US_TIMEOUT) {
983 UART_PUT_IDR(port, ATMEL_US_TIMEOUT);
984 tasklet_schedule(&atmel_port->tasklet);
985 }
986 }
987
Remy Bohmerb843aa22008-02-08 04:21:01 -0800988 /* Interrupt receive */
989 if (pending & ATMEL_US_RXRDY)
990 atmel_rx_chars(port);
991 else if (pending & ATMEL_US_RXBRK) {
992 /*
993 * End of break detected. If it came along with a
994 * character, atmel_rx_chars will handle it.
995 */
996 UART_PUT_CR(port, ATMEL_US_RSTSTA);
997 UART_PUT_IDR(port, ATMEL_US_RXBRK);
998 atmel_port->break_active = 0;
999 }
1000}
1001
1002/*
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001003 * transmit interrupt handler. (Transmit is IRQF_NODELAY safe)
Remy Bohmerb843aa22008-02-08 04:21:01 -08001004 */
1005static void
1006atmel_handle_transmit(struct uart_port *port, unsigned int pending)
1007{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001008 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001009
Claudio Scordinoe8faff72010-05-03 13:31:28 +01001010 if (pending & atmel_port->tx_done_mask) {
1011 /* Either PDC or interrupt transmission */
1012 UART_PUT_IDR(port, atmel_port->tx_done_mask);
1013 tasklet_schedule(&atmel_port->tasklet);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001014 }
Remy Bohmerb843aa22008-02-08 04:21:01 -08001015}
1016
1017/*
1018 * status flags interrupt handler.
1019 */
1020static void
1021atmel_handle_status(struct uart_port *port, unsigned int pending,
1022 unsigned int status)
1023{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001024 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001025
Remy Bohmerb843aa22008-02-08 04:21:01 -08001026 if (pending & (ATMEL_US_RIIC | ATMEL_US_DSRIC | ATMEL_US_DCDIC
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001027 | ATMEL_US_CTSIC)) {
1028 atmel_port->irq_status = status;
1029 tasklet_schedule(&atmel_port->tasklet);
1030 }
Remy Bohmerb843aa22008-02-08 04:21:01 -08001031}
1032
1033/*
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001034 * Interrupt handler
1035 */
David Howells7d12e782006-10-05 14:55:46 +01001036static irqreturn_t atmel_interrupt(int irq, void *dev_id)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001037{
1038 struct uart_port *port = dev_id;
1039 unsigned int status, pending, pass_counter = 0;
1040
Chip Coldwella6670612008-02-08 04:21:06 -08001041 do {
1042 status = UART_GET_CSR(port);
1043 pending = status & UART_GET_IMR(port);
1044 if (!pending)
1045 break;
1046
Remy Bohmerb843aa22008-02-08 04:21:01 -08001047 atmel_handle_receive(port, pending);
1048 atmel_handle_status(port, pending, status);
1049 atmel_handle_transmit(port, pending);
Chip Coldwella6670612008-02-08 04:21:06 -08001050 } while (pass_counter++ < ATMEL_ISR_PASS_LIMIT);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001051
Haavard Skinnemoen0400b692008-02-23 15:23:36 -08001052 return pass_counter ? IRQ_HANDLED : IRQ_NONE;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001053}
1054
Elen Songa930e522013-07-22 16:30:25 +08001055static void atmel_release_tx_pdc(struct uart_port *port)
1056{
1057 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1058 struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1059
1060 dma_unmap_single(port->dev,
1061 pdc->dma_addr,
1062 pdc->dma_size,
1063 DMA_TO_DEVICE);
1064}
1065
Chip Coldwella6670612008-02-08 04:21:06 -08001066/*
1067 * Called from tasklet with ENDTX and TXBUFE interrupts disabled.
1068 */
Elen Song64e22eb2013-07-22 16:30:24 +08001069static void atmel_tx_pdc(struct uart_port *port)
Chip Coldwella6670612008-02-08 04:21:06 -08001070{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001071 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Alan Coxebd2c8f2009-09-19 13:13:28 -07001072 struct circ_buf *xmit = &port->state->xmit;
Chip Coldwella6670612008-02-08 04:21:06 -08001073 struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1074 int count;
1075
Michael Trimarchiba0657f2008-04-02 13:04:41 -07001076 /* nothing left to transmit? */
1077 if (UART_GET_TCR(port))
1078 return;
1079
Chip Coldwella6670612008-02-08 04:21:06 -08001080 xmit->tail += pdc->ofs;
1081 xmit->tail &= UART_XMIT_SIZE - 1;
1082
1083 port->icount.tx += pdc->ofs;
1084 pdc->ofs = 0;
1085
Michael Trimarchiba0657f2008-04-02 13:04:41 -07001086 /* more to transmit - setup next transfer */
Chip Coldwella6670612008-02-08 04:21:06 -08001087
Michael Trimarchiba0657f2008-04-02 13:04:41 -07001088 /* disable PDC transmit */
1089 UART_PUT_PTCR(port, ATMEL_PDC_TXTDIS);
1090
Itai Levi1f140812009-01-15 13:50:43 -08001091 if (!uart_circ_empty(xmit) && !uart_tx_stopped(port)) {
Chip Coldwella6670612008-02-08 04:21:06 -08001092 dma_sync_single_for_device(port->dev,
1093 pdc->dma_addr,
1094 pdc->dma_size,
1095 DMA_TO_DEVICE);
1096
1097 count = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
1098 pdc->ofs = count;
1099
1100 UART_PUT_TPR(port, pdc->dma_addr + xmit->tail);
1101 UART_PUT_TCR(port, count);
Claudio Scordinoe8faff72010-05-03 13:31:28 +01001102 /* re-enable PDC transmit */
Chip Coldwella6670612008-02-08 04:21:06 -08001103 UART_PUT_PTCR(port, ATMEL_PDC_TXTEN);
Claudio Scordinoe8faff72010-05-03 13:31:28 +01001104 /* Enable interrupts */
1105 UART_PUT_IER(port, atmel_port->tx_done_mask);
1106 } else {
Bernhard Roth83cac9f2011-08-24 09:48:23 +02001107 if ((atmel_port->rs485.flags & SER_RS485_ENABLED) &&
1108 !(atmel_port->rs485.flags & SER_RS485_RX_DURING_TX)) {
Claudio Scordinoe8faff72010-05-03 13:31:28 +01001109 /* DMA done, stop TX, start RX for RS485 */
1110 atmel_start_rx(port);
1111 }
Chip Coldwella6670612008-02-08 04:21:06 -08001112 }
1113
1114 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1115 uart_write_wakeup(port);
1116}
1117
Elen Songa930e522013-07-22 16:30:25 +08001118static int atmel_prepare_tx_pdc(struct uart_port *port)
1119{
1120 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1121 struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1122 struct circ_buf *xmit = &port->state->xmit;
1123
1124 pdc->buf = xmit->buf;
1125 pdc->dma_addr = dma_map_single(port->dev,
1126 pdc->buf,
1127 UART_XMIT_SIZE,
1128 DMA_TO_DEVICE);
1129 pdc->dma_size = UART_XMIT_SIZE;
1130 pdc->ofs = 0;
1131
1132 return 0;
1133}
1134
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001135static void atmel_rx_from_ring(struct uart_port *port)
1136{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001137 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001138 struct circ_buf *ring = &atmel_port->rx_ring;
1139 unsigned int flg;
1140 unsigned int status;
1141
1142 while (ring->head != ring->tail) {
1143 struct atmel_uart_char c;
1144
1145 /* Make sure c is loaded after head. */
1146 smp_rmb();
1147
1148 c = ((struct atmel_uart_char *)ring->buf)[ring->tail];
1149
1150 ring->tail = (ring->tail + 1) & (ATMEL_SERIAL_RINGSIZE - 1);
1151
1152 port->icount.rx++;
1153 status = c.status;
1154 flg = TTY_NORMAL;
1155
1156 /*
1157 * note that the error handling code is
1158 * out of the main execution path
1159 */
1160 if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME
1161 | ATMEL_US_OVRE | ATMEL_US_RXBRK))) {
1162 if (status & ATMEL_US_RXBRK) {
1163 /* ignore side-effect */
1164 status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME);
1165
1166 port->icount.brk++;
1167 if (uart_handle_break(port))
1168 continue;
1169 }
1170 if (status & ATMEL_US_PARE)
1171 port->icount.parity++;
1172 if (status & ATMEL_US_FRAME)
1173 port->icount.frame++;
1174 if (status & ATMEL_US_OVRE)
1175 port->icount.overrun++;
1176
1177 status &= port->read_status_mask;
1178
1179 if (status & ATMEL_US_RXBRK)
1180 flg = TTY_BREAK;
1181 else if (status & ATMEL_US_PARE)
1182 flg = TTY_PARITY;
1183 else if (status & ATMEL_US_FRAME)
1184 flg = TTY_FRAME;
1185 }
1186
1187
1188 if (uart_handle_sysrq_char(port, c.ch))
1189 continue;
1190
1191 uart_insert_char(port, status, ATMEL_US_OVRE, c.ch, flg);
1192 }
1193
1194 /*
1195 * Drop the lock here since it might end up calling
1196 * uart_start(), which takes the lock.
1197 */
1198 spin_unlock(&port->lock);
Jiri Slaby2e124b42013-01-03 15:53:06 +01001199 tty_flip_buffer_push(&port->state->port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001200 spin_lock(&port->lock);
1201}
1202
Elen Songa930e522013-07-22 16:30:25 +08001203static void atmel_release_rx_pdc(struct uart_port *port)
1204{
1205 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1206 int i;
1207
1208 for (i = 0; i < 2; i++) {
1209 struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i];
1210
1211 dma_unmap_single(port->dev,
1212 pdc->dma_addr,
1213 pdc->dma_size,
1214 DMA_FROM_DEVICE);
1215 kfree(pdc->buf);
1216 }
1217}
1218
Elen Song64e22eb2013-07-22 16:30:24 +08001219static void atmel_rx_from_pdc(struct uart_port *port)
Chip Coldwella6670612008-02-08 04:21:06 -08001220{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001221 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Jiri Slaby05c7cd32013-01-03 15:53:04 +01001222 struct tty_port *tport = &port->state->port;
Chip Coldwella6670612008-02-08 04:21:06 -08001223 struct atmel_dma_buffer *pdc;
1224 int rx_idx = atmel_port->pdc_rx_idx;
1225 unsigned int head;
1226 unsigned int tail;
1227 unsigned int count;
1228
1229 do {
1230 /* Reset the UART timeout early so that we don't miss one */
1231 UART_PUT_CR(port, ATMEL_US_STTTO);
1232
1233 pdc = &atmel_port->pdc_rx[rx_idx];
1234 head = UART_GET_RPR(port) - pdc->dma_addr;
1235 tail = pdc->ofs;
1236
1237 /* If the PDC has switched buffers, RPR won't contain
1238 * any address within the current buffer. Since head
1239 * is unsigned, we just need a one-way comparison to
1240 * find out.
1241 *
1242 * In this case, we just need to consume the entire
1243 * buffer and resubmit it for DMA. This will clear the
1244 * ENDRX bit as well, so that we can safely re-enable
1245 * all interrupts below.
1246 */
1247 head = min(head, pdc->dma_size);
1248
1249 if (likely(head != tail)) {
1250 dma_sync_single_for_cpu(port->dev, pdc->dma_addr,
1251 pdc->dma_size, DMA_FROM_DEVICE);
1252
1253 /*
1254 * head will only wrap around when we recycle
1255 * the DMA buffer, and when that happens, we
1256 * explicitly set tail to 0. So head will
1257 * always be greater than tail.
1258 */
1259 count = head - tail;
1260
Jiri Slaby05c7cd32013-01-03 15:53:04 +01001261 tty_insert_flip_string(tport, pdc->buf + pdc->ofs,
1262 count);
Chip Coldwella6670612008-02-08 04:21:06 -08001263
1264 dma_sync_single_for_device(port->dev, pdc->dma_addr,
1265 pdc->dma_size, DMA_FROM_DEVICE);
1266
1267 port->icount.rx += count;
1268 pdc->ofs = head;
1269 }
1270
1271 /*
1272 * If the current buffer is full, we need to check if
1273 * the next one contains any additional data.
1274 */
1275 if (head >= pdc->dma_size) {
1276 pdc->ofs = 0;
1277 UART_PUT_RNPR(port, pdc->dma_addr);
1278 UART_PUT_RNCR(port, pdc->dma_size);
1279
1280 rx_idx = !rx_idx;
1281 atmel_port->pdc_rx_idx = rx_idx;
1282 }
1283 } while (head >= pdc->dma_size);
1284
1285 /*
1286 * Drop the lock here since it might end up calling
1287 * uart_start(), which takes the lock.
1288 */
1289 spin_unlock(&port->lock);
Jiri Slaby2e124b42013-01-03 15:53:06 +01001290 tty_flip_buffer_push(tport);
Chip Coldwella6670612008-02-08 04:21:06 -08001291 spin_lock(&port->lock);
1292
1293 UART_PUT_IER(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT);
1294}
1295
Elen Songa930e522013-07-22 16:30:25 +08001296static int atmel_prepare_rx_pdc(struct uart_port *port)
1297{
1298 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1299 int i;
1300
1301 for (i = 0; i < 2; i++) {
1302 struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i];
1303
1304 pdc->buf = kmalloc(PDC_BUFFER_SIZE, GFP_KERNEL);
1305 if (pdc->buf == NULL) {
1306 if (i != 0) {
1307 dma_unmap_single(port->dev,
1308 atmel_port->pdc_rx[0].dma_addr,
1309 PDC_BUFFER_SIZE,
1310 DMA_FROM_DEVICE);
1311 kfree(atmel_port->pdc_rx[0].buf);
1312 }
1313 atmel_port->use_pdc_rx = 0;
1314 return -ENOMEM;
1315 }
1316 pdc->dma_addr = dma_map_single(port->dev,
1317 pdc->buf,
1318 PDC_BUFFER_SIZE,
1319 DMA_FROM_DEVICE);
1320 pdc->dma_size = PDC_BUFFER_SIZE;
1321 pdc->ofs = 0;
1322 }
1323
1324 atmel_port->pdc_rx_idx = 0;
1325
1326 UART_PUT_RPR(port, atmel_port->pdc_rx[0].dma_addr);
1327 UART_PUT_RCR(port, PDC_BUFFER_SIZE);
1328
1329 UART_PUT_RNPR(port, atmel_port->pdc_rx[1].dma_addr);
1330 UART_PUT_RNCR(port, PDC_BUFFER_SIZE);
1331
1332 return 0;
1333}
1334
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001335/*
1336 * tasklet handling tty stuff outside the interrupt handler.
1337 */
1338static void atmel_tasklet_func(unsigned long data)
1339{
1340 struct uart_port *port = (struct uart_port *)data;
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001341 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001342 unsigned int status;
1343 unsigned int status_change;
1344
1345 /* The interrupt handler does not take the lock */
1346 spin_lock(&port->lock);
1347
Elen Songa930e522013-07-22 16:30:25 +08001348 atmel_port->schedule_tx(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001349
1350 status = atmel_port->irq_status;
1351 status_change = status ^ atmel_port->irq_status_prev;
1352
1353 if (status_change & (ATMEL_US_RI | ATMEL_US_DSR
1354 | ATMEL_US_DCD | ATMEL_US_CTS)) {
1355 /* TODO: All reads to CSR will clear these interrupts! */
1356 if (status_change & ATMEL_US_RI)
1357 port->icount.rng++;
1358 if (status_change & ATMEL_US_DSR)
1359 port->icount.dsr++;
1360 if (status_change & ATMEL_US_DCD)
1361 uart_handle_dcd_change(port, !(status & ATMEL_US_DCD));
1362 if (status_change & ATMEL_US_CTS)
1363 uart_handle_cts_change(port, !(status & ATMEL_US_CTS));
1364
Alan Coxbdc04e32009-09-19 13:13:31 -07001365 wake_up_interruptible(&port->state->port.delta_msr_wait);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001366
1367 atmel_port->irq_status_prev = status;
1368 }
1369
Elen Songa930e522013-07-22 16:30:25 +08001370 atmel_port->schedule_rx(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001371
1372 spin_unlock(&port->lock);
1373}
1374
Elen Song33d64c42013-07-22 16:30:28 +08001375static int atmel_init_property(struct atmel_uart_port *atmel_port,
1376 struct platform_device *pdev)
1377{
1378 struct device_node *np = pdev->dev.of_node;
1379 struct atmel_uart_data *pdata = pdev->dev.platform_data;
1380
1381 if (np) {
1382 /* DMA/PDC usage specification */
1383 if (of_get_property(np, "atmel,use-dma-rx", NULL)) {
1384 if (of_get_property(np, "dmas", NULL)) {
1385 atmel_port->use_dma_rx = true;
1386 atmel_port->use_pdc_rx = false;
1387 } else {
1388 atmel_port->use_dma_rx = false;
1389 atmel_port->use_pdc_rx = true;
1390 }
1391 } else {
1392 atmel_port->use_dma_rx = false;
1393 atmel_port->use_pdc_rx = false;
1394 }
1395
1396 if (of_get_property(np, "atmel,use-dma-tx", NULL)) {
1397 if (of_get_property(np, "dmas", NULL)) {
1398 atmel_port->use_dma_tx = true;
1399 atmel_port->use_pdc_tx = false;
1400 } else {
1401 atmel_port->use_dma_tx = false;
1402 atmel_port->use_pdc_tx = true;
1403 }
1404 } else {
1405 atmel_port->use_dma_tx = false;
1406 atmel_port->use_pdc_tx = false;
1407 }
1408
1409 } else {
1410 atmel_port->use_pdc_rx = pdata->use_dma_rx;
1411 atmel_port->use_pdc_tx = pdata->use_dma_tx;
1412 atmel_port->use_dma_rx = false;
1413 atmel_port->use_dma_tx = false;
1414 }
1415
1416 return 0;
1417}
1418
1419static void atmel_init_rs485(struct atmel_uart_port *atmel_port,
1420 struct platform_device *pdev)
1421{
1422 struct device_node *np = pdev->dev.of_node;
1423 struct atmel_uart_data *pdata = pdev->dev.platform_data;
1424
1425 if (np) {
1426 u32 rs485_delay[2];
1427 /* rs485 properties */
1428 if (of_property_read_u32_array(np, "rs485-rts-delay",
1429 rs485_delay, 2) == 0) {
1430 struct serial_rs485 *rs485conf = &atmel_port->rs485;
1431
1432 rs485conf->delay_rts_before_send = rs485_delay[0];
1433 rs485conf->delay_rts_after_send = rs485_delay[1];
1434 rs485conf->flags = 0;
1435
1436 if (of_get_property(np, "rs485-rx-during-tx", NULL))
1437 rs485conf->flags |= SER_RS485_RX_DURING_TX;
1438
1439 if (of_get_property(np, "linux,rs485-enabled-at-boot-time",
1440 NULL))
1441 rs485conf->flags |= SER_RS485_ENABLED;
1442 }
1443 } else {
1444 atmel_port->rs485 = pdata->rs485;
1445 }
1446
1447}
1448
Elen Songa930e522013-07-22 16:30:25 +08001449static void atmel_set_ops(struct uart_port *port)
1450{
1451 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1452
Elen Song34df42f2013-07-22 16:30:27 +08001453 if (atmel_use_dma_rx(port)) {
1454 atmel_port->prepare_rx = &atmel_prepare_rx_dma;
1455 atmel_port->schedule_rx = &atmel_rx_from_dma;
1456 atmel_port->release_rx = &atmel_release_rx_dma;
1457 } else if (atmel_use_pdc_rx(port)) {
Elen Songa930e522013-07-22 16:30:25 +08001458 atmel_port->prepare_rx = &atmel_prepare_rx_pdc;
1459 atmel_port->schedule_rx = &atmel_rx_from_pdc;
1460 atmel_port->release_rx = &atmel_release_rx_pdc;
1461 } else {
1462 atmel_port->prepare_rx = NULL;
1463 atmel_port->schedule_rx = &atmel_rx_from_ring;
1464 atmel_port->release_rx = NULL;
1465 }
1466
Elen Song08f738b2013-07-22 16:30:26 +08001467 if (atmel_use_dma_tx(port)) {
1468 atmel_port->prepare_tx = &atmel_prepare_tx_dma;
1469 atmel_port->schedule_tx = &atmel_tx_dma;
1470 atmel_port->release_tx = &atmel_release_tx_dma;
1471 } else if (atmel_use_pdc_tx(port)) {
Elen Songa930e522013-07-22 16:30:25 +08001472 atmel_port->prepare_tx = &atmel_prepare_tx_pdc;
1473 atmel_port->schedule_tx = &atmel_tx_pdc;
1474 atmel_port->release_tx = &atmel_release_tx_pdc;
1475 } else {
1476 atmel_port->prepare_tx = NULL;
1477 atmel_port->schedule_tx = &atmel_tx_chars;
1478 atmel_port->release_tx = NULL;
1479 }
1480}
1481
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001482/*
Elen Song055560b2013-07-22 16:30:29 +08001483 * Get ip name usart or uart
1484 */
1485static int atmel_get_ip_name(struct uart_port *port)
1486{
1487 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1488 int name = UART_GET_IP_NAME(port);
1489 int usart, uart;
1490 /* usart and uart ascii */
1491 usart = 0x55534152;
1492 uart = 0x44424755;
1493
1494 atmel_port->is_usart = false;
1495
1496 if (name == usart) {
1497 dev_dbg(port->dev, "This is usart\n");
1498 atmel_port->is_usart = true;
1499 } else if (name == uart) {
1500 dev_dbg(port->dev, "This is uart\n");
1501 atmel_port->is_usart = false;
1502 } else {
1503 dev_err(port->dev, "Not supported ip name, set to uart\n");
1504 return -EINVAL;
1505 }
1506
1507 return 0;
1508}
1509
1510/*
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001511 * Perform initialization and enable port for reception
1512 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001513static int atmel_startup(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001514{
Elen Song33d64c42013-07-22 16:30:28 +08001515 struct platform_device *pdev = to_platform_device(port->dev);
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001516 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Alan Coxebd2c8f2009-09-19 13:13:28 -07001517 struct tty_struct *tty = port->state->port.tty;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001518 int retval;
1519
1520 /*
1521 * Ensure that no interrupts are enabled otherwise when
1522 * request_irq() is called we could get stuck trying to
1523 * handle an unexpected interrupt
1524 */
1525 UART_PUT_IDR(port, -1);
1526
1527 /*
1528 * Allocate the IRQ
1529 */
Remy Bohmerb843aa22008-02-08 04:21:01 -08001530 retval = request_irq(port->irq, atmel_interrupt, IRQF_SHARED,
Haavard Skinnemoenae161062008-02-08 04:21:08 -08001531 tty ? tty->name : "atmel_serial", port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001532 if (retval) {
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001533 printk("atmel_serial: atmel_startup - Can't get irq\n");
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001534 return retval;
1535 }
1536
1537 /*
Chip Coldwella6670612008-02-08 04:21:06 -08001538 * Initialize DMA (if necessary)
1539 */
Elen Song33d64c42013-07-22 16:30:28 +08001540 atmel_init_property(atmel_port, pdev);
1541
Elen Songa930e522013-07-22 16:30:25 +08001542 if (atmel_port->prepare_rx) {
1543 retval = atmel_port->prepare_rx(port);
1544 if (retval < 0)
1545 atmel_set_ops(port);
Chip Coldwella6670612008-02-08 04:21:06 -08001546 }
1547
Elen Songa930e522013-07-22 16:30:25 +08001548 if (atmel_port->prepare_tx) {
1549 retval = atmel_port->prepare_tx(port);
1550 if (retval < 0)
1551 atmel_set_ops(port);
1552 }
Chip Coldwella6670612008-02-08 04:21:06 -08001553 /*
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001554 * If there is a specific "open" function (to register
1555 * control line interrupts)
1556 */
Haavard Skinnemoen71f2e2b2006-10-04 16:02:07 +02001557 if (atmel_open_hook) {
1558 retval = atmel_open_hook(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001559 if (retval) {
1560 free_irq(port->irq, port);
1561 return retval;
1562 }
1563 }
1564
Atsushi Nemoto27c0c8e2009-02-18 14:48:28 -08001565 /* Save current CSR for comparison in atmel_tasklet_func() */
1566 atmel_port->irq_status_prev = UART_GET_CSR(port);
1567 atmel_port->irq_status = atmel_port->irq_status_prev;
1568
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001569 /*
1570 * Finally, enable the serial port
1571 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001572 UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
Remy Bohmerb843aa22008-02-08 04:21:01 -08001573 /* enable xmit & rcvr */
1574 UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN);
Andrew Victorafefc412006-06-19 19:53:19 +01001575
Elen Song64e22eb2013-07-22 16:30:24 +08001576 if (atmel_use_pdc_rx(port)) {
Chip Coldwella6670612008-02-08 04:21:06 -08001577 /* set UART timeout */
1578 UART_PUT_RTOR(port, PDC_RX_TIMEOUT);
1579 UART_PUT_CR(port, ATMEL_US_STTTO);
1580
1581 UART_PUT_IER(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT);
1582 /* enable PDC controller */
1583 UART_PUT_PTCR(port, ATMEL_PDC_RXTEN);
Elen Song34df42f2013-07-22 16:30:27 +08001584 } else if (atmel_use_dma_rx(port)) {
1585 UART_PUT_RTOR(port, PDC_RX_TIMEOUT);
1586 UART_PUT_CR(port, ATMEL_US_STTTO);
1587
1588 UART_PUT_IER(port, ATMEL_US_TIMEOUT);
Chip Coldwella6670612008-02-08 04:21:06 -08001589 } else {
1590 /* enable receive only */
1591 UART_PUT_IER(port, ATMEL_US_RXRDY);
1592 }
Andrew Victorafefc412006-06-19 19:53:19 +01001593
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001594 return 0;
1595}
1596
1597/*
1598 * Disable the port
1599 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001600static void atmel_shutdown(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001601{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001602 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Chip Coldwella6670612008-02-08 04:21:06 -08001603 /*
1604 * Ensure everything is stopped.
1605 */
1606 atmel_stop_rx(port);
1607 atmel_stop_tx(port);
1608
1609 /*
1610 * Shut-down the DMA.
1611 */
Elen Songa930e522013-07-22 16:30:25 +08001612 if (atmel_port->release_rx)
1613 atmel_port->release_rx(port);
1614 if (atmel_port->release_tx)
1615 atmel_port->release_tx(port);
Chip Coldwella6670612008-02-08 04:21:06 -08001616
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001617 /*
1618 * Disable all interrupts, port and break condition.
1619 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001620 UART_PUT_CR(port, ATMEL_US_RSTSTA);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001621 UART_PUT_IDR(port, -1);
1622
1623 /*
1624 * Free the interrupt
1625 */
1626 free_irq(port->irq, port);
1627
1628 /*
1629 * If there is a specific "close" function (to unregister
1630 * control line interrupts)
1631 */
Haavard Skinnemoen71f2e2b2006-10-04 16:02:07 +02001632 if (atmel_close_hook)
1633 atmel_close_hook(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001634}
1635
1636/*
Haavard Skinnemoen9afd5612008-07-16 21:52:46 +01001637 * Flush any TX data submitted for DMA. Called when the TX circular
1638 * buffer is reset.
1639 */
1640static void atmel_flush_buffer(struct uart_port *port)
1641{
1642 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1643
Elen Song64e22eb2013-07-22 16:30:24 +08001644 if (atmel_use_pdc_tx(port)) {
Haavard Skinnemoen9afd5612008-07-16 21:52:46 +01001645 UART_PUT_TCR(port, 0);
1646 atmel_port->pdc_tx.ofs = 0;
1647 }
1648}
1649
1650/*
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001651 * Power / Clock management.
1652 */
Remy Bohmerb843aa22008-02-08 04:21:01 -08001653static void atmel_serial_pm(struct uart_port *port, unsigned int state,
1654 unsigned int oldstate)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001655{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001656 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Andrew Victorafefc412006-06-19 19:53:19 +01001657
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001658 switch (state) {
Remy Bohmerb843aa22008-02-08 04:21:01 -08001659 case 0:
1660 /*
1661 * Enable the peripheral clock for this serial port.
1662 * This is called on uart_open() or a resume event.
1663 */
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02001664 clk_prepare_enable(atmel_port->clk);
Anti Sullinf05596d2008-09-22 13:57:54 -07001665
1666 /* re-enable interrupts if we disabled some on suspend */
1667 UART_PUT_IER(port, atmel_port->backup_imr);
Remy Bohmerb843aa22008-02-08 04:21:01 -08001668 break;
1669 case 3:
Anti Sullinf05596d2008-09-22 13:57:54 -07001670 /* Back up the interrupt mask and disable all interrupts */
1671 atmel_port->backup_imr = UART_GET_IMR(port);
1672 UART_PUT_IDR(port, -1);
1673
Remy Bohmerb843aa22008-02-08 04:21:01 -08001674 /*
1675 * Disable the peripheral clock for this serial port.
1676 * This is called on uart_close() or a suspend event.
1677 */
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02001678 clk_disable_unprepare(atmel_port->clk);
Remy Bohmerb843aa22008-02-08 04:21:01 -08001679 break;
1680 default:
1681 printk(KERN_ERR "atmel_serial: unknown pm %d\n", state);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001682 }
1683}
1684
1685/*
1686 * Change the port parameters
1687 */
Remy Bohmerb843aa22008-02-08 04:21:01 -08001688static void atmel_set_termios(struct uart_port *port, struct ktermios *termios,
1689 struct ktermios *old)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001690{
1691 unsigned long flags;
1692 unsigned int mode, imr, quot, baud;
Claudio Scordinoe8faff72010-05-03 13:31:28 +01001693 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001694
Andrew Victor03abeac2007-05-03 12:26:24 +01001695 /* Get current mode register */
Remy Bohmerb843aa22008-02-08 04:21:01 -08001696 mode = UART_GET_MR(port) & ~(ATMEL_US_USCLKS | ATMEL_US_CHRL
Peter Ma8e706c42009-03-31 10:31:02 -07001697 | ATMEL_US_NBSTOP | ATMEL_US_PAR
1698 | ATMEL_US_USMODE);
Andrew Victor03abeac2007-05-03 12:26:24 +01001699
Remy Bohmerb843aa22008-02-08 04:21:01 -08001700 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001701 quot = uart_get_divisor(port, baud);
1702
Remy Bohmerb843aa22008-02-08 04:21:01 -08001703 if (quot > 65535) { /* BRGR is 16-bit, so switch to slower clock */
Andrew Victor03abeac2007-05-03 12:26:24 +01001704 quot /= 8;
1705 mode |= ATMEL_US_USCLKS_MCK_DIV8;
1706 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001707
1708 /* byte size */
1709 switch (termios->c_cflag & CSIZE) {
1710 case CS5:
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001711 mode |= ATMEL_US_CHRL_5;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001712 break;
1713 case CS6:
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001714 mode |= ATMEL_US_CHRL_6;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001715 break;
1716 case CS7:
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001717 mode |= ATMEL_US_CHRL_7;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001718 break;
1719 default:
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001720 mode |= ATMEL_US_CHRL_8;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001721 break;
1722 }
1723
1724 /* stop bits */
1725 if (termios->c_cflag & CSTOPB)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001726 mode |= ATMEL_US_NBSTOP_2;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001727
1728 /* parity */
1729 if (termios->c_cflag & PARENB) {
Remy Bohmerb843aa22008-02-08 04:21:01 -08001730 /* Mark or Space parity */
1731 if (termios->c_cflag & CMSPAR) {
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001732 if (termios->c_cflag & PARODD)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001733 mode |= ATMEL_US_PAR_MARK;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001734 else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001735 mode |= ATMEL_US_PAR_SPACE;
Remy Bohmerb843aa22008-02-08 04:21:01 -08001736 } else if (termios->c_cflag & PARODD)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001737 mode |= ATMEL_US_PAR_ODD;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001738 else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001739 mode |= ATMEL_US_PAR_EVEN;
Remy Bohmerb843aa22008-02-08 04:21:01 -08001740 } else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001741 mode |= ATMEL_US_PAR_NONE;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001742
Peter Ma8e706c42009-03-31 10:31:02 -07001743 /* hardware handshake (RTS/CTS) */
1744 if (termios->c_cflag & CRTSCTS)
1745 mode |= ATMEL_US_USMODE_HWHS;
1746 else
1747 mode |= ATMEL_US_USMODE_NORMAL;
1748
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001749 spin_lock_irqsave(&port->lock, flags);
1750
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001751 port->read_status_mask = ATMEL_US_OVRE;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001752 if (termios->c_iflag & INPCK)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001753 port->read_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001754 if (termios->c_iflag & (BRKINT | PARMRK))
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001755 port->read_status_mask |= ATMEL_US_RXBRK;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001756
Elen Song64e22eb2013-07-22 16:30:24 +08001757 if (atmel_use_pdc_rx(port))
Chip Coldwella6670612008-02-08 04:21:06 -08001758 /* need to enable error interrupts */
1759 UART_PUT_IER(port, port->read_status_mask);
1760
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001761 /*
1762 * Characters to ignore
1763 */
1764 port->ignore_status_mask = 0;
1765 if (termios->c_iflag & IGNPAR)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001766 port->ignore_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001767 if (termios->c_iflag & IGNBRK) {
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001768 port->ignore_status_mask |= ATMEL_US_RXBRK;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001769 /*
1770 * If we're ignoring parity and break indicators,
1771 * ignore overruns too (for real raw support).
1772 */
1773 if (termios->c_iflag & IGNPAR)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001774 port->ignore_status_mask |= ATMEL_US_OVRE;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001775 }
Remy Bohmerb843aa22008-02-08 04:21:01 -08001776 /* TODO: Ignore all characters if CREAD is set.*/
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001777
1778 /* update the per-port timeout */
1779 uart_update_timeout(port, termios->c_cflag, baud);
1780
Haavard Skinnemoen0ccad872009-06-16 17:02:03 +01001781 /*
1782 * save/disable interrupts. The tty layer will ensure that the
1783 * transmitter is empty if requested by the caller, so there's
1784 * no need to wait for it here.
1785 */
Remy Bohmerb843aa22008-02-08 04:21:01 -08001786 imr = UART_GET_IMR(port);
1787 UART_PUT_IDR(port, -1);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001788
1789 /* disable receiver and transmitter */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001790 UART_PUT_CR(port, ATMEL_US_TXDIS | ATMEL_US_RXDIS);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001791
Claudio Scordinoe8faff72010-05-03 13:31:28 +01001792 /* Resetting serial mode to RS232 (0x0) */
1793 mode &= ~ATMEL_US_USMODE;
1794
1795 if (atmel_port->rs485.flags & SER_RS485_ENABLED) {
1796 dev_dbg(port->dev, "Setting UART to RS485\n");
Claudio Scordino93f33502011-11-09 15:51:49 +01001797 if ((atmel_port->rs485.delay_rts_after_send) > 0)
Claudio Scordino1b633182010-07-20 15:26:47 -07001798 UART_PUT_TTGR(port,
1799 atmel_port->rs485.delay_rts_after_send);
Claudio Scordinoe8faff72010-05-03 13:31:28 +01001800 mode |= ATMEL_US_USMODE_RS485;
1801 } else {
1802 dev_dbg(port->dev, "Setting UART to RS232\n");
1803 }
1804
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001805 /* set the parity, stop bits and data size */
1806 UART_PUT_MR(port, mode);
1807
1808 /* set the baud rate */
1809 UART_PUT_BRGR(port, quot);
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001810 UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
1811 UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001812
1813 /* restore interrupts */
1814 UART_PUT_IER(port, imr);
1815
1816 /* CTS flow-control and modem-status interrupts */
1817 if (UART_ENABLE_MS(port, termios->c_cflag))
1818 port->ops->enable_ms(port);
1819
1820 spin_unlock_irqrestore(&port->lock, flags);
1821}
1822
Viktar Palstsiuk42bd7a42011-02-09 15:26:13 +01001823static void atmel_set_ldisc(struct uart_port *port, int new)
1824{
Jiri Slabyb54bf3b2011-11-09 21:33:44 +01001825 if (new == N_PPS) {
Viktar Palstsiuk42bd7a42011-02-09 15:26:13 +01001826 port->flags |= UPF_HARDPPS_CD;
1827 atmel_enable_ms(port);
1828 } else {
1829 port->flags &= ~UPF_HARDPPS_CD;
1830 }
1831}
1832
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001833/*
1834 * Return string describing the specified port
1835 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001836static const char *atmel_type(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001837{
Haavard Skinnemoen9ab4f882006-10-04 16:02:06 +02001838 return (port->type == PORT_ATMEL) ? "ATMEL_SERIAL" : NULL;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001839}
1840
1841/*
1842 * Release the memory region(s) being used by 'port'.
1843 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001844static void atmel_release_port(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001845{
Andrew Victorafefc412006-06-19 19:53:19 +01001846 struct platform_device *pdev = to_platform_device(port->dev);
1847 int size = pdev->resource[0].end - pdev->resource[0].start + 1;
1848
1849 release_mem_region(port->mapbase, size);
1850
1851 if (port->flags & UPF_IOREMAP) {
1852 iounmap(port->membase);
1853 port->membase = NULL;
1854 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001855}
1856
1857/*
1858 * Request the memory region(s) being used by 'port'.
1859 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001860static int atmel_request_port(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001861{
Andrew Victorafefc412006-06-19 19:53:19 +01001862 struct platform_device *pdev = to_platform_device(port->dev);
1863 int size = pdev->resource[0].end - pdev->resource[0].start + 1;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001864
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001865 if (!request_mem_region(port->mapbase, size, "atmel_serial"))
Andrew Victorafefc412006-06-19 19:53:19 +01001866 return -EBUSY;
1867
1868 if (port->flags & UPF_IOREMAP) {
1869 port->membase = ioremap(port->mapbase, size);
1870 if (port->membase == NULL) {
1871 release_mem_region(port->mapbase, size);
1872 return -ENOMEM;
1873 }
1874 }
1875
1876 return 0;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001877}
1878
1879/*
1880 * Configure/autoconfigure the port.
1881 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001882static void atmel_config_port(struct uart_port *port, int flags)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001883{
1884 if (flags & UART_CONFIG_TYPE) {
Haavard Skinnemoen9ab4f882006-10-04 16:02:06 +02001885 port->type = PORT_ATMEL;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001886 atmel_request_port(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001887 }
1888}
1889
1890/*
1891 * Verify the new serial_struct (for TIOCSSERIAL).
1892 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001893static int atmel_verify_port(struct uart_port *port, struct serial_struct *ser)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001894{
1895 int ret = 0;
Haavard Skinnemoen9ab4f882006-10-04 16:02:06 +02001896 if (ser->type != PORT_UNKNOWN && ser->type != PORT_ATMEL)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001897 ret = -EINVAL;
1898 if (port->irq != ser->irq)
1899 ret = -EINVAL;
1900 if (ser->io_type != SERIAL_IO_MEM)
1901 ret = -EINVAL;
1902 if (port->uartclk / 16 != ser->baud_base)
1903 ret = -EINVAL;
1904 if ((void *)port->mapbase != ser->iomem_base)
1905 ret = -EINVAL;
1906 if (port->iobase != ser->port)
1907 ret = -EINVAL;
1908 if (ser->hub6 != 0)
1909 ret = -EINVAL;
1910 return ret;
1911}
1912
Albin Tonnerre8fe2d542009-12-09 12:31:32 -08001913#ifdef CONFIG_CONSOLE_POLL
1914static int atmel_poll_get_char(struct uart_port *port)
1915{
1916 while (!(UART_GET_CSR(port) & ATMEL_US_RXRDY))
1917 cpu_relax();
1918
1919 return UART_GET_CHAR(port);
1920}
1921
1922static void atmel_poll_put_char(struct uart_port *port, unsigned char ch)
1923{
1924 while (!(UART_GET_CSR(port) & ATMEL_US_TXRDY))
1925 cpu_relax();
1926
1927 UART_PUT_CHAR(port, ch);
1928}
1929#endif
1930
Claudio Scordinoe8faff72010-05-03 13:31:28 +01001931static int
1932atmel_ioctl(struct uart_port *port, unsigned int cmd, unsigned long arg)
1933{
1934 struct serial_rs485 rs485conf;
1935
1936 switch (cmd) {
1937 case TIOCSRS485:
1938 if (copy_from_user(&rs485conf, (struct serial_rs485 *) arg,
1939 sizeof(rs485conf)))
1940 return -EFAULT;
1941
1942 atmel_config_rs485(port, &rs485conf);
1943 break;
1944
1945 case TIOCGRS485:
1946 if (copy_to_user((struct serial_rs485 *) arg,
1947 &(to_atmel_uart_port(port)->rs485),
1948 sizeof(rs485conf)))
1949 return -EFAULT;
1950 break;
1951
1952 default:
1953 return -ENOIOCTLCMD;
1954 }
1955 return 0;
1956}
1957
1958
1959
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001960static struct uart_ops atmel_pops = {
1961 .tx_empty = atmel_tx_empty,
1962 .set_mctrl = atmel_set_mctrl,
1963 .get_mctrl = atmel_get_mctrl,
1964 .stop_tx = atmel_stop_tx,
1965 .start_tx = atmel_start_tx,
1966 .stop_rx = atmel_stop_rx,
1967 .enable_ms = atmel_enable_ms,
1968 .break_ctl = atmel_break_ctl,
1969 .startup = atmel_startup,
1970 .shutdown = atmel_shutdown,
Haavard Skinnemoen9afd5612008-07-16 21:52:46 +01001971 .flush_buffer = atmel_flush_buffer,
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001972 .set_termios = atmel_set_termios,
Viktar Palstsiuk42bd7a42011-02-09 15:26:13 +01001973 .set_ldisc = atmel_set_ldisc,
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001974 .type = atmel_type,
1975 .release_port = atmel_release_port,
1976 .request_port = atmel_request_port,
1977 .config_port = atmel_config_port,
1978 .verify_port = atmel_verify_port,
1979 .pm = atmel_serial_pm,
Claudio Scordinoe8faff72010-05-03 13:31:28 +01001980 .ioctl = atmel_ioctl,
Albin Tonnerre8fe2d542009-12-09 12:31:32 -08001981#ifdef CONFIG_CONSOLE_POLL
1982 .poll_get_char = atmel_poll_get_char,
1983 .poll_put_char = atmel_poll_put_char,
1984#endif
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001985};
1986
Andrew Victorafefc412006-06-19 19:53:19 +01001987/*
1988 * Configure the port from the platform device resource info.
1989 */
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02001990static int atmel_init_port(struct atmel_uart_port *atmel_port,
Remy Bohmerb843aa22008-02-08 04:21:01 -08001991 struct platform_device *pdev)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001992{
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02001993 int ret;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001994 struct uart_port *port = &atmel_port->uart;
Nicolas Ferre1acfc7e2011-10-12 18:06:57 +02001995 struct atmel_uart_data *pdata = pdev->dev.platform_data;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001996
Elen Song33d64c42013-07-22 16:30:28 +08001997 if (!atmel_init_property(atmel_port, pdev))
1998 atmel_set_ops(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001999
Elen Song33d64c42013-07-22 16:30:28 +08002000 atmel_init_rs485(atmel_port, pdev);
Elen Songa930e522013-07-22 16:30:25 +08002001
Claudio Scordinoe8faff72010-05-03 13:31:28 +01002002 port->iotype = UPIO_MEM;
2003 port->flags = UPF_BOOT_AUTOCONF;
2004 port->ops = &atmel_pops;
2005 port->fifosize = 1;
Claudio Scordinoe8faff72010-05-03 13:31:28 +01002006 port->dev = &pdev->dev;
Andrew Victorafefc412006-06-19 19:53:19 +01002007 port->mapbase = pdev->resource[0].start;
2008 port->irq = pdev->resource[1].start;
2009
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08002010 tasklet_init(&atmel_port->tasklet, atmel_tasklet_func,
2011 (unsigned long)port);
2012
2013 memset(&atmel_port->rx_ring, 0, sizeof(atmel_port->rx_ring));
2014
Nicolas Ferre5fbe46b2011-10-12 18:07:00 +02002015 if (pdata && pdata->regs) {
Haavard Skinnemoen75d35212006-10-04 16:02:08 +02002016 /* Already mapped by setup code */
Nicolas Ferre1acfc7e2011-10-12 18:06:57 +02002017 port->membase = pdata->regs;
Nicolas Ferre588edbf2011-10-12 18:06:58 +02002018 } else {
Andrew Victorafefc412006-06-19 19:53:19 +01002019 port->flags |= UPF_IOREMAP;
2020 port->membase = NULL;
2021 }
2022
Remy Bohmerb843aa22008-02-08 04:21:01 -08002023 /* for console, the clock could already be configured */
2024 if (!atmel_port->clk) {
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002025 atmel_port->clk = clk_get(&pdev->dev, "usart");
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002026 if (IS_ERR(atmel_port->clk)) {
2027 ret = PTR_ERR(atmel_port->clk);
2028 atmel_port->clk = NULL;
2029 return ret;
2030 }
2031 ret = clk_prepare_enable(atmel_port->clk);
2032 if (ret) {
2033 clk_put(atmel_port->clk);
2034 atmel_port->clk = NULL;
2035 return ret;
2036 }
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002037 port->uartclk = clk_get_rate(atmel_port->clk);
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002038 clk_disable_unprepare(atmel_port->clk);
David Brownell06a7f052008-11-06 12:53:40 -08002039 /* only enable clock when USART is in use */
Andrew Victorafefc412006-06-19 19:53:19 +01002040 }
Chip Coldwella6670612008-02-08 04:21:06 -08002041
Claudio Scordinoe8faff72010-05-03 13:31:28 +01002042 /* Use TXEMPTY for interrupt when rs485 else TXRDY or ENDTX|TXBUFE */
2043 if (atmel_port->rs485.flags & SER_RS485_ENABLED)
2044 atmel_port->tx_done_mask = ATMEL_US_TXEMPTY;
Elen Song64e22eb2013-07-22 16:30:24 +08002045 else if (atmel_use_pdc_tx(port)) {
Chip Coldwella6670612008-02-08 04:21:06 -08002046 port->fifosize = PDC_BUFFER_SIZE;
Claudio Scordinoe8faff72010-05-03 13:31:28 +01002047 atmel_port->tx_done_mask = ATMEL_US_ENDTX | ATMEL_US_TXBUFE;
2048 } else {
2049 atmel_port->tx_done_mask = ATMEL_US_TXRDY;
2050 }
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002051
2052 return 0;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002053}
2054
Jean-Christophe PLAGNIOL-VILLARD69f6a272012-02-16 00:24:07 +08002055struct platform_device *atmel_default_console_device; /* the serial console device */
2056
Haavard Skinnemoen749c4e62006-10-04 16:02:02 +02002057#ifdef CONFIG_SERIAL_ATMEL_CONSOLE
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002058static void atmel_console_putchar(struct uart_port *port, int ch)
Russell Kingd3587882006-03-20 20:00:09 +00002059{
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002060 while (!(UART_GET_CSR(port) & ATMEL_US_TXRDY))
Haavard Skinnemoen829dd812008-02-08 04:21:02 -08002061 cpu_relax();
Russell Kingd3587882006-03-20 20:00:09 +00002062 UART_PUT_CHAR(port, ch);
2063}
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002064
2065/*
2066 * Interrupts are disabled on entering
2067 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002068static void atmel_console_write(struct console *co, const char *s, u_int count)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002069{
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002070 struct uart_port *port = &atmel_ports[co->index].uart;
Claudio Scordinoe8faff72010-05-03 13:31:28 +01002071 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Russell Kingd3587882006-03-20 20:00:09 +00002072 unsigned int status, imr;
Marc Pignat39d4c922008-04-02 13:04:42 -07002073 unsigned int pdc_tx;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002074
2075 /*
Remy Bohmerb843aa22008-02-08 04:21:01 -08002076 * First, save IMR and then disable interrupts
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002077 */
Remy Bohmerb843aa22008-02-08 04:21:01 -08002078 imr = UART_GET_IMR(port);
Claudio Scordinoe8faff72010-05-03 13:31:28 +01002079 UART_PUT_IDR(port, ATMEL_US_RXRDY | atmel_port->tx_done_mask);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002080
Marc Pignat39d4c922008-04-02 13:04:42 -07002081 /* Store PDC transmit status and disable it */
2082 pdc_tx = UART_GET_PTSR(port) & ATMEL_PDC_TXTEN;
2083 UART_PUT_PTCR(port, ATMEL_PDC_TXTDIS);
2084
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002085 uart_console_write(port, s, count, atmel_console_putchar);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002086
2087 /*
Remy Bohmerb843aa22008-02-08 04:21:01 -08002088 * Finally, wait for transmitter to become empty
2089 * and restore IMR
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002090 */
2091 do {
2092 status = UART_GET_CSR(port);
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002093 } while (!(status & ATMEL_US_TXRDY));
Marc Pignat39d4c922008-04-02 13:04:42 -07002094
2095 /* Restore PDC transmit status */
2096 if (pdc_tx)
2097 UART_PUT_PTCR(port, ATMEL_PDC_TXTEN);
2098
Remy Bohmerb843aa22008-02-08 04:21:01 -08002099 /* set interrupts back the way they were */
2100 UART_PUT_IER(port, imr);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002101}
2102
2103/*
Remy Bohmerb843aa22008-02-08 04:21:01 -08002104 * If the port was already initialised (eg, by a boot loader),
2105 * try to determine the current setup.
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002106 */
Remy Bohmerb843aa22008-02-08 04:21:01 -08002107static void __init atmel_console_get_options(struct uart_port *port, int *baud,
2108 int *parity, int *bits)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002109{
2110 unsigned int mr, quot;
2111
Haavard Skinnemoen1c0fd822008-02-08 04:21:03 -08002112 /*
2113 * If the baud rate generator isn't running, the port wasn't
2114 * initialized by the boot loader.
2115 */
Haavard Skinnemoen9c81c5c2008-06-05 22:46:39 -07002116 quot = UART_GET_BRGR(port) & ATMEL_US_CD;
Haavard Skinnemoen1c0fd822008-02-08 04:21:03 -08002117 if (!quot)
2118 return;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002119
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002120 mr = UART_GET_MR(port) & ATMEL_US_CHRL;
2121 if (mr == ATMEL_US_CHRL_8)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002122 *bits = 8;
2123 else
2124 *bits = 7;
2125
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002126 mr = UART_GET_MR(port) & ATMEL_US_PAR;
2127 if (mr == ATMEL_US_PAR_EVEN)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002128 *parity = 'e';
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002129 else if (mr == ATMEL_US_PAR_ODD)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002130 *parity = 'o';
2131
Haavard Skinnemoen4d5e3922006-10-04 16:02:11 +02002132 /*
2133 * The serial core only rounds down when matching this to a
2134 * supported baud rate. Make sure we don't end up slightly
2135 * lower than one of those, as it would make us fall through
2136 * to a much lower baud rate than we really want.
2137 */
Haavard Skinnemoen4d5e3922006-10-04 16:02:11 +02002138 *baud = port->uartclk / (16 * (quot - 1));
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002139}
2140
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002141static int __init atmel_console_setup(struct console *co, char *options)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002142{
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002143 int ret;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002144 struct uart_port *port = &atmel_ports[co->index].uart;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002145 int baud = 115200;
2146 int bits = 8;
2147 int parity = 'n';
2148 int flow = 'n';
2149
Remy Bohmerb843aa22008-02-08 04:21:01 -08002150 if (port->membase == NULL) {
2151 /* Port not initialized yet - delay setup */
Andrew Victorafefc412006-06-19 19:53:19 +01002152 return -ENODEV;
Remy Bohmerb843aa22008-02-08 04:21:01 -08002153 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002154
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002155 ret = clk_prepare_enable(atmel_ports[co->index].clk);
2156 if (ret)
2157 return ret;
David Brownell06a7f052008-11-06 12:53:40 -08002158
Remy Bohmerb843aa22008-02-08 04:21:01 -08002159 UART_PUT_IDR(port, -1);
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002160 UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
2161 UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002162
2163 if (options)
2164 uart_parse_options(options, &baud, &parity, &bits, &flow);
2165 else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002166 atmel_console_get_options(port, &baud, &parity, &bits);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002167
2168 return uart_set_options(port, co, baud, parity, bits, flow);
2169}
2170
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002171static struct uart_driver atmel_uart;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002172
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002173static struct console atmel_console = {
2174 .name = ATMEL_DEVICENAME,
2175 .write = atmel_console_write,
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002176 .device = uart_console_device,
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002177 .setup = atmel_console_setup,
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002178 .flags = CON_PRINTBUFFER,
2179 .index = -1,
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002180 .data = &atmel_uart,
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002181};
2182
David Brownell06a7f052008-11-06 12:53:40 -08002183#define ATMEL_CONSOLE_DEVICE (&atmel_console)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002184
Andrew Victorafefc412006-06-19 19:53:19 +01002185/*
2186 * Early console initialization (before VM subsystem initialized).
2187 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002188static int __init atmel_console_init(void)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002189{
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002190 int ret;
Haavard Skinnemoen73e27982006-10-04 16:02:04 +02002191 if (atmel_default_console_device) {
Voss, Nikolaus0d0a3cc2011-08-10 14:02:29 +02002192 struct atmel_uart_data *pdata =
2193 atmel_default_console_device->dev.platform_data;
Linus Torvaldsefb8d212011-10-26 15:11:09 +02002194 int id = pdata->num;
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002195 struct atmel_uart_port *port = &atmel_ports[id];
Voss, Nikolaus0d0a3cc2011-08-10 14:02:29 +02002196
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002197 port->backup_imr = 0;
2198 port->uart.line = id;
2199
2200 add_preferred_console(ATMEL_DEVICENAME, id, NULL);
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002201 ret = atmel_init_port(port, atmel_default_console_device);
2202 if (ret)
2203 return ret;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002204 register_console(&atmel_console);
Andrew Victorafefc412006-06-19 19:53:19 +01002205 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002206
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002207 return 0;
2208}
Remy Bohmerb843aa22008-02-08 04:21:01 -08002209
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002210console_initcall(atmel_console_init);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002211
Andrew Victorafefc412006-06-19 19:53:19 +01002212/*
2213 * Late console initialization.
2214 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002215static int __init atmel_late_console_init(void)
Andrew Victorafefc412006-06-19 19:53:19 +01002216{
Remy Bohmerb843aa22008-02-08 04:21:01 -08002217 if (atmel_default_console_device
2218 && !(atmel_console.flags & CON_ENABLED))
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002219 register_console(&atmel_console);
Andrew Victorafefc412006-06-19 19:53:19 +01002220
2221 return 0;
2222}
Remy Bohmerb843aa22008-02-08 04:21:01 -08002223
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002224core_initcall(atmel_late_console_init);
Andrew Victorafefc412006-06-19 19:53:19 +01002225
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002226static inline bool atmel_is_console_port(struct uart_port *port)
2227{
2228 return port->cons && port->cons->index == port->line;
2229}
2230
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002231#else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002232#define ATMEL_CONSOLE_DEVICE NULL
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002233
2234static inline bool atmel_is_console_port(struct uart_port *port)
2235{
2236 return false;
2237}
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002238#endif
2239
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002240static struct uart_driver atmel_uart = {
Remy Bohmerb843aa22008-02-08 04:21:01 -08002241 .owner = THIS_MODULE,
2242 .driver_name = "atmel_serial",
2243 .dev_name = ATMEL_DEVICENAME,
2244 .major = SERIAL_ATMEL_MAJOR,
2245 .minor = MINOR_START,
2246 .nr = ATMEL_MAX_UART,
2247 .cons = ATMEL_CONSOLE_DEVICE,
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002248};
2249
Andrew Victorafefc412006-06-19 19:53:19 +01002250#ifdef CONFIG_PM
Haavard Skinnemoenf826caa2008-02-24 14:34:45 +01002251static bool atmel_serial_clk_will_stop(void)
2252{
2253#ifdef CONFIG_ARCH_AT91
2254 return at91_suspend_entering_slow_clock();
2255#else
2256 return false;
2257#endif
2258}
2259
Remy Bohmerb843aa22008-02-08 04:21:01 -08002260static int atmel_serial_suspend(struct platform_device *pdev,
2261 pm_message_t state)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002262{
Andrew Victorafefc412006-06-19 19:53:19 +01002263 struct uart_port *port = platform_get_drvdata(pdev);
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08002264 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002265
Haavard Skinnemoene1c609e2008-03-14 14:54:13 +01002266 if (atmel_is_console_port(port) && console_suspend_enabled) {
2267 /* Drain the TX shifter */
2268 while (!(UART_GET_CSR(port) & ATMEL_US_TXEMPTY))
2269 cpu_relax();
2270 }
2271
Anti Sullinf05596d2008-09-22 13:57:54 -07002272 /* we can not wake up if we're running on slow clock */
2273 atmel_port->may_wakeup = device_may_wakeup(&pdev->dev);
2274 if (atmel_serial_clk_will_stop())
2275 device_set_wakeup_enable(&pdev->dev, 0);
2276
2277 uart_suspend_port(&atmel_uart, port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002278
2279 return 0;
2280}
2281
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002282static int atmel_serial_resume(struct platform_device *pdev)
Andrew Victorafefc412006-06-19 19:53:19 +01002283{
2284 struct uart_port *port = platform_get_drvdata(pdev);
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08002285 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Andrew Victorafefc412006-06-19 19:53:19 +01002286
Anti Sullinf05596d2008-09-22 13:57:54 -07002287 uart_resume_port(&atmel_uart, port);
2288 device_set_wakeup_enable(&pdev->dev, atmel_port->may_wakeup);
Andrew Victorafefc412006-06-19 19:53:19 +01002289
2290 return 0;
2291}
2292#else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002293#define atmel_serial_suspend NULL
2294#define atmel_serial_resume NULL
Andrew Victorafefc412006-06-19 19:53:19 +01002295#endif
2296
Bill Pemberton9671f092012-11-19 13:21:50 -05002297static int atmel_serial_probe(struct platform_device *pdev)
Andrew Victorafefc412006-06-19 19:53:19 +01002298{
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002299 struct atmel_uart_port *port;
Nicolas Ferre5fbe46b2011-10-12 18:07:00 +02002300 struct device_node *np = pdev->dev.of_node;
Jean-Christophe PLAGNIOL-VILLARDdeba1a02011-06-21 12:17:00 +08002301 struct atmel_uart_data *pdata = pdev->dev.platform_data;
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08002302 void *data;
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002303 int ret = -ENODEV;
Andrew Victorafefc412006-06-19 19:53:19 +01002304
Haavard Skinnemoen9d09daf2009-10-26 16:50:02 -07002305 BUILD_BUG_ON(ATMEL_SERIAL_RINGSIZE & (ATMEL_SERIAL_RINGSIZE - 1));
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08002306
Nicolas Ferre5fbe46b2011-10-12 18:07:00 +02002307 if (np)
2308 ret = of_alias_get_id(np, "serial");
2309 else
2310 if (pdata)
2311 ret = pdata->num;
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002312
2313 if (ret < 0)
Nicolas Ferre5fbe46b2011-10-12 18:07:00 +02002314 /* port id not found in platform data nor device-tree aliases:
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002315 * auto-enumerate it */
Pawel Wieczorkiewicz503bded2013-02-20 17:26:20 +01002316 ret = find_first_zero_bit(atmel_ports_in_use, ATMEL_MAX_UART);
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002317
Pawel Wieczorkiewicz503bded2013-02-20 17:26:20 +01002318 if (ret >= ATMEL_MAX_UART) {
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002319 ret = -ENODEV;
2320 goto err;
2321 }
2322
Pawel Wieczorkiewicz503bded2013-02-20 17:26:20 +01002323 if (test_and_set_bit(ret, atmel_ports_in_use)) {
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002324 /* port already in use */
2325 ret = -EBUSY;
2326 goto err;
2327 }
2328
2329 port = &atmel_ports[ret];
Anti Sullinf05596d2008-09-22 13:57:54 -07002330 port->backup_imr = 0;
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002331 port->uart.line = ret;
Anti Sullinf05596d2008-09-22 13:57:54 -07002332
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002333 ret = atmel_init_port(port, pdev);
2334 if (ret)
2335 goto err;
Andrew Victorafefc412006-06-19 19:53:19 +01002336
Elen Song64e22eb2013-07-22 16:30:24 +08002337 if (!atmel_use_pdc_rx(&port->uart)) {
Chip Coldwella6670612008-02-08 04:21:06 -08002338 ret = -ENOMEM;
Haavard Skinnemoen64334712008-02-08 04:21:07 -08002339 data = kmalloc(sizeof(struct atmel_uart_char)
2340 * ATMEL_SERIAL_RINGSIZE, GFP_KERNEL);
Chip Coldwella6670612008-02-08 04:21:06 -08002341 if (!data)
2342 goto err_alloc_ring;
2343 port->rx_ring.buf = data;
2344 }
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08002345
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002346 ret = uart_add_one_port(&atmel_uart, &port->uart);
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002347 if (ret)
2348 goto err_add_port;
2349
Albin Tonnerre8da14b52009-07-29 15:04:18 -07002350#ifdef CONFIG_SERIAL_ATMEL_CONSOLE
David Brownell06a7f052008-11-06 12:53:40 -08002351 if (atmel_is_console_port(&port->uart)
2352 && ATMEL_CONSOLE_DEVICE->flags & CON_ENABLED) {
2353 /*
2354 * The serial core enabled the clock for us, so undo
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002355 * the clk_prepare_enable() in atmel_console_setup()
David Brownell06a7f052008-11-06 12:53:40 -08002356 */
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002357 clk_disable_unprepare(port->clk);
David Brownell06a7f052008-11-06 12:53:40 -08002358 }
Albin Tonnerre8da14b52009-07-29 15:04:18 -07002359#endif
David Brownell06a7f052008-11-06 12:53:40 -08002360
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002361 device_init_wakeup(&pdev->dev, 1);
2362 platform_set_drvdata(pdev, port);
2363
Claudio Scordino5dfbd1d72011-01-13 15:45:39 -08002364 if (port->rs485.flags & SER_RS485_ENABLED) {
2365 UART_PUT_MR(&port->uart, ATMEL_US_USMODE_NORMAL);
2366 UART_PUT_CR(&port->uart, ATMEL_US_RTSEN);
2367 }
2368
Elen Song055560b2013-07-22 16:30:29 +08002369 /*
2370 * Get port name of usart or uart
2371 */
2372 ret = atmel_get_ip_name(&port->uart);
2373 if (ret < 0)
2374 goto err_add_port;
2375
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002376 return 0;
2377
2378err_add_port:
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08002379 kfree(port->rx_ring.buf);
2380 port->rx_ring.buf = NULL;
2381err_alloc_ring:
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002382 if (!atmel_is_console_port(&port->uart)) {
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002383 clk_put(port->clk);
2384 port->clk = NULL;
Andrew Victorafefc412006-06-19 19:53:19 +01002385 }
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002386err:
Andrew Victorafefc412006-06-19 19:53:19 +01002387 return ret;
2388}
2389
Bill Pembertonae8d8a12012-11-19 13:26:18 -05002390static int atmel_serial_remove(struct platform_device *pdev)
Andrew Victorafefc412006-06-19 19:53:19 +01002391{
2392 struct uart_port *port = platform_get_drvdata(pdev);
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08002393 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Andrew Victorafefc412006-06-19 19:53:19 +01002394 int ret = 0;
2395
Andrew Victorafefc412006-06-19 19:53:19 +01002396 device_init_wakeup(&pdev->dev, 0);
Andrew Victorafefc412006-06-19 19:53:19 +01002397
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002398 ret = uart_remove_one_port(&atmel_uart, port);
2399
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08002400 tasklet_kill(&atmel_port->tasklet);
2401 kfree(atmel_port->rx_ring.buf);
2402
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002403 /* "port" is allocated statically, so we shouldn't free it */
2404
Pawel Wieczorkiewicz503bded2013-02-20 17:26:20 +01002405 clear_bit(port->line, atmel_ports_in_use);
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002406
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002407 clk_put(atmel_port->clk);
Andrew Victorafefc412006-06-19 19:53:19 +01002408
2409 return ret;
2410}
2411
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002412static struct platform_driver atmel_serial_driver = {
2413 .probe = atmel_serial_probe,
Bill Pemberton2d47b712012-11-19 13:21:34 -05002414 .remove = atmel_serial_remove,
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002415 .suspend = atmel_serial_suspend,
2416 .resume = atmel_serial_resume,
Andrew Victorafefc412006-06-19 19:53:19 +01002417 .driver = {
Haavard Skinnemoen1e8ea802006-10-04 16:02:03 +02002418 .name = "atmel_usart",
Andrew Victorafefc412006-06-19 19:53:19 +01002419 .owner = THIS_MODULE,
Nicolas Ferre5fbe46b2011-10-12 18:07:00 +02002420 .of_match_table = of_match_ptr(atmel_serial_dt_ids),
Andrew Victorafefc412006-06-19 19:53:19 +01002421 },
2422};
2423
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002424static int __init atmel_serial_init(void)
Andrew Victorafefc412006-06-19 19:53:19 +01002425{
2426 int ret;
2427
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002428 ret = uart_register_driver(&atmel_uart);
Andrew Victorafefc412006-06-19 19:53:19 +01002429 if (ret)
2430 return ret;
2431
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002432 ret = platform_driver_register(&atmel_serial_driver);
Andrew Victorafefc412006-06-19 19:53:19 +01002433 if (ret)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002434 uart_unregister_driver(&atmel_uart);
Andrew Victorafefc412006-06-19 19:53:19 +01002435
2436 return ret;
2437}
2438
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002439static void __exit atmel_serial_exit(void)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002440{
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002441 platform_driver_unregister(&atmel_serial_driver);
2442 uart_unregister_driver(&atmel_uart);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002443}
2444
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002445module_init(atmel_serial_init);
2446module_exit(atmel_serial_exit);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002447
2448MODULE_AUTHOR("Rick Bronson");
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002449MODULE_DESCRIPTION("Atmel AT91 / AT32 serial port driver");
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002450MODULE_LICENSE("GPL");
Kay Sieverse169c132008-04-15 14:34:35 -07002451MODULE_ALIAS("platform:atmel_usart");