blob: d8185c7f476f8ba3f1e28c926b656f42921aedb0 [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>
Linus Walleij354e57f2013-11-07 10:25:55 +010038#include <linux/of_gpio.h>
Chip Coldwella6670612008-02-08 04:21:06 -080039#include <linux/dma-mapping.h>
Vinod Koul6b997ba2014-10-16 12:59:06 +053040#include <linux/dmaengine.h>
Andrew Victor93a3ddc2007-02-08 11:31:22 +010041#include <linux/atmel_pdc.h>
Guennadi Liakhovetskifa3218d2008-01-29 15:43:13 +010042#include <linux/atmel_serial.h>
Claudio Scordinoe8faff72010-05-03 13:31:28 +010043#include <linux/uaccess.h>
Jean-Christophe PLAGNIOL-VILLARDbcd23602012-10-30 05:12:23 +080044#include <linux/platform_data/atmel.h>
Elen Song2e68c222013-07-22 16:30:30 +080045#include <linux/timer.h>
Linus Walleij354e57f2013-11-07 10:25:55 +010046#include <linux/gpio.h>
Richard Genoude0b0baa2014-05-13 20:20:44 +020047#include <linux/gpio/consumer.h>
48#include <linux/err.h>
Richard Genoudab5e4e42014-05-13 20:20:45 +020049#include <linux/irq.h>
Andrew Victor1e6c9c22006-01-10 16:59:27 +000050
51#include <asm/io.h>
Peter Huewef7512e72010-06-29 19:35:39 +020052#include <asm/ioctls.h>
Andrew Victor1e6c9c22006-01-10 16:59:27 +000053
Chip Coldwella6670612008-02-08 04:21:06 -080054#define PDC_BUFFER_SIZE 512
55/* Revisit: We should calculate this based on the actual port settings */
56#define PDC_RX_TIMEOUT (3 * 10) /* 3 bytes */
57
Haavard Skinnemoen749c4e62006-10-04 16:02:02 +020058#if defined(CONFIG_SERIAL_ATMEL_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
Andrew Victor1e6c9c22006-01-10 16:59:27 +000059#define SUPPORT_SYSRQ
60#endif
61
62#include <linux/serial_core.h>
63
Richard Genoude0b0baa2014-05-13 20:20:44 +020064#include "serial_mctrl_gpio.h"
65
Claudio Scordinoe8faff72010-05-03 13:31:28 +010066static void atmel_start_rx(struct uart_port *port);
67static void atmel_stop_rx(struct uart_port *port);
68
Haavard Skinnemoen749c4e62006-10-04 16:02:02 +020069#ifdef CONFIG_SERIAL_ATMEL_TTYAT
Andrew Victor1e6c9c22006-01-10 16:59:27 +000070
71/* Use device name ttyAT, major 204 and minor 154-169. This is necessary if we
72 * should coexist with the 8250 driver, such as if we have an external 16C550
73 * UART. */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +020074#define SERIAL_ATMEL_MAJOR 204
Andrew Victor1e6c9c22006-01-10 16:59:27 +000075#define MINOR_START 154
Haavard Skinnemoen7192f922006-10-04 16:02:05 +020076#define ATMEL_DEVICENAME "ttyAT"
Andrew Victor1e6c9c22006-01-10 16:59:27 +000077
78#else
79
80/* Use device name ttyS, major 4, minor 64-68. This is the usual serial port
81 * name, but it is legally reserved for the 8250 driver. */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +020082#define SERIAL_ATMEL_MAJOR TTY_MAJOR
Andrew Victor1e6c9c22006-01-10 16:59:27 +000083#define MINOR_START 64
Haavard Skinnemoen7192f922006-10-04 16:02:05 +020084#define ATMEL_DEVICENAME "ttyS"
Andrew Victor1e6c9c22006-01-10 16:59:27 +000085
86#endif
87
Haavard Skinnemoen7192f922006-10-04 16:02:05 +020088#define ATMEL_ISR_PASS_LIMIT 256
Andrew Victor1e6c9c22006-01-10 16:59:27 +000089
Remy Bohmerb843aa22008-02-08 04:21:01 -080090/* UART registers. CR is write-only, hence no GET macro */
Haavard Skinnemoen544fc722007-02-12 00:52:15 -080091#define UART_PUT_CR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_CR)
92#define UART_GET_MR(port) __raw_readl((port)->membase + ATMEL_US_MR)
93#define UART_PUT_MR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_MR)
94#define UART_PUT_IER(port,v) __raw_writel(v, (port)->membase + ATMEL_US_IER)
95#define UART_PUT_IDR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_IDR)
96#define UART_GET_IMR(port) __raw_readl((port)->membase + ATMEL_US_IMR)
97#define UART_GET_CSR(port) __raw_readl((port)->membase + ATMEL_US_CSR)
98#define UART_GET_CHAR(port) __raw_readl((port)->membase + ATMEL_US_RHR)
99#define UART_PUT_CHAR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_THR)
100#define UART_GET_BRGR(port) __raw_readl((port)->membase + ATMEL_US_BRGR)
101#define UART_PUT_BRGR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_BRGR)
102#define UART_PUT_RTOR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_RTOR)
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100103#define UART_PUT_TTGR(port, v) __raw_writel(v, (port)->membase + ATMEL_US_TTGR)
Elen Song055560b2013-07-22 16:30:29 +0800104#define UART_GET_IP_NAME(port) __raw_readl((port)->membase + ATMEL_US_NAME)
Nicolas Ferre731d9ca2013-10-17 17:37:12 +0200105#define UART_GET_IP_VERSION(port) __raw_readl((port)->membase + ATMEL_US_VERSION)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000106
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000107 /* PDC registers */
Haavard Skinnemoen544fc722007-02-12 00:52:15 -0800108#define UART_PUT_PTCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_PTCR)
109#define UART_GET_PTSR(port) __raw_readl((port)->membase + ATMEL_PDC_PTSR)
Andrew Victorafefc412006-06-19 19:53:19 +0100110
Haavard Skinnemoen544fc722007-02-12 00:52:15 -0800111#define UART_PUT_RPR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RPR)
112#define UART_GET_RPR(port) __raw_readl((port)->membase + ATMEL_PDC_RPR)
113#define UART_PUT_RCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RCR)
114#define UART_PUT_RNPR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RNPR)
115#define UART_PUT_RNCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RNCR)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000116
Haavard Skinnemoen544fc722007-02-12 00:52:15 -0800117#define UART_PUT_TPR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_TPR)
118#define UART_PUT_TCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_TCR)
Marc Pignat39d4c922008-04-02 13:04:42 -0700119#define UART_GET_TCR(port) __raw_readl((port)->membase + ATMEL_PDC_TCR)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000120
Chip Coldwella6670612008-02-08 04:21:06 -0800121struct atmel_dma_buffer {
122 unsigned char *buf;
123 dma_addr_t dma_addr;
124 unsigned int dma_size;
125 unsigned int ofs;
126};
127
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800128struct atmel_uart_char {
129 u16 status;
130 u16 ch;
131};
132
133#define ATMEL_SERIAL_RINGSIZE 1024
134
Andrew Victorafefc412006-06-19 19:53:19 +0100135/*
136 * We wrap our port structure around the generic uart_port.
137 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200138struct atmel_uart_port {
Andrew Victorafefc412006-06-19 19:53:19 +0100139 struct uart_port uart; /* uart */
140 struct clk *clk; /* uart clock */
Anti Sullinf05596d2008-09-22 13:57:54 -0700141 int may_wakeup; /* cached value of device_may_wakeup for times we need to disable it */
142 u32 backup_imr; /* IMR saved during suspend */
Haavard Skinnemoen9e6077b2007-07-15 23:40:36 -0700143 int break_active; /* break being received */
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800144
Elen Song34df42f2013-07-22 16:30:27 +0800145 bool use_dma_rx; /* enable DMA receiver */
Elen Song64e22eb2013-07-22 16:30:24 +0800146 bool use_pdc_rx; /* enable PDC receiver */
Chip Coldwella6670612008-02-08 04:21:06 -0800147 short pdc_rx_idx; /* current PDC RX buffer */
148 struct atmel_dma_buffer pdc_rx[2]; /* PDC receier */
149
Elen Song08f738b2013-07-22 16:30:26 +0800150 bool use_dma_tx; /* enable DMA transmitter */
Elen Song64e22eb2013-07-22 16:30:24 +0800151 bool use_pdc_tx; /* enable PDC transmitter */
Chip Coldwella6670612008-02-08 04:21:06 -0800152 struct atmel_dma_buffer pdc_tx; /* PDC transmitter */
153
Elen Song08f738b2013-07-22 16:30:26 +0800154 spinlock_t lock_tx; /* port lock */
Elen Song34df42f2013-07-22 16:30:27 +0800155 spinlock_t lock_rx; /* port lock */
Elen Song08f738b2013-07-22 16:30:26 +0800156 struct dma_chan *chan_tx;
Elen Song34df42f2013-07-22 16:30:27 +0800157 struct dma_chan *chan_rx;
Elen Song08f738b2013-07-22 16:30:26 +0800158 struct dma_async_tx_descriptor *desc_tx;
Elen Song34df42f2013-07-22 16:30:27 +0800159 struct dma_async_tx_descriptor *desc_rx;
Elen Song08f738b2013-07-22 16:30:26 +0800160 dma_cookie_t cookie_tx;
Elen Song34df42f2013-07-22 16:30:27 +0800161 dma_cookie_t cookie_rx;
Elen Song08f738b2013-07-22 16:30:26 +0800162 struct scatterlist sg_tx;
Elen Song34df42f2013-07-22 16:30:27 +0800163 struct scatterlist sg_rx;
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800164 struct tasklet_struct tasklet;
165 unsigned int irq_status;
166 unsigned int irq_status_prev;
167
168 struct circ_buf rx_ring;
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100169
170 struct serial_rs485 rs485; /* rs485 settings */
Richard Genoude0b0baa2014-05-13 20:20:44 +0200171 struct mctrl_gpios *gpios;
Richard Genoudab5e4e42014-05-13 20:20:45 +0200172 int gpio_irq[UART_GPIO_MAX];
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100173 unsigned int tx_done_mask;
Richard Genoudab5e4e42014-05-13 20:20:45 +0200174 bool ms_irq_enabled;
Elen Song055560b2013-07-22 16:30:29 +0800175 bool is_usart; /* usart or uart */
Elen Song2e68c222013-07-22 16:30:30 +0800176 struct timer_list uart_timer; /* uart timer */
Elen Songa930e522013-07-22 16:30:25 +0800177 int (*prepare_rx)(struct uart_port *port);
178 int (*prepare_tx)(struct uart_port *port);
179 void (*schedule_rx)(struct uart_port *port);
180 void (*schedule_tx)(struct uart_port *port);
181 void (*release_rx)(struct uart_port *port);
182 void (*release_tx)(struct uart_port *port);
Andrew Victorafefc412006-06-19 19:53:19 +0100183};
184
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200185static struct atmel_uart_port atmel_ports[ATMEL_MAX_UART];
Pawel Wieczorkiewicz503bded2013-02-20 17:26:20 +0100186static DECLARE_BITMAP(atmel_ports_in_use, ATMEL_MAX_UART);
Andrew Victorafefc412006-06-19 19:53:19 +0100187
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000188#ifdef SUPPORT_SYSRQ
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200189static struct console atmel_console;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000190#endif
191
Nicolas Ferre5fbe46b2011-10-12 18:07:00 +0200192#if defined(CONFIG_OF)
193static const struct of_device_id atmel_serial_dt_ids[] = {
194 { .compatible = "atmel,at91rm9200-usart" },
195 { .compatible = "atmel,at91sam9260-usart" },
196 { /* sentinel */ }
197};
198
199MODULE_DEVICE_TABLE(of, atmel_serial_dt_ids);
200#endif
201
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -0800202static inline struct atmel_uart_port *
203to_atmel_uart_port(struct uart_port *uart)
204{
205 return container_of(uart, struct atmel_uart_port, uart);
206}
207
Chip Coldwella6670612008-02-08 04:21:06 -0800208#ifdef CONFIG_SERIAL_ATMEL_PDC
Elen Song64e22eb2013-07-22 16:30:24 +0800209static bool atmel_use_pdc_rx(struct uart_port *port)
Chip Coldwella6670612008-02-08 04:21:06 -0800210{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -0800211 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Chip Coldwella6670612008-02-08 04:21:06 -0800212
Elen Song64e22eb2013-07-22 16:30:24 +0800213 return atmel_port->use_pdc_rx;
Chip Coldwella6670612008-02-08 04:21:06 -0800214}
215
Elen Song64e22eb2013-07-22 16:30:24 +0800216static bool atmel_use_pdc_tx(struct uart_port *port)
Chip Coldwella6670612008-02-08 04:21:06 -0800217{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -0800218 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Chip Coldwella6670612008-02-08 04:21:06 -0800219
Elen Song64e22eb2013-07-22 16:30:24 +0800220 return atmel_port->use_pdc_tx;
Chip Coldwella6670612008-02-08 04:21:06 -0800221}
222#else
Elen Song64e22eb2013-07-22 16:30:24 +0800223static bool atmel_use_pdc_rx(struct uart_port *port)
Chip Coldwella6670612008-02-08 04:21:06 -0800224{
225 return false;
226}
227
Elen Song64e22eb2013-07-22 16:30:24 +0800228static bool atmel_use_pdc_tx(struct uart_port *port)
Chip Coldwella6670612008-02-08 04:21:06 -0800229{
230 return false;
231}
232#endif
233
Elen Song08f738b2013-07-22 16:30:26 +0800234static bool atmel_use_dma_tx(struct uart_port *port)
235{
236 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
237
238 return atmel_port->use_dma_tx;
239}
240
Elen Song34df42f2013-07-22 16:30:27 +0800241static bool atmel_use_dma_rx(struct uart_port *port)
242{
243 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
244
245 return atmel_port->use_dma_rx;
246}
247
Richard Genoude0b0baa2014-05-13 20:20:44 +0200248static unsigned int atmel_get_lines_status(struct uart_port *port)
249{
250 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
251 unsigned int status, ret = 0;
252
253 status = UART_GET_CSR(port);
254
255 mctrl_gpio_get(atmel_port->gpios, &ret);
256
257 if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios,
258 UART_GPIO_CTS))) {
259 if (ret & TIOCM_CTS)
260 status &= ~ATMEL_US_CTS;
261 else
262 status |= ATMEL_US_CTS;
263 }
264
265 if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios,
266 UART_GPIO_DSR))) {
267 if (ret & TIOCM_DSR)
268 status &= ~ATMEL_US_DSR;
269 else
270 status |= ATMEL_US_DSR;
271 }
272
273 if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios,
274 UART_GPIO_RI))) {
275 if (ret & TIOCM_RI)
276 status &= ~ATMEL_US_RI;
277 else
278 status |= ATMEL_US_RI;
279 }
280
281 if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios,
282 UART_GPIO_DCD))) {
283 if (ret & TIOCM_CD)
284 status &= ~ATMEL_US_DCD;
285 else
286 status |= ATMEL_US_DCD;
287 }
288
289 return status;
290}
291
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100292/* Enable or disable the rs485 support */
293void atmel_config_rs485(struct uart_port *port, struct serial_rs485 *rs485conf)
294{
295 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
296 unsigned int mode;
Claudio Scordinodbf11152011-12-16 15:08:49 +0100297 unsigned long flags;
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100298
Claudio Scordinodbf11152011-12-16 15:08:49 +0100299 spin_lock_irqsave(&port->lock, flags);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100300
301 /* Disable interrupts */
302 UART_PUT_IDR(port, atmel_port->tx_done_mask);
303
304 mode = UART_GET_MR(port);
305
306 /* Resetting serial mode to RS232 (0x0) */
307 mode &= ~ATMEL_US_USMODE;
308
309 atmel_port->rs485 = *rs485conf;
310
311 if (rs485conf->flags & SER_RS485_ENABLED) {
312 dev_dbg(port->dev, "Setting UART to RS485\n");
313 atmel_port->tx_done_mask = ATMEL_US_TXEMPTY;
Claudio Scordino93f33502011-11-09 15:51:49 +0100314 if ((rs485conf->delay_rts_after_send) > 0)
Claudio Scordino1b633182010-07-20 15:26:47 -0700315 UART_PUT_TTGR(port, rs485conf->delay_rts_after_send);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100316 mode |= ATMEL_US_USMODE_RS485;
317 } else {
318 dev_dbg(port->dev, "Setting UART to RS232\n");
Elen Song64e22eb2013-07-22 16:30:24 +0800319 if (atmel_use_pdc_tx(port))
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100320 atmel_port->tx_done_mask = ATMEL_US_ENDTX |
321 ATMEL_US_TXBUFE;
322 else
323 atmel_port->tx_done_mask = ATMEL_US_TXRDY;
324 }
325 UART_PUT_MR(port, mode);
326
327 /* Enable interrupts */
328 UART_PUT_IER(port, atmel_port->tx_done_mask);
329
Claudio Scordinodbf11152011-12-16 15:08:49 +0100330 spin_unlock_irqrestore(&port->lock, flags);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100331
332}
333
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000334/*
335 * Return TIOCSER_TEMT when transmitter FIFO and Shift register is empty.
336 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200337static u_int atmel_tx_empty(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000338{
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200339 return (UART_GET_CSR(port) & ATMEL_US_TXEMPTY) ? TIOCSER_TEMT : 0;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000340}
341
342/*
343 * Set state of the modem control output lines
344 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200345static void atmel_set_mctrl(struct uart_port *port, u_int mctrl)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000346{
347 unsigned int control = 0;
Andrew Victorafefc412006-06-19 19:53:19 +0100348 unsigned int mode;
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100349 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000350
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000351 if (mctrl & TIOCM_RTS)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200352 control |= ATMEL_US_RTSEN;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000353 else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200354 control |= ATMEL_US_RTSDIS;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000355
356 if (mctrl & TIOCM_DTR)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200357 control |= ATMEL_US_DTREN;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000358 else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200359 control |= ATMEL_US_DTRDIS;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000360
Andrew Victorafefc412006-06-19 19:53:19 +0100361 UART_PUT_CR(port, control);
362
Richard Genoude0b0baa2014-05-13 20:20:44 +0200363 mctrl_gpio_set(atmel_port->gpios, mctrl);
364
Andrew Victorafefc412006-06-19 19:53:19 +0100365 /* Local loopback mode? */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200366 mode = UART_GET_MR(port) & ~ATMEL_US_CHMODE;
Andrew Victorafefc412006-06-19 19:53:19 +0100367 if (mctrl & TIOCM_LOOP)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200368 mode |= ATMEL_US_CHMODE_LOC_LOOP;
Andrew Victorafefc412006-06-19 19:53:19 +0100369 else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200370 mode |= ATMEL_US_CHMODE_NORMAL;
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100371
372 /* Resetting serial mode to RS232 (0x0) */
373 mode &= ~ATMEL_US_USMODE;
374
375 if (atmel_port->rs485.flags & SER_RS485_ENABLED) {
376 dev_dbg(port->dev, "Setting UART to RS485\n");
Claudio Scordino93f33502011-11-09 15:51:49 +0100377 if ((atmel_port->rs485.delay_rts_after_send) > 0)
Claudio Scordino1b633182010-07-20 15:26:47 -0700378 UART_PUT_TTGR(port,
379 atmel_port->rs485.delay_rts_after_send);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100380 mode |= ATMEL_US_USMODE_RS485;
381 } else {
382 dev_dbg(port->dev, "Setting UART to RS232\n");
383 }
Andrew Victorafefc412006-06-19 19:53:19 +0100384 UART_PUT_MR(port, mode);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000385}
386
387/*
388 * Get state of the modem control input lines
389 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200390static u_int atmel_get_mctrl(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000391{
Richard Genoude0b0baa2014-05-13 20:20:44 +0200392 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
393 unsigned int ret = 0, status;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000394
395 status = UART_GET_CSR(port);
396
397 /*
398 * The control signals are active low.
399 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200400 if (!(status & ATMEL_US_DCD))
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000401 ret |= TIOCM_CD;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200402 if (!(status & ATMEL_US_CTS))
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000403 ret |= TIOCM_CTS;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200404 if (!(status & ATMEL_US_DSR))
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000405 ret |= TIOCM_DSR;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200406 if (!(status & ATMEL_US_RI))
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000407 ret |= TIOCM_RI;
408
Richard Genoude0b0baa2014-05-13 20:20:44 +0200409 return mctrl_gpio_get(atmel_port->gpios, &ret);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000410}
411
412/*
413 * Stop transmitting.
414 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200415static void atmel_stop_tx(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000416{
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100417 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
418
Elen Song64e22eb2013-07-22 16:30:24 +0800419 if (atmel_use_pdc_tx(port)) {
Chip Coldwella6670612008-02-08 04:21:06 -0800420 /* disable PDC transmit */
421 UART_PUT_PTCR(port, ATMEL_PDC_TXTDIS);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100422 }
423 /* Disable interrupts */
424 UART_PUT_IDR(port, atmel_port->tx_done_mask);
425
Bernhard Roth83cac9f2011-08-24 09:48:23 +0200426 if ((atmel_port->rs485.flags & SER_RS485_ENABLED) &&
427 !(atmel_port->rs485.flags & SER_RS485_RX_DURING_TX))
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100428 atmel_start_rx(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000429}
430
431/*
432 * Start transmitting.
433 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200434static void atmel_start_tx(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000435{
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100436 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
437
Elen Song64e22eb2013-07-22 16:30:24 +0800438 if (atmel_use_pdc_tx(port)) {
Chip Coldwella6670612008-02-08 04:21:06 -0800439 if (UART_GET_PTSR(port) & ATMEL_PDC_TXTEN)
440 /* The transmitter is already running. Yes, we
441 really need this.*/
442 return;
443
Bernhard Roth83cac9f2011-08-24 09:48:23 +0200444 if ((atmel_port->rs485.flags & SER_RS485_ENABLED) &&
445 !(atmel_port->rs485.flags & SER_RS485_RX_DURING_TX))
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100446 atmel_stop_rx(port);
447
Chip Coldwella6670612008-02-08 04:21:06 -0800448 /* re-enable PDC transmit */
449 UART_PUT_PTCR(port, ATMEL_PDC_TXTEN);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100450 }
451 /* Enable interrupts */
452 UART_PUT_IER(port, atmel_port->tx_done_mask);
453}
454
455/*
456 * start receiving - port is in process of being opened.
457 */
458static void atmel_start_rx(struct uart_port *port)
459{
460 UART_PUT_CR(port, ATMEL_US_RSTSTA); /* reset status and receiver */
461
Siftar, Gabe57c36862012-03-29 15:40:05 +0200462 UART_PUT_CR(port, ATMEL_US_RXEN);
463
Elen Song64e22eb2013-07-22 16:30:24 +0800464 if (atmel_use_pdc_rx(port)) {
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100465 /* enable PDC controller */
466 UART_PUT_IER(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT |
467 port->read_status_mask);
468 UART_PUT_PTCR(port, ATMEL_PDC_RXTEN);
469 } else {
470 UART_PUT_IER(port, ATMEL_US_RXRDY);
471 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000472}
473
474/*
475 * Stop receiving - port is in process of being closed.
476 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200477static void atmel_stop_rx(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000478{
Siftar, Gabe57c36862012-03-29 15:40:05 +0200479 UART_PUT_CR(port, ATMEL_US_RXDIS);
480
Elen Song64e22eb2013-07-22 16:30:24 +0800481 if (atmel_use_pdc_rx(port)) {
Chip Coldwella6670612008-02-08 04:21:06 -0800482 /* disable PDC receive */
483 UART_PUT_PTCR(port, ATMEL_PDC_RXTDIS);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100484 UART_PUT_IDR(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT |
485 port->read_status_mask);
486 } else {
Chip Coldwella6670612008-02-08 04:21:06 -0800487 UART_PUT_IDR(port, ATMEL_US_RXRDY);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100488 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000489}
490
491/*
492 * Enable modem status interrupts
493 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200494static void atmel_enable_ms(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000495{
Richard Genoudab5e4e42014-05-13 20:20:45 +0200496 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
497 uint32_t ier = 0;
498
499 /*
500 * Interrupt should not be enabled twice
501 */
502 if (atmel_port->ms_irq_enabled)
503 return;
504
505 atmel_port->ms_irq_enabled = true;
506
507 if (atmel_port->gpio_irq[UART_GPIO_CTS] >= 0)
508 enable_irq(atmel_port->gpio_irq[UART_GPIO_CTS]);
509 else
510 ier |= ATMEL_US_CTSIC;
511
512 if (atmel_port->gpio_irq[UART_GPIO_DSR] >= 0)
513 enable_irq(atmel_port->gpio_irq[UART_GPIO_DSR]);
514 else
515 ier |= ATMEL_US_DSRIC;
516
517 if (atmel_port->gpio_irq[UART_GPIO_RI] >= 0)
518 enable_irq(atmel_port->gpio_irq[UART_GPIO_RI]);
519 else
520 ier |= ATMEL_US_RIIC;
521
522 if (atmel_port->gpio_irq[UART_GPIO_DCD] >= 0)
523 enable_irq(atmel_port->gpio_irq[UART_GPIO_DCD]);
524 else
525 ier |= ATMEL_US_DCDIC;
526
527 UART_PUT_IER(port, ier);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000528}
529
530/*
531 * Control the transmission of a break signal
532 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200533static void atmel_break_ctl(struct uart_port *port, int break_state)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000534{
535 if (break_state != 0)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200536 UART_PUT_CR(port, ATMEL_US_STTBRK); /* start break */
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000537 else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200538 UART_PUT_CR(port, ATMEL_US_STPBRK); /* stop break */
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000539}
540
541/*
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800542 * Stores the incoming character in the ring buffer
543 */
544static void
545atmel_buffer_rx_char(struct uart_port *port, unsigned int status,
546 unsigned int ch)
547{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -0800548 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800549 struct circ_buf *ring = &atmel_port->rx_ring;
550 struct atmel_uart_char *c;
551
552 if (!CIRC_SPACE(ring->head, ring->tail, ATMEL_SERIAL_RINGSIZE))
553 /* Buffer overflow, ignore char */
554 return;
555
556 c = &((struct atmel_uart_char *)ring->buf)[ring->head];
557 c->status = status;
558 c->ch = ch;
559
560 /* Make sure the character is stored before we update head. */
561 smp_wmb();
562
563 ring->head = (ring->head + 1) & (ATMEL_SERIAL_RINGSIZE - 1);
564}
565
566/*
Chip Coldwella6670612008-02-08 04:21:06 -0800567 * Deal with parity, framing and overrun errors.
568 */
569static void atmel_pdc_rxerr(struct uart_port *port, unsigned int status)
570{
571 /* clear error */
572 UART_PUT_CR(port, ATMEL_US_RSTSTA);
573
574 if (status & ATMEL_US_RXBRK) {
575 /* ignore side-effect */
576 status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME);
577 port->icount.brk++;
578 }
579 if (status & ATMEL_US_PARE)
580 port->icount.parity++;
581 if (status & ATMEL_US_FRAME)
582 port->icount.frame++;
583 if (status & ATMEL_US_OVRE)
584 port->icount.overrun++;
585}
586
587/*
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000588 * Characters received (called from interrupt handler)
589 */
David Howells7d12e782006-10-05 14:55:46 +0100590static void atmel_rx_chars(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000591{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -0800592 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800593 unsigned int status, ch;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000594
Andrew Victorafefc412006-06-19 19:53:19 +0100595 status = UART_GET_CSR(port);
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200596 while (status & ATMEL_US_RXRDY) {
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000597 ch = UART_GET_CHAR(port);
598
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000599 /*
600 * note that the error handling code is
601 * out of the main execution path
602 */
Haavard Skinnemoen9e6077b2007-07-15 23:40:36 -0700603 if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME
604 | ATMEL_US_OVRE | ATMEL_US_RXBRK)
605 || atmel_port->break_active)) {
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800606
Remy Bohmerb843aa22008-02-08 04:21:01 -0800607 /* clear error */
608 UART_PUT_CR(port, ATMEL_US_RSTSTA);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800609
Haavard Skinnemoen9e6077b2007-07-15 23:40:36 -0700610 if (status & ATMEL_US_RXBRK
611 && !atmel_port->break_active) {
Haavard Skinnemoen9e6077b2007-07-15 23:40:36 -0700612 atmel_port->break_active = 1;
613 UART_PUT_IER(port, ATMEL_US_RXBRK);
Haavard Skinnemoen9e6077b2007-07-15 23:40:36 -0700614 } else {
615 /*
616 * This is either the end-of-break
617 * condition or we've received at
618 * least one character without RXBRK
619 * being set. In both cases, the next
620 * RXBRK will indicate start-of-break.
621 */
622 UART_PUT_IDR(port, ATMEL_US_RXBRK);
623 status &= ~ATMEL_US_RXBRK;
624 atmel_port->break_active = 0;
Andrew Victorafefc412006-06-19 19:53:19 +0100625 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000626 }
627
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800628 atmel_buffer_rx_char(port, status, ch);
Andrew Victorafefc412006-06-19 19:53:19 +0100629 status = UART_GET_CSR(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000630 }
631
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800632 tasklet_schedule(&atmel_port->tasklet);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000633}
634
635/*
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800636 * Transmit characters (called from tasklet with TXRDY interrupt
637 * disabled)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000638 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200639static void atmel_tx_chars(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000640{
Alan Coxebd2c8f2009-09-19 13:13:28 -0700641 struct circ_buf *xmit = &port->state->xmit;
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100642 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000643
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100644 if (port->x_char && UART_GET_CSR(port) & atmel_port->tx_done_mask) {
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000645 UART_PUT_CHAR(port, port->x_char);
646 port->icount.tx++;
647 port->x_char = 0;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000648 }
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800649 if (uart_circ_empty(xmit) || uart_tx_stopped(port))
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000650 return;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000651
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100652 while (UART_GET_CSR(port) & atmel_port->tx_done_mask) {
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000653 UART_PUT_CHAR(port, xmit->buf[xmit->tail]);
654 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
655 port->icount.tx++;
656 if (uart_circ_empty(xmit))
657 break;
658 }
659
660 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
661 uart_write_wakeup(port);
662
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800663 if (!uart_circ_empty(xmit))
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100664 /* Enable interrupts */
665 UART_PUT_IER(port, atmel_port->tx_done_mask);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000666}
667
Elen Song08f738b2013-07-22 16:30:26 +0800668static void atmel_complete_tx_dma(void *arg)
669{
670 struct atmel_uart_port *atmel_port = arg;
671 struct uart_port *port = &atmel_port->uart;
672 struct circ_buf *xmit = &port->state->xmit;
673 struct dma_chan *chan = atmel_port->chan_tx;
674 unsigned long flags;
675
676 spin_lock_irqsave(&port->lock, flags);
677
678 if (chan)
679 dmaengine_terminate_all(chan);
680 xmit->tail += sg_dma_len(&atmel_port->sg_tx);
681 xmit->tail &= UART_XMIT_SIZE - 1;
682
683 port->icount.tx += sg_dma_len(&atmel_port->sg_tx);
684
685 spin_lock_irq(&atmel_port->lock_tx);
686 async_tx_ack(atmel_port->desc_tx);
687 atmel_port->cookie_tx = -EINVAL;
688 atmel_port->desc_tx = NULL;
689 spin_unlock_irq(&atmel_port->lock_tx);
690
691 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
692 uart_write_wakeup(port);
693
694 /* Do we really need this? */
695 if (!uart_circ_empty(xmit))
696 tasklet_schedule(&atmel_port->tasklet);
697
698 spin_unlock_irqrestore(&port->lock, flags);
699}
700
701static void atmel_release_tx_dma(struct uart_port *port)
702{
703 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
704 struct dma_chan *chan = atmel_port->chan_tx;
705
706 if (chan) {
707 dmaengine_terminate_all(chan);
708 dma_release_channel(chan);
709 dma_unmap_sg(port->dev, &atmel_port->sg_tx, 1,
Wolfram Sang48479142014-07-21 11:42:04 +0200710 DMA_TO_DEVICE);
Elen Song08f738b2013-07-22 16:30:26 +0800711 }
712
713 atmel_port->desc_tx = NULL;
714 atmel_port->chan_tx = NULL;
715 atmel_port->cookie_tx = -EINVAL;
716}
717
718/*
719 * Called from tasklet with TXRDY interrupt is disabled.
720 */
721static void atmel_tx_dma(struct uart_port *port)
722{
723 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
724 struct circ_buf *xmit = &port->state->xmit;
725 struct dma_chan *chan = atmel_port->chan_tx;
726 struct dma_async_tx_descriptor *desc;
727 struct scatterlist *sg = &atmel_port->sg_tx;
728
729 /* Make sure we have an idle channel */
730 if (atmel_port->desc_tx != NULL)
731 return;
732
733 if (!uart_circ_empty(xmit) && !uart_tx_stopped(port)) {
734 /*
735 * DMA is idle now.
736 * Port xmit buffer is already mapped,
737 * and it is one page... Just adjust
738 * offsets and lengths. Since it is a circular buffer,
739 * we have to transmit till the end, and then the rest.
740 * Take the port lock to get a
741 * consistent xmit buffer state.
742 */
743 sg->offset = xmit->tail & (UART_XMIT_SIZE - 1);
744 sg_dma_address(sg) = (sg_dma_address(sg) &
745 ~(UART_XMIT_SIZE - 1))
746 + sg->offset;
747 sg_dma_len(sg) = CIRC_CNT_TO_END(xmit->head,
748 xmit->tail,
749 UART_XMIT_SIZE);
750 BUG_ON(!sg_dma_len(sg));
751
752 desc = dmaengine_prep_slave_sg(chan,
753 sg,
754 1,
755 DMA_MEM_TO_DEV,
756 DMA_PREP_INTERRUPT |
757 DMA_CTRL_ACK);
758 if (!desc) {
759 dev_err(port->dev, "Failed to send via dma!\n");
760 return;
761 }
762
763 dma_sync_sg_for_device(port->dev, sg, 1, DMA_MEM_TO_DEV);
764
765 atmel_port->desc_tx = desc;
766 desc->callback = atmel_complete_tx_dma;
767 desc->callback_param = atmel_port;
768 atmel_port->cookie_tx = dmaengine_submit(desc);
769
770 } else {
771 if (atmel_port->rs485.flags & SER_RS485_ENABLED) {
772 /* DMA done, stop TX, start RX for RS485 */
773 atmel_start_rx(port);
774 }
775 }
776
777 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
778 uart_write_wakeup(port);
779}
780
781static int atmel_prepare_tx_dma(struct uart_port *port)
782{
783 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
784 dma_cap_mask_t mask;
785 struct dma_slave_config config;
786 int ret, nent;
787
788 dma_cap_zero(mask);
789 dma_cap_set(DMA_SLAVE, mask);
790
791 atmel_port->chan_tx = dma_request_slave_channel(port->dev, "tx");
792 if (atmel_port->chan_tx == NULL)
793 goto chan_err;
794 dev_info(port->dev, "using %s for tx DMA transfers\n",
795 dma_chan_name(atmel_port->chan_tx));
796
797 spin_lock_init(&atmel_port->lock_tx);
798 sg_init_table(&atmel_port->sg_tx, 1);
799 /* UART circular tx buffer is an aligned page. */
800 BUG_ON((int)port->state->xmit.buf & ~PAGE_MASK);
801 sg_set_page(&atmel_port->sg_tx,
802 virt_to_page(port->state->xmit.buf),
803 UART_XMIT_SIZE,
804 (int)port->state->xmit.buf & ~PAGE_MASK);
805 nent = dma_map_sg(port->dev,
806 &atmel_port->sg_tx,
807 1,
Wolfram Sang48479142014-07-21 11:42:04 +0200808 DMA_TO_DEVICE);
Elen Song08f738b2013-07-22 16:30:26 +0800809
810 if (!nent) {
811 dev_dbg(port->dev, "need to release resource of dma\n");
812 goto chan_err;
813 } else {
814 dev_dbg(port->dev, "%s: mapped %d@%p to %x\n", __func__,
815 sg_dma_len(&atmel_port->sg_tx),
816 port->state->xmit.buf,
817 sg_dma_address(&atmel_port->sg_tx));
818 }
819
820 /* Configure the slave DMA */
821 memset(&config, 0, sizeof(config));
822 config.direction = DMA_MEM_TO_DEV;
823 config.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
824 config.dst_addr = port->mapbase + ATMEL_US_THR;
825
826 ret = dmaengine_device_control(atmel_port->chan_tx,
827 DMA_SLAVE_CONFIG,
828 (unsigned long)&config);
829 if (ret) {
830 dev_err(port->dev, "DMA tx slave configuration failed\n");
831 goto chan_err;
832 }
833
834 return 0;
835
836chan_err:
837 dev_err(port->dev, "TX channel not available, switch to pio\n");
838 atmel_port->use_dma_tx = 0;
839 if (atmel_port->chan_tx)
840 atmel_release_tx_dma(port);
841 return -EINVAL;
842}
843
Elen Song34df42f2013-07-22 16:30:27 +0800844static void atmel_flip_buffer_rx_dma(struct uart_port *port,
845 char *buf, size_t count)
846{
847 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
848 struct tty_port *tport = &port->state->port;
849
850 dma_sync_sg_for_cpu(port->dev,
851 &atmel_port->sg_rx,
852 1,
853 DMA_DEV_TO_MEM);
854
855 tty_insert_flip_string(tport, buf, count);
856
857 dma_sync_sg_for_device(port->dev,
858 &atmel_port->sg_rx,
859 1,
860 DMA_DEV_TO_MEM);
861 /*
862 * Drop the lock here since it might end up calling
863 * uart_start(), which takes the lock.
864 */
865 spin_unlock(&port->lock);
866 tty_flip_buffer_push(tport);
867 spin_lock(&port->lock);
868}
869
870static void atmel_complete_rx_dma(void *arg)
871{
872 struct uart_port *port = arg;
873 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
874
875 tasklet_schedule(&atmel_port->tasklet);
876}
877
878static void atmel_release_rx_dma(struct uart_port *port)
879{
880 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
881 struct dma_chan *chan = atmel_port->chan_rx;
882
883 if (chan) {
884 dmaengine_terminate_all(chan);
885 dma_release_channel(chan);
886 dma_unmap_sg(port->dev, &atmel_port->sg_rx, 1,
Wolfram Sang48479142014-07-21 11:42:04 +0200887 DMA_FROM_DEVICE);
Elen Song34df42f2013-07-22 16:30:27 +0800888 }
889
890 atmel_port->desc_rx = NULL;
891 atmel_port->chan_rx = NULL;
892 atmel_port->cookie_rx = -EINVAL;
893}
894
895static void atmel_rx_from_dma(struct uart_port *port)
896{
897 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
898 struct circ_buf *ring = &atmel_port->rx_ring;
899 struct dma_chan *chan = atmel_port->chan_rx;
900 struct dma_tx_state state;
901 enum dma_status dmastat;
902 size_t pending, count;
903
904
905 /* Reset the UART timeout early so that we don't miss one */
906 UART_PUT_CR(port, ATMEL_US_STTTO);
907 dmastat = dmaengine_tx_status(chan,
908 atmel_port->cookie_rx,
909 &state);
910 /* Restart a new tasklet if DMA status is error */
911 if (dmastat == DMA_ERROR) {
912 dev_dbg(port->dev, "Get residue error, restart tasklet\n");
913 UART_PUT_IER(port, ATMEL_US_TIMEOUT);
914 tasklet_schedule(&atmel_port->tasklet);
915 return;
916 }
917 /* current transfer size should no larger than dma buffer */
918 pending = sg_dma_len(&atmel_port->sg_rx) - state.residue;
919 BUG_ON(pending > sg_dma_len(&atmel_port->sg_rx));
920
921 /*
922 * This will take the chars we have so far,
923 * ring->head will record the transfer size, only new bytes come
924 * will insert into the framework.
925 */
926 if (pending > ring->head) {
927 count = pending - ring->head;
928
929 atmel_flip_buffer_rx_dma(port, ring->buf + ring->head, count);
930
931 ring->head += count;
932 if (ring->head == sg_dma_len(&atmel_port->sg_rx))
933 ring->head = 0;
934
935 port->icount.rx += count;
936 }
937
938 UART_PUT_IER(port, ATMEL_US_TIMEOUT);
939}
940
941static int atmel_prepare_rx_dma(struct uart_port *port)
942{
943 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
944 struct dma_async_tx_descriptor *desc;
945 dma_cap_mask_t mask;
946 struct dma_slave_config config;
947 struct circ_buf *ring;
948 int ret, nent;
949
950 ring = &atmel_port->rx_ring;
951
952 dma_cap_zero(mask);
953 dma_cap_set(DMA_CYCLIC, mask);
954
955 atmel_port->chan_rx = dma_request_slave_channel(port->dev, "rx");
956 if (atmel_port->chan_rx == NULL)
957 goto chan_err;
958 dev_info(port->dev, "using %s for rx DMA transfers\n",
959 dma_chan_name(atmel_port->chan_rx));
960
961 spin_lock_init(&atmel_port->lock_rx);
962 sg_init_table(&atmel_port->sg_rx, 1);
963 /* UART circular rx buffer is an aligned page. */
964 BUG_ON((int)port->state->xmit.buf & ~PAGE_MASK);
965 sg_set_page(&atmel_port->sg_rx,
966 virt_to_page(ring->buf),
967 ATMEL_SERIAL_RINGSIZE,
968 (int)ring->buf & ~PAGE_MASK);
969 nent = dma_map_sg(port->dev,
970 &atmel_port->sg_rx,
971 1,
Wolfram Sang48479142014-07-21 11:42:04 +0200972 DMA_FROM_DEVICE);
Elen Song34df42f2013-07-22 16:30:27 +0800973
974 if (!nent) {
975 dev_dbg(port->dev, "need to release resource of dma\n");
976 goto chan_err;
977 } else {
978 dev_dbg(port->dev, "%s: mapped %d@%p to %x\n", __func__,
979 sg_dma_len(&atmel_port->sg_rx),
980 ring->buf,
981 sg_dma_address(&atmel_port->sg_rx));
982 }
983
984 /* Configure the slave DMA */
985 memset(&config, 0, sizeof(config));
986 config.direction = DMA_DEV_TO_MEM;
987 config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
988 config.src_addr = port->mapbase + ATMEL_US_RHR;
989
990 ret = dmaengine_device_control(atmel_port->chan_rx,
991 DMA_SLAVE_CONFIG,
992 (unsigned long)&config);
993 if (ret) {
994 dev_err(port->dev, "DMA rx slave configuration failed\n");
995 goto chan_err;
996 }
997 /*
998 * Prepare a cyclic dma transfer, assign 2 descriptors,
999 * each one is half ring buffer size
1000 */
1001 desc = dmaengine_prep_dma_cyclic(atmel_port->chan_rx,
1002 sg_dma_address(&atmel_port->sg_rx),
1003 sg_dma_len(&atmel_port->sg_rx),
1004 sg_dma_len(&atmel_port->sg_rx)/2,
1005 DMA_DEV_TO_MEM,
1006 DMA_PREP_INTERRUPT);
1007 desc->callback = atmel_complete_rx_dma;
1008 desc->callback_param = port;
1009 atmel_port->desc_rx = desc;
1010 atmel_port->cookie_rx = dmaengine_submit(desc);
1011
1012 return 0;
1013
1014chan_err:
1015 dev_err(port->dev, "RX channel not available, switch to pio\n");
1016 atmel_port->use_dma_rx = 0;
1017 if (atmel_port->chan_rx)
1018 atmel_release_rx_dma(port);
1019 return -EINVAL;
1020}
1021
Elen Song2e68c222013-07-22 16:30:30 +08001022static void atmel_uart_timer_callback(unsigned long data)
1023{
1024 struct uart_port *port = (void *)data;
1025 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1026
1027 tasklet_schedule(&atmel_port->tasklet);
1028 mod_timer(&atmel_port->uart_timer, jiffies + uart_poll_timeout(port));
1029}
1030
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001031/*
Remy Bohmerb843aa22008-02-08 04:21:01 -08001032 * receive interrupt handler.
1033 */
1034static void
1035atmel_handle_receive(struct uart_port *port, unsigned int pending)
1036{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001037 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Remy Bohmerb843aa22008-02-08 04:21:01 -08001038
Elen Song64e22eb2013-07-22 16:30:24 +08001039 if (atmel_use_pdc_rx(port)) {
Chip Coldwella6670612008-02-08 04:21:06 -08001040 /*
1041 * PDC receive. Just schedule the tasklet and let it
1042 * figure out the details.
1043 *
1044 * TODO: We're not handling error flags correctly at
1045 * the moment.
1046 */
1047 if (pending & (ATMEL_US_ENDRX | ATMEL_US_TIMEOUT)) {
1048 UART_PUT_IDR(port, (ATMEL_US_ENDRX
1049 | ATMEL_US_TIMEOUT));
1050 tasklet_schedule(&atmel_port->tasklet);
1051 }
1052
1053 if (pending & (ATMEL_US_RXBRK | ATMEL_US_OVRE |
1054 ATMEL_US_FRAME | ATMEL_US_PARE))
1055 atmel_pdc_rxerr(port, pending);
1056 }
1057
Elen Song34df42f2013-07-22 16:30:27 +08001058 if (atmel_use_dma_rx(port)) {
1059 if (pending & ATMEL_US_TIMEOUT) {
1060 UART_PUT_IDR(port, ATMEL_US_TIMEOUT);
1061 tasklet_schedule(&atmel_port->tasklet);
1062 }
1063 }
1064
Remy Bohmerb843aa22008-02-08 04:21:01 -08001065 /* Interrupt receive */
1066 if (pending & ATMEL_US_RXRDY)
1067 atmel_rx_chars(port);
1068 else if (pending & ATMEL_US_RXBRK) {
1069 /*
1070 * End of break detected. If it came along with a
1071 * character, atmel_rx_chars will handle it.
1072 */
1073 UART_PUT_CR(port, ATMEL_US_RSTSTA);
1074 UART_PUT_IDR(port, ATMEL_US_RXBRK);
1075 atmel_port->break_active = 0;
1076 }
1077}
1078
1079/*
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001080 * transmit interrupt handler. (Transmit is IRQF_NODELAY safe)
Remy Bohmerb843aa22008-02-08 04:21:01 -08001081 */
1082static void
1083atmel_handle_transmit(struct uart_port *port, unsigned int pending)
1084{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001085 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001086
Claudio Scordinoe8faff72010-05-03 13:31:28 +01001087 if (pending & atmel_port->tx_done_mask) {
1088 /* Either PDC or interrupt transmission */
1089 UART_PUT_IDR(port, atmel_port->tx_done_mask);
1090 tasklet_schedule(&atmel_port->tasklet);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001091 }
Remy Bohmerb843aa22008-02-08 04:21:01 -08001092}
1093
1094/*
1095 * status flags interrupt handler.
1096 */
1097static void
1098atmel_handle_status(struct uart_port *port, unsigned int pending,
1099 unsigned int status)
1100{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001101 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001102
Remy Bohmerb843aa22008-02-08 04:21:01 -08001103 if (pending & (ATMEL_US_RIIC | ATMEL_US_DSRIC | ATMEL_US_DCDIC
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001104 | ATMEL_US_CTSIC)) {
1105 atmel_port->irq_status = status;
1106 tasklet_schedule(&atmel_port->tasklet);
1107 }
Remy Bohmerb843aa22008-02-08 04:21:01 -08001108}
1109
1110/*
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001111 * Interrupt handler
1112 */
David Howells7d12e782006-10-05 14:55:46 +01001113static irqreturn_t atmel_interrupt(int irq, void *dev_id)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001114{
1115 struct uart_port *port = dev_id;
Richard Genoudab5e4e42014-05-13 20:20:45 +02001116 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001117 unsigned int status, pending, pass_counter = 0;
Richard Genoudab5e4e42014-05-13 20:20:45 +02001118 bool gpio_handled = false;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001119
Chip Coldwella6670612008-02-08 04:21:06 -08001120 do {
Richard Genoude0b0baa2014-05-13 20:20:44 +02001121 status = atmel_get_lines_status(port);
Chip Coldwella6670612008-02-08 04:21:06 -08001122 pending = status & UART_GET_IMR(port);
Richard Genoudab5e4e42014-05-13 20:20:45 +02001123 if (!gpio_handled) {
1124 /*
1125 * Dealing with GPIO interrupt
1126 */
1127 if (irq == atmel_port->gpio_irq[UART_GPIO_CTS])
1128 pending |= ATMEL_US_CTSIC;
1129
1130 if (irq == atmel_port->gpio_irq[UART_GPIO_DSR])
1131 pending |= ATMEL_US_DSRIC;
1132
1133 if (irq == atmel_port->gpio_irq[UART_GPIO_RI])
1134 pending |= ATMEL_US_RIIC;
1135
1136 if (irq == atmel_port->gpio_irq[UART_GPIO_DCD])
1137 pending |= ATMEL_US_DCDIC;
1138
1139 gpio_handled = true;
1140 }
Chip Coldwella6670612008-02-08 04:21:06 -08001141 if (!pending)
1142 break;
1143
Remy Bohmerb843aa22008-02-08 04:21:01 -08001144 atmel_handle_receive(port, pending);
1145 atmel_handle_status(port, pending, status);
1146 atmel_handle_transmit(port, pending);
Chip Coldwella6670612008-02-08 04:21:06 -08001147 } while (pass_counter++ < ATMEL_ISR_PASS_LIMIT);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001148
Haavard Skinnemoen0400b692008-02-23 15:23:36 -08001149 return pass_counter ? IRQ_HANDLED : IRQ_NONE;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001150}
1151
Elen Songa930e522013-07-22 16:30:25 +08001152static void atmel_release_tx_pdc(struct uart_port *port)
1153{
1154 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1155 struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1156
1157 dma_unmap_single(port->dev,
1158 pdc->dma_addr,
1159 pdc->dma_size,
1160 DMA_TO_DEVICE);
1161}
1162
Chip Coldwella6670612008-02-08 04:21:06 -08001163/*
1164 * Called from tasklet with ENDTX and TXBUFE interrupts disabled.
1165 */
Elen Song64e22eb2013-07-22 16:30:24 +08001166static void atmel_tx_pdc(struct uart_port *port)
Chip Coldwella6670612008-02-08 04:21:06 -08001167{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001168 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Alan Coxebd2c8f2009-09-19 13:13:28 -07001169 struct circ_buf *xmit = &port->state->xmit;
Chip Coldwella6670612008-02-08 04:21:06 -08001170 struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1171 int count;
1172
Michael Trimarchiba0657f2008-04-02 13:04:41 -07001173 /* nothing left to transmit? */
1174 if (UART_GET_TCR(port))
1175 return;
1176
Chip Coldwella6670612008-02-08 04:21:06 -08001177 xmit->tail += pdc->ofs;
1178 xmit->tail &= UART_XMIT_SIZE - 1;
1179
1180 port->icount.tx += pdc->ofs;
1181 pdc->ofs = 0;
1182
Michael Trimarchiba0657f2008-04-02 13:04:41 -07001183 /* more to transmit - setup next transfer */
Chip Coldwella6670612008-02-08 04:21:06 -08001184
Michael Trimarchiba0657f2008-04-02 13:04:41 -07001185 /* disable PDC transmit */
1186 UART_PUT_PTCR(port, ATMEL_PDC_TXTDIS);
1187
Itai Levi1f140812009-01-15 13:50:43 -08001188 if (!uart_circ_empty(xmit) && !uart_tx_stopped(port)) {
Chip Coldwella6670612008-02-08 04:21:06 -08001189 dma_sync_single_for_device(port->dev,
1190 pdc->dma_addr,
1191 pdc->dma_size,
1192 DMA_TO_DEVICE);
1193
1194 count = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
1195 pdc->ofs = count;
1196
1197 UART_PUT_TPR(port, pdc->dma_addr + xmit->tail);
1198 UART_PUT_TCR(port, count);
Claudio Scordinoe8faff72010-05-03 13:31:28 +01001199 /* re-enable PDC transmit */
Chip Coldwella6670612008-02-08 04:21:06 -08001200 UART_PUT_PTCR(port, ATMEL_PDC_TXTEN);
Claudio Scordinoe8faff72010-05-03 13:31:28 +01001201 /* Enable interrupts */
1202 UART_PUT_IER(port, atmel_port->tx_done_mask);
1203 } else {
Bernhard Roth83cac9f2011-08-24 09:48:23 +02001204 if ((atmel_port->rs485.flags & SER_RS485_ENABLED) &&
1205 !(atmel_port->rs485.flags & SER_RS485_RX_DURING_TX)) {
Claudio Scordinoe8faff72010-05-03 13:31:28 +01001206 /* DMA done, stop TX, start RX for RS485 */
1207 atmel_start_rx(port);
1208 }
Chip Coldwella6670612008-02-08 04:21:06 -08001209 }
1210
1211 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1212 uart_write_wakeup(port);
1213}
1214
Elen Songa930e522013-07-22 16:30:25 +08001215static int atmel_prepare_tx_pdc(struct uart_port *port)
1216{
1217 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1218 struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1219 struct circ_buf *xmit = &port->state->xmit;
1220
1221 pdc->buf = xmit->buf;
1222 pdc->dma_addr = dma_map_single(port->dev,
1223 pdc->buf,
1224 UART_XMIT_SIZE,
1225 DMA_TO_DEVICE);
1226 pdc->dma_size = UART_XMIT_SIZE;
1227 pdc->ofs = 0;
1228
1229 return 0;
1230}
1231
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001232static void atmel_rx_from_ring(struct uart_port *port)
1233{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001234 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001235 struct circ_buf *ring = &atmel_port->rx_ring;
1236 unsigned int flg;
1237 unsigned int status;
1238
1239 while (ring->head != ring->tail) {
1240 struct atmel_uart_char c;
1241
1242 /* Make sure c is loaded after head. */
1243 smp_rmb();
1244
1245 c = ((struct atmel_uart_char *)ring->buf)[ring->tail];
1246
1247 ring->tail = (ring->tail + 1) & (ATMEL_SERIAL_RINGSIZE - 1);
1248
1249 port->icount.rx++;
1250 status = c.status;
1251 flg = TTY_NORMAL;
1252
1253 /*
1254 * note that the error handling code is
1255 * out of the main execution path
1256 */
1257 if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME
1258 | ATMEL_US_OVRE | ATMEL_US_RXBRK))) {
1259 if (status & ATMEL_US_RXBRK) {
1260 /* ignore side-effect */
1261 status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME);
1262
1263 port->icount.brk++;
1264 if (uart_handle_break(port))
1265 continue;
1266 }
1267 if (status & ATMEL_US_PARE)
1268 port->icount.parity++;
1269 if (status & ATMEL_US_FRAME)
1270 port->icount.frame++;
1271 if (status & ATMEL_US_OVRE)
1272 port->icount.overrun++;
1273
1274 status &= port->read_status_mask;
1275
1276 if (status & ATMEL_US_RXBRK)
1277 flg = TTY_BREAK;
1278 else if (status & ATMEL_US_PARE)
1279 flg = TTY_PARITY;
1280 else if (status & ATMEL_US_FRAME)
1281 flg = TTY_FRAME;
1282 }
1283
1284
1285 if (uart_handle_sysrq_char(port, c.ch))
1286 continue;
1287
1288 uart_insert_char(port, status, ATMEL_US_OVRE, c.ch, flg);
1289 }
1290
1291 /*
1292 * Drop the lock here since it might end up calling
1293 * uart_start(), which takes the lock.
1294 */
1295 spin_unlock(&port->lock);
Jiri Slaby2e124b42013-01-03 15:53:06 +01001296 tty_flip_buffer_push(&port->state->port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001297 spin_lock(&port->lock);
1298}
1299
Elen Songa930e522013-07-22 16:30:25 +08001300static void atmel_release_rx_pdc(struct uart_port *port)
1301{
1302 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1303 int i;
1304
1305 for (i = 0; i < 2; i++) {
1306 struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i];
1307
1308 dma_unmap_single(port->dev,
1309 pdc->dma_addr,
1310 pdc->dma_size,
1311 DMA_FROM_DEVICE);
1312 kfree(pdc->buf);
1313 }
1314}
1315
Elen Song64e22eb2013-07-22 16:30:24 +08001316static void atmel_rx_from_pdc(struct uart_port *port)
Chip Coldwella6670612008-02-08 04:21:06 -08001317{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001318 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Jiri Slaby05c7cd32013-01-03 15:53:04 +01001319 struct tty_port *tport = &port->state->port;
Chip Coldwella6670612008-02-08 04:21:06 -08001320 struct atmel_dma_buffer *pdc;
1321 int rx_idx = atmel_port->pdc_rx_idx;
1322 unsigned int head;
1323 unsigned int tail;
1324 unsigned int count;
1325
1326 do {
1327 /* Reset the UART timeout early so that we don't miss one */
1328 UART_PUT_CR(port, ATMEL_US_STTTO);
1329
1330 pdc = &atmel_port->pdc_rx[rx_idx];
1331 head = UART_GET_RPR(port) - pdc->dma_addr;
1332 tail = pdc->ofs;
1333
1334 /* If the PDC has switched buffers, RPR won't contain
1335 * any address within the current buffer. Since head
1336 * is unsigned, we just need a one-way comparison to
1337 * find out.
1338 *
1339 * In this case, we just need to consume the entire
1340 * buffer and resubmit it for DMA. This will clear the
1341 * ENDRX bit as well, so that we can safely re-enable
1342 * all interrupts below.
1343 */
1344 head = min(head, pdc->dma_size);
1345
1346 if (likely(head != tail)) {
1347 dma_sync_single_for_cpu(port->dev, pdc->dma_addr,
1348 pdc->dma_size, DMA_FROM_DEVICE);
1349
1350 /*
1351 * head will only wrap around when we recycle
1352 * the DMA buffer, and when that happens, we
1353 * explicitly set tail to 0. So head will
1354 * always be greater than tail.
1355 */
1356 count = head - tail;
1357
Jiri Slaby05c7cd32013-01-03 15:53:04 +01001358 tty_insert_flip_string(tport, pdc->buf + pdc->ofs,
1359 count);
Chip Coldwella6670612008-02-08 04:21:06 -08001360
1361 dma_sync_single_for_device(port->dev, pdc->dma_addr,
1362 pdc->dma_size, DMA_FROM_DEVICE);
1363
1364 port->icount.rx += count;
1365 pdc->ofs = head;
1366 }
1367
1368 /*
1369 * If the current buffer is full, we need to check if
1370 * the next one contains any additional data.
1371 */
1372 if (head >= pdc->dma_size) {
1373 pdc->ofs = 0;
1374 UART_PUT_RNPR(port, pdc->dma_addr);
1375 UART_PUT_RNCR(port, pdc->dma_size);
1376
1377 rx_idx = !rx_idx;
1378 atmel_port->pdc_rx_idx = rx_idx;
1379 }
1380 } while (head >= pdc->dma_size);
1381
1382 /*
1383 * Drop the lock here since it might end up calling
1384 * uart_start(), which takes the lock.
1385 */
1386 spin_unlock(&port->lock);
Jiri Slaby2e124b42013-01-03 15:53:06 +01001387 tty_flip_buffer_push(tport);
Chip Coldwella6670612008-02-08 04:21:06 -08001388 spin_lock(&port->lock);
1389
1390 UART_PUT_IER(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT);
1391}
1392
Elen Songa930e522013-07-22 16:30:25 +08001393static int atmel_prepare_rx_pdc(struct uart_port *port)
1394{
1395 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1396 int i;
1397
1398 for (i = 0; i < 2; i++) {
1399 struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i];
1400
1401 pdc->buf = kmalloc(PDC_BUFFER_SIZE, GFP_KERNEL);
1402 if (pdc->buf == NULL) {
1403 if (i != 0) {
1404 dma_unmap_single(port->dev,
1405 atmel_port->pdc_rx[0].dma_addr,
1406 PDC_BUFFER_SIZE,
1407 DMA_FROM_DEVICE);
1408 kfree(atmel_port->pdc_rx[0].buf);
1409 }
1410 atmel_port->use_pdc_rx = 0;
1411 return -ENOMEM;
1412 }
1413 pdc->dma_addr = dma_map_single(port->dev,
1414 pdc->buf,
1415 PDC_BUFFER_SIZE,
1416 DMA_FROM_DEVICE);
1417 pdc->dma_size = PDC_BUFFER_SIZE;
1418 pdc->ofs = 0;
1419 }
1420
1421 atmel_port->pdc_rx_idx = 0;
1422
1423 UART_PUT_RPR(port, atmel_port->pdc_rx[0].dma_addr);
1424 UART_PUT_RCR(port, PDC_BUFFER_SIZE);
1425
1426 UART_PUT_RNPR(port, atmel_port->pdc_rx[1].dma_addr);
1427 UART_PUT_RNCR(port, PDC_BUFFER_SIZE);
1428
1429 return 0;
1430}
1431
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001432/*
1433 * tasklet handling tty stuff outside the interrupt handler.
1434 */
1435static void atmel_tasklet_func(unsigned long data)
1436{
1437 struct uart_port *port = (struct uart_port *)data;
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001438 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001439 unsigned int status;
1440 unsigned int status_change;
1441
1442 /* The interrupt handler does not take the lock */
1443 spin_lock(&port->lock);
1444
Elen Songa930e522013-07-22 16:30:25 +08001445 atmel_port->schedule_tx(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001446
1447 status = atmel_port->irq_status;
1448 status_change = status ^ atmel_port->irq_status_prev;
1449
1450 if (status_change & (ATMEL_US_RI | ATMEL_US_DSR
1451 | ATMEL_US_DCD | ATMEL_US_CTS)) {
1452 /* TODO: All reads to CSR will clear these interrupts! */
1453 if (status_change & ATMEL_US_RI)
1454 port->icount.rng++;
1455 if (status_change & ATMEL_US_DSR)
1456 port->icount.dsr++;
1457 if (status_change & ATMEL_US_DCD)
1458 uart_handle_dcd_change(port, !(status & ATMEL_US_DCD));
1459 if (status_change & ATMEL_US_CTS)
1460 uart_handle_cts_change(port, !(status & ATMEL_US_CTS));
1461
Alan Coxbdc04e32009-09-19 13:13:31 -07001462 wake_up_interruptible(&port->state->port.delta_msr_wait);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001463
1464 atmel_port->irq_status_prev = status;
1465 }
1466
Elen Songa930e522013-07-22 16:30:25 +08001467 atmel_port->schedule_rx(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001468
1469 spin_unlock(&port->lock);
1470}
1471
Elen Song33d64c42013-07-22 16:30:28 +08001472static int atmel_init_property(struct atmel_uart_port *atmel_port,
1473 struct platform_device *pdev)
1474{
1475 struct device_node *np = pdev->dev.of_node;
Jingoo Han574de552013-07-30 17:06:57 +09001476 struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
Elen Song33d64c42013-07-22 16:30:28 +08001477
1478 if (np) {
1479 /* DMA/PDC usage specification */
1480 if (of_get_property(np, "atmel,use-dma-rx", NULL)) {
1481 if (of_get_property(np, "dmas", NULL)) {
1482 atmel_port->use_dma_rx = true;
1483 atmel_port->use_pdc_rx = false;
1484 } else {
1485 atmel_port->use_dma_rx = false;
1486 atmel_port->use_pdc_rx = true;
1487 }
1488 } else {
1489 atmel_port->use_dma_rx = false;
1490 atmel_port->use_pdc_rx = false;
1491 }
1492
1493 if (of_get_property(np, "atmel,use-dma-tx", NULL)) {
1494 if (of_get_property(np, "dmas", NULL)) {
1495 atmel_port->use_dma_tx = true;
1496 atmel_port->use_pdc_tx = false;
1497 } else {
1498 atmel_port->use_dma_tx = false;
1499 atmel_port->use_pdc_tx = true;
1500 }
1501 } else {
1502 atmel_port->use_dma_tx = false;
1503 atmel_port->use_pdc_tx = false;
1504 }
1505
1506 } else {
1507 atmel_port->use_pdc_rx = pdata->use_dma_rx;
1508 atmel_port->use_pdc_tx = pdata->use_dma_tx;
1509 atmel_port->use_dma_rx = false;
1510 atmel_port->use_dma_tx = false;
1511 }
1512
1513 return 0;
1514}
1515
1516static void atmel_init_rs485(struct atmel_uart_port *atmel_port,
1517 struct platform_device *pdev)
1518{
1519 struct device_node *np = pdev->dev.of_node;
Jingoo Han574de552013-07-30 17:06:57 +09001520 struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
Elen Song33d64c42013-07-22 16:30:28 +08001521
1522 if (np) {
1523 u32 rs485_delay[2];
1524 /* rs485 properties */
1525 if (of_property_read_u32_array(np, "rs485-rts-delay",
1526 rs485_delay, 2) == 0) {
1527 struct serial_rs485 *rs485conf = &atmel_port->rs485;
1528
1529 rs485conf->delay_rts_before_send = rs485_delay[0];
1530 rs485conf->delay_rts_after_send = rs485_delay[1];
1531 rs485conf->flags = 0;
1532
1533 if (of_get_property(np, "rs485-rx-during-tx", NULL))
1534 rs485conf->flags |= SER_RS485_RX_DURING_TX;
1535
1536 if (of_get_property(np, "linux,rs485-enabled-at-boot-time",
1537 NULL))
1538 rs485conf->flags |= SER_RS485_ENABLED;
1539 }
1540 } else {
1541 atmel_port->rs485 = pdata->rs485;
1542 }
1543
1544}
1545
Elen Songa930e522013-07-22 16:30:25 +08001546static void atmel_set_ops(struct uart_port *port)
1547{
1548 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1549
Elen Song34df42f2013-07-22 16:30:27 +08001550 if (atmel_use_dma_rx(port)) {
1551 atmel_port->prepare_rx = &atmel_prepare_rx_dma;
1552 atmel_port->schedule_rx = &atmel_rx_from_dma;
1553 atmel_port->release_rx = &atmel_release_rx_dma;
1554 } else if (atmel_use_pdc_rx(port)) {
Elen Songa930e522013-07-22 16:30:25 +08001555 atmel_port->prepare_rx = &atmel_prepare_rx_pdc;
1556 atmel_port->schedule_rx = &atmel_rx_from_pdc;
1557 atmel_port->release_rx = &atmel_release_rx_pdc;
1558 } else {
1559 atmel_port->prepare_rx = NULL;
1560 atmel_port->schedule_rx = &atmel_rx_from_ring;
1561 atmel_port->release_rx = NULL;
1562 }
1563
Elen Song08f738b2013-07-22 16:30:26 +08001564 if (atmel_use_dma_tx(port)) {
1565 atmel_port->prepare_tx = &atmel_prepare_tx_dma;
1566 atmel_port->schedule_tx = &atmel_tx_dma;
1567 atmel_port->release_tx = &atmel_release_tx_dma;
1568 } else if (atmel_use_pdc_tx(port)) {
Elen Songa930e522013-07-22 16:30:25 +08001569 atmel_port->prepare_tx = &atmel_prepare_tx_pdc;
1570 atmel_port->schedule_tx = &atmel_tx_pdc;
1571 atmel_port->release_tx = &atmel_release_tx_pdc;
1572 } else {
1573 atmel_port->prepare_tx = NULL;
1574 atmel_port->schedule_tx = &atmel_tx_chars;
1575 atmel_port->release_tx = NULL;
1576 }
1577}
1578
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001579/*
Elen Song055560b2013-07-22 16:30:29 +08001580 * Get ip name usart or uart
1581 */
Nicolas Ferre892db582013-10-17 17:37:11 +02001582static void atmel_get_ip_name(struct uart_port *port)
Elen Song055560b2013-07-22 16:30:29 +08001583{
1584 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1585 int name = UART_GET_IP_NAME(port);
Nicolas Ferre731d9ca2013-10-17 17:37:12 +02001586 u32 version;
Elen Song055560b2013-07-22 16:30:29 +08001587 int usart, uart;
1588 /* usart and uart ascii */
1589 usart = 0x55534152;
1590 uart = 0x44424755;
1591
1592 atmel_port->is_usart = false;
1593
1594 if (name == usart) {
1595 dev_dbg(port->dev, "This is usart\n");
1596 atmel_port->is_usart = true;
1597 } else if (name == uart) {
1598 dev_dbg(port->dev, "This is uart\n");
1599 atmel_port->is_usart = false;
1600 } else {
Nicolas Ferre731d9ca2013-10-17 17:37:12 +02001601 /* fallback for older SoCs: use version field */
1602 version = UART_GET_IP_VERSION(port);
1603 switch (version) {
1604 case 0x302:
1605 case 0x10213:
1606 dev_dbg(port->dev, "This version is usart\n");
1607 atmel_port->is_usart = true;
1608 break;
1609 case 0x203:
1610 case 0x10202:
1611 dev_dbg(port->dev, "This version is uart\n");
1612 atmel_port->is_usart = false;
1613 break;
1614 default:
1615 dev_err(port->dev, "Not supported ip name nor version, set to uart\n");
1616 }
Elen Song055560b2013-07-22 16:30:29 +08001617 }
Elen Song055560b2013-07-22 16:30:29 +08001618}
1619
Richard Genoudab5e4e42014-05-13 20:20:45 +02001620static void atmel_free_gpio_irq(struct uart_port *port)
1621{
1622 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1623 enum mctrl_gpio_idx i;
1624
1625 for (i = 0; i < UART_GPIO_MAX; i++)
1626 if (atmel_port->gpio_irq[i] >= 0)
1627 free_irq(atmel_port->gpio_irq[i], port);
1628}
1629
1630static int atmel_request_gpio_irq(struct uart_port *port)
1631{
1632 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1633 int *irq = atmel_port->gpio_irq;
1634 enum mctrl_gpio_idx i;
1635 int err = 0;
1636
1637 for (i = 0; (i < UART_GPIO_MAX) && !err; i++) {
1638 if (irq[i] < 0)
1639 continue;
1640
1641 irq_set_status_flags(irq[i], IRQ_NOAUTOEN);
1642 err = request_irq(irq[i], atmel_interrupt, IRQ_TYPE_EDGE_BOTH,
1643 "atmel_serial", port);
1644 if (err)
1645 dev_err(port->dev, "atmel_startup - Can't get %d irq\n",
1646 irq[i]);
1647 }
1648
1649 /*
1650 * If something went wrong, rollback.
1651 */
1652 while (err && (--i >= 0))
1653 if (irq[i] >= 0)
1654 free_irq(irq[i], port);
1655
1656 return err;
1657}
1658
Elen Song055560b2013-07-22 16:30:29 +08001659/*
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001660 * Perform initialization and enable port for reception
1661 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001662static int atmel_startup(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001663{
Elen Song33d64c42013-07-22 16:30:28 +08001664 struct platform_device *pdev = to_platform_device(port->dev);
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001665 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Alan Coxebd2c8f2009-09-19 13:13:28 -07001666 struct tty_struct *tty = port->state->port.tty;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001667 int retval;
1668
1669 /*
1670 * Ensure that no interrupts are enabled otherwise when
1671 * request_irq() is called we could get stuck trying to
1672 * handle an unexpected interrupt
1673 */
1674 UART_PUT_IDR(port, -1);
Richard Genoudab5e4e42014-05-13 20:20:45 +02001675 atmel_port->ms_irq_enabled = false;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001676
1677 /*
1678 * Allocate the IRQ
1679 */
Remy Bohmerb843aa22008-02-08 04:21:01 -08001680 retval = request_irq(port->irq, atmel_interrupt, IRQF_SHARED,
Haavard Skinnemoenae161062008-02-08 04:21:08 -08001681 tty ? tty->name : "atmel_serial", port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001682 if (retval) {
Richard Genoudddaa6032014-02-26 17:19:45 +01001683 dev_err(port->dev, "atmel_startup - Can't get irq\n");
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001684 return retval;
1685 }
1686
1687 /*
Richard Genoudab5e4e42014-05-13 20:20:45 +02001688 * Get the GPIO lines IRQ
1689 */
1690 retval = atmel_request_gpio_irq(port);
1691 if (retval)
1692 goto free_irq;
1693
1694 /*
Chip Coldwella6670612008-02-08 04:21:06 -08001695 * Initialize DMA (if necessary)
1696 */
Elen Song33d64c42013-07-22 16:30:28 +08001697 atmel_init_property(atmel_port, pdev);
1698
Elen Songa930e522013-07-22 16:30:25 +08001699 if (atmel_port->prepare_rx) {
1700 retval = atmel_port->prepare_rx(port);
1701 if (retval < 0)
1702 atmel_set_ops(port);
Chip Coldwella6670612008-02-08 04:21:06 -08001703 }
1704
Elen Songa930e522013-07-22 16:30:25 +08001705 if (atmel_port->prepare_tx) {
1706 retval = atmel_port->prepare_tx(port);
1707 if (retval < 0)
1708 atmel_set_ops(port);
1709 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001710
Atsushi Nemoto27c0c8e2009-02-18 14:48:28 -08001711 /* Save current CSR for comparison in atmel_tasklet_func() */
Richard Genoude0b0baa2014-05-13 20:20:44 +02001712 atmel_port->irq_status_prev = atmel_get_lines_status(port);
Atsushi Nemoto27c0c8e2009-02-18 14:48:28 -08001713 atmel_port->irq_status = atmel_port->irq_status_prev;
1714
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001715 /*
1716 * Finally, enable the serial port
1717 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001718 UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
Remy Bohmerb843aa22008-02-08 04:21:01 -08001719 /* enable xmit & rcvr */
1720 UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN);
Andrew Victorafefc412006-06-19 19:53:19 +01001721
Marek Roszko8bc661b2014-01-10 10:33:11 +01001722 setup_timer(&atmel_port->uart_timer,
1723 atmel_uart_timer_callback,
1724 (unsigned long)port);
1725
Elen Song64e22eb2013-07-22 16:30:24 +08001726 if (atmel_use_pdc_rx(port)) {
Chip Coldwella6670612008-02-08 04:21:06 -08001727 /* set UART timeout */
Elen Song2e68c222013-07-22 16:30:30 +08001728 if (!atmel_port->is_usart) {
Elen Song2e68c222013-07-22 16:30:30 +08001729 mod_timer(&atmel_port->uart_timer,
1730 jiffies + uart_poll_timeout(port));
1731 /* set USART timeout */
1732 } else {
1733 UART_PUT_RTOR(port, PDC_RX_TIMEOUT);
1734 UART_PUT_CR(port, ATMEL_US_STTTO);
Chip Coldwella6670612008-02-08 04:21:06 -08001735
Elen Song2e68c222013-07-22 16:30:30 +08001736 UART_PUT_IER(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT);
1737 }
Chip Coldwella6670612008-02-08 04:21:06 -08001738 /* enable PDC controller */
1739 UART_PUT_PTCR(port, ATMEL_PDC_RXTEN);
Elen Song34df42f2013-07-22 16:30:27 +08001740 } else if (atmel_use_dma_rx(port)) {
Elen Song2e68c222013-07-22 16:30:30 +08001741 /* set UART timeout */
1742 if (!atmel_port->is_usart) {
Elen Song2e68c222013-07-22 16:30:30 +08001743 mod_timer(&atmel_port->uart_timer,
1744 jiffies + uart_poll_timeout(port));
1745 /* set USART timeout */
1746 } else {
1747 UART_PUT_RTOR(port, PDC_RX_TIMEOUT);
1748 UART_PUT_CR(port, ATMEL_US_STTTO);
Elen Song34df42f2013-07-22 16:30:27 +08001749
Elen Song2e68c222013-07-22 16:30:30 +08001750 UART_PUT_IER(port, ATMEL_US_TIMEOUT);
1751 }
Chip Coldwella6670612008-02-08 04:21:06 -08001752 } else {
1753 /* enable receive only */
1754 UART_PUT_IER(port, ATMEL_US_RXRDY);
1755 }
Andrew Victorafefc412006-06-19 19:53:19 +01001756
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001757 return 0;
Richard Genoudab5e4e42014-05-13 20:20:45 +02001758
1759free_irq:
1760 free_irq(port->irq, port);
1761
1762 return retval;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001763}
1764
1765/*
1766 * Disable the port
1767 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001768static void atmel_shutdown(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001769{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001770 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Marek Roszko0cc7c6c2014-01-07 11:45:06 +01001771
Chip Coldwella6670612008-02-08 04:21:06 -08001772 /*
Marek Roszko8bc661b2014-01-10 10:33:11 +01001773 * Prevent any tasklets being scheduled during
1774 * cleanup
1775 */
1776 del_timer_sync(&atmel_port->uart_timer);
1777
1778 /*
Marek Roszko0cc7c6c2014-01-07 11:45:06 +01001779 * Clear out any scheduled tasklets before
1780 * we destroy the buffers
1781 */
1782 tasklet_kill(&atmel_port->tasklet);
1783
1784 /*
1785 * Ensure everything is stopped and
1786 * disable all interrupts, port and break condition.
Chip Coldwella6670612008-02-08 04:21:06 -08001787 */
1788 atmel_stop_rx(port);
1789 atmel_stop_tx(port);
1790
Marek Roszko0cc7c6c2014-01-07 11:45:06 +01001791 UART_PUT_CR(port, ATMEL_US_RSTSTA);
1792 UART_PUT_IDR(port, -1);
1793
1794
Chip Coldwella6670612008-02-08 04:21:06 -08001795 /*
1796 * Shut-down the DMA.
1797 */
Elen Songa930e522013-07-22 16:30:25 +08001798 if (atmel_port->release_rx)
1799 atmel_port->release_rx(port);
1800 if (atmel_port->release_tx)
1801 atmel_port->release_tx(port);
Chip Coldwella6670612008-02-08 04:21:06 -08001802
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001803 /*
Mark Deneenbb7e73c2014-01-07 11:45:09 +01001804 * Reset ring buffer pointers
1805 */
1806 atmel_port->rx_ring.head = 0;
1807 atmel_port->rx_ring.tail = 0;
1808
1809 /*
Richard Genoudab5e4e42014-05-13 20:20:45 +02001810 * Free the interrupts
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001811 */
1812 free_irq(port->irq, port);
Richard Genoudab5e4e42014-05-13 20:20:45 +02001813 atmel_free_gpio_irq(port);
1814
1815 atmel_port->ms_irq_enabled = false;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001816}
1817
1818/*
Haavard Skinnemoen9afd5612008-07-16 21:52:46 +01001819 * Flush any TX data submitted for DMA. Called when the TX circular
1820 * buffer is reset.
1821 */
1822static void atmel_flush_buffer(struct uart_port *port)
1823{
1824 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1825
Elen Song64e22eb2013-07-22 16:30:24 +08001826 if (atmel_use_pdc_tx(port)) {
Haavard Skinnemoen9afd5612008-07-16 21:52:46 +01001827 UART_PUT_TCR(port, 0);
1828 atmel_port->pdc_tx.ofs = 0;
1829 }
1830}
1831
1832/*
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001833 * Power / Clock management.
1834 */
Remy Bohmerb843aa22008-02-08 04:21:01 -08001835static void atmel_serial_pm(struct uart_port *port, unsigned int state,
1836 unsigned int oldstate)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001837{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001838 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Andrew Victorafefc412006-06-19 19:53:19 +01001839
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001840 switch (state) {
Remy Bohmerb843aa22008-02-08 04:21:01 -08001841 case 0:
1842 /*
1843 * Enable the peripheral clock for this serial port.
1844 * This is called on uart_open() or a resume event.
1845 */
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02001846 clk_prepare_enable(atmel_port->clk);
Anti Sullinf05596d2008-09-22 13:57:54 -07001847
1848 /* re-enable interrupts if we disabled some on suspend */
1849 UART_PUT_IER(port, atmel_port->backup_imr);
Remy Bohmerb843aa22008-02-08 04:21:01 -08001850 break;
1851 case 3:
Anti Sullinf05596d2008-09-22 13:57:54 -07001852 /* Back up the interrupt mask and disable all interrupts */
1853 atmel_port->backup_imr = UART_GET_IMR(port);
1854 UART_PUT_IDR(port, -1);
1855
Remy Bohmerb843aa22008-02-08 04:21:01 -08001856 /*
1857 * Disable the peripheral clock for this serial port.
1858 * This is called on uart_close() or a suspend event.
1859 */
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02001860 clk_disable_unprepare(atmel_port->clk);
Remy Bohmerb843aa22008-02-08 04:21:01 -08001861 break;
1862 default:
Richard Genoudddaa6032014-02-26 17:19:45 +01001863 dev_err(port->dev, "atmel_serial: unknown pm %d\n", state);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001864 }
1865}
1866
1867/*
1868 * Change the port parameters
1869 */
Remy Bohmerb843aa22008-02-08 04:21:01 -08001870static void atmel_set_termios(struct uart_port *port, struct ktermios *termios,
1871 struct ktermios *old)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001872{
1873 unsigned long flags;
1874 unsigned int mode, imr, quot, baud;
Claudio Scordinoe8faff72010-05-03 13:31:28 +01001875 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001876
Andrew Victor03abeac2007-05-03 12:26:24 +01001877 /* Get current mode register */
Remy Bohmerb843aa22008-02-08 04:21:01 -08001878 mode = UART_GET_MR(port) & ~(ATMEL_US_USCLKS | ATMEL_US_CHRL
Peter Ma8e706c42009-03-31 10:31:02 -07001879 | ATMEL_US_NBSTOP | ATMEL_US_PAR
1880 | ATMEL_US_USMODE);
Andrew Victor03abeac2007-05-03 12:26:24 +01001881
Remy Bohmerb843aa22008-02-08 04:21:01 -08001882 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001883 quot = uart_get_divisor(port, baud);
1884
Remy Bohmerb843aa22008-02-08 04:21:01 -08001885 if (quot > 65535) { /* BRGR is 16-bit, so switch to slower clock */
Andrew Victor03abeac2007-05-03 12:26:24 +01001886 quot /= 8;
1887 mode |= ATMEL_US_USCLKS_MCK_DIV8;
1888 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001889
1890 /* byte size */
1891 switch (termios->c_cflag & CSIZE) {
1892 case CS5:
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001893 mode |= ATMEL_US_CHRL_5;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001894 break;
1895 case CS6:
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001896 mode |= ATMEL_US_CHRL_6;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001897 break;
1898 case CS7:
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001899 mode |= ATMEL_US_CHRL_7;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001900 break;
1901 default:
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001902 mode |= ATMEL_US_CHRL_8;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001903 break;
1904 }
1905
1906 /* stop bits */
1907 if (termios->c_cflag & CSTOPB)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001908 mode |= ATMEL_US_NBSTOP_2;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001909
1910 /* parity */
1911 if (termios->c_cflag & PARENB) {
Remy Bohmerb843aa22008-02-08 04:21:01 -08001912 /* Mark or Space parity */
1913 if (termios->c_cflag & CMSPAR) {
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001914 if (termios->c_cflag & PARODD)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001915 mode |= ATMEL_US_PAR_MARK;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001916 else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001917 mode |= ATMEL_US_PAR_SPACE;
Remy Bohmerb843aa22008-02-08 04:21:01 -08001918 } else if (termios->c_cflag & PARODD)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001919 mode |= ATMEL_US_PAR_ODD;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001920 else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001921 mode |= ATMEL_US_PAR_EVEN;
Remy Bohmerb843aa22008-02-08 04:21:01 -08001922 } else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001923 mode |= ATMEL_US_PAR_NONE;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001924
Peter Ma8e706c42009-03-31 10:31:02 -07001925 /* hardware handshake (RTS/CTS) */
1926 if (termios->c_cflag & CRTSCTS)
1927 mode |= ATMEL_US_USMODE_HWHS;
1928 else
1929 mode |= ATMEL_US_USMODE_NORMAL;
1930
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001931 spin_lock_irqsave(&port->lock, flags);
1932
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001933 port->read_status_mask = ATMEL_US_OVRE;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001934 if (termios->c_iflag & INPCK)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001935 port->read_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
Peter Hurleyef8b9dd2014-06-16 08:10:41 -04001936 if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001937 port->read_status_mask |= ATMEL_US_RXBRK;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001938
Elen Song64e22eb2013-07-22 16:30:24 +08001939 if (atmel_use_pdc_rx(port))
Chip Coldwella6670612008-02-08 04:21:06 -08001940 /* need to enable error interrupts */
1941 UART_PUT_IER(port, port->read_status_mask);
1942
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001943 /*
1944 * Characters to ignore
1945 */
1946 port->ignore_status_mask = 0;
1947 if (termios->c_iflag & IGNPAR)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001948 port->ignore_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001949 if (termios->c_iflag & IGNBRK) {
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001950 port->ignore_status_mask |= ATMEL_US_RXBRK;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001951 /*
1952 * If we're ignoring parity and break indicators,
1953 * ignore overruns too (for real raw support).
1954 */
1955 if (termios->c_iflag & IGNPAR)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001956 port->ignore_status_mask |= ATMEL_US_OVRE;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001957 }
Remy Bohmerb843aa22008-02-08 04:21:01 -08001958 /* TODO: Ignore all characters if CREAD is set.*/
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001959
1960 /* update the per-port timeout */
1961 uart_update_timeout(port, termios->c_cflag, baud);
1962
Haavard Skinnemoen0ccad872009-06-16 17:02:03 +01001963 /*
1964 * save/disable interrupts. The tty layer will ensure that the
1965 * transmitter is empty if requested by the caller, so there's
1966 * no need to wait for it here.
1967 */
Remy Bohmerb843aa22008-02-08 04:21:01 -08001968 imr = UART_GET_IMR(port);
1969 UART_PUT_IDR(port, -1);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001970
1971 /* disable receiver and transmitter */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001972 UART_PUT_CR(port, ATMEL_US_TXDIS | ATMEL_US_RXDIS);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001973
Claudio Scordinoe8faff72010-05-03 13:31:28 +01001974 /* Resetting serial mode to RS232 (0x0) */
1975 mode &= ~ATMEL_US_USMODE;
1976
1977 if (atmel_port->rs485.flags & SER_RS485_ENABLED) {
Claudio Scordino93f33502011-11-09 15:51:49 +01001978 if ((atmel_port->rs485.delay_rts_after_send) > 0)
Claudio Scordino1b633182010-07-20 15:26:47 -07001979 UART_PUT_TTGR(port,
1980 atmel_port->rs485.delay_rts_after_send);
Claudio Scordinoe8faff72010-05-03 13:31:28 +01001981 mode |= ATMEL_US_USMODE_RS485;
Claudio Scordinoe8faff72010-05-03 13:31:28 +01001982 }
1983
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001984 /* set the parity, stop bits and data size */
1985 UART_PUT_MR(port, mode);
1986
1987 /* set the baud rate */
1988 UART_PUT_BRGR(port, quot);
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001989 UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
1990 UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001991
1992 /* restore interrupts */
1993 UART_PUT_IER(port, imr);
1994
1995 /* CTS flow-control and modem-status interrupts */
1996 if (UART_ENABLE_MS(port, termios->c_cflag))
1997 port->ops->enable_ms(port);
1998
1999 spin_unlock_irqrestore(&port->lock, flags);
2000}
2001
Viktar Palstsiuk42bd7a42011-02-09 15:26:13 +01002002static void atmel_set_ldisc(struct uart_port *port, int new)
2003{
Jiri Slabyb54bf3b2011-11-09 21:33:44 +01002004 if (new == N_PPS) {
Viktar Palstsiuk42bd7a42011-02-09 15:26:13 +01002005 port->flags |= UPF_HARDPPS_CD;
2006 atmel_enable_ms(port);
2007 } else {
2008 port->flags &= ~UPF_HARDPPS_CD;
2009 }
2010}
2011
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002012/*
2013 * Return string describing the specified port
2014 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002015static const char *atmel_type(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002016{
Haavard Skinnemoen9ab4f882006-10-04 16:02:06 +02002017 return (port->type == PORT_ATMEL) ? "ATMEL_SERIAL" : NULL;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002018}
2019
2020/*
2021 * Release the memory region(s) being used by 'port'.
2022 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002023static void atmel_release_port(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002024{
Andrew Victorafefc412006-06-19 19:53:19 +01002025 struct platform_device *pdev = to_platform_device(port->dev);
2026 int size = pdev->resource[0].end - pdev->resource[0].start + 1;
2027
2028 release_mem_region(port->mapbase, size);
2029
2030 if (port->flags & UPF_IOREMAP) {
2031 iounmap(port->membase);
2032 port->membase = NULL;
2033 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002034}
2035
2036/*
2037 * Request the memory region(s) being used by 'port'.
2038 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002039static int atmel_request_port(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002040{
Andrew Victorafefc412006-06-19 19:53:19 +01002041 struct platform_device *pdev = to_platform_device(port->dev);
2042 int size = pdev->resource[0].end - pdev->resource[0].start + 1;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002043
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002044 if (!request_mem_region(port->mapbase, size, "atmel_serial"))
Andrew Victorafefc412006-06-19 19:53:19 +01002045 return -EBUSY;
2046
2047 if (port->flags & UPF_IOREMAP) {
2048 port->membase = ioremap(port->mapbase, size);
2049 if (port->membase == NULL) {
2050 release_mem_region(port->mapbase, size);
2051 return -ENOMEM;
2052 }
2053 }
2054
2055 return 0;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002056}
2057
2058/*
2059 * Configure/autoconfigure the port.
2060 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002061static void atmel_config_port(struct uart_port *port, int flags)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002062{
2063 if (flags & UART_CONFIG_TYPE) {
Haavard Skinnemoen9ab4f882006-10-04 16:02:06 +02002064 port->type = PORT_ATMEL;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002065 atmel_request_port(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002066 }
2067}
2068
2069/*
2070 * Verify the new serial_struct (for TIOCSSERIAL).
2071 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002072static int atmel_verify_port(struct uart_port *port, struct serial_struct *ser)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002073{
2074 int ret = 0;
Haavard Skinnemoen9ab4f882006-10-04 16:02:06 +02002075 if (ser->type != PORT_UNKNOWN && ser->type != PORT_ATMEL)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002076 ret = -EINVAL;
2077 if (port->irq != ser->irq)
2078 ret = -EINVAL;
2079 if (ser->io_type != SERIAL_IO_MEM)
2080 ret = -EINVAL;
2081 if (port->uartclk / 16 != ser->baud_base)
2082 ret = -EINVAL;
2083 if ((void *)port->mapbase != ser->iomem_base)
2084 ret = -EINVAL;
2085 if (port->iobase != ser->port)
2086 ret = -EINVAL;
2087 if (ser->hub6 != 0)
2088 ret = -EINVAL;
2089 return ret;
2090}
2091
Albin Tonnerre8fe2d542009-12-09 12:31:32 -08002092#ifdef CONFIG_CONSOLE_POLL
2093static int atmel_poll_get_char(struct uart_port *port)
2094{
2095 while (!(UART_GET_CSR(port) & ATMEL_US_RXRDY))
2096 cpu_relax();
2097
2098 return UART_GET_CHAR(port);
2099}
2100
2101static void atmel_poll_put_char(struct uart_port *port, unsigned char ch)
2102{
2103 while (!(UART_GET_CSR(port) & ATMEL_US_TXRDY))
2104 cpu_relax();
2105
2106 UART_PUT_CHAR(port, ch);
2107}
2108#endif
2109
Claudio Scordinoe8faff72010-05-03 13:31:28 +01002110static int
2111atmel_ioctl(struct uart_port *port, unsigned int cmd, unsigned long arg)
2112{
2113 struct serial_rs485 rs485conf;
2114
2115 switch (cmd) {
2116 case TIOCSRS485:
2117 if (copy_from_user(&rs485conf, (struct serial_rs485 *) arg,
2118 sizeof(rs485conf)))
2119 return -EFAULT;
2120
2121 atmel_config_rs485(port, &rs485conf);
2122 break;
2123
2124 case TIOCGRS485:
2125 if (copy_to_user((struct serial_rs485 *) arg,
2126 &(to_atmel_uart_port(port)->rs485),
2127 sizeof(rs485conf)))
2128 return -EFAULT;
2129 break;
2130
2131 default:
2132 return -ENOIOCTLCMD;
2133 }
2134 return 0;
2135}
2136
2137
2138
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002139static struct uart_ops atmel_pops = {
2140 .tx_empty = atmel_tx_empty,
2141 .set_mctrl = atmel_set_mctrl,
2142 .get_mctrl = atmel_get_mctrl,
2143 .stop_tx = atmel_stop_tx,
2144 .start_tx = atmel_start_tx,
2145 .stop_rx = atmel_stop_rx,
2146 .enable_ms = atmel_enable_ms,
2147 .break_ctl = atmel_break_ctl,
2148 .startup = atmel_startup,
2149 .shutdown = atmel_shutdown,
Haavard Skinnemoen9afd5612008-07-16 21:52:46 +01002150 .flush_buffer = atmel_flush_buffer,
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002151 .set_termios = atmel_set_termios,
Viktar Palstsiuk42bd7a42011-02-09 15:26:13 +01002152 .set_ldisc = atmel_set_ldisc,
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002153 .type = atmel_type,
2154 .release_port = atmel_release_port,
2155 .request_port = atmel_request_port,
2156 .config_port = atmel_config_port,
2157 .verify_port = atmel_verify_port,
2158 .pm = atmel_serial_pm,
Claudio Scordinoe8faff72010-05-03 13:31:28 +01002159 .ioctl = atmel_ioctl,
Albin Tonnerre8fe2d542009-12-09 12:31:32 -08002160#ifdef CONFIG_CONSOLE_POLL
2161 .poll_get_char = atmel_poll_get_char,
2162 .poll_put_char = atmel_poll_put_char,
2163#endif
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002164};
2165
Andrew Victorafefc412006-06-19 19:53:19 +01002166/*
2167 * Configure the port from the platform device resource info.
2168 */
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002169static int atmel_init_port(struct atmel_uart_port *atmel_port,
Remy Bohmerb843aa22008-02-08 04:21:01 -08002170 struct platform_device *pdev)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002171{
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002172 int ret;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002173 struct uart_port *port = &atmel_port->uart;
Jingoo Han574de552013-07-30 17:06:57 +09002174 struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002175
Elen Song33d64c42013-07-22 16:30:28 +08002176 if (!atmel_init_property(atmel_port, pdev))
2177 atmel_set_ops(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002178
Elen Song33d64c42013-07-22 16:30:28 +08002179 atmel_init_rs485(atmel_port, pdev);
Elen Songa930e522013-07-22 16:30:25 +08002180
Claudio Scordinoe8faff72010-05-03 13:31:28 +01002181 port->iotype = UPIO_MEM;
2182 port->flags = UPF_BOOT_AUTOCONF;
2183 port->ops = &atmel_pops;
2184 port->fifosize = 1;
Claudio Scordinoe8faff72010-05-03 13:31:28 +01002185 port->dev = &pdev->dev;
Andrew Victorafefc412006-06-19 19:53:19 +01002186 port->mapbase = pdev->resource[0].start;
2187 port->irq = pdev->resource[1].start;
2188
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08002189 tasklet_init(&atmel_port->tasklet, atmel_tasklet_func,
2190 (unsigned long)port);
2191
2192 memset(&atmel_port->rx_ring, 0, sizeof(atmel_port->rx_ring));
2193
Nicolas Ferre5fbe46b2011-10-12 18:07:00 +02002194 if (pdata && pdata->regs) {
Haavard Skinnemoen75d35212006-10-04 16:02:08 +02002195 /* Already mapped by setup code */
Nicolas Ferre1acfc7e2011-10-12 18:06:57 +02002196 port->membase = pdata->regs;
Nicolas Ferre588edbf2011-10-12 18:06:58 +02002197 } else {
Andrew Victorafefc412006-06-19 19:53:19 +01002198 port->flags |= UPF_IOREMAP;
2199 port->membase = NULL;
2200 }
2201
Remy Bohmerb843aa22008-02-08 04:21:01 -08002202 /* for console, the clock could already be configured */
2203 if (!atmel_port->clk) {
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002204 atmel_port->clk = clk_get(&pdev->dev, "usart");
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002205 if (IS_ERR(atmel_port->clk)) {
2206 ret = PTR_ERR(atmel_port->clk);
2207 atmel_port->clk = NULL;
2208 return ret;
2209 }
2210 ret = clk_prepare_enable(atmel_port->clk);
2211 if (ret) {
2212 clk_put(atmel_port->clk);
2213 atmel_port->clk = NULL;
2214 return ret;
2215 }
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002216 port->uartclk = clk_get_rate(atmel_port->clk);
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002217 clk_disable_unprepare(atmel_port->clk);
David Brownell06a7f052008-11-06 12:53:40 -08002218 /* only enable clock when USART is in use */
Andrew Victorafefc412006-06-19 19:53:19 +01002219 }
Chip Coldwella6670612008-02-08 04:21:06 -08002220
Claudio Scordinoe8faff72010-05-03 13:31:28 +01002221 /* Use TXEMPTY for interrupt when rs485 else TXRDY or ENDTX|TXBUFE */
2222 if (atmel_port->rs485.flags & SER_RS485_ENABLED)
2223 atmel_port->tx_done_mask = ATMEL_US_TXEMPTY;
Elen Song64e22eb2013-07-22 16:30:24 +08002224 else if (atmel_use_pdc_tx(port)) {
Chip Coldwella6670612008-02-08 04:21:06 -08002225 port->fifosize = PDC_BUFFER_SIZE;
Claudio Scordinoe8faff72010-05-03 13:31:28 +01002226 atmel_port->tx_done_mask = ATMEL_US_ENDTX | ATMEL_US_TXBUFE;
2227 } else {
2228 atmel_port->tx_done_mask = ATMEL_US_TXRDY;
2229 }
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002230
2231 return 0;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002232}
2233
Jean-Christophe PLAGNIOL-VILLARD69f6a272012-02-16 00:24:07 +08002234struct platform_device *atmel_default_console_device; /* the serial console device */
2235
Haavard Skinnemoen749c4e62006-10-04 16:02:02 +02002236#ifdef CONFIG_SERIAL_ATMEL_CONSOLE
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002237static void atmel_console_putchar(struct uart_port *port, int ch)
Russell Kingd3587882006-03-20 20:00:09 +00002238{
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002239 while (!(UART_GET_CSR(port) & ATMEL_US_TXRDY))
Haavard Skinnemoen829dd812008-02-08 04:21:02 -08002240 cpu_relax();
Russell Kingd3587882006-03-20 20:00:09 +00002241 UART_PUT_CHAR(port, ch);
2242}
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002243
2244/*
2245 * Interrupts are disabled on entering
2246 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002247static void atmel_console_write(struct console *co, const char *s, u_int count)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002248{
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002249 struct uart_port *port = &atmel_ports[co->index].uart;
Claudio Scordinoe8faff72010-05-03 13:31:28 +01002250 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Russell Kingd3587882006-03-20 20:00:09 +00002251 unsigned int status, imr;
Marc Pignat39d4c922008-04-02 13:04:42 -07002252 unsigned int pdc_tx;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002253
2254 /*
Remy Bohmerb843aa22008-02-08 04:21:01 -08002255 * First, save IMR and then disable interrupts
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002256 */
Remy Bohmerb843aa22008-02-08 04:21:01 -08002257 imr = UART_GET_IMR(port);
Claudio Scordinoe8faff72010-05-03 13:31:28 +01002258 UART_PUT_IDR(port, ATMEL_US_RXRDY | atmel_port->tx_done_mask);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002259
Marc Pignat39d4c922008-04-02 13:04:42 -07002260 /* Store PDC transmit status and disable it */
2261 pdc_tx = UART_GET_PTSR(port) & ATMEL_PDC_TXTEN;
2262 UART_PUT_PTCR(port, ATMEL_PDC_TXTDIS);
2263
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002264 uart_console_write(port, s, count, atmel_console_putchar);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002265
2266 /*
Remy Bohmerb843aa22008-02-08 04:21:01 -08002267 * Finally, wait for transmitter to become empty
2268 * and restore IMR
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002269 */
2270 do {
2271 status = UART_GET_CSR(port);
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002272 } while (!(status & ATMEL_US_TXRDY));
Marc Pignat39d4c922008-04-02 13:04:42 -07002273
2274 /* Restore PDC transmit status */
2275 if (pdc_tx)
2276 UART_PUT_PTCR(port, ATMEL_PDC_TXTEN);
2277
Remy Bohmerb843aa22008-02-08 04:21:01 -08002278 /* set interrupts back the way they were */
2279 UART_PUT_IER(port, imr);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002280}
2281
2282/*
Remy Bohmerb843aa22008-02-08 04:21:01 -08002283 * If the port was already initialised (eg, by a boot loader),
2284 * try to determine the current setup.
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002285 */
Remy Bohmerb843aa22008-02-08 04:21:01 -08002286static void __init atmel_console_get_options(struct uart_port *port, int *baud,
2287 int *parity, int *bits)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002288{
2289 unsigned int mr, quot;
2290
Haavard Skinnemoen1c0fd822008-02-08 04:21:03 -08002291 /*
2292 * If the baud rate generator isn't running, the port wasn't
2293 * initialized by the boot loader.
2294 */
Haavard Skinnemoen9c81c5c2008-06-05 22:46:39 -07002295 quot = UART_GET_BRGR(port) & ATMEL_US_CD;
Haavard Skinnemoen1c0fd822008-02-08 04:21:03 -08002296 if (!quot)
2297 return;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002298
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002299 mr = UART_GET_MR(port) & ATMEL_US_CHRL;
2300 if (mr == ATMEL_US_CHRL_8)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002301 *bits = 8;
2302 else
2303 *bits = 7;
2304
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002305 mr = UART_GET_MR(port) & ATMEL_US_PAR;
2306 if (mr == ATMEL_US_PAR_EVEN)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002307 *parity = 'e';
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002308 else if (mr == ATMEL_US_PAR_ODD)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002309 *parity = 'o';
2310
Haavard Skinnemoen4d5e3922006-10-04 16:02:11 +02002311 /*
2312 * The serial core only rounds down when matching this to a
2313 * supported baud rate. Make sure we don't end up slightly
2314 * lower than one of those, as it would make us fall through
2315 * to a much lower baud rate than we really want.
2316 */
Haavard Skinnemoen4d5e3922006-10-04 16:02:11 +02002317 *baud = port->uartclk / (16 * (quot - 1));
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002318}
2319
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002320static int __init atmel_console_setup(struct console *co, char *options)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002321{
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002322 int ret;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002323 struct uart_port *port = &atmel_ports[co->index].uart;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002324 int baud = 115200;
2325 int bits = 8;
2326 int parity = 'n';
2327 int flow = 'n';
2328
Remy Bohmerb843aa22008-02-08 04:21:01 -08002329 if (port->membase == NULL) {
2330 /* Port not initialized yet - delay setup */
Andrew Victorafefc412006-06-19 19:53:19 +01002331 return -ENODEV;
Remy Bohmerb843aa22008-02-08 04:21:01 -08002332 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002333
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002334 ret = clk_prepare_enable(atmel_ports[co->index].clk);
2335 if (ret)
2336 return ret;
David Brownell06a7f052008-11-06 12:53:40 -08002337
Remy Bohmerb843aa22008-02-08 04:21:01 -08002338 UART_PUT_IDR(port, -1);
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002339 UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
2340 UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002341
2342 if (options)
2343 uart_parse_options(options, &baud, &parity, &bits, &flow);
2344 else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002345 atmel_console_get_options(port, &baud, &parity, &bits);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002346
2347 return uart_set_options(port, co, baud, parity, bits, flow);
2348}
2349
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002350static struct uart_driver atmel_uart;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002351
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002352static struct console atmel_console = {
2353 .name = ATMEL_DEVICENAME,
2354 .write = atmel_console_write,
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002355 .device = uart_console_device,
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002356 .setup = atmel_console_setup,
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002357 .flags = CON_PRINTBUFFER,
2358 .index = -1,
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002359 .data = &atmel_uart,
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002360};
2361
David Brownell06a7f052008-11-06 12:53:40 -08002362#define ATMEL_CONSOLE_DEVICE (&atmel_console)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002363
Andrew Victorafefc412006-06-19 19:53:19 +01002364/*
2365 * Early console initialization (before VM subsystem initialized).
2366 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002367static int __init atmel_console_init(void)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002368{
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002369 int ret;
Haavard Skinnemoen73e27982006-10-04 16:02:04 +02002370 if (atmel_default_console_device) {
Voss, Nikolaus0d0a3cc2011-08-10 14:02:29 +02002371 struct atmel_uart_data *pdata =
Jingoo Han574de552013-07-30 17:06:57 +09002372 dev_get_platdata(&atmel_default_console_device->dev);
Linus Torvaldsefb8d212011-10-26 15:11:09 +02002373 int id = pdata->num;
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002374 struct atmel_uart_port *port = &atmel_ports[id];
Voss, Nikolaus0d0a3cc2011-08-10 14:02:29 +02002375
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002376 port->backup_imr = 0;
2377 port->uart.line = id;
2378
2379 add_preferred_console(ATMEL_DEVICENAME, id, NULL);
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002380 ret = atmel_init_port(port, atmel_default_console_device);
2381 if (ret)
2382 return ret;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002383 register_console(&atmel_console);
Andrew Victorafefc412006-06-19 19:53:19 +01002384 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002385
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002386 return 0;
2387}
Remy Bohmerb843aa22008-02-08 04:21:01 -08002388
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002389console_initcall(atmel_console_init);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002390
Andrew Victorafefc412006-06-19 19:53:19 +01002391/*
2392 * Late console initialization.
2393 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002394static int __init atmel_late_console_init(void)
Andrew Victorafefc412006-06-19 19:53:19 +01002395{
Remy Bohmerb843aa22008-02-08 04:21:01 -08002396 if (atmel_default_console_device
2397 && !(atmel_console.flags & CON_ENABLED))
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002398 register_console(&atmel_console);
Andrew Victorafefc412006-06-19 19:53:19 +01002399
2400 return 0;
2401}
Remy Bohmerb843aa22008-02-08 04:21:01 -08002402
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002403core_initcall(atmel_late_console_init);
Andrew Victorafefc412006-06-19 19:53:19 +01002404
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002405static inline bool atmel_is_console_port(struct uart_port *port)
2406{
2407 return port->cons && port->cons->index == port->line;
2408}
2409
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002410#else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002411#define ATMEL_CONSOLE_DEVICE NULL
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002412
2413static inline bool atmel_is_console_port(struct uart_port *port)
2414{
2415 return false;
2416}
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002417#endif
2418
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002419static struct uart_driver atmel_uart = {
Remy Bohmerb843aa22008-02-08 04:21:01 -08002420 .owner = THIS_MODULE,
2421 .driver_name = "atmel_serial",
2422 .dev_name = ATMEL_DEVICENAME,
2423 .major = SERIAL_ATMEL_MAJOR,
2424 .minor = MINOR_START,
2425 .nr = ATMEL_MAX_UART,
2426 .cons = ATMEL_CONSOLE_DEVICE,
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002427};
2428
Andrew Victorafefc412006-06-19 19:53:19 +01002429#ifdef CONFIG_PM
Haavard Skinnemoenf826caa2008-02-24 14:34:45 +01002430static bool atmel_serial_clk_will_stop(void)
2431{
2432#ifdef CONFIG_ARCH_AT91
2433 return at91_suspend_entering_slow_clock();
2434#else
2435 return false;
2436#endif
2437}
2438
Remy Bohmerb843aa22008-02-08 04:21:01 -08002439static int atmel_serial_suspend(struct platform_device *pdev,
2440 pm_message_t state)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002441{
Andrew Victorafefc412006-06-19 19:53:19 +01002442 struct uart_port *port = platform_get_drvdata(pdev);
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08002443 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002444
Haavard Skinnemoene1c609e2008-03-14 14:54:13 +01002445 if (atmel_is_console_port(port) && console_suspend_enabled) {
2446 /* Drain the TX shifter */
2447 while (!(UART_GET_CSR(port) & ATMEL_US_TXEMPTY))
2448 cpu_relax();
2449 }
2450
Anti Sullinf05596d2008-09-22 13:57:54 -07002451 /* we can not wake up if we're running on slow clock */
2452 atmel_port->may_wakeup = device_may_wakeup(&pdev->dev);
2453 if (atmel_serial_clk_will_stop())
2454 device_set_wakeup_enable(&pdev->dev, 0);
2455
2456 uart_suspend_port(&atmel_uart, port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002457
2458 return 0;
2459}
2460
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002461static int atmel_serial_resume(struct platform_device *pdev)
Andrew Victorafefc412006-06-19 19:53:19 +01002462{
2463 struct uart_port *port = platform_get_drvdata(pdev);
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08002464 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Andrew Victorafefc412006-06-19 19:53:19 +01002465
Anti Sullinf05596d2008-09-22 13:57:54 -07002466 uart_resume_port(&atmel_uart, port);
2467 device_set_wakeup_enable(&pdev->dev, atmel_port->may_wakeup);
Andrew Victorafefc412006-06-19 19:53:19 +01002468
2469 return 0;
2470}
2471#else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002472#define atmel_serial_suspend NULL
2473#define atmel_serial_resume NULL
Andrew Victorafefc412006-06-19 19:53:19 +01002474#endif
2475
Richard Genoude0b0baa2014-05-13 20:20:44 +02002476static int atmel_init_gpios(struct atmel_uart_port *p, struct device *dev)
2477{
Richard Genoudab5e4e42014-05-13 20:20:45 +02002478 enum mctrl_gpio_idx i;
2479 struct gpio_desc *gpiod;
2480
Richard Genoude0b0baa2014-05-13 20:20:44 +02002481 p->gpios = mctrl_gpio_init(dev, 0);
2482 if (IS_ERR_OR_NULL(p->gpios))
2483 return -1;
2484
Richard Genoudab5e4e42014-05-13 20:20:45 +02002485 for (i = 0; i < UART_GPIO_MAX; i++) {
2486 gpiod = mctrl_gpio_to_gpiod(p->gpios, i);
2487 if (gpiod && (gpiod_get_direction(gpiod) == GPIOF_DIR_IN))
2488 p->gpio_irq[i] = gpiod_to_irq(gpiod);
2489 else
2490 p->gpio_irq[i] = -EINVAL;
2491 }
2492
Richard Genoude0b0baa2014-05-13 20:20:44 +02002493 return 0;
2494}
2495
Bill Pemberton9671f092012-11-19 13:21:50 -05002496static int atmel_serial_probe(struct platform_device *pdev)
Andrew Victorafefc412006-06-19 19:53:19 +01002497{
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002498 struct atmel_uart_port *port;
Nicolas Ferre5fbe46b2011-10-12 18:07:00 +02002499 struct device_node *np = pdev->dev.of_node;
Jingoo Han574de552013-07-30 17:06:57 +09002500 struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08002501 void *data;
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002502 int ret = -ENODEV;
Andrew Victorafefc412006-06-19 19:53:19 +01002503
Haavard Skinnemoen9d09daf2009-10-26 16:50:02 -07002504 BUILD_BUG_ON(ATMEL_SERIAL_RINGSIZE & (ATMEL_SERIAL_RINGSIZE - 1));
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08002505
Nicolas Ferre5fbe46b2011-10-12 18:07:00 +02002506 if (np)
2507 ret = of_alias_get_id(np, "serial");
2508 else
2509 if (pdata)
2510 ret = pdata->num;
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002511
2512 if (ret < 0)
Nicolas Ferre5fbe46b2011-10-12 18:07:00 +02002513 /* port id not found in platform data nor device-tree aliases:
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002514 * auto-enumerate it */
Pawel Wieczorkiewicz503bded2013-02-20 17:26:20 +01002515 ret = find_first_zero_bit(atmel_ports_in_use, ATMEL_MAX_UART);
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002516
Pawel Wieczorkiewicz503bded2013-02-20 17:26:20 +01002517 if (ret >= ATMEL_MAX_UART) {
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002518 ret = -ENODEV;
2519 goto err;
2520 }
2521
Pawel Wieczorkiewicz503bded2013-02-20 17:26:20 +01002522 if (test_and_set_bit(ret, atmel_ports_in_use)) {
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002523 /* port already in use */
2524 ret = -EBUSY;
2525 goto err;
2526 }
2527
2528 port = &atmel_ports[ret];
Anti Sullinf05596d2008-09-22 13:57:54 -07002529 port->backup_imr = 0;
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002530 port->uart.line = ret;
Linus Walleij354e57f2013-11-07 10:25:55 +01002531
Richard Genoude0b0baa2014-05-13 20:20:44 +02002532 ret = atmel_init_gpios(port, &pdev->dev);
2533 if (ret < 0)
2534 dev_err(&pdev->dev, "%s",
2535 "Failed to initialize GPIOs. The serial port may not work as expected");
Anti Sullinf05596d2008-09-22 13:57:54 -07002536
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002537 ret = atmel_init_port(port, pdev);
2538 if (ret)
2539 goto err;
Andrew Victorafefc412006-06-19 19:53:19 +01002540
Elen Song64e22eb2013-07-22 16:30:24 +08002541 if (!atmel_use_pdc_rx(&port->uart)) {
Chip Coldwella6670612008-02-08 04:21:06 -08002542 ret = -ENOMEM;
Haavard Skinnemoen64334712008-02-08 04:21:07 -08002543 data = kmalloc(sizeof(struct atmel_uart_char)
2544 * ATMEL_SERIAL_RINGSIZE, GFP_KERNEL);
Chip Coldwella6670612008-02-08 04:21:06 -08002545 if (!data)
2546 goto err_alloc_ring;
2547 port->rx_ring.buf = data;
2548 }
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08002549
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002550 ret = uart_add_one_port(&atmel_uart, &port->uart);
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002551 if (ret)
2552 goto err_add_port;
2553
Albin Tonnerre8da14b52009-07-29 15:04:18 -07002554#ifdef CONFIG_SERIAL_ATMEL_CONSOLE
David Brownell06a7f052008-11-06 12:53:40 -08002555 if (atmel_is_console_port(&port->uart)
2556 && ATMEL_CONSOLE_DEVICE->flags & CON_ENABLED) {
2557 /*
2558 * The serial core enabled the clock for us, so undo
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002559 * the clk_prepare_enable() in atmel_console_setup()
David Brownell06a7f052008-11-06 12:53:40 -08002560 */
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002561 clk_disable_unprepare(port->clk);
David Brownell06a7f052008-11-06 12:53:40 -08002562 }
Albin Tonnerre8da14b52009-07-29 15:04:18 -07002563#endif
David Brownell06a7f052008-11-06 12:53:40 -08002564
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002565 device_init_wakeup(&pdev->dev, 1);
2566 platform_set_drvdata(pdev, port);
2567
Claudio Scordino5dfbd1d72011-01-13 15:45:39 -08002568 if (port->rs485.flags & SER_RS485_ENABLED) {
2569 UART_PUT_MR(&port->uart, ATMEL_US_USMODE_NORMAL);
2570 UART_PUT_CR(&port->uart, ATMEL_US_RTSEN);
2571 }
2572
Elen Song055560b2013-07-22 16:30:29 +08002573 /*
2574 * Get port name of usart or uart
2575 */
Nicolas Ferre892db582013-10-17 17:37:11 +02002576 atmel_get_ip_name(&port->uart);
Elen Song055560b2013-07-22 16:30:29 +08002577
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002578 return 0;
2579
2580err_add_port:
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08002581 kfree(port->rx_ring.buf);
2582 port->rx_ring.buf = NULL;
2583err_alloc_ring:
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002584 if (!atmel_is_console_port(&port->uart)) {
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002585 clk_put(port->clk);
2586 port->clk = NULL;
Andrew Victorafefc412006-06-19 19:53:19 +01002587 }
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002588err:
Andrew Victorafefc412006-06-19 19:53:19 +01002589 return ret;
2590}
2591
Bill Pembertonae8d8a12012-11-19 13:26:18 -05002592static int atmel_serial_remove(struct platform_device *pdev)
Andrew Victorafefc412006-06-19 19:53:19 +01002593{
2594 struct uart_port *port = platform_get_drvdata(pdev);
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08002595 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Andrew Victorafefc412006-06-19 19:53:19 +01002596 int ret = 0;
2597
Marek Roszkof50c995f2014-01-07 11:45:07 +01002598 tasklet_kill(&atmel_port->tasklet);
2599
Andrew Victorafefc412006-06-19 19:53:19 +01002600 device_init_wakeup(&pdev->dev, 0);
Andrew Victorafefc412006-06-19 19:53:19 +01002601
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002602 ret = uart_remove_one_port(&atmel_uart, port);
2603
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08002604 kfree(atmel_port->rx_ring.buf);
2605
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002606 /* "port" is allocated statically, so we shouldn't free it */
2607
Pawel Wieczorkiewicz503bded2013-02-20 17:26:20 +01002608 clear_bit(port->line, atmel_ports_in_use);
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002609
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002610 clk_put(atmel_port->clk);
Andrew Victorafefc412006-06-19 19:53:19 +01002611
2612 return ret;
2613}
2614
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002615static struct platform_driver atmel_serial_driver = {
2616 .probe = atmel_serial_probe,
Bill Pemberton2d47b712012-11-19 13:21:34 -05002617 .remove = atmel_serial_remove,
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002618 .suspend = atmel_serial_suspend,
2619 .resume = atmel_serial_resume,
Andrew Victorafefc412006-06-19 19:53:19 +01002620 .driver = {
Haavard Skinnemoen1e8ea802006-10-04 16:02:03 +02002621 .name = "atmel_usart",
Andrew Victorafefc412006-06-19 19:53:19 +01002622 .owner = THIS_MODULE,
Nicolas Ferre5fbe46b2011-10-12 18:07:00 +02002623 .of_match_table = of_match_ptr(atmel_serial_dt_ids),
Andrew Victorafefc412006-06-19 19:53:19 +01002624 },
2625};
2626
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002627static int __init atmel_serial_init(void)
Andrew Victorafefc412006-06-19 19:53:19 +01002628{
2629 int ret;
2630
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002631 ret = uart_register_driver(&atmel_uart);
Andrew Victorafefc412006-06-19 19:53:19 +01002632 if (ret)
2633 return ret;
2634
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002635 ret = platform_driver_register(&atmel_serial_driver);
Andrew Victorafefc412006-06-19 19:53:19 +01002636 if (ret)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002637 uart_unregister_driver(&atmel_uart);
Andrew Victorafefc412006-06-19 19:53:19 +01002638
2639 return ret;
2640}
2641
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002642static void __exit atmel_serial_exit(void)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002643{
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002644 platform_driver_unregister(&atmel_serial_driver);
2645 uart_unregister_driver(&atmel_uart);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002646}
2647
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002648module_init(atmel_serial_init);
2649module_exit(atmel_serial_exit);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002650
2651MODULE_AUTHOR("Rick Bronson");
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002652MODULE_DESCRIPTION("Atmel AT91 / AT32 serial port driver");
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002653MODULE_LICENSE("GPL");
Kay Sieverse169c132008-04-15 14:34:35 -07002654MODULE_ALIAS("platform:atmel_usart");